]> The Tcpdump Group git mirrors - libpcap/blob - pcap-new.c
Move the pcap_findalldevs_ex code for rpcap into pcap-rpcap.c
[libpcap] / pcap-new.c
1 /*
2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
19 *
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.
31 *
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
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
44
45 #ifndef WIN32
46 #include <dirent.h> // for readdir
47 #endif
48
49 /* Keeps a list of all the opened connections in the active mode. */
50 extern struct activehosts *activeHosts;
51
52 /*
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.
55 */
56 SOCKET sockmain;
57
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"
62
63 /* String identifier to be used in the pcap_findalldevs_ex() */
64 #define PCAP_TEXT_SOURCE_ON_LOCAL_HOST "on local host"
65
66 /****************************************************
67 * *
68 * Function bodies *
69 * *
70 ****************************************************/
71
72 int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf)
73 {
74 int type;
75 char name[PCAP_BUF_SIZE], path[PCAP_BUF_SIZE], filename[PCAP_BUF_SIZE];
76 pcap_t *fp;
77 char tmpstring[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */
78 pcap_if_t *dev; /* Previous device into the pcap_if_t chain */
79
80 (*alldevs) = NULL;
81
82 if (strlen(source) > PCAP_BUF_SIZE)
83 {
84 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
85 return -1;
86 }
87
88 /*
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.
96 */
97 if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
98 return -1;
99
100 switch (type)
101 {
102 case PCAP_SRC_IFLOCAL:
103 if (pcap_parsesrcstr(source, &type, NULL, NULL, NULL, errbuf) == -1)
104 return -1;
105
106 /* Initialize temporary string */
107 tmpstring[PCAP_BUF_SIZE] = 0;
108
109 /* The user wants to retrieve adapters from a local host */
110 if (pcap_findalldevs(alldevs, errbuf) == -1)
111 return -1;
112
113 if ((alldevs == NULL) || (*alldevs == NULL))
114 {
115 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
116 "No interfaces found! Make sure libpcap/WinPcap is properly installed"
117 " on the local machine.");
118 return -1;
119 }
120
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 */
123 dev = *alldevs;
124 while (dev)
125 {
126 /* Create the new device identifier */
127 if (pcap_createsrcstr(tmpstring, PCAP_SRC_IFLOCAL, NULL, NULL, dev->name, errbuf) == -1)
128 return -1;
129
130 /* Delete the old pointer */
131 free(dev->name);
132
133 /* Make a copy of the new device identifier */
134 dev->name = strdup(tmpstring);
135 if (dev->name == NULL)
136 {
137 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
138 return -1;
139 }
140
141 /* Create the new device description */
142 if ((dev->description == NULL) || (dev->description[0] == 0))
143 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_ADAPTER,
144 dev->name, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
145 else
146 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_ADAPTER,
147 dev->description, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
148
149 /* Delete the old pointer */
150 free(dev->description);
151
152 /* Make a copy of the description */
153 dev->description = strdup(tmpstring);
154 if (dev->description == NULL)
155 {
156 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
157 return -1;
158 }
159
160 dev = dev->next;
161 }
162
163 return 0;
164
165 case PCAP_SRC_FILE:
166 {
167 size_t stringlen;
168 #ifdef WIN32
169 WIN32_FIND_DATA filedata;
170 HANDLE filehandle;
171 #else
172 struct dirent *filedata;
173 DIR *unixdir;
174 #endif
175
176 if (pcap_parsesrcstr(source, &type, NULL, NULL, name, errbuf) == -1)
177 return -1;
178
179 /* Check that the filename is correct */
180 stringlen = strlen(name);
181
182 /* The directory must end with '\' in Win32 and '/' in UNIX */
183 #ifdef WIN32
184 #define ENDING_CHAR '\\'
185 #else
186 #define ENDING_CHAR '/'
187 #endif
188
189 if (name[stringlen - 1] != ENDING_CHAR)
190 {
191 name[stringlen] = ENDING_CHAR;
192 name[stringlen + 1] = 0;
193
194 stringlen++;
195 }
196
197 /* Save the path for future reference */
198 pcap_snprintf(path, sizeof(path), "%s", name);
199
200 #ifdef WIN32
201 /* To perform directory listing, Win32 must have an 'asterisk' as ending char */
202 if (name[stringlen - 1] != '*')
203 {
204 name[stringlen] = '*';
205 name[stringlen + 1] = 0;
206 }
207
208 filehandle = FindFirstFile(name, &filedata);
209
210 if (filehandle == INVALID_HANDLE_VALUE)
211 {
212 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
213 return -1;
214 }
215
216 #else
217 /* opening the folder */
218 unixdir= opendir(path);
219
220 /* get the first file into it */
221 filedata= readdir(unixdir);
222
223 if (filedata == NULL)
224 {
225 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error when listing files: does folder '%s' exist?", path);
226 return -1;
227 }
228 #endif
229
230 do
231 {
232
233 #ifdef WIN32
234 pcap_snprintf(filename, sizeof(filename), "%s%s", path, filedata.cFileName);
235 #else
236 pcap_snprintf(filename, sizeof(filename), "%s%s", path, filedata->d_name);
237 #endif
238
239 fp = pcap_open_offline(filename, errbuf);
240
241 if (fp)
242 {
243 /* allocate the main structure */
244 if (*alldevs == NULL) /* This is in case it is the first file */
245 {
246 (*alldevs) = (pcap_if_t *)malloc(sizeof(pcap_if_t));
247 dev = (*alldevs);
248 }
249 else
250 {
251 dev->next = (pcap_if_t *)malloc(sizeof(pcap_if_t));
252 dev = dev->next;
253 }
254
255 /* check that the malloc() didn't fail */
256 if (dev == NULL)
257 {
258 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
259 return -1;
260 }
261
262 /* Initialize the structure to 'zero' */
263 memset(dev, 0, sizeof(pcap_if_t));
264
265 /* Create the new source identifier */
266 if (pcap_createsrcstr(tmpstring, PCAP_SRC_FILE, NULL, NULL, filename, errbuf) == -1)
267 return -1;
268
269 stringlen = strlen(tmpstring);
270
271 dev->name = (char *)malloc(stringlen + 1);
272 if (dev->name == NULL)
273 {
274 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
275 return -1;
276 }
277
278 strlcpy(dev->name, tmpstring, stringlen);
279
280 dev->name[stringlen] = 0;
281
282 /* Create the description */
283 pcap_snprintf(tmpstring, sizeof(tmpstring) - 1, "%s '%s' %s", PCAP_TEXT_SOURCE_FILE,
284 filename, PCAP_TEXT_SOURCE_ON_LOCAL_HOST);
285
286 stringlen = strlen(tmpstring);
287
288 dev->description = (char *)malloc(stringlen + 1);
289
290 if (dev->description == NULL)
291 {
292 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
293 return -1;
294 }
295
296 /* Copy the new device description into the correct memory location */
297 strlcpy(dev->description, tmpstring, stringlen + 1);
298
299 pcap_close(fp);
300 }
301 }
302 #ifdef WIN32
303 while (FindNextFile(filehandle, &filedata) != 0);
304 #else
305 while ( (filedata= readdir(unixdir)) != NULL);
306 #endif
307
308
309 #ifdef WIN32
310 /* Close the search handle. */
311 FindClose(filehandle);
312 #endif
313
314 return 0;
315 }
316
317 case PCAP_SRC_IFREMOTE:
318 return pcap_findalldevs_ex_remote(source, auth, alldevs, errbuf);
319
320 default:
321 strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
322 return -1;
323 }
324 }
325
326 int pcap_createsrcstr(char *source, int type, const char *host, const char *port, const char *name, char *errbuf)
327 {
328 switch (type)
329 {
330 case PCAP_SRC_FILE:
331 {
332 strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE);
333 if ((name) && (*name))
334 {
335 strlcat(source, name, PCAP_BUF_SIZE);
336 return 0;
337 }
338 else
339 {
340 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The file name cannot be NULL.");
341 return -1;
342 }
343 }
344
345 case PCAP_SRC_IFREMOTE:
346 {
347 strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
348 if ((host) && (*host))
349 {
350 if ((strcspn(host, "aAbBcCdDeEfFgGhHjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ")) == strlen(host))
351 {
352 /* the host name does not contains alphabetic chars. So, it is a numeric address */
353 /* In this case we have to include it between square brackets */
354 strlcat(source, "[", PCAP_BUF_SIZE);
355 strlcat(source, host, PCAP_BUF_SIZE);
356 strlcat(source, "]", PCAP_BUF_SIZE);
357 }
358 else
359 strlcat(source, host, PCAP_BUF_SIZE);
360
361 if ((port) && (*port))
362 {
363 strlcat(source, ":", PCAP_BUF_SIZE);
364 strlcat(source, port, PCAP_BUF_SIZE);
365 }
366
367 strlcat(source, "/", PCAP_BUF_SIZE);
368 }
369 else
370 {
371 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The host name cannot be NULL.");
372 return -1;
373 }
374
375 if ((name) && (*name))
376 strlcat(source, name, PCAP_BUF_SIZE);
377
378 return 0;
379 }
380
381 case PCAP_SRC_IFLOCAL:
382 {
383 strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
384
385 if ((name) && (*name))
386 strlcat(source, name, PCAP_BUF_SIZE);
387
388 return 0;
389 }
390
391 default:
392 {
393 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface type is not valid.");
394 return -1;
395 }
396 }
397 }
398
399 int pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char *name, char *errbuf)
400 {
401 char *ptr;
402 int ntoken;
403 char tmpname[PCAP_BUF_SIZE];
404 char tmphost[PCAP_BUF_SIZE];
405 char tmpport[PCAP_BUF_SIZE];
406 int tmptype;
407
408 /* Initialization stuff */
409 tmpname[0] = 0;
410 tmphost[0] = 0;
411 tmpport[0] = 0;
412
413 if (host)
414 *host = 0;
415 if (port)
416 *port = 0;
417 if (name)
418 *name = 0;
419
420 /* Look for a 'rpcap://' identifier */
421 if ((ptr = strstr(source, PCAP_SRC_IF_STRING)) != NULL)
422 {
423 if (strlen(PCAP_SRC_IF_STRING) == strlen(source))
424 {
425 /* The source identifier contains only the 'rpcap://' string. */
426 /* So, this is a local capture. */
427 *type = PCAP_SRC_IFLOCAL;
428 return 0;
429 }
430
431 ptr += strlen(PCAP_SRC_IF_STRING);
432
433 if (strchr(ptr, '[')) /* This is probably a numeric address */
434 {
435 ntoken = sscanf(ptr, "[%[1234567890:.]]:%[^/]/%s", tmphost, tmpport, tmpname);
436
437 if (ntoken == 1) /* probably the port is missing */
438 ntoken = sscanf(ptr, "[%[1234567890:.]]/%s", tmphost, tmpname);
439
440 tmptype = PCAP_SRC_IFREMOTE;
441 }
442 else
443 {
444 ntoken = sscanf(ptr, "%[^/:]:%[^/]/%s", tmphost, tmpport, tmpname);
445
446 if (ntoken == 1)
447 {
448 /*
449 * This can be due to two reasons:
450 * - we want a remote capture, but the network port is missing
451 * - we want to do a local capture
452 * To distinguish between the two, we look for the '/' char
453 */
454 if (strchr(ptr, '/'))
455 {
456 /* We're on a remote capture */
457 sscanf(ptr, "%[^/]/%s", tmphost, tmpname);
458 tmptype = PCAP_SRC_IFREMOTE;
459 }
460 else
461 {
462 /* We're on a local capture */
463 if (*ptr)
464 strlcpy(tmpname, ptr, PCAP_BUF_SIZE);
465
466 /* Clean the host name, since it is a remote capture */
467 /* NOTE: the host name has been assigned in the previous "ntoken= sscanf(...)" line */
468 tmphost[0] = 0;
469
470 tmptype = PCAP_SRC_IFLOCAL;
471 }
472 }
473 else
474 tmptype = PCAP_SRC_IFREMOTE;
475 }
476
477 if (host)
478 strlcpy(host, tmphost, PCAP_BUF_SIZE);
479 if (port)
480 strlcpy(port, tmpport, PCAP_BUF_SIZE);
481 if (type)
482 *type = tmptype;
483
484 if (name)
485 {
486 /*
487 * If the user wants the host name, but it cannot be located into the source string, return error
488 * However, if the user is not interested in the interface name (e.g. if we're called by
489 * pcap_findalldevs_ex(), which does not have interface name, do not return error
490 */
491 if (tmpname[0])
492 {
493 strlcpy(name, tmpname, PCAP_BUF_SIZE);
494 }
495 else
496 {
497 if (errbuf)
498 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface name has not been specified in the source string.");
499
500 return -1;
501 }
502 }
503
504 return 0;
505 }
506
507 /* Look for a 'file://' identifier */
508 if ((ptr = strstr(source, PCAP_SRC_FILE_STRING)) != NULL)
509 {
510 ptr += strlen(PCAP_SRC_FILE_STRING);
511 if (*ptr)
512 {
513 if (name)
514 strlcpy(name, ptr, PCAP_BUF_SIZE);
515
516 if (type)
517 *type = PCAP_SRC_FILE;
518
519 return 0;
520 }
521 else
522 {
523 if (errbuf)
524 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The file name has not been specified in the source string.");
525
526 return -1;
527 }
528
529 }
530
531 /* Backward compatibility; the user didn't use the 'rpcap://, file://' specifiers */
532 if ((source) && (*source))
533 {
534 if (name)
535 strlcpy(name, source, PCAP_BUF_SIZE);
536
537 if (type)
538 *type = PCAP_SRC_IFLOCAL;
539
540 return 0;
541 }
542 else
543 {
544 if (errbuf)
545 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The interface name has not been specified in the source string.");
546
547 return -1;
548 }
549 };
550
551 pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf)
552 {
553 char name[PCAP_BUF_SIZE];
554 int type;
555 pcap_t *fp;
556
557 if (strlen(source) > PCAP_BUF_SIZE)
558 {
559 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The source string is too long. Cannot handle it correctly.");
560 return NULL;
561 }
562
563 /*
564 * Determine the type of the source (file, local, remote) and,
565 * if it's file or local, the name of the file or capture device.
566 */
567 if (pcap_parsesrcstr(source, &type, NULL, NULL, name, errbuf) == -1)
568 return NULL;
569
570 switch (type)
571 {
572 case PCAP_SRC_FILE:
573 fp = pcap_open_offline(name, errbuf);
574 break;
575
576 case PCAP_SRC_IFREMOTE:
577 /*
578 * Although we already have host, port and iface, we prefer
579 * to pass only 'source' to pcap_open_rpcap(), so that it
580 * has to call pcap_parsesrcstr() again.
581 * This is less optimized, but much clearer.
582 */
583 fp = pcap_open_rpcap(source, snaplen, flags, read_timeout, auth, errbuf);
584 break;
585
586 case PCAP_SRC_IFLOCAL:
587 fp = pcap_open_live(name, snaplen, (flags & PCAP_OPENFLAG_PROMISCUOUS), read_timeout, errbuf);
588
589 #ifdef WIN32
590 /*
591 * these flags are supported on Windows only
592 */
593 if (fp != NULL && fp->adapter != NULL)
594 {
595 /* disable loopback capture if requested */
596 if (flags & PCAP_OPENFLAG_NOCAPTURE_LOCAL)
597 {
598 if (!PacketSetLoopbackBehavior(fp->adapter, NPF_DISABLE_LOOPBACK))
599 {
600 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unable to disable the capture of loopback packets.");
601 pcap_close(fp);
602 return NULL;
603 }
604 }
605
606 /* set mintocopy to zero if requested */
607 if (flags & PCAP_OPENFLAG_MAX_RESPONSIVENESS)
608 {
609 if (!PacketSetMinToCopy(fp->adapter, 0))
610 {
611 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unable to set max responsiveness.");
612 pcap_close(fp);
613 return NULL;
614 }
615 }
616 }
617 #endif /* WIN32 */
618
619 break;
620
621 default:
622 strlcpy(errbuf, "Source type not supported", PCAP_ERRBUF_SIZE);
623 return NULL;
624 }
625 return fp;
626 }
627
628 struct pcap_samp *pcap_setsampling(pcap_t *p)
629 {
630 return &p->rmt_samp;
631 }
632
633 SOCKET pcap_remoteact_accept(const char *address, const char *port, const char *hostlist, char *connectinghost, struct pcap_rmtauth *auth, char *errbuf)
634 {
635 /* socket-related variables */
636 struct addrinfo hints; /* temporary struct to keep settings needed to open the new socket */
637 struct addrinfo *addrinfo; /* keeps the addrinfo chain; required to open a new socket */
638 struct sockaddr_storage from; /* generic sockaddr_storage variable */
639 socklen_t fromlen; /* keeps the length of the sockaddr_storage variable */
640 SOCKET sockctrl; /* keeps the main socket identifier */
641 struct activehosts *temp, *prev; /* temp var needed to scan he host list chain */
642
643 *connectinghost = 0; /* just in case */
644
645 /* Prepare to open a new server socket */
646 memset(&hints, 0, sizeof(struct addrinfo));
647 /* WARNING Currently it supports only ONE socket family among ipv4 and IPv6 */
648 hints.ai_family = AF_INET; /* PF_UNSPEC to have both IPv4 and IPv6 server */
649 hints.ai_flags = AI_PASSIVE; /* Ready to a bind() socket */
650 hints.ai_socktype = SOCK_STREAM;
651
652 /* Warning: this call can be the first one called by the user. */
653 /* For this reason, we have to initialize the WinSock support. */
654 if (sock_init(errbuf, PCAP_ERRBUF_SIZE) == -1)
655 return -1;
656
657 /* Do the work */
658 if ((port == NULL) || (port[0] == 0))
659 {
660 if (sock_initaddress(address, RPCAP_DEFAULT_NETPORT_ACTIVE, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
661 {
662 SOCK_ASSERT(errbuf, 1);
663 return -2;
664 }
665 }
666 else
667 {
668 if (sock_initaddress(address, port, &hints, &addrinfo, errbuf, PCAP_ERRBUF_SIZE) == -1)
669 {
670 SOCK_ASSERT(errbuf, 1);
671 return -2;
672 }
673 }
674
675
676 if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE)) == -1)
677 {
678 SOCK_ASSERT(errbuf, 1);
679 return -2;
680 }
681
682 /* Connection creation */
683 fromlen = sizeof(struct sockaddr_storage);
684
685 sockctrl = accept(sockmain, (struct sockaddr *) &from, &fromlen);
686
687 /* We're not using sock_close, since we do not want to send a shutdown */
688 /* (which is not allowed on a non-connected socket) */
689 closesocket(sockmain);
690 sockmain = 0;
691
692 if (sockctrl == -1)
693 {
694 sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
695 return -2;
696 }
697
698 /* Get the numeric for of the name of the connecting host */
699 if (getnameinfo((struct sockaddr *) &from, fromlen, connectinghost, RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST))
700 {
701 sock_geterror("getnameinfo(): ", errbuf, PCAP_ERRBUF_SIZE);
702 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
703 sock_close(sockctrl, NULL, 0);
704 return -1;
705 }
706
707 /* checks if the connecting host is among the ones allowed */
708 if (sock_check_hostlist((char *)hostlist, RPCAP_HOSTLIST_SEP, &from, errbuf, PCAP_ERRBUF_SIZE) < 0)
709 {
710 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
711 sock_close(sockctrl, NULL, 0);
712 return -1;
713 }
714
715 /* Send authentication to the remote machine */
716 if (rpcap_sendauth(sockctrl, auth, errbuf) == -1)
717 {
718 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
719 sock_close(sockctrl, NULL, 0);
720 return -3;
721 }
722
723 /* Checks that this host does not already have a cntrl connection in place */
724
725 /* Initialize pointers */
726 temp = activeHosts;
727 prev = NULL;
728
729 while (temp)
730 {
731 /* This host already has an active connection in place, so I don't have to update the host list */
732 if (sock_cmpaddr(&temp->host, &from) == 0)
733 return sockctrl;
734
735 prev = temp;
736 temp = temp->next;
737 }
738
739 /* The host does not exist in the list; so I have to update the list */
740 if (prev)
741 {
742 prev->next = (struct activehosts *) malloc(sizeof(struct activehosts));
743 temp = prev->next;
744 }
745 else
746 {
747 activeHosts = (struct activehosts *) malloc(sizeof(struct activehosts));
748 temp = activeHosts;
749 }
750
751 if (temp == NULL)
752 {
753 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno));
754 rpcap_senderror(sockctrl, errbuf, PCAP_ERR_REMOTEACCEPT, NULL);
755 sock_close(sockctrl, NULL, 0);
756 return -1;
757 }
758
759 memcpy(&temp->host, &from, fromlen);
760 temp->sockctrl = sockctrl;
761 temp->next = NULL;
762
763 return sockctrl;
764 }
765
766 int pcap_remoteact_close(const char *host, char *errbuf)
767 {
768 struct activehosts *temp, *prev; /* temp var needed to scan the host list chain */
769 struct addrinfo hints, *addrinfo, *ai_next; /* temp var needed to translate between hostname to its address */
770 int retval;
771
772 temp = activeHosts;
773 prev = NULL;
774
775 /* retrieve the network address corresponding to 'host' */
776 addrinfo = NULL;
777 memset(&hints, 0, sizeof(struct addrinfo));
778 hints.ai_family = PF_UNSPEC;
779 hints.ai_socktype = SOCK_STREAM;
780
781 retval = getaddrinfo(host, "0", &hints, &addrinfo);
782 if (retval != 0)
783 {
784 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "getaddrinfo() %s", gai_strerror(retval));
785 return -1;
786 }
787
788 while (temp)
789 {
790 ai_next = addrinfo;
791 while (ai_next)
792 {
793 if (sock_cmpaddr(&temp->host, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
794 {
795 struct rpcap_header header;
796
797 /* Close this connection */
798 rpcap_createhdr(&header, RPCAP_MSG_CLOSE, 0, 0);
799
800 /* I don't check for errors, since I'm going to close everything */
801 sock_send(temp->sockctrl, (char *)&header, sizeof(struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE);
802
803 if (sock_close(temp->sockctrl, errbuf, PCAP_ERRBUF_SIZE))
804 {
805 /* To avoid inconsistencies in the number of sock_init() */
806 sock_cleanup();
807
808 return -1;
809 }
810
811 if (prev)
812 prev->next = temp->next;
813 else
814 activeHosts = temp->next;
815
816 freeaddrinfo(addrinfo);
817
818 free(temp);
819
820 /* To avoid inconsistencies in the number of sock_init() */
821 sock_cleanup();
822
823 return 0;
824 }
825
826 ai_next = ai_next->ai_next;
827 }
828 prev = temp;
829 temp = temp->next;
830 }
831
832 if (addrinfo)
833 freeaddrinfo(addrinfo);
834
835 /* To avoid inconsistencies in the number of sock_init() */
836 sock_cleanup();
837
838 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The host you want to close the active connection is not known");
839 return -1;
840 }
841
842 void pcap_remoteact_cleanup(void)
843 {
844 /* Very dirty, but it works */
845 if (sockmain)
846 {
847 closesocket(sockmain);
848
849 /* To avoid inconsistencies in the number of sock_init() */
850 sock_cleanup();
851 }
852
853 }
854
855 int pcap_remoteact_list(char *hostlist, char sep, int size, char *errbuf)
856 {
857 struct activehosts *temp; /* temp var needed to scan the host list chain */
858 size_t len;
859 char hoststr[RPCAP_HOSTLIST_SIZE + 1];
860
861 temp = activeHosts;
862
863 len = 0;
864 *hostlist = 0;
865
866 while (temp)
867 {
868 /*int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen) */
869
870 /* Get the numeric form of the name of the connecting host */
871 if (sock_getascii_addrport((struct sockaddr_storage *) &temp->host, hoststr,
872 RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST, errbuf, PCAP_ERRBUF_SIZE) != -1)
873 /* if (getnameinfo( (struct sockaddr *) &temp->host, sizeof (struct sockaddr_storage), hoststr, */
874 /* RPCAP_HOSTLIST_SIZE, NULL, 0, NI_NUMERICHOST) ) */
875 {
876 /* sock_geterror("getnameinfo(): ", errbuf, PCAP_ERRBUF_SIZE); */
877 return -1;
878 }
879
880 len = len + strlen(hoststr) + 1 /* the separator */;
881
882 if ((size < 0) || (len >= (size_t)size))
883 {
884 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "The string you provided is not able to keep "
885 "the hostnames for all the active connections");
886 return -1;
887 }
888
889 strlcat(hostlist, hoststr, PCAP_ERRBUF_SIZE);
890 hostlist[len - 1] = sep;
891 hostlist[len] = 0;
892
893 temp = temp->next;
894 }
895
896 return 0;
897 }