Commit 826e6012 authored by Dafydd Harries's avatar Dafydd Harries

use getifaddrs() instead of ioctl() for added portability/IPv6 support

darcs-hash:20070210180751-c9803-a2df61f48025dec22d5dc10b0fb0228e38f32785.gz
parent 6fc76a70
#include <glib.h> #include <glib.h>
#include <sys/ioctl.h> #include <arpa/inet.h>
#include <net/if.h> #include <ifaddrs.h>
#include <netinet/in.h>
#include <unistd.h>
#include "local.h" #include "local.h"
...@@ -24,45 +22,37 @@ nice_interface_free (NiceInterface *iface) ...@@ -24,45 +22,37 @@ nice_interface_free (NiceInterface *iface)
GSList * GSList *
nice_list_local_interfaces () nice_list_local_interfaces ()
{ {
char buf[1024];
gint sock;
guint i;
GSList *ret = NULL; GSList *ret = NULL;
struct ifconf ifc; struct ifaddrs *ifs;
struct ifaddrs *i;
sock = socket (PF_INET, SOCK_DGRAM, 0); getifaddrs (&ifs);
if (sock < 0) for (i = ifs; i; i = i->ifa_next)
return NULL; {
struct sockaddr_in *addr;
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
if (ioctl (sock, SIOCGIFCONF, &ifc) < 0)
return NULL;
/* XXX: test case where ifc.ifc_len == sizeof (buf) (overflow) */ addr = (struct sockaddr_in *) i->ifa_addr;
/* XXX: support IPv6 */
for (i = 0; i < ifc.ifc_len / sizeof (struct ifreq); i++) if (addr->sin_family == AF_INET || addr->sin_family == AF_INET6)
{ {
struct ifreq *ifr = ifc.ifc_req + i;
struct sockaddr_in *sin;
NiceInterface *iface; NiceInterface *iface;
if (ifr->ifr_addr.sa_family != AF_INET)
/* this probably shouldn't happen */
continue;
iface = g_slice_new0 (NiceInterface); iface = g_slice_new0 (NiceInterface);
iface->name = g_strdup (ifr->ifr_name); iface->name = g_strdup (i->ifa_name);
if (addr->sin_family == AF_INET)
nice_address_set_ipv4 (&iface->addr,
ntohl (addr->sin_addr.s_addr));
else
nice_address_set_ipv6 (&iface->addr,
(gchar *) &((struct sockaddr_in6 *) addr)->sin6_addr);
sin = (struct sockaddr_in *) &(ifr->ifr_addr);
nice_address_set_ipv4 (&iface->addr, ntohl (sin->sin_addr.s_addr));
ret = g_slist_append (ret, iface); ret = g_slist_append (ret, iface);
} }
}
close (sock); freeifaddrs (ifs);
return ret; return ret;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment