Skip to content

Commit

Permalink
Merge pull request #19 from richardschneider/18-quad9-formerr
Browse files Browse the repository at this point in the history
quad9 returning FORMERR
  • Loading branch information
richardschneider committed May 24, 2019
2 parents 4d8591a + 83a833f commit 4970552
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/DotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Makaretu.Dns;

namespace Makaretu.Dns
{
Expand All @@ -33,6 +34,7 @@ public class DotClient : DnsClientBase
{
SslStream dnsServer;
readonly AsyncLock dnsServerLock = new AsyncLock();
readonly Random rng = new Random();

/// <summary>
/// The default port of a DOT server.
Expand Down Expand Up @@ -102,11 +104,14 @@ public class DotClient : DnsClientBase
Pins = new[] { "h3mufC43MEqRD6uE4lz6gAgULZ5/riqH/E+U+jE3H8g=" },
Address = IPAddress.Parse("146.185.167.43")
},
// see https://github.com/richardschneider/net-udns/issues/18"
#if false
new DotEndPoint
{
Hostname = "dns.quad9.net",
Address = IPAddress.Parse("9.9.9.9")
},
#endif
};

static ILog log = LogManager.GetLogger(typeof(DotClient));
Expand All @@ -120,7 +125,7 @@ public class DotClient : DnsClientBase
/// <remarks>
/// All queries are padded to the closest multiple of <see cref="BlockLength"/> octets.
/// </remarks>
/// <seealso href="https://tools.ietf.org/html/draft-ietf-dprive-padding-policy-05#section-4.1"/>
/// <seealso href="https://tools.ietf.org/html/rfc8467#section-4.1"/>
public int BlockLength { get; set; } = 128;

/// <summary>
Expand Down Expand Up @@ -201,6 +206,10 @@ protected override void Dispose(bool disposing)
.Aggregate((current, next) => current + ", " + next);
log.Debug($"query #{request.Id} for '{names}'");
}
if (log.IsTraceEnabled)
{
log.Trace(request.ToString());
}

// Cancel the request when either the timeout is reached or the
// task is cancelled by the caller.
Expand Down Expand Up @@ -259,6 +268,12 @@ protected override void Dispose(bool disposing)

byte[] BuildRequest(Message request)
{
// Always have a query ID.
if (request.Id == 0)
{
request.Id = this.NextQueryId();
}

// Add an OPT if not already present.
var opt = request.AdditionalRecords.OfType<OPTRecord>().FirstOrDefault();
if (opt == null)
Expand All @@ -282,9 +297,12 @@ byte[] BuildRequest(Message request)
{
var paddingOption = new EdnsPaddingOption();
opt.Options.Add(paddingOption);
var need = BlockLength - (request.Length() % BlockLength);
var need = BlockLength - ((request.Length() + 2) % BlockLength);
if (need > 0)
{
paddingOption.Padding = new byte[need];
rng.NextBytes(paddingOption.Padding);
}
};

using (var tcpRequest = new MemoryStream())
Expand Down
2 changes: 1 addition & 1 deletion src/Udns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<ItemGroup>
<PackageReference Include="Common.Logging" Version="3.4.1" />
<PackageReference Include="Makaretu.Dns" Version="1.3.0" />
<PackageReference Include="Makaretu.Dns" Version="1.4.1" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion test/DotClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public async Task Query_Quad9()
}
})
{
var query = new Message { RD = true, Id = 1234 };
var query = new Message { RD = true };
query.Questions.Add(new Question { Name = "ipfs.io", Type = DnsType.TXT });
var response = await dot.QueryAsync(query);
Assert.IsNotNull(response);
Expand Down

0 comments on commit 4970552

Please sign in to comment.