Coverage Report

Created: 2024-02-25 06:23

/src/libpcap/savefile.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3
 *  The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that: (1) source code distributions
7
 * retain the above copyright notice and this paragraph in its entirety, (2)
8
 * distributions including binary code include the above copyright notice and
9
 * this paragraph in its entirety in the documentation or other materials
10
 * provided with the distribution, and (3) all advertising materials mentioning
11
 * features or use of this software display the following acknowledgement:
12
 * ``This product includes software developed by the University of California,
13
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14
 * the University nor the names of its contributors may be used to endorse
15
 * or promote products derived from this software without specific prior
16
 * written permission.
17
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
 *
21
 * savefile.c - supports offline use of tcpdump
22
 *  Extraction/creation by Jeffrey Mogul, DECWRL
23
 *  Modified by Steve McCanne, LBL.
24
 *
25
 * Used to save the received packet headers, after filtering, to
26
 * a file, and then read them later.
27
 * The first record in the file contains saved values for the machine
28
 * dependent values so we can print the dump file on any architecture.
29
 */
30
31
#ifdef HAVE_CONFIG_H
32
#include <config.h>
33
#endif
34
35
#include <pcap-types.h>
36
#ifdef _WIN32
37
#include <io.h>
38
#include <fcntl.h>
39
#endif /* _WIN32 */
40
41
#include <errno.h>
42
#include <memory.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <limits.h> /* for INT_MAX */
47
48
#include "pcap-int.h"
49
50
#ifdef HAVE_OS_PROTO_H
51
#include "os-proto.h"
52
#endif
53
54
#include "sf-pcap.h"
55
#include "sf-pcapng.h"
56
#include "pcap-common.h"
57
#include "charconv.h"
58
59
#ifdef _WIN32
60
/*
61
 * This isn't exported on Windows, because it would only work if both
62
 * WinPcap/Npcap and the code using it were to use the Universal CRT; otherwise,
63
 * a FILE structure in WinPcap/Npcap and a FILE structure in the code using it
64
 * could be different if they're using different versions of the C runtime.
65
 *
66
 * Instead, pcap/pcap.h defines it as a macro that wraps the hopen version,
67
 * with the wrapper calling _fileno() and _get_osfhandle() themselves,
68
 * so that it convert the appropriate CRT version's FILE structure to
69
 * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32
70
 * and Win64 ABIs).
71
 */
72
static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
73
#endif
74
75
/*
76
 * Setting O_BINARY on Windows is a bit tricky.
77
 */
78
#if defined(_WIN32)
79
  #define SET_BINMODE(f)  _setmode(_fileno(f), _O_BINARY)
80
#endif
81
82
static int
83
sf_getnonblock(pcap_t *p _U_)
84
0
{
85
  /*
86
   * This is a savefile, not a live capture file, so never say
87
   * it's in non-blocking mode.
88
   */
89
0
  return (0);
90
0
}
91
92
static int
93
sf_setnonblock(pcap_t *p, int nonblock _U_)
94
0
{
95
  /*
96
   * This is a savefile, not a live capture file, so reject
97
   * requests to put it in non-blocking mode.  (If it's a
98
   * pipe, it could be put in non-blocking mode, but that
99
   * would significantly complicate the code to read packets,
100
   * as it would have to handle reading partial packets and
101
   * keeping the state of the read.)
102
   */
103
0
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
104
0
      "Savefiles cannot be put into non-blocking mode");
105
0
  return (-1);
106
0
}
107
108
static int
109
sf_cant_set_rfmon(pcap_t *p _U_)
110
0
{
111
  /*
112
   * This is a savefile, not a device on which you can capture,
113
   * so never say it supports being put into monitor mode.
114
   */
115
0
  return (0);
116
0
}
117
118
static int
119
sf_stats(pcap_t *p, struct pcap_stat *ps _U_)
120
3.54k
{
121
3.54k
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
122
3.54k
      "Statistics aren't available from savefiles");
123
3.54k
  return (-1);
124
3.54k
}
125
126
#ifdef _WIN32
127
static struct pcap_stat *
128
sf_stats_ex(pcap_t *p, int *size _U_)
129
{
130
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
131
      "Statistics aren't available from savefiles");
132
  return (NULL);
133
}
134
135
static int
136
sf_setbuff(pcap_t *p, int dim _U_)
137
{
138
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
139
      "The kernel buffer size cannot be set while reading from a file");
140
  return (-1);
141
}
142
143
static int
144
sf_setmode(pcap_t *p, int mode _U_)
145
{
146
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
147
      "impossible to set mode while reading from a file");
148
  return (-1);
