-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Library for IP and MAC addresses
--   
--   The <a>ip</a> package provides types and functions for dealing with
--   IPv4 addresses, CIDR blocks, and MAC addresses. We provide instances
--   for typeclasses found in commonly used packages like <a>aeson</a>,
--   <a>vector</a>, and <a>hashable</a>. We also provide <a>Parser</a>s for
--   working with attoparsec.
--   
--   Notably, this package does not overload functions by introducing any
--   typeclasses of its own. Neither does it prefix functions with the name
--   of the type that they work on. Instead, functions of the same name are
--   exported by several different modules, and it is expected that end
--   users disambiguate by importing these modules qualified.
--   
--   The only module intended to be imported unqualified is
--   <a>Net.Types</a>. The types in this package should not conflict with
--   the types in any other commonly used packages.
--   
--   The following packages are intended to be used with this package:
--   
--   <ul>
--   <li>`yesod-ip`: Provides orphan instances needed to work with yesod
--   and persistent. Also, provides a `yesod-form` helper.</li>
--   </ul>
@package ip
@version 1.4.2.1


-- | This module provides the IPv4 data type and functions for working with
--   it.
module Net.IPv4

-- | Create an <a>IPv4</a> address from four octets. The first argument is
--   the most significant octet. The last argument is the least
--   significant. Since IP addresses are commonly written using dot-decimal
--   notation, this is the recommended way to create an IP address.
--   Additionally, it is used for the <a>Show</a> and <a>Read</a> instances
--   of <a>IPv4</a> to help keep things readable in GHCi.
--   
--   <pre>
--   &gt;&gt;&gt; let addr = ipv4 192 168 1 1
--   
--   &gt;&gt;&gt; addr
--   ipv4 192 168 1 1
--   
--   &gt;&gt;&gt; getIPv4 addr
--   3232235777
--   </pre>
ipv4 :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4

-- | An alias for the <a>ipv4</a> smart constructor.
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4

-- | An uncurried variant of <a>fromOctets</a>.
fromTupleOctets :: (Word8, Word8, Word8, Word8) -> IPv4

-- | Convert an <a>IPv4</a> address into a quadruple of octets. The first
--   element in the quadruple is the most significant octet. The last
--   element is the least significant octet.
toOctets :: IPv4 -> (Word8, Word8, Word8, Word8)

-- | The IP address representing any host.
--   
--   <pre>
--   &gt;&gt;&gt; any
--   ipv4 0 0 0 0
--   </pre>
any :: IPv4

-- | The local loopback IP address.
--   
--   <pre>
--   &gt;&gt;&gt; loopback
--   ipv4 127 0 0 1
--   </pre>
loopback :: IPv4

-- | A useful and common alias for <a>loopback</a>.
--   
--   <pre>
--   &gt;&gt;&gt; localhost
--   ipv4 127 0 0 1
--   </pre>
localhost :: IPv4

-- | The broadcast IP address.
--   
--   <pre>
--   &gt;&gt;&gt; broadcast
--   ipv4 255 255 255 255
--   </pre>
broadcast :: IPv4

-- | Checks to see if the <a>IPv4</a> address belongs to a private network.
--   The three private networks that are checked are <tt>10.0.0.0/8</tt>,
--   <tt>172.16.0.0/12</tt>, and <tt>192.168.0.0/16</tt>.
private :: IPv4 -> Bool

-- | Checks to see if the <a>IPv4</a> address belongs to a reserved
--   network. This includes the three private networks that <a>private</a>
--   checks along with several other ranges that are not used on the public
--   Internet.
reserved :: IPv4 -> Bool

-- | Checks to see if the <a>IPv4</a> address is publicly routable.
--   
--   <pre>
--   public x == not (reserved x)
--   </pre>
public :: IPv4 -> Bool

-- | Encode an <a>IPv4</a> address to <a>Text</a> using dot-decimal
--   notation:
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn (encode (ipv4 192 168 2 47))
--   192.168.2.47
--   </pre>
encode :: IPv4 -> Text

-- | Decode an <a>IPv4</a> address.
--   
--   <pre>
--   &gt;&gt;&gt; decode (Text.pack "192.168.2.47")
--   Just (ipv4 192 168 2 47)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; decode (Text.pack "10.100.256.256")
--   Nothing
--   </pre>
decode :: Text -> Maybe IPv4

-- | Encode an <a>IPv4</a> address to a text <a>Builder</a>.
--   
--   <pre>
--   &gt;&gt;&gt; builder (ipv4 192 168 2 47)
--   "192.168.2.47"
--   </pre>
builder :: IPv4 -> Builder

-- | Parse an <a>IPv4</a> address using a <a>Reader</a>.
--   
--   <pre>
--   &gt;&gt;&gt; reader (Text.pack "192.168.2.47")
--   Right (ipv4 192 168 2 47,"")
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; reader (Text.pack "192.168.2.470")
--   Left "All octets in an IPv4 address must be between 0 and 255"
--   </pre>
reader :: Reader IPv4

-- | Parse an <a>IPv4</a> address using a <a>Parser</a>.
--   
--   <pre>
--   &gt;&gt;&gt; AT.parseOnly parser (Text.pack "192.168.2.47")
--   Right (ipv4 192 168 2 47)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; AT.parseOnly parser (Text.pack "192.168.2.470")
--   Left "Failed reading: All octets in an IPv4 address must be between 0 and 255"
--   </pre>
parser :: Parser IPv4

-- | Encode an <a>IPv4</a> address to a UTF-8 encoded <a>ByteString</a>.
--   
--   <pre>
--   &gt;&gt;&gt; encodeUtf8 (ipv4 192 168 2 47)
--   "192.168.2.47"
--   </pre>
encodeUtf8 :: IPv4 -> ByteString

