-#if defined(LINUX)
- /* This code path requires root privileges */
- struct ifreq ifr;
- struct ethtool_cmd ecmd;
-
- ecmd.cmd = ETHTOOL_GSET;
- (void)memset(&ifr, 0, sizeof(ifr));
- ifr.ifr_data = (caddr_t)&ecmd;
- (void)strcpy(ifr.ifr_name, interface);
- if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
- ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
- printf("%d Mbit/s", ethspeed);
- } else printf("?");
-#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
- struct ifmediareq ifm;
- (void)memset(&ifm, 0, sizeof(ifm));
- (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
- int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
-
- /* Get the description of the media type, partially taken from
- * FreeBSD's ifconfig */
- const struct ifmedia_description *desc;
- struct ifmedia_description ifm_subtype_descriptions[] =
- IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
-
- for (desc = ifm_subtype_descriptions;
- desc->ifmt_string != NULL;
- desc++) {
- if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
- IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
- break;
- }
- ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
- printf("%s", ethspeed);
+ /* Get the description of the media type, partially taken from
+ * FreeBSD's ifconfig */
+ const struct ifmedia_description *desc;
+ static struct ifmedia_description ifm_subtype_descriptions[] =
+ IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
+
+ if (IFM_TYPE(ifm.ifm_active) != IFM_ETHER)
+ return sprintf(outwalk, "?");
+ if (ifm.ifm_status & IFM_AVALID && !(ifm.ifm_status & IFM_ACTIVE))
+ return sprintf(outwalk, "no carrier");
+ for (desc = ifm_subtype_descriptions;
+ desc->ifmt_string != NULL;
+ desc++) {
+ if (desc->ifmt_word == IFM_SUBTYPE(ifm.ifm_active))
+ break;
+ }
+ ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
+ return sprintf(outwalk, "%s", ethspeed);
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
+ const 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 (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
+ BEGINS_WITH(desc->ifmt_string, "auto"))
+ 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, "?");