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