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