iphlpapi: Flesh out NotifyIpInterfaceChange arguments.
[wine.git] / dlls / iphlpapi / iphlpapi_main.c
blob953af886db887f390ff6499e7c8d0f9bc8825501
1 /*
2 * iphlpapi dll implementation
4 * Copyright (C) 2003,2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
32 #endif
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
35 #endif
36 #ifdef HAVE_RESOLV_H
37 # include <resolv.h>
38 #endif
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winreg.h"
45 #define USE_WS_PREFIX
46 #include "winsock2.h"
47 #include "winternl.h"
48 #include "ws2ipdef.h"
49 #include "iphlpapi.h"
50 #include "ifenum.h"
51 #include "ipstats.h"
52 #include "ipifcons.h"
53 #include "fltdefs.h"
54 #include "ifdef.h"
55 #include "netioapi.h"
56 #include "tcpestats.h"
58 #include "wine/debug.h"
59 #include "wine/unicode.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
63 #ifndef IF_NAMESIZE
64 #define IF_NAMESIZE 16
65 #endif
67 #ifndef INADDR_NONE
68 #define INADDR_NONE ~0UL
69 #endif
71 /******************************************************************
72 * AddIPAddress (IPHLPAPI.@)
74 * Add an IP address to an adapter.
76 * PARAMS
77 * Address [In] IP address to add to the adapter
78 * IpMask [In] subnet mask for the IP address
79 * IfIndex [In] adapter index to add the address
80 * NTEContext [Out] Net Table Entry (NTE) context for the IP address
81 * NTEInstance [Out] NTE instance for the IP address
83 * RETURNS
84 * Success: NO_ERROR
85 * Failure: error code from winerror.h
87 * FIXME
88 * Stub. Currently returns ERROR_NOT_SUPPORTED.
90 DWORD WINAPI AddIPAddress(IPAddr Address, IPMask IpMask, DWORD IfIndex, PULONG NTEContext, PULONG NTEInstance)
92 FIXME(":stub\n");
93 return ERROR_NOT_SUPPORTED;
97 /******************************************************************
98 * AllocateAndGetIfTableFromStack (IPHLPAPI.@)
100 * Get table of local interfaces.
101 * Like GetIfTable(), but allocate the returned table from heap.
103 * PARAMS
104 * ppIfTable [Out] pointer into which the MIB_IFTABLE is
105 * allocated and returned.
106 * bOrder [In] whether to sort the table
107 * heap [In] heap from which the table is allocated
108 * flags [In] flags to HeapAlloc
110 * RETURNS
111 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, whatever
112 * GetIfTable() returns otherwise.
114 DWORD WINAPI AllocateAndGetIfTableFromStack(PMIB_IFTABLE *ppIfTable,
115 BOOL bOrder, HANDLE heap, DWORD flags)
117 DWORD ret;
119 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable,
120 bOrder, heap, flags);
121 if (!ppIfTable)
122 ret = ERROR_INVALID_PARAMETER;
123 else {
124 DWORD dwSize = 0;
126 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
127 if (ret == ERROR_INSUFFICIENT_BUFFER) {
128 *ppIfTable = HeapAlloc(heap, flags, dwSize);
129 ret = GetIfTable(*ppIfTable, &dwSize, bOrder);
132 TRACE("returning %d\n", ret);
133 return ret;
137 static int IpAddrTableNumericSorter(const void *a, const void *b)
139 int ret = 0;
141 if (a && b)
142 ret = ((const MIB_IPADDRROW*)a)->dwAddr - ((const MIB_IPADDRROW*)b)->dwAddr;
143 return ret;
146 static int IpAddrTableLoopbackSorter(const void *a, const void *b)
148 const MIB_IPADDRROW *left = a, *right = b;
149 int ret = 0;
151 if (isIfIndexLoopback(left->dwIndex))
152 ret = 1;
153 else if (isIfIndexLoopback(right->dwIndex))
154 ret = -1;
156 return ret;
159 /******************************************************************
160 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
162 * Get interface-to-IP address mapping table.
163 * Like GetIpAddrTable(), but allocate the returned table from heap.
165 * PARAMS
166 * ppIpAddrTable [Out] pointer into which the MIB_IPADDRTABLE is
167 * allocated and returned.
168 * bOrder [In] whether to sort the table
169 * heap [In] heap from which the table is allocated
170 * flags [In] flags to HeapAlloc
172 * RETURNS
173 * ERROR_INVALID_PARAMETER if ppIpAddrTable is NULL, other error codes on
174 * failure, NO_ERROR on success.
176 DWORD WINAPI AllocateAndGetIpAddrTableFromStack(PMIB_IPADDRTABLE *ppIpAddrTable,
177 BOOL bOrder, HANDLE heap, DWORD flags)
179 DWORD ret;
181 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
182 ppIpAddrTable, bOrder, heap, flags);
183 ret = getIPAddrTable(ppIpAddrTable, heap, flags);
184 if (!ret && bOrder)
185 qsort((*ppIpAddrTable)->table, (*ppIpAddrTable)->dwNumEntries,
186 sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
187 TRACE("returning %d\n", ret);
188 return ret;
192 /******************************************************************
193 * CancelIPChangeNotify (IPHLPAPI.@)
195 * Cancel a previous notification created by NotifyAddrChange or
196 * NotifyRouteChange.
198 * PARAMS
199 * overlapped [In] overlapped structure that notifies the caller
201 * RETURNS
202 * Success: TRUE
203 * Failure: FALSE
205 * FIXME
206 * Stub, returns FALSE.
208 BOOL WINAPI CancelIPChangeNotify(LPOVERLAPPED overlapped)
210 FIXME("(overlapped %p): stub\n", overlapped);
211 return FALSE;
215 /******************************************************************
216 * CancelMibChangeNotify2 (IPHLPAPI.@)
218 DWORD WINAPI CancelMibChangeNotify2(HANDLE handle)
220 FIXME("(handle %p): stub\n", handle);
221 return NO_ERROR;
225 /******************************************************************
226 * CreateIpForwardEntry (IPHLPAPI.@)
228 * Create a route in the local computer's IP table.
230 * PARAMS
231 * pRoute [In] new route information
233 * RETURNS
234 * Success: NO_ERROR
235 * Failure: error code from winerror.h
237 * FIXME
238 * Stub, always returns NO_ERROR.
240 DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
242 FIXME("(pRoute %p): stub\n", pRoute);
243 /* could use SIOCADDRT, not sure I want to */
244 return 0;
248 /******************************************************************
249 * CreateIpNetEntry (IPHLPAPI.@)
251 * Create entry in the ARP table.
253 * PARAMS
254 * pArpEntry [In] new ARP entry
256 * RETURNS
257 * Success: NO_ERROR
258 * Failure: error code from winerror.h
260 * FIXME
261 * Stub, always returns NO_ERROR.
263 DWORD WINAPI CreateIpNetEntry(PMIB_IPNETROW pArpEntry)
265 FIXME("(pArpEntry %p)\n", pArpEntry);
266 /* could use SIOCSARP on systems that support it, not sure I want to */
267 return 0;
271 /******************************************************************
272 * CreateProxyArpEntry (IPHLPAPI.@)
274 * Create a Proxy ARP (PARP) entry for an IP address.
276 * PARAMS
277 * dwAddress [In] IP address for which this computer acts as a proxy.
278 * dwMask [In] subnet mask for dwAddress
279 * dwIfIndex [In] interface index
281 * RETURNS
282 * Success: NO_ERROR
283 * Failure: error code from winerror.h
285 * FIXME
286 * Stub, returns ERROR_NOT_SUPPORTED.
288 DWORD WINAPI CreateProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
290 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
291 dwAddress, dwMask, dwIfIndex);
292 return ERROR_NOT_SUPPORTED;
295 static char *debugstr_ipv6(const struct WS_sockaddr_in6 *sin, char *buf)
297 const IN6_ADDR *addr = &sin->sin6_addr;
298 char *p = buf;
299 int i;
300 BOOL in_zero = FALSE;
302 for (i = 0; i < 7; i++)
304 if (!addr->u.Word[i])
306 if (i == 0)
307 *p++ = ':';
308 if (!in_zero)
310 *p++ = ':';
311 in_zero = TRUE;
314 else
316 p += sprintf(p, "%x:", ntohs(addr->u.Word[i]));
317 in_zero = FALSE;
320 sprintf(p, "%x", ntohs(addr->u.Word[7]));
321 return buf;
324 static BOOL map_address_6to4( const SOCKADDR_IN6 *addr6, SOCKADDR_IN *addr4 )
326 ULONG i;
328 if (addr6->sin6_family != WS_AF_INET6) return FALSE;
330 for (i = 0; i < 5; i++)
331 if (addr6->sin6_addr.u.Word[i]) return FALSE;
333 if (addr6->sin6_addr.u.Word[5] != 0xffff) return FALSE;
335 addr4->sin_family = WS_AF_INET;
336 addr4->sin_port = addr6->sin6_port;
337 addr4->sin_addr.S_un.S_addr = addr6->sin6_addr.u.Word[6] << 16 | addr6->sin6_addr.u.Word[7];
338 memset( &addr4->sin_zero, 0, sizeof(addr4->sin_zero) );
340 return TRUE;
343 static BOOL find_src_address( MIB_IPADDRTABLE *table, const SOCKADDR_IN *dst, SOCKADDR_IN6 *src )
345 MIB_IPFORWARDROW row;
346 DWORD i, j;
348 if (GetBestRoute( dst->sin_addr.S_un.S_addr, 0, &row )) return FALSE;
350 for (i = 0; i < table->dwNumEntries; i++)
352 /* take the first address */
353 if (table->table[i].dwIndex == row.dwForwardIfIndex)
355 src->sin6_family = WS_AF_INET6;
356 src->sin6_port = 0;
357 src->sin6_flowinfo = 0;
358 for (j = 0; j < 5; j++) src->sin6_addr.u.Word[j] = 0;
359 src->sin6_addr.u.Word[5] = 0xffff;
360 src->sin6_addr.u.Word[6] = table->table[i].dwAddr & 0xffff;
361 src->sin6_addr.u.Word[7] = table->table[i].dwAddr >> 16;
362 return TRUE;
366 return FALSE;
369 /******************************************************************
370 * CreateSortedAddressPairs (IPHLPAPI.@)
372 DWORD WINAPI CreateSortedAddressPairs( const PSOCKADDR_IN6 src_list, DWORD src_count,
373 const PSOCKADDR_IN6 dst_list, DWORD dst_count,
374 DWORD options, PSOCKADDR_IN6_PAIR *pair_list,
375 DWORD *pair_count )
377 DWORD i, size, ret;
378 SOCKADDR_IN6_PAIR *pairs;
379 SOCKADDR_IN6 *ptr;
380 SOCKADDR_IN addr4;
381 MIB_IPADDRTABLE *table;
383 FIXME( "(src_list %p src_count %u dst_list %p dst_count %u options %x pair_list %p pair_count %p): stub\n",
384 src_list, src_count, dst_list, dst_count, options, pair_list, pair_count );
386 if (src_list || src_count || !dst_list || !pair_list || !pair_count || dst_count > 500)
387 return ERROR_INVALID_PARAMETER;
389 for (i = 0; i < dst_count; i++)
391 if (!map_address_6to4( &dst_list[i], &addr4 ))
393 FIXME("only mapped IPv4 addresses are supported\n");
394 return ERROR_NOT_SUPPORTED;
398 size = dst_count * sizeof(*pairs);
399 size += dst_count * sizeof(SOCKADDR_IN6) * 2; /* source address + destination address */
400 if (!(pairs = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_NOT_ENOUGH_MEMORY;
401 ptr = (SOCKADDR_IN6 *)&pairs[dst_count];
403 if ((ret = getIPAddrTable( &table, GetProcessHeap(), 0 )))
405 HeapFree( GetProcessHeap(), 0, pairs );
406 return ret;
409 for (i = 0; i < dst_count; i++)
411 pairs[i].SourceAddress = ptr++;
412 if (!map_address_6to4( &dst_list[i], &addr4 ) ||
413 !find_src_address( table, &addr4, pairs[i].SourceAddress ))
415 char buf[46];
416 FIXME( "source address for %s not found\n", debugstr_ipv6(&dst_list[i], buf) );
417 memset( pairs[i].SourceAddress, 0, sizeof(*pairs[i].SourceAddress) );
418 pairs[i].SourceAddress->sin6_family = WS_AF_INET6;
421 pairs[i].DestinationAddress = ptr++;
422 memcpy( pairs[i].DestinationAddress, &dst_list[i], sizeof(*pairs[i].DestinationAddress) );
424 *pair_list = pairs;
425 *pair_count = dst_count;
427 HeapFree( GetProcessHeap(), 0, table );
428 return NO_ERROR;
432 /******************************************************************
433 * DeleteIPAddress (IPHLPAPI.@)
435 * Delete an IP address added with AddIPAddress().
437 * PARAMS
438 * NTEContext [In] NTE context from AddIPAddress();
440 * RETURNS
441 * Success: NO_ERROR
442 * Failure: error code from winerror.h
444 * FIXME
445 * Stub, returns ERROR_NOT_SUPPORTED.
447 DWORD WINAPI DeleteIPAddress(ULONG NTEContext)
449 FIXME("(NTEContext %d): stub\n", NTEContext);
450 return ERROR_NOT_SUPPORTED;
454 /******************************************************************
455 * DeleteIpForwardEntry (IPHLPAPI.@)
457 * Delete a route.
459 * PARAMS
460 * pRoute [In] route to delete
462 * RETURNS
463 * Success: NO_ERROR
464 * Failure: error code from winerror.h
466 * FIXME
467 * Stub, returns NO_ERROR.
469 DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
471 FIXME("(pRoute %p): stub\n", pRoute);
472 /* could use SIOCDELRT, not sure I want to */
473 return 0;
477 /******************************************************************
478 * DeleteIpNetEntry (IPHLPAPI.@)
480 * Delete an ARP entry.
482 * PARAMS
483 * pArpEntry [In] ARP entry to delete
485 * RETURNS
486 * Success: NO_ERROR
487 * Failure: error code from winerror.h
489 * FIXME
490 * Stub, returns NO_ERROR.
492 DWORD WINAPI DeleteIpNetEntry(PMIB_IPNETROW pArpEntry)
494 FIXME("(pArpEntry %p): stub\n", pArpEntry);
495 /* could use SIOCDARP on systems that support it, not sure I want to */
496 return 0;
500 /******************************************************************
501 * DeleteProxyArpEntry (IPHLPAPI.@)
503 * Delete a Proxy ARP entry.
505 * PARAMS
506 * dwAddress [In] IP address for which this computer acts as a proxy.
507 * dwMask [In] subnet mask for dwAddress
508 * dwIfIndex [In] interface index
510 * RETURNS
511 * Success: NO_ERROR
512 * Failure: error code from winerror.h
514 * FIXME
515 * Stub, returns ERROR_NOT_SUPPORTED.
517 DWORD WINAPI DeleteProxyArpEntry(DWORD dwAddress, DWORD dwMask, DWORD dwIfIndex)
519 FIXME("(dwAddress 0x%08x, dwMask 0x%08x, dwIfIndex 0x%08x): stub\n",
520 dwAddress, dwMask, dwIfIndex);
521 return ERROR_NOT_SUPPORTED;
525 /******************************************************************
526 * EnableRouter (IPHLPAPI.@)
528 * Turn on ip forwarding.
530 * PARAMS
531 * pHandle [In/Out]
532 * pOverlapped [In/Out] hEvent member should contain a valid handle.
534 * RETURNS
535 * Success: ERROR_IO_PENDING
536 * Failure: error code from winerror.h
538 * FIXME
539 * Stub, returns ERROR_NOT_SUPPORTED.
541 DWORD WINAPI EnableRouter(HANDLE * pHandle, OVERLAPPED * pOverlapped)
543 FIXME("(pHandle %p, pOverlapped %p): stub\n", pHandle, pOverlapped);
544 /* could echo "1" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
545 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
547 return ERROR_NOT_SUPPORTED;
551 /******************************************************************
552 * FlushIpNetTable (IPHLPAPI.@)
554 * Delete all ARP entries of an interface
556 * PARAMS
557 * dwIfIndex [In] interface index
559 * RETURNS
560 * Success: NO_ERROR
561 * Failure: error code from winerror.h
563 * FIXME
564 * Stub, returns ERROR_NOT_SUPPORTED.
566 DWORD WINAPI FlushIpNetTable(DWORD dwIfIndex)
568 FIXME("(dwIfIndex 0x%08x): stub\n", dwIfIndex);
569 /* this flushes the arp cache of the given index */
570 return ERROR_NOT_SUPPORTED;
573 /******************************************************************
574 * FreeMibTable (IPHLPAPI.@)
576 * Free buffer allocated by network functions
578 * PARAMS
579 * ptr [In] pointer to the buffer to free
582 void WINAPI FreeMibTable(void *ptr)
584 TRACE("(%p)\n", ptr);
585 HeapFree(GetProcessHeap(), 0, ptr);
588 /******************************************************************
589 * GetAdapterIndex (IPHLPAPI.@)
591 * Get interface index from its name.
593 * PARAMS
594 * AdapterName [In] unicode string with the adapter name
595 * IfIndex [Out] returns found interface index
597 * RETURNS
598 * Success: NO_ERROR
599 * Failure: error code from winerror.h
601 DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex)
603 char adapterName[MAX_ADAPTER_NAME];
604 unsigned int i;
605 DWORD ret;
607 TRACE("(AdapterName %p, IfIndex %p)\n", AdapterName, IfIndex);
608 /* The adapter name is guaranteed not to have any unicode characters, so
609 * this translation is never lossy */
610 for (i = 0; i < sizeof(adapterName) - 1 && AdapterName[i]; i++)
611 adapterName[i] = (char)AdapterName[i];
612 adapterName[i] = '\0';
613 ret = getInterfaceIndexByName(adapterName, IfIndex);
614 TRACE("returning %d\n", ret);
615 return ret;
619 /******************************************************************
620 * GetAdaptersInfo (IPHLPAPI.@)
622 * Get information about adapters.
624 * PARAMS
625 * pAdapterInfo [Out] buffer for adapter infos
626 * pOutBufLen [In] length of output buffer
628 * RETURNS
629 * Success: NO_ERROR
630 * Failure: error code from winerror.h
632 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
634 DWORD ret;
636 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen);
637 if (!pOutBufLen)
638 ret = ERROR_INVALID_PARAMETER;
639 else {
640 DWORD numNonLoopbackInterfaces = get_interface_indices( TRUE, NULL );
642 if (numNonLoopbackInterfaces > 0) {
643 DWORD numIPAddresses = getNumIPAddresses();
644 ULONG size;
646 /* This may slightly overestimate the amount of space needed, because
647 * the IP addresses include the loopback address, but it's easier
648 * to make sure there's more than enough space than to make sure there's
649 * precisely enough space.
651 size = sizeof(IP_ADAPTER_INFO) * numNonLoopbackInterfaces;
652 size += numIPAddresses * sizeof(IP_ADDR_STRING);
653 if (!pAdapterInfo || *pOutBufLen < size) {
654 *pOutBufLen = size;
655 ret = ERROR_BUFFER_OVERFLOW;
657 else {
658 InterfaceIndexTable *table = NULL;
659 PMIB_IPADDRTABLE ipAddrTable = NULL;
660 PMIB_IPFORWARDTABLE routeTable = NULL;
662 ret = getIPAddrTable(&ipAddrTable, GetProcessHeap(), 0);
663 if (!ret)
664 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
665 if (!ret)
666 get_interface_indices( TRUE, &table );
667 if (table) {
668 size = sizeof(IP_ADAPTER_INFO) * table->numIndexes;
669 size += ipAddrTable->dwNumEntries * sizeof(IP_ADDR_STRING);
670 if (*pOutBufLen < size) {
671 *pOutBufLen = size;
672 ret = ERROR_INSUFFICIENT_BUFFER;
674 else {
675 DWORD ndx;
676 HKEY hKey;
677 BOOL winsEnabled = FALSE;
678 IP_ADDRESS_STRING primaryWINS, secondaryWINS;
679 PIP_ADDR_STRING nextIPAddr = (PIP_ADDR_STRING)((LPBYTE)pAdapterInfo
680 + numNonLoopbackInterfaces * sizeof(IP_ADAPTER_INFO));
682 memset(pAdapterInfo, 0, size);
683 /* @@ Wine registry key: HKCU\Software\Wine\Network */
684 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Network",
685 &hKey) == ERROR_SUCCESS) {
686 DWORD size = sizeof(primaryWINS.String);
687 unsigned long addr;
689 RegQueryValueExA(hKey, "WinsServer", NULL, NULL,
690 (LPBYTE)primaryWINS.String, &size);
691 addr = inet_addr(primaryWINS.String);
692 if (addr != INADDR_NONE && addr != INADDR_ANY)
693 winsEnabled = TRUE;
694 size = sizeof(secondaryWINS.String);
695 RegQueryValueExA(hKey, "BackupWinsServer", NULL, NULL,
696 (LPBYTE)secondaryWINS.String, &size);
697 addr = inet_addr(secondaryWINS.String);
698 if (addr != INADDR_NONE && addr != INADDR_ANY)
699 winsEnabled = TRUE;
700 RegCloseKey(hKey);
702 for (ndx = 0; ndx < table->numIndexes; ndx++) {
703 PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx];
704 DWORD i;
705 PIP_ADDR_STRING currentIPAddr = &ptr->IpAddressList;
706 BOOL firstIPAddr = TRUE;
708 /* on Win98 this is left empty, but whatever */
709 getInterfaceNameByIndex(table->indexes[ndx], ptr->AdapterName);
710 getInterfaceNameByIndex(table->indexes[ndx], ptr->Description);
711 ptr->AddressLength = sizeof(ptr->Address);
712 getInterfacePhysicalByIndex(table->indexes[ndx],
713 &ptr->AddressLength, ptr->Address, &ptr->Type);
714 ptr->Index = table->indexes[ndx];
715 for (i = 0; i < ipAddrTable->dwNumEntries; i++) {
716 if (ipAddrTable->table[i].dwIndex == ptr->Index) {
717 if (firstIPAddr) {
718 toIPAddressString(ipAddrTable->table[i].dwAddr,
719 ptr->IpAddressList.IpAddress.String);
720 toIPAddressString(ipAddrTable->table[i].dwMask,
721 ptr->IpAddressList.IpMask.String);
722 firstIPAddr = FALSE;
724 else {
725 currentIPAddr->Next = nextIPAddr;
726 currentIPAddr = nextIPAddr;
727 toIPAddressString(ipAddrTable->table[i].dwAddr,
728 currentIPAddr->IpAddress.String);
729 toIPAddressString(ipAddrTable->table[i].dwMask,
730 currentIPAddr->IpMask.String);
731 nextIPAddr++;
735 /* If no IP was found it probably means that the interface is not
736 * configured. In this case we have to return a zeroed IP and mask. */
737 if (firstIPAddr) {
738 strcpy(ptr->IpAddressList.IpAddress.String, "0.0.0.0");
739 strcpy(ptr->IpAddressList.IpMask.String, "0.0.0.0");
741 /* Find first router through this interface, which we'll assume
742 * is the default gateway for this adapter */
743 for (i = 0; i < routeTable->dwNumEntries; i++)
744 if (routeTable->table[i].dwForwardIfIndex == ptr->Index
745 && routeTable->table[i].u1.ForwardType ==
746 MIB_IPROUTE_TYPE_INDIRECT)
748 toIPAddressString(routeTable->table[i].dwForwardNextHop,
749 ptr->GatewayList.IpAddress.String);
750 toIPAddressString(routeTable->table[i].dwForwardMask,
751 ptr->GatewayList.IpMask.String);
753 if (winsEnabled) {
754 ptr->HaveWins = TRUE;
755 memcpy(ptr->PrimaryWinsServer.IpAddress.String,
756 primaryWINS.String, sizeof(primaryWINS.String));
757 memcpy(ptr->SecondaryWinsServer.IpAddress.String,
758 secondaryWINS.String, sizeof(secondaryWINS.String));
760 if (ndx < table->numIndexes - 1)
761 ptr->Next = &pAdapterInfo[ndx + 1];
762 else
763 ptr->Next = NULL;
765 ptr->DhcpEnabled = TRUE;
767 ret = NO_ERROR;
769 HeapFree(GetProcessHeap(), 0, table);
771 else
772 ret = ERROR_OUTOFMEMORY;
773 HeapFree(GetProcessHeap(), 0, routeTable);
774 HeapFree(GetProcessHeap(), 0, ipAddrTable);
777 else
778 ret = ERROR_NO_DATA;
780 TRACE("returning %d\n", ret);
781 return ret;
784 static DWORD typeFromMibType(DWORD mib_type)
786 switch (mib_type)
788 case MIB_IF_TYPE_ETHERNET: return IF_TYPE_ETHERNET_CSMACD;
789 case MIB_IF_TYPE_TOKENRING: return IF_TYPE_ISO88025_TOKENRING;
790 case MIB_IF_TYPE_PPP: return IF_TYPE_PPP;
791 case MIB_IF_TYPE_LOOPBACK: return IF_TYPE_SOFTWARE_LOOPBACK;
792 default: return IF_TYPE_OTHER;
796 static NET_IF_CONNECTION_TYPE connectionTypeFromMibType(DWORD mib_type)
798 switch (mib_type)
800 case MIB_IF_TYPE_PPP: return NET_IF_CONNECTION_DEMAND;
801 case MIB_IF_TYPE_SLIP: return NET_IF_CONNECTION_DEMAND;
802 default: return NET_IF_CONNECTION_DEDICATED;
806 static ULONG v4addressesFromIndex(IF_INDEX index, DWORD **addrs, ULONG *num_addrs, DWORD **masks)
808 ULONG ret, i, j;
809 MIB_IPADDRTABLE *at;
811 *num_addrs = 0;
812 if ((ret = getIPAddrTable(&at, GetProcessHeap(), 0))) return ret;
813 for (i = 0; i < at->dwNumEntries; i++)
815 if (at->table[i].dwIndex == index) (*num_addrs)++;
817 if (!(*addrs = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
819 HeapFree(GetProcessHeap(), 0, at);
820 return ERROR_OUTOFMEMORY;
822 if (!(*masks = HeapAlloc(GetProcessHeap(), 0, *num_addrs * sizeof(DWORD))))
824 HeapFree(GetProcessHeap(), 0, *addrs);
825 HeapFree(GetProcessHeap(), 0, at);
826 return ERROR_OUTOFMEMORY;
828 for (i = 0, j = 0; i < at->dwNumEntries; i++)
830 if (at->table[i].dwIndex == index)
832 (*addrs)[j] = at->table[i].dwAddr;
833 (*masks)[j] = at->table[i].dwMask;
834 j++;
837 HeapFree(GetProcessHeap(), 0, at);
838 return ERROR_SUCCESS;
841 static char *debugstr_ipv4(const in_addr_t *in_addr, char *buf)
843 const BYTE *addrp;
844 char *p = buf;
846 for (addrp = (const BYTE *)in_addr;
847 addrp - (const BYTE *)in_addr < sizeof(*in_addr);
848 addrp++)
850 if (addrp == (const BYTE *)in_addr + sizeof(*in_addr) - 1)
851 sprintf(p, "%d", *addrp);
852 else
853 p += sprintf(p, "%d.", *addrp);
855 return buf;
858 static ULONG count_v4_gateways(DWORD index, PMIB_IPFORWARDTABLE routeTable)
860 DWORD i, num_gateways = 0;
862 for (i = 0; i < routeTable->dwNumEntries; i++)
864 if (routeTable->table[i].dwForwardIfIndex == index &&
865 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
866 num_gateways++;
868 return num_gateways;
871 static DWORD mask_v4_to_prefix(DWORD m)
873 #ifdef HAVE___BUILTIN_POPCOUNT
874 return __builtin_popcount(m);
875 #else
876 m -= m >> 1 & 0x55555555;
877 m = (m & 0x33333333) + (m >> 2 & 0x33333333);
878 return ((m + (m >> 4)) & 0x0f0f0f0f) * 0x01010101 >> 24;
879 #endif
882 static DWORD mask_v6_to_prefix(SOCKET_ADDRESS *m)
884 const IN6_ADDR *mask = &((struct WS_sockaddr_in6 *)m->lpSockaddr)->sin6_addr;
885 DWORD ret = 0, i;
887 for (i = 0; i < 8; i++)
888 ret += mask_v4_to_prefix(mask->u.Word[i]);
889 return ret;
892 static PMIB_IPFORWARDROW findIPv4Gateway(DWORD index,
893 PMIB_IPFORWARDTABLE routeTable)
895 DWORD i;
896 PMIB_IPFORWARDROW row = NULL;
898 for (i = 0; !row && i < routeTable->dwNumEntries; i++)
900 if (routeTable->table[i].dwForwardIfIndex == index &&
901 routeTable->table[i].u1.ForwardType == MIB_IPROUTE_TYPE_INDIRECT)
902 row = &routeTable->table[i];
904 return row;
907 static void fill_unicast_addr_data(IP_ADAPTER_ADDRESSES *aa, IP_ADAPTER_UNICAST_ADDRESS *ua)
909 /* Actually this information should be read somewhere from the system
910 * but it doesn't matter much for the bugs found so far.
911 * This information is required for DirectPlay8 games. */
912 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK)
914 ua->PrefixOrigin = IpPrefixOriginDhcp;
915 ua->SuffixOrigin = IpSuffixOriginDhcp;
917 else
919 ua->PrefixOrigin = IpPrefixOriginManual;
920 ua->SuffixOrigin = IpSuffixOriginManual;
923 /* The address is not duplicated in the network */
924 ua->DadState = IpDadStatePreferred;
926 /* Some address life time values, required even for non-dhcp addresses */
927 ua->ValidLifetime = 60000;
928 ua->PreferredLifetime = 60000;
929 ua->LeaseLifetime = 60000;
932 static ULONG adapterAddressesFromIndex(ULONG family, ULONG flags, IF_INDEX index,
933 IP_ADAPTER_ADDRESSES *aa, ULONG *size)
935 ULONG ret = ERROR_SUCCESS, i, j, num_v4addrs = 0, num_v4_gateways = 0, num_v6addrs = 0, total_size;
936 DWORD *v4addrs = NULL, *v4masks = NULL;
937 SOCKET_ADDRESS *v6addrs = NULL, *v6masks = NULL;
938 PMIB_IPFORWARDTABLE routeTable = NULL;
940 if (family == WS_AF_INET)
942 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
944 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
946 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
947 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
950 else if (family == WS_AF_INET6)
952 ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
954 else if (family == WS_AF_UNSPEC)
956 ret = v4addressesFromIndex(index, &v4addrs, &num_v4addrs, &v4masks);
958 if (!ret && flags & GAA_FLAG_INCLUDE_ALL_GATEWAYS)
960 ret = AllocateAndGetIpForwardTableFromStack(&routeTable, FALSE, GetProcessHeap(), 0);
961 if (!ret) num_v4_gateways = count_v4_gateways(index, routeTable);
963 if (!ret) ret = v6addressesFromIndex(index, &v6addrs, &num_v6addrs, &v6masks);
965 else
967 FIXME("address family %u unsupported\n", family);
968 ret = ERROR_NO_DATA;
970 if (ret)
972 HeapFree(GetProcessHeap(), 0, v4addrs);
973 HeapFree(GetProcessHeap(), 0, v4masks);
974 HeapFree(GetProcessHeap(), 0, v6addrs);
975 HeapFree(GetProcessHeap(), 0, v6masks);
976 HeapFree(GetProcessHeap(), 0, routeTable);
977 return ret;
980 total_size = sizeof(IP_ADAPTER_ADDRESSES);
981 total_size += IF_NAMESIZE;
982 total_size += IF_NAMESIZE * sizeof(WCHAR);
983 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
984 total_size += IF_NAMESIZE * sizeof(WCHAR);
985 if (flags & GAA_FLAG_INCLUDE_PREFIX)
987 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v4addrs;
988 total_size += sizeof(IP_ADAPTER_PREFIX) * num_v6addrs;
989 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
990 for (i = 0; i < num_v6addrs; i++)
991 total_size += v6masks[i].iSockaddrLength;
993 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v4addrs;
994 total_size += sizeof(struct sockaddr_in) * num_v4addrs;
995 total_size += (sizeof(IP_ADAPTER_GATEWAY_ADDRESS) + sizeof(SOCKADDR_IN)) * num_v4_gateways;
996 total_size += sizeof(IP_ADAPTER_UNICAST_ADDRESS) * num_v6addrs;
997 total_size += sizeof(SOCKET_ADDRESS) * num_v6addrs;
998 for (i = 0; i < num_v6addrs; i++)
999 total_size += v6addrs[i].iSockaddrLength;
1001 if (aa && *size >= total_size)
1003 char name[IF_NAMESIZE], *ptr = (char *)aa + sizeof(IP_ADAPTER_ADDRESSES), *src;
1004 WCHAR *dst;
1005 DWORD buflen, type;
1006 INTERNAL_IF_OPER_STATUS status;
1008 memset(aa, 0, sizeof(IP_ADAPTER_ADDRESSES));
1009 aa->u.s.Length = sizeof(IP_ADAPTER_ADDRESSES);
1010 aa->u.s.IfIndex = index;
1012 getInterfaceNameByIndex(index, name);
1013 memcpy(ptr, name, IF_NAMESIZE);
1014 aa->AdapterName = ptr;
1015 ptr += IF_NAMESIZE;
1016 if (!(flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
1018 aa->FriendlyName = (WCHAR *)ptr;
1019 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
1020 *dst = *src;
1021 *dst++ = 0;
1022 ptr = (char *)dst;
1024 aa->Description = (WCHAR *)ptr;
1025 for (src = name, dst = (WCHAR *)ptr; *src; src++, dst++)
1026 *dst = *src;
1027 *dst++ = 0;
1028 ptr = (char *)dst;
1030 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name, num_v4addrs,
1031 num_v6addrs);
1033 buflen = MAX_INTERFACE_PHYSADDR;
1034 getInterfacePhysicalByIndex(index, &buflen, aa->PhysicalAddress, &type);
1035 aa->PhysicalAddressLength = buflen;
1036 aa->IfType = typeFromMibType(type);
1037 aa->ConnectionType = connectionTypeFromMibType(type);
1038 aa->Luid.Info.NetLuidIndex = index;
1039 aa->Luid.Info.IfType = aa->IfType;
1041 if (num_v4_gateways)
1043 PMIB_IPFORWARDROW adapterRow;
1045 if ((adapterRow = findIPv4Gateway(index, routeTable)))
1047 PIP_ADAPTER_GATEWAY_ADDRESS gw;
1048 PSOCKADDR_IN sin;
1050 gw = (PIP_ADAPTER_GATEWAY_ADDRESS)ptr;
1051 aa->FirstGatewayAddress = gw;
1053 gw->u.s.Length = sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1054 ptr += sizeof(IP_ADAPTER_GATEWAY_ADDRESS);
1055 sin = (PSOCKADDR_IN)ptr;
1056 sin->sin_family = WS_AF_INET;
1057 sin->sin_port = 0;
1058 memcpy(&sin->sin_addr, &adapterRow->dwForwardNextHop,
1059 sizeof(DWORD));
1060 gw->Address.lpSockaddr = (LPSOCKADDR)sin;
1061 gw->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1062 gw->Next = NULL;
1063 ptr += sizeof(SOCKADDR_IN);
1066 if (num_v4addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1068 IP_ADAPTER_UNICAST_ADDRESS *ua;
1069 struct WS_sockaddr_in *sa;
1070 aa->Flags |= IP_ADAPTER_IPV4_ENABLED;
1071 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1072 for (i = 0; i < num_v4addrs; i++)
1074 char addr_buf[16];
1076 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1077 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1078 ua->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1079 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1081 sa = (struct WS_sockaddr_in *)ua->Address.lpSockaddr;
1082 sa->sin_family = WS_AF_INET;
1083 sa->sin_addr.S_un.S_addr = v4addrs[i];
1084 sa->sin_port = 0;
1085 TRACE("IPv4 %d/%d: %s\n", i + 1, num_v4addrs,
1086 debugstr_ipv4(&sa->sin_addr.S_un.S_addr, addr_buf));
1087 fill_unicast_addr_data(aa, ua);
1089 ua->OnLinkPrefixLength = mask_v4_to_prefix(v4masks[i]);
1091 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1092 if (i < num_v4addrs - 1)
1094 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1095 ua = ua->Next;
1099 if (num_v6addrs && !(flags & GAA_FLAG_SKIP_UNICAST))
1101 IP_ADAPTER_UNICAST_ADDRESS *ua;
1102 struct WS_sockaddr_in6 *sa;
1104 aa->Flags |= IP_ADAPTER_IPV6_ENABLED;
1105 if (aa->FirstUnicastAddress)
1107 for (ua = aa->FirstUnicastAddress; ua->Next; ua = ua->Next)
1109 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1110 ua = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1112 else
1113 ua = aa->FirstUnicastAddress = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1114 for (i = 0; i < num_v6addrs; i++)
1116 char addr_buf[46];
1118 memset(ua, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS));
1119 ua->u.s.Length = sizeof(IP_ADAPTER_UNICAST_ADDRESS);
1120 ua->Address.iSockaddrLength = v6addrs[i].iSockaddrLength;
1121 ua->Address.lpSockaddr = (SOCKADDR *)((char *)ua + ua->u.s.Length);
1123 sa = (struct WS_sockaddr_in6 *)ua->Address.lpSockaddr;
1124 memcpy(sa, v6addrs[i].lpSockaddr, sizeof(*sa));
1125 TRACE("IPv6 %d/%d: %s\n", i + 1, num_v6addrs,
1126 debugstr_ipv6(sa, addr_buf));
1127 fill_unicast_addr_data(aa, ua);
1129 ua->OnLinkPrefixLength = mask_v6_to_prefix(&v6masks[i]);
1131 ptr += ua->u.s.Length + ua->Address.iSockaddrLength;
1132 if (i < num_v6addrs - 1)
1134 ua->Next = (IP_ADAPTER_UNICAST_ADDRESS *)ptr;
1135 ua = ua->Next;
1139 if (num_v4addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1141 IP_ADAPTER_PREFIX *prefix;
1143 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1144 for (i = 0; i < num_v4addrs; i++)
1146 char addr_buf[16];
1147 struct WS_sockaddr_in *sa;
1149 prefix->u.s.Length = sizeof(*prefix);
1150 prefix->u.s.Flags = 0;
1151 prefix->Next = NULL;
1152 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in);
1153 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1155 sa = (struct WS_sockaddr_in *)prefix->Address.lpSockaddr;
1156 sa->sin_family = WS_AF_INET;
1157 sa->sin_addr.S_un.S_addr = v4addrs[i] & v4masks[i];
1158 sa->sin_port = 0;
1160 prefix->PrefixLength = mask_v4_to_prefix(v4masks[i]);
1162 TRACE("IPv4 network: %s/%u\n",
1163 debugstr_ipv4((const in_addr_t *)&sa->sin_addr.S_un.S_addr, addr_buf),
1164 prefix->PrefixLength);
1166 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1167 if (i < num_v4addrs - 1)
1169 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1170 prefix = prefix->Next;
1174 if (num_v6addrs && (flags & GAA_FLAG_INCLUDE_PREFIX))
1176 IP_ADAPTER_PREFIX *prefix;
1178 if (aa->FirstPrefix)
1180 for (prefix = aa->FirstPrefix; prefix->Next; prefix = prefix->Next)
1182 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1183 prefix = (IP_ADAPTER_PREFIX *)ptr;
1185 else
1186 prefix = aa->FirstPrefix = (IP_ADAPTER_PREFIX *)ptr;
1187 for (i = 0; i < num_v6addrs; i++)
1189 char addr_buf[46];
1190 struct WS_sockaddr_in6 *sa;
1191 const IN6_ADDR *addr, *mask;
1193 prefix->u.s.Length = sizeof(*prefix);
1194 prefix->u.s.Flags = 0;
1195 prefix->Next = NULL;
1196 prefix->Address.iSockaddrLength = sizeof(struct sockaddr_in6);
1197 prefix->Address.lpSockaddr = (SOCKADDR *)((char *)prefix + prefix->u.s.Length);
1199 sa = (struct WS_sockaddr_in6 *)prefix->Address.lpSockaddr;
1200 sa->sin6_family = WS_AF_INET6;
1201 sa->sin6_port = 0;
1202 sa->sin6_flowinfo = 0;
1203 addr = &((struct WS_sockaddr_in6 *)v6addrs[i].lpSockaddr)->sin6_addr;
1204 mask = &((struct WS_sockaddr_in6 *)v6masks[i].lpSockaddr)->sin6_addr;
1205 for (j = 0; j < 8; j++) sa->sin6_addr.u.Word[j] = addr->u.Word[j] & mask->u.Word[j];
1206 sa->sin6_scope_id = 0;
1208 prefix->PrefixLength = mask_v6_to_prefix(&v6masks[i]);
1210 TRACE("IPv6 network: %s/%u\n", debugstr_ipv6(sa, addr_buf), prefix->PrefixLength);
1212 ptr += prefix->u.s.Length + prefix->Address.iSockaddrLength;
1213 if (i < num_v6addrs - 1)
1215 prefix->Next = (IP_ADAPTER_PREFIX *)ptr;
1216 prefix = prefix->Next;
1221 getInterfaceMtuByName(name, &aa->Mtu);
1223 getInterfaceStatusByName(name, &status);
1224 if (status == MIB_IF_OPER_STATUS_OPERATIONAL) aa->OperStatus = IfOperStatusUp;
1225 else if (status == MIB_IF_OPER_STATUS_NON_OPERATIONAL) aa->OperStatus = IfOperStatusDown;
1226 else aa->OperStatus = IfOperStatusUnknown;
1228 *size = total_size;
1229 HeapFree(GetProcessHeap(), 0, routeTable);
1230 HeapFree(GetProcessHeap(), 0, v6addrs);
1231 HeapFree(GetProcessHeap(), 0, v6masks);
1232 HeapFree(GetProcessHeap(), 0, v4addrs);
1233 HeapFree(GetProcessHeap(), 0, v4masks);
1234 return ERROR_SUCCESS;
1237 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in *src )
1239 SOCKADDR_IN *s = (SOCKADDR_IN *)dst;
1241 s->sin_family = WS_AF_INET;
1242 s->sin_port = src->sin_port;
1243 memcpy( &s->sin_addr, &src->sin_addr, sizeof(IN_ADDR) );
1244 memset( (char *)s + FIELD_OFFSET( SOCKADDR_IN, sin_zero ), 0,
1245 sizeof(SOCKADDR_STORAGE) - FIELD_OFFSET( SOCKADDR_IN, sin_zero) );
1248 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE *dst, const struct sockaddr_in6 *src )
1250 SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)dst;
1252 s->sin6_family = WS_AF_INET6;
1253 s->sin6_port = src->sin6_port;
1254 s->sin6_flowinfo = src->sin6_flowinfo;
1255 memcpy( &s->sin6_addr, &src->sin6_addr, sizeof(IN6_ADDR) );
1256 s->sin6_scope_id = src->sin6_scope_id;
1257 memset( (char *)s + sizeof(SOCKADDR_IN6), 0,
1258 sizeof(SOCKADDR_STORAGE) - sizeof(SOCKADDR_IN6) );
1261 #ifdef HAVE_STRUCT___RES_STATE
1262 /* call res_init() just once because of a bug in Mac OS X 10.4 */
1263 /* Call once per thread on systems that have per-thread _res. */
1265 static CRITICAL_SECTION res_init_cs;
1266 static CRITICAL_SECTION_DEBUG res_init_cs_debug = {
1267 0, 0, &res_init_cs,
1268 { &res_init_cs_debug.ProcessLocksList, &res_init_cs_debug.ProcessLocksList },
1269 0, 0, { (DWORD_PTR)(__FILE__ ": res_init_cs") }
1271 static CRITICAL_SECTION res_init_cs = { &res_init_cs_debug, -1, 0, 0, 0, 0 };
1273 static void initialise_resolver(void)
1275 EnterCriticalSection(&res_init_cs);
1276 if ((_res.options & RES_INIT) == 0)
1277 res_init();
1278 LeaveCriticalSection(&res_init_cs);
1281 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1283 int i, ip6_count = 0;
1284 SOCKADDR_STORAGE *addr;
1286 initialise_resolver();
1288 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1289 ip6_count = _res._u._ext.nscount6;
1290 #endif
1292 if (!servers || !num)
1294 num = _res.nscount;
1295 if (ip4_only) num -= ip6_count;
1296 return num;
1299 for (i = 0, addr = servers; addr < (servers + num) && i < _res.nscount; i++)
1301 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1302 if (_res._u._ext.nsaddrs[i])
1304 if (ip4_only) continue;
1305 sockaddr_in6_to_WS_storage( addr, _res._u._ext.nsaddrs[i] );
1307 else
1308 #endif
1310 sockaddr_in_to_WS_storage( addr, _res.nsaddr_list + i );
1312 addr++;
1314 return addr - servers;
1316 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
1318 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1320 extern struct res_state *__res_get_state( void );
1321 extern int __res_getservers( struct res_state *, struct sockaddr_storage *, int );
1322 struct res_state *state = __res_get_state();
1323 int i, found = 0, total = __res_getservers( state, NULL, 0 );
1324 SOCKADDR_STORAGE *addr = servers;
1325 struct sockaddr_storage *buf;
1327 if ((!servers || !num) && !ip4_only) return total;
1329 buf = HeapAlloc( GetProcessHeap(), 0, total * sizeof(struct sockaddr_storage) );
1330 total = __res_getservers( state, buf, total );
1332 for (i = 0; i < total; i++)
1334 if (buf[i].ss_family == AF_INET6 && ip4_only) continue;
1335 if (buf[i].ss_family != AF_INET && buf[i].ss_family != AF_INET6) continue;
1337 found++;
1338 if (!servers || !num) continue;
1340 if (buf[i].ss_family == AF_INET6)
1342 sockaddr_in6_to_WS_storage( addr, (struct sockaddr_in6 *)(buf + i) );
1344 else
1346 sockaddr_in_to_WS_storage( addr, (struct sockaddr_in *)(buf + i) );
1348 if (++addr >= servers + num) break;
1351 HeapFree( GetProcessHeap(), 0, buf );
1352 return found;
1354 #else
1356 static int get_dns_servers( SOCKADDR_STORAGE *servers, int num, BOOL ip4_only )
1358 FIXME("Unimplemented on this system\n");
1359 return 0;
1361 #endif
1363 static ULONG get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address, ULONG *len)
1365 int num = get_dns_servers( NULL, 0, FALSE );
1366 DWORD size;
1368 size = num * (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS) + sizeof(SOCKADDR_STORAGE));
1369 if (!address || *len < size)
1371 *len = size;
1372 return ERROR_BUFFER_OVERFLOW;
1374 *len = size;
1375 if (num > 0)
1377 PIP_ADAPTER_DNS_SERVER_ADDRESS addr = address;
1378 SOCKADDR_STORAGE *sock_addrs = (SOCKADDR_STORAGE *)(address + num);
1379 int i;
1381 get_dns_servers( sock_addrs, num, FALSE );
1383 for (i = 0; i < num; i++, addr = addr->Next)
1385 addr->u.s.Length = sizeof(*addr);
1386 if (sock_addrs[i].ss_family == WS_AF_INET6)
1387 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN6);
1388 else
1389 addr->Address.iSockaddrLength = sizeof(SOCKADDR_IN);
1390 addr->Address.lpSockaddr = (SOCKADDR *)(sock_addrs + i);
1391 if (i == num - 1)
1392 addr->Next = NULL;
1393 else
1394 addr->Next = addr + 1;
1397 return ERROR_SUCCESS;
1400 #ifdef HAVE_STRUCT___RES_STATE
1401 static BOOL is_ip_address_string(const char *str)
1403 struct in_addr in;
1404 int ret;
1406 ret = inet_aton(str, &in);
1407 return ret != 0;
1409 #endif
1411 static ULONG get_dns_suffix(WCHAR *suffix, ULONG *len)
1413 ULONG size;
1414 const char *found_suffix = "";
1415 /* Always return a NULL-terminated string, even if it's empty. */
1417 #ifdef HAVE_STRUCT___RES_STATE
1419 ULONG i;
1420 initialise_resolver();
1421 for (i = 0; !*found_suffix && i < MAXDNSRCH + 1 && _res.dnsrch[i]; i++)
1423 /* This uses a heuristic to select a DNS suffix:
1424 * the first, non-IP address string is selected.
1426 if (!is_ip_address_string(_res.dnsrch[i]))
1427 found_suffix = _res.dnsrch[i];
1430 #endif
1432 size = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, NULL, 0 ) * sizeof(WCHAR);
1433 if (!suffix || *len < size)
1435 *len = size;
1436 return ERROR_BUFFER_OVERFLOW;
1438 *len = MultiByteToWideChar( CP_UNIXCP, 0, found_suffix, -1, suffix, *len / sizeof(WCHAR) ) * sizeof(WCHAR);
1439 return ERROR_SUCCESS;
1442 ULONG WINAPI DECLSPEC_HOTPATCH GetAdaptersAddresses(ULONG family, ULONG flags, PVOID reserved,
1443 PIP_ADAPTER_ADDRESSES aa, PULONG buflen)
1445 InterfaceIndexTable *table;
1446 ULONG i, size, dns_server_size = 0, dns_suffix_size, total_size, ret = ERROR_NO_DATA;
1448 TRACE("(%d, %08x, %p, %p, %p)\n", family, flags, reserved, aa, buflen);
1450 if (!buflen) return ERROR_INVALID_PARAMETER;
1452 get_interface_indices( FALSE, &table );
1453 if (!table || !table->numIndexes)
1455 HeapFree(GetProcessHeap(), 0, table);
1456 return ERROR_NO_DATA;
1458 total_size = 0;
1459 for (i = 0; i < table->numIndexes; i++)
1461 size = 0;
1462 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], NULL, &size)))
1464 HeapFree(GetProcessHeap(), 0, table);
1465 return ret;
1467 total_size += size;
1469 if (!(flags & GAA_FLAG_SKIP_DNS_SERVER))
1471 /* Since DNS servers aren't really per adapter, get enough space for a
1472 * single copy of them.
1474 get_dns_server_addresses(NULL, &dns_server_size);
1475 total_size += dns_server_size;
1477 /* Since DNS suffix also isn't really per adapter, get enough space for a
1478 * single copy of it.
1480 get_dns_suffix(NULL, &dns_suffix_size);
1481 total_size += dns_suffix_size;
1482 if (aa && *buflen >= total_size)
1484 ULONG bytes_left = size = total_size;
1485 PIP_ADAPTER_ADDRESSES first_aa = aa;
1486 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns;
1487 WCHAR *dnsSuffix;
1489 for (i = 0; i < table->numIndexes; i++)
1491 if ((ret = adapterAddressesFromIndex(family, flags, table->indexes[i], aa, &size)))
1493 HeapFree(GetProcessHeap(), 0, table);
1494 return ret;
1496 if (i < table->numIndexes - 1)
1498 aa->Next = (IP_ADAPTER_ADDRESSES *)((char *)aa + size);
1499 aa = aa->Next;
1500 size = bytes_left -= size;
1503 if (dns_server_size)
1505 firstDns = (PIP_ADAPTER_DNS_SERVER_ADDRESS)((BYTE *)first_aa + total_size - dns_server_size - dns_suffix_size);
1506 get_dns_server_addresses(firstDns, &dns_server_size);
1507 for (aa = first_aa; aa; aa = aa->Next)
1509 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1510 aa->FirstDnsServerAddress = firstDns;
1513 aa = first_aa;
1514 dnsSuffix = (WCHAR *)((BYTE *)aa + total_size - dns_suffix_size);
1515 get_dns_suffix(dnsSuffix, &dns_suffix_size);
1516 for (; aa; aa = aa->Next)
1518 if (aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK && aa->OperStatus == IfOperStatusUp)
1519 aa->DnsSuffix = dnsSuffix;
1520 else
1521 aa->DnsSuffix = dnsSuffix + dns_suffix_size / sizeof(WCHAR) - 1;
1523 ret = ERROR_SUCCESS;
1525 else
1527 ret = ERROR_BUFFER_OVERFLOW;
1528 *buflen = total_size;
1531 TRACE("num adapters %u\n", table->numIndexes);
1532 HeapFree(GetProcessHeap(), 0, table);
1533 return ret;
1536 /******************************************************************
1537 * GetBestInterface (IPHLPAPI.@)
1539 * Get the interface, with the best route for the given IP address.
1541 * PARAMS
1542 * dwDestAddr [In] IP address to search the interface for
1543 * pdwBestIfIndex [Out] found best interface
1545 * RETURNS
1546 * Success: NO_ERROR
1547 * Failure: error code from winerror.h
1549 DWORD WINAPI GetBestInterface(IPAddr dwDestAddr, PDWORD pdwBestIfIndex)
1551 struct WS_sockaddr_in sa_in;
1552 memset(&sa_in, 0, sizeof(sa_in));
1553 sa_in.sin_family = WS_AF_INET;
1554 sa_in.sin_addr.S_un.S_addr = dwDestAddr;
1555 return GetBestInterfaceEx((struct WS_sockaddr *)&sa_in, pdwBestIfIndex);
1558 /******************************************************************
1559 * GetBestInterfaceEx (IPHLPAPI.@)
1561 * Get the interface, with the best route for the given IP address.
1563 * PARAMS
1564 * dwDestAddr [In] IP address to search the interface for
1565 * pdwBestIfIndex [Out] found best interface
1567 * RETURNS
1568 * Success: NO_ERROR
1569 * Failure: error code from winerror.h
1571 DWORD WINAPI GetBestInterfaceEx(struct WS_sockaddr *pDestAddr, PDWORD pdwBestIfIndex)
1573 DWORD ret;
1575 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr, pdwBestIfIndex);
1576 if (!pDestAddr || !pdwBestIfIndex)
1577 ret = ERROR_INVALID_PARAMETER;
1578 else {
1579 MIB_IPFORWARDROW ipRow;
1581 if (pDestAddr->sa_family == WS_AF_INET) {
1582 ret = GetBestRoute(((struct WS_sockaddr_in *)pDestAddr)->sin_addr.S_un.S_addr, 0, &ipRow);
1583 if (ret == ERROR_SUCCESS)
1584 *pdwBestIfIndex = ipRow.dwForwardIfIndex;
1585 } else {
1586 FIXME("address family %d not supported\n", pDestAddr->sa_family);
1587 ret = ERROR_NOT_SUPPORTED;
1590 TRACE("returning %d\n", ret);
1591 return ret;
1595 /******************************************************************
1596 * GetBestRoute (IPHLPAPI.@)
1598 * Get the best route for the given IP address.
1600 * PARAMS
1601 * dwDestAddr [In] IP address to search the best route for
1602 * dwSourceAddr [In] optional source IP address
1603 * pBestRoute [Out] found best route
1605 * RETURNS
1606 * Success: NO_ERROR
1607 * Failure: error code from winerror.h
1609 DWORD WINAPI GetBestRoute(DWORD dwDestAddr, DWORD dwSourceAddr, PMIB_IPFORWARDROW pBestRoute)
1611 PMIB_IPFORWARDTABLE table;
1612 DWORD ret;
1614 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr,
1615 dwSourceAddr, pBestRoute);
1616 if (!pBestRoute)
1617 return ERROR_INVALID_PARAMETER;
1619 ret = AllocateAndGetIpForwardTableFromStack(&table, FALSE, GetProcessHeap(), 0);
1620 if (!ret) {
1621 DWORD ndx, matchedBits, matchedNdx = table->dwNumEntries;
1623 for (ndx = 0, matchedBits = 0; ndx < table->dwNumEntries; ndx++) {
1624 if (table->table[ndx].u1.ForwardType != MIB_IPROUTE_TYPE_INVALID &&
1625 (dwDestAddr & table->table[ndx].dwForwardMask) ==
1626 (table->table[ndx].dwForwardDest & table->table[ndx].dwForwardMask)) {
1627 DWORD numShifts, mask;
1629 for (numShifts = 0, mask = table->table[ndx].dwForwardMask;
1630 mask && mask & 1; mask >>= 1, numShifts++)
1632 if (numShifts > matchedBits) {
1633 matchedBits = numShifts;
1634 matchedNdx = ndx;
1636 else if (!matchedBits) {
1637 matchedNdx = ndx;
1641 if (matchedNdx < table->dwNumEntries) {
1642 memcpy(pBestRoute, &table->table[matchedNdx], sizeof(MIB_IPFORWARDROW));
1643 ret = ERROR_SUCCESS;
1645 else {
1646 /* No route matches, which can happen if there's no default route. */
1647 ret = ERROR_HOST_UNREACHABLE;
1649 HeapFree(GetProcessHeap(), 0, table);
1651 TRACE("returning %d\n", ret);
1652 return ret;
1656 /******************************************************************
1657 * GetFriendlyIfIndex (IPHLPAPI.@)
1659 * Get a "friendly" version of IfIndex, which is one that doesn't
1660 * have the top byte set. Doesn't validate whether IfIndex is a valid
1661 * adapter index.
1663 * PARAMS
1664 * IfIndex [In] interface index to get the friendly one for
1666 * RETURNS
1667 * A friendly version of IfIndex.
1669 DWORD WINAPI GetFriendlyIfIndex(DWORD IfIndex)
1671 /* windows doesn't validate these, either, just makes sure the top byte is
1672 cleared. I assume my ifenum module never gives an index with the top
1673 byte set. */
1674 TRACE("returning %d\n", IfIndex);
1675 return IfIndex;
1679 /******************************************************************
1680 * GetIfEntry (IPHLPAPI.@)
1682 * Get information about an interface.
1684 * PARAMS
1685 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1686 * Out: interface information
1688 * RETURNS
1689 * Success: NO_ERROR
1690 * Failure: error code from winerror.h
1692 DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
1694 DWORD ret;
1695 char nameBuf[MAX_ADAPTER_NAME];
1696 char *name;
1698 TRACE("pIfRow %p\n", pIfRow);
1699 if (!pIfRow)
1700 return ERROR_INVALID_PARAMETER;
1702 name = getInterfaceNameByIndex(pIfRow->dwIndex, nameBuf);
1703 if (name) {
1704 ret = getInterfaceEntryByName(name, pIfRow);
1705 if (ret == NO_ERROR)
1706 ret = getInterfaceStatsByName(name, pIfRow);
1708 else
1709 ret = ERROR_INVALID_DATA;
1710 TRACE("returning %d\n", ret);
1711 return ret;
1714 /******************************************************************
1715 * GetIfEntry2 (IPHLPAPI.@)
1717 DWORD WINAPI GetIfEntry2( MIB_IF_ROW2 *row2 )
1719 DWORD ret, len = sizeof(row2->Description)/sizeof(row2->Description[0]);
1720 char buf[MAX_ADAPTER_NAME], *name;
1721 MIB_IFROW row;
1723 TRACE("%p\n", row2);
1725 if (!row2 || (!(name = getInterfaceNameByIndex( row2->InterfaceIndex, buf )) &&
1726 !(name = getInterfaceNameByIndex( row2->InterfaceLuid.Info.NetLuidIndex, buf ))))
1728 return ERROR_INVALID_PARAMETER;
1730 if ((ret = getInterfaceEntryByName( name, &row ))) return ret;
1731 if ((ret = getInterfaceStatsByName( name, &row ))) return ret;
1733 memset( row2, 0, sizeof(*row2) );
1734 row2->InterfaceLuid.Info.Reserved = 0;
1735 row2->InterfaceLuid.Info.NetLuidIndex = row.dwIndex;
1736 row2->InterfaceLuid.Info.IfType = row.dwType;
1737 row2->InterfaceIndex = row.dwIndex;
1738 row2->InterfaceGuid.Data1 = row.dwIndex;
1739 row2->Type = row.dwType;
1740 row2->Mtu = row.dwMtu;
1741 MultiByteToWideChar( CP_UNIXCP, 0, (const char *)row.bDescr, -1, row2->Description, len );
1742 row2->PhysicalAddressLength = row.dwPhysAddrLen;
1743 memcpy( &row2->PhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1744 memcpy( &row2->PermanentPhysicalAddress, &row.bPhysAddr, row.dwPhysAddrLen );
1745 row2->OperStatus = IfOperStatusUp;
1746 row2->AdminStatus = NET_IF_ADMIN_STATUS_UP;
1747 row2->MediaConnectState = MediaConnectStateConnected;
1748 row2->ConnectionType = NET_IF_CONNECTION_DEDICATED;
1750 /* stats */
1751 row2->InOctets = row.dwInOctets;
1752 row2->InUcastPkts = row.dwInUcastPkts;
1753 row2->InNUcastPkts = row.dwInNUcastPkts;
1754 row2->InDiscards = row.dwInDiscards;
1755 row2->InErrors = row.dwInErrors;
1756 row2->InUnknownProtos = row.dwInUnknownProtos;
1757 row2->OutOctets = row.dwOutOctets;
1758 row2->OutUcastPkts = row.dwOutUcastPkts;
1759 row2->OutNUcastPkts = row.dwOutNUcastPkts;
1760 row2->OutDiscards = row.dwOutDiscards;
1761 row2->OutErrors = row.dwOutErrors;
1763 return NO_ERROR;
1766 static int IfTableSorter(const void *a, const void *b)
1768 int ret;
1770 if (a && b)
1771 ret = ((const MIB_IFROW*)a)->dwIndex - ((const MIB_IFROW*)b)->dwIndex;
1772 else
1773 ret = 0;
1774 return ret;
1778 /******************************************************************
1779 * GetIfTable (IPHLPAPI.@)
1781 * Get a table of local interfaces.
1783 * PARAMS
1784 * pIfTable [Out] buffer for local interfaces table
1785 * pdwSize [In/Out] length of output buffer
1786 * bOrder [In] whether to sort the table
1788 * RETURNS
1789 * Success: NO_ERROR
1790 * Failure: error code from winerror.h
1792 * NOTES
1793 * If pdwSize is less than required, the function will return
1794 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1795 * size.
1796 * If bOrder is true, the returned table will be sorted by interface index.
1798 DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
1800 DWORD ret;
1802 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pdwSize, pdwSize,
1803 (DWORD)bOrder);
1804 if (!pdwSize)
1805 ret = ERROR_INVALID_PARAMETER;
1806 else {
1807 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1808 ULONG size = sizeof(MIB_IFTABLE);
1810 if (numInterfaces > 1)
1811 size += (numInterfaces - 1) * sizeof(MIB_IFROW);
1812 if (!pIfTable || *pdwSize < size) {
1813 *pdwSize = size;
1814 ret = ERROR_INSUFFICIENT_BUFFER;
1816 else {
1817 InterfaceIndexTable *table;
1818 get_interface_indices( FALSE, &table );
1820 if (table) {
1821 size = sizeof(MIB_IFTABLE);
1822 if (table->numIndexes > 1)
1823 size += (table->numIndexes - 1) * sizeof(MIB_IFROW);
1824 if (*pdwSize < size) {
1825 *pdwSize = size;
1826 ret = ERROR_INSUFFICIENT_BUFFER;
1828 else {
1829 DWORD ndx;
1831 *pdwSize = size;
1832 pIfTable->dwNumEntries = 0;
1833 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1834 pIfTable->table[ndx].dwIndex = table->indexes[ndx];
1835 GetIfEntry(&pIfTable->table[ndx]);
1836 pIfTable->dwNumEntries++;
1838 if (bOrder)
1839 qsort(pIfTable->table, pIfTable->dwNumEntries, sizeof(MIB_IFROW),
1840 IfTableSorter);
1841 ret = NO_ERROR;
1843 HeapFree(GetProcessHeap(), 0, table);
1845 else
1846 ret = ERROR_OUTOFMEMORY;
1849 TRACE("returning %d\n", ret);
1850 return ret;
1853 /******************************************************************
1854 * GetIfTable2 (IPHLPAPI.@)
1856 DWORD WINAPI GetIfTable2( MIB_IF_TABLE2 **table )
1858 DWORD i, nb_interfaces, size = sizeof(MIB_IF_TABLE2);
1859 InterfaceIndexTable *index_table;
1860 MIB_IF_TABLE2 *ret;
1862 TRACE( "table %p\n", table );
1864 if (!table) return ERROR_INVALID_PARAMETER;
1866 if ((nb_interfaces = get_interface_indices( FALSE, NULL )) > 1)
1867 size += (nb_interfaces - 1) * sizeof(MIB_IF_ROW2);
1869 if (!(ret = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
1871 get_interface_indices( FALSE, &index_table );
1872 if (!index_table)
1874 HeapFree( GetProcessHeap(), 0, ret );
1875 return ERROR_OUTOFMEMORY;
1878 ret->NumEntries = 0;
1879 for (i = 0; i < index_table->numIndexes; i++)
1881 ret->Table[i].InterfaceIndex = index_table->indexes[i];
1882 GetIfEntry2( &ret->Table[i] );
1883 ret->NumEntries++;
1886 HeapFree( GetProcessHeap(), 0, index_table );
1887 *table = ret;
1888 return NO_ERROR;
1891 /******************************************************************
1892 * GetInterfaceInfo (IPHLPAPI.@)
1894 * Get a list of network interface adapters.
1896 * PARAMS
1897 * pIfTable [Out] buffer for interface adapters
1898 * dwOutBufLen [Out] if buffer is too small, returns required size
1900 * RETURNS
1901 * Success: NO_ERROR
1902 * Failure: error code from winerror.h
1904 * BUGS
1905 * MSDN states this should return non-loopback interfaces only.
1907 DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
1909 DWORD ret;
1911 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable, dwOutBufLen);
1912 if (!dwOutBufLen)
1913 ret = ERROR_INVALID_PARAMETER;
1914 else {
1915 DWORD numInterfaces = get_interface_indices( FALSE, NULL );
1916 ULONG size = sizeof(IP_INTERFACE_INFO);
1918 if (numInterfaces > 1)
1919 size += (numInterfaces - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1920 if (!pIfTable || *dwOutBufLen < size) {
1921 *dwOutBufLen = size;
1922 ret = ERROR_INSUFFICIENT_BUFFER;
1924 else {
1925 InterfaceIndexTable *table;
1926 get_interface_indices( FALSE, &table );
1928 if (table) {
1929 size = sizeof(IP_INTERFACE_INFO);
1930 if (table->numIndexes > 1)
1931 size += (table->numIndexes - 1) * sizeof(IP_ADAPTER_INDEX_MAP);
1932 if (*dwOutBufLen < size) {
1933 *dwOutBufLen = size;
1934 ret = ERROR_INSUFFICIENT_BUFFER;
1936 else {
1937 DWORD ndx;
1938 char nameBuf[MAX_ADAPTER_NAME];
1940 *dwOutBufLen = size;
1941 pIfTable->NumAdapters = 0;
1942 for (ndx = 0; ndx < table->numIndexes; ndx++) {
1943 const char *walker, *name;
1944 WCHAR *assigner;
1946 pIfTable->Adapter[ndx].Index = table->indexes[ndx];
1947 name = getInterfaceNameByIndex(table->indexes[ndx], nameBuf);
1948 for (walker = name, assigner = pIfTable->Adapter[ndx].Name;
1949 walker && *walker &&
1950 assigner - pIfTable->Adapter[ndx].Name < MAX_ADAPTER_NAME - 1;
1951 walker++, assigner++)
1952 *assigner = *walker;
1953 *assigner = 0;
1954 pIfTable->NumAdapters++;
1956 ret = NO_ERROR;
1958 HeapFree(GetProcessHeap(), 0, table);
1960 else
1961 ret = ERROR_OUTOFMEMORY;
1964 TRACE("returning %d\n", ret);
1965 return ret;
1969 /******************************************************************
1970 * GetIpAddrTable (IPHLPAPI.@)
1972 * Get interface-to-IP address mapping table.
1974 * PARAMS
1975 * pIpAddrTable [Out] buffer for mapping table
1976 * pdwSize [In/Out] length of output buffer
1977 * bOrder [In] whether to sort the table
1979 * RETURNS
1980 * Success: NO_ERROR
1981 * Failure: error code from winerror.h
1983 * NOTES
1984 * If pdwSize is less than required, the function will return
1985 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1986 * size.
1987 * If bOrder is true, the returned table will be sorted by the next hop and
1988 * an assortment of arbitrary parameters.
1990 DWORD WINAPI GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize, BOOL bOrder)
1992 DWORD ret;
1994 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable, pdwSize,
1995 (DWORD)bOrder);
1996 if (!pdwSize)
1997 ret = ERROR_INVALID_PARAMETER;
1998 else {
1999 PMIB_IPADDRTABLE table;
2001 ret = getIPAddrTable(&table, GetProcessHeap(), 0);
2002 if (ret == NO_ERROR)
2004 ULONG size = FIELD_OFFSET(MIB_IPADDRTABLE, table[table->dwNumEntries]);
2006 if (!pIpAddrTable || *pdwSize < size) {
2007 *pdwSize = size;
2008 ret = ERROR_INSUFFICIENT_BUFFER;
2010 else {
2011 *pdwSize = size;
2012 memcpy(pIpAddrTable, table, size);
2013 /* sort by numeric IP value */
2014 if (bOrder)
2015 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
2016 sizeof(MIB_IPADDRROW), IpAddrTableNumericSorter);
2017 /* sort ensuring loopback interfaces are in the end */
2018 else
2019 qsort(pIpAddrTable->table, pIpAddrTable->dwNumEntries,
2020 sizeof(MIB_IPADDRROW), IpAddrTableLoopbackSorter);
2021 ret = NO_ERROR;
2023 HeapFree(GetProcessHeap(), 0, table);
2026 TRACE("returning %d\n", ret);
2027 return ret;
2031 /******************************************************************
2032 * GetIpForwardTable (IPHLPAPI.@)
2034 * Get the route table.
2036 * PARAMS
2037 * pIpForwardTable [Out] buffer for route table
2038 * pdwSize [In/Out] length of output buffer
2039 * bOrder [In] whether to sort the table
2041 * RETURNS
2042 * Success: NO_ERROR
2043 * Failure: error code from winerror.h
2045 * NOTES
2046 * If pdwSize is less than required, the function will return
2047 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2048 * size.
2049 * If bOrder is true, the returned table will be sorted by the next hop and
2050 * an assortment of arbitrary parameters.
2052 DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
2054 DWORD ret;
2055 PMIB_IPFORWARDTABLE table;
2057 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable, pdwSize, bOrder);
2059 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2061 ret = AllocateAndGetIpForwardTableFromStack(&table, bOrder, GetProcessHeap(), 0);
2062 if (!ret) {
2063 DWORD size = FIELD_OFFSET( MIB_IPFORWARDTABLE, table[table->dwNumEntries] );
2064 if (!pIpForwardTable || *pdwSize < size) {
2065 *pdwSize = size;
2066 ret = ERROR_INSUFFICIENT_BUFFER;
2068 else {
2069 *pdwSize = size;
2070 memcpy(pIpForwardTable, table, size);
2072 HeapFree(GetProcessHeap(), 0, table);
2074 TRACE("returning %d\n", ret);
2075 return ret;
2079 /******************************************************************
2080 * GetIpNetTable (IPHLPAPI.@)
2082 * Get the IP-to-physical address mapping table.
2084 * PARAMS
2085 * pIpNetTable [Out] buffer for mapping table
2086 * pdwSize [In/Out] length of output buffer
2087 * bOrder [In] whether to sort the table
2089 * RETURNS
2090 * Success: NO_ERROR
2091 * Failure: error code from winerror.h
2093 * NOTES
2094 * If pdwSize is less than required, the function will return
2095 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2096 * size.
2097 * If bOrder is true, the returned table will be sorted by IP address.
2099 DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
2101 DWORD ret;
2102 PMIB_IPNETTABLE table;
2104 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
2106 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2108 ret = AllocateAndGetIpNetTableFromStack( &table, bOrder, GetProcessHeap(), 0 );
2109 if (!ret) {
2110 DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
2111 if (!pIpNetTable || *pdwSize < size) {
2112 *pdwSize = size;
2113 ret = ERROR_INSUFFICIENT_BUFFER;
2115 else {
2116 *pdwSize = size;
2117 memcpy(pIpNetTable, table, size);
2119 HeapFree(GetProcessHeap(), 0, table);
2121 TRACE("returning %d\n", ret);
2122 return ret;
2125 /* Gets the DNS server list into the list beginning at list. Assumes that
2126 * a single server address may be placed at list if *len is at least
2127 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
2128 * and assumes that all remaining DNS servers are contiguously located
2129 * beginning at firstDynamic. On input, *len is assumed to be the total number
2130 * of bytes available for all DNS servers, and is ignored if list is NULL.
2131 * On return, *len is set to the total number of bytes required for all DNS
2132 * servers.
2133 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
2134 * ERROR_SUCCESS otherwise.
2136 static DWORD get_dns_server_list(PIP_ADDR_STRING list,
2137 PIP_ADDR_STRING firstDynamic, DWORD *len)
2139 DWORD size;
2140 int num = get_dns_servers( NULL, 0, TRUE );
2142 size = num * sizeof(IP_ADDR_STRING);
2143 if (!list || *len < size) {
2144 *len = size;
2145 return ERROR_BUFFER_OVERFLOW;
2147 *len = size;
2148 if (num > 0) {
2149 PIP_ADDR_STRING ptr;
2150 int i;
2151 SOCKADDR_STORAGE *addr = HeapAlloc( GetProcessHeap(), 0, num * sizeof(SOCKADDR_STORAGE) );
2153 get_dns_servers( addr, num, TRUE );
2155 for (i = 0, ptr = list; i < num; i++, ptr = ptr->Next) {
2156 toIPAddressString(((struct sockaddr_in *)(addr + i))->sin_addr.s_addr,
2157 ptr->IpAddress.String);
2158 if (i == num - 1)
2159 ptr->Next = NULL;
2160 else if (i == 0)
2161 ptr->Next = firstDynamic;
2162 else
2163 ptr->Next = (PIP_ADDR_STRING)((PBYTE)ptr + sizeof(IP_ADDR_STRING));
2165 HeapFree( GetProcessHeap(), 0, addr );
2167 return ERROR_SUCCESS;
2170 /******************************************************************
2171 * GetNetworkParams (IPHLPAPI.@)
2173 * Get the network parameters for the local computer.
2175 * PARAMS
2176 * pFixedInfo [Out] buffer for network parameters
2177 * pOutBufLen [In/Out] length of output buffer
2179 * RETURNS
2180 * Success: NO_ERROR
2181 * Failure: error code from winerror.h
2183 * NOTES
2184 * If pOutBufLen is less than required, the function will return
2185 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
2186 * size.
2188 DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
2190 DWORD ret, size, serverListSize;
2191 LONG regReturn;
2192 HKEY hKey;
2194 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo, pOutBufLen);
2195 if (!pOutBufLen)
2196 return ERROR_INVALID_PARAMETER;
2198 get_dns_server_list(NULL, NULL, &serverListSize);
2199 size = sizeof(FIXED_INFO) + serverListSize - sizeof(IP_ADDR_STRING);
2200 if (!pFixedInfo || *pOutBufLen < size) {
2201 *pOutBufLen = size;
2202 return ERROR_BUFFER_OVERFLOW;
2205 memset(pFixedInfo, 0, size);
2206 size = sizeof(pFixedInfo->HostName);
2207 GetComputerNameExA(ComputerNameDnsHostname, pFixedInfo->HostName, &size);
2208 size = sizeof(pFixedInfo->DomainName);
2209 GetComputerNameExA(ComputerNameDnsDomain, pFixedInfo->DomainName, &size);
2210 get_dns_server_list(&pFixedInfo->DnsServerList,
2211 (PIP_ADDR_STRING)((BYTE *)pFixedInfo + sizeof(FIXED_INFO)),
2212 &serverListSize);
2213 /* Assume the first DNS server in the list is the "current" DNS server: */
2214 pFixedInfo->CurrentDnsServer = &pFixedInfo->DnsServerList;
2215 pFixedInfo->NodeType = HYBRID_NODETYPE;
2216 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2217 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
2218 if (regReturn != ERROR_SUCCESS)
2219 regReturn = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
2220 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ,
2221 &hKey);
2222 if (regReturn == ERROR_SUCCESS)
2224 DWORD size = sizeof(pFixedInfo->ScopeId);
2226 RegQueryValueExA(hKey, "ScopeID", NULL, NULL, (LPBYTE)pFixedInfo->ScopeId, &size);
2227 RegCloseKey(hKey);
2230 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
2231 I suppose could also check for a listener on port 53 to set EnableDns */
2232 ret = NO_ERROR;
2233 TRACE("returning %d\n", ret);
2234 return ret;
2238 /******************************************************************
2239 * GetNumberOfInterfaces (IPHLPAPI.@)
2241 * Get the number of interfaces.
2243 * PARAMS
2244 * pdwNumIf [Out] number of interfaces
2246 * RETURNS
2247 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
2249 DWORD WINAPI GetNumberOfInterfaces(PDWORD pdwNumIf)
2251 DWORD ret;
2253 TRACE("pdwNumIf %p\n", pdwNumIf);
2254 if (!pdwNumIf)
2255 ret = ERROR_INVALID_PARAMETER;
2256 else {
2257 *pdwNumIf = get_interface_indices( FALSE, NULL );
2258 ret = NO_ERROR;
2260 TRACE("returning %d\n", ret);
2261 return ret;
2265 /******************************************************************
2266 * GetPerAdapterInfo (IPHLPAPI.@)
2268 * Get information about an adapter corresponding to an interface.
2270 * PARAMS
2271 * IfIndex [In] interface info
2272 * pPerAdapterInfo [Out] buffer for per adapter info
2273 * pOutBufLen [In/Out] length of output buffer
2275 * RETURNS
2276 * Success: NO_ERROR
2277 * Failure: error code from winerror.h
2279 DWORD WINAPI GetPerAdapterInfo(ULONG IfIndex, PIP_PER_ADAPTER_INFO pPerAdapterInfo, PULONG pOutBufLen)
2281 ULONG bytesNeeded = sizeof(IP_PER_ADAPTER_INFO), serverListSize = 0;
2282 DWORD ret = NO_ERROR;
2284 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex, pPerAdapterInfo, pOutBufLen);
2286 if (!pOutBufLen) return ERROR_INVALID_PARAMETER;
2288 if (!isIfIndexLoopback(IfIndex)) {
2289 get_dns_server_list(NULL, NULL, &serverListSize);
2290 if (serverListSize > sizeof(IP_ADDR_STRING))
2291 bytesNeeded += serverListSize - sizeof(IP_ADDR_STRING);
2293 if (!pPerAdapterInfo || *pOutBufLen < bytesNeeded)
2295 *pOutBufLen = bytesNeeded;
2296 return ERROR_BUFFER_OVERFLOW;
2299 memset(pPerAdapterInfo, 0, bytesNeeded);
2300 if (!isIfIndexLoopback(IfIndex)) {
2301 ret = get_dns_server_list(&pPerAdapterInfo->DnsServerList,
2302 (PIP_ADDR_STRING)((PBYTE)pPerAdapterInfo + sizeof(IP_PER_ADAPTER_INFO)),
2303 &serverListSize);
2304 /* Assume the first DNS server in the list is the "current" DNS server: */
2305 pPerAdapterInfo->CurrentDnsServer = &pPerAdapterInfo->DnsServerList;
2307 return ret;
2311 /******************************************************************
2312 * GetRTTAndHopCount (IPHLPAPI.@)
2314 * Get round-trip time (RTT) and hop count.
2316 * PARAMS
2318 * DestIpAddress [In] destination address to get the info for
2319 * HopCount [Out] retrieved hop count
2320 * MaxHops [In] maximum hops to search for the destination
2321 * RTT [Out] RTT in milliseconds
2323 * RETURNS
2324 * Success: TRUE
2325 * Failure: FALSE
2327 * FIXME
2328 * Stub, returns FALSE.
2330 BOOL WINAPI GetRTTAndHopCount(IPAddr DestIpAddress, PULONG HopCount, ULONG MaxHops, PULONG RTT)
2332 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
2333 DestIpAddress, HopCount, MaxHops, RTT);
2334 return FALSE;
2338 /******************************************************************
2339 * GetTcpTable (IPHLPAPI.@)
2341 * Get the table of active TCP connections.
2343 * PARAMS
2344 * pTcpTable [Out] buffer for TCP connections table
2345 * pdwSize [In/Out] length of output buffer
2346 * bOrder [In] whether to order the table
2348 * RETURNS
2349 * Success: NO_ERROR
2350 * Failure: error code from winerror.h
2352 * NOTES
2353 * If pdwSize is less than required, the function will return
2354 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2355 * the required byte size.
2356 * If bOrder is true, the returned table will be sorted, first by
2357 * local address and port number, then by remote address and port
2358 * number.
2360 DWORD WINAPI GetTcpTable(PMIB_TCPTABLE pTcpTable, PDWORD pdwSize, BOOL bOrder)
2362 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable, pdwSize, bOrder);
2363 return GetExtendedTcpTable(pTcpTable, pdwSize, bOrder, WS_AF_INET, TCP_TABLE_BASIC_ALL, 0);
2366 /******************************************************************
2367 * GetExtendedTcpTable (IPHLPAPI.@)
2369 DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder,
2370 ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
2372 DWORD ret, size;
2373 void *table;
2375 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2376 pTcpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2378 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2380 if (ulAf != WS_AF_INET)
2382 FIXME("ulAf = %u not supported\n", ulAf);
2383 return ERROR_NOT_SUPPORTED;
2385 if (TableClass >= TCP_TABLE_OWNER_MODULE_LISTENER)
2386 FIXME("module classes not fully supported\n");
2388 if ((ret = build_tcp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2389 return ret;
2391 if (!pTcpTable || *pdwSize < size)
2393 *pdwSize = size;
2394 ret = ERROR_INSUFFICIENT_BUFFER;
2396 else
2398 *pdwSize = size;
2399 memcpy(pTcpTable, table, size);
2401 HeapFree(GetProcessHeap(), 0, table);
2402 return ret;
2405 /******************************************************************
2406 * GetUdpTable (IPHLPAPI.@)
2408 * Get a table of active UDP connections.
2410 * PARAMS
2411 * pUdpTable [Out] buffer for UDP connections table
2412 * pdwSize [In/Out] length of output buffer
2413 * bOrder [In] whether to order the table
2415 * RETURNS
2416 * Success: NO_ERROR
2417 * Failure: error code from winerror.h
2419 * NOTES
2420 * If pdwSize is less than required, the function will return
2421 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2422 * required byte size.
2423 * If bOrder is true, the returned table will be sorted, first by
2424 * local address, then by local port number.
2426 DWORD WINAPI GetUdpTable(PMIB_UDPTABLE pUdpTable, PDWORD pdwSize, BOOL bOrder)
2428 return GetExtendedUdpTable(pUdpTable, pdwSize, bOrder, WS_AF_INET, UDP_TABLE_BASIC, 0);
2431 /******************************************************************
2432 * GetExtendedUdpTable (IPHLPAPI.@)
2434 DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder,
2435 ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
2437 DWORD ret, size;
2438 void *table;
2440 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2441 pUdpTable, pdwSize, bOrder, ulAf, TableClass, Reserved);
2443 if (!pdwSize) return ERROR_INVALID_PARAMETER;
2445 if (ulAf != WS_AF_INET)
2447 FIXME("ulAf = %u not supported\n", ulAf);
2448 return ERROR_NOT_SUPPORTED;
2450 if (TableClass == UDP_TABLE_OWNER_MODULE)
2451 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2453 if ((ret = build_udp_table(TableClass, &table, bOrder, GetProcessHeap(), 0, &size)))
2454 return ret;
2456 if (!pUdpTable || *pdwSize < size)
2458 *pdwSize = size;
2459 ret = ERROR_INSUFFICIENT_BUFFER;
2461 else
2463 *pdwSize = size;
2464 memcpy(pUdpTable, table, size);
2466 HeapFree(GetProcessHeap(), 0, table);
2467 return ret;
2470 DWORD WINAPI GetUnicastIpAddressEntry(MIB_UNICASTIPADDRESS_ROW *row)
2472 IP_ADAPTER_ADDRESSES *aa, *ptr;
2473 ULONG size = 0;
2474 DWORD ret;
2476 TRACE("%p\n", row);
2478 if (!row)
2479 return ERROR_INVALID_PARAMETER;
2481 ret = GetAdaptersAddresses(row->Address.si_family, 0, NULL, NULL, &size);
2482 if (ret != ERROR_BUFFER_OVERFLOW)
2483 return ret;
2484 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, size)))
2485 return ERROR_OUTOFMEMORY;
2486 if ((ret = GetAdaptersAddresses(row->Address.si_family, 0, NULL, ptr, &size)))
2488 HeapFree(GetProcessHeap(), 0, ptr);
2489 return ret;
2492 ret = ERROR_FILE_NOT_FOUND;
2493 for (aa = ptr; aa; aa = aa->Next)
2495 IP_ADAPTER_UNICAST_ADDRESS *ua;
2497 if (aa->u.s.IfIndex != row->InterfaceIndex &&
2498 memcmp(&aa->Luid, &row->InterfaceLuid, sizeof(row->InterfaceLuid)))
2499 continue;
2500 ret = ERROR_NOT_FOUND;
2502 ua = aa->FirstUnicastAddress;
2503 while (ua)
2505 SOCKADDR_INET *uaaddr = (SOCKADDR_INET *)ua->Address.lpSockaddr;
2507 if ((row->Address.si_family == WS_AF_INET6 &&
2508 !memcmp(&row->Address.Ipv6.sin6_addr, &uaaddr->Ipv6.sin6_addr, sizeof(uaaddr->Ipv6.sin6_addr))) ||
2509 (row->Address.si_family == WS_AF_INET &&
2510 row->Address.Ipv4.sin_addr.S_un.S_addr == uaaddr->Ipv4.sin_addr.S_un.S_addr))
2512 memcpy(&row->InterfaceLuid, &aa->Luid, sizeof(aa->Luid));
2513 row->InterfaceIndex = aa->u.s.IfIndex;
2514 row->PrefixOrigin = ua->PrefixOrigin;
2515 row->SuffixOrigin = ua->SuffixOrigin;
2516 row->ValidLifetime = ua->ValidLifetime;
2517 row->PreferredLifetime = ua->PreferredLifetime;
2518 row->OnLinkPrefixLength = ua->OnLinkPrefixLength;
2519 row->SkipAsSource = 0;
2520 row->DadState = ua->DadState;
2521 if (row->Address.si_family == WS_AF_INET6)
2522 row->ScopeId.u.Value = row->Address.Ipv6.sin6_scope_id;
2523 else
2524 row->ScopeId.u.Value = 0;
2525 NtQuerySystemTime(&row->CreationTimeStamp);
2526 HeapFree(GetProcessHeap(), 0, ptr);
2527 return NO_ERROR;
2529 ua = ua->Next;
2532 HeapFree(GetProcessHeap(), 0, ptr);
2534 return ret;
2537 /******************************************************************
2538 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2540 * This is a Win98-only function to get information on "unidirectional"
2541 * adapters. Since this is pretty nonsensical in other contexts, it
2542 * never returns anything.
2544 * PARAMS
2545 * pIPIfInfo [Out] buffer for adapter infos
2546 * dwOutBufLen [Out] length of the output buffer
2548 * RETURNS
2549 * Success: NO_ERROR
2550 * Failure: error code from winerror.h
2552 * FIXME
2553 * Stub, returns ERROR_NOT_SUPPORTED.
2555 DWORD WINAPI GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo, PULONG dwOutBufLen)
2557 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo, dwOutBufLen);
2558 /* a unidirectional adapter?? not bloody likely! */
2559 return ERROR_NOT_SUPPORTED;
2563 /******************************************************************
2564 * IpReleaseAddress (IPHLPAPI.@)
2566 * Release an IP obtained through DHCP,
2568 * PARAMS
2569 * AdapterInfo [In] adapter to release IP address
2571 * RETURNS
2572 * Success: NO_ERROR
2573 * Failure: error code from winerror.h
2575 * NOTES
2576 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2577 * this function does nothing.
2579 * FIXME
2580 * Stub, returns ERROR_NOT_SUPPORTED.
2582 DWORD WINAPI IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2584 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2585 return ERROR_NOT_SUPPORTED;
2589 /******************************************************************
2590 * IpRenewAddress (IPHLPAPI.@)
2592 * Renew an IP obtained through DHCP.
2594 * PARAMS
2595 * AdapterInfo [In] adapter to renew IP address
2597 * RETURNS
2598 * Success: NO_ERROR
2599 * Failure: error code from winerror.h
2601 * NOTES
2602 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2603 * this function does nothing.
2605 * FIXME
2606 * Stub, returns ERROR_NOT_SUPPORTED.
2608 DWORD WINAPI IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo)
2610 FIXME("Stub AdapterInfo %p\n", AdapterInfo);
2611 return ERROR_NOT_SUPPORTED;
2615 /******************************************************************
2616 * NotifyAddrChange (IPHLPAPI.@)
2618 * Notify caller whenever the ip-interface map is changed.
2620 * PARAMS
2621 * Handle [Out] handle usable in asynchronous notification
2622 * overlapped [In] overlapped structure that notifies the caller
2624 * RETURNS
2625 * Success: NO_ERROR
2626 * Failure: error code from winerror.h
2628 * FIXME
2629 * Stub, returns ERROR_NOT_SUPPORTED.
2631 DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2633 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2634 if (Handle) *Handle = INVALID_HANDLE_VALUE;
2635 if (overlapped) ((IO_STATUS_BLOCK *) overlapped)->u.Status = STATUS_PENDING;
2636 return ERROR_IO_PENDING;
2640 /******************************************************************
2641 * NotifyIpInterfaceChange (IPHLPAPI.@)
2643 DWORD WINAPI NotifyIpInterfaceChange(ADDRESS_FAMILY family, PIPINTERFACE_CHANGE_CALLBACK callback,
2644 PVOID context, BOOLEAN init_notify, PHANDLE handle)
2646 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2647 family, callback, context, init_notify, handle);
2648 if (handle) *handle = NULL;
2649 return ERROR_NOT_SUPPORTED;
2653 /******************************************************************
2654 * NotifyRouteChange (IPHLPAPI.@)
2656 * Notify caller whenever the ip routing table is changed.
2658 * PARAMS
2659 * Handle [Out] handle usable in asynchronous notification
2660 * overlapped [In] overlapped structure that notifies the caller
2662 * RETURNS
2663 * Success: NO_ERROR
2664 * Failure: error code from winerror.h
2666 * FIXME
2667 * Stub, returns ERROR_NOT_SUPPORTED.
2669 DWORD WINAPI NotifyRouteChange(PHANDLE Handle, LPOVERLAPPED overlapped)
2671 FIXME("(Handle %p, overlapped %p): stub\n", Handle, overlapped);
2672 return ERROR_NOT_SUPPORTED;
2676 /******************************************************************
2677 * NotifyUnicastIpAddressChange (IPHLPAPI.@)
2679 DWORD WINAPI NotifyUnicastIpAddressChange(ADDRESS_FAMILY family, PUNICAST_IPADDRESS_CHANGE_CALLBACK callback,
2680 PVOID context, BOOLEAN init_notify, PHANDLE handle)
2682 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2683 family, callback, context, init_notify, handle);
2684 if (handle) *handle = NULL;
2685 return ERROR_NOT_SUPPORTED;
2688 /******************************************************************
2689 * SendARP (IPHLPAPI.@)
2691 * Send an ARP request.
2693 * PARAMS
2694 * DestIP [In] attempt to obtain this IP
2695 * SrcIP [In] optional sender IP address
2696 * pMacAddr [Out] buffer for the mac address
2697 * PhyAddrLen [In/Out] length of the output buffer
2699 * RETURNS
2700 * Success: NO_ERROR
2701 * Failure: error code from winerror.h
2703 * FIXME
2704 * Stub, returns ERROR_NOT_SUPPORTED.
2706 DWORD WINAPI SendARP(IPAddr DestIP, IPAddr SrcIP, PULONG pMacAddr, PULONG PhyAddrLen)
2708 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2709 DestIP, SrcIP, pMacAddr, PhyAddrLen);
2710 return ERROR_NOT_SUPPORTED;
2714 /******************************************************************
2715 * SetIfEntry (IPHLPAPI.@)
2717 * Set the administrative status of an interface.
2719 * PARAMS
2720 * pIfRow [In] dwAdminStatus member specifies the new status.
2722 * RETURNS
2723 * Success: NO_ERROR
2724 * Failure: error code from winerror.h
2726 * FIXME
2727 * Stub, returns ERROR_NOT_SUPPORTED.
2729 DWORD WINAPI SetIfEntry(PMIB_IFROW pIfRow)
2731 FIXME("(pIfRow %p): stub\n", pIfRow);
2732 /* this is supposed to set an interface administratively up or down.
2733 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2734 this sort of down is indistinguishable from other sorts of down (e.g. no
2735 link). */
2736 return ERROR_NOT_SUPPORTED;
2740 /******************************************************************
2741 * SetIpForwardEntry (IPHLPAPI.@)
2743 * Modify an existing route.
2745 * PARAMS
2746 * pRoute [In] route with the new information
2748 * RETURNS
2749 * Success: NO_ERROR
2750 * Failure: error code from winerror.h
2752 * FIXME
2753 * Stub, returns NO_ERROR.
2755 DWORD WINAPI SetIpForwardEntry(PMIB_IPFORWARDROW pRoute)
2757 FIXME("(pRoute %p): stub\n", pRoute);
2758 /* this is to add a route entry, how's it distinguishable from
2759 CreateIpForwardEntry?
2760 could use SIOCADDRT, not sure I want to */
2761 return 0;
2765 /******************************************************************
2766 * SetIpNetEntry (IPHLPAPI.@)
2768 * Modify an existing ARP entry.
2770 * PARAMS
2771 * pArpEntry [In] ARP entry with the new information
2773 * RETURNS
2774 * Success: NO_ERROR
2775 * Failure: error code from winerror.h
2777 * FIXME
2778 * Stub, returns NO_ERROR.
2780 DWORD WINAPI SetIpNetEntry(PMIB_IPNETROW pArpEntry)
2782 FIXME("(pArpEntry %p): stub\n", pArpEntry);
2783 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2784 return 0;
2788 /******************************************************************
2789 * SetIpStatistics (IPHLPAPI.@)
2791 * Toggle IP forwarding and det the default TTL value.
2793 * PARAMS
2794 * pIpStats [In] IP statistics with the new information
2796 * RETURNS
2797 * Success: NO_ERROR
2798 * Failure: error code from winerror.h
2800 * FIXME
2801 * Stub, returns NO_ERROR.
2803 DWORD WINAPI SetIpStatistics(PMIB_IPSTATS pIpStats)
2805 FIXME("(pIpStats %p): stub\n", pIpStats);
2806 return 0;
2810 /******************************************************************
2811 * SetIpTTL (IPHLPAPI.@)
2813 * Set the default TTL value.
2815 * PARAMS
2816 * nTTL [In] new TTL value
2818 * RETURNS
2819 * Success: NO_ERROR
2820 * Failure: error code from winerror.h
2822 * FIXME
2823 * Stub, returns NO_ERROR.
2825 DWORD WINAPI SetIpTTL(UINT nTTL)
2827 FIXME("(nTTL %d): stub\n", nTTL);
2828 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2829 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2830 return 0;
2834 /******************************************************************
2835 * SetTcpEntry (IPHLPAPI.@)
2837 * Set the state of a TCP connection.
2839 * PARAMS
2840 * pTcpRow [In] specifies connection with new state
2842 * RETURNS
2843 * Success: NO_ERROR
2844 * Failure: error code from winerror.h
2846 * FIXME
2847 * Stub, returns NO_ERROR.
2849 DWORD WINAPI SetTcpEntry(PMIB_TCPROW pTcpRow)
2851 FIXME("(pTcpRow %p): stub\n", pTcpRow);
2852 return 0;
2855 /******************************************************************
2856 * SetPerTcpConnectionEStats (IPHLPAPI.@)
2858 DWORD WINAPI SetPerTcpConnectionEStats(PMIB_TCPROW row, TCP_ESTATS_TYPE state, PBYTE rw,
2859 ULONG version, ULONG size, ULONG offset)
2861 FIXME("(row %p, state %d, rw %p, version %u, size %u, offset %u): stub\n",
2862 row, state, rw, version, size, offset);
2863 return ERROR_NOT_SUPPORTED;
2867 /******************************************************************
2868 * UnenableRouter (IPHLPAPI.@)
2870 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2871 * if it reaches zero.
2873 * PARAMS
2874 * pOverlapped [In/Out] should be the same as in EnableRouter()
2875 * lpdwEnableCount [Out] optional, receives reference count
2877 * RETURNS
2878 * Success: NO_ERROR
2879 * Failure: error code from winerror.h
2881 * FIXME
2882 * Stub, returns ERROR_NOT_SUPPORTED.
2884 DWORD WINAPI UnenableRouter(OVERLAPPED * pOverlapped, LPDWORD lpdwEnableCount)
2886 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped,
2887 lpdwEnableCount);
2888 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2889 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2891 return ERROR_NOT_SUPPORTED;
2894 /******************************************************************
2895 * PfCreateInterface (IPHLPAPI.@)
2897 DWORD WINAPI PfCreateInterface(DWORD dwName, PFFORWARD_ACTION inAction, PFFORWARD_ACTION outAction,
2898 BOOL bUseLog, BOOL bMustBeUnique, INTERFACE_HANDLE *ppInterface)
2900 FIXME("(%d %d %d %x %x %p) stub\n", dwName, inAction, outAction, bUseLog, bMustBeUnique, ppInterface);
2901 return ERROR_CALL_NOT_IMPLEMENTED;
2904 /******************************************************************
2905 * PfUnBindInterface (IPHLPAPI.@)
2907 DWORD WINAPI PfUnBindInterface(INTERFACE_HANDLE interface)
2909 FIXME("(%p) stub\n", interface);
2910 return ERROR_CALL_NOT_IMPLEMENTED;
2913 /******************************************************************
2914 * PfDeleteInterface(IPHLPAPI.@)
2916 DWORD WINAPI PfDeleteInterface(INTERFACE_HANDLE interface)
2918 FIXME("(%p) stub\n", interface);
2919 return ERROR_CALL_NOT_IMPLEMENTED;
2922 /******************************************************************
2923 * PfBindInterfaceToIPAddress(IPHLPAPI.@)
2925 DWORD WINAPI PfBindInterfaceToIPAddress(INTERFACE_HANDLE interface, PFADDRESSTYPE type, PBYTE ip)
2927 FIXME("(%p %d %p) stub\n", interface, type, ip);
2928 return ERROR_CALL_NOT_IMPLEMENTED;
2931 /******************************************************************
2932 * GetTcpTable2 (IPHLPAPI.@)
2934 ULONG WINAPI GetTcpTable2(PMIB_TCPTABLE2 table, PULONG size, BOOL order)
2936 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table, size, order);
2937 return ERROR_NOT_SUPPORTED;
2940 /******************************************************************
2941 * GetTcp6Table (IPHLPAPI.@)
2943 ULONG WINAPI GetTcp6Table(PMIB_TCP6TABLE table, PULONG size, BOOL order)
2945 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table, size, order);
2946 return ERROR_NOT_SUPPORTED;
2949 /******************************************************************
2950 * GetTcp6Table2 (IPHLPAPI.@)
2952 ULONG WINAPI GetTcp6Table2(PMIB_TCP6TABLE2 table, PULONG size, BOOL order)
2954 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table, size, order);
2955 return ERROR_NOT_SUPPORTED;
2958 /******************************************************************
2959 * ConvertInterfaceGuidToLuid (IPHLPAPI.@)
2961 DWORD WINAPI ConvertInterfaceGuidToLuid(const GUID *guid, NET_LUID *luid)
2963 DWORD ret;
2964 MIB_IFROW row;
2966 TRACE("(%s %p)\n", debugstr_guid(guid), luid);
2968 if (!guid || !luid) return ERROR_INVALID_PARAMETER;
2970 row.dwIndex = guid->Data1;
2971 if ((ret = GetIfEntry( &row ))) return ret;
2973 luid->Info.Reserved = 0;
2974 luid->Info.NetLuidIndex = guid->Data1;
2975 luid->Info.IfType = row.dwType;
2976 return NO_ERROR;
2979 /******************************************************************
2980 * ConvertInterfaceIndexToLuid (IPHLPAPI.@)
2982 DWORD WINAPI ConvertInterfaceIndexToLuid(NET_IFINDEX index, NET_LUID *luid)
2984 MIB_IFROW row;
2986 TRACE("(%u %p)\n", index, luid);
2988 if (!luid) return ERROR_INVALID_PARAMETER;
2989 memset( luid, 0, sizeof(*luid) );
2991 row.dwIndex = index;
2992 if (GetIfEntry( &row )) return ERROR_FILE_NOT_FOUND;
2994 luid->Info.Reserved = 0;
2995 luid->Info.NetLuidIndex = index;
2996 luid->Info.IfType = row.dwType;
2997 return NO_ERROR;
3000 /******************************************************************
3001 * ConvertInterfaceLuidToGuid (IPHLPAPI.@)
3003 DWORD WINAPI ConvertInterfaceLuidToGuid(const NET_LUID *luid, GUID *guid)
3005 DWORD ret;
3006 MIB_IFROW row;
3008 TRACE("(%p %p)\n", luid, guid);
3010 if (!luid || !guid) return ERROR_INVALID_PARAMETER;
3012 row.dwIndex = luid->Info.NetLuidIndex;
3013 if ((ret = GetIfEntry( &row ))) return ret;
3015 guid->Data1 = luid->Info.NetLuidIndex;
3016 return NO_ERROR;
3019 /******************************************************************
3020 * ConvertInterfaceLuidToIndex (IPHLPAPI.@)
3022 DWORD WINAPI ConvertInterfaceLuidToIndex(const NET_LUID *luid, NET_IFINDEX *index)
3024 DWORD ret;
3025 MIB_IFROW row;
3027 TRACE("(%p %p)\n", luid, index);
3029 if (!luid || !index) return ERROR_INVALID_PARAMETER;
3031 row.dwIndex = luid->Info.NetLuidIndex;
3032 if ((ret = GetIfEntry( &row ))) return ret;
3034 *index = luid->Info.NetLuidIndex;
3035 return NO_ERROR;
3038 /******************************************************************
3039 * ConvertInterfaceLuidToNameA (IPHLPAPI.@)
3041 DWORD WINAPI ConvertInterfaceLuidToNameA(const NET_LUID *luid, char *name, SIZE_T len)
3043 DWORD ret;
3044 MIB_IFROW row;
3046 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
3048 if (!luid) return ERROR_INVALID_PARAMETER;
3050 row.dwIndex = luid->Info.NetLuidIndex;
3051 if ((ret = GetIfEntry( &row ))) return ret;
3053 if (!name || len < WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, NULL, 0, NULL, NULL ))
3054 return ERROR_NOT_ENOUGH_MEMORY;
3056 WideCharToMultiByte( CP_UNIXCP, 0, row.wszName, -1, name, len, NULL, NULL );
3057 return NO_ERROR;
3060 /******************************************************************
3061 * ConvertInterfaceLuidToNameW (IPHLPAPI.@)
3063 DWORD WINAPI ConvertInterfaceLuidToNameW(const NET_LUID *luid, WCHAR *name, SIZE_T len)
3065 DWORD ret;
3066 MIB_IFROW row;
3068 TRACE("(%p %p %u)\n", luid, name, (DWORD)len);
3070 if (!luid || !name) return ERROR_INVALID_PARAMETER;
3072 row.dwIndex = luid->Info.NetLuidIndex;
3073 if ((ret = GetIfEntry( &row ))) return ret;
3075 if (len < strlenW( row.wszName ) + 1) return ERROR_NOT_ENOUGH_MEMORY;
3076 strcpyW( name, row.wszName );
3077 return NO_ERROR;
3080 /******************************************************************
3081 * ConvertInterfaceNameToLuidA (IPHLPAPI.@)
3083 DWORD WINAPI ConvertInterfaceNameToLuidA(const char *name, NET_LUID *luid)
3085 DWORD ret;
3086 IF_INDEX index;
3087 MIB_IFROW row;
3089 TRACE("(%s %p)\n", debugstr_a(name), luid);
3091 if ((ret = getInterfaceIndexByName( name, &index ))) return ERROR_INVALID_NAME;
3092 if (!luid) return ERROR_INVALID_PARAMETER;
3094 row.dwIndex = index;
3095 if ((ret = GetIfEntry( &row ))) return ret;
3097 luid->Info.Reserved = 0;
3098 luid->Info.NetLuidIndex = index;
3099 luid->Info.IfType = row.dwType;
3100 return NO_ERROR;
3103 /******************************************************************
3104 * ConvertInterfaceNameToLuidW (IPHLPAPI.@)
3106 DWORD WINAPI ConvertInterfaceNameToLuidW(const WCHAR *name, NET_LUID *luid)
3108 DWORD ret;
3109 IF_INDEX index;
3110 MIB_IFROW row;
3111 char nameA[IF_MAX_STRING_SIZE + 1];
3113 TRACE("(%s %p)\n", debugstr_w(name), luid);
3115 if (!luid) return ERROR_INVALID_PARAMETER;
3116 memset( luid, 0, sizeof(*luid) );
3118 if (!WideCharToMultiByte( CP_UNIXCP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL ))
3119 return ERROR_INVALID_NAME;
3121 if ((ret = getInterfaceIndexByName( nameA, &index ))) return ret;
3123 row.dwIndex = index;
3124 if ((ret = GetIfEntry( &row ))) return ret;
3126 luid->Info.Reserved = 0;
3127 luid->Info.NetLuidIndex = index;
3128 luid->Info.IfType = row.dwType;
3129 return NO_ERROR;