From f8206181da2a8d7e24c60d19f2193cf8f5ebe54e Mon Sep 17 00:00:00 2001 From: Jesper Dangaard Brouer Date: Fri, 25 Apr 2014 13:39:23 +0200 Subject: ifpps: fix matching wrong interface/device There were a bug in ifpps, that would match the first occurance of a device/interface name in /proc/net/dev, because it located the device based on a substring. If the device name were starting with the same characters, e.g. having a device named eth4 and a VLAN device named eth4.100. If specifying --dev eth4, then depending on the order of /proc/net/dev the wrong device could be choosen for stats. (Note kernel does not guarantee the order of devices in proc file) Fix the bug, by matching with a colon ':' appended to the device name, as the kernel proc format prints a ':' behind the device name. Fixes #119 Signed-off-by: Jesper Dangaard Brouer --- ifpps.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'ifpps.c') diff --git a/ifpps.c b/ifpps.c index 6f50bed..785a882 100644 --- a/ifpps.c +++ b/ifpps.c @@ -226,12 +226,17 @@ static int stats_proc_net_dev(const char *ifname, struct ifstat *stats) { int ret = -EINVAL; char buff[256]; + char *ifname_colon; FILE *fp; fp = fopen("/proc/net/dev", "r"); if (!fp) panic("Cannot open /proc/net/dev!\n"); + ifname_colon = xstrndup(ifname, strlen(ifname)+2); + ifname_colon[strlen(ifname)] = ':'; + ifname_colon[strlen(ifname)+1] = '\0'; + if (fgets(buff, sizeof(buff), fp)) { ; } if (fgets(buff, sizeof(buff), fp)) { ; } @@ -240,7 +245,7 @@ static int stats_proc_net_dev(const char *ifname, struct ifstat *stats) while (fgets(buff, sizeof(buff), fp) != NULL) { buff[sizeof(buff) -1] = 0; - if (strstr(buff, ifname) == NULL) + if (strstr(buff, ifname_colon) == NULL) continue; if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu" @@ -259,6 +264,7 @@ static int stats_proc_net_dev(const char *ifname, struct ifstat *stats) memset(buff, 0, sizeof(buff)); } + xfree(ifname_colon); fclose(fp); return ret; } -- cgit v1.2.3-54-g00ecf