aboutsummaryrefslogtreecommitdiffstats
path: root/fad-win32.c
blob: 270ba72644b91338282e67f923db7889ea04005e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * Copyright (c) 1999, 2002
 *	Politecnico di Torino.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the Politecnico
 * di Torino, and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <pcap.h>
#include <packet32.h>

#include <errno.h>

#ifndef SA_LEN
#ifdef HAVE_SOCKADDR_SA_LEN
#define SA_LEN(addr)	((addr)->sa_len)
#else /* HAVE_SOCKADDR_SA_LEN */
#define SA_LEN(addr)	(sizeof (struct sockaddr))
#endif /* HAVE_SOCKADDR_SA_LEN */
#endif /* SA_LEN */

/*
 * Add an entry to the list of addresses for an interface.
 * "curdev" is the entry for that interface.
 */
static int
add_addr_to_list(pcap_if_t *curdev, struct sockaddr *addr,
    struct sockaddr *netmask, struct sockaddr *broadaddr,
    struct sockaddr *dstaddr, char *errbuf)
{
	pcap_addr_t *curaddr, *prevaddr, *nextaddr;

	/*
	 * Allocate the new entry and fill it in.
	 */
	curaddr = (pcap_addr_t*)malloc(sizeof(pcap_addr_t));
	if (curaddr == NULL) {
		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "malloc: %s", pcap_strerror(errno));
		return (-1);
	}

	curaddr->next = NULL;
	if (addr != NULL) {
		curaddr->addr = (struct sockaddr*)dup_sockaddr(addr, SA_LEN(addr));
		if (curaddr->addr == NULL) {
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "malloc: %s", pcap_strerror(errno));
			free(curaddr);
			return (-1);
		}
	} else
		curaddr->addr = NULL;

	if (netmask != NULL) {
		curaddr->netmask = (struct sockaddr*)dup_sockaddr(netmask, SA_LEN(netmask));
		if (curaddr->netmask == NULL) {
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "malloc: %s", pcap_strerror(errno));
			free(curaddr);
			return (-1);
		}
	} else
		curaddr->netmask = NULL;
		
	if (broadaddr != NULL) {
		curaddr->broadaddr = (struct sockaddr*)dup_sockaddr(broadaddr, SA_LEN(broadaddr));
		if (curaddr->broadaddr == NULL) {
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "malloc: %s", pcap_strerror(errno));
			free(curaddr);
			return (-1);
		}
	} else
		curaddr->broadaddr = NULL;
		
	if (dstaddr != NULL) {
		curaddr->dstaddr = (struct sockaddr*)dup_sockaddr(dstaddr, SA_LEN(dstaddr));
		if (curaddr->dstaddr == NULL) {
			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "malloc: %s", pcap_strerror(errno));
			free(curaddr);
			return (-1);
		}
	} else
		curaddr->dstaddr = NULL;
		
	/*
	 * Find the end of the list of addresses.
	 */
	for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
		nextaddr = prevaddr->next;
		if (nextaddr == NULL) {
			/*
			 * This is the end of the list.
			 */
			break;
		}
	}

	if (prevaddr == NULL) {
		/*
		 * The list was empty; this is the first member.
		 */
		curdev->addresses = curaddr;
	} else {
		/*
		 * "prevaddr" is the last member of the list; append
		 * this member to it.
		 */
		prevaddr->next = curaddr;
	}

	return (0);
}


