How to Connect to a DNS Server using ARSoft.Tools.Net.Core DNSClient, IPAddress and port number

I have 2 DNS Servers(domainserverwithport.com:5356 and domainserverwithoutport.com) I am connecting to for a lookup request. One has a port number but the other one doesn't have.

The one without the port number connects and gets answer from the request. But the second returns nothing. And while using ARSoft.Tools.Net.Core, I could not see option to instantiate a new DNSClient with IP Address and Port Number. Please is there a way around this?

I did the following for the DNS Server that has no port number.

var ip = Dns.GetHostEntry("domainserverwithoutport.com"); Console.WriteLine(ip.AddressList[0]); var newIp = ip.AddressList[0]; IPAddress iPAddress = IPAddress.Parse(newIp.ToString()); var result = EnumDnsQuery(iPAddress.ToString(), "8.7.4.3.9.5.3.1.0.9.4.3.2.e164.arpa.4.couretech.0ceed", RecordType.Naptr, 0, 5000); 

And the EnumDnsQuery Method

public static EnumDnsResponse EnumDnsQuery(string serverIp, string domain, RecordType recordType, int maximumRetries = 0, int queryTimeout = QUERY_TIMEOUT_MILLISECONDS) { int retriesToUse = maximumRetries > 0 ? maximumRetries : 0; DnsMessage dnsMessage = null; TimeSpan duration = TimeSpan.Zero; IPAddress ipAddress = IPAddress.Parse(serverIp); DomainName domainName = DomainName.Parse(domain); var stopwatch = new Stopwatch(); DnsClient client = new DnsClient(ipAddress, queryTimeout); int attempts = 0; stopwatch.Start(); while (dnsMessage == null && attempts <= retriesToUse) { attempts++; dnsMessage = client.Resolve(domainName, recordType); } stopwatch.Stop(); string rawMessage = string.Empty; if (dnsMessage != null && recordType == RecordType.Naptr) { var naptrRecords = dnsMessage.AnswerRecords.Where(x => x.GetType() == typeof(NaptrRecord)) .Select(x => (NaptrRecord)x) .Where(x => x.Services.Equals("E2U+pstn:tel")) .OrderByDescending(x => x.Order); var naptrRecord = naptrRecords.FirstOrDefault(); if (naptrRecord != null) rawMessage = naptrRecord.RegExp.Trim('!'); } else if (dnsMessage != null && recordType == RecordType.Txt) { var txtRecords = dnsMessage.AnswerRecords.Where(x => x.GetType() == typeof(TxtRecord)) .Select(x => (TxtRecord)x); var txtRecord = txtRecords.FirstOrDefault(); if (txtRecord == null) rawMessage = txtRecord.TextData.Trim('!'); } return new EnumDnsResponse( dnsMessage: dnsMessage, rawMessage: rawMessage, totalDuration: stopwatch.Elapsed, attempts: attempts); } 

The code above returns a DnsMessage. But when I do same thing for another domain server that expects a port number, DnsMessage becomes null.Please is there a way around this?

1 Answer

Reading the code at you can see there is an internal constructor that takes a port number...

And here on line 81 we can see an example of an accessible constructor that accesses the internal one, passing in 53 as a hard coded value (i.e. you cant set the port number in your code, if you're using the provided DnsClient)

There doesn't appear to be a way to change the port number either upon instantiation of a DnsClient or afterwards (eg via a property) and you can't subclass DnsClientBase yourself because the relevant constructor you want is internal to the other assembly and not accessible. If you're desperate to do this, you might have to clone the ARSoft.Tools.Net.Core repo yourself and add a constructor to DnsClient

It's worth noting that the port setting is client-wide; there isn't the notion of having server A on port 53 and server B on port 5353, and both servers known to the same client. The port is fixed at instantiate time and applies to all the servers in the known list for that DnsClientBase

You may also get some mileage out of writing to the package author and asking for more info on why the code is arranged thus, as it seems to make it deliberately hard to use a custom port externally but internally all the necessary guts are there to make it a variable setting

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like