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 Vanara.PInvoke; using static Vanara.PInvoke.Dhcp; using static Vanara.PInvoke.Kernel32; namespace Vanara.Net; /// Encapsulates the functions and properties for a DHCP client. /// public class DhcpClient : IDisposable { #pragma warning disable IDE0052 // Remove unread private members private static readonly DhcpInit init = new(); #pragma warning restore IDE0052 // Remove unread private members private readonly SafeEventHandle closing = CreateEvent(default, false, false), updateList = CreateEvent(default, false, false); private readonly SafeHTHREAD hThread; private readonly Dictionary paramChgEvents = new(); /// Initializes a new instance of the class. public DhcpClient() { var h = GCHandle.Alloc(this, GCHandleType.Normal); hThread = CreateThread(default, 0, ThreadProc, (IntPtr)h, CREATE_THREAD_FLAGS.RUN_IMMEDIATELY, out _); } /// /// Occurs when the value related to a has changed. /// Use the to set the list of identifiers that are watched. /// public event Action ParamChanged; /// /// Specifies whether or not the client may assume that all subnets of the IP network to which the client is connected use the same MTU /// as the subnet of that network to which the client is directly connected. A value of true indicates that all subnets share the same /// MTU. A value of false means that the client should assume that some subnets of the directly connected network may have smaller MTUs. /// public bool AllSubnetsMTU => GetParam(DHCP_OPTION_ID.OPTION_ALL_SUBNETS_MTU); /// Specifies the timeout in seconds for ARP cache entries. public TimeSpan ARPCacheTimeout => TimeSpan.FromSeconds(GetParam(DHCP_OPTION_ID.OPTION_ARP_CACHE_TIMEOUT)); /// /// Specifies whether or not the client should respond to subnet mask requests using ICMP. A value of false indicates that the client /// should not respond. A value of true means that the client should respond. /// public bool BeAMaskSupplier => GetParam(DHCP_OPTION_ID.OPTION_BE_A_MASK_SUPPLIER); /// /// Specifies whether or not the client should solicit routers using the Router Discovery mechanism defined in RFC 1256. A value of false /// indicates that the client should not perform router discovery. A value of true means that the client should perform router discovery. /// public bool BeARouter => GetParam(DHCP_OPTION_ID.OPTION_BE_A_ROUTER); /// /// Identifies a bootstrap file. If supported by the client, it should have the same effect as the filename declaration. BOOTP clients /// are unlikely to support this option. Some DHCP clients will support it, and others actually require it. /// public string BootfileName => GetParam(DHCP_OPTION_ID.OPTION_BOOTFILE_NAME); /// This option specifies the length in 512-octet blocks of the default boot image for the client. public ushort BootFileSize => GetParam(DHCP_OPTION_ID.OPTION_BOOT_FILE_SIZE); /// /// This option specifies the broadcast address in use on the client’s subnet. Legal values for broadcast addresses are specified in /// section 3.2.1.3 of STD 3 (RFC1122). /// public IPAddress BroadcastAddress => new(GetParam(DHCP_OPTION_ID.OPTION_BROADCAST_ADDRESS)); /// /// Gets or sets the list of values that, when changed, will fire the event. /// /// The list of values that, when changed, will fire the event. public DHCP_OPTION_ID[] ChangeEventIds { get => paramChgEvents.Values.ToArray(); set { ClearListeners(); updateList.Set(); if (value is null || value.Length == 0) return; string adapter = Adapter; foreach (DHCP_OPTION_ID id in value) { if (DhcpRegisterParamChange(DHCPCAPI_REGISTER_HANDLE_EVENT, default, adapter, default, DHCPCAPI_PARAMS_ARRAY.Make(out _, id), out HEVENT hEvt).Succeeded) { paramChgEvents.Add(hEvt, id); } } updateList.Set(); } } /// /// Class identifier (ID) that should be used if DHCP INFORM messages are being transmitted onto the network. This value is optional. /// public byte[] ClassId { get; set; } /// /// This option is used by some DHCP clients as a way for users to specify identifying information to the client. This can be used in a /// similar way to the vendor-class-identifier option, but the value of the option is specified by the user, not the vendor. Most recent /// DHCP clients have a way in the user interface to specify the value for this identifier, usually as a text string. /// public string ClientClassInfo => GetParam(DHCP_OPTION_ID.OPTION_CLIENT_CLASS_INFO); /// /// This option can be used to specify a DHCP client identifier in a host declaration, so that dhcpd can find the host record by matching /// against the client identifier. /// public string ClientId => GetParam(DHCP_OPTION_ID.OPTION_CLIENT_ID); /// /// The cookie server option specifies a list of RFC 865 cookie servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] CookieServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_COOKIE_SERVERS)); /// This option specifies the default time-to-live that the client should use on outgoing datagrams. public byte DefaultTTL => GetParam(DHCP_OPTION_ID.OPTION_DEFAULT_TTL); /// This option specifies the domain name that client should use when resolving hostnames via the Domain Name System. public string DomainName => GetParam(DHCP_OPTION_ID.OPTION_DOMAIN_NAME); /// /// The domain-name-servers option specifies a list of Domain Name System (STD 13, RFC 1035) name servers available to the client. /// Servers should be listed in order of preference. /// public IPAddress[] DomainNameServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_DOMAIN_NAME_SERVERS)); /// /// This option specifies whether or not the client should use Ethernet Version 2 (RFC 894) or IEEE 802.3 (RFC 1042) encapsulation if the /// interface is an Ethernet. A value of false indicates that the client should use RFC 894 encapsulation. A value of true means that the /// client should use RFC 1042 encapsulation. /// public bool EthernetEncapsulation => GetParam(DHCP_OPTION_ID.OPTION_ETHERNET_ENCAPSULATION); /// /// This option specifies the name of a file containing additional options to be interpreted according to the DHCP option format as /// specified in RFC2132. /// public string ExtensionsPath => GetParam(DHCP_OPTION_ID.OPTION_EXTENSIONS_PATH); /// /// This option specifies the name of the client. The name may or may not be qualified with the local domain name (it is preferable to /// use the domain-name option to specify the domain name). See RFC 1035 for character set restrictions. This option is only honored by /// dhclient-script(8) if the hostname for the client machine is not set. /// public string HostName => GetParam(DHCP_OPTION_ID.OPTION_HOST_NAME); /// /// The ien116-name-servers option specifies a list of IEN 116 name servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] IEN116NameServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_IEN116_NAME_SERVERS)); /// The Internet Explorer proxy. public string IEProxy => GetParam(DHCP_OPTION_ID.OPTION_MSFT_IE_PROXY); /// /// The impress-server option specifies a list of Imagen Impress servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] ImpressServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_IMPRESS_SERVERS)); /// public uint KeepAliveDataSize => GetParam(DHCP_OPTION_ID.OPTION_KEEP_ALIVE_DATA_SIZE); /// /// This option specifies the interval (in seconds) that the client TCP should wait before sending a keepalive message on a TCP /// connection. The time is specified as a 32-bit unsigned integer. A value of zero indicates that the client should not generate /// keepalive messages on connections unless specifically requested by an application. /// public TimeSpan KeepAliveInterval => TimeSpan.FromSeconds(GetParam(DHCP_OPTION_ID.OPTION_KEEP_ALIVE_INTERVAL)); /// /// This option is used in a client request (DHCPDISCOVER or DHCPREQUEST) to allow the client to request a lease time for the IP address. /// In a server reply (DHCPOFFER), a DHCP server uses this option to specify the lease time it is willing to offer. /// public TimeSpan LeaseTime => new(GetParam(DHCP_OPTION_ID.OPTION_LEASE_TIME)); /// /// The log-server option specifies a list of MIT-LCS UDP log servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] LogServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_LOG_SERVERS)); /// /// The LPR server option specifies a list of RFC 1179 line printer servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] LPRServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_LPR_SERVERS)); /// /// This option specifies the maximum size datagram that the client should be prepared to reassemble. The minimum legal value is 576. /// public ushort MaxReassemblySize => GetParam(DHCP_OPTION_ID.OPTION_MAX_REASSEMBLY_SIZE); /// /// This option specifies the path-name of a file to which the client’s core image should be dumped in the event the client crashes. The /// path is formatted as a character string consisting of characters from the NVT ASCII character set. /// public string MeritDumpFile => GetParam(DHCP_OPTION_ID.OPTION_MERIT_DUMP_FILE); /// /// This option is used by a DHCP server to provide an error message to a DHCP client in a DHCPNAK message in the event of a failure. A /// client may use this option in a DHCPDECLINE message to indicate why the client declined the offered parameters. /// public string Message => GetParam(DHCP_OPTION_ID.OPTION_MESSAGE); /// /// This option, when sent by the client, specifies the maximum size of any response that the server sends to the client. When specified /// on the server, if the client did not send a dhcp-max-message-size option, the size specified on the server is used. This works for /// BOOTP as well as DHCP responses. /// public ushort MessageLength => GetParam(DHCP_OPTION_ID.OPTION_MESSAGE_LENGTH); /// This option, sent by both client and server, specifies the type of DHCP message contained in the DHCP packet. public DhcpMessageType MessageType => GetParam(DHCP_OPTION_ID.OPTION_MESSAGE_TYPE); /// This option specifies the MTU to use on this interface. The minimum legal value for the MTU is 68. public ushort MTU => GetParam(DHCP_OPTION_ID.OPTION_MTU); /// /// The NetBIOS datagram distribution server (NBDD) option specifies a list of RFC 1001/1002 NBDD servers listed in order of preference. /// public IPAddress[] NetBIOSDatagramServer => ToIP(GetParam(DHCP_OPTION_ID.OPTION_NETBIOS_DATAGRAM_SERVER)); /// /// The NetBIOS name server (NBNS) option specifies a list of RFC 1001/1002 NBNS name servers listed in order of preference. NetBIOS Name /// Service is currently more commonly referred to as WINS. WINS servers can be specified using the netbios-name-servers option. /// public IPAddress[] NetBIOSNameServer => ToIP(GetParam(DHCP_OPTION_ID.OPTION_NETBIOS_NAME_SERVER)); /// /// The NetBIOS node type option allows NetBIOS over TCP/IP clients which are configurable to be configured as described in RFC /// 1001/1002. The value is specified as a single octet which identifies the client type. /// public NetBIOSNodeType NetBIOSNodeType => GetParam(DHCP_OPTION_ID.OPTION_NETBIOS_NODE_TYPE); /// /// The NetBIOS scope option specifies the NetBIOS over TCP/IP scope parameter for the client as specified in RFC 1001/1002. See RFC1001, /// RFC1002, and RFC1035 for character-set restrictions. /// public string NetBIOSScopeOption => GetParam(DHCP_OPTION_ID.OPTION_NETBIOS_SCOPE_OPTION); /// /// The netinfo-server-address option has not been described in any RFC, but has been allocated (and is claimed to be in use) by Apple /// Computers. It’s hard to say if the above is the correct format, or what clients might be expected to do if values were configured. /// Use at your own risk. /// public IPAddress[] NetworkInfoServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_NETWORK_INFO_SERVERS)); /// /// This option specifies the name of the client’s NIS (Sun Network Information Services) domain. The domain is formatted as a character /// string consisting of characters from the NVT ASCII character set. /// public string NetworkInfoServiceDomain => GetParam(DHCP_OPTION_ID.OPTION_NETWORK_INFO_SERVICE_DOM); /// /// The NNTP server option specifies a list of NNTP servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] NetworkTimeServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_NETWORK_TIME_SERVERS)); /// /// This option specifies whether the client should configure its IP layer to allow forwarding of datagrams with non-local source routes /// (see Section 3.3.5 of [4] for a discussion of this topic). A value of false means disallow forwarding of such datagrams, and a value /// of true means allow forwarding. /// public bool NonLocalSourceRouting => GetParam(DHCP_OPTION_ID.OPTION_NON_LOCAL_SOURCE_ROUTING); /// public bool OkToOverlay => GetParam(DHCP_OPTION_ID.OPTION_OK_TO_OVERLAY); /// /// This option, when sent by the client, specifies which options the client wishes the server to return. Normally, in the ISC DHCP /// client, this is done using the request statement. If this option is not specified by the client, the DHCP server will normally return /// every option that is valid in scope and that fits into the reply. When this option is specified on the server, the server returns the /// specified options. This can be used to force a client to take options that it hasn’t requested, and it can also be used to tailor the /// response of the DHCP server for clients that may need a more limited set of options than those the server would normally return. /// public byte[] ParameterRequestList => GetParam(DHCP_OPTION_ID.OPTION_PARAMETER_REQUEST_LIST); /// /// This option specifies the timeout (in seconds) to use when aging Path MTU values discovered by the mechanism defined in RFC 1191. /// public TimeSpan PathMTUAgingTimeout => TimeSpan.FromSeconds(GetParam(DHCP_OPTION_ID.OPTION_PMTU_AGING_TIMEOUT)); /// /// This option specifies a table of MTU sizes to use when performing Path MTU Discovery as defined in RFC 1191. The table is formatted /// as a list of 16-bit unsigned integers, ordered from smallest to largest. The minimum MTU value cannot be smaller than 68. /// public ushort[] PathMTUPlateauTable => GetParam(DHCP_OPTION_ID.OPTION_PMTU_PLATEAU_TABLE); /// /// This option specifies whether or not the client should perform subnet mask discovery using ICMP. A value of false indicates that the /// client should not perform mask discovery. A value of true means that the client should perform mask discovery. /// public bool PerformMaskDiscovery => GetParam(DHCP_OPTION_ID.OPTION_PERFORM_MASK_DISCOVERY); /// /// This option specifies whether or not the client should solicit routers using the Router Discovery mechanism defined in RFC 1256. A /// value of false indicates that the client should not perform router discovery. A value of true means that the client should perform /// router discovery. /// public bool PerformRouterDiscovery => GetParam(DHCP_OPTION_ID.OPTION_PERFORM_ROUTER_DISCOVERY); /// /// This option specifies policy filters for non-local source routing. The filters consist of a list of IP addresses and masks which /// specify destination/mask pairs with which to filter incoming source routes. /// Any source routed datagram whose next-hop address does not match one of the filters should be discarded by the client. /// public IPAddress[] PolicyFilter => ToIP(GetParam(DHCP_OPTION_ID.OPTION_POLICY_FILTER_FOR_NLSR)); /// /// /// This option specifies the number of seconds from the time a client gets an address until the client transitions to the REBINDING state. /// /// This option is user configurable, but it will be ignored if the value is greater than or equal to the lease time. /// /// To make DHCPv4+DHCPv6 migration easier in the future, any value configured in this option is also used as a DHCPv6 "T1" (renew) time. /// /// public TimeSpan RebindTime => TimeSpan.FromSeconds(GetParam(DHCP_OPTION_ID.OPTION_REBIND_TIME)); /// /// /// This option specifies the number of seconds from the time a client gets an address until the client transitions to the RENEWING state. /// /// /// This option is user configurable, but it will be ignored if the value is greater than or equal to the rebinding time, or lease time. /// /// To make DHCPv4+DHCPv6 migration easier in the future, /// public TimeSpan RenewalTime => TimeSpan.FromSeconds(GetParam(DHCP_OPTION_ID.OPTION_RENEWAL_TIME)); /// /// This option is used by the client in a DHCPDISCOVER to request that a particular IP address be assigned. /// This option is not user configurable. /// public IPAddress RequestedAddress => new(GetParam(DHCP_OPTION_ID.OPTION_REQUESTED_ADDRESS)); /// public IPAddress[] RlpServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_RLP_SERVERS)); /// /// This option specifies the path-name that contains the client’s root disk. The path is formatted as a character string consisting of /// characters from the NVT ASCII character set. /// public string RootDisk => GetParam(DHCP_OPTION_ID.OPTION_ROOT_DISK); /// /// The routers option specifies a list of IP addresses for routers on the client’s subnet. Routers should be listed in order of preference. /// public IPAddress RouterAddress => new(GetParam(DHCP_OPTION_ID.OPTION_ROUTER_ADDRESS)); /// This option specifies the address to which the client should transmit router solicitation requests. public IPAddress RouterSolicitationAddress => new(GetParam(DHCP_OPTION_ID.OPTION_ROUTER_SOLICITATION_ADDR)); /// /// GUID of the adapter on which requested data is being made. Must be under 256 characters. If this value is , the /// first adapter with an address supplied via DHCP will be used. /// public string SelectedAdapterId { get; set; } /// /// /// This option is used in DHCPOFFER and DHCPREQUEST messages, and may optionally be included in the DHCPACK and DHCPNAK messages. DHCP /// servers include this option in the DHCPOFFER in order to allow the client to distinguish between lease offers. DHCP clients use the /// contents of the ´server identifier´ field as the destination address for any DHCP messages unicast to the DHCP server. DHCP clients /// also indicate which of several lease offers is being accepted by including this option in a DHCPREQUEST message. /// /// The value of this option is the IP address of the server. /// This option is not directly user configurable. See the server-identifier server option in dhcpd.conf(5). /// public IPAddress ServerIdentifier => new(GetParam(DHCP_OPTION_ID.OPTION_SERVER_IDENTIFIER)); /// /// /// This option specifies a list of static routes that the client should install in its routing cache. If multiple routes to the same /// destination are specified, they are listed in descending order of priority. /// /// /// The routes consist of a list of IP address pairs. The first address is the destination address, and the second address is the router /// for the destination. /// /// /// The default route (0.0.0.0) is an illegal destination for a static route. To specify the default route, use the routers option. Also, /// please note that this option is not intended for classless IP routing - it does not include a subnet mask. Since classless IP routing /// is now the most widely deployed routing standard, this option is virtually useless, and is not implemented by any of the popular DHCP /// clients, for example the Microsoft DHCP client. /// /// public IPAddress[] StaticRoutes => ToIP(GetParam(DHCP_OPTION_ID.OPTION_STATIC_ROUTES)); /// /// The subnet mask option specifies the client’s subnet mask as per RFC 950. If no subnet mask option is provided anywhere in scope, as /// a last resort dhcpd will use the subnet mask from the subnet declaration for the network on which an address is being assigned. /// However, any subnet-mask option declaration that is in scope for the address being assigned will override the subnet mask specified /// in the subnet declaration. /// public IPAddress SubnetMask => new(GetParam(DHCP_OPTION_ID.OPTION_SUBNET_MASK)); /// This specifies the IP address of the client’s swap server. public IPAddress SwapServer => new(GetParam(DHCP_OPTION_ID.OPTION_SWAP_SERVER)); /// /// This option is used to identify a TFTP server and, if supported by the client, should have the same effect as the server-name /// declaration. BOOTP clients are unlikely to support this option. Some DHCP clients will support it, and others actually require it. /// public string TFTPServerName => GetParam(DHCP_OPTION_ID.OPTION_TFTP_SERVER_NAME); /// The time-offset option specifies the offset of the client’s subnet in seconds from Coordinated Universal Time (UTC). public DateTimeOffset TimeOffset => new(0, TimeSpan.FromSeconds(GetParam(DHCP_OPTION_ID.OPTION_TIME_OFFSET))); /// /// The time-server option specifies a list of RFC 868 time servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] TimeServers => ToIP(GetParam(DHCP_OPTION_ID.OPTION_TIME_SERVERS)); /// /// This option specifies whether or not the client should negotiate the use of trailers (RFC 893 [14]) when using the ARP protocol. A /// value of false indicates that the client should not attempt to use trailers. A value of true means that the client should attempt to /// use trailers. /// public bool Trailers => GetParam(DHCP_OPTION_ID.OPTION_TRAILERS); /// This option specifies the default TTL that the client should use when sending TCP segments. The minimum value is 1. public byte TTL => GetParam(DHCP_OPTION_ID.OPTION_TTL); /// /// /// This option is used by some DHCP clients to identify the vendor type and possibly the configuration of a DHCP client. The information /// is a string of bytes whose contents are specific to the vendor and are not specified in a standard. To see what vendor class /// identifier clients are sending, you can write the following in your DHCP server configuration file: /// /// set vendor-string = option vendor-class-identifier; /// /// This will result in all entries in the DHCP server lease database file for clients that sent vendor-class-identifier options having a /// set statement that looks something like this: /// /// set vendor-string = "SUNW.Ultra-5_10"; /// /// The vendor-class-identifier option is normally used by the DHCP server to determine the options that are returned in the /// vendor-encapsulated-options option. Please see the VENDOR ENCAPSULATED OPTIONS section later in this manual page for further information. /// /// public string VendorSpecInfo => GetParam(DHCP_OPTION_ID.OPTION_VENDOR_SPEC_INFO); /// /// This option specifies a list of systems that are running the X Window System Display Manager and are available to the client. /// Addresses should be listed in order of preference. /// public IPAddress[] XwindowDisplayManager => ToIP(GetParam(DHCP_OPTION_ID.OPTION_XWINDOW_DISPLAY_MANAGER)); /// /// This option specifies a list of X Window System Font servers available to the client. Servers should be listed in order of preference. /// public IPAddress[] XwindowFontServer => ToIP(GetParam(DHCP_OPTION_ID.OPTION_XWINDOW_FONT_SERVER)); internal static NetworkInterface CurrentAdapter => NetworkInterface.GetAllNetworkInterfaces(). Where(i => i.NetworkInterfaceType == NetworkInterfaceType.Ethernet && i.OperationalStatus == OperationalStatus.Up && i.Supports(NetworkInterfaceComponent.IPv4) && (i.GetIPProperties()?.GetIPv4Properties().IsDhcpEnabled ?? false)). FirstOrDefault(); internal string Adapter => SelectedAdapterId ?? CurrentAdapter?.Id; /// Gets the original subnet mask. /// The retrieved subnet mask. public IPAddress GetOriginalSubnetMask() { DhcpGetOriginalSubnetMask(Adapter, out DHCP_IP_ADDRESS mask); return new(mask.value); } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { ClearListeners(); closing.Set(); hThread.Close(); } /// Gets the current DHCP servers for this client. /// A sequence of addresses of DHCP servers. public IEnumerable GetDhcpServers() => NetworkInterface.GetAllNetworkInterfaces().Where(i => i.OperationalStatus == OperationalStatus.Up).SelectMany(i => i.GetIPProperties().DhcpServerAddresses).Distinct(); /// The DhcpRemoveDNSRegistrations function removes all DHCP-initiated DNS registrations for the client. public void RemoveDNSRegistrations() => DhcpRemoveDNSRegistrations().ThrowIfFailed(); private void ClearListeners() { foreach (KeyValuePair kv in paramChgEvents) { DhcpDeRegisterParamChange(0, default, kv.Key); } paramChgEvents.Clear(); } private T GetParam(DHCP_OPTION_ID optionId) { using SafeCoTaskMemHandle pClassIdData = new(ClassId); using SafeCoTaskMemStruct pClass = (DHCPCAPI_CLASSID?)(ClassId is null ? null : new DHCPCAPI_CLASSID() { nBytesData = (uint)ClassId.Length, Data = pClassIdData }); DHCPCAPI_PARAMS_ARRAY sendParams = new(); DHCPCAPI_PARAMS_ARRAY reqParams = DHCPCAPI_PARAMS_ARRAY.Make(out SafeNativeArray pparam, optionId); uint sz = 0; string adapter = Adapter; DhcpRequestParams(DHCPCAPI_REQUEST.DHCPCAPI_REQUEST_SYNCHRONOUS, default, adapter, pClass, sendParams, reqParams, IntPtr.Zero, ref sz, null).ThrowUnless(Win32Error.ERROR_MORE_DATA); using SafeCoTaskMemHandle buffer = new(sz); Guid appId = Guid.NewGuid(); DhcpRequestParams(DHCPCAPI_REQUEST.DHCPCAPI_REQUEST_SYNCHRONOUS, default, adapter, pClass, sendParams, reqParams, buffer, ref sz, appId.ToString("N")).ThrowIfFailed(); if (sz == 0) return default; DHCPAPI_PARAMS p = pparam[0]; if (typeof(T).IsArray) { Type elemType = typeof(T).GetElementType(); System.Diagnostics.Debug.WriteLine($"Array: type={elemType.Name}, elemSz={InteropExtensions.SizeOf(elemType)}, memSz={sz}"); return (T)(object)p.Data.ToArray(elemType, sz / InteropExtensions.SizeOf(elemType), 0, sz); } else { System.Diagnostics.Debug.WriteLine(typeof(T) == typeof(string) ? $"String: memSz={sz}" : $"Value: type={typeof(T).Name}, sz={InteropExtensions.SizeOf()}, memSz={sz}"); return p.Data.Convert(p.nBytesData, CharSet.Ansi); } } private static uint ThreadProc(IntPtr hgc) { var c = (DhcpClient)GCHandle.FromIntPtr(hgc).Target; HEVENT[] hevts; RebuildList(); do { WAIT_STATUS state = WaitForMultipleObjects(hevts, false, INFINITE); if (state == (WAIT_STATUS)c.paramChgEvents.Count) { break; } else if (state == (WAIT_STATUS)c.paramChgEvents.Count + 1) { RebuildList(); } else { if (hevts.Length > (int)state && c.paramChgEvents.TryGetValue(hevts[(int)state], out var id)) c.ParamChanged?.Invoke(id); } } while (true); return 0; void RebuildList() => hevts = c.paramChgEvents.Keys.Concat(new HEVENT[] { c.closing, c.updateList }).ToArray(); } private IPAddress[] ToIP(uint[] ips) => ips is null ? new IPAddress[0] : Array.ConvertAll(ips, i => new IPAddress(i)); internal class DhcpInit { public readonly uint DhcpVersion, DhcpV6Version; public DhcpInit() { DhcpCApiInitialize(out DhcpVersion).ThrowIfFailed(); Dhcpv6CApiInitialize(out DhcpV6Version); } ~DhcpInit() { Dhcpv6CApiCleanup(); DhcpCApiCleanup(); } } }