New tests.
[mono.git] / mcs / class / System / System.Net / Dns.cs
index f4a2e47aea1d9ac8655ac8fe78d495e16936f9b1..15a17be550041196a7c988ed5845394c24d3ca08 100644 (file)
@@ -48,6 +48,8 @@ namespace System.Net {
                        System.Net.Sockets.Socket.CheckProtocolSupport();
                }
 
+#if !MOONLIGHT // global remove of async methods
+
                private delegate IPHostEntry GetHostByNameCallback (string hostName);
                private delegate IPHostEntry ResolveCallback (string hostName);
 #if NET_2_0
@@ -83,34 +85,44 @@ namespace System.Net {
                }
 
 #if NET_2_0
-               public static IAsyncResult BeginGetHostAddresses (string hostName,
+               public static IAsyncResult BeginGetHostAddresses (string hostNameOrAddress,
                        AsyncCallback requestCallback, object stateObject)
                {
-                       if (hostName == null)
+                       if (hostNameOrAddress == null)
                                throw new ArgumentNullException ("hostName");
+                       if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
+                               throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
+                                       "and ::0 (IPv6) are unspecified addresses. You " +
+                                       "cannot use them as target address.",
+                                       "hostNameOrAddress");
 
                        GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
-                       return c.BeginInvoke (hostName, requestCallback, stateObject);
+                       return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
                }
 
                public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress,
                        AsyncCallback requestCallback, object stateObject)
                {
                        if (hostNameOrAddress == null)
-                               throw new ArgumentNullException ("hostNameOrAddress");
+                               throw new ArgumentNullException ("hostName");
+                       if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
+                               throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
+                                       "and ::0 (IPv6) are unspecified addresses. You " +
+                                       "cannot use them as target address.",
+                                       "hostNameOrAddress");
 
                        GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
                        return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
                }
 
