]> The Tcpdump Group git mirrors - libpcap/blob - fad-getad.c
Add a "pcap_close_common()" routine which can be used as the close
[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[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/fad-getad.c,v 1.9 2004-03-11 23:04:24 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 #include <netinet/in.h>
47
48 #include <net/if.h>
49
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <ifaddrs.h>
54
55 #include "pcap-int.h"
56
57 #ifdef HAVE_OS_PROTO_H
58 #include "os-proto.h"
59 #endif
60
61 /*
62 * This is fun.
63 *
64 * In older BSD systems, socket addresses were fixed-length, and
65 * "sizeof (struct sockaddr)" gave the size of the structure.
66 * All addresses fit within a "struct sockaddr".
67 *
68 * In newer BSD systems, the socket address is variable-length, and
69 * there's an "sa_len" field giving the length of the structure;
70 * this allows socket addresses to be longer than 2 bytes of family
71 * and 14 bytes of data.
72 *
73 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
74 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
75 * than "struct sockaddr"), and some use the new BSD scheme.
76 *
77 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
78 * macro that determines the size based on the address family. Other
79 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
80 * but not in the final version). On the latter systems, we explicitly
81 * check the AF_ type to determine the length; we assume that on
82 * all those systems we have "struct sockaddr_storage".
83 */
84 #ifndef SA_LEN
85 #ifdef HAVE_SOCKADDR_SA_LEN
86 #define SA_LEN(addr) ((addr)->sa_len)
87 #else /* HAVE_SOCKADDR_SA_LEN */
88 #ifdef HAVE_SOCKADDR_STORAGE
89 static size_t
90 get_sa_len(struct sockaddr *addr)
91 {
92 switch (addr->sa_family) {
93
94 #ifdef AF_INET
95 case AF_INET:
96 return (sizeof (struct sockaddr_in));
97 #endif
98
99 #ifdef AF_INET6
100 case AF_INET6:
101 return (sizeof (struct sockaddr_in6));
102 #endif
103
104 default:
105 return (sizeof (struct sockaddr));
106 }
107 }
108 #define SA_LEN(addr) (get_sa_len(addr))
109 #else /* HAVE_SOCKADDR_STORAGE */
110 #define SA_LEN(addr) (sizeof (struct sockaddr))
111 #endif /* HAVE_SOCKADDR_STORAGE */
112 #endif /* HAVE_SOCKADDR_SA_LEN */
113 #endif /* SA_LEN */
114
115 /*
116 * Get a list of all interfaces that are up and that we can open.
117 * Returns -1 on error, 0 otherwise.
118 * The list, as returned through "alldevsp", may be null if no interfaces
119 * were up and could be opened.
120 *
121 * This is the implementation used on platforms that have "getifaddrs()".
122 */
123 int
124 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
125 {
126 pcap_if_t *devlist = NULL;
127 struct ifaddrs *ifap, *ifa;
128 struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
129 size_t addr_size, broadaddr_size, dstaddr_size;
130 int ret = 0;
131
132 /*
133 * Get the list of interface addresses.
134 *
135 * Note: this won't return information about interfaces
136 * with no addresses; are there any such interfaces
137 * that would be capable of receiving packets?
138 * (Interfaces incapable of receiving packets aren't
139 * very interesting from libpcap's point of view.)
140 *
141 * LAN interfaces will probably have link-layer
142 * addresses; I don't know whether all implementations
143 * of "getifaddrs()" now, or in the future, will return
144 * those.
145 */
146 if (getifaddrs(&ifap) != 0) {
147 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
148 "getifaddrs: %s", pcap_strerror(errno));
149 return (-1);
150 }
151 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
152 /*
153 * Is this interface up?
154 */
155 if (!(ifa->ifa_flags & IFF_UP)) {
156 /*
157 * No, so don't add it to the list.
158 */
159 continue;
160 }
161
162 /*
163 * "ifa_addr" was apparently null on at least one
164 * interface on some system.
165 *
166 * "ifa_broadaddr" may be non-null even on
167 * non-broadcast interfaces, and was null on
168 * at least one OpenBSD 3.4 system on at least
169 * one interface with IFF_BROADCAST set.
170 *
171 * "ifa_dstaddr" was, on at least one FreeBSD 4.1
172 * system, non-null on a non-point-to-point
173 * interface.
174 *
175 * Therefore, we supply the address and netmask only
176 * if "ifa_addr" is non-null (if there's no address,
177 * there's obviously no netmask), and supply the
178 * broadcast and destination addresses if the appropriate
179 * flag is set *and* the appropriate "ifa_" entry doesn't
180 * evaluate to a null pointer.
181 */
182 if (ifa->ifa_addr != NULL) {
183 addr = ifa->ifa_addr;
184 addr_size = SA_LEN(addr);
185 netmask = ifa->ifa_netmask;
186 } else {
187 addr = NULL;
188 addr_size = 0;
189 netmask = NULL;
190 }
191 if (ifa->ifa_flags & IFF_BROADCAST &&
192 ifa->ifa_broadaddr != NULL) {
193 broadaddr = ifa->ifa_broadaddr;
194 broadaddr_size = SA_LEN(broadaddr);
195 } else {
196 broadaddr = NULL;
197 broadaddr_size = 0;
198 }
199 if (ifa->ifa_flags & IFF_POINTOPOINT &&
200 ifa->ifa_dstaddr != NULL) {
201 dstaddr = ifa->ifa_dstaddr;
202 dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
203 } else {
204 dstaddr = NULL;
205 dstaddr_size = 0;
206 }
207
208 /*
209 * Add information for this address to the list.
210 */
211 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
212 ifa->ifa_flags, addr, addr_size, netmask, addr_size,
213 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
214 errbuf) < 0) {
215 ret = -1;
216 break;
217 }
218 }
219
220 freeifaddrs(ifap);
221
222 if (ret != -1) {
223 /*
224 * We haven't had any errors yet; do any platform-specific
225 * operations to add devices.
226 */
227 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
228 ret = -1;
229 }
230
231 if (ret == -1) {
232 /*
233 * We had an error; free the list we've been constructing.
234 */
235 if (devlist != NULL) {
236 pcap_freealldevs(devlist);
237 devlist = NULL;
238 }
239 }
240
241 *alldevsp = devlist;
242 return (ret);
243 }