ifiter_getifaddrs.c revision 282408
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#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 33static isc_boolean_t seenv6 = ISC_FALSE; 34#endif 35 36/*% Iterator structure */ 37struct 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 53isc_result_t 54isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { 55 isc_interfaceiter_t *iter; 56 isc_result_t result; 57 char strbuf[ISC_STRERRORSIZE]; 58 int trys, ret; 59 60 REQUIRE(mctx != NULL); 61 REQUIRE(iterp != NULL); 62 REQUIRE(*iterp == NULL); 63 64 iter = isc_mem_get(mctx, sizeof(*iter)); 65 if (iter == NULL) 66 return (ISC_R_NOMEMORY); 67 68 iter->mctx = mctx; 69 iter->buf = NULL; 70 iter->bufsize = 0; 71 iter->ifaddrs = NULL; 72#ifdef __linux 73 /* 74 * Only open "/proc/net/if_inet6" if we have never seen a IPv6 75 * address returned by getifaddrs(). 76 */ 77 if (!seenv6) { 78 iter->proc = fopen("/proc/net/if_inet6", "r"); 79 if (iter->proc == NULL) { 80 isc__strerror(errno, strbuf, sizeof(strbuf)); 81 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 82 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING, 83 "failed to open /proc/net/if_inet6"); 84 } 85 } else 86 iter->proc = NULL; 87 iter->valid = ISC_R_FAILURE; 88#endif 89 90 /* If interrupted, try again */ 91 for (trys = 0; trys < 3; trys++) { 92 if ((ret = getifaddrs(&iter->ifaddrs)) >= 0) 93 break; 94 if (errno != EINTR) 95 break; 96 } 97 if (ret < 0) { 98 isc__strerror(errno, strbuf, sizeof(strbuf)); 99 UNEXPECTED_ERROR(__FILE__, __LINE__, 100 "getting interface addresses: %s: %s", 101 isc_msgcat_get(isc_msgcat, 102 ISC_MSGSET_IFITERGETIFADDRS, 103 ISC_MSG_GETIFADDRS, 104 "getifaddrs"), 105 strbuf); 106 result = ISC_R_UNEXPECTED; 107 goto failure; 108 } 109 110 /* 111 * A newly created iterator has an undefined position 112 * until isc_interfaceiter_first() is called. 113 */ 114 iter->pos = NULL; 115 iter->result = ISC_R_FAILURE; 116 117 iter->magic = IFITER_MAGIC; 118 *iterp = iter; 119 return (ISC_R_SUCCESS); 120 121 failure: 122#ifdef __linux 123 if (iter->proc != NULL) 124 fclose(iter->proc); 125#endif 126 if (iter->ifaddrs != NULL) /* just in case */ 127 freeifaddrs(iter->ifaddrs); 128 isc_mem_put(mctx, iter, sizeof(*iter)); 129 return (result); 130} 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 139static isc_result_t 140internal_current(isc_interfaceiter_t *iter) { 141 struct ifaddrs *ifa; 142 int family; 143 unsigned int namelen; 144 145 REQUIRE(VALID_IFITER(iter)); 146 147 ifa = iter->pos; 148 149#ifdef __linux 150 if (iter->pos == NULL) 151 return (linux_if_inet6_current(iter)); 152#endif 153 154 INSIST(ifa != NULL); 155 INSIST(ifa->ifa_name != NULL); 156 157 if (ifa->ifa_addr == NULL) 158 return (ISC_R_IGNORE); 159 160 family = ifa->ifa_addr->sa_family; 161 if (family != AF_INET && family != AF_INET6) 162 return (ISC_R_IGNORE); 163 164#ifdef __linux 165 if (family == AF_INET6) 166 seenv6 = ISC_TRUE; 167#endif 168 169 memset(&iter->current, 0, sizeof(iter->current)); 170 171 namelen = strlen(ifa->ifa_name); 172 if (namelen > sizeof(iter->current.name) - 1) 173 namelen = sizeof(iter->current.name) - 1; 174 175 memset(iter->current.name, 0, sizeof(iter->current.name)); 176 memcpy(iter->current.name, ifa->ifa_name, namelen); 177 178 iter->current.flags = 0; 179 180 if ((ifa->ifa_flags & IFF_UP) != 0) 181 iter->current.flags |= INTERFACE_F_UP; 182 183 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) 184 iter->current.flags |= INTERFACE_F_POINTTOPOINT; 185 186 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 187 iter->current.flags |= INTERFACE_F_LOOPBACK; 188 189 if ((ifa->ifa_flags & IFF_BROADCAST) != 0) 190 iter->current.flags |= INTERFACE_F_BROADCAST; 191 192#ifdef IFF_MULTICAST 193 if ((ifa->ifa_flags & IFF_MULTICAST) != 0) 194 iter->current.flags |= INTERFACE_F_MULTICAST; 195#endif 196 197 iter->current.af = family; 198 199 get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name); 200 201 if (ifa->ifa_netmask != NULL) 202 get_addr(family, &iter->current.netmask, ifa->ifa_netmask, 203 ifa->ifa_name); 204 205 if (ifa->ifa_dstaddr != NULL && 206 (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) 207 get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr, 208 ifa->ifa_name); 209 210 if (ifa->ifa_broadaddr != NULL && 211 (iter->current.flags & INTERFACE_F_BROADCAST) != 0) 212 get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr, 213 ifa->ifa_name); 214 215#ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 216 iter->current.ifindex = if_nametoindex(iter->current.name); 217#endif 218 return (ISC_R_SUCCESS); 219} 220 221/* 222 * Step the iterator to the next interface. Unlike 223 * isc_interfaceiter_next(), this may leave the iterator 224 * positioned on an interface that will ultimately 225 * be ignored. Return ISC_R_NOMORE if there are no more 226 * interfaces, otherwise ISC_R_SUCCESS. 227 */ 228static isc_result_t 229internal_next(isc_interfaceiter_t *iter) { 230 231 if (iter->pos != NULL) 232 iter->pos = iter->pos->ifa_next; 233 if (iter->pos == NULL) { 234#ifdef __linux 235 if (!seenv6) 236 return (linux_if_inet6_next(iter)); 237#endif 238 return (ISC_R_NOMORE); 239 } 240 241 return (ISC_R_SUCCESS); 242} 243 244static void 245internal_destroy(isc_interfaceiter_t *iter) { 246 247#ifdef __linux 248 if (iter->proc != NULL) 249 fclose(iter->proc); 250 iter->proc = NULL; 251#endif 252 if (iter->ifaddrs) 253 freeifaddrs(iter->ifaddrs); 254 iter->ifaddrs = NULL; 255} 256 257static 258void internal_first(isc_interfaceiter_t *iter) { 259 260#ifdef __linux 261 linux_if_inet6_first(iter); 262#endif 263 iter->pos = iter->ifaddrs; 264} 265