diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/first_network_device.c | 5 | ||||
-rw-r--r-- | src/print_battery_info.c | 8 | ||||
-rw-r--r-- | src/print_eth_info.c | 54 | ||||
-rw-r--r-- | src/print_ip_addr.c | 13 | ||||
-rw-r--r-- | src/print_time.c | 2 | ||||
-rw-r--r-- | src/print_volume.c | 11 | ||||
-rw-r--r-- | src/print_wireless_info.c | 65 |
7 files changed, 124 insertions, 34 deletions
diff --git a/src/first_network_device.c b/src/first_network_device.c index 3f34cf2..1ed50a2 100644 --- a/src/first_network_device.c +++ b/src/first_network_device.c @@ -82,8 +82,6 @@ static net_type_t iface_type(const char *ifname) { if (strcmp(devtype, "wwan") == 0) return NET_TYPE_OTHER; - - return NET_TYPE_OTHER; #elif __OpenBSD__ /* *First determine if the device is a wireless device by trying two ioctl(2) @@ -126,9 +124,8 @@ static net_type_t iface_type(const char *ifname) { close(s); return NET_TYPE_ETHERNET; } -#else -#error Missing implementation to determine interface type. #endif + return NET_TYPE_OTHER; } const char *first_eth_interface(const net_type_t type) { diff --git a/src/print_battery_info.c b/src/print_battery_info.c index e22ca3e..7a462f7 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -513,13 +513,13 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char // We prefer the design capacity, but use the last capacity if we don't have it, // or if we are asked to (last_full_capacity == true); but similarly we use // the design capacity if we don't have the last capacity. - // If we don't have either then both full_design and full_last < 0, - // which implies full < 0, which bails out on the following line. + // If we don't have either then both full_design and full_last <= 0, + // which implies full <= 0, which bails out on the following line. int full = batt_info.full_design; - if (full < 0 || (last_full_capacity && batt_info.full_last >= 0)) { + if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) { full = batt_info.full_last; } - if (full < 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) { + if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) { /* We have no physical measurements and no estimates. Nothing * much we can report, then. */ OUTPUT_FULL_TEXT(format_down); diff --git a/src/print_eth_info.c b/src/print_eth_info.c index e4bf15e..b30d2b0 100644 --- a/src/print_eth_info.c +++ b/src/print_eth_info.c @@ -2,6 +2,7 @@ #include <string.h> #include <limits.h> #include <stdio.h> +#include <stdlib.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> @@ -118,25 +119,60 @@ static int print_eth_speed(char *outwalk, const char *interface) { /* * Combines ethernet IP addresses and speed (if requested) for displaying * + * Table summarizing what is the decision to prefer IPv4 or IPv6 + * based their values. + * + * | ipv4_address | ipv6_address | Chosen IP | Color | + * |--------------|--------------|-----------|-------------------| + * | NULL | NULL | None | bad (red) | + * | NULL | no IP | IPv6 | degraded (orange) | + * | NULL | ::1/128 | IPv6 | ok (green) | + * | no IP | NULL | IPv4 | degraded | + * | no IP | no IP | IPv4 | degraded | + * | no IP | ::1/128 | IPv6 | ok | + * | 127.0.0.1 | NULL | IPv4 | ok | + * | 127.0.0.1 | no IP | IPv4 | ok | + * | 127.0.0.1 | ::1/128 | IPv4 | ok | */ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) { const char *walk; - const char *ip_address = get_ip_addr(interface); char *outwalk = buffer; INSTANCE(interface); - if (ip_address == NULL) { - START_COLOR("color_bad"); - outwalk += sprintf(outwalk, "%s", format_down); - goto out; + char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET)); + char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6)); + + /* + * Removing '%' and following characters from IPv6 since the interface identifier is redundant, + * as the output already includes the interface name. + */ + if (ipv6_address != NULL) { + char *prct_ptr = strstr(ipv6_address, "%"); + if (prct_ptr != NULL) { + *prct_ptr = '\0'; + } } - if (BEGINS_WITH(ip_address, "no IP")) + bool prefer_ipv4 = true; + if (ipv4_address == NULL) { + if (ipv6_address == NULL) { + START_COLOR("color_bad"); + outwalk += sprintf(outwalk, "%s", format_down); + goto out; + } else { + prefer_ipv4 = false; + } + } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) { + prefer_ipv4 = false; + } + + const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address; + if (BEGINS_WITH(ip_address, "no IP")) { START_COLOR("color_degraded"); - else + } else { START_COLOR("color_good"); - + } for (walk = format_up; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; @@ -154,5 +190,7 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons out: END_COLOR; + free(ipv4_address); + free(ipv6_address); OUTPUT_FULL_TEXT(buffer); } diff --git a/src/print_ip_addr.c b/src/print_ip_addr.c index 09b0992..f9cd1f4 100644 --- a/src/print_ip_addr.c +++ b/src/print_ip_addr.c @@ -17,9 +17,14 @@ * interface is up and running but hasn't got an IP address yet * */ -const char *get_ip_addr(const char *interface) { +const char *get_ip_addr(const char *interface, int family) { static char part[512]; - socklen_t len = sizeof(struct sockaddr_in); + socklen_t len = 0; + if (family == AF_INET) + len = sizeof(struct sockaddr_in); + else if (family == AF_INET6) + len = sizeof(struct sockaddr_in6); + memset(part, 0, sizeof(part)); struct ifaddrs *ifaddr, *addrp; @@ -30,13 +35,13 @@ const char *get_ip_addr(const char *interface) { if (ifaddr == NULL) return NULL; - /* Skip until we are at the AF_INET address of interface */ + /* Skip until we are at the input family address of interface */ for (addrp = ifaddr; (addrp != NULL && (strcmp(addrp->ifa_name, interface) != 0 || addrp->ifa_addr == NULL || - addrp->ifa_addr->sa_family != AF_INET)); + addrp->ifa_addr->sa_family != family)); addrp = addrp->ifa_next) { /* Check if the interface is down */ diff --git a/src/print_time.c b/src/print_time.c index c8da9d6..3ed32b0 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -29,9 +29,9 @@ void set_timezone(const char *tz) { } else { unsetenv("TZ"); } - tzset(); current_timezone = tz; } + tzset(); } void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, time_t t) { diff --git a/src/print_volume.c b/src/print_volume.c index 51e84f3..be6a1d7 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -211,6 +211,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * #if defined(__OpenBSD__) int oclass_idx = -1, master_idx = -1, master_mute_idx = -1; + int master_next = AUDIO_MIXER_LAST; mixer_devinfo_t devinfo, devinfo2; mixer_ctrl_t vinfo; @@ -228,12 +229,17 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * devinfo2.index = 0; while (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo2) >= 0) { - if ((devinfo2.type == AUDIO_MIXER_VALUE) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmaster, MAX_AUDIO_DEV_LEN) == 0)) + if ((devinfo2.type == AUDIO_MIXER_VALUE) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmaster, MAX_AUDIO_DEV_LEN) == 0)) { master_idx = devinfo2.index; + master_next = devinfo2.next; + } if ((devinfo2.type == AUDIO_MIXER_ENUM) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmute, MAX_AUDIO_DEV_LEN) == 0)) - master_mute_idx = devinfo2.index; + if (master_next == devinfo2.index) + master_mute_idx = devinfo2.index; + if (master_next != AUDIO_MIXER_LAST) + master_next = devinfo2.next; devinfo2.index++; } @@ -246,6 +252,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * vinfo.dev = master_idx; vinfo.type = AUDIO_MIXER_VALUE; + vinfo.un.value.num_channels = devinfo.un.v.num_channels; if (ioctl(mixfd, AUDIO_MIXER_READ, &vinfo) == -1) goto out; diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 13f8184..c3b5270 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -464,6 +464,21 @@ error1: return 0; } +/* Table summarizing what is the decision to prefer IPv4 or IPv6 + * based their values. + * + * | ipv4_address | ipv6_address | Chosen IP | Color | + * |--------------|--------------|-----------|-------------------| + * | NULL | NULL | None | bad (red) | + * | NULL | no IP | IPv6 | degraded (orange) | + * | NULL | ::1/128 | IPv6 | ok (green) | + * | no IP | NULL | IPv4 | degraded | + * | no IP | no IP | IPv4 | degraded | + * | no IP | ::1/128 | IPv6 | ok | + * | 127.0.0.1 | NULL | IPv4 | ok | + * | 127.0.0.1 | no IP | IPv4 | ok | + * | 127.0.0.1 | ::1/128 | IPv4 | ok | + */ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) { const char *walk; char *outwalk = buffer; @@ -471,22 +486,48 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, INSTANCE(interface); - const char *ip_address = get_ip_addr(interface); - if (ip_address == NULL) { - START_COLOR("color_bad"); - outwalk += sprintf(outwalk, "%s", format_down); - goto out; + char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET)); + char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6)); + + /* + * Removing '%' and following characters from IPv6 since the interface identifier is redundant, + * as the output already includes the interface name. + */ + if (ipv6_address != NULL) { + char *prct_ptr = strstr(ipv6_address, "%"); + if (prct_ptr != NULL) { + *prct_ptr = '\0'; + } + } + + bool prefer_ipv4 = true; + if (ipv4_address == NULL) { + if (ipv6_address == NULL) { + START_COLOR("color_bad"); + outwalk += sprintf(outwalk, "%s", format_down); + goto out; + } else { + prefer_ipv4 = false; + } + } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) { + prefer_ipv4 = false; } - if (get_wireless_info(interface, &info)) { + const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address; + if (!get_wireless_info(interface, &info)) { + walk = format_down; + START_COLOR("color_bad"); + } else { walk = format_up; if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good")); - else - START_COLOR((BEGINS_WITH(ip_address, "no IP") ? "color_degraded" : "color_good")); - } else { - walk = format_down; - START_COLOR("color_bad"); + else { + if (BEGINS_WITH(ip_address, "no IP")) { + START_COLOR("color_degraded"); + } else { + START_COLOR("color_good"); + } + } } for (; *walk != '\0'; walk++) { @@ -568,5 +609,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, out: END_COLOR; + free(ipv4_address); + free(ipv6_address); OUTPUT_FULL_TEXT(buffer); } |