diff --git a/PInvoke/Dhcp/CorrelationReport.md b/PInvoke/Dhcp/CorrelationReport.md new file mode 100644 index 000000000..536a27929 --- /dev/null +++ b/PInvoke/Dhcp/CorrelationReport.md @@ -0,0 +1,39 @@ +## Correlation report for dhcpcsvc6.dll, dhcpcsvc.dll +PInvoke API (methods, structures and constants) imported from Windows Dhcpcsvc.dll and Dhcpcsvc6.dll. + +### Methods (66% complete, 14 of 21 functions) +Native Method | Native DLL | Header | Managed Method +--- | --- | --- | --- +[DhcpCApiCleanup](https://www.google.com/search?num=5&q=DhcpCApiCleanup+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpCApiCleanup](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpCApiCleanup) +[DhcpCApiInitialize](https://www.google.com/search?num=5&q=DhcpCApiInitialize+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpCApiInitialize](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpCApiInitialize) +[DhcpDeRegisterParamChange](https://www.google.com/search?num=5&q=DhcpDeRegisterParamChange+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpDeRegisterParamChange](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpDeRegisterParamChange) +[DhcpGetOriginalSubnetMask](https://www.google.com/search?num=5&q=DhcpGetOriginalSubnetMask+site%3Adocs.microsoft.com) | dhcpcsvc.dll | Dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpGetOriginalSubnetMask](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpGetOriginalSubnetMask) +[DhcpRegisterParamChange](https://www.google.com/search?num=5&q=DhcpRegisterParamChange+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpRegisterParamChange](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpRegisterParamChange) +[DhcpRemoveDNSRegistrations](https://www.google.com/search?num=5&q=DhcpRemoveDNSRegistrations+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpRemoveDNSRegistrations](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpRemoveDNSRegistrations) +[DhcpRequestParams](https://www.google.com/search?num=5&q=DhcpRequestParams+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpRequestParams](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpRequestParams) +[DhcpUndoRequestParams](https://www.google.com/search?num=5&q=DhcpUndoRequestParams+site%3Adocs.microsoft.com) | dhcpcsvc.dll | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DhcpUndoRequestParams](https://github.com/dahall/Vanara/search?l=C%23&q=DhcpUndoRequestParams) +[Dhcpv6CApiCleanup](https://www.google.com/search?num=5&q=Dhcpv6CApiCleanup+site%3Adocs.microsoft.com) | dhcpcsvc6.dll | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.Dhcpv6CApiCleanup](https://github.com/dahall/Vanara/search?l=C%23&q=Dhcpv6CApiCleanup) +[Dhcpv6CApiInitialize](https://www.google.com/search?num=5&q=Dhcpv6CApiInitialize+site%3Adocs.microsoft.com) | dhcpcsvc6.dll | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.Dhcpv6CApiInitialize](https://github.com/dahall/Vanara/search?l=C%23&q=Dhcpv6CApiInitialize) +[Dhcpv6ReleasePrefix](https://www.google.com/search?num=5&q=Dhcpv6ReleasePrefix+site%3Adocs.microsoft.com) | dhcpcsvc6.dll | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.Dhcpv6ReleasePrefix](https://github.com/dahall/Vanara/search?l=C%23&q=Dhcpv6ReleasePrefix) +[Dhcpv6RenewPrefix](https://www.google.com/search?num=5&q=Dhcpv6RenewPrefix+site%3Adocs.microsoft.com) | dhcpcsvc6.dll | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.Dhcpv6RenewPrefix](https://github.com/dahall/Vanara/search?l=C%23&q=Dhcpv6RenewPrefix) +[Dhcpv6RequestParams](https://www.google.com/search?num=5&q=Dhcpv6RequestParams+site%3Adocs.microsoft.com) | dhcpcsvc6.dll | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.Dhcpv6RequestParams](https://github.com/dahall/Vanara/search?l=C%23&q=Dhcpv6RequestParams) +[Dhcpv6RequestPrefix](https://www.google.com/search?num=5&q=Dhcpv6RequestPrefix+site%3Adocs.microsoft.com) | dhcpcsvc6.dll | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.Dhcpv6RequestPrefix](https://github.com/dahall/Vanara/search?l=C%23&q=Dhcpv6RequestPrefix) +[McastApiCleanup](https://www.google.com/search?num=5&q=McastApiCleanup+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +[McastApiStartup](https://www.google.com/search?num=5&q=McastApiStartup+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +[McastEnumerateScopes](https://www.google.com/search?num=5&q=McastEnumerateScopes+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +[McastGenUID](https://www.google.com/search?num=5&q=McastGenUID+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +[McastReleaseAddress](https://www.google.com/search?num=5&q=McastReleaseAddress+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +[McastRenewAddress](https://www.google.com/search?num=5&q=McastRenewAddress+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +[McastRequestAddress](https://www.google.com/search?num=5&q=McastRequestAddress+site%3Adocs.microsoft.com) | dhcpcsvc.dll | | +### Structures +Native Structure | Header | Managed Structure +--- | --- | --- +[DHCP_IP_ADDRESS](https://www.google.com/search?num=5&q=DHCP_IP_ADDRESS+site%3Adocs.microsoft.com) | dhcpsapi.h | [Vanara.PInvoke.Dhcp.DHCP_IP_ADDRESS](https://github.com/dahall/Vanara/search?l=C%23&q=DHCP_IP_ADDRESS) +[DHCPAPI_PARAMS](https://www.google.com/search?num=5&q=DHCPAPI_PARAMS+site%3Adocs.microsoft.com) | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DHCPAPI_PARAMS](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPAPI_PARAMS) +[DHCPCAPI_CLASSID](https://www.google.com/search?num=5&q=DHCPCAPI_CLASSID+site%3Adocs.microsoft.com) | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DHCPCAPI_CLASSID](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPCAPI_CLASSID) +[DHCPCAPI_PARAMS_ARRAY](https://www.google.com/search?num=5&q=DHCPCAPI_PARAMS_ARRAY+site%3Adocs.microsoft.com) | dhcpcsdk.h | [Vanara.PInvoke.Dhcp.DHCPCAPI_PARAMS_ARRAY](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPCAPI_PARAMS_ARRAY) +[DHCPV6CAPI_CLASSID](https://www.google.com/search?num=5&q=DHCPV6CAPI_CLASSID+site%3Adocs.microsoft.com) | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.DHCPV6CAPI_CLASSID](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPV6CAPI_CLASSID) +[DHCPV6CAPI_PARAMS](https://www.google.com/search?num=5&q=DHCPV6CAPI_PARAMS+site%3Adocs.microsoft.com) | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.DHCPV6CAPI_PARAMS](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPV6CAPI_PARAMS) +[DHCPV6CAPI_PARAMS_ARRAY](https://www.google.com/search?num=5&q=DHCPV6CAPI_PARAMS_ARRAY+site%3Adocs.microsoft.com) | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.DHCPV6CAPI_PARAMS_ARRAY](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPV6CAPI_PARAMS_ARRAY) +[DHCPV6Prefix](https://www.google.com/search?num=5&q=DHCPV6Prefix+site%3Adocs.microsoft.com) | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.DHCPV6Prefix](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPV6Prefix) +[DHCPV6PrefixLeaseInformation](https://www.google.com/search?num=5&q=DHCPV6PrefixLeaseInformation+site%3Adocs.microsoft.com) | dhcpv6csdk.h | [Vanara.PInvoke.Dhcp.DHCPV6PrefixLeaseInformation](https://github.com/dahall/Vanara/search?l=C%23&q=DHCPV6PrefixLeaseInformation) diff --git a/PInvoke/Dhcp/DhcpCSdk.cs b/PInvoke/Dhcp/DhcpCSdk.cs new file mode 100644 index 000000000..7f24b58b7 --- /dev/null +++ b/PInvoke/Dhcp/DhcpCSdk.cs @@ -0,0 +1,584 @@ +using System; +using System.Runtime.InteropServices; + +namespace Vanara.PInvoke +{ + /// Items from Dhcpcsvc6.dll and Dhcpcsvc.dll. + public static partial class Dhcp + { + /// De-register handle that is an event + public const uint DHCPCAPI_DEREGISTER_HANDLE_EVENT = 0x01; + + /// Handle returned is to an event + public const uint DHCPCAPI_REGISTER_HANDLE_EVENT = 0x01; + + private const string Lib_Dhcp = "Dhcpcsvc.dll"; + + /// + /// DHCP options. See ISC DHCP 4.4 Manual Pages - + /// dhcp-options for some details. + /// + [PInvokeData("dhcpcsdk.h")] + public enum DHCP_OPTION_ID : uint + { +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + OPTION_PAD = 0, + OPTION_SUBNET_MASK = 1, + OPTION_TIME_OFFSET = 2, + OPTION_ROUTER_ADDRESS = 3, + OPTION_TIME_SERVERS = 4, + OPTION_IEN116_NAME_SERVERS = 5, + OPTION_DOMAIN_NAME_SERVERS = 6, + OPTION_LOG_SERVERS = 7, + OPTION_COOKIE_SERVERS = 8, + OPTION_LPR_SERVERS = 9, + OPTION_IMPRESS_SERVERS = 10, + OPTION_RLP_SERVERS = 11, + OPTION_HOST_NAME = 12, + OPTION_BOOT_FILE_SIZE = 13, + OPTION_MERIT_DUMP_FILE = 14, + OPTION_DOMAIN_NAME = 15, + OPTION_SWAP_SERVER = 16, + OPTION_ROOT_DISK = 17, + OPTION_EXTENSIONS_PATH = 18, + OPTION_BE_A_ROUTER = 19, + OPTION_NON_LOCAL_SOURCE_ROUTING = 20, + OPTION_POLICY_FILTER_FOR_NLSR = 21, + OPTION_MAX_REASSEMBLY_SIZE = 22, + OPTION_DEFAULT_TTL = 23, + OPTION_PMTU_AGING_TIMEOUT = 24, + OPTION_PMTU_PLATEAU_TABLE = 25, + OPTION_MTU = 26, + OPTION_ALL_SUBNETS_MTU = 27, + OPTION_BROADCAST_ADDRESS = 28, + OPTION_PERFORM_MASK_DISCOVERY = 29, + OPTION_BE_A_MASK_SUPPLIER = 30, + OPTION_PERFORM_ROUTER_DISCOVERY = 31, + OPTION_ROUTER_SOLICITATION_ADDR = 32, + OPTION_STATIC_ROUTES = 33, + OPTION_TRAILERS = 34, + OPTION_ARP_CACHE_TIMEOUT = 35, + OPTION_ETHERNET_ENCAPSULATION = 36, + OPTION_TTL = 37, + OPTION_KEEP_ALIVE_INTERVAL = 38, + OPTION_KEEP_ALIVE_DATA_SIZE = 39, + OPTION_NETWORK_INFO_SERVICE_DOM = 40, + OPTION_NETWORK_INFO_SERVERS = 41, + OPTION_NETWORK_TIME_SERVERS = 42, + OPTION_VENDOR_SPEC_INFO = 43, + OPTION_NETBIOS_NAME_SERVER = 44, + OPTION_NETBIOS_DATAGRAM_SERVER = 45, + OPTION_NETBIOS_NODE_TYPE = 46, + OPTION_NETBIOS_SCOPE_OPTION = 47, + OPTION_XWINDOW_FONT_SERVER = 48, + OPTION_XWINDOW_DISPLAY_MANAGER = 49, + OPTION_REQUESTED_ADDRESS = 50, + OPTION_LEASE_TIME = 51, + OPTION_OK_TO_OVERLAY = 52, + OPTION_MESSAGE_TYPE = 53, + OPTION_SERVER_IDENTIFIER = 54, + OPTION_PARAMETER_REQUEST_LIST = 55, + OPTION_MESSAGE = 56, + OPTION_MESSAGE_LENGTH = 57, + OPTION_RENEWAL_TIME = 58, + OPTION_REBIND_TIME = 59, + OPTION_CLIENT_CLASS_INFO = 60, + OPTION_CLIENT_ID = 61, + OPTION_TFTP_SERVER_NAME = 66, + OPTION_BOOTFILE_NAME = 67, + OPTION_MSFT_IE_PROXY = 252, + OPTION_END = 255, +#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member + } + + /// Flags that specify the data being requested. + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpRequestParams")] + [Flags] + public enum DHCPCAPI_REQUEST + { + /// The request is persisted but no options are fetched. + DHCPCAPI_REQUEST_PERSISTENT = 0x01, + + /// Options will be fetched from the server. + DHCPCAPI_REQUEST_SYNCHRONOUS = 0x02, + + /// Request and return, set event on completion. + DHCPCAPI_REQUEST_ASYNCHRONOUS = 0x04, + + /// Cancel request. + DHCPCAPI_REQUEST_CANCEL = 0x08, + } + + /// + /// The DhcpCApiCleanup function enables DHCP to properly clean up resources allocated throughout the use of DHCP function + /// calls. The DhcpCApiCleanup function must only be called if a previous call to DhcpCApiInitialize executed successfully. + /// + /// None + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcpcapicleanup void DhcpCApiCleanup(); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpCApiCleanup")] + public static extern void DhcpCApiCleanup(); + + /// + /// The DhcpCApiInitialize function must be the first function call made by users of DHCP; it prepares the system for all + /// other DHCP function calls. Other DHCP functions should only be called if the DhcpCApiInitialize function executes successfully. + /// + /// Pointer to the DHCP version implemented by the client. + /// Returns ERROR_SUCCESS upon successful completion. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcpcapiinitialize DWORD DhcpCApiInitialize( LPDWORD + // Version ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpCApiInitialize")] + public static extern Win32Error DhcpCApiInitialize(out uint Version); + + /// + /// The DhcpDeRegisterParamChange function releases resources associated with previously registered event notifications, and + /// closes the associated event handle. + /// + /// Reserved. Must be set to zero. + /// Reserved. Must be set to NULL. + /// + /// Must be the same value as the HANDLE variable in the DhcpRegisterParamChange function call for which the client is + /// deregistering event notification. + /// + /// Returns ERROR_SUCCESS upon successful completion. Otherwise, returns Windows error codes. + /// + /// The DhcpDeRegisterParamChange function must be made subsequent to an associated DhcpRegisterParamChange function call, + /// and the Flags parameter and the HANDLE variable passed in the Event parameter to DhcpDeRegisterParamChange must + /// match corresponding Flags parameter and the HANDLE variable of the previous and associated DhcpRegisterParamChange + /// function call. + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcpderegisterparamchange DWORD + // DhcpDeRegisterParamChange( DWORD Flags, LPVOID Reserved, LPVOID Event ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpDeRegisterParamChange")] + public static extern Win32Error DhcpDeRegisterParamChange([Optional] uint Flags, [Optional] IntPtr Reserved, HEVENT Event); + + /// The DhcpGetOriginalSubnetMask is used to get the subnet mask of any given adapter name. + /// [in] Contains the name of the local DHCP-enabled adapter for which the subnet mask is being retrieved. + /// [out] Pointer to the retrieved subnet mask. + /// + /// This function always returns 0. + /// Failure is indicated by a return value of 0 for the dwSubnetMask parameter. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if the sAdapterName parameter is invalid. + /// + /// + /// + // https://docs.microsoft.com/en-us/previous-versions/bb656318(v=vs.85) + // void APIENTRY DhcpGetOriginalSubnetMask( __in LPCWSTR sAdapterName, __out DWORD *dwSubnetMask ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("Dhcpcsdk.h")] + public static extern void DhcpGetOriginalSubnetMask([MarshalAs(UnmanagedType.LPWStr)] string sAdapterName, out DHCP_IP_ADDRESS dwSubnetMask); + + /// + /// The DhcpRegisterParamChange function enables clients to register for notification of changes in DHCP configuration parameters. + /// + /// + /// Reserved. Must be set to DHCPCAPI_REGISTER_HANDLE_EVENT. If it is not set to this flag value, the API call will not be successful. + /// + /// Reserved. Must be set to NULL. + /// GUID of the adapter for which event notification is being requested. Must be under 256 characters. + /// Reserved. Must be set to NULL. + /// + /// Parameters for which the client is interested in registering for notification, in the form of a DHCPCAPI_PARAMS_ARRAY structure. + /// + /// + /// Attributes of Handle are determined by the value of Flags. In version 2 of the DHCP API, Flags must be set to + /// DHCPCAPI_REGISTER_HANDLE_EVENT, and therefore, Handle must be a pointer to a HANDLE variable that will hold the handle to + /// a Windows event that gets signaled when parameters specified in Params change. Note that this HANDLE variable is used in + /// a subsequent call to the DhcpDeRegisterParamChange function to deregister event notifications associated with this + /// particular call to the DhcpRegisterParamChange function. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. Otherwise, returns Windows error codes. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if the AdapterName parameter is over 256 characters long. + /// + /// + /// + /// + /// Version 2 of the DHCP Client API provides only event-based notification. With event-based notification in DHCP, clients enable + /// notification by having Handle point to a variable that, upon successful return, holds the EVENT handles that are signaled + /// whenever changes occur to the parameters requested in Params. + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcpregisterparamchange DWORD DhcpRegisterParamChange( + // DWORD Flags, LPVOID Reserved, LPWSTR AdapterName, LPDHCPCAPI_CLASSID ClassId, DHCPCAPI_PARAMS_ARRAY Params, LPVOID Handle ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpRegisterParamChange")] + public static extern Win32Error DhcpRegisterParamChange(uint Flags, [Optional] IntPtr Reserved, [MarshalAs(UnmanagedType.LPWStr)] string AdapterName, + [Optional] IntPtr ClassId, DHCPCAPI_PARAMS_ARRAY Params, out HEVENT Handle); + + /// The DhcpRemoveDNSRegistrations function removes all DHCP-initiated DNS registrations for the client. + /// Returns ERROR_SUCCESS upon successful completion. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcpremovednsregistrations DWORD DhcpRemoveDNSRegistrations(); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpRemoveDNSRegistrations")] + public static extern Win32Error DhcpRemoveDNSRegistrations(); + + /// + /// The DhcpRequestParams function enables callers to synchronously, or synchronously and persistently obtain DHCP data from + /// a DHCP server. + /// + /// + /// + /// Flags that specify the data being requested. This parameter is optional. The following possible values are supported and are not + /// mutually exclusive: + /// + /// + /// + /// Value + /// Meaning + /// + /// + /// DHCPCAPI_REQUEST_PERSISTENT + /// The request is persisted but no options are fetched. + /// + /// + /// DHCPCAPI_REQUEST_SYNCHRONOUS + /// Options will be fetched from the server. + /// + /// + /// + /// Reserved for future use. Must be set to NULL. + /// GUID of the adapter on which requested data is being made. Must be under 256 characters. + /// + /// Class identifier (ID) that should be used if DHCP INFORM messages are being transmitted onto the network. This parameter is optional. + /// + /// + /// Optional data to be requested, in addition to the data requested in the RecdParams array. The SendParams parameter cannot + /// contain any of the standard options that the DHCP client sends by default. + /// + /// + /// Array of DHCP data the caller is interested in receiving. This array must be empty prior to the DhcpRequestParams + /// function call. + /// + /// Buffer used for storing the data associated with requests made in RecdParams. + /// + /// Size of Buffer. + /// + /// Required size of the buffer, if it is insufficiently sized to hold the data, otherwise indicates size of the buffer which was + /// successfully filled. + /// + /// + /// + /// Application identifier (ID) used to facilitate a persistent request. Must be a printable string with no special characters + /// (commas, backslashes, colons, or other illegal characters may not be used). The specified application identifier (ID) is used in + /// a subsequent DhcpUndoRequestParams function call to clear the persistent request, as necessary. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// Upon return, RecdParams is filled with pointers to requested data, with corresponding data placed in Buffer. If pSize indicates + /// that Buffer has insufficient space to store returned data, the DhcpRequestParams function returns ERROR_MORE_DATA, and + /// returns the required buffer size in pSize. Note that the required size of Buffer may increase during the time that elapses + /// between the initial function call's return and a subsequent call; therefore, the required size of Buffer (indicated in pSize) + /// provides an indication of the approximate size required of Buffer, rather than guaranteeing that subsequent calls will return + /// successfully if Buffer is set to the size indicated in pSize. + /// + /// Other errors return appropriate Windows error codes. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if the AdapterName parameter is over 256 characters long. + /// + /// + /// ERROR_BUFFER_OVERFLOW + /// Returned if the AdapterName parameter is over 256 characters long. + /// + /// + /// + /// + /// + /// DHCP clients store data obtained from a DHCP server in their local cache. If the DHCP client cache contains all data requested + /// in the RecdParams array of a DhcpRequestParams function call, the client returns data from its cache. If requested data + /// is not available in the client cache, the client processes the DhcpRequestParams function call by submitting a + /// DHCP-INFORM message to the DHCP server. + /// + /// + /// When the client submits a DHCP-INFORM message to the DHCP server, it includes any requests provided in the optional SendParams + /// parameter, and provides the Class identifier (ID) specified in the ClassId parameter, if provided. + /// + /// + /// Clients can also specify that DHCP data be retrieved from the DHCP server each time the DHCP client boots, which is considered a + /// persistent request. To enable persistent requests, the caller must specify the RequestIdStr parameter, and also specify the + /// additional DHCPAPI_REQUEST_PERSISTENT flag in the dwFlags parameter. This persistent request capability is especially + /// useful when clients need to automatically request application-critical information at each boot. To disable a persist request, + /// clients must call the function. + /// + /// + /// Note The callers of this API must not make blocking calls to this API, since it can take up to a maximum of 2 minutes to + /// return a code or status. UI behaviors in particular should not block on the return of this call, since it can introduce a + /// significant delay in UI response time. + /// + /// For more information about DHCP INFORM messages, and other standards-based information about DHCP, consult DHCP Standards. + /// To see the DhcpRequestParams function in use, see DHCP Examples. + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcprequestparams DWORD DhcpRequestParams( DWORD Flags, + // LPVOID Reserved, LPWSTR AdapterName, LPDHCPCAPI_CLASSID ClassId, DHCPCAPI_PARAMS_ARRAY SendParams, DHCPCAPI_PARAMS_ARRAY + // RecdParams, LPBYTE Buffer, LPDWORD pSize, LPWSTR RequestIdStr ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpRequestParams")] + public static extern Win32Error DhcpRequestParams([Optional] DHCPCAPI_REQUEST Flags, [In, Optional] IntPtr Reserved, [MarshalAs(UnmanagedType.LPWStr)] string AdapterName, + in DHCPCAPI_CLASSID ClassId, DHCPCAPI_PARAMS_ARRAY SendParams, DHCPCAPI_PARAMS_ARRAY RecdParams, [Out] IntPtr Buffer, ref uint pSize, [Optional, MarshalAs(UnmanagedType.LPWStr)] string RequestIdStr); + + /// + /// The DhcpRequestParams function enables callers to synchronously, or synchronously and persistently obtain DHCP data from + /// a DHCP server. + /// + /// + /// + /// Flags that specify the data being requested. This parameter is optional. The following possible values are supported and are not + /// mutually exclusive: + /// + /// + /// + /// Value + /// Meaning + /// + /// + /// DHCPCAPI_REQUEST_PERSISTENT + /// The request is persisted but no options are fetched. + /// + /// + /// DHCPCAPI_REQUEST_SYNCHRONOUS + /// Options will be fetched from the server. + /// + /// + /// + /// Reserved for future use. Must be set to NULL. + /// GUID of the adapter on which requested data is being made. Must be under 256 characters. + /// + /// Class identifier (ID) that should be used if DHCP INFORM messages are being transmitted onto the network. This parameter is optional. + /// + /// + /// Optional data to be requested, in addition to the data requested in the RecdParams array. The SendParams parameter cannot + /// contain any of the standard options that the DHCP client sends by default. + /// + /// + /// Array of DHCP data the caller is interested in receiving. This array must be empty prior to the DhcpRequestParams + /// function call. + /// + /// Buffer used for storing the data associated with requests made in RecdParams. + /// + /// Size of Buffer. + /// + /// Required size of the buffer, if it is insufficiently sized to hold the data, otherwise indicates size of the buffer which was + /// successfully filled. + /// + /// + /// + /// Application identifier (ID) used to facilitate a persistent request. Must be a printable string with no special characters + /// (commas, backslashes, colons, or other illegal characters may not be used). The specified application identifier (ID) is used in + /// a subsequent DhcpUndoRequestParams function call to clear the persistent request, as necessary. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// Upon return, RecdParams is filled with pointers to requested data, with corresponding data placed in Buffer. If pSize indicates + /// that Buffer has insufficient space to store returned data, the DhcpRequestParams function returns ERROR_MORE_DATA, and + /// returns the required buffer size in pSize. Note that the required size of Buffer may increase during the time that elapses + /// between the initial function call's return and a subsequent call; therefore, the required size of Buffer (indicated in pSize) + /// provides an indication of the approximate size required of Buffer, rather than guaranteeing that subsequent calls will return + /// successfully if Buffer is set to the size indicated in pSize. + /// + /// Other errors return appropriate Windows error codes. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if the AdapterName parameter is over 256 characters long. + /// + /// + /// ERROR_BUFFER_OVERFLOW + /// Returned if the AdapterName parameter is over 256 characters long. + /// + /// + /// + /// + /// + /// DHCP clients store data obtained from a DHCP server in their local cache. If the DHCP client cache contains all data requested + /// in the RecdParams array of a DhcpRequestParams function call, the client returns data from its cache. If requested data + /// is not available in the client cache, the client processes the DhcpRequestParams function call by submitting a + /// DHCP-INFORM message to the DHCP server. + /// + /// + /// When the client submits a DHCP-INFORM message to the DHCP server, it includes any requests provided in the optional SendParams + /// parameter, and provides the Class identifier (ID) specified in the ClassId parameter, if provided. + /// + /// + /// Clients can also specify that DHCP data be retrieved from the DHCP server each time the DHCP client boots, which is considered a + /// persistent request. To enable persistent requests, the caller must specify the RequestIdStr parameter, and also specify the + /// additional DHCPAPI_REQUEST_PERSISTENT flag in the dwFlags parameter. This persistent request capability is especially + /// useful when clients need to automatically request application-critical information at each boot. To disable a persist request, + /// clients must call the function. + /// + /// + /// Note The callers of this API must not make blocking calls to this API, since it can take up to a maximum of 2 minutes to + /// return a code or status. UI behaviors in particular should not block on the return of this call, since it can introduce a + /// significant delay in UI response time. + /// + /// For more information about DHCP INFORM messages, and other standards-based information about DHCP, consult DHCP Standards. + /// To see the DhcpRequestParams function in use, see DHCP Examples. + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcprequestparams DWORD DhcpRequestParams( DWORD Flags, + // LPVOID Reserved, LPWSTR AdapterName, LPDHCPCAPI_CLASSID ClassId, DHCPCAPI_PARAMS_ARRAY SendParams, DHCPCAPI_PARAMS_ARRAY + // RecdParams, LPBYTE Buffer, LPDWORD pSize, LPWSTR RequestIdStr ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpRequestParams")] + public static extern Win32Error DhcpRequestParams([Optional] DHCPCAPI_REQUEST Flags, [In, Optional] IntPtr Reserved, [MarshalAs(UnmanagedType.LPWStr)] string AdapterName, + [In, Optional] IntPtr ClassId, DHCPCAPI_PARAMS_ARRAY SendParams, DHCPCAPI_PARAMS_ARRAY RecdParams, [Out] IntPtr Buffer, ref uint pSize, [Optional, MarshalAs(UnmanagedType.LPWStr)] string RequestIdStr); + + /// + /// The DhcpUndoRequestParams function removes persistent requests previously made with a DhcpRequestParams function call. + /// + /// Reserved. Must be zero. + /// Reserved for future use. Must be set to NULL. + /// + /// GUID of the adapter for which information is no longer required. Must be under 256 characters. + /// Note This parameter is no longer used. + /// + /// + /// Application identifier (ID) originally used to make a persistent request. This string must match the RequestIdStr parameter used + /// in the DhcpRequestParams function call that obtained the corresponding persistent request. Note that this must match the + /// previous application identifier (ID) used, and must be a printable string with no special characters (commas, backslashes, + /// colons, or other illegal characters may not be used). + /// + /// Returns ERROR_SUCCESS upon successful completion. Otherwise, returns a Windows error code. + /// + /// Persistent requests are typically made by the setup or installer process associated with the application. When appropriate, the + /// setup or installer process would likely make the DhcpUndoRequestParams function call to cancel its associated persistent request. + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcpundorequestparams DWORD DhcpUndoRequestParams( DWORD + // Flags, LPVOID Reserved, LPWSTR AdapterName, LPWSTR RequestIdStr ); + [DllImport(Lib_Dhcp, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NF:dhcpcsdk.DhcpUndoRequestParams")] + public static extern Win32Error DhcpUndoRequestParams([Optional] uint Flags, [In, Optional] IntPtr Reserved, [MarshalAs(UnmanagedType.LPWStr)] string AdapterName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string RequestIdStr); + + /// Represents an 8-byte IP v4 address. + [PInvokeData("dhcpsapi.h")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCP_IP_ADDRESS + { + /// The underlying value. + public uint value; + + /// Initializes a new instance of the struct from a UInt32/DWORD. + /// The value. + public DHCP_IP_ADDRESS(uint val = 0) => value = val; + + /// Initializes a new instance of the struct from an . + /// The value. + /// + public DHCP_IP_ADDRESS(System.Net.IPAddress ipaddr) + { + if (ipaddr is null) value = 0; + if (ipaddr.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) + throw new InvalidCastException(); +#pragma warning disable CS0618 // Type or member is obsolete + value = (uint)ipaddr.Address; +#pragma warning restore CS0618 // Type or member is obsolete + } + + /// Performs an explicit conversion from to . + /// The DWORD based address. + /// The resulting instance from the conversion. + public static explicit operator System.Net.IPAddress(DHCP_IP_ADDRESS addr) => + new System.Net.IPAddress(BitConverter.GetBytes(addr.value)); + + /// Performs an implicit conversion from to . + /// The value. + /// The resulting instance from the conversion. + public static implicit operator DHCP_IP_ADDRESS(System.Net.IPAddress addr) => new DHCP_IP_ADDRESS(addr); + + /// Performs an implicit conversion from to . + /// The address as four bytes. + /// The resulting instance from the conversion. + public static implicit operator DHCP_IP_ADDRESS(uint addr) => new DHCP_IP_ADDRESS(addr); + + /// Converts this address to standard notation. + /// A that represents this instance. + public override string ToString() => ((System.Net.IPAddress)this).ToString(); + + /// Returns a hash code for this instance. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() => value.GetHashCode(); + } + + /// The DHCPAPI_PARAMS structure is used to request DHCP parameters. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/ns-dhcpcsdk-dhcpapi_params typedef struct _DHCPAPI_PARAMS { ULONG + // Flags; ULONG OptionId; BOOL IsVendor; #if ... LPBYTE Data; #else LPBYTE Data; #endif DWORD nBytesData; } DHCPAPI_PARAMS, + // *PDHCPAPI_PARAMS, *LPDHCPAPI_PARAMS, DHCPCAPI_PARAMS, *PDHCPCAPI_PARAMS, *LPDHCPCAPI_PARAMS; + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NS:dhcpcsdk._DHCPAPI_PARAMS")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPAPI_PARAMS + { + /// Reserved. Must be set to zero. + public uint Flags; + + /// Identifier for the DHCP parameter being requested. + public DHCP_OPTION_ID OptionId; + + /// Specifies whether the DHCP parameter is vendor-specific. Set to TRUE if the parameter is vendor-specific. + [MarshalAs(UnmanagedType.Bool)] + public bool IsVendor; + + /// Pointer to the parameter data. + public IntPtr Data; + + /// Size of the data pointed to by Data, in bytes. + public uint nBytesData; + } + + /// The DHCPCAPI_CLASSID structure defines a client Class ID. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/ns-dhcpcsdk-dhcpcapi_classid typedef struct _DHCPCAPI_CLASSID { ULONG + // Flags; #if ... LPBYTE Data; #else LPBYTE Data; #endif ULONG nBytesData; } DHCPCAPI_CLASSID, *PDHCPCAPI_CLASSID, *LPDHCPCAPI_CLASSID; + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NS:dhcpcsdk._DHCPCAPI_CLASSID")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPCAPI_CLASSID + { + /// Reserved. Must be set to zero. + public uint Flags; + + /// Class ID binary data. + public IntPtr Data; + + /// Size of Data, in bytes. + public uint nBytesData; + } + + /// The DHCPCAPI_PARAMS_ARRAY structure stores an array of DHCPAPI_PARAMS structures used to query DHCP parameters. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpcsdk/ns-dhcpcsdk-dhcpcapi_params_array typedef struct + // _DHCPCAPI_PARAMS_ARARAY { ULONG nParams; #if ... LPDHCPCAPI_PARAMS Params; #else LPDHCPCAPI_PARAMS Params; #endif } + // DHCPCAPI_PARAMS_ARRAY, *PDHCPCAPI_PARAMS_ARRAY, *LPDHCPCAPI_PARAMS_ARRAY; + [PInvokeData("dhcpcsdk.h", MSDNShortId = "NS:dhcpcsdk._DHCPCAPI_PARAMS_ARARAY")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPCAPI_PARAMS_ARRAY + { + /// Number of elements in the Params array. + public uint nParams; + + /// Array of DHCPAPI_PARAMS structures. + public IntPtr Params; + } + } +} \ No newline at end of file diff --git a/PInvoke/Dhcp/DhcpV6CSdk.cs b/PInvoke/Dhcp/DhcpV6CSdk.cs new file mode 100644 index 000000000..aafa8a38a --- /dev/null +++ b/PInvoke/Dhcp/DhcpV6CSdk.cs @@ -0,0 +1,727 @@ +using System; +using System.Runtime.InteropServices; + +namespace Vanara.PInvoke +{ + /// Items from Dhcpcsvc6.dll and Dhcpcsvc.dll. + public static partial class Dhcp + { + private const string Lib_Dhcpcsvc6 = "dhcpcsvc6.dll"; + + /// DHCP V6 options. + [PInvokeData("dhcpcsdk.h")] + public enum DHCPV6_OPTION_ID : uint + { +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + DHCPV6_OPTION_CLIENTID = 1, + DHCPV6_OPTION_SERVERID = 2, + DHCPV6_OPTION_IA_NA = 3, + DHCPV6_OPTION_IA_TA = 4, + DHCPV6_OPTION_ORO = 6, + DHCPV6_OPTION_PREFERENCE = 7, + DHCPV6_OPTION_UNICAST = 12, + DHCPV6_OPTION_RAPID_COMMIT = 14, + DHCPV6_OPTION_USER_CLASS = 15, + DHCPV6_OPTION_VENDOR_CLASS = 16, + DHCPV6_OPTION_VENDOR_OPTS = 17, + DHCPV6_OPTION_RECONF_MSG = 19, + DHCPV6_OPTION_SIP_SERVERS_NAMES = 21, + DHCPV6_OPTION_SIP_SERVERS_ADDRS = 22, + DHCPV6_OPTION_DNS_SERVERS = 23, + DHCPV6_OPTION_DOMAIN_LIST = 24, + DHCPV6_OPTION_IA_PD = 25, + DHCPV6_OPTION_NIS_SERVERS = 27, + DHCPV6_OPTION_NISP_SERVERS = 28, + DHCPV6_OPTION_NIS_DOMAIN_NAME = 29, + DHCPV6_OPTION_NISP_DOMAIN_NAME = 30, +#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member + } + + /// The StatusCode enum contains status codes for IPv6 operations. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/ne-dhcpv6csdk-statuscode typedef enum { STATUS_NO_ERROR, + // STATUS_UNSPECIFIED_FAILURE, STATUS_NO_BINDING, STATUS_NOPREFIX_AVAIL } StatusCode; + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NE:dhcpv6csdk.__unnamed_enum_0")] + public enum StatusCode + { + /// + STATUS_NO_ERROR, + + /// + STATUS_UNSPECIFIED_FAILURE, + + /// + STATUS_NO_BINDING, + + /// + STATUS_NOPREFIX_AVAIL, + } + + /// + /// The Dhcpv6CApiCleanup function enables DHCPv6 to properly clean up resources allocated throughout the use of DHCPv6 + /// function calls. The Dhcpv6CApiCleanup function must only be called if a previous call to Dhcpv6CApiInitialize executed successfully. + /// + /// None + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6capicleanup void Dhcpv6CApiCleanup(); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6CApiCleanup")] + public static extern void Dhcpv6CApiCleanup(); + + /// + /// The Dhcpv6CApiInitialize function must be the first function call made by users of DHCPv6. The function prepares the + /// system for all other DHCPv6 function calls. Other DHCPv6 functions should only be called if the Dhcpv6CApiInitialize + /// function executes successfully. + /// + /// + /// Pointer to the DHCPv6 version implemented by the client. If a valid pointer is passed, the DHCPv6 client will be returned + /// through it. + /// + /// Returns ERROR_SUCCESS upon successful completion. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6capiinitialize void Dhcpv6CApiInitialize( + // LPDWORD Version ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6CApiInitialize")] + public static extern void Dhcpv6CApiInitialize(out uint Version); + + /// The Dhcpv6ReleasePrefix function releases a prefix previously acquired with the Dhcpv6RequestPrefix function. + /// Name of the adapter on which the PD request must be sent. + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to use to send on the wire. + /// + /// Note DHCPv6 Option Code 15 (0x000F) is not supported by this API. Typically, the User Class option is used by a client to + /// identify the type or category of user or application it represents. A server selects the configuration information for the + /// client based on the classes identified in this option. + /// + /// + /// Pointer to a DHCPV6CAPIPrefixLeaseInformation structure that is used to release the prefix. + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + /// + /// + /// Release messages sent as the result of the call to this function must contain the following values for the T1 and + /// T2 fields of the DHCPV6CAPIPrefixLeaseInformation structure supplied in the prefixleaseInfo parameter: + /// + /// + /// + /// T1: the renewal time for the prefix, in seconds specified as absolute time values. + /// + /// + /// T2: the rebind time of the prefix, in seconds specified as absolute time values. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6releaseprefix DWORD Dhcpv6ReleasePrefix( LPWSTR + // adapterName, LPDHCPV6CAPI_CLASSID classId, LPDHCPV6PrefixLeaseInformation leaseInfo ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6ReleasePrefix")] + public static extern Win32Error Dhcpv6ReleasePrefix([MarshalAs(UnmanagedType.LPWStr)] string adapterName, in DHCPV6CAPI_CLASSID classId, + in DHCPV6PrefixLeaseInformation leaseInfo); + + /// The Dhcpv6ReleasePrefix function releases a prefix previously acquired with the Dhcpv6RequestPrefix function. + /// Name of the adapter on which the PD request must be sent. + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to use to send on the wire. + /// + /// Note DHCPv6 Option Code 15 (0x000F) is not supported by this API. Typically, the User Class option is used by a client to + /// identify the type or category of user or application it represents. A server selects the configuration information for the + /// client based on the classes identified in this option. + /// + /// + /// Pointer to a DHCPV6CAPIPrefixLeaseInformation structure that is used to release the prefix. + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + /// + /// + /// Release messages sent as the result of the call to this function must contain the following values for the T1 and + /// T2 fields of the DHCPV6CAPIPrefixLeaseInformation structure supplied in the prefixleaseInfo parameter: + /// + /// + /// + /// T1: the renewal time for the prefix, in seconds specified as absolute time values. + /// + /// + /// T2: the rebind time of the prefix, in seconds specified as absolute time values. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6releaseprefix DWORD Dhcpv6ReleasePrefix( LPWSTR + // adapterName, LPDHCPV6CAPI_CLASSID classId, LPDHCPV6PrefixLeaseInformation leaseInfo ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6ReleasePrefix")] + public static extern Win32Error Dhcpv6ReleasePrefix([MarshalAs(UnmanagedType.LPWStr)] string adapterName, [In, Optional] IntPtr classId, + in DHCPV6PrefixLeaseInformation leaseInfo); + + /// The Dhcpv6RenewPrefix function renews a prefix previously acquired with the Dhcpv6RequestPrefix function. + /// GUID of the adapter on which the prefix renewal must be sent. + /// + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to send on the wire. This parameter is + /// can be NULL. + /// + /// + /// Note DHCPv6 Option Code 15 (0x000F) is not supported by this API. Typically, the User Class option is used by a client to + /// identify the type or category of user or application it represents. A server selects the configuration information for the + /// client based on the classes identified in this option. + /// + /// + /// Pointer to a DHCPV6PrefixLeaseInformation structure that contains the prefix lease information. + /// + /// Contains the number of seconds a requesting application needs to wait before calling the Dhcpv6RenewPrefix function to + /// renew its acquired prefixes. A value of 0xFFFFFFFF indicates that the application does not need to renew its lease. + /// + /// + /// Specifies to the DHCPv6 client whether or not to send a REBIND in order to validate the prefix bindings. TRUE indicates + /// that a REBIND is required. FALSE indicates RENEW is required. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_MORE_DATA + /// Returned if the API responds with more prefixes than there is memory allocated. + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6renewprefix DWORD Dhcpv6RenewPrefix( LPWSTR + // adapterName, LPDHCPV6CAPI_CLASSID pclassId, LPDHCPV6PrefixLeaseInformation prefixleaseInfo, DWORD *pdwTimeToWait, DWORD + // bValidatePrefix ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6RenewPrefix")] + public static extern Win32Error Dhcpv6RenewPrefix([MarshalAs(UnmanagedType.LPWStr)] string adapterName, in DHCPV6CAPI_CLASSID pclassId, + ref DHCPV6PrefixLeaseInformation prefixleaseInfo, out uint pdwTimeToWait, [MarshalAs(UnmanagedType.Bool)] bool bValidatePrefix); + + /// The Dhcpv6RenewPrefix function renews a prefix previously acquired with the Dhcpv6RequestPrefix function. + /// GUID of the adapter on which the prefix renewal must be sent. + /// + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to send on the wire. This parameter is + /// can be NULL. + /// + /// + /// Note DHCPv6 Option Code 15 (0x000F) is not supported by this API. Typically, the User Class option is used by a client to + /// identify the type or category of user or application it represents. A server selects the configuration information for the + /// client based on the classes identified in this option. + /// + /// + /// Pointer to a DHCPV6PrefixLeaseInformation structure that contains the prefix lease information. + /// + /// Contains the number of seconds a requesting application needs to wait before calling the Dhcpv6RenewPrefix function to + /// renew its acquired prefixes. A value of 0xFFFFFFFF indicates that the application does not need to renew its lease. + /// + /// + /// Specifies to the DHCPv6 client whether or not to send a REBIND in order to validate the prefix bindings. TRUE indicates + /// that a REBIND is required. FALSE indicates RENEW is required. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_MORE_DATA + /// Returned if the API responds with more prefixes than there is memory allocated. + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6renewprefix DWORD Dhcpv6RenewPrefix( LPWSTR + // adapterName, LPDHCPV6CAPI_CLASSID pclassId, LPDHCPV6PrefixLeaseInformation prefixleaseInfo, DWORD *pdwTimeToWait, DWORD + // bValidatePrefix ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6RenewPrefix")] + public static extern Win32Error Dhcpv6RenewPrefix([MarshalAs(UnmanagedType.LPWStr)] string adapterName, [In, Optional] IntPtr pclassId, + ref DHCPV6PrefixLeaseInformation prefixleaseInfo, out uint pdwTimeToWait, [MarshalAs(UnmanagedType.Bool)] bool bValidatePrefix); + + /// The Dhcpv6RequestParams function requests options from the DHCPv6 client cache or directly from the DHCPv6 server. + /// + /// If this value is set to TRUE, any available cached information will be ignored and new information will be requested. + /// Otherwise, the request is only sent if there is no cached information. + /// + /// Reserved for future use. Must be set to NULL. + /// GUID of the adapter for which this request is meant. This parameter must not be NULL. + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to use to send on the wire. This + /// parameter is optional. + /// + /// A DHCPV6CAPI_PARAMS_ARRAY structure that contains the parameters to be received from the DHCPV6 server. + /// A buffer to contain information returned by some pointers in recdParams. + /// + /// Size of the buffer. When the function returns ERROR_MORE_DATA, this parameter will contain the size, in bytes, required to + /// complete the operation. If the function is successful, this parameter contains the number of bytes used. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_MORE_DATA + /// + /// The call to this API was made with insufficient memory allocated for the Buffer parameter, while pSize contains the actual + /// memory size required. + /// + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6requestparams DWORD Dhcpv6RequestParams( BOOL + // forceNewInform, LPVOID reserved, LPWSTR adapterName, LPDHCPV6CAPI_CLASSID classId, DHCPV6CAPI_PARAMS_ARRAY recdParams, LPBYTE + // buffer, LPDWORD pSize ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6RequestParams")] + public static extern Win32Error Dhcpv6RequestParams([MarshalAs(UnmanagedType.Bool)] bool forceNewInform, [In, Optional] IntPtr reserved, + [MarshalAs(UnmanagedType.LPWStr)] string adapterName, in DHCPV6CAPI_CLASSID classId, [In, Out] DHCPV6CAPI_PARAMS_ARRAY recdParams, + [In] IntPtr buffer, ref uint pSize); + + /// The Dhcpv6RequestParams function requests options from the DHCPv6 client cache or directly from the DHCPv6 server. + /// + /// If this value is set to TRUE, any available cached information will be ignored and new information will be requested. + /// Otherwise, the request is only sent if there is no cached information. + /// + /// Reserved for future use. Must be set to NULL. + /// GUID of the adapter for which this request is meant. This parameter must not be NULL. + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to use to send on the wire. This + /// parameter is optional. + /// + /// A DHCPV6CAPI_PARAMS_ARRAY structure that contains the parameters to be received from the DHCPV6 server. + /// A buffer to contain information returned by some pointers in recdParams. + /// + /// Size of the buffer. When the function returns ERROR_MORE_DATA, this parameter will contain the size, in bytes, required to + /// complete the operation. If the function is successful, this parameter contains the number of bytes used. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_MORE_DATA + /// + /// The call to this API was made with insufficient memory allocated for the Buffer parameter, while pSize contains the actual + /// memory size required. + /// + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6requestparams DWORD Dhcpv6RequestParams( BOOL + // forceNewInform, LPVOID reserved, LPWSTR adapterName, LPDHCPV6CAPI_CLASSID classId, DHCPV6CAPI_PARAMS_ARRAY recdParams, LPBYTE + // buffer, LPDWORD pSize ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6RequestParams")] + public static extern Win32Error Dhcpv6RequestParams([MarshalAs(UnmanagedType.Bool)] bool forceNewInform, [In, Optional] IntPtr reserved, + [MarshalAs(UnmanagedType.LPWStr)] string adapterName, [In, Optional] IntPtr classId, [In, Out] DHCPV6CAPI_PARAMS_ARRAY recdParams, + [In] IntPtr buffer, ref uint pSize); + + /// The Dhcpv6RequestPrefix function requests a specific prefix. + /// GUID of the adapter on which the prefix request must be sent. + /// + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to send on the wire. This parameter is optional. + /// + /// + /// Note DHCPv6 Option Code 15 (0x000F) is not supported by this API. Typically, the User Class option is used by a client to + /// identify the type or category of user or application it represents. A server selects the configuration information for the + /// client based on the classes identified in this option. + /// + /// + /// + /// Pointer to a DHCPV6PrefixLeaseInformation structure that contains the prefix lease information. + /// The following members of the DHCPV6PrefixLeaseInformation structure must follow these guidelines. + /// + /// + /// DHCPV6PrefixLeaseInformation member + /// Consideration + /// + /// + /// nPrefixes + /// + /// Must contain a maximum value of 10. The caller should have the memory allocated in the prefixArray member based on the number of + /// prefixes specified. + /// + /// + /// + /// iaid + /// + /// A unique positive number assigned to this member. This same value should be reused if this function is called again.This + /// mandatory value must be set by the calling application. + /// + /// + /// + /// ServerIdLen + /// + /// Must contain a maximum value of 128. The caller must have the memory allocated in the ServerId member based on the specified + /// ServerIdLen value. + /// + /// + /// + /// + /// The caller must follow these considerations when assigning the values of the nPrefixes, iaid, and + /// ServerIdLen members of the DHCPV6PrefixLeaseInformation structure. Based on these values, memory must also be properly + /// allocated to the ServerId and PrefixArray members before the Dhcpv6RequestPrefix function is called. + /// + /// + /// + /// Contains the number of seconds a requesting application needs to wait before calling the Dhcpv6RenewPrefix function to renew its + /// acquired prefixes. A value of 0xFFFFFFFF indicates that the application does not need to renew its lease. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_MORE_DATA + /// + /// The value of the nPrefixes or the ServerIdLen member specified is less than the number of prefixes available from the server or + /// the available server ID length. Increase the nPrefixes or the ServerIdLen member and make sure the corresponding memory has been + /// allocated properly before calling the Dhcpv6RequestPrefix function again. + /// + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6requestprefix DWORD Dhcpv6RequestPrefix( LPWSTR + // adapterName, LPDHCPV6CAPI_CLASSID pclassId, LPDHCPV6PrefixLeaseInformation prefixleaseInfo, DWORD *pdwTimeToWait ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6RequestPrefix")] + public static extern Win32Error Dhcpv6RequestPrefix([MarshalAs(UnmanagedType.LPWStr)] string adapterName, in DHCPV6CAPI_CLASSID pclassId, + ref DHCPV6PrefixLeaseInformation prefixleaseInfo, out uint pdwTimeToWait); + + /// The Dhcpv6RequestPrefix function requests a specific prefix. + /// GUID of the adapter on which the prefix request must be sent. + /// + /// + /// Pointer to a DHCPV6CAPI_CLASSID structure that contains the binary ClassId information to send on the wire. This parameter is optional. + /// + /// + /// Note DHCPv6 Option Code 15 (0x000F) is not supported by this API. Typically, the User Class option is used by a client to + /// identify the type or category of user or application it represents. A server selects the configuration information for the + /// client based on the classes identified in this option. + /// + /// + /// + /// Pointer to a DHCPV6PrefixLeaseInformation structure that contains the prefix lease information. + /// The following members of the DHCPV6PrefixLeaseInformation structure must follow these guidelines. + /// + /// + /// DHCPV6PrefixLeaseInformation member + /// Consideration + /// + /// + /// nPrefixes + /// + /// Must contain a maximum value of 10. The caller should have the memory allocated in the prefixArray member based on the number of + /// prefixes specified. + /// + /// + /// + /// iaid + /// + /// A unique positive number assigned to this member. This same value should be reused if this function is called again.This + /// mandatory value must be set by the calling application. + /// + /// + /// + /// ServerIdLen + /// + /// Must contain a maximum value of 128. The caller must have the memory allocated in the ServerId member based on the specified + /// ServerIdLen value. + /// + /// + /// + /// + /// The caller must follow these considerations when assigning the values of the nPrefixes, iaid, and + /// ServerIdLen members of the DHCPV6PrefixLeaseInformation structure. Based on these values, memory must also be properly + /// allocated to the ServerId and PrefixArray members before the Dhcpv6RequestPrefix function is called. + /// + /// + /// + /// Contains the number of seconds a requesting application needs to wait before calling the Dhcpv6RenewPrefix function to renew its + /// acquired prefixes. A value of 0xFFFFFFFF indicates that the application does not need to renew its lease. + /// + /// + /// Returns ERROR_SUCCESS upon successful completion. + /// + /// + /// Return code + /// Description + /// + /// + /// ERROR_MORE_DATA + /// + /// The value of the nPrefixes or the ServerIdLen member specified is less than the number of prefixes available from the server or + /// the available server ID length. Increase the nPrefixes or the ServerIdLen member and make sure the corresponding memory has been + /// allocated properly before calling the Dhcpv6RequestPrefix function again. + /// + /// + /// + /// ERROR_INVALID_PARAMETER + /// Returned if one of the following conditions are true: + /// + /// + /// ERROR_INVALID_NAME + /// The AdapterName is not in the correct format. It should be in this format: {00000000-0000-0000-0000-000000000000}. + /// + /// + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/nf-dhcpv6csdk-dhcpv6requestprefix DWORD Dhcpv6RequestPrefix( LPWSTR + // adapterName, LPDHCPV6CAPI_CLASSID pclassId, LPDHCPV6PrefixLeaseInformation prefixleaseInfo, DWORD *pdwTimeToWait ); + [DllImport(Lib_Dhcpcsvc6, SetLastError = false, ExactSpelling = true)] + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NF:dhcpv6csdk.Dhcpv6RequestPrefix")] + public static extern Win32Error Dhcpv6RequestPrefix([MarshalAs(UnmanagedType.LPWStr)] string adapterName, [In, Optional] IntPtr pclassId, + ref DHCPV6PrefixLeaseInformation prefixleaseInfo, out uint pdwTimeToWait); + + /// The DHCPV6CAPI_CLASSID structure defines an IPv6 client class ID. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/ns-dhcpv6csdk-dhcpv6capi_classid typedef struct _DHCPV6CAPI_CLASSID + // { ULONG Flags; #if ... LPBYTE Data; #else LPBYTE Data; #endif ULONG nBytesData; } DHCPV6CAPI_CLASSID, *PDHCPV6CAPI_CLASSID, *LPDHCPV6CAPI_CLASSID; + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NS:dhcpv6csdk._DHCPV6CAPI_CLASSID")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPV6CAPI_CLASSID + { + /// Reserved for future use. Must be set to 0. + public uint Flags; + + /// Class ID binary data. + public IntPtr Data; + + /// Size of Data, in bytes. + public uint nBytesData; + } + + /// A DHCPV6CAPI_PARAMS structure contains a requested parameter. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/ns-dhcpv6csdk-dhcpv6capi_params typedef struct _DHCPV6CAPI_PARAMS { + // ULONG Flags; ULONG OptionId; BOOL IsVendor; LPBYTE Data; DWORD nBytesData; } DHCPV6CAPI_PARAMS, *PDHCPV6CAPI_PARAMS, *LPDHCPV6CAPI_PARAMS; + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NS:dhcpv6csdk._DHCPV6CAPI_PARAMS")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPV6CAPI_PARAMS + { + /// Reserved for future use. + public uint Flags; + + /// + /// Identifier for the DHCPv6 parameter being requested. + /// DHCPV6_OPTION_CLIENTID + /// DHCPV6_OPTION_SERVERID + /// DHCPV6_OPTION_IA_NA + /// DHCPV6_OPTION_IA_TA + /// DHCPV6_OPTION_ORO + /// DHCPV6_OPTION_PREFERENCE + /// DHCPV6_OPTION_UNICAST + /// DHCPV6_OPTION_RAPID_COMMIT + /// DHCPV6_OPTION_USER_CLASS + /// DHCPV6_OPTION_VENDOR_CLASS + /// DHCPV6_OPTION_VENDOR_OPTS + /// DHCPV6_OPTION_RECONF_MSG + /// DHCPV6_OPTION_SIP_SERVERS_NAMES + /// DHCPV6_OPTION_SIP_SERVERS_ADDRS + /// DHCPV6_OPTION_DNS_SERVERS + /// DHCPV6_OPTION_DOMAIN_LIST + /// DHCPV6_OPTION_IA_PD + /// DHCPV6_OPTION_NIS_SERVERS + /// DHCPV6_OPTION_NISP_SERVERS + /// DHCPV6_OPTION_NIS_DOMAIN_NAME + /// DHCPV6_OPTION_CLIENTIDNISP_DOMAIN_NAME + /// + public DHCPV6_OPTION_ID OptionId; + + /// This option is set to TRUE if this parameter is vendor-specific. Otherwise, it is FALSE. + [MarshalAs(UnmanagedType.Bool)] + public bool IsVendor; + + /// Contains the actual parameter data. + public IntPtr Data; + + /// Size of the Data member, in bytes. + public uint nBytesData; + } + + /// The DHCPV6CAPI_PARAMS_ARRAY structure contains an array of requested parameters. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/ns-dhcpv6csdk-dhcpv6capi_params_array typedef struct + // _DHCPV6CAPI_PARAMS_ARRAY { ULONG nParams; LPDHCPV6CAPI_PARAMS Params; } DHCPV6CAPI_PARAMS_ARRAY, *PDHCPV6CAPI_PARAMS_ARRAY, *LPDHCPV6CAPI_PARAMS_ARRAY; + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NS:dhcpv6csdk._DHCPV6CAPI_PARAMS_ARRAY")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPV6CAPI_PARAMS_ARRAY + { + /// Number of parameters in the array. + public uint nParams; + + /// Pointer to a DHCPV6CAPI_PARAMS structure that contains a parameter. + public IntPtr /*LPDHCPV6CAPI_PARAMS*/ Params; + } + + /// The DHCPV6Prefix contains an IPv6 prefix. + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/ns-dhcpv6csdk-dhcpv6prefix typedef struct _DHCPV6Prefix { UCHAR + // prefix[16]; DWORD prefixLength; DWORD preferredLifeTime; DWORD validLifeTime; StatusCode status; } DHCPV6Prefix, *PDHCPV6Prefix, *LPDHCPV6Prefix; + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NS:dhcpv6csdk._DHCPV6Prefix")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPV6Prefix + { + /// 128 bit prefix. + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[] prefix; + + /// Length of the prefix. + public uint prefixLength; + + /// Preferred lifetime of the prefix, in seconds. + public uint preferredLifeTime; + + /// The valid lifetime of the prefix in seconds. + public uint validLifeTime; + + /// The status code returned. + public StatusCode status; + } + + /// The DHCPV6PrefixLeaseInformation structure contains information about a prefix lease. + /// + /// In a prefix delegation scenario, the validation of lease lifetime values (specific status codes, T1, T2, + /// MaxLeaseExpirationTime, and LastRenewalTime) are performed by the calling API, rather than the application + /// consuming the data, as the latter might interpret these values differently. + /// + // https://docs.microsoft.com/en-us/windows/win32/api/dhcpv6csdk/ns-dhcpv6csdk-dhcpv6prefixleaseinformation typedef struct + // _DHCPV6PrefixLeaseInformation { DWORD nPrefixes; LPDHCPV6Prefix prefixArray; DWORD iaid; time_t T1; time_t T2; time_t + // MaxLeaseExpirationTime; time_t LastRenewalTime; StatusCode status; LPBYTE ServerId; DWORD ServerIdLen; } + // DHCPV6PrefixLeaseInformation, *PDHCPV6PrefixLeaseInformation, *LPDHCPV6PrefixLeaseInformation; + [PInvokeData("dhcpv6csdk.h", MSDNShortId = "NS:dhcpv6csdk._DHCPV6PrefixLeaseInformation")] + [StructLayout(LayoutKind.Sequential)] + public struct DHCPV6PrefixLeaseInformation + { + /// Number of prefixes. + public uint nPrefixes; + + /// Pointer to a list DHCPV6Prefix structures that contain the prefixes requested or returned by the server. + public IntPtr /*LPDHCPV6Prefix*/ prefixArray; + + /// Identity Association identifier for the prefix operation. + public uint iaid; + + /// The renewal time for the prefix, in seconds. + public time_t T1; + + /// The rebind time of the prefix, in seconds. + public time_t T2; + + /// The maximum lease expiration time of all the prefix leases in this structure. + public time_t MaxLeaseExpirationTime; + + /// The time at which the last renewal for the prefixes occurred. + public time_t LastRenewalTime; + + /// + /// + /// Status code returned by the server for the IAPD. The following codes can be returned by the DHCP server for prefix + /// delegation scenarios: + /// + /// + /// + /// Value + /// Meaning + /// + /// + /// STATUS_NO_ERROR 0 + /// The prefix was successfully leased or renewed. + /// + /// + /// STATUS_UNSPECIFIED_FAILURE 1 + /// The lease or renewal action failed for an unspecified reason. + /// + /// + /// STATUS_NO_BINDING 3 + /// The DHCPv6 server does not have a binding for the prefix. + /// + /// + /// STATUS_NOPREFIX_AVAIL 6 + /// The DHCPv6 server does not have a prefix availble to offer the requesting client. + /// + /// + /// + public StatusCode status; + + /// The server DUID from which the prefix is received. This data is used in subsequent renews. + public IntPtr ServerId; + + /// The length of the above DUID data. + public uint ServerIdLen; + } + } +} \ No newline at end of file diff --git a/PInvoke/Dhcp/Vanara.PInvoke.Dhcp.csproj b/PInvoke/Dhcp/Vanara.PInvoke.Dhcp.csproj new file mode 100644 index 000000000..a55904398 --- /dev/null +++ b/PInvoke/Dhcp/Vanara.PInvoke.Dhcp.csproj @@ -0,0 +1,27 @@ + + + + Dhcpcsvc6.dll;Dhcpcsvc.dll + + + PInvoke API (methods, structures and constants) imported from Windows Dhcpcsvc.dll and Dhcpcsvc6.dll. + $(AssemblyName) + net20;net35;net40;net45;net5.0-windows;netstandard2.0;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;netcoreapp3.1 + Vanara.PInvoke.Dhcp + $(AssemblyName) + pinvoke;vanara;net-extensions;interop;Dhcp + Currently implements: + +Functions +DhcpCApiCleanup, DhcpCApiInitialize, DhcpDeRegisterParamChange, DhcpGetOriginalSubnetMask, DhcpRegisterParamChange, DhcpRemoveDNSRegistrations, DhcpRequestParams, DhcpUndoRequestParams, Dhcpv6CApiCleanup, Dhcpv6CApiInitialize, Dhcpv6ReleasePrefix, Dhcpv6RenewPrefix, Dhcpv6RequestParams, Dhcpv6RequestPrefix, McastApiCleanup, McastApiStartup, McastEnumerateScopes, McastGenUID, McastReleaseAddress, McastRenewAddress, McastRequestAddress + +Structures +DHCP_IP_ADDRESS, DHCPAPI_PARAMS, DHCPCAPI_CLASSID, DHCPCAPI_PARAMS_ARRAY, DHCPV6CAPI_CLASSID, DHCPV6CAPI_PARAMS, DHCPV6CAPI_PARAMS_ARRAY, DHCPV6Prefix, DHCPV6PrefixLeaseInformation + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 0b62012f5..7403d5dd7 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ credui.dll | [Vanara.PInvoke.CredUI](https://github.com/dahall/Vanara/blob/maste crypt32.dll, bcrypt.dll, ncrypt.dll, tokenbinding.dll, cryptui.dll, cryptnet.dll, cryptdlg.dll | [Vanara.PInvoke.Cryptography](https://github.com/dahall/Vanara/blob/master/PInvoke/Cryptography/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/100%25-green.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.Cryptography?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.Cryptography?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.Cryptography) d2d1.dll, dxgi.dll, dwrite.dll, windowscodecs.dll | [Vanara.PInvoke.Graphics](https://github.com/dahall/Vanara/blob/master/PInvoke/Graphics/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/100%25-green.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.Graphics?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.Graphics?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.Graphics) DbgHelp.dll, ImageHlp.dll | [Vanara.PInvoke.DbgHelp](https://github.com/dahall/Vanara/blob/master/PInvoke/DbgHelp/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/100%25-green.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.DbgHelp?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.DbgHelp?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.DbgHelp) +Dhcpcsvc6.dll, Dhcpcsvc.dll | [Vanara.PInvoke.Dhcp](https://github.com/dahall/Vanara/blob/master/PInvoke/Dhcp/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/66%25-yellow.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.Dhcp?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.Dhcp?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.Dhcp) DnsApi.dll | [Vanara.PInvoke.DnsApi](https://github.com/dahall/Vanara/blob/master/PInvoke/DnsApi/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/100%25-green.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.DnsApi?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.DnsApi?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.DnsApi) Drt.dll | [Vanara.PInvoke.Drt](https://github.com/dahall/Vanara/blob/master/PInvoke/Drt/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/100%25-green.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.Drt?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.Drt?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.Drt) dwmapi.dll | [Vanara.PInvoke.DwmApi](https://github.com/dahall/Vanara/blob/master/PInvoke/DwmApi/CorrelationReport.md) | ![Coverage](https://img.shields.io/badge/100%25-green.svg?style=flat-square) | [![Nuget](https://img.shields.io/nuget/v/Vanara.PInvoke.DwmApi?label=%20&logo=nuget&style=flat-square)![Nuget](https://img.shields.io/nuget/dt/Vanara.PInvoke.DwmApi?label=%20&style=flat-square)](https://www.nuget.org/packages/Vanara.PInvoke.DwmApi) diff --git a/UnitTests/PInvoke/Dhcp/Dhcp.csproj b/UnitTests/PInvoke/Dhcp/Dhcp.csproj new file mode 100644 index 000000000..d4caa63bb --- /dev/null +++ b/UnitTests/PInvoke/Dhcp/Dhcp.csproj @@ -0,0 +1,8 @@ + + + UnitTest.PInvoke.Dhcp + + + + + \ No newline at end of file diff --git a/UnitTests/PInvoke/Dhcp/DhcpTests.cs b/UnitTests/PInvoke/Dhcp/DhcpTests.cs new file mode 100644 index 000000000..445753cdd --- /dev/null +++ b/UnitTests/PInvoke/Dhcp/DhcpTests.cs @@ -0,0 +1,66 @@ +using NUnit.Framework; +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; +using Vanara.Extensions; +using Vanara.InteropServices; +using static Vanara.PInvoke.Dhcp; + +namespace Vanara.PInvoke.Tests +{ + [TestFixture] + public class DhcpTests + { + internal readonly static NetworkInterface adp = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.OperationalStatus == OperationalStatus.Up).FirstOrDefault(); + internal readonly static string dhcpSvr = GetDhcpServers().FirstOrDefault()?.ToString(); + internal const string dhcpSub = "192.168.0.0"; + + [OneTimeSetUp] + public void _Setup() => DhcpCApiInitialize(out _).ThrowIfFailed(); + + [OneTimeTearDown] + public void _TearDown() => DhcpCApiCleanup(); + + [Test] + public void DhcpRequestParamsTest() + { + var sendParams = default(DHCPCAPI_PARAMS_ARRAY); + using var pparam = new SafeCoTaskMemStruct(new DHCPAPI_PARAMS { OptionId = DHCP_OPTION_ID.OPTION_SUBNET_MASK }); + //using var pparam = new SafeCoTaskMemStruct(new DHCPAPI_PARAMS { OptionId = DHCP_OPTION_ID.OPTION_HOST_NAME }); + var reqParams = new DHCPCAPI_PARAMS_ARRAY { nParams = 1, Params = pparam }; + uint sz = 1000; + using var mem = new SafeCoTaskMemHandle(sz); + Assert.That(DhcpRequestParams(DHCPCAPI_REQUEST.DHCPCAPI_REQUEST_SYNCHRONOUS, default, adp.Id, IntPtr.Zero, sendParams, reqParams, mem, ref sz, null), ResultIs.Successful); + var p = pparam.Value; + Assert.That(p.nBytesData, Is.GreaterThan(0)); + TestContext.Write(new IPAddress(p.Data.ToArray(4)).ToString()); + //TestContext.Write(StringHelper.GetString(p.Data, CharSet.Ansi, p.nBytesData)); + } + + [Test] + public void DhcpRegisterParamChangeTest() + { + using var pparam = new SafeCoTaskMemStruct(new DHCPAPI_PARAMS { OptionId = DHCP_OPTION_ID.OPTION_ROUTER_ADDRESS }); + //using var pparam = new SafeCoTaskMemStruct(new DHCPAPI_PARAMS { OptionId = DHCP_OPTION_ID.OPTION_HOST_NAME }); + var watchParams = new DHCPCAPI_PARAMS_ARRAY { nParams = 1, Params = pparam }; + Assert.That(DhcpRegisterParamChange(DHCPCAPI_REGISTER_HANDLE_EVENT, default, adp.Id, IntPtr.Zero, watchParams, out var hEvent), ResultIs.Successful); + Kernel32.WaitForSingleObject(hEvent, 2000); + Assert.That(DhcpDeRegisterParamChange(Event: hEvent), ResultIs.Successful); + } + + internal static DHCP_IP_ADDRESS IPAddrFromStr(string addr) + { + if (!System.Net.IPAddress.TryParse(addr, out var ip) || ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) + return 0; +#pragma warning disable CS0618 // Type or member is obsolete + return (uint)ip.Address; +#pragma warning restore CS0618 // Type or member is obsolete + } + + static IEnumerable GetDhcpServers() => NetworkInterface.GetAllNetworkInterfaces().Where(i => i.OperationalStatus == OperationalStatus.Up).SelectMany(i => i.GetIPProperties().DhcpServerAddresses).Distinct(); + } +} \ No newline at end of file diff --git a/UnitTests/PInvoke/Dhcp/DhcpV6Tests.cs b/UnitTests/PInvoke/Dhcp/DhcpV6Tests.cs new file mode 100644 index 000000000..0d3e1c478 --- /dev/null +++ b/UnitTests/PInvoke/Dhcp/DhcpV6Tests.cs @@ -0,0 +1,50 @@ +using NUnit.Framework; +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; +using Vanara.Extensions; +using Vanara.InteropServices; +using static Vanara.PInvoke.Dhcp; + +namespace Vanara.PInvoke.Tests +{ + [TestFixture] + public class DhcpV6Tests + { + [OneTimeSetUp] + public void _Setup() => Dhcpv6CApiInitialize(out _); + + [OneTimeTearDown] + public void _TearDown() => Dhcpv6CApiCleanup(); + + [Test] + public void Dhcpv6RequestParamsTest() + { + using var pparam = new SafeCoTaskMemStruct(new DHCPV6CAPI_PARAMS { OptionId = DHCPV6_OPTION_ID.DHCPV6_OPTION_CLIENTID }); + var recdParams = new DHCPV6CAPI_PARAMS_ARRAY { nParams = 1, Params = pparam }; + uint sz = 1000; + using var mem = new SafeCoTaskMemHandle(sz); + Assert.That(Dhcpv6RequestParams(false, default, DhcpTests.adp.Id, IntPtr.Zero, recdParams, mem, ref sz), ResultIs.Successful); + } + + [Test] + public void Dhcpv6RequestPrefixTest() + { + using var pPrefix = new SafeNativeArray(1); + using var duid = new SafeCoTaskMemHandle(128); + var pfxLease = new DHCPV6PrefixLeaseInformation { iaid = 7, nPrefixes = 1, prefixArray = pPrefix, ServerId = duid, ServerIdLen = duid.Size }; + Assert.That(Dhcpv6RequestPrefix(DhcpTests.adp.Id, IntPtr.Zero, ref pfxLease, out var ttw), ResultIs.Successful); + if (ttw != uint.MaxValue) + { + Kernel32.Sleep(ttw * 1000); + Assert.That(Dhcpv6RenewPrefix(DhcpTests.adp.Id, IntPtr.Zero, ref pfxLease, out ttw, false), ResultIs.Successful); + } + if (ttw != uint.MaxValue) Kernel32.Sleep(ttw * 1000); + Assert.That(Dhcpv6ReleasePrefix(DhcpTests.adp.Id, IntPtr.Zero, pfxLease), ResultIs.Successful); + } + } +} \ No newline at end of file diff --git a/Vanara.sln b/Vanara.sln index 3349d0e7d..d805308bc 100644 --- a/Vanara.sln +++ b/Vanara.sln @@ -257,6 +257,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build Files", "Build Files" UnitTests\Directory.Build.targets = UnitTests\Directory.Build.targets EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vanara.PInvoke.Dhcp", "PInvoke\Dhcp\Vanara.PInvoke.Dhcp.csproj", "{CB2EAE48-7503-459F-BD63-69FAE9740A20}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dhcp", "UnitTests\PInvoke\Dhcp\Dhcp.csproj", "{94D2C3ED-2101-439C-AAAA-452883A493B4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -815,6 +819,16 @@ Global {80E10D65-5B7C-4BC8-8000-2B5BC9A21874}.Debug|Any CPU.Build.0 = Debug|Any CPU {80E10D65-5B7C-4BC8-8000-2B5BC9A21874}.DebugNoTests|Any CPU.ActiveCfg = Debug|Any CPU {80E10D65-5B7C-4BC8-8000-2B5BC9A21874}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB2EAE48-7503-459F-BD63-69FAE9740A20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB2EAE48-7503-459F-BD63-69FAE9740A20}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB2EAE48-7503-459F-BD63-69FAE9740A20}.DebugNoTests|Any CPU.ActiveCfg = Debug|Any CPU + {CB2EAE48-7503-459F-BD63-69FAE9740A20}.DebugNoTests|Any CPU.Build.0 = Debug|Any CPU + {CB2EAE48-7503-459F-BD63-69FAE9740A20}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB2EAE48-7503-459F-BD63-69FAE9740A20}.Release|Any CPU.Build.0 = Release|Any CPU + {94D2C3ED-2101-439C-AAAA-452883A493B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94D2C3ED-2101-439C-AAAA-452883A493B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94D2C3ED-2101-439C-AAAA-452883A493B4}.DebugNoTests|Any CPU.ActiveCfg = Debug|Any CPU + {94D2C3ED-2101-439C-AAAA-452883A493B4}.Release|Any CPU.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -924,6 +938,8 @@ Global {CD8A5176-6EEC-4739-9748-A45D3FDECFA0} = {385CAD2D-0A5E-4F80-927B-D5499D126B90} {80E10D65-5B7C-4BC8-8000-2B5BC9A21874} = {385CAD2D-0A5E-4F80-927B-D5499D126B90} {27FA62A4-C1EE-461D-9F5B-287C0A848B88} = {3EC6B40D-71D3-4E59-A0E0-544EC605FE11} + {CB2EAE48-7503-459F-BD63-69FAE9740A20} = {212ABBD0-B724-4CFA-9D6D-E3891547FA90} + {94D2C3ED-2101-439C-AAAA-452883A493B4} = {385CAD2D-0A5E-4F80-927B-D5499D126B90} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {543FAC75-2AF1-4EF1-9609-B242B63FEED4}