]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
print_eth_info: fix warnings on FreeBSD
[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 #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__) || defined(__NetBSD__)
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
50         return sprintf(outwalk, "?");
51 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
52     const char *ethspeed;
53     struct ifmediareq ifm;
54     (void)memset(&ifm, 0, sizeof(ifm));
55     (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
56     if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm) < 0) {
57         return sprintf(outwalk, "?");
58     }
59
60     /* Get the description of the media type, partially taken from
61      * FreeBSD's ifconfig */
62     const struct ifmedia_description *desc;
63     struct ifmedia_description ifm_subtype_descriptions[] =
64         IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
65
66     for (desc = ifm_subtype_descriptions;
67          desc->ifmt_string != NULL;
68          desc++) {
69         if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
70             IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
71             break;
72     }
73     ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
74     return sprintf(outwalk, "%s", ethspeed);
75 #elif defined(__OpenBSD__) || defined(__NetBSD__)
76     char *ethspeed;
77     struct ifmediareq ifmr;
78
79     (void)memset(&ifmr, 0, sizeof(ifmr));
80     (void)strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
81
82     if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
83         if (errno != E2BIG)
84             return sprintf(outwalk, "?");
85     }
86
87     struct ifmedia_description *desc;
88     struct ifmedia_description ifm_subtype_descriptions[] =
89         IFM_SUBTYPE_DESCRIPTIONS;
90
91     for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
92         /*
93                  * Skip these non-informative values and go right ahead to the
94                  * actual speeds.
95                  */
96         if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
97             BEGINS_WITH(desc->ifmt_string, "auto"))
98             continue;
99
100         if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
101             IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
102             break;
103     }
104     ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
105     return sprintf(outwalk, "%s", ethspeed);
106
107 #else
108     return sprintf(outwalk, "?");
109 #endif
110 }
111
112 /*
113  * Combines ethernet IP addresses and speed (if requested) for displaying
114  *
115  */
116 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
117     const char *walk;
118     const char *ip_address = get_ip_addr(interface);
119     char *outwalk = buffer;
120
121     INSTANCE(interface);
122
123     if (ip_address == NULL) {
124         START_COLOR("color_bad");
125         outwalk += sprintf(outwalk, "%s", format_down);
126         goto out;
127     }
128
129     START_COLOR("color_good");
130
131     for (walk = format_up; *walk != '\0'; walk++) {
132         if (*walk != '%') {
133             *(outwalk++) = *walk;
134             continue;
135         }
136
137         if (BEGINS_WITH(walk + 1, "ip")) {
138             outwalk += sprintf(outwalk, "%s", ip_address);
139             walk += strlen("ip");
140         } else if (BEGINS_WITH(walk + 1, "speed")) {
141             outwalk += print_eth_speed(outwalk, interface);
142             walk += strlen("speed");
143         }
144     }
145
146 out:
147     END_COLOR;
148     OUTPUT_FULL_TEXT(buffer);
149 }