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>
43 /* network interface stuff */
45 struct net_stat netstats[16];
47 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
55 /* find interface stat */
56 for (i = 0; i < 16; i++) {
57 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
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);
70 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
74 void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash)
79 obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
82 void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
85 arg = scan_bar(obj, arg);
86 obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
88 // default to DEFAULTNETDEV
89 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
90 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
95 void print_downspeed(struct text_object *obj, char *p, int p_max_size)
97 struct net_stat *ns = obj->data.opaque;
102 human_readable(ns->recv_speed, p, p_max_size);
105 void print_downspeedf(struct text_object *obj, char *p, int p_max_size)
107 struct net_stat *ns = obj->data.opaque;
112 spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0);
115 void print_upspeed(struct text_object *obj, char *p, int p_max_size)
117 struct net_stat *ns = obj->data.opaque;
122 human_readable(ns->trans_speed, p, p_max_size);
125 void print_upspeedf(struct text_object *obj, char *p, int p_max_size)
127 struct net_stat *ns = obj->data.opaque;
132 spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0);
135 void print_totaldown(struct text_object *obj, char *p, int p_max_size)
137 struct net_stat *ns = obj->data.opaque;
142 human_readable(ns->recv, p, p_max_size);
145 void print_totalup(struct text_object *obj, char *p, int p_max_size)
147 struct net_stat *ns = obj->data.opaque;
152 human_readable(ns->trans, p, p_max_size);
155 void print_addr(struct text_object *obj, char *p, int p_max_size)
157 struct net_stat *ns = obj->data.opaque;
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");
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);
177 void print_addrs(struct text_object *obj, char *p, int p_max_size)
179 struct net_stat *ns = obj->data.opaque;
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);
188 strncpy(p, "0.0.0.0", p_max_size);
191 #endif /* __linux__ */
194 void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
197 buf = scan_graph(obj, arg, 0);
199 // default to DEFAULTNETDEV
201 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
205 obj->data.opaque = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
208 void print_downspeedgraph(struct text_object *obj, char *p)
210 struct net_stat *ns = obj->data.opaque;
215 new_graph(obj, p, ns->recv_speed / 1024.0);
218 void print_upspeedgraph(struct text_object *obj, char *p)
220 struct net_stat *ns = obj->data.opaque;
225 new_graph(obj, p, ns->trans_speed / 1024.0);
231 void print_wireless_essid(struct text_object *obj, char *p, int p_max_size)
233 struct net_stat *ns = obj->data.opaque;
238 snprintf(p, p_max_size, "%s", ns->essid);
240 void print_wireless_mode(struct text_object *obj, char *p, int p_max_size)
242 struct net_stat *ns = obj->data.opaque;
247 snprintf(p, p_max_size, "%s", ns->mode);
249 void print_wireless_bitrate(struct text_object *obj, char *p, int p_max_size)
251 struct net_stat *ns = obj->data.opaque;
256 snprintf(p, p_max_size, "%s", ns->bitrate);
258 void print_wireless_ap(struct text_object *obj, char *p, int p_max_size)
260 struct net_stat *ns = obj->data.opaque;
265 snprintf(p, p_max_size, "%s", ns->ap);
267 void print_wireless_link_qual(struct text_object *obj, char *p, int p_max_size)
269 struct net_stat *ns = obj->data.opaque;
274 spaced_print(p, p_max_size, "%d", 4, ns->link_qual);
276 void print_wireless_link_qual_max(struct text_object *obj, char *p, int p_max_size)
278 struct net_stat *ns = obj->data.opaque;
283 spaced_print(p, p_max_size, "%d", 4, ns->link_qual_max);
285 void print_wireless_link_qual_perc(struct text_object *obj, char *p, int p_max_size)
287 struct net_stat *ns = obj->data.opaque;
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);
297 spaced_print(p, p_max_size, "unk", 5);
300 void print_wireless_link_bar(struct text_object *obj, char *p, int p_max_size)
302 struct net_stat *ns = obj->data.opaque;
308 if(output_methods & TO_X) {
309 new_bar(obj, p, ((double) ns->link_qual /
310 ns->link_qual_max) * 255.0);
313 new_bar_in_shell(obj, p, p_max_size, ((double) ns->link_qual /
314 ns->link_qual_max) * 100.0);
316 #endif /* HAVE_IWLIB */
317 #endif /* __linux__ */
319 void clear_net_stats(void)
322 for (i = 0; i < 16; i++) {
323 if (netstats[i].dev) {
324 free(netstats[i].dev);
327 memset(netstats, 0, sizeof(netstats));
330 void parse_if_up_arg(struct text_object *obj, const char *arg)
332 obj->data.opaque = strndup(arg, text_buffer_size);
335 void free_if_up(struct text_object *obj)
337 if (obj->data.opaque) {
338 free(obj->data.opaque);
339 obj->data.opaque = NULL;
343 /* We should check if this is ok with OpenBSD and NetBSD as well. */
344 int interface_up(struct text_object *obj)
348 char *dev = obj->data.opaque;
353 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
354 CRIT_ERR(NULL, NULL, "could not create sockfd");
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");
365 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
367 if (ifup_strictness == IFUP_UP)
370 if (!(ifr.ifr_flags & IFF_RUNNING))
372 if (ifup_strictness == IFUP_LINK)
375 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
376 perror("SIOCGIFADDR");
379 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
398 void free_dns_data(void)
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));
408 void update_dns_data(void)
412 //static double last_dns_update = 0.0;
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)
418 last_dns_update = current_update_time;
423 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
426 if (fgets(line, 255, fp) == NULL) {
429 if (!strncmp(line, "nameserver ", 11)) {
430 line[strlen(line) - 1] = '\0'; // remove trailing newline
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);
439 void parse_nameserver_arg(struct text_object *obj, const char *arg)
441 obj->data.l = arg ? atoi(arg) : 0;
444 void print_nameserver(struct text_object *obj, char *p, int p_max_size)
446 if (dns_data.nscount > obj->data.l)
447 snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);