Skip to content

Commit

Permalink
- Added RFC 8976
Browse files Browse the repository at this point in the history
- Added Zone Validation
- Optimized Zone Signing
- Optimized Zone masterfile parsing
- Optimized pipelined client transport
  • Loading branch information
alexreinert committed Jun 20, 2023
1 parent 5bff4c5 commit b6849c9
Show file tree
Hide file tree
Showing 37 changed files with 1,419 additions and 718 deletions.
2 changes: 1 addition & 1 deletion ARSoft.Tools.Net/ARSoft.Tools.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Copyright>Copyright 2010..2023 Alexander Reinert</Copyright>
<VersionPrefix>3.4.0</VersionPrefix>
<VersionPrefix>3.5.0</VersionPrefix>
</PropertyGroup>

<ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions ARSoft.Tools.Net/DisposableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#region Copyright and License
// Copyright 2010..2023 Alexander Reinert
//
// This file is part of the ARSoft.Tools.Net - C# DNS client/server and SPF Library (https://github.com/alexreinert/ARSoft.Tools.Net)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace ARSoft.Tools.Net;

internal static class DisposableExtensions
{
public static bool TryDispose(this IDisposable disposable)
{
try
{
disposable.Dispose();
return true;
}
catch
{
return false;
}
}
}
9 changes: 3 additions & 6 deletions ARSoft.Tools.Net/Dns/DnsMessageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,9 @@ internal static void EncodeTextBlock(IList<byte> messageData, ref int currentPos
{
var textData = Encoding.ASCII.GetBytes(text);

for (var i = 0; i < textData.Length; i += 255)
{
var blockLength = Math.Min(255, (textData.Length - i));
messageData[currentPosition++] = (byte) blockLength;
EncodeByteArray(messageData, ref currentPosition, textData, i, blockLength);
}
var blockLength = Math.Min(255, textData.Length);
messageData[currentPosition++] = (byte) blockLength;
EncodeByteArray(messageData, ref currentPosition, textData, 0, blockLength);
}

internal static void EncodeTextWithoutLength(IList<byte> messageData, ref int currentPosition, string text)
Expand Down
2 changes: 1 addition & 1 deletion ARSoft.Tools.Net/Dns/DnsRecord/AplRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ internal static AddressPrefix Parse(string s)
if ((address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) && (groups["fam"].Value != "1"))
throw new FormatException();

return new AddressPrefix(groups["isneg"].Success, address, Byte.Parse(groups["pref"].Value));
return new AddressPrefix(groups["isneg"].Length > 0, address, Byte.Parse(groups["pref"].Value));
}
}

