Ensure to have a valid string pointer when writing OS SHB option
[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_block_t wtapng_if_descr;
114   wtapng_if_descr_mandatory_t *wtapng_if_descr_mand;
115   wtap_block_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_block_t, i);
167     wtapng_if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(wtapng_if_descr);
168     if (wtap_block_get_custom_option_value(wtapng_if_descr, OPT_IDB_FILTER, (void**)&if_filter) == WTAP_OPTTYPE_SUCCESS) {
169       iface.cfilter = g_strdup(if_filter->if_filter_str);
170     } else {
171       iface.cfilter = NULL;
172     }
173     if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_NAME, &if_string) == WTAP_OPTTYPE_SUCCESS) {
174       iface.name = g_strdup(if_string);
175     } else {
176       iface.name = NULL;
177     }
178     if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_DESCR, &if_string) == WTAP_OPTTYPE_SUCCESS) {
179       iface.descr = g_strdup(if_string);
180     } else {
181       iface.descr = NULL;
182     }
183     iface.drops_known = FALSE;
184     iface.drops = 0;
185     iface.snap = wtapng_if_descr_mand->snap_len;
186     iface.has_snap = (iface.snap != 65535);
187     iface.encap_type = wtapng_if_descr_mand->wtap_encap;
188     iface.isb_comment = NULL;
189     if(wtapng_if_descr_mand->num_stat_entries == 1){
190       /* dumpcap only writes one ISB, only handle that for now */
191       if_stats = g_array_index(wtapng_if_descr_mand->interface_statistics, wtap_block_t, 0);
192       if (wtap_block_get_uint64_option_value(if_stats, OPT_ISB_IFDROP, &isb_ifdrop) == WTAP_OPTTYPE_SUCCESS) {
193         iface.drops_known = TRUE;
194         iface.drops = isb_ifdrop;
195       }
196       /* XXX: this doesn't get used, and might need to be g_strdup'ed when it does */
197       /* XXX - support multiple comments */
198       if (wtap_block_get_nth_string_option_value(if_stats, OPT_COMMENT, 0, &iface.isb_comment) != WTAP_OPTTYPE_SUCCESS) {
199         iface.isb_comment = NULL;
200       }
201     }
202     g_array_append_val(st->ifaces, iface);
203   }
204   g_free(idb_info);
205 }
206
207 #ifdef HAVE_LIBPCAP
208 void
209 summary_fill_in_capture(capture_file *cf,capture_options *capture_opts, summary_tally *st)
210 {
211   iface_options iface;
212   interface_t device;
213   guint i;
214
215   if (st->ifaces->len == 0) {
216     /*
217      * XXX - do this only if we have a live capture.
218      */
219     for (i = 0; i < capture_opts->all_ifaces->len; i++) {
220       device = g_array_index(capture_opts->all_ifaces, interface_t, i);
221       if (!device.selected) {
222         continue;
223       }
224       iface.cfilter = g_strdup(device.cfilter);
225       iface.name = g_strdup(device.name);
226       iface.descr = g_strdup(device.display_name);
227       iface.drops_known = cf->drops_known;
228       iface.drops = cf->drops;
229       iface.has_snap = device.has_snaplen;
230       iface.snap = device.snaplen;
231       iface.encap_type = wtap_pcap_encap_to_wtap_encap(device.active_dlt);
232       g_array_append_val(st->ifaces, iface);
233     }
234   }
235 }
236 #endif
237
238 /*
239  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
240  *
241  * Local Variables:
242  * c-basic-offset: 2
243  * tab-width: 8
244  * indent-tabs-mode: nil
245  * End:
246  *
247  * ex: set shiftwidth=2 tabstop=8 expandtab:
248  * :indentSize=2:tabSize=8:noTabs=true:
249  */