English
ours-net-info
libs/net-info — what the host's network looks like from inside the process: interfaces and the default route, the socket and process behind a connection, local network neighbours. Plus writing to the kernel for auto_nft.
Reading (no privileges)
netlink— rtnetlink dumps: the tables of links, addresses, routes, neighbours (Link,Route,Neighbour…). Only dumps, no changes. Responses are parsed with bounds checks: a short or broken response must not bring down the router.inet_diag—INET_DIAG: which socket (and which uid) a connection belongs to by its 4-tuple. First an exact kernel query (inet_diag_bc); if the kernel cannot answer for a flow (UDP, "connected" differently, a listener) — a dump filtered by the local port.proc— the socket's process from/proc: the socket inode is found by walking/proc/<pid>/fd, the uid is taken from the socket table (the process may have dropped privileges). The walk starts with the uid the kernel reported and, failing that, goes through all users (ALL_USERS), as in sing-box. The/procroot is a parameter, so the parsing is checked on fixtures.lease— DHCP lease files as sing-box reads them: dnsmasq/odhcpd, ISC dhcpd, Kea CSV, bootpd plist. The format is chosen by the file name; parsing does not fail — an incomprehensible line is skipped.Network— the shared table read by the dialer and rule conditions. It is filled when the instance is built and reread on use (REFRESH), so a rule on the default interface's type sees the current one.Snapshot— one snapshot (default_interface,interface,up).wan— the host's WAN:read()— the interfaces the default routes of themaintable go through, separately for IPv4 and IPv6 (there may be several, by metric), with the addresses of their own family without link-local;from_tables— the same from ready tables (for tests);is_private(RFC 1918, CGNAT100.64.0.0/10, ULA, link-local, loopback) andWan::is_private— so that the caller warns that the address is not visible from outside.Monitor— a non-blocking rtnetlink socket subscribed to the link, address and route groups of both families:drain()only says that something has changed (a queue overflow is a change too), and the tables are reread.
What cannot be read is returned as an error, not as an empty answer: a rule whose mechanism is missing must fail loudly.
Writing (needs CAP_NET_ADMIN)
request— netlink requests with every message acknowledged;Attributeswith nested attributes (nest),Socket::transact/call(a request with a response)/dump.nft— nf_tables messages (tables, chains, sets, rules made of expressions), sent in one batchNFNL_MSG_BATCH_BEGIN … END: the kernel applies it entirely or not at all.policy—ip rulefor marked packets and the routelocal default dev lo(delivery to the local tproxy socket);FibRule— a rule with any sing-tun selectors (from/to,iif/oif,fwmark,uidrange,dport,not,goto,nop,unreachable,suppress_prefixlength), deletion by priority, a list of priorities;HostRoute—local <address> dev <interface> table <n>.link— an interface the process created itself:add(ip link add <name> type <kind>; the kernel loads the kind's module itself),delete,kind(the index and kind by name),set_up(bringing up and MTU),add_address/delete_address(for IPv6 without DAD, so that the address can be taken right away),add_route/delete_route(with a table and a metric),index.genl— generic netlink: a family by name (CTRL_CMD_GETFAMILY; a query for an unknown family makes the kernel try to load its module) and thegenlmsghdrheader.wireguard— the kernel's WireGuard interface, likewg set/wg show:WG_CMD_SET_DEVICE(key, port, fwmark, peers with the flagsREMOVE_ME/REPLACE_ALLOWEDIPS, endpoint, psk, keepalive, allowed IPs) and parsing theWG_CMD_GET_DEVICEdump (a peer that did not fit into a message continues in the next one). The familieswireguard(Linux) andamneziawg(amnezia-vpn/amneziawg-linux-kernel-module): the numbers are the same, and amneziawg has the AWG parameters after them — Jc/Jmin/Jmax, S1–S4, H1–H4 (u64ranges = last<<32|first), I1–I5 (strings), the header protection key, the 3.x parameters (u16ranges), and persistent keepalive is au32range, not au16. The numbers are pinned by the module's uapi at commit 63ed35de3d3f (3.1.20261113-02).conntrack— conntrack entries through ctnetlink: a list (flows) and deletion by the original tuple (delete), so that a flow is classified anew after the capture set changes.
None of nft, ip or conntrack is run as a program.
Who uses it
ours-runtime (SystemHost: interfaces, process and neighbour lookup), ours-mod-router (network and neighbour conditions), ours-mod-rule-process (proc::executable_name), ours-mod-dns, ours-auto-nft (all the writing), ours-mod-wireguard (link, genl, wireguard, nft, policy, wan), ours-mod-tun (link, policy, nft, netlink), ours-mod-clients (wan); ours-mod-dial uses it only in its tests.
Specifics
Dependencies — rustix only. No unsafe: everything goes through rustix sockets.
Tests
- Unit tests in
src/— parsing messages and files on fixtures; thewireguard,genlandlinkmessages are checked byte for byte against whatwg/ipand the modules' uapi write. libs/net-info/tests/host.rs— the real host: the kernel's responses must parse into the same form; tests that need a missing table skip themselves.libs/net-info/tests/bench_cost.rs—#[ignore], the cost of queries:cargo test --release -p ours-net-info --test bench_cost -- --ignored --nocapture.