How to get local IP + what to send for NAT punch through

Started by
1 comment, last by hplus0603 12 years, 5 months ago
Hi!

I am working on a NAT punch through implementation for my engine which is using the ENet library. I am planning on doing the following.

1. A lobby server runs on a publicly reachable IP address and port number.
2. A game client starts up and hosts a game (the game server).
3. The game server connects to the lobby server and notifies it that it is hosting a game
4. The lobby server gets the ip address and port of the game server (which may be different from the game server's local address and port)
5. Another game client starts up and wants to join a game, it connects to the lobby server and gets the game server's information
6. The lobby server notifies both game clients to start bombarding each other with NAT punch through messages
7. When the game server receives a NAT punch through message it sends a few more (just in case) and then stops
8. When the game client who wants to join receives a NAT punch through message sends a few more (just in case) and tries to connect to the game server using ENet
9. The game clients are connected

What happens if both clients are on the same local private network? Is there any risk of the NAT device messing things up? Should I pass the local IP address and port number of each client to the lobby server (and via the lobby server to the other peers), so that the peers can try to do NAT punch through with the local addresses in case the public address punch through fails?

And that leads us to the other question, how do I get the IP address of the current machine most easily? How about machines with multiple network cards? I know about the gethostbyname() function, but that accepts the name of the machine, and it feels a bit clumsy. Is there a better way? I am working on Win7 right now, but a cross-platform solution would be awesome.

Thanks!
Advertisement
Okay I found the answer to the first question myself, from Game Programming Gems 5. I should send both addresses and try with the public first, and if that does not work, try with the internal address. Though, it seems that most modern NAT boxes should handle this without the internal address.

That leaves the second question, how should I get the local IP?

Also, thoughts on the first question are still welcome :)

Okay I found the answer to the first question myself, from Game Programming Gems 5. I should send both addresses and try with the public first, and if that does not work, try with the internal address. Though, it seems that most modern NAT boxes should handle this without the internal address.

That leaves the second question, how should I get the local IP?

Also, thoughts on the first question are still welcome :)


You get the local IP using OS-specific mechanisms. There is no well-defined way to get the address of the local IP interfaces that may be on the machine (note that there may be more than one!)
Which OS/es are you thinking about?
On Windows, you may find the ip helper library useful. For example: http://msdn.microsoft.com/en-us/library/aa366302(v=VS.85).aspx
On Linux, you may want to look at getifaddrs(): http://www.kernel.org/doc/man-pages/online/pages/man3/getifaddrs.3.html
On MacOS X, getifaddrs() may also work.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement