network-address 0.2.0 → 0.3.0
raw patch · 6 files changed
+554/−134 lines, 6 filesdep +deepseqdep −Cabaldep ~QuickCheckdep ~criteriondep ~test-framework
Dependencies added: deepseq
Dependencies removed: Cabal
Dependency ranges changed: QuickCheck, criterion, test-framework, test-framework-quickcheck2
Files
- Benchmark.hs +30/−33
- Data/Network/Address/IP.hs +137/−86
- README.md +104/−0
- Test.hs +8/−0
- Test/Data/Network/Address/IP.hs +256/−0
- network-address.cabal +19/−15
Benchmark.hs view
@@ -1,50 +1,47 @@+{-# LANGUAGE BangPatterns #-} module Main where+import Control.DeepSeq (NFData, rnf) import Criterion.Main import Data.Network.Address -ipv4str :: Integral a => a -> String-ipv4str n = "192.168." ++ show (n `div` 255)- ++ "."- ++ show (n `mod` 255)+instance NFData IPv4 where+ rnf (IPv4 ip) = rnf ip +instance NFData IPv6 where+ rnf (IPv6 a b) = rnf a `seq` rnf b -ipv4 :: IPv4-ipv4 = toAddress 3232235777+instance (Address a, NFData a) => NFData (IPSubnet a) where+ rnf (IPSubnet ip m) = rnf ip `seq` rnf m -ipv6 :: IPv6-ipv6 = toAddress 42540766452641154071740215577757643572 -ipv4subnet :: IPSubnet IPv4-ipv4subnet = IPSubnet ipv4 (toMask (8 :: Int))--ipv6subnet :: IPSubnet IPv6-ipv6subnet = IPSubnet ipv6 (toMask (56 :: Integer))--main = defaultMain+main =+ let ipv4 :: IPv4+ !ipv4 = toAddress 3232235777+ ipv6 :: IPv6+ !ipv6 = toAddress 42540766452641154071740215577757643572+ ipv4subnet :: IPSubnet IPv4+ !ipv4subnet = readSubnet "192.168.1.1/8"+ ipv6subnet :: IPSubnet IPv6+ !ipv6subnet = readSubnet "2001:0db8:85a3:0:0:8a2e:0370:7334/56"+ in defaultMain [ bgroup "IPv4"- [ bgroup "Parse"- [ bench "read" $ whnf (readAddress :: String -> IPv4) "192.168.1.1"- ]- , bgroup "PrettyPrint"- [ bench "show" $ whnf showAddress ipv4- ]+ [ bench "readAddress" $ nf (readAddress :: String -> IPv4) "192.168.1.1"+ , bench "showAddress" $ nf showAddress ipv4 ] , bgroup "IPv6"- [ bgroup "Parse"- [ bench "read" $ whnf (readAddress :: String -> IPv6) "2001:0db8:85a3:0:0:8a2e:0370:7334"- ]- , bgroup "PrettyPrint"- [ bench "show" $ whnf showAddress ipv6- ]+ [ bench "readAddress" $ nf (readAddress :: String -> IPv6) "2001:0db8:85a3:0:0:8a2e:0370:7334"+ , bench "showAddress" $ nf showAddress ipv6 ] , bgroup "Subnet"- [ bgroup "Parse"- [ bench "readIPv4" $ whnf (readSubnet :: String -> IPSubnet IPv4) "192.168.1.10/8"- , bench "readIPv6" $ whnf (readSubnet :: String -> IPSubnet IPv6) "2001:0db8:85a3:0:0:8a2e:0370:7334/56"+ [ bgroup "IPv4"+ [ bench "readSubnet" $ nf (readSubnet :: String -> IPSubnet IPv4) "192.168.1.10/8"+ , bench "showSubnet" $ nf showSubnet ipv4subnet+ , bench "member" $ nf (member ipv4) ipv4subnet ]- , bgroup "PrettyPrint"- [ bench "showIPv4" $ whnf showSubnet ipv4subnet- , bench "showIPv6" $ whnf showSubnet ipv6subnet+ , bgroup "IPv6"+ [ bench "readSubnet" $ nf (readSubnet :: String -> IPSubnet IPv6) "2001:0db8:85a3:0:0:8a2e:0370:7334/56"+ , bench "showSubnet" $ nf showSubnet ipv6subnet+ , bench "member" $ nf (member ipv6) ipv6subnet ] ] ]
Data/Network/Address/IP.hs view
@@ -33,17 +33,16 @@ IPv4(..), IPv6(..), IPSubnet(..),- Mask,- fromMask,+ Prefix, readAddress, readSubnet,- toMask,+ ipSubnetForPrefix ) where -import Control.Monad (when)+import Control.Monad (liftM2) import Data.Bits import Data.Char (digitToInt, isDigit)-import Data.List (foldl', intercalate, findIndex, groupBy)+import Data.List (foldl', findIndex, isSuffixOf, scanl) import Data.Maybe (fromJust) import Data.Word import Numeric (showHex)@@ -51,8 +50,9 @@ import Text.Printf (printf) import Text.Read.Lex (readDecP, readHexP) --- |The byte representation of an IP network mask.-type Mask = Integer+-- |The number of leading bits that identify a network, e.g. the 8 in+-- @10.0.0.0/8@.+type Prefix = Integer -- |The abstract data structure to represent an IPv4 address. data IPv4 = IPv4 !Word32@@ -62,8 +62,8 @@ data IPv6 = IPv6 !Word64 !Word64 deriving (Eq, Ord, Show, Read) --- |The abstract data structure to represent an IPv4 subnetwork.-data (Address a) => IPSubnet a = IPSubnet a Mask deriving (Eq, Ord, Show, Read)+-- |The abstract data structure to represent an IP subnetwork.+data (Address a) => IPSubnet a = IPSubnet a Prefix deriving (Eq, Ord, Show, Read) class (Eq a) => Address a where -- |Convert the byte representation to an IP 'Address'.@@ -74,10 +74,9 @@ readsAddress :: ReadS a -- |Return a conanical textual representation of an IP 'Address'. showAddress :: a -> String- -- |Apply a mask to an 'Address'.- maskAddress :: (Bits b, Integral b) => a -> b -> a- maskAddress ip mask = toAddress . (\x -> x `div` (2^n) * 2^n) . fromAddress $ ip- where n = fromMask mask+ -- |Return the largest 'Prefix' for an IP 'Address', which is its+ -- length in bits. The argument is not evaluated.+ maxPrefix :: a -> Prefix -- |The 'Subnet' class is used to perform operations on and manipulate -- IP subnetworks.@@ -88,11 +87,17 @@ showSubnet :: s -> String -- |Return the first 'IP' in the 'Subnet'. base :: s -> a- -- |Return an binary representation of the subnet mask.- netmask :: s -> Mask+ -- |Return the 'Prefix' length of the 'Subnet'.+ prefix :: s -> Prefix+ -- |Return the subnet mask as an 'Address', e.g. @255.0.0.0@ for+ -- @10.0.0.0/8@.+ netmask :: s -> a+ netmask s = toAddress $ prefixMask (maxPrefix (base s)) (prefix s) -- |Determine if an 'IP' is within the range of a 'Subnet'. member :: a -> s -> Bool- member ip s = fromAddress ip .&. netmask s == fromAddress (base s)+ member ip s = networkBits ip == networkBits (base s)+ where networkBits x = fromAddress x `shiftR` hostBits+ hostBits = fromInteger $ maxPrefix ip - prefix s -- |@readAddress s@ parses an 'Address'. The 'String' @s@ must be -- completely consumed.@@ -104,7 +109,7 @@ -- |@readSubnet s@ parses a 'Subnet'. The 'String' @s@ must be -- completely consumed.-readSubnet :: (Address a, Subnet s a) => String -> s+readSubnet :: (Subnet s a) => String -> s readSubnet s = case [x | (x, "") <- readsSubnet s] of [ip] -> ip [] -> error "readSubnet: no parse"@@ -118,12 +123,23 @@ fromAddress (IPv4 x) = toInteger x toAddress = IPv4 . fromInteger readsAddress = readsIPv4- showAddress = showIPv4+ showAddress ip = showsIPv4 ip ""+ maxPrefix _ = 32 +instance Address Word32 where+ fromAddress = toInteger+ toAddress = fromInteger+ readsAddress = map (\(IPv4 x, s) -> (x, s)) . readsIPv4+ showAddress ip = showsIPv4 (IPv4 ip) ""+ maxPrefix _ = 32+ -- |Return a conanical textual representation of an IPv4 IP address, -- e.g. @127.0.0.1@-showIPv4 :: IPv4 -> String-showIPv4 (IPv4 ip) = printf "%d.%d.%d.%d" a b c d+showsIPv4 :: IPv4 -> ShowS+showsIPv4 (IPv4 ip) = shows a+ . showChar '.' . shows b+ . showChar '.' . shows c+ . showChar '.' . shows d where ( _, a) = shift8 r1 (r1, b) = shift8 r2 (r2, c) = shift8 r3@@ -160,8 +176,16 @@ fromAddress = fromIPv6 toAddress = toIPv6 readsAddress = readsIPv6- showAddress = showIPv6+ showAddress ip = showsIPv6 ip ""+ maxPrefix _ = 128 +instance Address (Word32, Word32, Word32, Word32) where+ fromAddress = fromHostAddress6+ toAddress = toHostAddress6+ readsAddress = map (\(ip, s) -> ((toHostAddress6 . fromAddress) ip, s)) . readsIPv6+ showAddress = showAddress . toIPv6 . fromAddress+ maxPrefix _ = 128+ -- |Parse a textual representation of an 'IPv6' IP address, -- e.g. @1080:0:0:0:8:800:200C:417A@ readsIPv6 :: ReadS IPv6@@ -169,55 +193,64 @@ -- |An IPv6 parser. readpIPv6 :: ReadP IPv6-readpIPv6 = fmap (toAddress . octetsToInteger) $ choice- [ readHexP >>= \o -> count 7 (char ':' >> readHexP) >>= \os -> return (o:os)+readpIPv6 = fmap (toAddress . word16sToInteger) $ choice+ [ liftM2 (:) readHexP (count 7 (char ':' >> readHexP)) , do- head <- many1 (readHexP <<* char ':')- tail <- many1 (char ':' >> readHexP)- let body = replicate (8 - (length head + length tail)) 0- os = head ++ body ++ tail- case length os of- 8 -> return os- _ -> pfail- , char ':' >> many1 (char ':' >> readHexP) >>= \xs ->- if length xs <= 8- then return xs- else pfail+ a <- upTo1 7 (readHexP <<* char ':')+ c <- choice [ upTo1 (8 - length a) (char ':' >> readHexP)+ , char ':' >> return []+ ]+ let b = replicate (8 - (length a + length c)) 0+ return $ a ++ b ++ c+ , char ':' >> upTo1 7 (char ':' >> readHexP) , string "::" >> return [0]- , many1 (readHexP <<* char ':') <<* char ':' >>= \xs ->- if length xs > 8- then pfail- else return (xs ++ replicate (8 - length xs) 0) ] +-- |@upTo n p@ parses zero or up to @n@ occurrances of @p@.+upTo :: Int -> ReadP a -> ReadP [a]+upTo n p | n <= 0 = return []+ | otherwise = return [] +++ upTo1 n p++-- |@upTo n p@ parses one or up to @n@ occurrances of @p@.+upTo1 :: Int -> ReadP a -> ReadP [a]+upTo1 n p = liftM2 (:) p (upTo (n - 1) p)+ infixl 4 <<* -- |Monad version of 'Applicative's '<*' operator. (<<*) :: Monad m => m a -> m b -> m a a <<* b = a >>= (b >>) . return -octetsToInteger :: [Word16] -> Integer-octetsToInteger = foldl' (\x y -> (x `shift` 16) + fromIntegral y) 0+word16sToInteger :: [Word16] -> Integer+word16sToInteger = foldl' (\x y -> (x `shift` 16) + fromIntegral y) 0 -- |Return a conanical textual representation of an 'IPv6' IP address, -- e.g. @fedc:ba98:7654:3210:fedc:ba98:7654:3210@-showIPv6 :: IPv6 -> String-showIPv6 ip = intercalate ":" fields- where fields = if m == 1- then map (`showHex` "") octets- else (init ++ [""] ++ tail)- init = if i == 0- then [""]- else map (`showHex` "") . concat . take i $ grouped- tail = if i == (length lengths - 1)- then [""]- else map (`showHex` "") . concat . drop (i + 1) $ grouped- i = fromJust $ findIndex (== m) lengths- m = maximum lengths- lengths = map length grouped- grouped = groupBy (\x y -> x == 0 && y == 0) octets- octets = toIPv6Octets . fromAddress $ ip+showsIPv6 :: IPv6 -> ShowS+showsIPv6 ip | fromAddress ip == 0 = showString "::"+ | otherwise = case indexOfLongestSequence 0 word16s of+ Nothing -> foldl' (\s x -> s . showChar ':' . showHex x) (showHex field) fields+ Just idx -> if idx == 0+ then fst3 $ foldl' (collapse idx) (showString "", 0, False) word16s+ else fst3 (foldl' (collapse idx) (showHex field, 1, False) fields) .+ if replicate (length word16s - idx) 0 `isSuffixOf` word16s+ then showChar ':'+ else id+ where word16s@(field:fields) = toIPv6Octets $ fromAddress ip+ fst3 (x, _, _) = x+ collapse idx (s, i, z) x | z && x == 0 = (s, i+1, True)+ | i == idx = (s . showChar ':', i+1, True)+ | otherwise = (s . showChar ':' . showHex x, i+1, False) --- |Split a number into 'IPv6' octets.+-- |Find the longest, consecutive, occurance of an element within+-- a list.+indexOfLongestSequence :: (Eq a) => a -> [a] -> Maybe Int+indexOfLongestSequence x xs = case m of+ 0 -> Nothing+ _ -> fmap (\x -> x - m + 1) $ findIndex (== m) ms+ where m = maximum ms+ ms = drop 1 $ scanl (\c y -> if y == x then c + 1 else 0) 0 xs++-- |Split a number into 'IPv6' word16s. toIPv6Octets :: Integral a => a -> [a] toIPv6Octets = fill . reverse . go where fill os = replicate (8 - length os) 0 ++ os@@ -238,30 +271,58 @@ where a' = fromIntegral a b' = fromIntegral b +-- |Convert the byte representation to an 'HostAddress6' IP address.+toHostAddress6 :: Integer -> (Word32, Word32, Word32, Word32)+toHostAddress6 x = (a, b, c, d)+ where ( _, a) = shift32 r1+ (r1, b) = shift32 r2+ (r2, c) = shift32 r3+ (r3, d) = shift32 x+ shift32 x = divMod (fromIntegral x) (2 ^ 32)++-- |Return the byte representation of a 'HostAddress6' IP address.+fromHostAddress6 :: (Word32, Word32, Word32, Word32) -> Integer+fromHostAddress6 (a, b, c, d) = (a' `shift` 96) + (b' `shift` 64) + (c' `shift` 32) + d'+ where a' = fromIntegral a+ b' = fromIntegral b+ c' = fromIntegral c+ d' = fromIntegral d+ -- -- Subnet -- instance Subnet (IPSubnet IPv4) IPv4 where- showSubnet = showIPSubnet+ showSubnet s = showsIPv4Subnet s "" readsSubnet = readsIPv4Subnet base (IPSubnet b _) = b- netmask (IPSubnet _ m) = m+ prefix (IPSubnet _ p) = p instance Subnet (IPSubnet IPv6) IPv6 where- showSubnet = showIPSubnet+ showSubnet s = showsIPv6Subnet s "" readsSubnet = readsIPv6Subnet base (IPSubnet b _) = b- netmask (IPSubnet _ m) = m+ prefix (IPSubnet _ p) = p --- |Create an 'IPv4Subnet' data structure.-ipSubnet :: (Address a) => a -> Mask -> IPSubnet a-ipSubnet ip m = IPSubnet (maskAddress ip m) m+-- |Return the byte representation of the mask for a 'Prefix', given the+-- 'maxPrefix' of the address.+prefixMask :: Prefix -> Prefix -> Integer+prefixMask maxP p = (2^p - 1) * 2^(maxP - p) --- |Return a conanical textual representation of an IP 'Address' and+-- |Create an 'IPSubnet' containing an 'Address', clearing the host bits+-- so the base is the first address in the subnet. Return 'Nothing' if+-- the 'Prefix' is not between 0 and the 'maxPrefix' of the 'Address'.+ipSubnetForPrefix :: (Address a) => a -> Prefix -> Maybe (IPSubnet a)+ipSubnetForPrefix ip p+ | 0 <= p && p <= maxP = Just $ IPSubnet b p+ | otherwise = Nothing+ where maxP = maxPrefix ip+ b = toAddress $ fromAddress ip .&. prefixMask maxP p++-- |Return a conanical textual representation of an IPv4 'Address' and -- 'Subnet'.-showIPSubnet :: (Address a) => IPSubnet a -> String-showIPSubnet (IPSubnet ip mask) = showAddress ip ++ "/" ++ show (fromMask mask :: Integer)+showsIPv4Subnet :: IPSubnet IPv4 -> ShowS+showsIPv4Subnet (IPSubnet ip p) = showsIPv4 ip . showChar '/' . shows p -- |Parse a textual representation of an IPv4 address and subnet. readsIPv4Subnet :: ReadS (IPSubnet IPv4)@@ -272,9 +333,14 @@ readpIPv4Subnet = do ip <- readpIPv4 _ <- char '/'- m <- readDecP :: ReadP Word32- if 0 <= m && m <= 32 then return (ipSubnet ip (toMask m)) else pfail+ p <- readDecP+ maybe pfail return $ ipSubnetForPrefix ip p +-- |Return a conanical textual representation of an IPv6 'Address' and+-- 'Subnet'.+showsIPv6Subnet :: IPSubnet IPv6 -> ShowS+showsIPv6Subnet (IPSubnet ip p) = showsIPv6 ip . showChar '/' . shows p+ -- |Parse a textual representation of an IPv6 address and subnet. readsIPv6Subnet :: ReadS (IPSubnet IPv6) readsIPv6Subnet = readP_to_S readpIPv6Subnet@@ -284,21 +350,6 @@ readpIPv6Subnet = do ip <- readpIPv6 _ <- char '/'- m <- readDecP :: ReadP Int- if 0 <= m && m <= 128 then return (ipSubnet ip (toMask m)) else pfail---- |Convert a decimal mask to binary (e.g. 8 -> 1111.0000.0000.0000).-toMask :: (Integral a, Bits a, Integral b, Bits b) => a -> b-toMask size | size > 0 = complement (2^size - 1)- | otherwise = complement 0---- |Convert a binary mask to decimal (e.g. 1111.0000.0000.0000 -> 8).-fromMask :: (Integral a, Bits a, Integral b) => a -> b-fromMask m = timesDivisible (fromIntegral . complement $ m) 2---- |Determine how many times a number is divisible by another number.-timesDivisible :: (Integral a) => a -> a -> a-timesDivisible a x = timesDivisible' a x 0- where timesDivisible' a x c | a > 0 = timesDivisible' (a `div` x) x (c + 1)- | otherwise = c+ p <- readDecP+ maybe pfail return $ ipSubnetForPrefix ip p
+ README.md view
@@ -0,0 +1,104 @@+Network Address+===============++The network-address library provides data structures for IP addresses as+well as sub-networks. The goal is to have a generic, fast and complete+library for representing network addresses in binary or textual form.+Currently it supports IPv4 and IPv6 (partially).+++Usage+=====++ *Data.Network.Address> readAddress "127.0.0.1" :: IPv4+ IPv4 2130706433+ *Data.Network.Address> let ip = readAddress "127.0.0.1" :: IPv4+ *Data.Network.Address> showAddress ip+ "127.0.0.1"+ *Data.Network.Address> let subnet = readSubnet "192.168.1.42/8" :: IPSubnet IPv4+ *Data.Network.Address> subnet+ IPSubnet (IPv4 3221225472) 8+ *Data.Network.Address> showSubnet subnet+ "192.0.0.0/8"+ *Data.Network.Address> showAddress . base $ subnet+ "192.0.0.0"+ *Data.Network.Address> showAddress . netmask $ subnet+ "255.0.0.0"+ *Data.Network.Address> (readAddress "192.168.2.5" :: IPv4) `member` subnet+ True+ *Data.Network.Address> (readAddress "10.0.0.1" :: IPv4) `member` subnet+ False+ *Data.Network.Address> let ip = readAddress "2001:db8:0:0:0::1" :: IPv6+ *Data.Network.Address> ip+ IPv6 2306139568115548160 1+ *Data.Network.Address> showAddress ip+ "2001:db8::1"+ *Data.Network.Address> let subnet = readSubnet "2001:db8::1/56" :: IPSubnet IPv6+ *Data.Network.Address> showSubnet subnet+ "2001:db8::/56"+ *Data.Network.Address> ip `member` subnet+ True+++Installation+============++The network-address library is build using Cabal and Hackage. Just run:++ cabal configure+ cabal build+ cabal install+++Stability+=========++The library has not reached v1.0.0 yet, and thus has an unstable API.+And minor version increase (e.g. v0.1.0 -> v0.2.0) may break backwards+compatibility until v1.0.0 is reached.+++Documentation+=============++Documentation is available online on Hackage:+http://hackage.haskell.org/package/network-address++Documentation can be generated locally using Cabal and Haddock:++ cabal haddock+++Testing+=======++The test suite can be built and run with Cabal:++ cabal configure --enable-tests+ cabal build+ cabal test+++Benchmarking+============++A benchmarking tool using Criterion is provided by enabling the+'benchmark' flag in Cabal:++ cabal configure -fbenchmark+ cabal build+ ./dist/build/benchmark/benchmark+++TODO+====++ * Support for IPv4 embedded in IPv6 as per RFC 4291 Section 2.2.+++License+=======++The network-address library is licensed under the MIT license. Please read+the LICENSE file for details.+
+ Test.hs view
@@ -0,0 +1,8 @@+module Main where+import Test.Framework (defaultMain)+import qualified Test.Data.Network.Address.IP as IP++main = defaultMain $ concat+ [ IP.tests+ ]+
+ Test/Data/Network/Address/IP.hs view
@@ -0,0 +1,256 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE FlexibleContexts #-}+module Test.Data.Network.Address.IP (tests) where+import Data.Bits (shift, shiftR, xor)+import Data.Char (isDigit, isHexDigit)+import Data.List (intercalate, isPrefixOf)+import Data.Maybe (isNothing)+import Data.Network.Address.IP+import Data.Word+import Numeric (showHex)+import Test.Framework+import Test.Framework.Providers.QuickCheck2+import Test.QuickCheck+import Test.QuickCheck.Arbitrary++-- |Integral type for 32bit network masks (i.e. 0..32)+newtype Prefix32 = Prefix32 {getPrefix32 :: Word8} deriving (Show, Read)++newtype Prefix128 = Prefix128 {getPrefix128 :: Word8} deriving (Show, Read)++instance Arbitrary IPv4 where+ arbitrary = fmap (toAddress . toInteger) (arbitrary :: Gen Word32)++instance Arbitrary IPv6 where+ arbitrary = do+ a <- arbitrary :: Gen Word64+ b <- arbitrary :: Gen Word64+ return . toAddress . toInteger $ (a `shift` 64) + b++instance Arbitrary (IPSubnet IPv4) where+ arbitrary = do+ ip <- arbitrary :: Gen IPv4+ size <- arbitrary :: Gen Prefix32+ return . readSubnet $ showAddress ip ++ "/" ++ show (getPrefix32 size)++instance Arbitrary (IPSubnet IPv6) where+ arbitrary = do+ ip <- arbitrary :: Gen IPv6+ size <- arbitrary :: Gen Prefix128+ return . readSubnet $ showAddress ip ++ "/" ++ show (getPrefix128 size)++instance Arbitrary Prefix32 where+ arbitrary = fmap (Prefix32 . fromIntegral) (choose (0, 32) :: Gen Int)++instance Arbitrary Prefix128 where+ arbitrary = fmap (Prefix128 . fromIntegral) (choose (0, 128) :: Gen Int)++tests = [ testGroup "IPv4"+ [ testGroup "Read/Show"+ [ testProperty "Symmetric Read/Show" prop_ipv4_symmetric_readable+ , testProperty "Symmetric readAddress/showAddress" prop_ipv4_symmetric_parsable+ , testProperty "Invalid readsAddress" prop_ipv4_invalid_reads+ ]+ , testGroup "Binary"+ [ testProperty "Symmetric to/from" prop_ipv4_symmetric_tofrom+ ]+ , testGroup "Subnet"+ [ testProperty "Symmetric IPv4 Read/Show" prop_subnet_ipv4_symmetric_readable+ ]+ , testGroup "Membership"+ [ testProperty "Reflexivity" prop_reflexivityIPv4+ , testProperty "Reflexivity for subnet" prop_reflexivityIPv4Subnet+ , testProperty "BitsyContains" prop_bitsyContainsIPv4+ , testProperty "Subnet below range" prop_subnetBelowRangeIPv4+ , testProperty "Subnet above range" prop_subnetAboveRangeIPv4+ , testProperty "Prefix out of range" prop_prefixOutOfRangeIPv4+ ]+ , testGroup "Netmask"+ [ testProperty "Leading prefix bits set" prop_netmaskIPv4+ , testProperty "255.0.0.0 for /8" prop_netmaskSlash8+ ]+ ]+ , testGroup "IPv6"+ [ testGroup "Read/Show"+ [ testProperty "Symmetric Read/Show" prop_ipv4_symmetric_readable+ , testProperty "Symmetric readAddress/showAddress" prop_ipv6_symmetric_parsable+ , testProperty "Invalid readsAddress" prop_ipv6_invalid_reads+ , testProperty "Parse zero-compressed" prop_ipv6_parse_compressed+ , testProperty "Zero-compressed once" prop_ipv6_compressed_once+ ]+ , testGroup "Binary"+ [ testProperty "Symmetric to/from" prop_ipv6_symmetric_tofrom+ ]+ , testGroup "Subnet"+ [ testProperty "Symmetric IPv6 Read/Show" prop_subnet_ipv6_symmetric_readable+ ]+ , testGroup "Membership"+ [ testProperty "Reflexivity" prop_reflexivityIPv6+ , testProperty "Reflexivity for subnet" prop_reflexivityIPv6Subnet+ , testProperty "BitsyContains" prop_bitsyContainsIPv6+ , testProperty "Subnet below range" prop_subnetBelowRangeIPv6+ , testProperty "Subnet above range" prop_subnetAboveRangeIPv6+ , testProperty "Prefix out of range" prop_prefixOutOfRangeIPv6+ ]+ , testGroup "Netmask"+ [ testProperty "Leading prefix bits set" prop_netmaskIPv6+ , testProperty "ffff:ffff:: for /32" prop_netmaskSlash32+ ]+ ]+ ]++mostSignificantBit :: Integer -> Integer+mostSignificantBit n+ | n <= 0 = error "Non-positive integer provided"+ | otherwise = go 0 n+ where+ go !pos x+ | x == 1 = pos+ | otherwise = go (pos + 1) (x `shiftR` 1)++readAddressMaybe :: Address a => String -> Maybe a+readAddressMaybe s = case [x | (x, "") <- readsAddress s] of+ [ip] -> Just ip+ _ -> Nothing++-- |Returns the indices of all sublists.+elemsIndices :: Eq a => [a] -> [a] -> [Int]+elemsIndices needle xs = reverse $ go needle xs 0 []+ where go needle [] i is = is+ go needle xs i is = case needle `isPrefixOf` xs of+ True -> go needle (drop (length needle) xs) (i + 1) (i:is)+ False -> go needle (drop 1 xs) (i + 1) is++prop_fun_id :: (Eq a) => (a -> a) -> a -> Bool+prop_fun_id f x = f x == f (f x)++prop_ipv4_symmetric_readable :: IPv4 -> Bool+prop_ipv4_symmetric_readable ip = (read . show) ip == id ip++prop_ipv4_symmetric_parsable :: IPv4 -> Bool+prop_ipv4_symmetric_parsable ip = (readAddressMaybe . showAddress) ip == Just ip++prop_ipv4_symmetric_tofrom :: IPv4 -> Bool+prop_ipv4_symmetric_tofrom ip = (toAddress . fromAddress) ip == id ip++prop_ipv4_invalid_reads :: IPv4 -> String -> Property+prop_ipv4_invalid_reads a x = length x > 0 && (not . isDigit . head $ x)+ ==> (a, x) `elem` (readsAddress $ (showAddress a) ++ x)++prop_subnet_ipv4_symmetric_readable :: IPSubnet IPv4 -> Bool+prop_subnet_ipv4_symmetric_readable subnet = (readSubnet . showSubnet) subnet == id subnet++prop_ipv6_symmetric_readable :: IPv6 -> Bool+prop_ipv6_symmetric_readable ip = (read . show) ip == id ip++prop_ipv6_symmetric_parsable :: IPv6 -> Bool+prop_ipv6_symmetric_parsable ip = (readAddressMaybe . showAddress) ip == Just ip++prop_ipv6_symmetric_tofrom :: IPv6 -> Bool+prop_ipv6_symmetric_tofrom ip = (toAddress . fromAddress) ip == id ip++prop_ipv6_parse_compressed :: Word16 -> Word16 -> Word8 -> Bool+prop_ipv6_parse_compressed x y p = readAddressMaybe ip' == Just ip+ where h = fromIntegral x+ t = fromIntegral y+ ip = toAddress ((h `shift` 112) + t) :: IPv6+ os = showHex h "" : replicate (fromIntegral p `rem` 6) "0" ++ [showHex t ""]+ ip' = if length os == 8+ then intercalate ":" os+ else intercalate ":" $ (head os ++ ":") : tail os++prop_ipv6_compressed_once :: IPv6 -> Bool+prop_ipv6_compressed_once ip = length ("::" `elemsIndices` (showAddress ip)) == 1++prop_ipv6_invalid_reads :: IPv6 -> String -> Property+prop_ipv6_invalid_reads a x = length x > 0 && (not . isHexDigit . head $ x)+ ==> (a, x) `elem` (readsAddress $ (showAddress a) ++ x)++prop_subnet_invalid_reads :: (Address a, Subnet s a, Eq s) => s -> String -> Property+prop_subnet_invalid_reads s x = length x > 0 && (not . isDigit . head $ x)+ ==> (s, x) `elem` (readsSubnet $ (showSubnet s) ++ x)++prop_subnet_ipv6_symmetric_readable :: IPSubnet IPv6 -> Bool+prop_subnet_ipv6_symmetric_readable subnet = (readSubnet . showSubnet) subnet == id subnet++prop_subnet_ipv6_invalid_reads :: IPSubnet IPv6 -> String -> Property+prop_subnet_ipv6_invalid_reads = prop_subnet_invalid_reads++prop_subnet_ipv4_invalid_reads :: IPSubnet IPv4 -> String -> Property+prop_subnet_ipv4_invalid_reads = prop_subnet_invalid_reads++prop_reflexivityIPv4 :: IPv4 -> IPSubnet IPv4 -> Property+prop_reflexivityIPv4 addr (IPSubnet _ p) = property $ maybe False (member addr) (ipSubnetForPrefix addr p)++prop_reflexivityIPv4Subnet :: IPSubnet IPv4 -> Property+prop_reflexivityIPv4Subnet s@(IPSubnet a _) = property $ member a s++prop_reflexivityIPv6 :: IPv6 -> IPSubnet IPv6 -> Property+prop_reflexivityIPv6 addr (IPSubnet _ p) = property $ maybe False (member addr) (ipSubnetForPrefix addr p)++prop_reflexivityIPv6Subnet :: IPSubnet IPv6 -> Property+prop_reflexivityIPv6Subnet s@(IPSubnet a _) = property $ member a s++prop_bitsyContainsIPv4 :: IPv4 -> IPSubnet IPv4 -> Property+prop_bitsyContainsIPv4 a s@(IPSubnet sa p) =+ let xed = fromAddress a `xor` fromAddress sa+ in (xed == 0 || mostSignificantBit xed < maxPrefix a - p) === member a s++prop_bitsyContainsIPv6 :: IPv6 -> IPSubnet IPv6 -> Property+prop_bitsyContainsIPv6 a s@(IPSubnet sa p) =+ let xed = fromAddress a `xor` fromAddress sa+ in (xed == 0 || mostSignificantBit xed < maxPrefix a - p) === member a s++prop_subnetBelowRangeIPv4 :: IPSubnet IPv4 -> Property+prop_subnetBelowRangeIPv4 s@(IPSubnet a _) =+ fromAddress a > 0+ ==> not (member (toAddress . subtract 1 . fromAddress $ a) s)++prop_subnetBelowRangeIPv6 :: IPSubnet IPv6 -> Property+prop_subnetBelowRangeIPv6 s@(IPSubnet a _) =+ fromAddress a > 0+ ==> not (member (toAddress . subtract 1 . fromAddress $ a) s)++prop_subnetAboveRangeIPv4 :: IPSubnet IPv4 -> Property+prop_subnetAboveRangeIPv4 = prop_subnetAboveRange++prop_subnetAboveRangeIPv6 :: IPSubnet IPv6 -> Property+prop_subnetAboveRangeIPv6 = prop_subnetAboveRange++-- |The first address after the subnet is not a member.+prop_subnetAboveRange :: Subnet (IPSubnet a) a => IPSubnet a -> Property+prop_subnetAboveRange s@(IPSubnet a p) =+ next < 2^(maxPrefix a) ==> not (member (toAddress next) s)+ where+ next = fromAddress a + 2^(maxPrefix a - p)++prop_prefixOutOfRangeIPv4 :: IPv4 -> Positive Integer -> Bool+prop_prefixOutOfRangeIPv4 = prop_prefixOutOfRange++prop_prefixOutOfRangeIPv6 :: IPv6 -> Positive Integer -> Bool+prop_prefixOutOfRangeIPv6 = prop_prefixOutOfRange++prop_prefixOutOfRange :: Address a => a -> Positive Integer -> Bool+prop_prefixOutOfRange a (Positive d) =+ isNothing (ipSubnetForPrefix a (-d)) &&+ isNothing (ipSubnetForPrefix a (maxPrefix a + d))++prop_netmaskIPv4 :: IPSubnet IPv4 -> Property+prop_netmaskIPv4 = prop_netmask++prop_netmaskIPv6 :: IPSubnet IPv6 -> Property+prop_netmaskIPv6 = prop_netmask++-- |The netmask has exactly the leading prefix bits set.+prop_netmask :: Subnet (IPSubnet a) a => IPSubnet a -> Property+prop_netmask s@(IPSubnet a p) =+ fromAddress (netmask s) === 2^(maxPrefix a) - 2^(maxPrefix a - p)++prop_netmaskSlash8 :: Property+prop_netmaskSlash8 = once $+ showAddress (netmask (readSubnet "10.1.2.3/8" :: IPSubnet IPv4)) === "255.0.0.0"++prop_netmaskSlash32 :: Property+prop_netmaskSlash32 = once $+ showAddress (netmask (readSubnet "2001:db8::1/32" :: IPSubnet IPv6)) === "ffff:ffff::"
network-address.cabal view
@@ -1,5 +1,5 @@ Name: network-address-Version: 0.2.0+Version: 0.3.0 Synopsis: IP data structures and textual representation Description: A library for parsing, representing, computing and transforming network addresses such a IPv4, IPv6@@ -11,43 +11,47 @@ Maintainer: sebnow@gmail.com Category: Data, Network Build-type: Simple-Cabal-version: >=1.9.2+Extra-source-files: README.md+Cabal-version: >=1.10 Source-Repository head type: git- location: git://github.com/sebnow/haskell-network-address.git+ location: https://github.com/sebnow/haskell-network-address.git Flag benchmark- Description: Build benchmarking + Description: Build benchmarking Default: False Library Exposed-modules: Data.Network.Address Data.Network.Address.IP+ Default-language: Haskell2010 Build-depends:- base == 4.*,- Cabal >= 1.9.2 && < 1.11+ base == 4.* test-suite test-package Type: exitcode-stdio-1.0 Main-is: Test.hs+ Default-language: Haskell2010+ Other-modules:+ Data.Network.Address.IP+ Test.Data.Network.Address.IP Build-depends: base == 4.*,- Cabal >= 1.9.2 && < 1.11,- QuickCheck == 2.4.*,- test-framework == 0.4.*,- test-framework-quickcheck2 == 0.2.*+ QuickCheck >= 2.4 && < 3.0,+ test-framework >= 0.4 && < 0.9,+ test-framework-quickcheck2 >= 0.2 && < 0.4 Executable benchmark Main-is: Benchmark.hs+ Default-language: Haskell2010 if flag(benchmark) Buildable: True+ Build-depends:+ base == 4.*,+ criterion >= 0.5.0.0 && < 0.7,+ deepseq == 1.1.0.* else Buildable: False- Build-depends:- base == 4.*,- Cabal >= 1.9.2 && < 1.11,- criterion == 0.5.0.*-