xref: /illumos-gate/usr/src/man/man3socket/getifaddrs.3socket (revision 38aced4fb3d60e34a44207b22d0b0dd4909bf4d1)
t

This file and its contents are supplied under the terms of the
Common Development and Distribution License ("CDDL"), version 1.0.
You may only use this file in accordance with the terms of version
1.0 of the CDDL.

A full copy of the text of the CDDL should have accompanied this
source. A copy of the CDDL is also available via the Internet at
http://www.illumos.org/license/CDDL.


Copyright (c) 2013, Joyent, Inc. All rights reserved.

GETIFADDRS 3SOCKET "Feb 1, 2022"
NAME
getifaddrs, freeifaddrs - get interface addresses
SYNOPSIS
cc [ flag ... ] file ... -lsocket -lnsl \
[ library ... ]
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>

int getifaddrs(struct ifaddrs **ifap);

void freeifaddrs(struct ifaddrs *ifp);
DESCRIPTION
The getifaddrs() function is used to obtain the list of network interfaces on the local machine. A reference to a linked list of ifaddrs structures, as defined in <ifaddrs.h>, is stored in the memory referenced by ifap. Each structure in the list describes one network interface address, and is of the form:
struct ifaddrs {
 struct ifaddrs *ifa_next;
 char *ifa_name;
 uint64_t ifa_flags;
 struct sockaddr *ifa_addr;
 struct sockaddr *ifa_netmask;
 union {
 struct sockaddr *ifu_broadaddr;
 struct sockaddr *ifu_dstaddr;
 } ifa_ifu;
 void *ifa_data;
};
#define ifa_broadaddr ifa_ifu.ifu_broadaddr
#define ifa_dstaddr ifa_ifu.ifu_dstaddr

The list is traversed by following the ifa_next pointer. This member is NULL on the last structure in the list.

The ifa_name member contains the interface name.

The ifa_flags member contains the interface flags.

The ifa_addr member references the address of the interface. Use the sa_family member of this structure to determine the format of the address, as described in socket.h(3HEAD).

The ifa_netmask member references the netmask associated with ifa_addr, or NULL if one is not set.

If the IFF_BROADCAST bit is set in ifa_flags, then ifa_broadaddr is valid, or NULL if not present. If the IFF_POINTOPOINT bit is set, then ifa_dstaddr is valid, or NULL if not present. These two flags are mutually exclusive; see if_tcp(4P) for more information.

The ifa_data member is specific to the address family. It is currently only available for AF_LINK entries where it contains a pointer to the struct if_data (as defined in if.h(3HEAD)).

The memory used by getifaddrs() to back the list is dynamically allocated. It should be freed using freeifaddrs().

RETURN VALUES
If successful, getifaddrs() returns the value 0; otherwise it returns -1 and sets errno to indicate the error.
ERRORS
The getifaddrs() function may fail and set errno for any of the errors specified for the library routines ioctl(2), socket(3SOCKET), and malloc(3C).
ATTRIBUTES
ATTRIBUTE TYPE ATTRIBUTE VALUE
Interface Stability Committed
MT-Level MT-Safe
SEE ALSO
ioctl (2), malloc (3C), socket.h (3HEAD), sockaddr (3SOCKET), socket (3SOCKET), if_tcp (4P), attributes (7), ifconfig (8), ipadm (8)
NOTES
This function lists interfaces of type AF_INET, AF_INET6, and AF_LINK. For AF_INET and AF_INET6 only interfaces with the IFF_UP flag set are listed; see if_tcp(7P) and ifconfig(1M) for more information. For AF_LINK entries the interface index is only available when the link is plumbed.