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
[] =
37 "@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.53 2002-11-13 06:46:16 guy Exp $ (LBL)";
45 #include <pcap-stdinc.h>
48 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/socket.h>
52 #ifdef HAVE_SYS_SOCKIO_H
53 #include <sys/sockio.h>
55 #include <sys/time.h> /* concession to AIX */
57 struct mbuf
; /* Squelch compiler warnings on some platforms for */
58 struct rtentry
; /* declarations in <net/if.h> */
60 #include <netinet/in.h>
75 #define INT_MAX 2147483647
83 #ifdef HAVE_OS_PROTO_H
87 /* Not all systems have IFF_LOOPBACK */
89 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
91 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
92 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
96 dup_sockaddr(struct sockaddr
*sa
, size_t sa_length
)
98 struct sockaddr
*newsa
;
100 if ((newsa
= malloc(sa_length
)) == NULL
)
102 return (memcpy(newsa
, sa
, sa_length
));
106 get_instance(char *name
)
111 if (strcmp(name
, "any") == 0) {
113 * Give the "any" device an artificially high instance
114 * number, so it shows up after all other non-loopback
120 endcp
= name
+ strlen(name
);
121 for (cp
= name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
124 if (isdigit((unsigned char)*cp
))
132 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, char *name
,
133 u_int flags
, const char *description
, char *errbuf
)
136 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
140 * Can we open this interface for live capture?
142 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
145 * No. Don't bother including it.
146 * Don't treat this as an error, though.
154 * Is there already an entry in the list for this interface?
156 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
157 if (strcmp(name
, curdev
->name
) == 0)
158 break; /* yes, we found it */
160 if (curdev
== NULL
) {
162 * No, we didn't find it.
163 * Allocate a new entry.
165 curdev
= malloc(sizeof(pcap_if_t
));
166 if (curdev
== NULL
) {
167 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
168 "malloc: %s", pcap_strerror(errno
));
176 curdev
->name
= malloc(strlen(name
) + 1);
177 strcpy(curdev
->name
, name
);
178 if (description
!= NULL
) {
180 * We have a description for this interface.
182 curdev
->description
= malloc(strlen(description
) + 1);
183 strcpy(curdev
->description
, description
);
188 curdev
->description
= NULL
;
190 curdev
->addresses
= NULL
; /* list starts out as empty */
192 if (ISLOOPBACK(name
, flags
))
193 curdev
->flags
|= PCAP_IF_LOOPBACK
;
196 * Add it to the list, in the appropriate location.
197 * First, get the instance number of this interface.
199 this_instance
= get_instance(name
);
202 * Now look for the last interface with an instance number
203 * less than or equal to the new interface's instance
204 * number - except that non-loopback interfaces are
205 * arbitrarily treated as having interface numbers less
206 * than those of loopback interfaces, so the loopback
207 * interfaces are put at the end of the list.
209 * We start with "prevdev" being NULL, meaning we're before
210 * the first element in the list.
215 * Get the interface after this one.
217 if (prevdev
== NULL
) {
219 * The next element is the first element.
223 nextdev
= prevdev
->next
;
226 * Are we at the end of the list?
228 if (nextdev
== NULL
) {
230 * Yes - we have to put the new entry
237 * Is the new interface a non-loopback interface
238 * and the next interface a loopback interface?
240 if (!(curdev
->flags
& PCAP_IF_LOOPBACK
) &&
241 (nextdev
->flags
& PCAP_IF_LOOPBACK
)) {
243 * Yes, we should put the new entry
244 * before "nextdev", i.e. after "prevdev".
250 * Is the new interface's instance number less
251 * than the next interface's instance number,
252 * and is it the case that the new interface is a
253 * non-loopback interface or the next interface is
254 * a loopback interface?
256 * (The goal of both loopback tests is to make
257 * sure that we never put a loopback interface
258 * before any non-loopback interface and that we
259 * always put a non-loopback interface before all
260 * loopback interfaces.)
262 if (this_instance
< get_instance(nextdev
->name
) &&
263 (!(curdev
->flags
& PCAP_IF_LOOPBACK
) ||
264 (nextdev
->flags
& PCAP_IF_LOOPBACK
))) {
266 * Yes - we should put the new entry
267 * before "nextdev", i.e. after "prevdev".
276 * Insert before "nextdev".
278 curdev
->next
= nextdev
;
281 * Insert after "prevdev" - unless "prevdev" is null,
282 * in which case this is the first interface.
284 if (prevdev
== NULL
) {
286 * This is the first interface. Pass back a
287 * pointer to it, and put "curdev" before
292 prevdev
->next
= curdev
;
295 *curdev_ret
= curdev
;
300 add_addr_to_iflist(pcap_if_t
**alldevs
, char *name
, u_int flags
,
301 struct sockaddr
*addr
, size_t addr_size
,
302 struct sockaddr
*netmask
, size_t netmask_size
,
303 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
304 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
308 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
310 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, NULL
, errbuf
) == -1) {
316 if (curdev
== NULL
) {
318 * Device wasn't added because it can't be opened.
325 * "curdev" is an entry for this interface; add an entry for this
326 * address to its list of addresses.
328 * Allocate the new entry and fill it in.
330 curaddr
= malloc(sizeof(pcap_addr_t
));
331 if (curaddr
== NULL
) {
332 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
333 "malloc: %s", pcap_strerror(errno
));
337 curaddr
->next
= NULL
;
339 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
340 if (curaddr
->addr
== NULL
) {
341 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
342 "malloc: %s", pcap_strerror(errno
));
347 curaddr
->addr
= NULL
;
349 if (netmask
!= NULL
) {
350 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
351 if (curaddr
->netmask
== NULL
) {
352 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
353 "malloc: %s", pcap_strerror(errno
));
358 curaddr
->netmask
= NULL
;
360 if (broadaddr
!= NULL
) {
361 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
362 if (curaddr
->broadaddr
== NULL
) {
363 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
364 "malloc: %s", pcap_strerror(errno
));
369 curaddr
->broadaddr
= NULL
;
371 if (dstaddr
!= NULL
) {
372 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
373 if (curaddr
->dstaddr
== NULL
) {
374 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
375 "malloc: %s", pcap_strerror(errno
));
380 curaddr
->dstaddr
= NULL
;
383 * Find the end of the list of addresses.
385 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
386 nextaddr
= prevaddr
->next
;
387 if (nextaddr
== NULL
) {
389 * This is the end of the list.
395 if (prevaddr
== NULL
) {
397 * The list was empty; this is the first member.
399 curdev
->addresses
= curaddr
;
402 * "prevaddr" is the last member of the list; append
405 prevaddr
->next
= curaddr
;
412 pcap_add_if(pcap_if_t
**devlist
, char *name
, u_int flags
,
413 const char *description
, char *errbuf
)
417 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
423 * Free a list of interfaces.
426 pcap_freealldevs(pcap_if_t
*alldevs
)
428 pcap_if_t
*curdev
, *nextdev
;
429 pcap_addr_t
*curaddr
, *nextaddr
;
431 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
432 nextdev
= curdev
->next
;
435 * Free all addresses.
437 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
438 nextaddr
= curaddr
->next
;
441 if (curaddr
->netmask
)
442 free(curaddr
->netmask
);
443 if (curaddr
->broadaddr
)
444 free(curaddr
->broadaddr
);
445 if (curaddr
->dstaddr
)
446 free(curaddr
->dstaddr
);
451 * Free the name string.
456 * Free the description string, if any.
458 if (curdev
->description
!= NULL
)
459 free(curdev
->description
);
462 * Free the interface.
471 * Return the name of a network interface attached to the system, or NULL
472 * if none can be found. The interface must be configured up; the
473 * lowest unit number is preferred; loopback is ignored.
476 pcap_lookupdev(errbuf
)
477 register char *errbuf
;
480 /* for old BSD systems, including bsdi3 */
482 #define IF_NAMESIZE IFNAMSIZ
484 static char device
[IF_NAMESIZE
+ 1];
487 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
490 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
492 * There are no devices on the list, or the first device
493 * on the list is a loopback device, which means there
494 * are no non-loopback devices on the list. This means
495 * we can't return any device.
497 * XXX - why not return a loopback device? If we can't
498 * capture on it, it won't be on the list, and if it's
499 * on the list, there aren't any non-loopback devices,
500 * so why not just supply it as the default device?
502 (void)strlcpy(errbuf
, "no suitable device found",
507 * Return the name of the first device on the list.
509 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
513 pcap_freealldevs(alldevs
);
518 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
519 register char *device
;
520 register bpf_u_int32
*netp
, *maskp
;
521 register char *errbuf
;
524 register struct sockaddr_in
*sin
;
528 * The pseudo-device "any" listens on all interfaces and therefore
529 * has the network address and -mask "0.0.0.0" therefore catching
530 * all traffic. Using NULL for the interface is the same as "any".
532 if (!device
|| strcmp(device
, "any") == 0) {
537 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
539 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
540 pcap_strerror(errno
));
543 memset(&ifr
, 0, sizeof(ifr
));
545 /* XXX Work around Linux kernel bug */
546 ifr
.ifr_addr
.sa_family
= AF_INET
;
548 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
549 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
550 if (errno
== EADDRNOTAVAIL
) {
551 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
552 "%s: no IPv4 address assigned", device
);
554 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
555 "SIOCGIFADDR: %s: %s",
556 device
, pcap_strerror(errno
));
561 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
562 *netp
= sin
->sin_addr
.s_addr
;
563 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
564 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
565 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
570 *maskp
= sin
->sin_addr
.s_addr
;
572 if (IN_CLASSA(*netp
))
573 *maskp
= IN_CLASSA_NET
;
574 else if (IN_CLASSB(*netp
))
575 *maskp
= IN_CLASSB_NET
;
576 else if (IN_CLASSC(*netp
))
577 *maskp
= IN_CLASSC_NET
;
579 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
580 "inet class for 0x%x unknown", *netp
);
591 * Return the name of a network interface attached to the system, or NULL
592 * if none can be found. The interface must be configured up; the
593 * lowest unit number is preferred; loopback is ignored.
596 pcap_lookupdev(errbuf
)
597 register char *errbuf
;
600 DWORD dwWindowsMajorVersion
;
601 dwVersion
= GetVersion(); /* get the OS version */
602 dwWindowsMajorVersion
= (DWORD
)(LOBYTE(LOWORD(dwVersion
)));
604 if (dwVersion
>= 0x80000000 && dwWindowsMajorVersion
>= 4) {
606 * Windows 95, 98, ME.
608 ULONG NameLength
= 8192;
609 static char AdaptersName
[8192];
611 PacketGetAdapterNames(AdaptersName
,&NameLength
);
613 return (AdaptersName
);
616 * Windows NT (NT 4.0, W2K, WXP).
618 ULONG NameLength
= 8192;
619 static WCHAR AdaptersName
[8192];
621 PacketGetAdapterNames((PTSTR
)AdaptersName
,&NameLength
);
623 return (char *)(AdaptersName
);
629 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
630 register char *device
;
631 register bpf_u_int32
*netp
, *maskp
;
632 register char *errbuf
;
635 * We need only the first address, so we allocate a single
636 * npf_if_addr structure and we set if_addr_size to 1.
638 npf_if_addr if_addrs
;
639 LONG if_addr_size
= 1;
640 struct sockaddr_in
*t_addr
;
642 if (!PacketGetNetInfoEx((void *)device
, &if_addrs
, &if_addr_size
)) {
647 t_addr
= (struct sockaddr_in
*) &(if_addrs
.IPAddress
);
648 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
649 t_addr
= (struct sockaddr_in
*) &(if_addrs
.SubnetMask
);
650 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
653 * XXX - will we ever get back a 0 netmask?
654 * If so, we should presumably make the "if (*maskp == 0)" code
655 * above common, rather than non-Win32-specific.