]> git.sur5r.net Git - i3/i3status/blob - src/get_eth_info.c
5e9ab542f151f304b4263204cc6d871f51ba6e5f
[i3/i3status] / src / get_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 <net/if.h>
8
9 #ifdef LINUX
10 #include <linux/ethtool.h>
11 #include <linux/sockios.h>
12 #endif
13
14 #include "i3status.h"
15
16 /*
17  * Combines ethernet IP addresses and speed (if requested) for displaying
18  *
19  */
20 const char *get_eth_info() {
21         static char part[512];
22         const char *ip_address = get_ip_addr(eth_interface);
23         int ethspeed = 0;
24
25         if (get_ethspeed) {
26 #ifdef LINUX
27                 /* This code path requires root privileges */
28                 struct ifreq ifr;
29                 struct ethtool_cmd ecmd;
30
31                 ecmd.cmd = ETHTOOL_GSET;
32                 (void)memset(&ifr, 0, sizeof(ifr));
33                 ifr.ifr_data = (caddr_t)&ecmd;
34                 (void)strcpy(ifr.ifr_name, eth_interface);
35                 if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0)
36                         ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
37                 else get_ethspeed = false;
38 #endif
39         }
40
41         if (ip_address == NULL)
42                 (void)snprintf(part, sizeof(part), "E: down");
43         else {
44                 if (get_ethspeed)
45                         (void)snprintf(part, sizeof(part), "E: %s (%d Mbit/s)", ip_address, ethspeed);
46                 else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
47         }
48
49         return part;
50 }