This Question is almost the same as the previously asked Get the IP Address of local computer-Question. However I need to find the IP address(es) of a Linux Machine.
So: How do I - programmatically in C++ - detect the IP addresses of the linux server my application is running on. The servers will have at least two IP addresses and I need a specific one (the one in a given network (the public one)).
I'm sure there is a simple function to do that - but where?
To make things a bit clearer:
- The server will obviously have the "localhost": 127.0.0.1
- The server will have an internal (management) IP address: 172.16.x.x
- The server will have an external (public) IP address: 80.190.x.x
I need to find the external IP address to bind my application to it. Obviously I can also bind to INADDR_ANY (and actually that's what I do at the moment). I would prefer to detect the public address, though.
Don't hard code it: this is the sort of thing that can change. Many programs figure out what to bind to by reading in a config file, and doing whatever that says. This way, should your program sometime in the future need to bind to something that's not a public IP, it can do so.
I do not think there is a definitive right answer to your question. Instead there is a big bundle of ways how to get close to what you wish. Hence I will provide some hints how to get it done.
If a machine has more than 2 interfaces (
lo
counts as one) you will have problems to autodetect the right interface easily. Here are some recipes on how to do it.The problem, for example, is if hosts are in a DMZ behind a NAT firewall which changes the public IP into some private IP and forwards the requests. Your machine may have 10 interfaces, but only one corresponds to the public one.
Even autodetection does not work in case you are on double-NAT, where your firewall even translates the source-IP into something completely different. So you cannot even be sure, that the default route leads to your interface with a public interface.
Detect it via the default route
Something like
ip r get 1.1.1.1
usually tells you the interface which has the default route.If you want to recreate this in your favourite scripting/programming language, use
strace ip r get 1.1.1.1
and follow the yellow brick road.Set it with
/etc/hosts
You can create an entry in
/etc/hosts
likeThen you can use this alias
publicinterfaceip
to refer to your public interface.Use the environment
Same as
/etc/hosts
. but use the environment for this. You can try/etc/profile
or~/.profile
for this.Hence if your program needs a variable
MYPUBLICIP
then you can include code like (this is C, feel free to create C++ from it):So you can call your script/program
/path/to/your/script
like thisMYPUBLICIP=80.190.1.3 /path/to/your/script
this even works in
crontab
.Enumerate all interfaces and eliminate those you do not want
If you do know what you do not want, you can enumerate all interfaces and ignore all the false ones.
Here already seems to be an answer https://stackoverflow.com/a/265978/490291 for this approach.
Do it like DLNA
You can try to enumerate all the UPnP gateways on your network and this way find out a proper route for some "external" thing. This even might be on a route where your default route does not point to.
For more on this perhaps see https://en.wikipedia.org/wiki/Internet_Gateway_Device_Protocol
This gives you a good impression which one is your real public interface, even if your default route points elsewhere.
There are even more
IPv6 routers advertise themselves to give you the right IPv6 prefix. Looking at the prefix gives you a hint about if it has some internal IP or a global one.
You can listen for IGMP or IBGP frames to find out some suitable gateway.
There are less than 2^32 IP addresses. Hence it does not take long on a LAN to just ping them all. This gives you a statistical hint on where the majority of the Internet is located from your point of view. However you should be a bit more sensible than the famous https://de.wikipedia.org/wiki/SQL_Slammer
ICMP and even ARP are good sources for network sideband information. It might help you out as well.
You can use Ethernet Broadcast address to contact to all your network infrastructure devices which often will help out, like DHCP (even DHCPv6) and so on.
This additional list is probably endless and always incomplete, because every manufacturer of network devices is busily inventing new security holes on how to auto-detect their own devices. Which often helps a lot on how to detect some public interface where there shouln't be one.
'Nuff said. Out.
You can do some integration with curl as something as easy as:
curl www.whatismyip.org
from the shell will get you your global ip. You're kind of reliant on some external server, but you will always be if you're behind a NAT.An elegant scripted solution for Linux can be found at: http://www.damnsmalllinux.org/f/topic-3-23-17031-0.html
This has the advantage of working on many flavors of unix ...and you can modify it trivially to work on any o/s. All of the solutions above give me compiler errors depending on the phase of the moon. The moment there's a good POSIX way to do it... don't use this (at the time this was written, that wasn't the case).
I found the ioctl solution problematic on os x (which is POSIX compliant so should be similiar to linux). However getifaddress() will let you do the same thing easily, it works fine for me on os x 10.5 and should be the same below.
I've done a quick example below which will print all of the machine's IPv4 address, (you should also check the getifaddrs was successful ie returns 0).
I've updated it show IPv6 addresses too.