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.
38 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
46 pcap_add_if_win32(pcap_if_t
**devlist
, char *name
, const char *desc
,
50 npf_if_addr if_addrs
[MAX_NETWORK_ADDRESSES
];
54 if_addr_size
= MAX_NETWORK_ADDRESSES
;
57 * Add an entry for this interface, with no addresses.
59 if (add_or_find_if(&curdev
, devlist
, name
, 0, desc
, errbuf
) == -1) {
67 * Get the list of addresses for the interface.
69 if (!PacketGetNetInfoEx((void *)name
, if_addrs
, &if_addr_size
)) {
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.
77 * We return an entry with an empty address list.
83 * Now add the addresses.
85 while (if_addr_size
-- > 0) {
87 * "curdev" is an entry for this interface; add an entry for
88 * this address to its list of addresses.
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
),
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.
120 * Win32 implementation, based on WinPcap
123 pcap_findalldevs_interfaces(pcap_if_t
**alldevsp
, char *errbuf
)
125 pcap_if_t
*devlist
= NULL
;
131 char our_errbuf
[PCAP_ERRBUF_SIZE
+1];
134 * Find out how big a buffer we need.
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.
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.
151 if (!PacketGetAdapterNames(NULL
, &NameLength
))
153 DWORD last_error
= GetLastError();
155 if (last_error
!= ERROR_INSUFFICIENT_BUFFER
)
157 pcap_win32_err_to_str(last_error
, our_errbuf
);
158 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
159 "PacketGetAdapterNames: %s", our_errbuf
);
165 AdaptersName
= (char*) malloc(NameLength
);
171 if (AdaptersName
== NULL
)
173 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "Cannot allocate enough memory to list the adapters.");
177 if (!PacketGetAdapterNames(AdaptersName
, &NameLength
)) {
178 pcap_win32_err_to_str(GetLastError(), our_errbuf
);
179 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetAdapterNames: %s",
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
194 * Find the end of the first list; that's the
195 * beginning of the second list.
197 desc
= &AdaptersName
[0];
198 while (*desc
!= '\0' || *(desc
+ 1) != '\0')
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
210 * Loop over the elements in the first list.
212 name
= &AdaptersName
[0];
213 while (*name
!= '\0') {
215 * Add an entry for this interface.
217 if (pcap_add_if_win32(&devlist
, name
, desc
, errbuf
) == -1) {
224 name
+= strlen(name
) + 1;
225 desc
+= strlen(desc
) + 1;
230 * We haven't had any errors yet; do any platform-specific
231 * operations to add devices.
233 if (pcap_platform_finddevs(&devlist
, errbuf
) < 0)
239 * We had an error; free the list we've been constructing.
241 if (devlist
!= NULL
) {
242 pcap_freealldevs(devlist
);