Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2c217a3
First integration of Resolver sources
rzikm Oct 4, 2024
704e33c
Allow mocking
rzikm Oct 4, 2024
aff6d17
Fix dispose
rzikm Oct 4, 2024
15db8a1
Fix tests
rzikm Oct 4, 2024
6bda235
Actually run ResolvConf tests
rzikm Oct 4, 2024
6fc61e0
Add retry functionality
rzikm Oct 8, 2024
2b6d91e
Add logging
rzikm Oct 10, 2024
2137954
Fix Microsoft.Extensions.ServiceDiscovery.Yarp.Tests
rzikm Oct 10, 2024
c204b01
Merge remote-tracking branch 'upstream/main' into managed-resolver
rzikm Nov 26, 2024
2fc7de4
Activity WIP
rzikm Nov 26, 2024
9b4de29
Fix reading CNAME with pointers in domain name segments
rzikm Nov 28, 2024
3ad5a75
Reenable telemetry
rzikm Nov 28, 2024
40f8f9b
Last changes to telemetry
rzikm Nov 28, 2024
e385605
Use strong RNG for Transaction ID generation
rzikm Jan 15, 2025
3cf0a9d
Check against too long domain name
rzikm Feb 3, 2025
6016e41
Disallow pointer to pointer.
rzikm Feb 3, 2025
f8c189d
Code review feedback (easy fixes)
rzikm Feb 3, 2025
d929889
More code review feedback
rzikm Feb 6, 2025
00e41d7
Fix increment
rzikm Feb 6, 2025
7267fb7
Detect CNAME loops
rzikm Feb 7, 2025
c1b76d3
Handle empty Tcp fallback responses and lack of TCP failover support …
rzikm Feb 7, 2025
86490cc
Dispose result.Response when overwritten by another result.
rzikm Feb 7, 2025
92b2905
Move DnsMessageHeader parsing to DnsPrimitives
rzikm Feb 11, 2025
fdf1904
Rework and add tests to retries and failover
rzikm Feb 12, 2025
61814e7
Test failover after exhausting attempts on one server
rzikm Feb 13, 2025
82a8108
Streamline options, remove unsupported options from ResolverOptions
rzikm Feb 13, 2025
fa627fd
Better handling of malformed responses
rzikm Feb 13, 2025
4ec79ca
Code review feedback
rzikm Mar 25, 2025
123375d
More feedback
rzikm Mar 27, 2025
6311616
Guarantee linear parsing of CNAME chains
rzikm Mar 27, 2025
c57a8c5
Fix decoding compressed CNAME in TCP fallback
rzikm Mar 27, 2025
83ca958
More code coverage
rzikm May 12, 2025
03e6a99
Correctly handle IDN names
rzikm May 15, 2025
46418fe
Minor fixes
rzikm May 22, 2025
4075e4f
Add Fuzzing tests for DNS resolver
rzikm May 23, 2025
56cd169
Improve fuzzing of EncodedDomainName
rzikm May 23, 2025
a19e8a2
Merge remote-tracking branch 'upstream/main' into managed-resolver
rzikm Jun 10, 2025
5d5edae
Remove commented out code
rzikm Jun 10, 2025
3c387e9
Fix build
rzikm Jun 10, 2025
c6e3d63
Fix Yarp service discovery tests
rzikm Jun 10, 2025
e66229a
Lazy TCP socket allocation in LoopbackTests
rzikm Jun 12, 2025
da018f7
LoopbackTestBaseLogging
rzikm Jun 12, 2025
2697412
fixup! LoopbackTestBaseLogging
rzikm Jun 12, 2025
214cc9b
Downgrade SharpFuzz
rzikm Jun 17, 2025
0d891cb
Fix buffer use after returning to pool
rzikm Jun 18, 2025
46cf262
Merge branch 'main' into managed-resolver
rzikm Jun 18, 2025
35a5809
Fix build
rzikm Jun 18, 2025
fcf18cf
Merge branch 'main' into managed-resolver
rzikm Jun 25, 2025
df785d1
Remove DnsClient package ref
rzikm Jun 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More code review feedback
  • Loading branch information
