iphlpapi: Reimplement GetTcpTable to avoid parsing the same information three times.
[wine/multimedia.git] / dlls / iphlpapi / iphlpapi_main.c
bloba3fc93ee69d033b8bfa16119800ba5142cc26bd6
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 #define USE_WS_PREFIX
43 #include "winsock2.h"
44 #include "iphlpapi.h"
45 #include "ifenum.h"
46 #include "ipstats.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
52 #ifndef INADDR_NONE
53 #define INADDR_NONE ~0UL
54 #endif
56 static int resolver_initialised;
58 /* call res_init() just once because of a bug in Mac OS X 10.4 */
59 static void initialise_resolver(void)
61 if (!resolver_initialised)
63 res_init();
64 resolver_initialised = 1;
68 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
70 switch (fdwReason) {
71 case DLL_PROCESS_ATTACH:
72 DisableThreadLibraryCalls( hinstDLL );
73 break;
75 case DLL_PROCESS_DETACH:
76 break;
78 return TRUE;
81 /******************************************************************
82 * AddIPAddress (IPHLPAPI.@)
84 * Add an IP address to an adapter.
86 * PARAMS
87 * Address [In] IP address to add to the adapter
88 * IpMask [In] subnet mask for the IP address
89 * IfIndex [In] adapter index to add the address
90 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
91 * NTEInstance [Out] NTE instance for the IP address
93 * RETURNS
94 * Success: NO_ERROR
95 * Failure: error code from winerror.h
97 * FIXME
98 * Stub. Currently returns ERROR_NOT_SUPPORTED.
100 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
102 FIXME(":stub\n");
103 return ERROR_NOT_SUPPORTED;
107 /******************************************************************
108 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
110 * Get table of local interfaces.
111 * Like GetIfTable(), but allocate the returned table from heap.
113 * PARAMS
114 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
115 * allocated and returned.
116 * bOrder [In] whether to sort the table
117 * heap [In] heap from which the table is allocated
118 * flags [In] flags to HeapAlloc
120 * RETURNS
121 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
122 * GetIfTable() returns otherwise.
124 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
125 BOOL bOrder, HANDLE heap, DWORD flags)
127 DWORD ret;
129 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
130 bOrder, heap, flags);
131 if (!ppIfTable)
132 ret = ERROR_INVALID_PARAMETER;
133 else {
134 DWORD dwSize = 0;
136 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
137 if (ret == ERROR_INSUFFICIENT_BUFFER) {
138 *ppIfTable = HeapAlloc(heap, flags, dwSize);
139 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
142 TRACE("returning %d\n", ret);
143 return ret;
147 static int IpAddrTableSorter(const void *a, const void *b)
149 int ret;
151 if (a && b)
152 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
153 else
154 ret = 0;
155 return ret;
159 /******************************************************************
160 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
162 * Get interface-to-IP address mapping table.
163 * Like GetIpAddrTable(), but allocate the returned table from heap.
165 * PARAMS
166 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
167 * allocated and returned.
168 * bOrder [In] whether to sort the table
169 * heap [In] heap from which the table is allocated
170 * flags [In] flags to HeapAlloc
172 * RETURNS
173 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
174 * failure, NO_ERROR on success.
176 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
177 BOOL bOrder, HANDLE heap, DWORD flags)
179 DWORD ret;
181 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
182 ppIpAddrTable, bOrder, heap, flags);
183 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
184 if (!ret && bOrder)
185 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
186 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
187 TRACE("returning %d\n", ret);
188 return ret;
192 static int IpForwardTableSorter(const void *a, const void *b)
194 int ret;
196 if (a && b) {
197 const MIB_IPFORWARDROW* rowA = (const MIB_IPFORWARDROW*)a;
198 const MIB_IPFORWARDROW* rowB = (const MIB_IPFORWARDROW*)b;
200 ret = rowA->dwForwardDest - rowB->dwForwardDest;
201 if (ret == 0) {
202 ret = rowA->dwForwardProto - rowB->dwForwardProto;
203 if (ret == 0) {
204 ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy;
205 if (ret == 0)
206 ret = rowA->dwForwardNextHop - rowB->dwForwardNextHop;
210 else
211 ret = 0;
212 return ret;
216 /******************************************************************
217 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
219 * Get the route table.
220 * Like GetIpForwardTable(), but allocate the returned table from heap.
222 * PARAMS
223 * ppIpForwardTable [Out] pointer into which the MIB_IPFORWARDTABLE is
224 * allocated and returned.
225 * bOrder [In] whether to sort the table
226 * heap [In] heap from which the table is allocated
227 * flags [In] flags to HeapAlloc
229 * RETURNS
230 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, other error codes
231 * on failure, NO_ERROR on success.
233 DWORD WINAPI AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE *
234 ppIpForwardTable, BOOL bOrder, HANDLE heap, DWORD flags)
236 DWORD ret;
238 TRACE("ppIpForwardTable %p, bOrder %d, heap %p, flags 0x%08x\n",
239 ppIpForwardTable, bOrder, heap, flags);
240 ret = getRouteTable(ppIpForwardTable, heap, flags);
241 if (!ret && bOrder)
242 qsort((*ppIpForwardTable)->table, (*ppIpForwardTable)->dwNumEntries,
243 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
244 TRACE("returning %d\n", ret);
245 return ret;
249 static int IpNetTableSorter(const void *a, const void *b)
251 int ret;
253 if (a && b)
254 ret = ((const MIB_IPNETROW*)a)->dwAddr - ((const MIB_IPNETROW*)b)->dwAddr;
255 else
256 ret = 0;
257 return ret;
261 /******************************************************************
262 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
264 * Get the IP-to-physical address mapping table.
265 * Like GetIpNetTable(), but allocate the returned table from heap.
267 * PARAMS
268 * ppIpNetTable [Out] pointer into which the MIB_IPNETTABLE is
269 * allocated and returned.
270 * bOrder [In] whether to sort the table
271 * heap [In] heap from which the table is allocated
272 * flags [In] flags to HeapAlloc
274 * RETURNS
275 * ERROR_INVALID_PARAMETER if ppIpNetTable is NULL, other error codes
276 * on failure, NO_ERROR on success.
278 DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable,
279 BOOL bOrder, HANDLE heap, DWORD flags)
281 DWORD ret;
283 TRACE("ppIpNetTable %p, bOrder %d, heap %p, flags 0x%08x\n",
284 ppIpNetTable, bOrder, heap, flags);
285 ret = getArpTable(ppIpNetTable, heap, flags);
286 if (!ret && bOrder)
287 qsort((*ppIpNetTable)->table, (*ppIpNetTable)->dwNumEntries,
288 sizeof(MIB_IPADDRROW), IpNetTableSorter);
289 TRACE("returning %d\n", ret);
290 return ret;
294 static int TcpTableSorter(const void *a, const void *b)
296 int ret;
298 if (a && b) {
299 const MIB_TCPROW* rowA = a;
300 const MIB_TCPROW* rowB = b;
302 ret = ntohl (rowA->dwLocalAddr) - ntohl (rowB->dwLocalAddr);
303 if (ret == 0) {
304 ret = ntohs ((unsigned short)rowA->dwLocalPort) -
305 ntohs ((unsigned short)rowB->dwLocalPort);
306 if (ret == 0) {
307 ret = ntohl (rowA->dwRemoteAddr) - ntohl (rowB->dwRemoteAddr);
308 if (ret == 0)
309 ret = ntohs ((unsigned short)rowA->dwRemotePort) -
310 ntohs ((unsigned short)rowB->dwRemotePort);
314 else
315 ret = 0;
316 return ret;
320 /******************************************************************
321 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
323 * Get the TCP connection table.
324 * Like GetTcpTable(), but allocate the returned table from heap.
326 * PARAMS
327 * ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
328 * allocated and returned.
329 * bOrder [In] whether to sort the table
330 * heap [In] heap from which the table is allocated
331 * flags [In] flags to HeapAlloc
333 * RETURNS
334 * ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
335 * returns otherwise.
337 DWORD WINAPI AllocateAndGetTcpTableFromStack(PMIB_TCPTABLE *ppTcpTable,
338 BOOL bOrder, HANDLE heap, DWORD flags)
340 DWORD ret;
342 TRACE("ppTcpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
343 ppTcpTable, bOrder, heap, flags);
345 *ppTcpTable = NULL;
346 ret = getTcpTable(ppTcpTable, heap, flags);
347 if (!ret && bOrder)
348 qsort((*ppTcpTable)->table, (*ppTcpTable)->dwNumEntries,
349 sizeof(MIB_TCPROW), TcpTableSorter);
350 TRACE("returning %d\n", ret);
351 return ret;
355 static int UdpTableSorter(const void *a, const void *b)
357 int ret;
359 if (a && b) {
360 const MIB_UDPROW* rowA = (const MIB_UDPROW*)a;
361 const MIB_UDPROW* rowB = (const MIB_UDPROW*)b;
363 ret = rowA->dwLocalAddr - rowB->dwLocalAddr;
364 if (ret == 0)
365 ret = rowA->dwLocalPort - rowB->dwLocalPort;
367 else
368 ret = 0;
369 return ret;
373 /******************************************************************
374 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
376 * Get the UDP listener table.
377 * Like GetUdpTable(), but allocate the returned table from heap.
379 * PARAMS
380 * ppUdpTable [Out] pointer into which the MIB_UDPTABLE is
381 * allocated and returned.
382 * bOrder [In] whether to sort the table
383 * heap [In] heap from which the table is allocated
384 * flags [In] flags to HeapAlloc
386 * RETURNS
387 * ERROR_INVALID_PARAMETER if ppUdpTable is NULL, whatever GetUdpTable()
388 * returns otherwise.
390 DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable,
391 BOOL bOrder, HANDLE heap, DWORD flags)
393 DWORD ret;
395 TRACE("ppUdpTable %p, bOrder %d, heap %p, flags 0x%08x\n",
396 ppUdpTable, bOrder, heap, flags);
397 ret = getUdpTable(ppUdpTable, heap, flags);
398 if (!ret && bOrder)
399 qsort((*ppUdpTable)->table, (*ppUdpTable)->dwNumEntries,
400 sizeof(MIB_UDPROW), UdpTableSorter);
401 TRACE("returning %d\n", ret);
402 return ret;
406 /******************************************************************
407 * CreateIpForwardEntry (IPHLPAPI.@)
409 * Create a route in the local computer's IP table.
411 * PARAMS
412 * pRoute [In] new route information
414 * RETURNS
415 * Success: NO_ERROR
416 * Failure: error code from winerror.h
418 * FIXME
419 * Stub, always returns NO_ERROR.
421 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
423 FIXME("(pRoute %p): stub\n", pRoute);
424 /* could use SIOCADDRT, not sure I want to */
425 return 0;
429 /******************************************************************
430 * CreateIpNetEntry (IPHLPAPI.@)
432 * Create entry in the ARP table.
434 * PARAMS
435 * pArpEntry [In] new ARP entry
437 * RETURNS
438 * Success: NO_ERROR
439 * Failure: error code from winerror.h
441 * FIXME
442 * Stub, always returns NO_ERROR.
444 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
446 FIXME("(pArpEntry %p)\n", pArpEntry);
447 /* could use SIOCSARP on systems that support it, not sure I want to */
448 return 0;
452 /******************************************************************
453 * CreateProxyArpEntry (IPHLPAPI.@)
455 * Create a Proxy ARP (PARP) entry for an IP address.
457 * PARAMS
458 * dwAddress [In] IP address for which this computer acts as a proxy.
459 * dwMask [In] subnet mask for dwAddress
460 * dwIfIndex [In] interface index
462 * RETURNS
463 * Success: NO_ERROR
464 * Failure: error code from winerror.h
466 * FIXME
467 * Stub, returns ERROR_NOT_SUPPORTED.
469 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
471 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
472 dwAddress, dwMask, dwIfIndex);
473 return ERROR_NOT_SUPPORTED;
477 /******************************************************************
478 * DeleteIPAddress (IPHLPAPI.@)
480 * Delete an IP address added with AddIPAddress().
482 * PARAMS
483 * NTEContext [In] NTE context from AddIPAddress();
485 * RETURNS
486 * Success: NO_ERROR
487 * Failure: error code from winerror.h
489 * FIXME
490 * Stub, returns ERROR_NOT_SUPPORTED.
492 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
494 FIXME("(NTEContext %d): stub\n", NTEContext);
495 return ERROR_NOT_SUPPORTED;
499 /******************************************************************
500 * DeleteIpForwardEntry (IPHLPAPI.@)
502 * Delete a route.
504 * PARAMS
505 * pRoute [In] route to delete
507 * RETURNS
508 * Success: NO_ERROR
509 * Failure: error code from winerror.h
511 * FIXME
512 * Stub, returns NO_ERROR.
514 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
516 FIXME("(pRoute %p): stub\n", pRoute);
517 /* could use SIOCDELRT, not sure I want to */
518 return 0;
522 /******************************************************************
523 * DeleteIpNetEntry (IPHLPAPI.@)
525 * Delete an ARP entry.
527 * PARAMS
528 * pArpEntry [In] ARP entry to delete
530 * RETURNS
531 * Success: NO_ERROR
532 * Failure: error code from winerror.h
534 * FIXME
535 * Stub, returns NO_ERROR.
537 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
539 FIXME("(pArpEntry %p): stub\n", pArpEntry);
540 /* could use SIOCDARP on systems that support it, not sure I want to */
541 return 0;
545 /******************************************************************
546 * DeleteProxyArpEntry (IPHLPAPI.@)
548 * Delete a Proxy ARP entry.
550 * PARAMS
551 * dwAddress [In] IP address for which this computer acts as a proxy.
552 * dwMask [In] subnet mask for dwAddress
553 * dwIfIndex [In] interface index
555 * RETURNS
556 * Success: NO_ERROR
557 * Failure: error code from winerror.h
559 * FIXME
560 * Stub, returns ERROR_NOT_SUPPORTED.
562 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
564 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
565 dwAddress, dwMask, dwIfIndex);
566 return ERROR_NOT_SUPPORTED;
570 /******************************************************************
571 * EnableRouter (IPHLPAPI.@)
573 * Turn on ip forwarding.
575 * PARAMS
576 * pHandle [In/Out]
577 * pOverlapped [In/Out] hEvent member should contain a valid handle.
579 * RETURNS
580 * Success: ERROR_IO_PENDING
581 * Failure: error code from winerror.h
583 * FIXME
584 * Stub, returns ERROR_NOT_SUPPORTED.
586 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
588 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
589 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
590 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
592 return ERROR_NOT_SUPPORTED;
596 /******************************************************************
597 * FlushIpNetTable (IPHLPAPI.@)
599 * Delete all ARP entries of an interface
601 * PARAMS
602 * dwIfIndex [In] interface index
604 * RETURNS
605 * Success: NO_ERROR
606 * Failure: error code from winerror.h
608 * FIXME
609 * Stub, returns ERROR_NOT_SUPPORTED.
611 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
613 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
614 /* this flushes the arp cache of the given index */
615 return ERROR_NOT_SUPPORTED;
619 /******************************************************************
620 * GetAdapterIndex (IPHLPAPI.@)
622 * Get interface index from its name.
624 * PARAMS
625 * AdapterName [In] unicode string with the adapter name
626 * IfIndex [Out] returns found interface index
628 * RETURNS
629 * Success: NO_ERROR
630 * Failure: error code from winerror.h
632 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
634 char adapterName[MAX_ADAPTER_NAME];
635 unsigned int i;
636 DWORD ret;
638 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
639 /* The adapter name is guaranteed not to have any unicode characters, so
640 * this translation is never lossy */
641 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
642 adapterName[i] = (char)AdapterName[i];
643 adapterName[i] = '\0';
644 ret = getInterfaceIndexByName(adapterName, IfIndex);
645 TRACE("returning %d\n", ret);
646 return ret;
650 /******************************************************************
651 * GetAdaptersInfo (IPHLPAPI.@)
653 * Get information about adapters.
655 * PARAMS
656 * pAdapterInfo [Out] buffer for adapter infos
657 * pOutBufLen [In] length of output buffer
659 * RETURNS
660 * Success: NO_ERROR
661 * Failure: error code from winerror.h
663 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
665 DWORD ret;
667 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
668 if (!pOutBufLen)
669 ret = ERROR_INVALID_PARAMETER;
670 else {
671 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
673 if (numNonLoopbackInterfaces > 0) {
674 DWORD numIPAddresses = getNumIPAddresses();
675 ULONG size;
677 /* This may slightly overestimate the amount of space needed, because
678 * the IP addresses include the loopback address, but it's easier
679 * to make sure there's more than enough space than to make sure there's
680 * precisely enough space.
682 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
683 size += numIPAddresses * sizeof(IP_ADDR_STRING);
684 if (!pAdapterInfo || *pOutBufLen < size) {
685 *pOutBufLen = size;
686 ret = ERROR_BUFFER_OVERFLOW;
688 else {
689 InterfaceIndexTable *table = NULL;
690 PMIB_IPADDRTABLE ipAddrTable = NULL;
691 PMIB_IPFORWARDTABLE routeTable = NULL;
693 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
694 if (!ret)
695 ret = getRouteTable(&routeTable, GetProcessHeap(), 0);
696 if (!ret)
697 table = getNonLoopbackInterfaceIndexTable();
698 if (table) {
699 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
700 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
701 if (*pOutBufLen < size) {
702 *pOutBufLen = size;
703 ret = ERROR_INSUFFICIENT_BUFFER;
705 else {
706 DWORD ndx;
707 HKEY hKey;
708 BOOL winsEnabled = FALSE;
709 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
710 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
711 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
713 memset(pAdapterInfo, 0, size);
714 /* @@ Wine registry key: HKCU\Software\Wine\Network */
715 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
716 &hKey) == ERROR_SUCCESS) {
717 DWORD size = sizeof(primaryWINS.String);
718 unsigned long addr;
720 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
721 (LPBYTE)primaryWINS.String, &size);
722 addr = inet_addr(primaryWINS.String);
723 if (addr != INADDR_NONE && addr != INADDR_ANY)
724 winsEnabled = TRUE;
725 size = sizeof(secondaryWINS.String);
726 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
727 (LPBYTE)secondaryWINS.String, &size);
728 addr = inet_addr(secondaryWINS.String);
729 if (addr != INADDR_NONE && addr != INADDR_ANY)
730 winsEnabled = TRUE;
731 RegCloseKey(hKey);
733 for (ndx = 0; ndx < table->numIndexes; ndx++) {
734 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
735 DWORD i;
736 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
737 BOOL firstIPAddr = TRUE;
739 /* on Win98 this is left empty, but whatever */
740 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
741 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
742 ptr->AddressLength = sizeof(ptr->Address);
743 getInterfacePhysicalByIndex(table->indexes[ndx],
744 &ptr->AddressLength, ptr->Address, &ptr->Type);
745 ptr->Index = table->indexes[ndx];
746 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
747 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
748 if (firstIPAddr) {
749 toIPAddressString(ipAddrTable->table[i].dwAddr,
750 ptr->IpAddressList.IpAddress.String);
751 toIPAddressString(ipAddrTable->table[i].dwMask,
752 ptr->IpAddressList.IpMask.String);
753 firstIPAddr = FALSE;
755 else {
756 currentIPAddr->Next = nextIPAddr;
757 currentIPAddr = nextIPAddr;
758 toIPAddressString(ipAddrTable->table[i].dwAddr,
759 currentIPAddr->IpAddress.String);
760 toIPAddressString(ipAddrTable->table[i].dwMask,
761 currentIPAddr->IpMask.String);
762 nextIPAddr++;
766 /* Find first router through this interface, which we'll assume
767 * is the default gateway for this adapter */
768 for (i = 0; i < routeTable->dwNumEntries; i++)
769 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
770 && routeTable->table[i].dwForwardType ==
771 MIB_IPROUTE_TYPE_INDIRECT)
772 toIPAddressString(routeTable->table[i].dwForwardNextHop,
773 ptr->GatewayList.IpAddress.String);
774 if (winsEnabled) {
775 ptr->HaveWins = TRUE;
776 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
777 primaryWINS.String, sizeof(primaryWINS.String));
778 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
779 secondaryWINS.String, sizeof(secondaryWINS.String));
781 if (ndx < table->numIndexes - 1)
782 ptr->Next = &pAdapterInfo[ndx + 1];
783 else
784 ptr->Next = NULL;
786 ret = NO_ERROR;
788 HeapFree(GetProcessHeap(), 0, table);
790 else
791 ret = ERROR_OUTOFMEMORY;
792 HeapFree(GetProcessHeap(), 0, routeTable);
793 HeapFree(GetProcessHeap(), 0, ipAddrTable);
796 else
797 ret = ERROR_NO_DATA;
799 TRACE("returning %d\n", ret);
800 return ret;
804 /******************************************************************
805 * GetBestInterface (IPHLPAPI.@)
807 * Get the interface, with the best route for the given IP address.
809 * PARAMS
810 * dwDestAddr [In] IP address to search the interface for
811 * pdwBestIfIndex [Out] found best interface
813 * RETURNS
814 * Success: NO_ERROR
815 * Failure: error code from winerror.h
817 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
819 struct WS_sockaddr_in sa_in;
820 memset(&sa_in, 0, sizeof(sa_in));
821 sa_in.sin_family = AF_INET;
822 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
823 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
826 /******************************************************************
827 * GetBestInterfaceEx (IPHLPAPI.@)
829 * Get the interface, with the best route for the given IP address.
831 * PARAMS
832 * dwDestAddr [In] IP address to search the interface for
833 * pdwBestIfIndex [Out] found best interface
835 * RETURNS
836 * Success: NO_ERROR
837 * Failure: error code from winerror.h
839 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
841 DWORD ret;
843 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
844 if (!pDestAddr || !pdwBestIfIndex)
845 ret = ERROR_INVALID_PARAMETER;
846 else {
847 MIB_IPFORWARDROW ipRow;
849 if (pDestAddr->sa_family == AF_INET) {
850 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
851 if (ret == ERROR_SUCCESS)
852 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
853 } else {
854 FIXME("address family %d not supported\n", pDestAddr->sa_family);
855 ret = ERROR_NOT_SUPPORTED;
858 TRACE("returning %d\n", ret);
859 return ret;
863 /******************************************************************
864 * GetBestRoute (IPHLPAPI.@)
866 * Get the best route for the given IP address.
868 * PARAMS
869 * dwDestAddr [In] IP address to search the best route for
870 * dwSourceAddr [In] optional source IP address
871 * pBestRoute [Out] found best route
873 * RETURNS
874 * Success: NO_ERROR
875 * Failure: error code from winerror.h
877 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
879 PMIB_IPFORWARDTABLE table;
880 DWORD ret;
882 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
883 dwSourceAddr, pBestRoute);
884 if (!pBestRoute)
885 return ERROR_INVALID_PARAMETER;
887 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
888 if (!ret) {
889 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
891 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
892 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
893 (dwDestAddr & table->table[ndx].dwForwardMask) ==
894 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
895 DWORD numShifts, mask;
897 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
898 mask && !(mask & 1); mask >>= 1, numShifts++)
900 if (numShifts > matchedBits) {
901 matchedBits = numShifts;
902 matchedNdx = ndx;
904 else if (!matchedBits && table->table[ndx].dwForwardType ==
905 MIB_IPROUTE_TYPE_INDIRECT) {
906 /* default to a default gateway */
907 matchedNdx = ndx;
911 if (matchedNdx < table->dwNumEntries) {
912 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
913 ret = ERROR_SUCCESS;
915 else {
916 /* No route matches, which can happen if there's no default route. */
917 ret = ERROR_HOST_UNREACHABLE;
919 HeapFree(GetProcessHeap(), 0, table);
921 TRACE("returning %d\n", ret);
922 return ret;
926 /******************************************************************
927 * GetFriendlyIfIndex (IPHLPAPI.@)
929 * Get a "friendly" version of IfIndex, which is one that doesn't
930 * have the top byte set. Doesn't validate whether IfIndex is a valid
931 * adapter index.
933 * PARAMS
934 * IfIndex [In] interface index to get the friendly one for
936 * RETURNS
937 * A friendly version of IfIndex.
939 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
941 /* windows doesn't validate these, either, just makes sure the top byte is
942 cleared. I assume my ifenum module never gives an index with the top
943 byte set. */
944 TRACE("returning %d\n", IfIndex);
945 return IfIndex;
949 /******************************************************************
950 * GetIcmpStatistics (IPHLPAPI.@)
952 * Get the ICMP statistics for the local computer.
954 * PARAMS
955 * pStats [Out] buffer for ICMP statistics
957 * RETURNS
958 * Success: NO_ERROR
959 * Failure: error code from winerror.h
961 DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
963 DWORD ret;
965 TRACE("pStats %p\n", pStats);
966 ret = getICMPStats(pStats);
967 TRACE("returning %d\n", ret);
968 return ret;
972 /******************************************************************
973 * GetIfEntry (IPHLPAPI.@)
975 * Get information about an interface.
977 * PARAMS
978 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
979 * Out: interface information
981 * RETURNS
982 * Success: NO_ERROR
983 * Failure: error code from winerror.h
985 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
987 DWORD ret;
988 char nameBuf[MAX_ADAPTER_NAME];
989 char *name;
991 TRACE("pIfRow %p\n", pIfRow);
992 if (!pIfRow)
993 return ERROR_INVALID_PARAMETER;
995 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
996 if (name) {
997 ret = getInterfaceEntryByName(name, pIfRow);
998 if (ret == NO_ERROR)
999 ret = getInterfaceStatsByName(name, pIfRow);
1001 else
1002 ret = ERROR_INVALID_DATA;
1003 TRACE("returning %d\n", ret);
1004 return ret;
1008 static int IfTableSorter(const void *a, const void *b)
1010 int ret;
1012 if (a && b)
1013 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1014 else
1015 ret = 0;
1016 return ret;
1020 /******************************************************************
1021 * GetIfTable (IPHLPAPI.@)
1023 * Get a table of local interfaces.
1025 * PARAMS
1026 * pIfTable [Out] buffer for local interfaces table
1027 * pdwSize [In/Out] length of output buffer
1028 * bOrder [In] whether to sort the table
1030 * RETURNS
1031 * Success: NO_ERROR
1032 * Failure: error code from winerror.h
1034 * NOTES
1035 * If pdwSize is less than required, the function will return
1036 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1037 * size.
1038 * If bOrder is true, the returned table will be sorted by interface index.
1040 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1042 DWORD ret;
1044 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1045 (DWORD)bOrder);
1046 if (!pdwSize)
1047 ret = ERROR_INVALID_PARAMETER;
1048 else {
1049 DWORD numInterfaces = getNumInterfaces();
1050 ULONG size = sizeof(MIB_IFTABLE);
1052 if (numInterfaces > 1)
1053 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1054 if (!pIfTable || *pdwSize < size) {
1055 *pdwSize = size;
1056 ret = ERROR_INSUFFICIENT_BUFFER;
1058 else {
1059 InterfaceIndexTable *table = getInterfaceIndexTable();
1061 if (table) {
1062 size = sizeof(MIB_IFTABLE);
1063 if (table->numIndexes > 1)
1064 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1065 if (*pdwSize < size) {
1066 *pdwSize = size;
1067 ret = ERROR_INSUFFICIENT_BUFFER;
1069 else {
1070 DWORD ndx;
1072 *pdwSize = size;
1073 pIfTable->dwNumEntries = 0;
1074 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1075 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1076 GetIfEntry(&pIfTable->table[ndx]);
1077 pIfTable->dwNumEntries++;
1079 if (bOrder)
1080 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1081 IfTableSorter);
1082 ret = NO_ERROR;
1084 HeapFree(GetProcessHeap(), 0, table);
1086 else
1087 ret = ERROR_OUTOFMEMORY;
1090 TRACE("returning %d\n", ret);
1091 return ret;
1095 /******************************************************************
1096 * GetInterfaceInfo (IPHLPAPI.@)
1098 * Get a list of network interface adapters.
1100 * PARAMS
1101 * pIfTable [Out] buffer for interface adapters
1102 * dwOutBufLen [Out] if buffer is too small, returns required size
1104 * RETURNS
1105 * Success: NO_ERROR
1106 * Failure: error code from winerror.h
1108 * BUGS
1109 * MSDN states this should return non-loopback interfaces only.
1111 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1113 DWORD ret;
1115 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1116 if (!dwOutBufLen)
1117 ret = ERROR_INVALID_PARAMETER;
1118 else {
1119 DWORD numInterfaces = getNumInterfaces();
1120 ULONG size = sizeof(IP_INTERFACE_INFO);
1122 if (numInterfaces > 1)
1123 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1124 if (!pIfTable || *dwOutBufLen < size) {
1125 *dwOutBufLen = size;
1126 ret = ERROR_INSUFFICIENT_BUFFER;
1128 else {
1129 InterfaceIndexTable *table = getInterfaceIndexTable();
1131 if (table) {
1132 size = sizeof(IP_INTERFACE_INFO);
1133 if (table->numIndexes > 1)
1134 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1135 if (*dwOutBufLen < size) {
1136 *dwOutBufLen = size;
1137 ret = ERROR_INSUFFICIENT_BUFFER;
1139 else {
1140 DWORD ndx;
1141 char nameBuf[MAX_ADAPTER_NAME];
1143 *dwOutBufLen = size;
1144 pIfTable->NumAdapters = 0;
1145 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1146 const char *walker, *name;
1147 WCHAR *assigner;
1149 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1150 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1151 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1152 walker && *walker &&
1153 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1154 walker++, assigner++)
1155 *assigner = *walker;
1156 *assigner = 0;
1157 pIfTable->NumAdapters++;
1159 ret = NO_ERROR;
1161 HeapFree(GetProcessHeap(), 0, table);
1163 else
1164 ret = ERROR_OUTOFMEMORY;
1167 TRACE("returning %d\n", ret);
1168 return ret;
1172 /******************************************************************
1173 * GetIpAddrTable (IPHLPAPI.@)
1175 * Get interface-to-IP address mapping table.
1177 * PARAMS
1178 * pIpAddrTable [Out] buffer for mapping table
1179 * pdwSize [In/Out] length of output buffer
1180 * bOrder [In] whether to sort the table
1182 * RETURNS
1183 * Success: NO_ERROR
1184 * Failure: error code from winerror.h
1186 * NOTES
1187 * If pdwSize is less than required, the function will return
1188 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1189 * size.
1190 * If bOrder is true, the returned table will be sorted by the next hop and
1191 * an assortment of arbitrary parameters.
1193 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1195 DWORD ret;
1197 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1198 (DWORD)bOrder);
1199 if (!pdwSize)
1200 ret = ERROR_INVALID_PARAMETER;
1201 else {
1202 PMIB_IPADDRTABLE table;
1204 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1205 if (ret == NO_ERROR)
1207 ULONG size = sizeof(MIB_IPADDRTABLE);
1209 if (table->dwNumEntries > 1)
1210 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1211 if (!pIpAddrTable || *pdwSize < size) {
1212 *pdwSize = size;
1213 ret = ERROR_INSUFFICIENT_BUFFER;
1215 else {
1216 *pdwSize = size;
1217 memcpy(pIpAddrTable, table, size);
1218 if (bOrder)
1219 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1220 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1221 ret = NO_ERROR;
1223 HeapFree(GetProcessHeap(), 0, table);
1226 TRACE("returning %d\n", ret);
1227 return ret;
1231 /******************************************************************
1232 * GetIpForwardTable (IPHLPAPI.@)
1234 * Get the route table.
1236 * PARAMS
1237 * pIpForwardTable [Out] buffer for route table
1238 * pdwSize [In/Out] length of output buffer
1239 * bOrder [In] whether to sort the table
1241 * RETURNS
1242 * Success: NO_ERROR
1243 * Failure: error code from winerror.h
1245 * NOTES
1246 * If pdwSize is less than required, the function will return
1247 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1248 * size.
1249 * If bOrder is true, the returned table will be sorted by the next hop and
1250 * an assortment of arbitrary parameters.
1252 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1254 DWORD ret;
1256 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable,
1257 pdwSize, (DWORD)bOrder);
1258 if (!pdwSize)
1259 ret = ERROR_INVALID_PARAMETER;
1260 else {
1261 DWORD numRoutes = getNumRoutes();
1262 ULONG sizeNeeded = sizeof(MIB_IPFORWARDTABLE);
1264 if (numRoutes > 1)
1265 sizeNeeded += (numRoutes - 1) * sizeof(MIB_IPFORWARDROW);
1266 if (!pIpForwardTable || *pdwSize < sizeNeeded) {
1267 *pdwSize = sizeNeeded;
1268 ret = ERROR_INSUFFICIENT_BUFFER;
1270 else {
1271 PMIB_IPFORWARDTABLE table;
1273 ret = getRouteTable(&table, GetProcessHeap(), 0);
1274 if (!ret) {
1275 sizeNeeded = sizeof(MIB_IPFORWARDTABLE);
1276 if (table->dwNumEntries > 1)
1277 sizeNeeded += (table->dwNumEntries - 1) * sizeof(MIB_IPFORWARDROW);
1278 if (*pdwSize < sizeNeeded) {
1279 *pdwSize = sizeNeeded;
1280 ret = ERROR_INSUFFICIENT_BUFFER;
1282 else {
1283 *pdwSize = sizeNeeded;
1284 memcpy(pIpForwardTable, table, sizeNeeded);
1285 if (bOrder)
1286 qsort(pIpForwardTable->table, pIpForwardTable->dwNumEntries,
1287 sizeof(MIB_IPFORWARDROW), IpForwardTableSorter);
1288 ret = NO_ERROR;
1290 HeapFree(GetProcessHeap(), 0, table);
1294 TRACE("returning %d\n", ret);
1295 return ret;
1299 /******************************************************************
1300 * GetIpNetTable (IPHLPAPI.@)
1302 * Get the IP-to-physical address mapping table.
1304 * PARAMS
1305 * pIpNetTable [Out] buffer for mapping table
1306 * pdwSize [In/Out] length of output buffer
1307 * bOrder [In] whether to sort the table
1309 * RETURNS
1310 * Success: NO_ERROR
1311 * Failure: error code from winerror.h
1313 * NOTES
1314 * If pdwSize is less than required, the function will return
1315 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1316 * size.
1317 * If bOrder is true, the returned table will be sorted by IP address.
1319 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1321 DWORD ret;
1323 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize,
1324 (DWORD)bOrder);
1325 if (!pdwSize)
1326 ret = ERROR_INVALID_PARAMETER;
1327 else {
1328 DWORD numEntries = getNumArpEntries();
1329 ULONG size = sizeof(MIB_IPNETTABLE);
1331 if (numEntries > 1)
1332 size += (numEntries - 1) * sizeof(MIB_IPNETROW);
1333 if (!pIpNetTable || *pdwSize < size) {
1334 *pdwSize = size;
1335 ret = ERROR_INSUFFICIENT_BUFFER;
1337 else {
1338 PMIB_IPNETTABLE table;
1340 ret = getArpTable(&table, GetProcessHeap(), 0);
1341 if (!ret) {
1342 size = sizeof(MIB_IPNETTABLE);
1343 if (table->dwNumEntries > 1)
1344 size += (table->dwNumEntries - 1) * sizeof(MIB_IPNETROW);
1345 if (*pdwSize < size) {
1346 *pdwSize = size;
1347 ret = ERROR_INSUFFICIENT_BUFFER;
1349 else {
1350 *pdwSize = size;
1351 memcpy(pIpNetTable, table, size);
1352 if (bOrder)
1353 qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
1354 sizeof(MIB_IPNETROW), IpNetTableSorter);
1355 ret = NO_ERROR;
1357 HeapFree(GetProcessHeap(), 0, table);
1361 TRACE("returning %d\n", ret);
1362 return ret;
1366 /******************************************************************
1367 * GetIpStatistics (IPHLPAPI.@)
1369 * Get the IP statistics for the local computer.
1371 * PARAMS
1372 * pStats [Out] buffer for IP statistics
1374 * RETURNS
1375 * Success: NO_ERROR
1376 * Failure: error code from winerror.h
1378 DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
1380 DWORD ret;
1382 TRACE("pStats %p\n", pStats);
1383 ret = getIPStats(pStats);
1384 TRACE("returning %d\n", ret);
1385 return ret;
1389 /******************************************************************
1390 * GetNetworkParams (IPHLPAPI.@)
1392 * Get the network parameters for the local computer.
1394 * PARAMS
1395 * pFixedInfo [Out] buffer for network parameters
1396 * pOutBufLen [In/Out] length of output buffer
1398 * RETURNS
1399 * Success: NO_ERROR
1400 * Failure: error code from winerror.h
1402 * NOTES
1403 * If pOutBufLen is less than required, the function will return
1404 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1405 * size.
1407 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1409 DWORD ret, size;
1410 LONG regReturn;
1411 HKEY hKey;
1413 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1414 if (!pOutBufLen)
1415 return ERROR_INVALID_PARAMETER;
1417 initialise_resolver();
1418 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1419 sizeof(IP_ADDR_STRING) : 0);
1420 if (!pFixedInfo || *pOutBufLen < size) {
1421 *pOutBufLen = size;
1422 return ERROR_BUFFER_OVERFLOW;
1425 memset(pFixedInfo, 0, size);
1426 size = sizeof(pFixedInfo->HostName);
1427 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1428 size = sizeof(pFixedInfo->DomainName);
1429 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1430 if (_res.nscount > 0) {
1431 PIP_ADDR_STRING ptr;
1432 int i;
1434 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1435 i++, ptr = ptr->Next) {
1436 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1437 ptr->IpAddress.String);
1438 if (i == _res.nscount - 1)
1439 ptr->Next = NULL;
1440 else if (i == 0)
1441 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1442 else
1443 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1446 pFixedInfo->NodeType = HYBRID_NODETYPE;
1447 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1448 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1449 if (regReturn != ERROR_SUCCESS)
1450 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1451 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1452 &hKey);
1453 if (regReturn == ERROR_SUCCESS)
1455 DWORD size = sizeof(pFixedInfo->ScopeId);
1457 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1458 RegCloseKey(hKey);
1461 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1462 I suppose could also check for a listener on port 53 to set EnableDns */
1463 ret = NO_ERROR;
1464 TRACE("returning %d\n", ret);
1465 return ret;
1469 /******************************************************************
1470 * GetNumberOfInterfaces (IPHLPAPI.@)
1472 * Get the number of interfaces.
1474 * PARAMS
1475 * pdwNumIf [Out] number of interfaces
1477 * RETURNS
1478 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1480 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1482 DWORD ret;
1484 TRACE("pdwNumIf %p\n", pdwNumIf);
1485 if (!pdwNumIf)
1486 ret = ERROR_INVALID_PARAMETER;
1487 else {
1488 *pdwNumIf = getNumInterfaces();
1489 ret = NO_ERROR;
1491 TRACE("returning %d\n", ret);
1492 return ret;
1496 /******************************************************************
1497 * GetPerAdapterInfo (IPHLPAPI.@)
1499 * Get information about an adapter corresponding to an interface.
1501 * PARAMS
1502 * IfIndex [In] interface info
1503 * pPerAdapterInfo [Out] buffer for per adapter info
1504 * pOutBufLen [In/Out] length of output buffer
1506 * RETURNS
1507 * Success: NO_ERROR
1508 * Failure: error code from winerror.h
1510 * FIXME
1511 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1513 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1515 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1517 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1519 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1521 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1523 *pOutBufLen = bytesNeeded;
1524 return ERROR_BUFFER_OVERFLOW;
1527 memset(pPerAdapterInfo, 0, bytesNeeded);
1528 return NO_ERROR;
1532 /******************************************************************
1533 * GetRTTAndHopCount (IPHLPAPI.@)
1535 * Get round-trip time (RTT) and hop count.
1537 * PARAMS
1539 * DestIpAddress [In] destination address to get the info for
1540 * HopCount [Out] retrieved hop count
1541 * MaxHops [In] maximum hops to search for the destination
1542 * RTT [Out] RTT in milliseconds
1544 * RETURNS
1545 * Success: TRUE
1546 * Failure: FALSE
1548 * FIXME
1549 * Stub, returns FALSE.
1551 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1553 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1554 DestIpAddress, HopCount, MaxHops, RTT);
1555 return FALSE;
1559 /******************************************************************
1560 * GetTcpStatistics (IPHLPAPI.@)
1562 * Get the TCP statistics for the local computer.
1564 * PARAMS
1565 * pStats [Out] buffer for TCP statistics
1567 * RETURNS
1568 * Success: NO_ERROR
1569 * Failure: error code from winerror.h
1571 DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)
1573 DWORD ret;
1575 TRACE("pStats %p\n", pStats);
1576 ret = getTCPStats(pStats);
1577 TRACE("returning %d\n", ret);
1578 return ret;
1582 /******************************************************************
1583 * GetTcpTable (IPHLPAPI.@)
1585 * Get the table of active TCP connections.
1587 * PARAMS
1588 * pTcpTable [Out] buffer for TCP connections table
1589 * pdwSize [In/Out] length of output buffer
1590 * bOrder [In] whether to order the table
1592 * RETURNS
1593 * Success: NO_ERROR
1594 * Failure: error code from winerror.h
1596 * NOTES
1597 * If pdwSize is less than required, the function will return
1598 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1599 * the required byte size.
1600 * If bOrder is true, the returned table will be sorted, first by
1601 * local address and port number, then by remote address and port
1602 * number.
1604 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1606 DWORD ret;
1607 PMIB_TCPTABLE table;
1609 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1611 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1613 ret = getTcpTable(&table, GetProcessHeap(), 0);
1614 if (!ret) {
1615 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1616 if (!pTcpTable || *pdwSize < size) {
1617 *pdwSize = size;
1618 ret = ERROR_INSUFFICIENT_BUFFER;
1620 else {
1621 *pdwSize = size;
1622 memcpy(pTcpTable, table, size);
1623 if (bOrder)
1624 qsort(pTcpTable->table, pTcpTable->dwNumEntries,
1625 sizeof(MIB_TCPROW), TcpTableSorter);
1627 HeapFree(GetProcessHeap(), 0, table);
1629 TRACE("returning %d\n", ret);
1630 return ret;
1634 /******************************************************************
1635 * GetUdpStatistics (IPHLPAPI.@)
1637 * Get the UDP statistics for the local computer.
1639 * PARAMS
1640 * pStats [Out] buffer for UDP statistics
1642 * RETURNS
1643 * Success: NO_ERROR
1644 * Failure: error code from winerror.h
1646 DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
1648 DWORD ret;
1650 TRACE("pStats %p\n", pStats);
1651 ret = getUDPStats(pStats);
1652 TRACE("returning %d\n", ret);
1653 return ret;
1657 /******************************************************************
1658 * GetUdpTable (IPHLPAPI.@)
1660 * Get a table of active UDP connections.
1662 * PARAMS
1663 * pUdpTable [Out] buffer for UDP connections table
1664 * pdwSize [In/Out] length of output buffer
1665 * bOrder [In] whether to order the table
1667 * RETURNS
1668 * Success: NO_ERROR
1669 * Failure: error code from winerror.h
1671 * NOTES
1672 * If pdwSize is less than required, the function will return
1673 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1674 * required byte size.
1675 * If bOrder is true, the returned table will be sorted, first by
1676 * local address, then by local port number.
1678 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1680 DWORD ret;
1681 PMIB_UDPTABLE table;
1683 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1685 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1687 ret = getUdpTable(&table, GetProcessHeap(), 0);
1688 if (!ret) {
1689 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1690 if (!pUdpTable || *pdwSize < size) {
1691 *pdwSize = size;
1692 ret = ERROR_INSUFFICIENT_BUFFER;
1694 else {
1695 *pdwSize = size;
1696 memcpy(pUdpTable, table, size);
1697 if (bOrder)
1698 qsort(pUdpTable->table, pUdpTable->dwNumEntries,
1699 sizeof(MIB_UDPROW), UdpTableSorter);
1701 HeapFree(GetProcessHeap(), 0, table);
1703 TRACE("returning %d\n", ret);
1704 return ret;
1708 /******************************************************************
1709 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1711 * This is a Win98-only function to get information on "unidirectional"
1712 * adapters. Since this is pretty nonsensical in other contexts, it
1713 * never returns anything.
1715 * PARAMS
1716 * pIPIfInfo [Out] buffer for adapter infos
1717 * dwOutBufLen [Out] length of the output buffer
1719 * RETURNS
1720 * Success: NO_ERROR
1721 * Failure: error code from winerror.h
1723 * FIXME
1724 * Stub, returns ERROR_NOT_SUPPORTED.
1726 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1728 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1729 /* a unidirectional adapter?? not bloody likely! */
1730 return ERROR_NOT_SUPPORTED;
1734 /******************************************************************
1735 * IpReleaseAddress (IPHLPAPI.@)
1737 * Release an IP obtained through DHCP,
1739 * PARAMS
1740 * AdapterInfo [In] adapter to release IP address
1742 * RETURNS
1743 * Success: NO_ERROR
1744 * Failure: error code from winerror.h
1746 * NOTES
1747 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1748 * this function does nothing.
1750 * FIXME
1751 * Stub, returns ERROR_NOT_SUPPORTED.
1753 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1755 TRACE("AdapterInfo %p\n", AdapterInfo);
1756 /* not a stub, never going to support this (and I never mark an adapter as
1757 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1758 return ERROR_NOT_SUPPORTED;
1762 /******************************************************************
1763 * IpRenewAddress (IPHLPAPI.@)
1765 * Renew an IP obtained through DHCP.
1767 * PARAMS
1768 * AdapterInfo [In] adapter to renew IP address
1770 * RETURNS
1771 * Success: NO_ERROR
1772 * Failure: error code from winerror.h
1774 * NOTES
1775 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1776 * this function does nothing.
1778 * FIXME
1779 * Stub, returns ERROR_NOT_SUPPORTED.
1781 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1783 TRACE("AdapterInfo %p\n", AdapterInfo);
1784 /* not a stub, never going to support this (and I never mark an adapter as
1785 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1786 return ERROR_NOT_SUPPORTED;
1790 /******************************************************************
1791 * NotifyAddrChange (IPHLPAPI.@)
1793 * Notify caller whenever the ip-interface map is changed.
1795 * PARAMS
1796 * Handle [Out] handle usable in asynchronous notification
1797 * overlapped [In] overlapped structure that notifies the caller
1799 * RETURNS
1800 * Success: NO_ERROR
1801 * Failure: error code from winerror.h
1803 * FIXME
1804 * Stub, returns ERROR_NOT_SUPPORTED.
1806 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1808 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1809 return ERROR_NOT_SUPPORTED;
1813 /******************************************************************
1814 * NotifyRouteChange (IPHLPAPI.@)
1816 * Notify caller whenever the ip routing table is changed.
1818 * PARAMS
1819 * Handle [Out] handle usable in asynchronous notification
1820 * overlapped [In] overlapped structure that notifies the caller
1822 * RETURNS
1823 * Success: NO_ERROR
1824 * Failure: error code from winerror.h
1826 * FIXME
1827 * Stub, returns ERROR_NOT_SUPPORTED.
1829 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1831 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1832 return ERROR_NOT_SUPPORTED;
1836 /******************************************************************
1837 * SendARP (IPHLPAPI.@)
1839 * Send an ARP request.
1841 * PARAMS
1842 * DestIP [In] attempt to obtain this IP
1843 * SrcIP [In] optional sender IP address
1844 * pMacAddr [Out] buffer for the mac address
1845 * PhyAddrLen [In/Out] length of the output buffer
1847 * RETURNS
1848 * Success: NO_ERROR
1849 * Failure: error code from winerror.h
1851 * FIXME
1852 * Stub, returns ERROR_NOT_SUPPORTED.
1854 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1856 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1857 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1858 return ERROR_NOT_SUPPORTED;
1862 /******************************************************************
1863 * SetIfEntry (IPHLPAPI.@)
1865 * Set the administrative status of an interface.
1867 * PARAMS
1868 * pIfRow [In] dwAdminStatus member specifies the new status.
1870 * RETURNS
1871 * Success: NO_ERROR
1872 * Failure: error code from winerror.h
1874 * FIXME
1875 * Stub, returns ERROR_NOT_SUPPORTED.
1877 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1879 FIXME("(pIfRow %p): stub\n", pIfRow);
1880 /* this is supposed to set an interface administratively up or down.
1881 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1882 this sort of down is indistinguishable from other sorts of down (e.g. no
1883 link). */
1884 return ERROR_NOT_SUPPORTED;
1888 /******************************************************************
1889 * SetIpForwardEntry (IPHLPAPI.@)
1891 * Modify an existing route.
1893 * PARAMS
1894 * pRoute [In] route with the new information
1896 * RETURNS
1897 * Success: NO_ERROR
1898 * Failure: error code from winerror.h
1900 * FIXME
1901 * Stub, returns NO_ERROR.
1903 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1905 FIXME("(pRoute %p): stub\n", pRoute);
1906 /* this is to add a route entry, how's it distinguishable from
1907 CreateIpForwardEntry?
1908 could use SIOCADDRT, not sure I want to */
1909 return 0;
1913 /******************************************************************
1914 * SetIpNetEntry (IPHLPAPI.@)
1916 * Modify an existing ARP entry.
1918 * PARAMS
1919 * pArpEntry [In] ARP entry with the new information
1921 * RETURNS
1922 * Success: NO_ERROR
1923 * Failure: error code from winerror.h
1925 * FIXME
1926 * Stub, returns NO_ERROR.
1928 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1930 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1931 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1932 return 0;
1936 /******************************************************************
1937 * SetIpStatistics (IPHLPAPI.@)
1939 * Toggle IP forwarding and det the default TTL value.
1941 * PARAMS
1942 * pIpStats [In] IP statistics with the new information
1944 * RETURNS
1945 * Success: NO_ERROR
1946 * Failure: error code from winerror.h
1948 * FIXME
1949 * Stub, returns NO_ERROR.
1951 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1953 FIXME("(pIpStats %p): stub\n", pIpStats);
1954 return 0;
1958 /******************************************************************
1959 * SetIpTTL (IPHLPAPI.@)
1961 * Set the default TTL value.
1963 * PARAMS
1964 * nTTL [In] new TTL value
1966 * RETURNS
1967 * Success: NO_ERROR
1968 * Failure: error code from winerror.h
1970 * FIXME
1971 * Stub, returns NO_ERROR.
1973 DWORD WINAPI SetIpTTL(UINT nTTL)
1975 FIXME("(nTTL %d): stub\n", nTTL);
1976 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
1977 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
1978 return 0;
1982 /******************************************************************
1983 * SetTcpEntry (IPHLPAPI.@)
1985 * Set the state of a TCP connection.
1987 * PARAMS
1988 * pTcpRow [In] specifies connection with new state
1990 * RETURNS
1991 * Success: NO_ERROR
1992 * Failure: error code from winerror.h
1994 * FIXME
1995 * Stub, returns NO_ERROR.
1997 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
1999 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2000 return 0;
2004 /******************************************************************
2005 * UnenableRouter (IPHLPAPI.@)
2007 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2008 * if it reaches zero.
2010 * PARAMS
2011 * pOverlapped [In/Out] should be the same as in EnableRouter()
2012 * lpdwEnableCount [Out] optional, receives reference count
2014 * RETURNS
2015 * Success: NO_ERROR
2016 * Failure: error code from winerror.h
2018 * FIXME
2019 * Stub, returns ERROR_NOT_SUPPORTED.
2021 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2023 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2024 lpdwEnableCount);
2025 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2026 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2028 return ERROR_NOT_SUPPORTED;