1 // vim:ts=4:sw=4:expandtab
7 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <yajl/yajl_gen.h>
12 #include <yajl/yajl_version.h>
17 #include <linux/ethtool.h>
18 #include <linux/sockios.h>
19 #define PART_ETHSPEED "E: %s (%d Mbit/s)"
22 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
23 #include <net/if_media.h>
25 #define PART_ETHSPEED "E: %s (%s)"
28 #if defined(__OpenBSD__) || defined(__NetBSD__)
30 #include <net/if_media.h>
33 static int print_eth_speed(char *outwalk, const char *interface) {
35 /* This code path requires root privileges */
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
122 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
124 const char *ip_address = get_ip_addr(interface);
125 char *outwalk = buffer;
129 if (ip_address == NULL) {
130 START_COLOR("color_bad");
131 outwalk += sprintf(outwalk, "%s", format_down);
135 if (BEGINS_WITH(ip_address, "no IP"))
136 START_COLOR("color_degraded");
138 START_COLOR("color_good");
140 for (walk = format_up; *walk != '\0'; walk++) {
142 *(outwalk++) = *walk;
146 if (BEGINS_WITH(walk + 1, "ip")) {
147 outwalk += sprintf(outwalk, "%s", ip_address);
148 walk += strlen("ip");
149 } else if (BEGINS_WITH(walk + 1, "speed")) {
150 outwalk += print_eth_speed(outwalk, interface);
151 walk += strlen("speed");
157 OUTPUT_FULL_TEXT(buffer);