]> The Tcpdump Group git mirrors - libpcap/blob - fad-getad.c
Cope with a NetBSD annoyance.
[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 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42
43 #include <net/if.h>
44
45 /*
46 * NetBSD insists on having <net/if.h> pull in <net/dlt.h>, so that
47 * including <net/if.h> defines a bunch of stuff we later define in our
48 * pcap/dlt.h; those definitions will be different, if they haven't
49 * picked up all of our changes yet.
50 *
51 * Undefine some of the offending items.
52 */
53 #ifdef __NetBSD__
54 #undef DLT_USB /* we now define it as DLT_FREEBSD_USB */
55 #undef DLT_MATCHING_MAX /* we may define more DLT_ values than they do */
56 #endif
57
58 #include <ctype.h>
59 #include <errno.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <ifaddrs.h>
64
65 #include "pcap-int.h"
66
67 #ifdef HAVE_OS_PROTO_H
68 #include "os-proto.h"
69 #endif
70
71 /*
72 * We don't do this on Solaris 11 and later, as it appears there aren't
73 * any AF_PACKET addresses on interfaces, so we don't need this, and
74 * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>,
75 * and their definitions of some data structures collide.
76 */
77 #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
78 # ifdef HAVE_NETPACKET_PACKET_H
79 /* Linux distributions with newer glibc */
80 # include <netpacket/packet.h>
81 # else /* HAVE_NETPACKET_PACKET_H */
82 /* LynxOS, Linux distributions with older glibc */
83 # ifdef __Lynx__
84 /* LynxOS */
85 # include <netpacket/if_packet.h>
86 # else /* __Lynx__ */
87 /* Linux */
88 # include <linux/types.h>
89 # include <linux/if_packet.h>
90 # endif /* __Lynx__ */
91 # endif /* HAVE_NETPACKET_PACKET_H */
92 #endif /* (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) */
93
94 /*
95 * This is fun.
96 *
97 * In older BSD systems, socket addresses were fixed-length, and
98 * "sizeof (struct sockaddr)" gave the size of the structure.
99 * All addresses fit within a "struct sockaddr".
100 *
101 * In newer BSD systems, the socket address is variable-length, and
102 * there's an "sa_len" field giving the length of the structure;
103 * this allows socket addresses to be longer than 2 bytes of family
104 * and 14 bytes of data.
105 *
106 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
107 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
108 * than "struct sockaddr"), and some use the new BSD scheme.
109 *
110 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
111 * macro that determines the size based on the address family. Other
112 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
113 * but not in the final version). On the latter systems, we explicitly
114 * check the AF_ type to determine the length; we assume that on
115 * all those systems we have "struct sockaddr_storage".
116 */
117 #ifndef SA_LEN
118 #ifdef HAVE_SOCKADDR_SA_LEN
119 #define SA_LEN(addr) ((addr)->sa_len)
120 #else /* HAVE_SOCKADDR_SA_LEN */
121 #ifdef HAVE_SOCKADDR_STORAGE
122 static size_t
123 get_sa_len(struct sockaddr *addr)
124 {
125 switch (addr->sa_family) {
126
127 #ifdef AF_INET
128 case AF_INET:
129 return (sizeof (struct sockaddr_in));
130 #endif
131
132 #ifdef AF_INET6
133 case AF_INET6:
134 return (sizeof (struct sockaddr_in6));
135 #endif
136
137 #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
138 case AF_PACKET:
139 return (sizeof (struct sockaddr_ll));
140 #endif
141
142 default:
143 return (sizeof (struct sockaddr));
144 }
145 }
146 #define SA_LEN(addr) (get_sa_len(addr))
147 #else /* HAVE_SOCKADDR_STORAGE */
148 #define SA_LEN(addr) (sizeof (struct sockaddr))
149 #endif /* HAVE_SOCKADDR_STORAGE */
150 #endif /* HAVE_SOCKADDR_SA_LEN */
151 #endif /* SA_LEN */
152
153 /*
154 * Get a list of all interfaces that are up and that we can open.
155 * Returns -1 on error, 0 otherwise.
156 * The list, as returned through "alldevsp", may be null if no interfaces
157 * could be opened.
158 */
159 int
160 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf,
161 int (*check_usable)(const char *))
162 {
163 pcap_if_t *devlist = NULL;
164 struct ifaddrs *ifap, *ifa;
165 struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
166 size_t addr_size, broadaddr_size, dstaddr_size;
167 int ret = 0;
168 char *p, *q;
169
170 /*
171 * Get the list of interface addresses.
172 *
173 * Note: this won't return information about interfaces
174 * with no addresses, so, if a platform has interfaces
175 * with no interfaces on which traffic can be captured,
176 * we must check for those interfaces as well (see, for
177 * example, what's done on Linux).
178 *
179 * LAN interfaces will probably have link-layer
180 * addresses; I don't know whether all implementations
181 * of "getifaddrs()" now, or in the future, will return
182 * those.
183 */
184 if (getifaddrs(&ifap) != 0) {
185 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
186 "getifaddrs: %s", pcap_strerror(errno));
187 return (-1);
188 }
189 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
190 /*
191 * If this entry has a colon followed by a number at
192 * the end, we assume it's a logical interface. Those
193 * are just the way you assign multiple IP addresses to
194 * a real interface on Linux, so an entry for a logical
195 * interface should be treated like the entry for the
196 * real interface; we do that by stripping off the ":"
197 * and the number.
198 *
199 * XXX - should we do this only on Linux?
200 */
201 p = strchr(ifa->ifa_name, ':');
202 if (p != NULL) {
203 /*
204 * We have a ":"; is it followed by a number?
205 */
206 q = p + 1;
207 while (isdigit((unsigned char)*q))
208 q++;
209 if (*q == '\0') {
210 /*
211 * All digits after the ":" until the end.
212 * Strip off the ":" and everything after
213 * it.
214 */
215 *p = '\0';
216 }
217 }
218
219 /*
220 * Can we capture on this device?
221 */
222 if (!(*check_usable)(ifa->ifa_name)) {
223 /*
224 * No.
225 */
226 continue;
227 }
228
229 /*
230 * "ifa_addr" was apparently null on at least one
231 * interface on some system. Therefore, we supply
232 * the address and netmask only if "ifa_addr" is
233 * non-null (if there's no address, there's obviously
234 * no netmask).
235 */
236 if (ifa->ifa_addr != NULL) {
237 addr = ifa->ifa_addr;
238 addr_size = SA_LEN(addr);
239 netmask = ifa->ifa_netmask;
240 } else {
241 addr = NULL;
242 addr_size = 0;
243 netmask = NULL;
244 }
245
246 /*
247 * Note that, on some platforms, ifa_broadaddr and
248 * ifa_dstaddr could be the same field (true on at
249 * least some versions of *BSD and OS X), so we
250 * can't just check whether the broadcast address
251 * is null and add it if so and check whether the
252 * destination address is null and add it if so.
253 *
254 * Therefore, we must also check the IFF_BROADCAST
255 * flag, and only add a broadcast address if it's
256 * set, and check the IFF_POINTTOPOINT flag, and
257 * only add a destination address if it's set (as
258 * per man page recommendations on some of those
259 * platforms).
260 */
261 if (ifa->ifa_flags & IFF_BROADCAST &&
262 ifa->ifa_broadaddr != NULL) {
263 broadaddr = ifa->ifa_broadaddr;
264 broadaddr_size = SA_LEN(broadaddr);
265 } else {
266 broadaddr = NULL;
267 broadaddr_size = 0;
268 }
269 if (ifa->ifa_flags & IFF_POINTOPOINT &&
270 ifa->ifa_dstaddr != NULL) {
271 dstaddr = ifa->ifa_dstaddr;
272 dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
273 } else {
274 dstaddr = NULL;
275 dstaddr_size = 0;
276 }
277
278 /*
279 * Add information for this address to the list.
280 */
281 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
282 if_flags_to_pcap_flags(ifa->ifa_name, ifa->ifa_flags),
283 addr, addr_size, netmask, addr_size,
284 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
285 errbuf) < 0) {
286 ret = -1;
287 break;
288 }
289 }
290
291 freeifaddrs(ifap);
292
293 if (ret == -1) {
294 /*
295 * We had an error; free the list we've been constructing.
296 */
297 if (devlist != NULL) {
298 pcap_freealldevs(devlist);
299 devlist = NULL;
300 }
301 }
302
303 *alldevsp = devlist;
304 return (ret);
305 }