static EndPointListener GetEPListener (string host, int port, HttpListener listener, bool secure)
{
IPAddress addr;
- if (IPAddress.TryParse(host, out addr) == false)
+ 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];
t.Start ();
t.Stop ();
}
+
+ //
+ // Bugs: #17204, #10818
+ //
+ // Sadly, on Unix, if there are different calls to bind
+ // like *:port and host:port that is not an error,
+ // it would only be an error if host:port is done twice, so
+ // the best we can hope for is that listening on a specific interface
+ // does not also listen to another interface.
+ //
+ [Test]
+ public void BindToSingleInterface ()
+ {
+ var machineAddress = Dns.GetHostAddresses (Dns.GetHostName ());
+ int port = 61234;
+ var h = new HttpListener ();
+ h.Prefixes.Add ("http://" + machineAddress [0] + ":" + port + "/");
+ h.Start ();
+
+ try {
+ var c = new TcpClient ("localhost", port);
+ Assert.Fail ("The TcpClient should have failed to connect since HttpListener is not listening on localhost");
+ } catch (SocketException){
+ // Pass
+ }
+ h.Stop ();
+ }
+
+ [Test]
+ public void BindToAllInterfaces ()
+ {
+ var h = new HttpListener ();
+ int port = 62234;
+ h.Prefixes.Add ("http://*:" + port + "/");
+ h.Start ();
+ var c = new TcpClient ("localhost", port);
+ h.Stop ();
+ }
}
}