implemented openbsd netspeed functions
authorTobias Tschinkowitz <he4d@posteo.de>
Fri, 18 May 2018 23:29:20 +0000 (01:29 +0200)
committerAaron Marcher <me@drkhsh.at>
Fri, 18 May 2018 23:44:36 +0000 (01:44 +0200)
implemented the netspeed functionality for openbsd.
furthermore the static keyword was removed of the interval variable in
config.def.h for usage as extern variable.

components/netspeeds.c
config.def.h

index ef8bf93570df6f61b940ca996e1571df970cbb99..9315feff23497788e627aa16743fa84ed3da2b58 100644 (file)
                return fmt_scaled((txbytes - oldtxbytes) / interval * 1000);
        }
 #elif defined(__OpenBSD__)
-       /* unimplemented */
+       #include <string.h>
+       #include <ifaddrs.h>
+       #include <sys/types.h>
+       #include <sys/socket.h>
+       #include <net/if.h>
+
+       const char *
+       netspeed_rx(const char *interface)
+       {
+               struct ifaddrs *ifal, *ifa;
+               struct if_data *ifd;
+               static uint64_t oldrxbytes;
+               uint64_t rxbytes = 0;
+               const char *rxs;
+               extern const unsigned int interval;
+
+               if (getifaddrs(&ifal) == -1) {
+                       warn("getifaddrs failed");
+                       return NULL;
+               }
+               for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
+                       if (!strcmp(ifa->ifa_name, interface) &&
+                          (ifd = (struct if_data *)ifa->ifa_data)) {
+                               rxbytes += ifd->ifi_ibytes;
+                       }
+               }
+               freeifaddrs(ifal);
+
+               rxs = oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
+                                             interval * 1000) : NULL;
+               return (oldrxbytes = rxbytes, rxs);
+       }
+
+       const char *
+       netspeed_tx(const char *interface)
+       {
+               struct ifaddrs *ifal, *ifa;
+               struct if_data *ifd;
+               static uint64_t oldtxbytes;
+               uint64_t txbytes = 0;
+               const char *txs;
+               extern const unsigned int interval;
+
+               if (getifaddrs(&ifal) == -1) {
+                       warn("getifaddrs failed");
+                       return NULL;
+               }
+               for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
+                       if (!strcmp(ifa->ifa_name, interface) &&
+                          (ifd = (struct if_data *)ifa->ifa_data)) {
+                               txbytes += ifd->ifi_obytes;
+                       }
+               }
+               freeifaddrs(ifal);
+
+               txs = oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
+                                             interval * 1000) : NULL;
+               return (oldtxbytes = txbytes, txs);
+       }
 #endif
index bf6aef9e13257367ec4435cc9bad5110209f3a9a..49ea282c7c286d950b93872b40ab6e2cf750af8d 100644 (file)
@@ -1,7 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 
 /* interval between updates (in ms) */
-static const unsigned int interval = 1000;
+const unsigned int interval = 1000;
 
 /* text to show if no value can be retrieved */
 static const char unknown_str[] = "n/a";