Commit 588a0191 authored by Olivier Crête's avatar Olivier Crête

interfaces: Also return IPv6 addresses

parent f1775923
...@@ -68,12 +68,14 @@ nice_interfaces_get_local_interfaces (void) ...@@ -68,12 +68,14 @@ nice_interfaces_get_local_interfaces (void)
if ((ifa->ifa_flags & IFF_UP) == 0) if ((ifa->ifa_flags & IFF_UP) == 0)
continue; continue;
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) if (ifa->ifa_addr == NULL)
continue; continue;
if (ifa->ifa_addr->sa_family == AF_INET || ifa->ifa_addr->sa_family == AF_INET6) {
nice_debug ("Found interface : %s", ifa->ifa_name); nice_debug ("Found interface : %s", ifa->ifa_name);
interfaces = g_list_prepend (interfaces, g_strdup (ifa->ifa_name)); interfaces = g_list_prepend (interfaces, g_strdup (ifa->ifa_name));
} }
}
freeifaddrs (results); freeifaddrs (results);
...@@ -137,23 +139,27 @@ nice_interfaces_get_local_interfaces (void) ...@@ -137,23 +139,27 @@ nice_interfaces_get_local_interfaces (void)
static gboolean static gboolean
nice_interfaces_is_private_ip (const struct in_addr in) nice_interfaces_is_private_ip (const struct sockaddr *sa)
{ {
if (sa->sa_family == AF_INET) {
struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
/* 10.x.x.x/8 */ /* 10.x.x.x/8 */
if (in.s_addr >> 24 == 0x0A) if (sa4->sin_addr.s_addr >> 24 == 0x0A)
return TRUE; return TRUE;
/* 172.16.0.0 - 172.31.255.255 = 172.16.0.0/10 */ /* 172.16.0.0 - 172.31.255.255 = 172.16.0.0/10 */
if (in.s_addr >> 20 == 0xAC1) if (sa4->sin_addr.s_addr >> 20 == 0xAC1)
return TRUE; return TRUE;
/* 192.168.x.x/16 */ /* 192.168.x.x/16 */
if (in.s_addr >> 16 == 0xC0A8) if (sa4->sin_addr.s_addr >> 16 == 0xC0A8)
return TRUE; return TRUE;
/* 169.254.x.x/16 (for APIPA) */ /* 169.254.x.x/16 (for APIPA) */
if (in.s_addr >> 16 == 0xA9FE) if (sa4->sin_addr.s_addr >> 16 == 0xA9FE)
return TRUE; return TRUE;
}
return FALSE; return FALSE;
} }
...@@ -164,7 +170,6 @@ GList * ...@@ -164,7 +170,6 @@ GList *
nice_interfaces_get_local_ips (gboolean include_loopback) nice_interfaces_get_local_ips (gboolean include_loopback)
{ {
GList *ips = NULL; GList *ips = NULL;
struct sockaddr_in *sa;
struct ifaddrs *ifa, *results; struct ifaddrs *ifa, *results;
gchar *loopback = NULL; gchar *loopback = NULL;
...@@ -174,27 +179,47 @@ nice_interfaces_get_local_ips (gboolean include_loopback) ...@@ -174,27 +179,47 @@ nice_interfaces_get_local_ips (gboolean include_loopback)
/* Loop through the interface list and get the IP address of each IF */ /* Loop through the interface list and get the IP address of each IF */
for (ifa = results; ifa; ifa = ifa->ifa_next) { for (ifa = results; ifa; ifa = ifa->ifa_next) {
char addr_as_string[INET6_ADDRSTRLEN+1];
int ret;
/* no ip address from interface that is down */ /* no ip address from interface that is down */
if ((ifa->ifa_flags & IFF_UP) == 0) if ((ifa->ifa_flags & IFF_UP) == 0)
continue; continue;
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) if (ifa->ifa_addr == NULL) {
continue;
} else if (ifa->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *sa4 = (struct sockaddr_in *) ifa->ifa_addr;
if (inet_ntop (AF_INET, &sa4->sin_addr, addr_as_string,
INET6_ADDRSTRLEN) == NULL)
continue;
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)ifa->ifa_addr;
/* Skip link-local addresses, they require a scope */
if (IN6_IS_ADDR_LINKLOCAL (sa6->sin6_addr.s6_addr))
continue;
if (inet_ntop (AF_INET6, &sa6->sin6_addr, addr_as_string,
INET6_ADDRSTRLEN) == NULL)
continue;
} else
continue; continue;
sa = (struct sockaddr_in *) ifa->ifa_addr;
nice_debug ("Interface: %s", ifa->ifa_name); nice_debug ("Interface: %s", ifa->ifa_name);
nice_debug ("IP Address: %s", inet_ntoa (sa->sin_addr)); nice_debug ("IP Address: %s", addr_as_string);
if ((ifa->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK) { if ((ifa->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK) {
if (include_loopback) if (include_loopback)
loopback = g_strdup (inet_ntoa (sa->sin_addr)); loopback = g_strdup (addr_as_string);
else else
nice_debug ("Ignoring loopback interface"); nice_debug ("Ignoring loopback interface");
} else { } else {
if (nice_interfaces_is_private_ip (sa->sin_addr)) if (nice_interfaces_is_private_ip (ifa->ifa_addr))
ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr))); ips = g_list_append (ips, g_strdup (addr_as_string));
else else
ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr))); ips = g_list_prepend (ips, g_strdup (addr_as_string));
} }
} }
...@@ -267,7 +292,7 @@ nice_interfaces_get_local_ips (gboolean include_loopback) ...@@ -267,7 +292,7 @@ nice_interfaces_get_local_ips (gboolean include_loopback)
else else
nice_debug ("Ignoring loopback interface"); nice_debug ("Ignoring loopback interface");
} else { } else {
if (nice_interfaces_is_private_ip (sa->sin_addr)) { if (nice_interfaces_is_private_ip (sa)) {
ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr))); ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr)));
} else { } else {
ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr))); ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr)));
......
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