Skip to content

feat(torrent): punch through NATs with BEP 55 so peers behind them reach each other - #541

Merged
linroid merged 5 commits into
mainfrom
feat/torrent-holepunch
Oct 13, 2026
Merged

linroid merged 5 commits into
mainfrom
feat/torrent-holepunch

Conversation

@linroid

@linroid linroid commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

A device behind more than one NAT can't be reached by any peer: port mapping only opens the nearest router. On the desktop this was found on, the path is computer → home router (e.g. 192.168.1.1) → a second NAT (e.g. 10.0.0.1) → internet. The outer NAT answers neither NAT-PMP nor PCP, so every swarm reported incoming [none]. Hole punching gets peers through anyway.

What changes for users

  • Peers behind NATs reach each other. This covers home routers, carrier-grade NAT, and several layers of either. When two peers can't dial each other, a peer connected to both introduces them, and they dial each other over uTP at the same moment, so each NAT lets the other's packets in.
  • Ketch takes all three roles:
    • It relays introductions for its own peers.
    • When it can't dial a peer, it asks the peer that told it about that one to introduce them.
    • When introduced, it dials at once. Seeds do this too, so leechers behind NATs reach a seed behind its own.
  • Compatible with other clients: the wire format follows BEP 55 (ut_holepunch), as qBittorrent, Deluge and other libtorrent-based clients speak it.
  • Scope: public v1 torrents only, the same rule as peer exchange, and only while uTP is on, since punching dials over uTP. With uTP off, Ketch neither offers it nor acts on it. V2 and hybrid sessions don't punch yet.
  • Bounded: each peer may name at most 8 hosts a minute for Ketch to dial, within the swarm's usual 4,096 candidates.

How

  • peer/PeerHolepunch.kt: HolepunchMessage codec in BEP 55's layout (type, address type, IPv4 or IPv6 address, port, err_code).
    • Decoding tolerates a missing err_code.
    • Unknown or short messages decode to null and are ignored rather than closing the peer.
  • PeerExtensions.handshake(holepunch = …) advertises ut_holepunch; pexFlags sets BEP 11 flag 0x08 for peers that offer it.
  • v1/SwarmHolepunch.kt, one per swarm run, knows the connected peers that take part and which peer introduced each endpoint by PEX.
    • relay: sends a CONNECT to both peers, each naming the other: a peer reached over uTP by its uTP source endpoint, else where it listens. Otherwise it answers with NOT_CONNECTED, NO_SUPPORT or NO_SELF, and ignores peers that never offered the extension (BEP 55's MUST).
    • rendezvous: asks the last introducer of an endpoint, at most once per endpoint in 5 minutes.
  • TorrentSwarm wiring:
    • utpEnabled, the engine's live uTP setting passed through KotlinTorrentSession, gates everything (holepunchOn()).
    • Each worker sends the coordinator's messages for its peer through its own outbox on its next pass.
    • A dial that fails while still dialing triggers rendezvous.
    • A CONNECT (at most 8 a minute per peer, MAX_CONNECTS_PER_MINUTE) goes through a punches channel: within MAX_CANDIDATES, the endpoint jumps the queue and is dialed uTP first (onUtpPeers), downloading or seeding.
    • The peer's simultaneous uTP connection arrives as an incoming peer; a duplicate of an endpoint being dialed is closed, so one connection remains.
    • The swarm summary adds holepunch peers= relayed= asked= connects=.
  • AGENTS.md and docs/torrent.md updated.
  • Also fixes a flaky KetchDispatchersTest (library:core) that compared a pool thread's name while coroutine debug mode, on under Gradle's assertions, could still carry @coroutine#N on it.

Tests

  • :library:torrent:jvmTest passes in full.
  • New PeerHolepunchTest covers the IPv4/IPv6 layout, a missing err_code, ignored garbage and the handshake entry.
  • New SwarmHolepunchTest covers a relay picking the uTP source or listen endpoint, each error code, ignoring non-participants, and introducer retry and leave.
  • New TorrentSwarmHolepunchTest drives a real swarm with scripted peers on loopback:
    • it relays a rendezvous between two peers;
    • it dials, uTP first, a peer a relay names, while downloading and while seeding;
    • it limits CONNECTs to 8 a minute per peer;
    • it neither offers nor acts on hole punching with uTP off;
    • it asks an introducer to punch to a peer it couldn't dial.
  • Not yet tried against live swarms; the summary's holepunch counters are there to check it.

Known limits

  • NAT behavior: punching works through NATs that keep one external port per local port, which most home routers do; NATs that change it per destination defeat it.
  • TCP-connected peers: for them, the relay hands out where they listen, assuming the NAT keeps that port for UDP. Peers reached over uTP use their real uTP mapping.
  • Libtorrent relays: a libtorrent relay introduces us by our connection's source port, so introductions through it work best when we reached it over uTP; Ketch still dials TCP first.
  • V2 and hybrid: not covered by this PR.

…ach each other

Peers behind routers or carrier-grade NAT, however many layers, cannot dial each other, so a
device that no port mapping can open only ever talks to peers it reaches itself. v1 swarms of
public torrents now speak `ut_holepunch`: they relay a rendezvous between two connected peers
with a CONNECT to each, ask the peer that introduced an endpoint they cannot dial to introduce
them, and dial a peer they are introduced to over uTP at once, while it dials them.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T14:42:50.130691Z df7711e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 13, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: df7711e590

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@linroid
linroid merged commit 26190bd into main Oct 13, 2026
11 checks passed
@linroid
linroid deleted the feat/torrent-holepunch branch October 13, 2026 15:10
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