149
}
150
151
static int
152
sf_setmintocopy(pcap_t *p, int size _U_)
153
{
154
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
155
      "The mintocopy parameter cannot be set while reading from a file");
156
  return (-1);
157
}
158
159
static HANDLE
160
sf_getevent(pcap_t *pcap)
161
{
162
  (void)snprintf(pcap->errbuf, sizeof(pcap->errbuf),
163
      "The read event cannot be retrieved while reading from a file");
164
  return (INVALID_HANDLE_VALUE);
165
}
166
167
static int
168
sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
169
    size_t *lenp _U_)
170
{
171
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
172
      "An OID get request cannot be performed on a file");
173
  return (PCAP_ERROR);
174
}
175
176
static int
177
sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
178
    size_t *lenp _U_)
179
{
180
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
181
      "An OID set request cannot be performed on a file");
182
  return (PCAP_ERROR);
183
}
184
185
static u_int
186
sf_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue _U_, int sync _U_)
187
{
188
  pcapint_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
189
      PCAP_ERRBUF_SIZE);
190
  return (0);
191
}
192
193
static int
194
sf_setuserbuffer(pcap_t *p, int size _U_)
195
{
196
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
197
      "The user buffer cannot be set when reading from a file");
198
  return (-1);
199
}
200
201
static int
202
sf_live_dump(pcap_t *p, char *filename _U_, int maxsize _U_, int maxpacks _U_)
203
{
204
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
205
      "Live packet dumping cannot be performed when reading from a file");
206
  return (-1);
207
}
208
209
static int
210
sf_live_dump_ended(pcap_t *p, int sync _U_)
211
{
212
  snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
213
      "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
214
  return (-1);
215
}
216
217
static PAirpcapHandle
218
sf_get_airpcap_handle(pcap_t *pcap _U_)
219
{
220
  return (NULL);
221
}
222
#endif
223
224
static int
225
sf_inject(pcap_t *p, const void *buf _U_, int size _U_)
226
0
{
227
0
  pcapint_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
228
0
      PCAP_ERRBUF_SIZE);
229
0
  return (-1);
230
0
}
231
232
/*
233
 * Set direction flag: Which packets do we accept on a forwarding
234
 * single device? IN, OUT or both?
235
 */
236
static int
237
sf_setdirection(pcap_t *p, pcap_direction_t d _U_)
238
0
{
239
0
  snprintf(p->errbuf, sizeof(p->errbuf),
240
0
      "Setting direction is not supported on savefiles");
241
0
  return (-1);
242
0
}
243
244
void
245
pcapint_sf_cleanup(pcap_t *p)
246
3.54k
{
247
3.54k
  if (p->rfile != stdin)
248
3.54k
    (void)fclose(p->rfile);
249
3.54k
  if (p->buffer != NULL)
250
3.54k
    free(p->buffer);
251
3.54k
  pcap_freecode(&p->fcode);
252
3.54k
}
253
254
#ifdef _WIN32
255
/*
256
 * Wrapper for fopen() and _wfopen().
257
 *
258
 * If we're in UTF-8 mode, map the pathname from UTF-8 to UTF-16LE and
259
 * call _wfopen().
260
 *
261
 * If we're not, just use fopen(); that'll treat it as being in the
262
 * local code page.
263
 */
264
FILE *
265
pcapint_charset_fopen(const char *path, const char *mode)
266
{
267
  wchar_t *utf16_path;
268
#define MAX_MODE_LEN  16
269
  wchar_t utf16_mode[MAX_MODE_LEN+1];
270
  int i;
271
  char c;
272
  FILE *fp;
273
  int save_errno;
274
275
  if (pcapint_utf_8_mode) {
276
    /*
277
     * Map from UTF-8 to UTF-16LE.
278
     * Fail if there are invalid characters in the input
279
     * string, rather than converting them to REPLACEMENT
280
     * CHARACTER; the latter is appropriate for strings
281
     * to be displayed to the user, but for file names
282
     * you just want the attempt to open the file to fail.
283
     */
284
    utf16_path = cp_to_utf_16le(CP_UTF8, path,
285
        MB_ERR_INVALID_CHARS);
286
    if (utf16_path == NULL) {
287
      /*
288
       * Error.  Assume errno has been set.
289
       *
290
       * XXX - what about Windows errors?
291
       */
292
      return (NULL);
293
    }
294
295
    /*
296
     * Now convert the mode to UTF-16LE as well.
297
     * We assume the mode is ASCII, and that
298
     * it's short, so that's easy.
299
     */
300
    for (i = 0; (c = *mode) != '\0'; i++, mode++) {
301
      if (c > 0x7F) {
302
        /* Not an ASCII character; fail with EINVAL. */
303
        free(utf16_path);
304
        errno = EINVAL;
305
        return (NULL);
306
      }
307
      if (i >= MAX_MODE_LEN) {
308
        /* The mode string is longer than we allow. */
309
        free(utf16_path);
310
        errno = EINVAL;
311
        return (NULL);
312
      }
313
      utf16_mode[i] = c;
314
    }
315
    utf16_mode[i] = '\0';
316
317
    /*
318
     * OK, we have UTF-16LE strings; hand them to
319
     * _wfopen().
320
     */
321
    fp = _wfopen(utf16_path, utf16_mode);
322
323
    /*
324
     * Make sure freeing the UTF-16LE string doesn't
325
     * overwrite the error code we got from _wfopen().
326
     */
327
    save_errno = errno;
328
    free(utf16_path);
329
    errno = save_errno;
330
331
    return (fp);
332
  } else {
333
    /*
334
     * This takes strings in the local code page as an
335
     * argument.
336
     */
337
    return (fopen(path, mode));
338
  }
339
}
340
#endif
341
342
pcap_t *
343
pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
344
          char *errbuf)
