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