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
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
64 #define IF_NAMESIZE 16
68 #define INADDR_NONE ~0UL
71 /* call res_init() just once because of a bug in Mac OS X 10.4 */
72 /* Call once per thread on systems that have per-thread _res. */
73 static void initialise_resolver(void)
75 if ((_res.options & RES_INIT) == 0)
79 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
82 case DLL_PROCESS_ATTACH:
83 DisableThreadLibraryCalls( hinstDLL );
86 case DLL_PROCESS_DETACH:
92 /******************************************************************
93 * AddIPAddress (IPHLPAPI.@)
95 * Add an IP address to an adapter.
98 * Address [In] IP address to add to the adapter
99 * IpMask [In] subnet mask for the IP address
100 * IfIndex [In] adapter index to add the address
101 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
102 * NTEInstance [Out] NTE instance for the IP address
106 * Failure: error code from winerror.h
109 * Stub. Currently returns ERROR_NOT_SUPPORTED.
111 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
114 return ERROR_NOT_SUPPORTED;
118 /******************************************************************
119 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
121 * Get table of local interfaces.
122 * Like GetIfTable(), but allocate the returned table from heap.
125 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
126 * allocated and returned.
127 * bOrder [In] whether to sort the table
128 * heap [In] heap from which the table is allocated
129 * flags [In] flags to HeapAlloc
132 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
133 * GetIfTable() returns otherwise.
135 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
136 BOOL bOrder, HANDLE heap, DWORD flags)
140 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
141 bOrder, heap, flags);
143 ret = ERROR_INVALID_PARAMETER;
147 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
148 if (ret == ERROR_INSUFFICIENT_BUFFER) {
149 *ppIfTable = HeapAlloc(heap, flags, dwSize);
150 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
153 TRACE("returning %d\n", ret);
158 static int IpAddrTableSorter(const void *a, const void *b)
163 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
170 /******************************************************************
171 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
173 * Get interface-to-IP address mapping table.
174 * Like GetIpAddrTable(), but allocate the returned table from heap.
177 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
178 * allocated and returned.
179 * bOrder [In] whether to sort the table
180 * heap [In] heap from which the table is allocated
181 * flags [In] flags to HeapAlloc
184 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
185 * failure, NO_ERROR on success.
187 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
188 BOOL bOrder, HANDLE heap, DWORD flags)
192 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
193 ppIpAddrTable, bOrder, heap, flags);
194 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
196 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
197 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
198 TRACE("returning %d\n", ret);
203 /******************************************************************
204 * CancelIPChangeNotify (IPHLPAPI.@)
206 * Cancel a previous notification created by NotifyAddrChange or
210 * overlapped [In] overlapped structure that notifies the caller
217 * Stub, returns FALSE.
219 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
221 FIXME("(overlapped %p): stub\n", overlapped);
227 /******************************************************************
228 * CreateIpForwardEntry (IPHLPAPI.@)
230 * Create a route in the local computer's IP table.
233 * pRoute [In] new route information
237 * Failure: error code from winerror.h
240 * Stub, always returns NO_ERROR.
242 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
244 FIXME("(pRoute %p): stub\n", pRoute);
245 /* could use SIOCADDRT, not sure I want to */
250 /******************************************************************
251 * CreateIpNetEntry (IPHLPAPI.@)
253 * Create entry in the ARP table.
256 * pArpEntry [In] new ARP entry
260 * Failure: error code from winerror.h
263 * Stub, always returns NO_ERROR.
265 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
267 FIXME("(pArpEntry %p)\n", pArpEntry);
268 /* could use SIOCSARP on systems that support it, not sure I want to */
273 /******************************************************************
274 * CreateProxyArpEntry (IPHLPAPI.@)
276 * Create a Proxy ARP (PARP) entry for an IP address.
279 * dwAddress [In] IP address for which this computer acts as a proxy.
280 * dwMask [In] subnet mask for dwAddress
281 * dwIfIndex [In] interface index
285 * Failure: error code from winerror.h
288 * Stub, returns ERROR_NOT_SUPPORTED.
290 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
292 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
293 dwAddress, dwMask, dwIfIndex);
294 return ERROR_NOT_SUPPORTED;
298 /******************************************************************
299 * DeleteIPAddress (IPHLPAPI.@)
301 * Delete an IP address added with AddIPAddress().
304 * NTEContext [In] NTE context from AddIPAddress();
308 * Failure: error code from winerror.h
311 * Stub, returns ERROR_NOT_SUPPORTED.
313 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
315 FIXME("(NTEContext %d): stub\n", NTEContext);
316 return ERROR_NOT_SUPPORTED;
320 /******************************************************************
321 * DeleteIpForwardEntry (IPHLPAPI.@)
326 * pRoute [In] route to delete
330 * Failure: error code from winerror.h
333 * Stub, returns NO_ERROR.
335 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
337 FIXME("(pRoute %p): stub\n", pRoute);
338 /* could use SIOCDELRT, not sure I want to */
343 /******************************************************************
344 * DeleteIpNetEntry (IPHLPAPI.@)
346 * Delete an ARP entry.
349 * pArpEntry [In] ARP entry to delete
353 * Failure: error code from winerror.h
356 * Stub, returns NO_ERROR.
358 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
360 FIXME("(pArpEntry %p): stub\n", pArpEntry);
361 /* could use SIOCDARP on systems that support it, not sure I want to */
366 /******************************************************************
367 * DeleteProxyArpEntry (IPHLPAPI.@)
369 * Delete a Proxy ARP entry.
372 * dwAddress [In] IP address for which this computer acts as a proxy.
373 * dwMask [In] subnet mask for dwAddress
374 * dwIfIndex [In] interface index
378 * Failure: error code from winerror.h
381 * Stub, returns ERROR_NOT_SUPPORTED.
383 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
385 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
386 dwAddress, dwMask, dwIfIndex);
387 return ERROR_NOT_SUPPORTED;
391 /******************************************************************
392 * EnableRouter (IPHLPAPI.@)
394 * Turn on ip forwarding.
398 * pOverlapped [In/Out] hEvent member should contain a valid handle.
401 * Success: ERROR_IO_PENDING
402 * Failure: error code from winerror.h
405 * Stub, returns ERROR_NOT_SUPPORTED.
407 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
409 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
410 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
411 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
413 return ERROR_NOT_SUPPORTED;
417 /******************************************************************
418 * FlushIpNetTable (IPHLPAPI.@)
420 * Delete all ARP entries of an interface
423 * dwIfIndex [In] interface index
427 * Failure: error code from winerror.h
430 * Stub, returns ERROR_NOT_SUPPORTED.
432 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
434 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
435 /* this flushes the arp cache of the given index */
436 return ERROR_NOT_SUPPORTED;
440 /******************************************************************
441 * GetAdapterIndex (IPHLPAPI.@)
443 * Get interface index from its name.
446 * AdapterName [In] unicode string with the adapter name
447 * IfIndex [Out] returns found interface index
451 * Failure: error code from winerror.h
453 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
455 char adapterName[MAX_ADAPTER_NAME];
459 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
460 /* The adapter name is guaranteed not to have any unicode characters, so
461 * this translation is never lossy */
462 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
463 adapterName[i] = (char)AdapterName[i];
464 adapterName[i] = '\0';
465 ret = getInterfaceIndexByName(adapterName, IfIndex);
466 TRACE("returning %d\n", ret);
471 /******************************************************************
472 * GetAdaptersInfo (IPHLPAPI.@)
474 * Get information about adapters.
477 * pAdapterInfo [Out] buffer for adapter infos
478 * pOutBufLen [In] length of output buffer
482 * Failure: error code from winerror.h
484 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
488 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
490 ret = ERROR_INVALID_PARAMETER;
492 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
494 if (numNonLoopbackInterfaces > 0) {
495 DWORD numIPAddresses = getNumIPAddresses();
498 /* This may slightly overestimate the amount of space needed, because
499 * the IP addresses include the loopback address, but it's easier
500 * to make sure there's more than enough space than to make sure there's
501 * precisely enough space.
503 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
504 size += numIPAddresses * sizeof(IP_ADDR_STRING);
505 if (!pAdapterInfo || *pOutBufLen < size) {
507 ret = ERROR_BUFFER_OVERFLOW;
510 InterfaceIndexTable *table = NULL;
511 PMIB_IPADDRTABLE ipAddrTable = NULL;
512 PMIB_IPFORWARDTABLE routeTable = NULL;
514 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
516 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
518 table = getNonLoopbackInterfaceIndexTable();
520 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
521 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
522 if (*pOutBufLen < size) {
524 ret = ERROR_INSUFFICIENT_BUFFER;
529 BOOL winsEnabled = FALSE;
530 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
531 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
532 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
534 memset(pAdapterInfo, 0, size);
535 /* @@ Wine registry key: HKCU\Software\Wine\Network */
536 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
537 &hKey) == ERROR_SUCCESS) {
538 DWORD size = sizeof(primaryWINS.String);
541 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
542 (LPBYTE)primaryWINS.String, &size);
543 addr = inet_addr(primaryWINS.String);
544 if (addr != INADDR_NONE && addr != INADDR_ANY)
546 size = sizeof(secondaryWINS.String);
547 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
548 (LPBYTE)secondaryWINS.String, &size);
549 addr = inet_addr(secondaryWINS.String);
550 if (addr != INADDR_NONE && addr != INADDR_ANY)
554 for (ndx = 0; ndx < table->numIndexes; ndx++) {
555 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
557 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
558 BOOL firstIPAddr = TRUE;
560 /* on Win98 this is left empty, but whatever */
561 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
562 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
563 ptr->AddressLength = sizeof(ptr->Address);
564 getInterfacePhysicalByIndex(table->indexes[ndx],
565 &ptr->AddressLength, ptr->Address, &ptr->Type);
566 ptr->Index = table->indexes[ndx];
567 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
568 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
570 toIPAddressString(ipAddrTable->table[i].dwAddr,
571 ptr->IpAddressList.IpAddress.String);
572 toIPAddressString(ipAddrTable->table[i].dwMask,
573 ptr->IpAddressList.IpMask.String);
577 currentIPAddr->Next = nextIPAddr;
578 currentIPAddr = nextIPAddr;
579 toIPAddressString(ipAddrTable->table[i].dwAddr,
580 currentIPAddr->IpAddress.String);
581 toIPAddressString(ipAddrTable->table[i].dwMask,
582 currentIPAddr->IpMask.String);
587 /* Find first router through this interface, which we'll assume
588 * is the default gateway for this adapter */
589 for (i = 0; i < routeTable->dwNumEntries; i++)
590 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
591 && routeTable->table[i].dwForwardType ==
592 MIB_IPROUTE_TYPE_INDIRECT)
593 toIPAddressString(routeTable->table[i].dwForwardNextHop,
594 ptr->GatewayList.IpAddress.String);
596 ptr->HaveWins = TRUE;
597 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
598 primaryWINS.String, sizeof(primaryWINS.String));
599 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
600 secondaryWINS.String, sizeof(secondaryWINS.String));
602 if (ndx < table->numIndexes - 1)
603 ptr->Next = &pAdapterInfo[ndx + 1];
609 HeapFree(GetProcessHeap(), 0, table);
612 ret = ERROR_OUTOFMEMORY;
613 HeapFree(GetProcessHeap(), 0, routeTable);
614 HeapFree(GetProcessHeap(), 0, ipAddrTable);
620 TRACE("returning %d\n", ret);
624 static DWORD typeFromMibType(DWORD mib_type)
628 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
629 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
630 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
631 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
632 default: return IF_TYPE_OTHER;
636 static DWORD connectionTypeFromMibType(DWORD mib_type)
640 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
641 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
642 default: return NET_IF_CONNECTION_DEDICATED;
646 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
652 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
653 for (i = 0; i < at->dwNumEntries; i++)
655 if (at->table[i].dwIndex == index) (*num_addrs)++;
657 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
659 HeapFree(GetProcessHeap(), 0, at);
660 return ERROR_OUTOFMEMORY;
662 for (i = 0, j = 0; i < at->dwNumEntries; i++)
664 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
666 HeapFree(GetProcessHeap(), 0, at);
667 return ERROR_SUCCESS;
670 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
675 for (addrp = (const BYTE *)in_addr;
676 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
679 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
680 sprintf(p, "%d", *addrp);
682 p += sprintf(p, "%d.", *addrp);
687 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
689 const IN6_ADDR *addr = &sin->sin6_addr;
692 BOOL in_zero = FALSE;
694 for (i = 0; i < 7; i++)
696 if (!addr->u.Word[i])
708 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
712 sprintf(p, "%x", ntohs(addr->u.Word[7]));
716 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
718 DWORD i, num_gateways = 0;
720 for (i = 0; i < routeTable->dwNumEntries; i++)
722 if (routeTable->table[i].dwForwardIfIndex == index &&
723 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
729 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
730 PMIB_IPFORWARDTABLE routeTable)
733 PMIB_IPFORWARDROW row = NULL;
735 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
737 if (routeTable->table[i].dwForwardIfIndex == index &&
738 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
739 row = &routeTable->table[i];
744 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
745 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
747 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
748 DWORD *v4addrs = NULL;
749 SOCKET_ADDRESS *v6addrs = NULL;
750 PMIB_IPFORWARDTABLE routeTable = NULL;
752 if (family == WS_AF_INET)
754 if (!(flags & GAA_FLAG_SKIP_UNICAST))
755 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
756 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
758 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
759 GetProcessHeap(), 0);
761 num_v4_gateways = count_v4_gateways(index, routeTable);
764 else if (family == WS_AF_INET6)
766 if (!(flags & GAA_FLAG_SKIP_UNICAST))
767 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
769 else if (family == WS_AF_UNSPEC)
771 if (!(flags & GAA_FLAG_SKIP_UNICAST))
772 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
773 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
775 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
776 GetProcessHeap(), 0);
779 num_v4_gateways = count_v4_gateways(index, routeTable);
780 if (!(flags & GAA_FLAG_SKIP_UNICAST))
781 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
787 FIXME("address family %u unsupported\n", family);
792 HeapFree(GetProcessHeap(), 0, routeTable);
796 total_size = sizeof(IP_ADAPTER_ADDRESSES);
797 total_size += IF_NAMESIZE;
798 total_size += IF_NAMESIZE * sizeof(WCHAR);
799 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
800 total_size += IF_NAMESIZE * sizeof(WCHAR);
801 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
802 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
803 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
804 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
805 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
806 for (i = 0; i < num_v6addrs; i++)
807 total_size += v6addrs[i].iSockaddrLength;
809 if (aa && *size >= total_size)
811 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
813 DWORD buflen, type, status;
815 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
816 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
817 aa->u.s.IfIndex = index;
819 getInterfaceNameByIndex(index, name);
820 memcpy(ptr, name, IF_NAMESIZE);
821 aa->AdapterName = ptr;
823 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
825 aa->FriendlyName = (WCHAR *)ptr;
826 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
831 aa->Description = (WCHAR *)ptr;
832 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
837 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
841 PMIB_IPFORWARDROW adapterRow;
843 if ((adapterRow = findIPv4Gateway(index, routeTable)))
845 PIP_ADAPTER_GATEWAY_ADDRESS gw;
848 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
849 aa->FirstGatewayAddress = gw;
851 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
852 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
853 sin = (PSOCKADDR_IN)ptr;
854 sin->sin_family = AF_INET;
856 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
858 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
859 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
861 ptr += sizeof(SOCKADDR_IN);
866 IP_ADAPTER_UNICAST_ADDRESS *ua;
867 struct sockaddr_in *sa;
868 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
869 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
870 for (i = 0; i < num_v4addrs; i++)
874 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
875 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
876 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
877 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
879 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
880 sa->sin_family = AF_INET;
881 sa->sin_addr.s_addr = v4addrs[i];
883 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
884 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
886 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
887 if (i < num_v4addrs - 1)
889 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
896 IP_ADAPTER_UNICAST_ADDRESS *ua;
897 struct WS_sockaddr_in6 *sa;
899 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
900 if (aa->FirstUnicastAddress)
902 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
904 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
905 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
908 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
909 for (i = 0; i < num_v6addrs; i++)
913 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
914 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
915 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
916 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
918 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
919 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
920 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
921 debugstr_ipv6(sa, addr_buf));
923 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
924 if (i < num_v6addrs - 1)
926 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
932 buflen = MAX_INTERFACE_PHYSADDR;
933 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
934 aa->PhysicalAddressLength = buflen;
935 aa->IfType = typeFromMibType(type);
936 aa->ConnectionType = connectionTypeFromMibType(type);
938 getInterfaceMtuByName(name, &aa->Mtu);
940 getInterfaceStatusByName(name, &status);
941 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
942 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
943 else aa->OperStatus = IfOperStatusUnknown;
946 HeapFree(GetProcessHeap(), 0, routeTable);
947 HeapFree(GetProcessHeap(), 0, v6addrs);
948 HeapFree(GetProcessHeap(), 0, v4addrs);
949 return ERROR_SUCCESS;
952 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
956 initialise_resolver();
957 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
958 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
961 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
962 if (!address || *len < size)
965 return ERROR_BUFFER_OVERFLOW;
968 if (_res.nscount > 0)
970 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
973 for (i = 0, addr = address; i < _res.nscount && addr;
974 i++, addr = addr->Next)
978 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
979 addr->Address.lpSockaddr =
980 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
981 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
982 sin->sin_family = WS_AF_INET;
983 sin->sin_port = _res.nsaddr_list[i].sin_port;
984 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
985 if (i == _res.nscount - 1)
989 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
990 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
993 return ERROR_SUCCESS;
996 static BOOL is_ip_address_string(const char *str)
1001 ret = inet_aton(str, &in);
1005 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1008 char *found_suffix = NULL;
1010 initialise_resolver();
1011 /* Always return a NULL-terminated string, even if it's empty. */
1012 size = sizeof(WCHAR);
1013 for (i = 0, found_suffix = NULL;
1014 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1016 /* This uses a heuristic to select a DNS suffix:
1017 * the first, non-IP address string is selected.
1019 if (!is_ip_address_string(_res.dnsrch[i]))
1020 found_suffix = _res.dnsrch[i];
1023 size += strlen(found_suffix) * sizeof(WCHAR);
1024 if (!suffix || *len < size)
1027 return ERROR_BUFFER_OVERFLOW;
1034 for (p = found_suffix; *p; p++)
1038 return ERROR_SUCCESS;
1041 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1042 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1044 InterfaceIndexTable *table;
1045 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1047 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1049 if (!buflen) return ERROR_INVALID_PARAMETER;
1051 table = getInterfaceIndexTable();
1052 if (!table || !table->numIndexes)
1054 HeapFree(GetProcessHeap(), 0, table);
1055 return ERROR_NO_DATA;
1058 for (i = 0; i < table->numIndexes; i++)
1061 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1063 HeapFree(GetProcessHeap(), 0, table);
1068 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1070 /* Since DNS servers aren't really per adapter, get enough space for a
1071 * single copy of them.
1073 get_dns_server_addresses(NULL, &dns_server_size);
1074 total_size += dns_server_size;
1076 /* Since DNS suffix also isn't really per adapter, get enough space for a
1077 * single copy of it.
1079 get_dns_suffix(NULL, &dns_suffix_size);
1080 total_size += dns_suffix_size;
1081 if (aa && *buflen >= total_size)
1083 ULONG bytes_left = size = total_size;
1084 PIP_ADAPTER_ADDRESSES first_aa = aa;
1085 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1088 for (i = 0; i < table->numIndexes; i++)
1090 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1092 HeapFree(GetProcessHeap(), 0, table);
1095 if (i < table->numIndexes - 1)
1097 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1099 size = bytes_left -= size;
1102 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1104 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1105 get_dns_server_addresses(firstDns, &dns_server_size);
1106 for (aa = first_aa; aa; aa = aa->Next)
1108 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1109 aa->FirstDnsServerAddress = firstDns;
1113 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1114 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1115 for (; aa; aa = aa->Next)
1117 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1118 aa->DnsSuffix = dnsSuffix;
1120 aa->DnsSuffix = (WCHAR *)((BYTE*)dnsSuffix + dns_suffix_size - 2);
1122 ret = ERROR_SUCCESS;
1125 ret = ERROR_BUFFER_OVERFLOW;
1126 *buflen = total_size;
1128 TRACE("num adapters %u\n", table->numIndexes);
1129 HeapFree(GetProcessHeap(), 0, table);
1133 /******************************************************************
1134 * GetBestInterface (IPHLPAPI.@)
1136 * Get the interface, with the best route for the given IP address.
1139 * dwDestAddr [In] IP address to search the interface for
1140 * pdwBestIfIndex [Out] found best interface
1144 * Failure: error code from winerror.h
1146 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1148 struct WS_sockaddr_in sa_in;
1149 memset(&sa_in, 0, sizeof(sa_in));
1150 sa_in.sin_family = AF_INET;
1151 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1152 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1155 /******************************************************************
1156 * GetBestInterfaceEx (IPHLPAPI.@)
1158 * Get the interface, with the best route for the given IP address.
1161 * dwDestAddr [In] IP address to search the interface for
1162 * pdwBestIfIndex [Out] found best interface
1166 * Failure: error code from winerror.h
1168 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1172 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1173 if (!pDestAddr || !pdwBestIfIndex)
1174 ret = ERROR_INVALID_PARAMETER;
1176 MIB_IPFORWARDROW ipRow;
1178 if (pDestAddr->sa_family == AF_INET) {
1179 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1180 if (ret == ERROR_SUCCESS)
1181 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1183 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1184 ret = ERROR_NOT_SUPPORTED;
1187 TRACE("returning %d\n", ret);
1192 /******************************************************************
1193 * GetBestRoute (IPHLPAPI.@)
1195 * Get the best route for the given IP address.
1198 * dwDestAddr [In] IP address to search the best route for
1199 * dwSourceAddr [In] optional source IP address
1200 * pBestRoute [Out] found best route
1204 * Failure: error code from winerror.h
1206 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1208 PMIB_IPFORWARDTABLE table;
1211 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1212 dwSourceAddr, pBestRoute);
1214 return ERROR_INVALID_PARAMETER;
1216 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1218 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1220 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1221 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1222 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1223 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1224 DWORD numShifts, mask;
1226 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1227 mask && mask & 1; mask >>= 1, numShifts++)
1229 if (numShifts > matchedBits) {
1230 matchedBits = numShifts;
1233 else if (!matchedBits) {
1238 if (matchedNdx < table->dwNumEntries) {
1239 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1240 ret = ERROR_SUCCESS;
1243 /* No route matches, which can happen if there's no default route. */
1244 ret = ERROR_HOST_UNREACHABLE;
1246 HeapFree(GetProcessHeap(), 0, table);
1248 TRACE("returning %d\n", ret);
1253 /******************************************************************
1254 * GetFriendlyIfIndex (IPHLPAPI.@)
1256 * Get a "friendly" version of IfIndex, which is one that doesn't
1257 * have the top byte set. Doesn't validate whether IfIndex is a valid
1261 * IfIndex [In] interface index to get the friendly one for
1264 * A friendly version of IfIndex.
1266 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1268 /* windows doesn't validate these, either, just makes sure the top byte is
1269 cleared. I assume my ifenum module never gives an index with the top
1271 TRACE("returning %d\n", IfIndex);
1276 /******************************************************************
1277 * GetIfEntry (IPHLPAPI.@)
1279 * Get information about an interface.
1282 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1283 * Out: interface information
1287 * Failure: error code from winerror.h
1289 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1292 char nameBuf[MAX_ADAPTER_NAME];
1295 TRACE("pIfRow %p\n", pIfRow);
1297 return ERROR_INVALID_PARAMETER;
1299 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1301 ret = getInterfaceEntryByName(name, pIfRow);
1302 if (ret == NO_ERROR)
1303 ret = getInterfaceStatsByName(name, pIfRow);
1306 ret = ERROR_INVALID_DATA;
1307 TRACE("returning %d\n", ret);
1312 static int IfTableSorter(const void *a, const void *b)
1317 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1324 /******************************************************************
1325 * GetIfTable (IPHLPAPI.@)
1327 * Get a table of local interfaces.
1330 * pIfTable [Out] buffer for local interfaces table
1331 * pdwSize [In/Out] length of output buffer
1332 * bOrder [In] whether to sort the table
1336 * Failure: error code from winerror.h
1339 * If pdwSize is less than required, the function will return
1340 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1342 * If bOrder is true, the returned table will be sorted by interface index.
1344 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1348 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1351 ret = ERROR_INVALID_PARAMETER;
1353 DWORD numInterfaces = getNumInterfaces();
1354 ULONG size = sizeof(MIB_IFTABLE);
1356 if (numInterfaces > 1)
1357 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1358 if (!pIfTable || *pdwSize < size) {
1360 ret = ERROR_INSUFFICIENT_BUFFER;
1363 InterfaceIndexTable *table = getInterfaceIndexTable();
1366 size = sizeof(MIB_IFTABLE);
1367 if (table->numIndexes > 1)
1368 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1369 if (*pdwSize < size) {
1371 ret = ERROR_INSUFFICIENT_BUFFER;
1377 pIfTable->dwNumEntries = 0;
1378 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1379 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1380 GetIfEntry(&pIfTable->table[ndx]);
1381 pIfTable->dwNumEntries++;
1384 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1388 HeapFree(GetProcessHeap(), 0, table);
1391 ret = ERROR_OUTOFMEMORY;
1394 TRACE("returning %d\n", ret);
1399 /******************************************************************
1400 * GetInterfaceInfo (IPHLPAPI.@)
1402 * Get a list of network interface adapters.
1405 * pIfTable [Out] buffer for interface adapters
1406 * dwOutBufLen [Out] if buffer is too small, returns required size
1410 * Failure: error code from winerror.h
1413 * MSDN states this should return non-loopback interfaces only.
1415 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1419 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1421 ret = ERROR_INVALID_PARAMETER;
1423 DWORD numInterfaces = getNumInterfaces();
1424 ULONG size = sizeof(IP_INTERFACE_INFO);
1426 if (numInterfaces > 1)
1427 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1428 if (!pIfTable || *dwOutBufLen < size) {
1429 *dwOutBufLen = size;
1430 ret = ERROR_INSUFFICIENT_BUFFER;
1433 InterfaceIndexTable *table = getInterfaceIndexTable();
1436 size = sizeof(IP_INTERFACE_INFO);
1437 if (table->numIndexes > 1)
1438 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1439 if (*dwOutBufLen < size) {
1440 *dwOutBufLen = size;
1441 ret = ERROR_INSUFFICIENT_BUFFER;
1445 char nameBuf[MAX_ADAPTER_NAME];
1447 *dwOutBufLen = size;
1448 pIfTable->NumAdapters = 0;
1449 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1450 const char *walker, *name;
1453 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1454 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1455 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1456 walker && *walker &&
1457 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1458 walker++, assigner++)
1459 *assigner = *walker;
1461 pIfTable->NumAdapters++;
1465 HeapFree(GetProcessHeap(), 0, table);
1468 ret = ERROR_OUTOFMEMORY;
1471 TRACE("returning %d\n", ret);
1476 /******************************************************************
1477 * GetIpAddrTable (IPHLPAPI.@)
1479 * Get interface-to-IP address mapping table.
1482 * pIpAddrTable [Out] buffer for mapping table
1483 * pdwSize [In/Out] length of output buffer
1484 * bOrder [In] whether to sort the table
1488 * Failure: error code from winerror.h
1491 * If pdwSize is less than required, the function will return
1492 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1494 * If bOrder is true, the returned table will be sorted by the next hop and
1495 * an assortment of arbitrary parameters.
1497 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1501 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1504 ret = ERROR_INVALID_PARAMETER;
1506 PMIB_IPADDRTABLE table;
1508 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1509 if (ret == NO_ERROR)
1511 ULONG size = sizeof(MIB_IPADDRTABLE);
1513 if (table->dwNumEntries > 1)
1514 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1515 if (!pIpAddrTable || *pdwSize < size) {
1517 ret = ERROR_INSUFFICIENT_BUFFER;
1521 memcpy(pIpAddrTable, table, size);
1523 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1524 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1527 HeapFree(GetProcessHeap(), 0, table);
1530 TRACE("returning %d\n", ret);
1535 /******************************************************************
1536 * GetIpForwardTable (IPHLPAPI.@)
1538 * Get the route table.
1541 * pIpForwardTable [Out] buffer for route table
1542 * pdwSize [In/Out] length of output buffer
1543 * bOrder [In] whether to sort the table
1547 * Failure: error code from winerror.h
1550 * If pdwSize is less than required, the function will return
1551 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1553 * If bOrder is true, the returned table will be sorted by the next hop and
1554 * an assortment of arbitrary parameters.
1556 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1559 PMIB_IPFORWARDTABLE table;
1561 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1563 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1565 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1567 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1568 if (!pIpForwardTable || *pdwSize < size) {
1570 ret = ERROR_INSUFFICIENT_BUFFER;
1574 memcpy(pIpForwardTable, table, size);
1576 HeapFree(GetProcessHeap(), 0, table);
1578 TRACE("returning %d\n", ret);
1583 /******************************************************************
1584 * GetIpNetTable (IPHLPAPI.@)
1586 * Get the IP-to-physical address mapping table.
1589 * pIpNetTable [Out] buffer for mapping table
1590 * pdwSize [In/Out] length of output buffer
1591 * bOrder [In] whether to sort the table
1595 * Failure: error code from winerror.h
1598 * If pdwSize is less than required, the function will return
1599 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1601 * If bOrder is true, the returned table will be sorted by IP address.
1603 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1606 PMIB_IPNETTABLE table;
1608 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1610 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1612 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1614 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1615 if (!pIpNetTable || *pdwSize < size) {
1617 ret = ERROR_INSUFFICIENT_BUFFER;
1621 memcpy(pIpNetTable, table, size);
1623 HeapFree(GetProcessHeap(), 0, table);
1625 TRACE("returning %d\n", ret);
1629 /* Gets the DNS server list into the list beginning at list. Assumes that
1630 * a single server address may be placed at list if *len is at least
1631 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1632 * and assumes that all remaining DNS servers are contiguously located
1633 * beginning at firstDynamic. On input, *len is assumed to be the total number
1634 * of bytes available for all DNS servers, and is ignored if list is NULL.
1635 * On return, *len is set to the total number of bytes required for all DNS
1637 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1638 * ERROR_SUCCESS otherwise.
1640 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1641 PIP_ADDR_STRING firstDynamic, DWORD *len)
1645 initialise_resolver();
1646 size = _res.nscount * sizeof(IP_ADDR_STRING);
1647 if (!list || *len < size) {
1649 return ERROR_BUFFER_OVERFLOW;
1652 if (_res.nscount > 0) {
1653 PIP_ADDR_STRING ptr;
1656 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1657 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1658 ptr->IpAddress.String);
1659 if (i == _res.nscount - 1)
1662 ptr->Next = firstDynamic;
1664 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1667 return ERROR_SUCCESS;
1670 /******************************************************************
1671 * GetNetworkParams (IPHLPAPI.@)
1673 * Get the network parameters for the local computer.
1676 * pFixedInfo [Out] buffer for network parameters
1677 * pOutBufLen [In/Out] length of output buffer
1681 * Failure: error code from winerror.h
1684 * If pOutBufLen is less than required, the function will return
1685 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1688 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1690 DWORD ret, size, serverListSize;
1694 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1696 return ERROR_INVALID_PARAMETER;
1698 get_dns_server_list(NULL, NULL, &serverListSize);
1699 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1700 if (!pFixedInfo || *pOutBufLen < size) {
1702 return ERROR_BUFFER_OVERFLOW;
1705 memset(pFixedInfo, 0, size);
1706 size = sizeof(pFixedInfo->HostName);
1707 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1708 size = sizeof(pFixedInfo->DomainName);
1709 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1710 get_dns_server_list(&pFixedInfo->DnsServerList,
1711 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1713 /* Assume the first DNS server in the list is the "current" DNS server: */
1714 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1715 pFixedInfo->NodeType = HYBRID_NODETYPE;
1716 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1717 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1718 if (regReturn != ERROR_SUCCESS)
1719 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1720 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1722 if (regReturn == ERROR_SUCCESS)
1724 DWORD size = sizeof(pFixedInfo->ScopeId);
1726 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1730 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1731 I suppose could also check for a listener on port 53 to set EnableDns */
1733 TRACE("returning %d\n", ret);
1738 /******************************************************************
1739 * GetNumberOfInterfaces (IPHLPAPI.@)
1741 * Get the number of interfaces.
1744 * pdwNumIf [Out] number of interfaces
1747 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1749 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1753 TRACE("pdwNumIf %p\n", pdwNumIf);
1755 ret = ERROR_INVALID_PARAMETER;
1757 *pdwNumIf = getNumInterfaces();
1760 TRACE("returning %d\n", ret);
1765 /******************************************************************
1766 * GetPerAdapterInfo (IPHLPAPI.@)
1768 * Get information about an adapter corresponding to an interface.
1771 * IfIndex [In] interface info
1772 * pPerAdapterInfo [Out] buffer for per adapter info
1773 * pOutBufLen [In/Out] length of output buffer
1777 * Failure: error code from winerror.h
1779 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1781 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1782 DWORD ret = NO_ERROR;
1784 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1786 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1788 if (!isIfIndexLoopback(IfIndex)) {
1789 get_dns_server_list(NULL, NULL, &serverListSize);
1790 if (serverListSize > sizeof(IP_ADDR_STRING))
1791 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1793 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1795 *pOutBufLen = bytesNeeded;
1796 return ERROR_BUFFER_OVERFLOW;
1799 memset(pPerAdapterInfo, 0, bytesNeeded);
1800 if (!isIfIndexLoopback(IfIndex)) {
1801 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1802 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1804 /* Assume the first DNS server in the list is the "current" DNS server: */
1805 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1811 /******************************************************************
1812 * GetRTTAndHopCount (IPHLPAPI.@)
1814 * Get round-trip time (RTT) and hop count.
1818 * DestIpAddress [In] destination address to get the info for
1819 * HopCount [Out] retrieved hop count
1820 * MaxHops [In] maximum hops to search for the destination
1821 * RTT [Out] RTT in milliseconds
1828 * Stub, returns FALSE.
1830 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1832 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1833 DestIpAddress, HopCount, MaxHops, RTT);
1838 /******************************************************************
1839 * GetTcpTable (IPHLPAPI.@)
1841 * Get the table of active TCP connections.
1844 * pTcpTable [Out] buffer for TCP connections table
1845 * pdwSize [In/Out] length of output buffer
1846 * bOrder [In] whether to order the table
1850 * Failure: error code from winerror.h
1853 * If pdwSize is less than required, the function will return
1854 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1855 * the required byte size.
1856 * If bOrder is true, the returned table will be sorted, first by
1857 * local address and port number, then by remote address and port
1860 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1863 PMIB_TCPTABLE table;
1865 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1867 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1869 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1871 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1872 if (!pTcpTable || *pdwSize < size) {
1874 ret = ERROR_INSUFFICIENT_BUFFER;
1878 memcpy(pTcpTable, table, size);
1880 HeapFree(GetProcessHeap(), 0, table);
1882 TRACE("returning %d\n", ret);
1887 /******************************************************************
1888 * GetUdpTable (IPHLPAPI.@)
1890 * Get a table of active UDP connections.
1893 * pUdpTable [Out] buffer for UDP connections table
1894 * pdwSize [In/Out] length of output buffer
1895 * bOrder [In] whether to order the table
1899 * Failure: error code from winerror.h
1902 * If pdwSize is less than required, the function will return
1903 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1904 * required byte size.
1905 * If bOrder is true, the returned table will be sorted, first by
1906 * local address, then by local port number.
1908 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1911 PMIB_UDPTABLE table;
1913 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1915 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1917 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1919 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1920 if (!pUdpTable || *pdwSize < size) {
1922 ret = ERROR_INSUFFICIENT_BUFFER;
1926 memcpy(pUdpTable, table, size);
1928 HeapFree(GetProcessHeap(), 0, table);
1930 TRACE("returning %d\n", ret);
1935 /******************************************************************
1936 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1938 * This is a Win98-only function to get information on "unidirectional"
1939 * adapters. Since this is pretty nonsensical in other contexts, it
1940 * never returns anything.
1943 * pIPIfInfo [Out] buffer for adapter infos
1944 * dwOutBufLen [Out] length of the output buffer
1948 * Failure: error code from winerror.h
1951 * Stub, returns ERROR_NOT_SUPPORTED.
1953 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1955 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1956 /* a unidirectional adapter?? not bloody likely! */
1957 return ERROR_NOT_SUPPORTED;
1961 /******************************************************************
1962 * IpReleaseAddress (IPHLPAPI.@)
1964 * Release an IP obtained through DHCP,
1967 * AdapterInfo [In] adapter to release IP address
1971 * Failure: error code from winerror.h
1974 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1975 * this function does nothing.
1978 * Stub, returns ERROR_NOT_SUPPORTED.
1980 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1982 TRACE("AdapterInfo %p\n", AdapterInfo);
1983 /* not a stub, never going to support this (and I never mark an adapter as
1984 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1985 return ERROR_NOT_SUPPORTED;
1989 /******************************************************************
1990 * IpRenewAddress (IPHLPAPI.@)
1992 * Renew an IP obtained through DHCP.
1995 * AdapterInfo [In] adapter to renew IP address
1999 * Failure: error code from winerror.h
2002 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2003 * this function does nothing.
2006 * Stub, returns ERROR_NOT_SUPPORTED.
2008 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2010 TRACE("AdapterInfo %p\n", AdapterInfo);
2011 /* not a stub, never going to support this (and I never mark an adapter as
2012 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
2013 return ERROR_NOT_SUPPORTED;
2017 /******************************************************************
2018 * NotifyAddrChange (IPHLPAPI.@)
2020 * Notify caller whenever the ip-interface map is changed.
2023 * Handle [Out] handle usable in asynchronous notification
2024 * overlapped [In] overlapped structure that notifies the caller
2028 * Failure: error code from winerror.h
2031 * Stub, returns ERROR_NOT_SUPPORTED.
2033 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2035 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2036 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2037 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2038 return ERROR_IO_PENDING;
2042 /******************************************************************
2043 * NotifyRouteChange (IPHLPAPI.@)
2045 * Notify caller whenever the ip routing table is changed.
2048 * Handle [Out] handle usable in asynchronous notification
2049 * overlapped [In] overlapped structure that notifies the caller
2053 * Failure: error code from winerror.h
2056 * Stub, returns ERROR_NOT_SUPPORTED.
2058 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2060 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2061 return ERROR_NOT_SUPPORTED;
2065 /******************************************************************
2066 * SendARP (IPHLPAPI.@)
2068 * Send an ARP request.
2071 * DestIP [In] attempt to obtain this IP
2072 * SrcIP [In] optional sender IP address
2073 * pMacAddr [Out] buffer for the mac address
2074 * PhyAddrLen [In/Out] length of the output buffer
2078 * Failure: error code from winerror.h
2081 * Stub, returns ERROR_NOT_SUPPORTED.
2083 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2085 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2086 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2087 return ERROR_NOT_SUPPORTED;
2091 /******************************************************************
2092 * SetIfEntry (IPHLPAPI.@)
2094 * Set the administrative status of an interface.
2097 * pIfRow [In] dwAdminStatus member specifies the new status.
2101 * Failure: error code from winerror.h
2104 * Stub, returns ERROR_NOT_SUPPORTED.
2106 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2108 FIXME("(pIfRow %p): stub\n", pIfRow);
2109 /* this is supposed to set an interface administratively up or down.
2110 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2111 this sort of down is indistinguishable from other sorts of down (e.g. no
2113 return ERROR_NOT_SUPPORTED;
2117 /******************************************************************
2118 * SetIpForwardEntry (IPHLPAPI.@)
2120 * Modify an existing route.
2123 * pRoute [In] route with the new information
2127 * Failure: error code from winerror.h
2130 * Stub, returns NO_ERROR.
2132 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2134 FIXME("(pRoute %p): stub\n", pRoute);
2135 /* this is to add a route entry, how's it distinguishable from
2136 CreateIpForwardEntry?
2137 could use SIOCADDRT, not sure I want to */
2142 /******************************************************************
2143 * SetIpNetEntry (IPHLPAPI.@)
2145 * Modify an existing ARP entry.
2148 * pArpEntry [In] ARP entry with the new information
2152 * Failure: error code from winerror.h
2155 * Stub, returns NO_ERROR.
2157 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2159 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2160 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2165 /******************************************************************
2166 * SetIpStatistics (IPHLPAPI.@)
2168 * Toggle IP forwarding and det the default TTL value.
2171 * pIpStats [In] IP statistics with the new information
2175 * Failure: error code from winerror.h
2178 * Stub, returns NO_ERROR.
2180 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2182 FIXME("(pIpStats %p): stub\n", pIpStats);
2187 /******************************************************************
2188 * SetIpTTL (IPHLPAPI.@)
2190 * Set the default TTL value.
2193 * nTTL [In] new TTL value
2197 * Failure: error code from winerror.h
2200 * Stub, returns NO_ERROR.
2202 DWORD WINAPI SetIpTTL(UINT nTTL)
2204 FIXME("(nTTL %d): stub\n", nTTL);
2205 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2206 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2211 /******************************************************************
2212 * SetTcpEntry (IPHLPAPI.@)
2214 * Set the state of a TCP connection.
2217 * pTcpRow [In] specifies connection with new state
2221 * Failure: error code from winerror.h
2224 * Stub, returns NO_ERROR.
2226 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2228 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2233 /******************************************************************
2234 * UnenableRouter (IPHLPAPI.@)
2236 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2237 * if it reaches zero.
2240 * pOverlapped [In/Out] should be the same as in EnableRouter()
2241 * lpdwEnableCount [Out] optional, receives reference count
2245 * Failure: error code from winerror.h
2248 * Stub, returns ERROR_NOT_SUPPORTED.
2250 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2252 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2254 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2255 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2257 return ERROR_NOT_SUPPORTED;