Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions DnsServerCore/Dhcp/Lease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,34 @@ public class Lease : IComparable<Lease>
readonly byte[] _hardwareAddress;
readonly IPAddress _address;
string _comments;
bool _isEnabled;
readonly DateTime _leaseObtained;
DateTime _leaseExpires;

#endregion

#region constructor

internal Lease(LeaseType type, ClientIdentifierOption clientIdentifier, string hostName, byte[] hardwareAddress, IPAddress address, string comments, uint leaseTime)
internal Lease(LeaseType type, ClientIdentifierOption clientIdentifier, string hostName, byte[] hardwareAddress, IPAddress address, string comments, uint leaseTime, bool isEnabled)
{
_type = type;
_clientIdentifier = clientIdentifier;
_hostName = hostName;
_hardwareAddress = hardwareAddress;
_address = address;
_comments = comments;
_isEnabled = isEnabled;
_leaseObtained = DateTime.UtcNow;

ExtendLease(leaseTime);
}

internal Lease(LeaseType type, string hostName, DhcpMessageHardwareAddressType hardwareAddressType, byte[] hardwareAddress, IPAddress address, string comments)
: this(type, new ClientIdentifierOption((byte)hardwareAddressType, hardwareAddress), hostName, hardwareAddress, address, comments, 0)
internal Lease(LeaseType type, string hostName, DhcpMessageHardwareAddressType hardwareAddressType, byte[] hardwareAddress, IPAddress address, string comments, bool isEnabled)
: this(type, new ClientIdentifierOption((byte)hardwareAddressType, hardwareAddress), hostName, hardwareAddress, address, comments, 0, isEnabled)
{ }

internal Lease(LeaseType type, string hostName, DhcpMessageHardwareAddressType hardwareAddressType, string hardwareAddress, IPAddress address, string comments)
: this(type, hostName, hardwareAddressType, ParseHardwareAddress(hardwareAddress), address, comments)
internal Lease(LeaseType type, string hostName, DhcpMessageHardwareAddressType hardwareAddressType, string hardwareAddress, IPAddress address, string comments, bool isEnabled)
: this(type, hostName, hardwareAddressType, ParseHardwareAddress(hardwareAddress), address, comments, isEnabled)
{ }

internal Lease(BinaryReader bR)
Expand Down Expand Up @@ -101,6 +103,7 @@ internal Lease(BinaryReader bR)

_leaseObtained = bR.ReadDateTime();
_leaseExpires = bR.ReadDateTime();
_isEnabled = bR.ReadBoolean();
break;

default:
Expand Down Expand Up @@ -169,6 +172,7 @@ public void WriteTo(BinaryWriter bW)

bW.Write(_leaseObtained);
bW.Write(_leaseExpires);
bW.Write(_isEnabled);
}

public string GetClientInfo()
Expand Down Expand Up @@ -205,6 +209,9 @@ public byte[] HardwareAddress
public IPAddress Address
{ get { return _address; } }

public bool IsEnabled
{ get { return _isEnabled; } }

public string Comments
{
get { return _comments; }
Expand Down
26 changes: 17 additions & 9 deletions DnsServerCore/Dhcp/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ private void ConvertToReservedLease(Lease lease)
lease.ConvertToReserved();

//add reserved lease
Lease reservedLease = new Lease(LeaseType.Reserved, null, DhcpMessageHardwareAddressType.Ethernet, lease.HardwareAddress, lease.Address, null);
Lease reservedLease = new Lease(LeaseType.Reserved, null, DhcpMessageHardwareAddressType.Ethernet, lease.HardwareAddress, lease.Address, null, lease.IsEnabled);
_reservedLeases[reservedLease.ClientIdentifier] = reservedLease;
}

Expand All @@ -622,7 +622,7 @@ private void ConvertToDynamicLease(Lease lease)
lease.ConvertToDynamic();

//remove reserved lease
Lease reservedLease = new Lease(LeaseType.Reserved, null, DhcpMessageHardwareAddressType.Ethernet, lease.HardwareAddress, lease.Address, null);
Lease reservedLease = new Lease(LeaseType.Reserved, null, DhcpMessageHardwareAddressType.Ethernet, lease.HardwareAddress, lease.Address, null, lease.IsEnabled);
_reservedLeases.TryRemove(reservedLease.ClientIdentifier, out _);

