rewrote. Now should be able to handle aliases and IPv6 addresses
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@3452 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -56,6 +56,10 @@ struct mbuf;
|
|||||||
#include <sys/sockio.h>
|
#include <sys/sockio.h>
|
||||||
#endif /* HAVE_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)
|
||||||
{
|
{
|
||||||
@@ -92,133 +96,157 @@ gethostname_fallback (krb5_addresses *res)
|
|||||||
* Include loopback (lo*) interfaces iff loop.
|
* Include loopback (lo*) interfaces iff loop.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS) && defined(SIOCGIFADDR)
|
|
||||||
static krb5_error_code
|
static krb5_error_code
|
||||||
find_all_addresses (krb5_addresses *res, int loop)
|
find_all_addresses (krb5_addresses *res, int loop,
|
||||||
|
int af, int siocgifconf, int siocgifflags,
|
||||||
|
size_t ifreq_sz)
|
||||||
{
|
{
|
||||||
krb5_error_code err;
|
krb5_error_code ret;
|
||||||
int fd;
|
int fd;
|
||||||
char buf[BUFSIZ];
|
size_t buf_size;
|
||||||
struct ifreq ifreq;
|
char *buf;
|
||||||
struct ifconf ifconf;
|
struct ifconf ifconf;
|
||||||
int num, j;
|
int num, j;
|
||||||
char *p;
|
char *p;
|
||||||
size_t sz;
|
size_t sz;
|
||||||
|
struct sockaddr sa_zero;
|
||||||
|
struct ifreq *ifr;
|
||||||
|
|
||||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
buf = NULL;
|
||||||
|
res->val = NULL;
|
||||||
|
|
||||||
|
memset (&sa_zero, 0, sizeof(sa_zero));
|
||||||
|
fd = socket(af, SOCK_DGRAM, 0);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ifconf.ifc_len = sizeof(buf);
|
buf_size = 8192;
|
||||||
ifconf.ifc_buf = buf;
|
do {
|
||||||
if(ioctl(fd, SIOCGIFCONF, &ifconf) < 0)
|
buf = malloc(buf_size);
|
||||||
return -1;
|
if (buf == NULL) {
|
||||||
num = ifconf.ifc_len / sizeof(struct ifreq);
|
ret = ENOMEM;
|
||||||
|
goto error_out;
|
||||||
|
}
|
||||||
|
ifconf.ifc_len = buf_size;
|
||||||
|
ifconf.ifc_buf = buf;
|
||||||
|
if (ioctl (fd, siocgifconf, &ifconf) < 0) {
|
||||||
|
ret = errno;
|
||||||
|
goto error_out;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Can the difference between a full and a overfull buf
|
||||||
|
* be determined?
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (ifconf.ifc_len == buf_size)
|
||||||
|
free (buf);
|
||||||
|
} while (ifconf.ifc_len == buf_size);
|
||||||
|
|
||||||
|
num = ifconf.ifc_len / ifreq_sz;
|
||||||
res->len = num;
|
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) {
|
||||||
close (fd);
|
ret = ENOMEM;
|
||||||
return ENOMEM;
|
goto error_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
ifreq.ifr_name[0] = '\0';
|
|
||||||
for (p = ifconf.ifc_buf;
|
for (p = ifconf.ifc_buf;
|
||||||
p < ifconf.ifc_buf + ifconf.ifc_len;
|
p < ifconf.ifc_buf + ifconf.ifc_len;
|
||||||
p += sz) {
|
p += sz) {
|
||||||
struct ifreq *ifr = (struct ifreq *)p;
|
struct ifreq ifreq;
|
||||||
|
struct sockaddr *sa;
|
||||||
|
|
||||||
/* This is somewhat kludgy, but it seems to work. */
|
ifr = (struct ifreq *)p;
|
||||||
|
sa = &ifr->ifr_addr;
|
||||||
|
|
||||||
sz = sizeof(*ifr);
|
sz = ifreq_sz;
|
||||||
#ifdef SOCKADDR_HAS_SA_LEN
|
#ifdef SOCKADDR_HAS_SA_LEN
|
||||||
if(ifr->ifr_addr.sa_len)
|
sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
|
||||||
sz = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
|
|
||||||
#endif
|
#endif
|
||||||
if(strncmp(ifreq.ifr_name,
|
|
||||||
ifr->ifr_name,
|
|
||||||
sizeof(ifr->ifr_name)) == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(ioctl(fd, SIOCGIFFLAGS, ifr) < 0) {
|
memcpy (ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
|
||||||
close (fd);
|
|
||||||
free (res->val);
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!(ifr->ifr_flags & IFF_UP)
|
if (ioctl(fd, siocgifflags, &ifreq) < 0) {
|
||||||
|| (loop == 0 && (ifr->ifr_flags & IFF_LOOPBACK)))
|
ret = errno;
|
||||||
continue;
|
goto error_out;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
if(!(ifreq.ifr_flags & IFF_UP)
|
||||||
* Get the address of the interface. If this fails, it's
|
|| (!loop && (ifreq.ifr_flags & IFF_LOOPBACK))
|
||||||
* usually because this interface is up, but has no address
|
|| memcmp (sa, &sa_zero, sizeof(sa_zero)) == 0)
|
||||||
* configured. There might be some fatal errors for which
|
continue;
|
||||||
* we should bail out, but now we prefer ignoring them.
|
|
||||||
*/
|
|
||||||
if(ioctl(fd, SIOCGIFADDR, ifr) < 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
switch (ifr->ifr_addr.sa_family) {
|
switch (sa->sa_family) {
|
||||||
#ifdef AF_INET
|
#ifdef AF_INET
|
||||||
case AF_INET: {
|
case AF_INET: {
|
||||||
unsigned char addr[4];
|
unsigned char addr[4];
|
||||||
struct sockaddr_in *sin;
|
struct sockaddr_in *sin;
|
||||||
res->val[j].addr_type = AF_INET;
|
res->val[j].addr_type = AF_INET;
|
||||||
/* This is somewhat XXX */
|
/* This is somewhat XXX */
|
||||||
sin = (struct sockaddr_in*)&ifr->ifr_addr;
|
sin = (struct sockaddr_in*)sa;
|
||||||
memcpy(addr, &sin->sin_addr, 4);
|
memcpy(addr, &sin->sin_addr, 4);
|
||||||
err = krb5_data_copy(&res->val[j].address,
|
ret = krb5_data_copy(&res->val[j].address,
|
||||||
addr, 4);
|
addr, 4);
|
||||||
if (err) {
|
if (ret)
|
||||||
close (fd);
|
goto error_out;
|
||||||
free (res->val);
|
++j;
|
||||||
return ENOMEM;
|
break;
|
||||||
}
|
}
|
||||||
++j;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#endif /* AF_INET */
|
#endif /* AF_INET */
|
||||||
|
|
||||||
/*
|
|
||||||
* This is not an correct nor ideal test.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(AF_INET6) && defined(HAVE_NETINET_IN6_H)
|
#if defined(AF_INET6) && defined(HAVE_NETINET_IN6_H)
|
||||||
case AF_INET6: {
|
case AF_INET6: {
|
||||||
res->val[j].addr_type = AF_INET6;
|
struct in6_addr *sin6;
|
||||||
err = krb5_data_copy(&res->val[j].address,
|
|
||||||
&ifr->ifr_addr,
|
sin6 = &((struct sockaddr_in6 *)(&ifr->ifr_addr))->sin6_addr;
|
||||||
sizeof(struct sockaddr_in6));
|
|
||||||
if (err) {
|
if (IN6_LOOPBACK(*sin6) || IN6_LINK_LOCAL(*sin6)) {
|
||||||
close (fd);
|
break;
|
||||||
free (res->val);
|
} else if (IN6_IN4COMPAT(*sin6)) {
|
||||||
return ENOMEM;
|
res->val[j].addr_type = AF_INET;
|
||||||
}
|
ret = krb5_data_copy(&res->val[j].address,
|
||||||
++j;
|
&IN6_IN4ADDR(*sin6),
|
||||||
break;
|
sizeof(struct in_addr));
|
||||||
}
|
} else {
|
||||||
|
res->val[j].addr_type = AF_INET6;
|
||||||
|
ret = krb5_data_copy(&res->val[j].address,
|
||||||
|
sin6,
|
||||||
|
sizeof(struct in6_addr));
|
||||||
|
}
|
||||||
|
if (ret)
|
||||||
|
goto error_out;
|
||||||
|
++j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
#endif /* AF_INET6 */
|
#endif /* AF_INET6 */
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ifreq = *ifr;
|
|
||||||
}
|
}
|
||||||
close (fd);
|
|
||||||
if (j != num) {
|
if (j != num) {
|
||||||
void *tmp;
|
void *tmp;
|
||||||
|
|
||||||
res->len = j;
|
res->len = j;
|
||||||
tmp = realloc (res->val, j * sizeof(*res->val));
|
tmp = realloc (res->val, j * sizeof(*res->val));
|
||||||
if (tmp == NULL) {
|
if (j != 0 && tmp == NULL) {
|
||||||
free (res->val);
|
ret = ENOMEM;
|
||||||
return ENOMEM;
|
goto error_out;
|
||||||
}
|
}
|
||||||
res->val = tmp;
|
res->val = tmp;
|
||||||
}
|
}
|
||||||
return 0;
|
ret = 0;
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
error_out:
|
||||||
|
while(j--) {
|
||||||
|
krb5_data_free (&res->val[j].address);
|
||||||
|
}
|
||||||
|
free (res->val);
|
||||||
|
cleanup:
|
||||||
|
close (fd);
|
||||||
|
free (buf);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
#endif /* SIOCGIFCONF */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to get all addresses, but return the one corresponding to
|
* Try to get all addresses, but return the one corresponding to
|
||||||
@@ -231,11 +259,17 @@ find_all_addresses (krb5_addresses *res, int loop)
|
|||||||
krb5_error_code
|
krb5_error_code
|
||||||
krb5_get_all_client_addrs (krb5_addresses *res)
|
krb5_get_all_client_addrs (krb5_addresses *res)
|
||||||
{
|
{
|
||||||
#if !defined(SIOCGIFCONF) || !defined(SIOCGIFFLAGS) || !defined(SIOCGIFADDR)
|
#if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
|
||||||
return gethostname_fallback (res);
|
return find_all_addresses (res, 1,
|
||||||
|
AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
|
||||||
|
sizeof(struct in6_ifreq));
|
||||||
|
#elif defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
|
||||||
|
return find_all_addresses (res, 0,
|
||||||
|
AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
|
||||||
|
sizeof(struct ifreq));
|
||||||
#else
|
#else
|
||||||
return find_all_addresses (res, 0);
|
return gethostname_fallback (res);
|
||||||
#endif /* SIOCGIFCONF */
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user