]> git.sur5r.net Git - i3/i3status/blob - src/print_wireless_info.c
Merge pull request #149 from tommie/incremental_multibatt
[i3/i3status] / src / print_wireless_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdio.h>
3 #include <string.h>
4 #include <yajl/yajl_gen.h>
5 #include <yajl/yajl_version.h>
6
7 #ifdef LINUX
8 #include <errno.h>
9 #include <net/if.h>
10 #include <netlink/netlink.h>
11 #include <netlink/genl/genl.h>
12 #include <netlink/genl/ctrl.h>
13 #include <linux/nl80211.h>
14 #include <linux/if_ether.h>
15 #define IW_ESSID_MAX_SIZE 32
16 #endif
17
18 #ifdef __APPLE__
19 #define IW_ESSID_MAX_SIZE 32
20 #endif
21
22 #ifdef __FreeBSD__
23 #include <sys/param.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <ifaddrs.h>
27 #include <net/if.h>
28 #include <net/if_media.h>
29 #include <net80211/ieee80211.h>
30 #include <net80211/ieee80211_ioctl.h>
31 #include <unistd.h>
32 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
33 #endif
34
35 #ifdef __DragonFly__
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <ifaddrs.h>
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <netproto/802_11/ieee80211.h>
43 #include <netproto/802_11/ieee80211_ioctl.h>
44 #include <unistd.h>
45 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
46 #endif
47
48 #ifdef __OpenBSD__
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <net/if.h>
52 #include <sys/types.h>
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
55 #include <net80211/ieee80211.h>
56 #include <net80211/ieee80211_ioctl.h>
57 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
58 #endif
59
60 #ifdef __NetBSD__
61 #include <sys/types.h>
62 #include <net80211/ieee80211.h>
63 #define IW_ESSID_MAX_SIZE IEEE80211_NWID_LEN
64 #endif
65
66 #include "i3status.h"
67
68 #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
69 #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
70 #define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2)
71 #define WIRELESS_INFO_FLAG_HAS_NOISE (1 << 3)
72 #define WIRELESS_INFO_FLAG_HAS_FREQUENCY (1 << 4)
73
74 #define PERCENT_VALUE(value, total) ((int)(value * 100 / (float)total + 0.5f))
75
76 typedef struct {
77     int flags;
78     char essid[IW_ESSID_MAX_SIZE + 1];
79 #ifdef LINUX
80     uint8_t bssid[ETH_ALEN];
81 #endif
82     int quality;
83     int quality_max;
84     int quality_average;
85     int signal_level;
86     int signal_level_max;
87     int noise_level;
88     int noise_level_max;
89     int bitrate;
90     double frequency;
91 } wireless_info_t;
92
93 #ifdef LINUX
94 // Like iw_print_bitrate, but without the dependency on libiw.
95 static void print_bitrate(char *buffer, int buflen, int bitrate) {
96     const int kilo = 1e3;
97     const int mega = 1e6;
98     const int giga = 1e9;
99
100     const double rate = bitrate;
101     char scale;
102     int divisor;
103
104     if (rate >= giga) {
105         scale = 'G';
106         divisor = giga;
107     } else if (rate >= mega) {
108         scale = 'M';
109         divisor = mega;
110     } else {
111         scale = 'k';
112         divisor = kilo;
113     }
114     snprintf(buffer, buflen, "%g %cb/s", rate / divisor, scale);
115 }
116
117 // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
118 static uint32_t nl80211_xbm_to_percent(int32_t xbm, int32_t divisor) {
119 #define NOISE_FLOOR_DBM -90
120 #define SIGNAL_MAX_DBM -20
121
122     xbm /= divisor;
123     if (xbm < NOISE_FLOOR_DBM)
124         xbm = NOISE_FLOOR_DBM;
125     if (xbm > SIGNAL_MAX_DBM)
126         xbm = SIGNAL_MAX_DBM;
127
128     return 100 - 70 * (((float)SIGNAL_MAX_DBM - (float)xbm) / ((float)SIGNAL_MAX_DBM - (float)NOISE_FLOOR_DBM));
129 }
130
131 // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
132 static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *ssid_len) {
133 #define WLAN_EID_SSID 0
134     *ssid = NULL;
135     *ssid_len = 0;
136
137     while (ies_len > 2 && ies[0] != WLAN_EID_SSID) {
138         ies_len -= ies[1] + 2;
139         ies += ies[1] + 2;
140     }
141     if (ies_len < 2)
142         return;
143     if (ies_len < (uint32_t)(2 + ies[1]))
144         return;
145
146     *ssid_len = ies[1];
147     *ssid = ies + 2;
148 }
149
150 static int gwi_sta_cb(struct nl_msg *msg, void *data) {
151     wireless_info_t *info = data;
152
153     struct nlattr *tb[NL80211_ATTR_MAX + 1];
154     struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
155     struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
156     struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
157     static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
158             [NL80211_STA_INFO_RX_BITRATE] = {.type = NLA_NESTED},
159     };
160
161     static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
162             [NL80211_RATE_INFO_BITRATE] = {.type = NLA_U16},
163     };
164
165     if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
166         return NL_SKIP;
167
168     if (tb[NL80211_ATTR_STA_INFO] == NULL)
169         return NL_SKIP;
170
171     if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, tb[NL80211_ATTR_STA_INFO], stats_policy))
172         return NL_SKIP;
173
174     if (sinfo[NL80211_STA_INFO_RX_BITRATE] == NULL)
175         return NL_SKIP;
176
177     if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
178         return NL_SKIP;
179
180     if (rinfo[NL80211_RATE_INFO_BITRATE] == NULL)
181         return NL_SKIP;
182
183     // NL80211_RATE_INFO_BITRATE is specified in units of 100 kbit/s, but iw
184     // used to specify bit/s, so we convert to use the same code path.
185     info->bitrate = (int)nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100 * 1000;
186
187     return NL_SKIP;
188 }
189
190 static int gwi_scan_cb(struct nl_msg *msg, void *data) {
191     wireless_info_t *info = data;
192     struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
193     struct nlattr *tb[NL80211_ATTR_MAX + 1];
194     struct nlattr *bss[NL80211_BSS_MAX + 1];
195     struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
196             [NL80211_BSS_FREQUENCY] = {.type = NLA_U32},
197             [NL80211_BSS_BSSID] = {.type = NLA_UNSPEC},
198             [NL80211_BSS_INFORMATION_ELEMENTS] = {.type = NLA_UNSPEC},
199             [NL80211_BSS_SIGNAL_MBM] = {.type = NLA_U32},
200             [NL80211_BSS_SIGNAL_UNSPEC] = {.type = NLA_U8},
201             [NL80211_BSS_STATUS] = {.type = NLA_U32},
202     };
203
204     if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
205         return NL_SKIP;
206
207     if (tb[NL80211_ATTR_BSS] == NULL)
208         return NL_SKIP;
209
210     if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy))
211         return NL_SKIP;
212
213     if (bss[NL80211_BSS_STATUS] == NULL)
214         return NL_SKIP;
215
216     const uint32_t status = nla_get_u32(bss[NL80211_BSS_STATUS]);
217
218     if (status != NL80211_BSS_STATUS_ASSOCIATED &&
219         status != NL80211_BSS_STATUS_IBSS_JOINED)
220         return NL_SKIP;
221
222     if (bss[NL80211_BSS_BSSID] == NULL)
223         return NL_SKIP;
224
225     memcpy(info->bssid, nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
226
227     if (bss[NL80211_BSS_FREQUENCY]) {
228         info->flags |= WIRELESS_INFO_FLAG_HAS_FREQUENCY;
229         info->frequency = (double)nla_get_u32(bss[NL80211_BSS_FREQUENCY]) * 1e6;
230     }
231
232     if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
233         info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
234         info->signal_level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
235         info->signal_level_max = 100;
236
237         info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
238         info->quality = info->signal_level;
239         info->quality_max = 100;
240         info->quality_average = 50;
241     }
242
243     if (bss[NL80211_BSS_SIGNAL_MBM]) {
244         info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
245         info->signal_level = (int)nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]) / 100;
246
247         info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY;
248         info->quality = nl80211_xbm_to_percent(nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]), 100);
249         info->quality_max = 100;
250         info->quality_average = 50;
251     }
252
253     if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
254         uint8_t *ssid;
255         uint32_t ssid_len;
256
257         find_ssid(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
258                   nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
259                   &ssid, &ssid_len);
260         if (ssid && ssid_len) {
261             info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
262             snprintf(info->essid, sizeof(info->essid), "%.*s", ssid_len, ssid);
263         }
264     }
265
266     return NL_SKIP;
267 }
268 #endif
269
270 static int get_wireless_info(const char *interface, wireless_info_t *info) {
271     memset(info, 0, sizeof(wireless_info_t));
272
273 #ifdef LINUX
274     struct nl_sock *sk = nl_socket_alloc();
275     if (genl_connect(sk) != 0)
276         goto error1;
277
278     if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_scan_cb, info) < 0)
279         goto error1;
280
281     const int nl80211_id = genl_ctrl_resolve(sk, "nl80211");
282     if (nl80211_id < 0)
283         goto error1;
284
285     const unsigned int ifidx = if_nametoindex(interface);
286     if (ifidx == 0)
287         goto error1;
288
289     struct nl_msg *msg = NULL;
290     if ((msg = nlmsg_alloc()) == NULL)
291         goto error1;
292
293     if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0) ||
294         nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0)
295         goto error2;
296
297     if (nl_send_sync(sk, msg) < 0)
298         // nl_send_sync calls nlmsg_free()
299         goto error1;
300     msg = NULL;
301
302     if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_sta_cb, info) < 0)
303         goto error1;
304
305     if ((msg = nlmsg_alloc()) == NULL)
306         goto error1;
307
308     if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_STATION, 0) || nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0 || nla_put(msg, NL80211_ATTR_MAC, 6, info->bssid) < 0)
309         goto error2;
310
311     if (nl_send_sync(sk, msg) < 0)
312         // nl_send_sync calls nlmsg_free()
313         goto error1;
314     msg = NULL;
315
316     nl_socket_free(sk);
317     return 1;
318
319 error2:
320     nlmsg_free(msg);
321 error1:
322     nl_socket_free(sk);
323     return 0;
324
325 #endif
326 #if defined(__FreeBSD__) || defined(__DragonFly__)
327     int s, inwid;
328     union {
329         struct ieee80211req_sta_req req;
330         uint8_t buf[24 * 1024];
331     } u;
332     struct ieee80211req na;
333     char bssid[IEEE80211_ADDR_LEN];
334     size_t len;
335
336     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
337         return (0);
338
339     memset(&na, 0, sizeof(na));
340     strlcpy(na.i_name, interface, sizeof(na.i_name));
341     na.i_type = IEEE80211_IOC_SSID;
342     na.i_data = &info->essid[0];
343     na.i_len = IEEE80211_NWID_LEN + 1;
344     if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
345         close(s);
346         return (0);
347     }
348     if (inwid == 0) {
349         if (na.i_len <= IEEE80211_NWID_LEN)
350             len = na.i_len + 1;
351         else
352             len = IEEE80211_NWID_LEN + 1;
353         info->essid[len - 1] = '\0';
354     } else {
355         close(s);
356         return (0);
357     }
358     info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
359
360     memset(&na, 0, sizeof(na));
361     strlcpy(na.i_name, interface, sizeof(na.i_name));
362     na.i_type = IEEE80211_IOC_BSSID;
363     na.i_data = bssid;
364     na.i_len = sizeof(bssid);
365
366     if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
367         close(s);
368         return (0);
369     }
370
371     memcpy(u.req.is_u.macaddr, bssid, sizeof(bssid));
372     memset(&na, 0, sizeof(na));
373     strlcpy(na.i_name, interface, sizeof(na.i_name));
374     na.i_type = IEEE80211_IOC_STA_INFO;
375     na.i_data = &u;
376     na.i_len = sizeof(u);
377
378     if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
379         close(s);
380         return (0);
381     }
382
383     close(s);
384     if (na.i_len >= sizeof(u.req)) {
385         /*
386          * Just use the first BSSID returned even if there are
387          * multiple APs sharing the same BSSID.
388          */
389         info->signal_level = u.req.info[0].isi_rssi / 2 +
390                              u.req.info[0].isi_noise;
391         info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
392         info->noise_level = u.req.info[0].isi_noise;
393         info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
394     }
395
396     return 1;
397 #endif
398 #ifdef __OpenBSD__
399     struct ifreq ifr;
400     struct ieee80211_bssid bssid;
401     struct ieee80211_nwid nwid;
402     struct ieee80211_nodereq nr;
403
404     struct ether_addr ea;
405
406     int s, len, ibssid, inwid;
407     u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
408
409     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
410         return (0);
411
412     memset(&ifr, 0, sizeof(ifr));
413     ifr.ifr_data = (caddr_t)&nwid;
414     (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
415     inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
416
417     memset(&bssid, 0, sizeof(bssid));
418     strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
419     ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
420
421     if (ibssid != 0 || inwid != 0) {
422         close(s);
423         return 0;
424     }
425
426     /* NWID */
427     {
428         if (nwid.i_len <= IEEE80211_NWID_LEN)
429             len = nwid.i_len + 1;
430         else
431             len = IEEE80211_NWID_LEN + 1;
432
433         strncpy(&info->essid[0], (char *)nwid.i_nwid, len);
434         info->essid[IW_ESSID_MAX_SIZE] = '\0';
435         info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
436     }
437
438     /* Signal strength */
439     {
440         memset(&zero_bssid, 0, sizeof(zero_bssid));
441         if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
442             memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
443
444             bzero(&nr, sizeof(nr));
445             bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
446             strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
447
448             if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
449                 info->signal_level = nr.nr_rssi;
450                 if (nr.nr_max_rssi)
451                     info->signal_level_max = nr.nr_max_rssi;
452
453                 info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
454             }
455         }
456     }
457
458     close(s);
459     return 1;
460 #endif
461     return 0;
462 }
463
464 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
465     const char *walk;
466     char *outwalk = buffer;
467     wireless_info_t info;
468
469     INSTANCE(interface);
470
471     const char *ip_address = get_ip_addr(interface);
472     if (ip_address == NULL) {
473         START_COLOR("color_bad");
474         outwalk += sprintf(outwalk, "%s", format_down);
475         goto out;
476     }
477
478     if (get_wireless_info(interface, &info)) {
479         walk = format_up;
480         if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
481             START_COLOR((info.quality < info.quality_average ? "color_degraded" : "color_good"));
482         else
483             START_COLOR((BEGINS_WITH(ip_address, "no IP") ? "color_degraded" : "color_good"));
484     } else {
485         walk = format_down;
486         START_COLOR("color_bad");
487     }
488
489     for (; *walk != '\0'; walk++) {
490         if (*walk != '%') {
491             *(outwalk++) = *walk;
492             continue;
493         }
494
495         if (BEGINS_WITH(walk + 1, "quality")) {
496             if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
497                 if (info.quality_max)
498                     outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.quality, info.quality_max), pct_mark);
499                 else
500                     outwalk += sprintf(outwalk, "%d", info.quality);
501             } else {
502                 *(outwalk++) = '?';
503             }
504             walk += strlen("quality");
505         }
506
507         if (BEGINS_WITH(walk + 1, "signal")) {
508             if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
509                 if (info.signal_level_max)
510                     outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark);
511                 else
512                     outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
513             } else {
514                 *(outwalk++) = '?';
515             }
516             walk += strlen("signal");
517         }
518
519         if (BEGINS_WITH(walk + 1, "noise")) {
520             if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
521                 if (info.noise_level_max)
522                     outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark);
523                 else
524                     outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
525             } else {
526                 *(outwalk++) = '?';
527             }
528             walk += strlen("noise");
529         }
530
531         if (BEGINS_WITH(walk + 1, "essid")) {
532             if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
533                 maybe_escape_markup(info.essid, &outwalk);
534             else
535                 *(outwalk++) = '?';
536             walk += strlen("essid");
537         }
538
539         if (BEGINS_WITH(walk + 1, "frequency")) {
540             if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
541                 outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9);
542             else
543                 *(outwalk++) = '?';
544             walk += strlen("frequency");
545         }
546
547         if (BEGINS_WITH(walk + 1, "ip")) {
548             outwalk += sprintf(outwalk, "%s", ip_address);
549             walk += strlen("ip");
550         }
551
552 #ifdef LINUX
553         if (BEGINS_WITH(walk + 1, "bitrate")) {
554             char br_buffer[128];
555
556             print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
557
558             outwalk += sprintf(outwalk, "%s", br_buffer);
559             walk += strlen("bitrate");
560         }
561 #endif
562     }
563
564 out:
565     END_COLOR;
566     OUTPUT_FULL_TEXT(buffer);
567 }