ARP packet codec and resolution cache
README.md

nox-arp #

Warning. This is a work-in-progress experimental fork of arp. Do not use it in production; use arp instead.

A host on an Ethernet network knows the IPv4 address of the machine it wants to reach but needs its hardware (MAC) address to build the frame. The Address Resolution Protocol (RFC 826) answers that question: the host broadcasts a 28-byte request and the owner of the address replies. nox-arp encodes and decodes these IPv4-over-Ethernet packets in pure OCaml, and adds the resolver a TCP/IP stack needs on top of them.

The package has three parts:

  • Arp is the packet codec: Arp.of_string and Arp.decode read a packet, Arp.to_string writes one, and a packet whose hardware or protocol type is not Ethernet and IPv4 is refused.
  • Arp.Cache is a pure resolver. It learns mappings from replies and gratuitous requests (a gratuitous request is one a host broadcasts for its own address, to announce its MAC address when an interface comes up or an address moves; every host that hears it updates its cache). It answers requests for the host's own addresses, queues the data waiting on a resolution, and on each tick retries or gives up on pending requests. It returns the packets to send instead of sending them, so it runs on any I/O layer. By default (Arp.Cache.v) it holds 1024 dynamic entries and evicts the least recently used one beyond that; an entry learnt from the network lives 800 ticks, one tick before the end the cache asks for the address again so that a live neighbour's reply renews it, and a pending request is resent on each tick for 5 ticks before the query fails.
  • Arp_eio, in the nox-arp-eio library, runs that resolver on Eio: each update runs under one lock in the calling fiber, a timer fiber ticks the cache once a second by default, a sender fiber calls the caller's send from a queue bounded by the cache size, and Arp_eio.query blocks the caller until the address resolves. A blocked send drops packets beyond that bound and never delays a timeout. With those defaults an entry lives about 13 minutes (800 s) and an unanswered query gives up after about 5 seconds.

To read the ARP cache of the host's own kernel instead, use arp-cache.

Installation #

Install with opam:

$ opam install nox-arp

If opam cannot find the package, it has probably not reached the public opam-repository yet. Add the overlay repository, then install it:

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

Credits #

Forked from mirage/arp, originally by Hannes Mehnert, released under the ISC license. The packet record and its equal come from upstream src/arp_packet.ml, and the resolution cache is a port of upstream src/arp_handler.ml: the entry type, the eviction of the bounded map, the input/tick/query protocol and the cache defaults all come from upstream. The cstruct codec has been replaced by the wire library. The original copyright notices are preserved in LICENSE.md.