|
|
#include <net/if.h>unsigned int if_nametoindex(const char * name);
char *if_indextoname(unsigned int index, char * name);
struct if_nameindex *if_nameindex(void);
void if_freenameindex(struct if_nameindex * ptr);
if_nametoindex maps an interface name into its corresponding index. It returns 0 if the specified interface does not exist.
if_indextoname maps an interface index into its corresponding name. The name argument must point to a buffer of at least IFNAMSIZ bytes which is used to hold the returned interface name (including a terminating null character). This pointer is also the return value of the function. If there is not interface corresponding to the specified index, if_indextoname returns NULL.
if_nameindex returns an array of if_nameindex structures, one structure per interface. An if_nameindex structure is defined as follows:
struct if_nameindex { unsigned int if_index; /* 1, 2, ... */ char *if_name; /* null-terminated name */ };The end of the array of structures is indicated by a structure with an if_index of 0 and an if_name of NULL. if_nameindex returns a NULL pointer on error. The memory used for the array of structures and if_name members is obtained dynamically.
if_freenameindex frees the dynamic memory that was allocated by if_nameindex. The argument ptr must be a pointer that was previously returned by if_nameindex.