wined3d: Call wglGetPixelFormat() through the gl_ops table.
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
blob90dcda3f6f778c9142b1810b61ccaa7eb1c0e25b
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 <stdio.h>
26 #include <sys/types.h>
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
32 #endif
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
35 #endif
36 #ifdef HAVE_RESOLV_H
37 # include <resolv.h>
38 #endif
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winreg.h"
45 #define USE_WS_PREFIX
46 #include "winsock2.h"
47 #include "winternl.h"
48 #include "ws2ipdef.h"
49 #include "iphlpapi.h"
50 #include "ifenum.h"
51 #include "ipstats.h"
52 #include "ipifcons.h"
53 #include "fltdefs.h"
54 #include "ifdef.h"
55 #include "netioapi.h"
56 #include "tcpestats.h"
58 #include "wine/debug.h"
59 #include "wine/unicode.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
63 #ifndef IF_NAMESIZE
64 #define IF_NAMESIZE 16
65 #endif
67 #ifndef INADDR_NONE
68 #define INADDR_NONE ~0UL
69 #endif
71 /******************************************************************
72 * AddIPAddress (IPHLPAPI.@)
74 * Add an IP address to an adapter.
76 * PARAMS
77 * Address [In] IP address to add to the adapter
78 * IpMask [In] subnet mask for the IP address
79 * IfIndex [In] adapter index to add the address
80 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
81 * NTEInstance [Out] NTE instance for the IP address
83 * RETURNS
84 * Success: NO_ERROR
85 * Failure: error code from winerror.h
87 * FIXME
88 * Stub. Currently returns ERROR_NOT_SUPPORTED.
90 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
92 FIXME(":stub\n");
93 return ERROR_NOT_SUPPORTED;
97 /******************************************************************
98 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
100 * Get table of local interfaces.
101 * Like GetIfTable(), but allocate the returned table from heap.
103 * PARAMS
104 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
105 * allocated and returned.
106 * bOrder [In] whether to sort the table
107 * heap [In] heap from which the table is allocated
108 * flags [In] flags to HeapAlloc
110 * RETURNS
111 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
112 * GetIfTable() returns otherwise.
114 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
115 BOOL bOrder, HANDLE heap, DWORD flags)
117 DWORD ret;
119 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
120 bOrder, heap, flags);
121 if (!ppIfTable)
122 ret = ERROR_INVALID_PARAMETER;
123 else {
124 DWORD dwSize = 0;
126 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
127 if (ret == ERROR_INSUFFICIENT_BUFFER) {
128 *ppIfTable = HeapAlloc(heap, flags, dwSize);
129 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
132 TRACE("returning %d\n", ret);
133 return ret;
137 static int IpAddrTableSorter(const void *a, const void *b)
139 int ret;
141 if (a && b)
142 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
143 else
144 ret = 0;
145 return ret;
149 /******************************************************************
150 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
152 * Get interface-to-IP address mapping table.
153 * Like GetIpAddrTable(), but allocate the returned table from heap.
155 * PARAMS
156 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
157 * allocated and returned.
158 * bOrder [In] whether to sort the table
159 * heap [In] heap from which the table is allocated
160 * flags [In] flags to HeapAlloc
162 * RETURNS
163 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
164 * failure, NO_ERROR on success.
166 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
167 BOOL bOrder, HANDLE heap, DWORD flags)
169 DWORD ret;
171 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
172 ppIpAddrTable, bOrder, heap, flags);
173 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
174 if (!ret && bOrder)
175 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
176 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
177 TRACE("returning %d\n", ret);
178 return ret;
182 /******************************************************************
183 * CancelIPChangeNotify (IPHLPAPI.@)
185 * Cancel a previous notification created by NotifyAddrChange or
186 * NotifyRouteChange.
188 * PARAMS
189 * overlapped [In] overlapped structure that notifies the caller
191 * RETURNS
192 * Success: TRUE
193 * Failure: FALSE
195 * FIXME
196 * Stub, returns FALSE.
198 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
200 FIXME("(overlapped %p): stub\n", overlapped);
201 return FALSE;
205 /******************************************************************
206 * CancelMibChangeNotify2 (IPHLPAPI.@)
208 DWORD WINAPI CancelMibChangeNotify2(HANDLE handle)
210 FIXME("(handle %p): stub\n", handle);
211 return NO_ERROR;
215 /******************************************************************
216 * CreateIpForwardEntry (IPHLPAPI.@)
218 * Create a route in the local computer's IP table.
220 * PARAMS
221 * pRoute [In] new route information
223 * RETURNS
224 * Success: NO_ERROR
225 * Failure: error code from winerror.h
227 * FIXME
228 * Stub, always returns NO_ERROR.
230 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
232 FIXME("(pRoute %p): stub\n", pRoute);
233 /* could use SIOCADDRT, not sure I want to */
234 return 0;
238 /******************************************************************
239 * CreateIpNetEntry (IPHLPAPI.@)
241 * Create entry in the ARP table.
243 * PARAMS
244 * pArpEntry [In] new ARP entry
246 * RETURNS
247 * Success: NO_ERROR
248 * Failure: error code from winerror.h
250 * FIXME
251 * Stub, always returns NO_ERROR.
253 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
255 FIXME("(pArpEntry %p)\n", pArpEntry);
256 /* could use SIOCSARP on systems that support it, not sure I want to */
257 return 0;
261 /******************************************************************
262 * CreateProxyArpEntry (IPHLPAPI.@)
264 * Create a Proxy ARP (PARP) entry for an IP address.
266 * PARAMS
267 * dwAddress [In] IP address for which this computer acts as a proxy.
268 * dwMask [In] subnet mask for dwAddress
269 * dwIfIndex [In] interface index
271 * RETURNS
272 * Success: NO_ERROR
273 * Failure: error code from winerror.h
275 * FIXME
276 * Stub, returns ERROR_NOT_SUPPORTED.
278 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
280 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
281 dwAddress, dwMask, dwIfIndex);
282 return ERROR_NOT_SUPPORTED;
285 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
287 const IN6_ADDR *addr = &sin->sin6_addr;
288 char *p = buf;
289 int i;
290 BOOL in_zero = FALSE;
292 for (i = 0; i < 7; i++)
294 if (!addr->u.Word[i])
296 if (i == 0)
297 *p++ = ':';
298 if (!in_zero)
300 *p++ = ':';
301 in_zero = TRUE;
304 else
306 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
307 in_zero = FALSE;
310 sprintf(p, "%x", ntohs(addr->u.Word[7]));
311 return buf;
314 static BOOL map_address_6to4( const SOCKADDR_IN6 *addr6, SOCKADDR_IN *addr4 )
316 ULONG i;
318 if (addr6->sin6_family != WS_AF_INET6) return FALSE;
320 for (i = 0; i < 5; i++)
321 if (addr6->sin6_addr.u.Word[i]) return FALSE;
323 if (addr6->sin6_addr.u.Word[5] != 0xffff) return FALSE;
325 addr4->sin_family = WS_AF_INET;
326 addr4->sin_port = addr6->sin6_port;
327 addr4->sin_addr.S_un.S_addr = addr6->sin6_addr.u.Word[6] << 16 | addr6->sin6_addr.u.Word[7];
328 memset( &addr4->sin_zero, 0, sizeof(addr4->sin_zero) );
330 return TRUE;
333 static BOOL find_src_address( MIB_IPADDRTABLE *table, const SOCKADDR_IN *dst, SOCKADDR_IN6 *src )
335 MIB_IPFORWARDROW row;
336 DWORD i, j;
338 if (GetBestRoute( dst->sin_addr.S_un.S_addr, 0, &row )) return FALSE;
340 for (i = 0; i < table->dwNumEntries; i++)
342 /* take the first address */
343 if (table->table[i].dwIndex == row.dwForwardIfIndex)
345 src->sin6_family = WS_AF_INET6;
346 src->sin6_port = 0;
347 src->sin6_flowinfo = 0;
348 for (j = 0; j < 5; j++) src->sin6_addr.u.Word[j] = 0;
349 src->sin6_addr.u.Word[5] = 0xffff;
350 src->sin6_addr.u.Word[6] = table->table[i].dwAddr & 0xffff;
351 src->sin6_addr.u.Word[7] = table->table[i].dwAddr >> 16;
352 return TRUE;
356 return FALSE;
359 /******************************************************************
360 * CreateSortedAddressPairs (IPHLPAPI.@)
362 DWORD WINAPI CreateSortedAddressPairs( const PSOCKADDR_IN6 src_list, DWORD src_count,
363 const PSOCKADDR_IN6 dst_list, DWORD dst_count,
364 DWORD options, PSOCKADDR_IN6_PAIR *pair_list,
365 DWORD *pair_count )
367 DWORD i, size, ret;
368 SOCKADDR_IN6_PAIR *pairs;
369 SOCKADDR_IN6 *ptr;
370 SOCKADDR_IN addr4;
371 MIB_IPADDRTABLE *table;
373 FIXME( "(src_list %p src_count %u dst_list %p dst_count %u options %x pair_list %p pair_count %p): stub\n",
374 src_list, src_count, dst_list, dst_count, options, pair_list, pair_count );
376 if (src_list || src_count || !dst_list || !pair_list || !pair_count || dst_count > 500)
377 return ERROR_INVALID_PARAMETER;
379 for (i = 0; i < dst_count; i++)
381 if (!map_address_6to4( &dst_list[i], &addr4 ))
383 FIXME("only mapped IPv4 addresses are supported\n");
384 return ERROR_NOT_SUPPORTED;
388 size = dst_count * sizeof(*pairs);
389 size += dst_count * sizeof(SOCKADDR_IN6) * 2; /* source address + destination address */
390 if (!(pairs = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_NOT_ENOUGH_MEMORY;
391 ptr = (SOCKADDR_IN6 *)&pairs[dst_count];
393 if ((ret = getIPAddrTable( &table, GetProcessHeap(), 0 )))
395 HeapFree( GetProcessHeap(), 0, pairs );
396 return ret;
399 for (i = 0; i < dst_count; i++)
401 pairs[i].SourceAddress = ptr++;
402 if (!map_address_6to4( &dst_list[i], &addr4 ) ||
403 !find_src_address( table, &addr4, pairs[i].SourceAddress ))
405 char buf[46];
406 FIXME( "source address for %s not found\n", debugstr_ipv6(&dst_list[i], buf) );
407 memset( pairs[i].SourceAddress, 0, sizeof(*pairs[i].SourceAddress) );
408 pairs[i].SourceAddress->sin6_family = WS_AF_INET6;
411 pairs[i].DestinationAddress = ptr++;
412 memcpy( pairs[i].DestinationAddress, &dst_list[i], sizeof(*pairs[i].DestinationAddress) );
414 *pair_list = pairs;
415 *pair_count = dst_count;
417 HeapFree( GetProcessHeap(), 0, table );
418 return NO_ERROR;
422 /******************************************************************
423 * DeleteIPAddress (IPHLPAPI.@)
425 * Delete an IP address added with AddIPAddress().
427 * PARAMS
428 * NTEContext [In] NTE context from AddIPAddress();
430 * RETURNS
431 * Success: NO_ERROR
432 * Failure: error code from winerror.h
434 * FIXME
435 * Stub, returns ERROR_NOT_SUPPORTED.
437 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
439 FIXME("(NTEContext %d): stub\n", NTEContext);
440 return ERROR_NOT_SUPPORTED;
444 /******************************************************************
445 * DeleteIpForwardEntry (IPHLPAPI.@)
447 * Delete a route.
449 * PARAMS
450 * pRoute [In] route to delete
452 * RETURNS
453 * Success: NO_ERROR
454 * Failure: error code from winerror.h
456 * FIXME
457 * Stub, returns NO_ERROR.
459 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
461 FIXME("(pRoute %p): stub\n", pRoute);
462 /* could use SIOCDELRT, not sure I want to */
463 return 0;
467 /******************************************************************
468 * DeleteIpNetEntry (IPHLPAPI.@)
470 * Delete an ARP entry.
472 * PARAMS
473 * pArpEntry [In] ARP entry to delete
475 * RETURNS
476 * Success: NO_ERROR
477 * Failure: error code from winerror.h
479 * FIXME
480 * Stub, returns NO_ERROR.
482 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
484 FIXME("(pArpEntry %p): stub\n", pArpEntry);
485 /* could use SIOCDARP on systems that support it, not sure I want to */
486 return 0;
490 /******************************************************************
491 * DeleteProxyArpEntry (IPHLPAPI.@)
493 * Delete a Proxy ARP entry.
495 * PARAMS
496 * dwAddress [In] IP address for which this computer acts as a proxy.
497 * dwMask [In] subnet mask for dwAddress
498 * dwIfIndex [In] interface index
500 * RETURNS
501 * Success: NO_ERROR
502 * Failure: error code from winerror.h
504 * FIXME
505 * Stub, returns ERROR_NOT_SUPPORTED.
507 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
509 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
510 dwAddress, dwMask, dwIfIndex);
511 return ERROR_NOT_SUPPORTED;
515 /******************************************************************
516 * EnableRouter (IPHLPAPI.@)
518 * Turn on ip forwarding.
520 * PARAMS
521 * pHandle [In/Out]
522 * pOverlapped [In/Out] hEvent member should contain a valid handle.
524 * RETURNS
525 * Success: ERROR_IO_PENDING
526 * Failure: error code from winerror.h
528 * FIXME
529 * Stub, returns ERROR_NOT_SUPPORTED.
531 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
533 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
534 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
535 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
537 return ERROR_NOT_SUPPORTED;
541 /******************************************************************
542 * FlushIpNetTable (IPHLPAPI.@)
544 * Delete all ARP entries of an interface
546 * PARAMS
547 * dwIfIndex [In] interface index
549 * RETURNS
550 * Success: NO_ERROR
551 * Failure: error code from winerror.h
553 * FIXME
554 * Stub, returns ERROR_NOT_SUPPORTED.
556 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
558 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
559 /* this flushes the arp cache of the given index */
560 return ERROR_NOT_SUPPORTED;
563 /******************************************************************
564 * FreeMibTable (IPHLPAPI.@)
566 * Free buffer allocated by network functions
568 * PARAMS
569 * ptr [In] pointer to the buffer to free
572 void WINAPI FreeMibTable(void *ptr)
574 TRACE("(%p)\n", ptr);
575 HeapFree(GetProcessHeap(), 0, ptr);
578 /******************************************************************
579 * GetAdapterIndex (IPHLPAPI.@)
581 * Get interface index from its name.
583 * PARAMS
584 * AdapterName [In] unicode string with the adapter name
585 * IfIndex [Out] returns found interface index
587 * RETURNS
588 * Success: NO_ERROR
589 * Failure: error code from winerror.h
591 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
593 char adapterName[MAX_ADAPTER_NAME];
594 unsigned int i;
595 DWORD ret;
597 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
598 /* The adapter name is guaranteed not to have any unicode characters, so
599 * this translation is never lossy */
600 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
601 adapterName[i] = (char)AdapterName[i];
602 adapterName[i] = '\0';
603 ret = getInterfaceIndexByName(adapterName, IfIndex);
604 TRACE("returning %d\n", ret);
605 return ret;
609 /******************************************************************
610 * GetAdaptersInfo (IPHLPAPI.@)
612 * Get information about adapters.
614 * PARAMS
615 * pAdapterInfo [Out] buffer for adapter infos
616 * pOutBufLen [In] length of output buffer
618 * RETURNS
619 * Success: NO_ERROR
620 * Failure: error code from winerror.h
622 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
624 DWORD ret;
626 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
627 if (!pOutBufLen)
628 ret = ERROR_INVALID_PARAMETER;
629 else {
630 DWORD numNonLoopbackInterfaces = get_interface_indices( TRUE, NULL );
632 if (numNonLoopbackInterfaces > 0) {
633 DWORD numIPAddresses = getNumIPAddresses();
634 ULONG size;
636 /* This may slightly overestimate the amount of space needed, because
637 * the IP addresses include the loopback address, but it's easier
638 * to make sure there's more than enough space than to make sure there's
639 * precisely enough space.
641 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
642 size += numIPAddresses * sizeof(IP_ADDR_STRING);
643 if (!pAdapterInfo || *pOutBufLen < size) {
644 *pOutBufLen = size;
645 ret = ERROR_BUFFER_OVERFLOW;
647 else {
648 InterfaceIndexTable *table = NULL;
649 PMIB_IPADDRTABLE ipAddrTable = NULL;
650 PMIB_IPFORWARDTABLE routeTable = NULL;
652 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
653 if (!ret)
654 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
655 if (!ret)
656 get_interface_indices( TRUE, &table );
657 if (table) {
658 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
659 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
660 if (*pOutBufLen < size) {
661 *pOutBufLen = size;
662 ret = ERROR_INSUFFICIENT_BUFFER;
664 else {
665 DWORD ndx;
666 HKEY hKey;
667 BOOL winsEnabled = FALSE;
668 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
669 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
670 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
672 memset(pAdapterInfo, 0, size);
673 /* @@ Wine registry key: HKCU\Software\Wine\Network */
674 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
675 &hKey) == ERROR_SUCCESS) {
676 DWORD size = sizeof(primaryWINS.String);
677 unsigned long addr;
679 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
680 (LPBYTE)primaryWINS.String, &size);
681 addr = inet_addr(primaryWINS.String);
682 if (addr != INADDR_NONE && addr != INADDR_ANY)
683 winsEnabled = TRUE;
684 size = sizeof(secondaryWINS.String);
685 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
686 (LPBYTE)secondaryWINS.String, &size);
687 addr = inet_addr(secondaryWINS.String);
688 if (addr != INADDR_NONE && addr != INADDR_ANY)
689 winsEnabled = TRUE;
690 RegCloseKey(hKey);
692 for (ndx = 0; ndx < table->numIndexes; ndx++) {
693 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
694 DWORD i;
695 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
696 BOOL firstIPAddr = TRUE;
698 /* on Win98 this is left empty, but whatever */
699 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
700 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
701 ptr->AddressLength = sizeof(ptr->Address);
702 getInterfacePhysicalByIndex(table->indexes[ndx],
703 &ptr->AddressLength, ptr->Address, &ptr->Type);
704 ptr->Index = table->indexes[ndx];
705 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
706 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
707 if (firstIPAddr) {
708 toIPAddressString(ipAddrTable->table[i].dwAddr,
709 ptr->IpAddressList.IpAddress.String);
710 toIPAddressString(ipAddrTable->table[i].dwMask,
711 ptr->IpAddressList.IpMask.String);
712 firstIPAddr = FALSE;
714 else {
715 currentIPAddr->Next = nextIPAddr;
716 currentIPAddr = nextIPAddr;
717 toIPAddressString(ipAddrTable->table[i].dwAddr,
718 currentIPAddr->IpAddress.String);
719 toIPAddressString(ipAddrTable->table[i].dwMask,
720 currentIPAddr->IpMask.String);
721 nextIPAddr++;
725 /* If no IP was found it probably means that the interface is not
726 * configured. In this case we have to return a zeroed IP and mask. */
727 if (firstIPAddr) {
728 strcpy(ptr->IpAddressList.IpAddress.String, "0.0.0.0");
729 strcpy(ptr->IpAddressList.IpMask.String, "0.0.0.0");
731 /* Find first router through this interface, which we'll assume
732 * is the default gateway for this adapter */
733 for (i = 0; i < routeTable->dwNumEntries; i++)
734 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
735 && routeTable->table[i].u1.ForwardType ==
736 MIB_IPROUTE_TYPE_INDIRECT)
738 toIPAddressString(routeTable->table[i].dwForwardNextHop,
739 ptr->GatewayList.IpAddress.String);
740 toIPAddressString(routeTable->table[i].dwForwardMask,
741 ptr->GatewayList.IpMask.String);
743 if (winsEnabled) {
744 ptr->HaveWins = TRUE;
745 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
746 primaryWINS.String, sizeof(primaryWINS.String));
747 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
748 secondaryWINS.String, sizeof(secondaryWINS.String));
750 if (ndx < table->numIndexes - 1)
751 ptr->Next = &pAdapterInfo[ndx + 1];
752 else
753 ptr->Next = NULL;
755 ptr->DhcpEnabled = TRUE;
757 ret = NO_ERROR;
759 HeapFree(GetProcessHeap(), 0, table);
761 else
762 ret = ERROR_OUTOFMEMORY;
763 HeapFree(GetProcessHeap(), 0, routeTable);
764 HeapFree(GetProcessHeap(), 0, ipAddrTable);
767 else
768 ret = ERROR_NO_DATA;
770 TRACE("returning %d\n", ret);
771 return ret;
774 static DWORD typeFromMibType(DWORD mib_type)
776 switch (mib_type)
778 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
779 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
780 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
781 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
782 default: return IF_TYPE_OTHER;
786 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
788 switch (mib_type)
790 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
791 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
792 default: return NET_IF_CONNECTION_DEDICATED;
796 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs, DWORD **masks)
798 ULONG ret, i, j;
799 MIB_IPADDRTABLE *at;
801 *num_addrs = 0;
802 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
803 for (i = 0; i < at->dwNumEntries; i++)
805 if (at->table[i].dwIndex == index) (*num_addrs)++;
807 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
809 HeapFree(GetProcessHeap(), 0, at);
810 return ERROR_OUTOFMEMORY;
812 if (!(*masks = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
814 HeapFree(GetProcessHeap(), 0, *addrs);
815 HeapFree(GetProcessHeap(), 0, at);
816 return ERROR_OUTOFMEMORY;
818 for (i = 0, j = 0; i < at->dwNumEntries; i++)
820 if (at->table[i].dwIndex == index)
822 (*addrs)[j] = at->table[i].dwAddr;
823 (*masks)[j] = at->table[i].dwMask;
824 j++;
827 HeapFree(GetProcessHeap(), 0, at);
828 return ERROR_SUCCESS;
831 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
833 const BYTE *addrp;
834 char *p = buf;
836 for (addrp = (const BYTE *)in_addr;
837 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
838 addrp++)
840 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
841 sprintf(p, "%d", *addrp);
842 else
843 p += sprintf(p, "%d.", *addrp);
845 return buf;
848 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
850 DWORD i, num_gateways = 0;
852 for (i = 0; i < routeTable->dwNumEntries; i++)
854 if (routeTable->table[i].dwForwardIfIndex == index &&
855 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
856 num_gateways++;
858 return num_gateways;
861 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
862 PMIB_IPFORWARDTABLE routeTable)
864 DWORD i;
865 PMIB_IPFORWARDROW row = NULL;
867 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
869 if (routeTable->table[i].dwForwardIfIndex == index &&
870 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
871 row = &routeTable->table[i];
873 return row;
876 static void fill_unicast_addr_data(IP_ADAPTER_ADDRESSES *aa, IP_ADAPTER_UNICAST_ADDRESS *ua)
878 /* Actually this information should be read somewhere from the system
879 * but it doesn't matter much for the bugs found so far.
880 * This information is required for DirectPlay8 games. */
881 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK)
883 ua->PrefixOrigin = IpPrefixOriginDhcp;
884 ua->SuffixOrigin = IpSuffixOriginDhcp;
886 else
888 ua->PrefixOrigin = IpPrefixOriginManual;
889 ua->SuffixOrigin = IpSuffixOriginManual;
892 /* The address is not duplicated in the network */
893 ua->DadState = IpDadStatePreferred;
895 /* Some address life time values, required even for non-dhcp addresses */
896 ua->ValidLifetime = 60000;
897 ua->PreferredLifetime = 60000;
898 ua->LeaseLifetime = 60000;
901 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
902 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
904 ULONG ret = ERROR_SUCCESS, i, j, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
905 DWORD *v4addrs = NULL, *v4masks = NULL;
906 SOCKET_ADDRESS *v6addrs = NULL, *v6masks = NULL;
907 PMIB_IPFORWARDTABLE routeTable = NULL;
909 if (family == WS_AF_INET)
911 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
913 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
915 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
916 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
919 else if (family == WS_AF_INET6)
921 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
923 else if (family == WS_AF_UNSPEC)
925 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
927 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
929 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
930 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
932 if (!ret) ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
934 else
936 FIXME("address family %u unsupported\n", family);
937 ret = ERROR_NO_DATA;
939 if (ret)
941 HeapFree(GetProcessHeap(), 0, v4addrs);
942 HeapFree(GetProcessHeap(), 0, v4masks);
943 HeapFree(GetProcessHeap(), 0, v6addrs);
944 HeapFree(GetProcessHeap(), 0, v6masks);
945 HeapFree(GetProcessHeap(), 0, routeTable);
946 return ret;
949 total_size = sizeof(IP_ADAPTER_ADDRESSES);
950 total_size += IF_NAMESIZE;
951 total_size += IF_NAMESIZE * sizeof(WCHAR);
952 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
953 total_size += IF_NAMESIZE * sizeof(WCHAR);
954 if (flags & GAA_FLAG_INCLUDE_PREFIX)
956 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v4addrs;
957 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v6addrs;
958 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
959 for (i = 0; i < num_v6addrs; i++)
960 total_size += v6masks[i].iSockaddrLength;
962 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
963 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
964 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
965 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
966 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
967 for (i = 0; i < num_v6addrs; i++)
968 total_size += v6addrs[i].iSockaddrLength;
970 if (aa && *size >= total_size)
972 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
973 WCHAR *dst;
974 DWORD buflen, type;
975 INTERNAL_IF_OPER_STATUS status;
977 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
978 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
979 aa->u.s.IfIndex = index;
981 getInterfaceNameByIndex(index, name);
982 memcpy(ptr, name, IF_NAMESIZE);
983 aa->AdapterName = ptr;
984 ptr += IF_NAMESIZE;
985 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
987 aa->FriendlyName = (WCHAR *)ptr;
988 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
989 *dst = *src;
990 *dst++ = 0;
991 ptr = (char *)dst;
993 aa->Description = (WCHAR *)ptr;
994 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
995 *dst = *src;
996 *dst++ = 0;
997 ptr = (char *)dst;
999 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
1000 num_v6addrs);
1002 buflen = MAX_INTERFACE_PHYSADDR;
1003 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
1004 aa->PhysicalAddressLength = buflen;
1005 aa->IfType = typeFromMibType(type);
1006 aa->ConnectionType = connectionTypeFromMibType(type);
1007 aa->Luid.Info.NetLuidIndex = index;
1008 aa->Luid.Info.IfType = aa->IfType;
1010 if (num_v4_gateways)
1012 PMIB_IPFORWARDROW adapterRow;
1014 if ((adapterRow = findIPv4Gateway(index, routeTable)))
1016 PIP_ADAPTER_GATEWAY_ADDRESS gw;
1017 PSOCKADDR_IN sin;
1019 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
1020 aa->FirstGatewayAddress = gw;
1022 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1023 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1024 sin = (PSOCKADDR_IN)ptr;
1025 sin->sin_family = WS_AF_INET;
1026 sin->sin_port = 0;
1027 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
1028 sizeof(DWORD));
1029 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
1030 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1031 gw->Next = NULL;
1032 ptr += sizeof(SOCKADDR_IN);
1035 if (num_v4addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1037 IP_ADAPTER_UNICAST_ADDRESS *ua;
1038 struct WS_sockaddr_in *sa;
1039 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
1040 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1041 for (i = 0; i < num_v4addrs; i++)
1043 char addr_buf[16];
1045 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1046 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1047 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1048 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1050 sa = (struct WS_sockaddr_in *)ua->Address.lpSockaddr;
1051 sa->sin_family = WS_AF_INET;
1052 sa->sin_addr.S_un.S_addr = v4addrs[i];
1053 sa->sin_port = 0;
1054 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
1055 debugstr_ipv4(&sa->sin_addr.S_un.S_addr, addr_buf));
1056 fill_unicast_addr_data(aa, ua);
1058 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1059 if (i < num_v4addrs - 1)
1061 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1062 ua = ua->Next;
1066 if (num_v6addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1068 IP_ADAPTER_UNICAST_ADDRESS *ua;
1069 struct WS_sockaddr_in6 *sa;
1071 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
1072 if (aa->FirstUnicastAddress)
1074 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
1076 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1077 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1079 else
1080 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1081 for (i = 0; i < num_v6addrs; i++)
1083 char addr_buf[46];
1085 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1086 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1087 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
1088 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1090 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
1091 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
1092 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
1093 debugstr_ipv6(sa, addr_buf));
1094 fill_unicast_addr_data(aa, ua);
1096 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1097 if (i < num_v6addrs - 1)
1099 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1100 ua = ua->Next;
1104 if (num_v4addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1106 IP_ADAPTER_PREFIX *prefix;
1108 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1109 for (i = 0; i < num_v4addrs; i++)
1111 char addr_buf[16];
1112 struct WS_sockaddr_in *sa;
1114 prefix->u.s.Length = sizeof(*prefix);
1115 prefix->u.s.Flags = 0;
1116 prefix->Next = NULL;
1117 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1118 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1120 sa = (struct WS_sockaddr_in *)prefix->Address.lpSockaddr;
1121 sa->sin_family = WS_AF_INET;
1122 sa->sin_addr.S_un.S_addr = v4addrs[i] & v4masks[i];
1123 sa->sin_port = 0;
1125 prefix->PrefixLength = 0;
1126 for (j = 0; j < sizeof(*v4masks) * 8; j++)
1128 if (v4masks[i] & 1 << j) prefix->PrefixLength++;
1129 else break;
1131 TRACE("IPv4 network: %s/%u\n",
1132 debugstr_ipv4((const in_addr_t *)&sa->sin_addr.S_un.S_addr, addr_buf),
1133 prefix->PrefixLength);
1135 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1136 if (i < num_v4addrs - 1)
1138 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1139 prefix = prefix->Next;
1143 if (num_v6addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1145 IP_ADAPTER_PREFIX *prefix;
1147 if (aa->FirstPrefix)
1149 for (prefix = aa->FirstPrefix; prefix->Next; prefix = prefix->Next)
1151 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1152 prefix = (IP_ADAPTER_PREFIX *)ptr;
1154 else
1155 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1156 for (i = 0; i < num_v6addrs; i++)
1158 char addr_buf[46];
1159 struct WS_sockaddr_in6 *sa;
1160 const IN6_ADDR *addr, *mask;
1161 BOOL done = FALSE;
1162 ULONG k;
1164 prefix->u.s.Length = sizeof(*prefix);
1165 prefix->u.s.Flags = 0;
1166 prefix->Next = NULL;
1167 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in6);
1168 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1170 sa = (struct WS_sockaddr_in6 *)prefix->Address.lpSockaddr;
1171 sa->sin6_family = WS_AF_INET6;
1172 sa->sin6_port = 0;
1173 sa->sin6_flowinfo = 0;
1174 addr = &((struct WS_sockaddr_in6 *)v6addrs[i].lpSockaddr)->sin6_addr;
1175 mask = &((struct WS_sockaddr_in6 *)v6masks[i].lpSockaddr)->sin6_addr;
1176 for (j = 0; j < 8; j++) sa->sin6_addr.u.Word[j] = addr->u.Word[j] & mask->u.Word[j];
1177 sa->sin6_scope_id = 0;
1179 prefix->PrefixLength = 0;
1180 for (k = 0; k < 8 && !done; k++)
1182 for (j = 0; j < sizeof(WORD) * 8 && !done; j++)
1184 if (mask->u.Word[k] & 1 << j) prefix->PrefixLength++;
1185 else done = TRUE;
1188 TRACE("IPv6 network: %s/%u\n", debugstr_ipv6(sa, addr_buf), prefix->PrefixLength);
1190 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1191 if (i < num_v6addrs - 1)
1193 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1194 prefix = prefix->Next;
1199 getInterfaceMtuByName(name, &aa->Mtu);
1201 getInterfaceStatusByName(name, &status);
1202 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
1203 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
1204 else aa->OperStatus = IfOperStatusUnknown;
1206 *size = total_size;
1207 HeapFree(GetProcessHeap(), 0, routeTable);
1208 HeapFree(GetProcessHeap(), 0, v6addrs);
1209 HeapFree(GetProcessHeap(), 0, v6masks);
1210 HeapFree(GetProcessHeap(), 0, v4addrs);
1211 HeapFree(GetProcessHeap(), 0, v4masks);
1212 return ERROR_SUCCESS;
1215 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in *src )
1217 SOCKADDR_IN *s = (SOCKADDR_IN *)dst;
1219 s->sin_family = WS_AF_INET;
1220 s->sin_port = src->sin_port;
1221 memcpy( &s->sin_addr, &src->sin_addr, sizeof(IN_ADDR) );
1222 memset( (char *)s + FIELD_OFFSET( SOCKADDR_IN, sin_zero ), 0,
1223 sizeof(SOCKADDR_STORAGE) - FIELD_OFFSET( SOCKADDR_IN, sin_zero) );
1226 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in6 *src )
1228 SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)dst;
1230 s->sin6_family = WS_AF_INET6;
1231 s->sin6_port = src->sin6_port;
1232 s->sin6_flowinfo = src->sin6_flowinfo;
1233 memcpy( &s->sin6_addr, &src->sin6_addr, sizeof(IN6_ADDR) );
1234 s->sin6_scope_id = src->sin6_scope_id;
1235 memset( (char *)s + sizeof(SOCKADDR_IN6), 0,
1236 sizeof(SOCKADDR_STORAGE) - sizeof(SOCKADDR_IN6) );
1239 #ifdef HAVE_STRUCT___RES_STATE
1240 /* call res_init() just once because of a bug in Mac OS X 10.4 */
1241 /* Call once per thread on systems that have per-thread _res. */
1243 static CRITICAL_SECTION res_init_cs;
1244 static CRITICAL_SECTION_DEBUG res_init_cs_debug = {
1245 0, 0, &res_init_cs,
1246 { &res_init_cs_debug.ProcessLocksList, &res_init_cs_debug.ProcessLocksList },
1247 0, 0, { (DWORD_PTR)(__FILE__ ": res_init_cs") }
1249 static CRITICAL_SECTION res_init_cs = { &res_init_cs_debug, -1, 0, 0, 0, 0 };
1251 static void initialise_resolver(void)
1253 EnterCriticalSection(&res_init_cs);
1254 if ((_res.options & RES_INIT) == 0)
1255 res_init();
1256 LeaveCriticalSection(&res_init_cs);
1259 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1261 int i, ip6_count = 0;
1262 SOCKADDR_STORAGE *addr;
1264 initialise_resolver();
1266 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1267 ip6_count = _res._u._ext.nscount6;
1268 #endif
1270 if (!servers || !num)
1272 num = _res.nscount;
1273 if (ip4_only) num -= ip6_count;
1274 return num;
1277 for (i = 0, addr = servers; addr < (servers + num) && i < _res.nscount; i++)
1279 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1280 if (_res._u._ext.nsaddrs[i])
1282 if (ip4_only) continue;
1283 sockaddr_in6_to_WS_storage( addr, _res._u._ext.nsaddrs[i] );
1285 else
1286 #endif
1288 sockaddr_in_to_WS_storage( addr, _res.nsaddr_list + i );
1290 addr++;
1292 return addr - servers;
1294 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
1296 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1298 extern struct res_state *__res_get_state( void );
1299 extern int __res_getservers( struct res_state *, struct sockaddr_storage *, int );
1300 struct res_state *state = __res_get_state();
1301 int i, found = 0, total = __res_getservers( state, NULL, 0 );
1302 SOCKADDR_STORAGE *addr = servers;
1303 struct sockaddr_storage *buf;
1305 if ((!servers || !num) && !ip4_only) return total;
1307 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(struct sockaddr_storage) );
1308 total = __res_getservers( state, buf, total );
1310 for (i = 0; i < total; i++)
1312 if (buf[i].ss_family == AF_INET6 && ip4_only) continue;
1313 if (buf[i].ss_family != AF_INET && buf[i].ss_family != AF_INET6) continue;
1315 found++;
1316 if (!servers || !num) continue;
1318 if (buf[i].ss_family == AF_INET6)
1320 sockaddr_in6_to_WS_storage( addr, (struct sockaddr_in6 *)(buf + i) );
1322 else
1324 sockaddr_in_to_WS_storage( addr, (struct sockaddr_in *)(buf + i) );
1326 if (++addr >= servers + num) break;
1329 HeapFree( GetProcessHeap(), 0, buf );
1330 return found;
1332 #else
1334 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1336 FIXME("Unimplemented on this system\n");
1337 return 0;
1339 #endif
1341 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
1343 int num = get_dns_servers( NULL, 0, FALSE );
1344 DWORD size;
1346 size = num * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR_STORAGE));
1347 if (!address || *len < size)
1349 *len = size;
1350 return ERROR_BUFFER_OVERFLOW;
1352 *len = size;
1353 if (num > 0)
1355 PIP_ADAPTER_DNS_SERVER_ADDRESS addr = address;
1356 SOCKADDR_STORAGE *sock_addrs = (SOCKADDR_STORAGE *)(address + num);
1357 int i;
1359 get_dns_servers( sock_addrs, num, FALSE );
1361 for (i = 0; i < num; i++, addr = addr->Next)
1363 addr->u.s.Length = sizeof(*addr);
1364 if (sock_addrs[i].ss_family == WS_AF_INET6)
1365 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN6);
1366 else
1367 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1368 addr->Address.lpSockaddr = (SOCKADDR *)(sock_addrs + i);
1369 if (i == num - 1)
1370 addr->Next = NULL;
1371 else
1372 addr->Next = addr + 1;
1375 return ERROR_SUCCESS;
1378 #ifdef HAVE_STRUCT___RES_STATE
1379 static BOOL is_ip_address_string(const char *str)
1381 struct in_addr in;
1382 int ret;
1384 ret = inet_aton(str, &in);
1385 return ret != 0;
1387 #endif
1389 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1391 ULONG size;
1392 const char *found_suffix = "";
1393 /* Always return a NULL-terminated string, even if it's empty. */
1395 #ifdef HAVE_STRUCT___RES_STATE
1397 ULONG i;
1398 initialise_resolver();
1399 for (i = 0; !*found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1401 /* This uses a heuristic to select a DNS suffix:
1402 * the first, non-IP address string is selected.
1404 if (!is_ip_address_string(_res.dnsrch[i]))
1405 found_suffix = _res.dnsrch[i];
1408 #endif
1410 size = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, NULL, 0 ) * sizeof(WCHAR);
1411 if (!suffix || *len < size)
1413 *len = size;
1414 return ERROR_BUFFER_OVERFLOW;
1416 *len = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, suffix, *len / sizeof(WCHAR) ) * sizeof(WCHAR);
1417 return ERROR_SUCCESS;
1420 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1421 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1423 InterfaceIndexTable *table;
1424 ULONG i, size, dns_server_size = 0, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1426 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1428 if (!buflen) return ERROR_INVALID_PARAMETER;
1430 get_interface_indices( FALSE, &table );
1431 if (!table || !table->numIndexes)
1433 HeapFree(GetProcessHeap(), 0, table);
1434 return ERROR_NO_DATA;
1436 total_size = 0;
1437 for (i = 0; i < table->numIndexes; i++)
1439 size = 0;
1440 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1442 HeapFree(GetProcessHeap(), 0, table);
1443 return ret;
1445 total_size += size;
1447 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1449 /* Since DNS servers aren't really per adapter, get enough space for a
1450 * single copy of them.
1452 get_dns_server_addresses(NULL, &dns_server_size);
1453 total_size += dns_server_size;
1455 /* Since DNS suffix also isn't really per adapter, get enough space for a
1456 * single copy of it.
1458 get_dns_suffix(NULL, &dns_suffix_size);
1459 total_size += dns_suffix_size;
1460 if (aa && *buflen >= total_size)
1462 ULONG bytes_left = size = total_size;
1463 PIP_ADAPTER_ADDRESSES first_aa = aa;
1464 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1465 WCHAR *dnsSuffix;
1467 for (i = 0; i < table->numIndexes; i++)
1469 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1471 HeapFree(GetProcessHeap(), 0, table);
1472 return ret;
1474 if (i < table->numIndexes - 1)
1476 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1477 aa = aa->Next;
1478 size = bytes_left -= size;
1481 if (dns_server_size)
1483 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1484 get_dns_server_addresses(firstDns, &dns_server_size);
1485 for (aa = first_aa; aa; aa = aa->Next)
1487 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1488 aa->FirstDnsServerAddress = firstDns;
1491 aa = first_aa;
1492 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1493 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1494 for (; aa; aa = aa->Next)
1496 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1497 aa->DnsSuffix = dnsSuffix;
1498 else
1499 aa->DnsSuffix = dnsSuffix + dns_suffix_size / sizeof(WCHAR) - 1;
1501 ret = ERROR_SUCCESS;
1503 else
1505 ret = ERROR_BUFFER_OVERFLOW;
1506 *buflen = total_size;
1509 TRACE("num adapters %u\n", table->numIndexes);
1510 HeapFree(GetProcessHeap(), 0, table);
1511 return ret;
1514 /******************************************************************
1515 * GetBestInterface (IPHLPAPI.@)
1517 * Get the interface, with the best route for the given IP address.
1519 * PARAMS
1520 * dwDestAddr [In] IP address to search the interface for
1521 * pdwBestIfIndex [Out] found best interface
1523 * RETURNS
1524 * Success: NO_ERROR
1525 * Failure: error code from winerror.h
1527 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1529 struct WS_sockaddr_in sa_in;
1530 memset(&sa_in, 0, sizeof(sa_in));
1531 sa_in.sin_family = WS_AF_INET;
1532 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1533 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1536 /******************************************************************
1537 * GetBestInterfaceEx (IPHLPAPI.@)
1539 * Get the interface, with the best route for the given IP address.
1541 * PARAMS
1542 * dwDestAddr [In] IP address to search the interface for
1543 * pdwBestIfIndex [Out] found best interface
1545 * RETURNS
1546 * Success: NO_ERROR
1547 * Failure: error code from winerror.h
1549 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1551 DWORD ret;
1553 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1554 if (!pDestAddr || !pdwBestIfIndex)
1555 ret = ERROR_INVALID_PARAMETER;
1556 else {
1557 MIB_IPFORWARDROW ipRow;
1559 if (pDestAddr->sa_family == WS_AF_INET) {
1560 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1561 if (ret == ERROR_SUCCESS)
1562 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1563 } else {
1564 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1565 ret = ERROR_NOT_SUPPORTED;
1568 TRACE("returning %d\n", ret);
1569 return ret;
1573 /******************************************************************
1574 * GetBestRoute (IPHLPAPI.@)
1576 * Get the best route for the given IP address.
1578 * PARAMS
1579 * dwDestAddr [In] IP address to search the best route for
1580 * dwSourceAddr [In] optional source IP address
1581 * pBestRoute [Out] found best route
1583 * RETURNS
1584 * Success: NO_ERROR
1585 * Failure: error code from winerror.h
1587 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1589 PMIB_IPFORWARDTABLE table;
1590 DWORD ret;
1592 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1593 dwSourceAddr, pBestRoute);
1594 if (!pBestRoute)
1595 return ERROR_INVALID_PARAMETER;
1597 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1598 if (!ret) {
1599 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1601 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1602 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1603 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1604 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1605 DWORD numShifts, mask;
1607 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1608 mask && mask & 1; mask >>= 1, numShifts++)
1610 if (numShifts > matchedBits) {
1611 matchedBits = numShifts;
1612 matchedNdx = ndx;
1614 else if (!matchedBits) {
1615 matchedNdx = ndx;
1619 if (matchedNdx < table->dwNumEntries) {
1620 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1621 ret = ERROR_SUCCESS;
1623 else {
1624 /* No route matches, which can happen if there's no default route. */
1625 ret = ERROR_HOST_UNREACHABLE;
1627 HeapFree(GetProcessHeap(), 0, table);
1629 TRACE("returning %d\n", ret);
1630 return ret;
1634 /******************************************************************
1635 * GetFriendlyIfIndex (IPHLPAPI.@)
1637 * Get a "friendly" version of IfIndex, which is one that doesn't
1638 * have the top byte set. Doesn't validate whether IfIndex is a valid
1639 * adapter index.
1641 * PARAMS
1642 * IfIndex [In] interface index to get the friendly one for
1644 * RETURNS
1645 * A friendly version of IfIndex.
1647 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1649 /* windows doesn't validate these, either, just makes sure the top byte is
1650 cleared. I assume my ifenum module never gives an index with the top
1651 byte set. */
1652 TRACE("returning %d\n", IfIndex);
1653 return IfIndex;
1657 /******************************************************************
1658 * GetIfEntry (IPHLPAPI.@)
1660 * Get information about an interface.
1662 * PARAMS
1663 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1664 * Out: interface information
1666 * RETURNS
1667 * Success: NO_ERROR
1668 * Failure: error code from winerror.h
1670 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1672 DWORD ret;
1673 char nameBuf[MAX_ADAPTER_NAME];
1674 char *name;
1676 TRACE("pIfRow %p\n", pIfRow);
1677 if (!pIfRow)
1678 return ERROR_INVALID_PARAMETER;
1680 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1681 if (name) {
1682 ret = getInterfaceEntryByName(name, pIfRow);
1683 if (ret == NO_ERROR)
1684 ret = getInterfaceStatsByName(name, pIfRow);
1686 else
1687 ret = ERROR_INVALID_DATA;
1688 TRACE("returning %d\n", ret);
1689 return ret;
1692 /******************************************************************
1693 * GetIfEntry2 (IPHLPAPI.@)
1695 DWORD WINAPI GetIfEntry2( MIB_IF_ROW2 *row2 )
1697 DWORD ret, len = sizeof(row2->Description)/sizeof(row2->Description[0]);
1698 char buf[MAX_ADAPTER_NAME], *name;
1699 MIB_IFROW row;
1701 TRACE("%p\n", row2);
1703 if (!row2 || (!(name = getInterfaceNameByIndex( row2->InterfaceIndex, buf )) &&
1704 !(name = getInterfaceNameByIndex( row2->InterfaceLuid.Info.NetLuidIndex, buf ))))
1706 return ERROR_INVALID_PARAMETER;
1708 if ((ret = getInterfaceEntryByName( name, &row ))) return ret;
1709 if ((ret = getInterfaceStatsByName( name, &row ))) return ret;
1711 memset( row2, 0, sizeof(*row2) );
1712 row2->InterfaceLuid.Info.Reserved = 0;
1713 row2->InterfaceLuid.Info.NetLuidIndex = row.dwIndex;
1714 row2->InterfaceLuid.Info.IfType = row.dwType;
1715 row2->InterfaceIndex = row.dwIndex;
1716 row2->InterfaceGuid.Data1 = row.dwIndex;
1717 row2->Type = row.dwType;
1718 row2->Mtu = row.dwMtu;
1719 MultiByteToWideChar( CP_UNIXCP, 0, (const char *)row.bDescr, -1, row2->Description, len );
1720 row2->PhysicalAddressLength = row.dwPhysAddrLen;
1721 memcpy( &row2->PhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1722 memcpy( &row2->PermanentPhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1723 row2->OperStatus = IfOperStatusUp;
1724 row2->AdminStatus = NET_IF_ADMIN_STATUS_UP;
1725 row2->MediaConnectState = MediaConnectStateConnected;
1726 row2->ConnectionType = NET_IF_CONNECTION_DEDICATED;
1728 /* stats */
1729 row2->InOctets = row.dwInOctets;
1730 row2->InUcastPkts = row.dwInUcastPkts;
1731 row2->InNUcastPkts = row.dwInNUcastPkts;
1732 row2->InDiscards = row.dwInDiscards;
1733 row2->InErrors = row.dwInErrors;
1734 row2->InUnknownProtos = row.dwInUnknownProtos;
1735 row2->OutOctets = row.dwOutOctets;
1736 row2->OutUcastPkts = row.dwOutUcastPkts;
1737 row2->OutNUcastPkts = row.dwOutNUcastPkts;
1738 row2->OutDiscards = row.dwOutDiscards;
1739 row2->OutErrors = row.dwOutErrors;
1741 return NO_ERROR;
1744 static int IfTableSorter(const void *a, const void *b)
1746 int ret;
1748 if (a && b)
1749 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1750 else
1751 ret = 0;
1752 return ret;
1756 /******************************************************************
1757 * GetIfTable (IPHLPAPI.@)
1759 * Get a table of local interfaces.
1761 * PARAMS
1762 * pIfTable [Out] buffer for local interfaces table
1763 * pdwSize [In/Out] length of output buffer
1764 * bOrder [In] whether to sort the table
1766 * RETURNS
1767 * Success: NO_ERROR
1768 * Failure: error code from winerror.h
1770 * NOTES
1771 * If pdwSize is less than required, the function will return
1772 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1773 * size.
1774 * If bOrder is true, the returned table will be sorted by interface index.
1776 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1778 DWORD ret;
1780 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1781 (DWORD)bOrder);
1782 if (!pdwSize)
1783 ret = ERROR_INVALID_PARAMETER;
1784 else {
1785 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1786 ULONG size = sizeof(MIB_IFTABLE);
1788 if (numInterfaces > 1)
1789 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1790 if (!pIfTable || *pdwSize < size) {
1791 *pdwSize = size;
1792 ret = ERROR_INSUFFICIENT_BUFFER;
1794 else {
1795 InterfaceIndexTable *table;
1796 get_interface_indices( FALSE, &table );
1798 if (table) {
1799 size = sizeof(MIB_IFTABLE);
1800 if (table->numIndexes > 1)
1801 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1802 if (*pdwSize < size) {
1803 *pdwSize = size;
1804 ret = ERROR_INSUFFICIENT_BUFFER;
1806 else {
1807 DWORD ndx;
1809 *pdwSize = size;
1810 pIfTable->dwNumEntries = 0;
1811 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1812 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1813 GetIfEntry(&pIfTable->table[ndx]);
1814 pIfTable->dwNumEntries++;
1816 if (bOrder)
1817 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1818 IfTableSorter);
1819 ret = NO_ERROR;
1821 HeapFree(GetProcessHeap(), 0, table);
1823 else
1824 ret = ERROR_OUTOFMEMORY;
1827 TRACE("returning %d\n", ret);
1828 return ret;
1831 /******************************************************************
1832 * GetIfTable2 (IPHLPAPI.@)
1834 DWORD WINAPI GetIfTable2( MIB_IF_TABLE2 **table )
1836 DWORD i, nb_interfaces, size = sizeof(MIB_IF_TABLE2);
1837 InterfaceIndexTable *index_table;
1838 MIB_IF_TABLE2 *ret;
1840 TRACE( "table %p\n", table );
1842 if (!table) return ERROR_INVALID_PARAMETER;
1844 if ((nb_interfaces = get_interface_indices( FALSE, NULL )) > 1)
1845 size += (nb_interfaces - 1) * sizeof(MIB_IF_ROW2);
1847 if (!(ret = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
1849 get_interface_indices( FALSE, &index_table );
1850 if (!index_table)
1852 HeapFree( GetProcessHeap(), 0, ret );
1853 return ERROR_OUTOFMEMORY;
1856 ret->NumEntries = 0;
1857 for (i = 0; i < index_table->numIndexes; i++)
1859 ret->Table[i].InterfaceIndex = index_table->indexes[i];
1860 GetIfEntry2( &ret->Table[i] );
1861 ret->NumEntries++;
1864 HeapFree( GetProcessHeap(), 0, index_table );
1865 *table = ret;
1866 return NO_ERROR;
1869 /******************************************************************
1870 * GetInterfaceInfo (IPHLPAPI.@)
1872 * Get a list of network interface adapters.
1874 * PARAMS
1875 * pIfTable [Out] buffer for interface adapters
1876 * dwOutBufLen [Out] if buffer is too small, returns required size
1878 * RETURNS
1879 * Success: NO_ERROR
1880 * Failure: error code from winerror.h
1882 * BUGS
1883 * MSDN states this should return non-loopback interfaces only.
1885 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1887 DWORD ret;
1889 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1890 if (!dwOutBufLen)
1891 ret = ERROR_INVALID_PARAMETER;
1892 else {
1893 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1894 ULONG size = sizeof(IP_INTERFACE_INFO);
1896 if (numInterfaces > 1)
1897 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1898 if (!pIfTable || *dwOutBufLen < size) {
1899 *dwOutBufLen = size;
1900 ret = ERROR_INSUFFICIENT_BUFFER;
1902 else {
1903 InterfaceIndexTable *table;
1904 get_interface_indices( FALSE, &table );
1906 if (table) {
1907 size = sizeof(IP_INTERFACE_INFO);
1908 if (table->numIndexes > 1)
1909 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1910 if (*dwOutBufLen < size) {
1911 *dwOutBufLen = size;
1912 ret = ERROR_INSUFFICIENT_BUFFER;
1914 else {
1915 DWORD ndx;
1916 char nameBuf[MAX_ADAPTER_NAME];
1918 *dwOutBufLen = size;
1919 pIfTable->NumAdapters = 0;
1920 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1921 const char *walker, *name;
1922 WCHAR *assigner;
1924 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1925 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1926 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1927 walker && *walker &&
1928 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1929 walker++, assigner++)
1930 *assigner = *walker;
1931 *assigner = 0;
1932 pIfTable->NumAdapters++;
1934 ret = NO_ERROR;
1936 HeapFree(GetProcessHeap(), 0, table);
1938 else
1939 ret = ERROR_OUTOFMEMORY;
1942 TRACE("returning %d\n", ret);
1943 return ret;
1947 /******************************************************************
1948 * GetIpAddrTable (IPHLPAPI.@)
1950 * Get interface-to-IP address mapping table.
1952 * PARAMS
1953 * pIpAddrTable [Out] buffer for mapping table
1954 * pdwSize [In/Out] length of output buffer
1955 * bOrder [In] whether to sort the table
1957 * RETURNS
1958 * Success: NO_ERROR
1959 * Failure: error code from winerror.h
1961 * NOTES
1962 * If pdwSize is less than required, the function will return
1963 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1964 * size.
1965 * If bOrder is true, the returned table will be sorted by the next hop and
1966 * an assortment of arbitrary parameters.
1968 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1970 DWORD ret;
1972 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1973 (DWORD)bOrder);
1974 if (!pdwSize)
1975 ret = ERROR_INVALID_PARAMETER;
1976 else {
1977 PMIB_IPADDRTABLE table;
1979 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
1980 if (ret == NO_ERROR)
1982 ULONG size = FIELD_OFFSET(MIB_IPADDRTABLE, table[table->dwNumEntries]);
1984 if (!pIpAddrTable || *pdwSize < size) {
1985 *pdwSize = size;
1986 ret = ERROR_INSUFFICIENT_BUFFER;
1988 else {
1989 *pdwSize = size;
1990 memcpy(pIpAddrTable, table, size);
1991 if (bOrder)
1992 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
1993 sizeof(MIB_IPADDRROW), IpAddrTableSorter);
1994 ret = NO_ERROR;
1996 HeapFree(GetProcessHeap(), 0, table);
1999 TRACE("returning %d\n", ret);
2000 return ret;
2004 /******************************************************************
2005 * GetIpForwardTable (IPHLPAPI.@)
2007 * Get the route table.
2009 * PARAMS
2010 * pIpForwardTable [Out] buffer for route table
2011 * pdwSize [In/Out] length of output buffer
2012 * bOrder [In] whether to sort the table
2014 * RETURNS
2015 * Success: NO_ERROR
2016 * Failure: error code from winerror.h
2018 * NOTES
2019 * If pdwSize is less than required, the function will return
2020 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2021 * size.
2022 * If bOrder is true, the returned table will be sorted by the next hop and
2023 * an assortment of arbitrary parameters.
2025 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
2027 DWORD ret;
2028 PMIB_IPFORWARDTABLE table;
2030 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
2032 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2034 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
2035 if (!ret) {
2036 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
2037 if (!pIpForwardTable || *pdwSize < size) {
2038 *pdwSize = size;
2039 ret = ERROR_INSUFFICIENT_BUFFER;
2041 else {
2042 *pdwSize = size;
2043 memcpy(pIpForwardTable, table, size);
2045 HeapFree(GetProcessHeap(), 0, table);
2047 TRACE("returning %d\n", ret);
2048 return ret;
2052 /******************************************************************
2053 * GetIpNetTable (IPHLPAPI.@)
2055 * Get the IP-to-physical address mapping table.
2057 * PARAMS
2058 * pIpNetTable [Out] buffer for mapping table
2059 * pdwSize [In/Out] length of output buffer
2060 * bOrder [In] whether to sort the table
2062 * RETURNS
2063 * Success: NO_ERROR
2064 * Failure: error code from winerror.h
2066 * NOTES
2067 * If pdwSize is less than required, the function will return
2068 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2069 * size.
2070 * If bOrder is true, the returned table will be sorted by IP address.
2072 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
2074 DWORD ret;
2075 PMIB_IPNETTABLE table;
2077 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
2079 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2081 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
2082 if (!ret) {
2083 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
2084 if (!pIpNetTable || *pdwSize < size) {
2085 *pdwSize = size;
2086 ret = ERROR_INSUFFICIENT_BUFFER;
2088 else {
2089 *pdwSize = size;
2090 memcpy(pIpNetTable, table, size);
2092 HeapFree(GetProcessHeap(), 0, table);
2094 TRACE("returning %d\n", ret);
2095 return ret;
2098 /* Gets the DNS server list into the list beginning at list. Assumes that
2099 * a single server address may be placed at list if *len is at least
2100 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
2101 * and assumes that all remaining DNS servers are contiguously located
2102 * beginning at firstDynamic. On input, *len is assumed to be the total number
2103 * of bytes available for all DNS servers, and is ignored if list is NULL.
2104 * On return, *len is set to the total number of bytes required for all DNS
2105 * servers.
2106 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
2107 * ERROR_SUCCESS otherwise.
2109 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
2110 PIP_ADDR_STRING firstDynamic, DWORD *len)
2112 DWORD size;
2113 int num = get_dns_servers( NULL, 0, TRUE );
2115 size = num * sizeof(IP_ADDR_STRING);
2116 if (!list || *len < size) {
2117 *len = size;
2118 return ERROR_BUFFER_OVERFLOW;
2120 *len = size;
2121 if (num > 0) {
2122 PIP_ADDR_STRING ptr;
2123 int i;
2124 SOCKADDR_STORAGE *addr = HeapAlloc( GetProcessHeap(), 0, num * sizeof(SOCKADDR_STORAGE) );
2126 get_dns_servers( addr, num, TRUE );
2128 for (i = 0, ptr = list; i < num; i++, ptr = ptr->Next) {
2129 toIPAddressString(((struct sockaddr_in *)(addr + i))->sin_addr.s_addr,
2130 ptr->IpAddress.String);
2131 if (i == num - 1)
2132 ptr->Next = NULL;
2133 else if (i == 0)
2134 ptr->Next = firstDynamic;
2135 else
2136 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
2138 HeapFree( GetProcessHeap(), 0, addr );
2140 return ERROR_SUCCESS;
2143 /******************************************************************
2144 * GetNetworkParams (IPHLPAPI.@)
2146 * Get the network parameters for the local computer.
2148 * PARAMS
2149 * pFixedInfo [Out] buffer for network parameters
2150 * pOutBufLen [In/Out] length of output buffer
2152 * RETURNS
2153 * Success: NO_ERROR
2154 * Failure: error code from winerror.h
2156 * NOTES
2157 * If pOutBufLen is less than required, the function will return
2158 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
2159 * size.
2161 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
2163 DWORD ret, size, serverListSize;
2164 LONG regReturn;
2165 HKEY hKey;
2167 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
2168 if (!pOutBufLen)
2169 return ERROR_INVALID_PARAMETER;
2171 get_dns_server_list(NULL, NULL, &serverListSize);
2172 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
2173 if (!pFixedInfo || *pOutBufLen < size) {
2174 *pOutBufLen = size;
2175 return ERROR_BUFFER_OVERFLOW;
2178 memset(pFixedInfo, 0, size);
2179 size = sizeof(pFixedInfo->HostName);
2180 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
2181 size = sizeof(pFixedInfo->DomainName);
2182 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
2183 get_dns_server_list(&pFixedInfo->DnsServerList,
2184 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
2185 &serverListSize);
2186 /* Assume the first DNS server in the list is the "current" DNS server: */
2187 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
2188 pFixedInfo->NodeType = HYBRID_NODETYPE;
2189 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2190 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
2191 if (regReturn != ERROR_SUCCESS)
2192 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2193 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
2194 &hKey);
2195 if (regReturn == ERROR_SUCCESS)
2197 DWORD size = sizeof(pFixedInfo->ScopeId);
2199 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
2200 RegCloseKey(hKey);
2203 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
2204 I suppose could also check for a listener on port 53 to set EnableDns */
2205 ret = NO_ERROR;
2206 TRACE("returning %d\n", ret);
2207 return ret;
2211 /******************************************************************
2212 * GetNumberOfInterfaces (IPHLPAPI.@)
2214 * Get the number of interfaces.
2216 * PARAMS
2217 * pdwNumIf [Out] number of interfaces
2219 * RETURNS
2220 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
2222 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
2224 DWORD ret;
2226 TRACE("pdwNumIf %p\n", pdwNumIf);
2227 if (!pdwNumIf)
2228 ret = ERROR_INVALID_PARAMETER;
2229 else {
2230 *pdwNumIf = get_interface_indices( FALSE, NULL );
2231 ret = NO_ERROR;
2233 TRACE("returning %d\n", ret);
2234 return ret;
2238 /******************************************************************
2239 * GetPerAdapterInfo (IPHLPAPI.@)
2241 * Get information about an adapter corresponding to an interface.
2243 * PARAMS
2244 * IfIndex [In] interface info
2245 * pPerAdapterInfo [Out] buffer for per adapter info
2246 * pOutBufLen [In/Out] length of output buffer
2248 * RETURNS
2249 * Success: NO_ERROR
2250 * Failure: error code from winerror.h
2252 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
2254 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
2255 DWORD ret = NO_ERROR;
2257 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
2259 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
2261 if (!isIfIndexLoopback(IfIndex)) {
2262 get_dns_server_list(NULL, NULL, &serverListSize);
2263 if (serverListSize > sizeof(IP_ADDR_STRING))
2264 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
2266 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
2268 *pOutBufLen = bytesNeeded;
2269 return ERROR_BUFFER_OVERFLOW;
2272 memset(pPerAdapterInfo, 0, bytesNeeded);
2273 if (!isIfIndexLoopback(IfIndex)) {
2274 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
2275 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
2276 &serverListSize);
2277 /* Assume the first DNS server in the list is the "current" DNS server: */
2278 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
2280 return ret;
2284 /******************************************************************
2285 * GetRTTAndHopCount (IPHLPAPI.@)
2287 * Get round-trip time (RTT) and hop count.
2289 * PARAMS
2291 * DestIpAddress [In] destination address to get the info for
2292 * HopCount [Out] retrieved hop count
2293 * MaxHops [In] maximum hops to search for the destination
2294 * RTT [Out] RTT in milliseconds
2296 * RETURNS
2297 * Success: TRUE
2298 * Failure: FALSE
2300 * FIXME
2301 * Stub, returns FALSE.
2303 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
2305 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
2306 DestIpAddress, HopCount, MaxHops, RTT);
2307 return FALSE;
2311 /******************************************************************
2312 * GetTcpTable (IPHLPAPI.@)
2314 * Get the table of active TCP connections.
2316 * PARAMS
2317 * pTcpTable [Out] buffer for TCP connections table
2318 * pdwSize [In/Out] length of output buffer
2319 * bOrder [In] whether to order the table
2321 * RETURNS
2322 * Success: NO_ERROR
2323 * Failure: error code from winerror.h
2325 * NOTES
2326 * If pdwSize is less than required, the function will return
2327 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2328 * the required byte size.
2329 * If bOrder is true, the returned table will be sorted, first by
2330 * local address and port number, then by remote address and port
2331 * number.
2333 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
2335 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
2336 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, WS_AF_INET, TCP_TABLE_BASIC_ALL, 0);
2339 /******************************************************************
2340 * GetExtendedTcpTable (IPHLPAPI.@)
2342 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
2343 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
2345 DWORD ret, size;
2346 void *table;
2348 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2349 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2351 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2353 if (ulAf != WS_AF_INET)
2355 FIXME("ulAf = %u not supported\n", ulAf);
2356 return ERROR_NOT_SUPPORTED;
2358 if (TableClass >= TCP_TABLE_OWNER_MODULE_LISTENER)
2359 FIXME("module classes not fully supported\n");
2361 if ((ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2362 return ret;
2364 if (!pTcpTable || *pdwSize < size)
2366 *pdwSize = size;
2367 ret = ERROR_INSUFFICIENT_BUFFER;
2369 else
2371 *pdwSize = size;
2372 memcpy(pTcpTable, table, size);
2374 HeapFree(GetProcessHeap(), 0, table);
2375 return ret;
2378 /******************************************************************
2379 * GetUdpTable (IPHLPAPI.@)
2381 * Get a table of active UDP connections.
2383 * PARAMS
2384 * pUdpTable [Out] buffer for UDP connections table
2385 * pdwSize [In/Out] length of output buffer
2386 * bOrder [In] whether to order the table
2388 * RETURNS
2389 * Success: NO_ERROR
2390 * Failure: error code from winerror.h
2392 * NOTES
2393 * If pdwSize is less than required, the function will return
2394 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2395 * required byte size.
2396 * If bOrder is true, the returned table will be sorted, first by
2397 * local address, then by local port number.
2399 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2401 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, WS_AF_INET, UDP_TABLE_BASIC, 0);
2404 /******************************************************************
2405 * GetExtendedUdpTable (IPHLPAPI.@)
2407 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
2408 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
2410 DWORD ret, size;
2411 void *table;
2413 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2414 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2416 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2418 if (ulAf != WS_AF_INET)
2420 FIXME("ulAf = %u not supported\n", ulAf);
2421 return ERROR_NOT_SUPPORTED;
2423 if (TableClass == UDP_TABLE_OWNER_MODULE)
2424 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2426 if ((ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2427 return ret;
2429 if (!pUdpTable || *pdwSize < size)
2431 *pdwSize = size;
2432 ret = ERROR_INSUFFICIENT_BUFFER;
2434 else
2436 *pdwSize = size;
2437 memcpy(pUdpTable, table, size);
2439 HeapFree(GetProcessHeap(), 0, table);
2440 return ret;
2443 /******************************************************************
2444 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2446 * This is a Win98-only function to get information on "unidirectional"
2447 * adapters. Since this is pretty nonsensical in other contexts, it
2448 * never returns anything.
2450 * PARAMS
2451 * pIPIfInfo [Out] buffer for adapter infos
2452 * dwOutBufLen [Out] length of the output buffer
2454 * RETURNS
2455 * Success: NO_ERROR
2456 * Failure: error code from winerror.h
2458 * FIXME
2459 * Stub, returns ERROR_NOT_SUPPORTED.
2461 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
2463 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
2464 /* a unidirectional adapter?? not bloody likely! */
2465 return ERROR_NOT_SUPPORTED;
2469 /******************************************************************
2470 * IpReleaseAddress (IPHLPAPI.@)
2472 * Release an IP obtained through DHCP,
2474 * PARAMS
2475 * AdapterInfo [In] adapter to release IP address
2477 * RETURNS
2478 * Success: NO_ERROR
2479 * Failure: error code from winerror.h
2481 * NOTES
2482 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2483 * this function does nothing.
2485 * FIXME
2486 * Stub, returns ERROR_NOT_SUPPORTED.
2488 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2490 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2491 return ERROR_NOT_SUPPORTED;
2495 /******************************************************************
2496 * IpRenewAddress (IPHLPAPI.@)
2498 * Renew an IP obtained through DHCP.
2500 * PARAMS
2501 * AdapterInfo [In] adapter to renew IP address
2503 * RETURNS
2504 * Success: NO_ERROR
2505 * Failure: error code from winerror.h
2507 * NOTES
2508 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2509 * this function does nothing.
2511 * FIXME
2512 * Stub, returns ERROR_NOT_SUPPORTED.
2514 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2516 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2517 return ERROR_NOT_SUPPORTED;
2521 /******************************************************************
2522 * NotifyAddrChange (IPHLPAPI.@)
2524 * Notify caller whenever the ip-interface map is changed.
2526 * PARAMS
2527 * Handle [Out] handle usable in asynchronous notification
2528 * overlapped [In] overlapped structure that notifies the caller
2530 * RETURNS
2531 * Success: NO_ERROR
2532 * Failure: error code from winerror.h
2534 * FIXME
2535 * Stub, returns ERROR_NOT_SUPPORTED.
2537 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2539 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2540 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2541 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2542 return ERROR_IO_PENDING;
2546 /******************************************************************
2547 * NotifyIpInterfaceChange (IPHLPAPI.@)
2549 DWORD WINAPI NotifyIpInterfaceChange(ULONG family, PVOID callback, PVOID context,
2550 BOOLEAN init_notify, PHANDLE handle)
2552 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2553 family, callback, context, init_notify, handle);
2554 if (handle) *handle = NULL;
2555 return ERROR_NOT_SUPPORTED;
2559 /******************************************************************
2560 * NotifyRouteChange (IPHLPAPI.@)
2562 * Notify caller whenever the ip routing table is changed.
2564 * PARAMS
2565 * Handle [Out] handle usable in asynchronous notification
2566 * overlapped [In] overlapped structure that notifies the caller
2568 * RETURNS
2569 * Success: NO_ERROR
2570 * Failure: error code from winerror.h
2572 * FIXME
2573 * Stub, returns ERROR_NOT_SUPPORTED.
2575 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2577 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2578 return ERROR_NOT_SUPPORTED;
2582 /******************************************************************
2583 * SendARP (IPHLPAPI.@)
2585 * Send an ARP request.
2587 * PARAMS
2588 * DestIP [In] attempt to obtain this IP
2589 * SrcIP [In] optional sender IP address
2590 * pMacAddr [Out] buffer for the mac address
2591 * PhyAddrLen [In/Out] length of the output buffer
2593 * RETURNS
2594 * Success: NO_ERROR
2595 * Failure: error code from winerror.h
2597 * FIXME
2598 * Stub, returns ERROR_NOT_SUPPORTED.
2600 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2602 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2603 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2604 return ERROR_NOT_SUPPORTED;
2608 /******************************************************************
2609 * SetIfEntry (IPHLPAPI.@)
2611 * Set the administrative status of an interface.
2613 * PARAMS
2614 * pIfRow [In] dwAdminStatus member specifies the new status.
2616 * RETURNS
2617 * Success: NO_ERROR
2618 * Failure: error code from winerror.h
2620 * FIXME
2621 * Stub, returns ERROR_NOT_SUPPORTED.
2623 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2625 FIXME("(pIfRow %p): stub\n", pIfRow);
2626 /* this is supposed to set an interface administratively up or down.
2627 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2628 this sort of down is indistinguishable from other sorts of down (e.g. no
2629 link). */
2630 return ERROR_NOT_SUPPORTED;
2634 /******************************************************************
2635 * SetIpForwardEntry (IPHLPAPI.@)
2637 * Modify an existing route.
2639 * PARAMS
2640 * pRoute [In] route with the new information
2642 * RETURNS
2643 * Success: NO_ERROR
2644 * Failure: error code from winerror.h
2646 * FIXME
2647 * Stub, returns NO_ERROR.
2649 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2651 FIXME("(pRoute %p): stub\n", pRoute);
2652 /* this is to add a route entry, how's it distinguishable from
2653 CreateIpForwardEntry?
2654 could use SIOCADDRT, not sure I want to */
2655 return 0;
2659 /******************************************************************
2660 * SetIpNetEntry (IPHLPAPI.@)
2662 * Modify an existing ARP entry.
2664 * PARAMS
2665 * pArpEntry [In] ARP entry with the new information
2667 * RETURNS
2668 * Success: NO_ERROR
2669 * Failure: error code from winerror.h
2671 * FIXME
2672 * Stub, returns NO_ERROR.
2674 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2676 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2677 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2678 return 0;
2682 /******************************************************************
2683 * SetIpStatistics (IPHLPAPI.@)
2685 * Toggle IP forwarding and det the default TTL value.
2687 * PARAMS
2688 * pIpStats [In] IP statistics with the new information
2690 * RETURNS
2691 * Success: NO_ERROR
2692 * Failure: error code from winerror.h
2694 * FIXME
2695 * Stub, returns NO_ERROR.
2697 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2699 FIXME("(pIpStats %p): stub\n", pIpStats);
2700 return 0;
2704 /******************************************************************
2705 * SetIpTTL (IPHLPAPI.@)
2707 * Set the default TTL value.
2709 * PARAMS
2710 * nTTL [In] new TTL value
2712 * RETURNS
2713 * Success: NO_ERROR
2714 * Failure: error code from winerror.h
2716 * FIXME
2717 * Stub, returns NO_ERROR.
2719 DWORD WINAPI SetIpTTL(UINT nTTL)
2721 FIXME("(nTTL %d): stub\n", nTTL);
2722 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2723 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2724 return 0;
2728 /******************************************************************
2729 * SetTcpEntry (IPHLPAPI.@)
2731 * Set the state of a TCP connection.
2733 * PARAMS
2734 * pTcpRow [In] specifies connection with new state
2736 * RETURNS
2737 * Success: NO_ERROR
2738 * Failure: error code from winerror.h
2740 * FIXME
2741 * Stub, returns NO_ERROR.
2743 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2745 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2746 return 0;
2749 /******************************************************************
2750 * SetPerTcpConnectionEStats (IPHLPAPI.@)
2752 DWORD WINAPI SetPerTcpConnectionEStats(PMIB_TCPROW row, TCP_ESTATS_TYPE state, PBYTE rw,
2753 ULONG version, ULONG size, ULONG offset)
2755 FIXME("(row %p, state %d, rw %p, version %u, size %u, offset %u): stub\n",
2756 row, state, rw, version, size, offset);
2757 return ERROR_NOT_SUPPORTED;
2761 /******************************************************************
2762 * UnenableRouter (IPHLPAPI.@)
2764 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2765 * if it reaches zero.
2767 * PARAMS
2768 * pOverlapped [In/Out] should be the same as in EnableRouter()
2769 * lpdwEnableCount [Out] optional, receives reference count
2771 * RETURNS
2772 * Success: NO_ERROR
2773 * Failure: error code from winerror.h
2775 * FIXME
2776 * Stub, returns ERROR_NOT_SUPPORTED.
2778 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2780 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2781 lpdwEnableCount);
2782 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2783 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2785 return ERROR_NOT_SUPPORTED;
2788 /******************************************************************
2789 * PfCreateInterface (IPHLPAPI.@)
2791 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2792 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2794 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2795 return ERROR_CALL_NOT_IMPLEMENTED;
2798 /******************************************************************
2799 * PfUnBindInterface (IPHLPAPI.@)
2801 DWORD WINAPI PfUnBindInterface(INTERFACE_HANDLE interface)
2803 FIXME("(%p) stub\n", interface);
2804 return ERROR_CALL_NOT_IMPLEMENTED;
2807 /******************************************************************
2808 * PfDeleteInterface(IPHLPAPI.@)
2810 DWORD WINAPI PfDeleteInterface(INTERFACE_HANDLE interface)
2812 FIXME("(%p) stub\n", interface);
2813 return ERROR_CALL_NOT_IMPLEMENTED;
2816 /******************************************************************
2817 * PfBindInterfaceToIPAddress(IPHLPAPI.@)
2819 DWORD WINAPI PfBindInterfaceToIPAddress(INTERFACE_HANDLE interface, PFADDRESSTYPE type, PBYTE ip)
2821 FIXME("(%p %d %p) stub\n", interface, type, ip);
2822 return ERROR_CALL_NOT_IMPLEMENTED;
2825 /******************************************************************
2826 * GetTcpTable2 (IPHLPAPI.@)
2828 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
2830 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
2831 return ERROR_NOT_SUPPORTED;
2834 /******************************************************************
2835 * GetTcp6Table (IPHLPAPI.@)
2837 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
2839 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table, size, order);
2840 return ERROR_NOT_SUPPORTED;
2843 /******************************************************************
2844 * GetTcp6Table2 (IPHLPAPI.@)
2846 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
2848 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
2849 return ERROR_NOT_SUPPORTED;
2852 /******************************************************************
2853 * ConvertInterfaceGuidToLuid (IPHLPAPI.@)
2855 DWORD WINAPI ConvertInterfaceGuidToLuid(const GUID *guid, NET_LUID *luid)
2857 DWORD ret;
2858 MIB_IFROW row;
2860 TRACE("(%s %p)\n", debugstr_guid(guid), luid);
2862 if (!guid || !luid) return ERROR_INVALID_PARAMETER;
2864 row.dwIndex = guid->Data1;
2865 if ((ret = GetIfEntry( &row ))) return ret;
2867 luid->Info.Reserved = 0;
2868 luid->Info.NetLuidIndex = guid->Data1;
2869 luid->Info.IfType = row.dwType;
2870 return NO_ERROR;
2873 /******************************************************************
2874 * ConvertInterfaceIndexToLuid (IPHLPAPI.@)
2876 DWORD WINAPI ConvertInterfaceIndexToLuid(NET_IFINDEX index, NET_LUID *luid)
2878 MIB_IFROW row;
2880 TRACE("(%u %p)\n", index, luid);
2882 if (!luid) return ERROR_INVALID_PARAMETER;
2883 memset( luid, 0, sizeof(*luid) );
2885 row.dwIndex = index;
2886 if (GetIfEntry( &row )) return ERROR_FILE_NOT_FOUND;
2888 luid->Info.Reserved = 0;
2889 luid->Info.NetLuidIndex = index;
2890 luid->Info.IfType = row.dwType;
2891 return NO_ERROR;
2894 /******************************************************************
2895 * ConvertInterfaceLuidToGuid (IPHLPAPI.@)
2897 DWORD WINAPI ConvertInterfaceLuidToGuid(const NET_LUID *luid, GUID *guid)
2899 DWORD ret;
2900 MIB_IFROW row;
2902 TRACE("(%p %p)\n", luid, guid);
2904 if (!luid || !guid) return ERROR_INVALID_PARAMETER;
2906 row.dwIndex = luid->Info.NetLuidIndex;
2907 if ((ret = GetIfEntry( &row ))) return ret;
2909 guid->Data1 = luid->Info.NetLuidIndex;
2910 return NO_ERROR;
2913 /******************************************************************
2914 * ConvertInterfaceLuidToIndex (IPHLPAPI.@)
2916 DWORD WINAPI ConvertInterfaceLuidToIndex(const NET_LUID *luid, NET_IFINDEX *index)
2918 DWORD ret;
2919 MIB_IFROW row;
2921 TRACE("(%p %p)\n", luid, index);
2923 if (!luid || !index) return ERROR_INVALID_PARAMETER;
2925 row.dwIndex = luid->Info.NetLuidIndex;
2926 if ((ret = GetIfEntry( &row ))) return ret;
2928 *index = luid->Info.NetLuidIndex;
2929 return NO_ERROR;
2932 /******************************************************************
2933 * ConvertInterfaceLuidToNameA (IPHLPAPI.@)
2935 DWORD WINAPI ConvertInterfaceLuidToNameA(const NET_LUID *luid, char *name, SIZE_T len)
2937 DWORD ret;
2938 MIB_IFROW row;
2940 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
2942 if (!luid) return ERROR_INVALID_PARAMETER;
2944 row.dwIndex = luid->Info.NetLuidIndex;
2945 if ((ret = GetIfEntry( &row ))) return ret;
2947 if (!name || len < WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, NULL, 0, NULL, NULL ))
2948 return ERROR_NOT_ENOUGH_MEMORY;
2950 WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, name, len, NULL, NULL );
2951 return NO_ERROR;
2954 /******************************************************************
2955 * ConvertInterfaceLuidToNameW (IPHLPAPI.@)
2957 DWORD WINAPI ConvertInterfaceLuidToNameW(const NET_LUID *luid, WCHAR *name, SIZE_T len)
2959 DWORD ret;
2960 MIB_IFROW row;
2962 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
2964 if (!luid || !name) return ERROR_INVALID_PARAMETER;
2966 row.dwIndex = luid->Info.NetLuidIndex;
2967 if ((ret = GetIfEntry( &row ))) return ret;
2969 if (len < strlenW( row.wszName ) + 1) return ERROR_NOT_ENOUGH_MEMORY;
2970 strcpyW( name, row.wszName );
2971 return NO_ERROR;
2974 /******************************************************************
2975 * ConvertInterfaceNameToLuidA (IPHLPAPI.@)
2977 DWORD WINAPI ConvertInterfaceNameToLuidA(const char *name, NET_LUID *luid)
2979 DWORD ret;
2980 IF_INDEX index;
2981 MIB_IFROW row;
2983 TRACE("(%s %p)\n", debugstr_a(name), luid);
2985 if ((ret = getInterfaceIndexByName( name, &index ))) return ERROR_INVALID_NAME;
2986 if (!luid) return ERROR_INVALID_PARAMETER;
2988 row.dwIndex = index;
2989 if ((ret = GetIfEntry( &row ))) return ret;
2991 luid->Info.Reserved = 0;
2992 luid->Info.NetLuidIndex = index;
2993 luid->Info.IfType = row.dwType;
2994 return NO_ERROR;
2997 /******************************************************************
2998 * ConvertInterfaceNameToLuidW (IPHLPAPI.@)
3000 DWORD WINAPI ConvertInterfaceNameToLuidW(const WCHAR *name, NET_LUID *luid)
3002 DWORD ret;
3003 IF_INDEX index;
3004 MIB_IFROW row;
3005 char nameA[IF_MAX_STRING_SIZE + 1];
3007 TRACE("(%s %p)\n", debugstr_w(name), luid);
3009 if (!luid) return ERROR_INVALID_PARAMETER;
3010 memset( luid, 0, sizeof(*luid) );
3012 if (!WideCharToMultiByte( CP_UNIXCP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL ))
3013 return ERROR_INVALID_NAME;
3015 if ((ret = getInterfaceIndexByName( nameA, &index ))) return ret;
3017 row.dwIndex = index;
3018 if ((ret = GetIfEntry( &row ))) return ret;
3020 luid->Info.Reserved = 0;
3021 luid->Info.NetLuidIndex = index;
3022 luid->Info.IfType = row.dwType;
3023 return NO_ERROR;