TCP打孔 [英] TCP Hole Punching

查看:150
本文介绍了TCP打孔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mingw工具链使用Windows套接字实现TCP打孔.我认为这个过程是正确的,但是 hole 似乎没有必要.我使用了作为参考.

I'm trying to implement TCP hole punching with windows socket using mingw toolchain. I think the process is right but the hole doesn't seems to take. I used this as reference.

  1. A B 连接到服务器 S
  2. S 发送到 A B 的路由器IP +用于连接到 S 的端口
  3. S B
  4. 执行相同的操作
  5. A 开始2个线程:
    • 一个线程尝试使用 S
    • 发送的信息连接到 B 的路由器
    • 当另一个线程连接到 S
    • 时,另一个线程正在等待用于连接其路由器的同一端口上的传入连接
  1. A and B connect to the server S
  2. S sends to A, B's router IP + the port it used to connect to S
  3. S does the same for B
  4. A start 2 threads:
    • One thread tries connecting to B's router with the info sent by S
    • The other thread is waiting for an incoming connection on the same port used to connect to its router when it connected to S

自那时以来,我认为代码没有问题:

I have no issue in the code I think since:

  • A B 确实可以互相使用IP和端口
  • 他们俩都在与服务器联系时监听他们用来连接路由器的端口
  • 它们都连接到正确的IP和端口,但超时(代码错误10060)
  • A and B does get each other ip and port to use
  • They are both listening on the port they used to connect to their router when they contacted the server
  • They are both connecting to the right ip and port but get timed out (code error 10060)

我想念什么吗?

编辑:在进程浏览器的帮助下,我看到一个客户端成功建立了与对等方的连接.但是同伴似乎并没有考虑要建立连接.

With the help of process explorer, I see that one of the client managed to establish a connection to the peer. But the peer doesn't seems to consider the connection to be made.

这是我用Wireshark捕获的内容.为了示例,服务器 S 和客户端 A 在同一台PC上.服务器 S 侦听重定向到该PC的特定端口(8060). B 仍尝试连接正确的IP,因为它看到由 S 发送的 A 的公共地址为localhost,因此使用改为 S 的公共IP. (我已经用占位符代替了公共IP)

Here is what I captured with Wireshark. For the sake of the example, the server S and the client A are on the same PC. The server S listens on a specific port (8060) redirected to that PC. B still tries to connect on the right IP because it sees that the public address of A sent by S is localhost and therefore uses the public IP of S instead. (I have replaced the public IPs by placeholders)

编辑2 :我认为造成混淆的原因是,传入和传出连接请求数据都在同一端口上传输.这似乎弄乱了连接状态,因为我们不知道哪个套接字将从端口获取数据.如果我引用msdn:

EDIT 2: I think the confusion is due to the fact that both incoming and outcoming connection request data are transfered on the same port. Which seems to mess up the connection state because we don't know which socket will get the data from the port. If I quote msdn:

SO_REUSEADDR套接字选项允许套接字强制绑定到 另一个套接字正在使用的端口.第二个套接字使用 optname参数设置为SO_REUSEADDR和optval参数设置 到TRUE的布尔值,然后在与 原始插座. 第二个套接字成功绑定后, 绑定到该端口的所有套接字的行为都是不确定的.

The SO_REUSEADDR socket option allows a socket to forcibly bind to a port in use by another socket. The second socket calls setsockopt with the optname parameter set to SO_REUSEADDR and the optval parameter set to a boolean value of TRUE before calling bind on the same port as the original socket. Once the second socket has successfully bound, the behavior for all sockets bound to that port is indeterminate.

但是TCP孔打孔技术需要在同一个端口上进行通话,以打开孔

But talking on the same port is required by the TCP Hole Punching technique to open up the holes !

推荐答案

开始2个线程:
一个线程尝试使用S
发送的信息连接到B的路由器 另一个线程正在等待与路由器连接的同一端口上的传入连接,该路由器连接到S

A start 2 threads:
One thread tries connecting to B's router with the info sent by S
The other thread is waiting for an incoming connection on the same port used to connect to its router when it connected to S

您不能使用两个线程来执行此操作,因为这只是一个操作.每个建立出站连接的TCP连接都在等待传入连接.您只需调用"connect",就可以发送出站SYN进行连接,并等待入站SYN进行连接.

You can't do this with two threads, since it's just one operation. Every TCP connection that is making an outbound connection is also waiting for an incoming connection. You simply call 'connect', and you are both sending outbound SYNs to make a connection and waiting for inbound SYNs to make a connection.

但是,您可能需要关闭与服务器的连接.当您已经从同一端口建立了连接时,您的平台可能不允许您从该端口建立TCP连接.因此,就像开始TCP打孔一样,请关闭与服务器的连接.将新的TCP套接字绑定到相同的端口,然后调用connect.

You may, however, need to close your connection to the server. Your platform likely doesn't permit you to make a TCP connection from a port when you already have an established connection from that same port. So just as you start TCP hole punching, close the connection to the server. Bind a new TCP socket to that same port, and call connect.

这篇关于TCP打孔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