Skip to content

Commit b5166f0

Browse files
authored
Finalizing 1.3.0 API changes and cleanup (#65)
* Making name server collection in `DnsQueryAndServerOptions` readonly * Changing the way Options and Settings work slightly. Resolve name servers now happens in LookupClient's ctor instead of the options. See #33 * New property on `AutoResolveNameServers` which defaults to `True` if no name servers are passed in. Goes to `False` if there are (but can be set to true again... * IDnsQuery API's with options simplified and I went back from using optional parameters, there are 2 overloads now instead, but not for all variants.
1 parent 93c5629 commit b5166f0

35 files changed

+765
-569
lines changed

DnsClientDotNet.sln

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniDig", "samples\MiniDig\
2222
EndProject
2323
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DnsClient.Tests", "test\DnsClient.Tests\DnsClient.Tests.csproj", "{01902674-53A3-4E81-B08F-CFAA7CA62F56}"
2424
EndProject
25-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiDesign", "test\ApiDesign\ApiDesign.csproj", "{2F789CAA-31E5-4394-96B9-1B06A1AA5EA0}"
25+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiDesign", "test-other\ApiDesign\ApiDesign.csproj", "{2F789CAA-31E5-4394-96B9-1B06A1AA5EA0}"
2626
EndProject
2727
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DnsClient", "src\DnsClient\DnsClient.csproj", "{493E6161-7F71-4A59-AB28-B4A5E45A5E27}"
2828
EndProject
29-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarks", "test\Benchmarks\Benchmarks.csproj", "{7461E2C7-008E-4E61-ABC3-AF85DE37084B}"
29+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarks", "test-other\Benchmarks\Benchmarks.csproj", "{7461E2C7-008E-4E61-ABC3-AF85DE37084B}"
3030
EndProject
3131
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{5352F4A6-D88E-43E8-B0D1-E90CBB162613}"
3232
ProjectSection(SolutionItems) = preProject
@@ -37,11 +37,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentat
3737
Documentation\toc.yml = Documentation\toc.yml
3838
EndProjectSection
3939
EndProject
40-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DnsClient.TestsCommon", "test\DnsClient.TestsCommon\DnsClient.TestsCommon.csproj", "{8422A76D-CC40-4B4B-AA03-C17C3D86A073}"
40+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DnsClient.TestsCommon", "test-other\DnsClient.TestsCommon\DnsClient.TestsCommon.csproj", "{8422A76D-CC40-4B4B-AA03-C17C3D86A073}"
4141
EndProject
42-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DnsClient.PerfTestHost", "test\DnsClient.PerfTestHost\DnsClient.PerfTestHost.csproj", "{58173158-2B2D-4C26-A666-495641477066}"
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DnsClient.PerfTestHost", "test-other\DnsClient.PerfTestHost\DnsClient.PerfTestHost.csproj", "{58173158-2B2D-4C26-A666-495641477066}"
4343
EndProject
44-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiDesign.OldReference", "test\ApiDesign.OldReference\ApiDesign.OldReference.csproj", "{77554E92-95AB-404F-8999-A82BE4978361}"
44+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiDesign.OldReference", "test-other\ApiDesign.OldReference\ApiDesign.OldReference.csproj", "{77554E92-95AB-404F-8999-A82BE4978361}"
45+
EndProject
46+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test-other", "test-other", "{28CFF2BF-88D0-481F-829E-43B51DE7FB1F}"
4547
EndProject
4648
Global
4749
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -88,12 +90,12 @@ Global
8890
GlobalSection(NestedProjects) = preSolution
8991
{CEC9D7AD-8000-4FC6-8EB3-AC1417B9808A} = {A1BE93CF-0106-495B-9720-008046657391}
9092
{01902674-53A3-4E81-B08F-CFAA7CA62F56} = {34E43CED-3DF4-49CE-8556-69225ABD84E6}
91-
{2F789CAA-31E5-4394-96B9-1B06A1AA5EA0} = {34E43CED-3DF4-49CE-8556-69225ABD84E6}
93+
{2F789CAA-31E5-4394-96B9-1B06A1AA5EA0} = {28CFF2BF-88D0-481F-829E-43B51DE7FB1F}
9294
{493E6161-7F71-4A59-AB28-B4A5E45A5E27} = {3D3BE1AD-F551-420A-A81D-4CB88286C8CD}
93-
{7461E2C7-008E-4E61-ABC3-AF85DE37084B} = {34E43CED-3DF4-49CE-8556-69225ABD84E6}
94-
{8422A76D-CC40-4B4B-AA03-C17C3D86A073} = {34E43CED-3DF4-49CE-8556-69225ABD84E6}
95-
{58173158-2B2D-4C26-A666-495641477066} = {34E43CED-3DF4-49CE-8556-69225ABD84E6}
96-
{77554E92-95AB-404F-8999-A82BE4978361} = {34E43CED-3DF4-49CE-8556-69225ABD84E6}
95+
{7461E2C7-008E-4E61-ABC3-AF85DE37084B} = {28CFF2BF-88D0-481F-829E-43B51DE7FB1F}
96+
{8422A76D-CC40-4B4B-AA03-C17C3D86A073} = {28CFF2BF-88D0-481F-829E-43B51DE7FB1F}
97+
{58173158-2B2D-4C26-A666-495641477066} = {28CFF2BF-88D0-481F-829E-43B51DE7FB1F}
98+
{77554E92-95AB-404F-8999-A82BE4978361} = {28CFF2BF-88D0-481F-829E-43B51DE7FB1F}
9799
EndGlobalSection
98100
GlobalSection(ExtensibilityGlobals) = postSolution
99101
SolutionGuid = {E26DC1C5-A82B-4984-88DB-107C9D66271F}

samples/MiniDig/DnsCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ public NameServer[] GetEndpointsValue()
8787

8888
public LookupClientOptions GetLookupSettings()
8989
{
90-
return new LookupClientOptions()
90+
return new LookupClientOptions(GetEndpointsValue())
9191
{
92-
NameServers = GetEndpointsValue(),
9392
Recursion = GetUseRecursionValue(),
9493
Retries = GetTriesValue(),
9594
Timeout = TimeSpan.FromMilliseconds(GetTimeoutValue()),

samples/MiniDig/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Program
99
{
1010
public static async Task Main(string[] args)
1111
{
12-
DnsClient.Tracing.Source.Switch.Level = SourceLevels.Verbose;
12+
DnsClient.Tracing.Source.Switch.Level = SourceLevels.Information;
1313
DnsClient.Tracing.Source.Listeners.Add(new ConsoleTraceListener());
1414

1515
var app = new CommandLineApplication(throwOnUnexpectedArg: true);

src/DnsClient/DnsQueryOptions.cs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public class DnsQueryOptions
129129
/// Default is <c>True</c>.
130130
/// </summary>
131131
/// <remarks>
132-
/// The query is answered if there is at least one <see cref="DnsResourceRecord"/> in the answers section
132+
/// The query is answered if there is at least one <see cref="DnsResourceRecord"/> in the answers section
133133
/// matching the <see cref="DnsQuestion"/>'s <see cref="QueryType"/>.
134134
/// <para>
135-
/// If there are zero answeres in the response, the query is not answered, independent of the <see cref="QueryType"/>.
135+
/// If there are zero answers in the response, the query is not answered, independent of the <see cref="QueryType"/>.
136136
/// If there are answers in the response, the <see cref="QueryType"/> is used to find a matching record,
137137
/// query types <see cref="QueryType.ANY"/> and <see cref="QueryType.AXFR"/> will be ignored by this check.
138138
/// </para>
@@ -241,39 +241,38 @@ public static implicit operator DnsQuerySettings(DnsQueryOptions fromOptions)
241241
/// </summary>
242242
public class DnsQueryAndServerOptions : DnsQueryOptions
243243
{
244-
// TODO: evalualte if defaulting resolveNameServers to false here and in the query plan, fall back to configured nameservers of the client
245244
/// <summary>
246245
/// Creates a new instance of <see cref="DnsQueryAndServerOptions"/> without name servers.
247-
/// </summary>
248-
/// <remarks>
249246
/// If no nameservers are configured, a query will fallback to the nameservers already configured on the <see cref="LookupClient"/> instance.
250-
/// </remarks>
251-
/// <param name="resolveNameServers">If set to <c>true</c>, <see cref="NameServer.ResolveNameServers(bool, bool)"/>
252-
/// will be used to get a list of nameservers.</param>
253-
public DnsQueryAndServerOptions(bool resolveNameServers = false)
254-
: this(resolveNameServers ? NameServer.ResolveNameServers()?.ToArray() : null)
247+
/// </summary>
248+
public DnsQueryAndServerOptions()
255249
{
256-
AutoResolvedNameServers = resolveNameServers;
257250
}
258251

259252
/// <summary>
260253
/// Creates a new instance of <see cref="DnsQueryAndServerOptions"/>.
261254
/// </summary>
262255
/// <param name="nameServers">A collection of name servers.</param>
256+
/// <exception cref="ArgumentNullException">If <paramref name="nameServers"/> is null.</exception>
263257
public DnsQueryAndServerOptions(params NameServer[] nameServers)
264258
: base()
265259
{
266260
if (nameServers != null && nameServers.Length > 0)
267261
{
268262
NameServers = nameServers.ToList();
269263
}
264+
else
265+
{
266+
throw new ArgumentNullException(nameof(nameServers));
267+
}
270268
}
271269

272270
// TODO: remove overloads in favor of implicit conversion to NameServer?
273271
/// <summary>
274272
/// Creates a new instance of <see cref="DnsQueryAndServerOptions"/>.
275273
/// </summary>
276274
/// <param name="nameServers">A collection of name servers.</param>
275+
/// <exception cref="ArgumentNullException">If <paramref name="nameServers"/> is null.</exception>
277276
public DnsQueryAndServerOptions(params IPEndPoint[] nameServers)
278277
: this(nameServers?.Select(p => (NameServer)p).ToArray())
279278
{
@@ -283,20 +282,16 @@ public DnsQueryAndServerOptions(params IPEndPoint[] nameServers)
283282
/// Creates a new instance of <see cref="DnsQueryAndServerOptions"/>.
284283
/// </summary>
285284
/// <param name="nameServers">A collection of name servers.</param>
285+
/// <exception cref="ArgumentNullException">If <paramref name="nameServers"/> is null.</exception>
286286
public DnsQueryAndServerOptions(params IPAddress[] nameServers)
287287
: this(nameServers?.Select(p => (NameServer)p).ToArray())
288288
{
289289
}
290290

291291
/// <summary>
292-
/// Gets a flag indicating whether the name server collection was manually defined or automatically resolved
293-
/// </summary>
294-
public bool AutoResolvedNameServers { get; }
295-
296-
/// <summary>
297-
/// Gets or sets a list of name servers which should be used to query.
292+
/// Gets a list of name servers which should be used to query.
298293
/// </summary>
299-
public IList<NameServer> NameServers { get; set; } = new List<NameServer>();
294+
public IReadOnlyList<NameServer> NameServers { get; } = new NameServer[0];
300295

301296
/// <summary>
302297
/// Converts the query options into readonly settings.
@@ -327,40 +322,57 @@ public class LookupClientOptions : DnsQueryAndServerOptions
327322
private TimeSpan? _maximumCacheTimeout;
328323

329324
/// <summary>
330-
/// Creates a new instance of <see cref="LookupClientOptions"/> without name servers.
325+
/// Creates a new instance of <see cref="LookupClientOptions"/> with default settings.
331326
/// </summary>
332-
public LookupClientOptions(bool resolveNameServers = true)
333-
: base(resolveNameServers)
327+
public LookupClientOptions()
328+
: base()
334329
{
335330
}
336331

337332
/// <summary>
338333
/// Creates a new instance of <see cref="LookupClientOptions"/>.
339334
/// </summary>
340335
/// <param name="nameServers">A collection of name servers.</param>
336+
/// <exception cref="ArgumentNullException">If <paramref name="nameServers"/> is null.</exception>
341337
public LookupClientOptions(params NameServer[] nameServers)
342338
: base(nameServers)
343339
{
340+
AutoResolveNameServers = false;
344341
}
345342

346343
/// <summary>
347344
/// Creates a new instance of <see cref="LookupClientOptions"/>.
348345
/// </summary>
349346
/// <param name="nameServers">A collection of name servers.</param>
347+
/// <exception cref="ArgumentNullException">If <paramref name="nameServers"/> is null.</exception>
350348
public LookupClientOptions(params IPEndPoint[] nameServers)
351349
: base(nameServers)
352350
{
351+
AutoResolveNameServers = false;
353352
}
354353

355354
/// <summary>
356355
/// Creates a new instance of <see cref="LookupClientOptions"/>.
357356
/// </summary>
358357
/// <param name="nameServers">A collection of name servers.</param>
358+
/// <exception cref="ArgumentNullException">If <paramref name="nameServers"/> is null.</exception>
359359
public LookupClientOptions(params IPAddress[] nameServers)
360360
: base(nameServers)
361361
{
362+
AutoResolveNameServers = false;
362363
}
363364

365+
/// <summary>
366+
/// Gets or sets a flag indicating whether the name server collection should be automatically resolved.
367+
/// Default is <c>True</c>.
368+
/// </summary>
369+
/// <remarks>
370+
/// If name servers are configured manually via the constructor, this flag is set to false.
371+
/// If you want both, your manually configured servers and auto resolved name servers,
372+
/// you can use both (ctor or) <see cref="DnsQueryAndServerOptions.NameServers"/> and <see cref="AutoResolveNameServers"/> set to <c>True</c>.
373+
/// </remarks>
374+
public bool AutoResolveNameServers { get; set; } = true;
375+
364376
/// <summary>
365377
/// Gets or sets a <see cref="TimeSpan"/> which can override the TTL of a resource record in case the
366378
/// TTL of the record is lower than this minimum value.
@@ -427,20 +439,6 @@ public TimeSpan? MaximumCacheTimeout
427439
}
428440
}
429441
}
430-
431-
/// <summary>
432-
/// Converts the options into readonly settings.
433-
/// </summary>
434-
/// <param name="fromOptions">The options.</param>
435-
public static implicit operator LookupClientSettings(LookupClientOptions fromOptions)
436-
{
437-
if (fromOptions == null)
438-
{
439-
return null;
440-
}
441-
442-
return new LookupClientSettings(fromOptions);
443-
}
444442
}
445443

446444
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode() - intentionally, Equals is only used by unit tests
@@ -551,10 +549,10 @@ public class DnsQuerySettings : IEquatable<DnsQuerySettings>
551549
/// Default is <c>True</c>.
552550
/// </summary>
553551
/// <remarks>
554-
/// The query is answered if there is at least one <see cref="DnsResourceRecord"/> in the answers section
552+
/// The query is answered if there is at least one <see cref="DnsResourceRecord"/> in the answers section
555553
/// matching the <see cref="DnsQuestion"/>'s <see cref="QueryType"/>.
556554
/// <para>
557-
/// If there are zero answeres in the response, the query is not answered, independent of the <see cref="QueryType"/>.
555+
/// If there are zero answers in the response, the query is not answered, independent of the <see cref="QueryType"/>.
558556
/// If there are answers in the response, the <see cref="QueryType"/> is used to find a matching record,
559557
/// query types <see cref="QueryType.ANY"/> and <see cref="QueryType.AXFR"/> will be ignored by this check.
560558
/// </para>
@@ -638,7 +636,7 @@ public DnsQuerySettings(DnsQueryOptions options)
638636
UseTcpFallback = options.UseTcpFallback;
639637
UseTcpOnly = options.UseTcpOnly;
640638
ExtendedDnsBufferSize = options.ExtendedDnsBufferSize;
641-
RequestDnsSecRecords = options.RequestDnsSecRecords;
639+
RequestDnsSecRecords = options.RequestDnsSecRecords;
642640
}
643641

644642
/// <inheritdocs />
@@ -699,10 +697,6 @@ public class DnsQueryAndServerSettings : DnsQuerySettings, IEquatable<DnsQueryAn
699697
/// </summary>
700698
public IReadOnlyList<NameServer> NameServers => _endpoints;
701699

702-
/// <summary>
703-
/// Gets a flag indicating whether the name server collection was manually defined or automatically resolved
704-
/// </summary>
705-
public bool AutoResolvedNameServers { get; }
706700

707701
/// <summary>
708702
/// Creates a new instance of <see cref="DnsQueryAndServerSettings"/>.
@@ -711,8 +705,6 @@ public DnsQueryAndServerSettings(DnsQueryAndServerOptions options)
711705
: base(options)
712706
{
713707
_endpoints = options.NameServers?.ToArray() ?? new NameServer[0];
714-
715-
AutoResolvedNameServers = options.AutoResolvedNameServers;
716708
}
717709

718710
/// <summary>
@@ -754,7 +746,6 @@ public bool Equals(DnsQueryAndServerSettings other)
754746
}
755747

756748
return NameServers.SequenceEqual(other.NameServers)
757-
&& AutoResolvedNameServers == other.AutoResolvedNameServers
758749
&& base.Equals(other);
759750
}
760751

@@ -794,6 +785,13 @@ public LookupClientSettings(LookupClientOptions options)
794785
MaximumCacheTimeout = options.MaximumCacheTimeout;
795786
}
796787

788+
internal LookupClientSettings(LookupClientOptions options, IReadOnlyCollection<NameServer> overrideServers)
789+
: base(options, overrideServers)
790+
{
791+
MinimumCacheTimeout = options.MinimumCacheTimeout;
792+
MaximumCacheTimeout = options.MaximumCacheTimeout;
793+
}
794+
797795
/// <summary>
798796
/// Gets a <see cref="TimeSpan"/> which can override the TTL of a resource record in case the
799797
/// TTL of the record is lower than this minimum value.
@@ -870,7 +868,7 @@ internal LookupClientSettings Copy(
870868
bool? useTcpOnly = null)
871869
{
872870
// auto resolved flag might get lost here. But this stuff gets deleted anyways.
873-
return new LookupClientOptions(nameServers?.ToArray())
871+
return new LookupClientSettings(new LookupClientOptions(nameServers?.ToArray())
874872
{
875873
MinimumCacheTimeout = minimumCacheTimeout,
876874
ContinueOnDnsError = continueOnDnsError ?? ContinueOnDnsError,
@@ -887,7 +885,7 @@ internal LookupClientSettings Copy(
887885
ExtendedDnsBufferSize = ExtendedDnsBufferSize,
888886
RequestDnsSecRecords = RequestDnsSecRecords,
889887
ContinueOnEmptyResponse = ContinueOnEmptyResponse
890-
};
888+
});
891889
}
892890

893891
// TODO: remove if LookupClient settings can be made readonly

0 commit comments

Comments
 (0)