zhboner / realm

A network relay tool
MIT License
1.54k stars 285 forks source link

Problem with UDP #26

Closed zephyrchien closed 3 years ago

zephyrchien commented 3 years ago

As has been mentioned in #21, sometimes UDP forwarding does not work properly.

https://github.com/zhboner/realm/blob/3243f54e045aef77f5db028fbbd1cfe5f38a88f9/src/relay.rs#L115-L131

When sending a UDP packet, we need to specify its destination. According to L115-L131, sender_vec stores the SockAddr from a incoming packet(usually from a client) one by one, then consumes them when the response arrives.

However, this would work only under some ideal conditions, where there is only one client sending, and the number of response packets should be the same. Otherwise, the program would drop some of the response packets, or incorrectly send them to another peer, or have the SockAddr left in sender_vec...

Instead of counting the incoming SockAddr one by one, I think we should record the associated pairs of incoming SockAddr and remote SockAddr for a period of time. To deal with multiple clients, maybe we need to allocate a unique local SockAddr for each incoming Sockaddr, and also have it recorded. Finally, the UDP part would work somewhat like a NAT device.

zephyrchien commented 3 years ago

This will bring extra complexity and costs. I think add these codes to a new module would be a better choice. And we should consider to make UDP forwarding optional, since not everyone needs it.