Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with NAT punching

Discussion in 'Multiplayer' started by Martho42, Feb 24, 2019.

  1. Martho42

    Martho42

    Joined:
    Oct 1, 2015
    Posts:
    14
    Hey y'all. I'm making an online FPS. I'm using plain UNET right now, and it seems to be working well enough. I may switch to Mirror combined with Ignorance transport if UNET starts giving me trouble. Anyway, I've spent the past week or so researching NAT punching, STUN, etc. Unfortunately UPNP doesn't work on my college housing internet because my router is sitting behind multiple switches, so I've got to get NAT punch working. I downloaded and compiled stuntman STUN client and server to get started. When I connect to a public STUN server with stuntman client, it shows that my external mapping is using the same port as my internal mapping. I supposed this ought to be pretty easy then. My basic (probably flawed) methodology for a simple connection test is:

    Computer 1: Host game, then using the LLAPI send out UDP packets to a random IP address on the internet using the same port that I'm hosting on. The idea is that the packets will open a hole in my external network on the same port since stuntman has shown the port # stays the same.

    Computer 2: Connect to a VPN (so it's like this computer is on a different network), type in the external IP of computer 1, and click join.

    As you might have guessed, it didn't work. I don't know if my methodology is just fundamentally flawed, if I'm not sending packets correctly with the LLAPI, or if there's any number of other things I'm doing wrong. I have been able to join a game successfully through the VPN I'm using with the host on a properly port forwarded network, so I don't think that's the problem. I also tried connecting both computers to different VPN servers to test if my network is the problem. For the hosting and joining, I'm just using the NetworkManagerHUD buttons with the port set to 49000. Here's my code for sending packets with the LLAPI:

    Code (CSharp):
    1.      public void SendHoleData()
    2.      {
    3.          if (!isServer)
    4.             return;
    5.  
    6.         Debug.Log("Sending data");
    7.  
    8.          byte[] payload = new byte[1024];
    9.  
    10.         UnityWebRequest wr = new UnityWebRequest("http://172.217.5.206:49000"); //Google's IP
    11.    
    12.         UploadHandler uploader = new UploadHandlerRaw(payload);
    13.  
    14.         // Sends header: "Content-Type: custom/content-type";
    15.         uploader.contentType = "custom/content-type";
    16.  
    17.         wr.uploadHandler = uploader;
    18.  
    19.         wr.SendWebRequest();
    20.      }
    I have this function running on Invoke repeating after hosting starts. It repeats every 0.2 seconds. I'm not sure if just sticking the port on the end of the URL is the right way to specify port or not, I couldn't find any info on that. I'd very much appreciate help getting anything working. Thanks!
     
    JesOb likes this.
  2. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    - Making a random web request to Google's servers on a specific port, probably, do not achieve a NAT punchthrough. My recommendation is to talk to your IT Admin at the University, describe your situation, it could be a great learning experience and they'll be excited to help you.
    - For a variety of reasons, merely connecting a computer on the same network via a VPN, on Windows, will be totally unpredictable and not necessarily actually put the computer on a different network. It's hard to know because I'm not a Windows user, and Windows' networking behaviour is kind of complicated.

    According to their "Hamachi is free for up to 5 computers in your network." Hamachi is a VPN service that's basically purpose-built for Windows gamers. If and when you want to expose your game to be played by the public, you will have to run a centrally hosted server anyway!
     
  3. Martho42

    Martho42

    Joined:
    Oct 1, 2015
    Posts:
    14
    I figured this was likely the case, though I don't know exactly why it wouldn't work given the premise of STUN. It would be pretty helpful if anyone is able to explain that to me.

    Unfortunately my student housing isn't owned or managed by the University. I doubt anyone at the apartment office knows anything more about networking than I do haha. I'm sure they hired people to install everything. Looking for someone at my university who knows about hole punching is a good idea though.

    I'm using Linux, and my VPN is Private Internet Access, which is made to redirect all internet traffic. I'm fairly certain that the VPN is working how I expect it to. I've also tethered my second computer to my phone to get it on a different network and had the same results; it works fine connecting to a properly port forwarded host, but not with my punch through attempt.

    I do have access to a digital ocean VPS to run a matchmaking server on. I could use hamachi for testing purposes right now, but long term I think it would be a lot better to get punch through working.

    I think I'm going to attempt to get a basic non-Unity program working with PJNATH, and then see if I can integrate the solution into Unity.
     
  4. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    If you want to save yourself the headache maybe look in to the NAT Traversal asset.
     
  5. Martho42

    Martho42

    Joined:
    Oct 1, 2015
    Posts:
    14
    I have been highly tempted to purchase your asset. However, it would not work with mirror, correct? I have had some trouble with UNET, though not enough to make me switch yet. If I do switch to mirror however, I'd hate to have paid for the NAT traversal asset and then not be able to use it.
     
  6. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    NAT Traversal unfortunately does not support Mirror, and there are no plans on adding Mirror support. Keep a look out for my next asset which is like improved NAT Traversal + relays and it will work with Mirror (shortly after release sometime at least).
     
    Joe-Censored likes this.
  7. Martho42

    Martho42

    Joined:
    Oct 1, 2015
    Posts:
    14
    I would definitely be interested if it worked with Mirror + Ignorance UDP transport. Any chance of free/discounted upgrade to the new asset for those who have purchased NAT Traversal? Completely understandable if not. Just means I'll have to wait.
     
  8. DJ_Design

    DJ_Design

    Joined:
    Mar 14, 2013
    Posts:
    124
    Any immediate plans on that?
     
  9. FakeByte

    FakeByte

    Joined:
    Dec 8, 2015
    Posts:
    147
    It's in his signature
     
    DJ_Design likes this.