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].dwForwardType ==
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];
604 HeapFree(GetProcessHeap(), 0, table);
607 ret = ERROR_OUTOFMEMORY;
608 HeapFree(GetProcessHeap(), 0, routeTable);
609 HeapFree(GetProcessHeap(), 0, ipAddrTable);
615 TRACE("returning %d\n", ret);
619 static DWORD typeFromMibType(DWORD mib_type)
623 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
624 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
625 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
626 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
627 default: return IF_TYPE_OTHER;
631 static DWORD connectionTypeFromMibType(DWORD mib_type)
635 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
636 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
637 default: return NET_IF_CONNECTION_DEDICATED;
641 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
647 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
648 for (i = 0; i < at->dwNumEntries; i++)
650 if (at->table[i].dwIndex == index) (*num_addrs)++;
652 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
654 HeapFree(GetProcessHeap(), 0, at);
655 return ERROR_OUTOFMEMORY;
657 for (i = 0, j = 0; i < at->dwNumEntries; i++)
659 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
661 HeapFree(GetProcessHeap(), 0, at);
662 return ERROR_SUCCESS;
665 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
670 for (addrp = (const BYTE *)in_addr;
671 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
674 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
675 sprintf(p, "%d", *addrp);
677 p += sprintf(p, "%d.", *addrp);
682 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
684 const IN6_ADDR *addr = &sin->sin6_addr;
687 BOOL in_zero = FALSE;
689 for (i = 0; i < 7; i++)
691 if (!addr->u.Word[i])
703 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
707 sprintf(p, "%x", ntohs(addr->u.Word[7]));
711 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
713 DWORD i, num_gateways = 0;
715 for (i = 0; i < routeTable->dwNumEntries; i++)
717 if (routeTable->table[i].dwForwardIfIndex == index &&
718 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
724 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
725 PMIB_IPFORWARDTABLE routeTable)
728 PMIB_IPFORWARDROW row = NULL;
730 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
732 if (routeTable->table[i].dwForwardIfIndex == index &&
733 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
734 row = &routeTable->table[i];
739 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
740 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
742 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
743 DWORD *v4addrs = NULL;
744 SOCKET_ADDRESS *v6addrs = NULL;
745 PMIB_IPFORWARDTABLE routeTable = NULL;
747 if (family == WS_AF_INET)
749 if (!(flags & GAA_FLAG_SKIP_UNICAST))
750 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
751 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
753 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
754 GetProcessHeap(), 0);
756 num_v4_gateways = count_v4_gateways(index, routeTable);
759 else if (family == WS_AF_INET6)
761 if (!(flags & GAA_FLAG_SKIP_UNICAST))
762 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
764 else if (family == WS_AF_UNSPEC)
766 if (!(flags & GAA_FLAG_SKIP_UNICAST))
767 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
768 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
770 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
771 GetProcessHeap(), 0);
773 num_v4_gateways = count_v4_gateways(index, routeTable);
775 if (!ret && !(flags & GAA_FLAG_SKIP_UNICAST))
776 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
780 FIXME("address family %u unsupported\n", family);
785 HeapFree(GetProcessHeap(), 0, routeTable);
789 total_size = sizeof(IP_ADAPTER_ADDRESSES);
790 total_size += IF_NAMESIZE;
791 total_size += IF_NAMESIZE * sizeof(WCHAR);
792 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
793 total_size += IF_NAMESIZE * sizeof(WCHAR);
794 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
795 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
796 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
797 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
798 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
799 for (i = 0; i < num_v6addrs; i++)
800 total_size += v6addrs[i].iSockaddrLength;
802 if (aa && *size >= total_size)
804 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
806 DWORD buflen, type, status;
808 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
809 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
810 aa->u.s.IfIndex = index;
812 getInterfaceNameByIndex(index, name);
813 memcpy(ptr, name, IF_NAMESIZE);
814 aa->AdapterName = ptr;
816 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
818 aa->FriendlyName = (WCHAR *)ptr;
819 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
824 aa->Description = (WCHAR *)ptr;
825 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
830 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
834 PMIB_IPFORWARDROW adapterRow;
836 if ((adapterRow = findIPv4Gateway(index, routeTable)))
838 PIP_ADAPTER_GATEWAY_ADDRESS gw;
841 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
842 aa->FirstGatewayAddress = gw;
844 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
845 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
846 sin = (PSOCKADDR_IN)ptr;
847 sin->sin_family = AF_INET;
849 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
851 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
852 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
854 ptr += sizeof(SOCKADDR_IN);
859 IP_ADAPTER_UNICAST_ADDRESS *ua;
860 struct sockaddr_in *sa;
861 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
862 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
863 for (i = 0; i < num_v4addrs; i++)
867 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
868 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
869 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
870 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
872 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
873 sa->sin_family = AF_INET;
874 sa->sin_addr.s_addr = v4addrs[i];
876 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
877 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
879 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
880 if (i < num_v4addrs - 1)
882 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
889 IP_ADAPTER_UNICAST_ADDRESS *ua;
890 struct WS_sockaddr_in6 *sa;
892 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
893 if (aa->FirstUnicastAddress)
895 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
897 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
898 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
901 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
902 for (i = 0; i < num_v6addrs; i++)
906 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
907 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
908 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
909 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
911 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
912 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
913 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
914 debugstr_ipv6(sa, addr_buf));
916 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
917 if (i < num_v6addrs - 1)
919 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
925 buflen = MAX_INTERFACE_PHYSADDR;
926 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
927 aa->PhysicalAddressLength = buflen;
928 aa->IfType = typeFromMibType(type);
929 aa->ConnectionType = connectionTypeFromMibType(type);
931 getInterfaceMtuByName(name, &aa->Mtu);
933 getInterfaceStatusByName(name, &status);
934 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
935 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
936 else aa->OperStatus = IfOperStatusUnknown;
939 HeapFree(GetProcessHeap(), 0, routeTable);
940 HeapFree(GetProcessHeap(), 0, v6addrs);
941 HeapFree(GetProcessHeap(), 0, v4addrs);
942 return ERROR_SUCCESS;
945 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
949 initialise_resolver();
950 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
951 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
954 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
955 if (!address || *len < size)
958 return ERROR_BUFFER_OVERFLOW;
961 if (_res.nscount > 0)
963 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
966 for (i = 0, addr = address; i < _res.nscount && addr;
967 i++, addr = addr->Next)
971 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
972 addr->Address.lpSockaddr =
973 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
974 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
975 sin->sin_family = WS_AF_INET;
976 sin->sin_port = _res.nsaddr_list[i].sin_port;
977 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
978 if (i == _res.nscount - 1)
982 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
983 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
986 return ERROR_SUCCESS;
989 static BOOL is_ip_address_string(const char *str)
994 ret = inet_aton(str, &in);
998 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1001 char *found_suffix = NULL;
1003 initialise_resolver();
1004 /* Always return a NULL-terminated string, even if it's empty. */
1005 size = sizeof(WCHAR);
1006 for (i = 0, found_suffix = NULL;
1007 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1009 /* This uses a heuristic to select a DNS suffix:
1010 * the first, non-IP address string is selected.
1012 if (!is_ip_address_string(_res.dnsrch[i]))
1013 found_suffix = _res.dnsrch[i];
1016 size += strlen(found_suffix) * sizeof(WCHAR);
1017 if (!suffix || *len < size)
1020 return ERROR_BUFFER_OVERFLOW;
1027 for (p = found_suffix; *p; p++)
1031 return ERROR_SUCCESS;
1034 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1035 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1037 InterfaceIndexTable *table;
1038 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1040 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1042 if (!buflen) return ERROR_INVALID_PARAMETER;
1044 table = getInterfaceIndexTable();
1045 if (!table || !table->numIndexes)
1047 HeapFree(GetProcessHeap(), 0, table);
1048 return ERROR_NO_DATA;
1051 for (i = 0; i < table->numIndexes; i++)
1054 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1056 HeapFree(GetProcessHeap(), 0, table);
1061 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1063 /* Since DNS servers aren't really per adapter, get enough space for a
1064 * single copy of them.
1066 get_dns_server_addresses(NULL, &dns_server_size);
1067 total_size += dns_server_size;
1069 /* Since DNS suffix also isn't really per adapter, get enough space for a
1070 * single copy of it.
1072 get_dns_suffix(NULL, &dns_suffix_size);
1073 total_size += dns_suffix_size;
1074 if (aa && *buflen >= total_size)
1076 ULONG bytes_left = size = total_size;
1077 PIP_ADAPTER_ADDRESSES first_aa = aa;
1078 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1081 for (i = 0; i < table->numIndexes; i++)
1083 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1085 HeapFree(GetProcessHeap(), 0, table);
1088 if (i < table->numIndexes - 1)
1090 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1092 size = bytes_left -= size;
1095 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1097 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1098 get_dns_server_addresses(firstDns, &dns_server_size);
1099 for (aa = first_aa; aa; aa = aa->Next)
1101 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1102 aa->FirstDnsServerAddress = firstDns;
1106 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1107 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1108 for (; aa; aa = aa->Next)
1110 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1111 aa->DnsSuffix = dnsSuffix;
1113 aa->DnsSuffix = (WCHAR *)((BYTE*)dnsSuffix + dns_suffix_size - 2);
1115 ret = ERROR_SUCCESS;
1118 ret = ERROR_BUFFER_OVERFLOW;
1119 *buflen = total_size;
1121 TRACE("num adapters %u\n", table->numIndexes);
1122 HeapFree(GetProcessHeap(), 0, table);
1126 /******************************************************************
1127 * GetBestInterface (IPHLPAPI.@)
1129 * Get the interface, with the best route for the given IP address.
1132 * dwDestAddr [In] IP address to search the interface for
1133 * pdwBestIfIndex [Out] found best interface
1137 * Failure: error code from winerror.h
1139 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1141 struct WS_sockaddr_in sa_in;
1142 memset(&sa_in, 0, sizeof(sa_in));
1143 sa_in.sin_family = AF_INET;
1144 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1145 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1148 /******************************************************************
1149 * GetBestInterfaceEx (IPHLPAPI.@)
1151 * Get the interface, with the best route for the given IP address.
1154 * dwDestAddr [In] IP address to search the interface for
1155 * pdwBestIfIndex [Out] found best interface
1159 * Failure: error code from winerror.h
1161 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1165 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1166 if (!pDestAddr || !pdwBestIfIndex)
1167 ret = ERROR_INVALID_PARAMETER;
1169 MIB_IPFORWARDROW ipRow;
1171 if (pDestAddr->sa_family == AF_INET) {
1172 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1173 if (ret == ERROR_SUCCESS)
1174 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1176 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1177 ret = ERROR_NOT_SUPPORTED;
1180 TRACE("returning %d\n", ret);
1185 /******************************************************************
1186 * GetBestRoute (IPHLPAPI.@)
1188 * Get the best route for the given IP address.
1191 * dwDestAddr [In] IP address to search the best route for
1192 * dwSourceAddr [In] optional source IP address
1193 * pBestRoute [Out] found best route
1197 * Failure: error code from winerror.h
1199 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1201 PMIB_IPFORWARDTABLE table;
1204 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1205 dwSourceAddr, pBestRoute);
1207 return ERROR_INVALID_PARAMETER;
1209 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1211 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1213 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1214 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1215 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1216 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1217 DWORD numShifts, mask;
1219 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1220 mask && mask & 1; mask >>= 1, numShifts++)
1222 if (numShifts > matchedBits) {
1223 matchedBits = numShifts;
1226 else if (!matchedBits) {
1231 if (matchedNdx < table->dwNumEntries) {
1232 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1233 ret = ERROR_SUCCESS;
1236 /* No route matches, which can happen if there's no default route. */
1237 ret = ERROR_HOST_UNREACHABLE;
1239 HeapFree(GetProcessHeap(), 0, table);
1241 TRACE("returning %d\n", ret);
1246 /******************************************************************
1247 * GetFriendlyIfIndex (IPHLPAPI.@)
1249 * Get a "friendly" version of IfIndex, which is one that doesn't
1250 * have the top byte set. Doesn't validate whether IfIndex is a valid
1254 * IfIndex [In] interface index to get the friendly one for
1257 * A friendly version of IfIndex.
1259 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1261 /* windows doesn't validate these, either, just makes sure the top byte is
1262 cleared. I assume my ifenum module never gives an index with the top
1264 TRACE("returning %d\n", IfIndex);
1269 /******************************************************************
1270 * GetIfEntry (IPHLPAPI.@)
1272 * Get information about an interface.
1275 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1276 * Out: interface information
1280 * Failure: error code from winerror.h
1282 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1285 char nameBuf[MAX_ADAPTER_NAME];
1288 TRACE("pIfRow %p\n", pIfRow);
1290 return ERROR_INVALID_PARAMETER;
1292 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1294 ret = getInterfaceEntryByName(name, pIfRow);
1295 if (ret == NO_ERROR)
1296 ret = getInterfaceStatsByName(name, pIfRow);
1299 ret = ERROR_INVALID_DATA;
1300 TRACE("returning %d\n", ret);
1305 static int IfTableSorter(const void *a, const void *b)
1310 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1317 /******************************************************************
1318 * GetIfTable (IPHLPAPI.@)
1320 * Get a table of local interfaces.
1323 * pIfTable [Out] buffer for local interfaces table
1324 * pdwSize [In/Out] length of output buffer
1325 * bOrder [In] whether to sort the table
1329 * Failure: error code from winerror.h
1332 * If pdwSize is less than required, the function will return
1333 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1335 * If bOrder is true, the returned table will be sorted by interface index.
1337 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1341 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1344 ret = ERROR_INVALID_PARAMETER;
1346 DWORD numInterfaces = getNumInterfaces();
1347 ULONG size = sizeof(MIB_IFTABLE);
1349 if (numInterfaces > 1)
1350 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1351 if (!pIfTable || *pdwSize < size) {
1353 ret = ERROR_INSUFFICIENT_BUFFER;
1356 InterfaceIndexTable *table = getInterfaceIndexTable();
1359 size = sizeof(MIB_IFTABLE);
1360 if (table->numIndexes > 1)
1361 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1362 if (*pdwSize < size) {
1364 ret = ERROR_INSUFFICIENT_BUFFER;
1370 pIfTable->dwNumEntries = 0;
1371 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1372 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1373 GetIfEntry(&pIfTable->table[ndx]);
1374 pIfTable->dwNumEntries++;
1377 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1381 HeapFree(GetProcessHeap(), 0, table);
1384 ret = ERROR_OUTOFMEMORY;
1387 TRACE("returning %d\n", ret);
1392 /******************************************************************
1393 * GetInterfaceInfo (IPHLPAPI.@)
1395 * Get a list of network interface adapters.
1398 * pIfTable [Out] buffer for interface adapters
1399 * dwOutBufLen [Out] if buffer is too small, returns required size
1403 * Failure: error code from winerror.h
1406 * MSDN states this should return non-loopback interfaces only.
1408 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1412 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1414 ret = ERROR_INVALID_PARAMETER;
1416 DWORD numInterfaces = getNumInterfaces();
1417 ULONG size = sizeof(IP_INTERFACE_INFO);
1419 if (numInterfaces > 1)
1420 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1421 if (!pIfTable || *dwOutBufLen < size) {
1422 *dwOutBufLen = size;
1423 ret = ERROR_INSUFFICIENT_BUFFER;
1426 InterfaceIndexTable *table = getInterfaceIndexTable();
1429 size = sizeof(IP_INTERFACE_INFO);
1430 if (table->numIndexes > 1)
1431 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1432 if (*dwOutBufLen < size) {
1433 *dwOutBufLen = size;
1434 ret = ERROR_INSUFFICIENT_BUFFER;
1438 char nameBuf[MAX_ADAPTER_NAME];
1440 *dwOutBufLen = size;
1441 pIfTable->NumAdapters = 0;
1442 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1443 const char *walker, *name;
1446 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1447 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1448 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1449 walker && *walker &&
1450 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1451 walker++, assigner++)
1452 *assigner = *walker;
1454 pIfTable->NumAdapters++;
1458 HeapFree(GetProcessHeap(), 0, table);
1461 ret = ERROR_OUTOFMEMORY;
1464 TRACE("returning %d\n", ret);
1469 /******************************************************************
1470 * GetIpAddrTable (IPHLPAPI.@)
1472 * Get interface-to-IP address mapping table.
1475 * pIpAddrTable [Out] buffer for mapping table
1476 * pdwSize [In/Out] length of output buffer
1477 * bOrder [In] whether to sort the table
1481 * Failure: error code from winerror.h
1484 * If pdwSize is less than required, the function will return
1485 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1487 * If bOrder is true, the returned table will be sorted by the next hop and
1488 * an assortment of arbitrary parameters.
1490 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1494 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1497 ret = ERROR_INVALID_PARAMETER;
1499 PMIB_IPADDRTABLE table;
1501 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1502 if (ret == NO_ERROR)
1504 ULONG size = sizeof(MIB_IPADDRTABLE);
1506 if (table->dwNumEntries > 1)
1507 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1508 if (!pIpAddrTable || *pdwSize < size) {
1510 ret = ERROR_INSUFFICIENT_BUFFER;
1514 memcpy(pIpAddrTable, table, size);
1516 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1517 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1520 HeapFree(GetProcessHeap(), 0, table);
1523 TRACE("returning %d\n", ret);
1528 /******************************************************************
1529 * GetIpForwardTable (IPHLPAPI.@)
1531 * Get the route table.
1534 * pIpForwardTable [Out] buffer for route table
1535 * pdwSize [In/Out] length of output buffer
1536 * bOrder [In] whether to sort the table
1540 * Failure: error code from winerror.h
1543 * If pdwSize is less than required, the function will return
1544 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1546 * If bOrder is true, the returned table will be sorted by the next hop and
1547 * an assortment of arbitrary parameters.
1549 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1552 PMIB_IPFORWARDTABLE table;
1554 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1556 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1558 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1560 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1561 if (!pIpForwardTable || *pdwSize < size) {
1563 ret = ERROR_INSUFFICIENT_BUFFER;
1567 memcpy(pIpForwardTable, table, size);
1569 HeapFree(GetProcessHeap(), 0, table);
1571 TRACE("returning %d\n", ret);
1576 /******************************************************************
1577 * GetIpNetTable (IPHLPAPI.@)
1579 * Get the IP-to-physical address mapping table.
1582 * pIpNetTable [Out] buffer for mapping table
1583 * pdwSize [In/Out] length of output buffer
1584 * bOrder [In] whether to sort the table
1588 * Failure: error code from winerror.h
1591 * If pdwSize is less than required, the function will return
1592 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1594 * If bOrder is true, the returned table will be sorted by IP address.
1596 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1599 PMIB_IPNETTABLE table;
1601 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1603 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1605 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1607 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1608 if (!pIpNetTable || *pdwSize < size) {
1610 ret = ERROR_INSUFFICIENT_BUFFER;
1614 memcpy(pIpNetTable, table, size);
1616 HeapFree(GetProcessHeap(), 0, table);
1618 TRACE("returning %d\n", ret);
1622 /* Gets the DNS server list into the list beginning at list. Assumes that
1623 * a single server address may be placed at list if *len is at least
1624 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1625 * and assumes that all remaining DNS servers are contiguously located
1626 * beginning at firstDynamic. On input, *len is assumed to be the total number
1627 * of bytes available for all DNS servers, and is ignored if list is NULL.
1628 * On return, *len is set to the total number of bytes required for all DNS
1630 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1631 * ERROR_SUCCESS otherwise.
1633 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1634 PIP_ADDR_STRING firstDynamic, DWORD *len)
1638 initialise_resolver();
1639 size = _res.nscount * sizeof(IP_ADDR_STRING);
1640 if (!list || *len < size) {
1642 return ERROR_BUFFER_OVERFLOW;
1645 if (_res.nscount > 0) {
1646 PIP_ADDR_STRING ptr;
1649 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1650 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1651 ptr->IpAddress.String);
1652 if (i == _res.nscount - 1)
1655 ptr->Next = firstDynamic;
1657 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1660 return ERROR_SUCCESS;
1663 /******************************************************************
1664 * GetNetworkParams (IPHLPAPI.@)
1666 * Get the network parameters for the local computer.
1669 * pFixedInfo [Out] buffer for network parameters
1670 * pOutBufLen [In/Out] length of output buffer
1674 * Failure: error code from winerror.h
1677 * If pOutBufLen is less than required, the function will return
1678 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1681 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1683 DWORD ret, size, serverListSize;
1687 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1689 return ERROR_INVALID_PARAMETER;
1691 get_dns_server_list(NULL, NULL, &serverListSize);
1692 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1693 if (!pFixedInfo || *pOutBufLen < size) {
1695 return ERROR_BUFFER_OVERFLOW;
1698 memset(pFixedInfo, 0, size);
1699 size = sizeof(pFixedInfo->HostName);
1700 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1701 size = sizeof(pFixedInfo->DomainName);
1702 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1703 get_dns_server_list(&pFixedInfo->DnsServerList,
1704 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1706 /* Assume the first DNS server in the list is the "current" DNS server: */
1707 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1708 pFixedInfo->NodeType = HYBRID_NODETYPE;
1709 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1710 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1711 if (regReturn != ERROR_SUCCESS)
1712 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1713 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1715 if (regReturn == ERROR_SUCCESS)
1717 DWORD size = sizeof(pFixedInfo->ScopeId);
1719 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1723 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1724 I suppose could also check for a listener on port 53 to set EnableDns */
1726 TRACE("returning %d\n", ret);
1731 /******************************************************************
1732 * GetNumberOfInterfaces (IPHLPAPI.@)
1734 * Get the number of interfaces.
1737 * pdwNumIf [Out] number of interfaces
1740 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1742 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1746 TRACE("pdwNumIf %p\n", pdwNumIf);
1748 ret = ERROR_INVALID_PARAMETER;
1750 *pdwNumIf = getNumInterfaces();
1753 TRACE("returning %d\n", ret);
1758 /******************************************************************
1759 * GetPerAdapterInfo (IPHLPAPI.@)
1761 * Get information about an adapter corresponding to an interface.
1764 * IfIndex [In] interface info
1765 * pPerAdapterInfo [Out] buffer for per adapter info
1766 * pOutBufLen [In/Out] length of output buffer
1770 * Failure: error code from winerror.h
1772 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1774 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1775 DWORD ret = NO_ERROR;
1777 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1779 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1781 if (!isIfIndexLoopback(IfIndex)) {
1782 get_dns_server_list(NULL, NULL, &serverListSize);
1783 if (serverListSize > sizeof(IP_ADDR_STRING))
1784 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1786 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1788 *pOutBufLen = bytesNeeded;
1789 return ERROR_BUFFER_OVERFLOW;
1792 memset(pPerAdapterInfo, 0, bytesNeeded);
1793 if (!isIfIndexLoopback(IfIndex)) {
1794 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1795 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1797 /* Assume the first DNS server in the list is the "current" DNS server: */
1798 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1804 /******************************************************************
1805 * GetRTTAndHopCount (IPHLPAPI.@)
1807 * Get round-trip time (RTT) and hop count.
1811 * DestIpAddress [In] destination address to get the info for
1812 * HopCount [Out] retrieved hop count
1813 * MaxHops [In] maximum hops to search for the destination
1814 * RTT [Out] RTT in milliseconds
1821 * Stub, returns FALSE.
1823 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1825 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1826 DestIpAddress, HopCount, MaxHops, RTT);
1831 /******************************************************************
1832 * GetTcpTable (IPHLPAPI.@)
1834 * Get the table of active TCP connections.
1837 * pTcpTable [Out] buffer for TCP connections table
1838 * pdwSize [In/Out] length of output buffer
1839 * bOrder [In] whether to order the table
1843 * Failure: error code from winerror.h
1846 * If pdwSize is less than required, the function will return
1847 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1848 * the required byte size.
1849 * If bOrder is true, the returned table will be sorted, first by
1850 * local address and port number, then by remote address and port
1853 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1856 PMIB_TCPTABLE table;
1858 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1860 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1862 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1864 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1865 if (!pTcpTable || *pdwSize < size) {
1867 ret = ERROR_INSUFFICIENT_BUFFER;
1871 memcpy(pTcpTable, table, size);
1873 HeapFree(GetProcessHeap(), 0, table);
1875 TRACE("returning %d\n", ret);
1880 /******************************************************************
1881 * GetUdpTable (IPHLPAPI.@)
1883 * Get a table of active UDP connections.
1886 * pUdpTable [Out] buffer for UDP connections table
1887 * pdwSize [In/Out] length of output buffer
1888 * bOrder [In] whether to order the table
1892 * Failure: error code from winerror.h
1895 * If pdwSize is less than required, the function will return
1896 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1897 * required byte size.
1898 * If bOrder is true, the returned table will be sorted, first by
1899 * local address, then by local port number.
1901 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1904 PMIB_UDPTABLE table;
1906 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1908 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1910 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1912 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1913 if (!pUdpTable || *pdwSize < size) {
1915 ret = ERROR_INSUFFICIENT_BUFFER;
1919 memcpy(pUdpTable, table, size);
1921 HeapFree(GetProcessHeap(), 0, table);
1923 TRACE("returning %d\n", ret);
1928 /******************************************************************
1929 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1931 * This is a Win98-only function to get information on "unidirectional"
1932 * adapters. Since this is pretty nonsensical in other contexts, it
1933 * never returns anything.
1936 * pIPIfInfo [Out] buffer for adapter infos
1937 * dwOutBufLen [Out] length of the output buffer
1941 * Failure: error code from winerror.h
1944 * Stub, returns ERROR_NOT_SUPPORTED.
1946 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1948 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1949 /* a unidirectional adapter?? not bloody likely! */
1950 return ERROR_NOT_SUPPORTED;
1954 /******************************************************************
1955 * IpReleaseAddress (IPHLPAPI.@)
1957 * Release an IP obtained through DHCP,
1960 * AdapterInfo [In] adapter to release IP address
1964 * Failure: error code from winerror.h
1967 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1968 * this function does nothing.
1971 * Stub, returns ERROR_NOT_SUPPORTED.
1973 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1975 TRACE("AdapterInfo %p\n", AdapterInfo);
1976 /* not a stub, never going to support this (and I never mark an adapter as
1977 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1978 return ERROR_NOT_SUPPORTED;
1982 /******************************************************************
1983 * IpRenewAddress (IPHLPAPI.@)
1985 * Renew an IP obtained through DHCP.
1988 * AdapterInfo [In] adapter to renew IP address
1992 * Failure: error code from winerror.h
1995 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1996 * this function does nothing.
1999 * Stub, returns ERROR_NOT_SUPPORTED.
2001 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2003 TRACE("AdapterInfo %p\n", AdapterInfo);
2004 /* not a stub, never going to support this (and I never mark an adapter as
2005 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
2006 return ERROR_NOT_SUPPORTED;
2010 /******************************************************************
2011 * NotifyAddrChange (IPHLPAPI.@)
2013 * Notify caller whenever the ip-interface map is changed.
2016 * Handle [Out] handle usable in asynchronous notification
2017 * overlapped [In] overlapped structure that notifies the caller
2021 * Failure: error code from winerror.h
2024 * Stub, returns ERROR_NOT_SUPPORTED.
2026 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2028 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2029 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2030 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2031 return ERROR_IO_PENDING;
2035 /******************************************************************
2036 * NotifyRouteChange (IPHLPAPI.@)
2038 * Notify caller whenever the ip routing table is changed.
2041 * Handle [Out] handle usable in asynchronous notification
2042 * overlapped [In] overlapped structure that notifies the caller
2046 * Failure: error code from winerror.h
2049 * Stub, returns ERROR_NOT_SUPPORTED.
2051 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2053 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2054 return ERROR_NOT_SUPPORTED;
2058 /******************************************************************
2059 * SendARP (IPHLPAPI.@)
2061 * Send an ARP request.
2064 * DestIP [In] attempt to obtain this IP
2065 * SrcIP [In] optional sender IP address
2066 * pMacAddr [Out] buffer for the mac address
2067 * PhyAddrLen [In/Out] length of the output buffer
2071 * Failure: error code from winerror.h
2074 * Stub, returns ERROR_NOT_SUPPORTED.
2076 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2078 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2079 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2080 return ERROR_NOT_SUPPORTED;
2084 /******************************************************************
2085 * SetIfEntry (IPHLPAPI.@)
2087 * Set the administrative status of an interface.
2090 * pIfRow [In] dwAdminStatus member specifies the new status.
2094 * Failure: error code from winerror.h
2097 * Stub, returns ERROR_NOT_SUPPORTED.
2099 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2101 FIXME("(pIfRow %p): stub\n", pIfRow);
2102 /* this is supposed to set an interface administratively up or down.
2103 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2104 this sort of down is indistinguishable from other sorts of down (e.g. no
2106 return ERROR_NOT_SUPPORTED;
2110 /******************************************************************
2111 * SetIpForwardEntry (IPHLPAPI.@)
2113 * Modify an existing route.
2116 * pRoute [In] route with the new information
2120 * Failure: error code from winerror.h
2123 * Stub, returns NO_ERROR.
2125 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2127 FIXME("(pRoute %p): stub\n", pRoute);
2128 /* this is to add a route entry, how's it distinguishable from
2129 CreateIpForwardEntry?
2130 could use SIOCADDRT, not sure I want to */
2135 /******************************************************************
2136 * SetIpNetEntry (IPHLPAPI.@)
2138 * Modify an existing ARP entry.
2141 * pArpEntry [In] ARP entry with the new information
2145 * Failure: error code from winerror.h
2148 * Stub, returns NO_ERROR.
2150 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2152 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2153 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2158 /******************************************************************
2159 * SetIpStatistics (IPHLPAPI.@)
2161 * Toggle IP forwarding and det the default TTL value.
2164 * pIpStats [In] IP statistics with the new information
2168 * Failure: error code from winerror.h
2171 * Stub, returns NO_ERROR.
2173 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2175 FIXME("(pIpStats %p): stub\n", pIpStats);
2180 /******************************************************************
2181 * SetIpTTL (IPHLPAPI.@)
2183 * Set the default TTL value.
2186 * nTTL [In] new TTL value
2190 * Failure: error code from winerror.h
2193 * Stub, returns NO_ERROR.
2195 DWORD WINAPI SetIpTTL(UINT nTTL)
2197 FIXME("(nTTL %d): stub\n", nTTL);
2198 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2199 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2204 /******************************************************************
2205 * SetTcpEntry (IPHLPAPI.@)
2207 * Set the state of a TCP connection.
2210 * pTcpRow [In] specifies connection with new state
2214 * Failure: error code from winerror.h
2217 * Stub, returns NO_ERROR.
2219 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2221 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2226 /******************************************************************
2227 * UnenableRouter (IPHLPAPI.@)
2229 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2230 * if it reaches zero.
2233 * pOverlapped [In/Out] should be the same as in EnableRouter()
2234 * lpdwEnableCount [Out] optional, receives reference count
2238 * Failure: error code from winerror.h
2241 * Stub, returns ERROR_NOT_SUPPORTED.
2243 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2245 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2247 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2248 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2250 return ERROR_NOT_SUPPORTED;
2253 /******************************************************************
2254 * PfCreateInterface (IPHLPAPI.@)
2256 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2257 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2259 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2260 return ERROR_CALL_NOT_IMPLEMENTED;