6e561fb0cb9420d278c2cfb607a6bf290640680b
[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   /* The next for-loop is used to obtain the amount of packets to be processed
75    * and is used to present the information in the Save/Print As widget.
76    * We have different types of ranges: All the packets, the number
77    * of packets of a marked range, a single packet, and a user specified
78    * packet range. The last one is not calculated here since this
79    * data must be entered in the widget by the user.
80    */
81
82   for(framenum = 1; framenum <= cfile.count; framenum++) {
83       packet = cap_file_find_fdata(&cfile, framenum);
84
85       if (cfile.current_frame == packet) {
86           range->selected_packet = framenum;
87       }
88       if (packet->flags.passed_dfilter) {
89           range->displayed_cnt++;
90       }
91       if (packet->flags.marked) {
92             if (packet->flags.ignored) {
93                 range->ignored_marked_cnt++;
94             }
95             if (packet->flags.passed_dfilter) {
96                 range->displayed_marked_cnt++;
97                 if (packet->flags.ignored) {
98                     range->displayed_ignored_marked_cnt++;
99                 }
100                 if (displayed_mark_low == 0) {
101                    displayed_mark_low = framenum;
102                 }
103                 if (framenum > displayed_mark_high) {
104                    displayed_mark_high = framenum;
105                 }
106             }
107
108             if (mark_low == 0) {
109                mark_low = framenum;
110             }
111             if (framenum > mark_high) {
112                mark_high = framenum;
113             }
114       }
115       if (packet->flags.ignored) {
116           range->ignored_cnt++;
117           if (packet->flags.passed_dfilter) {
118               range->displayed_ignored_cnt++;
119           }
120       }
121   }
122
123   for(framenum = 1; framenum <= cfile.count; framenum++) {
124       packet = cap_file_find_fdata(&cfile, framenum);
125
126       if (framenum >= mark_low &&
127           framenum <= mark_high)
128       {
129           range->mark_range_cnt++;
130           if (packet->flags.ignored) {
131               range->ignored_mark_range_cnt++;
132           }
133       }
134
135       if (framenum >= displayed_mark_low &&
136           framenum <= displayed_mark_high)
137       {
138           if (packet->flags.passed_dfilter) {
139             range->displayed_mark_range_cnt++;
140             if (packet->flags.ignored) {
141                 range->displayed_ignored_mark_range_cnt++;
142             }
143           }
144       }
145   }
146
147   /* in case we marked just one packet, we add 1. */
148   /*if (cfile.marked_count != 0) {
149     range->mark_range = mark_high - mark_low + 1;
150   }*/
151
152   /* in case we marked just one packet, we add 1. */
153   /*if (range->displayed_marked_cnt != 0) {
154     range->displayed_mark_range = displayed_mark_high - displayed_mark_low + 1;
155   }*/
156 }
157
158
159 /* (re-)calculate the user specified packet range counts */
160 static void packet_range_calc_user(packet_range_t *range) {
161   guint32       framenum;
162   frame_data    *packet;
163
164   range->user_range_cnt             = 0L;
165   range->ignored_user_range_cnt     = 0L;
166   range->displayed_user_range_cnt   = 0L;
167   range->displayed_ignored_user_range_cnt = 0L;
168
169   for(framenum = 1; framenum <= cfile.count; framenum++) {
170       packet = cap_file_find_fdata(&cfile, framenum);
171
172       if (value_is_in_range(range->user_range, framenum)) {
173           range->user_range_cnt++;
174           if (packet->flags.ignored) {
175               range->ignored_user_range_cnt++;
176           }
177           if (packet->flags.passed_dfilter) {
178             range->displayed_user_range_cnt++;
179             if (packet->flags.ignored) {
180                 range->displayed_ignored_user_range_cnt++;
181             }
182           }
183       }
184   }
185 }
186
187
188 /* init the range struct */
189 void packet_range_init(packet_range_t *range) {
190
191   range->process            = range_process_all;
192   range->process_filtered   = FALSE;
193   range->remove_ignored     = FALSE;
194   range->user_range         = range_empty();
195
196   /* calculate all packet range counters */
197   packet_range_calc(range);
198   packet_range_calc_user(range);
199 }
200
201 /* check whether the packet range is OK */
202 convert_ret_t packet_range_check(packet_range_t *range) {
203   if (range->process == range_process_user_range && range->user_range == NULL) {
204     /* Not valid - return the error. */
205     return range->user_range_status;
206   }
207   return CVT_NO_ERROR;
208 }
209
210 /* init the processing run */
211 void packet_range_process_init(packet_range_t *range) {
212   /* Check that, if an explicit range was selected, it's valid. */
213   /* "enumeration" values */
214   range->marked_range_active    = FALSE;
215   range->selected_done          = FALSE;
216
217   if (range->process_filtered == FALSE) {
218     range->marked_range_left = range->mark_range_cnt;
219   } else {
220     range->marked_range_left = range->displayed_mark_range_cnt;
221   }
222 }
223
224 /* do we have to process all packets? */
225 gboolean packet_range_process_all(packet_range_t *range) {
226     return range->process == range_process_all && !range->process_filtered && !range->remove_ignored;
227 }
228
229 /* do we have to process this packet? */
230 range_process_e packet_range_process_packet(packet_range_t *range, frame_data *fdata) {
231
232     if (range->remove_ignored && fdata->flags.ignored) {
233         return range_process_next;
234     }
235
236     switch(range->process) {
237     case(range_process_all):
238         break;
239     case(range_process_selected):
240         if (range->selected_done) {
241           return range_processing_finished;
242         }
243         if (fdata->num != cfile.current_frame->num) {
244           return range_process_next;
245         }
246         range->selected_done = TRUE;
247         break;
248     case(range_process_marked):
249         if (fdata->flags.marked == FALSE) {
250           return range_process_next;
251         }
252         break;
253     case(range_process_marked_range):
254         if (range->marked_range_left == 0) {
255           return range_processing_finished;
256         }
257         if (fdata->flags.marked == TRUE) {
258           range->marked_range_active = TRUE;
259         }
260         if (range->marked_range_active == FALSE ) {
261           return range_process_next;
262         }
263         if (!range->process_filtered ||
264           (range->process_filtered && fdata->flags.passed_dfilter == TRUE))
265         {
266           range->marked_range_left--;
267         }
268         break;
269     case(range_process_user_range):
270         if (value_is_in_range(range->user_range, fdata->num) == FALSE) {
271           return range_process_next;
272         }
273         break;
274     default:
275         g_assert_not_reached();
276     }
277
278     /* this packet has to pass the display filter but didn't? -> try next */
279     if (range->process_filtered && fdata->flags.passed_dfilter == FALSE) {
280         return range_process_next;
281     }
282
283     /* We fell through the conditions above, so we accept this packet */
284     return range_process_this;
285 }
286
287
288 /******************** Range Entry Parser *********************************/
289
290 /* Converts a range string to a user range.
291  * The parameter 'es' points to the string to be converted, and is defined in
292  * the Save/Print-As widget.
293  */
294
295 void packet_range_convert_str(packet_range_t *range, const gchar *es)
296 {
297     range_t *new_range;
298     convert_ret_t ret;
299
300     if (range->user_range != NULL)
301         g_free(range->user_range);
302     ret = range_convert_str(&new_range, es, cfile.count);
303     if (ret != CVT_NO_ERROR) {
304         /* range isn't valid */
305         range->user_range                 = NULL;
306         range->user_range_status          = ret;
307         range->user_range_cnt             = 0L;
308         range->ignored_user_range_cnt     = 0L;
309         range->displayed_user_range_cnt   = 0L;
310         range->displayed_ignored_user_range_cnt = 0L;
311         return;
312     }
313     range->user_range = new_range;
314
315     /* calculate new user specified packet range counts */
316     packet_range_calc_user(range);
317 } /* packet_range_convert_str */