How UniBlue handles cancellation, limits socket waits, and decides which TCP ports belong in a network-wide scan.
· 3 min read

How UniBlue Bounds Network Work in Go


A network inventory can spend most of its time waiting for devices that never answer. UniBlue, a Go tool for UniFi networks, limits that wait in two places. Its port planner decides which connections are worth attempting. Once a connection is open, individual checks can set a deadline on the socket.

What Ctrl-C reaches

UniBlue’s CLI passes its arguments and output writers to a run function. A test can call that function with a buffer to inspect its output. The top-level help command prints usage and returns successfully. For commands such as scan and watch, the dispatcher also passes down a context created with signal.NotifyContext. An interrupt or SIGTERM cancels that shared context. UniBlue CLI · Go signal documentation

What happens next depends on where the command is waiting. A pending DialContext can return a cancellation error. Once the dial succeeds, cancelling its context has no effect on the connection. A goroutine already blocked in Read can still be waiting after the user presses Ctrl-C. Go dialer documentation

UniBlue’s IPMI check sets an absolute deadline after its UDP dial. The initial calculation is shown below.

deadline := time.Now().Add(600 * time.Millisecond)
if d, ok := ctx.Deadline(); ok && d.Before(deadline) {
	deadline = d
}

The check passes that time to conn.SetDeadline, which covers reads and writes. If the parent has 100 milliseconds left, the initial exchange gets at most those 100 milliseconds, assuming the deadline was installed successfully. The code ignores the setter’s error. With a signal context alone there is no parent deadline, so the local 600-millisecond limit applies. A signal arriving during the read leaves that deadline in place. Go connection deadlines

Which ports belong in the baseline?

A check for a particular device might need a TCP port that serves no purpose on the rest of the network. UniBlue builds a shared baseline with PlanGlobalPorts. It starts with configured base ports, adds eligible check ports, discards numbers outside 1–65535, and returns a sorted list. A map[int]struct{} records membership, so a port mentioned in several checks appears once. Passive checks that evaluate cached observations contribute no ports of their own. Port planner

The same file gives the example of a ventilation controller identified by its MAC prefix or hostname. PlanHostPorts can add the controller’s specialty port to the hints for a matching client. It starts from existing per-host hints and skips checks already classified as global. That distinction matters when two independent checks name the same port. Excluding a device-specific check from the baseline still leaves other checks and the configured base list free to include that number.

Keeping distinct findings on one port

An HTTP service and an expired TLS certificate can turn up on the same endpoint. When UniBlue converts fingerprinting results into findings, it keys them by endpoint, port, transport, and the property being reported. Including the property lets those two observations coexist.

The converter first skips keys already covered by existing findings. For the remaining candidates, a map holds the index of each key’s entry in the output slice. If a later candidate has a higher base severity, the converter replaces the entry at that index. Equal severity leaves the first entry in place. This preserves the position where each key first appeared while retaining the most severe candidate seen for it. Finding conversion