MichaCo / DnsClient.NET

DnsClient.NET is a simple yet very powerful and high performant open source library for the .NET Framework to do DNS lookups
https://dnsclient.michaco.net
Apache License 2.0
762 stars 136 forks source link

LookupClientOptions name servers should not be read-only #160

Closed dchauran closed 2 years ago

dchauran commented 2 years ago

I am trying to use the DnsClient to validate DNS entries agianst different sets of name servers or the client's default name servers.

In all of these cases, the LookupClientOptions are the same, but the servers are not. I have a common function to create the client with the correct options, but since the name servers in the LookupClientOptions are read-only, this requires a rather ugly workaround:

            if (nameServerIps.Count > 0)
            {
                dnsClientOptions = new LookupClientOptions(nameServerIps.ToArray());
            }
            else
            {
                dnsClientOptions = new LookupClientOptions();
            }

            dnsClientOptions.UseTcpFallback = false;
            dnsClientOptions.UseCache = false;
            dnsClientOptions.EnableAuditTrail = true;
            dnsClientOptions.Timeout = new TimeSpan(Settings.QueryTimeoutMs * TimeSpan.TicksPerMillisecond);
            dnsClientOptions.Recursion = !authoritative;
            dnsClientOptions.AutoResolveNameServers = nameServerIps.Count > 0 ? false : true;
            dnsClientOptions.ContinueOnDnsError = false;
            dnsClientOptions.ThrowDnsErrors = false;

as opposed to a more elegant:

            dnsClientOptions = new LookupClientOptions()
            {
                NameServers = nameServerIps.ToArray(),
                UseTcpFallback = false,
                UseCache = false,
                EnableAuditTrail = true,
                Timeout = new TimeSpan(Settings.QueryTimeoutMs * TimeSpan.TicksPerMillisecond),
                Recursion = !authoritative,
                AutoResolveNameServers = nameServerIps.Count > 0 ? false : true,
                ContinueOnDnsError = false,
                ThrowDnsErrors = false,
            };
MichaCo commented 2 years ago

That's by design and those properties are immutable for good reason.

Your use case is a different one, you want to call multiple different servers? Then just use the .QueryServer overloads which have options to specify a different server (and options) each call.