|hadron|: QQT-013 the chain is ordered by authority, not preference: a literal is never rewritten because an operator who typed an address meant that address, and the fallback answers only where the client declined rather than disagreed. The consequence is invisible until it inverts: the fallback is a hand-written replica of ssh’s grammar, so the first config it reads differently from the client turns a working -r gpu into a silent dial at the wrong host while ssh gpu still reaches the right one. Every ordering choice below is that claim’s consequence.
The behavioural musts are docs/functional_requirements_qqt-ssh-bridge.md §2 (SB-4 … SB-8); this file owns how they are met, and the module boundaries that keep them met.
The chain
A typed host on -r resolves down one ordered chain. The first link with an answer wins; a link that declines is not an error, and a link that reports a fatal value ends the chain with that fatal named (SB-8).
| order | link | answers when | declines when |
|---|---|---|---|
| 1 | literal (SB-7) | the typed host is an IPv4 dotted quad or a bracketed IPv6 literal | never — a literal is dialled as written, no config consulted |
| 2 | the client (SB-4) | an ssh client is found and its dump names a hostname | no client, or the client reports no hostname |
| 3 | the fallback (SB-6) | the host list (~/.ssh/config, or --ssh-config) yields a match |
the file is absent, unreadable, or has no matching block |
| 4 | the typed name | always | never |
-r accepts host or host:port. A port from -r overrides the resolved one (SB-5). Where neither -r nor the config names a port the chain dials ssh’s own default of 22 and marks the target defaulted, so the report says (default port) rather than letting a default be read as the config’s choice — a config that omits Port is nearly every real one, and refusing it would refuse ordinary use. The :port split belongs to the caller, not the resolver: only a bracket-aware split keeps a zoned [fe80::1%eth0] intact.
Each link reports which one answered (Source), and -r logs that label — “gpu resolved by ssh” tells an operator which file to edit; “dialled as written” tells them nothing is wrong. Resolution runs inside the connect loop, so a --reconnect redial asks the chain again: the client link runs a fresh ssh -G (a config edited while the tunnel was down is seen), while a parsed host list is read once at startup and reused — the fallback answers from the bytes it was built with.
The single-dump invariant
One ssh -G per resolve, never two. A second dump would re-run Match exec, which is side-effecting by SB-4’s own design, so a config that answers differently on the second call would hand back a hostname from one invocation and a port from another. Chain::resolve therefore asks the client once and takes both fields from that one answer; the count is asserted by one_resolve_runs_the_clients_match_exec_exactly_once.
The run is bounded. A Match exec whose command never returns — or a HostName whose % token blocks on a lookup — would otherwise hold the dial open forever, so the client is spawned, polled to a ten-second deadline, and killed past it. Both pipes drain on their own threads, because a Match exec that prints before the dump would fill a pipe and deadlock a parent polling for exit. A timeout is Fatal, named as such, and the chain ends there like any other refusal.
Module boundaries
The fallback parser is split across src/hosts/, one concern per file, with the public surface on mod.rs and everything else pub(crate).
src/hosts/mod.rs — the list
The type an operator’s config becomes, and the two-pass resolve over it:
pub struct Resolved { pub hostname: String, pub port: Option<u16> }
pub struct HostList { /* private */ }
impl HostList {
pub fn empty() -> Self;
pub fn parse(text: &str) -> Self;
pub fn load(path: &Path) -> Self;
pub fn from_default_file() -> Self;
pub fn resolve_host_list(&self, typed: &str) -> Option<Resolved>;
}
impl From<&str> for Resolved;
parse and load return the list itself, not a Result: a missing file is “no entries”, never an error, because a host list is a convenience.
None from resolve_host_list means the list has no opinion — no match, or the typed string is a literal. It is never an error channel; fatality is the client’s to report (SB-8) and the fallback’s to tolerate.
Two passes, not one: Match final is defined by what the first pass left unset, so the first fills from every ordinary block and the second tops up from the blocks only it enables, and no value already obtained is reached twice.
src/hosts/lex.rs — the line lexer
split_keyword, unquote, parse_port, split_patterns, logical_lines, is_literal, glob_match. This is the one home for the literal test: the chain re-exports is_literal rather than keeping a second copy, because a second copy is a second answer the day either changes. split_keyword takes both keyword value and keyword=value, ssh’s own tolerance; a port is 1..=65535.
src/hosts/parse.rs — the walk
parse_text and parse_file: the directive walk that turns lines into Blocks, expanding Include where it appears and folding a Match line into the enclosing block’s Criteria. A top-level Include is unconditional; one inside a Host/Match block is conditional on that block, which is ssh’s rule. Include nesting is bounded at sixteen hops, with a cycle cut earlier at the hop that repeats a file already on the stack — a config can, by intent or by a symlink cycle, include itself. A HostName expands its %h token and is lower-cased, matching the client’s dump.
src/hosts/match_block.rs — criteria evaluation
Query is the name being resolved; applies decides whether one block’s Criteria holds for it. Clauses join with and; patterns inside one clause join with or, a leading ! excluding. Match exec runs the command and follows its exit status, because the client runs it and the fallback must agree; Match localnetwork tests the dialling box’s own address.
src/hosts/include.rs — include paths
include_paths: tilde and %d expansion, ~user homes, globs, and several paths on one Include line — ssh reads every one.
The client
src/ssh_dump.rs:
pub enum ClientAnswer {
/// The client ran and answered. `hostname` is the dump's `hostname` line.
Answered { hostname: String, port: Option<u16> },
/// No ssh client found.
NoClient,
/// The client refused the file, or the run timed out. The string is the
/// client's own stderr, or the timeout named.
Fatal(String),
}
pub fn via_client(typed: &str, config: Option<&Path>) -> ClientAnswer;
pub fn parse_dump(dump: &str) -> Option<(String, Option<u16>)>;
Runs ssh -G -F <config> <typed> when a config path is given, ssh -G <typed> otherwise. The client is looked for on PATH first — a wrapper or a version manager’s ssh is what the operator’s own command runs — then in /usr/bin and /bin, where a daemon started with a minimal environment usually finds it.
The dump is key value lines, lower-case keys, first value wins per key — read only the keys the chain consumes: hostname and port. A repeated port is a different directive’s value, never a repair of an unparsable first one. A non-zero exit is Fatal, carrying stderr; a timeout is Fatal too.
Match exec runs because the client runs it (SB-4). Never filter it.
The chain
src/resolve.rs:
pub enum Source { Literal, Client, Fallback, Typed }
pub struct Target {
pub hostname: String,
pub port: Option<u16>,
pub source: Source,
/// The chain supplied ssh's default port rather than the config naming one.
pub defaulted: bool,
}
/// ssh's own default when nothing names a port.
pub const DEFAULT_PORT: u16 = 22;
pub enum ClientPolicy { Auto, Never }
pub struct Chain { /* owns the parsed list, the config path, the policy */ }
impl Chain {
pub fn from_default_file(policy: ClientPolicy) -> Self;
pub fn from_config(path: Option<PathBuf>, policy: ClientPolicy) -> Self;
pub fn resolve(&self, typed: &str, explicit_port: Option<u16>) -> Result<Target, String>;
pub fn config(&self) -> Option<&Path>;
}
Err carries a sentence a reader can act on, and exactly one shape earns one: the client reporting a fatal value (SB-8). A chain that ends with no port is not an error — it dials DEFAULT_PORT and sets defaulted, because ssh does the same and a portless block is the ordinary config.
ClientPolicy::Never skips the client link entirely and lets the fallback answer — the deterministic mode a scripted dial uses when it must not depend on a file the operator happens to have, or on a Match exec that runs a command. It is what --no-ssh-client sets.
The two flags
--ssh-config <PATH> names the host list. The same file goes to the client with -F and is read directly by the fallback, so both links answer from the same bytes. The default is ~/.ssh/config.
--no-ssh-client resolves from the host list only, never through the client. A dial that must not depend on a config whose Match exec runs a command belongs under this flag; the list’s answer stands in for the client’s wherever the list is able to give one.
The proof
Two layers, both required. The unit layer is a #[cfg(test)] module beside the code it holds, because the crate is a binary with no library target and so no tests/ directory. The end-to-end layer is two shell harnesses, because the questions they answer — whether the fallback agrees with a real client on a real config, and whether a payload actually crosses — cannot be asked from inside a unit test.
src/hosts/differential.rs— the differential corpus: a table of config/name/port rows, each answered twice, once by the host’s own ssh client and once by the fallback, asserting the fallback agrees with the client wherever it claims to answer. Two differences are declared rows rather than silent passes — SB-7’s literal deviation, and unknown keywords that are fatal to a client withoutIgnoreUnknownbut tolerated by the fallback — so a fallback that quietly starts agreeing also fails. Skipped, loudly, when the box has no usable client.docker/scenarios.sh— the scenario matrix: fixture rows for every grammar andMatch/Includeshape, then every alias reachable in the host’s own config, each answered twice (client lane and--no-ssh-client) and compared againstssh -G. It follows the config’sIncludechain to its leaves, so a config assembled from an include tree is swept whole. It exits non-zero on any mismatch, and a mismatch prints both answers.docker/run-tunnel.sh— the tunnel proof. The client dials an alias from a config rather than a literal, in a container with no ssh client, so the byte that crosses is carried by the fallback link; direct TCP to the origin must be refused in the same run or the pass does not count.src/resolve.rs’s own test module — the chain itself: literal pass-through, the single-Match execcount, explicit port override, the defaulted port, fallback decline and tolerance, client absent, client fatal,Neverpolicy, and the label of everySource.src/ssh_dump.rs’s test module — the dump parser’s keys and edge cases, the bounded wait (a_hung_exec_is_bounded_and_named), the pinned-config precedence, and the client’s own fatal words.- Each parser module’s test module — the semantics it owns: precedence and negation in
mod.rs,Match finalin bothparse.rsandmod.rs, the conditionalIncludeand the recursion bound inparse.rs, clause joining inmatch_block.rs, globs and lexing inlex.rs, multi-path includes ininclude.rs.
A behaviour that differs from OpenSSH and is deliberate is a row in docs/functional_requirements_qqt-ssh-bridge.md §9, not a comment.