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
26 #include <sys/types.h>
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
56 #include "tcpestats.h"
58 #include "wine/debug.h"
59 #include "wine/unicode.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
64 #define IF_NAMESIZE 16
68 #define INADDR_NONE ~0UL
71 /******************************************************************
72 * AddIPAddress (IPHLPAPI.@)
74 * Add an IP address to an adapter.
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
85 * Failure: error code from winerror.h
88 * Stub. Currently returns ERROR_NOT_SUPPORTED.
90 DWORD WINAPI
AddIPAddress(IPAddr Address
, IPMask IpMask
, DWORD IfIndex
, PULONG NTEContext
, PULONG NTEInstance
)
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.
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
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
)
119 TRACE("ppIfTable %p, bOrder %d, heap %p, flags 0x%08x\n", ppIfTable
,
120 bOrder
, heap
, flags
);
122 ret
= ERROR_INVALID_PARAMETER
;
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
);
137 static int IpAddrTableNumericSorter(const void *a
, const void *b
)
142 ret
= ((const MIB_IPADDRROW
*)a
)->dwAddr
- ((const MIB_IPADDRROW
*)b
)->dwAddr
;
146 static int IpAddrTableLoopbackSorter(const void *a
, const void *b
)
148 const MIB_IPADDRROW
*left
= a
, *right
= b
;
151 if (isIfIndexLoopback(left
->dwIndex
))
153 else if (isIfIndexLoopback(right
->dwIndex
))
159 /******************************************************************
160 * AllocateAndGetIpAddrTableFromStack (IPHLPAPI.@)
162 * Get interface-to-IP address mapping table.
163 * Like GetIpAddrTable(), but allocate the returned table from heap.
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
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
)
181 TRACE("ppIpAddrTable %p, bOrder %d, heap %p, flags 0x%08x\n",
182 ppIpAddrTable
, bOrder
, heap
, flags
);
183 ret
= getIPAddrTable(ppIpAddrTable
, heap
, flags
);
185 qsort((*ppIpAddrTable
)->table
, (*ppIpAddrTable
)->dwNumEntries
,
186 sizeof(MIB_IPADDRROW
), IpAddrTableNumericSorter
);
187 TRACE("returning %d\n", ret
);
192 /******************************************************************
193 * CancelIPChangeNotify (IPHLPAPI.@)
195 * Cancel a previous notification created by NotifyAddrChange or
199 * overlapped [In] overlapped structure that notifies the caller
206 * Stub, returns FALSE.
208 BOOL WINAPI
CancelIPChangeNotify(LPOVERLAPPED overlapped
)
210 FIXME("(overlapped %p): stub\n", overlapped
);
215 /******************************************************************
216 * CancelMibChangeNotify2 (IPHLPAPI.@)
218 DWORD WINAPI
CancelMibChangeNotify2(HANDLE handle
)
220 FIXME("(handle %p): stub\n", handle
);
225 /******************************************************************
226 * CreateIpForwardEntry (IPHLPAPI.@)
228 * Create a route in the local computer's IP table.
231 * pRoute [In] new route information
235 * Failure: error code from winerror.h
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 */
248 /******************************************************************
249 * CreateIpNetEntry (IPHLPAPI.@)
251 * Create entry in the ARP table.
254 * pArpEntry [In] new ARP entry
258 * Failure: error code from winerror.h
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 */
271 /******************************************************************
272 * CreateProxyArpEntry (IPHLPAPI.@)
274 * Create a Proxy ARP (PARP) entry for an IP address.
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
283 * Failure: error code from winerror.h
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
;
300 BOOL in_zero
= FALSE
;
302 for (i
= 0; i
< 7; i
++)
304 if (!addr
->u
.Word
[i
])
316 p
+= sprintf(p
, "%x:", ntohs(addr
->u
.Word
[i
]));
320 sprintf(p
, "%x", ntohs(addr
->u
.Word
[7]));
324 static BOOL
map_address_6to4( const SOCKADDR_IN6
*addr6
, SOCKADDR_IN
*addr4
)
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
) );
343 static BOOL
find_src_address( MIB_IPADDRTABLE
*table
, const SOCKADDR_IN
*dst
, SOCKADDR_IN6
*src
)
345 MIB_IPFORWARDROW row
;
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
;
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;
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
,
378 SOCKADDR_IN6_PAIR
*pairs
;
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
);
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
))
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
) );
425 *pair_count
= dst_count
;
427 HeapFree( GetProcessHeap(), 0, table
);
432 /******************************************************************
433 * DeleteIPAddress (IPHLPAPI.@)
435 * Delete an IP address added with AddIPAddress().
438 * NTEContext [In] NTE context from AddIPAddress();
442 * Failure: error code from winerror.h
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.@)
460 * pRoute [In] route to delete
464 * Failure: error code from winerror.h
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 */
477 /******************************************************************
478 * DeleteIpNetEntry (IPHLPAPI.@)
480 * Delete an ARP entry.
483 * pArpEntry [In] ARP entry to delete
487 * Failure: error code from winerror.h
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 */
500 /******************************************************************
501 * DeleteProxyArpEntry (IPHLPAPI.@)
503 * Delete a Proxy ARP entry.
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
512 * Failure: error code from winerror.h
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.
532 * pOverlapped [In/Out] hEvent member should contain a valid handle.
535 * Success: ERROR_IO_PENDING
536 * Failure: error code from winerror.h
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
557 * dwIfIndex [In] interface index
561 * Failure: error code from winerror.h
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
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.
594 * AdapterName [In] unicode string with the adapter name
595 * IfIndex [Out] returns found interface index
599 * Failure: error code from winerror.h
601 DWORD WINAPI
GetAdapterIndex(LPWSTR AdapterName
, PULONG IfIndex
)
603 char adapterName
[MAX_ADAPTER_NAME
];
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
);
619 /******************************************************************
620 * GetAdaptersInfo (IPHLPAPI.@)
622 * Get information about adapters.
625 * pAdapterInfo [Out] buffer for adapter infos
626 * pOutBufLen [In] length of output buffer
630 * Failure: error code from winerror.h
632 DWORD WINAPI
GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo
, PULONG pOutBufLen
)
636 TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo
, pOutBufLen
);
638 ret
= ERROR_INVALID_PARAMETER
;
640 DWORD numNonLoopbackInterfaces
= get_interface_indices( TRUE
, NULL
);
642 if (numNonLoopbackInterfaces
> 0) {
643 DWORD numIPAddresses
= getNumIPAddresses();
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
) {
655 ret
= ERROR_BUFFER_OVERFLOW
;
658 InterfaceIndexTable
*table
= NULL
;
659 PMIB_IPADDRTABLE ipAddrTable
= NULL
;
660 PMIB_IPFORWARDTABLE routeTable
= NULL
;
662 ret
= getIPAddrTable(&ipAddrTable
, GetProcessHeap(), 0);
664 ret
= AllocateAndGetIpForwardTableFromStack(&routeTable
, FALSE
, GetProcessHeap(), 0);
666 get_interface_indices( TRUE
, &table
);
668 size
= sizeof(IP_ADAPTER_INFO
) * table
->numIndexes
;
669 size
+= ipAddrTable
->dwNumEntries
* sizeof(IP_ADDR_STRING
);
670 if (*pOutBufLen
< size
) {
672 ret
= ERROR_INSUFFICIENT_BUFFER
;
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
);
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
)
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
)
702 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
703 PIP_ADAPTER_INFO ptr
= &pAdapterInfo
[ndx
];
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
) {
718 toIPAddressString(ipAddrTable
->table
[i
].dwAddr
,
719 ptr
->IpAddressList
.IpAddress
.String
);
720 toIPAddressString(ipAddrTable
->table
[i
].dwMask
,
721 ptr
->IpAddressList
.IpMask
.String
);
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
);
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. */
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
);
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];
765 ptr
->DhcpEnabled
= TRUE
;
769 HeapFree(GetProcessHeap(), 0, table
);
772 ret
= ERROR_OUTOFMEMORY
;
773 HeapFree(GetProcessHeap(), 0, routeTable
);
774 HeapFree(GetProcessHeap(), 0, ipAddrTable
);
780 TRACE("returning %d\n", ret
);
784 static DWORD
typeFromMibType(DWORD 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
)
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
)
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
;
837 HeapFree(GetProcessHeap(), 0, at
);
838 return ERROR_SUCCESS
;
841 static char *debugstr_ipv4(const in_addr_t
*in_addr
, char *buf
)
846 for (addrp
= (const BYTE
*)in_addr
;
847 addrp
- (const BYTE
*)in_addr
< sizeof(*in_addr
);
850 if (addrp
== (const BYTE
*)in_addr
+ sizeof(*in_addr
) - 1)
851 sprintf(p
, "%d", *addrp
);
853 p
+= sprintf(p
, "%d.", *addrp
);
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
)
871 static DWORD
mask_v4_to_prefix(DWORD m
)
873 #ifdef HAVE___BUILTIN_POPCOUNT
874 return __builtin_popcount(m
);
876 m
-= m
>> 1 & 0x55555555;
877 m
= (m
& 0x33333333) + (m
>> 2 & 0x33333333);
878 return ((m
+ (m
>> 4)) & 0x0f0f0f0f) * 0x01010101 >> 24;
882 static DWORD
mask_v6_to_prefix(SOCKET_ADDRESS
*m
)
884 const IN6_ADDR
*mask
= &((struct WS_sockaddr_in6
*)m
->lpSockaddr
)->sin6_addr
;
887 for (i
= 0; i
< 8; i
++)
888 ret
+= mask_v4_to_prefix(mask
->u
.Word
[i
]);
892 static PMIB_IPFORWARDROW
findIPv4Gateway(DWORD index
,
893 PMIB_IPFORWARDTABLE routeTable
)
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
];
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
;
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
);
967 FIXME("address family %u unsupported\n", family
);
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
);
980 total_size
= sizeof(IP_ADAPTER_ADDRESSES
);
981 total_size
+= 39; /* "{00000000-0000-0000-0000-000000000000}" */
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
;
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 sprintf(ptr
, "{%08x-0000-0000-0000-000000000000}", index
);
1013 aa
->AdapterName
= ptr
;
1016 getInterfaceNameByIndex(index
, name
);
1017 if (!(flags
& GAA_FLAG_SKIP_FRIENDLY_NAME
))
1019 aa
->FriendlyName
= (WCHAR
*)ptr
;
1020 for (src
= name
, dst
= (WCHAR
*)ptr
; *src
; src
++, dst
++)
1025 aa
->Description
= (WCHAR
*)ptr
;
1026 for (src
= name
, dst
= (WCHAR
*)ptr
; *src
; src
++, dst
++)
1031 TRACE("%s: %d IPv4 addresses, %d IPv6 addresses:\n", name
, num_v4addrs
,
1034 buflen
= MAX_INTERFACE_PHYSADDR
;
1035 getInterfacePhysicalByIndex(index
, &buflen
, aa
->PhysicalAddress
, &type
);
1036 aa
->PhysicalAddressLength
= buflen
;
1037 aa
->IfType
= typeFromMibType(type
);
1038 aa
->ConnectionType
= connectionTypeFromMibType(type
);
1039 aa
->Luid
.Info
.NetLuidIndex
= index
;
1040 aa
->Luid
.Info
.IfType
= aa
->IfType
;
1042 if (num_v4_gateways
)
1044 PMIB_IPFORWARDROW adapterRow
;
1046 if ((adapterRow
= findIPv4Gateway(index
, routeTable
)))
1048 PIP_ADAPTER_GATEWAY_ADDRESS gw
;
1051 gw
= (PIP_ADAPTER_GATEWAY_ADDRESS
)ptr
;
1052 aa
->FirstGatewayAddress
= gw
;
1054 gw
->u
.s
.Length
= sizeof(IP_ADAPTER_GATEWAY_ADDRESS
);
1055 ptr
+= sizeof(IP_ADAPTER_GATEWAY_ADDRESS
);
1056 sin
= (PSOCKADDR_IN
)ptr
;
1057 sin
->sin_family
= WS_AF_INET
;
1059 memcpy(&sin
->sin_addr
, &adapterRow
->dwForwardNextHop
,
1061 gw
->Address
.lpSockaddr
= (LPSOCKADDR
)sin
;
1062 gw
->Address
.iSockaddrLength
= sizeof(SOCKADDR_IN
);
1064 ptr
+= sizeof(SOCKADDR_IN
);
1067 if (num_v4addrs
&& !(flags
& GAA_FLAG_SKIP_UNICAST
))
1069 IP_ADAPTER_UNICAST_ADDRESS
*ua
;
1070 struct WS_sockaddr_in
*sa
;
1071 aa
->u1
.s1
.Ipv4Enabled
= TRUE
;
1072 ua
= aa
->FirstUnicastAddress
= (IP_ADAPTER_UNICAST_ADDRESS
*)ptr
;
1073 for (i
= 0; i
< num_v4addrs
; i
++)
1077 memset(ua
, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS
));
1078 ua
->u
.s
.Length
= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
1079 ua
->Address
.iSockaddrLength
= sizeof(struct sockaddr_in
);
1080 ua
->Address
.lpSockaddr
= (SOCKADDR
*)((char *)ua
+ ua
->u
.s
.Length
);
1082 sa
= (struct WS_sockaddr_in
*)ua
->Address
.lpSockaddr
;
1083 sa
->sin_family
= WS_AF_INET
;
1084 sa
->sin_addr
.S_un
.S_addr
= v4addrs
[i
];
1086 TRACE("IPv4 %d/%d: %s\n", i
+ 1, num_v4addrs
,
1087 debugstr_ipv4(&sa
->sin_addr
.S_un
.S_addr
, addr_buf
));
1088 fill_unicast_addr_data(aa
, ua
);
1090 ua
->OnLinkPrefixLength
= mask_v4_to_prefix(v4masks
[i
]);
1092 ptr
+= ua
->u
.s
.Length
+ ua
->Address
.iSockaddrLength
;
1093 if (i
< num_v4addrs
- 1)
1095 ua
->Next
= (IP_ADAPTER_UNICAST_ADDRESS
*)ptr
;
1100 if (num_v6addrs
&& !(flags
& GAA_FLAG_SKIP_UNICAST
))
1102 IP_ADAPTER_UNICAST_ADDRESS
*ua
;
1103 struct WS_sockaddr_in6
*sa
;
1105 aa
->u1
.s1
.Ipv6Enabled
= TRUE
;
1106 if (aa
->FirstUnicastAddress
)
1108 for (ua
= aa
->FirstUnicastAddress
; ua
->Next
; ua
= ua
->Next
)
1110 ua
->Next
= (IP_ADAPTER_UNICAST_ADDRESS
*)ptr
;
1111 ua
= (IP_ADAPTER_UNICAST_ADDRESS
*)ptr
;
1114 ua
= aa
->FirstUnicastAddress
= (IP_ADAPTER_UNICAST_ADDRESS
*)ptr
;
1115 for (i
= 0; i
< num_v6addrs
; i
++)
1119 memset(ua
, 0, sizeof(IP_ADAPTER_UNICAST_ADDRESS
));
1120 ua
->u
.s
.Length
= sizeof(IP_ADAPTER_UNICAST_ADDRESS
);
1121 ua
->Address
.iSockaddrLength
= v6addrs
[i
].iSockaddrLength
;
1122 ua
->Address
.lpSockaddr
= (SOCKADDR
*)((char *)ua
+ ua
->u
.s
.Length
);
1124 sa
= (struct WS_sockaddr_in6
*)ua
->Address
.lpSockaddr
;
1125 memcpy(sa
, v6addrs
[i
].lpSockaddr
, sizeof(*sa
));
1126 TRACE("IPv6 %d/%d: %s\n", i
+ 1, num_v6addrs
,
1127 debugstr_ipv6(sa
, addr_buf
));
1128 fill_unicast_addr_data(aa
, ua
);
1130 ua
->OnLinkPrefixLength
= mask_v6_to_prefix(&v6masks
[i
]);
1132 ptr
+= ua
->u
.s
.Length
+ ua
->Address
.iSockaddrLength
;
1133 if (i
< num_v6addrs
- 1)
1135 ua
->Next
= (IP_ADAPTER_UNICAST_ADDRESS
*)ptr
;
1140 if (num_v4addrs
&& (flags
& GAA_FLAG_INCLUDE_PREFIX
))
1142 IP_ADAPTER_PREFIX
*prefix
;
1144 prefix
= aa
->FirstPrefix
= (IP_ADAPTER_PREFIX
*)ptr
;
1145 for (i
= 0; i
< num_v4addrs
; i
++)
1148 struct WS_sockaddr_in
*sa
;
1150 prefix
->u
.s
.Length
= sizeof(*prefix
);
1151 prefix
->u
.s
.Flags
= 0;
1152 prefix
->Next
= NULL
;
1153 prefix
->Address
.iSockaddrLength
= sizeof(struct sockaddr_in
);
1154 prefix
->Address
.lpSockaddr
= (SOCKADDR
*)((char *)prefix
+ prefix
->u
.s
.Length
);
1156 sa
= (struct WS_sockaddr_in
*)prefix
->Address
.lpSockaddr
;
1157 sa
->sin_family
= WS_AF_INET
;
1158 sa
->sin_addr
.S_un
.S_addr
= v4addrs
[i
] & v4masks
[i
];
1161 prefix
->PrefixLength
= mask_v4_to_prefix(v4masks
[i
]);
1163 TRACE("IPv4 network: %s/%u\n",
1164 debugstr_ipv4((const in_addr_t
*)&sa
->sin_addr
.S_un
.S_addr
, addr_buf
),
1165 prefix
->PrefixLength
);
1167 ptr
+= prefix
->u
.s
.Length
+ prefix
->Address
.iSockaddrLength
;
1168 if (i
< num_v4addrs
- 1)
1170 prefix
->Next
= (IP_ADAPTER_PREFIX
*)ptr
;
1171 prefix
= prefix
->Next
;
1175 if (num_v6addrs
&& (flags
& GAA_FLAG_INCLUDE_PREFIX
))
1177 IP_ADAPTER_PREFIX
*prefix
;
1179 if (aa
->FirstPrefix
)
1181 for (prefix
= aa
->FirstPrefix
; prefix
->Next
; prefix
= prefix
->Next
)
1183 prefix
->Next
= (IP_ADAPTER_PREFIX
*)ptr
;
1184 prefix
= (IP_ADAPTER_PREFIX
*)ptr
;
1187 prefix
= aa
->FirstPrefix
= (IP_ADAPTER_PREFIX
*)ptr
;
1188 for (i
= 0; i
< num_v6addrs
; i
++)
1191 struct WS_sockaddr_in6
*sa
;
1192 const IN6_ADDR
*addr
, *mask
;
1194 prefix
->u
.s
.Length
= sizeof(*prefix
);
1195 prefix
->u
.s
.Flags
= 0;
1196 prefix
->Next
= NULL
;
1197 prefix
->Address
.iSockaddrLength
= sizeof(struct sockaddr_in6
);
1198 prefix
->Address
.lpSockaddr
= (SOCKADDR
*)((char *)prefix
+ prefix
->u
.s
.Length
);
1200 sa
= (struct WS_sockaddr_in6
*)prefix
->Address
.lpSockaddr
;
1201 sa
->sin6_family
= WS_AF_INET6
;
1203 sa
->sin6_flowinfo
= 0;
1204 addr
= &((struct WS_sockaddr_in6
*)v6addrs
[i
].lpSockaddr
)->sin6_addr
;
1205 mask
= &((struct WS_sockaddr_in6
*)v6masks
[i
].lpSockaddr
)->sin6_addr
;
1206 for (j
= 0; j
< 8; j
++) sa
->sin6_addr
.u
.Word
[j
] = addr
->u
.Word
[j
] & mask
->u
.Word
[j
];
1207 sa
->sin6_scope_id
= 0;
1209 prefix
->PrefixLength
= mask_v6_to_prefix(&v6masks
[i
]);
1211 TRACE("IPv6 network: %s/%u\n", debugstr_ipv6(sa
, addr_buf
), prefix
->PrefixLength
);
1213 ptr
+= prefix
->u
.s
.Length
+ prefix
->Address
.iSockaddrLength
;
1214 if (i
< num_v6addrs
- 1)
1216 prefix
->Next
= (IP_ADAPTER_PREFIX
*)ptr
;
1217 prefix
= prefix
->Next
;
1222 getInterfaceMtuByName(name
, &aa
->Mtu
);
1224 getInterfaceStatusByName(name
, &status
);
1225 if (status
== MIB_IF_OPER_STATUS_OPERATIONAL
) aa
->OperStatus
= IfOperStatusUp
;
1226 else if (status
== MIB_IF_OPER_STATUS_NON_OPERATIONAL
) aa
->OperStatus
= IfOperStatusDown
;
1227 else aa
->OperStatus
= IfOperStatusUnknown
;
1230 HeapFree(GetProcessHeap(), 0, routeTable
);
1231 HeapFree(GetProcessHeap(), 0, v6addrs
);
1232 HeapFree(GetProcessHeap(), 0, v6masks
);
1233 HeapFree(GetProcessHeap(), 0, v4addrs
);
1234 HeapFree(GetProcessHeap(), 0, v4masks
);
1235 return ERROR_SUCCESS
;
1238 static void sockaddr_in_to_WS_storage( SOCKADDR_STORAGE
*dst
, const struct sockaddr_in
*src
)
1240 SOCKADDR_IN
*s
= (SOCKADDR_IN
*)dst
;
1242 s
->sin_family
= WS_AF_INET
;
1243 s
->sin_port
= src
->sin_port
;
1244 memcpy( &s
->sin_addr
, &src
->sin_addr
, sizeof(IN_ADDR
) );
1245 memset( (char *)s
+ FIELD_OFFSET( SOCKADDR_IN
, sin_zero
), 0,
1246 sizeof(SOCKADDR_STORAGE
) - FIELD_OFFSET( SOCKADDR_IN
, sin_zero
) );
1249 static void sockaddr_in6_to_WS_storage( SOCKADDR_STORAGE
*dst
, const struct sockaddr_in6
*src
)
1251 SOCKADDR_IN6
*s
= (SOCKADDR_IN6
*)dst
;
1253 s
->sin6_family
= WS_AF_INET6
;
1254 s
->sin6_port
= src
->sin6_port
;
1255 s
->sin6_flowinfo
= src
->sin6_flowinfo
;
1256 memcpy( &s
->sin6_addr
, &src
->sin6_addr
, sizeof(IN6_ADDR
) );
1257 s
->sin6_scope_id
= src
->sin6_scope_id
;
1258 memset( (char *)s
+ sizeof(SOCKADDR_IN6
), 0,
1259 sizeof(SOCKADDR_STORAGE
) - sizeof(SOCKADDR_IN6
) );
1262 #ifdef HAVE_STRUCT___RES_STATE
1263 /* call res_init() just once because of a bug in Mac OS X 10.4 */
1264 /* Call once per thread on systems that have per-thread _res. */
1266 static CRITICAL_SECTION res_init_cs
;
1267 static CRITICAL_SECTION_DEBUG res_init_cs_debug
= {
1269 { &res_init_cs_debug
.ProcessLocksList
, &res_init_cs_debug
.ProcessLocksList
},
1270 0, 0, { (DWORD_PTR
)(__FILE__
": res_init_cs") }
1272 static CRITICAL_SECTION res_init_cs
= { &res_init_cs_debug
, -1, 0, 0, 0, 0 };
1274 static void initialise_resolver(void)
1276 EnterCriticalSection(&res_init_cs
);
1277 if ((_res
.options
& RES_INIT
) == 0)
1279 LeaveCriticalSection(&res_init_cs
);
1282 static int get_dns_servers( SOCKADDR_STORAGE
*servers
, int num
, BOOL ip4_only
)
1284 int i
, ip6_count
= 0;
1285 SOCKADDR_STORAGE
*addr
;
1287 initialise_resolver();
1289 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1290 ip6_count
= _res
._u
._ext
.nscount6
;
1293 if (!servers
|| !num
)
1296 if (ip4_only
) num
-= ip6_count
;
1300 for (i
= 0, addr
= servers
; addr
< (servers
+ num
) && i
< _res
.nscount
; i
++)
1302 #ifdef HAVE_STRUCT___RES_STATE__U__EXT_NSCOUNT6
1303 if (_res
._u
._ext
.nsaddrs
[i
] && _res
._u
._ext
.nsaddrs
[i
]->sin6_family
== AF_INET6
)
1305 if (ip4_only
) continue;
1306 sockaddr_in6_to_WS_storage( addr
, _res
._u
._ext
.nsaddrs
[i
] );
1311 sockaddr_in_to_WS_storage( addr
, _res
.nsaddr_list
+ i
);
1315 return addr
- servers
;
1317 #elif defined(HAVE___RES_GET_STATE) && defined(HAVE___RES_GETSERVERS)
1319 static int get_dns_servers( SOCKADDR_STORAGE
*servers
, int num
, BOOL ip4_only
)
1321 extern struct res_state
*__res_get_state( void );
1322 extern int __res_getservers( struct res_state
*, struct sockaddr_storage
*, int );
1323 struct res_state
*state
= __res_get_state();
1324 int i
, found
= 0, total
= __res_getservers( state
, NULL
, 0 );
1325 SOCKADDR_STORAGE
*addr
= servers
;
1326 struct sockaddr_storage
*buf
;
1328 if ((!servers
|| !num
) && !ip4_only
) return total
;
1330 buf
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(struct sockaddr_storage
) );
1331 total
= __res_getservers( state
, buf
, total
);
1333 for (i
= 0; i
< total
; i
++)
1335 if (buf
[i
].ss_family
== AF_INET6
&& ip4_only
) continue;
1336 if (buf
[i
].ss_family
!= AF_INET
&& buf
[i
].ss_family
!= AF_INET6
) continue;
1339 if (!servers
|| !num
) continue;
1341 if (buf
[i
].ss_family
== AF_INET6
)
1343 sockaddr_in6_to_WS_storage( addr
, (struct sockaddr_in6
*)(buf
+ i
) );
1347 sockaddr_in_to_WS_storage( addr
, (struct sockaddr_in
*)(buf
+ i
) );
1349 if (++addr
>= servers
+ num
) break;
1352 HeapFree( GetProcessHeap(), 0, buf
);
1357 static int get_dns_servers( SOCKADDR_STORAGE
*servers
, int num
, BOOL ip4_only
)
1359 FIXME("Unimplemented on this system\n");
1364 static ULONG
get_dns_server_addresses(PIP_ADAPTER_DNS_SERVER_ADDRESS address
, ULONG
*len
)
1366 int num
= get_dns_servers( NULL
, 0, FALSE
);
1369 size
= num
* (sizeof(IP_ADAPTER_DNS_SERVER_ADDRESS
) + sizeof(SOCKADDR_STORAGE
));
1370 if (!address
|| *len
< size
)
1373 return ERROR_BUFFER_OVERFLOW
;
1378 PIP_ADAPTER_DNS_SERVER_ADDRESS addr
= address
;
1379 SOCKADDR_STORAGE
*sock_addrs
= (SOCKADDR_STORAGE
*)(address
+ num
);
1382 get_dns_servers( sock_addrs
, num
, FALSE
);
1384 for (i
= 0; i
< num
; i
++, addr
= addr
->Next
)
1386 addr
->u
.s
.Length
= sizeof(*addr
);
1387 if (sock_addrs
[i
].ss_family
== WS_AF_INET6
)
1388 addr
->Address
.iSockaddrLength
= sizeof(SOCKADDR_IN6
);
1390 addr
->Address
.iSockaddrLength
= sizeof(SOCKADDR_IN
);
1391 addr
->Address
.lpSockaddr
= (SOCKADDR
*)(sock_addrs
+ i
);
1395 addr
->Next
= addr
+ 1;
1398 return ERROR_SUCCESS
;
1401 #ifdef HAVE_STRUCT___RES_STATE
1402 static BOOL
is_ip_address_string(const char *str
)
1407 ret
= inet_aton(str
, &in
);
1412 static ULONG
get_dns_suffix(WCHAR
*suffix
, ULONG
*len
)
1415 const char *found_suffix
= "";
1416 /* Always return a NULL-terminated string, even if it's empty. */
1418 #ifdef HAVE_STRUCT___RES_STATE
1421 initialise_resolver();
1422 for (i
= 0; !*found_suffix
&& i
< MAXDNSRCH
+ 1 && _res
.dnsrch
[i
]; i
++)
1424 /* This uses a heuristic to select a DNS suffix:
1425 * the first, non-IP address string is selected.
1427 if (!is_ip_address_string(_res
.dnsrch
[i
]))
1428 found_suffix
= _res
.dnsrch
[i
];
1433 size
= MultiByteToWideChar( CP_UNIXCP
, 0, found_suffix
, -1, NULL
, 0 ) * sizeof(WCHAR
);
1434 if (!suffix
|| *len
< size
)
1437 return ERROR_BUFFER_OVERFLOW
;
1439 *len
= MultiByteToWideChar( CP_UNIXCP
, 0, found_suffix
, -1, suffix
, *len
/ sizeof(WCHAR
) ) * sizeof(WCHAR
);
1440 return ERROR_SUCCESS
;
1443 ULONG WINAPI DECLSPEC_HOTPATCH
GetAdaptersAddresses(ULONG family
, ULONG flags
, PVOID reserved
,
1444 PIP_ADAPTER_ADDRESSES aa
, PULONG buflen
)
1446 InterfaceIndexTable
*table
;
1447 ULONG i
, size
, dns_server_size
= 0, dns_suffix_size
, total_size
, ret
= ERROR_NO_DATA
;
1449 TRACE("(%d, %08x, %p, %p, %p)\n", family
, flags
, reserved
, aa
, buflen
);
1451 if (!buflen
) return ERROR_INVALID_PARAMETER
;
1453 get_interface_indices( FALSE
, &table
);
1454 if (!table
|| !table
->numIndexes
)
1456 HeapFree(GetProcessHeap(), 0, table
);
1457 return ERROR_NO_DATA
;
1460 for (i
= 0; i
< table
->numIndexes
; i
++)
1463 if ((ret
= adapterAddressesFromIndex(family
, flags
, table
->indexes
[i
], NULL
, &size
)))
1465 HeapFree(GetProcessHeap(), 0, table
);
1470 if (!(flags
& GAA_FLAG_SKIP_DNS_SERVER
))
1472 /* Since DNS servers aren't really per adapter, get enough space for a
1473 * single copy of them.
1475 get_dns_server_addresses(NULL
, &dns_server_size
);
1476 total_size
+= dns_server_size
;
1478 /* Since DNS suffix also isn't really per adapter, get enough space for a
1479 * single copy of it.
1481 get_dns_suffix(NULL
, &dns_suffix_size
);
1482 total_size
+= dns_suffix_size
;
1483 if (aa
&& *buflen
>= total_size
)
1485 ULONG bytes_left
= size
= total_size
;
1486 PIP_ADAPTER_ADDRESSES first_aa
= aa
;
1487 PIP_ADAPTER_DNS_SERVER_ADDRESS firstDns
;
1490 for (i
= 0; i
< table
->numIndexes
; i
++)
1492 if ((ret
= adapterAddressesFromIndex(family
, flags
, table
->indexes
[i
], aa
, &size
)))
1494 HeapFree(GetProcessHeap(), 0, table
);
1497 if (i
< table
->numIndexes
- 1)
1499 aa
->Next
= (IP_ADAPTER_ADDRESSES
*)((char *)aa
+ size
);
1501 size
= bytes_left
-= size
;
1504 if (dns_server_size
)
1506 firstDns
= (PIP_ADAPTER_DNS_SERVER_ADDRESS
)((BYTE
*)first_aa
+ total_size
- dns_server_size
- dns_suffix_size
);
1507 get_dns_server_addresses(firstDns
, &dns_server_size
);
1508 for (aa
= first_aa
; aa
; aa
= aa
->Next
)
1510 if (aa
->IfType
!= IF_TYPE_SOFTWARE_LOOPBACK
&& aa
->OperStatus
== IfOperStatusUp
)
1511 aa
->FirstDnsServerAddress
= firstDns
;
1515 dnsSuffix
= (WCHAR
*)((BYTE
*)aa
+ total_size
- dns_suffix_size
);
1516 get_dns_suffix(dnsSuffix
, &dns_suffix_size
);
1517 for (; aa
; aa
= aa
->Next
)
1519 if (aa
->IfType
!= IF_TYPE_SOFTWARE_LOOPBACK
&& aa
->OperStatus
== IfOperStatusUp
)
1520 aa
->DnsSuffix
= dnsSuffix
;
1522 aa
->DnsSuffix
= dnsSuffix
+ dns_suffix_size
/ sizeof(WCHAR
) - 1;
1524 ret
= ERROR_SUCCESS
;
1528 ret
= ERROR_BUFFER_OVERFLOW
;
1529 *buflen
= total_size
;
1532 TRACE("num adapters %u\n", table
->numIndexes
);
1533 HeapFree(GetProcessHeap(), 0, table
);
1537 /******************************************************************
1538 * GetBestInterface (IPHLPAPI.@)
1540 * Get the interface, with the best route for the given IP address.
1543 * dwDestAddr [In] IP address to search the interface for
1544 * pdwBestIfIndex [Out] found best interface
1548 * Failure: error code from winerror.h
1550 DWORD WINAPI
GetBestInterface(IPAddr dwDestAddr
, PDWORD pdwBestIfIndex
)
1552 struct WS_sockaddr_in sa_in
;
1553 memset(&sa_in
, 0, sizeof(sa_in
));
1554 sa_in
.sin_family
= WS_AF_INET
;
1555 sa_in
.sin_addr
.S_un
.S_addr
= dwDestAddr
;
1556 return GetBestInterfaceEx((struct WS_sockaddr
*)&sa_in
, pdwBestIfIndex
);
1559 /******************************************************************
1560 * GetBestInterfaceEx (IPHLPAPI.@)
1562 * Get the interface, with the best route for the given IP address.
1565 * dwDestAddr [In] IP address to search the interface for
1566 * pdwBestIfIndex [Out] found best interface
1570 * Failure: error code from winerror.h
1572 DWORD WINAPI
GetBestInterfaceEx(struct WS_sockaddr
*pDestAddr
, PDWORD pdwBestIfIndex
)
1576 TRACE("pDestAddr %p, pdwBestIfIndex %p\n", pDestAddr
, pdwBestIfIndex
);
1577 if (!pDestAddr
|| !pdwBestIfIndex
)
1578 ret
= ERROR_INVALID_PARAMETER
;
1580 MIB_IPFORWARDROW ipRow
;
1582 if (pDestAddr
->sa_family
== WS_AF_INET
) {
1583 ret
= GetBestRoute(((struct WS_sockaddr_in
*)pDestAddr
)->sin_addr
.S_un
.S_addr
, 0, &ipRow
);
1584 if (ret
== ERROR_SUCCESS
)
1585 *pdwBestIfIndex
= ipRow
.dwForwardIfIndex
;
1587 FIXME("address family %d not supported\n", pDestAddr
->sa_family
);
1588 ret
= ERROR_NOT_SUPPORTED
;
1591 TRACE("returning %d\n", ret
);
1596 /******************************************************************
1597 * GetBestRoute (IPHLPAPI.@)
1599 * Get the best route for the given IP address.
1602 * dwDestAddr [In] IP address to search the best route for
1603 * dwSourceAddr [In] optional source IP address
1604 * pBestRoute [Out] found best route
1608 * Failure: error code from winerror.h
1610 DWORD WINAPI
GetBestRoute(DWORD dwDestAddr
, DWORD dwSourceAddr
, PMIB_IPFORWARDROW pBestRoute
)
1612 PMIB_IPFORWARDTABLE table
;
1615 TRACE("dwDestAddr 0x%08x, dwSourceAddr 0x%08x, pBestRoute %p\n", dwDestAddr
,
1616 dwSourceAddr
, pBestRoute
);
1618 return ERROR_INVALID_PARAMETER
;
1620 ret
= AllocateAndGetIpForwardTableFromStack(&table
, FALSE
, GetProcessHeap(), 0);
1622 DWORD ndx
, matchedBits
, matchedNdx
= table
->dwNumEntries
;
1624 for (ndx
= 0, matchedBits
= 0; ndx
< table
->dwNumEntries
; ndx
++) {
1625 if (table
->table
[ndx
].u1
.ForwardType
!= MIB_IPROUTE_TYPE_INVALID
&&
1626 (dwDestAddr
& table
->table
[ndx
].dwForwardMask
) ==
1627 (table
->table
[ndx
].dwForwardDest
& table
->table
[ndx
].dwForwardMask
)) {
1628 DWORD numShifts
, mask
;
1630 for (numShifts
= 0, mask
= table
->table
[ndx
].dwForwardMask
;
1631 mask
&& mask
& 1; mask
>>= 1, numShifts
++)
1633 if (numShifts
> matchedBits
) {
1634 matchedBits
= numShifts
;
1637 else if (!matchedBits
) {
1642 if (matchedNdx
< table
->dwNumEntries
) {
1643 memcpy(pBestRoute
, &table
->table
[matchedNdx
], sizeof(MIB_IPFORWARDROW
));
1644 ret
= ERROR_SUCCESS
;
1647 /* No route matches, which can happen if there's no default route. */
1648 ret
= ERROR_HOST_UNREACHABLE
;
1650 HeapFree(GetProcessHeap(), 0, table
);
1652 TRACE("returning %d\n", ret
);
1657 /******************************************************************
1658 * GetFriendlyIfIndex (IPHLPAPI.@)
1660 * Get a "friendly" version of IfIndex, which is one that doesn't
1661 * have the top byte set. Doesn't validate whether IfIndex is a valid
1665 * IfIndex [In] interface index to get the friendly one for
1668 * A friendly version of IfIndex.
1670 DWORD WINAPI
GetFriendlyIfIndex(DWORD IfIndex
)
1672 /* windows doesn't validate these, either, just makes sure the top byte is
1673 cleared. I assume my ifenum module never gives an index with the top
1675 TRACE("returning %d\n", IfIndex
);
1680 /******************************************************************
1681 * GetIfEntry (IPHLPAPI.@)
1683 * Get information about an interface.
1686 * pIfRow [In/Out] In: dwIndex of MIB_IFROW selects the interface.
1687 * Out: interface information
1691 * Failure: error code from winerror.h
1693 DWORD WINAPI
GetIfEntry(PMIB_IFROW pIfRow
)
1696 char nameBuf
[MAX_ADAPTER_NAME
];
1699 TRACE("pIfRow %p\n", pIfRow
);
1701 return ERROR_INVALID_PARAMETER
;
1703 name
= getInterfaceNameByIndex(pIfRow
->dwIndex
, nameBuf
);
1705 ret
= getInterfaceEntryByName(name
, pIfRow
);
1706 if (ret
== NO_ERROR
)
1707 ret
= getInterfaceStatsByName(name
, pIfRow
);
1710 ret
= ERROR_INVALID_DATA
;
1711 TRACE("returning %d\n", ret
);
1715 /******************************************************************
1716 * GetIfEntry2 (IPHLPAPI.@)
1718 DWORD WINAPI
GetIfEntry2( MIB_IF_ROW2
*row2
)
1720 DWORD ret
, len
= sizeof(row2
->Description
)/sizeof(row2
->Description
[0]);
1721 char buf
[MAX_ADAPTER_NAME
], *name
;
1724 TRACE("%p\n", row2
);
1726 if (!row2
|| (!(name
= getInterfaceNameByIndex( row2
->InterfaceIndex
, buf
)) &&
1727 !(name
= getInterfaceNameByIndex( row2
->InterfaceLuid
.Info
.NetLuidIndex
, buf
))))
1729 return ERROR_INVALID_PARAMETER
;
1731 if ((ret
= getInterfaceEntryByName( name
, &row
))) return ret
;
1732 if ((ret
= getInterfaceStatsByName( name
, &row
))) return ret
;
1734 memset( row2
, 0, sizeof(*row2
) );
1735 row2
->InterfaceLuid
.Info
.Reserved
= 0;
1736 row2
->InterfaceLuid
.Info
.NetLuidIndex
= row
.dwIndex
;
1737 row2
->InterfaceLuid
.Info
.IfType
= row
.dwType
;
1738 row2
->InterfaceIndex
= row
.dwIndex
;
1739 row2
->InterfaceGuid
.Data1
= row
.dwIndex
;
1740 row2
->Type
= row
.dwType
;
1741 row2
->Mtu
= row
.dwMtu
;
1742 MultiByteToWideChar( CP_UNIXCP
, 0, (const char *)row
.bDescr
, -1, row2
->Description
, len
);
1743 row2
->PhysicalAddressLength
= row
.dwPhysAddrLen
;
1744 memcpy( &row2
->PhysicalAddress
, &row
.bPhysAddr
, row
.dwPhysAddrLen
);
1745 memcpy( &row2
->PermanentPhysicalAddress
, &row
.bPhysAddr
, row
.dwPhysAddrLen
);
1746 row2
->OperStatus
= IfOperStatusUp
;
1747 row2
->AdminStatus
= NET_IF_ADMIN_STATUS_UP
;
1748 row2
->MediaConnectState
= MediaConnectStateConnected
;
1749 row2
->ConnectionType
= NET_IF_CONNECTION_DEDICATED
;
1752 row2
->InOctets
= row
.dwInOctets
;
1753 row2
->InUcastPkts
= row
.dwInUcastPkts
;
1754 row2
->InNUcastPkts
= row
.dwInNUcastPkts
;
1755 row2
->InDiscards
= row
.dwInDiscards
;
1756 row2
->InErrors
= row
.dwInErrors
;
1757 row2
->InUnknownProtos
= row
.dwInUnknownProtos
;
1758 row2
->OutOctets
= row
.dwOutOctets
;
1759 row2
->OutUcastPkts
= row
.dwOutUcastPkts
;
1760 row2
->OutNUcastPkts
= row
.dwOutNUcastPkts
;
1761 row2
->OutDiscards
= row
.dwOutDiscards
;
1762 row2
->OutErrors
= row
.dwOutErrors
;
1767 static int IfTableSorter(const void *a
, const void *b
)
1772 ret
= ((const MIB_IFROW
*)a
)->dwIndex
- ((const MIB_IFROW
*)b
)->dwIndex
;
1779 /******************************************************************
1780 * GetIfTable (IPHLPAPI.@)
1782 * Get a table of local interfaces.
1785 * pIfTable [Out] buffer for local interfaces table
1786 * pdwSize [In/Out] length of output buffer
1787 * bOrder [In] whether to sort the table
1791 * Failure: error code from winerror.h
1794 * If pdwSize is less than required, the function will return
1795 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
1797 * If bOrder is true, the returned table will be sorted by interface index.
1799 DWORD WINAPI
GetIfTable(PMIB_IFTABLE pIfTable
, PULONG pdwSize
, BOOL bOrder
)
1803 TRACE("pIfTable %p, pdwSize %p, bOrder %d\n", pIfTable
, pdwSize
, bOrder
);
1806 ret
= ERROR_INVALID_PARAMETER
;
1808 DWORD numInterfaces
= get_interface_indices( FALSE
, NULL
);
1809 ULONG size
= sizeof(MIB_IFTABLE
);
1811 if (numInterfaces
> 1)
1812 size
+= (numInterfaces
- 1) * sizeof(MIB_IFROW
);
1813 if (!pIfTable
|| *pdwSize
< size
) {
1815 ret
= ERROR_INSUFFICIENT_BUFFER
;
1818 InterfaceIndexTable
*table
;
1819 get_interface_indices( FALSE
, &table
);
1822 size
= sizeof(MIB_IFTABLE
);
1823 if (table
->numIndexes
> 1)
1824 size
+= (table
->numIndexes
- 1) * sizeof(MIB_IFROW
);
1825 if (*pdwSize
< size
) {
1827 ret
= ERROR_INSUFFICIENT_BUFFER
;
1833 pIfTable
->dwNumEntries
= 0;
1834 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1835 pIfTable
->table
[ndx
].dwIndex
= table
->indexes
[ndx
];
1836 GetIfEntry(&pIfTable
->table
[ndx
]);
1837 pIfTable
->dwNumEntries
++;
1840 qsort(pIfTable
->table
, pIfTable
->dwNumEntries
, sizeof(MIB_IFROW
),
1844 HeapFree(GetProcessHeap(), 0, table
);
1847 ret
= ERROR_OUTOFMEMORY
;
1850 TRACE("returning %d\n", ret
);
1854 /******************************************************************
1855 * GetIfTable2Ex (IPHLPAPI.@)
1857 DWORD WINAPI
GetIfTable2Ex( MIB_IF_TABLE_LEVEL level
, MIB_IF_TABLE2
**table
)
1859 DWORD i
, nb_interfaces
, size
= sizeof(MIB_IF_TABLE2
);
1860 InterfaceIndexTable
*index_table
;
1863 TRACE( "level %u, table %p\n", level
, table
);
1865 if (!table
|| level
> MibIfTableNormalWithoutStatistics
)
1866 return ERROR_INVALID_PARAMETER
;
1868 if (level
!= MibIfTableNormal
)
1869 FIXME("level %u not fully supported\n", level
);
1871 if ((nb_interfaces
= get_interface_indices( FALSE
, NULL
)) > 1)
1872 size
+= (nb_interfaces
- 1) * sizeof(MIB_IF_ROW2
);
1874 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, size
))) return ERROR_OUTOFMEMORY
;
1876 get_interface_indices( FALSE
, &index_table
);
1879 HeapFree( GetProcessHeap(), 0, ret
);
1880 return ERROR_OUTOFMEMORY
;
1883 ret
->NumEntries
= 0;
1884 for (i
= 0; i
< index_table
->numIndexes
; i
++)
1886 ret
->Table
[i
].InterfaceIndex
= index_table
->indexes
[i
];
1887 GetIfEntry2( &ret
->Table
[i
] );
1891 HeapFree( GetProcessHeap(), 0, index_table
);
1896 /******************************************************************
1897 * GetIfTable2 (IPHLPAPI.@)
1899 DWORD WINAPI
GetIfTable2( MIB_IF_TABLE2
**table
)
1901 TRACE( "table %p\n", table
);
1902 return GetIfTable2Ex(MibIfTableNormal
, table
);
1905 /******************************************************************
1906 * GetInterfaceInfo (IPHLPAPI.@)
1908 * Get a list of network interface adapters.
1911 * pIfTable [Out] buffer for interface adapters
1912 * dwOutBufLen [Out] if buffer is too small, returns required size
1916 * Failure: error code from winerror.h
1919 * MSDN states this should return non-loopback interfaces only.
1921 DWORD WINAPI
GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable
, PULONG dwOutBufLen
)
1925 TRACE("pIfTable %p, dwOutBufLen %p\n", pIfTable
, dwOutBufLen
);
1927 ret
= ERROR_INVALID_PARAMETER
;
1929 DWORD numInterfaces
= get_interface_indices( FALSE
, NULL
);
1930 ULONG size
= sizeof(IP_INTERFACE_INFO
);
1932 if (numInterfaces
> 1)
1933 size
+= (numInterfaces
- 1) * sizeof(IP_ADAPTER_INDEX_MAP
);
1934 if (!pIfTable
|| *dwOutBufLen
< size
) {
1935 *dwOutBufLen
= size
;
1936 ret
= ERROR_INSUFFICIENT_BUFFER
;
1939 InterfaceIndexTable
*table
;
1940 get_interface_indices( FALSE
, &table
);
1943 size
= sizeof(IP_INTERFACE_INFO
);
1944 if (table
->numIndexes
> 1)
1945 size
+= (table
->numIndexes
- 1) * sizeof(IP_ADAPTER_INDEX_MAP
);
1946 if (*dwOutBufLen
< size
) {
1947 *dwOutBufLen
= size
;
1948 ret
= ERROR_INSUFFICIENT_BUFFER
;
1952 char nameBuf
[MAX_ADAPTER_NAME
];
1954 *dwOutBufLen
= size
;
1955 pIfTable
->NumAdapters
= 0;
1956 for (ndx
= 0; ndx
< table
->numIndexes
; ndx
++) {
1957 const char *walker
, *name
;
1960 pIfTable
->Adapter
[ndx
].Index
= table
->indexes
[ndx
];
1961 name
= getInterfaceNameByIndex(table
->indexes
[ndx
], nameBuf
);
1962 for (walker
= name
, assigner
= pIfTable
->Adapter
[ndx
].Name
;
1963 walker
&& *walker
&&
1964 assigner
- pIfTable
->Adapter
[ndx
].Name
< MAX_ADAPTER_NAME
- 1;
1965 walker
++, assigner
++)
1966 *assigner
= *walker
;
1968 pIfTable
->NumAdapters
++;
1972 HeapFree(GetProcessHeap(), 0, table
);
1975 ret
= ERROR_OUTOFMEMORY
;
1978 TRACE("returning %d\n", ret
);
1983 /******************************************************************
1984 * GetIpAddrTable (IPHLPAPI.@)
1986 * Get interface-to-IP address mapping table.
1989 * pIpAddrTable [Out] buffer for mapping table
1990 * pdwSize [In/Out] length of output buffer
1991 * bOrder [In] whether to sort the table
1995 * Failure: error code from winerror.h
1998 * If pdwSize is less than required, the function will return
1999 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2001 * If bOrder is true, the returned table will be sorted by the next hop and
2002 * an assortment of arbitrary parameters.
2004 DWORD WINAPI
GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable
, PULONG pdwSize
, BOOL bOrder
)
2008 TRACE("pIpAddrTable %p, pdwSize %p, bOrder %d\n", pIpAddrTable
, pdwSize
,
2011 ret
= ERROR_INVALID_PARAMETER
;
2013 PMIB_IPADDRTABLE table
;
2015 ret
= getIPAddrTable(&table
, GetProcessHeap(), 0);
2016 if (ret
== NO_ERROR
)
2018 ULONG size
= FIELD_OFFSET(MIB_IPADDRTABLE
, table
[table
->dwNumEntries
]);
2020 if (!pIpAddrTable
|| *pdwSize
< size
) {
2022 ret
= ERROR_INSUFFICIENT_BUFFER
;
2026 memcpy(pIpAddrTable
, table
, size
);
2027 /* sort by numeric IP value */
2029 qsort(pIpAddrTable
->table
, pIpAddrTable
->dwNumEntries
,
2030 sizeof(MIB_IPADDRROW
), IpAddrTableNumericSorter
);
2031 /* sort ensuring loopback interfaces are in the end */
2033 qsort(pIpAddrTable
->table
, pIpAddrTable
->dwNumEntries
,
2034 sizeof(MIB_IPADDRROW
), IpAddrTableLoopbackSorter
);
2037 HeapFree(GetProcessHeap(), 0, table
);
2040 TRACE("returning %d\n", ret
);
2045 /******************************************************************
2046 * GetIpForwardTable (IPHLPAPI.@)
2048 * Get the route table.
2051 * pIpForwardTable [Out] buffer for route table
2052 * pdwSize [In/Out] length of output buffer
2053 * bOrder [In] whether to sort the table
2057 * Failure: error code from winerror.h
2060 * If pdwSize is less than required, the function will return
2061 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2063 * If bOrder is true, the returned table will be sorted by the next hop and
2064 * an assortment of arbitrary parameters.
2066 DWORD WINAPI
GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable
, PULONG pdwSize
, BOOL bOrder
)
2069 PMIB_IPFORWARDTABLE table
;
2071 TRACE("pIpForwardTable %p, pdwSize %p, bOrder %d\n", pIpForwardTable
, pdwSize
, bOrder
);
2073 if (!pdwSize
) return ERROR_INVALID_PARAMETER
;
2075 ret
= AllocateAndGetIpForwardTableFromStack(&table
, bOrder
, GetProcessHeap(), 0);
2077 DWORD size
= FIELD_OFFSET( MIB_IPFORWARDTABLE
, table
[table
->dwNumEntries
] );
2078 if (!pIpForwardTable
|| *pdwSize
< size
) {
2080 ret
= ERROR_INSUFFICIENT_BUFFER
;
2084 memcpy(pIpForwardTable
, table
, size
);
2086 HeapFree(GetProcessHeap(), 0, table
);
2088 TRACE("returning %d\n", ret
);
2093 /******************************************************************
2094 * GetIpNetTable (IPHLPAPI.@)
2096 * Get the IP-to-physical address mapping table.
2099 * pIpNetTable [Out] buffer for mapping table
2100 * pdwSize [In/Out] length of output buffer
2101 * bOrder [In] whether to sort the table
2105 * Failure: error code from winerror.h
2108 * If pdwSize is less than required, the function will return
2109 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the required byte
2111 * If bOrder is true, the returned table will be sorted by IP address.
2113 DWORD WINAPI
GetIpNetTable(PMIB_IPNETTABLE pIpNetTable
, PULONG pdwSize
, BOOL bOrder
)
2116 PMIB_IPNETTABLE table
;
2118 TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable
, pdwSize
, bOrder
);
2120 if (!pdwSize
) return ERROR_INVALID_PARAMETER
;
2122 ret
= AllocateAndGetIpNetTableFromStack( &table
, bOrder
, GetProcessHeap(), 0 );
2124 DWORD size
= FIELD_OFFSET( MIB_IPNETTABLE
, table
[table
->dwNumEntries
] );
2125 if (!pIpNetTable
|| *pdwSize
< size
) {
2127 ret
= ERROR_INSUFFICIENT_BUFFER
;
2131 memcpy(pIpNetTable
, table
, size
);
2133 HeapFree(GetProcessHeap(), 0, table
);
2135 TRACE("returning %d\n", ret
);
2139 /* Gets the DNS server list into the list beginning at list. Assumes that
2140 * a single server address may be placed at list if *len is at least
2141 * sizeof(IP_ADDR_STRING) long. Otherwise, list->Next is set to firstDynamic,
2142 * and assumes that all remaining DNS servers are contiguously located
2143 * beginning at firstDynamic. On input, *len is assumed to be the total number
2144 * of bytes available for all DNS servers, and is ignored if list is NULL.
2145 * On return, *len is set to the total number of bytes required for all DNS
2147 * Returns ERROR_BUFFER_OVERFLOW if *len is insufficient,
2148 * ERROR_SUCCESS otherwise.
2150 static DWORD
get_dns_server_list(PIP_ADDR_STRING list
,
2151 PIP_ADDR_STRING firstDynamic
, DWORD
*len
)
2154 int num
= get_dns_servers( NULL
, 0, TRUE
);
2156 size
= num
* sizeof(IP_ADDR_STRING
);
2157 if (!list
|| *len
< size
) {
2159 return ERROR_BUFFER_OVERFLOW
;
2163 PIP_ADDR_STRING ptr
;
2165 SOCKADDR_STORAGE
*addr
= HeapAlloc( GetProcessHeap(), 0, num
* sizeof(SOCKADDR_STORAGE
) );
2167 get_dns_servers( addr
, num
, TRUE
);
2169 for (i
= 0, ptr
= list
; i
< num
; i
++, ptr
= ptr
->Next
) {
2170 toIPAddressString(((struct sockaddr_in
*)(addr
+ i
))->sin_addr
.s_addr
,
2171 ptr
->IpAddress
.String
);
2175 ptr
->Next
= firstDynamic
;
2177 ptr
->Next
= (PIP_ADDR_STRING
)((PBYTE
)ptr
+ sizeof(IP_ADDR_STRING
));
2179 HeapFree( GetProcessHeap(), 0, addr
);
2181 return ERROR_SUCCESS
;
2184 /******************************************************************
2185 * GetNetworkParams (IPHLPAPI.@)
2187 * Get the network parameters for the local computer.
2190 * pFixedInfo [Out] buffer for network parameters
2191 * pOutBufLen [In/Out] length of output buffer
2195 * Failure: error code from winerror.h
2198 * If pOutBufLen is less than required, the function will return
2199 * ERROR_INSUFFICIENT_BUFFER, and pOutBufLen will be set to the required byte
2202 DWORD WINAPI
GetNetworkParams(PFIXED_INFO pFixedInfo
, PULONG pOutBufLen
)
2204 DWORD ret
, size
, serverListSize
;
2208 TRACE("pFixedInfo %p, pOutBufLen %p\n", pFixedInfo
, pOutBufLen
);
2210 return ERROR_INVALID_PARAMETER
;
2212 get_dns_server_list(NULL
, NULL
, &serverListSize
);
2213 size
= sizeof(FIXED_INFO
) + serverListSize
- sizeof(IP_ADDR_STRING
);
2214 if (!pFixedInfo
|| *pOutBufLen
< size
) {
2216 return ERROR_BUFFER_OVERFLOW
;
2219 memset(pFixedInfo
, 0, size
);
2220 size
= sizeof(pFixedInfo
->HostName
);
2221 GetComputerNameExA(ComputerNameDnsHostname
, pFixedInfo
->HostName
, &size
);
2222 size
= sizeof(pFixedInfo
->DomainName
);
2223 GetComputerNameExA(ComputerNameDnsDomain
, pFixedInfo
->DomainName
, &size
);
2224 get_dns_server_list(&pFixedInfo
->DnsServerList
,
2225 (PIP_ADDR_STRING
)((BYTE
*)pFixedInfo
+ sizeof(FIXED_INFO
)),
2227 /* Assume the first DNS server in the list is the "current" DNS server: */
2228 pFixedInfo
->CurrentDnsServer
= &pFixedInfo
->DnsServerList
;
2229 pFixedInfo
->NodeType
= HYBRID_NODETYPE
;
2230 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
2231 "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ
, &hKey
);
2232 if (regReturn
!= ERROR_SUCCESS
)
2233 regReturn
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
2234 "SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0, KEY_READ
,
2236 if (regReturn
== ERROR_SUCCESS
)
2238 DWORD size
= sizeof(pFixedInfo
->ScopeId
);
2240 RegQueryValueExA(hKey
, "ScopeID", NULL
, NULL
, (LPBYTE
)pFixedInfo
->ScopeId
, &size
);
2244 /* FIXME: can check whether routing's enabled in /proc/sys/net/ipv4/ip_forward
2245 I suppose could also check for a listener on port 53 to set EnableDns */
2247 TRACE("returning %d\n", ret
);
2252 /******************************************************************
2253 * GetNumberOfInterfaces (IPHLPAPI.@)
2255 * Get the number of interfaces.
2258 * pdwNumIf [Out] number of interfaces
2261 * NO_ERROR on success, ERROR_INVALID_PARAMETER if pdwNumIf is NULL.
2263 DWORD WINAPI
GetNumberOfInterfaces(PDWORD pdwNumIf
)
2267 TRACE("pdwNumIf %p\n", pdwNumIf
);
2269 ret
= ERROR_INVALID_PARAMETER
;
2271 *pdwNumIf
= get_interface_indices( FALSE
, NULL
);
2274 TRACE("returning %d\n", ret
);
2279 /******************************************************************
2280 * GetPerAdapterInfo (IPHLPAPI.@)
2282 * Get information about an adapter corresponding to an interface.
2285 * IfIndex [In] interface info
2286 * pPerAdapterInfo [Out] buffer for per adapter info
2287 * pOutBufLen [In/Out] length of output buffer
2291 * Failure: error code from winerror.h
2293 DWORD WINAPI
GetPerAdapterInfo(ULONG IfIndex
, PIP_PER_ADAPTER_INFO pPerAdapterInfo
, PULONG pOutBufLen
)
2295 ULONG bytesNeeded
= sizeof(IP_PER_ADAPTER_INFO
), serverListSize
= 0;
2296 DWORD ret
= NO_ERROR
;
2298 TRACE("(IfIndex %d, pPerAdapterInfo %p, pOutBufLen %p)\n", IfIndex
, pPerAdapterInfo
, pOutBufLen
);
2300 if (!pOutBufLen
) return ERROR_INVALID_PARAMETER
;
2302 if (!isIfIndexLoopback(IfIndex
)) {
2303 get_dns_server_list(NULL
, NULL
, &serverListSize
);
2304 if (serverListSize
> sizeof(IP_ADDR_STRING
))
2305 bytesNeeded
+= serverListSize
- sizeof(IP_ADDR_STRING
);
2307 if (!pPerAdapterInfo
|| *pOutBufLen
< bytesNeeded
)
2309 *pOutBufLen
= bytesNeeded
;
2310 return ERROR_BUFFER_OVERFLOW
;
2313 memset(pPerAdapterInfo
, 0, bytesNeeded
);
2314 if (!isIfIndexLoopback(IfIndex
)) {
2315 ret
= get_dns_server_list(&pPerAdapterInfo
->DnsServerList
,
2316 (PIP_ADDR_STRING
)((PBYTE
)pPerAdapterInfo
+ sizeof(IP_PER_ADAPTER_INFO
)),
2318 /* Assume the first DNS server in the list is the "current" DNS server: */
2319 pPerAdapterInfo
->CurrentDnsServer
= &pPerAdapterInfo
->DnsServerList
;
2325 /******************************************************************
2326 * GetRTTAndHopCount (IPHLPAPI.@)
2328 * Get round-trip time (RTT) and hop count.
2332 * DestIpAddress [In] destination address to get the info for
2333 * HopCount [Out] retrieved hop count
2334 * MaxHops [In] maximum hops to search for the destination
2335 * RTT [Out] RTT in milliseconds
2342 * Stub, returns FALSE.
2344 BOOL WINAPI
GetRTTAndHopCount(IPAddr DestIpAddress
, PULONG HopCount
, ULONG MaxHops
, PULONG RTT
)
2346 FIXME("(DestIpAddress 0x%08x, HopCount %p, MaxHops %d, RTT %p): stub\n",
2347 DestIpAddress
, HopCount
, MaxHops
, RTT
);
2352 /******************************************************************
2353 * GetTcpTable (IPHLPAPI.@)
2355 * Get the table of active TCP connections.
2358 * pTcpTable [Out] buffer for TCP connections table
2359 * pdwSize [In/Out] length of output buffer
2360 * bOrder [In] whether to order the table
2364 * Failure: error code from winerror.h
2367 * If pdwSize is less than required, the function will return
2368 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to
2369 * the required byte size.
2370 * If bOrder is true, the returned table will be sorted, first by
2371 * local address and port number, then by remote address and port
2374 DWORD WINAPI
GetTcpTable(PMIB_TCPTABLE pTcpTable
, PDWORD pdwSize
, BOOL bOrder
)
2376 TRACE("pTcpTable %p, pdwSize %p, bOrder %d\n", pTcpTable
, pdwSize
, bOrder
);
2377 return GetExtendedTcpTable(pTcpTable
, pdwSize
, bOrder
, WS_AF_INET
, TCP_TABLE_BASIC_ALL
, 0);
2380 /******************************************************************
2381 * GetExtendedTcpTable (IPHLPAPI.@)
2383 DWORD WINAPI
GetExtendedTcpTable(PVOID pTcpTable
, PDWORD pdwSize
, BOOL bOrder
,
2384 ULONG ulAf
, TCP_TABLE_CLASS TableClass
, ULONG Reserved
)
2389 TRACE("pTcpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2390 pTcpTable
, pdwSize
, bOrder
, ulAf
, TableClass
, Reserved
);
2392 if (!pdwSize
) return ERROR_INVALID_PARAMETER
;
2394 if (ulAf
!= WS_AF_INET
)
2396 FIXME("ulAf = %u not supported\n", ulAf
);
2397 return ERROR_NOT_SUPPORTED
;
2399 if (TableClass
>= TCP_TABLE_OWNER_MODULE_LISTENER
)
2400 FIXME("module classes not fully supported\n");
2402 if ((ret
= build_tcp_table(TableClass
, &table
, bOrder
, GetProcessHeap(), 0, &size
)))
2405 if (!pTcpTable
|| *pdwSize
< size
)
2408 ret
= ERROR_INSUFFICIENT_BUFFER
;
2413 memcpy(pTcpTable
, table
, size
);
2415 HeapFree(GetProcessHeap(), 0, table
);
2419 /******************************************************************
2420 * GetUdpTable (IPHLPAPI.@)
2422 * Get a table of active UDP connections.
2425 * pUdpTable [Out] buffer for UDP connections table
2426 * pdwSize [In/Out] length of output buffer
2427 * bOrder [In] whether to order the table
2431 * Failure: error code from winerror.h
2434 * If pdwSize is less than required, the function will return
2435 * ERROR_INSUFFICIENT_BUFFER, and *pdwSize will be set to the
2436 * required byte size.
2437 * If bOrder is true, the returned table will be sorted, first by
2438 * local address, then by local port number.
2440 DWORD WINAPI
GetUdpTable(PMIB_UDPTABLE pUdpTable
, PDWORD pdwSize
, BOOL bOrder
)
2442 return GetExtendedUdpTable(pUdpTable
, pdwSize
, bOrder
, WS_AF_INET
, UDP_TABLE_BASIC
, 0);
2445 /******************************************************************
2446 * GetExtendedUdpTable (IPHLPAPI.@)
2448 DWORD WINAPI
GetExtendedUdpTable(PVOID pUdpTable
, PDWORD pdwSize
, BOOL bOrder
,
2449 ULONG ulAf
, UDP_TABLE_CLASS TableClass
, ULONG Reserved
)
2454 TRACE("pUdpTable %p, pdwSize %p, bOrder %d, ulAf %u, TableClass %u, Reserved %u\n",
2455 pUdpTable
, pdwSize
, bOrder
, ulAf
, TableClass
, Reserved
);
2457 if (!pdwSize
) return ERROR_INVALID_PARAMETER
;
2459 if (ulAf
!= WS_AF_INET
)
2461 FIXME("ulAf = %u not supported\n", ulAf
);
2462 return ERROR_NOT_SUPPORTED
;
2464 if (TableClass
== UDP_TABLE_OWNER_MODULE
)
2465 FIXME("UDP_TABLE_OWNER_MODULE not fully supported\n");
2467 if ((ret
= build_udp_table(TableClass
, &table
, bOrder
, GetProcessHeap(), 0, &size
)))
2470 if (!pUdpTable
|| *pdwSize
< size
)
2473 ret
= ERROR_INSUFFICIENT_BUFFER
;
2478 memcpy(pUdpTable
, table
, size
);
2480 HeapFree(GetProcessHeap(), 0, table
);
2484 DWORD WINAPI
GetUnicastIpAddressEntry(MIB_UNICASTIPADDRESS_ROW
*row
)
2486 IP_ADAPTER_ADDRESSES
*aa
, *ptr
;
2493 return ERROR_INVALID_PARAMETER
;
2495 ret
= GetAdaptersAddresses(row
->Address
.si_family
, 0, NULL
, NULL
, &size
);
2496 if (ret
!= ERROR_BUFFER_OVERFLOW
)
2498 if (!(ptr
= HeapAlloc(GetProcessHeap(), 0, size
)))
2499 return ERROR_OUTOFMEMORY
;
2500 if ((ret
= GetAdaptersAddresses(row
->Address
.si_family
, 0, NULL
, ptr
, &size
)))
2502 HeapFree(GetProcessHeap(), 0, ptr
);
2506 ret
= ERROR_FILE_NOT_FOUND
;
2507 for (aa
= ptr
; aa
; aa
= aa
->Next
)
2509 IP_ADAPTER_UNICAST_ADDRESS
*ua
;
2511 if (aa
->u
.s
.IfIndex
!= row
->InterfaceIndex
&&
2512 memcmp(&aa
->Luid
, &row
->InterfaceLuid
, sizeof(row
->InterfaceLuid
)))
2514 ret
= ERROR_NOT_FOUND
;
2516 ua
= aa
->FirstUnicastAddress
;
2519 SOCKADDR_INET
*uaaddr
= (SOCKADDR_INET
*)ua
->Address
.lpSockaddr
;
2521 if ((row
->Address
.si_family
== WS_AF_INET6
&&
2522 !memcmp(&row
->Address
.Ipv6
.sin6_addr
, &uaaddr
->Ipv6
.sin6_addr
, sizeof(uaaddr
->Ipv6
.sin6_addr
))) ||
2523 (row
->Address
.si_family
== WS_AF_INET
&&
2524 row
->Address
.Ipv4
.sin_addr
.S_un
.S_addr
== uaaddr
->Ipv4
.sin_addr
.S_un
.S_addr
))
2526 memcpy(&row
->InterfaceLuid
, &aa
->Luid
, sizeof(aa
->Luid
));
2527 row
->InterfaceIndex
= aa
->u
.s
.IfIndex
;
2528 row
->PrefixOrigin
= ua
->PrefixOrigin
;
2529 row
->SuffixOrigin
= ua
->SuffixOrigin
;
2530 row
->ValidLifetime
= ua
->ValidLifetime
;
2531 row
->PreferredLifetime
= ua
->PreferredLifetime
;
2532 row
->OnLinkPrefixLength
= ua
->OnLinkPrefixLength
;
2533 row
->SkipAsSource
= 0;
2534 row
->DadState
= ua
->DadState
;
2535 if (row
->Address
.si_family
== WS_AF_INET6
)
2536 row
->ScopeId
.u
.Value
= row
->Address
.Ipv6
.sin6_scope_id
;
2538 row
->ScopeId
.u
.Value
= 0;
2539 NtQuerySystemTime(&row
->CreationTimeStamp
);
2540 HeapFree(GetProcessHeap(), 0, ptr
);
2546 HeapFree(GetProcessHeap(), 0, ptr
);
2551 DWORD WINAPI
GetUnicastIpAddressTable(ADDRESS_FAMILY family
, MIB_UNICASTIPADDRESS_TABLE
**table
)
2553 IP_ADAPTER_ADDRESSES
*aa
, *ptr
;
2554 MIB_UNICASTIPADDRESS_TABLE
*data
;
2555 DWORD ret
, count
= 0;
2558 TRACE("%u, %p\n", family
, table
);
2560 if (!table
|| (family
!= WS_AF_INET
&& family
!= WS_AF_INET6
&& family
!= WS_AF_UNSPEC
))
2561 return ERROR_INVALID_PARAMETER
;
2563 flags
= GAA_FLAG_SKIP_ANYCAST
|
2564 GAA_FLAG_SKIP_MULTICAST
|
2565 GAA_FLAG_SKIP_DNS_SERVER
|
2566 GAA_FLAG_SKIP_FRIENDLY_NAME
;
2568 ret
= GetAdaptersAddresses(family
, flags
, NULL
, NULL
, &size
);
2569 if (ret
!= ERROR_BUFFER_OVERFLOW
)
2571 if (!(ptr
= HeapAlloc(GetProcessHeap(), 0, size
)))
2572 return ERROR_OUTOFMEMORY
;
2573 if ((ret
= GetAdaptersAddresses(family
, flags
, NULL
, ptr
, &size
)))
2575 HeapFree(GetProcessHeap(), 0, ptr
);
2579 for (aa
= ptr
; aa
; aa
= aa
->Next
)
2581 IP_ADAPTER_UNICAST_ADDRESS
*ua
= aa
->FirstUnicastAddress
;
2589 if (!(data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) + (count
- 1) * sizeof(data
->Table
[0]))))
2591 HeapFree(GetProcessHeap(), 0, ptr
);
2592 return ERROR_OUTOFMEMORY
;
2595 data
->NumEntries
= 0;
2596 for (aa
= ptr
; aa
; aa
= aa
->Next
)
2598 IP_ADAPTER_UNICAST_ADDRESS
*ua
= aa
->FirstUnicastAddress
;
2601 MIB_UNICASTIPADDRESS_ROW
*row
= &data
->Table
[data
->NumEntries
];
2602 memcpy(&row
->Address
, ua
->Address
.lpSockaddr
, ua
->Address
.iSockaddrLength
);
2603 memcpy(&row
->InterfaceLuid
, &aa
->Luid
, sizeof(aa
->Luid
));
2604 row
->InterfaceIndex
= aa
->u
.s
.IfIndex
;
2605 row
->PrefixOrigin
= ua
->PrefixOrigin
;
2606 row
->SuffixOrigin
= ua
->SuffixOrigin
;
2607 row
->ValidLifetime
= ua
->ValidLifetime
;
2608 row
->PreferredLifetime
= ua
->PreferredLifetime
;
2609 row
->OnLinkPrefixLength
= ua
->OnLinkPrefixLength
;
2610 row
->SkipAsSource
= 0;
2611 row
->DadState
= ua
->DadState
;
2612 if (row
->Address
.si_family
== WS_AF_INET6
)
2613 row
->ScopeId
.u
.Value
= row
->Address
.Ipv6
.sin6_scope_id
;
2615 row
->ScopeId
.u
.Value
= 0;
2616 NtQuerySystemTime(&row
->CreationTimeStamp
);
2623 HeapFree(GetProcessHeap(), 0, ptr
);
2629 /******************************************************************
2630 * GetUniDirectionalAdapterInfo (IPHLPAPI.@)
2632 * This is a Win98-only function to get information on "unidirectional"
2633 * adapters. Since this is pretty nonsensical in other contexts, it
2634 * never returns anything.
2637 * pIPIfInfo [Out] buffer for adapter infos
2638 * dwOutBufLen [Out] length of the output buffer
2642 * Failure: error code from winerror.h
2645 * Stub, returns ERROR_NOT_SUPPORTED.
2647 DWORD WINAPI
GetUniDirectionalAdapterInfo(PIP_UNIDIRECTIONAL_ADAPTER_ADDRESS pIPIfInfo
, PULONG dwOutBufLen
)
2649 TRACE("pIPIfInfo %p, dwOutBufLen %p\n", pIPIfInfo
, dwOutBufLen
);
2650 /* a unidirectional adapter?? not bloody likely! */
2651 return ERROR_NOT_SUPPORTED
;
2655 /******************************************************************
2656 * IpReleaseAddress (IPHLPAPI.@)
2658 * Release an IP obtained through DHCP,
2661 * AdapterInfo [In] adapter to release IP address
2665 * Failure: error code from winerror.h
2668 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2669 * this function does nothing.
2672 * Stub, returns ERROR_NOT_SUPPORTED.
2674 DWORD WINAPI
IpReleaseAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
2676 FIXME("Stub AdapterInfo %p\n", AdapterInfo
);
2677 return ERROR_NOT_SUPPORTED
;
2681 /******************************************************************
2682 * IpRenewAddress (IPHLPAPI.@)
2684 * Renew an IP obtained through DHCP.
2687 * AdapterInfo [In] adapter to renew IP address
2691 * Failure: error code from winerror.h
2694 * Since GetAdaptersInfo never returns adapters that have DHCP enabled,
2695 * this function does nothing.
2698 * Stub, returns ERROR_NOT_SUPPORTED.
2700 DWORD WINAPI
IpRenewAddress(PIP_ADAPTER_INDEX_MAP AdapterInfo
)
2702 FIXME("Stub AdapterInfo %p\n", AdapterInfo
);
2703 return ERROR_NOT_SUPPORTED
;
2707 /******************************************************************
2708 * NotifyAddrChange (IPHLPAPI.@)
2710 * Notify caller whenever the ip-interface map is changed.
2713 * Handle [Out] handle usable in asynchronous notification
2714 * overlapped [In] overlapped structure that notifies the caller
2718 * Failure: error code from winerror.h
2721 * Stub, returns ERROR_NOT_SUPPORTED.
2723 DWORD WINAPI
NotifyAddrChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2725 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2726 if (Handle
) *Handle
= INVALID_HANDLE_VALUE
;
2727 if (overlapped
) ((IO_STATUS_BLOCK
*) overlapped
)->u
.Status
= STATUS_PENDING
;
2728 return ERROR_IO_PENDING
;
2732 /******************************************************************
2733 * NotifyIpInterfaceChange (IPHLPAPI.@)
2735 DWORD WINAPI
NotifyIpInterfaceChange(ADDRESS_FAMILY family
, PIPINTERFACE_CHANGE_CALLBACK callback
,
2736 PVOID context
, BOOLEAN init_notify
, PHANDLE handle
)
2738 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2739 family
, callback
, context
, init_notify
, handle
);
2740 if (handle
) *handle
= NULL
;
2741 return ERROR_NOT_SUPPORTED
;
2745 /******************************************************************
2746 * NotifyRouteChange (IPHLPAPI.@)
2748 * Notify caller whenever the ip routing table is changed.
2751 * Handle [Out] handle usable in asynchronous notification
2752 * overlapped [In] overlapped structure that notifies the caller
2756 * Failure: error code from winerror.h
2759 * Stub, returns ERROR_NOT_SUPPORTED.
2761 DWORD WINAPI
NotifyRouteChange(PHANDLE Handle
, LPOVERLAPPED overlapped
)
2763 FIXME("(Handle %p, overlapped %p): stub\n", Handle
, overlapped
);
2764 return ERROR_NOT_SUPPORTED
;
2768 /******************************************************************
2769 * NotifyUnicastIpAddressChange (IPHLPAPI.@)
2771 DWORD WINAPI
NotifyUnicastIpAddressChange(ADDRESS_FAMILY family
, PUNICAST_IPADDRESS_CHANGE_CALLBACK callback
,
2772 PVOID context
, BOOLEAN init_notify
, PHANDLE handle
)
2774 FIXME("(family %d, callback %p, context %p, init_notify %d, handle %p): stub\n",
2775 family
, callback
, context
, init_notify
, handle
);
2776 if (handle
) *handle
= NULL
;
2777 return ERROR_NOT_SUPPORTED
;
2780 /******************************************************************
2781 * SendARP (IPHLPAPI.@)
2783 * Send an ARP request.
2786 * DestIP [In] attempt to obtain this IP
2787 * SrcIP [In] optional sender IP address
2788 * pMacAddr [Out] buffer for the mac address
2789 * PhyAddrLen [In/Out] length of the output buffer
2793 * Failure: error code from winerror.h
2796 * Stub, returns ERROR_NOT_SUPPORTED.
2798 DWORD WINAPI
SendARP(IPAddr DestIP
, IPAddr SrcIP
, PULONG pMacAddr
, PULONG PhyAddrLen
)
2800 FIXME("(DestIP 0x%08x, SrcIP 0x%08x, pMacAddr %p, PhyAddrLen %p): stub\n",
2801 DestIP
, SrcIP
, pMacAddr
, PhyAddrLen
);
2802 return ERROR_NOT_SUPPORTED
;
2806 /******************************************************************
2807 * SetIfEntry (IPHLPAPI.@)
2809 * Set the administrative status of an interface.
2812 * pIfRow [In] dwAdminStatus member specifies the new status.
2816 * Failure: error code from winerror.h
2819 * Stub, returns ERROR_NOT_SUPPORTED.
2821 DWORD WINAPI
SetIfEntry(PMIB_IFROW pIfRow
)
2823 FIXME("(pIfRow %p): stub\n", pIfRow
);
2824 /* this is supposed to set an interface administratively up or down.
2825 Could do SIOCSIFFLAGS and set/clear IFF_UP, but, not sure I want to, and
2826 this sort of down is indistinguishable from other sorts of down (e.g. no
2828 return ERROR_NOT_SUPPORTED
;
2832 /******************************************************************
2833 * SetIpForwardEntry (IPHLPAPI.@)
2835 * Modify an existing route.
2838 * pRoute [In] route with the new information
2842 * Failure: error code from winerror.h
2845 * Stub, returns NO_ERROR.
2847 DWORD WINAPI
SetIpForwardEntry(PMIB_IPFORWARDROW pRoute
)
2849 FIXME("(pRoute %p): stub\n", pRoute
);
2850 /* this is to add a route entry, how's it distinguishable from
2851 CreateIpForwardEntry?
2852 could use SIOCADDRT, not sure I want to */
2857 /******************************************************************
2858 * SetIpNetEntry (IPHLPAPI.@)
2860 * Modify an existing ARP entry.
2863 * pArpEntry [In] ARP entry with the new information
2867 * Failure: error code from winerror.h
2870 * Stub, returns NO_ERROR.
2872 DWORD WINAPI
SetIpNetEntry(PMIB_IPNETROW pArpEntry
)
2874 FIXME("(pArpEntry %p): stub\n", pArpEntry
);
2875 /* same as CreateIpNetEntry here, could use SIOCSARP, not sure I want to */
2880 /******************************************************************
2881 * SetIpStatistics (IPHLPAPI.@)
2883 * Toggle IP forwarding and det the default TTL value.
2886 * pIpStats [In] IP statistics with the new information
2890 * Failure: error code from winerror.h
2893 * Stub, returns NO_ERROR.
2895 DWORD WINAPI
SetIpStatistics(PMIB_IPSTATS pIpStats
)
2897 FIXME("(pIpStats %p): stub\n", pIpStats
);
2902 /******************************************************************
2903 * SetIpTTL (IPHLPAPI.@)
2905 * Set the default TTL value.
2908 * nTTL [In] new TTL value
2912 * Failure: error code from winerror.h
2915 * Stub, returns NO_ERROR.
2917 DWORD WINAPI
SetIpTTL(UINT nTTL
)
2919 FIXME("(nTTL %d): stub\n", nTTL
);
2920 /* could echo nTTL > /proc/net/sys/net/ipv4/ip_default_ttl, not sure I
2921 want to. Could map EACCESS to ERROR_ACCESS_DENIED, I suppose */
2926 /******************************************************************
2927 * SetTcpEntry (IPHLPAPI.@)
2929 * Set the state of a TCP connection.
2932 * pTcpRow [In] specifies connection with new state
2936 * Failure: error code from winerror.h
2939 * Stub, returns NO_ERROR.
2941 DWORD WINAPI
SetTcpEntry(PMIB_TCPROW pTcpRow
)
2943 FIXME("(pTcpRow %p): stub\n", pTcpRow
);
2947 /******************************************************************
2948 * SetPerTcpConnectionEStats (IPHLPAPI.@)
2950 DWORD WINAPI
SetPerTcpConnectionEStats(PMIB_TCPROW row
, TCP_ESTATS_TYPE state
, PBYTE rw
,
2951 ULONG version
, ULONG size
, ULONG offset
)
2953 FIXME("(row %p, state %d, rw %p, version %u, size %u, offset %u): stub\n",
2954 row
, state
, rw
, version
, size
, offset
);
2955 return ERROR_NOT_SUPPORTED
;
2959 /******************************************************************
2960 * UnenableRouter (IPHLPAPI.@)
2962 * Decrement the IP-forwarding reference count. Turn off IP-forwarding
2963 * if it reaches zero.
2966 * pOverlapped [In/Out] should be the same as in EnableRouter()
2967 * lpdwEnableCount [Out] optional, receives reference count
2971 * Failure: error code from winerror.h
2974 * Stub, returns ERROR_NOT_SUPPORTED.
2976 DWORD WINAPI
UnenableRouter(OVERLAPPED
* pOverlapped
, LPDWORD lpdwEnableCount
)
2978 FIXME("(pOverlapped %p, lpdwEnableCount %p): stub\n", pOverlapped
,
2980 /* could echo "0" > /proc/net/sys/net/ipv4/ip_forward, not sure I want to
2981 could map EACCESS to ERROR_ACCESS_DENIED, I suppose
2983 return ERROR_NOT_SUPPORTED
;
2986 /******************************************************************
2987 * PfCreateInterface (IPHLPAPI.@)
2989 DWORD WINAPI
PfCreateInterface(DWORD dwName
, PFFORWARD_ACTION inAction
, PFFORWARD_ACTION outAction
,
2990 BOOL bUseLog
, BOOL bMustBeUnique
, INTERFACE_HANDLE
*ppInterface
)
2992 FIXME("(%d %d %d %x %x %p) stub\n", dwName
, inAction
, outAction
, bUseLog
, bMustBeUnique
, ppInterface
);
2993 return ERROR_CALL_NOT_IMPLEMENTED
;
2996 /******************************************************************
2997 * PfUnBindInterface (IPHLPAPI.@)
2999 DWORD WINAPI
PfUnBindInterface(INTERFACE_HANDLE interface
)
3001 FIXME("(%p) stub\n", interface
);
3002 return ERROR_CALL_NOT_IMPLEMENTED
;
3005 /******************************************************************
3006 * PfDeleteInterface(IPHLPAPI.@)
3008 DWORD WINAPI
PfDeleteInterface(INTERFACE_HANDLE interface
)
3010 FIXME("(%p) stub\n", interface
);
3011 return ERROR_CALL_NOT_IMPLEMENTED
;
3014 /******************************************************************
3015 * PfBindInterfaceToIPAddress(IPHLPAPI.@)
3017 DWORD WINAPI
PfBindInterfaceToIPAddress(INTERFACE_HANDLE interface
, PFADDRESSTYPE type
, PBYTE ip
)
3019 FIXME("(%p %d %p) stub\n", interface
, type
, ip
);
3020 return ERROR_CALL_NOT_IMPLEMENTED
;
3023 /******************************************************************
3024 * GetTcpTable2 (IPHLPAPI.@)
3026 ULONG WINAPI
GetTcpTable2(PMIB_TCPTABLE2 table
, PULONG size
, BOOL order
)
3028 FIXME("pTcpTable2 %p, pdwSize %p, bOrder %d: stub\n", table
, size
, order
);
3029 return ERROR_NOT_SUPPORTED
;
3032 /******************************************************************
3033 * GetTcp6Table (IPHLPAPI.@)
3035 ULONG WINAPI
GetTcp6Table(PMIB_TCP6TABLE table
, PULONG size
, BOOL order
)
3037 FIXME("pTcp6Table %p, size %p, order %d: stub\n", table
, size
, order
);
3038 return ERROR_NOT_SUPPORTED
;
3041 /******************************************************************
3042 * GetTcp6Table2 (IPHLPAPI.@)
3044 ULONG WINAPI
GetTcp6Table2(PMIB_TCP6TABLE2 table
, PULONG size
, BOOL order
)
3046 FIXME("pTcp6Table2 %p, size %p, order %d: stub\n", table
, size
, order
);
3047 return ERROR_NOT_SUPPORTED
;
3050 /******************************************************************
3051 * ConvertInterfaceGuidToLuid (IPHLPAPI.@)
3053 DWORD WINAPI
ConvertInterfaceGuidToLuid(const GUID
*guid
, NET_LUID
*luid
)
3058 TRACE("(%s %p)\n", debugstr_guid(guid
), luid
);
3060 if (!guid
|| !luid
) return ERROR_INVALID_PARAMETER
;
3062 row
.dwIndex
= guid
->Data1
;
3063 if ((ret
= GetIfEntry( &row
))) return ret
;
3065 luid
->Info
.Reserved
= 0;
3066 luid
->Info
.NetLuidIndex
= guid
->Data1
;
3067 luid
->Info
.IfType
= row
.dwType
;
3071 /******************************************************************
3072 * ConvertInterfaceIndexToLuid (IPHLPAPI.@)
3074 DWORD WINAPI
ConvertInterfaceIndexToLuid(NET_IFINDEX index
, NET_LUID
*luid
)
3078 TRACE("(%u %p)\n", index
, luid
);
3080 if (!luid
) return ERROR_INVALID_PARAMETER
;
3081 memset( luid
, 0, sizeof(*luid
) );
3083 row
.dwIndex
= index
;
3084 if (GetIfEntry( &row
)) return ERROR_FILE_NOT_FOUND
;
3086 luid
->Info
.Reserved
= 0;
3087 luid
->Info
.NetLuidIndex
= index
;
3088 luid
->Info
.IfType
= row
.dwType
;
3092 /******************************************************************
3093 * ConvertInterfaceLuidToGuid (IPHLPAPI.@)
3095 DWORD WINAPI
ConvertInterfaceLuidToGuid(const NET_LUID
*luid
, GUID
*guid
)
3100 TRACE("(%p %p)\n", luid
, guid
);
3102 if (!luid
|| !guid
) return ERROR_INVALID_PARAMETER
;
3104 row
.dwIndex
= luid
->Info
.NetLuidIndex
;
3105 if ((ret
= GetIfEntry( &row
))) return ret
;
3107 memset( guid
, 0, sizeof(*guid
) );
3108 guid
->Data1
= luid
->Info
.NetLuidIndex
;
3112 /******************************************************************
3113 * ConvertInterfaceLuidToIndex (IPHLPAPI.@)
3115 DWORD WINAPI
ConvertInterfaceLuidToIndex(const NET_LUID
*luid
, NET_IFINDEX
*index
)
3120 TRACE("(%p %p)\n", luid
, index
);
3122 if (!luid
|| !index
) return ERROR_INVALID_PARAMETER
;
3124 row
.dwIndex
= luid
->Info
.NetLuidIndex
;
3125 if ((ret
= GetIfEntry( &row
))) return ret
;
3127 *index
= luid
->Info
.NetLuidIndex
;
3131 /******************************************************************
3132 * ConvertInterfaceLuidToNameA (IPHLPAPI.@)
3134 DWORD WINAPI
ConvertInterfaceLuidToNameA(const NET_LUID
*luid
, char *name
, SIZE_T len
)
3139 TRACE("(%p %p %u)\n", luid
, name
, (DWORD
)len
);
3141 if (!luid
) return ERROR_INVALID_PARAMETER
;
3143 row
.dwIndex
= luid
->Info
.NetLuidIndex
;
3144 if ((ret
= GetIfEntry( &row
))) return ret
;
3146 if (!name
|| len
< WideCharToMultiByte( CP_UNIXCP
, 0, row
.wszName
, -1, NULL
, 0, NULL
, NULL
))
3147 return ERROR_NOT_ENOUGH_MEMORY
;
3149 WideCharToMultiByte( CP_UNIXCP
, 0, row
.wszName
, -1, name
, len
, NULL
, NULL
);
3153 /******************************************************************
3154 * ConvertInterfaceLuidToNameW (IPHLPAPI.@)
3156 DWORD WINAPI
ConvertInterfaceLuidToNameW(const NET_LUID
*luid
, WCHAR
*name
, SIZE_T len
)
3161 TRACE("(%p %p %u)\n", luid
, name
, (DWORD
)len
);
3163 if (!luid
|| !name
) return ERROR_INVALID_PARAMETER
;
3165 row
.dwIndex
= luid
->Info
.NetLuidIndex
;
3166 if ((ret
= GetIfEntry( &row
))) return ret
;
3168 if (len
< strlenW( row
.wszName
) + 1) return ERROR_NOT_ENOUGH_MEMORY
;
3169 strcpyW( name
, row
.wszName
);
3173 /******************************************************************
3174 * ConvertInterfaceNameToLuidA (IPHLPAPI.@)
3176 DWORD WINAPI
ConvertInterfaceNameToLuidA(const char *name
, NET_LUID
*luid
)
3182 TRACE("(%s %p)\n", debugstr_a(name
), luid
);
3184 if ((ret
= getInterfaceIndexByName( name
, &index
))) return ERROR_INVALID_NAME
;
3185 if (!luid
) return ERROR_INVALID_PARAMETER
;
3187 row
.dwIndex
= index
;
3188 if ((ret
= GetIfEntry( &row
))) return ret
;
3190 luid
->Info
.Reserved
= 0;
3191 luid
->Info
.NetLuidIndex
= index
;
3192 luid
->Info
.IfType
= row
.dwType
;
3196 /******************************************************************
3197 * ConvertInterfaceNameToLuidW (IPHLPAPI.@)
3199 DWORD WINAPI
ConvertInterfaceNameToLuidW(const WCHAR
*name
, NET_LUID
*luid
)
3204 char nameA
[IF_MAX_STRING_SIZE
+ 1];
3206 TRACE("(%s %p)\n", debugstr_w(name
), luid
);
3208 if (!luid
) return ERROR_INVALID_PARAMETER
;
3209 memset( luid
, 0, sizeof(*luid
) );
3211 if (!WideCharToMultiByte( CP_UNIXCP
, 0, name
, -1, nameA
, sizeof(nameA
), NULL
, NULL
))
3212 return ERROR_INVALID_NAME
;
3214 if ((ret
= getInterfaceIndexByName( nameA
, &index
))) return ret
;
3216 row
.dwIndex
= index
;
3217 if ((ret
= GetIfEntry( &row
))) return ret
;
3219 luid
->Info
.Reserved
= 0;
3220 luid
->Info
.NetLuidIndex
= index
;
3221 luid
->Info
.IfType
= row
.dwType
;
3225 /******************************************************************
3226 * ConvertLengthToIpv4Mask (IPHLPAPI.@)
3228 DWORD WINAPI
ConvertLengthToIpv4Mask(ULONG mask_len
, ULONG
*mask
)
3232 *mask
= INADDR_NONE
;
3233 return ERROR_INVALID_PARAMETER
;
3239 *mask
= htonl(~0u << (32 - mask_len
));
3244 /******************************************************************
3245 * if_nametoindex (IPHLPAPI.@)
3247 IF_INDEX WINAPI
IPHLP_if_nametoindex(const char *name
)
3251 TRACE("(%s)\n", name
);
3252 if (getInterfaceIndexByName(name
, &idx
) == NO_ERROR
)
3258 /******************************************************************
3259 * if_indextoname (IPHLPAPI.@)
3261 PCHAR WINAPI
IPHLP_if_indextoname(NET_IFINDEX index
, PCHAR name
)
3263 TRACE("(%u, %p)\n", index
, name
);
3265 return getInterfaceNameByIndex(index
, name
);