]> The Tcpdump Group git mirrors - libpcap/blob - fad-getad.c
From an anonymous Sourceforge user: include <net/if.h> to declare the
[libpcap] / fad-getad.c
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char rcsid[] =
37 "@(#) $Header: /tcpdump/master/libpcap/fad-getad.c,v 1.3 2002-08-26 09:50:45 guy Exp $ (LBL)";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/socket.h>
46
47 #include <net/if.h>
48
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <ifaddrs.h>
53
54 #include "pcap-int.h"
55
56 #ifdef HAVE_OS_PROTO_H
57 #include "os-proto.h"
58 #endif
59
60 /*
61 * This is fun.
62 *
63 * In older BSD systems, socket addresses were fixed-length, and
64 * "sizeof (struct sockaddr)" gave the size of the structure.
65 * All addresses fit within a "struct sockaddr".
66 *
67 * In newer BSD systems, the socket address is variable-length, and
68 * there's an "sa_len" field giving the length of the structure;
69 * this allows socket addresses to be longer than 2 bytes of family
70 * and 14 bytes of data.
71 *
72 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
73 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
74 * than "struct sockaddr"), and some use the new BSD scheme.
75 *
76 * GNU libc uses neither scheme, but has an "SA_LEN()" macro that
77 * determines the size based on the address family.
78 */
79 #ifndef SA_LEN
80 #ifdef HAVE_SOCKADDR_SA_LEN
81 #define SA_LEN(addr) ((addr)->sa_len)
82 #else /* HAVE_SOCKADDR_SA_LEN */
83 #ifdef HAVE_SOCKADDR_STORAGE
84 #define SA_LEN(addr) (sizeof (struct sockaddr_storage))
85 #else /* HAVE_SOCKADDR_STORAGE */
86 #define SA_LEN(addr) (sizeof (struct sockaddr))
87 #endif /* HAVE_SOCKADDR_STORAGE */
88 #endif /* HAVE_SOCKADDR_SA_LEN */
89 #endif /* SA_LEN */
90
91 /*
92 * Get a list of all interfaces that are up and that we can open.
93 * Returns -1 on error, 0 otherwise.
94 * The list, as returned through "alldevsp", may be null if no interfaces
95 * were up and could be opened.
96 *
97 * This is the implementation used on platforms that have "getifaddrs()".
98 */
99 int
100 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
101 {
102 pcap_if_t *devlist = NULL;
103 struct ifaddrs *ifap, *ifa;
104 struct sockaddr *broadaddr, *dstaddr;
105 size_t broadaddr_size, dstaddr_size;
106 int ret = 0;
107
108 /*
109 * Get the list of interface addresses.
110 *
111 * Note: this won't return information about interfaces
112 * with no addresses; are there any such interfaces
113 * that would be capable of receiving packets?
114 * (Interfaces incapable of receiving packets aren't
115 * very interesting from libpcap's point of view.)
116 *
117 * LAN interfaces will probably have link-layer
118 * addresses; I don't know whether all implementations
119 * of "getifaddrs()" now, or in the future, will return
120 * those.
121 */
122 if (getifaddrs(&ifap) != 0) {
123 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
124 "getifaddrs: %s", pcap_strerror(errno));
125 return (-1);
126 }
127 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
128 /*
129 * Is this interface up?
130 */
131 if (!(ifa->ifa_flags & IFF_UP)) {
132 /*
133 * No, so don't add it to the list.
134 */
135 continue;
136 }
137
138 /*
139 * "ifa_broadaddr" may be non-null even on
140 * non-broadcast interfaces; "ifa_dstaddr"
141 * was, on at least one FreeBSD 4.1 system,
142 * non-null on a non-point-to-point
143 * interface.
144 */
145 if (ifa->ifa_flags & IFF_BROADCAST) {
146 broadaddr = ifa->ifa_broadaddr;
147 broadaddr_size = SA_LEN(broadaddr);
148 } else {
149 broadaddr = NULL;
150 broadaddr_size = 0;
151 }
152 if (ifa->ifa_flags & IFF_POINTOPOINT) {
153 dstaddr = ifa->ifa_dstaddr;
154 dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
155 } else {
156 dstaddr = NULL;
157 dstaddr_size = 0;
158 }
159
160 /*
161 * Add information for this address to the list.
162 */
163 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
164 ifa->ifa_flags, ifa->ifa_addr, SA_LEN(ifa->ifa_addr),
165 ifa->ifa_netmask, SA_LEN(ifa->ifa_netmask),
166 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
167 errbuf) < 0) {
168 ret = -1;
169 break;
170 }
171 }
172
173 freeifaddrs(ifap);
174
175 if (ret != -1) {
176 /*
177 * We haven't had any errors yet; do any platform-specific
178 * operations to add devices.
179 */
180 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
181 ret = -1;
182 }
183
184 if (ret == -1) {
185 /*
186 * We had an error; free the list we've been constructing.
187 */
188 if (devlist != NULL) {
189 pcap_freealldevs(devlist);
190 devlist = NULL;
191 }
192 }
193
194 *alldevsp = devlist;
195 return (ret);
196 }