Dns.GetHostEntry not working the same as Dns.Resolve or Dns.GetHos

Q

QSIDeveloper

I am trying to get the DNS name of an arbitrary IP address on the network.

If I use GetHostEntry as the documentation suggests I only get the name of
the machine I am running the code on. All other IP’s returns the IP in the
HostName property of the IPHostEntry object returned by GetHostEntry.

If I use Dns.GetHostByAddress or Dns.Resolve I get the name, but these are
marked as obsolete. What is the correct way to get this information in .Net
2.0?
 
J

Jialiang Ge [MSFT]

Hello QSIDeveloper,

As far as I know, Dns.GetHostEntry returns IP address as the HostName when
no DNS servers respond to the reverse DNS query. For example, when all DNS
servers are unreachable. Accompanied with this, the call of
GetHostByAddress will throw a SocketException with message "The requested
name is valid, but no data of the requested type was found". However,
Dns.GetHostByAddress seems working fine on your side. Please give me some
time. I will look for other reasons for the abnormal behavior of
GetHostEntry.

For your second question "What is the correct way to get this information
in .Net 2.0":
System.Net.Dns.GetHostByAddress is for .Net 1.1 or prior. From .NET 2.0, we
use Dns.GetHostEntry.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Q

QSIDeveloper

Thank you for you research.

The IP is xxx.xxx.xxx.xxx, I believe that is IPv4

1.) What I think you are saying is
System.Net.Dns.GetHostEntry("10.10.60.221"); should work. Is that correct?

When I make that call I always get back the IP address in
IPHostEntry.HostName except when I put the IP address of my PC, in that case
I get the machine name of my PC.

2.) Do you have any Ideas why this would behave this way?

3.) If System.Net.Dns.Resolve and System.Net.Dns.GetHostByAddress are
obsolete when will they stop working in the 2.0 framework?

I will look into you suggestion of debugging into framework code but could
you respond to the question above in the meantime?


Jialiang Ge said:
Hello QSIDeveloper,

Sorry for my delayed response. I was trying to reproduce the symptom in my
lab and look into the source code of Dns. Code list 1 is the source of the
obsolete function "GetHostByAddress", and Code list 2 is for GetHostEntry:

