Install the new menu files.
[obnox/wireshark/wip.git] / packet-range.c
1 /* packet-range.c
2  * Packet range routines (save, print, ...)
3  *
4  * $Id$
5  *
6  * Dick Gooris <gooris@lucent.com>
7  * Ulf Lamping <ulf.lamping@web.de>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include <glib.h>
37
38 #include <epan/frame_data.h>
39
40 #include "globals.h"
41
42 #include "packet-range.h"
43
44 /* (re-)calculate the packet counts (except the user specified range) */
45 static void packet_range_calc(packet_range_t *range) {
46     guint32       framenum;
47     guint32       mark_low;
48     guint32       mark_high;
49     guint32       displayed_mark_low;
50     guint32       displayed_mark_high;
51     frame_data    *packet;
52
53
54     range->selected_packet        = 0L;
55
56     mark_low                      = 0L;
57     mark_high                     = 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;
63
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;
73
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.
78      */
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.
87          */
88
89         for(framenum = 1; framenum <= cfile.count; framenum++) {
90             packet = frame_data_sequence_find(cfile.frames, framenum);
91
92             if (cfile.current_frame == packet) {
93                 range->selected_packet = framenum;
94             }
95             if (packet->flags.passed_dfilter) {
96                 range->displayed_cnt++;
97             }
98             if (packet->flags.marked) {
99                 if (packet->flags.ignored) {
100                     range->ignored_marked_cnt++;
101                 }
102                 if (packet->flags.passed_dfilter) {
103                     range->displayed_marked_cnt++;
104                     if (packet->flags.ignored) {
105                         range->displayed_ignored_marked_cnt++;
106                     }
107                     if (displayed_mark_low == 0) {
108                        displayed_mark_low = framenum;
109                     }
110                     if (framenum > displayed_mark_high) {
111                        displayed_mark_high = framenum;
112                     }
113                 }
114
115                 if (mark_low == 0) {
116                    mark_low = framenum;
117                 }
118                 if (framenum > mark_high) {
119                    mark_high = framenum;
120                 }
121             }
122             if (packet->flags.ignored) {
123                 range->ignored_cnt++;
124                 if (packet->flags.passed_dfilter) {
125                     range->displayed_ignored_cnt++;
126                 }
127             }
128         }
129
130         for(framenum = 1; framenum <= cfile.count; framenum++) {
131             packet = frame_data_sequence_find(cfile.frames, framenum);
132
133             if (framenum >= mark_low &&
134                 framenum <= mark_high)
135             {
136                 range->mark_range_cnt++;
137                 if (packet->flags.ignored) {
138                     range->ignored_mark_range_cnt++;
139                 }
140             }
141
142             if (framenum >= displayed_mark_low &&
143                 framenum <= displayed_mark_high)
144             {
145                 if (packet->flags.passed_dfilter) {
146                     range->displayed_mark_range_cnt++;
147                     if (packet->flags.ignored) {
148                         range->displayed_ignored_mark_range_cnt++;
149                     }
150                 }
151             }
152         }
153
154 #if 0
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;
158         }
159
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;
163         }
164 #endif
165     }
166 }
167
168
169 /* (re-)calculate the user specified packet range counts */
170 static void packet_range_calc_user(packet_range_t *range) {
171     guint32       framenum;
172     frame_data    *packet;
173
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;
178
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.
183      */
184     if (cfile.frames != NULL) {
185         for(framenum = 1; framenum <= cfile.count; framenum++) {
186             packet = frame_data_sequence_find(cfile.frames, framenum);
187
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++;
192                 }
193                 if (packet->flags.passed_dfilter) {
194                     range->displayed_user_range_cnt++;
195                     if (packet->flags.ignored) {
196                         range->displayed_ignored_user_range_cnt++;
197                     }
198                 }
199             }
200         }
201     }
202 }
203
204
205 /* init the range struct */
206 void packet_range_init(packet_range_t *range) {
207
208     range->process            = range_process_all;
209     range->process_filtered   = FALSE;
210     range->remove_ignored     = FALSE;
211     range->user_range         = range_empty();
212
213     /* calculate all packet range counters */
214     packet_range_calc(range);
215     packet_range_calc_user(range);
216 }
217
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;
223     }
224     return CVT_NO_ERROR;
225 }
226
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;
233
234     if (range->process_filtered == FALSE) {
235         range->marked_range_left = range->mark_range_cnt;
236     } else {
237         range->marked_range_left = range->displayed_mark_range_cnt;
238     }
239 }
240
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;
244 }
245
246 /* do we have to process this packet? */
247 range_process_e packet_range_process_packet(packet_range_t *range, frame_data *fdata) {
248
249     if (range->remove_ignored && fdata->flags.ignored) {
250         return range_process_next;
251     }
252
253     switch(range->process) {
254     case(range_process_all):
255         break;
256     case(range_process_selected):
257         if (range->selected_done) {
258           return range_processing_finished;
259         }
260         if (fdata->num != cfile.current_frame->num) {
261           return range_process_next;
262         }
263         range->selected_done = TRUE;
264         break;
265     case(range_process_marked):
266         if (fdata->flags.marked == FALSE) {
267           return range_process_next;
268         }
269         break;
270     case(range_process_marked_range):
271         if (range->marked_range_left == 0) {
272           return range_processing_finished;
273         }
274         if (fdata->flags.marked == TRUE) {
275           range->marked_range_active = TRUE;
276         }
277         if (range->marked_range_active == FALSE ) {
278           return range_process_next;
279         }
280         if (!range->process_filtered ||
281           (range->process_filtered && fdata->flags.passed_dfilter == TRUE))
282         {
283           range->marked_range_left--;
284         }
285         break;
286     case(range_process_user_range):
287         if (value_is_in_range(range->user_range, fdata->num) == FALSE) {
288           return range_process_next;
289         }
290         break;
291     default:
292         g_assert_not_reached();
293     }
294
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;
298     }
299
300     /* We fell through the conditions above, so we accept this packet */
301     return range_process_this;
302 }
303
304
305 /******************** Range Entry Parser *********************************/
306
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.
310  */
311
312 void packet_range_convert_str(packet_range_t *range, const gchar *es)
313 {
314     range_t *new_range;
315     convert_ret_t ret;
316
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;
328         return;
329     }
330     range->user_range = new_range;
331
332     /* calculate new user specified packet range counts */
333     packet_range_calc_user(range);
334 } /* packet_range_convert_str */