Getting list of all interfaces and associated IP addresses

Getting list of all interfaces and associated IP addresses

Post by Andrew Franc » Wed, 21 Oct 1998 04:00:00



Hello, I am trying to write a programme that will allow me to get all the
interfaces on a machine and all the IP addresses associated with an
interface (I believe methods IP aliasing allows for more than one IP
address per interface). So far I have been able to to get interfaces and
*one* IP address associated with that interface through use of socket
(AF_INET, SOCK_DGRAM, 0) and of ioctl SIOCGIFCONF and SIOCGIFADDR. However
these use ifc and ifreq structures, the later referring to only one
IP address. Looking through sockio.h, it seems that I need access to
the ifnet structure which contains the ifaddr *if_addrlist variable which
is a pointer to a list of IP addresses.

I would appreciate it if someone can tell me how to do the aforementioned,
especially under Solaris. Also if anyone can give me tips on how a utility
like ifconfig or netstat under Solaris works (I am not sure how portable
the technique I am using, is) would also help.

Cheers,
Andrew Francis

 
 
 

Getting list of all interfaces and associated IP addresses

Post by Andrew Giert » Wed, 21 Oct 1998 04:00:00


 Andrew> Hello, I am trying to write a programme that will allow me to
 Andrew> get all the interfaces on a machine and all the IP addresses
 Andrew> associated with an interface (I believe methods IP aliasing
 Andrew> allows for more than one IP address per interface). So far I
 Andrew> have been able to to get interfaces and *one* IP address
 Andrew> associated with that interface through use of socket
 Andrew> (AF_INET, SOCK_DGRAM, 0) and of ioctl SIOCGIFCONF and
 Andrew> SIOCGIFADDR. However these use ifc and ifreq structures, the
 Andrew> later referring to only one IP address.

The precise behaviour of SIOCGIFCONF when aliases are involved does
depend on the system.

 Andrew> I would appreciate it if someone can tell me how to do the
 Andrew> aforementioned, especially under Solaris.

This is covered in vol. 1 of Stevens' "Unix Network Programming",
in chapter 16.

 Andrew> Also if anyone can give me tips on how a utility like
 Andrew> ifconfig or netstat under Solaris works (I am not sure how
 Andrew> portable the technique I am using, is) would also help.

Most implementations of netstat rely on grovelling directly in kernel
memory or using implementation-specific syscalls. ifconfig, on the
other hand, is likely to be implemented using the interface ioctls
(SIOCGIFADDR, etc.)

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Getting list of all interfaces and associated IP addresses

Post by Michael Clar » Fri, 23 Oct 1998 04:00:00




>  Andrew> Hello, I am trying to write a programme that will allow me to
>  Andrew> get all the interfaces on a machine and all the IP addresses
>  Andrew> associated with an interface (I believe methods IP aliasing
>  Andrew> allows for more than one IP address per interface). So far I
>  Andrew> have been able to to get interfaces and *one* IP address
>  Andrew> associated with that interface through use of socket
>  Andrew> (AF_INET, SOCK_DGRAM, 0) and of ioctl SIOCGIFCONF and
>  Andrew> SIOCGIFADDR. However these use ifc and ifreq structures, the
>  Andrew> later referring to only one IP address.

One virtual interface will only have one address and associated subnet mask.
You need to iteratethrough the structure returned from SIOCGIFCONF to see all
of the interfaces (base or virtual) names and do a SIOCGIFADDR for each one.

    struct ifconf ifc;
    struct ifreq *ifr;
    int i
    ...
    ifr = ifc.ifc_req;
    for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
    {
        printf("%s\n", ifr->ifr_name);
        ....
    }

Quote:> The precise behaviour of SIOCGIFCONF when aliases are involved does
> depend on the system.

On Solaris 2.5.1, Solaris 2.6 and Linux 2.0.34 the ifconf.ifr_name returned
from a SIOCGIFCONF will contain <int_name>:<virtual_slot> for virtual
interfaces i.e. hme0:1 for the first virtual interface. Base interfaces will
not contain the colon.

I have written a replacement for ifconfig that raises multiple interfaces in
one go (for web hosting virtual servers) and have used this method
successfully.

Quote:>  Andrew> I would appreciate it if someone can tell me how to do the
>  Andrew> aforementioned, especially under Solaris.

> This is covered in vol. 1 of Stevens' "Unix Network Programming",
> in chapter 16.

>  Andrew> Also if anyone can give me tips on how a utility like
>  Andrew> ifconfig or netstat under Solaris works (I am not sure how
>  Andrew> portable the technique I am using, is) would also help.

> Most implementations of netstat rely on grovelling directly in kernel
> memory or using implementation-specific syscalls. ifconfig, on the
> other hand, is likely to be implemented using the interface ioctls
> (SIOCGIFADDR, etc.)

> --
> Andrew.

> comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
>                            or <URL: http://www.whitefang.com/unix/>

 
 
 

1. getting list of interfaces or IP addresses

I'm looking for a method of extracting all the IP addresses (or
interfaces) configured on a Redhat Linux system.  Under BSDI while looking
at how the rwhod daemon multicasts its output on all multicast capable
interfaces I ran across a BSDI system function called getifaddrs() which
apparently does the job.  But under Linux (gcc/egcs installed) I haven't
yet found something equivalent to it.  Can anybody point me to some C
source that will do this?  

Tony
--
Antonio Querubin

2. Redhat 6.1 kickstart problems

3. How to obtain IP Address associated with a physical interface in C

4. Need help with boot disks and Sun Volume Manager

5. How to obtain IP Address associated with a physical interface

6. X.25 API programming questions

7. Getting interface IP addresses with proc filesystem

8. Malloc warning: free():

9. Getting the IP address of an interface in C.

10. Getting the broadcast address for IP interface

11. getting IP addresses of network interfaces on Linux box

12. Host primary interface address as source address in IP header

13. How to associate multiple IP addresses with the same Sun workstation