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/>.
35 #include "text_object.h"
39 #include <sys/ioctl.h>
41 /* network interface stuff */
43 struct net_stat netstats[16];
45 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
53 /* find interface stat */
54 for (i = 0; i < 16; i++) {
55 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
60 /* wasn't found? add it */
61 for (i = 0; i < 16; i++) {
62 if (netstats[i].dev == 0) {
63 netstats[i].dev = strndup(dev, text_buffer_size);
68 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
72 void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash)
77 obj->data.net = get_net_stat(arg, obj, free_at_crash);
80 void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
84 arg = scan_bar(arg, &obj->a, &obj->b);
85 obj->data.net = get_net_stat(arg, obj, free_at_crash);
87 // default to DEFAULTNETDEV
88 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
89 obj->data.net = get_net_stat(buf, obj, free_at_crash);
94 void print_downspeed(struct text_object *obj, char *p, int p_max_size)
99 human_readable(obj->data.net.recv_speed, p, p_max_size);
102 void print_downspeedf(struct text_object *obj, char *p, int p_max_size)
107 spaced_print(p, p_max_size, "%.1f", 8, obj->data.netrecv_speed / 1024.0);
110 void print_upspeed(struct text_object *obj, char *p, int p_max_size)
115 human_readable(obj->data.net.trans_speed, p, p_max_size);
118 void print_upspeedf(struct text_object *obj, char *p, int p_max_size)
123 spaced_print(p, p_max_size, "%.1f", 8, obj->data.net.trans_speed / 1024.0);
126 void print_totaldown(struct text_object *obj, char *p, int p_max_size)
131 human_readable(obj->data.net.recv, p, p_max_size);
134 void print_totalup(struct text_object *obj, char *p, int p_max_size)
139 human_readable(obj->data.net.trans, p, p_max_size);
142 void print_addr(struct text_object *obj, char *p, int p_max_size)
147 if ((obj->data.net.addr.sa_data[2] & 255) == 0 &&
148 (obj->data.net.addr.sa_data[3] & 255) == 0 &&
149 (obj->data.net.addr.sa_data[4] & 255) == 0 &&
150 (obj->data.net.addr.sa_data[5] & 255) == 0) {
151 snprintf(p, p_max_size, "No Address");
153 snprintf(p, p_max_size, "%u.%u.%u.%u",
154 obj->data.net.addr.sa_data[2] & 255,
155 obj->data.net.addr.sa_data[3] & 255,
156 obj->data.net.addr.sa_data[4] & 255,
157 obj->data.net.addr.sa_data[5] & 255);
162 void print_addrs(struct text_object *obj, char *p, int p_max_size)
167 if (NULL != obj->data.net.addrs && strlen(obj->data.net.addrs) > 2) {
168 obj->data.net.addrs[strlen(obj->data.net.addrs) - 2] = 0; /* remove ", " from end of string */
169 strncpy(p, obj->data.net.addrs, p_max_size);
171 strncpy(p, "0.0.0.0", p_max_size);
174 #endif /* __linux__ */
177 void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
180 SIZE_DEFAULTS(graph);
181 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
182 &obj->e, &obj->char_a, &obj->char_b);
184 // default to DEFAULTNETDEV
186 obj->data.net = get_net_stat(buf, obj, free_at_crash);
190 obj->data.net = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
193 void print_downspeedgraph(struct text_object *obj, char *p)
198 new_graph(p, obj->a, obj->b, obj->c, obj->d,
199 obj->data.net.recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
202 void print_upspeedgraph(struct text_object *obj, char *p)
207 new_graph(p, obj->a, obj->b, obj->c, obj->d,
208 obj->data.net.trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
214 void print_wireless_essid(struct text_object *obj, char *p, int p_max_size)
219 snprintf(p, p_max_size, "%s", obj->data.net.essid);
221 void print_wireless_mode(struct text_object *obj, char *p, int p_max_size)
226 snprintf(p, p_max_size, "%s", obj->data.net.mode);
228 void print_wireless_bitrate(struct text_object *obj, char *p, int p_max_size)
233 snprintf(p, p_max_size, "%s", obj->data.net.bitrate);
235 void print_wireless_ap(struct text_object *obj, char *p, int p_max_size)
240 snprintf(p, p_max_size, "%s", obj->data.net.ap);
242 void print_wireless_link_qual(struct text_object *obj, char *p, int p_max_size)
247 spaced_print(p, p_max_size, "%d", 4, obj->data.net.link_qual);
249 void print_wireless_link_qual_max(struct text_object *obj, char *p, int p_max_size)
254 spaced_print(p, p_max_size, "%d", 4, obj->data.net.link_qual_max);
256 void print_wireless_link_qual_perc(struct text_object *obj, char *p, int p_max_size)
261 if (obj->data.net.link_qual_max > 0) {
262 spaced_print(p, p_max_size, "%.0f", 5,
263 (double) obj->data.net.link_qual /
264 obj->data.net.link_qual_max * 100);
266 spaced_print(p, p_max_size, "unk", 5);
269 void print_wireless_link_bar(struct text_object *obj, char *p, int p_max_size)
275 if(output_methods & TO_X) {
276 new_bar(p, obj->a, obj->b, ((double) obj->data.net.link_qual /
277 obj->data.net.link_qual_max) * 255.0);
281 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
282 new_bar_in_shell(p, p_max_size, ((double) obj->data.net.link_qual /
283 obj->data.net.link_qual_max) * 100.0, obj->a);
286 #endif /* HAVE_IWLIB */
287 #endif /* __linux__ */
289 void clear_net_stats(void)
292 for (i = 0; i < 16; i++) {
293 if (netstats[i].dev) {
294 free(netstats[i].dev);
297 memset(netstats, 0, sizeof(netstats));
300 /* We should check if this is ok with OpenBSD and NetBSD as well. */
301 int interface_up(const char *dev)
306 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
307 CRIT_ERR(NULL, NULL, "could not create sockfd");
310 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
311 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
312 /* if device does not exist, treat like not up */
313 if (errno != ENODEV && errno != ENXIO)
314 perror("SIOCGIFFLAGS");
318 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
320 if (ifup_strictness == IFUP_UP)
323 if (!(ifr.ifr_flags & IFF_RUNNING))
325 if (ifup_strictness == IFUP_LINK)
328 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
329 perror("SIOCGIFADDR");
332 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
351 void free_dns_data(void)
354 for (i = 0; i < dns_data.nscount; i++)
355 free(dns_data.ns_list[i]);
356 if (dns_data.ns_list)
357 free(dns_data.ns_list);
358 memset(&dns_data, 0, sizeof(dns_data));
361 void update_dns_data(void)
365 //static double last_dns_update = 0.0;
367 /* maybe updating too often causes higher load because of /etc lying on a real FS
368 if (current_update_time - last_dns_update < 10.0)
371 last_dns_update = current_update_time;
376 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
379 if (fgets(line, 255, fp) == NULL) {
382 if (!strncmp(line, "nameserver ", 11)) {
383 line[strlen(line) - 1] = '\0'; // remove trailing newline
385 dns_data.ns_list = realloc(dns_data.ns_list, dns_data.nscount * sizeof(char *));
386 dns_data.ns_list[dns_data.nscount - 1] = strndup(line + 11, text_buffer_size);
392 void parse_nameserver_arg(struct text_object *obj, const char *arg)
394 obj->data.l = arg ? atoi(arg) : 0;
397 void print_nameserver(struct text_object *obj, char *p, int p_max_size)
399 if (dns_data.nscount > obj->data.l)
400 snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);