1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 static const char rcsid
[] _U_
=
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.71 2006-10-13 09:06:05 guy Exp $ (LBL)";
45 #include <pcap-stdinc.h>
48 #include <sys/param.h>
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
58 struct mbuf
; /* Squelch compiler warnings on some platforms for */
59 struct rtentry
; /* declarations in <net/if.h> */
61 #include <netinet/in.h>
70 #if !defined(WIN32) && !defined(__BORLANDC__)
72 #endif /* !WIN32 && !__BORLANDC__ */
76 #define INT_MAX 2147483647
81 #ifdef HAVE_OS_PROTO_H
85 /* Not all systems have IFF_LOOPBACK */
87 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
89 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
90 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
94 dup_sockaddr(struct sockaddr
*sa
, size_t sa_length
)
96 struct sockaddr
*newsa
;
98 if ((newsa
= malloc(sa_length
)) == NULL
)
100 return (memcpy(newsa
, sa
, sa_length
));
104 get_instance(const char *name
)
106 const char *cp
, *endcp
;
109 if (strcmp(name
, "any") == 0) {
111 * Give the "any" device an artificially high instance
112 * number, so it shows up after all other non-loopback
118 endcp
= name
+ strlen(name
);
119 for (cp
= name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
122 if (isdigit((unsigned char)*cp
))
130 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, const char *name
,
131 u_int flags
, const char *description
, char *errbuf
)
134 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
138 * Is there already an entry in the list for this interface?
140 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
141 if (strcmp(name
, curdev
->name
) == 0)
142 break; /* yes, we found it */
145 if (curdev
== NULL
) {
147 * No, we didn't find it.
149 * Can we open this interface for live capture?
151 * We do this check so that interfaces that are
152 * supplied by the interface enumeration mechanism
153 * we're using but that don't support packet capture
154 * aren't included in the list. Loopback interfaces
155 * on Solaris are an example of this; we don't just
156 * omit loopback interfaces on all platforms because
157 * you *can* capture on loopback interfaces on some
160 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
163 * No. Don't bother including it.
164 * Don't treat this as an error, though.
172 * Yes, we can open it.
173 * Allocate a new entry.
175 curdev
= malloc(sizeof(pcap_if_t
));
176 if (curdev
== NULL
) {
177 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
178 "malloc: %s", pcap_strerror(errno
));
186 curdev
->name
= strdup(name
);
187 if (curdev
->name
== NULL
) {
188 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
189 "malloc: %s", pcap_strerror(errno
));
193 if (description
!= NULL
) {
195 * We have a description for this interface.
197 curdev
->description
= strdup(description
);
198 if (curdev
->description
== NULL
) {
199 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
200 "malloc: %s", pcap_strerror(errno
));
209 curdev
->description
= NULL
;
211 curdev
->addresses
= NULL
; /* list starts out as empty */
213 if (ISLOOPBACK(name
, flags
))
214 curdev
->flags
|= PCAP_IF_LOOPBACK
;
217 * Add it to the list, in the appropriate location.
218 * First, get the instance number of this interface.
220 this_instance
= get_instance(name
);
223 * Now look for the last interface with an instance number
224 * less than or equal to the new interface's instance
225 * number - except that non-loopback interfaces are
226 * arbitrarily treated as having interface numbers less
227 * than those of loopback interfaces, so the loopback
228 * interfaces are put at the end of the list.
230 * We start with "prevdev" being NULL, meaning we're before
231 * the first element in the list.
236 * Get the interface after this one.
238 if (prevdev
== NULL
) {
240 * The next element is the first element.
244 nextdev
= prevdev
->next
;
247 * Are we at the end of the list?
249 if (nextdev
== NULL
) {
251 * Yes - we have to put the new entry
258 * Is the new interface a non-loopback interface
259 * and the next interface a loopback interface?
261 if (!(curdev
->flags
& PCAP_IF_LOOPBACK
) &&
262 (nextdev
->flags
& PCAP_IF_LOOPBACK
)) {
264 * Yes, we should put the new entry
265 * before "nextdev", i.e. after "prevdev".
271 * Is the new interface's instance number less
272 * than the next interface's instance number,
273 * and is it the case that the new interface is a
274 * non-loopback interface or the next interface is
275 * a loopback interface?
277 * (The goal of both loopback tests is to make
278 * sure that we never put a loopback interface
279 * before any non-loopback interface and that we
280 * always put a non-loopback interface before all
281 * loopback interfaces.)
283 if (this_instance
< get_instance(nextdev
->name
) &&
284 (!(curdev
->flags
& PCAP_IF_LOOPBACK
) ||
285 (nextdev
->flags
& PCAP_IF_LOOPBACK
))) {
287 * Yes - we should put the new entry
288 * before "nextdev", i.e. after "prevdev".
297 * Insert before "nextdev".
299 curdev
->next
= nextdev
;
302 * Insert after "prevdev" - unless "prevdev" is null,
303 * in which case this is the first interface.
305 if (prevdev
== NULL
) {
307 * This is the first interface. Pass back a
308 * pointer to it, and put "curdev" before
313 prevdev
->next
= curdev
;
316 *curdev_ret
= curdev
;
321 * XXX - on FreeBSDs that support it, should it get the sysctl named
322 * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
323 * of the adapter? Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
324 * with my Cisco 350 card, so the name isn't entirely descriptive. The
325 * "dev.an.0.%pnpinfo" has a better description, although one might argue
326 * that the problem is really a driver bug - if it can find out that it's
327 * a Cisco 340 or 350, rather than an old Aironet card, it should use
328 * that in the description.
330 * Do NetBSD, DragonflyBSD, or OpenBSD support this as well?
331 * Do any other UN*Xes support getting a description?
334 add_addr_to_iflist(pcap_if_t
**alldevs
, const char *name
, u_int flags
,
335 struct sockaddr
*addr
, size_t addr_size
,
336 struct sockaddr
*netmask
, size_t netmask_size
,
337 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
338 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
342 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
344 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, NULL
, errbuf
) == -1) {
350 if (curdev
== NULL
) {
352 * Device wasn't added because it can't be opened.
359 * "curdev" is an entry for this interface; add an entry for this
360 * address to its list of addresses.
362 * Allocate the new entry and fill it in.
364 curaddr
= malloc(sizeof(pcap_addr_t
));
365 if (curaddr
== NULL
) {
366 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
367 "malloc: %s", pcap_strerror(errno
));
371 curaddr
->next
= NULL
;
373 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
374 if (curaddr
->addr
== NULL
) {
375 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
376 "malloc: %s", pcap_strerror(errno
));
381 curaddr
->addr
= NULL
;
383 if (netmask
!= NULL
) {
384 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
385 if (curaddr
->netmask
== NULL
) {
386 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
387 "malloc: %s", pcap_strerror(errno
));
388 if (curaddr
->addr
!= NULL
)
394 curaddr
->netmask
= NULL
;
396 if (broadaddr
!= NULL
) {
397 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
398 if (curaddr
->broadaddr
== NULL
) {
399 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
400 "malloc: %s", pcap_strerror(errno
));
401 if (curaddr
->netmask
!= NULL
)
402 free(curaddr
->netmask
);
403 if (curaddr
->addr
!= NULL
)
409 curaddr
->broadaddr
= NULL
;
411 if (dstaddr
!= NULL
) {
412 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
413 if (curaddr
->dstaddr
== NULL
) {
414 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
415 "malloc: %s", pcap_strerror(errno
));
416 if (curaddr
->broadaddr
!= NULL
)
417 free(curaddr
->broadaddr
);
418 if (curaddr
->netmask
!= NULL
)
419 free(curaddr
->netmask
);
420 if (curaddr
->addr
!= NULL
)
426 curaddr
->dstaddr
= NULL
;
429 * Find the end of the list of addresses.
431 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
432 nextaddr
= prevaddr
->next
;
433 if (nextaddr
== NULL
) {
435 * This is the end of the list.
441 if (prevaddr
== NULL
) {
443 * The list was empty; this is the first member.
445 curdev
->addresses
= curaddr
;
448 * "prevaddr" is the last member of the list; append
451 prevaddr
->next
= curaddr
;
458 pcap_add_if(pcap_if_t
**devlist
, const char *name
, u_int flags
,
459 const char *description
, char *errbuf
)
463 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
469 * Free a list of interfaces.
472 pcap_freealldevs(pcap_if_t
*alldevs
)
474 pcap_if_t
*curdev
, *nextdev
;
475 pcap_addr_t
*curaddr
, *nextaddr
;
477 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
478 nextdev
= curdev
->next
;
481 * Free all addresses.
483 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
484 nextaddr
= curaddr
->next
;
487 if (curaddr
->netmask
)
488 free(curaddr
->netmask
);
489 if (curaddr
->broadaddr
)
490 free(curaddr
->broadaddr
);
491 if (curaddr
->dstaddr
)
492 free(curaddr
->dstaddr
);
497 * Free the name string.
502 * Free the description string, if any.
504 if (curdev
->description
!= NULL
)
505 free(curdev
->description
);
508 * Free the interface.
514 #if !defined(WIN32) && !defined(MSDOS)
517 * Return the name of a network interface attached to the system, or NULL
518 * if none can be found. The interface must be configured up; the
519 * lowest unit number is preferred; loopback is ignored.
522 pcap_lookupdev(errbuf
)
523 register char *errbuf
;
526 /* for old BSD systems, including bsdi3 */
528 #define IF_NAMESIZE IFNAMSIZ
530 static char device
[IF_NAMESIZE
+ 1];
533 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
536 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
538 * There are no devices on the list, or the first device
539 * on the list is a loopback device, which means there
540 * are no non-loopback devices on the list. This means
541 * we can't return any device.
543 * XXX - why not return a loopback device? If we can't
544 * capture on it, it won't be on the list, and if it's
545 * on the list, there aren't any non-loopback devices,
546 * so why not just supply it as the default device?
548 (void)strlcpy(errbuf
, "no suitable device found",
553 * Return the name of the first device on the list.
555 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
559 pcap_freealldevs(alldevs
);
564 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
565 register const char *device
;
566 register bpf_u_int32
*netp
, *maskp
;
567 register char *errbuf
;
570 register struct sockaddr_in
*sin
;
574 * The pseudo-device "any" listens on all interfaces and therefore
575 * has the network address and -mask "0.0.0.0" therefore catching
576 * all traffic. Using NULL for the interface is the same as "any".
578 if (!device
|| strcmp(device
, "any") == 0
580 || strstr(device
, "dag") != NULL
582 #ifdef HAVE_SEPTEL_API
583 || strstr(device
, "septel") != NULL
585 #ifdef PCAP_SUPPORT_USB
586 || strstr(device
, "usb") != NULL
593 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
595 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
596 pcap_strerror(errno
));
599 memset(&ifr
, 0, sizeof(ifr
));
601 /* XXX Work around Linux kernel bug */
602 ifr
.ifr_addr
.sa_family
= AF_INET
;
604 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
605 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
606 if (errno
== EADDRNOTAVAIL
) {
607 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
608 "%s: no IPv4 address assigned", device
);
610 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
611 "SIOCGIFADDR: %s: %s",
612 device
, pcap_strerror(errno
));
617 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
618 *netp
= sin
->sin_addr
.s_addr
;
619 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
620 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
621 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
626 *maskp
= sin
->sin_addr
.s_addr
;
628 if (IN_CLASSA(*netp
))
629 *maskp
= IN_CLASSA_NET
;
630 else if (IN_CLASSB(*netp
))
631 *maskp
= IN_CLASSB_NET
;
632 else if (IN_CLASSC(*netp
))
633 *maskp
= IN_CLASSC_NET
;
635 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
636 "inet class for 0x%x unknown", *netp
);
647 * Return the name of a network interface attached to the system, or NULL
648 * if none can be found. The interface must be configured up; the
649 * lowest unit number is preferred; loopback is ignored.
652 pcap_lookupdev(errbuf
)
653 register char *errbuf
;
656 DWORD dwWindowsMajorVersion
;
657 dwVersion
= GetVersion(); /* get the OS version */
658 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
660 if (dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4) {
662 * Windows 95, 98, ME.
664 ULONG NameLength
= 8192;
665 static char AdaptersName
[8192];
667 if (PacketGetAdapterNames(AdaptersName
,&NameLength
) )
668 return (AdaptersName
);
673 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
675 ULONG NameLength
= 8192;
676 static WCHAR AdaptersName
[8192];
679 WCHAR
*TAdaptersName
= (WCHAR
*)malloc(8192 * sizeof(WCHAR
));
682 if(TAdaptersName
== NULL
)
684 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "memory allocation failure");
688 if ( !PacketGetAdapterNames((PTSTR
)TAdaptersName
,&NameLength
) )
690 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
691 "PacketGetAdapterNames: %s",
692 pcap_win32strerror());
698 tAstr
= (char*)TAdaptersName
;
699 tUstr
= (WCHAR
*)AdaptersName
;
702 * Convert and copy the device names
704 while(sscanf(tAstr
, "%S", tUstr
) > 0)
706 tAstr
+= strlen(tAstr
) + 1;
707 tUstr
+= wcslen(tUstr
) + 1;
716 * Copy the descriptions
720 strcpy((char*)tUstr
, tAstr
);
721 (char*)tUstr
+= strlen(tAstr
) + 1;;
722 tAstr
+= strlen(tAstr
) + 1;
726 return (char *)(AdaptersName
);
732 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
733 register const char *device
;
734 register bpf_u_int32
*netp
, *maskp
;
735 register char *errbuf
;
738 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
739 * in order to skip non IPv4 (i.e. IPv6 addresses)
741 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
742 LONG if_addr_size
= 1;
743 struct sockaddr_in
*t_addr
;
746 if (!PacketGetNetInfoEx((void *)device
, if_addrs
, &if_addr_size
)) {
751 for(i
=0; i
<MAX_NETWORK_ADDRESSES
; i
++)
753 if(if_addrs
[i
].IPAddress
.ss_family
== AF_INET
)
755 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].IPAddress
);
756 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
757 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].SubnetMask
);
758 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
770 #endif /* !WIN32 && !MSDOS */