Skip to main content

Module net

Module net 

Source
Expand description

This computer’s network, as a caller needs it before it acts: whether a host port is free (port_free) and, when it isn’t, which process holds it (port_holder), and its public IPv4 address, as the internet sees it (public_ipv4): what a cloud firewall opens to when it lets this computer alone in. Independent services, run by different companies, are asked at once (curl, through the Context’s runner), and two must give the same address: one that is down or blocked fails nothing, and one that answers wrong (a captive portal’s page, a compromised service) can’t open a firewall to someone else.

Enums§

PublicIpFailed
Why no public address was agreed on.

Constants§

HOLDER_LOOKUP
How long each of the system’s tools port_holder asks may take.
RESOLVERS
The services asked by default: each answers a plain-text address over HTTPS.
RESOLVER_TIMEOUT
How long one service may take: one that hangs doesn’t hold up the others’ answer for long.

Functions§

agreed
The address answers (one per service of resolvers, in their order) agree on: at least two gave it, and none another one.
is_public_ipv4
Whether ip is an address on the internet: not private, loopback, link-local, shared (carrier-grade NAT), documentation, benchmarking, multicast, reserved or broadcast. Anything else can’t be where this computer’s traffic comes from, as a cloud firewall sees it.
port_free
Whether nothing on this computer holds host port for protocol where a published port goes (the loopback): a socket bound there and let go at once, nothing kept. A TCP port is also taken when something answers on it: a listener on every address (0.0.0.0) doesn’t stop a loopback bind on every system (macOS lets one through), but it does answer. A free port can still be taken a moment later: this is a check before acting, not a reservation.
port_holder
The process holding host port for protocol on this computer (its program’s name and its pid), when the system’s own tools name one: lsof, then ss (macOS, Linux), netstat then tasklist (Windows), each run through the Context’s runner within HOLDER_LOOKUP. What a caller says beside a port port_free found taken, so the user knows what to stop. Best-effort: none when no tool answers or names a process (ss without root names none).
public_ipv4
This computer’s public IPv4 address, as resolvers (RESOLVERS, unless a caller names its own) agree on it, all asked at once.