Merge pull request #3626 from lateralusX/jlorenss/win-api-family-support-eglib
[mono.git] / mcs / class / System / System.Net / Dns.cs
index de0e569c63a2b1c9a0e835ec69a5633ee407a916..806bfc119d6e52d9f4157c3ba1552643104a9beb 100644 (file)
@@ -41,9 +41,7 @@ using System.Collections;
 using System.Threading;
 using System.Runtime.CompilerServices;
 using System.Runtime.Remoting.Messaging;
-#if NET_4_5
 using System.Threading.Tasks;
-#endif
 
 #if !MOBILE
 using Mono.Net.Dns;
@@ -58,8 +56,6 @@ namespace System.Net {
 
                static Dns ()
                {
-                       System.Net.Sockets.Socket.CheckProtocolSupport();
-
 #if !MOBILE
                        if (Environment.GetEnvironmentVariable ("MONO_DNS") != null) {
                                resolver = new SimpleResolver ();
@@ -74,9 +70,6 @@ namespace System.Net {
                }
 #endif
 
-#if !MOONLIGHT // global remove of async methods
-
-
                private delegate IPHostEntry GetHostByNameCallback (string hostName);
                private delegate IPHostEntry ResolveCallback (string hostName);
                private delegate IPHostEntry GetHostEntryNameCallback (string hostName);
@@ -129,7 +122,7 @@ namespace System.Net {
                                throw ares.Exception;
                        IPHostEntry entry = ares.HostEntry;
                        if (entry == null || entry.AddressList == null || entry.AddressList.Length == 0)
-                               throw new SocketException(11001);
+                               Error_11001 (entry.HostName);
                        return entry;
                }
 #endif
@@ -137,6 +130,9 @@ namespace System.Net {
                [Obsolete ("Use BeginGetHostEntry instead")]
                public static IAsyncResult BeginGetHostByName (string hostName, AsyncCallback requestCallback, object stateObject)
                {
+#if FEATURE_NO_BSD_SOCKETS
+                       throw new PlatformNotSupportedException ("System.Net.Dns:BeginGetHostByName is not supported on this platform.");
+#else
                        if (hostName == null)
                                throw new ArgumentNullException ("hostName");
 
@@ -147,11 +143,15 @@ namespace System.Net {
 
                        GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
                        return c.BeginInvoke (hostName, requestCallback, stateObject);
+#endif // FEATURE_NO_BSD_SOCKETS
                }
 
                [Obsolete ("Use BeginGetHostEntry instead")]
                public static IAsyncResult BeginResolve (string hostName, AsyncCallback requestCallback, object stateObject)
                {
+#if FEATURE_NO_BSD_SOCKETS
+                       throw new PlatformNotSupportedException ("System.Net.Dns:BeginResolve is not supported on this platform.");
+#else
                        if (hostName == null)
                                throw new ArgumentNullException ("hostName");
 
@@ -162,6 +162,7 @@ namespace System.Net {
 
                        ResolveCallback c = new ResolveCallback (Resolve);
                        return c.BeginInvoke (hostName, requestCallback, stateObject);
+#endif // FEATURE_NO_BSD_SOCKETS
                }
 
                public static IAsyncResult BeginGetHostAddresses (string hostNameOrAddress, AsyncCallback requestCallback, object state)
@@ -185,6 +186,9 @@ namespace System.Net {
 
                public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress, AsyncCallback requestCallback, object stateObject)
                {
+#if FEATURE_NO_BSD_SOCKETS
+                       throw new PlatformNotSupportedException ("System.Net.Dns:GetHostEntry is not supported on this platform.");
+#else
                        if (hostNameOrAddress == null)
                                throw new ArgumentNullException ("hostName");
                        if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
@@ -200,10 +204,14 @@ namespace System.Net {
 
                        GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
                        return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
+#endif // FEATURE_NO_BSD_SOCKETS
                }
 
                public static IAsyncResult BeginGetHostEntry (IPAddress address, AsyncCallback requestCallback, object stateObject)
                {
+#if FEATURE_NO_BSD_SOCKETS
+                       throw new PlatformNotSupportedException ("System.Net.Dns:BeginGetHostEntry is not supported on this platform.");
+#else
                        if (address == null)
                                throw new ArgumentNullException ("address");
 
@@ -214,6 +222,7 @@ namespace System.Net {
 
                        GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
                        return c.BeginInvoke (address, requestCallback, stateObject);
+#endif // FEATURE_NO_BSD_SOCKETS
                }
 
                [Obsolete ("Use EndGetHostEntry instead")]
@@ -283,10 +292,8 @@ namespace System.Net {
                        GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
                        return cb.EndInvoke(asyncResult);
                }
-               
-#endif // !MOONLIGHT: global remove of async methods
 
-#if !TARGET_JVM
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
 
@@ -295,9 +302,14 @@ namespace System.Net {
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static bool GetHostName_internal(out string h_name);
-#endif 
 
-               private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) 
+               static void Error_11001 (string hostName)
+               {
+                       throw new SocketException(11001, string.Format ("Could not resolve host '{0}'", hostName));
+
+               }
+
+               private static IPHostEntry hostent_to_IPHostEntry(string originalHostName, string h_name, string[] h_aliases, string[] h_addrlist) 
                {
                        IPHostEntry he = new IPHostEntry();
                        ArrayList addrlist = new ArrayList();
@@ -321,7 +333,7 @@ namespace System.Net {
                        }
 
                        if(addrlist.Count == 0)
-                               throw new SocketException(11001);
+                               Error_11001 (originalHostName);
 
                        he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
                        return he;
@@ -360,30 +372,10 @@ namespace System.Net {
 
                        string h_name;
                        string[] h_aliases, h_addrlist;
-#if TARGET_JVM
-                       h_name = null;
-                       h_aliases = null;
-                       h_addrlist = null;
-                       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)
-                               throw new SocketException(11001);
-#endif
-                       return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
+                               Error_11001 (address);
+                       return (hostent_to_IPHostEntry (address, h_name, h_aliases, h_addrlist));
                        
                }
 
@@ -433,58 +425,40 @@ namespace System.Net {
                [Obsolete ("Use GetHostEntry instead")]
                public static IPHostEntry GetHostByName (string hostName)
                {
+#if FEATURE_NO_BSD_SOCKETS
+                       if (!string.IsNullOrEmpty (hostName))
+                               throw new PlatformNotSupportedException ("System.Net.Dns:GetHostByName is not supported on this platform.");
+#endif // FEATURE_NO_BSD_SOCKETS
                        if (hostName == null)
                                throw new ArgumentNullException ("hostName");
-#if TARGET_JVM
-                       if (hostName.Length == 0)
-                               hostName = "localhost";
-                       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);
-                       }
-#else
                        string h_name;
                        string[] h_aliases, h_addrlist;
 
                        bool ret = GetHostByName_internal(hostName, out h_name, out h_aliases, out h_addrlist);
                        if (ret == false)
-                               throw new SocketException(11001);
+                               Error_11001 (hostName);
 
-                       return(hostent_to_IPHostEntry(h_name, h_aliases, h_addrlist));
-#endif
+                       return(hostent_to_IPHostEntry(hostName, h_name, h_aliases, h_addrlist));
                }
 
                public static string GetHostName ()
                {
-#if TARGET_JVM
-                       return java.net.InetAddress.getLocalHost ().getHostName ();
-#else
                        string hostName;
 
                        bool ret = GetHostName_internal(out hostName);
 
                        if (ret == false)
-                               throw new SocketException(11001);
+                               Error_11001 (hostName);
 
                        return hostName;
-#endif
                }
 
                [Obsolete ("Use GetHostEntry instead")]
                public static IPHostEntry Resolve(string hostName) 
                {
+#if FEATURE_NO_BSD_SOCKETS
+                       throw new PlatformNotSupportedException ("System.Net.Dns:Resolve is not supported on this platform.");
+#else
                        if (hostName == null)
                                throw new ArgumentNullException ("hostName");
 
@@ -499,9 +473,9 @@ namespace System.Net {
                                ret =  GetHostByName(hostName);
 
                        return ret;
+#endif // FEATURE_NO_BSD_SOCKETS
                }
 
-#if NET_4_5
                public static Task<IPAddress[]> GetHostAddressesAsync (string hostNameOrAddress)
                {
                        return Task<IPAddress[]>.Factory.FromAsync (BeginGetHostAddresses, EndGetHostAddresses, hostNameOrAddress, null);
@@ -516,7 +490,6 @@ namespace System.Net {
                {
                        return Task<IPHostEntry>.Factory.FromAsync (BeginGetHostEntry, EndGetHostEntry, hostNameOrAddress, null);
                }
-#endif
        }
 }