]> The Tcpdump Group git mirrors - libpcap/blob - inet.c
pcap_lookupdev() converts the adapter list to unicode for backward compatibility.
[libpcap] / inet.c
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char rcsid[] =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.57 2003-09-22 11:51:37 risso Exp $ (LBL)";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #ifdef WIN32
45 #include <pcap-stdinc.h>
46 #else /* WIN32 */
47
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/ioctl.h>
51 #include <sys/socket.h>
52 #ifdef HAVE_SYS_SOCKIO_H
53 #include <sys/sockio.h>
54 #endif
55 #include <sys/time.h> /* concession to AIX */
56
57 struct mbuf; /* Squelch compiler warnings on some platforms for */
58 struct rtentry; /* declarations in <net/if.h> */
59 #include <net/if.h>
60 #include <netinet/in.h>
61 #endif /* WIN32 */
62
63 #include <ctype.h>
64 #include <errno.h>
65 #include <memory.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #ifndef WIN32
70 #include <unistd.h>
71 #endif /* WIN32 */
72 #ifdef HAVE_LIMITS_H
73 #include <limits.h>
74 #else
75 #define INT_MAX 2147483647
76 #endif
77 #ifdef HAVE_IFADDRS_H
78 #include <ifaddrs.h>
79 #endif
80
81 #include "pcap-int.h"
82
83 #ifdef HAVE_OS_PROTO_H
84 #include "os-proto.h"
85 #endif
86
87 /* Not all systems have IFF_LOOPBACK */
88 #ifdef IFF_LOOPBACK
89 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
90 #else
91 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
92 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
93 #endif
94
95 struct sockaddr *
96 dup_sockaddr(struct sockaddr *sa, size_t sa_length)
97 {
98 struct sockaddr *newsa;
99
100 if ((newsa = malloc(sa_length)) == NULL)
101 return (NULL);
102 return (memcpy(newsa, sa, sa_length));
103 }
104
105 static int
106 get_instance(const char *name)
107 {
108 const char *cp, *endcp;
109 int n;
110
111 if (strcmp(name, "any") == 0) {
112 /*
113 * Give the "any" device an artificially high instance
114 * number, so it shows up after all other non-loopback
115 * interfaces.
116 */
117 return INT_MAX;
118 }
119
120 endcp = name + strlen(name);
121 for (cp = name; cp < endcp && !isdigit((unsigned char)*cp); ++cp)
122 continue;
123
124 if (isdigit((unsigned char)*cp))
125 n = atoi(cp);
126 else
127 n = 0;
128 return (n);
129 }
130
131 int
132 add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
133 u_int flags, const char *description, char *errbuf)
134 {
135 pcap_t *p;
136 pcap_if_t *curdev, *prevdev, *nextdev;
137 int this_instance;
138
139 /*
140 * Is there already an entry in the list for this interface?
141 */
142 for (curdev = *alldevs; curdev != NULL; curdev = curdev->next) {
143 if (strcmp(name, curdev->name) == 0)
144 break; /* yes, we found it */
145 }
146 if (curdev == NULL) {
147 /*
148 * No, we didn't find it.
149 * Allocate a new entry.
150 */
151 curdev = malloc(sizeof(pcap_if_t));
152 if (curdev == NULL) {
153 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
154 "malloc: %s", pcap_strerror(errno));
155 return (-1);
156 }
157
158 /*
159 * Fill in the entry.
160 */
161 curdev->next = NULL;
162 curdev->name = malloc(strlen(name) + 1);
163 strcpy(curdev->name, name);
164 if (description != NULL) {
165 /*
166 * We have a description for this interface.
167 */
168 curdev->description = malloc(strlen(description) + 1);
169 strcpy(curdev->description, description);
170 } else {
171 /*
172 * We don't.
173 */
174 curdev->description = NULL;
175 }
176 curdev->addresses = NULL; /* list starts out as empty */
177 curdev->flags = 0;
178 if (ISLOOPBACK(name, flags))
179 curdev->flags |= PCAP_IF_LOOPBACK;
180
181 /*
182 * Add it to the list, in the appropriate location.
183 * First, get the instance number of this interface.
184 */
185 this_instance = get_instance(name);
186
187 /*
188 * Now look for the last interface with an instance number
189 * less than or equal to the new interface's instance
190 * number - except that non-loopback interfaces are
191 * arbitrarily treated as having interface numbers less
192 * than those of loopback interfaces, so the loopback
193 * interfaces are put at the end of the list.
194 *
195 * We start with "prevdev" being NULL, meaning we're before
196 * the first element in the list.
197 */
198 prevdev = NULL;
199 for (;;) {
200 /*
201 * Get the interface after this one.
202 */
203 if (prevdev == NULL) {
204 /*
205 * The next element is the first element.
206 */
207 nextdev = *alldevs;
208 } else
209 nextdev = prevdev->next;
210
211 /*
212 * Are we at the end of the list?
213 */
214 if (nextdev == NULL) {
215 /*
216 * Yes - we have to put the new entry
217 * after "prevdev".
218 */
219 break;
220 }
221
222 /*
223 * Is the new interface a non-loopback interface
224 * and the next interface a loopback interface?
225 */
226 if (!(curdev->flags & PCAP_IF_LOOPBACK) &&
227 (nextdev->flags & PCAP_IF_LOOPBACK)) {
228 /*
229 * Yes, we should put the new entry
230 * before "nextdev", i.e. after "prevdev".
231 */
232 break;
233 }
234
235 /*
236 * Is the new interface's instance number less
237 * than the next interface's instance number,
238 * and is it the case that the new interface is a
239 * non-loopback interface or the next interface is
240 * a loopback interface?
241 *
242 * (The goal of both loopback tests is to make
243 * sure that we never put a loopback interface
244 * before any non-loopback interface and that we
245 * always put a non-loopback interface before all
246 * loopback interfaces.)
247 */
248 if (this_instance < get_instance(nextdev->name) &&
249 (!(curdev->flags & PCAP_IF_LOOPBACK) ||
250 (nextdev->flags & PCAP_IF_LOOPBACK))) {
251 /*
252 * Yes - we should put the new entry
253 * before "nextdev", i.e. after "prevdev".
254 */
255 break;
256 }
257
258 prevdev = nextdev;
259 }
260
261 /*
262 * Insert before "nextdev".
263 */
264 curdev->next = nextdev;
265
266 /*
267 * Insert after "prevdev" - unless "prevdev" is null,
268 * in which case this is the first interface.
269 */
270 if (prevdev == NULL) {
271 /*
272 * This is the first interface. Pass back a
273 * pointer to it, and put "curdev" before
274 * "nextdev".
275 */
276 *alldevs = curdev;
277 } else
278 prevdev->next = curdev;
279 }
280
281 *curdev_ret = curdev;
282 return (0);
283 }
284
285 int
286 add_addr_to_iflist(pcap_if_t **alldevs, char *name, u_int flags,
287 struct sockaddr *addr, size_t addr_size,
288 struct sockaddr *netmask, size_t netmask_size,
289 struct sockaddr *broadaddr, size_t broadaddr_size,
290 struct sockaddr *dstaddr, size_t dstaddr_size,
291 char *errbuf)
292 {
293 pcap_if_t *curdev;
294 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
295
296 if (add_or_find_if(&curdev, alldevs, name, flags, NULL, errbuf) == -1) {
297 /*
298 * Error - give up.
299 */
300 return (-1);
301 }
302 if (curdev == NULL) {
303 /*
304 * Device wasn't added because it can't be opened.
305 * Not a fatal error.
306 */
307 return (0);
308 }
309
310 /*
311 * "curdev" is an entry for this interface; add an entry for this
312 * address to its list of addresses.
313 *
314 * Allocate the new entry and fill it in.
315 */
316 curaddr = malloc(sizeof(pcap_addr_t));
317 if (curaddr == NULL) {
318 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
319 "malloc: %s", pcap_strerror(errno));
320 return (-1);
321 }
322
323 curaddr->next = NULL;
324 if (addr != NULL) {
325 curaddr->addr = dup_sockaddr(addr, addr_size);
326 if (curaddr->addr == NULL) {
327 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
328 "malloc: %s", pcap_strerror(errno));
329 free(curaddr);
330 return (-1);
331 }
332 } else
333 curaddr->addr = NULL;
334
335 if (netmask != NULL) {
336 curaddr->netmask = dup_sockaddr(netmask, netmask_size);
337 if (curaddr->netmask == NULL) {
338 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
339 "malloc: %s", pcap_strerror(errno));
340 free(curaddr);
341 return (-1);
342 }
343 } else
344 curaddr->netmask = NULL;
345
346 if (broadaddr != NULL) {
347 curaddr->broadaddr = dup_sockaddr(broadaddr, broadaddr_size);
348 if (curaddr->broadaddr == NULL) {
349 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
350 "malloc: %s", pcap_strerror(errno));
351 free(curaddr);
352 return (-1);
353 }
354 } else
355 curaddr->broadaddr = NULL;
356
357 if (dstaddr != NULL) {
358 curaddr->dstaddr = dup_sockaddr(dstaddr, dstaddr_size);
359 if (curaddr->dstaddr == NULL) {
360 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
361 "malloc: %s", pcap_strerror(errno));
362 free(curaddr);
363 return (-1);
364 }
365 } else
366 curaddr->dstaddr = NULL;
367
368 /*
369 * Find the end of the list of addresses.
370 */
371 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
372 nextaddr = prevaddr->next;
373 if (nextaddr == NULL) {
374 /*
375 * This is the end of the list.
376 */
377 break;
378 }
379 }
380
381 if (prevaddr == NULL) {
382 /*
383 * The list was empty; this is the first member.
384 */
385 curdev->addresses = curaddr;
386 } else {
387 /*
388 * "prevaddr" is the last member of the list; append
389 * this member to it.
390 */
391 prevaddr->next = curaddr;
392 }
393
394 return (0);
395 }
396
397 int
398 pcap_add_if(pcap_if_t **devlist, char *name, u_int flags,
399 const char *description, char *errbuf)
400 {
401 pcap_if_t *curdev;
402
403 return (add_or_find_if(&curdev, devlist, name, flags, description,
404 errbuf));
405 }
406
407
408 /*
409 * Free a list of interfaces.
410 */
411 void
412 pcap_freealldevs(pcap_if_t *alldevs)
413 {
414 pcap_if_t *curdev, *nextdev;
415 pcap_addr_t *curaddr, *nextaddr;
416
417 for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
418 nextdev = curdev->next;
419
420 /*
421 * Free all addresses.
422 */
423 for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
424 nextaddr = curaddr->next;
425 if (curaddr->addr)
426 free(curaddr->addr);
427 if (curaddr->netmask)
428 free(curaddr->netmask);
429 if (curaddr->broadaddr)
430 free(curaddr->broadaddr);
431 if (curaddr->dstaddr)
432 free(curaddr->dstaddr);
433 free(curaddr);
434 }
435
436 /*
437 * Free the name string.
438 */
439 free(curdev->name);
440
441 /*
442 * Free the description string, if any.
443 */
444 if (curdev->description != NULL)
445 free(curdev->description);
446
447 /*
448 * Free the interface.
449 */
450 free(curdev);
451 }
452 }
453
454 #ifndef WIN32
455
456 /*
457 * Return the name of a network interface attached to the system, or NULL
458 * if none can be found. The interface must be configured up; the
459 * lowest unit number is preferred; loopback is ignored.
460 */
461 char *
462 pcap_lookupdev(errbuf)
463 register char *errbuf;
464 {
465 pcap_if_t *alldevs;
466 /* for old BSD systems, including bsdi3 */
467 #ifndef IF_NAMESIZE
468 #define IF_NAMESIZE IFNAMSIZ
469 #endif
470 static char device[IF_NAMESIZE + 1];
471 char *ret;
472
473 if (pcap_findalldevs(&alldevs, errbuf) == -1)
474 return (NULL);
475
476 if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
477 /*
478 * There are no devices on the list, or the first device
479 * on the list is a loopback device, which means there
480 * are no non-loopback devices on the list. This means
481 * we can't return any device.
482 *
483 * XXX - why not return a loopback device? If we can't
484 * capture on it, it won't be on the list, and if it's
485 * on the list, there aren't any non-loopback devices,
486 * so why not just supply it as the default device?
487 */
488 (void)strlcpy(errbuf, "no suitable device found",
489 PCAP_ERRBUF_SIZE);
490 ret = NULL;
491 } else {
492 /*
493 * Return the name of the first device on the list.
494 */
495 (void)strlcpy(device, alldevs->name, sizeof(device));
496 ret = device;
497 }
498
499 pcap_freealldevs(alldevs);
500 return (ret);
501 }
502
503 int
504 pcap_lookupnet(device, netp, maskp, errbuf)
505 register const char *device;
506 register bpf_u_int32 *netp, *maskp;
507 register char *errbuf;
508 {
509 register int fd;
510 register struct sockaddr_in *sin;
511 struct ifreq ifr;
512
513 /*
514 * The pseudo-device "any" listens on all interfaces and therefore
515 * has the network address and -mask "0.0.0.0" therefore catching
516 * all traffic. Using NULL for the interface is the same as "any".
517 */
518 if (!device || strcmp(device, "any") == 0
519 #ifdef HAVE_DAG_API
520 || strstr(device, "dag") != NULL
521 #endif
522 ) {
523 *netp = *maskp = 0;
524 return 0;
525 }
526
527 fd = socket(AF_INET, SOCK_DGRAM, 0);
528 if (fd < 0) {
529 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
530 pcap_strerror(errno));
531 return (-1);
532 }
533 memset(&ifr, 0, sizeof(ifr));
534 #ifdef linux
535 /* XXX Work around Linux kernel bug */
536 ifr.ifr_addr.sa_family = AF_INET;
537 #endif
538 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
539 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
540 if (errno == EADDRNOTAVAIL) {
541 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
542 "%s: no IPv4 address assigned", device);
543 } else {
544 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
545 "SIOCGIFADDR: %s: %s",
546 device, pcap_strerror(errno));
547 }
548 (void)close(fd);
549 return (-1);
550 }
551 sin = (struct sockaddr_in *)&ifr.ifr_addr;
552 *netp = sin->sin_addr.s_addr;
553 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
554 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
555 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
556 (void)close(fd);
557 return (-1);
558 }
559 (void)close(fd);
560 *maskp = sin->sin_addr.s_addr;
561 if (*maskp == 0) {
562 if (IN_CLASSA(*netp))
563 *maskp = IN_CLASSA_NET;
564 else if (IN_CLASSB(*netp))
565 *maskp = IN_CLASSB_NET;
566 else if (IN_CLASSC(*netp))
567 *maskp = IN_CLASSC_NET;
568 else {
569 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
570 "inet class for 0x%x unknown", *netp);
571 return (-1);
572 }
573 }
574 *netp &= *maskp;
575 return (0);
576 }
577
578 #else /* WIN32 */
579
580 /*
581 * Return the name of a network interface attached to the system, or NULL
582 * if none can be found. The interface must be configured up; the
583 * lowest unit number is preferred; loopback is ignored.
584 */
585 char *
586 pcap_lookupdev(errbuf)
587 register char *errbuf;
588 {
589 DWORD dwVersion;
590 DWORD dwWindowsMajorVersion;
591 dwVersion = GetVersion(); /* get the OS version */
592 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
593
594 if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
595 /*
596 * Windows 95, 98, ME.
597 */
598 ULONG NameLength = 8192;
599 static char AdaptersName[8192];
600
601 PacketGetAdapterNames(AdaptersName,&NameLength);
602
603 return (AdaptersName);
604 } else {
605 /*
606 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
607 */
608 ULONG NameLength = 8192;
609 static WCHAR AdaptersName[8192];
610 char *tAstr;
611 WCHAR *tUstr;
612 WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
613 int NAdapts = 0;
614
615 if(TAdaptersName == NULL)
616 {
617 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
618 return NULL;
619 }
620
621 PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength);
622
623 tAstr = (char*)TAdaptersName;
624 tUstr = (WCHAR*)AdaptersName;
625
626 /*
627 * Convert and copy the device names
628 */
629 while(sscanf(tAstr, "%S", tUstr) > 0)
630 {
631 tAstr += strlen(tAstr) + 1;
632 tUstr += wcslen(tUstr) + 1;
633 NAdapts ++;
634 }
635
636 tAstr++;
637 *tUstr = 0;
638 tUstr++;
639
640 /*
641 * Copy the descriptions
642 */
643 while(NAdapts--)
644 {
645 strcpy((char*)tUstr, tAstr);
646 (char*)tUstr += strlen(tAstr) + 1;;
647 tAstr += strlen(tAstr) + 1;
648 }
649
650 return (char *)(AdaptersName);
651 }
652 }
653
654
655 int
656 pcap_lookupnet(device, netp, maskp, errbuf)
657 const register char *device;
658 register bpf_u_int32 *netp, *maskp;
659 register char *errbuf;
660 {
661 /*
662 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
663 * in order to skip non IPv4 (i.e. IPv6 addresses)
664 */
665 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
666 LONG if_addr_size = 1;
667 struct sockaddr_in *t_addr;
668 unsigned int i;
669
670 if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
671 *netp = *maskp = 0;
672 return (0);
673 }
674
675 for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
676 {
677 if(if_addrs[i].IPAddress.ss_family == AF_INET)
678 {
679 t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
680 *netp = t_addr->sin_addr.S_un.S_addr;
681 t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
682 *maskp = t_addr->sin_addr.S_un.S_addr;
683
684 *netp &= *maskp;
685 return (0);
686 }
687
688 }
689
690 *netp = *maskp = 0;
691 return (0);
692 }
693
694 #endif /* WIN32 */