---
name: "if_indextoname"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/if_freenameindex"
---

# if\_indextoname(3)

provide mappings between interface names and indexes

Part of [if\_freenameindex(3)](/if_freenameindex/3.md).

## NAME

**if\_nametoindex**, **if\_indextoname**, **if\_nameindex**, **if\_freenameindex** – provide mappings between interface names and indexes

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/socket.h\>**

**#include \<net/if.h\>**

*unsigned int*\

**if\_nametoindex**(*const char \*ifname*);

*char \**\

**if\_indextoname**(*unsigned int ifindex*, *char \*ifname*);

*struct if\_nameindex \**\

**if\_nameindex**(*void*);

*void*\

**if\_freenameindex**(*struct if\_nameindex \*ptr*);

## DESCRIPTION

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)](/getifaddrs/3.md).

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)](/getifaddrs/3.md).

The **if\_nameindex**() returns a NULL pointer if an error occurs while retrieving the list of interfaces via [getifaddrs(3)](/getifaddrs/3.md), or if sufficient memory cannot be allocated.

## SEE ALSO

[getifaddrs(3)](/getifaddrs/3.md), [networking(4)](/networking/4.md)

## STANDARDS

The **if\_nametoindex**(), **if\_indextoname**(), **if\_nameindex**(), and **if\_freenameindex**() functions conform to

## HISTORY

The implementation first appeared in BSDi BSD/OS.
