From 451ff9be0b87946778250fdffcc62b04817b6394 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 4 Jun 2018 19:59:15 +0200 Subject: Fix shown IP address belonging to wrong interface The following commit: 6a75ea9 Show IP address when address has a label introduced a way to show the IP address of an interface when a label is associated to the IP. When a label is associated to an IP, the structure returned by getifaddrs() has the label concatenated to the interface name in the .ifa_name field as in the following example: struct ifaddrs ifaddr = { .ifa_name = "eth0:mylabel", }; As a consequence, using a strict comparison between the interface name and the .ifa_name field yields a falsy result. However, checking if the .ifa_name starts with the interface name (e.g. eth0) does not work either because other network interfaces can have a name which starts with eth0. This commit solves the issue by stripping out the optional label from the .ifa_name field before making a strict comparison with the interface name. Fix #283 Signed-off-by: Olivier Gayot --- src/print_ip_addr.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/print_ip_addr.c') diff --git a/src/print_ip_addr.c b/src/print_ip_addr.c index cf476ae..30a1ce3 100644 --- a/src/print_ip_addr.c +++ b/src/print_ip_addr.c @@ -12,6 +12,31 @@ #include "i3status.h" +/* + * Return a copy of the .ifa_name field passed as argument where the optional + * IP label, if present, is removed. + * + * example: + * - strip_optional_label("eth0") => "eth0" + * - strip_optional_label("eth0:label") => "eth0" + * + * The memory for the returned string is obtained with malloc(3), and can be + * freed with free(3). + * + * + */ +static char *strip_optional_label(const char *ifa_name) { + char *copy = sstrdup(ifa_name); + + char *ptr = strchr(copy, ':'); + + if (ptr) { + *ptr = '\0'; + } + + return copy; +} + /* * Return the IP address for the given interface or "no IP" if the * interface is up and running but hasn't got an IP address yet @@ -29,7 +54,6 @@ const char *get_ip_addr(const char *interface, int family) { struct ifaddrs *ifaddr, *addrp; bool found = false; - int interface_len = strlen(interface); getifaddrs(&ifaddr); @@ -38,7 +62,12 @@ const char *get_ip_addr(const char *interface, int family) { /* Skip until we are at the input family address of interface */ for (addrp = ifaddr; addrp != NULL; addrp = addrp->ifa_next) { - if (strncmp(addrp->ifa_name, interface, interface_len) != 0) { + /* Strip the label if present in the .ifa_name field. */ + char *stripped_ifa_name = strip_optional_label(addrp->ifa_name); + + bool name_matches = strcmp(stripped_ifa_name, interface) != 0; + free(stripped_ifa_name); + if (name_matches) { /* The interface does not have the right name, skip it. */ continue; } -- cgit v1.2.3