if.h File Reference

IF_NAMESIZE

Length of interface external name, including terminating '\0'. Limitation: IAP names in P.I.P.S. are restricted to 49 bytes in ASCII. For names in languages with a multi-byte character set, you can go upto 24 characters for 2-byte and 16 characters for 3-byte representations. All lengths are excluding the terminal NULL character.

if_freenameindex ( struct if_nameindex * )

IMPORT_C voidif_freenameindex(struct if_nameindex *)

Parameters
Refer to if_nametoindex() for the documentation

if_indextoname ( unsigned, char * )

IMPORT_C char *if_indextoname(unsignedint,
char *
)

Parameters
Refer to if_nametoindex() for the documentation

if_nameindex ( void )

IMPORT_C struct if_nameindex *if_nameindex(void)

Refer to if_nametoindex() for the documentation

if_nametoindex ( const char * )

IMPORT_C unsigned intif_nametoindex(const char *)

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 #include <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.

Parameters
Note: This description also covers the following functions - if_indextoname() if_nameindex() if_freenameindex()
Return Value
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. Upon successful completion, if_indextoname returns ifname. If the interface is not found, a NULL pointer is returned and errno is set to ENXIO. The if_nameindex returns a NULL pointer if sufficient memory cannot be allocated.

setdefaultif ( const struct ifreq * )

IMPORT_C intsetdefaultif(const struct ifreq *)

The setdefaultif function can be used to set (or remove) a default network interface (either IAP or SNAP) for the application. This default interface, if set, will be used by all the further socket related function calls (connect, send, write etc) and all the host resolver function calls (getaddrinfo, getnameinfo, gethostbyname, getaddrbyname etc).

If there is a default interface set using setdefaultif and if there is a separate interface set on a socket using the ioctl system call, network operations on that socket will not use the default one, but the socket specific interface.

To remove the default interace, pass NULL as the argument.

To set an IAP name as the default interface, the 'ifr_name' member of the 'ifreq' structure must be filled with the IAP name to be set. Unicode IAP names can also be set by converting them to UTF8 format before passing them to this API. See the example below:

#include <stdio.h>
#include <string.h>
#include <net/if.h>

int main()
	{
	struct ifreq ifReq;
	int ret;
	
	/* Set the default interface using IAP name */
	strcpy( ifReq.ifr_name, "Example Interface Name" );
	ret = setdefaultif( &ifReq );
	if( ret == -1 )
		printf( "Setting default interface failed with errno = %d", errno );
	
	/* Perform network operations */
	/* ... */
	
	/* Remove the default interface */
	ret = setdefaultif( NULL );
	if( ret == -1 )
		printf( "Removing default interface failed with errno = %d", errno );
	
	return 0;
	}

To set a SNAP id as the default interface, the 'ifr_name' member of the 'ifreq' structure must be an empty string. In this case, the 'snap_id' member of the 'ifr_ifru' union in the parameter should contain the SNAP id to be set. It is recommended to zero initialize the 'ifreq' structure in this case. See the example below.

#include <stdio.h>
#include <string.h>
#include <net/if.h>

int main()
	{
	struct ifreq ifReq;
	int ret;
	unsigned int snapId = /* Get the SNAP id to be set from the application settings */
	
	/* Set the default interface using SNAP id */
	
	/* memset the ifreq to make sure that the interface name is an empty string */
	memset(&ifReq, 0, sizeof(struct ifreq));
	ifReq.ifr_ifru.snap_id = snapId;
	
	ret = setdefaultif( &ifReq );
	if( ret == -1 )
		printf( "Setting default interface failed with errno = %d", errno );
	
	/* Perform network operations */
	/* ... */
	
	/* Remove the default interface */
	ret = setdefaultif( NULL );
	if( ret == -1 )
		printf( "Removing default interface failed with errno = %d", errno );
	
	return 0;
	}

The setdefaultif is not guaranteed to be thread safe.

Return Value
The setdefaultif returns 0 on success and -1 on failure. Specifically, if the interface is not found, -1 is returned and errno is set to ENOENT.

unsetdefaultif ( )

IMPORT_C intunsetdefaultif()

MAXDNSSUFFIXES

freednssuffixes ( struct if_dns_suffixes * )

IMPORT_C voidfreednssuffixes(struct if_dns_suffixes *)