using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { /// Items from Dhcpcsvc.dll for Multicast Address Dynamic Client Allocation Protocol (MADCAP). public static partial class MADCAP { /// Required length of the buffer for . public const int MCAST_CLIENT_ID_LEN = 17; private const string Lib_Dhcpcsvc = "dhcpcsvc.dll"; /// /// The McastApiCleanup function deallocates resources that are allocated with McastApiStartup. The McastApiCleanup /// function must only be called after a successful call to McastApiStartup. /// /// None // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastapicleanup void McastApiCleanup(); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastApiCleanup")] public static extern void McastApiCleanup(); /// /// The McastApiStartup function facilitates MADCAP-version negotiation between requesting clients and the version of MADCAP /// implemented on the system. Calling McastApiStartup allocates necessary resources; it must be called before any other /// MADCAP client functions are called. /// /// /// Pointer to the version of multicast (MCAST) that the client wishes to use. /// [out] Pointer to the version of MCAST implemented on the system. /// /// /// If the client requests a version of MADCAP that is not supported by the system, the McastApiStartup function returns /// ERROR_NOT_SUPPORTED. If resources fail to be allocated for the function call, ERROR_NO_SYSTEM_RESOURCES is returned. /// /// /// /// Clients can specify which version they want to use in the pVersion parameter. If the system's implementation supports the /// requested MCAST version, the function call succeeds. If the system's implementation does not support the requested version, the /// function fails with MCAST_API_CURRENT_VERSION. /// /// /// The client can automatically negotiate the first version of MCAST (MCAST_API_VERSION_1) by setting the pVersion parameter to zero. /// /// /// The McastApiStartup function always returns the most recent version of MADCAP available on the system /// (MCAST_API_CURRENT_VERSION) in pVersion, enabling clients to discover the most recent version implemented on the system. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastapistartup DWORD McastApiStartup( PDWORD Version ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastApiStartup")] public static extern Win32Error McastApiStartup(out uint Version); /// The McastEnumerateScopes function enumerates multicast scopes available on the network. /// /// Specifies the address family to be used in enumeration, in the form of an IPNG_ADDRESS structure. Use AF_INET for IPv4 addresses /// and AF_INET6 for IPv6 addresses. /// /// /// Enables a caller to query a list again. Set this parameter to TRUE if the list is to be queried more than once. /// Otherwise, set it to FALSE. /// /// /// /// Pointer to a buffer used for storing scope list information, in the form of an MCAST_SCOPE_ENTRY structure. The return value of /// pScopeList depends on its input value, and on the value of the buffer to which it points: /// /// If pScopeList is a valid pointer on input, the scope list is returned. /// If pScopeList is NULL on input, the length of the buffer required to hold the scope list is returned. /// /// If the buffer pointed to in pScopeList is NULL on input, McastEnumerateScopes forces a repeat querying of scope /// lists from MCAST servers. /// /// /// To determine the size of buffer required to hold scope list data, set pScopeList to NULL and pScopeLen to a non- /// NULL value. The McastEnumerateScopes function will then return ERROR_SUCCESS and store the size of the scope list /// data, in bytes, in pScopeLen. /// /// /// /// /// Pointer to a value used to communicate the size of data or buffer space in pScopeList. On input, pScopeLen points to the size, /// in bytes, of the buffer pointed to by pScopeList. On return, pScopeLen points to the size of the data copied to pScopeList. /// /// /// The pScopeLen parameter cannot be NULL. If the buffer pointed to by pScopeList is not large enough to hold the scope list /// data, McastEnumerateScopes returns ERROR_MORE_DATA and stores the required buffer size, in bytes, in pScopeLen. /// /// /// To determine the size of buffer required to hold scope list data, set pScopeList to NULL and pScopeLen to a non- /// NULL value. The McastEnumerateScopes function will then return ERROR_SUCCESS and store the size of the scope list /// data, in bytes, in pScopeLen. /// /// /// Pointer to the number of scopes returned in pScopeList. /// /// If the function succeeds, it returns ERROR_SUCCESS. /// /// If the buffer pointed to by pScopeList is too small to hold the scope list, the McastEnumerateScopes function returns /// ERROR_MORE_DATA, and stores the required buffer size, in bytes, in pScopeLen. /// /// /// If the McastApiStartup function has not been called (it must be called before any other MADCAP client functions may be called), /// the McastEnumerateScopes function returns ERROR_NOT_READY. /// /// /// /// The McastEnumerateScopes function queries multicast scopes for each network interface, and the interface on which the /// scope is retrieved is returned as part of the pScopeList parameter. Therefore, on multihomed computers it is possible that some /// scopes will get listed multiple times, once for each interface. /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastenumeratescopes DWORD McastEnumerateScopes( // IP_ADDR_FAMILY AddrFamily, BOOL ReQuery, PMCAST_SCOPE_ENTRY pScopeList, PDWORD pScopeLen, PDWORD pScopeCount ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastEnumerateScopes")] public static extern Win32Error McastEnumerateScopes(System.Net.Sockets.AddressFamily AddrFamily, [MarshalAs(UnmanagedType.Bool)] bool ReQuery, ref MCAST_SCOPE_ENTRY pScopeList, ref uint pScopeLen, out uint pScopeCount); /// The McastEnumerateScopes function enumerates multicast scopes available on the network. /// /// Specifies the address family to be used in enumeration, in the form of an IPNG_ADDRESS structure. Use AF_INET for IPv4 addresses /// and AF_INET6 for IPv6 addresses. /// /// /// Enables a caller to query a list again. Set this parameter to TRUE if the list is to be queried more than once. /// Otherwise, set it to FALSE. /// /// /// /// Pointer to a buffer used for storing scope list information, in the form of an MCAST_SCOPE_ENTRY structure. The return value of /// pScopeList depends on its input value, and on the value of the buffer to which it points: /// /// If pScopeList is a valid pointer on input, the scope list is returned. /// If pScopeList is NULL on input, the length of the buffer required to hold the scope list is returned. /// /// If the buffer pointed to in pScopeList is NULL on input, McastEnumerateScopes forces a repeat querying of scope /// lists from MCAST servers. /// /// /// To determine the size of buffer required to hold scope list data, set pScopeList to NULL and pScopeLen to a non- /// NULL value. The McastEnumerateScopes function will then return ERROR_SUCCESS and store the size of the scope list /// data, in bytes, in pScopeLen. /// /// /// /// /// Pointer to a value used to communicate the size of data or buffer space in pScopeList. On input, pScopeLen points to the size, /// in bytes, of the buffer pointed to by pScopeList. On return, pScopeLen points to the size of the data copied to pScopeList. /// /// /// The pScopeLen parameter cannot be NULL. If the buffer pointed to by pScopeList is not large enough to hold the scope list /// data, McastEnumerateScopes returns ERROR_MORE_DATA and stores the required buffer size, in bytes, in pScopeLen. /// /// /// To determine the size of buffer required to hold scope list data, set pScopeList to NULL and pScopeLen to a non- /// NULL value. The McastEnumerateScopes function will then return ERROR_SUCCESS and store the size of the scope list /// data, in bytes, in pScopeLen. /// /// /// Pointer to the number of scopes returned in pScopeList. /// /// If the function succeeds, it returns ERROR_SUCCESS. /// /// If the buffer pointed to by pScopeList is too small to hold the scope list, the McastEnumerateScopes function returns /// ERROR_MORE_DATA, and stores the required buffer size, in bytes, in pScopeLen. /// /// /// If the McastApiStartup function has not been called (it must be called before any other MADCAP client functions may be called), /// the McastEnumerateScopes function returns ERROR_NOT_READY. /// /// /// /// The McastEnumerateScopes function queries multicast scopes for each network interface, and the interface on which the /// scope is retrieved is returned as part of the pScopeList parameter. Therefore, on multihomed computers it is possible that some /// scopes will get listed multiple times, once for each interface. /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastenumeratescopes DWORD McastEnumerateScopes( // IP_ADDR_FAMILY AddrFamily, BOOL ReQuery, PMCAST_SCOPE_ENTRY pScopeList, PDWORD pScopeLen, PDWORD pScopeCount ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastEnumerateScopes")] public static extern Win32Error McastEnumerateScopes(System.Net.Sockets.AddressFamily AddrFamily, [MarshalAs(UnmanagedType.Bool)] bool ReQuery, [In, Optional] IntPtr pScopeList, ref uint pScopeLen, out uint pScopeCount); /// /// The McastGenUID function generates a unique identifier, subsequently used by clients to request and renew addresses. /// /// /// Pointer to the MCAST_CLIENT_UID structure into which the unique identifier is stored. The size of the buffer to which pRequestID /// points must be at least MCAST_CLIENT_ID_LEN in size. /// /// The McastGenUID function returns the status of the operation. // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastgenuid DWORD McastGenUID( LPMCAST_CLIENT_UID // pRequestID ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastGenUID")] public static extern Win32Error McastGenUID(ref MCAST_CLIENT_UID pRequestID); /// The McastReleaseAddress function releases leased multicast addresses from the MCAST server. /// /// Designates the address family. Use AF_INET for Internet Protocol version 4 (IPv4), and AF_INET6 for Internet Protocol version 6 (IPv6). /// /// Unique identifier used when the address or addresses were initially obtained. /// /// Pointer to the MCAST_LEASE_REQUEST structure containing multicast parameters associated with the release request. /// /// The McastReleaseAddress function returns the status of the operation. // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastreleaseaddress DWORD McastReleaseAddress( // IP_ADDR_FAMILY AddrFamily, LPMCAST_CLIENT_UID pRequestID, PMCAST_LEASE_REQUEST pReleaseRequest ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastReleaseAddress")] public static extern Win32Error McastReleaseAddress(System.Net.Sockets.AddressFamily AddrFamily, in MCAST_CLIENT_UID pRequestID, in MCAST_LEASE_REQUEST pReleaseRequest); /// The McastRenewAddress function renews one or more multicast addresses from a MADCAP server. /// /// Designates the address family. Use AF_INET for Internet Protocol version 4 (IPv4), and AF_INET6 for Internet Protocol version 6 (IPv6). /// /// Unique identifier used when the address or addresses were initially obtained. /// Pointer to the MCAST_LEASE_REQUEST structure containing multicast renew–request parameters. /// /// Pointer to a buffer containing response parameters for the multicast address–renew request, in the form of an /// MCAST_LEASE_RESPONSE structure. The caller is responsible for allocating sufficient buffer space for the pAddrBuf member /// of the MCAST_LEASE_RESPONSE structure to hold the requested number of addresses; the caller is also responsible for /// setting the pointer to that buffer. /// /// The McastRenewAddress function returns the status of the operation. // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastrenewaddress DWORD McastRenewAddress( IP_ADDR_FAMILY // AddrFamily, LPMCAST_CLIENT_UID pRequestID, PMCAST_LEASE_REQUEST pRenewRequest, PMCAST_LEASE_RESPONSE pRenewResponse ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastRenewAddress")] public static extern Win32Error McastRenewAddress(System.Net.Sockets.AddressFamily AddrFamily, in MCAST_CLIENT_UID pRequestID, in MCAST_LEASE_REQUEST pRenewRequest, ref MCAST_LEASE_RESPONSE pRenewResponse); /// The McastRequestAddress function requests one or more multicast addresses from a MADCAP server. /// /// Specifies the address family to be used in the request, in the form of an IPNG_ADDRESS structure. Use AF_INET for IPv4 addresses /// and AF_INET6 for IPv6 addresses. /// /// /// Pointer to a unique identifier for the request, in the form of an MCAST_CLIENT_UID structure. Clients are responsible for /// ensuring that each request contains a unique identifier; unique identifiers can be obtained by calling the McastGenUID function. /// /// /// Pointer to the context of the scope from which the address is to be allocated, in the form of an MCAST_SCOPE_CTX structure. The /// scope context must be retrieved by calling the McastEnumerateScopes function prior to calling the McastRequestAddress function. /// /// Pointer to the MCAST_LEASE_REQUEST structure containing multicast lease–request parameters. /// /// Pointer to a buffer containing response parameters for the multicast address request, in the form of an MCAST_LEASE_RESPONSE /// structure. The caller is responsible for allocating sufficient buffer space for the pAddrBuf member of the /// MCAST_LEASE_RESPONSE structure to hold the requested number of addresses; the caller is also responsible for setting the /// pointer to that buffer. /// /// The McastRequestAddress function returns the status of the operation. /// /// Before the McastRequestAddress function is called, the scope context must be retrieved by calling the /// McastEnumerateScopes function. /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/nf-madcapcl-mcastrequestaddress DWORD McastRequestAddress( // IP_ADDR_FAMILY AddrFamily, LPMCAST_CLIENT_UID pRequestID, PMCAST_SCOPE_CTX pScopeCtx, PMCAST_LEASE_REQUEST pAddrRequest, // PMCAST_LEASE_RESPONSE pAddrResponse ); [DllImport(Lib_Dhcpcsvc, SetLastError = false, ExactSpelling = true)] [PInvokeData("madcapcl.h", MSDNShortId = "NF:madcapcl.McastRequestAddress")] public static extern Win32Error McastRequestAddress(System.Net.Sockets.AddressFamily AddrFamily, in MCAST_CLIENT_UID pRequestID, in MCAST_SCOPE_CTX pScopeCtx, in MCAST_LEASE_REQUEST pAddrRequest, ref MCAST_LEASE_RESPONSE pAddrResponse); /// /// The IPNG_ADDRESS union provides Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6) addresses. /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/ns-madcapcl-ipng_address typedef union _IPNG_ADDRESS { DWORD // IpAddrV4; BYTE IpAddrV6[16]; } IPNG_ADDRESS, *PIPNG_ADDRESS; [PInvokeData("madcapcl.h", MSDNShortId = "NS:madcapcl._IPNG_ADDRESS")] [StructLayout(LayoutKind.Sequential)] public struct IPNG_ADDRESS { /// Internet Protocol (IP) address, in version 6 format (IPv6). [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] IpAddrV6; /// Internet Protocol (IP) address, in version 4 format (IPv4). public uint IpAddrV4 { get => BitConverter.ToUInt32(IpAddrV6, 0); set => Array.Copy(BitConverter.GetBytes(value), IpAddrV6, 4); } } /// The MCAST_CLIENT_UID structure describes the unique client identifier for each multicast request. // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/ns-madcapcl-mcast_client_uid typedef struct _MCAST_CLIENT_UID { // LPBYTE ClientUID; DWORD ClientUIDLength; } MCAST_CLIENT_UID, *LPMCAST_CLIENT_UID; [PInvokeData("madcapcl.h", MSDNShortId = "NS:madcapcl._MCAST_CLIENT_UID")] [StructLayout(LayoutKind.Sequential)] public struct MCAST_CLIENT_UID { /// Buffer containing the unique client identifier. public IntPtr ClientUID; /// Size of the ClientUID member, in bytes. public uint ClientUIDLength; } /// /// The MCAST_LEASE_REQUEST structure defines the request, renew, or release parameters for a given multicast scope. In the /// MCAST_API_VERSION_1 implementation, only one IP address may be allocated at a time. /// /// /// In MCAST_API_VERSION_1 version, MaxLeaseStartTime, MinLeaseDuration, and MinAddrCount members are ignored. /// Clients should still set appropriate values for these members, however, to take advantage of their implementation in future updates. /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/ns-madcapcl-mcast_lease_request typedef struct _MCAST_LEASE_REQUEST { // LONG LeaseStartTime; LONG MaxLeaseStartTime; DWORD LeaseDuration; DWORD MinLeaseDuration; IPNG_ADDRESS ServerAddress; WORD // MinAddrCount; WORD AddrCount; PBYTE pAddrBuf; } MCAST_LEASE_REQUEST, *PMCAST_LEASE_REQUEST; [PInvokeData("madcapcl.h", MSDNShortId = "NS:madcapcl._MCAST_LEASE_REQUEST")] [StructLayout(LayoutKind.Sequential)] public struct MCAST_LEASE_REQUEST { /// /// Requested start time, in seconds, for the multicast scope lease elapsed since midnight of January 1, 1970, coordinated /// universal time. To request the current time as the lease start time, set LeaseStartTime to zero. /// public int LeaseStartTime; /// /// Maximum start time, in seconds, elapsed since midnight of January 1, 1970, coordinated universal time, that the client is /// willing to accept. /// public int MaxLeaseStartTime; /// /// Duration of the lease request, in seconds. To request the default lease duration, set both LeaseDuration and /// MinLeaseDuration to zero. /// public uint LeaseDuration; /// Minimum lease duration, in seconds, that the client is willing to accept. public uint MinLeaseDuration; /// /// Internet Protocol (IP) address of the server on which the lease is to be requested or renewed, in the form of an /// IPNG_ADDRESS structure. If the IP address of the server is unknown, such as when using this structure in an /// McastRequestAddress function call, set ServerAddress to zero. /// public IPNG_ADDRESS ServerAddress; /// Minimum number of IP addresses the client is willing to accept. public ushort MinAddrCount; /// Number of requested IP addresses. Note that the value of this member dictates the size of pAddrBuf. public ushort AddrCount; /// /// Pointer to a buffer containing the requested IP addresses. For IPv4 addresses, the pAddrBuf member points to 4-byte /// addresses; for IPv6 addresses, the pAddrBuf member points to 16-byte addresses. If no specific addresses are /// requested, set pAddrBuf to NULL. /// public IntPtr pAddrBuf; } /// The MCAST_LEASE_RESPONSE structure is used to respond to multicast lease requests. // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/ns-madcapcl-mcast_lease_response typedef struct _MCAST_LEASE_RESPONSE // { LONG LeaseStartTime; LONG LeaseEndTime; IPNG_ADDRESS ServerAddress; WORD AddrCount; PBYTE pAddrBuf; } MCAST_LEASE_RESPONSE, *PMCAST_LEASE_RESPONSE; [PInvokeData("madcapcl.h", MSDNShortId = "NS:madcapcl._MCAST_LEASE_RESPONSE")] [StructLayout(LayoutKind.Sequential)] public struct MCAST_LEASE_RESPONSE { /// /// Start time, in seconds, for the multicast scope lease elapsed since midnight of January 1, 1970, coordinated universal time. /// public int LeaseStartTime; /// /// Expiration time, in seconds of the multicast scope lease elapsed since midnight of January 1, 1970, coordinated universal time. /// public int LeaseEndTime; /// /// Internet Protocol (IP) address of the server on which the lease request has been granted or renewed, in the form of an /// IPNG_ADDRESS structure. /// public IPNG_ADDRESS ServerAddress; /// /// Number of IP addresses that are granted or renewed with the lease. Note that the value of this member dictates the size of pAddrBuf. /// public ushort AddrCount; /// /// Pointer to a buffer containing the granted IP addresses. For IPv4 addresses, the pAddrBuf member points to 4-byte /// addresses; for IPv6 addresses, the pAddrBuf member points to 16-byte addresses. /// public IntPtr pAddrBuf; } /// /// The MCAST_SCOPE_CTX structure defines the scope context for programmatic interaction with multicast addresses. The /// MCAST_SCOPE_CTX structure is used by various MADCAP functions as a handle for allocating, renewing, or releasing MADCAP addresses. /// // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/ns-madcapcl-mcast_scope_ctx typedef struct _MCAST_SCOPE_CTX { // IPNG_ADDRESS ScopeID; IPNG_ADDRESS Interface; IPNG_ADDRESS ServerID; } MCAST_SCOPE_CTX, *PMCAST_SCOPE_CTX; [PInvokeData("madcapcl.h", MSDNShortId = "NS:madcapcl._MCAST_SCOPE_CTX")] [StructLayout(LayoutKind.Sequential)] public struct MCAST_SCOPE_CTX { /// Identifier for the multicast scope, in the form of an IPNG_ADDRESS structure. public IPNG_ADDRESS ScopeID; /// Interface on which the multicast scope is available, in the form of an IPNG_ADDRESS structure. public IPNG_ADDRESS Interface; /// Internet Protocol (IP) address of the MADCAP server, in the form of an IPNG_ADDRESS structure. public IPNG_ADDRESS ServerID; } /// The MCAST_SCOPE_ENTRY structure provides a complete set of information about a given multicast scope. // https://docs.microsoft.com/en-us/windows/win32/api/madcapcl/ns-madcapcl-mcast_scope_entry typedef struct _MCAST_SCOPE_ENTRY { // MCAST_SCOPE_CTX ScopeCtx; IPNG_ADDRESS LastAddr; DWORD TTL; UNICODE_STRING ScopeDesc; } MCAST_SCOPE_ENTRY, *PMCAST_SCOPE_ENTRY; [PInvokeData("madcapcl.h", MSDNShortId = "NS:madcapcl._MCAST_SCOPE_ENTRY")] [StructLayout(LayoutKind.Sequential)] public struct MCAST_SCOPE_ENTRY { /// Handle for the multicast scope, in the form of an MCAST_SCOPE_CTX structure. public MCAST_SCOPE_CTX ScopeCtx; /// Internet Protocol (IP) address of the last address in the scope, in the form of an IPNG_ADDRESS structure. public IPNG_ADDRESS LastAddr; /// Time To Live (TTL) value of the scope. public uint TTL; /// Description of the scope, in human readable, user-friendly format. [MarshalAs(UnmanagedType.LPWStr)] public string ScopeDesc; } } }