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