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