345
4.38k
{
346
4.38k
  FILE *fp;
347
4.38k
  pcap_t *p;
348
349
4.38k
  if (fname == NULL) {
350
0
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
351
0
        "A null pointer was supplied as the file name");
352
0
    return (NULL);
353
0
  }
354
4.38k
  if (fname[0] == '-' && fname[1] == '\0')
355
0
  {
356
0
    fp = stdin;
357
0
    if (fp == NULL) {
358
0
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
359
0
          "The standard input is not open");
360
0
      return (NULL);
361
0
    }
362
#if defined(_WIN32)
363
    /*
364
     * We're reading from the standard input, so put it in binary
365
     * mode, as savefiles are binary files.
366
     */
367
    SET_BINMODE(fp);
368
#endif
369
0
  }
370
4.38k
  else {
371
    /*
372
     * Use pcapint_charset_fopen(); on Windows, it tests whether we're
373
     * in "local code page" or "UTF-8" mode, and treats the
374
     * pathname appropriately, and on other platforms, it just
375
     * wraps fopen().
376
     *
377
     * "b" is supported as of C90, so *all* UN*Xes should
378
     * support it, even though it does nothing.
379
     */
380
4.38k
    fp = pcapint_charset_fopen(fname, "rb");
381
4.38k
    if (fp == NULL) {
382
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
383
0
          errno, "%s", fname);
384
0
      return (NULL);
385
0
    }
386
4.38k
  }
387
4.38k
  p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf);
388
4.38k
  if (p == NULL) {
389
845
    if (fp != stdin)
390
845
      fclose(fp);
391
845
  }
392
4.38k
  return (p);
393
4.38k
}
394
395
pcap_t *
396
pcap_open_offline(const char *fname, char *errbuf)
397
4.38k
{
398
4.38k
  return (pcap_open_offline_with_tstamp_precision(fname,
399
4.38k
      PCAP_TSTAMP_PRECISION_MICRO, errbuf));
400
4.38k
}
401
402
#ifdef _WIN32
403
pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision,
404
    char *errbuf)
405
{
406
  int fd;
407
  FILE *file;
408
409
  fd = _open_osfhandle(osfd, _O_RDONLY);
410
  if ( fd < 0 )
411
  {
412
    pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
413
        errno, "_open_osfhandle");
414
    return NULL;
415
  }
416
417
  file = _fdopen(fd, "rb");
418
  if ( file == NULL )
419
  {
420
    pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
421
        errno, "_fdopen");
422
    _close(fd);
423
    return NULL;
424
  }
425
426
  return pcap_fopen_offline_with_tstamp_precision(file, precision,
427
      errbuf);
428
}
429
430
pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
431
{
432
  return pcap_hopen_offline_with_tstamp_precision(osfd,
433
      PCAP_TSTAMP_PRECISION_MICRO, errbuf);
434
}
435
#endif
436
437
/*
438
 * Given a link-layer header type and snapshot length, return a
439
 * snapshot length to use when reading the file; it's guaranteed
440
 * to be > 0 and <= INT_MAX.
441
 *
442
 * XXX - the only reason why we limit it to <= INT_MAX is so that
443
 * it fits in p->snapshot, and the only reason that p->snapshot is
444
 * signed is that pcap_snapshot() returns an int, not an unsigned int.
445
 */
