How do I find my own IP address?

Joachim Durchholz <>
Wed Jan 21 18:15:00 CET 2004


Joe Armstrong wrote:

> I need to know my own IP address

IP addresses are associated with network cards, not with machines.

For example, 127.0.0.1 is not an alias for your host, it's a quite 
separate virtual network card that happens to have just a single node on 
its virtual network, namely the host itself.
Actually that's a feature, not a bug: port listening code can specify 
whether it will listen on any port, or just on specific ports. If you 
have an insecure protocol and trust your machine but not other machines 
on the network, it's safe to configure it to just listen on 127.0.0.1 
but not on any real network cards.

Note also that your Unix host name is not necessarily related to your 
Internet host name; the former is what's displayed on the shell prompt, 
the latter is how the machine is addressed from the DNS. These can be 
related (and usually are), but they don't have to.
Things get really lively if you are doing virtual hosts. For example, I 
have rented a host with root access, and asked to have it configured 
with several domain names. It turned out that it was configured with all 
the domain names that I had asked for (durchholz.org, nutshell-games.de, 
nutshell-games.com, and a few others), but its host name was p15138084, 
and it also had an additional DNS name of p15138084.pureserver.info. 
And, best of all: I noticed no problems with that set-up.

I haven't explored the ramifications in detail, but I suspect that 
what's called the "Unix hostname" is logically unrelated to the Internet 
name (aka Fully Qualified Domain Name). The only use of the hostname 
that I have been able to identify was that it's the name that's used if 
you configure the shell prompt to display the host name...

In hindsight, this is not a real surprise: DNS just maps names to IP 
addresses, without ever contacting the host that it's supposed to manage 
names/addresses for. And whatever the host itself thinks, the canonical 
authoritative sources for DNS information are the DNS servers :-)

> The undocumented function inet:getif() produces
> 
> 4> inet:getif().
> {ok,[{{193,10,65,254},{193,10,67,255},{255,255,252,0}},
>      {{127,0,0,1},{127,255,255,255},{255,0,0,0}}]}
> 
> The first element of this list appears to be my IP address - 

This seems to be a list of all IP addresses that your host is willing to 
respond do.

> can I reply on this????

If you reply on an incoming IP connection, simply use that IP 
connection. If the IP connection might close on you, save the IP address 
of the originator of the request (the IP connection object should carry 
that information somewhere).
If you initiate a connection, let the caller (ultimately: the end user 
and/or some environment variable) decide where it should go. If it's an 
IP address, just specify that and let the OS's kernel routing tables 
worry about what outgoing IP interface to use. If it's a DNS name, look 
up its IP address and continue from there. The originator IP address on 
the outgoing packets should automatically be set by your IP stack.

HTH.
It might help if you said what you're trying to do - it's unlikely that 
you need to enumerate your network interfaces unless you're doing 
network administration tools :-)

Regards,
Jo
--
Currently looking for a new job.




More information about the erlang-questions mailing list