fm/ipmitopo: fix 64-bit compilation
[unleashed.git] / contrib / libpcap / pcap-new.c
blob494e425f8bb112a511610f749d04eac1a3f732f4
1 /*
2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
18 * permission.
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.
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include "pcap-int.h" // for the details of the pcap_t structure
39 #include "pcap-rpcap.h"
40 #include "sockutils.h"
41 #include <errno.h> // for the errno variable
42 #include <stdlib.h> // for malloc(), free(), ...
43 #include <string.h> // for strstr, etc
45 #ifndef WIN32
46 #include <dirent.h> // for readdir
47 #endif
49 /* Keeps a list of all the opened connections in the active mode. */
50 extern struct activehosts *activeHosts;
53 * \brief Keeps the main socket identifier when we want to accept a new remote connection (active mode only).
54 * See the documentation of pcap_remoteact_accept() and pcap_remoteact_cleanup() for more details.
56 SOCKET sockmain;
58 /* String identifier to be used in the pcap_findalldevs_ex() */
59 #define PCAP_TEXT_SOURCE_FILE "File"
60 /* String identifier to be used in the pcap_findalldevs_ex() */
61 #define PCAP_TEXT_SOURCE_ADAPTER "Network adapter"
63 /* String identifier to be used in the pcap_findalldevs_ex() */
64 #define PCAP_TEXT_SOURCE_ON_LOCAL_HOST "on local host"
65 /* String identifier to be used in the pcap_findalldevs_ex() */
66 #define PCAP_TEXT_SOURCE_ON_REMOTE_HOST "on remote node"
69 * Private data for capturing on WinPcap devices.
71 struct pcap_win {
72 int nonblock;
73 int rfmon_selfstart; /* a flag tells whether the monitor mode is set by itself */
74 int filtering_in_kernel; /* using kernel filter */
76 #ifdef HAVE_DAG_API
77 int dag_fcs_bits; /* Number of checksum bits from link layer */
78 #endif
81 /****************************************************
82 * *
83 * Function bodies *
84 * *
85 ****************************************************/
87 int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf)
89 SOCKET sockctrl; /* socket descriptor of the control connection */
90 uint32 totread = 0; /* number of bytes of the payload read from the socket */
91 int nread;
92 struct addrinfo hints; /* temp variable needed to resolve hostnames into to socket representation */
93 struct addrinfo *addrinfo; /* temp variable needed to resolve hostnames into to socket representation */
94 struct rpcap_header header; /* structure that keeps the general header of the rpcap protocol */
95 int i, j; /* temp variables */
96 int naddr; /* temp var needed to avoid problems with IPv6 addresses */
97 struct pcap_addr *addr; /* another such temp */
98 int retval; /* store the return value of the functions */
99 int nif; /* Number of interfaces listed */
100 int active = 0; /* 'true' if we the other end-party is in active mode */
101 char host[PCAP_BUF_SIZE], port[PCAP_BUF_SIZE], name[PCAP_BUF_SIZE], path[PCAP_BUF_SIZE], filename[PCAP_BUF_SIZE];
102 int type;
103 pcap_t *fp;
104 char tmpstring[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
105 pcap_if_t *dev; /* Previous device into the pcap_if_t chain */
108 if (strlen(source) > PCAP_BUF_SIZE)
110 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
111 return -1;
115 * Determine the type of the source (file, local, remote)
116 * There are some differences if pcap_findalldevs_ex() is called to list files and remote adapters.
117 * In the first case, the name of the directory we have to look into must be present (therefore
118 * the 'name' parameter of the pcap_parsesrcstr() is present).
119 * In the second case, the name of the adapter is not required (we need just the host). So, we have
120 * to use a first time this function to get the source type, and a second time to get the appropriate
121 * info, which depends on the source type.
123 if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
124 return -1;
126 if (type == PCAP_SRC_IFLOCAL)
128 if (pcap_parsesrcstr(source, &type, host, NULL, NULL, errbuf) == -1)
129 return -1;
131 /* Initialize temporary string */
132 tmpstring[PCAP_BUF_SIZE] = 0;
134 /* The user wants to retrieve adapters from a local host */
135 if (pcap_findalldevs(alldevs, errbuf) == -1)
136 return -1;
138 if ((alldevs == NULL) || (*alldevs == NULL))
140 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
141 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
142 " on the local machine.");
143 return -1;
146 /* Scan all the interfaces and modify name and description */
147 /* This is a trick in order to avoid the re-implementation of the pcap_findalldevs here */
148 dev = *alldevs;
149 while (dev)
151 /* Create the new device identifier */
152 if (pcap_createsrcstr(tmpstring, PCAP_SRC_IFLOCAL, NULL, NULL, dev->name, errbuf) == -1)
153 return -1;
155 /* Delete the old pointer */
156 free(dev->name);
158 /* Make a copy of the new device identifier */
159 dev->name = strdup(tmpstring);
160 if (dev->name == NULL)
162 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
163 return -1;
166 /* Create the new device description */
167 if ((dev->description == NULL) || (dev->description[0] == 0))
168 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_ADAPTER,
169 dev->name, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
170 else
171 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_ADAPTER,
172 dev->description, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
174 /* Delete the old pointer */
175 free(dev->description);
177 /* Make a copy of the description */
178 dev->description = strdup(tmpstring);
179 if (dev->description == NULL)
181 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
182 return -1;
185 dev = dev->next;
188 return 0;
191 (*alldevs) = NULL;
193 if (type == PCAP_SRC_FILE)
195 size_t stringlen;
196 #ifdef WIN32
197 WIN32_FIND_DATA filedata;
198 HANDLE filehandle;
199 #else
200 struct dirent *filedata;
201 DIR *unixdir;
202 #endif
204 if (pcap_parsesrcstr(source, &type, host, port, name, errbuf) == -1)
205 return -1;
207 /* Check that the filename is correct */
208 stringlen = strlen(name);
210 /* The directory must end with '\' in Win32 and '/' in UNIX */
211 #ifdef WIN32
212 #define ENDING_CHAR '\\'
213 #else
214 #define ENDING_CHAR '/'
215 #endif
217 if (name[stringlen - 1] != ENDING_CHAR)
219 name[stringlen] = ENDING_CHAR;
220 name[stringlen + 1] = 0;
222 stringlen++;
225 /* Save the path for future reference */
226 pcap_snprintf(path, sizeof(path), "%s", name);
228 #ifdef WIN32
229 /* To perform directory listing, Win32 must have an 'asterisk' as ending char */
230 if (name[stringlen - 1] != '*')
232 name[stringlen] = '*';
233 name[stringlen + 1] = 0;
236 filehandle = FindFirstFile(name, &filedata);
238 if (filehandle == INVALID_HANDLE_VALUE)
240 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
241 return -1;
244 #else
245 /* opening the folder */
246 unixdir= opendir(path);
248 /* get the first file into it */
249 filedata= readdir(unixdir);
251 if (filedata == NULL)
253 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
254 return -1;
256 #endif
261 #ifdef WIN32
262 pcap_snprintf(filename, sizeof(filename), "%s%s", path, filedata.cFileName);
263 #else
264 pcap_snprintf(filename, sizeof(filename), "%s%s", path, filedata->d_name);
265 #endif
267 fp = pcap_open_offline(filename, errbuf);
269 if (fp)
271 /* allocate the main structure */
272 if (*alldevs == NULL) /* This is in case it is the first file */
274 (*alldevs) = (pcap_if_t *)malloc(sizeof(pcap_if_t));
275 dev = (*alldevs);
277 else
279 dev->next = (pcap_if_t *)malloc(sizeof(pcap_if_t));
280 dev = dev->next;
283 /* check that the malloc() didn't fail */
284 if (dev == NULL)
286 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
287 return -1;
290 /* Initialize the structure to 'zero' */
291 memset(dev, 0, sizeof(pcap_if_t));
293 /* Create the new source identifier */
294 if (pcap_createsrcstr(tmpstring, PCAP_SRC_FILE, NULL, NULL, filename, errbuf) == -1)
295 return -1;
297 stringlen = strlen(tmpstring);
299 dev->name = (char *)malloc(stringlen + 1);
300 if (dev->name == NULL)
302 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
303 return -1;
306 strlcpy(dev->name, tmpstring, stringlen);
308 dev->name[stringlen] = 0;
310 /* Create the description */
311 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_FILE,
312 filename, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
314 stringlen = strlen(tmpstring);
316 dev->description = (char *)malloc(stringlen + 1);
318 if (dev->description == NULL)
320 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
321 return -1;
324 /* Copy the new device description into the correct memory location */
325 strlcpy(dev->description, tmpstring, stringlen + 1);
327 pcap_close(fp);
330 #ifdef WIN32
331 while (FindNextFile(filehandle, &filedata) != 0);
332 #else
333 while ( (filedata= readdir(unixdir)) != NULL);
334 #endif
337 #ifdef WIN32
338 /* Close the search handle. */
339 FindClose(filehandle);
340 #endif
342 return 0;
345 /* If we come here, it is a remote host */
347 /* Retrieve the needed data for getting adapter list */
348 if (pcap_parsesrcstr(source, &type, host, port, NULL, errbuf) == -1)
349 return -1;
351 /* Warning: this call can be the first one called by the user. */
352 /* For this reason, we have to initialize the WinSock support. */
353 if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
354 return -1;
356 /* Check for active mode */
357 sockctrl = rpcap_remoteact_getsock(host, &active, errbuf);
358 if (sockctrl == INVALID_SOCKET)
359 return -1;
361 if (!active) {
363 * We're not in active mode; let's try to open a new
364 * control connection.
366 addrinfo = NULL;
368 memset(&hints, 0, sizeof(struct addrinfo));
369 hints.ai_family = PF_UNSPEC;
370 hints.ai_socktype = SOCK_STREAM;
372 if (port[0] == 0)
374 /* the user chose not to specify the port */
375 if (sock_initaddress(host, RPCAP_DEFAULT_NETPORT, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
376 return -1;
378 else
380 if (sock_initaddress(host, port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
381 return -1;
384 if ((sockctrl = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE)) == -1)
385 goto error;
387 /* addrinfo is no longer used */
388 freeaddrinfo(addrinfo);
389 addrinfo = NULL;
391 if (rpcap_sendauth(sockctrl, auth, errbuf) == -1)
393 sock_close(sockctrl, NULL, 0);
394 return -1;
398 /* RPCAP findalldevs command */
399 rpcap_createhdr(&header, RPCAP_MSG_FINDALLIF_REQ, 0, 0);
401 if (sock_send(sockctrl, (char *)&header, sizeof(struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE) == -1)
402 goto error;
404 if (sock_recv(sockctrl, (char *)&header, sizeof(struct rpcap_header), SOCK_RECEIVEALL_YES, errbuf, PCAP_ERRBUF_SIZE) == -1)
405 goto error;
407 /* Checks if the message is correct */
408 retval = rpcap_checkmsg(errbuf, sockctrl, &header, RPCAP_MSG_FINDALLIF_REPLY, RPCAP_MSG_ERROR, 0);
410 if (retval != RPCAP_MSG_FINDALLIF_REPLY) /* the message is not the one expected */
412 switch (retval)
414 case -3: /* Unrecoverable network error */
415 case -2: /* The other endpoint send a message that is not allowed here */
416 case -1: /* The other endpoint has a version number that is not compatible with our */
417 break;
419 case RPCAP_MSG_ERROR: /* The other endpoint reported an error */
420 break;
422 default:
424 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Internal error");
425 break;
429 if (!active)
430 sock_close(sockctrl, NULL, 0);
432 return -1;
435 /* read the number of interfaces */
436 nif = ntohs(header.value);
438 /* loop until all interfaces have been received */
439 for (i = 0; i < nif; i++)
441 struct rpcap_findalldevs_if findalldevs_if;
442 char tmpstring2[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
443 size_t stringlen;
445 tmpstring2[PCAP_BUF_SIZE] = 0;
447 /* receive the findalldevs structure from remote host */
448 nread = sock_recv(sockctrl, (char *)&findalldevs_if,
449 sizeof(struct rpcap_findalldevs_if), SOCK_RECEIVEALL_YES,
450 errbuf, PCAP_ERRBUF_SIZE);
451 if (nread == -1)
452 goto error;
453 totread += nread;
455 findalldevs_if.namelen = ntohs(findalldevs_if.namelen);
456 findalldevs_if.desclen = ntohs(findalldevs_if.desclen);
457 findalldevs_if.naddr = ntohs(findalldevs_if.naddr);
459 /* allocate the main structure */
460 if (i == 0)
462 (*alldevs) = (pcap_if_t *)malloc(sizeof(pcap_if_t));
463 dev = (*alldevs);
465 else
467 dev->next = (pcap_if_t *)malloc(sizeof(pcap_if_t));
468 dev = dev->next;
471 /* check that the malloc() didn't fail */
472 if (dev == NULL)
474 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
475 goto error;
478 /* Initialize the structure to 'zero' */
479 memset(dev, 0, sizeof(pcap_if_t));
481 /* allocate mem for name and description */
482 if (findalldevs_if.namelen)
485 if (findalldevs_if.namelen >= sizeof(tmpstring))
487 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Interface name too long");
488 goto error;
491 /* Retrieve adapter name */
492 nread = sock_recv(sockctrl, tmpstring,
493 findalldevs_if.namelen, SOCK_RECEIVEALL_YES,
494 errbuf, PCAP_ERRBUF_SIZE);
495 if (nread == -1)
496 goto error;
497 totread += nread;
499 tmpstring[findalldevs_if.namelen] = 0;
501 /* Create the new device identifier */
502 if (pcap_createsrcstr(tmpstring2, PCAP_SRC_IFREMOTE, host, port, tmpstring, errbuf) == -1)
503 return -1;
505 stringlen = strlen(tmpstring2);
507 dev->name = (char *)malloc(stringlen + 1);
508 if (dev->name == NULL)
510 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
511 goto error;
514 /* Copy the new device name into the correct memory location */
515 strlcpy(dev->name, tmpstring2, stringlen + 1);
518 if (findalldevs_if.desclen)
520 if (findalldevs_if.desclen >= sizeof(tmpstring))
522 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Interface description too long");
523 goto error;
526 /* Retrieve adapter description */
527 nread = sock_recv(sockctrl, tmpstring,
528 findalldevs_if.desclen, SOCK_RECEIVEALL_YES,
529 errbuf, PCAP_ERRBUF_SIZE);
530 if (nread == -1)
531 goto error;
532 totread += nread;
534 tmpstring[findalldevs_if.desclen] = 0;
536 pcap_snprintf(tmpstring2, sizeof(tmpstring2) - 1, "%s '%s' %s %s", PCAP_TEXT_SOURCE_ADAPTER,
537 tmpstring, PCAP_TEXT_SOURCE_ON_REMOTE_HOST, host);
539 stringlen = strlen(tmpstring2);
541 dev->description = (char *)malloc(stringlen + 1);
543 if (dev->description == NULL)
545 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
546 goto error;
549 /* Copy the new device description into the correct memory location */
550 strlcpy(dev->description, tmpstring2, stringlen + 1);
553 dev->flags = ntohl(findalldevs_if.flags);
555 naddr = 0;
556 addr = NULL;
557 /* loop until all addresses have been received */
558 for (j = 0; j < findalldevs_if.naddr; j++)
560 struct rpcap_findalldevs_ifaddr ifaddr;
562 /* Retrieve the interface addresses */
563 nread = sock_recv(sockctrl, (char *)&ifaddr,
564 sizeof(struct rpcap_findalldevs_ifaddr),
565 SOCK_RECEIVEALL_YES, errbuf, PCAP_ERRBUF_SIZE);
566 if (nread == -1)
567 goto error;
568 totread += nread;
571 * WARNING libpcap bug: the address listing is
572 * available only for AF_INET.
574 * XXX - IPv6?
576 if (ntohs(ifaddr.addr.ss_family) == AF_INET)
578 if (addr == NULL)
580 dev->addresses = (struct pcap_addr *) malloc(sizeof(struct pcap_addr));
581 addr = dev->addresses;
583 else
585 addr->next = (struct pcap_addr *) malloc(sizeof(struct pcap_addr));
586 addr = addr->next;
588 naddr++;
590 if (addr == NULL)
592 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
593 goto error;
595 addr->next = NULL;
597 if (rpcap_deseraddr((struct sockaddr_storage *) &ifaddr.addr,
598 (struct sockaddr_storage **) &addr->addr, errbuf) == -1)
599 goto error;
600 if (rpcap_deseraddr((struct sockaddr_storage *) &ifaddr.netmask,
601 (struct sockaddr_storage **) &addr->netmask, errbuf) == -1)
602 goto error;
603 if (rpcap_deseraddr((struct sockaddr_storage *) &ifaddr.broadaddr,
604 (struct sockaddr_storage **) &addr->broadaddr, errbuf) == -1)
605 goto error;
606 if (rpcap_deseraddr((struct sockaddr_storage *) &ifaddr.dstaddr,
607 (struct sockaddr_storage **) &addr->dstaddr, errbuf) == -1)
608 goto error;
610 if ((addr->addr == NULL) && (addr->netmask == NULL) &&
611 (addr->broadaddr == NULL) && (addr->dstaddr == NULL))
613 free(addr);
614 addr = NULL;
615 if (naddr == 1)
616 naddr = 0; /* the first item of the list had NULL addresses */
622 /* Checks if all the data has been read; if not, discard the data in excess */
623 if (totread != ntohl(header.plen))
625 if (sock_discard(sockctrl, ntohl(header.plen) - totread, errbuf, PCAP_ERRBUF_SIZE) == 1)
626 return -1;
629 /* Control connection has to be closed only in case the remote machine is in passive mode */
630 if (!active)
632 /* DO not send RPCAP_CLOSE, since we did not open a pcap_t; no need to free resources */
633 if (sock_close(sockctrl, errbuf, PCAP_ERRBUF_SIZE))
634 return -1;
637 /* To avoid inconsistencies in the number of sock_init() */
638 sock_cleanup();
640 return 0;
642 error:
644 * In case there has been an error, I don't want to overwrite it with a new one
645 * if the following call fails. I want to return always the original error.
647 * Take care: this connection can already be closed when we try to close it.
648 * This happens because a previous error in the rpcapd, which requested to
649 * closed the connection. In that case, we already recognized that into the
650 * rpspck_isheaderok() and we already acknowledged the closing.
651 * In that sense, this call is useless here (however it is needed in case
652 * the client generates the error).
654 * Checks if all the data has been read; if not, discard the data in excess
656 if (totread != ntohl(header.plen))
658 if (sock_discard(sockctrl, ntohl(header.plen) - totread, NULL, 0) == 1)
659 return -1;
662 /* Control connection has to be closed only in case the remote machine is in passive mode */
663 if (!active)
664 sock_close(sockctrl, NULL, 0);
666 /* To avoid inconsistencies in the number of sock_init() */
667 sock_cleanup();
669 return -1;
672 int pcap_createsrcstr(char *source, int type, const char *host, const char *port, const char *name, char *errbuf)
674 switch (type)
676 case PCAP_SRC_FILE:
678 strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE);
679 if ((name) && (*name))
681 strlcat(source, name, PCAP_BUF_SIZE);
682 return 0;
684 else
686 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The file name cannot be NULL.");
687 return -1;
691 case PCAP_SRC_IFREMOTE:
693 strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
694 if ((host) && (*host))
696 if ((strcspn(host, "aAbBcCdDeEfFgGhHjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ")) == strlen(host))
698 /* the host name does not contains alphabetic chars. So, it is a numeric address */
699 /* In this case we have to include it between square brackets */
700 strlcat(source, "[", PCAP_BUF_SIZE);
701 strlcat(source, host, PCAP_BUF_SIZE);
702 strlcat(source, "]", PCAP_BUF_SIZE);
704 else
705 strlcat(source, host, PCAP_BUF_SIZE);
707 if ((port) && (*port))
709 strlcat(source, ":", PCAP_BUF_SIZE);
710 strlcat(source, port, PCAP_BUF_SIZE);
713 strlcat(source, "/", PCAP_BUF_SIZE);
715 else
717 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The host name cannot be NULL.");
718 return -1;
721 if ((name) && (*name))
722 strlcat(source, name, PCAP_BUF_SIZE);
724 return 0;
727 case PCAP_SRC_IFLOCAL:
729 strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
731 if ((name) && (*name))
732 strlcat(source, name, PCAP_BUF_SIZE);
734 return 0;
737 default:
739 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface type is not valid.");
740 return -1;
745 int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char *name, char *errbuf)
747 char *ptr;
748 int ntoken;
749 char tmpname[PCAP_BUF_SIZE];
750 char tmphost[PCAP_BUF_SIZE];
751 char tmpport[PCAP_BUF_SIZE];
752 int tmptype;
754 /* Initialization stuff */
755 tmpname[0] = 0;
756 tmphost[0] = 0;
757 tmpport[0] = 0;
759 if (host)
760 *host = 0;
761 if (port)
762 *port = 0;
763 if (name)
764 *name = 0;
766 /* Look for a 'rpcap://' identifier */
767 if ((ptr = strstr(source, PCAP_SRC_IF_STRING)) != NULL)
769 if (strlen(PCAP_SRC_IF_STRING) == strlen(source))
771 /* The source identifier contains only the 'rpcap://' string. */
772 /* So, this is a local capture. */
773 *type = PCAP_SRC_IFLOCAL;
774 return 0;
777 ptr += strlen(PCAP_SRC_IF_STRING);
779 if (strchr(ptr, '[')) /* This is probably a numeric address */
781 ntoken = sscanf(ptr, "[%[1234567890:.]]:%[^/]/%s", tmphost, tmpport, tmpname);
783 if (ntoken == 1) /* probably the port is missing */
784 ntoken = sscanf(ptr, "[%[1234567890:.]]/%s", tmphost, tmpname);
786 tmptype = PCAP_SRC_IFREMOTE;
788 else
790 ntoken = sscanf(ptr, "%[^/:]:%[^/]/%s", tmphost, tmpport, tmpname);
792 if (ntoken == 1)
795 * This can be due to two reasons:
796 * - we want a remote capture, but the network port is missing
797 * - we want to do a local capture
798 * To distinguish between the two, we look for the '/' char
800 if (strchr(ptr, '/'))
802 /* We're on a remote capture */
803 sscanf(ptr, "%[^/]/%s", tmphost, tmpname);
804 tmptype = PCAP_SRC_IFREMOTE;
806 else
808 /* We're on a local capture */
809 if (*ptr)
810 strlcpy(tmpname, ptr, PCAP_BUF_SIZE);
812 /* Clean the host name, since it is a remote capture */
813 /* NOTE: the host name has been assigned in the previous "ntoken= sscanf(...)" line */
814 tmphost[0] = 0;
816 tmptype = PCAP_SRC_IFLOCAL;
819 else
820 tmptype = PCAP_SRC_IFREMOTE;
823 if (host)
824 strlcpy(host, tmphost, PCAP_BUF_SIZE);
825 if (port)
826 strlcpy(port, tmpport, PCAP_BUF_SIZE);
827 if (type)
828 *type = tmptype;
830 if (name)
833 * If the user wants the host name, but it cannot be located into the source string, return error
834 * However, if the user is not interested in the interface name (e.g. if we're called by
835 * pcap_findalldevs_ex(), which does not have interface name, do not return error
837 if (tmpname[0])
839 strlcpy(name, tmpname, PCAP_BUF_SIZE);
841 else
843 if (errbuf)
844 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface name has not been specified in the source string.");
846 return -1;
850 return 0;
853 /* Look for a 'file://' identifier */
854 if ((ptr = strstr(source, PCAP_SRC_FILE_STRING)) != NULL)
856 ptr += strlen(PCAP_SRC_FILE_STRING);
857 if (*ptr)
859 if (name)
860 strlcpy(name, ptr, PCAP_BUF_SIZE);
862 if (type)
863 *type = PCAP_SRC_FILE;
865 return 0;
867 else
869 if (errbuf)
870 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The file name has not been specified in the source string.");
872 return -1;
877 /* Backward compatibility; the user didn't use the 'rpcap://, file://' specifiers */
878 if ((source) && (*source))
880 if (name)
881 strlcpy(name, source, PCAP_BUF_SIZE);
883 if (type)
884 *type = PCAP_SRC_IFLOCAL;
886 return 0;
888 else
890 if (errbuf)
891 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface name has not been specified in the source string.");
893 return -1;
897 pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf)
899 char host[PCAP_BUF_SIZE], port[PCAP_BUF_SIZE], name[PCAP_BUF_SIZE];
900 int type;
901 pcap_t *fp;
902 int result;
904 if (strlen(source) > PCAP_BUF_SIZE)
906 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
907 return NULL;
910 /* determine the type of the source (file, local, remote) */
911 if (pcap_parsesrcstr(source, &type, host, port, name, errbuf) == -1)
912 return NULL;
915 switch (type)
917 case PCAP_SRC_FILE:
918 fp = pcap_open_offline(name, errbuf);
919 break;
921 case PCAP_SRC_IFREMOTE:
922 fp = pcap_create(source, errbuf);
923 if (fp == NULL)
925 return NULL;
929 * Although we already have host, port and iface, we prefer TO PASS only 'pars' to the
930 * pcap_open_remote() so that it has to call the pcap_parsesrcstr() again.
931 * This is less optimized, but much clearer.
934 result = pcap_opensource_remote(fp, auth);
936 if (result != 0)
938 pcap_close(fp);
939 return NULL;
942 struct pcap_md *md; /* structure used when doing a remote live capture */
943 md = (struct pcap_md *) ((u_char*)fp->priv + sizeof(struct pcap_win));
945 fp->snapshot = snaplen;
946 fp->opt.timeout = read_timeout;
947 md->rmt_flags = flags;
948 break;
950 case PCAP_SRC_IFLOCAL:
952 fp = pcap_open_live(name, snaplen, (flags & PCAP_OPENFLAG_PROMISCUOUS), read_timeout, errbuf);
954 #ifdef WIN32
956 * these flags are supported on Windows only
958 if (fp != NULL && fp->adapter != NULL)
960 /* disable loopback capture if requested */
961 if (flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
963 if (!PacketSetLoopbackBehavior(fp->adapter, NPF_DISABLE_LOOPBACK))
965 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unable to disable the capture of loopback packets.");
966 pcap_close(fp);
967 return NULL;
971 /* set mintocopy to zero if requested */
972 if (flags & PCAP_OPENFLAG_MAX_RESPONSIVENESS)
974 if (!PacketSetMinToCopy(fp->adapter, 0))
976 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unable to set max responsiveness.");
977 pcap_close(fp);
978 return NULL;
982 #endif /* WIN32 */
984 break;
986 default:
987 strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
988 return NULL;
990 return fp;
993 struct pcap_samp *pcap_setsampling(pcap_t *p)
995 struct pcap_md *md; /* structure used when doing a remote live capture */
997 md = (struct pcap_md *) ((u_char*)p->priv + sizeof(struct pcap_win));
998 return &(md->rmt_samp);
1001 SOCKET pcap_remoteact_accept(const char *address, const char *port, const char *hostlist, char *connectinghost, struct pcap_rmtauth *auth, char *errbuf)
1003 /* socket-related variables */
1004 struct addrinfo hints; /* temporary struct to keep settings needed to open the new socket */
1005 struct addrinfo *addrinfo; /* keeps the addrinfo chain; required to open a new socket */
1006 struct sockaddr_storage from; /* generic sockaddr_storage variable */
1007 socklen_t fromlen; /* keeps the length of the sockaddr_storage variable */
1008 SOCKET sockctrl; /* keeps the main socket identifier */
1009 struct activehosts *temp, *prev; /* temp var needed to scan he host list chain */
1011 *connectinghost = 0; /* just in case */
1013 /* Prepare to open a new server socket */
1014 memset(&hints, 0, sizeof(struct addrinfo));
1015 /* WARNING Currently it supports only ONE socket family among ipv4 and IPv6 */
1016 hints.ai_family = AF_INET; /* PF_UNSPEC to have both IPv4 and IPv6 server */
1017 hints.ai_flags = AI_PASSIVE; /* Ready to a bind() socket */
1018 hints.ai_socktype = SOCK_STREAM;
1020 /* Warning: this call can be the first one called by the user. */
1021 /* For this reason, we have to initialize the WinSock support. */
1022 if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
1023 return -1;
1025 /* Do the work */
1026 if ((port == NULL) || (port[0] == 0))
1028 if (sock_initaddress(address, RPCAP_DEFAULT_NETPORT_ACTIVE, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
1030 SOCK_ASSERT(errbuf, 1);
1031 return -2;
1034 else
1036 if (sock_initaddress(address, port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
1038 SOCK_ASSERT(errbuf, 1);
1039 return -2;
1044 if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE)) == -1)
1046 SOCK_ASSERT(errbuf, 1);
1047 return -2;
1050 /* Connection creation */
1051 fromlen = sizeof(struct sockaddr_storage);
1053 sockctrl = accept(sockmain, (struct sockaddr *) &from, &fromlen);
1055 /* We're not using sock_close, since we do not want to send a shutdown */
1056 /* (which is not allowed on a non-connected socket) */
1057 closesocket(sockmain);
1058 sockmain = 0;
1060 if (sockctrl == -1)
1062 sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
1063 return -2;
1066 /* Get the numeric for of the name of the connecting host */
1067 if (getnameinfo((struct sockaddr *) &from, fromlen, connectinghost, RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST))
1069 sock_geterror("getnameinfo(): ", errbuf, PCAP_ERRBUF_SIZE);
1070 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
1071 sock_close(sockctrl, NULL, 0);
1072 return -1;
1075 /* checks if the connecting host is among the ones allowed */
1076 if (sock_check_hostlist((char *)hostlist, RPCAP_HOSTLIST_SEP, &from, errbuf, PCAP_ERRBUF_SIZE) < 0)
1078 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
1079 sock_close(sockctrl, NULL, 0);
1080 return -1;
1083 /* Send authentication to the remote machine */
1084 if (rpcap_sendauth(sockctrl, auth, errbuf) == -1)
1086 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
1087 sock_close(sockctrl, NULL, 0);
1088 return -3;
1091 /* Checks that this host does not already have a cntrl connection in place */
1093 /* Initialize pointers */
1094 temp = activeHosts;
1095 prev = NULL;
1097 while (temp)
1099 /* This host already has an active connection in place, so I don't have to update the host list */
1100 if (sock_cmpaddr(&temp->host, &from) == 0)
1101 return sockctrl;
1103 prev = temp;
1104 temp = temp->next;
1107 /* The host does not exist in the list; so I have to update the list */
1108 if (prev)
1110 prev->next = (struct activehosts *) malloc(sizeof(struct activehosts));
1111 temp = prev->next;
1113 else
1115 activeHosts = (struct activehosts *) malloc(sizeof(struct activehosts));
1116 temp = activeHosts;
1119 if (temp == NULL)
1121 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
1122 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
1123 sock_close(sockctrl, NULL, 0);
1124 return -1;
1127 memcpy(&temp->host, &from, fromlen);
1128 temp->sockctrl = sockctrl;
1129 temp->next = NULL;
1131 return sockctrl;
1134 int pcap_remoteact_close(const char *host, char *errbuf)
1136 struct activehosts *temp, *prev; /* temp var needed to scan the host list chain */
1137 struct addrinfo hints, *addrinfo, *ai_next; /* temp var needed to translate between hostname to its address */
1138 int retval;
1140 temp = activeHosts;
1141 prev = NULL;
1143 /* retrieve the network address corresponding to 'host' */
1144 addrinfo = NULL;
1145 memset(&hints, 0, sizeof(struct addrinfo));
1146 hints.ai_family = PF_UNSPEC;
1147 hints.ai_socktype = SOCK_STREAM;
1149 retval = getaddrinfo(host, "0", &hints, &addrinfo);
1150 if (retval != 0)
1152 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "getaddrinfo() %s", gai_strerror(retval));
1153 return -1;
1156 while (temp)
1158 ai_next = addrinfo;
1159 while (ai_next)
1161 if (sock_cmpaddr(&temp->host, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
1163 struct rpcap_header header;
1165 /* Close this connection */
1166 rpcap_createhdr(&header, RPCAP_MSG_CLOSE, 0, 0);
1168 /* I don't check for errors, since I'm going to close everything */
1169 sock_send(temp->sockctrl, (char *)&header, sizeof(struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE);
1171 if (sock_close(temp->sockctrl, errbuf, PCAP_ERRBUF_SIZE))
1173 /* To avoid inconsistencies in the number of sock_init() */
1174 sock_cleanup();
1176 return -1;
1179 if (prev)
1180 prev->next = temp->next;
1181 else
1182 activeHosts = temp->next;
1184 freeaddrinfo(addrinfo);
1186 free(temp);
1188 /* To avoid inconsistencies in the number of sock_init() */
1189 sock_cleanup();
1191 return 0;
1194 ai_next = ai_next->ai_next;
1196 prev = temp;
1197 temp = temp->next;
1200 if (addrinfo)
1201 freeaddrinfo(addrinfo);
1203 /* To avoid inconsistencies in the number of sock_init() */
1204 sock_cleanup();
1206 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The host you want to close the active connection is not known");
1207 return -1;
1210 void pcap_remoteact_cleanup(void)
1212 /* Very dirty, but it works */
1213 if (sockmain)
1215 closesocket(sockmain);
1217 /* To avoid inconsistencies in the number of sock_init() */
1218 sock_cleanup();
1223 int pcap_remoteact_list(char *hostlist, char sep, int size, char *errbuf)
1225 struct activehosts *temp; /* temp var needed to scan the host list chain */
1226 size_t len;
1227 char hoststr[RPCAP_HOSTLIST_SIZE + 1];
1229 temp = activeHosts;
1231 len = 0;
1232 *hostlist = 0;
1234 while (temp)
1236 /*int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen) */
1238 /* Get the numeric form of the name of the connecting host */
1239 if (sock_getascii_addrport((struct sockaddr_storage *) &temp->host, hoststr,
1240 RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST, errbuf, PCAP_ERRBUF_SIZE) != -1)
1241 /* if (getnameinfo( (struct sockaddr *) &temp->host, sizeof (struct sockaddr_storage), hoststr, */
1242 /* RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST) ) */
1244 /* sock_geterror("getnameinfo(): ", errbuf, PCAP_ERRBUF_SIZE); */
1245 return -1;
1248 len = len + strlen(hoststr) + 1 /* the separator */;
1250 if ((size < 0) || (len >= (size_t)size))
1252 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The string you provided is not able to keep "
1253 "the hostnames for all the active connections");
1254 return -1;
1257 strlcat(hostlist, hoststr, PCAP_ERRBUF_SIZE);
1258 hostlist[len - 1] = sep;
1259 hostlist[len] = 0;
1261 temp = temp->next;
1264 return 0;