push b5232b2081a0e20e4bf07d6ded424d0101e4a589
[wine/hacks.git] / dlls / iphlpapi / iphlpapi_main.c
blob2aac18de6b748dbee65aa75752dd071cb2f0c5df
1 /*
2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 Juan Lang
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef HAVE_NETINET_IN_H
27 # include <netinet/in.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
34 #endif
35 #ifdef HAVE_RESOLV_H
36 # include <resolv.h>
37 #endif
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winreg.h"
42 #include "iphlpapi.h"
43 #include "ifenum.h"
44 #include "ipstats.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
49 #ifndef INADDR_NONE
50 #define INADDR_NONE ~0UL
51 #endif
53 static int resolver_initialised;
55 /* call res_init() just once because of a bug in Mac OS X 10.4 */
56 static void initialise_resolver(void)
58 if (!resolver_initialised)
60 res_init();
61 resolver_initialised = 1;
65 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
67 switch (fdwReason) {
68 case DLL_PROCESS_ATTACH:
69 DisableThreadLibraryCalls( hinstDLL );
70 break;
72 case DLL_PROCESS_DETACH:
73 break;
75 return TRUE;
78 /******************************************************************
79 * AddIPAddress (IPHLPAPI.@)
81 * Add an IP address to an adapter.
83 * PARAMS
84 * Address [In] IP address to add to the adapter
85 * IpMask [In] subnet mask for the IP address
86 * IfIndex [In] adapter index to add the address
87 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
88 * NTEInstance [Out] NTE instance for the IP address
90 * RETURNS
91 * Success: NO_ERROR
92 * Failure: error code from winerror.h
94 * FIXME
95 * Stub. Currently returns ERROR_NOT_SUPPORTED.
97 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
99 FIXME(":stub\n");
100 return ERROR_NOT_SUPPORTED;
104 /******************************************************************
105 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
107 * Get table of local interfaces.
108 * Like GetIfTable(), but allocate the returned table from heap.
110 * PARAMS
111 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
112 * allocated and returned.
113 * bOrder [In] whether to sort the table
114 * heap [In] heap from which the table is allocated
115 * flags [In] flags to HeapAlloc
117 * RETURNS
118 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
119 * GetIfTable() returns otherwise.
121 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
122 BOOL bOrder, HANDLE heap, DWORD flags)
124 DWORD ret;
126 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
127 bOrder, heap, flags);
128 if (!ppIfTable)
129 ret = ERROR_INVALID_PARAMETER;
130 else {
131 DWORD dwSize = 0;
133 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
134 if (ret == ERROR_INSUFFICIENT_BUFFER) {
135 *ppIfTable = HeapAlloc(heap, flags, dwSize);
136 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
139 TRACE("returning %d\n", ret);
140 return ret;
144 static int IpAddrTableSorter(const void *a, const void *b)
146 int ret;
148 if (a && b)
149 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
150 else
151 ret = 0;
152 return ret;
156 /******************************************************************
157 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
159 * Get interface-to-IP address mapping table.
160 * Like GetIpAddrTable(), but allocate the returned table from heap.
162 * PARAMS
163 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
164 * allocated and returned.
165 * bOrder [In] whether to sort the table
166 * heap [In] heap from which the table is allocated
167 * flags [In] flags to HeapAlloc
169 * RETURNS
170 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
171 * failure, NO_ERROR on success.
173 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
174 BOOL bOrder, HANDLE heap, DWORD flags)
176 DWORD ret;
178 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
179 ppIpAddrTable, bOrder, heap, flags);
180 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
181 if (!ret && bOrder)
182 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
183 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
184 TRACE("returning %d\n", ret);
185 return ret;
189 static int IpForwardTableSorter(const void *a, const void *b)
191 int ret;
193 if (a && b) {
194 const MIB_IPFORWARDROW* rowA = (const MIB_IPFORWARDROW*)a;
195 const MIB_IPFORWARDROW* rowB = (const MIB_IPFORWARDROW*)b;
197 ret = rowA->dwForwardDest - rowB->dwForwardDest;
198 if (ret == 0) {
199 ret = rowA->dwForwardProto - rowB->dwForwardProto;
200 if (ret == 0) {
201 ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy;
202 if (ret == 0)
203 ret = rowA->dwForwardNextHop - rowB->dwForwardNextHop;
207 else
208 ret = 0;
209 return ret;
213 /******************************************************************
214 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
216 * Get the route table.
217 * Like GetIpForwardTable(), but allocate the returned table from heap.
219 * PARAMS
220 * ppIpForwardTable [Out] pointer into which the MIB_IPFORWARDTABLE is
221 * allocated and returned.
222 * bOrder [In] whether to sort the table
223 * heap [In] heap from which the table is allocated
224 * flags [In] flags to HeapAlloc
226 * RETURNS
227 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, other error codes
228 * on failure, NO_ERROR on success.
230 DWORD WINAPI AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE *
231 ppIpForwardTable, BOOL bOrder, HANDLE heap, DWORD flags)
233 DWORD ret;
235 TRACE("ppIpForwardTable %p, bOrder %d, heap %p, flags 0x%08x\n",
236 ppIpForwardTable, bOrder, heap, flags);
237 ret = getRouteTable(ppIpForwardTable, heap, flags);
238 if (!ret && bOrder)
239 qsort((*ppIpForwardTable)->table, (*ppIpForwardTable)->dwNumEntries,
240 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
241 TRACE("returning %d\n", ret);
242 return ret;
246 static int IpNetTableSorter(const void *a, const void *b)
248 int ret;
250 if (a && b)
251 ret = ((const MIB_IPNETROW*)a)->dwAddr - ((const MIB_IPNETROW*)b)->dwAddr;
252 else
253 ret = 0;
254 return ret;
258 /******************************************************************
259 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
261 * Get the IP-to-physical address mapping table.
262 * Like GetIpNetTable(), but allocate the returned table from heap.
264 * PARAMS
265 * ppIpNetTable [Out] pointer into which the MIB_IPNETTABLE is
266 * allocated and returned.
267 * bOrder [In] whether to sort the table
268 * heap [In] heap from which the table is allocated
269 * flags [In] flags to HeapAlloc
271 * RETURNS
272 * ERROR_INVALID_PARAMETER if ppIpNetTable is NULL, other error codes
273 * on failure, NO_ERROR on success.
275 DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable,
276 BOOL bOrder, HANDLE heap, DWORD flags)
278 DWORD ret;
280 TRACE("ppIpNetTable %p, bOrder %d, heap %p, flags 0x%08x\n",
281 ppIpNetTable, bOrder, heap, flags);
282 ret = getArpTable(ppIpNetTable, heap, flags);
283 if (!ret && bOrder)
284 qsort((*ppIpNetTable)->table, (*ppIpNetTable)->dwNumEntries,
285 sizeof(MIB_IPADDRROW), IpNetTableSorter);
286 TRACE("returning %d\n", ret);
287 return ret;
291 static int TcpTableSorter(const void *a, const void *b)
293 int ret;
295 if (a && b) {
296 const MIB_TCPROW* rowA = a;
297 const MIB_TCPROW* rowB = b;
299 ret = ntohl (rowA->dwLocalAddr) - ntohl (rowB->dwLocalAddr);
300 if (ret == 0) {
301 ret = ntohs ((unsigned short)rowA->dwLocalPort) -
302 ntohs ((unsigned short)rowB->dwLocalPort);
303 if (ret == 0) {
304 ret = ntohl (rowA->dwRemoteAddr) - ntohl (rowB->dwRemoteAddr);
305 if (ret == 0)
306 ret = ntohs ((unsigned short)rowA->dwRemotePort) -
307 ntohs ((unsigned short)rowB->dwRemotePort);
311 else
312 ret = 0;
313 return ret;
317 /******************************************************************
318 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
320 * Get the TCP connection table.
321 * Like GetTcpTable(), but allocate the returned table from heap.
323 * PARAMS
324 * ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
325 * allocated and returned.
326 * bOrder [In] whether to sort the table
327 * heap [In] heap from which the table is allocated
328 * flags [In] flags to HeapAlloc
330 * RETURNS
331 * ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
332 * returns otherwise.
334 DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable,
335 BOOL bOrder, HANDLE heap, DWORD flags)
337 DWORD ret;
339 TRACE("ppTcpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
340 ppTcpTable, bOrder, heap, flags);
342 *ppTcpTable = NULL;
343 ret = getTcpTable(ppTcpTable, 0, heap, flags);
344 if (!ret && bOrder)
345 qsort((*ppTcpTable)->table, (*ppTcpTable)->dwNumEntries,
346 sizeof(MIB_TCPROW), TcpTableSorter);
347 TRACE("returning %d\n", ret);
348 return ret;
352 static int UdpTableSorter(const void *a, const void *b)
354 int ret;
356 if (a && b) {
357 const MIB_UDPROW* rowA = (const MIB_UDPROW*)a;
358 const MIB_UDPROW* rowB = (const MIB_UDPROW*)b;
360 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
361 if (ret == 0)
362 ret = rowA->dwLocalPort - rowB->dwLocalPort;
364 else
365 ret = 0;
366 return ret;
370 /******************************************************************
371 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
373 * Get the UDP listener table.
374 * Like GetUdpTable(), but allocate the returned table from heap.
376 * PARAMS
377 * ppUdpTable [Out] pointer into which the MIB_UDPTABLE is
378 * allocated and returned.
379 * bOrder [In] whether to sort the table
380 * heap [In] heap from which the table is allocated
381 * flags [In] flags to HeapAlloc
383 * RETURNS
384 * ERROR_INVALID_PARAMETER if ppUdpTable is NULL, whatever GetUdpTable()
385 * returns otherwise.
387 DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable,
388 BOOL bOrder, HANDLE heap, DWORD flags)
390 DWORD ret;
392 TRACE("ppUdpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
393 ppUdpTable, bOrder, heap, flags);
394 ret = getUdpTable(ppUdpTable, heap, flags);
395 if (!ret && bOrder)
396 qsort((*ppUdpTable)->table, (*ppUdpTable)->dwNumEntries,
397 sizeof(MIB_UDPROW), UdpTableSorter);
398 TRACE("returning %d\n", ret);
399 return ret;
403 /******************************************************************
404 * CreateIpForwardEntry (IPHLPAPI.@)
406 * Create a route in the local computer's IP table.
408 * PARAMS
409 * pRoute [In] new route information
411 * RETURNS
412 * Success: NO_ERROR
413 * Failure: error code from winerror.h
415 * FIXME
416 * Stub, always returns NO_ERROR.
418 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
420 FIXME("(pRoute %p): stub\n", pRoute);
421 /* could use SIOCADDRT, not sure I want to */
422 return (DWORD) 0;
426 /******************************************************************
427 * CreateIpNetEntry (IPHLPAPI.@)
429 * Create entry in the ARP table.
431 * PARAMS
432 * pArpEntry [In] new ARP entry
434 * RETURNS
435 * Success: NO_ERROR
436 * Failure: error code from winerror.h
438 * FIXME
439 * Stub, always returns NO_ERROR.
441 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
443 FIXME("(pArpEntry %p)\n", pArpEntry);
444 /* could use SIOCSARP on systems that support it, not sure I want to */
445 return (DWORD) 0;
449 /******************************************************************
450 * CreateProxyArpEntry (IPHLPAPI.@)
452 * Create a Proxy ARP (PARP) entry for an IP address.
454 * PARAMS
455 * dwAddress [In] IP address for which this computer acts as a proxy.
456 * dwMask [In] subnet mask for dwAddress
457 * dwIfIndex [In] interface index
459 * RETURNS
460 * Success: NO_ERROR
461 * Failure: error code from winerror.h
463 * FIXME
464 * Stub, returns ERROR_NOT_SUPPORTED.
466 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
468 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
469 dwAddress, dwMask, dwIfIndex);
470 return ERROR_NOT_SUPPORTED;
474 /******************************************************************
475 * DeleteIPAddress (IPHLPAPI.@)
477 * Delete an IP address added with AddIPAddress().
479 * PARAMS
480 * NTEContext [In] NTE context from AddIPAddress();
482 * RETURNS
483 * Success: NO_ERROR
484 * Failure: error code from winerror.h
486 * FIXME
487 * Stub, returns ERROR_NOT_SUPPORTED.
489 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
491 FIXME("(NTEContext %d): stub\n", NTEContext);
492 return ERROR_NOT_SUPPORTED;
496 /******************************************************************
497 * DeleteIpForwardEntry (IPHLPAPI.@)
499 * Delete a route.
501 * PARAMS
502 * pRoute [In] route to delete
504 * RETURNS
505 * Success: NO_ERROR
506 * Failure: error code from winerror.h
508 * FIXME
509 * Stub, returns NO_ERROR.
511 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
513 FIXME("(pRoute %p): stub\n", pRoute);
514 /* could use SIOCDELRT, not sure I want to */
515 return (DWORD) 0;
519 /******************************************************************
520 * DeleteIpNetEntry (IPHLPAPI.@)
522 * Delete an ARP entry.
524 * PARAMS
525 * pArpEntry [In] ARP entry to delete
527 * RETURNS
528 * Success: NO_ERROR
529 * Failure: error code from winerror.h
531 * FIXME
532 * Stub, returns NO_ERROR.
534 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
536 FIXME("(pArpEntry %p): stub\n", pArpEntry);
537 /* could use SIOCDARP on systems that support it, not sure I want to */
538 return (DWORD) 0;
542 /******************************************************************
543 * DeleteProxyArpEntry (IPHLPAPI.@)
545 * Delete a Proxy ARP entry.
547 * PARAMS
548 * dwAddress [In] IP address for which this computer acts as a proxy.
549 * dwMask [In] subnet mask for dwAddress
550 * dwIfIndex [In] interface index
552 * RETURNS
553 * Success: NO_ERROR
554 * Failure: error code from winerror.h
556 * FIXME
557 * Stub, returns ERROR_NOT_SUPPORTED.
559 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
561 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
562 dwAddress, dwMask, dwIfIndex);
563 return ERROR_NOT_SUPPORTED;
567 /******************************************************************
568 * EnableRouter (IPHLPAPI.@)
570 * Turn on ip forwarding.
572 * PARAMS
573 * pHandle [In/Out]
574 * pOverlapped [In/Out] hEvent member should contain a valid handle.
576 * RETURNS
577 * Success: ERROR_IO_PENDING
578 * Failure: error code from winerror.h
580 * FIXME
581 * Stub, returns ERROR_NOT_SUPPORTED.
583 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
585 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
586 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
587 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
589 return ERROR_NOT_SUPPORTED;
593 /******************************************************************
594 * FlushIpNetTable (IPHLPAPI.@)
596 * Delete all ARP entries of an interface
598 * PARAMS
599 * dwIfIndex [In] interface index
601 * RETURNS
602 * Success: NO_ERROR
603 * Failure: error code from winerror.h
605 * FIXME
606 * Stub, returns ERROR_NOT_SUPPORTED.
608 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
610 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
611 /* this flushes the arp cache of the given index */
612 return ERROR_NOT_SUPPORTED;
616 /******************************************************************
617 * GetAdapterIndex (IPHLPAPI.@)
619 * Get interface index from its name.
621 * PARAMS
622 * AdapterName [In] unicode string with the adapter name
623 * IfIndex [Out] returns found interface index
625 * RETURNS
626 * Success: NO_ERROR
627 * Failure: error code from winerror.h
629 * FIXME
630 * Stub, returns ERROR_NOT_SUPPORTED.
632 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
634 FIXME("(AdapterName %p, IfIndex %p): stub\n", AdapterName, IfIndex);
635 /* FIXME: implement using getInterfaceIndexByName */
636 return ERROR_NOT_SUPPORTED;
640 /******************************************************************
641 * GetAdaptersInfo (IPHLPAPI.@)
643 * Get information about adapters.
645 * PARAMS
646 * pAdapterInfo [Out] buffer for adapter infos
647 * pOutBufLen [In] length of output buffer
649 * RETURNS
650 * Success: NO_ERROR
651 * Failure: error code from winerror.h
653 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
655 DWORD ret;
657 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
658 if (!pOutBufLen)
659 ret = ERROR_INVALID_PARAMETER;
660 else {
661 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
663 if (numNonLoopbackInterfaces > 0) {
664 DWORD numIPAddresses = getNumIPAddresses();
665 ULONG size;
667 /* This may slightly overestimate the amount of space needed, because
668 * the IP addresses include the loopback address, but it's easier
669 * to make sure there's more than enough space than to make sure there's
670 * precisely enough space.
672 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
673 size += numIPAddresses * sizeof(IP_ADDR_STRING);
674 if (!pAdapterInfo || *pOutBufLen < size) {
675 *pOutBufLen = size;
676 ret = ERROR_BUFFER_OVERFLOW;
678 else {
679 InterfaceIndexTable *table = NULL;
680 PMIB_IPADDRTABLE ipAddrTable = NULL;
682 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
683 if (!ret)
684 table = getNonLoopbackInterfaceIndexTable();
685 if (table) {
686 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
687 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
688 if (*pOutBufLen < size) {
689 *pOutBufLen = size;
690 ret = ERROR_INSUFFICIENT_BUFFER;
692 else {
693 DWORD ndx;
694 HKEY hKey;
695 BOOL winsEnabled = FALSE;
696 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
697 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
698 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
700 memset(pAdapterInfo, 0, size);
701 /* @@ Wine registry key: HKCU\Software\Wine\Network */
702 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
703 &hKey) == ERROR_SUCCESS) {
704 DWORD size = sizeof(primaryWINS.String);
705 unsigned long addr;
707 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
708 (LPBYTE)primaryWINS.String, &size);
709 addr = inet_addr(primaryWINS.String);
710 if (addr != INADDR_NONE && addr != INADDR_ANY)
711 winsEnabled = TRUE;
712 size = sizeof(secondaryWINS.String);
713 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
714 (LPBYTE)secondaryWINS.String, &size);
715 addr = inet_addr(secondaryWINS.String);
716 if (addr != INADDR_NONE && addr != INADDR_ANY)
717 winsEnabled = TRUE;
718 RegCloseKey(hKey);
720 for (ndx = 0; ndx < table->numIndexes; ndx++) {
721 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
722 DWORD addrLen = sizeof(ptr->Address), type, i;
723 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
724 BOOL firstIPAddr = TRUE;
726 /* on Win98 this is left empty, but whatever */
727 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
728 getInterfacePhysicalByIndex(table->indexes[ndx], &addrLen,
729 ptr->Address, &type);
730 /* MS defines address length and type as UINT in some places and
731 DWORD in others, **sigh**. Don't want to assume that PUINT and
732 PDWORD are equiv (64-bit?) */
733 ptr->AddressLength = addrLen;
734 ptr->Type = type;
735 ptr->Index = table->indexes[ndx];
736 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
737 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
738 if (firstIPAddr) {
739 toIPAddressString(ipAddrTable->table[i].dwAddr,
740 ptr->IpAddressList.IpAddress.String);
741 toIPAddressString(ipAddrTable->table[i].dwMask,
742 ptr->IpAddressList.IpMask.String);
743 firstIPAddr = FALSE;
745 else {
746 currentIPAddr->Next = nextIPAddr;
747 currentIPAddr = nextIPAddr;
748 toIPAddressString(ipAddrTable->table[i].dwAddr,
749 currentIPAddr->IpAddress.String);
750 toIPAddressString(ipAddrTable->table[i].dwMask,
751 currentIPAddr->IpMask.String);
752 nextIPAddr++;
756 if (winsEnabled) {
757 ptr->HaveWins = TRUE;
758 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
759 primaryWINS.String, sizeof(primaryWINS.String));
760 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
761 secondaryWINS.String, sizeof(secondaryWINS.String));
763 if (ndx < table->numIndexes - 1)
764 ptr->Next = &pAdapterInfo[ndx + 1];
765 else
766 ptr->Next = NULL;
768 ret = NO_ERROR;
770 HeapFree(GetProcessHeap(), 0, table);
772 else
773 ret = ERROR_OUTOFMEMORY;
774 HeapFree(GetProcessHeap(), 0, ipAddrTable);
777 else
778 ret = ERROR_NO_DATA;
780 TRACE("returning %d\n", ret);
781 return ret;
785 /******************************************************************
786 * GetBestInterface (IPHLPAPI.@)
788 * Get the interface, with the best route for the given IP address.
790 * PARAMS
791 * dwDestAddr [In] IP address to search the interface for
792 * pdwBestIfIndex [Out] found best interface
794 * RETURNS
795 * Success: NO_ERROR
796 * Failure: error code from winerror.h
798 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
800 DWORD ret;
802 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr, pdwBestIfIndex);
803 if (!pdwBestIfIndex)
804 ret = ERROR_INVALID_PARAMETER;
805 else {
806 MIB_IPFORWARDROW ipRow;
808 ret = GetBestRoute(dwDestAddr, 0, &ipRow);
809 if (ret == ERROR_SUCCESS)
810 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
812 TRACE("returning %d\n", ret);
813 return ret;
817 /******************************************************************
818 * GetBestRoute (IPHLPAPI.@)
820 * Get the best route for the given IP address.
822 * PARAMS
823 * dwDestAddr [In] IP address to search the best route for
824 * dwSourceAddr [In] optional source IP address
825 * pBestRoute [Out] found best route
827 * RETURNS
828 * Success: NO_ERROR
829 * Failure: error code from winerror.h
831 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
833 PMIB_IPFORWARDTABLE table;
834 DWORD ret;
836 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
837 dwSourceAddr, pBestRoute);
838 if (!pBestRoute)
839 return ERROR_INVALID_PARAMETER;
841 AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
842 if (table) {
843 DWORD ndx, matchedBits, matchedNdx = 0;
845 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
846 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
847 (dwDestAddr & table->table[ndx].dwForwardMask) ==
848 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
849 DWORD numShifts, mask;
851 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
852 mask && !(mask & 1); mask >>= 1, numShifts++)
854 if (numShifts > matchedBits) {
855 matchedBits = numShifts;
856 matchedNdx = ndx;
860 if (matchedNdx < table->dwNumEntries) {
861 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
862 ret = ERROR_SUCCESS;
864 else {
865 /* No route matches, which can happen if there's no default route. */
866 ret = ERROR_HOST_UNREACHABLE;
868 HeapFree(GetProcessHeap(), 0, table);
870 else
871 ret = ERROR_OUTOFMEMORY;
872 TRACE("returning %d\n", ret);
873 return ret;
877 /******************************************************************
878 * GetFriendlyIfIndex (IPHLPAPI.@)
880 * Get a "friendly" version of IfIndex, which is one that doesn't
881 * have the top byte set. Doesn't validate whether IfIndex is a valid
882 * adapter index.
884 * PARAMS
885 * IfIndex [In] interface index to get the friendly one for
887 * RETURNS
888 * A friendly version of IfIndex.
890 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
892 /* windows doesn't validate these, either, just makes sure the top byte is
893 cleared. I assume my ifenum module never gives an index with the top
894 byte set. */
895 TRACE("returning %d\n", IfIndex);
896 return IfIndex;
900 /******************************************************************
901 * GetIcmpStatistics (IPHLPAPI.@)
903 * Get the ICMP statistics for the local computer.
905 * PARAMS
906 * pStats [Out] buffer for ICMP statistics
908 * RETURNS
909 * Success: NO_ERROR
910 * Failure: error code from winerror.h
912 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
914 DWORD ret;
916 TRACE("pStats %p\n", pStats);
917 ret = getICMPStats(pStats);
918 TRACE("returning %d\n", ret);
919 return ret;
923 /******************************************************************
924 * GetIfEntry (IPHLPAPI.@)
926 * Get information about an interface.
928 * PARAMS
929 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
930 * Out: interface information
932 * RETURNS
933 * Success: NO_ERROR
934 * Failure: error code from winerror.h
936 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
938 DWORD ret;
939 char nameBuf[MAX_ADAPTER_NAME];
940 char *name;
942 TRACE("pIfRow %p\n", pIfRow);
943 if (!pIfRow)
944 return ERROR_INVALID_PARAMETER;
946 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
947 if (name) {
948 ret = getInterfaceEntryByName(name, pIfRow);
949 if (ret == NO_ERROR)
950 ret = getInterfaceStatsByName(name, pIfRow);
952 else
953 ret = ERROR_INVALID_DATA;
954 TRACE("returning %d\n", ret);
955 return ret;
959 static int IfTableSorter(const void *a, const void *b)
961 int ret;
963 if (a && b)
964 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
965 else
966 ret = 0;
967 return ret;
971 /******************************************************************
972 * GetIfTable (IPHLPAPI.@)
974 * Get a table of local interfaces.
976 * PARAMS
977 * pIfTable [Out] buffer for local interfaces table
978 * pdwSize [In/Out] length of output buffer
979 * bOrder [In] whether to sort the table
981 * RETURNS
982 * Success: NO_ERROR
983 * Failure: error code from winerror.h
985 * NOTES
986 * If pdwSize is less than required, the function will return
987 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
988 * size.
989 * If bOrder is true, the returned table will be sorted by interface index.
991 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
993 DWORD ret;
995 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
996 (DWORD)bOrder);
997 if (!pdwSize)
998 ret = ERROR_INVALID_PARAMETER;
999 else {
1000 DWORD numInterfaces = getNumInterfaces();
1001 ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW);
1003 if (!pIfTable || *pdwSize < size) {
1004 *pdwSize = size;
1005 ret = ERROR_INSUFFICIENT_BUFFER;
1007 else {
1008 InterfaceIndexTable *table = getInterfaceIndexTable();
1010 if (table) {
1011 size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) *
1012 sizeof(MIB_IFROW);
1013 if (*pdwSize < size) {
1014 *pdwSize = size;
1015 ret = ERROR_INSUFFICIENT_BUFFER;
1017 else {
1018 DWORD ndx;
1020 *pdwSize = size;
1021 pIfTable->dwNumEntries = 0;
1022 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1023 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1024 GetIfEntry(&pIfTable->table[ndx]);
1025 pIfTable->dwNumEntries++;
1027 if (bOrder)
1028 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1029 IfTableSorter);
1030 ret = NO_ERROR;
1032 HeapFree(GetProcessHeap(), 0, table);
1034 else
1035 ret = ERROR_OUTOFMEMORY;
1038 TRACE("returning %d\n", ret);
1039 return ret;
1043 /******************************************************************
1044 * GetInterfaceInfo (IPHLPAPI.@)
1046 * Get a list of network interface adapters.
1048 * PARAMS
1049 * pIfTable [Out] buffer for interface adapters
1050 * dwOutBufLen [Out] if buffer is too small, returns required size
1052 * RETURNS
1053 * Success: NO_ERROR
1054 * Failure: error code from winerror.h
1056 * BUGS
1057 * MSDN states this should return non-loopback interfaces only.
1059 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1061 DWORD ret;
1063 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1064 if (!dwOutBufLen)
1065 ret = ERROR_INVALID_PARAMETER;
1066 else {
1067 DWORD numInterfaces = getNumInterfaces();
1068 ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) *
1069 sizeof(IP_ADAPTER_INDEX_MAP);
1071 if (!pIfTable || *dwOutBufLen < size) {
1072 *dwOutBufLen = size;
1073 ret = ERROR_INSUFFICIENT_BUFFER;
1075 else {
1076 InterfaceIndexTable *table = getInterfaceIndexTable();
1078 if (table) {
1079 size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) *
1080 sizeof(IP_ADAPTER_INDEX_MAP);
1081 if (*dwOutBufLen < size) {
1082 *dwOutBufLen = size;
1083 ret = ERROR_INSUFFICIENT_BUFFER;
1085 else {
1086 DWORD ndx;
1087 char nameBuf[MAX_ADAPTER_NAME];
1089 *dwOutBufLen = size;
1090 pIfTable->NumAdapters = 0;
1091 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1092 const char *walker, *name;
1093 WCHAR *assigner;
1095 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1096 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1097 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1098 walker && *walker &&
1099 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1100 walker++, assigner++)
1101 *assigner = *walker;
1102 *assigner = 0;
1103 pIfTable->NumAdapters++;
1105 ret = NO_ERROR;
1107 HeapFree(GetProcessHeap(), 0, table);
1109 else
1110 ret = ERROR_OUTOFMEMORY;
1113 TRACE("returning %d\n", ret);
1114 return ret;
1118 /******************************************************************
1119 * GetIpAddrTable (IPHLPAPI.@)
1121 * Get interface-to-IP address mapping table.
1123 * PARAMS
1124 * pIpAddrTable [Out] buffer for mapping table
1125 * pdwSize [In/Out] length of output buffer
1126 * bOrder [In] whether to sort the table
1128 * RETURNS
1129 * Success: NO_ERROR
1130 * Failure: error code from winerror.h
1132 * NOTES
1133 * If pdwSize is less than required, the function will return
1134 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1135 * size.
1136 * If bOrder is true, the returned table will be sorted by the next hop and
1137 * an assortment of arbitrary parameters.
1139 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1141 DWORD ret;
1143 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1144 (DWORD)bOrder);
1145 if (!pdwSize)
1146 ret = ERROR_INVALID_PARAMETER;
1147 else {
1148 PMIB_IPADDRTABLE table;
1150 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1151 if (ret == NO_ERROR)
1153 ULONG size = sizeof(MIB_IPADDRTABLE) + (table->dwNumEntries - 1) *
1154 sizeof(MIB_IPADDRROW);
1156 if (!pIpAddrTable || *pdwSize < size) {
1157 *pdwSize = size;
1158 ret = ERROR_INSUFFICIENT_BUFFER;
1160 else {
1161 *pdwSize = size;
1162 memcpy(pIpAddrTable, table, sizeof(MIB_IPADDRTABLE) +
1163 (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW));
1164 if (bOrder)
1165 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1166 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1167 ret = NO_ERROR;
1169 HeapFree(GetProcessHeap(), 0, table);
1172 TRACE("returning %d\n", ret);
1173 return ret;
1177 /******************************************************************
1178 * GetIpForwardTable (IPHLPAPI.@)
1180 * Get the route table.
1182 * PARAMS
1183 * pIpForwardTable [Out] buffer for route table
1184 * pdwSize [In/Out] length of output buffer
1185 * bOrder [In] whether to sort the table
1187 * RETURNS
1188 * Success: NO_ERROR
1189 * Failure: error code from winerror.h
1191 * NOTES
1192 * If pdwSize is less than required, the function will return
1193 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1194 * size.
1195 * If bOrder is true, the returned table will be sorted by the next hop and
1196 * an assortment of arbitrary parameters.
1198 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1200 DWORD ret;
1202 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable,
1203 pdwSize, (DWORD)bOrder);
1204 if (!pdwSize)
1205 ret = ERROR_INVALID_PARAMETER;
1206 else {
1207 DWORD numRoutes = getNumRoutes();
1208 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (numRoutes - 1) *
1209 sizeof(MIB_IPFORWARDROW);
1211 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1212 *pdwSize = sizeNeeded;
1213 ret = ERROR_INSUFFICIENT_BUFFER;
1215 else {
1216 PMIB_IPFORWARDTABLE table;
1218 ret = getRouteTable(&table, GetProcessHeap(), 0);
1219 if (!ret) {
1220 sizeNeeded = sizeof(MIB_IPFORWARDTABLE) + (table->dwNumEntries - 1) *
1221 sizeof(MIB_IPFORWARDROW);
1222 if (*pdwSize < sizeNeeded) {
1223 *pdwSize = sizeNeeded;
1224 ret = ERROR_INSUFFICIENT_BUFFER;
1226 else {
1227 *pdwSize = sizeNeeded;
1228 memcpy(pIpForwardTable, table, sizeNeeded);
1229 if (bOrder)
1230 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1231 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1232 ret = NO_ERROR;
1234 HeapFree(GetProcessHeap(), 0, table);
1238 TRACE("returning %d\n", ret);
1239 return ret;
1243 /******************************************************************
1244 * GetIpNetTable (IPHLPAPI.@)
1246 * Get the IP-to-physical address mapping table.
1248 * PARAMS
1249 * pIpNetTable [Out] buffer for mapping table
1250 * pdwSize [In/Out] length of output buffer
1251 * bOrder [In] whether to sort the table
1253 * RETURNS
1254 * Success: NO_ERROR
1255 * Failure: error code from winerror.h
1257 * NOTES
1258 * If pdwSize is less than required, the function will return
1259 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1260 * size.
1261 * If bOrder is true, the returned table will be sorted by IP address.
1263 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1265 DWORD ret;
1267 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize,
1268 (DWORD)bOrder);
1269 if (!pdwSize)
1270 ret = ERROR_INVALID_PARAMETER;
1271 else {
1272 DWORD numEntries = getNumArpEntries();
1273 ULONG size = sizeof(MIB_IPNETTABLE) + (numEntries - 1) *
1274 sizeof(MIB_IPNETROW);
1276 if (!pIpNetTable || *pdwSize < size) {
1277 *pdwSize = size;
1278 ret = ERROR_INSUFFICIENT_BUFFER;
1280 else {
1281 PMIB_IPNETTABLE table;
1283 ret = getArpTable(&table, GetProcessHeap(), 0);
1284 if (!ret) {
1285 size = sizeof(MIB_IPNETTABLE) + (table->dwNumEntries - 1) *
1286 sizeof(MIB_IPNETROW);
1287 if (*pdwSize < size) {
1288 *pdwSize = size;
1289 ret = ERROR_INSUFFICIENT_BUFFER;
1291 else {
1292 *pdwSize = size;
1293 memcpy(pIpNetTable, table, size);
1294 if (bOrder)
1295 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1296 sizeof(MIB_IPNETROW), IpNetTableSorter);
1297 ret = NO_ERROR;
1299 HeapFree(GetProcessHeap(), 0, table);
1303 TRACE("returning %d\n", ret);
1304 return ret;
1308 /******************************************************************
1309 * GetIpStatistics (IPHLPAPI.@)
1311 * Get the IP statistics for the local computer.
1313 * PARAMS
1314 * pStats [Out] buffer for IP statistics
1316 * RETURNS
1317 * Success: NO_ERROR
1318 * Failure: error code from winerror.h
1320 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1322 DWORD ret;
1324 TRACE("pStats %p\n", pStats);
1325 ret = getIPStats(pStats);
1326 TRACE("returning %d\n", ret);
1327 return ret;
1331 /******************************************************************
1332 * GetNetworkParams (IPHLPAPI.@)
1334 * Get the network parameters for the local computer.
1336 * PARAMS
1337 * pFixedInfo [Out] buffer for network parameters
1338 * pOutBufLen [In/Out] length of output buffer
1340 * RETURNS
1341 * Success: NO_ERROR
1342 * Failure: error code from winerror.h
1344 * NOTES
1345 * If pOutBufLen is less than required, the function will return
1346 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1347 * size.
1349 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1351 DWORD ret, size;
1352 LONG regReturn;
1353 HKEY hKey;
1355 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1356 if (!pOutBufLen)
1357 return ERROR_INVALID_PARAMETER;
1359 initialise_resolver();
1360 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1361 sizeof(IP_ADDR_STRING) : 0);
1362 if (!pFixedInfo || *pOutBufLen < size) {
1363 *pOutBufLen = size;
1364 return ERROR_BUFFER_OVERFLOW;
1367 memset(pFixedInfo, 0, size);
1368 size = sizeof(pFixedInfo->HostName);
1369 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1370 size = sizeof(pFixedInfo->DomainName);
1371 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1372 if (_res.nscount > 0) {
1373 PIP_ADDR_STRING ptr;
1374 int i;
1376 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1377 i++, ptr = ptr->Next) {
1378 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1379 ptr->IpAddress.String);
1380 if (i == _res.nscount - 1)
1381 ptr->Next = NULL;
1382 else if (i == 0)
1383 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1384 else
1385 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1388 pFixedInfo->NodeType = HYBRID_NODETYPE;
1389 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1390 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1391 if (regReturn != ERROR_SUCCESS)
1392 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1393 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1394 &hKey);
1395 if (regReturn == ERROR_SUCCESS)
1397 DWORD size = sizeof(pFixedInfo->ScopeId);
1399 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1400 RegCloseKey(hKey);
1403 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1404 I suppose could also check for a listener on port 53 to set EnableDns */
1405 ret = NO_ERROR;
1406 TRACE("returning %d\n", ret);
1407 return ret;
1411 /******************************************************************
1412 * GetNumberOfInterfaces (IPHLPAPI.@)
1414 * Get the number of interfaces.
1416 * PARAMS
1417 * pdwNumIf [Out] number of interfaces
1419 * RETURNS
1420 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1422 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1424 DWORD ret;
1426 TRACE("pdwNumIf %p\n", pdwNumIf);
1427 if (!pdwNumIf)
1428 ret = ERROR_INVALID_PARAMETER;
1429 else {
1430 *pdwNumIf = getNumInterfaces();
1431 ret = NO_ERROR;
1433 TRACE("returning %d\n", ret);
1434 return ret;
1438 /******************************************************************
1439 * GetPerAdapterInfo (IPHLPAPI.@)
1441 * Get information about an adapter corresponding to an interface.
1443 * PARAMS
1444 * IfIndex [In] interface info
1445 * pPerAdapterInfo [Out] buffer for per adapter info
1446 * pOutBufLen [In/Out] length of output buffer
1448 * RETURNS
1449 * Success: NO_ERROR
1450 * Failure: error code from winerror.h
1452 * FIXME
1453 * Stub, returns ERROR_NOT_SUPPORTED.
1455 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1457 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex,
1458 pPerAdapterInfo, pOutBufLen);
1459 return ERROR_NOT_SUPPORTED;
1463 /******************************************************************
1464 * GetRTTAndHopCount (IPHLPAPI.@)
1466 * Get round-trip time (RTT) and hop count.
1468 * PARAMS
1470 * DestIpAddress [In] destination address to get the info for
1471 * HopCount [Out] retrieved hop count
1472 * MaxHops [In] maximum hops to search for the destination
1473 * RTT [Out] RTT in milliseconds
1475 * RETURNS
1476 * Success: TRUE
1477 * Failure: FALSE
1479 * FIXME
1480 * Stub, returns FALSE.
1482 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1484 FIXME("(DestIpAddress 0x%08lx, HopCount %p, MaxHops %d, RTT %p): stub\n",
1485 DestIpAddress, HopCount, MaxHops, RTT);
1486 return FALSE;
1490 /******************************************************************
1491 * GetTcpStatistics (IPHLPAPI.@)
1493 * Get the TCP statistics for the local computer.
1495 * PARAMS
1496 * pStats [Out] buffer for TCP statistics
1498 * RETURNS
1499 * Success: NO_ERROR
1500 * Failure: error code from winerror.h
1502 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1504 DWORD ret;
1506 TRACE("pStats %p\n", pStats);
1507 ret = getTCPStats(pStats);
1508 TRACE("returning %d\n", ret);
1509 return ret;
1513 /******************************************************************
1514 * GetTcpTable (IPHLPAPI.@)
1516 * Get the table of active TCP connections.
1518 * PARAMS
1519 * pTcpTable [Out] buffer for TCP connections table
1520 * pdwSize [In/Out] length of output buffer
1521 * bOrder [In] whether to order the table
1523 * RETURNS
1524 * Success: NO_ERROR
1525 * Failure: error code from winerror.h
1527 * NOTES
1528 * If pdwSize is less than required, the function will return
1529 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1530 * the required byte size.
1531 * If bOrder is true, the returned table will be sorted, first by
1532 * local address and port number, then by remote address and port
1533 * number.
1535 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1537 DWORD ret;
1539 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize,
1540 (DWORD)bOrder);
1541 if (!pdwSize)
1542 ret = ERROR_INVALID_PARAMETER;
1543 else {
1544 DWORD numEntries = getNumTcpEntries();
1545 DWORD size = sizeof(MIB_TCPTABLE) + (numEntries - 1) * sizeof(MIB_TCPROW);
1547 if (!pTcpTable || *pdwSize < size) {
1548 *pdwSize = size;
1549 ret = ERROR_INSUFFICIENT_BUFFER;
1551 else {
1552 ret = getTcpTable(&pTcpTable, numEntries, 0, 0);
1553 if (!ret) {
1554 size = sizeof(MIB_TCPTABLE) + (pTcpTable->dwNumEntries - 1) *
1555 sizeof(MIB_TCPROW);
1556 *pdwSize = size;
1558 if (bOrder)
1559 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1560 sizeof(MIB_TCPROW), TcpTableSorter);
1561 ret = NO_ERROR;
1563 else
1564 ret = ERROR_OUTOFMEMORY;
1567 TRACE("returning %d\n", ret);
1568 return ret;
1572 /******************************************************************
1573 * GetUdpStatistics (IPHLPAPI.@)
1575 * Get the UDP statistics for the local computer.
1577 * PARAMS
1578 * pStats [Out] buffer for UDP statistics
1580 * RETURNS
1581 * Success: NO_ERROR
1582 * Failure: error code from winerror.h
1584 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1586 DWORD ret;
1588 TRACE("pStats %p\n", pStats);
1589 ret = getUDPStats(pStats);
1590 TRACE("returning %d\n", ret);
1591 return ret;
1595 /******************************************************************
1596 * GetUdpTable (IPHLPAPI.@)
1598 * Get a table of active UDP connections.
1600 * PARAMS
1601 * pUdpTable [Out] buffer for UDP connections table
1602 * pdwSize [In/Out] length of output buffer
1603 * bOrder [In] whether to order the table
1605 * RETURNS
1606 * Success: NO_ERROR
1607 * Failure: error code from winerror.h
1609 * NOTES
1610 * If pdwSize is less than required, the function will return
1611 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1612 * required byte size.
1613 * If bOrder is true, the returned table will be sorted, first by
1614 * local address, then by local port number.
1616 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1618 DWORD ret;
1620 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize,
1621 (DWORD)bOrder);
1622 if (!pdwSize)
1623 ret = ERROR_INVALID_PARAMETER;
1624 else {
1625 DWORD numEntries = getNumUdpEntries();
1626 DWORD size = sizeof(MIB_UDPTABLE) + (numEntries - 1) * sizeof(MIB_UDPROW);
1628 if (!pUdpTable || *pdwSize < size) {
1629 *pdwSize = size;
1630 ret = ERROR_INSUFFICIENT_BUFFER;
1632 else {
1633 PMIB_UDPTABLE table;
1635 ret = getUdpTable(&table, GetProcessHeap(), 0);
1636 if (!ret) {
1637 size = sizeof(MIB_UDPTABLE) + (table->dwNumEntries - 1) *
1638 sizeof(MIB_UDPROW);
1639 if (*pdwSize < size) {
1640 *pdwSize = size;
1641 ret = ERROR_INSUFFICIENT_BUFFER;
1643 else {
1644 *pdwSize = size;
1645 memcpy(pUdpTable, table, size);
1646 if (bOrder)
1647 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1648 sizeof(MIB_UDPROW), UdpTableSorter);
1649 ret = NO_ERROR;
1651 HeapFree(GetProcessHeap(), 0, table);
1653 else
1654 ret = ERROR_OUTOFMEMORY;
1657 TRACE("returning %d\n", ret);
1658 return ret;
1662 /******************************************************************
1663 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1665 * This is a Win98-only function to get information on "unidirectional"
1666 * adapters. Since this is pretty nonsensical in other contexts, it
1667 * never returns anything.
1669 * PARAMS
1670 * pIPIfInfo [Out] buffer for adapter infos
1671 * dwOutBufLen [Out] length of the output buffer
1673 * RETURNS
1674 * Success: NO_ERROR
1675 * Failure: error code from winerror.h
1677 * FIXME
1678 * Stub, returns ERROR_NOT_SUPPORTED.
1680 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1682 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1683 /* a unidirectional adapter?? not bloody likely! */
1684 return ERROR_NOT_SUPPORTED;
1688 /******************************************************************
1689 * IpReleaseAddress (IPHLPAPI.@)
1691 * Release an IP optained through DHCP,
1693 * PARAMS
1694 * AdapterInfo [In] adapter to release IP address
1696 * RETURNS
1697 * Success: NO_ERROR
1698 * Failure: error code from winerror.h
1700 * NOTES
1701 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1702 * this function does nothing.
1704 * FIXME
1705 * Stub, returns ERROR_NOT_SUPPORTED.
1707 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1709 TRACE("AdapterInfo %p\n", AdapterInfo);
1710 /* not a stub, never going to support this (and I never mark an adapter as
1711 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1712 return ERROR_NOT_SUPPORTED;
1716 /******************************************************************
1717 * IpRenewAddress (IPHLPAPI.@)
1719 * Renew an IP optained through DHCP.
1721 * PARAMS
1722 * AdapterInfo [In] adapter to renew IP address
1724 * RETURNS
1725 * Success: NO_ERROR
1726 * Failure: error code from winerror.h
1728 * NOTES
1729 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1730 * this function does nothing.
1732 * FIXME
1733 * Stub, returns ERROR_NOT_SUPPORTED.
1735 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1737 TRACE("AdapterInfo %p\n", AdapterInfo);
1738 /* not a stub, never going to support this (and I never mark an adapter as
1739 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1740 return ERROR_NOT_SUPPORTED;
1744 /******************************************************************
1745 * NotifyAddrChange (IPHLPAPI.@)
1747 * Notify caller whenever the ip-interface map is changed.
1749 * PARAMS
1750 * Handle [Out] handle useable in asynchronus notification
1751 * overlapped [In] overlapped structure that notifies the caller
1753 * RETURNS
1754 * Success: NO_ERROR
1755 * Failure: error code from winerror.h
1757 * FIXME
1758 * Stub, returns ERROR_NOT_SUPPORTED.
1760 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1762 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1763 return ERROR_NOT_SUPPORTED;
1767 /******************************************************************
1768 * NotifyRouteChange (IPHLPAPI.@)
1770 * Notify caller whenever the ip routing table is changed.
1772 * PARAMS
1773 * Handle [Out] handle useable in asynchronus notification
1774 * overlapped [In] overlapped structure that notifies the caller
1776 * RETURNS
1777 * Success: NO_ERROR
1778 * Failure: error code from winerror.h
1780 * FIXME
1781 * Stub, returns ERROR_NOT_SUPPORTED.
1783 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1785 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1786 return ERROR_NOT_SUPPORTED;
1790 /******************************************************************
1791 * SendARP (IPHLPAPI.@)
1793 * Send an ARP request.
1795 * PARAMS
1796 * DestIP [In] attempt to obtain this IP
1797 * SrcIP [In] optional sender IP address
1798 * pMacAddr [Out] buffer for the mac address
1799 * PhyAddrLen [In/Out] length of the output buffer
1801 * RETURNS
1802 * Success: NO_ERROR
1803 * Failure: error code from winerror.h
1805 * FIXME
1806 * Stub, returns ERROR_NOT_SUPPORTED.
1808 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1810 FIXME("(DestIP 0x%08lx, SrcIP 0x%08lx, pMacAddr %p, PhyAddrLen %p): stub\n",
1811 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1812 return ERROR_NOT_SUPPORTED;
1816 /******************************************************************
1817 * SetIfEntry (IPHLPAPI.@)
1819 * Set the administrative status of an interface.
1821 * PARAMS
1822 * pIfRow [In] dwAdminStatus member specifies the new status.
1824 * RETURNS
1825 * Success: NO_ERROR
1826 * Failure: error code from winerror.h
1828 * FIXME
1829 * Stub, returns ERROR_NOT_SUPPORTED.
1831 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1833 FIXME("(pIfRow %p): stub\n", pIfRow);
1834 /* this is supposed to set an interface administratively up or down.
1835 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1836 this sort of down is indistinguishable from other sorts of down (e.g. no
1837 link). */
1838 return ERROR_NOT_SUPPORTED;
1842 /******************************************************************
1843 * SetIpForwardEntry (IPHLPAPI.@)
1845 * Modify an existing route.
1847 * PARAMS
1848 * pRoute [In] route with the new information
1850 * RETURNS
1851 * Success: NO_ERROR
1852 * Failure: error code from winerror.h
1854 * FIXME
1855 * Stub, returns NO_ERROR.
1857 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1859 FIXME("(pRoute %p): stub\n", pRoute);
1860 /* this is to add a route entry, how's it distinguishable from
1861 CreateIpForwardEntry?
1862 could use SIOCADDRT, not sure I want to */
1863 return (DWORD) 0;
1867 /******************************************************************
1868 * SetIpNetEntry (IPHLPAPI.@)
1870 * Modify an existing ARP entry.
1872 * PARAMS
1873 * pArpEntry [In] ARP entry with the new information
1875 * RETURNS
1876 * Success: NO_ERROR
1877 * Failure: error code from winerror.h
1879 * FIXME
1880 * Stub, returns NO_ERROR.
1882 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1884 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1885 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1886 return (DWORD) 0;
1890 /******************************************************************
1891 * SetIpStatistics (IPHLPAPI.@)
1893 * Toggle IP forwarding and det the default TTL value.
1895 * PARAMS
1896 * pIpStats [In] IP statistics with the new information
1898 * RETURNS
1899 * Success: NO_ERROR
1900 * Failure: error code from winerror.h
1902 * FIXME
1903 * Stub, returns NO_ERROR.
1905 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1907 FIXME("(pIpStats %p): stub\n", pIpStats);
1908 return (DWORD) 0;
1912 /******************************************************************
1913 * SetIpTTL (IPHLPAPI.@)
1915 * Set the default TTL value.
1917 * PARAMS
1918 * nTTL [In] new TTL value
1920 * RETURNS
1921 * Success: NO_ERROR
1922 * Failure: error code from winerror.h
1924 * FIXME
1925 * Stub, returns NO_ERROR.
1927 DWORD WINAPI SetIpTTL(UINT nTTL)
1929 FIXME("(nTTL %d): stub\n", nTTL);
1930 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1931 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1932 return (DWORD) 0;
1936 /******************************************************************
1937 * SetTcpEntry (IPHLPAPI.@)
1939 * Set the state of a TCP connection.
1941 * PARAMS
1942 * pTcpRow [In] specifies connection with new state
1944 * RETURNS
1945 * Success: NO_ERROR
1946 * Failure: error code from winerror.h
1948 * FIXME
1949 * Stub, returns NO_ERROR.
1951 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1953 FIXME("(pTcpRow %p): stub\n", pTcpRow);
1954 return (DWORD) 0;
1958 /******************************************************************
1959 * UnenableRouter (IPHLPAPI.@)
1961 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
1962 * if it reaches zero.
1964 * PARAMS
1965 * pOverlapped [In/Out] should be the same as in EnableRouter()
1966 * lpdwEnableCount [Out] optional, receives reference count
1968 * RETURNS
1969 * Success: NO_ERROR
1970 * Failure: error code from winerror.h
1972 * FIXME
1973 * Stub, returns ERROR_NOT_SUPPORTED.
1975 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
1977 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
1978 lpdwEnableCount);
1979 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
1980 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
1982 return ERROR_NOT_SUPPORTED;