aboutsummaryrefslogtreecommitdiffstats
path: root/range.c
blob: 0ff0dcacf5249355d5c6a9b5161ad57b8afad495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* range.c
 * Packet range routines (save, print, ...)
 *
 * $Id: range.c,v 1.9 2004/02/11 09:19:54 guy Exp $
 *
 * Dick Gooris <gooris@lucent.com>
 * Ulf Lamping <ulf.lamping@web.de>
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <ctype.h>

#include <glib.h>

#include <epan/frame_data.h>


#include "range.h"

#include "globals.h"


static gboolean packet_is_in_range(packet_range_t *range, guint32 val);


/* (re-)calculate the packet counts (except the user specified range) */
void packet_range_calc(packet_range_t *range) {
  guint32       current_count;
  guint32       mark_low;
  guint32       mark_high;
  guint32       displayed_mark_low;
  guint32       displayed_mark_high;
  frame_data    *packet;


  range->selected_packet        = 0L;

  mark_low                      = 0L;
  mark_high                     = 0L;
  range->mark_range_cnt         = 0L;

  displayed_mark_low            = 0L;
  displayed_mark_high           = 0L;
  range->displayed_cnt          = 0L;
  range->displayed_marked_cnt   = 0L;
  range->displayed_mark_range_cnt=0L;

  /* The next for-loop is used to obtain the amount of packets to be processed
   * and is used to present the information in the Save/Print As widget.
   * We have different types of ranges: All the packets, the number
   * of packets of a marked range, a single packet, and a user specified 
   * packet range. The last one is not calculated here since this
   * data must be entered in the widget by the user.
   */

  current_count = 0;
  for(packet = cfile.plist; packet != NULL; packet = packet->next) {
      current_count++;
      if (cfile.current_frame == packet) {
          range->selected_packet = current_count;
      }
      if (packet->flags.passed_dfilter) {
          range->displayed_cnt++;
      }
      if (packet->flags.marked) {
            if (packet->flags.passed_dfilter) {
                range->displayed_marked_cnt++;
                if (displayed_mark_low == 0) {
                   displayed_mark_low = current_count;
                }
                if (current_count > displayed_mark_high) {
                   displayed_mark_high = current_count;
                }
            }

            if (mark_low == 0) {
               mark_low = current_count;
            }
            if (current_count > mark_high) {
               mark_high = current_count;
            }
      }
  }
        
  current_count = 0;
  for(packet = cfile.plist; packet != NULL; packet = packet->next) {
      current_count++;

      if (current_count >= mark_low && 
          current_count <= mark_high)
      {
          range->mark_range_cnt++;
      }

      if (current_count >= displayed_mark_low && 
          current_count <= displayed_mark_high)
      {
          if (packet->flags.passed_dfilter) {
            range->displayed_mark_range_cnt++;
          }
      }
  }

  /* in case we marked just one packet, we add 1. */
  /*if (cfile.marked_count != 0) {
    range->mark_range = mark_high - mark_low + 1;
  }*/
        
  /* in case we marked just one packet, we add 1. */
  /*if (range->displayed_marked_cnt != 0) {
    range->displayed_mark_range = displayed_mark_high - displayed_mark_low + 1;
  }*/
}


/* (re-)calculate the user specified packet range counts */
void packet_range_calc_user(packet_range_t *range) {
  guint32       current_count;
  frame_data    *packet;

  range->user_range_cnt             = 0L;
  range->displayed_user_range_cnt   = 0L;

  current_count = 0;
  for(packet = cfile.plist; packet != NULL; packet = packet->next) {
      current_count++;

      if (packet_is_in_range(range, current_count)) {
          range->user_range_cnt++;
          if (packet->flags.passed_dfilter) {
            range->displayed_user_range_cnt++;
          }
      }
  }
}


/* init the range struct */
void packet_range_init(packet_range_t *range) {

  range->process            = range_process_all;
  range->process_filtered   = FALSE;
  range->nranges            = 0;
  range->ranges[range->nranges].low  = 0L;
  range->ranges[range->nranges].high = 0L;

  /* calculate all packet range counters */
  packet_range_calc(range);
  packet_range_calc_user(range);
}

