Added support for and fixed on Linux
[monky] / src / net_stat.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "conky.h"
33 #include "logging.h"
34 #include "specials.h"
35 #include "net/if.h"
36 #include "text_object.h"
37 #include "net_stat.h"
38 #include <errno.h>
39 #include <string.h>
40 #include <sys/ioctl.h>
41 #include <unistd.h>
42
43 /* network interface stuff */
44
45 struct net_stat netstats[16];
46
47 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
48 {
49         unsigned int i;
50
51         if (!dev) {
52                 return 0;
53         }
54
55         /* find interface stat */
56         for (i = 0; i < 16; i++) {
57                 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
58                         return &netstats[i];
59                 }
60         }
61
62         /* wasn't found? add it */
63         for (i = 0; i < 16; i++) {
64                 if (netstats[i].dev == 0) {
65                         netstats[i].dev = strndup(dev, text_buffer_size);
66                         return &netstats[i];
67                 }
68         }
69
70         CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
71         return 0;
72 }
73
74 void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash)
75 {
76         if (!arg)
77                 arg = DEFAULTNETDEV;
78
79         obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
80 }
81
82 void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
83 {
84         if (arg) {
85                 arg = scan_bar(obj, arg);
86                 obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
87         } else {
88                 // default to DEFAULTNETDEV
89                 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
90                 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
91                 free(buf);
92         }
93 }
94
95 void print_downspeed(struct text_object *obj, char *p, int p_max_size)
96 {
97         struct net_stat *ns = obj->data.opaque;
98
99         if (!ns)
100                 return;
101
102         human_readable(ns->recv_speed, p, p_max_size);
103 }
104
105 void print_downspeedf(struct text_object *obj, char *p, int p_max_size)
106 {
107         struct net_stat *ns = obj->data.opaque;
108
109         if (!ns)
110                 return;
111
112         spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0);
113 }
114
115 void print_upspeed(struct text_object *obj, char *p, int p_max_size)
116 {
117         struct net_stat *ns = obj->data.opaque;
118
119         if (!ns)
120                 return;
121
122         human_readable(ns->trans_speed, p, p_max_size);
123 }
124
125 void print_upspeedf(struct text_object *obj, char *p, int p_max_size)
126 {
127         struct net_stat *ns = obj->data.opaque;
128
129         if (!ns)
130                 return;
131
132         spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0);
133 }
134
135 void print_totaldown(struct text_object *obj, char *p, int p_max_size)
136 {
137         struct net_stat *ns = obj->data.opaque;
138
139         if (!ns)
140                 return;
141
142         human_readable(ns->recv, p, p_max_size);
143 }
144
145 void print_totalup(struct text_object *obj, char *p, int p_max_size)
146 {
147         struct net_stat *ns = obj->data.opaque;
148
149         if (!ns)
150                 return;
151
152         human_readable(ns->trans, p, p_max_size);
153 }
154
155 void print_addr(struct text_object *obj, char *p, int p_max_size)
156 {
157         struct net_stat *ns = obj->data.opaque;
158
159         if (!ns)
160                 return;
161
162         if ((ns->addr.sa_data[2] & 255) == 0 &&
163             (ns->addr.sa_data[3] & 255) == 0 &&
164             (ns->addr.sa_data[4] & 255) == 0 &&
165             (ns->addr.sa_data[5] & 255) == 0) {
166                 snprintf(p, p_max_size, "No Address");
167         } else {
168                 snprintf(p, p_max_size, "%u.%u.%u.%u",
169                          ns->addr.sa_data[2] & 255,
170                          ns->addr.sa_data[3] & 255,
171                          ns->addr.sa_data[4] & 255,
172                          ns->addr.sa_data[5] & 255);
173         }
174 }
175
176 #ifdef __linux__
177 void print_addrs(struct text_object *obj, char *p, int p_max_size)
178 {
179         struct net_stat *ns = obj->data.opaque;
180
181         if (!ns)
182                 return;
183
184         if (NULL != ns->addrs && strlen(ns->addrs) > 2) {
185                 ns->addrs[strlen(ns->addrs) - 2] = 0; /* remove ", " from end of string */
186                 strncpy(p, ns->addrs, p_max_size);
187         } else {
188                 strncpy(p, "0.0.0.0", p_max_size);
189         }
190 }
191 #endif /* __linux__ */
192
193 #ifdef X11
194 void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
195 {
196         char *buf = 0;
197         buf = scan_graph(obj, arg, 0);
198
199         // default to DEFAULTNETDEV
200         if (buf) {
201                 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
202                 free(buf);
203                 return;
204         }
205         obj->data.opaque = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
206 }
207
208 void print_downspeedgraph(struct text_object *obj, char *p)
209 {
210         struct net_stat *ns = obj->data.opaque;
211
212         if (!ns)
213                 return;
214
215         new_graph(obj, p, ns->recv_speed / 1024.0);
216 }
217
218 void print_upspeedgraph(struct text_object *obj, char *p)
219 {
220         struct net_stat *ns = obj->data.opaque;
221
222         if (!ns)
223                 return;
224
225         new_graph(obj, p, ns->trans_speed / 1024.0);
226 }
227 #endif /* X11 */
228
229 #ifdef __linux__
230 #ifdef HAVE_IWLIB
231 void print_wireless_essid(struct text_object *obj, char *p, int p_max_size)
232 {
233         struct net_stat *ns = obj->data.opaque;
234
235         if (!ns)
236                 return;
237
238         snprintf(p, p_max_size, "%s", ns->essid);
239 }
240 void print_wireless_mode(struct text_object *obj, char *p, int p_max_size)
241 {
242         struct net_stat *ns = obj->data.opaque;
243
244         if (!ns)
245                 return;
246
247         snprintf(p, p_max_size, "%s", ns->mode);
248 }
249 void print_wireless_bitrate(struct text_object *obj, char *p, int p_max_size)
250 {
251         struct net_stat *ns = obj->data.opaque;
252
253         if (!ns)
254                 return;
255
256         snprintf(p, p_max_size, "%s", ns->bitrate);
257 }
258 void print_wireless_ap(struct text_object *obj, char *p, int p_max_size)
259 {
260         struct net_stat *ns = obj->data.opaque;
261
262         if (!ns)
263                 return;
264
265         snprintf(p, p_max_size, "%s", ns->ap);
266 }
267 void print_wireless_link_qual(struct text_object *obj, char *p, int p_max_size)
268 {
269         struct net_stat *ns = obj->data.opaque;
270
271         if (!ns)
272                 return;
273
274         spaced_print(p, p_max_size, "%d", 4, ns->link_qual);
275 }
276 void print_wireless_link_qual_max(struct text_object *obj, char *p, int p_max_size)
277 {
278         struct net_stat *ns = obj->data.opaque;
279
280         if (!ns)
281                 return;
282
283         spaced_print(p, p_max_size, "%d", 4, ns->link_qual_max);
284 }
285 void print_wireless_link_qual_perc(struct text_object *obj, char *p, int p_max_size)
286 {
287         struct net_stat *ns = obj->data.opaque;
288
289         if (!ns)
290                 return;
291
292         if (ns->link_qual_max > 0) {
293                 spaced_print(p, p_max_size, "%.0f", 5,
294                                 (double) ns->link_qual /
295                                 ns->link_qual_max * 100);
296         } else {
297                 spaced_print(p, p_max_size, "unk", 5);
298         }
299 }
300 void print_wireless_link_bar(struct text_object *obj, char *p, int p_max_size)
301 {
302         struct net_stat *ns = obj->data.opaque;
303
304         if (!ns)
305                 return;
306
307 #ifdef X11
308         if(output_methods & TO_X) {
309                 new_bar(obj, p, ((double) ns->link_qual /
310                                         ns->link_qual_max) * 255.0);
311         } else
312 #endif /* X11 */
313                 new_bar_in_shell(obj, p, p_max_size, ((double) ns->link_qual /
314                                         ns->link_qual_max) * 100.0);
315 }
316 #endif /* HAVE_IWLIB */
317 #endif /* __linux__ */
318
319 void clear_net_stats(void)
320 {
321         int i;
322         for (i = 0; i < 16; i++) {
323                 if (netstats[i].dev) {
324                         free(netstats[i].dev);
325                 }
326         }
327         memset(netstats, 0, sizeof(netstats));
328 }
329
330 void parse_if_up_arg(struct text_object *obj, const char *arg)
331 {
332         obj->data.opaque = strndup(arg, text_buffer_size);
333 }
334
335 void free_if_up(struct text_object *obj)
336 {
337         if (obj->data.opaque) {
338                 free(obj->data.opaque);
339                 obj->data.opaque = NULL;
340         }
341 }
342
343 /* We should check if this is ok with OpenBSD and NetBSD as well. */
344 int interface_up(struct text_object *obj)
345 {
346         int fd;
347         struct ifreq ifr;
348         char *dev = obj->data.opaque;
349
350         if (!dev)
351                 return 0;
352
353         if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
354                 CRIT_ERR(NULL, NULL, "could not create sockfd");
355                 return 0;
356         }
357         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
358         if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
359                 /* if device does not exist, treat like not up */
360                 if (errno != ENODEV && errno != ENXIO)
361                         perror("SIOCGIFFLAGS");
362                 goto END_FALSE;
363         }
364
365         if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
366                 goto END_FALSE;
367         if (ifup_strictness == IFUP_UP)
368                 goto END_TRUE;
369
370         if (!(ifr.ifr_flags & IFF_RUNNING))
371                 goto END_FALSE;
372         if (ifup_strictness == IFUP_LINK)
373                 goto END_TRUE;
374
375         if (ioctl(fd, SIOCGIFADDR, &ifr)) {
376                 perror("SIOCGIFADDR");
377                 goto END_FALSE;
378         }
379         if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
380                 goto END_TRUE;
381
382 END_FALSE:
383         close(fd);
384         return 0;
385 END_TRUE:
386         close(fd);
387         return 1;
388 }
389
390 static struct {
391         int nscount;
392         char **ns_list;
393 } dns_data = {
394         .nscount = 0,
395         .ns_list = NULL,
396 };
397
398 void free_dns_data(void)
399 {
400         int i;
401         for (i = 0; i < dns_data.nscount; i++)
402                 free(dns_data.ns_list[i]);
403         if (dns_data.ns_list)
404                 free(dns_data.ns_list);
405         memset(&dns_data, 0, sizeof(dns_data));
406 }
407
408 void update_dns_data(void)
409 {
410         FILE *fp;
411         char line[256];
412         //static double last_dns_update = 0.0;
413
414         /* maybe updating too often causes higher load because of /etc lying on a real FS
415         if (current_update_time - last_dns_update < 10.0)
416                 return;
417
418         last_dns_update = current_update_time;
419         */
420
421         free_dns_data();
422
423         if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
424                 return;
425         while(!feof(fp)) {
426                 if (fgets(line, 255, fp) == NULL) {
427                         break;
428                 }
429                 if (!strncmp(line, "nameserver ", 11)) {
430                         line[strlen(line) - 1] = '\0';  // remove trailing newline
431                         dns_data.nscount++;
432                         dns_data.ns_list = realloc(dns_data.ns_list, dns_data.nscount * sizeof(char *));
433                         dns_data.ns_list[dns_data.nscount - 1] = strndup(line + 11, text_buffer_size);
434                 }
435         }
436         fclose(fp);
437 }
438
439 void parse_nameserver_arg(struct text_object *obj, const char *arg)
440 {
441         obj->data.l = arg ? atoi(arg) : 0;
442 }
443
444 void print_nameserver(struct text_object *obj, char *p, int p_max_size)
445 {
446         if (dns_data.nscount > obj->data.l)
447                 snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);
448 }