From c47ffa433de0dbf94d57aacbccac5c0cb440951d Mon Sep 17 00:00:00 2001 From: rbuj Date: Fri, 18 Dec 2020 11:10:55 +0100 Subject: netspeed: show all ipv6 addrs in device details dialog --- netspeed/data/netspeed-details.ui | 161 +++++++++++++++++++------------------- netspeed/src/backend.c | 102 ++++++++++++++++++++++++ netspeed/src/backend.h | 4 + netspeed/src/netspeed.c | 22 ++++-- 4 files changed, 203 insertions(+), 86 deletions(-) (limited to 'netspeed') diff --git a/netspeed/data/netspeed-details.ui b/netspeed/data/netspeed-details.ui index a537555f..32f01ed1 100644 --- a/netspeed/data/netspeed-details.ui +++ b/netspeed/data/netspeed-details.ui @@ -1,40 +1,40 @@ - + True - False - window-close + False + window-close True - False - help-browser + False + help-browser - False - 12 - dialog + False + 12 + dialog - False + False vertical 12 - False - end + False + end _Help True - True - True + True + True help_img - True - True + True + True True @@ -46,13 +46,13 @@ _Close True - True - True - True - True + True + True + True + True close_img - True - True + True + True True @@ -70,26 +70,26 @@ True - False + False vertical 10 True - False - 0 - none + False + 0 + none True - False + False 0 0 - 180 + 180 True - False + False @@ -97,7 +97,7 @@ - False + False Transfer Rate Graph @@ -111,13 +111,13 @@ True - False + False 6 True - True - True + True + True @@ -129,10 +129,10 @@ True - False + False _In graph color - True - incolor_sel + True + incolor_sel False @@ -143,8 +143,8 @@ True - True - True + True + True @@ -156,10 +156,10 @@ True - False + False _Out graph color - True - outcolor_sel + True + outcolor_sel False @@ -177,19 +177,19 @@ True - False + False vertical 6 True - False + False start 12 True - False + False start IPv4 Address: 0 @@ -203,7 +203,7 @@ True - False + False start True 0 @@ -217,7 +217,7 @@ True - False + False start Netmask: 0 @@ -231,7 +231,7 @@ True - False + False start True 0 @@ -252,12 +252,12 @@ True - False + False 12 True - False + False start Hardware Address: 0 @@ -271,7 +271,7 @@ True - False + False start True 0 @@ -285,7 +285,7 @@ True - False + False start P-t-P Address: 0 @@ -299,7 +299,7 @@ True - False + False start True 0 @@ -320,12 +320,12 @@ True - False + False 12 True - False + False start Bytes in: 0 @@ -339,7 +339,7 @@ True - False + False start 0 byte 0 @@ -353,7 +353,7 @@ True - False + False start Bytes out: 0 @@ -367,7 +367,7 @@ True - False + False start 0 byte 0 @@ -388,15 +388,16 @@ True - False + False 12 True - False + False start IPv6 Address: 0 + 0 False @@ -407,10 +408,11 @@ True - False + False start True 0 + 0 False @@ -428,18 +430,18 @@ True - False + False vertical 6 True - False + False 12 True - False + False start ESSID: 0 @@ -453,7 +455,7 @@ True - False + False start True 0 @@ -467,7 +469,7 @@ True - False + False start Signal Strength: 0 @@ -481,7 +483,7 @@ True - False + False start True @@ -501,18 +503,18 @@ True - False + False vertical 6 True - False + False 12 True - False + False Station: 0 @@ -525,7 +527,7 @@ True - False + False 0 @@ -537,7 +539,7 @@ True - False + False Channel: 0 @@ -550,7 +552,7 @@ True - False + False 0 @@ -569,12 +571,12 @@ True - False + False 12 True - False + False Connected Time: 0 @@ -587,7 +589,7 @@ True - False + False 0 @@ -637,12 +639,9 @@ button1 button2 - - - - True + True @@ -654,7 +653,7 @@ - True + True @@ -664,7 +663,7 @@ - True + True @@ -674,7 +673,7 @@ - True + True diff --git a/netspeed/src/backend.c b/netspeed/src/backend.c index 110173d6..5eb67319 100644 --- a/netspeed/src/backend.c +++ b/netspeed/src/backend.c @@ -27,6 +27,12 @@ #include #endif +#include +#include +#include +#include +#include + #include #include #include @@ -105,6 +111,102 @@ get_available_devices (void) return device_glist; } +GSList* +get_ip_address_list (const char *iface_name, + gboolean ipv4) +{ + GSList *list = NULL; + struct ifaddrs *ifaces; + + if (getifaddrs (&ifaces) != -1) { + int family; + char ip[INET6_ADDRSTRLEN]; + + family = ipv4 ? AF_INET : AF_INET6; + for (struct ifaddrs *iface = ifaces; iface != NULL; iface = iface->ifa_next) { + if (iface->ifa_addr == NULL) + continue; + + if ((iface->ifa_addr->sa_family == family) && + !g_strcmp0 (iface->ifa_name, iface_name)) + { + unsigned int netmask = 0; + void *sinx_addr = NULL; + + if (iface->ifa_addr->sa_family == AF_INET6) { + struct sockaddr_in6 ip6_addr; + struct sockaddr_in6 ip6_network; + uint32_t ip6_netmask = 0; + const char *scope; + int i; + + memcpy (&ip6_addr, iface->ifa_addr, sizeof (struct sockaddr_in6)); + memcpy (&ip6_network, iface->ifa_netmask, sizeof (struct sockaddr_in6)); + + /* get network scope */ + if (IN6_IS_ADDR_LINKLOCAL (&ip6_addr.sin6_addr)) { + scope = _("link-local"); + } else if (IN6_IS_ADDR_SITELOCAL (&ip6_addr.sin6_addr)) { + scope = _("site-local"); + } else if (IN6_IS_ADDR_V4MAPPED (&ip6_addr.sin6_addr)) { + scope = _("v4mapped"); + } else if (IN6_IS_ADDR_V4COMPAT (&ip6_addr.sin6_addr)) { + scope = _("v4compat"); + } else if (IN6_IS_ADDR_LOOPBACK (&ip6_addr.sin6_addr)) { + scope = _("host"); + } else if (IN6_IS_ADDR_UNSPECIFIED (&ip6_addr.sin6_addr)) { + scope = _("unspecified"); + } else { + scope = _("global"); + } + + /* get network ip */ + sinx_addr = &ip6_addr.sin6_addr; + inet_ntop (iface->ifa_addr->sa_family, sinx_addr, ip, sizeof (ip)); + + /* get network mask length */ + for (i = 0; i < 4; i++) { + ip6_netmask = ntohl (((uint32_t*)(&ip6_network.sin6_addr))[i]); + while (ip6_netmask) { + netmask++; + ip6_netmask <<= 1; + } + } + + list = g_slist_prepend (list, + g_strdup_printf ("%s: %s/%u", + scope, ip, netmask)); + } else { + struct sockaddr_in ip4_addr; + struct sockaddr_in ip4_network; + in_addr_t ip4_netmask = 0; + + memcpy (&ip4_addr, iface->ifa_addr, sizeof (struct sockaddr_in)); + memcpy (&ip4_network, iface->ifa_netmask, sizeof (struct sockaddr_in)); + + /* get network ip */ + sinx_addr = &ip4_addr.sin_addr; + inet_ntop (iface->ifa_addr->sa_family, sinx_addr, ip, sizeof (ip)); + + /* get network mask length */ + ip4_netmask = ntohl (ip4_network.sin_addr.s_addr); + while (ip4_netmask) { + netmask++; + ip4_netmask <<= 1; + } + + list = g_slist_prepend (list, + g_strdup_printf ("%s/%u", ip, netmask)); + } + } + } + if (list != NULL) + list = g_slist_sort (list, (GCompareFunc) g_strcmp0); + freeifaddrs (ifaces); + } + return list; +} + const gchar* get_default_route (void) { diff --git a/netspeed/src/backend.h b/netspeed/src/backend.h index 536bf862..cd98c66c 100644 --- a/netspeed/src/backend.h +++ b/netspeed/src/backend.h @@ -104,4 +104,8 @@ compare_device_info (const DevInfo *a, const DevInfo *b); void get_wireless_info (DevInfo *devinfo); + +GSList* +get_ip_address_list (const char *ifa_name, gboolean ipv4); + #endif /* _BACKEND_H */ diff --git a/netspeed/src/netspeed.c b/netspeed/src/netspeed.c index b3e88847..eaa1b5ae 100644 --- a/netspeed/src/netspeed.c +++ b/netspeed/src/netspeed.c @@ -688,7 +688,6 @@ fill_details_dialog (MateNetspeedApplet *applet) { char *text; char ipv4_text [INET_ADDRSTRLEN]; - char ipv6_text [INET6_ADDRSTRLEN]; if (applet->devinfo->ip) { format_ipv4 (applet->devinfo->ip, ipv4_text); @@ -723,10 +722,23 @@ fill_details_dialog (MateNetspeedApplet *applet) gtk_label_set_text (GTK_LABEL (applet->ptpip_text), text); /* check if we got an ipv6 address */ - format_ipv6 (applet->devinfo->ipv6, ipv6_text); - if (strlen (ipv6_text) > 2) { - gtk_label_set_text (GTK_LABEL (applet->ipv6_text), ipv6_text); - gtk_widget_show (applet->ipv6_box); + GSList *ipv6_address_list = get_ip_address_list (applet->devinfo->name, FALSE); + if (ipv6_address_list != NULL) { + GSList *iterator; + GString *string = NULL; + + for (iterator = ipv6_address_list; iterator; iterator = iterator->next) { + if (string == NULL) + string = g_string_new ((char*) iterator->data); + else + g_string_append_printf (string, "\n%s", (char*) iterator->data); + } + if (string != NULL) { + gtk_label_set_text (GTK_LABEL (applet->ipv6_text), string->str); + gtk_widget_show (applet->ipv6_box); + } + g_string_free (string, TRUE); + g_slist_free_full (ipv6_address_list, g_free); } else { gtk_widget_hide (applet->ipv6_box); } -- cgit v1.2.1