]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
d09873ed8aefbd619624601d1a42815b7a1c0259
[i3/i3status] / src / print_eth_info.c
1 // vim:ts=8: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__)
23 #include <net/if_media.h>
24 #define IFM_TYPE_MATCH(dt, t)                       \
25         (IFM_TYPE((dt)) == 0 || IFM_TYPE((dt)) == IFM_TYPE((t)))
26
27 #define PART_ETHSPEED  "E: %s (%s)"
28 #endif
29
30 #if defined(__OpenBSD__)
31 #include <errno.h>
32 #include <net/if_media.h>
33 #endif
34
35 static int print_eth_speed(char *outwalk, const char *interface) {
36 #if defined(LINUX)
37         /* This code path requires root privileges */
38         int ethspeed = 0;
39         struct ifreq ifr;
40         struct ethtool_cmd ecmd;
41
42         ecmd.cmd = ETHTOOL_GSET;
43         (void)memset(&ifr, 0, sizeof(ifr));
44         ifr.ifr_data = (caddr_t)&ecmd;
45         (void)strcpy(ifr.ifr_name, interface);
46         if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
47                 ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
48                 return sprintf(outwalk, "%d Mbit/s", ethspeed);
49         } else return sprintf(outwalk, "?");
50 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
51         char *ethspeed;
52         struct ifmediareq ifm;
53         (void)memset(&ifm, 0, sizeof(ifm));
54         (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
55         int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
56
57         /* Get the description of the media type, partially taken from
58          * FreeBSD's ifconfig */
59         const struct ifmedia_description *desc;
60         struct ifmedia_description ifm_subtype_descriptions[] =
61                 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
62
63         for (desc = ifm_subtype_descriptions;
64              desc->ifmt_string != NULL;
65              desc++) {
66             if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
67                 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
68                 break;
69         }
70         ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
71         return sprintf(outwalk, "%s", ethspeed);
72 #elif defined(__OpenBSD__)
73         char *ethspeed;
74         struct ifmediareq ifmr;
75
76         (void) memset(&ifmr, 0, sizeof(ifmr));
77         (void) strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
78
79         if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
80                 if (errno != E2BIG)
81                         return sprintf(outwalk, "?");
82         }
83
84         struct ifmedia_description *desc;
85         struct ifmedia_description ifm_subtype_descriptions[] =
86             IFM_SUBTYPE_DESCRIPTIONS;
87
88         for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
89                 /*
90                  * Skip these non-informative values and go right ahead to the
91                  * actual speeds.
92                  */
93                 if (strncmp(desc->ifmt_string, "autoselect", strlen("autoselect")) == 0 ||
94                     strncmp(desc->ifmt_string, "auto", strlen("auto")) == 0)
95                         continue;
96
97                 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
98                     IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
99                         break;
100         }
101         ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
102         return sprintf(outwalk, "%s", ethspeed);
103
104 #else
105         return sprintf(outwalk, "?");
106 #endif
107 }
108
109 /*
110  * Combines ethernet IP addresses and speed (if requested) for displaying
111  *
112  */
113 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
114         const char *walk;
115         const char *ip_address = get_ip_addr(interface);
116         char *outwalk = buffer;
117
118         INSTANCE(interface);
119
120         if (ip_address == NULL) {
121                 START_COLOR("color_bad");
122                 outwalk += sprintf(outwalk, "%s", format_down);
123                 goto out;
124         }
125
126         START_COLOR("color_good");
127
128         for (walk = format_up; *walk != '\0'; walk++) {
129                 if (*walk != '%') {
130                         *(outwalk++) = *walk;
131                         continue;
132                 }
133
134                 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
135                         outwalk += sprintf(outwalk, "%s", ip_address);
136                         walk += strlen("ip");
137                 } else if (strncmp(walk+1, "speed", strlen("speed")) == 0) {
138                         outwalk += print_eth_speed(outwalk, interface);
139                         walk += strlen("speed");
140                 }
141         }
142
143 out:
144         END_COLOR;
145         OUTPUT_FULL_TEXT(buffer);
146 }