Skip to content

dispatch ​

The dispatcher: it takes a connection from an inbound, walks the rules (with sniffing where they say so), chooses an outbound, connects and starts the relay. The only provider of the Dispatcher slot in the binary.

Crateours-mod-dispatch, modules/system/dispatch
Binary featurenone, always in the build; sniff enables ours-mod-dispatch/sniff
Crate featuressniff → ours-sniff/all (not checked in src/, it only enables the sniffers)

requires ​

Nothing: the dispatcher works with the ours-api abstractions (Routing, Outbound, Dns), not with other modules' crates.

provides ​

  • Provide::Dispatcher — the factory Dispatcher::new(routing). Besides the dispatcher itself it puts Arc<Tracker> (live connections; the Admin API reads it through Instance::service, app/ui/src/api.rs) and Arc<Dispatcher> into the instance's services.

The kernel requires exactly one Dispatcher provider (see the contract).

Dependencies ​

ours-api, ours-relay (moving data, splice), ours-tracker (the connection registry), ours-sniff, ours-net-sys, ours-bufpool, arc-swap.

Design ​

The whole module is src/lib.rs:

  • Dispatcher — holds the Routing snapshot behind an ArcSwap. Inbounds hold the dispatcher for their whole life; reload replaces only the snapshot.
  • Dispatch::route/connect/packet — the connection's path: the tracker entry is created before routing (the task handle goes there too, a connection has no stop handle of its own), then the rule walk, connecting, the relay.
  • Sniffing (the sniff action, like sing-box's actionSniff): skipped for server-first ports, after a protocol has already been determined and after the same sniffers have failed. Plain TCP is only "peeked at" (peek), so that the data stays in the socket for splice; while it waits for more, SO_RCVLOWAT is one byte above what was seen, so readiness means new data or the end, and is put back to one however the wait ends. Other streams are read and the data is put back in front of the stream.
  • DNS hijack: a connection sent to DNS gets a DnsOutbound — the stream's queries (length + message) and packets are answered by the DNS module; tracking and the reload policy are the same as for ordinary connections. The timeout is sing-box's C.DNSTimeout.
  • FakeIP and override_*: for a packet stream with a changed destination, Translated substitutes addresses there and back; an unknown fake address closes the connection.
  • The resolve action — resolving the destination domain for the following rules.
  • "On demand" metadata: the process, the neighbour's MAC and so on are requested from the host only if the rules need them (RuleNeeds), and the process — only for connections from the host's own addresses.

Reload ​

  • reload(routing) — new connections follow the new snapshot; live ones walk the rules again on the already collected metadata, and the reload policy decides: keep, close, redirect (only for a packet stream). The same outbound object, if reload did not rebuild it, counts as the same.
  • preview(&routing) — the same estimate without changes (for diff/the dashboard).
  • rule_set_updated(tag) — a small reload: it concerns only connections whose rules looked at this rule-set; rule_sets_changed() wakes those who watch the rules outside the dispatcher.

Tests ​

Unit tests in src/lib.rs; behaviour on live connections — in the router tests (modules/routing/router/tests/live.rs, dns_live.rs, rule_set_updates.rs).

GPL-3.0-or-later