{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric      #-}
{-# LANGUAGE FlexibleInstances  #-}
module Data.IP.Range where

import Data.Bits
import Data.Char
import Data.Data (Data)
import Data.IP.Addr
import Data.IP.Mask
import Data.String
import Data.Typeable (Typeable)
import Text.Appar.String
import GHC.Generics

----------------------------------------------------------------

{-|
  A unified data for 'AddrRange' 'IPv4' and 'AddrRange' 'IPv6'.
  To create this, use 'read' @\"192.0.2.0/24\"@ :: 'IPRange'.
  Also, @\"192.0.2.0/24\"@ can be used as literal with OverloadedStrings.

>>> (read "192.0.2.1/24" :: IPRange) == IPv4Range (read "192.0.2.0/24" :: AddrRange IPv4)
True
>>> (read "2001:db8:00:00:00:00:00:01/48" :: IPRange) == IPv6Range (read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv6)
True
-}

data IPRange = IPv4Range { IPRange -> AddrRange IPv4
ipv4range :: AddrRange IPv4 }
             | IPv6Range { IPRange -> AddrRange IPv6
ipv6range :: AddrRange IPv6 }
        deriving (IPRange -> IPRange -> Bool
(IPRange -> IPRange -> Bool)
-> (IPRange -> IPRange -> Bool) -> Eq IPRange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IPRange -> IPRange -> Bool
$c/= :: IPRange -> IPRange -> Bool
== :: IPRange -> IPRange -> Bool
$c== :: IPRange -> IPRange -> Bool
Eq, Eq IPRange
Eq IPRange
-> (IPRange -> IPRange -> Ordering)
-> (IPRange -> IPRange -> Bool)
-> (IPRange -> IPRange -> Bool)
-> (IPRange -> IPRange -> Bool)
-> (IPRange -> IPRange -> Bool)
-> (IPRange -> IPRange -> IPRange)
-> (IPRange -> IPRange -> IPRange)
-> Ord IPRange
IPRange -> IPRange -> Bool
IPRange -> IPRange -> Ordering
IPRange -> IPRange -> IPRange
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 :: IPRange -> IPRange -> IPRange
$cmin :: IPRange -> IPRange -> IPRange
max :: IPRange -> IPRange -> IPRange
$cmax :: IPRange -> IPRange -> IPRange
>= :: IPRange -> IPRange -> Bool
$c>= :: IPRange -> IPRange -> Bool
> :: IPRange -> IPRange -> Bool
$c> :: IPRange -> IPRange -> Bool
<= :: IPRange -> IPRange -> Bool
$c<= :: IPRange -> IPRange -> Bool
< :: IPRange -> IPRange -> Bool
$c< :: IPRange -> IPRange -> Bool
compare :: IPRange -> IPRange -> Ordering
$ccompare :: IPRange -> IPRange -> Ordering
$cp1Ord :: Eq IPRange
Ord, Typeable IPRange
DataType
Constr
Typeable IPRange
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> IPRange -> c IPRange)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c IPRange)
-> (IPRange -> Constr)
-> (IPRange -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c IPRange))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPRange))
-> ((forall b. Data b => b -> b) -> IPRange -> IPRange)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> IPRange -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> IPRange -> r)
-> (forall u. (forall d. Data d => d -> u) -> IPRange -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> IPRange -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> IPRange -> m IPRange)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> IPRange -> m IPRange)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> IPRange -> m IPRange)
-> Data IPRange
IPRange -> DataType
IPRange -> Constr
(forall b. Data b => b -> b) -> IPRange -> IPRange
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IPRange -> c IPRange
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c IPRange
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> IPRange -> u
forall u. (forall d. Data d => d -> u) -> IPRange -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> IPRange -> m IPRange
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> IPRange -> m IPRange
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c IPRange
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IPRange -> c IPRange
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c IPRange)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPRange)
$cIPv6Range :: Constr
$cIPv4Range :: Constr
$tIPRange :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> IPRange -> m IPRange
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> IPRange -> m IPRange
gmapMp :: (forall d. Data d => d -> m d) -> IPRange -> m IPRange
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> IPRange -> m IPRange
gmapM :: (forall d. Data d => d -> m d) -> IPRange -> m IPRange
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> IPRange -> m IPRange
gmapQi :: Int -> (forall d. Data d => d -> u) -> IPRange -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> IPRange -> u
gmapQ :: (forall d. Data d => d -> u) -> IPRange -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> IPRange -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
gmapT :: (forall b. Data b => b -> b) -> IPRange -> IPRange
$cgmapT :: (forall b. Data b => b -> b) -> IPRange -> IPRange
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPRange)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPRange)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c IPRange)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c IPRange)
dataTypeOf :: IPRange -> DataType
$cdataTypeOf :: IPRange -> DataType
toConstr :: IPRange -> Constr
$ctoConstr :: IPRange -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c IPRange
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c IPRange
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IPRange -> c IPRange
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IPRange -> c IPRange
$cp1Data :: Typeable IPRange
Data, (forall x. IPRange -> Rep IPRange x)
-> (forall x. Rep IPRange x -> IPRange) -> Generic IPRange
forall x. Rep IPRange x -> IPRange
forall x. IPRange -> Rep IPRange x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IPRange x -> IPRange
$cfrom :: forall x. IPRange -> Rep IPRange x
Generic, Typeable)