//remove any old single address exclusion entry
Expand Down Expand Up @@ -908,7 +908,7 @@ internal bool IsAddressReserved(IPAddress address)
{
foreach (KeyValuePair<ClientIdentifierOption, Lease> reservedLease in _reservedLeases)
{
if (address.Equals(reservedLease.Value.Address))
if (address.Equals(reservedLease.Value.Address) && reservedLease.Value.IsEnabled)
return true;
}

Expand Down Expand Up @@ -951,7 +951,7 @@ internal async Task<Lease> GetOfferAsync(DhcpMessage request)
if (existingLease.Type == LeaseType.Reserved)
{
Lease existingReservedLease = GetReservedLease(request);
if ((existingReservedLease is not null) && (existingReservedLease.Address == existingLease.Address))
if ((existingReservedLease is not null) && (existingReservedLease.Address == existingLease.Address) && (existingReservedLease.IsEnabled))
return existingLease; //return existing reserved lease

//reserved lease address was changed; proceed to offer new lease
Expand All @@ -975,9 +975,17 @@ internal async Task<Lease> GetOfferAsync(DhcpMessage request)
Lease reservedLease = GetReservedLease(request);
if (reservedLease != null)
{
Lease reservedOffer = new Lease(LeaseType.Reserved, clientIdentifier, null, request.ClientHardwareAddress, reservedLease.Address, null, GetLeaseTime());
_offers[clientIdentifier] = reservedOffer;
return reservedOffer;
if (reservedLease.IsEnabled)
{
Lease reservedOffer = new Lease(LeaseType.Reserved, clientIdentifier, null, request.ClientHardwareAddress, reservedLease.Address, null, GetLeaseTime(), reservedLease.IsEnabled);
_offers[clientIdentifier] = reservedOffer;
return reservedOffer;
}
else
{
if (_log is not null)
_log.Write("DHCP Server skipped to offer IP address to " + request.GetClientFullIdentifier() + " for scope '" + _name + "': the reserved lease is disabled.");
}
}

if (_allowOnlyReservedLeases)
Expand All @@ -999,7 +1007,7 @@ internal async Task<Lease> GetOfferAsync(DhcpMessage request)
}
}

Lease dummyOffer = new Lease(LeaseType.None, null, null, null, null, null, 0);
Lease dummyOffer = new Lease(LeaseType.None, null, null, null, null, null, 0, false);
Lease existingOffer = _offers.GetOrAdd(clientIdentifier, dummyOffer);

if (dummyOffer != existingOffer)
Expand Down Expand Up @@ -1076,7 +1084,7 @@ internal async Task<Lease> GetOfferAsync(DhcpMessage request)
}
}

Lease offerLease = new Lease(LeaseType.Dynamic, clientIdentifier, null, request.ClientHardwareAddress, offerAddress, null, GetLeaseTime());
Lease offerLease = new Lease(LeaseType.Dynamic, clientIdentifier, null, request.ClientHardwareAddress, offerAddress, null, GetLeaseTime(), true);
return _offers[clientIdentifier] = offerLease;
}

Expand Down
9 changes: 5 additions & 4 deletions DnsServerCore/WebServiceDhcpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public void GetDhcpScope(HttpContext context)
jsonWriter.WriteString("hardwareAddress", BitConverter.ToString(reservedLease.HardwareAddress));
jsonWriter.WriteString("address", reservedLease.Address.ToString());
jsonWriter.WriteString("comments", reservedLease.Comments);

jsonWriter.WriteString("isEnabled", reservedLease.IsEnabled.ToString());
jsonWriter.WriteEndObject();
}

Expand Down Expand Up @@ -630,8 +630,8 @@ public async Task SetDhcpScopeAsync(HttpContext context)
string[] strReservedLeaseParts = strReservedLeases.Split('|');
List<Lease> reservedLeases = new List<Lease>();

for (int i = 0; i < strReservedLeaseParts.Length; i += 4)
reservedLeases.Add(new Lease(LeaseType.Reserved, strReservedLeaseParts[i + 0], DhcpMessageHardwareAddressType.Ethernet, strReservedLeaseParts[i + 1], IPAddress.Parse(strReservedLeaseParts[i + 2]), strReservedLeaseParts[i + 3]));
for (int i = 0; i < strReservedLeaseParts.Length; i += 5)
reservedLeases.Add(new Lease(LeaseType.Reserved, strReservedLeaseParts[i + 0], DhcpMessageHardwareAddressType.Ethernet, strReservedLeaseParts[i + 1], IPAddress.Parse(strReservedLeaseParts[i + 2]), strReservedLeaseParts[i + 3], Convert.ToBoolean(strReservedLeaseParts[i + 4])));

scope.ReservedLeases = reservedLeases;
}
Expand Down Expand Up @@ -686,8 +686,9 @@ public void AddReservedLease(HttpContext context)
string hardwareAddress = request.GetQueryOrForm("hardwareAddress");
string strIpAddress = request.GetQueryOrForm("ipAddress");
string comments = request.QueryOrForm("comments");
bool isEnabled = Convert.ToBoolean(request.QueryOrForm("isEnabled"));

