2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * sockutils.h may include <crtdbg.h> on Windows, and pcap-int.h will
40 * include portability.h, and portability.h, on Windows, expects that
41 * <crtdbg.h> has already been included, so include sockutils.h first.
43 #include "sockutils.h"
44 #include "pcap-int.h" // for the details of the pcap_t structure
45 #include "pcap-rpcap.h"
46 #include "rpcap-protocol.h"
47 #include <errno.h> // for the errno variable
48 #include <stdlib.h> // for malloc(), free(), ...
49 #include <string.h> // for strstr, etc
52 #include <dirent.h> // for readdir
55 /* String identifier to be used in the pcap_findalldevs_ex() */
56 #define PCAP_TEXT_SOURCE_FILE "File"
57 /* String identifier to be used in the pcap_findalldevs_ex() */
58 #define PCAP_TEXT_SOURCE_ADAPTER "Network adapter"
60 /* String identifier to be used in the pcap_findalldevs_ex() */
61 #define PCAP_TEXT_SOURCE_ON_LOCAL_HOST "on local host"
63 /****************************************************
67 ****************************************************/
69 int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf)
72 char name[PCAP_BUF_SIZE], path[PCAP_BUF_SIZE], filename[PCAP_BUF_SIZE];
74 char tmpstring[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
75 pcap_if_t *lastdev; /* Last device in the pcap_if_t list */
76 pcap_if_t *dev; /* Device we're adding to the pcap_if_t list */
78 /* List starts out empty. */
82 if (strlen(source) > PCAP_BUF_SIZE)
84 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
89 * Determine the type of the source (file, local, remote)
90 * There are some differences if pcap_findalldevs_ex() is called to list files and remote adapters.
91 * In the first case, the name of the directory we have to look into must be present (therefore
92 * the 'name' parameter of the pcap_parsesrcstr() is present).
93 * In the second case, the name of the adapter is not required (we need just the host). So, we have
94 * to use a first time this function to get the source type, and a second time to get the appropriate
95 * info, which depends on the source type.
97 if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
102 case PCAP_SRC_IFLOCAL:
103 if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
106 /* Initialize temporary string */
107 tmpstring[PCAP_BUF_SIZE] = 0;
109 /* The user wants to retrieve adapters from a local host */
110 if (pcap_findalldevs(alldevs, errbuf) == -1)
113 if (*alldevs == NULL)
115 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
116 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
117 " on the local machine.");
121 /* Scan all the interfaces and modify name and description */
122 /* This is a trick in order to avoid the re-implementation of the pcap_findalldevs here */
126 /* Create the new device identifier */
127 if (pcap_createsrcstr(tmpstring, PCAP_SRC_IFLOCAL, NULL, NULL, dev->name, errbuf) == -1)
130 /* Delete the old pointer */
133 /* Make a copy of the new device identifier */
134 dev->name = strdup(tmpstring);
135 if (dev->name == NULL)
137 pcap_fmt_errmsg_for_errno(errbuf,
138 PCAP_ERRBUF_SIZE, errno,
140 pcap_freealldevs(*alldevs);
144 /* Create the new device description */
145 if ((dev->description == NULL) || (dev->description[0] == 0))
146 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_ADAPTER,
147 dev->name, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
149 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_ADAPTER,
150 dev->description, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
152 /* Delete the old pointer */
153 free(dev->description);
155 /* Make a copy of the description */
156 dev->description = strdup(tmpstring);
157 if (dev->description == NULL)
159 pcap_fmt_errmsg_for_errno(errbuf,
160 PCAP_ERRBUF_SIZE, errno,
162 pcap_freealldevs(*alldevs);
175 WIN32_FIND_DATA filedata;
178 struct dirent *filedata;
182 if (pcap_parsesrcstr(source, &type, NULL, NULL, name, errbuf) == -1)
185 /* Check that the filename is correct */
186 stringlen = strlen(name);
188 /* The directory must end with '\' in Win32 and '/' in UNIX */
190 #define ENDING_CHAR '\\'
192 #define ENDING_CHAR '/'
195 if (name[stringlen - 1] != ENDING_CHAR)
197 name[stringlen] = ENDING_CHAR;
198 name[stringlen + 1] = 0;
203 /* Save the path for future reference */
204 pcap_snprintf(path, sizeof(path), "%s", name);
207 /* To perform directory listing, Win32 must have an 'asterisk' as ending char */
208 if (name[stringlen - 1] != '*')
210 name[stringlen] = '*';
211 name[stringlen + 1] = 0;
214 filehandle = FindFirstFile(name, &filedata);
216 if (filehandle == INVALID_HANDLE_VALUE)
218 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
223 /* opening the folder */
224 unixdir= opendir(path);
226 /* get the first file into it */
227 filedata= readdir(unixdir);
229 if (filedata == NULL)
231 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
236 /* Add all files we find to the list. */
241 pcap_snprintf(filename, sizeof(filename), "%s%s", path, filedata.cFileName);
243 pcap_snprintf(filename, sizeof(filename), "%s%s", path, filedata->d_name);
246 fp = pcap_open_offline(filename, errbuf);
250 /* allocate the main structure */
251 dev = (pcap_if_t *)malloc(sizeof(pcap_if_t));
254 pcap_fmt_errmsg_for_errno(errbuf,
255 PCAP_ERRBUF_SIZE, errno,
257 pcap_freealldevs(*alldevs);
261 /* Initialize the structure to 'zero' */
262 memset(dev, 0, sizeof(pcap_if_t));
264 /* Append it to the list. */
268 * List is empty, so it's also
276 * Append after the last device.
280 /* It's now the last device. */
283 /* Create the new source identifier */
284 if (pcap_createsrcstr(tmpstring, PCAP_SRC_FILE, NULL, NULL, filename, errbuf) == -1)
286 pcap_freealldevs(*alldevs);
290 stringlen = strlen(tmpstring);
292 dev->name = (char *)malloc(stringlen + 1);
293 if (dev->name == NULL)
295 pcap_fmt_errmsg_for_errno(errbuf,
296 PCAP_ERRBUF_SIZE, errno,
298 pcap_freealldevs(*alldevs);
302 strlcpy(dev->name, tmpstring, stringlen);
304 dev->name[stringlen] = 0;
306 /* Create the description */
307 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_FILE,
308 filename, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
310 stringlen = strlen(tmpstring);
312 dev->description = (char *)malloc(stringlen + 1);
314 if (dev->description == NULL)
316 pcap_fmt_errmsg_for_errno(errbuf,
317 PCAP_ERRBUF_SIZE, errno,
319 pcap_freealldevs(*alldevs);
323 /* Copy the new device description into the correct memory location */
324 strlcpy(dev->description, tmpstring, stringlen + 1);
330 while (FindNextFile(filehandle, &filedata) != 0);
332 while ( (filedata= readdir(unixdir)) != NULL);
337 /* Close the search handle. */
338 FindClose(filehandle);
344 case PCAP_SRC_IFREMOTE:
345 return pcap_findalldevs_ex_remote(source, auth, alldevs, errbuf);
348 strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
353 pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf)
355 char name[PCAP_BUF_SIZE];
360 if (strlen(source) > PCAP_BUF_SIZE)
362 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
367 * Determine the type of the source (file, local, remote) and,
368 * if it's file or local, the name of the file or capture device.
370 if (pcap_parsesrcstr(source, &type, NULL, NULL, name, errbuf) == -1)
376 return pcap_open_offline(name, errbuf);
378 case PCAP_SRC_IFLOCAL:
379 fp = pcap_create(name, errbuf);
382 case PCAP_SRC_IFREMOTE:
384 * Although we already have host, port and iface, we prefer
385 * to pass only 'source' to pcap_open_rpcap(), so that it
386 * has to call pcap_parsesrcstr() again.
387 * This is less optimized, but much clearer.
389 return pcap_open_rpcap(source, snaplen, flags, read_timeout, auth, errbuf);
392 strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
398 status = pcap_set_snaplen(fp, snaplen);
401 if (flags & PCAP_OPENFLAG_PROMISCUOUS)
403 status = pcap_set_promisc(fp, 1);
407 if (flags & PCAP_OPENFLAG_MAX_RESPONSIVENESS)
409 status = pcap_set_immediate_mode(fp, 1);
415 * This flag is supported on Windows only.
416 * XXX - is there a way to support it with
417 * the capture mechanisms on UN*X? It's not
418 * exactly a "set direction" operation; I
419 * think it means "do not capture packets
420 * injected with pcap_sendpacket() or
423 /* disable loopback capture if requested */
424 if (flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
425 fp->opt.nocapture_local = 1;
427 status = pcap_set_timeout(fp, read_timeout);
430 status = pcap_activate(fp);
436 if (status == PCAP_ERROR)
437 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
439 else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
440 status == PCAP_ERROR_PERM_DENIED ||
441 status == PCAP_ERROR_PROMISC_PERM_DENIED)
442 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)",
443 name, pcap_statustostr(status), fp->errbuf);
445 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
446 name, pcap_statustostr(status));
451 struct pcap_samp *pcap_setsampling(pcap_t *p)