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.63 2004-12-18 08:52:09 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
)
133 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
137 * Is there already an entry in the list for this interface?
139 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
140 if (strcmp(name
, curdev
->name
) == 0)
141 break; /* yes, we found it */
143 if (curdev
== NULL
) {
145 * No, we didn't find it.
146 * Allocate a new entry.
148 curdev
= malloc(sizeof(pcap_if_t
));
149 if (curdev
== NULL
) {
150 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
151 "malloc: %s", pcap_strerror(errno
));
159 curdev
->name
= malloc(strlen(name
) + 1);
160 strcpy(curdev
->name
, name
);
161 if (description
!= NULL
) {
163 * We have a description for this interface.
165 curdev
->description
= malloc(strlen(description
) + 1);
166 strcpy(curdev
->description
, description
);
171 curdev
->description
= NULL
;
173 curdev
->addresses
= NULL
; /* list starts out as empty */
175 if (ISLOOPBACK(name
, flags
))
176 curdev
->flags
|= PCAP_IF_LOOPBACK
;
179 * Add it to the list, in the appropriate location.
180 * First, get the instance number of this interface.
182 this_instance
= get_instance(name
);
185 * Now look for the last interface with an instance number
186 * less than or equal to the new interface's instance
187 * number - except that non-loopback interfaces are
188 * arbitrarily treated as having interface numbers less
189 * than those of loopback interfaces, so the loopback
190 * interfaces are put at the end of the list.
192 * We start with "prevdev" being NULL, meaning we're before
193 * the first element in the list.
198 * Get the interface after this one.
200 if (prevdev
== NULL
) {
202 * The next element is the first element.
206 nextdev
= prevdev
->next
;
209 * Are we at the end of the list?
211 if (nextdev
== NULL
) {
213 * Yes - we have to put the new entry
220 * Is the new interface a non-loopback interface
221 * and the next interface a loopback interface?
223 if (!(curdev
->flags
& PCAP_IF_LOOPBACK
) &&
224 (nextdev
->flags
& PCAP_IF_LOOPBACK
)) {
226 * Yes, we should put the new entry
227 * before "nextdev", i.e. after "prevdev".
233 * Is the new interface's instance number less
234 * than the next interface's instance number,
235 * and is it the case that the new interface is a
236 * non-loopback interface or the next interface is
237 * a loopback interface?
239 * (The goal of both loopback tests is to make
240 * sure that we never put a loopback interface
241 * before any non-loopback interface and that we
242 * always put a non-loopback interface before all
243 * loopback interfaces.)
245 if (this_instance
< get_instance(nextdev
->name
) &&
246 (!(curdev
->flags
& PCAP_IF_LOOPBACK
) ||
247 (nextdev
->flags
& PCAP_IF_LOOPBACK
))) {
249 * Yes - we should put the new entry
250 * before "nextdev", i.e. after "prevdev".
259 * Insert before "nextdev".
261 curdev
->next
= nextdev
;
264 * Insert after "prevdev" - unless "prevdev" is null,
265 * in which case this is the first interface.
267 if (prevdev
== NULL
) {
269 * This is the first interface. Pass back a
270 * pointer to it, and put "curdev" before
275 prevdev
->next
= curdev
;
278 *curdev_ret
= curdev
;
283 add_addr_to_iflist(pcap_if_t
**alldevs
, const char *name
, u_int flags
,
284 struct sockaddr
*addr
, size_t addr_size
,
285 struct sockaddr
*netmask
, size_t netmask_size
,
286 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
287 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
291 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
293 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, NULL
, errbuf
) == -1) {
299 if (curdev
== NULL
) {
301 * Device wasn't added because it can't be opened.
308 * "curdev" is an entry for this interface; add an entry for this
309 * address to its list of addresses.
311 * Allocate the new entry and fill it in.
313 curaddr
= malloc(sizeof(pcap_addr_t
));
314 if (curaddr
== NULL
) {
315 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
316 "malloc: %s", pcap_strerror(errno
));
320 curaddr
->next
= NULL
;
322 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
323 if (curaddr
->addr
== NULL
) {
324 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
325 "malloc: %s", pcap_strerror(errno
));
330 curaddr
->addr
= NULL
;
332 if (netmask
!= NULL
) {
333 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
334 if (curaddr
->netmask
== NULL
) {
335 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
336 "malloc: %s", pcap_strerror(errno
));
341 curaddr
->netmask
= NULL
;
343 if (broadaddr
!= NULL
) {
344 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
345 if (curaddr
->broadaddr
== NULL
) {
346 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
347 "malloc: %s", pcap_strerror(errno
));
352 curaddr
->broadaddr
= NULL
;
354 if (dstaddr
!= NULL
) {
355 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
356 if (curaddr
->dstaddr
== NULL
) {
357 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
358 "malloc: %s", pcap_strerror(errno
));
363 curaddr
->dstaddr
= NULL
;
366 * Find the end of the list of addresses.
368 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
369 nextaddr
= prevaddr
->next
;
370 if (nextaddr
== NULL
) {
372 * This is the end of the list.
378 if (prevaddr
== NULL
) {
380 * The list was empty; this is the first member.
382 curdev
->addresses
= curaddr
;
385 * "prevaddr" is the last member of the list; append
388 prevaddr
->next
= curaddr
;
395 pcap_add_if(pcap_if_t
**devlist
, const char *name
, u_int flags
,
396 const char *description
, char *errbuf
)
400 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
406 * Free a list of interfaces.
409 pcap_freealldevs(pcap_if_t
*alldevs
)
411 pcap_if_t
*curdev
, *nextdev
;
412 pcap_addr_t
*curaddr
, *nextaddr
;
414 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
415 nextdev
= curdev
->next
;
418 * Free all addresses.
420 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
421 nextaddr
= curaddr
->next
;
424 if (curaddr
->netmask
)
425 free(curaddr
->netmask
);
426 if (curaddr
->broadaddr
)
427 free(curaddr
->broadaddr
);
428 if (curaddr
->dstaddr
)
429 free(curaddr
->dstaddr
);
434 * Free the name string.
439 * Free the description string, if any.
441 if (curdev
->description
!= NULL
)
442 free(curdev
->description
);
445 * Free the interface.
451 #if !defined(WIN32) && !defined(MSDOS)
454 * Return the name of a network interface attached to the system, or NULL
455 * if none can be found. The interface must be configured up; the
456 * lowest unit number is preferred; loopback is ignored.
459 pcap_lookupdev(errbuf
)
460 register char *errbuf
;
463 /* for old BSD systems, including bsdi3 */
465 #define IF_NAMESIZE IFNAMSIZ
467 static char device
[IF_NAMESIZE
+ 1];
470 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
473 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
475 * There are no devices on the list, or the first device
476 * on the list is a loopback device, which means there
477 * are no non-loopback devices on the list. This means
478 * we can't return any device.
480 * XXX - why not return a loopback device? If we can't
481 * capture on it, it won't be on the list, and if it's
482 * on the list, there aren't any non-loopback devices,
483 * so why not just supply it as the default device?
485 (void)strlcpy(errbuf
, "no suitable device found",
490 * Return the name of the first device on the list.
492 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
496 pcap_freealldevs(alldevs
);
501 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
502 register const char *device
;
503 register bpf_u_int32
*netp
, *maskp
;
504 register char *errbuf
;
507 register struct sockaddr_in
*sin
;
511 * The pseudo-device "any" listens on all interfaces and therefore
512 * has the network address and -mask "0.0.0.0" therefore catching
513 * all traffic. Using NULL for the interface is the same as "any".
515 if (!device
|| strcmp(device
, "any") == 0
517 || strstr(device
, "dag") != NULL
524 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
526 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
527 pcap_strerror(errno
));
530 memset(&ifr
, 0, sizeof(ifr
));
532 /* XXX Work around Linux kernel bug */
533 ifr
.ifr_addr
.sa_family
= AF_INET
;
535 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
536 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
537 if (errno
== EADDRNOTAVAIL
) {
538 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
539 "%s: no IPv4 address assigned", device
);
541 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
542 "SIOCGIFADDR: %s: %s",
543 device
, pcap_strerror(errno
));
548 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
549 *netp
= sin
->sin_addr
.s_addr
;
550 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
551 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
552 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
557 *maskp
= sin
->sin_addr
.s_addr
;
559 if (IN_CLASSA(*netp
))
560 *maskp
= IN_CLASSA_NET
;
561 else if (IN_CLASSB(*netp
))
562 *maskp
= IN_CLASSB_NET
;
563 else if (IN_CLASSC(*netp
))
564 *maskp
= IN_CLASSC_NET
;
566 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
567 "inet class for 0x%x unknown", *netp
);
578 * Return the name of a network interface attached to the system, or NULL
579 * if none can be found. The interface must be configured up; the
580 * lowest unit number is preferred; loopback is ignored.
583 pcap_lookupdev(errbuf
)
584 register char *errbuf
;
587 DWORD dwWindowsMajorVersion
;
588 dwVersion
= GetVersion(); /* get the OS version */
589 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
591 if (dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4) {
593 * Windows 95, 98, ME.
595 ULONG NameLength
= 8192;
596 static char AdaptersName
[8192];
598 if (PacketGetAdapterNames(AdaptersName
,&NameLength
) )
599 return (AdaptersName
);
604 * Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
606 ULONG NameLength
= 8192;
607 static WCHAR AdaptersName
[8192];
610 WCHAR
*TAdaptersName
= (WCHAR
*)malloc(8192 * sizeof(WCHAR
));
613 if(TAdaptersName
== NULL
)
615 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "memory allocation failure");
619 if ( !PacketGetAdapterNames((PTSTR
)TAdaptersName
,&NameLength
) )
621 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
622 "PacketGetAdapterNames: %s",
623 pcap_win32strerror());
629 tAstr
= (char*)TAdaptersName
;
630 tUstr
= (WCHAR
*)AdaptersName
;
633 * Convert and copy the device names
635 while(sscanf(tAstr
, "%S", tUstr
) > 0)
637 tAstr
+= strlen(tAstr
) + 1;
638 tUstr
+= wcslen(tUstr
) + 1;
647 * Copy the descriptions
651 strcpy((char*)tUstr
, tAstr
);
652 (char*)tUstr
+= strlen(tAstr
) + 1;;
653 tAstr
+= strlen(tAstr
) + 1;
657 return (char *)(AdaptersName
);
663 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
664 const register char *device
;
665 register bpf_u_int32
*netp
, *maskp
;
666 register char *errbuf
;
669 * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
670 * in order to skip non IPv4 (i.e. IPv6 addresses)
672 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
673 LONG if_addr_size
= 1;
674 struct sockaddr_in
*t_addr
;
677 if (!PacketGetNetInfoEx((void *)device
, if_addrs
, &if_addr_size
)) {
682 for(i
=0; i
<MAX_NETWORK_ADDRESSES
; i
++)
684 if(if_addrs
[i
].IPAddress
.ss_family
== AF_INET
)
686 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].IPAddress
);
687 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
688 t_addr
= (struct sockaddr_in
*) &(if_addrs
[i
].SubnetMask
);
689 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
701 #endif /* !WIN32 && !MSDOS */