]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
Breaks configfiles! Major refactoring of i3status, see below
[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
12 #include "i3status.h"
13
14 #if defined(LINUX)
15 #include <linux/ethtool.h>
16 #include <linux/sockios.h>
17 #define PART_ETHSPEED  "E: %s (%d Mbit/s)"
18 #endif
19
20 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
21 #include <net/if_media.h>
22 #define IFM_TYPE_MATCH(dt, t)                       \
23         (IFM_TYPE((dt)) == 0 || IFM_TYPE((dt)) == IFM_TYPE((t)))
24
25 #define PART_ETHSPEED  "E: %s (%s)"
26
27 #endif
28
29 static void print_eth_speed(const char *interface) {
30 #if defined(LINUX)
31         int ethspeed = 0;
32 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
33         char *ethspeed;
34 #endif
35
36 #if defined(LINUX)
37         /* This code path requires root privileges */
38         struct ifreq ifr;
39         struct ethtool_cmd ecmd;
40
41         ecmd.cmd = ETHTOOL_GSET;
42         (void)memset(&ifr, 0, sizeof(ifr));
43         ifr.ifr_data = (caddr_t)&ecmd;
44         (void)strcpy(ifr.ifr_name, interface);
45         if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
46                 ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
47                 printf("%d Mbit/s", ethspeed);
48         } else printf("?");
49 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
50         struct ifmediareq ifm;
51         (void)memset(&ifm, 0, sizeof(ifm));
52         (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
53         int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
54
55         /* Get the description of the media type, partially taken from
56          * FreeBSD's ifconfig */
57         const struct ifmedia_description *desc;
58         struct ifmedia_description ifm_subtype_descriptions[] =
59                 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
60
61         for (desc = ifm_subtype_descriptions;
62              desc->ifmt_string != NULL;
63              desc++) {
64             if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
65                 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
66                 break;
67         }
68         ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
69         printf("%s", ethspeed);
70 #endif
71 }
72
73 /*
74  * Combines ethernet IP addresses and speed (if requested) for displaying
75  *
76  */
77 void print_eth_info(const char *interface, const char *format) {
78         const char *walk;
79         const char *ip_address = get_ip_addr(interface);
80
81         for (walk = format; *walk != '\0'; walk++) {
82                 if (*walk != '%') {
83                         putchar(*walk);
84                         continue;
85                 }
86
87                 if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
88                         printf("%s", ip_address);
89                         walk += strlen("ip");
90                 } else if (strncmp(walk+1, "speed", strlen("speed")) == 0) {
91                         print_eth_speed(interface);
92                         walk += strlen("speed");
93                 }
94         }
95 }