├── .gitattributes
├── .gitignore
├── COPYING
├── README.md
└── src
├── Dhcp.Callout
├── AddressDeleteControlCodes.cs
├── CalloutConsumerSupportFlags.cs
├── CalloutControlCodes.cs
├── Dhcp.Callout.csproj
├── DhcpServerCalloutConsumerProxy.cs
├── IAddressDelete.cs
├── IAddressOffer.cs
├── ICalloutConsumer.cs
├── IDeleteClient.cs
├── IHandleOptions.cs
├── INewPacket.cs
├── IPacketDrop.cs
├── IPacketSend.cs
├── Native
│ ├── .editorconfig
│ ├── CalloutNative.cs
│ ├── DHCP_CALLOUT_TABLE.cs
│ └── Win32Api.cs
├── OfferAddressControlCodes.cs
├── OfferAddressTypes.cs
└── PacketDropControlCodes.cs
├── Dhcp.sln
├── Dhcp
├── .editorconfig
├── BitHelper.cs
├── Dhcp.csproj
├── DhcpServer.cs
├── DhcpServerApiProtocol.cs
├── DhcpServerAuditLog.cs
├── DhcpServerBindingElement.cs
├── DhcpServerBindingElementCollection.cs
├── DhcpServerClass.cs
├── DhcpServerClassCollection.cs
├── DhcpServerClient.cs
├── DhcpServerClientAddressStates.cs
├── DhcpServerClientCollection.cs
├── DhcpServerClientDnsStates.cs
├── DhcpServerClientNameProtectionStates.cs
├── DhcpServerClientQuarantineStatuses.cs
├── DhcpServerClientTypes.cs
├── DhcpServerConfiguration.cs
├── DhcpServerDnsSettings.cs
├── DhcpServerException.cs
├── DhcpServerFailoverMode.cs
├── DhcpServerFailoverRelationship.cs
├── DhcpServerFailoverRelationshipCollection.cs
├── DhcpServerFailoverServerType.cs
├── DhcpServerFailoverState.cs
├── DhcpServerHardwareAddress.cs
├── DhcpServerHardwareType.cs
├── DhcpServerHost.cs
├── DhcpServerIpAddress.cs
├── DhcpServerIpMask.cs
├── DhcpServerIpRange.cs
├── DhcpServerIpRangeType.cs
├── DhcpServerMessageTypes.cs
├── DhcpServerOption.cs
├── DhcpServerOptionCollection.cs
├── DhcpServerOptionElement.cs
├── DhcpServerOptionElementType.cs
├── DhcpServerOptionIdTypeAttribute.cs
├── DhcpServerOptionIdTypes.cs
├── DhcpServerOptionIds.cs
├── DhcpServerOptionValue.cs
├── DhcpServerPacket.cs
├── DhcpServerPacketFlags.cs
├── DhcpServerPacketMessageTypes.cs
├── DhcpServerPacketOption.cs
├── DhcpServerPacketOptions.cs
├── DhcpServerPacketWritable.cs
├── DhcpServerScope.cs
├── DhcpServerScopeClientCollection.cs
├── DhcpServerScopeCollection.cs
├── DhcpServerScopeExcludedIpRangeCollection.cs
├── DhcpServerScopeFailoverStatistics.cs
├── DhcpServerScopeOptionValueCollection.cs
├── DhcpServerScopeReservation.cs
├── DhcpServerScopeReservationCollection.cs
├── DhcpServerScopeReservationOptionValueCollection.cs
├── DhcpServerScopeState.cs
├── DhcpServerSpecificStrings.cs
├── DhcpServerVersions.cs
├── IDhcpServer.cs
├── IDhcpServerAuditLog.cs
├── IDhcpServerBindingElement.cs
├── IDhcpServerBindingElementCollection.cs
├── IDhcpServerClass.cs
├── IDhcpServerClassCollection.cs
├── IDhcpServerClient.cs
├── IDhcpServerClientCollection.cs
├── IDhcpServerConfiguration.cs
├── IDhcpServerDnsSettings.cs
├── IDhcpServerFailoverRelationship.cs
├── IDhcpServerFailoverRelationshipCollection.cs
├── IDhcpServerHost.cs
├── IDhcpServerOption.cs
├── IDhcpServerOptionCollection.cs
├── IDhcpServerOptionElement.cs
├── IDhcpServerOptionValue.cs
├── IDhcpServerPacket.cs
├── IDhcpServerPacketRaw.cs
├── IDhcpServerPacketRawWritable.cs
├── IDhcpServerPacketWritable.cs
├── IDhcpServerScope.cs
├── IDhcpServerScopeClientCollection.cs
├── IDhcpServerScopeCollection.cs
├── IDhcpServerScopeExcludedIpRangeCollection.cs
├── IDhcpServerScopeFailoverStatistics.cs
├── IDhcpServerScopeOptionValueCollection.cs
├── IDhcpServerScopeReservation.cs
├── IDhcpServerScopeReservationCollection.cs
├── IDhcpServerScopeReservationOptionValueCollection.cs
├── IDhcpServerSpecificStrings.cs
├── Native
│ ├── .editorconfig
│ ├── Api.cs
│ ├── Constants.cs
│ ├── DATE_TIME.cs
│ ├── DHCPDS_SERVER.cs
│ ├── DHCPDS_SERVERS.cs
│ ├── DHCP_ALL_OPTIONS.cs
│ ├── DHCP_ALL_OPTION_VALUES.cs
│ ├── DHCP_ALL_OPTION_VALUE_ITEM.cs
│ ├── DHCP_BINARY_DATA.cs
│ ├── DHCP_BIND_ELEMENT.cs
│ ├── DHCP_BIND_ELEMENT_ARRAY.cs
│ ├── DHCP_BOOTP_IP_RANGE.cs
│ ├── DHCP_CLASS_INFO.cs
│ ├── DHCP_CLASS_INFO_ARRAY.cs
│ ├── DHCP_CLIENT_INFO.cs
│ ├── DHCP_CLIENT_INFO_ARRAY.cs
│ ├── DHCP_CLIENT_INFO_ARRAY_V4.cs
│ ├── DHCP_CLIENT_INFO_ARRAY_V5.cs
│ ├── DHCP_CLIENT_INFO_ARRAY_VQ.cs
│ ├── DHCP_CLIENT_INFO_V4.cs
│ ├── DHCP_CLIENT_INFO_V5.cs
│ ├── DHCP_CLIENT_INFO_VQ.cs
│ ├── DHCP_CLIENT_TYPE.cs
│ ├── DHCP_CLIENT_UID.cs
│ ├── DHCP_FAILOVER_MODE.cs
│ ├── DHCP_FAILOVER_RELATIONSHIP.cs
│ ├── DHCP_FAILOVER_RELATIONSHIP_ARRAY.cs
│ ├── DHCP_FAILOVER_RELATIONSHIP_SET_FLAGS.cs
│ ├── DHCP_FAILOVER_SERVER.cs
│ ├── DHCP_FAILOVER_STATISTICS.cs
│ ├── DHCP_FORCE_FLAG.cs
│ ├── DHCP_HOST_INFO.cs
│ ├── DHCP_IP_ADDRESS.cs
│ ├── DHCP_IP_ARRAY.cs
│ ├── DHCP_IP_CLUSTER.cs
│ ├── DHCP_IP_MASK.cs
│ ├── DHCP_IP_RANGE.cs
│ ├── DHCP_IP_RESERVATION.cs
│ ├── DHCP_IP_RESERVATION_V4.cs
│ ├── DHCP_OPTION.cs
│ ├── DHCP_OPTION_ARRAY.cs
│ ├── DHCP_OPTION_DATA.cs
│ ├── DHCP_OPTION_DATA_ELEMENT.cs
│ ├── DHCP_OPTION_DATA_TYPE.cs
│ ├── DHCP_OPTION_SCOPE_INFO.cs
│ ├── DHCP_OPTION_SCOPE_TYPE.cs
│ ├── DHCP_OPTION_TYPE.cs
│ ├── DHCP_OPTION_VALUE.cs
│ ├── DHCP_OPTION_VALUE_ARRAY.cs
│ ├── DHCP_SEARCH_INFO.cs
│ ├── DHCP_SEARCH_INFO_TYPE.cs
│ ├── DHCP_SERVER_CONFIG_INFO.cs
│ ├── DHCP_SERVER_OPTIONS.cs
│ ├── DHCP_SERVER_SPECIFIC_STRINGS.cs
│ ├── DHCP_SUBNET_ELEMENT_DATA.cs
│ ├── DHCP_SUBNET_ELEMENT_DATA_V5.cs
│ ├── DHCP_SUBNET_ELEMENT_INFO_ARRAY.cs
│ ├── DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5.cs
│ ├── DHCP_SUBNET_ELEMENT_TYPE.cs
│ ├── DHCP_SUBNET_INFO.cs
│ ├── DHCP_SUBNET_INFO_VQ.cs
│ ├── DHCP_SUBNET_STATE.cs
│ ├── DHCP_VENDOR_OPTION.cs
│ ├── DhcpErrorDescriptionAttribute.cs
│ ├── DhcpErrors.cs
│ ├── FSM_STATE.cs
│ └── QuarantineStatus.cs
└── UnmanagedDisposer.cs
├── DhcpCalloutDemo
├── DemoDhcpCalloutConsumer.cs
├── DhcpCalloutDemo.csproj
└── Properties
│ └── AssemblyInfo.cs
├── DhcpDemo
├── DhcpDemo.csproj
└── Program.cs
├── DhcpWritableDemo
├── DhcpWritableDemo.csproj
└── Program.cs
├── DllExport.bat
└── DllExport_Configure.bat
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015 Gary Sharp
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/AddressDeleteControlCodes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | public enum AddressDeleteControlCodes : int
4 | {
5 | ///
6 | /// The address attempted to be offered, as provided in LeaseAddress, is already in use on the network.
7 | ///
8 | Conflict = 0x20000001,
9 | ///
10 | /// The packet was a DECLINE message for the address specified in LeaseAddress.
11 | ///
12 | Decline = 0x20000002,
13 | ///
14 | /// The packet was a RELEASE message for the address specified in LeaseAddress.
15 | ///
16 | Release = 0x20000003,
17 | ///
18 | /// The packet was a REQUEST message for the address specified in LeaseAddress, and the request was declined by Microsoft DHCP Server.
19 | ///
20 | Nacked = 0x20000004,
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/CalloutConsumerSupportFlags.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | internal enum CalloutConsumerSupportFlags
4 | {
5 | Control = 1,
6 | NewPacket = 2,
7 | PacketDrop = 4,
8 | PacketSend = 16,
9 | AddressDelete = 32,
10 | AddressOffer = 64,
11 | HandleOptions = 128,
12 | DeleteClient = 256,
13 |
14 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363343.aspx
15 | // Reserved for future use:
16 | Extension = 512,
17 | Reserved = 1024,
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/CalloutControlCodes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | public enum CalloutControlCodes : int
4 | {
5 | Start = 1,
6 | Stop = 2,
7 | Pause = 3,
8 | Continue = 4,
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/Dhcp.Callout.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net40;net45;net46
5 | DhcpServerApi.Callout
6 | DhcpServerApi.Callout
7 | Gary Sharp
8 |
9 | Dhcp Server Callout API
10 | Integrate with Microsoft DHCP Server Callout
11 | Copyright 2019 Gary Sharp
12 | https://github.com/garysharp/DhcpServerApi
13 | https://github.com/garysharp/DhcpServerApi
14 | git
15 | true
16 | true
17 | portable
18 | true
19 | MIT
20 | DHCP Win32 Windows Callout
21 | preview
22 | AnyCPU;x64
23 | x64
24 | true
25 |
26 |
27 | D1942B48-9F45-4CD3-BD50-F5CCC3DC9C38
28 | DllExport.dll
29 | System.Runtime.InteropServices
30 | true
31 | x64
32 | 1
33 | false
34 | true
35 | false
36 | 30000
37 | 2
38 | 1.0.3-alpha
39 |
40 |
41 |
42 | 1.0.0-beta2-18618-05
43 | all
44 | runtime; build; native; contentfiles; analyzers; buildtransitive
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | $(SolutionDir)packages\DllExport.1.6.2\gcache\metalib\$(DllExportNamespace)\$(DllExportMetaLibName)
53 | False
54 | False
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/IAddressDelete.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | ///
4 | /// If inherited by classes the library registers
5 | /// the consumer for the hook (DhcpAddressOfferHook)
6 | ///
7 | ///
8 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363269.aspx
9 | ///
10 | public interface IAddressDelete
11 | {
12 | ///
13 | /// The function is called by Microsoft DHCP Server when one of the events defined by Control Codes.
14 | /// The function should not block.
15 | ///
16 | ///
17 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363269.aspx
18 | ///
19 | /// The packet being processed.
20 | /// Specifies the reason for the event.
21 | /// The IP address of the socket on which the packet was received.
22 | /// The IP address relevant to this event. The meaning varies based on the ControlCode.
23 | /// Set to to prevent propagation of this event to subsequent consumers of this hook.
24 | void AddressDelete(IDhcpServerPacket Packet, AddressDeleteControlCodes ControlCode, DhcpServerIpAddress ServerAddress, DhcpServerIpAddress LeaseAddress, ref bool StopPropagation);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/IAddressOffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Callout
4 | {
5 | ///
6 | /// If inherited by classes the library registers
7 | /// the consumer for the hook (DhcpAddressOfferHook)
8 | ///
9 | ///
10 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363269.aspx
11 | ///
12 | public interface IAddressOffer
13 | {
14 | ///
15 | /// The function is called by Microsoft DHCP Server directly before Microsoft DHCP Server
16 | /// sends an acknowledgment (ACK) to a DHCP REQUEST message. The function should not block.
17 | ///
18 | ///
19 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363269.aspx
20 | ///
21 | /// The packet being processed.
22 | /// The type of lease being approved
23 | /// The IP address of the socket on which the packet was received.
24 | /// The IP address being handed out in the lease.
25 | /// Specifies whether the address is DHCP or BOOTP.
26 | /// Lease duration being passed.
27 | /// Set to to prevent propagation of this event to subsequent consumers of this hook.
28 | void AddressOffer(IDhcpServerPacket Packet,
29 | OfferAddressControlCodes ControlCode,
30 | DhcpServerIpAddress ServerAddress,
31 | DhcpServerIpAddress LeaseAddress,
32 | OfferAddressTypes AddressType,
33 | TimeSpan LeaseTime,
34 | ref bool StopPropagation);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/ICalloutConsumer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Callout
4 | {
5 | ///
6 | /// Interface inherited by DHCP Server Callout API Consumers. The library uses this interface to locate consumers.
7 | ///
8 | public interface ICalloutConsumer : IDisposable
9 | {
10 | ///
11 | /// Called during DhcpServerCalloutEntry.
12 | ///
13 | ///
14 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363300.aspx
15 | ///
16 | /// Version of the DHCP Server API
17 | void Initialize(int ApiVersion);
18 |
19 | ///
20 | /// The Control function (DhcpControlHook) is called whenever the DHCP Server service is
21 | /// started, stopped, paused or continued. This function should not block.
22 | ///
23 | ///
24 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363276(v=vs.85).aspx
25 | ///
26 | /// Specifies the control event that triggered the notification.
27 | void Control(CalloutControlCodes ControlCode);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/IDeleteClient.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | ///
4 | /// If inherited by classes the library registers
5 | /// the consumer for the hook (DhcpDeleteClientHook)
6 | ///
7 | ///
8 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363279.aspx
9 | ///
10 | public interface IDeleteClient
11 | {
12 |
13 | ///
14 | /// The function is called by Microsoft DHCP Server directly before a client lease is deleted from the active leases database.
15 | /// The function should not block.
16 | ///
17 | ///
18 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363279.aspx
19 | ///
20 | /// The IP address of the client lease being deleted.
21 | /// The Hardware Address of the client, often referred to as the MAC Address.
22 | /// Set to to prevent propagation of this event to subsequent consumers of this hook.
23 | void DeleteClient(DhcpServerIpAddress LeaseAddress, DhcpServerHardwareAddress LeaseHardwareAddress, ref bool StopPropagation);
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/IHandleOptions.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | ///
4 | /// If inherited by classes the library registers
5 | /// the consumer for the hook (DhcpHandleOptionsHook)
6 | ///
7 | ///
8 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363291.aspx
9 | ///
10 | public interface IHandleOptions
11 | {
12 |
13 | ///
14 | /// The function enables third-party DLLs to obtain commonly used options from a DHCP packet, avoiding the need to process the entire DHCP packet.
15 | /// The DhcpHandleOptionsHook function should not block. Will only be called with the API Version is '0' (as the ServerOptions could change if the version is incremented).
16 | ///
17 | ///
18 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363291.aspx
19 | ///
20 | /// The packet being processed.
21 | /// The information parsed from the packet by Microsoft DHCP Server, and provided as the collection of commonly used server options.
22 | /// Set to to prevent propagation of this event to subsequent consumers of this hook.
23 | void HandleOptions(IDhcpServerPacket Packet, DhcpServerPacketOptions ServerOptions, ref bool StopPropagation);
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/INewPacket.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | ///
4 | /// If inherited by classes the library registers
5 | /// the consumer for the hook (DhcpNewPktHook)
6 | ///
7 | ///
8 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363292.aspx
9 | ///
10 | public interface INewPacket
11 | {
12 |
13 | ///
14 | /// The NewPacket (DhcpNewPktHook) function is called soon after the DHCP Server receives a
15 | /// packet that it attempts to process. This function is in the
16 | /// critical path of server execution and should return very fast, as
17 | /// otherwise server performance will be impacted.
18 | ///
19 | ///
20 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363292.aspx
21 | ///
22 | /// The packet being processed.
23 | /// The IP address of the socket on which the packet was received.
24 | /// Flag identifying whether Microsoft DHCP Server should continue processing the packet. Set to to indicate processing should proceed. Set to to have Microsoft DHCP Server drop the packet.
25 | /// Set to to prevent propagation of this event to subsequent consumers of this hook. If the ProcessIt argument is set to false then propagation stops regardless.
26 | void NewPacket(IDhcpServerPacketWritable Packet, DhcpServerIpAddress ServerAddress, ref bool ProcessIt, ref bool StopPropagation);
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/IPacketDrop.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | ///
4 | /// If inherited by classes the library registers
5 | /// the consumer for the hook (DhcpPktDropHook)
6 | ///
7 | ///
8 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363293.aspx
9 | ///
10 | public interface IPacketDrop
11 | {
12 |
13 | ///
14 | /// The function is called by Microsoft DHCP Server when a DHCP packet is dropped, or a packet is completely processed.
15 | /// If a packet is dropped, the function is called twice as it is called once again to note that the packet has been completely processed.
16 | /// The implementor should be prepared to handle this hook multiple times for a packet.
17 | /// The function should not block.
18 | ///
19 | ///
20 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363293.aspx
21 | ///
22 | /// The dropped packet.
23 | /// Control code that specifies the reason for dropping.
24 | /// The IP address of the socket on which the packet was received.
25 | /// Set to to prevent propagation of this event to subsequent consumers of this hook.
26 | void PacketDrop(IDhcpServerPacket Packet, PacketDropControlCodes ControlCode, DhcpServerIpAddress ServerAddress, ref bool StopPropagation);
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/IPacketSend.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | ///
4 | /// If inherited by classes the library registers
5 | /// the consumer for the hook (DhcpPktSendHook)
6 | ///
7 | ///
8 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363294.aspx
9 | ///
10 | public interface IPacketSend
11 | {
12 |
13 | ///
14 | /// The function is called by Microsoft DHCP Server directly before Microsoft DHCP Server sends a response to a client.
15 | /// Registering for this notification enables third-party developers to alter the response of the Microsoft DHCP Server by manipulation of the packet pointers.
16 | /// Manipulation of packet pointers is not currently supported by the DhcpServerAPI wrapper.
17 | /// The function should not block.
18 | ///
19 | ///
20 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363294.aspx
21 | ///
22 | /// The packet to be sent by Microsoft DHCP Server.
23 | /// The IP address of the socket on which the packet was received.
24 | /// Set to to prevent propagation of this event to subsequent consumers of this hook.
25 | void PacketSend(IDhcpServerPacketWritable Packet, DhcpServerIpAddress ServerAddress, ref bool StopPropagation);
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/Native/DHCP_CALLOUT_TABLE.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Callout.Native
5 | {
6 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
7 | internal delegate uint DhcpControlHookUnsafeDelegate(uint dwControlCode, IntPtr lpReserved);
8 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
9 | internal delegate uint DhcpNewPktHookUnsafeDelegate(ref IntPtr Packet, ref uint PacketSize, uint IpAddress, IntPtr Reserved, IntPtr PktContext, out bool ProcessIt);
10 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
11 | internal delegate uint DhcpPktDropHookUnsafeDelegate(ref IntPtr Packet, ref uint PacketSize, uint ControlCode, uint IpAddress, IntPtr Reserved, IntPtr PktContext);
12 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
13 | internal delegate uint DhcpPktSendHookUnsafeDelegate(ref IntPtr Packet, ref uint PacketSize, uint ControlCode, uint IpAddress, IntPtr Reserved, IntPtr PktContext);
14 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
15 | internal delegate uint DhcpAddressDelHookUnsafeDelegate(IntPtr Packet, uint PacketSize, uint ControlCode, uint IpAddress, uint AltAddress, IntPtr Reserved, IntPtr PktContext);
16 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
17 | internal delegate uint DhcpAddressOfferHookUnsafeDelegate(IntPtr Packet, uint PacketSize, uint ControlCode, uint IpAddress, uint AltAddress, uint AddrType, uint LeaseTime, IntPtr Reserved, IntPtr PktContext);
18 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
19 | internal delegate uint DhcpHandleOptionsHookUnsafeDelegate(IntPtr Packet, uint PacketSize, IntPtr Reserved, IntPtr PktContext, IntPtr ServerOptions);
20 | [UnmanagedFunctionPointer(CallingConvention.StdCall)]
21 | internal delegate uint DhcpDeleteClientHookUnsafeDelegate(uint IpAddress, IntPtr HwAddress, uint HwAddressLength, uint Reserved, uint ClientType);
22 |
23 | internal struct DHCP_CALLOUT_TABLE
24 | {
25 | public DhcpControlHookUnsafeDelegate DhcpControlHook;
26 | public DhcpNewPktHookUnsafeDelegate DhcpNewPktHook;
27 | public DhcpPktDropHookUnsafeDelegate DhcpPktDropHook;
28 | public DhcpPktSendHookUnsafeDelegate DhcpPktSendHook;
29 | public DhcpAddressDelHookUnsafeDelegate DhcpAddressDelHook;
30 | public DhcpAddressOfferHookUnsafeDelegate DhcpAddressOfferHook;
31 | public DhcpHandleOptionsHookUnsafeDelegate DhcpHandleOptionsHook;
32 | public DhcpDeleteClientHookUnsafeDelegate DhcpDeleteClientHook;
33 | public IntPtr DhcpExtensionHook;
34 | public IntPtr DhcpReservedHook;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/OfferAddressControlCodes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | public enum OfferAddressControlCodes : int
4 | {
5 | ///
6 | /// Specifies a new lease is being approved
7 | ///
8 | GiveNewAddress = 0x30000001,
9 | ///
10 | /// Specifies the renewal of an existing lease
11 | ///
12 | GiveOldAddress = 0x30000002,
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/OfferAddressTypes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | public enum OfferAddressTypes : uint
4 | {
5 | ///
6 | /// The address is a BOOTP-served address.
7 | ///
8 | BOOTP = 0x30000003U,
9 | ///
10 | /// The address is a DHCP-served address.
11 | ///
12 | DHCP = 0x30000004U,
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Dhcp.Callout/PacketDropControlCodes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Callout
2 | {
3 | public enum PacketDropControlCodes : int
4 | {
5 | ///
6 | /// The packet is a duplicate of another received by the DHCP server.
7 | ///
8 | Duplicate = 0x00000001,
9 | ///
10 | /// There is not enough memory available to process the packet.
11 | ///
12 | NoMemory = 0x00000002,
13 | ///
14 | /// An unexpected internal error has occurred.
15 | ///
16 | InternalError = 0x00000003,
17 | ///
18 | /// The packet is too old to process.
19 | ///
20 | Timeout = 0x00000004,
21 | ///
22 | /// The server is not authorized to process this packet.
23 | ///
24 | Unauthorized = 0x00000005,
25 | ///
26 | /// The server is paused.
27 | ///
28 | Paused = 0x00000006,
29 | ///
30 | /// There are no subnets configured, therefore there is no point in processing the packet.
31 | ///
32 | NoSubnets = 0x00000007,
33 | ///
34 | /// The packet is invalid, or it was received on an invalid socket.
35 | ///
36 | Invalid = 0x00000008,
37 | ///
38 | /// The packet was sent to the wrong DHCP Server.
39 | ///
40 | WrongServer = 0x00000009,
41 | ///
42 | /// There is no address to offer.
43 | ///
44 | NoAddress = 0x0000000A,
45 | ///
46 | /// The packet has been processed.
47 | ///
48 | Processed = 0x0000000B,
49 | ///
50 | /// An unknown error has occurred.
51 | ///
52 | GenericError = 0x00000100,
53 | }
54 | }
--------------------------------------------------------------------------------
/src/Dhcp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.28803.202
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dhcp", "Dhcp\Dhcp.csproj", "{3F2E7427-2ADC-43D4-A92B-1F6ABAE23BED}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dhcp.Callout", "Dhcp.Callout\Dhcp.Callout.csproj", "{4CB9D6C3-67CE-478E-BA98-EB2C2C927282}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DhcpDemo", "DhcpDemo\DhcpDemo.csproj", "{944CD562-FBC1-4E3A-B129-B4ED26FCB173}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DhcpCalloutDemo", "DhcpCalloutDemo\DhcpCalloutDemo.csproj", "{AB9AD770-6EF7-4AF0-91F5-11640E296BA8}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DhcpWritableDemo", "DhcpWritableDemo\DhcpWritableDemo.csproj", "{B49D9FF1-D3C9-49E5-AC3A-E804F01F6541}"
15 | EndProject
16 | Global
17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
18 | Debug|Any CPU = Debug|Any CPU
19 | Release|Any CPU = Release|Any CPU
20 | EndGlobalSection
21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
22 | {3F2E7427-2ADC-43D4-A92B-1F6ABAE23BED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {3F2E7427-2ADC-43D4-A92B-1F6ABAE23BED}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {3F2E7427-2ADC-43D4-A92B-1F6ABAE23BED}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {3F2E7427-2ADC-43D4-A92B-1F6ABAE23BED}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {944CD562-FBC1-4E3A-B129-B4ED26FCB173}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {944CD562-FBC1-4E3A-B129-B4ED26FCB173}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {944CD562-FBC1-4E3A-B129-B4ED26FCB173}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {944CD562-FBC1-4E3A-B129-B4ED26FCB173}.Release|Any CPU.Build.0 = Release|Any CPU
30 | {AB9AD770-6EF7-4AF0-91F5-11640E296BA8}.Debug|Any CPU.ActiveCfg = Debug|x64
31 | {AB9AD770-6EF7-4AF0-91F5-11640E296BA8}.Debug|Any CPU.Build.0 = Debug|x64
32 | {AB9AD770-6EF7-4AF0-91F5-11640E296BA8}.Release|Any CPU.ActiveCfg = Release|x64
33 | {AB9AD770-6EF7-4AF0-91F5-11640E296BA8}.Release|Any CPU.Build.0 = Release|x64
34 | {B49D9FF1-D3C9-49E5-AC3A-E804F01F6541}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35 | {B49D9FF1-D3C9-49E5-AC3A-E804F01F6541}.Debug|Any CPU.Build.0 = Debug|Any CPU
36 | {B49D9FF1-D3C9-49E5-AC3A-E804F01F6541}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 | {B49D9FF1-D3C9-49E5-AC3A-E804F01F6541}.Release|Any CPU.Build.0 = Release|Any CPU
38 | {4CB9D6C3-67CE-478E-BA98-EB2C2C927282}.Debug|Any CPU.ActiveCfg = Debug|x64
39 | {4CB9D6C3-67CE-478E-BA98-EB2C2C927282}.Debug|Any CPU.Build.0 = Debug|x64
40 | {4CB9D6C3-67CE-478E-BA98-EB2C2C927282}.Release|Any CPU.ActiveCfg = Release|x64
41 | {4CB9D6C3-67CE-478E-BA98-EB2C2C927282}.Release|Any CPU.Build.0 = Release|x64
42 | EndGlobalSection
43 | GlobalSection(SolutionProperties) = preSolution
44 | HideSolutionNode = FALSE
45 | EndGlobalSection
46 | GlobalSection(ExtensibilityGlobals) = postSolution
47 | SolutionGuid = {029977BA-CD42-4F3D-80B0-B64CD126357D}
48 | BuildVersion_StartDate = 2000/1/1
49 | EndGlobalSection
50 | EndGlobal
51 |
--------------------------------------------------------------------------------
/src/Dhcp/Dhcp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net40;net45;net46;netstandard2.0
5 | DhcpServerApi
6 | DhcpServerApi
7 | Gary Sharp
8 |
9 | Dhcp Server API
10 | Query and Manage Microsoft DHCP Server
11 | Copyright 2019 Gary Sharp
12 | https://github.com/garysharp/DhcpServerApi
13 | https://github.com/garysharp/DhcpServerApi
14 | git
15 | true
16 | true
17 | snupkg
18 | portable
19 | true
20 | MIT
21 | DHCP Win32 Windows
22 | preview
23 | AnyCPU;x64
24 | AnyCPU
25 | true
26 | 1.0.3-alpha
27 |
28 |
29 |
30 | 1.0.0-beta2-18618-05
31 | all
32 | runtime; build; native; contentfiles; analyzers; buildtransitive
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerApiProtocol.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | [Flags]
6 | public enum DhcpServerApiProtocol
7 | {
8 | ///
9 | /// TCP/IP can be used for DHCP API RPC calls. (DHCP_SERVER_USE_RPC_OVER_TCPIP)
10 | ///
11 | RpcOverTcpIp = 0x1,
12 | ///
13 | /// Named pipes can be used for DHCP API RPC calls. (DHCP_SERVER_USE_RPC_OVER_NP)
14 | ///
15 | RpcOverNamedPipes = 0x2,
16 | ///
17 | /// Local Procedure Call (LPC) can be used for local DHCP API RPC calls. (DHCP_SERVER_USE_RPC_OVER_LPC)
18 | ///
19 | RpcOverLocalProcedureCall = 0x4,
20 | ///
21 | /// The DHCP server supports all of the preceding protocols. (DHCP_SERVER_USE_RPC_OVER_ALL)
22 | ///
23 | RpcOverAll = 0x7
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerAuditLog.cs:
--------------------------------------------------------------------------------
1 | using Dhcp.Native;
2 |
3 | namespace Dhcp
4 | {
5 | public class DhcpServerAuditLog : IDhcpServerAuditLog
6 | {
7 | ///
8 | /// The associated DHCP Server
9 | ///
10 | public IDhcpServer Server { get; }
11 |
12 | ///
13 | /// The directory where the audit log is stored as an absolute path within the file system.
14 | ///
15 | public string AuditLogDirectory { get; }
16 |
17 | ///
18 | /// The disk check interval for attempting to write the audit log to the specified file as the number of logged DHCP server events that should occur between checks.
19 | ///
20 | public int DiskCheckInterval { get; }
21 |
22 | ///
23 | /// The maximum log file size, in bytes.
24 | ///
25 | public int MaxLogFilesSize { get; }
26 |
27 | ///
28 | /// The minimum required disk space, in bytes, for audit log storage.
29 | ///
30 | public int MinSpaceOnDisk { get; }
31 |
32 | private DhcpServerAuditLog(DhcpServer server, string auditLogDir, int diskCheckInterval, int maxLogFilesSize, int minSpaceOnDisk)
33 | {
34 | Server = server;
35 | AuditLogDirectory = auditLogDir;
36 | DiskCheckInterval = diskCheckInterval;
37 | MaxLogFilesSize = maxLogFilesSize;
38 | MinSpaceOnDisk = minSpaceOnDisk;
39 | }
40 |
41 | internal static DhcpServerAuditLog GetAuditLog(DhcpServer server)
42 | {
43 | var result = Api.DhcpAuditLogGetParams(ServerIpAddress: server.Address,
44 | Flags: 0,
45 | AuditLogDir: out var auditLogDir,
46 | DiskCheckInterval: out var diskCheckInterval,
47 | MaxLogFilesSize: out var maxLogFilesSize,
48 | MinSpaceOnDisk: out var minSpaceOnDisk);
49 |
50 | if (result != DhcpErrors.SUCCESS)
51 | throw new DhcpServerException(nameof(Api.DhcpAuditLogGetParams), result);
52 |
53 | return new DhcpServerAuditLog(server, auditLogDir, diskCheckInterval, maxLogFilesSize, minSpaceOnDisk);
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerBindingElementCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerBindingElementCollection : IDhcpServerBindingElementCollection
7 | {
8 | public DhcpServer Server { get; }
9 | IDhcpServer IDhcpServerBindingElementCollection.Server => Server;
10 |
11 | internal DhcpServerBindingElementCollection(DhcpServer server)
12 | {
13 | Server = server;
14 | }
15 |
16 | public IEnumerator GetEnumerator()
17 | => DhcpServerBindingElement.GetBindingInfo(Server).GetEnumerator();
18 |
19 | IEnumerator IEnumerable.GetEnumerator()
20 | => GetEnumerator();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClassCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerClassCollection : IDhcpServerClassCollection
7 | {
8 | public DhcpServer Server { get; }
9 | IDhcpServer IDhcpServerClassCollection.Server => Server;
10 |
11 | internal DhcpServerClassCollection(DhcpServer server)
12 | {
13 | Server = server;
14 | }
15 |
16 | ///
17 | /// Queries the DHCP Server for the specified User or Vendor Class
18 | ///
19 | /// The name of the User or Vendor Class to retrieve
20 | /// A .
21 | public IDhcpServerClass GetClass(string name)
22 | => DhcpServerClass.GetClass(Server, name);
23 |
24 | public IEnumerator GetEnumerator()
25 | => DhcpServerClass.GetClasses(Server).GetEnumerator();
26 |
27 | IEnumerator IEnumerable.GetEnumerator()
28 | => GetEnumerator();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClientAddressStates.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerClientAddressStates
4 | {
5 | ///
6 | /// The DHCPv4 client is offered this IPv4 address.
7 | ///
8 | Offered = 0x00,
9 | ///
10 | /// The IPv4 address is active and has an active DHCPv4 client lease record.
11 | ///
12 | Active = 0x01,
13 | ///
14 | /// The IPv4 address request is declined by the DHCPv4 client; hence it is a bad IPv4 address.
15 | ///
16 | Declined = 0x02,
17 | ///
18 | /// The IPv4 address is in DOOMED state and is due to be deleted.
19 | ///
20 | Doomed = 0x03,
21 | ///
22 | /// The Address State is unknown or not supported by the server.
23 | ///
24 | Unknown = -1,
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClientCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerClientCollection : IDhcpServerClientCollection
7 | {
8 | public DhcpServer Server { get; }
9 | IDhcpServer IDhcpServerClientCollection.Server => Server;
10 |
11 | internal DhcpServerClientCollection(DhcpServer server)
12 | {
13 | Server = server;
14 | }
15 |
16 | public IEnumerator GetEnumerator()
17 | => DhcpServerClient.GetClients(Server).GetEnumerator();
18 |
19 | IEnumerator IEnumerable.GetEnumerator()
20 | => GetEnumerator();
21 |
22 | public void RemoveClient(IDhcpServerClient client)
23 | => client.Delete();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClientDnsStates.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | [Flags]
6 | public enum DhcpServerClientDnsStates
7 | {
8 | ///
9 | /// The DNS update for the DHCPv4 client lease record needs to be deleted from the DNS server when the lease is deleted.
10 | ///
11 | Cleanup = 0x01,
12 | ///
13 | /// The DNS update needs to be sent for both A and PTR resource records ([RFC1034] section 3.6).
14 | ///
15 | UpdateBothRecords = 0x02,
16 | ///
17 | /// The DNS update is not completed for the lease record.
18 | ///
19 | Unregistered = 0x04,
20 | ///
21 | /// The address lease is expired, but the DNS updates for the lease record have not been deleted from the DNS server.
22 | ///
23 | Deleted = 0x08,
24 | ///
25 | /// The DNS State is unknown or not supported by the server.
26 | ///
27 | Unknown = -1,
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClientNameProtectionStates.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | ///
4 | /// Client Name Protection States
5 | ///
6 | public enum DhcpServerClientNameProtectionStates
7 | {
8 | ///
9 | /// The address is leased to the DHCPv4 client without DHCID (sections 3 and 3.5 of [RFC4701]).
10 | ///
11 | NoDhcid = 0x00,
12 | ///
13 | /// The address is leased to the DHCPv4 client with DHCID as specified in section 3.5.3 of [RFC4701].
14 | ///
15 | NoClientIdOption = 0x01,
16 | ///
17 | /// The address is leased to the DHCPv4 client with DHCID as specified in section 3.5.2 of [RFC4701].
18 | ///
19 | DhcidWithClientIdOption = 0x02,
20 | ///
21 | /// The address is leased to the DHCPv4 client with DHCID as specified in section 3.5.1 of [RFC4701].
22 | ///
23 | DhcidWithDuid = 0x03,
24 | ///
25 | /// The Name Protection State is unknown or not supported by the server.
26 | ///
27 | Unknown = -1,
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClientQuarantineStatuses.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerClientQuarantineStatuses
4 | {
5 | ///
6 | /// The DHCP client is compliant with the health policies defined by the administrator and has normal access to the network.
7 | ///
8 | NoQuarantine = 0,
9 | ///
10 | /// The DHCP client is not compliant with the health policies defined by the administrator and is being quarantined with restricted access to the network.
11 | ///
12 | RestrictedAccess = 1,
13 | ///
14 | /// The DHCP client is not compliant with the health policies defined by the administrator and is being denied access to the network. The DHCP server does not grant an IP address lease to this client.
15 | ///
16 | DropPacket = 2,
17 | ///
18 | /// The DHCP client is not compliant with the health policies defined by the administrator and is being granted normal access to the network for a limited time.
19 | ///
20 | Probation = 3,
21 | ///
22 | /// The DHCP client is exempt from compliance with the health policies defined by the administrator and is granted normal access to the network.
23 | ///
24 | Exempt = 4,
25 | ///
26 | /// The DHCP client is put into the default quarantine state configured on the DHCP NAP server. When a network policy server (NPS) is unavailable, the DHCP client can be put in any of the states NOQUARANTINE, RESTRICTEDACCESS, or DROPPACKET, depending on the default setting on the DHCP NAP server.
27 | ///
28 | DefaultQuarantineState = 5,
29 | ///
30 | /// No quarantine.
31 | ///
32 | NoQuarantineInformation = 6
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerClientTypes.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | [Flags]
6 | public enum DhcpServerClientTypes
7 | {
8 | ///
9 | /// A client other than ones defined in this table.
10 | ///
11 | Unspecified = 0x00,
12 | ///
13 | /// Supports the DHCP protocol.
14 | ///
15 | Dhcp = 0x01,
16 | ///
17 | /// Supports the BOOTP protocol.
18 | ///
19 | Bootp = 0x02,
20 | ///
21 | /// Supports the DHCP and BOOTP protocols.
22 | ///
23 | DhcpAndBootp = 0x03,
24 | ///
25 | /// There is an IPv4 reservation created for the DHCPv4 client.
26 | ///
27 | Reservation = 0x04,
28 | ///
29 | /// Backward compatibility for manual addressing.
30 | ///
31 | None = 0x64,
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerConfiguration.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Dhcp.Native;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerConfiguration : IDhcpServerConfiguration
7 | {
8 | public IDhcpServer Server { get; }
9 |
10 | ///
11 | /// Specifies a set of bit flags that contain the RPC protocols supported by the DHCP server.
12 | ///
13 | public DhcpServerApiProtocol ApiProtocolSupport { get; }
14 |
15 | ///
16 | /// Unicode string that specifies the file name of the client lease JET database.
17 | ///
18 | public string DatabaseName { get; }
19 |
20 | ///
21 | /// Unicode string that specifies the absolute path to DatabaseName.
22 | ///
23 | public string DatabasePath { get; }
24 |
25 | ///
26 | /// Unicode string that specifies the absolute path and file name of the backup client lease JET database.
27 | ///
28 | public string BackupPath { get; }
29 |
30 | ///
31 | /// Specifies the interval between backups of the client lease database.
32 | ///
33 | public TimeSpan BackupInterval { get; }
34 |
35 | ///
36 | /// Specifies a bit flag that indicates whether or not database actions should be logged.
37 | ///
38 | public bool DatabaseLoggingEnabled { get; }
39 |
40 | ///
41 | /// Specifies the interval between cleanup operations performed on the client lease database.
42 | ///
43 | public TimeSpan DatabaseCleanupInterval { get; }
44 |
45 | private DhcpServerConfiguration(DhcpServer server, DhcpServerApiProtocol apiProtocolSupport, string databaseName, string databasePath, string backupPath, TimeSpan backupInterval, bool databaseLoggingEnabled, TimeSpan databaseCleanupInterval)
46 | {
47 | Server = server;
48 | ApiProtocolSupport = apiProtocolSupport;
49 | DatabaseName = databaseName;
50 | DatabasePath = databasePath;
51 | BackupPath = backupPath;
52 | BackupInterval = backupInterval;
53 | DatabaseLoggingEnabled = databaseLoggingEnabled;
54 | DatabaseCleanupInterval = databaseCleanupInterval;
55 | }
56 |
57 | internal static DhcpServerConfiguration GetConfiguration(DhcpServer server)
58 | {
59 | var result = Api.DhcpServerGetConfig(ServerIpAddress: server.Address,
60 | ConfigInfo: out var configInfoPtr);
61 |
62 | if (result != DhcpErrors.SUCCESS)
63 | throw new DhcpServerException(nameof(Api.DhcpServerGetConfig), result);
64 |
65 | try
66 | {
67 | var configInfo = configInfoPtr.MarshalToStructure();
68 | return FromNative(server, in configInfo);
69 | }
70 | finally
71 | {
72 | Api.FreePointer(configInfoPtr);
73 | }
74 | }
75 |
76 | private static DhcpServerConfiguration FromNative(DhcpServer server, in DHCP_SERVER_CONFIG_INFO native)
77 | {
78 | return new DhcpServerConfiguration(server,
79 | apiProtocolSupport: (DhcpServerApiProtocol)native.APIProtocolSupport,
80 | databaseName: native.DatabaseName,
81 | databasePath: native.DatabasePath,
82 | backupPath: native.BackupPath,
83 | backupInterval: TimeSpan.FromMinutes(native.BackupInterval),
84 | databaseLoggingEnabled: (native.DatabaseLoggingFlag & 0x1) == 0x1,
85 | databaseCleanupInterval: TimeSpan.FromMinutes(native.DatabaseCleanupInterval));
86 | }
87 |
88 | public override string ToString() => $"DHCP Configuration: {Server.Name} ({Server.Address})";
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using Dhcp.Native;
4 |
5 | namespace Dhcp
6 | {
7 | public class DhcpServerException : Exception
8 | {
9 | private readonly DhcpErrors error;
10 | private readonly string message;
11 |
12 | internal DhcpServerException(string apiFunction, DhcpErrors error, string additionalMessage)
13 | {
14 | ApiFunction = apiFunction;
15 | this.error = error;
16 | ApiErrorMessage = BuildApiErrorMessage(error);
17 | message = BuildMessage(apiFunction, additionalMessage, error, ApiErrorMessage);
18 | }
19 |
20 | internal DhcpServerException(string apiFunction, DhcpErrors error)
21 | : this(apiFunction, error, null)
22 | { }
23 |
24 | public string ApiFunction { get; }
25 |
26 | public string ApiError => error.ToString();
27 |
28 | internal DhcpErrors ApiErrorNative => error;
29 | public uint ApiErrorId => (uint)error;
30 |
31 | public string ApiErrorMessage { get; }
32 |
33 | public override string Message => message;
34 |
35 | private string BuildApiErrorMessage(DhcpErrors error)
36 | {
37 | var errorType = typeof(DhcpErrors).GetMember(error.ToString());
38 | if (errorType.Length != 0)
39 | {
40 | var errorAttribute = errorType[0].GetCustomAttributes(typeof(DhcpErrorDescriptionAttribute), false);
41 |
42 | if (errorAttribute.Length != 0)
43 | return ((DhcpErrorDescriptionAttribute)errorAttribute[0]).Description;
44 | }
45 |
46 | return "Unknown Error";
47 | }
48 |
49 | private string BuildMessage(string apiFunction, string additionalMessage, DhcpErrors error, string apiErrorMessage)
50 | {
51 | var builder = new StringBuilder();
52 |
53 | if (apiFunction != null)
54 | builder.Append("An error occurred calling '").Append(apiFunction).Append("'. ");
55 |
56 | if (additionalMessage != null)
57 | builder.Append(additionalMessage).Append(". ");
58 |
59 | builder.Append(apiErrorMessage).Append(" [").Append(error.ToString()).Append(' ').Append((uint)error).Append(']');
60 |
61 | return builder.ToString();
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerFailoverMode.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerFailoverMode
4 | {
5 | ///
6 | /// The DHCPv4 server failover relationship is in Load Balancing mode.
7 | ///
8 | LoadBalance,
9 | ///
10 | /// The DHCPv4 server failover relationship is in Hot Standby mode.
11 | ///
12 | HotStandby
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerFailoverRelationshipCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerFailoverRelationshipCollection : IDhcpServerFailoverRelationshipCollection
7 | {
8 | public DhcpServer Server { get; }
9 | IDhcpServer IDhcpServerFailoverRelationshipCollection.Server => Server;
10 |
11 | internal DhcpServerFailoverRelationshipCollection(DhcpServer server)
12 | {
13 | Server = server;
14 | }
15 |
16 | public IDhcpServerFailoverRelationship GetRelationship(string relationshipName)
17 | => DhcpServerFailoverRelationship.GetFailoverRelationship(Server, relationshipName);
18 |
19 | public void RemoveRelationship(IDhcpServerFailoverRelationship relationship)
20 | => relationship.Delete();
21 |
22 | public IEnumerator GetEnumerator()
23 | => DhcpServerFailoverRelationship.GetFailoverRelationships(Server).GetEnumerator();
24 |
25 | IEnumerator IEnumerable.GetEnumerator()
26 | => GetEnumerator();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerFailoverServerType.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerFailoverServerType
4 | {
5 | ///
6 | /// The server is a primary server in the failover relationship.
7 | ///
8 | PrimaryServer,
9 | ///
10 | /// The server is a secondary server in the failover relationship.
11 | ///
12 | SecondaryServer
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerFailoverState.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | public enum DhcpServerFailoverState
6 | {
7 | ///
8 | /// Indicates that no state is configured for the DHCPv4 failover relationship.
9 | ///
10 | None,
11 | ///
12 | /// Indicates that the failover relationship on the DHCPv4 server is in the initialization state.
13 | ///
14 | Initialization,
15 | ///
16 | /// Indicates that each server participating in the failover relationship can probe its partner server before starting the DHCP client service. A DHCPv4 server moves into the STARTUP state after INIT.
17 | ///
18 | Startup,
19 | ///
20 | /// Indicates that each server in the failover relationship can service DHCPDISCOVER messages and all other DHCP requests as defined in RFC2131. DHCPv4 servers in the NORMAL state can not service DHCPREQUEST/RENEWAL or DHCPREQUEST/REBINDING requests from the client set defined according to the load balancing algorithm in RFC3074. However, each server can service DHCPREQUEST/RENEWAL or DHCPDISCOVER/REBINDING requests from any client.
21 | ///
22 | Normal,
23 | ///
24 | /// Indicates that each server in a failover relationship is operating independently, but neither assumes that their partner is not operating. The partner server might be operating and simply unable to communicate with this server, or it might not be operating at all.
25 | ///
26 | CommunicationInterupted,
27 | ///
28 | /// Indicates that a server assumes its partner is not currently operating.
29 | ///
30 | PartnerDown,
31 | ///
32 | /// Indicates that a failover relationship between two DHCPv4 servers is attempting to reestablish itself.
33 | ///
34 | PotentialConflict,
35 | ///
36 | /// Indicates that the primary server has received all updates from the secondary server during the failover relationship reintegration process.
37 | ///
38 | ConflictDone,
39 | ///
40 | /// Indicates that two servers in the POTENTIAL_CONFLICT state were attempting to reintegrate their failover relationship with each other, but communications between them failed prior to completion of the reintegration.
41 | ///
42 | ResolutionInterupted,
43 | ///
44 | /// Indicates that a server in a failover relationship has no information in its stable storage facility or that it is reintegrating with a server in the PARTNER_DOWN state.
45 | ///
46 | Recover,
47 | ///
48 | /// Indicates that the DHCPv4 server should wait for a time period equal to Maximum Client Lead Time (MCLT) before moving to the RECOVER_DONE state. The MCLT is the maximum time, in seconds, that one server can extend a lease for a client beyond the lease time known by the partner server.
49 | ///
50 | RecoverWait,
51 | ///
52 | /// This value enables an interlocked transition of one server from the RECOVER state and another server from the PARTNER_DOWN or COMMUNICATION-INT state to the NORMAL state.
53 | ///
54 | RecoverDone,
55 | ///
56 | /// Reserved. Do not use.
57 | ///
58 | [Obsolete]
59 | Paused,
60 | ///
61 | /// Reserved. Do not use.
62 | ///
63 | [Obsolete]
64 | Shutdown
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerHardwareType.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerHardwareType : byte
4 | {
5 | Ethernet = 1,
6 | ExperimentalEthernet = 2,
7 | AmateurRadioAX25 = 3,
8 | ProteonProNETTokenRing = 4,
9 | Chaos = 5,
10 | IEEE802Networks = 6,
11 | ARCNET = 7,
12 | Hyperchannel = 8,
13 | Lanstar = 9,
14 | AutonetShortAddress = 10,
15 | LocalTalk = 11,
16 | LocalNet = 12,
17 | UltraLink = 13,
18 | SMDS = 14,
19 | FrameRelay = 15,
20 | AsynchronousTransmissionMode16 = 16,
21 | HDLC = 17,
22 | FibreChannel = 18,
23 | AsynchronousTransmissionMode19 = 19,
24 | SerialLine = 20,
25 | AsynchronousTransmissionMode21 = 21,
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerHost.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 | using Dhcp.Native;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerHost : IDhcpServerHost
7 | {
8 | public DhcpServerIpAddress Address { get; }
9 | public string NetBiosName { get; }
10 | public string ServerName { get; }
11 |
12 | private DhcpServerHost(DhcpServerIpAddress address, string netBiosName, string serverName)
13 | {
14 | Address = address;
15 | NetBiosName = netBiosName;
16 | ServerName = serverName;
17 | }
18 |
19 | public static DhcpServerHost Empty { get; } = new DhcpServerHost(DhcpServerIpAddress.Empty, null, null);
20 |
21 | internal static DhcpServerHost FromNative(ref DHCP_HOST_INFO native)
22 | {
23 | return new DhcpServerHost(address: native.IpAddress.AsHostToIpAddress(),
24 | netBiosName: native.NetBiosName,
25 | serverName: native.ServerName);
26 | }
27 |
28 | internal static DhcpServerHost FromNative(DHCP_HOST_INFO native)
29 | {
30 | return new DhcpServerHost(address: native.IpAddress.AsHostToIpAddress(),
31 | netBiosName: native.NetBiosName,
32 | serverName: native.ServerName);
33 | }
34 |
35 | internal DHCP_HOST_INFO_Managed ToNative()
36 | => new DHCP_HOST_INFO_Managed(Address.ToNativeAsNetwork(), NetBiosName, ServerName);
37 |
38 | public override string ToString()
39 | {
40 | var builder = new StringBuilder(Address);
41 |
42 | if (!string.IsNullOrEmpty(ServerName))
43 | builder.Append(" [").Append(ServerName).Append("]");
44 | else if (!string.IsNullOrEmpty(NetBiosName))
45 | builder.Append(" [").Append(NetBiosName).Append("]");
46 |
47 | return builder.ToString();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerIpMask.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 | using Dhcp.Native;
4 |
5 | namespace Dhcp
6 | {
7 | [Serializable]
8 | public struct DhcpServerIpMask
9 | {
10 | #pragma warning disable IDE0032// Use auto property
11 | ///
12 | /// IP Mask stored in big-endian (network) order
13 | ///
14 | private readonly uint mask;
15 | #pragma warning restore IDE0032 // Use auto property
16 |
17 | public DhcpServerIpMask(uint mask)
18 | {
19 | this.mask = mask;
20 | }
21 | public DhcpServerIpMask(string mask)
22 | {
23 | this.mask = BitHelper.StringToIpAddress(mask);
24 | }
25 |
26 | ///
27 | /// IP Mask in network order
28 | ///
29 | public uint Native => mask;
30 |
31 | public static DhcpServerIpMask Empty => new DhcpServerIpMask(0);
32 | public static DhcpServerIpMask FromNative(uint nativeMask)
33 | => new DhcpServerIpMask(nativeMask);
34 |
35 | public static DhcpServerIpMask FromNative(int nativeMask)
36 | => new DhcpServerIpMask((uint)nativeMask);
37 |
38 | internal static DhcpServerIpMask FromNative(IntPtr pointer)
39 | {
40 | if (pointer == IntPtr.Zero)
41 | throw new ArgumentNullException(nameof(pointer));
42 |
43 | return new DhcpServerIpMask((uint)BitHelper.HostToNetworkOrder(Marshal.ReadInt32(pointer)));
44 | }
45 |
46 | public static DhcpServerIpMask FromString(string mask)
47 | => new DhcpServerIpMask(mask);
48 |
49 | public static DhcpServerIpMask FromSignificantBits(int bitCount)
50 | {
51 | if (bitCount > 32 || bitCount < 0)
52 | throw new ArgumentOutOfRangeException(nameof(bitCount));
53 |
54 | if (bitCount == 0)
55 | return new DhcpServerIpMask(0U);
56 |
57 | var m = unchecked((int)0x8000_0000) >> (bitCount - 1); // signed int
58 |
59 | return new DhcpServerIpMask((uint)m);
60 | }
61 |
62 | public int SignificantBits => BitHelper.HighSignificantBits(mask);
63 |
64 | public DhcpServerIpRange GetDhcpIpRange(DhcpServerIpAddress address) => DhcpServerIpRange.AsDhcpScope(address, this);
65 | public DhcpServerIpRange GetDhcpAndBootpIpRange(DhcpServerIpAddress address) => DhcpServerIpRange.AsDhcpAndBootpScope(address, this);
66 | public DhcpServerIpRange GetBootpIpRange(DhcpServerIpAddress address) => DhcpServerIpRange.AsBootpScope(address, this);
67 | internal DhcpServerIpRange GetIpRange(DhcpServerIpAddress address, DhcpServerIpRangeType type) => DhcpServerIpRange.FromMask(address, this, type);
68 |
69 | internal DHCP_IP_MASK ToNativeAsHost() => new DHCP_IP_MASK(BitHelper.NetworkToHostOrder(mask));
70 | internal DHCP_IP_MASK ToNativeAsNetwork() => new DHCP_IP_MASK(mask);
71 |
72 | public override string ToString() => BitHelper.IpAddressToString(mask);
73 |
74 | public static explicit operator DhcpServerIpAddress(DhcpServerIpMask mask) => new DhcpServerIpAddress(mask.mask);
75 | public static explicit operator DhcpServerIpMask(string mask) => new DhcpServerIpMask(mask);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerIpRangeType.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerIpRangeType
4 | {
5 | Reserved = 2,
6 | Excluded = 3,
7 | ScopeDhcpOnly = 5,
8 | ScopeDhcpAndBootp = 6,
9 | ScopeBootpOnly = 7,
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerMessageTypes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerMessageTypes : byte
4 | {
5 | BootRequest = 1,
6 | BootReply = 2
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerOptionElementType.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | ///
4 | /// Defines the set of formats that represent DHCP option data
5 | ///
6 | public enum DhcpServerOptionElementType
7 | {
8 | ///
9 | /// The option data is stored as a BYTE value.
10 | ///
11 | Byte,
12 | ///
13 | /// The option data is stored as a WORD value.
14 | ///
15 | Word,
16 | ///
17 | /// The option data is stored as a DWORD value.
18 | ///
19 | DWord,
20 | ///
21 | /// The option data is stored as a DWORD_DWORD value.
22 | ///
23 | DWordDWord,
24 | ///
25 | /// The option data is an IP address, stored as a DHCP_IP_ADDRESS value (DWORD).
26 | ///
27 | IpAddress,
28 | ///
29 | /// The option data is stored as a Unicode string.
30 | ///
31 | StringData,
32 | ///
33 | /// The option data is stored as a binary structure.
34 | ///
35 | BinaryData,
36 | ///
37 | /// The option data is encapsulated and stored as a binary structure.
38 | ///
39 | EncapsulatedData,
40 | ///
41 | /// The option data is stored as a Unicode string.
42 | ///
43 | Ipv6Address
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerOptionIdTypeAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
6 | public class DhcpServerOptionIdTypeAttribute : Attribute
7 | {
8 | public DhcpServerOptionIdTypes Type;
9 |
10 | public DhcpServerOptionIdTypeAttribute(DhcpServerOptionIdTypes type)
11 | {
12 | Type = type;
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerOptionIdTypes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerOptionIdTypes : byte
4 | {
5 | Hex = 0,
6 | Pad,
7 | End,
8 | IpAddress,
9 | IpAddressList,
10 | Byte,
11 | Int16,
12 | UInt16,
13 | Int32,
14 | UInt32,
15 | AsciiString,
16 | Utf8String,
17 | IpAddressAndSubnet,
18 | IpAddressAndIpAddress,
19 | UInt16List,
20 | DhcpMessageType,
21 | DhcpParameterRequestList,
22 | ZeroLengthFlag,
23 | ClientFQDN,
24 | DnsName,
25 | DnsNameList,
26 | ClientUUID,
27 | SipServers,
28 | StatusCode,
29 | DhcpState
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerPacketFlags.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | [Flags]
6 | public enum DhcpServerPacketFlags : ushort
7 | {
8 | Broadcast = 0x8000
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerPacketMessageTypes.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerPacketMessageTypes : byte
4 | {
5 | Unknown = 0,
6 | Discover = 1,
7 | Offer = 2,
8 | Request = 3,
9 | Decline = 4,
10 | Acknowledgment = 5,
11 | NegativeAcknowledgment = 6,
12 | Release = 7,
13 | Inform = 8,
14 | ForceRenew = 9,
15 | LeaseQuery = 10,
16 | LeaseUnassigned = 11,
17 | LeaseUknown = 12,
18 | LeaseActive = 13,
19 | BulkLeaseQuery = 14,
20 | LeaseQueryDone = 15,
21 | ActiveLeaseQuery = 16,
22 | LeaseQueryStatus = 17,
23 | Tls = 18
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerScopeClientCollection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 |
5 | namespace Dhcp
6 | {
7 | public class DhcpServerScopeClientCollection : IDhcpServerScopeClientCollection
8 | {
9 | public DhcpServer Server { get; }
10 | IDhcpServer IDhcpServerScopeClientCollection.Server => Server;
11 | public DhcpServerScope Scope { get; }
12 | IDhcpServerScope IDhcpServerScopeClientCollection.Scope => Scope;
13 |
14 | internal DhcpServerScopeClientCollection(DhcpServerScope scope)
15 | {
16 | Server = scope.Server;
17 | Scope = scope;
18 | }
19 |
20 | public IEnumerator GetEnumerator()
21 | => DhcpServerClient.GetScopeClients(Scope).GetEnumerator();
22 |
23 | IEnumerator IEnumerable.GetEnumerator()
24 | => GetEnumerator();
25 |
26 | public void RemoveClient(IDhcpServerClient client)
27 | => client.Delete();
28 |
29 | public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress)
30 | => DhcpServerClient.CreateClient(Scope, address, hardwareAddress);
31 | public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name)
32 | => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name);
33 |
34 | public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment)
35 | => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment);
36 |
37 | public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires)
38 | => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment, leaseExpires);
39 |
40 | public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires, IDhcpServerHost ownerHost)
41 | => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment, leaseExpires, (DhcpServerHost)ownerHost);
42 |
43 | public IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires, IDhcpServerHost ownerHost, DhcpServerClientTypes clientType, DhcpServerClientAddressStates addressState, DhcpServerClientQuarantineStatuses quarantineStatus, DateTime probationEnds, bool quarantineCapable)
44 | => DhcpServerClient.CreateClient(Scope, address, hardwareAddress, name, comment, leaseExpires, (DhcpServerHost)ownerHost, clientType, addressState, quarantineStatus, probationEnds, quarantineCapable);
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerScopeExcludedIpRangeCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public class DhcpServerScopeExcludedIpRangeCollection : IDhcpServerScopeExcludedIpRangeCollection
7 | {
8 | public DhcpServer Server { get; }
9 | IDhcpServer IDhcpServerScopeExcludedIpRangeCollection.Server => Server;
10 | public DhcpServerScope Scope { get; }
11 | IDhcpServerScope IDhcpServerScopeExcludedIpRangeCollection.Scope => Scope;
12 |
13 | internal DhcpServerScopeExcludedIpRangeCollection(DhcpServerScope scope)
14 | {
15 | Server = scope.Server;
16 | Scope = scope;
17 | }
18 |
19 | ///
20 | /// Enumerates a list of excluded IP ranges associated with the DHCP Scope
21 | ///
22 | public IEnumerator GetEnumerator()
23 | => DhcpServerScope.EnumSubnetElements(Server, Scope.Address, Native.DHCP_SUBNET_ELEMENT_TYPE.DhcpExcludedIpRanges).GetEnumerator();
24 |
25 | ///
26 | /// Enumerates a list of excluded IP ranges associated with the DHCP Scope
27 | ///
28 | IEnumerator IEnumerable.GetEnumerator()
29 | => GetEnumerator();
30 |
31 | public void AddExcludedIpRange(DhcpServerIpRange range)
32 | => DhcpServerScope.AddSubnetExcludedIpRangeElement(Server, Scope.Address, range);
33 | public void AddExcludedIpRange(DhcpServerIpAddress startAddress, DhcpServerIpAddress endAddress)
34 | => DhcpServerScope.AddSubnetExcludedIpRangeElement(Server, Scope.Address, DhcpServerIpRange.AsExcluded(startAddress, endAddress));
35 | public void AddExcludedIpRange(DhcpServerIpAddress address, DhcpServerIpMask mask)
36 | => DhcpServerScope.AddSubnetExcludedIpRangeElement(Server, Scope.Address, DhcpServerIpRange.AsExcluded(address, mask));
37 | public void AddExcludedIpRange(string cidrRange)
38 | => DhcpServerScope.AddSubnetExcludedIpRangeElement(Server, Scope.Address, DhcpServerIpRange.AsExcluded(cidrRange));
39 |
40 | public void RemoveExcludedIpRange(DhcpServerIpRange range)
41 | => DhcpServerScope.RemoveSubnetExcludedIpRangeElement(Server, Scope.Address, range);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerScopeReservationCollection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 |
5 | namespace Dhcp
6 | {
7 | public class DhcpServerScopeReservationCollection : IDhcpServerScopeReservationCollection
8 | {
9 | public DhcpServer Server { get; }
10 | IDhcpServer IDhcpServerScopeReservationCollection.Server => Server;
11 | public DhcpServerScope Scope { get; }
12 | IDhcpServerScope IDhcpServerScopeReservationCollection.Scope => Scope;
13 |
14 | internal DhcpServerScopeReservationCollection(DhcpServerScope scope)
15 | {
16 | Server = scope.Server;
17 | Scope = scope;
18 | }
19 |
20 | ///
21 | /// Enumerates a list of reservations associated with the DHCP Scope
22 | ///
23 | public IEnumerator GetEnumerator()
24 | => DhcpServerScopeReservation.GetReservations(Scope).GetEnumerator();
25 |
26 | ///
27 | /// Enumerates a list of reservations associated with the DHCP Scope
28 | ///
29 | IEnumerator IEnumerable.GetEnumerator()
30 | => GetEnumerator();
31 |
32 | ///
33 | /// Creates a DHCP scope reservation from a client
34 | ///
35 | /// A DHCP client to convert to a reservation
36 | /// The scope reservation
37 | public IDhcpServerScopeReservation AddReservation(IDhcpServerClient client)
38 | {
39 | if (!Scope.IpRange.Contains(client.IpAddress))
40 | throw new ArgumentOutOfRangeException(nameof(client), "The client address is not within the IP range of the scope");
41 |
42 | return DhcpServerScopeReservation.CreateReservation(Scope, client.IpAddress, client.HardwareAddress);
43 | }
44 | ///
45 | /// Creates a DHCP scope reservation
46 | ///
47 | /// IP Address to reserve
48 | /// Hardware address (MAC address) of client associated with this reservation
49 | /// The scope reservation
50 | public IDhcpServerScopeReservation AddReservation(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress)
51 | => DhcpServerScopeReservation.CreateReservation(Scope, address, hardwareAddress);
52 | ///
53 | /// Creates a DHCP scope reservation
54 | ///
55 | /// IP Address to reserve
56 | /// Hardware address (MAC address) of client associated with this reservation
57 | /// Protocols this reservation supports
58 | /// The scope reservation
59 | public IDhcpServerScopeReservation AddReservation(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, DhcpServerClientTypes allowedClientTypes)
60 | => DhcpServerScopeReservation.CreateReservation(Scope, address, hardwareAddress, allowedClientTypes);
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerScopeState.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerScopeState : uint
4 | {
5 | ///
6 | /// The subnet is enabled; the server will distribute addresses, extend leases, and release addresses within the subnet range to clients.
7 | ///
8 | Enabled = 0,
9 | ///
10 | /// The subnet is disabled; the server will not distribute addresses or extend leases within the subnet range to clients. However, the server will still release addresses within the subnet range.
11 | ///
12 | Disabled,
13 | ///
14 | /// The subnet is enabled; the server will distribute addresses, extend leases, and release addresses within the subnet range to clients. The default gateway is set to the local machine itself.
15 | ///
16 | EnabledSwitched,
17 | ///
18 | /// The subnet is disabled; the server will not distribute addresses or extend leases within the subnet range to clients. However, the server will still release addresses within the subnet range. The default gateway is set to the local machine itself.
19 | ///
20 | DisabledSwitched,
21 | ///
22 | /// The subnet is in an invalid state.
23 | ///
24 | InvalidState
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerSpecificStrings.cs:
--------------------------------------------------------------------------------
1 | using Dhcp.Native;
2 |
3 | namespace Dhcp
4 | {
5 | public class DhcpServerSpecificStrings : IDhcpServerSpecificStrings
6 | {
7 | public string DefaultVendorClassName { get; }
8 | public string DefaultUserClassName { get; }
9 |
10 | private DhcpServerSpecificStrings(string defaultVendorClassName, string defaultUserClassName)
11 | {
12 | DefaultVendorClassName = defaultVendorClassName;
13 | DefaultUserClassName = defaultUserClassName;
14 | }
15 |
16 | private static DhcpServerSpecificStrings FromNative(DHCP_SERVER_SPECIFIC_STRINGS strings)
17 | => new DhcpServerSpecificStrings(strings.DefaultVendorClassName, strings.DefaultUserClassName);
18 |
19 | internal static DhcpServerSpecificStrings GetSpecificStrings(DhcpServer server)
20 | {
21 | var result = Api.DhcpGetServerSpecificStrings(ServerIpAddress: server.Address,
22 | ServerSpecificStrings: out var stringsPtr);
23 |
24 | if (result != DhcpErrors.SUCCESS)
25 | throw new DhcpServerException(nameof(Api.DhcpGetServerSpecificStrings), result);
26 |
27 | try
28 | {
29 | var strings = stringsPtr.MarshalToStructure();
30 | return FromNative(strings);
31 | }
32 | finally
33 | {
34 | Api.DhcpRpcFreeMemory(stringsPtr);
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Dhcp/DhcpServerVersions.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public enum DhcpServerVersions : long
4 | {
5 | WindowsNT310 = 0x3000A,
6 | WindowsNT350 = 0x30032,
7 | WindowsNT351 = 0x30033,
8 | WindowsNT40 = 0x40000,
9 | Windows2000 = 0x50000,
10 | Windows2003 = 0x50002,
11 | Windows2003R2 = 0x50002,
12 | Windows2008 = 0x60000,
13 | Windows2008R2 = 0x60001,
14 | Windows2012 = 0x60002,
15 | Windows2012R2 = 0x60003,
16 | ///
17 | /// Represents Windows Server 2016 and greater
18 | ///
19 | WindowsServer = 0xA0000,
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServer.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServer
4 | {
5 | DhcpServerIpAddress Address { get; }
6 | IDhcpServerAuditLog AuditLog { get; }
7 | IDhcpServerBindingElementCollection BindingElements { get; }
8 | IDhcpServerClassCollection Classes { get; }
9 | IDhcpServerClientCollection Clients { get; }
10 | IDhcpServerConfiguration Configuration { get; }
11 | IDhcpServerDnsSettings DnsSettings { get; }
12 | IDhcpServerFailoverRelationshipCollection FailoverRelationships { get; }
13 | string Name { get; }
14 | IDhcpServerOptionCollection Options { get; }
15 | IDhcpServerScopeCollection Scopes { get; }
16 | IDhcpServerSpecificStrings SpecificStrings { get; }
17 | DhcpServerVersions Version { get; }
18 | int VersionMajor { get; }
19 | int VersionMinor { get; }
20 |
21 | bool IsCompatible(DhcpServerVersions version);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerAuditLog.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerAuditLog
4 | {
5 | string AuditLogDirectory { get; }
6 | int DiskCheckInterval { get; }
7 | int MaxLogFilesSize { get; }
8 | int MinSpaceOnDisk { get; }
9 | IDhcpServer Server { get; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerBindingElement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerBindingElement
6 | {
7 | DhcpServerIpAddress AdapterPrimaryIpAddress { get; }
8 | DhcpServerIpMask AdapterSubnetAddress { get; }
9 | bool CantModify { get; }
10 | string InterfaceDescription { get; }
11 | Guid InterfaceGuidId { get; }
12 | byte[] InterfaceId { get; }
13 | bool IsBound { get; }
14 | IDhcpServer Server { get; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerBindingElementCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerBindingElementCollection : IEnumerable
6 | {
7 | IDhcpServer Server { get; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerClass.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerClass
6 | {
7 | string Comment { get; }
8 | byte[] Data { get; }
9 | string DataText { get; }
10 | IEnumerable GlobalOptionValues { get; }
11 | bool IsUserClass { get; }
12 | bool IsVendorClass { get; }
13 | string Name { get; }
14 | IEnumerable Options { get; }
15 | IDhcpServer Server { get; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerClassCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerClassCollection : IEnumerable
6 | {
7 | IDhcpServer Server { get; }
8 |
9 | IDhcpServerClass GetClass(string name);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerClient.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerClient
6 | {
7 | DhcpServerClientAddressStates AddressState { get; }
8 | string Comment { get; set; }
9 | DhcpServerClientDnsStates DnsState { get; }
10 | DhcpServerHardwareAddress HardwareAddress { get; }
11 | DhcpServerIpAddress IpAddress { get; }
12 | bool LeaseExpired { get; }
13 | DateTime LeaseExpires { get; }
14 | DateTime LeaseExpiresLocal { get; }
15 | DateTime LeaseExpiresUtc { get; }
16 | bool LeaseHasExpiry { get; }
17 | string Name { get; set; }
18 | DhcpServerClientNameProtectionStates NameProtectionState { get; }
19 | DateTime ProbationEnds { get; }
20 | bool QuarantineCapable { get; }
21 | DhcpServerClientQuarantineStatuses QuarantineStatus { get; }
22 | IDhcpServerScope Scope { get; }
23 | IDhcpServer Server { get; }
24 | DhcpServerIpMask SubnetMask { get; }
25 | DhcpServerClientTypes Type { get; }
26 |
27 | IDhcpServerScopeReservation ConvertToReservation();
28 | void Delete();
29 | string ToString();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerClientCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerClientCollection : IEnumerable
6 | {
7 | IDhcpServer Server { get; }
8 |
9 | void RemoveClient(IDhcpServerClient client);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerConfiguration.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerConfiguration
6 | {
7 | DhcpServerApiProtocol ApiProtocolSupport { get; }
8 | TimeSpan BackupInterval { get; }
9 | string BackupPath { get; }
10 | TimeSpan DatabaseCleanupInterval { get; }
11 | bool DatabaseLoggingEnabled { get; }
12 | string DatabaseName { get; }
13 | string DatabasePath { get; }
14 | IDhcpServer Server { get; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerDnsSettings.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerDnsSettings
4 | {
5 | bool DisableDynamicPtrRecordUpdates { get; }
6 | bool DiscardRecordsWhenLeasesDeleted { get; }
7 | bool DynamicDnsUpdatedAlways { get; }
8 | bool DynamicDnsUpdatedOnlyWhenRequested { get; }
9 | bool DynamicDnsUpdatesEnabled { get; }
10 | bool UpdateRecordsForDownLevelClients { get; }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerFailoverRelationship.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public interface IDhcpServerFailoverRelationship
7 | {
8 | byte? HotStandbyAddressesReservedPercentage { get; }
9 | byte? LoadBalancePercentage { get; }
10 | TimeSpan MaximumClientLeadTime { get; }
11 | DhcpServerFailoverMode Mode { get; }
12 | string Name { get; }
13 | DhcpServerFailoverState PreviousState { get; }
14 | DhcpServerIpAddress PrimaryServerAddress { get; }
15 | string PrimaryServerName { get; }
16 | IEnumerable Scopes { get; }
17 | DhcpServerIpAddress SecondaryServerAddress { get; }
18 | string SecondaryServerName { get; }
19 | IDhcpServer Server { get; }
20 | DhcpServerFailoverServerType ServerType { get; }
21 | string SharedSecret { get; }
22 | DhcpServerFailoverState State { get; }
23 | TimeSpan? StateSwitchoverInterval { get; }
24 |
25 | IDhcpServer ConnectToPartner();
26 | void Delete();
27 | void RedistributesFreeAddresses();
28 | void ReplicateRelationship();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerFailoverRelationshipCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerFailoverRelationshipCollection : IEnumerable
6 | {
7 | IDhcpServer Server { get; }
8 |
9 | IDhcpServerFailoverRelationship GetRelationship(string relationshipName);
10 | void RemoveRelationship(IDhcpServerFailoverRelationship relationship);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerHost.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerHost
4 | {
5 | DhcpServerIpAddress Address { get; }
6 | string NetBiosName { get; }
7 | string ServerName { get; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerOption.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerOption
6 | {
7 | IDhcpServerClass Class { get; }
8 | string ClassName { get; }
9 | string Comment { get; }
10 | IEnumerable DefaultValue { get; }
11 | bool IsUnaryOption { get; }
12 | bool IsUserClassOption { get; }
13 | bool IsVendorClassOption { get; }
14 | string Name { get; }
15 | int OptionId { get; }
16 | IDhcpServer Server { get; }
17 | DhcpServerOptionElementType ValueType { get; }
18 | string VendorName { get; }
19 |
20 | IDhcpServerOptionValue CreateOptionBinaryValue(byte[] value);
21 | IDhcpServerOptionValue CreateOptionBinaryValue(IEnumerable values);
22 | IDhcpServerOptionValue CreateOptionBinaryValue(params byte[][] values);
23 | IDhcpServerOptionValue CreateOptionByteValue(byte value);
24 | IDhcpServerOptionValue CreateOptionByteValue(IEnumerable values);
25 | IDhcpServerOptionValue CreateOptionByteValue(params byte[] values);
26 | IDhcpServerOptionValue CreateOptionEncapsulatedValue(byte[] value);
27 | IDhcpServerOptionValue CreateOptionEncapsulatedValue(IEnumerable values);
28 | IDhcpServerOptionValue CreateOptionEncapsulatedValue(params byte[][] values);
29 | IDhcpServerOptionValue CreateOptionInt16Value(IEnumerable values);
30 | IDhcpServerOptionValue CreateOptionInt16Value(params short[] values);
31 | IDhcpServerOptionValue CreateOptionInt16Value(short value);
32 | IDhcpServerOptionValue CreateOptionInt32Value(IEnumerable values);
33 | IDhcpServerOptionValue CreateOptionInt32Value(int value);
34 | IDhcpServerOptionValue CreateOptionInt32Value(params int[] values);
35 | IDhcpServerOptionValue CreateOptionInt64Value(IEnumerable values);
36 | IDhcpServerOptionValue CreateOptionInt64Value(long value);
37 | IDhcpServerOptionValue CreateOptionInt64Value(params long[] values);
38 | IDhcpServerOptionValue CreateOptionIpAddressValue(DhcpServerIpAddress value);
39 | IDhcpServerOptionValue CreateOptionIpAddressValue(IEnumerable values);
40 | IDhcpServerOptionValue CreateOptionIpAddressValue(params DhcpServerIpAddress[] values);
41 | IDhcpServerOptionValue CreateOptionIpv6AddressValue(IEnumerable values);
42 | IDhcpServerOptionValue CreateOptionIpv6AddressValue(params string[] values);
43 | IDhcpServerOptionValue CreateOptionIpv6AddressValue(string value);
44 | IDhcpServerOptionValue CreateOptionStringValue(IEnumerable values);
45 | IDhcpServerOptionValue CreateOptionStringValue(params string[] values);
46 | IDhcpServerOptionValue CreateOptionStringValue(string value);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerOptionCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerOptionCollection : IEnumerable
6 | {
7 | IDhcpServer Server { get; }
8 |
9 | IDhcpServerOption GetDefaultOption(DhcpServerOptionIds optionId);
10 | IDhcpServerOption GetDefaultOption(int optionId);
11 | IEnumerable GetDefaultOptions();
12 | IDhcpServerOptionValue GetDefaultOptionValue(DhcpServerOptionIds optionId);
13 | IDhcpServerOptionValue GetDefaultOptionValue(int optionId);
14 | IEnumerable GetDefaultOptionValues();
15 | IEnumerable GetOptionValues();
16 | IDhcpServerOption GetUserOption(string className, DhcpServerOptionIds optionId);
17 | IDhcpServerOption GetUserOption(string className, int optionId);
18 | IEnumerable GetUserOptions(string className);
19 | IDhcpServerOptionValue GetUserOptionValue(string className, DhcpServerOptionIds optionId);
20 | IDhcpServerOptionValue GetUserOptionValue(string className, int optionId);
21 | IDhcpServerOption GetVendorOption(string vendorName, DhcpServerOptionIds optionId);
22 | IDhcpServerOption GetVendorOption(string vendorName, int optionId);
23 | IEnumerable GetVendorOptions(string vendorName);
24 | IDhcpServerOptionValue GetVendorOptionValue(string vendorName, DhcpServerOptionIds optionId);
25 | IDhcpServerOptionValue GetVendorOptionValue(string vendorName, int optionId);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerOptionElement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerOptionElement : IEquatable
6 | {
7 | DhcpServerOptionElementType Type { get; }
8 | object Value { get; }
9 | string ValueFormatted { get; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerOptionValue.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerOptionValue
6 | {
7 | string ClassName { get; }
8 | IDhcpServerOption Option { get; }
9 | int OptionId { get; }
10 | IDhcpServer Server { get; }
11 | IEnumerable Values { get; }
12 | string VendorName { get; }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerPacket.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.ObjectModel;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerPacket
6 | {
7 | ///
8 | /// Packet OP Code/Message Type (OP)
9 | ///
10 | DhcpServerMessageTypes MessageType { get; }
11 |
12 | ///
13 | /// Type of Hardware Address (HTYPE)
14 | ///
15 | DhcpServerHardwareType HardwareAddressType { get; }
16 |
17 | ///
18 | /// Length (in bytes) of the Hardware Address (HLEN)
19 | ///
20 | byte HardwareAddressLength { get; }
21 |
22 | ///
23 | /// Optionally used by relay agents when booting via a relay agent (HOPS)
24 | ///
25 | byte GatewayHops { get; }
26 |
27 | ///
28 | /// Transaction Id, a random number, used to match this boot request with the responses it generates (XID)
29 | ///
30 | int TransactionId { get; }
31 |
32 | ///
33 | /// Seconds elapsed since client started trying to boot (SECS)
34 | ///
35 | ushort SecondsElapsed { get; }
36 |
37 | ///
38 | /// Flags (FLAGS)
39 | ///
40 | DhcpServerPacketFlags Flags { get; }
41 |
42 | ///
43 | /// Client IP Address; only filled in if client is in BOUND, RENEW or REBINDING state and can respond to ARP requests (CIADDR)
44 | ///
45 | DhcpServerIpAddress ClientIpAddress { get; }
46 |
47 | ///
48 | /// 'your' (client) IP address (YIADDR)
49 | ///
50 | DhcpServerIpAddress YourIpAddress { get; }
51 |
52 | ///
53 | /// IP address of next server to use in bootstrap; returned in DHCPOFFER, DHCPACK by server (SIADDR)
54 | ///
55 | DhcpServerIpAddress NextServerIpAddress { get; }
56 |
57 | ///
58 | /// Relay agent IP address, used in booting via a relay agent (GIADDR)
59 | ///
60 | DhcpServerIpAddress RelayAgentIpAddress { get; }
61 |
62 | ///
63 | /// Client hardware address (CHADDR)
64 | ///
65 | DhcpServerHardwareAddress ClientHardwareAddress { get; }
66 |
67 | ///
68 | /// Optional server host name (SNAME)
69 | ///
70 | string ServerHostName { get; }
71 |
72 | ///
73 | /// Boot file name; "generic" name or null in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER (FILE)
74 | ///
75 | string FileName { get; }
76 |
77 | int OptionsMagicCookie { get; }
78 |
79 | DhcpServerPacketMessageTypes DhcpMessageType { get; }
80 |
81 | ReadOnlyCollection Options { get; }
82 | bool TryGetOption(DhcpServerOptionIds optionId, out DhcpServerPacketOption option);
83 | bool TryGetOption(byte optionId, out DhcpServerPacketOption option);
84 |
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerPacketRaw.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerPacketRaw : IDhcpServerPacket
4 | {
5 | ///
6 | /// Returns the buffer containing the raw packet bytes
7 | ///
8 | byte[] Buffer { get; }
9 |
10 | ///
11 | /// Calculates the length of the packet
12 | ///
13 | /// The length of the packet in the buffer
14 | int GetLength();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerPacketRawWritable.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerPacketRawWritable : IDhcpServerPacketWritable, IDhcpServerPacketRaw
4 | {
5 |
6 | ///
7 | /// Attempts to increase or decrease the packet buffer at the by the .
8 | ///
9 | /// The buffer index where the adjustment should be made.
10 | /// The amount to adjust the buffer by. Positive numbers increase the buffer, negative numbers shrink the buffer.
11 | /// true if the adjustment was successful
12 | bool TryAdjustBuffer(int index, int adjustmentAmount);
13 |
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerPacketWritable.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerPacketWritable : IDhcpServerPacket
4 | {
5 | ///
6 | /// Packet OP Code/Message Type (OP)
7 | ///
8 | new DhcpServerMessageTypes MessageType { get; set; }
9 |
10 | ///
11 | /// Type of Hardware Address (HTYPE)
12 | ///
13 | new DhcpServerHardwareType HardwareAddressType { get; set; }
14 |
15 | ///
16 | /// Length (in bytes) of the Hardware Address (HLEN)
17 | ///
18 | new byte HardwareAddressLength { get; set; }
19 |
20 | ///
21 | /// Optionally used by relay agents when booting via a relay agent (HOPS)
22 | ///
23 | new byte GatewayHops { get; set; }
24 |
25 | ///
26 | /// Transaction Id, a random number, used to match this boot request with the responses it generates (XID)
27 | ///
28 | new int TransactionId { get; set; }
29 |
30 | ///
31 | /// Seconds elapsed since client started trying to boot (SECS)
32 | ///
33 | new ushort SecondsElapsed { get; set; }
34 |
35 | ///
36 | /// Flags (FLAGS)
37 | ///
38 | new DhcpServerPacketFlags Flags { get; set; }
39 |
40 | ///
41 | /// Client IP Address; only filled in if client is in BOUND, RENEW or REBINDING state and can respond to ARP requests (CIADDR)
42 | ///
43 | new DhcpServerIpAddress ClientIpAddress { get; set; }
44 |
45 | ///
46 | /// 'your' (client) IP address (YIADDR)
47 | ///
48 | new DhcpServerIpAddress YourIpAddress { get; set; }
49 |
50 | ///
51 | /// IP address of next server to use in bootstrap; returned in DHCPOFFER, DHCPACK by server (SIADDR)
52 | ///
53 | new DhcpServerIpAddress NextServerIpAddress { get; set; }
54 |
55 | ///
56 | /// Relay agent IP address, used in booting via a relay agent (GIADDR)
57 | ///
58 | new DhcpServerIpAddress RelayAgentIpAddress { get; set; }
59 |
60 | ///
61 | /// Client hardware address (CHADDR)
62 | ///
63 | new DhcpServerHardwareAddress ClientHardwareAddress { get; set; }
64 |
65 | ///
66 | /// Optional server host name (SNAME)
67 | ///
68 | new string ServerHostName { get; set; }
69 |
70 | ///
71 | /// Boot file name; "generic" name or null in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER (FILE)
72 | ///
73 | new string FileName { get; set; }
74 |
75 | new int OptionsMagicCookie { get; set; }
76 |
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScope.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerScope
6 | {
7 | DhcpServerIpAddress Address { get; }
8 | IDhcpServerScopeClientCollection Clients { get; }
9 | string Comment { get; set; }
10 | IDhcpServerDnsSettings DnsSettings { get; }
11 | IDhcpServerScopeExcludedIpRangeCollection ExcludedIpRanges { get; }
12 | DhcpServerIpRange IpRange { get; set; }
13 | TimeSpan? LeaseDuration { get; set; }
14 | DhcpServerIpMask Mask { get; }
15 | string Name { get; set; }
16 | IDhcpServerScopeOptionValueCollection Options { get; }
17 | IDhcpServerHost PrimaryHost { get; }
18 | bool QuarantineOn { get; }
19 | IDhcpServerScopeReservationCollection Reservations { get; }
20 | IDhcpServer Server { get; }
21 | DhcpServerScopeState State { get; }
22 | TimeSpan TimeDelayOffer { get; set; }
23 |
24 | void Activate();
25 | IDhcpServerFailoverRelationship ConfigureFailover(IDhcpServer partnerServer, string sharedSecret, DhcpServerFailoverMode mode);
26 | IDhcpServerFailoverRelationship ConfigureFailover(IDhcpServer partnerServer, string sharedSecret, DhcpServerFailoverMode mode, byte modePercentage);
27 | IDhcpServerFailoverRelationship ConfigureFailover(IDhcpServer partnerServer, string sharedSecret, DhcpServerFailoverMode mode, byte modePercentage, TimeSpan maximumClientLeadTime, TimeSpan? stateSwitchInterval);
28 | IDhcpServerFailoverRelationship ConfigureFailover(IDhcpServer partnerServer, string name, string sharedSecret, DhcpServerFailoverMode mode);
29 | IDhcpServerFailoverRelationship ConfigureFailover(IDhcpServer partnerServer, string name, string sharedSecret, DhcpServerFailoverMode mode, byte modePercentage);
30 | IDhcpServerFailoverRelationship ConfigureFailover(IDhcpServer partnerServer, string name, string sharedSecret, DhcpServerFailoverMode mode, byte modePercentage, TimeSpan maximumClientLeadTime, TimeSpan? stateSwitchInterval);
31 | void ConfigureFailover(IDhcpServerFailoverRelationship failoverRelationship);
32 | void Deactivate();
33 | void DeconfigureFailover();
34 | void Delete(bool retainClientDnsRecords = false);
35 | IDhcpServerFailoverRelationship GetFailoverRelationship();
36 | IDhcpServerScopeFailoverStatistics GetFailoverStatistics();
37 | void ReplicateFailoverPartner();
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeClientCollection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public interface IDhcpServerScopeClientCollection : IEnumerable
7 | {
8 | IDhcpServerScope Scope { get; }
9 | IDhcpServer Server { get; }
10 |
11 | IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress);
12 | IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name);
13 | IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment);
14 | IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires);
15 | IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires, IDhcpServerHost ownerHost);
16 | IDhcpServerClient AddClient(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, string name, string comment, DateTime leaseExpires, IDhcpServerHost ownerHost, DhcpServerClientTypes clientType, DhcpServerClientAddressStates addressState, DhcpServerClientQuarantineStatuses quarantineStatus, DateTime probationEnds, bool quarantineCapable);
17 | void RemoveClient(IDhcpServerClient client);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeCollection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Dhcp
5 | {
6 | public interface IDhcpServerScopeCollection : IEnumerable
7 | {
8 | IDhcpServer Server { get; }
9 |
10 | IDhcpServerScope AddScope(string name, DhcpServerIpRange ipRange);
11 | IDhcpServerScope AddScope(string name, DhcpServerIpRange ipRange, DhcpServerIpMask mask);
12 | IDhcpServerScope AddScope(string name, DhcpServerIpRange ipRange, DhcpServerIpMask mask, TimeSpan timeDelayOffer, TimeSpan? leaseDuration);
13 | IDhcpServerScope AddScope(string name, string comment, DhcpServerIpRange ipRange);
14 | IDhcpServerScope AddScope(string name, string comment, DhcpServerIpRange ipRange, DhcpServerIpMask mask);
15 | IDhcpServerScope AddScope(string name, string comment, DhcpServerIpRange ipRange, DhcpServerIpMask mask, TimeSpan timeDelayOffer, TimeSpan? leaseDuration);
16 | IDhcpServerScope GetScope(DhcpServerIpAddress scopeAddress);
17 | void RemoveScope(IDhcpServerScope scope, bool retainClientDnsRecords = false);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeExcludedIpRangeCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerScopeExcludedIpRangeCollection : IEnumerable
6 | {
7 | IDhcpServerScope Scope { get; }
8 | IDhcpServer Server { get; }
9 |
10 | void AddExcludedIpRange(DhcpServerIpAddress startAddress, DhcpServerIpAddress endAddress);
11 | void AddExcludedIpRange(DhcpServerIpAddress address, DhcpServerIpMask mask);
12 | void AddExcludedIpRange(DhcpServerIpRange range);
13 | void AddExcludedIpRange(string cidrRange);
14 | void RemoveExcludedIpRange(DhcpServerIpRange range);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeFailoverStatistics.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerScopeFailoverStatistics
4 | {
5 | int AddressesFree { get; }
6 | int AddressesInUse { get; }
7 | int AddressesTotal { get; }
8 | int LocalAddressesFree { get; }
9 | int LocalAddressesInUse { get; }
10 | int PartnerAddressesFree { get; }
11 | int PartnerAddressesInUse { get; }
12 | IDhcpServerScope Scope { get; }
13 | IDhcpServer Server { get; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeOptionValueCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerScopeOptionValueCollection : IEnumerable
6 | {
7 | IDhcpServerScope Scope { get; }
8 | IDhcpServer Server { get; }
9 |
10 | void AddOrSetOptionValue(IDhcpServerOptionValue value);
11 | IDhcpServerOptionValue GetDefaultOptionValue(DhcpServerOptionIds optionId);
12 | IDhcpServerOptionValue GetDefaultOptionValue(int optionId);
13 | IEnumerable GetDefaultOptionValues();
14 | IDhcpServerOptionValue GetOptionValue(IDhcpServerOption option);
15 | IDhcpServerOptionValue GetUserOptionValue(string className, DhcpServerOptionIds optionId);
16 | IDhcpServerOptionValue GetUserOptionValue(string className, int optionId);
17 | IEnumerable GetUserOptionValues(string className);
18 | IDhcpServerOptionValue GetVendorOptionValue(string vendorName, DhcpServerOptionIds optionId);
19 | IDhcpServerOptionValue GetVendorOptionValue(string vendorName, int optionId);
20 | IEnumerable GetVendorOptionValues(string vendorName);
21 | void RemoveOptionValue(DhcpServerOptionIds optionId);
22 | void RemoveOptionValue(IDhcpServerOptionValue value);
23 | void RemoveOptionValue(int optionId);
24 | void RemoveUserOptionValue(string className, DhcpServerOptionIds optionId);
25 | void RemoveUserOptionValue(string className, int optionId);
26 | void RemoveVendorOptionValue(string vendorName, DhcpServerOptionIds optionId);
27 | void RemoveVendorOptionValue(string vendorName, int optionId);
28 | void SetOptionValue(IDhcpServerOptionValue value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeReservation.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerScopeReservation
4 | {
5 | DhcpServerIpAddress Address { get; }
6 | DhcpServerClientTypes AllowedClientTypes { get; }
7 | IDhcpServerClient Client { get; }
8 | DhcpServerHardwareAddress HardwareAddress { get; }
9 | IDhcpServerScopeReservationOptionValueCollection Options { get; }
10 | IDhcpServerScope Scope { get; }
11 | IDhcpServer Server { get; }
12 |
13 | void Delete();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeReservationCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerScopeReservationCollection : IEnumerable
6 | {
7 | IDhcpServerScope Scope { get; }
8 | IDhcpServer Server { get; }
9 |
10 | IDhcpServerScopeReservation AddReservation(IDhcpServerClient client);
11 | IDhcpServerScopeReservation AddReservation(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress);
12 | IDhcpServerScopeReservation AddReservation(DhcpServerIpAddress address, DhcpServerHardwareAddress hardwareAddress, DhcpServerClientTypes allowedClientTypes);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerScopeReservationOptionValueCollection.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Dhcp
4 | {
5 | public interface IDhcpServerScopeReservationOptionValueCollection : IEnumerable
6 | {
7 | IDhcpServerScopeReservation Reservation { get; }
8 | IDhcpServerScope Scope { get; }
9 | IDhcpServer Server { get; }
10 |
11 | void AddOrSetOptionValue(IDhcpServerOptionValue value);
12 | IDhcpServerOptionValue GetDefaultOptionValue(DhcpServerOptionIds optionId);
13 | IDhcpServerOptionValue GetDefaultOptionValue(int optionId);
14 | IEnumerable GetDefaultOptionValues();
15 | IDhcpServerOptionValue GetOptionValue(IDhcpServerOption option);
16 | IDhcpServerOptionValue GetUserOptionValue(string className, DhcpServerOptionIds optionId);
17 | IDhcpServerOptionValue GetUserOptionValue(string className, int optionId);
18 | IEnumerable GetUserOptionValues(string className);
19 | IDhcpServerOptionValue GetVendorOptionValue(string vendorName, DhcpServerOptionIds optionId);
20 | IDhcpServerOptionValue GetVendorOptionValue(string vendorName, int optionId);
21 | IEnumerable GetVendorOptionValues(string vendorName);
22 | void RemoveOptionValue(DhcpServerOptionIds optionId);
23 | void RemoveOptionValue(IDhcpServerOptionValue value);
24 | void RemoveOptionValue(int optionId);
25 | void RemoveUserOptionValue(string className, DhcpServerOptionIds optionId);
26 | void RemoveUserOptionValue(string className, int optionId);
27 | void RemoveVendorOptionValue(string vendorName, DhcpServerOptionIds optionId);
28 | void RemoveVendorOptionValue(string vendorName, int optionId);
29 | void SetOptionValue(IDhcpServerOptionValue value);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Dhcp/IDhcpServerSpecificStrings.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp
2 | {
3 | public interface IDhcpServerSpecificStrings
4 | {
5 | string DefaultUserClassName { get; }
6 | string DefaultVendorClassName { get; }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/Constants.cs:
--------------------------------------------------------------------------------
1 |
2 | namespace Dhcp.Native
3 | {
4 | internal static class Constants
5 | {
6 |
7 | public const uint DHCP_FLAGS_OPTION_IS_VENDOR = 0x03;
8 |
9 | public const uint DHCP_ENDPOINT_FLAG_CANT_MODIFY = 0x01;
10 |
11 | public const uint DNS_FLAG_ENABLED = 0x01;
12 | public const uint DNS_FLAG_UPDATE_DOWNLEVEL = 0x02;
13 | public const uint DNS_FLAG_CLEANUP_EXPIRED = 0x04;
14 | public const uint DNS_FLAG_UPDATE_BOTH_ALWAYS = 0x10;
15 | public const uint DNS_FLAG_UPDATE_DHCID = 0x20;
16 | public const uint DNS_FLAG_DISABLE_PTR_UPDATE = 0x40;
17 |
18 | ///
19 | /// The DHCPv4 client has been offered this IPv4 address.
20 | ///
21 | public const byte ADDRESS_STATE_OFFERED = 0x00;
22 | ///
23 | /// The IPv4 address is active and has an active DHCPv4 client lease record.
24 | ///
25 | public const byte ADDRESS_STATE_ACTIVE = 0x01;
26 | ///
27 | /// The IPv4 address request was declined by the DHCPv4 client; hence, it is a bad IPv4 address.
28 | ///
29 | public const byte ADDRESS_STATE_DECLINED = 0x02;
30 | ///
31 | /// The IPv4 address is in DOOMED state and is due to be deleted.
32 | ///
33 | public const byte ADDRESS_STATE_DOOM = 0x03;
34 |
35 | ///
36 | /// The address is leased to the DHCPv4 client without DHCID (sections 3 and 3.5 of [RFC4701]).
37 | ///
38 | public const byte ADDRESS_BIT_NO_DHCID = 0x00;
39 | ///
40 | /// The address is leased to the DHCPv4 client with DHCID as specified in section 3.5.3 of [RFC4701].
41 | ///
42 | public const byte ADDRESS_BIT_DHCID_NO_CLIENTIDOPTION = 0x01;
43 | ///
44 | /// The address is leased to the DHCPv4 client with DHCID as specified in section 3.5.2 of [RFC4701].
45 | ///
46 | public const byte ADDRESS_BIT_DHCID_WITH_CLIENTIDOPTION = 0x02;
47 | ///
48 | /// The address is leased to the DHCPv4 client with DHCID as specified in section 3.5.1 of [RFC4701].
49 | ///
50 | public const byte ADDRESS_BIT_DHCID_WITH_DUID = 0x03;
51 |
52 | ///
53 | /// The DNS update for the DHCPv4 client lease record needs to be deleted from the DNS server when the lease is deleted.
54 | ///
55 | public const byte ADDRESS_BIT_CLEANUP = 0x10;
56 | ///
57 | /// The DNS update needs to be sent for both A and PTR resource records ([RFC1034] section 3.6).
58 | ///
59 | public const byte ADDRESS_BIT_BOTH_REC = 0x20;
60 | ///
61 | /// The DNS update is not completed for the lease record.
62 | ///
63 | public const byte ADDRESS_BIT_UNREGISTERED = 0x40;
64 | ///
65 | /// The address lease is expired, but the DNS updates for the lease record have not been deleted from the DNS server.
66 | ///
67 | public const byte ADDRESS_BIT_DELETED = 0x80;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DATE_TIME.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DATE_TIME structure defines a 64-bit integer value that contains a date/time, expressed as the number of ticks (100-nanosecond increments) since 12:00 midnight, January 1, 1 C.E. in the Gregorian calendar.
8 | ///
9 | [StructLayout(LayoutKind.Explicit)]
10 | internal readonly struct DATE_TIME
11 | {
12 | [FieldOffset(0)]
13 | public readonly long dwDateTime;
14 |
15 | private DATE_TIME(long fileTimeUtc)
16 | {
17 | dwDateTime = fileTimeUtc;
18 | }
19 |
20 | private DateTime ToDateTime()
21 | {
22 | if (dwDateTime == 0)
23 | return DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
24 | else if (dwDateTime == long.MaxValue)
25 | return DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc);
26 | else
27 | return DateTime.FromFileTimeUtc(dwDateTime);
28 | }
29 |
30 | public static DATE_TIME FromDateTime(DateTime dateTime)
31 | {
32 | if (dateTime == DateTime.MinValue)
33 | return new DATE_TIME(0);
34 | else if (dateTime == DateTime.MaxValue)
35 | return new DATE_TIME(long.MaxValue);
36 | else
37 | return new DATE_TIME(dateTime.ToFileTimeUtc());
38 | }
39 |
40 | public static implicit operator DateTime(DATE_TIME dateTime)
41 | => dateTime.ToDateTime();
42 |
43 | public static implicit operator DATE_TIME(DateTime dateTime)
44 | => FromDateTime(dateTime);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCPDS_SERVER.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCPDS_SERVER structure defines information on a DHCP server in the context of directory services.
7 | ///
8 | [StructLayout(LayoutKind.Sequential)]
9 | internal readonly struct DHCPDS_SERVER
10 | {
11 | ///
12 | /// Reserved. This value should be set to 0.
13 | ///
14 | public readonly uint Version;
15 | ///
16 | /// Unicode string that contains the unique name of the DHCP server.
17 | ///
18 | [MarshalAs(UnmanagedType.LPWStr)]
19 | public readonly string ServerName;
20 | ///
21 | /// Specifies the IP address of the DHCP server as an unsigned 32-bit integer.
22 | ///
23 | public readonly DHCP_IP_ADDRESS ServerAddress;
24 | ///
25 | /// Specifies a set of bit flags that describe active directory settings for the DHCP server.
26 | ///
27 | public readonly uint Flags;
28 | ///
29 | /// Reserved. This value should be set to 0.
30 | ///
31 | public readonly uint State;
32 | ///
33 | /// Unicode string that contains the active directory path to the DHCP server.
34 | ///
35 | [MarshalAs(UnmanagedType.LPWStr)]
36 | public readonly string DsLocation;
37 | ///
38 | /// Reserved. This value should be set to 0.
39 | ///
40 | public readonly uint DsLocType;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCPDS_SERVERS.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCPDS_SERVERS structure defines a list of DHCP servers in the context of directory services.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCPDS_SERVERS
12 | {
13 | ///
14 | /// Reserved. This value should be 0.
15 | ///
16 | public readonly uint Flags;
17 | ///
18 | /// Specifies the number of elements in Servers.
19 | ///
20 | public readonly uint NumElements;
21 | ///
22 | /// Pointer to an array of structures that contain information on individual DHCP servers.
23 | ///
24 | private readonly IntPtr ServersPointer;
25 |
26 | ///
27 | /// Pointer to an array of structures that contain information on individual DHCP servers.
28 | ///
29 | public IEnumerable Servers
30 | {
31 | get
32 | {
33 | var instanceIter = ServersPointer;
34 | var instanceSize = Marshal.SizeOf(typeof(DHCPDS_SERVER));
35 | for (var i = 0; i < NumElements; i++)
36 | {
37 | yield return instanceIter.MarshalToStructure();
38 | instanceIter += instanceSize;
39 | }
40 | }
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_ALL_OPTIONS.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_ALL_OPTIONS structure defines the set of all options available on a DHCP server.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_ALL_OPTIONS : IDisposable
12 | {
13 | ///
14 | /// Reserved. This value should be set to 0.
15 | ///
16 | public readonly int Flags;
17 | ///
18 | /// DHCP_OPTION_ARRAY structure that contains the set of non-vendor options.
19 | ///
20 | private readonly IntPtr NonVendorOptionsPointer;
21 | ///
22 | /// Specifies the number of vendor options listed in VendorOptions.
23 | ///
24 | public readonly int NumVendorOptions;
25 | ///
26 | /// Pointer to a list of structures that contain the following fields.
27 | /// - Option: DHCP_OPTION structure that contains specific information describing the option.
28 | /// - VendorName: Unicode string that contains the name of the vendor for the option.
29 | /// - ClassName: Unicode string that contains the name of the DHCP class for the option.
30 | ///
31 | private readonly IntPtr VendorOptionsPointer;
32 |
33 | ///
34 | /// DHCP_OPTION_ARRAY structure that contains the set of non-vendor options.
35 | ///
36 | public DHCP_OPTION_ARRAY NonVendorOptions => NonVendorOptionsPointer.MarshalToStructure();
37 |
38 | ///
39 | /// Pointer to a list of structures containing DHCP server options and the associated data.
40 | ///
41 | public IEnumerable VendorOptions
42 | {
43 | get
44 | {
45 | if (NumVendorOptions == 0 || VendorOptionsPointer == IntPtr.Zero)
46 | yield break;
47 |
48 | var size = Marshal.SizeOf(typeof(DHCP_VENDOR_OPTION));
49 | var iter = VendorOptionsPointer;
50 | for (var i = 0; i < NumVendorOptions; i++)
51 | {
52 | yield return iter.MarshalToStructure();
53 | iter += size;
54 | }
55 | }
56 | }
57 |
58 | public void Dispose()
59 | {
60 | foreach (var option in VendorOptions)
61 | option.Dispose();
62 | Api.FreePointer(VendorOptionsPointer);
63 |
64 | if (NonVendorOptionsPointer != IntPtr.Zero)
65 | {
66 | NonVendorOptions.Dispose();
67 | Api.FreePointer(NonVendorOptionsPointer);
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_ALL_OPTION_VALUES.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_ALL_OPTION_VALUES structure defines the set of all option values defined on a DHCP server, organized according to class/vendor pairing.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_ALL_OPTION_VALUES : IDisposable
12 | {
13 | ///
14 | /// Reserved. This value should be set to 0.
15 | ///
16 | public readonly int Flags;
17 | ///
18 | /// Specifies the number of elements in Options.
19 | ///
20 | public readonly int NumElements;
21 | ///
22 | /// Pointer to a list of structures that contain the option values for specific class/vendor pairs.
23 | ///
24 | private readonly IntPtr OptionsPointer;
25 |
26 | ///
27 | /// A list of structures containing the option values for specific class/vendor pairs.
28 | ///
29 | public IEnumerable Options
30 | {
31 | get
32 | {
33 | if (NumElements == 0 || OptionsPointer == IntPtr.Zero)
34 | yield break;
35 |
36 | var iter = OptionsPointer;
37 | var size = Marshal.SizeOf(typeof(DHCP_ALL_OPTION_VALUE_ITEM));
38 | for (var i = 0; i < NumElements; i++)
39 | {
40 | yield return iter.MarshalToStructure();
41 | iter += size;
42 | }
43 | }
44 | }
45 |
46 | public void Dispose()
47 | {
48 | foreach (var option in Options)
49 | option.Dispose();
50 |
51 | Api.FreePointer(OptionsPointer);
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_ALL_OPTION_VALUE_ITEM.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_ALL_OPTIONS_VALUE_ITEM structure contain the option values for specific class/vendor pairs.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_ALL_OPTION_VALUE_ITEM : IDisposable
11 | {
12 | ///
13 | /// Unicode string that contains the name of the DHCP class for the option list.
14 | ///
15 | private readonly IntPtr ClassNamePointer;
16 | ///
17 | /// Unicode string that contains the name of the vendor for the option list.
18 | ///
19 | private readonly IntPtr VendorNamePointer;
20 | ///
21 | /// Specifies whether or not this set of options is vendor-specific. This value is TRUE if it is, and FALSE if it is not.
22 | ///
23 | public readonly bool IsVendor;
24 | ///
25 | /// DHCP_OPTION_VALUE_ARRAY structure that contains the option values for the specified vendor/class pair.
26 | ///
27 | private readonly IntPtr OptionsArrayPointer;
28 |
29 | ///
30 | /// Unicode string that contains the name of the DHCP class for the option list.
31 | ///
32 | public string ClassName => Marshal.PtrToStringUni(ClassNamePointer);
33 | ///
34 | /// Unicode string that contains the name of the vendor for the option list.
35 | ///
36 | public string VendorName => Marshal.PtrToStringUni(VendorNamePointer);
37 | ///
38 | /// DHCP_OPTION_VALUE_ARRAY structure that contains the option values for the specified vendor/class pair.
39 | ///
40 | public DHCP_OPTION_VALUE_ARRAY OptionsArray
41 | {
42 | get
43 | {
44 | if (OptionsArrayPointer == IntPtr.Zero)
45 | return default;
46 | else
47 | return OptionsArrayPointer.MarshalToStructure();
48 | }
49 | }
50 |
51 | public void Dispose()
52 | {
53 | Api.FreePointer(ClassNamePointer);
54 | Api.FreePointer(VendorNamePointer);
55 |
56 | if (OptionsArrayPointer != IntPtr.Zero)
57 | {
58 | OptionsArray.Dispose();
59 | Api.FreePointer(OptionsArrayPointer);
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_BIND_ELEMENT.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_BIND_ELEMENT structure defines an individual network binding for the DHCP server. A single DHCP server can contain multiple bindings and serve multiple networks.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_BIND_ELEMENT : IDisposable
11 | {
12 | ///
13 | /// Specifies a set of bit flags indicating properties of the network binding.
14 | /// DHCP_ENDPOINT_FLAG_CANT_MODIFY (0x01) = The binding specified in this structure cannot be modified.
15 | ///
16 | public readonly uint Flags;
17 | ///
18 | /// Specifies whether or not this binding is set on the DHCP server. If TRUE, the binding is set; if FALSE, it is not.
19 | ///
20 | public readonly bool fBoundToDHCPServer;
21 | ///
22 | /// DHCP_IP_ADDRESS value that specifies the IP address assigned to the ethernet adapter of the DHCP server.
23 | ///
24 | public readonly DHCP_IP_ADDRESS AdapterPrimaryAddress;
25 | ///
26 | /// DHCP_IP_ADDRESS value that specifies the subnet IP mask used by this ethernet adapter.
27 | ///
28 | public readonly DHCP_IP_MASK AdapterSubnetAddress;
29 | ///
30 | /// Unicode string that specifies the name assigned to this network interface device.
31 | ///
32 | private readonly IntPtr IfDescriptionPointer;
33 | ///
34 | /// Specifies the size of the network interface device ID, in bytes.
35 | ///
36 | public readonly int IfIdSize;
37 | ///
38 | /// Specifies the network interface device ID.
39 | ///
40 | private readonly IntPtr IfIdPointer;
41 |
42 | ///
43 | /// Unicode string that specifies the name assigned to this network interface device.
44 | ///
45 | public string IfDescription => Marshal.PtrToStringUni(IfDescriptionPointer);
46 | ///
47 | /// Specifies the network interface device ID.
48 | ///
49 | public byte[] IfId
50 | {
51 | get
52 | {
53 | if (IfIdPointer == IntPtr.Zero)
54 | return null;
55 |
56 | var buffer = new byte[IfIdSize];
57 | Marshal.Copy(IfIdPointer, buffer, 0, buffer.Length);
58 | return buffer;
59 | }
60 | }
61 |
62 | public bool IfIdIsGuid => IfIdSize == 16 && IfIdPointer != IntPtr.Zero;
63 |
64 | public Guid IfIdGuid
65 | {
66 | get
67 | {
68 | if (!IfIdIsGuid)
69 | return Guid.Empty;
70 |
71 | return (Guid)Marshal.PtrToStructure(IfIdPointer, typeof(Guid));
72 | }
73 | }
74 |
75 | public void Dispose()
76 | {
77 | Api.FreePointer(IfDescriptionPointer);
78 | Api.FreePointer(IfIdPointer);
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_BIND_ELEMENT_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_BIND_ELEMENT_ARRAY structure defines an array of network binding elements used by a DHCP server.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_BIND_ELEMENT_ARRAY : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of network binding elements listed in Elements.
15 | ///
16 | public readonly int NumElements;
17 | ///
18 | /// Specifies an array of DHCP_BIND_ELEMENT structures
19 | ///
20 | private readonly IntPtr ElementsPointer;
21 |
22 | ///
23 | /// Specifies an array of DHCP_BIND_ELEMENT structures
24 | ///
25 | public IEnumerable Elements
26 | {
27 | get
28 | {
29 | if (NumElements == 0 || ElementsPointer == IntPtr.Zero)
30 | yield break;
31 |
32 | var iter = ElementsPointer;
33 | var size = Marshal.SizeOf(typeof(DHCP_BIND_ELEMENT));
34 | for (var i = 0; i < NumElements; i++)
35 | {
36 | yield return iter.MarshalToStructure();
37 | iter += size;
38 | }
39 | }
40 | }
41 |
42 | public void Dispose()
43 | {
44 | foreach (var element in Elements)
45 | element.Dispose();
46 |
47 | Api.FreePointer(ElementsPointer);
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_BOOTP_IP_RANGE.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCP_BOOTP_IP_RANGE structure defines a suite of IPs for lease to BOOTP-specific clients.
7 | ///
8 | [StructLayout(LayoutKind.Sequential)]
9 | internal readonly struct DHCP_BOOTP_IP_RANGE
10 | {
11 | ///
12 | /// DHCP_IP_ADDRESS value that specifies the start of the IP range used for BOOTP service.
13 | ///
14 | public readonly DHCP_IP_ADDRESS StartAddress;
15 | ///
16 | /// DHCP_IP_ADDRESS value that specifies the end of the IP range used for BOOTP service.
17 | ///
18 | public readonly DHCP_IP_ADDRESS EndAddress;
19 | ///
20 | /// Specifies the number of BOOTP clients with addresses served from this range.
21 | ///
22 | public readonly int BootpAllocated;
23 | ///
24 | /// Specifies the maximum number of BOOTP clients this range is allowed to serve.
25 | ///
26 | public readonly int MaxBootpAllowed;
27 |
28 | public DHCP_BOOTP_IP_RANGE(DHCP_IP_ADDRESS startAddress, DHCP_IP_ADDRESS endAddress, int bootpAllocated, int maxBootpAllowed)
29 | {
30 | StartAddress = startAddress;
31 | EndAddress = endAddress;
32 | BootpAllocated = bootpAllocated;
33 | MaxBootpAllowed = maxBootpAllowed;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLASS_INFO_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_CLASS_INFO_ARRAY structure defines an array of elements that contain DHCP class information.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_CLASS_INFO_ARRAY : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of elements in Classes.
15 | ///
16 | public readonly int NumElements;
17 | ///
18 | /// Pointer to an array of structures that contain DHCP class information.
19 | ///
20 | private readonly IntPtr ClassesPointer;
21 |
22 | ///
23 | /// Pointer to an array of structures that contain DHCP class information.
24 | ///
25 | public IEnumerable Classes
26 | {
27 | get
28 | {
29 | if (NumElements == 0 || ClassesPointer == IntPtr.Zero)
30 | yield break;
31 |
32 | var iter = ClassesPointer;
33 | var size = Marshal.SizeOf(typeof(DHCP_CLASS_INFO));
34 | for (var i = 0; i < NumElements; i++)
35 | {
36 | yield return iter.MarshalToStructure();
37 | iter += size;
38 | }
39 | }
40 | }
41 |
42 | public void Dispose()
43 | {
44 | foreach (var @class in Classes)
45 | @class.Dispose();
46 |
47 | Api.FreePointer(ClassesPointer);
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_INFO_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | [StructLayout(LayoutKind.Sequential)]
8 | internal readonly struct DHCP_CLIENT_INFO_ARRAY : IDisposable
9 | {
10 | ///
11 | /// Specifies the number of elements present in Clients.
12 | ///
13 | public readonly int NumElements;
14 | ///
15 | /// Pointer to a list of DHCP_CLIENT_INFO structures that contain information on specific DHCP subnet clients.).
16 | ///
17 | private readonly IntPtr ClientsPointer;
18 |
19 | ///
20 | /// Pointer to a list of DHCP_CLIENT_INFO structures that contain information on specific DHCP subnet clients.).
21 | ///
22 | public IEnumerable Clients
23 | {
24 | get
25 | {
26 | if (NumElements == 0 || ClientsPointer == IntPtr.Zero)
27 | yield break;
28 |
29 | var iter = ClientsPointer;
30 | for (var i = 0; i < NumElements; i++)
31 | {
32 | var clientPtr = Marshal.ReadIntPtr(iter);
33 | yield return new ClientTuple()
34 | {
35 | Pointer = clientPtr,
36 | Value = clientPtr.MarshalToStructure()
37 | };
38 | iter += IntPtr.Size;
39 | }
40 | }
41 | }
42 |
43 | public void Dispose()
44 | {
45 | foreach (var client in Clients)
46 | {
47 | client.Value.Dispose();
48 | Api.FreePointer(client.Pointer);
49 | }
50 |
51 | Api.FreePointer(ClientsPointer);
52 | }
53 |
54 | internal struct ClientTuple
55 | {
56 | public IntPtr Pointer;
57 | public DHCP_CLIENT_INFO Value;
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_INFO_ARRAY_V4.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_CLIENT_INFO_ARRAY_V4 structure defines an array of DHCP_CLIENT_INFO_V4 structures for use with enumeration functions.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_CLIENT_INFO_ARRAY_V4 : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of elements present in Clients.
15 | ///
16 | public readonly int NumElements;
17 | ///
18 | /// Pointer to a list of DHCP_CLIENT_INFO_V4 structures that contain information on specific DHCP subnet clients, including the dynamic address type (DHCP and/or BOOTP).
19 | ///
20 | private readonly IntPtr ClientsPointer;
21 |
22 | ///
23 | /// Pointer to a list of DHCP_CLIENT_INFO_V4 structures that contain information on specific DHCP subnet clients, including the dynamic address type (DHCP and/or BOOTP).
24 | ///
25 | public IEnumerable Clients
26 | {
27 | get
28 | {
29 | if (NumElements == 0 || ClientsPointer == IntPtr.Zero)
30 | yield break;
31 |
32 | var iter = ClientsPointer;
33 | for (var i = 0; i < NumElements; i++)
34 | {
35 | var clientPtr = Marshal.ReadIntPtr(iter);
36 | yield return new ClientTuple()
37 | {
38 | Pointer = clientPtr,
39 | Value = clientPtr.MarshalToStructure()
40 | };
41 | iter += IntPtr.Size;
42 | }
43 | }
44 | }
45 |
46 | public void Dispose()
47 | {
48 | foreach (var client in Clients)
49 | {
50 | client.Value.Dispose();
51 | Api.FreePointer(client.Pointer);
52 | }
53 |
54 | Api.FreePointer(ClientsPointer);
55 | }
56 |
57 | internal struct ClientTuple
58 | {
59 | public IntPtr Pointer;
60 | public DHCP_CLIENT_INFO_V4 Value;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_INFO_ARRAY_V5.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_CLIENT_INFO_ARRAY_V5 structure defines an array of DHCP_CLIENT_INFO_V5 structures for use with enumeration functions.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_CLIENT_INFO_ARRAY_V5 : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of elements present in Clients.
15 | ///
16 | public readonly int NumElements;
17 | ///
18 | /// Pointer to a list of DHCP_CLIENT_INFO_V5 structures that contain information on specific DHCP subnet clients, including the dynamic address type (DHCP and/or BOOTP) and address state information.
19 | ///
20 | private readonly IntPtr ClientsPointer;
21 |
22 | ///
23 | /// Pointer to a list of DHCP_CLIENT_INFO_V5 structures that contain information on specific DHCP subnet clients, including the dynamic address type (DHCP and/or BOOTP) and address state information.
24 | ///
25 | public IEnumerable Clients
26 | {
27 | get
28 | {
29 | if (NumElements == 0 || ClientsPointer == IntPtr.Zero)
30 | yield break;
31 |
32 | var iter = ClientsPointer;
33 | for (var i = 0; i < NumElements; i++)
34 | {
35 | var clientPtr = Marshal.ReadIntPtr(iter);
36 | yield return new ClientTuple()
37 | {
38 | Pointer = clientPtr,
39 | Value = clientPtr.MarshalToStructure()
40 | };
41 | iter += IntPtr.Size;
42 | }
43 | }
44 | }
45 |
46 | public void Dispose()
47 | {
48 | foreach (var client in Clients)
49 | {
50 | client.Value.Dispose();
51 | Api.FreePointer(client.Pointer);
52 | }
53 |
54 | Api.FreePointer(ClientsPointer);
55 | }
56 |
57 | internal struct ClientTuple
58 | {
59 | public IntPtr Pointer;
60 | public DHCP_CLIENT_INFO_V5 Value;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_INFO_ARRAY_VQ.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_CLIENT_INFO_ARRAY_VQ structure specifies an array of DHCP_CLIENT_INFO_VQ structures.
9 | ///
10 | internal readonly struct DHCP_CLIENT_INFO_ARRAY_VQ : IDisposable
11 | {
12 | #pragma warning disable CS0649
13 | ///
14 | /// The number of elements in the array.
15 | ///
16 | public readonly int NumElements;
17 | #pragma warning restore CS0649
18 |
19 | ///
20 | /// Pointer to the first element in the array of DHCP_CLIENT_INFO_VQ structures.
21 | ///
22 | private readonly IntPtr ClientsPointer;
23 |
24 | public IEnumerable Clients
25 | {
26 | get
27 | {
28 | if (NumElements == 0 || ClientsPointer == IntPtr.Zero)
29 | yield break;
30 |
31 | var iter = ClientsPointer;
32 | for (var i = 0; i < NumElements; i++)
33 | {
34 | var clientPtr = Marshal.ReadIntPtr(iter);
35 | yield return new ClientTuple()
36 | {
37 | Pointer = clientPtr,
38 | Value = clientPtr.MarshalToStructure()
39 | };
40 | iter += IntPtr.Size;
41 | }
42 | }
43 | }
44 |
45 | public void Dispose()
46 | {
47 | foreach (var client in Clients)
48 | {
49 | client.Value.Dispose();
50 | Api.FreePointer(client.Pointer);
51 | }
52 |
53 | Api.FreePointer(ClientsPointer);
54 | }
55 |
56 | internal struct ClientTuple
57 | {
58 | public IntPtr Pointer;
59 | public DHCP_CLIENT_INFO_VQ Value;
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_INFO_V4.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_CLIENT_INFO_V4 structure defines a client information record used by the DHCP server, extending the definition provided in DHCP_CLIENT_INFO by including client type information.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_CLIENT_INFO_V4 : IDisposable
11 | {
12 | ///
13 | /// DHCP_IP_ADDRESS value that contains the assigned IP address of the DHCP client.
14 | ///
15 | public readonly DHCP_IP_ADDRESS ClientIpAddress;
16 | ///
17 | /// DHCP_IP_MASK value that contains the subnet mask value assigned to the DHCP client.
18 | ///
19 | public readonly DHCP_IP_MASK SubnetMask;
20 | ///
21 | /// DHCP_CLIENT_UID structure containing the MAC address of the client's network interface device.
22 | ///
23 | public readonly DHCP_BINARY_DATA ClientHardwareAddress;
24 | ///
25 | /// Unicode string that specifies the network name of the DHCP client. This member is optional.
26 | ///
27 | private readonly IntPtr ClientNamePointer;
28 | ///
29 | /// Unicode string that contains a comment associated with the DHCP client. This member is optional.
30 | ///
31 | private readonly IntPtr ClientCommentPointer;
32 | ///
33 | /// DATE_TIME structure that contains the date and time the DHCP client lease will expire, in UTC time.
34 | ///
35 | public readonly DATE_TIME ClientLeaseExpires;
36 | ///
37 | /// DHCP_HOST_INFO structure that contains information on the DHCP server that assigned the IP address to the client.
38 | ///
39 | public readonly DHCP_HOST_INFO OwnerHost;
40 | ///
41 | /// Specifies the types of dynamic IP address service used by the client.
42 | ///
43 | public readonly DHCP_CLIENT_TYPE bClientType;
44 |
45 | ///
46 | /// Unicode string that specifies the network name of the DHCP client. This member is optional.
47 | ///
48 | public string ClientName => Marshal.PtrToStringUni(ClientNamePointer);
49 |
50 | ///
51 | /// Unicode string that contains a comment associated with the DHCP client. This member is optional.
52 | ///
53 | public string ClientComment => Marshal.PtrToStringUni(ClientCommentPointer);
54 |
55 | public void Dispose()
56 | {
57 | ClientHardwareAddress.Dispose();
58 | Api.FreePointer(ClientNamePointer);
59 | Api.FreePointer(ClientCommentPointer);
60 | OwnerHost.Dispose();
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_INFO_V5.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_CLIENT_INFO_V5 structure defines a client information record used by the DHCP server, extending the definition provided in DHCP_CLIENT_INFO by including client type and address state information.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_CLIENT_INFO_V5 : IDisposable
11 | {
12 | ///
13 | /// DHCP_IP_ADDRESS value that contains the assigned IP address of the DHCP client.
14 | ///
15 | public readonly DHCP_IP_ADDRESS ClientIpAddress;
16 | ///
17 | /// DHCP_IP_MASK value that contains the subnet mask value assigned to the DHCP client.
18 | ///
19 | public readonly DHCP_IP_MASK SubnetMask;
20 | ///
21 | /// DHCP_CLIENT_UID structure containing the MAC address of the client's network interface device.
22 | ///
23 | public readonly DHCP_BINARY_DATA ClientHardwareAddress;
24 | ///
25 | /// Pointer to a Unicode string that specifies the network name of the DHCP client. This member is optional.
26 | ///
27 | private readonly IntPtr ClientNamePointer;
28 | ///
29 | /// Pointer to a Unicode string that contains a comment associated with the DHCP client. This member is optional.
30 | ///
31 | private readonly IntPtr ClientCommentPointer;
32 | ///
33 | /// DATE_TIME structure that contains the date and time the DHCP client lease will expire, in UTC time.
34 | ///
35 | public readonly DATE_TIME ClientLeaseExpires;
36 | ///
37 | /// DHCP_HOST_INFO structure that contains information on the DHCP server that assigned the IP address to the client.
38 | ///
39 | public readonly DHCP_HOST_INFO OwnerHost;
40 | ///
41 | /// Specifies the types of dynamic IP address service used by the client.
42 | ///
43 | public readonly DHCP_CLIENT_TYPE bClientType;
44 | ///
45 | /// Specifies the current state of the client IP address.
46 | ///
47 | public readonly byte AddressState;
48 |
49 | ///
50 | /// Pointer to a Unicode string that specifies the network name of the DHCP client. This member is optional.
51 | ///
52 | public string ClientName => Marshal.PtrToStringUni(ClientNamePointer);
53 |
54 | ///
55 | /// Pointer to a Unicode string that contains a comment associated with the DHCP client. This member is optional.
56 | ///
57 | public string ClientComment => Marshal.PtrToStringUni(ClientCommentPointer);
58 |
59 | public void Dispose()
60 | {
61 | ClientHardwareAddress.Dispose();
62 | Api.FreePointer(ClientNamePointer);
63 | Api.FreePointer(ClientCommentPointer);
64 | OwnerHost.Dispose();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_CLIENT_TYPE.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// Possible types of the DHCPv4 client.
7 | ///
8 | [Flags]
9 | internal enum DHCP_CLIENT_TYPE : byte
10 | {
11 | ///
12 | /// A DHCPv4 client other than ones defined in this table.
13 | ///
14 | CLIENT_TYPE_UNSPECIFIED = 0x00,
15 | ///
16 | /// The DHCPv4 client supports the DHCP protocol.
17 | ///
18 | CLIENT_TYPE_DHCP = 0x01,
19 | ///
20 | /// The DHCPv4 client supports the BOOTP protocol.
21 | ///
22 | CLIENT_TYPE_BOOTP = 0x02,
23 | ///
24 | /// The DHCPv4 client understands both the DHCPv4 and the BOOTP protocols.
25 | ///
26 | CLIENT_TYPE_BOTH = 0x03,
27 | ///
28 | /// There is an IPv4 reservation created for the DHCPv4 client.
29 | ///
30 | CLIENT_TYPE_RESERVATION_FLAG = 0x04,
31 | ///
32 | /// Backward compatibility for manual addressing.
33 | ///
34 | CLIENT_TYPE_NONE = 0x64,
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_FAILOVER_MODE.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The DHCP_FAILOVER_MODE enumeration defines the DHCPv4 server mode operation in a failover relationship.
5 | ///
6 | internal enum DHCP_FAILOVER_MODE
7 | {
8 | ///
9 | /// The DHCPv4 server failover relationship is in Load Balancing mode.
10 | ///
11 | LoadBalance,
12 | ///
13 | /// The DHCPv4 server failover relationship is in Hot Standby mode.
14 | ///
15 | HotStandby
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_FAILOVER_RELATIONSHIP_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | internal readonly struct DHCP_FAILOVER_RELATIONSHIP_ARRAY : IDisposable
8 | {
9 | ///
10 | /// This member is of type DWORD and contains the number of DHCP_FAILOVER_RELATIONSHIP elements specified in the subsequent pRelationships field.
11 | ///
12 | public readonly int NumElements;
13 | ///
14 | /// This member is a pointer to an array of DHCP_FAILOVER_RELATIONSHIP structures of length numElements and contains failover relationship information.
15 | ///
16 | private readonly IntPtr RelationshipsPointer;
17 |
18 | public IEnumerable Relationships
19 | {
20 | get
21 | {
22 | if (NumElements == 0 || RelationshipsPointer == IntPtr.Zero)
23 | yield break;
24 |
25 | var iter = RelationshipsPointer;
26 | var size = Marshal.SizeOf(typeof(DHCP_FAILOVER_RELATIONSHIP));
27 | for (var i = 0; i < NumElements; i++)
28 | {
29 | yield return iter.MarshalToStructure();
30 | iter += size;
31 | }
32 | }
33 | }
34 |
35 | public void Dispose()
36 | {
37 | foreach (var relationship in Relationships)
38 | relationship.Dispose();
39 |
40 | Api.FreePointer(RelationshipsPointer);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_FAILOVER_RELATIONSHIP_SET_FLAGS.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// Flags used by DhcpV4FailoverSetRelationship
7 | ///
8 | [Flags]
9 | internal enum DHCP_FAILOVER_RELATIONSHIP_SET_FLAGS
10 | {
11 | ///
12 | /// The mclt member in pRelationship parameter structure is populated.
13 | ///
14 | MCLT = 0x00000001,
15 | ///
16 | /// The safePeriod member in pRelationship parameter structure is populated.
17 | ///
18 | SAFEPERIOD = 0x00000002,
19 | ///
20 | /// The state member in pRelationship parameter structure is populated.
21 | ///
22 | CHANGESTATE = 0x00000004,
23 | ///
24 | /// The percentage member in pRelationship parameter structure is populated.
25 | ///
26 | PERCENTAGE = 0x00000008,
27 | ///
28 | /// The mode member in pRelationship parameter structure is populated.
29 | ///
30 | MODE = 0x00000010,
31 | ///
32 | /// The prevState member in pRelationship parameter structure is populated.
33 | ///
34 | PREVSTATE = 0x00000020,
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_FAILOVER_SERVER.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The DHCP_FAILOVER_SERVER enumeration defines whether the DHCP server is the primary or secondary server in a DHCPv4 failover relationship.
5 | ///
6 | internal enum DHCP_FAILOVER_SERVER
7 | {
8 | ///
9 | /// The server is a primary server in the failover relationship.
10 | ///
11 | PrimaryServer,
12 | ///
13 | /// The server is a secondary server in the failover relationship.
14 | ///
15 | SecondaryServer
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_FAILOVER_STATISTICS.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The DHCP_FAILOVER_STATISTICS structure defines DHCP server scope statistics that are part of a failover relationship.
5 | ///
6 | internal readonly struct DHCP_FAILOVER_STATISTICS
7 | {
8 | ///
9 | /// This member is of type DWORD and contains the total number of IPv4 addresses that can be leased out to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship.
10 | ///
11 | public readonly int NumAddr;
12 | ///
13 | /// This member is of type DWORD and contains the total number of IPv4 addresses that are free and can be leased to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship.
14 | ///
15 | public readonly int AddrFree;
16 | ///
17 | /// This member is of type DWORD and contains the total number of IPv4 addresses leased to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship.
18 | ///
19 | public readonly int AddrInUse;
20 | ///
21 | /// This member is of type DWORD and contains the total number of IPv4 addresses that are free and can be leased to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship on the partner server.
22 | ///
23 | public readonly int PartnerAddrFree;
24 | ///
25 | /// This member is of type DWORD and contains the total number of IPv4 addresses that are free and can be leased to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship on the local DHCP server.
26 | ///
27 | public readonly int ThisAddrFree;
28 | ///
29 | /// This member is of type DWORD and contains the total number of IPv4 addresses leased to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship on the partner server.
30 | ///
31 | public readonly int PartnerAddrInUse;
32 | ///
33 | /// This member is of type DWORD and contains the total number of IPv4 addresses leased to DHCPv4 clients on an IPv4 subnet that is part of a failover relationship on the local DHCP server.
34 | ///
35 | public readonly int ThisAddrInUse;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_FORCE_FLAG.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | internal enum DHCP_FORCE_FLAG
4 | {
5 | ///
6 | /// The operation deletes all client records affected by the element, and then deletes the element.
7 | ///
8 | DhcpFullForce,
9 | ///
10 | /// The operation only deletes the subnet element, leaving intact any client records impacted by the change.
11 | ///
12 | DhcpNoForce,
13 | ///
14 | /// The operation deletes all client records affected by the element, and then deletes the element from the DHCP server.
15 | /// But it does not delete any registered DNS records associated with the deleted client records from the DNS server.
16 | /// This flag is only valid when passed to DhcpDeleteSubnet.
17 | /// Note that the minimum server OS requirement for this value is Windows Server 2012 R2 with KB 3100473 installed.
18 | ///
19 | DhcpFailoverForce
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_HOST_INFO.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_HOST_INFO structure defines information on a DHCP server (host).
8 | ///
9 | ///
10 | /// When this structure is populated by the DHCP Server, the HostName and NetBiosName members may be set to NULL.
11 | ///
12 | [StructLayout(LayoutKind.Sequential)]
13 | internal readonly struct DHCP_HOST_INFO : IDisposable
14 | {
15 | ///
16 | /// DHCP_IP_ADDRESS value that contains the IP address of the DHCP server.
17 | ///
18 | public readonly DHCP_IP_ADDRESS IpAddress;
19 | ///
20 | /// Unicode string that contains the NetBIOS name of the DHCP server.
21 | ///
22 | private readonly IntPtr NetBiosNamePointer;
23 | ///
24 | /// Unicode string that contains the network name of the DHCP server.
25 | ///
26 | private readonly IntPtr ServerNamePointer;
27 |
28 | ///
29 | /// Unicode string that contains the NetBIOS name of the DHCP server.
30 | ///
31 | public string NetBiosName => Marshal.PtrToStringUni(NetBiosNamePointer);
32 |
33 | ///
34 | /// Unicode string that contains the network name of the DHCP server.
35 | ///
36 | public string ServerName => Marshal.PtrToStringUni(ServerNamePointer);
37 |
38 | public void Dispose()
39 | {
40 | Api.FreePointer(NetBiosNamePointer);
41 |
42 | // Freeing ServerName causes heap corruption ?!?!?
43 | // Api.FreePointer(ServerNamePointer);
44 | }
45 | }
46 |
47 | ///
48 | /// The DHCP_HOST_INFO structure defines information on a DHCP server (host).
49 | ///
50 | ///
51 | /// When this structure is populated by the DHCP Server, the HostName and NetBiosName members may be set to NULL.
52 | ///
53 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
54 | internal readonly struct DHCP_HOST_INFO_Managed
55 | {
56 | ///
57 | /// DHCP_IP_ADDRESS value that contains the IP address of the DHCP server.
58 | ///
59 | public readonly DHCP_IP_ADDRESS IpAddress;
60 | ///
61 | /// Unicode string that contains the NetBIOS name of the DHCP server.
62 | ///
63 | private readonly string NetBiosName;
64 | ///
65 | /// Unicode string that contains the network name of the DHCP server.
66 | ///
67 | private readonly string ServerName;
68 |
69 | public DHCP_HOST_INFO_Managed(DHCP_IP_ADDRESS ipAddress, string netBiosName, string serverName)
70 | {
71 | IpAddress = ipAddress;
72 | NetBiosName = netBiosName;
73 | ServerName = serverName;
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_IP_ADDRESS.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | [StructLayout(LayoutKind.Sequential, Size = 4)]
6 | internal readonly struct DHCP_IP_ADDRESS
7 | {
8 | private readonly uint ipAddress;
9 |
10 | public DHCP_IP_ADDRESS(uint IpAddress)
11 | {
12 | ipAddress = IpAddress;
13 | }
14 |
15 | public static DHCP_IP_ADDRESS FromString(string IpAddress) => new DHCP_IP_ADDRESS(BitHelper.StringToIpAddress(IpAddress));
16 | public override string ToString() => BitHelper.IpAddressToString(ipAddress);
17 |
18 | public DhcpServerIpAddress AsHostToIpAddress() => new DhcpServerIpAddress(BitHelper.HostToNetworkOrder(ipAddress));
19 | public DhcpServerIpAddress AsNetworkToIpAddress() => new DhcpServerIpAddress(ipAddress);
20 |
21 | public static explicit operator DHCP_IP_ADDRESS(int ipAddress) => new DHCP_IP_ADDRESS((uint)ipAddress);
22 | public static explicit operator int(DHCP_IP_ADDRESS ipAddress) => (int)ipAddress.ipAddress;
23 |
24 | public static DHCP_IP_ADDRESS operator &(DHCP_IP_ADDRESS address, DHCP_IP_MASK mask)
25 | => (DHCP_IP_ADDRESS)((int)address & (int)mask);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_IP_CLUSTER.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCP_IP_CLUSTER structure defines the address and mast for a network cluster.
7 | ///
8 | [StructLayout(LayoutKind.Sequential)]
9 | internal readonly struct DHCP_IP_CLUSTER
10 | {
11 | ///
12 | /// DHCP_IP_ADDRESS value that contains the IP address of the cluster.
13 | ///
14 | public readonly DHCP_IP_ADDRESS ClusterAddress;
15 | ///
16 | /// Specifies the mask value for a cluster. This value should be set to 0xFFFFFFFF if the cluster is full.
17 | ///
18 | public readonly uint ClusterMask;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_IP_MASK.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | [StructLayout(LayoutKind.Sequential, Size = 4)]
6 | internal readonly struct DHCP_IP_MASK
7 | {
8 | private readonly uint ipMask;
9 |
10 | public DHCP_IP_MASK(uint IpMask)
11 | {
12 | ipMask = IpMask;
13 | }
14 |
15 | public override string ToString() => BitHelper.IpAddressToString(ipMask);
16 |
17 | public static explicit operator uint(DHCP_IP_MASK ipMask) => ipMask.ipMask;
18 | public static explicit operator DHCP_IP_MASK(uint ipMask) => new DHCP_IP_MASK(ipMask);
19 | public static explicit operator int(DHCP_IP_MASK ipMask) => (int)ipMask.ipMask;
20 | public static explicit operator DHCP_IP_MASK(int ipMask) => new DHCP_IP_MASK((uint)ipMask);
21 |
22 | public DhcpServerIpMask AsHostToIpMask() => new DhcpServerIpMask(BitHelper.HostToNetworkOrder(ipMask));
23 |
24 | public DhcpServerIpMask AsNetworkToIpMask() => new DhcpServerIpMask(ipMask);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_IP_RANGE.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCP_IP_RANGE structure defines a range of IP addresses.
7 | ///
8 | [StructLayout(LayoutKind.Sequential)]
9 | internal readonly struct DHCP_IP_RANGE
10 | {
11 | ///
12 | /// DHCP_IP_ADDRESS value that contains the first IP address in the range.
13 | ///
14 | public readonly DHCP_IP_ADDRESS StartAddress;
15 | ///
16 | /// DHCP_IP_ADDRESS value that contains the last IP address in the range.
17 | ///
18 | public readonly DHCP_IP_ADDRESS EndAddress;
19 |
20 | public DHCP_IP_RANGE(DHCP_IP_ADDRESS StartAddress, DHCP_IP_ADDRESS EndAddress)
21 | {
22 | this.StartAddress = StartAddress;
23 | this.EndAddress = EndAddress;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_IP_RESERVATION.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_IP_RESERVATION structure defines a client IP reservation.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_IP_RESERVATION : IDisposable
11 | {
12 | ///
13 | /// DHCP_IP_ADDRESS value that contains the reserved IP address.
14 | ///
15 | public readonly DHCP_IP_ADDRESS ReservedIpAddress;
16 | ///
17 | /// DHCP_CLIENT_UID structure that contains information on the client holding this IP reservation.
18 | ///
19 | private readonly IntPtr ReservedForClientPointer;
20 |
21 | ///
22 | /// DHCP_CLIENT_UID structure that contains information on the client holding this IP reservation.
23 | ///
24 | public DHCP_CLIENT_UID ReservedForClient => ReservedForClientPointer.MarshalToStructure();
25 |
26 | public void Dispose()
27 | {
28 | if (ReservedForClientPointer != IntPtr.Zero)
29 | {
30 | ReservedForClient.Dispose();
31 | Api.FreePointer(ReservedForClientPointer);
32 | }
33 | }
34 | }
35 |
36 | ///
37 | /// The DHCP_IP_RESERVATION structure defines a client IP reservation.
38 | ///
39 | [StructLayout(LayoutKind.Sequential)]
40 | internal readonly struct DHCP_IP_RESERVATION_Managed : IDisposable
41 | {
42 | ///
43 | /// DHCP_IP_ADDRESS value that contains the reserved IP address.
44 | ///
45 | public readonly DHCP_IP_ADDRESS ReservedIpAddress;
46 | ///
47 | /// DHCP_CLIENT_UID structure that contains information on the client holding this IP reservation.
48 | ///
49 | private readonly IntPtr ReservedForClientPointer;
50 |
51 | public DHCP_IP_RESERVATION_Managed(DHCP_IP_ADDRESS reservedIpAddress, DHCP_CLIENT_UID_Managed reservedForClient)
52 | {
53 | ReservedIpAddress = reservedIpAddress;
54 | ReservedForClientPointer = Marshal.AllocHGlobal(Marshal.SizeOf(reservedForClient));
55 | Marshal.StructureToPtr(reservedForClient, ReservedForClientPointer, false);
56 | }
57 |
58 | public DHCP_IP_RESERVATION_Managed(DhcpServerIpAddress address, DhcpServerHardwareAddress reservedForClient)
59 | : this(reservedIpAddress: address.ToNativeAsNetwork(),
60 | reservedForClient: reservedForClient.ToNativeClientUid())
61 | { }
62 |
63 | public void Dispose()
64 | {
65 | if (ReservedForClientPointer != IntPtr.Zero)
66 | {
67 | var reservedForClient = ReservedForClientPointer.MarshalToStructure();
68 | reservedForClient.Dispose();
69 |
70 | Marshal.FreeHGlobal(ReservedForClientPointer);
71 | }
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_OPTION structure defines a single DHCP option and any data elements associated with it.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_OPTION : IDisposable
11 | {
12 | ///
13 | /// DHCP_OPTION_ID value that specifies a unique ID number (also called a "code") for this option.
14 | ///
15 | public readonly int OptionID;
16 | ///
17 | /// Unicode string that contains the name of this option.
18 | ///
19 | private readonly IntPtr OptionNamePointer;
20 | ///
21 | /// Unicode string that contains a comment about this option.
22 | ///
23 | private readonly IntPtr OptionCommentPointer;
24 | ///
25 | /// structure that contains the data associated with this option.
26 | ///
27 | public readonly DHCP_OPTION_DATA DefaultValue;
28 | ///
29 | /// DHCP_OPTION_TYPE enumeration value that indicates whether this option is a single unary item or an element in an array of options.
30 | ///
31 | public readonly DHCP_OPTION_TYPE OptionType;
32 |
33 | ///
34 | /// Unicode string that contains the name of this option.
35 | ///
36 | public string OptionName => Marshal.PtrToStringUni(OptionNamePointer);
37 | ///
38 | /// Unicode string that contains a comment about this option.
39 | ///
40 | public string OptionComment => Marshal.PtrToStringUni(OptionCommentPointer);
41 |
42 | public void Dispose()
43 | {
44 | Api.FreePointer(OptionNamePointer);
45 | Api.FreePointer(OptionCommentPointer);
46 |
47 | DefaultValue.Dispose();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_OPTION_ARRAY structure defines an array of DHCP server options.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_OPTION_ARRAY : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of option elements in Options.
15 | ///
16 | public readonly int NumElements;
17 | ///
18 | /// Pointer to a list of structures containing DHCP server options and the associated data.
19 | ///
20 | private readonly IntPtr OptionsPointer;
21 |
22 | ///
23 | /// Pointer to a list of structures containing DHCP server options and the associated data.
24 | ///
25 | public IEnumerable Options
26 | {
27 | get
28 | {
29 | if (NumElements == 0 || OptionsPointer == IntPtr.Zero)
30 | yield break;
31 |
32 | var iter = OptionsPointer;
33 | var size = Marshal.SizeOf(typeof(DHCP_OPTION));
34 | for (var i = 0; i < NumElements; i++)
35 | {
36 | yield return iter.MarshalToStructure();
37 | iter += size;
38 | }
39 | }
40 | }
41 |
42 | public void Dispose()
43 | {
44 | foreach (var option in Options)
45 | option.Dispose();
46 |
47 | Api.FreePointer(OptionsPointer);
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION_DATA_TYPE.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The DHCP_OPTION_DATA_TYPE enumeration defines the set of formats that represent DHCP option data.
5 | ///
6 | internal enum DHCP_OPTION_DATA_TYPE : uint
7 | {
8 | ///
9 | /// The option data is stored as a BYTE value.
10 | ///
11 | DhcpByteOption = 0,
12 | ///
13 | /// The option data is stored as a WORD value.
14 | ///
15 | DhcpWordOption,
16 | ///
17 | /// The option data is stored as a DWORD value.
18 | ///
19 | DhcpDWordOption,
20 | ///
21 | /// The option data is stored as a DWORD_DWORD value.
22 | ///
23 | DhcpDWordDWordOption,
24 | ///
25 | /// The option data is an IP address, stored as a DHCP_IP_ADDRESS value (DWORD).
26 | ///
27 | DhcpIpAddressOption,
28 | ///
29 | /// The option data is stored as a Unicode string.
30 | ///
31 | DhcpStringDataOption,
32 | ///
33 | /// The option data is stored as a DHCP_BINARY_DATA structure.
34 | ///
35 | DhcpBinaryDataOption,
36 | ///
37 | /// The option data is encapsulated and stored as a DHCP_BINARY_DATA structure.
38 | ///
39 | DhcpEncapsulatedDataOption,
40 | ///
41 | /// The option data is stored as a Unicode string.
42 | ///
43 | DhcpIpv6AddressOption
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION_SCOPE_TYPE.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCP_OPTION_SCOPE_TYPE enumeration defines the set of possible DHCP option scopes.
7 | ///
8 | internal enum DHCP_OPTION_SCOPE_TYPE : uint
9 | {
10 | ///
11 | /// The DHCP options correspond to the default scope.
12 | ///
13 | [Obsolete]
14 | DhcpDefaultOptions,
15 | ///
16 | /// The DHCP options correspond to the global scope.
17 | ///
18 | DhcpGlobalOptions,
19 | ///
20 | /// The DHCP options correspond to a specific subnet scope.
21 | ///
22 | DhcpSubnetOptions,
23 | ///
24 | /// The DHCP options correspond to a reserved IP address.
25 | ///
26 | DhcpReservedOptions,
27 | ///
28 | /// The DHCP options correspond to a multicast scope.
29 | ///
30 | DhcpMScopeOptions
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION_TYPE.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The DHCP_OPTION_TYPE enumeration defines the set of possible DHCP option types.
5 | ///
6 | internal enum DHCP_OPTION_TYPE : uint
7 | {
8 | ///
9 | /// The option has a single data item associated with it.
10 | ///
11 | DhcpUnaryElementTypeOption,
12 | ///
13 | /// The option is an array of data items associated with it.
14 | ///
15 | DhcpArrayTypeOption
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION_VALUE.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_OPTION_VALUE structure defines a DHCP option value (just the option data with an associated ID tag).
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_OPTION_VALUE : IDisposable
11 | {
12 | ///
13 | /// DHCP_OPTION_ID value that specifies a unique ID number for the option.
14 | ///
15 | public readonly int OptionID;
16 | ///
17 | /// DHCP_OPTION_DATA structure that contains the data for a DHCP server option.
18 | ///
19 | public readonly DHCP_OPTION_DATA Value;
20 |
21 | public void Dispose()
22 | {
23 | Value.Dispose();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_OPTION_VALUE_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_OPTION_VALUE_ARRAY structure defines a list of DHCP option values (just the option data with associated ID tags).
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_OPTION_VALUE_ARRAY : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of option values listed in Values.
15 | ///
16 | public readonly int NumElements;
17 | ///
18 | /// Pointer to a list of DHCP_OPTION_VALUE structures containing DHCP option values.
19 | ///
20 | private readonly IntPtr ValuesPointer;
21 | ///
22 | /// Pointer to a list of DHCP_OPTION_VALUE structures containing DHCP option values.
23 | ///
24 | public IEnumerable Values
25 | {
26 | get
27 | {
28 | if (NumElements == 0 || ValuesPointer == IntPtr.Zero)
29 | yield break;
30 |
31 | var iter = ValuesPointer;
32 | var size = Marshal.SizeOf(typeof(DHCP_OPTION_VALUE));
33 | for (var i = 0; i < NumElements; i++)
34 | {
35 | yield return iter.MarshalToStructure();
36 | iter += size;
37 | }
38 | }
39 | }
40 |
41 | public void Dispose()
42 | {
43 | foreach (var value in Values)
44 | value.Dispose();
45 |
46 | Api.FreePointer(ValuesPointer);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SEARCH_INFO.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_SEARCH_INFO structure defines the DHCP client record data used to search against for particular server operations.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_SEARCH_INFO_Managed_IpAddress
11 | {
12 | private readonly IntPtr searchType;
13 |
14 | ///
15 | /// DHCP_SEARCH_INFO_TYPE enumeration value that specifies the data included in the subsequent member of this structure.
16 | ///
17 | public DHCP_SEARCH_INFO_TYPE SearchType => (DHCP_SEARCH_INFO_TYPE)searchType;
18 |
19 | ///
20 | /// DHCP_IP_ADDRESS value that specifies a client IP address. This field is populated if SearchType is set to DhcpClientIpAddress.
21 | ///
22 | public readonly DHCP_IP_ADDRESS ClientIpAddress;
23 |
24 | public DHCP_SEARCH_INFO_Managed_IpAddress(DHCP_IP_ADDRESS clientIpAddress)
25 | {
26 | searchType = (IntPtr)DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress;
27 | ClientIpAddress = clientIpAddress;
28 | }
29 | }
30 |
31 | ///
32 | /// The DHCP_SEARCH_INFO structure defines the DHCP client record data used to search against for particular server operations.
33 | ///
34 | [StructLayout(LayoutKind.Sequential)]
35 | internal readonly struct DHCP_SEARCH_INFO_Managed_HardwareAddress
36 | {
37 | private readonly IntPtr searchType;
38 |
39 | ///
40 | /// DHCP_SEARCH_INFO_TYPE enumeration value that specifies the data included in the subsequent member of this structure.
41 | ///
42 | public DHCP_SEARCH_INFO_TYPE SearchType => (DHCP_SEARCH_INFO_TYPE)searchType;
43 |
44 | ///
45 | /// DHCP_CLIENT_UID structure that contains a hardware MAC address. This field is populated if SearchType is set to DhcpClientHardwareAddress.
46 | ///
47 | public readonly DHCP_CLIENT_UID ClientHardwareAddress;
48 |
49 | public DHCP_SEARCH_INFO_Managed_HardwareAddress(DHCP_CLIENT_UID clientHardwareAddress)
50 | {
51 | searchType = (IntPtr)DHCP_SEARCH_INFO_TYPE.DhcpClientHardwareAddress;
52 | ClientHardwareAddress = clientHardwareAddress;
53 | }
54 | }
55 |
56 | ///
57 | /// The DHCP_SEARCH_INFO structure defines the DHCP client record data used to search against for particular server operations.
58 | ///
59 | [StructLayout(LayoutKind.Sequential)]
60 | internal readonly struct DHCP_SEARCH_INFO_Managed_Name
61 | {
62 | private readonly IntPtr searchType;
63 |
64 | ///
65 | /// DHCP_SEARCH_INFO_TYPE enumeration value that specifies the data included in the subsequent member of this structure.
66 | ///
67 | public DHCP_SEARCH_INFO_TYPE SearchType => (DHCP_SEARCH_INFO_TYPE)searchType;
68 |
69 | ///
70 | /// Unicode string that specifies the network name of the DHCP client. This field is populated if SearchType is set to DhcpClientName.
71 | ///
72 | public readonly IntPtr ClientName;
73 |
74 | public DHCP_SEARCH_INFO_Managed_Name(IntPtr clientName)
75 | {
76 | searchType = (IntPtr)DHCP_SEARCH_INFO_TYPE.DhcpClientName;
77 | ClientName = clientName;
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SEARCH_INFO_TYPE.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The DHCP_SEARCH_INFO_TYPE enumeration defines the set of possible attributes used to search DHCP client information records.
5 | ///
6 | internal enum DHCP_SEARCH_INFO_TYPE : int
7 | {
8 | ///
9 | /// The search will be performed against the assigned DHCP client IP address, represented as a 32-bit unsigned integer value.
10 | ///
11 | DhcpClientIpAddress = 0,
12 | ///
13 | /// The search will be performed against the MAC address of the DHCP client network interface device, represented as a DHCP_BINARY_DATA structure.
14 | ///
15 | DhcpClientHardwareAddress = 1,
16 | ///
17 | /// The search will be performed against the DHCP client's network name, represented as a Unicode string.
18 | ///
19 | DhcpClientName = 2
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SERVER_CONFIG_INFO.cs:
--------------------------------------------------------------------------------
1 | using System.Runtime.InteropServices;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCP_SERVER_CONFIG_INFO structure defines the data used to configure the DHCP server.
7 | ///
8 | [StructLayout(LayoutKind.Sequential)]
9 | internal readonly struct DHCP_SERVER_CONFIG_INFO
10 | {
11 | ///
12 | /// Specifies a set of bit flags that contain the RPC protocols supported by the DHCP server.
13 | ///
14 | public readonly uint APIProtocolSupport;
15 | ///
16 | /// Unicode string that specifies the file name of the client lease JET database.
17 | ///
18 | [MarshalAs(UnmanagedType.LPWStr)]
19 | public readonly string DatabaseName;
20 | ///
21 | /// Unicode string that specifies the absolute path to DatabaseName.
22 | ///
23 | [MarshalAs(UnmanagedType.LPWStr)]
24 | public readonly string DatabasePath;
25 | ///
26 | /// Unicode string that specifies the absolute path and file name of the backup client lease JET database.
27 | ///
28 | [MarshalAs(UnmanagedType.LPWStr)]
29 | public readonly string BackupPath;
30 | ///
31 | /// Specifies the interval, in minutes, between backups of the client lease database.
32 | ///
33 | public readonly int BackupInterval;
34 | ///
35 | /// Specifies a bit flag that indicates whether or not database actions should be logged.
36 | ///
37 | public readonly uint DatabaseLoggingFlag;
38 | ///
39 | /// Specifies a bit flag that indicates whether or not a database restore operation should be performed.
40 | ///
41 | public readonly uint RestoreFlag;
42 | ///
43 | /// Specifies the interval, in minutes, between cleanup operations performed on the client lease database.
44 | ///
45 | public readonly int DatabaseCleanupInterval;
46 | ///
47 | /// Reserved. This field should be set to 0x00000000.
48 | ///
49 | public readonly uint DebugFlag;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SERVER_SPECIFIC_STRINGS.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_SERVER_SPECIFIC_STRINGS structure contains the default string values for user and vendor class names.
8 | ///
9 | [StructLayout(LayoutKind.Sequential)]
10 | internal readonly struct DHCP_SERVER_SPECIFIC_STRINGS : IDisposable
11 | {
12 | ///
13 | /// Pointer to a Unicode string that specifies the default vendor class name for the DHCP server.
14 | ///
15 | private readonly IntPtr DefaultVendorClassNamePointer;
16 | ///
17 | /// Pointer to a Unicode string that specifies the default user class name for the DHCP server.
18 | ///
19 | private readonly IntPtr DefaultUserClassNamePointer;
20 |
21 | ///
22 | /// Pointer to a Unicode string that specifies the default vendor class name for the DHCP server.
23 | ///
24 | public string DefaultVendorClassName => Marshal.PtrToStringUni(DefaultVendorClassNamePointer);
25 | ///
26 | /// Pointer to a Unicode string that specifies the default user class name for the DHCP server.
27 | ///
28 | public string DefaultUserClassName => Marshal.PtrToStringUni(DefaultUserClassNamePointer);
29 |
30 | public void Dispose()
31 | {
32 | Api.FreePointer(DefaultVendorClassNamePointer);
33 | Api.FreePointer(DefaultUserClassNamePointer);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SUBNET_ELEMENT_INFO_ARRAY.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_SUBNET_ELEMENT_INFO_ARRAY structure defines an array of subnet element data.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_SUBNET_ELEMENT_INFO_ARRAY : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of elements in Elements.
15 | ///
16 | public readonly int NumElements;
17 |
18 | ///
19 | /// Pointer to a list of DHCP_SUBNET_ELEMENT_DATA structures that contain the data for the corresponding subnet elements.
20 | ///
21 | private readonly IntPtr ElementsPointer;
22 |
23 | ///
24 | /// Pointer to a list of DHCP_SUBNET_ELEMENT_DATA structures that contain the data for the corresponding subnet elements.
25 | ///
26 | public IEnumerable Elements
27 | {
28 | get
29 | {
30 | if (NumElements == 0 || ElementsPointer == IntPtr.Zero)
31 | yield break;
32 |
33 | var iter = ElementsPointer;
34 | var size = Marshal.SizeOf(typeof(DHCP_SUBNET_ELEMENT_DATA));
35 | for (var i = 0; i < NumElements; i++)
36 | {
37 | yield return iter.MarshalToStructure();
38 | iter += size;
39 | }
40 | }
41 | }
42 |
43 | public void Dispose()
44 | {
45 | foreach (var element in Elements)
46 | element.Dispose();
47 |
48 | Api.FreePointer(ElementsPointer);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.InteropServices;
4 |
5 | namespace Dhcp.Native
6 | {
7 | ///
8 | /// The DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5 structure defines an array of subnet element data. Element data in the V5 structure is BOOTP specific.
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5 : IDisposable
12 | {
13 | ///
14 | /// Specifies the number of elements in Elements.
15 | ///
16 | public readonly int NumElements;
17 |
18 | ///
19 | /// Pointer to a list of DHCP_SUBNET_ELEMENT_DATA_V5 structures that contain the data for the corresponding subnet elements.
20 | ///
21 | private readonly IntPtr ElementsPointer;
22 |
23 | ///
24 | /// Pointer to a list of DHCP_SUBNET_ELEMENT_DATA_V5 structures that contain the data for the corresponding subnet elements.
25 | ///
26 | public IEnumerable Elements
27 | {
28 | get
29 | {
30 | if (NumElements == 0 || ElementsPointer == IntPtr.Zero)
31 | yield break;
32 |
33 | var iter = ElementsPointer;
34 | var size = Marshal.SizeOf(typeof(DHCP_SUBNET_ELEMENT_DATA_V5));
35 | for (var i = 0; i < NumElements; i++)
36 | {
37 | yield return iter.MarshalToStructure();
38 | iter += size;
39 | }
40 | }
41 | }
42 |
43 | public void Dispose()
44 | {
45 | foreach (var element in Elements)
46 | element.Dispose();
47 |
48 | Api.FreePointer(ElementsPointer);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SUBNET_ELEMENT_TYPE.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Native
4 | {
5 | ///
6 | /// The DHCP_SUBNET_ELEMENT_TYPE enumeration defines the set of possible subnet element types.
7 | ///
8 | internal enum DHCP_SUBNET_ELEMENT_TYPE
9 | {
10 | ///
11 | /// The subnet element contains the range of DHCP-served IP addresses.
12 | ///
13 | DhcpIpRanges,
14 | ///
15 | /// The subnet element contains the IP addresses of secondary DHCP hosts available in the subnet.
16 | ///
17 | [Obsolete("Not Supported")]
18 | DhcpSecondaryHosts,
19 | ///
20 | /// The subnet element contains the individual reserved IP addresses for the subnet.
21 | ///
22 | DhcpReservedIps,
23 | ///
24 | /// The subnet element contains the IP addresses excluded from the range of DHCP-served addresses.
25 | ///
26 | DhcpExcludedIpRanges,
27 | ///
28 | /// Undocumented Option
29 | ///
30 | DhcpIpUsedClusters,
31 | ///
32 | /// The subnet element contains the IP addresses served by DHCP to the subnet (as opposed to those served by other dynamic address services, such as BOOTP).
33 | ///
34 | DhcpIpRangesDhcpOnly,
35 | ///
36 | /// The subnet element contains the IP addresses served by both DHCP and BOOTP to the subnet.
37 | ///
38 | DhcpIpRangesDhcpBootp,
39 | ///
40 | /// The subnet element contains the IP addresses served by BOOTP to the subnet (specifically excluding DHCP-served addresses).
41 | ///
42 | DhcpIpRangesBootpOnly,
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_SUBNET_STATE.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | internal enum DHCP_SUBNET_STATE : uint
4 | {
5 | ///
6 | /// The subnet is enabled; the server will distribute addresses, extend leases, and release addresses within the subnet range to clients.
7 | ///
8 | DhcpSubnetEnabled = 0,
9 | ///
10 | /// The subnet is disabled; the server will not distribute addresses or extend leases within the subnet range to clients. However, the server will still release addresses within the subnet range.
11 | ///
12 | DhcpSubnetDisabled,
13 | ///
14 | /// The subnet is enabled; the server will distribute addresses, extend leases, and release addresses within the subnet range to clients. The default gateway is set to the local machine itself.
15 | ///
16 | DhcpSubnetEnabledSwitched,
17 | ///
18 | /// The subnet is disabled; the server will not distribute addresses or extend leases within the subnet range to clients. However, the server will still release addresses within the subnet range. The default gateway is set to the local machine itself.
19 | ///
20 | DhcpSubnetDisabledSwitched,
21 | ///
22 | /// The subnet is in an invalid state.
23 | ///
24 | DhcpSubnetInvalidState,
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DHCP_VENDOR_OPTION.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp.Native
5 | {
6 | ///
7 | /// The DHCP_OPTION structure defines a single DHCP option and any data elements associated with it.
8 | /// Additional fields are added to support Vendor Options in DHCP_ALL_OPTIONS
9 | ///
10 | [StructLayout(LayoutKind.Sequential)]
11 | internal readonly struct DHCP_VENDOR_OPTION : IDisposable
12 | {
13 | ///
14 | /// DHCP_OPTION_ID value that specifies a unique ID number (also called a "code") for this option.
15 | ///
16 | public readonly int OptionID;
17 | ///
18 | /// Unicode string that contains the name of this option.
19 | ///
20 | private readonly IntPtr OptionNamePointer;
21 | ///
22 | /// Unicode string that contains a comment about this option.
23 | ///
24 | private readonly IntPtr OptionCommentPointer;
25 | ///
26 | /// structure that contains the data associated with this option.
27 | ///
28 | public readonly DHCP_OPTION_DATA DefaultValue;
29 | ///
30 | /// DHCP_OPTION_TYPE enumeration value that indicates whether this option is a single unary item or an element in an array of options.
31 | ///
32 | public readonly DHCP_OPTION_TYPE OptionType;
33 | ///
34 | /// Unicode string that contains the name of the vendor for the option.
35 | ///
36 | private readonly IntPtr VendorNamePointer;
37 | ///
38 | /// Unicode string that contains the name of the DHCP class for the option.
39 | ///
40 | private readonly IntPtr ClassNamePointer;
41 |
42 | ///
43 | /// Unicode string that contains the name of this option.
44 | ///
45 | public string OptionName => Marshal.PtrToStringUni(OptionNamePointer);
46 | ///
47 | /// Unicode string that contains a comment about this option.
48 | ///
49 | public string OptionComment => Marshal.PtrToStringUni(OptionCommentPointer);
50 | ///
51 | /// Unicode string that contains the name of the vendor for the option.
52 | ///
53 | public string VendorName => Marshal.PtrToStringUni(VendorNamePointer);
54 | ///
55 | /// Unicode string that contains the name of the DHCP class for the option.
56 | ///
57 | public string ClassName => Marshal.PtrToStringUni(ClassNamePointer);
58 |
59 | public void Dispose()
60 | {
61 | Api.FreePointer(OptionNamePointer);
62 | Api.FreePointer(OptionCommentPointer);
63 |
64 | DefaultValue.Dispose();
65 |
66 | Api.FreePointer(VendorNamePointer);
67 | Api.FreePointer(ClassNamePointer);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/DhcpErrorDescriptionAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Dhcp.Native
4 | {
5 | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
6 | internal class DhcpErrorDescriptionAttribute : Attribute
7 | {
8 | public string Description { get; }
9 |
10 | public DhcpErrorDescriptionAttribute(string description)
11 | {
12 | Description = description;
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/FSM_STATE.cs:
--------------------------------------------------------------------------------
1 | namespace Dhcp.Native
2 | {
3 | ///
4 | /// The FSM_STATE enumeration defines the set of possible failover relationship states on a DHCPv4 server.
5 | /// These states are in conformance with the states described in the IETF Failover Protocol draft: http://tools.ietf.org/html/draft-ietf-dhc-failover-12
6 | ///
7 | internal enum FSM_STATE
8 | {
9 | ///
10 | /// Indicates that no state is configured for the DHCPv4 failover relationship.
11 | ///
12 | NO_STATE,
13 | ///
14 | /// Indicates that the failover relationship on the DHCPv4 server is in the initialization state.
15 | ///
16 | INIT,
17 | ///
18 | /// Indicates that each server participating in the failover relationship can probe its partner server before starting the DHCP client service. A DHCPv4 server moves into the STARTUP state after INIT.
19 | ///
20 | STARTUP,
21 | ///
22 | /// Indicates that each server in the failover relationship can service DHCPDISCOVER messages and all other DHCP requests as defined in RFC2131. DHCPv4 servers in the NORMAL state can not service DHCPREQUEST/RENEWAL or DHCPREQUEST/REBINDING requests from the client set defined according to the load balancing algorithm in RFC3074. However, each server can service DHCPREQUEST/RENEWAL or DHCPDISCOVER/REBINDING requests from any client.
23 | ///
24 | NORMAL,
25 | ///
26 | /// Indicates that each server in a failover relationship is operating independently, but neither assumes that their partner is not operating. The partner server might be operating and simply unable to communicate with this server, or it might not be operating at all.
27 | ///
28 | COMMUNICATION_INT,
29 | ///
30 | /// Indicates that a server assumes its partner is not currently operating.
31 | ///
32 | PARTNER_DOWN,
33 | ///
34 | /// Indicates that a failover relationship between two DHCPv4 servers is attempting to reestablish itself.
35 | ///
36 | POTENTIAL_CONFLICT,
37 | ///
38 | /// Indicates that the primary server has received all updates from the secondary server during the failover relationship reintegration process.
39 | ///
40 | CONFLICT_DONE,
41 | ///
42 | /// Indicates that two servers in the POTENTIAL_CONFLICT state were attempting to reintegrate their failover relationship with each other, but communications between them failed prior to completion of the reintegration.
43 | ///
44 | RESOLUTION_INT,
45 | ///
46 | /// Indicates that a server in a failover relationship has no information in its stable storage facility or that it is reintegrating with a server in the PARTNER_DOWN state.
47 | ///
48 | RECOVER,
49 | ///
50 | /// Indicates that the DHCPv4 server should wait for a time period equal to Maximum Client Lead Time (MCLT) before moving to the RECOVER_DONE state. The MCLT is the maximum time, in seconds, that one server can extend a lease for a client beyond the lease time known by the partner server.
51 | ///
52 | RECOVER_WAIT,
53 | ///
54 | /// This value enables an interlocked transition of one server from the RECOVER state and another server from the PARTNER_DOWN or COMMUNICATION-INT state to the NORMAL state.
55 | ///
56 | RECOVER_DONE,
57 | ///
58 | /// Reserved. Do not use.
59 | ///
60 | PAUSED,
61 | ///
62 | /// Reserved. Do not use.
63 | ///
64 | SHUTDOWN
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/Dhcp/Native/QuarantineStatus.cs:
--------------------------------------------------------------------------------
1 |
2 | namespace Dhcp.Native
3 | {
4 | ///
5 | /// The QuarantineStatus enumeration specifies possible health status values for the DHCPv4 client, as validated at the NAP server.
6 | ///
7 | internal enum QuarantineStatus
8 | {
9 | ///
10 | /// The DHCP client is compliant with the health policies defined by the administrator and has normal access to the network.
11 | ///
12 | NOQUARANTINE = 0,
13 | ///
14 | /// The DHCP client is not compliant with the health policies defined by the administrator and is being quarantined with restricted access to the network.
15 | ///
16 | RESTRICTEDACCESS = 1,
17 | ///
18 | /// The DHCP client is not compliant with the health policies defined by the administrator and is being denied access to the network. The DHCP server does not grant an IP address lease to this client.
19 | ///
20 | DROPPACKET = 2,
21 | ///
22 | /// The DHCP client is not compliant with the health policies defined by the administrator and is being granted normal access to the network for a limited time.
23 | ///
24 | PROBATION = 3,
25 | ///
26 | /// The DHCP client is exempt from compliance with the health policies defined by the administrator and is granted normal access to the network.
27 | ///
28 | EXEMPT = 4,
29 | ///
30 | /// The DHCP client is put into the default quarantine state configured on the DHCP NAP server. When a network policy server (NPS) is unavailable, the DHCP client can be put in any of the states NOQUARANTINE, RESTRICTEDACCESS, or DROPPACKET, depending on the default setting on the DHCP NAP server.
31 | ///
32 | DEFAULTQUARSETTING = 5,
33 | ///
34 | /// No quarantine.
35 | ///
36 | NOQUARINFO = 6,
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Dhcp/UnmanagedDisposer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 |
4 | namespace Dhcp
5 | {
6 | internal readonly struct UnmanagedDisposer : IDisposable
7 | {
8 | #pragma warning disable IDE0032 // Use auto property
9 | private readonly IntPtr pointer;
10 | #pragma warning restore IDE0032 // Use auto property
11 |
12 | public IntPtr Pointer => pointer;
13 |
14 | public UnmanagedDisposer(T structure)
15 | {
16 | var size = Marshal.SizeOf(structure);
17 | pointer = Marshal.AllocHGlobal(size);
18 | try
19 | {
20 | Marshal.StructureToPtr(structure, pointer, false);
21 | }
22 | catch (Exception)
23 | {
24 | Marshal.FreeHGlobal(pointer);
25 | throw;
26 | }
27 | }
28 |
29 | public void Dispose()
30 | {
31 | if (pointer != IntPtr.Zero)
32 | {
33 | Marshal.DestroyStructure(pointer, typeof(T));
34 | Marshal.FreeHGlobal(pointer);
35 | }
36 | }
37 |
38 | public static implicit operator IntPtr(UnmanagedDisposer disposer)
39 | {
40 | return disposer.pointer;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/DhcpCalloutDemo/DhcpCalloutDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {AB9AD770-6EF7-4AF0-91F5-11640E296BA8}
8 | Library
9 | Properties
10 | DhcpCalloutDemo
11 | DhcpCalloutDemo
12 | v4.0
13 | 512
14 | true
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 |
33 |
34 | true
35 | bin\x64\Debug\
36 | DEBUG;TRACE
37 | full
38 | x64
39 | prompt
40 | MinimumRecommendedRules.ruleset
41 |
42 |
43 | bin\x64\Release\
44 | TRACE
45 | true
46 | pdbonly
47 | x64
48 | prompt
49 | MinimumRecommendedRules.ruleset
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | {4cb9d6c3-67ce-478e-ba98-eb2c2c927282}
67 | Dhcp.Callout
68 |
69 |
70 | {3f2e7427-2adc-43d4-a92b-1f6abae23bed}
71 | Dhcp
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/src/DhcpCalloutDemo/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("Dhcp Server API Callout Demo")]
8 | [assembly: AssemblyDescription("Demonstrates the Dhcp Server Callout API")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("Dhcp Server API")]
12 | [assembly: AssemblyCopyright("Copyright © Gary Sharp 2019")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("ab9ad770-6ef7-4af0-91f5-11640e296ba8")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
36 |
--------------------------------------------------------------------------------
/src/DhcpDemo/DhcpDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.2
6 | AnyCPU;x64
7 | DhcpServerApi.Demo
8 | Gary Sharp
9 | Gary Sharp
10 | Dhcp Server API
11 | Copyright 2019 Gary Sharp
12 | Demonstrates the Dhcp Server API
13 | https://github.com/garysharp/DhcpServerApi
14 | git
15 | https://github.com/garysharp/DhcpServerApi
16 | DHCP Win32 Windows
17 | MIT
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/DhcpWritableDemo/DhcpWritableDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.2
6 | AnyCPU;x64
7 | DhcpServerApi.WritableDemo
8 | Gary Sharp
9 | Gary Sharp
10 | Dhcp Server API
11 | Copyright 2019 Gary Sharp
12 | Demonstrates the Dhcp Server Writable API
13 | https://github.com/garysharp/DhcpServerApi
14 | git
15 | https://github.com/garysharp/DhcpServerApi
16 | DHCP Win32 Windows
17 | MIT
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/DllExport_Configure.bat:
--------------------------------------------------------------------------------
1 | DllExport -action Configure %*
--------------------------------------------------------------------------------