if_freenameindex3
Provide mappings between interface names and indexes
Usage
unsigned int
char *
struct if_nameindex
*
void
Overview
The if_nametoindex() function maps the interface name specified in ifname to its corresponding index. If the specified interface does not exist, it returns 0.
The if_indextoname() function maps the interface index specified in ifindex to it corresponding name, which is copied into the buffer pointed to by ifname, which must be of at least IFNAMSIZ bytes. This pointer is also the return value of the function. If there is no interface corresponding to the specified index, NULL is returned.
The if_nameindex() function returns an array of if_nameindex structures, one structure per interface, as defined in the include file ⟨net/if.h⟩. The if_nameindex structure contains at least the following entries:
unsigned int if_index; /* 1, 2, ... */
char *if_name; /* null terminated name: "le0", ... */
The end of the array of structures is indicated by a structure with an if_index of 0 and an if_name of NULL. A NULL pointer is returned upon an error.
The if_freenameindex() function frees the dynamic memory that was allocated by if_nameindex().
Return values
Upon successful completion, if_nametoindex() returns the index number of the interface. If the interface is not found, a value of 0 is returned and errno is set to ENXIO. A value of 0 is also returned if an error occurs while retrieving the list of interfaces via getifaddrs(3).
Upon successful completion, if_indextoname() returns ifname. If the interface is not found, a NULL pointer is returned and errno is set to ENXIO. A NULL pointer is also returned if an error occurs while retrieving the list of interfaces via getifaddrs(3).
The if_nameindex() returns a NULL pointer if an error occurs while retrieving the list of interfaces via getifaddrs(3), or if sufficient memory cannot be allocated.
History
- FreeBSD 16.0-CURRENT (2026-10-27) to FreeBSD 13.3-RELEASE: the text shown here
- FreeBSD 13.2-RELEASE to FreeBSD 13.0-RELEASE
- FreeBSD 12.4-RELEASE
- FreeBSD 12.3-RELEASE
- FreeBSD 12.2-RELEASE
- FreeBSD 12.1-RELEASE
- FreeBSD 12.0-RELEASE
- FreeBSD 11.4-RELEASE
- FreeBSD 11.3-RELEASE
- FreeBSD 11.2-RELEASE
- FreeBSD 11.1-RELEASE
- FreeBSD 11.0-RELEASE
- FreeBSD 10.4-RELEASE
- FreeBSD 10.3-RELEASE
- FreeBSD 10.2-RELEASE
- FreeBSD 10.1-RELEASE
- FreeBSD 10.0-RELEASE
- FreeBSD 9.3-RELEASE
- FreeBSD 9.2-RELEASE
- FreeBSD 9.1-RELEASE
- FreeBSD 9.0-RELEASE
- FreeBSD 8.4-RELEASE
- FreeBSD 8.3-RELEASE
- FreeBSD 8.2-RELEASE
- FreeBSD 8.1-RELEASE
- FreeBSD 8.0-RELEASE
- FreeBSD 7.4-RELEASE
- FreeBSD 7.3-RELEASE
- FreeBSD 7.2-RELEASE
- FreeBSD 7.1-RELEASE
- FreeBSD 7.0-RELEASE
- FreeBSD 6.4-RELEASE
- FreeBSD 6.3-RELEASE to FreeBSD 5.2-RELEASE
- FreeBSD 5.1-RELEASE to FreeBSD 5.0-RELEASE
- FreeBSD 4.11-RELEASE to FreeBSD 4.7-RELEASE
- FreeBSD 4.6.2-RELEASE to FreeBSD 4.5-RELEASE
- FreeBSD 4.4-RELEASE
- FreeBSD 4.3-RELEASE
- FreeBSD 4.2-RELEASE to FreeBSD 4.1.1-RELEASE
- FreeBSD 4.1-RELEASE
- FreeBSD 4.0-RELEASE