X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_wireless_info.c;h=c3b52708f743a61f1ba218bc62b2e7bd7a0b0eac;hb=657c0498dbc4eed26df8b7af1c87ea943c2bbdf6;hp=3410ba93efb8f9a770e69bbbd8bef77c867a634f;hpb=e1203ca62ac13f29185e077b88d8cde0068188ed;p=i3%2Fi3status diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 3410ba9..c3b5270 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -15,6 +15,10 @@ #define IW_ESSID_MAX_SIZE 32 #endif +#ifdef __APPLE__ +#define IW_ESSID_MAX_SIZE 32 +#endif + #ifdef __FreeBSD__ #include #include @@ -45,11 +49,19 @@ #include #include #include +#include #include #include #include #include #include +#define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN +#endif + +#ifdef __NetBSD__ +#include +#include +#define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN #endif #include "i3status.h" @@ -64,8 +76,12 @@ typedef struct { int flags; +#ifdef IW_ESSID_MAX_SIZE char essid[IW_ESSID_MAX_SIZE + 1]; +#endif +#ifdef LINUX uint8_t bssid[ETH_ALEN]; +#endif int quality; int quality_max; int quality_average; @@ -77,6 +93,7 @@ typedef struct { double frequency; } wireless_info_t; +#ifdef LINUX // Like iw_print_bitrate, but without the dependency on libiw. static void print_bitrate(char *buffer, int buflen, int bitrate) { const int kilo = 1e3; @@ -100,7 +117,8 @@ static void print_bitrate(char *buffer, int buflen, int bitrate) { snprintf(buffer, buflen, "%g %cb/s", rate / divisor, scale); } -static uint32_t nl80211_xbm_to_percent(int32_t xbm, uint32_t divisor) { +// Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c +static uint32_t nl80211_xbm_to_percent(int32_t xbm, int32_t divisor) { #define NOISE_FLOOR_DBM -90 #define SIGNAL_MAX_DBM -20 @@ -113,9 +131,9 @@ static uint32_t nl80211_xbm_to_percent(int32_t xbm, uint32_t divisor) { return 100 - 70 * (((float)SIGNAL_MAX_DBM - (float)xbm) / ((float)SIGNAL_MAX_DBM - (float)NOISE_FLOOR_DBM)); } -#define WLAN_EID_SSID 0 - +// Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *ssid_len) { +#define WLAN_EID_SSID 0 *ssid = NULL; *ssid_len = 0; @@ -125,7 +143,7 @@ static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t * } if (ies_len < 2) return; - if (ies_len < 2 + ies[1]) + if (ies_len < (uint32_t)(2 + ies[1])) return; *ssid_len = ies[1]; @@ -174,7 +192,7 @@ static int gwi_sta_cb(struct nl_msg *msg, void *data) { static int gwi_scan_cb(struct nl_msg *msg, void *data) { wireless_info_t *info = data; - struct genlmsghdr *gnlh = genlmsg_hdr(nlmsg_hdr(msg)); + struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct nlattr *bss[NL80211_BSS_MAX + 1]; struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { @@ -250,6 +268,7 @@ static int gwi_scan_cb(struct nl_msg *msg, void *data) { return NL_SKIP; } +#endif static int get_wireless_info(const char *interface, wireless_info_t *info) { memset(info, 0, sizeof(wireless_info_t)); @@ -414,7 +433,7 @@ error1: else len = IEEE80211_NWID_LEN + 1; - strncpy(&info->essid[0], nwid.i_nwid, len); + strncpy(&info->essid[0], (char *)nwid.i_nwid, len); info->essid[IW_ESSID_MAX_SIZE] = '\0'; info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID; } @@ -445,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; @@ -452,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'; + } } - if (get_wireless_info(interface, &info)) { + 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 (!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++) { @@ -479,7 +539,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, if (BEGINS_WITH(walk + 1, "quality")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { if (info.quality_max) - outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max)); + outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.quality, info.quality_max), pct_mark); else outwalk += sprintf(outwalk, "%d", info.quality); } else { @@ -491,7 +551,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, if (BEGINS_WITH(walk + 1, "signal")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { if (info.signal_level_max) - outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max)); + outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); else outwalk += sprintf(outwalk, "%d dBm", info.signal_level); } else { @@ -503,7 +563,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, if (BEGINS_WITH(walk + 1, "noise")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { if (info.noise_level_max) - outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max)); + outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); else outwalk += sprintf(outwalk, "%d dBm", info.noise_level); } else { @@ -513,9 +573,11 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, } if (BEGINS_WITH(walk + 1, "essid")) { +#ifdef IW_ESSID_MAX_SIZE if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) - outwalk += sprintf(outwalk, "%s", info.essid); + maybe_escape_markup(info.essid, &outwalk); else +#endif *(outwalk++) = '?'; walk += strlen("essid"); } @@ -547,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); }