mshtml: Added separated structure to store request data.
[wine/multimedia.git] / dlls / iphlpapi / iphlpapi_main.c
blob9d23c82d5fb0f5f94fd8a2a8ae15a85368887da3
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 /******************************************************************
67 * AddIPAddress (IPHLPAPI.@)
69 * Add an IP address to an adapter.
71 * PARAMS
72 * Address [In] IP address to add to the adapter
73 * IpMask [In] subnet mask for the IP address
74 * IfIndex [In] adapter index to add the address
75 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
76 * NTEInstance [Out] NTE instance for the IP address
78 * RETURNS
79 * Success: NO_ERROR
80 * Failure: error code from winerror.h
82 * FIXME
83 * Stub. Currently returns ERROR_NOT_SUPPORTED.
85 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
87 FIXME(":stub\n");
88 return ERROR_NOT_SUPPORTED;
92 /******************************************************************
93 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
95 * Get table of local interfaces.
96 * Like GetIfTable(), but allocate the returned table from heap.
98 * PARAMS
99 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
100 * allocated and returned.
101 * bOrder [In] whether to sort the table
102 * heap [In] heap from which the table is allocated
103 * flags [In] flags to HeapAlloc
105 * RETURNS
106 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
107 * GetIfTable() returns otherwise.
109 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
110 BOOL bOrder, HANDLE heap, DWORD flags)
112 DWORD ret;
114 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
115 bOrder, heap, flags);
116 if (!ppIfTable)
117 ret = ERROR_INVALID_PARAMETER;
118 else {
119 DWORD dwSize = 0;
121 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
122 if (ret == ERROR_INSUFFICIENT_BUFFER) {
123 *ppIfTable = HeapAlloc(heap, flags, dwSize);
124 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
127 TRACE("returning %d\n", ret);
128 return ret;
132 static int IpAddrTableSorter(const void *a, const void *b)
134 int ret;
136 if (a && b)
137 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
138 else
139 ret = 0;
140 return ret;
144 /******************************************************************
145 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
147 * Get interface-to-IP address mapping table.
148 * Like GetIpAddrTable(), but allocate the returned table from heap.
150 * PARAMS
151 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
152 * allocated and returned.
153 * bOrder [In] whether to sort the table
154 * heap [In] heap from which the table is allocated
155 * flags [In] flags to HeapAlloc
157 * RETURNS
158 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
159 * failure, NO_ERROR on success.
161 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
162 BOOL bOrder, HANDLE heap, DWORD flags)
164 DWORD ret;
166 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
167 ppIpAddrTable, bOrder, heap, flags);
168 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
169 if (!ret && bOrder)
170 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
171 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
172 TRACE("returning %d\n", ret);
173 return ret;
177 /******************************************************************
178 * CancelIPChangeNotify (IPHLPAPI.@)
180 * Cancel a previous notification created by NotifyAddrChange or
181 * NotifyRouteChange.
183 * PARAMS
184 * overlapped [In] overlapped structure that notifies the caller
186 * RETURNS
187 * Success: TRUE
188 * Failure: FALSE
190 * FIXME
191 * Stub, returns FALSE.
193 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
195 FIXME("(overlapped %p): stub\n", overlapped);
196 return FALSE;
201 /******************************************************************
202 * CreateIpForwardEntry (IPHLPAPI.@)
204 * Create a route in the local computer's IP table.
206 * PARAMS
207 * pRoute [In] new route information
209 * RETURNS
210 * Success: NO_ERROR
211 * Failure: error code from winerror.h
213 * FIXME
214 * Stub, always returns NO_ERROR.
216 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
218 FIXME("(pRoute %p): stub\n", pRoute);
219 /* could use SIOCADDRT, not sure I want to */
220 return 0;
224 /******************************************************************
225 * CreateIpNetEntry (IPHLPAPI.@)
227 * Create entry in the ARP table.
229 * PARAMS
230 * pArpEntry [In] new ARP entry
232 * RETURNS
233 * Success: NO_ERROR
234 * Failure: error code from winerror.h
236 * FIXME
237 * Stub, always returns NO_ERROR.
239 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
241 FIXME("(pArpEntry %p)\n", pArpEntry);
242 /* could use SIOCSARP on systems that support it, not sure I want to */
243 return 0;
247 /******************************************************************
248 * CreateProxyArpEntry (IPHLPAPI.@)
250 * Create a Proxy ARP (PARP) entry for an IP address.
252 * PARAMS
253 * dwAddress [In] IP address for which this computer acts as a proxy.
254 * dwMask [In] subnet mask for dwAddress
255 * dwIfIndex [In] interface index
257 * RETURNS
258 * Success: NO_ERROR
259 * Failure: error code from winerror.h
261 * FIXME
262 * Stub, returns ERROR_NOT_SUPPORTED.
264 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
266 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
267 dwAddress, dwMask, dwIfIndex);
268 return ERROR_NOT_SUPPORTED;
272 /******************************************************************
273 * DeleteIPAddress (IPHLPAPI.@)
275 * Delete an IP address added with AddIPAddress().
277 * PARAMS
278 * NTEContext [In] NTE context from AddIPAddress();
280 * RETURNS
281 * Success: NO_ERROR
282 * Failure: error code from winerror.h
284 * FIXME
285 * Stub, returns ERROR_NOT_SUPPORTED.
287 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
289 FIXME("(NTEContext %d): stub\n", NTEContext);
290 return ERROR_NOT_SUPPORTED;
294 /******************************************************************
295 * DeleteIpForwardEntry (IPHLPAPI.@)
297 * Delete a route.
299 * PARAMS
300 * pRoute [In] route to delete
302 * RETURNS
303 * Success: NO_ERROR
304 * Failure: error code from winerror.h
306 * FIXME
307 * Stub, returns NO_ERROR.
309 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
311 FIXME("(pRoute %p): stub\n", pRoute);
312 /* could use SIOCDELRT, not sure I want to */
313 return 0;
317 /******************************************************************
318 * DeleteIpNetEntry (IPHLPAPI.@)
320 * Delete an ARP entry.
322 * PARAMS
323 * pArpEntry [In] ARP entry to delete
325 * RETURNS
326 * Success: NO_ERROR
327 * Failure: error code from winerror.h
329 * FIXME
330 * Stub, returns NO_ERROR.
332 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
334 FIXME("(pArpEntry %p): stub\n", pArpEntry);
335 /* could use SIOCDARP on systems that support it, not sure I want to */
336 return 0;
340 /******************************************************************
341 * DeleteProxyArpEntry (IPHLPAPI.@)
343 * Delete a Proxy ARP entry.
345 * PARAMS
346 * dwAddress [In] IP address for which this computer acts as a proxy.
347 * dwMask [In] subnet mask for dwAddress
348 * dwIfIndex [In] interface index
350 * RETURNS
351 * Success: NO_ERROR
352 * Failure: error code from winerror.h
354 * FIXME
355 * Stub, returns ERROR_NOT_SUPPORTED.
357 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
359 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
360 dwAddress, dwMask, dwIfIndex);
361 return ERROR_NOT_SUPPORTED;
365 /******************************************************************
366 * EnableRouter (IPHLPAPI.@)
368 * Turn on ip forwarding.
370 * PARAMS
371 * pHandle [In/Out]
372 * pOverlapped [In/Out] hEvent member should contain a valid handle.
374 * RETURNS
375 * Success: ERROR_IO_PENDING
376 * Failure: error code from winerror.h
378 * FIXME
379 * Stub, returns ERROR_NOT_SUPPORTED.
381 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
383 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
384 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
385 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
387 return ERROR_NOT_SUPPORTED;
391 /******************************************************************
392 * FlushIpNetTable (IPHLPAPI.@)
394 * Delete all ARP entries of an interface
396 * PARAMS
397 * dwIfIndex [In] interface index
399 * RETURNS
400 * Success: NO_ERROR
401 * Failure: error code from winerror.h
403 * FIXME
404 * Stub, returns ERROR_NOT_SUPPORTED.
406 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
408 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
409 /* this flushes the arp cache of the given index */
410 return ERROR_NOT_SUPPORTED;
414 /******************************************************************
415 * GetAdapterIndex (IPHLPAPI.@)
417 * Get interface index from its name.
419 * PARAMS
420 * AdapterName [In] unicode string with the adapter name
421 * IfIndex [Out] returns found interface index
423 * RETURNS
424 * Success: NO_ERROR
425 * Failure: error code from winerror.h
427 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
429 char adapterName[MAX_ADAPTER_NAME];
430 unsigned int i;
431 DWORD ret;
433 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
434 /* The adapter name is guaranteed not to have any unicode characters, so
435 * this translation is never lossy */
436 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
437 adapterName[i] = (char)AdapterName[i];
438 adapterName[i] = '\0';
439 ret = getInterfaceIndexByName(adapterName, IfIndex);
440 TRACE("returning %d\n", ret);
441 return ret;
445 /******************************************************************
446 * GetAdaptersInfo (IPHLPAPI.@)
448 * Get information about adapters.
450 * PARAMS
451 * pAdapterInfo [Out] buffer for adapter infos
452 * pOutBufLen [In] length of output buffer
454 * RETURNS
455 * Success: NO_ERROR
456 * Failure: error code from winerror.h
458 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
460 DWORD ret;
462 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
463 if (!pOutBufLen)
464 ret = ERROR_INVALID_PARAMETER;
465 else {
466 DWORD numNonLoopbackInterfaces = get_interface_indices( TRUE, NULL );
468 if (numNonLoopbackInterfaces > 0) {
469 DWORD numIPAddresses = getNumIPAddresses();
470 ULONG size;
472 /* This may slightly overestimate the amount of space needed, because
473 * the IP addresses include the loopback address, but it's easier
474 * to make sure there's more than enough space than to make sure there's
475 * precisely enough space.
477 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
478 size += numIPAddresses * sizeof(IP_ADDR_STRING);
479 if (!pAdapterInfo || *pOutBufLen < size) {
480 *pOutBufLen = size;
481 ret = ERROR_BUFFER_OVERFLOW;
483 else {
484 InterfaceIndexTable *table = NULL;
485 PMIB_IPADDRTABLE ipAddrTable = NULL;
486 PMIB_IPFORWARDTABLE routeTable = NULL;
488 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
489 if (!ret)
490 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
491 if (!ret)
492 get_interface_indices( TRUE, &table );
493 if (table) {
494 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
495 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
496 if (*pOutBufLen < size) {
497 *pOutBufLen = size;
498 ret = ERROR_INSUFFICIENT_BUFFER;
500 else {
501 DWORD ndx;
502 HKEY hKey;
503 BOOL winsEnabled = FALSE;
504 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
505 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
506 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
508 memset(pAdapterInfo, 0, size);
509 /* @@ Wine registry key: HKCU\Software\Wine\Network */
510 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
511 &hKey) == ERROR_SUCCESS) {
512 DWORD size = sizeof(primaryWINS.String);
513 unsigned long addr;
515 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
516 (LPBYTE)primaryWINS.String, &size);
517 addr = inet_addr(primaryWINS.String);
518 if (addr != INADDR_NONE && addr != INADDR_ANY)
519 winsEnabled = TRUE;
520 size = sizeof(secondaryWINS.String);
521 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
522 (LPBYTE)secondaryWINS.String, &size);
523 addr = inet_addr(secondaryWINS.String);
524 if (addr != INADDR_NONE && addr != INADDR_ANY)
525 winsEnabled = TRUE;
526 RegCloseKey(hKey);
528 for (ndx = 0; ndx < table->numIndexes; ndx++) {
529 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
530 DWORD i;
531 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
532 BOOL firstIPAddr = TRUE;
534 /* on Win98 this is left empty, but whatever */
535 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
536 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
537 ptr->AddressLength = sizeof(ptr->Address);
538 getInterfacePhysicalByIndex(table->indexes[ndx],
539 &ptr->AddressLength, ptr->Address, &ptr->Type);
540 ptr->Index = table->indexes[ndx];
541 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
542 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
543 if (firstIPAddr) {
544 toIPAddressString(ipAddrTable->table[i].dwAddr,
545 ptr->IpAddressList.IpAddress.String);
546 toIPAddressString(ipAddrTable->table[i].dwMask,
547 ptr->IpAddressList.IpMask.String);
548 firstIPAddr = FALSE;
550 else {
551 currentIPAddr->Next = nextIPAddr;
552 currentIPAddr = nextIPAddr;
553 toIPAddressString(ipAddrTable->table[i].dwAddr,
554 currentIPAddr->IpAddress.String);
555 toIPAddressString(ipAddrTable->table[i].dwMask,
556 currentIPAddr->IpMask.String);
557 nextIPAddr++;
561 /* Find first router through this interface, which we'll assume
562 * is the default gateway for this adapter */
563 for (i = 0; i < routeTable->dwNumEntries; i++)
564 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
565 && routeTable->table[i].u1.ForwardType ==
566 MIB_IPROUTE_TYPE_INDIRECT)
567 toIPAddressString(routeTable->table[i].dwForwardNextHop,
568 ptr->GatewayList.IpAddress.String);
569 if (winsEnabled) {
570 ptr->HaveWins = TRUE;
571 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
572 primaryWINS.String, sizeof(primaryWINS.String));
573 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
574 secondaryWINS.String, sizeof(secondaryWINS.String));
576 if (ndx < table->numIndexes - 1)
577 ptr->Next = &pAdapterInfo[ndx + 1];
578 else
579 ptr->Next = NULL;
581 ptr->DhcpEnabled = TRUE;
583 ret = NO_ERROR;
585 HeapFree(GetProcessHeap(), 0, table);
587 else
588 ret = ERROR_OUTOFMEMORY;
589 HeapFree(GetProcessHeap(), 0, routeTable);
590 HeapFree(GetProcessHeap(), 0, ipAddrTable);
593 else
594 ret = ERROR_NO_DATA;
596 TRACE("returning %d\n", ret);
597 return ret;
600 static DWORD typeFromMibType(DWORD mib_type)
602 switch (mib_type)
604 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
605 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
606 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
607 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
608 default: return IF_TYPE_OTHER;
612 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
614 switch (mib_type)
616 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
617 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
618 default: return NET_IF_CONNECTION_DEDICATED;
622 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs)
624 ULONG ret, i, j;
625 MIB_IPADDRTABLE *at;
627 *num_addrs = 0;
628 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
629 for (i = 0; i < at->dwNumEntries; i++)
631 if (at->table[i].dwIndex == index) (*num_addrs)++;
633 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
635 HeapFree(GetProcessHeap(), 0, at);
636 return ERROR_OUTOFMEMORY;
638 for (i = 0, j = 0; i < at->dwNumEntries; i++)
640 if (at->table[i].dwIndex == index) (*addrs)[j++] = at->table[i].dwAddr;
642 HeapFree(GetProcessHeap(), 0, at);
643 return ERROR_SUCCESS;
646 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
648 const BYTE *addrp;
649 char *p = buf;
651 for (addrp = (const BYTE *)in_addr;
652 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
653 addrp++)
655 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
656 sprintf(p, "%d", *addrp);
657 else
658 p += sprintf(p, "%d.", *addrp);
660 return buf;
663 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
665 const IN6_ADDR *addr = &sin->sin6_addr;
666 char *p = buf;
667 int i;
668 BOOL in_zero = FALSE;
670 for (i = 0; i < 7; i++)
672 if (!addr->u.Word[i])
674 if (i == 0)
675 *p++ = ':';
676 if (!in_zero)
678 *p++ = ':';
679 in_zero = TRUE;
682 else
684 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
685 in_zero = FALSE;
688 sprintf(p, "%x", ntohs(addr->u.Word[7]));
689 return buf;
692 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
694 DWORD i, num_gateways = 0;
696 for (i = 0; i < routeTable->dwNumEntries; i++)
698 if (routeTable->table[i].dwForwardIfIndex == index &&
699 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
700 num_gateways++;
702 return num_gateways;
705 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
706 PMIB_IPFORWARDTABLE routeTable)
708 DWORD i;
709 PMIB_IPFORWARDROW row = NULL;
711 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
713 if (routeTable->table[i].dwForwardIfIndex == index &&
714 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
715 row = &routeTable->table[i];
717 return row;
720 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
721 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
723 ULONG ret = ERROR_SUCCESS, i, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
724 DWORD *v4addrs = NULL;
725 SOCKET_ADDRESS *v6addrs = NULL;
726 PMIB_IPFORWARDTABLE routeTable = NULL;
728 if (family == WS_AF_INET)
730 if (!(flags & GAA_FLAG_SKIP_UNICAST))
731 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
732 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
734 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
735 GetProcessHeap(), 0);
736 if (!ret)
737 num_v4_gateways = count_v4_gateways(index, routeTable);
740 else if (family == WS_AF_INET6)
742 if (!(flags & GAA_FLAG_SKIP_UNICAST))
743 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
745 else if (family == WS_AF_UNSPEC)
747 if (!(flags & GAA_FLAG_SKIP_UNICAST))
748 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs);
749 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
751 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE,
752 GetProcessHeap(), 0);
753 if (!ret)
754 num_v4_gateways = count_v4_gateways(index, routeTable);
756 if (!ret && !(flags & GAA_FLAG_SKIP_UNICAST))
757 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs);
759 else
761 FIXME("address family %u unsupported\n", family);
762 ret = ERROR_NO_DATA;
764 if (ret)
766 HeapFree(GetProcessHeap(), 0, v4addrs);
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;
789 INTERNAL_IF_OPER_STATUS status;
791 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
792 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
793 aa->u.s.IfIndex = index;
795 getInterfaceNameByIndex(index, name);
796 memcpy(ptr, name, IF_NAMESIZE);
797 aa->AdapterName = ptr;
798 ptr += IF_NAMESIZE;
799 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
801 aa->FriendlyName = (WCHAR *)ptr;
802 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
803 *dst = *src;
804 *dst++ = 0;
805 ptr = (char *)dst;
807 aa->Description = (WCHAR *)ptr;
808 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
809 *dst = *src;
810 *dst++ = 0;
811 ptr = (char *)dst;
813 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
814 num_v6addrs);
815 if (num_v4_gateways)
817 PMIB_IPFORWARDROW adapterRow;
819 if ((adapterRow = findIPv4Gateway(index, routeTable)))
821 PIP_ADAPTER_GATEWAY_ADDRESS gw;
822 PSOCKADDR_IN sin;
824 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
825 aa->FirstGatewayAddress = gw;
827 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
828 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
829 sin = (PSOCKADDR_IN)ptr;
830 sin->sin_family = AF_INET;
831 sin->sin_port = 0;
832 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
833 sizeof(DWORD));
834 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
835 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
836 gw->Next = NULL;
837 ptr += sizeof(SOCKADDR_IN);
840 if (num_v4addrs)
842 IP_ADAPTER_UNICAST_ADDRESS *ua;
843 struct WS_sockaddr_in *sa;
844 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
845 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
846 for (i = 0; i < num_v4addrs; i++)
848 char addr_buf[16];
850 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
851 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
852 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
853 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
855 sa = (struct WS_sockaddr_in *)ua->Address.lpSockaddr;
856 sa->sin_family = WS_AF_INET;
857 sa->sin_addr.S_un.S_addr = v4addrs[i];
858 sa->sin_port = 0;
859 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
860 debugstr_ipv4(&sa->sin_addr.S_un.S_addr, addr_buf));
862 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
863 if (i < num_v4addrs - 1)
865 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
866 ua = ua->Next;
870 if (num_v6addrs)
872 IP_ADAPTER_UNICAST_ADDRESS *ua;
873 struct WS_sockaddr_in6 *sa;
875 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
876 if (aa->FirstUnicastAddress)
878 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
880 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
881 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
883 else
884 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
885 for (i = 0; i < num_v6addrs; i++)
887 char addr_buf[46];
889 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
890 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
891 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
892 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
894 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
895 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
896 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
897 debugstr_ipv6(sa, addr_buf));
899 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
900 if (i < num_v6addrs - 1)
902 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
903 ua = ua->Next;
908 buflen = MAX_INTERFACE_PHYSADDR;
909 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
910 aa->PhysicalAddressLength = buflen;
911 aa->IfType = typeFromMibType(type);
912 aa->ConnectionType = connectionTypeFromMibType(type);
914 getInterfaceMtuByName(name, &aa->Mtu);
916 getInterfaceStatusByName(name, &status);
917 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
918 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
919 else aa->OperStatus = IfOperStatusUnknown;
921 *size = total_size;
922 HeapFree(GetProcessHeap(), 0, routeTable);
923 HeapFree(GetProcessHeap(), 0, v6addrs);
924 HeapFree(GetProcessHeap(), 0, v4addrs);
925 return ERROR_SUCCESS;
928 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in *src )
930 SOCKADDR_IN *s = (SOCKADDR_IN *)dst;
932 s->sin_family = WS_AF_INET;
933 s->sin_port = src->sin_port;
934 memcpy( &s->sin_addr, &src->sin_addr, sizeof(IN_ADDR) );
935 memset( (char *)s + FIELD_OFFSET( SOCKADDR_IN, sin_zero ), 0,
936 sizeof(SOCKADDR_STORAGE) - FIELD_OFFSET( SOCKADDR_IN, sin_zero) );
939 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in6 *src )
941 SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)dst;
943 s->sin6_family = WS_AF_INET6;
944 s->sin6_port = src->sin6_port;
945 s->sin6_flowinfo = src->sin6_flowinfo;
946 memcpy( &s->sin6_addr, &src->sin6_addr, sizeof(IN6_ADDR) );
947 s->sin6_scope_id = src->sin6_scope_id;
948 memset( (char *)s + sizeof(SOCKADDR_IN6), 0,
949 sizeof(SOCKADDR_STORAGE) - sizeof(SOCKADDR_IN6) );
952 #ifdef HAVE_STRUCT___RES_STATE
953 /* call res_init() just once because of a bug in Mac OS X 10.4 */
954 /* Call once per thread on systems that have per-thread _res. */
955 static void initialise_resolver(void)
957 if ((_res.options & RES_INIT) == 0)
958 res_init();
961 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
963 int i, ip6_count = 0;
964 SOCKADDR_STORAGE *addr;
966 initialise_resolver();
968 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
969 ip6_count = _res._u._ext.nscount6;
970 #endif
972 if (!servers || !num)
974 num = _res.nscount;
975 if (ip4_only) num -= ip6_count;
976 return num;
979 for (i = 0, addr = servers; addr < (servers + num) && i < _res.nscount; i++)
981 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
982 if (_res._u._ext.nsaddrs[i])
984 if (ip4_only) continue;
985 sockaddr_in6_to_WS_storage( addr, _res._u._ext.nsaddrs[i] );
987 else
988 #endif
990 sockaddr_in_to_WS_storage( addr, _res.nsaddr_list + i );
992 addr++;
994 return addr - servers;
996 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
998 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1000 extern struct res_state *__res_get_state( void );
1001 extern int __res_getservers( struct res_state *, struct sockaddr_storage *, int );
1002 struct res_state *state = __res_get_state();
1003 int i, found = 0, total = __res_getservers( state, NULL, 0 );
1004 SOCKADDR_STORAGE *addr = servers;
1005 struct sockaddr_storage *buf;
1007 if ((!servers || !num) && !ip4_only) return total;
1009 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(struct sockaddr_storage) );
1010 total = __res_getservers( state, buf, total );
1012 for (i = 0; i < total; i++)
1014 if (buf[i].ss_family == AF_INET6 && ip4_only) continue;
1015 if (buf[i].ss_family != AF_INET && buf[i].ss_family != AF_INET6) continue;
1017 found++;
1018 if (!servers || !num) continue;
1020 if (buf[i].ss_family == AF_INET6)
1022 sockaddr_in6_to_WS_storage( addr, (struct sockaddr_in6 *)(buf + i) );
1024 else
1026 sockaddr_in_to_WS_storage( addr, (struct sockaddr_in *)(buf + i) );
1028 if (++addr >= servers + num) break;
1031 HeapFree( GetProcessHeap(), 0, buf );
1032 return found;
1034 #else
1036 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1038 FIXME("Unimplemented on this system\n");
1039 return 0;
1041 #endif
1043 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
1045 int num = get_dns_servers( NULL, 0, FALSE );
1046 DWORD size;
1048 size = num * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR_STORAGE));
1049 if (!address || *len < size)
1051 *len = size;
1052 return ERROR_BUFFER_OVERFLOW;
1054 *len = size;
1055 if (num > 0)
1057 PIP_ADAPTER_DNS_SERVER_ADDRESS addr = address;
1058 SOCKADDR_STORAGE *sock_addrs = (SOCKADDR_STORAGE *)(address + num);
1059 int i;
1061 get_dns_servers( sock_addrs, num, FALSE );
1063 for (i = 0; i < num; i++, addr = addr->Next)
1065 addr->u.s.Length = sizeof(*addr);
1066 if (sock_addrs[i].ss_family == WS_AF_INET6)
1067 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN6);
1068 else
1069 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1070 addr->Address.lpSockaddr = (SOCKADDR *)(sock_addrs + i);
1071 if (i == num - 1)
1072 addr->Next = NULL;
1073 else
1074 addr->Next = addr + 1;
1077 return ERROR_SUCCESS;
1080 #ifdef HAVE_STRUCT___RES_STATE
1081 static BOOL is_ip_address_string(const char *str)
1083 struct in_addr in;
1084 int ret;
1086 ret = inet_aton(str, &in);
1087 return ret != 0;
1089 #endif
1091 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1093 ULONG size;
1094 const char *found_suffix = "";
1095 /* Always return a NULL-terminated string, even if it's empty. */
1097 #ifdef HAVE_STRUCT___RES_STATE
1099 ULONG i;
1100 initialise_resolver();
1101 for (i = 0; !*found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1103 /* This uses a heuristic to select a DNS suffix:
1104 * the first, non-IP address string is selected.
1106 if (!is_ip_address_string(_res.dnsrch[i]))
1107 found_suffix = _res.dnsrch[i];
1110 #endif
1112 size = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, NULL, 0 ) * sizeof(WCHAR);
1113 if (!suffix || *len < size)
1115 *len = size;
1116 return ERROR_BUFFER_OVERFLOW;
1118 *len = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, suffix, *len / sizeof(WCHAR) ) * sizeof(WCHAR);
1119 return ERROR_SUCCESS;
1122 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1123 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1125 InterfaceIndexTable *table;
1126 ULONG i, size, dns_server_size, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1128 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1130 if (!buflen) return ERROR_INVALID_PARAMETER;
1132 get_interface_indices( FALSE, &table );
1133 if (!table || !table->numIndexes)
1135 HeapFree(GetProcessHeap(), 0, table);
1136 return ERROR_NO_DATA;
1138 total_size = 0;
1139 for (i = 0; i < table->numIndexes; i++)
1141 size = 0;
1142 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1144 HeapFree(GetProcessHeap(), 0, table);
1145 return ret;
1147 total_size += size;
1149 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1151 /* Since DNS servers aren't really per adapter, get enough space for a
1152 * single copy of them.
1154 get_dns_server_addresses(NULL, &dns_server_size);
1155 total_size += dns_server_size;
1157 /* Since DNS suffix also isn't really per adapter, get enough space for a
1158 * single copy of it.
1160 get_dns_suffix(NULL, &dns_suffix_size);
1161 total_size += dns_suffix_size;
1162 if (aa && *buflen >= total_size)
1164 ULONG bytes_left = size = total_size;
1165 PIP_ADAPTER_ADDRESSES first_aa = aa;
1166 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1167 WCHAR *dnsSuffix;
1169 for (i = 0; i < table->numIndexes; i++)
1171 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1173 HeapFree(GetProcessHeap(), 0, table);
1174 return ret;
1176 if (i < table->numIndexes - 1)
1178 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1179 aa = aa->Next;
1180 size = bytes_left -= size;
1183 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER) && dns_server_size)
1185 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1186 get_dns_server_addresses(firstDns, &dns_server_size);
1187 for (aa = first_aa; aa; aa = aa->Next)
1189 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1190 aa->FirstDnsServerAddress = firstDns;
1193 aa = first_aa;
1194 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1195 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1196 for (; aa; aa = aa->Next)
1198 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1199 aa->DnsSuffix = dnsSuffix;
1200 else
1201 aa->DnsSuffix = dnsSuffix + dns_suffix_size / sizeof(WCHAR) - 1;
1203 ret = ERROR_SUCCESS;
1205 else
1206 ret = ERROR_BUFFER_OVERFLOW;
1207 *buflen = total_size;
1209 TRACE("num adapters %u\n", table->numIndexes);
1210 HeapFree(GetProcessHeap(), 0, table);
1211 return ret;
1214 /******************************************************************
1215 * GetBestInterface (IPHLPAPI.@)
1217 * Get the interface, with the best route for the given IP address.
1219 * PARAMS
1220 * dwDestAddr [In] IP address to search the interface for
1221 * pdwBestIfIndex [Out] found best interface
1223 * RETURNS
1224 * Success: NO_ERROR
1225 * Failure: error code from winerror.h
1227 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1229 struct WS_sockaddr_in sa_in;
1230 memset(&sa_in, 0, sizeof(sa_in));
1231 sa_in.sin_family = AF_INET;
1232 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1233 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1236 /******************************************************************
1237 * GetBestInterfaceEx (IPHLPAPI.@)
1239 * Get the interface, with the best route for the given IP address.
1241 * PARAMS
1242 * dwDestAddr [In] IP address to search the interface for
1243 * pdwBestIfIndex [Out] found best interface
1245 * RETURNS
1246 * Success: NO_ERROR
1247 * Failure: error code from winerror.h
1249 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1251 DWORD ret;
1253 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1254 if (!pDestAddr || !pdwBestIfIndex)
1255 ret = ERROR_INVALID_PARAMETER;
1256 else {
1257 MIB_IPFORWARDROW ipRow;
1259 if (pDestAddr->sa_family == AF_INET) {
1260 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1261 if (ret == ERROR_SUCCESS)
1262 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1263 } else {
1264 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1265 ret = ERROR_NOT_SUPPORTED;
1268 TRACE("returning %d\n", ret);
1269 return ret;
1273 /******************************************************************
1274 * GetBestRoute (IPHLPAPI.@)
1276 * Get the best route for the given IP address.
1278 * PARAMS
1279 * dwDestAddr [In] IP address to search the best route for
1280 * dwSourceAddr [In] optional source IP address
1281 * pBestRoute [Out] found best route
1283 * RETURNS
1284 * Success: NO_ERROR
1285 * Failure: error code from winerror.h
1287 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1289 PMIB_IPFORWARDTABLE table;
1290 DWORD ret;
1292 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1293 dwSourceAddr, pBestRoute);
1294 if (!pBestRoute)
1295 return ERROR_INVALID_PARAMETER;
1297 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1298 if (!ret) {
1299 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1301 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1302 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1303 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1304 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1305 DWORD numShifts, mask;
1307 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1308 mask && mask & 1; mask >>= 1, numShifts++)
1310 if (numShifts > matchedBits) {
1311 matchedBits = numShifts;
1312 matchedNdx = ndx;
1314 else if (!matchedBits) {
1315 matchedNdx = ndx;
1319 if (matchedNdx < table->dwNumEntries) {
1320 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1321 ret = ERROR_SUCCESS;
1323 else {
1324 /* No route matches, which can happen if there's no default route. */
1325 ret = ERROR_HOST_UNREACHABLE;
1327 HeapFree(GetProcessHeap(), 0, table);
1329 TRACE("returning %d\n", ret);
1330 return ret;
1334 /******************************************************************
1335 * GetFriendlyIfIndex (IPHLPAPI.@)
1337 * Get a "friendly" version of IfIndex, which is one that doesn't
1338 * have the top byte set. Doesn't validate whether IfIndex is a valid
1339 * adapter index.
1341 * PARAMS
1342 * IfIndex [In] interface index to get the friendly one for
1344 * RETURNS
1345 * A friendly version of IfIndex.
1347 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1349 /* windows doesn't validate these, either, just makes sure the top byte is
1350 cleared. I assume my ifenum module never gives an index with the top
1351 byte set. */
1352 TRACE("returning %d\n", IfIndex);
1353 return IfIndex;
1357 /******************************************************************
1358 * GetIfEntry (IPHLPAPI.@)
1360 * Get information about an interface.
1362 * PARAMS
1363 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1364 * Out: interface information
1366 * RETURNS
1367 * Success: NO_ERROR
1368 * Failure: error code from winerror.h
1370 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1372 DWORD ret;
1373 char nameBuf[MAX_ADAPTER_NAME];
1374 char *name;
1376 TRACE("pIfRow %p\n", pIfRow);
1377 if (!pIfRow)
1378 return ERROR_INVALID_PARAMETER;
1380 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1381 if (name) {
1382 ret = getInterfaceEntryByName(name, pIfRow);
1383 if (ret == NO_ERROR)
1384 ret = getInterfaceStatsByName(name, pIfRow);
1386 else
1387 ret = ERROR_INVALID_DATA;
1388 TRACE("returning %d\n", ret);
1389 return ret;
1393 static int IfTableSorter(const void *a, const void *b)
1395 int ret;
1397 if (a && b)
1398 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1399 else
1400 ret = 0;
1401 return ret;
1405 /******************************************************************
1406 * GetIfTable (IPHLPAPI.@)
1408 * Get a table of local interfaces.
1410 * PARAMS
1411 * pIfTable [Out] buffer for local interfaces table
1412 * pdwSize [In/Out] length of output buffer
1413 * bOrder [In] whether to sort the table
1415 * RETURNS
1416 * Success: NO_ERROR
1417 * Failure: error code from winerror.h
1419 * NOTES
1420 * If pdwSize is less than required, the function will return
1421 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1422 * size.
1423 * If bOrder is true, the returned table will be sorted by interface index.
1425 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1427 DWORD ret;
1429 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1430 (DWORD)bOrder);
1431 if (!pdwSize)
1432 ret = ERROR_INVALID_PARAMETER;
1433 else {
1434 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1435 ULONG size = sizeof(MIB_IFTABLE);
1437 if (numInterfaces > 1)
1438 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1439 if (!pIfTable || *pdwSize < size) {
1440 *pdwSize = size;
1441 ret = ERROR_INSUFFICIENT_BUFFER;
1443 else {
1444 InterfaceIndexTable *table;
1445 get_interface_indices( FALSE, &table );
1447 if (table) {
1448 size = sizeof(MIB_IFTABLE);
1449 if (table->numIndexes > 1)
1450 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1451 if (*pdwSize < size) {
1452 *pdwSize = size;
1453 ret = ERROR_INSUFFICIENT_BUFFER;
1455 else {
1456 DWORD ndx;
1458 *pdwSize = size;
1459 pIfTable->dwNumEntries = 0;
1460 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1461 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1462 GetIfEntry(&pIfTable->table[ndx]);
1463 pIfTable->dwNumEntries++;
1465 if (bOrder)
1466 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1467 IfTableSorter);
1468 ret = NO_ERROR;
1470 HeapFree(GetProcessHeap(), 0, table);
1472 else
1473 ret = ERROR_OUTOFMEMORY;
1476 TRACE("returning %d\n", ret);
1477 return ret;
1481 /******************************************************************
1482 * GetInterfaceInfo (IPHLPAPI.@)
1484 * Get a list of network interface adapters.
1486 * PARAMS
1487 * pIfTable [Out] buffer for interface adapters
1488 * dwOutBufLen [Out] if buffer is too small, returns required size
1490 * RETURNS
1491 * Success: NO_ERROR
1492 * Failure: error code from winerror.h
1494 * BUGS
1495 * MSDN states this should return non-loopback interfaces only.
1497 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1499 DWORD ret;
1501 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1502 if (!dwOutBufLen)
1503 ret = ERROR_INVALID_PARAMETER;
1504 else {
1505 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1506 ULONG size = sizeof(IP_INTERFACE_INFO);
1508 if (numInterfaces > 1)
1509 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1510 if (!pIfTable || *dwOutBufLen < size) {
1511 *dwOutBufLen = size;
1512 ret = ERROR_INSUFFICIENT_BUFFER;
1514 else {
1515 InterfaceIndexTable *table;
1516 get_interface_indices( FALSE, &table );
1518 if (table) {
1519 size = sizeof(IP_INTERFACE_INFO);
1520 if (table->numIndexes > 1)
1521 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1522 if (*dwOutBufLen < size) {
1523 *dwOutBufLen = size;
1524 ret = ERROR_INSUFFICIENT_BUFFER;
1526 else {
1527 DWORD ndx;
1528 char nameBuf[MAX_ADAPTER_NAME];
1530 *dwOutBufLen = size;
1531 pIfTable->NumAdapters = 0;
1532 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1533 const char *walker, *name;
1534 WCHAR *assigner;
1536 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1537 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1538 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1539 walker && *walker &&
1540 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1541 walker++, assigner++)
1542 *assigner = *walker;
1543 *assigner = 0;
1544 pIfTable->NumAdapters++;
1546 ret = NO_ERROR;
1548 HeapFree(GetProcessHeap(), 0, table);
1550 else
1551 ret = ERROR_OUTOFMEMORY;
1554 TRACE("returning %d\n", ret);
1555 return ret;
1559 /******************************************************************
1560 * GetIpAddrTable (IPHLPAPI.@)
1562 * Get interface-to-IP address mapping table.
1564 * PARAMS
1565 * pIpAddrTable [Out] buffer for mapping table
1566 * pdwSize [In/Out] length of output buffer
1567 * bOrder [In] whether to sort the table
1569 * RETURNS
1570 * Success: NO_ERROR
1571 * Failure: error code from winerror.h
1573 * NOTES
1574 * If pdwSize is less than required, the function will return
1575 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1576 * size.
1577 * If bOrder is true, the returned table will be sorted by the next hop and
1578 * an assortment of arbitrary parameters.
1580 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1582 DWORD ret;
1584 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1585 (DWORD)bOrder);
1586 if (!pdwSize)
1587 ret = ERROR_INVALID_PARAMETER;
1588 else {
1589 PMIB_IPADDRTABLE table;
1591 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1592 if (ret == NO_ERROR)
1594 ULONG size = FIELD_OFFSET(MIB_IPADDRTABLE, table[table->dwNumEntries]);
1596 if (!pIpAddrTable || *pdwSize < size) {
1597 *pdwSize = size;
1598 ret = ERROR_INSUFFICIENT_BUFFER;
1600 else {
1601 *pdwSize = size;
1602 memcpy(pIpAddrTable, table, size);
1603 if (bOrder)
1604 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1605 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1606 ret = NO_ERROR;
1608 HeapFree(GetProcessHeap(), 0, table);
1611 TRACE("returning %d\n", ret);
1612 return ret;
1616 /******************************************************************
1617 * GetIpForwardTable (IPHLPAPI.@)
1619 * Get the route table.
1621 * PARAMS
1622 * pIpForwardTable [Out] buffer for route table
1623 * pdwSize [In/Out] length of output buffer
1624 * bOrder [In] whether to sort the table
1626 * RETURNS
1627 * Success: NO_ERROR
1628 * Failure: error code from winerror.h
1630 * NOTES
1631 * If pdwSize is less than required, the function will return
1632 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1633 * size.
1634 * If bOrder is true, the returned table will be sorted by the next hop and
1635 * an assortment of arbitrary parameters.
1637 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
1639 DWORD ret;
1640 PMIB_IPFORWARDTABLE table;
1642 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
1644 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1646 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
1647 if (!ret) {
1648 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
1649 if (!pIpForwardTable || *pdwSize < size) {
1650 *pdwSize = size;
1651 ret = ERROR_INSUFFICIENT_BUFFER;
1653 else {
1654 *pdwSize = size;
1655 memcpy(pIpForwardTable, table, size);
1657 HeapFree(GetProcessHeap(), 0, table);
1659 TRACE("returning %d\n", ret);
1660 return ret;
1664 /******************************************************************
1665 * GetIpNetTable (IPHLPAPI.@)
1667 * Get the IP-to-physical address mapping table.
1669 * PARAMS
1670 * pIpNetTable [Out] buffer for mapping table
1671 * pdwSize [In/Out] length of output buffer
1672 * bOrder [In] whether to sort the table
1674 * RETURNS
1675 * Success: NO_ERROR
1676 * Failure: error code from winerror.h
1678 * NOTES
1679 * If pdwSize is less than required, the function will return
1680 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1681 * size.
1682 * If bOrder is true, the returned table will be sorted by IP address.
1684 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
1686 DWORD ret;
1687 PMIB_IPNETTABLE table;
1689 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
1691 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1693 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
1694 if (!ret) {
1695 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
1696 if (!pIpNetTable || *pdwSize < size) {
1697 *pdwSize = size;
1698 ret = ERROR_INSUFFICIENT_BUFFER;
1700 else {
1701 *pdwSize = size;
1702 memcpy(pIpNetTable, table, size);
1704 HeapFree(GetProcessHeap(), 0, table);
1706 TRACE("returning %d\n", ret);
1707 return ret;
1710 /* Gets the DNS server list into the list beginning at list. Assumes that
1711 * a single server address may be placed at list if *len is at least
1712 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
1713 * and assumes that all remaining DNS servers are contiguously located
1714 * beginning at firstDynamic. On input, *len is assumed to be the total number
1715 * of bytes available for all DNS servers, and is ignored if list is NULL.
1716 * On return, *len is set to the total number of bytes required for all DNS
1717 * servers.
1718 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
1719 * ERROR_SUCCESS otherwise.
1721 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
1722 PIP_ADDR_STRING firstDynamic, DWORD *len)
1724 DWORD size;
1725 int num = get_dns_servers( NULL, 0, TRUE );
1727 size = num * sizeof(IP_ADDR_STRING);
1728 if (!list || *len < size) {
1729 *len = size;
1730 return ERROR_BUFFER_OVERFLOW;
1732 *len = size;
1733 if (num > 0) {
1734 PIP_ADDR_STRING ptr;
1735 int i;
1736 SOCKADDR_STORAGE *addr = HeapAlloc( GetProcessHeap(), 0, num * sizeof(SOCKADDR_STORAGE) );
1738 get_dns_servers( addr, num, TRUE );
1740 for (i = 0, ptr = list; i < num; i++, ptr = ptr->Next) {
1741 toIPAddressString(((struct sockaddr_in *)(addr + i))->sin_addr.s_addr,
1742 ptr->IpAddress.String);
1743 if (i == num - 1)
1744 ptr->Next = NULL;
1745 else if (i == 0)
1746 ptr->Next = firstDynamic;
1747 else
1748 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
1750 HeapFree( GetProcessHeap(), 0, addr );
1752 return ERROR_SUCCESS;
1755 /******************************************************************
1756 * GetNetworkParams (IPHLPAPI.@)
1758 * Get the network parameters for the local computer.
1760 * PARAMS
1761 * pFixedInfo [Out] buffer for network parameters
1762 * pOutBufLen [In/Out] length of output buffer
1764 * RETURNS
1765 * Success: NO_ERROR
1766 * Failure: error code from winerror.h
1768 * NOTES
1769 * If pOutBufLen is less than required, the function will return
1770 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
1771 * size.
1773 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
1775 DWORD ret, size, serverListSize;
1776 LONG regReturn;
1777 HKEY hKey;
1779 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
1780 if (!pOutBufLen)
1781 return ERROR_INVALID_PARAMETER;
1783 get_dns_server_list(NULL, NULL, &serverListSize);
1784 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
1785 if (!pFixedInfo || *pOutBufLen < size) {
1786 *pOutBufLen = size;
1787 return ERROR_BUFFER_OVERFLOW;
1790 memset(pFixedInfo, 0, size);
1791 size = sizeof(pFixedInfo->HostName);
1792 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
1793 size = sizeof(pFixedInfo->DomainName);
1794 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
1795 get_dns_server_list(&pFixedInfo->DnsServerList,
1796 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
1797 &serverListSize);
1798 /* Assume the first DNS server in the list is the "current" DNS server: */
1799 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
1800 pFixedInfo->NodeType = HYBRID_NODETYPE;
1801 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1802 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1803 if (regReturn != ERROR_SUCCESS)
1804 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1805 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
1806 &hKey);
1807 if (regReturn == ERROR_SUCCESS)
1809 DWORD size = sizeof(pFixedInfo->ScopeId);
1811 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
1812 RegCloseKey(hKey);
1815 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
1816 I suppose could also check for a listener on port 53 to set EnableDns */
1817 ret = NO_ERROR;
1818 TRACE("returning %d\n", ret);
1819 return ret;
1823 /******************************************************************
1824 * GetNumberOfInterfaces (IPHLPAPI.@)
1826 * Get the number of interfaces.
1828 * PARAMS
1829 * pdwNumIf [Out] number of interfaces
1831 * RETURNS
1832 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
1834 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
1836 DWORD ret;
1838 TRACE("pdwNumIf %p\n", pdwNumIf);
1839 if (!pdwNumIf)
1840 ret = ERROR_INVALID_PARAMETER;
1841 else {
1842 *pdwNumIf = get_interface_indices( FALSE, NULL );
1843 ret = NO_ERROR;
1845 TRACE("returning %d\n", ret);
1846 return ret;
1850 /******************************************************************
1851 * GetPerAdapterInfo (IPHLPAPI.@)
1853 * Get information about an adapter corresponding to an interface.
1855 * PARAMS
1856 * IfIndex [In] interface info
1857 * pPerAdapterInfo [Out] buffer for per adapter info
1858 * pOutBufLen [In/Out] length of output buffer
1860 * RETURNS
1861 * Success: NO_ERROR
1862 * Failure: error code from winerror.h
1864 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
1866 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
1867 DWORD ret = NO_ERROR;
1869 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
1871 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
1873 if (!isIfIndexLoopback(IfIndex)) {
1874 get_dns_server_list(NULL, NULL, &serverListSize);
1875 if (serverListSize > sizeof(IP_ADDR_STRING))
1876 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
1878 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
1880 *pOutBufLen = bytesNeeded;
1881 return ERROR_BUFFER_OVERFLOW;
1884 memset(pPerAdapterInfo, 0, bytesNeeded);
1885 if (!isIfIndexLoopback(IfIndex)) {
1886 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
1887 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
1888 &serverListSize);
1889 /* Assume the first DNS server in the list is the "current" DNS server: */
1890 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
1892 return ret;
1896 /******************************************************************
1897 * GetRTTAndHopCount (IPHLPAPI.@)
1899 * Get round-trip time (RTT) and hop count.
1901 * PARAMS
1903 * DestIpAddress [In] destination address to get the info for
1904 * HopCount [Out] retrieved hop count
1905 * MaxHops [In] maximum hops to search for the destination
1906 * RTT [Out] RTT in milliseconds
1908 * RETURNS
1909 * Success: TRUE
1910 * Failure: FALSE
1912 * FIXME
1913 * Stub, returns FALSE.
1915 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
1917 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
1918 DestIpAddress, HopCount, MaxHops, RTT);
1919 return FALSE;
1923 /******************************************************************
1924 * GetTcpTable (IPHLPAPI.@)
1926 * Get the table of active TCP connections.
1928 * PARAMS
1929 * pTcpTable [Out] buffer for TCP connections table
1930 * pdwSize [In/Out] length of output buffer
1931 * bOrder [In] whether to order the table
1933 * RETURNS
1934 * Success: NO_ERROR
1935 * Failure: error code from winerror.h
1937 * NOTES
1938 * If pdwSize is less than required, the function will return
1939 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
1940 * the required byte size.
1941 * If bOrder is true, the returned table will be sorted, first by
1942 * local address and port number, then by remote address and port
1943 * number.
1945 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
1947 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
1948 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, AF_INET, TCP_TABLE_BASIC_ALL, 0);
1951 /******************************************************************
1952 * GetExtendedTcpTable (IPHLPAPI.@)
1954 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
1955 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
1957 DWORD ret, size;
1958 void *table;
1960 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
1961 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
1963 if (!pdwSize) return ERROR_INVALID_PARAMETER;
1965 if (ulAf != AF_INET)
1967 FIXME("ulAf = %u not supported\n", ulAf);
1968 return ERROR_NOT_SUPPORTED;
1970 if (TableClass >= TCP_TABLE_OWNER_MODULE_LISTENER)
1971 FIXME("module classes not fully supported\n");
1973 if ((ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
1974 return ret;
1976 if (!pTcpTable || *pdwSize < size)
1978 *pdwSize = size;
1979 ret = ERROR_INSUFFICIENT_BUFFER;
1981 else
1983 *pdwSize = size;
1984 memcpy(pTcpTable, table, size);
1986 HeapFree(GetProcessHeap(), 0, table);
1987 return ret;
1990 /******************************************************************
1991 * GetUdpTable (IPHLPAPI.@)
1993 * Get a table of active UDP connections.
1995 * PARAMS
1996 * pUdpTable [Out] buffer for UDP connections table
1997 * pdwSize [In/Out] length of output buffer
1998 * bOrder [In] whether to order the table
2000 * RETURNS
2001 * Success: NO_ERROR
2002 * Failure: error code from winerror.h
2004 * NOTES
2005 * If pdwSize is less than required, the function will return
2006 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2007 * required byte size.
2008 * If bOrder is true, the returned table will be sorted, first by
2009 * local address, then by local port number.
2011 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2013 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, AF_INET, UDP_TABLE_BASIC, 0);
2016 /******************************************************************
2017 * GetExtendedUdpTable (IPHLPAPI.@)
2019 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
2020 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
2022 DWORD ret, size;
2023 void *table;
2025 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2026 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2028 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2030 if (ulAf != AF_INET)
2032 FIXME("ulAf = %u not supported\n", ulAf);
2033 return ERROR_NOT_SUPPORTED;
2035 if (TableClass == UDP_TABLE_OWNER_MODULE)
2036 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2038 if ((ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2039 return ret;
2041 if (!pUdpTable || *pdwSize < size)
2043 *pdwSize = size;
2044 ret = ERROR_INSUFFICIENT_BUFFER;
2046 else
2048 *pdwSize = size;
2049 memcpy(pUdpTable, table, size);
2051 HeapFree(GetProcessHeap(), 0, table);
2052 return ret;
2055 /******************************************************************
2056 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2058 * This is a Win98-only function to get information on "unidirectional"
2059 * adapters. Since this is pretty nonsensical in other contexts, it
2060 * never returns anything.
2062 * PARAMS
2063 * pIPIfInfo [Out] buffer for adapter infos
2064 * dwOutBufLen [Out] length of the output buffer
2066 * RETURNS
2067 * Success: NO_ERROR
2068 * Failure: error code from winerror.h
2070 * FIXME
2071 * Stub, returns ERROR_NOT_SUPPORTED.
2073 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
2075 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
2076 /* a unidirectional adapter?? not bloody likely! */
2077 return ERROR_NOT_SUPPORTED;
2081 /******************************************************************
2082 * IpReleaseAddress (IPHLPAPI.@)
2084 * Release an IP obtained through DHCP,
2086 * PARAMS
2087 * AdapterInfo [In] adapter to release IP address
2089 * RETURNS
2090 * Success: NO_ERROR
2091 * Failure: error code from winerror.h
2093 * NOTES
2094 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2095 * this function does nothing.
2097 * FIXME
2098 * Stub, returns ERROR_NOT_SUPPORTED.
2100 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2102 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2103 return ERROR_NOT_SUPPORTED;
2107 /******************************************************************
2108 * IpRenewAddress (IPHLPAPI.@)
2110 * Renew an IP obtained through DHCP.
2112 * PARAMS
2113 * AdapterInfo [In] adapter to renew IP address
2115 * RETURNS
2116 * Success: NO_ERROR
2117 * Failure: error code from winerror.h
2119 * NOTES
2120 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2121 * this function does nothing.
2123 * FIXME
2124 * Stub, returns ERROR_NOT_SUPPORTED.
2126 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2128 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2129 return ERROR_NOT_SUPPORTED;
2133 /******************************************************************
2134 * NotifyAddrChange (IPHLPAPI.@)
2136 * Notify caller whenever the ip-interface map is changed.
2138 * PARAMS
2139 * Handle [Out] handle usable in asynchronous notification
2140 * overlapped [In] overlapped structure that notifies the caller
2142 * RETURNS
2143 * Success: NO_ERROR
2144 * Failure: error code from winerror.h
2146 * FIXME
2147 * Stub, returns ERROR_NOT_SUPPORTED.
2149 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2151 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2152 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2153 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2154 return ERROR_IO_PENDING;
2158 /******************************************************************
2159 * NotifyRouteChange (IPHLPAPI.@)
2161 * Notify caller whenever the ip routing table is changed.
2163 * PARAMS
2164 * Handle [Out] handle usable in asynchronous notification
2165 * overlapped [In] overlapped structure that notifies the caller
2167 * RETURNS
2168 * Success: NO_ERROR
2169 * Failure: error code from winerror.h
2171 * FIXME
2172 * Stub, returns ERROR_NOT_SUPPORTED.
2174 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2176 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2177 return ERROR_NOT_SUPPORTED;
2181 /******************************************************************
2182 * SendARP (IPHLPAPI.@)
2184 * Send an ARP request.
2186 * PARAMS
2187 * DestIP [In] attempt to obtain this IP
2188 * SrcIP [In] optional sender IP address
2189 * pMacAddr [Out] buffer for the mac address
2190 * PhyAddrLen [In/Out] length of the output buffer
2192 * RETURNS
2193 * Success: NO_ERROR
2194 * Failure: error code from winerror.h
2196 * FIXME
2197 * Stub, returns ERROR_NOT_SUPPORTED.
2199 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2201 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2202 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2203 return ERROR_NOT_SUPPORTED;
2207 /******************************************************************
2208 * SetIfEntry (IPHLPAPI.@)
2210 * Set the administrative status of an interface.
2212 * PARAMS
2213 * pIfRow [In] dwAdminStatus member specifies the new status.
2215 * RETURNS
2216 * Success: NO_ERROR
2217 * Failure: error code from winerror.h
2219 * FIXME
2220 * Stub, returns ERROR_NOT_SUPPORTED.
2222 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2224 FIXME("(pIfRow %p): stub\n", pIfRow);
2225 /* this is supposed to set an interface administratively up or down.
2226 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2227 this sort of down is indistinguishable from other sorts of down (e.g. no
2228 link). */
2229 return ERROR_NOT_SUPPORTED;
2233 /******************************************************************
2234 * SetIpForwardEntry (IPHLPAPI.@)
2236 * Modify an existing route.
2238 * PARAMS
2239 * pRoute [In] route with the new information
2241 * RETURNS
2242 * Success: NO_ERROR
2243 * Failure: error code from winerror.h
2245 * FIXME
2246 * Stub, returns NO_ERROR.
2248 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2250 FIXME("(pRoute %p): stub\n", pRoute);
2251 /* this is to add a route entry, how's it distinguishable from
2252 CreateIpForwardEntry?
2253 could use SIOCADDRT, not sure I want to */
2254 return 0;
2258 /******************************************************************
2259 * SetIpNetEntry (IPHLPAPI.@)
2261 * Modify an existing ARP entry.
2263 * PARAMS
2264 * pArpEntry [In] ARP entry with the new information
2266 * RETURNS
2267 * Success: NO_ERROR
2268 * Failure: error code from winerror.h
2270 * FIXME
2271 * Stub, returns NO_ERROR.
2273 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2275 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2276 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2277 return 0;
2281 /******************************************************************
2282 * SetIpStatistics (IPHLPAPI.@)
2284 * Toggle IP forwarding and det the default TTL value.
2286 * PARAMS
2287 * pIpStats [In] IP statistics with the new information
2289 * RETURNS
2290 * Success: NO_ERROR
2291 * Failure: error code from winerror.h
2293 * FIXME
2294 * Stub, returns NO_ERROR.
2296 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2298 FIXME("(pIpStats %p): stub\n", pIpStats);
2299 return 0;
2303 /******************************************************************
2304 * SetIpTTL (IPHLPAPI.@)
2306 * Set the default TTL value.
2308 * PARAMS
2309 * nTTL [In] new TTL value
2311 * RETURNS
2312 * Success: NO_ERROR
2313 * Failure: error code from winerror.h
2315 * FIXME
2316 * Stub, returns NO_ERROR.
2318 DWORD WINAPI SetIpTTL(UINT nTTL)
2320 FIXME("(nTTL %d): stub\n", nTTL);
2321 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2322 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2323 return 0;
2327 /******************************************************************
2328 * SetTcpEntry (IPHLPAPI.@)
2330 * Set the state of a TCP connection.
2332 * PARAMS
2333 * pTcpRow [In] specifies connection with new state
2335 * RETURNS
2336 * Success: NO_ERROR
2337 * Failure: error code from winerror.h
2339 * FIXME
2340 * Stub, returns NO_ERROR.
2342 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2344 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2345 return 0;
2349 /******************************************************************
2350 * UnenableRouter (IPHLPAPI.@)
2352 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2353 * if it reaches zero.
2355 * PARAMS
2356 * pOverlapped [In/Out] should be the same as in EnableRouter()
2357 * lpdwEnableCount [Out] optional, receives reference count
2359 * RETURNS
2360 * Success: NO_ERROR
2361 * Failure: error code from winerror.h
2363 * FIXME
2364 * Stub, returns ERROR_NOT_SUPPORTED.
2366 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2368 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2369 lpdwEnableCount);
2370 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2371 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2373 return ERROR_NOT_SUPPORTED;
2376 /******************************************************************
2377 * PfCreateInterface (IPHLPAPI.@)
2379 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2380 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2382 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2383 return ERROR_CALL_NOT_IMPLEMENTED;
2386 /******************************************************************
2387 * GetTcpTable2 (IPHLPAPI.@)
2389 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
2391 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
2392 return ERROR_NOT_SUPPORTED;
2395 /******************************************************************
2396 * GetTcp6Table (IPHLPAPI.@)
2398 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
2400 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table, size, order);
2401 return ERROR_NOT_SUPPORTED;
2404 /******************************************************************
2405 * GetTcp6Table2 (IPHLPAPI.@)
2407 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
2409 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
2410 return ERROR_NOT_SUPPORTED;