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