When a new display filter is to be applied, don't set "cf.dfilter" or
[obnox/wireshark/wip.git] / summary.c
1 /* summary.c
2  * Routines for capture file summary window
3  *
4  * $Id: summary.c,v 1.13 1999/10/11 06:39:01 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <gtk/gtk.h>
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #ifdef NEED_SNPRINTF_H
46 # ifdef HAVE_STDARG_H
47 #  include <stdarg.h>
48 # else
49 #  include <varargs.h>
50 # endif
51 # include "snprintf.h"
52 #endif
53
54 #ifdef HAVE_SYS_SOCKIO_H
55 # include <sys/sockio.h>
56 #endif
57
58 #include "gtk/main.h"
59 #include "packet.h"
60 #include "file.h"
61 #include "summary.h"
62 #include "capture.h"
63 #include "util.h"
64 #include "prefs.h"
65
66 extern capture_file  cf;
67
68 /* File selection data keys */
69 #define E_SUM_PREP_FS_KEY "sum_prep_fs"
70 #define E_SUM_PREP_TE_KEY "sum_prep_te"
71
72 /* Summary callback data keys */
73 #define E_SUM_IFACE_KEY "sum_iface"
74 #define E_SUM_FILT_KEY  "sum_filter"
75 #define E_SUM_COUNT_KEY "sum_count"
76 #define E_SUM_OPEN_KEY  "sum_open"
77 #define E_SUM_SNAP_KEY  "sum_snap"
78
79 #define SUM_STR_MAX 1024
80
81 /* Summary filter key */
82 #define E_SUM_FILT_TE_KEY "sum_filt_te"
83
84 double
85 secs_usecs( guint32 s, guint32 us) {
86   return (us / 1000000.0) + (double)s;
87 }
88
89 void
90 tally_frame_data(gpointer cf, gpointer st) {
91   double cur_time;
92   summary_tally * sum_tally = (summary_tally *)st;
93   frame_data *cur_frame = (frame_data *)cf;
94
95   cur_time = secs_usecs(cur_frame->abs_secs, cur_frame->abs_usecs);
96     if (cur_time < sum_tally->start_time) {
97       sum_tally->start_time = cur_time;
98     }
99     if (cur_time > sum_tally->stop_time){
100     sum_tally->stop_time = cur_time;
101   }
102   sum_tally->bytes += cur_frame->pkt_len;
103   if (cur_frame->passed_dfilter)
104           sum_tally->filtered_count++;
105 }
106
107 void
108 add_string_to_box(gchar *str, GtkWidget *box) {
109   GtkWidget *lb;
110   lb = gtk_label_new(str);
111   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
112   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
113   gtk_widget_show(lb);
114 }
115
116 void
117 summary_prep_cb(GtkWidget *w, gpointer d) {
118   frame_data    *first_frame, *cur_frame;
119   summary_tally *st;
120   GtkWidget     *sum_open_w,
121                 *main_vb, *file_fr, *data_fr, *capture_fr, *file_box, 
122 *data_box,
123                 *capture_box;
124
125  gchar          string_buff[SUM_STR_MAX];
126
127  guint32        traffic_bytes, i;
128  double         seconds;
129  frame_data    *cur_glist;
130
131  /* initialize the tally */
132   first_frame = cf.plist;
133   st = (summary_tally *)g_malloc(sizeof(summary_tally));
134   st->start_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
135 ;
136   st->stop_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
137 ;
138   st->bytes = 0;
139   st->filtered_count = 0;
140   cur_glist = cf.plist;
141
142   for (i = 0; i < cf.count; i++){
143     cur_frame = cur_glist;
144     tally_frame_data(cur_frame, st);
145     cur_glist = cur_glist->next;
146   }
147
148   /* traffic_bytes will be computed here */
149   traffic_bytes = st->bytes;
150   seconds = st->stop_time - st->start_time;
151   sum_open_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
152   gtk_window_set_title(GTK_WINDOW(sum_open_w), "Ethereal: Summary");
153
154   /* Container for each row of widgets */
155   main_vb = gtk_vbox_new(FALSE, 3);
156   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
157   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
158   gtk_widget_show(main_vb);
159
160   /* File frame */
161   file_fr = gtk_frame_new("File");
162   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
163   gtk_widget_show(file_fr);
164
165   file_box = gtk_vbox_new(FALSE, 3);
166   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
167   gtk_widget_show(file_box);
168
169   /* filename */
170   snprintf(string_buff, SUM_STR_MAX, "Name: %s", cf.filename);
171   add_string_to_box(string_buff, file_box);
172
173   /* length */
174   snprintf(string_buff, SUM_STR_MAX, "Length: %lu", cf.f_len);
175   add_string_to_box(string_buff, file_box);
176
177   /* format */
178   snprintf(string_buff, SUM_STR_MAX, "Format: %s", cf.cd_t_desc);
179   add_string_to_box(string_buff, file_box);
180
181   /* snapshot length */
182   snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", cf.snap);
183   add_string_to_box(string_buff, file_box);
184
185   /* Data frame */
186   data_fr = gtk_frame_new("Data");
187   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
188   gtk_widget_show(data_fr);
189
190   data_box = gtk_vbox_new(FALSE, 3);
191   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
192   gtk_widget_show(data_box);
193
194   /* seconds */
195   snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", 
196 secs_usecs(cf.esec,cf.eusec));
197   add_string_to_box(string_buff, data_box);
198
199   snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
200   add_string_to_box(string_buff, data_box);
201
202   /* Packet count */
203   snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", cf.count);
204   add_string_to_box(string_buff, data_box);
205
206   /* Filtered Packet count */
207   snprintf(string_buff, SUM_STR_MAX, "Filtered packet count: %i", st->filtered_count);
208   add_string_to_box(string_buff, data_box);
209
210   /* Packets per second */
211   if (seconds > 0){
212     snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", 
213 cf.count/seconds);
214     add_string_to_box(string_buff, data_box);
215   }
216
217   /* Dropped count */
218   snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %i", cf.drops);
219   add_string_to_box(string_buff, data_box);
220
221   /* Byte count */
222   snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", 
223 traffic_bytes);
224   add_string_to_box(string_buff, data_box);
225
226   /* Bytes per second */
227   if (seconds > 0){
228     snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", 
229 traffic_bytes/seconds);
230     add_string_to_box(string_buff, data_box);
231   }
232
233   /* Capture frame */
234   capture_fr = gtk_frame_new("Capture");
235   gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
236   gtk_widget_show(capture_fr);
237
238   capture_box = gtk_vbox_new(FALSE, 3);
239   gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
240   gtk_widget_show(capture_box);
241
242
243   /* interface */
244   if (cf.iface) {
245     snprintf(string_buff, SUM_STR_MAX, "Interface: %s", cf.iface);
246   } else {
247     sprintf(string_buff, "Interface: unknown");
248   }
249   add_string_to_box(string_buff, capture_box);
250
251   /* Display filter */
252   if (cf.dfilter) {
253     snprintf(string_buff, SUM_STR_MAX, "Display filter: %s", cf.dfilter);
254   } else {
255     sprintf(string_buff, "Display filter: none");
256   }
257   add_string_to_box(string_buff, capture_box);
258
259 #ifdef HAVE_LIBPCAP
260   /* Capture filter */
261   if (cf.cfilter) {
262     snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", cf.cfilter);
263   } else {
264     sprintf(string_buff, "Capture filter: none");
265   }
266   add_string_to_box(string_buff, capture_box);
267 #endif
268
269   gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
270   gtk_widget_show(sum_open_w);
271 }
272
273 /* this is never called 
274 void
275 summary_prep_close_cb(GtkWidget *w, gpointer win) {
276
277   gtk_grab_remove(GTK_WIDGET(win));
278   gtk_widget_destroy(GTK_WIDGET(win));
279 } */