From f45581f8d8bffff8f1eb98486953ec491eb77a05 Mon Sep 17 00:00:00 2001 From: Fabian Franzen Date: Wed, 12 Apr 2017 03:30:28 +0200 Subject: Add check for virtual ethernet devices The _first_ option for ethernet devices now uses the link in sysfs to determine if it's a real device or just a virtual one (i.e veth** devices created by docker). --- src/first_network_device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/first_network_device.c') diff --git a/src/first_network_device.c b/src/first_network_device.c index 5932385..abbc9b0 100644 --- a/src/first_network_device.c +++ b/src/first_network_device.c @@ -50,6 +50,21 @@ static bool sysfs_devtype(char *dest, size_t n, const char *ifnam) { return true; } +static bool is_virtual(const char *ifname) { + char path[1024]; + char *target = NULL; + const char virtual_template[] = "/sys/devices/virtual/"; + + snprintf(path, sizeof(path), "/sys/class/net/%s", ifname); + if ((target = realpath(path, NULL))) { + if (strncmp(virtual_template, target, strlen(virtual_template)) == 0) + return true; + } + free(target); + + return false; +} + static net_type_t iface_type(const char *ifname) { #ifdef __linux__ char devtype[32]; @@ -57,6 +72,7 @@ static net_type_t iface_type(const char *ifname) { if (!sysfs_devtype(devtype, sizeof(devtype), ifname)) return NET_TYPE_OTHER; + /* Default to Ethernet when no devtype is available */ if (!devtype[0]) return NET_TYPE_ETHERNET; @@ -147,6 +163,8 @@ const char *first_eth_interface(const net_type_t type) { iftype = iface_type(addrp->ifa_name); if (iftype != type) continue; + if (is_virtual(addrp->ifa_name)) + continue; interface = strdup(addrp->ifa_name); break; } -- cgit v1.2.3 From ad3fac03c34ddee7108542d292a09e99f879d3db Mon Sep 17 00:00:00 2001 From: Fabian Franzen Date: Wed, 12 Apr 2017 13:11:34 +0200 Subject: Fix memory leak/Use BEGINS_WITH macro The orignal proposed code had a memory leak when returning true. Furthermore I included the handy BEGINS_WITH macro of i3 which makes the code (IMHO) a lot more readable. --- src/first_network_device.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/first_network_device.c') diff --git a/src/first_network_device.c b/src/first_network_device.c index abbc9b0..656e457 100644 --- a/src/first_network_device.c +++ b/src/first_network_device.c @@ -53,16 +53,16 @@ static bool sysfs_devtype(char *dest, size_t n, const char *ifnam) { static bool is_virtual(const char *ifname) { char path[1024]; char *target = NULL; - const char virtual_template[] = "/sys/devices/virtual/"; + bool is_virtual = false; snprintf(path, sizeof(path), "/sys/class/net/%s", ifname); if ((target = realpath(path, NULL))) { - if (strncmp(virtual_template, target, strlen(virtual_template)) == 0) - return true; + if (BEGINS_WITH(target, "/sys/devices/virtual/")) + is_virtual = true; } - free(target); - return false; + free(target); + return is_virtual; } static net_type_t iface_type(const char *ifname) { -- cgit v1.2.3 From 6a19709e65c6aac2085962e989f2429c539c5af5 Mon Sep 17 00:00:00 2001 From: Fabian Franzen Date: Thu, 13 Apr 2017 12:51:29 +0200 Subject: Added braces Added braces to the if-statement as requested. --- src/first_network_device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/first_network_device.c') diff --git a/src/first_network_device.c b/src/first_network_device.c index 656e457..3f34cf2 100644 --- a/src/first_network_device.c +++ b/src/first_network_device.c @@ -57,8 +57,9 @@ static bool is_virtual(const char *ifname) { snprintf(path, sizeof(path), "/sys/class/net/%s", ifname); if ((target = realpath(path, NULL))) { - if (BEGINS_WITH(target, "/sys/devices/virtual/")) + if (BEGINS_WITH(target, "/sys/devices/virtual/")) { is_virtual = true; + } } free(target); -- cgit v1.2.3