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.
IMPORT_C void | if_freenameindex | ( | struct if_nameindex * | ) |
Parameters | |
---|---|
Refer to if_nametoindex() for the documentation |
IMPORT_C char * | if_indextoname | ( | unsigned | int, |
char * | ||||
) |
Parameters | |
---|---|
Refer to if_nametoindex() for the documentation |
IMPORT_C unsigned int | if_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() |
IMPORT_C int | setdefaultif | ( | 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.