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