{-# 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
$c== :: IPRange -> IPRange -> Bool
== :: IPRange -> IPRange -> Bool
$c/= :: IPRange -> IPRange -> Bool
/= :: 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
$ccompare :: IPRange -> IPRange -> Ordering
compare :: IPRange -> IPRange -> Ordering
$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
>= :: IPRange -> IPRange -> Bool
$cmax :: IPRange -> IPRange -> IPRange
max :: IPRange -> IPRange -> IPRange
$cmin :: IPRange -> IPRange -> IPRange
min :: IPRange -> IPRange -> IPRange
Ord, Typeable IPRange
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 -> Constr
IPRange -> DataType
(forall b. Data b => b -> b) -> IPRange -> 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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IPRange -> c IPRange
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> IPRange -> c IPRange
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c IPRange
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c IPRange
$ctoConstr :: IPRange -> Constr
toConstr :: IPRange -> Constr
$cdataTypeOf :: IPRange -> DataType
dataTypeOf :: IPRange -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c IPRange)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c IPRange)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPRange)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPRange)
$cgmapT :: (forall b. Data b => b -> b) -> IPRange -> IPRange
gmapT :: (forall b. Data b => b -> b) -> IPRange -> IPRange
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> IPRange -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> IPRange -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> IPRange -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> IPRange -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> IPRange -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> IPRange -> m IPRange
gmapM :: forall (m :: * -> *).
Monad m =>
(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
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> IPRange -> m 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
$cfrom :: forall x. IPRange -> Rep IPRange x
from :: forall x. IPRange -> Rep IPRange x
$cto :: forall x. Rep IPRange x -> IPRange
to :: forall x. Rep IPRange x -> IPRange
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'.
        forall a. AddrRange a -> a
addr :: !a
        -- |The 'mask' function returns a contiguous 'IP' mask from 'AddrRange'.
      , forall a. AddrRange a -> a
mask :: !a
        -- |The 'mlen' function returns a mask length from 'AddrRange'.
      , forall a. 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
$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
/= :: 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
$ccompare :: forall a. Ord a => AddrRange a -> AddrRange a -> Ordering
compare :: AddrRange a -> AddrRange a -> Ordering
$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
>= :: AddrRange a -> AddrRange a -> Bool
$cmax :: forall a. Ord a => AddrRange a -> AddrRange a -> AddrRange a
max :: AddrRange a -> AddrRange a -> AddrRange a
$cmin :: forall a. Ord a => AddrRange a -> AddrRange a -> AddrRange a
min :: AddrRange a -> AddrRange a -> AddrRange a
Ord, Typeable (AddrRange a)
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 -> Constr
AddrRange a -> DataType
(forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
forall a. Data a => Typeable (AddrRange a)
forall a. Data a => AddrRange a -> Constr
forall a. Data a => AddrRange a -> DataType
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))
$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)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AddrRange a -> 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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AddrRange a)
$ctoConstr :: forall a. Data a => AddrRange a -> Constr
toConstr :: AddrRange a -> Constr
$cdataTypeOf :: forall a. Data a => AddrRange a -> DataType
dataTypeOf :: AddrRange a -> DataType
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (AddrRange a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AddrRange a))
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
gmapT :: (forall b. Data b => b -> b) -> AddrRange a -> AddrRange a
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AddrRange a -> r
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> AddrRange a -> [u]
gmapQ :: forall u. (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
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> AddrRange a -> u
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (AddrRange a)
gmapM :: forall (m :: * -> *).
Monad m =>
(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)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AddrRange a -> m (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
$cfrom :: forall a x. AddrRange a -> Rep (AddrRange a) x
from :: forall x. AddrRange a -> Rep (AddrRange a) x
$cto :: forall a x. Rep (AddrRange a) x -> AddrRange a
to :: forall x. Rep (AddrRange a) x -> AddrRange a
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 a b. a -> MkParser String b -> MkParser String a
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 a b. a -> MkParser String b -> MkParser String a
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 a.
MkParser String a -> MkParser String a -> MkParser String a
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 a b.
MkParser String (a -> b) -> MkParser String a -> MkParser String b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> MkParser String Char -> MkParser String String
forall a. MkParser String a -> MkParser String [a]
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 a. a -> MkParser String a
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 a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"mask length") Int -> m Int
forall a. a -> m a
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 a b. (a -> b -> b) -> b -> t a -> b
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
    Parser ()
skipSpaces
    IPv4
ip <- [Int] -> IPv4
toIPv4 ([Int] -> IPv4) -> MkParser String [Int] -> MkParser String IPv4
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> MkParser String [Int]
ip4' Bool
False
    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 a. a -> MkParser String a
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 Word32
a maskedIPv4 :: IPv4 -> IPv4 -> IPv4
`maskedIPv4` IP4 Word32
m = Word32 -> IPv4
IP4 (Word32
a Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
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 a. a -> MkParser String a
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 (Word32
a1,Word32
a2,Word32
a3,Word32
a4) maskedIPv6 :: IPv6 -> IPv6 -> IPv6
`maskedIPv6` IP6 (Word32
m1,Word32
m2,Word32
m3,Word32
m4) = (Word32, Word32, Word32, Word32) -> IPv6
IP6 (Word32
a1Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&.Word32
m1,Word32
a2Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&.Word32
m2,Word32
a3Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&.Word32
m3,Word32
a4Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&.Word32
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