2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-10-17 14:43:12 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2018-05-12 16:03:00 +00:00
|
|
|
* Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al.
|
2009-10-17 14:43:12 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2009-11-07 11:21:48 +00:00
|
|
|
#include <unistd.h>
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2018-08-13 18:40:30 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "conky.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "net/if.h"
|
2018-05-13 17:50:46 +00:00
|
|
|
#include "net_stat.h"
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "specials.h"
|
|
|
|
#include "text_object.h"
|
2018-01-19 14:00:42 +00:00
|
|
|
#if defined(__sun)
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
2018-01-19 16:36:40 +00:00
|
|
|
#if defined(__HAIKU__)
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#define IFF_RUNNING IFF_LINK
|
|
|
|
#endif
|
2018-05-12 23:26:31 +00:00
|
|
|
#ifndef SOCK_CLOEXEC
|
2018-05-13 17:50:46 +00:00
|
|
|
#define SOCK_CLOEXEC O_CLOEXEC
|
2018-05-12 23:26:31 +00:00
|
|
|
#endif /* SOCK_CLOEXEC */
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-08-06 21:48:07 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include "linux.h"
|
|
|
|
#else
|
2018-08-13 18:40:30 +00:00
|
|
|
static char e_iface[50] = "empty";
|
|
|
|
static char interfaces_arr[64][64] = {""};
|
2018-08-06 21:48:07 +00:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2009-10-17 14:43:12 +00:00
|
|
|
/* network interface stuff */
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
enum if_up_strictness_ { IFUP_UP, IFUP_LINK, IFUP_ADDR };
|
2010-08-25 17:40:24 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
template <>
|
|
|
|
conky::lua_traits<if_up_strictness_>::Map
|
|
|
|
conky::lua_traits<if_up_strictness_>::map = {
|
|
|
|
{"up", IFUP_UP}, {"link", IFUP_LINK}, {"address", IFUP_ADDR}};
|
2010-08-25 17:40:24 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static conky::simple_config_setting<if_up_strictness_> if_up_strictness(
|
|
|
|
"if_up_strictness", IFUP_UP, true);
|
2015-12-10 20:13:29 +00:00
|
|
|
/**
|
|
|
|
* global array of structs containing network statistics for each interface
|
|
|
|
**/
|
2009-11-07 22:46:46 +00:00
|
|
|
struct net_stat netstats[MAX_NET_INTERFACES];
|
2018-04-26 11:27:21 +00:00
|
|
|
struct net_stat foo_netstats;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2015-12-10 20:13:29 +00:00
|
|
|
/**
|
|
|
|
* Returns pointer to specified interface in netstats array.
|
|
|
|
* If not found then add the specified interface to the array.
|
|
|
|
* The added interface will have all its members initialized to 0,
|
|
|
|
* because clear_net_stats() is called from main() in conky.cc!
|
|
|
|
*
|
2018-05-12 23:26:31 +00:00
|
|
|
* @param[in] dev device / interface name. Silently ignores char * == nullptr
|
2015-12-10 20:13:29 +00:00
|
|
|
**/
|
2018-05-12 23:26:31 +00:00
|
|
|
struct net_stat *get_net_stat(const char *dev, void * /*free_at_crash1*/,
|
|
|
|
void * /*free_at_crash2*/) {
|
2018-05-12 16:03:00 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (dev == nullptr) { return nullptr; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
/* find interface stat */
|
|
|
|
for (i = 0; i < MAX_NET_INTERFACES; i++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((netstats[i].dev != nullptr) && strcmp(netstats[i].dev, dev) == 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
return &netstats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wasn't found? add it */
|
|
|
|
for (i = 0; i < MAX_NET_INTERFACES; i++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (netstats[i].dev == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
netstats[i].dev = strndup(dev, text_buffer_size.get(*state));
|
|
|
|
/* initialize last_read_recv and last_read_trans to -1 denoting
|
|
|
|
* that they were never read before */
|
|
|
|
netstats[i].last_read_recv = -1;
|
|
|
|
netstats[i].last_read_trans = -1;
|
|
|
|
return &netstats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_net_stats(&foo_netstats);
|
|
|
|
foo_netstats.dev = strndup(dev, text_buffer_size.get(*state));
|
|
|
|
/* initialize last_read_recv and last_read_trans to -1 denoting
|
|
|
|
* that they were never read before */
|
|
|
|
foo_netstats.last_read_recv = -1;
|
|
|
|
foo_netstats.last_read_trans = -1;
|
|
|
|
return &foo_netstats;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_net_stat_arg(struct text_object *obj, const char *arg,
|
|
|
|
void *free_at_crash) {
|
|
|
|
bool shownetmask = false;
|
|
|
|
bool showscope = false;
|
|
|
|
char nextarg[21]; // longest arg possible is a devname (max 20 chars)
|
|
|
|
int i = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
struct net_stat *netstat = nullptr;
|
2018-08-13 18:40:30 +00:00
|
|
|
long int x = 0;
|
|
|
|
unsigned int found = 0;
|
|
|
|
char *arg_ptr = (char *)arg;
|
|
|
|
char buf[64];
|
|
|
|
char *buf_ptr = buf;
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (arg == nullptr) { arg = DEFAULTNETDEV; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-08-06 21:48:07 +00:00
|
|
|
if (0 == (strcmp("$gw_iface", arg)) ||
|
|
|
|
0 == (strcmp("${gw_iface}", arg))) {
|
|
|
|
arg = e_iface;
|
|
|
|
}
|
|
|
|
|
2018-08-13 18:40:30 +00:00
|
|
|
if (0 == strncmp(arg, "${iface", 7)) {
|
|
|
|
if (nullptr != arg_ptr) {
|
|
|
|
for (; *arg_ptr; arg_ptr++) {
|
|
|
|
if (isdigit((unsigned char)*arg_ptr)) {
|
|
|
|
*buf_ptr++ = *arg_ptr;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (1U == found) {
|
|
|
|
*buf_ptr = '\0';
|
|
|
|
x = strtol(buf, (char **)NULL, 10);
|
|
|
|
if (63L > x) {
|
|
|
|
arg = interfaces_arr[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
while (sscanf(arg + i, " %20s", nextarg) == 1) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (strcmp(nextarg, "-n") == 0 || strcmp(nextarg, "--netmask") == 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
shownetmask = true;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else if (strcmp(nextarg, "-s") == 0 || strcmp(nextarg, "--scope") == 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
showscope = true;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else if (nextarg[0] == '-') { // multiple flags in 1 arg
|
2018-05-12 16:03:00 +00:00
|
|
|
for (int j = 1; nextarg[j] != 0; j++) {
|
2018-05-13 17:50:46 +00:00
|
|
|
if (nextarg[j] == 'n') { shownetmask = true; }
|
|
|
|
if (nextarg[j] == 's') { showscope = true; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
netstat = get_net_stat(nextarg, obj, free_at_crash);
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
i += strlen(nextarg); // skip this arg
|
2018-08-02 15:15:16 +00:00
|
|
|
while (!((isspace((unsigned char)arg[i]) != 0) || arg[i] == 0)) {
|
2018-05-12 16:03:00 +00:00
|
|
|
i++; // and skip the spaces in front of it
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
if (netstat == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
netstat = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2011-02-10 22:27:14 +00:00
|
|
|
|
2011-02-10 23:27:12 +00:00
|
|
|
#ifdef BUILD_IPV6
|
2018-05-12 16:03:00 +00:00
|
|
|
netstat->v6show_nm = shownetmask;
|
|
|
|
netstat->v6show_sc = showscope;
|
2011-02-10 23:27:12 +00:00
|
|
|
#endif /* BUILD_IPV6 */
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = netstat;
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_net_stat_bar_arg(struct text_object *obj, const char *arg,
|
|
|
|
void *free_at_crash) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (arg != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
arg = scan_bar(obj, arg, 1);
|
|
|
|
obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
|
|
|
|
} else {
|
|
|
|
// default to DEFAULTNETDEV
|
|
|
|
char *buf = strndup(DEFAULTNETDEV, text_buffer_size.get(*state));
|
|
|
|
obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
|
|
|
|
free(buf);
|
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_downspeed(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
human_readable(ns->recv_speed, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_downspeedf(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_upspeed(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
human_readable(ns->trans_speed, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_upspeedf(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_totaldown(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
human_readable(ns->recv, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_totalup(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
human_readable(ns->trans, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_addr(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (ns == nullptr) { return; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if ((ns->addr.sa_data[2] & 255) == 0 && (ns->addr.sa_data[3] & 255) == 0 &&
|
|
|
|
(ns->addr.sa_data[4] & 255) == 0 && (ns->addr.sa_data[5] & 255) == 0) {
|
2018-08-02 15:15:16 +00:00
|
|
|
snprintf(p, p_max_size, "%s", "No Address");
|
2018-05-12 16:03:00 +00:00
|
|
|
} else {
|
|
|
|
snprintf(p, p_max_size, "%u.%u.%u.%u", ns->addr.sa_data[2] & 255,
|
|
|
|
ns->addr.sa_data[3] & 255, ns->addr.sa_data[4] & 255,
|
|
|
|
ns->addr.sa_data[5] & 255);
|
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_addrs(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-13 16:25:21 +00:00
|
|
|
if (0 != ns->addrs[0] && strlen(ns->addrs) > 2) {
|
2018-05-12 16:03:00 +00:00
|
|
|
ns->addrs[strlen(ns->addrs) - 2] = 0; /* remove ", " from end of string */
|
|
|
|
strncpy(p, ns->addrs, p_max_size);
|
|
|
|
} else {
|
|
|
|
strncpy(p, "0.0.0.0", p_max_size);
|
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2011-02-09 17:49:52 +00:00
|
|
|
|
|
|
|
#ifdef BUILD_IPV6
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_v6addrs(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
|
|
|
char tempaddress[INET6_ADDRSTRLEN];
|
|
|
|
struct v6addr *current_v6 = ns->v6addrs;
|
|
|
|
|
|
|
|
if (p_max_size == 0) return;
|
|
|
|
if (!ns->v6addrs) {
|
2018-08-02 15:15:16 +00:00
|
|
|
snprintf(p, p_max_size, "%s", "No Address");
|
2018-05-12 16:03:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
while (current_v6) {
|
|
|
|
inet_ntop(AF_INET6, &(current_v6->addr), tempaddress, INET6_ADDRSTRLEN);
|
|
|
|
strncat(p, tempaddress, p_max_size);
|
|
|
|
// netmask
|
|
|
|
if (ns->v6show_nm) {
|
|
|
|
char netmaskstr[5]; // max 5 chars (/128 + null-terminator)
|
|
|
|
sprintf(netmaskstr, "/%u", current_v6->netmask);
|
|
|
|
strncat(p, netmaskstr, p_max_size);
|
|
|
|
}
|
|
|
|
// scope
|
|
|
|
if (ns->v6show_sc) {
|
2018-08-07 13:38:36 +00:00
|
|
|
char scopestr[4];
|
2018-05-12 16:03:00 +00:00
|
|
|
sprintf(scopestr, "(%c)", current_v6->scope);
|
|
|
|
strncat(p, scopestr, p_max_size);
|
|
|
|
}
|
|
|
|
// next (or last) address
|
|
|
|
current_v6 = current_v6->next;
|
|
|
|
if (current_v6) strncat(p, ", ", p_max_size);
|
|
|
|
}
|
2011-02-09 17:49:52 +00:00
|
|
|
}
|
|
|
|
#endif /* BUILD_IPV6 */
|
|
|
|
|
2009-10-17 14:43:12 +00:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2015-12-11 00:39:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called periodically to update the download and upload graphs
|
|
|
|
*
|
|
|
|
* - evaluates argument strings like 'eth0 50,120 #FFFFFF #FF0000 0 -l'
|
|
|
|
* - sets the obj->data.opaque pointer to the interface specified
|
|
|
|
*
|
|
|
|
* @param[out] obj struct which will hold evaluated arguments
|
|
|
|
* @param[in] arg argument string to evaluate
|
|
|
|
**/
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_net_stat_graph_arg(struct text_object *obj, const char *arg,
|
|
|
|
void *free_at_crash) {
|
|
|
|
/* scan arguments and get interface name back */
|
2018-05-12 23:26:31 +00:00
|
|
|
char *buf = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
buf = scan_graph(obj, arg, 0);
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
// default to DEFAULTNETDEV
|
2018-05-12 23:26:31 +00:00
|
|
|
if (buf != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
obj->data.opaque = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:39:59 +00:00
|
|
|
/**
|
|
|
|
* returns the download speed in kiB/s for the interface referenced by obj
|
|
|
|
*
|
|
|
|
* @param[in] obj struct containting a member data, which is a struct
|
|
|
|
* containing a void * to a net_stat struct
|
|
|
|
**/
|
2018-05-12 16:03:00 +00:00
|
|
|
double downspeedgraphval(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
return (ns != nullptr ? (ns->recv_speed / 1024.0) : 0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
double upspeedgraphval(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ns = static_cast<struct net_stat *>(obj->data.opaque);
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
return (ns != nullptr ? (ns->trans_speed / 1024.0) : 0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_X11 */
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2010-01-11 00:13:42 +00:00
|
|
|
#ifdef BUILD_WLAN
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_essid(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) {
|
|
|
|
for (unsigned int i = 0; *(netstats[i].dev) != 0; i++) {
|
|
|
|
if (*(netstats[i].essid) != 0) {
|
|
|
|
snprintf(p, p_max_size, "%s", netstats[i].essid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->essid);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_mode(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->mode);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_channel(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2010-06-07 07:54:25 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2010-06-07 07:54:25 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (ns->channel != 0) {
|
|
|
|
snprintf(p, p_max_size, "%i", ns->channel);
|
|
|
|
} else {
|
2018-08-02 15:15:16 +00:00
|
|
|
snprintf(p, p_max_size, "%s", "/");
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2010-06-07 07:54:25 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_frequency(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2010-06-07 08:50:02 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2010-06-07 08:50:02 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (ns->freq[0] != 0) {
|
|
|
|
snprintf(p, p_max_size, "%s", ns->freq);
|
|
|
|
} else {
|
|
|
|
snprintf(p, p_max_size, "/");
|
|
|
|
}
|
2010-06-07 08:50:02 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_bitrate(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->bitrate);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_ap(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->ap);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_link_qual(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
spaced_print(p, p_max_size, "%d", 4, ns->link_qual);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_link_qual_max(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
spaced_print(p, p_max_size, "%d", 4, ns->link_qual_max);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_wireless_link_qual_perc(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (ns->link_qual_max > 0) {
|
|
|
|
spaced_print(p, p_max_size, "%.0f", 5,
|
|
|
|
(double)ns->link_qual / ns->link_qual_max * 100);
|
|
|
|
} else {
|
|
|
|
spaced_print(p, p_max_size, "unk", 5);
|
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
double wireless_link_barval(struct text_object *obj) {
|
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (!ns) return 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return (double)ns->link_qual / ns->link_qual_max;
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
2010-01-11 00:13:42 +00:00
|
|
|
#endif /* BUILD_WLAN */
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2015-12-10 20:13:29 +00:00
|
|
|
/**
|
|
|
|
* Clears the global array of net_stat structs which contains networks
|
|
|
|
* statistics for every interface.
|
|
|
|
**/
|
2018-05-12 23:26:31 +00:00
|
|
|
void clear_net_stats() {
|
2011-02-09 17:49:52 +00:00
|
|
|
#ifdef BUILD_IPV6
|
2018-05-12 16:03:00 +00:00
|
|
|
struct v6addr *nextv6;
|
2011-02-09 17:49:52 +00:00
|
|
|
#endif /* BUILD_IPV6 */
|
2018-05-12 16:03:00 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_NET_INTERFACES; i++) {
|
|
|
|
free_and_zero(netstats[i].dev);
|
2011-02-09 17:49:52 +00:00
|
|
|
#ifdef BUILD_IPV6
|
2018-05-12 16:03:00 +00:00
|
|
|
while (netstats[i].v6addrs) {
|
|
|
|
nextv6 = netstats[i].v6addrs;
|
|
|
|
netstats[i].v6addrs = netstats[i].v6addrs->next;
|
|
|
|
free_and_zero(nextv6);
|
|
|
|
}
|
2011-02-09 17:49:52 +00:00
|
|
|
#endif /* BUILD_IPV6 */
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
memset(netstats, 0, sizeof(netstats));
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 11:27:21 +00:00
|
|
|
void clear_net_stats(net_stat *in) {
|
|
|
|
#ifdef BUILD_IPV6
|
2018-05-12 16:03:00 +00:00
|
|
|
struct v6addr *nextv6;
|
2018-04-26 11:27:21 +00:00
|
|
|
#endif /* BUILD_IPV6 */
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(in->dev);
|
2018-04-26 11:27:21 +00:00
|
|
|
#ifdef BUILD_IPV6
|
2018-05-12 16:03:00 +00:00
|
|
|
while (in->v6addrs) {
|
|
|
|
nextv6 = in->v6addrs;
|
|
|
|
in->v6addrs = in->v6addrs->next;
|
|
|
|
free_and_zero(nextv6);
|
|
|
|
}
|
2018-04-26 11:27:21 +00:00
|
|
|
#endif /* BUILD_IPV6 */
|
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_if_up_arg(struct text_object *obj, const char *arg) {
|
|
|
|
obj->data.opaque = strndup(arg, text_buffer_size.get(*state));
|
2009-10-09 02:30:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_if_up(struct text_object *obj) { free_and_zero(obj->data.opaque); }
|
2009-10-09 02:30:02 +00:00
|
|
|
|
2009-10-17 14:43:12 +00:00
|
|
|
/* We should check if this is ok with OpenBSD and NetBSD as well. */
|
2018-05-12 16:03:00 +00:00
|
|
|
int interface_up(struct text_object *obj) {
|
|
|
|
int fd;
|
2018-05-12 23:26:31 +00:00
|
|
|
struct ifreq ifr {};
|
|
|
|
auto *dev = static_cast<char *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if (dev == nullptr) { return 0; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-08-07 15:54:01 +00:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
#else
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) {
|
2018-08-07 15:54:01 +00:00
|
|
|
#endif
|
2018-05-12 23:26:31 +00:00
|
|
|
CRIT_ERR(nullptr, nullptr, "could not create sockfd");
|
2018-05-12 16:03:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
/* if device does not exist, treat like not up */
|
2018-05-13 17:50:46 +00:00
|
|
|
if (errno != ENODEV && errno != ENXIO) { perror("SIOCGIFFLAGS"); }
|
2018-05-12 16:03:00 +00:00
|
|
|
goto END_FALSE;
|
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((ifr.ifr_flags & IFF_UP) == 0) { /* iface is not up */
|
2018-05-12 16:03:00 +00:00
|
|
|
goto END_FALSE;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-13 17:50:46 +00:00
|
|
|
if (if_up_strictness.get(*state) == IFUP_UP) { goto END_TRUE; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-01-19 16:36:40 +00:00
|
|
|
#ifdef IFF_RUNNING
|
2018-05-13 17:50:46 +00:00
|
|
|
if ((ifr.ifr_flags & IFF_RUNNING) == 0) { goto END_FALSE; }
|
2018-01-19 16:36:40 +00:00
|
|
|
#endif
|
2018-05-13 17:50:46 +00:00
|
|
|
if (if_up_strictness.get(*state) == IFUP_LINK) { goto END_TRUE; }
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (ioctl(fd, SIOCGIFADDR, &ifr) != 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
perror("SIOCGIFADDR");
|
|
|
|
goto END_FALSE;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((reinterpret_cast<struct sockaddr_in *>(&(ifr.ifr_addr)))
|
|
|
|
->sin_addr.s_addr != 0u) {
|
|
|
|
goto END_TRUE;
|
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
|
|
|
|
END_FALSE:
|
2018-05-12 16:03:00 +00:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
END_TRUE:
|
2018-05-12 16:03:00 +00:00
|
|
|
close(fd);
|
|
|
|
return 1;
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
struct _dns_data {
|
2018-05-12 23:26:31 +00:00
|
|
|
_dns_data() = default;
|
|
|
|
int nscount{0};
|
|
|
|
char **ns_list{nullptr};
|
2009-10-17 14:43:12 +00:00
|
|
|
};
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
static _dns_data dns_data;
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_dns_data(struct text_object *obj) {
|
|
|
|
int i;
|
2009-11-29 19:55:42 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
(void)obj;
|
2009-11-29 19:55:42 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
for (i = 0; i < dns_data.nscount; i++) { free(dns_data.ns_list[i]); }
|
2018-05-12 16:03:00 +00:00
|
|
|
free(dns_data.ns_list);
|
|
|
|
memset(&dns_data, 0, sizeof(dns_data));
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
int update_dns_data() {
|
2018-05-12 16:03:00 +00:00
|
|
|
FILE *fp;
|
|
|
|
char line[256];
|
|
|
|
// static double last_dns_update = 0.0;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
/* maybe updating too often causes higher load because of /etc lying on a real
|
|
|
|
FS if (current_update_time - last_dns_update < 10.0) return 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
last_dns_update = current_update_time;
|
|
|
|
*/
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
free_dns_data(nullptr);
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2018-05-13 17:50:46 +00:00
|
|
|
if ((fp = fopen("/etc/resolv.conf", "re")) == nullptr) { return 0; }
|
2018-05-12 23:26:31 +00:00
|
|
|
while (feof(fp) == 0) {
|
2018-05-13 17:50:46 +00:00
|
|
|
if (fgets(line, 255, fp) == nullptr) { break; }
|
2018-05-12 23:26:31 +00:00
|
|
|
if (strncmp(line, "nameserver ", 11) == 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
line[strlen(line) - 1] = '\0'; // remove trailing newline
|
|
|
|
dns_data.nscount++;
|
2018-05-12 23:26:31 +00:00
|
|
|
dns_data.ns_list = static_cast<char **>(
|
|
|
|
realloc(dns_data.ns_list, dns_data.nscount * sizeof(char *)));
|
2018-05-12 16:03:00 +00:00
|
|
|
dns_data.ns_list[dns_data.nscount - 1] =
|
|
|
|
strndup(line + 11, text_buffer_size.get(*state));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_nameserver_arg(struct text_object *obj, const char *arg) {
|
2018-05-12 23:26:31 +00:00
|
|
|
obj->data.l = arg != nullptr ? atoi(arg) : 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_nameserver(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (dns_data.nscount > obj->data.l) {
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|