Skip to content

fix(dht): use tokenless get_peers replies for traversal - #31

Open
rrmars wants to merge 1 commit into
motrixapp:mainfrom
rrmars:fix/dht-tokenless-get-peers
Open

rrmars wants to merge 1 commit into
motrixapp:mainfrom
rrmars:fix/dht-tokenless-get-peers

Conversation

@rrmars

@rrmars rrmars commented Nov 2, 2026

Copy link
Copy Markdown

Summary

Use tokenless get_peers replies for DHT traversal without treating the replying node as an announce target.

Problem

Some bootstrap-router replies observed through a single local network contain a valid 20-byte id and compact nodes, but no token. aria2 currently extracts the nodes and then throws Malformed DHT message. Missing token. DHTMessageTracker handles the rejected reply as a timeout, so the useful contacts never reach the lookup. With an initially empty routing table this can prevent DHT discovery from progressing. Tracker-discovered peers may sometimes mask the problem.

Protocol rationale and interoperability

BEP 5 specifies a token in both forms of get_peers response; it does not declare the field optional. This change is a deliberate interoperability tolerance for nonconforming router replies, not a claim that those replies comply with BEP 5.

The token protects the subsequent write operation: announce_peer must return a token previously issued by the same node. Read-only traversal can still use the returned contacts without performing that write.

This follows the same separation as libtorrent's find_data_observer::reply: record a write token if present, but continue traversal independently. Its write-candidate selection skips nodes without a recorded token. aria2 remains stricter about a token key that is present with a non-string value.

Changes and boundaries

  • Allow an absent token when parsing a get_peers reply, preserving its otherwise usable nodes/peers.
  • Keep a present non-string token invalid.
  • Only store nonempty tokens for announce candidates; use find rather than inserting missing map entries when finishing lookup.
  • Preserve the existing rule that a node without a nonempty token is not sent announce_peer.
  • Keep required ID and compact-node length validation unchanged, for both IPv4 nodes and IPv6 nodes6. Existing peer-value entry filtering is unchanged; this is not a broad malformed-packet validation rewrite.
  • Do not change the format of replies sent by aria2, incoming announce token validation, tracker behavior, or metadata downloading policy.

Automated tests

Added five CppUnit test methods using the existing test infrastructure:

  • IPv4 and IPv6 tokenless replies retain the returned node.
  • Present integer/list tokens, missing/short IDs, and truncated/oversized compact node records remain rejected in both families.
  • Empty-token nodes produce no announce; a nonempty token is preserved on the encoded announce message.
  • Real parser → tokenless router → lookup of a returned node → completion: only the token-bearing node is announced to, in both families.

Local macOS arm64 results (Apple clang 21.0.0, CppUnit 1.15.1, OpenSSL 3):

  • Before the implementation fix, make -j4 check with the new tests: exactly three Missing token errors among 1,130 tests, identifying both tokenless parser cases and continued traversal. No unrelated failures.
  • After the fix, make -j4 check: 1,130 passed, zero failures.
  • The six e2e files run by the native build job in CI (security-regressions, task-cookies, http-retry, select-dns, legacy-checkpoint-import, http-redirect-policy): 87 passed, 0 failed, 3 conditionally skipped. The skips are the two Windows-only legacy-import cases and the previous-schema-binary compatibility case whose optional baseline binary was not supplied.

The local build was not sanitizer-instrumented. Linux/Windows and the other compiler/TLS configurations remain for CI.

Metadata-only network experiment

Completed 40 trials, 5 per arm per hash. All 35 successful metadata files were independently hash-verified; all failures reached the 120-second deadline. No trials were discarded or replaced.

Magnet / infohash Pristine → patched metadata successes Successful-time median, seconds Missing token log total Median exit IPv4 good nodes
Sintel — 08ada5a7a6183aae1e09d831df6748d566095a10 5/5 → 5/5 4.563 → 4.525 0 → 0 33 → 21
Big Buck Bunny — dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c 5/5 → 5/5 4.405 → 4.445 0 → 0 23 → 35
c4d64a8b2b944fbb546253f144791dc367980eff 2/5 → 5/5 10.452 → 9.692 17 → 0 0 → 3
70eb735e4cc90b6849d9f2e2373f89db465aa94b 4/5 → 4/5 6.538 → 7.548 7 → 0 1 → 6

The reported c4d6 case improved in this sample; the two healthy controls stayed successful. 70eb did not improve in success rate: one patched trial still timed out despite saving six IPv4 good nodes and recording no missing-token error. This patch fixes one interoperability failure, not all peer-connectivity problems. Successful-only times should not be interpreted as a general speedup. The mean wait with failed trials capped at 120 seconds was 76.181 → 23.979 seconds for c4d6, and 29.531 → 30.718 seconds for 70eb.

All results are from one machine and one network environment, on November 2, 2026. Compare a pristine build of current default main (96b49bb471c82d414c760bb67dd6085607174d7e) with the same build plus this patch. No dependency, compiler, discovery-option or network-configuration changes between arms.

Four bare-infohash magnets, five trials per arm per hash, cold DHT state and a fresh private directory/process per trial. Strictly alternating arms per hash, opposite starting order for alternating hashes, at most 120 seconds per trial, at least 180 seconds after native process exit before the same hash is retried. The same nine UDP trackers and dht.transmissionbt.com:6881 bootstrap were used in all trials. DHT/PEX enabled; local discovery disabled. Only metadata was requested (--bt-metadata-only=true); successful saved metadata must hash to the requested infohash. No HTTP xs, torrent-file injection or webseed fallback was used for the trials.

The two public Creative Commons controls, Sintel and Big Buck Bunny, come from the official WebTorrent free-torrents list; their advertised infohashes were independently checked against the official torrent metadata before the trial. The additional hashes reproduce the originally reported discovery cases.

Exit DHT counts are serialized good nodes at shutdown, not runtime peak/total nodes. A trial stops once metadata is saved, so durations differ and node counts are not equal-time measurements. Missing measurements are not treated as zero. Payload byte counters were unavailable; the metadata-only setting and directory audit are separate from a measured zero-payload claim.

This is a small, nonrandomized sample with shared remote caches, changing peers and one network route (including its existing proxy/fake-IP path). Early trials overlapped local automated checks, adding a timing-load limitation. It is not a general success-rate or speed estimate, and does not prove every successful peer was discovered through DHT. IPv6 is covered deterministically in CppUnit, not established as a successful live IPv6 deployment by this experiment.

Bootstrap routers may reply to get_peers with useful nodes but no announce token. Preserve these replies for lookup traversal instead of discarding all returned contacts.

Keep non-string tokens invalid. Only retain nonempty tokens for announce candidates, and look up tokens without inserting missing entries when the lookup finishes.

Add CppUnit coverage for IPv4 and IPv6 tokenless replies, malformed IDs and compact nodes, wrong token types, continued lookup, and the announce token boundary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant