2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
63 #define IF_NAMESIZE 16
67 #define INADDR_NONE ~0UL
70 /* call res_init() just once because of a bug in Mac OS X 10.4 */
71 /* Call once per thread on systems that have per-thread _res. */
72 static void initialise_resolver(void)
74 if ((_res.options & RES_INIT) == 0)
78 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
81 case DLL_PROCESS_ATTACH:
82 DisableThreadLibraryCalls( hinstDLL );
85 case DLL_PROCESS_DETACH:
91 /******************************************************************
92 * AddIPAddress (IPHLPAPI.@)
94 * Add an IP address to an adapter.
97 * Address [In] IP address to add to the adapter
98 * IpMask [In] subnet mask for the IP address
99 * IfIndex [In] adapter index to add the address
100 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
101 * NTEInstance [Out] NTE instance for the IP address
105 * Failure: error code from winerror.h
108 * Stub. Currently returns ERROR_NOT_SUPPORTED.
110 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
113 return ERROR_NOT_SUPPORTED;
117 /******************************************************************
118 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
120 * Get table of local interfaces.
121 * Like GetIfTable(), but allocate the returned table from heap.
124 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
125 * allocated and returned.
126 * bOrder [In] whether to sort the table
127 * heap [In] heap from which the table is allocated
128 * flags [In] flags to HeapAlloc
131 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
132 * GetIfTable() returns otherwise.
134 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
135 BOOL bOrder, HANDLE heap, DWORD flags)
139 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
140 bOrder, heap, flags);
142 ret = ERROR_INVALID_PARAMETER;
146 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
147 if (ret == ERROR_INSUFFICIENT_BUFFER) {
148 *ppIfTable = HeapAlloc(heap, flags, dwSize);
149 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
152 TRACE("returning %d\n", ret);
157 static int IpAddrTableSorter(const void *a, const void *b)
162 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
169 /******************************************************************
170 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
172 * Get interface-to-IP address mapping table.
173 * Like GetIpAddrTable(), but allocate the returned table from heap.
176 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
177 * allocated and returned.
178 * bOrder [In] whether to sort the table
179 * heap [In] heap from which the table is allocated
180 * flags [In] flags to HeapAlloc
183 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
184 * failure, NO_ERROR on success.
186 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
187 BOOL bOrder, HANDLE heap, DWORD flags)
191 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
192 ppIpAddrTable, bOrder, heap, flags);
193 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
195 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
196 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
197 TRACE("returning %d\n", ret);
202 /******************************************************************
203 * CreateIpForwardEntry (IPHLPAPI.@)
205 * Create a route in the local computer's IP table.
208 * pRoute [In] new route information
212 * Failure: error code from winerror.h
215 * Stub, always returns NO_ERROR.
217 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
219 FIXME("(pRoute %p): stub\n", pRoute);
220 /* could use SIOCADDRT, not sure I want to */
225 /******************************************************************
226 * CreateIpNetEntry (IPHLPAPI.@)
228 * Create entry in the ARP table.
231 * pArpEntry [In] new ARP entry
235 * Failure: error code from winerror.h
238 * Stub, always returns NO_ERROR.
240 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
242 FIXME("(pArpEntry %p)\n", pArpEntry);
243 /* could use SIOCSARP on systems that support it, not sure I want to */
248 /******************************************************************
249 * CreateProxyArpEntry (IPHLPAPI.@)
251 * Create a Proxy ARP (PARP) entry for an IP address.
254 * dwAddress [In] IP address for which this computer acts as a proxy.
255 * dwMask [In] subnet mask for dwAddress
256 * dwIfIndex [In] interface index
260 * Failure: error code from winerror.h
263 * Stub, returns ERROR_NOT_SUPPORTED.
265 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
267 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
268 dwAddress, dwMask, dwIfIndex);
269 return ERROR_NOT_SUPPORTED;
273 /******************************************************************
274 * DeleteIPAddress (IPHLPAPI.@)
276 * Delete an IP address added with AddIPAddress().
279 * NTEContext [In] NTE context from AddIPAddress();
283 * Failure: error code from winerror.h
286 * Stub, returns ERROR_NOT_SUPPORTED.
288 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
290 FIXME("(NTEContext %d): stub\n", NTEContext);
291 return ERROR_NOT_SUPPORTED;
295 /******************************************************************
296 * DeleteIpForwardEntry (IPHLPAPI.@)
301 * pRoute [In] route to delete
305 * Failure: error code from winerror.h
308 * Stub, returns NO_ERROR.
310 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
312 FIXME("(pRoute %p): stub\n", pRoute);
313 /* could use SIOCDELRT, not sure I want to */
318 /******************************************************************
319 * DeleteIpNetEntry (IPHLPAPI.@)
321 * Delete an ARP entry.
324 * pArpEntry [In] ARP entry to delete
328 * Failure: error code from winerror.h
331 * Stub, returns NO_ERROR.
333 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
335 FIXME("(pArpEntry %p): stub\n", pArpEntry);
336 /* could use SIOCDARP on systems that support it, not sure I want to */
341 /******************************************************************
342 * DeleteProxyArpEntry (IPHLPAPI.@)
344 * Delete a Proxy ARP entry.
347 * dwAddress [In] IP address for which this computer acts as a proxy.
348 * dwMask [In] subnet mask for dwAddress
349 * dwIfIndex [In] interface index
353 * Failure: error code from winerror.h
356 * Stub, returns ERROR_NOT_SUPPORTED.
358 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
360 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
361 dwAddress, dwMask, dwIfIndex);
362 return ERROR_NOT_SUPPORTED;
366 /******************************************************************
367 * EnableRouter (IPHLPAPI.@)
369 * Turn on ip forwarding.
373 * pOverlapped [In/Out] hEvent member should contain a valid handle.
376 * Success: ERROR_IO_PENDING
377 * Failure: error code from winerror.h
380 * Stub, returns ERROR_NOT_SUPPORTED.
382 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
384 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
385 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
386 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
388 return ERROR_NOT_SUPPORTED;
392 /******************************************************************
393 * FlushIpNetTable (IPHLPAPI.@)
395 * Delete all ARP entries of an interface
398 * dwIfIndex [In] interface index
402 * Failure: error code from winerror.h
405 * Stub, returns ERROR_NOT_SUPPORTED.
407 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
409 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
410 /* this flushes the arp cache of the given index */
411 return ERROR_NOT_SUPPORTED;
415 /******************************************************************
416 * GetAdapterIndex (IPHLPAPI.@)
418 * Get interface index from its name.
421 * AdapterName [In] unicode string with the adapter name
422 * IfIndex [Out] returns found interface index
426 * Failure: error code from winerror.h
428 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
430 char adapterName[MAX_ADAPTER_NAME];
434 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
435 /* The adapter name is guaranteed not to have any unicode characters, so
436 * this translation is never lossy */
437 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
438 adapterName[i] = (char)AdapterName[i];
439 adapterName[i] = '\0';
440 ret = getInterfaceIndexByName(adapterName, IfIndex);
441 TRACE("returning %d\n", ret);
446 /******************************************************************
447 * GetAdaptersInfo (IPHLPAPI.@)
449 * Get information about adapters.
452 * pAdapterInfo [Out] buffer for adapter infos
453 * pOutBufLen [In] length of output buffer
457 * Failure: error code from winerror.h
459 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
463 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
465 ret = ERROR_INVALID_PARAMETER;
467 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
469 if (numNonLoopbackInterfaces > 0) {
470 DWORD numIPAddresses = getNumIPAddresses();
473 /* This may slightly overestimate the amount of space needed, because
474 * the IP addresses include the loopback address, but it's easier
475 * to make sure there's more than enough space than to make sure there's
476 * precisely enough space.
478 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
479 size += numIPAddresses * sizeof(IP_ADDR_STRING);
480 if (!pAdapterInfo || *pOutBufLen < size) {
482 ret = ERROR_BUFFER_OVERFLOW;
485 InterfaceIndexTable *table = NULL;
486 PMIB_IPADDRTABLE ipAddrTable = NULL;
487 PMIB_IPFORWARDTABLE routeTable = NULL;
489 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
491 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
493 table = getNonLoopbackInterfaceIndexTable();
495 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
496 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
497 if (*pOutBufLen < size) {
499 ret = ERROR_INSUFFICIENT_BUFFER;
504 BOOL winsEnabled = FALSE;
505 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
506 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
507 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
509 memset(pAdapterInfo, 0, size);
510 /* @@ Wine registry key: HKCU\Software\Wine\Network */
511 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
512 &hKey) == ERROR_SUCCESS) {
513 DWORD size = sizeof(primaryWINS.String);
516 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
517 (LPBYTE)primaryWINS.String, &size);
518 addr = inet_addr(primaryWINS.String);
519 if (addr != INADDR_NONE && addr != INADDR_ANY)
521 size = sizeof(secondaryWINS.String);
522 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
523 (LPBYTE)secondaryWINS.String, &size);
524 addr = inet_addr(secondaryWINS.String);
525 if (addr != INADDR_NONE && addr != INADDR_ANY)
529 for (ndx = 0; ndx < table->numIndexes; ndx++) {
530 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
532 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
533 BOOL firstIPAddr = TRUE;
535 /* on Win98 this is left empty, but whatever */
536 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
537 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
538 ptr->AddressLength = sizeof(ptr->Address);
539 getInterfacePhysicalByIndex(table->indexes[ndx],
540 &ptr->AddressLength, ptr->Address, &ptr->Type);
541 ptr->Index = table->indexes[ndx];
542 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
543 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
545 toIPAddressString(ipAddrTable->table[i].dwAddr,
546 ptr->IpAddressList.IpAddress.String);
547 toIPAddressString(ipAddrTable->table[i].dwMask,
548 ptr->IpAddressList.IpMask.String);
552 currentIPAddr->Next = nextIPAddr;
553 currentIPAddr = nextIPAddr;
554 toIPAddressString(ipAddrTable->table[i].dwAddr,
555 currentIPAddr->IpAddress.String);
556 toIPAddressString(ipAddrTable->table[i].dwMask,
557 currentIPAddr->IpMask.String);
562 /* Find first router through this interface, which we'll assume
563 * is the default gateway for this adapter */
564 for (i = 0; i < routeTable->dwNumEntries; i++)
565 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
566 && routeTable->table[i].dwForwardType ==
567 MIB_IPROUTE_TYPE_INDIRECT)
568 toIPAddressString(routeTable->table[i].dwForwardNextHop,
569 ptr->GatewayList.IpAddress.String);
571 ptr->HaveWins = TRUE;
572 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
573 primaryWINS.String, sizeof(primaryWINS.String));
574 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
575 secondaryWINS.String, sizeof(secondaryWINS.String));
577 if (ndx < table->numIndexes - 1)
578 ptr->Next = &pAdapterInfo[ndx + 1];
584 HeapFree(GetProcessHeap(), 0, table);
587 ret = ERROR_OUTOFMEMORY;
588 HeapFree(GetProcessHeap(), 0, routeTable);
589 HeapFree(GetProcessHeap(), 0, ipAddrTable);
595 TRACE("returning %d\n", ret);
599 static DWORD typeFromMibType(DWORD mib_type)
603 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
604 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
605 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
606 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
607 default: return IF_TYPE_OTHER;
611 static DWORD connectionTypeFromMibType(DWORD mib_type)
615 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
616 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
617 default: return NET_IF_CONNECTION_DEDICATED;
621 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
627 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
628 for (i = 0; i < at->dwNumEntries; i++)
630 if (at->table[i].dwIndex == index) (*num_addrs)++;
632 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
634 HeapFree(GetProcessHeap(), 0, at);
635 return ERROR_OUTOFMEMORY;
637 for (i = 0, j = 0; i < at->dwNumEntries; i++)
639 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
641 HeapFree(GetProcessHeap(), 0, at);
642 return ERROR_SUCCESS;
645 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
650 for (addrp = (const BYTE *)in_addr;
651 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
654 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
655 sprintf(p, "%d", *addrp);
657 p += sprintf(p, "%d.", *addrp);
662 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
664 const IN6_ADDR *addr = &sin->sin6_addr;
667 BOOL in_zero = FALSE;
669 for (i = 0; i < 7; i++)
671 if (!addr->u.Word[i])
683 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
687 sprintf(p, "%x", ntohs(addr->u.Word[7]));
691 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
693 DWORD i, num_gateways = 0;
695 for (i = 0; i < routeTable->dwNumEntries; i++)
697 if (routeTable->table[i].dwForwardIfIndex == index &&
698 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
704 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
705 PMIB_IPFORWARDTABLE routeTable)
708 PMIB_IPFORWARDROW row = NULL;
710 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
712 if (routeTable->table[i].dwForwardIfIndex == index &&
713 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
714 row = &routeTable->table[i];
719 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
720 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
722 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
723 DWORD *v4addrs = NULL;
724 SOCKET_ADDRESS *v6addrs = NULL;
725 PMIB_IPFORWARDTABLE routeTable = NULL;
727 if (family == WS_AF_INET)
729 if (!(flags & GAA_FLAG_SKIP_UNICAST))
730 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
731 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
733 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
734 GetProcessHeap(), 0);
736 num_v4_gateways = count_v4_gateways(index, routeTable);
739 else if (family == WS_AF_INET6)
741 if (!(flags & GAA_FLAG_SKIP_UNICAST))
742 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
744 else if (family == WS_AF_UNSPEC)
746 if (!(flags & GAA_FLAG_SKIP_UNICAST))
747 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
748 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
750 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
751 GetProcessHeap(), 0);
754 num_v4_gateways = count_v4_gateways(index, routeTable);
755 if (!(flags & GAA_FLAG_SKIP_UNICAST))
756 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
762 FIXME("address family %u unsupported\n", family);
767 HeapFree(GetProcessHeap(), 0, routeTable);
771 total_size = sizeof(IP_ADAPTER_ADDRESSES);
772 total_size += IF_NAMESIZE;
773 total_size += IF_NAMESIZE * sizeof(WCHAR);
774 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
775 total_size += IF_NAMESIZE * sizeof(WCHAR);
776 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
777 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
778 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
779 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
780 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
781 for (i = 0; i < num_v6addrs; i++)
782 total_size += v6addrs[i].iSockaddrLength;
784 if (aa && *size >= total_size)
786 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
788 DWORD buflen, type, status;
790 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
791 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
792 aa->u.s.IfIndex = index;
794 getInterfaceNameByIndex(index, name);
795 memcpy(ptr, name, IF_NAMESIZE);
796 aa->AdapterName = ptr;
798 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
800 aa->FriendlyName = (WCHAR *)ptr;
801 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
806 aa->Description = (WCHAR *)ptr;
807 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
812 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
816 PMIB_IPFORWARDROW adapterRow;
818 if ((adapterRow = findIPv4Gateway(index, routeTable)))
820 PIP_ADAPTER_GATEWAY_ADDRESS gw;
823 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
824 aa->FirstGatewayAddress = gw;
826 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
827 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
828 sin = (PSOCKADDR_IN)ptr;
829 sin->sin_family = AF_INET;
831 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
833 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
834 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
836 ptr += sizeof(SOCKADDR_IN);
841 IP_ADAPTER_UNICAST_ADDRESS *ua;
842 struct sockaddr_in *sa;
843 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
844 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
845 for (i = 0; i < num_v4addrs; i++)
849 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
850 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
851 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
852 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
854 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
855 sa->sin_family = AF_INET;
856 sa->sin_addr.s_addr = v4addrs[i];
858 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
859 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
861 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
862 if (i < num_v4addrs - 1)
864 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
871 IP_ADAPTER_UNICAST_ADDRESS *ua;
872 struct WS_sockaddr_in6 *sa;
874 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
875 if (aa->FirstUnicastAddress)
877 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
879 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
880 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
883 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
884 for (i = 0; i < num_v6addrs; i++)
888 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
889 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
890 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
891 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
893 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
894 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
895 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
896 debugstr_ipv6(sa, addr_buf));
898 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
899 if (i < num_v6addrs - 1)
901 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
907 buflen = MAX_INTERFACE_PHYSADDR;
908 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
909 aa->PhysicalAddressLength = buflen;
910 aa->IfType = typeFromMibType(type);
911 aa->ConnectionType = connectionTypeFromMibType(type);
913 getInterfaceMtuByName(name, &aa->Mtu);
915 getInterfaceStatusByName(name, &status);
916 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
917 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
918 else aa->OperStatus = IfOperStatusUnknown;
921 HeapFree(GetProcessHeap(), 0, routeTable);
922 HeapFree(GetProcessHeap(), 0, v6addrs);
923 HeapFree(GetProcessHeap(), 0, v4addrs);
924 return ERROR_SUCCESS;
927 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
931 initialise_resolver();
932 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
933 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
936 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
937 if (!address || *len < size)
940 return ERROR_BUFFER_OVERFLOW;
943 if (_res.nscount > 0)
945 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
948 for (i = 0, addr = address; i < _res.nscount && addr;
949 i++, addr = addr->Next)
953 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
954 addr->Address.lpSockaddr =
955 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
956 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
957 sin->sin_family = WS_AF_INET;
958 sin->sin_port = _res.nsaddr_list[i].sin_port;
959 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
960 if (i == _res.nscount - 1)
964 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
965 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
968 return ERROR_SUCCESS;
971 static BOOL is_ip_address_string(const char *str)
976 ret = inet_aton(str, &in);
980 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
983 char *found_suffix = NULL;
985 initialise_resolver();
986 /* Always return a NULL-terminated string, even if it's empty. */
987 size = sizeof(WCHAR);
988 for (i = 0, found_suffix = NULL;
989 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
991 /* This uses a heuristic to select a DNS suffix:
992 * the first, non-IP address string is selected.
994 if (!is_ip_address_string(_res.dnsrch[i]))
995 found_suffix = _res.dnsrch[i];
998 size += strlen(found_suffix) * sizeof(WCHAR);
999 if (!suffix || *len < size)
1002 return ERROR_BUFFER_OVERFLOW;
1009 for (p = found_suffix; *p; p++)
1013 return ERROR_SUCCESS;
1016 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1017 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1019 InterfaceIndexTable *table;
1020 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1022 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1024 if (!buflen) return ERROR_INVALID_PARAMETER;
1026 table = getInterfaceIndexTable();
1027 if (!table || !table->numIndexes)
1029 HeapFree(GetProcessHeap(), 0, table);
1030 return ERROR_NO_DATA;
1033 for (i = 0; i < table->numIndexes; i++)
1036 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1038 HeapFree(GetProcessHeap(), 0, table);
1043 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1045 /* Since DNS servers aren't really per adapter, get enough space for a
1046 * single copy of them.
1048 get_dns_server_addresses(NULL, &dns_server_size);
1049 total_size += dns_server_size;
1051 /* Since DNS suffix also isn't really per adapter, get enough space for a
1052 * single copy of it.
1054 get_dns_suffix(NULL, &dns_suffix_size);
1055 total_size += dns_suffix_size;
1056 if (aa && *buflen >= total_size)
1058 ULONG bytes_left = size = total_size;
1059 PIP_ADAPTER_ADDRESSES first_aa = aa;
1060 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1063 for (i = 0; i < table->numIndexes; i++)
1065 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1067 HeapFree(GetProcessHeap(), 0, table);
1070 if (i < table->numIndexes - 1)
1072 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1074 size = bytes_left -= size;
1077 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1079 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)aa + total_size - dns_server_size - dns_suffix_size);
1080 get_dns_server_addresses(firstDns, &dns_server_size);
1081 for (aa = first_aa; aa; aa = aa->Next)
1083 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1084 aa->FirstDnsServerAddress = firstDns;
1088 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1089 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1090 for (; aa; aa = aa->Next)
1092 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1093 aa->DnsSuffix = dnsSuffix;
1095 ret = ERROR_SUCCESS;
1097 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
1098 *buflen = total_size;
1100 TRACE("num adapters %u\n", table->numIndexes);
1101 HeapFree(GetProcessHeap(), 0, table);
1105 /******************************************************************
1106 * GetBestInterface (IPHLPAPI.@)
1108 * Get the interface, with the best route for the given IP address.
1111 * dwDestAddr [In] IP address to search the interface for
1112 * pdwBestIfIndex [Out] found best interface
1116 * Failure: error code from winerror.h
1118 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1120 struct WS_sockaddr_in sa_in;
1121 memset(&sa_in, 0, sizeof(sa_in));
1122 sa_in.sin_family = AF_INET;
1123 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1124 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1127 /******************************************************************
1128 * GetBestInterfaceEx (IPHLPAPI.@)
1130 * Get the interface, with the best route for the given IP address.
1133 * dwDestAddr [In] IP address to search the interface for
1134 * pdwBestIfIndex [Out] found best interface
1138 * Failure: error code from winerror.h
1140 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1144 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1145 if (!pDestAddr || !pdwBestIfIndex)
1146 ret = ERROR_INVALID_PARAMETER;
1148 MIB_IPFORWARDROW ipRow;
1150 if (pDestAddr->sa_family == AF_INET) {
1151 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1152 if (ret == ERROR_SUCCESS)
1153 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1155 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1156 ret = ERROR_NOT_SUPPORTED;
1159 TRACE("returning %d\n", ret);
1164 /******************************************************************
1165 * GetBestRoute (IPHLPAPI.@)
1167 * Get the best route for the given IP address.
1170 * dwDestAddr [In] IP address to search the best route for
1171 * dwSourceAddr [In] optional source IP address
1172 * pBestRoute [Out] found best route
1176 * Failure: error code from winerror.h
1178 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1180 PMIB_IPFORWARDTABLE table;
1183 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1184 dwSourceAddr, pBestRoute);
1186 return ERROR_INVALID_PARAMETER;
1188 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1190 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1192 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1193 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1194 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1195 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1196 DWORD numShifts, mask;
1198 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1199 mask && mask & 1; mask >>= 1, numShifts++)
1201 if (numShifts > matchedBits) {
1202 matchedBits = numShifts;
1205 else if (!matchedBits) {
1210 if (matchedNdx < table->dwNumEntries) {
1211 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1212 ret = ERROR_SUCCESS;
1215 /* No route matches, which can happen if there's no default route. */
1216 ret = ERROR_HOST_UNREACHABLE;
1218 HeapFree(GetProcessHeap(), 0, table);
1220 TRACE("returning %d\n", ret);
1225 /******************************************************************
1226 * GetFriendlyIfIndex (IPHLPAPI.@)
1228 * Get a "friendly" version of IfIndex, which is one that doesn't
1229 * have the top byte set. Doesn't validate whether IfIndex is a valid
1233 * IfIndex [In] interface index to get the friendly one for
1236 * A friendly version of IfIndex.
1238 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1240 /* windows doesn't validate these, either, just makes sure the top byte is
1241 cleared. I assume my ifenum module never gives an index with the top
1243 TRACE("returning %d\n", IfIndex);
1248 /******************************************************************
1249 * GetIfEntry (IPHLPAPI.@)
1251 * Get information about an interface.
1254 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1255 * Out: interface information
1259 * Failure: error code from winerror.h
1261 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1264 char nameBuf[MAX_ADAPTER_NAME];
1267 TRACE("pIfRow %p\n", pIfRow);
1269 return ERROR_INVALID_PARAMETER;
1271 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1273 ret = getInterfaceEntryByName(name, pIfRow);
1274 if (ret == NO_ERROR)
1275 ret = getInterfaceStatsByName(name, pIfRow);
1278 ret = ERROR_INVALID_DATA;
1279 TRACE("returning %d\n", ret);
1284 static int IfTableSorter(const void *a, const void *b)
1289 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1296 /******************************************************************
1297 * GetIfTable (IPHLPAPI.@)
1299 * Get a table of local interfaces.
1302 * pIfTable [Out] buffer for local interfaces table
1303 * pdwSize [In/Out] length of output buffer
1304 * bOrder [In] whether to sort the table
1308 * Failure: error code from winerror.h
1311 * If pdwSize is less than required, the function will return
1312 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1314 * If bOrder is true, the returned table will be sorted by interface index.
1316 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1320 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1323 ret = ERROR_INVALID_PARAMETER;
1325 DWORD numInterfaces = getNumInterfaces();
1326 ULONG size = sizeof(MIB_IFTABLE);
1328 if (numInterfaces > 1)
1329 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1330 if (!pIfTable || *pdwSize < size) {
1332 ret = ERROR_INSUFFICIENT_BUFFER;
1335 InterfaceIndexTable *table = getInterfaceIndexTable();
1338 size = sizeof(MIB_IFTABLE);
1339 if (table->numIndexes > 1)
1340 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1341 if (*pdwSize < size) {
1343 ret = ERROR_INSUFFICIENT_BUFFER;
1349 pIfTable->dwNumEntries = 0;
1350 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1351 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1352 GetIfEntry(&pIfTable->table[ndx]);
1353 pIfTable->dwNumEntries++;
1356 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1360 HeapFree(GetProcessHeap(), 0, table);
1363 ret = ERROR_OUTOFMEMORY;
1366 TRACE("returning %d\n", ret);
1371 /******************************************************************
1372 * GetInterfaceInfo (IPHLPAPI.@)
1374 * Get a list of network interface adapters.
1377 * pIfTable [Out] buffer for interface adapters
1378 * dwOutBufLen [Out] if buffer is too small, returns required size
1382 * Failure: error code from winerror.h
1385 * MSDN states this should return non-loopback interfaces only.
1387 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1391 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1393 ret = ERROR_INVALID_PARAMETER;
1395 DWORD numInterfaces = getNumInterfaces();
1396 ULONG size = sizeof(IP_INTERFACE_INFO);
1398 if (numInterfaces > 1)
1399 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1400 if (!pIfTable || *dwOutBufLen < size) {
1401 *dwOutBufLen = size;
1402 ret = ERROR_INSUFFICIENT_BUFFER;
1405 InterfaceIndexTable *table = getInterfaceIndexTable();
1408 size = sizeof(IP_INTERFACE_INFO);
1409 if (table->numIndexes > 1)
1410 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1411 if (*dwOutBufLen < size) {
1412 *dwOutBufLen = size;
1413 ret = ERROR_INSUFFICIENT_BUFFER;
1417 char nameBuf[MAX_ADAPTER_NAME];
1419 *dwOutBufLen = size;
1420 pIfTable->NumAdapters = 0;
1421 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1422 const char *walker, *name;
1425 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1426 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1427 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1428 walker && *walker &&
1429 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1430 walker++, assigner++)
1431 *assigner = *walker;
1433 pIfTable->NumAdapters++;
1437 HeapFree(GetProcessHeap(), 0, table);
1440 ret = ERROR_OUTOFMEMORY;
1443 TRACE("returning %d\n", ret);
1448 /******************************************************************
1449 * GetIpAddrTable (IPHLPAPI.@)
1451 * Get interface-to-IP address mapping table.
1454 * pIpAddrTable [Out] buffer for mapping table
1455 * pdwSize [In/Out] length of output buffer
1456 * bOrder [In] whether to sort the table
1460 * Failure: error code from winerror.h
1463 * If pdwSize is less than required, the function will return
1464 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1466 * If bOrder is true, the returned table will be sorted by the next hop and
1467 * an assortment of arbitrary parameters.
1469 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1473 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1476 ret = ERROR_INVALID_PARAMETER;
1478 PMIB_IPADDRTABLE table;
1480 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1481 if (ret == NO_ERROR)
1483 ULONG size = sizeof(MIB_IPADDRTABLE);
1485 if (table->dwNumEntries > 1)
1486 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1487 if (!pIpAddrTable || *pdwSize < size) {
1489 ret = ERROR_INSUFFICIENT_BUFFER;
1493 memcpy(pIpAddrTable, table, size);
1495 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1496 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1499 HeapFree(GetProcessHeap(), 0, table);
1502 TRACE("returning %d\n", ret);
1507 /******************************************************************
1508 * GetIpForwardTable (IPHLPAPI.@)
1510 * Get the route table.
1513 * pIpForwardTable [Out] buffer for route table
1514 * pdwSize [In/Out] length of output buffer
1515 * bOrder [In] whether to sort the table
1519 * Failure: error code from winerror.h
1522 * If pdwSize is less than required, the function will return
1523 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1525 * If bOrder is true, the returned table will be sorted by the next hop and
1526 * an assortment of arbitrary parameters.
1528 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1531 PMIB_IPFORWARDTABLE table;
1533 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1535 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1537 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1539 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1540 if (!pIpForwardTable || *pdwSize < size) {
1542 ret = ERROR_INSUFFICIENT_BUFFER;
1546 memcpy(pIpForwardTable, table, size);
1548 HeapFree(GetProcessHeap(), 0, table);
1550 TRACE("returning %d\n", ret);
1555 /******************************************************************
1556 * GetIpNetTable (IPHLPAPI.@)
1558 * Get the IP-to-physical address mapping table.
1561 * pIpNetTable [Out] buffer for mapping table
1562 * pdwSize [In/Out] length of output buffer
1563 * bOrder [In] whether to sort the table
1567 * Failure: error code from winerror.h
1570 * If pdwSize is less than required, the function will return
1571 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1573 * If bOrder is true, the returned table will be sorted by IP address.
1575 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1578 PMIB_IPNETTABLE table;
1580 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1582 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1584 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1586 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1587 if (!pIpNetTable || *pdwSize < size) {
1589 ret = ERROR_INSUFFICIENT_BUFFER;
1593 memcpy(pIpNetTable, table, size);
1595 HeapFree(GetProcessHeap(), 0, table);
1597 TRACE("returning %d\n", ret);
1601 /* Gets the DNS server list into the list beginning at list. Assumes that
1602 * a single server address may be placed at list if *len is at least
1603 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1604 * and assumes that all remaining DNS servers are contiguously located
1605 * beginning at firstDynamic. On input, *len is assumed to be the total number
1606 * of bytes available for all DNS servers, and is ignored if list is NULL.
1607 * On return, *len is set to the total number of bytes required for all DNS
1609 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1610 * ERROR_SUCCESS otherwise.
1612 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1613 PIP_ADDR_STRING firstDynamic, DWORD *len)
1617 initialise_resolver();
1618 size = _res.nscount * sizeof(IP_ADDR_STRING);
1619 if (!list || *len < size) {
1621 return ERROR_BUFFER_OVERFLOW;
1624 if (_res.nscount > 0) {
1625 PIP_ADDR_STRING ptr;
1628 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1629 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1630 ptr->IpAddress.String);
1631 if (i == _res.nscount - 1)
1634 ptr->Next = firstDynamic;
1636 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1639 return ERROR_SUCCESS;
1642 /******************************************************************
1643 * GetNetworkParams (IPHLPAPI.@)
1645 * Get the network parameters for the local computer.
1648 * pFixedInfo [Out] buffer for network parameters
1649 * pOutBufLen [In/Out] length of output buffer
1653 * Failure: error code from winerror.h
1656 * If pOutBufLen is less than required, the function will return
1657 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1660 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1662 DWORD ret, size, serverListSize;
1666 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1668 return ERROR_INVALID_PARAMETER;
1670 get_dns_server_list(NULL, NULL, &serverListSize);
1671 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1672 if (!pFixedInfo || *pOutBufLen < size) {
1674 return ERROR_BUFFER_OVERFLOW;
1677 memset(pFixedInfo, 0, size);
1678 size = sizeof(pFixedInfo->HostName);
1679 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1680 size = sizeof(pFixedInfo->DomainName);
1681 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1682 get_dns_server_list(&pFixedInfo->DnsServerList,
1683 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1685 /* Assume the first DNS server in the list is the "current" DNS server: */
1686 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1687 pFixedInfo->NodeType = HYBRID_NODETYPE;
1688 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1689 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1690 if (regReturn != ERROR_SUCCESS)
1691 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1692 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1694 if (regReturn == ERROR_SUCCESS)
1696 DWORD size = sizeof(pFixedInfo->ScopeId);
1698 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1702 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1703 I suppose could also check for a listener on port 53 to set EnableDns */
1705 TRACE("returning %d\n", ret);
1710 /******************************************************************
1711 * GetNumberOfInterfaces (IPHLPAPI.@)
1713 * Get the number of interfaces.
1716 * pdwNumIf [Out] number of interfaces
1719 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1721 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1725 TRACE("pdwNumIf %p\n", pdwNumIf);
1727 ret = ERROR_INVALID_PARAMETER;
1729 *pdwNumIf = getNumInterfaces();
1732 TRACE("returning %d\n", ret);
1737 /******************************************************************
1738 * GetPerAdapterInfo (IPHLPAPI.@)
1740 * Get information about an adapter corresponding to an interface.
1743 * IfIndex [In] interface info
1744 * pPerAdapterInfo [Out] buffer for per adapter info
1745 * pOutBufLen [In/Out] length of output buffer
1749 * Failure: error code from winerror.h
1751 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1753 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1754 DWORD ret = NO_ERROR;
1756 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1758 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1760 if (!isIfIndexLoopback(IfIndex)) {
1761 get_dns_server_list(NULL, NULL, &serverListSize);
1762 if (serverListSize > sizeof(IP_ADDR_STRING))
1763 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1765 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1767 *pOutBufLen = bytesNeeded;
1768 return ERROR_BUFFER_OVERFLOW;
1771 memset(pPerAdapterInfo, 0, bytesNeeded);
1772 if (!isIfIndexLoopback(IfIndex)) {
1773 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1774 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1776 /* Assume the first DNS server in the list is the "current" DNS server: */
1777 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1783 /******************************************************************
1784 * GetRTTAndHopCount (IPHLPAPI.@)
1786 * Get round-trip time (RTT) and hop count.
1790 * DestIpAddress [In] destination address to get the info for
1791 * HopCount [Out] retrieved hop count
1792 * MaxHops [In] maximum hops to search for the destination
1793 * RTT [Out] RTT in milliseconds
1800 * Stub, returns FALSE.
1802 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1804 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1805 DestIpAddress, HopCount, MaxHops, RTT);
1810 /******************************************************************
1811 * GetTcpTable (IPHLPAPI.@)
1813 * Get the table of active TCP connections.
1816 * pTcpTable [Out] buffer for TCP connections table
1817 * pdwSize [In/Out] length of output buffer
1818 * bOrder [In] whether to order the table
1822 * Failure: error code from winerror.h
1825 * If pdwSize is less than required, the function will return
1826 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1827 * the required byte size.
1828 * If bOrder is true, the returned table will be sorted, first by
1829 * local address and port number, then by remote address and port
1832 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1835 PMIB_TCPTABLE table;
1837 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1839 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1841 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1843 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1844 if (!pTcpTable || *pdwSize < size) {
1846 ret = ERROR_INSUFFICIENT_BUFFER;
1850 memcpy(pTcpTable, table, size);
1852 HeapFree(GetProcessHeap(), 0, table);
1854 TRACE("returning %d\n", ret);
1859 /******************************************************************
1860 * GetUdpTable (IPHLPAPI.@)
1862 * Get a table of active UDP connections.
1865 * pUdpTable [Out] buffer for UDP connections table
1866 * pdwSize [In/Out] length of output buffer
1867 * bOrder [In] whether to order the table
1871 * Failure: error code from winerror.h
1874 * If pdwSize is less than required, the function will return
1875 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1876 * required byte size.
1877 * If bOrder is true, the returned table will be sorted, first by
1878 * local address, then by local port number.
1880 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1883 PMIB_UDPTABLE table;
1885 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1887 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1889 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1891 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1892 if (!pUdpTable || *pdwSize < size) {
1894 ret = ERROR_INSUFFICIENT_BUFFER;
1898 memcpy(pUdpTable, table, size);
1900 HeapFree(GetProcessHeap(), 0, table);
1902 TRACE("returning %d\n", ret);
1907 /******************************************************************
1908 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1910 * This is a Win98-only function to get information on "unidirectional"
1911 * adapters. Since this is pretty nonsensical in other contexts, it
1912 * never returns anything.
1915 * pIPIfInfo [Out] buffer for adapter infos
1916 * dwOutBufLen [Out] length of the output buffer
1920 * Failure: error code from winerror.h
1923 * Stub, returns ERROR_NOT_SUPPORTED.
1925 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1927 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1928 /* a unidirectional adapter?? not bloody likely! */
1929 return ERROR_NOT_SUPPORTED;
1933 /******************************************************************
1934 * IpReleaseAddress (IPHLPAPI.@)
1936 * Release an IP obtained through DHCP,
1939 * AdapterInfo [In] adapter to release IP address
1943 * Failure: error code from winerror.h
1946 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1947 * this function does nothing.
1950 * Stub, returns ERROR_NOT_SUPPORTED.
1952 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1954 TRACE("AdapterInfo %p\n", AdapterInfo);
1955 /* not a stub, never going to support this (and I never mark an adapter as
1956 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1957 return ERROR_NOT_SUPPORTED;
1961 /******************************************************************
1962 * IpRenewAddress (IPHLPAPI.@)
1964 * Renew an IP obtained through DHCP.
1967 * AdapterInfo [In] adapter to renew 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 IpRenewAddress(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 * NotifyAddrChange (IPHLPAPI.@)
1992 * Notify caller whenever the ip-interface map is changed.
1995 * Handle [Out] handle usable in asynchronous notification
1996 * overlapped [In] overlapped structure that notifies the caller
2000 * Failure: error code from winerror.h
2003 * Stub, returns ERROR_NOT_SUPPORTED.
2005 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2007 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2008 return ERROR_NOT_SUPPORTED;
2012 /******************************************************************
2013 * NotifyRouteChange (IPHLPAPI.@)
2015 * Notify caller whenever the ip routing table is changed.
2018 * Handle [Out] handle usable in asynchronous notification
2019 * overlapped [In] overlapped structure that notifies the caller
2023 * Failure: error code from winerror.h
2026 * Stub, returns ERROR_NOT_SUPPORTED.
2028 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2030 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2031 return ERROR_NOT_SUPPORTED;
2035 /******************************************************************
2036 * SendARP (IPHLPAPI.@)
2038 * Send an ARP request.
2041 * DestIP [In] attempt to obtain this IP
2042 * SrcIP [In] optional sender IP address
2043 * pMacAddr [Out] buffer for the mac address
2044 * PhyAddrLen [In/Out] length of the output buffer
2048 * Failure: error code from winerror.h
2051 * Stub, returns ERROR_NOT_SUPPORTED.
2053 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2055 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2056 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2057 return ERROR_NOT_SUPPORTED;
2061 /******************************************************************
2062 * SetIfEntry (IPHLPAPI.@)
2064 * Set the administrative status of an interface.
2067 * pIfRow [In] dwAdminStatus member specifies the new status.
2071 * Failure: error code from winerror.h
2074 * Stub, returns ERROR_NOT_SUPPORTED.
2076 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2078 FIXME("(pIfRow %p): stub\n", pIfRow);
2079 /* this is supposed to set an interface administratively up or down.
2080 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2081 this sort of down is indistinguishable from other sorts of down (e.g. no
2083 return ERROR_NOT_SUPPORTED;
2087 /******************************************************************
2088 * SetIpForwardEntry (IPHLPAPI.@)
2090 * Modify an existing route.
2093 * pRoute [In] route with the new information
2097 * Failure: error code from winerror.h
2100 * Stub, returns NO_ERROR.
2102 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2104 FIXME("(pRoute %p): stub\n", pRoute);
2105 /* this is to add a route entry, how's it distinguishable from
2106 CreateIpForwardEntry?
2107 could use SIOCADDRT, not sure I want to */
2112 /******************************************************************
2113 * SetIpNetEntry (IPHLPAPI.@)
2115 * Modify an existing ARP entry.
2118 * pArpEntry [In] ARP entry with the new information
2122 * Failure: error code from winerror.h
2125 * Stub, returns NO_ERROR.
2127 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2129 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2130 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2135 /******************************************************************
2136 * SetIpStatistics (IPHLPAPI.@)
2138 * Toggle IP forwarding and det the default TTL value.
2141 * pIpStats [In] IP statistics with the new information
2145 * Failure: error code from winerror.h
2148 * Stub, returns NO_ERROR.
2150 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2152 FIXME("(pIpStats %p): stub\n", pIpStats);
2157 /******************************************************************
2158 * SetIpTTL (IPHLPAPI.@)
2160 * Set the default TTL value.
2163 * nTTL [In] new TTL value
2167 * Failure: error code from winerror.h
2170 * Stub, returns NO_ERROR.
2172 DWORD WINAPI SetIpTTL(UINT nTTL)
2174 FIXME("(nTTL %d): stub\n", nTTL);
2175 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2176 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2181 /******************************************************************
2182 * SetTcpEntry (IPHLPAPI.@)
2184 * Set the state of a TCP connection.
2187 * pTcpRow [In] specifies connection with new state
2191 * Failure: error code from winerror.h
2194 * Stub, returns NO_ERROR.
2196 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2198 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2203 /******************************************************************
2204 * UnenableRouter (IPHLPAPI.@)
2206 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2207 * if it reaches zero.
2210 * pOverlapped [In/Out] should be the same as in EnableRouter()
2211 * lpdwEnableCount [Out] optional, receives reference count
2215 * Failure: error code from winerror.h
2218 * Stub, returns ERROR_NOT_SUPPORTED.
2220 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2222 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2224 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2225 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2227 return ERROR_NOT_SUPPORTED;