Safe Haskell | None |
---|---|
Language | Haskell2010 |
Net.IPv4
Contents
Description
An IPv4 data type
This module provides the IPv4 data type and functions for working
with it. There are also encoding and decoding functions provided
in this module, but they should be imported from
Net.IPv4.Text
and Net.IPv4.ByteString.Char8
instead. They are
defined here so that the FromJSON
and ToJSON
instances can
use them.
At some point, a highly efficient IPv4-to-ByteString function needs
to be added to this module to take advantage of aeson
's new
toEncoding
method.
- ipv4 :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
- fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
- fromTupleOctets :: (Word8, Word8, Word8, Word8) -> IPv4
- toOctets :: IPv4 -> (Word8, Word8, Word8, Word8)
- any :: IPv4
- loopback :: IPv4
- broadcast :: IPv4
- private :: IPv4 -> Bool
- reserved :: IPv4 -> Bool
- public :: IPv4 -> Bool
- encode :: IPv4 -> Text
- decode :: Text -> Maybe IPv4
- builder :: IPv4 -> Builder
- reader :: Reader IPv4
- parser :: Parser IPv4
- encodeUtf8 :: IPv4 -> ByteString
- decodeUtf8 :: ByteString -> Maybe IPv4
- builderUtf8 :: IPv4 -> Builder
- parserUtf8 :: Parser IPv4
- encodeString :: IPv4 -> String
- decodeString :: String -> Maybe IPv4
- print :: IPv4 -> IO ()
- newtype IPv4 = IPv4 {}
Conversion Functions
ipv4 :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4 #
Create an IPv4
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 Show
and Read
instances
of IPv4
to help keep things readable in GHCi.
>>>
let addr = ipv4 192 168 1 1
>>>
addr
ipv4 192 168 1 1>>>
getIPv4 addr
3232235777
fromTupleOctets :: (Word8, Word8, Word8, Word8) -> IPv4 #
An uncurried variant of fromOctets
.
toOctets :: IPv4 -> (Word8, Word8, Word8, Word8) #
Convert an IPv4
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.
Special IP Addresses
Range Predicates
Checks to see if the IPv4
address belongs to a private
network. The three private networks that are checked are
10.0.0.0/8
, 172.16.0.0/12
, and 192.168.0.0/16
.
Checks to see if the IPv4
address is publicly routable.
public x == not (reserved x)
Textual Conversion
Text
UTF-8 ByteString
encodeUtf8 :: IPv4 -> ByteString #
Encode an IPv4
address to a UTF-8 encoded ByteString
.
decodeUtf8 :: ByteString -> Maybe IPv4 #
builderUtf8 :: IPv4 -> Builder #
parserUtf8 :: Parser IPv4 #
String
These functions exist for the convenience of those who need a
String
representation of an IPv4
address. Using them
is discouraged unless the end user is working with a library
that can only use String
to deal with textual data (such as
pandoc
, hxr
, or network
).
encodeString :: IPv4 -> String #
decodeString :: String -> Maybe IPv4 #
Printing
Types
A 32-bit Internet Protocol version 4 address. To use this with the
network
library, it is necessary to use Network.Socket.htonl
to
convert the underlying Word32
from host byte order to network byte
order.
Instances
Bounded IPv4 # | |
Enum IPv4 # | |
Eq IPv4 # | |
Ord IPv4 # | |
Read IPv4 # | |
Show IPv4 # | |
Generic IPv4 # | |
Hashable IPv4 # | |
ToJSON IPv4 # | |
ToJSONKey IPv4 # | |
FromJSON IPv4 # | |
FromJSONKey IPv4 # | |
Storable IPv4 # | |
Bits IPv4 # | Note: we use network order (big endian) as opposed to host order (little endian) which differs from the underlying IPv4 type representation. |
FiniteBits IPv4 # | |
Prim IPv4 # | |
Unbox IPv4 # | |
Vector Vector IPv4 # | |
MVector MVector IPv4 # | |
type Rep IPv4 # | |
data Vector IPv4 # | |
data MVector s IPv4 # | |
Interoperability
The network
library is commonly
used to open sockets and communicate over them. In the Network.Socket
module,
it provides a type synonym HostAddress
that, like IPv4
, is used
to represent an IPv4 address. However, while IPv4
uses a big-endian representation
for ip addresses, HostAddress
has platform dependent endianness.
Consequently, it is necessary to convert between the two as follows:
import Network.Socket (HostAddress,htonl,ntohl) toHostAddr :: IPv4 -> HostAddress toHostAddr (IPv4 w) = htonl w fromHostAddr :: HostAddress -> IPv4 fromHostAddr w = IPv4 (ntohl w)
These functions are not included with this library since it would require
picking up a dependency on network
.