2342. [func] Use getifaddrs() if available under Linux. [RT #17224]
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: ifiter_getifaddrs.c,v 1.9 2007/06/18 23:47:48 tbox Exp $ */
|
||||
/* $Id: ifiter_getifaddrs.c,v 1.10 2008/03/20 04:50:02 marka Exp $ */
|
||||
|
||||
/*! \file
|
||||
* \brief
|
||||
@@ -29,6 +29,10 @@
|
||||
/*% Valid Iterator */
|
||||
#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
|
||||
|
||||
#ifdef __linux
|
||||
static isc_boolean_t seenv6 = ISC_FALSE;
|
||||
#endif
|
||||
|
||||
/*% Iterator structure */
|
||||
struct isc_interfaceiter {
|
||||
unsigned int magic; /*%< Magic number. */
|
||||
@@ -39,9 +43,13 @@ struct isc_interfaceiter {
|
||||
struct ifaddrs *pos; /*%< Ptr to current ifaddr */
|
||||
isc_interface_t current; /*%< Current interface data. */
|
||||
isc_result_t result; /*%< Last result code. */
|
||||
#ifdef __linux
|
||||
FILE * proc;
|
||||
char entry[ISC_IF_INET6_SZ];
|
||||
isc_result_t valid;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
|
||||
isc_interfaceiter_t *iter;
|
||||
@@ -60,6 +68,17 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
|
||||
iter->buf = NULL;
|
||||
iter->bufsize = 0;
|
||||
iter->ifaddrs = NULL;
|
||||
#ifdef __linux
|
||||
/*
|
||||
* Only open "/proc/net/if_inet6" if we have never seen a IPv6
|
||||
* address returned by getifaddrs().
|
||||
*/
|
||||
if (!seenv6)
|
||||
iter->proc = fopen("/proc/net/if_inet6", "r");
|
||||
else
|
||||
iter->proc = NULL;
|
||||
iter->valid = ISC_R_FAILURE;
|
||||
#endif
|
||||
|
||||
if (getifaddrs(&iter->ifaddrs) < 0) {
|
||||
isc__strerror(errno, strbuf, sizeof(strbuf));
|
||||
@@ -86,6 +105,10 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
failure:
|
||||
#ifdef __linux
|
||||
if (iter->proc != NULL)
|
||||
fclose(iter->proc);
|
||||
#endif
|
||||
if (iter->ifaddrs != NULL) /* just in case */
|
||||
freeifaddrs(iter->ifaddrs);
|
||||
isc_mem_put(mctx, iter, sizeof(*iter));
|
||||
@@ -109,6 +132,11 @@ internal_current(isc_interfaceiter_t *iter) {
|
||||
|
||||
ifa = iter->pos;
|
||||
|
||||
#ifdef __linux
|
||||
if (iter->pos == NULL)
|
||||
return (linux_if_inet6_current(iter));
|
||||
#endif
|
||||
|
||||
INSIST(ifa != NULL);
|
||||
INSIST(ifa->ifa_name != NULL);
|
||||
|
||||
@@ -119,6 +147,11 @@ internal_current(isc_interfaceiter_t *iter) {
|
||||
if (family != AF_INET && family != AF_INET6)
|
||||
return (ISC_R_IGNORE);
|
||||
|
||||
#ifdef __linux
|
||||
if (family == AF_INET6)
|
||||
seenv6 = ISC_TRUE;
|
||||
#endif
|
||||
|
||||
memset(&iter->current, 0, sizeof(iter->current));
|
||||
|
||||
namelen = strlen(ifa->ifa_name);
|
||||
@@ -164,16 +197,28 @@ internal_current(isc_interfaceiter_t *iter) {
|
||||
*/
|
||||
static isc_result_t
|
||||
internal_next(isc_interfaceiter_t *iter) {
|
||||
iter->pos = iter->pos->ifa_next;
|
||||
|
||||
if (iter->pos == NULL)
|
||||
if (iter->pos != NULL)
|
||||
iter->pos = iter->pos->ifa_next;
|
||||
if (iter->pos == NULL) {
|
||||
#ifdef __linux
|
||||
if (!seenv6)
|
||||
return (linux_if_inet6_next(iter));
|
||||
#endif
|
||||
return (ISC_R_NOMORE);
|
||||
}
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
static void
|
||||
internal_destroy(isc_interfaceiter_t *iter) {
|
||||
|
||||
#ifdef __linux
|
||||
if (iter->proc != NULL)
|
||||
fclose(iter->proc);
|
||||
iter->proc = NULL;
|
||||
#endif
|
||||
if (iter->ifaddrs)
|
||||
freeifaddrs(iter->ifaddrs);
|
||||
iter->ifaddrs = NULL;
|
||||
@@ -181,5 +226,9 @@ internal_destroy(isc_interfaceiter_t *iter) {
|
||||
|
||||
static
|
||||
void internal_first(isc_interfaceiter_t *iter) {
|
||||
|
||||
#ifdef __linux
|
||||
linux_if_inet6_first(iter);
|
||||
#endif
|
||||
iter->pos = iter->ifaddrs;
|
||||
}
|
||||
|
||||
+23
-121
@@ -15,7 +15,7 @@
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: ifiter_ioctl.c,v 1.58 2007/08/30 06:02:28 marka Exp $ */
|
||||
/* $Id: ifiter_ioctl.c,v 1.59 2008/03/20 04:50:02 marka Exp $ */
|
||||
|
||||
/*! \file
|
||||
* \brief
|
||||
@@ -50,9 +50,6 @@
|
||||
#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'T')
|
||||
#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
|
||||
|
||||
#define ISC_IF_INET6_SZ \
|
||||
sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
|
||||
|
||||
struct isc_interfaceiter {
|
||||
unsigned int magic; /* Magic number. */
|
||||
isc_mem_t *mctx;
|
||||
@@ -82,7 +79,6 @@ struct isc_interfaceiter {
|
||||
FILE * proc;
|
||||
char entry[ISC_IF_INET6_SZ];
|
||||
isc_result_t valid;
|
||||
isc_boolean_t first;
|
||||
#endif
|
||||
isc_interface_t current; /* Current interface data. */
|
||||
isc_result_t result; /* Last result code. */
|
||||
@@ -372,7 +368,6 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
|
||||
#ifdef __linux
|
||||
iter->proc = fopen("/proc/net/if_inet6", "r");
|
||||
iter->valid = ISC_R_FAILURE;
|
||||
iter->first = ISC_FALSE;
|
||||
#endif
|
||||
iter->result = ISC_R_FAILURE;
|
||||
|
||||
@@ -422,89 +417,6 @@ internal_current_clusteralias(isc_interfaceiter_t *iter) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __linux
|
||||
static isc_result_t
|
||||
linux_if_inet6_next(isc_interfaceiter_t *iter) {
|
||||
if (iter->proc != NULL &&
|
||||
fgets(iter->entry, sizeof(iter->entry), iter->proc) != NULL)
|
||||
iter->valid = ISC_R_SUCCESS;
|
||||
else
|
||||
iter->valid = ISC_R_NOMORE;
|
||||
return (iter->valid);
|
||||
}
|
||||
|
||||
static void
|
||||
linux_if_inet6_first(isc_interfaceiter_t *iter) {
|
||||
if (iter->proc != NULL) {
|
||||
rewind(iter->proc);
|
||||
(void)linux_if_inet6_next(iter);
|
||||
} else
|
||||
iter->valid = ISC_R_NOMORE;
|
||||
iter->first = ISC_FALSE;
|
||||
}
|
||||
|
||||
static isc_result_t
|
||||
linux_if_inet6_current(isc_interfaceiter_t *iter) {
|
||||
char address[33];
|
||||
char name[IF_NAMESIZE+1];
|
||||
struct in6_addr addr6;
|
||||
int ifindex, prefix, flag3, flag4;
|
||||
int res;
|
||||
unsigned int i;
|
||||
|
||||
if (iter->valid != ISC_R_SUCCESS)
|
||||
return (iter->valid);
|
||||
if (iter->proc == NULL) {
|
||||
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
||||
ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
|
||||
"/proc/net/if_inet6:iter->proc == NULL");
|
||||
return (ISC_R_FAILURE);
|
||||
}
|
||||
|
||||
res = sscanf(iter->entry, "%32[a-f0-9] %x %x %x %x %16s\n",
|
||||
address, &ifindex, &prefix, &flag3, &flag4, name);
|
||||
if (res != 6) {
|
||||
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
||||
ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
|
||||
"/proc/net/if_inet6:sscanf() -> %d (expected 6)",
|
||||
res);
|
||||
return (ISC_R_FAILURE);
|
||||
}
|
||||
if (strlen(address) != 32) {
|
||||
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
||||
ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
|
||||
"/proc/net/if_inet6:strlen(%s) != 32", address);
|
||||
return (ISC_R_FAILURE);
|
||||
}
|
||||
for (i = 0; i < 16; i++) {
|
||||
unsigned char byte;
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
byte = ((index(hex, address[i * 2]) - hex) << 4) |
|
||||
(index(hex, address[i * 2 + 1]) - hex);
|
||||
addr6.s6_addr[i] = byte;
|
||||
}
|
||||
iter->current.af = AF_INET6;
|
||||
iter->current.flags = INTERFACE_F_UP;
|
||||
isc_netaddr_fromin6(&iter->current.address, &addr6);
|
||||
if (isc_netaddr_islinklocal(&iter->current.address)) {
|
||||
isc_netaddr_setzone(&iter->current.address,
|
||||
(isc_uint32_t)ifindex);
|
||||
}
|
||||
for (i = 0; i < 16; i++) {
|
||||
if (prefix > 8) {
|
||||
addr6.s6_addr[i] = 0xff;
|
||||
prefix -= 8;
|
||||
} else {
|
||||
addr6.s6_addr[i] = (0xff << (8 - prefix)) & 0xff;
|
||||
prefix = 0;
|
||||
}
|
||||
}
|
||||
isc_netaddr_fromin6(&iter->current.netmask, &addr6);
|
||||
strncpy(iter->current.name, name, sizeof(iter->current.name));
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Get information about the current interface to iter->current.
|
||||
* If successful, return ISC_R_SUCCESS.
|
||||
@@ -525,23 +437,19 @@ internal_current4(isc_interfaceiter_t *iter) {
|
||||
char sabuf[256];
|
||||
#endif
|
||||
int i, bits, prefixlen;
|
||||
#ifdef __linux
|
||||
isc_result_t result;
|
||||
#endif
|
||||
|
||||
REQUIRE(VALID_IFITER(iter));
|
||||
REQUIRE(iter->ifc.ifc_len == 0 ||
|
||||
iter->pos < (unsigned int) iter->ifc.ifc_len);
|
||||
|
||||
if (iter->ifc.ifc_len == 0 ||
|
||||
iter->pos == (unsigned int)iter->ifc.ifc_len) {
|
||||
#ifdef __linux
|
||||
result = linux_if_inet6_current(iter);
|
||||
if (result != ISC_R_NOMORE)
|
||||
return (result);
|
||||
iter->first = ISC_TRUE;
|
||||
#endif
|
||||
|
||||
if (iter->ifc.ifc_len == 0)
|
||||
return (linux_if_inet6_current(iter));
|
||||
#else
|
||||
return (ISC_R_NOMORE);
|
||||
#endif
|
||||
}
|
||||
|
||||
INSIST( iter->pos < (unsigned int) iter->ifc.ifc_len);
|
||||
|
||||
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
|
||||
|
||||
@@ -905,31 +813,25 @@ internal_next4(isc_interfaceiter_t *iter) {
|
||||
struct ifreq *ifrp;
|
||||
#endif
|
||||
|
||||
REQUIRE(iter->ifc.ifc_len == 0 ||
|
||||
iter->pos < (unsigned int) iter->ifc.ifc_len);
|
||||
|
||||
#ifdef __linux
|
||||
if (linux_if_inet6_next(iter) == ISC_R_SUCCESS)
|
||||
return (ISC_R_SUCCESS);
|
||||
if (!iter->first)
|
||||
return (ISC_R_SUCCESS);
|
||||
#endif
|
||||
|
||||
if (iter->ifc.ifc_len == 0)
|
||||
return (ISC_R_NOMORE);
|
||||
|
||||
if (iter->pos < (unsigned int) iter->ifc.ifc_len) {
|
||||
#ifdef ISC_PLATFORM_HAVESALEN
|
||||
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
|
||||
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
|
||||
|
||||
if (ifrp->ifr_addr.sa_len > sizeof(struct sockaddr))
|
||||
iter->pos += sizeof(ifrp->ifr_name) + ifrp->ifr_addr.sa_len;
|
||||
else
|
||||
if (ifrp->ifr_addr.sa_len > sizeof(struct sockaddr))
|
||||
iter->pos += sizeof(ifrp->ifr_name) +
|
||||
ifrp->ifr_addr.sa_len;
|
||||
else
|
||||
#endif
|
||||
iter->pos += sizeof(struct ifreq);
|
||||
iter->pos += sizeof(struct ifreq);
|
||||
|
||||
if (iter->pos >= (unsigned int) iter->ifc.ifc_len)
|
||||
} else {
|
||||
INSIST(iter->pos == (unsigned int) iter->ifc.ifc_len);
|
||||
#ifdef __linux
|
||||
return (linux_if_inet6_next(iter));
|
||||
#else
|
||||
return (ISC_R_NOMORE);
|
||||
|
||||
#endif
|
||||
}
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: interfaceiter.c,v 1.42 2007/06/19 23:47:18 tbox Exp $ */
|
||||
/* $Id: interfaceiter.c,v 1.43 2008/03/20 04:50:02 marka Exp $ */
|
||||
|
||||
/*! \file */
|
||||
|
||||
@@ -145,6 +145,14 @@ get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
|
||||
* Include system-dependent code.
|
||||
*/
|
||||
|
||||
#ifdef __linux
|
||||
#define ISC_IF_INET6_SZ \
|
||||
sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
|
||||
static isc_result_t linux_if_inet6_next(isc_interfaceiter_t *);
|
||||
static isc_result_t linux_if_inet6_current(isc_interfaceiter_t *);
|
||||
static void linux_if_inet6_first(isc_interfaceiter_t *iter);
|
||||
#endif
|
||||
|
||||
#if HAVE_GETIFADDRS
|
||||
#include "ifiter_getifaddrs.c"
|
||||
#elif HAVE_IFLIST_SYSCTL
|
||||
@@ -153,6 +161,88 @@ get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
|
||||
#include "ifiter_ioctl.c"
|
||||
#endif
|
||||
|
||||
#ifdef __linux
|
||||
static void
|
||||
linux_if_inet6_first(isc_interfaceiter_t *iter) {
|
||||
if (iter->proc != NULL) {
|
||||
rewind(iter->proc);
|
||||
(void)linux_if_inet6_next(iter);
|
||||
} else
|
||||
iter->valid = ISC_R_NOMORE;
|
||||
}
|
||||
|
||||
static isc_result_t
|
||||
linux_if_inet6_next(isc_interfaceiter_t *iter) {
|
||||
if (iter->proc != NULL &&
|
||||
fgets(iter->entry, sizeof(iter->entry), iter->proc) != NULL)
|
||||
iter->valid = ISC_R_SUCCESS;
|
||||
else
|
||||
iter->valid = ISC_R_NOMORE;
|
||||
return (iter->valid);
|
||||
}
|
||||
|
||||
static isc_result_t
|
||||
linux_if_inet6_current(isc_interfaceiter_t *iter) {
|
||||
char address[33];
|
||||
char name[IF_NAMESIZE+1];
|
||||
struct in6_addr addr6;
|
||||
int ifindex, prefix, flag3, flag4;
|
||||
int res;
|
||||
unsigned int i;
|
||||
|
||||
if (iter->valid != ISC_R_SUCCESS)
|
||||
return (iter->valid);
|
||||
if (iter->proc == NULL) {
|
||||
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
||||
ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
|
||||
"/proc/net/if_inet6:iter->proc == NULL");
|
||||
return (ISC_R_FAILURE);
|
||||
}
|
||||
|
||||
res = sscanf(iter->entry, "%32[a-f0-9] %x %x %x %x %16s\n",
|
||||
address, &ifindex, &prefix, &flag3, &flag4, name);
|
||||
if (res != 6) {
|
||||
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
||||
ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
|
||||
"/proc/net/if_inet6:sscanf() -> %d (expected 6)",
|
||||
res);
|
||||
return (ISC_R_FAILURE);
|
||||
}
|
||||
if (strlen(address) != 32) {
|
||||
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
||||
ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
|
||||
"/proc/net/if_inet6:strlen(%s) != 32", address);
|
||||
return (ISC_R_FAILURE);
|
||||
}
|
||||
for (i = 0; i < 16; i++) {
|
||||
unsigned char byte;
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
byte = ((index(hex, address[i * 2]) - hex) << 4) |
|
||||
(index(hex, address[i * 2 + 1]) - hex);
|
||||
addr6.s6_addr[i] = byte;
|
||||
}
|
||||
iter->current.af = AF_INET6;
|
||||
iter->current.flags = INTERFACE_F_UP;
|
||||
isc_netaddr_fromin6(&iter->current.address, &addr6);
|
||||
if (isc_netaddr_islinklocal(&iter->current.address)) {
|
||||
isc_netaddr_setzone(&iter->current.address,
|
||||
(isc_uint32_t)ifindex);
|
||||
}
|
||||
for (i = 0; i < 16; i++) {
|
||||
if (prefix > 8) {
|
||||
addr6.s6_addr[i] = 0xff;
|
||||
prefix -= 8;
|
||||
} else {
|
||||
addr6.s6_addr[i] = (0xff << (8 - prefix)) & 0xff;
|
||||
prefix = 0;
|
||||
}
|
||||
}
|
||||
isc_netaddr_fromin6(&iter->current.netmask, &addr6);
|
||||
strncpy(iter->current.name, name, sizeof(iter->current.name));
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* The remaining code is common to the sysctl and ioctl case.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user