Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / libpcap / dist / fad-getad.c
blob8c8111524dca21beb98bb6556db60f8d6e39cf34
1 /* $NetBSD: fad-getad.c,v 1.2 2014/11/19 19:33:30 christos Exp $ */
3 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
4 /*
5 * Copyright (c) 1994, 1995, 1996, 1997, 1998
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the Computer Systems
19 * Engineering Group at Lawrence Berkeley Laboratory.
20 * 4. Neither the name of the University nor of the Laboratory may be used
21 * to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: fad-getad.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
48 #include <net/if.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ifaddrs.h>
57 #include "pcap-int.h"
59 #ifdef HAVE_OS_PROTO_H
60 #include "os-proto.h"
61 #endif
64 * We don't do this on Solaris 11 and later, as it appears there aren't
65 * any AF_PACKET addresses on interfaces, so we don't need this, and
66 * we end up including both the OS's <net/bpf.h> and our <pcap/bpf.h>,
67 * and their definitions of some data structures collide.
69 #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
70 # ifdef HAVE_NETPACKET_PACKET_H
71 /* Linux distributions with newer glibc */
72 # include <netpacket/packet.h>
73 # else /* HAVE_NETPACKET_PACKET_H */
74 /* LynxOS, Linux distributions with older glibc */
75 # ifdef __Lynx__
76 /* LynxOS */
77 # include <netpacket/if_packet.h>
78 # else /* __Lynx__ */
79 /* Linux */
80 # include <linux/types.h>
81 # include <linux/if_packet.h>
82 # endif /* __Lynx__ */
83 # endif /* HAVE_NETPACKET_PACKET_H */
84 #endif /* (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET) */
87 * This is fun.
89 * In older BSD systems, socket addresses were fixed-length, and
90 * "sizeof (struct sockaddr)" gave the size of the structure.
91 * All addresses fit within a "struct sockaddr".
93 * In newer BSD systems, the socket address is variable-length, and
94 * there's an "sa_len" field giving the length of the structure;
95 * this allows socket addresses to be longer than 2 bytes of family
96 * and 14 bytes of data.
98 * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
99 * variant of the old BSD scheme (with "struct sockaddr_storage" rather
100 * than "struct sockaddr"), and some use the new BSD scheme.
102 * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
103 * macro that determines the size based on the address family. Other
104 * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
105 * but not in the final version). On the latter systems, we explicitly
106 * check the AF_ type to determine the length; we assume that on
107 * all those systems we have "struct sockaddr_storage".
109 #ifndef SA_LEN
110 #ifdef HAVE_SOCKADDR_SA_LEN
111 #define SA_LEN(addr) ((addr)->sa_len)
112 #else /* HAVE_SOCKADDR_SA_LEN */
113 #ifdef HAVE_SOCKADDR_STORAGE
114 static size_t
115 get_sa_len(struct sockaddr *addr)
117 switch (addr->sa_family) {
119 #ifdef AF_INET
120 case AF_INET:
121 return (sizeof (struct sockaddr_in));
122 #endif
124 #ifdef AF_INET6
125 case AF_INET6:
126 return (sizeof (struct sockaddr_in6));
127 #endif
129 #if (defined(linux) || defined(__Lynx__)) && defined(AF_PACKET)
130 case AF_PACKET:
131 return (sizeof (struct sockaddr_ll));
132 #endif
134 default:
135 return (sizeof (struct sockaddr));
138 #define SA_LEN(addr) (get_sa_len(addr))
139 #else /* HAVE_SOCKADDR_STORAGE */
140 #define SA_LEN(addr) (sizeof (struct sockaddr))
141 #endif /* HAVE_SOCKADDR_STORAGE */
142 #endif /* HAVE_SOCKADDR_SA_LEN */
143 #endif /* SA_LEN */
146 * Get a list of all interfaces that are up and that we can open.
147 * Returns -1 on error, 0 otherwise.
148 * The list, as returned through "alldevsp", may be null if no interfaces
149 * could be opened.
152 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
154 pcap_if_t *devlist = NULL;
155 struct ifaddrs *ifap, *ifa;
156 struct sockaddr *addr, *netmask, *broadaddr, *dstaddr;
157 size_t addr_size, broadaddr_size, dstaddr_size;
158 int ret = 0;
159 char *p, *q;
162 * Get the list of interface addresses.
164 * Note: this won't return information about interfaces
165 * with no addresses, so, if a platform has interfaces
166 * with no interfaces on which traffic can be captured,
167 * we must check for those interfaces as well (see, for
168 * example, what's done on Linux).
170 * LAN interfaces will probably have link-layer
171 * addresses; I don't know whether all implementations
172 * of "getifaddrs()" now, or in the future, will return
173 * those.
175 if (getifaddrs(&ifap) != 0) {
176 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
177 "getifaddrs: %s", pcap_strerror(errno));
178 return (-1);
180 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
182 * "ifa_addr" was apparently null on at least one
183 * interface on some system. Therefore, we supply
184 * the address and netmask only if "ifa_addr" is
185 * non-null (if there's no address, there's obviously
186 * no netmask).
188 if (ifa->ifa_addr != NULL) {
189 addr = ifa->ifa_addr;
190 addr_size = SA_LEN(addr);
191 netmask = ifa->ifa_netmask;
192 } else {
193 addr = NULL;
194 addr_size = 0;
195 netmask = NULL;
199 * Note that, on some platforms, ifa_broadaddr and
200 * ifa_dstaddr could be the same field (true on at
201 * least some versions of *BSD and OS X), so we
202 * can't just check whether the broadcast address
203 * is null and add it if so and check whether the
204 * destination address is null and add it if so.
206 * Therefore, we must also check the IFF_BROADCAST
207 * flag, and only add a broadcast address if it's
208 * set, and check the IFF_POINTTOPOINT flag, and
209 * only add a destination address if it's set (as
210 * per man page recommendations on some of those
211 * platforms).
213 if (ifa->ifa_flags & IFF_BROADCAST &&
214 ifa->ifa_broadaddr != NULL) {
215 broadaddr = ifa->ifa_broadaddr;
216 broadaddr_size = SA_LEN(broadaddr);
217 } else {
218 broadaddr = NULL;
219 broadaddr_size = 0;
221 if (ifa->ifa_flags & IFF_POINTOPOINT &&
222 ifa->ifa_dstaddr != NULL) {
223 dstaddr = ifa->ifa_dstaddr;
224 dstaddr_size = SA_LEN(ifa->ifa_dstaddr);
225 } else {
226 dstaddr = NULL;
227 dstaddr_size = 0;
231 * If this entry has a colon followed by a number at
232 * the end, we assume it's a logical interface. Those
233 * are just the way you assign multiple IP addresses to
234 * a real interface on Linux, so an entry for a logical
235 * interface should be treated like the entry for the
236 * real interface; we do that by stripping off the ":"
237 * and the number.
239 * XXX - should we do this only on Linux?
241 p = strchr(ifa->ifa_name, ':');
242 if (p != NULL) {
244 * We have a ":"; is it followed by a number?
246 q = p + 1;
247 while (isdigit((unsigned char)*q))
248 q++;
249 if (*q == '\0') {
251 * All digits after the ":" until the end.
252 * Strip off the ":" and everything after
253 * it.
255 *p = '\0';
260 * Add information for this address to the list.
262 if (add_addr_to_iflist(&devlist, ifa->ifa_name,
263 ifa->ifa_flags, addr, addr_size, netmask, addr_size,
264 broadaddr, broadaddr_size, dstaddr, dstaddr_size,
265 errbuf) < 0) {
266 ret = -1;
267 break;
271 freeifaddrs(ifap);
273 if (ret == -1) {
275 * We had an error; free the list we've been constructing.
277 if (devlist != NULL) {
278 pcap_freealldevs(devlist);
279 devlist = NULL;
283 *alldevsp = devlist;
284 return (ret);