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