]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
941b46582f74f5790274aaa850bceaf8c151b7f0
[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
29 #endif
30
31 static int print_eth_speed(char *outwalk, const char *interface) {
32 #if defined(LINUX)
33         /* This code path requires root privileges */
34         int ethspeed = 0;
35         struct ifreq ifr;
36         struct ethtool_cmd ecmd;
37
38         ecmd.cmd = ETHTOOL_GSET;
39         (void)memset(&ifr, 0, sizeof(ifr));
40         ifr.ifr_data = (caddr_t)&ecmd;
41         (void)strcpy(ifr.ifr_name, interface);
42         if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
43                 ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
44                 return sprintf(outwalk, "%d Mbit/s", ethspeed);
45         } else return sprintf(outwalk, "?");
46 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47         char *ethspeed;
48         struct ifmediareq ifm;
49         (void)memset(&ifm, 0, sizeof(ifm));
50         (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
51         int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
52
53         /* Get the description of the media type, partially taken from
54          * FreeBSD's ifconfig */
55         const struct ifmedia_description *desc;
56         struct ifmedia_description ifm_subtype_descriptions[] =
57                 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
58
59         for (desc = ifm_subtype_descriptions;
60              desc->ifmt_string != NULL;
61              desc++) {
62             if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
63                 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
64                 break;
65         }
66         ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
67         return sprintf(outwalk, "%s", ethspeed);
68 #else
69         return sprintf(outwalk, "?");
70 #endif
71 }
72
73 /*
74  * Combines ethernet IP addresses and speed (if requested) for displaying
75  *
76  */
77 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
78         const char *walk;
79         const char *ip_address = get_ip_addr(interface);
80         char *outwalk = buffer;
81
82         INSTANCE(interface);
83
84         if (ip_address == NULL) {
85                 START_COLOR("color_bad");
86                 outwalk += sprintf(outwalk, "%s", format_down);
87                 goto out;
88         }
89
90         START_COLOR("color_good");
91
92         for (walk = format_up; *walk != '\0'; walk++) {
93                 if (*walk != '%') {
94                         *(outwalk++) = *walk;
95                         continue;
96                 }
97
98                 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
99                         outwalk += sprintf(outwalk, "%s", ip_address);
100                         walk += strlen("ip");
101                 } else if (strncmp(walk+1, "speed", strlen("speed")) == 0) {
102                         outwalk += print_eth_speed(outwalk, interface);
103                         walk += strlen("speed");
104                 }
105         }
106
107 out:
108         END_COLOR;
109         OUTPUT_FULL_TEXT(buffer);
110 }