2 * Packet range routines (save, print, ...)
6 * Dick Gooris <gooris@lucent.com>
7 * Ulf Lamping <ulf.lamping@web.de>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include <epan/frame_data.h>
42 #include "packet-range.h"
44 /* (re-)calculate the packet counts (except the user specified range) */
45 static void packet_range_calc(packet_range_t *range) {
49 guint32 displayed_mark_low;
50 guint32 displayed_mark_high;
54 range->selected_packet = 0L;
58 range->mark_range_cnt = 0L;
59 range->ignored_cnt = 0L;
60 range->ignored_marked_cnt = 0L;
61 range->ignored_mark_range_cnt = 0L;
62 range->ignored_user_range_cnt = 0L;
64 displayed_mark_low = 0L;
65 displayed_mark_high = 0L;
66 range->displayed_cnt = 0L;
67 range->displayed_marked_cnt = 0L;
68 range->displayed_mark_range_cnt=0L;
69 range->displayed_ignored_cnt = 0L;
70 range->displayed_ignored_marked_cnt = 0L;
71 range->displayed_ignored_mark_range_cnt = 0L;
72 range->displayed_ignored_user_range_cnt = 0L;
74 /* This doesn't work unless you have a full set of frame_data
75 * structures for all packets in the capture, which is not,
76 * for example, the case when TShark is doing a one-pass
77 * read of a file or a live capture.
79 if (cfile.frames != NULL) {
80 /* The next for-loop is used to obtain the amount of packets
81 * to be processed and is used to present the information in
82 * the Save/Print As widget.
83 * We have different types of ranges: All the packets, the number
84 * of packets of a marked range, a single packet, and a user specified
85 * packet range. The last one is not calculated here since this
86 * data must be entered in the widget by the user.
89 for(framenum = 1; framenum <= cfile.count; framenum++) {
90 packet = frame_data_sequence_find(cfile.frames, framenum);
92 if (cfile.current_frame == packet) {
93 range->selected_packet = framenum;
95 if (packet->flags.passed_dfilter) {
96 range->displayed_cnt++;
98 if (packet->flags.marked) {
99 if (packet->flags.ignored) {
100 range->ignored_marked_cnt++;
102 if (packet->flags.passed_dfilter) {
103 range->displayed_marked_cnt++;
104 if (packet->flags.ignored) {
105 range->displayed_ignored_marked_cnt++;
107 if (displayed_mark_low == 0) {
108 displayed_mark_low = framenum;
110 if (framenum > displayed_mark_high) {
111 displayed_mark_high = framenum;
118 if (framenum > mark_high) {
119 mark_high = framenum;
122 if (packet->flags.ignored) {
123 range->ignored_cnt++;
124 if (packet->flags.passed_dfilter) {
125 range->displayed_ignored_cnt++;
130 for(framenum = 1; framenum <= cfile.count; framenum++) {
131 packet = frame_data_sequence_find(cfile.frames, framenum);
133 if (framenum >= mark_low &&
134 framenum <= mark_high)
136 range->mark_range_cnt++;
137 if (packet->flags.ignored) {
138 range->ignored_mark_range_cnt++;
142 if (framenum >= displayed_mark_low &&
143 framenum <= displayed_mark_high)
145 if (packet->flags.passed_dfilter) {
146 range->displayed_mark_range_cnt++;
147 if (packet->flags.ignored) {
148 range->displayed_ignored_mark_range_cnt++;
155 /* in case we marked just one packet, we add 1. */
156 if (cfile.marked_count != 0) {
157 range->mark_range = mark_high - mark_low + 1;
160 /* in case we marked just one packet, we add 1. */
161 if (range->displayed_marked_cnt != 0) {
162 range->displayed_mark_range = displayed_mark_high - displayed_mark_low + 1;
169 /* (re-)calculate the user specified packet range counts */
170 static void packet_range_calc_user(packet_range_t *range) {
174 range->user_range_cnt = 0L;
175 range->ignored_user_range_cnt = 0L;
176 range->displayed_user_range_cnt = 0L;
177 range->displayed_ignored_user_range_cnt = 0L;
179 /* This doesn't work unless you have a full set of frame_data
180 * structures for all packets in the capture, which is not,
181 * for example, the case when TShark is doing a one-pass
182 * read of a file or a live capture.
184 if (cfile.frames != NULL) {
185 for(framenum = 1; framenum <= cfile.count; framenum++) {
186 packet = frame_data_sequence_find(cfile.frames, framenum);
188 if (value_is_in_range(range->user_range, framenum)) {
189 range->user_range_cnt++;
190 if (packet->flags.ignored) {
191 range->ignored_user_range_cnt++;
193 if (packet->flags.passed_dfilter) {
194 range->displayed_user_range_cnt++;
195 if (packet->flags.ignored) {
196 range->displayed_ignored_user_range_cnt++;
205 /* init the range struct */
206 void packet_range_init(packet_range_t *range) {
208 range->process = range_process_all;
209 range->process_filtered = FALSE;
210 range->remove_ignored = FALSE;
211 range->user_range = range_empty();
213 /* calculate all packet range counters */
214 packet_range_calc(range);
215 packet_range_calc_user(range);
218 /* check whether the packet range is OK */
219 convert_ret_t packet_range_check(packet_range_t *range) {
220 if (range->process == range_process_user_range && range->user_range == NULL) {
221 /* Not valid - return the error. */
222 return range->user_range_status;
227 /* init the processing run */
228 void packet_range_process_init(packet_range_t *range) {
229 /* Check that, if an explicit range was selected, it's valid. */
230 /* "enumeration" values */
231 range->marked_range_active = FALSE;
232 range->selected_done = FALSE;
234 if (range->process_filtered == FALSE) {
235 range->marked_range_left = range->mark_range_cnt;
237 range->marked_range_left = range->displayed_mark_range_cnt;
241 /* do we have to process all packets? */
242 gboolean packet_range_process_all(packet_range_t *range) {
243 return range->process == range_process_all && !range->process_filtered && !range->remove_ignored;
246 /* do we have to process this packet? */
247 range_process_e packet_range_process_packet(packet_range_t *range, frame_data *fdata) {
249 if (range->remove_ignored && fdata->flags.ignored) {
250 return range_process_next;
253 switch(range->process) {
254 case(range_process_all):
256 case(range_process_selected):
257 if (range->selected_done) {
258 return range_processing_finished;
260 if (fdata->num != cfile.current_frame->num) {
261 return range_process_next;
263 range->selected_done = TRUE;
265 case(range_process_marked):
266 if (fdata->flags.marked == FALSE) {
267 return range_process_next;
270 case(range_process_marked_range):
271 if (range->marked_range_left == 0) {
272 return range_processing_finished;
274 if (fdata->flags.marked == TRUE) {
275 range->marked_range_active = TRUE;
277 if (range->marked_range_active == FALSE ) {
278 return range_process_next;
280 if (!range->process_filtered ||
281 (range->process_filtered && fdata->flags.passed_dfilter == TRUE))
283 range->marked_range_left--;
286 case(range_process_user_range):
287 if (value_is_in_range(range->user_range, fdata->num) == FALSE) {
288 return range_process_next;
292 g_assert_not_reached();
295 /* this packet has to pass the display filter but didn't? -> try next */
296 if (range->process_filtered && fdata->flags.passed_dfilter == FALSE) {
297 return range_process_next;
300 /* We fell through the conditions above, so we accept this packet */
301 return range_process_this;
305 /******************** Range Entry Parser *********************************/
307 /* Converts a range string to a user range.
308 * The parameter 'es' points to the string to be converted, and is defined in
309 * the Save/Print-As widget.
312 void packet_range_convert_str(packet_range_t *range, const gchar *es)
317 if (range->user_range != NULL)
318 g_free(range->user_range);
319 ret = range_convert_str(&new_range, es, cfile.count);
320 if (ret != CVT_NO_ERROR) {
321 /* range isn't valid */
322 range->user_range = NULL;
323 range->user_range_status = ret;
324 range->user_range_cnt = 0L;
325 range->ignored_user_range_cnt = 0L;
326 range->displayed_user_range_cnt = 0L;
327 range->displayed_ignored_user_range_cnt = 0L;
330 range->user_range = new_range;
332 /* calculate new user specified packet range counts */
333 packet_range_calc_user(range);
334 } /* packet_range_convert_str */