]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_eth_info.c
Implement %speed for Ethernet on OpenBSD.
[i3/i3status] / src / print_eth_info.c
index 90005bf5fc0c2199a813d74236948c8b334c33f3..d09873ed8aefbd619624601d1a42815b7a1c0259 100644 (file)
         (IFM_TYPE((dt)) == 0 || IFM_TYPE((dt)) == IFM_TYPE((t)))
 
 #define PART_ETHSPEED  "E: %s (%s)"
-
 #endif
 
-static int print_eth_speed(char *outwalk, const char *interface) {
-#if defined(LINUX)
-        int ethspeed = 0;
-#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-        char *ethspeed;
+#if defined(__OpenBSD__)
+#include <errno.h>
+#include <net/if_media.h>
 #endif
 
+static int print_eth_speed(char *outwalk, const char *interface) {
 #if defined(LINUX)
         /* This code path requires root privileges */
+        int ethspeed = 0;
         struct ifreq ifr;
         struct ethtool_cmd ecmd;
 
@@ -49,6 +48,7 @@ static int print_eth_speed(char *outwalk, const char *interface) {
                 return sprintf(outwalk, "%d Mbit/s", ethspeed);
         } else return sprintf(outwalk, "?");
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+        char *ethspeed;
         struct ifmediareq ifm;
         (void)memset(&ifm, 0, sizeof(ifm));
         (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
@@ -69,6 +69,40 @@ static int print_eth_speed(char *outwalk, const char *interface) {
         }
         ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
         return sprintf(outwalk, "%s", ethspeed);
+#elif defined(__OpenBSD__)
+       char *ethspeed;
+       struct ifmediareq ifmr;
+
+       (void) memset(&ifmr, 0, sizeof(ifmr));
+       (void) strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
+
+       if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
+                if (errno != E2BIG)
+                       return sprintf(outwalk, "?");
+       }
+
+       struct ifmedia_description *desc;
+       struct ifmedia_description ifm_subtype_descriptions[] =
+           IFM_SUBTYPE_DESCRIPTIONS;
+
+        for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
+               /*
+                * Skip these non-informative values and go right ahead to the
+                * actual speeds.
+                */
+               if (strncmp(desc->ifmt_string, "autoselect", strlen("autoselect")) == 0 ||
+                   strncmp(desc->ifmt_string, "auto", strlen("auto")) == 0)
+                       continue;
+
+               if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
+                   IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
+                       break;
+        }
+        ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
+        return sprintf(outwalk, "%s", ethspeed);
+
+#else
+       return sprintf(outwalk, "?");
 #endif
 }