]>
The Tcpdump Group git mirrors - libpcap/blob - inet.c
75ecc12eb9abd6c0d969d1e8dfbb702eaf6017ce
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.49 2002-07-27 18:45:35 guy Exp $ (LBL)";
44 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
51 #include <sys/time.h> /* concession to AIX */
53 struct mbuf
; /* Squelch compiler warnings on some platforms for */
54 struct rtentry
; /* declarations in <net/if.h> */
56 #include <netinet/in.h>
68 #define INT_MAX 2147483647
76 #ifdef HAVE_OS_PROTO_H
80 /* Not all systems have IFF_LOOPBACK */
82 #define ISLOOPBACK(name, flags) ((flags) & IFF_LOOPBACK)
84 #define ISLOOPBACK(name, flags) ((name)[0] == 'l' && (name)[1] == 'o' && \
85 (isdigit((unsigned char)((name)[2])) || (name)[2] == '\0'))
91 * In older BSD systems, socket addresses were fixed-length, and
92 * "sizeof (struct sockaddr)" gave the size of the structure.
93 * All addresses fit within a "struct sockaddr".
95 * In newer BSD systems, the socket address is variable-length, and
96 * there's an "sa_len" field giving the length of the structure;
97 * this allows socket addresses to be longer than 2 bytes of family
98 * and 14 bytes of data.
100 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
101 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
102 * than "struct sockaddr"), and some use the new BSD scheme.
104 * GNU libc uses neither scheme, but has an "SA_LEN()" macro that
105 * determines the size based on the address family.
108 #ifdef HAVE_SOCKADDR_SA_LEN
109 #define SA_LEN(addr) ((addr)->sa_len)
110 #else /* HAVE_SOCKADDR_SA_LEN */
111 #ifdef HAVE_SOCKADDR_STORAGE
112 #define SA_LEN(addr) (sizeof (struct sockaddr_storage))
113 #else /* HAVE_SOCKADDR_STORAGE */
114 #define SA_LEN(addr) (sizeof (struct sockaddr))
115 #endif /* HAVE_SOCKADDR_STORAGE */
116 #endif /* HAVE_SOCKADDR_SA_LEN */
119 static struct sockaddr
*
120 dup_sockaddr(struct sockaddr
*sa
)
122 struct sockaddr
*newsa
;
126 if ((newsa
= malloc(size
)) == NULL
)
128 return (memcpy(newsa
, sa
, size
));
132 get_instance(char *name
)
137 if (strcmp(name
, "any") == 0) {
139 * Give the "any" device an artificially high instance
140 * number, so it shows up after all other non-loopback
146 endcp
= name
+ strlen(name
);
147 for (cp
= name
; cp
< endcp
&& !isdigit((unsigned char)*cp
); ++cp
)
150 if (isdigit((unsigned char)*cp
))
158 add_or_find_if(pcap_if_t
**curdev_ret
, pcap_if_t
**alldevs
, char *name
,
159 u_int flags
, const char *description
, char *errbuf
)
162 pcap_if_t
*curdev
, *prevdev
, *nextdev
;
166 * Can we open this interface for live capture?
168 p
= pcap_open_live(name
, 68, 0, 0, errbuf
);
171 * No. Don't bother including it.
172 * Don't treat this as an error, though.
180 * Is there already an entry in the list for this interface?
182 for (curdev
= *alldevs
; curdev
!= NULL
; curdev
= curdev
->next
) {
183 if (strcmp(name
, curdev
->name
) == 0)
184 break; /* yes, we found it */
186 if (curdev
== NULL
) {
188 * No, we didn't find it.
189 * Allocate a new entry.
191 curdev
= malloc(sizeof(pcap_if_t
));
192 if (curdev
== NULL
) {
193 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
194 "malloc: %s", pcap_strerror(errno
));
202 curdev
->name
= malloc(strlen(name
) + 1);
203 strcpy(curdev
->name
, name
);
204 if (description
!= NULL
) {
206 * We have a description for this interface.
208 curdev
->description
= malloc(strlen(description
) + 1);
209 strcpy(curdev
->description
, description
);
214 curdev
->description
= NULL
;
216 curdev
->addresses
= NULL
; /* list starts out as empty */
218 if (ISLOOPBACK(name
, flags
))
219 curdev
->flags
|= PCAP_IF_LOOPBACK
;
222 * Add it to the list, in the appropriate location.
223 * First, get the instance number of this interface.
225 this_instance
= get_instance(name
);
228 * Now look for the last interface with an instance number
229 * less than or equal to the new interface's instance
230 * number - except that non-loopback interfaces are
231 * arbitrarily treated as having interface numbers less
232 * than those of loopback interfaces, so the loopback
233 * interfaces are put at the end of the list.
235 * We start with "prevdev" being NULL, meaning we're before
236 * the first element in the list.
241 * Get the interface after this one.
243 if (prevdev
== NULL
) {
245 * The next element is the first element.
249 nextdev
= prevdev
->next
;
252 * Are we at the end of the list?
254 if (nextdev
== NULL
) {
256 * Yes - we have to put the new entry
263 * Is the new interface a non-loopback interface
264 * and the next interface a loopback interface?
266 if (!(curdev
->flags
& PCAP_IF_LOOPBACK
) &&
267 (nextdev
->flags
& PCAP_IF_LOOPBACK
)) {
269 * Yes, we should put the new entry
270 * before "nextdev", i.e. after "prevdev".
276 * Is the new interface's instance number less
277 * than the next interface's instance number,
278 * and is it the case that the new interface is a
279 * non-loopback interface or the next interface is
280 * a loopback interface?
282 * (The goal of both loopback tests is to make
283 * sure that we never put a loopback interface
284 * before any non-loopback interface and that we
285 * always put a non-loopback interface before all
286 * loopback interfaces.)
288 if (this_instance
< get_instance(nextdev
->name
) &&
289 (!(curdev
->flags
& PCAP_IF_LOOPBACK
) ||
290 (nextdev
->flags
& PCAP_IF_LOOPBACK
))) {
292 * Yes - we should put the new entry
293 * before "nextdev", i.e. after "prevdev".
302 * Insert before "nextdev".
304 curdev
->next
= nextdev
;
307 * Insert after "prevdev" - unless "prevdev" is null,
308 * in which case this is the first interface.
310 if (prevdev
== NULL
) {
312 * This is the first interface. Pass back a
313 * pointer to it, and put "curdev" before
318 prevdev
->next
= curdev
;
321 *curdev_ret
= curdev
;
326 add_addr_to_iflist(pcap_if_t
**alldevs
, char *name
, u_int flags
,
327 struct sockaddr
*addr
, struct sockaddr
*netmask
,
328 struct sockaddr
*broadaddr
, struct sockaddr
*dstaddr
, char *errbuf
)
331 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
333 if (add_or_find_if(&curdev
, alldevs
, name
, flags
, NULL
, errbuf
) == -1) {
339 if (curdev
== NULL
) {
341 * Device wasn't added because it can't be opened.
348 * "curdev" is an entry for this interface; add an entry for this
349 * address to its list of addresses.
351 * Allocate the new entry and fill it in.
353 curaddr
= malloc(sizeof(pcap_addr_t
));
354 if (curaddr
== NULL
) {
355 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
356 "malloc: %s", pcap_strerror(errno
));
360 curaddr
->next
= NULL
;
362 curaddr
->addr
= dup_sockaddr(addr
);
363 if (curaddr
->addr
== NULL
) {
364 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
365 "malloc: %s", pcap_strerror(errno
));
370 curaddr
->addr
= NULL
;
372 if (netmask
!= NULL
) {
373 curaddr
->netmask
= dup_sockaddr(netmask
);
374 if (curaddr
->netmask
== NULL
) {
375 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
376 "malloc: %s", pcap_strerror(errno
));
381 curaddr
->netmask
= NULL
;
383 if (broadaddr
!= NULL
) {
384 curaddr
->broadaddr
= dup_sockaddr(broadaddr
);
385 if (curaddr
->broadaddr
== NULL
) {
386 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
387 "malloc: %s", pcap_strerror(errno
));
392 curaddr
->broadaddr
= NULL
;
394 if (dstaddr
!= NULL
) {
395 curaddr
->dstaddr
= dup_sockaddr(dstaddr
);
396 if (curaddr
->dstaddr
== NULL
) {
397 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
398 "malloc: %s", pcap_strerror(errno
));
403 curaddr
->dstaddr
= NULL
;
406 * Find the end of the list of addresses.
408 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
409 nextaddr
= prevaddr
->next
;
410 if (nextaddr
== NULL
) {
412 * This is the end of the list.
418 if (prevaddr
== NULL
) {
420 * The list was empty; this is the first member.
422 curdev
->addresses
= curaddr
;
425 * "prevaddr" is the last member of the list; append
428 prevaddr
->next
= curaddr
;
435 pcap_add_if(pcap_if_t
**devlist
, char *name
, u_int flags
,
436 const char *description
, char *errbuf
)
440 return (add_or_find_if(&curdev
, devlist
, name
, flags
, description
,
446 * Free a list of interfaces.
449 pcap_freealldevs(pcap_if_t
*alldevs
)
451 pcap_if_t
*curdev
, *nextdev
;
452 pcap_addr_t
*curaddr
, *nextaddr
;
454 for (curdev
= alldevs
; curdev
!= NULL
; curdev
= nextdev
) {
455 nextdev
= curdev
->next
;
458 * Free all addresses.
460 for (curaddr
= curdev
->addresses
; curaddr
!= NULL
; curaddr
= nextaddr
) {
461 nextaddr
= curaddr
->next
;
464 if (curaddr
->netmask
)
465 free(curaddr
->netmask
);
466 if (curaddr
->broadaddr
)
467 free(curaddr
->broadaddr
);
468 if (curaddr
->dstaddr
)
469 free(curaddr
->dstaddr
);
474 * Free the name string.
479 * Free the description string, if any.
481 if (curdev
->description
!= NULL
)
482 free(curdev
->description
);
485 * Free the interface.
492 * Return the name of a network interface attached to the system, or NULL
493 * if none can be found. The interface must be configured up; the
494 * lowest unit number is preferred; loopback is ignored.
497 pcap_lookupdev(errbuf
)
498 register char *errbuf
;
501 /* for old BSD systems, including bsdi3 */
503 #define IF_NAMESIZE IFNAMSIZ
505 static char device
[IF_NAMESIZE
+ 1];
508 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
511 if (alldevs
== NULL
|| (alldevs
->flags
& PCAP_IF_LOOPBACK
)) {
513 * There are no devices on the list, or the first device
514 * on the list is a loopback device, which means there
515 * are no non-loopback devices on the list. This means
516 * we can't return any device.
518 * XXX - why not return a loopback device? If we can't
519 * capture on it, it won't be on the list, and if it's
520 * on the list, there aren't any non-loopback devices,
521 * so why not just supply it as the default device?
523 (void)strlcpy(errbuf
, "no suitable device found",
528 * Return the name of the first device on the list.
530 (void)strlcpy(device
, alldevs
->name
, sizeof(device
));
534 pcap_freealldevs(alldevs
);
539 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
540 register char *device
;
541 register bpf_u_int32
*netp
, *maskp
;
542 register char *errbuf
;
545 register struct sockaddr_in
*sin
;
549 * The pseudo-device "any" listens on all interfaces and therefore
550 * has the network address and -mask "0.0.0.0" therefore catching
551 * all traffic. Using NULL for the interface is the same as "any".
553 if (!device
|| strcmp(device
, "any") == 0) {
558 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
560 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
561 pcap_strerror(errno
));
564 memset(&ifr
, 0, sizeof(ifr
));
566 /* XXX Work around Linux kernel bug */
567 ifr
.ifr_addr
.sa_family
= AF_INET
;
569 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
570 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
571 if (errno
== EADDRNOTAVAIL
) {
572 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
573 "%s: no IPv4 address assigned", device
);
575 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
576 "SIOCGIFADDR: %s: %s",
577 device
, pcap_strerror(errno
));
582 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
583 *netp
= sin
->sin_addr
.s_addr
;
584 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
585 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
586 "SIOCGIFNETMASK: %s: %s", device
, pcap_strerror(errno
));
591 *maskp
= sin
->sin_addr
.s_addr
;
593 if (IN_CLASSA(*netp
))
594 *maskp
= IN_CLASSA_NET
;
595 else if (IN_CLASSB(*netp
))
596 *maskp
= IN_CLASSB_NET
;
597 else if (IN_CLASSC(*netp
))
598 *maskp
= IN_CLASSC_NET
;
600 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
601 "inet class for 0x%x unknown", *netp
);