-- | Decode a UTF8-encoded <a>ByteString</a> into an <a>IPv4</a>.
--   
--   <pre>
--   &gt;&gt;&gt; decodeUtf8 (BC8.pack "192.168.2.47")
--   Just (ipv4 192 168 2 47)
--   </pre>
decodeUtf8 :: ByteString -> Maybe IPv4

-- | Encode an <a>IPv4</a> as a <a>Builder</a>
builderUtf8 :: IPv4 -> Builder

-- | Parse an <a>IPv4</a> using a <a>Parser</a>.
--   
--   <pre>
--   &gt;&gt;&gt; AB.parseOnly parserUtf8 (BC8.pack "192.168.2.47")
--   Right (ipv4 192 168 2 47)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; AB.parseOnly parserUtf8 (BC8.pack "192.168.2.470")
--   Left "Failed reading: All octets in an ipv4 address must be between 0 and 255"
--   </pre>
parserUtf8 :: Parser IPv4

-- | Encode an <a>IPv4</a> as a <a>String</a>.
encodeString :: IPv4 -> String

-- | Decode an <a>IPv4</a> from a <a>String</a>.
decodeString :: String -> Maybe IPv4

-- | Print an <a>IPv4</a> using the textual encoding.
print :: IPv4 -> IO ()

-- | Smart constructor for <a>IPv4Range</a>. Ensures the mask is
--   appropriately sized and sets masked bits in the <a>IPv4</a> to zero.
range :: IPv4 -> Word8 -> IPv4Range

-- | Given an inclusive lower and upper ip address, create the smallest
--   <a>IPv4Range</a> that contains the two. This is helpful in situations
--   where input given as a range like <tt>192.168.16.0-192.168.19.255</tt>
--   needs to be handled. This makes the range broader if it cannot be
--   represented in CIDR notation.
--   
--   <pre>
--   &gt;&gt;&gt; printRange $ fromBounds (fromOctets 192 168 16 0) (fromOctets 192 168 19 255)
--   192.168.16.0/22
--   
--   &gt;&gt;&gt; printRange $ fromBounds (fromOctets 10 0 5 7) (fromOctets 10 0 5 14)
--   10.0.5.0/28
--   </pre>
fromBounds :: IPv4 -> IPv4 -> IPv4Range

-- | Normalize an <a>IPv4Range</a>. The first result of this is that the
--   <a>IPv4</a> inside the <a>IPv4Range</a> is changed so that the
--   insignificant bits are zeroed out. For example:
--   
--   <pre>
--   &gt;&gt;&gt; printRange $ normalize $ IPv4Range (fromOctets 192 168 1 19) 24
--   192.168.1.0/24
--   
--   &gt;&gt;&gt; printRange $ normalize $ IPv4Range (fromOctets 192 168 1 163) 28
--   192.168.1.160/28
--   </pre>
--   
--   The second effect of this is that the mask length is lowered to be 32
--   or smaller. Working with <a>IPv4Range</a>s that have not been
--   normalized does not cause any issues for this library, although other
--   applications may reject such ranges (especially those with a mask
--   length above 32).
--   
--   Note that <a>normalize</a> is idempotent, that is:
--   
--   <pre>
--   normalize r == (normalize . normalize) r
--   </pre>
normalize :: IPv4Range -> IPv4Range

-- | Checks to see if an <a>IPv4</a> address belongs in the
--   <a>IPv4Range</a>.
--   
--   <pre>
--   &gt;&gt;&gt; let ip = fromOctets 10 10 1 92
--   
--   &gt;&gt;&gt; contains (IPv4Range (fromOctets 10 0 0 0) 8) ip
--   True
--   
--   &gt;&gt;&gt; contains (IPv4Range (fromOctets 10 11 0 0) 16) ip
--   False
--   </pre>
--   
--   Typically, element-testing functions are written to take the element
--   as the first argument and the set as the second argument. This is
--   intentionally written the other way for better performance when
--   iterating over a collection. For example, you might test elements in a
--   list for membership like this:
--   
--   <pre>
--   &gt;&gt;&gt; let r = IPv4Range (fromOctets 10 10 10 6) 31
--   
--   &gt;&gt;&gt; mapM_ (P.print . contains r) (take 5 $ iterate succ $ fromOctets 10 10 10 5)
--   False
--   True
--   True
--   False
--   False
--   </pre>
--   
--   The implementation of <a>contains</a> ensures that (with GHC), the
--   bitmask creation and range normalization only occur once in the above
--   example. They are reused as the list is iterated.
contains :: IPv4Range -> IPv4 -> Bool

-- | This is provided to mirror the interface provided by
--   <tt>Data.Set</tt>. It behaves just like <a>contains</a> but with
--   flipped arguments.
--   
--   <pre>
--   member ip r == contains r ip
--   </pre>
member :: IPv4 -> IPv4Range -> Bool

-- | The inclusive lower bound of an <a>IPv4Range</a>. This is
--   conventionally understood to be the broadcast address of a subnet. For
--   example:
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn $ encode $ lowerInclusive $ IPv4Range (ipv4 10 10 1 160) 25
--   10.10.1.128
--   </pre>
--   
--   Note that the lower bound of a normalized <a>IPv4Range</a> is simply
--   the ip address of the range:
--   
--   <pre>
--   lowerInclusive r == ipv4RangeBase (normalize r)
--   </pre>
lowerInclusive :: IPv4Range -> IPv4

-- | The inclusive upper bound of an <a>IPv4Range</a>.
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn $ encode $ upperInclusive $ IPv4Range (ipv4 10 10 1 160) 25
--   10.10.1.255
--   </pre>
upperInclusive :: IPv4Range -> IPv4