446
bpf_u_int32
447
pcapint_adjust_snapshot(bpf_u_int32 linktype, bpf_u_int32 snaplen)
448
14.2k
{
449
14.2k
  if (snaplen == 0 || snaplen > INT_MAX) {
450
    /*
451
     * Bogus snapshot length; use the maximum for this
452
     * link-layer type as a fallback.
453
     *
454
     * XXX - we don't clamp snapshot lengths that are
455
     * <= INT_MAX but > max_snaplen_for_dlt(linktype),
456
     * so a capture file could cause us to allocate
457
     * a Really Big Buffer.
458
     */
459
3.79k
    snaplen = max_snaplen_for_dlt(linktype);
460
3.79k
  }
461
14.2k
  return snaplen;
462
14.2k
}
463
464
static pcap_t *(*check_headers[])(const uint8_t *, FILE *, u_int, char *, int *) = {
465
  pcap_check_header,
466
  pcap_ng_check_header
467
};
468
469
6.33k
#define N_FILE_TYPES  (sizeof check_headers / sizeof check_headers[0])
470
471
#ifdef _WIN32
472
static
473
#endif
474
pcap_t *
475
pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision,
476
    char *errbuf)
477
4.38k
{
478
4.38k
  register pcap_t *p;
479
4.38k
  uint8_t magic[4];
480
4.38k
  size_t amt_read;
481
4.38k
  u_int i;
482
4.38k
  int err;
483
484
  /*
485
   * Fail if we were passed a NULL fp.
486
   *
487
   * That shouldn't happen if we're opening with a path name, but
488
   * it could happen if buggy code is opening with a FILE * and
489
   * didn't bother to make sure the FILE * isn't null.
490
   */
491
4.38k
  if (fp == NULL) {
492
0
    snprintf(errbuf, PCAP_ERRBUF_SIZE,
493
0
        "Null FILE * pointer provided to savefile open routine");
494
0
    return (NULL);
495
0
  }
496
497
  /*
498
   * Read the first 4 bytes of the file; the network analyzer dump
499
   * file formats we support (pcap and pcapng), and several other
500
   * formats we might support in the future (such as snoop, DOS and
501
   * Windows Sniffer, and Microsoft Network Monitor) all have magic
502
   * numbers that are unique in their first 4 bytes.
503
   */
504
4.38k
  amt_read = fread(&magic, 1, sizeof(magic), fp);
505
4.38k
  if (amt_read != sizeof(magic)) {
506
3
    if (ferror(fp)) {
507
0
      pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
508
0
          errno, "error reading dump file");
509
3
    } else {
510
3
      snprintf(errbuf, PCAP_ERRBUF_SIZE,
511
3
          "truncated dump file; tried to read %zu file header bytes, only got %zu",
512
3
          sizeof(magic), amt_read);
513
3
    }
514
3
    return (NULL);
515
3
  }
516
517
  /*
518
   * Try all file types.
519
   */
520
6.33k
  for (i = 0; i < N_FILE_TYPES; i++) {
521
6.00k
    p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
522
6.00k
    if (p != NULL) {
523
      /* Yup, that's it. */
524
3.54k
      goto found;
525
3.54k
    }
526
2.46k
    if (err) {
527
      /*
528
       * Error trying to read the header.
529
       */
530
516
      return (NULL);
531
516
    }
532
2.46k
  }
533
534
  /*
535
   * Well, who knows what this mess is....
536
   */
537
326
  snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
538
326
  return (NULL);
539
540
3.54k
found:
541
3.54k
  p->rfile = fp;
542
543
  /* Padding only needed for live capture fcode */
544
3.54k
  p->fddipad = 0;
545
546
3.54k
#if !defined(_WIN32)
547
  /*
548
   * You can do "select()" and "poll()" on plain files on most
549
   * platforms, and should be able to do so on pipes.
550
   *
551
   * You can't do "select()" on anything other than sockets in
552
   * Windows, so, on Win32 systems, we don't have "selectable_fd".
553
   */
554
3.54k
  p->selectable_fd = fileno(fp);
555
3.54k
#endif
556
557
3.54k
  p->can_set_rfmon_op = sf_cant_set_rfmon;
558
3.54k
  p->read_op = pcapint_offline_read;
559
3.54k
  p->inject_op = sf_inject;
560
3.54k
  p->setfilter_op = pcapint_install_bpf_program;
561
3.54k
  p->setdirection_op = sf_setdirection;
562
3.54k
  p->set_datalink_op = NULL;  /* we don't support munging link-layer headers */
563
3.54k
  p->getnonblock_op = sf_getnonblock;
564
3.54k
  p->setnonblock_op = sf_setnonblock;
565
3.54k
  p->stats_op = sf_stats;
566
#ifdef _WIN32
567
  p->stats_ex_op = sf_stats_ex;
