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
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
63 #define IF_NAMESIZE 16
67 #define INADDR_NONE ~0UL
70 /* call res_init() just once because of a bug in Mac OS X 10.4 */
71 /* Call once per thread on systems that have per-thread _res. */
72 static void initialise_resolver(void)
74 if ((_res.options & RES_INIT) == 0)
78 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
81 case DLL_PROCESS_ATTACH:
82 DisableThreadLibraryCalls( hinstDLL );
85 case DLL_PROCESS_DETACH:
91 /******************************************************************
92 * AddIPAddress (IPHLPAPI.@)
94 * Add an IP address to an adapter.
97 * Address [In] IP address to add to the adapter
98 * IpMask [In] subnet mask for the IP address
99 * IfIndex [In] adapter index to add the address
100 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
101 * NTEInstance [Out] NTE instance for the IP address
105 * Failure: error code from winerror.h
108 * Stub. Currently returns ERROR_NOT_SUPPORTED.
110 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
113 return ERROR_NOT_SUPPORTED;
117 /******************************************************************
118 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
120 * Get table of local interfaces.
121 * Like GetIfTable(), but allocate the returned table from heap.
124 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
125 * allocated and returned.
126 * bOrder [In] whether to sort the table
127 * heap [In] heap from which the table is allocated
128 * flags [In] flags to HeapAlloc
131 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
132 * GetIfTable() returns otherwise.
134 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
135 BOOL bOrder, HANDLE heap, DWORD flags)
139 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
140 bOrder, heap, flags);
142 ret = ERROR_INVALID_PARAMETER;
146 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
147 if (ret == ERROR_INSUFFICIENT_BUFFER) {
148 *ppIfTable = HeapAlloc(heap, flags, dwSize);
149 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
152 TRACE("returning %d\n", ret);
157 static int IpAddrTableSorter(const void *a, const void *b)
162 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
169 /******************************************************************
170 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
172 * Get interface-to-IP address mapping table.
173 * Like GetIpAddrTable(), but allocate the returned table from heap.
176 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
177 * allocated and returned.
178 * bOrder [In] whether to sort the table
179 * heap [In] heap from which the table is allocated
180 * flags [In] flags to HeapAlloc
183 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
184 * failure, NO_ERROR on success.
186 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
187 BOOL bOrder, HANDLE heap, DWORD flags)
191 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
192 ppIpAddrTable, bOrder, heap, flags);
193 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
195 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
196 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
197 TRACE("returning %d\n", ret);
202 /******************************************************************
203 * CancelIPChangeNotify (IPHLPAPI.@)
205 * Cancel a previous notification created by NotifyAddrChange or
209 * overlapped [In] overlapped structure that notifies the caller
216 * Stub, returns FALSE.
218 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
220 FIXME("(overlapped %p): stub\n", overlapped);
226 /******************************************************************
227 * CreateIpForwardEntry (IPHLPAPI.@)
229 * Create a route in the local computer's IP table.
232 * pRoute [In] new route information
236 * Failure: error code from winerror.h
239 * Stub, always returns NO_ERROR.
241 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
243 FIXME("(pRoute %p): stub\n", pRoute);
244 /* could use SIOCADDRT, not sure I want to */
249 /******************************************************************
250 * CreateIpNetEntry (IPHLPAPI.@)
252 * Create entry in the ARP table.
255 * pArpEntry [In] new ARP entry
259 * Failure: error code from winerror.h
262 * Stub, always returns NO_ERROR.
264 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
266 FIXME("(pArpEntry %p)\n", pArpEntry);
267 /* could use SIOCSARP on systems that support it, not sure I want to */
272 /******************************************************************
273 * CreateProxyArpEntry (IPHLPAPI.@)
275 * Create a Proxy ARP (PARP) entry for an IP address.
278 * dwAddress [In] IP address for which this computer acts as a proxy.
279 * dwMask [In] subnet mask for dwAddress
280 * dwIfIndex [In] interface index
284 * Failure: error code from winerror.h
287 * Stub, returns ERROR_NOT_SUPPORTED.
289 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
291 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
292 dwAddress, dwMask, dwIfIndex);
293 return ERROR_NOT_SUPPORTED;
297 /******************************************************************
298 * DeleteIPAddress (IPHLPAPI.@)
300 * Delete an IP address added with AddIPAddress().
303 * NTEContext [In] NTE context from AddIPAddress();
307 * Failure: error code from winerror.h
310 * Stub, returns ERROR_NOT_SUPPORTED.
312 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
314 FIXME("(NTEContext %d): stub\n", NTEContext);
315 return ERROR_NOT_SUPPORTED;
319 /******************************************************************
320 * DeleteIpForwardEntry (IPHLPAPI.@)
325 * pRoute [In] route to delete
329 * Failure: error code from winerror.h
332 * Stub, returns NO_ERROR.
334 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
336 FIXME("(pRoute %p): stub\n", pRoute);
337 /* could use SIOCDELRT, not sure I want to */
342 /******************************************************************
343 * DeleteIpNetEntry (IPHLPAPI.@)
345 * Delete an ARP entry.
348 * pArpEntry [In] ARP entry to delete
352 * Failure: error code from winerror.h
355 * Stub, returns NO_ERROR.
357 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
359 FIXME("(pArpEntry %p): stub\n", pArpEntry);
360 /* could use SIOCDARP on systems that support it, not sure I want to */
365 /******************************************************************
366 * DeleteProxyArpEntry (IPHLPAPI.@)
368 * Delete a Proxy ARP entry.
371 * dwAddress [In] IP address for which this computer acts as a proxy.
372 * dwMask [In] subnet mask for dwAddress
373 * dwIfIndex [In] interface index
377 * Failure: error code from winerror.h
380 * Stub, returns ERROR_NOT_SUPPORTED.
382 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
384 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
385 dwAddress, dwMask, dwIfIndex);
386 return ERROR_NOT_SUPPORTED;
390 /******************************************************************
391 * EnableRouter (IPHLPAPI.@)
393 * Turn on ip forwarding.
397 * pOverlapped [In/Out] hEvent member should contain a valid handle.
400 * Success: ERROR_IO_PENDING
401 * Failure: error code from winerror.h
404 * Stub, returns ERROR_NOT_SUPPORTED.
406 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
408 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
409 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
410 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
412 return ERROR_NOT_SUPPORTED;
416 /******************************************************************
417 * FlushIpNetTable (IPHLPAPI.@)
419 * Delete all ARP entries of an interface
422 * dwIfIndex [In] interface index
426 * Failure: error code from winerror.h
429 * Stub, returns ERROR_NOT_SUPPORTED.
431 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
433 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
434 /* this flushes the arp cache of the given index */
435 return ERROR_NOT_SUPPORTED;
439 /******************************************************************
440 * GetAdapterIndex (IPHLPAPI.@)
442 * Get interface index from its name.
445 * AdapterName [In] unicode string with the adapter name
446 * IfIndex [Out] returns found interface index
450 * Failure: error code from winerror.h
452 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
454 char adapterName[MAX_ADAPTER_NAME];
458 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
459 /* The adapter name is guaranteed not to have any unicode characters, so
460 * this translation is never lossy */
461 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
462 adapterName[i] = (char)AdapterName[i];
463 adapterName[i] = '\0';
464 ret = getInterfaceIndexByName(adapterName, IfIndex);
465 TRACE("returning %d\n", ret);
470 /******************************************************************
471 * GetAdaptersInfo (IPHLPAPI.@)
473 * Get information about adapters.
476 * pAdapterInfo [Out] buffer for adapter infos
477 * pOutBufLen [In] length of output buffer
481 * Failure: error code from winerror.h
483 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
487 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
489 ret = ERROR_INVALID_PARAMETER;
491 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
493 if (numNonLoopbackInterfaces > 0) {
494 DWORD numIPAddresses = getNumIPAddresses();
497 /* This may slightly overestimate the amount of space needed, because
498 * the IP addresses include the loopback address, but it's easier
499 * to make sure there's more than enough space than to make sure there's
500 * precisely enough space.
502 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
503 size += numIPAddresses * sizeof(IP_ADDR_STRING);
504 if (!pAdapterInfo || *pOutBufLen < size) {
506 ret = ERROR_BUFFER_OVERFLOW;
509 InterfaceIndexTable *table = NULL;
510 PMIB_IPADDRTABLE ipAddrTable = NULL;
511 PMIB_IPFORWARDTABLE routeTable = NULL;
513 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
515 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
517 table = getNonLoopbackInterfaceIndexTable();
519 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
520 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
521 if (*pOutBufLen < size) {
523 ret = ERROR_INSUFFICIENT_BUFFER;
528 BOOL winsEnabled = FALSE;
529 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
530 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
531 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
533 memset(pAdapterInfo, 0, size);
534 /* @@ Wine registry key: HKCU\Software\Wine\Network */
535 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
536 &hKey) == ERROR_SUCCESS) {
537 DWORD size = sizeof(primaryWINS.String);
540 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
541 (LPBYTE)primaryWINS.String, &size);
542 addr = inet_addr(primaryWINS.String);
543 if (addr != INADDR_NONE && addr != INADDR_ANY)
545 size = sizeof(secondaryWINS.String);
546 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
547 (LPBYTE)secondaryWINS.String, &size);
548 addr = inet_addr(secondaryWINS.String);
549 if (addr != INADDR_NONE && addr != INADDR_ANY)
553 for (ndx = 0; ndx < table->numIndexes; ndx++) {
554 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
556 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
557 BOOL firstIPAddr = TRUE;
559 /* on Win98 this is left empty, but whatever */
560 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
561 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
562 ptr->AddressLength = sizeof(ptr->Address);
563 getInterfacePhysicalByIndex(table->indexes[ndx],
564 &ptr->AddressLength, ptr->Address, &ptr->Type);
565 ptr->Index = table->indexes[ndx];
566 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
567 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
569 toIPAddressString(ipAddrTable->table[i].dwAddr,
570 ptr->IpAddressList.IpAddress.String);
571 toIPAddressString(ipAddrTable->table[i].dwMask,
572 ptr->IpAddressList.IpMask.String);
576 currentIPAddr->Next = nextIPAddr;
577 currentIPAddr = nextIPAddr;
578 toIPAddressString(ipAddrTable->table[i].dwAddr,
579 currentIPAddr->IpAddress.String);
580 toIPAddressString(ipAddrTable->table[i].dwMask,
581 currentIPAddr->IpMask.String);
586 /* Find first router through this interface, which we'll assume
587 * is the default gateway for this adapter */
588 for (i = 0; i < routeTable->dwNumEntries; i++)
589 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
590 && routeTable->table[i].dwForwardType ==
591 MIB_IPROUTE_TYPE_INDIRECT)
592 toIPAddressString(routeTable->table[i].dwForwardNextHop,
593 ptr->GatewayList.IpAddress.String);
595 ptr->HaveWins = TRUE;
596 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
597 primaryWINS.String, sizeof(primaryWINS.String));
598 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
599 secondaryWINS.String, sizeof(secondaryWINS.String));
601 if (ndx < table->numIndexes - 1)
602 ptr->Next = &pAdapterInfo[ndx + 1];
608 HeapFree(GetProcessHeap(), 0, table);
611 ret = ERROR_OUTOFMEMORY;
612 HeapFree(GetProcessHeap(), 0, routeTable);
613 HeapFree(GetProcessHeap(), 0, ipAddrTable);
619 TRACE("returning %d\n", ret);
623 static DWORD typeFromMibType(DWORD mib_type)
627 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
628 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
629 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
630 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
631 default: return IF_TYPE_OTHER;
635 static DWORD connectionTypeFromMibType(DWORD mib_type)
639 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
640 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
641 default: return NET_IF_CONNECTION_DEDICATED;
645 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
651 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
652 for (i = 0; i < at->dwNumEntries; i++)
654 if (at->table[i].dwIndex == index) (*num_addrs)++;
656 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
658 HeapFree(GetProcessHeap(), 0, at);
659 return ERROR_OUTOFMEMORY;
661 for (i = 0, j = 0; i < at->dwNumEntries; i++)
663 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
665 HeapFree(GetProcessHeap(), 0, at);
666 return ERROR_SUCCESS;
669 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
674 for (addrp = (const BYTE *)in_addr;
675 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
678 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
679 sprintf(p, "%d", *addrp);
681 p += sprintf(p, "%d.", *addrp);
686 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
688 const IN6_ADDR *addr = &sin->sin6_addr;
691 BOOL in_zero = FALSE;
693 for (i = 0; i < 7; i++)
695 if (!addr->u.Word[i])
707 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
711 sprintf(p, "%x", ntohs(addr->u.Word[7]));
715 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
717 DWORD i, num_gateways = 0;
719 for (i = 0; i < routeTable->dwNumEntries; i++)
721 if (routeTable->table[i].dwForwardIfIndex == index &&
722 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
728 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
729 PMIB_IPFORWARDTABLE routeTable)
732 PMIB_IPFORWARDROW row = NULL;
734 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
736 if (routeTable->table[i].dwForwardIfIndex == index &&
737 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
738 row = &routeTable->table[i];
743 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
744 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
746 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
747 DWORD *v4addrs = NULL;
748 SOCKET_ADDRESS *v6addrs = NULL;
749 PMIB_IPFORWARDTABLE routeTable = NULL;
751 if (family == WS_AF_INET)
753 if (!(flags & GAA_FLAG_SKIP_UNICAST))
754 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
755 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
757 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
758 GetProcessHeap(), 0);
760 num_v4_gateways = count_v4_gateways(index, routeTable);
763 else if (family == WS_AF_INET6)
765 if (!(flags & GAA_FLAG_SKIP_UNICAST))
766 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
768 else if (family == WS_AF_UNSPEC)
770 if (!(flags & GAA_FLAG_SKIP_UNICAST))
771 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
772 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
774 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
775 GetProcessHeap(), 0);
778 num_v4_gateways = count_v4_gateways(index, routeTable);
779 if (!(flags & GAA_FLAG_SKIP_UNICAST))
780 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
786 FIXME("address family %u unsupported\n", family);
791 HeapFree(GetProcessHeap(), 0, routeTable);
795 total_size = sizeof(IP_ADAPTER_ADDRESSES);
796 total_size += IF_NAMESIZE;
797 total_size += IF_NAMESIZE * sizeof(WCHAR);
798 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
799 total_size += IF_NAMESIZE * sizeof(WCHAR);
800 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
801 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
802 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
803 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
804 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
805 for (i = 0; i < num_v6addrs; i++)
806 total_size += v6addrs[i].iSockaddrLength;
808 if (aa && *size >= total_size)
810 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
812 DWORD buflen, type, status;
814 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
815 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
816 aa->u.s.IfIndex = index;
818 getInterfaceNameByIndex(index, name);
819 memcpy(ptr, name, IF_NAMESIZE);
820 aa->AdapterName = ptr;
822 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
824 aa->FriendlyName = (WCHAR *)ptr;
825 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
830 aa->Description = (WCHAR *)ptr;
831 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
836 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
840 PMIB_IPFORWARDROW adapterRow;
842 if ((adapterRow = findIPv4Gateway(index, routeTable)))
844 PIP_ADAPTER_GATEWAY_ADDRESS gw;
847 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
848 aa->FirstGatewayAddress = gw;
850 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
851 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
852 sin = (PSOCKADDR_IN)ptr;
853 sin->sin_family = AF_INET;
855 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
857 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
858 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
860 ptr += sizeof(SOCKADDR_IN);
865 IP_ADAPTER_UNICAST_ADDRESS *ua;
866 struct sockaddr_in *sa;
867 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
868 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
869 for (i = 0; i < num_v4addrs; i++)
873 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
874 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
875 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
876 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
878 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
879 sa->sin_family = AF_INET;
880 sa->sin_addr.s_addr = v4addrs[i];
882 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
883 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
885 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
886 if (i < num_v4addrs - 1)
888 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
895 IP_ADAPTER_UNICAST_ADDRESS *ua;
896 struct WS_sockaddr_in6 *sa;
898 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
899 if (aa->FirstUnicastAddress)
901 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
903 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
904 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
907 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
908 for (i = 0; i < num_v6addrs; i++)
912 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
913 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
914 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
915 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
917 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
918 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
919 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
920 debugstr_ipv6(sa, addr_buf));
922 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
923 if (i < num_v6addrs - 1)
925 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
931 buflen = MAX_INTERFACE_PHYSADDR;
932 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
933 aa->PhysicalAddressLength = buflen;
934 aa->IfType = typeFromMibType(type);
935 aa->ConnectionType = connectionTypeFromMibType(type);
937 getInterfaceMtuByName(name, &aa->Mtu);
939 getInterfaceStatusByName(name, &status);
940 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
941 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
942 else aa->OperStatus = IfOperStatusUnknown;
945 HeapFree(GetProcessHeap(), 0, routeTable);
946 HeapFree(GetProcessHeap(), 0, v6addrs);
947 HeapFree(GetProcessHeap(), 0, v4addrs);
948 return ERROR_SUCCESS;
951 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
955 initialise_resolver();
956 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
957 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
960 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
961 if (!address || *len < size)
964 return ERROR_BUFFER_OVERFLOW;
967 if (_res.nscount > 0)
969 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
972 for (i = 0, addr = address; i < _res.nscount && addr;
973 i++, addr = addr->Next)
977 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
978 addr->Address.lpSockaddr =
979 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
980 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
981 sin->sin_family = WS_AF_INET;
982 sin->sin_port = _res.nsaddr_list[i].sin_port;
983 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
984 if (i == _res.nscount - 1)
988 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
989 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
992 return ERROR_SUCCESS;
995 static BOOL is_ip_address_string(const char *str)
1000 ret = inet_aton(str, &in);
1004 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1007 char *found_suffix = NULL;
1009 initialise_resolver();
1010 /* Always return a NULL-terminated string, even if it's empty. */
1011 size = sizeof(WCHAR);
1012 for (i = 0, found_suffix = NULL;
1013 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1015 /* This uses a heuristic to select a DNS suffix:
1016 * the first, non-IP address string is selected.
1018 if (!is_ip_address_string(_res.dnsrch[i]))
1019 found_suffix = _res.dnsrch[i];
1022 size += strlen(found_suffix) * sizeof(WCHAR);
1023 if (!suffix || *len < size)
1026 return ERROR_BUFFER_OVERFLOW;
1033 for (p = found_suffix; *p; p++)
1037 return ERROR_SUCCESS;
1040 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1041 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1043 InterfaceIndexTable *table;
1044 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1046 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1048 if (!buflen) return ERROR_INVALID_PARAMETER;
1050 table = getInterfaceIndexTable();
1051 if (!table || !table->numIndexes)
1053 HeapFree(GetProcessHeap(), 0, table);
1054 return ERROR_NO_DATA;
1057 for (i = 0; i < table->numIndexes; i++)
1060 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1062 HeapFree(GetProcessHeap(), 0, table);
1067 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1069 /* Since DNS servers aren't really per adapter, get enough space for a
1070 * single copy of them.
1072 get_dns_server_addresses(NULL, &dns_server_size);
1073 total_size += dns_server_size;
1075 /* Since DNS suffix also isn't really per adapter, get enough space for a
1076 * single copy of it.
1078 get_dns_suffix(NULL, &dns_suffix_size);
1079 total_size += dns_suffix_size;
1080 if (aa && *buflen >= total_size)
1082 ULONG bytes_left = size = total_size;
1083 PIP_ADAPTER_ADDRESSES first_aa = aa;
1084 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1087 for (i = 0; i < table->numIndexes; i++)
1089 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1091 HeapFree(GetProcessHeap(), 0, table);
1094 if (i < table->numIndexes - 1)
1096 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1098 size = bytes_left -= size;
1101 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1103 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)aa + total_size - dns_server_size - dns_suffix_size);
1104 get_dns_server_addresses(firstDns, &dns_server_size);
1105 for (aa = first_aa; aa; aa = aa->Next)
1107 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1108 aa->FirstDnsServerAddress = firstDns;
1112 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1113 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1114 for (; aa; aa = aa->Next)
1116 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1117 aa->DnsSuffix = dnsSuffix;
1119 ret = ERROR_SUCCESS;
1121 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
1122 *buflen = total_size;
1124 TRACE("num adapters %u\n", table->numIndexes);
1125 HeapFree(GetProcessHeap(), 0, table);
1129 /******************************************************************
1130 * GetBestInterface (IPHLPAPI.@)
1132 * Get the interface, with the best route for the given IP address.
1135 * dwDestAddr [In] IP address to search the interface for
1136 * pdwBestIfIndex [Out] found best interface
1140 * Failure: error code from winerror.h
1142 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1144 struct WS_sockaddr_in sa_in;
1145 memset(&sa_in, 0, sizeof(sa_in));
1146 sa_in.sin_family = AF_INET;
1147 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1148 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1151 /******************************************************************
1152 * GetBestInterfaceEx (IPHLPAPI.@)
1154 * Get the interface, with the best route for the given IP address.
1157 * dwDestAddr [In] IP address to search the interface for
1158 * pdwBestIfIndex [Out] found best interface
1162 * Failure: error code from winerror.h
1164 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1168 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1169 if (!pDestAddr || !pdwBestIfIndex)
1170 ret = ERROR_INVALID_PARAMETER;
1172 MIB_IPFORWARDROW ipRow;
1174 if (pDestAddr->sa_family == AF_INET) {
1175 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1176 if (ret == ERROR_SUCCESS)
1177 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1179 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1180 ret = ERROR_NOT_SUPPORTED;
1183 TRACE("returning %d\n", ret);
1188 /******************************************************************
1189 * GetBestRoute (IPHLPAPI.@)
1191 * Get the best route for the given IP address.
1194 * dwDestAddr [In] IP address to search the best route for
1195 * dwSourceAddr [In] optional source IP address
1196 * pBestRoute [Out] found best route
1200 * Failure: error code from winerror.h
1202 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1204 PMIB_IPFORWARDTABLE table;
1207 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1208 dwSourceAddr, pBestRoute);
1210 return ERROR_INVALID_PARAMETER;
1212 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1214 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1216 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1217 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1218 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1219 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1220 DWORD numShifts, mask;
1222 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1223 mask && mask & 1; mask >>= 1, numShifts++)
1225 if (numShifts > matchedBits) {
1226 matchedBits = numShifts;
1229 else if (!matchedBits) {
1234 if (matchedNdx < table->dwNumEntries) {
1235 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1236 ret = ERROR_SUCCESS;
1239 /* No route matches, which can happen if there's no default route. */
1240 ret = ERROR_HOST_UNREACHABLE;
1242 HeapFree(GetProcessHeap(), 0, table);
1244 TRACE("returning %d\n", ret);
1249 /******************************************************************
1250 * GetFriendlyIfIndex (IPHLPAPI.@)
1252 * Get a "friendly" version of IfIndex, which is one that doesn't
1253 * have the top byte set. Doesn't validate whether IfIndex is a valid
1257 * IfIndex [In] interface index to get the friendly one for
1260 * A friendly version of IfIndex.
1262 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1264 /* windows doesn't validate these, either, just makes sure the top byte is
1265 cleared. I assume my ifenum module never gives an index with the top
1267 TRACE("returning %d\n", IfIndex);
1272 /******************************************************************
1273 * GetIfEntry (IPHLPAPI.@)
1275 * Get information about an interface.
1278 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1279 * Out: interface information
1283 * Failure: error code from winerror.h
1285 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1288 char nameBuf[MAX_ADAPTER_NAME];
1291 TRACE("pIfRow %p\n", pIfRow);
1293 return ERROR_INVALID_PARAMETER;
1295 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1297 ret = getInterfaceEntryByName(name, pIfRow);
1298 if (ret == NO_ERROR)
1299 ret = getInterfaceStatsByName(name, pIfRow);
1302 ret = ERROR_INVALID_DATA;
1303 TRACE("returning %d\n", ret);
1308 static int IfTableSorter(const void *a, const void *b)
1313 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1320 /******************************************************************
1321 * GetIfTable (IPHLPAPI.@)
1323 * Get a table of local interfaces.
1326 * pIfTable [Out] buffer for local interfaces table
1327 * pdwSize [In/Out] length of output buffer
1328 * bOrder [In] whether to sort the table
1332 * Failure: error code from winerror.h
1335 * If pdwSize is less than required, the function will return
1336 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1338 * If bOrder is true, the returned table will be sorted by interface index.
1340 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1344 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1347 ret = ERROR_INVALID_PARAMETER;
1349 DWORD numInterfaces = getNumInterfaces();
1350 ULONG size = sizeof(MIB_IFTABLE);
1352 if (numInterfaces > 1)
1353 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1354 if (!pIfTable || *pdwSize < size) {
1356 ret = ERROR_INSUFFICIENT_BUFFER;
1359 InterfaceIndexTable *table = getInterfaceIndexTable();
1362 size = sizeof(MIB_IFTABLE);
1363 if (table->numIndexes > 1)
1364 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1365 if (*pdwSize < size) {
1367 ret = ERROR_INSUFFICIENT_BUFFER;
1373 pIfTable->dwNumEntries = 0;
1374 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1375 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1376 GetIfEntry(&pIfTable->table[ndx]);
1377 pIfTable->dwNumEntries++;
1380 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1384 HeapFree(GetProcessHeap(), 0, table);
1387 ret = ERROR_OUTOFMEMORY;
1390 TRACE("returning %d\n", ret);
1395 /******************************************************************
1396 * GetInterfaceInfo (IPHLPAPI.@)
1398 * Get a list of network interface adapters.
1401 * pIfTable [Out] buffer for interface adapters
1402 * dwOutBufLen [Out] if buffer is too small, returns required size
1406 * Failure: error code from winerror.h
1409 * MSDN states this should return non-loopback interfaces only.
1411 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1415 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1417 ret = ERROR_INVALID_PARAMETER;
1419 DWORD numInterfaces = getNumInterfaces();
1420 ULONG size = sizeof(IP_INTERFACE_INFO);
1422 if (numInterfaces > 1)
1423 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1424 if (!pIfTable || *dwOutBufLen < size) {
1425 *dwOutBufLen = size;
1426 ret = ERROR_INSUFFICIENT_BUFFER;
1429 InterfaceIndexTable *table = getInterfaceIndexTable();
1432 size = sizeof(IP_INTERFACE_INFO);
1433 if (table->numIndexes > 1)
1434 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1435 if (*dwOutBufLen < size) {
1436 *dwOutBufLen = size;
1437 ret = ERROR_INSUFFICIENT_BUFFER;
1441 char nameBuf[MAX_ADAPTER_NAME];
1443 *dwOutBufLen = size;
1444 pIfTable->NumAdapters = 0;
1445 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1446 const char *walker, *name;
1449 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1450 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1451 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1452 walker && *walker &&
1453 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1454 walker++, assigner++)
1455 *assigner = *walker;
1457 pIfTable->NumAdapters++;
1461 HeapFree(GetProcessHeap(), 0, table);
1464 ret = ERROR_OUTOFMEMORY;
1467 TRACE("returning %d\n", ret);
1472 /******************************************************************
1473 * GetIpAddrTable (IPHLPAPI.@)
1475 * Get interface-to-IP address mapping table.
1478 * pIpAddrTable [Out] buffer for mapping table
1479 * pdwSize [In/Out] length of output buffer
1480 * bOrder [In] whether to sort the table
1484 * Failure: error code from winerror.h
1487 * If pdwSize is less than required, the function will return
1488 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1490 * If bOrder is true, the returned table will be sorted by the next hop and
1491 * an assortment of arbitrary parameters.
1493 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1497 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1500 ret = ERROR_INVALID_PARAMETER;
1502 PMIB_IPADDRTABLE table;
1504 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1505 if (ret == NO_ERROR)
1507 ULONG size = sizeof(MIB_IPADDRTABLE);
1509 if (table->dwNumEntries > 1)
1510 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1511 if (!pIpAddrTable || *pdwSize < size) {
1513 ret = ERROR_INSUFFICIENT_BUFFER;
1517 memcpy(pIpAddrTable, table, size);
1519 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1520 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1523 HeapFree(GetProcessHeap(), 0, table);
1526 TRACE("returning %d\n", ret);
1531 /******************************************************************
1532 * GetIpForwardTable (IPHLPAPI.@)
1534 * Get the route table.
1537 * pIpForwardTable [Out] buffer for route table
1538 * pdwSize [In/Out] length of output buffer
1539 * bOrder [In] whether to sort the table
1543 * Failure: error code from winerror.h
1546 * If pdwSize is less than required, the function will return
1547 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1549 * If bOrder is true, the returned table will be sorted by the next hop and
1550 * an assortment of arbitrary parameters.
1552 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1555 PMIB_IPFORWARDTABLE table;
1557 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1559 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1561 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1563 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1564 if (!pIpForwardTable || *pdwSize < size) {
1566 ret = ERROR_INSUFFICIENT_BUFFER;
1570 memcpy(pIpForwardTable, table, size);
1572 HeapFree(GetProcessHeap(), 0, table);
1574 TRACE("returning %d\n", ret);
1579 /******************************************************************
1580 * GetIpNetTable (IPHLPAPI.@)
1582 * Get the IP-to-physical address mapping table.
1585 * pIpNetTable [Out] buffer for mapping table
1586 * pdwSize [In/Out] length of output buffer
1587 * bOrder [In] whether to sort the table
1591 * Failure: error code from winerror.h
1594 * If pdwSize is less than required, the function will return
1595 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1597 * If bOrder is true, the returned table will be sorted by IP address.
1599 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1602 PMIB_IPNETTABLE table;
1604 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1606 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1608 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1610 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1611 if (!pIpNetTable || *pdwSize < size) {
1613 ret = ERROR_INSUFFICIENT_BUFFER;
1617 memcpy(pIpNetTable, table, size);
1619 HeapFree(GetProcessHeap(), 0, table);
1621 TRACE("returning %d\n", ret);
1625 /* Gets the DNS server list into the list beginning at list. Assumes that
1626 * a single server address may be placed at list if *len is at least
1627 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1628 * and assumes that all remaining DNS servers are contiguously located
1629 * beginning at firstDynamic. On input, *len is assumed to be the total number
1630 * of bytes available for all DNS servers, and is ignored if list is NULL.
1631 * On return, *len is set to the total number of bytes required for all DNS
1633 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1634 * ERROR_SUCCESS otherwise.
1636 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1637 PIP_ADDR_STRING firstDynamic, DWORD *len)
1641 initialise_resolver();
1642 size = _res.nscount * sizeof(IP_ADDR_STRING);
1643 if (!list || *len < size) {
1645 return ERROR_BUFFER_OVERFLOW;
1648 if (_res.nscount > 0) {
1649 PIP_ADDR_STRING ptr;
1652 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1653 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1654 ptr->IpAddress.String);
1655 if (i == _res.nscount - 1)
1658 ptr->Next = firstDynamic;
1660 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1663 return ERROR_SUCCESS;
1666 /******************************************************************
1667 * GetNetworkParams (IPHLPAPI.@)
1669 * Get the network parameters for the local computer.
1672 * pFixedInfo [Out] buffer for network parameters
1673 * pOutBufLen [In/Out] length of output buffer
1677 * Failure: error code from winerror.h
1680 * If pOutBufLen is less than required, the function will return
1681 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1684 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1686 DWORD ret, size, serverListSize;
1690 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1692 return ERROR_INVALID_PARAMETER;
1694 get_dns_server_list(NULL, NULL, &serverListSize);
1695 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1696 if (!pFixedInfo || *pOutBufLen < size) {
1698 return ERROR_BUFFER_OVERFLOW;
1701 memset(pFixedInfo, 0, size);
1702 size = sizeof(pFixedInfo->HostName);
1703 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1704 size = sizeof(pFixedInfo->DomainName);
1705 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1706 get_dns_server_list(&pFixedInfo->DnsServerList,
1707 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1709 /* Assume the first DNS server in the list is the "current" DNS server: */
1710 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1711 pFixedInfo->NodeType = HYBRID_NODETYPE;
1712 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1713 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1714 if (regReturn != ERROR_SUCCESS)
1715 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1716 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1718 if (regReturn == ERROR_SUCCESS)
1720 DWORD size = sizeof(pFixedInfo->ScopeId);
1722 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1726 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1727 I suppose could also check for a listener on port 53 to set EnableDns */
1729 TRACE("returning %d\n", ret);
1734 /******************************************************************
1735 * GetNumberOfInterfaces (IPHLPAPI.@)
1737 * Get the number of interfaces.
1740 * pdwNumIf [Out] number of interfaces
1743 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1745 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1749 TRACE("pdwNumIf %p\n", pdwNumIf);
1751 ret = ERROR_INVALID_PARAMETER;
1753 *pdwNumIf = getNumInterfaces();
1756 TRACE("returning %d\n", ret);
1761 /******************************************************************
1762 * GetPerAdapterInfo (IPHLPAPI.@)
1764 * Get information about an adapter corresponding to an interface.
1767 * IfIndex [In] interface info
1768 * pPerAdapterInfo [Out] buffer for per adapter info
1769 * pOutBufLen [In/Out] length of output buffer
1773 * Failure: error code from winerror.h
1775 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1777 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1778 DWORD ret = NO_ERROR;
1780 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1782 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1784 if (!isIfIndexLoopback(IfIndex)) {
1785 get_dns_server_list(NULL, NULL, &serverListSize);
1786 if (serverListSize > sizeof(IP_ADDR_STRING))
1787 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1789 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1791 *pOutBufLen = bytesNeeded;
1792 return ERROR_BUFFER_OVERFLOW;
1795 memset(pPerAdapterInfo, 0, bytesNeeded);
1796 if (!isIfIndexLoopback(IfIndex)) {
1797 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1798 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1800 /* Assume the first DNS server in the list is the "current" DNS server: */
1801 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1807 /******************************************************************
1808 * GetRTTAndHopCount (IPHLPAPI.@)
1810 * Get round-trip time (RTT) and hop count.
1814 * DestIpAddress [In] destination address to get the info for
1815 * HopCount [Out] retrieved hop count
1816 * MaxHops [In] maximum hops to search for the destination
1817 * RTT [Out] RTT in milliseconds
1824 * Stub, returns FALSE.
1826 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1828 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1829 DestIpAddress, HopCount, MaxHops, RTT);
1834 /******************************************************************
1835 * GetTcpTable (IPHLPAPI.@)
1837 * Get the table of active TCP connections.
1840 * pTcpTable [Out] buffer for TCP connections table
1841 * pdwSize [In/Out] length of output buffer
1842 * bOrder [In] whether to order the table
1846 * Failure: error code from winerror.h
1849 * If pdwSize is less than required, the function will return
1850 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1851 * the required byte size.
1852 * If bOrder is true, the returned table will be sorted, first by
1853 * local address and port number, then by remote address and port
1856 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1859 PMIB_TCPTABLE table;
1861 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1863 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1865 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1867 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1868 if (!pTcpTable || *pdwSize < size) {
1870 ret = ERROR_INSUFFICIENT_BUFFER;
1874 memcpy(pTcpTable, table, size);
1876 HeapFree(GetProcessHeap(), 0, table);
1878 TRACE("returning %d\n", ret);
1883 /******************************************************************
1884 * GetUdpTable (IPHLPAPI.@)
1886 * Get a table of active UDP connections.
1889 * pUdpTable [Out] buffer for UDP connections table
1890 * pdwSize [In/Out] length of output buffer
1891 * bOrder [In] whether to order the table
1895 * Failure: error code from winerror.h
1898 * If pdwSize is less than required, the function will return
1899 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1900 * required byte size.
1901 * If bOrder is true, the returned table will be sorted, first by
1902 * local address, then by local port number.
1904 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1907 PMIB_UDPTABLE table;
1909 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1911 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1913 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1915 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1916 if (!pUdpTable || *pdwSize < size) {
1918 ret = ERROR_INSUFFICIENT_BUFFER;
1922 memcpy(pUdpTable, table, size);
1924 HeapFree(GetProcessHeap(), 0, table);
1926 TRACE("returning %d\n", ret);
1931 /******************************************************************
1932 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1934 * This is a Win98-only function to get information on "unidirectional"
1935 * adapters. Since this is pretty nonsensical in other contexts, it
1936 * never returns anything.
1939 * pIPIfInfo [Out] buffer for adapter infos
1940 * dwOutBufLen [Out] length of the output buffer
1944 * Failure: error code from winerror.h
1947 * Stub, returns ERROR_NOT_SUPPORTED.
1949 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1951 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1952 /* a unidirectional adapter?? not bloody likely! */
1953 return ERROR_NOT_SUPPORTED;
1957 /******************************************************************
1958 * IpReleaseAddress (IPHLPAPI.@)
1960 * Release an IP obtained through DHCP,
1963 * AdapterInfo [In] adapter to release IP address
1967 * Failure: error code from winerror.h
1970 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1971 * this function does nothing.
1974 * Stub, returns ERROR_NOT_SUPPORTED.
1976 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1978 TRACE("AdapterInfo %p\n", AdapterInfo);
1979 /* not a stub, never going to support this (and I never mark an adapter as
1980 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1981 return ERROR_NOT_SUPPORTED;
1985 /******************************************************************
1986 * IpRenewAddress (IPHLPAPI.@)
1988 * Renew an IP obtained through DHCP.
1991 * AdapterInfo [In] adapter to renew IP address
1995 * Failure: error code from winerror.h
1998 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1999 * this function does nothing.
2002 * Stub, returns ERROR_NOT_SUPPORTED.
2004 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2006 TRACE("AdapterInfo %p\n", AdapterInfo);
2007 /* not a stub, never going to support this (and I never mark an adapter as
2008 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
2009 return ERROR_NOT_SUPPORTED;
2013 /******************************************************************
2014 * NotifyAddrChange (IPHLPAPI.@)
2016 * Notify caller whenever the ip-interface map is changed.
2019 * Handle [Out] handle usable in asynchronous notification
2020 * overlapped [In] overlapped structure that notifies the caller
2024 * Failure: error code from winerror.h
2027 * Stub, returns ERROR_NOT_SUPPORTED.
2029 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2031 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2032 return ERROR_NOT_SUPPORTED;
2036 /******************************************************************
2037 * NotifyRouteChange (IPHLPAPI.@)
2039 * Notify caller whenever the ip routing table is changed.
2042 * Handle [Out] handle usable in asynchronous notification
2043 * overlapped [In] overlapped structure that notifies the caller
2047 * Failure: error code from winerror.h
2050 * Stub, returns ERROR_NOT_SUPPORTED.
2052 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2054 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2055 return ERROR_NOT_SUPPORTED;
2059 /******************************************************************
2060 * SendARP (IPHLPAPI.@)
2062 * Send an ARP request.
2065 * DestIP [In] attempt to obtain this IP
2066 * SrcIP [In] optional sender IP address
2067 * pMacAddr [Out] buffer for the mac address
2068 * PhyAddrLen [In/Out] length of the output buffer
2072 * Failure: error code from winerror.h
2075 * Stub, returns ERROR_NOT_SUPPORTED.
2077 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2079 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2080 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2081 return ERROR_NOT_SUPPORTED;
2085 /******************************************************************
2086 * SetIfEntry (IPHLPAPI.@)
2088 * Set the administrative status of an interface.
2091 * pIfRow [In] dwAdminStatus member specifies the new status.
2095 * Failure: error code from winerror.h
2098 * Stub, returns ERROR_NOT_SUPPORTED.
2100 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2102 FIXME("(pIfRow %p): stub\n", pIfRow);
2103 /* this is supposed to set an interface administratively up or down.
2104 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2105 this sort of down is indistinguishable from other sorts of down (e.g. no
2107 return ERROR_NOT_SUPPORTED;
2111 /******************************************************************
2112 * SetIpForwardEntry (IPHLPAPI.@)
2114 * Modify an existing route.
2117 * pRoute [In] route with the new information
2121 * Failure: error code from winerror.h
2124 * Stub, returns NO_ERROR.
2126 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2128 FIXME("(pRoute %p): stub\n", pRoute);
2129 /* this is to add a route entry, how's it distinguishable from
2130 CreateIpForwardEntry?
2131 could use SIOCADDRT, not sure I want to */
2136 /******************************************************************
2137 * SetIpNetEntry (IPHLPAPI.@)
2139 * Modify an existing ARP entry.
2142 * pArpEntry [In] ARP entry with the new information
2146 * Failure: error code from winerror.h
2149 * Stub, returns NO_ERROR.
2151 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2153 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2154 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2159 /******************************************************************
2160 * SetIpStatistics (IPHLPAPI.@)
2162 * Toggle IP forwarding and det the default TTL value.
2165 * pIpStats [In] IP statistics with the new information
2169 * Failure: error code from winerror.h
2172 * Stub, returns NO_ERROR.
2174 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2176 FIXME("(pIpStats %p): stub\n", pIpStats);
2181 /******************************************************************
2182 * SetIpTTL (IPHLPAPI.@)
2184 * Set the default TTL value.
2187 * nTTL [In] new TTL value
2191 * Failure: error code from winerror.h
2194 * Stub, returns NO_ERROR.
2196 DWORD WINAPI SetIpTTL(UINT nTTL)
2198 FIXME("(nTTL %d): stub\n", nTTL);
2199 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2200 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2205 /******************************************************************
2206 * SetTcpEntry (IPHLPAPI.@)
2208 * Set the state of a TCP connection.
2211 * pTcpRow [In] specifies connection with new state
2215 * Failure: error code from winerror.h
2218 * Stub, returns NO_ERROR.
2220 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2222 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2227 /******************************************************************
2228 * UnenableRouter (IPHLPAPI.@)
2230 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2231 * if it reaches zero.
2234 * pOverlapped [In/Out] should be the same as in EnableRouter()
2235 * lpdwEnableCount [Out] optional, receives reference count
2239 * Failure: error code from winerror.h
2242 * Stub, returns ERROR_NOT_SUPPORTED.
2244 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2246 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2248 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2249 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2251 return ERROR_NOT_SUPPORTED;