Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
motmot80 opened this issue Oct 3, 2023 · 3 comments
Open

Comments

@motmot80
Copy link

motmot80 commented Oct 3, 2023

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
Copy link
Author

motmot80 commented Oct 3, 2023

F.e access to

KcpClient.socket.LocalEndpoint

@motmot80
Copy link
Author

motmot80 commented Oct 3, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants