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