/* Returns whether the node in question should be displayed in the * diagram or not */ static gboolean display_node (node_t * node) { double diffms; if (!node) return FALSE; diffms = substract_times_ms(&appdata.now, &node->node_stats.stats.last_time); /* There are problems if a canvas_node is deleted if it still * has packets, so we have to check that as well */ /* Remove canvas_node if node is too old */ if (diffms >= pref.gui_node_timeout_time && pref.gui_node_timeout_time && !node->node_stats.pkt_list.length) return FALSE; #if 1 if ((pref.gui_node_timeout_time == 1) && !node->node_stats.pkt_list.length) g_my_critical ("Impossible situation in display node"); #endif return TRUE; } /* display_node */
void add_conversation (address_t * src_address, address_t * dst_address, guint16 src_port, guint16 dst_port, const gchar * data) { conversation_t *conv = NULL; const gchar *old_data = NULL; if (!src_address || !dst_address) { g_my_critical("NULL ptr in add_conversation"); return; } /* Make sure there is not one such conversation */ if ((old_data = find_conversation (src_address, dst_address, src_port, dst_port))) { if (!strcmp (old_data, data)) g_my_critical ("Conflicting conversations %s:%d-%s:%d in add_conversation", address_to_str (src_address), src_port, address_to_str (dst_address), dst_port); else g_my_debug ("Conversation %s:%d-%s:%d %s already exists in add_conversation", address_to_str (src_address), src_port, address_to_str (dst_address), dst_port, data); return; } g_my_debug ("Adding new conversation %s:%d-%s:%d %s", address_to_str (src_address), src_port, address_to_str (dst_address), dst_port, data); conv = g_malloc (sizeof (conversation_t)); g_assert(conv); address_copy(&conv->src_address, src_address); address_copy(&conv->dst_address, dst_address); conv->src_port = src_port; conv->dst_port = dst_port; conv->data = g_strdup (data); conversations = g_list_prepend (conversations, conv); n_conversations++; } /* add_conversation */
/* A zero in any of the ports matches any port number */ const gchar* find_conversation (address_t * src_address, address_t * dst_address, guint16 src_port, guint16 dst_port) { GList *item; if (!src_address || !dst_address) { g_my_critical("NULL ptr in find_conversation"); return NULL; } item = find_conversation_ptr(src_address, dst_address, src_port, dst_port); if (item) { /* found */ conversation_t *conv = item->data; return conv->data; } else return NULL; }
/* removes all conversations with the specified addresses */ void delete_conversation_link(address_t * src_address, address_t * dst_address) { GList *item; if (!src_address || !dst_address) { g_my_critical("NULL ptr in delete_conversation_link"); return; } while ( (item = find_conversation_ptr(src_address, dst_address, 0, 0)) ) { conversation_t *conv = NULL; conv = item->data; g_my_debug ("Removing conversation %s:%d-%s:%d %s", address_to_str (&conv->src_address), conv->src_port, address_to_str (&conv->dst_address), conv->dst_port, conv->data); g_free (conv->data); g_free (conv); conversations = g_list_delete_link(conversations, item); n_conversations--; } }
/* TODO this is probably this single piece of code I am most ashamed of. * I should learn how to use flex or yacc and do this The Right Way (TM)*/ void services_init(void) { FILE *services = NULL; gchar *line; gchar **t1 = NULL, **t2 = NULL; gchar *str; port_service_t *port_service; guint i; char filename[PATH_MAX]; port_type_t port_number; /* udp and tcp are the same */ if (tcp_services) return; /* already loaded */ safe_strncpy(filename, CONFDIR "/services", sizeof(filename)); if (!(services = fopen (filename, "r"))) { safe_strncpy(filename, "/etc/services", sizeof(filename)); if (!(services = fopen (filename, "r"))) { g_my_critical (_ ("Failed to open %s. No TCP or UDP services will be recognized"), filename); return; } } g_my_info (_("Reading TCP and UDP services from %s"), filename); service_names = g_tree_new_full(services_name_cmp, NULL, NULL, services_tree_free); tcp_services = g_tree_new_full(services_port_cmp, NULL, NULL, services_tree_free); udp_services = g_tree_new_full(services_port_cmp, NULL, NULL, services_tree_free); line = g_malloc (LINESIZE); g_assert(line); while (fgets (line, LINESIZE, services)) { if (line[0] != '#' && line[0] != ' ' && line[0] != '\n' && line[0] != '\t') { gboolean error = FALSE; if (!g_strdelimit (line, " \t\n", ' ')) error = TRUE; if (error || !(t1 = g_strsplit (line, " ", 0))) error = TRUE; if (!error && t1[0]) { gchar *told = t1[0]; t1[0] = g_ascii_strup (told, -1); g_free(told); } for (i = 1; t1[i] && !strcmp ("", t1[i]); i++) ; if (!error && (str = t1[i])) if (!(t2 = g_strsplit (str, "/", 0))) error = TRUE; if (error || !t2 || !t2[0]) error = TRUE; /* TODO The h here is not portable */ if (error || !sscanf (t2[0], "%hd", &port_number) || (port_number < 1)) error = TRUE; if (error || !t2[1]) error = TRUE; if (error || (g_ascii_strcasecmp ("udp", t2[1]) && g_ascii_strcasecmp ("tcp", t2[1]) && g_ascii_strcasecmp ("ddp", t2[1]) && g_ascii_strcasecmp ("sctp", t2[1]) )) error = TRUE; if (error) g_warning (_("Unable to parse line %s"), line); else { #if DEBUG g_my_debug ("Loading service %s %s %d", t2[1], t1[0], port_number); #endif if (!g_ascii_strcasecmp ("ddp", t2[1])) g_my_info (_("DDP protocols not supported in %s"), line); else if (!g_ascii_strcasecmp ("sctp", t2[1])) g_my_info (_("SCTP protocols not supported in %s"), line); else { /* map port to name, to two trees */ port_service = port_service_new(port_number, t1[0]); if (!g_ascii_strcasecmp ("tcp", t2[1])) g_tree_replace(tcp_services, &(port_service->port), port_service); else if (!g_ascii_strcasecmp ("udp", t2[1])) g_tree_replace(udp_services, &(port_service->port), port_service); } } g_strfreev (t2); t2 = NULL; g_strfreev (t1); t1 = NULL; } } fclose (services); g_free (line); /* now traverse port->name trees to fill the name->port tree */ g_tree_foreach(udp_services, services_port_trv, service_names); g_tree_foreach(tcp_services, services_port_trv, service_names); /* and finally assign preferred services */ services_fill_preferred(); } /* services_init */