77cd3886a5eb7be34f6782a064fcf0860c1d4f18
[metze/wireshark/wip.git] / summary.c
1 /* summary.c
2  * Routines for capture file summary info
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <config.h>
24
25 #include <wiretap/pcap-encap.h>
26 #include <wiretap/wtap_opttypes.h>
27 #include <wiretap/pcapng.h>
28
29 #include <epan/packet.h>
30 #include "cfile.h"
31 #include "summary.h"
32 #if 0
33 #include "ui/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   frame_data    *first_frame, *cur_frame;
109   guint32        framenum;
110   iface_options iface;
111   guint i;
112   wtapng_iface_descriptions_t* idb_info;
113   wtap_optionblock_t wtapng_if_descr;
114   wtapng_if_descr_mandatory_t *wtapng_if_descr_mand;
115   wtap_optionblock_t if_stats;
116   guint64 isb_ifdrop;
117   char* if_string;
118   wtapng_if_descr_filter_t* if_filter;
119
120   st->packet_count_ts = 0;
121   st->start_time = 0;
122   st->stop_time = 0;
123   st->bytes = 0;
124   st->filtered_count = 0;
125   st->filtered_count_ts = 0;
126   st->filtered_start = 0;
127   st->filtered_stop = 0;
128   st->filtered_bytes = 0;
129   st->marked_count = 0;
130   st->marked_count_ts = 0;
131   st->marked_start = 0;
132   st->marked_stop = 0;
133   st->marked_bytes = 0;
134   st->ignored_count = 0;
135
136   /* initialize the tally */
137   if (cf->count != 0) {
138     first_frame = frame_data_sequence_find(cf->frames, 1);
139     st->start_time = nstime_to_sec(&first_frame->abs_ts);
140     st->stop_time = nstime_to_sec(&first_frame->abs_ts);
141
142     for (framenum = 1; framenum <= cf->count; framenum++) {
143       cur_frame = frame_data_sequence_find(cf->frames, framenum);
144       tally_frame_data(cur_frame, st);
145     }
146   }
147
148   st->filename = cf->filename;
149   st->file_length = cf->f_datalen;
150   st->file_type = cf->cd_t;
151   st->iscompressed = cf->iscompressed;
152   st->is_tempfile = cf->is_tempfile;
153   st->file_encap_type = cf->lnk_t;
154   st->packet_encap_types = cf->linktypes;
155   st->has_snap = cf->has_snap;
156   st->snap = cf->snap;
157   st->elapsed_time = nstime_to_sec(&cf->elapsed_time);
158   st->packet_count = cf->count;
159   st->drops_known = cf->drops_known;
160   st->drops = cf->drops;
161   st->dfilter = cf->dfilter;
162
163   st->ifaces  = g_array_new(FALSE, FALSE, sizeof(iface_options));
164   idb_info = wtap_file_get_idb_info(cf->wth);
165   for (i = 0; i < idb_info->interface_data->len; i++) {
166     wtapng_if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
167     wtapng_if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_optionblock_get_mandatory_data(wtapng_if_descr);
168     wtap_optionblock_get_option_custom(wtapng_if_descr, OPT_IDB_FILTER, (void**)&if_filter);
169     iface.cfilter = g_strdup(if_filter->if_filter_str);
170     wtap_optionblock_get_option_string(wtapng_if_descr, OPT_IDB_NAME, &if_string);
171     iface.name = g_strdup(if_string);
172     wtap_optionblock_get_option_string(wtapng_if_descr, OPT_IDB_DESCR, &if_string);
173     iface.descr = g_strdup(if_string);
174     iface.drops_known = FALSE;
175     iface.drops = 0;
176     iface.snap = wtapng_if_descr_mand->snap_len;
177     iface.has_snap = (iface.snap != 65535);
178     iface.encap_type = wtapng_if_descr_mand->wtap_encap;
179     iface.isb_comment = NULL;
180     if(wtapng_if_descr_mand->num_stat_entries == 1){
181       /* dumpcap only writes one ISB, only handle that for now */
182       if_stats = g_array_index(wtapng_if_descr_mand->interface_statistics, wtap_optionblock_t, 0);
183       wtap_optionblock_get_option_uint64(if_stats, OPT_ISB_IFDROP, &isb_ifdrop);
184       if (isb_ifdrop != G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF)) {
185         iface.drops_known = TRUE;
186         iface.drops = isb_ifdrop;
187       }
188       /* XXX: this doesn't get used, and might need to be g_strdup'ed when it does */
189       wtap_optionblock_get_option_string(if_stats, OPT_COMMENT, &iface.isb_comment);
190     }
191     g_array_append_val(st->ifaces, iface);
192   }
193   g_free(idb_info);
194 }
195
196 #ifdef HAVE_LIBPCAP
197 void
198 summary_fill_in_capture(capture_file *cf,capture_options *capture_opts, summary_tally *st)
199 {
200   iface_options iface;
201   interface_t device;
202   guint i;
203
204   if (st->ifaces->len == 0) {
205     /*
206      * XXX - do this only if we have a live capture.
207      */
208     for (i = 0; i < capture_opts->all_ifaces->len; i++) {
209       device = g_array_index(capture_opts->all_ifaces, interface_t, i);
210       if (!device.selected) {
211         continue;
212       }
213       iface.cfilter = g_strdup(device.cfilter);
214       iface.name = g_strdup(device.name);
215       iface.descr = g_strdup(device.display_name);
216       iface.drops_known = cf->drops_known;
217       iface.drops = cf->drops;
218       iface.has_snap = device.has_snaplen;
219       iface.snap = device.snaplen;
220       iface.encap_type = wtap_pcap_encap_to_wtap_encap(device.active_dlt);
221       g_array_append_val(st->ifaces, iface);
222     }
223   }
224 }
225 #endif
226
227 /*
228  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
229  *
230  * Local Variables:
231  * c-basic-offset: 2
232  * tab-width: 8
233  * indent-tabs-mode: nil
234  * End:
235  *
236  * ex: set shiftwidth=2 tabstop=8 expandtab:
237  * :indentSize=2:tabSize=8:noTabs=true:
238  */