How do I find my own IP address?

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Joe Williams-2

I need to know my own IP address

I can find my hostname

1> inet:gethostname().
{ok,"enfield"}

And do this

2> inet:gethostbyname("enfield").
 
{ok,{hostent,"localhost.localdomain",
             ["localhost","enfield"],
             inet,
             4,
             [{127,0,0,1}]}}

But 127.0.0.1 is not my IP address

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 -
can I reply on this????

/Joe



Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Bengt Kleberg-4
Joe Armstrong wrote:

> I need to know my own IP address
>
> I can find my hostname
>
> 1> inet:gethostname().
> {ok,"enfield"}
>
> And do this
>
> 2> inet:gethostbyname("enfield").
>  
> {ok,{hostent,"localhost.localdomain",
>              ["localhost","enfield"],
>              inet,
>              4,
>              [{127,0,0,1}]}}
>
> But 127.0.0.1 is not my IP address
>

i tried the functions and got the expected results:

inet:gethostname().
{ok,"wsb017"}
2> inet:gethostbyname(
"wsb017"
).
{ok,{hostent,"wsb017",["loghost"],inet,4,[{134,138,232,116}]}}

before using undocumented functions i would suggest a close look at the
configuration of your computer. the #hostent{} claims your canonical
name is "localhost.localdomain". this does not sound correct.
what does your /etc/hosts say?


bengt


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Bengt Kleberg-4
In reply to this post by Joe Williams-2
Joe Armstrong wrote:

> I need to know my own IP address
>
> I can find my hostname
>
> 1> inet:gethostname().
> {ok,"enfield"}
>
> And do this
>
> 2> inet:gethostbyname("enfield").
>  
> {ok,{hostent,"localhost.localdomain",
>              ["localhost","enfield"],
>              inet,
>              4,
>              [{127,0,0,1}]}}
>
> But 127.0.0.1 is not my IP address

if you are interested i have written a c program to do some of the
things above (this is what i suppose the inet module is/does).
if you run it, what kind of information does it print?


bengt

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>






