Debian Codegraph @

Xpjproject/pjlib/src/pj/ip_helper_win32.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/config.h>
20
21 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23
24 /* PMIB_ICMP_EX is not declared in VC6, causing error.
25 * But EVC4, which also claims to be VC6, does have it!
26 */
27 #if defined(_MSC_VER) && _MSC_VER==1200 && !defined(PJ_WIN32_WINCE)
28 # define PMIB_ICMP_EX void*
29 #endif
30 #include <winsock2.h>
31
32 /* If you encounter error "Cannot open include file: 'Iphlpapi.h' here,
33 * you need to install newer Platform SDK. Presumably you're using
34 * Microsoft Visual Studio 6?
35 */
36 #include <Iphlpapi.h>
37
38 #include <pj/ip_helper.h>
39 #include <pj/assert.h>
40 #include <pj/errno.h>
41 #include <pj/string.h>
42
43 /* Dealing with Unicode quirks:
44
45 There seems to be a difference with GetProcAddress() API signature between
46 Windows (i.e. Win32) and Windows CE (e.g. Windows Mobile). On Windows, the
47 API is declared as:
48
49 FARPROC GetProcAddress(
50 HMODULE hModule,
51 LPCSTR lpProcName);
52
53 while on Windows CE:
54
55 FARPROC GetProcAddress(
56 HMODULE hModule,
57 LPCWSTR lpProcName);
58
59 Notice the difference with lpProcName argument type. This means that on
60 Windows, even on Unicode Windows, the lpProcName always takes ANSI format,
61 while on Windows CE, the argument follows the UNICODE setting.
62
63 Because of this, we use a different Unicode treatment here than the usual
64 PJ_NATIVE_STRING_IS_UNICODE PJLIB setting (<pj/unicode.h>):
65 - GPA_TEXT macro: convert literal string to platform's native literal
66 string
67 - gpa_char: the platform native character type
68
69 Note that "GPA" and "gpa" are abbreviations for GetProcAddress.
70 */
71 #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0
72 /* on CE, follow the PJLIB Unicode setting */
73 # define GPA_TEXT(x) PJ_T(x)
74 # define gpa_char pj_char_t
75 #else
76 /* on non-CE, always use ANSI format */
77 # define GPA_TEXT(x) x
78 # define gpa_char char
79 #endif
80
81
82 typedef DWORD (WINAPI *PFN_GetIpAddrTable)(PMIB_IPADDRTABLE pIpAddrTable,
83 PULONG pdwSize,
84 BOOL bOrder);
85 typedef DWORD (WINAPI *PFN_GetAdapterAddresses)(ULONG Family,
86 ULONG Flags,
87 PVOID Reserved,
88 PIP_ADAPTER_ADDRESSES AdapterAddresses,
89 PULONG SizePointer);
90 typedef DWORD (WINAPI *PFN_GetIpForwardTable)(PMIB_IPFORWARDTABLE pIpForwardTable,
91 PULONG pdwSize,
92 BOOL bOrder);
93 typedef DWORD (WINAPI *PFN_GetIfEntry)(PMIB_IFROW pIfRow);
94
95 static HANDLE s_hDLL;
96 static PFN_GetIpAddrTable s_pfnGetIpAddrTable;
97 static PFN_GetAdapterAddresses s_pfnGetAdapterAddresses;
98 static PFN_GetIpForwardTable s_pfnGetIpForwardTable;
99 static PFN_GetIfEntry s_pfnGetIfEntry;
100
101
102 static void unload_iphlp_module(void)
103 {
104 FreeLibrary(s_hDLL);
105 s_hDLL = NULL;
106 s_pfnGetIpAddrTable = NULL;
107 s_pfnGetIpForwardTable = NULL;
108 s_pfnGetIfEntry = NULL;
109 s_pfnGetAdapterAddresses = NULL;
110 }
111
112 static FARPROC GetIpHlpApiProc(gpa_char *lpProcName)
113 {
114 if(NULL == s_hDLL) {
115 s_hDLL = LoadLibrary(PJ_T("IpHlpApi"));
116 if(NULL != s_hDLL) {
117 pj_atexit(&unload_iphlp_module);
118 }
119 }
120
121 if(NULL != s_hDLL)
122 return GetProcAddress(s_hDLL, lpProcName);
123
124 return NULL;
125 }
126
127 static DWORD MyGetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable,
128 PULONG pdwSize,
129 BOOL bOrder)
130 {
131 if(NULL == s_pfnGetIpAddrTable) {
132 s_pfnGetIpAddrTable = (PFN_GetIpAddrTable)
133 GetIpHlpApiProc(GPA_TEXT("GetIpAddrTable"));
134 }
135
136 if(NULL != s_pfnGetIpAddrTable) {
137 return s_pfnGetIpAddrTable(pIpAddrTable, pdwSize, bOrder);
138 }
139
140 return ERROR_NOT_SUPPORTED;
141 }
142
143 static DWORD MyGetAdapterAddresses(ULONG Family,
144 ULONG Flags,
145 PVOID Reserved,
146 PIP_ADAPTER_ADDRESSES AdapterAddresses,
147 PULONG SizePointer)
148 {
149 if(NULL == s_pfnGetAdapterAddresses) {
150 s_pfnGetAdapterAddresses = (PFN_GetAdapterAddresses)
151 GetIpHlpApiProc(GPA_TEXT("GetAdaptersAddresses"));
152 }
153
154 if(NULL != s_pfnGetAdapterAddresses) {
155 return s_pfnGetAdapterAddresses(Family, Flags, Reserved,
156 AdapterAddresses, SizePointer);
157 }
158
159 return ERROR_NOT_SUPPORTED;
160 }
161
162 #if PJ_IP_HELPER_IGNORE_LOOPBACK_IF
163 static DWORD MyGetIfEntry(MIB_IFROW *pIfRow)
164 {
165 if(NULL == s_pfnGetIfEntry) {
166 s_pfnGetIfEntry = (PFN_GetIfEntry)
167 GetIpHlpApiProc(GPA_TEXT("GetIfEntry"));
168 }
169
170 if(NULL != s_pfnGetIfEntry) {
171 return s_pfnGetIfEntry(pIfRow);
172 }
173
174 return ERROR_NOT_SUPPORTED;
175 }
176 #endif
177
178
179 static DWORD MyGetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable,
180 PULONG pdwSize,
181 BOOL bOrder)
182 {
183 if(NULL == s_pfnGetIpForwardTable) {
184 s_pfnGetIpForwardTable = (PFN_GetIpForwardTable)
185 GetIpHlpApiProc(GPA_TEXT("GetIpForwardTable"));
186 }
187
188 if(NULL != s_pfnGetIpForwardTable) {
189 return s_pfnGetIpForwardTable(pIpForwardTable, pdwSize, bOrder);
190 }
191
192 return ERROR_NOT_SUPPORTED;
193 }
194
195 /* Enumerate local IP interface using GetIpAddrTable()
196 * for IPv4 addresses only.
197 */
198 static pj_status_t enum_ipv4_interface(unsigned *p_cnt,
199 pj_sockaddr ifs[])
200 {
201 char ipTabBuff[512];
202 MIB_IPADDRTABLE *pTab = (MIB_IPADDRTABLE*)ipTabBuff;
203 ULONG tabSize = sizeof(ipTabBuff);
204 unsigned i, count;
205 DWORD rc = NO_ERROR;
206
207 PJ_ASSERT_RETURN(p_cnt && ifs, PJ_EINVAL);
208
209 /* Get IP address table */
210 rc = MyGetIpAddrTable(pTab, &tabSize, FALSE);
211 if (rc != NO_ERROR) {
212 if (rc == ERROR_INSUFFICIENT_BUFFER) {
213 /* Retry with larger buffer */
214 pTab = (MIB_IPADDRTABLE*)malloc(tabSize);
215 if (pTab)
216 rc = MyGetIpAddrTable(pTab, &tabSize, FALSE);
217 }
218
219 if (rc != NO_ERROR) {
220 if (pTab != (MIB_IPADDRTABLE*)ipTabBuff)
221 free(pTab);
222 return PJ_RETURN_OS_ERROR(rc);
223 }
224 }
225
226 /* Reset result */
227 pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt));
228
229 /* Now fill out the entries */
230 count = (pTab->dwNumEntries < *p_cnt) ? pTab->dwNumEntries : *p_cnt;
231 *p_cnt = 0;
232 for (i=0; i<count; ++i) {
233 MIB_IFROW ifRow;
234
235 /* Ignore 0.0.0.0 address (interface is down?) */
236 if (pTab->table[i].dwAddr == 0)
237 continue;
238
239 /* Ignore 0.0.0.0/8 address. This is a special address
240 * which doesn't seem to have practical use.
241 */
242 if ((pj_ntohl(pTab->table[i].dwAddr) >> 24) == 0)
243 continue;
244
245 #if PJ_IP_HELPER_IGNORE_LOOPBACK_IF
246 /* Investigate the type of this interface */
247 pj_bzero(&ifRow, sizeof(ifRow));
248 ifRow.dwIndex = pTab->table[i].dwIndex;
249 if (MyGetIfEntry(&ifRow) != 0)
250 continue;
251
252 if (ifRow.dwType == MIB_IF_TYPE_LOOPBACK)
253 continue;
254 #endif
255
256 ifs[*p_cnt].ipv4.sin_family = PJ_AF_INET;
257 ifs[*p_cnt].ipv4.sin_addr.s_addr = pTab->table[i].dwAddr;
258 (*p_cnt)++;
259 }
260
261 if (pTab != (MIB_IPADDRTABLE*)ipTabBuff)
262 free(pTab);
263
264 return (*p_cnt) ? PJ_SUCCESS : PJ_ENOTFOUND;
265 }
266
267 /* Enumerate local IP interface using GetAdapterAddresses(),
268 * which works for both IPv4 and IPv6.
269 */
270 static pj_status_t enum_ipv4_ipv6_interface(int af,
271 unsigned *p_cnt,
272 pj_sockaddr ifs[])
273 {
274 pj_uint8_t buffer[600];
275 IP_ADAPTER_ADDRESSES *adapter = (IP_ADAPTER_ADDRESSES*)buffer;
276 void *adapterBuf = NULL;
277 ULONG size = sizeof(buffer);
278 ULONG flags;
279 unsigned i;
280 DWORD rc;
281
282 flags = GAA_FLAG_SKIP_FRIENDLY_NAME |
283 GAA_FLAG_SKIP_DNS_SERVER |
284 GAA_FLAG_SKIP_MULTICAST;
285
286 rc = MyGetAdapterAddresses(af, flags, NULL, adapter, &size);
287 if (rc != ERROR_SUCCESS) {
288 if (rc == ERROR_BUFFER_OVERFLOW) {
289 /* Retry with larger memory size */
290 adapterBuf = malloc(size);
291 adapter = (IP_ADAPTER_ADDRESSES*) adapterBuf;
292 if (adapter != NULL)
293 rc = MyGetAdapterAddresses(af, flags, NULL, adapter, &size);
294 }
295
296 if (rc != ERROR_SUCCESS) {
297 if (adapterBuf)
298 free(adapterBuf);
299 return PJ_RETURN_OS_ERROR(rc);
300 }
301 }
302
303 /* Reset result */
304 pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt));
305
306 /* Enumerate interface */
307 for (i=0; i<*p_cnt && adapter; adapter = adapter->Next) {
308 if (adapter->FirstUnicastAddress) {
309 SOCKET_ADDRESS *pAddr = &adapter->FirstUnicastAddress->Address;
310
311 /* Ignore address family which we didn't request, just in case */
312 if (pAddr->lpSockaddr->sa_family != PJ_AF_INET &&
313 pAddr->lpSockaddr->sa_family != PJ_AF_INET6)
314 {
315 continue;
316 }
317
318 /* Apply some filtering to known IPv4 unusable addresses */
319 if (pAddr->lpSockaddr->sa_family == PJ_AF_INET) {
320 const pj_sockaddr_in *addr_in =
321 (const pj_sockaddr_in*)pAddr->lpSockaddr;
322
323 /* Ignore 0.0.0.0 address (interface is down?) */
324 if (addr_in->sin_addr.s_addr == 0)
325 continue;
326
327 /* Ignore 0.0.0.0/8 address. This is a special address
328 * which doesn't seem to have practical use.
329 */
330 if ((pj_ntohl(addr_in->sin_addr.s_addr) >> 24) == 0)
331 continue;
332 }
333
334 #if PJ_IP_HELPER_IGNORE_LOOPBACK_IF
335 /* Ignore loopback interfaces */
336 /* This should have been IF_TYPE_SOFTWARE_LOOPBACK according to
337 * MSDN, and this macro should have been declared in Ipifcons.h,
338 * but some SDK versions don't have it.
339 */
340 if (adapter->IfType == MIB_IF_TYPE_LOOPBACK)
341 continue;
342 #endif
343
344 /* Ignore down interface */
345 if (adapter->OperStatus != IfOperStatusUp)
346 continue;
347
348 ifs[i].addr.sa_family = pAddr->lpSockaddr->sa_family;
349 pj_memcpy(&ifs[i], pAddr->lpSockaddr, pAddr->iSockaddrLength);
350 ++i;
351 }
352 }
353
354 if (adapterBuf)
355 free(adapterBuf);
356
357 *p_cnt = i;
358 return (*p_cnt) ? PJ_SUCCESS : PJ_ENOTFOUND;
359 }
360
361
362 /*
363 * Enumerate the local IP interface currently active in the host.
364 */
365 PJ_DEF(pj_status_t) pj_enum_ip_interface(int af,
366 unsigned *p_cnt,
367 pj_sockaddr ifs[])
368 {
369 pj_status_t status = -1;
370
371 PJ_ASSERT_RETURN(p_cnt && ifs, PJ_EINVAL);
372 PJ_ASSERT_RETURN(af==PJ_AF_UNSPEC || af==PJ_AF_INET || af==PJ_AF_INET6,
373 PJ_EAFNOTSUP);
374
375 status = enum_ipv4_ipv6_interface(af, p_cnt, ifs);
376 if (status != PJ_SUCCESS && (af==PJ_AF_INET || af==PJ_AF_UNSPEC))
377 status = enum_ipv4_interface(p_cnt, ifs);
378 return status;
379 }
380
381
382 /*
383 * Enumerate the local IP interface currently active in the host.
384 */
385 PJ_DEF(pj_status_t) pj_enum_ip_interface2( const pj_enum_ip_option *opt,
386 unsigned *p_cnt,
387 pj_sockaddr ifs[])
388 {
389 pj_enum_ip_option opt_;
390
391 if (opt && opt->omit_deprecated_ipv6)
392 return PJ_ENOTSUP;
393
394 if (opt)
395 opt_ = *opt;
396 else
397 pj_enum_ip_option_default(&opt_);
398
399 return pj_enum_ip_interface(opt_.af, p_cnt, ifs);
400 }
401
402
403 /*
404 * Enumerate the IP routing table for this host.
405 */
406 PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt,
407 pj_ip_route_entry routes[])
408 {
409 char ipTabBuff[1024];
410 MIB_IPADDRTABLE *pIpTab;
411 char rtabBuff[1024];
412 MIB_IPFORWARDTABLE *prTab;
413 ULONG tabSize;
414 unsigned i, count;
415 DWORD rc = NO_ERROR;
416
417 PJ_ASSERT_RETURN(p_cnt && routes, PJ_EINVAL);
418
419 pIpTab = (MIB_IPADDRTABLE *)ipTabBuff;
420 prTab = (MIB_IPFORWARDTABLE *)rtabBuff;
421
422 /* First get IP address table */
423 tabSize = sizeof(ipTabBuff);
424 rc = MyGetIpAddrTable(pIpTab, &tabSize, FALSE);
425 if (rc != NO_ERROR)
426 return PJ_RETURN_OS_ERROR(rc);
427
428 /* Next get IP route table */
429 tabSize = sizeof(rtabBuff);
430
431 rc = MyGetIpForwardTable(prTab, &tabSize, 1);
432 if (rc != NO_ERROR)
433 return PJ_RETURN_OS_ERROR(rc);
434
435 /* Reset routes */
436 pj_bzero(routes, sizeof(routes[0]) * (*p_cnt));
437
438 /* Now fill out the route entries */
439 count = (prTab->dwNumEntries < *p_cnt) ? prTab->dwNumEntries : *p_cnt;
440 *p_cnt = 0;
441 for (i=0; i<count; ++i) {
442 unsigned j;
443
444 /* Find interface entry */
445 for (j=0; j<pIpTab->dwNumEntries; ++j) {
446 if (pIpTab->table[j].dwIndex == prTab->table[i].dwForwardIfIndex)
447 break;
448 }
449
450 if (j==pIpTab->dwNumEntries)
451 continue; /* Interface not found */
452
453 routes[*p_cnt].ipv4.if_addr.s_addr = pIpTab->table[j].dwAddr;
454 routes[*p_cnt].ipv4.dst_addr.s_addr = prTab->table[i].dwForwardDest;
455 routes[*p_cnt].ipv4.mask.s_addr = prTab->table[i].dwForwardMask;
456
457 (*p_cnt)++;
458 }
459
460 return PJ_SUCCESS;
461 }
462