]> The Tcpdump Group git mirrors - libpcap/blob - fad-win32.c
remove libpcap's own CVS keywords
[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 #include <pcap.h>
39 #include <pcap-int.h>
40 #include <Packet32.h>
41
42 #include <errno.h>
43
44 /*
45 * Add an entry to the list of addresses for an interface.
46 * "curdev" is the entry for that interface.
47 */
48 static int
49 add_addr_to_list(pcap_if_t *curdev, struct sockaddr *addr,
50 struct sockaddr *netmask, struct sockaddr *broadaddr,
51 struct sockaddr *dstaddr, char *errbuf)
52 {
53 pcap_addr_t *curaddr, *prevaddr, *nextaddr;
54
55 /*
56 * Allocate the new entry and fill it in.
57 */
58 curaddr = (pcap_addr_t*)malloc(sizeof(pcap_addr_t));
59 if (curaddr == NULL) {
60 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
61 "malloc: %s", pcap_strerror(errno));
62 return (-1);
63 }
64
65 curaddr->next = NULL;
66 if (addr != NULL) {
67 curaddr->addr = (struct sockaddr*)dup_sockaddr(addr, sizeof(struct sockaddr_storage));
68 if (curaddr->addr == NULL) {
69 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
70 "malloc: %s", pcap_strerror(errno));
71 free(curaddr);
72 return (-1);
73 }
74 } else
75 curaddr->addr = NULL;
76
77 if (netmask != NULL) {
78 curaddr->netmask = (struct sockaddr*)dup_sockaddr(netmask, sizeof(struct sockaddr_storage));
79 if (curaddr->netmask == NULL) {
80 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
81 "malloc: %s", pcap_strerror(errno));
82 free(curaddr);
83 return (-1);
84 }
85 } else
86 curaddr->netmask = NULL;
87
88 if (broadaddr != NULL) {
89 curaddr->broadaddr = (struct sockaddr*)dup_sockaddr(broadaddr, sizeof(struct sockaddr_storage));
90 if (curaddr->broadaddr == NULL) {
91 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
92 "malloc: %s", pcap_strerror(errno));
93 free(curaddr);
94 return (-1);
95 }
96 } else
97 curaddr->broadaddr = NULL;
98
99 if (dstaddr != NULL) {
100 curaddr->dstaddr = (struct sockaddr*)dup_sockaddr(dstaddr, sizeof(struct sockaddr_storage));
101 if (curaddr->dstaddr == NULL) {
102 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
103 "malloc: %s", pcap_strerror(errno));
104 free(curaddr);
105 return (-1);
106 }
107 } else
108 curaddr->dstaddr = NULL;
109
110 /*
111 * Find the end of the list of addresses.
112 */
113 for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
114 nextaddr = prevaddr->next;
115 if (nextaddr == NULL) {
116 /*
117 * This is the end of the list.
118 */
119 break;
120 }
121 }
122
123 if (prevaddr == NULL) {
124 /*
125 * The list was empty; this is the first member.
126 */
127 curdev->addresses = curaddr;
128 } else {
129 /*
130 * "prevaddr" is the last member of the list; append
131 * this member to it.
132 */
133 prevaddr->next = curaddr;
134 }
135
136 return (0);
137 }
138
139
140 static int
141 pcap_add_if_win32(pcap_if_t **devlist, char *name, const char *desc,
142 char *errbuf)
143 {
144 pcap_if_t *curdev;
145 npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
146 LONG if_addr_size;
147 int res = 0;
148
149 if_addr_size = MAX_NETWORK_ADDRESSES;
150
151 /*
152 * Add an entry for this interface, with no addresses.
153 */
154 if (add_or_find_if(&curdev, devlist, name, 0, desc, errbuf) == -1) {
155 /*
156 * Failure.
157 */
158 return (-1);
159 }
160
161 /*
162 * Get the list of addresses for the interface.
163 */
164 if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
165 /*
166 * Failure.
167 *
168 * We don't return an error, because this can happen with
169 * NdisWan interfaces, and we want to supply them even
170 * if we can't supply their addresses.
171 *
172 * We return an entry with an empty address list.
173 */
174 return (0);
175 }
176
177 /*
178 * Now add the addresses.
179 */
180 while (if_addr_size-- > 0) {
181 /*
182 * "curdev" is an entry for this interface; add an entry for
183 * this address to its list of addresses.
184 */
185 if(curdev == NULL)
186 break;
187 res = add_addr_to_list(curdev,
188 (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
189 (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
190 (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
191 NULL,
192 errbuf);
193 if (res == -1) {
194 /*
195 * Failure.
196 */
197 break;
198 }
199 }
200
201 return (res);
202 }
203
204
205 /*
206 * Get a list of all interfaces that are up and that we can open.
207 * Returns -1 on error, 0 otherwise.
208 * The list, as returned through "alldevsp", may be null if no interfaces
209 * were up and could be opened.
210 *
211 * Win32 implementation, based on WinPcap
212 */
213 int
214 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
215 {
216 pcap_if_t *devlist = NULL;
217 int ret = 0;
218 const char *desc;
219 char *AdaptersName;
220 ULONG NameLength;
221 char *name;
222
223 /*
224 * Find out how big a buffer we need.
225 *
226 * This call should always return FALSE; if the error is
227 * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
228 * the size of the buffer we need, otherwise there's a
229 * problem, and NameLength should be set to 0.
230 *
231 * It shouldn't require NameLength to be set, but,
232 * at least as of WinPcap 4.1.3, it checks whether
233 * NameLength is big enough before it checks for a
234 * NULL buffer argument, so, while it'll still do
235 * the right thing if NameLength is uninitialized and
236 * whatever junk happens to be there is big enough
237 * (because the pointer argument will be null), it's
238 * still reading an uninitialized variable.
239 */
240 NameLength = 0;
241 if (!PacketGetAdapterNames(NULL, &NameLength))
242 {
243 DWORD last_error = GetLastError();
244
245 if (last_error != ERROR_INSUFFICIENT_BUFFER)
246 {
247 snprintf(errbuf, PCAP_ERRBUF_SIZE,
248 "PacketGetAdapterNames: %s",
249 pcap_win32strerror());
250 return (-1);
251 }
252 }
253
254 if (NameLength > 0)
255 AdaptersName = (char*) malloc(NameLength);
256 else
257 {
258 *alldevsp = NULL;
259 return 0;
260 }
261 if (AdaptersName == NULL)
262 {
263 snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
264 return (-1);
265 }
266
267 if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
268 snprintf(errbuf, PCAP_ERRBUF_SIZE,
269 "PacketGetAdapterNames: %s",
270 pcap_win32strerror());
271 free(AdaptersName);
272 return (-1);
273 }
274
275 /*
276 * "PacketGetAdapterNames()" returned a list of
277 * null-terminated ASCII interface name strings,
278 * terminated by a null string, followed by a list
279 * of null-terminated ASCII interface description
280 * strings, terminated by a null string.
281 * This means there are two ASCII nulls at the end
282 * of the first list.
283 *
284 * Find the end of the first list; that's the
285 * beginning of the second list.
286 */
287 desc = &AdaptersName[0];
288 while (*desc != '\0' || *(desc + 1) != '\0')
289 desc++;
290
291 /*
292 * Found it - "desc" points to the first of the two
293 * nulls at the end of the list of names, so the
294 * first byte of the list of descriptions is two bytes
295 * after it.
296 */
297 desc += 2;
298
299 /*
300 * Loop over the elements in the first list.
301 */
302 name = &AdaptersName[0];
303 while (*name != '\0') {
304 /*
305 * Add an entry for this interface.
306 */
307 if (pcap_add_if_win32(&devlist, name, desc, errbuf) == -1) {
308 /*
309 * Failure.
310 */
311 ret = -1;
312 break;
313 }
314 name += strlen(name) + 1;
315 desc += strlen(desc) + 1;
316 }
317
318 if (ret != -1) {
319 /*
320 * We haven't had any errors yet; do any platform-specific
321 * operations to add devices.
322 */
323 if (pcap_platform_finddevs(&devlist, errbuf) < 0)
324 ret = -1;
325 }
326
327 if (ret == -1) {
328 /*
329 * We had an error; free the list we've been constructing.
330 */
331 if (devlist != NULL) {
332 pcap_freealldevs(devlist);
333 devlist = NULL;
334 }
335 }
336
337 *alldevsp = devlist;
338 free(AdaptersName);
339 return (ret);
340 }