kernelbase: Use the correct machine type for ARM.
[wine/zf.git] / dlls / iphlpapi / iphlpapi_main.c
blob09414d26dde60d2bc12b557b9f318eea74d7db4c
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 RtlIpv4AddressToStringA((IN_ADDR *)&ipAddrTable->table[i].dwAddr,
721 ptr->IpAddressList.IpAddress.String);
722 RtlIpv4AddressToStringA((IN_ADDR *)&ipAddrTable->table[i].dwMask,
723 ptr->IpAddressList.IpMask.String);
724 firstIPAddr = FALSE;
726 else {
727 currentIPAddr->Next = nextIPAddr;
728 currentIPAddr = nextIPAddr;
729 RtlIpv4AddressToStringA((IN_ADDR *)&ipAddrTable->table[i].dwAddr,
730 currentIPAddr->IpAddress.String);
731 RtlIpv4AddressToStringA((IN_ADDR *)&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 strcpy(ptr->GatewayList.IpAddress.String, "0.0.0.0");
746 strcpy(ptr->GatewayList.IpMask.String, "255.255.255.255");
747 for (i = 0; i < routeTable->dwNumEntries; i++)
748 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
749 && routeTable->table[i].u1.ForwardType ==
750 MIB_IPROUTE_TYPE_INDIRECT)
752 RtlIpv4AddressToStringA((IN_ADDR *)&routeTable->table[i].dwForwardNextHop,
753 ptr->GatewayList.IpAddress.String);
754 RtlIpv4AddressToStringA((IN_ADDR *)&routeTable->table[i].dwForwardMask,
755 ptr->GatewayList.IpMask.String);
757 if (winsEnabled) {
758 ptr->HaveWins = TRUE;
759 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
760 primaryWINS.String, sizeof(primaryWINS.String));
761 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
762 secondaryWINS.String, sizeof(secondaryWINS.String));
764 if (ndx < table->numIndexes - 1)
765 ptr->Next = &pAdapterInfo[ndx + 1];
766 else
767 ptr->Next = NULL;
769 ptr->DhcpEnabled = TRUE;
771 ret = NO_ERROR;
773 HeapFree(GetProcessHeap(), 0, table);
775 else
776 ret = ERROR_OUTOFMEMORY;
777 HeapFree(GetProcessHeap(), 0, routeTable);
778 HeapFree(GetProcessHeap(), 0, ipAddrTable);
781 else
782 ret = ERROR_NO_DATA;
784 TRACE("returning %d\n", ret);
785 return ret;
788 static DWORD typeFromMibType(DWORD mib_type)
790 switch (mib_type)
792 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
793 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
794 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
795 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
796 default: return IF_TYPE_OTHER;
800 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
802 switch (mib_type)
804 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
805 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
806 default: return NET_IF_CONNECTION_DEDICATED;
810 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs, DWORD **masks)
812 ULONG ret, i, j;
813 MIB_IPADDRTABLE *at;
815 *num_addrs = 0;
816 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
817 for (i = 0; i < at->dwNumEntries; i++)
819 if (at->table[i].dwIndex == index) (*num_addrs)++;
821 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
823 HeapFree(GetProcessHeap(), 0, at);
824 return ERROR_OUTOFMEMORY;
826 if (!(*masks = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
828 HeapFree(GetProcessHeap(), 0, *addrs);
829 HeapFree(GetProcessHeap(), 0, at);
830 return ERROR_OUTOFMEMORY;
832 for (i = 0, j = 0; i < at->dwNumEntries; i++)
834 if (at->table[i].dwIndex == index)
836 (*addrs)[j] = at->table[i].dwAddr;
837 (*masks)[j] = at->table[i].dwMask;
838 j++;
841 HeapFree(GetProcessHeap(), 0, at);
842 return ERROR_SUCCESS;
845 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
847 const BYTE *addrp;
848 char *p = buf;
850 for (addrp = (const BYTE *)in_addr;
851 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
852 addrp++)
854 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
855 sprintf(p, "%d", *addrp);
856 else
857 p += sprintf(p, "%d.", *addrp);
859 return buf;
862 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
864 DWORD i, num_gateways = 0;
866 for (i = 0; i < routeTable->dwNumEntries; i++)
868 if (routeTable->table[i].dwForwardIfIndex == index &&
869 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
870 num_gateways++;
872 return num_gateways;
875 static DWORD mask_v4_to_prefix(DWORD m)
877 #ifdef HAVE___BUILTIN_POPCOUNT
878 return __builtin_popcount(m);
879 #else
880 m -= m >> 1 & 0x55555555;
881 m = (m & 0x33333333) + (m >> 2 & 0x33333333);
882 return ((m + (m >> 4)) & 0x0f0f0f0f) * 0x01010101 >> 24;
883 #endif
886 static DWORD mask_v6_to_prefix(SOCKET_ADDRESS *m)
888 const IN6_ADDR *mask = &((struct WS_sockaddr_in6 *)m->lpSockaddr)->sin6_addr;
889 DWORD ret = 0, i;
891 for (i = 0; i < 8; i++)
892 ret += mask_v4_to_prefix(mask->u.Word[i]);
893 return ret;
896 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
897 PMIB_IPFORWARDTABLE routeTable)
899 DWORD i;
900 PMIB_IPFORWARDROW row = NULL;
902 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
904 if (routeTable->table[i].dwForwardIfIndex == index &&
905 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
906 row = &routeTable->table[i];
908 return row;
911 static void fill_unicast_addr_data(IP_ADAPTER_ADDRESSES *aa, IP_ADAPTER_UNICAST_ADDRESS *ua)
913 /* Actually this information should be read somewhere from the system
914 * but it doesn't matter much for the bugs found so far.
915 * This information is required for DirectPlay8 games. */
916 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK)
918 ua->PrefixOrigin = IpPrefixOriginDhcp;
919 ua->SuffixOrigin = IpSuffixOriginDhcp;
921 else
923 ua->PrefixOrigin = IpPrefixOriginManual;
924 ua->SuffixOrigin = IpSuffixOriginManual;
927 /* The address is not duplicated in the network */
928 ua->DadState = IpDadStatePreferred;
930 /* Some address life time values, required even for non-dhcp addresses */
931 ua->ValidLifetime = 60000;
932 ua->PreferredLifetime = 60000;
933 ua->LeaseLifetime = 60000;
936 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
937 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
939 ULONG ret = ERROR_SUCCESS, i, j, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
940 DWORD *v4addrs = NULL, *v4masks = NULL;
941 SOCKET_ADDRESS *v6addrs = NULL, *v6masks = NULL;
942 PMIB_IPFORWARDTABLE routeTable = NULL;
943 BOOL output_gateways;
945 if ((flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS) || !(flags & GAA_FLAG_SKIP_UNICAST))
947 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
948 if (ret) return ret;
949 num_v4_gateways = count_v4_gateways(index, routeTable);
951 output_gateways = (flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS) && (family == WS_AF_INET || family == WS_AF_UNSPEC);
953 if (family == WS_AF_INET)
955 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
957 else if (family == WS_AF_INET6)
959 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
961 else if (family == WS_AF_UNSPEC)
963 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
964 if (!ret) ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
966 else
968 FIXME("address family %u unsupported\n", family);
969 ret = ERROR_NO_DATA;
971 if (ret)
973 HeapFree(GetProcessHeap(), 0, v4addrs);
974 HeapFree(GetProcessHeap(), 0, v4masks);
975 HeapFree(GetProcessHeap(), 0, v6addrs);
976 HeapFree(GetProcessHeap(), 0, v6masks);
977 HeapFree(GetProcessHeap(), 0, routeTable);
978 return ret;
981 total_size = sizeof(IP_ADAPTER_ADDRESSES);
982 total_size += 39; /* "{00000000-0000-0000-0000-000000000000}" */
983 total_size += IF_NAMESIZE * sizeof(WCHAR);
984 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
985 total_size += IF_NAMESIZE * sizeof(WCHAR);
986 if (flags & GAA_FLAG_INCLUDE_PREFIX)
988 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v4addrs;
989 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v6addrs;
990 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
991 for (i = 0; i < num_v6addrs; i++)
992 total_size += v6masks[i].iSockaddrLength;
994 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
995 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
996 if (output_gateways)
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;
1009 NET_LUID luid;
1010 GUID guid;
1012 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
1013 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
1014 aa->u.s.IfIndex = index;
1016 ConvertInterfaceIndexToLuid(index, &luid);
1017 ConvertInterfaceLuidToGuid(&luid, &guid);
1018 sprintf(ptr, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
1019 guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1],
1020 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5],
1021 guid.Data4[6], guid.Data4[7]);
1022 aa->AdapterName = ptr;
1023 ptr += 39;
1025 getInterfaceNameByIndex(index, name);
1026 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
1028 aa->FriendlyName = (WCHAR *)ptr;
1029 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
1030 *dst = *src;
1031 *dst++ = 0;
1032 ptr = (char *)dst;
1034 aa->Description = (WCHAR *)ptr;
1035 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
1036 *dst = *src;
1037 *dst++ = 0;
1038 ptr = (char *)dst;
1040 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
1041 num_v6addrs);
1043 buflen = MAX_INTERFACE_PHYSADDR;
1044 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
1045 aa->PhysicalAddressLength = buflen;
1046 aa->IfType = typeFromMibType(type);
1047 aa->ConnectionType = connectionTypeFromMibType(type);
1048 aa->Luid.Info.NetLuidIndex = index;
1049 aa->Luid.Info.IfType = aa->IfType;
1051 if (output_gateways && num_v4_gateways)
1053 PMIB_IPFORWARDROW adapterRow;
1055 if ((adapterRow = findIPv4Gateway(index, routeTable)))
1057 PIP_ADAPTER_GATEWAY_ADDRESS gw;
1058 PSOCKADDR_IN sin;
1060 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
1061 aa->FirstGatewayAddress = gw;
1063 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1064 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1065 sin = (PSOCKADDR_IN)ptr;
1066 sin->sin_family = WS_AF_INET;
1067 sin->sin_port = 0;
1068 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
1069 sizeof(DWORD));
1070 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
1071 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1072 gw->Next = NULL;
1073 ptr += sizeof(SOCKADDR_IN);
1076 if (num_v4addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1078 IP_ADAPTER_UNICAST_ADDRESS *ua;
1079 struct WS_sockaddr_in *sa;
1080 aa->u1.s1.Ipv4Enabled = TRUE;
1081 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1082 for (i = 0; i < num_v4addrs; i++)
1084 char addr_buf[16];
1086 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1087 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1088 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1089 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1090 if (num_v4_gateways)
1091 ua->u.s.Flags |= IP_ADAPTER_ADDRESS_DNS_ELIGIBLE;
1093 sa = (struct WS_sockaddr_in *)ua->Address.lpSockaddr;
1094 sa->sin_family = WS_AF_INET;
1095 sa->sin_addr.S_un.S_addr = v4addrs[i];
1096 sa->sin_port = 0;
1097 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
1098 debugstr_ipv4(&sa->sin_addr.S_un.S_addr, addr_buf));
1099 fill_unicast_addr_data(aa, ua);
1101 ua->OnLinkPrefixLength = mask_v4_to_prefix(v4masks[i]);
1103 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1104 if (i < num_v4addrs - 1)
1106 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1107 ua = ua->Next;
1111 if (num_v6addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1113 IP_ADAPTER_UNICAST_ADDRESS *ua;
1114 struct WS_sockaddr_in6 *sa;
1116 aa->u1.s1.Ipv6Enabled = TRUE;
1117 if (aa->FirstUnicastAddress)
1119 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
1121 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1122 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1124 else
1125 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1126 for (i = 0; i < num_v6addrs; i++)
1128 char addr_buf[46];
1130 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1131 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1132 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
1133 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1135 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
1136 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
1137 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
1138 debugstr_ipv6(sa, addr_buf));
1139 fill_unicast_addr_data(aa, ua);
1141 ua->OnLinkPrefixLength = mask_v6_to_prefix(&v6masks[i]);
1143 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1144 if (i < num_v6addrs - 1)
1146 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1147 ua = ua->Next;
1151 if (num_v4addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1153 IP_ADAPTER_PREFIX *prefix;
1155 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1156 for (i = 0; i < num_v4addrs; i++)
1158 char addr_buf[16];
1159 struct WS_sockaddr_in *sa;
1161 prefix->u.s.Length = sizeof(*prefix);
1162 prefix->u.s.Flags = 0;
1163 prefix->Next = NULL;
1164 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1165 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1167 sa = (struct WS_sockaddr_in *)prefix->Address.lpSockaddr;
1168 sa->sin_family = WS_AF_INET;
1169 sa->sin_addr.S_un.S_addr = v4addrs[i] & v4masks[i];
1170 sa->sin_port = 0;
1172 prefix->PrefixLength = mask_v4_to_prefix(v4masks[i]);
1174 TRACE("IPv4 network: %s/%u\n",
1175 debugstr_ipv4((const in_addr_t *)&sa->sin_addr.S_un.S_addr, addr_buf),
1176 prefix->PrefixLength);
1178 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1179 if (i < num_v4addrs - 1)
1181 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1182 prefix = prefix->Next;
1186 if (num_v6addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1188 IP_ADAPTER_PREFIX *prefix;
1190 if (aa->FirstPrefix)
1192 for (prefix = aa->FirstPrefix; prefix->Next; prefix = prefix->Next)
1194 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1195 prefix = (IP_ADAPTER_PREFIX *)ptr;
1197 else
1198 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1199 for (i = 0; i < num_v6addrs; i++)
1201 char addr_buf[46];
1202 struct WS_sockaddr_in6 *sa;
1203 const IN6_ADDR *addr, *mask;
1205 prefix->u.s.Length = sizeof(*prefix);
1206 prefix->u.s.Flags = 0;
1207 prefix->Next = NULL;
1208 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in6);
1209 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1211 sa = (struct WS_sockaddr_in6 *)prefix->Address.lpSockaddr;
1212 sa->sin6_family = WS_AF_INET6;
1213 sa->sin6_port = 0;
1214 sa->sin6_flowinfo = 0;
1215 addr = &((struct WS_sockaddr_in6 *)v6addrs[i].lpSockaddr)->sin6_addr;
1216 mask = &((struct WS_sockaddr_in6 *)v6masks[i].lpSockaddr)->sin6_addr;
1217 for (j = 0; j < 8; j++) sa->sin6_addr.u.Word[j] = addr->u.Word[j] & mask->u.Word[j];
1218 sa->sin6_scope_id = 0;
1220 prefix->PrefixLength = mask_v6_to_prefix(&v6masks[i]);
1222 TRACE("IPv6 network: %s/%u\n", debugstr_ipv6(sa, addr_buf), prefix->PrefixLength);
1224 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1225 if (i < num_v6addrs - 1)
1227 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1228 prefix = prefix->Next;
1233 getInterfaceMtuByName(name, &aa->Mtu);
1235 getInterfaceStatusByName(name, &status);
1236 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
1237 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
1238 else aa->OperStatus = IfOperStatusUnknown;
1240 *size = total_size;
1241 HeapFree(GetProcessHeap(), 0, routeTable);
1242 HeapFree(GetProcessHeap(), 0, v6addrs);
1243 HeapFree(GetProcessHeap(), 0, v6masks);
1244 HeapFree(GetProcessHeap(), 0, v4addrs);
1245 HeapFree(GetProcessHeap(), 0, v4masks);
1246 return ERROR_SUCCESS;
1249 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in *src )
1251 SOCKADDR_IN *s = (SOCKADDR_IN *)dst;
1253 s->sin_family = WS_AF_INET;
1254 s->sin_port = src->sin_port;
1255 memcpy( &s->sin_addr, &src->sin_addr, sizeof(IN_ADDR) );
1256 memset( (char *)s + FIELD_OFFSET( SOCKADDR_IN, sin_zero ), 0,
1257 sizeof(SOCKADDR_STORAGE) - FIELD_OFFSET( SOCKADDR_IN, sin_zero) );
1260 #if defined(HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6) || \
1261 (defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)) || \
1262 defined(HAVE_RES_GETSERVERS)
1263 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in6 *src )
1265 SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)dst;
1267 s->sin6_family = WS_AF_INET6;
1268 s->sin6_port = src->sin6_port;
1269 s->sin6_flowinfo = src->sin6_flowinfo;
1270 memcpy( &s->sin6_addr, &src->sin6_addr, sizeof(IN6_ADDR) );
1271 s->sin6_scope_id = src->sin6_scope_id;
1272 memset( (char *)s + sizeof(SOCKADDR_IN6), 0,
1273 sizeof(SOCKADDR_STORAGE) - sizeof(SOCKADDR_IN6) );
1275 #endif
1277 #ifdef HAVE_STRUCT___RES_STATE
1278 /* call res_init() just once because of a bug in Mac OS X 10.4 */
1279 /* Call once per thread on systems that have per-thread _res. */
1281 static CRITICAL_SECTION res_init_cs;
1282 static CRITICAL_SECTION_DEBUG res_init_cs_debug = {
1283 0, 0, &res_init_cs,
1284 { &res_init_cs_debug.ProcessLocksList, &res_init_cs_debug.ProcessLocksList },
1285 0, 0, { (DWORD_PTR)(__FILE__ ": res_init_cs") }
1287 static CRITICAL_SECTION res_init_cs = { &res_init_cs_debug, -1, 0, 0, 0, 0 };
1289 static void initialise_resolver(void)
1291 EnterCriticalSection(&res_init_cs);
1292 if ((_res.options & RES_INIT) == 0)
1293 res_init();
1294 LeaveCriticalSection(&res_init_cs);
1297 #ifdef HAVE_RES_GETSERVERS
1298 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1300 struct __res_state *state = &_res;
1301 int i, found = 0, total;
1302 SOCKADDR_STORAGE *addr = servers;
1303 union res_sockaddr_union *buf;
1305 initialise_resolver();
1307 total = res_getservers( state, NULL, 0 );
1309 if ((!servers || !num) && !ip4_only) return total;
1311 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(union res_sockaddr_union) );
1312 total = res_getservers( state, buf, total );
1314 for (i = 0; i < total; i++)
1316 if (buf[i].sin6.sin6_family == AF_INET6 && ip4_only) continue;
1317 if (buf[i].sin.sin_family != AF_INET && buf[i].sin6.sin6_family != AF_INET6) continue;
1319 found++;
1320 if (!servers || !num) continue;
1322 if (buf[i].sin6.sin6_family == AF_INET6)
1324 sockaddr_in6_to_WS_storage( addr, &buf[i].sin6 );
1326 else
1328 sockaddr_in_to_WS_storage( addr, &buf[i].sin );
1330 if (++addr >= servers + num) break;
1333 HeapFree( GetProcessHeap(), 0, buf );
1334 return found;
1336 #else
1338 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1340 int i, ip6_count = 0;
1341 SOCKADDR_STORAGE *addr;
1343 initialise_resolver();
1345 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1346 ip6_count = _res._u._ext.nscount6;
1347 #endif
1349 if (!servers || !num)
1351 num = _res.nscount;
1352 if (ip4_only) num -= ip6_count;
1353 return num;
1356 for (i = 0, addr = servers; addr < (servers + num) && i < _res.nscount; i++)
1358 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1359 if (_res._u._ext.nsaddrs[i] && _res._u._ext.nsaddrs[i]->sin6_family == AF_INET6)
1361 if (ip4_only) continue;
1362 sockaddr_in6_to_WS_storage( addr, _res._u._ext.nsaddrs[i] );
1364 else
1365 #endif
1367 sockaddr_in_to_WS_storage( addr, _res.nsaddr_list + i );
1369 addr++;
1371 return addr - servers;
1373 #endif
1374 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
1376 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1378 extern struct res_state *__res_get_state( void );
1379 extern int __res_getservers( struct res_state *, struct sockaddr_storage *, int );
1380 struct res_state *state = __res_get_state();
1381 int i, found = 0, total = __res_getservers( state, NULL, 0 );
1382 SOCKADDR_STORAGE *addr = servers;
1383 struct sockaddr_storage *buf;
1385 if ((!servers || !num) && !ip4_only) return total;
1387 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(struct sockaddr_storage) );
1388 total = __res_getservers( state, buf, total );
1390 for (i = 0; i < total; i++)
1392 if (buf[i].ss_family == AF_INET6 && ip4_only) continue;
1393 if (buf[i].ss_family != AF_INET && buf[i].ss_family != AF_INET6) continue;
1395 found++;
1396 if (!servers || !num) continue;
1398 if (buf[i].ss_family == AF_INET6)
1400 sockaddr_in6_to_WS_storage( addr, (struct sockaddr_in6 *)(buf + i) );
1402 else
1404 sockaddr_in_to_WS_storage( addr, (struct sockaddr_in *)(buf + i) );
1406 if (++addr >= servers + num) break;
1409 HeapFree( GetProcessHeap(), 0, buf );
1410 return found;
1412 #else
1414 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1416 FIXME("Unimplemented on this system\n");
1417 return 0;
1419 #endif
1421 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
1423 int num = get_dns_servers( NULL, 0, FALSE );
1424 DWORD size;
1426 size = num * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR_STORAGE));
1427 if (!address || *len < size)
1429 *len = size;
1430 return ERROR_BUFFER_OVERFLOW;
1432 *len = size;
1433 if (num > 0)
1435 PIP_ADAPTER_DNS_SERVER_ADDRESS addr = address;
1436 SOCKADDR_STORAGE *sock_addrs = (SOCKADDR_STORAGE *)(address + num);
1437 int i;
1439 get_dns_servers( sock_addrs, num, FALSE );
1441 for (i = 0; i < num; i++, addr = addr->Next)
1443 addr->u.s.Length = sizeof(*addr);
1444 if (sock_addrs[i].ss_family == WS_AF_INET6)
1445 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN6);
1446 else
1447 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1448 addr->Address.lpSockaddr = (SOCKADDR *)(sock_addrs + i);
1449 if (i == num - 1)
1450 addr->Next = NULL;
1451 else
1452 addr->Next = addr + 1;
1455 return ERROR_SUCCESS;
1458 #ifdef HAVE_STRUCT___RES_STATE
1459 static BOOL is_ip_address_string(const char *str)
1461 struct in_addr in;
1462 int ret;
1464 ret = inet_aton(str, &in);
1465 return ret != 0;
1467 #endif
1469 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1471 ULONG size;
1472 const char *found_suffix = "";
1473 /* Always return a NULL-terminated string, even if it's empty. */
1475 #ifdef HAVE_STRUCT___RES_STATE
1477 ULONG i;
1478 initialise_resolver();
1479 for (i = 0; !*found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1481 /* This uses a heuristic to select a DNS suffix:
1482 * the first, non-IP address string is selected.
1484 if (!is_ip_address_string(_res.dnsrch[i]))
1485 found_suffix = _res.dnsrch[i];
1488 #endif
1490 size = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, NULL, 0 ) * sizeof(WCHAR);
1491 if (!suffix || *len < size)
1493 *len = size;
1494 return ERROR_BUFFER_OVERFLOW;
1496 *len = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, suffix, *len / sizeof(WCHAR) ) * sizeof(WCHAR);
1497 return ERROR_SUCCESS;
1500 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1501 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1503 InterfaceIndexTable *table;
1504 ULONG i, size, dns_server_size = 0, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1506 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1508 if (!buflen) return ERROR_INVALID_PARAMETER;
1510 get_interface_indices( FALSE, &table );
1511 if (!table || !table->numIndexes)
1513 HeapFree(GetProcessHeap(), 0, table);
1514 return ERROR_NO_DATA;
1516 total_size = 0;
1517 for (i = 0; i < table->numIndexes; i++)
1519 size = 0;
1520 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1522 HeapFree(GetProcessHeap(), 0, table);
1523 return ret;
1525 total_size += size;
1527 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1529 /* Since DNS servers aren't really per adapter, get enough space for a
1530 * single copy of them.
1532 get_dns_server_addresses(NULL, &dns_server_size);
1533 total_size += dns_server_size;
1535 /* Since DNS suffix also isn't really per adapter, get enough space for a
1536 * single copy of it.
1538 get_dns_suffix(NULL, &dns_suffix_size);
1539 total_size += dns_suffix_size;
1540 if (aa && *buflen >= total_size)
1542 ULONG bytes_left = size = total_size;
1543 PIP_ADAPTER_ADDRESSES first_aa = aa;
1544 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1545 WCHAR *dnsSuffix;
1547 for (i = 0; i < table->numIndexes; i++)
1549 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1551 HeapFree(GetProcessHeap(), 0, table);
1552 return ret;
1554 if (i < table->numIndexes - 1)
1556 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1557 aa = aa->Next;
1558 size = bytes_left -= size;
1561 if (dns_server_size)
1563 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1564 get_dns_server_addresses(firstDns, &dns_server_size);
1565 for (aa = first_aa; aa; aa = aa->Next)
1567 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1568 aa->FirstDnsServerAddress = firstDns;
1571 aa = first_aa;
1572 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1573 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1574 for (; aa; aa = aa->Next)
1576 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1577 aa->DnsSuffix = dnsSuffix;
1578 else
1579 aa->DnsSuffix = dnsSuffix + dns_suffix_size / sizeof(WCHAR) - 1;
1581 ret = ERROR_SUCCESS;
1583 else
1585 ret = ERROR_BUFFER_OVERFLOW;
1586 *buflen = total_size;
1589 TRACE("num adapters %u\n", table->numIndexes);
1590 HeapFree(GetProcessHeap(), 0, table);
1591 return ret;
1594 /******************************************************************
1595 * GetBestInterface (IPHLPAPI.@)
1597 * Get the interface, with the best route for the given IP address.
1599 * PARAMS
1600 * dwDestAddr [In] IP address to search the interface for
1601 * pdwBestIfIndex [Out] found best interface
1603 * RETURNS
1604 * Success: NO_ERROR
1605 * Failure: error code from winerror.h
1607 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1609 struct WS_sockaddr_in sa_in;
1610 memset(&sa_in, 0, sizeof(sa_in));
1611 sa_in.sin_family = WS_AF_INET;
1612 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1613 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1616 /******************************************************************
1617 * GetBestInterfaceEx (IPHLPAPI.@)
1619 * Get the interface, with the best route for the given IP address.
1621 * PARAMS
1622 * dwDestAddr [In] IP address to search the interface for
1623 * pdwBestIfIndex [Out] found best interface
1625 * RETURNS
1626 * Success: NO_ERROR
1627 * Failure: error code from winerror.h
1629 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1631 DWORD ret;
1633 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1634 if (!pDestAddr || !pdwBestIfIndex)
1635 ret = ERROR_INVALID_PARAMETER;
1636 else {
1637 MIB_IPFORWARDROW ipRow;
1639 if (pDestAddr->sa_family == WS_AF_INET) {
1640 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1641 if (ret == ERROR_SUCCESS)
1642 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1643 } else {
1644 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1645 ret = ERROR_NOT_SUPPORTED;
1648 TRACE("returning %d\n", ret);
1649 return ret;
1653 /******************************************************************
1654 * GetBestRoute (IPHLPAPI.@)
1656 * Get the best route for the given IP address.
1658 * PARAMS
1659 * dwDestAddr [In] IP address to search the best route for
1660 * dwSourceAddr [In] optional source IP address
1661 * pBestRoute [Out] found best route
1663 * RETURNS
1664 * Success: NO_ERROR
1665 * Failure: error code from winerror.h
1667 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1669 PMIB_IPFORWARDTABLE table;
1670 DWORD ret;
1672 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1673 dwSourceAddr, pBestRoute);
1674 if (!pBestRoute)
1675 return ERROR_INVALID_PARAMETER;
1677 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1678 if (!ret) {
1679 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1681 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1682 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1683 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1684 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1685 DWORD numShifts, mask;
1687 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1688 mask && mask & 1; mask >>= 1, numShifts++)
1690 if (numShifts > matchedBits) {
1691 matchedBits = numShifts;
1692 matchedNdx = ndx;
1694 else if (!matchedBits) {
1695 matchedNdx = ndx;
1699 if (matchedNdx < table->dwNumEntries) {
1700 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1701 ret = ERROR_SUCCESS;
1703 else {
1704 /* No route matches, which can happen if there's no default route. */
1705 ret = ERROR_HOST_UNREACHABLE;
1707 HeapFree(GetProcessHeap(), 0, table);
1709 TRACE("returning %d\n", ret);
1710 return ret;
1714 /******************************************************************
1715 * GetFriendlyIfIndex (IPHLPAPI.@)
1717 * Get a "friendly" version of IfIndex, which is one that doesn't
1718 * have the top byte set. Doesn't validate whether IfIndex is a valid
1719 * adapter index.
1721 * PARAMS
1722 * IfIndex [In] interface index to get the friendly one for
1724 * RETURNS
1725 * A friendly version of IfIndex.
1727 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1729 /* windows doesn't validate these, either, just makes sure the top byte is
1730 cleared. I assume my ifenum module never gives an index with the top
1731 byte set. */
1732 TRACE("returning %d\n", IfIndex);
1733 return IfIndex;
1737 /******************************************************************
1738 * GetIfEntry (IPHLPAPI.@)
1740 * Get information about an interface.
1742 * PARAMS
1743 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1744 * Out: interface information
1746 * RETURNS
1747 * Success: NO_ERROR
1748 * Failure: error code from winerror.h
1750 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1752 DWORD ret;
1753 char nameBuf[MAX_ADAPTER_NAME];
1754 char *name;
1756 TRACE("pIfRow %p\n", pIfRow);
1757 if (!pIfRow)
1758 return ERROR_INVALID_PARAMETER;
1760 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1761 if (name) {
1762 ret = getInterfaceEntryByName(name, pIfRow);
1763 if (ret == NO_ERROR)
1764 ret = getInterfaceStatsByName(name, pIfRow);
1766 else
1767 ret = ERROR_INVALID_DATA;
1768 TRACE("returning %d\n", ret);
1769 return ret;
1772 /******************************************************************
1773 * GetIfEntry2 (IPHLPAPI.@)
1775 DWORD WINAPI GetIfEntry2( MIB_IF_ROW2 *row2 )
1777 DWORD ret, len = ARRAY_SIZE(row2->Description);
1778 char buf[MAX_ADAPTER_NAME], *name;
1779 MIB_IFROW row;
1781 TRACE("%p\n", row2);
1783 if (!row2 || (!(name = getInterfaceNameByIndex( row2->InterfaceIndex, buf )) &&
1784 !(name = getInterfaceNameByIndex( row2->InterfaceLuid.Info.NetLuidIndex, buf ))))
1786 return ERROR_INVALID_PARAMETER;
1788 if ((ret = getInterfaceEntryByName( name, &row ))) return ret;
1789 if ((ret = getInterfaceStatsByName( name, &row ))) return ret;
1791 memset( row2, 0, sizeof(*row2) );
1792 row2->InterfaceLuid.Info.Reserved = 0;
1793 row2->InterfaceLuid.Info.NetLuidIndex = row.dwIndex;
1794 row2->InterfaceLuid.Info.IfType = row.dwType;
1795 row2->InterfaceIndex = row.dwIndex;
1796 ConvertInterfaceLuidToGuid( &row2->InterfaceLuid, &row2->InterfaceGuid );
1797 row2->Type = row.dwType;
1798 row2->Mtu = row.dwMtu;
1799 MultiByteToWideChar( CP_UNIXCP, 0, (const char *)row.bDescr, -1, row2->Description, len );
1800 row2->PhysicalAddressLength = row.dwPhysAddrLen;
1801 memcpy( &row2->PhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1802 memcpy( &row2->PermanentPhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1803 row2->OperStatus = IfOperStatusUp;
1804 row2->AdminStatus = NET_IF_ADMIN_STATUS_UP;
1805 row2->MediaConnectState = MediaConnectStateConnected;
1806 row2->ConnectionType = NET_IF_CONNECTION_DEDICATED;
1808 /* stats */
1809 row2->InOctets = row.dwInOctets;
1810 row2->InUcastPkts = row.dwInUcastPkts;
1811 row2->InNUcastPkts = row.dwInNUcastPkts;
1812 row2->InDiscards = row.dwInDiscards;
1813 row2->InErrors = row.dwInErrors;
1814 row2->InUnknownProtos = row.dwInUnknownProtos;
1815 row2->OutOctets = row.dwOutOctets;
1816 row2->OutUcastPkts = row.dwOutUcastPkts;
1817 row2->OutNUcastPkts = row.dwOutNUcastPkts;
1818 row2->OutDiscards = row.dwOutDiscards;
1819 row2->OutErrors = row.dwOutErrors;
1821 return NO_ERROR;
1824 static int IfTableSorter(const void *a, const void *b)
1826 int ret;
1828 if (a && b)
1829 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1830 else
1831 ret = 0;
1832 return ret;
1836 /******************************************************************
1837 * GetIfTable (IPHLPAPI.@)
1839 * Get a table of local interfaces.
1841 * PARAMS
1842 * pIfTable [Out] buffer for local interfaces table
1843 * pdwSize [In/Out] length of output buffer
1844 * bOrder [In] whether to sort the table
1846 * RETURNS
1847 * Success: NO_ERROR
1848 * Failure: error code from winerror.h
1850 * NOTES
1851 * If pdwSize is less than required, the function will return
1852 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1853 * size.
1854 * If bOrder is true, the returned table will be sorted by interface index.
1856 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1858 DWORD ret;
1860 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pIfTable, pdwSize, bOrder);
1862 if (!pdwSize)
1863 ret = ERROR_INVALID_PARAMETER;
1864 else {
1865 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1866 ULONG size = sizeof(MIB_IFTABLE);
1868 if (numInterfaces > 1)
1869 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1870 if (!pIfTable || *pdwSize < size) {
1871 *pdwSize = size;
1872 ret = ERROR_INSUFFICIENT_BUFFER;
1874 else {
1875 InterfaceIndexTable *table;
1876 get_interface_indices( FALSE, &table );
1878 if (table) {
1879 size = sizeof(MIB_IFTABLE);
1880 if (table->numIndexes > 1)
1881 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1882 if (*pdwSize < size) {
1883 *pdwSize = size;
1884 ret = ERROR_INSUFFICIENT_BUFFER;
1886 else {
1887 DWORD ndx;
1889 *pdwSize = size;
1890 pIfTable->dwNumEntries = 0;
1891 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1892 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1893 GetIfEntry(&pIfTable->table[ndx]);
1894 pIfTable->dwNumEntries++;
1896 if (bOrder)
1897 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1898 IfTableSorter);
1899 ret = NO_ERROR;
1901 HeapFree(GetProcessHeap(), 0, table);
1903 else
1904 ret = ERROR_OUTOFMEMORY;
1907 TRACE("returning %d\n", ret);
1908 return ret;
1911 /******************************************************************
1912 * GetIfTable2Ex (IPHLPAPI.@)
1914 DWORD WINAPI GetIfTable2Ex( MIB_IF_TABLE_LEVEL level, MIB_IF_TABLE2 **table )
1916 DWORD i, nb_interfaces, size = sizeof(MIB_IF_TABLE2);
1917 InterfaceIndexTable *index_table;
1918 MIB_IF_TABLE2 *ret;
1920 TRACE( "level %u, table %p\n", level, table );
1922 if (!table || level > MibIfTableNormalWithoutStatistics)
1923 return ERROR_INVALID_PARAMETER;
1925 if (level != MibIfTableNormal)
1926 FIXME("level %u not fully supported\n", level);
1928 if ((nb_interfaces = get_interface_indices( FALSE, NULL )) > 1)
1929 size += (nb_interfaces - 1) * sizeof(MIB_IF_ROW2);
1931 if (!(ret = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
1933 get_interface_indices( FALSE, &index_table );
1934 if (!index_table)
1936 HeapFree( GetProcessHeap(), 0, ret );
1937 return ERROR_OUTOFMEMORY;
1940 ret->NumEntries = 0;
1941 for (i = 0; i < index_table->numIndexes; i++)
1943 ret->Table[i].InterfaceIndex = index_table->indexes[i];
1944 GetIfEntry2( &ret->Table[i] );
1945 ret->NumEntries++;
1948 HeapFree( GetProcessHeap(), 0, index_table );
1949 *table = ret;
1950 return NO_ERROR;
1953 /******************************************************************
1954 * GetIfTable2 (IPHLPAPI.@)
1956 DWORD WINAPI GetIfTable2( MIB_IF_TABLE2 **table )
1958 TRACE( "table %p\n", table );
1959 return GetIfTable2Ex(MibIfTableNormal, table);
1962 /******************************************************************
1963 * GetInterfaceInfo (IPHLPAPI.@)
1965 * Get a list of network interface adapters.
1967 * PARAMS
1968 * pIfTable [Out] buffer for interface adapters
1969 * dwOutBufLen [Out] if buffer is too small, returns required size
1971 * RETURNS
1972 * Success: NO_ERROR
1973 * Failure: error code from winerror.h
1975 * BUGS
1976 * MSDN states this should return non-loopback interfaces only.
1978 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1980 DWORD ret;
1982 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1983 if (!dwOutBufLen)
1984 ret = ERROR_INVALID_PARAMETER;
1985 else {
1986 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1987 ULONG size = sizeof(IP_INTERFACE_INFO);
1989 if (numInterfaces > 1)
1990 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1991 if (!pIfTable || *dwOutBufLen < size) {
1992 *dwOutBufLen = size;
1993 ret = ERROR_INSUFFICIENT_BUFFER;
1995 else {
1996 InterfaceIndexTable *table;
1997 get_interface_indices( FALSE, &table );
1999 if (table) {
2000 size = sizeof(IP_INTERFACE_INFO);
2001 if (table->numIndexes > 1)
2002 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
2003 if (*dwOutBufLen < size) {
2004 *dwOutBufLen = size;
2005 ret = ERROR_INSUFFICIENT_BUFFER;
2007 else {
2008 DWORD ndx;
2009 char nameBuf[MAX_ADAPTER_NAME];
2011 *dwOutBufLen = size;
2012 pIfTable->NumAdapters = 0;
2013 for (ndx = 0; ndx < table->numIndexes; ndx++) {
2014 const char *walker, *name;
2015 WCHAR *assigner;
2017 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
2018 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
2019 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
2020 walker && *walker &&
2021 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
2022 walker++, assigner++)
2023 *assigner = *walker;
2024 *assigner = 0;
2025 pIfTable->NumAdapters++;
2027 ret = NO_ERROR;
2029 HeapFree(GetProcessHeap(), 0, table);
2031 else
2032 ret = ERROR_OUTOFMEMORY;
2035 TRACE("returning %d\n", ret);
2036 return ret;
2040 /******************************************************************
2041 * GetIpAddrTable (IPHLPAPI.@)
2043 * Get interface-to-IP address mapping table.
2045 * PARAMS
2046 * pIpAddrTable [Out] buffer for mapping table
2047 * pdwSize [In/Out] length of output buffer
2048 * bOrder [In] whether to sort the table
2050 * RETURNS
2051 * Success: NO_ERROR
2052 * Failure: error code from winerror.h
2054 * NOTES
2055 * If pdwSize is less than required, the function will return
2056 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2057 * size.
2058 * If bOrder is true, the returned table will be sorted by the next hop and
2059 * an assortment of arbitrary parameters.
2061 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
2063 DWORD ret;
2065 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
2066 (DWORD)bOrder);
2067 if (!pdwSize)
2068 ret = ERROR_INVALID_PARAMETER;
2069 else {
2070 PMIB_IPADDRTABLE table;
2072 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
2073 if (ret == NO_ERROR)
2075 ULONG size = FIELD_OFFSET(MIB_IPADDRTABLE, table[table->dwNumEntries]);
2077 if (!pIpAddrTable || *pdwSize < size) {
2078 *pdwSize = size;
2079 ret = ERROR_INSUFFICIENT_BUFFER;
2081 else {
2082 *pdwSize = size;
2083 memcpy(pIpAddrTable, table, size);
2084 /* sort by numeric IP value */
2085 if (bOrder)
2086 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
2087 sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
2088 /* sort ensuring loopback interfaces are in the end */
2089 else
2090 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
2091 sizeof(MIB_IPADDRROW), IpAddrTableLoopbackSorter);
2092 ret = NO_ERROR;
2094 HeapFree(GetProcessHeap(), 0, table);
2097 TRACE("returning %d\n", ret);
2098 return ret;
2102 /******************************************************************
2103 * GetIpForwardTable (IPHLPAPI.@)
2105 * Get the route table.
2107 * PARAMS
2108 * pIpForwardTable [Out] buffer for route table
2109 * pdwSize [In/Out] length of output buffer
2110 * bOrder [In] whether to sort the table
2112 * RETURNS
2113 * Success: NO_ERROR
2114 * Failure: error code from winerror.h
2116 * NOTES
2117 * If pdwSize is less than required, the function will return
2118 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2119 * size.
2120 * If bOrder is true, the returned table will be sorted by the next hop and
2121 * an assortment of arbitrary parameters.
2123 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
2125 DWORD ret;
2126 PMIB_IPFORWARDTABLE table;
2128 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
2130 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2132 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
2133 if (!ret) {
2134 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
2135 if (!pIpForwardTable || *pdwSize < size) {
2136 *pdwSize = size;
2137 ret = ERROR_INSUFFICIENT_BUFFER;
2139 else {
2140 *pdwSize = size;
2141 memcpy(pIpForwardTable, table, size);
2143 HeapFree(GetProcessHeap(), 0, table);
2145 TRACE("returning %d\n", ret);
2146 return ret;
2150 /******************************************************************
2151 * GetIpNetTable (IPHLPAPI.@)
2153 * Get the IP-to-physical address mapping table.
2155 * PARAMS
2156 * pIpNetTable [Out] buffer for mapping table
2157 * pdwSize [In/Out] length of output buffer
2158 * bOrder [In] whether to sort the table
2160 * RETURNS
2161 * Success: NO_ERROR
2162 * Failure: error code from winerror.h
2164 * NOTES
2165 * If pdwSize is less than required, the function will return
2166 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2167 * size.
2168 * If bOrder is true, the returned table will be sorted by IP address.
2170 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
2172 DWORD ret;
2173 PMIB_IPNETTABLE table;
2175 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
2177 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2179 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
2180 if (!ret) {
2181 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
2182 if (!pIpNetTable || *pdwSize < size) {
2183 *pdwSize = size;
2184 ret = ERROR_INSUFFICIENT_BUFFER;
2186 else {
2187 *pdwSize = size;
2188 memcpy(pIpNetTable, table, size);
2190 HeapFree(GetProcessHeap(), 0, table);
2192 TRACE("returning %d\n", ret);
2193 return ret;
2196 /* Gets the DNS server list into the list beginning at list. Assumes that
2197 * a single server address may be placed at list if *len is at least
2198 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
2199 * and assumes that all remaining DNS servers are contiguously located
2200 * beginning at firstDynamic. On input, *len is assumed to be the total number
2201 * of bytes available for all DNS servers, and is ignored if list is NULL.
2202 * On return, *len is set to the total number of bytes required for all DNS
2203 * servers.
2204 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
2205 * ERROR_SUCCESS otherwise.
2207 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
2208 PIP_ADDR_STRING firstDynamic, DWORD *len)
2210 DWORD size;
2211 int num = get_dns_servers( NULL, 0, TRUE );
2213 size = num * sizeof(IP_ADDR_STRING);
2214 if (!list || *len < size) {
2215 *len = size;
2216 return ERROR_BUFFER_OVERFLOW;
2218 *len = size;
2219 if (num > 0) {
2220 PIP_ADDR_STRING ptr;
2221 int i;
2222 SOCKADDR_STORAGE *addr = HeapAlloc( GetProcessHeap(), 0, num * sizeof(SOCKADDR_STORAGE) );
2224 get_dns_servers( addr, num, TRUE );
2226 for (i = 0, ptr = list; i < num; i++, ptr = ptr->Next) {
2227 RtlIpv4AddressToStringA((IN_ADDR *)&((struct sockaddr_in *)(addr + i))->sin_addr.s_addr,
2228 ptr->IpAddress.String);
2229 if (i == num - 1)
2230 ptr->Next = NULL;
2231 else if (i == 0)
2232 ptr->Next = firstDynamic;
2233 else
2234 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
2236 HeapFree( GetProcessHeap(), 0, addr );
2238 return ERROR_SUCCESS;
2241 /******************************************************************
2242 * GetNetworkParams (IPHLPAPI.@)
2244 * Get the network parameters for the local computer.
2246 * PARAMS
2247 * pFixedInfo [Out] buffer for network parameters
2248 * pOutBufLen [In/Out] length of output buffer
2250 * RETURNS
2251 * Success: NO_ERROR
2252 * Failure: error code from winerror.h
2254 * NOTES
2255 * If pOutBufLen is less than required, the function will return
2256 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
2257 * size.
2259 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
2261 DWORD ret, size, serverListSize;
2262 LONG regReturn;
2263 HKEY hKey;
2265 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
2266 if (!pOutBufLen)
2267 return ERROR_INVALID_PARAMETER;
2269 get_dns_server_list(NULL, NULL, &serverListSize);
2270 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
2271 if (!pFixedInfo || *pOutBufLen < size) {
2272 *pOutBufLen = size;
2273 return ERROR_BUFFER_OVERFLOW;
2276 memset(pFixedInfo, 0, size);
2277 size = sizeof(pFixedInfo->HostName);
2278 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
2279 size = sizeof(pFixedInfo->DomainName);
2280 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
2281 get_dns_server_list(&pFixedInfo->DnsServerList,
2282 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
2283 &serverListSize);
2284 /* Assume the first DNS server in the list is the "current" DNS server: */
2285 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
2286 pFixedInfo->NodeType = HYBRID_NODETYPE;
2287 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2288 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
2289 if (regReturn != ERROR_SUCCESS)
2290 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2291 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
2292 &hKey);
2293 if (regReturn == ERROR_SUCCESS)
2295 DWORD size = sizeof(pFixedInfo->ScopeId);
2297 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
2298 RegCloseKey(hKey);
2301 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
2302 I suppose could also check for a listener on port 53 to set EnableDns */
2303 ret = NO_ERROR;
2304 TRACE("returning %d\n", ret);
2305 return ret;
2309 /******************************************************************
2310 * GetNumberOfInterfaces (IPHLPAPI.@)
2312 * Get the number of interfaces.
2314 * PARAMS
2315 * pdwNumIf [Out] number of interfaces
2317 * RETURNS
2318 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
2320 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
2322 DWORD ret;
2324 TRACE("pdwNumIf %p\n", pdwNumIf);
2325 if (!pdwNumIf)
2326 ret = ERROR_INVALID_PARAMETER;
2327 else {
2328 *pdwNumIf = get_interface_indices( FALSE, NULL );
2329 ret = NO_ERROR;
2331 TRACE("returning %d\n", ret);
2332 return ret;
2336 /******************************************************************
2337 * GetPerAdapterInfo (IPHLPAPI.@)
2339 * Get information about an adapter corresponding to an interface.
2341 * PARAMS
2342 * IfIndex [In] interface info
2343 * pPerAdapterInfo [Out] buffer for per adapter info
2344 * pOutBufLen [In/Out] length of output buffer
2346 * RETURNS
2347 * Success: NO_ERROR
2348 * Failure: error code from winerror.h
2350 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
2352 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
2353 DWORD ret = NO_ERROR;
2355 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
2357 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
2359 if (!isIfIndexLoopback(IfIndex)) {
2360 get_dns_server_list(NULL, NULL, &serverListSize);
2361 if (serverListSize > sizeof(IP_ADDR_STRING))
2362 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
2364 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
2366 *pOutBufLen = bytesNeeded;
2367 return ERROR_BUFFER_OVERFLOW;
2370 memset(pPerAdapterInfo, 0, bytesNeeded);
2371 if (!isIfIndexLoopback(IfIndex)) {
2372 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
2373 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
2374 &serverListSize);
2375 /* Assume the first DNS server in the list is the "current" DNS server: */
2376 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
2378 return ret;
2382 /******************************************************************
2383 * GetRTTAndHopCount (IPHLPAPI.@)
2385 * Get round-trip time (RTT) and hop count.
2387 * PARAMS
2389 * DestIpAddress [In] destination address to get the info for
2390 * HopCount [Out] retrieved hop count
2391 * MaxHops [In] maximum hops to search for the destination
2392 * RTT [Out] RTT in milliseconds
2394 * RETURNS
2395 * Success: TRUE
2396 * Failure: FALSE
2398 * FIXME
2399 * Stub, returns FALSE.
2401 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
2403 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
2404 DestIpAddress, HopCount, MaxHops, RTT);
2405 return FALSE;
2409 /******************************************************************
2410 * GetTcpTable (IPHLPAPI.@)
2412 * Get the table of active TCP connections.
2414 * PARAMS
2415 * pTcpTable [Out] buffer for TCP connections table
2416 * pdwSize [In/Out] length of output buffer
2417 * bOrder [In] whether to order the table
2419 * RETURNS
2420 * Success: NO_ERROR
2421 * Failure: error code from winerror.h
2423 * NOTES
2424 * If pdwSize is less than required, the function will return
2425 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2426 * the required byte size.
2427 * If bOrder is true, the returned table will be sorted, first by
2428 * local address and port number, then by remote address and port
2429 * number.
2431 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
2433 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
2434 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, WS_AF_INET, TCP_TABLE_BASIC_ALL, 0);
2437 /******************************************************************
2438 * GetExtendedTcpTable (IPHLPAPI.@)
2440 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
2441 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
2443 DWORD ret, size;
2444 void *table;
2446 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2447 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2449 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2451 if (TableClass >= TCP_TABLE_OWNER_MODULE_LISTENER)
2452 FIXME("module classes not fully supported\n");
2454 switch (ulAf)
2456 case WS_AF_INET:
2457 ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size);
2458 break;
2460 case WS_AF_INET6:
2461 ret = build_tcp6_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size);
2462 break;
2464 default:
2465 FIXME("ulAf = %u not supported\n", ulAf);
2466 ret = ERROR_NOT_SUPPORTED;
2469 if (ret)
2470 return ret;
2472 if (!pTcpTable || *pdwSize < size)
2474 *pdwSize = size;
2475 ret = ERROR_INSUFFICIENT_BUFFER;
2477 else
2479 *pdwSize = size;
2480 memcpy(pTcpTable, table, size);
2482 HeapFree(GetProcessHeap(), 0, table);
2483 return ret;
2486 /******************************************************************
2487 * GetUdpTable (IPHLPAPI.@)
2489 * Get a table of active UDP connections.
2491 * PARAMS
2492 * pUdpTable [Out] buffer for UDP connections table
2493 * pdwSize [In/Out] length of output buffer
2494 * bOrder [In] whether to order the table
2496 * RETURNS
2497 * Success: NO_ERROR
2498 * Failure: error code from winerror.h
2500 * NOTES
2501 * If pdwSize is less than required, the function will return
2502 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2503 * required byte size.
2504 * If bOrder is true, the returned table will be sorted, first by
2505 * local address, then by local port number.
2507 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2509 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, WS_AF_INET, UDP_TABLE_BASIC, 0);
2512 /******************************************************************
2513 * GetUdp6Table (IPHLPAPI.@)
2515 DWORD WINAPI GetUdp6Table(PMIB_UDP6TABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2517 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, WS_AF_INET6, UDP_TABLE_BASIC, 0);
2520 /******************************************************************
2521 * GetExtendedUdpTable (IPHLPAPI.@)
2523 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
2524 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
2526 DWORD ret, size;
2527 void *table;
2529 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2530 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2532 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2534 if (TableClass == UDP_TABLE_OWNER_MODULE)
2535 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2537 switch (ulAf)
2539 case WS_AF_INET:
2540 ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size);
2541 break;
2543 case WS_AF_INET6:
2544 ret = build_udp6_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size);
2545 break;
2547 default:
2548 FIXME("ulAf = %u not supported\n", ulAf);
2549 ret = ERROR_NOT_SUPPORTED;
2552 if (ret)
2553 return ret;
2555 if (!pUdpTable || *pdwSize < size)
2557 *pdwSize = size;
2558 ret = ERROR_INSUFFICIENT_BUFFER;
2560 else
2562 *pdwSize = size;
2563 memcpy(pUdpTable, table, size);
2565 HeapFree(GetProcessHeap(), 0, table);
2566 return ret;
2569 DWORD WINAPI GetUnicastIpAddressEntry(MIB_UNICASTIPADDRESS_ROW *row)
2571 IP_ADAPTER_ADDRESSES *aa, *ptr;
2572 ULONG size = 0;
2573 DWORD ret;
2575 TRACE("%p\n", row);
2577 if (!row)
2578 return ERROR_INVALID_PARAMETER;
2580 ret = GetAdaptersAddresses(row->Address.si_family, 0, NULL, NULL, &size);
2581 if (ret != ERROR_BUFFER_OVERFLOW)
2582 return ret;
2583 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, size)))
2584 return ERROR_OUTOFMEMORY;
2585 if ((ret = GetAdaptersAddresses(row->Address.si_family, 0, NULL, ptr, &size)))
2587 HeapFree(GetProcessHeap(), 0, ptr);
2588 return ret;
2591 ret = ERROR_FILE_NOT_FOUND;
2592 for (aa = ptr; aa; aa = aa->Next)
2594 IP_ADAPTER_UNICAST_ADDRESS *ua;
2596 if (aa->u.s.IfIndex != row->InterfaceIndex &&
2597 memcmp(&aa->Luid, &row->InterfaceLuid, sizeof(row->InterfaceLuid)))
2598 continue;
2599 ret = ERROR_NOT_FOUND;
2601 ua = aa->FirstUnicastAddress;
2602 while (ua)
2604 SOCKADDR_INET *uaaddr = (SOCKADDR_INET *)ua->Address.lpSockaddr;
2606 if ((row->Address.si_family == WS_AF_INET6 &&
2607 !memcmp(&row->Address.Ipv6.sin6_addr, &uaaddr->Ipv6.sin6_addr, sizeof(uaaddr->Ipv6.sin6_addr))) ||
2608 (row->Address.si_family == WS_AF_INET &&
2609 row->Address.Ipv4.sin_addr.S_un.S_addr == uaaddr->Ipv4.sin_addr.S_un.S_addr))
2611 memcpy(&row->InterfaceLuid, &aa->Luid, sizeof(aa->Luid));
2612 row->InterfaceIndex = aa->u.s.IfIndex;
2613 row->PrefixOrigin = ua->PrefixOrigin;
2614 row->SuffixOrigin = ua->SuffixOrigin;
2615 row->ValidLifetime = ua->ValidLifetime;
2616 row->PreferredLifetime = ua->PreferredLifetime;
2617 row->OnLinkPrefixLength = ua->OnLinkPrefixLength;
2618 row->SkipAsSource = 0;
2619 row->DadState = ua->DadState;
2620 if (row->Address.si_family == WS_AF_INET6)
2621 row->ScopeId.u.Value = row->Address.Ipv6.sin6_scope_id;
2622 else
2623 row->ScopeId.u.Value = 0;
2624 NtQuerySystemTime(&row->CreationTimeStamp);
2625 HeapFree(GetProcessHeap(), 0, ptr);
2626 return NO_ERROR;
2628 ua = ua->Next;
2631 HeapFree(GetProcessHeap(), 0, ptr);
2633 return ret;
2636 DWORD WINAPI GetUnicastIpAddressTable(ADDRESS_FAMILY family, MIB_UNICASTIPADDRESS_TABLE **table)
2638 IP_ADAPTER_ADDRESSES *aa, *ptr;
2639 MIB_UNICASTIPADDRESS_TABLE *data;
2640 DWORD ret, count = 0;
2641 ULONG size, flags;
2643 TRACE("%u, %p\n", family, table);
2645 if (!table || (family != WS_AF_INET && family != WS_AF_INET6 && family != WS_AF_UNSPEC))
2646 return ERROR_INVALID_PARAMETER;
2648 flags = GAA_FLAG_SKIP_ANYCAST |
2649 GAA_FLAG_SKIP_MULTICAST |
2650 GAA_FLAG_SKIP_DNS_SERVER |
2651 GAA_FLAG_SKIP_FRIENDLY_NAME;
2653 ret = GetAdaptersAddresses(family, flags, NULL, NULL, &size);
2654 if (ret != ERROR_BUFFER_OVERFLOW)
2655 return ret;
2656 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, size)))
2657 return ERROR_OUTOFMEMORY;
2658 if ((ret = GetAdaptersAddresses(family, flags, NULL, ptr, &size)))
2660 HeapFree(GetProcessHeap(), 0, ptr);
2661 return ret;
2664 for (aa = ptr; aa; aa = aa->Next)
2666 IP_ADAPTER_UNICAST_ADDRESS *ua = aa->FirstUnicastAddress;
2667 while (ua)
2669 count++;
2670 ua = ua->Next;
2674 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) + (count - 1) * sizeof(data->Table[0]))))
2676 HeapFree(GetProcessHeap(), 0, ptr);
2677 return ERROR_OUTOFMEMORY;
2680 data->NumEntries = 0;
2681 for (aa = ptr; aa; aa = aa->Next)
2683 IP_ADAPTER_UNICAST_ADDRESS *ua = aa->FirstUnicastAddress;
2684 while (ua)
2686 MIB_UNICASTIPADDRESS_ROW *row = &data->Table[data->NumEntries];
2687 memcpy(&row->Address, ua->Address.lpSockaddr, ua->Address.iSockaddrLength);
2688 memcpy(&row->InterfaceLuid, &aa->Luid, sizeof(aa->Luid));
2689 row->InterfaceIndex = aa->u.s.IfIndex;
2690 row->PrefixOrigin = ua->PrefixOrigin;
2691 row->SuffixOrigin = ua->SuffixOrigin;
2692 row->ValidLifetime = ua->ValidLifetime;
2693 row->PreferredLifetime = ua->PreferredLifetime;
2694 row->OnLinkPrefixLength = ua->OnLinkPrefixLength;
2695 row->SkipAsSource = 0;
2696 row->DadState = ua->DadState;
2697 if (row->Address.si_family == WS_AF_INET6)
2698 row->ScopeId.u.Value = row->Address.Ipv6.sin6_scope_id;
2699 else
2700 row->ScopeId.u.Value = 0;
2701 NtQuerySystemTime(&row->CreationTimeStamp);
2703 data->NumEntries++;
2704 ua = ua->Next;
2708 HeapFree(GetProcessHeap(), 0, ptr);
2710 *table = data;
2711 return ret;
2714 /******************************************************************
2715 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2717 * This is a Win98-only function to get information on "unidirectional"
2718 * adapters. Since this is pretty nonsensical in other contexts, it
2719 * never returns anything.
2721 * PARAMS
2722 * pIPIfInfo [Out] buffer for adapter infos
2723 * dwOutBufLen [Out] length of the output buffer
2725 * RETURNS
2726 * Success: NO_ERROR
2727 * Failure: error code from winerror.h
2729 * FIXME
2730 * Stub, returns ERROR_NOT_SUPPORTED.
2732 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
2734 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
2735 /* a unidirectional adapter?? not bloody likely! */
2736 return ERROR_NOT_SUPPORTED;
2740 /******************************************************************
2741 * IpReleaseAddress (IPHLPAPI.@)
2743 * Release an IP obtained through DHCP,
2745 * PARAMS
2746 * AdapterInfo [In] adapter to release IP address
2748 * RETURNS
2749 * Success: NO_ERROR
2750 * Failure: error code from winerror.h
2752 * NOTES
2753 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2754 * this function does nothing.
2756 * FIXME
2757 * Stub, returns ERROR_NOT_SUPPORTED.
2759 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2761 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2762 return ERROR_NOT_SUPPORTED;
2766 /******************************************************************
2767 * IpRenewAddress (IPHLPAPI.@)
2769 * Renew an IP obtained through DHCP.
2771 * PARAMS
2772 * AdapterInfo [In] adapter to renew IP address
2774 * RETURNS
2775 * Success: NO_ERROR
2776 * Failure: error code from winerror.h
2778 * NOTES
2779 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2780 * this function does nothing.
2782 * FIXME
2783 * Stub, returns ERROR_NOT_SUPPORTED.
2785 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2787 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2788 return ERROR_NOT_SUPPORTED;
2792 /******************************************************************
2793 * NotifyAddrChange (IPHLPAPI.@)
2795 * Notify caller whenever the ip-interface map is changed.
2797 * PARAMS
2798 * Handle [Out] handle usable in asynchronous notification
2799 * overlapped [In] overlapped structure that notifies the caller
2801 * RETURNS
2802 * Success: NO_ERROR
2803 * Failure: error code from winerror.h
2805 * FIXME
2806 * Stub, returns ERROR_NOT_SUPPORTED.
2808 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2810 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2811 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2812 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2813 return ERROR_IO_PENDING;
2817 /******************************************************************
2818 * NotifyIpInterfaceChange (IPHLPAPI.@)
2820 DWORD WINAPI NotifyIpInterfaceChange(ADDRESS_FAMILY family, PIPINTERFACE_CHANGE_CALLBACK callback,
2821 PVOID context, BOOLEAN init_notify, PHANDLE handle)
2823 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2824 family, callback, context, init_notify, handle);
2825 if (handle) *handle = NULL;
2826 return NO_ERROR;
2829 /******************************************************************
2830 * NotifyRouteChange2 (IPHLPAPI.@)
2832 DWORD WINAPI NotifyRouteChange2(ADDRESS_FAMILY family, PIPFORWARD_CHANGE_CALLBACK callback, VOID* context,
2833 BOOLEAN init_notify, HANDLE* handle)
2835 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2836 family, callback, context, init_notify, handle);
2837 if (handle) *handle = NULL;
2838 return NO_ERROR;
2842 /******************************************************************
2843 * NotifyRouteChange (IPHLPAPI.@)
2845 * Notify caller whenever the ip routing table is changed.
2847 * PARAMS
2848 * Handle [Out] handle usable in asynchronous notification
2849 * overlapped [In] overlapped structure that notifies the caller
2851 * RETURNS
2852 * Success: NO_ERROR
2853 * Failure: error code from winerror.h
2855 * FIXME
2856 * Stub, returns ERROR_NOT_SUPPORTED.
2858 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2860 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2861 return ERROR_NOT_SUPPORTED;
2865 /******************************************************************
2866 * NotifyUnicastIpAddressChange (IPHLPAPI.@)
2868 DWORD WINAPI NotifyUnicastIpAddressChange(ADDRESS_FAMILY family, PUNICAST_IPADDRESS_CHANGE_CALLBACK callback,
2869 PVOID context, BOOLEAN init_notify, PHANDLE handle)
2871 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): semi-stub\n",
2872 family, callback, context, init_notify, handle);
2873 if (handle) *handle = NULL;
2875 if (init_notify)
2876 callback(context, NULL, MibInitialNotification);
2878 return NO_ERROR;
2881 /******************************************************************
2882 * SendARP (IPHLPAPI.@)
2884 * Send an ARP request.
2886 * PARAMS
2887 * DestIP [In] attempt to obtain this IP
2888 * SrcIP [In] optional sender IP address
2889 * pMacAddr [Out] buffer for the mac address
2890 * PhyAddrLen [In/Out] length of the output buffer
2892 * RETURNS
2893 * Success: NO_ERROR
2894 * Failure: error code from winerror.h
2896 * FIXME
2897 * Stub, returns ERROR_NOT_SUPPORTED.
2899 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2901 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2902 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2903 return ERROR_NOT_SUPPORTED;
2907 /******************************************************************
2908 * SetIfEntry (IPHLPAPI.@)
2910 * Set the administrative status of an interface.
2912 * PARAMS
2913 * pIfRow [In] dwAdminStatus member specifies the new status.
2915 * RETURNS
2916 * Success: NO_ERROR
2917 * Failure: error code from winerror.h
2919 * FIXME
2920 * Stub, returns ERROR_NOT_SUPPORTED.
2922 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2924 FIXME("(pIfRow %p): stub\n", pIfRow);
2925 /* this is supposed to set an interface administratively up or down.
2926 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2927 this sort of down is indistinguishable from other sorts of down (e.g. no
2928 link). */
2929 return ERROR_NOT_SUPPORTED;
2933 /******************************************************************
2934 * SetIpForwardEntry (IPHLPAPI.@)
2936 * Modify an existing route.
2938 * PARAMS
2939 * pRoute [In] route with the new information
2941 * RETURNS
2942 * Success: NO_ERROR
2943 * Failure: error code from winerror.h
2945 * FIXME
2946 * Stub, returns NO_ERROR.
2948 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2950 FIXME("(pRoute %p): stub\n", pRoute);
2951 /* this is to add a route entry, how's it distinguishable from
2952 CreateIpForwardEntry?
2953 could use SIOCADDRT, not sure I want to */
2954 return 0;
2958 /******************************************************************
2959 * SetIpNetEntry (IPHLPAPI.@)
2961 * Modify an existing ARP entry.
2963 * PARAMS
2964 * pArpEntry [In] ARP entry with the new information
2966 * RETURNS
2967 * Success: NO_ERROR
2968 * Failure: error code from winerror.h
2970 * FIXME
2971 * Stub, returns NO_ERROR.
2973 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2975 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2976 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2977 return 0;
2981 /******************************************************************
2982 * SetIpStatistics (IPHLPAPI.@)
2984 * Toggle IP forwarding and det the default TTL value.
2986 * PARAMS
2987 * pIpStats [In] IP statistics with the new information
2989 * RETURNS
2990 * Success: NO_ERROR
2991 * Failure: error code from winerror.h
2993 * FIXME
2994 * Stub, returns NO_ERROR.
2996 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2998 FIXME("(pIpStats %p): stub\n", pIpStats);
2999 return 0;
3003 /******************************************************************
3004 * SetIpTTL (IPHLPAPI.@)
3006 * Set the default TTL value.
3008 * PARAMS
3009 * nTTL [In] new TTL value
3011 * RETURNS
3012 * Success: NO_ERROR
3013 * Failure: error code from winerror.h
3015 * FIXME
3016 * Stub, returns NO_ERROR.
3018 DWORD WINAPI SetIpTTL(UINT nTTL)
3020 FIXME("(nTTL %d): stub\n", nTTL);
3021 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
3022 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
3023 return 0;
3027 /******************************************************************
3028 * SetTcpEntry (IPHLPAPI.@)
3030 * Set the state of a TCP connection.
3032 * PARAMS
3033 * pTcpRow [In] specifies connection with new state
3035 * RETURNS
3036 * Success: NO_ERROR
3037 * Failure: error code from winerror.h
3039 * FIXME
3040 * Stub, returns NO_ERROR.
3042 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
3044 FIXME("(pTcpRow %p): stub\n", pTcpRow);
3045 return 0;
3048 /******************************************************************
3049 * SetPerTcpConnectionEStats (IPHLPAPI.@)
3051 DWORD WINAPI SetPerTcpConnectionEStats(PMIB_TCPROW row, TCP_ESTATS_TYPE state, PBYTE rw,
3052 ULONG version, ULONG size, ULONG offset)
3054 FIXME("(row %p, state %d, rw %p, version %u, size %u, offset %u): stub\n",
3055 row, state, rw, version, size, offset);
3056 return ERROR_NOT_SUPPORTED;
3060 /******************************************************************
3061 * UnenableRouter (IPHLPAPI.@)
3063 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
3064 * if it reaches zero.
3066 * PARAMS
3067 * pOverlapped [In/Out] should be the same as in EnableRouter()
3068 * lpdwEnableCount [Out] optional, receives reference count
3070 * RETURNS
3071 * Success: NO_ERROR
3072 * Failure: error code from winerror.h
3074 * FIXME
3075 * Stub, returns ERROR_NOT_SUPPORTED.
3077 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
3079 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
3080 lpdwEnableCount);
3081 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
3082 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
3084 return ERROR_NOT_SUPPORTED;
3087 /******************************************************************
3088 * PfCreateInterface (IPHLPAPI.@)
3090 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
3091 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
3093 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
3094 return ERROR_CALL_NOT_IMPLEMENTED;
3097 /******************************************************************
3098 * PfUnBindInterface (IPHLPAPI.@)
3100 DWORD WINAPI PfUnBindInterface(INTERFACE_HANDLE interface)
3102 FIXME("(%p) stub\n", interface);
3103 return ERROR_CALL_NOT_IMPLEMENTED;
3106 /******************************************************************
3107 * PfDeleteInterface(IPHLPAPI.@)
3109 DWORD WINAPI PfDeleteInterface(INTERFACE_HANDLE interface)
3111 FIXME("(%p) stub\n", interface);
3112 return ERROR_CALL_NOT_IMPLEMENTED;
3115 /******************************************************************
3116 * PfBindInterfaceToIPAddress(IPHLPAPI.@)
3118 DWORD WINAPI PfBindInterfaceToIPAddress(INTERFACE_HANDLE interface, PFADDRESSTYPE type, PBYTE ip)
3120 FIXME("(%p %d %p) stub\n", interface, type, ip);
3121 return ERROR_CALL_NOT_IMPLEMENTED;
3124 /******************************************************************
3125 * GetTcpTable2 (IPHLPAPI.@)
3127 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
3129 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
3130 return ERROR_NOT_SUPPORTED;
3133 /******************************************************************
3134 * GetTcp6Table (IPHLPAPI.@)
3136 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
3138 TRACE("(table %p, size %p, order %d)\n", table, size, order);
3139 return GetExtendedTcpTable(table, size, order, WS_AF_INET6, TCP_TABLE_BASIC_ALL, 0);
3142 /******************************************************************
3143 * GetTcp6Table2 (IPHLPAPI.@)
3145 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
3147 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
3148 return ERROR_NOT_SUPPORTED;
3151 /******************************************************************
3152 * ConvertInterfaceGuidToLuid (IPHLPAPI.@)
3154 DWORD WINAPI ConvertInterfaceGuidToLuid(const GUID *guid, NET_LUID *luid)
3156 DWORD ret;
3157 MIB_IFROW row;
3159 TRACE("(%s %p)\n", debugstr_guid(guid), luid);
3161 if (!guid || !luid) return ERROR_INVALID_PARAMETER;
3163 row.dwIndex = guid->Data1;
3164 if ((ret = GetIfEntry( &row ))) return ret;
3166 luid->Info.Reserved = 0;
3167 luid->Info.NetLuidIndex = guid->Data1;
3168 luid->Info.IfType = row.dwType;
3169 return NO_ERROR;
3172 /******************************************************************
3173 * ConvertInterfaceIndexToLuid (IPHLPAPI.@)
3175 DWORD WINAPI ConvertInterfaceIndexToLuid(NET_IFINDEX index, NET_LUID *luid)
3177 MIB_IFROW row;
3179 TRACE("(%u %p)\n", index, luid);
3181 if (!luid) return ERROR_INVALID_PARAMETER;
3182 memset( luid, 0, sizeof(*luid) );
3184 row.dwIndex = index;
3185 if (GetIfEntry( &row )) return ERROR_FILE_NOT_FOUND;
3187 luid->Info.Reserved = 0;
3188 luid->Info.NetLuidIndex = index;
3189 luid->Info.IfType = row.dwType;
3190 return NO_ERROR;
3193 /******************************************************************
3194 * ConvertInterfaceLuidToGuid (IPHLPAPI.@)
3196 DWORD WINAPI ConvertInterfaceLuidToGuid(const NET_LUID *luid, GUID *guid)
3198 DWORD ret;
3199 MIB_IFROW row;
3201 TRACE("(%p %p)\n", luid, guid);
3203 if (!luid || !guid) return ERROR_INVALID_PARAMETER;
3205 row.dwIndex = luid->Info.NetLuidIndex;
3206 if ((ret = GetIfEntry( &row ))) return ret;
3208 memset( guid, 0, sizeof(*guid) );
3209 guid->Data1 = luid->Info.NetLuidIndex;
3210 memcpy( guid->Data4+2, "NetDev", 6 );
3211 return NO_ERROR;
3214 /******************************************************************
3215 * ConvertInterfaceLuidToIndex (IPHLPAPI.@)
3217 DWORD WINAPI ConvertInterfaceLuidToIndex(const NET_LUID *luid, NET_IFINDEX *index)
3219 DWORD ret;
3220 MIB_IFROW row;
3222 TRACE("(%p %p)\n", luid, index);
3224 if (!luid || !index) return ERROR_INVALID_PARAMETER;
3226 row.dwIndex = luid->Info.NetLuidIndex;
3227 if ((ret = GetIfEntry( &row ))) return ret;
3229 *index = luid->Info.NetLuidIndex;
3230 return NO_ERROR;
3233 /******************************************************************
3234 * ConvertInterfaceLuidToNameA (IPHLPAPI.@)
3236 DWORD WINAPI ConvertInterfaceLuidToNameA(const NET_LUID *luid, char *name, SIZE_T len)
3238 DWORD ret;
3239 MIB_IFROW row;
3241 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
3243 if (!luid) return ERROR_INVALID_PARAMETER;
3245 row.dwIndex = luid->Info.NetLuidIndex;
3246 if ((ret = GetIfEntry( &row ))) return ret;
3248 if (!name || len < WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, NULL, 0, NULL, NULL ))
3249 return ERROR_NOT_ENOUGH_MEMORY;
3251 WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, name, len, NULL, NULL );
3252 return NO_ERROR;
3255 /******************************************************************
3256 * ConvertInterfaceLuidToNameW (IPHLPAPI.@)
3258 DWORD WINAPI ConvertInterfaceLuidToNameW(const NET_LUID *luid, WCHAR *name, SIZE_T len)
3260 DWORD ret;
3261 MIB_IFROW row;
3263 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
3265 if (!luid || !name) return ERROR_INVALID_PARAMETER;
3267 row.dwIndex = luid->Info.NetLuidIndex;
3268 if ((ret = GetIfEntry( &row ))) return ret;
3270 if (len < strlenW( row.wszName ) + 1) return ERROR_NOT_ENOUGH_MEMORY;
3271 strcpyW( name, row.wszName );
3272 return NO_ERROR;
3275 /******************************************************************
3276 * ConvertInterfaceNameToLuidA (IPHLPAPI.@)
3278 DWORD WINAPI ConvertInterfaceNameToLuidA(const char *name, NET_LUID *luid)
3280 DWORD ret;
3281 IF_INDEX index;
3282 MIB_IFROW row;
3284 TRACE("(%s %p)\n", debugstr_a(name), luid);
3286 if ((ret = getInterfaceIndexByName( name, &index ))) return ERROR_INVALID_NAME;
3287 if (!luid) return ERROR_INVALID_PARAMETER;
3289 row.dwIndex = index;
3290 if ((ret = GetIfEntry( &row ))) return ret;
3292 luid->Info.Reserved = 0;
3293 luid->Info.NetLuidIndex = index;
3294 luid->Info.IfType = row.dwType;
3295 return NO_ERROR;
3298 /******************************************************************
3299 * ConvertInterfaceNameToLuidW (IPHLPAPI.@)
3301 DWORD WINAPI ConvertInterfaceNameToLuidW(const WCHAR *name, NET_LUID *luid)
3303 DWORD ret;
3304 IF_INDEX index;
3305 MIB_IFROW row;
3306 char nameA[IF_MAX_STRING_SIZE + 1];
3308 TRACE("(%s %p)\n", debugstr_w(name), luid);
3310 if (!luid) return ERROR_INVALID_PARAMETER;
3311 memset( luid, 0, sizeof(*luid) );
3313 if (!WideCharToMultiByte( CP_UNIXCP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL ))
3314 return ERROR_INVALID_NAME;
3316 if ((ret = getInterfaceIndexByName( nameA, &index ))) return ret;
3318 row.dwIndex = index;
3319 if ((ret = GetIfEntry( &row ))) return ret;
3321 luid->Info.Reserved = 0;
3322 luid->Info.NetLuidIndex = index;
3323 luid->Info.IfType = row.dwType;
3324 return NO_ERROR;
3327 /******************************************************************
3328 * ConvertLengthToIpv4Mask (IPHLPAPI.@)
3330 DWORD WINAPI ConvertLengthToIpv4Mask(ULONG mask_len, ULONG *mask)
3332 if (mask_len > 32)
3334 *mask = INADDR_NONE;
3335 return ERROR_INVALID_PARAMETER;
3338 if (mask_len == 0)
3339 *mask = 0;
3340 else
3341 *mask = htonl(~0u << (32 - mask_len));
3343 return NO_ERROR;
3346 /******************************************************************
3347 * if_nametoindex (IPHLPAPI.@)
3349 IF_INDEX WINAPI IPHLP_if_nametoindex(const char *name)
3351 IF_INDEX idx;
3353 TRACE("(%s)\n", name);
3354 if (getInterfaceIndexByName(name, &idx) == NO_ERROR)
3355 return idx;
3357 return 0;
3360 /******************************************************************
3361 * if_indextoname (IPHLPAPI.@)
3363 PCHAR WINAPI IPHLP_if_indextoname(NET_IFINDEX index, PCHAR name)
3365 TRACE("(%u, %p)\n", index, name);
3367 return getInterfaceNameByIndex(index, name);
3370 /******************************************************************
3371 * GetIpForwardTable2 (IPHLPAPI.@)
3373 DWORD WINAPI GetIpForwardTable2(ADDRESS_FAMILY family, PMIB_IPFORWARD_TABLE2 *table)
3375 static int once;
3377 if (!once++) FIXME("(%u %p): stub\n", family, table);
3378 return ERROR_NOT_SUPPORTED;
3381 /******************************************************************
3382 * GetIpNetTable2 (IPHLPAPI.@)
3384 DWORD WINAPI GetIpNetTable2(ADDRESS_FAMILY family, PMIB_IPNET_TABLE2 *table)
3386 static int once;
3388 if (!once++) FIXME("(%u %p): stub\n", family, table);
3389 return ERROR_NOT_SUPPORTED;
3392 /******************************************************************
3393 * GetIpInterfaceTable (IPHLPAPI.@)
3395 DWORD WINAPI GetIpInterfaceTable(ADDRESS_FAMILY family, PMIB_IPINTERFACE_TABLE *table)
3397 FIXME("(%u %p): stub\n", family, table);
3398 return ERROR_NOT_SUPPORTED;
3401 /******************************************************************
3402 * GetBestRoute2 (IPHLPAPI.@)
3404 DWORD WINAPI GetBestRoute2(NET_LUID *luid, NET_IFINDEX index,
3405 const SOCKADDR_INET *source, const SOCKADDR_INET *destination,
3406 ULONG options, PMIB_IPFORWARD_ROW2 bestroute,
3407 SOCKADDR_INET *bestaddress)
3409 static int once;
3411 if (!once++)
3412 FIXME("(%p, %d, %p, %p, 0x%08x, %p, %p): stub\n", luid, index, source,
3413 destination, options, bestroute, bestaddress);
3415 if (!destination || !bestroute || !bestaddress)
3416 return ERROR_INVALID_PARAMETER;
3418 return ERROR_NOT_SUPPORTED;
3421 /******************************************************************
3422 * ParseNetworkString (IPHLPAPI.@)
3424 DWORD WINAPI ParseNetworkString(const WCHAR *str, DWORD type,
3425 NET_ADDRESS_INFO *info, USHORT *port, BYTE *prefix_len)
3427 IN_ADDR temp_addr4;
3428 IN6_ADDR temp_addr6;
3429 ULONG temp_scope;
3430 USHORT temp_port = 0;
3431 NTSTATUS status;
3433 TRACE("(%s, %d, %p, %p, %p)\n", debugstr_w(str), type, info, port, prefix_len);
3435 if (!str)
3436 return ERROR_INVALID_PARAMETER;
3438 if (type & NET_STRING_IPV4_ADDRESS)
3440 status = RtlIpv4StringToAddressExW(str, TRUE, &temp_addr4, &temp_port);
3441 if (SUCCEEDED(status) && !temp_port)
3443 if (info)
3445 info->Format = NET_ADDRESS_IPV4;
3446 info->u.Ipv4Address.sin_addr = temp_addr4;
3447 info->u.Ipv4Address.sin_port = 0;
3449 if (port) *port = 0;
3450 if (prefix_len) *prefix_len = 255;
3451 return ERROR_SUCCESS;
3454 if (type & NET_STRING_IPV4_SERVICE)
3456 status = RtlIpv4StringToAddressExW(str, TRUE, &temp_addr4, &temp_port);
3457 if (SUCCEEDED(status) && temp_port)
3459 if (info)
3461 info->Format = NET_ADDRESS_IPV4;
3462 info->u.Ipv4Address.sin_addr = temp_addr4;
3463 info->u.Ipv4Address.sin_port = temp_port;
3465 if (port) *port = ntohs(temp_port);
3466 if (prefix_len) *prefix_len = 255;
3467 return ERROR_SUCCESS;
3470 if (type & NET_STRING_IPV6_ADDRESS)
3472 status = RtlIpv6StringToAddressExW(str, &temp_addr6, &temp_scope, &temp_port);
3473 if (SUCCEEDED(status) && !temp_port)
3475 if (info)
3477 info->Format = NET_ADDRESS_IPV6;
3478 info->u.Ipv6Address.sin6_addr = temp_addr6;
3479 info->u.Ipv6Address.sin6_scope_id = temp_scope;
3480 info->u.Ipv6Address.sin6_port = 0;
3482 if (port) *port = 0;
3483 if (prefix_len) *prefix_len = 255;
3484 return ERROR_SUCCESS;
3487 if (type & NET_STRING_IPV6_SERVICE)
3489 status = RtlIpv6StringToAddressExW(str, &temp_addr6, &temp_scope, &temp_port);
3490 if (SUCCEEDED(status) && temp_port)
3492 if (info)
3494 info->Format = NET_ADDRESS_IPV6;
3495 info->u.Ipv6Address.sin6_addr = temp_addr6;
3496 info->u.Ipv6Address.sin6_scope_id = temp_scope;
3497 info->u.Ipv6Address.sin6_port = temp_port;
3499 if (port) *port = ntohs(temp_port);
3500 if (prefix_len) *prefix_len = 255;
3501 return ERROR_SUCCESS;
3505 if (info) info->Format = NET_ADDRESS_FORMAT_UNSPECIFIED;
3507 if (type & ~(NET_STRING_IPV4_ADDRESS|NET_STRING_IPV4_SERVICE|NET_STRING_IPV6_ADDRESS|NET_STRING_IPV6_SERVICE))
3509 FIXME("Unimplemented type 0x%x\n", type);
3510 return ERROR_NOT_SUPPORTED;
3513 return ERROR_INVALID_PARAMETER;