Skip to main content
GameDev.net gamedev.net
🔒 Locked

determin my IP

Started by spree Mar 12, 2005 at 10:21 AM 8 replies 2k views
Original Post
spree
spree
Hey all. On winsock2.h , how do i determine my own IP address?
;)
stein
stein
If I remember correctly, gethostname and gethostbyname might be what you're looking for :)
| Stein Nygård - http://steinware.dk |
hplus0603
hplus0603
This should go in the FAQ...

Typically, it's a bad idea to get your own IP address, because it's not certain that the address is visible to people outside your router. Also, many machines have more than one IP address (each interface has one in a machine). Last, not all ISPs or routers have DNS/reverse DNS set up such that gethostname() followed by gethostbyname() will give you the right answer.

To use command-line tools to list your addresses on Windows, use "ipconfig". On Linux, use "ifconfig". On BSD, use "ifconfig" but you probably have to be root.

To programmatically determine the CORRECT address to use, you have to bounce a query off a server that's available on the public internet, and look at what address it returns back. This will work correctly for situations where you're behind NAT (routers/firewalls).

If you want to know the list of all interface addresses, say for selectively binding to only some interface, then you need to use OS-specific system calls; i e you need to do whatever ipconfig/ifconfig does, which involves the network device API on Windows, and walking /proc on Linux.
enum Bool { True, False, FileNotFound };
Daaark
Daaark
http://whatismyip.com/

Write a parser for that html file. :)
spree
spree
Thanks alot guys
;)
markf_gg
markf_gg
There may be cases in an internet game where you want to get the IP addresses of locally bound interfaces - for example if you are on the same LAN as a server for a multiplayer game -- where the NAT you live behind won't properly forward packets back inside the lan to an externally addressable port. The APIs you'd want to use vary from system to system. It took me a while to track down the actual information a while ago, so if anyone else actually needs the info, try:

On Win32, use the IP Helper API (iphlpapi.dll) function GetIpAddrTable
On OS X or BSD, use the getifaddrs/freeifaddrs functions
On Linux it gets a little ugly - the best way I've found is to read out the /proc/net/dev file and then iterate through the device names, doing an ioctl with SIOCGIFADDR to get the bound address.

For actual code on all three platforms, see the function Socket::getInterfaceAddresses in tnl/tnlUDP.cpp in the Torque Network Library. http://www.opentnl.org.
hplus0603
hplus0603
If you're on a LAN and want to find game hosting, you could also look into broadcasting on UDP, or using an open source package like Rendezvous (Apple's open source, not the Net-Z for-pay namesake).
enum Bool { True, False, FileNotFound };
markr
markr
There is a method to get the configured IP addresses using ioctl(SIOCGIFCONF) on an unbound UDP (or TCP) socket.

It varies slightly between platforms (enough that you'll probably have to rewrite it). Here is a Linux example:


Interfaces example


Of course as other posters have already suggested, relying on the IP address(es) returned by this method being visible on the public internet, or the same as those seen by the server is a very bad idea.

In general you should not rely on either of these behaviours and not send the IP address anywhere else either.

Might be useful for LANs though.

Note that http://whatismyip.com/ is also unreliable. It certainly doesn't return my machine's IP address correctly.

This is because my ISP (like many of them) uses a transparent proxy which intercepts the request to whatismyip.com, so it sees its IP address instead.

In the source code for openttd, there is an example which does this on Linux, Win32 and some other OSs,

here
(look at NetworkFindIPs() )


Mark
markf_gg
markf_gg
Quote:
Original post by hplus0603
If you're on a LAN and want to find game hosting, you could also look into broadcasting on UDP, or using an open source package like Rendezvous (Apple's open source, not the Net-Z for-pay namesake).


The only case I can think of where broadcast packets won't work is if your LAN has multiple subnets and broadcast packets are not allowed between them. In this case a third party master server can report to a client the internally determined address of a game server for the client to connect to. The TNL master server operates in this fashion.
bjogio
bjogio
here: http://www.gamedev.net/community/forums/topic.asp?topic_id=299253
there a post with a program written by myself that write your ip in the clipboard
[ILTUOMONDOFUTURO]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.