Skip to content

Commit

Permalink
Implement SOA record
Browse files Browse the repository at this point in the history
  • Loading branch information
jdomnitz committed Jul 19, 2024
1 parent 3b7901d commit cd60eb1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions TinyDNS/Enums/DNSRecordType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum DNSRecordType : ushort
AAAA = 0x1C,
SRV = 0x21,
DNAME = 0x27,
SVCB = 0x40,
HTTPS = 0x41,
ANY = 0xFF,
}
}
3 changes: 3 additions & 0 deletions TinyDNS/Records/ResourceRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public static ResourceRecord Parse(Span<byte> buffer, ref int pos)
case DNSRecordType.NS:
record = new NSRecord(header, buffer, ref pos);
break;
case DNSRecordType.SOA:
record = new SOARecord(header, buffer, ref pos);
break;
default:
record = new UnsupportedRecord(header, buffer, ref pos);
break;
Expand Down
61 changes: 61 additions & 0 deletions TinyDNS/Records/SOARecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// TinyDNS Copyright (C) 2024
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or 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 Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System.Buffers.Binary;
using TinyDNS.Enums;

namespace TinyDNS.Records
{
public class SOARecord : ResourceRecord
{
public List<string> MNameLabels { get; }
public List<string> RNameLabels { get; }
public Version Serial { get; set; }
public TimeSpan Refresh { get; set; }
public TimeSpan Retry { get; set; }
public TimeSpan Expire { get; set; }
public TimeSpan Minimum { get; set; }
public string MName { get { return string.Join('.', MNameLabels); } }

internal SOARecord(ResourceRecordHeader header, Span<byte> buffer, ref int pos) : base(header)
{
pos += 2;
MNameLabels = DomainParser.Read(buffer, ref pos);
RNameLabels = DomainParser.Read(buffer, ref pos);
Serial = new Version((int)BinaryPrimitives.ReadUInt32BigEndian(buffer.Slice(pos, 4)), 0);
pos += 4;
Refresh = TimeSpan.FromSeconds(BinaryPrimitives.ReadInt32BigEndian(buffer.Slice(pos, 4)));
pos += 4;
Retry = TimeSpan.FromSeconds(BinaryPrimitives.ReadInt32BigEndian(buffer.Slice(pos, 4)));
pos += 4;
Expire = TimeSpan.FromSeconds(BinaryPrimitives.ReadInt32BigEndian(buffer.Slice(pos, 4)));
pos += 4;
Minimum = TimeSpan.FromSeconds(BinaryPrimitives.ReadUInt32BigEndian(buffer.Slice(pos, 4)));
pos += 4;
}

public SOARecord(string mname, string rname, TimeSpan minimum, List<string> labels, DNSClass @class, uint ttl) : base(labels, DNSRecordType.SOA, @class, ttl)
{
MNameLabels = DomainParser.Parse(mname);
RNameLabels = DomainParser.Parse(rname);
Minimum = minimum;
Serial = new Version();
}

public override bool Equals(ResourceRecord? other)
{
if (other is SOARecord otherSOA)
return base.Equals(other) && MNameLabels.SequenceEqual(otherSOA.MNameLabels) && Serial.Equals(otherSOA.Serial);
return false;
}
}
}
11 changes: 11 additions & 0 deletions TinyDNS/TinyDNS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.5</Version>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>jdomnitz</Authors>
<Company>SmartHomeOS and Contributors</Company>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down

0 comments on commit cd60eb1

Please sign in to comment.