1 // vim:ts=4:sw=4:expandtab
8 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <yajl/yajl_gen.h>
13 #include <yajl/yajl_version.h>
18 #include <linux/ethtool.h>
19 #include <linux/sockios.h>
20 #define PART_ETHSPEED "E: %s (%d Mbit/s)"
23 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
24 #include <net/if_media.h>
26 #define PART_ETHSPEED "E: %s (%s)"
29 #if defined(__OpenBSD__) || defined(__NetBSD__)
31 #include <net/if_media.h>
34 static int print_eth_speed(char *outwalk, const char *interface) {
36 /* This code path requires root privileges */
39 struct ethtool_cmd ecmd;
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 return sprintf(outwalk, "%d Mbit/s", ethspeed);
49 return sprintf(outwalk, "?");
50 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
52 struct ifmediareq ifm;
53 (void)memset(&ifm, 0, sizeof(ifm));
54 (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
57 ret = ioctl(general_socket, SIOCGIFXMEDIA, (caddr_t)&ifm);
60 ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
62 return sprintf(outwalk, "?");
64 /* Get the description of the media type, partially taken from
65 * FreeBSD's ifconfig */
66 const struct ifmedia_description *desc;
67 static struct ifmedia_description ifm_subtype_descriptions[] =
68 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
70 if (IFM_TYPE(ifm.ifm_active) != IFM_ETHER)
71 return sprintf(outwalk, "?");
72 if (ifm.ifm_status & IFM_AVALID && !(ifm.ifm_status & IFM_ACTIVE))
73 return sprintf(outwalk, "no carrier");
74 for (desc = ifm_subtype_descriptions;
75 desc->ifmt_string != NULL;
77 if (desc->ifmt_word == IFM_SUBTYPE(ifm.ifm_active))
80 ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
81 return sprintf(outwalk, "%s", ethspeed);
82 #elif defined(__OpenBSD__) || defined(__NetBSD__)
84 struct ifmediareq ifmr;
86 (void)memset(&ifmr, 0, sizeof(ifmr));
87 (void)strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
89 if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
91 return sprintf(outwalk, "?");
94 struct ifmedia_description *desc;
95 struct ifmedia_description ifm_subtype_descriptions[] =
96 IFM_SUBTYPE_DESCRIPTIONS;
98 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
100 * Skip these non-informative values and go right ahead to the
103 if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
104 BEGINS_WITH(desc->ifmt_string, "auto"))
107 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
108 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
111 ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
112 return sprintf(outwalk, "%s", ethspeed);
115 return sprintf(outwalk, "?");
120 * Combines ethernet IP addresses and speed (if requested) for displaying
122 * Table summarizing what is the decision to prefer IPv4 or IPv6
123 * based their values.
125 * | ipv4_address | ipv6_address | Chosen IP | Color |
126 * |--------------|--------------|-----------|-------------------|
127 * | NULL | NULL | None | bad (red) |
128 * | NULL | no IP | IPv6 | degraded (orange) |
129 * | NULL | ::1/128 | IPv6 | ok (green) |
130 * | no IP | NULL | IPv4 | degraded |
131 * | no IP | no IP | IPv4 | degraded |
132 * | no IP | ::1/128 | IPv6 | ok |
133 * | 127.0.0.1 | NULL | IPv4 | ok |
134 * | 127.0.0.1 | no IP | IPv4 | ok |
135 * | 127.0.0.1 | ::1/128 | IPv4 | ok |
137 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
139 char *outwalk = buffer;
143 char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET));
144 char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6));
147 * Removing '%' and following characters from IPv6 since the interface identifier is redundant,
148 * as the output already includes the interface name.
150 if (ipv6_address != NULL) {
151 char *prct_ptr = strstr(ipv6_address, "%");
152 if (prct_ptr != NULL) {
157 bool prefer_ipv4 = true;
158 if (ipv4_address == NULL) {
159 if (ipv6_address == NULL) {
160 START_COLOR("color_bad");
161 outwalk += sprintf(outwalk, "%s", format_down);
166 } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) {
170 const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address;
171 if (BEGINS_WITH(ip_address, "no IP")) {
172 START_COLOR("color_degraded");
174 START_COLOR("color_good");
176 for (walk = format_up; *walk != '\0'; walk++) {
178 *(outwalk++) = *walk;
182 if (BEGINS_WITH(walk + 1, "ip")) {
183 outwalk += sprintf(outwalk, "%s", ip_address);
184 walk += strlen("ip");
185 } else if (BEGINS_WITH(walk + 1, "speed")) {
186 outwalk += print_eth_speed(outwalk, interface);
187 walk += strlen("speed");
195 OUTPUT_FULL_TEXT(buffer);