]> The Tcpdump Group git mirrors - libpcap/blob - fad-win32.c
Define ffs() in optimize.c; that's all that uses it.
[libpcap] / fad-win32.c
1 /*
2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * 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 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
39 #include <pcap.h>
40 #include <Packet32.h>
41 #include <pcap-int.h>
42
43 #include <errno.h>
44
45 static int
46 pcap_add_if_win32(pcap_if_t **devlist, char *name, const char *desc,
47 char *errbuf)
48 {
49 pcap_if_t *curdev;
50 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
51 LONG if_addr_size;
52 int res = 0;
53
54 if_addr_size = MAX_NETWORK_ADDRESSES;
55
56 /*
57 * Add an entry for this interface, with no addresses.
58 */
59 if (add_or_find_if(&curdev, devlist, name, 0, desc, errbuf) == -1) {
60 /*
61 * Failure.
62 */
63 return (-1);
64 }
65
66 /*
67 * Get the list of addresses for the interface.
68 */
69 if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
70 /*
71 * Failure.
72 *
73 * We don't return an error, because this can happen with
74 * NdisWan interfaces, and we want to supply them even
75 * if we can't supply their addresses.
76 *
77 * We return an entry with an empty address list.
78 */
79 return (0);
80 }
81
82 /*
83 * Now add the addresses.
84 */
85 while (if_addr_size-- > 0) {
86 /*
87 * "curdev" is an entry for this interface; add an entry for
88 * this address to its list of addresses.
89 */
90 if(curdev == NULL)
91 break;
92 res = add_addr_to_dev(curdev,
93 (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
94 sizeof (struct sockaddr_storage),
95 (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
96 sizeof (struct sockaddr_storage),
97 (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
98 sizeof (struct sockaddr_storage),
99 NULL,
100 0,
101 errbuf);
102 if (res == -1) {
103 /*
104 * Failure.
105 */
106 break;
107 }
108 }
109
110 return (res);
111 }
112
113
114 /*
115 * Get a list of all interfaces that are up and that we can open.
116 * Returns -1 on error, 0 otherwise.
117 * The list, as returned through "alldevsp", may be null if no interfaces
118 * were up and could be opened.
119 *
120 * Win32 implementation, based on WinPcap
121 */
122 int
123 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
124 {
125 pcap_if_t *devlist = NULL;
126 int ret = 0;
127 const char *desc;
128 char *AdaptersName;
129 ULONG NameLength;
130 char *name;
131 char our_errbuf[PCAP_ERRBUF_SIZE+1];
132
133 /*
134 * Find out how big a buffer we need.
135 *
136 * This call should always return FALSE; if the error is
137 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
138 * the size of the buffer we need, otherwise there's a
139 * problem, and NameLength should be set to 0.
140 *
141 * It shouldn't require NameLength to be set, but,
142 * at least as of WinPcap 4.1.3, it checks whether
143 * NameLength is big enough before it checks for a
144 * NULL buffer argument, so, while it'll still do
145 * the right thing if NameLength is uninitialized and
146 * whatever junk happens to be there is big enough
147 * (because the pointer argument will be null), it's
148 * still reading an uninitialized variable.
149 */
150 NameLength = 0;
151 if (!PacketGetAdapterNames(NULL, &NameLength))
152 {
153 DWORD last_error = GetLastError();
154
155 if (last_error != ERROR_INSUFFICIENT_BUFFER)
156 {
157 pcap_win32_err_to_str(last_error, our_errbuf);
158 snprintf(errbuf, PCAP_ERRBUF_SIZE,
159 "PacketGetAdapterNames: %s", our_errbuf);
160 return (-1);
161 }
162 }
163
164 if (NameLength > 0)
165 AdaptersName = (char*) malloc(NameLength);
166 else
167 {
168 *alldevsp = NULL;
169 return 0;
170 }
171 if (AdaptersName == NULL)
172 {
173 snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
174 return (-1);
175 }
176
177 if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
178 pcap_win32_err_to_str(GetLastError(), our_errbuf);
179 snprintf(errbuf, PCAP_ERRBUF_SIZE, "PacketGetAdapterNames: %s",
180 our_errbuf);
181 free(AdaptersName);
182 return (-1);
183 }
184
185 /*
186 * "PacketGetAdapterNames()" returned a list of
187 * null-terminated ASCII interface name strings,
188 * terminated by a null string, followed by a list
189 * of null-terminated ASCII interface description
190 * strings, terminated by a null string.
191 * This means there are two ASCII nulls at the end
192 * of the first list.
193 *
194 * Find the end of the first list; that's the
195 * beginning of the second list.
196 */
197 desc = &AdaptersName[0];
198 while (*desc != '\0' || *(desc + 1) != '\0')
199 desc++;
200
201 /*
202 * Found it - "desc" points to the first of the two
203 * nulls at the end of the list of names, so the
204 * first byte of the list of descriptions is two bytes
205 * after it.
206 */
207 desc += 2;
208
209 /*
210 * Loop over the elements in the first list.
211 */
212 name = &AdaptersName[0];
213 while (*name != '\0') {
214 /*
215 * Add an entry for this interface.
216 */
217 if (pcap_add_if_win32(&devlist, name, desc, errbuf) == -1) {
218 /*
219 * Failure.
220 */
221 ret = -1;
222 break;
223 }
224 name += strlen(name) + 1;
225 desc += strlen(desc) + 1;
226 }
227
228 if (ret != -1) {
229 /*
230 * We haven't had any errors yet; do any platform-specific
231 * operations to add devices.
232 */
233 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
234 ret = -1;
235 }
236
237 if (ret == -1) {
238 /*
239 * We had an error; free the list we've been constructing.
240 */
241 if (devlist != NULL) {
242 pcap_freealldevs(devlist);
243 devlist = NULL;
244 }
245 }
246
247 *alldevsp = devlist;
248 free(AdaptersName);
249 return (ret);
250 }