-- | Convert an <a>IPv4Range</a> into a list of the <a>IPv4</a> addresses
--   that are in it.
--   
--   <pre>
--   &gt;&gt;&gt; let r = IPv4Range (fromOctets 192 168 1 8) 30
--   
--   &gt;&gt;&gt; mapM_ (T.putStrLn . encode) (toList r)
--   192.168.1.8
--   192.168.1.9
--   192.168.1.10
--   192.168.1.11
--   </pre>
toList :: IPv4Range -> [IPv4]

-- | A stream-polymorphic generator over an <a>IPv4Range</a>. For more
--   information, see <a>How to build library-agnostic streaming
--   sources</a>.
toGenerator :: MonadPlus m => IPv4Range -> m IPv4

-- | The RFC1918 24-bit block. Subnet mask: <tt>10.0.0.0/8</tt>
private24 :: IPv4Range

-- | The RFC1918 20-bit block. Subnet mask: <tt>172.16.0.0/12</tt>
private20 :: IPv4Range

-- | The RFC1918 16-bit block. Subnet mask: <tt>192.168.0.0/16</tt>
private16 :: IPv4Range

-- | Encode an <a>IPv4Range</a> as <a>Text</a>.
encodeRange :: IPv4Range -> Text

-- | Decode an <a>IPv4Range</a> from <a>Text</a>.
decodeRange :: Text -> Maybe IPv4Range

-- | Encode an <a>IPv4Range</a> to a <a>Builder</a>.
builderRange :: IPv4Range -> Builder

-- | Parse an <a>IPv4Range</a> using a 'AT.Parser.'
parserRange :: Parser IPv4Range

-- | This exists mostly for testing purposes.
printRange :: IPv4Range -> IO ()

-- | A 32-bit Internet Protocol version 4 address. To use this with the
--   <tt>network</tt> library, it is necessary to use
--   <tt>Network.Socket.htonl</tt> to convert the underlying <a>Word32</a>
--   from host byte order to network byte order.
newtype IPv4
IPv4 :: Word32 -> IPv4
[getIPv4] :: IPv4 -> Word32

