Next: Local Namespace, Previous: Socket Addresses, Up: Sockets
Each network interface has a name. This usually consists of a few
letters that relate to the type of interface, which may be followed by a
number if there is more than one interface of that type. Examples
might be lo
(the loopback interface) and eth0
(the first
Ethernet interface).
Although such names are convenient for humans, it would be clumsy to have to use them whenever a program needs to refer to an interface. In such situations an interface is referred to by its index, which is an arbitrarily-assigned small positive integer.
The following functions, constants and data types are declared in the header file net/if.h.
This constant defines the maximum buffer size needed to hold an interface name, including its terminating zero byte.
This function yields the interface index corresponding to a particular name. If no interface exists with the name given, it returns 0.
This function maps an interface index to its corresponding name. The returned name is placed in the buffer pointed to by
ifname
, which must be at leastIFNAMSIZ
bytes in length. If the index was invalid, the function's return value is a null pointer, otherwise it isifname
.
This data type is used to hold the information about a single interface. It has the following members:
unsigned int if_index;
- This is the interface index.
char *if_name
- This is the null-terminated index name.
This function returns an array of
if_nameindex
structures, one for every interface that is present. The end of the list is indicated by a structure with an interface of 0 and a null name pointer. If an error occurs, this function returns a null pointer.The returned structure must be freed with
if_freenameindex
after use.