go-net-dhcp

A small, dependency-free DHCPv4 server library in pure Go — no cgo.

pure Go · zero cgo RFC 2131 / 2132 DISCOVER · OFFER · REQUEST · ACK · NAK SO_BINDTODEVICE stateless · caller-driven optional Prometheus no external dnsmasq 100% coverage 6 arches
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-net-dhcp is a pure-Go (no cgo) DHCPv4 server library. It implements the RFC 2131 / 2132 wire format (Parse a DISCOVER/REQUEST, BuildReply an OFFER / ACK / NAK), a stateless platform-agnostic decision core (Decide), and a real Linux UDP/67 server bound to a single kernel interface via SO_BINDTODEVICE. It targets the "one subnet, hand out a lease per known MAC" case — a host that owns a bridge / VLAN and needs to answer DHCP for the guests it spawns on it, without running an external dnsmasq. You supply a Source that resolves a client MAC into a Lease; the library never persists anything itself. The wire codec is hand-rolled, so the core module has zero third-party dependencies; the optional Prometheus adapter lives in its own sub-package.

Wire codec ready

RFC 2131 / 2132 parse + build — Parse decodes a DISCOVER/REQUEST (BOOTP header + magic cookie + TLV options), BuildReply emits an OFFER / ACK / NAK. Strict on framing, tolerant of unknown option codes, hand-rolled so the core has zero third-party dependencies.

Decision core ready

Decide(pkt, opts) runs the stateless state machine — DISCOVER→OFFER, REQUEST→ACK, mismatched requested-IP→NAK, unknown MAC / other message types→drop. Build-tag-free and portable, so it is unit-tested on every platform without a socket.

Linux UDP/67 server ready

LinuxServer binds UDP/67 to one kernel interface via SO_BINDTODEVICE + SO_BROADCAST + SO_REUSEADDR and replies by broadcast. The socket path is covered without root through injectable syscall seams, so every error branch runs in CI.

Cross-platform surface ready

A build stub on non-Linux plus a socket-free StubServer that drives the SourceLease pipeline. Callers compile and test the integration surface on darwin / windows / *bsd; the real bind only ships on Linux.

Optional metrics ready

Pass any Metrics implementation in Options; a nil sink discards everything. A ready-made Prometheus adapter (dhcpv4_packets_total{outcome} + dhcpv4_handle_duration_seconds) lives in the prom sub-package so the core library keeps no Prometheus dependency.

Coverage & 6 arches ready

100% test coverage including error branches (socket paths via syscall seams, no root), gofmt + go vet clean, green across all six 64-bit Go arches (amd64, arm64, riscv64, loong64, ppc64le, s390x).

Pure Go, cgo disabled, so it cross-compiles and embeds anywhere. A hand-rolled RFC 2131 / 2132 wire codec keeps the core dependency-free; the socket path is covered without root via injectable syscall seams; metrics are optional and the Prometheus adapter is quarantined in the prom sub-package. Part of the github.com/go-net-dhcp organization.