/* init the processing run */
void packet_range_process_init(packet_range_t *range) {
  /* "enumeration" values */
  range->marked_range_active    = FALSE;
  range->selected_done          = FALSE;

  if (range->process_filtered == FALSE) {
    range->marked_range_left = range->mark_range_cnt;
  } else {
    range->marked_range_left = range->displayed_mark_range_cnt;
  }
}

/* do we have to process all packets? */
gboolean packet_range_process_all(packet_range_t *range) {
    return range->process == range_process_all && !range->process_filtered;
}

/* do we have to process this packet? */
range_process_e packet_range_process_packet(packet_range_t *range, frame_data *fdata) {

    switch(range->process) {
    case(range_process_all):
        break;
    case(range_process_selected):
        if (range->selected_done) {
          return range_processing_finished;
        }
        if (fdata->num != cfile.current_frame->num) {
          return range_process_next;
        }
        range->selected_done = TRUE;
        break;
    case(range_process_marked):
        if (fdata->flags.marked == FALSE) {
          return range_process_next;
        }
        break;
    case(range_process_marked_range):
        if (range->marked_range_left == 0) {
          return range_processing_finished;
        }
        if (fdata->flags.marked == TRUE) {
          range->marked_range_active = TRUE;
        }
        if (range->marked_range_active == FALSE ) {
          return range_process_next;
        }
        if (!range->process_filtered ||
          (range->process_filtered && fdata->flags.passed_dfilter == TRUE))
        {
          range->marked_range_left--;
        }
        break;
    case(range_process_user_range):
        if (packet_is_in_range(range, fdata->num) == FALSE) {
          return range_process_next;
        }
        break;
    default:
        g_assert_not_reached();
    }

    /* this packet has to pass the display filter but didn't? -> try next */
    if (range->process_filtered && fdata->flags.passed_dfilter == FALSE) {
        return range_process_next;
    }

    /* We fell through the conditions above, so we accept this packet */
    return range_process_this;
}


/******************** Range Entry Parser *********************************/

/* Converts a range string to a fast comparable array of ranges.
 * The parameter 'es' points to the string to be converted, and is defined in
 * the Save/Print-As widget.
 *
 * This function fills the array ranges containing low and high values indexed 
 * by a global variable nranges. After having called this function, the function 
 * packet_is_in_range() determines whether a given (packet) number is within 
 * the range or not. 
 *
 * In case of a single packet number, we make a range where low is equal to high. 
 * We strip any characters other than commas, digits, or hyphens. We take care 
 * on wrongly entered ranges; opposite order will be taken care of.
 * 
 * The following syntax is accepted :
 *
 *   1-20,30-40     Range from 1 to 20, and packets 30 to 40
 *   -20,30         Range from 1 to 20, and packet 30
 *   20,30,40-      Packet number 20, 30, and the range from 40 to the end
 *   20-10,30-25    Range from 10 to 20, and from 25 to 30
 *   -              All packets
 */