rzikm committed Feb 6, 2025
commit d9298890144b896ab541cdf72a853270d068b74d
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

Expand All @@ -23,7 +24,9 @@ public DnsDataReader(ReadOnlyMemory<byte> buffer, byte[]? returnToPool = null)

public bool TryReadHeader(out DnsMessageHeader header)
{
if (_buffer.Length - _position < DnsMessageHeader.HeaderLength)
Debug.Assert(_position == 0);

if (_buffer.Length < DnsMessageHeader.HeaderLength)
{
header = default;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace Microsoft.Extensions.ServiceDiscovery.Dns.Resolver;

// RFC 1035 4.1.1. Header section format
[StructLayout(LayoutKind.Explicit, Size = HeaderLength)]
internal struct DnsMessageHeader
{
internal const int HeaderLength = 12;

[FieldOffset(0)]
private ushort _transactionId;
[FieldOffset(2)]
private ushort _flags;

[FieldOffset(4)]
private ushort _queryCount;
[FieldOffset(6)]
private ushort _answerCount;
[FieldOffset(8)]
private ushort _authorityCount;
[FieldOffset(10)]
private ushort _additionalRecordCount;

internal ushort QueryCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;

Expand All @@ -25,7 +26,11 @@ internal static bool TryWriteQName(Span<byte> destination, string name, out int
// padding is used.
//

if (!Encoding.ASCII.TryGetBytes(name, destination.IsEmpty ? destination : destination.Slice(1), out int length) || destination.Length < length + 2)
// The is assumed to be already validated and puny-encoded if needed
Debug.Assert(name.Length <= MaxDomainNameLength);
Debug.Assert(Ascii.IsValid(name));

if (destination.IsEmpty || !Encoding.ASCII.TryGetBytes(name, destination.Slice(1), out int length) || destination.Length < length + 2)
{
// buffer too small
written = 0;
Expand All @@ -38,13 +43,14 @@ internal static bool TryWriteQName(Span<byte> destination, string name, out int
while (true)
{
// figure out the next label and prepend the length
int index = nameBuffer.Slice(1).IndexOf<byte>((byte)'.');
int index = nameBuffer.Slice(1).IndexOf((byte)'.');
int labelLen = index == -1 ? nameBuffer.Length - 1 : index;

// https://www.rfc-editor.org/rfc/rfc1035#section-2.3.4
// labels 63 octets or less
if (labelLen > 63)
{
// this should never happen, as we validate the name before calling this method
throw new ArgumentException("Label is too long");
}

Expand Down Expand Up @@ -197,41 +203,6 @@ internal static bool TryReadService(ReadOnlySpan<byte> buffer, out ushort priori
return true;
}

internal static bool TryWriteService(Span<byte> buffer, ushort priority, ushort weight, ushort port, string target, out int bytesWritten)
{
// https://www.rfc-editor.org/rfc/rfc2782
if (!BinaryPrimitives.TryWriteUInt16BigEndian(buffer, priority) ||
!BinaryPrimitives.TryWriteUInt16BigEndian(buffer.Slice(2), weight) ||
!BinaryPrimitives.TryWriteUInt16BigEndian(buffer.Slice(4), port) ||
!TryWriteQName(buffer.Slice(6), target, out bytesWritten))
{
bytesWritten = 0;
return false;
}

bytesWritten += 6;
return true;
}

internal static bool TryWriteSoa(Span<byte> buffer, string primaryNameServer, string responsibleMailAddress, uint serial, uint refresh, uint retry, uint expire, uint minimum, out int bytesWritten)
{
// https://www.rfc-editor.org/rfc/rfc1035#section-3.3.13
if (!TryWriteQName(buffer, primaryNameServer, out int w1) ||
!TryWriteQName(buffer.Slice(w1), responsibleMailAddress, out int w2) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buffer.Slice(w1 + w2), serial) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buffer.Slice(w1 + w2 + 4), refresh) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buffer.Slice(w1 + w2 + 8), retry) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buffer.Slice(w1 + w2 + 12), expire) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buffer.Slice(w1 + w2 + 16), minimum))
{
bytesWritten = 0;
return false;
}

bytesWritten = w1 + w2 + 20;
return true;
}

internal static bool TryReadSoa(ReadOnlySpan<byte> buffer, [NotNullWhen(true)] out string? primaryNameServer, [NotNullWhen(true)] out string? responsibleMailAddress, out uint serial, out uint refresh, out uint retry, out uint expire, out uint minimum, out int bytesRead)
{
// https://www.rfc-editor.org/rfc/rfc1035#section-3.3.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ internal static SendQueryError ValidateResponse(in DnsResponse response)
throw new InvalidOperationException("Invalid response: Header mismatch");
}

// transfer ownership of buffer to the caller
buffer = null!;
return (responseReader, header);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private static async Task<int> ProcessRequestCore(ReadOnlyMemory<byte> message,
}))
{
throw new InvalidOperationException("Failed to write header");
};
}
;

