runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Dns.GetHostEntry("IPv4_string") does not work in Linux environment.

Open geleems opened this issue 9 years ago • 11 comments

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: IPHostEntry hostEntry = Dns.GetHostEntry("xxx.xxx.xxx.xxx"); System.Net.Internals.SocketExceptionFactory+ExtendedSocketException : No such device or address Stack Trace: at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6) at System.Net.Dns.GetHostEntry(String hostNameOrAddress)

CC: @saurabh500 @corivera @YoungGah

geleems avatar Jan 30 '17 21:01 geleems

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.

stephentoub avatar Jan 30 '17 22:01 stephentoub

you can check with nslookup if it can resolve the ipv4 address to hostname.

Priya91 avatar Jan 31 '17 00:01 Priya91

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.b__16_1(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.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 ?

saurabh500 avatar Jan 31 '17 01:01 saurabh500

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..

geleems avatar Feb 07 '17 21:02 geleems

@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>

Priya91 avatar Feb 08 '17 00:02 Priya91

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!

Priya91 avatar Feb 13 '17 22:02 Priya91

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.

toorajhelmi avatar Nov 08 '17 02:11 toorajhelmi

Agreed. This is definitely an issue. The call succeeds on Windows but fails in a Linux environment. Is there a workaround for this?

rmsutton avatar Mar 05 '18 13:03 rmsutton

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.

wfurt avatar Mar 05 '18 18:03 wfurt

this is related to dotnet/runtime#1488

ahmadalli avatar Sep 19 '18 06:09 ahmadalli

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.

wfurt avatar Sep 19 '18 06:09 wfurt