8b6512719aaf508b7efde0d05ddb596334a7ecd3
[metze/wireshark/wip.git] / summary.c
1 /* summary.c
2  * Routines for capture file summary info
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <epan/packet.h>
30 #include "cfile.h"
31 #include "summary.h"
32 #ifdef HAVE_LIBPCAP
33 #include "capture_ui_utils.h"
34 #endif
35
36
37 static void
38 tally_frame_data(frame_data *cur_frame, summary_tally *sum_tally)
39 {
40   double cur_time;
41
42   sum_tally->bytes += cur_frame->pkt_len;
43   if (cur_frame->flags.passed_dfilter){
44     sum_tally->filtered_count++;
45     sum_tally->filtered_bytes += cur_frame->pkt_len;
46   }
47   if (cur_frame->flags.marked){
48     sum_tally->marked_count++;
49     sum_tally->marked_bytes += cur_frame->pkt_len;
50   }
51   if (cur_frame->flags.ignored){
52     sum_tally->ignored_count++;
53   }
54
55   if (cur_frame->flags.has_ts) {
56     /* This packet has a time stamp. */
57     cur_time = nstime_to_sec(&cur_frame->abs_ts);
58
59     sum_tally->packet_count_ts++;
60     if (cur_time < sum_tally->start_time) {
61       sum_tally->start_time = cur_time;
62     }
63     if (cur_time > sum_tally->stop_time){
64       sum_tally->stop_time = cur_time;
65     }
66     if (cur_frame->flags.passed_dfilter){
67       sum_tally->filtered_count_ts++;
68       /*
69        * If we've seen one filtered packet, this is the first
70        * one.
71        */
72       if (sum_tally->filtered_count == 1){
73         sum_tally->filtered_start= cur_time;
74         sum_tally->filtered_stop = cur_time;
75       } else {
76         if (cur_time < sum_tally->filtered_start) {
77           sum_tally->filtered_start = cur_time;
78         }
79         if (cur_time > sum_tally->filtered_stop) {
80           sum_tally->filtered_stop = cur_time;
81         }
82       }
83     }
84     if (cur_frame->flags.marked){
85       sum_tally->marked_count_ts++;
86       /*
87        * If we've seen one marked packet, this is the first
88        * one.
89        */
90       if (sum_tally->marked_count == 1){
91         sum_tally->marked_start= cur_time;
92         sum_tally->marked_stop = cur_time;
93       } else {
94         if (cur_time < sum_tally->marked_start) {
95           sum_tally->marked_start = cur_time;
96         }
97         if (cur_time > sum_tally->marked_stop) {
98           sum_tally->marked_stop = cur_time;
99         }
100       }
101     }
102   }
103 }
104
105 void
106 summary_fill_in(capture_file *cf, summary_tally *st)
107 {
108
109   frame_data    *first_frame, *cur_frame;
110   guint32        framenum;
111   wtapng_section_t* shb_inf;
112
113   st->packet_count_ts = 0;
114   st->start_time = 0;
115   st->stop_time = 0;
116   st->bytes = 0;
117   st->filtered_count = 0;
118   st->filtered_count_ts = 0;
119   st->filtered_start = 0;
120   st->filtered_stop = 0;
121   st->filtered_bytes = 0;
122   st->marked_count = 0;
123   st->marked_count_ts = 0;
124   st->marked_start = 0;
125   st->marked_stop = 0;
126   st->marked_bytes = 0;
127   st->ignored_count = 0;
128
129   /* initialize the tally */
130   if (cf->count != 0) {
131     first_frame = frame_data_sequence_find(cf->frames, 1);
132     st->start_time = nstime_to_sec(&first_frame->abs_ts);
133     st->stop_time = nstime_to_sec(&first_frame->abs_ts);
134
135     for (framenum = 1; framenum <= cf->count; framenum++) {
136       cur_frame = frame_data_sequence_find(cf->frames, framenum);
137       tally_frame_data(cur_frame, st);
138     }
139   }
140
141   st->filename = cf->filename;
142   st->file_length = cf->f_datalen;
143   st->file_type = cf->cd_t;
144   st->iscompressed = cf->iscompressed;
145   st->is_tempfile = cf->is_tempfile;
146   st->encap_type = cf->lnk_t;
147   st->has_snap = cf->has_snap;
148   st->snap = cf->snap;
149   st->elapsed_time = nstime_to_sec(&cf->elapsed_time);
150   st->packet_count = cf->count;
151   st->drops_known = cf->drops_known;
152   st->drops = cf->drops;
153   st->dfilter = cf->dfilter;
154
155   /* Get info from SHB */
156   shb_inf = wtap_file_get_shb_info(cf->wth);
157   if(shb_inf == NULL){
158     st->opt_comment    = NULL;
159     st->shb_hardware   = NULL;
160     st->shb_os         = NULL;
161     st->shb_user_appl  = NULL;
162   }else{
163     st->opt_comment    = shb_inf->opt_comment;
164     st->shb_hardware   = shb_inf->shb_hardware;
165     st->shb_os         = shb_inf->shb_os;
166     st->shb_user_appl  = shb_inf->shb_user_appl;
167     g_free(shb_inf);
168   }
169
170   st->ifaces  = g_array_new(FALSE, FALSE, sizeof(iface_options));
171 }
172
173
174 #ifdef HAVE_LIBPCAP
175 void
176 summary_fill_in_capture(capture_file *cf,capture_options *capture_opts, summary_tally *st)
177 {
178   iface_options iface;
179   interface_t device;
180   guint i;
181   wtapng_iface_descriptions_t* idb_info;
182   wtapng_if_descr_t wtapng_if_descr;
183   wtapng_if_stats_t *if_stats;
184
185   while (st->ifaces->len > 0) {
186     iface = g_array_index(st->ifaces, iface_options, 0);
187     st->ifaces = g_array_remove_index(st->ifaces, 0);
188     g_free(iface.name);
189     g_free(iface.descr);
190     g_free(iface.cfilter);
191   }
192   if (st->is_tempfile) {
193     for (i = 0; i < capture_opts->all_ifaces->len; i++) {
194       device = g_array_index(capture_opts->all_ifaces, interface_t, i);
195       if (!device.selected) {
196         continue;
197       }
198       iface.cfilter = g_strdup(device.cfilter);
199       iface.name = g_strdup(device.name);
200       iface.descr = g_strdup(device.display_name);
201       iface.drops_known = cf->drops_known;
202       iface.drops = cf->drops;
203       iface.has_snap = device.has_snaplen;
204       iface.snap = device.snaplen;
205       iface.linktype = device.active_dlt;
206       g_array_append_val(st->ifaces, iface);
207     }
208   } else {
209     idb_info = wtap_file_get_idb_info(cf->wth);
210     for (i = 0; i < idb_info->number_of_interfaces; i++) {
211       wtapng_if_descr = g_array_index(idb_info->interface_data, wtapng_if_descr_t, i);
212       iface.cfilter = g_strdup(wtapng_if_descr.if_filter_str);
213       iface.name = g_strdup(wtapng_if_descr.if_name);
214       iface.descr = g_strdup(wtapng_if_descr.if_description);
215       iface.drops_known = FALSE;
216       iface.drops = 0;
217       iface.snap = wtapng_if_descr.snap_len;
218       iface.has_snap = (iface.snap != 65535);
219       iface.linktype = wtapng_if_descr.link_type;
220       if(wtapng_if_descr.num_stat_entries == 1){
221           /* dumpcap only writes one ISB, only handle that for now */
222           if_stats = &g_array_index(wtapng_if_descr.interface_statistics, wtapng_if_stats_t, 0);
223           iface.drops_known = TRUE;
224           iface.drops = if_stats->isb_ifdrop;
225           iface.isb_comment = if_stats->opt_comment;
226       }
227       g_array_append_val(st->ifaces, iface);
228     }
229     g_free(idb_info);
230   }
231 }
232 #endif