-- | The length should be between 0 and 32. These bounds are inclusive.
--   This expectation is not in any way enforced by this library because it
--   does not cause errors. A mask length greater than 32 will be treated
--   as if it were 32.
data IPv4Range
IPv4Range :: {-# UNPACK #-} !IPv4 -> {-# UNPACK #-} !Word8 -> IPv4Range
[ipv4RangeBase] :: IPv4Range -> {-# UNPACK #-} !IPv4
[ipv4RangeLength] :: IPv4Range -> {-# UNPACK #-} !Word8
instance GHC.Generics.Generic Net.IPv4.IPv4Range
instance GHC.Read.Read Net.IPv4.IPv4Range
instance GHC.Show.Show Net.IPv4.IPv4Range
instance GHC.Classes.Ord Net.IPv4.IPv4Range
instance GHC.Classes.Eq Net.IPv4.IPv4Range
instance Foreign.Storable.Storable Net.IPv4.IPv4
instance Data.Primitive.Types.Prim Net.IPv4.IPv4
instance GHC.Generics.Generic Net.IPv4.IPv4
instance Data.Hashable.Class.Hashable Net.IPv4.IPv4
instance GHC.Enum.Bounded Net.IPv4.IPv4
instance GHC.Enum.Enum Net.IPv4.IPv4
instance GHC.Classes.Ord Net.IPv4.IPv4
instance GHC.Classes.Eq Net.IPv4.IPv4
instance Control.DeepSeq.NFData Net.IPv4.IPv4Range
instance Data.Hashable.Class.Hashable Net.IPv4.IPv4Range
instance Data.Aeson.Types.ToJSON.ToJSON Net.IPv4.IPv4Range
instance Data.Aeson.Types.FromJSON.FromJSON Net.IPv4.IPv4Range
instance Data.Vector.Unboxed.Base.Unbox Net.IPv4.IPv4Range
instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Unboxed.Base.MVector Net.IPv4.IPv4Range
instance Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector Net.IPv4.IPv4Range
instance Data.Bits.Bits Net.IPv4.IPv4Range
instance Data.Bits.FiniteBits Net.IPv4.IPv4Range
instance Control.DeepSeq.NFData Net.IPv4.IPv4
instance GHC.Show.Show Net.IPv4.IPv4
instance GHC.Read.Read Net.IPv4.IPv4
instance Data.Vector.Unboxed.Base.Unbox Net.IPv4.IPv4
instance Data.Vector.Generic.Mutable.Base.MVector Data.Vector.Unboxed.Base.MVector Net.IPv4.IPv4
instance Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector Net.IPv4.IPv4
instance Data.Aeson.Types.ToJSON.ToJSON Net.IPv4.IPv4
instance Data.Aeson.Types.FromJSON.FromJSON Net.IPv4.IPv4
instance Data.Aeson.Types.ToJSON.ToJSONKey Net.IPv4.IPv4
instance Data.Aeson.Types.FromJSON.FromJSONKey Net.IPv4.IPv4
instance Data.Bits.Bits Net.IPv4.IPv4
instance Data.Bits.FiniteBits Net.IPv4.IPv4


-- | This module provides the IPv6 data type and functions for working with
--   it.
module Net.IPv6

-- | Create an <a>IPv6</a> address from the eight 16-bit fragments that
--   make it up. This closely resembles the standard IPv6 notation, so is
--   used for the <a>Show</a> instance. Note that this lacks the formatting
--   feature for suppress zeroes in an <a>IPv6</a> address, but it should
--   be readable enough for hacking in GHCi.
--   
--   <pre>
--   &gt;&gt;&gt; let addr = ipv6 0x3124 0x0 0x0 0xDEAD 0xCAFE 0xFF 0xFE00 0x1
--   
--   &gt;&gt;&gt; addr
--   ipv6 0x3124 0x0000 0x0000 0xdead 0xcafe 0x00ff 0xfe00 0x0001
--   
--   &gt;&gt;&gt; T.putStrLn (encode addr)
--   3124::dead:cafe:ff:fe00:1
--   </pre>
ipv6 :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IPv6

-- | This could be useful for the rare occasion in which one could
--   construct an <a>IPv6</a> from octets.
--   
--   Note that while <tt>Net.IPv4.<a>fromOctets</a> =
--   Net.IPv4.<a>ipv4</a></tt>, <tt>Net.IPv6.fromOctets /=
--   Net.IPv6.ipv6</tt>. While this should be obvious from their types, it
--   is worth mentioning since the similarity in naming might be confusing.
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> IPv6

-- | An alias for the <a>ipv6</a> smart constructor.
fromWord16s :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IPv6

-- | Build an <a>IPv6</a> from four 32-bit words. The leftmost argument is
--   the high word and the rightword is the low word.
fromWord32s :: Word32 -> Word32 -> Word32 -> Word32 -> IPv6

-- | Uncurried variant of <a>fromWord16s</a>.
fromTupleWord16s :: (Word16, Word16, Word16, Word16, Word16, Word16, Word16, Word16) -> IPv6

-- | Uncurried variant of <a>fromWord32s</a>.
fromTupleWord32s :: (Word32, Word32, Word32, Word32) -> IPv6

-- | Convert an <a>IPv6</a> to eight 16-bit words.
toWord16s :: IPv6 -> (Word16, Word16, Word16, Word16, Word16, Word16, Word16, Word16)

-- | Convert an <a>IPv6</a> to four 32-bit words.
toWord32s :: IPv6 -> (Word32, Word32, Word32, Word32)

-- | The IP address representing any host.
--   
--   <pre>
--   &gt;&gt;&gt; any
--   ipv6 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
--   </pre>
any :: IPv6

-- | The local loopback IP address.
--   
--   <pre>
--   &gt;&gt;&gt; loopback
--   ipv6 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001
--   </pre>
loopback :: IPv6

-- | A useful alias for <a>loopback</a>.
--   
--   <pre>
--   &gt;&gt;&gt; localhost
--   ipv6 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001
--   </pre>
localhost :: IPv6

-- | Encodes the IP, using zero-compression on the leftmost-longest string
--   of zeroes in the address. Per <a>RFC 5952 Section 5</a>, this uses
--   mixed notation when encoding an IPv4-mapped IPv6 address:
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn $ encode $ fromWord16s 0xDEAD 0xBEEF 0x0 0x0 0x0 0x0 0x0 0x1234
--   dead:beef::1234
--   
--   &gt;&gt;&gt; T.putStrLn $ encode $ fromWord16s 0x0 0x0 0x0 0x0 0x0 0xFFFF 0x6437 0xA5B4
--   ::ffff:100.55.165.180
--   
--   &gt;&gt;&gt; T.putStrLn $ encode $ fromWord16s 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
--   ::
--   </pre>
encode :: IPv6 -> Text

-- | Decode an IPv6 address. This accepts both standard IPv6 notation (with
--   zero compression) and mixed notation for IPv4-mapped IPv6 addresses.
decode :: Text -> Maybe IPv6

-- | Parse an <a>IPv6</a> using <a>Parser</a>.
--   
--   <pre>
--   &gt;&gt;&gt; ip = ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
--   
--   &gt;&gt;&gt; Atto.parseOnly parser (Text.pack "dead:beef:3240:a426:ba68:1cd0:4263:109b")
--   Right (ipv6 0xdead 0xbeef 0x3240 0xa426 0xba68 0x1cd0 0x4263 0x109b)
--   </pre>
parser :: Parser IPv6

-- | Print an <a>IPv6</a> using the textual encoding.
print :: IPv6 -> IO ()

-- | Smart constructor for <a>IPv6Range</a>. Ensures the mask is
--   appropriately sized and sets masked bits in the <a>IPv6</a> to zero.
--   
--   <pre>
--   &gt;&gt;&gt; let addr = ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
--   
--   &gt;&gt;&gt; printRange $ range addr 25
--   dead:be80::/25
--   </pre>
range :: IPv6 -> Word8 -> IPv6Range

-- | Given an inclusive lower and upper ip address, create the smallest
--   <a>IPv6Range</a> that contains the two. This is helpful in situations
--   where input is given as a range, like <tt> </tt>.
--   
--   This makes the range broader if it cannot be represented in
--   <a>CIDR</a> notation.
--   
--   <pre>
--   &gt;&gt;&gt; addrLower = ipv6 0xDEAD 0xBE80 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
--   
--   &gt;&gt;&gt; addrUpper = ipv6 0xDEAD 0xBEFF 0xFFFF 0xFFFF 0xFFFF 0xFFFF 0xFFFF 0xFFFF
--   
--   &gt;&gt;&gt; printRange $ fromBounds addrLower addrUpper
--   dead:be80::/25
--   </pre>
fromBounds :: IPv6 -> IPv6 -> IPv6Range

-- | Normalize an <a>IPv6Range</a>. The first result of this is that the
--   <a>IPv6</a> inside the <a>IPv6Range</a> is changed so that the
--   insignificant bits are zeroed out. For example:
--   
--   <pre>
--   &gt;&gt;&gt; addr1 = ipv6 0x0192 0x0168 0x0001 0x0019 0x0000 0x0000 0x0000 0x0000
--   
--   &gt;&gt;&gt; addr2 = ipv6 0x0192 0x0168 0x0001 0x0163 0x0000 0x0000 0x0000 0x0000
--   
--   &gt;&gt;&gt; printRange $ normalize $ IPv6Range addr1 24
--   192:100::/24 
--   
--   &gt;&gt;&gt; printRange $ normalize $ IPv6Range addr2 28
--   192:160::/28
--   </pre>
--   
--   The second effect of this is that the mask length is lowered to be 128
--   or smaller. Working with <a>IPv6Range</a>s that have not been
--   normalized does not cause any issues for this library, although other
--   applications may reject such ranges (especially those with a mask
--   length above 128).
--   
--   Note that 'normalize is idempotent, that is:
--   
--   <pre>
--   normalize r == (normalize . normalize) r
--   </pre>
normalize :: IPv6Range -> IPv6Range

-- | Checks to see if an <a>IPv6</a> address belongs in the
--   <a>IPv6Range</a>.
--   
--   <pre>
--   &gt;&gt;&gt; let ip = ipv6 0x2001 0x0db8 0x0db8 0x1094 0x2051 0x0000 0x0000 0x0001
--   
--   &gt;&gt;&gt; let iprange mask = IPv6Range (ipv6 0x2001 0x0db8 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001) mask
--   
--   &gt;&gt;&gt; contains (iprange 8) ip
--   True
--   
--   &gt;&gt;&gt; contains (iprange 48) ip
--   False
--   </pre>
--   
--   Typically, element-testing functions are written to take the element
--   as the first argument and the set as the second argument. This is
--   intentionally written the other way for better performance when
--   iterating over a collection. For example, you might test elements in a
--   list for membership like this:
--   
--   <pre>
--   &gt;&gt;&gt; let r = IPv6Range (ipv6 0x2001 0x0db8 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001) 64
--   
--   &gt;&gt;&gt; fmap (contains r) (take 5 $ iterate succ $ ipv6 0x2001 0x0db8 0x0000 0x0000 0xffff 0xffff 0xffff 0xfffe)
--   [True,True,False,False,False]
--   </pre>
--   
--   The implementation of <a>contains</a> ensures that (with GHC), the
--   bitmask creation and range normalization only occur once in the above
--   example. They are reused as the list is iterated.
contains :: IPv6Range -> IPv6 -> Bool

-- | This is provided to mirror the interface provided by
--   <tt>Data.Set</tt>. It behaves just like <a>contains</a> but with
--   flipped arguments.
--   
--   <pre>
--   member ip r == contains r ip
--   </pre>
member :: IPv6 -> IPv6Range -> Bool

-- | The inclusive lower bound of an <a>IPv6Range</a>. This is
--   conventionally understood to be the broadcast address of a subnet. For
--   example:
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn $ encode $ lowerInclusive $ IPv6Range (ipv6 0x2001 0x0db8 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001) 25
--   2001:d80::
--   </pre>
--   
--   Note that the lower bound of a normalized <a>IPv6Range</a> is simply
--   the ip address of the range:
--   
--   <pre>
--   lowerInclusive r == ipv6RangeBase (normalize r)
--   </pre>
lowerInclusive :: IPv6Range -> IPv6

-- | The inclusive upper bound of an <a>IPv6Range</a>.
--   
--   <pre>
--   &gt;&gt;&gt; let addr = ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
--   
--   &gt;&gt;&gt; T.putStrLn $ encode $ upperInclusive $ IPv6Range addr 25
--   dead:beff:ffff:ffff:ffff:ffff:ffff:ffff
--   </pre>
upperInclusive :: IPv6Range -> IPv6

-- | Encode an <a>IPv6Range</a> as <a>Text</a>.
--   
--   <pre>
--   &gt;&gt;&gt; addr = ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
--   
--   &gt;&gt;&gt; T.putStrLn $ encodeRange $ IPv6Range addr 28
--   dead:beef:3240:a426:ba68:1cd0:4263:109b/28
--   </pre>
encodeRange :: IPv6Range -> Text

-- | Decode an <a>IPv6Range</a> from <a>Text</a>.
--   
--   <pre>
--   &gt;&gt;&gt; addr = ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
--   
--   &gt;&gt;&gt; fmap encodeRange $ decodeRange (Text.pack "dead:beef:3240:a426:ba68:1cd0:4263:109b/28")
--   Just "dead:bee0::/28"
--   </pre>
decodeRange :: Text -> Maybe IPv6Range

-- | Parse an <a>IPv6Range</a> using a <a>Parser</a>.
parserRange :: Parser IPv6Range

-- | Print an <a>IPv6Range</a> using the textual encoding.
printRange :: IPv6Range -> IO ()

-- | A 128-bit Internet Protocol version 6 address.
data IPv6
IPv6 :: {-# UNPACK #-} !Word64 -> {-# UNPACK #-} !Word64 -> IPv6
[ipv6A] :: IPv6 -> {-# UNPACK #-} !Word64
[ipv6B] :: IPv6 -> {-# UNPACK #-} !Word64

-- | An <a>IPv6Range</a>. It is made up of the first <a>IPv6</a> in the
--   range and its length.
data IPv6Range
IPv6Range :: {-# UNPACK #-} !IPv6 -> {-# UNPACK #-} !Word8 -> IPv6Range
[ipv6RangeBase] :: IPv6Range -> {-# UNPACK #-} !IPv6
[ipv6RangeLength] :: IPv6Range -> {-# UNPACK #-} !Word8
instance GHC.Generics.Generic Net.IPv6.IPv6Range
instance GHC.Read.Read Net.IPv6.IPv6Range
instance GHC.Show.Show Net.IPv6.IPv6Range
instance GHC.Classes.Ord Net.IPv6.IPv6Range
instance GHC.Classes.Eq Net.IPv6.IPv6Range
instance GHC.Generics.Generic Net.IPv6.IPv6
instance GHC.Classes.Ord Net.IPv6.IPv6
instance GHC.Classes.Eq Net.IPv6.IPv6
instance Control.DeepSeq.NFData Net.IPv6.IPv6Range
instance Control.DeepSeq.NFData Net.IPv6.IPv6
instance GHC.Enum.Enum Net.IPv6.IPv6
instance GHC.Enum.Bounded Net.IPv6.IPv6
instance GHC.Show.Show Net.IPv6.IPv6
instance Data.Primitive.Types.Prim Net.IPv6.IPv6
instance GHC.Read.Read Net.IPv6.IPv6
instance Data.Aeson.Types.ToJSON.ToJSON Net.IPv6.IPv6
instance Data.Aeson.Types.FromJSON.FromJSON Net.IPv6.IPv6


-- | An IP data type representing either an IPv4 address or an IPv6
--   address. The user can think of this as though it were a sum type.
--   However, to minimize indirections, it is actually implemented as an
--   <a>IPv6</a> address, with <a>IPv4</a> addresses being represented as
--   an IPv4-mapped IPv6 addresses:
--   
--   <pre>
--   +---------+---------+--------------+
--   | 80 bits | 16 bits | 32 bits      |
--   +---------+---------+--------------+
--   | 00...00 | FFFF    | IPv4 address |
--   +---------+---------+--------------+
--   </pre>
--   
--   All functions and instance methods that deal with textual conversion
--   will encode an <a>IP</a> using either dot-decimal notation (for IPv4)
--   or RFC 5952 (for IPv6). They will decode an <a>IP</a> from either
--   format as well. The <a>Show</a> instance presents an address in as
--   valid haskell code that resembles the formatted address:
--   
--   <pre>
--   &gt;&gt;&gt; decode "192.168.3.100"
--   Just (ipv4 192 168 3 100)
--   
--   &gt;&gt;&gt; decode "A3F5:12:F26::1466:8B91"
--   Just (ipv6 0xa3f5 0x0012 0x0f26 0x0000 0x0000 0x0000 0x1466 0x8b91)
--   </pre>
module Net.IP

-- | Run a function over an <a>IP</a> depending on its status as an
--   <a>IPv4</a> or <a>IPv6</a>.
--   
--   <pre>
--   &gt;&gt;&gt; case_ IPv4.encode IPv6.encode (ipv4 192 168 2 47)
--   "192.168.2.47"
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; addr = ipv6 0x2001 0x0db8 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001
--   
--   &gt;&gt;&gt; case_ IPv4.encode IPv6.encode addr
--   "2001:db8::1"
--   </pre>
case_ :: (IPv4 -> a) -> (IPv6 -> a) -> IP -> a

-- | Construct an <a>IP</a> address from the four octets of an IPv4
--   address.
ipv4 :: Word8 -> Word8 -> Word8 -> Word8 -> IP

-- | Construct an <a>IP</a> address from the eight 16-bit chunks of an IPv6
--   address.
ipv6 :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IP

-- | Turn an <a>IPv4</a> into an <a>IP</a>.
fromIPv4 :: IPv4 -> IP

-- | Turn an <a>IPv6</a> into an <a>IP</a>.
fromIPv6 :: IPv6 -> IP

-- | Encode an <a>IP</a> as <a>Text</a>.
encode :: IP -> Text

-- | Decode an <a>IP</a> from <a>Text</a>.
decode :: Text -> Maybe IP

-- | Print an <a>IP</a> using the textual encoding.
print :: IP -> IO ()

-- | A 32-bit <a>IPv4</a> address or a 128-bit <a>IPv6</a> address.
--   Internally, this is just represented as an <a>IPv6</a> address. The
--   functions provided in <tt>Net.IP</tt> help simulate constructing and
--   pattern matching on values of this type. All functions and typeclass
--   methods that convert <a>IP</a> values to text will display it as an
--   <a>IPv4</a> address if possible.
newtype IP
IP :: IPv6 -> IP
[getIP] :: IP -> IPv6
instance GHC.Generics.Generic Net.IP.IP
instance GHC.Classes.Ord Net.IP.IP
instance GHC.Classes.Eq Net.IP.IP
instance Control.DeepSeq.NFData Net.IP.IP
instance GHC.Show.Show Net.IP.IP
instance GHC.Read.Read Net.IP.IP
instance Data.Aeson.Types.ToJSON.ToJSON Net.IP.IP
instance Data.Aeson.Types.FromJSON.FromJSON Net.IP.IP


-- | This module provides the Mac data type and functions for working with
--   it.
module Net.Mac

-- | Construct a <a>Mac</a> address from a <a>Word64</a>. Only the lower 48
--   bits are used.
mac :: Word64 -> Mac

-- | Create a <a>Mac</a> address from six octets.
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Mac

-- | Convert a <a>Mac</a> address to the six octets that make it up. This
--   function and <a>fromOctets</a> are inverses:
--   
--   <pre>
--   m == (let (a,b,c,d,e,f) = toOctets m in fromOctets a b c d e f)
--   </pre>
toOctets :: Mac -> (Word8, Word8, Word8, Word8, Word8, Word8)

-- | Encode a <a>Mac</a> address using the default <a>MacCodec</a>
--   <a>defCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn (encode (Mac 0xA47F247AB423))
--   a4:7f:24:7a:b4:23
--   </pre>
encode :: Mac -> Text

-- | Encode a <a>Mac</a> address using the given <a>MacCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; m = Mac 0xA47F247AB423
--   
--   &gt;&gt;&gt; T.putStrLn $ encodeWith defCodec m
--   a4:7f:24:7a:b4:23
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; T.putStrLn $ encodeWith (MacCodec (MacGroupingTriples '-') True) m
--   A47-F24-7AB-423
--   </pre>
encodeWith :: MacCodec -> Mac -> Text

-- | Decode a <a>Mac</a> address using the default <a>MacCodec</a>
--   <a>defCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; decode (Text.pack "a4:7f:24:7a:b4:23")
--   Just (mac 0xa47f247ab423)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; decode (Text.pack "a47-f24-7ab-423")
--   Nothing
--   </pre>
decode :: Text -> Maybe Mac

-- | Decode a <a>Mac</a> address from <a>Text</a> using the given
--   <a>MacCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; decodeWith defCodec (Text.pack "a4:7f:24:7a:b4:23")
--   Just (mac 0xa47f247ab423)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; decodeWith (MacCodec MacGroupingNoSeparator False) (Text.pack "a47f247ab423")
--   Just (mac 0xa47f247ab423)
--   </pre>
decodeWith :: MacCodec -> Text -> Maybe Mac

-- | Encode a <a>Mac</a> address as a <a>Builder</a>.
builder :: Mac -> Builder

-- | Parse a <a>Mac</a> address using a <a>Parser</a>.
--   
--   <pre>
--   &gt;&gt;&gt; AT.parseOnly parser (Text.pack "a4:7f:24:7a:b4:23")
--   Right (mac 0xa47f247ab423)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; AT.parseOnly parser (Text.pack "a47-f24-7ab-423")
--   Left "':': Failed reading: satisfy"
--   </pre>
parser :: Parser Mac

-- | Parser a <a>Mac</a> address using the given <a>MacCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; p1 = parserWith defCodec
--   
--   &gt;&gt;&gt; AT.parseOnly p1 (Text.pack "a4:7f:24:7a:b4:23")
--   Right (mac 0xa47f247ab423)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; p2 = parserWith (MacCodec MacGroupingNoSeparator False)
--   
--   &gt;&gt;&gt; AT.parseOnly p2 (Text.pack "a47f247ab423")
--   Right (mac 0xa47f247ab423)
--   </pre>
parserWith :: MacCodec -> Parser Mac

-- | Encode a <a>Mac</a> address using the default <a>MacCodec</a>
--   <a>defCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; BC.putStrLn (encodeUtf8 (mac 0x64255A0F2C47))
--   64:25:5a:0f:2c:47
--   </pre>
encodeUtf8 :: Mac -> ByteString

-- | Encode a <a>Mac</a> address as a <a>ByteString</a> using the given
--   <a>MacCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; m = Mac 0xA47F247AB423
--   
--   &gt;&gt;&gt; BC.putStrLn $ encodeWithUtf8 defCodec m
--   a4:7f:24:7a:b4:23
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; BC.putStrLn $ encodeWithUtf8 (MacCodec (MacGroupingTriples '-') True) m
--   A47-F24-7AB-423
--   </pre>
encodeWithUtf8 :: MacCodec -> Mac -> ByteString

-- | Lenient decoding of MAC address that accepts lowercase, uppercase, and
--   any kind of separator.
--   
--   <pre>
--   &gt;&gt;&gt; decodeUtf8 "A2:DE:AD:BE:EF:67"
--   Just (mac 0xa2deadbeef67)
--   
--   &gt;&gt;&gt; decodeUtf8 "13-a2-fe-a4-17-96"
--   Just (mac 0x13a2fea41796)
--   
--   &gt;&gt;&gt; decodeUtf8 "0A42.47BA.67C2"
--   Just (mac 0x0a4247ba67c2)
--   </pre>
decodeUtf8 :: ByteString -> Maybe Mac

-- | Decode a <a>ByteString</a> as a <a>Mac</a> address using the given
--   <a>MacCodec</a>.
--   
--   <pre>
--   &gt;&gt;&gt; decodeWithUtf8 defCodec (BC.pack "64:25:5a:0f:2c:47")
--   Just (mac 0x64255a0f2c47)
--   </pre>
--   
--   <pre>
--   &gt;&gt;&gt; decodeWithUtf8 (MacCodec MacGroupingNoSeparator False) (BC.pack "64255a0f2c47")
--   Just (mac 0x64255a0f2c47)
--   </pre>
decodeWithUtf8 :: MacCodec -> ByteString -> Maybe Mac

-- | Make a bytestring builder from a <a>Mac</a> address using a colon as
--   the separator.
builderUtf8 :: Mac -> Builder

-- | Lenient parser for a <a>Mac</a> address using any character as the
--   separator and accepting any digit grouping (i.e.
--   <tt>FA:43:B2:C0:0F:99</tt> or <tt>A065.647B.87FA</tt>).
parserUtf8 :: Parser Mac

-- | Parser for a <a>Mac</a> address using the provided settings.
parserWithUtf8 :: MacCodec -> Parser Mac

-- | Decode a <a>Mac</a> address from a <a>ByteString</a>. Each byte is
--   interpreted as an octet of the <a>Mac</a> address. Consequently,
--   <a>ByteString</a>s of length 6 successfully decode, and all other
--   <a>ByteString</a>s fail to decode.
--   
--   <pre>
--   &gt;&gt;&gt; decodeBytes (B.pack [0x6B,0x47,0x18,0x90,0x55,0xC3])
--   Just (mac 0x6b47189055c3)
--   
--   &gt;&gt;&gt; decodeBytes (B.replicate 6 0x3A)
--   Just (mac 0x3a3a3a3a3a3a)
--   
--   &gt;&gt;&gt; decodeBytes (B.replicate 7 0x3A)
--   Nothing
--   </pre>
decodeBytes :: ByteString -> Maybe Mac

-- | Print a <a>Mac</a> address using the textual encoding.
print :: Mac -> IO ()

-- | A 48-bit MAC address. Do not use the data constructor for this type.
--   It is not considered part of the stable API, and it allows you to
--   construct invalid MAC addresses.
newtype Mac
Mac :: Word64 -> Mac

-- | A <a>MacCodec</a> allows users to control the encoding/decoding of
--   their <a>Mac</a> addresses.
data MacCodec
MacCodec :: !MacGrouping -> !Bool -> MacCodec
[macCodecGrouping] :: MacCodec -> !MacGrouping
[macCodecUpperCase] :: MacCodec -> !Bool

-- | The format expected by the mac address parser. The <a>Word8</a> taken
--   by some of these constructors is the ascii value of the character to
--   be used as the separator. This is typically a colon, a hyphen, or a
--   space character. All decoding functions are case insensitive.
data MacGrouping

-- | Two-character groups, <tt>FA:2B:40:09:8C:11</tt>
MacGroupingPairs :: !Char -> MacGrouping

-- | Three-character groups, <tt>24B-F0A-025-829</tt>
MacGroupingTriples :: !Char -> MacGrouping

-- | Four-character groups, <tt>A220.0745.CAC7</tt>
MacGroupingQuadruples :: !Char -> MacGrouping

-- | No separator, <tt>24AF4B5B0780</tt>
MacGroupingNoSeparator :: MacGrouping
instance GHC.Generics.Generic Net.Mac.MacCodec
instance GHC.Read.Read Net.Mac.MacCodec
instance GHC.Show.Show Net.Mac.MacCodec
instance GHC.Classes.Ord Net.Mac.MacCodec
instance GHC.Classes.Eq Net.Mac.MacCodec
instance GHC.Generics.Generic Net.Mac.MacGrouping
instance GHC.Read.Read Net.Mac.MacGrouping
instance GHC.Show.Show Net.Mac.MacGrouping
instance GHC.Classes.Ord Net.Mac.MacGrouping
instance GHC.Classes.Eq Net.Mac.MacGrouping
instance GHC.Generics.Generic Net.Mac.Mac
instance GHC.Classes.Ord Net.Mac.Mac
instance GHC.Classes.Eq Net.Mac.Mac
instance Control.DeepSeq.NFData Net.Mac.Mac
instance Data.Primitive.Types.Prim Net.Mac.Mac
instance GHC.Show.Show Net.Mac.Mac
instance GHC.Read.Read Net.Mac.Mac
instance GHC.Enum.Bounded Net.Mac.Mac
instance GHC.Enum.Enum Net.Mac.Mac
instance Data.Hashable.Class.Hashable Net.Mac.Mac
instance Data.Aeson.Types.ToJSON.ToJSON Net.Mac.Mac
instance Data.Aeson.Types.ToJSON.ToJSONKey Net.Mac.Mac
instance Data.Aeson.Types.FromJSON.FromJSONKey Net.Mac.Mac
instance Data.Aeson.Types.FromJSON.FromJSON Net.Mac.Mac


-- | This module re-exports all of the thematic types that this library
--   defines.
module Net.Types

-- | A 32-bit Internet Protocol version 4 address. To use this with the
--   <tt>network</tt> library, it is necessary to use
--   <tt>Network.Socket.htonl</tt> to convert the underlying <a>Word32</a>
--   from host byte order to network byte order.
newtype IPv4
IPv4 :: Word32 -> IPv4
[getIPv4] :: IPv4 -> Word32

-- | A 128-bit Internet Protocol version 6 address.
data IPv6
IPv6 :: {-# UNPACK #-} !Word64 -> {-# UNPACK #-} !Word64 -> IPv6
[ipv6A] :: IPv6 -> {-# UNPACK #-} !Word64
[ipv6B] :: IPv6 -> {-# UNPACK #-} !Word64

-- | A 32-bit <a>IPv4</a> address or a 128-bit <a>IPv6</a> address.
--   Internally, this is just represented as an <a>IPv6</a> address. The
--   functions provided in <tt>Net.IP</tt> help simulate constructing and
--   pattern matching on values of this type. All functions and typeclass
--   methods that convert <a>IP</a> values to text will display it as an
--   <a>IPv4</a> address if possible.
newtype IP
IP :: IPv6 -> IP
[getIP] :: IP -> IPv6

-- | The length should be between 0 and 32. These bounds are inclusive.
--   This expectation is not in any way enforced by this library because it
--   does not cause errors. A mask length greater than 32 will be treated
--   as if it were 32.
data IPv4Range
IPv4Range :: {-# UNPACK #-} !IPv4 -> {-# UNPACK #-} !Word8 -> IPv4Range
[ipv4RangeBase] :: IPv4Range -> {-# UNPACK #-} !IPv4
[ipv4RangeLength] :: IPv4Range -> {-# UNPACK #-} !Word8

-- | An <a>IPv6Range</a>. It is made up of the first <a>IPv6</a> in the
--   range and its length.
data IPv6Range
IPv6Range :: {-# UNPACK #-} !IPv6 -> {-# UNPACK #-} !Word8 -> IPv6Range
[ipv6RangeBase] :: IPv6Range -> {-# UNPACK #-} !IPv6
[ipv6RangeLength] :: IPv6Range -> {-# UNPACK #-} !Word8

-- | A 48-bit MAC address. Do not use the data constructor for this type.
--   It is not considered part of the stable API, and it allows you to
--   construct invalid MAC addresses.
newtype Mac
Mac :: Word64 -> Mac

-- | A <a>MacCodec</a> allows users to control the encoding/decoding of
--   their <a>Mac</a> addresses.
data MacCodec
MacCodec :: !MacGrouping -> !Bool -> MacCodec
[macCodecGrouping] :: MacCodec -> !MacGrouping
[macCodecUpperCase] :: MacCodec -> !Bool

-- | The format expected by the mac address parser. The <a>Word8</a> taken
--   by some of these constructors is the ascii value of the character to
--   be used as the separator. This is typically a colon, a hyphen, or a
--   space character. All decoding functions are case insensitive.
data MacGrouping

-- | Two-character groups, <tt>FA:2B:40:09:8C:11</tt>
MacGroupingPairs :: !Char -> MacGrouping

-- | Three-character groups, <tt>24B-F0A-025-829</tt>
MacGroupingTriples :: !Char -> MacGrouping

-- | Four-character groups, <tt>A220.0745.CAC7</tt>
MacGroupingQuadruples :: !Char -> MacGrouping

-- | No separator, <tt>24AF4B5B0780</tt>
MacGroupingNoSeparator :: MacGrouping