Expand Down
13 changes: 12 additions & 1 deletion ARSoft.Tools.Net/Dns/DnsRecord/CertRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal CertRecord(DomainName name, RecordType recordType, RecordClass recordCl
if (stringRepresentation.Length < 4)
throw new FormatException();

Type = (CertType) UInt16.Parse(stringRepresentation[0]);
Type = ParseCertType(stringRepresentation[0]);
KeyTag = UInt16.Parse(stringRepresentation[1]);
Algorithm = (DnsSecAlgorithm) Byte.Parse(stringRepresentation[2]);
Certificate = String.Join(String.Empty, stringRepresentation.Skip(3)).FromBase64String();
Expand All @@ -196,6 +196,17 @@ public CertRecord(DomainName name, int timeToLive, CertType type, ushort keyTag,
Certificate = certificate ?? new byte[] { };
}

private static CertType ParseCertType(string s)
{
if (EnumHelper<CertType>.TryParse(s, true, out var mnemonic))
return mnemonic;

if (UInt16.TryParse(s, out var numeric))
return (CertType) numeric;

throw new FormatException();
}

internal override string RecordDataToString()
{
return (ushort) Type
Expand Down
16 changes: 8 additions & 8 deletions ARSoft.Tools.Net/Dns/DnsRecord/DnsRecordBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ internal static DnsRecordBase ParseRecordFromBinary(int recordStartPosition, Dom
return new OpenPGPKeyRecord(name, recordType, recordClass, timeToLive, resultData, recordDataPosition, recordDataLength);
case RecordType.CSync:
return new CSyncRecord(name, recordType, recordClass, timeToLive, resultData, recordDataPosition, recordDataLength);
case RecordType.ZoneMD:
return new ZoneMDRecord(name, recordType, recordClass, timeToLive, resultData, recordDataPosition, recordDataLength);
case RecordType.SvcB:
return new SvcBRecord(name, recordType, recordClass, timeToLive, resultData, recordDataPosition, recordDataLength);
case RecordType.Https:
Expand Down Expand Up @@ -322,6 +324,8 @@ internal static DnsRecordBase ParseFromStringRepresentation(DomainName name, Rec
return new OpenPGPKeyRecord(name, recordType, recordClass, timeToLive, origin, stringRepresentation);
case RecordType.CSync:
return new CSyncRecord(name, recordType, recordClass, timeToLive, origin, stringRepresentation);
case RecordType.ZoneMD:
return new ZoneMDRecord(name, recordType, recordClass, timeToLive, origin, stringRepresentation);
case RecordType.SvcB:
return new SvcBRecord(name, recordType, recordClass, timeToLive, origin, stringRepresentation);
case RecordType.Https:
Expand Down Expand Up @@ -372,13 +376,13 @@ protected DomainName ParseDomainName(DomainName origin, string name)
#region Encoding
internal sealed override int MaximumLength => Name.MaximumRecordDataLength + 12 + MaximumRecordDataLength;

internal void Encode(IList<byte> messageData, ref int currentPosition, Dictionary<DomainName, ushort> domainNames, bool useCanonical = false)
internal void Encode(IList<byte> messageData, ref int currentPosition, Dictionary<DomainName, ushort>? domainNames, bool useCanonical = false)
{
EncodeRecordHeader(messageData, ref currentPosition, domainNames, useCanonical);
EncodeRecordBody(messageData, ref currentPosition, domainNames, useCanonical);
}

internal void EncodeRecordHeader(IList<byte> messageData, ref int currentPosition, Dictionary<DomainName, ushort> domainNames, bool useCanonical)
internal void EncodeRecordHeader(IList<byte> messageData, ref int currentPosition, Dictionary<DomainName, ushort>? domainNames, bool useCanonical)
{
DnsMessageBase.EncodeDomainName(messageData, ref currentPosition, Name, domainNames, useCanonical);
DnsMessageBase.EncodeUShort(messageData, ref currentPosition, (ushort) RecordType);
Expand Down Expand Up @@ -508,17 +512,13 @@ public int CompareTo(DnsRecordBase? other)
if (compare != 0)
return compare;

compare = TimeToLive.CompareTo(other.TimeToLive);
if (compare != 0)
return compare;

var thisBuffer = new byte[MaximumRecordDataLength];
int thisLength = 0;
EncodeRecordData(thisBuffer, ref thisLength, null, false);
EncodeRecordData(thisBuffer, ref thisLength, null, true);

var otherBuffer = new byte[other.MaximumRecordDataLength];
int otherLength = 0;
other.EncodeRecordData(otherBuffer, ref otherLength, null, false);
other.EncodeRecordData(otherBuffer, ref otherLength, null, true);

for (int i = 0; i < Math.Min(thisLength, otherLength); i++)
{
Expand Down
2 changes: 1 addition & 1 deletion ARSoft.Tools.Net/Dns/DnsRecord/LocRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace ARSoft.Tools.Net.Dns
/// </summary>
public class LocRecord : DnsRecordBase
{
private static readonly Regex _parserRegex = new Regex(@"^(?<latd>\d{1,2})( (?<latm>\d{1,2})( (?<lats>\d{1,2})(\.(?<latms>\d{1,3}))?)?)? (?<lat>(N|S)) (?<longd>\d{1,2})( (?<longm>\d{1,2})( (?<longs>\d{1,2})(\.(?<longms>\d{1,3}))?)?)? (?<long>(W|E)) (?<alt>-?\d{1,2}(\.\d+)?)m?( (?<size>\d+(\.\d+)?)m?( (?<hp>\d+(\.\d+)?)m?( (?<vp>\d+(\.\d+)?)m?)?)?)?$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static readonly Regex _parserRegex = new Regex(@"^(?<latd>\d{1,2})( (?<latm>\d{1,2})( (?<lats>\d{1,2})(\.(?<latms>\d{1,3}))?)?)? (?<lat>(N|S)) (?<longd>\d{1,3})( (?<longm>\d{1,2})( (?<longs>\d{1,2})(\.(?<longms>\d{1,3}))?)?)? (?<long>(W|E)) (?<alt>-?\d+(\.\d+)?)m?( (?<size>\d+(\.\d+)?)m?( (?<hp>\d+(\.\d+)?)m?( (?<vp>\d+(\.\d+)?)m?)?)?)?$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);

/// <summary>
/// Represents a geopgraphical degree
Expand Down
2 changes: 1 addition & 1 deletion ARSoft.Tools.Net/Dns/DnsRecord/NaptrRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal NaptrRecord(DomainName name, RecordType recordType, RecordClass recordC
: base(name, recordType, recordClass, timeToLive)
{
if (stringRepresentation.Length != 6)
throw new NotSupportedException();
throw new FormatException();

Order = UInt16.Parse(stringRepresentation[0]);
Preference = UInt16.Parse(stringRepresentation[1]);
Expand Down
Loading

0 comments on commit b6849c9

Please sign in to comment.