foreach (var (questionName, questionType, questionClass) in responseBuilder.Questions)
{
Expand Down Expand Up @@ -198,23 +199,40 @@ public static List<DnsResourceRecord> AddCname(this List<DnsResourceRecord> reco
public static List<DnsResourceRecord> AddService(this List<DnsResourceRecord> records, string name, int ttl, ushort priority, ushort weight, ushort port, string target)
{
byte[] buff = new byte[256];
if (!DnsPrimitives.TryWriteService(buff, priority, weight, port, target, out int length))

// https://www.rfc-editor.org/rfc/rfc2782
if (!BinaryPrimitives.TryWriteUInt16BigEndian(buff, priority) ||
!BinaryPrimitives.TryWriteUInt16BigEndian(buff.AsSpan(2), weight) ||
!BinaryPrimitives.TryWriteUInt16BigEndian(buff.AsSpan(4), port) ||
!DnsPrimitives.TryWriteQName(buff.AsSpan(6), target, out int length))
{
throw new InvalidOperationException("Failed to encode SRV record");
}

length += 6;

records.Add(new DnsResourceRecord(name, QueryType.SRV, QueryClass.Internet, ttl, buff.AsMemory(0, length)));
return records;
}

public static List<DnsResourceRecord> AddStartOfAuthority(this List<DnsResourceRecord> records, string name, int ttl, string mname, string rname, uint serial, uint refresh, uint retry, uint expire, uint minimum)
{
byte[] buff = new byte[256];
if (!DnsPrimitives.TryWriteSoa(buff, mname, rname, serial, refresh, retry, expire, minimum, out int length))

// https://www.rfc-editor.org/rfc/rfc1035#section-3.3.13
if (!DnsPrimitives.TryWriteQName(buff, mname, out int w1) ||
!DnsPrimitives.TryWriteQName(buff.AsSpan(w1), rname, out int w2) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buff.AsSpan(w1 + w2), serial) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buff.AsSpan(w1 + w2 + 4), refresh) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buff.AsSpan(w1 + w2 + 8), retry) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buff.AsSpan(w1 + w2 + 12), expire) ||
!BinaryPrimitives.TryWriteUInt32BigEndian(buff.AsSpan(w1 + w2 + 16), minimum))
{
throw new InvalidOperationException("Failed to encode SOA record");
}

int length = w1 + w2 + 20;

records.Add(new DnsResourceRecord(name, QueryType.SOA, QueryClass.Internet, ttl, buff.AsMemory(0, length)));
return records;
}
Expand Down