Dns.GetHostEntry("IPv4_string") does not work in Linux environment.
I am working on developing integrated authentication feature in Linux. The feature utilizes API System.Net.Dns.GetHostEntry(string hostNameOrAddress) for domain name reverse look up while generating server SPN. This method works great in Windows, but does not work in Linux when I feed IPv4 address for parameter. IPv6 parameter works though in Linux.
Here is a sample code and its stack trace:
CC: @saurabh500 @corivera @YoungGah
Are you sure IPv4 in general isn't working? Sounds like the Linux box simply isn't able to resolve that specific address? My Windows machine for example produces a similar error as it can't resolve it.
you can check with nslookup
If I use the IPv4 address of the target host in the Dns.GetHostEntry(), the resolution fails with the error message. The address is for a Windows and the code is executing on macOS/Linux.
Unhandled Exception: System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
at System.Net.Dns.<>c.1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at ConsoleApplication.Program.<GetHostEntry>d__1.MoveNext() in /home/REDMOND/sausing/reproapp/Program.cs:line 16
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)
at ConsoleApplication.Program.Main(String[] args) in
If the IPv6 address of the target host is used, then the reverse lookup succeeds.
If I ping the target host from linux, the IPv4 address is resolved and is being used to do a reverse lookup Dns.GetHostEntry.
In this case the target host is a Windows Server 2012 R2 and the reverse lookup is happening from macOS and Ubuntu16.04. The nslookup from Unix to Windows host fails as well.
However while trying Windows to Windows scenario, there was successful Dns.GetHostEntry execution even though nslookup failed.
Is the API working differently on Windows vs Linux and macOS ?
Now IPHostEntry hostEntry = Dns.GetHostEntry("IPv6_address"); does not work anymore in Linux.
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException : No such device or address
Stack Trace:
/home/REDMOND/gelee/Projects/CoreFx/src/System.Net.NameResolution/src/System/Net/DNS.cs(178,0): at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
/home/REDMOND/gelee/Projects/CoreFx/src/System.Net.NameResolution/src/System/Net/DNS.cs(470,0): at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
Let me open another issue for this..
@saurabh500
Is the API working differently on Windows vs Linux and macOS ?
The implementation is different on the OSes as they call into the platform specific OS APIs. However for this functionality to work, your system has to be configured to setup reverse dns lookup to ping the correct dns server. Can you verify if running this from Mac OS terminal resolves the hostname,
host <ip-address>
This issue is highly likely that the host machine doesn't have the network setting configured for reverse dns lookup.
Closing the issue due to inactivity, @saurabh500 or @geleems please reopen if you believe this is a .net issue, based on the information provided in previous comment. Thanks!
This is definitely a .net core issue. I can make a connection to a URL using HttpClient and setting the base address to the URL. But if I use a TcpClient and use the same URL I get this error. Looks like the implementation of Dns.GetHostEntry in Mac is different than HttpClient and not working.
Agreed. This is definitely an issue. The call succeeds on Windows but fails in a Linux environment. Is there a workaround for this?
I tried simple program like this:
using System;
using System.Net;
namespace ipv6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
IPHostEntry hostEntry = System.Net.Dns.GetHostEntry("11.10.10.10");
Console.WriteLine("hostEntry={0}", hostEntry);
}
}
}
and it also fails on Windows 10. When I use "127.0.0.1" it works on Linux & Windows. Also note that gethostbyname("11.10.10.10") succeeds on my Linux system.
Unhandled Exception: System.Net.Sockets.SocketException: No such host is known at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6) at System.Net.Dns.GetHostEntry(String hostNameOrAddress) at ipv6.Program.Main(String[] args) in C:\Users\build\ipv6\Program.cs:line 11
so the behavior seems consistent and observed differences can be in network configuration.
However many applications (at least on Linux) would take host or IP and they would function function without IP having reverse entry. It seems reasonable that if valid IP address string is passed in and DNS lookup fails we return IPHostEntry crafted from that address. My guess is that they are after AddressList.
Maybe there is some other API you can recommend @stephentoub to make mix of names and addresses easier for app writers. Matching behavior of gethostbyname() may be good improvement.
this is related to dotnet/runtime#1488
system resolver is particularly flaky on OSX. I put in some traces and resolving same name over and over fails frequently on 10.13. (OS getaddrinfo() fails)
However that is different than claim that string representation of IPv4 address cannot resolve if system is configured properly.