usp10: Merge neutral scripts.
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
bloba2bdea3858c6a50ec9abdf70b5c021207e71fad4
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_NETINET_IN_H
27 # include <netinet/in.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
34 #endif
35 #ifdef HAVE_RESOLV_H
36 # include <resolv.h>
37 #endif
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winreg.h"
44 #define USE_WS_PREFIX
45 #include "winsock2.h"
46 #include "winternl.h"
47 #include "ws2ipdef.h"
48 #include "iphlpapi.h"
49 #include "ifenum.h"
50 #include "ipstats.h"
51 #include "ipifcons.h"
52 #include "fltdefs.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
58 #ifndef IF_NAMESIZE
59 #define IF_NAMESIZE 16
60 #endif
62 #ifndef INADDR_NONE
63 #define INADDR_NONE ~0UL
64 #endif
66 /* call res_init() just once because of a bug in Mac OS X 10.4 */
67 /* Call once per thread on systems that have per-thread _res. */
68 static void initialise_resolver(void)
70 if ((_res.options & RES_INIT) == 0)
71 res_init();
74 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
76 switch (fdwReason) {
77 case DLL_PROCESS_ATTACH:
78 DisableThreadLibraryCalls( hinstDLL );
79 break;
81 case DLL_PROCESS_DETACH:
82 break;
84 return TRUE;
87 /******************************************************************
88 * AddIPAddress (IPHLPAPI.@)
90 * Add an IP address to an adapter.
92 * PARAMS
93 * Address [In] IP address to add to the adapter
94 * IpMask [In] subnet mask for the IP address
95 * IfIndex [In] adapter index to add the address
96 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
97 * NTEInstance [Out] NTE instance for the IP address
99 * RETURNS
100 * Success: NO_ERROR
101 * Failure: error code from winerror.h
103 * FIXME
104 * Stub. Currently returns ERROR_NOT_SUPPORTED.
106 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
108 FIXME(":stub\n");
109 return ERROR_NOT_SUPPORTED;
113 /******************************************************************
114 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
116 * Get table of local interfaces.
117 * Like GetIfTable(), but allocate the returned table from heap.
119 * PARAMS
120 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
121 * allocated and returned.
122 * bOrder [In] whether to sort the table
123 * heap [In] heap from which the table is allocated
124 * flags [In] flags to HeapAlloc
126 * RETURNS
127 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
128 * GetIfTable() returns otherwise.
130 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
131 BOOL bOrder, HANDLE heap, DWORD flags)
133 DWORD ret;
135 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
136 bOrder, heap, flags);
137 if (!ppIfTable)
138 ret = ERROR_INVALID_PARAMETER;
139 else {
140 DWORD dwSize = 0;
142 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
143 if (ret == ERROR_INSUFFICIENT_BUFFER) {
144 *ppIfTable = HeapAlloc(heap, flags, dwSize);
145 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
148 TRACE("returning %d\n", ret);
149 return ret;
153 static int IpAddrTableSorter(const void *a, const void *b)
155 int ret;
157 if (a && b)
158 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
159 else
160 ret = 0;
161 return ret;
165 /******************************************************************
166 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
168 * Get interface-to-IP address mapping table.
169 * Like GetIpAddrTable(), but allocate the returned table from heap.
171 * PARAMS
172 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
173 * allocated and returned.
174 * bOrder [In] whether to sort the table
175 * heap [In] heap from which the table is allocated
176 * flags [In] flags to HeapAlloc
178 * RETURNS
179 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
180 * failure, NO_ERROR on success.
182 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
183 BOOL bOrder, HANDLE heap, DWORD flags)
185 DWORD ret;
187 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
188 ppIpAddrTable, bOrder, heap, flags);
189 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
190 if (!ret && bOrder)
191 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
192 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
193 TRACE("returning %d\n", ret);
194 return ret;
198 /******************************************************************
199 * CancelIPChangeNotify (IPHLPAPI.@)
201 * Cancel a previous notification created by NotifyAddrChange or
202 * NotifyRouteChange.
204 * PARAMS
205 * overlapped [In] overlapped structure that notifies the caller
207 * RETURNS
208 * Success: TRUE
209 * Failure: FALSE
211 * FIXME
212 * Stub, returns FALSE.
214 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
216 FIXME("(overlapped %p): stub\n", overlapped);
217 return FALSE;
222 /******************************************************************
223 * CreateIpForwardEntry (IPHLPAPI.@)
225 * Create a route in the local computer's IP table.
227 * PARAMS
228 * pRoute [In] new route information
230 * RETURNS
231 * Success: NO_ERROR
232 * Failure: error code from winerror.h
234 * FIXME
235 * Stub, always returns NO_ERROR.
237 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
239 FIXME("(pRoute %p): stub\n", pRoute);
240 /* could use SIOCADDRT, not sure I want to */
241 return 0;
245 /******************************************************************
246 * CreateIpNetEntry (IPHLPAPI.@)
248 * Create entry in the ARP table.
250 * PARAMS
251 * pArpEntry [In] new ARP entry
253 * RETURNS
254 * Success: NO_ERROR
255 * Failure: error code from winerror.h
257 * FIXME
258 * Stub, always returns NO_ERROR.
260 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
262 FIXME("(pArpEntry %p)\n", pArpEntry);
263 /* could use SIOCSARP on systems that support it, not sure I want to */
264 return 0;
268 /******************************************************************
269 * CreateProxyArpEntry (IPHLPAPI.@)
271 * Create a Proxy ARP (PARP) entry for an IP address.
273 * PARAMS
274 * dwAddress [In] IP address for which this computer acts as a proxy.
275 * dwMask [In] subnet mask for dwAddress
276 * dwIfIndex [In] interface index
278 * RETURNS
279 * Success: NO_ERROR
280 * Failure: error code from winerror.h
282 * FIXME
283 * Stub, returns ERROR_NOT_SUPPORTED.
285 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
287 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
288 dwAddress, dwMask, dwIfIndex);
289 return ERROR_NOT_SUPPORTED;
293 /******************************************************************
294 * DeleteIPAddress (IPHLPAPI.@)
296 * Delete an IP address added with AddIPAddress().
298 * PARAMS
299 * NTEContext [In] NTE context from AddIPAddress();
301 * RETURNS
302 * Success: NO_ERROR
303 * Failure: error code from winerror.h
305 * FIXME
306 * Stub, returns ERROR_NOT_SUPPORTED.
308 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
310 FIXME("(NTEContext %d): stub\n", NTEContext);
311 return ERROR_NOT_SUPPORTED;
315 /******************************************************************
316 * DeleteIpForwardEntry (IPHLPAPI.@)
318 * Delete a route.
320 * PARAMS
321 * pRoute [In] route to delete
323 * RETURNS
324 * Success: NO_ERROR
325 * Failure: error code from winerror.h
327 * FIXME
328 * Stub, returns NO_ERROR.
330 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
332 FIXME("(pRoute %p): stub\n", pRoute);
333 /* could use SIOCDELRT, not sure I want to */
334 return 0;
338 /******************************************************************
339 * DeleteIpNetEntry (IPHLPAPI.@)
341 * Delete an ARP entry.
343 * PARAMS
344 * pArpEntry [In] ARP entry to delete
346 * RETURNS
347 * Success: NO_ERROR
348 * Failure: error code from winerror.h
350 * FIXME
351 * Stub, returns NO_ERROR.
353 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
355 FIXME("(pArpEntry %p): stub\n", pArpEntry);
356 /* could use SIOCDARP on systems that support it, not sure I want to */
357 return 0;
361 /******************************************************************
362 * DeleteProxyArpEntry (IPHLPAPI.@)
364 * Delete a Proxy ARP entry.
366 * PARAMS
367 * dwAddress [In] IP address for which this computer acts as a proxy.
368 * dwMask [In] subnet mask for dwAddress
369 * dwIfIndex [In] interface index
371 * RETURNS
372 * Success: NO_ERROR
373 * Failure: error code from winerror.h
375 * FIXME
376 * Stub, returns ERROR_NOT_SUPPORTED.
378 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
380 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
381 dwAddress, dwMask, dwIfIndex);
382 return ERROR_NOT_SUPPORTED;
386 /******************************************************************
387 * EnableRouter (IPHLPAPI.@)
389 * Turn on ip forwarding.
391 * PARAMS
392 * pHandle [In/Out]
393 * pOverlapped [In/Out] hEvent member should contain a valid handle.
395 * RETURNS
396 * Success: ERROR_IO_PENDING
397 * Failure: error code from winerror.h
399 * FIXME
400 * Stub, returns ERROR_NOT_SUPPORTED.
402 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
404 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
405 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
406 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
408 return ERROR_NOT_SUPPORTED;
412 /******************************************************************
413 * FlushIpNetTable (IPHLPAPI.@)
415 * Delete all ARP entries of an interface
417 * PARAMS
418 * dwIfIndex [In] interface index
420 * RETURNS
421 * Success: NO_ERROR
422 * Failure: error code from winerror.h
424 * FIXME
425 * Stub, returns ERROR_NOT_SUPPORTED.
427 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
429 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
430 /* this flushes the arp cache of the given index */
431 return ERROR_NOT_SUPPORTED;
435 /******************************************************************
436 * GetAdapterIndex (IPHLPAPI.@)
438 * Get interface index from its name.
440 * PARAMS
441 * AdapterName [In] unicode string with the adapter name
442 * IfIndex [Out] returns found interface index
444 * RETURNS
445 * Success: NO_ERROR
446 * Failure: error code from winerror.h
448 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
450 char adapterName[MAX_ADAPTER_NAME];
451 unsigned int i;
452 DWORD ret;
454 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
455 /* The adapter name is guaranteed not to have any unicode characters, so
456 * this translation is never lossy */
457 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
458 adapterName[i] = (char)AdapterName[i];
459 adapterName[i] = '\0';
460 ret = getInterfaceIndexByName(adapterName, IfIndex);
461 TRACE("returning %d\n", ret);
462 return ret;
466 /******************************************************************
467 * GetAdaptersInfo (IPHLPAPI.@)
469 * Get information about adapters.
471 * PARAMS
472 * pAdapterInfo [Out] buffer for adapter infos
473 * pOutBufLen [In] length of output buffer
475 * RETURNS
476 * Success: NO_ERROR
477 * Failure: error code from winerror.h
479 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
481 DWORD ret;
483 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
484 if (!pOutBufLen)
485 ret = ERROR_INVALID_PARAMETER;
486 else {
487 DWORD numNonLoopbackInterfaces = getNumNonLoopbackInterfaces();
489 if (numNonLoopbackInterfaces > 0) {
490 DWORD numIPAddresses = getNumIPAddresses();
491 ULONG size;
493 /* This may slightly overestimate the amount of space needed, because
494 * the IP addresses include the loopback address, but it's easier
495 * to make sure there's more than enough space than to make sure there's
496 * precisely enough space.
498 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
499 size += numIPAddresses * sizeof(IP_ADDR_STRING);
500 if (!pAdapterInfo || *pOutBufLen < size) {
501 *pOutBufLen = size;
502 ret = ERROR_BUFFER_OVERFLOW;
504 else {
505 InterfaceIndexTable *table = NULL;
506 PMIB_IPADDRTABLE ipAddrTable = NULL;
507 PMIB_IPFORWARDTABLE routeTable = NULL;
509 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
510 if (!ret)
511 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
512 if (!ret)
513 table = getNonLoopbackInterfaceIndexTable();
514 if (table) {
515 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
516 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
517 if (*pOutBufLen < size) {
518 *pOutBufLen = size;
519 ret = ERROR_INSUFFICIENT_BUFFER;
521 else {
522 DWORD ndx;
523 HKEY hKey;
524 BOOL winsEnabled = FALSE;
525 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
526 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
527 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
529 memset(pAdapterInfo, 0, size);
530 /* @@ Wine registry key: HKCU\Software\Wine\Network */
531 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
532 &hKey) == ERROR_SUCCESS) {
533 DWORD size = sizeof(primaryWINS.String);
534 unsigned long addr;
536 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
537 (LPBYTE)primaryWINS.String, &size);
538 addr = inet_addr(primaryWINS.String);
539 if (addr != INADDR_NONE && addr != INADDR_ANY)
540 winsEnabled = TRUE;
541 size = sizeof(secondaryWINS.String);
542 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
543 (LPBYTE)secondaryWINS.String, &size);
544 addr = inet_addr(secondaryWINS.String);
545 if (addr != INADDR_NONE && addr != INADDR_ANY)
546 winsEnabled = TRUE;
547 RegCloseKey(hKey);
549 for (ndx = 0; ndx < table->numIndexes; ndx++) {
550 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
551 DWORD i;
552 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
553 BOOL firstIPAddr = TRUE;
555 /* on Win98 this is left empty, but whatever */
556 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
557 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
558 ptr->AddressLength = sizeof(ptr->Address);
559 getInterfacePhysicalByIndex(table->indexes[ndx],
560 &ptr->AddressLength, ptr->Address, &ptr->Type);
561 ptr->Index = table->indexes[ndx];
562 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
563 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
564 if (firstIPAddr) {
565 toIPAddressString(ipAddrTable->table[i].dwAddr,
566 ptr->IpAddressList.IpAddress.String);
567 toIPAddressString(ipAddrTable->table[i].dwMask,
568 ptr->IpAddressList.IpMask.String);
569 firstIPAddr = FALSE;
571 else {
572 currentIPAddr->Next = nextIPAddr;
573 currentIPAddr = nextIPAddr;
574 toIPAddressString(ipAddrTable->table[i].dwAddr,
575 currentIPAddr->IpAddress.String);
576 toIPAddressString(ipAddrTable->table[i].dwMask,
577 currentIPAddr->IpMask.String);
578 nextIPAddr++;
582 /* Find first router through this interface, which we'll assume
583 * is the default gateway for this adapter */
584 for (i = 0; i < routeTable->dwNumEntries; i++)
585 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
586 && routeTable->table[i].dwForwardType ==
587 MIB_IPROUTE_TYPE_INDIRECT)
588 toIPAddressString(routeTable->table[i].dwForwardNextHop,
589 ptr->GatewayList.IpAddress.String);
590 if (winsEnabled) {
591 ptr->HaveWins = TRUE;
592 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
593 primaryWINS.String, sizeof(primaryWINS.String));
594 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
595 secondaryWINS.String, sizeof(secondaryWINS.String));
597 if (ndx < table->numIndexes - 1)
598 ptr->Next = &pAdapterInfo[ndx + 1];
599 else
600 ptr->Next = NULL;
602 ret = NO_ERROR;
604 HeapFree(GetProcessHeap(), 0, table);
606 else
607 ret = ERROR_OUTOFMEMORY;
608 HeapFree(GetProcessHeap(), 0, routeTable);
609 HeapFree(GetProcessHeap(), 0, ipAddrTable);
612 else
613 ret = ERROR_NO_DATA;
615 TRACE("returning %d\n", ret);
616 return ret;
619 static DWORD typeFromMibType(DWORD mib_type)
621 switch (mib_type)
623 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
624 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
625 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
626 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
627 default: return IF_TYPE_OTHER;
631 static DWORD connectionTypeFromMibType(DWORD mib_type)
633 switch (mib_type)
635 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
636 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
637 default: return NET_IF_CONNECTION_DEDICATED;
641 static ULONG v4addressesFromIndex(DWORD index, DWORD **addrs, ULONG *num_addrs)
643 ULONG ret, i, j;
644 MIB_IPADDRTABLE *at;
646 *num_addrs = 0;
647 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
648 for (i = 0; i < at->dwNumEntries; i++)
650 if (at->table[i].dwIndex == index) (*num_addrs)++;
652 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
654 HeapFree(GetProcessHeap(), 0, at);
655 return ERROR_OUTOFMEMORY;
657 for (i = 0, j = 0; i < at->dwNumEntries; i++)
659 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
661 HeapFree(GetProcessHeap(), 0, at);
662 return ERROR_SUCCESS;
665 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
667 const BYTE *addrp;
668 char *p = buf;
670 for (addrp = (const BYTE *)in_addr;
671 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
672 addrp++)
674 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
675 sprintf(p, "%d", *addrp);
676 else
677 p += sprintf(p, "%d.", *addrp);
679 return buf;
682 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
684 const IN6_ADDR *addr = &sin->sin6_addr;
685 char *p = buf;
686 int i;
687 BOOL in_zero = FALSE;
689 for (i = 0; i < 7; i++)
691 if (!addr->u.Word[i])
693 if (i == 0)
694 *p++ = ':';
695 if (!in_zero)
697 *p++ = ':';
698 in_zero = TRUE;
701 else
703 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
704 in_zero = FALSE;
707 sprintf(p, "%x", ntohs(addr->u.Word[7]));
708 return buf;
711 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
713 DWORD i, num_gateways = 0;
715 for (i = 0; i < routeTable->dwNumEntries; i++)
717 if (routeTable->table[i].dwForwardIfIndex == index &&
718 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
719 num_gateways++;
721 return num_gateways;
724 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
725 PMIB_IPFORWARDTABLE routeTable)
727 DWORD i;
728 PMIB_IPFORWARDROW row = NULL;
730 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
732 if (routeTable->table[i].dwForwardIfIndex == index &&
733 routeTable->table[i].dwForwardType == MIB_IPROUTE_TYPE_INDIRECT)
734 row = &routeTable->table[i];
736 return row;
739 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, DWORD index,
740 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
742 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
743 DWORD *v4addrs = NULL;
744 SOCKET_ADDRESS *v6addrs = NULL;
745 PMIB_IPFORWARDTABLE routeTable = NULL;
747 if (family == WS_AF_INET)
749 if (!(flags & GAA_FLAG_SKIP_UNICAST))
750 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
751 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
753 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
754 GetProcessHeap(), 0);
755 if (!ret)
756 num_v4_gateways = count_v4_gateways(index, routeTable);
759 else if (family == WS_AF_INET6)
761 if (!(flags & GAA_FLAG_SKIP_UNICAST))
762 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
764 else if (family == WS_AF_UNSPEC)
766 if (!(flags & GAA_FLAG_SKIP_UNICAST))
767 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
768 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
770 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
771 GetProcessHeap(), 0);
772 if (!ret)
774 num_v4_gateways = count_v4_gateways(index, routeTable);
775 if (!(flags & GAA_FLAG_SKIP_UNICAST))
776 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
780 else
782 FIXME("address family %u unsupported\n", family);
783 ret = ERROR_NO_DATA;
785 if (ret)
787 HeapFree(GetProcessHeap(), 0, routeTable);
788 return ret;
791 total_size = sizeof(IP_ADAPTER_ADDRESSES);
792 total_size += IF_NAMESIZE;
793 total_size += IF_NAMESIZE * sizeof(WCHAR);
794 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
795 total_size += IF_NAMESIZE * sizeof(WCHAR);
796 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
797 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
798 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
799 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
800 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
801 for (i = 0; i < num_v6addrs; i++)
802 total_size += v6addrs[i].iSockaddrLength;
804 if (aa && *size >= total_size)
806 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
807 WCHAR *dst;
808 DWORD buflen, type, status;
810 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
811 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
812 aa->u.s.IfIndex = index;
814 getInterfaceNameByIndex(index, name);
815 memcpy(ptr, name, IF_NAMESIZE);
816 aa->AdapterName = ptr;
817 ptr += IF_NAMESIZE;
818 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
820 aa->FriendlyName = (WCHAR *)ptr;
821 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
822 *dst = *src;
823 *dst++ = 0;
824 ptr = (char *)dst;
826 aa->Description = (WCHAR *)ptr;
827 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
828 *dst = *src;
829 *dst++ = 0;
830 ptr = (char *)dst;
832 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
833 num_v6addrs);
834 if (num_v4_gateways)
836 PMIB_IPFORWARDROW adapterRow;
838 if ((adapterRow = findIPv4Gateway(index, routeTable)))
840 PIP_ADAPTER_GATEWAY_ADDRESS gw;
841 PSOCKADDR_IN sin;
843 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
844 aa->FirstGatewayAddress = gw;
846 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
847 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
848 sin = (PSOCKADDR_IN)ptr;
849 sin->sin_family = AF_INET;
850 sin->sin_port = 0;
851 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
852 sizeof(DWORD));
853 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
854 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
855 gw->Next = NULL;
856 ptr += sizeof(SOCKADDR_IN);
859 if (num_v4addrs)
861 IP_ADAPTER_UNICAST_ADDRESS *ua;
862 struct sockaddr_in *sa;
863 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
864 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
865 for (i = 0; i < num_v4addrs; i++)
867 char addr_buf[16];
869 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
870 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
871 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
872 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
874 sa = (struct sockaddr_in *)ua->Address.lpSockaddr;
875 sa->sin_family = AF_INET;
876 sa->sin_addr.s_addr = v4addrs[i];
877 sa->sin_port = 0;
878 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
879 debugstr_ipv4(&sa->sin_addr.s_addr, addr_buf));
881 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
882 if (i < num_v4addrs - 1)
884 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
885 ua = ua->Next;
889 if (num_v6addrs)
891 IP_ADAPTER_UNICAST_ADDRESS *ua;
892 struct WS_sockaddr_in6 *sa;
894 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
895 if (aa->FirstUnicastAddress)
897 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
899 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
900 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
902 else
903 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
904 for (i = 0; i < num_v6addrs; i++)
906 char addr_buf[46];
908 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
909 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
910 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
911 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
913 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
914 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
915 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
916 debugstr_ipv6(sa, addr_buf));
918 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
919 if (i < num_v6addrs - 1)
921 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
922 ua = ua->Next;
927 buflen = MAX_INTERFACE_PHYSADDR;
928 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
929 aa->PhysicalAddressLength = buflen;
930 aa->IfType = typeFromMibType(type);
931 aa->ConnectionType = connectionTypeFromMibType(type);
933 getInterfaceMtuByName(name, &aa->Mtu);
935 getInterfaceStatusByName(name, &status);
936 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
937 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
938 else aa->OperStatus = IfOperStatusUnknown;
940 *size = total_size;
941 HeapFree(GetProcessHeap(), 0, routeTable);
942 HeapFree(GetProcessHeap(), 0, v6addrs);
943 HeapFree(GetProcessHeap(), 0, v4addrs);
944 return ERROR_SUCCESS;
947 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
949 DWORD size;
951 initialise_resolver();
952 /* FIXME: no support for IPv6 DNS server addresses. Doing so requires
953 * sizeof SOCKADDR_STORAGE instead, and using _res._u._ext.nsaddrs when
954 * available.
956 size = _res.nscount * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
957 if (!address || *len < size)
959 *len = size;
960 return ERROR_BUFFER_OVERFLOW;
962 *len = size;
963 if (_res.nscount > 0)
965 PIP_ADAPTER_DNS_SERVER_ADDRESS addr;
966 int i;
968 for (i = 0, addr = address; i < _res.nscount && addr;
969 i++, addr = addr->Next)
971 SOCKADDR_IN *sin;
973 addr->Address.iSockaddrLength = sizeof(SOCKADDR);
974 addr->Address.lpSockaddr =
975 (LPSOCKADDR)((PBYTE)addr + sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS));
976 sin = (SOCKADDR_IN *)addr->Address.lpSockaddr;
977 sin->sin_family = WS_AF_INET;
978 sin->sin_port = _res.nsaddr_list[i].sin_port;
979 memcpy(&sin->sin_addr, &_res.nsaddr_list[i].sin_addr, sizeof(sin->sin_addr));
980 if (i == _res.nscount - 1)
981 addr->Next = NULL;
982 else
983 addr->Next =
984 (PIP_ADAPTER_DNS_SERVER_ADDRESS)((PBYTE)addr +
985 sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR));
988 return ERROR_SUCCESS;
991 static BOOL is_ip_address_string(const char *str)
993 struct in_addr in;
994 int ret;
996 ret = inet_aton(str, &in);
997 return ret != 0;
1000 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1002 ULONG size, i;
1003 char *found_suffix = NULL;
1005 initialise_resolver();
1006 /* Always return a NULL-terminated string, even if it's empty. */
1007 size = sizeof(WCHAR);
1008 for (i = 0, found_suffix = NULL;
1009 !found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1011 /* This uses a heuristic to select a DNS suffix:
1012 * the first, non-IP address string is selected.
1014 if (!is_ip_address_string(_res.dnsrch[i]))
1015 found_suffix = _res.dnsrch[i];
1017 if (found_suffix)
1018 size += strlen(found_suffix) * sizeof(WCHAR);
1019 if (!suffix || *len < size)
1021 *len = size;
1022 return ERROR_BUFFER_OVERFLOW;
1024 *len = size;
1025 if (found_suffix)
1027 char *p;
1029 for (p = found_suffix; *p; p++)
1030 *suffix++ = *p;
1032 *suffix = 0;
1033 return ERROR_SUCCESS;
1036 ULONG WINAPI GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1037 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1039 InterfaceIndexTable *table;
1040 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1042 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1044 if (!buflen) return ERROR_INVALID_PARAMETER;
1046 table = getInterfaceIndexTable();
1047 if (!table || !table->numIndexes)
1049 HeapFree(GetProcessHeap(), 0, table);
1050 return ERROR_NO_DATA;
1052 total_size = 0;
1053 for (i = 0; i < table->numIndexes; i++)
1055 size = 0;
1056 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1058 HeapFree(GetProcessHeap(), 0, table);
1059 return ret;
1061 total_size += size;
1063 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1065 /* Since DNS servers aren't really per adapter, get enough space for a
1066 * single copy of them.
1068 get_dns_server_addresses(NULL, &dns_server_size);
1069 total_size += dns_server_size;
1071 /* Since DNS suffix also isn't really per adapter, get enough space for a
1072 * single copy of it.
1074 get_dns_suffix(NULL, &dns_suffix_size);
1075 total_size += dns_suffix_size;
1076 if (aa && *buflen >= total_size)
1078 ULONG bytes_left = size = total_size;
1079 PIP_ADAPTER_ADDRESSES first_aa = aa;
1080 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1081 WCHAR *dnsSuffix;
1083 for (i = 0; i < table->numIndexes; i++)
1085 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1087 HeapFree(GetProcessHeap(), 0, table);
1088 return ret;
1090 if (i < table->numIndexes - 1)
1092 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1093 aa = aa->Next;
1094 size = bytes_left -= size;
1097 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1099 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1100 get_dns_server_addresses(firstDns, &dns_server_size);
1101 for (aa = first_aa; aa; aa = aa->Next)
1103 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1104 aa->FirstDnsServerAddress = firstDns;
1107 aa = first_aa;
1108 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1109 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1110 for (; aa; aa = aa->Next)
1112 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1113 aa->DnsSuffix = dnsSuffix;
1114 else
1115 aa->DnsSuffix = (WCHAR *)((BYTE*)dnsSuffix + dns_suffix_size - 2);
1117 ret = ERROR_SUCCESS;
1119 else
1120 ret = ERROR_BUFFER_OVERFLOW;
1121 *buflen = total_size;
1123 TRACE("num adapters %u\n", table->numIndexes);
1124 HeapFree(GetProcessHeap(), 0, table);
1125 return ret;
1128 /******************************************************************
1129 * GetBestInterface (IPHLPAPI.@)
1131 * Get the interface, with the best route for the given IP address.
1133 * PARAMS
1134 * dwDestAddr [In] IP address to search the interface for
1135 * pdwBestIfIndex [Out] found best interface
1137 * RETURNS
1138 * Success: NO_ERROR
1139 * Failure: error code from winerror.h
1141 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1143 struct WS_sockaddr_in sa_in;
1144 memset(&sa_in, 0, sizeof(sa_in));
1145 sa_in.sin_family = AF_INET;
1146 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1147 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1150 /******************************************************************
1151 * GetBestInterfaceEx (IPHLPAPI.@)
1153 * Get the interface, with the best route for the given IP address.
1155 * PARAMS
1156 * dwDestAddr [In] IP address to search the interface for
1157 * pdwBestIfIndex [Out] found best interface
1159 * RETURNS
1160 * Success: NO_ERROR
1161 * Failure: error code from winerror.h
1163 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1165 DWORD ret;
1167 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1168 if (!pDestAddr || !pdwBestIfIndex)
1169 ret = ERROR_INVALID_PARAMETER;
1170 else {
1171 MIB_IPFORWARDROW ipRow;
1173 if (pDestAddr->sa_family == AF_INET) {
1174 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1175 if (ret == ERROR_SUCCESS)
1176 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1177 } else {
1178 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1179 ret = ERROR_NOT_SUPPORTED;
1182 TRACE("returning %d\n", ret);
1183 return ret;
1187 /******************************************************************
1188 * GetBestRoute (IPHLPAPI.@)
1190 * Get the best route for the given IP address.
1192 * PARAMS
1193 * dwDestAddr [In] IP address to search the best route for
1194 * dwSourceAddr [In] optional source IP address
1195 * pBestRoute [Out] found best route
1197 * RETURNS
1198 * Success: NO_ERROR
1199 * Failure: error code from winerror.h
1201 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1203 PMIB_IPFORWARDTABLE table;
1204 DWORD ret;
1206 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1207 dwSourceAddr, pBestRoute);
1208 if (!pBestRoute)
1209 return ERROR_INVALID_PARAMETER;
1211 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1212 if (!ret) {
1213 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1215 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1216 if (table->table[ndx].dwForwardType != MIB_IPROUTE_TYPE_INVALID &&
1217 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1218 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1219 DWORD numShifts, mask;
1221 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1222 mask && mask & 1; mask >>= 1, numShifts++)
1224 if (numShifts > matchedBits) {
1225 matchedBits = numShifts;
1226 matchedNdx = ndx;
1228 else if (!matchedBits) {
1229 matchedNdx = ndx;
1233 if (matchedNdx < table->dwNumEntries) {
1234 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1235 ret = ERROR_SUCCESS;
1237 else {
1238 /* No route matches, which can happen if there's no default route. */
1239 ret = ERROR_HOST_UNREACHABLE;
1241 HeapFree(GetProcessHeap(), 0, table);
1243 TRACE("returning %d\n", ret);
1244 return ret;
1248 /******************************************************************
1249 * GetFriendlyIfIndex (IPHLPAPI.@)
1251 * Get a "friendly" version of IfIndex, which is one that doesn't
1252 * have the top byte set. Doesn't validate whether IfIndex is a valid
1253 * adapter index.
1255 * PARAMS
1256 * IfIndex [In] interface index to get the friendly one for
1258 * RETURNS
1259 * A friendly version of IfIndex.
1261 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1263 /* windows doesn't validate these, either, just makes sure the top byte is
1264 cleared. I assume my ifenum module never gives an index with the top
1265 byte set. */
1266 TRACE("returning %d\n", IfIndex);
1267 return IfIndex;
1271 /******************************************************************
1272 * GetIfEntry (IPHLPAPI.@)
1274 * Get information about an interface.
1276 * PARAMS
1277 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1278 * Out: interface information
1280 * RETURNS
1281 * Success: NO_ERROR
1282 * Failure: error code from winerror.h
1284 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1286 DWORD ret;
1287 char nameBuf[MAX_ADAPTER_NAME];
1288 char *name;
1290 TRACE("pIfRow %p\n", pIfRow);
1291 if (!pIfRow)
1292 return ERROR_INVALID_PARAMETER;
1294 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1295 if (name) {
1296 ret = getInterfaceEntryByName(name, pIfRow);
1297 if (ret == NO_ERROR)
1298 ret = getInterfaceStatsByName(name, pIfRow);
1300 else
1301 ret = ERROR_INVALID_DATA;
1302 TRACE("returning %d\n", ret);
1303 return ret;
1307 static int IfTableSorter(const void *a, const void *b)
1309 int ret;
1311 if (a && b)
1312 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1313 else
1314 ret = 0;
1315 return ret;
1319 /******************************************************************
1320 * GetIfTable (IPHLPAPI.@)
1322 * Get a table of local interfaces.
1324 * PARAMS
1325 * pIfTable [Out] buffer for local interfaces table
1326 * pdwSize [In/Out] length of output buffer
1327 * bOrder [In] whether to sort the table
1329 * RETURNS
1330 * Success: NO_ERROR
1331 * Failure: error code from winerror.h
1333 * NOTES
1334 * If pdwSize is less than required, the function will return
1335 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1336 * size.
1337 * If bOrder is true, the returned table will be sorted by interface index.
1339 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1341 DWORD ret;
1343 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1344 (DWORD)bOrder);
1345 if (!pdwSize)
1346 ret = ERROR_INVALID_PARAMETER;
1347 else {
1348 DWORD numInterfaces = getNumInterfaces();
1349 ULONG size = sizeof(MIB_IFTABLE);
1351 if (numInterfaces > 1)
1352 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1353 if (!pIfTable || *pdwSize < size) {
1354 *pdwSize = size;
1355 ret = ERROR_INSUFFICIENT_BUFFER;
1357 else {
1358 InterfaceIndexTable *table = getInterfaceIndexTable();
1360 if (table) {
1361 size = sizeof(MIB_IFTABLE);
1362 if (table->numIndexes > 1)
1363 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1364 if (*pdwSize < size) {
1365 *pdwSize = size;
1366 ret = ERROR_INSUFFICIENT_BUFFER;
1368 else {
1369 DWORD ndx;
1371 *pdwSize = size;
1372 pIfTable->dwNumEntries = 0;
1373 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1374 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1375 GetIfEntry(&pIfTable->table[ndx]);
1376 pIfTable->dwNumEntries++;
1378 if (bOrder)
1379 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1380 IfTableSorter);
1381 ret = NO_ERROR;
1383 HeapFree(GetProcessHeap(), 0, table);
1385 else
1386 ret = ERROR_OUTOFMEMORY;
1389 TRACE("returning %d\n", ret);
1390 return ret;
1394 /******************************************************************
1395 * GetInterfaceInfo (IPHLPAPI.@)
1397 * Get a list of network interface adapters.
1399 * PARAMS
1400 * pIfTable [Out] buffer for interface adapters
1401 * dwOutBufLen [Out] if buffer is too small, returns required size
1403 * RETURNS
1404 * Success: NO_ERROR
1405 * Failure: error code from winerror.h
1407 * BUGS
1408 * MSDN states this should return non-loopback interfaces only.
1410 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1412 DWORD ret;
1414 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1415 if (!dwOutBufLen)
1416 ret = ERROR_INVALID_PARAMETER;
1417 else {
1418 DWORD numInterfaces = getNumInterfaces();
1419 ULONG size = sizeof(IP_INTERFACE_INFO);
1421 if (numInterfaces > 1)
1422 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1423 if (!pIfTable || *dwOutBufLen < size) {
1424 *dwOutBufLen = size;
1425 ret = ERROR_INSUFFICIENT_BUFFER;
1427 else {
1428 InterfaceIndexTable *table = getInterfaceIndexTable();
1430 if (table) {
1431 size = sizeof(IP_INTERFACE_INFO);
1432 if (table->numIndexes > 1)
1433 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1434 if (*dwOutBufLen < size) {
1435 *dwOutBufLen = size;
1436 ret = ERROR_INSUFFICIENT_BUFFER;
1438 else {
1439 DWORD ndx;
1440 char nameBuf[MAX_ADAPTER_NAME];
1442 *dwOutBufLen = size;
1443 pIfTable->NumAdapters = 0;
1444 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1445 const char *walker, *name;
1446 WCHAR *assigner;
1448 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1449 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1450 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1451 walker && *walker &&
1452 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1453 walker++, assigner++)
1454 *assigner = *walker;
1455 *assigner = 0;
1456 pIfTable->NumAdapters++;
1458 ret = NO_ERROR;
1460 HeapFree(GetProcessHeap(), 0, table);
1462 else
1463 ret = ERROR_OUTOFMEMORY;
1466 TRACE("returning %d\n", ret);
1467 return ret;
1471 /******************************************************************
1472 * GetIpAddrTable (IPHLPAPI.@)
1474 * Get interface-to-IP address mapping table.
1476 * PARAMS
1477 * pIpAddrTable [Out] buffer for mapping table
1478 * pdwSize [In/Out] length of output buffer
1479 * bOrder [In] whether to sort the table
1481 * RETURNS
1482 * Success: NO_ERROR
1483 * Failure: error code from winerror.h
1485 * NOTES
1486 * If pdwSize is less than required, the function will return
1487 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1488 * size.
1489 * If bOrder is true, the returned table will be sorted by the next hop and
1490 * an assortment of arbitrary parameters.
1492 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1494 DWORD ret;
1496 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1497 (DWORD)bOrder);
1498 if (!pdwSize)
1499 ret = ERROR_INVALID_PARAMETER;
1500 else {
1501 PMIB_IPADDRTABLE table;
1503 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1504 if (ret == NO_ERROR)
1506 ULONG size = sizeof(MIB_IPADDRTABLE);
1508 if (table->dwNumEntries > 1)
1509 size += (table->dwNumEntries - 1) * sizeof(MIB_IPADDRROW);
1510 if (!pIpAddrTable || *pdwSize < size) {
1511 *pdwSize = size;
1512 ret = ERROR_INSUFFICIENT_BUFFER;
1514 else {
1515 *pdwSize = size;
1516 memcpy(pIpAddrTable, table, size);
1517 if (bOrder)
1518 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1519 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1520 ret = NO_ERROR;
1522 HeapFree(GetProcessHeap(), 0, table);
1525 TRACE("returning %d\n", ret);
1526 return ret;
1530 /******************************************************************
1531 * GetIpForwardTable (IPHLPAPI.@)
1533 * Get the route table.
1535 * PARAMS
1536 * pIpForwardTable [Out] buffer for route table
1537 * pdwSize [In/Out] length of output buffer
1538 * bOrder [In] whether to sort the table
1540 * RETURNS
1541 * Success: NO_ERROR
1542 * Failure: error code from winerror.h
1544 * NOTES
1545 * If pdwSize is less than required, the function will return
1546 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1547 * size.
1548 * If bOrder is true, the returned table will be sorted by the next hop and
1549 * an assortment of arbitrary parameters.
1551 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1553 DWORD ret;
1554 PMIB_IPFORWARDTABLE table;
1556 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1558 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1560 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1561 if (!ret) {
1562 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1563 if (!pIpForwardTable || *pdwSize < size) {
1564 *pdwSize = size;
1565 ret = ERROR_INSUFFICIENT_BUFFER;
1567 else {
1568 *pdwSize = size;
1569 memcpy(pIpForwardTable, table, size);
1571 HeapFree(GetProcessHeap(), 0, table);
1573 TRACE("returning %d\n", ret);
1574 return ret;
1578 /******************************************************************
1579 * GetIpNetTable (IPHLPAPI.@)
1581 * Get the IP-to-physical address mapping table.
1583 * PARAMS
1584 * pIpNetTable [Out] buffer for mapping table
1585 * pdwSize [In/Out] length of output buffer
1586 * bOrder [In] whether to sort the table
1588 * RETURNS
1589 * Success: NO_ERROR
1590 * Failure: error code from winerror.h
1592 * NOTES
1593 * If pdwSize is less than required, the function will return
1594 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1595 * size.
1596 * If bOrder is true, the returned table will be sorted by IP address.
1598 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1600 DWORD ret;
1601 PMIB_IPNETTABLE table;
1603 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1605 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1607 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1608 if (!ret) {
1609 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1610 if (!pIpNetTable || *pdwSize < size) {
1611 *pdwSize = size;
1612 ret = ERROR_INSUFFICIENT_BUFFER;
1614 else {
1615 *pdwSize = size;
1616 memcpy(pIpNetTable, table, size);
1618 HeapFree(GetProcessHeap(), 0, table);
1620 TRACE("returning %d\n", ret);
1621 return ret;
1624 /* Gets the DNS server list into the list beginning at list. Assumes that
1625 * a single server address may be placed at list if *len is at least
1626 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1627 * and assumes that all remaining DNS servers are contiguously located
1628 * beginning at firstDynamic. On input, *len is assumed to be the total number
1629 * of bytes available for all DNS servers, and is ignored if list is NULL.
1630 * On return, *len is set to the total number of bytes required for all DNS
1631 * servers.
1632 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1633 * ERROR_SUCCESS otherwise.
1635 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1636 PIP_ADDR_STRING firstDynamic, DWORD *len)
1638 DWORD size;
1640 initialise_resolver();
1641 size = _res.nscount * sizeof(IP_ADDR_STRING);
1642 if (!list || *len < size) {
1643 *len = size;
1644 return ERROR_BUFFER_OVERFLOW;
1646 *len = size;
1647 if (_res.nscount > 0) {
1648 PIP_ADDR_STRING ptr;
1649 int i;
1651 for (i = 0, ptr = list; i < _res.nscount && ptr; i++, ptr = ptr->Next) {
1652 toIPAddressString(_res.nsaddr_list[i].sin_addr.s_addr,
1653 ptr->IpAddress.String);
1654 if (i == _res.nscount - 1)
1655 ptr->Next = NULL;
1656 else if (i == 0)
1657 ptr->Next = firstDynamic;
1658 else
1659 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1662 return ERROR_SUCCESS;
1665 /******************************************************************
1666 * GetNetworkParams (IPHLPAPI.@)
1668 * Get the network parameters for the local computer.
1670 * PARAMS
1671 * pFixedInfo [Out] buffer for network parameters
1672 * pOutBufLen [In/Out] length of output buffer
1674 * RETURNS
1675 * Success: NO_ERROR
1676 * Failure: error code from winerror.h
1678 * NOTES
1679 * If pOutBufLen is less than required, the function will return
1680 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1681 * size.
1683 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1685 DWORD ret, size, serverListSize;
1686 LONG regReturn;
1687 HKEY hKey;
1689 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1690 if (!pOutBufLen)
1691 return ERROR_INVALID_PARAMETER;
1693 get_dns_server_list(NULL, NULL, &serverListSize);
1694 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1695 if (!pFixedInfo || *pOutBufLen < size) {
1696 *pOutBufLen = size;
1697 return ERROR_BUFFER_OVERFLOW;
1700 memset(pFixedInfo, 0, size);
1701 size = sizeof(pFixedInfo->HostName);
1702 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1703 size = sizeof(pFixedInfo->DomainName);
1704 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1705 get_dns_server_list(&pFixedInfo->DnsServerList,
1706 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1707 &serverListSize);
1708 /* Assume the first DNS server in the list is the "current" DNS server: */
1709 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1710 pFixedInfo->NodeType = HYBRID_NODETYPE;
1711 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1712 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1713 if (regReturn != ERROR_SUCCESS)
1714 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1715 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1716 &hKey);
1717 if (regReturn == ERROR_SUCCESS)
1719 DWORD size = sizeof(pFixedInfo->ScopeId);
1721 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1722 RegCloseKey(hKey);
1725 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1726 I suppose could also check for a listener on port 53 to set EnableDns */
1727 ret = NO_ERROR;
1728 TRACE("returning %d\n", ret);
1729 return ret;
1733 /******************************************************************
1734 * GetNumberOfInterfaces (IPHLPAPI.@)
1736 * Get the number of interfaces.
1738 * PARAMS
1739 * pdwNumIf [Out] number of interfaces
1741 * RETURNS
1742 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1744 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1746 DWORD ret;
1748 TRACE("pdwNumIf %p\n", pdwNumIf);
1749 if (!pdwNumIf)
1750 ret = ERROR_INVALID_PARAMETER;
1751 else {
1752 *pdwNumIf = getNumInterfaces();
1753 ret = NO_ERROR;
1755 TRACE("returning %d\n", ret);
1756 return ret;
1760 /******************************************************************
1761 * GetPerAdapterInfo (IPHLPAPI.@)
1763 * Get information about an adapter corresponding to an interface.
1765 * PARAMS
1766 * IfIndex [In] interface info
1767 * pPerAdapterInfo [Out] buffer for per adapter info
1768 * pOutBufLen [In/Out] length of output buffer
1770 * RETURNS
1771 * Success: NO_ERROR
1772 * Failure: error code from winerror.h
1774 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1776 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1777 DWORD ret = NO_ERROR;
1779 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1781 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1783 if (!isIfIndexLoopback(IfIndex)) {
1784 get_dns_server_list(NULL, NULL, &serverListSize);
1785 if (serverListSize > sizeof(IP_ADDR_STRING))
1786 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1788 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1790 *pOutBufLen = bytesNeeded;
1791 return ERROR_BUFFER_OVERFLOW;
1794 memset(pPerAdapterInfo, 0, bytesNeeded);
1795 if (!isIfIndexLoopback(IfIndex)) {
1796 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1797 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1798 &serverListSize);
1799 /* Assume the first DNS server in the list is the "current" DNS server: */
1800 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1802 return ret;
1806 /******************************************************************
1807 * GetRTTAndHopCount (IPHLPAPI.@)
1809 * Get round-trip time (RTT) and hop count.
1811 * PARAMS
1813 * DestIpAddress [In] destination address to get the info for
1814 * HopCount [Out] retrieved hop count
1815 * MaxHops [In] maximum hops to search for the destination
1816 * RTT [Out] RTT in milliseconds
1818 * RETURNS
1819 * Success: TRUE
1820 * Failure: FALSE
1822 * FIXME
1823 * Stub, returns FALSE.
1825 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1827 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1828 DestIpAddress, HopCount, MaxHops, RTT);
1829 return FALSE;
1833 /******************************************************************
1834 * GetTcpTable (IPHLPAPI.@)
1836 * Get the table of active TCP connections.
1838 * PARAMS
1839 * pTcpTable [Out] buffer for TCP connections table
1840 * pdwSize [In/Out] length of output buffer
1841 * bOrder [In] whether to order the table
1843 * RETURNS
1844 * Success: NO_ERROR
1845 * Failure: error code from winerror.h
1847 * NOTES
1848 * If pdwSize is less than required, the function will return
1849 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1850 * the required byte size.
1851 * If bOrder is true, the returned table will be sorted, first by
1852 * local address and port number, then by remote address and port
1853 * number.
1855 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1857 DWORD ret;
1858 PMIB_TCPTABLE table;
1860 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1862 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1864 ret = AllocateAndGetTcpTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1865 if (!ret) {
1866 DWORD size = FIELD_OFFSET( MIB_TCPTABLE, table[table->dwNumEntries] );
1867 if (!pTcpTable || *pdwSize < size) {
1868 *pdwSize = size;
1869 ret = ERROR_INSUFFICIENT_BUFFER;
1871 else {
1872 *pdwSize = size;
1873 memcpy(pTcpTable, table, size);
1875 HeapFree(GetProcessHeap(), 0, table);
1877 TRACE("returning %d\n", ret);
1878 return ret;
1882 /******************************************************************
1883 * GetUdpTable (IPHLPAPI.@)
1885 * Get a table of active UDP connections.
1887 * PARAMS
1888 * pUdpTable [Out] buffer for UDP connections table
1889 * pdwSize [In/Out] length of output buffer
1890 * bOrder [In] whether to order the table
1892 * RETURNS
1893 * Success: NO_ERROR
1894 * Failure: error code from winerror.h
1896 * NOTES
1897 * If pdwSize is less than required, the function will return
1898 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
1899 * required byte size.
1900 * If bOrder is true, the returned table will be sorted, first by
1901 * local address, then by local port number.
1903 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
1905 DWORD ret;
1906 PMIB_UDPTABLE table;
1908 TRACE("pUdpTable %p, pdwSize %p, bOrder %d\n", pUdpTable, pdwSize, bOrder);
1910 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1912 ret = AllocateAndGetUdpTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1913 if (!ret) {
1914 DWORD size = FIELD_OFFSET( MIB_UDPTABLE, table[table->dwNumEntries] );
1915 if (!pUdpTable || *pdwSize < size) {
1916 *pdwSize = size;
1917 ret = ERROR_INSUFFICIENT_BUFFER;
1919 else {
1920 *pdwSize = size;
1921 memcpy(pUdpTable, table, size);
1923 HeapFree(GetProcessHeap(), 0, table);
1925 TRACE("returning %d\n", ret);
1926 return ret;
1930 /******************************************************************
1931 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
1933 * This is a Win98-only function to get information on "unidirectional"
1934 * adapters. Since this is pretty nonsensical in other contexts, it
1935 * never returns anything.
1937 * PARAMS
1938 * pIPIfInfo [Out] buffer for adapter infos
1939 * dwOutBufLen [Out] length of the output buffer
1941 * RETURNS
1942 * Success: NO_ERROR
1943 * Failure: error code from winerror.h
1945 * FIXME
1946 * Stub, returns ERROR_NOT_SUPPORTED.
1948 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
1950 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
1951 /* a unidirectional adapter?? not bloody likely! */
1952 return ERROR_NOT_SUPPORTED;
1956 /******************************************************************
1957 * IpReleaseAddress (IPHLPAPI.@)
1959 * Release an IP obtained through DHCP,
1961 * PARAMS
1962 * AdapterInfo [In] adapter to release IP address
1964 * RETURNS
1965 * Success: NO_ERROR
1966 * Failure: error code from winerror.h
1968 * NOTES
1969 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1970 * this function does nothing.
1972 * FIXME
1973 * Stub, returns ERROR_NOT_SUPPORTED.
1975 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
1977 TRACE("AdapterInfo %p\n", AdapterInfo);
1978 /* not a stub, never going to support this (and I never mark an adapter as
1979 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
1980 return ERROR_NOT_SUPPORTED;
1984 /******************************************************************
1985 * IpRenewAddress (IPHLPAPI.@)
1987 * Renew an IP obtained through DHCP.
1989 * PARAMS
1990 * AdapterInfo [In] adapter to renew IP address
1992 * RETURNS
1993 * Success: NO_ERROR
1994 * Failure: error code from winerror.h
1996 * NOTES
1997 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
1998 * this function does nothing.
2000 * FIXME
2001 * Stub, returns ERROR_NOT_SUPPORTED.
2003 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2005 TRACE("AdapterInfo %p\n", AdapterInfo);
2006 /* not a stub, never going to support this (and I never mark an adapter as
2007 DHCP enabled, see GetAdaptersInfo, so this should never get called) */
2008 return ERROR_NOT_SUPPORTED;
2012 /******************************************************************
2013 * NotifyAddrChange (IPHLPAPI.@)
2015 * Notify caller whenever the ip-interface map 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 NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2030 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2031 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2032 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2033 return ERROR_IO_PENDING;
2037 /******************************************************************
2038 * NotifyRouteChange (IPHLPAPI.@)
2040 * Notify caller whenever the ip routing table is changed.
2042 * PARAMS
2043 * Handle [Out] handle usable in asynchronous notification
2044 * overlapped [In] overlapped structure that notifies the caller
2046 * RETURNS
2047 * Success: NO_ERROR
2048 * Failure: error code from winerror.h
2050 * FIXME
2051 * Stub, returns ERROR_NOT_SUPPORTED.
2053 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2055 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2056 return ERROR_NOT_SUPPORTED;
2060 /******************************************************************
2061 * SendARP (IPHLPAPI.@)
2063 * Send an ARP request.
2065 * PARAMS
2066 * DestIP [In] attempt to obtain this IP
2067 * SrcIP [In] optional sender IP address
2068 * pMacAddr [Out] buffer for the mac address
2069 * PhyAddrLen [In/Out] length of the output buffer
2071 * RETURNS
2072 * Success: NO_ERROR
2073 * Failure: error code from winerror.h
2075 * FIXME
2076 * Stub, returns ERROR_NOT_SUPPORTED.
2078 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2080 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2081 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2082 return ERROR_NOT_SUPPORTED;
2086 /******************************************************************
2087 * SetIfEntry (IPHLPAPI.@)
2089 * Set the administrative status of an interface.
2091 * PARAMS
2092 * pIfRow [In] dwAdminStatus member specifies the new status.
2094 * RETURNS
2095 * Success: NO_ERROR
2096 * Failure: error code from winerror.h
2098 * FIXME
2099 * Stub, returns ERROR_NOT_SUPPORTED.
2101 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2103 FIXME("(pIfRow %p): stub\n", pIfRow);
2104 /* this is supposed to set an interface administratively up or down.
2105 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2106 this sort of down is indistinguishable from other sorts of down (e.g. no
2107 link). */
2108 return ERROR_NOT_SUPPORTED;
2112 /******************************************************************
2113 * SetIpForwardEntry (IPHLPAPI.@)
2115 * Modify an existing route.
2117 * PARAMS
2118 * pRoute [In] route 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 SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2129 FIXME("(pRoute %p): stub\n", pRoute);
2130 /* this is to add a route entry, how's it distinguishable from
2131 CreateIpForwardEntry?
2132 could use SIOCADDRT, not sure I want to */
2133 return 0;
2137 /******************************************************************
2138 * SetIpNetEntry (IPHLPAPI.@)
2140 * Modify an existing ARP entry.
2142 * PARAMS
2143 * pArpEntry [In] ARP entry with the new information
2145 * RETURNS
2146 * Success: NO_ERROR
2147 * Failure: error code from winerror.h
2149 * FIXME
2150 * Stub, returns NO_ERROR.
2152 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2154 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2155 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2156 return 0;
2160 /******************************************************************
2161 * SetIpStatistics (IPHLPAPI.@)
2163 * Toggle IP forwarding and det the default TTL value.
2165 * PARAMS
2166 * pIpStats [In] IP statistics with the new information
2168 * RETURNS
2169 * Success: NO_ERROR
2170 * Failure: error code from winerror.h
2172 * FIXME
2173 * Stub, returns NO_ERROR.
2175 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2177 FIXME("(pIpStats %p): stub\n", pIpStats);
2178 return 0;
2182 /******************************************************************
2183 * SetIpTTL (IPHLPAPI.@)
2185 * Set the default TTL value.
2187 * PARAMS
2188 * nTTL [In] new TTL value
2190 * RETURNS
2191 * Success: NO_ERROR
2192 * Failure: error code from winerror.h
2194 * FIXME
2195 * Stub, returns NO_ERROR.
2197 DWORD WINAPI SetIpTTL(UINT nTTL)
2199 FIXME("(nTTL %d): stub\n", nTTL);
2200 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2201 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2202 return 0;
2206 /******************************************************************
2207 * SetTcpEntry (IPHLPAPI.@)
2209 * Set the state of a TCP connection.
2211 * PARAMS
2212 * pTcpRow [In] specifies connection with new state
2214 * RETURNS
2215 * Success: NO_ERROR
2216 * Failure: error code from winerror.h
2218 * FIXME
2219 * Stub, returns NO_ERROR.
2221 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2223 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2224 return 0;
2228 /******************************************************************
2229 * UnenableRouter (IPHLPAPI.@)
2231 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2232 * if it reaches zero.
2234 * PARAMS
2235 * pOverlapped [In/Out] should be the same as in EnableRouter()
2236 * lpdwEnableCount [Out] optional, receives reference count
2238 * RETURNS
2239 * Success: NO_ERROR
2240 * Failure: error code from winerror.h
2242 * FIXME
2243 * Stub, returns ERROR_NOT_SUPPORTED.
2245 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2247 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2248 lpdwEnableCount);
2249 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2250 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2252 return ERROR_NOT_SUPPORTED;
2255 /******************************************************************
2256 * PfCreateInterface (IPHLPAPI.@)
2258 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2259 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2261 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2262 return ERROR_CALL_NOT_IMPLEMENTED;