----------------------------------------------------------------
--
-- Range
--

{-|
  The Addr range consists of an address, a contiguous mask,
  and mask length. The contiguous mask and the mask length
  are essentially same information but contained for pre
  calculation.

  To create this, use 'makeAddrRange' or 'read' @\"192.0.2.0/24\"@ :: 'AddrRange' 'IPv4'.
  Also, @\"192.0.2.0/24\"@ can be used as literal with OverloadedStrings.

>>> read "192.0.2.1/24" :: AddrRange IPv4
192.0.2.0/24
>>> read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv6
2001:db8::/48
-}
data AddrRange a = AddrRange {
        -- |The 'addr' function returns an address from 'AddrRange'.
        AddrRange a -> a
addr :: !a
        -- |The 'mask' function returns a contiguous 'IP' mask from 'AddrRange'.
      , AddrRange a -> a
mask :: !a
        -- |The 'mlen' function returns a mask length from 'AddrRange'.
      , AddrRange a -> Int
mlen :: {-# UNPACK #-} !Int
    }
    deriving (AddrRange a -> AddrRange a -> Bool
(AddrRange a -> AddrRange a -> Bool)
-> (AddrRange a -> AddrRange a -> Bool) -> Eq (AddrRange a)
forall a. Eq a => AddrRange a -> AddrRange a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AddrRange a -> AddrRange a -> Bool
$c/= :: forall a. Eq a => AddrRange a -> AddrRange a -> Bool
== :: AddrRange a -> AddrRange a -> Bool
$c== :: forall a. Eq a => AddrRange a -> AddrRange a -> Bool
Eq, Eq (AddrRange a)
Eq (AddrRange a)
-> (AddrRange a -> AddrRange a -> Ordering)
-> (AddrRange a -> AddrRange a -> Bool)
-> (AddrRange a -> AddrRange a -> Bool)
-> (AddrRange a -> AddrRange a -> Bool)
-> (AddrRange a -> AddrRange a -> Bool)
-> (AddrRange a -> AddrRange a -> AddrRange a)
-> (AddrRange a -> AddrRange a -> AddrRange a)
-> Ord (AddrRange a)
AddrRange a -> AddrRange a -> Bool
AddrRange a -> AddrRange a -> Ordering
AddrRange a -> AddrRange a -> AddrRange a
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
forall a. Ord a => Eq (AddrRange a)
forall a. Ord a => AddrRange a -> AddrRange a -> Bool
forall a. Ord a => AddrRange a -> AddrRange a -> Ordering
forall a. Ord a => AddrRange a -> AddrRange a -> AddrRange a
min :: AddrRange a -> AddrRange a -> AddrRange a
$cmin :: forall a. Ord a => AddrRange a -> AddrRange a -> AddrRange a
max :: AddrRange a -> AddrRange a -> AddrRange a
$cmax :: forall a. Ord a => AddrRange a -> AddrRange a -> AddrRange a
>= :: AddrRange a -> AddrRange a -> Bool
$c>= :: forall a. Ord a => AddrRange a -> AddrRange a -> Bool
> :: AddrRange a -> AddrRange a -> Bool
$c> :: forall a. Ord a => AddrRange a -> AddrRange a -> Bool
<= :: AddrRange a -> AddrRange a -> Bool
$c<= :: forall a. Ord a => AddrRange a -> AddrRange a -> Bool
< :: AddrRange a -> AddrRange a -> Bool
$c< :: forall a. Ord a => AddrRange a -> AddrRange a -> Bool
compare :: AddrRange a -> AddrRange a -> Ordering
$ccompare :: forall a. Ord a => AddrRange a -> AddrRange a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (AddrRange a)
Ord, Typeable (AddrRange a)
DataType
Constr
Typeable (AddrRange a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> AddrRange a -> c (AddrRange a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (AddrRange a))
-> (AddrRange a -> Constr)
-> (AddrRange a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (AddrRange a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (AddrRange a)))
-> ((forall b. Data b => b -> b) -> AddrRange a -> AddrRange a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> AddrRange a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> AddrRange a -> r)
-> (forall u. (forall d. Data d => d -> u) -> AddrRange a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> AddrRange a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a))
-> Data (AddrRange a)
AddrRange a -> DataType
AddrRange a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (AddrRange a))
(forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AddrRange a -> c (AddrRange a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AddrRange a)
forall a. Data a => Typeable (AddrRange a)
forall a. Data a => AddrRange a -> DataType
forall a. Data a => AddrRange a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> AddrRange a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> AddrRange a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AddrRange a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AddrRange a -> c (AddrRange a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (AddrRange a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AddrRange a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> AddrRange a -> u
forall u. (forall d. Data d => d -> u) -> AddrRange a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AddrRange a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AddrRange a -> c (AddrRange a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (AddrRange a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AddrRange a))
$cAddrRange :: Constr
$tAddrRange :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
gmapMp :: (forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
gmapM :: (forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> AddrRange a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> AddrRange a -> u
gmapQ :: (forall d. Data d => d -> u) -> AddrRange a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> AddrRange a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
gmapT :: (forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AddrRange a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AddrRange a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (AddrRange a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (AddrRange a))
dataTypeOf :: AddrRange a -> DataType
$cdataTypeOf :: forall a. Data a => AddrRange a -> DataType
toConstr :: AddrRange a -> Constr
$ctoConstr :: forall a. Data a => AddrRange a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AddrRange a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AddrRange a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AddrRange a -> c (AddrRange a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AddrRange a -> c (AddrRange a)
$cp1Data :: forall a. Data a => Typeable (AddrRange a)
Data, (forall x. AddrRange a -> Rep (AddrRange a) x)
-> (forall x. Rep (AddrRange a) x -> AddrRange a)
-> Generic (AddrRange a)
forall x. Rep (AddrRange a) x -> AddrRange a
forall x. AddrRange a -> Rep (AddrRange a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (AddrRange a) x -> AddrRange a
forall a x. AddrRange a -> Rep (AddrRange a) x
$cto :: forall a x. Rep (AddrRange a) x -> AddrRange a
$cfrom :: forall a x. AddrRange a -> Rep (AddrRange a) x
Generic, Typeable)

----------------------------------------------------------------
--
-- Show
--

instance Show a => Show (AddrRange a) where
    show :: AddrRange a -> String
show AddrRange a
x = a -> String
forall a. Show a => a -> String
show (AddrRange a -> a
forall a. AddrRange a -> a
addr AddrRange a
x) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"/" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (AddrRange a -> Int
forall a. AddrRange a -> Int
mlen AddrRange a
x)

instance Show IPRange where
    show :: IPRange -> String
show (IPv4Range AddrRange IPv4
ip) = AddrRange IPv4 -> String
forall a. Show a => a -> String
show AddrRange IPv4
ip
    show (IPv6Range AddrRange IPv6
ip) = AddrRange IPv6 -> String
forall a. Show a => a -> String
show AddrRange IPv6
ip

----------------------------------------------------------------
--
-- Read
--

instance Read IPRange where
    readsPrec :: Int -> ReadS IPRange
readsPrec Int
_ = ReadS IPRange
parseIPRange

parseIPRange :: String -> [(IPRange,String)]
parseIPRange :: ReadS IPRange
parseIPRange String
cs = case MkParser String (AddrRange IPv4)
-> String -> (Maybe (AddrRange IPv4), String)
forall inp a. MkParser inp a -> inp -> (Maybe a, inp)
runParser MkParser String (AddrRange IPv4)
ip4range String
cs of
    (Just AddrRange IPv4
ip,String
rest) -> [(AddrRange IPv4 -> IPRange
IPv4Range AddrRange IPv4
ip,String
rest)]
    (Maybe (AddrRange IPv4)
Nothing,String
_)    -> case MkParser String (AddrRange IPv6)
-> String -> (Maybe (AddrRange IPv6), String)
forall inp a. MkParser inp a -> inp -> (Maybe a, inp)
runParser MkParser String (AddrRange IPv6)
ip6range String
cs of
        (Just AddrRange IPv6
ip,String
rest) -> [(AddrRange IPv6 -> IPRange
IPv6Range AddrRange IPv6
ip,String
rest)]
        (Maybe (AddrRange IPv6)
Nothing,String
_) -> []

instance Read (AddrRange IPv4) where
    readsPrec :: Int -> ReadS (AddrRange IPv4)
readsPrec Int
_ = ReadS (AddrRange IPv4)
parseIPv4Range

instance Read (AddrRange IPv6) where
    readsPrec :: Int -> ReadS (AddrRange IPv6)
readsPrec Int
_ = ReadS (AddrRange IPv6)
parseIPv6Range

parseIPv4Range :: String -> [(AddrRange IPv4,String)]
parseIPv4Range :: ReadS (AddrRange IPv4)
parseIPv4Range String
cs = case MkParser String (AddrRange IPv4)
-> String -> (Maybe (AddrRange IPv4), String)
forall inp a. MkParser inp a -> inp -> (Maybe a, inp)
runParser MkParser String (AddrRange IPv4)
ip4range String
cs of
    (Maybe (AddrRange IPv4)
Nothing,String
_)    -> []
    (Just AddrRange IPv4
a4,String
rest) -> [(AddrRange IPv4
a4,String
rest)]

parseIPv6Range :: String -> [(AddrRange IPv6,String)]
parseIPv6Range :: ReadS (AddrRange IPv6)
parseIPv6Range String
cs = case MkParser String (AddrRange IPv6)
-> String -> (Maybe (AddrRange IPv6), String)
forall inp a. MkParser inp a -> inp -> (Maybe a, inp)
runParser MkParser String (AddrRange IPv6)
ip6range String
cs of
    (Maybe (AddrRange IPv6)
Nothing,String
_)    -> []
    (Just AddrRange IPv6
a6,String
rest) -> [(AddrRange IPv6
a6,String
rest)]

maskLen :: Int -> Parser Int
maskLen :: Int -> Parser Int
maskLen Int
maxLen = do
  Bool
hasSlash <- Bool -> MkParser String Bool -> MkParser String Bool
forall a inp. a -> MkParser inp a -> MkParser inp a
option Bool
False (MkParser String Bool -> MkParser String Bool)
-> MkParser String Bool -> MkParser String Bool
forall a b. (a -> b) -> a -> b
$ Bool
True Bool -> MkParser String Char -> MkParser String Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> MkParser String Char
forall inp. Input inp => Char -> MkParser inp Char
char Char
'/'
  if Bool
hasSlash
    then Int
0 Int -> MkParser String Char -> Parser Int
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> MkParser String Char
forall inp. Input inp => Char -> MkParser inp Char
char Char
'0'
      Parser Int -> Parser Int -> Parser Int
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (String -> Parser Int
forall (m :: * -> *) (t :: * -> *).
(MonadFail m, Foldable t) =>
t Char -> m Int
toInt (String -> Parser Int) -> MkParser String String -> Parser Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (:) (Char -> ShowS) -> MkParser String Char -> MkParser String ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> MkParser String Char
forall inp. Input inp => String -> MkParser inp Char
oneOf [Char
'1'..Char
'9'] MkParser String ShowS
-> MkParser String String -> MkParser String String
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> MkParser String Char -> MkParser String String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many MkParser String Char
forall inp. Input inp => MkParser inp Char
digit)
    else Int -> Parser Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
maxLen
  where
    toInt :: t Char -> m Int
toInt t Char
ds = m Int -> (Int -> m Int) -> Maybe Int -> m Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> m Int
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"mask length") Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Int -> m Int) -> Maybe Int -> m Int
forall a b. (a -> b) -> a -> b
$ (Char -> (Int -> Maybe Int) -> Int -> Maybe Int)
-> (Int -> Maybe Int) -> t Char -> Int -> Maybe Int
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Char -> (Int -> Maybe Int) -> Int -> Maybe Int
forall a. Char -> (Int -> Maybe a) -> Int -> Maybe a
go Int -> Maybe Int
forall a. a -> Maybe a
Just t Char
ds Int
0
    go :: Char -> (Int -> Maybe a) -> Int -> Maybe a
go !Char
d !Int -> Maybe a
f !Int
n =
      let n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
ord Char
d Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
48
      in  if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxLen then Int -> Maybe a
f Int
n' else Maybe a
forall a. Maybe a
Nothing

ip4range :: Parser (AddrRange IPv4)
ip4range :: MkParser String (AddrRange IPv4)
ip4range = do
    IPv4
ip <- Parser IPv4
ip4
    Int
len <- Int -> Parser Int
maskLen Int
32
    let msk :: IPv4
msk = Int -> IPv4
maskIPv4 Int
len
        adr :: IPv4
adr = IPv4
ip IPv4 -> IPv4 -> IPv4
`maskedIPv4` IPv4
msk
    AddrRange IPv4 -> MkParser String (AddrRange IPv4)
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrRange IPv4 -> MkParser String (AddrRange IPv4))
-> AddrRange IPv4 -> MkParser String (AddrRange IPv4)
forall a b. (a -> b) -> a -> b
$ IPv4 -> IPv4 -> Int -> AddrRange IPv4
forall a. a -> a -> Int -> AddrRange a
AddrRange IPv4
adr IPv4
msk Int
len

maskedIPv4 :: IPv4 -> IPv4 -> IPv4
IP4 IPv4Addr
a maskedIPv4 :: IPv4 -> IPv4 -> IPv4
`maskedIPv4` IP4 IPv4Addr
m = IPv4Addr -> IPv4
IP4 (IPv4Addr
a IPv4Addr -> IPv4Addr -> IPv4Addr
forall a. Bits a => a -> a -> a
.&. IPv4Addr
m)

ip6range :: Parser (AddrRange IPv6)
ip6range :: MkParser String (AddrRange IPv6)
ip6range = do
    IPv6
ip <- Parser IPv6
ip6
    Int
len <- Int -> Parser Int
maskLen Int
128
    let msk :: IPv6
msk = Int -> IPv6
maskIPv6 Int
len
        adr :: IPv6
adr = IPv6
ip IPv6 -> IPv6 -> IPv6
`maskedIPv6` IPv6
msk
    AddrRange IPv6 -> MkParser String (AddrRange IPv6)
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrRange IPv6 -> MkParser String (AddrRange IPv6))
-> AddrRange IPv6 -> MkParser String (AddrRange IPv6)
forall a b. (a -> b) -> a -> b
$ IPv6 -> IPv6 -> Int -> AddrRange IPv6
forall a. a -> a -> Int -> AddrRange a
AddrRange IPv6
adr IPv6
msk Int
len

maskedIPv6 :: IPv6 -> IPv6 -> IPv6
IP6 (IPv4Addr
a1,IPv4Addr
a2,IPv4Addr
a3,IPv4Addr
a4) maskedIPv6 :: IPv6 -> IPv6 -> IPv6
`maskedIPv6` IP6 (IPv4Addr
m1,IPv4Addr
m2,IPv4Addr
m3,IPv4Addr
m4) = (IPv4Addr, IPv4Addr, IPv4Addr, IPv4Addr) -> IPv6
IP6 (IPv4Addr
a1IPv4Addr -> IPv4Addr -> IPv4Addr
forall a. Bits a => a -> a -> a
.&.IPv4Addr
m1,IPv4Addr
a2IPv4Addr -> IPv4Addr -> IPv4Addr
forall a. Bits a => a -> a -> a
.&.IPv4Addr
m2,IPv4Addr
a3IPv4Addr -> IPv4Addr -> IPv4Addr
forall a. Bits a => a -> a -> a
.&.IPv4Addr
m3,IPv4Addr
a4IPv4Addr -> IPv4Addr -> IPv4Addr
forall a. Bits a => a -> a -> a
.&.IPv4Addr
m4)



----------------------------------------------------------------
--
-- IsString
--

instance IsString IPRange where
    fromString :: String -> IPRange
fromString = String -> IPRange
forall a. Read a => String -> a
read

instance IsString (AddrRange IPv4) where
    fromString :: String -> AddrRange IPv4
fromString = String -> AddrRange IPv4
forall a. Read a => String -> a
read

instance IsString (AddrRange IPv6) where
    fromString :: String -> AddrRange IPv6
fromString = String -> AddrRange IPv6
forall a. Read a => String -> a
read