1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
'\" 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.
.\"
.TH GETIFADDRS 3SOCKET "Feb 1, 2022"
.SH NAME
getifaddrs, freeifaddrs \- get interface addresses
.SH SYNOPSIS
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB-lnsl\fR \
[ \fIlibrary\fR ... ]
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
.fi
.LP
.nf
\fBint\fR \fBgetifaddrs\fR(\fBstruct ifaddrs **\fR\fIifap\fR);
.fi
.LP
.nf
\fBvoid\fR \fBfreeifaddrs\fR(\fBstruct ifaddrs *\fR\fIifp\fR);
.fi
.SH DESCRIPTION
The \fBgetifaddrs\fR() function is used to obtain the list of network
interfaces on the local machine. A reference to a linked list of \fBifaddrs\fR
structures, as defined in \fB<ifaddrs.h>\fR, is stored in the memory referenced
by \fIifap\fR. Each structure in the list describes one network interface
address, and is of the form:
.sp
.in +2
.nf
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
.fi
.in -2
.sp
.LP
The list is traversed by following the \fIifa_next\fR pointer. This member is
\fBNULL\fR on the last structure in the list.
.sp
.LP
The \fIifa_name\fR member contains the interface name.
.sp
.LP
The \fIifa_flags\fR member contains the interface flags.
.sp
.LP
The \fIifa_addr\fR member references the address of the interface. Use the
\fIsa_family\fR member of this structure to determine the format of the
address, as described in \fBsocket.h\fR(3HEAD).
.sp
.LP
The \fIifa_netmask\fR member references the netmask associated with ifa_addr,
or \fBNULL\fR if one is not set.
.sp
.LP
If the \fBIFF_BROADCAST\fR bit is set in \fIifa_flags\fR, then
\fIifa_broadaddr\fR is valid, or \fBNULL\fR if not present. If the
\fBIFF_POINTOPOINT\fR bit is set, then \fIifa_dstaddr\fR is valid, or \fBNULL\fR
if not present. These two flags are mutually exclusive; see \fBif_tcp\fR(4P)
for more information.
.sp
.LP
The \fIifa_data\fR member is specific to the address family. It is currently
only available for AF_LINK entries where it contains a pointer to the
\fBstruct if_data\fR (as defined in \fBif.h\fR(3HEAD)).
.sp
.LP
The memory used by \fBgetifaddrs\fR() to back the list is dynamically allocated.
It should be freed using \fBfreeifaddrs\fR().
.SH RETURN VALUES
If successful, \fBgetifaddrs\fR() returns the value \fB0\fR; otherwise it
returns \fB\(mi1\fR and sets \fIerrno\fR to indicate the error.
.SH ERRORS
The \fBgetifaddrs\fR() function may fail and set \fIerrno\fR for any of the
errors specified for the library routines \fBioctl\fR(2),
\fBsocket\fR(3SOCKET), and \fBmalloc\fR(3C).
.SH ATTRIBUTES
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
Interface Stability Committed
_
MT-Level MT-Safe
.TE
.SH SEE ALSO
.BR ioctl (2),
.BR malloc (3C),
.BR socket.h (3HEAD),
.BR sockaddr (3SOCKET),
.BR socket (3SOCKET),
.BR if_tcp (4P),
.BR attributes (7),
.BR ifconfig (8),
.BR ipadm (8)
.SH NOTES
This function lists interfaces of type AF_INET, AF_INET6, and AF_LINK.
For AF_INET and AF_INET6 only interfaces with the \fBIFF_UP\fR
flag set are listed; see \fBif_tcp\fR(7P) and \fBifconfig\fR(1M) for more
information. For AF_LINK entries the interface index is only available when the
link is plumbed.
|