Added support for compiling on win32 with Visual C and 'nmake'. It compiles,
[obnox/wireshark/wip.git] / summary.c
1 /* summary.c
2  * Routines for capture file summary window
3  *
4  * $Id: summary.c,v 1.5 1999/07/13 02:52:59 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 "etypes.h"
65 #include "util.h"
66 #include "prefs.h"
67
68 extern capture_file  cf;
69
70 /* File selection data keys */
71 #define E_SUM_PREP_FS_KEY "sum_prep_fs"
72 #define E_SUM_PREP_TE_KEY "sum_prep_te"
73
74 /* Summary callback data keys */
75 #define E_SUM_IFACE_KEY "sum_iface"
76 #define E_SUM_FILT_KEY  "sum_filter"
77 #define E_SUM_COUNT_KEY "sum_count"
78 #define E_SUM_OPEN_KEY  "sum_open"
79 #define E_SUM_SNAP_KEY  "sum_snap"
80
81 #define SUM_STR_MAX 1024
82
83 /* Summary filter key */
84 #define E_SUM_FILT_TE_KEY "sum_filt_te"
85
86 char * string_for_format(guint16 cd_t){
87   switch (cd_t) {
88   case WTAP_FILE_WTAP:
89     return "wiretap";
90   case WTAP_FILE_PCAP:
91     return "pcap";
92   case WTAP_FILE_LANALYZER:
93     return "LanAlyzer";
94   case WTAP_FILE_NGSNIFFER:
95     return "Sniffer";
96   case WTAP_FILE_SNOOP:
97     return "snoop";
98   case WTAP_FILE_IPTRACE:
99     return "iptrace";
100   case WTAP_FILE_NETMON:
101     return "Network Monitor";
102   case WTAP_FILE_NETXRAY:
103     return "NetXray/Sniffer Pro";
104   default:
105     return "unknown";
106   }
107 }
108
109 double
110 secs_usecs( guint32 s, guint32 us) {
111   return (us / 1000000.0) + (double)s;
112 }
113
114 void
115 tally_frame_data(gpointer cf, gpointer st) {
116   double cur_time;
117   summary_tally * sum_tally = (summary_tally *)st;
118   frame_data *cur_frame = (frame_data *)cf;
119
120   cur_time = secs_usecs(cur_frame->abs_secs, cur_frame->abs_usecs);
121     if (cur_time < sum_tally->start_time) {
122       sum_tally->start_time = cur_time;
123     }
124     if (cur_time > sum_tally->stop_time){
125     sum_tally->stop_time = cur_time;
126   }
127   sum_tally->bytes += cur_frame->pkt_len;
128   if (cur_frame->passed_dfilter)
129           sum_tally->filtered_count++;
130 }
131
132 void
133 add_string_to_box(gchar *str, GtkWidget *box) {
134   GtkWidget *lb;
135   lb = gtk_label_new(str);
136   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
137   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
138   gtk_widget_show(lb);
139 }
140
141 void
142 summary_prep_cb(GtkWidget *w, gpointer d) {
143   frame_data    *first_frame, *cur_frame;
144   summary_tally *st;
145   GtkWidget     *sum_open_w,
146                 *main_vb, *file_fr, *data_fr, *capture_fr, *file_box, 
147 *data_box,
148                 *capture_box;
149
150  gchar          string_buff[SUM_STR_MAX];
151
152  guint32        traffic_bytes, i;
153  double         seconds;
154  GList          *cur_glist;
155
156  /* initialize the tally */
157   first_frame = (frame_data *)(cf.plist->data);
158   st = (summary_tally *)g_malloc(sizeof(summary_tally));
159   st->start_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
160 ;
161   st->stop_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
162 ;
163   st->bytes = 0;
164   st->filtered_count = 0;
165   cur_glist = cf.plist;
166
167   for (i = 0; i < cf.count; i++){
168     cur_frame = (frame_data *)cur_glist->data;
169     tally_frame_data(cur_frame, st);
170     cur_glist = cur_glist->next;
171     }
172
173   /*  g_list_foreach(cf.plist_first, (GFunc)tally_frame_data, st); */
174
175   /* traffic_bytes will be computed here */
176   traffic_bytes = st->bytes;
177   seconds = st->stop_time - st->start_time;
178   sum_open_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
179   gtk_window_set_title(GTK_WINDOW(sum_open_w), "Ethereal: Summary");
180
181   /* Container for each row of widgets */
182   main_vb = gtk_vbox_new(FALSE, 3);
183   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
184   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
185   gtk_widget_show(main_vb);
186
187   /* File frame */
188   file_fr = gtk_frame_new("File");
189   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
190   gtk_widget_show(file_fr);
191
192   file_box = gtk_vbox_new(FALSE, 3);
193   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
194   gtk_widget_show(file_box);
195
196   /* filename */
197   snprintf(string_buff, SUM_STR_MAX, "Name: %s", cf.filename);
198   add_string_to_box(string_buff, file_box);
199
200   /* length */
201   snprintf(string_buff, SUM_STR_MAX, "Length: %lu", cf.f_len);
202   add_string_to_box(string_buff, file_box);
203
204   /* format */
205   snprintf(string_buff, SUM_STR_MAX, "Format: %s", 
206 string_for_format(cf.cd_t));
207   add_string_to_box(string_buff, file_box);
208
209   /* Data frame */
210   data_fr = gtk_frame_new("Data");
211   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
212   gtk_widget_show(data_fr);
213
214   data_box = gtk_vbox_new(FALSE, 3);
215   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
216   gtk_widget_show(data_box);
217
218   /* seconds */
219   snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", 
220 secs_usecs(cf.esec,cf.eusec));
221   add_string_to_box(string_buff, data_box);
222
223   snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
224   add_string_to_box(string_buff, data_box);
225
226   /* Packet count */
227   snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", cf.count);
228   add_string_to_box(string_buff, data_box);
229
230   /* Filtered Packet count */
231   snprintf(string_buff, SUM_STR_MAX, "Filtered packet count: %i", st->filtered_count);
232   add_string_to_box(string_buff, data_box);
233
234   /* Packets per second */
235   if (seconds > 0){
236     snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", 
237 cf.count/seconds);
238     add_string_to_box(string_buff, data_box);
239   }
240
241   /* Dropped count */
242   snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %i", cf.drops);
243   add_string_to_box(string_buff, data_box);
244
245   /* Byte count */
246   snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", 
247 traffic_bytes);
248   add_string_to_box(string_buff, data_box);
249
250   /* Bytes per second */
251   if (seconds > 0){
252     snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", 
253 traffic_bytes/seconds);
254     add_string_to_box(string_buff, data_box);
255   }
256
257   /* Capture frame */
258   capture_fr = gtk_frame_new("Capture");
259   gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
260   gtk_widget_show(capture_fr);
261
262   capture_box = gtk_vbox_new(FALSE, 3);
263   gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
264   gtk_widget_show(capture_box);
265
266
267   /* interface */
268   if (cf.iface) {
269     snprintf(string_buff, SUM_STR_MAX, "Interface: %s", cf.iface);
270   } else {
271     sprintf(string_buff, "Interface: unknown");
272   }
273   add_string_to_box(string_buff, capture_box);
274
275   /* Display filter. The situation where cf.dfilter="" and cf.dfcode=NULL can exist,
276         so I'll check for both */
277   if (cf.dfilter && cf.dfcode) {
278     snprintf(string_buff, SUM_STR_MAX, "Display filter: %s", cf.dfilter);
279   } else {
280     sprintf(string_buff, "Display filter: none");
281   }
282   add_string_to_box(string_buff, capture_box);
283
284 #ifdef HAVE_LIBPCAP
285   /* Capture filter */
286   if (cf.cfilter) {
287     snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", cf.cfilter);
288   } else {
289     sprintf(string_buff, "Capture filter: none");
290   }
291   add_string_to_box(string_buff, capture_box);
292 #endif
293
294   gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
295   gtk_widget_show(sum_open_w);
296 }
297
298
299 void
300 summary_prep_close_cb(GtkWidget *w, gpointer win) {
301
302 #ifdef GTK_HAVE_FEATURES_1_1_0
303   win = w;
304 #endif
305   gtk_grab_remove(GTK_WIDGET(win));
306   gtk_widget_destroy(GTK_WIDGET(win));
307 }