#pragma warning disable IDE1006 // Naming Styles
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Vanara.Extensions;
using Vanara.InteropServices;
namespace Vanara.PInvoke
{
public static partial class Ws2_32
{
/// An optional pointer to a function to be invoked upon successful completion for asynchronous operations.
/// Set to a Winsock error code.
/// Reserved for future use and must be ignored.
///
/// the value of lpOverlapped parameter passed to GetAddrInfoEx. The Pointer member of the OVERLAPPED structure will be set to the
/// value of the ppResult parameter of the original call. If the Pointer member points to a non-NULL pointer to the addrinfoex
/// structure, it is the caller’s responsibility to call FreeAddrInfoEx to free the addrinfoex structure.
///
[PInvokeData("ws2tcpip.h", MSDNShortId = "cc4ccb2d-ea5a-48bd-a3ae-f70432ab2c39")]
public unsafe delegate void LPLOOKUPSERVICE_COMPLETION_ROUTINE(uint dwError, uint dwBytes, NativeOverlapped* lpOverlapped);
///
/// The FreeAddrInfoEx function frees address information that the GetAddrInfoEx function dynamically allocates in addrinfoex structures.
///
///
/// A pointer to the addrinfoex structure or linked list of addrinfoex structures to be freed. All dynamic storage pointed to
/// within the addrinfoex structure or structures is also freed.
///
/// This function does not return a value.
///
///
/// The FreeAddrInfoEx function frees addrinfoex structures dynamically allocated by the GetAddrInfoEx function. The
/// FreeAddrInfoEx function frees the initial addrinfoex structure pointed to in the pAddrInfo parameter, including
/// any buffers to which structure members point, then continues freeing any addrinfoex structures linked by the
/// ai_next member of the addrinfoex structure. The FreeAddrInfoEx function continues freeing linked structures
/// until a NULLai_next member is encountered.
///
///
/// When UNICODE or _UNICODE is defined, FreeAddrInfoEx is defined to FreeAddrInfoExW, the Unicode version of the
/// function, and ADDRINFOEX is defined to the addrinfoexW structure. When UNICODE or _UNICODE is not defined,
/// FreeAddrInfoEx is defined to FreeAddrInfoExA, the ANSI version of the function, and ADDRINFOEX is defined
/// to the addrinfoexA structure.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The FreeAddrInfoExW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-freeaddrinfoex void WSAAPI FreeAddrInfoEx( PADDRINFOEXA
// pAddrInfoEx );
[DllImport(Lib.Ws2_32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "bc3d7ba7-ec00-4ee0-ad7d-d46641043a7b")]
public static extern void FreeAddrInfoExW(IntPtr pAddrInfoEx);
///
/// The FreeAddrInfoW function frees address information that the GetAddrInfoW function dynamically allocates in addrinfoW structures.
///
///
/// A pointer to the addrinfoW structure or linked list of addrinfoW structures to be freed. All dynamic storage pointed to
/// within the addrinfoW structure or structures is also freed.
///
///
///
/// The FreeAddrInfoW function frees addrinfoW structures dynamically allocated by the Unicode GetAddrInfoW function. The
/// FreeAddrInfoW function frees the initial addrinfoW structure pointed to in the pAddrInfo parameter, including any
/// buffers to which structure members point, then continues freeing any addrinfoW structures linked by the ai_next
/// member of the addrinfoW structure. The FreeAddrInfoW function continues freeing linked structures until a
/// NULLai_next member is encountered.
///
///
/// Macros in the Winsock header file define a mixed-case function name of FreeAddrInfo and an ADDRINFOT structure.
/// This FreeAddrInfo function should be called with the pAddrInfo parameter of a pointer of type ADDRINFOT. When
/// UNICODE or _UNICODE is defined, FreeAddrInfo is defined to FreeAddrInfoW, the Unicode version of the function, and
/// ADDRINFOT is defined to the addrinfoW structure. When UNICODE or _UNICODE is not defined, FreeAddrInfo is defined
/// to freeaddrinfo, the ANSI version of the function, and ADDRINFOT is defined to the addrinfo structure.
///
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-freeaddrinfow VOID WSAAPI FreeAddrInfoW( PADDRINFOW
// pAddrInfo );
[DllImport(Lib.Ws2_32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "0a2a226c-2068-4538-b499-04cfbfd65b8a")]
public static extern void FreeAddrInfoW(IntPtr pAddrInfo);
///
/// The gai_strerror function assists in printing error messages based on the EAI_* errors returned by the getaddrinfo
/// function. Note that the gai_strerror function is not thread safe, and therefore, use of traditional Windows Sockets
/// functions such as the WSAGetLastError function is recommended.
///
///
/// Error code from the list of available getaddrinfo error codes. For a complete listing of error codes, see the getaddrinfo function.
///
/// None
///
/// If the ecode parameter is not an error code value that getaddrinfo returns, the gai_strerror function returns a pointer
/// to a string that indicates an unknown error.
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-gai_strerrora char * gai_strerrorA( int ecode );
[PInvokeData("ws2tcpip.h", MSDNShortId = "00b4c5de-89c9-419f-bff8-822ef0446697")]
public static string gai_strerror(int ecode) =>
Kernel32.FormatMessage((uint)ecode, flags: Kernel32.FormatMessageFlags.FORMAT_MESSAGE_MAX_WIDTH_MASK);
/// The GetAddrInfoExCancel function cancels an asynchronous operation by the GetAddrInfoEx function.
///
/// The handle of the asynchronous operation to cancel. This is the handle returned in the lpNameHandle parameter by the
/// GetAddrInfoEx function.
///
///
/// On success, GetAddrInfoExCancel returns NO_ERROR (0). Failure returns a nonzero Windows Sockets error code, as
/// found in the Windows Sockets Error Codes.
///
///
///
/// The GetAddrInfoExCancel function cancels an asynchronous GetAddrInfoEx operation. The result is that the user's
/// completion mechanism, either a callback or an event, is immediately invoked. No results are returned, and the error code
/// returned for the GetAddrInfoEx asynchronous operation is set to WSA_E_CANCELLED. If the GetAddrInfoEx
/// request has already completed or timed out, or the handle is invalid, and WSA_INVALID_HANDLE will be returned by
/// GetAddrInfoExCancel function.
///
///
/// Since many of the underlying operations (legacy name service providers, for example) are synchronous, these operations will not
/// actually be cancelled. These operations will continue running and consuming resources. Once the last outstanding name service
/// provider request has completed, the resources will be released.
///
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfoexcancel INT WSAAPI GetAddrInfoExCancel(
// LPHANDLE lpHandle );
[DllImport(Lib.Ws2_32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "A4DE552D-DEA7-44F5-865F-5B02C9BB4AB6")]
public static extern Win32Error GetAddrInfoExCancel(in HANDLE lpHandle);
///
/// The GetAddrInfoExOverlappedResult function gets the return code for an OVERLAPPED structure used by an
/// asynchronous operation for the GetAddrInfoEx function.
///
/// A pointer to an OVERLAPPED structure for the asynchronous operation.
///
/// On success, the GetAddrInfoExOverlappedResult function returns NO_ERROR (0). When the underlying operation hasn't
/// yet completed, the GetAddrInfoExOverlappedResult function returns WSAEINPROGRESS. On failure, the
/// GetAddrInfoExOverlappedResult function returns WSAEINVAL.
///
///
/// The GetAddrInfoExOverlappedResult function is used with the GetAddrInfoEx function for asynchronous operations.
///
/// If the GetAddrInfoExOverlappedResult function returns WSAEINVAL, the only way to distinguish whether
/// GetAddrInfoExOverlappedResult function or the asynchronous operation returned the error is to check that the lpOverlapped
/// parameter was not NULL. If the lpOverlapped parameter was NULL, then the GetAddrInfoExOverlappedResult function was
/// passed a NULL pointer and failed.
///
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfoexoverlappedresult INT WSAAPI
// GetAddrInfoExOverlappedResult( LPOVERLAPPED lpOverlapped );
[DllImport(Lib.Ws2_32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "BBA6E407-561C-4B3C-9218-0047477E82DE")]
public static extern Win32Error GetAddrInfoExOverlappedResult(IntPtr lpOverlapped);
///
/// The GetAddrInfoExOverlappedResult function gets the return code for an OVERLAPPED structure used by an
/// asynchronous operation for the GetAddrInfoEx function.
///
/// A pointer to an OVERLAPPED structure for the asynchronous operation.
///
/// On success, the GetAddrInfoExOverlappedResult function returns NO_ERROR (0). When the underlying operation hasn't
/// yet completed, the GetAddrInfoExOverlappedResult function returns WSAEINPROGRESS. On failure, the
/// GetAddrInfoExOverlappedResult function returns WSAEINVAL.
///
///
/// The GetAddrInfoExOverlappedResult function is used with the GetAddrInfoEx function for asynchronous operations.
///
/// If the GetAddrInfoExOverlappedResult function returns WSAEINVAL, the only way to distinguish whether
/// GetAddrInfoExOverlappedResult function or the asynchronous operation returned the error is to check that the lpOverlapped
/// parameter was not NULL. If the lpOverlapped parameter was NULL, then the GetAddrInfoExOverlappedResult function was
/// passed a NULL pointer and failed.
///
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfoexoverlappedresult INT WSAAPI
// GetAddrInfoExOverlappedResult( LPOVERLAPPED lpOverlapped );
[DllImport(Lib.Ws2_32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "BBA6E407-561C-4B3C-9218-0047477E82DE")]
public static unsafe extern Win32Error GetAddrInfoExOverlappedResult(NativeOverlapped* lpOverlapped);
///
/// The GetAddrInfoEx function provides protocol-independent name resolution with additional parameters to qualify which
/// namespace providers should handle the request.
///
///
/// A pointer to a NULL-terminated string containing a host (node) name or a numeric host address string. For the Internet
/// protocol, the numeric host address string is a dotted-decimal IPv4 address or an IPv6 hex address.
///
///
///
/// A pointer to an optional NULL-terminated string that contains either a service name or port number represented as a string.
///
///
/// A service name is a string alias for a port number. For example, “http” is an alias for port 80 defined by the Internet
/// Engineering Task Force (IETF) as the default port used by web servers for the HTTP protocol. Possible values for the
/// pServiceName parameter when a port number is not specified are listed in the following file:
///
/// %WINDIR%\system32\drivers\etc\services
///
///
///
/// An optional namespace identifier that determines which namespace providers are queried. Passing a specific namespace identifier
/// will result in only namespace providers that support the specified namespace being queried. Specifying NS_ALL will result
/// in all installed and active namespace providers being queried.
///
///
/// Options for the dwNameSpace parameter are listed in the Winsock2.h include file. Several namespace providers are added on
/// Windows Vista and later. Other namespace providers can be installed, so the following possible values are only those commonly
/// available. Many other values are possible.
///
///
///
/// Value
/// Meaning
///
/// -
/// NS_ALL 0
/// All installed and active namespaces.
///
/// -
/// NS_DNS 12
/// The domain name system (DNS) namespace.
///
/// -
/// NS_NETBT 13
/// The NetBIOS over TCP/IP (NETBT) namespace.
///
/// -
/// NS_WINS 14
/// The Windows Internet Naming Service (NS_WINS) namespace.
///
/// -
/// NS_NLA 15
/// The network location awareness (NLA) namespace. This namespace identifier is supported on Windows XP and later.
///
/// -
/// NS_BTH 16
/// The Bluetooth namespace. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_NTDS 32
/// The Windows NT Directory Services (NS_NTDS) namespace.
///
/// -
/// NS_EMAIL 37
/// The email namespace. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_PNRPNAME 38
/// The peer-to-peer namespace for a specific peer name. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_PNRPCLOUD 39
///
/// The peer-to-peer namespace for a collection of peer names. This namespace identifier is supported on Windows Vista and later.
///
///
///
///
///
/// A pointer to an optional GUID of a specific namespace provider to query in the case where multiple namespace providers are
/// registered under a single namespace such as NS_DNS. Passing the GUID for specific namespace provider will result in only
/// the specified namespace provider being queried. The WSAEnumNameSpaceProviders function can be called to retrieve the GUID for a
/// namespace provider.
///
///
/// A pointer to an addrinfoex structure that provides hints about the type of socket the caller supports.
///
/// The ai_addrlen, ai_canonname, ai_addr, and ai_next members of the addrinfoex structure pointed to by
/// the pHints parameter must be zero or NULL. Otherwise the GetAddrInfoEx function will fail with WSANO_RECOVERY.
///
/// See the Remarks for more details.
///
///
/// A pointer to a linked list of one or more addrinfoex structures that contains response information about the host.
///
///
///
/// An optional parameter indicating the time, in milliseconds, to wait for a response from the namespace provider before aborting
/// the call.
///
///
/// This parameter is only supported when the UNICODE or _UNICODE macro has been defined in the sources before calling
/// the GetAddrInfoEx function. Otherwise, this parameter is currently reserved and must be set to NULL since a
/// timeout option is not supported.
///
///
///
/// An optional pointer to an overlapped structure used for asynchronous operation.
///
/// This parameter is only supported when the UNICODE or _UNICODE macro has been defined in the sources before calling
/// the GetAddrInfoEx function.
///
///
/// On Windows 8 and Windows Server 2012, if no lpCompletionRoutine parameter is specified, the hEvent member of the
/// OVERLAPPED structure must be set to a manual-reset event to be called upon completion of an asynchronous call. If a
/// completion routine has been specified, the hEvent member must be NULL. When the event specified by hEvent has been
/// set, the result of the operation can be retrieved by calling GetAddrInfoExOverlappedResult function.
///
///
/// On Windows 8 and Windows Server 2012 whenever the UNICODE or _UNICODE macro is not defined, this parameter is
/// currently reserved and must be set to NULL.
///
///
/// On Windows 7 and Windows Server 2008 R2 or earlier, this parameter is currently reserved and must be set to NULL since
/// asynchronous operations are not supported.
///
///
///
/// An optional pointer to a function to be invoked upon successful completion for asynchronous operations.
///
/// This parameter is only supported when the UNICODE or _UNICODE macro has been defined in the sources before calling
/// the GetAddrInfoEx function.
///
///
/// On Windows 8 and Windows Server 2012, if this parameter is specified, it must be a pointer to a function with the following signature:
///
///
/// When the asynchronous operation has completed, the completion routine will be invoked with lpOverlapped parameter set to the
/// value of lpOverlapped parameter passed to GetAddrInfoEx. The Pointer member of the OVERLAPPED structure will be
/// set to the value of the ppResult parameter of the original call. If the Pointer member points to a non-NULL pointer to
/// the addrinfoex structure, it is the caller’s responsibility to call FreeAddrInfoEx to free the addrinfoex structure. The
/// dwError parameter passed to the completion routine will be set to a Winsock error code. The dwBytes parameter is reserved for
/// future use and must be ignored.
///
///
/// On Windows 8 and Windows Server 2012 whenever the UNICODE or _UNICODE macro is not defined, this parameter is
/// currently reserved and must be set to NULL.
///
///
/// On Windows 7 and Windows Server 2008 R2 or earlier, this parameter is currently reserved and must be set to NULL since
/// asynchronous operations are not supported.
///
///
///
/// An optional pointer used only for asynchronous operations.
///
/// This parameter is only supported when the UNICODE or _UNICODE macro has been defined in the sources before calling
/// the GetAddrInfoEx function.
///
///
/// On Windows 8 and Windows Server 2012, if the GetAddrInfoEx function will complete asynchronously, the pointer returned in
/// this field may be used with the GetAddrInfoExCancel function. The handle returned is valid when GetAddrInfoEx
/// returns until the completion routine is called, the event is triggered, or GetAddrInfoExCancel function is called with this handle.
///
///
/// On Windows 8 and Windows Server 2012 whenever the UNICODE or _UNICODE macro is not defined, this parameter is
/// currently reserved and must be set to NULL.
///
///
/// On Windows 7 and Windows Server 2008 R2 or earlier, this parameter is currently reserved and must be set to NULL since
/// asynchronous operations are not supported.
///
///
///
///
/// On success, GetAddrInfoEx returns NO_ERROR (0). Failure returns a nonzero Windows Sockets error code, as found in
/// the Windows Sockets Error Codes.
///
///
/// Most nonzero error codes returned by the GetAddrInfoEx function map to the set of errors outlined by Internet Engineering
/// Task Force (IETF) recommendations. The following table shows these error codes and their WSA equivalents. It is recommended that
/// the WSA error codes be used, as they offer familiar and comprehensive error information for Winsock programmers.
///
///
///
/// Error value
/// WSA equivalent
/// Description
///
/// -
/// EAI_AGAIN
/// WSATRY_AGAIN
/// A temporary failure in name resolution occurred.
///
/// -
/// EAI_BADFLAGS
/// WSAEINVAL
///
/// An invalid parameter was provided. This error is returned if any of the reserved parameters are not NULL. This error is also
/// returned if an invalid value was provided for the ai_flags member of the pHints parameter.
///
///
/// -
/// EAI_FAIL
/// WSANO_RECOVERY
/// A nonrecoverable failure in name resolution occurred.
///
/// -
/// EAI_FAMILY
/// WSAEAFNOSUPPORT
/// The ai_family member of the pHints parameter is not supported.
///
/// -
/// EAI_MEMORY
/// WSA_NOT_ENOUGH_MEMORY
/// A memory allocation failure occurred.
///
/// -
/// EAI_NONAME
/// WSAHOST_NOT_FOUND
/// The name does not resolve for the supplied parameters or the pName and pServiceName parameters were not provided.
///
/// -
/// EAI_SERVICE
/// WSATYPE_NOT_FOUND
/// The pServiceName parameter is not supported for the specified ai_socktype member of the pHints parameter.
///
/// -
/// EAI_SOCKTYPE
/// WSAESOCKTNOSUPPORT
/// The ai_socktype member of the pHints parameter is not supported.
///
///
///
/// Use the gai_strerror function to print error messages based on the EAI codes returned by the GetAddrInfoEx function. The
/// gai_strerror function is provided for compliance with IETF recommendations, but it is not thread safe. Therefore, use of
/// traditional Windows Sockets functions such as WSAGetLastError is recommended.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSA_NOT_ENOUGH_MEMORY
/// There was insufficient memory to perform the operation.
///
/// -
/// WSAEAFNOSUPPORT
///
/// An address incompatible with the requested protocol was used. This error is returned if the ai_family member of the
/// addrinfoexstructure pointed to by the pHints parameter is not supported.
///
///
/// -
/// WSAEINVAL
///
/// An invalid argument was supplied. This error is returned if an invalid value was provided for the ai_flags member of the
/// addrinfoex structure pointed to by the pHints parameter. This error is also returned when the dwNameSpace parameter is
/// NS_PNRPNAME or NS_PNRPCLOUD and the peer-to-peer name service is not operating.
///
///
/// -
/// WSAESOCKTNOSUPPORT
///
/// The support for the specified socket type does not exist in this address family. This error is returned if the ai_socktype
/// member of the addrinfoex structure pointed to by the pHints parameter is not supported.
///
///
/// -
/// WSAHOST_NOT_FOUND
///
/// No such host is known. This error is returned if the name does not resolve for the supplied parameters or the pName and
/// pServiceName parameters were not provided.
///
///
/// -
/// WSANO_DATA
/// The requested name is valid, but no data of the requested type was found.
///
/// -
/// WSANO_RECOVERY
///
/// A nonrecoverable error occurred during a database lookup. This error is returned if nonrecoverable error in name resolution occurred.
///
///
/// -
/// WSANOTINITIALISED
/// A successful WSAStartup call must occur before using this function.
///
/// -
/// WSASERVICE_NOT_FOUND
///
/// No such service is known. The service cannot be found in the specified name space. This error is returned if the pName or
/// pServiceName parameter is not found for the namespace specified in the dwNameSpace parameter or the namespace specified in the
/// dwNameSpace parameter is not installed.
///
///
/// -
/// WSATRY_AGAIN
///
/// This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an
/// authoritative server. This error is returned when a temporary failure in name resolution occurred.
///
///
/// -
/// WSATYPE_NOT_FOUND
///
/// The specified class was not found. The pServiceName parameter is not supported for the specified ai_socktype member of the
/// addrinfoexstructure pointed to by the pHints parameter.
///
///
///
///
///
///
/// The GetAddrInfoEx function provides protocol-independent translation from host name to address and from service name to
/// port number. The GetAddrInfoEx function is an enhanced version of the getaddrinfo and GetAddrInfoW functions. The
/// GetAddrInfoEx function allows specifying the namespace provider to resolve the query.
///
///
/// The GetAddrInfoEx function aggregates and returns results from multiple namespace providers, unless a specific namespace
/// provider is specified. For use with the IPv6 and IPv4 protocol, name resolution can be by the Domain Name System (DNS), a local
/// hosts file, an email provider (the NS_EMAIL namespace), or by other naming mechanisms.
///
///
/// When UNICODE or _UNICODE is defined, GetAddrInfoEx is defined to GetAddrInfoExW, the Unicode version of this
/// function. The string parameters are defined to the PWSTR data type and the ADDRINFOEXW structure is used. On
/// Windows 8 and Windows Server 2012, the timeout, lpOverlapped, lpCompletionRoutine, and lpNameHandle parameters may be used to
/// call the GetAddrInfoEx function so that it can complete asynchronously.
///
///
/// When UNICODE or _UNICODE is not defined, GetAddrInfoEx is defined to GetAddrInfoExA, the ANSI version of this
/// function. The string parameters are of the PCSTR data type and the ADDRINFOEXA structure is used. The timeout,
/// lpOverlapped, lpCompletionRoutine, and lpNameHandle parameters must be set to NULL.
///
///
/// One or both of the pName or pServiceName parameters must point to a NULL-terminated string. Generally both are provided.
///
///
/// Upon success, a linked list of addrinfoex structures is returned in the ppResult parameter. The list can be processed by
/// following the pointer provided in the ai_next member of each returned addrinfoex structure until a NULL
/// pointer is encountered. In each returned addrinfoex structure, the ai_family, ai_socktype, and
/// ai_protocol members correspond to respective arguments in a socket or WSASocket function call. Also, the ai_addr
/// member in each returned addrinfoex structure points to a filled-in socket address structure, the length of which is
/// specified in its ai_addrlen member.
///
///
/// If the pName parameter points to a computer name, all permanent addresses for the computer that can be used as a source address
/// are returned. On Windows Vista and later, these addresses would include all unicast IP addresses returned by the
/// GetUnicastIpAddressTable or GetUnicastIpAddressEntry functions in which the SkipAsSource member is set to false in the
/// MIB_UNICASTIPADDRESS_ROW structure.
///
/// If the pName parameter points to a string equal to "localhost", all loopback addresses on the local computer are returned.
/// If the pName parameter contains an empty string, all registered addresses on the local computer are returned.
///
/// On Windows Server 2003 and later if the pName parameter points to a string equal to "..localmachine", all registered addresses
/// on the local computer are returned.
///
///
/// If the pName parameter refers to a cluster virtual server name, only virtual server addresses are returned. On Windows Vista and
/// later, these addresses would include all unicast IP addresses returned by the GetUnicastIpAddressTable or
/// GetUnicastIpAddressEntry functions in which the SkipAsSource member is set to true in the MIB_UNICASTIPADDRESS_ROW
/// structure. See Windows Clustering for more information about clustering.
///
///
/// Windows 7 with Service Pack 1 (SP1) and Windows Server 2008 R2 with Service Pack 1 (SP1) add support to Netsh.exe for setting
/// the SkipAsSource attribute on an IP address. This also changes the behavior such that if the SkipAsSource member in the
/// MIB_UNICASTIPADDRESS_ROW structure is set to false, the IP address will be registered in DNS. If the SkipAsSource member
/// is set to true, the IP address is not registered in DNS.
///
///
/// A hotfix is available for Windows 7 and Windows Server 2008 R2 that adds support to Netsh.exe for setting the SkipAsSource
/// attribute on an IP address. This hotfix also changes behavior such that if the SkipAsSource member in the
/// MIB_UNICASTIPADDRESS_ROW structure is set to false, the IP address will be registered in DNS. If the SkipAsSource member
/// is set to true, the IP address is not registered in DNS. For more information, see Knowledge Base (KB) 2386184.
///
///
/// A similar hotfix is also available for Windows Vista with Service Pack 2 (SP2) and Windows Server 2008 with Service Pack 2 (SP2)
/// that adds support to Netsh.exe for setting the SkipAsSource attribute on an IP address. This hotfix also changes behavior such
/// that if the SkipAsSource member in the MIB_UNICASTIPADDRESS_ROW structure is set to false, the IP address will be
/// registered in DNS. If the SkipAsSource member is set to true, the IP address is not registered in DNS. For more
/// information, see Knowledge Base (KB) 975808.
///
///
/// Callers of the GetAddrInfoEx function can provide hints about the type of socket supported through an addrinfoex
/// structure pointed to by the pHints parameter. When the pHints parameter is used, the following rules apply to its associated
/// addrinfoex structure:
///
///
/// -
///
/// A value of AF_UNSPEC for ai_family indicates the caller will accept only the AF_INET and AF_INET6
/// address families. Note that AF_UNSPEC and PF_UNSPEC are the same.
///
///
/// -
/// A value of zero for ai_socktype indicates the caller will accept any socket type.
///
/// -
/// A value of zero for ai_protocol indicates the caller will accept any protocol.
///
/// -
/// The ai_addrlen member must be set to zero.
///
/// -
/// The ai_canonname member must be set to NULL.
///
/// -
/// The ai_addr member must be set to NULL.
///
/// -
/// The ai_next member must be set to NULL.
///
///
///
/// Other values in the addrinfoex structure provided in the pHints parameter indicate specific requirements. For example, if the
/// caller handles only IPv4 and does not handle IPv6, the ai_family member should be set to AF_INET. For another
/// example, if the caller handles only TCP and does not handle UDP, the ai_socktype member should be set to SOCK_STREAM.
///
///
/// If the pHints parameter is a NULL pointer, the GetAddrInfoEx function treats it as if the addrinfoex structure in
/// pHints were initialized with its ai_family member set to AF_UNSPEC and all other members set to NULL or zero.
///
///
/// When GetAddrInfoEx is called from a service, if the operation is the result of a user process calling the service, the
/// service should impersonate the user. This is to allow security to be properly enforced.
///
///
/// The GetAddrInfoEx function can be used to convert a text string representation of an IP address to an addrinfoexstructure
/// that contains a sockaddr structure for the IP address and other information. To be used in this way, the string pointed to by
/// the pName parameter must contain a text representation of an IP address and the addrinfoex structure pointed to by the
/// pHints parameter must have the AI_NUMERICHOST flag set in the ai_flags member. The string pointed to by the pName
/// parameter may contain a text representation of either an IPv4 or an IPv6 address. The text IP address is converted to an
/// addrinfoex structure pointed to by the ppResult parameter. The returned addrinfoex structure contains a
/// sockaddr structure for the IP address along with additional information about the IP address.
///
///
/// Multiple namespace providers may be installed on a local computer for the same namespace. For example, the base Windows TCP/IP
/// networking software registers for the NS_DNS namespace. The Microsoft Forefront Threat Management Gateway (TMG) and the older
/// Microsoft Internet Security and Acceleration (ISA) Server include Firewall Client software that also registers for the NS_DNS
/// namespace. When the dwNameSpace parameter is set to a value (NS_DNS, for example) and the lpNspId parameter is NULL, the
/// results returned by the GetAddrInfoEx function are the merged results from all namespace providers that register for the
/// specified namespace with duplicate results eliminated. The lpNspId parameter should be set to the GUID of the specific namespace
/// provider if only a single namespace provider is to be queried.
///
///
/// If the pNameSpace parameter is set to NS_ALL, then the results from querying all namespace providers is merged and returned. In
/// this case, duplicate responses may be returned in the results pointed to by the ppResult parameter if multiple namespace
/// providers return the same information.
///
///
/// On Windows 8 and Windows Server 2012, if the GetAddrInfoEx function will complete asynchronously, the pointer returned in
/// the lpNameHandle parameter may be used with the GetAddrInfoExCancel function. The handle returned is valid when
/// GetAddrInfoEx returns until the completion routine is called, the event is triggered, or GetAddrInfoExCancel function is
/// called with this handle.
///
/// Freeing Address Information from Dynamic Allocation
///
/// All information returned by the GetAddrInfoEx function pointed to by the ppResult parameter is dynamically allocated,
/// including all addrinfoex structures, socket address structures, and canonical host name strings pointed to by addrinfoex
/// structures. Memory allocated by a successful call to this function must be released with a subsequent call to FreeAddrInfoEx.
///
/// Example Code
/// The following example demonstrates the use of the GetAddrInfoEx function.
///
/// The following example demonstrates how to use the GetAddrInfoEx function asynchronous to resolve a name to an IP address..
///
///
/// Note Ensure that the development environment targets the newest version of Ws2tcpip.h which includes structure and
/// function definitions for addrinfoex and GetAddrInfoEx, respectively.
///
/// Internationalized Domain Names
/// Internet host names typically consist of a very restricted set of characters:
///
/// -
/// Upper and lower case ASCII letters from the English alphabet.
///
/// -
/// Digits from 0 to 9.
///
/// -
/// ASCII hyphen characters.
///
///
///
/// With the growth of the Internet, there is a growing need to identify Internet host names for other languages not represented by
/// the ASCII character set. Identifiers which facilitate this need and allow non-ASCII characters (Unicode) to be represented as
/// special ASCII character strings are known as Internationalized Domain Names (IDNs). A mechanism called Internationalizing Domain
/// Names in Applications (IDNA) is used to handle IDNs in a standard fashion. The specifications for IDNs and IDNA are documented
/// in RFC 3490, RTF 5890, and RFC 6365 published by the Internet Engineering Task Force (IETF).
///
///
/// On Windows 8 and Windows Server 2012, the GetAddrInfoEx function provides support for Internationalized Domain Name (IDN)
/// parsing applied to the name passed in the pName parameter. Winsock performs Punycode/IDN encoding and conversion. This behavior
/// can be disabled using the AI_DISABLE_IDN_ENCODING flag discussed below.
///
///
/// On Windows 7 and Windows Server 2008 R2 or earlier, the GetAddrInfoEx function does not currently provide support for IDN
/// parsing applied to the name passed in the pName parameter. The wide character version of the GetAddrInfoEx function does
/// not use Punycode to convert an IDN Punycode format as per RFC 3490. The wide character version of the GetAddrInfoEx
/// function when querying DNS encodes the Unicode name in UTF-8 format, the format used by Microsoft DNS servers in an enterprise environment.
///
///
/// Several functions on Windows Vista and later support conversion between Unicode labels in an IDN to their ASCII equivalents. The
/// resulting representation of each Unicode label contains only ASCII characters and starts with the xn-- prefix if the Unicode
/// label contained any non-ASCII characters. The reason for this is to support existing DNS servers on the Internet, since some DNS
/// tools and servers only support ASCII characters (see RFC 3490).
///
///
/// The IdnToAscii function uses Punycode to convert an IDN to the ASCII representation of the original Unicode string using the
/// standard algorithm defined in RFC 3490. The IdnToUnicode function converts the ASCII form of an IDN to the normal Unicode UTF-16
/// encoding syntax. For more information and links to related draft standards, see Handling Internationalized Domain Names (IDNs).
///
///
/// The IdnToAscii function can be used to convert an IDN name to an ASCII form that then can be passed in the pName parameter to
/// the GetAddrInfoEx function when the ASCII version of this function is used (when UNICODE and _UNICODE are not defined).
/// To pass this IDN name to the GetAddrInfoEx function when the wide character version of this function is used (when
/// UNICODE or _UNICODE is defined), you can use the MultiByteToWideChar function to convert the CHAR string into a
/// WCHAR string.
///
/// Use of ai_flags in the hints parameter
///
/// Flags in the ai_flags member of the optional addrinfoex structure provided in the hints parameter modify the behavior of
/// the function.
///
///
/// These flag bits are defined in the Ws2def.h header file on the Microsoft Windows Software Development Kit (SDK) for Windows 7.
/// These flag bits are defined in the Ws2tcpip.h header file on the Windows SDK for Windows Server 2008 and Windows Vista. These
/// flag bits are defined in the Ws2tcpip.h header file on the Platform Software Development Kit (SDK) for Windows Server 2003, and
/// Windows XP.
///
/// The flag bits can be a combination of the following:
///
///
/// Flag Bits
/// Description
///
/// -
/// AI_PASSIVE
///
/// Setting the AI_PASSIVE flag indicates the caller intends to use the returned socket address structure in a call to the bind
/// function. When the AI_PASSIVE flag is set and pName is a NULL pointer, the IP address portion of the socket address structure is
/// set to INADDR_ANY for IPv4 addresses and IN6ADDR_ANY_INIT for IPv6 addresses. When the AI_PASSIVE flag is not set, the returned
/// socket address structure is ready for a call to the connect function for a connection-oriented protocol, or ready for a call to
/// either the connect, sendto, or send functions for a connectionless protocol. If the pName parameter is a NULL pointer in this
/// case, the IP address portion of the socket address structure is set to the loopback address.
///
///
/// -
/// AI_CANONNAME
///
/// If neither AI_CANONNAME nor AI_NUMERICHOST is used, the GetAddrInfoEx function attempts resolution. If a literal string is
/// passed GetAddrInfoEx attempts to convert the string, and if a host name is passed the GetAddrInfoEx function attempts to resolve
/// the name to an address or multiple addresses. When the AI_CANONNAME bit is set, the pName parameter cannot be NULL. Otherwise
/// the GetAddrInfoEx function will fail with WSANO_RECOVERY. When the AI_CANONNAME bit is set and the GetAddrInfoEx function
/// returns success, the ai_canonname member in the ppResult parameter points to a NULL-terminated string that contains the
/// canonical name of the specified node.
///
///
/// -
/// AI_NUMERICHOST
///
/// When the AI_NUMERICHOST bit is set, the pName parameter must contain a non-NULL numeric host address string, otherwise the
/// EAI_NONAME error is returned. This flag prevents a name resolution service from being called.
///
///
/// -
/// AI_NUMERICSERV
///
/// When the AI_NUMERICSERV bit is set, the pServiceName parameter must contain a non-NULL numeric port number, otherwise the
/// EAI_NONAME error is returned. This flag prevents a name resolution service from being called. The AI_NUMERICSERV flag is defined
/// on Windows SDK for Windows Vista and later. The AI_NUMERICSERV flag is not supported by Microsoft providers.
///
///
/// -
/// AI_ALL
///
/// If the AI_ALL bit is set, a request is made for IPv6 addresses and IPv4 addresses with AI_V4MAPPED. The AI_ALL flag is defined
/// on the Windows SDK for Windows Vista and later. The AI_ALL flag is supported on Windows Vista and later.
///
///
/// -
/// AI_ADDRCONFIG
///
/// If the AI_ADDRCONFIG bit is set, GetAddrInfoEx will resolve only if a global address is configured. If AI_ADDRCONFIG flag is
/// specified, IPv4 addresses shall be returned only if an IPv4 address is configured on the local system, and IPv6 addresses shall
/// be returned only if an IPv6 address is configured on the local system. The IPv4 or IPv6 loopback address is not considered a
/// valid global address. The AI_ADDRCONFIG flag is defined on the Windows SDK for Windows Vista and later. The AI_ADDRCONFIG flag
/// is supported on Windows Vista and later.
///
///
/// -
/// AI_V4MAPPED
///
/// If the AI_V4MAPPED bit is set and a request for IPv6 addresses fails, a name service request is made for IPv4 addresses and
/// these addresses are converted to IPv4-mapped IPv6 address format. The AI_V4MAPPED flag is defined on the Windows SDK for Windows
/// Vista and later. The AI_V4MAPPED flag is supported on Windows Vista and later.
///
///
/// -
/// AI_NON_AUTHORITATIVE
///
/// If the AI_NON_AUTHORITATIVE bit is set, the NS_EMAIL namespace provider returns both authoritative and non-authoritative
/// results. If the AI_NON_AUTHORITATIVE bit is not set, the NS_EMAIL namespace provider returns only authoritative results. The
/// AI_NON_AUTHORITATIVE flag is defined on the Windows SDK for Windows Vista and later. The AI_NON_AUTHORITATIVE flag is supported
/// on Windows Vista and later and applies only to the NS_EMAIL namespace.
///
///
/// -
/// AI_SECURE
///
/// If the AI_SECURE bit is set, the NS_EMAIL namespace provider will return results that were obtained with enhanced security to
/// minimize possible spoofing. The AI_SECURE flag is defined on the Windows SDK for Windows Vista and later. The AI_SECURE flag is
/// supported on Windows Vista and later and applies only to the NS_EMAIL namespace.
///
///
/// -
/// AI_RETURN_PREFERRED_NAMES
///
/// If the AI_RETURN_PREFERRED_NAMES is set, then no name should be provided in the pName parameter. The NS_EMAIL namespace provider
/// will return preferred names for publication. The AI_RETURN_PREFERRED_NAMES flag is defined on the Windows SDK for Windows Vista
/// and later. The AI_RETURN_PREFERRED_NAMES flag is supported on Windows Vista and later and applies only to the NS_EMAIL namespace.
///
///
/// -
/// AI_FQDN
///
/// If the AI_FQDN is set and a flat name (single label) is specified, GetAddrInfoEx will return the fully qualified domain name
/// that the name eventually resolved to. The fully qualified domain name is returned in the ai_canonname member in the associated
/// addrinfoex structure. This is different than AI_CANONNAME bit flag that returns the canonical name registered in DNS which may
/// be different than the fully qualified domain name that the flat name resolved to. When the AI_FQDN bit is set, the pName
/// parameter cannot be NULL. Otherwise the GetAddrInfoEx function will fail with WSANO_RECOVERY. On Windows 8 and Windows Server
/// 2012, both the AI_FQDN and AI_CANONNAME bits can be set. If the GetAddrInfoEx function is called with both the AI_FQDN and
/// AI_CANONNAME bits, the ppResult parameter returns a pointer to an addrinfoex2 structure, not an addrinfoex structure. On Windows
/// 7 and Windows Server 2008 R2, only one of the AI_FQDN and AI_CANONNAME bits can be set. The GetAddrInfoEx function will fail if
/// both flags are present with EAI_BADFLAGS. Windows 7: The AI_FQDN flag is defined on the Windows SDK for Windows 7 and later. The
/// AI_FQDN flag is supported on Windows 7 and later.
///
///
/// -
/// AI_FILESERVER
///
/// If the AI_FILESERVER is set, this is a hint to the namespace provider that the hostname being queried is being used in file
/// share scenario. The namespace provider may ignore this hint. Windows 7: The AI_FILESERVER flag is defined on the Windows SDK for
/// Windows 7 and later. The AI_FILESERVER flag is supported on Windows 7 and later.
///
///
/// -
/// AI_DISABLE_IDN_ENCODING
///
/// If the AI_DISABLE_IDN_ENCODING is set, this disables the automatic International Domain Name encoding using Punycode in the name
/// resolution functions called by the GetAddrInfoEx function. Windows 8: The AI_DISABLE_IDN_ENCODING flag is defined on the Windows
/// SDK for Windows 8 and later. The AI_DISABLE_IDN_ENCODING flag is supported on Windows 8 and later.
///
///
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfoexa INT WSAAPI GetAddrInfoExA( PCSTR pName,
// PCSTR pServiceName, DWORD dwNameSpace, LPGUID lpNspId, const ADDRINFOEXA *hints, PADDRINFOEXA *ppResult, timeval *timeout,
// LPOVERLAPPED lpOverlapped, LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, LPHANDLE lpNameHandle );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "cc4ccb2d-ea5a-48bd-a3ae-f70432ab2c39")]
public static unsafe extern Win32Error GetAddrInfoExW([Optional, MarshalAs(UnmanagedType.LPWStr)] string pName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pServiceName,
[Optional] NS dwNameSpace, [In, Optional] Guid* lpNspId, [In, Optional] ADDRINFOEXW* hints, out SafeADDRINFOEXWArray ppResult, [In, Optional] TIMEVAL* timeout, [In, Optional] NativeOverlapped* lpOverlapped,
[In, Optional] LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, [Out, Optional] HANDLE* lpNameHandle);
/// The GetAddrInfoW function provides protocol-independent translation from a Unicode host name to an address.
///
/// A pointer to a NULL-terminated Unicode string that contains a host (node) name or a numeric host address string. For the
/// Internet protocol, the numeric host address string is a dotted-decimal IPv4 address or an IPv6 hex address.
///
///
///
/// A pointer to a NULL-terminated Unicode string that contains either a service name or port number represented as a string.
///
///
/// A service name is a string alias for a port number. For example, “http” is an alias for port 80 defined by the Internet
/// Engineering Task Force (IETF) as the default port used by web servers for the HTTP protocol. Possible values for the
/// pServiceName parameter when a port number is not specified are listed in the following file:
///
///
///
/// A pointer to an addrinfoW structure that provides hints about the type of socket the caller supports.
///
/// The ai_addrlen, ai_canonname, ai_addr, and ai_next members of the addrinfoW structure pointed to by
/// the pHints parameter must be zero or NULL. Otherwise the GetAddrInfoEx function will fail with WSANO_RECOVERY.
///
/// See the Remarks for more details.
///
///
/// A pointer to a linked list of one or more addrinfoW structures that contains response information about the host.
///
///
/// Success returns zero. Failure returns a nonzero Windows Sockets error code, as found in the Windows Sockets Error Codes.
///
/// Most nonzero error codes returned by the GetAddrInfoW function map to the set of errors outlined by Internet Engineering
/// Task Force (IETF) recommendations. The following table lists these error codes and their WSA equivalents. It is recommended that
/// the WSA error codes be used, as they offer familiar and comprehensive error information for Winsock programmers.
///
///
///
/// Error value
/// WSA equivalent
/// Description
///
/// -
/// EAI_AGAIN
/// WSATRY_AGAIN
/// A temporary failure in name resolution occurred.
///
/// -
/// EAI_BADFLAGS
/// WSAEINVAL
/// An invalid value was provided for the ai_flags member of the pHints parameter.
///
/// -
/// EAI_FAIL
/// WSANO_RECOVERY
/// A nonrecoverable failure in name resolution occurred.
///
/// -
/// EAI_FAMILY
/// WSAEAFNOSUPPORT
/// The ai_family member of the pHints parameter is not supported.
///
/// -
/// EAI_MEMORY
/// WSA_NOT_ENOUGH_MEMORY
/// A memory allocation failure occurred.
///
/// -
/// EAI_NONAME
/// WSAHOST_NOT_FOUND
/// The name does not resolve for the supplied parameters or the pNodeName and pServiceName parameters were not provided.
///
/// -
/// EAI_SERVICE
/// WSATYPE_NOT_FOUND
/// The pServiceName parameter is not supported for the specified ai_socktype member of the pHints parameter.
///
/// -
/// EAI_SOCKTYPE
/// WSAESOCKTNOSUPPORT
/// The ai_socktype member of the pHints parameter is not supported.
///
///
///
/// Use the gai_strerror function to print error messages based on the EAI_* codes returned by the GetAddrInfoW function. The
/// gai_strerror function is provided for compliance with IETF recommendations, but it is not thread safe. Therefore, use of
/// a traditional Windows Sockets function, such as WSAGetLastError, is recommended.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSA_NOT_ENOUGH_MEMORY
/// There was insufficient memory to perform the operation.
///
/// -
/// WSAEAFNOSUPPORT
///
/// An address incompatible with the requested protocol was used. This error is returned if the ai_family member of the
/// addrinfoWstructure pointed to by the hints parameter is not supported.
///
///
/// -
/// WSAEINVAL
///
/// An invalid argument was supplied. This error is returned if an invalid value was provided for the ai_flags member of the
/// addrinfoWstructure pointed to by the hints parameter.
///
///
/// -
/// WSAESOCKTNOSUPPORT
///
/// The support for the specified socket type does not exist in this address family. This error is returned if the ai_socktype
/// member of the addrinfoWstructure pointed to by the hints parameter is not supported.
///
///
/// -
/// WSAHOST_NOT_FOUND
///
/// No such host is known. This error is returned if the name does not resolve for the supplied parameters or the pNodename and
/// pServicename parameters were not provided.
///
///
/// -
/// WSANO_DATA
/// The requested name is valid, but no data of the requested type was found.
///
/// -
/// WSANO_RECOVERY
///
/// A nonrecoverable error occurred during a database lookup. This error is returned if nonrecoverable error in name resolution occurred.
///
///
/// -
/// WSANOTINITIALISED
/// A successful WSAStartup call must occur before using this function.
///
/// -
/// WSATRY_AGAIN
///
/// This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an
/// authoritative server. This error is returned when a temporary failure in name resolution occurred.
///
///
/// -
/// WSATYPE_NOT_FOUND
///
/// The specified class was not found. The pServiceName parameter is not supported for the specified ai_socktype member of the
/// addrinfoWstructure pointed to by the hints parameter.
///
///
///
///
///
///
/// The GetAddrInfoW function is the Unicode version of a function that provides protocol-independent translation from host
/// name to address. The ANSI version of this function is getaddrinfo.
///
///
/// The GetAddrInfoW function returns results for the NS_DNS namespace. The GetAddrInfoW function aggregates
/// all responses if more than one namespace provider returns information. For use with the IPv6 and IPv4 protocol, name resolution
/// can be by the Domain Name System (DNS), a local hosts file, or by other naming mechanisms for the NS_DNS namespace.
///
///
/// Macros in the Winsock header file define a mixed-case function name of GetAddrInfo and a ADDRINFOT structure. This
/// GetAddrInfo function should be called with the pNodeName and pServiceName parameters of a pointer of type TCHAR
/// and the pHints and ppResult parameters of a pointer of type ADDRINFOT. When UNICODE or _UNICODE is defined,
/// GetAddrInfo is defined to GetAddrInfoW, the Unicode version of the function, and ADDRINFOT is defined to
/// the addrinfoW structure. When UNICODE or _UNICODE is not defined, GetAddrInfo is defined to getaddrinfo, the ANSI version
/// of the function, and ADDRINFOT is defined to the addrinfo structure.
///
///
/// One or both of the pNodeName or pServiceName parameters must point to a NULL-terminated Unicode string; generally both
/// are provided.
///
///
/// Upon success, a linked list of addrinfoW structures is returned in the ppResult parameter. The list can be processed by
/// following the pointer provided in the ai_next member of each returned addrinfoW structure until a NULL
/// pointer is encountered. In each returned addrinfoW structure, the ai_family, ai_socktype, and
/// ai_protocol members correspond to respective arguments in a socket or WSASocket function call. Also, the ai_addr
/// member in each returned addrinfoW structure points to a filled-in socket address structure, the length of which is
/// specified in its ai_addrlen member.
///
///
/// If the pNodeName parameter points to a computer name, all permanent addresses for the computer that can be used as a source
/// address are returned. On Windows Vista and later, these addresses would include all unicast IP addresses returned by the
/// GetUnicastIpAddressTable or GetUnicastIpAddressEntry functions in which the SkipAsSource member is set to false in the
/// MIB_UNICASTIPADDRESS_ROW structure.
///
/// If the pNodeName parameter points to a string equal to "localhost", all loopback addresses on the local computer are returned.
/// If the pNodeName parameter contains an empty string, all registered addresses on the local computer are returned.
///
/// On Windows Server 2003 and later if the pNodeName parameter points to a string equal to "..localmachine", all registered
/// addresses on the local computer are returned.
///
///
/// If the pNodeName parameter refers to a cluster virtual server name, only virtual server addresses are returned. On Windows Vista
/// and later, these addresses would include all unicast IP addresses returned by the GetUnicastIpAddressTable or
/// GetUnicastIpAddressEntry functions in which the SkipAsSource member is set to true in the MIB_UNICASTIPADDRESS_ROW
/// structure. See Windows Clustering for more information about clustering.
///
///
/// Windows 7 with Service Pack 1 (SP1) and Windows Server 2008 R2 with Service Pack 1 (SP1) add support to Netsh.exe for setting
/// the SkipAsSource attribute on an IP address. This also changes the behavior such that if the SkipAsSource member in the
/// MIB_UNICASTIPADDRESS_ROW structure is set to false, the IP address will be registered in DNS. If the SkipAsSource member
/// is set to true, the IP address is not registered in DNS.
///
///
/// A hotfix is available for Windows 7 and Windows Server 2008 R2 that adds support to Netsh.exe for setting the SkipAsSource
/// attribute on an IP address. This hotfix also changes behavior such that if the SkipAsSource member in the
/// MIB_UNICASTIPADDRESS_ROW structure is set to false, the IP address will be registered in DNS. If the SkipAsSource member
/// is set to true, the IP address is not registered in DNS. For more information, see Knowledge Base (KB) 2386184.
///
///
/// A similar hotfix is also available for Windows Vista with Service Pack 2 (SP2) and Windows Server 2008 with Service Pack 2 (SP2)
/// that adds support to Netsh.exe for setting the SkipAsSource attribute on an IP address. This hotfix also changes behavior such
/// that if the SkipAsSource member in the MIB_UNICASTIPADDRESS_ROW structure is set to false, the IP address will be
/// registered in DNS. If the SkipAsSource member is set to true, the IP address is not registered in DNS. For more
/// information, see Knowledge Base (KB) 975808.
///
///
/// Callers of the GetAddrInfoW function can provide hints about the type of socket supported through an addrinfoW structure
/// pointed to by the pHints parameter. When the pHints parameter is used, the following rules apply to its associated
/// addrinfoW structure:
///
///
/// -
///
/// A value of AF_UNSPEC for ai_family indicates the caller will accept only the AF_INET and AF_INET6
/// address families. Note that AF_UNSPEC and PF_UNSPEC are the same.
///
///
/// -
/// A value of zero for ai_socktype indicates the caller will accept any socket type.
///
/// -
/// A value of zero for ai_protocol indicates the caller will accept any protocol.
///
/// -
/// The ai_addrlen member must be set to zero.
///
/// -
/// The ai_canonname member must be set to NULL.
///
/// -
/// The ai_addr member must be set to NULL.
///
/// -
/// The ai_next member must be set to NULL.
///
///
///
/// Other values in the addrinfoW structure provided in the pHints parameter indicate specific requirements. For example, if the
/// caller handles only IPv4 and does not handle IPv6, the ai_family member should be set to AF_INET. For another
/// example, if the caller handles only TCP and does not handle UDP, the ai_socktype member should be set to SOCK_STREAM.
///
///
/// If the pHints parameter is a NULL pointer, the GetAddrInfoW function handles it as if the addrinfoW structure in
/// pHints were initialized with its ai_family member set to AF_UNSPEC and all other members set to zero.
///
///
/// On Windows Vista and later when GetAddrInfoW is called from a service, if the operation is the result of a user process
/// calling the service, then the service should impersonate the user. This is to allow security to be properly enforced.
///
///
/// The GetAddrInfoW function can be used to convert a text string representation of an IP address to an addrinfoWstructure
/// that contains a sockaddr structure for the IP address and other information. To be used in this way, the string pointed to by
/// the pNodeName parameter must contain a text representation of an IP address and the addrinfoW structure pointed to by the
/// pHints parameter must have the AI_NUMERICHOST flag set in the ai_flags member. The string pointed to by the
/// pNodeName parameter may contain a text representation of either an IPv4 or an IPv6 address. The text IP address is converted to
/// an addrinfoW structure pointed to by the ppResult parameter. The returned addrinfoW structure contains a
/// sockaddr structure for the IP address along with additional information about the IP address. For this method to work
/// with an IPv6 address string on Windows Server 2003 and Windows XP, the IPv6 protocol must be installed on the local computer.
/// Otherwise, the WSAHOST_NOT_FOUND error is returned.
///
/// Freeing Address Information from Dynamic Allocation
///
/// All information returned by the GetAddrInfoW function pointed to by the ppResult parameter is dynamically allocated,
/// including all addrinfoW structures, socket address structures, and canonical host name strings pointed to by addrinfoW
/// structures. Memory allocated by a successful call to this function must be released with a subsequent call to FreeAddrInfoW.
///
/// Example Code
/// The following code example shows how to use the GetAddrInfoW function.
///
/// Note Ensure that the development environment targets the newest version of Ws2tcpip.h which includes structure and
/// function definitions for addrinfoW and GetAddrInfoW, respectively.
///
/// Internationalized Domain Names
/// Internet host names typically consist of a very restricted set of characters:
///
/// -
/// Upper and lower case ASCII letters from the English alphabet.
///
/// -
/// Digits from 0 to 9.
///
/// -
/// ASCII hyphen characters.
///
///
///
/// With the growth of the Internet, there is a growing need to identify Internet host names for other languages not represented by
/// the ASCII character set. Identifiers which facilitate this need and allow non-ASCII characters (Unicode) to be represented as
/// special ASCII character strings are known as Internationalized Domain Names (IDNs). A mechanism called Internationalizing Domain
/// Names in Applications (IDNA) is used to handle IDNs in a standard fashion. The specifications for IDNs and IDNA are documented
/// in RFC 3490, RTF 5890, and RFC 6365 published by the Internet Engineering Task Force (IETF).
///
///
/// On Windows 8 and Windows Server 2012, the GetAddrInfoW function provides support for Internationalized Domain Name (IDN)
/// parsing applied to the name passed in the pNodeName parameter. Winsock performs Punycode/IDN encoding and conversion. This
/// behavior can be disabled using the AI_DISABLE_IDN_ENCODING flag discussed below.
///
///
/// On Windows 7 and Windows Server 2008 R2 or earlier, the GetAddrInfoW function does not currently provide support for IDN
/// parsing applied to the name passed in the pNodeName parameter. Winsock does not perform any Punycode/IDN conversion. The
/// GetAddrInfoW function does not use Punycode to convert an IDN as per RFC 3490. The GetAddrInfoW function when
/// querying DNS encodes the Unicode name in UTF-8 format, the format used by Microsoft DNS servers in an enterprise environment.
///
///
/// Several functions on Windows Vista and later support conversion between Unicode labels in an IDN to their ASCII equivalents. The
/// resulting representation of each Unicode label contains only ASCII characters and starts with the xn-- prefix if the Unicode
/// label contained any non-ASCII characters. The reason for this is to support existing DNS servers on the Internet, since some DNS
/// tools and servers only support ASCII characters (see RFC 3490).
///
///
/// The IdnToAscii function use Punycode to convert an IDN to the ASCII representation of the original Unicode string using the
/// standard algorithm defined in RFC 3490. The IdnToUnicode function converts the ASCII form of an IDN to the normal Unicode UTF-16
/// encoding syntax. For more information and links to related draft standards, see Handling Internationalized Domain Names (IDNs).
///
///
/// The IdnToAscii function can be used to convert an IDN name to the ASCII form. To pass this ASCII form to the GetAddrInfoW
/// function, you can use the MultiByteToWideChar function to convert the CHAR string into a WCHAR string that then
/// can be passed in the pNodeName parameter to the GetAddrInfoW function.
///
/// Use of ai_flags in the hints parameter
///
/// Flags in the ai_flags member of the optional addrinfoW structure provided in the pHints parameter modify the behavior of
/// the function.
///
///
/// These flag bits are defined in the Ws2def.h header file on the Microsoft Windows Software Development Kit (SDK) for Windows 7.
/// These flag bits are defined in the Ws2tcpip.h header file on the Windows SDK for Windows Server 2008 and Windows Vista. These
/// flag bits are defined in the Ws2tcpip.h header file on the Platform Software Development Kit (SDK) for Windows Server 2003, and
/// Windows XP.
///
/// The flag bits can be a combination of the following:
///
///
/// Flag Bits
/// Description
///
/// -
/// AI_PASSIVE
///
/// Setting the AI_PASSIVE flag indicates the caller intends to use the returned socket address structure in a call to the bind
/// function. When the AI_PASSIVE flag is set and pNodeName is a NULL pointer, the IP address portion of the socket address
/// structure is set to INADDR_ANY for IPv4 addresses and IN6ADDR_ANY_INIT for IPv6 addresses. When the AI_PASSIVE flag is not set,
/// the returned socket address structure is ready for a call to the connect function for a connection-oriented protocol, or ready
/// for a call to either the connect, sendto, or send functions for a connectionless protocol. If the pNodeName parameter is a NULL
/// pointer in this case, the IP address portion of the socket address structure is set to the loopback address.
///
///
/// -
/// AI_CANONNAME
///
/// If neither AI_CANONNAME nor AI_NUMERICHOST is used, the GetAddrInfoW function attempts resolution. If a literal string is passed
/// GetAddrInfoW attempts to convert the string, and if a host name is passed the GetAddrInfoW function attempts to resolve the name
/// to an address or multiple addresses. When the AI_CANONNAME bit is set, the pNodeName parameter cannot be NULL. Otherwise the
/// GetAddrInfoEx function will fail with WSANO_RECOVERY. When the AI_CANONNAME bit is set and the GetAddrInfoW function returns
/// success, the ai_canonname member in the ppResult parameter points to a NULL-terminated string that contains the canonical name
/// of the specified node.
///
///
/// -
/// AI_NUMERICHOST
///
/// When the AI_NUMERICHOST bit is set, the pNodeName parameter must contain a non-NULL numeric host address string, otherwise the
/// EAI_NONAME error is returned. This flag prevents a name resolution service from being called.
///
///
/// -
/// AI_NUMERICSERV
///
/// When the AI_NUMERICSERV bit is set, the pServiceName parameter must contain a non-NULL numeric port number, otherwise the
/// EAI_NONAME error is returned. This flag prevents a name resolution service from being called. The AI_NUMERICSERV flag is defined
/// on Windows SDK for Windows Vista and later. The AI_NUMERICSERV flag is not supported by Microsoft providers.
///
///
/// -
/// AI_ALL
///
/// If the AI_ALL bit is set, a request is made for IPv6 addresses and IPv4 addresses with AI_V4MAPPED. The AI_ALL flag is defined
/// on the Windows SDK for Windows Vista and later. The AI_ALL flag is supported on Windows Vista and later.
///
///
/// -
/// AI_ADDRCONFIG
///
/// If the AI_ADDRCONFIG bit is set, GetAddrInfoW will resolve only if a global address is configured. If AI_ADDRCONFIG flag is
/// specified, IPv4 addresses shall be returned only if an IPv4 address is configured on the local system, and IPv6 addresses shall
/// be returned only if an IPv6 address is configured on the local system. The IPv4 or IPv6 loopback address is not considered a
/// valid global address. The AI_ADDRCONFIG flag is defined on the Windows SDK for Windows Vista and later. The AI_ADDRCONFIG flag
/// is supported on Windows Vista and later.
///
///
/// -
/// AI_V4MAPPED
///
/// If the AI_V4MAPPED bit is set and a request for IPv6 addresses fails, a name service request is made for IPv4 addresses and
/// these addresses are converted to IPv4-mapped IPv6 address format. The AI_V4MAPPED flag is defined on the Windows SDK for Windows
/// Vista and later. The AI_V4MAPPED flag is supported on Windows Vista and later.
///
///
/// -
/// AI_NON_AUTHORITATIVE
///
/// If the AI_NON_AUTHORITATIVE bit is set, the NS_EMAIL namespace provider returns both authoritative and non-authoritative
/// results. If the AI_NON_AUTHORITATIVE bit is not set, the NS_EMAIL namespace provider returns only authoritative results. The
/// AI_NON_AUTHORITATIVE flag is defined on the Windows SDK for Windows Vista and later. The AI_NON_AUTHORITATIVE flag is supported
/// on Windows Vista and later and applies only to the NS_EMAIL namespace.
///
///
/// -
/// AI_SECURE
///
/// If the AI_SECURE bit is set, the NS_EMAIL namespace provider will return results that were obtained with enhanced security to
/// minimize possible spoofing. The AI_SECURE flag is defined on the Windows SDK for Windows Vista and later. The AI_SECURE flag is
/// supported on Windows Vista and later and applies only to the NS_EMAIL namespace.
///
///
/// -
/// AI_RETURN_PREFERRED_NAMES
///
/// If the AI_RETURN_PREFERRED_NAMES is set, then no name should be provided in the pNodeName parameter. The NS_EMAIL namespace
/// provider will return preferred names for publication. The AI_RETURN_PREFERRED_NAMES flag is defined on the Windows SDK for
/// Windows Vista and later. The AI_RETURN_PREFERRED_NAMES flag is supported on Windows Vista and later and applies only to the
/// NS_EMAIL namespace.
///
///
/// -
/// AI_FQDN
///
/// If the AI_FQDN is set and a flat name (single label) is specified, GetAddrInfoW will return the fully qualified domain name that
/// the name eventually resolved to. The fully qualified domain name is returned in the ai_canonname member in the associated
/// addrinfoW structure. This is different than AI_CANONNAME bit flag that returns the canonical name registered in DNS which may be
/// different than the fully qualified domain name that the flat name resolved to. Only one of the AI_FQDN and AI_CANONNAME bits can
/// be set. The GetAddrInfoW function will fail if both flags are present with EAI_BADFLAGS. When the AI_FQDN bit is set, the
/// pNodeName parameter cannot be NULL. Otherwise the GetAddrInfoEx function will fail with WSANO_RECOVERY. Windows 7: The AI_FQDN
/// flag is defined on the Windows SDK for Windows 7 and later. The AI_FQDN flag is supported on Windows 7 and later.
///
///
/// -
/// AI_FILESERVER
///
/// If the AI_FILESERVER is set, this is a hint to the namespace provider that the hostname being queried is being used in file
/// share scenario. The namespace provider may ignore this hint. Windows 7: The AI_FILESERVER flag is defined on the Windows SDK for
/// Windows 7 and later. The AI_FILESERVER flag is supported on Windows 7 and later.
///
///
/// -
/// AI_DISABLE_IDN_ENCODING
///
/// If the AI_DISABLE_IDN_ENCODING is set, this disables the automatic International Domain Name encoding using Punycode in the name
/// resolution functions called by the GetAddrInfoW function. Windows 8: The AI_DISABLE_IDN_ENCODING flag is defined on the Windows
/// SDK for Windows 8 and later. The AI_DISABLE_IDN_ENCODING flag is supported on Windows 8 and later.
///
///
///
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-getaddrinfow INT WSAAPI GetAddrInfoW( PCWSTR pNodeName,
// PCWSTR pServiceName, const ADDRINFOW *pHints, PADDRINFOW *ppResult );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "82436a88-5b37-4758-a5c9-b60dd1cbc36c")]
public static extern Win32Error GetAddrInfoW([Optional] string pNodeName, [Optional] string pServiceName, [Optional] in ADDRINFOW pHints, out SafeADDRINFOWArray ppResult);
/// The getipv4sourcefilter inline function retrieves the multicast filter state for an IPv4 socket.
/// A descriptor that identifies a multicast socket.
///
/// The local IPv4 address of the interface or the interface index on which the multicast group should be joined or dropped.
///
/// This value is in network byte order. If this member specifies an IPv4 address of 0.0.0.0, the default IPv4 multicast interface
/// is used.
///
///
/// Any IP address in the 0.x.x.x block (first octet of 0) except IPv4 address 0.0.0.0 is treated as an interface index. An
/// interface index is a 24-bit number, and the 0.0.0.0/8 IPv4 address block is not used (this range is reserved).
///
/// To use an interface index of 1 would be the same as an IP address of 0.0.0.1.
///
/// The IPv4 address of the multicast group.
///
/// A pointer to a value to receive the multicast filter mode for multicast group address when the function returns.
///
///
///
/// On input, a pointer to a value that indicates the maximum number of source addresses that will fit in the buffer pointed to by
/// the SourceList parameter.
///
/// On output, a pointer to a value that indicates the total number of source addresses associated with the multicast filter.
///
///
/// A pointer to a buffer to receive the list of IP addresses associated with the multicast filter.
/// If SourceCount is zero on input, a NULL pointer may be supplied.
///
///
///
/// On success, getipv4sourcefilter returns NO_ERROR (0). Any nonzero return value indicates failure and a specific error
/// code can be retrieved by calling WSAGetLastError.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSAENOBUFS
/// Insufficient buffer space is available.
///
/// -
/// WSAENOTSOCK
/// The descriptor is not a socket.
///
///
///
///
/// The getipv4sourcefilter inline function is used to retrieve the multicast filter state for an IPv4 socket.
///
/// If the app does not know the size of the source list beforehand, it can make a guess (zero, for example). If upon completion,
/// the SourceCount parameter holds a larger value, the operation can be repeated with a large enough buffer.
///
///
/// On return, the SourceCount parameter is always updated to be the total number of sources in the filter, while the buffer pointed
/// to by the SourceList parameter will hold as many source addresses as fit, up to the minimum of the array size passed in as the
/// original SourceCount value and the total number of sources in the filter.
///
///
/// This function is part of socket interface extensions for multicast source filters defined in RFC 3678. An app can use these
/// functions to retrieve and set the multicast source address filters associated with a socket.
///
/// Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getipv4sourcefilter int getipv4sourcefilter( SOCKET
// Socket, IN_ADDR Interface, IN_ADDR Group, MULTICAST_MODE_TYPE *FilterMode, ULONG *SourceCount, IN_ADDR *SourceList );
[PInvokeData("ws2tcpip.h", MSDNShortId = "17D35D24-C419-4787-AB93-E6B1B6B13807")]
public static Win32Error getipv4sourcefilter(SOCKET Socket, IN_ADDR Interface, IN_ADDR Group, out MULTICAST_MODE_TYPE FilterMode, ref int SourceCount, IN_ADDR[] SourceList)
{
FilterMode = MULTICAST_MODE_TYPE.MCAST_INCLUDE;
if (SourceCount > (SourceList?.Length ?? 0))
{
WSASetLastError(unchecked((int)Win32Error.WSAENOBUFS));
return SOCKET_ERROR;
}
var Filter = new IP_MSFILTER
{
imsf_multiaddr = Group,
imsf_interface = Interface,
imsf_numsrc = (uint)SourceCount
};
using var pFilter = SafeHGlobalHandle.CreateFromStructure(Filter);
pFilter.Size = IP_MSFILTER_SIZE(SourceCount);
var err = WSAIoctl(Socket, SIOCGIPMSFILTER, pFilter, pFilter.Size, pFilter, pFilter.Size, out var Returned);
if (err == 0)
{
Filter = pFilter.ToStructure();
if (SourceCount > 0)
{
Array.Copy(Filter.imsf_slist, SourceList, Math.Min(SourceCount, Filter.imsf_numsrc));
SourceCount = (int)Filter.imsf_numsrc;
}
FilterMode = Filter.imsf_fmode;
}
return err;
}
///
/// The GetNameInfoW function provides protocol-independent name resolution from an address to a Unicode host name and from a
/// port number to the Unicode service name.
///
///
/// A pointer to a socket address structure containing the IP address and port number of the socket. For IPv4, the pSockaddr
/// parameter points to a sockaddr_in structure. For IPv6, the pSockaddr parameter points to a sockaddr_in6 structure.
///
/// The length, in bytes, of the structure pointed to by the pSockaddr parameter.
///
/// A pointer to a Unicode string to hold the host name. On success, a pointer to the Unicode host name is returned as a Fully
/// Qualified Domain Name (FQDN) by default. If the pNodeBuffer parameter is NULL, this indicates the caller does not want to
/// receive a host name string.
///
///
/// The number of WCHAR characters in the buffer pointed to by the pNodeBuffer parameter. The caller must provide a buffer
/// large enough to hold the Unicode host name, including the terminating NULL character.
///
///
/// A pointer to a Unicode string to hold the service name. On success, a pointer is returned to a Unicode string representing the
/// service name associated with the port number. If the pServiceBuffer parameter is NULL, this indicates the caller does not
/// want to receive a service name string.
///
///
/// The number of WCHAR characters in the buffer pointed to by the pServiceBuffer parameter. The caller must provide a buffer
/// large enough to hold the Unicode service name, including the terminating NULL character.
///
/// A value used to customize processing of the GetNameInfoW function. See the Remarks section.
///
///
/// On success, GetNameInfoW returns zero. Any nonzero return value indicates failure and a specific error code can be
/// retrieved by calling WSAGetLastError.
///
///
/// Nonzero error codes returned by the GetNameInfoW function also map to the set of errors outlined by Internet Engineering
/// Task Force (IETF) recommendations. The following table shows these error codes and their WSA equivalents. It is recommended that
/// the WSA error codes be used, as they offer familiar and comprehensive error information for Winsock programmers.
///
///
///
/// Error value
/// WSA equivalent
/// Description
///
/// -
/// EAI_AGAIN
/// WSATRY_AGAIN
/// A temporary failure in name resolution occurred.
///
/// -
/// EAI_BADFLAGS
/// WSAEINVAL
///
/// One or more invalid parameters was passed to the GetNameInfoW function. This error is returned if a host name was requested but
/// the NodeBufferSize parameter was zero or if a service name was requested but the ServiceBufferSize parameter was zero.
///
///
/// -
/// EAI_FAIL
/// WSANO_RECOVERY
/// A nonrecoverable failure in name resolution occurred.
///
/// -
/// EAI_FAMILY
/// WSAEAFNOSUPPORT
/// The sa_family member of socket address structure pointed to by the pSockaddr parameter is not supported.
///
/// -
/// EAI_MEMORY
/// WSA_NOT_ENOUGH_MEMORY
/// A memory allocation failure occurred.
///
/// -
/// EAI_NONAME
/// WSAHOST_NOT_FOUND
///
/// A service name was requested, but no port number was found in the structure pointed to by the pSockaddr parameter or no service
/// name matching the port number was found. NI_NAMEREQD is set and the host's name cannot be located, or both the pNodeBuffer and
/// pServiceBuffer parameters were NULL.
///
///
///
///
/// You can use the gai_strerror function to print error messages based on the EAI codes returned by the GetNameInfoW
/// function. The gai_strerror function is provided for compliance with IETF recommendations, but it is not thread safe.
/// Therefore, use of traditional Windows Sockets functions such as WSAGetLastError is recommended.
///
/// In addition, the following error codes can be returned.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEFAULT
///
/// This error is returned if the pSockaddr parameter is NULL or the SockaddrLength parameter is less than the length needed for the
/// size of sockaddr_in structure for IPv4 or the sockaddr_in6 structure for IPv6.
///
///
///
///
///
///
/// The GetNameInfoW function is the Unicode version of a function that provides protocol-independent name resolution. The
/// GetNameInfoW function is used to translate the contents of a socket address structure to a node name and/or a service name.
///
///
/// For the IPv6 and IPv4 protocols, name resolution can be by the Domain Name System (DNS), a local hosts file, or by other naming
/// mechanisms. This function can be used to determine the host name for an IPv4 or IPv6 address, a reverse DNS lookup, or determine
/// the service name for a port number. The GetNameInfoW function can also be used to convert an IP address or a port number
/// in a SOCKADDR structure to an Unicode string. This function can also be used to determine the IP address for a host name.
///
/// The ANSI version of this function is getnameinfo.
///
/// Macros in the Winsock header file define a mixed-case function name of GetNameInfo that can be used when the application
/// is targeted for Windows XP with Service Pack 2 (SP2) and later (_WIN32_WINNT >= 0x0502). This GetNameInfo function
/// should be called with the pNodeBuffer and pServiceBuffer parameters of a pointer of type TCHAR. When UNICODE or _UNICODE
/// is defined, GetNameInfo is defined to the Unicode version and GetNameInfoW is called with the host and serv
/// parameters of a pointer of type char. When UNICODE or _UNICODE is not defined, GetNameInfo is defined to the ANSI
/// version and getnameinfo is called with the pNodeBuffer and pServiceBuffer parameters of a pointer of type PWCHAR.
///
///
/// To simplify determining buffer requirements for the pNodeBuffer and pServiceBuffer parameters, the following values for maximum
/// host name length and maximum service name are defined in the Ws2tcpip.h header file:
///
/// The Flags parameter can be used to customize processing of the GetNameInfoW function. The following flags are available:
///
/// -
/// NI_NOFQDN
///
/// -
/// NI_NUMERICHOST
///
/// -
/// NI_NAMEREQD
///
/// -
/// NI_NUMERICSERV
///
/// -
/// NI_DGRAM
///
///
/// When the NI_NAMEREQD flag is set, a host name that cannot be resolved by the DNS results in an error.
///
/// Setting the NI_NOFQDN flag results in local hosts having only their Relative Distinguished Name (RDN) returned in the
/// pNodeBuffer parameter.
///
///
/// Setting the NI_NUMERICHOST flag returns the numeric form of the host name instead of its name. The numeric form of the
/// host name is also returned if the host name cannot be resolved by DNS.
///
///
/// Setting the NI_NUMERICSERV flag returns the port number of the service instead of its name. Also, if a host name is not
/// found for an IP address (127.0.0.2, for example), the hostname is returned as the IP address.
///
///
/// On Windows Vista and later, if NI_NUMERICSERV is not specified in the flags parameter, and the port number contained in
/// sockaddr structure pointed to by the sa parameter does not resolve to a well known service, the GetNameInfoW
/// function returns the numeric form of the service address (the port number) as a numeric string. When NI_NUMERICSERV is
/// specified, the port number is returned as a numeric string. This behavior is specified in section 6.2 of RFC 3493. For more
/// information, see www.ietf.org/rfc/rfc3493.txt
///
///
/// On Windows Server 2003 and earlier, if NI_NUMERICSERV is not specified in the flags parameter and the port number
/// contained in sockaddr structure pointed to by the sa parameter does not resolve to a well known service, the GetNameInfoW
/// function fails. When NI_NUMERICSERV is specified, the port number is returned as a numeric string.
///
///
/// Setting the NI_DGRAM flag indicates that the service is a datagram service. This flag is necessary for the few services
/// that provide different port numbers for UDP and TCP service.
///
///
/// Note The capability to perform reverse DNS lookups using the GetNameInfoW function is convenient, but such lookups
/// are considered inherently unreliable, and should be used only as a hint.
///
/// NoteGetNameInfoW cannot be used to resolve alias names.
/// Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
/// Example Code
/// The following example demonstrates the use of the GetNameInfoW function.
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getnameinfow INT WSAAPI GetNameInfoW( const SOCKADDR
// *pSockaddr, socklen_t SockaddrLength, PWCHAR pNodeBuffer, DWORD NodeBufferSize, PWCHAR pServiceBuffer, DWORD ServiceBufferSize,
// INT Flags );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "5630a49a-c182-440c-ad54-6ff3ba4274c6")]
public static extern Win32Error GetNameInfoW([In] SOCKADDR pSockaddr, int SockaddrLength, StringBuilder pNodeBuffer, uint NodeBufferSize, StringBuilder pServiceBuffer, uint ServiceBufferSize, NI Flags);
/// The getsourcefilter inline function retrieves the multicast filter state for an IPv4 or IPv6 socket.
/// A descriptor that identifies a multicast socket.
/// The interface index of the multicast interface.
/// A pointer to the socket address of the multicast group.
/// The length, in bytes, of the socket address pointed to by the Group parameter.
///
/// A pointer to a value to receive the multicast filter mode for the multicast group address when the function returns.
///
///
///
/// On input, a pointer to a value that indicates the maximum number of source addresses that will fit in the buffer pointed to by
/// the SourceList parameter.
///
/// On output, a pointer to a value that indicates the total number of source addresses associated with the multicast filter.
///
///
/// A pointer to a buffer to receive the list of IP addresses associated with the multicast filter.
/// If SourceCount is zero on input, a NULL pointer may be supplied.
///
///
///
/// On success, getsourcefilter returns NO_ERROR (0). Any nonzero return value indicates failure and a specific error code
/// can be retrieved by calling WSAGetLastError.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSAENOBUFS
/// Insufficient buffer space is available.
///
/// -
/// WSAENOTSOCK
/// The descriptor is not a socket.
///
///
///
///
/// The getsourcefilter inline function is used to retrieve the multicast filter state for an IPv4 or IPv6 socket.
///
/// If the app does not know the size of the source list beforehand, it can make a guess (zero, for example). If upon completion,
/// the SourceCount parameter holds a larger value, the operation can be repeated with a large enough buffer.
///
///
/// On return, the SourceCount parameter is always updated to be the total number of sources in the filter, while the buffer pointed
/// to by the SourceList parameter will hold as many source addresses as fit, up to the minimum of the array size passed in as the
/// original SourceCount value and the total number of sources in the filter.
///
///
/// This function is part of socket interface extensions for multicast source filters defined in RFC 3678. An app can use these
/// functions to retrieve and set the multicast source address filters associated with a socket.
///
/// Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getsourcefilter int getsourcefilter( SOCKET Socket, ULONG
// Interface, const SOCKADDR *Group, int GroupLength, MULTICAST_MODE_TYPE *FilterMode, ULONG *SourceCount, SOCKADDR_STORAGE
// *SourceList );
[PInvokeData("ws2tcpip.h", MSDNShortId = "2CA84000-F114-439D-BEDE-9990044C7785")]
public static Win32Error getsourcefilter(SOCKET Socket, uint Interface, [In] SOCKADDR Group, int GroupLength, out MULTICAST_MODE_TYPE FilterMode, ref int SourceCount, SOCKADDR_STORAGE[] SourceList)
{
FilterMode = MULTICAST_MODE_TYPE.MCAST_INCLUDE;
if (SourceCount > SourceList.Length || GroupLength > Group.Size)
{
WSASetLastError(unchecked((int)Win32Error.WSAENOBUFS));
return SOCKET_ERROR;
}
var Filter = new GROUP_FILTER
{
gf_interface = Interface,
gf_numsrc = (uint)SourceCount
};
using var pFilter = SafeHGlobalHandle.CreateFromStructure(Filter);
pFilter.Size = GROUP_FILTER_SIZE(SourceCount);
Group.DangerousGetHandle().CopyTo(pFilter.DangerousGetHandle().Offset(4), Group.Size);
var Error = WSAIoctl(Socket, SIOCGMSFILTER, pFilter, pFilter.Size, pFilter, pFilter.Size, out var Returned);
if (Error == 0)
{
Filter = pFilter.ToStructure();
if (SourceCount > 0)
{
Array.Copy(Filter.gf_slist, SourceList, Math.Min(SourceCount, Filter.gf_numsrc));
SourceCount = (int)Filter.gf_numsrc;
}
FilterMode = Filter.gf_fmode;
}
return Error;
}
///
/// The InetNtop function converts an IPv4 or IPv6 Internet network address into a string in Internet standard format. The
/// ANSI version of this function is inet_ntop.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, this function returns an IPv4 address string.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, this function returns an IPv6 address string.
///
///
///
///
///
/// A pointer to the IP address in network byte to convert to a string.
///
/// When the Family parameter is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4 address
/// to convert.
///
///
/// When the Family parameter is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6 address
/// to convert.
///
///
///
/// A pointer to a buffer in which to store the NULL-terminated string representation of the IP address.
/// For an IPv4 address, this buffer should be large enough to hold at least 16 characters.
/// For an IPv6 address, this buffer should be large enough to hold at least 46 characters.
///
/// On input, the length, in characters, of the buffer pointed to by the pStringBuf parameter.
///
///
/// If no error occurs, InetNtop function returns a pointer to a buffer containing the string representation of IP address in
/// standard format.
///
///
/// Otherwise, a value of NULL is returned, and a specific error code can be retrieved by calling the WSAGetLastError for
/// extended error information.
///
/// If the function fails, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// ERROR_INVALID_PARAMETER
///
/// An invalid parameter was passed to the function. This error is returned if a NULL pointer is passed in the pStringBuf or the
/// StringBufSize parameter is zero. This error is also returned if the length of the buffer pointed to by the pStringBuf parameter
/// is not large enough to receive the string representation of the IP address.
///
///
///
///
///
/// The InetNtop function is supported on Windows Vistaand later.
///
/// The InetNtop function provides a protocol-independent address-to-string translation. The InetNtop function takes
/// an Internet address structure specified by the pAddr parameter and returns a NULL-terminated string that represents the
/// IP address. While the inet_ntoa function works only with IPv4 addresses, the InetNtop function works with either IPv4 or
/// IPv6 addresses.
///
///
/// The ANSI version of this function is inet_ntop as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
/// The InetNtop function does not require that the Windows Sockets DLL be loaded to perform IP address to string conversion.
///
/// If the Family parameter specified is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in dotted-decimal
/// notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in Internet standard
/// format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A string of consecutive zero
/// numbers is replaced with a double-colon. There can only be one double-colon in the string representation of the IPv6 address.
/// The last 32 bits are represented in IPv4-style dotted-octet notation if the address is a IPv4-compatible address.
///
///
/// If the length of the buffer pointed to by the pStringBuf parameter is not large enough to receive the string representation of
/// the IP address, InetNtop returns ERROR_INVALID_PARAMETER.
///
///
/// When UNICODE or _UNICODE is defined, InetNtop is defined to InetNtopW, the Unicode version of this function. The
/// pStringBuf parameter is defined to the PSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetNtop is defined to InetNtopA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_ntop. The pStringBuf parameter is defined to the PWSTR
/// data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4AddressToString and RtlIpv4AddressToStringEx functions can be used to convert an IPv4
/// address represented as an IN_ADDR structure to a string representation of an IPv4 address in Internet standard dotted-decimal
/// notation. On Windows Vista and later, the RtlIpv6AddressToString and RtlIpv6AddressToStringEx functions can be used to convert
/// an IPv6 address represented as an IN6_ADDR structure to a string representation of an IPv6 address. The
/// RtlIpv6AddressToStringEx function is more flexible since it also converts an IPv6 address, scope ID, and port to a IPv6
/// string in standard format.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetNtopW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inet_ntop PCSTR WSAAPI inet_ntop( INT Family, const
// VOID *pAddr, PSTR pStringBuf, size_t StringBufSize );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Ansi)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "1e26b88c-808f-4807-8641-e5c6b10853ad")]
public static extern StrPtrAnsi inet_ntop(ADDRESS_FAMILY Family, in IN_ADDR pAddr, StringBuilder pStringBuf, SizeT StringBufSize);
///
/// The InetNtop function converts an IPv4 or IPv6 Internet network address into a string in Internet standard format. The
/// ANSI version of this function is inet_ntop.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, this function returns an IPv4 address string.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, this function returns an IPv6 address string.
///
///
///
///
///
/// A pointer to the IP address in network byte to convert to a string.
///
/// When the Family parameter is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4 address
/// to convert.
///
///
/// When the Family parameter is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6 address
/// to convert.
///
///
///
/// A pointer to a buffer in which to store the NULL-terminated string representation of the IP address.
/// For an IPv4 address, this buffer should be large enough to hold at least 16 characters.
/// For an IPv6 address, this buffer should be large enough to hold at least 46 characters.
///
/// On input, the length, in characters, of the buffer pointed to by the pStringBuf parameter.
///
///
/// If no error occurs, InetNtop function returns a pointer to a buffer containing the string representation of IP address in
/// standard format.
///
///
/// Otherwise, a value of NULL is returned, and a specific error code can be retrieved by calling the WSAGetLastError for
/// extended error information.
///
/// If the function fails, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// ERROR_INVALID_PARAMETER
///
/// An invalid parameter was passed to the function. This error is returned if a NULL pointer is passed in the pStringBuf or the
/// StringBufSize parameter is zero. This error is also returned if the length of the buffer pointed to by the pStringBuf parameter
/// is not large enough to receive the string representation of the IP address.
///
///
///
///
///
/// The InetNtop function is supported on Windows Vistaand later.
///
/// The InetNtop function provides a protocol-independent address-to-string translation. The InetNtop function takes
/// an Internet address structure specified by the pAddr parameter and returns a NULL-terminated string that represents the
/// IP address. While the inet_ntoa function works only with IPv4 addresses, the InetNtop function works with either IPv4 or
/// IPv6 addresses.
///
///
/// The ANSI version of this function is inet_ntop as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
/// The InetNtop function does not require that the Windows Sockets DLL be loaded to perform IP address to string conversion.
///
/// If the Family parameter specified is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in dotted-decimal
/// notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in Internet standard
/// format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A string of consecutive zero
/// numbers is replaced with a double-colon. There can only be one double-colon in the string representation of the IPv6 address.
/// The last 32 bits are represented in IPv4-style dotted-octet notation if the address is a IPv4-compatible address.
///
///
/// If the length of the buffer pointed to by the pStringBuf parameter is not large enough to receive the string representation of
/// the IP address, InetNtop returns ERROR_INVALID_PARAMETER.
///
///
/// When UNICODE or _UNICODE is defined, InetNtop is defined to InetNtopW, the Unicode version of this function. The
/// pStringBuf parameter is defined to the PSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetNtop is defined to InetNtopA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_ntop. The pStringBuf parameter is defined to the PWSTR
/// data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4AddressToString and RtlIpv4AddressToStringEx functions can be used to convert an IPv4
/// address represented as an IN_ADDR structure to a string representation of an IPv4 address in Internet standard dotted-decimal
/// notation. On Windows Vista and later, the RtlIpv6AddressToString and RtlIpv6AddressToStringEx functions can be used to convert
/// an IPv6 address represented as an IN6_ADDR structure to a string representation of an IPv6 address. The
/// RtlIpv6AddressToStringEx function is more flexible since it also converts an IPv6 address, scope ID, and port to a IPv6
/// string in standard format.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetNtopW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inet_ntop PCSTR WSAAPI inet_ntop( INT Family, const
// VOID *pAddr, PSTR pStringBuf, size_t StringBufSize );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Ansi)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "1e26b88c-808f-4807-8641-e5c6b10853ad")]
public static extern StrPtrAnsi inet_ntop(ADDRESS_FAMILY Family, in IN6_ADDR pAddr, StringBuilder pStringBuf, SizeT StringBufSize);
///
/// The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its
/// numeric binary form. The ANSI version of this function is inet_pton.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv4 address and the pAddrBuf parameter returns a pointer to an IN_ADDR structure that represents
/// the IPv4 address.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv6 address and the pAddrBuf parameter returns a pointer to an IN6_ADDR structure that
/// represents the IPv6 address.
///
///
///
///
///
///
/// A pointer to the NULL-terminated string that contains the text representation of the IP address to convert to numeric
/// binary form.
///
///
/// When the Family parameter is AF_INET, then the pszAddrString parameter must point to a text representation of an IPv4
/// address in standard dotted-decimal notation.
///
///
/// When the Family parameter is AF_INET6, then the pszAddrString parameter must point to a text representation of an IPv6
/// address in standard notation.
///
///
///
///
/// A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in
/// network byte order.
///
/// When the Family parameter is AF_INET, this buffer should be large enough to hold an IN_ADDR structure.
/// When the Family parameter is AF_INET6, this buffer should be large enough to hold an IN6_ADDR structure.
///
///
///
/// If no error occurs, the InetPton function returns a value of 1 and the buffer pointed to by the pAddrBuf parameter
/// contains the binary numeric IP address in network byte order.
///
///
/// The InetPton function returns a value of 0 if the pAddrBuf parameter points to a string that is not a valid IPv4
/// dotted-decimal string or a valid IPv6 address string. Otherwise, a value of -1 is returned, and a specific error code can be
/// retrieved by calling the WSAGetLastError for extended error information.
///
/// If the function has an error, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// WSAEFAULT
/// The pszAddrString or pAddrBuf parameters are NULL or are not part of the user address space.
///
///
///
///
/// The InetPton function is supported on Windows Vistaand later.
///
/// The InetPton function provides a protocol-independent conversion of an Internet network address in its standard text
/// presentation form into its numeric binary form. The InetPton function takes a text representation of an Internet address
/// pointed to by the pszAddrString parameter and returns a pointer to the numeric binary IP address in the pAddrBuf parameter.
/// While the inet_addrfunction works only with IPv4 address strings, the InetPton function works with either IPv4 or IPv6
/// address strings.
///
///
/// The ANSI version of this function is inet_pton as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
///
/// The InetPton function does not require that the Windows Sockets DLL be loaded to perform conversion of a text string that
/// represents an IP address to a numeric binary IP address.
///
///
/// If the Family parameter specified is AF_INET, then the pszAddrString parameter must point a text string of an IPv4
/// address in dotted-decimal notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pszAddrString parameter must point a text string of an IPv6
/// address in Internet standard format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A
/// string of consecutive zero numbers may be replaced with a double-colon. There can only be one double-colon in the string
/// representation of the IPv6 address. The last 32 bits may be represented in IPv4-style dotted-octet notation if the address is a
/// IPv4-compatible address.
///
///
/// When UNICODE or _UNICODE is defined, InetPton is defined to InetPtonW, the Unicode version of this function. The
/// pszAddrString parameter is defined to the PCWSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_pton. The pszAddrString parameter is defined to the PCSTR data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4StringToAddress and RtlIpv4StringToAddressEx functions can be used to convert a text
/// representation of an IPv4 address in Internet standard dotted-decimal notation to a numeric binary address represented as an
/// IN_ADDR structure. On Windows Vista and later, the RtlIpv6StringToAddress and RtlIpv6StringToAddressEx functions can be used to
/// convert a string representation of an IPv6 address to a numeric binary IPv6 address represented as an IN6_ADDR structure. The
/// RtlIpv6StringToAddressEx function is more flexible since it also converts a string representation of an IPv6 address that
/// can include a scope ID and port in standard notation to a numeric binary form.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetPtonW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inet_pton INT WSAAPI inet_pton( INT Family, PCSTR
// pszAddrString, PVOID pAddrBuf );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Ansi)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "d0705997-0dc7-443b-a43f-611301cc9169")]
public static extern int inet_pton(ADDRESS_FAMILY Family, string pszAddrString, out IN_ADDR pAddrBuf);
///
/// The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its
/// numeric binary form. The ANSI version of this function is inet_pton.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv4 address and the pAddrBuf parameter returns a pointer to an IN_ADDR structure that represents
/// the IPv4 address.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv6 address and the pAddrBuf parameter returns a pointer to an IN6_ADDR structure that
/// represents the IPv6 address.
///
///
///
///
///
///
/// A pointer to the NULL-terminated string that contains the text representation of the IP address to convert to numeric
/// binary form.
///
///
/// When the Family parameter is AF_INET, then the pszAddrString parameter must point to a text representation of an IPv4
/// address in standard dotted-decimal notation.
///
///
/// When the Family parameter is AF_INET6, then the pszAddrString parameter must point to a text representation of an IPv6
/// address in standard notation.
///
///
///
///
/// A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in
/// network byte order.
///
/// When the Family parameter is AF_INET, this buffer should be large enough to hold an IN_ADDR structure.
/// When the Family parameter is AF_INET6, this buffer should be large enough to hold an IN6_ADDR structure.
///
///
///
/// If no error occurs, the InetPton function returns a value of 1 and the buffer pointed to by the pAddrBuf parameter
/// contains the binary numeric IP address in network byte order.
///
///
/// The InetPton function returns a value of 0 if the pAddrBuf parameter points to a string that is not a valid IPv4
/// dotted-decimal string or a valid IPv6 address string. Otherwise, a value of -1 is returned, and a specific error code can be
/// retrieved by calling the WSAGetLastError for extended error information.
///
/// If the function has an error, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// WSAEFAULT
/// The pszAddrString or pAddrBuf parameters are NULL or are not part of the user address space.
///
///
///
///
/// The InetPton function is supported on Windows Vistaand later.
///
/// The InetPton function provides a protocol-independent conversion of an Internet network address in its standard text
/// presentation form into its numeric binary form. The InetPton function takes a text representation of an Internet address
/// pointed to by the pszAddrString parameter and returns a pointer to the numeric binary IP address in the pAddrBuf parameter.
/// While the inet_addrfunction works only with IPv4 address strings, the InetPton function works with either IPv4 or IPv6
/// address strings.
///
///
/// The ANSI version of this function is inet_pton as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
///
/// The InetPton function does not require that the Windows Sockets DLL be loaded to perform conversion of a text string that
/// represents an IP address to a numeric binary IP address.
///
///
/// If the Family parameter specified is AF_INET, then the pszAddrString parameter must point a text string of an IPv4
/// address in dotted-decimal notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pszAddrString parameter must point a text string of an IPv6
/// address in Internet standard format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A
/// string of consecutive zero numbers may be replaced with a double-colon. There can only be one double-colon in the string
/// representation of the IPv6 address. The last 32 bits may be represented in IPv4-style dotted-octet notation if the address is a
/// IPv4-compatible address.
///
///
/// When UNICODE or _UNICODE is defined, InetPton is defined to InetPtonW, the Unicode version of this function. The
/// pszAddrString parameter is defined to the PCWSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_pton. The pszAddrString parameter is defined to the PCSTR data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4StringToAddress and RtlIpv4StringToAddressEx functions can be used to convert a text
/// representation of an IPv4 address in Internet standard dotted-decimal notation to a numeric binary address represented as an
/// IN_ADDR structure. On Windows Vista and later, the RtlIpv6StringToAddress and RtlIpv6StringToAddressEx functions can be used to
/// convert a string representation of an IPv6 address to a numeric binary IPv6 address represented as an IN6_ADDR structure. The
/// RtlIpv6StringToAddressEx function is more flexible since it also converts a string representation of an IPv6 address that
/// can include a scope ID and port in standard notation to a numeric binary form.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetPtonW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inet_pton INT WSAAPI inet_pton( INT Family, PCSTR
// pszAddrString, PVOID pAddrBuf );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Ansi)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "d0705997-0dc7-443b-a43f-611301cc9169")]
public static extern int inet_pton(ADDRESS_FAMILY Family, string pszAddrString, out IN6_ADDR pAddrBuf);
///
/// The InetNtop function converts an IPv4 or IPv6 Internet network address into a string in Internet standard format. The
/// ANSI version of this function is inet_ntop.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, this function returns an IPv4 address string.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, this function returns an IPv6 address string.
///
///
///
///
///
/// A pointer to the IP address in network byte to convert to a string.
///
/// When the Family parameter is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4 address
/// to convert.
///
///
/// When the Family parameter is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6 address
/// to convert.
///
///
///
/// A pointer to a buffer in which to store the NULL-terminated string representation of the IP address.
/// For an IPv4 address, this buffer should be large enough to hold at least 16 characters.
/// For an IPv6 address, this buffer should be large enough to hold at least 46 characters.
///
/// On input, the length, in characters, of the buffer pointed to by the pStringBuf parameter.
///
///
/// If no error occurs, InetNtop function returns a pointer to a buffer containing the string representation of IP address in
/// standard format.
///
///
/// Otherwise, a value of NULL is returned, and a specific error code can be retrieved by calling the WSAGetLastError for
/// extended error information.
///
/// If the function fails, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// ERROR_INVALID_PARAMETER
///
/// An invalid parameter was passed to the function. This error is returned if a NULL pointer is passed in the pStringBuf or the
/// StringBufSize parameter is zero. This error is also returned if the length of the buffer pointed to by the pStringBuf parameter
/// is not large enough to receive the string representation of the IP address.
///
///
///
///
///
/// The InetNtop function is supported on Windows Vista and later.
///
/// The InetNtop function provides a protocol-independent address-to-string translation. The InetNtop function takes
/// an Internet address structure specified by the pAddr parameter and returns a NULL-terminated string that represents the
/// IP address. While the inet_ntoa function works only with IPv4 addresses, the InetNtop function works with either IPv4 or
/// IPv6 addresses.
///
///
/// The ANSI version of this function is inet_ntop as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
/// The InetNtop function does not require that the Windows Sockets DLL be loaded to perform IP address to string conversion.
///
/// If the Family parameter specified is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in dotted-decimal
/// notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in Internet standard
/// format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A string of consecutive zero
/// numbers is replaced with a double-colon. There can only be one double-colon in the string representation of the IPv6 address.
/// The last 32 bits are represented in IPv4-style dotted-octet notation if the address is a IPv4-compatible address.
///
///
/// If the length of the buffer pointed to by the pStringBuf parameter is not large enough to receive the string representation of
/// the IP address, InetNtop returns ERROR_INVALID_PARAMETER.
///
///
/// When UNICODE or _UNICODE is defined, InetNtop is defined to InetNtopW, the Unicode version of this function. The
/// pStringBuf parameter is defined to the PSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetNtop is defined to InetNtopA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_ntop. The pStringBuf parameter is defined to the PWSTR
/// data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4AddressToString and RtlIpv4AddressToStringEx functions can be used to convert an IPv4
/// address represented as an IN_ADDR structure to a string representation of an IPv4 address in Internet standard dotted-decimal
/// notation. On Windows Vista and later, the RtlIpv6AddressToString and RtlIpv6AddressToStringEx functions can be used to convert
/// an IPv6 address represented as an IN6_ADDR structure to a string representation of an IPv6 address. The
/// RtlIpv6AddressToStringEx function is more flexible since it also converts an IPv6 address, scope ID, and port to a IPv6
/// string in standard format.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetNtopW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetntopw PCWSTR WSAAPI InetNtopW( INT Family, const VOID
// *pAddr, PWSTR pStringBuf, size_t StringBufSize );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "1e26b88c-808f-4807-8641-e5c6b10853ad")]
public static extern StrPtrUni InetNtopW(ADDRESS_FAMILY Family, in IN_ADDR pAddr, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pStringBuf, SizeT StringBufSize);
///
/// The InetNtop function converts an IPv4 or IPv6 Internet network address into a string in Internet standard format. The
/// ANSI version of this function is inet_ntop.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, this function returns an IPv4 address string.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, this function returns an IPv6 address string.
///
///
///
///
///
/// A pointer to the IP address in network byte to convert to a string.
///
/// When the Family parameter is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4 address
/// to convert.
///
///
/// When the Family parameter is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6 address
/// to convert.
///
///
///
/// A pointer to a buffer in which to store the NULL-terminated string representation of the IP address.
/// For an IPv4 address, this buffer should be large enough to hold at least 16 characters.
/// For an IPv6 address, this buffer should be large enough to hold at least 46 characters.
///
/// On input, the length, in characters, of the buffer pointed to by the pStringBuf parameter.
///
///
/// If no error occurs, InetNtop function returns a pointer to a buffer containing the string representation of IP address in
/// standard format.
///
///
/// Otherwise, a value of NULL is returned, and a specific error code can be retrieved by calling the WSAGetLastError for
/// extended error information.
///
/// If the function fails, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// ERROR_INVALID_PARAMETER
///
/// An invalid parameter was passed to the function. This error is returned if a NULL pointer is passed in the pStringBuf or the
/// StringBufSize parameter is zero. This error is also returned if the length of the buffer pointed to by the pStringBuf parameter
/// is not large enough to receive the string representation of the IP address.
///
///
///
///
///
/// The InetNtop function is supported on Windows Vista and later.
///
/// The InetNtop function provides a protocol-independent address-to-string translation. The InetNtop function takes
/// an Internet address structure specified by the pAddr parameter and returns a NULL-terminated string that represents the
/// IP address. While the inet_ntoa function works only with IPv4 addresses, the InetNtop function works with either IPv4 or
/// IPv6 addresses.
///
///
/// The ANSI version of this function is inet_ntop as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
/// The InetNtop function does not require that the Windows Sockets DLL be loaded to perform IP address to string conversion.
///
/// If the Family parameter specified is AF_INET, then the pAddr parameter must point to an IN_ADDR structure with the IPv4
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in dotted-decimal
/// notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pAddr parameter must point to an IN6_ADDR structure with the IPv6
/// address to convert. The address string returned in the buffer pointed to by the pStringBuf parameter is in Internet standard
/// format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A string of consecutive zero
/// numbers is replaced with a double-colon. There can only be one double-colon in the string representation of the IPv6 address.
/// The last 32 bits are represented in IPv4-style dotted-octet notation if the address is a IPv4-compatible address.
///
///
/// If the length of the buffer pointed to by the pStringBuf parameter is not large enough to receive the string representation of
/// the IP address, InetNtop returns ERROR_INVALID_PARAMETER.
///
///
/// When UNICODE or _UNICODE is defined, InetNtop is defined to InetNtopW, the Unicode version of this function. The
/// pStringBuf parameter is defined to the PSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetNtop is defined to InetNtopA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_ntop. The pStringBuf parameter is defined to the PWSTR
/// data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4AddressToString and RtlIpv4AddressToStringEx functions can be used to convert an IPv4
/// address represented as an IN_ADDR structure to a string representation of an IPv4 address in Internet standard dotted-decimal
/// notation. On Windows Vista and later, the RtlIpv6AddressToString and RtlIpv6AddressToStringEx functions can be used to convert
/// an IPv6 address represented as an IN6_ADDR structure to a string representation of an IPv6 address. The
/// RtlIpv6AddressToStringEx function is more flexible since it also converts an IPv6 address, scope ID, and port to a IPv6
/// string in standard format.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetNtopW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetntopw PCWSTR WSAAPI InetNtopW( INT Family, const VOID
// *pAddr, PWSTR pStringBuf, size_t StringBufSize );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "1e26b88c-808f-4807-8641-e5c6b10853ad")]
public static extern StrPtrUni InetNtopW(ADDRESS_FAMILY Family, in IN6_ADDR pAddr, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pStringBuf, SizeT StringBufSize);
///
/// The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its
/// numeric binary form. The ANSI version of this function is inet_pton.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv4 address and the pAddrBuf parameter returns a pointer to an IN_ADDR structure that represents
/// the IPv4 address.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv6 address and the pAddrBuf parameter returns a pointer to an IN6_ADDR structure that
/// represents the IPv6 address.
///
///
///
///
///
///
/// A pointer to the NULL-terminated string that contains the text representation of the IP address to convert to numeric
/// binary form.
///
///
/// When the Family parameter is AF_INET, then the pszAddrString parameter must point to a text representation of an IPv4
/// address in standard dotted-decimal notation.
///
///
/// When the Family parameter is AF_INET6, then the pszAddrString parameter must point to a text representation of an IPv6
/// address in standard notation.
///
///
///
///
/// A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in
/// network byte order.
///
/// When the Family parameter is AF_INET, this buffer should be large enough to hold an IN_ADDR structure.
/// When the Family parameter is AF_INET6, this buffer should be large enough to hold an IN6_ADDR structure.
///
///
///
/// If no error occurs, the InetPton function returns a value of 1 and the buffer pointed to by the pAddrBuf parameter
/// contains the binary numeric IP address in network byte order.
///
///
/// The InetPton function returns a value of 0 if the pAddrBuf parameter points to a string that is not a valid IPv4
/// dotted-decimal string or a valid IPv6 address string. Otherwise, a value of -1 is returned, and a specific error code can be
/// retrieved by calling the WSAGetLastError for extended error information.
///
/// If the function has an error, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// WSAEFAULT
/// The pszAddrString or pAddrBuf parameters are NULL or are not part of the user address space.
///
///
///
///
/// The InetPton function is supported on Windows Vistaand later.
///
/// The InetPton function provides a protocol-independent conversion of an Internet network address in its standard text
/// presentation form into its numeric binary form. The InetPton function takes a text representation of an Internet address
/// pointed to by the pszAddrString parameter and returns a pointer to the numeric binary IP address in the pAddrBuf parameter.
/// While the inet_addrfunction works only with IPv4 address strings, the InetPton function works with either IPv4 or IPv6
/// address strings.
///
///
/// The ANSI version of this function is inet_pton as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
///
/// The InetPton function does not require that the Windows Sockets DLL be loaded to perform conversion of a text string that
/// represents an IP address to a numeric binary IP address.
///
///
/// If the Family parameter specified is AF_INET, then the pszAddrString parameter must point a text string of an IPv4
/// address in dotted-decimal notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pszAddrString parameter must point a text string of an IPv6
/// address in Internet standard format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A
/// string of consecutive zero numbers may be replaced with a double-colon. There can only be one double-colon in the string
/// representation of the IPv6 address. The last 32 bits may be represented in IPv4-style dotted-octet notation if the address is a
/// IPv4-compatible address.
///
///
/// When UNICODE or _UNICODE is defined, InetPton is defined to InetPtonW, the Unicode version of this function. The
/// pszAddrString parameter is defined to the PCWSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_pton. The pszAddrString parameter is defined to the PCSTR data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4StringToAddress and RtlIpv4StringToAddressEx functions can be used to convert a text
/// representation of an IPv4 address in Internet standard dotted-decimal notation to a numeric binary address represented as an
/// IN_ADDR structure. On Windows Vista and later, the RtlIpv6StringToAddress and RtlIpv6StringToAddressEx functions can be used to
/// convert a string representation of an IPv6 address to a numeric binary IPv6 address represented as an IN6_ADDR structure. The
/// RtlIpv6StringToAddressEx function is more flexible since it also converts a string representation of an IPv6 address that
/// can include a scope ID and port in standard notation to a numeric binary form.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetPtonW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetptonw INT WSAAPI InetPtonW( INT Family, PCWSTR
// pszAddrString, PVOID pAddrBuf );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "d0705997-0dc7-443b-a43f-611301cc9169")]
public static extern int InetPtonW(ADDRESS_FAMILY Family, [MarshalAs(UnmanagedType.LPWStr)] string pszAddrString, out IN_ADDR pAddrBuf);
///
/// The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its
/// numeric binary form. The ANSI version of this function is inet_pton.
///
///
/// The address family.
///
/// Possible values for the address family are defined in the Ws2def.h header file. Note that the Ws2def.h header file is
/// automatically included in Winsock2.h, and should never be used directly. Note that the values for the AF_ address family and PF_
/// protocol family constants are identical (for example, AF_INET and PF_INET), so either constant can be used.
///
/// The values currently supported are AF_INET and AF_INET6.
///
///
/// Value
/// Meaning
///
/// -
/// AF_INET 2
///
/// The Internet Protocol version 4 (IPv4) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv4 address and the pAddrBuf parameter returns a pointer to an IN_ADDR structure that represents
/// the IPv4 address.
///
///
/// -
/// AF_INET6 23
///
/// The Internet Protocol version 6 (IPv6) address family. When this parameter is specified, the pszAddrString parameter must point
/// to a text representation of an IPv6 address and the pAddrBuf parameter returns a pointer to an IN6_ADDR structure that
/// represents the IPv6 address.
///
///
///
///
///
///
/// A pointer to the NULL-terminated string that contains the text representation of the IP address to convert to numeric
/// binary form.
///
///
/// When the Family parameter is AF_INET, then the pszAddrString parameter must point to a text representation of an IPv4
/// address in standard dotted-decimal notation.
///
///
/// When the Family parameter is AF_INET6, then the pszAddrString parameter must point to a text representation of an IPv6
/// address in standard notation.
///
///
///
///
/// A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in
/// network byte order.
///
/// When the Family parameter is AF_INET, this buffer should be large enough to hold an IN_ADDR structure.
/// When the Family parameter is AF_INET6, this buffer should be large enough to hold an IN6_ADDR structure.
///
///
///
/// If no error occurs, the InetPton function returns a value of 1 and the buffer pointed to by the pAddrBuf parameter
/// contains the binary numeric IP address in network byte order.
///
///
/// The InetPton function returns a value of 0 if the pAddrBuf parameter points to a string that is not a valid IPv4
/// dotted-decimal string or a valid IPv6 address string. Otherwise, a value of -1 is returned, and a specific error code can be
/// retrieved by calling the WSAGetLastError for extended error information.
///
/// If the function has an error, the extended error code returned by WSAGetLastError can be one of the following values.
///
///
/// Error code
/// Meaning
///
/// -
/// WSAEAFNOSUPPORT
///
/// The address family specified in the Family parameter is not supported. This error is returned if the Family parameter specified
/// was not AF_INET or AF_INET6.
///
///
/// -
/// WSAEFAULT
/// The pszAddrString or pAddrBuf parameters are NULL or are not part of the user address space.
///
///
///
///
/// The InetPton function is supported on Windows Vistaand later.
///
/// The InetPton function provides a protocol-independent conversion of an Internet network address in its standard text
/// presentation form into its numeric binary form. The InetPton function takes a text representation of an Internet address
/// pointed to by the pszAddrString parameter and returns a pointer to the numeric binary IP address in the pAddrBuf parameter.
/// While the inet_addrfunction works only with IPv4 address strings, the InetPton function works with either IPv4 or IPv6
/// address strings.
///
///
/// The ANSI version of this function is inet_pton as defined in RFC 2553. For more information, see RFC 2553 available at
/// the IETF website.
///
///
/// The InetPton function does not require that the Windows Sockets DLL be loaded to perform conversion of a text string that
/// represents an IP address to a numeric binary IP address.
///
///
/// If the Family parameter specified is AF_INET, then the pszAddrString parameter must point a text string of an IPv4
/// address in dotted-decimal notation as in "192.168.16.0", an example of an IPv4 address in dotted-decimal notation.
///
///
/// If the Family parameter specified is AF_INET6, then the pszAddrString parameter must point a text string of an IPv6
/// address in Internet standard format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A
/// string of consecutive zero numbers may be replaced with a double-colon. There can only be one double-colon in the string
/// representation of the IPv6 address. The last 32 bits may be represented in IPv4-style dotted-octet notation if the address is a
/// IPv4-compatible address.
///
///
/// When UNICODE or _UNICODE is defined, InetPton is defined to InetPtonW, the Unicode version of this function. The
/// pszAddrString parameter is defined to the PCWSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The
/// ANSI version of this function is always defined as inet_pton. The pszAddrString parameter is defined to the PCSTR data type.
///
/// The IN_ADDR structure is defined in the Inaddr.h header file.
/// The IN6_ADDR structure is defined in the In6addr.h header file.
///
/// On Windows Vista and later, the RtlIpv4StringToAddress and RtlIpv4StringToAddressEx functions can be used to convert a text
/// representation of an IPv4 address in Internet standard dotted-decimal notation to a numeric binary address represented as an
/// IN_ADDR structure. On Windows Vista and later, the RtlIpv6StringToAddress and RtlIpv6StringToAddressEx functions can be used to
/// convert a string representation of an IPv6 address to a numeric binary IPv6 address represented as an IN6_ADDR structure. The
/// RtlIpv6StringToAddressEx function is more flexible since it also converts a string representation of an IPv6 address that
/// can include a scope ID and port in standard notation to a numeric binary form.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The InetPtonW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetptonw INT WSAAPI InetPtonW( INT Family, PCWSTR
// pszAddrString, PVOID pAddrBuf );
[DllImport(Lib.Ws2_32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "d0705997-0dc7-443b-a43f-611301cc9169")]
public static extern int InetPtonW(ADDRESS_FAMILY Family, [MarshalAs(UnmanagedType.LPWStr)] string pszAddrString, out IN6_ADDR pAddrBuf);
///
/// The SetAddrInfoEx function registers or deregisters a name, a service name, and associated addresses with a specific
/// namespace provider.
///
///
/// A pointer to a NULL-terminated string containing a name under which addresses are to be registered or deregistered. The
/// interpretation of this parameter specific to the namespace provider.
///
///
/// A pointer to an optional NULL-terminated string that contains the service name associated with the name being registered.
/// The interpretation of this parameter is specific to the namespace provider.
///
/// A pointer to an optional list of addresses to register with the namespace provider.
///
/// The number of addresses passed in pAddresses parameter. If this parameter is zero, the pName parameter is deregistered from the
/// namespace provider.
///
///
/// An optional pointer to data that is used to set provider-specific namespace information that is associated with the pName
/// parameter beyond a list of addresses. Any information that cannot be passed in the pAddresses parameter can be passed in the
/// lpBlob parameter. The format of this information is specific to the namespace provider.
///
///
/// A set of flags controlling how the pName and pServiceName parameters are to be registered with the namespace provider. The
/// interpretation of this information is specific to the namespace provider.
///
///
///
/// A namespace identifier that determines which namespace provider to register this information with. Passing a specific namespace
/// identifier will result in registering this information only with the namespace providers that support the specified namespace.
/// Specifying NS_ALL will result in registering the information with all installed and active namespace providers.
///
///
/// Options for the dwNameSpace parameter are listed in the Winsock2.h include file. Several namespace providers are included with
/// Windows Vista and later. Other namespace providers can be installed, so the following possible values are only those commonly
/// available. Many others are possible.
///
///
///
/// Value
/// Meaning
///
/// -
/// NS_ALL
/// All installed and active namespaces.
///
/// -
/// NS_BTH
/// The Bluetooth namespace. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_DNS
/// The domain name system (DNS) namespace.
///
/// -
/// NS_EMAIL
/// The email namespace. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_NLA
/// The network location awareness (NLA) namespace. This namespace identifier is supported on Windows XP and later.
///
/// -
/// NS_PNRPNAME
/// The peer-to-peer namespace for a specific peer name. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_PNRPCLOUD
///
/// The peer-to-peer namespace for a collection of peer names. This namespace identifier is supported on Windows Vista and later.
///
///
///
///
///
/// A pointer to an optional GUID of a specific namespace provider to register this information with in the case where multiple
/// namespace providers are registered under a single namespace such as NS_DNS. Passing the GUID for a specific namespace provider
/// will result in the information being registered with only the specified namespace provider. The WSAEnumNameSpaceProviders
/// function can be called to retrieve the GUID for a namespace provider.
///
///
/// An optional parameter indicating the time, in milliseconds, to wait for a response from the namespace provider before aborting
/// the call. This parameter is currently reserved and must be set to NULL since a timeout option is not supported.
///
///
/// An optional pointer to an overlapped structure used for asynchronous operation. This parameter is currently reserved and must be
/// set to NULL since asynchronous operations are not supported.
///
///
/// An optional pointer to a function to be invoked upon successful completion for asynchronous operations. This parameter is
/// currently reserved and must be set to NULL since asynchronous operations are not supported.
///
///
/// An optional pointer used only for asynchronous operations. This parameter is currently reserved and must be set to NULL
/// since asynchronous operations are not supported.
///
///
///
/// On success, SetAddrInfoEx returns NO_ERROR (0). Failure returns a nonzero Windows Sockets error code, as found in the
/// Windows Sockets Error Codes.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSANOTINITIALISED
/// A successful WSAStartup call must occur before using this function.
///
/// -
/// WSATRY_AGAIN
/// A temporary failure in name resolution occurred.
///
/// -
/// WSAEINVAL
/// An invalid parameter was provided. This error is returned if any of the reserved parameters are not NULL.
///
/// -
/// WSAENOBUFS
/// Insufficient buffer space is available.
///
/// -
/// WSANO_RECOVERY
/// A nonrecoverable failure in name resolution occurred.
///
/// -
/// WSA_NOT_ENOUGH_MEMORY
/// A memory allocation failure occurred.
///
///
///
///
///
/// The SetAddrInfoEx function provides a protocol-independent method to register or deregister a name and one or more
/// addresses with a namespace provider. The NS_EMAIL namespace provider in Windows Vista and later supports registration and
/// deregistration of addresses. The default NS_DNS, NS_PNRPNAME, and NS_PNRPNAME namespace providers do not currently support name registration.
///
///
/// If the SetAddrInfoEx function is called with NS_ALL set as the dwNameSpace parameter and the lpNspId parameter
/// unspecified, then SetAddrInfoEx will attempt to register or deregister the name and associated addresses with all
/// installed and active namespaces. The SetAddrInfoEx function will return success if any of the namespace providers
/// successfully registered or deregistered the name, but there will not be any indication of which namespace provider succeeded or
/// which ones failed the request.
///
///
/// When UNICODE or _UNICODE is defined, SetAddrInfoEx is defined to SetAddrInfoExW, the Unicode version of
/// this function. The string parameters are defined to the PWSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, SetAddrInfoEx is defined to SetAddrInfoExA, the ANSI version of
/// this function. The string parameters are of the PCSTR data type.
///
///
/// Information that is registered with a namespace provider can be returned by calling the GetAddrInfoEx, getaddrinfo, or
/// GetAddrInfoWfunctions. The GetAddrInfoEx function is an enhanced version of the getaddrinfo and
/// GetAddrInfoW functions.
///
///
/// On Windows Vista and later, when SetAddrInfoEx is called from a service, if the operation is the result of a user process
/// calling the service, then the service should impersonate the user. This is to allow security and routing compartments to be
/// properly enforced.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The SetAddrInfoExW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-setaddrinfoexa INT WSAAPI SetAddrInfoExA( PCSTR pName,
// PCSTR pServiceName, SOCKET_ADDRESS *pAddresses, DWORD dwAddressCount, LPBLOB lpBlob, DWORD dwFlags, DWORD dwNameSpace, LPGUID
// lpNspId, timeval *timeout, LPOVERLAPPED lpOverlapped, LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, LPHANDLE
// lpNameHandle );
[DllImport(Lib.Ws2_32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "6d3c5b97-32ce-4eb5-a047-d9b37c37cdda")]
public static extern int SetAddrInfoEx([MarshalAs(UnmanagedType.LPTStr)] string pName, [Optional, MarshalAs(UnmanagedType.LPTStr)] string pServiceName,
[In, Optional, MarshalAs(UnmanagedType.LPArray)] SOCKET_ADDRESS[] pAddresses, [Optional] uint dwAddressCount, [In, Optional] IntPtr lpBlob, [Optional] uint dwFlags, [Optional] NS dwNameSpace,
in Guid lpNspId, in TIMEVAL timeout, [In, Optional] IntPtr lpOverlapped, [In, Optional] LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, out HANDLE lpNameHandle);
///
/// The SetAddrInfoEx function registers or deregisters a name, a service name, and associated addresses with a specific
/// namespace provider.
///
///
/// A pointer to a NULL-terminated string containing a name under which addresses are to be registered or deregistered. The
/// interpretation of this parameter specific to the namespace provider.
///
///
/// A pointer to an optional NULL-terminated string that contains the service name associated with the name being registered.
/// The interpretation of this parameter is specific to the namespace provider.
///
/// A pointer to an optional list of addresses to register with the namespace provider.
///
/// The number of addresses passed in pAddresses parameter. If this parameter is zero, the pName parameter is deregistered from the
/// namespace provider.
///
///
/// An optional pointer to data that is used to set provider-specific namespace information that is associated with the pName
/// parameter beyond a list of addresses. Any information that cannot be passed in the pAddresses parameter can be passed in the
/// lpBlob parameter. The format of this information is specific to the namespace provider.
///
///
/// A set of flags controlling how the pName and pServiceName parameters are to be registered with the namespace provider. The
/// interpretation of this information is specific to the namespace provider.
///
///
///
/// A namespace identifier that determines which namespace provider to register this information with. Passing a specific namespace
/// identifier will result in registering this information only with the namespace providers that support the specified namespace.
/// Specifying NS_ALL will result in registering the information with all installed and active namespace providers.
///
///
/// Options for the dwNameSpace parameter are listed in the Winsock2.h include file. Several namespace providers are included with
/// Windows Vista and later. Other namespace providers can be installed, so the following possible values are only those commonly
/// available. Many others are possible.
///
///
///
/// Value
/// Meaning
///
/// -
/// NS_ALL
/// All installed and active namespaces.
///
/// -
/// NS_BTH
/// The Bluetooth namespace. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_DNS
/// The domain name system (DNS) namespace.
///
/// -
/// NS_EMAIL
/// The email namespace. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_NLA
/// The network location awareness (NLA) namespace. This namespace identifier is supported on Windows XP and later.
///
/// -
/// NS_PNRPNAME
/// The peer-to-peer namespace for a specific peer name. This namespace identifier is supported on Windows Vista and later.
///
/// -
/// NS_PNRPCLOUD
///
/// The peer-to-peer namespace for a collection of peer names. This namespace identifier is supported on Windows Vista and later.
///
///
///
///
///
/// A pointer to an optional GUID of a specific namespace provider to register this information with in the case where multiple
/// namespace providers are registered under a single namespace such as NS_DNS. Passing the GUID for a specific namespace provider
/// will result in the information being registered with only the specified namespace provider. The WSAEnumNameSpaceProviders
/// function can be called to retrieve the GUID for a namespace provider.
///
///
/// An optional parameter indicating the time, in milliseconds, to wait for a response from the namespace provider before aborting
/// the call. This parameter is currently reserved and must be set to NULL since a timeout option is not supported.
///
///
/// An optional pointer to an overlapped structure used for asynchronous operation. This parameter is currently reserved and must be
/// set to NULL since asynchronous operations are not supported.
///
///
/// An optional pointer to a function to be invoked upon successful completion for asynchronous operations. This parameter is
/// currently reserved and must be set to NULL since asynchronous operations are not supported.
///
///
/// An optional pointer used only for asynchronous operations. This parameter is currently reserved and must be set to NULL
/// since asynchronous operations are not supported.
///
///
///
/// On success, SetAddrInfoEx returns NO_ERROR (0). Failure returns a nonzero Windows Sockets error code, as found in the
/// Windows Sockets Error Codes.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSANOTINITIALISED
/// A successful WSAStartup call must occur before using this function.
///
/// -
/// WSATRY_AGAIN
/// A temporary failure in name resolution occurred.
///
/// -
/// WSAEINVAL
/// An invalid parameter was provided. This error is returned if any of the reserved parameters are not NULL.
///
/// -
/// WSAENOBUFS
/// Insufficient buffer space is available.
///
/// -
/// WSANO_RECOVERY
/// A nonrecoverable failure in name resolution occurred.
///
/// -
/// WSA_NOT_ENOUGH_MEMORY
/// A memory allocation failure occurred.
///
///
///
///
///
/// The SetAddrInfoEx function provides a protocol-independent method to register or deregister a name and one or more
/// addresses with a namespace provider. The NS_EMAIL namespace provider in Windows Vista and later supports registration and
/// deregistration of addresses. The default NS_DNS, NS_PNRPNAME, and NS_PNRPNAME namespace providers do not currently support name registration.
///
///
/// If the SetAddrInfoEx function is called with NS_ALL set as the dwNameSpace parameter and the lpNspId parameter
/// unspecified, then SetAddrInfoEx will attempt to register or deregister the name and associated addresses with all
/// installed and active namespaces. The SetAddrInfoEx function will return success if any of the namespace providers
/// successfully registered or deregistered the name, but there will not be any indication of which namespace provider succeeded or
/// which ones failed the request.
///
///
/// When UNICODE or _UNICODE is defined, SetAddrInfoEx is defined to SetAddrInfoExW, the Unicode version of
/// this function. The string parameters are defined to the PWSTR data type.
///
///
/// When UNICODE or _UNICODE is not defined, SetAddrInfoEx is defined to SetAddrInfoExA, the ANSI version of
/// this function. The string parameters are of the PCSTR data type.
///
///
/// Information that is registered with a namespace provider can be returned by calling the GetAddrInfoEx, getaddrinfo, or
/// GetAddrInfoWfunctions. The GetAddrInfoEx function is an enhanced version of the getaddrinfo and
/// GetAddrInfoW functions.
///
///
/// On Windows Vista and later, when SetAddrInfoEx is called from a service, if the operation is the result of a user process
/// calling the service, then the service should impersonate the user. This is to allow security and routing compartments to be
/// properly enforced.
///
///
/// Windows 8.1 and Windows Server 2012 R2: The SetAddrInfoExW function is supported for Windows Store apps on
/// Windows 8.1, Windows Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-setaddrinfoexa INT WSAAPI SetAddrInfoExA( PCSTR pName,
// PCSTR pServiceName, SOCKET_ADDRESS *pAddresses, DWORD dwAddressCount, LPBLOB lpBlob, DWORD dwFlags, DWORD dwNameSpace, LPGUID
// lpNspId, timeval *timeout, LPOVERLAPPED lpOverlapped, LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, LPHANDLE
// lpNameHandle );
[DllImport(Lib.Ws2_32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("ws2tcpip.h", MSDNShortId = "6d3c5b97-32ce-4eb5-a047-d9b37c37cdda")]
public static unsafe extern int SetAddrInfoEx([MarshalAs(UnmanagedType.LPTStr)] string pName, [Optional, MarshalAs(UnmanagedType.LPTStr)] string pServiceName,
[In, Optional, MarshalAs(UnmanagedType.LPArray)] SOCKET_ADDRESS[] pAddresses, [Optional] uint dwAddressCount, [In, Optional] IntPtr lpBlob,
[Optional] uint dwFlags, [Optional] NS dwNameSpace, [In, Optional] Guid* lpNspId, [In, Optional] TIMEVAL* timeout,
[In, Optional] NativeOverlapped* lpOverlapped, [In, Optional] LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, [Out, Optional] HANDLE* lpNameHandle);
/// The setipv4sourcefilter inline function sets the multicast filter state for an IPv4 socket.
/// A descriptor that identifies a multicast socket.
///
/// The local IPv4 address of the interface or the interface index on which the multicast group should be joined or dropped.
///
/// This value is in network byte order. If this member specifies an IPv4 address of 0.0.0.0, the default IPv4 multicast interface
/// is used.
///
///
/// Any IP address in the 0.x.x.x block (first octet of 0) except IPv4 address 0.0.0.0 is treated as an interface index. An
/// interface index is a 24-bit number, and the 0.0.0.0/8 IPv4 address block is not used (this range is reserved).
///
/// To use an interface index of 1 would be the same as an IP address of 0.0.0.1.
///
/// The IPv4 address of the multicast group.
/// The multicast filter mode for multicast group address.
/// The number of source addresses in the buffer pointed to by the SourceList parameter.
/// A pointer to a buffer with the IP addresses to associate with the multicast filter.
///
///
/// On success, setipv4sourcefilter returns NO_ERROR (0). Any nonzero return value indicates failure and a specific error
/// code can be retrieved by calling WSAGetLastError.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSAENOBUFS
/// Insufficient buffer space is available.
///
/// -
/// WSAENOTSOCK
/// The descriptor is not a socket.
///
///
///
///
/// The setipv4sourcefilter inline function is used to set the multicast filter state for an IPv4 socket.
///
/// This function is part of socket interface extensions for multicast source filters defined in RFC 3678. An app can use these
/// functions to retrieve and set the multicast source address filters associated with a socket.
///
/// Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-setipv4sourcefilter int setipv4sourcefilter( SOCKET
// Socket, IN_ADDR Interface, IN_ADDR Group, MULTICAST_MODE_TYPE FilterMode, ULONG SourceCount, const IN_ADDR *SourceList );
[PInvokeData("ws2tcpip.h", MSDNShortId = "C296D050-9195-42B5-8EBE-C6004F2DA855")]
public static Win32Error setipv4sourcefilter(SOCKET Socket, IN_ADDR Interface, IN_ADDR Group, MULTICAST_MODE_TYPE FilterMode, uint SourceCount, IN_ADDR[] SourceList)
{
if (SourceCount > SourceList.Length)
{
WSASetLastError(unchecked((int)Win32Error.WSAENOBUFS));
return SOCKET_ERROR;
}
var Filter = new IP_MSFILTER
{
imsf_multiaddr = Group,
imsf_interface = Interface,
imsf_fmode = FilterMode,
imsf_numsrc = SourceCount,
imsf_slist = SourceList
};
using var pFilter = SafeHGlobalHandle.CreateFromStructure(Filter);
return WSAIoctl(Socket, SIOCSIPMSFILTER, pFilter, pFilter.Size, default, 0, out _);
}
/// The setsourcefilter inline function sets the multicast filter state for an IPv4 or IPv6 socket.
/// A descriptor that identifies a multicast socket.
/// The interface index of the multicast interface.
/// A pointer to the socket address of the multicast group.
/// The length, in bytes, of the socket address pointed to by the Group parameter.
/// The multicast filter mode for the multicast group address.
/// The number of source addresses in the buffer pointed to by the SourceList parameter.
/// A pointer to a buffer with the IP addresses to associate with the multicast filter.
///
///
/// On success, setsourcefilter returns NO_ERROR (0). Any nonzero return value indicates failure and a specific error code
/// can be retrieved by calling WSAGetLastError.
///
///
///
/// Error code
/// Meaning
///
/// -
/// WSAENOBUFS
/// Insufficient buffer space is available.
///
/// -
/// WSAENOTSOCK
/// The descriptor is not a socket.
///
///
///
///
/// The setsourcefilter inline function is used to set the multicast filter state for an IPv4 or IPv6 socket.
///
/// This function is part of socket interface extensions for multicast source filters defined in RFC 3678. An app can use these
/// functions to retrieve and set the multicast source address filters associated with a socket.
///
/// Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.
///
/// Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows
/// Server 2012 R2, and later.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-setsourcefilter int setsourcefilter( SOCKET Socket, ULONG
// Interface, const SOCKADDR *Group, int GroupLength, MULTICAST_MODE_TYPE FilterMode, ULONG SourceCount, const SOCKADDR_STORAGE
// *SourceList );
[PInvokeData("ws2tcpip.h", MSDNShortId = "320455F3-FDFB-46C6-9F26-3C60064A2CB0")]
public static Win32Error setsourcefilter(SOCKET Socket, uint Interface, [In] SOCKADDR Group, int GroupLength, MULTICAST_MODE_TYPE FilterMode, uint SourceCount, SOCKADDR_STORAGE[] SourceList)
{
if (SourceCount > SourceList.Length || GroupLength > Group.Size)
{
WSASetLastError(unchecked((int)Win32Error.WSAENOBUFS));
return SOCKET_ERROR;
}
var Filter = new GROUP_FILTER
{
gf_interface = Interface,
gf_fmode = FilterMode,
gf_numsrc = SourceCount,
gf_slist = SourceList
};
using var pFilter = SafeHGlobalHandle.CreateFromStructure(Filter);
Group.DangerousGetHandle().CopyTo(pFilter.DangerousGetHandle().Offset(4), Group.Size);
return WSAIoctl(Socket, SIOCSMSFILTER, pFilter, pFilter.Size, default, 0, out _);
}
/// Provides a for an array of that is disposed using .
public class SafeADDRINFOWArray : SafeHANDLE, IEnumerable
{
/// Initializes a new instance of the class.
private SafeADDRINFOWArray() : base() { }
/// Gets the number of elements contained in the .
public int Length => IsInvalid ? 0 : Items.Count();
/// Enumerates the elements.
/// An enumeration of values from the pointer.
protected virtual IEnumerable Items => handle.LinkedListToIEnum(ai => ai.ai_next);
/// Gets or sets the value at the specified index.
/// The index of the info within the array.
/// The value.
/// index or index
public ADDRINFOW this[int index] => Items.ElementAt(index);
/// Determines whether this instance contains the object.
/// The object to locate in the .
///
/// if is found in the ;
/// otherwise, .
///
public bool Contains(ADDRINFOW item) => Items.Contains(item);
/// Returns an enumerator that iterates through the collection.
/// A that can be used to iterate through the collection.
public IEnumerator GetEnumerator() => Items.GetEnumerator();
/// Returns an enumerator that iterates through a collection.
/// An object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
///
protected override bool InternalReleaseHandle() { FreeAddrInfoW(handle); return true; }
}
/// Provides a for an array of that is disposed using .
public class SafeADDRINFOEXWArray : SafeHANDLE, IEnumerable
{
/// Initializes a new instance of the class.
private SafeADDRINFOEXWArray() : base() { }
/// Gets the number of elements contained in the .
public int Length => IsInvalid ? 0 : Items.Count();
/// Enumerates the elements.
/// An enumeration of values from the pointer.
protected virtual IEnumerable Items => handle.LinkedListToIEnum(ai => ai.ai_next);
/// Gets or sets the value at the specified index.
/// The index of the info within the array.
/// The value.
/// index or index
public ADDRINFOEXW this[int index] => Items.ElementAt(index);
/// Determines whether this instance contains the object.
/// The object to locate in the .
///
/// if is found in the ;
/// otherwise, .
///
public bool Contains(ADDRINFOEXW item) => Items.Contains(item);
/// Returns an enumerator that iterates through the collection.
/// A that can be used to iterate through the collection.
public IEnumerator GetEnumerator() => Items.GetEnumerator();
/// Returns an enumerator that iterates through a collection.
/// An object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
///
protected override bool InternalReleaseHandle() { FreeAddrInfoExW(handle); return true; }
}
}
}
#pragma warning restore IDE1006 // Naming Styles