網頁

2014年5月11日 星期日

tcp udp hole punching

Env ?


  • no limitation. (windows, unix/linux, mac ...)
  • The only limitation is it is "socket programming".


What is it ?


  • This is a trick for TCP hole punching defined in the RFC tcp spec3.4. Establishing a connection
    • There are two ways to establish a connection.
      • The first method is "accept()...connect()".
      • The second method is "connect()...connect()".
    • We discuss here is the second method.
  • Use this tech you can conquer NAT issue.
  • This method is also called p2p tcp hole punching.


How to use it ?


  • As Peer-to-Peer Communication across NAT 4. TCP hole punching explains, we can use "connect()...connect()" to do tcp hole punching.
  • The example code is simple:
    • tcp_hole_punching_client.c
    • while (connect(s, (struct sockaddr *)&serverAddr, sizeof(serverAddr))) {
          printf("\n[%d]", connect_times++);
          perror("connect failed");
          usleep(1000+(rand()%1000));
      }
      
  • You may have some questions after read above code:
    • 1. why use "while" to connect ? 2. why need to use usleep ?
      • Ans: Before this, you need to know how does it work ?

How does it work?

  • If each client send SYN to each other simultaneously, we can imagine for the left side when it sends out SYN packet, it will allow packets which are the destination of the SYN packet to send in some packets. For the right side, it also does.
  • So that the TCP hole punching can be established by this way.

Pre-condition


  • The router type is "preserves port" type and should not be
    • Random port type
    • Symmetric (NAT cones, please refer NAT wifki)


沒有留言:

張貼留言