lsalzman / enet

ENet reliable UDP networking library
MIT License
2.77k stars 673 forks source link

Add ENET_SOCKOPT_TTL #217

Closed skyfloogle closed 1 year ago

skyfloogle commented 1 year ago

The currently popular methods for NAT traversal involve having two clients send each other a UDP packet. This adds an entry to their NAT, linking their endpoint to the other client, which means the other client can reply, allowing communication. However, this doesn't work for everyone: some NATs, upon receiving an unsolicited packet, will add an entry linking that packet's source to the NAT itself, which completely blocks communication. The solution is to ensure the first packet does not reach the other side, by giving it an appropriate TTL value.

This PR adds ENET_SOCKOPT_TTL, allowing for setting/getting a socket's TTL value and making this technique possible. I wrote a simple C program using this PR that tests whether a given TTL value is high enough to get past the NAT.

This technique doesn't appear to be very widely known, but I can confirm its usefulness. Additional information and resources are noted at https://github.com/dolphin-emu/dolphin/pull/11382.