2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
35 static const char rcsid
[] _U_
=
36 "@(#) $Header: /tcpdump/master/libpcap/fad-win32.c,v 1.11.2.3 2006/02/22 17:09:32 gianluca Exp $ (LBL)";
50 * Add an entry to the list of addresses for an interface.
51 * "curdev" is the entry for that interface.
54 add_addr_to_list(pcap_if_t
*curdev
, struct sockaddr
*addr
,
55 struct sockaddr
*netmask
, struct sockaddr
*broadaddr
,
56 struct sockaddr
*dstaddr
, char *errbuf
)
58 pcap_addr_t
*curaddr
, *prevaddr
, *nextaddr
;
61 * Allocate the new entry and fill it in.
63 curaddr
= (pcap_addr_t
*)malloc(sizeof(pcap_addr_t
));
64 if (curaddr
== NULL
) {
65 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
66 "malloc: %s", pcap_strerror(errno
));
72 curaddr
->addr
= (struct sockaddr
*)dup_sockaddr(addr
, sizeof(struct sockaddr_storage
));
73 if (curaddr
->addr
== NULL
) {
74 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
75 "malloc: %s", pcap_strerror(errno
));
82 if (netmask
!= NULL
) {
83 curaddr
->netmask
= (struct sockaddr
*)dup_sockaddr(netmask
, sizeof(struct sockaddr_storage
));
84 if (curaddr
->netmask
== NULL
) {
85 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
86 "malloc: %s", pcap_strerror(errno
));
91 curaddr
->netmask
= NULL
;
93 if (broadaddr
!= NULL
) {
94 curaddr
->broadaddr
= (struct sockaddr
*)dup_sockaddr(broadaddr
, sizeof(struct sockaddr_storage
));
95 if (curaddr
->broadaddr
== NULL
) {
96 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
97 "malloc: %s", pcap_strerror(errno
));
102 curaddr
->broadaddr
= NULL
;
104 if (dstaddr
!= NULL
) {
105 curaddr
->dstaddr
= (struct sockaddr
*)dup_sockaddr(dstaddr
, sizeof(struct sockaddr_storage
));
106 if (curaddr
->dstaddr
== NULL
) {
107 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
108 "malloc: %s", pcap_strerror(errno
));
113 curaddr
->dstaddr
= NULL
;
116 * Find the end of the list of addresses.
118 for (prevaddr
= curdev
->addresses
; prevaddr
!= NULL
; prevaddr
= nextaddr
) {
119 nextaddr
= prevaddr
->next
;
120 if (nextaddr
== NULL
) {
122 * This is the end of the list.
128 if (prevaddr
== NULL
) {
130 * The list was empty; this is the first member.
132 curdev
->addresses
= curaddr
;
135 * "prevaddr" is the last member of the list; append
138 prevaddr
->next
= curaddr
;
146 pcap_add_if_win32(pcap_if_t
**devlist
, char *name
, const char *desc
,
150 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
154 if_addr_size
= MAX_NETWORK_ADDRESSES
;
157 * Add an entry for this interface, with no addresses.
159 if (add_or_find_if(&curdev
, devlist
, name
, 0, desc
, errbuf
) == -1) {
167 * Get the list of addresses for the interface.
169 if (!PacketGetNetInfoEx((void *)name
, if_addrs
, &if_addr_size
)) {
173 * We don't return an error, because this can happen with
174 * NdisWan interfaces, and we want to supply them even
175 * if we can't supply their addresses.
177 * We return an entry with an empty address list.
183 * Now add the addresses.
185 while (if_addr_size
-- > 0) {
187 * "curdev" is an entry for this interface; add an entry for
188 * this address to its list of addresses.
192 res
= add_addr_to_list(curdev
,
193 (struct sockaddr
*)&if_addrs
[if_addr_size
].IPAddress
,
194 (struct sockaddr
*)&if_addrs
[if_addr_size
].SubnetMask
,
195 (struct sockaddr
*)&if_addrs
[if_addr_size
].Broadcast
,
211 * Get a list of all interfaces that are up and that we can open.
212 * Returns -1 on error, 0 otherwise.
213 * The list, as returned through "alldevsp", may be null if no interfaces
214 * were up and could be opened.
216 * Win32 implementation, based on WinPcap
219 pcap_findalldevs(pcap_if_t
**alldevsp
, char *errbuf
)
221 pcap_if_t
*devlist
= NULL
;
228 if (!PacketGetAdapterNames(NULL
, &NameLength
))
230 DWORD last_error
= GetLastError();
232 if (last_error
!= ERROR_INSUFFICIENT_BUFFER
)
234 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
235 "PacketGetAdapterNames: %s",
236 pcap_win32strerror());
242 AdaptersName
= (char*) malloc(NameLength
);
248 if (AdaptersName
== NULL
)
250 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Cannot allocate enough memory to list the adapters.");
254 if (!PacketGetAdapterNames(AdaptersName
, &NameLength
)) {
255 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
256 "PacketGetAdapterNames: %s",
257 pcap_win32strerror());
263 * "PacketGetAdapterNames()" returned a list of
264 * null-terminated ASCII interface name strings,
265 * terminated by a null string, followed by a list
266 * of null-terminated ASCII interface description
267 * strings, terminated by a null string.
268 * This means there are two ASCII nulls at the end
271 * Find the end of the first list; that's the
272 * beginning of the second list.
274 desc
= &AdaptersName
[0];
275 while (*desc
!= '\0' || *(desc
+ 1) != '\0')
279 * Found it - "desc" points to the first of the two
280 * nulls at the end of the list of names, so the
281 * first byte of the list of descriptions is two bytes
287 * Loop over the elements in the first list.
289 name
= &AdaptersName
[0];
290 while (*name
!= '\0') {
292 * Add an entry for this interface.
294 if (pcap_add_if_win32(&devlist
, name
, desc
, errbuf
) == -1) {
301 name
+= strlen(name
) + 1;
302 desc
+= strlen(desc
) + 1;
307 * We had an error; free the list we've been constructing.
309 if (devlist
!= NULL
) {
310 pcap_freealldevs(devlist
);