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
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
65 #define IF_NAMESIZE 16
69 #define INADDR_NONE ~0UL
72 /* call res_init() just once because of a bug in Mac OS X 10.4 */
73 /* Call once per thread on systems that have per-thread _res. */
74 static void initialise_resolver(void)
76 if ((_res.options & RES_INIT) == 0)
80 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
83 case DLL_PROCESS_ATTACH:
84 DisableThreadLibraryCalls( hinstDLL );
87 case DLL_PROCESS_DETACH:
93 /******************************************************************
94 * AddIPAddress (IPHLPAPI.@)
96 * Add an IP address to an adapter.
99 * Address [In] IP address to add to the adapter
100 * IpMask [In] subnet mask for the IP address
101 * IfIndex [In] adapter index to add the address
102 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
103 * NTEInstance [Out] NTE instance for the IP address
107 * Failure: error code from winerror.h
110 * Stub. Currently returns ERROR_NOT_SUPPORTED.
112 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
115 return ERROR_NOT_SUPPORTED;
119 /******************************************************************
120 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
122 * Get table of local interfaces.
123 * Like GetIfTable(), but allocate the returned table from heap.
126 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
127 * allocated and returned.
128 * bOrder [In] whether to sort the table
129 * heap [In] heap from which the table is allocated
130 * flags [In] flags to HeapAlloc
133 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
134 * GetIfTable() returns otherwise.
136 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
137 BOOL bOrder, HANDLE heap, DWORD flags)
141 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
142 bOrder, heap, flags);
144 ret = ERROR_INVALID_PARAMETER;
148 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
149 if (ret == ERROR_INSUFFICIENT_BUFFER) {
150 *ppIfTable = HeapAlloc(heap, flags, dwSize);
151 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
154 TRACE("returning %d\n", ret);
159 static int IpAddrTableSorter(const void *a, const void *b)
164 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
171 /******************************************************************
172 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
174 * Get interface-to-IP address mapping table.
175 * Like GetIpAddrTable(), but allocate the returned table from heap.
178 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
179 * allocated and returned.
180 * bOrder [In] whether to sort the table
181 * heap [In] heap from which the table is allocated
182 * flags [In] flags to HeapAlloc
185 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
186 * failure, NO_ERROR on success.
188 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
189 BOOL bOrder, HANDLE heap, DWORD flags)
193 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
194 ppIpAddrTable, bOrder, heap, flags);
195 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
197 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
198 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
199 TRACE("returning %d\n", ret);
204 /******************************************************************
205 * CancelIPChangeNotify (IPHLPAPI.@)
207 * Cancel a previous notification created by NotifyAddrChange or
211 * overlapped [In] overlapped structure that notifies the caller
218 * Stub, returns FALSE.
220 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
222 FIXME("(overlapped %p): stub\n", overlapped);
228 /******************************************************************
229 * CreateIpForwardEntry (IPHLPAPI.@)
231 * Create a route in the local computer's IP table.
234 * pRoute [In] new route information
238 * Failure: error code from winerror.h
241 * Stub, always returns NO_ERROR.
243 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
245 FIXME("(pRoute %p): stub\n", pRoute);
246 /* could use SIOCADDRT, not sure I want to */
251 /******************************************************************
252 * CreateIpNetEntry (IPHLPAPI.@)
254 * Create entry in the ARP table.
257 * pArpEntry [In] new ARP entry
261 * Failure: error code from winerror.h
264 * Stub, always returns NO_ERROR.
266 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
268 FIXME("(pArpEntry %p)\n", pArpEntry);
269 /* could use SIOCSARP on systems that support it, not sure I want to */
274 /******************************************************************
275 * CreateProxyArpEntry (IPHLPAPI.@)
277 * Create a Proxy ARP (PARP) entry for an IP address.
280 * dwAddress [In] IP address for which this computer acts as a proxy.
281 * dwMask [In] subnet mask for dwAddress
282 * dwIfIndex [In] interface index
286 * Failure: error code from winerror.h
289 * Stub, returns ERROR_NOT_SUPPORTED.
291 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
293 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
294 dwAddress, dwMask, dwIfIndex);
295 return ERROR_NOT_SUPPORTED;
299 /******************************************************************
300 * DeleteIPAddress (IPHLPAPI.@)
302 * Delete an IP address added with AddIPAddress().
305 * NTEContext [In] NTE context from AddIPAddress();
309 * Failure: error code from winerror.h
312 * Stub, returns ERROR_NOT_SUPPORTED.
314 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
316 FIXME("(NTEContext %d): stub\n", NTEContext);
317 return ERROR_NOT_SUPPORTED;
321 /******************************************************************
322 * DeleteIpForwardEntry (IPHLPAPI.@)
327 * pRoute [In] route to delete
331 * Failure: error code from winerror.h
334 * Stub, returns NO_ERROR.
336 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
338 FIXME("(pRoute %p): stub\n", pRoute);
339 /* could use SIOCDELRT, not sure I want to */
344 /******************************************************************
345 * DeleteIpNetEntry (IPHLPAPI.@)
347 * Delete an ARP entry.
350 * pArpEntry [In] ARP entry to delete
354 * Failure: error code from winerror.h
357 * Stub, returns NO_ERROR.
359 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
361 FIXME("(pArpEntry %p): stub\n", pArpEntry);
362 /* could use SIOCDARP on systems that support it, not sure I want to */
367 /******************************************************************
368 * DeleteProxyArpEntry (IPHLPAPI.@)
370 * Delete a Proxy ARP entry.
373 * dwAddress [In] IP address for which this computer acts as a proxy.
374 * dwMask [In] subnet mask for dwAddress
375 * dwIfIndex [In] interface index
379 * Failure: error code from winerror.h
382 * Stub, returns ERROR_NOT_SUPPORTED.
384 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
386 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
387 dwAddress, dwMask, dwIfIndex);
388 return ERROR_NOT_SUPPORTED;
392 /******************************************************************
393 * EnableRouter (IPHLPAPI.@)
395 * Turn on ip forwarding.
399 * pOverlapped [In/Out] hEvent member should contain a valid handle.
402 * Success: ERROR_IO_PENDING
403 * Failure: error code from winerror.h
406 * Stub, returns ERROR_NOT_SUPPORTED.
408 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
410 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
411 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
412 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
414 return ERROR_NOT_SUPPORTED;
418 /******************************************************************
419 * FlushIpNetTable (IPHLPAPI.@)
421 * Delete all ARP entries of an interface
424 * dwIfIndex [In] interface index
428 * Failure: error code from winerror.h
431 * Stub, returns ERROR_NOT_SUPPORTED.
433 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
435 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
436 /* this flushes the arp cache of the given index */
437 return ERROR_NOT_SUPPORTED;
441 /******************************************************************
442 * GetAdapterIndex (IPHLPAPI.@)
444 * Get interface index from its name.
447 * AdapterName [In] unicode string with the adapter name
448 * IfIndex [Out] returns found interface index
452 * Failure: error code from winerror.h
454 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
456 char adapterName[MAX_ADAPTER_NAME];
460 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
461 /* The adapter name is guaranteed not to have any unicode characters, so
462 * this translation is never lossy */
463 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
464 adapterName[i] = (char)AdapterName[i];
465 adapterName[i] = '\0';
466 ret = getInterfaceIndexByName(adapterName, IfIndex);
467 TRACE("returning %d\n", ret);
472 /******************************************************************
473 * GetAdaptersInfo (IPHLPAPI.@)
475 * Get information about adapters.
478 * pAdapterInfo [Out] buffer for adapter infos
479 * pOutBufLen [In] length of output buffer
483 * Failure: error code from winerror.h
485 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
489 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
491 ret = ERROR_INVALID_PARAMETER;
493 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
495 if (numNonLoopbackInterfaces > 0) {
496 DWORD numIPAddresses = getNumIPAddresses();
499 /* This may slightly overestimate the amount of space needed, because
500 * the IP addresses include the loopback address, but it's easier
501 * to make sure there's more than enough space than to make sure there's
502 * precisely enough space.
504 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
505 size += numIPAddresses * sizeof(IP_ADDR_STRING);
506 if (!pAdapterInfo || *pOutBufLen < size) {
508 ret = ERROR_BUFFER_OVERFLOW;
511 InterfaceIndexTable *table = NULL;
512 PMIB_IPADDRTABLE ipAddrTable = NULL;
513 PMIB_IPFORWARDTABLE routeTable = NULL;
515 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
517 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
519 table = getNonLoopbackInterfaceIndexTable();
521 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
522 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
523 if (*pOutBufLen < size) {
525 ret = ERROR_INSUFFICIENT_BUFFER;
530 BOOL winsEnabled = FALSE;
531 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
532 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
533 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
535 memset(pAdapterInfo, 0, size);
536 /* @@ Wine registry key: HKCU\Software\Wine\Network */
537 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
538 &hKey) == ERROR_SUCCESS) {
539 DWORD size = sizeof(primaryWINS.String);
542 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
543 (LPBYTE)primaryWINS.String, &size);
544 addr = inet_addr(primaryWINS.String);
545 if (addr != INADDR_NONE && addr != INADDR_ANY)
547 size = sizeof(secondaryWINS.String);
548 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
549 (LPBYTE)secondaryWINS.String, &size);
550 addr = inet_addr(secondaryWINS.String);
551 if (addr != INADDR_NONE && addr != INADDR_ANY)
555 for (ndx = 0; ndx < table->numIndexes; ndx++) {
556 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
558 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
559 BOOL firstIPAddr = TRUE;
561 /* on Win98 this is left empty, but whatever */
562 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
563 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
564 ptr->AddressLength = sizeof(ptr->Address);
565 getInterfacePhysicalByIndex(table->indexes[ndx],
566 &ptr->AddressLength, ptr->Address, &ptr->Type);
567 ptr->Index = table->indexes[ndx];
568 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
569 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
571 toIPAddressString(ipAddrTable->table[i].dwAddr,
572 ptr->IpAddressList.IpAddress.String);
573 toIPAddressString(ipAddrTable->table[i].dwMask,
574 ptr->IpAddressList.IpMask.String);
578 currentIPAddr->Next = nextIPAddr;
579 currentIPAddr = nextIPAddr;
580 toIPAddressString(ipAddrTable->table[i].dwAddr,
581 currentIPAddr->IpAddress.String);
582 toIPAddressString(ipAddrTable->table[i].dwMask,
583 currentIPAddr->IpMask.String);
588 /* Find first router through this interface, which we'll assume
589 * is the default gateway for this adapter */
590 for (i = 0; i < routeTable->dwNumEntries; i++)
591 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
592 && routeTable->table[i].dwForwardType ==
593 MIB_IPROUTE_TYPE_INDIRECT)
594 toIPAddressString(routeTable->table[i].dwForwardNextHop,
595 ptr->GatewayList.IpAddress.String);
597 ptr->HaveWins = TRUE;
598 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
599 primaryWINS.String, sizeof(primaryWINS.String));
600 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
601 secondaryWINS.String, sizeof(secondaryWINS.String));
603 if (ndx < table->numIndexes - 1)
604 ptr->Next = &pAdapterInfo[ndx + 1];
610 HeapFree(GetProcessHeap(), 0, table);
613 ret = ERROR_OUTOFMEMORY;
614 HeapFree(GetProcessHeap(), 0, routeTable);
615 HeapFree(GetProcessHeap(), 0, ipAddrTable);
621 TRACE("returning %d\n", ret);
625 static DWORD typeFromMibType(DWORD mib_type)
629 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
630 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
631 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
632 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
633 default: return IF_TYPE_OTHER;
637 static DWORD connectionTypeFromMibType(DWORD mib_type)
641 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
642 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
643 default: return NET_IF_CONNECTION_DEDICATED;
647 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
653 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
654 for (i = 0; i < at->dwNumEntries; i++)
656 if (at->table[i].dwIndex == index) (*num_addrs)++;
658 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
660 HeapFree(GetProcessHeap(), 0, at);
661 return ERROR_OUTOFMEMORY;
663 for (i = 0, j = 0; i < at->dwNumEntries; i++)
665 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
667 HeapFree(GetProcessHeap(), 0, at);
668 return ERROR_SUCCESS;
671 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
676 for (addrp = (const BYTE *)in_addr;
677 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
680 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
681 sprintf(p, "%d", *addrp);
683 p += sprintf(p, "%d.", *addrp);
688 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
690 const IN6_ADDR *addr = &sin->sin6_addr;
693 BOOL in_zero = FALSE;
695 for (i = 0; i < 7; i++)
697 if (!addr->u.Word[i])
709 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
713 sprintf(p, "%x", ntohs(addr->u.Word[7]));
717 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
719 DWORD i, num_gateways = 0;
721 for (i = 0; i < routeTable->dwNumEntries; i++)
723 if (routeTable->table[i].dwForwardIfIndex == index &&
724 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
730 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
731 PMIB_IPFORWARDTABLE routeTable)
734 PMIB_IPFORWARDROW row = NULL;
736 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
738 if (routeTable->table[i].dwForwardIfIndex == index &&
739 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
740 row = &routeTable->table[i];
745 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
746 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
748 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
749 DWORD *v4addrs = NULL;
750 SOCKET_ADDRESS *v6addrs = NULL;
751 PMIB_IPFORWARDTABLE routeTable = NULL;
753 if (family == WS_AF_INET)
755 if (!(flags & GAA_FLAG_SKIP_UNICAST))
756 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
757 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
759 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
760 GetProcessHeap(), 0);
762 num_v4_gateways = count_v4_gateways(index, routeTable);
765 else if (family == WS_AF_INET6)
767 if (!(flags & GAA_FLAG_SKIP_UNICAST))
768 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
770 else if (family == WS_AF_UNSPEC)
772 if (!(flags & GAA_FLAG_SKIP_UNICAST))
773 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
774 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
776 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
777 GetProcessHeap(), 0);
780 num_v4_gateways = count_v4_gateways(index, routeTable);
781 if (!(flags & GAA_FLAG_SKIP_UNICAST))
782 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
788 FIXME("address family %u unsupported\n", family);
793 HeapFree(GetProcessHeap(), 0, routeTable);
797 total_size = sizeof(IP_ADAPTER_ADDRESSES);
798 total_size += IF_NAMESIZE;
799 total_size += IF_NAMESIZE * sizeof(WCHAR);
800 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
801 total_size += IF_NAMESIZE * sizeof(WCHAR);
802 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
803 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
804 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
805 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
806 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
807 for (i = 0; i < num_v6addrs; i++)
808 total_size += v6addrs[i].iSockaddrLength;
810 if (aa && *size >= total_size)
812 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
814 DWORD buflen, type, status;
816 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
817 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
818 aa->u.s.IfIndex = index;
820 getInterfaceNameByIndex(index, name);
821 memcpy(ptr, name, IF_NAMESIZE);
822 aa->AdapterName = ptr;
824 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
826 aa->FriendlyName = (WCHAR *)ptr;
827 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
832 aa->Description = (WCHAR *)ptr;
833 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
838 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
842 PMIB_IPFORWARDROW adapterRow;
844 if ((adapterRow = findIPv4Gateway(index, routeTable)))
846 PIP_ADAPTER_GATEWAY_ADDRESS gw;
849 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
850 aa->FirstGatewayAddress = gw;
852 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
853 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
854 sin = (PSOCKADDR_IN)ptr;
855 sin->sin_family = AF_INET;
857 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
859 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
860 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
862 ptr += sizeof(SOCKADDR_IN);
867 IP_ADAPTER_UNICAST_ADDRESS *ua;
868 struct sockaddr_in *sa;
869 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
870 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
871 for (i = 0; i < num_v4addrs; i++)
875 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
876 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
877 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
878 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
880 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
881 sa->sin_family = AF_INET;
882 sa->sin_addr.s_addr = v4addrs[i];
884 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
885 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
887 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
888 if (i < num_v4addrs - 1)
890 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
897 IP_ADAPTER_UNICAST_ADDRESS *ua;
898 struct WS_sockaddr_in6 *sa;
900 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
901 if (aa->FirstUnicastAddress)
903 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
905 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
906 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
909 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
910 for (i = 0; i < num_v6addrs; i++)
914 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
915 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
916 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
917 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
919 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
920 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
921 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
922 debugstr_ipv6(sa, addr_buf));
924 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
925 if (i < num_v6addrs - 1)
927 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
933 buflen = MAX_INTERFACE_PHYSADDR;
934 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
935 aa->PhysicalAddressLength = buflen;
936 aa->IfType = typeFromMibType(type);
937 aa->ConnectionType = connectionTypeFromMibType(type);
939 getInterfaceMtuByName(name, &aa->Mtu);
941 getInterfaceStatusByName(name, &status);
942 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
943 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
944 else aa->OperStatus = IfOperStatusUnknown;
947 HeapFree(GetProcessHeap(), 0, routeTable);
948 HeapFree(GetProcessHeap(), 0, v6addrs);
949 HeapFree(GetProcessHeap(), 0, v4addrs);
950 return ERROR_SUCCESS;
953 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
957 initialise_resolver();
958 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
959 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
962 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
963 if (!address || *len < size)
966 return ERROR_BUFFER_OVERFLOW;
969 if (_res.nscount > 0)
971 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
974 for (i = 0, addr = address; i < _res.nscount && addr;
975 i++, addr = addr->Next)
979 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
980 addr->Address.lpSockaddr =
981 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
982 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
983 sin->sin_family = WS_AF_INET;
984 sin->sin_port = _res.nsaddr_list[i].sin_port;
985 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
986 if (i == _res.nscount - 1)
990 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
991 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
994 return ERROR_SUCCESS;
997 static BOOL is_ip_address_string(const char *str)
1002 ret = inet_aton(str, &in);
1006 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1009 char *found_suffix = NULL;
1011 initialise_resolver();
1012 /* Always return a NULL-terminated string, even if it's empty. */
1013 size = sizeof(WCHAR);
1014 for (i = 0, found_suffix = NULL;
1015 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1017 /* This uses a heuristic to select a DNS suffix:
1018 * the first, non-IP address string is selected.
1020 if (!is_ip_address_string(_res.dnsrch[i]))
1021 found_suffix = _res.dnsrch[i];
1024 size += strlen(found_suffix) * sizeof(WCHAR);
1025 if (!suffix || *len < size)
1028 return ERROR_BUFFER_OVERFLOW;
1035 for (p = found_suffix; *p; p++)
1039 return ERROR_SUCCESS;
1042 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1043 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1045 InterfaceIndexTable *table;
1046 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1048 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1050 if (!buflen) return ERROR_INVALID_PARAMETER;
1052 table = getInterfaceIndexTable();
1053 if (!table || !table->numIndexes)
1055 HeapFree(GetProcessHeap(), 0, table);
1056 return ERROR_NO_DATA;
1059 for (i = 0; i < table->numIndexes; i++)
1062 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1064 HeapFree(GetProcessHeap(), 0, table);
1069 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1071 /* Since DNS servers aren't really per adapter, get enough space for a
1072 * single copy of them.
1074 get_dns_server_addresses(NULL, &dns_server_size);
1075 total_size += dns_server_size;
1077 /* Since DNS suffix also isn't really per adapter, get enough space for a
1078 * single copy of it.
1080 get_dns_suffix(NULL, &dns_suffix_size);
1081 total_size += dns_suffix_size;
1082 if (aa && *buflen >= total_size)
1084 ULONG bytes_left = size = total_size;
1085 PIP_ADAPTER_ADDRESSES first_aa = aa;
1086 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1089 for (i = 0; i < table->numIndexes; i++)
1091 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1093 HeapFree(GetProcessHeap(), 0, table);
1096 if (i < table->numIndexes - 1)
1098 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1100 size = bytes_left -= size;
1103 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1105 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1106 get_dns_server_addresses(firstDns, &dns_server_size);
1107 for (aa = first_aa; aa; aa = aa->Next)
1109 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1110 aa->FirstDnsServerAddress = firstDns;
1114 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1115 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1116 for (; aa; aa = aa->Next)
1118 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1119 aa->DnsSuffix = dnsSuffix;
1121 aa->DnsSuffix = (WCHAR *)((BYTE*)dnsSuffix + dns_suffix_size - 2);
1123 ret = ERROR_SUCCESS;
1126 ret = ERROR_BUFFER_OVERFLOW;
1127 *buflen = total_size;
1129 TRACE("num adapters %u\n", table->numIndexes);
1130 HeapFree(GetProcessHeap(), 0, table);
1134 /******************************************************************
1135 * GetBestInterface (IPHLPAPI.@)
1137 * Get the interface, with the best route for the given IP address.
1140 * dwDestAddr [In] IP address to search the interface for
1141 * pdwBestIfIndex [Out] found best interface
1145 * Failure: error code from winerror.h
1147 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1149 struct WS_sockaddr_in sa_in;
1150 memset(&sa_in, 0, sizeof(sa_in));
1151 sa_in.sin_family = AF_INET;
1152 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1153 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1156 /******************************************************************
1157 * GetBestInterfaceEx (IPHLPAPI.@)
1159 * Get the interface, with the best route for the given IP address.
1162 * dwDestAddr [In] IP address to search the interface for
1163 * pdwBestIfIndex [Out] found best interface
1167 * Failure: error code from winerror.h
1169 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1173 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1174 if (!pDestAddr || !pdwBestIfIndex)
1175 ret = ERROR_INVALID_PARAMETER;
1177 MIB_IPFORWARDROW ipRow;
1179 if (pDestAddr->sa_family == AF_INET) {
1180 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1181 if (ret == ERROR_SUCCESS)
1182 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1184 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1185 ret = ERROR_NOT_SUPPORTED;
1188 TRACE("returning %d\n", ret);
1193 /******************************************************************
1194 * GetBestRoute (IPHLPAPI.@)
1196 * Get the best route for the given IP address.
1199 * dwDestAddr [In] IP address to search the best route for
1200 * dwSourceAddr [In] optional source IP address
1201 * pBestRoute [Out] found best route
1205 * Failure: error code from winerror.h
1207 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1209 PMIB_IPFORWARDTABLE table;
1212 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1213 dwSourceAddr, pBestRoute);
1215 return ERROR_INVALID_PARAMETER;
1217 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1219 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1221 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1222 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1223 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1224 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1225 DWORD numShifts, mask;
1227 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1228 mask && mask & 1; mask >>= 1, numShifts++)
1230 if (numShifts > matchedBits) {
1231 matchedBits = numShifts;
1234 else if (!matchedBits) {
1239 if (matchedNdx < table->dwNumEntries) {
1240 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1241 ret = ERROR_SUCCESS;
1244 /* No route matches, which can happen if there's no default route. */
1245 ret = ERROR_HOST_UNREACHABLE;
1247 HeapFree(GetProcessHeap(), 0, table);
1249 TRACE("returning %d\n", ret);
1254 /******************************************************************
1255 * GetFriendlyIfIndex (IPHLPAPI.@)
1257 * Get a "friendly" version of IfIndex, which is one that doesn't
1258 * have the top byte set. Doesn't validate whether IfIndex is a valid
1262 * IfIndex [In] interface index to get the friendly one for
1265 * A friendly version of IfIndex.
1267 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1269 /* windows doesn't validate these, either, just makes sure the top byte is
1270 cleared. I assume my ifenum module never gives an index with the top
1272 TRACE("returning %d\n", IfIndex);
1277 /******************************************************************
1278 * GetIfEntry (IPHLPAPI.@)
1280 * Get information about an interface.
1283 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1284 * Out: interface information
1288 * Failure: error code from winerror.h
1290 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1293 char nameBuf[MAX_ADAPTER_NAME];
1296 TRACE("pIfRow %p\n", pIfRow);
1298 return ERROR_INVALID_PARAMETER;
1300 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1302 ret = getInterfaceEntryByName(name, pIfRow);
1303 if (ret == NO_ERROR)
1304 ret = getInterfaceStatsByName(name, pIfRow);
1307 ret = ERROR_INVALID_DATA;
1308 TRACE("returning %d\n", ret);
1313 static int IfTableSorter(const void *a, const void *b)
1318 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1325 /******************************************************************
1326 * GetIfTable (IPHLPAPI.@)
1328 * Get a table of local interfaces.
1331 * pIfTable [Out] buffer for local interfaces table
1332 * pdwSize [In/Out] length of output buffer
1333 * bOrder [In] whether to sort the table
1337 * Failure: error code from winerror.h
1340 * If pdwSize is less than required, the function will return
1341 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1343 * If bOrder is true, the returned table will be sorted by interface index.
1345 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1349 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1352 ret = ERROR_INVALID_PARAMETER;
1354 DWORD numInterfaces = getNumInterfaces();
1355 ULONG size = sizeof(MIB_IFTABLE);
1357 if (numInterfaces > 1)
1358 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1359 if (!pIfTable || *pdwSize < size) {
1361 ret = ERROR_INSUFFICIENT_BUFFER;
1364 InterfaceIndexTable *table = getInterfaceIndexTable();
1367 size = sizeof(MIB_IFTABLE);
1368 if (table->numIndexes > 1)
1369 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1370 if (*pdwSize < size) {
1372 ret = ERROR_INSUFFICIENT_BUFFER;
1378 pIfTable->dwNumEntries = 0;
1379 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1380 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1381 GetIfEntry(&pIfTable->table[ndx]);
1382 pIfTable->dwNumEntries++;
1385 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1389 HeapFree(GetProcessHeap(), 0, table);
1392 ret = ERROR_OUTOFMEMORY;
1395 TRACE("returning %d\n", ret);
1400 /******************************************************************
1401 * GetInterfaceInfo (IPHLPAPI.@)
1403 * Get a list of network interface adapters.
1406 * pIfTable [Out] buffer for interface adapters
1407 * dwOutBufLen [Out] if buffer is too small, returns required size
1411 * Failure: error code from winerror.h
1414 * MSDN states this should return non-loopback interfaces only.
1416 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1420 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1422 ret = ERROR_INVALID_PARAMETER;
1424 DWORD numInterfaces = getNumInterfaces();
1425 ULONG size = sizeof(IP_INTERFACE_INFO);
1427 if (numInterfaces > 1)
1428 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1429 if (!pIfTable || *dwOutBufLen < size) {
1430 *dwOutBufLen = size;
1431 ret = ERROR_INSUFFICIENT_BUFFER;
1434 InterfaceIndexTable *table = getInterfaceIndexTable();
1437 size = sizeof(IP_INTERFACE_INFO);
1438 if (table->numIndexes > 1)
1439 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1440 if (*dwOutBufLen < size) {
1441 *dwOutBufLen = size;
1442 ret = ERROR_INSUFFICIENT_BUFFER;
1446 char nameBuf[MAX_ADAPTER_NAME];
1448 *dwOutBufLen = size;
1449 pIfTable->NumAdapters = 0;
1450 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1451 const char *walker, *name;
1454 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1455 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1456 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1457 walker && *walker &&
1458 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1459 walker++, assigner++)
1460 *assigner = *walker;
1462 pIfTable->NumAdapters++;
1466 HeapFree(GetProcessHeap(), 0, table);
1469 ret = ERROR_OUTOFMEMORY;
1472 TRACE("returning %d\n", ret);
1477 /******************************************************************
1478 * GetIpAddrTable (IPHLPAPI.@)
1480 * Get interface-to-IP address mapping table.
1483 * pIpAddrTable [Out] buffer for mapping table
1484 * pdwSize [In/Out] length of output buffer
1485 * bOrder [In] whether to sort the table
1489 * Failure: error code from winerror.h
1492 * If pdwSize is less than required, the function will return
1493 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1495 * If bOrder is true, the returned table will be sorted by the next hop and
1496 * an assortment of arbitrary parameters.
1498 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1502 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1505 ret = ERROR_INVALID_PARAMETER;
1507 PMIB_IPADDRTABLE table;
1509 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1510 if (ret == NO_ERROR)
1512 ULONG size = sizeof(MIB_IPADDRTABLE);
1514 if (table->dwNumEntries > 1)
1515 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1516 if (!pIpAddrTable || *pdwSize < size) {
1518 ret = ERROR_INSUFFICIENT_BUFFER;
1522 memcpy(pIpAddrTable, table, size);
1524 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1525 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1528 HeapFree(GetProcessHeap(), 0, table);
1531 TRACE("returning %d\n", ret);
1536 /******************************************************************
1537 * GetIpForwardTable (IPHLPAPI.@)
1539 * Get the route table.
1542 * pIpForwardTable [Out] buffer for route table
1543 * pdwSize [In/Out] length of output buffer
1544 * bOrder [In] whether to sort the table
1548 * Failure: error code from winerror.h
1551 * If pdwSize is less than required, the function will return
1552 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1554 * If bOrder is true, the returned table will be sorted by the next hop and
1555 * an assortment of arbitrary parameters.
1557 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1560 PMIB_IPFORWARDTABLE table;
1562 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1564 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1566 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1568 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1569 if (!pIpForwardTable || *pdwSize < size) {
1571 ret = ERROR_INSUFFICIENT_BUFFER;
1575 memcpy(pIpForwardTable, table, size);
1577 HeapFree(GetProcessHeap(), 0, table);
1579 TRACE("returning %d\n", ret);
1584 /******************************************************************
1585 * GetIpNetTable (IPHLPAPI.@)
1587 * Get the IP-to-physical address mapping table.
1590 * pIpNetTable [Out] buffer for mapping table
1591 * pdwSize [In/Out] length of output buffer
1592 * bOrder [In] whether to sort the table
1596 * Failure: error code from winerror.h
1599 * If pdwSize is less than required, the function will return
1600 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1602 * If bOrder is true, the returned table will be sorted by IP address.
1604 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1607 PMIB_IPNETTABLE table;
1609 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1611 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1613 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1615 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1616 if (!pIpNetTable || *pdwSize < size) {
1618 ret = ERROR_INSUFFICIENT_BUFFER;
1622 memcpy(pIpNetTable, table, size);
1624 HeapFree(GetProcessHeap(), 0, table);
1626 TRACE("returning %d\n", ret);
1630 /* Gets the DNS server list into the list beginning at list. Assumes that
1631 * a single server address may be placed at list if *len is at least
1632 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1633 * and assumes that all remaining DNS servers are contiguously located
1634 * beginning at firstDynamic. On input, *len is assumed to be the total number
1635 * of bytes available for all DNS servers, and is ignored if list is NULL.
1636 * On return, *len is set to the total number of bytes required for all DNS
1638 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1639 * ERROR_SUCCESS otherwise.
1641 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1642 PIP_ADDR_STRING firstDynamic, DWORD *len)
1646 initialise_resolver();
1647 size = _res.nscount * sizeof(IP_ADDR_STRING);
1648 if (!list || *len < size) {
1650 return ERROR_BUFFER_OVERFLOW;
1653 if (_res.nscount > 0) {
1654 PIP_ADDR_STRING ptr;
1657 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1658 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1659 ptr->IpAddress.String);
1660 if (i == _res.nscount - 1)
1663 ptr->Next = firstDynamic;
1665 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1668 return ERROR_SUCCESS;
1671 /******************************************************************
1672 * GetNetworkParams (IPHLPAPI.@)
1674 * Get the network parameters for the local computer.
1677 * pFixedInfo [Out] buffer for network parameters
1678 * pOutBufLen [In/Out] length of output buffer
1682 * Failure: error code from winerror.h
1685 * If pOutBufLen is less than required, the function will return
1686 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1689 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1691 DWORD ret, size, serverListSize;
1695 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1697 return ERROR_INVALID_PARAMETER;
1699 get_dns_server_list(NULL, NULL, &serverListSize);
1700 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1701 if (!pFixedInfo || *pOutBufLen < size) {
1703 return ERROR_BUFFER_OVERFLOW;
1706 memset(pFixedInfo, 0, size);
1707 size = sizeof(pFixedInfo->HostName);
1708 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1709 size = sizeof(pFixedInfo->DomainName);
1710 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1711 get_dns_server_list(&pFixedInfo->DnsServerList,
1712 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1714 /* Assume the first DNS server in the list is the "current" DNS server: */
1715 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1716 pFixedInfo->NodeType = HYBRID_NODETYPE;
1717 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1718 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1719 if (regReturn != ERROR_SUCCESS)
1720 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1721 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1723 if (regReturn == ERROR_SUCCESS)
1725 DWORD size = sizeof(pFixedInfo->ScopeId);
1727 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1731 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1732 I suppose could also check for a listener on port 53 to set EnableDns */
1734 TRACE("returning %d\n", ret);
1739 /******************************************************************
1740 * GetNumberOfInterfaces (IPHLPAPI.@)
1742 * Get the number of interfaces.
1745 * pdwNumIf [Out] number of interfaces
1748 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1750 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1754 TRACE("pdwNumIf %p\n", pdwNumIf);
1756 ret = ERROR_INVALID_PARAMETER;
1758 *pdwNumIf = getNumInterfaces();
1761 TRACE("returning %d\n", ret);
1766 /******************************************************************
1767 * GetPerAdapterInfo (IPHLPAPI.@)
1769 * Get information about an adapter corresponding to an interface.
1772 * IfIndex [In] interface info
1773 * pPerAdapterInfo [Out] buffer for per adapter info
1774 * pOutBufLen [In/Out] length of output buffer
1778 * Failure: error code from winerror.h
1780 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1782 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1783 DWORD ret = NO_ERROR;
1785 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1787 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1789 if (!isIfIndexLoopback(IfIndex)) {
1790 get_dns_server_list(NULL, NULL, &serverListSize);
1791 if (serverListSize > sizeof(IP_ADDR_STRING))
1792 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1794 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1796 *pOutBufLen = bytesNeeded;
1797 return ERROR_BUFFER_OVERFLOW;
1800 memset(pPerAdapterInfo, 0, bytesNeeded);
1801 if (!isIfIndexLoopback(IfIndex)) {
1802 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1803 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1805 /* Assume the first DNS server in the list is the "current" DNS server: */
1806 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1812 /******************************************************************
1813 * GetRTTAndHopCount (IPHLPAPI.@)
1815 * Get round-trip time (RTT) and hop count.
1819 * DestIpAddress [In] destination address to get the info for
1820 * HopCount [Out] retrieved hop count
1821 * MaxHops [In] maximum hops to search for the destination
1822 * RTT [Out] RTT in milliseconds
1829 * Stub, returns FALSE.
1831 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1833 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1834 DestIpAddress, HopCount, MaxHops, RTT);
1839 /******************************************************************
1840 * GetTcpTable (IPHLPAPI.@)
1842 * Get the table of active TCP connections.
1845 * pTcpTable [Out] buffer for TCP connections table
1846 * pdwSize [In/Out] length of output buffer
1847 * bOrder [In] whether to order the table
1851 * Failure: error code from winerror.h
1854 * If pdwSize is less than required, the function will return
1855 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1856 * the required byte size.
1857 * If bOrder is true, the returned table will be sorted, first by
1858 * local address and port number, then by remote address and port
1861 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1864 PMIB_TCPTABLE table;
1866 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1868 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1870 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1872 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1873 if (!pTcpTable || *pdwSize < size) {
1875 ret = ERROR_INSUFFICIENT_BUFFER;
1879 memcpy(pTcpTable, table, size);
1881 HeapFree(GetProcessHeap(), 0, table);
1883 TRACE("returning %d\n", ret);
1888 /******************************************************************
1889 * GetUdpTable (IPHLPAPI.@)
1891 * Get a table of active UDP connections.
1894 * pUdpTable [Out] buffer for UDP connections table
1895 * pdwSize [In/Out] length of output buffer
1896 * bOrder [In] whether to order the table
1900 * Failure: error code from winerror.h
1903 * If pdwSize is less than required, the function will return
1904 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1905 * required byte size.
1906 * If bOrder is true, the returned table will be sorted, first by
1907 * local address, then by local port number.
1909 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1912 PMIB_UDPTABLE table;
1914 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1916 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1918 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1920 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1921 if (!pUdpTable || *pdwSize < size) {
1923 ret = ERROR_INSUFFICIENT_BUFFER;
1927 memcpy(pUdpTable, table, size);
1929 HeapFree(GetProcessHeap(), 0, table);
1931 TRACE("returning %d\n", ret);
1936 /******************************************************************
1937 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1939 * This is a Win98-only function to get information on "unidirectional"
1940 * adapters. Since this is pretty nonsensical in other contexts, it
1941 * never returns anything.
1944 * pIPIfInfo [Out] buffer for adapter infos
1945 * dwOutBufLen [Out] length of the output buffer
1949 * Failure: error code from winerror.h
1952 * Stub, returns ERROR_NOT_SUPPORTED.
1954 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1956 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1957 /* a unidirectional adapter?? not bloody likely! */
1958 return ERROR_NOT_SUPPORTED;
1962 /******************************************************************
1963 * IpReleaseAddress (IPHLPAPI.@)
1965 * Release an IP obtained through DHCP,
1968 * AdapterInfo [In] adapter to release IP address
1972 * Failure: error code from winerror.h
1975 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1976 * this function does nothing.
1979 * Stub, returns ERROR_NOT_SUPPORTED.
1981 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1983 TRACE("AdapterInfo %p\n", AdapterInfo);
1984 /* not a stub, never going to support this (and I never mark an adapter as
1985 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1986 return ERROR_NOT_SUPPORTED;
1990 /******************************************************************
1991 * IpRenewAddress (IPHLPAPI.@)
1993 * Renew an IP obtained through DHCP.
1996 * AdapterInfo [In] adapter to renew IP address
2000 * Failure: error code from winerror.h
2003 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2004 * this function does nothing.
2007 * Stub, returns ERROR_NOT_SUPPORTED.
2009 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2011 TRACE("AdapterInfo %p\n", AdapterInfo);
2012 /* not a stub, never going to support this (and I never mark an adapter as
2013 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
2014 return ERROR_NOT_SUPPORTED;
2018 /******************************************************************
2019 * NotifyAddrChange (IPHLPAPI.@)
2021 * Notify caller whenever the ip-interface map is changed.
2024 * Handle [Out] handle usable in asynchronous notification
2025 * overlapped [In] overlapped structure that notifies the caller
2029 * Failure: error code from winerror.h
2032 * Stub, returns ERROR_NOT_SUPPORTED.
2034 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2036 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2037 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2038 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2039 return ERROR_IO_PENDING;
2043 /******************************************************************
2044 * NotifyRouteChange (IPHLPAPI.@)
2046 * Notify caller whenever the ip routing table is changed.
2049 * Handle [Out] handle usable in asynchronous notification
2050 * overlapped [In] overlapped structure that notifies the caller
2054 * Failure: error code from winerror.h
2057 * Stub, returns ERROR_NOT_SUPPORTED.
2059 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2061 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2062 return ERROR_NOT_SUPPORTED;
2066 /******************************************************************
2067 * SendARP (IPHLPAPI.@)
2069 * Send an ARP request.
2072 * DestIP [In] attempt to obtain this IP
2073 * SrcIP [In] optional sender IP address
2074 * pMacAddr [Out] buffer for the mac address
2075 * PhyAddrLen [In/Out] length of the output buffer
2079 * Failure: error code from winerror.h
2082 * Stub, returns ERROR_NOT_SUPPORTED.
2084 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2086 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2087 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2088 return ERROR_NOT_SUPPORTED;
2092 /******************************************************************
2093 * SetIfEntry (IPHLPAPI.@)
2095 * Set the administrative status of an interface.
2098 * pIfRow [In] dwAdminStatus member specifies the new status.
2102 * Failure: error code from winerror.h
2105 * Stub, returns ERROR_NOT_SUPPORTED.
2107 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2109 FIXME("(pIfRow %p): stub\n", pIfRow);
2110 /* this is supposed to set an interface administratively up or down.
2111 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2112 this sort of down is indistinguishable from other sorts of down (e.g. no
2114 return ERROR_NOT_SUPPORTED;
2118 /******************************************************************
2119 * SetIpForwardEntry (IPHLPAPI.@)
2121 * Modify an existing route.
2124 * pRoute [In] route with the new information
2128 * Failure: error code from winerror.h
2131 * Stub, returns NO_ERROR.
2133 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2135 FIXME("(pRoute %p): stub\n", pRoute);
2136 /* this is to add a route entry, how's it distinguishable from
2137 CreateIpForwardEntry?
2138 could use SIOCADDRT, not sure I want to */
2143 /******************************************************************
2144 * SetIpNetEntry (IPHLPAPI.@)
2146 * Modify an existing ARP entry.
2149 * pArpEntry [In] ARP entry with the new information
2153 * Failure: error code from winerror.h
2156 * Stub, returns NO_ERROR.
2158 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2160 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2161 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2166 /******************************************************************
2167 * SetIpStatistics (IPHLPAPI.@)
2169 * Toggle IP forwarding and det the default TTL value.
2172 * pIpStats [In] IP statistics with the new information
2176 * Failure: error code from winerror.h
2179 * Stub, returns NO_ERROR.
2181 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2183 FIXME("(pIpStats %p): stub\n", pIpStats);
2188 /******************************************************************
2189 * SetIpTTL (IPHLPAPI.@)
2191 * Set the default TTL value.
2194 * nTTL [In] new TTL value
2198 * Failure: error code from winerror.h
2201 * Stub, returns NO_ERROR.
2203 DWORD WINAPI SetIpTTL(UINT nTTL)
2205 FIXME("(nTTL %d): stub\n", nTTL);
2206 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2207 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2212 /******************************************************************
2213 * SetTcpEntry (IPHLPAPI.@)
2215 * Set the state of a TCP connection.
2218 * pTcpRow [In] specifies connection with new state
2222 * Failure: error code from winerror.h
2225 * Stub, returns NO_ERROR.
2227 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2229 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2234 /******************************************************************
2235 * UnenableRouter (IPHLPAPI.@)
2237 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2238 * if it reaches zero.
2241 * pOverlapped [In/Out] should be the same as in EnableRouter()
2242 * lpdwEnableCount [Out] optional, receives reference count
2246 * Failure: error code from winerror.h
2249 * Stub, returns ERROR_NOT_SUPPORTED.
2251 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2253 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2255 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2256 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2258 return ERROR_NOT_SUPPORTED;
2261 /******************************************************************
2262 * PfCreateInterface (IPHLPAPI.@)
2264 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2265 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2267 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2268 return ERROR_CALL_NOT_IMPLEMENTED;