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) {
38 struct ethtool_cmd ecmd;
40 ecmd.cmd = ETHTOOL_GSET;
41 (void)memset(&ifr, 0, sizeof(ifr));
42 ifr.ifr_data = (caddr_t)&ecmd;
43 (void)strcpy(ifr.ifr_name, interface);
44 if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
45 ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
46 return sprintf(outwalk, "%d Mbit/s", ethspeed);
48 return sprintf(outwalk, "?");
49 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
51 struct ifmediareq ifm;
52 (void)memset(&ifm, 0, sizeof(ifm));
53 (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
56 ret = ioctl(general_socket, SIOCGIFXMEDIA, (caddr_t)&ifm);
59 ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
61 return sprintf(outwalk, "?");
63 /* Get the description of the media type, partially taken from
64 * FreeBSD's ifconfig */
65 const struct ifmedia_description *desc;
66 static struct ifmedia_description ifm_subtype_descriptions[] =
67 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
69 if (IFM_TYPE(ifm.ifm_active) != IFM_ETHER)
70 return sprintf(outwalk, "?");
71 if (ifm.ifm_status & IFM_AVALID && !(ifm.ifm_status & IFM_ACTIVE))
72 return sprintf(outwalk, "no carrier");
73 for (desc = ifm_subtype_descriptions;
74 desc->ifmt_string != NULL;
76 if (desc->ifmt_word == IFM_SUBTYPE(ifm.ifm_active))
79 ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
80 return sprintf(outwalk, "%s", ethspeed);
81 #elif defined(__OpenBSD__) || defined(__NetBSD__)
83 struct ifmediareq ifmr;
85 (void)memset(&ifmr, 0, sizeof(ifmr));
86 (void)strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
88 if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
90 return sprintf(outwalk, "?");
93 struct ifmedia_description *desc;
94 struct ifmedia_description ifm_subtype_descriptions[] =
95 IFM_SUBTYPE_DESCRIPTIONS;
97 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
99 * Skip these non-informative values and go right ahead to the
102 if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
103 BEGINS_WITH(desc->ifmt_string, "auto"))
106 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
107 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
110 ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
111 return sprintf(outwalk, "%s", ethspeed);
114 return sprintf(outwalk, "?");
119 * Combines ethernet IP addresses and speed (if requested) for displaying
121 * Table summarizing what is the decision to prefer IPv4 or IPv6
122 * based their values.
124 * | ipv4_address | ipv6_address | Chosen IP | Color |
125 * |--------------|--------------|-----------|-------------------|
126 * | NULL | NULL | None | bad (red) |
127 * | NULL | no IP | IPv6 | degraded (orange) |
128 * | NULL | ::1/128 | IPv6 | ok (green) |
129 * | no IP | NULL | IPv4 | degraded |
130 * | no IP | no IP | IPv4 | degraded |
131 * | no IP | ::1/128 | IPv6 | ok |
132 * | 127.0.0.1 | NULL | IPv4 | ok |
133 * | 127.0.0.1 | no IP | IPv4 | ok |
134 * | 127.0.0.1 | ::1/128 | IPv4 | ok |
136 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
137 const char *format = format_down; // default format
140 char *outwalk = buffer;
144 char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET));
145 char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6));
148 * Removing '%' and following characters from IPv6 since the interface identifier is redundant,
149 * as the output already includes the interface name.
151 if (ipv6_address != NULL) {
152 char *prct_ptr = strstr(ipv6_address, "%");
153 if (prct_ptr != NULL) {
158 bool prefer_ipv4 = true;
159 if (ipv4_address == NULL) {
160 if (ipv6_address == NULL) {
161 START_COLOR("color_bad");
166 } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) {
172 const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address;
173 if (BEGINS_WITH(ip_address, "no IP")) {
174 START_COLOR("color_degraded");
176 START_COLOR("color_good");
180 for (walk = format; *walk != '\0'; walk++) {
182 *(outwalk++) = *walk;
184 } else if (BEGINS_WITH(walk + 1, "ip")) {
185 outwalk += sprintf(outwalk, "%s", ip_address);
186 walk += strlen("ip");
188 } else if (BEGINS_WITH(walk + 1, "speed")) {
189 outwalk += print_eth_speed(outwalk, interface);
190 walk += strlen("speed");
192 } else if (BEGINS_WITH(walk + 1, "interface")) {
193 outwalk += sprintf(outwalk, "%s", interface);
194 walk += strlen("interface");
203 OUTPUT_FULL_TEXT(buffer);