/src/ntp-dev/lib/isc/unix/ifiter_getifaddrs.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC") |
3 | | * Copyright (C) 2003 Internet Software Consortium. |
4 | | * |
5 | | * Permission to use, copy, modify, and/or distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
10 | | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
11 | | * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
12 | | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
13 | | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
14 | | * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
15 | | * PERFORMANCE OF THIS SOFTWARE. |
16 | | */ |
17 | | |
18 | | /* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */ |
19 | | |
20 | | /*! \file |
21 | | * \brief |
22 | | * Obtain the list of network interfaces using the getifaddrs(3) library. |
23 | | */ |
24 | | |
25 | | #include <ifaddrs.h> |
26 | | |
27 | | /*% Iterator Magic */ |
28 | 1 | #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G') |
29 | | /*% Valid Iterator */ |
30 | | #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC) |
31 | | |
32 | | #ifdef __linux |
33 | | static isc_boolean_t seenv6 = ISC_FALSE; |
34 | | #endif |
35 | | |
36 | | /*% Iterator structure */ |
37 | | struct isc_interfaceiter { |
38 | | unsigned int magic; /*%< Magic number. */ |
39 | | isc_mem_t *mctx; |
40 | | void *buf; /*%< (unused) */ |
41 | | unsigned int bufsize; /*%< (always 0) */ |
42 | | struct ifaddrs *ifaddrs; /*%< List of ifaddrs */ |
43 | | struct ifaddrs *pos; /*%< Ptr to current ifaddr */ |
44 | | isc_interface_t current; /*%< Current interface data. */ |
45 | | isc_result_t result; /*%< Last result code. */ |
46 | | #ifdef __linux |
47 | | FILE * proc; |
48 | | char entry[ISC_IF_INET6_SZ]; |
49 | | isc_result_t valid; |
50 | | #endif |
51 | | }; |
52 | | |
53 | | isc_result_t |
54 | 1 | isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { |
55 | 1 | isc_interfaceiter_t *iter; |
56 | 1 | isc_result_t result; |
57 | 1 | char strbuf[ISC_STRERRORSIZE]; |
58 | 1 | int trys, ret; |
59 | | |
60 | 1 | REQUIRE(mctx != NULL); |
61 | 1 | REQUIRE(iterp != NULL); |
62 | 1 | REQUIRE(*iterp == NULL); |
63 | | |
64 | 1 | iter = isc_mem_get(mctx, sizeof(*iter)); |
65 | 1 | if (iter == NULL) |
66 | 0 | return (ISC_R_NOMEMORY); |
67 | | |
68 | 1 | iter->mctx = mctx; |
69 | 1 | iter->buf = NULL; |
70 | 1 | iter->bufsize = 0; |
71 | 1 | iter->ifaddrs = NULL; |
72 | 1 | #ifdef __linux |
73 | | /* |
74 | | * Only open "/proc/net/if_inet6" if we have never seen a IPv6 |
75 | | * address returned by getifaddrs(). |
76 | | */ |
77 | 1 | if (!seenv6) { |
78 | 1 | iter->proc = fopen("/proc/net/if_inet6", "r"); |
79 | 1 | if (iter->proc == NULL) { |
80 | 0 | isc__strerror(errno, strbuf, sizeof(strbuf)); |
81 | 0 | isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, |
82 | 0 | ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING, |
83 | 0 | "failed to open /proc/net/if_inet6"); |
84 | 0 | } |
85 | 1 | } else |
86 | 0 | iter->proc = NULL; |
87 | 1 | iter->valid = ISC_R_FAILURE; |
88 | 1 | #endif |
89 | | |
90 | | /* If interrupted, try again */ |
91 | 1 | for (trys = 0; trys < 3; trys++) { |
92 | 1 | if ((ret = getifaddrs(&iter->ifaddrs)) >= 0) |
93 | 1 | break; |
94 | 0 | if (errno != EINTR) |
95 | 0 | break; |
96 | 0 | } |
97 | 1 | if (ret < 0) { |
98 | 0 | isc__strerror(errno, strbuf, sizeof(strbuf)); |
99 | 0 | UNEXPECTED_ERROR(__FILE__, __LINE__, |
100 | 0 | "getting interface addresses: %s: %s", |
101 | 0 | isc_msgcat_get(isc_msgcat, |
102 | 0 | ISC_MSGSET_IFITERGETIFADDRS, |
103 | 0 | ISC_MSG_GETIFADDRS, |
104 | 0 | "getifaddrs"), |
105 | 0 | strbuf); |
106 | 0 | result = ISC_R_UNEXPECTED; |
107 | 0 | goto failure; |
108 | 0 | } |
109 | | |
110 | | /* |
111 | | * A newly created iterator has an undefined position |
112 | | * until isc_interfaceiter_first() is called. |
113 | | */ |
114 | 1 | iter->pos = NULL; |
115 | 1 | iter->result = ISC_R_FAILURE; |
116 | | |
117 | 1 | iter->magic = IFITER_MAGIC; |
118 | 1 | *iterp = iter; |
119 | 1 | return (ISC_R_SUCCESS); |
120 | | |
121 | 0 | failure: |
122 | 0 | #ifdef __linux |
123 | 0 | if (iter->proc != NULL) |
124 | 0 | fclose(iter->proc); |
125 | 0 | #endif |
126 | 0 | if (iter->ifaddrs != NULL) /* just in case */ |
127 | 0 | freeifaddrs(iter->ifaddrs); |
128 | 0 | isc_mem_put(mctx, iter, sizeof(*iter)); |
129 | 0 | return (result); |
130 | 1 | } |
131 | | |
132 | | /* |
133 | | * Get information about the current interface to iter->current. |
134 | | * If successful, return ISC_R_SUCCESS. |
135 | | * If the interface has an unsupported address family, |
136 | | * return ISC_R_IGNORE. |
137 | | */ |
138 | | |
139 | | static isc_result_t |
140 | 4 | internal_current(isc_interfaceiter_t *iter) { |
141 | 4 | struct ifaddrs *ifa; |
142 | 4 | int family; |
143 | 4 | unsigned int namelen; |
144 | | |
145 | 4 | REQUIRE(VALID_IFITER(iter)); |
146 | | |
147 | 0 | ifa = iter->pos; |
148 | | |
149 | 4 | #ifdef __linux |
150 | | /* |
151 | | * [Bug 2792] |
152 | | * burnicki: iter->pos is usually never NULL here (anymore?), |
153 | | * so linux_if_inet6_current(iter) is never called here. |
154 | | * However, that routine would check (under Linux), if the |
155 | | * interface is in a tentative state, e.g. if there's no link |
156 | | * yet but an IPv6 address has already be assigned. |
157 | | */ |
158 | 4 | if (iter->pos == NULL) |
159 | 0 | return (linux_if_inet6_current(iter)); |
160 | 4 | #endif |
161 | | |
162 | 4 | INSIST(ifa != NULL); |
163 | 4 | INSIST(ifa->ifa_name != NULL); |
164 | | |
165 | | |
166 | 0 | #ifdef IFF_RUNNING |
167 | | /* |
168 | | * [Bug 2792] |
169 | | * burnicki: if the interface is not running then |
170 | | * it may be in a tentative state. See above. |
171 | | */ |
172 | 4 | if ((ifa->ifa_flags & IFF_RUNNING) == 0) |
173 | 0 | return (ISC_R_IGNORE); |
174 | 4 | #endif |
175 | | |
176 | 4 | if (ifa->ifa_addr == NULL) |
177 | 0 | return (ISC_R_IGNORE); |
178 | | |
179 | 4 | family = ifa->ifa_addr->sa_family; |
180 | 4 | if (family != AF_INET && family != AF_INET6) |
181 | 2 | return (ISC_R_IGNORE); |
182 | | |
183 | 2 | #ifdef __linux |
184 | 2 | if (family == AF_INET6) |
185 | 0 | seenv6 = ISC_TRUE; |
186 | 2 | #endif |
187 | | |
188 | 2 | memset(&iter->current, 0, sizeof(iter->current)); |
189 | | |
190 | 2 | namelen = strlen(ifa->ifa_name); |
191 | 2 | if (namelen > sizeof(iter->current.name) - 1) |
192 | 0 | namelen = sizeof(iter->current.name) - 1; |
193 | | |
194 | 2 | memset(iter->current.name, 0, sizeof(iter->current.name)); |
195 | 2 | memcpy(iter->current.name, ifa->ifa_name, namelen); |
196 | | |
197 | 2 | iter->current.flags = 0; |
198 | | |
199 | 2 | if ((ifa->ifa_flags & IFF_UP) != 0) |
200 | 2 | iter->current.flags |= INTERFACE_F_UP; |
201 | | |
202 | 2 | if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) |
203 | 0 | iter->current.flags |= INTERFACE_F_POINTTOPOINT; |
204 | | |
205 | 2 | if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) |
206 | 1 | iter->current.flags |= INTERFACE_F_LOOPBACK; |
207 | | |
208 | 2 | if ((ifa->ifa_flags & IFF_BROADCAST) != 0) |
209 | 1 | iter->current.flags |= INTERFACE_F_BROADCAST; |
210 | | |
211 | 2 | #ifdef IFF_MULTICAST |
212 | 2 | if ((ifa->ifa_flags & IFF_MULTICAST) != 0) |
213 | 1 | iter->current.flags |= INTERFACE_F_MULTICAST; |
214 | 2 | #endif |
215 | | |
216 | 2 | iter->current.af = family; |
217 | | |
218 | 2 | get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name); |
219 | | |
220 | 2 | if (ifa->ifa_netmask != NULL) |
221 | 2 | get_addr(family, &iter->current.netmask, ifa->ifa_netmask, |
222 | 2 | ifa->ifa_name); |
223 | | |
224 | 2 | if (ifa->ifa_dstaddr != NULL && |
225 | 2 | (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) |
226 | 0 | get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr, |
227 | 0 | ifa->ifa_name); |
228 | | |
229 | 2 | if (ifa->ifa_broadaddr != NULL && |
230 | 2 | (iter->current.flags & INTERFACE_F_BROADCAST) != 0) |
231 | 1 | get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr, |
232 | 1 | ifa->ifa_name); |
233 | | |
234 | 2 | #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX |
235 | 2 | iter->current.ifindex = if_nametoindex(iter->current.name); |
236 | 2 | #endif |
237 | 2 | return (ISC_R_SUCCESS); |
238 | 4 | } |
239 | | |
240 | | /* |
241 | | * Step the iterator to the next interface. Unlike |
242 | | * isc_interfaceiter_next(), this may leave the iterator |
243 | | * positioned on an interface that will ultimately |
244 | | * be ignored. Return ISC_R_NOMORE if there are no more |
245 | | * interfaces, otherwise ISC_R_SUCCESS. |
246 | | */ |
247 | | static isc_result_t |
248 | 4 | internal_next(isc_interfaceiter_t *iter) { |
249 | | |
250 | 4 | if (iter->pos != NULL) |
251 | 4 | iter->pos = iter->pos->ifa_next; |
252 | 4 | if (iter->pos == NULL) { |
253 | 1 | #ifdef __linux |
254 | 1 | if (!seenv6) |
255 | 1 | return (linux_if_inet6_next(iter)); |
256 | 0 | #endif |
257 | 0 | return (ISC_R_NOMORE); |
258 | 1 | } |
259 | | |
260 | 3 | return (ISC_R_SUCCESS); |
261 | 4 | } |
262 | | |
263 | | static void |
264 | 1 | internal_destroy(isc_interfaceiter_t *iter) { |
265 | | |
266 | 1 | #ifdef __linux |
267 | 1 | if (iter->proc != NULL) |
268 | 1 | fclose(iter->proc); |
269 | 1 | iter->proc = NULL; |
270 | 1 | #endif |
271 | 1 | if (iter->ifaddrs) |
272 | 1 | freeifaddrs(iter->ifaddrs); |
273 | 1 | iter->ifaddrs = NULL; |
274 | 1 | } |
275 | | |
276 | | static |
277 | 1 | void internal_first(isc_interfaceiter_t *iter) { |
278 | | |
279 | 1 | #ifdef __linux |
280 | 1 | linux_if_inet6_first(iter); |
281 | 1 | #endif |
282 | 1 | iter->pos = iter->ifaddrs; |
283 | 1 | } |