568
  p->setbuff_op = sf_setbuff;
569
  p->setmode_op = sf_setmode;
570
  p->setmintocopy_op = sf_setmintocopy;
571
  p->getevent_op = sf_getevent;
572
  p->oid_get_request_op = sf_oid_get_request;
573
  p->oid_set_request_op = sf_oid_set_request;
574
  p->sendqueue_transmit_op = sf_sendqueue_transmit;
575
  p->setuserbuffer_op = sf_setuserbuffer;
576
  p->live_dump_op = sf_live_dump;
577
  p->live_dump_ended_op = sf_live_dump_ended;
578
  p->get_airpcap_handle_op = sf_get_airpcap_handle;
579
#endif
580
581
  /*
582
   * For offline captures, the standard one-shot callback can
583
   * be used for pcap_next()/pcap_next_ex().
584
   */
585
3.54k
  p->oneshot_callback = pcapint_oneshot;
586
587
  /*
588
   * Default breakloop operation.
589
   */
590
3.54k
  p->breakloop_op = pcapint_breakloop_common;
591
592
  /*
593
   * Savefiles never require special BPF code generation.
594
   */
595
3.54k
  p->bpf_codegen_flags = 0;
596
597
3.54k
  p->activated = 1;
598
599
3.54k
  return (p);
600
4.38k
}
601
602
/*
603
 * This isn't needed on Windows; we #define pcap_fopen_offline() as
604
 * a wrapper around pcap_hopen_offline(), and we don't call it from
605
 * inside this file, so it's unused.
606
 */
607
#ifndef _WIN32
608
pcap_t *
609
pcap_fopen_offline(FILE *fp, char *errbuf)
610
0
{
611
0
  return (pcap_fopen_offline_with_tstamp_precision(fp,
612
0
      PCAP_TSTAMP_PRECISION_MICRO, errbuf));
613
0
}
614
#endif
615
616
/*
617
 * Read packets from a capture file, and call the callback for each
618
 * packet.
619
 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
620
 */
621
int
622
pcapint_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
623
27.2k
{
624
27.2k
  struct bpf_insn *fcode;
625
27.2k
  int n = 0;
626
27.2k
  u_char *data;
627
628
  /*
629
   * This can conceivably process more than INT_MAX packets,
630
   * which would overflow the packet count, causing it either
631
   * to look like a negative number, and thus cause us to
632
   * return a value that looks like an error, or overflow
633
   * back into positive territory, and thus cause us to
634
   * return a too-low count.
635
   *
636
   * Therefore, if the packet count is unlimited, we clip
637
   * it at INT_MAX; this routine is not expected to
638
   * process packets indefinitely, so that's not an issue.
639
   */
640
27.2k
  if (PACKET_COUNT_IS_UNLIMITED(cnt))
641
0
    cnt = INT_MAX;
642
643
27.2k
  for (;;) {
644
27.2k
    struct pcap_pkthdr h;
645
27.2k
    int status;
646
647
    /*
648
     * Has "pcap_breakloop()" been called?
649
     * If so, return immediately - if we haven't read any
650
     * packets, clear the flag and return -2 to indicate
651
     * that we were told to break out of the loop, otherwise
652
     * leave the flag set, so that the *next* call will break
653
     * out of the loop without having read any packets, and
654
     * return the number of packets we've processed so far.
655
     */
656
27.2k
    if (p->break_loop) {
657
0
      if (n == 0) {
658
0
        p->break_loop = 0;
659
0
        return (-2);
660
0
      } else
661
0
        return (n);
662
0
    }
663
664
27.2k
    status = p->next_packet_op(p, &h, &data);
665
27.2k
    if (status < 0) {
666
      /*
667
       * Error.  Pass it back to the caller.
668
       */
669
827
      return (status);
670
827
    }
671
26.4k
    if (status == 0) {
672
      /*
673
       * EOF.  Nothing more to process;
674
       */
675
2.71k
      break;
676
2.71k
    }
677
678
    /*
679
     * OK, we've read a packet; run it through the filter
680
     * and, if it passes, process it.
681
     */
682
23.6k
    if ((fcode = p->fcode.bf_insns) == NULL ||
683
23.6k
        pcapint_filter(fcode, data, h.len, h.caplen)) {
684
23.6k
      (*callback)(user, &h, data);
685
23.6k
      n++;  /* count the packet */
686
23.6k
      if (n >= cnt)
687
23.6k
        break;
688
23.6k
    }
689
23.6k
  }
690
  /*XXX this breaks semantics tcpslice expects */
691
26.4k
  return (n);
692
27.2k
}