4e440af165e02c1db53835edbb5c3f722918c29c
[obnox/wireshark/wip.git] / summary.c
1 /* summary.c
2  * Routines for capture file summary window
3  *
4  * $Id: summary.c,v 1.10 1999/08/24 17:26:16 gram 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 "ethereal.h"
59 #include "packet.h"
60 #include "file.h"
61 #include "menu.h"
62 #include "summary.h"
63 #include "capture.h"
64 #include "util.h"
65 #include "prefs.h"
66
67 extern capture_file  cf;
68
69 /* File selection data keys */
70 #define E_SUM_PREP_FS_KEY "sum_prep_fs"
71 #define E_SUM_PREP_TE_KEY "sum_prep_te"
72
73 /* Summary callback data keys */
74 #define E_SUM_IFACE_KEY "sum_iface"
75 #define E_SUM_FILT_KEY  "sum_filter"
76 #define E_SUM_COUNT_KEY "sum_count"
77 #define E_SUM_OPEN_KEY  "sum_open"
78 #define E_SUM_SNAP_KEY  "sum_snap"
79
80 #define SUM_STR_MAX 1024
81
82 /* Summary filter key */
83 #define E_SUM_FILT_TE_KEY "sum_filt_te"
84
85 double
86 secs_usecs( guint32 s, guint32 us) {
87   return (us / 1000000.0) + (double)s;
88 }
89
90 void
91 tally_frame_data(gpointer cf, gpointer st) {
92   double cur_time;
93   summary_tally * sum_tally = (summary_tally *)st;
94   frame_data *cur_frame = (frame_data *)cf;
95
96   cur_time = secs_usecs(cur_frame->abs_secs, cur_frame->abs_usecs);
97     if (cur_time < sum_tally->start_time) {
98       sum_tally->start_time = cur_time;
99     }
100     if (cur_time > sum_tally->stop_time){
101     sum_tally->stop_time = cur_time;
102   }
103   sum_tally->bytes += cur_frame->pkt_len;
104   if (cur_frame->passed_dfilter)
105           sum_tally->filtered_count++;
106 }
107
108 void
109 add_string_to_box(gchar *str, GtkWidget *box) {
110   GtkWidget *lb;
111   lb = gtk_label_new(str);
112   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
113   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
114   gtk_widget_show(lb);
115 }
116
117 void
118 summary_prep_cb(GtkWidget *w, gpointer d) {
119   frame_data    *first_frame, *cur_frame;
120   summary_tally *st;
121   GtkWidget     *sum_open_w,
122                 *main_vb, *file_fr, *data_fr, *capture_fr, *file_box, 
123 *data_box,
124                 *capture_box;
125
126  gchar          string_buff[SUM_STR_MAX];
127
128  guint32        traffic_bytes, i;
129  double         seconds;
130  frame_data    *cur_glist;
131
132  /* initialize the tally */
133   first_frame = cf.plist;
134   st = (summary_tally *)g_malloc(sizeof(summary_tally));
135   st->start_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
136 ;
137   st->stop_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
138 ;
139   st->bytes = 0;
140   st->filtered_count = 0;
141   cur_glist = cf.plist;
142
143   for (i = 0; i < cf.count; i++){
144     cur_frame = cur_glist;
145     tally_frame_data(cur_frame, st);
146     cur_glist = cur_glist->next;
147   }
148
149   /* traffic_bytes will be computed here */
150   traffic_bytes = st->bytes;
151   seconds = st->stop_time - st->start_time;
152   sum_open_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
153   gtk_window_set_title(GTK_WINDOW(sum_open_w), "Ethereal: Summary");
154
155   /* Container for each row of widgets */
156   main_vb = gtk_vbox_new(FALSE, 3);
157   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
158   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
159   gtk_widget_show(main_vb);
160
161   /* File frame */
162   file_fr = gtk_frame_new("File");
163   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
164   gtk_widget_show(file_fr);
165
166   file_box = gtk_vbox_new(FALSE, 3);
167   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
168   gtk_widget_show(file_box);
169
170   /* filename */
171   snprintf(string_buff, SUM_STR_MAX, "Name: %s", cf.filename);
172   add_string_to_box(string_buff, file_box);
173
174   /* length */
175   snprintf(string_buff, SUM_STR_MAX, "Length: %lu", cf.f_len);
176   add_string_to_box(string_buff, file_box);
177
178   /* format */
179   snprintf(string_buff, SUM_STR_MAX, "Format: %s", cf.cd_t_desc);
180   add_string_to_box(string_buff, file_box);
181
182   /* snapshot length */
183   snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", cf.snap);
184   add_string_to_box(string_buff, file_box);
185
186   /* Data frame */
187   data_fr = gtk_frame_new("Data");
188   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
189   gtk_widget_show(data_fr);
190
191   data_box = gtk_vbox_new(FALSE, 3);
192   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
193   gtk_widget_show(data_box);
194
195   /* seconds */
196   snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", 
197 secs_usecs(cf.esec,cf.eusec));
198   add_string_to_box(string_buff, data_box);
199
200   snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
201   add_string_to_box(string_buff, data_box);
202
203   /* Packet count */
204   snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", cf.count);
205   add_string_to_box(string_buff, data_box);
206
207   /* Filtered Packet count */
208   snprintf(string_buff, SUM_STR_MAX, "Filtered packet count: %i", st->filtered_count);
209   add_string_to_box(string_buff, data_box);
210
211   /* Packets per second */
212   if (seconds > 0){
213     snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", 
214 cf.count/seconds);
215     add_string_to_box(string_buff, data_box);
216   }
217
218   /* Dropped count */
219   snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %i", cf.drops);
220   add_string_to_box(string_buff, data_box);
221
222   /* Byte count */
223   snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", 
224 traffic_bytes);
225   add_string_to_box(string_buff, data_box);
226
227   /* Bytes per second */
228   if (seconds > 0){
229     snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", 
230 traffic_bytes/seconds);
231     add_string_to_box(string_buff, data_box);
232   }
233
234   /* Capture frame */
235   capture_fr = gtk_frame_new("Capture");
236   gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
237   gtk_widget_show(capture_fr);
238
239   capture_box = gtk_vbox_new(FALSE, 3);
240   gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
241   gtk_widget_show(capture_box);
242
243
244   /* interface */
245   if (cf.iface) {
246     snprintf(string_buff, SUM_STR_MAX, "Interface: %s", cf.iface);
247   } else {
248     sprintf(string_buff, "Interface: unknown");
249   }
250   add_string_to_box(string_buff, capture_box);
251
252   /* Display filter */
253   if (DFILTER_CONTAINS_FILTER(cf.dfcode)) {
254     snprintf(string_buff, SUM_STR_MAX, "Display filter: %s", cf.dfcode->dftext);
255   } else {
256     sprintf(string_buff, "Display filter: none");
257   }
258   add_string_to_box(string_buff, capture_box);
259
260 #ifdef HAVE_LIBPCAP
261   /* Capture filter */
262   if (cf.cfilter) {
263     snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", cf.cfilter);
264   } else {
265     sprintf(string_buff, "Capture filter: none");
266   }
267   add_string_to_box(string_buff, capture_box);
268 #endif
269
270   gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
271   gtk_widget_show(sum_open_w);
272 }
273
274 /* this is never called 
275 void
276 summary_prep_close_cb(GtkWidget *w, gpointer win) {
277
278   gtk_grab_remove(GTK_WIDGET(win));
279   gtk_widget_destroy(GTK_WIDGET(win));
280 } */