Code list 1
[Obsolete("GetHostByAddress is obsoleted for this type, please use
GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static IPHostEntry GetHostByAddress(string address) {
if(Logging.On)Logging.Enter(Logging.Sockets, "DNS",
"GetHostByAddress", address);
//
// demand Unrestricted DnsPermission for this call
//
s_DnsPermission.Demand();

if (address == null) {
throw new ArgumentNullException("address");
}

GlobalLog.Print("Dns.GetHostByAddress: " + address);

IPHostEntry ipHostEntry =
InternalGetHostByAddress(IPAddress.Parse(address), false, true);
if(Logging.On)Logging.Exit(Logging.Sockets, "DNS",
"GetHostByAddress", ipHostEntry);
return ipHostEntry;
} // GetHostByAddress

Code list 2
public static IPHostEntry GetHostEntry(string hostNameOrAddress)
{
if(Logging.On)Logging.Enter(Logging.Sockets, "DNS",
"GetHostEntry", hostNameOrAddress);
//
// demand Unrestricted DnsPermission for this call
//
s_DnsPermission.Demand();

if (hostNameOrAddress == null) {
throw new ArgumentNullException("hostNameOrAddress");
}

// See if it's an IP Address.
IPAddress address;
IPHostEntry ipHostEntry;
if (TryParseAsIP(hostNameOrAddress, out address))
{
if (address.Equals(IPAddress.Any) ||
address.Equals(IPAddress.IPv6Any))
{
throw new
ArgumentException(SR.GetString(SR.net_invalid_ip_addr),
"hostNameOrAddress");
}

ipHostEntry = InternalGetHostByAddress(address, true,
false);
}
else
{
ipHostEntry = InternalGetHostByName(hostNameOrAddress,
true);
}

if (Logging.On) Logging.Exit(Logging.Sockets, "DNS",
"GetHostEntry", ipHostEntry);
return ipHostEntry;
}

By comparing the two, I see the difference is at the call of
InternalGetHostByAddress.

Code list 1: InternalGetHostByAddress(IPAddress.Parse(address), false,
true);
Code list 2: InternalGetHostByAddress(address, true, false);

The declaration of InternalGetHostByAddress is:
internal static IPHostEntry InternalGetHostByAddress(IPAddress address, bool
includeIPv6, bool throwOnFailure)

QSIDeveloper, are you using IPV6 on your side? If we gave a IPV6 address,
the code path in InternalGetHostByAddress is different when the includeIPv6
param equals true and false. (I do not paste the code of
InternalGetHostByAddress here, but you can see its code with
http://blogs.msdn.com/sburke/archiv...tudio-to-debug-net-framework-source-code.aspx)

IPv6 requires the use of getaddrinfo() rather than the traditional IPv4
gethostbyaddr() / gethostbyname(). getaddrinfo() is also protocol
independant in that it will also resolve IPv4 names / addresses. As a
result, it is the preferred resolution mechanism on platforms that support
it (Windows 5.1+). In other words, if your environment is Window NT4 or 2000
where getaddrinfo() is unsupported, IPv6 resolution does not work.
QSIDeveloper, would you please check the OS version?

If IPv6 is disabled, we could detect IPv6 addresses and throw an unsupported
platform exception.

My additional suggestions that my help you trouble-shoot the issue:

1. Debug into the .NET Dsn source code according to the article
http://blogs.msdn.com/sburke/archiv...tudio-to-debug-net-framework-source-code.aspx,
and step through the code path of GetHostEntry.

2. looking at what the DNS interaction is using netmon.
http://www.microsoft.com/downloads/...9d-f4d8-4213-8d17-2f6dde7d7aac&displaylang=en

Hope it helps

Regards,
Jialiang Ge
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=============================================

"Jialiang Ge [MSFT]" said:
Hello QSIDeveloper,

As far as I know, Dns.GetHostEntry returns IP address as the HostName when
no DNS servers respond to the reverse DNS query. For example, when all DNS
servers are unreachable. Accompanied with this, the call of
GetHostByAddress will throw a SocketException with message "The requested
name is valid, but no data of the requested type was found". However,
Dns.GetHostByAddress seems working fine on your side. Please give me some
time. I will look for other reasons for the abnormal behavior of
GetHostEntry.

For your second question "What is the correct way to get this information
in .Net 2.0":
System.Net.Dns.GetHostByAddress is for .Net 1.1 or prior. From .NET 2.0,
we
use Dns.GetHostEntry.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

Jialiang Ge [MSFT]

Hello QSIDeveloper,

Yes, 10.10.60.221 is a IPv4 address. My last reply was based on my analysis
of Dns's source code and I found that IPv6 is a very possible cause of the
issue, thus I posted the finding to you. Now that we are using IPv4
address, I'd answer your question (1), (2) by analyzing the code path of
IPv4 in Dns:

==========================================
Both Dns.GetHostByAddress and Dns.GetHostEntry first demand Dns permissions
by calling:
s_DnsPermission.Demand();
After then, they call InternalGetHostByAddress.

In InternalGetHostByAddress, they first detect whether it's related to
IPv6. Because our address is a IPv4 one, the code path of both
Dns.GetHostByAddress and Dns.GetHostEntry go to this piece of code:

**********************************************************
// Use gethostbyaddr() to try to resolve the IP address
//
// End IPv6 Changes
//
int addressAsInt = unchecked((int)address.m_Address);
#if BIGENDIAN
addressAsInt = (int)( ((uint)addressAsInt << 24) |
(((uint)addressAsInt & 0x0000FF00) << 8) |
(((uint)addressAsInt >> 8) & 0x0000FF00) |
((uint)addressAsInt >> 24) );
#endif
IntPtr nativePointer =
UnsafeNclNativeMethods.OSSOCK.gethostbyaddr(
ref addressAsInt,
Marshal.SizeOf(typeof(int)),
ProtocolFamily.InterNetwork);
if (nativePointer != IntPtr.Zero) {
return NativeToHostEntry(nativePointer);
}
exception = new SocketException();
}
if(throwOnFailure){
throw exception;
}
IPHostEntry ipHostEntry = new IPHostEntry();
try{
ipHostEntry.HostName = address.ToString();
ipHostEntry.Aliases = new string[0];
ipHostEntry.AddressList = new IPAddress[] { address };
}
catch{
throw exception; //throw the original exception
}
return ipHostEntry;
} // InternalGetHostByAddress

**********************************************************

The code above first call the native function
UnsafeNclNativeMethods.OSSOCK.gethostbyaddr to get the host info. If it
succeeds, it call NativeToHostEntry to translate the native result to a
IPHostEntry object, otherwise, it throws an exception if throwOnFailture =
true, or sets the hostEntry's hostname property as the IP address.

In my opinion, there are two possible reasons for our symptom:

******* POSSIBLE REASON 1. *******
The native call UnsafeNclNativeMethods.OSSOCK.gethostbyaddr fails, and the
code path goes to:
try{
ipHostEntry.HostName = address.ToString();
ipHostEntry.Aliases = new string[0];
ipHostEntry.AddressList = new IPAddress[] { address };
}
,where the host name is set to the IP address.

This is a very possible reason, however, this assumption cannot explain why
GetHostByAddress is able to retrieve the host name info. GetHostByAddress
sets the parameter throwOnFailure = true. Thus, if GetHostByAddress also
fails at UnsafeNclNativeMethods.OSSOCK.gethostbyaddr, it would throw an
exception:

if(throwOnFailure){
throw exception;
}

******* POSSIBLE REASON 2. *********
The native call UnsafeNclNativeMethods.OSSOCK.gethostbyaddr succeeds, and
it calls
if (nativePointer != IntPtr.Zero) {
return NativeToHostEntry(nativePointer);
}

In the function NativeToHostEntry, the host name property is set at the
beginning:

hostent Host = (hostent)Marshal.PtrToStructure(nativePointer,
typeof(hostent));
IPHostEntry HostEntry = new IPHostEntry();

if (Host.h_name != IntPtr.Zero) {
HostEntry.HostName = Marshal.PtrToStringAnsi(Host.h_name);
GlobalLog.Print("HostEntry.HostName: " +
HostEntry.HostName);
}

In other words, UnsafeNclNativeMethods.OSSOCK.gethostbyaddr seems
successful, but it failed to get the correct host name.
The native function gethostbyaddr is introduced at:
http://msdn.microsoft.com/en-us/library/ms738521(VS.85).aspx
Currently, I cannot tell why the native call may return the ip address in
hostent.h_name because I'm not an expert on DNS and network, but I would
like to help you consult the product group, to see whether they have any
ideas.

==========================================
My Suggestions for you:

Debug into the source code of Dns, and compare the code path of
GetHostEntry and GetHostByAddress. In this way, we can tell which reason
above causes the problem.
(P.S. If I could reproduce the symptom in my lab, I would have debugged
into the issue for you.)

==========================================
For your question (3) If System.Net.Dns.Resolve and
System.Net.Dns.GetHostByAddress are
obsolete when will they stop working in the 2.0 framework?

The two functions are obsolete majorly because they cannot handle IPv6. For
IPv4, they will continue working.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
J

Jialiang Ge [MSFT]

Hello QSIDeveloper

Never mind my last reply. I get the product team's confirmation:

GetHostEntry tries to do a reverse DNS resolve when an IPaddress is passed.
It's very likely that the DNS server you are using does not have the
correct reverse lookup data for the IPAddress you are testing with. This is
a known issue of GetHostEntry, and it has been fixed in Windows Vista. You
could use Dns.GetHostAddresses if you just want the IP Address to be
returned without a reverse lookup attempted.

Please let me know if you have any other concerns about it. You may also
consider contacting Microsoft Customer Support Sercice, which will be free
for you in this case because the issue has been confirmed as our product
issue.

Have a nice weekend!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
J

Jialiang Ge [MSFT]

Hello QSIDeveloper,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of the suggestions? If you have any
concerns/questions regarding the issue, please DON'T hesitate to tell me.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top