MirrorNetworking / kcp2k

MIT License
283 stars 50 forks source link

Feature request: Add access to client udp port (UDP hole punching) #51

Open motmot80 opened 1 year ago

motmot80 commented 1 year ago

Description

I need to know the client udp port which is randomly assigned to the socket, so I can use it to punch a UDP hole from the server to the client (f.e. Full-cone NAT).

SERVER:7777 =UDP-PUNCH=> CLIENT:<RANDOM>

Without access to the udp client port (socket port) I cannot punch these holes during the connection phase.

Feature request

Public method to access the assigned client udp port.

You did great work!

Thanks in advance and best regards, Thomas

motmot80 commented 1 year ago

F.e access to

KcpClient.socket.LocalEndpoint

motmot80 commented 1 year ago

After patching this to get the client port I also found out, that there's no way to set custom socket options for the server side. I had to patch CreateServerSocket to make it possible to send udp hole punching events from the same server port to the clients while the server ist already bound to the socket (running/listening):

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

Now udp-hole-punching works like a charm.

  1. server is behind nat, but connecting to a central punch-relay-server
  2. client likes to connect to server behind nat firewall
  3. client sends punch request (client-pub-ip + kcp-client-port*) over the punch-relay-server
  4. server receives udp hole punch requests and send a udp packet from himself** to the client (server-port => client-pub-ip + kcp-client-port)
* KCPTransport.client.socket.LocalEndPoint.Port
** KCPTransport.server.socket.SetSocketOption
mightypanda commented 1 year ago

Hello, Thank you for this feature request, I am interested in UDP hole punching too. Do you use your own relay server or an existing service? Best