7 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
15 #include <linux/ethtool.h>
16 #include <linux/sockios.h>
17 #define PART_ETHSPEED "E: %s (%d Mbit/s)"
20 #if defined(__FreeBSD__)
21 #include <net/if_media.h>
22 #define IFM_TYPE_MATCH(dt, t) \
23 (IFM_TYPE((dt)) == 0 || IFM_TYPE((dt)) == IFM_TYPE((t)))
25 #define PART_ETHSPEED "E: %s (%s)"
30 * Combines ethernet IP addresses and speed (if requested) for displaying
33 const char *get_eth_info() {
34 static char part[512];
37 #elif defined(__FreeBSD__)
40 const char *ip_address = get_ip_addr(eth_interface);
42 if (ip_address == NULL) {
43 (void)snprintf(part, sizeof(part), "E: down");
49 /* This code path requires root privileges */
51 struct ethtool_cmd ecmd;
53 ecmd.cmd = ETHTOOL_GSET;
54 (void)memset(&ifr, 0, sizeof(ifr));
55 ifr.ifr_data = (caddr_t)&ecmd;
56 (void)strcpy(ifr.ifr_name, eth_interface);
57 if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0)
58 ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
59 else get_ethspeed = false;
60 #elif defined(__FreeBSD__)
61 struct ifmediareq ifm;
62 (void)memset(&ifm, 0, sizeof(ifm));
63 (void)strncpy(ifm.ifm_name, eth_interface, sizeof(ifm.ifm_name));
64 int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
66 /* Get the description of the media type, partially taken from
67 * FreeBSD's ifconfig */
68 const struct ifmedia_description *desc;
69 struct ifmedia_description ifm_subtype_descriptions[] =
70 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
72 for (desc = ifm_subtype_descriptions;
73 desc->ifmt_string != NULL;
75 if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
76 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
79 ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
84 (void)snprintf(part, sizeof(part), PART_ETHSPEED, ip_address, ethspeed);
85 else (void)snprintf(part, sizeof(part), "E: %s", ip_address);