dns server code refactoring done.

This commit is contained in:
Shreyas Zare
2019-06-15 13:28:39 +05:30
parent ed802b5693
commit 3338038867
10 changed files with 316 additions and 4369 deletions

View File

@@ -0,0 +1,75 @@
/*
Technitium DNS Server
Copyright (C) 2019 Shreyas Zare (shreyas@technitium.com)
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/>.
*/
using System;
using System.IO;
namespace DnsServerCore.Dns
{
public class DnsResourceRecordInfo
{
#region variables
readonly bool _disabled;
#endregion
#region constructor
public DnsResourceRecordInfo()
{ }
public DnsResourceRecordInfo(bool disabled)
{
_disabled = disabled;
}
public DnsResourceRecordInfo(BinaryReader bR)
{
switch (bR.ReadByte()) //version
{
case 1:
_disabled = bR.ReadBoolean();
break;
default:
throw new NotSupportedException("DnsResourceRecordInfo format version not supported.");
}
}
#endregion
#region public
public void WriteTo(BinaryWriter bW)
{
bW.Write((byte)1); //version
bW.Write(_disabled);
}
#endregion
#region properties
public bool Disabled
{ get { return _disabled; } }
#endregion
}
}

View File

@@ -32,9 +32,10 @@ using System.Threading;
using TechnitiumLibrary.IO;
using TechnitiumLibrary.Net;
using TechnitiumLibrary.Net.Dns;
using TechnitiumLibrary.Net.Dns.ResourceRecords;
using TechnitiumLibrary.Net.Proxy;
namespace DnsServerCore
namespace DnsServerCore.Dns
{
public class DnsServer : IDisposable
{
@@ -75,9 +76,6 @@ namespace DnsServerCore
readonly Zone _allowedZoneRoot = new Zone(true);
Zone _blockedZoneRoot = new Zone(true);
const uint NEGATIVE_RECORD_TTL = 300u;
const uint MINIMUM_RECORD_TTL = 10u;
const uint SERVE_STALE_TTL = 7 * 24 * 60 * 60; //7 days serve stale ttl as per draft-ietf-dnsop-serve-stale-04
readonly DnsCache _dnsCache;
bool _allowRecursion = false;
@@ -161,19 +159,8 @@ namespace DnsServerCore
return;
if (disposing)
{
Stop();
if (_log != null)
_log.Dispose();
if (_queryLog != null)
_queryLog.Dispose();
if (_stats != null)
_stats.Dispose();
}
_disposed = true;
}
@@ -202,11 +189,6 @@ namespace DnsServerCore
{
while (true)
{
if (udpListener.AddressFamily == AddressFamily.InterNetwork)
remoteEP = new IPEndPoint(IPAddress.Any, 0);
else
remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
try
{
bytesRecv = udpListener.ReceiveFrom(recvBuffer, ref remoteEP);
@@ -2104,102 +2086,5 @@ namespace DnsServerCore
}
#endregion
class ResolverDnsCache : DnsCache
{
#region variables
readonly protected Zone _cacheZoneRoot;
#endregion
#region constructor
public ResolverDnsCache(Zone cacheZoneRoot)
: base(NEGATIVE_RECORD_TTL, MINIMUM_RECORD_TTL, SERVE_STALE_TTL)
{
_cacheZoneRoot = cacheZoneRoot;
}
#endregion
#region public
public override DnsDatagram Query(DnsDatagram request)
{
return _cacheZoneRoot.Query(request);
}
protected override void CacheRecords(ICollection<DnsResourceRecord> resourceRecords)
{
_cacheZoneRoot.SetRecords(resourceRecords);
}
#endregion
}
class ResolverPrefetchDnsCache : ResolverDnsCache
{
#region variables
readonly DnsQuestionRecord _prefetchQuery;
#endregion
#region constructor
public ResolverPrefetchDnsCache(Zone cacheZoneRoot, DnsQuestionRecord prefetchQuery)
: base(cacheZoneRoot)
{
_prefetchQuery = prefetchQuery;
}
#endregion
#region public
public override DnsDatagram Query(DnsDatagram request)
{
if (_prefetchQuery.Equals(request.Question[0]))
return _cacheZoneRoot.QueryCacheGetClosestNameServers(request); //return closest name servers so that the recursive resolver queries them to refreshes cache instead of returning response from cache
return _cacheZoneRoot.Query(request);
}
#endregion
}
class RecursiveQueryLock
{
#region variables
bool _complete;
DnsDatagram _response;
#endregion
#region public
public void SetComplete(DnsDatagram response)
{
if (!_complete)
{
_complete = true;
_response = response;
}
}
#endregion
#region properties
public bool Complete
{ get { return _complete; } }
public DnsDatagram Response
{ get { return _response; } }
#endregion
}
}
}

View File

@@ -1,6 +1,6 @@
/*
Technitium DNS Server
Copyright (C) 2017 Shreyas Zare (shreyas@technitium.com)
Copyright (C) 2019 Shreyas Zare (shreyas@technitium.com)
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
@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace DnsServerCore
namespace DnsServerCore.Dns
{
public class DnsServerException : Exception
{

View File

@@ -0,0 +1,56 @@
/*
Technitium DNS Server
Copyright (C) 2019 Shreyas Zare (shreyas@technitium.com)
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/>.
*/
using TechnitiumLibrary.Net.Dns;
namespace DnsServerCore.Dns
{
class RecursiveQueryLock
{
#region variables
bool _complete;
DnsDatagram _response;
#endregion
#region public
public void SetComplete(DnsDatagram response)
{
if (!_complete)
{
_complete = true;
_response = response;
}
}
#endregion
#region properties
public bool Complete
{ get { return _complete; } }
public DnsDatagram Response
{ get { return _response; } }
#endregion
}
}

View File

@@ -0,0 +1,61 @@
/*
Technitium DNS Server
Copyright (C) 2019 Shreyas Zare (shreyas@technitium.com)
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/>.
*/
using System.Collections.Generic;
using TechnitiumLibrary.Net.Dns;
namespace DnsServerCore.Dns
{
class ResolverDnsCache : DnsCache
{
#region variables
const uint NEGATIVE_RECORD_TTL = 300u;
const uint MINIMUM_RECORD_TTL = 10u;
const uint SERVE_STALE_TTL = 7 * 24 * 60 * 60; //7 days serve stale ttl as per draft-ietf-dnsop-serve-stale-04
readonly protected Zone _cacheZoneRoot;
#endregion
#region constructor
public ResolverDnsCache(Zone cacheZoneRoot)
: base(NEGATIVE_RECORD_TTL, MINIMUM_RECORD_TTL, SERVE_STALE_TTL)
{
_cacheZoneRoot = cacheZoneRoot;
}
#endregion
#region public
public override DnsDatagram Query(DnsDatagram request)
{
return _cacheZoneRoot.Query(request);
}
protected override void CacheRecords(ICollection<DnsResourceRecord> resourceRecords)
{
_cacheZoneRoot.SetRecords(resourceRecords);
}
#endregion
}
}

View File

@@ -0,0 +1,54 @@
/*
Technitium DNS Server
Copyright (C) 2019 Shreyas Zare (shreyas@technitium.com)
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/>.
*/
using TechnitiumLibrary.Net.Dns;
namespace DnsServerCore.Dns
{
class ResolverPrefetchDnsCache : ResolverDnsCache
{
#region variables
readonly DnsQuestionRecord _prefetchQuery;
#endregion
#region constructor
public ResolverPrefetchDnsCache(Zone cacheZoneRoot, DnsQuestionRecord prefetchQuery)
: base(cacheZoneRoot)
{
_prefetchQuery = prefetchQuery;
}
#endregion
#region public
public override DnsDatagram Query(DnsDatagram request)
{
if (_prefetchQuery.Equals(request.Question[0]))
return _cacheZoneRoot.QueryCacheGetClosestNameServers(request); //return closest name servers so that the recursive resolver queries them to refreshes cache instead of returning response from cache
return _cacheZoneRoot.Query(request);
}
#endregion
}
}

View File

@@ -28,7 +28,7 @@ using TechnitiumLibrary.IO;
using TechnitiumLibrary.Net;
using TechnitiumLibrary.Net.Dns;
namespace DnsServerCore
namespace DnsServerCore.Dns
{
public enum StatsResponseType
{

View File

@@ -20,10 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using TechnitiumLibrary.Net.Dns;
using TechnitiumLibrary.Net.Dns.ResourceRecords;
namespace DnsServerCore
namespace DnsServerCore.Dns
{
public class Zone
{
@@ -1028,7 +1028,7 @@ namespace DnsServerCore
List<ZoneInfo> zoneNames = new List<ZoneInfo>();
foreach (Zone zone in zones)
zoneNames.Add(new ZoneInfo(zone));
zoneNames.Add(new ZoneInfo(zone._zoneName, zone._disabled));
return zoneNames;
}
@@ -1096,101 +1096,5 @@ namespace DnsServerCore
}
#endregion
public class ZoneInfo : IComparable<ZoneInfo>
{
#region variables
readonly string _zoneName;
readonly bool _disabled;
#endregion
#region constructor
public ZoneInfo(string zoneName, bool disabled)
{
_zoneName = zoneName;
_disabled = disabled;
}
public ZoneInfo(Zone zone)
{
_zoneName = zone._zoneName;
_disabled = zone._disabled;
}
#endregion
#region public
public int CompareTo(ZoneInfo other)
{
return this._zoneName.CompareTo(other._zoneName);
}
#endregion
#region properties
public string ZoneName
{ get { return _zoneName; } }
public bool Disabled
{ get { return _disabled; } }
#endregion
}
public class DnsResourceRecordInfo
{
#region variables
readonly bool _disabled;
#endregion
#region constructor
public DnsResourceRecordInfo()
{ }
public DnsResourceRecordInfo(bool disabled)
{
_disabled = disabled;
}
public DnsResourceRecordInfo(BinaryReader bR)
{
switch (bR.ReadByte()) //version
{
case 1:
_disabled = bR.ReadBoolean();
break;
default:
throw new NotSupportedException("Zone.DnsResourceRecordInfo format version not supported.");
}
}
#endregion
#region public
public void WriteTo(BinaryWriter bW)
{
bW.Write((byte)1); //version
bW.Write(_disabled);
}
#endregion
#region properties
public bool Disabled
{ get { return _disabled; } }
#endregion
}
}
}

View File

@@ -0,0 +1,62 @@
/*
Technitium DNS Server
Copyright (C) 2019 Shreyas Zare (shreyas@technitium.com)
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/>.
*/
using System;
namespace DnsServerCore.Dns
{
public class ZoneInfo : IComparable<ZoneInfo>
{
#region variables
readonly string _zoneName;
readonly bool _disabled;
#endregion
#region constructor
public ZoneInfo(string zoneName, bool disabled)
{
_zoneName = zoneName;
_disabled = disabled;
}
#endregion
#region public
public int CompareTo(ZoneInfo other)
{
return this._zoneName.CompareTo(other._zoneName);
}
#endregion
#region properties
public string ZoneName
{ get { return _zoneName; } }
public bool Disabled
{ get { return _disabled; } }
#endregion
}
}

File diff suppressed because it is too large Load Diff