- OCaml 90%
- C 10%
nox-icmp #
Warning. Work in progress. Do not use it in production; use mirage-tcpip instead, which implements the same protocols and is maintained.
ICMP and ICMPv6 codecs, and an I/O-free echo probe, written from RFC 792, RFC 4443 and RFC 4861.
A ping tool, a network stack and a NAT all read and write the same small
messages. Every ICMP message starts with a type, a code and a checksum over
the whole message (with no IP pseudo-header), followed by a four-byte
rest-of-header. nox-icmp encodes and decodes the echo request and reply,
whose rest-of-header is an identifier and a sequence number, and passes other
message types through verbatim. The codec follows the shape of the other
nox-* network headers, so a NAT can read and rewrite the identifier and the
checksum in place.
Icmp.Ping runs one echo probe. Its behaviour is a table,
Icmp.Ping.machine, written with the
fsm library: three phases
against three events, and every phase change a probe makes is one Fsm.step
over it. The phases are Waiting (request sent, deadline armed), Replied
and Timed_out. The events are a matching echo reply, any other received
message (Unmatched: a reply for another identifier or sequence, a bad
checksum, an ICMP error) and the deadline. From Waiting a matching reply
leads to Replied, the deadline to Timed_out, and an unmatched message back
to Waiting; the six cells of the two terminal phases refuse. Time is an
explicit Mtime.t input, as it is in nox-tcp, so the probe is deterministic
and works with any scheduler or clock. The request below is 21 bytes: the
8-byte echo header (type, code, checksum, identifier, sequence number) and the
default payload nox-icmp-ping, 13 bytes.
# let now = Mtime.of_uint64_ns 1_000_000_000L in
let ping, request = Icmp.Ping.v ~now ~seq:7 () in
String.length request, Option.is_some (Icmp.Ping.deadline ping)
- : int * bool = (21, true)
# let msg =
Icmp.to_string ~payload:"ping"
{ Icmp.ty = Icmp.Echo_request; code = 0; id = 0x1234; seq = 1 }
in
Icmp.checksum msg
- : int = 0
A message with a correct checksum sums back to zero.
Installation #
Install with opam:
$ opam install nox-icmp
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 nox-icmp
Library #
Icmp.tis an echo message header (ty,code,id,seq); the checksum is computed on encode.Icmp.to_stringandIcmp.of_stringbuild and parse a message and its payload.Icmp.Pingbuilds an echo request, handles replies with an explicit~now, and fires its timeout withtimer. A reply completes the probe only when its checksum is valid and its identifier, sequence number and echoed data match the request's. Anything else is discarded in silence, since RFC 792 defines no error to send about an ICMP message.Icmp.Ping.machineis the table the probe runs, built on first use and exported so a caller can read the phases, the events and the answer in every cell. The test suite checks the table withFsm_check.settle; a running probe never does.Icmp.checksumis the RFC 792 internet checksum over a whole message.Icmp.set_identifierrewrites the echo identifier of an encoded message and updates the checksum incrementally (RFC 1624), so a message damaged elsewhere still fails the checksum afterwards.Icmp.Packetis the 8-byte wire header as aWire.Codec.t, with a field handle per field for reading and rewriting in place.Icmp.V6encodes and decodes ICMPv6 echo messages and the Neighbor Discovery messages that replace ARP on IPv6.
The optional nox-icmp-eio package runs Icmp.Ping over any Eio datagram
socket with an Eio monotonic ~clock, and depends on neither Unix nor
eio.unix. Opening an ICMP socket is platform-specific, so the Unix
constructors live in nox-icmp-eio.unix. They open an unprivileged ICMP
datagram socket, on which the host kernel demultiplexes by echo identifier and
rewrites that field, so they pass ~restore_identifier:true; the
documentation of Icmp_eio.of_socket says what the probe then pairs a reply
on.
Credits #
This package carries no mirage-tcpip code. It is written against RFC 792,
RFC 4443 and RFC 4861 on the
wire codec library and
nox-crc, and its data model
differs. mirage-tcpip's Icmpv4_packet branches on a five-way subheader
under a closed eleven-type enum and rejects an unknown type; this package
keeps a flat {ty; code; id; seq} record under a three-constructor
message_type that passes an unknown type through. mirage-tcpip's ICMP layer
is a responder that logs and drops an echo reply, so Icmp.Ping has no
counterpart there.
License #
ISC