473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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?

Aug 14 '08 #1
5 14437
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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 15 '08 #2
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_a ddr),
"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/archive...urce-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/archive...urce-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/d...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:
ms****@microsoft.com.

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

""Jialiang Ge [MSFT]"" <ji****@online.microsoft.comwrote in message
news:iZ**************@TK2MSFTNGHUB02.phx.gbl...
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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 20 '08 #3
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/libr...21(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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

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

Aug 22 '08 #4
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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

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

Aug 22 '08 #5
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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

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

Aug 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: RMG | last post by:
I use VS.net 2003 and VSS 6a. Whilst working on the network the integration between VS.net and VSS works perfectly. But when I open a project whilst disconnected from the network I get some...
6
by: Chakkaradeep | last post by:
hi all, I have written a C# Service which is a Server accepting Clients,It is working fine when i connect from localhost but when i connect from any other machine to my machine , the service is...
3
by: ad | last post by:
Hi, I use the code below to get the IP of computer: My computer has only one ip: 192.168.0.100, But there are tree IPs get by GetHostEntry: 192.168.0.100 192.168.0.1 192.168.122.1 Why?
3
by: RWF | last post by:
I am trying to do a reverse DNS lookup using Dns.GetHostEntry in .NET 2.0, but it seems to kind of crap out when the result is an answer with multiple records. It doesnt seem to give any type of...
2
by: The Grim Reaper | last post by:
Hi all, I have a small "irritant" to do with DNS resolving in .NET Framework v2.0. For this example, assume the following network information; Local IP address is 192.168.0.10, on a PC named...
2
by: The Grim Reaper | last post by:
Hi all, I have a small "irritant" to do with DNS resolving in .NET Framework v2.0. For this example, assume the following network information; Local IP address is 192.168.0.10, on a PC named...
3
by: PeterKellner | last post by:
I have a project where a html designer gives me a pile of html to wire up. I do that and it works fine of course. Then, the html designer makes a bunch of changes, give me back the revised aspx...
2
by: prad | last post by:
Hi, I am working with webservices in asp.net framework 2.0. when i run the webservice it didnot give any errors and it was working well but when I add it to one of my projects as add web...
3
by: Steve Richter | last post by:
I am using Dns.GetHostByAddress and Dns.Resolve to successfully resolve and IP address to an IPEndPoint. IPEndPoint ep = null; IPAddress ipAddr = IPAddress.Parse(m_sRemoteHost); ep = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.