RevenantX / LiteNetLib

Lite reliable UDP library for Mono and .NET
https://revenantx.github.io/LiteNetLib/index.html
MIT License
3.08k stars 497 forks source link

NAT Punch for Players Behind CGNAT #384

Closed Jerry0526 closed 4 years ago

Jerry0526 commented 4 years ago

Hello,

We have been using this library for our P2P game, including a NAT Punch server. It works perfectly for the most part, but we have been receiving some issues where players cannot punch through each other. From some of the players' descriptions/logs, it looks like they are behind CGNAT (not because of symmetric NAT, that part we made some tests).

I searched about NAT Punch through CGNAT. Some say it just can't possibly work, but without explaining why. If this really is the case, is there any chance we can detect player behind CGNAT with the API? (So far what I saw is player need to login to their router and check whether their router's WAN address is the same as their IP reported from a public website)

If punch could theoretically work with CGNAT, could this line in NatPunchModule.cs line 216 be related?

_socket.Ttl = 2;

I'm not exactly sure of the actual reason of sending a short-lived packet. Found something in http://www.uni-kassel.de/eecs/fileadmin/datas/fb16/Fachgebiete/UC/papers/HWW_11-SYNI.pdf (though it's talking about TCP punch) saying something related to ICMP Destination Unreachable. But that paper also mentioned that "In case of multi-level NAT with different routes the low TTL needs to be determined for each destination individually before the hole punching attempt begins. " So I'm wondering whether it may need a greater value for player behind CGNAT (essentially double NAT)?

Thank you very much!

Library version: 0.9

Framework: Unity

OS: Windows

RevenantX commented 4 years ago

TTL 2 is used for some routers that will block port if message reaches other side with closed port. TTL is restored after this one message. Also there is some improvements in master branch to NAT punching. Try latest master branch

Jerry0526 commented 4 years ago

Hi yeah I adopted the NAT punching improvements already - they were indeed necessary, before the fix punching packets often had issue reaching the other side.

My question is about whether TTL=2 is enough when player is in a double NAT (Carrier-Grade NAT), since it's one extra router before packet goes "out".