📄 net.c
字号:
/* GKrellM| Copyright (C) 1999-2006 Bill Wilson|| Author: Bill Wilson billw@gkrellm.net| Latest versions might be found at: http://gkrellm.net|| This program is free software which I release under the GNU General Public| License. You may redistribute and/or modify this program under the terms| of that license as published by the Free Software Foundation; either| version 2 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. Version 2 is in the| COPYRIGHT file in the top level directory of this distribution.| | To get a copy of the GNU General Puplic License, write to the Free Software| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "gkrellm.h"#include "gkrellm-sysdeps.h"#include "gkrellm-private.h"#define PPP_LOCK_FILE "LCK..modem"#define TIMER_TYPE_NONE 0#define TIMER_TYPE_PPP 1#define TIMER_TYPE_IPPP 2#define TIMER_TYPE_SERVER 3#define N_DAY_STATS 31#define N_WEEK_STATS 26#define N_MONTH_STATS 12typedef struct { gchar *date; gdouble rx, tx; gint connect_time; } NetStat;enum StatType { DAY_STAT, WEEK_STAT, MONTH_STAT };typedef struct { gchar *name; GtkWidget *vbox, *parent_vbox; GtkWidget *enable_button, *force_button, *alert_button, *label_entry; GkrellmChart *chart; GkrellmChartdata *rx_cd, *tx_cd; GkrellmDecal *rxled, *txled; /* All known net interfaces are in the net_mon_list. Only interfaces | which are up or forced up and are config enabled will actually have | a chart created, unless the interface is linked to the timer button. | A linked interface always has a chart created for it regardless | of the up state but it will have a chart that may not be | visible if the interface is down (ppp) or the connect state is | hangup (ippp). */ gboolean locked; /* True if linked to timer button or forced up */ GkrellmChartconfig *chart_config; gboolean enabled, chart_labels, force_up; gchar *label; GkrellmAlert *alert; GtkWidget *alert_config_rx_button, *alert_config_tx_button; gboolean alert_uses_rx, alert_uses_tx; gulong rx_old, tx_old; gint rx_current, tx_current; gdouble rx_totalA, rx_totalB, tx_totalA, tx_totalB; NetStat day_stats[N_DAY_STATS], week_stats[N_WEEK_STATS], month_stats[N_MONTH_STATS]; gboolean show_totalB, totals_shown, reset_button_in, stats_button_in, mouse_in_chart, new_text_format; GkrellmLauncher launch; GtkWidget *launch_entry, *tooltip_entry; gboolean up, up_prev, up_event, down_event; gulong rx, tx; } NetMon;static void cb_alert_config(GkrellmAlert *ap, NetMon *net);static void cb_alert_config_create(GkrellmAlert *ap, GtkWidget *vbox, NetMon *net);static void net_stat_init(NetMon *net);typedef struct { gchar *name; gint type; } TimerType;static GList *net_mon_list;static GList *net_mon_sys_list;static GList *timer_defaults_list;static gchar *lock_directory;static gchar *net_data_dir;static void (*read_net_data)();static void (*check_net_routes)();static gboolean (*isdn_is_online)();static gboolean net_use_routed;static gboolean net_config_use_routed;static gint reset_mday;static gintstrcmp_net_name(NetMon *net1, NetMon *net2) { gchar *s; gint n, n1, n2, len; for (s = net1->name; *s; ++s) if (isdigit(*s)) break; if (!*s) return strcmp(net1->name, net2->name); n1 = atoi(s); len = s - net1->name; n = strncmp(net1->name, net2->name, len); if (n == 0) { n2 = atoi(net2->name + len); if (n1 < n2) n = -1; else if (n1 > n2) n = 1; } return n; }static NetMon *new_net(gchar *name) { NetMon *net; net = g_new0(NetMon, 1); net->name = g_strdup(name); if (strcmp(name, "lo")) net->enabled = TRUE; /* All except lo default to enabled */ net->chart_labels = TRUE; net->label = g_strdup(""); net->launch.command = g_strdup(""); net->launch.tooltip_comment = g_strdup(""); net->alert_uses_rx = net->alert_uses_tx = TRUE; net_mon_list = g_list_insert_sorted(net_mon_list, net, (GCompareFunc) strcmp_net_name); net_stat_init(net); return net; }static NetMon *lookup_net(gchar *name) { NetMon *net; GList *list; if (!name) return NULL; for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (!strcmp(net->name, name)) return net; } return NULL; }/* ------------- Net monitor to system dependent interface ------------- */voidgkrellm_net_client_divert(void (*read_func)(), void (*check_routes)(), gboolean (*isdn_online_func)()) { read_net_data = read_func; check_net_routes = check_routes; isdn_is_online = isdn_online_func; }static gbooleansetup_net_interface(void) { if (!read_net_data && !_GK.client_mode && gkrellm_sys_net_init()) { read_net_data = gkrellm_sys_net_read_data; check_net_routes = gkrellm_sys_net_check_routes; isdn_is_online = gkrellm_sys_net_isdn_online; } return read_net_data ? TRUE : FALSE; }gchar *gkrellm_net_mon_first(void) { gchar *name = NULL; net_mon_sys_list = net_mon_list; if (net_mon_sys_list) { name = ((NetMon *) (net_mon_sys_list->data))->name; net_mon_sys_list = net_mon_sys_list->next; } return name; }gchar*gkrellm_net_mon_next(void) { gchar *name = NULL; if (net_mon_sys_list) { name = ((NetMon *) (net_mon_sys_list->data))->name; net_mon_sys_list = net_mon_sys_list->next; } return name; }voidgkrellm_net_use_routed(gboolean real_routed) { /* real_routed should only ever be FALSE when called from client.c to | handle the server sysdep net code not using routed mode while the | client <-> server interface will always use routed mode. */ net_use_routed = TRUE; net_config_use_routed = real_routed; }voidgkrellm_net_routed_event(gchar *name, gboolean routed) { NetMon *net; if (!net_use_routed) return; if ((net = lookup_net(name)) == NULL) net = new_net(name); if (routed) net->up_event = TRUE; else net->down_event = TRUE; net->up = routed; }voidgkrellm_net_assign_data(gchar *name, gulong rx, gulong tx) { NetMon *net; if ((net = lookup_net(name)) == NULL) net = new_net(name); if (GK.second_tick && !net_use_routed) net->up = TRUE; net->rx = rx; net->tx = tx; }voidgkrellm_net_add_timer_type_ppp(gchar *name) { TimerType *t; if (!name || !*name || _GK.client_mode) return; t = g_new0(TimerType, 1); t->name = g_strdup(name); t->type = TIMER_TYPE_PPP; timer_defaults_list = g_list_append(timer_defaults_list, t); }voidgkrellm_net_add_timer_type_ippp(gchar *name) { TimerType *t; if (!name || !*name || _GK.client_mode) return; t = g_new0(TimerType, 1); t->name = g_strdup(name); t->type = TIMER_TYPE_IPPP; timer_defaults_list = g_list_append(timer_defaults_list, t); }voidgkrellm_net_set_lock_directory(gchar *dir) { lock_directory = g_strdup(dir); }/* ======================================================================== *//* Exporting net data for plugins */gintgkrellm_net_routes(void) { return g_list_length(net_mon_list); }gbooleangkrellm_net_stats(gint n, gchar *name, gulong *rx, gulong *tx) { GList *list; NetMon *net; list = g_list_nth(net_mon_list, n); if (!list) return FALSE; net = (NetMon *) list->data; if (name) strcpy(name, net->name); if (rx) *rx = net->rx; if (tx) *tx = net->tx; return TRUE; }voidgkrellm_net_led_positions(gint *x_rx_led, gint *y_rx_led, gint *x_tx_led, gint *y_tx_led) { if (x_rx_led) *x_rx_led = _GK.rx_led_x; if (y_rx_led) *y_rx_led = _GK.rx_led_y; if (x_tx_led) *x_tx_led = _GK.tx_led_x; if (y_tx_led) *y_tx_led = _GK.tx_led_y; }/* ======================================================================== */#include "pixmaps/net/decal_net_leds.xpm"#include "pixmaps/timer/bg_timer.xpm"#include "pixmaps/timer/decal_timer_button.xpm"#define MIN_GRID_RES 5#define MAX_GRID_RES 100000000#define DEFAULT_GRID_RES 20000 /* States for the timer button are indexes to the corresponding | timer button decal frame shown. */#define TB_NORMAL 0#define TB_PRESSED 1#define TB_STANDBY 2#define TB_ON 3#define N_TB_DECALS 4#define RX_LED 0#define TX_LED 1#define RX_OFF 0#define RX_ON 1#define TX_OFF 2#define TX_ON 3#define N_LEDS 4static GkrellmMonitor *mon_net;static GkrellmMonitor *mon_timer;static NetMon *net_timed; /* Monitor linked to timer button */GkrellmPanel *timer_panel; /* For the timer and button */static GtkWidget *net_vbox; /* Where all net monitors live */static GtkWidget *dynamic_net_vbox;static GtkWidget *timer_vbox;static GkrellmPiximage *bg_timer_piximage, *decal_net_led_piximage, *decal_timer_button_piximage;static GkrellmStyle *bg_timer_style;static GdkPixmap *decal_net_led_pixmap;static GdkBitmap *decal_net_led_mask;static GdkPixmap *decal_timer_button_pixmap;static GdkBitmap *decal_timer_button_mask;static GkrellmDecal *time_decal, *seconds_decal, *button_decal; /* These 3 decals will be drawn on net charts when mouse is in a chart and | the chart has a rx and/or tx total drawn. They won't live in a panel | so must be managed differently from decals on a panel. */static GkrellmDecal *decal_totalA, *decal_totalB, *decal_reset;static GkrellmDecal *decal_stats;static gchar *timer_on_command;static gchar *timer_off_command;static gchar *timer_button_iface;static gint timer_button_enabled;static gint last_time = -1;static gint timer_button_type = TIMER_TYPE_NONE, timer_button_state, timer_button_old_state, last_timer_command;static gboolean timer_seconds;static gint check_connect_state, net_stats_window_height;static gint ascent, ascent_alt, sec_pad;static time_t net_timer0;static gint net_style_id, timer_style_id;static GkrellmSizeAbbrev stats_bytes_abbrev[] = { { KB_SIZE(1), 1, "%.0f" }, { KB_SIZE(10), KB_SIZE(1), "%.1fKB" }, { MB_SIZE(1), KB_SIZE(1), "%.2fKB" }, { MB_SIZE(10), MB_SIZE(1), "%.3fMB" }, { MB_SIZE(100), MB_SIZE(1), "%.2fMB" }, { GB_SIZE(1), MB_SIZE(1), "%.2fMB" }, { GB_SIZE(10), GB_SIZE(1), "%.3fGB" }, { GB_SIZE(100), GB_SIZE(1), "%.2fGB" }, { TB_SIZE(1), GB_SIZE(1), "%.2fGB" }, { TB_SIZE(10), TB_SIZE(1), "%.3fTB" }, { TB_SIZE(100), TB_SIZE(1), "%.3fTB" }, { TB_SIZE(1000), TB_SIZE(1), "%.2fTB" } };#define NET_DATA_VERSION 2#define SATURDAY 6#define SUNDAY 0static gchar days_in_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* Called from client.c */voidgkrellm_net_server_has_timer(void) { timer_button_type = TIMER_TYPE_SERVER; }static voiddraw_led(NetMon *net, int rxtx, int led_index)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -