2010-01-04 17:06:14 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
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
|
2010-01-01 23:46:17 +00:00
|
|
|
* Copyright (c) 2005-2010 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 "config.h"
|
2009-10-24 23:59:07 +00:00
|
|
|
#include "conky.h"
|
2009-10-17 14:43:12 +00:00
|
|
|
#include "logging.h"
|
|
|
|
#include "specials.h"
|
|
|
|
#include "net/if.h"
|
|
|
|
#include "text_object.h"
|
|
|
|
#include "net_stat.h"
|
2009-11-14 03:19:51 +00:00
|
|
|
#include <netinet/in.h>
|
2009-10-17 14:43:12 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2009-11-07 11:21:48 +00:00
|
|
|
#include <unistd.h>
|
2009-10-17 14:43:12 +00:00
|
|
|
|
|
|
|
/* network interface stuff */
|
|
|
|
|
2009-11-07 22:46:46 +00:00
|
|
|
struct net_stat netstats[MAX_NET_INTERFACES];
|
2009-10-17 14:43:12 +00:00
|
|
|
|
|
|
|
struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!dev) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find interface stat */
|
2009-11-07 22:46:46 +00:00
|
|
|
for (i = 0; i < MAX_NET_INTERFACES; i++) {
|
2009-10-17 14:43:12 +00:00
|
|
|
if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
|
|
|
|
return &netstats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wasn't found? add it */
|
2009-11-07 22:46:46 +00:00
|
|
|
for (i = 0; i < MAX_NET_INTERFACES; i++) {
|
2009-10-17 14:43:12 +00:00
|
|
|
if (netstats[i].dev == 0) {
|
|
|
|
netstats[i].dev = strndup(dev, text_buffer_size);
|
|
|
|
return &netstats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-07 22:46:46 +00:00
|
|
|
CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is %d)", MAX_NET_INTERFACES);
|
2009-10-17 14:43:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash)
|
|
|
|
{
|
|
|
|
if (!arg)
|
|
|
|
arg = DEFAULTNETDEV;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
|
|
|
|
{
|
|
|
|
if (arg) {
|
2009-12-04 00:13:36 +00:00
|
|
|
arg = scan_bar(obj, arg, 1);
|
2009-10-09 01:16:17 +00:00
|
|
|
obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
|
2009-10-17 14:43:12 +00:00
|
|
|
} else {
|
|
|
|
// default to DEFAULTNETDEV
|
|
|
|
char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
|
2009-10-09 01:16:17 +00:00
|
|
|
obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
|
2009-10-17 14:43:12 +00:00
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_downspeed(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
human_readable(ns->recv_speed, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_downspeedf(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_upspeed(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
human_readable(ns->trans_speed, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_upspeedf(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_totaldown(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
human_readable(ns->recv, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_totalup(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
human_readable(ns->trans, p, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_addr(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +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) {
|
2009-10-17 14:43:12 +00:00
|
|
|
snprintf(p, p_max_size, "No Address");
|
|
|
|
} else {
|
|
|
|
snprintf(p, p_max_size, "%u.%u.%u.%u",
|
2009-10-09 01:16:17 +00:00
|
|
|
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__
|
|
|
|
void print_addrs(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
if (NULL != ns->addrs && strlen(ns->addrs) > 2) {
|
|
|
|
ns->addrs[strlen(ns->addrs) - 2] = 0; /* remove ", " from end of string */
|
|
|
|
strncpy(p, ns->addrs, p_max_size);
|
2009-10-17 14:43:12 +00:00
|
|
|
} else {
|
|
|
|
strncpy(p, "0.0.0.0", p_max_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2009-10-17 14:43:12 +00:00
|
|
|
void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
|
|
|
|
{
|
|
|
|
char *buf = 0;
|
2009-11-05 23:05:08 +00:00
|
|
|
buf = scan_graph(obj, arg, 0);
|
2009-10-17 14:43:12 +00:00
|
|
|
|
|
|
|
// default to DEFAULTNETDEV
|
|
|
|
if (buf) {
|
2009-10-09 01:16:17 +00:00
|
|
|
obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
|
2009-10-17 14:43:12 +00:00
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-09 01:16:17 +00:00
|
|
|
obj->data.opaque = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:53:53 +00:00
|
|
|
double downspeedgraphval(struct text_object *obj)
|
2009-10-17 14:43:12 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2009-12-04 00:53:53 +00:00
|
|
|
return (ns ? (ns->recv_speed / 1024.0) : 0);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:53:53 +00:00
|
|
|
double upspeedgraphval(struct text_object *obj)
|
2009-10-17 14:43:12 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
2009-12-04 00:53:53 +00:00
|
|
|
return (ns ? (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
|
2009-10-17 14:43:12 +00:00
|
|
|
void print_wireless_essid(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->essid);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
void print_wireless_mode(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->mode);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
void print_wireless_bitrate(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->bitrate);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
void print_wireless_ap(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
snprintf(p, p_max_size, "%s", ns->ap);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
void print_wireless_link_qual(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
spaced_print(p, p_max_size, "%d", 4, ns->link_qual);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
void print_wireless_link_qual_max(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
spaced_print(p, p_max_size, "%d", 4, ns->link_qual_max);
|
2009-10-17 14:43:12 +00:00
|
|
|
}
|
|
|
|
void print_wireless_link_qual_perc(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-10-17 14:43:12 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-09 01:16:17 +00:00
|
|
|
if (ns->link_qual_max > 0) {
|
2009-10-17 14:43:12 +00:00
|
|
|
spaced_print(p, p_max_size, "%.0f", 5,
|
2009-10-09 01:16:17 +00:00
|
|
|
(double) ns->link_qual /
|
|
|
|
ns->link_qual_max * 100);
|
2009-10-17 14:43:12 +00:00
|
|
|
} else {
|
|
|
|
spaced_print(p, p_max_size, "unk", 5);
|
|
|
|
}
|
|
|
|
}
|
2009-12-04 00:13:36 +00:00
|
|
|
double wireless_link_barval(struct text_object *obj)
|
2009-10-17 14:43:12 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct net_stat *ns = (struct net_stat *)obj->data.opaque;
|
2009-10-09 01:16:17 +00:00
|
|
|
|
|
|
|
if (!ns)
|
2009-11-22 17:15:36 +00:00
|
|
|
return 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
2009-12-04 00:13:36 +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
|
|
|
|
|
|
|
void clear_net_stats(void)
|
|
|
|
{
|
|
|
|
int i;
|
2009-11-07 22:46:46 +00:00
|
|
|
for (i = 0; i < MAX_NET_INTERFACES; i++) {
|
2009-10-17 14:43:12 +00:00
|
|
|
if (netstats[i].dev) {
|
|
|
|
free(netstats[i].dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(netstats, 0, sizeof(netstats));
|
|
|
|
}
|
|
|
|
|
2009-10-09 02:30:02 +00:00
|
|
|
void parse_if_up_arg(struct text_object *obj, const char *arg)
|
|
|
|
{
|
|
|
|
obj->data.opaque = strndup(arg, text_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_if_up(struct text_object *obj)
|
|
|
|
{
|
|
|
|
if (obj->data.opaque) {
|
|
|
|
free(obj->data.opaque);
|
|
|
|
obj->data.opaque = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 14:43:12 +00:00
|
|
|
/* We should check if this is ok with OpenBSD and NetBSD as well. */
|
2009-10-09 02:30:02 +00:00
|
|
|
int interface_up(struct text_object *obj)
|
2009-10-17 14:43:12 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct ifreq ifr;
|
2010-01-04 17:06:14 +00:00
|
|
|
char *dev = (char*)obj->data.opaque;
|
2009-10-09 02:30:02 +00:00
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return 0;
|
2009-10-17 14:43:12 +00:00
|
|
|
|
|
|
|
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
CRIT_ERR(NULL, NULL, "could not create sockfd");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
|
|
|
|
/* if device does not exist, treat like not up */
|
|
|
|
if (errno != ENODEV && errno != ENXIO)
|
|
|
|
perror("SIOCGIFFLAGS");
|
|
|
|
goto END_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
|
|
|
|
goto END_FALSE;
|
|
|
|
if (ifup_strictness == IFUP_UP)
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
if (!(ifr.ifr_flags & IFF_RUNNING))
|
|
|
|
goto END_FALSE;
|
|
|
|
if (ifup_strictness == IFUP_LINK)
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
|
|
|
|
perror("SIOCGIFADDR");
|
|
|
|
goto END_FALSE;
|
|
|
|
}
|
|
|
|
if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
END_FALSE:
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
END_TRUE:
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
struct _dns_data {
|
|
|
|
_dns_data() : nscount(0), ns_list(0) {}
|
|
|
|
int nscount;
|
|
|
|
char **ns_list;
|
2009-10-17 14:43:12 +00:00
|
|
|
};
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
static _dns_data dns_data;
|
|
|
|
|
2009-11-29 19:55:42 +00:00
|
|
|
void free_dns_data(struct text_object *obj)
|
2009-10-17 14:43:12 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-11-29 19:55:42 +00:00
|
|
|
|
|
|
|
(void)obj;
|
|
|
|
|
2009-10-17 14:43:12 +00:00
|
|
|
for (i = 0; i < dns_data.nscount; i++)
|
|
|
|
free(dns_data.ns_list[i]);
|
|
|
|
if (dns_data.ns_list)
|
|
|
|
free(dns_data.ns_list);
|
|
|
|
memset(&dns_data, 0, sizeof(dns_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_dns_data(void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[256];
|
|
|
|
//static double last_dns_update = 0.0;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
last_dns_update = current_update_time;
|
|
|
|
*/
|
|
|
|
|
2009-11-29 19:55:42 +00:00
|
|
|
free_dns_data(NULL);
|
2009-10-17 14:43:12 +00:00
|
|
|
|
|
|
|
if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
|
|
|
|
return;
|
|
|
|
while(!feof(fp)) {
|
|
|
|
if (fgets(line, 255, fp) == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!strncmp(line, "nameserver ", 11)) {
|
|
|
|
line[strlen(line) - 1] = '\0'; // remove trailing newline
|
|
|
|
dns_data.nscount++;
|
2010-01-04 17:06:14 +00:00
|
|
|
dns_data.ns_list = (char**)realloc(dns_data.ns_list, dns_data.nscount * sizeof(char *));
|
2009-10-17 14:43:12 +00:00
|
|
|
dns_data.ns_list[dns_data.nscount - 1] = strndup(line + 11, text_buffer_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_nameserver_arg(struct text_object *obj, const char *arg)
|
|
|
|
{
|
|
|
|
obj->data.l = arg ? atoi(arg) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_nameserver(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
if (dns_data.nscount > obj->data.l)
|
|
|
|
snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);
|
|
|
|
}
|