0

I am trying to experiment with UDP hole punching to try to connect 2 computers. The 2 computers I am trying to connect are on 2 different Wifi created by the same router. Let me call these computers C1 and C2. So I want to create a connection between C1 and C2. I have 2 public server IPs available where I run a UDP listener server. These servers are S1 and S2.

Experiment 1:

  1. Send a UDP packet from C1 to S1 and print the IP and port of the incoming packet. I get IP_C1 and PORT_C1.
  2. Send a message back to C1 from S1 using IP_C1 and PORT_C1. This works fine as expected and it remains connected. So in a way I have already punched a hole for S1 to communicate directly with C1.

Experiment 2:

  1. Send a UDP packet from C1 to S1 and get IP_C1 and PORT_C1.
  2. Send a UDP packet from C2 to S1 and get IP_C2 and PORT_C2.
  3. Use S1 to send a message back to C1 using IP_C1 and PORT_C1. This works as before.
  4. Use S1 to send a message back to C2 using IP_C2 and PORT_C2. This also works fine.

So now both C1 and C2 are accessible from S1. NOTE: What I see is that IP_C1 and IP_C2 are the same so lets call them IP_C. Ports however are different.

Continuing this I tried sending a message from C1 to C2 using IP_C and PORT_C2 as the destination. C2 did not receive the message as expected I then send a message from C2 to C1 using IP_C and PORT_C1. This is where I start getting stuck: I expected that now the message of C2 will reach C1 since C1 already punched a hole in the NAT going to C2. But C1 does not receive the message. Continuing if I now send another message from C1 and C2. C2 does not get it. But even now if I send a message from S1 to C1 or C2 it works.

Experiment 3:

To check whether the IP and port are maintained when the client sends another UDP packet I did the following:

  1. Send a UDP packet from C1 to S1 and get IP_C1 and PORT_C1.
  2. Send a UDP packet from C2 to S1 and get IP_C2 and PORT_C2.
  3. Send a UDP packet from C1 to S2 and get IP_C1_S2 and PORT_C1_S2.
  4. Send a UDP packet from C2 to S2 and get IP_C2_S2 and PORT_C2_S2.

Like S1, S2 also easily connects and I see IP_C1=IP_C1_S2=IP_C2=IP_C2_S2=IP_C and PORT_C1=PORT_C1_S1 and PORT_C2=PORT_C2_S2.

So the ports are maintained.

Even with this scenario I am not able to establish communication between C1 and C2. I am using luasocket library to send udp packets and do the experiments. If anyone can help guide me as to what other experiments I can do to do this successfully it would be much appreciated.

Thanks

Milind
  • 415
  • 8
  • 24
  • The address IP_C is the "external" IP of the NAT, it does not have meaning inside the NAT. To connect C1 to C2 you should use "inner" IP of C2, because there is no route from C1 to C2 which exits NAT and then returns back. – Egor Skriptunoff Oct 06 '19 at 00:37
  • So C1 and C2 are on different networks so there is no direct internal IP visible to the other to connect them directly. – Milind Oct 06 '19 at 01:48
  • The problem is neither Lua nor luasocket specific. Try to ask this question on https://serverfault.com/ – Egor Skriptunoff Oct 06 '19 at 07:27
  • Yes I assumed that its not a problem with luasocket. Just added that for completeness. Thank you for directing me to serverfault. I have posted it there. – Milind Oct 07 '19 at 16:58

0 Answers0