ifiter_getifaddrs.c revision 285612
176082Sbmah/* 276082Sbmah * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC") 376082Sbmah * Copyright (C) 2003 Internet Software Consortium. 476082Sbmah * 576082Sbmah * Permission to use, copy, modify, and/or distribute this software for any 676082Sbmah * purpose with or without fee is hereby granted, provided that the above 776082Sbmah * copyright notice and this permission notice appear in all copies. 876082Sbmah * 976082Sbmah * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 1076082Sbmah * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 1176082Sbmah * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 1276082Sbmah * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 1376082Sbmah * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 1476082Sbmah * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 1576082Sbmah * PERFORMANCE OF THIS SOFTWARE. 1676082Sbmah */ 1776082Sbmah 1876082Sbmah/* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */ 1976082Sbmah 2076082Sbmah/*! \file 2176254Sbmah * \brief 2276254Sbmah * Obtain the list of network interfaces using the getifaddrs(3) library. 2376222Sbmah */ 2476222Sbmah 2586150Solgeni#include <ifaddrs.h> 2676222Sbmah 27241096Sgabor/*% Iterator Magic */ 2876254Sbmah#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G') 2976254Sbmah/*% Valid Iterator */ 3076254Sbmah#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC) 31241096Sgabor 3276254Sbmah#ifdef __linux 3376254Sbmahstatic isc_boolean_t seenv6 = ISC_FALSE; 3476254Sbmah#endif 3576254Sbmah 36241096Sgabor/*% Iterator structure */ 3776222Sbmahstruct isc_interfaceiter { 3876222Sbmah unsigned int magic; /*%< Magic number. */ 3976222Sbmah isc_mem_t *mctx; 4076222Sbmah void *buf; /*%< (unused) */ 4176222Sbmah unsigned int bufsize; /*%< (always 0) */ 42158627Swilko struct ifaddrs *ifaddrs; /*%< List of ifaddrs */ 43241096Sgabor struct ifaddrs *pos; /*%< Ptr to current ifaddr */ 4476082Sbmah isc_interface_t current; /*%< Current interface data. */ 4576082Sbmah isc_result_t result; /*%< Last result code. */ 4676082Sbmah#ifdef __linux 4776082Sbmah FILE * proc; 48241096Sgabor char entry[ISC_IF_INET6_SZ]; 4976254Sbmah isc_result_t valid; 5076254Sbmah#endif 5176254Sbmah}; 5276254Sbmah 5376254Sbmahisc_result_t 54241096Sgaborisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { 5576254Sbmah isc_interfaceiter_t *iter; 5676082Sbmah isc_result_t result; 5776082Sbmah char strbuf[ISC_STRERRORSIZE]; 5876082Sbmah int trys, ret; 5976082Sbmah 6076082Sbmah REQUIRE(mctx != NULL); 6176082Sbmah REQUIRE(iterp != NULL); 6276082Sbmah REQUIRE(*iterp == NULL); 6376082Sbmah 6476082Sbmah iter = isc_mem_get(mctx, sizeof(*iter)); 6576082Sbmah if (iter == NULL) 6676082Sbmah return (ISC_R_NOMEMORY); 6776082Sbmah 6876082Sbmah iter->mctx = mctx; 6976082Sbmah iter->buf = NULL; 7076082Sbmah iter->bufsize = 0; 7176082Sbmah iter->ifaddrs = NULL; 7276082Sbmah#ifdef __linux 7376082Sbmah /* 7476082Sbmah * Only open "/proc/net/if_inet6" if we have never seen a IPv6 7576082Sbmah * address returned by getifaddrs(). 7676082Sbmah */ 7776082Sbmah if (!seenv6) { 7876082Sbmah iter->proc = fopen("/proc/net/if_inet6", "r"); 7976082Sbmah if (iter->proc == NULL) { 8076082Sbmah isc__strerror(errno, strbuf, sizeof(strbuf)); 8176082Sbmah isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 8276138Sbmah ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING, 8376138Sbmah "failed to open /proc/net/if_inet6"); 8476138Sbmah } 8576138Sbmah } else 8676138Sbmah iter->proc = NULL; 8776138Sbmah iter->valid = ISC_R_FAILURE; 8876138Sbmah#endif 8976138Sbmah 9076082Sbmah /* If interrupted, try again */ 91241096Sgabor for (trys = 0; trys < 3; trys++) { 9276082Sbmah if ((ret = getifaddrs(&iter->ifaddrs)) >= 0) 9376082Sbmah break; 9476082Sbmah if (errno != EINTR) 9576082Sbmah break; 9676082Sbmah } 9776082Sbmah if (ret < 0) { 9876082Sbmah isc__strerror(errno, strbuf, sizeof(strbuf)); 9976082Sbmah UNEXPECTED_ERROR(__FILE__, __LINE__, 100158627Swilko "getting interface addresses: %s: %s", 10176082Sbmah isc_msgcat_get(isc_msgcat, 102198114Shrs ISC_MSGSET_IFITERGETIFADDRS, 103198114Shrs ISC_MSG_GETIFADDRS, 104198114Shrs "getifaddrs"), 105198114Shrs strbuf); 10676082Sbmah result = ISC_R_UNEXPECTED; 10776082Sbmah goto failure; 10876082Sbmah } 10976254Sbmah 11076254Sbmah /* 111241096Sgabor * A newly created iterator has an undefined position 11276254Sbmah * until isc_interfaceiter_first() is called. 113241096Sgabor */ 11476254Sbmah iter->pos = NULL; 11576254Sbmah iter->result = ISC_R_FAILURE; 11676082Sbmah 11777211Sbmah iter->magic = IFITER_MAGIC; 11877494Sbmah *iterp = iter; 11977494Sbmah return (ISC_R_SUCCESS); 12077494Sbmah 12176082Sbmah failure: 12276082Sbmah#ifdef __linux 12377211Sbmah if (iter->proc != NULL) 12486150Solgeni fclose(iter->proc); 12576082Sbmah#endif 12678040Sache if (iter->ifaddrs != NULL) /* just in case */ 12776082Sbmah 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 /* 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 if (iter->pos == NULL) 159 return (linux_if_inet6_current(iter)); 160#endif 161 162 INSIST(ifa != NULL); 163 INSIST(ifa->ifa_name != NULL); 164 165 166#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 if ((ifa->ifa_flags & IFF_RUNNING) == 0) 173 return (ISC_R_IGNORE); 174#endif 175 176 if (ifa->ifa_addr == NULL) 177 return (ISC_R_IGNORE); 178 179 family = ifa->ifa_addr->sa_family; 180 if (family != AF_INET && family != AF_INET6) 181 return (ISC_R_IGNORE); 182 183#ifdef __linux 184 if (family == AF_INET6) 185 seenv6 = ISC_TRUE; 186#endif 187 188 memset(&iter->current, 0, sizeof(iter->current)); 189 190 namelen = strlen(ifa->ifa_name); 191 if (namelen > sizeof(iter->current.name) - 1) 192 namelen = sizeof(iter->current.name) - 1; 193 194 memset(iter->current.name, 0, sizeof(iter->current.name)); 195 memcpy(iter->current.name, ifa->ifa_name, namelen); 196 197 iter->current.flags = 0; 198 199 if ((ifa->ifa_flags & IFF_UP) != 0) 200 iter->current.flags |= INTERFACE_F_UP; 201 202 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) 203 iter->current.flags |= INTERFACE_F_POINTTOPOINT; 204 205 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 206 iter->current.flags |= INTERFACE_F_LOOPBACK; 207 208 if ((ifa->ifa_flags & IFF_BROADCAST) != 0) 209 iter->current.flags |= INTERFACE_F_BROADCAST; 210 211#ifdef IFF_MULTICAST 212 if ((ifa->ifa_flags & IFF_MULTICAST) != 0) 213 iter->current.flags |= INTERFACE_F_MULTICAST; 214#endif 215 216 iter->current.af = family; 217 218 get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name); 219 220 if (ifa->ifa_netmask != NULL) 221 get_addr(family, &iter->current.netmask, ifa->ifa_netmask, 222 ifa->ifa_name); 223 224 if (ifa->ifa_dstaddr != NULL && 225 (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) 226 get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr, 227 ifa->ifa_name); 228 229 if (ifa->ifa_broadaddr != NULL && 230 (iter->current.flags & INTERFACE_F_BROADCAST) != 0) 231 get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr, 232 ifa->ifa_name); 233 234#ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 235 iter->current.ifindex = if_nametoindex(iter->current.name); 236#endif 237 return (ISC_R_SUCCESS); 238} 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 */ 247static isc_result_t 248internal_next(isc_interfaceiter_t *iter) { 249 250 if (iter->pos != NULL) 251 iter->pos = iter->pos->ifa_next; 252 if (iter->pos == NULL) { 253#ifdef __linux 254 if (!seenv6) 255 return (linux_if_inet6_next(iter)); 256#endif 257 return (ISC_R_NOMORE); 258 } 259 260 return (ISC_R_SUCCESS); 261} 262 263static void 264internal_destroy(isc_interfaceiter_t *iter) { 265 266#ifdef __linux 267 if (iter->proc != NULL) 268 fclose(iter->proc); 269 iter->proc = NULL; 270#endif 271 if (iter->ifaddrs) 272 freeifaddrs(iter->ifaddrs); 273 iter->ifaddrs = NULL; 274} 275 276static 277void internal_first(isc_interfaceiter_t *iter) { 278 279#ifdef __linux 280 linux_if_inet6_first(iter); 281#endif 282 iter->pos = iter->ifaddrs; 283} 284