mshtml: Don't free string that will be cleared by SafeArrayDestroy.
[wine/wine-gecko.git] / dlls / iphlpapi / iphlpapi_main.c
blob84ed5584532d2d34d23617bcd156aa5225ba9d3f
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_NET_IF_H
30 #include <net/if.h>
31 #endif
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_ARPA_NAMESER_H
39 # include <arpa/nameser.h>
40 #endif
41 #ifdef HAVE_RESOLV_H
42 # include <resolv.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
52 #include "ws2ipdef.h"
53 #include "iphlpapi.h"
54 #include "ifenum.h"
55 #include "ipstats.h"
56 #include "ipifcons.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
62 #ifndef IF_NAMESIZE
63 #define IF_NAMESIZE 16
64 #endif
66 #ifndef INADDR_NONE
67 #define INADDR_NONE ~0UL
68 #endif
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)
75 res_init();
78 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
80 switch (fdwReason) {
81 case DLL_PROCESS_ATTACH:
82 DisableThreadLibraryCalls( hinstDLL );
83 break;
85 case DLL_PROCESS_DETACH:
86 break;
88 return TRUE;
91 /******************************************************************
92 * AddIPAddress (IPHLPAPI.@)
94 * Add an IP address to an adapter.
96 * PARAMS
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
103 * RETURNS
104 * Success: NO_ERROR
105 * Failure: error code from winerror.h
107 * FIXME
108 * Stub. Currently returns ERROR_NOT_SUPPORTED.
110 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
112 FIXME(":stub\n");
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.
123 * PARAMS
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
130 * RETURNS
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)
137 DWORD ret;
139 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
140 bOrder, heap, flags);
141 if (!ppIfTable)
142 ret = ERROR_INVALID_PARAMETER;
143 else {
144 DWORD dwSize = 0;
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);
153 return ret;
157 static int IpAddrTableSorter(const void *a, const void *b)
159 int ret;
161 if (a && b)
162 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
163 else
164 ret = 0;
165 return ret;
169 /******************************************************************
170 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
172 * Get interface-to-IP address mapping table.
173 * Like GetIpAddrTable(), but allocate the returned table from heap.
175 * PARAMS
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
182 * RETURNS
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)
189 DWORD ret;
191 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
192 ppIpAddrTable, bOrder, heap, flags);
193 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
194 if (!ret && bOrder)
195 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
196 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
197 TRACE("returning %d\n", ret);
198 return ret;
202 /******************************************************************
203 * CreateIpForwardEntry (IPHLPAPI.@)
205 * Create a route in the local computer's IP table.
207 * PARAMS
208 * pRoute [In] new route information
210 * RETURNS
211 * Success: NO_ERROR
212 * Failure: error code from winerror.h
214 * FIXME
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 */
221 return 0;
225 /******************************************************************
226 * CreateIpNetEntry (IPHLPAPI.@)
228 * Create entry in the ARP table.
230 * PARAMS
231 * pArpEntry [In] new ARP entry
233 * RETURNS
234 * Success: NO_ERROR
235 * Failure: error code from winerror.h
237 * FIXME
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 */
244 return 0;
248 /******************************************************************
249 * CreateProxyArpEntry (IPHLPAPI.@)
251 * Create a Proxy ARP (PARP) entry for an IP address.
253 * PARAMS
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
258 * RETURNS
259 * Success: NO_ERROR
260 * Failure: error code from winerror.h
262 * FIXME
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().
278 * PARAMS
279 * NTEContext [In] NTE context from AddIPAddress();
281 * RETURNS
282 * Success: NO_ERROR
283 * Failure: error code from winerror.h
285 * FIXME
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.@)
298 * Delete a route.
300 * PARAMS
301 * pRoute [In] route to delete
303 * RETURNS
304 * Success: NO_ERROR
305 * Failure: error code from winerror.h
307 * FIXME
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 */
314 return 0;
318 /******************************************************************
319 * DeleteIpNetEntry (IPHLPAPI.@)
321 * Delete an ARP entry.
323 * PARAMS
324 * pArpEntry [In] ARP entry to delete
326 * RETURNS
327 * Success: NO_ERROR
328 * Failure: error code from winerror.h
330 * FIXME
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 */
337 return 0;
341 /******************************************************************
342 * DeleteProxyArpEntry (IPHLPAPI.@)
344 * Delete a Proxy ARP entry.
346 * PARAMS
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
351 * RETURNS
352 * Success: NO_ERROR
353 * Failure: error code from winerror.h
355 * FIXME
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.
371 * PARAMS
372 * pHandle [In/Out]
373 * pOverlapped [In/Out] hEvent member should contain a valid handle.
375 * RETURNS
376 * Success: ERROR_IO_PENDING
377 * Failure: error code from winerror.h
379 * FIXME
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
397 * PARAMS
398 * dwIfIndex [In] interface index
400 * RETURNS
401 * Success: NO_ERROR
402 * Failure: error code from winerror.h
404 * FIXME
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.
420 * PARAMS
421 * AdapterName [In] unicode string with the adapter name
422 * IfIndex [Out] returns found interface index
424 * RETURNS
425 * Success: NO_ERROR
426 * Failure: error code from winerror.h
428 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
430 char adapterName[MAX_ADAPTER_NAME];
431 unsigned int i;
432 DWORD ret;
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);
442 return ret;
446 /******************************************************************
447 * GetAdaptersInfo (IPHLPAPI.@)
449 * Get information about adapters.
451 * PARAMS
452 * pAdapterInfo [Out] buffer for adapter infos
453 * pOutBufLen [In] length of output buffer
455 * RETURNS
456 * Success: NO_ERROR
457 * Failure: error code from winerror.h
459 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
461 DWORD ret;
463 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
464 if (!pOutBufLen)
465 ret = ERROR_INVALID_PARAMETER;
466 else {
467 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
469 if (numNonLoopbackInterfaces > 0) {
470 DWORD numIPAddresses = getNumIPAddresses();
471 ULONG size;
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) {
481 *pOutBufLen = size;
482 ret = ERROR_BUFFER_OVERFLOW;
484 else {
485 InterfaceIndexTable *table = NULL;
486 PMIB_IPADDRTABLE ipAddrTable = NULL;
487 PMIB_IPFORWARDTABLE routeTable = NULL;
489 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
490 if (!ret)
491 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
492 if (!ret)
493 table = getNonLoopbackInterfaceIndexTable();
494 if (table) {
495 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
496 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
497 if (*pOutBufLen < size) {
498 *pOutBufLen = size;
499 ret = ERROR_INSUFFICIENT_BUFFER;
501 else {
502 DWORD ndx;
503 HKEY hKey;
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);
514 unsigned long addr;
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)
520 winsEnabled = TRUE;
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)
526 winsEnabled = TRUE;
527 RegCloseKey(hKey);
529 for (ndx = 0; ndx < table->numIndexes; ndx++) {
530 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
531 DWORD i;
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) {
544 if (firstIPAddr) {
545 toIPAddressString(ipAddrTable->table[i].dwAddr,
546 ptr->IpAddressList.IpAddress.String);
547 toIPAddressString(ipAddrTable->table[i].dwMask,
548 ptr->IpAddressList.IpMask.String);
549 firstIPAddr = FALSE;
551 else {
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);
558 nextIPAddr++;
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);
570 if (winsEnabled) {
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];
579 else
580 ptr->Next = NULL;
582 ret = NO_ERROR;
584 HeapFree(GetProcessHeap(), 0, table);
586 else
587 ret = ERROR_OUTOFMEMORY;
588 HeapFree(GetProcessHeap(), 0, routeTable);
589 HeapFree(GetProcessHeap(), 0, ipAddrTable);
592 else
593 ret = ERROR_NO_DATA;
595 TRACE("returning %d\n", ret);
596 return ret;
599 static DWORD typeFromMibType(DWORD mib_type)
601 switch (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)
613 switch (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)
623 ULONG ret, i, j;
624 MIB_IPADDRTABLE *at;
626 *num_addrs = 0;
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)
647 const BYTE *addrp;
648 char *p = buf;
650 for (addrp = (const BYTE *)in_addr;
651 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
652 addrp++)
654 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
655 sprintf(p, "%d", *addrp);
656 else
657 p += sprintf(p, "%d.", *addrp);
659 return buf;
662 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
664 const IN6_ADDR *addr = &sin->sin6_addr;
665 char *p = buf;
666 int i;
667 BOOL in_zero = FALSE;
669 for (i = 0; i < 7; i++)
671 if (!addr->u.Word[i])
673 if (i == 0)
674 *p++ = ':';
675 if (!in_zero)
677 *p++ = ':';
678 in_zero = TRUE;
681 else
683 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
684 in_zero = FALSE;
687 sprintf(p, "%x", ntohs(addr->u.Word[7]));
688 return buf;
691 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
693 DWORD i, num_gateways = 0;
695 for (i = 0; i < routeTable->dwNumEntries; i++)
697 if (routeTable->table[i].dwForwardIfIndex == index &&
698 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
699 num_gateways++;
701 return num_gateways;
704 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
705 PMIB_IPFORWARDTABLE routeTable)
707 DWORD i;
708 PMIB_IPFORWARDROW row = NULL;
710 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
712 if (routeTable->table[i].dwForwardIfIndex == index &&
713 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
714 row = &routeTable->table[i];
716 return row;
719 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
720 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
722 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
723 DWORD *v4addrs = NULL;
724 SOCKET_ADDRESS *v6addrs = NULL;
725 PMIB_IPFORWARDTABLE routeTable = NULL;
727 if (family == WS_AF_INET)
729 if (!(flags & GAA_FLAG_SKIP_UNICAST))
730 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
731 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
733 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
734 GetProcessHeap(), 0);
735 if (!ret)
736 num_v4_gateways = count_v4_gateways(index, routeTable);
739 else if (family == WS_AF_INET6)
741 if (!(flags & GAA_FLAG_SKIP_UNICAST))
742 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
744 else if (family == WS_AF_UNSPEC)
746 if (!(flags & GAA_FLAG_SKIP_UNICAST))
747 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
748 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
750 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
751 GetProcessHeap(), 0);
752 if (!ret)
754 num_v4_gateways = count_v4_gateways(index, routeTable);
755 if (!(flags & GAA_FLAG_SKIP_UNICAST))
756 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
760 else
762 FIXME("address family %u unsupported\n", family);
763 ret = ERROR_NO_DATA;
765 if (ret)
767 HeapFree(GetProcessHeap(), 0, routeTable);
768 return ret;
771 total_size = sizeof(IP_ADAPTER_ADDRESSES);
772 total_size += IF_NAMESIZE;
773 total_size += IF_NAMESIZE * sizeof(WCHAR);
774 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
775 total_size += IF_NAMESIZE * sizeof(WCHAR);
776 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
777 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
778 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
779 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
780 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
781 for (i = 0; i < num_v6addrs; i++)
782 total_size += v6addrs[i].iSockaddrLength;
784 if (aa && *size >= total_size)
786 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
787 WCHAR *dst;
788 DWORD buflen, type, status;
790 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
791 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
792 aa->u.s.IfIndex = index;
794 getInterfaceNameByIndex(index, name);
795 memcpy(ptr, name, IF_NAMESIZE);
796 aa->AdapterName = ptr;
797 ptr += IF_NAMESIZE;
798 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
800 aa->FriendlyName = (WCHAR *)ptr;
801 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
802 *dst = *src;
803 *dst++ = 0;
804 ptr = (char *)dst;
806 aa->Description = (WCHAR *)ptr;
807 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
808 *dst = *src;
809 *dst++ = 0;
810 ptr = (char *)dst;
812 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
813 num_v6addrs);
814 if (num_v4_gateways)
816 PMIB_IPFORWARDROW adapterRow;
818 if ((adapterRow = findIPv4Gateway(index, routeTable)))
820 PIP_ADAPTER_GATEWAY_ADDRESS gw;
821 PSOCKADDR_IN sin;
823 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
824 aa->FirstGatewayAddress = gw;
826 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
827 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
828 sin = (PSOCKADDR_IN)ptr;
829 sin->sin_family = AF_INET;
830 sin->sin_port = 0;
831 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
832 sizeof(DWORD));
833 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
834 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
835 gw->Next = NULL;
836 ptr += sizeof(SOCKADDR_IN);
839 if (num_v4addrs)
841 IP_ADAPTER_UNICAST_ADDRESS *ua;
842 struct sockaddr_in *sa;
843 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
844 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
845 for (i = 0; i < num_v4addrs; i++)
847 char addr_buf[16];
849 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
850 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
851 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
852 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
854 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
855 sa->sin_family = AF_INET;
856 sa->sin_addr.s_addr = v4addrs[i];
857 sa->sin_port = 0;
858 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
859 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
861 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
862 if (i < num_v4addrs - 1)
864 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
865 ua = ua->Next;
869 if (num_v6addrs)
871 IP_ADAPTER_UNICAST_ADDRESS *ua;
872 struct WS_sockaddr_in6 *sa;
874 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
875 if (aa->FirstUnicastAddress)
877 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
879 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
880 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
882 else
883 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
884 for (i = 0; i < num_v6addrs; i++)
886 char addr_buf[46];
888 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
889 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
890 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
891 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
893 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
894 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
895 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
896 debugstr_ipv6(sa, addr_buf));
898 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
899 if (i < num_v6addrs - 1)
901 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
902 ua = ua->Next;
907 buflen = MAX_INTERFACE_PHYSADDR;
908 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
909 aa->PhysicalAddressLength = buflen;
910 aa->IfType = typeFromMibType(type);
911 aa->ConnectionType = connectionTypeFromMibType(type);
913 getInterfaceMtuByName(name, &aa->Mtu);
915 getInterfaceStatusByName(name, &status);
916 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
917 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
918 else aa->OperStatus = IfOperStatusUnknown;
920 *size = total_size;
921 HeapFree(GetProcessHeap(), 0, routeTable);
922 HeapFree(GetProcessHeap(), 0, v6addrs);
923 HeapFree(GetProcessHeap(), 0, v4addrs);
924 return ERROR_SUCCESS;
927 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
929 DWORD size;
931 initialise_resolver();
932 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
933 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
934 * available.
936 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
937 if (!address || *len < size)
939 *len = size;
940 return ERROR_BUFFER_OVERFLOW;
942 *len = size;
943 if (_res.nscount > 0)
945 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
946 int i;
948 for (i = 0, addr = address; i < _res.nscount && addr;
949 i++, addr = addr->Next)
951 SOCKADDR_IN *sin;
953 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
954 addr->Address.lpSockaddr =
955 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
956 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
957 sin->sin_family = WS_AF_INET;
958 sin->sin_port = _res.nsaddr_list[i].sin_port;
959 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
960 if (i == _res.nscount - 1)
961 addr->Next = NULL;
962 else
963 addr->Next =
964 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
965 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
968 return ERROR_SUCCESS;
971 static BOOL is_ip_address_string(const char *str)
973 struct in_addr in;
974 int ret;
976 ret = inet_aton(str, &in);
977 return ret != 0;
980 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
982 ULONG size, i;
983 char *found_suffix = NULL;
985 initialise_resolver();
986 /* Always return a NULL-terminated string, even if it's empty. */
987 size = sizeof(WCHAR);
988 for (i = 0, found_suffix = NULL;
989 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
991 /* This uses a heuristic to select a DNS suffix:
992 * the first, non-IP address string is selected.
994 if (!is_ip_address_string(_res.dnsrch[i]))
995 found_suffix = _res.dnsrch[i];
997 if (found_suffix)
998 size += strlen(found_suffix) * sizeof(WCHAR);
999 if (!suffix || *len < size)
1001 *len = size;
1002 return ERROR_BUFFER_OVERFLOW;
1004 *len = size;
1005 if (found_suffix)
1007 char *p;
1009 for (p = found_suffix; *p; p++)
1010 *suffix++ = *p;
1012 *suffix = 0;
1013 return ERROR_SUCCESS;
1016 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1017 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1019 InterfaceIndexTable *table;
1020 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1022 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1024 if (!buflen) return ERROR_INVALID_PARAMETER;
1026 table = getInterfaceIndexTable();
1027 if (!table || !table->numIndexes)
1029 HeapFree(GetProcessHeap(), 0, table);
1030 return ERROR_NO_DATA;
1032 total_size = 0;
1033 for (i = 0; i < table->numIndexes; i++)
1035 size = 0;
1036 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1038 HeapFree(GetProcessHeap(), 0, table);
1039 return ret;
1041 total_size += size;
1043 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1045 /* Since DNS servers aren't really per adapter, get enough space for a
1046 * single copy of them.
1048 get_dns_server_addresses(NULL, &dns_server_size);
1049 total_size += dns_server_size;
1051 /* Since DNS suffix also isn't really per adapter, get enough space for a
1052 * single copy of it.
1054 get_dns_suffix(NULL, &dns_suffix_size);
1055 total_size += dns_suffix_size;
1056 if (aa && *buflen >= total_size)
1058 ULONG bytes_left = size = total_size;
1059 PIP_ADAPTER_ADDRESSES first_aa = aa;
1060 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1061 WCHAR *dnsSuffix;
1063 for (i = 0; i < table->numIndexes; i++)
1065 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1067 HeapFree(GetProcessHeap(), 0, table);
1068 return ret;
1070 if (i < table->numIndexes - 1)
1072 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1073 aa = aa->Next;
1074 size = bytes_left -= size;
1077 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1079 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)aa + total_size - dns_server_size - dns_suffix_size);
1080 get_dns_server_addresses(firstDns, &dns_server_size);
1081 for (aa = first_aa; aa; aa = aa->Next)
1083 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1084 aa->FirstDnsServerAddress = firstDns;
1087 aa = first_aa;
1088 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1089 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1090 for (; aa; aa = aa->Next)
1092 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1093 aa->DnsSuffix = dnsSuffix;
1095 ret = ERROR_SUCCESS;
1097 if (*buflen < total_size) ret = ERROR_BUFFER_OVERFLOW;
1098 *buflen = total_size;
1100 TRACE("num adapters %u\n", table->numIndexes);
1101 HeapFree(GetProcessHeap(), 0, table);
1102 return ret;
1105 /******************************************************************
1106 * GetBestInterface (IPHLPAPI.@)
1108 * Get the interface, with the best route for the given IP address.
1110 * PARAMS
1111 * dwDestAddr [In] IP address to search the interface for
1112 * pdwBestIfIndex [Out] found best interface
1114 * RETURNS
1115 * Success: NO_ERROR
1116 * Failure: error code from winerror.h
1118 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1120 struct WS_sockaddr_in sa_in;
1121 memset(&sa_in, 0, sizeof(sa_in));
1122 sa_in.sin_family = AF_INET;
1123 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1124 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1127 /******************************************************************
1128 * GetBestInterfaceEx (IPHLPAPI.@)
1130 * Get the interface, with the best route for the given IP address.
1132 * PARAMS
1133 * dwDestAddr [In] IP address to search the interface for
1134 * pdwBestIfIndex [Out] found best interface
1136 * RETURNS
1137 * Success: NO_ERROR
1138 * Failure: error code from winerror.h
1140 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1142 DWORD ret;
1144 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1145 if (!pDestAddr || !pdwBestIfIndex)
1146 ret = ERROR_INVALID_PARAMETER;
1147 else {
1148 MIB_IPFORWARDROW ipRow;
1150 if (pDestAddr->sa_family == AF_INET) {
1151 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1152 if (ret == ERROR_SUCCESS)
1153 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1154 } else {
1155 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1156 ret = ERROR_NOT_SUPPORTED;
1159 TRACE("returning %d\n", ret);
1160 return ret;
1164 /******************************************************************
1165 * GetBestRoute (IPHLPAPI.@)
1167 * Get the best route for the given IP address.
1169 * PARAMS
1170 * dwDestAddr [In] IP address to search the best route for
1171 * dwSourceAddr [In] optional source IP address
1172 * pBestRoute [Out] found best route
1174 * RETURNS
1175 * Success: NO_ERROR
1176 * Failure: error code from winerror.h
1178 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1180 PMIB_IPFORWARDTABLE table;
1181 DWORD ret;
1183 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1184 dwSourceAddr, pBestRoute);
1185 if (!pBestRoute)
1186 return ERROR_INVALID_PARAMETER;
1188 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1189 if (!ret) {
1190 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1192 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1193 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1194 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1195 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1196 DWORD numShifts, mask;
1198 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1199 mask && mask & 1; mask >>= 1, numShifts++)
1201 if (numShifts > matchedBits) {
1202 matchedBits = numShifts;
1203 matchedNdx = ndx;
1205 else if (!matchedBits) {
1206 matchedNdx = ndx;
1210 if (matchedNdx < table->dwNumEntries) {
1211 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1212 ret = ERROR_SUCCESS;
1214 else {
1215 /* No route matches, which can happen if there's no default route. */
1216 ret = ERROR_HOST_UNREACHABLE;
1218 HeapFree(GetProcessHeap(), 0, table);
1220 TRACE("returning %d\n", ret);
1221 return ret;
1225 /******************************************************************
1226 * GetFriendlyIfIndex (IPHLPAPI.@)
1228 * Get a "friendly" version of IfIndex, which is one that doesn't
1229 * have the top byte set. Doesn't validate whether IfIndex is a valid
1230 * adapter index.
1232 * PARAMS
1233 * IfIndex [In] interface index to get the friendly one for
1235 * RETURNS
1236 * A friendly version of IfIndex.
1238 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1240 /* windows doesn't validate these, either, just makes sure the top byte is
1241 cleared. I assume my ifenum module never gives an index with the top
1242 byte set. */
1243 TRACE("returning %d\n", IfIndex);
1244 return IfIndex;
1248 /******************************************************************
1249 * GetIfEntry (IPHLPAPI.@)
1251 * Get information about an interface.
1253 * PARAMS
1254 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1255 * Out: interface information
1257 * RETURNS
1258 * Success: NO_ERROR
1259 * Failure: error code from winerror.h
1261 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1263 DWORD ret;
1264 char nameBuf[MAX_ADAPTER_NAME];
1265 char *name;
1267 TRACE("pIfRow %p\n", pIfRow);
1268 if (!pIfRow)
1269 return ERROR_INVALID_PARAMETER;
1271 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1272 if (name) {
1273 ret = getInterfaceEntryByName(name, pIfRow);
1274 if (ret == NO_ERROR)
1275 ret = getInterfaceStatsByName(name, pIfRow);
1277 else
1278 ret = ERROR_INVALID_DATA;
1279 TRACE("returning %d\n", ret);
1280 return ret;
1284 static int IfTableSorter(const void *a, const void *b)
1286 int ret;
1288 if (a && b)
1289 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1290 else
1291 ret = 0;
1292 return ret;
1296 /******************************************************************
1297 * GetIfTable (IPHLPAPI.@)
1299 * Get a table of local interfaces.
1301 * PARAMS
1302 * pIfTable [Out] buffer for local interfaces table
1303 * pdwSize [In/Out] length of output buffer
1304 * bOrder [In] whether to sort the table
1306 * RETURNS
1307 * Success: NO_ERROR
1308 * Failure: error code from winerror.h
1310 * NOTES
1311 * If pdwSize is less than required, the function will return
1312 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1313 * size.
1314 * If bOrder is true, the returned table will be sorted by interface index.
1316 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1318 DWORD ret;
1320 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1321 (DWORD)bOrder);
1322 if (!pdwSize)
1323 ret = ERROR_INVALID_PARAMETER;
1324 else {
1325 DWORD numInterfaces = getNumInterfaces();
1326 ULONG size = sizeof(MIB_IFTABLE);
1328 if (numInterfaces > 1)
1329 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1330 if (!pIfTable || *pdwSize < size) {
1331 *pdwSize = size;
1332 ret = ERROR_INSUFFICIENT_BUFFER;
1334 else {
1335 InterfaceIndexTable *table = getInterfaceIndexTable();
1337 if (table) {
1338 size = sizeof(MIB_IFTABLE);
1339 if (table->numIndexes > 1)
1340 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1341 if (*pdwSize < size) {
1342 *pdwSize = size;
1343 ret = ERROR_INSUFFICIENT_BUFFER;
1345 else {
1346 DWORD ndx;
1348 *pdwSize = size;
1349 pIfTable->dwNumEntries = 0;
1350 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1351 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1352 GetIfEntry(&pIfTable->table[ndx]);
1353 pIfTable->dwNumEntries++;
1355 if (bOrder)
1356 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1357 IfTableSorter);
1358 ret = NO_ERROR;
1360 HeapFree(GetProcessHeap(), 0, table);
1362 else
1363 ret = ERROR_OUTOFMEMORY;
1366 TRACE("returning %d\n", ret);
1367 return ret;
1371 /******************************************************************
1372 * GetInterfaceInfo (IPHLPAPI.@)
1374 * Get a list of network interface adapters.
1376 * PARAMS
1377 * pIfTable [Out] buffer for interface adapters
1378 * dwOutBufLen [Out] if buffer is too small, returns required size
1380 * RETURNS
1381 * Success: NO_ERROR
1382 * Failure: error code from winerror.h
1384 * BUGS
1385 * MSDN states this should return non-loopback interfaces only.
1387 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1389 DWORD ret;
1391 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1392 if (!dwOutBufLen)
1393 ret = ERROR_INVALID_PARAMETER;
1394 else {
1395 DWORD numInterfaces = getNumInterfaces();
1396 ULONG size = sizeof(IP_INTERFACE_INFO);
1398 if (numInterfaces > 1)
1399 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1400 if (!pIfTable || *dwOutBufLen < size) {
1401 *dwOutBufLen = size;
1402 ret = ERROR_INSUFFICIENT_BUFFER;
1404 else {
1405 InterfaceIndexTable *table = getInterfaceIndexTable();
1407 if (table) {
1408 size = sizeof(IP_INTERFACE_INFO);
1409 if (table->numIndexes > 1)
1410 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1411 if (*dwOutBufLen < size) {
1412 *dwOutBufLen = size;
1413 ret = ERROR_INSUFFICIENT_BUFFER;
1415 else {
1416 DWORD ndx;
1417 char nameBuf[MAX_ADAPTER_NAME];
1419 *dwOutBufLen = size;
1420 pIfTable->NumAdapters = 0;
1421 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1422 const char *walker, *name;
1423 WCHAR *assigner;
1425 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1426 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1427 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1428 walker && *walker &&
1429 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1430 walker++, assigner++)
1431 *assigner = *walker;
1432 *assigner = 0;
1433 pIfTable->NumAdapters++;
1435 ret = NO_ERROR;
1437 HeapFree(GetProcessHeap(), 0, table);
1439 else
1440 ret = ERROR_OUTOFMEMORY;
1443 TRACE("returning %d\n", ret);
1444 return ret;
1448 /******************************************************************
1449 * GetIpAddrTable (IPHLPAPI.@)
1451 * Get interface-to-IP address mapping table.
1453 * PARAMS
1454 * pIpAddrTable [Out] buffer for mapping table
1455 * pdwSize [In/Out] length of output buffer
1456 * bOrder [In] whether to sort the table
1458 * RETURNS
1459 * Success: NO_ERROR
1460 * Failure: error code from winerror.h
1462 * NOTES
1463 * If pdwSize is less than required, the function will return
1464 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1465 * size.
1466 * If bOrder is true, the returned table will be sorted by the next hop and
1467 * an assortment of arbitrary parameters.
1469 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1471 DWORD ret;
1473 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1474 (DWORD)bOrder);
1475 if (!pdwSize)
1476 ret = ERROR_INVALID_PARAMETER;
1477 else {
1478 PMIB_IPADDRTABLE table;
1480 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1481 if (ret == NO_ERROR)
1483 ULONG size = sizeof(MIB_IPADDRTABLE);
1485 if (table->dwNumEntries > 1)
1486 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1487 if (!pIpAddrTable || *pdwSize < size) {
1488 *pdwSize = size;
1489 ret = ERROR_INSUFFICIENT_BUFFER;
1491 else {
1492 *pdwSize = size;
1493 memcpy(pIpAddrTable, table, size);
1494 if (bOrder)
1495 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1496 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1497 ret = NO_ERROR;
1499 HeapFree(GetProcessHeap(), 0, table);
1502 TRACE("returning %d\n", ret);
1503 return ret;
1507 /******************************************************************
1508 * GetIpForwardTable (IPHLPAPI.@)
1510 * Get the route table.
1512 * PARAMS
1513 * pIpForwardTable [Out] buffer for route table
1514 * pdwSize [In/Out] length of output buffer
1515 * bOrder [In] whether to sort the table
1517 * RETURNS
1518 * Success: NO_ERROR
1519 * Failure: error code from winerror.h
1521 * NOTES
1522 * If pdwSize is less than required, the function will return
1523 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1524 * size.
1525 * If bOrder is true, the returned table will be sorted by the next hop and
1526 * an assortment of arbitrary parameters.
1528 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1530 DWORD ret;
1531 PMIB_IPFORWARDTABLE table;
1533 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1535 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1537 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1538 if (!ret) {
1539 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1540 if (!pIpForwardTable || *pdwSize < size) {
1541 *pdwSize = size;
1542 ret = ERROR_INSUFFICIENT_BUFFER;
1544 else {
1545 *pdwSize = size;
1546 memcpy(pIpForwardTable, table, size);
1548 HeapFree(GetProcessHeap(), 0, table);
1550 TRACE("returning %d\n", ret);
1551 return ret;
1555 /******************************************************************
1556 * GetIpNetTable (IPHLPAPI.@)
1558 * Get the IP-to-physical address mapping table.
1560 * PARAMS
1561 * pIpNetTable [Out] buffer for mapping table
1562 * pdwSize [In/Out] length of output buffer
1563 * bOrder [In] whether to sort the table
1565 * RETURNS
1566 * Success: NO_ERROR
1567 * Failure: error code from winerror.h
1569 * NOTES
1570 * If pdwSize is less than required, the function will return
1571 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1572 * size.
1573 * If bOrder is true, the returned table will be sorted by IP address.
1575 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1577 DWORD ret;
1578 PMIB_IPNETTABLE table;
1580 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1582 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1584 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1585 if (!ret) {
1586 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1587 if (!pIpNetTable || *pdwSize < size) {
1588 *pdwSize = size;
1589 ret = ERROR_INSUFFICIENT_BUFFER;
1591 else {
1592 *pdwSize = size;
1593 memcpy(pIpNetTable, table, size);
1595 HeapFree(GetProcessHeap(), 0, table);
1597 TRACE("returning %d\n", ret);
1598 return ret;
1601 /* Gets the DNS server list into the list beginning at list. Assumes that
1602 * a single server address may be placed at list if *len is at least
1603 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1604 * and assumes that all remaining DNS servers are contiguously located
1605 * beginning at firstDynamic. On input, *len is assumed to be the total number
1606 * of bytes available for all DNS servers, and is ignored if list is NULL.
1607 * On return, *len is set to the total number of bytes required for all DNS
1608 * servers.
1609 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1610 * ERROR_SUCCESS otherwise.
1612 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1613 PIP_ADDR_STRING firstDynamic, DWORD *len)
1615 DWORD size;
1617 initialise_resolver();
1618 size = _res.nscount * sizeof(IP_ADDR_STRING);
1619 if (!list || *len < size) {
1620 *len = size;
1621 return ERROR_BUFFER_OVERFLOW;
1623 *len = size;
1624 if (_res.nscount > 0) {
1625 PIP_ADDR_STRING ptr;
1626 int i;
1628 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1629 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1630 ptr->IpAddress.String);
1631 if (i == _res.nscount - 1)
1632 ptr->Next = NULL;
1633 else if (i == 0)
1634 ptr->Next = firstDynamic;
1635 else
1636 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1639 return ERROR_SUCCESS;
1642 /******************************************************************
1643 * GetNetworkParams (IPHLPAPI.@)
1645 * Get the network parameters for the local computer.
1647 * PARAMS
1648 * pFixedInfo [Out] buffer for network parameters
1649 * pOutBufLen [In/Out] length of output buffer
1651 * RETURNS
1652 * Success: NO_ERROR
1653 * Failure: error code from winerror.h
1655 * NOTES
1656 * If pOutBufLen is less than required, the function will return
1657 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1658 * size.
1660 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1662 DWORD ret, size, serverListSize;
1663 LONG regReturn;
1664 HKEY hKey;
1666 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1667 if (!pOutBufLen)
1668 return ERROR_INVALID_PARAMETER;
1670 get_dns_server_list(NULL, NULL, &serverListSize);
1671 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1672 if (!pFixedInfo || *pOutBufLen < size) {
1673 *pOutBufLen = size;
1674 return ERROR_BUFFER_OVERFLOW;
1677 memset(pFixedInfo, 0, size);
1678 size = sizeof(pFixedInfo->HostName);
1679 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1680 size = sizeof(pFixedInfo->DomainName);
1681 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1682 get_dns_server_list(&pFixedInfo->DnsServerList,
1683 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1684 &serverListSize);
1685 /* Assume the first DNS server in the list is the "current" DNS server: */
1686 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1687 pFixedInfo->NodeType = HYBRID_NODETYPE;
1688 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1689 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1690 if (regReturn != ERROR_SUCCESS)
1691 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1692 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1693 &hKey);
1694 if (regReturn == ERROR_SUCCESS)
1696 DWORD size = sizeof(pFixedInfo->ScopeId);
1698 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1699 RegCloseKey(hKey);
1702 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1703 I suppose could also check for a listener on port 53 to set EnableDns */
1704 ret = NO_ERROR;
1705 TRACE("returning %d\n", ret);
1706 return ret;
1710 /******************************************************************
1711 * GetNumberOfInterfaces (IPHLPAPI.@)
1713 * Get the number of interfaces.
1715 * PARAMS
1716 * pdwNumIf [Out] number of interfaces
1718 * RETURNS
1719 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1721 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1723 DWORD ret;
1725 TRACE("pdwNumIf %p\n", pdwNumIf);
1726 if (!pdwNumIf)
1727 ret = ERROR_INVALID_PARAMETER;
1728 else {
1729 *pdwNumIf = getNumInterfaces();
1730 ret = NO_ERROR;
1732 TRACE("returning %d\n", ret);
1733 return ret;
1737 /******************************************************************
1738 * GetPerAdapterInfo (IPHLPAPI.@)
1740 * Get information about an adapter corresponding to an interface.
1742 * PARAMS
1743 * IfIndex [In] interface info
1744 * pPerAdapterInfo [Out] buffer for per adapter info
1745 * pOutBufLen [In/Out] length of output buffer
1747 * RETURNS
1748 * Success: NO_ERROR
1749 * Failure: error code from winerror.h
1751 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1753 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1754 DWORD ret = NO_ERROR;
1756 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1758 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1760 if (!isIfIndexLoopback(IfIndex)) {
1761 get_dns_server_list(NULL, NULL, &serverListSize);
1762 if (serverListSize > sizeof(IP_ADDR_STRING))
1763 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1765 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1767 *pOutBufLen = bytesNeeded;
1768 return ERROR_BUFFER_OVERFLOW;
1771 memset(pPerAdapterInfo, 0, bytesNeeded);
1772 if (!isIfIndexLoopback(IfIndex)) {
1773 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1774 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1775 &serverListSize);
1776 /* Assume the first DNS server in the list is the "current" DNS server: */
1777 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1779 return ret;
1783 /******************************************************************
1784 * GetRTTAndHopCount (IPHLPAPI.@)
1786 * Get round-trip time (RTT) and hop count.
1788 * PARAMS
1790 * DestIpAddress [In] destination address to get the info for
1791 * HopCount [Out] retrieved hop count
1792 * MaxHops [In] maximum hops to search for the destination
1793 * RTT [Out] RTT in milliseconds
1795 * RETURNS
1796 * Success: TRUE
1797 * Failure: FALSE
1799 * FIXME
1800 * Stub, returns FALSE.
1802 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1804 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1805 DestIpAddress, HopCount, MaxHops, RTT);
1806 return FALSE;
1810 /******************************************************************
1811 * GetTcpTable (IPHLPAPI.@)
1813 * Get the table of active TCP connections.
1815 * PARAMS
1816 * pTcpTable [Out] buffer for TCP connections table
1817 * pdwSize [In/Out] length of output buffer
1818 * bOrder [In] whether to order the table
1820 * RETURNS
1821 * Success: NO_ERROR
1822 * Failure: error code from winerror.h
1824 * NOTES
1825 * If pdwSize is less than required, the function will return
1826 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1827 * the required byte size.
1828 * If bOrder is true, the returned table will be sorted, first by
1829 * local address and port number, then by remote address and port
1830 * number.
1832 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1834 DWORD ret;
1835 PMIB_TCPTABLE table;
1837 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1839 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1841 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1842 if (!ret) {
1843 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1844 if (!pTcpTable || *pdwSize < size) {
1845 *pdwSize = size;
1846 ret = ERROR_INSUFFICIENT_BUFFER;
1848 else {
1849 *pdwSize = size;
1850 memcpy(pTcpTable, table, size);
1852 HeapFree(GetProcessHeap(), 0, table);
1854 TRACE("returning %d\n", ret);
1855 return ret;
1859 /******************************************************************
1860 * GetUdpTable (IPHLPAPI.@)
1862 * Get a table of active UDP connections.
1864 * PARAMS
1865 * pUdpTable [Out] buffer for UDP connections table
1866 * pdwSize [In/Out] length of output buffer
1867 * bOrder [In] whether to order the table
1869 * RETURNS
1870 * Success: NO_ERROR
1871 * Failure: error code from winerror.h
1873 * NOTES
1874 * If pdwSize is less than required, the function will return
1875 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1876 * required byte size.
1877 * If bOrder is true, the returned table will be sorted, first by
1878 * local address, then by local port number.
1880 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1882 DWORD ret;
1883 PMIB_UDPTABLE table;
1885 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1887 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1889 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1890 if (!ret) {
1891 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1892 if (!pUdpTable || *pdwSize < size) {
1893 *pdwSize = size;
1894 ret = ERROR_INSUFFICIENT_BUFFER;
1896 else {
1897 *pdwSize = size;
1898 memcpy(pUdpTable, table, size);
1900 HeapFree(GetProcessHeap(), 0, table);
1902 TRACE("returning %d\n", ret);
1903 return ret;
1907 /******************************************************************
1908 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1910 * This is a Win98-only function to get information on "unidirectional"
1911 * adapters. Since this is pretty nonsensical in other contexts, it
1912 * never returns anything.
1914 * PARAMS
1915 * pIPIfInfo [Out] buffer for adapter infos
1916 * dwOutBufLen [Out] length of the output buffer
1918 * RETURNS
1919 * Success: NO_ERROR
1920 * Failure: error code from winerror.h
1922 * FIXME
1923 * Stub, returns ERROR_NOT_SUPPORTED.
1925 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1927 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1928 /* a unidirectional adapter?? not bloody likely! */
1929 return ERROR_NOT_SUPPORTED;
1933 /******************************************************************
1934 * IpReleaseAddress (IPHLPAPI.@)
1936 * Release an IP obtained through DHCP,
1938 * PARAMS
1939 * AdapterInfo [In] adapter to release IP address
1941 * RETURNS
1942 * Success: NO_ERROR
1943 * Failure: error code from winerror.h
1945 * NOTES
1946 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1947 * this function does nothing.
1949 * FIXME
1950 * Stub, returns ERROR_NOT_SUPPORTED.
1952 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1954 TRACE("AdapterInfo %p\n", AdapterInfo);
1955 /* not a stub, never going to support this (and I never mark an adapter as
1956 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1957 return ERROR_NOT_SUPPORTED;
1961 /******************************************************************
1962 * IpRenewAddress (IPHLPAPI.@)
1964 * Renew an IP obtained through DHCP.
1966 * PARAMS
1967 * AdapterInfo [In] adapter to renew IP address
1969 * RETURNS
1970 * Success: NO_ERROR
1971 * Failure: error code from winerror.h
1973 * NOTES
1974 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1975 * this function does nothing.
1977 * FIXME
1978 * Stub, returns ERROR_NOT_SUPPORTED.
1980 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1982 TRACE("AdapterInfo %p\n", AdapterInfo);
1983 /* not a stub, never going to support this (and I never mark an adapter as
1984 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1985 return ERROR_NOT_SUPPORTED;
1989 /******************************************************************
1990 * NotifyAddrChange (IPHLPAPI.@)
1992 * Notify caller whenever the ip-interface map is changed.
1994 * PARAMS
1995 * Handle [Out] handle usable in asynchronous notification
1996 * overlapped [In] overlapped structure that notifies the caller
1998 * RETURNS
1999 * Success: NO_ERROR
2000 * Failure: error code from winerror.h
2002 * FIXME
2003 * Stub, returns ERROR_NOT_SUPPORTED.
2005 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2007 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2008 return ERROR_NOT_SUPPORTED;
2012 /******************************************************************
2013 * NotifyRouteChange (IPHLPAPI.@)
2015 * Notify caller whenever the ip routing table is changed.
2017 * PARAMS
2018 * Handle [Out] handle usable in asynchronous notification
2019 * overlapped [In] overlapped structure that notifies the caller
2021 * RETURNS
2022 * Success: NO_ERROR
2023 * Failure: error code from winerror.h
2025 * FIXME
2026 * Stub, returns ERROR_NOT_SUPPORTED.
2028 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2030 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2031 return ERROR_NOT_SUPPORTED;
2035 /******************************************************************
2036 * SendARP (IPHLPAPI.@)
2038 * Send an ARP request.
2040 * PARAMS
2041 * DestIP [In] attempt to obtain this IP
2042 * SrcIP [In] optional sender IP address
2043 * pMacAddr [Out] buffer for the mac address
2044 * PhyAddrLen [In/Out] length of the output buffer
2046 * RETURNS
2047 * Success: NO_ERROR
2048 * Failure: error code from winerror.h
2050 * FIXME
2051 * Stub, returns ERROR_NOT_SUPPORTED.
2053 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2055 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2056 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2057 return ERROR_NOT_SUPPORTED;
2061 /******************************************************************
2062 * SetIfEntry (IPHLPAPI.@)
2064 * Set the administrative status of an interface.
2066 * PARAMS
2067 * pIfRow [In] dwAdminStatus member specifies the new status.
2069 * RETURNS
2070 * Success: NO_ERROR
2071 * Failure: error code from winerror.h
2073 * FIXME
2074 * Stub, returns ERROR_NOT_SUPPORTED.
2076 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2078 FIXME("(pIfRow %p): stub\n", pIfRow);
2079 /* this is supposed to set an interface administratively up or down.
2080 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2081 this sort of down is indistinguishable from other sorts of down (e.g. no
2082 link). */
2083 return ERROR_NOT_SUPPORTED;
2087 /******************************************************************
2088 * SetIpForwardEntry (IPHLPAPI.@)
2090 * Modify an existing route.
2092 * PARAMS
2093 * pRoute [In] route with the new information
2095 * RETURNS
2096 * Success: NO_ERROR
2097 * Failure: error code from winerror.h
2099 * FIXME
2100 * Stub, returns NO_ERROR.
2102 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2104 FIXME("(pRoute %p): stub\n", pRoute);
2105 /* this is to add a route entry, how's it distinguishable from
2106 CreateIpForwardEntry?
2107 could use SIOCADDRT, not sure I want to */
2108 return 0;
2112 /******************************************************************
2113 * SetIpNetEntry (IPHLPAPI.@)
2115 * Modify an existing ARP entry.
2117 * PARAMS
2118 * pArpEntry [In] ARP entry with the new information
2120 * RETURNS
2121 * Success: NO_ERROR
2122 * Failure: error code from winerror.h
2124 * FIXME
2125 * Stub, returns NO_ERROR.
2127 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2129 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2130 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2131 return 0;
2135 /******************************************************************
2136 * SetIpStatistics (IPHLPAPI.@)
2138 * Toggle IP forwarding and det the default TTL value.
2140 * PARAMS
2141 * pIpStats [In] IP statistics with the new information
2143 * RETURNS
2144 * Success: NO_ERROR
2145 * Failure: error code from winerror.h
2147 * FIXME
2148 * Stub, returns NO_ERROR.
2150 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2152 FIXME("(pIpStats %p): stub\n", pIpStats);
2153 return 0;
2157 /******************************************************************
2158 * SetIpTTL (IPHLPAPI.@)
2160 * Set the default TTL value.
2162 * PARAMS
2163 * nTTL [In] new TTL value
2165 * RETURNS
2166 * Success: NO_ERROR
2167 * Failure: error code from winerror.h
2169 * FIXME
2170 * Stub, returns NO_ERROR.
2172 DWORD WINAPI SetIpTTL(UINT nTTL)
2174 FIXME("(nTTL %d): stub\n", nTTL);
2175 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2176 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2177 return 0;
2181 /******************************************************************
2182 * SetTcpEntry (IPHLPAPI.@)
2184 * Set the state of a TCP connection.
2186 * PARAMS
2187 * pTcpRow [In] specifies connection with new state
2189 * RETURNS
2190 * Success: NO_ERROR
2191 * Failure: error code from winerror.h
2193 * FIXME
2194 * Stub, returns NO_ERROR.
2196 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2198 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2199 return 0;
2203 /******************************************************************
2204 * UnenableRouter (IPHLPAPI.@)
2206 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2207 * if it reaches zero.
2209 * PARAMS
2210 * pOverlapped [In/Out] should be the same as in EnableRouter()
2211 * lpdwEnableCount [Out] optional, receives reference count
2213 * RETURNS
2214 * Success: NO_ERROR
2215 * Failure: error code from winerror.h
2217 * FIXME
2218 * Stub, returns ERROR_NOT_SUPPORTED.
2220 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2222 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2223 lpdwEnableCount);
2224 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2225 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2227 return ERROR_NOT_SUPPORTED;