Connecting P2P over NAT?

NetworkingP2pNetwork ProtocolsNatBittorrent

Networking Problem Overview


I started to explore the option of connecting with other using a p2p connection, so I coded a simple socket program in JAVA for android devices in which the users can share simple messages p2p (I didn't have any idea about NAT then). I got to know about NAT, so I now need to establish a TCP connection with another user which uses a server for discovery but payload is transferred p2p. I have also looked at XMPP(a very good and detailed explanation of how protocol works is here) and UPnP but I dont know how to implement them.

Another interesting question that arises is of BitTorrent because they can work on any device and even behind a NAT. I am not able to get any explanation of how BitTorrent works.

I have researched a lot but I am stuck.

My questions are:

  1. A detailed explanation of BitTorrent(like here, not how torrents work) and how is it able to work around NAT ?
  2. Is there a way to make a NAT entry programmatically ?
  3. Is socket programming sufficient for p2p ?
  4. How difficult is it to create your own protocol and how can I build one ?
  5. If two devices D1 and D2 want to communicate p2p and they know each other's IP. D1 sends a request to D2 and that can't get through the D2's NAT, but there should be an entry created in D1's NAT. So when D2 tries to send something D1's NAT should discover an entry with D2's IP. Then why is the packet not allowed by it ?

Networking Solutions


Solution 1 - Networking

> Another interesting question that arises is of BitTorrent because they can work on any device and even behind a NAT. I am not able to get any explanation of how BitTorrent works.

This statement looks like you assume that bittorrent needs full connectivity to operate.

That is incorrect.

Behind a NAT device you will still be able to establish outgoing TCP connections. Which generally is sufficient for bittorrent as long as there are other, non-NATed (or NATed but properly port-forwarded) clients in the network that can accept incoming connnections.

NAT has no impact on the flow direction of the data because connections are bi-directional once they are established. It only is problematic for the initial connection setup.

This works perfectly fine for bittorrent because bittorent does not care from which specific node you get your data. Although better connectivity generally does improve performance.

If the identity of the node matters or one-on-one transfers are an important use-case then other p2p protocols usually attempt NAT traversal first and if that fails rely on 3rd party nodes relaying traffic between those nodes who cannot connect to each other directly.

Additionally, IPv6 support will become essential in the future to maintain end-to-end connectivity because more and more ISPs are starting to roll out carrier-grade NAT for IPv4 while IPv6 will remain non-NATed

Solution 2 - Networking

One thing need to be clear is that 100% P2P between all type of NAT is impossible right now. There is no practical way to establish P2P connectivity between **Symmetric and Symmetric/PRC NAT. In this scenario connection is established through a relay server called TURN.

I am answering from your 2nd question because I don't know much about the first one.

  1. Yes. You can send a packet through your NAT and there will be a mapping between your internal IP:Port to your NAT's external IP:Port. You can know these external IP:Port by sending a stun request. Note that this technique doesn't work for Symmetric NAT.

3)Yes socket programming sufficient for p2p.

4)Why do you need a protocol when there already exists several. ICE protocol is the best today for NAT traversal and I don't think it was easy to create. UPnP and NAT-PMP is really vulnerable in terms of security.

5)I think what happens is usually NAT blocks unknown packets coming to it. So when D1 sends a packet to D2, its NAT blocks all packets incoming from D1s IP:Port. That is why connection establishment fails. You have to employ hole punching technique for D1 and D2 to successfully establish P2P connectivity.

**By symmetric NAT I mean symmetric NAT with random port allocation.

Solution 3 - Networking

There is a paper on "Peer-to-Peer Communication Across Network Address Translators" which describes the UDP hole punching method and extends it to be used over TCP as well.

Of course, you will always need a relay server for the cases where hole punching is not supported.

Solution 4 - Networking

  1. Recent versions of BitTorrent use http://en.wikipedia.org/wiki/Micro_Transport_Protocol">µTP</a>;, which is layered above UDP, not TCP. µTorrent uses a private extension (ut_holepunch) that performs UDP hole punching, most other implementations don't bother (with the notable exception of Tixati).

  2. Some NAT routers accept port forwarding requests using either the http://en.wikipedia.org/wiki/Universal_Plug_and_Play">uPNP</a> or the http://en.wikipedia.org/wiki/NAT_Port_Mapping_Protocol">PMP</a> protocol. Whether this is supported depends on the particular brand of router and its configuration.

  3. Yes, socket programming is enough for P2P.

  4. Difficult to answer. I suggest that you read the https://wiki.theory.org/BitTorrentSpecification">wikified and annotated BitTorrent specification for a start.

  5. Yes, this is the principle behind http://en.wikipedia.org/wiki/UDP_hole_punching">UDP hole punching.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3439988View Question on Stackoverflow
Solution 1 - Networkingthe8472View Answer on Stackoverflow
Solution 2 - NetworkingTahlilView Answer on Stackoverflow
Solution 3 - NetworkingFardin K.View Answer on Stackoverflow
Solution 4 - NetworkingjchView Answer on Stackoverflow