In GTK+ 1.0[.x], "gtk_window_set_position()" was called
[obnox/wireshark/wip.git] / summary.c
1 /* summary.c
2  * Routines for capture file summary window
3  *
4  * $Id: summary.c,v 1.2 1999/07/04 06:41:19 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 #include <pcap.h>
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #ifdef NEED_SNPRINTF_H
44 # ifdef HAVE_STDARG_H
45 #  include <stdarg.h>
46 # else
47 #  include <varargs.h>
48 # endif
49 # include "snprintf.h"
50 #endif
51
52 #ifdef HAVE_SYS_SOCKIO_H
53 # include <sys/sockio.h>
54 #endif
55
56 #include "ethereal.h"
57 #include "packet.h"
58 #include "file.h"
59 #include "menu.h"
60 #include "summary.h"
61 #include "capture.h"
62 #include "etypes.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 char * string_for_format(guint16 cd_t){
85   switch (cd_t) {
86 #ifdef WITH_WIRETAP
87   case WTAP_FILE_WTAP:
88     return "wiretap";
89   case WTAP_FILE_PCAP:
90     return "pcap";
91   case WTAP_FILE_LANALYZER:
92     return "LanAlyzer";
93   case WTAP_FILE_NGSNIFFER:
94     return "Sniffer";
95   case WTAP_FILE_SNOOP:
96     return "snoop";
97   case WTAP_FILE_IPTRACE:
98     return "iptrace";
99   case WTAP_FILE_NETMON:
100     return "Network Monitor";
101   case WTAP_FILE_NETXRAY:
102     return "NetXray/Sniffer Pro";
103 #else
104   case CD_WIRE:
105     return "wiretap";
106   case CD_SNOOP:
107     return "snoop";
108   case CD_PCAP_BE:
109     return "pcap-be";
110   case CD_PCAP_LE:
111     return "pcap-le";
112   case CD_NA_UNCOMPR:
113     return "network-associates";
114 #endif
115   default:
116     return "unknown";
117   }
118 }
119
120 double
121 secs_usecs( guint32 s, guint32 us) {
122   return (us / 1000000.0) + (double)s;
123 }
124
125 void
126 tally_frame_data(gpointer cf, gpointer st) {
127   double cur_time;
128   summary_tally * sum_tally = (summary_tally *)st;
129   frame_data *cur_frame = (frame_data *)cf;
130
131   cur_time = secs_usecs(cur_frame->abs_secs, cur_frame->abs_usecs);
132     if (cur_time < sum_tally->start_time) {
133       sum_tally->start_time = cur_time;
134     }
135     if (cur_time > sum_tally->stop_time){
136     sum_tally->stop_time = cur_time;
137   }
138   sum_tally->bytes += cur_frame->pkt_len;
139   sum_tally->count++;
140 }
141
142 void
143 add_string_to_box(gchar *str, GtkWidget *box) {
144   GtkWidget *lb;
145   lb = gtk_label_new(str);
146   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
147   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
148   gtk_widget_show(lb);
149 }
150
151 void
152 summary_prep_cb(GtkWidget *w, gpointer d) {
153   frame_data    *first_frame, *cur_frame;
154   summary_tally *st;
155   GtkWidget     *sum_open_w,
156                 *main_vb, *file_fr, *data_fr, *capture_fr, *file_box, 
157 *data_box,
158                 *capture_box;
159
160  gchar          string_buff[SUM_STR_MAX];
161
162  guint32        traffic_bytes, i;
163  double         seconds;
164  GList          *cur_glist;
165
166  /* initialize the tally */
167   first_frame = (frame_data *)(cf.plist->data);
168   st = (summary_tally *)g_malloc(sizeof(summary_tally));
169   st->start_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
170 ;
171   st->stop_time = secs_usecs(first_frame->abs_secs,first_frame->abs_usecs) 
172 ;
173   st->bytes = 0;
174   st->count = 0;
175   cur_glist = cf.plist;
176
177   for (i = 0; i < cf.count; i++){
178     cur_frame = (frame_data *)cur_glist->data;
179     tally_frame_data(cur_frame, st);
180     cur_glist = cur_glist->next;
181     }
182
183   /*  g_list_foreach(cf.plist_first, (GFunc)tally_frame_data, st); */
184
185   /* traffic_bytes will be computed here */
186   traffic_bytes = st->bytes;
187   seconds = st->stop_time - st->start_time;
188   sum_open_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
189   gtk_window_set_title(GTK_WINDOW(sum_open_w), "Ethereal: Summary");
190
191   /* Container for each row of widgets */
192   main_vb = gtk_vbox_new(FALSE, 3);
193   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
194   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
195   gtk_widget_show(main_vb);
196
197   /* File frame */
198   file_fr = gtk_frame_new("File");
199   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
200   gtk_widget_show(file_fr);
201
202   file_box = gtk_vbox_new(FALSE, 3);
203   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
204   gtk_widget_show(file_box);
205
206   /* filename */
207   snprintf(string_buff, SUM_STR_MAX, "Name: %s", cf.filename);
208   add_string_to_box(string_buff, file_box);
209
210   /* length */
211   snprintf(string_buff, SUM_STR_MAX, "Length: %lu", cf.f_len);
212   add_string_to_box(string_buff, file_box);
213
214   /* format */
215   snprintf(string_buff, SUM_STR_MAX, "Format: %s", 
216 string_for_format(cf.cd_t));
217   add_string_to_box(string_buff, file_box);
218
219   /* Data frame */
220   data_fr = gtk_frame_new("Data");
221   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
222   gtk_widget_show(data_fr);
223
224   data_box = gtk_vbox_new(FALSE, 3);
225   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
226   gtk_widget_show(data_box);
227
228   /* seconds */
229   snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", 
230 secs_usecs(cf.esec,cf.eusec));
231   add_string_to_box(string_buff, data_box);
232
233   snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f 
234 seconds", seconds);
235   add_string_to_box(string_buff, data_box);
236
237   /* Packet count */
238   snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", cf.count);
239   add_string_to_box(string_buff, data_box);
240
241   /* Packets per second */
242   if (seconds > 0){
243     snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", 
244 cf.count/seconds);
245     add_string_to_box(string_buff, data_box);
246   }
247
248   /* Dropped count */
249   snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %i", cf.drops);
250   add_string_to_box(string_buff, data_box);
251
252   /* Byte count */
253   snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", 
254 traffic_bytes);
255   add_string_to_box(string_buff, data_box);
256
257   /* Bytes per second */
258   if (seconds > 0){
259     snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", 
260 traffic_bytes/seconds);
261     add_string_to_box(string_buff, data_box);
262   }
263
264   /* Capture frame */
265   capture_fr = gtk_frame_new("Capture");
266   gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
267   gtk_widget_show(capture_fr);
268
269   capture_box = gtk_vbox_new(FALSE, 3);
270   gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
271   gtk_widget_show(capture_box);
272
273
274   /* interface */
275   if (cf.iface) {
276     snprintf(string_buff, SUM_STR_MAX, "Interface: %s", cf.iface);
277   } else {
278     sprintf(string_buff, "Interface: unknown");
279   }
280   add_string_to_box(string_buff, capture_box);
281
282   /* Display filter */
283   if (cf.dfilter) {
284     snprintf(string_buff, SUM_STR_MAX, "Display filter: %s", cf.dfilter);
285   } else {
286     sprintf(string_buff, "Display filter: none");
287   }
288   add_string_to_box(string_buff, capture_box);
289
290
291   /* Capture filter */
292   if (cf.cfilter) {
293     snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", cf.cfilter);
294   } else {
295     sprintf(string_buff, "Capture filter: none");
296   }
297   add_string_to_box(string_buff, capture_box);
298 #if (GTK_MINOR_VERSION > 1) || ((GTK_MICRO_VERSION > 1) &&  (GTK_MINOR_VERSION > 0))
299   gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
300 #else
301   gtk_window_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
302 #endif
303   gtk_widget_show(sum_open_w);
304 }
305
306
307 void
308 summary_prep_close_cb(GtkWidget *w, gpointer win) {
309
310 #ifdef GTK_HAVE_FEATURES_1_1_0
311   win = w;
312 #endif
313   gtk_grab_remove(GTK_WIDGET(win));
314   gtk_widget_destroy(GTK_WIDGET(win));
315 }