3 * Allows to display a flow graph of the currently displayed packets
5 * Copyright 2004, Ericsson , Spain
6 * By Francisco Alcoba <francisco.alcoba@ericsson.com>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "epan/filesystem.h"
36 #include "graph_analysis.h"
37 #include <epan/stat_cmd_args.h>
38 #include "../stat_menu.h"
39 #include "gui_stat_menu.h"
40 #include "dlg_utils.h"
41 #include "gui_utils.h"
42 #include "gtkglobals.h"
44 #include "simple_dialog.h"
46 #include <epan/to_str.h>
48 #include <epan/emem.h>
49 #include <epan/dissectors/packet-tcp.h>
52 #include <epan/strutil.h>
61 static int type_of_packets = DISPLAYED;
62 static int type_of_flow = GENERAL;
63 static int node_addr_type = SRCDST;
65 static int tap_identifier;
66 static gboolean have_frame_tap_listener=FALSE;
67 static gboolean have_tcp_tap_listener=FALSE;
68 static graph_analysis_info_t *graph_analysis = NULL;
69 static graph_analysis_data_t *graph_analysis_data;
71 static GtkWidget *flow_graph_dlg = NULL;
73 static GtkWidget *select_all_rb;
74 static GtkWidget *select_displayed_rb;
75 static GtkWidget *select_general_rb;
76 static GtkWidget *select_tcp_rb;
77 static GtkWidget *src_dst_rb;
78 static GtkWidget *net_src_dst_rb;
80 void flow_graph_data_init(void);
83 /****************************************************************************/
84 /* free up memory and initialize the pointers */
86 static void flow_graph_reset(void *ptr _U_)
89 graph_analysis_item_t *graph_item;
93 if (graph_analysis !=NULL){
95 /* free the graph data items */
96 list = g_list_first(graph_analysis->list);
99 graph_item = list->data;
100 g_free(graph_item->frame_label);
101 g_free(graph_item->comment);
103 list = g_list_next(list);
105 g_list_free(graph_analysis->list);
106 graph_analysis->nconv = 0;
107 graph_analysis->list = NULL;
112 /****************************************************************************/
113 void flow_graph_data_init(void) {
114 graph_analysis = g_malloc(sizeof(graph_analysis_info_t));
115 graph_analysis->nconv = 0;
116 graph_analysis->list = NULL;
120 /* XXX just copied from gtk/rpc_stat.c */
121 void protect_thread_critical_region(void);
122 void unprotect_thread_critical_region(void);
124 /****************************************************************************/
126 remove_tap_listener_flow_graph(void)
128 protect_thread_critical_region();
129 remove_tap_listener(&(tap_identifier));
130 unprotect_thread_critical_region();
132 have_frame_tap_listener=FALSE;
133 have_tcp_tap_listener=FALSE;
137 /****************************************************************************/
139 /****************************************************************************/
141 flow_graph_on_destroy(GtkObject *object _U_, gpointer user_data _U_)
143 /* remove_tap_listeners */
144 remove_tap_listener_flow_graph();
146 /* Clean up memory used by tap */
147 flow_graph_reset(NULL);
149 /* Note that we no longer have a "Flow Graph" dialog box. */
150 flow_graph_dlg = NULL;
154 /****************************************************************************/
156 toggle_select_all(GtkWidget *widget _U_, gpointer user_data _U_)
158 /* is the button now active? */
159 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(select_all_rb))) {
160 type_of_packets = ALL;
164 /****************************************************************************/
166 toggle_select_displayed(GtkWidget *widget _U_, gpointer user_data _U_)
168 /* is the button now active? */
169 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(select_displayed_rb))) {
170 type_of_packets = DISPLAYED;
174 /****************************************************************************/
176 toggle_select_general(GtkWidget *widget _U_, gpointer user_data _U_)
178 /* is the button now active? */
179 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(select_general_rb))) {
180 type_of_flow = GENERAL;
184 /****************************************************************************/
186 toggle_select_tcp(GtkWidget *widget _U_, gpointer user_data _U_)
188 /* is the button now active? */
189 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(select_tcp_rb))) {
194 /****************************************************************************/
196 toggle_select_srcdst(GtkWidget *widget _U_, gpointer user_data _U_)
198 /* is the button now active? */
199 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(src_dst_rb))) {
200 node_addr_type = SRCDST;
204 /****************************************************************************/
206 toggle_select_netsrcdst(GtkWidget *widget _U_, gpointer user_data _U_)
208 /* is the button now active? */
209 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(net_src_dst_rb))) {
210 node_addr_type = NET_SRCDST;
214 /****************************************************************************/
215 /* Add a new frame into the graph */
216 static int flow_graph_frame_add_to_graph(packet_info *pinfo)
218 graph_analysis_item_t *gai;
226 if (node_addr_type == NET_SRCDST) {
227 if (pinfo->net_src.type!=AT_NONE && pinfo->net_dst.type!=AT_NONE) {
228 gai = g_malloc(sizeof(graph_analysis_item_t));
229 COPY_ADDRESS(&(gai->src_addr),&(pinfo->net_src));
230 COPY_ADDRESS(&(gai->dst_addr),&(pinfo->net_dst));
235 if (pinfo->src.type!=AT_NONE && pinfo->dst.type!=AT_NONE) {
236 gai = g_malloc(sizeof(graph_analysis_item_t));
237 COPY_ADDRESS(&(gai->src_addr),&(pinfo->src));
238 COPY_ADDRESS(&(gai->dst_addr),&(pinfo->dst));
243 gai->frame_num = pinfo->fd->num;
244 gai->time= nstime_to_sec(&pinfo->fd->rel_ts);
246 gai->port_src=pinfo->srcport;
247 gai->port_dst=pinfo->destport;
249 gai->frame_label=NULL;
251 if (gai->comment!=NULL){
252 g_free(gai->comment);
254 if (gai->frame_label!=NULL){
255 g_free(gai->frame_label);
260 if (pinfo->cinfo->col_first[COL_INFO]>=0){
262 for (i = pinfo->cinfo->col_first[COL_INFO]; i <= pinfo->cinfo->col_last[COL_INFO]; i++) {
263 if (pinfo->cinfo->fmt_matx[i][COL_INFO]) {
264 colinfo = g_strdup(pinfo->cinfo->col_data[i]);
269 if (pinfo->cinfo->col_first[COL_PROTOCOL]>=0){
271 for (i = pinfo->cinfo->col_first[COL_PROTOCOL]; i <= pinfo->cinfo->col_last[COL_PROTOCOL]; i++) {
272 if (pinfo->cinfo->fmt_matx[i][COL_PROTOCOL]) {
273 protocol = g_strdup(pinfo->cinfo->col_data[i]);
280 if (colinfo != NULL) {
281 if (protocol != NULL) {
282 gai->frame_label = g_strdup_printf("%.19s", colinfo);
283 gai->comment = g_strdup_printf("%s: %s", protocol, colinfo);
285 gai->frame_label = g_strdup_printf("%.19s", colinfo);
286 gai->comment = g_strdup_printf("%s", colinfo);
289 /* This will probably never happen...*/
290 if (protocol != NULL) {
291 gai->frame_label = g_strdup_printf("%.19s", protocol);
292 gai->comment = g_strdup_printf("%s", protocol);
294 gai->frame_label = NULL;
310 graph_analysis->list = g_list_append(graph_analysis->list, gai);
316 /****************************************************************************/
317 /* Add a new tcp frame into the graph */
318 static int flow_graph_tcp_add_to_graph(packet_info *pinfo, const struct tcpheader *tcph)
320 graph_analysis_item_t *gai;
321 /* copied from packet-tcp */
322 const gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
324 gboolean flags_found = FALSE;
327 gai = g_malloc(sizeof(graph_analysis_item_t));
328 gai->frame_num = pinfo->fd->num;
329 gai->time= nstime_to_sec(&pinfo->fd->rel_ts);
330 if (node_addr_type == NET_SRCDST) {
331 COPY_ADDRESS(&(gai->src_addr),&(pinfo->net_src));
332 COPY_ADDRESS(&(gai->dst_addr),&(pinfo->net_dst));
334 COPY_ADDRESS(&(gai->src_addr),&(pinfo->src));
335 COPY_ADDRESS(&(gai->dst_addr),&(pinfo->dst));
337 gai->port_src=pinfo->srcport;
338 gai->port_dst=pinfo->destport;
341 for (i = 0; i < 8; i++) {
343 if (tcph->th_flags & bpos) {
345 g_strlcat(flags, ", ", 64);
347 g_strlcat(flags, fstr[i], 64);
351 if (flags[0] == '\0') {
352 g_snprintf (flags, 64, "<None>");
355 if ((tcph->th_have_seglen)&&(tcph->th_seglen!=0)){
356 gai->frame_label = g_strdup_printf("%s - Len: %u",flags, tcph->th_seglen);
359 gai->frame_label = g_strdup(flags);
362 gai->comment = g_strdup_printf("Seq = %u Ack = %u",tcph->th_seq, tcph->th_ack);
368 graph_analysis->list = g_list_append(graph_analysis->list, gai);
376 /****************************************************************************/
377 /* whenever a frame packet is seen by the tap listener */
379 flow_graph_frame_packet( void *ptr _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *dummy _U_)
382 if ((type_of_packets == ALL)||(pinfo->fd->flags.passed_dfilter==1)){
383 flow_graph_frame_add_to_graph(pinfo);
389 /****************************************************************************/
390 /* whenever a TCP packet is seen by the tap listener */
392 flow_graph_tcp_packet( void *ptr _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *tcp_info)
394 const struct tcpheader *tcph = tcp_info;
396 if ((type_of_packets == ALL)||(pinfo->fd->flags.passed_dfilter==1)){
397 flow_graph_tcp_add_to_graph(pinfo,tcph);
404 static void flow_graph_packet_draw(void *prs _U_)
409 /****************************************************************************/
411 flow_graph_on_ok (GtkButton *button _U_,
415 if ((have_frame_tap_listener==TRUE)
416 ||(have_tcp_tap_listener==TRUE))
418 /* remove_tap_listeners */
419 remove_tap_listener_flow_graph();
422 /* Scan for displayed packets (retap all packets) */
424 if (type_of_flow == GENERAL){
425 /* Register the tap listener */
427 if(have_frame_tap_listener==FALSE)
429 /* don't register tap listener, if we have it already */
430 register_tap_listener("frame", &tap_identifier, NULL,
432 flow_graph_frame_packet,
433 flow_graph_packet_draw
435 have_frame_tap_listener=TRUE;
438 cf_retap_packets(&cfile, TRUE);
440 else if (type_of_flow == TCP){
441 /* Register the tap listener */
443 if(have_tcp_tap_listener==FALSE)
445 /* don't register tap listener, if we have it already */
446 register_tap_listener("tcp", &tap_identifier, NULL,
448 flow_graph_tcp_packet,
449 flow_graph_packet_draw
451 have_tcp_tap_listener=TRUE;
454 cf_retap_packets(&cfile, FALSE);
457 if (graph_analysis_data->dlg.window != NULL){ /* if we still have a window */
458 graph_analysis_update(graph_analysis_data); /* refresh it xxx */
461 graph_analysis_data->dlg.parent_w = user_data;
462 graph_analysis_create(graph_analysis_data);
468 /****************************************************************************/
470 /****************************************************************************/
472 static void flow_graph_dlg_create (void)
475 GtkWidget *flow_graph_dlg_w;
477 GtkWidget *hbuttonbox;
478 GtkWidget *bt_close, *bt_ok;
480 GtkWidget *top_label = NULL;
482 GtkWidget *flow_type_fr, *range_fr, *range_tb, *flow_type_tb, *node_addr_fr, *node_addr_tb;
485 GtkTooltips *tooltips = gtk_tooltips_new();
487 flow_graph_dlg_w=window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Flow Graph");
489 gtk_window_set_default_size(GTK_WINDOW(flow_graph_dlg_w), 350, 150);
491 main_vb = gtk_vbox_new (FALSE, 0);
492 gtk_container_add(GTK_CONTAINER(flow_graph_dlg_w), main_vb);
493 gtk_container_set_border_width (GTK_CONTAINER (main_vb), 12);
496 top_label = gtk_label_new ("Choose packets to include in the graph");
497 gtk_box_pack_start (GTK_BOX (main_vb), top_label, FALSE, FALSE, 8);
500 gtk_widget_show(flow_graph_dlg_w);
502 /*** Packet range frame ***/
503 range_fr = gtk_frame_new("Choose packets");
504 gtk_box_pack_start(GTK_BOX(main_vb), range_fr, FALSE, FALSE, 0);
506 range_tb = gtk_table_new(4, 4, FALSE);
507 gtk_container_border_width(GTK_CONTAINER(range_tb), 5);
508 gtk_container_add(GTK_CONTAINER(range_fr), range_tb);
510 /* Process all packets */
511 select_all_rb = gtk_radio_button_new_with_mnemonic_from_widget(NULL, "_All packets");
512 gtk_tooltips_set_tip (tooltips, select_all_rb,
513 ("Process all packets"), NULL);
514 g_signal_connect(select_all_rb, "toggled", G_CALLBACK(toggle_select_all), NULL);
515 gtk_table_attach_defaults(GTK_TABLE(range_tb), select_all_rb, 0, 1, 0, 1);
516 if (type_of_packets == ALL) {
517 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(select_all_rb),TRUE);
519 gtk_widget_show(select_all_rb);
521 /* Process displayed packets */
522 select_displayed_rb = gtk_radio_button_new_with_mnemonic_from_widget(GTK_RADIO_BUTTON(select_all_rb), "_Displayed packets");
523 gtk_tooltips_set_tip (tooltips, select_displayed_rb,
524 ("Process displayed packets"), NULL);
525 g_signal_connect(select_displayed_rb, "toggled", G_CALLBACK(toggle_select_displayed), NULL);
526 gtk_table_attach_defaults(GTK_TABLE(range_tb), select_displayed_rb, 1, 2, 0, 1);
527 if (type_of_packets == DISPLAYED) {
528 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(select_displayed_rb),TRUE);
530 gtk_widget_show(select_displayed_rb);
532 gtk_widget_show(range_tb);
534 gtk_widget_show(range_fr);
536 /*** Flow type frame ***/
537 flow_type_fr = gtk_frame_new("Choose flow type");
538 gtk_box_pack_start(GTK_BOX(main_vb), flow_type_fr, FALSE, FALSE, 0);
540 flow_type_tb = gtk_table_new(4, 4, FALSE);
541 gtk_container_border_width(GTK_CONTAINER(flow_type_tb), 5);
542 gtk_container_add(GTK_CONTAINER(flow_type_fr), flow_type_tb);
544 /* General information */
545 select_general_rb = gtk_radio_button_new_with_mnemonic_from_widget(NULL, "_General flow");
546 gtk_tooltips_set_tip (tooltips, select_general_rb,
547 ("Show all packets, with general information"), NULL);
548 g_signal_connect(select_general_rb, "toggled", G_CALLBACK(toggle_select_general), NULL);
549 gtk_table_attach_defaults(GTK_TABLE(flow_type_tb), select_general_rb, 0, 1, 0, 1);
550 if (type_of_flow == GENERAL) {
551 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(select_general_rb),TRUE);
553 gtk_widget_show(select_general_rb);
555 /* TCP specific information */
556 select_tcp_rb = gtk_radio_button_new_with_mnemonic_from_widget(GTK_RADIO_BUTTON(select_general_rb), "_TCP flow");
557 gtk_tooltips_set_tip (tooltips, select_tcp_rb,
558 ("Show only TCP packets, with TCP specific information"), NULL);
559 g_signal_connect(select_tcp_rb, "toggled", G_CALLBACK(toggle_select_tcp), NULL);
560 gtk_table_attach_defaults(GTK_TABLE(flow_type_tb), select_tcp_rb, 1, 2, 0, 1);
561 if (type_of_flow == TCP) {
562 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(select_tcp_rb),TRUE);
565 gtk_widget_show(select_tcp_rb);
567 gtk_widget_show(flow_type_tb);
568 gtk_widget_show(flow_type_fr);
570 /*** Node address type frame ***/
571 node_addr_fr = gtk_frame_new("Choose node address type");
572 gtk_box_pack_start(GTK_BOX(main_vb), node_addr_fr, FALSE, FALSE, 0);
574 node_addr_tb = gtk_table_new(4, 4, FALSE);
575 gtk_container_border_width(GTK_CONTAINER(node_addr_tb), 5);
576 gtk_container_add(GTK_CONTAINER(node_addr_fr), node_addr_tb);
578 /* Source / Dest address */
579 src_dst_rb = gtk_radio_button_new_with_mnemonic_from_widget(NULL, "_Standard source/destination addresses");
580 gtk_tooltips_set_tip (tooltips, src_dst_rb,
581 ("Nodes in the diagram are identified with source and destination addresses"), NULL);
582 g_signal_connect(src_dst_rb, "toggled", G_CALLBACK(toggle_select_srcdst), NULL);
583 gtk_table_attach_defaults(GTK_TABLE(node_addr_tb), src_dst_rb, 0, 1, 0, 1);
584 if (node_addr_type == SRCDST) {
585 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(src_dst_rb),TRUE);
587 gtk_widget_show(src_dst_rb);
589 /* Network source / dest address */
590 net_src_dst_rb = gtk_radio_button_new_with_mnemonic_from_widget(GTK_RADIO_BUTTON(src_dst_rb), "_Network source/destination addresses");
591 gtk_tooltips_set_tip (tooltips, net_src_dst_rb,
592 ("Nodes in the diagram are identified with network source and destination addresses"), NULL);
593 g_signal_connect(net_src_dst_rb, "toggled", G_CALLBACK(toggle_select_netsrcdst), NULL);
594 gtk_table_attach_defaults(GTK_TABLE(node_addr_tb), net_src_dst_rb, 1, 2, 0, 1);
595 if (node_addr_type == NET_SRCDST) {
596 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(net_src_dst_rb),TRUE);
599 gtk_widget_show(net_src_dst_rb);
601 gtk_widget_show(node_addr_tb);
602 gtk_widget_show(node_addr_fr);
605 hbuttonbox = gtk_hbutton_box_new ();
606 gtk_box_pack_start (GTK_BOX (main_vb), hbuttonbox, FALSE, FALSE, 0);
607 gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_SPREAD);
608 gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox), 30);
610 bt_ok = gtk_button_new_from_stock(GTK_STOCK_OK);
611 gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_ok);
612 gtk_tooltips_set_tip (tooltips, bt_ok, "Show the flow graph", NULL);
613 g_signal_connect(bt_ok, "clicked", G_CALLBACK(flow_graph_on_ok), flow_graph_dlg_w);
614 gtk_widget_show(bt_ok);
616 bt_close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
617 gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_close);
618 GTK_WIDGET_SET_FLAGS(bt_close, GTK_CAN_DEFAULT);
619 gtk_tooltips_set_tip (tooltips, bt_close, "Close this dialog", NULL);
620 window_set_cancel_button(flow_graph_dlg_w, bt_close, window_cancel_button_cb);
622 g_signal_connect(flow_graph_dlg_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
623 g_signal_connect(flow_graph_dlg_w, "destroy", G_CALLBACK(flow_graph_on_destroy), NULL);
625 gtk_widget_show_all(flow_graph_dlg_w);
626 window_present(flow_graph_dlg_w);
628 flow_graph_dlg = flow_graph_dlg_w;
631 /****************************************************************************/
633 /****************************************************************************/
635 /* init function for tap */
637 flow_graph_init_tap(const char *dummy _U_, void* userdata _U_)
640 /* initialize graph items store */
641 flow_graph_data_init();
643 /* init the Graph Analysys */
644 graph_analysis_data = graph_analysis_init();
645 graph_analysis_data->graph_info = graph_analysis;
647 /* create dialog box if necessary */
648 if (flow_graph_dlg == NULL) {
649 flow_graph_dlg_create();
651 /* There's already a dialog box; reactivate it. */
652 reactivate_window(flow_graph_dlg);
658 /****************************************************************************/
659 /* entry point when called via the GTK menu */
660 static void flow_graph_launch(GtkWidget *w _U_, gpointer data _U_)
662 flow_graph_init_tap("",NULL);
665 /****************************************************************************/
667 register_tap_listener_flow_graph(void)
669 register_stat_cmd_arg("flow_graph",flow_graph_init_tap,NULL);
670 register_stat_menu_item("Flo_w Graph...", REGISTER_STAT_GROUP_NONE,
671 flow_graph_launch, NULL, NULL, NULL);