]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
able to print percentage
[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 <stdlib.h>
6 #include <sys/ioctl.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <net/if.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <yajl/yajl_gen.h>
13 #include <yajl/yajl_version.h>
14
15 #include "i3status.h"
16
17 #if defined(LINUX)
18 #include <linux/ethtool.h>
19 #include <linux/sockios.h>
20 #define PART_ETHSPEED "E: %s (%d Mbit/s)"
21 #endif
22
23 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
24 #include <net/if_media.h>
25
26 #define PART_ETHSPEED "E: %s (%s)"
27 #endif
28
29 #if defined(__OpenBSD__) || defined(__NetBSD__)
30 #include <errno.h>
31 #include <net/if_media.h>
32 #endif
33
34 static int print_eth_speed(char *outwalk, const char *interface) {
35 #if defined(LINUX)
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     const 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  * Table summarizing what is the decision to prefer IPv4 or IPv6
122  * based their values.
123  *
124  * | ipv4_address | ipv6_address | Chosen IP | Color             |
125  * |--------------|--------------|-----------|-------------------|
126  * | NULL         | NULL         | None      | bad (red)         |
127  * | NULL         | no IP        | IPv6      | degraded (orange) |
128  * | NULL         | ::1/128      | IPv6      | ok (green)        |
129  * | no IP        | NULL         | IPv4      | degraded          |
130  * | no IP        | no IP        | IPv4      | degraded          |
131  * | no IP        | ::1/128      | IPv6      | ok                |
132  * | 127.0.0.1    | NULL         | IPv4      | ok                |
133  * | 127.0.0.1    | no IP        | IPv4      | ok                |
134  * | 127.0.0.1    | ::1/128      | IPv4      | ok                |
135  */
136 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
137     const char *walk;
138     char *outwalk = buffer;
139
140     INSTANCE(interface);
141
142     char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET));
143     char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6));
144
145     /*
146      * Removing '%' and following characters from IPv6 since the interface identifier is redundant,
147      * as the output already includes the interface name.
148     */
149     if (ipv6_address != NULL) {
150         char *prct_ptr = strstr(ipv6_address, "%");
151         if (prct_ptr != NULL) {
152             *prct_ptr = '\0';
153         }
154     }
155
156     bool prefer_ipv4 = true;
157     if (ipv4_address == NULL) {
158         if (ipv6_address == NULL) {
159             START_COLOR("color_bad");
160             outwalk += sprintf(outwalk, "%s", format_down);
161             goto out;
162         } else {
163             prefer_ipv4 = false;
164         }
165     } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) {
166         prefer_ipv4 = false;
167     }
168
169     const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address;
170     if (BEGINS_WITH(ip_address, "no IP")) {
171         START_COLOR("color_degraded");
172     } else {
173         START_COLOR("color_good");
174     }
175     for (walk = format_up; *walk != '\0'; walk++) {
176         if (*walk != '%') {
177             *(outwalk++) = *walk;
178
179         } else if (BEGINS_WITH(walk + 1, "ip")) {
180             outwalk += sprintf(outwalk, "%s", ip_address);
181             walk += strlen("ip");
182
183         } else if (BEGINS_WITH(walk + 1, "speed")) {
184             outwalk += print_eth_speed(outwalk, interface);
185             walk += strlen("speed");
186
187         } else {
188             *(outwalk++) = '%';
189         }
190     }
191
192 out:
193     END_COLOR;
194     free(ipv4_address);
195     free(ipv6_address);
196     OUTPUT_FULL_TEXT(buffer);
197 }