]> git.sur5r.net Git - i3/i3status/blob - src/get_eth_info.c
Support for (Debian) GNU/kFreeBSD
[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 <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 /*
30  * Combines ethernet IP addresses and speed (if requested) for displaying
31  *
32  */
33 const char *get_eth_info() {
34         static char part[512];
35 #if defined(LINUX)
36         int ethspeed=0;
37 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
38         char *ethspeed;
39 #endif
40         const char *ip_address = get_ip_addr(eth_interface);
41
42         if (ip_address == NULL) {
43                 (void)snprintf(part, sizeof(part), "E: down");
44                 return part;
45         }
46
47         if (get_ethspeed) {
48 #if defined(LINUX)
49                 /* This code path requires root privileges */
50                 struct ifreq ifr;
51                 struct ethtool_cmd ecmd;
52
53                 ecmd.cmd = ETHTOOL_GSET;
54                 (void)memset(&ifr, 0, sizeof(ifr));
55                 ifr.ifr_data = (caddr_t)&ecmd;
56                 (void)strcpy(ifr.ifr_name, eth_interface);
57                 if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0)
58                         ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
59                 else get_ethspeed = false;
60 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
61                 struct ifmediareq ifm;
62                 (void)memset(&ifm, 0, sizeof(ifm));
63                 (void)strncpy(ifm.ifm_name, eth_interface, sizeof(ifm.ifm_name));
64                 int ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
65
66                 /* Get the description of the media type, partially taken from
67                  * FreeBSD's ifconfig */
68                 const struct ifmedia_description *desc;
69                 struct ifmedia_description ifm_subtype_descriptions[] =
70                         IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
71
72                 for (desc = ifm_subtype_descriptions;
73                      desc->ifmt_string != NULL;
74                      desc++) {
75                     if (IFM_TYPE_MATCH(desc->ifmt_word, ifm.ifm_active) &&
76                         IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifm.ifm_active))
77                         break;
78                 }
79                 ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
80 #endif
81         }
82
83         if (get_ethspeed)
84                 (void)snprintf(part, sizeof(part), PART_ETHSPEED, ip_address, ethspeed);
85         else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
86
87         return part;
88 }