-               public static IAsyncResult BeginGetHostEntry (IPAddress hostAddress,
+               public static IAsyncResult BeginGetHostEntry (IPAddress address,
                        AsyncCallback requestCallback, object stateObject)
                {
-                       if (hostAddress == null)
-                               throw new ArgumentNullException ("hostAddress");
+                       if (address == null)
+                               throw new ArgumentNullException ("address");
 
                        GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
-                       return c.BeginInvoke (hostAddress, requestCallback, stateObject);
+                       return c.BeginInvoke (address, requestCallback, stateObject);
                }
 #endif
 
@@ -164,6 +176,8 @@ namespace System.Net {
                        return cb.EndInvoke(asyncResult);
                }
 #endif
+               
+#endif // !MOONLIGHT: global remove of async methods
 
 #if !TARGET_JVM
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -247,14 +261,20 @@ namespace System.Net {
                        h_name = null;
                        h_aliases = null;
                        h_addrlist = null;
-                       java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName (address);
-                       if (iaArr != null && iaArr.Length > 0)
-                               h_name = iaArr[0].getHostName ();
-                       if (iaArr != null && iaArr.Length > 0) {
-                               h_addrlist = new String[iaArr.Length];
-                               for (int i = 0; i < h_addrlist.Length; i++)
-                                       h_addrlist[i] = iaArr[i].getHostAddress ();
-                       }
+                       try {
+                               java.net.InetAddress[] iaArr = 
+                                       java.net.InetAddress.getAllByName(address);
+                               if (iaArr != null && iaArr.Length > 0)
+                                   h_name = iaArr[0].getHostName();
+                               if (iaArr != null && iaArr.Length > 0)
+                               {
+                                   h_addrlist = new String[iaArr.Length];
+                                   for (int i = 0; i < h_addrlist.Length; i++)
+                                       h_addrlist[i] = iaArr[i].getHostAddress();
+                               }
+                       } catch (java.net.UnknownHostException jUHE) {
+                               throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
+                       }
 #else
                        bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
                        if (!ret)
@@ -265,21 +285,33 @@ namespace System.Net {
                }
 
 #if NET_2_0
-               public static IPHostEntry GetHostEntry (string hostNameOrAddress)
+               public
+#else
+               internal
+#endif
+               static IPHostEntry GetHostEntry (string hostNameOrAddress)
                {
                        if (hostNameOrAddress == null)
                                throw new ArgumentNullException ("hostNameOrAddress");
+                       if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
+                               throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
+                                       "and ::0 (IPv6) are unspecified addresses. You " +
+                                       "cannot use them as target address.",
+                                       "hostNameOrAddress");
 
-                       if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
-                               hostNameOrAddress = "127.0.0.1";
                        IPAddress addr;
-                       if (IPAddress.TryParse (hostNameOrAddress, out addr))
+                       if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
                                return GetHostEntry (addr);
-                       else
-                               return GetHostByName (hostNameOrAddress);
+
+                       return GetHostByName (hostNameOrAddress);
                }
 
-               public static IPHostEntry GetHostEntry (IPAddress address)
+#if NET_2_0
+               public
+#else
+               internal
+#endif
+               static IPHostEntry GetHostEntry (IPAddress address)
                {
                        if (address == null)
                                throw new ArgumentNullException ("address");
@@ -287,26 +319,28 @@ namespace System.Net {
                        return GetHostByAddressFromString (address.ToString (), false);
                }
 
-               public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
+#if NET_2_0
+               public
+#else
+               internal
+#endif
+               static IPAddress [] GetHostAddresses (string hostNameOrAddress)
                {
                        if (hostNameOrAddress == null)
                                throw new ArgumentNullException ("hostNameOrAddress");
 
-                       if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
-                               hostNameOrAddress = "127.0.0.1";
+                       if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
+                               throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
+                                       "and ::0 (IPv6) are unspecified addresses. You " +
+                                       "cannot use them as target address.",
+                                       "hostNameOrAddress");
 
                        IPAddress addr;
-                       if (IPAddress.TryParse (hostNameOrAddress, out addr))
-                       {
+                       if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
                                return new IPAddress[1] { addr };
-                       }
-                       else
-                       {
-                         
-                               return GetHostEntry (hostNameOrAddress).AddressList;
-                       }
+
+                       return GetHostEntry (hostNameOrAddress).AddressList;
                }
-#endif
 
 #if NET_2_0
                [Obsolete ("Use GetHostEntry instead")]
@@ -314,37 +348,35 @@ namespace System.Net {
                public static IPHostEntry GetHostByName (string hostName)
                {
                        if (hostName == null)
-                               throw new ArgumentNullException ();
+                               throw new ArgumentNullException ("hostName");
 #if TARGET_JVM
                        if (hostName.Length == 0)
                                hostName = "localhost";
-
-                       java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName (hostName);
-                       IPHostEntry host = new IPHostEntry ();
-                       if (iaArr != null && iaArr.Length > 0) {
-
-                               host.HostName = iaArr[0].getHostName ();
-
-                               IPAddress[] ipArr = new IPAddress[iaArr.Length];
-                               for (int i = 0; i < iaArr.Length; i++)
-                                       ipArr[i] = IPAddress.Parse (iaArr[i].getHostAddress ());
-
-                               host.AddressList = ipArr;
-
+                       try {
+                               java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName(hostName);
+                               IPHostEntry host = new IPHostEntry();
+                               if (iaArr != null && iaArr.Length > 0)
+                               {
+                                       host.HostName = iaArr[0].getHostName();
+                                       IPAddress[] ipArr = new IPAddress[iaArr.Length];
+                                       for (int i = 0; i < iaArr.Length; i++)
+                                               ipArr[i] = IPAddress.Parse(iaArr[i].getHostAddress());
+
+                                       host.AddressList = ipArr;
+                               }
+                               return host;
+                       } catch (java.net.UnknownHostException jUHE) {
+                               throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
                        }
-                       return host;
 #else
                        string h_name;
                        string[] h_aliases, h_addrlist;
 
-                       bool ret = GetHostByName_internal(hostName, out h_name,
-                               out h_aliases,
-                               out h_addrlist);
+                       bool ret = GetHostByName_internal(hostName, out h_name, out h_aliases, out h_addrlist);
                        if (ret == false)
                                throw new SocketException(11001);
 
-                       return(hostent_to_IPHostEntry(h_name, h_aliases,
-                               h_addrlist));
+                       return(hostent_to_IPHostEntry(h_name, h_aliases, h_addrlist));
 #endif
                }