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.51 2002-08-01 08:33:02 risso Exp $ (LBL)";
44 #include <pcap-stdinc.h>
45 #ifdef HAVE_SYS_SOCKIO_H
46 #include <sys/sockio.h>
49 struct mbuf
; /* Squelch compiler warnings on some platforms for */
50 struct rtentry
; /* declarations in <net/if.h> */
62 #define INT_MAX 2147483647
70 #ifdef HAVE_OS_PROTO_H
74 /* Not all systems have IFF_LOOPBACK */
76 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
78 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
79 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
83 dup_sockaddr(struct sockaddr
*sa
, size_t sa_len
)
85 struct sockaddr
*newsa
;
87 if ((newsa
= malloc(sa_len
)) == NULL
)
89 return (memcpy(newsa
, sa
, sa_len
));
93 get_instance(char *name
)
98 if (strcmp(name
, "any") == 0) {
100 * Give the "any" device an artificially high instance
101 * number, so it shows up after all other non-loopback
107 endcp
= name
+ strlen(name
);
108 for (cp
= name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
111 if (isdigit((unsigned char)*cp
))
119 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, char *name
,
120 u_int flags
, const char *description
, char *errbuf
)
123 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
127 * Can we open this interface for live capture?
129 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
132 * No. Don't bother including it.
133 * Don't treat this as an error, though.
141 * Is there already an entry in the list for this interface?
143 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
144 if (strcmp(name
, curdev
->name
) == 0)
145 break; /* yes, we found it */
147 if (curdev
== NULL
) {
149 * No, we didn't find it.
150 * Allocate a new entry.
152 curdev
= malloc(sizeof(pcap_if_t
));
153 if (curdev
== NULL
) {
154 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
155 "malloc: %s", pcap_strerror(errno
));
163 curdev
->name
= malloc(strlen(name
) + 1);
164 strcpy(curdev
->name
, name
);
165 if (description
!= NULL
) {
167 * We have a description for this interface.
169 curdev
->description
= malloc(strlen(description
) + 1);
170 strcpy(curdev
->description
, description
);
175 curdev
->description
= NULL
;
177 curdev
->addresses
= NULL
; /* list starts out as empty */
179 if (ISLOOPBACK(name
, flags
))
180 curdev
->flags
|= PCAP_IF_LOOPBACK
;
183 * Add it to the list, in the appropriate location.
184 * First, get the instance number of this interface.
186 this_instance
= get_instance(name
);
189 * Now look for the last interface with an instance number
190 * less than or equal to the new interface's instance
191 * number - except that non-loopback interfaces are
192 * arbitrarily treated as having interface numbers less
193 * than those of loopback interfaces, so the loopback
194 * interfaces are put at the end of the list.
196 * We start with "prevdev" being NULL, meaning we're before
197 * the first element in the list.
202 * Get the interface after this one.
204 if (prevdev
== NULL
) {
206 * The next element is the first element.
210 nextdev
= prevdev
->next
;
213 * Are we at the end of the list?
215 if (nextdev
== NULL
) {
217 * Yes - we have to put the new entry
224 * Is the new interface a non-loopback interface
225 * and the next interface a loopback interface?
227 if (!(curdev
->flags
& PCAP_IF_LOOPBACK
) &&
228 (nextdev
->flags
& PCAP_IF_LOOPBACK
)) {
230 * Yes, we should put the new entry
231 * before "nextdev", i.e. after "prevdev".
237 * Is the new interface's instance number less
238 * than the next interface's instance number,
239 * and is it the case that the new interface is a
240 * non-loopback interface or the next interface is
241 * a loopback interface?
243 * (The goal of both loopback tests is to make
244 * sure that we never put a loopback interface
245 * before any non-loopback interface and that we
246 * always put a non-loopback interface before all
247 * loopback interfaces.)
249 if (this_instance
< get_instance(nextdev
->name
) &&
250 (!(curdev
->flags
& PCAP_IF_LOOPBACK
) ||
251 (nextdev
->flags
& PCAP_IF_LOOPBACK
))) {
253 * Yes - we should put the new entry
254 * before "nextdev", i.e. after "prevdev".
263 * Insert before "nextdev".
265 curdev
->next
= nextdev
;
268 * Insert after "prevdev" - unless "prevdev" is null,
269 * in which case this is the first interface.
271 if (prevdev
== NULL
) {
273 * This is the first interface. Pass back a
274 * pointer to it, and put "curdev" before
279 prevdev
->next
= curdev
;
282 *curdev_ret
= curdev
;
287 add_addr_to_iflist(pcap_if_t
**alldevs
, char *name
, u_int flags
,
288 struct sockaddr
*addr
, size_t addr_size
,
289 struct sockaddr
*netmask
, size_t netmask_size
,
290 struct sockaddr
*broadaddr
, size_t broadaddr_size
,
291 struct sockaddr
*dstaddr
, size_t dstaddr_size
,
295 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
297 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, NULL
, errbuf
) == -1) {
303 if (curdev
== NULL
) {
305 * Device wasn't added because it can't be opened.
312 * "curdev" is an entry for this interface; add an entry for this
313 * address to its list of addresses.
315 * Allocate the new entry and fill it in.
317 curaddr
= malloc(sizeof(pcap_addr_t
));
318 if (curaddr
== NULL
) {
319 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
320 "malloc: %s", pcap_strerror(errno
));
324 curaddr
->next
= NULL
;
326 curaddr
->addr
= dup_sockaddr(addr
, addr_size
);
327 if (curaddr
->addr
== NULL
) {
328 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
329 "malloc: %s", pcap_strerror(errno
));
334 curaddr
->addr
= NULL
;
336 if (netmask
!= NULL
) {
337 curaddr
->netmask
= dup_sockaddr(netmask
, netmask_size
);
338 if (curaddr
->netmask
== NULL
) {
339 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
340 "malloc: %s", pcap_strerror(errno
));
345 curaddr
->netmask
= NULL
;
347 if (broadaddr
!= NULL
) {
348 curaddr
->broadaddr
= dup_sockaddr(broadaddr
, broadaddr_size
);
349 if (curaddr
->broadaddr
== NULL
) {
350 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
351 "malloc: %s", pcap_strerror(errno
));
356 curaddr
->broadaddr
= NULL
;
358 if (dstaddr
!= NULL
) {
359 curaddr
->dstaddr
= dup_sockaddr(dstaddr
, dstaddr_size
);
360 if (curaddr
->dstaddr
== NULL
) {
361 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
362 "malloc: %s", pcap_strerror(errno
));
367 curaddr
->dstaddr
= NULL
;
370 * Find the end of the list of addresses.
372 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
373 nextaddr
= prevaddr
->next
;
374 if (nextaddr
== NULL
) {
376 * This is the end of the list.
382 if (prevaddr
== NULL
) {
384 * The list was empty; this is the first member.
386 curdev
->addresses
= curaddr
;
389 * "prevaddr" is the last member of the list; append
392 prevaddr
->next
= curaddr
;
399 pcap_add_if(pcap_if_t
**devlist
, char *name
, u_int flags
,
400 const char *description
, char *errbuf
)
404 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
410 * Free a list of interfaces.
413 pcap_freealldevs(pcap_if_t
*alldevs
)
415 pcap_if_t
*curdev
, *nextdev
;
416 pcap_addr_t
*curaddr
, *nextaddr
;
418 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
419 nextdev
= curdev
->next
;
422 * Free all addresses.
424 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
425 nextaddr
= curaddr
->next
;
428 if (curaddr
->netmask
)
429 free(curaddr
->netmask
);
430 if (curaddr
->broadaddr
)
431 free(curaddr
->broadaddr
);
432 if (curaddr
->dstaddr
)
433 free(curaddr
->dstaddr
);
438 * Free the name string.
443 * Free the description string, if any.
445 if (curdev
->description
!= NULL
)
446 free(curdev
->description
);
449 * Free the interface.
458 * Return the name of a network interface attached to the system, or NULL
459 * if none can be found. The interface must be configured up; the
460 * lowest unit number is preferred; loopback is ignored.
463 pcap_lookupdev(errbuf
)
464 register char *errbuf
;
467 /* for old BSD systems, including bsdi3 */
469 #define IF_NAMESIZE IFNAMSIZ
471 static char device
[IF_NAMESIZE
+ 1];
474 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
477 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
479 * There are no devices on the list, or the first device
480 * on the list is a loopback device, which means there
481 * are no non-loopback devices on the list. This means
482 * we can't return any device.
484 * XXX - why not return a loopback device? If we can't
485 * capture on it, it won't be on the list, and if it's
486 * on the list, there aren't any non-loopback devices,
487 * so why not just supply it as the default device?
489 (void)strlcpy(errbuf
, "no suitable device found",
494 * Return the name of the first device on the list.
496 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
500 pcap_freealldevs(alldevs
);
505 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
506 register char *device
;
507 register bpf_u_int32
*netp
, *maskp
;
508 register char *errbuf
;
511 register struct sockaddr_in
*sin
;
515 * The pseudo-device "any" listens on all interfaces and therefore
516 * has the network address and -mask "0.0.0.0" therefore catching
517 * all traffic. Using NULL for the interface is the same as "any".
519 if (!device
|| strcmp(device
, "any") == 0) {
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 PacketGetAdapterNames(AdaptersName
,&NameLength
);
600 return (AdaptersName
);
603 * Windows NT (NT 4.0, W2K, WXP).
605 ULONG NameLength
= 8192;
606 static WCHAR AdaptersName
[8192];
608 PacketGetAdapterNames((PTSTR
)AdaptersName
,&NameLength
);
610 return (char *)(AdaptersName
);
616 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
617 register char *device
;
618 register bpf_u_int32
*netp
, *maskp
;
619 register char *errbuf
;
622 * We need only the first address, so we allocate a single
623 * npf_if_addr structure and we set if_addr_size to 1.
625 npf_if_addr if_addrs
;
626 LONG if_addr_size
= 1;
627 struct sockaddr_in
*t_addr
;
629 if (!PacketGetNetInfoEx((void *)device
, &if_addrs
, &if_addr_size
)) {
634 t_addr
= (struct sockaddr_in
*) &(if_addrs
.IPAddress
);
635 *netp
= t_addr
->sin_addr
.S_un
.S_addr
;
636 t_addr
= (struct sockaddr_in
*) &(if_addrs
.SubnetMask
);
637 *maskp
= t_addr
->sin_addr
.S_un
.S_addr
;
640 * XXX - will we ever get back a 0 netmask?
641 * If so, we should presumably make the "if (*maskp == 0)" code
642 * above common, rather than non-Win32-specific.