E
E
elisey4742018-07-31 23:10:26
Computer networks
elisey474, 2018-07-31 23:10:26

How does BitTorrent work?

The main feature of BitTorrent is the ability to use each client connected to the distribution to distribute already downloaded parts to other such clients.
But how is this possible, if almost every client sits behind the provider's NAT, it is impossible to connect to you directly and request the desired piece.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2018-07-31
@GavriKos

Clients suddenly communicate directly with trackers - dedicated servers - directly to get a list of seeds and request the right pieces. Another conversation is that these pieces are already being transferred not through a dedicated server.

D
Dmitry Belyaev, 2018-08-01
@bingo347

If I understand correctly, I am interested in the implementation of a peer-to-peer connection (torrent, webRTC) between peers located on different subnets behind NAT.
Let me try to explain with an example:
Suppose a certain program on computer A, connected to the Internet through a router, wants to establish a connection with another copy of itself on computer B, which is also connected to the Internet through its router. Computers are on different subnets, they can only connect via the Internet, they do not know each other's addresses. To connect with each other, both need to solve two problems:
1. Open a local port for listening and forward it to an external port behind the router's NAT
2. Exchange their coordinates (external ip address + external port)
The first task has another problem - in order to "ask" NAT to forward our local port to an external one, you need to find out the address of the router. You can find it out by looking at the address of the gateway through which we access the Internet - this will be the NAT address. But our computer may have several network interfaces, in order to get the gateway address, we will also have to calculate the interface through which we access the Internet.
For the second task, we need an intermediary (torrent tracker, STUN server) known to both parties. It is to him that we will tell our coordinates, as well as some id (possibly more than one), by which another peer can request our coordinates (in the case of torrent, the hashes of the data that we want to distribute act as such id).
So let's get started:
1. Peer B opens a connection to the proxy server.
2. from the metadata of the established connection, it finds out through which network interface it is established
3. from the interface receives the gateway address (NAT)
4. it also starts listening to a free port on this interface
5. Requests NAT to forward the port opened in step 4 to the external port using the NAT-PMP or UPnP IGD
protocol (You can read about these protocols in the article on Habré )
6. Tells the proxy server its id and external port, the server can get the ip address from the open connection meta-
data points 1-6)
7. Peer A requests ip and port of peer B from the intermediary server by its id (in the case of a torrent, there are usually several peers for 1 id)
8. Establishes a connection with peer B by ip and port received from the intermediary server
Profit! can exchange data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question