Big merge in wine code. This merged version is far more complete than
[reactos.git] / reactos / lib / iphlpapi / iphlpapi_main.c
1 /*
2 * iphlpapi dll implementation
3 *
4 * Copyright (C) 2003 Juan Lang
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #ifdef HAVE_NETINET_IN_H
29 # include <netinet/in.h>
30 #endif
31 #ifdef HAVE_ARPA_INET_H
32 # include <arpa/inet.h>
33 #endif
34 #ifdef HAVE_ARPA_NAMESER_H
35 # include <arpa/nameser.h>
36 #endif
37 #ifdef HAVE_RESOLV_H
38 # include <resolv.h>
39 #endif
40
41 #ifdef __REACTOS__
42 # include <windows.h>
43 # include <windef.h>
44 # include <winbase.h>
45 # include <net/miniport.h>
46 # include <winsock2.h>
47 # include <nspapi.h>
48 # include <iptypes.h>
49 # include "iphlpapiextra.h"
50 # include "wine/debug.h"
51 #else
52 # include "windef.h"
53 # include "winbase.h"
54 # include "winreg.h"
55 # include "debug.h"
56 #endif
57
58 #include <stdio.h>
59 #include "iphlpapi.h"
60 #include "ifenum.h"
61 #include "ipstats.h"
62 #include "iphlp_res.h"
63
64 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
65
66 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
67 {
68 switch (fdwReason) {
69 case DLL_PROCESS_ATTACH:
70 DisableThreadLibraryCalls( hinstDLL );
71 interfaceMapInit();
72 break;
73
74 case DLL_PROCESS_DETACH:
75 interfaceMapFree();
76 break;
77 }
78 return TRUE;
79 }
80
81 /******************************************************************
82 * AddIPAddress (IPHLPAPI.@)
83 *
84 *
85 * PARAMS
86 *
87 * Address [In]
88 * IpMask [In]
89 * IfIndex [In]
90 * NTEContext [In/Out]
91 * NTEInstance [In/Out]
92 *
93 * RETURNS
94 *
95 * DWORD
96 *
97 */
98 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
99 {
100 FIXME(":stub\n");
101 /* marking Win2K+ functions not supported */
102 return ERROR_NOT_SUPPORTED;
103 }
104
105
106 /******************************************************************
107 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
108 *
109 *
110 * PARAMS
111 *
112 * ppIfTable [Out] -- pointer into which the MIB_IFTABLE is
113 * allocated and returned.
114 * bOrder [In] -- passed to GetIfTable to order the table
115 * heap [In] -- heap from which the table is allocated
116 * flags [In] -- flags to HeapAlloc
117 *
118 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
119 * GetIfTable returns otherwise
120 *
121 */
122 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
123 BOOL bOrder, HANDLE heap, DWORD flags)
124 {
125 DWORD ret;
126
127 TRACE("ppIfTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n", ppIfTable,
128 (DWORD)bOrder, (DWORD)heap, flags);
129 if (!ppIfTable)
130 ret = ERROR_INVALID_PARAMETER;
131 else {
132 DWORD dwSize = 0;
133
134 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
135 if (ret == ERROR_INSUFFICIENT_BUFFER) {
136 *ppIfTable = (PMIB_IFTABLE)HeapAlloc(heap, flags, dwSize);
137 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
138 }
139 }
140 TRACE("returning %ld\n", ret);
141 return ret;
142 }
143
144
145 /******************************************************************
146 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
147 *
148 *
149 * PARAMS
150 *
151 * ppIpAddrTable [Out]
152 * bOrder [In] -- passed to GetIpAddrTable to order the table
153 * heap [In] -- heap from which the table is allocated
154 * flags [In] -- flags to HeapAlloc
155 *
156 * RETURNS
157 *
158 * DWORD
159 *
160 */
161 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
162 BOOL bOrder, HANDLE heap, DWORD flags)
163 {
164 DWORD ret;
165
166 TRACE("ppIpAddrTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
167 ppIpAddrTable, (DWORD)bOrder, (DWORD)heap, flags);
168 if (!ppIpAddrTable)
169 ret = ERROR_INVALID_PARAMETER;
170 else {
171 DWORD dwSize = 0;
172
173 ret = GetIpAddrTable(*ppIpAddrTable, &dwSize, bOrder);
174 if (ret == ERROR_INSUFFICIENT_BUFFER) {
175 *ppIpAddrTable = (PMIB_IPADDRTABLE)HeapAlloc(heap, flags, dwSize);
176 ret = GetIpAddrTable(*ppIpAddrTable, &dwSize, bOrder);
177 }
178 }
179 TRACE("returning %ld\n", ret);
180 return ret;
181 }
182
183
184 /******************************************************************
185 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
186 *
187 *
188 * ppIpForwardTable [Out] -- pointer into which the MIB_IPFORWARDTABLE is
189 * allocated and returned.
190 * bOrder [In] -- passed to GetIfTable to order the table
191 * heap [In] -- heap from which the table is allocated
192 * flags [In] -- flags to HeapAlloc
193 *
194 * RETURNS -- ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
195 * GetIpForwardTable returns otherwise
196 *
197 */
198 DWORD WINAPI AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE *
199 ppIpForwardTable, BOOL bOrder, HANDLE heap, DWORD flags)
200 {
201 DWORD ret;
202
203 TRACE("ppIpForwardTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
204 ppIpForwardTable, (DWORD)bOrder, (DWORD)heap, flags);
205 if (!ppIpForwardTable)
206 ret = ERROR_INVALID_PARAMETER;
207 else {
208 DWORD dwSize = 0;
209
210 ret = GetIpForwardTable(*ppIpForwardTable, &dwSize, bOrder);
211 if (ret == ERROR_INSUFFICIENT_BUFFER) {
212 *ppIpForwardTable = (PMIB_IPFORWARDTABLE)HeapAlloc(heap, flags, dwSize);
213 ret = GetIpForwardTable(*ppIpForwardTable, &dwSize, bOrder);
214 }
215 }
216 TRACE("returning %ld\n", ret);
217 return ret;
218 }
219
220
221 /******************************************************************
222 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
223 *
224 *
225 * PARAMS
226 *
227 * ppIpNetTable [Out]
228 * bOrder [In] -- passed to GetIpNetTable to order the table
229 * heap [In] -- heap from which the table is allocated
230 * flags [In] -- flags to HeapAlloc
231 *
232 * RETURNS
233 *
234 * DWORD
235 *
236 */
237 DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable,
238 BOOL bOrder, HANDLE heap, DWORD flags)
239 {
240 DWORD ret;
241
242 TRACE("ppIpNetTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
243 ppIpNetTable, (DWORD)bOrder, (DWORD)heap, flags);
244 if (!ppIpNetTable)
245 ret = ERROR_INVALID_PARAMETER;
246 else {
247 DWORD dwSize = 0;
248
249 ret = GetIpNetTable(*ppIpNetTable, &dwSize, bOrder);
250 if (ret == ERROR_INSUFFICIENT_BUFFER) {
251 *ppIpNetTable = (PMIB_IPNETTABLE)HeapAlloc(heap, flags, dwSize);
252 ret = GetIpNetTable(*ppIpNetTable, &dwSize, bOrder);
253 }
254 }
255 TRACE("returning %ld\n", ret);
256 return ret;
257 }
258
259
260 /******************************************************************
261 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
262 *
263 *
264 * PARAMS
265 *
266 * ppTcpTable [Out]
267 * bOrder [In] -- passed to GetTcpTable to order the table
268 * heap [In] -- heap from which the table is allocated
269 * flags [In] -- flags to HeapAlloc
270 *
271 * RETURNS
272 *
273 * DWORD
274 *
275 */
276 DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable,
277 BOOL bOrder, HANDLE heap, DWORD flags)
278 {
279 DWORD ret;
280
281 TRACE("ppTcpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
282 ppTcpTable, (DWORD)bOrder, (DWORD)heap, flags);
283 if (!ppTcpTable)
284 ret = ERROR_INVALID_PARAMETER;
285 else {
286 DWORD dwSize = 0;
287
288 ret = GetTcpTable(*ppTcpTable, &dwSize, bOrder);
289 if (ret == ERROR_INSUFFICIENT_BUFFER) {
290 *ppTcpTable = (PMIB_TCPTABLE)HeapAlloc(heap, flags, dwSize);
291 ret = GetTcpTable(*ppTcpTable, &dwSize, bOrder);
292 }
293 }
294 TRACE("returning %ld\n", ret);
295 return ret;
296 }
297
298
299 /******************************************************************
300 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
301 *
302 *
303 * PARAMS
304 *
305 * ppUdpTable [Out]
306 * bOrder [In] -- passed to GetUdpTable to order the table
307 * heap [In] -- heap from which the table is allocated
308 * flags [In] -- flags to HeapAlloc
309 *
310 * RETURNS
311 *
312 * DWORD
313 *
314 */
315 DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable,
316 BOOL bOrder, HANDLE heap, DWORD flags)
317 {
318 DWORD ret;
319
320 TRACE("ppUdpTable %p, bOrder %ld, heap 0x%08lx, flags 0x%08lx\n",
321 ppUdpTable, (DWORD)bOrder, (DWORD)heap, flags);
322 if (!ppUdpTable)
323 ret = ERROR_INVALID_PARAMETER;
324 else {
325 DWORD dwSize = 0;
326
327 ret = GetUdpTable(*ppUdpTable, &dwSize, bOrder);
328 if (ret == ERROR_INSUFFICIENT_BUFFER) {
329 *ppUdpTable = (PMIB_UDPTABLE)HeapAlloc(heap, flags, dwSize);
330 ret = GetUdpTable(*ppUdpTable, &dwSize, bOrder);
331 }
332 }
333 TRACE("returning %ld\n", ret);
334 return ret;
335 }
336
337
338 /******************************************************************
339 * CreateIpForwardEntry (IPHLPAPI.@)
340 *
341 *
342 * PARAMS
343 *
344 * pRoute [In/Out]
345 *
346 * RETURNS
347 *
348 * DWORD
349 *
350 */
351 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
352 {
353 TRACE("pRoute %p\n", pRoute);
354 /* could use SIOCADDRT, not sure I want to */
355 FIXME(":stub\n");
356 return (DWORD) 0;
357 }
358
359
360 /******************************************************************
361 * CreateIpNetEntry (IPHLPAPI.@)
362 *
363 *
364 * PARAMS
365 *
366 * pArpEntry [In/Out]
367 *
368 * RETURNS
369 *
370 * DWORD
371 *
372 */
373 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
374 {
375 TRACE("pArpEntry %p\n", pArpEntry);
376 /* could use SIOCSARP on systems that support it, not sure I want to */
377 FIXME(":stub\n");
378 return (DWORD) 0;
379 }
380
381
382 /******************************************************************
383 * CreateProxyArpEntry (IPHLPAPI.@)
384 *
385 *
386 * PARAMS
387 *
388 * dwAddress [In]
389 * dwMask [In]
390 * dwIfIndex [In]
391 *
392 * RETURNS
393 *
394 * DWORD
395 *
396 */
397 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
398 {
399 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress,
400 dwMask, dwIfIndex);
401 FIXME(":stub\n");
402 /* marking Win2K+ functions not supported */
403 return ERROR_NOT_SUPPORTED;
404 }
405
406
407 /******************************************************************
408 * DeleteIPAddress (IPHLPAPI.@)
409 *
410 *
411 * PARAMS
412 *
413 * NTEContext [In]
414 *
415 * RETURNS
416 *
417 * DWORD
418 *
419 */
420 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
421 {
422 TRACE("NTEContext %ld\n", NTEContext);
423 FIXME(":stub\n");
424 /* marking Win2K+ functions not supported */
425 return ERROR_NOT_SUPPORTED;
426 }
427
428
429 /******************************************************************
430 * DeleteIpForwardEntry (IPHLPAPI.@)
431 *
432 *
433 * PARAMS
434 *
435 * pRoute [In/Out]
436 *
437 * RETURNS
438 *
439 * DWORD
440 *
441 */
442 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
443 {
444 TRACE("pRoute %p\n", pRoute);
445 /* could use SIOCDELRT, not sure I want to */
446 FIXME(":stub\n");
447 return (DWORD) 0;
448 }
449
450
451 /******************************************************************
452 * DeleteIpNetEntry (IPHLPAPI.@)
453 *
454 *
455 * PARAMS
456 *
457 * pArpEntry [In/Out]
458 *
459 * RETURNS
460 *
461 * DWORD
462 *
463 */
464 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
465 {
466 TRACE("pArpEntry %p\n", pArpEntry);
467 /* could use SIOCDARP on systems that support it, not sure I want to */
468 FIXME(":stub\n");
469 return (DWORD) 0;
470 }
471
472
473 /******************************************************************
474 * DeleteProxyArpEntry (IPHLPAPI.@)
475 *
476 *
477 * PARAMS
478 *
479 * dwAddress [In]
480 * dwMask [In]
481 * dwIfIndex [In]
482 *
483 * RETURNS
484 *
485 * DWORD
486 *
487 */
488 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
489 {
490 TRACE("dwAddress 0x%08lx, dwMask 0x%08lx, dwIfIndex 0x%08lx\n", dwAddress,
491 dwMask, dwIfIndex);
492 FIXME(":stub\n");
493 /* marking Win2K+ functions not supported */
494 return ERROR_NOT_SUPPORTED;
495 }
496
497 /******************************************************************
498 * EnableRouter (IPHLPAPI.@)
499 *
500 *
501 * PARAMS
502 *
503 * pHandle [In/Out]
504 * pOverlapped [In/Out]
505 *
506 * RETURNS
507 *
508 * DWORD
509 *
510 */
511 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
512 {
513 TRACE("pHandle %p, pOverlapped %p\n", pHandle, pOverlapped);
514 FIXME(":stub\n");
515 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
516 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
517 marking Win2K+ functions not supported */
518 return ERROR_NOT_SUPPORTED;
519 }
520
521
522 /******************************************************************
523 * FlushIpNetTable (IPHLPAPI.@)
524 *
525 *
526 * PARAMS
527 *
528 * dwIfIndex [In]
529 *
530 * RETURNS
531 *
532 * DWORD
533 *
534 */
535 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
536 {
537 TRACE("dwIfIndex 0x%08lx\n", dwIfIndex);
538 FIXME(":stub\n");
539 /* this flushes the arp cache of the given index
540 marking Win2K+ functions not supported */
541 return ERROR_NOT_SUPPORTED;
542 }
543
544
545 /******************************************************************
546 * GetAdapterIndex (IPHLPAPI.@)
547 *
548 *
549 * PARAMS
550 *
551 * AdapterName [In/Out]
552 * IfIndex [In/Out]
553 *
554 * RETURNS
555 *
556 * DWORD
557 *
558 */
559 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
560 {
561 TRACE("AdapterName %p, IfIndex %p\n", AdapterName, IfIndex);
562 FIXME(":stub\n");
563 /* marking Win2K+ functions not supported */
564 return ERROR_NOT_SUPPORTED;
565 }
566
567
568 /******************************************************************
569 * GetAdaptersInfo (IPHLPAPI.@)
570 *
571 *
572 * PARAMS
573 *
574 * pAdapterInfo [In/Out]
575 * pOutBufLen [In/Out]
576 *
577 * RETURNS
578 *
579 * DWORD
580 *
581 */
582 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
583 {
584 DWORD ret;
585
586 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
587 if (!pOutBufLen)
588 ret = ERROR_INVALID_PARAMETER;
589 else {
590 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
591
592 if (numNonLoopbackInterfaces > 0) {
593 /* this calculation assumes only one address in the IP_ADDR_STRING lists.
594 that's okay, because:
595 - we don't get multiple addresses per adapter anyway
596 - we don't know about per-adapter gateways
597 - DHCP and WINS servers can have max one entry per list */
598 ULONG size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
599
600 if (!pAdapterInfo || *pOutBufLen < size) {
601 *pOutBufLen = size;
602 ret = ERROR_BUFFER_OVERFLOW;
603 }
604 else {
605 InterfaceIndexTable *table = getNonLoopbackInterfaceIndexTable();
606
607 if (table) {
608 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
609 if (*pOutBufLen < size) {
610 *pOutBufLen = size;
611 ret = ERROR_INSUFFICIENT_BUFFER;
612 }
613 else {
614 DWORD ndx;
615 HKEY hKey;
616 BOOL winsEnabled = FALSE;
617 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
618
619 memset(pAdapterInfo, 0, size);
620 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
621 "Software\\Wine\\Wine\\Config\\Network", 0, KEY_READ,
622 &hKey) == ERROR_SUCCESS) {
623 DWORD size = sizeof(primaryWINS.String);
624 unsigned long addr;
625
626 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
627 primaryWINS.String, &size);
628 addr = inet_addr(primaryWINS.String);
629 if (addr != INADDR_NONE && addr != INADDR_ANY)
630 winsEnabled = TRUE;
631 size = sizeof(secondaryWINS.String);
632 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
633 secondaryWINS.String, &size);
634 addr = inet_addr(secondaryWINS.String);
635 if (addr != INADDR_NONE && addr != INADDR_ANY)
636 winsEnabled = TRUE;
637 RegCloseKey(hKey);
638 }
639 for (ndx = 0; ndx < table->numIndexes; ndx++) {
640 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
641 DWORD addrLen = sizeof(ptr->Address), type;
642 const char *ifname =
643 getInterfaceNameByIndex(table->indexes[ndx]);
644
645 /* on Win98 this is left empty, but whatever */
646
647 strncpy(ptr->AdapterName,ifname,sizeof(ptr->AdapterName));
648 consumeInterfaceName(ifname);
649 ptr->AdapterName[MAX_ADAPTER_NAME_LENGTH] = '\0';
650 getInterfacePhysicalByIndex(table->indexes[ndx], &addrLen,
651 ptr->Address, &type);
652 /* MS defines address length and type as UINT in some places and
653 DWORD in others, **sigh**. Don't want to assume that PUINT and
654 PDWORD are equiv (64-bit?) */
655 ptr->AddressLength = addrLen;
656 ptr->Type = type;
657 ptr->Index = table->indexes[ndx];
658 toIPAddressString(getInterfaceIPAddrByIndex(table->indexes[ndx]),
659 ptr->IpAddressList.IpAddress.String);
660 toIPAddressString(getInterfaceMaskByIndex(table->indexes[ndx]),
661 ptr->IpAddressList.IpMask.String);
662 if (winsEnabled) {
663 ptr->HaveWins = TRUE;
664 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
665 primaryWINS.String, sizeof(primaryWINS.String));
666 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
667 secondaryWINS.String, sizeof(secondaryWINS.String));
668 }
669 if (ndx < table->numIndexes - 1)
670 ptr->Next = &pAdapterInfo[ndx + 1];
671 else
672 ptr->Next = NULL;
673 }
674 ret = NO_ERROR;
675 }
676 free(table);
677 }
678 else
679 ret = ERROR_OUTOFMEMORY;
680 }
681 }
682 else
683 ret = ERROR_NO_DATA;
684 }
685 TRACE("returning %ld\n", ret);
686 return ret;
687 }
688
689
690 /******************************************************************
691 * GetBestInterface (IPHLPAPI.@)
692 *
693 *
694 * PARAMS
695 *
696 * dwDestAddr [In]
697 * pdwBestIfIndex [In/Out]
698 *
699 * RETURNS
700 *
701 * DWORD
702 *
703 */
704 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
705 {
706 DWORD ret;
707
708 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr, pdwBestIfIndex);
709 if (!pdwBestIfIndex)
710 ret = ERROR_INVALID_PARAMETER;
711 else {
712 MIB_IPFORWARDROW ipRow;
713
714 ret = GetBestRoute(dwDestAddr, 0, &ipRow);
715 if (ret == ERROR_SUCCESS)
716 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
717 }
718 TRACE("returning %ld\n", ret);
719 return ret;
720 }
721
722
723 /******************************************************************
724 * GetBestRoute (IPHLPAPI.@)
725 *
726 *
727 * PARAMS
728 *
729 * dwDestAddr [In]
730 * dwSourceAddr [In]
731 * OUT [In]
732 *
733 * RETURNS
734 *
735 * DWORD
736 *
737 */
738 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
739 {
740 PMIB_IPFORWARDTABLE table;
741 DWORD ret;
742
743 TRACE("dwDestAddr 0x%08lx, dwSourceAddr 0x%08lx, pBestRoute %p\n", dwDestAddr,
744 dwSourceAddr, pBestRoute);
745 if (!pBestRoute)
746 return ERROR_INVALID_PARAMETER;
747
748 AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
749 if (table) {
750 DWORD ndx, matchedBits, matchedNdx = 0;
751
752 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
753 if ((dwDestAddr & table->table[ndx].dwForwardMask) ==
754 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
755 DWORD numShifts, mask;
756
757 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
758 mask && !(mask & 1); mask >>= 1, numShifts++)
759 ;
760 if (numShifts > matchedBits) {
761 matchedBits = numShifts;
762 matchedNdx = ndx;
763 }
764 }
765 }
766 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
767 HeapFree(GetProcessHeap(), 0, table);
768 ret = ERROR_SUCCESS;
769 }
770 else
771 ret = ERROR_OUTOFMEMORY;
772 TRACE("returning %ld\n", ret);
773 return ret;
774 }
775
776
777 /******************************************************************
778 * GetFriendlyIfIndex (IPHLPAPI.@)
779 *
780 *
781 * PARAMS
782 *
783 * IfIndex [In]
784 *
785 * RETURNS
786 *
787 * DWORD
788 *
789 */
790 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
791 {
792 /* windows doesn't validate these, either, just makes sure the top byte is
793 cleared. I assume my ifenum module never gives an index with the top
794 byte set. */
795 TRACE("returning %ld\n", IfIndex);
796 return IfIndex;
797 }
798
799
800 /******************************************************************
801 * GetIcmpStatistics (IPHLPAPI.@)
802 *
803 *
804 * PARAMS
805 *
806 * pStats [In/Out]
807 *
808 * RETURNS
809 *
810 * DWORD
811 *
812 */
813 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
814 {
815 DWORD ret;
816
817 TRACE("pStats %p\n", pStats);
818 ret = getICMPStats(pStats);
819 TRACE("returning %ld\n", ret);
820 return ret;
821 }
822
823
824 /******************************************************************
825 * GetIfEntry (IPHLPAPI.@)
826 *
827 *
828 * PARAMS
829 *
830 * pIfRow [In/Out]
831 *
832 * RETURNS
833 *
834 * DWORD
835 *
836 */
837 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
838 {
839 DWORD ret;
840 const char *name;
841
842 TRACE("pIfRow %p\n", pIfRow);
843 if (!pIfRow)
844 return ERROR_INVALID_PARAMETER;
845
846 name = getInterfaceNameByIndex(pIfRow->dwIndex);
847 if (name) {
848 ret = getInterfaceEntryByIndex(pIfRow->dwIndex, pIfRow);
849 if (ret == NO_ERROR)
850 ret = getInterfaceStatsByIndex(pIfRow->dwIndex, pIfRow);
851 consumeInterfaceName(name);
852 }
853 else
854 ret = ERROR_INVALID_DATA;
855 TRACE("returning %ld\n", ret);
856 return ret;
857 }
858
859
860 static int IfTableSorter(const void *a, const void *b)
861 {
862 int ret;
863
864 if (a && b)
865 ret = ((PMIB_IFROW)a)->dwIndex - ((PMIB_IFROW)b)->dwIndex;
866 else
867 ret = 0;
868 return ret;
869 }
870
871
872 /******************************************************************
873 * GetIfTable (IPHLPAPI.@)
874 *
875 *
876 * PARAMS
877 *
878 * pIfTable [In/Out]
879 * pdwSize [In/Out]
880 * bOrder [In]
881 *
882 * RETURNS
883 *
884 * DWORD
885 *
886 */
887 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
888 {
889 DWORD ret;
890
891 TRACE("pIfTable %p, pdwSize %p, bOrder %ld\n", pdwSize, pdwSize,
892 (DWORD)bOrder);
893 if (!pdwSize)
894 ret = ERROR_INVALID_PARAMETER;
895 else {
896 DWORD numInterfaces = getNumInterfaces();
897 TRACE("GetIfTable: numInterfaces = %d\n", (int)numInterfaces);
898 ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW);
899
900 if (!pIfTable || *pdwSize < size) {
901 *pdwSize = size;
902 ret = ERROR_INSUFFICIENT_BUFFER;
903 }
904 else {
905 InterfaceIndexTable *table = getInterfaceIndexTable();
906
907 if (table) {
908 size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) *
909 sizeof(MIB_IFROW);
910 if (*pdwSize < size) {
911 *pdwSize = size;
912 ret = ERROR_INSUFFICIENT_BUFFER;
913 }
914 else {
915 DWORD ndx;
916
917 pIfTable->dwNumEntries = 0;
918 for (ndx = 0; ndx < table->numIndexes; ndx++) {
919 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
920 GetIfEntry(&pIfTable->table[ndx]);
921 pIfTable->dwNumEntries++;
922 }
923 if (bOrder)
924 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
925 IfTableSorter);
926 ret = NO_ERROR;
927 }
928 free(table);
929 }
930 else
931 ret = ERROR_OUTOFMEMORY;
932 }
933 }
934 TRACE("returning %ld\n", ret);
935 return ret;
936 }
937
938
939 /******************************************************************
940 * GetInterfaceInfo (IPHLPAPI.@)
941 *
942 *
943 * PARAMS
944 *
945 * pIfTable [In/Out]
946 * dwOutBufLen [In/Out]
947 *
948 * RETURNS
949 *
950 * DWORD
951 *
952 */
953 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
954 {
955 DWORD ret;
956
957 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
958 if (!dwOutBufLen)
959 ret = ERROR_INVALID_PARAMETER;
960 else {
961 DWORD numInterfaces = getNumInterfaces();
962 ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) *
963 sizeof(IP_ADAPTER_INDEX_MAP);
964
965 if (!pIfTable || *dwOutBufLen < size) {
966 *dwOutBufLen = size;
967 ret = ERROR_INSUFFICIENT_BUFFER;
968 }
969 else {
970 InterfaceIndexTable *table = getInterfaceIndexTable();
971
972 if (table) {
973 size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) *
974 sizeof(IP_ADAPTER_INDEX_MAP);
975 if (*dwOutBufLen < size) {
976 *dwOutBufLen = size;
977 ret = ERROR_INSUFFICIENT_BUFFER;
978 }
979 else {
980 DWORD ndx;
981
982 pIfTable->NumAdapters = 0;
983 for (ndx = 0; ndx < table->numIndexes; ndx++) {
984 const char *walker, *name;
985 WCHAR *assigner;
986
987 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
988 name = getInterfaceNameByIndex(table->indexes[ndx]);
989 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
990 walker && *walker &&
991 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
992 walker++, assigner++)
993 *assigner = *walker;
994 *assigner = 0;
995 consumeInterfaceName(name);
996 pIfTable->NumAdapters++;
997 }
998 ret = NO_ERROR;
999 }
1000 free(table);
1001 }
1002 else
1003 ret = ERROR_OUTOFMEMORY;
1004 }
1005 }
1006 TRACE("returning %ld\n", ret);
1007 return ret;
1008 }
1009
1010
1011 static int IpAddrTableSorter(const void *a, const void *b)
1012 {
1013 int ret;
1014
1015 if (a && b)
1016 ret = ((PMIB_IPADDRROW)a)->dwAddr - ((PMIB_IPADDRROW)b)->dwAddr;
1017 else
1018 ret = 0;
1019 return ret;
1020 }
1021
1022
1023 /******************************************************************
1024 * GetIpAddrTable (IPHLPAPI.@)
1025 *
1026 *
1027 * PARAMS
1028 *
1029 * pIpAddrTable [In/Out]
1030 * pdwSize [In/Out]
1031 * bOrder [In]
1032 *
1033 * RETURNS
1034 *
1035 * DWORD
1036 *
1037 */
1038 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1039 {
1040 DWORD ret;
1041
1042 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %ld\n", pIpAddrTable, pdwSize,
1043 (DWORD)bOrder);
1044 if (!pdwSize)
1045 ret = ERROR_INVALID_PARAMETER;
1046 else {
1047 DWORD numInterfaces = getNumInterfaces();
1048 ULONG size = sizeof(MIB_IPADDRTABLE) + (numInterfaces - 1) *
1049 sizeof(MIB_IPADDRROW);
1050
1051 if (!pIpAddrTable || *pdwSize < size) {
1052 *pdwSize = size;
1053 ret = ERROR_INSUFFICIENT_BUFFER;
1054 }
1055 else {
1056 InterfaceIndexTable *table = getInterfaceIndexTable();
1057
1058 if (table) {
1059 size = sizeof(MIB_IPADDRTABLE) + (table->numIndexes - 1) *
1060 sizeof(MIB_IPADDRROW);
1061 if (*pdwSize < size) {
1062 *pdwSize = size;
1063 ret = ERROR_INSUFFICIENT_BUFFER;
1064 }
1065 else {
1066 DWORD ndx, bcast;
1067
1068 pIpAddrTable->dwNumEntries = 0;
1069 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1070 pIpAddrTable->table[ndx].dwIndex = table->indexes[ndx];
1071 pIpAddrTable->table[ndx].dwAddr =
1072 getInterfaceIPAddrByIndex(table->indexes[ndx]);
1073 pIpAddrTable->table[ndx].dwMask =
1074 getInterfaceMaskByIndex(table->indexes[ndx]);
1075 /* the dwBCastAddr member isn't the broadcast address, it indicates
1076 * whether the interface uses the 1's broadcast address (1) or the
1077 * 0's broadcast address (0).
1078 */
1079 bcast = getInterfaceBCastAddrByIndex(table->indexes[ndx]);
1080 pIpAddrTable->table[ndx].dwBCastAddr =
1081 (bcast & pIpAddrTable->table[ndx].dwMask) ? 1 : 0;
1082 /* FIXME: hardcoded reasm size, not sure where to get it */
1083 pIpAddrTable->table[ndx].dwReasmSize = 65535;
1084 pIpAddrTable->table[ndx].unused1 = 0;
1085 pIpAddrTable->table[ndx].wType = 0; /* aka unused2 */
1086 pIpAddrTable->dwNumEntries++;
1087 }
1088 if (bOrder)
1089 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1090 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1091 ret = NO_ERROR;
1092 }
1093 free(table);
1094 }
1095 else
1096 ret = ERROR_OUTOFMEMORY;
1097 }
1098 }
1099 TRACE("returning %ld\n", ret);
1100 return ret;
1101 }
1102
1103
1104 static int IpForwardTableSorter(const void *a, const void *b)
1105 {
1106 int ret;
1107
1108 if (a && b) {
1109 PMIB_IPFORWARDROW rowA = (PMIB_IPFORWARDROW)a, rowB = (PMIB_IPFORWARDROW)b;
1110
1111 ret = rowA->dwForwardDest - rowB->dwForwardDest;
1112 if (ret == 0) {
1113 ret = rowA->dwForwardProto - rowB->dwForwardProto;
1114 if (ret == 0) {
1115 ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy;
1116 if (ret == 0)
1117 ret = rowA->dwForwardNextHop - rowB->dwForwardNextHop;
1118 }
1119 }
1120 }
1121 else
1122 ret = 0;
1123 return ret;
1124 }
1125
1126
1127 /******************************************************************
1128 * GetIpForwardTable (IPHLPAPI.@)
1129 *
1130 *
1131 * PARAMS
1132 *
1133 * pIpForwardTable [In/Out]
1134 * pdwSize [In/Out]
1135 * bOrder [In]
1136 *
1137 * RETURNS
1138 *
1139 * DWORD
1140 *
1141 */
1142 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1143 {
1144 DWORD ret;
1145
1146 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %ld\n", pIpForwardTable,
1147 pdwSize, (DWORD)bOrder);
1148 if (!pdwSize)
1149 ret = ERROR_INVALID_PARAMETER;
1150 else {
1151 DWORD numRoutes = getNumRoutes();
1152 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (numRoutes - 1) *
1153 sizeof(MIB_IPFORWARDROW);
1154
1155 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1156 *pdwSize = sizeNeeded;
1157 ret = ERROR_INSUFFICIENT_BUFFER;
1158 }
1159 else {
1160 RouteTable *table = getRouteTable();
1161 if (table) {
1162 sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (table->numRoutes - 1) *
1163 sizeof(MIB_IPFORWARDROW);
1164 if (*pdwSize < sizeNeeded) {
1165 *pdwSize = sizeNeeded;
1166 ret = ERROR_INSUFFICIENT_BUFFER;
1167 }
1168 else {
1169 DWORD ndx;
1170
1171 pIpForwardTable->dwNumEntries = table->numRoutes;
1172 for (ndx = 0; ndx < numRoutes; ndx++) {
1173 pIpForwardTable->table[ndx].dwForwardIfIndex =
1174 table->routes[ndx].ifIndex;
1175 pIpForwardTable->table[ndx].dwForwardDest =
1176 table->routes[ndx].dest;
1177 pIpForwardTable->table[ndx].dwForwardMask =
1178 table->routes[ndx].mask;
1179 pIpForwardTable->table[ndx].dwForwardPolicy = 0;
1180 pIpForwardTable->table[ndx].dwForwardNextHop =
1181 table->routes[ndx].gateway;
1182 /* FIXME: this type is appropriate for local interfaces; may not
1183 always be appropriate */
1184 pIpForwardTable->table[ndx].dwForwardType = MIB_IPROUTE_TYPE_DIRECT;
1185 /* FIXME: other protos might be appropriate, e.g. the default route
1186 is typically set with MIB_IPPROTO_NETMGMT instead */
1187 pIpForwardTable->table[ndx].dwForwardProto = MIB_IPPROTO_LOCAL;
1188 /* punt on age and AS */
1189 pIpForwardTable->table[ndx].dwForwardAge = 0;
1190 pIpForwardTable->table[ndx].dwForwardNextHopAS = 0;
1191 pIpForwardTable->table[ndx].dwForwardMetric1 =
1192 table->routes[ndx].metric;
1193 /* rest of the metrics are 0.. */
1194 pIpForwardTable->table[ndx].dwForwardMetric2 = 0;
1195 pIpForwardTable->table[ndx].dwForwardMetric3 = 0;
1196 pIpForwardTable->table[ndx].dwForwardMetric4 = 0;
1197 pIpForwardTable->table[ndx].dwForwardMetric5 = 0;
1198 }
1199 if (bOrder)
1200 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1201 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1202 ret = NO_ERROR;
1203 }
1204 free(table);
1205 }
1206 else
1207 ret = ERROR_OUTOFMEMORY;
1208 }
1209 }
1210 TRACE("returning %ld\n", ret);
1211 return ret;
1212 }
1213
1214
1215 static int IpNetTableSorter(const void *a, const void *b)
1216 {
1217 int ret;
1218
1219 if (a && b)
1220 ret = ((PMIB_IPNETROW)a)->dwAddr - ((PMIB_IPNETROW)b)->dwAddr;
1221 else
1222 ret = 0;
1223 return ret;
1224 }
1225
1226
1227 /******************************************************************
1228 * GetIpNetTable (IPHLPAPI.@)
1229 *
1230 *
1231 * PARAMS
1232 *
1233 * pIpNetTable [In/Out]
1234 * pdwSize [In/Out]
1235 * bOrder [In]
1236 *
1237 * RETURNS
1238 *
1239 * DWORD
1240 *
1241 */
1242 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1243 {
1244 DWORD ret;
1245
1246 TRACE("pIpNetTable %p, pdwSize %p, bOrder %ld\n", pIpNetTable, pdwSize,
1247 (DWORD)bOrder);
1248 if (!pdwSize)
1249 ret = ERROR_INVALID_PARAMETER;
1250 else {
1251 DWORD numEntries = getNumArpEntries();
1252 ULONG size = sizeof(MIB_IPNETTABLE) + (numEntries - 1) *
1253 sizeof(MIB_IPNETROW);
1254
1255 if (!pIpNetTable || *pdwSize < size) {
1256 *pdwSize = size;
1257 ret = ERROR_INSUFFICIENT_BUFFER;
1258 }
1259 else {
1260 PMIB_IPNETTABLE table = getArpTable();
1261
1262 if (table) {
1263 size = sizeof(MIB_IPNETTABLE) + (table->dwNumEntries - 1) *
1264 sizeof(MIB_IPNETROW);
1265 if (*pdwSize < size) {
1266 *pdwSize = size;
1267 ret = ERROR_INSUFFICIENT_BUFFER;
1268 }
1269 else {
1270 memcpy(pIpNetTable, table, size);
1271 if (bOrder)
1272 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1273 sizeof(MIB_IPNETROW), IpNetTableSorter);
1274 ret = NO_ERROR;
1275 }
1276 free(table);
1277 }
1278 else
1279 ret = ERROR_OUTOFMEMORY;
1280 }
1281 }
1282 TRACE("returning %ld\n", ret);
1283 return ret;
1284 }
1285
1286
1287 /******************************************************************
1288 * GetIpStatistics (IPHLPAPI.@)
1289 *
1290 *
1291 * PARAMS
1292 *
1293 * pStats [In/Out]
1294 *
1295 * RETURNS
1296 *
1297 * DWORD
1298 *
1299 */
1300 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1301 {
1302 return GetIpStatisticsEx(pStats, PF_INET);
1303 }
1304
1305 /******************************************************************
1306 * GetIpStatisticsEx (IPHLPAPI.@)
1307 *
1308 *
1309 * PARAMS
1310 *
1311 * pStats [In/Out]
1312 * dwFamily [In]
1313 *
1314 * RETURNS
1315 *
1316 * DWORD
1317 *
1318 */
1319 DWORD WINAPI GetIpStatisticsEx(PMIB_IPSTATS pStats, DWORD dwFamily)
1320 {
1321 DWORD ret;
1322
1323 TRACE("pStats %p\n", pStats);
1324 ret = getIPStats(pStats, dwFamily);
1325 TRACE("returning %ld\n", ret);
1326 return ret;
1327 }
1328
1329 /******************************************************************
1330 * GetNetworkParams (IPHLPAPI.@)
1331 *
1332 *
1333 * PARAMS
1334 *
1335 * pFixedInfo [In/Out]
1336 * pOutBufLen [In/Out]
1337 *
1338 * RETURNS
1339 *
1340 * DWORD
1341 *
1342 */
1343 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1344 {
1345 DWORD ret, size;
1346 LONG regReturn;
1347 HKEY hKey;
1348 PIPHLP_RES_INFO resInfo;
1349
1350 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1351 if (!pOutBufLen)
1352 return ERROR_INVALID_PARAMETER;
1353
1354 resInfo = getResInfo();
1355 if (!resInfo)
1356 return ERROR_OUTOFMEMORY;
1357
1358 TRACE("resInfo->riCount = %d\n", resInfo->riCount);
1359
1360 size = sizeof(FIXED_INFO) + (resInfo->riCount > 0 ? (resInfo->riCount - 1) *
1361 sizeof(IP_ADDR_STRING) : 0);
1362 if (!pFixedInfo || *pOutBufLen < size) {
1363 *pOutBufLen = size;
1364 disposeResInfo( resInfo );
1365 return ERROR_BUFFER_OVERFLOW;
1366 }
1367
1368 memset(pFixedInfo, 0, size);
1369 size = sizeof(pFixedInfo->HostName);
1370 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1371 size = sizeof(pFixedInfo->DomainName);
1372 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1373
1374 TRACE("GetComputerNameExA: %s\n", pFixedInfo->DomainName);
1375
1376 if (resInfo->riCount > 0) {
1377 PIP_ADDR_STRING ptr;
1378 int i;
1379
1380 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < resInfo->riCount && ptr;
1381 i++, ptr = ptr->Next) {
1382 toIPAddressString
1383 (((PSOCKADDR_IN)&resInfo->riAddressList[i])->sin_addr.s_addr,
1384 ptr->IpAddress.String);
1385 if (i == resInfo->riCount - 1)
1386 ptr->Next = NULL;
1387 else if (i == 0)
1388 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1389 else
1390 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1391 }
1392 }
1393 pFixedInfo->NodeType = HYBRID_NODETYPE;
1394 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1395 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1396 if (regReturn != ERROR_SUCCESS)
1397 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1398 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1399 &hKey);
1400 if (regReturn == ERROR_SUCCESS)
1401 {
1402 DWORD size = sizeof(pFixedInfo->ScopeId);
1403
1404 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, pFixedInfo->ScopeId, &size);
1405 RegCloseKey(hKey);
1406 }
1407
1408 disposeResInfo( resInfo );
1409 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1410 I suppose could also check for a listener on port 53 to set EnableDns */
1411 ret = NO_ERROR;
1412 TRACE("returning %ld\n", ret);
1413
1414 return ret;
1415 }
1416
1417
1418 /******************************************************************
1419 * GetNumberOfInterfaces (IPHLPAPI.@)
1420 *
1421 *
1422 * PARAMS
1423 *
1424 * pdwNumIf [In/Out]
1425 *
1426 * RETURNS
1427 *
1428 * DWORD
1429 *
1430 */
1431 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1432 {
1433 DWORD ret;
1434
1435 TRACE("pdwNumIf %p\n", pdwNumIf);
1436 if (!pdwNumIf)
1437 ret = ERROR_INVALID_PARAMETER;
1438 else {
1439 *pdwNumIf = getNumInterfaces();
1440 ret = NO_ERROR;
1441 }
1442 TRACE("returning %ld\n", ret);
1443 return ret;
1444 }
1445
1446
1447 /******************************************************************
1448 * GetPerAdapterInfo (IPHLPAPI.@)
1449 *
1450 *
1451 * PARAMS
1452 *
1453 * IfIndex [In]
1454 * pPerAdapterInfo [In/Out]
1455 * pOutBufLen [In/Out]
1456 *
1457 * RETURNS
1458 *
1459 * DWORD
1460 *
1461 */
1462 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1463 {
1464 TRACE("IfIndex %ld, pPerAdapterInfo %p, pOutBufLen %p\n", IfIndex,
1465 pPerAdapterInfo, pOutBufLen);
1466 FIXME(":stub\n");
1467 /* marking Win2K+ functions not supported */
1468 return ERROR_NOT_SUPPORTED;
1469 }
1470
1471
1472 /******************************************************************
1473 * GetRTTAndHopCount (IPHLPAPI.@)
1474 *
1475 *
1476 * PARAMS
1477 *
1478 * DestIpAddress [In]
1479 * HopCount [In/Out]
1480 * MaxHops [In]
1481 * RTT [In/Out]
1482 *
1483 * RETURNS
1484 *
1485 * BOOL
1486 *
1487 */
1488 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1489 {
1490 TRACE("DestIpAddress 0x%08lx, HopCount %p, MaxHops %ld, RTT %p\n",
1491 DestIpAddress, HopCount, MaxHops, RTT);
1492 FIXME(":stub\n");
1493 return (BOOL) 0;
1494 }
1495
1496
1497 /******************************************************************
1498 * GetTcpStatisticsEx (IPHLPAPI.@)
1499 *
1500 *
1501 * PARAMS
1502 *
1503 * pStats [In/Out]
1504 * dwFamily [In]
1505 *
1506 * RETURNS
1507 *
1508 * DWORD
1509 *
1510 */
1511 DWORD WINAPI GetTcpStatisticsEx(PMIB_TCPSTATS pStats, DWORD dwFamily)
1512 {
1513 DWORD ret;
1514
1515 TRACE("pStats %p\n", pStats);
1516 ret = getTCPStats(pStats, dwFamily);
1517 TRACE("returning %ld\n", ret);
1518 return ret;
1519 }
1520
1521 /******************************************************************
1522 * GetTcpStatistics (IPHLPAPI.@)
1523 *
1524 *
1525 * PARAMS
1526 *
1527 * pStats [In/Out]
1528 *
1529 * RETURNS
1530 *
1531 * DWORD
1532 *
1533 */
1534 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1535 {
1536 return GetTcpStatisticsEx(pStats, PF_INET);
1537 }
1538
1539
1540 static int TcpTableSorter(const void *a, const void *b)
1541 {
1542 int ret;
1543
1544 if (a && b) {
1545 PMIB_TCPROW rowA = (PMIB_TCPROW)a, rowB = (PMIB_TCPROW)b;
1546
1547 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
1548 if (ret == 0) {
1549 ret = rowA->dwLocalPort - rowB->dwLocalPort;
1550 if (ret == 0) {
1551 ret = rowA->dwRemoteAddr - rowB->dwRemoteAddr;
1552 if (ret == 0)
1553 ret = rowA->dwRemotePort - rowB->dwRemotePort;
1554 }
1555 }
1556 }
1557 else
1558 ret = 0;
1559 return ret;
1560 }
1561
1562
1563 /******************************************************************
1564 * GetTcpTable (IPHLPAPI.@)
1565 *
1566 *
1567 * PARAMS
1568 *
1569 * pTcpTable [In/Out]
1570 * pdwSize [In/Out]
1571 * bOrder [In]
1572 *
1573 * RETURNS
1574 *
1575 * DWORD
1576 *
1577 */
1578 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1579 {
1580 DWORD ret;
1581
1582 TRACE("pTcpTable %p, pdwSize %p, bOrder %ld\n", pTcpTable, pdwSize,
1583 (DWORD)bOrder);
1584 if (!pdwSize)
1585 ret = ERROR_INVALID_PARAMETER;
1586 else {
1587 DWORD numEntries = getNumTcpEntries();
1588 ULONG size = sizeof(MIB_TCPTABLE) + (numEntries - 1) * sizeof(MIB_TCPROW);
1589
1590 if (!pTcpTable || *pdwSize < size) {
1591 *pdwSize = size;
1592 ret = ERROR_INSUFFICIENT_BUFFER;
1593 }
1594 else {
1595 PMIB_TCPTABLE table = getTcpTable();
1596
1597 if (table) {
1598 size = sizeof(MIB_TCPTABLE) + (table->dwNumEntries - 1) *
1599 sizeof(MIB_TCPROW);
1600 if (*pdwSize < size) {
1601 *pdwSize = size;
1602 ret = ERROR_INSUFFICIENT_BUFFER;
1603 }
1604 else {
1605 memcpy(pTcpTable, table, size);
1606 if (bOrder)
1607 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1608 sizeof(MIB_TCPROW), TcpTableSorter);
1609 ret = NO_ERROR;
1610 }
1611 free(table);
1612 }
1613 else
1614 ret = ERROR_OUTOFMEMORY;
1615 }
1616 }
1617 TRACE("returning %ld\n", ret);
1618 return ret;
1619 }
1620
1621
1622 /******************************************************************
1623 * GetUdpStatisticsEx (IPHLPAPI.@)
1624 *
1625 *
1626 * PARAMS
1627 *
1628 * pStats [In/Out]
1629 * dwFamily [In]
1630 *
1631 * RETURNS
1632 *
1633 * DWORD
1634 *
1635 */
1636 DWORD WINAPI GetUdpStatisticsEx(PMIB_UDPSTATS pStats, DWORD dwFamily)
1637 {
1638 DWORD ret;
1639
1640 TRACE("pStats %p\n", pStats);
1641 ret = getUDPStats(pStats, dwFamily);
1642 TRACE("returning %ld\n", ret);
1643 return ret;
1644 }
1645
1646 /******************************************************************
1647 * GetUdpStatistics (IPHLPAPI.@)
1648 *
1649 *
1650 * PARAMS
1651 *
1652 * pStats [In/Out]
1653 *
1654 * RETURNS
1655 *
1656 * DWORD
1657 *
1658 */
1659 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1660 {
1661 return GetUdpStatisticsEx(pStats, PF_INET);
1662 }
1663
1664
1665 static int UdpTableSorter(const void *a, const void *b)
1666 {
1667 int ret;
1668
1669 if (a && b) {
1670 PMIB_UDPROW rowA = (PMIB_UDPROW)a, rowB = (PMIB_UDPROW)b;
1671
1672 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
1673 if (ret == 0)
1674 ret = rowA->dwLocalPort - rowB->dwLocalPort;
1675 }
1676 else
1677 ret = 0;
1678 return ret;
1679 }
1680
1681
1682 /******************************************************************
1683 * GetUdpTable (IPHLPAPI.@)
1684 *
1685 *
1686 * PARAMS
1687 *
1688 * pUdpTable [In/Out]
1689 * pdwSize [In/Out]
1690 * bOrder [In]
1691 *
1692 * RETURNS
1693 *
1694 * DWORD
1695 *
1696 */
1697 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1698 {
1699 DWORD ret;
1700
1701 TRACE("pUdpTable %p, pdwSize %p, bOrder %ld\n", pUdpTable, pdwSize,
1702 (DWORD)bOrder);
1703 if (!pdwSize)
1704 ret = ERROR_INVALID_PARAMETER;
1705 else {
1706 DWORD numEntries = getNumUdpEntries();
1707 ULONG size = sizeof(MIB_UDPTABLE) + (numEntries - 1) * sizeof(MIB_UDPROW);
1708
1709 if (!pUdpTable || *pdwSize < size) {
1710 *pdwSize = size;
1711 ret = ERROR_INSUFFICIENT_BUFFER;
1712 }
1713 else {
1714 PMIB_UDPTABLE table = getUdpTable();
1715
1716 if (table) {
1717 size = sizeof(MIB_UDPTABLE) + (table->dwNumEntries - 1) *
1718 sizeof(MIB_UDPROW);
1719 if (*pdwSize < size) {
1720 *pdwSize = size;
1721 ret = ERROR_INSUFFICIENT_BUFFER;
1722 }
1723 else {
1724 memcpy(pUdpTable, table, size);
1725 if (bOrder)
1726 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1727 sizeof(MIB_UDPROW), UdpTableSorter);
1728 ret = NO_ERROR;
1729 }
1730 free(table);
1731 }
1732 else
1733 ret = ERROR_OUTOFMEMORY;
1734 }
1735 }
1736 TRACE("returning %ld\n", ret);
1737 return ret;
1738 }
1739
1740
1741 /******************************************************************
1742 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1743 *
1744 *
1745 * PARAMS
1746 *
1747 * pIPIfInfo [In/Out]
1748 * dwOutBufLen [In/Out]
1749 *
1750 * RETURNS
1751 *
1752 * DWORD
1753 *
1754 */
1755 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1756 {
1757 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1758 /* a unidirectional adapter?? not bloody likely! */
1759 return ERROR_NOT_SUPPORTED;
1760 }
1761
1762
1763 /******************************************************************
1764 * IpReleaseAddress (IPHLPAPI.@)
1765 *
1766 *
1767 * PARAMS
1768 *
1769 * AdapterInfo [In/Out]
1770 *
1771 * RETURNS
1772 *
1773 * DWORD
1774 *
1775 */
1776 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1777 {
1778 TRACE("AdapterInfo %p\n", AdapterInfo);
1779 /* not a stub, never going to support this (and I never mark an adapter as
1780 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1781 return ERROR_NOT_SUPPORTED;
1782 }
1783
1784
1785 /******************************************************************
1786 * IpRenewAddress (IPHLPAPI.@)
1787 *
1788 *
1789 * PARAMS
1790 *
1791 * AdapterInfo [In/Out]
1792 *
1793 * RETURNS
1794 *
1795 * DWORD
1796 *
1797 */
1798 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1799 {
1800 TRACE("AdapterInfo %p\n", AdapterInfo);
1801 /* not a stub, never going to support this (and I never mark an adapter as
1802 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1803 return ERROR_NOT_SUPPORTED;
1804 }
1805
1806
1807 /******************************************************************
1808 * NotifyAddrChange (IPHLPAPI.@)
1809 *
1810 *
1811 * PARAMS
1812 *
1813 * Handle [In/Out]
1814 * overlapped [In/Out]
1815 *
1816 * RETURNS
1817 *
1818 * DWORD
1819 *
1820 */
1821 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1822 {
1823 TRACE("Handle %p, overlapped %p\n", Handle, overlapped);
1824 FIXME(":stub\n");
1825 /* marking Win2K+ functions not supported */
1826 return ERROR_NOT_SUPPORTED;
1827 }
1828
1829
1830 /******************************************************************
1831 * NotifyRouteChange (IPHLPAPI.@)
1832 *
1833 *
1834 * PARAMS
1835 *
1836 * Handle [In/Out]
1837 * overlapped [In/Out]
1838 *
1839 * RETURNS
1840 *
1841 * DWORD
1842 *
1843 */
1844 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1845 {
1846 TRACE("Handle %p, overlapped %p\n", Handle, overlapped);
1847 FIXME(":stub\n");
1848 /* marking Win2K+ functions not supported */
1849 return ERROR_NOT_SUPPORTED;
1850 }
1851
1852
1853 /******************************************************************
1854 * SendARP (IPHLPAPI.@)
1855 *
1856 *
1857 * PARAMS
1858 *
1859 * DestIP [In]
1860 * SrcIP [In]
1861 * pMacAddr [In/Out]
1862 * PhyAddrLen [In/Out]
1863 *
1864 * RETURNS
1865 *
1866 * DWORD
1867 *
1868 */
1869 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1870 {
1871 TRACE("DestIP 0x%08lx, SrcIP 0x%08lx, pMacAddr %p, PhyAddrLen %p\n", DestIP,
1872 SrcIP, pMacAddr, PhyAddrLen);
1873 FIXME(":stub\n");
1874 /* marking Win2K+ functions not supported */
1875 return ERROR_NOT_SUPPORTED;
1876 }
1877
1878
1879 /******************************************************************
1880 * SetIfEntry (IPHLPAPI.@)
1881 *
1882 *
1883 * PARAMS
1884 *
1885 * pIfRow [In/Out]
1886 *
1887 * RETURNS
1888 *
1889 * DWORD
1890 *
1891 */
1892 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1893 {
1894 TRACE("pIfRow %p\n", pIfRow);
1895 /* this is supposed to set an administratively interface up or down.
1896 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1897 this sort of down is indistinguishable from other sorts of down (e.g. no
1898 link). */
1899 FIXME(":stub\n");
1900 return ERROR_NOT_SUPPORTED;
1901 }
1902
1903
1904 /******************************************************************
1905 * SetIpForwardEntry (IPHLPAPI.@)
1906 *
1907 *
1908 * PARAMS
1909 *
1910 * pRoute [In/Out]
1911 *
1912 * RETURNS
1913 *
1914 * DWORD
1915 *
1916 */
1917 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1918 {
1919 TRACE("pRoute %p\n", pRoute);
1920 /* this is to add a route entry, how's it distinguishable from
1921 CreateIpForwardEntry?
1922 could use SIOCADDRT, not sure I want to */
1923 FIXME(":stub\n");
1924 return (DWORD) 0;
1925 }
1926
1927
1928 /******************************************************************
1929 * SetIpNetEntry (IPHLPAPI.@)
1930 *
1931 *
1932 * PARAMS
1933 *
1934 * pArpEntry [In/Out]
1935 *
1936 * RETURNS
1937 *
1938 * DWORD
1939 *
1940 */
1941 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1942 {
1943 TRACE("pArpEntry %p\n", pArpEntry);
1944 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1945 FIXME(":stub\n");
1946 return (DWORD) 0;
1947 }
1948
1949
1950 /******************************************************************
1951 * SetIpStatistics (IPHLPAPI.@)
1952 *
1953 *
1954 * PARAMS
1955 *
1956 * pIpStats [In/Out]
1957 *
1958 * RETURNS
1959 *
1960 * DWORD
1961 *
1962 */
1963 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1964 {
1965 TRACE("pIpStats %p\n", pIpStats);
1966 FIXME(":stub\n");
1967 return (DWORD) 0;
1968 }
1969
1970
1971 /******************************************************************
1972 * SetIpTTL (IPHLPAPI.@)
1973 *
1974 *
1975 * PARAMS
1976 *
1977 * nTTL [In]
1978 *
1979 * RETURNS
1980 *
1981 * DWORD
1982 *
1983 */
1984 DWORD WINAPI SetIpTTL(UINT nTTL)
1985 {
1986 TRACE("nTTL %d\n", nTTL);
1987 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1988 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1989 FIXME(":stub\n");
1990 return (DWORD) 0;
1991 }
1992
1993
1994 /******************************************************************
1995 * SetTcpEntry (IPHLPAPI.@)
1996 *
1997 *
1998 * PARAMS
1999 *
2000 * pTcpRow [In/Out]
2001 *
2002 * RETURNS
2003 *
2004 * DWORD
2005 *
2006 */
2007 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2008 {
2009 TRACE("pTcpRow %p\n", pTcpRow);
2010 FIXME(":stub\n");
2011 return (DWORD) 0;
2012 }
2013
2014
2015 /******************************************************************
2016 * UnenableRouter (IPHLPAPI.@)
2017 *
2018 *
2019 * PARAMS
2020 *
2021 * pOverlapped [In/Out]
2022 * lpdwEnableCount [In/Out]
2023 *
2024 * RETURNS
2025 *
2026 * DWORD
2027 *
2028 */
2029 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2030 {
2031 TRACE("pOverlapped %p, lpdwEnableCount %p\n", pOverlapped, lpdwEnableCount);
2032 FIXME(":stub\n");
2033 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2034 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2035 marking Win2K+ functions not supported */
2036 return ERROR_NOT_SUPPORTED;
2037 }
2038
2039 /*
2040 * @unimplemented
2041 */
2042 DWORD STDCALL GetIpErrorString(IP_STATUS ErrorCode,PWCHAR Buffer,PDWORD Size)
2043 {
2044 UNIMPLEMENTED
2045 return 0L;
2046 }
2047
2048
2049 /*
2050 * @unimplemented
2051 */
2052 PIP_ADAPTER_ORDER_MAP STDCALL GetAdapterOrderMap(VOID)
2053 {
2054 UNIMPLEMENTED
2055 return 0L;
2056 }
2057
2058 /*
2059 * @unimplemented
2060 */
2061 DWORD STDCALL GetAdaptersAddresses(ULONG Family,DWORD Flags,PVOID Reserved,PIP_ADAPTER_ADDRESSES pAdapterAddresses,PULONG pOutBufLen)
2062 {
2063 UNIMPLEMENTED
2064 return 0L;
2065 }
2066
2067 /*
2068 * @unimplemented
2069 */
2070 BOOL STDCALL CancelIPChangeNotify(LPOVERLAPPED notifyOverlapped)
2071 {
2072 UNIMPLEMENTED
2073 return 0L;
2074 }
2075
2076 /*
2077 * @unimplemented
2078 */
2079 DWORD STDCALL GetBestInterfaceEx(struct sockaddr *pDestAddr,PDWORD pdwBestIfIndex)
2080 {
2081 UNIMPLEMENTED
2082 return 0L;
2083 }
2084
2085 /*
2086 * @unimplemented
2087 */
2088 DWORD STDCALL NhpAllocateAndGetInterfaceInfoFromStack(IP_INTERFACE_NAME_INFO **ppTable,PDWORD pdwCount,BOOL bOrder,HANDLE hHeap,DWORD dwFlags)
2089 {
2090 UNIMPLEMENTED
2091 return 0L;
2092 }
2093
2094 /*
2095 * @unimplemented
2096 */
2097 DWORD STDCALL GetIcmpStatisticsEx(PMIB_ICMP_EX pStats,DWORD dwFamily)
2098 {
2099 UNIMPLEMENTED
2100 return 0L;
2101 }
2102