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
781 stars 137 forks source link

GetHostNameAsync doesn't work on iOS #143

Closed Immons closed 2 years ago

Immons commented 2 years ago

Trying to use this library, as standard .NET DNS implementation doesn't return nice host name. On iOS Simulator, below code returns fine:

var client = new LookupClient();
return await client.GetHostNameAsync(ip);

on device it throws error:

2022-01-20 14:45:45.395.iOS[1299:342331] System.ArgumentOutOfRangeException: List of configured name servers must not be empty.
Parameter name: servers
  at DnsClient.LookupClient.QueryInternalAsync (DnsClient.DnsQuestion question, DnsClient.DnsQuerySettings queryOptions, System.Collections.Generic.IReadOnlyCollection`1[T] servers, System.Threading.CancellationToken cancellationToken) [0x00061] in <afd7bd4618b64f01ae1ddbabc84fd0e8>:0 
  at DnsClient.DnsQueryExtensions.GetHostNameAsync (DnsClient.IDnsQuery query, System.Net.IPAddress address) [0x0009f] in <afd7bd4618b64f01ae1ddbabc84fd0e8>:0 
  at <project>.DevicesDiscovery.Scanners.DnsHostNameResolver.GetHostNameWithDnsClientNet (System.Net.IPAddress ip) [0x0003a] in /Users/immons/Projects/<project>.DevicesDiscovery/Scanners/DnsScanner.cs:62
MichaCo commented 2 years ago

The error might be slightly misleading. But this just means that the DNS server auto discovery didn't work. Which is expected on iOS.

To get the DNS server(s) on mobile, you need to call some different APIs, and until .NET Core supports some way to get that information for you, you have to do that step yourself and initialize the client with the server(s) you found.

That being said, I'm not a mobile developer and especially not on iOS, so if there is some way to get DNS server information I'm happy to look at a PR