iphlpapi: Don't allocate gobs of memory when the ARP table is empty.
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
blob92df63124d215f760f0ce057c77f6683557627e5
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 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
631 char adapterName[MAX_ADAPTER_NAME];
632 int i;
633 DWORD ret;
635 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
636 /* The adapter name is guaranteed not to have any unicode characters, so
637 * this translation is never lossy */
638 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
639 adapterName[i] = (char)AdapterName[i];
640 adapterName[i] = '\0';
641 ret = getInterfaceIndexByName(adapterName, IfIndex);
642 TRACE("returning %d\n", ret);
643 return ret;
647 /******************************************************************
648 * GetAdaptersInfo (IPHLPAPI.@)
650 * Get information about adapters.
652 * PARAMS
653 * pAdapterInfo [Out] buffer for adapter infos
654 * pOutBufLen [In] length of output buffer
656 * RETURNS
657 * Success: NO_ERROR
658 * Failure: error code from winerror.h
660 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
662 DWORD ret;
664 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
665 if (!pOutBufLen)
666 ret = ERROR_INVALID_PARAMETER;
667 else {
668 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
670 if (numNonLoopbackInterfaces > 0) {
671 DWORD numIPAddresses = getNumIPAddresses();
672 ULONG size;
674 /* This may slightly overestimate the amount of space needed, because
675 * the IP addresses include the loopback address, but it's easier
676 * to make sure there's more than enough space than to make sure there's
677 * precisely enough space.
679 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
680 size += numIPAddresses * sizeof(IP_ADDR_STRING);
681 if (!pAdapterInfo || *pOutBufLen < size) {
682 *pOutBufLen = size;
683 ret = ERROR_BUFFER_OVERFLOW;
685 else {
686 InterfaceIndexTable *table = NULL;
687 PMIB_IPADDRTABLE ipAddrTable = NULL;
688 PMIB_IPFORWARDTABLE routeTable = NULL;
690 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
691 if (!ret)
692 ret = getRouteTable(&routeTable, GetProcessHeap(), 0);
693 if (!ret)
694 table = getNonLoopbackInterfaceIndexTable();
695 if (table) {
696 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
697 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
698 if (*pOutBufLen < size) {
699 *pOutBufLen = size;
700 ret = ERROR_INSUFFICIENT_BUFFER;
702 else {
703 DWORD ndx;
704 HKEY hKey;
705 BOOL winsEnabled = FALSE;
706 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
707 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
708 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
710 memset(pAdapterInfo, 0, size);
711 /* @@ Wine registry key: HKCU\Software\Wine\Network */
712 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
713 &hKey) == ERROR_SUCCESS) {
714 DWORD size = sizeof(primaryWINS.String);
715 unsigned long addr;
717 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
718 (LPBYTE)primaryWINS.String, &size);
719 addr = inet_addr(primaryWINS.String);
720 if (addr != INADDR_NONE && addr != INADDR_ANY)
721 winsEnabled = TRUE;
722 size = sizeof(secondaryWINS.String);
723 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
724 (LPBYTE)secondaryWINS.String, &size);
725 addr = inet_addr(secondaryWINS.String);
726 if (addr != INADDR_NONE && addr != INADDR_ANY)
727 winsEnabled = TRUE;
728 RegCloseKey(hKey);
730 for (ndx = 0; ndx < table->numIndexes; ndx++) {
731 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
732 DWORD i;
733 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
734 BOOL firstIPAddr = TRUE;
736 /* on Win98 this is left empty, but whatever */
737 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
738 getInterfacePhysicalByIndex(table->indexes[ndx],
739 &ptr->AddressLength, ptr->Address, &ptr->Type);
740 ptr->Index = table->indexes[ndx];
741 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
742 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
743 if (firstIPAddr) {
744 toIPAddressString(ipAddrTable->table[i].dwAddr,
745 ptr->IpAddressList.IpAddress.String);
746 toIPAddressString(ipAddrTable->table[i].dwMask,
747 ptr->IpAddressList.IpMask.String);
748 firstIPAddr = FALSE;
750 else {
751 currentIPAddr->Next = nextIPAddr;
752 currentIPAddr = nextIPAddr;
753 toIPAddressString(ipAddrTable->table[i].dwAddr,
754 currentIPAddr->IpAddress.String);
755 toIPAddressString(ipAddrTable->table[i].dwMask,
756 currentIPAddr->IpMask.String);
757 nextIPAddr++;
761 /* Find first router through this interface, which we'll assume
762 * is the default gateway for this adapter */
763 for (i = 0; i < routeTable->dwNumEntries; i++)
764 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
765 && routeTable->table[i].dwForwardType ==
766 MIB_IPROUTE_TYPE_INDIRECT)
767 toIPAddressString(routeTable->table[i].dwForwardNextHop,
768 ptr->GatewayList.IpAddress.String);
769 if (winsEnabled) {
770 ptr->HaveWins = TRUE;
771 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
772 primaryWINS.String, sizeof(primaryWINS.String));
773 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
774 secondaryWINS.String, sizeof(secondaryWINS.String));
776 if (ndx < table->numIndexes - 1)
777 ptr->Next = &pAdapterInfo[ndx + 1];
778 else
779 ptr->Next = NULL;
781 ret = NO_ERROR;
783 HeapFree(GetProcessHeap(), 0, table);
785 else
786 ret = ERROR_OUTOFMEMORY;
787 HeapFree(GetProcessHeap(), 0, routeTable);
788 HeapFree(GetProcessHeap(), 0, ipAddrTable);
791 else
792 ret = ERROR_NO_DATA;
794 TRACE("returning %d\n", ret);
795 return ret;
799 /******************************************************************
800 * GetBestInterface (IPHLPAPI.@)
802 * Get the interface, with the best route for the given IP address.
804 * PARAMS
805 * dwDestAddr [In] IP address to search the interface for
806 * pdwBestIfIndex [Out] found best interface
808 * RETURNS
809 * Success: NO_ERROR
810 * Failure: error code from winerror.h
812 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
814 DWORD ret;
816 TRACE("dwDestAddr 0x%08lx, pdwBestIfIndex %p\n", dwDestAddr, pdwBestIfIndex);
817 if (!pdwBestIfIndex)
818 ret = ERROR_INVALID_PARAMETER;
819 else {
820 MIB_IPFORWARDROW ipRow;
822 ret = GetBestRoute(dwDestAddr, 0, &ipRow);
823 if (ret == ERROR_SUCCESS)
824 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
826 TRACE("returning %d\n", ret);
827 return ret;
831 /******************************************************************
832 * GetBestRoute (IPHLPAPI.@)
834 * Get the best route for the given IP address.
836 * PARAMS
837 * dwDestAddr [In] IP address to search the best route for
838 * dwSourceAddr [In] optional source IP address
839 * pBestRoute [Out] found best route
841 * RETURNS
842 * Success: NO_ERROR
843 * Failure: error code from winerror.h
845 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
847 PMIB_IPFORWARDTABLE table;
848 DWORD ret;
850 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
851 dwSourceAddr, pBestRoute);
852 if (!pBestRoute)
853 return ERROR_INVALID_PARAMETER;
855 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
856 if (!ret) {
857 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
859 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
860 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
861 (dwDestAddr & table->table[ndx].dwForwardMask) ==
862 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
863 DWORD numShifts, mask;
865 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
866 mask && !(mask & 1); mask >>= 1, numShifts++)
868 if (numShifts > matchedBits) {
869 matchedBits = numShifts;
870 matchedNdx = ndx;
872 else if (!matchedBits && table->table[ndx].dwForwardType ==
873 MIB_IPROUTE_TYPE_INDIRECT) {
874 /* default to a default gateway */
875 matchedNdx = ndx;
879 if (matchedNdx < table->dwNumEntries) {
880 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
881 ret = ERROR_SUCCESS;
883 else {
884 /* No route matches, which can happen if there's no default route. */
885 ret = ERROR_HOST_UNREACHABLE;
887 HeapFree(GetProcessHeap(), 0, table);
889 TRACE("returning %d\n", ret);
890 return ret;
894 /******************************************************************
895 * GetFriendlyIfIndex (IPHLPAPI.@)
897 * Get a "friendly" version of IfIndex, which is one that doesn't
898 * have the top byte set. Doesn't validate whether IfIndex is a valid
899 * adapter index.
901 * PARAMS
902 * IfIndex [In] interface index to get the friendly one for
904 * RETURNS
905 * A friendly version of IfIndex.
907 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
909 /* windows doesn't validate these, either, just makes sure the top byte is
910 cleared. I assume my ifenum module never gives an index with the top
911 byte set. */
912 TRACE("returning %d\n", IfIndex);
913 return IfIndex;
917 /******************************************************************
918 * GetIcmpStatistics (IPHLPAPI.@)
920 * Get the ICMP statistics for the local computer.
922 * PARAMS
923 * pStats [Out] buffer for ICMP statistics
925 * RETURNS
926 * Success: NO_ERROR
927 * Failure: error code from winerror.h
929 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
931 DWORD ret;
933 TRACE("pStats %p\n", pStats);
934 ret = getICMPStats(pStats);
935 TRACE("returning %d\n", ret);
936 return ret;
940 /******************************************************************
941 * GetIfEntry (IPHLPAPI.@)
943 * Get information about an interface.
945 * PARAMS
946 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
947 * Out: interface information
949 * RETURNS
950 * Success: NO_ERROR
951 * Failure: error code from winerror.h
953 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
955 DWORD ret;
956 char nameBuf[MAX_ADAPTER_NAME];
957 char *name;
959 TRACE("pIfRow %p\n", pIfRow);
960 if (!pIfRow)
961 return ERROR_INVALID_PARAMETER;
963 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
964 if (name) {
965 ret = getInterfaceEntryByName(name, pIfRow);
966 if (ret == NO_ERROR)
967 ret = getInterfaceStatsByName(name, pIfRow);
969 else
970 ret = ERROR_INVALID_DATA;
971 TRACE("returning %d\n", ret);
972 return ret;
976 static int IfTableSorter(const void *a, const void *b)
978 int ret;
980 if (a && b)
981 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
982 else
983 ret = 0;
984 return ret;
988 /******************************************************************
989 * GetIfTable (IPHLPAPI.@)
991 * Get a table of local interfaces.
993 * PARAMS
994 * pIfTable [Out] buffer for local interfaces table
995 * pdwSize [In/Out] length of output buffer
996 * bOrder [In] whether to sort the table
998 * RETURNS
999 * Success: NO_ERROR
1000 * Failure: error code from winerror.h
1002 * NOTES
1003 * If pdwSize is less than required, the function will return
1004 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1005 * size.
1006 * If bOrder is true, the returned table will be sorted by interface index.
1008 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1010 DWORD ret;
1012 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1013 (DWORD)bOrder);
1014 if (!pdwSize)
1015 ret = ERROR_INVALID_PARAMETER;
1016 else {
1017 DWORD numInterfaces = getNumInterfaces();
1018 ULONG size = sizeof(MIB_IFTABLE) + (numInterfaces - 1) * sizeof(MIB_IFROW);
1020 if (!pIfTable || *pdwSize < size) {
1021 *pdwSize = size;
1022 ret = ERROR_INSUFFICIENT_BUFFER;
1024 else {
1025 InterfaceIndexTable *table = getInterfaceIndexTable();
1027 if (table) {
1028 size = sizeof(MIB_IFTABLE) + (table->numIndexes - 1) *
1029 sizeof(MIB_IFROW);
1030 if (*pdwSize < size) {
1031 *pdwSize = size;
1032 ret = ERROR_INSUFFICIENT_BUFFER;
1034 else {
1035 DWORD ndx;
1037 *pdwSize = size;
1038 pIfTable->dwNumEntries = 0;
1039 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1040 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1041 GetIfEntry(&pIfTable->table[ndx]);
1042 pIfTable->dwNumEntries++;
1044 if (bOrder)
1045 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1046 IfTableSorter);
1047 ret = NO_ERROR;
1049 HeapFree(GetProcessHeap(), 0, table);
1051 else
1052 ret = ERROR_OUTOFMEMORY;
1055 TRACE("returning %d\n", ret);
1056 return ret;
1060 /******************************************************************
1061 * GetInterfaceInfo (IPHLPAPI.@)
1063 * Get a list of network interface adapters.
1065 * PARAMS
1066 * pIfTable [Out] buffer for interface adapters
1067 * dwOutBufLen [Out] if buffer is too small, returns required size
1069 * RETURNS
1070 * Success: NO_ERROR
1071 * Failure: error code from winerror.h
1073 * BUGS
1074 * MSDN states this should return non-loopback interfaces only.
1076 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1078 DWORD ret;
1080 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1081 if (!dwOutBufLen)
1082 ret = ERROR_INVALID_PARAMETER;
1083 else {
1084 DWORD numInterfaces = getNumInterfaces();
1085 ULONG size = sizeof(IP_INTERFACE_INFO) + (numInterfaces - 1) *
1086 sizeof(IP_ADAPTER_INDEX_MAP);
1088 if (!pIfTable || *dwOutBufLen < size) {
1089 *dwOutBufLen = size;
1090 ret = ERROR_INSUFFICIENT_BUFFER;
1092 else {
1093 InterfaceIndexTable *table = getInterfaceIndexTable();
1095 if (table) {
1096 size = sizeof(IP_INTERFACE_INFO) + (table->numIndexes - 1) *
1097 sizeof(IP_ADAPTER_INDEX_MAP);
1098 if (*dwOutBufLen < size) {
1099 *dwOutBufLen = size;
1100 ret = ERROR_INSUFFICIENT_BUFFER;
1102 else {
1103 DWORD ndx;
1104 char nameBuf[MAX_ADAPTER_NAME];
1106 *dwOutBufLen = size;
1107 pIfTable->NumAdapters = 0;
1108 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1109 const char *walker, *name;
1110 WCHAR *assigner;
1112 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1113 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1114 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1115 walker && *walker &&
1116 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1117 walker++, assigner++)
1118 *assigner = *walker;
1119 *assigner = 0;
1120 pIfTable->NumAdapters++;
1122 ret = NO_ERROR;
1124 HeapFree(GetProcessHeap(), 0, table);
1126 else
1127 ret = ERROR_OUTOFMEMORY;
1130 TRACE("returning %d\n", ret);
1131 return ret;
1135 /******************************************************************
1136 * GetIpAddrTable (IPHLPAPI.@)
1138 * Get interface-to-IP address mapping table.
1140 * PARAMS
1141 * pIpAddrTable [Out] buffer for mapping table
1142 * pdwSize [In/Out] length of output buffer
1143 * bOrder [In] whether to sort the table
1145 * RETURNS
1146 * Success: NO_ERROR
1147 * Failure: error code from winerror.h
1149 * NOTES
1150 * If pdwSize is less than required, the function will return
1151 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1152 * size.
1153 * If bOrder is true, the returned table will be sorted by the next hop and
1154 * an assortment of arbitrary parameters.
1156 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1158 DWORD ret;
1160 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1161 (DWORD)bOrder);
1162 if (!pdwSize)
1163 ret = ERROR_INVALID_PARAMETER;
1164 else {
1165 PMIB_IPADDRTABLE table;
1167 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1168 if (ret == NO_ERROR)
1170 ULONG size = sizeof(MIB_IPADDRTABLE) + (table->dwNumEntries - 1) *
1171 sizeof(MIB_IPADDRROW);
1173 if (!pIpAddrTable || *pdwSize < size) {
1174 *pdwSize = size;
1175 ret = ERROR_INSUFFICIENT_BUFFER;
1177 else {
1178 *pdwSize = size;
1179 memcpy(pIpAddrTable, table, sizeof(MIB_IPADDRTABLE) +
1180 (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW));
1181 if (bOrder)
1182 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1183 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1184 ret = NO_ERROR;
1186 HeapFree(GetProcessHeap(), 0, table);
1189 TRACE("returning %d\n", ret);
1190 return ret;
1194 /******************************************************************
1195 * GetIpForwardTable (IPHLPAPI.@)
1197 * Get the route table.
1199 * PARAMS
1200 * pIpForwardTable [Out] buffer for route table
1201 * pdwSize [In/Out] length of output buffer
1202 * bOrder [In] whether to sort the table
1204 * RETURNS
1205 * Success: NO_ERROR
1206 * Failure: error code from winerror.h
1208 * NOTES
1209 * If pdwSize is less than required, the function will return
1210 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1211 * size.
1212 * If bOrder is true, the returned table will be sorted by the next hop and
1213 * an assortment of arbitrary parameters.
1215 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1217 DWORD ret;
1219 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable,
1220 pdwSize, (DWORD)bOrder);
1221 if (!pdwSize)
1222 ret = ERROR_INVALID_PARAMETER;
1223 else {
1224 DWORD numRoutes = getNumRoutes();
1225 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE);
1227 if (numRoutes > 1)
1228 sizeNeeded += (numRoutes - 1) * sizeof(MIB_IPFORWARDROW);
1229 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1230 *pdwSize = sizeNeeded;
1231 ret = ERROR_INSUFFICIENT_BUFFER;
1233 else {
1234 PMIB_IPFORWARDTABLE table;
1236 ret = getRouteTable(&table, GetProcessHeap(), 0);
1237 if (!ret) {
1238 sizeNeeded = sizeof(MIB_IPFORWARDTABLE);
1239 if (table->dwNumEntries > 1)
1240 sizeNeeded += (table->dwNumEntries - 1) * sizeof(MIB_IPFORWARDROW);
1241 if (*pdwSize < sizeNeeded) {
1242 *pdwSize = sizeNeeded;
1243 ret = ERROR_INSUFFICIENT_BUFFER;
1245 else {
1246 *pdwSize = sizeNeeded;
1247 memcpy(pIpForwardTable, table, sizeNeeded);
1248 if (bOrder)
1249 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1250 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1251 ret = NO_ERROR;
1253 HeapFree(GetProcessHeap(), 0, table);
1257 TRACE("returning %d\n", ret);
1258 return ret;
1262 /******************************************************************
1263 * GetIpNetTable (IPHLPAPI.@)
1265 * Get the IP-to-physical address mapping table.
1267 * PARAMS
1268 * pIpNetTable [Out] buffer for mapping table
1269 * pdwSize [In/Out] length of output buffer
1270 * bOrder [In] whether to sort the table
1272 * RETURNS
1273 * Success: NO_ERROR
1274 * Failure: error code from winerror.h
1276 * NOTES
1277 * If pdwSize is less than required, the function will return
1278 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1279 * size.
1280 * If bOrder is true, the returned table will be sorted by IP address.
1282 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1284 DWORD ret;
1286 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize,
1287 (DWORD)bOrder);
1288 if (!pdwSize)
1289 ret = ERROR_INVALID_PARAMETER;
1290 else {
1291 DWORD numEntries = getNumArpEntries();
1292 ULONG size = sizeof(MIB_IPNETTABLE);
1294 if (numEntries > 1)
1295 size += (numEntries - 1) * sizeof(MIB_IPNETROW);
1296 if (!pIpNetTable || *pdwSize < size) {
1297 *pdwSize = size;
1298 ret = ERROR_INSUFFICIENT_BUFFER;
1300 else {
1301 PMIB_IPNETTABLE table;
1303 ret = getArpTable(&table, GetProcessHeap(), 0);
1304 if (!ret) {
1305 size = sizeof(MIB_IPNETTABLE);
1306 if (table->dwNumEntries > 1)
1307 size += (table->dwNumEntries - 1) * sizeof(MIB_IPNETROW);
1308 if (*pdwSize < size) {
1309 *pdwSize = size;
1310 ret = ERROR_INSUFFICIENT_BUFFER;
1312 else {
1313 *pdwSize = size;
1314 memcpy(pIpNetTable, table, size);
1315 if (bOrder)
1316 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1317 sizeof(MIB_IPNETROW), IpNetTableSorter);
1318 ret = NO_ERROR;
1320 HeapFree(GetProcessHeap(), 0, table);
1324 TRACE("returning %d\n", ret);
1325 return ret;
1329 /******************************************************************
1330 * GetIpStatistics (IPHLPAPI.@)
1332 * Get the IP statistics for the local computer.
1334 * PARAMS
1335 * pStats [Out] buffer for IP statistics
1337 * RETURNS
1338 * Success: NO_ERROR
1339 * Failure: error code from winerror.h
1341 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1343 DWORD ret;
1345 TRACE("pStats %p\n", pStats);
1346 ret = getIPStats(pStats);
1347 TRACE("returning %d\n", ret);
1348 return ret;
1352 /******************************************************************
1353 * GetNetworkParams (IPHLPAPI.@)
1355 * Get the network parameters for the local computer.
1357 * PARAMS
1358 * pFixedInfo [Out] buffer for network parameters
1359 * pOutBufLen [In/Out] length of output buffer
1361 * RETURNS
1362 * Success: NO_ERROR
1363 * Failure: error code from winerror.h
1365 * NOTES
1366 * If pOutBufLen is less than required, the function will return
1367 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1368 * size.
1370 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1372 DWORD ret, size;
1373 LONG regReturn;
1374 HKEY hKey;
1376 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1377 if (!pOutBufLen)
1378 return ERROR_INVALID_PARAMETER;
1380 initialise_resolver();
1381 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1382 sizeof(IP_ADDR_STRING) : 0);
1383 if (!pFixedInfo || *pOutBufLen < size) {
1384 *pOutBufLen = size;
1385 return ERROR_BUFFER_OVERFLOW;
1388 memset(pFixedInfo, 0, size);
1389 size = sizeof(pFixedInfo->HostName);
1390 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1391 size = sizeof(pFixedInfo->DomainName);
1392 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1393 if (_res.nscount > 0) {
1394 PIP_ADDR_STRING ptr;
1395 int i;
1397 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1398 i++, ptr = ptr->Next) {
1399 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1400 ptr->IpAddress.String);
1401 if (i == _res.nscount - 1)
1402 ptr->Next = NULL;
1403 else if (i == 0)
1404 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1405 else
1406 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1409 pFixedInfo->NodeType = HYBRID_NODETYPE;
1410 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1411 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1412 if (regReturn != ERROR_SUCCESS)
1413 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1414 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1415 &hKey);
1416 if (regReturn == ERROR_SUCCESS)
1418 DWORD size = sizeof(pFixedInfo->ScopeId);
1420 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1421 RegCloseKey(hKey);
1424 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1425 I suppose could also check for a listener on port 53 to set EnableDns */
1426 ret = NO_ERROR;
1427 TRACE("returning %d\n", ret);
1428 return ret;
1432 /******************************************************************
1433 * GetNumberOfInterfaces (IPHLPAPI.@)
1435 * Get the number of interfaces.
1437 * PARAMS
1438 * pdwNumIf [Out] number of interfaces
1440 * RETURNS
1441 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1443 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1445 DWORD ret;
1447 TRACE("pdwNumIf %p\n", pdwNumIf);
1448 if (!pdwNumIf)
1449 ret = ERROR_INVALID_PARAMETER;
1450 else {
1451 *pdwNumIf = getNumInterfaces();
1452 ret = NO_ERROR;
1454 TRACE("returning %d\n", ret);
1455 return ret;
1459 /******************************************************************
1460 * GetPerAdapterInfo (IPHLPAPI.@)
1462 * Get information about an adapter corresponding to an interface.
1464 * PARAMS
1465 * IfIndex [In] interface info
1466 * pPerAdapterInfo [Out] buffer for per adapter info
1467 * pOutBufLen [In/Out] length of output buffer
1469 * RETURNS
1470 * Success: NO_ERROR
1471 * Failure: error code from winerror.h
1473 * FIXME
1474 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1476 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1478 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1479 DWORD ret;
1481 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex,
1482 pPerAdapterInfo, pOutBufLen);
1483 if (!pOutBufLen)
1484 ret = ERROR_INVALID_PARAMETER;
1485 else if (!pPerAdapterInfo)
1487 *pOutBufLen = bytesNeeded;
1488 ret = NO_ERROR;
1490 else if (*pOutBufLen < bytesNeeded)
1492 *pOutBufLen = bytesNeeded;
1493 ret = ERROR_BUFFER_OVERFLOW;
1495 else
1497 memset(pPerAdapterInfo, 0, bytesNeeded);
1498 ret = NO_ERROR;
1500 return ret;
1504 /******************************************************************
1505 * GetRTTAndHopCount (IPHLPAPI.@)
1507 * Get round-trip time (RTT) and hop count.
1509 * PARAMS
1511 * DestIpAddress [In] destination address to get the info for
1512 * HopCount [Out] retrieved hop count
1513 * MaxHops [In] maximum hops to search for the destination
1514 * RTT [Out] RTT in milliseconds
1516 * RETURNS
1517 * Success: TRUE
1518 * Failure: FALSE
1520 * FIXME
1521 * Stub, returns FALSE.
1523 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1525 FIXME("(DestIpAddress 0x%08lx, HopCount %p, MaxHops %d, RTT %p): stub\n",
1526 DestIpAddress, HopCount, MaxHops, RTT);
1527 return FALSE;
1531 /******************************************************************
1532 * GetTcpStatistics (IPHLPAPI.@)
1534 * Get the TCP statistics for the local computer.
1536 * PARAMS
1537 * pStats [Out] buffer for TCP statistics
1539 * RETURNS
1540 * Success: NO_ERROR
1541 * Failure: error code from winerror.h
1543 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1545 DWORD ret;
1547 TRACE("pStats %p\n", pStats);
1548 ret = getTCPStats(pStats);
1549 TRACE("returning %d\n", ret);
1550 return ret;
1554 /******************************************************************
1555 * GetTcpTable (IPHLPAPI.@)
1557 * Get the table of active TCP connections.
1559 * PARAMS
1560 * pTcpTable [Out] buffer for TCP connections table
1561 * pdwSize [In/Out] length of output buffer
1562 * bOrder [In] whether to order the table
1564 * RETURNS
1565 * Success: NO_ERROR
1566 * Failure: error code from winerror.h
1568 * NOTES
1569 * If pdwSize is less than required, the function will return
1570 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1571 * the required byte size.
1572 * If bOrder is true, the returned table will be sorted, first by
1573 * local address and port number, then by remote address and port
1574 * number.
1576 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1578 DWORD ret;
1580 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize,
1581 (DWORD)bOrder);
1582 if (!pdwSize)
1583 ret = ERROR_INVALID_PARAMETER;
1584 else {
1585 DWORD numEntries = getNumTcpEntries();
1586 DWORD size = sizeof(MIB_TCPTABLE);
1588 if (numEntries > 1)
1589 size += (numEntries - 1) * sizeof(MIB_TCPROW);
1590 if (!pTcpTable || *pdwSize < size) {
1591 *pdwSize = size;
1592 ret = ERROR_INSUFFICIENT_BUFFER;
1594 else {
1595 ret = getTcpTable(&pTcpTable, numEntries, 0, 0);
1596 if (!ret) {
1597 size = sizeof(MIB_TCPTABLE);
1598 if (numEntries > 1)
1599 size += (numEntries - 1) * sizeof(MIB_TCPROW);
1600 *pdwSize = size;
1602 if (bOrder)
1603 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1604 sizeof(MIB_TCPROW), TcpTableSorter);
1605 ret = NO_ERROR;
1609 TRACE("returning %d\n", ret);
1610 return ret;
1614 /******************************************************************
1615 * GetUdpStatistics (IPHLPAPI.@)
1617 * Get the UDP statistics for the local computer.
1619 * PARAMS
1620 * pStats [Out] buffer for UDP statistics
1622 * RETURNS
1623 * Success: NO_ERROR
1624 * Failure: error code from winerror.h
1626 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1628 DWORD ret;
1630 TRACE("pStats %p\n", pStats);
1631 ret = getUDPStats(pStats);
1632 TRACE("returning %d\n", ret);
1633 return ret;
1637 /******************************************************************
1638 * GetUdpTable (IPHLPAPI.@)
1640 * Get a table of active UDP connections.
1642 * PARAMS
1643 * pUdpTable [Out] buffer for UDP connections table
1644 * pdwSize [In/Out] length of output buffer
1645 * bOrder [In] whether to order the table
1647 * RETURNS
1648 * Success: NO_ERROR
1649 * Failure: error code from winerror.h
1651 * NOTES
1652 * If pdwSize is less than required, the function will return
1653 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1654 * required byte size.
1655 * If bOrder is true, the returned table will be sorted, first by
1656 * local address, then by local port number.
1658 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1660 DWORD ret;
1662 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize,
1663 (DWORD)bOrder);
1664 if (!pdwSize)
1665 ret = ERROR_INVALID_PARAMETER;
1666 else {
1667 DWORD numEntries = getNumUdpEntries();
1668 DWORD size = sizeof(MIB_UDPTABLE) + (numEntries - 1) * sizeof(MIB_UDPROW);
1670 if (!pUdpTable || *pdwSize < size) {
1671 *pdwSize = size;
1672 ret = ERROR_INSUFFICIENT_BUFFER;
1674 else {
1675 PMIB_UDPTABLE table;
1677 ret = getUdpTable(&table, GetProcessHeap(), 0);
1678 if (!ret) {
1679 size = sizeof(MIB_UDPTABLE) + (table->dwNumEntries - 1) *
1680 sizeof(MIB_UDPROW);
1681 if (*pdwSize < size) {
1682 *pdwSize = size;
1683 ret = ERROR_INSUFFICIENT_BUFFER;
1685 else {
1686 *pdwSize = size;
1687 memcpy(pUdpTable, table, size);
1688 if (bOrder)
1689 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1690 sizeof(MIB_UDPROW), UdpTableSorter);
1691 ret = NO_ERROR;
1693 HeapFree(GetProcessHeap(), 0, table);
1695 else
1696 ret = ERROR_OUTOFMEMORY;
1699 TRACE("returning %d\n", ret);
1700 return ret;
1704 /******************************************************************
1705 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1707 * This is a Win98-only function to get information on "unidirectional"
1708 * adapters. Since this is pretty nonsensical in other contexts, it
1709 * never returns anything.
1711 * PARAMS
1712 * pIPIfInfo [Out] buffer for adapter infos
1713 * dwOutBufLen [Out] length of the output buffer
1715 * RETURNS
1716 * Success: NO_ERROR
1717 * Failure: error code from winerror.h
1719 * FIXME
1720 * Stub, returns ERROR_NOT_SUPPORTED.
1722 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1724 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1725 /* a unidirectional adapter?? not bloody likely! */
1726 return ERROR_NOT_SUPPORTED;
1730 /******************************************************************
1731 * IpReleaseAddress (IPHLPAPI.@)
1733 * Release an IP optained through DHCP,
1735 * PARAMS
1736 * AdapterInfo [In] adapter to release IP address
1738 * RETURNS
1739 * Success: NO_ERROR
1740 * Failure: error code from winerror.h
1742 * NOTES
1743 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1744 * this function does nothing.
1746 * FIXME
1747 * Stub, returns ERROR_NOT_SUPPORTED.
1749 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1751 TRACE("AdapterInfo %p\n", AdapterInfo);
1752 /* not a stub, never going to support this (and I never mark an adapter as
1753 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1754 return ERROR_NOT_SUPPORTED;
1758 /******************************************************************
1759 * IpRenewAddress (IPHLPAPI.@)
1761 * Renew an IP optained through DHCP.
1763 * PARAMS
1764 * AdapterInfo [In] adapter to renew IP address
1766 * RETURNS
1767 * Success: NO_ERROR
1768 * Failure: error code from winerror.h
1770 * NOTES
1771 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1772 * this function does nothing.
1774 * FIXME
1775 * Stub, returns ERROR_NOT_SUPPORTED.
1777 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1779 TRACE("AdapterInfo %p\n", AdapterInfo);
1780 /* not a stub, never going to support this (and I never mark an adapter as
1781 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1782 return ERROR_NOT_SUPPORTED;
1786 /******************************************************************
1787 * NotifyAddrChange (IPHLPAPI.@)
1789 * Notify caller whenever the ip-interface map is changed.
1791 * PARAMS
1792 * Handle [Out] handle useable in asynchronus notification
1793 * overlapped [In] overlapped structure that notifies the caller
1795 * RETURNS
1796 * Success: NO_ERROR
1797 * Failure: error code from winerror.h
1799 * FIXME
1800 * Stub, returns ERROR_NOT_SUPPORTED.
1802 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1804 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1805 return ERROR_NOT_SUPPORTED;
1809 /******************************************************************
1810 * NotifyRouteChange (IPHLPAPI.@)
1812 * Notify caller whenever the ip routing table is changed.
1814 * PARAMS
1815 * Handle [Out] handle useable in asynchronus notification
1816 * overlapped [In] overlapped structure that notifies the caller
1818 * RETURNS
1819 * Success: NO_ERROR
1820 * Failure: error code from winerror.h
1822 * FIXME
1823 * Stub, returns ERROR_NOT_SUPPORTED.
1825 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1827 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1828 return ERROR_NOT_SUPPORTED;
1832 /******************************************************************
1833 * SendARP (IPHLPAPI.@)
1835 * Send an ARP request.
1837 * PARAMS
1838 * DestIP [In] attempt to obtain this IP
1839 * SrcIP [In] optional sender IP address
1840 * pMacAddr [Out] buffer for the mac address
1841 * PhyAddrLen [In/Out] length of the output buffer
1843 * RETURNS
1844 * Success: NO_ERROR
1845 * Failure: error code from winerror.h
1847 * FIXME
1848 * Stub, returns ERROR_NOT_SUPPORTED.
1850 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1852 FIXME("(DestIP 0x%08lx, SrcIP 0x%08lx, pMacAddr %p, PhyAddrLen %p): stub\n",
1853 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1854 return ERROR_NOT_SUPPORTED;
1858 /******************************************************************
1859 * SetIfEntry (IPHLPAPI.@)
1861 * Set the administrative status of an interface.
1863 * PARAMS
1864 * pIfRow [In] dwAdminStatus member specifies the new status.
1866 * RETURNS
1867 * Success: NO_ERROR
1868 * Failure: error code from winerror.h
1870 * FIXME
1871 * Stub, returns ERROR_NOT_SUPPORTED.
1873 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1875 FIXME("(pIfRow %p): stub\n", pIfRow);
1876 /* this is supposed to set an interface administratively up or down.
1877 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1878 this sort of down is indistinguishable from other sorts of down (e.g. no
1879 link). */
1880 return ERROR_NOT_SUPPORTED;
1884 /******************************************************************
1885 * SetIpForwardEntry (IPHLPAPI.@)
1887 * Modify an existing route.
1889 * PARAMS
1890 * pRoute [In] route with the new information
1892 * RETURNS
1893 * Success: NO_ERROR
1894 * Failure: error code from winerror.h
1896 * FIXME
1897 * Stub, returns NO_ERROR.
1899 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1901 FIXME("(pRoute %p): stub\n", pRoute);
1902 /* this is to add a route entry, how's it distinguishable from
1903 CreateIpForwardEntry?
1904 could use SIOCADDRT, not sure I want to */
1905 return (DWORD) 0;
1909 /******************************************************************
1910 * SetIpNetEntry (IPHLPAPI.@)
1912 * Modify an existing ARP entry.
1914 * PARAMS
1915 * pArpEntry [In] ARP entry with the new information
1917 * RETURNS
1918 * Success: NO_ERROR
1919 * Failure: error code from winerror.h
1921 * FIXME
1922 * Stub, returns NO_ERROR.
1924 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1926 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1927 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1928 return (DWORD) 0;
1932 /******************************************************************
1933 * SetIpStatistics (IPHLPAPI.@)
1935 * Toggle IP forwarding and det the default TTL value.
1937 * PARAMS
1938 * pIpStats [In] IP statistics with the new information
1940 * RETURNS
1941 * Success: NO_ERROR
1942 * Failure: error code from winerror.h
1944 * FIXME
1945 * Stub, returns NO_ERROR.
1947 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1949 FIXME("(pIpStats %p): stub\n", pIpStats);
1950 return (DWORD) 0;
1954 /******************************************************************
1955 * SetIpTTL (IPHLPAPI.@)
1957 * Set the default TTL value.
1959 * PARAMS
1960 * nTTL [In] new TTL value
1962 * RETURNS
1963 * Success: NO_ERROR
1964 * Failure: error code from winerror.h
1966 * FIXME
1967 * Stub, returns NO_ERROR.
1969 DWORD WINAPI SetIpTTL(UINT nTTL)
1971 FIXME("(nTTL %d): stub\n", nTTL);
1972 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1973 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1974 return (DWORD) 0;
1978 /******************************************************************
1979 * SetTcpEntry (IPHLPAPI.@)
1981 * Set the state of a TCP connection.
1983 * PARAMS
1984 * pTcpRow [In] specifies connection with new state
1986 * RETURNS
1987 * Success: NO_ERROR
1988 * Failure: error code from winerror.h
1990 * FIXME
1991 * Stub, returns NO_ERROR.
1993 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1995 FIXME("(pTcpRow %p): stub\n", pTcpRow);
1996 return (DWORD) 0;
2000 /******************************************************************
2001 * UnenableRouter (IPHLPAPI.@)
2003 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2004 * if it reaches zero.
2006 * PARAMS
2007 * pOverlapped [In/Out] should be the same as in EnableRouter()
2008 * lpdwEnableCount [Out] optional, receives reference count
2010 * RETURNS
2011 * Success: NO_ERROR
2012 * Failure: error code from winerror.h
2014 * FIXME
2015 * Stub, returns ERROR_NOT_SUPPORTED.
2017 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2019 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2020 lpdwEnableCount);
2021 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2022 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2024 return ERROR_NOT_SUPPORTED;