Deleted Added
full compact
fad-getad.c (147894) fad-getad.c (172677)
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
36static const char rcsid[] _U_ =
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
36static const char rcsid[] _U_ =
37 "@(#) $Header: /tcpdump/master/libpcap/fad-getad.c,v 1.10.2.1 2005/04/10 18:04:49 hannes Exp $ (LBL)";
37 "@(#) $Header: /tcpdump/master/libpcap/fad-getad.c,v 1.10.2.2 2007/09/14 00:45:17 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 <ctype.h>
51#include <errno.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <ifaddrs.h>
56
57#include "pcap-int.h"
58
59#ifdef HAVE_OS_PROTO_H
60#include "os-proto.h"
61#endif
62
63#ifdef AF_PACKET
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 <ctype.h>
51#include <errno.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <ifaddrs.h>
56
57#include "pcap-int.h"
58
59#ifdef HAVE_OS_PROTO_H
60#include "os-proto.h"
61#endif
62
63#ifdef AF_PACKET
64# include <linux/if_packet.h>
64# ifdef __Lynx__
65# include <netpacket/if_packet.h> /* LynxOS */
66# else
67# include <linux/if_packet.h> /* Linux */
68# endif
65#endif
66
67/*
68 * This is fun.
69 *
70 * In older BSD systems, socket addresses were fixed-length, and
71 * "sizeof (struct sockaddr)" gave the size of the structure.
72 * All addresses fit within a "struct sockaddr".
73 *
74 * In newer BSD systems, the socket address is variable-length, and
75 * there's an "sa_len" field giving the length of the structure;
76 * this allows socket addresses to be longer than 2 bytes of family
77 * and 14 bytes of data.
78 *
79 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
80 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
81 * than "struct sockaddr"), and some use the new BSD scheme.
82 *
83 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
84 * macro that determines the size based on the address family. Other
85 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
86 * but not in the final version). On the latter systems, we explicitly
87 * check the AF_ type to determine the length; we assume that on
88 * all those systems we have "struct sockaddr_storage".
89 */
90#ifndef SA_LEN
91#ifdef HAVE_SOCKADDR_SA_LEN
92#define SA_LEN(addr) ((addr)->sa_len)
93#else /* HAVE_SOCKADDR_SA_LEN */
94#ifdef HAVE_SOCKADDR_STORAGE
95static size_t
96get_sa_len(struct sockaddr *addr)
97{
98 switch (addr->sa_family) {
99
100#ifdef AF_INET
101 case AF_INET:
102 return (sizeof (struct sockaddr_in));
103#endif
104
105#ifdef AF_INET6
106 case AF_INET6:
107 return (sizeof (struct sockaddr_in6));
108#endif
109
110#ifdef AF_PACKET
111 case AF_PACKET:
112 return (sizeof (struct sockaddr_ll));
113#endif
114
115 default:
116 return (sizeof (struct sockaddr));
117 }
118}
119#define SA_LEN(addr) (get_sa_len(addr))
120#else /* HAVE_SOCKADDR_STORAGE */
121#define SA_LEN(addr) (sizeof (struct sockaddr))
122#endif /* HAVE_SOCKADDR_STORAGE */
123#endif /* HAVE_SOCKADDR_SA_LEN */
124#endif /* SA_LEN */
125
126/*
127 * Get a list of all interfaces that are up and that we can open.
128 * Returns -1 on error, 0 otherwise.
129 * The list, as returned through "alldevsp", may be null if no interfaces
130 * were up and could be opened.
131 *
132 * This is the implementation used on platforms that have "getifaddrs()".
133 */
134int
135pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
136{
137 pcap_if_t *devlist = NULL;
138 struct ifaddrs *ifap, *ifa;
139 struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
140 size_t addr_size, broadaddr_size, dstaddr_size;
141 int ret = 0;
142 char *p, *q;
143
144 /*
145 * Get the list of interface addresses.
146 *
147 * Note: this won't return information about interfaces
148 * with no addresses; are there any such interfaces
149 * that would be capable of receiving packets?
150 * (Interfaces incapable of receiving packets aren't
151 * very interesting from libpcap's point of view.)
152 *
153 * LAN interfaces will probably have link-layer
154 * addresses; I don't know whether all implementations
155 * of "getifaddrs()" now, or in the future, will return
156 * those.
157 */
158 if (getifaddrs(&ifap) != 0) {
159 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
160 "getifaddrs: %s", pcap_strerror(errno));
161 return (-1);
162 }
163 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
164 /*
165 * Is this interface up?
166 */
167 if (!(ifa->ifa_flags & IFF_UP)) {
168 /*
169 * No, so don't add it to the list.
170 */
171 continue;
172 }
173
174 /*
175 * "ifa_addr" was apparently null on at least one
176 * interface on some system.
177 *
178 * "ifa_broadaddr" may be non-null even on
179 * non-broadcast interfaces, and was null on
180 * at least one OpenBSD 3.4 system on at least
181 * one interface with IFF_BROADCAST set.
182 *
183 * "ifa_dstaddr" was, on at least one FreeBSD 4.1
184 * system, non-null on a non-point-to-point
185 * interface.
186 *
187 * Therefore, we supply the address and netmask only
188 * if "ifa_addr" is non-null (if there's no address,
189 * there's obviously no netmask), and supply the
190 * broadcast and destination addresses if the appropriate
191 * flag is set *and* the appropriate "ifa_" entry doesn't
192 * evaluate to a null pointer.
193 */
194 if (ifa->ifa_addr != NULL) {
195 addr = ifa->ifa_addr;
196 addr_size = SA_LEN(addr);
197 netmask = ifa->ifa_netmask;
198 } else {
199 addr = NULL;
200 addr_size = 0;
201 netmask = NULL;
202 }
203 if (ifa->ifa_flags & IFF_BROADCAST &&
204 ifa->ifa_broadaddr != NULL) {
205 broadaddr = ifa->ifa_broadaddr;
206 broadaddr_size = SA_LEN(broadaddr);
207 } else {
208 broadaddr = NULL;
209 broadaddr_size = 0;
210 }
211 if (ifa->ifa_flags & IFF_POINTOPOINT &&
212 ifa->ifa_dstaddr != NULL) {
213 dstaddr = ifa->ifa_dstaddr;
214 dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
215 } else {
216 dstaddr = NULL;
217 dstaddr_size = 0;
218 }
219
220 /*
221 * If this entry has a colon followed by a number at
222 * the end, we assume it's a logical interface. Those
223 * are just the way you assign multiple IP addresses to
224 * a real interface on Linux, so an entry for a logical
225 * interface should be treated like the entry for the
226 * real interface; we do that by stripping off the ":"
227 * and the number.
228 *
229 * XXX - should we do this only on Linux?
230 */
231 p = strchr(ifa->ifa_name, ':');
232 if (p != NULL) {
233 /*
234 * We have a ":"; is it followed by a number?
235 */
236 q = p + 1;
237 while (isdigit((unsigned char)*q))
238 q++;
239 if (*q == '\0') {
240 /*
241 * All digits after the ":" until the end.
242 * Strip off the ":" and everything after
243 * it.
244 */
245 *p = '\0';
246 }
247 }
248
249 /*
250 * Add information for this address to the list.
251 */
252 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
253 ifa->ifa_flags, addr, addr_size, netmask, addr_size,
254 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
255 errbuf) < 0) {
256 ret = -1;
257 break;
258 }
259 }
260
261 freeifaddrs(ifap);
262
263 if (ret != -1) {
264 /*
265 * We haven't had any errors yet; do any platform-specific
266 * operations to add devices.
267 */
268 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
269 ret = -1;
270 }
271
272 if (ret == -1) {
273 /*
274 * We had an error; free the list we've been constructing.
275 */
276 if (devlist != NULL) {
277 pcap_freealldevs(devlist);
278 devlist = NULL;
279 }
280 }
281
282 *alldevsp = devlist;
283 return (ret);
284}
69#endif
70
71/*
72 * This is fun.
73 *
74 * In older BSD systems, socket addresses were fixed-length, and
75 * "sizeof (struct sockaddr)" gave the size of the structure.
76 * All addresses fit within a "struct sockaddr".
77 *
78 * In newer BSD systems, the socket address is variable-length, and
79 * there's an "sa_len" field giving the length of the structure;
80 * this allows socket addresses to be longer than 2 bytes of family
81 * and 14 bytes of data.
82 *
83 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
84 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
85 * than "struct sockaddr"), and some use the new BSD scheme.
86 *
87 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
88 * macro that determines the size based on the address family. Other
89 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
90 * but not in the final version). On the latter systems, we explicitly
91 * check the AF_ type to determine the length; we assume that on
92 * all those systems we have "struct sockaddr_storage".
93 */
94#ifndef SA_LEN
95#ifdef HAVE_SOCKADDR_SA_LEN
96#define SA_LEN(addr) ((addr)->sa_len)
97#else /* HAVE_SOCKADDR_SA_LEN */
98#ifdef HAVE_SOCKADDR_STORAGE
99static size_t
100get_sa_len(struct sockaddr *addr)
101{
102 switch (addr->sa_family) {
103
104#ifdef AF_INET
105 case AF_INET:
106 return (sizeof (struct sockaddr_in));
107#endif
108
109#ifdef AF_INET6
110 case AF_INET6:
111 return (sizeof (struct sockaddr_in6));
112#endif
113
114#ifdef AF_PACKET
115 case AF_PACKET:
116 return (sizeof (struct sockaddr_ll));
117#endif
118
119 default:
120 return (sizeof (struct sockaddr));
121 }
122}
123#define SA_LEN(addr) (get_sa_len(addr))
124#else /* HAVE_SOCKADDR_STORAGE */
125#define SA_LEN(addr) (sizeof (struct sockaddr))
126#endif /* HAVE_SOCKADDR_STORAGE */
127#endif /* HAVE_SOCKADDR_SA_LEN */
128#endif /* SA_LEN */
129
130/*
131 * Get a list of all interfaces that are up and that we can open.
132 * Returns -1 on error, 0 otherwise.
133 * The list, as returned through "alldevsp", may be null if no interfaces
134 * were up and could be opened.
135 *
136 * This is the implementation used on platforms that have "getifaddrs()".
137 */
138int
139pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
140{
141 pcap_if_t *devlist = NULL;
142 struct ifaddrs *ifap, *ifa;
143 struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
144 size_t addr_size, broadaddr_size, dstaddr_size;
145 int ret = 0;
146 char *p, *q;
147
148 /*
149 * Get the list of interface addresses.
150 *
151 * Note: this won't return information about interfaces
152 * with no addresses; are there any such interfaces
153 * that would be capable of receiving packets?
154 * (Interfaces incapable of receiving packets aren't
155 * very interesting from libpcap's point of view.)
156 *
157 * LAN interfaces will probably have link-layer
158 * addresses; I don't know whether all implementations
159 * of "getifaddrs()" now, or in the future, will return
160 * those.
161 */
162 if (getifaddrs(&ifap) != 0) {
163 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
164 "getifaddrs: %s", pcap_strerror(errno));
165 return (-1);
166 }
167 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
168 /*
169 * Is this interface up?
170 */
171 if (!(ifa->ifa_flags & IFF_UP)) {
172 /*
173 * No, so don't add it to the list.
174 */
175 continue;
176 }
177
178 /*
179 * "ifa_addr" was apparently null on at least one
180 * interface on some system.
181 *
182 * "ifa_broadaddr" may be non-null even on
183 * non-broadcast interfaces, and was null on
184 * at least one OpenBSD 3.4 system on at least
185 * one interface with IFF_BROADCAST set.
186 *
187 * "ifa_dstaddr" was, on at least one FreeBSD 4.1
188 * system, non-null on a non-point-to-point
189 * interface.
190 *
191 * Therefore, we supply the address and netmask only
192 * if "ifa_addr" is non-null (if there's no address,
193 * there's obviously no netmask), and supply the
194 * broadcast and destination addresses if the appropriate
195 * flag is set *and* the appropriate "ifa_" entry doesn't
196 * evaluate to a null pointer.
197 */
198 if (ifa->ifa_addr != NULL) {
199 addr = ifa->ifa_addr;
200 addr_size = SA_LEN(addr);
201 netmask = ifa->ifa_netmask;
202 } else {
203 addr = NULL;
204 addr_size = 0;
205 netmask = NULL;
206 }
207 if (ifa->ifa_flags & IFF_BROADCAST &&
208 ifa->ifa_broadaddr != NULL) {
209 broadaddr = ifa->ifa_broadaddr;
210 broadaddr_size = SA_LEN(broadaddr);
211 } else {
212 broadaddr = NULL;
213 broadaddr_size = 0;
214 }
215 if (ifa->ifa_flags & IFF_POINTOPOINT &&
216 ifa->ifa_dstaddr != NULL) {
217 dstaddr = ifa->ifa_dstaddr;
218 dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
219 } else {
220 dstaddr = NULL;
221 dstaddr_size = 0;
222 }
223
224 /*
225 * If this entry has a colon followed by a number at
226 * the end, we assume it's a logical interface. Those
227 * are just the way you assign multiple IP addresses to
228 * a real interface on Linux, so an entry for a logical
229 * interface should be treated like the entry for the
230 * real interface; we do that by stripping off the ":"
231 * and the number.
232 *
233 * XXX - should we do this only on Linux?
234 */
235 p = strchr(ifa->ifa_name, ':');
236 if (p != NULL) {
237 /*
238 * We have a ":"; is it followed by a number?
239 */
240 q = p + 1;
241 while (isdigit((unsigned char)*q))
242 q++;
243 if (*q == '\0') {
244 /*
245 * All digits after the ":" until the end.
246 * Strip off the ":" and everything after
247 * it.
248 */
249 *p = '\0';
250 }
251 }
252
253 /*
254 * Add information for this address to the list.
255 */
256 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
257 ifa->ifa_flags, addr, addr_size, netmask, addr_size,
258 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
259 errbuf) < 0) {
260 ret = -1;
261 break;
262 }
263 }
264
265 freeifaddrs(ifap);
266
267 if (ret != -1) {
268 /*
269 * We haven't had any errors yet; do any platform-specific
270 * operations to add devices.
271 */
272 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
273 ret = -1;
274 }
275
276 if (ret == -1) {
277 /*
278 * We had an error; free the list we've been constructing.
279 */
280 if (devlist != NULL) {
281 pcap_freealldevs(devlist);
282 devlist = NULL;
283 }
284 }
285
286 *alldevsp = devlist;
287 return (ret);
288}