[Mono-dev] Patch for System.Net.Dns.GetHostAddresses

Petri Latvala adrinael at adrinael.net
Wed Jul 5 04:01:45 EDT 2006


On Fri, Jun 30, 2006 at 01:39:27PM +0300, Petri Latvala wrote:
> MSDN says GetHostAddresses doesn't query DNS if given an IP address,
> but simply constructs an IPAddress array and returns it. Mono's
> implementation queries DNS to find the names for the IP, then
> discards them, and returns an array of IPAddress. Here's a patch to
> not query when given an IP, and a test for it.

The patch broke the case when passing an empty string. Here's a
revised patch that also makes GetHostEntry work correctly with the
empty string argument.

Comments, please.




Index: class/System/Test/System.Net/DnsTest.cs
===================================================================
--- class/System/Test/System.Net/DnsTest.cs	(revision 62252)
+++ class/System/Test/System.Net/DnsTest.cs	(working copy)
@@ -169,6 +169,18 @@
                 Dns.GetHostEntry (site1Name); // hostname
                 Dns.GetHostEntry (site1Dot); // IP address
         }
+
+	[Test]
+	public void GetHostAddresses ()
+	{
+		string localaddr = "127.0.0.1";
+		string arbitraryaddr = "1.2.3.4";
+		string emptyaddr = "";
+
+		AssertEquals (Dns.GetHostAddresses (localaddr) [0].ToString (), localaddr);
+		AssertEquals (Dns.GetHostAddresses (arbitraryaddr) [0].ToString (), arbitraryaddr);
+		AssertEquals (Dns.GetHostAddresses (emptyaddr) [0].ToString (), localaddr);
+	}
 #endif
 
         private void SubTestValidIPHostEntry(IPHostEntry h) {
Index: class/System/System.Net/Dns.cs
===================================================================
--- class/System/System.Net/Dns.cs	(revision 62252)
+++ class/System/System.Net/Dns.cs	(working copy)
@@ -255,7 +255,7 @@
 			if (hostNameOrAddress == null)
 				throw new ArgumentNullException ("hostNameOrAddress");
 
-			if (hostNameOrAddress == "0.0.0.0")
+			if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
 				hostNameOrAddress = "127.0.0.1";
 			IPAddress addr;
 			if (IPAddress.TryParse (hostNameOrAddress, out addr))
@@ -277,7 +277,19 @@
 			if (hostNameOrAddress == null)
 				throw new ArgumentNullException ("hostNameOrAddress");
 
-			return GetHostEntry (hostNameOrAddress).AddressList;
+			if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
+				hostNameOrAddress = "127.0.0.1";
+
+			IPAddress addr;
+			if (IPAddress.TryParse (hostNameOrAddress, out addr))
+			{
+				return new IPAddress[1] { addr };
+			}
+			else
+			{
+			  
+				return GetHostEntry (hostNameOrAddress).AddressList;
+			}
 		}
 #endif
 



-- 
Petri Latvala
Adrinael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060705/219d4e25/attachment.bin 


More information about the Mono-devel-list mailing list