diff options
author | rbuj <[email protected]> | 2020-12-18 11:10:55 +0100 |
---|---|---|
committer | raveit65 <[email protected]> | 2020-12-19 23:19:02 +0100 |
commit | c47ffa433de0dbf94d57aacbccac5c0cb440951d (patch) | |
tree | 7e473899532a949c9c6a1fb04d5e374f4f9e8520 /netspeed/src | |
parent | 43afa6d5de37f36c196459bb441311a65bcc386c (diff) | |
download | mate-applets-c47ffa433de0dbf94d57aacbccac5c0cb440951d.tar.bz2 mate-applets-c47ffa433de0dbf94d57aacbccac5c0cb440951d.tar.xz |
netspeed: show all ipv6 addrs in device details dialog
Diffstat (limited to 'netspeed/src')
-rw-r--r-- | netspeed/src/backend.c | 102 | ||||
-rw-r--r-- | netspeed/src/backend.h | 4 | ||||
-rw-r--r-- | netspeed/src/netspeed.c | 22 |
3 files changed, 123 insertions, 5 deletions
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 <sys/sockio.h> #endif +#include <sys/socket.h> +#include <sys/types.h> +#include <ifaddrs.h> +#include <netinet/in.h> +#include <arpa/inet.h> + #include <glib.h> #include <glib/gi18n.h> #include <glibtop/netlist.h> @@ -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); } |