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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
36 #include "text_object.h"
40 #include <sys/ioctl.h>
42 /* network interface stuff */
44 struct net_stat netstats[16];
46 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
54 /* find interface stat */
55 for (i = 0; i < 16; i++) {
56 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
61 /* wasn't found? add it */
62 for (i = 0; i < 16; i++) {
63 if (netstats[i].dev == 0) {
64 netstats[i].dev = strndup(dev, text_buffer_size);
69 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
73 void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash)
78 obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
81 void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
84 arg = scan_bar(obj, arg);
85 obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
87 // default to DEFAULTNETDEV
88 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
89 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
94 void print_downspeed(struct text_object *obj, char *p, int p_max_size)
96 struct net_stat *ns = obj->data.opaque;
101 human_readable(ns->recv_speed, p, p_max_size);
104 void print_downspeedf(struct text_object *obj, char *p, int p_max_size)
106 struct net_stat *ns = obj->data.opaque;
111 spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0);
114 void print_upspeed(struct text_object *obj, char *p, int p_max_size)
116 struct net_stat *ns = obj->data.opaque;
121 human_readable(ns->trans_speed, p, p_max_size);
124 void print_upspeedf(struct text_object *obj, char *p, int p_max_size)
126 struct net_stat *ns = obj->data.opaque;
131 spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0);
134 void print_totaldown(struct text_object *obj, char *p, int p_max_size)
136 struct net_stat *ns = obj->data.opaque;
141 human_readable(ns->recv, p, p_max_size);
144 void print_totalup(struct text_object *obj, char *p, int p_max_size)
146 struct net_stat *ns = obj->data.opaque;
151 human_readable(ns->trans, p, p_max_size);
154 void print_addr(struct text_object *obj, char *p, int p_max_size)
156 struct net_stat *ns = obj->data.opaque;
161 if ((ns->addr.sa_data[2] & 255) == 0 &&
162 (ns->addr.sa_data[3] & 255) == 0 &&
163 (ns->addr.sa_data[4] & 255) == 0 &&
164 (ns->addr.sa_data[5] & 255) == 0) {
165 snprintf(p, p_max_size, "No Address");
167 snprintf(p, p_max_size, "%u.%u.%u.%u",
168 ns->addr.sa_data[2] & 255,
169 ns->addr.sa_data[3] & 255,
170 ns->addr.sa_data[4] & 255,
171 ns->addr.sa_data[5] & 255);
176 void print_addrs(struct text_object *obj, char *p, int p_max_size)
178 struct net_stat *ns = obj->data.opaque;
183 if (NULL != ns->addrs && strlen(ns->addrs) > 2) {
184 ns->addrs[strlen(ns->addrs) - 2] = 0; /* remove ", " from end of string */
185 strncpy(p, ns->addrs, p_max_size);
187 strncpy(p, "0.0.0.0", p_max_size);
190 #endif /* __linux__ */
193 void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
196 buf = scan_graph(obj, arg);
198 // default to DEFAULTNETDEV
200 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
204 obj->data.opaque = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
207 void print_downspeedgraph(struct text_object *obj, char *p)
209 struct net_stat *ns = obj->data.opaque;
214 new_graph(obj, p, ns->recv_speed / 1024.0);
217 void print_upspeedgraph(struct text_object *obj, char *p)
219 struct net_stat *ns = obj->data.opaque;
224 new_graph(obj, p, ns->trans_speed / 1024.0);
230 void print_wireless_essid(struct text_object *obj, char *p, int p_max_size)
232 struct net_stat *ns = obj->data.opaque;
237 snprintf(p, p_max_size, "%s", ns->essid);
239 void print_wireless_mode(struct text_object *obj, char *p, int p_max_size)
241 struct net_stat *ns = obj->data.opaque;
246 snprintf(p, p_max_size, "%s", ns->mode);
248 void print_wireless_bitrate(struct text_object *obj, char *p, int p_max_size)
250 struct net_stat *ns = obj->data.opaque;
255 snprintf(p, p_max_size, "%s", ns->bitrate);
257 void print_wireless_ap(struct text_object *obj, char *p, int p_max_size)
259 struct net_stat *ns = obj->data.opaque;
264 snprintf(p, p_max_size, "%s", ns->ap);
266 void print_wireless_link_qual(struct text_object *obj, char *p, int p_max_size)
268 struct net_stat *ns = obj->data.opaque;
273 spaced_print(p, p_max_size, "%d", 4, ns->link_qual);
275 void print_wireless_link_qual_max(struct text_object *obj, char *p, int p_max_size)
277 struct net_stat *ns = obj->data.opaque;
282 spaced_print(p, p_max_size, "%d", 4, ns->link_qual_max);
284 void print_wireless_link_qual_perc(struct text_object *obj, char *p, int p_max_size)
286 struct net_stat *ns = obj->data.opaque;
291 if (ns->link_qual_max > 0) {
292 spaced_print(p, p_max_size, "%.0f", 5,
293 (double) ns->link_qual /
294 ns->link_qual_max * 100);
296 spaced_print(p, p_max_size, "unk", 5);
299 void print_wireless_link_bar(struct text_object *obj, char *p, int p_max_size)
301 struct net_stat *ns = obj->data.opaque;
307 if(output_methods & TO_X) {
308 new_bar(obj, p, ((double) ns->link_qual /
309 ns->link_qual_max) * 255.0);
312 new_bar_in_shell(obj, p, p_max_size, ((double) ns->link_qual /
313 ns->link_qual_max) * 100.0);
315 #endif /* HAVE_IWLIB */
316 #endif /* __linux__ */
318 void clear_net_stats(void)
321 for (i = 0; i < 16; i++) {
322 if (netstats[i].dev) {
323 free(netstats[i].dev);
326 memset(netstats, 0, sizeof(netstats));
329 void parse_if_up_arg(struct text_object *obj, const char *arg)
331 obj->data.opaque = strndup(arg, text_buffer_size);
334 void free_if_up(struct text_object *obj)
336 if (obj->data.opaque) {
337 free(obj->data.opaque);
338 obj->data.opaque = NULL;
342 /* We should check if this is ok with OpenBSD and NetBSD as well. */
343 int interface_up(struct text_object *obj)
347 char *dev = obj->data.opaque;
352 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
353 CRIT_ERR(NULL, NULL, "could not create sockfd");
356 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
357 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
358 /* if device does not exist, treat like not up */
359 if (errno != ENODEV && errno != ENXIO)
360 perror("SIOCGIFFLAGS");
364 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
366 if (ifup_strictness == IFUP_UP)
369 if (!(ifr.ifr_flags & IFF_RUNNING))
371 if (ifup_strictness == IFUP_LINK)
374 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
375 perror("SIOCGIFADDR");
378 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
397 void free_dns_data(void)
400 for (i = 0; i < dns_data.nscount; i++)
401 free(dns_data.ns_list[i]);
402 if (dns_data.ns_list)
403 free(dns_data.ns_list);
404 memset(&dns_data, 0, sizeof(dns_data));
407 void update_dns_data(void)
411 //static double last_dns_update = 0.0;
413 /* maybe updating too often causes higher load because of /etc lying on a real FS
414 if (current_update_time - last_dns_update < 10.0)
417 last_dns_update = current_update_time;
422 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
425 if (fgets(line, 255, fp) == NULL) {
428 if (!strncmp(line, "nameserver ", 11)) {
429 line[strlen(line) - 1] = '\0'; // remove trailing newline
431 dns_data.ns_list = realloc(dns_data.ns_list, dns_data.nscount * sizeof(char *));
432 dns_data.ns_list[dns_data.nscount - 1] = strndup(line + 11, text_buffer_size);
438 void parse_nameserver_arg(struct text_object *obj, const char *arg)
440 obj->data.l = arg ? atoi(arg) : 0;
443 void print_nameserver(struct text_object *obj, char *p, int p_max_size)
445 if (dns_data.nscount > obj->data.l)
446 snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);