ip-1.7.8: Library for IP and MAC addresses

Safe Haskell | None
Language | Haskell2010

Net.IPv4

Contents

- Conversion Functions
- Special IP Addresses
- Range Predicates
- Textual Conversion
  - Text
  - UTF-8 ByteString
  - UTF-8 Bytes
  - Non-textual Bytes
  - String
  - Printing
- IPv4 Ranges
  - Range functions
  - Conversion to IPv4
  - Private Ranges
  - Textual Conversion
    - Text
  - UTF-8 Bytes
- Types
- Unboxing
- Interoperability

Description

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

Synopsis

- 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
- localhost :: 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
- decodeShort :: ShortText -> Maybe IPv4
- encodeShort :: IPv4 -> ShortText
- encodeUtf8 :: IPv4 -> ByteString
- decodeUtf8 :: ByteString -> Maybe IPv4
- builderUtf8 :: IPv4 -> Builder
- parserUtf8 :: Parser IPv4
- decodeUtf8Bytes :: Bytes -> Maybe IPv4
- parserUtf8Bytes :: e -> Parser e s IPv4
- byteArrayBuilderUtf8 :: IPv4 -> Builder
- boundedBuilderUtf8 :: IPv4 -> Builder 15
- boundedBuilderOctetsBE :: IPv4 -> Builder 4
- boundedBuilderOctetsLE :: IPv4 -> Builder 4
- encodeString :: IPv4 -> String
- decodeString :: String -> Maybe IPv4
- print :: IPv4 -> IO ()
- range :: IPv4 -> Word8 -> IPv4Range
- fromBounds :: IPv4 -> IPv4 -> IPv4Range
- normalize :: IPv4Range -> IPv4Range
- contains :: IPv4Range -> IPv4 -> Bool
- isSubsetOf :: IPv4Range -> IPv4Range -> Bool
- member :: IPv4 -> IPv4Range -> Bool
- lowerInclusive :: IPv4Range -> IPv4
- upperInclusive :: IPv4Range -> IPv4
- toList :: IPv4Range -> [IPv4]
- toGenerator :: MonadPlus m => IPv4Range -> m IPv4
- private24 :: IPv4Range
- private20 :: IPv4Range
- private16 :: IPv4Range
- encodeRange :: IPv4Range -> Text
- decodeRange :: Text -> Maybe IPv4Range
- builderRange :: IPv4Range -> Builder
- parserRange :: Parser IPv4Range
- printRange :: IPv4Range -> IO ()
- parserRangeUtf8Bytes :: e -> Parser e s IPv4Range
- parserRangeUtf8BytesLenient :: e -> Parser e s IPv4Range
- newtype IPv4 = IPv4 {
  - getIPv4 :: Word32
  }
- type IPv4# = Word#
- data IPv4Range = IPv4Range {
  - ipv4RangeBase :: !IPv4
  - ipv4RangeLength :: !Word8
  }
- box :: IPv4# -> IPv4
- unbox :: IPv4 -> IPv4#
- parserUtf8Bytes# :: e -> Parser e s 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.ipv4 192 168 1 1
    >>> addr
    ipv4 192 168 1 1
    >>> getIPv4 addr
    3232235777

fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4 #

An alias for the `ipv4` smart constructor.

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

any :: IPv4 #

The IP address representing any host.

    >>> IPv4.any
    ipv4 0 0 0 0

loopback :: IPv4 #

The local loopback IP address.

    >>> IPv4.loopback
    ipv4 127 0 0 1

localhost :: IPv4 #

A useful and common alias for `loopback`.

    >>> IPv4.localhost
    ipv4 127 0 0 1

broadcast :: IPv4 #

The broadcast IP address.

    >>> IPv4.broadcast
    ipv4 255 255 255 255

# Range Predicates

