DHCP wire format, client and server in pure OCaml
README.md

ocaml-dhcp #

A pure-OCaml implementation of DHCP (RFC 2131 and RFC 2132): the wire codec, a client and a server.

Overview #

A unikernel or a freshly flashed board boots with no IP address, and it has to get one over DHCP before it can talk IP to anyone. The library has two layers. The core builds and parses messages, runs the client state machine of RFC 2131 figure 5 and answers requests from a server's lease database. It does no I/O and reads no clock, so a test or a fuzzer can feed it any sequence of messages and timer expiries. The Eio driver on top moves the bytes over a UDP socket and supplies the time.

Every client transition is read from one table, Dhcp.Client_control.machine. It has seven phases and fourteen events, and each of its 98 cells carries an answer and the RFC section it comes from. The server answers DISCOVER, REQUEST, DECLINE, RELEASE and INFORM from a static Dhcp.Server.Config.t. It does not read the ISC dhcpd.conf format.

Installation #

Install with opam:

$ opam install dhcp dhcp-eio

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install dhcp dhcp-eio

Usage #

Dhcp_eio.lease runs the DISCOVER, OFFER, REQUEST, ACK exchange for one hardware address and returns the lease. Dhcp_eio.renew extends that lease once T1 has passed. For a long-lived interface, Dhcp_eio.maintain acquires and renews leases until its fiber is cancelled. Its on_lease callback applies each ACK, including new DNS settings; on_lost withdraws the address after a NAK or expiry, before another acquisition starts. The caller owns interface cleanup when the fiber ends.

T1 is the renewal time of RFC 2131, section 4.4.5. The server may send it in option 58 (the lease's renewal_t1); if it does not, T1 is half the lease. After T1 the client asks the server that granted the lease for more time, and after T2 (option 59, by default 87.5% of the lease) it asks any server. Dhcp.Client.renewal_time and rebinding_time compute the two. A retransmission waits 4 seconds, then 8, doubling up to 64, and each wait moves by up to one second either way at random (section 4.1). While renewing, the client waits half the time left to the deadline, and never less than 60 seconds (section 4.4.5). The pure client holds no clock; a caller of renew schedules it. maintain supplies that scheduling: it records the original transmission time, renews at T1, rebinds at T2 and never waits past expiry. Each exchange has the same bounded send policy as lease.

Dhcp_eio.serve answers requests on port 67 until its fibre is cancelled. When the carrier is not a UDP socket, for example a raw Ethernet device in a unikernel, Dhcp_eio.drive takes the carrier's own send and recv functions.

API #

  • dhcp: the pure library.
    • Dhcp.Codec and Dhcp.Options: the message and option codecs.
    • Dhcp.Packet: the Ethernet, IPv4 and UDP framing a client must build before it has an address.
    • Dhcp.Client, Dhcp.Client_control and Dhcp.Client_event: the client state machine, its transition table and its events.
    • Dhcp.Server and Dhcp.Lease: the server and its lease database.
  • dhcp-eio: the Eio client and server drivers (Dhcp_eio).

Credits #

This library is forked from charrua, written by Christiano F. Haesbaert, Gina Marie Maini and Mindy Preston and released under the ISC licence. The cstruct-based wire codec is replaced by the wire library, and the Unix and MirageOS backends by Eio. The original copyright notices are kept in the file headers.

Licence #

ISC. See LICENSE.md.