(find_all_addresses): use getifaddrs, from Jason Thorpe <thorpej@netbsd.org>

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@9263 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
2000-12-05 05:42:18 +00:00
parent 419ef60ec7
commit a1366b4974

View File

@@ -43,14 +43,7 @@ struct mbuf;
#ifdef HAVE_NET_IF_H #ifdef HAVE_NET_IF_H
#include <net/if.h> #include <net/if.h>
#endif #endif
#include <ifaddrs.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif /* HAVE_SYS_SOCKIO_H */
#ifdef HAVE_NETINET_IN6_VAR_H
#include <netinet/in6_var.h>
#endif /* HAVE_NETINET_IN6_VAR_H */
static krb5_error_code static krb5_error_code
gethostname_fallback (krb5_addresses *res) gethostname_fallback (krb5_addresses *res)
@@ -94,147 +87,96 @@ enum {
*/ */
static krb5_error_code static krb5_error_code
find_all_addresses (krb5_context context, find_all_addresses (krb5_context context, krb5_addresses *res, int flags)
krb5_addresses *res, int flags,
int af, int siocgifconf, int siocgifflags,
size_t ifreq_sz)
{ {
krb5_error_code ret;
int fd;
size_t buf_size;
char *buf;
struct ifconf ifconf;
int num, j = 0;
char *p;
size_t sz;
struct sockaddr sa_zero; struct sockaddr sa_zero;
struct ifreq *ifr; struct ifaddrs *ifa0, *ifa;
krb5_address lo_addr; krb5_error_code ret;
int got_lo = FALSE; int num, idx;
buf = NULL;
res->val = NULL; res->val = NULL;
memset (&sa_zero, 0, sizeof(sa_zero)); if (getifaddrs(&ifa0) == -1)
fd = socket(af, SOCK_DGRAM, 0); return (errno);
if (fd < 0)
return -1;
buf_size = 8192; memset(&sa_zero, 0, sizeof(sa_zero));
for (;;) {
buf = malloc(buf_size);
if (buf == NULL) {
ret = ENOMEM;
goto error_out;
}
ifconf.ifc_len = buf_size;
ifconf.ifc_buf = buf;
/* /* First, count all the ifaddrs. */
* Solaris returns EINVAL when the buffer is too small. for (ifa = ifa0, num = 0; ifa != NULL; ifa = ifa->ifa_next, num++)
*/ /* nothing */;
if (ioctl (fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
ret = errno;
goto error_out;
}
/*
* Can the difference between a full and a overfull buf
* be determined?
*/
if (ifconf.ifc_len < buf_size) if (num == 0) {
break; freeifaddrs(ifa0);
free (buf); return (ENXIO);
buf_size *= 2;
} }
num = ifconf.ifc_len / ifreq_sz; /* Allocate storage for them. */
res->len = num;
res->val = calloc(num, sizeof(*res->val)); res->val = calloc(num, sizeof(*res->val));
if (res->val == NULL) { if (res->val == NULL) {
ret = ENOMEM; freeifaddrs(ifa0);
goto error_out; return (ENOMEM);
} }
j = 0; /* Now traverse the list. */
for (p = ifconf.ifc_buf; for (ifa = ifa0, idx = 0; ifa != NULL; ifa = ifa->ifa_next) {
p < ifconf.ifc_buf + ifconf.ifc_len; if ((ifa->ifa_flags & IFF_UP) == 0)
p += sz) { continue;
struct ifreq ifreq; if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
struct sockaddr *sa; continue;
if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
continue;
ifr = (struct ifreq *)p; if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
sa = &ifr->ifr_addr; /* We'll deal with the LOOP_IF_NONE case later. */
if ((flags & LOOP) == 0)
sz = ifreq_sz; continue;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
#endif
#ifdef SA_LEN
sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
#endif
memcpy (ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
if (ioctl(fd, siocgifflags, &ifreq) < 0) {
ret = errno;
goto error_out;
} }
if (!(ifreq.ifr_flags & IFF_UP)) ret = krb5_sockaddr2address(ifa->ifa_addr, &res->val[idx]);
if (ret) {
/*
* The most likely error here is going to be "Program
* lacks support for address type". This is no big
* deal -- just continue, and we'll listen on the
* addresses who's type we *do* support.
*/
continue; continue;
if (memcmp (sa, &sa_zero, sizeof(sa_zero)) == 0) }
idx++;
}
/*
* If no addresses were found, and LOOP_IF_NONE is set, then find
* the loopback addresses and add them to our list.
*/
if ((flags & LOOP_IF_NONE) != 0 && idx == 0) {
for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
if ((ifa->ifa_flags & IFF_UP) == 0)
continue; continue;
if (krb5_sockaddr_uninteresting (sa)) if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
continue;
if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
continue; continue;
if (ifreq.ifr_flags & IFF_LOOPBACK) { if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) {
if (flags & LOOP_IF_NONE) { ret = krb5_sockaddr2address(ifa->ifa_addr, &res->val[idx]);
ret = krb5_sockaddr2address (sa, &lo_addr); if (ret) {
/*
* See comment above.
*/
continue;
}
idx++;
}
}
}
freeifaddrs(ifa0);
if (ret) if (ret)
goto error_out; free(res->val);
got_lo = TRUE;
continue;
} else if((flags & LOOP) == 0)
continue;
}
ret = krb5_sockaddr2address (sa, &res->val[j]);
if (ret)
goto error_out;
++j;
}
if ((flags & LOOP_IF_NONE) && got_lo) {
if (j == 0)
res->val[j++] = lo_addr;
else else
krb5_free_address (context, &lo_addr); res->len = idx; /* Now a count. */
} return (ret);
if (j != num) {
void *tmp;
res->len = j;
tmp = realloc (res->val, j * sizeof(*res->val));
if (j != 0 && tmp == NULL) {
ret = ENOMEM;
goto error_out;
}
res->val = tmp;
}
ret = 0;
goto cleanup;
error_out:
if (got_lo)
krb5_free_address (context, &lo_addr);
while(j--) {
krb5_free_address (context, &res->val[j]);
}
free (res->val);
cleanup:
close (fd);
free (buf);
return ret;
} }
static krb5_error_code static krb5_error_code
@@ -243,26 +185,9 @@ get_addrs_int (krb5_context context, krb5_addresses *res, int flags)
krb5_error_code ret = -1; krb5_error_code ret = -1;
if (flags & SCAN_INTERFACES) { if (flags & SCAN_INTERFACES) {
#if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS) ret = find_all_addresses (context, res, flags);
if (ret)
ret = find_all_addresses (context, res, flags,
AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
sizeof(struct in6_ifreq));
#endif
#if defined(HAVE_IPV6) && defined(SIOCGIFCONF)
if (ret)
ret = find_all_addresses (context, res, flags,
AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
sizeof(struct ifreq));
#endif
#if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
if (ret)
ret = find_all_addresses (context, res, flags,
AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
sizeof(struct ifreq));
if(ret || res->len == 0) if(ret || res->len == 0)
ret = gethostname_fallback (res); ret = gethostname_fallback (res);
#endif
} else } else
ret = 0; ret = 0;