private :: IPv4 -> Bool #

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`.

reserved :: IPv4 -> Bool #

Checks to see if the `IPv4` address belongs to a reserved network. This includes the three private networks that `private` checks along with several other ranges that are not used on the public Internet. The implementation of this function is optimized.

public :: IPv4 -> Bool #

Checks to see if the `IPv4` address is publicly routable.

    IPv4.public x == not (IPv4.reserved x)

# Textual Conversion

## Text

encode :: IPv4 -> Text #

Encode an `IPv4` address to `Text` using dot-decimal notation:

    >>> T.putStrLn (IPv4.encode (IPv4.ipv4 192 168 2 47))
    192.168.2.47

decode :: Text -> Maybe IPv4 #

Decode an `IPv4` address.

    >>> IPv4.decode "192.168.2.47"
    Just (ipv4 192 168 2 47)

    >>> IPv4.decode "10.100.256.256"
    Nothing

builder :: IPv4 -> Builder #

Encode an `IPv4` address to a text `Builder`.

    >>> IPv4.builder (IPv4.ipv4 192 168 2 47)
    "192.168.2.47"

reader :: Reader IPv4 #

Parse an `IPv4` address using a `Reader`.

    >>> IPv4.reader "192.168.2.47"
    Right (ipv4 192 168 2 47,"")

    >>> IPv4.reader "192.168.2.470"
    Left "All octets in an IPv4 address must be between 0 and 255"

parser :: Parser IPv4 #

Parse an `IPv4` address using a `Parser`.

    >>> AT.parseOnly IPv4.parser "192.168.2.47"
    Right (ipv4 192 168 2 47)

    >>> AT.parseOnly IPv4.parser "192.168.2.470"
    Left "Failed reading: All octets in an IPv4 address must be between 0 and 255"

decodeShort :: ShortText -> Maybe IPv4 #

Decode `ShortText` as an `IPv4` address.

    >>> IPv4.decodeShort "192.168.3.48"
    Just (ipv4 192 168 3 48)

encodeShort :: IPv4 -> ShortText #

Encode an `IPv4` address as `ShortText`.

    >>> IPv4.encodeShort (IPv4.ipv4 192 168 5 99)
    "192.168.5.99"

## UTF-8 ByteString

encodeUtf8 :: IPv4 -> ByteString #

Encode an `IPv4` address to a UTF-8 encoded `ByteString`.

    >>> IPv4.encodeUtf8 (IPv4.ipv4 192 168 2 47)
    "192.168.2.47"

decodeUtf8 :: ByteString -> Maybe IPv4 #

Decode a UTF8-encoded `ByteString` into an `IPv4`.

    >>> IPv4.decodeUtf8 "192.168.2.47"
    Just (ipv4 192 168 2 47)

Currently not terribly efficient since the implementation re-encodes the argument as UTF-16 text before decoding that IPv4 address from that. PRs to fix this are welcome.

builderUtf8 :: IPv4 -> Builder #

Encode an `IPv4` as a bytestring `Builder`

    >>> Builder.toLazyByteString (IPv4.builderUtf8 (IPv4.fromOctets 192 168 2 12))
    "192.168.2.12"

parserUtf8 :: Parser IPv4 #

Parse an `IPv4` using a `Parser`.

    >>> AB.parseOnly IPv4.parserUtf8 "192.168.2.47"
    Right (ipv4 192 168 2 47)

    >>> AB.parseOnly IPv4.parserUtf8 "192.168.2.470"
    Left "Failed reading: All octets in an ipv4 address must be between 0 and 255"

## UTF-8 Bytes

decodeUtf8Bytes :: Bytes -> Maybe IPv4 #

Decode UTF-8-encoded `Bytes` into an `IPv4` address.

    >>> IPv4.decodeUtf8Bytes (Ascii.fromString "127.0.0.1")
    Just (ipv4 127 0 0 1)

parserUtf8Bytes :: e -> Parser e s IPv4 #

Parse UTF-8-encoded `Bytes` as an `IPv4` address.

    >>> Parser.parseBytes (IPv4.parserUtf8Bytes ()) (Ascii.fromString "10.0.1.254")
    Success (Slice {offset = 10, length = 0, value = ipv4 10 0 1 254})

byteArrayBuilderUtf8 :: IPv4 -> Builder #

Encode an `IPv4` address as a unbounded byte array builder.

    >>> Chunks.concat (UB.run 1 (IPv4.byteArrayBuilderUtf8 (IPv4.fromOctets 192 168 2 13)))
    [0x31,0x39,0x32,0x2e,0x31,0x36,0x38,0x2e,0x32,0x2e,0x31,0x33]

Note that period is encoded by UTF-8 as `0x2e`.

boundedBuilderUtf8 :: IPv4 -> Builder 15 #

Encode an `IPv4` address as a bounded byte array builder.

    >>> BB.run Nat.constant (IPv4.boundedBuilderUtf8 (IPv4.fromOctets 192 168 2 14))
    [0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x32, 0x2e, 0x31, 0x34]

Note that period is encoded by UTF-8 as `0x2e`.

## Non-textual Bytes

boundedBuilderOctetsBE :: IPv4 -> Builder 4 #

Encode `IPv4` address to a sequence a 4 bytes with the first byte representing corresponding to the most significant byte in the address.

    >>> BB.run Nat.constant (IPv4.boundedBuilderOctetsBE (IPv4.fromOctets 0xc0 0xa8 0x02 0x1f))
    [0xc0, 0xa8, 0x02, 0x1f]

boundedBuilderOctetsLE :: IPv4 -> Builder 4 #

Encode `IPv4` address to a sequence a 4 bytes with the first byte representing corresponding to the least significant byte in the address.

    >>> BB.run Nat.constant (IPv4.boundedBuilderOctetsLE (IPv4.fromOctets 0xc0 0xa8 0x02 0x1f))
    [0x1f, 0x02, 0xa8, 0xc0]

## 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 #

Encode an `IPv4` as a `String`.

decodeString :: String -> Maybe IPv4 #

Decode an `IPv4` from a `String`.

## Printing

print :: IPv4 -> IO () #

Print an `IPv4` using the textual encoding.

# IPv4 Ranges

## Range functions

range :: IPv4 -> Word8 -> IPv4Range #

Smart constructor for `IPv4Range`. Ensures the mask is appropriately sized and sets masked bits in the `IPv4` to zero.

fromBounds :: IPv4 -> IPv4 -> IPv4Range #

Given an inclusive lower and upper ip address, create the smallest `IPv4Range` that contains the two. This is helpful in situations where input given as a range like `192.168.16.0-192.168.19.255` needs to be handled. This makes the range broader if it cannot be represented in CIDR notation.

    >>> IPv4.printRange $ IPv4.fromBounds (IPv4.fromOctets 192 168 16 0) (IPv4.fromOctets 192 168 19 255)
    192.168.16.0/22
    >>> IPv4.printRange $ IPv4.fromBounds (IPv4.fromOctets 10 0 5 7) (IPv4.fromOctets 10 0 5 14)
    10.0.5.0/28

normalize :: IPv4Range -> IPv4Range #

Normalize an `IPv4Range`. The first result of this is that the `IPv4` inside the `IPv4Range` is changed so that the insignificant bits are zeroed out. For example:

    >>> IPv4.printRange $ IPv4.normalize $ IPv4.IPv4Range (IPv4.fromOctets 192 168 1 19) 24
    192.168.1.0/24
    >>> IPv4.printRange $ IPv4.normalize $ IPv4.IPv4Range (IPv4.fromOctets 192 168 1 163) 28
    192.168.1.160/28

The second effect of this is that the mask length is lowered to be 32 or smaller. Working with `IPv4Range`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 `normalize` is idempotent, that is:

    IPv4.normalize r == (IPv4.normalize . IPv4.normalize) r

contains :: IPv4Range -> IPv4 -> Bool #

Checks to see if an `IPv4` address belongs in the `IPv4Range`.

    >>> let ip = IPv4.fromOctets 10 10 1 92
    >>> IPv4.contains (IPv4.IPv4Range (IPv4.fromOctets 10 0 0 0) 8) ip
    True
    >>> IPv4.contains (IPv4.IPv4Range (IPv4.fromOctets 10 11 0 0) 16) ip
    False

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:

    >>> let r = IPv4.IPv4Range (IPv4.fromOctets 10 10 10 6) 31
    >>> mapM_ (P.print . IPv4.contains r) (take 5 $ iterate succ $ IPv4.fromOctets 10 10 10 5)
    False
    True
    True
    False
    False

The implementation of `contains` 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.

isSubsetOf :: IPv4Range -> IPv4Range -> Bool #

Checks if the first range is a subset of the second range.

    >>> IPv4.isSubsetOf (IPv4.IPv4Range (IPv4.fromOctets 192 0 2 128) 25) (IPv4.IPv4Range (IPv4.fromOctets 192 0 2 0) 24)
    True
    >>> IPv4.isSubsetOf (IPv4.IPv4Range (IPv4.fromOctets 192 0 2 0) 30) (IPv4.IPv4Range (IPv4.fromOctets 192 0 2 4) 30)
    False

member :: IPv4 -> IPv4Range -> Bool #

This is provided to mirror the interface provided by `Data.Set`. It behaves just like `contains` but with flipped arguments.

    IPv4.member ip r == IPv4.contains r ip

lowerInclusive :: IPv4Range -> IPv4 #

The inclusive lower bound of an `IPv4Range`. This is conventionally understood to be the broadcast address of a subnet. For example:

    >>> T.putStrLn $ IPv4.encode $ IPv4.lowerInclusive $ IPv4.IPv4Range (IPv4.ipv4 10 10 1 160) 25
    10.10.1.128

Note that the lower bound of a normalized `IPv4Range` is simply the ip address of the range:

    IPv4.lowerInclusive r == IPv4.ipv4RangeBase (IPv4.normalize r)

upperInclusive :: IPv4Range -> IPv4 #

The inclusive upper bound of an `IPv4Range`.

    >>> T.putStrLn $ IPv4.encode $ IPv4.upperInclusive $ IPv4.IPv4Range (IPv4.ipv4 10 10 1 160) 25
    10.10.1.255

## Conversion to IPv4

toList :: IPv4Range -> [IPv4] #

Convert an `IPv4Range` into a list of the `IPv4` addresses that are in it.

    >>> let r = IPv4.IPv4Range (IPv4.fromOctets 192 168 1 8) 30
    >>> mapM_ (T.putStrLn . IPv4.encode) (IPv4.toList r)
    192.168.1.8
    192.168.1.9
    192.168.1.10
    192.168.1.11

toGenerator :: MonadPlus m => IPv4Range -> m IPv4 #

A stream-polymorphic generator over an `IPv4Range`. For more information, see How to build library-agnostic streaming sources.

## Private Ranges

private24 :: IPv4Range #

The RFC1918 24-bit block. Subnet mask: `10.0.0.0/8`

private20 :: IPv4Range #

The RFC1918 20-bit block. Subnet mask: `172.16.0.0/12`

private16 :: IPv4Range #

The RFC1918 16-bit block. Subnet mask: `192.168.0.0/16`

## Textual Conversion

### Text

encodeRange :: IPv4Range -> Text #

Encode an `IPv4Range` as `Text`.

    >>> IPv4.encodeRange (IPv4.IPv4Range (IPv4.ipv4 172 16 0 0) 12)
    "172.16.0.0/12"

decodeRange :: Text -> Maybe IPv4Range #

Decode an `IPv4Range` from `Text`.

    >>> IPv4.decodeRange "172.16.0.0/12"
    Just (IPv4Range {ipv4RangeBase = ipv4 172 16 0 0, ipv4RangeLength = 12})
    >>> IPv4.decodeRange "192.168.25.254/16"
    Just (IPv4Range {ipv4RangeBase = ipv4 192 168 0 0, ipv4RangeLength = 16})

builderRange :: IPv4Range -> Builder #

Encode an `IPv4Range` to a `Builder`.

    >>> IPv4.builderRange (IPv4.IPv4Range (IPv4.ipv4 172 16 0 0) 12)
    "172.16.0.0/12"

parserRange :: Parser IPv4Range #

Parse an `IPv4Range` using a `Parser`.

    >>> AT.parseOnly IPv4.parserRange "192.168.25.254/16"
    Right (IPv4Range {ipv4RangeBase = ipv4 192 168 0 0, ipv4RangeLength = 16})

printRange :: IPv4Range -> IO () #

Print an `IPv4Range`. Helper function that exists mostly for testing purposes.

## UTF-8 Bytes

parserRangeUtf8Bytes :: e -> Parser e s IPv4Range #

Parse UTF-8-encoded `Bytes` into an `IPv4Range`. This requires the mask to be present.

    >>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8Bytes ()) (Ascii.fromString "192.168.0.0/16")
    192.168.0.0/16
    >>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8Bytes ()) (Ascii.fromString "10.10.10.1")
    nope

See `parserRangeUtf8BytesLenient` for a variant that treats a missing mask as a `/32` mask.

parserRangeUtf8BytesLenient :: e -> Parser e s IPv4Range #

Variant of `parserRangeUtf8Bytes` that allows the mask to be omitted. An omitted mask is treated as a `/32` mask.

    >>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8BytesLenient ()) (Ascii.fromString "192.168.0.0/16")
    192.168.0.0/16
    >>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8BytesLenient ()) (Ascii.fromString "10.10.10.1")
    10.10.10.1/32

# Types

newtype IPv4 #

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.

Constructors

IPv4 |
Fields getIPv4 :: Word32

#### Instances

Instances details

FromJSON IPv4 # |
Instance details Defined in Net.IPv4 Methods parseJSON :: Value -> Parser IPv4 # parseJSONList :: Value -> Parser [IPv4] # omittedField :: Maybe IPv4 #
FromJSONKey IPv4 # |
Instance details Defined in Net.IPv4 Methods fromJSONKey :: FromJSONKeyFunction IPv4 # fromJSONKeyList :: FromJSONKeyFunction [IPv4] #
ToJSON IPv4 # |
Instance details Defined in Net.IPv4 Methods toJSON :: IPv4 -> Value # toEncoding :: IPv4 -> Encoding # toJSONList :: [IPv4] -> Value # toEncodingList :: [IPv4] -> Encoding # omitField :: IPv4 -> Bool #
ToJSONKey IPv4 # |
Instance details Defined in Net.IPv4 Methods toJSONKey :: ToJSONKeyFunction IPv4 # toJSONKeyList :: ToJSONKeyFunction [IPv4] #
NFData IPv4 # |
Instance details Defined in Net.IPv4 Methods rnf :: IPv4 -> () #
Bits IPv4 # |
Instance details Defined in Net.IPv4 Methods (.&.) :: IPv4 -> IPv4 -> IPv4 # (.|.) :: IPv4 -> IPv4 -> IPv4 # xor :: IPv4 -> IPv4 -> IPv4 # complement :: IPv4 -> IPv4 # shift :: IPv4 -> Int -> IPv4 # rotate :: IPv4 -> Int -> IPv4 # zeroBits :: IPv4 # bit :: Int -> IPv4 # setBit :: IPv4 -> Int -> IPv4 # clearBit :: IPv4 -> Int -> IPv4 # complementBit :: IPv4 -> Int -> IPv4 # testBit :: IPv4 -> Int -> Bool # bitSizeMaybe :: IPv4 -> Maybe Int # bitSize :: IPv4 -> Int # isSigned :: IPv4 -> Bool # shiftL :: IPv4 -> Int -> IPv4 # unsafeShiftL :: IPv4 -> Int -> IPv4 # shiftR :: IPv4 -> Int -> IPv4 # unsafeShiftR :: IPv4 -> Int -> IPv4 # rotateL :: IPv4 -> Int -> IPv4 # rotateR :: IPv4 -> Int -> IPv4 # popCount :: IPv4 -> Int #
FiniteBits IPv4 # |
Instance details Defined in Net.IPv4 Methods finiteBitSize :: IPv4 -> Int # countLeadingZeros :: IPv4 -> Int # countTrailingZeros :: IPv4 -> Int #
Data IPv4 # |
Instance details Defined in Net.IPv4 Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> IPv4 -> c IPv4 # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c IPv4 # toConstr :: IPv4 -> Constr # dataTypeOf :: IPv4 -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c IPv4) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPv4) # gmapT :: (forall b. Data b => b -> b) -> IPv4 -> IPv4 # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> IPv4 -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> IPv4 -> r # gmapQ :: (forall d. Data d => d -> u) -> IPv4 -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> IPv4 -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> IPv4 -> m IPv4 # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> IPv4 -> m IPv4 # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> IPv4 -> m IPv4 #
Bounded IPv4 # |
Instance details Defined in Net.IPv4 Methods minBound :: IPv4 # maxBound :: IPv4 #
Enum IPv4 # |
Instance details Defined in Net.IPv4 Methods succ :: IPv4 -> IPv4 # pred :: IPv4 -> IPv4 # toEnum :: Int -> IPv4 # fromEnum :: IPv4 -> Int # enumFrom :: IPv4 -> [IPv4] # enumFromThen :: IPv4 -> IPv4 -> [IPv4] # enumFromTo :: IPv4 -> IPv4 -> [IPv4] # enumFromThenTo :: IPv4 -> IPv4 -> IPv4 -> [IPv4] #
Storable IPv4 # |
Instance details Defined in Net.IPv4 Methods sizeOf :: IPv4 -> Int # alignment :: IPv4 -> Int # peekElemOff :: Ptr IPv4 -> Int -> IO IPv4 # pokeElemOff :: Ptr IPv4 -> Int -> IPv4 -> IO () # peekByteOff :: Ptr b -> Int -> IO IPv4 # pokeByteOff :: Ptr b -> Int -> IPv4 -> IO () # peek :: Ptr IPv4 -> IO IPv4 # poke :: Ptr IPv4 -> IPv4 -> IO () #
Generic IPv4 # |
Instance details Defined in Net.IPv4 Associated Types
type Rep IPv4 |
Instance details Defined in Net.IPv4 type Rep IPv4 = D1 ('MetaData "IPv4" "Net.IPv4" "ip-1.7.8-AipOdb0Sv1p9R8aZCz7tFh" 'True) (C1 ('MetaCons "IPv4" 'PrefixI 'True) (S1 ('MetaSel ('Just "getIPv4") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word32)))
Methods from :: IPv4 -> Rep IPv4 x # to :: Rep IPv4 x -> IPv4 #
Ix IPv4 # |
Instance details Defined in Net.IPv4 Methods range :: (IPv4, IPv4) -> [IPv4] # index :: (IPv4, IPv4) -> IPv4 -> Int # unsafeIndex :: (IPv4, IPv4) -> IPv4 -> Int # inRange :: (IPv4, IPv4) -> IPv4 -> Bool # rangeSize :: (IPv4, IPv4) -> Int # unsafeRangeSize :: (IPv4, IPv4) -> Int #
Read IPv4 # |
Instance details Defined in Net.IPv4 Methods readsPrec :: Int -> ReadS IPv4 # readList :: ReadS [IPv4] # readPrec :: ReadPrec IPv4 # readListPrec :: ReadPrec [IPv4] #
Show IPv4 # |
Instance details Defined in Net.IPv4 Methods showsPrec :: Int -> IPv4 -> ShowS # show :: IPv4 -> String # showList :: [IPv4] -> ShowS #
Eq IPv4 # |
Instance details Defined in Net.IPv4 Methods (==) :: IPv4 -> IPv4 -> Bool # (/=) :: IPv4 -> IPv4 -> Bool #
Ord IPv4 # |
Instance details Defined in Net.IPv4 Methods compare :: IPv4 -> IPv4 -> Ordering # (<) :: IPv4 -> IPv4 -> Bool # (<=) :: IPv4 -> IPv4 -> Bool # (>) :: IPv4 -> IPv4 -> Bool # (>=) :: IPv4 -> IPv4 -> Bool # max :: IPv4 -> IPv4 -> IPv4 # min :: IPv4 -> IPv4 -> IPv4 #
Hashable IPv4 # |
Instance details Defined in Net.IPv4 Methods hashWithSalt :: Int -> IPv4 -> Int # hash :: IPv4 -> Int #
Prim IPv4 # |
Instance details Defined in Net.IPv4 Methods sizeOfType# :: Proxy IPv4 -> Int# # sizeOf# :: IPv4 -> Int# # alignmentOfType# :: Proxy IPv4 -> Int# # alignment# :: IPv4 -> Int# # indexByteArray# :: ByteArray# -> Int# -> IPv4 # readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, IPv4 #) # writeByteArray# :: MutableByteArray# s -> Int# -> IPv4 -> State# s -> State# s # setByteArray# :: MutableByteArray# s -> Int# -> Int# -> IPv4 -> State# s -> State# s # indexOffAddr# :: Addr# -> Int# -> IPv4 # readOffAddr# :: Addr# -> Int# -> State# s -> (# State# s, IPv4 #) # writeOffAddr# :: Addr# -> Int# -> IPv4 -> State# s -> State# s # setOffAddr# :: Addr# -> Int# -> Int# -> IPv4 -> State# s -> State# s #
Unbox IPv4 # |
Instance details Defined in Net.IPv4
Vector Vector IPv4 # |
Instance details Defined in Net.IPv4 Methods basicUnsafeFreeze :: Mutable Vector s IPv4 -> ST s (Vector IPv4) basicUnsafeThaw :: Vector IPv4 -> ST s (Mutable Vector s IPv4) basicLength :: Vector IPv4 -> Int basicUnsafeSlice :: Int -> Int -> Vector IPv4 -> Vector IPv4 basicUnsafeIndexM :: Vector IPv4 -> Int -> Box IPv4 basicUnsafeCopy :: Mutable Vector s IPv4 -> Vector IPv4 -> ST s () elemseq :: Vector IPv4 -> IPv4 -> b -> b
MVector MVector IPv4 # |
Instance details Defined in Net.IPv4 Methods basicLength :: MVector s IPv4 -> Int basicUnsafeSlice :: Int -> Int -> MVector s IPv4 -> MVector s IPv4 basicOverlaps :: MVector s IPv4 -> MVector s IPv4 -> Bool basicUnsafeNew :: Int -> ST s (MVector s IPv4) basicInitialize :: MVector s IPv4 -> ST s () basicUnsafeReplicate :: Int -> IPv4 -> ST s (MVector s IPv4) basicUnsafeRead :: MVector s IPv4 -> Int -> ST s IPv4 basicUnsafeWrite :: MVector s IPv4 -> Int -> IPv4 -> ST s () basicClear :: MVector s IPv4 -> ST s () basicSet :: MVector s IPv4 -> IPv4 -> ST s () basicUnsafeCopy :: MVector s IPv4 -> MVector s IPv4 -> ST s () basicUnsafeMove :: MVector s IPv4 -> MVector s IPv4 -> ST s () basicUnsafeGrow :: MVector s IPv4 -> Int -> ST s (MVector s IPv4)
type Rep IPv4 # |
Instance details Defined in Net.IPv4 type Rep IPv4 = D1 ('MetaData "IPv4" "Net.IPv4" "ip-1.7.8-AipOdb0Sv1p9R8aZCz7tFh" 'True) (C1 ('MetaCons "IPv4" 'PrefixI 'True) (S1 ('MetaSel ('Just "getIPv4") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word32)))
newtype Vector IPv4 # |
Instance details Defined in Net.IPv4 newtype Vector IPv4 = V_IPv4 (Vector IPv4)
newtype MVector s IPv4 # |
Instance details Defined in Net.IPv4 newtype MVector s IPv4 = MV_IPv4 (MVector s IPv4)

type IPv4# = Word# #

Unboxed variant of `IPv4`. Before GHC 8.10, this is implemented as a type synonym. Portable use of this type requires treating it as though it were opaque. Use `box` and `unbox` to convert between this and the lifted `IPv4`.

data IPv4Range #

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.

Constructors

IPv4Range |
Fields ipv4RangeBase :: !IPv4 ipv4RangeLength :: !Word8

#### Instances

Instances details

FromJSON IPv4Range # |
Instance details Defined in Net.IPv4 Methods parseJSON :: Value -> Parser IPv4Range # parseJSONList :: Value -> Parser [IPv4Range] # omittedField :: Maybe IPv4Range #
ToJSON IPv4Range # |
Instance details Defined in Net.IPv4 Methods toJSON :: IPv4Range -> Value # toEncoding :: IPv4Range -> Encoding # toJSONList :: [IPv4Range] -> Value # toEncodingList :: [IPv4Range] -> Encoding # omitField :: IPv4Range -> Bool #
NFData IPv4Range # |
Instance details Defined in Net.IPv4 Methods rnf :: IPv4Range -> () #
Data IPv4Range # |
Instance details Defined in Net.IPv4 Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> IPv4Range -> c IPv4Range # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c IPv4Range # toConstr :: IPv4Range -> Constr # dataTypeOf :: IPv4Range -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c IPv4Range) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IPv4Range) # gmapT :: (forall b. Data b => b -> b) -> IPv4Range -> IPv4Range # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> IPv4Range -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> IPv4Range -> r # gmapQ :: (forall d. Data d => d -> u) -> IPv4Range -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> IPv4Range -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> IPv4Range -> m IPv4Range # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> IPv4Range -> m IPv4Range # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> IPv4Range -> m IPv4Range #
Generic IPv4Range # |
Instance details Defined in Net.IPv4 Associated Types
type Rep IPv4Range |
Instance details Defined in Net.IPv4 type Rep IPv4Range = D1 ('MetaData "IPv4Range" "Net.IPv4" "ip-1.7.8-AipOdb0Sv1p9R8aZCz7tFh" 'False) (C1 ('MetaCons "IPv4Range" 'PrefixI 'True) (S1 ('MetaSel ('Just "ipv4RangeBase") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 IPv4) :*: S1 ('MetaSel ('Just "ipv4RangeLength") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word8)))
Methods from :: IPv4Range -> Rep IPv4Range x # to :: Rep IPv4Range x -> IPv4Range #
Read IPv4Range # |
Instance details Defined in Net.IPv4 Methods readsPrec :: Int -> ReadS IPv4Range # readList :: ReadS [IPv4Range] # readPrec :: ReadPrec IPv4Range # readListPrec :: ReadPrec [IPv4Range] #
Show IPv4Range # |
Instance details Defined in Net.IPv4 Methods showsPrec :: Int -> IPv4Range -> ShowS # show :: IPv4Range -> String # showList :: [IPv4Range] -> ShowS #
Eq IPv4Range # |
Instance details Defined in Net.IPv4 Methods (==) :: IPv4Range -> IPv4Range -> Bool # (/=) :: IPv4Range -> IPv4Range -> Bool #
Ord IPv4Range # |
Instance details Defined in Net.IPv4 Methods compare :: IPv4Range -> IPv4Range -> Ordering # (<) :: IPv4Range -> IPv4Range -> Bool # (<=) :: IPv4Range -> IPv4Range -> Bool # (>) :: IPv4Range -> IPv4Range -> Bool # (>=) :: IPv4Range -> IPv4Range -> Bool # max :: IPv4Range -> IPv4Range -> IPv4Range # min :: IPv4Range -> IPv4Range -> IPv4Range #
Hashable IPv4Range # |
Instance details Defined in Net.IPv4 Methods hashWithSalt :: Int -> IPv4Range -> Int # hash :: IPv4Range -> Int #
Unbox IPv4Range # |
Instance details Defined in Net.IPv4
Vector Vector IPv4Range # |
Instance details Defined in Net.IPv4 Methods basicUnsafeFreeze :: Mutable Vector s IPv4Range -> ST s (Vector IPv4Range) basicUnsafeThaw :: Vector IPv4Range -> ST s (Mutable Vector s IPv4Range) basicLength :: Vector IPv4Range -> Int basicUnsafeSlice :: Int -> Int -> Vector IPv4Range -> Vector IPv4Range basicUnsafeIndexM :: Vector IPv4Range -> Int -> Box IPv4Range basicUnsafeCopy :: Mutable Vector s IPv4Range -> Vector IPv4Range -> ST s () elemseq :: Vector IPv4Range -> IPv4Range -> b -> b
MVector MVector IPv4Range # |
Instance details Defined in Net.IPv4 Methods basicLength :: MVector s IPv4Range -> Int basicUnsafeSlice :: Int -> Int -> MVector s IPv4Range -> MVector s IPv4Range basicOverlaps :: MVector s IPv4Range -> MVector s IPv4Range -> Bool basicUnsafeNew :: Int -> ST s (MVector s IPv4Range) basicInitialize :: MVector s IPv4Range -> ST s () basicUnsafeReplicate :: Int -> IPv4Range -> ST s (MVector s IPv4Range) basicUnsafeRead :: MVector s IPv4Range -> Int -> ST s IPv4Range basicUnsafeWrite :: MVector s IPv4Range -> Int -> IPv4Range -> ST s () basicClear :: MVector s IPv4Range -> ST s () basicSet :: MVector s IPv4Range -> IPv4Range -> ST s () basicUnsafeCopy :: MVector s IPv4Range -> MVector s IPv4Range -> ST s () basicUnsafeMove :: MVector s IPv4Range -> MVector s IPv4Range -> ST s () basicUnsafeGrow :: MVector s IPv4Range -> Int -> ST s (MVector s IPv4Range)
type Rep IPv4Range # |
Instance details Defined in Net.IPv4 type Rep IPv4Range = D1 ('MetaData "IPv4Range" "Net.IPv4" "ip-1.7.8-AipOdb0Sv1p9R8aZCz7tFh" 'False) (C1 ('MetaCons "IPv4Range" 'PrefixI 'True) (S1 ('MetaSel ('Just "ipv4RangeBase") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 IPv4) :*: S1 ('MetaSel ('Just "ipv4RangeLength") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word8)))
data Vector IPv4Range # |
Instance details Defined in Net.IPv4 data Vector IPv4Range = V_IPv4Range !(Vector IPv4) !(Vector Word8)
data MVector s IPv4Range # |
Instance details Defined in Net.IPv4 data MVector s IPv4Range = MV_IPv4Range !(MVector s IPv4) !(MVector s Word8)

# Unboxing

These functions are useful for micro-optimizing when GHC does a poor job with worker-wrapper.

box :: IPv4# -> IPv4 #

Convert an unboxed IPv4 address to a boxed one.

unbox :: IPv4 -> IPv4# #

Convert a boxed IPv4 address to an unboxed one.

parserUtf8Bytes# :: e -> Parser e s IPv4# #

Variant of `parserUtf8Bytes` with unboxed result type.

# 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`.