Skip to content

Commit

Permalink
More work on zone and update client interaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanedwardes committed Feb 5, 2024
1 parent 39fea18 commit 5089a3d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
16 changes: 12 additions & 4 deletions src/Ae.Dns.Client/DnsUpdateClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Ae.Dns.Client.Zone;
using Ae.Dns.Protocol;
using Ae.Dns.Protocol.Enums;
using Ae.Dns.Protocol.Records;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,11 +32,17 @@ public async Task<DnsMessage> Query(DnsMessage query, CancellationToken token =
{
query.EnsureOperationCode(DnsOperationCode.UPDATE);

var recordsToAdd = query.Nameservers.Where(x => x.Type == DnsQueryType.A ||
x.Type == DnsQueryType.AAAA)
.ToArray();
var hostnames = query.Nameservers.Select(x => x.Host).ToArray();

if (recordsToAdd.Length > 0 && await _dnsZone.AddRecords(recordsToAdd, token))
void RemoveStaleRecords(ICollection<DnsResourceRecord> records)
{
foreach (var recordToRemove in records.Where(x => hostnames.Contains(x.Host)).ToArray())
{
records.Remove(recordToRemove);
}
};

if (query.Nameservers.Count > 0 && hostnames.Any(x => x.Last() != _dnsZone.Name) && await _dnsZone.ChangeRecords(RemoveStaleRecords, query.Nameservers, token))
{
return query.CreateAnswerMessage(DnsResponseCode.NoError, ToString());
}
Expand Down
19 changes: 6 additions & 13 deletions src/Ae.Dns.Client/Zone/FileDnsZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -34,6 +33,9 @@ public FileDnsZone(string name, FileInfo file)
/// <inheritdoc/>
public IEnumerable<DnsResourceRecord> Records => _records;

/// <inheritdoc/>
public string Name => _name;

private async Task ReloadZone(CancellationToken token)
{
await _zoneLock.WaitAsync(token);
Expand All @@ -49,28 +51,19 @@ private async Task ReloadZone(CancellationToken token)
}

/// <inheritdoc/>
public async Task<bool> AddRecords(IEnumerable<DnsResourceRecord> recordsToAdd, CancellationToken token = default)
public async Task<bool> ChangeRecords(Action<ICollection<DnsResourceRecord>> changeDelegate, IEnumerable<DnsResourceRecord> recordsToAdd, CancellationToken token = default)
{
if (recordsToAdd.Any(x => x.Host.Last() != _name))
{
return false;
}

await _zoneLock.WaitAsync(token);

try
{
var recordsToRemove = _records.Where(x => recordsToAdd.Select(x => x.Host).Contains(x.Host)).ToArray();
changeDelegate(_records);

foreach (var recordToAdd in recordsToAdd)
{
_records.Add(recordToAdd);
}

foreach (var recordToRemove in recordsToRemove)
{
_records.Remove(recordToRemove);
}

SerializeZone();
}
finally
Expand Down
10 changes: 8 additions & 2 deletions src/Ae.Dns.Client/Zone/IDnsZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ public interface IDnsZone
/// <summary>
/// Add the specified enumerable of <see cref="DnsResourceRecord"/> to this zone.
/// </summary>
/// <param name="records"></param>
/// <param name="changeDelegate"></param>
/// <param name="recordsToAdd"></param>
/// <param name="token"></param>
Task<bool> AddRecords(IEnumerable<DnsResourceRecord> records, CancellationToken token = default);
Task<bool> ChangeRecords(Action<ICollection<DnsResourceRecord>> changeDelegate, IEnumerable<DnsResourceRecord> recordsToAdd, CancellationToken token = default);

/// <summary>
/// Get all records in the zone.
/// </summary>
IEnumerable<DnsResourceRecord> Records { get; }

/// <summary>
/// The name of the zone.
/// </summary>
string Name { get; }
}
}
8 changes: 6 additions & 2 deletions tests/Ae.Dns.Tests/Client/Lookup/DnsZoneLookupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ private sealed class DummyZoneNoRecords : IDnsZone
{
public IEnumerable<DnsResourceRecord> Records => Enumerable.Empty<DnsResourceRecord>();

public Task<bool> AddRecords(IEnumerable<DnsResourceRecord> records, CancellationToken token = default)
public string Name => throw new NotImplementedException();

public Task<bool> ChangeRecords(Action<ICollection<DnsResourceRecord>> changeDelegate, IEnumerable<DnsResourceRecord> recordsToAdd, CancellationToken token = default)
{
throw new NotImplementedException();
}
Expand All @@ -47,7 +49,9 @@ private sealed class DummyZoneWithRecords : IDnsZone
new DnsResourceRecord { Host = "wibble", Class = DnsQueryClass.IN, Type = DnsQueryType.TEXT, Resource = new DnsTextResource { Entries = "hello2" } },
};

public Task<bool> AddRecords(IEnumerable<DnsResourceRecord> records, CancellationToken token = default)
public string Name => throw new NotImplementedException();

public Task<bool> ChangeRecords(Action<ICollection<DnsResourceRecord>> changeDelegate, IEnumerable<DnsResourceRecord> recordsToAdd, CancellationToken token = default)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit 5089a3d

Please sign in to comment.