winex11.drv: Ignore .dwAspect in FORMATETC during XDnD.
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
blob8c7c9018c484f368dcee6a167047d046b4daa17d
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 "windns.h"
50 #include "iphlpapi.h"
51 #include "ifenum.h"
52 #include "ipstats.h"
53 #include "ipifcons.h"
54 #include "fltdefs.h"
55 #include "ifdef.h"
56 #include "netioapi.h"
57 #include "tcpestats.h"
58 #include "ip2string.h"
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
65 #ifndef IF_NAMESIZE
66 #define IF_NAMESIZE 16
67 #endif
69 #ifndef INADDR_NONE
70 #define INADDR_NONE ~0UL
71 #endif
73 /******************************************************************
74 * AddIPAddress (IPHLPAPI.@)
76 * Add an IP address to an adapter.
78 * PARAMS
79 * Address [In] IP address to add to the adapter
80 * IpMask [In] subnet mask for the IP address
81 * IfIndex [In] adapter index to add the address
82 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
83 * NTEInstance [Out] NTE instance for the IP address
85 * RETURNS
86 * Success: NO_ERROR
87 * Failure: error code from winerror.h
89 * FIXME
90 * Stub. Currently returns ERROR_NOT_SUPPORTED.
92 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
94 FIXME(":stub\n");
95 return ERROR_NOT_SUPPORTED;
99 /******************************************************************
100 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
102 * Get table of local interfaces.
103 * Like GetIfTable(), but allocate the returned table from heap.
105 * PARAMS
106 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
107 * allocated and returned.
108 * bOrder [In] whether to sort the table
109 * heap [In] heap from which the table is allocated
110 * flags [In] flags to HeapAlloc
112 * RETURNS
113 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
114 * GetIfTable() returns otherwise.
116 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
117 BOOL bOrder, HANDLE heap, DWORD flags)
119 DWORD ret;
121 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
122 bOrder, heap, flags);
123 if (!ppIfTable)
124 ret = ERROR_INVALID_PARAMETER;
125 else {
126 DWORD dwSize = 0;
128 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
129 if (ret == ERROR_INSUFFICIENT_BUFFER) {
130 *ppIfTable = HeapAlloc(heap, flags, dwSize);
131 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
134 TRACE("returning %d\n", ret);
135 return ret;
139 static int IpAddrTableNumericSorter(const void *a, const void *b)
141 int ret = 0;
143 if (a && b)
144 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
145 return ret;
148 static int IpAddrTableLoopbackSorter(const void *a, const void *b)
150 const MIB_IPADDRROW *left = a, *right = b;
151 int ret = 0;
153 if (isIfIndexLoopback(left->dwIndex))
154 ret = 1;
155 else if (isIfIndexLoopback(right->dwIndex))
156 ret = -1;
158 return ret;
161 /******************************************************************
162 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
164 * Get interface-to-IP address mapping table.
165 * Like GetIpAddrTable(), but allocate the returned table from heap.
167 * PARAMS
168 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
169 * allocated and returned.
170 * bOrder [In] whether to sort the table
171 * heap [In] heap from which the table is allocated
172 * flags [In] flags to HeapAlloc
174 * RETURNS
175 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
176 * failure, NO_ERROR on success.
178 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
179 BOOL bOrder, HANDLE heap, DWORD flags)
181 DWORD ret;
183 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
184 ppIpAddrTable, bOrder, heap, flags);
185 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
186 if (!ret && bOrder)
187 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
188 sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
189 TRACE("returning %d\n", ret);
190 return ret;
194 /******************************************************************
195 * CancelIPChangeNotify (IPHLPAPI.@)
197 * Cancel a previous notification created by NotifyAddrChange or
198 * NotifyRouteChange.
200 * PARAMS
201 * overlapped [In] overlapped structure that notifies the caller
203 * RETURNS
204 * Success: TRUE
205 * Failure: FALSE
207 * FIXME
208 * Stub, returns FALSE.
210 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
212 FIXME("(overlapped %p): stub\n", overlapped);
213 return FALSE;
217 /******************************************************************
218 * CancelMibChangeNotify2 (IPHLPAPI.@)
220 DWORD WINAPI CancelMibChangeNotify2(HANDLE handle)
222 FIXME("(handle %p): stub\n", handle);
223 return NO_ERROR;
227 /******************************************************************
228 * CreateIpForwardEntry (IPHLPAPI.@)
230 * Create a route in the local computer's IP table.
232 * PARAMS
233 * pRoute [In] new route information
235 * RETURNS
236 * Success: NO_ERROR
237 * Failure: error code from winerror.h
239 * FIXME
240 * Stub, always returns NO_ERROR.
242 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
244 FIXME("(pRoute %p): stub\n", pRoute);
245 /* could use SIOCADDRT, not sure I want to */
246 return 0;
250 /******************************************************************
251 * CreateIpNetEntry (IPHLPAPI.@)
253 * Create entry in the ARP table.
255 * PARAMS
256 * pArpEntry [In] new ARP entry
258 * RETURNS
259 * Success: NO_ERROR
260 * Failure: error code from winerror.h
262 * FIXME
263 * Stub, always returns NO_ERROR.
265 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
267 FIXME("(pArpEntry %p)\n", pArpEntry);
268 /* could use SIOCSARP on systems that support it, not sure I want to */
269 return 0;
273 /******************************************************************
274 * CreateProxyArpEntry (IPHLPAPI.@)
276 * Create a Proxy ARP (PARP) entry for an IP address.
278 * PARAMS
279 * dwAddress [In] IP address for which this computer acts as a proxy.
280 * dwMask [In] subnet mask for dwAddress
281 * dwIfIndex [In] interface index
283 * RETURNS
284 * Success: NO_ERROR
285 * Failure: error code from winerror.h
287 * FIXME
288 * Stub, returns ERROR_NOT_SUPPORTED.
290 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
292 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
293 dwAddress, dwMask, dwIfIndex);
294 return ERROR_NOT_SUPPORTED;
297 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
299 const IN6_ADDR *addr = &sin->sin6_addr;
300 char *p = buf;
301 int i;
302 BOOL in_zero = FALSE;
304 for (i = 0; i < 7; i++)
306 if (!addr->u.Word[i])
308 if (i == 0)
309 *p++ = ':';
310 if (!in_zero)
312 *p++ = ':';
313 in_zero = TRUE;
316 else
318 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
319 in_zero = FALSE;
322 sprintf(p, "%x", ntohs(addr->u.Word[7]));
323 return buf;
326 static BOOL map_address_6to4( const SOCKADDR_IN6 *addr6, SOCKADDR_IN *addr4 )
328 ULONG i;
330 if (addr6->sin6_family != WS_AF_INET6) return FALSE;
332 for (i = 0; i < 5; i++)
333 if (addr6->sin6_addr.u.Word[i]) return FALSE;
335 if (addr6->sin6_addr.u.Word[5] != 0xffff) return FALSE;
337 addr4->sin_family = WS_AF_INET;
338 addr4->sin_port = addr6->sin6_port;
339 addr4->sin_addr.S_un.S_addr = addr6->sin6_addr.u.Word[6] << 16 | addr6->sin6_addr.u.Word[7];
340 memset( &addr4->sin_zero, 0, sizeof(addr4->sin_zero) );
342 return TRUE;
345 static BOOL find_src_address( MIB_IPADDRTABLE *table, const SOCKADDR_IN *dst, SOCKADDR_IN6 *src )
347 MIB_IPFORWARDROW row;
348 DWORD i, j;
350 if (GetBestRoute( dst->sin_addr.S_un.S_addr, 0, &row )) return FALSE;
352 for (i = 0; i < table->dwNumEntries; i++)
354 /* take the first address */
355 if (table->table[i].dwIndex == row.dwForwardIfIndex)
357 src->sin6_family = WS_AF_INET6;
358 src->sin6_port = 0;
359 src->sin6_flowinfo = 0;
360 for (j = 0; j < 5; j++) src->sin6_addr.u.Word[j] = 0;
361 src->sin6_addr.u.Word[5] = 0xffff;
362 src->sin6_addr.u.Word[6] = table->table[i].dwAddr & 0xffff;
363 src->sin6_addr.u.Word[7] = table->table[i].dwAddr >> 16;
364 return TRUE;
368 return FALSE;
371 /******************************************************************
372 * CreateSortedAddressPairs (IPHLPAPI.@)
374 DWORD WINAPI CreateSortedAddressPairs( const PSOCKADDR_IN6 src_list, DWORD src_count,
375 const PSOCKADDR_IN6 dst_list, DWORD dst_count,
376 DWORD options, PSOCKADDR_IN6_PAIR *pair_list,
377 DWORD *pair_count )
379 DWORD i, size, ret;
380 SOCKADDR_IN6_PAIR *pairs;
381 SOCKADDR_IN6 *ptr;
382 SOCKADDR_IN addr4;
383 MIB_IPADDRTABLE *table;
385 FIXME( "(src_list %p src_count %u dst_list %p dst_count %u options %x pair_list %p pair_count %p): stub\n",
386 src_list, src_count, dst_list, dst_count, options, pair_list, pair_count );
388 if (src_list || src_count || !dst_list || !pair_list || !pair_count || dst_count > 500)
389 return ERROR_INVALID_PARAMETER;
391 for (i = 0; i < dst_count; i++)
393 if (!map_address_6to4( &dst_list[i], &addr4 ))
395 FIXME("only mapped IPv4 addresses are supported\n");
396 return ERROR_NOT_SUPPORTED;
400 size = dst_count * sizeof(*pairs);
401 size += dst_count * sizeof(SOCKADDR_IN6) * 2; /* source address + destination address */
402 if (!(pairs = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_NOT_ENOUGH_MEMORY;
403 ptr = (SOCKADDR_IN6 *)&pairs[dst_count];
405 if ((ret = getIPAddrTable( &table, GetProcessHeap(), 0 )))
407 HeapFree( GetProcessHeap(), 0, pairs );
408 return ret;
411 for (i = 0; i < dst_count; i++)
413 pairs[i].SourceAddress = ptr++;
414 if (!map_address_6to4( &dst_list[i], &addr4 ) ||
415 !find_src_address( table, &addr4, pairs[i].SourceAddress ))
417 char buf[46];
418 FIXME( "source address for %s not found\n", debugstr_ipv6(&dst_list[i], buf) );
419 memset( pairs[i].SourceAddress, 0, sizeof(*pairs[i].SourceAddress) );
420 pairs[i].SourceAddress->sin6_family = WS_AF_INET6;
423 pairs[i].DestinationAddress = ptr++;
424 memcpy( pairs[i].DestinationAddress, &dst_list[i], sizeof(*pairs[i].DestinationAddress) );
426 *pair_list = pairs;
427 *pair_count = dst_count;
429 HeapFree( GetProcessHeap(), 0, table );
430 return NO_ERROR;
434 /******************************************************************
435 * DeleteIPAddress (IPHLPAPI.@)
437 * Delete an IP address added with AddIPAddress().
439 * PARAMS
440 * NTEContext [In] NTE context from AddIPAddress();
442 * RETURNS
443 * Success: NO_ERROR
444 * Failure: error code from winerror.h
446 * FIXME
447 * Stub, returns ERROR_NOT_SUPPORTED.
449 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
451 FIXME("(NTEContext %d): stub\n", NTEContext);
452 return ERROR_NOT_SUPPORTED;
456 /******************************************************************
457 * DeleteIpForwardEntry (IPHLPAPI.@)
459 * Delete a route.
461 * PARAMS
462 * pRoute [In] route to delete
464 * RETURNS
465 * Success: NO_ERROR
466 * Failure: error code from winerror.h
468 * FIXME
469 * Stub, returns NO_ERROR.
471 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
473 FIXME("(pRoute %p): stub\n", pRoute);
474 /* could use SIOCDELRT, not sure I want to */
475 return 0;
479 /******************************************************************
480 * DeleteIpNetEntry (IPHLPAPI.@)
482 * Delete an ARP entry.
484 * PARAMS
485 * pArpEntry [In] ARP entry to delete
487 * RETURNS
488 * Success: NO_ERROR
489 * Failure: error code from winerror.h
491 * FIXME
492 * Stub, returns NO_ERROR.
494 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
496 FIXME("(pArpEntry %p): stub\n", pArpEntry);
497 /* could use SIOCDARP on systems that support it, not sure I want to */
498 return 0;
502 /******************************************************************
503 * DeleteProxyArpEntry (IPHLPAPI.@)
505 * Delete a Proxy ARP entry.
507 * PARAMS
508 * dwAddress [In] IP address for which this computer acts as a proxy.
509 * dwMask [In] subnet mask for dwAddress
510 * dwIfIndex [In] interface index
512 * RETURNS
513 * Success: NO_ERROR
514 * Failure: error code from winerror.h
516 * FIXME
517 * Stub, returns ERROR_NOT_SUPPORTED.
519 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
521 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
522 dwAddress, dwMask, dwIfIndex);
523 return ERROR_NOT_SUPPORTED;
527 /******************************************************************
528 * EnableRouter (IPHLPAPI.@)
530 * Turn on ip forwarding.
532 * PARAMS
533 * pHandle [In/Out]
534 * pOverlapped [In/Out] hEvent member should contain a valid handle.
536 * RETURNS
537 * Success: ERROR_IO_PENDING
538 * Failure: error code from winerror.h
540 * FIXME
541 * Stub, returns ERROR_NOT_SUPPORTED.
543 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
545 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
546 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
547 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
549 return ERROR_NOT_SUPPORTED;
553 /******************************************************************
554 * FlushIpNetTable (IPHLPAPI.@)
556 * Delete all ARP entries of an interface
558 * PARAMS
559 * dwIfIndex [In] interface index
561 * RETURNS
562 * Success: NO_ERROR
563 * Failure: error code from winerror.h
565 * FIXME
566 * Stub, returns ERROR_NOT_SUPPORTED.
568 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
570 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
571 /* this flushes the arp cache of the given index */
572 return ERROR_NOT_SUPPORTED;
575 /******************************************************************
576 * FreeMibTable (IPHLPAPI.@)
578 * Free buffer allocated by network functions
580 * PARAMS
581 * ptr [In] pointer to the buffer to free
584 void WINAPI FreeMibTable(void *ptr)
586 TRACE("(%p)\n", ptr);
587 HeapFree(GetProcessHeap(), 0, ptr);
590 /******************************************************************
591 * GetAdapterIndex (IPHLPAPI.@)
593 * Get interface index from its name.
595 * PARAMS
596 * AdapterName [In] unicode string with the adapter name
597 * IfIndex [Out] returns found interface index
599 * RETURNS
600 * Success: NO_ERROR
601 * Failure: error code from winerror.h
603 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
605 char adapterName[MAX_ADAPTER_NAME];
606 unsigned int i;
607 DWORD ret;
609 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
610 /* The adapter name is guaranteed not to have any unicode characters, so
611 * this translation is never lossy */
612 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
613 adapterName[i] = (char)AdapterName[i];
614 adapterName[i] = '\0';
615 ret = getInterfaceIndexByName(adapterName, IfIndex);
616 TRACE("returning %d\n", ret);
617 return ret;
621 /******************************************************************
622 * GetAdaptersInfo (IPHLPAPI.@)
624 * Get information about adapters.
626 * PARAMS
627 * pAdapterInfo [Out] buffer for adapter infos
628 * pOutBufLen [In] length of output buffer
630 * RETURNS
631 * Success: NO_ERROR
632 * Failure: error code from winerror.h
634 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
636 DWORD ret;
638 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
639 if (!pOutBufLen)
640 ret = ERROR_INVALID_PARAMETER;
641 else {
642 DWORD numNonLoopbackInterfaces = get_interface_indices( TRUE, NULL );
644 if (numNonLoopbackInterfaces > 0) {
645 DWORD numIPAddresses = getNumIPAddresses();
646 ULONG size;
648 /* This may slightly overestimate the amount of space needed, because
649 * the IP addresses include the loopback address, but it's easier
650 * to make sure there's more than enough space than to make sure there's
651 * precisely enough space.
653 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
654 size += numIPAddresses * sizeof(IP_ADDR_STRING);
655 if (!pAdapterInfo || *pOutBufLen < size) {
656 *pOutBufLen = size;
657 ret = ERROR_BUFFER_OVERFLOW;
659 else {
660 InterfaceIndexTable *table = NULL;
661 PMIB_IPADDRTABLE ipAddrTable = NULL;
662 PMIB_IPFORWARDTABLE routeTable = NULL;
664 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
665 if (!ret)
666 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
667 if (!ret)
668 get_interface_indices( TRUE, &table );
669 if (table) {
670 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
671 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
672 if (*pOutBufLen < size) {
673 *pOutBufLen = size;
674 ret = ERROR_INSUFFICIENT_BUFFER;
676 else {
677 DWORD ndx;
678 HKEY hKey;
679 BOOL winsEnabled = FALSE;
680 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
681 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
682 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
684 memset(pAdapterInfo, 0, size);
685 /* @@ Wine registry key: HKCU\Software\Wine\Network */
686 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
687 &hKey) == ERROR_SUCCESS) {
688 DWORD size = sizeof(primaryWINS.String);
689 unsigned long addr;
691 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
692 (LPBYTE)primaryWINS.String, &size);
693 addr = inet_addr(primaryWINS.String);
694 if (addr != INADDR_NONE && addr != INADDR_ANY)
695 winsEnabled = TRUE;
696 size = sizeof(secondaryWINS.String);
697 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
698 (LPBYTE)secondaryWINS.String, &size);
699 addr = inet_addr(secondaryWINS.String);
700 if (addr != INADDR_NONE && addr != INADDR_ANY)
701 winsEnabled = TRUE;
702 RegCloseKey(hKey);
704 for (ndx = 0; ndx < table->numIndexes; ndx++) {
705 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
706 DWORD i;
707 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
708 BOOL firstIPAddr = TRUE;
710 /* on Win98 this is left empty, but whatever */
711 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
712 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
713 ptr->AddressLength = sizeof(ptr->Address);
714 getInterfacePhysicalByIndex(table->indexes[ndx],
715 &ptr->AddressLength, ptr->Address, &ptr->Type);
716 ptr->Index = table->indexes[ndx];
717 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
718 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
719 if (firstIPAddr) {
720 toIPAddressString(ipAddrTable->table[i].dwAddr,
721 ptr->IpAddressList.IpAddress.String);
722 toIPAddressString(ipAddrTable->table[i].dwMask,
723 ptr->IpAddressList.IpMask.String);
724 firstIPAddr = FALSE;
726 else {
727 currentIPAddr->Next = nextIPAddr;
728 currentIPAddr = nextIPAddr;
729 toIPAddressString(ipAddrTable->table[i].dwAddr,
730 currentIPAddr->IpAddress.String);
731 toIPAddressString(ipAddrTable->table[i].dwMask,
732 currentIPAddr->IpMask.String);
733 nextIPAddr++;
737 /* If no IP was found it probably means that the interface is not
738 * configured. In this case we have to return a zeroed IP and mask. */
739 if (firstIPAddr) {
740 strcpy(ptr->IpAddressList.IpAddress.String, "0.0.0.0");
741 strcpy(ptr->IpAddressList.IpMask.String, "0.0.0.0");
743 /* Find first router through this interface, which we'll assume
744 * is the default gateway for this adapter */
745 for (i = 0; i < routeTable->dwNumEntries; i++)
746 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
747 && routeTable->table[i].u1.ForwardType ==
748 MIB_IPROUTE_TYPE_INDIRECT)
750 toIPAddressString(routeTable->table[i].dwForwardNextHop,
751 ptr->GatewayList.IpAddress.String);
752 toIPAddressString(routeTable->table[i].dwForwardMask,
753 ptr->GatewayList.IpMask.String);
755 if (winsEnabled) {
756 ptr->HaveWins = TRUE;
757 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
758 primaryWINS.String, sizeof(primaryWINS.String));
759 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
760 secondaryWINS.String, sizeof(secondaryWINS.String));
762 if (ndx < table->numIndexes - 1)
763 ptr->Next = &pAdapterInfo[ndx + 1];
764 else
765 ptr->Next = NULL;
767 ptr->DhcpEnabled = TRUE;
769 ret = NO_ERROR;
771 HeapFree(GetProcessHeap(), 0, table);
773 else
774 ret = ERROR_OUTOFMEMORY;
775 HeapFree(GetProcessHeap(), 0, routeTable);
776 HeapFree(GetProcessHeap(), 0, ipAddrTable);
779 else
780 ret = ERROR_NO_DATA;
782 TRACE("returning %d\n", ret);
783 return ret;
786 static DWORD typeFromMibType(DWORD mib_type)
788 switch (mib_type)
790 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
791 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
792 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
793 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
794 default: return IF_TYPE_OTHER;
798 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
800 switch (mib_type)
802 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
803 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
804 default: return NET_IF_CONNECTION_DEDICATED;
808 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs, DWORD **masks)
810 ULONG ret, i, j;
811 MIB_IPADDRTABLE *at;
813 *num_addrs = 0;
814 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
815 for (i = 0; i < at->dwNumEntries; i++)
817 if (at->table[i].dwIndex == index) (*num_addrs)++;
819 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
821 HeapFree(GetProcessHeap(), 0, at);
822 return ERROR_OUTOFMEMORY;
824 if (!(*masks = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
826 HeapFree(GetProcessHeap(), 0, *addrs);
827 HeapFree(GetProcessHeap(), 0, at);
828 return ERROR_OUTOFMEMORY;
830 for (i = 0, j = 0; i < at->dwNumEntries; i++)
832 if (at->table[i].dwIndex == index)
834 (*addrs)[j] = at->table[i].dwAddr;
835 (*masks)[j] = at->table[i].dwMask;
836 j++;
839 HeapFree(GetProcessHeap(), 0, at);
840 return ERROR_SUCCESS;
843 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
845 const BYTE *addrp;
846 char *p = buf;
848 for (addrp = (const BYTE *)in_addr;
849 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
850 addrp++)
852 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
853 sprintf(p, "%d", *addrp);
854 else
855 p += sprintf(p, "%d.", *addrp);
857 return buf;
860 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
862 DWORD i, num_gateways = 0;
864 for (i = 0; i < routeTable->dwNumEntries; i++)
866 if (routeTable->table[i].dwForwardIfIndex == index &&
867 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
868 num_gateways++;
870 return num_gateways;
873 static DWORD mask_v4_to_prefix(DWORD m)
875 #ifdef HAVE___BUILTIN_POPCOUNT
876 return __builtin_popcount(m);
877 #else
878 m -= m >> 1 & 0x55555555;
879 m = (m & 0x33333333) + (m >> 2 & 0x33333333);
880 return ((m + (m >> 4)) & 0x0f0f0f0f) * 0x01010101 >> 24;
881 #endif
884 static DWORD mask_v6_to_prefix(SOCKET_ADDRESS *m)
886 const IN6_ADDR *mask = &((struct WS_sockaddr_in6 *)m->lpSockaddr)->sin6_addr;
887 DWORD ret = 0, i;
889 for (i = 0; i < 8; i++)
890 ret += mask_v4_to_prefix(mask->u.Word[i]);
891 return ret;
894 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
895 PMIB_IPFORWARDTABLE routeTable)
897 DWORD i;
898 PMIB_IPFORWARDROW row = NULL;
900 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
902 if (routeTable->table[i].dwForwardIfIndex == index &&
903 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
904 row = &routeTable->table[i];
906 return row;
909 static void fill_unicast_addr_data(IP_ADAPTER_ADDRESSES *aa, IP_ADAPTER_UNICAST_ADDRESS *ua)
911 /* Actually this information should be read somewhere from the system
912 * but it doesn't matter much for the bugs found so far.
913 * This information is required for DirectPlay8 games. */
914 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK)
916 ua->PrefixOrigin = IpPrefixOriginDhcp;
917 ua->SuffixOrigin = IpSuffixOriginDhcp;
919 else
921 ua->PrefixOrigin = IpPrefixOriginManual;
922 ua->SuffixOrigin = IpSuffixOriginManual;
925 /* The address is not duplicated in the network */
926 ua->DadState = IpDadStatePreferred;
928 /* Some address life time values, required even for non-dhcp addresses */
929 ua->ValidLifetime = 60000;
930 ua->PreferredLifetime = 60000;
931 ua->LeaseLifetime = 60000;
934 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
935 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
937 ULONG ret = ERROR_SUCCESS, i, j, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
938 DWORD *v4addrs = NULL, *v4masks = NULL;
939 SOCKET_ADDRESS *v6addrs = NULL, *v6masks = NULL;
940 PMIB_IPFORWARDTABLE routeTable = NULL;
942 if (family == WS_AF_INET)
944 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
946 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
948 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
949 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
952 else if (family == WS_AF_INET6)
954 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
956 else if (family == WS_AF_UNSPEC)
958 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
960 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
962 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
963 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
965 if (!ret) ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
967 else
969 FIXME("address family %u unsupported\n", family);
970 ret = ERROR_NO_DATA;
972 if (ret)
974 HeapFree(GetProcessHeap(), 0, v4addrs);
975 HeapFree(GetProcessHeap(), 0, v4masks);
976 HeapFree(GetProcessHeap(), 0, v6addrs);
977 HeapFree(GetProcessHeap(), 0, v6masks);
978 HeapFree(GetProcessHeap(), 0, routeTable);
979 return ret;
982 total_size = sizeof(IP_ADAPTER_ADDRESSES);
983 total_size += 39; /* "{00000000-0000-0000-0000-000000000000}" */
984 total_size += IF_NAMESIZE * sizeof(WCHAR);
985 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
986 total_size += IF_NAMESIZE * sizeof(WCHAR);
987 if (flags & GAA_FLAG_INCLUDE_PREFIX)
989 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v4addrs;
990 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v6addrs;
991 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
992 for (i = 0; i < num_v6addrs; i++)
993 total_size += v6masks[i].iSockaddrLength;
995 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
996 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
997 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
998 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
999 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
1000 for (i = 0; i < num_v6addrs; i++)
1001 total_size += v6addrs[i].iSockaddrLength;
1003 if (aa && *size >= total_size)
1005 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
1006 WCHAR *dst;
1007 DWORD buflen, type;
1008 INTERNAL_IF_OPER_STATUS status;
1010 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
1011 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
1012 aa->u.s.IfIndex = index;
1014 sprintf(ptr, "{%08x-0000-0000-0000-000000000000}", index);
1015 aa->AdapterName = ptr;
1016 ptr += 39;
1018 getInterfaceNameByIndex(index, name);
1019 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
1021 aa->FriendlyName = (WCHAR *)ptr;
1022 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
1023 *dst = *src;
1024 *dst++ = 0;
1025 ptr = (char *)dst;
1027 aa->Description = (WCHAR *)ptr;
1028 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
1029 *dst = *src;
1030 *dst++ = 0;
1031 ptr = (char *)dst;
1033 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
1034 num_v6addrs);
1036 buflen = MAX_INTERFACE_PHYSADDR;
1037 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
1038 aa->PhysicalAddressLength = buflen;
1039 aa->IfType = typeFromMibType(type);
1040 aa->ConnectionType = connectionTypeFromMibType(type);
1041 aa->Luid.Info.NetLuidIndex = index;
1042 aa->Luid.Info.IfType = aa->IfType;
1044 if (num_v4_gateways)
1046 PMIB_IPFORWARDROW adapterRow;
1048 if ((adapterRow = findIPv4Gateway(index, routeTable)))
1050 PIP_ADAPTER_GATEWAY_ADDRESS gw;
1051 PSOCKADDR_IN sin;
1053 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
1054 aa->FirstGatewayAddress = gw;
1056 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1057 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1058 sin = (PSOCKADDR_IN)ptr;
1059 sin->sin_family = WS_AF_INET;
1060 sin->sin_port = 0;
1061 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
1062 sizeof(DWORD));
1063 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
1064 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1065 gw->Next = NULL;
1066 ptr += sizeof(SOCKADDR_IN);
1069 if (num_v4addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1071 IP_ADAPTER_UNICAST_ADDRESS *ua;
1072 struct WS_sockaddr_in *sa;
1073 aa->u1.s1.Ipv4Enabled = TRUE;
1074 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1075 for (i = 0; i < num_v4addrs; i++)
1077 char addr_buf[16];
1079 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1080 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1081 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1082 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1084 sa = (struct WS_sockaddr_in *)ua->Address.lpSockaddr;
1085 sa->sin_family = WS_AF_INET;
1086 sa->sin_addr.S_un.S_addr = v4addrs[i];
1087 sa->sin_port = 0;
1088 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
1089 debugstr_ipv4(&sa->sin_addr.S_un.S_addr, addr_buf));
1090 fill_unicast_addr_data(aa, ua);
1092 ua->OnLinkPrefixLength = mask_v4_to_prefix(v4masks[i]);
1094 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1095 if (i < num_v4addrs - 1)
1097 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1098 ua = ua->Next;
1102 if (num_v6addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1104 IP_ADAPTER_UNICAST_ADDRESS *ua;
1105 struct WS_sockaddr_in6 *sa;
1107 aa->u1.s1.Ipv6Enabled = TRUE;
1108 if (aa->FirstUnicastAddress)
1110 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
1112 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1113 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1115 else
1116 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1117 for (i = 0; i < num_v6addrs; i++)
1119 char addr_buf[46];
1121 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1122 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1123 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
1124 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1126 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
1127 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
1128 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
1129 debugstr_ipv6(sa, addr_buf));
1130 fill_unicast_addr_data(aa, ua);
1132 ua->OnLinkPrefixLength = mask_v6_to_prefix(&v6masks[i]);
1134 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1135 if (i < num_v6addrs - 1)
1137 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1138 ua = ua->Next;
1142 if (num_v4addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1144 IP_ADAPTER_PREFIX *prefix;
1146 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1147 for (i = 0; i < num_v4addrs; i++)
1149 char addr_buf[16];
1150 struct WS_sockaddr_in *sa;
1152 prefix->u.s.Length = sizeof(*prefix);
1153 prefix->u.s.Flags = 0;
1154 prefix->Next = NULL;
1155 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1156 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1158 sa = (struct WS_sockaddr_in *)prefix->Address.lpSockaddr;
1159 sa->sin_family = WS_AF_INET;
1160 sa->sin_addr.S_un.S_addr = v4addrs[i] & v4masks[i];
1161 sa->sin_port = 0;
1163 prefix->PrefixLength = mask_v4_to_prefix(v4masks[i]);
1165 TRACE("IPv4 network: %s/%u\n",
1166 debugstr_ipv4((const in_addr_t *)&sa->sin_addr.S_un.S_addr, addr_buf),
1167 prefix->PrefixLength);
1169 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1170 if (i < num_v4addrs - 1)
1172 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1173 prefix = prefix->Next;
1177 if (num_v6addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1179 IP_ADAPTER_PREFIX *prefix;
1181 if (aa->FirstPrefix)
1183 for (prefix = aa->FirstPrefix; prefix->Next; prefix = prefix->Next)
1185 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1186 prefix = (IP_ADAPTER_PREFIX *)ptr;
1188 else
1189 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1190 for (i = 0; i < num_v6addrs; i++)
1192 char addr_buf[46];
1193 struct WS_sockaddr_in6 *sa;
1194 const IN6_ADDR *addr, *mask;
1196 prefix->u.s.Length = sizeof(*prefix);
1197 prefix->u.s.Flags = 0;
1198 prefix->Next = NULL;
1199 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in6);
1200 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1202 sa = (struct WS_sockaddr_in6 *)prefix->Address.lpSockaddr;
1203 sa->sin6_family = WS_AF_INET6;
1204 sa->sin6_port = 0;
1205 sa->sin6_flowinfo = 0;
1206 addr = &((struct WS_sockaddr_in6 *)v6addrs[i].lpSockaddr)->sin6_addr;
1207 mask = &((struct WS_sockaddr_in6 *)v6masks[i].lpSockaddr)->sin6_addr;
1208 for (j = 0; j < 8; j++) sa->sin6_addr.u.Word[j] = addr->u.Word[j] & mask->u.Word[j];
1209 sa->sin6_scope_id = 0;
1211 prefix->PrefixLength = mask_v6_to_prefix(&v6masks[i]);
1213 TRACE("IPv6 network: %s/%u\n", debugstr_ipv6(sa, addr_buf), prefix->PrefixLength);
1215 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1216 if (i < num_v6addrs - 1)
1218 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1219 prefix = prefix->Next;
1224 getInterfaceMtuByName(name, &aa->Mtu);
1226 getInterfaceStatusByName(name, &status);
1227 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
1228 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
1229 else aa->OperStatus = IfOperStatusUnknown;
1231 *size = total_size;
1232 HeapFree(GetProcessHeap(), 0, routeTable);
1233 HeapFree(GetProcessHeap(), 0, v6addrs);
1234 HeapFree(GetProcessHeap(), 0, v6masks);
1235 HeapFree(GetProcessHeap(), 0, v4addrs);
1236 HeapFree(GetProcessHeap(), 0, v4masks);
1237 return ERROR_SUCCESS;
1240 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in *src )
1242 SOCKADDR_IN *s = (SOCKADDR_IN *)dst;
1244 s->sin_family = WS_AF_INET;
1245 s->sin_port = src->sin_port;
1246 memcpy( &s->sin_addr, &src->sin_addr, sizeof(IN_ADDR) );
1247 memset( (char *)s + FIELD_OFFSET( SOCKADDR_IN, sin_zero ), 0,
1248 sizeof(SOCKADDR_STORAGE) - FIELD_OFFSET( SOCKADDR_IN, sin_zero) );
1251 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in6 *src )
1253 SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)dst;
1255 s->sin6_family = WS_AF_INET6;
1256 s->sin6_port = src->sin6_port;
1257 s->sin6_flowinfo = src->sin6_flowinfo;
1258 memcpy( &s->sin6_addr, &src->sin6_addr, sizeof(IN6_ADDR) );
1259 s->sin6_scope_id = src->sin6_scope_id;
1260 memset( (char *)s + sizeof(SOCKADDR_IN6), 0,
1261 sizeof(SOCKADDR_STORAGE) - sizeof(SOCKADDR_IN6) );
1264 #ifdef HAVE_STRUCT___RES_STATE
1265 /* call res_init() just once because of a bug in Mac OS X 10.4 */
1266 /* Call once per thread on systems that have per-thread _res. */
1268 static CRITICAL_SECTION res_init_cs;
1269 static CRITICAL_SECTION_DEBUG res_init_cs_debug = {
1270 0, 0, &res_init_cs,
1271 { &res_init_cs_debug.ProcessLocksList, &res_init_cs_debug.ProcessLocksList },
1272 0, 0, { (DWORD_PTR)(__FILE__ ": res_init_cs") }
1274 static CRITICAL_SECTION res_init_cs = { &res_init_cs_debug, -1, 0, 0, 0, 0 };
1276 static void initialise_resolver(void)
1278 EnterCriticalSection(&res_init_cs);
1279 if ((_res.options & RES_INIT) == 0)
1280 res_init();
1281 LeaveCriticalSection(&res_init_cs);
1284 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1286 int i, ip6_count = 0;
1287 SOCKADDR_STORAGE *addr;
1289 initialise_resolver();
1291 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1292 ip6_count = _res._u._ext.nscount6;
1293 #endif
1295 if (!servers || !num)
1297 num = _res.nscount;
1298 if (ip4_only) num -= ip6_count;
1299 return num;
1302 for (i = 0, addr = servers; addr < (servers + num) && i < _res.nscount; i++)
1304 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1305 if (_res._u._ext.nsaddrs[i] && _res._u._ext.nsaddrs[i]->sin6_family == AF_INET6)
1307 if (ip4_only) continue;
1308 sockaddr_in6_to_WS_storage( addr, _res._u._ext.nsaddrs[i] );
1310 else
1311 #endif
1313 sockaddr_in_to_WS_storage( addr, _res.nsaddr_list + i );
1315 addr++;
1317 return addr - servers;
1319 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
1321 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1323 extern struct res_state *__res_get_state( void );
1324 extern int __res_getservers( struct res_state *, struct sockaddr_storage *, int );
1325 struct res_state *state = __res_get_state();
1326 int i, found = 0, total = __res_getservers( state, NULL, 0 );
1327 SOCKADDR_STORAGE *addr = servers;
1328 struct sockaddr_storage *buf;
1330 if ((!servers || !num) && !ip4_only) return total;
1332 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(struct sockaddr_storage) );
1333 total = __res_getservers( state, buf, total );
1335 for (i = 0; i < total; i++)
1337 if (buf[i].ss_family == AF_INET6 && ip4_only) continue;
1338 if (buf[i].ss_family != AF_INET && buf[i].ss_family != AF_INET6) continue;
1340 found++;
1341 if (!servers || !num) continue;
1343 if (buf[i].ss_family == AF_INET6)
1345 sockaddr_in6_to_WS_storage( addr, (struct sockaddr_in6 *)(buf + i) );
1347 else
1349 sockaddr_in_to_WS_storage( addr, (struct sockaddr_in *)(buf + i) );
1351 if (++addr >= servers + num) break;
1354 HeapFree( GetProcessHeap(), 0, buf );
1355 return found;
1357 #else
1359 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1361 FIXME("Unimplemented on this system\n");
1362 return 0;
1364 #endif
1366 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
1368 int num = get_dns_servers( NULL, 0, FALSE );
1369 DWORD size;
1371 size = num * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR_STORAGE));
1372 if (!address || *len < size)
1374 *len = size;
1375 return ERROR_BUFFER_OVERFLOW;
1377 *len = size;
1378 if (num > 0)
1380 PIP_ADAPTER_DNS_SERVER_ADDRESS addr = address;
1381 SOCKADDR_STORAGE *sock_addrs = (SOCKADDR_STORAGE *)(address + num);
1382 int i;
1384 get_dns_servers( sock_addrs, num, FALSE );
1386 for (i = 0; i < num; i++, addr = addr->Next)
1388 addr->u.s.Length = sizeof(*addr);
1389 if (sock_addrs[i].ss_family == WS_AF_INET6)
1390 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN6);
1391 else
1392 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1393 addr->Address.lpSockaddr = (SOCKADDR *)(sock_addrs + i);
1394 if (i == num - 1)
1395 addr->Next = NULL;
1396 else
1397 addr->Next = addr + 1;
1400 return ERROR_SUCCESS;
1403 #ifdef HAVE_STRUCT___RES_STATE
1404 static BOOL is_ip_address_string(const char *str)
1406 struct in_addr in;
1407 int ret;
1409 ret = inet_aton(str, &in);
1410 return ret != 0;
1412 #endif
1414 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1416 ULONG size;
1417 const char *found_suffix = "";
1418 /* Always return a NULL-terminated string, even if it's empty. */
1420 #ifdef HAVE_STRUCT___RES_STATE
1422 ULONG i;
1423 initialise_resolver();
1424 for (i = 0; !*found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1426 /* This uses a heuristic to select a DNS suffix:
1427 * the first, non-IP address string is selected.
1429 if (!is_ip_address_string(_res.dnsrch[i]))
1430 found_suffix = _res.dnsrch[i];
1433 #endif
1435 size = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, NULL, 0 ) * sizeof(WCHAR);
1436 if (!suffix || *len < size)
1438 *len = size;
1439 return ERROR_BUFFER_OVERFLOW;
1441 *len = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, suffix, *len / sizeof(WCHAR) ) * sizeof(WCHAR);
1442 return ERROR_SUCCESS;
1445 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1446 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1448 InterfaceIndexTable *table;
1449 ULONG i, size, dns_server_size = 0, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1451 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1453 if (!buflen) return ERROR_INVALID_PARAMETER;
1455 get_interface_indices( FALSE, &table );
1456 if (!table || !table->numIndexes)
1458 HeapFree(GetProcessHeap(), 0, table);
1459 return ERROR_NO_DATA;
1461 total_size = 0;
1462 for (i = 0; i < table->numIndexes; i++)
1464 size = 0;
1465 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1467 HeapFree(GetProcessHeap(), 0, table);
1468 return ret;
1470 total_size += size;
1472 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1474 /* Since DNS servers aren't really per adapter, get enough space for a
1475 * single copy of them.
1477 get_dns_server_addresses(NULL, &dns_server_size);
1478 total_size += dns_server_size;
1480 /* Since DNS suffix also isn't really per adapter, get enough space for a
1481 * single copy of it.
1483 get_dns_suffix(NULL, &dns_suffix_size);
1484 total_size += dns_suffix_size;
1485 if (aa && *buflen >= total_size)
1487 ULONG bytes_left = size = total_size;
1488 PIP_ADAPTER_ADDRESSES first_aa = aa;
1489 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1490 WCHAR *dnsSuffix;
1492 for (i = 0; i < table->numIndexes; i++)
1494 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1496 HeapFree(GetProcessHeap(), 0, table);
1497 return ret;
1499 if (i < table->numIndexes - 1)
1501 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1502 aa = aa->Next;
1503 size = bytes_left -= size;
1506 if (dns_server_size)
1508 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1509 get_dns_server_addresses(firstDns, &dns_server_size);
1510 for (aa = first_aa; aa; aa = aa->Next)
1512 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1513 aa->FirstDnsServerAddress = firstDns;
1516 aa = first_aa;
1517 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1518 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1519 for (; aa; aa = aa->Next)
1521 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1522 aa->DnsSuffix = dnsSuffix;
1523 else
1524 aa->DnsSuffix = dnsSuffix + dns_suffix_size / sizeof(WCHAR) - 1;
1526 ret = ERROR_SUCCESS;
1528 else
1530 ret = ERROR_BUFFER_OVERFLOW;
1531 *buflen = total_size;
1534 TRACE("num adapters %u\n", table->numIndexes);
1535 HeapFree(GetProcessHeap(), 0, table);
1536 return ret;
1539 /******************************************************************
1540 * GetBestInterface (IPHLPAPI.@)
1542 * Get the interface, with the best route for the given IP address.
1544 * PARAMS
1545 * dwDestAddr [In] IP address to search the interface for
1546 * pdwBestIfIndex [Out] found best interface
1548 * RETURNS
1549 * Success: NO_ERROR
1550 * Failure: error code from winerror.h
1552 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1554 struct WS_sockaddr_in sa_in;
1555 memset(&sa_in, 0, sizeof(sa_in));
1556 sa_in.sin_family = WS_AF_INET;
1557 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1558 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1561 /******************************************************************
1562 * GetBestInterfaceEx (IPHLPAPI.@)
1564 * Get the interface, with the best route for the given IP address.
1566 * PARAMS
1567 * dwDestAddr [In] IP address to search the interface for
1568 * pdwBestIfIndex [Out] found best interface
1570 * RETURNS
1571 * Success: NO_ERROR
1572 * Failure: error code from winerror.h
1574 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1576 DWORD ret;
1578 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1579 if (!pDestAddr || !pdwBestIfIndex)
1580 ret = ERROR_INVALID_PARAMETER;
1581 else {
1582 MIB_IPFORWARDROW ipRow;
1584 if (pDestAddr->sa_family == WS_AF_INET) {
1585 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1586 if (ret == ERROR_SUCCESS)
1587 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1588 } else {
1589 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1590 ret = ERROR_NOT_SUPPORTED;
1593 TRACE("returning %d\n", ret);
1594 return ret;
1598 /******************************************************************
1599 * GetBestRoute (IPHLPAPI.@)
1601 * Get the best route for the given IP address.
1603 * PARAMS
1604 * dwDestAddr [In] IP address to search the best route for
1605 * dwSourceAddr [In] optional source IP address
1606 * pBestRoute [Out] found best route
1608 * RETURNS
1609 * Success: NO_ERROR
1610 * Failure: error code from winerror.h
1612 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1614 PMIB_IPFORWARDTABLE table;
1615 DWORD ret;
1617 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1618 dwSourceAddr, pBestRoute);
1619 if (!pBestRoute)
1620 return ERROR_INVALID_PARAMETER;
1622 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1623 if (!ret) {
1624 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1626 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1627 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1628 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1629 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1630 DWORD numShifts, mask;
1632 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1633 mask && mask & 1; mask >>= 1, numShifts++)
1635 if (numShifts > matchedBits) {
1636 matchedBits = numShifts;
1637 matchedNdx = ndx;
1639 else if (!matchedBits) {
1640 matchedNdx = ndx;
1644 if (matchedNdx < table->dwNumEntries) {
1645 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1646 ret = ERROR_SUCCESS;
1648 else {
1649 /* No route matches, which can happen if there's no default route. */
1650 ret = ERROR_HOST_UNREACHABLE;
1652 HeapFree(GetProcessHeap(), 0, table);
1654 TRACE("returning %d\n", ret);
1655 return ret;
1659 /******************************************************************
1660 * GetFriendlyIfIndex (IPHLPAPI.@)
1662 * Get a "friendly" version of IfIndex, which is one that doesn't
1663 * have the top byte set. Doesn't validate whether IfIndex is a valid
1664 * adapter index.
1666 * PARAMS
1667 * IfIndex [In] interface index to get the friendly one for
1669 * RETURNS
1670 * A friendly version of IfIndex.
1672 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1674 /* windows doesn't validate these, either, just makes sure the top byte is
1675 cleared. I assume my ifenum module never gives an index with the top
1676 byte set. */
1677 TRACE("returning %d\n", IfIndex);
1678 return IfIndex;
1682 /******************************************************************
1683 * GetIfEntry (IPHLPAPI.@)
1685 * Get information about an interface.
1687 * PARAMS
1688 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1689 * Out: interface information
1691 * RETURNS
1692 * Success: NO_ERROR
1693 * Failure: error code from winerror.h
1695 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1697 DWORD ret;
1698 char nameBuf[MAX_ADAPTER_NAME];
1699 char *name;
1701 TRACE("pIfRow %p\n", pIfRow);
1702 if (!pIfRow)
1703 return ERROR_INVALID_PARAMETER;
1705 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1706 if (name) {
1707 ret = getInterfaceEntryByName(name, pIfRow);
1708 if (ret == NO_ERROR)
1709 ret = getInterfaceStatsByName(name, pIfRow);
1711 else
1712 ret = ERROR_INVALID_DATA;
1713 TRACE("returning %d\n", ret);
1714 return ret;
1717 /******************************************************************
1718 * GetIfEntry2 (IPHLPAPI.@)
1720 DWORD WINAPI GetIfEntry2( MIB_IF_ROW2 *row2 )
1722 DWORD ret, len = ARRAY_SIZE(row2->Description);
1723 char buf[MAX_ADAPTER_NAME], *name;
1724 MIB_IFROW row;
1726 TRACE("%p\n", row2);
1728 if (!row2 || (!(name = getInterfaceNameByIndex( row2->InterfaceIndex, buf )) &&
1729 !(name = getInterfaceNameByIndex( row2->InterfaceLuid.Info.NetLuidIndex, buf ))))
1731 return ERROR_INVALID_PARAMETER;
1733 if ((ret = getInterfaceEntryByName( name, &row ))) return ret;
1734 if ((ret = getInterfaceStatsByName( name, &row ))) return ret;
1736 memset( row2, 0, sizeof(*row2) );
1737 row2->InterfaceLuid.Info.Reserved = 0;
1738 row2->InterfaceLuid.Info.NetLuidIndex = row.dwIndex;
1739 row2->InterfaceLuid.Info.IfType = row.dwType;
1740 row2->InterfaceIndex = row.dwIndex;
1741 row2->InterfaceGuid.Data1 = row.dwIndex;
1742 row2->Type = row.dwType;
1743 row2->Mtu = row.dwMtu;
1744 MultiByteToWideChar( CP_UNIXCP, 0, (const char *)row.bDescr, -1, row2->Description, len );
1745 row2->PhysicalAddressLength = row.dwPhysAddrLen;
1746 memcpy( &row2->PhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1747 memcpy( &row2->PermanentPhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1748 row2->OperStatus = IfOperStatusUp;
1749 row2->AdminStatus = NET_IF_ADMIN_STATUS_UP;
1750 row2->MediaConnectState = MediaConnectStateConnected;
1751 row2->ConnectionType = NET_IF_CONNECTION_DEDICATED;
1753 /* stats */
1754 row2->InOctets = row.dwInOctets;
1755 row2->InUcastPkts = row.dwInUcastPkts;
1756 row2->InNUcastPkts = row.dwInNUcastPkts;
1757 row2->InDiscards = row.dwInDiscards;
1758 row2->InErrors = row.dwInErrors;
1759 row2->InUnknownProtos = row.dwInUnknownProtos;
1760 row2->OutOctets = row.dwOutOctets;
1761 row2->OutUcastPkts = row.dwOutUcastPkts;
1762 row2->OutNUcastPkts = row.dwOutNUcastPkts;
1763 row2->OutDiscards = row.dwOutDiscards;
1764 row2->OutErrors = row.dwOutErrors;
1766 return NO_ERROR;
1769 static int IfTableSorter(const void *a, const void *b)
1771 int ret;
1773 if (a && b)
1774 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1775 else
1776 ret = 0;
1777 return ret;
1781 /******************************************************************
1782 * GetIfTable (IPHLPAPI.@)
1784 * Get a table of local interfaces.
1786 * PARAMS
1787 * pIfTable [Out] buffer for local interfaces table
1788 * pdwSize [In/Out] length of output buffer
1789 * bOrder [In] whether to sort the table
1791 * RETURNS
1792 * Success: NO_ERROR
1793 * Failure: error code from winerror.h
1795 * NOTES
1796 * If pdwSize is less than required, the function will return
1797 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1798 * size.
1799 * If bOrder is true, the returned table will be sorted by interface index.
1801 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1803 DWORD ret;
1805 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pIfTable, pdwSize, bOrder);
1807 if (!pdwSize)
1808 ret = ERROR_INVALID_PARAMETER;
1809 else {
1810 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1811 ULONG size = sizeof(MIB_IFTABLE);
1813 if (numInterfaces > 1)
1814 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1815 if (!pIfTable || *pdwSize < size) {
1816 *pdwSize = size;
1817 ret = ERROR_INSUFFICIENT_BUFFER;
1819 else {
1820 InterfaceIndexTable *table;
1821 get_interface_indices( FALSE, &table );
1823 if (table) {
1824 size = sizeof(MIB_IFTABLE);
1825 if (table->numIndexes > 1)
1826 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1827 if (*pdwSize < size) {
1828 *pdwSize = size;
1829 ret = ERROR_INSUFFICIENT_BUFFER;
1831 else {
1832 DWORD ndx;
1834 *pdwSize = size;
1835 pIfTable->dwNumEntries = 0;
1836 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1837 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1838 GetIfEntry(&pIfTable->table[ndx]);
1839 pIfTable->dwNumEntries++;
1841 if (bOrder)
1842 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1843 IfTableSorter);
1844 ret = NO_ERROR;
1846 HeapFree(GetProcessHeap(), 0, table);
1848 else
1849 ret = ERROR_OUTOFMEMORY;
1852 TRACE("returning %d\n", ret);
1853 return ret;
1856 /******************************************************************
1857 * GetIfTable2Ex (IPHLPAPI.@)
1859 DWORD WINAPI GetIfTable2Ex( MIB_IF_TABLE_LEVEL level, MIB_IF_TABLE2 **table )
1861 DWORD i, nb_interfaces, size = sizeof(MIB_IF_TABLE2);
1862 InterfaceIndexTable *index_table;
1863 MIB_IF_TABLE2 *ret;
1865 TRACE( "level %u, table %p\n", level, table );
1867 if (!table || level > MibIfTableNormalWithoutStatistics)
1868 return ERROR_INVALID_PARAMETER;
1870 if (level != MibIfTableNormal)
1871 FIXME("level %u not fully supported\n", level);
1873 if ((nb_interfaces = get_interface_indices( FALSE, NULL )) > 1)
1874 size += (nb_interfaces - 1) * sizeof(MIB_IF_ROW2);
1876 if (!(ret = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
1878 get_interface_indices( FALSE, &index_table );
1879 if (!index_table)
1881 HeapFree( GetProcessHeap(), 0, ret );
1882 return ERROR_OUTOFMEMORY;
1885 ret->NumEntries = 0;
1886 for (i = 0; i < index_table->numIndexes; i++)
1888 ret->Table[i].InterfaceIndex = index_table->indexes[i];
1889 GetIfEntry2( &ret->Table[i] );
1890 ret->NumEntries++;
1893 HeapFree( GetProcessHeap(), 0, index_table );
1894 *table = ret;
1895 return NO_ERROR;
1898 /******************************************************************
1899 * GetIfTable2 (IPHLPAPI.@)
1901 DWORD WINAPI GetIfTable2( MIB_IF_TABLE2 **table )
1903 TRACE( "table %p\n", table );
1904 return GetIfTable2Ex(MibIfTableNormal, table);
1907 /******************************************************************
1908 * GetInterfaceInfo (IPHLPAPI.@)
1910 * Get a list of network interface adapters.
1912 * PARAMS
1913 * pIfTable [Out] buffer for interface adapters
1914 * dwOutBufLen [Out] if buffer is too small, returns required size
1916 * RETURNS
1917 * Success: NO_ERROR
1918 * Failure: error code from winerror.h
1920 * BUGS
1921 * MSDN states this should return non-loopback interfaces only.
1923 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1925 DWORD ret;
1927 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1928 if (!dwOutBufLen)
1929 ret = ERROR_INVALID_PARAMETER;
1930 else {
1931 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1932 ULONG size = sizeof(IP_INTERFACE_INFO);
1934 if (numInterfaces > 1)
1935 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1936 if (!pIfTable || *dwOutBufLen < size) {
1937 *dwOutBufLen = size;
1938 ret = ERROR_INSUFFICIENT_BUFFER;
1940 else {
1941 InterfaceIndexTable *table;
1942 get_interface_indices( FALSE, &table );
1944 if (table) {
1945 size = sizeof(IP_INTERFACE_INFO);
1946 if (table->numIndexes > 1)
1947 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1948 if (*dwOutBufLen < size) {
1949 *dwOutBufLen = size;
1950 ret = ERROR_INSUFFICIENT_BUFFER;
1952 else {
1953 DWORD ndx;
1954 char nameBuf[MAX_ADAPTER_NAME];
1956 *dwOutBufLen = size;
1957 pIfTable->NumAdapters = 0;
1958 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1959 const char *walker, *name;
1960 WCHAR *assigner;
1962 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1963 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1964 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1965 walker && *walker &&
1966 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1967 walker++, assigner++)
1968 *assigner = *walker;
1969 *assigner = 0;
1970 pIfTable->NumAdapters++;
1972 ret = NO_ERROR;
1974 HeapFree(GetProcessHeap(), 0, table);
1976 else
1977 ret = ERROR_OUTOFMEMORY;
1980 TRACE("returning %d\n", ret);
1981 return ret;
1985 /******************************************************************
1986 * GetIpAddrTable (IPHLPAPI.@)
1988 * Get interface-to-IP address mapping table.
1990 * PARAMS
1991 * pIpAddrTable [Out] buffer for mapping table
1992 * pdwSize [In/Out] length of output buffer
1993 * bOrder [In] whether to sort the table
1995 * RETURNS
1996 * Success: NO_ERROR
1997 * Failure: error code from winerror.h
1999 * NOTES
2000 * If pdwSize is less than required, the function will return
2001 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2002 * size.
2003 * If bOrder is true, the returned table will be sorted by the next hop and
2004 * an assortment of arbitrary parameters.
2006 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
2008 DWORD ret;
2010 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
2011 (DWORD)bOrder);
2012 if (!pdwSize)
2013 ret = ERROR_INVALID_PARAMETER;
2014 else {
2015 PMIB_IPADDRTABLE table;
2017 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
2018 if (ret == NO_ERROR)
2020 ULONG size = FIELD_OFFSET(MIB_IPADDRTABLE, table[table->dwNumEntries]);
2022 if (!pIpAddrTable || *pdwSize < size) {
2023 *pdwSize = size;
2024 ret = ERROR_INSUFFICIENT_BUFFER;
2026 else {
2027 *pdwSize = size;
2028 memcpy(pIpAddrTable, table, size);
2029 /* sort by numeric IP value */
2030 if (bOrder)
2031 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
2032 sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
2033 /* sort ensuring loopback interfaces are in the end */
2034 else
2035 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
2036 sizeof(MIB_IPADDRROW), IpAddrTableLoopbackSorter);
2037 ret = NO_ERROR;
2039 HeapFree(GetProcessHeap(), 0, table);
2042 TRACE("returning %d\n", ret);
2043 return ret;
2047 /******************************************************************
2048 * GetIpForwardTable (IPHLPAPI.@)
2050 * Get the route table.
2052 * PARAMS
2053 * pIpForwardTable [Out] buffer for route table
2054 * pdwSize [In/Out] length of output buffer
2055 * bOrder [In] whether to sort the table
2057 * RETURNS
2058 * Success: NO_ERROR
2059 * Failure: error code from winerror.h
2061 * NOTES
2062 * If pdwSize is less than required, the function will return
2063 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2064 * size.
2065 * If bOrder is true, the returned table will be sorted by the next hop and
2066 * an assortment of arbitrary parameters.
2068 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
2070 DWORD ret;
2071 PMIB_IPFORWARDTABLE table;
2073 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
2075 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2077 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
2078 if (!ret) {
2079 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
2080 if (!pIpForwardTable || *pdwSize < size) {
2081 *pdwSize = size;
2082 ret = ERROR_INSUFFICIENT_BUFFER;
2084 else {
2085 *pdwSize = size;
2086 memcpy(pIpForwardTable, table, size);
2088 HeapFree(GetProcessHeap(), 0, table);
2090 TRACE("returning %d\n", ret);
2091 return ret;
2095 /******************************************************************
2096 * GetIpNetTable (IPHLPAPI.@)
2098 * Get the IP-to-physical address mapping table.
2100 * PARAMS
2101 * pIpNetTable [Out] buffer for mapping table
2102 * pdwSize [In/Out] length of output buffer
2103 * bOrder [In] whether to sort the table
2105 * RETURNS
2106 * Success: NO_ERROR
2107 * Failure: error code from winerror.h
2109 * NOTES
2110 * If pdwSize is less than required, the function will return
2111 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2112 * size.
2113 * If bOrder is true, the returned table will be sorted by IP address.
2115 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
2117 DWORD ret;
2118 PMIB_IPNETTABLE table;
2120 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
2122 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2124 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
2125 if (!ret) {
2126 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
2127 if (!pIpNetTable || *pdwSize < size) {
2128 *pdwSize = size;
2129 ret = ERROR_INSUFFICIENT_BUFFER;
2131 else {
2132 *pdwSize = size;
2133 memcpy(pIpNetTable, table, size);
2135 HeapFree(GetProcessHeap(), 0, table);
2137 TRACE("returning %d\n", ret);
2138 return ret;
2141 /* Gets the DNS server list into the list beginning at list. Assumes that
2142 * a single server address may be placed at list if *len is at least
2143 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
2144 * and assumes that all remaining DNS servers are contiguously located
2145 * beginning at firstDynamic. On input, *len is assumed to be the total number
2146 * of bytes available for all DNS servers, and is ignored if list is NULL.
2147 * On return, *len is set to the total number of bytes required for all DNS
2148 * servers.
2149 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
2150 * ERROR_SUCCESS otherwise.
2152 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
2153 PIP_ADDR_STRING firstDynamic, DWORD *len)
2155 DWORD size;
2156 int num = get_dns_servers( NULL, 0, TRUE );
2158 size = num * sizeof(IP_ADDR_STRING);
2159 if (!list || *len < size) {
2160 *len = size;
2161 return ERROR_BUFFER_OVERFLOW;
2163 *len = size;
2164 if (num > 0) {
2165 PIP_ADDR_STRING ptr;
2166 int i;
2167 SOCKADDR_STORAGE *addr = HeapAlloc( GetProcessHeap(), 0, num * sizeof(SOCKADDR_STORAGE) );
2169 get_dns_servers( addr, num, TRUE );
2171 for (i = 0, ptr = list; i < num; i++, ptr = ptr->Next) {
2172 toIPAddressString(((struct sockaddr_in *)(addr + i))->sin_addr.s_addr,
2173 ptr->IpAddress.String);
2174 if (i == num - 1)
2175 ptr->Next = NULL;
2176 else if (i == 0)
2177 ptr->Next = firstDynamic;
2178 else
2179 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
2181 HeapFree( GetProcessHeap(), 0, addr );
2183 return ERROR_SUCCESS;
2186 /******************************************************************
2187 * GetNetworkParams (IPHLPAPI.@)
2189 * Get the network parameters for the local computer.
2191 * PARAMS
2192 * pFixedInfo [Out] buffer for network parameters
2193 * pOutBufLen [In/Out] length of output buffer
2195 * RETURNS
2196 * Success: NO_ERROR
2197 * Failure: error code from winerror.h
2199 * NOTES
2200 * If pOutBufLen is less than required, the function will return
2201 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
2202 * size.
2204 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
2206 DWORD ret, size, serverListSize;
2207 LONG regReturn;
2208 HKEY hKey;
2210 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
2211 if (!pOutBufLen)
2212 return ERROR_INVALID_PARAMETER;
2214 get_dns_server_list(NULL, NULL, &serverListSize);
2215 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
2216 if (!pFixedInfo || *pOutBufLen < size) {
2217 *pOutBufLen = size;
2218 return ERROR_BUFFER_OVERFLOW;
2221 memset(pFixedInfo, 0, size);
2222 size = sizeof(pFixedInfo->HostName);
2223 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
2224 size = sizeof(pFixedInfo->DomainName);
2225 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
2226 get_dns_server_list(&pFixedInfo->DnsServerList,
2227 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
2228 &serverListSize);
2229 /* Assume the first DNS server in the list is the "current" DNS server: */
2230 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
2231 pFixedInfo->NodeType = HYBRID_NODETYPE;
2232 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2233 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
2234 if (regReturn != ERROR_SUCCESS)
2235 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2236 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
2237 &hKey);
2238 if (regReturn == ERROR_SUCCESS)
2240 DWORD size = sizeof(pFixedInfo->ScopeId);
2242 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
2243 RegCloseKey(hKey);
2246 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
2247 I suppose could also check for a listener on port 53 to set EnableDns */
2248 ret = NO_ERROR;
2249 TRACE("returning %d\n", ret);
2250 return ret;
2254 /******************************************************************
2255 * GetNumberOfInterfaces (IPHLPAPI.@)
2257 * Get the number of interfaces.
2259 * PARAMS
2260 * pdwNumIf [Out] number of interfaces
2262 * RETURNS
2263 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
2265 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
2267 DWORD ret;
2269 TRACE("pdwNumIf %p\n", pdwNumIf);
2270 if (!pdwNumIf)
2271 ret = ERROR_INVALID_PARAMETER;
2272 else {
2273 *pdwNumIf = get_interface_indices( FALSE, NULL );
2274 ret = NO_ERROR;
2276 TRACE("returning %d\n", ret);
2277 return ret;
2281 /******************************************************************
2282 * GetPerAdapterInfo (IPHLPAPI.@)
2284 * Get information about an adapter corresponding to an interface.
2286 * PARAMS
2287 * IfIndex [In] interface info
2288 * pPerAdapterInfo [Out] buffer for per adapter info
2289 * pOutBufLen [In/Out] length of output buffer
2291 * RETURNS
2292 * Success: NO_ERROR
2293 * Failure: error code from winerror.h
2295 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
2297 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
2298 DWORD ret = NO_ERROR;
2300 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
2302 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
2304 if (!isIfIndexLoopback(IfIndex)) {
2305 get_dns_server_list(NULL, NULL, &serverListSize);
2306 if (serverListSize > sizeof(IP_ADDR_STRING))
2307 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
2309 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
2311 *pOutBufLen = bytesNeeded;
2312 return ERROR_BUFFER_OVERFLOW;
2315 memset(pPerAdapterInfo, 0, bytesNeeded);
2316 if (!isIfIndexLoopback(IfIndex)) {
2317 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
2318 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
2319 &serverListSize);
2320 /* Assume the first DNS server in the list is the "current" DNS server: */
2321 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
2323 return ret;
2327 /******************************************************************
2328 * GetRTTAndHopCount (IPHLPAPI.@)
2330 * Get round-trip time (RTT) and hop count.
2332 * PARAMS
2334 * DestIpAddress [In] destination address to get the info for
2335 * HopCount [Out] retrieved hop count
2336 * MaxHops [In] maximum hops to search for the destination
2337 * RTT [Out] RTT in milliseconds
2339 * RETURNS
2340 * Success: TRUE
2341 * Failure: FALSE
2343 * FIXME
2344 * Stub, returns FALSE.
2346 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
2348 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
2349 DestIpAddress, HopCount, MaxHops, RTT);
2350 return FALSE;
2354 /******************************************************************
2355 * GetTcpTable (IPHLPAPI.@)
2357 * Get the table of active TCP connections.
2359 * PARAMS
2360 * pTcpTable [Out] buffer for TCP connections table
2361 * pdwSize [In/Out] length of output buffer
2362 * bOrder [In] whether to order the table
2364 * RETURNS
2365 * Success: NO_ERROR
2366 * Failure: error code from winerror.h
2368 * NOTES
2369 * If pdwSize is less than required, the function will return
2370 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2371 * the required byte size.
2372 * If bOrder is true, the returned table will be sorted, first by
2373 * local address and port number, then by remote address and port
2374 * number.
2376 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
2378 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
2379 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, WS_AF_INET, TCP_TABLE_BASIC_ALL, 0);
2382 /******************************************************************
2383 * GetExtendedTcpTable (IPHLPAPI.@)
2385 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
2386 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
2388 DWORD ret, size;
2389 void *table;
2391 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2392 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2394 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2396 if (ulAf != WS_AF_INET)
2398 FIXME("ulAf = %u not supported\n", ulAf);
2399 return ERROR_NOT_SUPPORTED;
2401 if (TableClass >= TCP_TABLE_OWNER_MODULE_LISTENER)
2402 FIXME("module classes not fully supported\n");
2404 if ((ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2405 return ret;
2407 if (!pTcpTable || *pdwSize < size)
2409 *pdwSize = size;
2410 ret = ERROR_INSUFFICIENT_BUFFER;
2412 else
2414 *pdwSize = size;
2415 memcpy(pTcpTable, table, size);
2417 HeapFree(GetProcessHeap(), 0, table);
2418 return ret;
2421 /******************************************************************
2422 * GetUdpTable (IPHLPAPI.@)
2424 * Get a table of active UDP connections.
2426 * PARAMS
2427 * pUdpTable [Out] buffer for UDP connections table
2428 * pdwSize [In/Out] length of output buffer
2429 * bOrder [In] whether to order the table
2431 * RETURNS
2432 * Success: NO_ERROR
2433 * Failure: error code from winerror.h
2435 * NOTES
2436 * If pdwSize is less than required, the function will return
2437 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2438 * required byte size.
2439 * If bOrder is true, the returned table will be sorted, first by
2440 * local address, then by local port number.
2442 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2444 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, WS_AF_INET, UDP_TABLE_BASIC, 0);
2447 /******************************************************************
2448 * GetUdp6Table (IPHLPAPI.@)
2450 DWORD WINAPI GetUdp6Table(PMIB_UDP6TABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2452 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, WS_AF_INET6, UDP_TABLE_BASIC, 0);
2455 /******************************************************************
2456 * GetExtendedUdpTable (IPHLPAPI.@)
2458 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
2459 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
2461 DWORD ret, size;
2462 void *table;
2464 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2465 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2467 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2469 if (TableClass == UDP_TABLE_OWNER_MODULE)
2470 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2472 switch (ulAf)
2474 case WS_AF_INET:
2475 ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size);
2476 break;
2478 case WS_AF_INET6:
2479 ret = build_udp6_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size);
2480 break;
2482 default:
2483 FIXME("ulAf = %u not supported\n", ulAf);
2484 ret = ERROR_NOT_SUPPORTED;
2487 if (ret)
2488 return ret;
2490 if (!pUdpTable || *pdwSize < size)
2492 *pdwSize = size;
2493 ret = ERROR_INSUFFICIENT_BUFFER;
2495 else
2497 *pdwSize = size;
2498 memcpy(pUdpTable, table, size);
2500 HeapFree(GetProcessHeap(), 0, table);
2501 return ret;
2504 DWORD WINAPI GetUnicastIpAddressEntry(MIB_UNICASTIPADDRESS_ROW *row)
2506 IP_ADAPTER_ADDRESSES *aa, *ptr;
2507 ULONG size = 0;
2508 DWORD ret;
2510 TRACE("%p\n", row);
2512 if (!row)
2513 return ERROR_INVALID_PARAMETER;
2515 ret = GetAdaptersAddresses(row->Address.si_family, 0, NULL, NULL, &size);
2516 if (ret != ERROR_BUFFER_OVERFLOW)
2517 return ret;
2518 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, size)))
2519 return ERROR_OUTOFMEMORY;
2520 if ((ret = GetAdaptersAddresses(row->Address.si_family, 0, NULL, ptr, &size)))
2522 HeapFree(GetProcessHeap(), 0, ptr);
2523 return ret;
2526 ret = ERROR_FILE_NOT_FOUND;
2527 for (aa = ptr; aa; aa = aa->Next)
2529 IP_ADAPTER_UNICAST_ADDRESS *ua;
2531 if (aa->u.s.IfIndex != row->InterfaceIndex &&
2532 memcmp(&aa->Luid, &row->InterfaceLuid, sizeof(row->InterfaceLuid)))
2533 continue;
2534 ret = ERROR_NOT_FOUND;
2536 ua = aa->FirstUnicastAddress;
2537 while (ua)
2539 SOCKADDR_INET *uaaddr = (SOCKADDR_INET *)ua->Address.lpSockaddr;
2541 if ((row->Address.si_family == WS_AF_INET6 &&
2542 !memcmp(&row->Address.Ipv6.sin6_addr, &uaaddr->Ipv6.sin6_addr, sizeof(uaaddr->Ipv6.sin6_addr))) ||
2543 (row->Address.si_family == WS_AF_INET &&
2544 row->Address.Ipv4.sin_addr.S_un.S_addr == uaaddr->Ipv4.sin_addr.S_un.S_addr))
2546 memcpy(&row->InterfaceLuid, &aa->Luid, sizeof(aa->Luid));
2547 row->InterfaceIndex = aa->u.s.IfIndex;
2548 row->PrefixOrigin = ua->PrefixOrigin;
2549 row->SuffixOrigin = ua->SuffixOrigin;
2550 row->ValidLifetime = ua->ValidLifetime;
2551 row->PreferredLifetime = ua->PreferredLifetime;
2552 row->OnLinkPrefixLength = ua->OnLinkPrefixLength;
2553 row->SkipAsSource = 0;
2554 row->DadState = ua->DadState;
2555 if (row->Address.si_family == WS_AF_INET6)
2556 row->ScopeId.u.Value = row->Address.Ipv6.sin6_scope_id;
2557 else
2558 row->ScopeId.u.Value = 0;
2559 NtQuerySystemTime(&row->CreationTimeStamp);
2560 HeapFree(GetProcessHeap(), 0, ptr);
2561 return NO_ERROR;
2563 ua = ua->Next;
2566 HeapFree(GetProcessHeap(), 0, ptr);
2568 return ret;
2571 DWORD WINAPI GetUnicastIpAddressTable(ADDRESS_FAMILY family, MIB_UNICASTIPADDRESS_TABLE **table)
2573 IP_ADAPTER_ADDRESSES *aa, *ptr;
2574 MIB_UNICASTIPADDRESS_TABLE *data;
2575 DWORD ret, count = 0;
2576 ULONG size, flags;
2578 TRACE("%u, %p\n", family, table);
2580 if (!table || (family != WS_AF_INET && family != WS_AF_INET6 && family != WS_AF_UNSPEC))
2581 return ERROR_INVALID_PARAMETER;
2583 flags = GAA_FLAG_SKIP_ANYCAST |
2584 GAA_FLAG_SKIP_MULTICAST |
2585 GAA_FLAG_SKIP_DNS_SERVER |
2586 GAA_FLAG_SKIP_FRIENDLY_NAME;
2588 ret = GetAdaptersAddresses(family, flags, NULL, NULL, &size);
2589 if (ret != ERROR_BUFFER_OVERFLOW)
2590 return ret;
2591 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, size)))
2592 return ERROR_OUTOFMEMORY;
2593 if ((ret = GetAdaptersAddresses(family, flags, NULL, ptr, &size)))
2595 HeapFree(GetProcessHeap(), 0, ptr);
2596 return ret;
2599 for (aa = ptr; aa; aa = aa->Next)
2601 IP_ADAPTER_UNICAST_ADDRESS *ua = aa->FirstUnicastAddress;
2602 while (ua)
2604 count++;
2605 ua = ua->Next;
2609 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) + (count - 1) * sizeof(data->Table[0]))))
2611 HeapFree(GetProcessHeap(), 0, ptr);
2612 return ERROR_OUTOFMEMORY;
2615 data->NumEntries = 0;
2616 for (aa = ptr; aa; aa = aa->Next)
2618 IP_ADAPTER_UNICAST_ADDRESS *ua = aa->FirstUnicastAddress;
2619 while (ua)
2621 MIB_UNICASTIPADDRESS_ROW *row = &data->Table[data->NumEntries];
2622 memcpy(&row->Address, ua->Address.lpSockaddr, ua->Address.iSockaddrLength);
2623 memcpy(&row->InterfaceLuid, &aa->Luid, sizeof(aa->Luid));
2624 row->InterfaceIndex = aa->u.s.IfIndex;
2625 row->PrefixOrigin = ua->PrefixOrigin;
2626 row->SuffixOrigin = ua->SuffixOrigin;
2627 row->ValidLifetime = ua->ValidLifetime;
2628 row->PreferredLifetime = ua->PreferredLifetime;
2629 row->OnLinkPrefixLength = ua->OnLinkPrefixLength;
2630 row->SkipAsSource = 0;
2631 row->DadState = ua->DadState;
2632 if (row->Address.si_family == WS_AF_INET6)
2633 row->ScopeId.u.Value = row->Address.Ipv6.sin6_scope_id;
2634 else
2635 row->ScopeId.u.Value = 0;
2636 NtQuerySystemTime(&row->CreationTimeStamp);
2638 data->NumEntries++;
2639 ua = ua->Next;
2643 HeapFree(GetProcessHeap(), 0, ptr);
2645 *table = data;
2646 return ret;
2649 /******************************************************************
2650 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2652 * This is a Win98-only function to get information on "unidirectional"
2653 * adapters. Since this is pretty nonsensical in other contexts, it
2654 * never returns anything.
2656 * PARAMS
2657 * pIPIfInfo [Out] buffer for adapter infos
2658 * dwOutBufLen [Out] length of the output buffer
2660 * RETURNS
2661 * Success: NO_ERROR
2662 * Failure: error code from winerror.h
2664 * FIXME
2665 * Stub, returns ERROR_NOT_SUPPORTED.
2667 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
2669 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
2670 /* a unidirectional adapter?? not bloody likely! */
2671 return ERROR_NOT_SUPPORTED;
2675 /******************************************************************
2676 * IpReleaseAddress (IPHLPAPI.@)
2678 * Release an IP obtained through DHCP,
2680 * PARAMS
2681 * AdapterInfo [In] adapter to release IP address
2683 * RETURNS
2684 * Success: NO_ERROR
2685 * Failure: error code from winerror.h
2687 * NOTES
2688 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2689 * this function does nothing.
2691 * FIXME
2692 * Stub, returns ERROR_NOT_SUPPORTED.
2694 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2696 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2697 return ERROR_NOT_SUPPORTED;
2701 /******************************************************************
2702 * IpRenewAddress (IPHLPAPI.@)
2704 * Renew an IP obtained through DHCP.
2706 * PARAMS
2707 * AdapterInfo [In] adapter to renew IP address
2709 * RETURNS
2710 * Success: NO_ERROR
2711 * Failure: error code from winerror.h
2713 * NOTES
2714 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2715 * this function does nothing.
2717 * FIXME
2718 * Stub, returns ERROR_NOT_SUPPORTED.
2720 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2722 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2723 return ERROR_NOT_SUPPORTED;
2727 /******************************************************************
2728 * NotifyAddrChange (IPHLPAPI.@)
2730 * Notify caller whenever the ip-interface map is changed.
2732 * PARAMS
2733 * Handle [Out] handle usable in asynchronous notification
2734 * overlapped [In] overlapped structure that notifies the caller
2736 * RETURNS
2737 * Success: NO_ERROR
2738 * Failure: error code from winerror.h
2740 * FIXME
2741 * Stub, returns ERROR_NOT_SUPPORTED.
2743 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2745 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2746 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2747 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2748 return ERROR_IO_PENDING;
2752 /******************************************************************
2753 * NotifyIpInterfaceChange (IPHLPAPI.@)
2755 DWORD WINAPI NotifyIpInterfaceChange(ADDRESS_FAMILY family, PIPINTERFACE_CHANGE_CALLBACK callback,
2756 PVOID context, BOOLEAN init_notify, PHANDLE handle)
2758 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2759 family, callback, context, init_notify, handle);
2760 if (handle) *handle = NULL;
2761 return NO_ERROR;
2765 /******************************************************************
2766 * NotifyRouteChange (IPHLPAPI.@)
2768 * Notify caller whenever the ip routing table is changed.
2770 * PARAMS
2771 * Handle [Out] handle usable in asynchronous notification
2772 * overlapped [In] overlapped structure that notifies the caller
2774 * RETURNS
2775 * Success: NO_ERROR
2776 * Failure: error code from winerror.h
2778 * FIXME
2779 * Stub, returns ERROR_NOT_SUPPORTED.
2781 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2783 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2784 return ERROR_NOT_SUPPORTED;
2788 /******************************************************************
2789 * NotifyUnicastIpAddressChange (IPHLPAPI.@)
2791 DWORD WINAPI NotifyUnicastIpAddressChange(ADDRESS_FAMILY family, PUNICAST_IPADDRESS_CHANGE_CALLBACK callback,
2792 PVOID context, BOOLEAN init_notify, PHANDLE handle)
2794 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2795 family, callback, context, init_notify, handle);
2796 if (handle) *handle = NULL;
2797 return ERROR_NOT_SUPPORTED;
2800 /******************************************************************
2801 * SendARP (IPHLPAPI.@)
2803 * Send an ARP request.
2805 * PARAMS
2806 * DestIP [In] attempt to obtain this IP
2807 * SrcIP [In] optional sender IP address
2808 * pMacAddr [Out] buffer for the mac address
2809 * PhyAddrLen [In/Out] length of the output buffer
2811 * RETURNS
2812 * Success: NO_ERROR
2813 * Failure: error code from winerror.h
2815 * FIXME
2816 * Stub, returns ERROR_NOT_SUPPORTED.
2818 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2820 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2821 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2822 return ERROR_NOT_SUPPORTED;
2826 /******************************************************************
2827 * SetIfEntry (IPHLPAPI.@)
2829 * Set the administrative status of an interface.
2831 * PARAMS
2832 * pIfRow [In] dwAdminStatus member specifies the new status.
2834 * RETURNS
2835 * Success: NO_ERROR
2836 * Failure: error code from winerror.h
2838 * FIXME
2839 * Stub, returns ERROR_NOT_SUPPORTED.
2841 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2843 FIXME("(pIfRow %p): stub\n", pIfRow);
2844 /* this is supposed to set an interface administratively up or down.
2845 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2846 this sort of down is indistinguishable from other sorts of down (e.g. no
2847 link). */
2848 return ERROR_NOT_SUPPORTED;
2852 /******************************************************************
2853 * SetIpForwardEntry (IPHLPAPI.@)
2855 * Modify an existing route.
2857 * PARAMS
2858 * pRoute [In] route with the new information
2860 * RETURNS
2861 * Success: NO_ERROR
2862 * Failure: error code from winerror.h
2864 * FIXME
2865 * Stub, returns NO_ERROR.
2867 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2869 FIXME("(pRoute %p): stub\n", pRoute);
2870 /* this is to add a route entry, how's it distinguishable from
2871 CreateIpForwardEntry?
2872 could use SIOCADDRT, not sure I want to */
2873 return 0;
2877 /******************************************************************
2878 * SetIpNetEntry (IPHLPAPI.@)
2880 * Modify an existing ARP entry.
2882 * PARAMS
2883 * pArpEntry [In] ARP entry with the new information
2885 * RETURNS
2886 * Success: NO_ERROR
2887 * Failure: error code from winerror.h
2889 * FIXME
2890 * Stub, returns NO_ERROR.
2892 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2894 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2895 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2896 return 0;
2900 /******************************************************************
2901 * SetIpStatistics (IPHLPAPI.@)
2903 * Toggle IP forwarding and det the default TTL value.
2905 * PARAMS
2906 * pIpStats [In] IP statistics with the new information
2908 * RETURNS
2909 * Success: NO_ERROR
2910 * Failure: error code from winerror.h
2912 * FIXME
2913 * Stub, returns NO_ERROR.
2915 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2917 FIXME("(pIpStats %p): stub\n", pIpStats);
2918 return 0;
2922 /******************************************************************
2923 * SetIpTTL (IPHLPAPI.@)
2925 * Set the default TTL value.
2927 * PARAMS
2928 * nTTL [In] new TTL value
2930 * RETURNS
2931 * Success: NO_ERROR
2932 * Failure: error code from winerror.h
2934 * FIXME
2935 * Stub, returns NO_ERROR.
2937 DWORD WINAPI SetIpTTL(UINT nTTL)
2939 FIXME("(nTTL %d): stub\n", nTTL);
2940 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2941 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2942 return 0;
2946 /******************************************************************
2947 * SetTcpEntry (IPHLPAPI.@)
2949 * Set the state of a TCP connection.
2951 * PARAMS
2952 * pTcpRow [In] specifies connection with new state
2954 * RETURNS
2955 * Success: NO_ERROR
2956 * Failure: error code from winerror.h
2958 * FIXME
2959 * Stub, returns NO_ERROR.
2961 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2963 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2964 return 0;
2967 /******************************************************************
2968 * SetPerTcpConnectionEStats (IPHLPAPI.@)
2970 DWORD WINAPI SetPerTcpConnectionEStats(PMIB_TCPROW row, TCP_ESTATS_TYPE state, PBYTE rw,
2971 ULONG version, ULONG size, ULONG offset)
2973 FIXME("(row %p, state %d, rw %p, version %u, size %u, offset %u): stub\n",
2974 row, state, rw, version, size, offset);
2975 return ERROR_NOT_SUPPORTED;
2979 /******************************************************************
2980 * UnenableRouter (IPHLPAPI.@)
2982 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2983 * if it reaches zero.
2985 * PARAMS
2986 * pOverlapped [In/Out] should be the same as in EnableRouter()
2987 * lpdwEnableCount [Out] optional, receives reference count
2989 * RETURNS
2990 * Success: NO_ERROR
2991 * Failure: error code from winerror.h
2993 * FIXME
2994 * Stub, returns ERROR_NOT_SUPPORTED.
2996 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2998 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2999 lpdwEnableCount);
3000 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
3001 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
3003 return ERROR_NOT_SUPPORTED;
3006 /******************************************************************
3007 * PfCreateInterface (IPHLPAPI.@)
3009 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
3010 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
3012 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
3013 return ERROR_CALL_NOT_IMPLEMENTED;
3016 /******************************************************************
3017 * PfUnBindInterface (IPHLPAPI.@)
3019 DWORD WINAPI PfUnBindInterface(INTERFACE_HANDLE interface)
3021 FIXME("(%p) stub\n", interface);
3022 return ERROR_CALL_NOT_IMPLEMENTED;
3025 /******************************************************************
3026 * PfDeleteInterface(IPHLPAPI.@)
3028 DWORD WINAPI PfDeleteInterface(INTERFACE_HANDLE interface)
3030 FIXME("(%p) stub\n", interface);
3031 return ERROR_CALL_NOT_IMPLEMENTED;
3034 /******************************************************************
3035 * PfBindInterfaceToIPAddress(IPHLPAPI.@)
3037 DWORD WINAPI PfBindInterfaceToIPAddress(INTERFACE_HANDLE interface, PFADDRESSTYPE type, PBYTE ip)
3039 FIXME("(%p %d %p) stub\n", interface, type, ip);
3040 return ERROR_CALL_NOT_IMPLEMENTED;
3043 /******************************************************************
3044 * GetTcpTable2 (IPHLPAPI.@)
3046 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
3048 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
3049 return ERROR_NOT_SUPPORTED;
3052 /******************************************************************
3053 * GetTcp6Table (IPHLPAPI.@)
3055 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
3057 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table, size, order);
3058 return ERROR_NOT_SUPPORTED;
3061 /******************************************************************
3062 * GetTcp6Table2 (IPHLPAPI.@)
3064 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
3066 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
3067 return ERROR_NOT_SUPPORTED;
3070 /******************************************************************
3071 * ConvertInterfaceGuidToLuid (IPHLPAPI.@)
3073 DWORD WINAPI ConvertInterfaceGuidToLuid(const GUID *guid, NET_LUID *luid)
3075 DWORD ret;
3076 MIB_IFROW row;
3078 TRACE("(%s %p)\n", debugstr_guid(guid), luid);
3080 if (!guid || !luid) return ERROR_INVALID_PARAMETER;
3082 row.dwIndex = guid->Data1;
3083 if ((ret = GetIfEntry( &row ))) return ret;
3085 luid->Info.Reserved = 0;
3086 luid->Info.NetLuidIndex = guid->Data1;
3087 luid->Info.IfType = row.dwType;
3088 return NO_ERROR;
3091 /******************************************************************
3092 * ConvertInterfaceIndexToLuid (IPHLPAPI.@)
3094 DWORD WINAPI ConvertInterfaceIndexToLuid(NET_IFINDEX index, NET_LUID *luid)
3096 MIB_IFROW row;
3098 TRACE("(%u %p)\n", index, luid);
3100 if (!luid) return ERROR_INVALID_PARAMETER;
3101 memset( luid, 0, sizeof(*luid) );
3103 row.dwIndex = index;
3104 if (GetIfEntry( &row )) return ERROR_FILE_NOT_FOUND;
3106 luid->Info.Reserved = 0;
3107 luid->Info.NetLuidIndex = index;
3108 luid->Info.IfType = row.dwType;
3109 return NO_ERROR;
3112 /******************************************************************
3113 * ConvertInterfaceLuidToGuid (IPHLPAPI.@)
3115 DWORD WINAPI ConvertInterfaceLuidToGuid(const NET_LUID *luid, GUID *guid)
3117 DWORD ret;
3118 MIB_IFROW row;
3120 TRACE("(%p %p)\n", luid, guid);
3122 if (!luid || !guid) return ERROR_INVALID_PARAMETER;
3124 row.dwIndex = luid->Info.NetLuidIndex;
3125 if ((ret = GetIfEntry( &row ))) return ret;
3127 memset( guid, 0, sizeof(*guid) );
3128 guid->Data1 = luid->Info.NetLuidIndex;
3129 return NO_ERROR;
3132 /******************************************************************
3133 * ConvertInterfaceLuidToIndex (IPHLPAPI.@)
3135 DWORD WINAPI ConvertInterfaceLuidToIndex(const NET_LUID *luid, NET_IFINDEX *index)
3137 DWORD ret;
3138 MIB_IFROW row;
3140 TRACE("(%p %p)\n", luid, index);
3142 if (!luid || !index) return ERROR_INVALID_PARAMETER;
3144 row.dwIndex = luid->Info.NetLuidIndex;
3145 if ((ret = GetIfEntry( &row ))) return ret;
3147 *index = luid->Info.NetLuidIndex;
3148 return NO_ERROR;
3151 /******************************************************************
3152 * ConvertInterfaceLuidToNameA (IPHLPAPI.@)
3154 DWORD WINAPI ConvertInterfaceLuidToNameA(const NET_LUID *luid, char *name, SIZE_T len)
3156 DWORD ret;
3157 MIB_IFROW row;
3159 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
3161 if (!luid) return ERROR_INVALID_PARAMETER;
3163 row.dwIndex = luid->Info.NetLuidIndex;
3164 if ((ret = GetIfEntry( &row ))) return ret;
3166 if (!name || len < WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, NULL, 0, NULL, NULL ))
3167 return ERROR_NOT_ENOUGH_MEMORY;
3169 WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, name, len, NULL, NULL );
3170 return NO_ERROR;
3173 /******************************************************************
3174 * ConvertInterfaceLuidToNameW (IPHLPAPI.@)
3176 DWORD WINAPI ConvertInterfaceLuidToNameW(const NET_LUID *luid, WCHAR *name, SIZE_T len)
3178 DWORD ret;
3179 MIB_IFROW row;
3181 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
3183 if (!luid || !name) return ERROR_INVALID_PARAMETER;
3185 row.dwIndex = luid->Info.NetLuidIndex;
3186 if ((ret = GetIfEntry( &row ))) return ret;
3188 if (len < strlenW( row.wszName ) + 1) return ERROR_NOT_ENOUGH_MEMORY;
3189 strcpyW( name, row.wszName );
3190 return NO_ERROR;
3193 /******************************************************************
3194 * ConvertInterfaceNameToLuidA (IPHLPAPI.@)
3196 DWORD WINAPI ConvertInterfaceNameToLuidA(const char *name, NET_LUID *luid)
3198 DWORD ret;
3199 IF_INDEX index;
3200 MIB_IFROW row;
3202 TRACE("(%s %p)\n", debugstr_a(name), luid);
3204 if ((ret = getInterfaceIndexByName( name, &index ))) return ERROR_INVALID_NAME;
3205 if (!luid) return ERROR_INVALID_PARAMETER;
3207 row.dwIndex = index;
3208 if ((ret = GetIfEntry( &row ))) return ret;
3210 luid->Info.Reserved = 0;
3211 luid->Info.NetLuidIndex = index;
3212 luid->Info.IfType = row.dwType;
3213 return NO_ERROR;
3216 /******************************************************************
3217 * ConvertInterfaceNameToLuidW (IPHLPAPI.@)
3219 DWORD WINAPI ConvertInterfaceNameToLuidW(const WCHAR *name, NET_LUID *luid)
3221 DWORD ret;
3222 IF_INDEX index;
3223 MIB_IFROW row;
3224 char nameA[IF_MAX_STRING_SIZE + 1];
3226 TRACE("(%s %p)\n", debugstr_w(name), luid);
3228 if (!luid) return ERROR_INVALID_PARAMETER;
3229 memset( luid, 0, sizeof(*luid) );
3231 if (!WideCharToMultiByte( CP_UNIXCP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL ))
3232 return ERROR_INVALID_NAME;
3234 if ((ret = getInterfaceIndexByName( nameA, &index ))) return ret;
3236 row.dwIndex = index;
3237 if ((ret = GetIfEntry( &row ))) return ret;
3239 luid->Info.Reserved = 0;
3240 luid->Info.NetLuidIndex = index;
3241 luid->Info.IfType = row.dwType;
3242 return NO_ERROR;
3245 /******************************************************************
3246 * ConvertLengthToIpv4Mask (IPHLPAPI.@)
3248 DWORD WINAPI ConvertLengthToIpv4Mask(ULONG mask_len, ULONG *mask)
3250 if (mask_len > 32)
3252 *mask = INADDR_NONE;
3253 return ERROR_INVALID_PARAMETER;
3256 if (mask_len == 0)
3257 *mask = 0;
3258 else
3259 *mask = htonl(~0u << (32 - mask_len));
3261 return NO_ERROR;
3264 /******************************************************************
3265 * if_nametoindex (IPHLPAPI.@)
3267 IF_INDEX WINAPI IPHLP_if_nametoindex(const char *name)
3269 IF_INDEX idx;
3271 TRACE("(%s)\n", name);
3272 if (getInterfaceIndexByName(name, &idx) == NO_ERROR)
3273 return idx;
3275 return 0;
3278 /******************************************************************
3279 * if_indextoname (IPHLPAPI.@)
3281 PCHAR WINAPI IPHLP_if_indextoname(NET_IFINDEX index, PCHAR name)
3283 TRACE("(%u, %p)\n", index, name);
3285 return getInterfaceNameByIndex(index, name);
3288 /******************************************************************
3289 * GetIpForwardTable2 (IPHLPAPI.@)
3291 DWORD WINAPI GetIpForwardTable2(ADDRESS_FAMILY family, PMIB_IPFORWARD_TABLE2 *table)
3293 static int once;
3295 if (!once++) FIXME("(%u %p): stub\n", family, table);
3296 return ERROR_NOT_SUPPORTED;
3299 /******************************************************************
3300 * GetIpNetTable2 (IPHLPAPI.@)
3302 DWORD WINAPI GetIpNetTable2(ADDRESS_FAMILY family, PMIB_IPNET_TABLE2 *table)
3304 static int once;
3306 if (!once++) FIXME("(%u %p): stub\n", family, table);
3307 return ERROR_NOT_SUPPORTED;
3310 /******************************************************************
3311 * GetIpInterfaceTable (IPHLPAPI.@)
3313 DWORD WINAPI GetIpInterfaceTable(ADDRESS_FAMILY family, PMIB_IPINTERFACE_TABLE *table)
3315 FIXME("(%u %p): stub\n", family, table);
3316 return ERROR_NOT_SUPPORTED;
3319 /******************************************************************
3320 * GetBestRoute2 (IPHLPAPI.@)
3322 DWORD WINAPI GetBestRoute2(NET_LUID *luid, NET_IFINDEX index,
3323 const SOCKADDR_INET *source, const SOCKADDR_INET *destination,
3324 ULONG options, PMIB_IPFORWARD_ROW2 bestroute,
3325 SOCKADDR_INET *bestaddress)
3327 static int once;
3329 if (!once++)
3330 FIXME("(%p, %d, %p, %p, 0x%08x, %p, %p): stub\n", luid, index, source,
3331 destination, options, bestroute, bestaddress);
3333 if (!destination || !bestroute || !bestaddress)
3334 return ERROR_INVALID_PARAMETER;
3336 return ERROR_NOT_SUPPORTED;
3339 /******************************************************************
3340 * ParseNetworkString (IPHLPAPI.@)
3342 DWORD WINAPI ParseNetworkString(const WCHAR *str, DWORD type,
3343 NET_ADDRESS_INFO *info, USHORT *port, BYTE *prefix_len)
3345 IN_ADDR temp_addr4;
3346 USHORT temp_port = 0;
3347 NTSTATUS status;
3349 TRACE("(%s, %d, %p, %p, %p)\n", debugstr_w(str), type, info, port, prefix_len);
3351 if (!str)
3352 return ERROR_INVALID_PARAMETER;
3354 if (type & NET_STRING_IPV4_ADDRESS)
3356 status = RtlIpv4StringToAddressExW(str, TRUE, &temp_addr4, &temp_port);
3357 if (SUCCEEDED(status) && !temp_port)
3359 if (info)
3361 info->Format = NET_ADDRESS_IPV4;
3362 info->u.Ipv4Address.sin_addr = temp_addr4;
3363 info->u.Ipv4Address.sin_port = 0;
3365 if (port) *port = 0;
3366 if (prefix_len) *prefix_len = 255;
3367 return ERROR_SUCCESS;
3370 if (type & NET_STRING_IPV4_SERVICE)
3372 status = RtlIpv4StringToAddressExW(str, TRUE, &temp_addr4, &temp_port);
3373 if (SUCCEEDED(status) && temp_port)
3375 if (info)
3377 info->Format = NET_ADDRESS_IPV4;
3378 info->u.Ipv4Address.sin_addr = temp_addr4;
3379 info->u.Ipv4Address.sin_port = temp_port;
3381 if (port) *port = ntohs(temp_port);
3382 if (prefix_len) *prefix_len = 255;
3383 return ERROR_SUCCESS;
3387 if (info) info->Format = NET_ADDRESS_FORMAT_UNSPECIFIED;
3389 if (type & ~(NET_STRING_IPV4_ADDRESS|NET_STRING_IPV4_SERVICE))
3391 FIXME("Unimplemented type 0x%x\n", type);
3392 return ERROR_NOT_SUPPORTED;
3395 return ERROR_INVALID_PARAMETER;