if_nametoindex()--Map an Interface Name to its Corresponding Index



  Syntax
  #include  <net/if.h>

  unsigned int if_nametoindex(const char *ifname);

  Service Program Name: QSOSRV2
  Default Public Authority: *USE
  Threadsafe: Yes

The if_nametoindex() function returns the interface index corresponding to name ifname.


Parameters

ifname  (Input) 
Pointer to a null terminated string containing the interface (line description) name.


Authorities

No authorization is required.


Return Value

if_nametoindex() returns an unsigned integer. Possible values are:


Error Conditions

When if_nametoindex() fails, errno can be set to one of the following:

[ENXIO]
The specified interface name does not exist.
[EFAULT]
The buffer pointed to by ifname can not be accessed.

Usage Notes

  1. The interface (line description) name found at ifname is assumed to be in the default coded character set identifier (CCSID) currently in effect for the job.
  2. It is important to note that the term "Interface" refers to the name on a line description (i.e. a physical interface) for this API. Other parts of the operating system, when refering to "Interface," mean an IP address.

Related Information


Example

The following example shows how if_nametoindex() is used.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <net/if.h>
#include <sys/types.h>
#include <errno.h>

void main()
{

  unsigned int interfaceIndex = if_nametoindex("MYETH");
  if (interfaceIndex == 0)
  {
     printf("if_nametoindex() failed with errno =  %d %s \n",
            errno,strerror(errno));
     return;
  }

  ...

}


API introduced: V5R4

[ Back to top | UNIX-Type APIs | APIs by category ]