Home > Specs > BitTorrent > Peer Connections
Peer Exchange

Peers keep each other updated as to the state of their other connections.  This way a peer can build a model of all connections one step away from it's immediate connections.

The purpose of this mechanism is to allow peers to quickly learn about other peers joining the torrent, without the delay and overhead of polling the DHT or trackers.  It also serves as the foundation of the hole-punching mechanism that can help overcome NAT routers and firewalls.

All PEX updates are done through the ut_pex extension message.  It starts with the message type 20 (one byte), followed by the ut_pex sub-type (one byte) that was specified in the initial extended handshake at the beginning of the connection.  The rest of the message is a BEncoded map with the following keys:

added
This value is a string, 6 bytes per peer.  The first four are an IPv4 address, followed by the two-byte big endian port number.

added.f
This value is a string, one byte per peer, corresponding to the peers in the added key.  This lowest bit (&1) is set if the connection is encrypted.  The next bit (&2) is set if the peer is complete.  The next bit (&4) is set if the connection is over UDP.  The next bit (&8) is set if the peer supports hole punching.  The next bit (&16) is set if the peer connection is incoming.

dropped
This value is a string, 6 bytes per peer.  The first four are an IPv4 address, followed by the two-byte big endian port number.

There are also IPv6 versions of these keys, added6, added6.f, and dropped6.  They have 18 bytes per peer.


This web site is powered by Super Simple Server