Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does BitTorrent and Gnutella uses for bypassing NAT when transferring files?

I´m trying to do a p2p file sharing proto/software in Java. I am interested in learning using socket so, no I will not use JXTA or any kind of API.

I read about TCP hole punching, and it may not work on all NAT types. But I can see that Gnutella and BitTorrent always works on every machine/NAT I try. So do they use TCP Hole Punching to initiate a connection between hosts?

Also, a code like this would do the TCP Hole Punching?

    final ServerSocket s = new ServerSocket(7777);
    Thread t = new Thread(new Runnable(){

        public void run() {
            try
            {
                s.accept();
            }
            catch(Exception ex)
            {

            }
        }
    });

    Socket sock = new Socket();
    sock.connect(new InetSocketAddress("IP ADDRESS", 7777), 50000);
like image 458
fredcrs Avatar asked Apr 08 '11 12:04

fredcrs


2 Answers

I think that Universal Plug and Play (UPnP) is a protocol that enables you to set up port forwarding in the router programatically. I'm not sure if that is the only method that those programs use, though.

Take a look at the UPnP PortMapper project for a Java implementation.

This CodeProject article also looks good, although it is not Java: Using UPnP for Programmatic Port Forwardings and NAT Traversal.

like image 63
Justin Avatar answered Oct 04 '22 15:10

Justin


You should have read NAT traversal on Wikipedia first.

However, in reality, the mainly used ways are UPnP, STUN, TURN, as well as ICE which is a combinition of STUN and TURN.

As TCP is a connection-oriented protocol, NATs can control it for each connection, and drop all the packets when the connection is over.
But UDP is connectionless, and the response may come from different port or different addresses (compared to your original destination). Also, there's no exact timeout for a UDP message. So usually less limitations for UDP exist on NATs.
As a result, many of these P2P implements are using UDP or some kinds of UDP-based protocols, like microTP, RUDP, UDT, or even SCTP over UDP (WebRTC is based on it).

You can also learn from articles about NAT traversals, RFCs, and BEPs (BitTorrent documents).

Edit: there's even one more interesting way - ICMP traversal. In fact ICMP (especially the TTL excceeding message) has even less limitations than UDP on NATs, for the error message can be sent from every corner of the internet, and routers with NAT cannot be aware that these messages are not the true ones. However, sending ICMP packets requires a root's permission on Unix-like systems, and Administrators' permission on Windows.

like image 26
swordfeng Avatar answered Oct 04 '22 14:10

swordfeng