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

Petri Latvala adrinael at adrinael.net
Fri Jun 30 06:39:27 EDT 2006


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.


-- 
Petri Latvala
Adrinael
-------------- next part --------------
Index: class/System/Test/System.Net/DnsTest.cs
===================================================================
--- class/System/Test/System.Net/DnsTest.cs	(revision 62136)
+++ class/System/Test/System.Net/DnsTest.cs	(working copy)
@@ -169,6 +169,16 @@
                 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";
+
+		AssertEquals (Dns.GetHostAddresses (localaddr) [0].ToString (), localaddr);
+		AssertEquals (Dns.GetHostAddresses (arbitraryaddr) [0].ToString (), arbitraryaddr);
+	}
 #endif
 
         private void SubTestValidIPHostEntry(IPHostEntry h) {
Index: class/System/System.Net/Dns.cs
===================================================================
--- class/System/System.Net/Dns.cs	(revision 62136)
+++ class/System/System.Net/Dns.cs	(working copy)
@@ -277,7 +277,16 @@
 			if (hostNameOrAddress == null)
 				throw new ArgumentNullException ("hostNameOrAddress");
 
-			return GetHostEntry (hostNameOrAddress).AddressList;
+			IPAddress addr;
+			if (IPAddress.TryParse (hostNameOrAddress, out addr))
+			{
+				return new IPAddress[1] { addr };
+			}
+			else
+			{
+			  
+				return GetHostEntry (hostNameOrAddress).AddressList;
+			}
 		}
 #endif
 
-------------- 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/20060630/78d18ae8/attachment.bin 


More information about the Mono-devel-list mailing list