module Data.IP.Op where
import Data.Bits
import Data.IP.Addr
import Data.IP.Mask
import Data.IP.Range
class Eq a => Addr a where
masked :: a -> a -> a
intToMask :: Int -> a
instance Addr IPv4 where
masked :: IPv4 -> IPv4 -> IPv4
masked = IPv4 -> IPv4 -> IPv4
maskedIPv4
intToMask :: Int -> IPv4
intToMask = Int -> IPv4
maskIPv4
instance Addr IPv6 where
masked :: IPv6 -> IPv6 -> IPv6
masked = IPv6 -> IPv6 -> IPv6
maskedIPv6
intToMask :: Int -> IPv6
intToMask = Int -> IPv6
maskIPv6
(>:>) :: Addr a => AddrRange a -> AddrRange a -> Bool
AddrRange a
a >:> :: AddrRange a -> AddrRange a -> Bool
>:> AddrRange a
b = AddrRange a -> Int
forall a. AddrRange a -> Int
mlen AddrRange a
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= AddrRange a -> Int
forall a. AddrRange a -> Int
mlen AddrRange a
b Bool -> Bool -> Bool
&& (AddrRange a -> a
forall a. AddrRange a -> a
addr AddrRange a
b a -> a -> a
forall a. Addr a => a -> a -> a
`masked` AddrRange a -> a
forall a. AddrRange a -> a
mask AddrRange a
a) a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== AddrRange a -> a
forall a. AddrRange a -> a
addr AddrRange a
a
isMatchedTo :: Addr a => a -> AddrRange a -> Bool
isMatchedTo :: a -> AddrRange a -> Bool
isMatchedTo a
a AddrRange a
r = a
a a -> a -> a
forall a. Addr a => a -> a -> a
`masked` AddrRange a -> a
forall a. AddrRange a -> a
mask AddrRange a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== AddrRange a -> a
forall a. AddrRange a -> a
addr AddrRange a
r
makeAddrRange :: Addr a => a -> Int -> AddrRange a
makeAddrRange :: a -> Int -> AddrRange a
makeAddrRange a
ad Int
len = a -> a -> Int -> AddrRange a
forall a. a -> a -> Int -> AddrRange a
AddrRange a
adr a
msk Int
len
where
msk :: a
msk = Int -> a
forall a. Addr a => Int -> a
intToMask Int
len
adr :: a
adr = a
ad a -> a -> a
forall a. Addr a => a -> a -> a
`masked` a
msk
ipv4RangeToIPv6 :: AddrRange IPv4 -> AddrRange IPv6
ipv4RangeToIPv6 :: AddrRange IPv4 -> AddrRange IPv6
ipv4RangeToIPv6 AddrRange IPv4
range =
IPv6 -> Int -> AddrRange IPv6
forall a. Addr a => a -> Int -> AddrRange a
makeAddrRange ([Int] -> IPv6
toIPv6 [Int
0,Int
0,Int
0,Int
0,Int
0,Int
0xffff, (Int
i1 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shift` Int
8) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i2, (Int
i3 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shift` Int
8) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
i4]) (Int
masklen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
96)
where
(IPv4
ip, Int
masklen) = AddrRange IPv4 -> (IPv4, Int)
forall a. Addr a => AddrRange a -> (a, Int)
addrRangePair AddrRange IPv4
range
[Int
i1,Int
i2,Int
i3,Int
i4] = IPv4 -> [Int]
fromIPv4 IPv4
ip
addrRangePair :: Addr a => AddrRange a -> (a, Int)
addrRangePair :: AddrRange a -> (a, Int)
addrRangePair (AddrRange a
adr a
_ Int
len) = (a
adr, Int
len)