2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
63 #define IF_NAMESIZE 16
67 #define INADDR_NONE ~0UL
70 /* call res_init() just once because of a bug in Mac OS X 10.4 */
71 /* Call once per thread on systems that have per-thread _res. */
72 static void initialise_resolver(void)
74 if ((_res.options & RES_INIT) == 0)
78 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
81 case DLL_PROCESS_ATTACH:
82 DisableThreadLibraryCalls( hinstDLL );
85 case DLL_PROCESS_DETACH:
91 /******************************************************************
92 * AddIPAddress (IPHLPAPI.@)
94 * Add an IP address to an adapter.
97 * Address [In] IP address to add to the adapter
98 * IpMask [In] subnet mask for the IP address
99 * IfIndex [In] adapter index to add the address
100 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
101 * NTEInstance [Out] NTE instance for the IP address
105 * Failure: error code from winerror.h
108 * Stub. Currently returns ERROR_NOT_SUPPORTED.
110 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
113 return ERROR_NOT_SUPPORTED;
117 /******************************************************************
118 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
120 * Get table of local interfaces.
121 * Like GetIfTable(), but allocate the returned table from heap.
124 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
125 * allocated and returned.
126 * bOrder [In] whether to sort the table
127 * heap [In] heap from which the table is allocated
128 * flags [In] flags to HeapAlloc
131 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
132 * GetIfTable() returns otherwise.
134 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
135 BOOL bOrder, HANDLE heap, DWORD flags)
139 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
140 bOrder, heap, flags);
142 ret = ERROR_INVALID_PARAMETER;
146 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
147 if (ret == ERROR_INSUFFICIENT_BUFFER) {
148 *ppIfTable = HeapAlloc(heap, flags, dwSize);
149 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
152 TRACE("returning %d\n", ret);
157 static int IpAddrTableSorter(const void *a, const void *b)
162 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
169 /******************************************************************
170 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
172 * Get interface-to-IP address mapping table.
173 * Like GetIpAddrTable(), but allocate the returned table from heap.
176 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
177 * allocated and returned.
178 * bOrder [In] whether to sort the table
179 * heap [In] heap from which the table is allocated
180 * flags [In] flags to HeapAlloc
183 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
184 * failure, NO_ERROR on success.
186 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
187 BOOL bOrder, HANDLE heap, DWORD flags)
191 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
192 ppIpAddrTable, bOrder, heap, flags);
193 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
195 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
196 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
197 TRACE("returning %d\n", ret);
202 /******************************************************************
203 * CreateIpForwardEntry (IPHLPAPI.@)
205 * Create a route in the local computer's IP table.
208 * pRoute [In] new route information
212 * Failure: error code from winerror.h
215 * Stub, always returns NO_ERROR.
217 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
219 FIXME("(pRoute %p): stub\n", pRoute);
220 /* could use SIOCADDRT, not sure I want to */
225 /******************************************************************
226 * CreateIpNetEntry (IPHLPAPI.@)
228 * Create entry in the ARP table.
231 * pArpEntry [In] new ARP entry
235 * Failure: error code from winerror.h
238 * Stub, always returns NO_ERROR.
240 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
242 FIXME("(pArpEntry %p)\n", pArpEntry);
243 /* could use SIOCSARP on systems that support it, not sure I want to */
248 /******************************************************************
249 * CreateProxyArpEntry (IPHLPAPI.@)
251 * Create a Proxy ARP (PARP) entry for an IP address.
254 * dwAddress [In] IP address for which this computer acts as a proxy.
255 * dwMask [In] subnet mask for dwAddress
256 * dwIfIndex [In] interface index
260 * Failure: error code from winerror.h
263 * Stub, returns ERROR_NOT_SUPPORTED.
265 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
267 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
268 dwAddress, dwMask, dwIfIndex);
269 return ERROR_NOT_SUPPORTED;
273 /******************************************************************
274 * DeleteIPAddress (IPHLPAPI.@)
276 * Delete an IP address added with AddIPAddress().
279 * NTEContext [In] NTE context from AddIPAddress();
283 * Failure: error code from winerror.h
286 * Stub, returns ERROR_NOT_SUPPORTED.
288 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
290 FIXME("(NTEContext %d): stub\n", NTEContext);
291 return ERROR_NOT_SUPPORTED;
295 /******************************************************************
296 * DeleteIpForwardEntry (IPHLPAPI.@)
301 * pRoute [In] route to delete
305 * Failure: error code from winerror.h
308 * Stub, returns NO_ERROR.
310 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
312 FIXME("(pRoute %p): stub\n", pRoute);
313 /* could use SIOCDELRT, not sure I want to */
318 /******************************************************************
319 * DeleteIpNetEntry (IPHLPAPI.@)
321 * Delete an ARP entry.
324 * pArpEntry [In] ARP entry to delete
328 * Failure: error code from winerror.h
331 * Stub, returns NO_ERROR.
333 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
335 FIXME("(pArpEntry %p): stub\n", pArpEntry);
336 /* could use SIOCDARP on systems that support it, not sure I want to */
341 /******************************************************************
342 * DeleteProxyArpEntry (IPHLPAPI.@)
344 * Delete a Proxy ARP entry.
347 * dwAddress [In] IP address for which this computer acts as a proxy.
348 * dwMask [In] subnet mask for dwAddress
349 * dwIfIndex [In] interface index
353 * Failure: error code from winerror.h
356 * Stub, returns ERROR_NOT_SUPPORTED.
358 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
360 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
361 dwAddress, dwMask, dwIfIndex);
362 return ERROR_NOT_SUPPORTED;
366 /******************************************************************
367 * EnableRouter (IPHLPAPI.@)
369 * Turn on ip forwarding.
373 * pOverlapped [In/Out] hEvent member should contain a valid handle.
376 * Success: ERROR_IO_PENDING
377 * Failure: error code from winerror.h
380 * Stub, returns ERROR_NOT_SUPPORTED.
382 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
384 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
385 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
386 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
388 return ERROR_NOT_SUPPORTED;
392 /******************************************************************
393 * FlushIpNetTable (IPHLPAPI.@)
395 * Delete all ARP entries of an interface
398 * dwIfIndex [In] interface index
402 * Failure: error code from winerror.h
405 * Stub, returns ERROR_NOT_SUPPORTED.
407 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
409 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
410 /* this flushes the arp cache of the given index */
411 return ERROR_NOT_SUPPORTED;
415 /******************************************************************
416 * GetAdapterIndex (IPHLPAPI.@)
418 * Get interface index from its name.
421 * AdapterName [In] unicode string with the adapter name
422 * IfIndex [Out] returns found interface index
426 * Failure: error code from winerror.h
428 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
430 char adapterName[MAX_ADAPTER_NAME];
434 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
435 /* The adapter name is guaranteed not to have any unicode characters, so
436 * this translation is never lossy */
437 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
438 adapterName[i] = (char)AdapterName[i];
439 adapterName[i] = '\0';
440 ret = getInterfaceIndexByName(adapterName, IfIndex);
441 TRACE("returning %d\n", ret);
446 /******************************************************************
447 * GetAdaptersInfo (IPHLPAPI.@)
449 * Get information about adapters.
452 * pAdapterInfo [Out] buffer for adapter infos
453 * pOutBufLen [In] length of output buffer
457 * Failure: error code from winerror.h
459 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
463 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
465 ret = ERROR_INVALID_PARAMETER;
467 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
469 if (numNonLoopbackInterfaces > 0) {
470 DWORD numIPAddresses = getNumIPAddresses();
473 /* This may slightly overestimate the amount of space needed, because
474 * the IP addresses include the loopback address, but it's easier
475 * to make sure there's more than enough space than to make sure there's
476 * precisely enough space.
478 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
479 size += numIPAddresses * sizeof(IP_ADDR_STRING);
480 if (!pAdapterInfo || *pOutBufLen < size) {
482 ret = ERROR_BUFFER_OVERFLOW;
485 InterfaceIndexTable *table = NULL;
486 PMIB_IPADDRTABLE ipAddrTable = NULL;
487 PMIB_IPFORWARDTABLE routeTable = NULL;
489 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
491 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
493 table = getNonLoopbackInterfaceIndexTable();
495 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
496 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
497 if (*pOutBufLen < size) {
499 ret = ERROR_INSUFFICIENT_BUFFER;
504 BOOL winsEnabled = FALSE;
505 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
506 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
507 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
509 memset(pAdapterInfo, 0, size);
510 /* @@ Wine registry key: HKCU\Software\Wine\Network */
511 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
512 &hKey) == ERROR_SUCCESS) {
513 DWORD size = sizeof(primaryWINS.String);
516 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
517 (LPBYTE)primaryWINS.String, &size);
518 addr = inet_addr(primaryWINS.String);
519 if (addr != INADDR_NONE && addr != INADDR_ANY)
521 size = sizeof(secondaryWINS.String);
522 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
523 (LPBYTE)secondaryWINS.String, &size);
524 addr = inet_addr(secondaryWINS.String);
525 if (addr != INADDR_NONE && addr != INADDR_ANY)
529 for (ndx = 0; ndx < table->numIndexes; ndx++) {
530 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
532 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
533 BOOL firstIPAddr = TRUE;
535 /* on Win98 this is left empty, but whatever */
536 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
537 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
538 ptr->AddressLength = sizeof(ptr->Address);
539 getInterfacePhysicalByIndex(table->indexes[ndx],
540 &ptr->AddressLength, ptr->Address, &ptr->Type);
541 ptr->Index = table->indexes[ndx];
542 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
543 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
545 toIPAddressString(ipAddrTable->table[i].dwAddr,
546 ptr->IpAddressList.IpAddress.String);
547 toIPAddressString(ipAddrTable->table[i].dwMask,
548 ptr->IpAddressList.IpMask.String);
552 currentIPAddr->Next = nextIPAddr;
553 currentIPAddr = nextIPAddr;
554 toIPAddressString(ipAddrTable->table[i].dwAddr,
555 currentIPAddr->IpAddress.String);
556 toIPAddressString(ipAddrTable->table[i].dwMask,
557 currentIPAddr->IpMask.String);
562 /* Find first router through this interface, which we'll assume
563 * is the default gateway for this adapter */
564 for (i = 0; i < routeTable->dwNumEntries; i++)
565 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
566 && routeTable->table[i].dwForwardType ==
567 MIB_IPROUTE_TYPE_INDIRECT)
568 toIPAddressString(routeTable->table[i].dwForwardNextHop,
569 ptr->GatewayList.IpAddress.String);
571 ptr->HaveWins = TRUE;
572 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
573 primaryWINS.String, sizeof(primaryWINS.String));
574 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
575 secondaryWINS.String, sizeof(secondaryWINS.String));
577 if (ndx < table->numIndexes - 1)
578 ptr->Next = &pAdapterInfo[ndx + 1];
584 HeapFree(GetProcessHeap(), 0, table);
587 ret = ERROR_OUTOFMEMORY;
588 HeapFree(GetProcessHeap(), 0, routeTable);
589 HeapFree(GetProcessHeap(), 0, ipAddrTable);
595 TRACE("returning %d\n", ret);
599 static DWORD typeFromMibType(DWORD mib_type)
603 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
604 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
605 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
606 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
607 default: return IF_TYPE_OTHER;
611 static DWORD connectionTypeFromMibType(DWORD mib_type)
615 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
616 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
617 default: return NET_IF_CONNECTION_DEDICATED;
621 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
627 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
628 for (i = 0; i < at->dwNumEntries; i++)
630 if (at->table[i].dwIndex == index) (*num_addrs)++;
632 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
634 HeapFree(GetProcessHeap(), 0, at);
635 return ERROR_OUTOFMEMORY;
637 for (i = 0, j = 0; i < at->dwNumEntries; i++)
639 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
641 HeapFree(GetProcessHeap(), 0, at);
642 return ERROR_SUCCESS;
645 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
650 for (addrp = (const BYTE *)in_addr;
651 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
654 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
655 sprintf(p, "%d", *addrp);
660 sprintf(p, "%d.%n", *addrp, &n);
667 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
669 const IN6_ADDR *addr = &sin->sin6_addr;
672 BOOL in_zero = FALSE;
674 for (i = 0; i < 7; i++)
676 if (!addr->u.Word[i])
690 sprintf(p, "%x:%n", ntohs(addr->u.Word[i]), &n);
695 sprintf(p, "%x", ntohs(addr->u.Word[7]));
699 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
701 DWORD i, num_gateways = 0;
703 for (i = 0; i < routeTable->dwNumEntries; i++)
705 if (routeTable->table[i].dwForwardIfIndex == index &&
706 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
712 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
713 PMIB_IPFORWARDTABLE routeTable)
716 PMIB_IPFORWARDROW row = NULL;
718 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
720 if (routeTable->table[i].dwForwardIfIndex == index &&
721 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
722 row = &routeTable->table[i];
727 static ULONG adapterAddressesFromIndex(ULONG family, DWORD index, IP_ADAPTER_ADDRESSES *aa, ULONG *size)
729 ULONG ret, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
730 DWORD *v4addrs = NULL;
731 SOCKET_ADDRESS *v6addrs = NULL;
732 PMIB_IPFORWARDTABLE routeTable = NULL;
734 if (family == AF_INET)
736 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
737 GetProcessHeap(), 0);
740 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
741 num_v4_gateways = count_v4_gateways(index, routeTable);
744 else if (family == AF_INET6)
745 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
746 else if (family == AF_UNSPEC)
748 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
749 GetProcessHeap(), 0);
752 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
753 num_v4_gateways = count_v4_gateways(index, routeTable);
755 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
760 FIXME("address family %u unsupported\n", family);
765 HeapFree(GetProcessHeap(), 0, routeTable);
769 total_size = sizeof(IP_ADAPTER_ADDRESSES);
770 total_size += IF_NAMESIZE;
771 total_size += IF_NAMESIZE * sizeof(WCHAR);
772 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
773 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
774 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
775 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
776 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
777 for (i = 0; i < num_v6addrs; i++)
778 total_size += v6addrs[i].iSockaddrLength;
780 if (aa && *size >= total_size)
782 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
784 DWORD buflen, type, status;
786 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
787 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
788 aa->u.s.IfIndex = index;
790 getInterfaceNameByIndex(index, name);
791 memcpy(ptr, name, IF_NAMESIZE);
792 aa->AdapterName = ptr;
794 aa->FriendlyName = (WCHAR *)ptr;
795 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
800 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
804 PMIB_IPFORWARDROW adapterRow;
806 if ((adapterRow = findIPv4Gateway(index, routeTable)))
808 PIP_ADAPTER_GATEWAY_ADDRESS gw;
811 for (gw = aa->FirstGatewayAddress; gw && gw->Next;
816 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
817 aa->FirstGatewayAddress = gw;
821 gw->Next = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
824 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
825 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
826 sin = (PSOCKADDR_IN)ptr;
827 sin->sin_family = AF_INET;
829 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
831 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
832 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
833 ptr += sizeof(SOCKADDR_IN);
838 IP_ADAPTER_UNICAST_ADDRESS *ua;
839 struct sockaddr_in *sa;
840 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
841 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
842 for (i = 0; i < num_v4addrs; i++)
846 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
847 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
848 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
849 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
851 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
852 sa->sin_family = AF_INET;
853 sa->sin_addr.s_addr = v4addrs[i];
855 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
856 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
858 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
859 if (i < num_v4addrs - 1)
861 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
868 IP_ADAPTER_UNICAST_ADDRESS *ua;
869 struct WS_sockaddr_in6 *sa;
871 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
872 if (aa->FirstUnicastAddress)
874 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
876 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
877 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
880 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
881 for (i = 0; i < num_v6addrs; i++)
885 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
886 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
887 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
888 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
890 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
891 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
892 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
893 debugstr_ipv6(sa, addr_buf));
895 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
896 if (i < num_v6addrs - 1)
898 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
904 buflen = MAX_INTERFACE_PHYSADDR;
905 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
906 aa->PhysicalAddressLength = buflen;
907 aa->IfType = typeFromMibType(type);
908 aa->ConnectionType = connectionTypeFromMibType(type);
910 getInterfaceMtuByName(name, &aa->Mtu);
912 getInterfaceStatusByName(name, &status);
913 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
914 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
915 else aa->OperStatus = IfOperStatusUnknown;
918 HeapFree(GetProcessHeap(), 0, routeTable);
919 HeapFree(GetProcessHeap(), 0, v6addrs);
920 HeapFree(GetProcessHeap(), 0, v4addrs);
921 return ERROR_SUCCESS;
924 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
925 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
927 InterfaceIndexTable *table;
928 ULONG i, size, total_size, ret = ERROR_NO_DATA;
930 if (!buflen) return ERROR_INVALID_PARAMETER;
932 table = getInterfaceIndexTable();
933 if (!table || !table->numIndexes)
935 HeapFree(GetProcessHeap(), 0, table);
936 return ERROR_NO_DATA;
939 for (i = 0; i < table->numIndexes; i++)
942 if ((ret = adapterAddressesFromIndex(family, table->indexes[i], NULL, &size)))
944 HeapFree(GetProcessHeap(), 0, table);
949 if (aa && *buflen >= total_size)
951 ULONG bytes_left = size = total_size;
952 for (i = 0; i < table->numIndexes; i++)
954 if ((ret = adapterAddressesFromIndex(family, table->indexes[i], aa, &size)))
956 HeapFree(GetProcessHeap(), 0, table);
959 if (i < table->numIndexes - 1)
961 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
963 size = bytes_left -= size;
968 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
969 *buflen = total_size;
971 TRACE("num adapters %u\n", table->numIndexes);
972 HeapFree(GetProcessHeap(), 0, table);
976 /******************************************************************
977 * GetBestInterface (IPHLPAPI.@)
979 * Get the interface, with the best route for the given IP address.
982 * dwDestAddr [In] IP address to search the interface for
983 * pdwBestIfIndex [Out] found best interface
987 * Failure: error code from winerror.h
989 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
991 struct WS_sockaddr_in sa_in;
992 memset(&sa_in, 0, sizeof(sa_in));
993 sa_in.sin_family = AF_INET;
994 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
995 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
998 /******************************************************************
999 * GetBestInterfaceEx (IPHLPAPI.@)
1001 * Get the interface, with the best route for the given IP address.
1004 * dwDestAddr [In] IP address to search the interface for
1005 * pdwBestIfIndex [Out] found best interface
1009 * Failure: error code from winerror.h
1011 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1015 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1016 if (!pDestAddr || !pdwBestIfIndex)
1017 ret = ERROR_INVALID_PARAMETER;
1019 MIB_IPFORWARDROW ipRow;
1021 if (pDestAddr->sa_family == AF_INET) {
1022 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1023 if (ret == ERROR_SUCCESS)
1024 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1026 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1027 ret = ERROR_NOT_SUPPORTED;
1030 TRACE("returning %d\n", ret);
1035 /******************************************************************
1036 * GetBestRoute (IPHLPAPI.@)
1038 * Get the best route for the given IP address.
1041 * dwDestAddr [In] IP address to search the best route for
1042 * dwSourceAddr [In] optional source IP address
1043 * pBestRoute [Out] found best route
1047 * Failure: error code from winerror.h
1049 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1051 PMIB_IPFORWARDTABLE table;
1054 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1055 dwSourceAddr, pBestRoute);
1057 return ERROR_INVALID_PARAMETER;
1059 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1061 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1063 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1064 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1065 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1066 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1067 DWORD numShifts, mask;
1069 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1070 mask && !(mask & 1); mask >>= 1, numShifts++)
1072 if (numShifts > matchedBits) {
1073 matchedBits = numShifts;
1076 else if (!matchedBits) {
1081 if (matchedNdx < table->dwNumEntries) {
1082 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1083 ret = ERROR_SUCCESS;
1086 /* No route matches, which can happen if there's no default route. */
1087 ret = ERROR_HOST_UNREACHABLE;
1089 HeapFree(GetProcessHeap(), 0, table);
1091 TRACE("returning %d\n", ret);
1096 /******************************************************************
1097 * GetFriendlyIfIndex (IPHLPAPI.@)
1099 * Get a "friendly" version of IfIndex, which is one that doesn't
1100 * have the top byte set. Doesn't validate whether IfIndex is a valid
1104 * IfIndex [In] interface index to get the friendly one for
1107 * A friendly version of IfIndex.
1109 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1111 /* windows doesn't validate these, either, just makes sure the top byte is
1112 cleared. I assume my ifenum module never gives an index with the top
1114 TRACE("returning %d\n", IfIndex);
1119 /******************************************************************
1120 * GetIfEntry (IPHLPAPI.@)
1122 * Get information about an interface.
1125 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1126 * Out: interface information
1130 * Failure: error code from winerror.h
1132 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1135 char nameBuf[MAX_ADAPTER_NAME];
1138 TRACE("pIfRow %p\n", pIfRow);
1140 return ERROR_INVALID_PARAMETER;
1142 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1144 ret = getInterfaceEntryByName(name, pIfRow);
1145 if (ret == NO_ERROR)
1146 ret = getInterfaceStatsByName(name, pIfRow);
1149 ret = ERROR_INVALID_DATA;
1150 TRACE("returning %d\n", ret);
1155 static int IfTableSorter(const void *a, const void *b)
1160 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1167 /******************************************************************
1168 * GetIfTable (IPHLPAPI.@)
1170 * Get a table of local interfaces.
1173 * pIfTable [Out] buffer for local interfaces table
1174 * pdwSize [In/Out] length of output buffer
1175 * bOrder [In] whether to sort the table
1179 * Failure: error code from winerror.h
1182 * If pdwSize is less than required, the function will return
1183 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1185 * If bOrder is true, the returned table will be sorted by interface index.
1187 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1191 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1194 ret = ERROR_INVALID_PARAMETER;
1196 DWORD numInterfaces = getNumInterfaces();
1197 ULONG size = sizeof(MIB_IFTABLE);
1199 if (numInterfaces > 1)
1200 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1201 if (!pIfTable || *pdwSize < size) {
1203 ret = ERROR_INSUFFICIENT_BUFFER;
1206 InterfaceIndexTable *table = getInterfaceIndexTable();
1209 size = sizeof(MIB_IFTABLE);
1210 if (table->numIndexes > 1)
1211 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1212 if (*pdwSize < size) {
1214 ret = ERROR_INSUFFICIENT_BUFFER;
1220 pIfTable->dwNumEntries = 0;
1221 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1222 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1223 GetIfEntry(&pIfTable->table[ndx]);
1224 pIfTable->dwNumEntries++;
1227 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1231 HeapFree(GetProcessHeap(), 0, table);
1234 ret = ERROR_OUTOFMEMORY;
1237 TRACE("returning %d\n", ret);
1242 /******************************************************************
1243 * GetInterfaceInfo (IPHLPAPI.@)
1245 * Get a list of network interface adapters.
1248 * pIfTable [Out] buffer for interface adapters
1249 * dwOutBufLen [Out] if buffer is too small, returns required size
1253 * Failure: error code from winerror.h
1256 * MSDN states this should return non-loopback interfaces only.
1258 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1262 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1264 ret = ERROR_INVALID_PARAMETER;
1266 DWORD numInterfaces = getNumInterfaces();
1267 ULONG size = sizeof(IP_INTERFACE_INFO);
1269 if (numInterfaces > 1)
1270 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1271 if (!pIfTable || *dwOutBufLen < size) {
1272 *dwOutBufLen = size;
1273 ret = ERROR_INSUFFICIENT_BUFFER;
1276 InterfaceIndexTable *table = getInterfaceIndexTable();
1279 size = sizeof(IP_INTERFACE_INFO);
1280 if (table->numIndexes > 1)
1281 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1282 if (*dwOutBufLen < size) {
1283 *dwOutBufLen = size;
1284 ret = ERROR_INSUFFICIENT_BUFFER;
1288 char nameBuf[MAX_ADAPTER_NAME];
1290 *dwOutBufLen = size;
1291 pIfTable->NumAdapters = 0;
1292 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1293 const char *walker, *name;
1296 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1297 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1298 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1299 walker && *walker &&
1300 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1301 walker++, assigner++)
1302 *assigner = *walker;
1304 pIfTable->NumAdapters++;
1308 HeapFree(GetProcessHeap(), 0, table);
1311 ret = ERROR_OUTOFMEMORY;
1314 TRACE("returning %d\n", ret);
1319 /******************************************************************
1320 * GetIpAddrTable (IPHLPAPI.@)
1322 * Get interface-to-IP address mapping table.
1325 * pIpAddrTable [Out] buffer for mapping table
1326 * pdwSize [In/Out] length of output buffer
1327 * bOrder [In] whether to sort the table
1331 * Failure: error code from winerror.h
1334 * If pdwSize is less than required, the function will return
1335 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1337 * If bOrder is true, the returned table will be sorted by the next hop and
1338 * an assortment of arbitrary parameters.
1340 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1344 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1347 ret = ERROR_INVALID_PARAMETER;
1349 PMIB_IPADDRTABLE table;
1351 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1352 if (ret == NO_ERROR)
1354 ULONG size = sizeof(MIB_IPADDRTABLE);
1356 if (table->dwNumEntries > 1)
1357 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1358 if (!pIpAddrTable || *pdwSize < size) {
1360 ret = ERROR_INSUFFICIENT_BUFFER;
1364 memcpy(pIpAddrTable, table, size);
1366 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1367 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1370 HeapFree(GetProcessHeap(), 0, table);
1373 TRACE("returning %d\n", ret);
1378 /******************************************************************
1379 * GetIpForwardTable (IPHLPAPI.@)
1381 * Get the route table.
1384 * pIpForwardTable [Out] buffer for route table
1385 * pdwSize [In/Out] length of output buffer
1386 * bOrder [In] whether to sort the table
1390 * Failure: error code from winerror.h
1393 * If pdwSize is less than required, the function will return
1394 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1396 * If bOrder is true, the returned table will be sorted by the next hop and
1397 * an assortment of arbitrary parameters.
1399 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1402 PMIB_IPFORWARDTABLE table;
1404 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1406 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1408 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1410 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1411 if (!pIpForwardTable || *pdwSize < size) {
1413 ret = ERROR_INSUFFICIENT_BUFFER;
1417 memcpy(pIpForwardTable, table, size);
1419 HeapFree(GetProcessHeap(), 0, table);
1421 TRACE("returning %d\n", ret);
1426 /******************************************************************
1427 * GetIpNetTable (IPHLPAPI.@)
1429 * Get the IP-to-physical address mapping table.
1432 * pIpNetTable [Out] buffer for mapping table
1433 * pdwSize [In/Out] length of output buffer
1434 * bOrder [In] whether to sort the table
1438 * Failure: error code from winerror.h
1441 * If pdwSize is less than required, the function will return
1442 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1444 * If bOrder is true, the returned table will be sorted by IP address.
1446 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1449 PMIB_IPNETTABLE table;
1451 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1453 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1455 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1457 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1458 if (!pIpNetTable || *pdwSize < size) {
1460 ret = ERROR_INSUFFICIENT_BUFFER;
1464 memcpy(pIpNetTable, table, size);
1466 HeapFree(GetProcessHeap(), 0, table);
1468 TRACE("returning %d\n", ret);
1473 /******************************************************************
1474 * GetNetworkParams (IPHLPAPI.@)
1476 * Get the network parameters for the local computer.
1479 * pFixedInfo [Out] buffer for network parameters
1480 * pOutBufLen [In/Out] length of output buffer
1484 * Failure: error code from winerror.h
1487 * If pOutBufLen is less than required, the function will return
1488 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1491 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1497 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1499 return ERROR_INVALID_PARAMETER;
1501 initialise_resolver();
1502 size = sizeof(FIXED_INFO) + (_res.nscount > 0 ? (_res.nscount - 1) *
1503 sizeof(IP_ADDR_STRING) : 0);
1504 if (!pFixedInfo || *pOutBufLen < size) {
1506 return ERROR_BUFFER_OVERFLOW;
1509 memset(pFixedInfo, 0, size);
1510 size = sizeof(pFixedInfo->HostName);
1511 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1512 size = sizeof(pFixedInfo->DomainName);
1513 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1514 if (_res.nscount > 0) {
1515 PIP_ADDR_STRING ptr;
1518 for (i = 0, ptr = &pFixedInfo->DnsServerList; i < _res.nscount && ptr;
1519 i++, ptr = ptr->Next) {
1520 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1521 ptr->IpAddress.String);
1522 if (i == _res.nscount - 1)
1525 ptr->Next = (PIP_ADDR_STRING)((LPBYTE)pFixedInfo + sizeof(FIXED_INFO));
1527 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1530 pFixedInfo->NodeType = HYBRID_NODETYPE;
1531 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1532 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1533 if (regReturn != ERROR_SUCCESS)
1534 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1535 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1537 if (regReturn == ERROR_SUCCESS)
1539 DWORD size = sizeof(pFixedInfo->ScopeId);
1541 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1545 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1546 I suppose could also check for a listener on port 53 to set EnableDns */
1548 TRACE("returning %d\n", ret);
1553 /******************************************************************
1554 * GetNumberOfInterfaces (IPHLPAPI.@)
1556 * Get the number of interfaces.
1559 * pdwNumIf [Out] number of interfaces
1562 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1564 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1568 TRACE("pdwNumIf %p\n", pdwNumIf);
1570 ret = ERROR_INVALID_PARAMETER;
1572 *pdwNumIf = getNumInterfaces();
1575 TRACE("returning %d\n", ret);
1580 /******************************************************************
1581 * GetPerAdapterInfo (IPHLPAPI.@)
1583 * Get information about an adapter corresponding to an interface.
1586 * IfIndex [In] interface info
1587 * pPerAdapterInfo [Out] buffer for per adapter info
1588 * pOutBufLen [In/Out] length of output buffer
1592 * Failure: error code from winerror.h
1595 * Stub, returns empty IP_PER_ADAPTER_INFO in every case.
1597 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1599 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO);
1601 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1603 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1605 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1607 *pOutBufLen = bytesNeeded;
1608 return ERROR_BUFFER_OVERFLOW;
1611 memset(pPerAdapterInfo, 0, bytesNeeded);
1616 /******************************************************************
1617 * GetRTTAndHopCount (IPHLPAPI.@)
1619 * Get round-trip time (RTT) and hop count.
1623 * DestIpAddress [In] destination address to get the info for
1624 * HopCount [Out] retrieved hop count
1625 * MaxHops [In] maximum hops to search for the destination
1626 * RTT [Out] RTT in milliseconds
1633 * Stub, returns FALSE.
1635 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1637 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1638 DestIpAddress, HopCount, MaxHops, RTT);
1643 /******************************************************************
1644 * GetTcpTable (IPHLPAPI.@)
1646 * Get the table of active TCP connections.
1649 * pTcpTable [Out] buffer for TCP connections table
1650 * pdwSize [In/Out] length of output buffer
1651 * bOrder [In] whether to order the table
1655 * Failure: error code from winerror.h
1658 * If pdwSize is less than required, the function will return
1659 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1660 * the required byte size.
1661 * If bOrder is true, the returned table will be sorted, first by
1662 * local address and port number, then by remote address and port
1665 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1668 PMIB_TCPTABLE table;
1670 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1672 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1674 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1676 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1677 if (!pTcpTable || *pdwSize < size) {
1679 ret = ERROR_INSUFFICIENT_BUFFER;
1683 memcpy(pTcpTable, table, size);
1685 HeapFree(GetProcessHeap(), 0, table);
1687 TRACE("returning %d\n", ret);
1692 /******************************************************************
1693 * GetUdpTable (IPHLPAPI.@)
1695 * Get a table of active UDP connections.
1698 * pUdpTable [Out] buffer for UDP connections table
1699 * pdwSize [In/Out] length of output buffer
1700 * bOrder [In] whether to order the table
1704 * Failure: error code from winerror.h
1707 * If pdwSize is less than required, the function will return
1708 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1709 * required byte size.
1710 * If bOrder is true, the returned table will be sorted, first by
1711 * local address, then by local port number.
1713 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1716 PMIB_UDPTABLE table;
1718 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1720 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1722 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1724 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1725 if (!pUdpTable || *pdwSize < size) {
1727 ret = ERROR_INSUFFICIENT_BUFFER;
1731 memcpy(pUdpTable, table, size);
1733 HeapFree(GetProcessHeap(), 0, table);
1735 TRACE("returning %d\n", ret);
1740 /******************************************************************
1741 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1743 * This is a Win98-only function to get information on "unidirectional"
1744 * adapters. Since this is pretty nonsensical in other contexts, it
1745 * never returns anything.
1748 * pIPIfInfo [Out] buffer for adapter infos
1749 * dwOutBufLen [Out] length of the output buffer
1753 * Failure: error code from winerror.h
1756 * Stub, returns ERROR_NOT_SUPPORTED.
1758 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1760 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1761 /* a unidirectional adapter?? not bloody likely! */
1762 return ERROR_NOT_SUPPORTED;
1766 /******************************************************************
1767 * IpReleaseAddress (IPHLPAPI.@)
1769 * Release an IP obtained through DHCP,
1772 * AdapterInfo [In] adapter to release IP address
1776 * Failure: error code from winerror.h
1779 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1780 * this function does nothing.
1783 * Stub, returns ERROR_NOT_SUPPORTED.
1785 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1787 TRACE("AdapterInfo %p\n", AdapterInfo);
1788 /* not a stub, never going to support this (and I never mark an adapter as
1789 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1790 return ERROR_NOT_SUPPORTED;
1794 /******************************************************************
1795 * IpRenewAddress (IPHLPAPI.@)
1797 * Renew an IP obtained through DHCP.
1800 * AdapterInfo [In] adapter to renew IP address
1804 * Failure: error code from winerror.h
1807 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1808 * this function does nothing.
1811 * Stub, returns ERROR_NOT_SUPPORTED.
1813 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1815 TRACE("AdapterInfo %p\n", AdapterInfo);
1816 /* not a stub, never going to support this (and I never mark an adapter as
1817 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1818 return ERROR_NOT_SUPPORTED;
1822 /******************************************************************
1823 * NotifyAddrChange (IPHLPAPI.@)
1825 * Notify caller whenever the ip-interface map is changed.
1828 * Handle [Out] handle usable in asynchronous notification
1829 * overlapped [In] overlapped structure that notifies the caller
1833 * Failure: error code from winerror.h
1836 * Stub, returns ERROR_NOT_SUPPORTED.
1838 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1840 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1841 return ERROR_NOT_SUPPORTED;
1845 /******************************************************************
1846 * NotifyRouteChange (IPHLPAPI.@)
1848 * Notify caller whenever the ip routing table is changed.
1851 * Handle [Out] handle usable in asynchronous notification
1852 * overlapped [In] overlapped structure that notifies the caller
1856 * Failure: error code from winerror.h
1859 * Stub, returns ERROR_NOT_SUPPORTED.
1861 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
1863 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
1864 return ERROR_NOT_SUPPORTED;
1868 /******************************************************************
1869 * SendARP (IPHLPAPI.@)
1871 * Send an ARP request.
1874 * DestIP [In] attempt to obtain this IP
1875 * SrcIP [In] optional sender IP address
1876 * pMacAddr [Out] buffer for the mac address
1877 * PhyAddrLen [In/Out] length of the output buffer
1881 * Failure: error code from winerror.h
1884 * Stub, returns ERROR_NOT_SUPPORTED.
1886 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
1888 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
1889 DestIP, SrcIP, pMacAddr, PhyAddrLen);
1890 return ERROR_NOT_SUPPORTED;
1894 /******************************************************************
1895 * SetIfEntry (IPHLPAPI.@)
1897 * Set the administrative status of an interface.
1900 * pIfRow [In] dwAdminStatus member specifies the new status.
1904 * Failure: error code from winerror.h
1907 * Stub, returns ERROR_NOT_SUPPORTED.
1909 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
1911 FIXME("(pIfRow %p): stub\n", pIfRow);
1912 /* this is supposed to set an interface administratively up or down.
1913 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
1914 this sort of down is indistinguishable from other sorts of down (e.g. no
1916 return ERROR_NOT_SUPPORTED;
1920 /******************************************************************
1921 * SetIpForwardEntry (IPHLPAPI.@)
1923 * Modify an existing route.
1926 * pRoute [In] route with the new information
1930 * Failure: error code from winerror.h
1933 * Stub, returns NO_ERROR.
1935 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
1937 FIXME("(pRoute %p): stub\n", pRoute);
1938 /* this is to add a route entry, how's it distinguishable from
1939 CreateIpForwardEntry?
1940 could use SIOCADDRT, not sure I want to */
1945 /******************************************************************
1946 * SetIpNetEntry (IPHLPAPI.@)
1948 * Modify an existing ARP entry.
1951 * pArpEntry [In] ARP entry with the new information
1955 * Failure: error code from winerror.h
1958 * Stub, returns NO_ERROR.
1960 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
1962 FIXME("(pArpEntry %p): stub\n", pArpEntry);
1963 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
1968 /******************************************************************
1969 * SetIpStatistics (IPHLPAPI.@)
1971 * Toggle IP forwarding and det the default TTL value.
1974 * pIpStats [In] IP statistics with the new information
1978 * Failure: error code from winerror.h
1981 * Stub, returns NO_ERROR.
1983 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
1985 FIXME("(pIpStats %p): stub\n", pIpStats);
1990 /******************************************************************
1991 * SetIpTTL (IPHLPAPI.@)
1993 * Set the default TTL value.
1996 * nTTL [In] new TTL value
2000 * Failure: error code from winerror.h
2003 * Stub, returns NO_ERROR.
2005 DWORD WINAPI SetIpTTL(UINT nTTL)
2007 FIXME("(nTTL %d): stub\n", nTTL);
2008 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2009 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2014 /******************************************************************
2015 * SetTcpEntry (IPHLPAPI.@)
2017 * Set the state of a TCP connection.
2020 * pTcpRow [In] specifies connection with new state
2024 * Failure: error code from winerror.h
2027 * Stub, returns NO_ERROR.
2029 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2031 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2036 /******************************************************************
2037 * UnenableRouter (IPHLPAPI.@)
2039 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2040 * if it reaches zero.
2043 * pOverlapped [In/Out] should be the same as in EnableRouter()
2044 * lpdwEnableCount [Out] optional, receives reference count
2048 * Failure: error code from winerror.h
2051 * Stub, returns ERROR_NOT_SUPPORTED.
2053 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2055 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2057 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2058 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2060 return ERROR_NOT_SUPPORTED;