Determining one's own IP address.

Micky Liu micky at cunixc.cc.columbia.edu
Thu Dec 14 15:51:12 AEST 1989


I have to admit that I have not been following the whole story here, but
if you are attempting to determine the IP address of a machine, then I think
the correct method is to use an ioctl() call to request information on all
of the connected network interfaces (since a machine can be multi-homed).
The next step is to examine the list of interfaces returned and look for
the IP address assigned to each physical network interface.  I have only
had experience with Sun's so this may be machine specific, but I think
conceptually it should be the same on all Unix machines...

The important structures are:

     struct ifconf ifc;
     struct ifreq  *ifr;

both of which I think can be found in /usr/include/net/if.h.  The particular
ioctl() call looks like:

     ioctl(socket,SIOCGIFCONF,(char*)&ifc)

Then there will be a list of structures that can accesses with

     ifr = ifc.ifc_req;

Much of this information can be found in the SunOS 4.x Network Programmer's
Manual, Chapter 9 -- An Advanced Socket-Based Interprocess Communications
Tutorial.  The example they use will find broadcast addresses for each
network interface, but the method can be readily adapted to find the
IP address of each interface.

Good Luck!

Micky

internet: micky at cunixc.cc.columbia.edu
  usenet: ...!rutgers!columbia!cunixc!micky
  bitnet: micky at cunixc.bitnet



More information about the Comp.unix.wizards mailing list