Safe Haskell | None |
---|---|
Language | Haskell2010 |
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 IPv6
address, with IPv4
addresses being represented as an IPv4-mapped IPv6 addresses:
+---------+---------+--------------+ | 80 bits | 16 bits | 32 bits | +---------+---------+--------------+ | 00...00 | FFFF | IPv4 address | +---------+---------+--------------+
All functions and instance methods that deal with textual conversion
will encode an IP
using either dot-decimal notation (for IPv4) or
RFC 5952 (for IPv6). They will decode an IP
from either format
as well. The Show
instance presents an address in as valid haskell code
that resembles the formatted address:
>>>
decode "192.168.3.100"
Just (ipv4 192 168 3 100)>>>
decode "A3F5:12:F26::1466:8B91"
Just (ipv6 0xa3f5 0x0012 0x0f26 0x0000 0x0000 0x0000 0x1466 0x8b91)
Synopsis
- case_ :: (IPv4 -> a) -> (IPv6 -> a) -> IP -> a
- isIPv4 :: IP -> Bool
- isIPv6 :: IP -> Bool
- ipv4 :: Word8 -> Word8 -> Word8 -> Word8 -> IP
- ipv6 :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IP
- fromIPv4 :: IPv4 -> IP
- fromIPv6 :: IPv6 -> IP
- encode :: IP -> Text
- encodeShort :: IP -> ShortText
- decode :: Text -> Maybe IP
- decodeShort :: ShortText -> Maybe IP
- boundedBuilderUtf8 :: IP -> Builder 39
- parserUtf8Bytes :: e -> Parser e s IP
- print :: IP -> IO ()
- newtype IP = IP {}
Pattern Matching
Is the IP
an IPv4 address?
>>>
isIPv4 (ipv4 10 0 0 25)
True
>>>
isIPv4 (ipv6 0x3124 0x0 0x0 0xDEAD 0xCAFE 0xFF 0xFE00 0x1)
False
Is the IP
an IPv6 address?
>>>
isIPv6 (ipv4 10 0 0 25)
False
>>>
isIPv6 (ipv6 0x3124 0x0 0x0 0xDEAD 0xCAFE 0xFF 0xFE00 0x1)
True
Construction
ipv4 :: Word8 -> Word8 -> Word8 -> Word8 -> IP Source #
Construct an IP
address from the four octets of
an IPv4 address.
ipv6 :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IP Source #
Construct an IP
address from the eight 16-bit
chunks of an IPv6 address.
Textual Conversion
Text
encodeShort :: IP -> ShortText Source #
boundedBuilderUtf8 :: IP -> Builder 39 Source #
Encode an IP
as a bounded bytearray builder.
>>>
BB.run Nat.constant (boundedBuilderUtf8 (ipv4 192 168 2 14))
[0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x32, 0x2e, 0x31, 0x34]
Bytes
Printing
Print an IP
using the textual encoding. This exists mostly for
debugging purposes.
>>>
print (ipv4 10 0 0 25)
10.0.0.25
>>>
print (ipv6 0x3124 0x0 0x0 0xDEAD 0xCAFE 0xFF 0xFE00 0x1)
3124::dead:cafe:ff:fe00:1
Types
A 32-bit IPv4
address or a 128-bit IPv6
address. Internally, this
is just represented as an IPv6
address. The functions provided
in Net.IP
help simulate constructing and pattern matching on values
of this type. All functions and typeclass methods that convert
IP
values to text will display it as an IPv4
address if possible.
Instances
Eq IP Source # | |
Data IP Source # | |
Defined in Net.IP gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> IP -> c IP # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c IP # dataTypeOf :: IP -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c IP) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IP) # gmapT :: (forall b. Data b => b -> b) -> IP -> IP # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> IP -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> IP -> r # gmapQ :: (forall d. Data d => d -> u) -> IP -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> IP -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> IP -> m IP # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> IP -> m IP # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> IP -> m IP # | |
Ord IP Source # | |
Read IP Source # | |
Show IP Source # | |
Ix IP Source # | |
Generic IP Source # | |
ToJSON IP Source # | |
FromJSON IP Source # | |
NFData IP Source # | |
type Rep IP Source # | |