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_NETINET_IN_H
27 # include <netinet/in.h>
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
59 #define IF_NAMESIZE 16
63 #define INADDR_NONE ~0UL
66 /* call res_init() just once because of a bug in Mac OS X 10.4 */
67 /* Call once per thread on systems that have per-thread _res. */
68 static void initialise_resolver(void)
70 if ((_res.options & RES_INIT) == 0)
74 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
77 case DLL_PROCESS_ATTACH:
78 DisableThreadLibraryCalls( hinstDLL );
81 case DLL_PROCESS_DETACH:
87 /******************************************************************
88 * AddIPAddress (IPHLPAPI.@)
90 * Add an IP address to an adapter.
93 * Address [In] IP address to add to the adapter
94 * IpMask [In] subnet mask for the IP address
95 * IfIndex [In] adapter index to add the address
96 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
97 * NTEInstance [Out] NTE instance for the IP address
101 * Failure: error code from winerror.h
104 * Stub. Currently returns ERROR_NOT_SUPPORTED.
106 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
109 return ERROR_NOT_SUPPORTED;
113 /******************************************************************
114 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
116 * Get table of local interfaces.
117 * Like GetIfTable(), but allocate the returned table from heap.
120 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
121 * allocated and returned.
122 * bOrder [In] whether to sort the table
123 * heap [In] heap from which the table is allocated
124 * flags [In] flags to HeapAlloc
127 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
128 * GetIfTable() returns otherwise.
130 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
131 BOOL bOrder, HANDLE heap, DWORD flags)
135 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
136 bOrder, heap, flags);
138 ret = ERROR_INVALID_PARAMETER;
142 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
143 if (ret == ERROR_INSUFFICIENT_BUFFER) {
144 *ppIfTable = HeapAlloc(heap, flags, dwSize);
145 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
148 TRACE("returning %d\n", ret);
153 static int IpAddrTableSorter(const void *a, const void *b)
158 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
165 /******************************************************************
166 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
168 * Get interface-to-IP address mapping table.
169 * Like GetIpAddrTable(), but allocate the returned table from heap.
172 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
173 * allocated and returned.
174 * bOrder [In] whether to sort the table
175 * heap [In] heap from which the table is allocated
176 * flags [In] flags to HeapAlloc
179 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
180 * failure, NO_ERROR on success.
182 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
183 BOOL bOrder, HANDLE heap, DWORD flags)
187 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
188 ppIpAddrTable, bOrder, heap, flags);
189 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
191 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
192 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
193 TRACE("returning %d\n", ret);
198 /******************************************************************
199 * CancelIPChangeNotify (IPHLPAPI.@)
201 * Cancel a previous notification created by NotifyAddrChange or
205 * overlapped [In] overlapped structure that notifies the caller
212 * Stub, returns FALSE.
214 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
216 FIXME("(overlapped %p): stub\n", overlapped);
222 /******************************************************************
223 * CreateIpForwardEntry (IPHLPAPI.@)
225 * Create a route in the local computer's IP table.
228 * pRoute [In] new route information
232 * Failure: error code from winerror.h
235 * Stub, always returns NO_ERROR.
237 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
239 FIXME("(pRoute %p): stub\n", pRoute);
240 /* could use SIOCADDRT, not sure I want to */
245 /******************************************************************
246 * CreateIpNetEntry (IPHLPAPI.@)
248 * Create entry in the ARP table.
251 * pArpEntry [In] new ARP entry
255 * Failure: error code from winerror.h
258 * Stub, always returns NO_ERROR.
260 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
262 FIXME("(pArpEntry %p)\n", pArpEntry);
263 /* could use SIOCSARP on systems that support it, not sure I want to */
268 /******************************************************************
269 * CreateProxyArpEntry (IPHLPAPI.@)
271 * Create a Proxy ARP (PARP) entry for an IP address.
274 * dwAddress [In] IP address for which this computer acts as a proxy.
275 * dwMask [In] subnet mask for dwAddress
276 * dwIfIndex [In] interface index
280 * Failure: error code from winerror.h
283 * Stub, returns ERROR_NOT_SUPPORTED.
285 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
287 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
288 dwAddress, dwMask, dwIfIndex);
289 return ERROR_NOT_SUPPORTED;
293 /******************************************************************
294 * DeleteIPAddress (IPHLPAPI.@)
296 * Delete an IP address added with AddIPAddress().
299 * NTEContext [In] NTE context from AddIPAddress();
303 * Failure: error code from winerror.h
306 * Stub, returns ERROR_NOT_SUPPORTED.
308 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
310 FIXME("(NTEContext %d): stub\n", NTEContext);
311 return ERROR_NOT_SUPPORTED;
315 /******************************************************************
316 * DeleteIpForwardEntry (IPHLPAPI.@)
321 * pRoute [In] route to delete
325 * Failure: error code from winerror.h
328 * Stub, returns NO_ERROR.
330 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
332 FIXME("(pRoute %p): stub\n", pRoute);
333 /* could use SIOCDELRT, not sure I want to */
338 /******************************************************************
339 * DeleteIpNetEntry (IPHLPAPI.@)
341 * Delete an ARP entry.
344 * pArpEntry [In] ARP entry to delete
348 * Failure: error code from winerror.h
351 * Stub, returns NO_ERROR.
353 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
355 FIXME("(pArpEntry %p): stub\n", pArpEntry);
356 /* could use SIOCDARP on systems that support it, not sure I want to */
361 /******************************************************************
362 * DeleteProxyArpEntry (IPHLPAPI.@)
364 * Delete a Proxy ARP entry.
367 * dwAddress [In] IP address for which this computer acts as a proxy.
368 * dwMask [In] subnet mask for dwAddress
369 * dwIfIndex [In] interface index
373 * Failure: error code from winerror.h
376 * Stub, returns ERROR_NOT_SUPPORTED.
378 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
380 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
381 dwAddress, dwMask, dwIfIndex);
382 return ERROR_NOT_SUPPORTED;
386 /******************************************************************
387 * EnableRouter (IPHLPAPI.@)
389 * Turn on ip forwarding.
393 * pOverlapped [In/Out] hEvent member should contain a valid handle.
396 * Success: ERROR_IO_PENDING
397 * Failure: error code from winerror.h
400 * Stub, returns ERROR_NOT_SUPPORTED.
402 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
404 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
405 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
406 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
408 return ERROR_NOT_SUPPORTED;
412 /******************************************************************
413 * FlushIpNetTable (IPHLPAPI.@)
415 * Delete all ARP entries of an interface
418 * dwIfIndex [In] interface index
422 * Failure: error code from winerror.h
425 * Stub, returns ERROR_NOT_SUPPORTED.
427 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
429 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
430 /* this flushes the arp cache of the given index */
431 return ERROR_NOT_SUPPORTED;
435 /******************************************************************
436 * GetAdapterIndex (IPHLPAPI.@)
438 * Get interface index from its name.
441 * AdapterName [In] unicode string with the adapter name
442 * IfIndex [Out] returns found interface index
446 * Failure: error code from winerror.h
448 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
450 char adapterName[MAX_ADAPTER_NAME];
454 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
455 /* The adapter name is guaranteed not to have any unicode characters, so
456 * this translation is never lossy */
457 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
458 adapterName[i] = (char)AdapterName[i];
459 adapterName[i] = '\0';
460 ret = getInterfaceIndexByName(adapterName, IfIndex);
461 TRACE("returning %d\n", ret);
466 /******************************************************************
467 * GetAdaptersInfo (IPHLPAPI.@)
469 * Get information about adapters.
472 * pAdapterInfo [Out] buffer for adapter infos
473 * pOutBufLen [In] length of output buffer
477 * Failure: error code from winerror.h
479 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
483 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
485 ret = ERROR_INVALID_PARAMETER;
487 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
489 if (numNonLoopbackInterfaces > 0) {
490 DWORD numIPAddresses = getNumIPAddresses();
493 /* This may slightly overestimate the amount of space needed, because
494 * the IP addresses include the loopback address, but it's easier
495 * to make sure there's more than enough space than to make sure there's
496 * precisely enough space.
498 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
499 size += numIPAddresses * sizeof(IP_ADDR_STRING);
500 if (!pAdapterInfo || *pOutBufLen < size) {
502 ret = ERROR_BUFFER_OVERFLOW;
505 InterfaceIndexTable *table = NULL;
506 PMIB_IPADDRTABLE ipAddrTable = NULL;
507 PMIB_IPFORWARDTABLE routeTable = NULL;
509 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
511 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
513 table = getNonLoopbackInterfaceIndexTable();
515 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
516 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
517 if (*pOutBufLen < size) {
519 ret = ERROR_INSUFFICIENT_BUFFER;
524 BOOL winsEnabled = FALSE;
525 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
526 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
527 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
529 memset(pAdapterInfo, 0, size);
530 /* @@ Wine registry key: HKCU\Software\Wine\Network */
531 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
532 &hKey) == ERROR_SUCCESS) {
533 DWORD size = sizeof(primaryWINS.String);
536 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
537 (LPBYTE)primaryWINS.String, &size);
538 addr = inet_addr(primaryWINS.String);
539 if (addr != INADDR_NONE && addr != INADDR_ANY)
541 size = sizeof(secondaryWINS.String);
542 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
543 (LPBYTE)secondaryWINS.String, &size);
544 addr = inet_addr(secondaryWINS.String);
545 if (addr != INADDR_NONE && addr != INADDR_ANY)
549 for (ndx = 0; ndx < table->numIndexes; ndx++) {
550 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
552 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
553 BOOL firstIPAddr = TRUE;
555 /* on Win98 this is left empty, but whatever */
556 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
557 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
558 ptr->AddressLength = sizeof(ptr->Address);
559 getInterfacePhysicalByIndex(table->indexes[ndx],
560 &ptr->AddressLength, ptr->Address, &ptr->Type);
561 ptr->Index = table->indexes[ndx];
562 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
563 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
565 toIPAddressString(ipAddrTable->table[i].dwAddr,
566 ptr->IpAddressList.IpAddress.String);
567 toIPAddressString(ipAddrTable->table[i].dwMask,
568 ptr->IpAddressList.IpMask.String);
572 currentIPAddr->Next = nextIPAddr;
573 currentIPAddr = nextIPAddr;
574 toIPAddressString(ipAddrTable->table[i].dwAddr,
575 currentIPAddr->IpAddress.String);
576 toIPAddressString(ipAddrTable->table[i].dwMask,
577 currentIPAddr->IpMask.String);
582 /* Find first router through this interface, which we'll assume
583 * is the default gateway for this adapter */
584 for (i = 0; i < routeTable->dwNumEntries; i++)
585 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
586 && routeTable->table[i].u1.ForwardType ==
587 MIB_IPROUTE_TYPE_INDIRECT)
588 toIPAddressString(routeTable->table[i].dwForwardNextHop,
589 ptr->GatewayList.IpAddress.String);
591 ptr->HaveWins = TRUE;
592 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
593 primaryWINS.String, sizeof(primaryWINS.String));
594 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
595 secondaryWINS.String, sizeof(secondaryWINS.String));
597 if (ndx < table->numIndexes - 1)
598 ptr->Next = &pAdapterInfo[ndx + 1];
602 ptr->DhcpEnabled = TRUE;
606 HeapFree(GetProcessHeap(), 0, table);
609 ret = ERROR_OUTOFMEMORY;
610 HeapFree(GetProcessHeap(), 0, routeTable);
611 HeapFree(GetProcessHeap(), 0, ipAddrTable);
617 TRACE("returning %d\n", ret);
621 static DWORD typeFromMibType(DWORD mib_type)
625 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
626 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
627 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
628 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
629 default: return IF_TYPE_OTHER;
633 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
637 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
638 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
639 default: return NET_IF_CONNECTION_DEDICATED;
643 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs)
649 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
650 for (i = 0; i < at->dwNumEntries; i++)
652 if (at->table[i].dwIndex == index) (*num_addrs)++;
654 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
656 HeapFree(GetProcessHeap(), 0, at);
657 return ERROR_OUTOFMEMORY;
659 for (i = 0, j = 0; i < at->dwNumEntries; i++)
661 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
663 HeapFree(GetProcessHeap(), 0, at);
664 return ERROR_SUCCESS;
667 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
672 for (addrp = (const BYTE *)in_addr;
673 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
676 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
677 sprintf(p, "%d", *addrp);
679 p += sprintf(p, "%d.", *addrp);
684 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
686 const IN6_ADDR *addr = &sin->sin6_addr;
689 BOOL in_zero = FALSE;
691 for (i = 0; i < 7; i++)
693 if (!addr->u.Word[i])
705 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
709 sprintf(p, "%x", ntohs(addr->u.Word[7]));
713 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
715 DWORD i, num_gateways = 0;
717 for (i = 0; i < routeTable->dwNumEntries; i++)
719 if (routeTable->table[i].dwForwardIfIndex == index &&
720 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
726 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
727 PMIB_IPFORWARDTABLE routeTable)
730 PMIB_IPFORWARDROW row = NULL;
732 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
734 if (routeTable->table[i].dwForwardIfIndex == index &&
735 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
736 row = &routeTable->table[i];
741 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
742 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
744 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
745 DWORD *v4addrs = NULL;
746 SOCKET_ADDRESS *v6addrs = NULL;
747 PMIB_IPFORWARDTABLE routeTable = NULL;
749 if (family == WS_AF_INET)
751 if (!(flags & GAA_FLAG_SKIP_UNICAST))
752 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
753 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
755 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
756 GetProcessHeap(), 0);
758 num_v4_gateways = count_v4_gateways(index, routeTable);
761 else if (family == WS_AF_INET6)
763 if (!(flags & GAA_FLAG_SKIP_UNICAST))
764 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
766 else if (family == WS_AF_UNSPEC)
768 if (!(flags & GAA_FLAG_SKIP_UNICAST))
769 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
770 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
772 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
773 GetProcessHeap(), 0);
775 num_v4_gateways = count_v4_gateways(index, routeTable);
777 if (!ret && !(flags & GAA_FLAG_SKIP_UNICAST))
778 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
782 FIXME("address family %u unsupported\n", family);
787 HeapFree(GetProcessHeap(), 0, routeTable);
791 total_size = sizeof(IP_ADAPTER_ADDRESSES);
792 total_size += IF_NAMESIZE;
793 total_size += IF_NAMESIZE * sizeof(WCHAR);
794 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
795 total_size += IF_NAMESIZE * sizeof(WCHAR);
796 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
797 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
798 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
799 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
800 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
801 for (i = 0; i < num_v6addrs; i++)
802 total_size += v6addrs[i].iSockaddrLength;
804 if (aa && *size >= total_size)
806 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
809 INTERNAL_IF_OPER_STATUS status;
811 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
812 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
813 aa->u.s.IfIndex = index;
815 getInterfaceNameByIndex(index, name);
816 memcpy(ptr, name, IF_NAMESIZE);
817 aa->AdapterName = ptr;
819 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
821 aa->FriendlyName = (WCHAR *)ptr;
822 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
827 aa->Description = (WCHAR *)ptr;
828 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
833 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
837 PMIB_IPFORWARDROW adapterRow;
839 if ((adapterRow = findIPv4Gateway(index, routeTable)))
841 PIP_ADAPTER_GATEWAY_ADDRESS gw;
844 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
845 aa->FirstGatewayAddress = gw;
847 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
848 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
849 sin = (PSOCKADDR_IN)ptr;
850 sin->sin_family = AF_INET;
852 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
854 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
855 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
857 ptr += sizeof(SOCKADDR_IN);
862 IP_ADAPTER_UNICAST_ADDRESS *ua;
863 struct sockaddr_in *sa;
864 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
865 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
866 for (i = 0; i < num_v4addrs; i++)
870 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
871 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
872 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
873 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
875 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
876 sa->sin_family = AF_INET;
877 sa->sin_addr.s_addr = v4addrs[i];
879 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
880 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
882 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
883 if (i < num_v4addrs - 1)
885 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
892 IP_ADAPTER_UNICAST_ADDRESS *ua;
893 struct WS_sockaddr_in6 *sa;
895 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
896 if (aa->FirstUnicastAddress)
898 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
900 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
901 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
904 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
905 for (i = 0; i < num_v6addrs; i++)
909 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
910 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
911 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
912 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
914 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
915 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
916 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
917 debugstr_ipv6(sa, addr_buf));
919 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
920 if (i < num_v6addrs - 1)
922 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
928 buflen = MAX_INTERFACE_PHYSADDR;
929 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
930 aa->PhysicalAddressLength = buflen;
931 aa->IfType = typeFromMibType(type);
932 aa->ConnectionType = connectionTypeFromMibType(type);
934 getInterfaceMtuByName(name, &aa->Mtu);
936 getInterfaceStatusByName(name, &status);
937 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
938 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
939 else aa->OperStatus = IfOperStatusUnknown;
942 HeapFree(GetProcessHeap(), 0, routeTable);
943 HeapFree(GetProcessHeap(), 0, v6addrs);
944 HeapFree(GetProcessHeap(), 0, v4addrs);
945 return ERROR_SUCCESS;
948 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
952 initialise_resolver();
953 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
954 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
957 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
958 if (!address || *len < size)
961 return ERROR_BUFFER_OVERFLOW;
964 if (_res.nscount > 0)
966 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
969 for (i = 0, addr = address; i < _res.nscount && addr;
970 i++, addr = addr->Next)
974 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
975 addr->Address.lpSockaddr =
976 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
977 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
978 sin->sin_family = WS_AF_INET;
979 sin->sin_port = _res.nsaddr_list[i].sin_port;
980 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
981 if (i == _res.nscount - 1)
985 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
986 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
989 return ERROR_SUCCESS;
992 static BOOL is_ip_address_string(const char *str)
997 ret = inet_aton(str, &in);
1001 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1004 char *found_suffix = NULL;
1006 initialise_resolver();
1007 /* Always return a NULL-terminated string, even if it's empty. */
1008 size = sizeof(WCHAR);
1009 for (i = 0, found_suffix = NULL;
1010 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1012 /* This uses a heuristic to select a DNS suffix:
1013 * the first, non-IP address string is selected.
1015 if (!is_ip_address_string(_res.dnsrch[i]))
1016 found_suffix = _res.dnsrch[i];
1019 size += strlen(found_suffix) * sizeof(WCHAR);
1020 if (!suffix || *len < size)
1023 return ERROR_BUFFER_OVERFLOW;
1030 for (p = found_suffix; *p; p++)
1034 return ERROR_SUCCESS;
1037 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1038 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1040 InterfaceIndexTable *table;
1041 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1043 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1045 if (!buflen) return ERROR_INVALID_PARAMETER;
1047 table = getInterfaceIndexTable();
1048 if (!table || !table->numIndexes)
1050 HeapFree(GetProcessHeap(), 0, table);
1051 return ERROR_NO_DATA;
1054 for (i = 0; i < table->numIndexes; i++)
1057 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1059 HeapFree(GetProcessHeap(), 0, table);
1064 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1066 /* Since DNS servers aren't really per adapter, get enough space for a
1067 * single copy of them.
1069 get_dns_server_addresses(NULL, &dns_server_size);
1070 total_size += dns_server_size;
1072 /* Since DNS suffix also isn't really per adapter, get enough space for a
1073 * single copy of it.
1075 get_dns_suffix(NULL, &dns_suffix_size);
1076 total_size += dns_suffix_size;
1077 if (aa && *buflen >= total_size)
1079 ULONG bytes_left = size = total_size;
1080 PIP_ADAPTER_ADDRESSES first_aa = aa;
1081 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1084 for (i = 0; i < table->numIndexes; i++)
1086 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1088 HeapFree(GetProcessHeap(), 0, table);
1091 if (i < table->numIndexes - 1)
1093 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1095 size = bytes_left -= size;
1098 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1100 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1101 get_dns_server_addresses(firstDns, &dns_server_size);
1102 for (aa = first_aa; aa; aa = aa->Next)
1104 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1105 aa->FirstDnsServerAddress = firstDns;
1109 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1110 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1111 for (; aa; aa = aa->Next)
1113 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1114 aa->DnsSuffix = dnsSuffix;
1116 aa->DnsSuffix = (WCHAR *)((BYTE*)dnsSuffix + dns_suffix_size - 2);
1118 ret = ERROR_SUCCESS;
1121 ret = ERROR_BUFFER_OVERFLOW;
1122 *buflen = total_size;
1124 TRACE("num adapters %u\n", table->numIndexes);
1125 HeapFree(GetProcessHeap(), 0, table);
1129 /******************************************************************
1130 * GetBestInterface (IPHLPAPI.@)
1132 * Get the interface, with the best route for the given IP address.
1135 * dwDestAddr [In] IP address to search the interface for
1136 * pdwBestIfIndex [Out] found best interface
1140 * Failure: error code from winerror.h
1142 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1144 struct WS_sockaddr_in sa_in;
1145 memset(&sa_in, 0, sizeof(sa_in));
1146 sa_in.sin_family = AF_INET;
1147 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1148 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1151 /******************************************************************
1152 * GetBestInterfaceEx (IPHLPAPI.@)
1154 * Get the interface, with the best route for the given IP address.
1157 * dwDestAddr [In] IP address to search the interface for
1158 * pdwBestIfIndex [Out] found best interface
1162 * Failure: error code from winerror.h
1164 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1168 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1169 if (!pDestAddr || !pdwBestIfIndex)
1170 ret = ERROR_INVALID_PARAMETER;
1172 MIB_IPFORWARDROW ipRow;
1174 if (pDestAddr->sa_family == AF_INET) {
1175 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1176 if (ret == ERROR_SUCCESS)
1177 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1179 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1180 ret = ERROR_NOT_SUPPORTED;
1183 TRACE("returning %d\n", ret);
1188 /******************************************************************
1189 * GetBestRoute (IPHLPAPI.@)
1191 * Get the best route for the given IP address.
1194 * dwDestAddr [In] IP address to search the best route for
1195 * dwSourceAddr [In] optional source IP address
1196 * pBestRoute [Out] found best route
1200 * Failure: error code from winerror.h
1202 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1204 PMIB_IPFORWARDTABLE table;
1207 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1208 dwSourceAddr, pBestRoute);
1210 return ERROR_INVALID_PARAMETER;
1212 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1214 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1216 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1217 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1218 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1219 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1220 DWORD numShifts, mask;
1222 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1223 mask && mask & 1; mask >>= 1, numShifts++)
1225 if (numShifts > matchedBits) {
1226 matchedBits = numShifts;
1229 else if (!matchedBits) {
1234 if (matchedNdx < table->dwNumEntries) {
1235 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1236 ret = ERROR_SUCCESS;
1239 /* No route matches, which can happen if there's no default route. */
1240 ret = ERROR_HOST_UNREACHABLE;
1242 HeapFree(GetProcessHeap(), 0, table);
1244 TRACE("returning %d\n", ret);
1249 /******************************************************************
1250 * GetFriendlyIfIndex (IPHLPAPI.@)
1252 * Get a "friendly" version of IfIndex, which is one that doesn't
1253 * have the top byte set. Doesn't validate whether IfIndex is a valid
1257 * IfIndex [In] interface index to get the friendly one for
1260 * A friendly version of IfIndex.
1262 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1264 /* windows doesn't validate these, either, just makes sure the top byte is
1265 cleared. I assume my ifenum module never gives an index with the top
1267 TRACE("returning %d\n", IfIndex);
1272 /******************************************************************
1273 * GetIfEntry (IPHLPAPI.@)
1275 * Get information about an interface.
1278 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1279 * Out: interface information
1283 * Failure: error code from winerror.h
1285 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1288 char nameBuf[MAX_ADAPTER_NAME];
1291 TRACE("pIfRow %p\n", pIfRow);
1293 return ERROR_INVALID_PARAMETER;
1295 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1297 ret = getInterfaceEntryByName(name, pIfRow);
1298 if (ret == NO_ERROR)
1299 ret = getInterfaceStatsByName(name, pIfRow);
1302 ret = ERROR_INVALID_DATA;
1303 TRACE("returning %d\n", ret);
1308 static int IfTableSorter(const void *a, const void *b)
1313 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1320 /******************************************************************
1321 * GetIfTable (IPHLPAPI.@)
1323 * Get a table of local interfaces.
1326 * pIfTable [Out] buffer for local interfaces table
1327 * pdwSize [In/Out] length of output buffer
1328 * bOrder [In] whether to sort the table
1332 * Failure: error code from winerror.h
1335 * If pdwSize is less than required, the function will return
1336 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1338 * If bOrder is true, the returned table will be sorted by interface index.
1340 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1344 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1347 ret = ERROR_INVALID_PARAMETER;
1349 DWORD numInterfaces = getNumInterfaces();
1350 ULONG size = sizeof(MIB_IFTABLE);
1352 if (numInterfaces > 1)
1353 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1354 if (!pIfTable || *pdwSize < size) {
1356 ret = ERROR_INSUFFICIENT_BUFFER;
1359 InterfaceIndexTable *table = getInterfaceIndexTable();
1362 size = sizeof(MIB_IFTABLE);
1363 if (table->numIndexes > 1)
1364 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1365 if (*pdwSize < size) {
1367 ret = ERROR_INSUFFICIENT_BUFFER;
1373 pIfTable->dwNumEntries = 0;
1374 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1375 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1376 GetIfEntry(&pIfTable->table[ndx]);
1377 pIfTable->dwNumEntries++;
1380 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1384 HeapFree(GetProcessHeap(), 0, table);
1387 ret = ERROR_OUTOFMEMORY;
1390 TRACE("returning %d\n", ret);
1395 /******************************************************************
1396 * GetInterfaceInfo (IPHLPAPI.@)
1398 * Get a list of network interface adapters.
1401 * pIfTable [Out] buffer for interface adapters
1402 * dwOutBufLen [Out] if buffer is too small, returns required size
1406 * Failure: error code from winerror.h
1409 * MSDN states this should return non-loopback interfaces only.
1411 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1415 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1417 ret = ERROR_INVALID_PARAMETER;
1419 DWORD numInterfaces = getNumInterfaces();
1420 ULONG size = sizeof(IP_INTERFACE_INFO);
1422 if (numInterfaces > 1)
1423 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1424 if (!pIfTable || *dwOutBufLen < size) {
1425 *dwOutBufLen = size;
1426 ret = ERROR_INSUFFICIENT_BUFFER;
1429 InterfaceIndexTable *table = getInterfaceIndexTable();
1432 size = sizeof(IP_INTERFACE_INFO);
1433 if (table->numIndexes > 1)
1434 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1435 if (*dwOutBufLen < size) {
1436 *dwOutBufLen = size;
1437 ret = ERROR_INSUFFICIENT_BUFFER;
1441 char nameBuf[MAX_ADAPTER_NAME];
1443 *dwOutBufLen = size;
1444 pIfTable->NumAdapters = 0;
1445 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1446 const char *walker, *name;
1449 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1450 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1451 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1452 walker && *walker &&
1453 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1454 walker++, assigner++)
1455 *assigner = *walker;
1457 pIfTable->NumAdapters++;
1461 HeapFree(GetProcessHeap(), 0, table);
1464 ret = ERROR_OUTOFMEMORY;
1467 TRACE("returning %d\n", ret);
1472 /******************************************************************
1473 * GetIpAddrTable (IPHLPAPI.@)
1475 * Get interface-to-IP address mapping table.
1478 * pIpAddrTable [Out] buffer for mapping table
1479 * pdwSize [In/Out] length of output buffer
1480 * bOrder [In] whether to sort the table
1484 * Failure: error code from winerror.h
1487 * If pdwSize is less than required, the function will return
1488 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1490 * If bOrder is true, the returned table will be sorted by the next hop and
1491 * an assortment of arbitrary parameters.
1493 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1497 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1500 ret = ERROR_INVALID_PARAMETER;
1502 PMIB_IPADDRTABLE table;
1504 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1505 if (ret == NO_ERROR)
1507 ULONG size = sizeof(MIB_IPADDRTABLE);
1509 if (table->dwNumEntries > 1)
1510 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1511 if (!pIpAddrTable || *pdwSize < size) {
1513 ret = ERROR_INSUFFICIENT_BUFFER;
1517 memcpy(pIpAddrTable, table, size);
1519 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1520 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1523 HeapFree(GetProcessHeap(), 0, table);
1526 TRACE("returning %d\n", ret);
1531 /******************************************************************
1532 * GetIpForwardTable (IPHLPAPI.@)
1534 * Get the route table.
1537 * pIpForwardTable [Out] buffer for route table
1538 * pdwSize [In/Out] length of output buffer
1539 * bOrder [In] whether to sort the table
1543 * Failure: error code from winerror.h
1546 * If pdwSize is less than required, the function will return
1547 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1549 * If bOrder is true, the returned table will be sorted by the next hop and
1550 * an assortment of arbitrary parameters.
1552 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1555 PMIB_IPFORWARDTABLE table;
1557 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1559 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1561 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1563 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1564 if (!pIpForwardTable || *pdwSize < size) {
1566 ret = ERROR_INSUFFICIENT_BUFFER;
1570 memcpy(pIpForwardTable, table, size);
1572 HeapFree(GetProcessHeap(), 0, table);
1574 TRACE("returning %d\n", ret);
1579 /******************************************************************
1580 * GetIpNetTable (IPHLPAPI.@)
1582 * Get the IP-to-physical address mapping table.
1585 * pIpNetTable [Out] buffer for mapping table
1586 * pdwSize [In/Out] length of output buffer
1587 * bOrder [In] whether to sort the table
1591 * Failure: error code from winerror.h
1594 * If pdwSize is less than required, the function will return
1595 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1597 * If bOrder is true, the returned table will be sorted by IP address.
1599 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1602 PMIB_IPNETTABLE table;
1604 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1606 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1608 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1610 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1611 if (!pIpNetTable || *pdwSize < size) {
1613 ret = ERROR_INSUFFICIENT_BUFFER;
1617 memcpy(pIpNetTable, table, size);
1619 HeapFree(GetProcessHeap(), 0, table);
1621 TRACE("returning %d\n", ret);
1625 /* Gets the DNS server list into the list beginning at list. Assumes that
1626 * a single server address may be placed at list if *len is at least
1627 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1628 * and assumes that all remaining DNS servers are contiguously located
1629 * beginning at firstDynamic. On input, *len is assumed to be the total number
1630 * of bytes available for all DNS servers, and is ignored if list is NULL.
1631 * On return, *len is set to the total number of bytes required for all DNS
1633 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1634 * ERROR_SUCCESS otherwise.
1636 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1637 PIP_ADDR_STRING firstDynamic, DWORD *len)
1641 initialise_resolver();
1642 size = _res.nscount * sizeof(IP_ADDR_STRING);
1643 if (!list || *len < size) {
1645 return ERROR_BUFFER_OVERFLOW;
1648 if (_res.nscount > 0) {
1649 PIP_ADDR_STRING ptr;
1652 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1653 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1654 ptr->IpAddress.String);
1655 if (i == _res.nscount - 1)
1658 ptr->Next = firstDynamic;
1660 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1663 return ERROR_SUCCESS;
1666 /******************************************************************
1667 * GetNetworkParams (IPHLPAPI.@)
1669 * Get the network parameters for the local computer.
1672 * pFixedInfo [Out] buffer for network parameters
1673 * pOutBufLen [In/Out] length of output buffer
1677 * Failure: error code from winerror.h
1680 * If pOutBufLen is less than required, the function will return
1681 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1684 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1686 DWORD ret, size, serverListSize;
1690 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1692 return ERROR_INVALID_PARAMETER;
1694 get_dns_server_list(NULL, NULL, &serverListSize);
1695 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1696 if (!pFixedInfo || *pOutBufLen < size) {
1698 return ERROR_BUFFER_OVERFLOW;
1701 memset(pFixedInfo, 0, size);
1702 size = sizeof(pFixedInfo->HostName);
1703 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1704 size = sizeof(pFixedInfo->DomainName);
1705 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1706 get_dns_server_list(&pFixedInfo->DnsServerList,
1707 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1709 /* Assume the first DNS server in the list is the "current" DNS server: */
1710 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1711 pFixedInfo->NodeType = HYBRID_NODETYPE;
1712 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1713 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1714 if (regReturn != ERROR_SUCCESS)
1715 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1716 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1718 if (regReturn == ERROR_SUCCESS)
1720 DWORD size = sizeof(pFixedInfo->ScopeId);
1722 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1726 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1727 I suppose could also check for a listener on port 53 to set EnableDns */
1729 TRACE("returning %d\n", ret);
1734 /******************************************************************
1735 * GetNumberOfInterfaces (IPHLPAPI.@)
1737 * Get the number of interfaces.
1740 * pdwNumIf [Out] number of interfaces
1743 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1745 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1749 TRACE("pdwNumIf %p\n", pdwNumIf);
1751 ret = ERROR_INVALID_PARAMETER;
1753 *pdwNumIf = getNumInterfaces();
1756 TRACE("returning %d\n", ret);
1761 /******************************************************************
1762 * GetPerAdapterInfo (IPHLPAPI.@)
1764 * Get information about an adapter corresponding to an interface.
1767 * IfIndex [In] interface info
1768 * pPerAdapterInfo [Out] buffer for per adapter info
1769 * pOutBufLen [In/Out] length of output buffer
1773 * Failure: error code from winerror.h
1775 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1777 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1778 DWORD ret = NO_ERROR;
1780 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1782 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1784 if (!isIfIndexLoopback(IfIndex)) {
1785 get_dns_server_list(NULL, NULL, &serverListSize);
1786 if (serverListSize > sizeof(IP_ADDR_STRING))
1787 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1789 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1791 *pOutBufLen = bytesNeeded;
1792 return ERROR_BUFFER_OVERFLOW;
1795 memset(pPerAdapterInfo, 0, bytesNeeded);
1796 if (!isIfIndexLoopback(IfIndex)) {
1797 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1798 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1800 /* Assume the first DNS server in the list is the "current" DNS server: */
1801 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1807 /******************************************************************
1808 * GetRTTAndHopCount (IPHLPAPI.@)
1810 * Get round-trip time (RTT) and hop count.
1814 * DestIpAddress [In] destination address to get the info for
1815 * HopCount [Out] retrieved hop count
1816 * MaxHops [In] maximum hops to search for the destination
1817 * RTT [Out] RTT in milliseconds
1824 * Stub, returns FALSE.
1826 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1828 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1829 DestIpAddress, HopCount, MaxHops, RTT);
1834 /******************************************************************
1835 * GetTcpTable (IPHLPAPI.@)
1837 * Get the table of active TCP connections.
1840 * pTcpTable [Out] buffer for TCP connections table
1841 * pdwSize [In/Out] length of output buffer
1842 * bOrder [In] whether to order the table
1846 * Failure: error code from winerror.h
1849 * If pdwSize is less than required, the function will return
1850 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1851 * the required byte size.
1852 * If bOrder is true, the returned table will be sorted, first by
1853 * local address and port number, then by remote address and port
1856 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1858 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1859 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, AF_INET, TCP_TABLE_BASIC_ALL, 0);
1862 /******************************************************************
1863 * GetExtendedTcpTable (IPHLPAPI.@)
1865 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
1866 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
1871 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
1872 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
1874 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1876 if (ulAf != AF_INET ||
1877 (TableClass != TCP_TABLE_BASIC_ALL && TableClass != TCP_TABLE_OWNER_PID_ALL))
1879 FIXME("ulAf = %u, TableClass = %u not supported\n", ulAf, TableClass);
1880 return ERROR_NOT_SUPPORTED;
1882 if ((ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
1885 if (!pTcpTable || *pdwSize < size)
1888 ret = ERROR_INSUFFICIENT_BUFFER;
1893 memcpy(pTcpTable, table, size);
1895 HeapFree(GetProcessHeap(), 0, table);
1899 /******************************************************************
1900 * GetUdpTable (IPHLPAPI.@)
1902 * Get a table of active UDP connections.
1905 * pUdpTable [Out] buffer for UDP connections table
1906 * pdwSize [In/Out] length of output buffer
1907 * bOrder [In] whether to order the table
1911 * Failure: error code from winerror.h
1914 * If pdwSize is less than required, the function will return
1915 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1916 * required byte size.
1917 * If bOrder is true, the returned table will be sorted, first by
1918 * local address, then by local port number.
1920 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1922 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, AF_INET, UDP_TABLE_BASIC, 0);
1925 /******************************************************************
1926 * GetExtendedUdpTable (IPHLPAPI.@)
1928 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
1929 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
1934 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
1935 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
1937 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1939 if (ulAf != AF_INET ||
1940 (TableClass != UDP_TABLE_BASIC && TableClass != UDP_TABLE_OWNER_PID &&
1941 TableClass != UDP_TABLE_OWNER_MODULE))
1943 FIXME("ulAf = %u, TableClass = %u not supported\n", ulAf, TableClass);
1944 return ERROR_NOT_SUPPORTED;
1946 if (TableClass == UDP_TABLE_OWNER_MODULE)
1947 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
1949 if ((ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
1952 if (!pUdpTable || *pdwSize < size)
1955 ret = ERROR_INSUFFICIENT_BUFFER;
1960 memcpy(pUdpTable, table, size);
1962 HeapFree(GetProcessHeap(), 0, table);
1966 /******************************************************************
1967 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1969 * This is a Win98-only function to get information on "unidirectional"
1970 * adapters. Since this is pretty nonsensical in other contexts, it
1971 * never returns anything.
1974 * pIPIfInfo [Out] buffer for adapter infos
1975 * dwOutBufLen [Out] length of the output buffer
1979 * Failure: error code from winerror.h
1982 * Stub, returns ERROR_NOT_SUPPORTED.
1984 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1986 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1987 /* a unidirectional adapter?? not bloody likely! */
1988 return ERROR_NOT_SUPPORTED;
1992 /******************************************************************
1993 * IpReleaseAddress (IPHLPAPI.@)
1995 * Release an IP obtained through DHCP,
1998 * AdapterInfo [In] adapter to release IP address
2002 * Failure: error code from winerror.h
2005 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2006 * this function does nothing.
2009 * Stub, returns ERROR_NOT_SUPPORTED.
2011 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2013 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2014 return ERROR_NOT_SUPPORTED;
2018 /******************************************************************
2019 * IpRenewAddress (IPHLPAPI.@)
2021 * Renew an IP obtained through DHCP.
2024 * AdapterInfo [In] adapter to renew IP address
2028 * Failure: error code from winerror.h
2031 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2032 * this function does nothing.
2035 * Stub, returns ERROR_NOT_SUPPORTED.
2037 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2039 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2040 return ERROR_NOT_SUPPORTED;
2044 /******************************************************************
2045 * NotifyAddrChange (IPHLPAPI.@)
2047 * Notify caller whenever the ip-interface map is changed.
2050 * Handle [Out] handle usable in asynchronous notification
2051 * overlapped [In] overlapped structure that notifies the caller
2055 * Failure: error code from winerror.h
2058 * Stub, returns ERROR_NOT_SUPPORTED.
2060 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2062 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2063 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2064 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2065 return ERROR_IO_PENDING;
2069 /******************************************************************
2070 * NotifyRouteChange (IPHLPAPI.@)
2072 * Notify caller whenever the ip routing table is changed.
2075 * Handle [Out] handle usable in asynchronous notification
2076 * overlapped [In] overlapped structure that notifies the caller
2080 * Failure: error code from winerror.h
2083 * Stub, returns ERROR_NOT_SUPPORTED.
2085 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2087 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2088 return ERROR_NOT_SUPPORTED;
2092 /******************************************************************
2093 * SendARP (IPHLPAPI.@)
2095 * Send an ARP request.
2098 * DestIP [In] attempt to obtain this IP
2099 * SrcIP [In] optional sender IP address
2100 * pMacAddr [Out] buffer for the mac address
2101 * PhyAddrLen [In/Out] length of the output buffer
2105 * Failure: error code from winerror.h
2108 * Stub, returns ERROR_NOT_SUPPORTED.
2110 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2112 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2113 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2114 return ERROR_NOT_SUPPORTED;
2118 /******************************************************************
2119 * SetIfEntry (IPHLPAPI.@)
2121 * Set the administrative status of an interface.
2124 * pIfRow [In] dwAdminStatus member specifies the new status.
2128 * Failure: error code from winerror.h
2131 * Stub, returns ERROR_NOT_SUPPORTED.
2133 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2135 FIXME("(pIfRow %p): stub\n", pIfRow);
2136 /* this is supposed to set an interface administratively up or down.
2137 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2138 this sort of down is indistinguishable from other sorts of down (e.g. no
2140 return ERROR_NOT_SUPPORTED;
2144 /******************************************************************
2145 * SetIpForwardEntry (IPHLPAPI.@)
2147 * Modify an existing route.
2150 * pRoute [In] route with the new information
2154 * Failure: error code from winerror.h
2157 * Stub, returns NO_ERROR.
2159 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2161 FIXME("(pRoute %p): stub\n", pRoute);
2162 /* this is to add a route entry, how's it distinguishable from
2163 CreateIpForwardEntry?
2164 could use SIOCADDRT, not sure I want to */
2169 /******************************************************************
2170 * SetIpNetEntry (IPHLPAPI.@)
2172 * Modify an existing ARP entry.
2175 * pArpEntry [In] ARP entry with the new information
2179 * Failure: error code from winerror.h
2182 * Stub, returns NO_ERROR.
2184 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2186 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2187 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2192 /******************************************************************
2193 * SetIpStatistics (IPHLPAPI.@)
2195 * Toggle IP forwarding and det the default TTL value.
2198 * pIpStats [In] IP statistics with the new information
2202 * Failure: error code from winerror.h
2205 * Stub, returns NO_ERROR.
2207 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2209 FIXME("(pIpStats %p): stub\n", pIpStats);
2214 /******************************************************************
2215 * SetIpTTL (IPHLPAPI.@)
2217 * Set the default TTL value.
2220 * nTTL [In] new TTL value
2224 * Failure: error code from winerror.h
2227 * Stub, returns NO_ERROR.
2229 DWORD WINAPI SetIpTTL(UINT nTTL)
2231 FIXME("(nTTL %d): stub\n", nTTL);
2232 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2233 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2238 /******************************************************************
2239 * SetTcpEntry (IPHLPAPI.@)
2241 * Set the state of a TCP connection.
2244 * pTcpRow [In] specifies connection with new state
2248 * Failure: error code from winerror.h
2251 * Stub, returns NO_ERROR.
2253 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2255 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2260 /******************************************************************
2261 * UnenableRouter (IPHLPAPI.@)
2263 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2264 * if it reaches zero.
2267 * pOverlapped [In/Out] should be the same as in EnableRouter()
2268 * lpdwEnableCount [Out] optional, receives reference count
2272 * Failure: error code from winerror.h
2275 * Stub, returns ERROR_NOT_SUPPORTED.
2277 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2279 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2281 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2282 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2284 return ERROR_NOT_SUPPORTED;
2287 /******************************************************************
2288 * PfCreateInterface (IPHLPAPI.@)
2290 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2291 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2293 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2294 return ERROR_CALL_NOT_IMPLEMENTED;
2297 /******************************************************************
2298 * GetTcpTable2 (IPHLPAPI.@)
2300 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
2302 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
2303 return ERROR_NOT_SUPPORTED;
2306 /******************************************************************
2307 * GetTcp6Table (IPHLPAPI.@)
2309 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
2311 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table, size, order);
2312 return ERROR_NOT_SUPPORTED;
2315 /******************************************************************
2316 * GetTcp6Table2 (IPHLPAPI.@)
2318 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
2320 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
2321 return ERROR_NOT_SUPPORTED;