(LOW LEVEL API) Getting the current machine's public IP and forwarding port

Greetings !

I am currently working on a multiplayer game where all players connect to a central Master Server which, among other things, references all currently open matches it knows about so that players can join matches easily and hosted by people they don’t have a way to communicate with.

I have two problems.

  1. One of these is that I only have one computer, meaning I have to test the Master Server and the client on the same machine, meaning the data Unity keeps about connection IP is local network IP, or even 127.0.0.1 if I connect with that IP from the client. It makes sense of course, no point looking for the public IP when you can have the machine’s IP on your network directly. But this means the IP that is shown to other players who are not on the same network is the local IP relative to the Master Server, meaning it won’t work for them.

I figured being able to get the local machine’s public IP and sending it to the Master Server separately no matter what could be useful. Problem is, I’m not sure how I could achieve the “get the local machine’s public IP” part reliably. Of course that problem alone would only apply for players logging from the same machine the Master Server is hosted on, but it would sure be annoying for all the tests I have to run.

  1. The other problem I have is NAT punching. The Master Server will have its NAT configured properly of course, but since the matches themselves work on a peer to peer model, I’d need everyone wishing to host a match to open the right port on their router, which is probably way too much to ask from the average player.

As such, I’m looking for a way to build a NAT punch through algorithm within Unity. I’ve googled it of course, and found interesting information, but nothing that really helped me fully understand or solve this problem.

Now, what I do understand is that public IP is the router’s adress, not the computer’s, meaning the router need to know where to redirect information coming from a given port, and that’s what NAT is for. Knowing that, I figured I could try to cobble something where clients would send their public AND local network IP to the Master Server no matter what, so then when a connection needs to be established there would be a way to “tell” the router at the public IP adress which machine we’d like to be connected to.

Thing is, as much as I don’t have a clue how to get a machine’s public IP reliably, I also don’t know how to get a machine’s local IP reliably, and even if I could I don’t think there would be a way to put it to use with Unity’s API.

If you guys need more detail on what I know, what I need, what I already have… just ask, I’ll be keeping an eye on this thread all day every day, since it’s critical to my current student project.

Thanks !

PS : I’ve seen a few things like this NAT Traversal - Automatic port forwarding, punch-through, and more! - Community Showcases - Unity Discussions but it’s all Unet, which is the high level API we’re not using. We might be shooting ourselves in the foot on some aspects but since it’s a student project we figured the most educative way would be the best way, even if it takes slightly more time to fully comprehend.

there are 3 answers so far

  1. No, we do not support such service for now
  2. We are going to introduce nat punch solution, where we can expose this service too, but eta has not defined yet (it is ready for alpha (imho) but we still have difficulties with other problem, and just afraid that we will have not time to support this project right now which could be bad for customer experience) :frowning:
    ====
  3. You can try to it by yourself, you will need amazon server, server.dll (ask me privately about this) and function GetConnectionInfo() exposed by llapi. When client connected to server deployed on amazon, you can use this function to grab client public ip address…

I’m sorry for those answers :frowning:

Don’t fret, it’s not even a professional project. We’ll just make people open their NAT and when needed and host matchs on other machines than the one the master server is on :wink: Or maybe we’ll just move to a dedicated server architecture, we’re making it so it wouldn’t be too difficult.
I guess it’s a good reason for me to get into .dll stuff and go even lower in level ! :stuck_out_tongue:

However, I am not sure I understand what amazon server, server.dll… are. I’m already using GetConnectionInfo to get the IP of a connection so I’m familiar with that one. Sorry, it might be a newbie question.

Ok, to get public ip address you need the “instance” deployed somewhere out of your local network, correct? You can use amazon cloud (or azure or anything else) for this, correct? It is what I meant when said amazon :slight_smile:

Then, to set up communication between you clients and this server you need some sort of software installed, correct? This software should be able to talk with your clients, you can for example set up unity instance on this server (running game) which people should connect for receiving public ip address, correct? For that goal you can use “server.dll” stuff - just dll which can be used with any .net application on win platform for talking using unet protocol.

Shortly speaking the picture is: Server in amzon cloud run your service, each game client connect to this server, serer obtain its ip using GetConnectionInfo() and report them back to client.

I might have found a solution but can’t get it to work for some reason…

I read that when a computer hidden behind a NAT initiates a connection with some other machine outside the local network, the router opens a port to that computer automatically, so that whoever the computer is connecting to can answer. I tested it, and it is also the case in Unity.

My idea was to use the Master Server as a “meeting area” for clients, for them to know one another’s opened Port upon connecting to the Master Server, and then use that Port along with their public IP to initiate a Client to Client connection. Unfortunately, it doesn’t seem to connect, but I tested it all on my local network so that might be the reason. Regardless, am I on a path that might lead anywhere ?

Sorry, nat punching is the long story for the forum + inet is full of different references which you can use… So… I do not have any special recommendation here :slight_smile:

@Hoshiqua

The NAT Traversal plugin actually works with the LLAPI and even other non-unet networking systems. There is often confusion because when it was initially released it did only work with the HLAPI, but those days are long past. I’m fairly positive the plugin is exactly what you’re looking for: NAT Traversal | Network | Unity Asset Store

Cheers