module Net.IPv4
( decodeRange
, IPv4Range
, member
, fromTupleOctets
, parser
, IPv4(..)
) where
import Prelude
import Data.Word
import qualified Data.Attoparsec.Text as AT
import Data.Text (Text)
import GHC.Generics (Generic)
import Data.Ix (Ix)
import Data.Hashable
import Data.Bits (Bits(..))
import qualified Data.Bits as Bits
data IPv4Range = IPv4Range
{ IPv4Range -> IPv4
ipv4RangeBase :: {-# UNPACK #-} !IPv4
, IPv4Range -> Word8
ipv4RangeLength :: {-# UNPACK #-} !Word8
} deriving (IPv4Range -> IPv4Range -> Bool
(IPv4Range -> IPv4Range -> Bool)
-> (IPv4Range -> IPv4Range -> Bool) -> Eq IPv4Range
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IPv4Range -> IPv4Range -> Bool
$c/= :: IPv4Range -> IPv4Range -> Bool
== :: IPv4Range -> IPv4Range -> Bool
$c== :: IPv4Range -> IPv4Range -> Bool
Eq,Eq IPv4Range
Eq IPv4Range
-> (IPv4Range -> IPv4Range -> Ordering)
-> (IPv4Range -> IPv4Range -> Bool)
-> (IPv4Range -> IPv4Range -> Bool)
-> (IPv4Range -> IPv4Range -> Bool)
-> (IPv4Range -> IPv4Range -> Bool)
-> (IPv4Range -> IPv4Range -> IPv4Range)
-> (IPv4Range -> IPv4Range -> IPv4Range)
-> Ord IPv4Range
IPv4Range -> IPv4Range -> Bool
IPv4Range -> IPv4Range -> Ordering
IPv4Range -> IPv4Range -> IPv4Range
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: IPv4Range -> IPv4Range -> IPv4Range
$cmin :: IPv4Range -> IPv4Range -> IPv4Range
max :: IPv4Range -> IPv4Range -> IPv4Range
$cmax :: IPv4Range -> IPv4Range -> IPv4Range
>= :: IPv4Range -> IPv4Range -> Bool
$c>= :: IPv4Range -> IPv4Range -> Bool
> :: IPv4Range -> IPv4Range -> Bool
$c> :: IPv4Range -> IPv4Range -> Bool
<= :: IPv4Range -> IPv4Range -> Bool
$c<= :: IPv4Range -> IPv4Range -> Bool
< :: IPv4Range -> IPv4Range -> Bool
$c< :: IPv4Range -> IPv4Range -> Bool
compare :: IPv4Range -> IPv4Range -> Ordering
$ccompare :: IPv4Range -> IPv4Range -> Ordering
$cp1Ord :: Eq IPv4Range
Ord,Int -> IPv4Range -> ShowS
[IPv4Range] -> ShowS
IPv4Range -> String
(Int -> IPv4Range -> ShowS)
-> (IPv4Range -> String)
-> ([IPv4Range] -> ShowS)
-> Show IPv4Range
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IPv4Range] -> ShowS
$cshowList :: [IPv4Range] -> ShowS
show :: IPv4Range -> String
$cshow :: IPv4Range -> String
showsPrec :: Int -> IPv4Range -> ShowS
$cshowsPrec :: Int -> IPv4Range -> ShowS
Show,ReadPrec [IPv4Range]
ReadPrec IPv4Range
Int -> ReadS IPv4Range
ReadS [IPv4Range]
(Int -> ReadS IPv4Range)
-> ReadS [IPv4Range]
-> ReadPrec IPv4Range
-> ReadPrec [IPv4Range]
-> Read IPv4Range
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [IPv4Range]
$creadListPrec :: ReadPrec [IPv4Range]
readPrec :: ReadPrec IPv4Range
$creadPrec :: ReadPrec IPv4Range
readList :: ReadS [IPv4Range]
$creadList :: ReadS [IPv4Range]
readsPrec :: Int -> ReadS IPv4Range
$creadsPrec :: Int -> ReadS IPv4Range
Read,(forall x. IPv4Range -> Rep IPv4Range x)
-> (forall x. Rep IPv4Range x -> IPv4Range) -> Generic IPv4Range
forall x. Rep IPv4Range x -> IPv4Range
forall x. IPv4Range -> Rep IPv4Range x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IPv4Range x -> IPv4Range
$cfrom :: forall x. IPv4Range -> Rep IPv4Range x
Generic)
newtype IPv4 = IPv4 { IPv4 -> Word32
getIPv4 :: Word32 }
deriving (Eq IPv4
IPv4
Eq IPv4
-> (IPv4 -> IPv4 -> IPv4)
-> (IPv4 -> IPv4 -> IPv4)
-> (IPv4 -> IPv4 -> IPv4)
-> (IPv4 -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> IPv4
-> (Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> Bool)
-> (IPv4 -> Maybe Int)
-> (IPv4 -> Int)
-> (IPv4 -> Bool)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int -> IPv4)
-> (IPv4 -> Int)
-> Bits IPv4
Int -> IPv4
IPv4 -> Bool
IPv4 -> Int
IPv4 -> Maybe Int
IPv4 -> IPv4
IPv4 -> Int -> Bool
IPv4 -> Int -> IPv4
IPv4 -> IPv4 -> IPv4
forall a.
Eq a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
popCount :: IPv4 -> Int
$cpopCount :: IPv4 -> Int
rotateR :: IPv4 -> Int -> IPv4
$crotateR :: IPv4 -> Int -> IPv4
rotateL :: IPv4 -> Int -> IPv4
$crotateL :: IPv4 -> Int -> IPv4
unsafeShiftR :: IPv4 -> Int -> IPv4
$cunsafeShiftR :: IPv4 -> Int -> IPv4
shiftR :: IPv4 -> Int -> IPv4
$cshiftR :: IPv4 -> Int -> IPv4
unsafeShiftL :: IPv4 -> Int -> IPv4
$cunsafeShiftL :: IPv4 -> Int -> IPv4
shiftL :: IPv4 -> Int -> IPv4
$cshiftL :: IPv4 -> Int -> IPv4
isSigned :: IPv4 -> Bool
$cisSigned :: IPv4 -> Bool
bitSize :: IPv4 -> Int
$cbitSize :: IPv4 -> Int
bitSizeMaybe :: IPv4 -> Maybe Int
$cbitSizeMaybe :: IPv4 -> Maybe Int
testBit :: IPv4 -> Int -> Bool
$ctestBit :: IPv4 -> Int -> Bool
complementBit :: IPv4 -> Int -> IPv4
$ccomplementBit :: IPv4 -> Int -> IPv4
clearBit :: IPv4 -> Int -> IPv4
$cclearBit :: IPv4 -> Int -> IPv4
setBit :: IPv4 -> Int -> IPv4
$csetBit :: IPv4 -> Int -> IPv4
bit :: Int -> IPv4
$cbit :: Int -> IPv4
zeroBits :: IPv4
$czeroBits :: IPv4
rotate :: IPv4 -> Int -> IPv4
$crotate :: IPv4 -> Int -> IPv4
shift :: IPv4 -> Int -> IPv4
$cshift :: IPv4 -> Int -> IPv4
complement :: IPv4 -> IPv4
$ccomplement :: IPv4 -> IPv4
xor :: IPv4 -> IPv4 -> IPv4
$cxor :: IPv4 -> IPv4 -> IPv4
.|. :: IPv4 -> IPv4 -> IPv4
$c.|. :: IPv4 -> IPv4 -> IPv4
.&. :: IPv4 -> IPv4 -> IPv4
$c.&. :: IPv4 -> IPv4 -> IPv4
$cp1Bits :: Eq IPv4
Bits.Bits,IPv4
IPv4 -> IPv4 -> Bounded IPv4
forall a. a -> a -> Bounded a
maxBound :: IPv4
$cmaxBound :: IPv4
minBound :: IPv4
$cminBound :: IPv4
Bounded,Int -> IPv4
IPv4 -> Int
IPv4 -> [IPv4]
IPv4 -> IPv4
IPv4 -> IPv4 -> [IPv4]
IPv4 -> IPv4 -> IPv4 -> [IPv4]
(IPv4 -> IPv4)
-> (IPv4 -> IPv4)
-> (Int -> IPv4)
-> (IPv4 -> Int)
-> (IPv4 -> [IPv4])
-> (IPv4 -> IPv4 -> [IPv4])
-> (IPv4 -> IPv4 -> [IPv4])
-> (IPv4 -> IPv4 -> IPv4 -> [IPv4])
-> Enum IPv4
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: IPv4 -> IPv4 -> IPv4 -> [IPv4]
$cenumFromThenTo :: IPv4 -> IPv4 -> IPv4 -> [IPv4]
enumFromTo :: IPv4 -> IPv4 -> [IPv4]
$cenumFromTo :: IPv4 -> IPv4 -> [IPv4]
enumFromThen :: IPv4 -> IPv4 -> [IPv4]
$cenumFromThen :: IPv4 -> IPv4 -> [IPv4]
enumFrom :: IPv4 -> [IPv4]
$cenumFrom :: IPv4 -> [IPv4]
fromEnum :: IPv4 -> Int
$cfromEnum :: IPv4 -> Int
toEnum :: Int -> IPv4
$ctoEnum :: Int -> IPv4
pred :: IPv4 -> IPv4
$cpred :: IPv4 -> IPv4
succ :: IPv4 -> IPv4
$csucc :: IPv4 -> IPv4
Enum,IPv4 -> IPv4 -> Bool
(IPv4 -> IPv4 -> Bool) -> (IPv4 -> IPv4 -> Bool) -> Eq IPv4
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IPv4 -> IPv4 -> Bool
$c/= :: IPv4 -> IPv4 -> Bool
== :: IPv4 -> IPv4 -> Bool
$c== :: IPv4 -> IPv4 -> Bool
Eq,Bits IPv4
Bits IPv4
-> (IPv4 -> Int)
-> (IPv4 -> Int)
-> (IPv4 -> Int)
-> FiniteBits IPv4
IPv4 -> Int
forall b.
Bits b -> (b -> Int) -> (b -> Int) -> (b -> Int) -> FiniteBits b
countTrailingZeros :: IPv4 -> Int
$ccountTrailingZeros :: IPv4 -> Int
countLeadingZeros :: IPv4 -> Int
$ccountLeadingZeros :: IPv4 -> Int
finiteBitSize :: IPv4 -> Int
$cfiniteBitSize :: IPv4 -> Int
$cp1FiniteBits :: Bits IPv4
Bits.FiniteBits,(forall x. IPv4 -> Rep IPv4 x)
-> (forall x. Rep IPv4 x -> IPv4) -> Generic IPv4
forall x. Rep IPv4 x -> IPv4
forall x. IPv4 -> Rep IPv4 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IPv4 x -> IPv4
$cfrom :: forall x. IPv4 -> Rep IPv4 x
Generic,Int -> IPv4 -> Int
IPv4 -> Int
(Int -> IPv4 -> Int) -> (IPv4 -> Int) -> Hashable IPv4
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: IPv4 -> Int
$chash :: IPv4 -> Int
hashWithSalt :: Int -> IPv4 -> Int
$chashWithSalt :: Int -> IPv4 -> Int
Hashable,Ord IPv4
Ord IPv4
-> ((IPv4, IPv4) -> [IPv4])
-> ((IPv4, IPv4) -> IPv4 -> Int)
-> ((IPv4, IPv4) -> IPv4 -> Int)
-> ((IPv4, IPv4) -> IPv4 -> Bool)
-> ((IPv4, IPv4) -> Int)
-> ((IPv4, IPv4) -> Int)
-> Ix IPv4
(IPv4, IPv4) -> Int
(IPv4, IPv4) -> [IPv4]
(IPv4, IPv4) -> IPv4 -> Bool
(IPv4, IPv4) -> IPv4 -> Int
forall a.
Ord a
-> ((a, a) -> [a])
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Bool)
-> ((a, a) -> Int)
-> ((a, a) -> Int)
-> Ix a
unsafeRangeSize :: (IPv4, IPv4) -> Int
$cunsafeRangeSize :: (IPv4, IPv4) -> Int
rangeSize :: (IPv4, IPv4) -> Int
$crangeSize :: (IPv4, IPv4) -> Int
inRange :: (IPv4, IPv4) -> IPv4 -> Bool
$cinRange :: (IPv4, IPv4) -> IPv4 -> Bool
unsafeIndex :: (IPv4, IPv4) -> IPv4 -> Int
$cunsafeIndex :: (IPv4, IPv4) -> IPv4 -> Int
index :: (IPv4, IPv4) -> IPv4 -> Int
$cindex :: (IPv4, IPv4) -> IPv4 -> Int
range :: (IPv4, IPv4) -> [IPv4]
$crange :: (IPv4, IPv4) -> [IPv4]
$cp1Ix :: Ord IPv4
Ix,Eq IPv4
Eq IPv4
-> (IPv4 -> IPv4 -> Ordering)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> IPv4)
-> (IPv4 -> IPv4 -> IPv4)
-> Ord IPv4
IPv4 -> IPv4 -> Bool
IPv4 -> IPv4 -> Ordering
IPv4 -> IPv4 -> IPv4
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: IPv4 -> IPv4 -> IPv4
$cmin :: IPv4 -> IPv4 -> IPv4
max :: IPv4 -> IPv4 -> IPv4
$cmax :: IPv4 -> IPv4 -> IPv4
>= :: IPv4 -> IPv4 -> Bool
$c>= :: IPv4 -> IPv4 -> Bool
> :: IPv4 -> IPv4 -> Bool
$c> :: IPv4 -> IPv4 -> Bool
<= :: IPv4 -> IPv4 -> Bool
$c<= :: IPv4 -> IPv4 -> Bool
< :: IPv4 -> IPv4 -> Bool
$c< :: IPv4 -> IPv4 -> Bool
compare :: IPv4 -> IPv4 -> Ordering
$ccompare :: IPv4 -> IPv4 -> Ordering
$cp1Ord :: Eq IPv4
Ord,ReadPrec [IPv4]
ReadPrec IPv4
Int -> ReadS IPv4
ReadS [IPv4]
(Int -> ReadS IPv4)
-> ReadS [IPv4] -> ReadPrec IPv4 -> ReadPrec [IPv4] -> Read IPv4
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [IPv4]
$creadListPrec :: ReadPrec [IPv4]
readPrec :: ReadPrec IPv4
$creadPrec :: ReadPrec IPv4
readList :: ReadS [IPv4]
$creadList :: ReadS [IPv4]
readsPrec :: Int -> ReadS IPv4
$creadsPrec :: Int -> ReadS IPv4
Read,Int -> IPv4 -> ShowS
[IPv4] -> ShowS
IPv4 -> String
(Int -> IPv4 -> ShowS)
-> (IPv4 -> String) -> ([IPv4] -> ShowS) -> Show IPv4
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IPv4] -> ShowS
$cshowList :: [IPv4] -> ShowS
show :: IPv4 -> String
$cshow :: IPv4 -> String
showsPrec :: Int -> IPv4 -> ShowS
$cshowsPrec :: Int -> IPv4 -> ShowS
Show)
decodeRange :: Text -> Maybe IPv4Range
decodeRange :: Text -> Maybe IPv4Range
decodeRange = Either String IPv4Range -> Maybe IPv4Range
forall a b. Either a b -> Maybe b
rightToMaybe (Either String IPv4Range -> Maybe IPv4Range)
-> (Text -> Either String IPv4Range) -> Text -> Maybe IPv4Range
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser IPv4Range -> Text -> Either String IPv4Range
forall a. Parser a -> Text -> Either String a
AT.parseOnly (Parser IPv4Range
parserRange Parser IPv4Range -> Parser Text () -> Parser IPv4Range
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text ()
forall t. Chunk t => Parser t ()
AT.endOfInput)
parserRange :: AT.Parser IPv4Range
parserRange :: Parser IPv4Range
parserRange = do
IPv4
ip <- Parser IPv4
parser
Char
_ <- Char -> Parser Char
AT.char Char
'/'
Word8
theMask <- Parser Word8
forall a. Integral a => Parser a
AT.decimal Parser Word8 -> (Word8 -> Parser Word8) -> Parser Word8
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word8 -> Parser Word8
forall a (m :: * -> *). (Ord a, Num a, MonadFail m) => a -> m a
limitSize
IPv4Range -> Parser IPv4Range
forall (m :: * -> *) a. Monad m => a -> m a
return (IPv4Range -> IPv4Range
normalize (IPv4 -> Word8 -> IPv4Range
IPv4Range IPv4
ip Word8
theMask))
where
limitSize :: a -> m a
limitSize a
i =
if a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
32
then String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"An IP range length must be between 0 and 32"
else a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
parser :: AT.Parser IPv4
parser :: Parser IPv4
parser = Parser IPv4
dotDecimalParser
dotDecimalParser :: AT.Parser IPv4
dotDecimalParser :: Parser IPv4
dotDecimalParser = Word -> Word -> Word -> Word -> IPv4
fromOctets'
(Word -> Word -> Word -> Word -> IPv4)
-> Parser Text Word -> Parser Text (Word -> Word -> Word -> IPv4)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser Text Word
forall a. Integral a => Parser a
AT.decimal Parser Text Word -> (Word -> Parser Text Word) -> Parser Text Word
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word -> Parser Text Word
forall a (m :: * -> *). (Ord a, Num a, MonadFail m) => a -> m a
limitSize)
Parser Text (Word -> Word -> Word -> IPv4)
-> Parser Char -> Parser Text (Word -> Word -> Word -> IPv4)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
AT.char Char
'.'
Parser Text (Word -> Word -> Word -> IPv4)
-> Parser Text Word -> Parser Text (Word -> Word -> IPv4)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser Text Word
forall a. Integral a => Parser a
AT.decimal Parser Text Word -> (Word -> Parser Text Word) -> Parser Text Word
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word -> Parser Text Word
forall a (m :: * -> *). (Ord a, Num a, MonadFail m) => a -> m a
limitSize)
Parser Text (Word -> Word -> IPv4)
-> Parser Char -> Parser Text (Word -> Word -> IPv4)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
AT.char Char
'.'
Parser Text (Word -> Word -> IPv4)
-> Parser Text Word -> Parser Text (Word -> IPv4)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser Text Word
forall a. Integral a => Parser a
AT.decimal Parser Text Word -> (Word -> Parser Text Word) -> Parser Text Word
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word -> Parser Text Word
forall a (m :: * -> *). (Ord a, Num a, MonadFail m) => a -> m a
limitSize)
Parser Text (Word -> IPv4)
-> Parser Char -> Parser Text (Word -> IPv4)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
AT.char Char
'.'
Parser Text (Word -> IPv4) -> Parser Text Word -> Parser IPv4
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser Text Word
forall a. Integral a => Parser a
AT.decimal Parser Text Word -> (Word -> Parser Text Word) -> Parser Text Word
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word -> Parser Text Word
forall a (m :: * -> *). (Ord a, Num a, MonadFail m) => a -> m a
limitSize)
where
limitSize :: a -> m a
limitSize a
i =
if a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
255
then String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
ipOctetSizeErrorMsg
else a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
contains :: IPv4Range -> IPv4 -> Bool
contains :: IPv4Range -> IPv4 -> Bool
contains (IPv4Range (IPv4 Word32
wsubnet) Word8
len) =
let theMask :: Word32
theMask = Word8 -> Word32
mask Word8
len
wsubnetNormalized :: Word32
wsubnetNormalized = Word32
wsubnet Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
theMask
in \(IPv4 Word32
w) -> (Word32
w Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
theMask) Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
wsubnetNormalized
mask :: Word8 -> Word32
mask :: Word8 -> Word32
mask = Word32 -> Word32
forall a. Bits a => a -> a
complement (Word32 -> Word32) -> (Word8 -> Word32) -> Word8 -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
shiftR Word32
0xffffffff (Int -> Word32) -> (Word8 -> Int) -> Word8 -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
member :: IPv4 -> IPv4Range -> Bool
member :: IPv4 -> IPv4Range -> Bool
member = (IPv4Range -> IPv4 -> Bool) -> IPv4 -> IPv4Range -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip IPv4Range -> IPv4 -> Bool
contains
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
fromOctets Word8
a Word8
b Word8
c Word8
d = Word -> Word -> Word -> Word -> IPv4
fromOctets'
(Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
a) (Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b) (Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c) (Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
d)
fromTupleOctets :: (Word8,Word8,Word8,Word8) -> IPv4
fromTupleOctets :: (Word8, Word8, Word8, Word8) -> IPv4
fromTupleOctets (Word8
a,Word8
b,Word8
c,Word8
d) = Word8 -> Word8 -> Word8 -> Word8 -> IPv4
fromOctets Word8
a Word8
b Word8
c Word8
d
fromOctets' :: Word -> Word -> Word -> Word -> IPv4
fromOctets' :: Word -> Word -> Word -> Word -> IPv4
fromOctets' Word
a Word
b Word
c Word
d = Word32 -> IPv4
IPv4 (Word32 -> IPv4) -> Word32 -> IPv4
forall a b. (a -> b) -> a -> b
$ Word -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral
( Word -> Int -> Word
forall a. Bits a => a -> Int -> a
shiftL Word
a Int
24
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Word -> Int -> Word
forall a. Bits a => a -> Int -> a
shiftL Word
b Int
16
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Word -> Int -> Word
forall a. Bits a => a -> Int -> a
shiftL Word
c Int
8
Word -> Word -> Word
forall a. Bits a => a -> a -> a
.|. Word
d
)
ipOctetSizeErrorMsg :: String
ipOctetSizeErrorMsg :: String
ipOctetSizeErrorMsg = String
"All octets in an IPv4 address must be between 0 and 255"
normalize :: IPv4Range -> IPv4Range
normalize :: IPv4Range -> IPv4Range
normalize (IPv4Range (IPv4 Word32
w) Word8
len) =
let len' :: Word8
len' = Word8 -> Word8 -> Word8
forall a. Ord a => a -> a -> a
min Word8
len Word8
32
w' :: Word32
w' = Word32
w Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word8 -> Word32
mask Word8
len'
in IPv4 -> Word8 -> IPv4Range
IPv4Range (Word32 -> IPv4
IPv4 Word32
w') Word8
len'
rightToMaybe :: Either a b -> Maybe b
rightToMaybe :: Either a b -> Maybe b
rightToMaybe = (a -> Maybe b) -> (b -> Maybe b) -> Either a b -> Maybe b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe b -> a -> Maybe b
forall a b. a -> b -> a
const Maybe b
forall a. Maybe a
Nothing) b -> Maybe b
forall a. a -> Maybe a
Just