]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
Fix some issues with displaying Ethernet media status on recent FreeBSD kernels.
[i3/i3status] / src / print_eth_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <string.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <net/if.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <yajl/yajl_gen.h>
12 #include <yajl/yajl_version.h>
13
14 #include "i3status.h"
15
16 #if defined(LINUX)
17 #include <linux/ethtool.h>
18 #include <linux/sockios.h>
19 #define PART_ETHSPEED "E: %s (%d Mbit/s)"
20 #endif
21
22 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
23 #include <net/if_media.h>
24
25 #define PART_ETHSPEED "E: %s (%s)"
26 #endif
27
28 #if defined(__OpenBSD__) || defined(__NetBSD__)
29 #include <errno.h>
30 #include <net/if_media.h>
31 #endif
32
33 static int print_eth_speed(char *outwalk, const char *interface) {
34 #if defined(LINUX)
35     /* This code path requires root privileges */
36     int ethspeed = 0;
37     struct ifreq ifr;
38     struct ethtool_cmd ecmd;
39
40     ecmd.cmd = ETHTOOL_GSET;
41     (void)memset(&ifr, 0, sizeof(ifr));
42     ifr.ifr_data = (caddr_t)&ecmd;
43     (void)strcpy(ifr.ifr_name, interface);
44     if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
45         ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
46         return sprintf(outwalk, "%d Mbit/s", ethspeed);
47     } else
48         return sprintf(outwalk, "?");
49 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
50     const char *ethspeed;
51     struct ifmediareq ifm;
52     (void)memset(&ifm, 0, sizeof(ifm));
53     (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
54     int ret;
55 #ifdef SIOCGIFXMEDIA
56     ret = ioctl(general_socket, SIOCGIFXMEDIA, (caddr_t)&ifm);
57     if (ret < 0)
58 #endif
59         ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
60     if (ret < 0)
61         return sprintf(outwalk, "?");
62
63     /* Get the description of the media type, partially taken from
64      * FreeBSD's ifconfig */
65     const struct ifmedia_description *desc;
66     static struct ifmedia_description ifm_subtype_descriptions[] =
67         IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
68
69     if (IFM_TYPE(ifm.ifm_active) != IFM_ETHER)
70         return sprintf(outwalk, "?");
71     if (ifm.ifm_status & IFM_AVALID && !(ifm.ifm_status & IFM_ACTIVE))
72         return sprintf(outwalk, "no carrier");
73     for (desc = ifm_subtype_descriptions;
74          desc->ifmt_string != NULL;
75          desc++) {
76         if (desc->ifmt_word == IFM_SUBTYPE(ifm.ifm_active))
77             break;
78     }
79     ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
80     return sprintf(outwalk, "%s", ethspeed);
81 #elif defined(__OpenBSD__) || defined(__NetBSD__)
82     char *ethspeed;
83     struct ifmediareq ifmr;
84
85     (void)memset(&ifmr, 0, sizeof(ifmr));
86     (void)strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
87
88     if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
89         if (errno != E2BIG)
90             return sprintf(outwalk, "?");
91     }
92
93     struct ifmedia_description *desc;
94     struct ifmedia_description ifm_subtype_descriptions[] =
95         IFM_SUBTYPE_DESCRIPTIONS;
96
97     for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
98         /*
99                  * Skip these non-informative values and go right ahead to the
100                  * actual speeds.
101                  */
102         if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
103             BEGINS_WITH(desc->ifmt_string, "auto"))
104             continue;
105
106         if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
107             IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
108             break;
109     }
110     ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
111     return sprintf(outwalk, "%s", ethspeed);
112
113 #else
114     return sprintf(outwalk, "?");
115 #endif
116 }
117
118 /*
119  * Combines ethernet IP addresses and speed (if requested) for displaying
120  *
121  */
122 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
123     const char *walk;
124     const char *ip_address = get_ip_addr(interface);
125     char *outwalk = buffer;
126
127     INSTANCE(interface);
128
129     if (ip_address == NULL) {
130         START_COLOR("color_bad");
131         outwalk += sprintf(outwalk, "%s", format_down);
132         goto out;
133     }
134
135     if (BEGINS_WITH(ip_address, "no IP"))
136         START_COLOR("color_degraded");
137     else
138         START_COLOR("color_good");
139
140     for (walk = format_up; *walk != '\0'; walk++) {
141         if (*walk != '%') {
142             *(outwalk++) = *walk;
143             continue;
144         }
145
146         if (BEGINS_WITH(walk + 1, "ip")) {
147             outwalk += sprintf(outwalk, "%s", ip_address);
148             walk += strlen("ip");
149         } else if (BEGINS_WITH(walk + 1, "speed")) {
150             outwalk += print_eth_speed(outwalk, interface);
151             walk += strlen("speed");
152         }
153     }
154
155 out:
156     END_COLOR;
157     OUTPUT_FULL_TEXT(buffer);
158 }