main(int argc, const char* argv[])
{
        char name[80];
        struct hostent *hp;
        char **p;

        if (0 != gethostname(name, sizeof (name))) {
                (void) printf("gethostname failed\n");
                exit (1);
        }

        hp = gethostbyname(name);
        if (hp == NULL) {
                (void) printf("host information for %s not found\n", name);
                exit (3);
        }

        for (p = hp->h_addr_list; *p != 0; p++) {
                struct in_addr in;
                char **q;

                (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
                (void)printf("%s\t%s", inet_ntoa(in), hp->h_name);

                for (q = hp->h_aliases; *q != 0; q++) {
                        (void) printf(" %s", *q);
                }

                (void) putchar('\n');
        }

        exit (0);
}


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Luke Gorrie-3
In reply to this post by Joe Williams-2
Joe Armstrong <joe> writes:

> I need to know my own IP address

For what purpose? There's no simple answer because you don't
necessarily just have one IP address, and there are different ways to
find the right IP address for different purposes.

If you want to tell someone else your IP address and you already know
theirs, it's probably best to just connect to them and let them see
what address you used. If they have a socket connected to you then
they can get your address from inet:peername/2.

Alternatively you could ask the OS "what address should I use to talk
with <other ip>?" - I'm not sure if you can do that portably, but on a
modern Linux box you can do a manual lookup like:

  $ ip route get 192.168.128.1
  192.168.128.1 dev eth0  src 192.168.128.44
      cache  mtu 1500 advmss 1460

which says I can use source address 192.168.128.44 to talk to
192.168.128.1.

Depending on what you're doing you might also want to be careful about
NAT. Maybe the address your computer thinks you have is not the same
one other people will see.

Cheers,
Luke (confident that Per will correct all my errors :-)



Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Joachim Durchholz
In reply to this post by Joe Williams-2
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.



Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Pascal Brisset
In reply to this post by Luke Gorrie-3
Luke Gorrie writes:
 > Depending on what you're doing you might also want to be careful about
 > NAT. Maybe the address your computer thinks you have is not the same
 > one other people will see.

Be careful about firewalls as well.  Please do not rewrite the
Erlang distribution protocol along the lines of (active) FTP !


By the way, regarding IP addresses: Is there a simple way to
connect Erlang nodes with multiple IP interfaces ?

With R9C-0 on a RedHat 9, inet_hosts:gethostbyname/1 ignores
"multi on" in /etc/host.conf, inet_gethost_native:gethostbyname/1
always returns addresses in the same order, and inet_tcp_dist
always uses to the first address.
So it looks like the only way is to use a round-robin DNS server.
(Or implement network redundancy with ethernet bonding instead.)

-- Pascal



Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Joe Williams-2
In reply to this post by Luke Gorrie-3
On Wed, 21 Jan 2004, Luke Gorrie wrote:

> Joe Armstrong <joe> writes:
>
> > I need to know my own IP address
>
> For what purpose? There's no simple answer because you don't
> necessarily just have one IP address, and there are different ways to
> find the right IP address for different purposes.

Because this is sometimes used as a key in the .Xauthority file

My problem is to write  bomb proof code to open a connection to the local
X-server - this should try to handle cases where DISPLAY etc is incorrect

/Joe

>
> If you want to tell someone else your IP address and you already know
> theirs, it's probably best to just connect to them and let them see
> what address you used. If they have a socket connected to you then
> they can get your address from inet:peername/2.
>
> Alternatively you could ask the OS "what address should I use to talk
> with <other ip>?" - I'm not sure if you can do that portably, but on a
> modern Linux box you can do a manual lookup like:
>
>   $ ip route get 192.168.128.1
>   192.168.128.1 dev eth0  src 192.168.128.44
>       cache  mtu 1500 advmss 1460
>
> which says I can use source address 192.168.128.44 to talk to
> 192.168.128.1.
>
> Depending on what you're doing you might also want to be careful about
> NAT. Maybe the address your computer thinks you have is not the same
> one other people will see.
>
> Cheers,
> Luke (confident that Per will correct all my errors :-)
>





Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Shawn Pearce
Joe, given you are just looking in the xauth file, you can change the
code to use the undocumented function you found which returns all
IP addresses on this machine, and then test each in the xauth file.
But connect to the X server using 127.0.0.1 if one of those matched.

In general though you definately should not attempt to connect to
the X server through any of those IP addresses, etc.

I hate systems that use a local IP address in a .Xauthority file.

Joe Armstrong <joe> wrote:

> On Wed, 21 Jan 2004, Luke Gorrie wrote:
>
> > Joe Armstrong <joe> writes:
> >
> > > I need to know my own IP address
> >
> > For what purpose? There's no simple answer because you don't
> > necessarily just have one IP address, and there are different ways to
> > find the right IP address for different purposes.
>
> Because this is sometimes used as a key in the .Xauthority file
>
> My problem is to write  bomb proof code to open a connection to the local
> X-server - this should try to handle cases where DISPLAY etc is incorrect
>
> /Joe

--
Shawn.

  For good, return good.
  For evil, return justice.


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Samuel Rivas-3
In reply to this post by Joe Williams-2
Joe Armstrong wrote:

>
> I need to know my own IP address
>
> 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 -
> can I reply on this????

Hi:

I dealt with the same problem in a host which:
  * Does not have a public name in the network
  * Gets its IP with DHCP
  * Needs to show that IP on a display (to be somehow reachable)

Following your discovery I've found out an (undocumented) way to get the
host IP within Erlang:

1> {ok, S} = inet_udp:open(0, []).
{ok,#Port<0.4112>}
2>prim_inet:ifget(S, "eth0", [addr]).
{ok,[{addr,{192,168,100,100}}]}

You can also know the broadcast and netmask if you write
[addr,broadaddr,netmask] instead of [addr].

However, as long as it is not a standard way to do this kind of things I
use a more triky solution in my app: I parse the output of
/sbin/ifconfig eht0.

Would be nice if it were possible to do that in cleaner way in future
OTP releases.

>
> /Joe
>

Regards

--
 ---------------------
| Samuel        |
 ---------------------


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Joachim Durchholz
Samuel Rivas wrote:
> Following your discovery I've found out an (undocumented) way to get the
> host IP within Erlang:
>
> 1> {ok, S} = inet_udp:open(0, []).
> {ok,#Port<0.4112>}
> 2>prim_inet:ifget(S, "eth0", [addr]).
> {ok,[{addr,{192,168,100,100}}]}

This will work on a vanilla Unix system.
It will not work, for example, if you use FreeS/WAN (where the network
device to use would be ipsec0 for an encrypted connection, and eth0 for
an unencrypted one). There may be other software that installs virtual
network cards ("interfaces"). I'm not sure, but I suspect that even an
ipsec interface might have a regular IP address, including DNS entry,
routing table, and all.
It will also not properly work if the machine has two network
interfaces. For example, I'm sitting on a desktop system that has a
network card (for the LAN) and an ISDN adapter that pretends to be a
network card. If I were running Unix, I'd have an eth0 and an eth1 card,
one of them with a DHCP-generated IP, the other with a local masqueraded
192.168.x.x address. There's a third local network that's entirely
virtual, for the virtual network that serves the VMWare machines. I
think this kind of setup isn't too unusal for telecommuters (well,
except for the VMWare bit maybe... *g*)
Oh, and I'm running Windows, which means that my machine will respond
with utter ignorance if you try names like /dev/eth0 on it :-)

... and, no, I'm not picking nits here. FreeS/WAN is an important use
case (particularly for X sessions over unsecured connections), and both
X servers and clients do run under Windows. It's OK to rely on eth0 and
other Unix specifica if you know that your application is going to run
on machines that you control, but that's not a viable option for
general-purpose libraries.
IMNSHO ;-P

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



Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Chris Pressey
On Thu, 22 Jan 2004 15:07:14 +0100
Joachim Durchholz <joachim.durchholz> wrote:

> Samuel Rivas wrote:
> > Following your discovery I've found out an (undocumented) way to get
> > the host IP within Erlang:
> >
> > 1> {ok, S} = inet_udp:open(0, []).
> > {ok,#Port<0.4112>}
> > 2>prim_inet:ifget(S, "eth0", [addr]).
> > {ok,[{addr,{192,168,100,100}}]}
>
> This will work on a vanilla Unix system.

Not even there (depending on your interpretation of 'vanilla Unix') -
the BSD's have not traditionally had interfaces named eth*.  They're
named after the specific devices (ed*, rl*, dc*, etc) instead.

-Chris


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Miguel Barreiro Paz-2

> > > 2>prim_inet:ifget(S, "eth0", [addr]).
> > > {ok,[{addr,{192,168,100,100}}]}
> >
> > This will work on a vanilla Unix system.
>
> Not even there (depending on your interpretation of 'vanilla Unix') -
> the BSD's have not traditionally had interfaces named eth*.  They're
> named after the specific devices (ed*, rl*, dc*, etc) instead.

Please. We're talking "how do I know what's my IP", that of course means
"how do I know what's this interface's IP". If one doesn't even know which
interface, then just list them first.

ObOfftopic: traditionally, AIX systems have had two interfaces (en* and
ent* IIRC) for each physical NIC: one of them behaves as an Ethernet card
and the other as IEEE 802.3.





Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Matthias Lang-2
Miguel Barreiro writes:

 > ObOfftopic: traditionally, AIX systems have had two interfaces (en* and
 > ent* IIRC) for each physical NIC: one of them behaves as an Ethernet card
 > and the other as IEEE 802.3.

IEEE 802.3 _is_ Ethernet. Maybe you meant IEEE 802.5 (Token ring).

  http://www.ieee802.org/3/
  http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/tokenrng.htm

Matthias


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Vlad Dumitrescu-4
I can't remember if this is the right thread :-)

I couldn't connect with ex11 to the X server, and I think I found the cause. If
$DISPLAY refers to localhost or 127.0.0.1, then ex11 first resolves the local
host name, which is then used to look for a cookie.

This means that it isn't enough to have a xauth cookie for localhost:0 or
127.0.0.1:0, but it needs one for "inet:gethostname()":0. Which isn't quite
proper, I think.

regards,
Vlad


Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Miguel Barreiro Paz-2
In reply to this post by Matthias Lang-2

>  > ObOfftopic: traditionally, AIX systems have had two interfaces (en* and
>  > ent* IIRC) for each physical NIC: one of them behaves as an Ethernet card
>  > and the other as IEEE 802.3.
>
> IEEE 802.3 _is_ Ethernet. Maybe you meant IEEE 802.5 (Token ring).

No. IEEE 802.3 is _almost_ Ethernet, thus the AIX comment. See offset 12
in an Ethernet frame (frame type) vs. offset 12 in a 802.3 frame (frame
length). AIX chooses one or the other according to the interface you use,
and both can be used in the same card.

AFAIK no physical medium defined for token ring can be used for ethernet.
so the above wouldn't apply at all.

Regards,

Miguel



Reply | Threaded
Open this post in threaded view
|

How do I find my own IP address?

Shawn Pearce
In reply to this post by Vlad Dumitrescu-4
Vlad Dumitrescu <vlad_dumitrescu> wrote:
> I couldn't connect with ex11 to the X server, and I think I found the cause. If
> $DISPLAY refers to localhost or 127.0.0.1, then ex11 first resolves the local
> host name, which is then used to look for a cookie.
>
> This means that it isn't enough to have a xauth cookie for localhost:0 or
> 127.0.0.1:0, but it needs one for "inet:gethostname()":0. Which isn't quite
> proper, I think.

I think this is my bug Vlad, so don't blame Joe for it.  His second
release has my smart connection patch, which I apparently made a
few wrong assumptions in.

I think you are right, if $DISPLAY directly contains 127.0.0.1 or
localhost, or has the empty string, inet:gethostname() should only
be used as one of the possible names during the xauth cookie search,
but the actual connection should be to 127.0.0.1.

--
Shawn.

  Break into jail and claim police brutality.