package ipaddr

  1. Overview
  2. Docs

A collection of functions for IPv4 addresses.

type t

Type of the internet protocol v4 address of a host

val make : int -> int -> int -> int -> t

Converts the low bytes of four int values into an abstract V4.t.

Text string conversion

These manipulate human-readable IPv4 addresses (for example 192.168.1.2).

val of_string : string -> (t, [> `Msg of string ]) Stdlib.result

of_string s is the address t represented by the human-readable IPv4 address s. Returns a human-readable error string if parsing failed.

val of_string_exn : string -> t

of_string_exn s is the address t represented as a human-readable IPv4 address s. Raises Parse_error if s is not exactly 4 bytes long.

val of_string_raw : string -> int Stdlib.ref -> t

of_string_raw s off acts as of_string_exn but takes as an extra argument the offset into the string for reading. off will be mutated to an unspecified value during the function call. s will a Parse_error exception if it is an invalid or truncated IP address.

val with_port_of_string : default:int -> string -> (t * int, [> `Msg of string ]) Stdlib.result

with_port_of_string ~default s is the address t represented by the human-readble IPv4 address s with a possibly port :<port> (otherwise, we take the default value).

val to_string : t -> string

to_string ipv4 is the dotted decimal string representation of ipv4, i.e. XXX.XX.X.XXX.

val to_buffer : Stdlib.Buffer.t -> t -> unit

to_buffer buf ipv4 writes the string representation of ipv4 into the buffer buf.

val pp : Stdlib.Format.formatter -> t -> unit

pp f ipv4 outputs a human-readable representation of ipv4 to the formatter f.

Octets conversion

These manipulate IPv4 addresses represented as a sequence of four bytes. (e.g for example 0xc0a80102 will be the representation of the human-readable 192.168.1.2 address.

val of_octets : ?off:int -> string -> (t, [> `Msg of string ]) Stdlib.result

of_octets ?off s is the address t represented by the IPv4 octets represented by s starting from offset off within the string. s must be at least off+4 bytes long. Returns a human-readable error string if parsing fails. off defaults to 0.

val of_octets_exn : ?off:int -> string -> t

of_octets_exn ipv4_octets is the IPv4 address represented by ipv4_octets starting from offset off within the string. Raises Parse_error if ipv4_octets is not at least off+4 bytes long. off defaults to 0.

val write_octets : ?off:int -> t -> bytes -> (unit, [> `Msg of string ]) Stdlib.result

write_octets ?off ipv4 b writes the ipv4 as octets to b starting from offset off. b must be at least off+4 long or an error is returned.

val write_octets_exn : ?off:int -> t -> bytes -> unit

write_octets_exn ?off ipv4 b writes the ipv4 as octets to b starting from offset off. b must be at least off+4 long or a Parse_error is raised.

val to_octets : t -> string

to_octets ipv4 returns the 4 bytes representing the ipv4 octets.

Int conversion

val of_int32 : int32 -> t

of_int32 ipv4_packed is the address represented by ipv4_packed.

val to_int32 : t -> int32

to_int32 ipv4 is the 32-bit packed encoding of ipv4.

val of_int16 : (int * int) -> t

of_int16 ipv4_packed is the address represented by ipv4_packed.

val to_int16 : t -> int * int

to_int16 ipv4 is the 16-bit packed encoding of ipv4.

MAC conversion

val multicast_to_mac : t -> Macaddr.t

multicast_to_mac ipv4 is the MAC address corresponding to the multicast address ipv4. Described by RFC 1112.

Host conversion

val to_domain_name : t -> [ `host ] Domain_name.t

to_domain_name ipv4 is the domain name label list for reverse lookups of ipv4. This includes the .in-addr.arpa suffix.

val of_domain_name : 'a Domain_name.t -> t option

of_domain_name name is Some t if the name has an .in-addr.arpa suffix, and an IPv4 address prefixed.

Utility functions

val succ : t -> (t, [> `Msg of string ]) Stdlib.result

succ ipv4 is ip address next to ipv4. Returns a human-readable error string if it's already the highest address.

val pred : t -> (t, [> `Msg of string ]) Stdlib.result

pred ipv4 is ip address before ipv4. Returns a human-readable error string if it's already the lowest address.

Common addresses

val any : t

any is 0.0.0.0.

val unspecified : t

unspecified is 0.0.0.0.

val broadcast : t

broadcast is 255.255.255.255.

val nodes : t

nodes is 224.0.0.1.

val routers : t

routers is 224.0.0.2.

val localhost : t

localhost is 127.0.0.1.

module Prefix : sig ... end

A module for manipulating IPv4 network prefixes (CIDR).

val scope : t -> scope

scope ipv4 is the classification of ipv4 by the scope hierarchy.

val is_global : t -> bool

is_global ipv4 is a predicate indicating whether ipv4 globally addresses a node.

val is_multicast : t -> bool

is_multicast ipv4 is a predicate indicating whether ipv4 is a multicast address.

val is_private : t -> bool

is_private ipv4 is a predicate indicating whether ipv4 privately addresses a node.

include Stdlib.Map.OrderedType with type t := t
val compare : t -> t -> int
module Set : Stdlib.Set.S with type elt := t
module Map : Stdlib.Map.S with type key := t