1

I am trying to connect to peer via add_peer() function in LibTorrent. But what if the peer from which I want to download the file is behind NAT? Is there something for NAT Traversal in Libtorrent?

1 Answers1

1

The NAT traversal in libtorrent is limited to:

  1. Explicit port forwards using UPnP, NAT-PMP and PCP.
  2. Implicit (opportunistic) attempts to reach peers via their external port
  3. The peer receiving the connection attempt is not behind a NAT, but the initiating peer is. This is the case NATs are meant to support.

It sounds like you're mostly interested in (2), where we assume both peers are behind a NAT. This is commonly referred to UDP hole-punching.

Generally, if you don't control or have any influence over the peer you're trying to connect to, you're limited in what measures you can take.

Also, if the neither NAT is a full-cone (or let's say, p2p-friendly) it may not be possible for the peers to connect. A p2p-friendly NAT generally accepts incoming connections from IPs they have not had any interaction with previously.

The main two approaches used by libtorrent (and bittorrent clients generally) are:

  1. commonly connected peers may introduce two NATed peers to each other via the peer exchange extension. In this mode both peers try to connect to each other simultaneously, hoping that both NATs will open up pin-holes for the ports that are being attempted. This only works if the swarm has at least one peer that's not behind a NAT. You can find more information about this in BEP 55

  2. Sharing the UDP port for uTP, DHT and UDP trackers and having the listen port be implied by the source port of the tracker and DHT announce. With some luck, that source port can also be used by other hosts to reach the NATes client. This works because uTP connections also run over UDP.

Arvid
  • 10,915
  • 1
  • 32
  • 40