1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4*8b26181fSAndroid Build Coastguard Worker * All rights reserved.
5*8b26181fSAndroid Build Coastguard Worker *
6*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*8b26181fSAndroid Build Coastguard Worker * are met:
9*8b26181fSAndroid Build Coastguard Worker *
10*8b26181fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
12*8b26181fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
13*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
14*8b26181fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
15*8b26181fSAndroid Build Coastguard Worker * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16*8b26181fSAndroid Build Coastguard Worker * nor the names of its contributors may be used to endorse or promote
17*8b26181fSAndroid Build Coastguard Worker * products derived from this software without specific prior written
18*8b26181fSAndroid Build Coastguard Worker * permission.
19*8b26181fSAndroid Build Coastguard Worker *
20*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*8b26181fSAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*8b26181fSAndroid Build Coastguard Worker * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*8b26181fSAndroid Build Coastguard Worker * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*8b26181fSAndroid Build Coastguard Worker * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*8b26181fSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*8b26181fSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*8b26181fSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*8b26181fSAndroid Build Coastguard Worker * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*8b26181fSAndroid Build Coastguard Worker *
32*8b26181fSAndroid Build Coastguard Worker */
33*8b26181fSAndroid Build Coastguard Worker
34*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
35*8b26181fSAndroid Build Coastguard Worker #include <config.h>
36*8b26181fSAndroid Build Coastguard Worker #endif
37*8b26181fSAndroid Build Coastguard Worker
38*8b26181fSAndroid Build Coastguard Worker #include "ftmacros.h"
39*8b26181fSAndroid Build Coastguard Worker #include "diag-control.h"
40*8b26181fSAndroid Build Coastguard Worker
41*8b26181fSAndroid Build Coastguard Worker /*
42*8b26181fSAndroid Build Coastguard Worker * sockutils.h may include <crtdbg.h> on Windows, and pcap-int.h will
43*8b26181fSAndroid Build Coastguard Worker * include portability.h, and portability.h, on Windows, expects that
44*8b26181fSAndroid Build Coastguard Worker * <crtdbg.h> has already been included, so include sockutils.h first.
45*8b26181fSAndroid Build Coastguard Worker */
46*8b26181fSAndroid Build Coastguard Worker #include "sockutils.h"
47*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h" // for the details of the pcap_t structure
48*8b26181fSAndroid Build Coastguard Worker #include "pcap-rpcap.h"
49*8b26181fSAndroid Build Coastguard Worker #include "rpcap-protocol.h"
50*8b26181fSAndroid Build Coastguard Worker #include <errno.h> // for the errno variable
51*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h> // for malloc(), free(), ...
52*8b26181fSAndroid Build Coastguard Worker #include <string.h> // for strstr, etc
53*8b26181fSAndroid Build Coastguard Worker
54*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
55*8b26181fSAndroid Build Coastguard Worker #include <dirent.h> // for readdir
56*8b26181fSAndroid Build Coastguard Worker #endif
57*8b26181fSAndroid Build Coastguard Worker
58*8b26181fSAndroid Build Coastguard Worker /* String identifier to be used in the pcap_findalldevs_ex() */
59*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_FILE "File"
60*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_FILE_LEN (sizeof PCAP_TEXT_SOURCE_FILE - 1)
61*8b26181fSAndroid Build Coastguard Worker /* String identifier to be used in the pcap_findalldevs_ex() */
62*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ADAPTER "Network adapter"
63*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ADAPTER_LEN (sizeof "Network adapter" - 1)
64*8b26181fSAndroid Build Coastguard Worker
65*8b26181fSAndroid Build Coastguard Worker /* String identifier to be used in the pcap_findalldevs_ex() */
66*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ON_LOCAL_HOST "on local host"
67*8b26181fSAndroid Build Coastguard Worker #define PCAP_TEXT_SOURCE_ON_LOCAL_HOST_LEN (sizeof PCAP_TEXT_SOURCE_ON_LOCAL_HOST + 1)
68*8b26181fSAndroid Build Coastguard Worker
69*8b26181fSAndroid Build Coastguard Worker /****************************************************
70*8b26181fSAndroid Build Coastguard Worker * *
71*8b26181fSAndroid Build Coastguard Worker * Function bodies *
72*8b26181fSAndroid Build Coastguard Worker * *
73*8b26181fSAndroid Build Coastguard Worker ****************************************************/
74*8b26181fSAndroid Build Coastguard Worker
pcap_findalldevs_ex(const char * source,struct pcap_rmtauth * auth,pcap_if_t ** alldevs,char * errbuf)75*8b26181fSAndroid Build Coastguard Worker int pcap_findalldevs_ex(const char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf)
76*8b26181fSAndroid Build Coastguard Worker {
77*8b26181fSAndroid Build Coastguard Worker int type;
78*8b26181fSAndroid Build Coastguard Worker char name[PCAP_BUF_SIZE], path[PCAP_BUF_SIZE], filename[PCAP_BUF_SIZE];
79*8b26181fSAndroid Build Coastguard Worker size_t pathlen;
80*8b26181fSAndroid Build Coastguard Worker size_t stringlen;
81*8b26181fSAndroid Build Coastguard Worker pcap_t *fp;
82*8b26181fSAndroid Build Coastguard Worker char tmpstring[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
83*8b26181fSAndroid Build Coastguard Worker pcap_if_t *lastdev; /* Last device in the pcap_if_t list */
84*8b26181fSAndroid Build Coastguard Worker pcap_if_t *dev; /* Device we're adding to the pcap_if_t list */
85*8b26181fSAndroid Build Coastguard Worker
86*8b26181fSAndroid Build Coastguard Worker /* List starts out empty. */
87*8b26181fSAndroid Build Coastguard Worker (*alldevs) = NULL;
88*8b26181fSAndroid Build Coastguard Worker lastdev = NULL;
89*8b26181fSAndroid Build Coastguard Worker
90*8b26181fSAndroid Build Coastguard Worker if (strlen(source) > PCAP_BUF_SIZE)
91*8b26181fSAndroid Build Coastguard Worker {
92*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
93*8b26181fSAndroid Build Coastguard Worker return -1;
94*8b26181fSAndroid Build Coastguard Worker }
95*8b26181fSAndroid Build Coastguard Worker
96*8b26181fSAndroid Build Coastguard Worker /*
97*8b26181fSAndroid Build Coastguard Worker * Determine the type of the source (file, local, remote)
98*8b26181fSAndroid Build Coastguard Worker * There are some differences if pcap_findalldevs_ex() is called to list files and remote adapters.
99*8b26181fSAndroid Build Coastguard Worker * In the first case, the name of the directory we have to look into must be present (therefore
100*8b26181fSAndroid Build Coastguard Worker * the 'name' parameter of the pcap_parsesrcstr() is present).
101*8b26181fSAndroid Build Coastguard Worker * In the second case, the name of the adapter is not required (we need just the host). So, we have
102*8b26181fSAndroid Build Coastguard Worker * to use a first time this function to get the source type, and a second time to get the appropriate
103*8b26181fSAndroid Build Coastguard Worker * info, which depends on the source type.
104*8b26181fSAndroid Build Coastguard Worker */
105*8b26181fSAndroid Build Coastguard Worker if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
106*8b26181fSAndroid Build Coastguard Worker return -1;
107*8b26181fSAndroid Build Coastguard Worker
108*8b26181fSAndroid Build Coastguard Worker switch (type)
109*8b26181fSAndroid Build Coastguard Worker {
110*8b26181fSAndroid Build Coastguard Worker case PCAP_SRC_IFLOCAL:
111*8b26181fSAndroid Build Coastguard Worker if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
112*8b26181fSAndroid Build Coastguard Worker return -1;
113*8b26181fSAndroid Build Coastguard Worker
114*8b26181fSAndroid Build Coastguard Worker /* Initialize temporary string */
115*8b26181fSAndroid Build Coastguard Worker tmpstring[PCAP_BUF_SIZE] = 0;
116*8b26181fSAndroid Build Coastguard Worker
117*8b26181fSAndroid Build Coastguard Worker /* The user wants to retrieve adapters from a local host */
118*8b26181fSAndroid Build Coastguard Worker if (pcap_findalldevs(alldevs, errbuf) == -1)
119*8b26181fSAndroid Build Coastguard Worker return -1;
120*8b26181fSAndroid Build Coastguard Worker
121*8b26181fSAndroid Build Coastguard Worker if (*alldevs == NULL)
122*8b26181fSAndroid Build Coastguard Worker {
123*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
124*8b26181fSAndroid Build Coastguard Worker "No interfaces found! Make sure libpcap/Npcap is properly installed"
125*8b26181fSAndroid Build Coastguard Worker " on the local machine.");
126*8b26181fSAndroid Build Coastguard Worker return -1;
127*8b26181fSAndroid Build Coastguard Worker }
128*8b26181fSAndroid Build Coastguard Worker
129*8b26181fSAndroid Build Coastguard Worker /* Scan all the interfaces and modify name and description */
130*8b26181fSAndroid Build Coastguard Worker /* This is a trick in order to avoid the re-implementation of the pcap_findalldevs here */
131*8b26181fSAndroid Build Coastguard Worker dev = *alldevs;
132*8b26181fSAndroid Build Coastguard Worker while (dev)
133*8b26181fSAndroid Build Coastguard Worker {
134*8b26181fSAndroid Build Coastguard Worker char *localdesc, *desc;
135*8b26181fSAndroid Build Coastguard Worker
136*8b26181fSAndroid Build Coastguard Worker /* Create the new device identifier */
137*8b26181fSAndroid Build Coastguard Worker if (pcap_createsrcstr(tmpstring, PCAP_SRC_IFLOCAL, NULL, NULL, dev->name, errbuf) == -1)
138*8b26181fSAndroid Build Coastguard Worker return -1;
139*8b26181fSAndroid Build Coastguard Worker
140*8b26181fSAndroid Build Coastguard Worker /* Delete the old pointer */
141*8b26181fSAndroid Build Coastguard Worker free(dev->name);
142*8b26181fSAndroid Build Coastguard Worker
143*8b26181fSAndroid Build Coastguard Worker /* Make a copy of the new device identifier */
144*8b26181fSAndroid Build Coastguard Worker dev->name = strdup(tmpstring);
145*8b26181fSAndroid Build Coastguard Worker if (dev->name == NULL)
146*8b26181fSAndroid Build Coastguard Worker {
147*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
148*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
149*8b26181fSAndroid Build Coastguard Worker "malloc() failed");
150*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
151*8b26181fSAndroid Build Coastguard Worker return -1;
152*8b26181fSAndroid Build Coastguard Worker }
153*8b26181fSAndroid Build Coastguard Worker
154*8b26181fSAndroid Build Coastguard Worker /*
155*8b26181fSAndroid Build Coastguard Worker * Create the description.
156*8b26181fSAndroid Build Coastguard Worker */
157*8b26181fSAndroid Build Coastguard Worker if ((dev->description == NULL) || (dev->description[0] == 0))
158*8b26181fSAndroid Build Coastguard Worker localdesc = dev->name;
159*8b26181fSAndroid Build Coastguard Worker else
160*8b26181fSAndroid Build Coastguard Worker localdesc = dev->description;
161*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&desc, "%s '%s' %s",
162*8b26181fSAndroid Build Coastguard Worker PCAP_TEXT_SOURCE_ADAPTER, localdesc,
163*8b26181fSAndroid Build Coastguard Worker PCAP_TEXT_SOURCE_ON_LOCAL_HOST) == -1)
164*8b26181fSAndroid Build Coastguard Worker {
165*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
166*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
167*8b26181fSAndroid Build Coastguard Worker "malloc() failed");
168*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
169*8b26181fSAndroid Build Coastguard Worker return -1;
170*8b26181fSAndroid Build Coastguard Worker }
171*8b26181fSAndroid Build Coastguard Worker
172*8b26181fSAndroid Build Coastguard Worker /* Now overwrite the description */
173*8b26181fSAndroid Build Coastguard Worker free(dev->description);
174*8b26181fSAndroid Build Coastguard Worker dev->description = desc;
175*8b26181fSAndroid Build Coastguard Worker
176*8b26181fSAndroid Build Coastguard Worker dev = dev->next;
177*8b26181fSAndroid Build Coastguard Worker }
178*8b26181fSAndroid Build Coastguard Worker
179*8b26181fSAndroid Build Coastguard Worker return 0;
180*8b26181fSAndroid Build Coastguard Worker
181*8b26181fSAndroid Build Coastguard Worker case PCAP_SRC_FILE:
182*8b26181fSAndroid Build Coastguard Worker {
183*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
184*8b26181fSAndroid Build Coastguard Worker WIN32_FIND_DATA filedata;
185*8b26181fSAndroid Build Coastguard Worker HANDLE filehandle;
186*8b26181fSAndroid Build Coastguard Worker #else
187*8b26181fSAndroid Build Coastguard Worker struct dirent *filedata;
188*8b26181fSAndroid Build Coastguard Worker DIR *unixdir;
189*8b26181fSAndroid Build Coastguard Worker #endif
190*8b26181fSAndroid Build Coastguard Worker
191*8b26181fSAndroid Build Coastguard Worker if (pcap_parsesrcstr(source, &type, NULL, NULL, name, errbuf) == -1)
192*8b26181fSAndroid Build Coastguard Worker return -1;
193*8b26181fSAndroid Build Coastguard Worker
194*8b26181fSAndroid Build Coastguard Worker /* Check that the filename is correct */
195*8b26181fSAndroid Build Coastguard Worker stringlen = strlen(name);
196*8b26181fSAndroid Build Coastguard Worker
197*8b26181fSAndroid Build Coastguard Worker /* The directory must end with '\' in Win32 and '/' in UNIX */
198*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
199*8b26181fSAndroid Build Coastguard Worker #define ENDING_CHAR '\\'
200*8b26181fSAndroid Build Coastguard Worker #else
201*8b26181fSAndroid Build Coastguard Worker #define ENDING_CHAR '/'
202*8b26181fSAndroid Build Coastguard Worker #endif
203*8b26181fSAndroid Build Coastguard Worker
204*8b26181fSAndroid Build Coastguard Worker if (name[stringlen - 1] != ENDING_CHAR)
205*8b26181fSAndroid Build Coastguard Worker {
206*8b26181fSAndroid Build Coastguard Worker name[stringlen] = ENDING_CHAR;
207*8b26181fSAndroid Build Coastguard Worker name[stringlen + 1] = 0;
208*8b26181fSAndroid Build Coastguard Worker
209*8b26181fSAndroid Build Coastguard Worker stringlen++;
210*8b26181fSAndroid Build Coastguard Worker }
211*8b26181fSAndroid Build Coastguard Worker
212*8b26181fSAndroid Build Coastguard Worker /* Save the path for future reference */
213*8b26181fSAndroid Build Coastguard Worker snprintf(path, sizeof(path), "%s", name);
214*8b26181fSAndroid Build Coastguard Worker pathlen = strlen(path);
215*8b26181fSAndroid Build Coastguard Worker
216*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
217*8b26181fSAndroid Build Coastguard Worker /* To perform directory listing, Win32 must have an 'asterisk' as ending char */
218*8b26181fSAndroid Build Coastguard Worker if (name[stringlen - 1] != '*')
219*8b26181fSAndroid Build Coastguard Worker {
220*8b26181fSAndroid Build Coastguard Worker name[stringlen] = '*';
221*8b26181fSAndroid Build Coastguard Worker name[stringlen + 1] = 0;
222*8b26181fSAndroid Build Coastguard Worker }
223*8b26181fSAndroid Build Coastguard Worker
224*8b26181fSAndroid Build Coastguard Worker filehandle = FindFirstFile(name, &filedata);
225*8b26181fSAndroid Build Coastguard Worker
226*8b26181fSAndroid Build Coastguard Worker if (filehandle == INVALID_HANDLE_VALUE)
227*8b26181fSAndroid Build Coastguard Worker {
228*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
229*8b26181fSAndroid Build Coastguard Worker return -1;
230*8b26181fSAndroid Build Coastguard Worker }
231*8b26181fSAndroid Build Coastguard Worker
232*8b26181fSAndroid Build Coastguard Worker #else
233*8b26181fSAndroid Build Coastguard Worker /* opening the folder */
234*8b26181fSAndroid Build Coastguard Worker unixdir= opendir(path);
235*8b26181fSAndroid Build Coastguard Worker
236*8b26181fSAndroid Build Coastguard Worker /* get the first file into it */
237*8b26181fSAndroid Build Coastguard Worker filedata= readdir(unixdir);
238*8b26181fSAndroid Build Coastguard Worker
239*8b26181fSAndroid Build Coastguard Worker if (filedata == NULL)
240*8b26181fSAndroid Build Coastguard Worker {
241*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_FORMAT_TRUNCATION
242*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
243*8b26181fSAndroid Build Coastguard Worker DIAG_ON_FORMAT_TRUNCATION
244*8b26181fSAndroid Build Coastguard Worker closedir(unixdir);
245*8b26181fSAndroid Build Coastguard Worker return -1;
246*8b26181fSAndroid Build Coastguard Worker }
247*8b26181fSAndroid Build Coastguard Worker #endif
248*8b26181fSAndroid Build Coastguard Worker
249*8b26181fSAndroid Build Coastguard Worker /* Add all files we find to the list. */
250*8b26181fSAndroid Build Coastguard Worker do
251*8b26181fSAndroid Build Coastguard Worker {
252*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
253*8b26181fSAndroid Build Coastguard Worker /* Skip the file if the pathname won't fit in the buffer */
254*8b26181fSAndroid Build Coastguard Worker if (pathlen + strlen(filedata.cFileName) >= sizeof(filename))
255*8b26181fSAndroid Build Coastguard Worker continue;
256*8b26181fSAndroid Build Coastguard Worker snprintf(filename, sizeof(filename), "%s%s", path, filedata.cFileName);
257*8b26181fSAndroid Build Coastguard Worker #else
258*8b26181fSAndroid Build Coastguard Worker if (pathlen + strlen(filedata->d_name) >= sizeof(filename))
259*8b26181fSAndroid Build Coastguard Worker continue;
260*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_FORMAT_TRUNCATION
261*8b26181fSAndroid Build Coastguard Worker snprintf(filename, sizeof(filename), "%s%s", path, filedata->d_name);
262*8b26181fSAndroid Build Coastguard Worker DIAG_ON_FORMAT_TRUNCATION
263*8b26181fSAndroid Build Coastguard Worker #endif
264*8b26181fSAndroid Build Coastguard Worker
265*8b26181fSAndroid Build Coastguard Worker fp = pcap_open_offline(filename, errbuf);
266*8b26181fSAndroid Build Coastguard Worker
267*8b26181fSAndroid Build Coastguard Worker if (fp)
268*8b26181fSAndroid Build Coastguard Worker {
269*8b26181fSAndroid Build Coastguard Worker /* allocate the main structure */
270*8b26181fSAndroid Build Coastguard Worker dev = (pcap_if_t *)malloc(sizeof(pcap_if_t));
271*8b26181fSAndroid Build Coastguard Worker if (dev == NULL)
272*8b26181fSAndroid Build Coastguard Worker {
273*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
274*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
275*8b26181fSAndroid Build Coastguard Worker "malloc() failed");
276*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
277*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
278*8b26181fSAndroid Build Coastguard Worker FindClose(filehandle);
279*8b26181fSAndroid Build Coastguard Worker #else
280*8b26181fSAndroid Build Coastguard Worker closedir(unixdir);
281*8b26181fSAndroid Build Coastguard Worker #endif
282*8b26181fSAndroid Build Coastguard Worker return -1;
283*8b26181fSAndroid Build Coastguard Worker }
284*8b26181fSAndroid Build Coastguard Worker
285*8b26181fSAndroid Build Coastguard Worker /* Initialize the structure to 'zero' */
286*8b26181fSAndroid Build Coastguard Worker memset(dev, 0, sizeof(pcap_if_t));
287*8b26181fSAndroid Build Coastguard Worker
288*8b26181fSAndroid Build Coastguard Worker /* Append it to the list. */
289*8b26181fSAndroid Build Coastguard Worker if (lastdev == NULL)
290*8b26181fSAndroid Build Coastguard Worker {
291*8b26181fSAndroid Build Coastguard Worker /*
292*8b26181fSAndroid Build Coastguard Worker * List is empty, so it's also
293*8b26181fSAndroid Build Coastguard Worker * the first device.
294*8b26181fSAndroid Build Coastguard Worker */
295*8b26181fSAndroid Build Coastguard Worker *alldevs = dev;
296*8b26181fSAndroid Build Coastguard Worker }
297*8b26181fSAndroid Build Coastguard Worker else
298*8b26181fSAndroid Build Coastguard Worker {
299*8b26181fSAndroid Build Coastguard Worker /*
300*8b26181fSAndroid Build Coastguard Worker * Append after the last device.
301*8b26181fSAndroid Build Coastguard Worker */
302*8b26181fSAndroid Build Coastguard Worker lastdev->next = dev;
303*8b26181fSAndroid Build Coastguard Worker }
304*8b26181fSAndroid Build Coastguard Worker /* It's now the last device. */
305*8b26181fSAndroid Build Coastguard Worker lastdev = dev;
306*8b26181fSAndroid Build Coastguard Worker
307*8b26181fSAndroid Build Coastguard Worker /* Create the new source identifier */
308*8b26181fSAndroid Build Coastguard Worker if (pcap_createsrcstr(tmpstring, PCAP_SRC_FILE, NULL, NULL, filename, errbuf) == -1)
309*8b26181fSAndroid Build Coastguard Worker {
310*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
311*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
312*8b26181fSAndroid Build Coastguard Worker FindClose(filehandle);
313*8b26181fSAndroid Build Coastguard Worker #else
314*8b26181fSAndroid Build Coastguard Worker closedir(unixdir);
315*8b26181fSAndroid Build Coastguard Worker #endif
316*8b26181fSAndroid Build Coastguard Worker return -1;
317*8b26181fSAndroid Build Coastguard Worker }
318*8b26181fSAndroid Build Coastguard Worker
319*8b26181fSAndroid Build Coastguard Worker dev->name = strdup(tmpstring);
320*8b26181fSAndroid Build Coastguard Worker if (dev->name == NULL)
321*8b26181fSAndroid Build Coastguard Worker {
322*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
323*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
324*8b26181fSAndroid Build Coastguard Worker "malloc() failed");
325*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
326*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
327*8b26181fSAndroid Build Coastguard Worker FindClose(filehandle);
328*8b26181fSAndroid Build Coastguard Worker #else
329*8b26181fSAndroid Build Coastguard Worker closedir(unixdir);
330*8b26181fSAndroid Build Coastguard Worker #endif
331*8b26181fSAndroid Build Coastguard Worker return -1;
332*8b26181fSAndroid Build Coastguard Worker }
333*8b26181fSAndroid Build Coastguard Worker
334*8b26181fSAndroid Build Coastguard Worker /*
335*8b26181fSAndroid Build Coastguard Worker * Create the description.
336*8b26181fSAndroid Build Coastguard Worker */
337*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&dev->description,
338*8b26181fSAndroid Build Coastguard Worker "%s '%s' %s", PCAP_TEXT_SOURCE_FILE,
339*8b26181fSAndroid Build Coastguard Worker filename, PCAP_TEXT_SOURCE_ON_LOCAL_HOST) == -1)
340*8b26181fSAndroid Build Coastguard Worker {
341*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
342*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
343*8b26181fSAndroid Build Coastguard Worker "malloc() failed");
344*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(*alldevs);
345*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
346*8b26181fSAndroid Build Coastguard Worker FindClose(filehandle);
347*8b26181fSAndroid Build Coastguard Worker #else
348*8b26181fSAndroid Build Coastguard Worker closedir(unixdir);
349*8b26181fSAndroid Build Coastguard Worker #endif
350*8b26181fSAndroid Build Coastguard Worker return -1;
351*8b26181fSAndroid Build Coastguard Worker }
352*8b26181fSAndroid Build Coastguard Worker
353*8b26181fSAndroid Build Coastguard Worker pcap_close(fp);
354*8b26181fSAndroid Build Coastguard Worker }
355*8b26181fSAndroid Build Coastguard Worker }
356*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
357*8b26181fSAndroid Build Coastguard Worker while (FindNextFile(filehandle, &filedata) != 0);
358*8b26181fSAndroid Build Coastguard Worker #else
359*8b26181fSAndroid Build Coastguard Worker while ( (filedata= readdir(unixdir)) != NULL);
360*8b26181fSAndroid Build Coastguard Worker #endif
361*8b26181fSAndroid Build Coastguard Worker
362*8b26181fSAndroid Build Coastguard Worker
363*8b26181fSAndroid Build Coastguard Worker /* Close the search handle. */
364*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
365*8b26181fSAndroid Build Coastguard Worker FindClose(filehandle);
366*8b26181fSAndroid Build Coastguard Worker #else
367*8b26181fSAndroid Build Coastguard Worker closedir(unixdir);
368*8b26181fSAndroid Build Coastguard Worker #endif
369*8b26181fSAndroid Build Coastguard Worker
370*8b26181fSAndroid Build Coastguard Worker return 0;
371*8b26181fSAndroid Build Coastguard Worker }
372*8b26181fSAndroid Build Coastguard Worker
373*8b26181fSAndroid Build Coastguard Worker case PCAP_SRC_IFREMOTE:
374*8b26181fSAndroid Build Coastguard Worker return pcap_findalldevs_ex_remote(source, auth, alldevs, errbuf);
375*8b26181fSAndroid Build Coastguard Worker
376*8b26181fSAndroid Build Coastguard Worker default:
377*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
378*8b26181fSAndroid Build Coastguard Worker return -1;
379*8b26181fSAndroid Build Coastguard Worker }
380*8b26181fSAndroid Build Coastguard Worker }
381*8b26181fSAndroid Build Coastguard Worker
pcap_open(const char * source,int snaplen,int flags,int read_timeout,struct pcap_rmtauth * auth,char * errbuf)382*8b26181fSAndroid Build Coastguard Worker pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf)
383*8b26181fSAndroid Build Coastguard Worker {
384*8b26181fSAndroid Build Coastguard Worker char name[PCAP_BUF_SIZE];
385*8b26181fSAndroid Build Coastguard Worker int type;
386*8b26181fSAndroid Build Coastguard Worker pcap_t *fp;
387*8b26181fSAndroid Build Coastguard Worker int status;
388*8b26181fSAndroid Build Coastguard Worker
389*8b26181fSAndroid Build Coastguard Worker /*
390*8b26181fSAndroid Build Coastguard Worker * A null device name is equivalent to the "any" device -
391*8b26181fSAndroid Build Coastguard Worker * which might not be supported on this platform, but
392*8b26181fSAndroid Build Coastguard Worker * this means that you'll get a "not supported" error
393*8b26181fSAndroid Build Coastguard Worker * rather than, say, a crash when we try to dereference
394*8b26181fSAndroid Build Coastguard Worker * the null pointer.
395*8b26181fSAndroid Build Coastguard Worker */
396*8b26181fSAndroid Build Coastguard Worker if (source == NULL)
397*8b26181fSAndroid Build Coastguard Worker source = "any";
398*8b26181fSAndroid Build Coastguard Worker
399*8b26181fSAndroid Build Coastguard Worker if (strlen(source) > PCAP_BUF_SIZE)
400*8b26181fSAndroid Build Coastguard Worker {
401*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
402*8b26181fSAndroid Build Coastguard Worker return NULL;
403*8b26181fSAndroid Build Coastguard Worker }
404*8b26181fSAndroid Build Coastguard Worker
405*8b26181fSAndroid Build Coastguard Worker /*
406*8b26181fSAndroid Build Coastguard Worker * Determine the type of the source (file, local, remote) and,
407*8b26181fSAndroid Build Coastguard Worker * if it's file or local, the name of the file or capture device.
408*8b26181fSAndroid Build Coastguard Worker */
409*8b26181fSAndroid Build Coastguard Worker if (pcap_parsesrcstr(source, &type, NULL, NULL, name, errbuf) == -1)
410*8b26181fSAndroid Build Coastguard Worker return NULL;
411*8b26181fSAndroid Build Coastguard Worker
412*8b26181fSAndroid Build Coastguard Worker switch (type)
413*8b26181fSAndroid Build Coastguard Worker {
414*8b26181fSAndroid Build Coastguard Worker case PCAP_SRC_FILE:
415*8b26181fSAndroid Build Coastguard Worker return pcap_open_offline(name, errbuf);
416*8b26181fSAndroid Build Coastguard Worker
417*8b26181fSAndroid Build Coastguard Worker case PCAP_SRC_IFLOCAL:
418*8b26181fSAndroid Build Coastguard Worker fp = pcap_create(name, errbuf);
419*8b26181fSAndroid Build Coastguard Worker break;
420*8b26181fSAndroid Build Coastguard Worker
421*8b26181fSAndroid Build Coastguard Worker case PCAP_SRC_IFREMOTE:
422*8b26181fSAndroid Build Coastguard Worker /*
423*8b26181fSAndroid Build Coastguard Worker * Although we already have host, port and iface, we prefer
424*8b26181fSAndroid Build Coastguard Worker * to pass only 'source' to pcap_open_rpcap(), so that it
425*8b26181fSAndroid Build Coastguard Worker * has to call pcap_parsesrcstr() again.
426*8b26181fSAndroid Build Coastguard Worker * This is less optimized, but much clearer.
427*8b26181fSAndroid Build Coastguard Worker */
428*8b26181fSAndroid Build Coastguard Worker return pcap_open_rpcap(source, snaplen, flags, read_timeout, auth, errbuf);
429*8b26181fSAndroid Build Coastguard Worker
430*8b26181fSAndroid Build Coastguard Worker default:
431*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
432*8b26181fSAndroid Build Coastguard Worker return NULL;
433*8b26181fSAndroid Build Coastguard Worker }
434*8b26181fSAndroid Build Coastguard Worker
435*8b26181fSAndroid Build Coastguard Worker if (fp == NULL)
436*8b26181fSAndroid Build Coastguard Worker return (NULL);
437*8b26181fSAndroid Build Coastguard Worker status = pcap_set_snaplen(fp, snaplen);
438*8b26181fSAndroid Build Coastguard Worker if (status < 0)
439*8b26181fSAndroid Build Coastguard Worker goto fail;
440*8b26181fSAndroid Build Coastguard Worker if (flags & PCAP_OPENFLAG_PROMISCUOUS)
441*8b26181fSAndroid Build Coastguard Worker {
442*8b26181fSAndroid Build Coastguard Worker status = pcap_set_promisc(fp, 1);
443*8b26181fSAndroid Build Coastguard Worker if (status < 0)
444*8b26181fSAndroid Build Coastguard Worker goto fail;
445*8b26181fSAndroid Build Coastguard Worker }
446*8b26181fSAndroid Build Coastguard Worker if (flags & PCAP_OPENFLAG_MAX_RESPONSIVENESS)
447*8b26181fSAndroid Build Coastguard Worker {
448*8b26181fSAndroid Build Coastguard Worker status = pcap_set_immediate_mode(fp, 1);
449*8b26181fSAndroid Build Coastguard Worker if (status < 0)
450*8b26181fSAndroid Build Coastguard Worker goto fail;
451*8b26181fSAndroid Build Coastguard Worker }
452*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
453*8b26181fSAndroid Build Coastguard Worker /*
454*8b26181fSAndroid Build Coastguard Worker * This flag is supported on Windows only.
455*8b26181fSAndroid Build Coastguard Worker * XXX - is there a way to support it with
456*8b26181fSAndroid Build Coastguard Worker * the capture mechanisms on UN*X? It's not
457*8b26181fSAndroid Build Coastguard Worker * exactly a "set direction" operation; I
458*8b26181fSAndroid Build Coastguard Worker * think it means "do not capture packets
459*8b26181fSAndroid Build Coastguard Worker * injected with pcap_sendpacket() or
460*8b26181fSAndroid Build Coastguard Worker * pcap_inject()".
461*8b26181fSAndroid Build Coastguard Worker */
462*8b26181fSAndroid Build Coastguard Worker /* disable loopback capture if requested */
463*8b26181fSAndroid Build Coastguard Worker if (flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
464*8b26181fSAndroid Build Coastguard Worker fp->opt.nocapture_local = 1;
465*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
466*8b26181fSAndroid Build Coastguard Worker status = pcap_set_timeout(fp, read_timeout);
467*8b26181fSAndroid Build Coastguard Worker if (status < 0)
468*8b26181fSAndroid Build Coastguard Worker goto fail;
469*8b26181fSAndroid Build Coastguard Worker status = pcap_activate(fp);
470*8b26181fSAndroid Build Coastguard Worker if (status < 0)
471*8b26181fSAndroid Build Coastguard Worker goto fail;
472*8b26181fSAndroid Build Coastguard Worker return fp;
473*8b26181fSAndroid Build Coastguard Worker
474*8b26181fSAndroid Build Coastguard Worker fail:
475*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_FORMAT_TRUNCATION
476*8b26181fSAndroid Build Coastguard Worker if (status == PCAP_ERROR)
477*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
478*8b26181fSAndroid Build Coastguard Worker name, fp->errbuf);
479*8b26181fSAndroid Build Coastguard Worker else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
480*8b26181fSAndroid Build Coastguard Worker status == PCAP_ERROR_PERM_DENIED ||
481*8b26181fSAndroid Build Coastguard Worker status == PCAP_ERROR_PROMISC_PERM_DENIED)
482*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)",
483*8b26181fSAndroid Build Coastguard Worker name, pcap_statustostr(status), fp->errbuf);
484*8b26181fSAndroid Build Coastguard Worker else
485*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
486*8b26181fSAndroid Build Coastguard Worker name, pcap_statustostr(status));
487*8b26181fSAndroid Build Coastguard Worker DIAG_ON_FORMAT_TRUNCATION
488*8b26181fSAndroid Build Coastguard Worker pcap_close(fp);
489*8b26181fSAndroid Build Coastguard Worker return NULL;
490*8b26181fSAndroid Build Coastguard Worker }
491*8b26181fSAndroid Build Coastguard Worker
pcap_setsampling(pcap_t * p)492*8b26181fSAndroid Build Coastguard Worker struct pcap_samp *pcap_setsampling(pcap_t *p)
493*8b26181fSAndroid Build Coastguard Worker {
494*8b26181fSAndroid Build Coastguard Worker return &p->rmt_samp;
495*8b26181fSAndroid Build Coastguard Worker }
496