473,520 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14479
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
488
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 problems. When I open a solution VS.net tells me it can't find the VSS database and gives me an option to "work disconnected". If I select this...
6
1945
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 not working, the Service is in "Network Service".....i even tried "Local System" and also "Local Service" but the end result is , it is working only...
3
6960
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
7438
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 answer. The old Dns.GetHostByAddress seems to work partially by atleast returning 1 IpHostEntry, but it has been deprecated. Any ideas on how...
2
6183
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 "PC_01" Remote address is 192.168.0.20, on a local LAN, and named "PC_02" DNS.GetHostByAddress("192.168.0.20").Hostname returns "PC_02"...
2
5372
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 "PC_01" Remote address is 192.168.0.20, on a local LAN, and named "PC_02" DNS.GetHostByAddress("192.168.0.20").Hostname returns "PC_02"...
3
1440
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 file and of course it always gives compiler errors. I've asked the designer not to remove any controls, div tags etc. The problem is the errors...
2
1575
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 reference its giving the following error. ++++++++++++++++++++++++++++++++++++++++++++++++ Server Error in '/' Application....
3
16573
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 IPEndPoint(Dns.GetHostByAddress(ipAddr).AddressList, m_iRemotePort); // ep = new IPEndPoint(Dns.GetHostEntry(ipAddr).AddressList, m_iRemotePort); // ep...
0
7219
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7450
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7613
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7177
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3287
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1663
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
844
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.