static int
pcap_add_if_win32(pcap_if_t **devlist, char *name, const char *desc,
    char *errbuf)
{
	pcap_if_t *curdev;
	npf_if_addr if_addrs[16];
	LONG if_addr_size;
	int res = 0;
	struct sockaddr_in *addr, *netmask;

	if_addr_size = 16;

	/*
	 * Add an entry for this interface, with no addresses.
	 */
	if (add_or_find_if(&curdev, devlist, (char *)name, 0, (char *)desc,
	    errbuf) == -1) {
		/*
		 * Failure.
		 */
		return (-1);
	}

	/*
	 * Get the list of addresses for the interface.
	 *
	 * XXX - what about IPv6?
	 */
	if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
		/*
		 * Failure.
		 */

		addr=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
		netmask=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));

		addr->sin_family = AF_INET;
		addr->sin_addr.s_addr = 0;
		netmask->sin_family = AF_INET;
		netmask->sin_addr.s_addr = 0;
	
		return (add_addr_to_list(curdev, 
			(struct sockaddr*)addr,
			(struct sockaddr*)netmask,
			NULL,
			NULL,
			errbuf));
	}

	/*
	 * Now add the addresses.
	 */
	while (if_addr_size-- > 0) {
		/*
		 * "curdev" is an entry for this interface; add an entry for
		 * this address to its list of addresses.
		 */
		if(curdev == NULL)
			break;
		res = add_addr_to_list(curdev,
		    (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
		    (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
		    (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
		    NULL,
			errbuf);
		if (res == -1) {
			/*
			 * Failure.
			 */
			break;
		}
	}

	return (res);
}


/*
 * Get a list of all interfaces that are up and that we can open.
 * Returns -1 on error, 0 otherwise.
 * The list, as returned through "alldevsp", may be null if no interfaces
 * were up and could be opened.
 *
 * Win32 implementation, based on WinPcap
 */
int
pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
{
	pcap_if_t *devlist = NULL;
	DWORD dwVersion;
	DWORD dwWindowsMajorVersion;
	int ret = 0;
	const char *desc;

	dwVersion = GetVersion();	/* get the OS version */
	dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
	if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
		/*
		 * Windows 95, 98, ME.
		 */
		char AdaptersName[8192];
		ULONG NameLength = 8192;
		char *name;

		if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "PacketGetAdapterNames: %s",
			    pcap_win32strerror());
			return (-1);
		}

		/*
		 * "PacketGetAdapterNames()" returned a list of
		 * null-terminated ASCII interface name strings,
		 * terminated by a null string, followed by a list
		 * of null-terminated ASCII interface description
		 * strings, terminated by a null string.
		 * This means there are two ASCII nulls at the end
		 * of the first list.
		 *
		 * Find the end of the first list; that's the
		 * beginning of the second list.
		 */
		desc = &AdaptersName[0];
		while (*desc != '\0' || *(desc + 1) != '\0')
			desc++;

		/*
		 * Found it - "desc" points to the first of the two
		 * nulls at the end of the list of names, so the
		 * first byte of the list of descriptions is two bytes
		 * after it.
		 */
		desc += 2;

		/*
		 * Loop over the elements in the first list.
		 */
		name = &AdaptersName[0];
		while (*name != '\0') {
			/*
			 * Add an entry for this interface.
			 */
			if (pcap_add_if_win32(&devlist, name, desc,
			    errbuf) == -1) {
				/*
				 * Failure.
				 */
				ret = -1;
				break;
			}
			name += strlen(name) + 1;
			desc += strlen(desc) + 1;
		}
	} else {
		/*
		 * Windows NT (NT 4.0, W2K, WXP).
		 */
		WCHAR AdaptersName[8192];
		ULONG NameLength = 8192;
		const WCHAR *t;
		WCHAR *uc_name;
		char ascii_name[8192];
		char ascii_desc[8192];
		char *p;

		if (!PacketGetAdapterNames((PTSTR)AdaptersName, &NameLength)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "PacketGetAdapterNames: %s",
			    pcap_win32strerror());
			return (-1);
		}

		/*
		 * "PacketGetAdapterNames()" returned a list of
		 * null-terminated Unicode interface name strings,
		 * terminated by a null string, followed by a list
		 * of null-terminated ASCII interface description
		 * strings, terminated by a null string.
		 * This means there are two Unicode nulls at the end
		 * of the first list.
		 *
		 * Find the end of the first list; that's the
		 * beginning of the second list.
		 */
		t = &AdaptersName[0];
		while (*t != '\0' || *(t + 1) != '\0')
			t++;

		/*
		 * Found it - "t" points to the first of the two
		 * nulls at the end of the list of names, so the
		 * first byte of the list of descriptions is two wide
		 * characters after it.
		 */
		t += 2;
		desc = (const char *)t;

		/*
		 * Loop over the elements in the first list.
		 *
		 * We assume all characters in the name string are valid
		 * ASCII characters.
		 */
		uc_name = &AdaptersName[0];
		while (*uc_name != '\0') {
			p = ascii_name;
			while ((*p++ = (char)*uc_name++) != '\0')
				;
			p = ascii_desc;
			while ((*p++ = *desc++) != '\0')
				;

			/*
			 * Add an entry for this interface.
			 */
			if (pcap_add_if_win32(&devlist, ascii_name,
			    ascii_desc, errbuf) == -1) {
				/*
				 * Failure.
				 */
				ret = -1;
				break;
			}
		}
	}

	if (ret == -1) {
		/*
		 * We had an error; free the list we've been constructing.
		 */
		if (devlist != NULL) {
			pcap_freealldevs(devlist);
			devlist = NULL;
		}
	}

	*alldevsp = devlist;
	return (ret);
}