void packet_range_convert_str(packet_range_t *range, const gchar *es)
{
    gchar     EntryStr[255], OrgStr[255], value[255], p;
    guint     i, j=0;
    guint32   tmp, val;
    gboolean  hyphenseen;

    /* Reset the number of ranges we are going to find */
    range->nranges = 0;
    range->ranges[range->nranges].low  = 0L;
    range->ranges[range->nranges].high = 0L;

    /* Make a copy of the string, and check the validity of the input */
    strcpy(OrgStr,es);
    if (strlen(OrgStr) == 0 ) {
        return;
    }

    /* Only keep digits, commas, and hyphens. */
    for (i=0; i<=strlen(OrgStr); i++) {
      if ( isdigit((guchar)OrgStr[i]) || OrgStr[i] == '-' || OrgStr[i] == ',' ) {
         EntryStr[j++] = OrgStr[i];
      }
    }
    EntryStr[j] = '\0';

    /* Remove any starting commas */
    strcpy(OrgStr,EntryStr);
    i = 0;
    while (OrgStr[i] == ',') {
       i++;
    }
    strcpy(EntryStr,OrgStr+i);

    /* Remove any double commas */
    strcpy(OrgStr,EntryStr);
    p = ',';
    j = 0;
    for (i=0; i<=strlen(OrgStr); i++) {
      if ( OrgStr[i] != ',' || p != ',') {
         EntryStr[j++] = OrgStr[i];
      }
      p = OrgStr[i];
    }
    EntryStr[j] = '\0';

    /* Remove any double hyphens */
    strcpy(OrgStr,EntryStr);
    p = '-';
    j = 0;
    for (i=0; i<=strlen(OrgStr); i++) {
      if (OrgStr[i] != '-' || p != '-' || i == 0) {
         EntryStr[j++] = OrgStr[i];
      }
      p = OrgStr[i];
    }
    EntryStr[j] = '\0';

    /* Remove any trailing commas */
    i = strlen(EntryStr) - 1;
    while (EntryStr[i] == ',') {
       EntryStr[i] = '\0';
       i--;
    }

    /* The entry string is now filtered, and ready for further parsing */
    /* printf("Function : packet_range_convert_str EntryStr = %s\n",EntryStr); */

    /* Now we are going to process the ranges separately until we get a comma,
     * or end of string.
     *
     * We build a structure array called ranges of high and low values. After the
     * following loop, we have the nranges variable which tells how many ranges
     * were found. The number of individual ranges is limited to 'MaxRanges'
     */

    j = 0;
    hyphenseen = FALSE;
    for (i=0; i<=strlen(EntryStr);i++) {

       /* Copy the digit string until a no-digit character is seen */
       if (isdigit((guchar)EntryStr[i])) {
          value[j++] = EntryStr[i];
          continue;
       }

       /* Terminate the digit string, and convert it */
       value[j] = '\0';
       val=atol(value);
       j=0;

       /* In case we see a hyphen, store the value we read in the low part 
        * of ranges. In case it is a trailer hyphen, store the low value, and
        * set the high value to the maximum of packets captured.
        */
       if (EntryStr[i] == '-') {
          /* If this is a trailer hyphen, then treat it in a different
           * way, then the high value is the maximum number of packets counted
           * and we are ready 
           */
          if (i == strlen(EntryStr)-1) {
             range->ranges[range->nranges].low  = val;
             range->ranges[range->nranges].high = cfile.count;
             range->nranges++;
             break;
          } else {
             /* Store the low value of the range */
             range->ranges[range->nranges].low  = val;
          }
          hyphenseen=TRUE;
          continue;
       }

       /* In case we see a comma, or end of string */
       if (EntryStr[i] == ',' || i == strlen(EntryStr)) {
          if (hyphenseen) {
             /* Normal treatment: store the high value range in ranges */
             range->ranges[range->nranges].high = val;
          } else {
             /* We did not see a hyphen and we get a comma, then this must
              * be a single packet number */
             range->ranges[range->nranges].low  = val;
             range->ranges[range->nranges].high = val;
          }
          hyphenseen=FALSE;
       }

       /* Increase the index for the number of ranges we found, and protect
        * against wildly outside array bound jumps */
       range->nranges++;
       if (range->nranges > MaxRange) {
           range->nranges--;
       }
    }
    range->nranges--;

    /*  Now we are going through the low and high values, and check
     *  whether they are in a proper order. Low should be equal or lower
     *  than high. So, go through the loop and swap if needed.
     */
    for (i=0; i <= range->nranges; i++) {
       if (range->ranges[i].low > range->ranges[i].high) {
          tmp = range->ranges[i].low;
          range->ranges[i].low  = range->ranges[i].high;
          range->ranges[i].high = tmp;
       }
    }

    /* In case we want to know what the result ranges are :
     *
     * for (i=0; i <= nranges; i++) {
     *  printf("Function : packet_range_convert_str L=%u \t H=%u\n",ranges[i].low,ranges[i].high);
     * }
     *
     */

    /* calculate new user specified packet range counts */
    packet_range_calc_user(range);
} /* packet_range_convert_str */

/* This function returns TRUE if a given value is within one of the ranges
 * stored in the ranges array.
 */
static gboolean packet_is_in_range(packet_range_t *range, guint32 val)
{
   guint i;

   for (i=0; i <= range->nranges; i++) {
      if (val >= range->ranges[i].low && val <= range->ranges[i].high)
         return TRUE;
   }
   return(FALSE);
}

#if 0
/* This is a debug function to check the range functionality */
static void packet_is_in_range_check(packet_range_t *range, guint32 val)
{

  /* Print the result for a given value */
  printf("Function : packet_is_in_range_check Number %u\t",val);

  if (packet_is_in_range(range, val)) {
     printf("is in range\n");
  } else {
     printf("is not in range\n");
  }
}
#endif