Lease reservedLease = new Lease(LeaseType.Reserved, hostName, DhcpMessageHardwareAddressType.Ethernet, hardwareAddress, IPAddress.Parse(strIpAddress), comments);
Lease reservedLease = new Lease(LeaseType.Reserved, hostName, DhcpMessageHardwareAddressType.Ethernet, hardwareAddress, IPAddress.Parse(strIpAddress), comments, isEnabled);

if (!scope.AddReservedLease(reservedLease))
throw new DnsWebServiceException("Failed to add reserved lease for scope: " + scopeName);
Expand Down
3 changes: 2 additions & 1 deletion DnsServerCore/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2645,7 +2645,8 @@ <h4 style="padding: 10px 0px;" id="titleDhcpEditScope">Edit Scope</h4>
<th>MAC Address</th>
<th>IP Address</th>
<th>Comments</th>
<th style="width: 84px;"><button type="button" class="btn btn-default" style="padding: 0px 20px;" onclick="addDhcpScopeReservedLeaseRow('', '', '', '');">Add</button></th>
<th>Active</th>
<th style="width: 84px;"><button type="button" class="btn btn-default" style="padding: 0px 20px;" onclick="addDhcpScopeReservedLeaseRow('', '', '', '', '');">Add</button></th>
</tr>
</thead>
<tbody id="tableDhcpScopeReservedLeases"></tbody>
Expand Down
8 changes: 4 additions & 4 deletions DnsServerCore/www/js/dhcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,16 @@ function addDhcpScopeExclusionRow(startingAddress, endingAddress) {
$("#tableDhcpScopeExclusions").append(tableHtmlRows);
}

function addDhcpScopeReservedLeaseRow(hostName, hardwareAddress, address, comments) {
function addDhcpScopeReservedLeaseRow(hostName, hardwareAddress, address, comments, isEnabled) {
var id = Math.floor(Math.random() * 10000);

var tableHtmlRows = "<tr id=\"tableDhcpScopeReservedLeaseRow" + id + "\">";
tableHtmlRows += "<td><input type=\"text\" class=\"form-control\" value=\"" + (hostName == null ? "" : htmlEncode(hostName)) + "\" data-optional=\"true\"></td>";
tableHtmlRows += "<td><input type=\"text\" class=\"form-control\" value=\"" + htmlEncode(hardwareAddress) + "\"></td>";
tableHtmlRows += "<td><input type=\"text\" class=\"form-control\" value=\"" + htmlEncode(address) + "\"></td>";
tableHtmlRows += "<td><input type=\"text\" class=\"form-control\" value=\"" + (comments == null ? "" : htmlEncode(comments)) + "\" data-optional=\"true\"></td>";
tableHtmlRows += "<td style=\"vertical-align: middle !important\" ><input type=\"checkbox\" class=\"checkbox\" " + (isEnabled === true || isEnabled === "True" ? "checked" : "") + " value =\"" + htmlEncode(isEnabled) + "\"></td>";
tableHtmlRows += "<td><button type=\"button\" class=\"btn btn-danger\" onclick=\"$('#tableDhcpScopeReservedLeaseRow" + id + "').remove();\">Delete</button></td></tr>";

$("#tableDhcpScopeReservedLeases").append(tableHtmlRows);
}

Expand Down Expand Up @@ -431,7 +431,7 @@ function showEditDhcpScope(scopeName) {

if (responseJSON.response.reservedLeases != null) {
for (var i = 0; i < responseJSON.response.reservedLeases.length; i++) {
addDhcpScopeReservedLeaseRow(responseJSON.response.reservedLeases[i].hostName, responseJSON.response.reservedLeases[i].hardwareAddress, responseJSON.response.reservedLeases[i].address, responseJSON.response.reservedLeases[i].comments);
addDhcpScopeReservedLeaseRow(responseJSON.response.reservedLeases[i].hostName, responseJSON.response.reservedLeases[i].hardwareAddress, responseJSON.response.reservedLeases[i].address, responseJSON.response.reservedLeases[i].comments, responseJSON.response.reservedLeases[i].isEnabled);
}
}

Expand Down Expand Up @@ -508,7 +508,7 @@ function saveDhcpScope() {
if (exclusions === false)
return;

var reservedLeases = serializeTableData($("#tableDhcpScopeReservedLeases"), 4);
var reservedLeases = serializeTableData($("#tableDhcpScopeReservedLeases"), 5);
if (reservedLeases === false)
return;

Expand Down