garysharp / dhcpserverapi Goto Github PK
View Code? Open in Web Editor NEWMicrosoft DHCP Server querying and management
License: MIT License
Microsoft DHCP Server querying and management
License: MIT License
Nearly all the "AddressState" information I receive from this API is wrong. The API infers that there is a simple enumeration of five states. This is not so....
Please see link:
https://social.technet.microsoft.com/Forums/en-US/4e6e1313-2743-462f-94a2-f95a6b312bbd/dhcp-lease-address-state-values?forum=winserverPN
Unfortunately, this API is masking the real "byte" value of the true Address State and returns bad values that cannot be correctly interpretted
As an immediate fix... please provide the Raw/native byte value as the Address State is really supplying multiple states/values in one field based on corresponding bit values within the byte (again see link for details).
An exception is thrown when iterating ExcludedIpRanges for a scope in the demo console application. This only happens when the scope does not have any ExcludedIpRanges. I'm actually not sure if the exception should be swallowed by the API and just return 0 objects to the caller or if it should bubble the exception. This seems like one of the rare cases where swallowing the exception might be the appropriate thing to do to avoid messy try/catches in calling code.
Blocked by #19
Implement the designed API and perform adhoc testing.
Involving creating/editing:
DNS Settings at the server level and at the individual scope level need to be writable (currently read-only).
Hi,
I need to modify the reservation option 67 (bootfile name) from my Web app.
Do you think it's possible to add this function to your .Net API?
BTW, great work, a little gem for DHCP managers, easy to use and program.
Writing DNS settings does not always result in the desired configuration being made. The flag bits are not being set in the correct manner.
When setting a flag the configuration is as expected (or
operation used to set the flag).
When unsetting a flag, the bitwise xor
operation is used. This results in the flags value being toggled instead of unset! The nand
operation should be used instead (to remove the flag).
Should be (nand
):
flags &= ~Constants.DNS_FLAG_UPDATE_BOTH_ALWAYS;
When using version 1.0.5-alpha the Scope.FailoverRelationship
property is always null
unless using the newly introduced Scopes.GetAllScopes(preloadFailoverRelationships: true)
method.
Hello, I am not an expert and I need to read a dhcp server, specifically from a hosted network, and I want to test your code, but I get this error:
"dhcp authorization error 20070"
I understand that I should request AD permissions, but how would I do it in C #?
I look forward to your help, thank you very much.
Failover details are not currently included in the API.
This API change will involve reading, creating, editing and deleting:
Blocked by #25 (informs design decisions)
A scope's lease duration can be unlimited. The platform returns -1 seconds in this case. I propose at the next major release (which allows breaking API compatibility) the DhcpServerScope.LeaseDuration
property be changed from TimeSpan
to TimeSpan?
(Nullable<TimeSpan>
). If this value is null it would represent an unlimited lease duration.
It appears DhcpEnumOptionsV5 must be implemented to retrieve custom vendor options. Interestingly, I can see the vendors options defined in the server classes, but was unable to find a way to access them.
Determine what the writable API should look like for creating, editing and deleting subnets.
Initial question: Should the API directly update edited properties, or should the properties be changed and a SaveChanges()
method be used to commit the changes. A SaveChanges()
method would likely be more complex but potentially more efficient - less roundtrips/config-changes for the server.
It would be fine to work also with 3rd-party dhcp servers.
i. e. http://www.n3k.de/produkte/ip-management/runip/runip-managed-dns-dhcp-appliance/
Is ist possible to read/write the Filters V4?
User feedback indicated that properties were preferred over method calls.
Scope.GetFailoverRelationship()
and Scope.GetFailoverStatistics
have been replaced with:
Scope.FailoverRelationship
and Scope.FailoverStatistics
Hello,
I am trying to add a Option Value to a new created reservation, but I am always getting an access violation. I tried both ways as below both times I get the violation.
Can anybody explain me, why this error occurs?
var dhcpServer = DhcpServer.Connect(192.168.0.1);
var scope = dhcpServer.Scopes.First();
var NewReservation = scope.Reservations.AddReservation("192.168.0.10", "AA:BB:CC:DD:EE:FF");
NewReservation.Client.Name = "Demo";
var Option67 = dhcpServer.Options.GetDefaultOption(67);
var ReservationOption67 = Option67.CreateOptionStringValue("demo");
NewReservation.Options.AddOrSetOptionValue(Option67);
NewReservation.Options.SetOptionValue(dhcpServer.Options.GetDefaultOption(67).CreateOptionStringValue("DEMO"));
Hi,
I tried to get a list of all reservations on a dhcp server 2016 scope, but I ran into an error, that I attempted to read or write protected memory. Reading other scopes from other server with less reservations are working without any error. I am currently using 1.0.9 alpha version.
Currently there are more that 500 reservations in the scope. Do I run against any limits here?
After initial analysis of #1 there may be memory leaks for certain parts of the API. In particular, I want to ensure the subnet and reservation APIs are not leaking.
Option values can only be written at the Scope and Reservation level.
Server-options should also be writable (and is required for #33 as DNS settings are stored as an option).
Just as with scopes and reservations, it would be expected that the API be exposed as Server.Options.[Set|Remove][Default|User|Vendor]OptionValue()
The following exception is thrown when trying to enumerate clients of a subnet with no clients.
Unhandled Exception: Dhcp.DhcpServerException: An error occurred calling 'DhcpEnumSubnetClientsVQ'. No more data is available. [ERROR_NO_MORE_ITEMS 259]
at Dhcp.DhcpServerClient.<GetClientsVQ>d__64.MoveNext() in C:\GitHub\DhcpServerApi\src\Dhcp\DhcpServerClient.cs:line 317
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Expected behaviour is to return an empty list.
Blocked by #13.
This task is a placeholder for any eventual tasks which are determined as necessary as a result.
Hi,
You wrote this on the readme:
// Connect to DHCP Server
var dhcpServer = DhcpServer.Servers.First();
// Display scope information
foreach (var scope in dhcpServer.Scopes)
{
Console.WriteLine("Scope: ", scope.Name);
Console.WriteLine(" Address: ", scope.Address);
Console.WriteLine(" Mask: ", scope.Mask);
Console.WriteLine(" Range: ", scope.IpRange);
Console.WriteLine(" State: ", scope.State);
}
But there is an issue, it should be:
// Connect to DHCP Server
var dhcpServer = DhcpServer.Servers.First();
// Display scope information
foreach (var scope in dhcpServer.Scopes)
{
Console.WriteLine("Scope: {0}", scope.Name);
Console.WriteLine(" Address: {0}", scope.Address);
Console.WriteLine(" Mask: {0}", scope.Mask);
Console.WriteLine(" Range: {0}", scope.IpRange);
Console.WriteLine(" State: {0}", scope.State);
}
Hope it helps,
Arnaud H.
Hi!
If you set the leasing time of a scope to "0day, 0 hour, 0 minutes", when you try to read the scope-LeaseDuration property, you get an unhandled exception. (Dhcp.DhcpServerException)
Please help if i am wrong or there is a bug in the API.
Thanks,
Regards,
Csontikka
Currently the API caches lists of entites which could result in stale values for long-running processes. I feel the responsibility of caching certain values should be moved to the consumer (eg. a UI).
While not specifically related to the v1.0 project, source code was recovered which included a previous upgrade to this library.
The major feature worked on in this recovered source (besides efforts into #14) included an implementation of the DHCP Callout API.
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/dhcp/dhcp-server-api
Using DllExport this implementation allows for a managed .NET library to be loaded and interface directly with the DHCP Server.
All hooks are provided and can be consumed by simply implementing the managed interface.
The library automatically discovers and hooks multiple consumers and compiles with the Callout chaining technique.
.net 5,.net 6 target not support now
Native API documentation: https://docs.microsoft.com/en-us/windows/desktop/api/dhcpsapi/
The following is an initial investigation of which native elements need to be migrated
Methods:
Structures:
Note: Looking to support Windows Server 2018 R2 and newer (at this stage).
When replicating a failover relationship (either the entire relationship, or individual scopes), the reservation client names and descriptions are not transferred.
The DNS Settings (pseudo option 81) are excluded from replication.
Can i get and set 'Allow' and 'Deny' filters?
Hi @garysharp ,
When creating a reservation using the full AddClient parameters, then looking at the DHCP server, the DHCP type is always set to Both, but I set it to DHCP. Looking at the API code, I see you're not using the VQ version of the DhcpCreateClientInfo:
DhcpServerApi/src/Dhcp/Native/Api.cs
Line 547 in bdd50f6
Failover details are not currently included in the API. Create/Read/Update/Delete operations will be required.
Methods:
Structures:
Note: Failover API functionality is supported Windows Server 2012 and newer.
Native API documentation: https://docs.microsoft.com/en-us/windows/desktop/api/dhcpsapi/
The following is an initial investigation of which native elements need to be migrated
Methods:
Reservation option methods should be handled by #21 (Subnet options share the interface)
Note: Looking to support Windows Server 2018 R2 and newer (at this stage).
When catching exceptions its nice to be able to filter for a particular API error.
For example:
// Discover DHCP Servers
try
{
dhcpServers = DhcpServer.Servers.ToList();
}
catch (DhcpServerException ex) when (ex.ApiErrorNative == DhcpServerNativeErrors.DDS_NO_DS_AVAILABLE)
{
// No DHCP Servers could be automatically discovered
}
I'm trying to impersonate a user so that I can pass in custom credentials when authenticating to the DHCP server, but it appears to ignore the impersonation context in most cases. It would be excellent if I could simply pass credentials in when calling Connect
Perform some code cleanup to modernize and re-familiarize myself with the codebase.
Convert to interpolated strings
Convert simple parameters to lambda expression methods
Add readonly field hints
Convert static strings to use nameof()
syntax (particularly with DhcpServerException
constructor)
etc
Migrating the project to .NET Standard would allow compatibility between the Full .NET Framework and .NET Core.
Possibly .NET Standard 1.1 (minimum requires .NET 4.5 or .NET Core 1.0).
If additional framework features are required I'm inclined to upgrade the requirement. If anyone has particular backwards compatibility requirements please speak up.
For servers with large numbers of scopes (especially those with large latency between the library and the DHCP server) the (significant) number of round-trips impacts performance greatly.
It has been identified that all failover relationships and all clients can be loaded with a single API call. When enumerating scopes we can preload failover relationships and clients, pre-populating this data and avoid significant server roundtrips.
However, this behavior should be opt-in. Eager-loading all of this data is likely undesirable, unless specifically needing this behavior. An additional method should be added to the ScopeCollection class allowing consumers to request preloading of failover relationships and/or clients when enumerating scopes.
No behavior should change when loading individual scopes.
If I use DhcpServerClient.GetClients()
in my code, I can watch my process memory usage continuously increase until I stop and restart my process. I narrowed it down to somewhere in here but I'm not knoweldagable enough of the library at this point to fix it.
Native API documentation: https://docs.microsoft.com/en-us/windows/desktop/api/dhcpsapi/
The following is an initial investigation of which native elements need to be migrated
Methods:
Note: Looking to support Windows Server 2018 R2 and newer (at this stage).
A user has encountered a ArgumentOutOfRangeException
when reading scope reservations.
Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: length
at Dhcp.DhcpServerHardwareAddress.FromNative(DhcpServerHardwareType type, IntPtr pointer, Int32 length) in src\Dhcp\DhcpServerHardwareAddress.cs:line 166
at Dhcp.Native.DHCP_CLIENT_UID.get_ClientHardwareAddress() in src\Dhcp\Native\DHCP_CLIENT_UID.cs:line 53
at Dhcp.DhcpServerScopeReservation.FromNative(DhcpServerScope scope, DHCP_IP_RESERVATION_V4& native) in src\Dhcp\DhcpServerScopeReservation.cs:line 91
at Dhcp.DhcpServerScopeReservation.GetReservations(DhcpServerScope scope)+MoveNext() in src\Dhcp\DhcpServerScopeReservation.cs:line 74
...
This occurred when a reservation had been configured with an invalid hardware address. Unfortunately it means the reservations and clients cannot be enumerated (as this invalid one causes the enumeration to fail).
This library receives some big efficiency boosts by treating hardware addresses as value types (living on the stack instead of needing memory allocated). Typical hardware addresses are 24 bits long. I have encountered scenarios where 128 bit hardware addresses were used. This determined a maximum supported hardware address length (16 bytes; 2 x unsigned long).
I don't know what the limit is for the Microsoft DHCP Server.
We need a way of flagging a hardware address as invalid and perhaps offering the truncated value in a way which still allows reservations/clients to be enumerated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.