ifiter_getifaddrs.c revision 0a7e6db2db81e8ca518ab9d5620d69fe1b1837f5
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Copyright (C) 2003 Internet Software Consortium.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
f97d56e757b9a293ffbaa915ca4d792ae84ba85aTinderbox User * PERFORMANCE OF THIS SOFTWARE.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/* $Id: ifiter_getifaddrs.c,v 1.11 2008/03/20 23:47:00 tbox Exp $ */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Obtain the list of network interfaces using the getifaddrs(3) library.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt/*% Iterator Magic */
3d751891410f9892ca1c1deba2f7d8556ae91b0cMark Andrews/*% Valid Iterator */
ac5ed748602c890d596bed07b0b23b8b5f42b2f6Mark Andrews#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/*% Iterator structure */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews struct ifaddrs *ifaddrs; /*%< List of ifaddrs */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews struct ifaddrs *pos; /*%< Ptr to current ifaddr */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_interface_t current; /*%< Current interface data. */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver * Only open "/proc/net/if_inet6" if we have never seen a IPv6
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * address returned by getifaddrs().
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver iter->proc = fopen("/proc/net/if_inet6", "r");
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc__strerror(errno, strbuf, sizeof(strbuf));
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews "getting interface "
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt "addresses: getifaddrs: %s"),
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * A newly created iterator has an undefined position
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * until isc_interfaceiter_first() is called.
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (iter->ifaddrs != NULL) /* just in case */
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver * Get information about the current interface to iter->current.
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver * If successful, return ISC_R_SUCCESS.
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver * If the interface has an unsupported address family,
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver * return ISC_R_IGNORE.
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverinternal_current(isc_interfaceiter_t *iter) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt memset(iter->current.name, 0, sizeof(iter->current.name));
3a26e75e3c475b6f2f34716fa2847bf015db57c5Mark Andrews memcpy(iter->current.name, ifa->ifa_name, namelen);
3a26e75e3c475b6f2f34716fa2847bf015db57c5Mark Andrews iter->current.flags |= INTERFACE_F_POINTTOPOINT;
3a26e75e3c475b6f2f34716fa2847bf015db57c5Mark Andrews get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver get_addr(family, &iter->current.netmask, ifa->ifa_netmask,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (iter->current.flags & IFF_POINTOPOINT) != 0)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * Step the iterator to the next interface. Unlike
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * isc_interfaceiter_next(), this may leave the iterator
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * positioned on an interface that will ultimately
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * be ignored. Return ISC_R_NOMORE if there are no more
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * interfaces, otherwise ISC_R_SUCCESS.