2021-09-18 19:02:40 +05:30
/ *
Technitium DNS Server
2025-02-15 12:51:16 +05:30
Copyright ( C ) 2025 Shreyas Zare ( shreyas @technitium . com )
2021-09-18 19:02:40 +05:30
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2023-01-01 18:05:07 +05:30
using DnsServerCore.Auth ;
2021-09-18 19:02:40 +05:30
using DnsServerCore.Dhcp ;
using DnsServerCore.Dhcp.Options ;
2023-01-01 18:05:07 +05:30
using Microsoft.AspNetCore.Http ;
2021-09-18 19:02:40 +05:30
using System ;
using System.Collections.Generic ;
using System.Net ;
2022-12-24 13:10:48 +05:30
using System.Text.Json ;
2021-09-18 19:02:40 +05:30
using System.Threading.Tasks ;
2023-01-01 18:05:07 +05:30
using TechnitiumLibrary ;
2025-03-31 19:37:15 +05:30
using TechnitiumLibrary.Net.Dns ;
2021-09-18 19:02:40 +05:30
namespace DnsServerCore
{
2025-02-15 12:51:16 +05:30
public partial class DnsWebService
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
class WebServiceDhcpApi
{
#region variables
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
static readonly char [ ] _commaSeparator = new char [ ] { ',' } ;
2024-02-04 18:05:11 +05:30
2025-02-15 12:51:16 +05:30
readonly DnsWebService _dnsWebService ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region constructor
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public WebServiceDhcpApi ( DnsWebService dnsWebService )
{
_dnsWebService = dnsWebService ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region public
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void ListDhcpLeases ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
IReadOnlyDictionary < string , Scope > scopes = _dnsWebService . _dhcpServer . Scopes ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
//sort by name
List < Scope > sortedScopes = new List < Scope > ( scopes . Count ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( KeyValuePair < string , Scope > entry in scopes )
sortedScopes . Add ( entry . Value ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
sortedScopes . Sort ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Utf8JsonWriter jsonWriter = context . GetCurrentJsonWriter ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "leases" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( Scope scope in sortedScopes )
{
IReadOnlyDictionary < ClientIdentifierOption , Lease > leases = scope . Leases ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
//sort by address
List < Lease > sortedLeases = new List < Lease > ( leases . Count ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( KeyValuePair < ClientIdentifierOption , Lease > entry in leases )
sortedLeases . Add ( entry . Value ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
sortedLeases . Sort ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( Lease lease in sortedLeases )
{
jsonWriter . WriteStartObject ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "scope" , scope . Name ) ;
jsonWriter . WriteString ( "type" , lease . Type . ToString ( ) ) ;
jsonWriter . WriteString ( "hardwareAddress" , BitConverter . ToString ( lease . HardwareAddress ) ) ;
jsonWriter . WriteString ( "clientIdentifier" , lease . ClientIdentifier . ToString ( ) ) ;
jsonWriter . WriteString ( "address" , lease . Address . ToString ( ) ) ;
jsonWriter . WriteString ( "hostName" , lease . HostName ) ;
jsonWriter . WriteString ( "leaseObtained" , lease . LeaseObtained ) ;
jsonWriter . WriteString ( "leaseExpires" , lease . LeaseExpires ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndObject ( ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
public void ListDhcpScopes ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
IReadOnlyDictionary < string , Scope > scopes = _dnsWebService . _dhcpServer . Scopes ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
//sort by name
List < Scope > sortedScopes = new List < Scope > ( scopes . Count ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
foreach ( KeyValuePair < string , Scope > entry in scopes )
sortedScopes . Add ( entry . Value ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
sortedScopes . Sort ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Utf8JsonWriter jsonWriter = context . GetCurrentJsonWriter ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "scopes" ) ;
jsonWriter . WriteStartArray ( ) ;
foreach ( Scope scope in sortedScopes )
{
jsonWriter . WriteStartObject ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "name" , scope . Name ) ;
jsonWriter . WriteBoolean ( "enabled" , scope . Enabled ) ;
jsonWriter . WriteString ( "startingAddress" , scope . StartingAddress . ToString ( ) ) ;
jsonWriter . WriteString ( "endingAddress" , scope . EndingAddress . ToString ( ) ) ;
jsonWriter . WriteString ( "subnetMask" , scope . SubnetMask . ToString ( ) ) ;
jsonWriter . WriteString ( "networkAddress" , scope . NetworkAddress . ToString ( ) ) ;
jsonWriter . WriteString ( "broadcastAddress" , scope . BroadcastAddress . ToString ( ) ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . InterfaceAddress is not null )
jsonWriter . WriteString ( "interfaceAddress" , scope . InterfaceAddress . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndObject ( ) ;
}
jsonWriter . WriteEndArray ( ) ;
}
public void GetDhcpScope ( HttpContext context )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
UserSession session = context . GetCurrentSession ( ) ;
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
string scopeName = context . Request . GetQueryOrForm ( "name" ) ;
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope is null )
throw new DnsWebServiceException ( "DHCP scope was not found: " + scopeName ) ;
Utf8JsonWriter jsonWriter = context . GetCurrentJsonWriter ( ) ;
2021-09-18 19:02:40 +05:30
2022-12-24 13:10:48 +05:30
jsonWriter . WriteString ( "name" , scope . Name ) ;
jsonWriter . WriteString ( "startingAddress" , scope . StartingAddress . ToString ( ) ) ;
jsonWriter . WriteString ( "endingAddress" , scope . EndingAddress . ToString ( ) ) ;
jsonWriter . WriteString ( "subnetMask" , scope . SubnetMask . ToString ( ) ) ;
2025-02-15 12:51:16 +05:30
jsonWriter . WriteNumber ( "leaseTimeDays" , scope . LeaseTimeDays ) ;
jsonWriter . WriteNumber ( "leaseTimeHours" , scope . LeaseTimeHours ) ;
jsonWriter . WriteNumber ( "leaseTimeMinutes" , scope . LeaseTimeMinutes ) ;
jsonWriter . WriteNumber ( "offerDelayTime" , scope . OfferDelayTime ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteBoolean ( "pingCheckEnabled" , scope . PingCheckEnabled ) ;
jsonWriter . WriteNumber ( "pingCheckTimeout" , scope . PingCheckTimeout ) ;
jsonWriter . WriteNumber ( "pingCheckRetries" , scope . PingCheckRetries ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! string . IsNullOrEmpty ( scope . DomainName ) )
jsonWriter . WriteString ( "domainName" , scope . DomainName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . DomainSearchList is not null )
{
jsonWriter . WritePropertyName ( "domainSearchList" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( string domainSearchString in scope . DomainSearchList )
jsonWriter . WriteStringValue ( domainSearchString ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteBoolean ( "dnsUpdates" , scope . DnsUpdates ) ;
jsonWriter . WriteNumber ( "dnsTtl" , scope . DnsTtl ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . ServerAddress is not null )
jsonWriter . WriteString ( "serverAddress" , scope . ServerAddress . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . ServerHostName is not null )
jsonWriter . WriteString ( "serverHostName" , scope . ServerHostName ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . BootFileName is not null )
jsonWriter . WriteString ( "bootFileName" , scope . BootFileName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . RouterAddress is not null )
jsonWriter . WriteString ( "routerAddress" , scope . RouterAddress . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteBoolean ( "useThisDnsServer" , scope . UseThisDnsServer ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . DnsServers is not null )
{
jsonWriter . WritePropertyName ( "dnsServers" ) ;
jsonWriter . WriteStartArray ( ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
foreach ( IPAddress dnsServer in scope . DnsServers )
jsonWriter . WriteStringValue ( dnsServer . ToString ( ) ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . WinsServers is not null )
{
jsonWriter . WritePropertyName ( "winsServers" ) ;
jsonWriter . WriteStartArray ( ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
foreach ( IPAddress winsServer in scope . WinsServers )
jsonWriter . WriteStringValue ( winsServer . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . NtpServers is not null )
{
jsonWriter . WritePropertyName ( "ntpServers" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( IPAddress ntpServer in scope . NtpServers )
jsonWriter . WriteStringValue ( ntpServer . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . NtpServerDomainNames is not null )
{
jsonWriter . WritePropertyName ( "ntpServerDomainNames" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( string ntpServerDomainName in scope . NtpServerDomainNames )
jsonWriter . WriteStringValue ( ntpServerDomainName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . StaticRoutes is not null )
{
jsonWriter . WritePropertyName ( "staticRoutes" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( ClasslessStaticRouteOption . Route route in scope . StaticRoutes )
{
jsonWriter . WriteStartObject ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "destination" , route . Destination . ToString ( ) ) ;
jsonWriter . WriteString ( "subnetMask" , route . SubnetMask . ToString ( ) ) ;
jsonWriter . WriteString ( "router" , route . Router . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndObject ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . VendorInfo is not null )
{
jsonWriter . WritePropertyName ( "vendorInfo" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( KeyValuePair < string , VendorSpecificInformationOption > entry in scope . VendorInfo )
{
jsonWriter . WriteStartObject ( ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "identifier" , entry . Key ) ;
jsonWriter . WriteString ( "information" , BitConverter . ToString ( entry . Value . Information ) . Replace ( '-' , ':' ) ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndObject ( ) ;
}
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . CAPWAPAcIpAddresses is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "capwapAcIpAddresses" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( IPAddress acIpAddress in scope . CAPWAPAcIpAddresses )
jsonWriter . WriteStringValue ( acIpAddress . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
if ( scope . TftpServerAddresses is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "tftpServerAddresses" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( IPAddress address in scope . TftpServerAddresses )
jsonWriter . WriteStringValue ( address . ToString ( ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
if ( scope . GenericOptions is not null )
{
jsonWriter . WritePropertyName ( "genericOptions" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( DhcpOption genericOption in scope . GenericOptions )
{
jsonWriter . WriteStartObject ( ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteNumber ( "code" , ( byte ) genericOption . Code ) ;
jsonWriter . WriteString ( "value" , BitConverter . ToString ( genericOption . RawValue ) . Replace ( '-' , ':' ) ) ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndObject ( ) ;
}
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
}
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
if ( scope . Exclusions is not null )
{
jsonWriter . WritePropertyName ( "exclusions" ) ;
jsonWriter . WriteStartArray ( ) ;
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
foreach ( Exclusion exclusion in scope . Exclusions )
{
jsonWriter . WriteStartObject ( ) ;
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "startingAddress" , exclusion . StartingAddress . ToString ( ) ) ;
jsonWriter . WriteString ( "endingAddress" , exclusion . EndingAddress . ToString ( ) ) ;
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndObject ( ) ;
}
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2022-12-24 13:10:48 +05:30
}
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "reservedLeases" ) ;
2021-09-18 19:02:40 +05:30
jsonWriter . WriteStartArray ( ) ;
2025-02-15 12:51:16 +05:30
foreach ( Lease reservedLease in scope . ReservedLeases )
2021-09-18 19:02:40 +05:30
{
jsonWriter . WriteStartObject ( ) ;
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "hostName" , reservedLease . HostName ) ;
jsonWriter . WriteString ( "hardwareAddress" , BitConverter . ToString ( reservedLease . HardwareAddress ) ) ;
jsonWriter . WriteString ( "address" , reservedLease . Address . ToString ( ) ) ;
jsonWriter . WriteString ( "comments" , reservedLease . Comments ) ;
2021-09-18 19:02:40 +05:30
jsonWriter . WriteEndObject ( ) ;
}
jsonWriter . WriteEndArray ( ) ;
2025-02-15 12:51:16 +05:30
jsonWriter . WriteBoolean ( "allowOnlyReservedLeases" , scope . AllowOnlyReservedLeases ) ;
jsonWriter . WriteBoolean ( "blockLocallyAdministeredMacAddresses" , scope . BlockLocallyAdministeredMacAddresses ) ;
jsonWriter . WriteBoolean ( "ignoreClientIdentifierOption" , scope . IgnoreClientIdentifierOption ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
public async Task SetDhcpScopeAsync ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = request . GetQueryOrForm ( "name" ) ;
string strStartingAddress = request . QueryOrForm ( "startingAddress" ) ;
string strEndingAddress = request . QueryOrForm ( "endingAddress" ) ;
string strSubnetMask = request . QueryOrForm ( "subnetMask" ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
bool scopeExists ;
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope is null )
{
//scope does not exists; create new scope
if ( string . IsNullOrEmpty ( strStartingAddress ) )
throw new DnsWebServiceException ( "Parameter 'startingAddress' missing." ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( string . IsNullOrEmpty ( strEndingAddress ) )
throw new DnsWebServiceException ( "Parameter 'endingAddress' missing." ) ;
2022-02-20 17:17:39 +05:30
2025-02-15 12:51:16 +05:30
if ( string . IsNullOrEmpty ( strSubnetMask ) )
throw new DnsWebServiceException ( "Parameter 'subnetMask' missing." ) ;
2022-02-20 17:17:39 +05:30
2025-02-15 12:51:16 +05:30
scopeExists = false ;
scope = new Scope ( scopeName , true , IPAddress . Parse ( strStartingAddress ) , IPAddress . Parse ( strEndingAddress ) , IPAddress . Parse ( strSubnetMask ) , _dnsWebService . _log , _dnsWebService . _dhcpServer ) ;
scope . IgnoreClientIdentifierOption = true ;
}
else
{
scopeExists = true ;
2022-02-20 17:17:39 +05:30
2025-02-15 12:51:16 +05:30
IPAddress startingAddress = string . IsNullOrEmpty ( strStartingAddress ) ? scope . StartingAddress : IPAddress . Parse ( strStartingAddress ) ;
IPAddress endingAddress = string . IsNullOrEmpty ( strEndingAddress ) ? scope . EndingAddress : IPAddress . Parse ( strEndingAddress ) ;
IPAddress subnetMask = string . IsNullOrEmpty ( strSubnetMask ) ? scope . SubnetMask : IPAddress . Parse ( strSubnetMask ) ;
2022-02-20 17:17:39 +05:30
2025-02-15 12:51:16 +05:30
//validate scope address
foreach ( KeyValuePair < string , Scope > entry in _dnsWebService . _dhcpServer . Scopes )
{
Scope existingScope = entry . Value ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( existingScope . Equals ( scope ) )
continue ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( existingScope . IsAddressInRange ( startingAddress ) | | existingScope . IsAddressInRange ( endingAddress ) )
throw new DhcpServerException ( "Scope with overlapping range already exists: " + existingScope . StartingAddress . ToString ( ) + "-" + existingScope . EndingAddress . ToString ( ) ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
scope . ChangeNetwork ( startingAddress , endingAddress , subnetMask ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "leaseTimeDays" , ushort . Parse , out ushort leaseTimeDays ) )
scope . LeaseTimeDays = leaseTimeDays ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "leaseTimeHours" , byte . Parse , out byte leaseTimeHours ) )
scope . LeaseTimeHours = leaseTimeHours ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "leaseTimeMinutes" , byte . Parse , out byte leaseTimeMinutes ) )
scope . LeaseTimeMinutes = leaseTimeMinutes ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "offerDelayTime" , ushort . Parse , out ushort offerDelayTime ) )
scope . OfferDelayTime = offerDelayTime ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "pingCheckEnabled" , bool . Parse , out bool pingCheckEnabled ) )
scope . PingCheckEnabled = pingCheckEnabled ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "pingCheckTimeout" , ushort . Parse , out ushort pingCheckTimeout ) )
scope . PingCheckTimeout = pingCheckTimeout ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "pingCheckRetries" , byte . Parse , out byte pingCheckRetries ) )
scope . PingCheckRetries = pingCheckRetries ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string domainName = request . QueryOrForm ( "domainName" ) ;
if ( domainName is not null )
scope . DomainName = domainName . Length = = 0 ? null : domainName ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string domainSearchList = request . QueryOrForm ( "domainSearchList" ) ;
if ( domainSearchList is not null )
{
if ( domainSearchList . Length = = 0 )
scope . DomainSearchList = null ;
else
scope . DomainSearchList = domainSearchList . Split ( _commaSeparator , StringSplitOptions . RemoveEmptyEntries ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "dnsUpdates" , bool . Parse , out bool dnsUpdates ) )
scope . DnsUpdates = dnsUpdates ;
2022-11-12 15:11:36 +05:30
2025-03-31 19:37:15 +05:30
if ( request . TryGetQueryOrForm ( "dnsTtl" , ZoneFile . ParseTtl , out uint dnsTtl ) )
2025-02-15 12:51:16 +05:30
scope . DnsTtl = dnsTtl ;
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
string serverAddress = request . QueryOrForm ( "serverAddress" ) ;
if ( serverAddress is not null )
scope . ServerAddress = serverAddress . Length = = 0 ? null : IPAddress . Parse ( serverAddress ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string serverHostName = request . QueryOrForm ( "serverHostName" ) ;
if ( serverHostName is not null )
scope . ServerHostName = serverHostName . Length = = 0 ? null : serverHostName ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string bootFileName = request . QueryOrForm ( "bootFileName" ) ;
if ( bootFileName is not null )
scope . BootFileName = bootFileName . Length = = 0 ? null : bootFileName ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string routerAddress = request . QueryOrForm ( "routerAddress" ) ;
if ( routerAddress is not null )
scope . RouterAddress = routerAddress . Length = = 0 ? null : IPAddress . Parse ( routerAddress ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "useThisDnsServer" , bool . Parse , out bool useThisDnsServer ) )
scope . UseThisDnsServer = useThisDnsServer ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! scope . UseThisDnsServer )
{
string dnsServers = request . QueryOrForm ( "dnsServers" ) ;
if ( dnsServers is not null )
{
if ( dnsServers . Length = = 0 )
scope . DnsServers = null ;
else
scope . DnsServers = dnsServers . Split ( IPAddress . Parse , ',' ) ;
}
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string winsServers = request . QueryOrForm ( "winsServers" ) ;
if ( winsServers is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
if ( winsServers . Length = = 0 )
scope . WinsServers = null ;
2021-09-18 19:02:40 +05:30
else
2025-02-15 12:51:16 +05:30
scope . WinsServers = winsServers . Split ( IPAddress . Parse , ',' ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
string ntpServers = request . QueryOrForm ( "ntpServers" ) ;
if ( ntpServers is not null )
{
if ( ntpServers . Length = = 0 )
scope . NtpServers = null ;
else
scope . NtpServers = ntpServers . Split ( IPAddress . Parse , ',' ) ;
}
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
string ntpServerDomainNames = request . QueryOrForm ( "ntpServerDomainNames" ) ;
if ( ntpServerDomainNames is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
if ( ntpServerDomainNames . Length = = 0 )
scope . NtpServerDomainNames = null ;
else
scope . NtpServerDomainNames = ntpServerDomainNames . Split ( _commaSeparator , StringSplitOptions . RemoveEmptyEntries ) ;
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
string strStaticRoutes = request . QueryOrForm ( "staticRoutes" ) ;
if ( strStaticRoutes is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
if ( strStaticRoutes . Length = = 0 )
{
scope . StaticRoutes = null ;
}
else
{
string [ ] strStaticRoutesParts = strStaticRoutes . Split ( '|' ) ;
List < ClasslessStaticRouteOption . Route > staticRoutes = new List < ClasslessStaticRouteOption . Route > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
for ( int i = 0 ; i < strStaticRoutesParts . Length ; i + = 3 )
staticRoutes . Add ( new ClasslessStaticRouteOption . Route ( IPAddress . Parse ( strStaticRoutesParts [ i + 0 ] ) , IPAddress . Parse ( strStaticRoutesParts [ i + 1 ] ) , IPAddress . Parse ( strStaticRoutesParts [ i + 2 ] ) ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
scope . StaticRoutes = staticRoutes ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
string strVendorInfo = request . QueryOrForm ( "vendorInfo" ) ;
if ( strVendorInfo is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
if ( strVendorInfo . Length = = 0 )
{
scope . VendorInfo = null ;
}
else
{
string [ ] strVendorInfoParts = strVendorInfo . Split ( '|' ) ;
Dictionary < string , VendorSpecificInformationOption > vendorInfo = new Dictionary < string , VendorSpecificInformationOption > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
for ( int i = 0 ; i < strVendorInfoParts . Length ; i + = 2 )
vendorInfo . Add ( strVendorInfoParts [ i + 0 ] , new VendorSpecificInformationOption ( strVendorInfoParts [ i + 1 ] ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
scope . VendorInfo = vendorInfo ;
}
2021-09-18 19:02:40 +05:30
}
2022-11-12 15:11:36 +05:30
2025-02-15 12:51:16 +05:30
string capwapAcIpAddresses = request . QueryOrForm ( "capwapAcIpAddresses" ) ;
if ( capwapAcIpAddresses is not null )
{
if ( capwapAcIpAddresses . Length = = 0 )
scope . CAPWAPAcIpAddresses = null ;
else
scope . CAPWAPAcIpAddresses = capwapAcIpAddresses . Split ( IPAddress . Parse , ',' ) ;
}
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
string tftpServerAddresses = request . QueryOrForm ( "tftpServerAddresses" ) ;
if ( tftpServerAddresses is not null )
2022-12-24 13:10:48 +05:30
{
2025-02-15 12:51:16 +05:30
if ( tftpServerAddresses . Length = = 0 )
scope . TftpServerAddresses = null ;
else
scope . TftpServerAddresses = tftpServerAddresses . Split ( IPAddress . Parse , ',' ) ;
2022-12-24 13:10:48 +05:30
}
2025-02-15 12:51:16 +05:30
string strGenericOptions = request . QueryOrForm ( "genericOptions" ) ;
if ( strGenericOptions is not null )
2022-12-24 13:10:48 +05:30
{
2025-02-15 12:51:16 +05:30
if ( strGenericOptions . Length = = 0 )
{
scope . GenericOptions = null ;
}
else
{
string [ ] strGenericOptionsParts = strGenericOptions . Split ( '|' ) ;
List < DhcpOption > genericOptions = new List < DhcpOption > ( ) ;
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
for ( int i = 0 ; i < strGenericOptionsParts . Length ; i + = 2 )
genericOptions . Add ( new DhcpOption ( ( DhcpOptionCode ) byte . Parse ( strGenericOptionsParts [ i + 0 ] ) , strGenericOptionsParts [ i + 1 ] ) ) ;
2022-12-24 13:10:48 +05:30
2025-02-15 12:51:16 +05:30
scope . GenericOptions = genericOptions ;
}
2022-12-24 13:10:48 +05:30
}
2025-02-15 12:51:16 +05:30
string strExclusions = request . QueryOrForm ( "exclusions" ) ;
if ( strExclusions is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
if ( strExclusions . Length = = 0 )
{
scope . Exclusions = null ;
}
else
{
string [ ] strExclusionsParts = strExclusions . Split ( '|' ) ;
List < Exclusion > exclusions = new List < Exclusion > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
for ( int i = 0 ; i < strExclusionsParts . Length ; i + = 2 )
exclusions . Add ( new Exclusion ( IPAddress . Parse ( strExclusionsParts [ i + 0 ] ) , IPAddress . Parse ( strExclusionsParts [ i + 1 ] ) ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
scope . Exclusions = exclusions ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
string strReservedLeases = request . QueryOrForm ( "reservedLeases" ) ;
if ( strReservedLeases is not null )
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
if ( strReservedLeases . Length = = 0 )
{
scope . ReservedLeases = null ;
}
else
{
string [ ] strReservedLeaseParts = strReservedLeases . Split ( '|' ) ;
List < Lease > reservedLeases = new List < Lease > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
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 ] ) ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
scope . ReservedLeases = reservedLeases ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "allowOnlyReservedLeases" , bool . Parse , out bool allowOnlyReservedLeases ) )
scope . AllowOnlyReservedLeases = allowOnlyReservedLeases ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "blockLocallyAdministeredMacAddresses" , bool . Parse , out bool blockLocallyAdministeredMacAddresses ) )
scope . BlockLocallyAdministeredMacAddresses = blockLocallyAdministeredMacAddresses ;
2022-09-24 11:52:29 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "ignoreClientIdentifierOption" , bool . Parse , out bool ignoreClientIdentifierOption ) )
scope . IgnoreClientIdentifierOption = ignoreClientIdentifierOption ;
2024-03-16 14:02:14 +05:30
2025-02-15 12:51:16 +05:30
if ( scopeExists )
{
_dnsWebService . _dhcpServer . SaveScope ( scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope was updated successfully: " + scopeName ) ;
}
else
{
await _dnsWebService . _dhcpServer . AddScopeAsync ( scope ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope was added successfully: " + scopeName ) ;
}
2022-03-06 16:11:06 +05:30
2025-02-15 12:51:16 +05:30
if ( request . TryGetQueryOrForm ( "newName" , out string newName ) & & ! newName . Equals ( scopeName ) )
{
_dnsWebService . _dhcpServer . RenameScope ( scopeName , newName ) ;
2022-03-06 16:11:06 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope was renamed successfully: '" + scopeName + "' to '" + newName + "'" ) ;
}
2022-03-06 16:11:06 +05:30
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void AddReservedLease ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = request . GetQueryOrForm ( "name" ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope is null )
throw new DnsWebServiceException ( "No such scope exists: " + scopeName ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
string hostName = request . QueryOrForm ( "hostName" ) ;
string hardwareAddress = request . GetQueryOrForm ( "hardwareAddress" ) ;
string strIpAddress = request . GetQueryOrForm ( "ipAddress" ) ;
string comments = request . QueryOrForm ( "comments" ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
Lease reservedLease = new Lease ( LeaseType . Reserved , hostName , DhcpMessageHardwareAddressType . Ethernet , hardwareAddress , IPAddress . Parse ( strIpAddress ) , comments ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
if ( ! scope . AddReservedLease ( reservedLease ) )
throw new DnsWebServiceException ( "Failed to add reserved lease for scope: " + scopeName ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . SaveScope ( scopeName ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope reserved lease was added successfully: " + scopeName ) ;
}
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
public void RemoveReservedLease ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = request . GetQueryOrForm ( "name" ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope is null )
throw new DnsWebServiceException ( "No such scope exists: " + scopeName ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
string hardwareAddress = request . GetQueryOrForm ( "hardwareAddress" ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
if ( ! scope . RemoveReservedLease ( hardwareAddress ) )
throw new DnsWebServiceException ( "Failed to remove reserved lease for scope: " + scopeName ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . SaveScope ( scopeName ) ;
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope reserved lease was removed successfully: " + scopeName ) ;
}
2022-09-18 17:50:30 +05:30
2025-02-15 12:51:16 +05:30
public async Task EnableDhcpScopeAsync ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = context . Request . GetQueryOrForm ( "name" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
await _dnsWebService . _dhcpServer . EnableScopeAsync ( scopeName , true ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope was enabled successfully: " + scopeName ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void DisableDhcpScope ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = context . Request . GetQueryOrForm ( "name" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . DisableScope ( scopeName , true ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope was disabled successfully: " + scopeName ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void DeleteDhcpScope ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = context . Request . GetQueryOrForm ( "name" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . DeleteScope ( scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope was deleted successfully: " + scopeName ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void RemoveDhcpLease ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = request . GetQueryOrForm ( "name" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope is null )
throw new DnsWebServiceException ( "DHCP scope does not exists: " + scopeName ) ;
2022-04-30 13:04:36 +05:30
2025-02-15 12:51:16 +05:30
string clientIdentifier = request . QueryOrForm ( "clientIdentifier" ) ;
string hardwareAddress = request . QueryOrForm ( "hardwareAddress" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! string . IsNullOrEmpty ( clientIdentifier ) )
scope . RemoveLease ( ClientIdentifierOption . Parse ( clientIdentifier ) ) ;
else if ( ! string . IsNullOrEmpty ( hardwareAddress ) )
scope . RemoveLease ( hardwareAddress ) ;
else
throw new DnsWebServiceException ( " Parameter ' hardwareAddress ' or ' clientIdentifier ' missing . At least one of them must be specified . ");
2022-04-30 12:11:43 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . SaveScope ( scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope's lease was removed successfully: " + scopeName ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void ConvertToReservedLease ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = request . GetQueryOrForm ( "name" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope = = null )
throw new DnsWebServiceException ( "DHCP scope does not exists: " + scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string clientIdentifier = request . QueryOrForm ( "clientIdentifier" ) ;
string hardwareAddress = request . QueryOrForm ( "hardwareAddress" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! string . IsNullOrEmpty ( clientIdentifier ) )
scope . ConvertToReservedLease ( ClientIdentifierOption . Parse ( clientIdentifier ) ) ;
else if ( ! string . IsNullOrEmpty ( hardwareAddress ) )
scope . ConvertToReservedLease ( hardwareAddress ) ;
else
throw new DnsWebServiceException ( "Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified." ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . SaveScope ( scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope's lease was reserved successfully: " + scopeName ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void ConvertToDynamicLease ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . DhcpServer , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:05:07 +05:30
2025-02-15 12:51:16 +05:30
string scopeName = request . GetQueryOrForm ( "name" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Scope scope = _dnsWebService . _dhcpServer . GetScope ( scopeName ) ;
if ( scope = = null )
throw new DnsWebServiceException ( "DHCP scope does not exists: " + scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string clientIdentifier = request . QueryOrForm ( "clientIdentifier" ) ;
string hardwareAddress = request . QueryOrForm ( "hardwareAddress" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( ! string . IsNullOrEmpty ( clientIdentifier ) )
scope . ConvertToDynamicLease ( ClientIdentifierOption . Parse ( clientIdentifier ) ) ;
else if ( ! string . IsNullOrEmpty ( hardwareAddress ) )
scope . ConvertToDynamicLease ( hardwareAddress ) ;
else
throw new DnsWebServiceException ( "Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified." ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dhcpServer . SaveScope ( scopeName ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] DHCP scope's lease was unreserved successfully: " + scopeName ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
}
2021-09-18 19:02:40 +05:30
}
}