[System]: Remove 'SECURITY_DEP' conditional from HttpListener and related classes...
[mono.git] / mcs / class / System / System.Net / EndPointManager.cs
index dc05a339acd27dbb3591acb9d0ea94bfa790ad60..2f1658ab113ee155ffbb146e8c69ca85652d8abd 100644 (file)
@@ -26,8 +26,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0 && SECURITY_DEP
-
 using System.Collections;
 using System.Collections.Generic;
 namespace System.Net {
@@ -74,13 +72,27 @@ namespace System.Net {
                        if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code?
                                throw new HttpListenerException (400, "Invalid path.");
 
-                       // Always listens on all the interfaces, no matter the host name/ip used.
-                       EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
+                       // listens on all the interfaces if host name cannot be parsed by IPAddress.
+                       EndPointListener epl = GetEPListener (lp.Host, lp.Port, listener, lp.Secure);
                        epl.AddPrefix (lp, listener);
                }
 
-               static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
+               static EndPointListener GetEPListener (string host, int port, HttpListener listener, bool secure)
                {
+                       IPAddress addr;
+                       if (host == "*")
+                               addr = IPAddress.Any;
+                       else if (IPAddress.TryParse(host, out addr) == false){
+                               try {
+                                       IPHostEntry iphost = Dns.GetHostByName(host);
+                                       if (iphost != null)
+                                               addr = iphost.AddressList[0];
+                                       else
+                                               addr = IPAddress.Any;
+                               } catch {
+                                       addr = IPAddress.Any;
+                               } 
+                       }
                        Hashtable p = null;  // Dictionary<int, EndPointListener>
                        if (ip_to_endpoints.ContainsKey (addr)) {
                                p = (Hashtable) ip_to_endpoints [addr];
@@ -93,7 +105,7 @@ namespace System.Net {
                        if (p.ContainsKey (port)) {
                                epl = (EndPointListener) p [port];
                        } else {
-                               epl = new EndPointListener (addr, port, secure);
+                               epl = new EndPointListener (listener, addr, port, secure);
                                p [port] = epl;
                        }
 
@@ -139,10 +151,9 @@ namespace System.Net {
                        if (lp.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
                                return;
 
-                       EndPointListener epl = GetEPListener (IPAddress.Any, lp.Port, listener, lp.Secure);
+                       EndPointListener epl = GetEPListener (lp.Host, lp.Port, listener, lp.Secure);
                        epl.RemovePrefix (lp, listener);
                }
        }
 }
-#endif