📄 inet.c
字号:
/* Assume all connections are dead, then read_tcp_data() will set | still alive ones back to alive. Then I can prune really dead ones. */ for (list = active_tcp_list; list; list = list->next) { tcp = (ActiveTCP *)(list->data); tcp->state = TCP_DEAD; } (*read_tcp_data)(); for (list = active_tcp_list; list; ) { tcp = (ActiveTCP *)(list->data); if (tcp->state == TCP_DEAD) { if (list == active_tcp_list) active_tcp_list = active_tcp_list->next; list = g_list_remove(list, tcp); free_tcp_list = g_list_prepend(free_tcp_list, tcp); } else list = list->next; } } if (GK.second_tick) check_tcp = (check_tcp + 1) % update_interval; for (list = inet_mon_list; list; list = list->next) { in = (InetMon *) list->data; cp = in->chart; if (GK.hour_tick) { if (!*in->label0) in->hits0_hour = in->hits1_hour; gkrellm_store_chartdata(in->chart_hour, 0, in->hits0_hour, in->hits1_hour); in->hits0_hour = in->hits1_hour = 0; if (GK.day_tick) /* Make room for vertical day grid */ { gkrellm_store_chartdata(in->chart_hour, 0, 0, 0); gkrellm_store_chartdata(in->chart_hour, 0, 0, 0); } } if (GK.minute_tick) { if (!*in->label0) in->hits0_minute = in->hits1_minute; gkrellm_store_chartdata(in->chart_minute, 0, in->hits0_minute, in->hits1_minute); in->hits0_minute = in->hits1_minute = 0; if (GK.hour_tick) /* Make room for vertical hour grid */ { gkrellm_store_chartdata(in->chart_minute, 0, 0, 0); gkrellm_store_chartdata(in->chart_minute, 0, 0, 0); } gkrellm_refresh_chart(in->chart); draw_inet_mark_data(in, 1); } else if ( GK.second_tick && ( in->prev_active0 != in->active0 || in->prev_active1 != in->active1 ) ) draw_inet_chart(in); /* Just to update extra info draw */ if (GK.second_tick) draw_inet_mark_data(in, 0); if (in->busy && in->list_button->cur_index == D_MISC_BUTTON_OUT) i = D_MISC_BUTTON_ON; else i = D_MISC_BUTTON_OUT; gkrellm_set_decal_button_index(in->list_button, i); gkrellm_update_krell(in->panel, KRELL(in->panel), in->krell_hits); gkrellm_draw_panel_layers(in->panel); if (in->connection_string_event) { snprintf(buf, sizeof(buf), _("%s Connections"), in->name); gkrellm_message_dialog(buf, in->connection_string->str); in->connection_string_event = FALSE; in->busy = FALSE; } } }static gbooleantcp_port_is_monitored(ActiveTCP *tcp, gboolean range, gulong p0, gulong p1) { if ( (!range && (p0 == tcp->local_port || p1 == tcp->local_port)) || ( range && tcp->local_port >= p0 && tcp->local_port <= p1) ) return TRUE; return FALSE; }static gpointerget_connection_string_thread(void *data) { InetMon *in = (InetMon *) data; GList *list; ActiveTCP *tcp;#if defined(INET6) struct sockaddr_storage ss; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; gint salen, flag = 0; gchar hbuf[NI_MAXHOST]; gchar buf[NI_MAXHOST + 10];#else struct hostent *hostent; gchar buf[64];#endif if (in->connection_string) in->connection_string = g_string_truncate(in->connection_string, 0); else in->connection_string = g_string_new(""); for (list = in->tcp_save_list; list; list = list->next) { tcp = (ActiveTCP *) list->data;#if defined(INET6) memset(&ss, 0, sizeof(ss)); switch (tcp->family) { case AF_INET: sin = (struct sockaddr_in *)&ss; salen = sizeof(struct sockaddr_in); memcpy(&sin->sin_addr, &tcp->remote_addr, salen);#if defined(SIN6_LEN) sin->sin_len = salen;#endif sin->sin_family = tcp->family; break; case AF_INET6: sin6 = (struct sockaddr_in6 *)&ss; salen = sizeof(struct sockaddr_in6); memcpy(&sin6->sin6_addr, &tcp->remote_addr6, salen);#if defined(SIN6_LEN) sin6->sin6_len = salen;#endif sin6->sin6_family = tcp->family; /* XXX: We should mention about | scope, too. */ break; default: continue; } if (getnameinfo((struct sockaddr *)&ss, salen, hbuf, sizeof(hbuf), NULL, 0, flag)) continue; snprintf(buf, sizeof(buf), "%6d: %s\n", tcp->local_port, hbuf);#else hostent = gethostbyaddr((char *) &tcp->remote_addr, sizeof(struct in_addr), AF_INET); if (hostent) snprintf(buf, sizeof(buf), "%6d: %s\n", tcp->local_port, hostent->h_name); else snprintf(buf, sizeof(buf), "%6d: %s\n", tcp->local_port, inet_ntoa(tcp->remote_addr));#endif g_string_append(in->connection_string, buf); } if (in->connection_string->len == 0) g_string_append(in->connection_string, _("No current connections.")); in->connection_string_event = TRUE; gkrellm_free_glist_and_data(&in->tcp_save_list); return NULL; }static voidcb_list_button(GkrellmDecalbutton *button) { InetMon *in = (InetMon *) button->data; GList *list; ActiveTCP *tcp, *tcp_save; if (in->busy) return; in->busy = TRUE; /* Save a snapshot of active connections so I don't have to worry about | the active_tcp_list changing while in the thread. */ for (list = active_tcp_list; list; list = list->next) { tcp = (ActiveTCP *) list->data; if ( tcp_port_is_monitored(tcp, in->data0_is_range, in->port0_0, in->port0_1) || tcp_port_is_monitored(tcp, in->data1_is_range, in->port1_0, in->port1_1) ) { tcp_save = g_new0(ActiveTCP, 1); *tcp_save = *tcp; in->tcp_save_list = g_list_append(in->tcp_save_list, tcp_save); } } g_thread_create(get_connection_string_thread, in, FALSE, NULL); }static gintinet_expose_event(GtkWidget *widget, GdkEventExpose *ev) { InetMon *in; GList *list; GdkPixmap *pixmap = NULL; for (list = inet_mon_list; list; list = list->next) { in = (InetMon *) list->data; if (widget == in->panel->drawing_area) pixmap = in->panel->pixmap; else if (widget == in->chart_minute->drawing_area) pixmap = in->chart_minute->pixmap; else if (widget == in->chart_hour->drawing_area) pixmap = in->chart_hour->pixmap; if (pixmap) { gdk_draw_drawable(widget->window, gkrellm_draw_GC(1), pixmap, ev->area.x, ev->area.y, ev->area.x, ev->area.y, ev->area.width, ev->area.height); break; } } return FALSE; }static gintcb_inet_extra(GtkWidget *widget, GdkEventButton *ev) { InetMon *in; GList *list; for (list = inet_mon_list; list; list = list->next) { in = (InetMon *) list->data; if (widget != in->chart->drawing_area) continue; if (ev->button == 1 && ev->type == GDK_BUTTON_PRESS) { in->extra_info = !in->extra_info; gkrellm_refresh_chart(in->chart); gkrellm_config_modified(); } else if (ev->button == 2) { in->hour_mode = !in->hour_mode; select_hour_or_minute_chart(in); gkrellm_rescale_chart(in->chart); } else if ( ev->button == 3 || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) ) gkrellm_chartconfig_window_create(in->chart); break; } return FALSE; }static gintcb_panel_press(GtkWidget *widget, GdkEventButton *ev) { if (ev->button == 3) gkrellm_open_config_window(mon_inet); return FALSE; } /* Lock the hour and minute heights together. */static voidcb_inet_height(GkrellmChartconfig *cf, InetMon *in) { gint h; h = gkrellm_get_chartconfig_height(cf); if (in->chart_minute->h != h) gkrellm_set_chart_height(in->chart_minute, h); if (in->chart_hour->h != h) gkrellm_set_chart_height(in->chart_hour, h); }static voiddestroy_inet_monitor(InetMon *in) { if (in->launch_table) gtk_widget_destroy(in->launch_table); g_free(in->name); g_free(in->label0); g_free(in->label1); if (in->launch.command) g_free(in->launch.command); if (in->launch.button) gkrellm_destroy_button(in->launch.button); in->launch.tooltip = NULL; g_free(in->mark_data); /* The panel doesn't live in the chart struct, so destroy it separately */ gkrellm_panel_destroy(in->panel); gkrellm_chartconfig_destroy(&in->chart_config_minute); gkrellm_chart_destroy(in->chart_minute); gkrellm_chartconfig_destroy(&in->chart_config_hour); gkrellm_chart_destroy(in->chart_hour); gtk_widget_destroy(in->vbox); g_free(in); }#define MIN_GRID_RES 2#define MAX_GRID_RES 1000000#define DEFAULT_GRID_RES 10static voidchart_create(InetMon *in, GkrellmChart *cp, GkrellmChartconfig **cfp, gint first_create) { GkrellmChartconfig *cf; GkrellmChartdata *cd; GdkPixmap **src_pixmap, *grid_pixmap; cp->y = 3; gkrellm_chart_create(in->vbox, mon_inet, cp, cfp); cf = *cfp; /* I accumulate tcp hits myself, so I'm free to make the chartdata | accumulate monotonically or not. I choose not monotonic to make saving | and loading data simpler. */ src_pixmap = gkrellm_data_out_pixmap(); grid_pixmap = gkrellm_data_out_grid_pixmap(); if (*in->label0) { cd = gkrellm_add_chartdata(cp, src_pixmap, grid_pixmap, in->label0); gkrellm_monotonic_chartdata(cd, FALSE); } src_pixmap = gkrellm_data_in_pixmap(); grid_pixmap = gkrellm_data_in_grid_pixmap(); if (*in->label1) { cd = gkrellm_add_chartdata(cp, src_pixmap, grid_pixmap, in->label1); gkrellm_monotonic_chartdata(cd, FALSE); } gkrellm_set_draw_chart_function(cp, draw_inet_chart, in); /* krell is not function of chart grids or resolution, so no interest | in connecting to grid or resolution changes. */ gkrellm_chartconfig_height_connect(cf, cb_inet_height, in); gkrellm_chartconfig_grid_resolution_adjustment(cf, TRUE, 0, (gfloat) MIN_GRID_RES, (gfloat) MAX_GRID_RES, 0, 0, 0, 70); if (gkrellm_get_chartconfig_grid_resolution(cf) < MIN_GRID_RES) gkrellm_set_chartconfig_grid_resolution(cf, DEFAULT_GRID_RES); /* Don't want to waste an hour priming the pump, and don't need to | because data always starts at zero. */ cp->primed = TRUE; /* XXX */ gkrellm_alloc_chartdata(cp); if (first_create) { g_signal_connect(G_OBJECT (cp->drawing_area), "expose_event", G_CALLBACK(inet_expose_event), NULL); g_signal_connect(G_OBJECT(cp->drawing_area),"button_press_event", G_CALLBACK(cb_inet_extra), NULL); } }static voidcreate_inet_monitor(GtkWidget *vbox1, InetMon *in, gint first_create) { GtkWidget *vbox; GkrellmChart *cp; GkrellmPanel *p; GkrellmMargin *m; GkrellmStyle *style; gint x; if (first_create) { vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(vbox1), vbox); in->vbox = vbox; in->chart_minute = gkrellm_chart_new0(); in->chart_hour = gkrellm_chart_new0(); in->panel = gkrellm_panel_new0(); in->chart = in->chart_minute; in->name = g_strdup_printf(_("inet%d"), n_inet_monitors++); } else { vbox = in->vbox; gkrellm_destroy_decal_list(in->panel); gkrellm_destroy_krell_list(in->panel); } if (in->chart_config_hour && in->chart_config_minute) in->chart_config_hour->h = in->chart_config_minute->h; chart_create(in, in->chart_minute, &in->chart_config_minute, first_create); gkrellm_chartconfig_grid_resolution_label(in->chart_config_minute, _("TCP hits per minute")); chart_create(in, in->chart_hour, &in->chart_config_hour, first_create); gkrellm_chartconfig_grid_resolution_label(in->chart_config_hour, _("TCP hits per hour")); cp = in->chart; p = in->panel; style = gkrellm_panel_style(style_id); m = gkrellm_get_style_margins(style); if (style->label_position == 50 && gkrellm_chart_width() < 80) style->label_position = 40; /* Not a kludge, an adjustment! */ in->list_decal = gkrellm_create_decal_pixmap(p, gkrellm_decal_misc_pixmap(), gkrellm_decal_misc_mask(), N_MISC_DECALS, style, -1, -1); if (style->label_position <= 50) x = gkrellm_chart_width() - in->list_decal->w - m->right; else x = m->left; gkrellm_move_decal(p, in->list_decal, x, in->list_decal->y); gkrellm_create_krell(p, gkrellm_krell_panel_piximage(style_id), style); /* Inet krells are not related to chart scale_max. Just give a constant | full scale of 5. */ KRELL(p)->full_scale = 5; gkrellm_panel_configure(p, in->name, style); gkrellm_panel_create(vbox, mon_inet, p); /* At first_create both charts will be visible, but this will be | undone below */ in->list_button = gkrellm_make_decal_button(p, in->list_decal, cb_list_button, in, D_MISC_BUTTON_OUT, D_MISC_BUTTON_IN); if (first_create) { g_signal_connect(G_OBJECT(p->drawing_area),"expose_event", G_CALLBACK(inet_expose_event), NULL); g_signal_connect(G_OBJECT(p->drawing_area), "button_press_event", G_CALLBACK(cb_panel_press), NULL); gtk_widget_show(vbox); gkrellm_chart_hide(in->chart_hour, FALSE); } gkrellm_setup_launcher(p, &in->launch, CHART_PANEL_TYPE, 4); if (in->mark_data) g_free(in->mark_data); in->mark_data = g_new0(gshort, cp->w); if (! first_create) gkrellm_rescale_chart(in->chart); }static voidcreate_inet(GtkWidget *vbox, gint first_create) { GList *list; gint new_data = FALSE; gint i; static gint last_chart_width; inet_vbox = vbox; if (grid) { g_object_unref(G_OBJECT(grid)); grid = NULL; } n_inet_monitors = 0; if (!first_create && last_chart_width != gkrellm_chart_width()) { /* Will be allocating new data arrays */ gkrellm_inet_save_data(); new_data = TRUE; last_chart_width = gkrellm_chart_width(); } for (i = 0, list = inet_mon_list; list; ++i, list = list->next) create_inet_monitor(inet_vbox, (InetMon *)list->data, first_create); if (first_create || new_data) gkrellm_inet_load_data(); if (inet_mon_list) gkrellm_spacers_show(mon_inet); else gkrellm_spacers_hide(mon_inet); }static InetMon *lookup_inet(gchar *name) { InetMon *in; GList *list; for (list = inet_mon_list; list; list = list->next) { in = (InetMon *) list->data; if (name && in->name && !strcmp(in->name, name)) return in; } return NULL; }/* --------------------------------------------------------------------- */#define INET_CONFIG_KEYWORD "inet"static voidsave_inet_config(FILE *f) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -