]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
bump version number
[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         int ethspeed = 0;
34 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
35         char *ethspeed;
36 #endif
37
38 #if defined(LINUX)
39         /* This code path requires root privileges */
40         struct ifreq ifr;
41         struct ethtool_cmd ecmd;
42
43         ecmd.cmd = ETHTOOL_GSET;
44         (void)memset(&ifr, 0, sizeof(ifr));
45         ifr.ifr_data = (caddr_t)&ecmd;
46         (void)strcpy(ifr.ifr_name, interface);
47         if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
48                 ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
49                 return sprintf(outwalk, "%d Mbit/s", ethspeed);
50         } else return sprintf(outwalk, "?");
51 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
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 #endif
73 }
74
75 /*
76  * Combines ethernet IP addresses and speed (if requested) for displaying
77  *
78  */
79 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
80         const char *walk;
81         const char *ip_address = get_ip_addr(interface);
82         char *outwalk = buffer;
83
84         INSTANCE(interface);
85
86         if (ip_address == NULL) {
87                 START_COLOR("color_bad");
88                 outwalk += sprintf(outwalk, "%s", format_down);
89                 goto out;
90         }
91
92         START_COLOR("color_good");
93
94         for (walk = format_up; *walk != '\0'; walk++) {
95                 if (*walk != '%') {
96                         *(outwalk++) = *walk;
97                         continue;
98                 }
99
100                 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
101                         outwalk += sprintf(outwalk, "%s", ip_address);
102                         walk += strlen("ip");
103                 } else if (strncmp(walk+1, "speed", strlen("speed")) == 0) {
104                         outwalk += print_eth_speed(outwalk, interface);
105                         walk += strlen("speed");
106                 }
107         }
108
109 out:
110         END_COLOR;
111         OUTPUT_FULL_TEXT(buffer);
112 }