Bind to specific IP addresses if provided with any

This still only applies to IPv4.

#98
This commit is contained in:
Stephen Halter 2015-09-25 15:53:30 -07:00
parent a7b65efa75
commit f0137b7b9e
6 changed files with 59 additions and 8 deletions

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{
var socket = new UvTcpHandle(Log);
socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.Bind(new IPEndPoint(IPAddress.Any, port));
socket.Bind(host, port);
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
return socket;
}

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{
var socket = new UvTcpHandle(Log);
socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.Bind(new IPEndPoint(IPAddress.Any, port));
socket.Bind(host, port);
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
return socket;
}

View File

@ -33,8 +33,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
_uv.tcp_init(loop, this);
}
public void Bind(IPEndPoint endpoint)
public void Bind(string host, int port)
{
var endpoint = CreateIPEndpoint(host, port);
Libuv.sockaddr addr;
var addressText = endpoint.Address.ToString();
@ -58,5 +60,29 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{
_uv.tcp_open(this, hSocket);
}
/// <summary>
/// Returns an <see cref="IPEndPoint"/> for the given host an port.
/// If the host parameter isn't "localhost" or an IP address, use IPAddress.Any.
/// </summary>
public static IPEndPoint CreateIPEndpoint(string host, int port)
{
// TODO: IPv6 support
IPAddress ip;
if (!IPAddress.TryParse(host, out ip))
{
if (string.Equals(host, "localhost", StringComparison.OrdinalIgnoreCase))
{
ip = IPAddress.Loopback;
}
else
{
ip = IPAddress.Any;
}
}
return new IPEndPoint(ip, port);
}
}
}

View File

@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net;
using Microsoft.AspNet.Server.Kestrel.Networking;
using Xunit;
namespace Microsoft.AspNet.Server.KestrelTests
{
public class CreateIPEndpointTests
{
[Theory]
[InlineData("localhost", "127.0.0.1")]
[InlineData("10.10.10.10", "10.10.10.10")]
[InlineData("randomhost", "0.0.0.0")]
public void CorrectIPEndpointsAreCreated(string host, string expectedAddress)
{
// "0.0.0.0" is IPAddress.Any
var endpoint = UvTcpHandle.CreateIPEndpoint(host, 5000);
Assert.NotNull(endpoint);
Assert.Equal(IPAddress.Parse(expectedAddress), endpoint.Address);
Assert.Equal(5000, endpoint.Port);
}
}
}

View File

@ -166,7 +166,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
var serverListenTcp = new UvTcpHandle(_logger);
serverListenTcp.Init(loop);
serverListenTcp.Bind(new IPEndPoint(0, 54321));
serverListenTcp.Bind("0.0.0.0", 54321);
serverListenTcp.Listen(128, (_1, status, error, _2) =>
{
var serverConnectionTcp = new UvTcpHandle(_logger);

View File

@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
loop.Init(_uv);
var tcp = new UvTcpHandle(_logger);
tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 0));
tcp.Bind("localhost", 0);
tcp.Dispose();
loop.Run();
loop.Dispose();
@ -95,7 +95,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
loop.Init(_uv);
var tcp = new UvTcpHandle(_logger);
tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
tcp.Bind("localhost", 54321);
tcp.Listen(10, (stream, status, error, state) =>
{
var tcp2 = new UvTcpHandle(_logger);
@ -132,7 +132,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
loop.Init(_uv);
var tcp = new UvTcpHandle(_logger);
tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
tcp.Bind("localhost", 54321);
tcp.Listen(10, (_, status, error, state) =>
{
Console.WriteLine("Connected");
@ -188,7 +188,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
loop.Init(_uv);
var tcp = new UvTcpHandle(_logger);
tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
tcp.Bind("localhost", 54321);
tcp.Listen(10, (_, status, error, state) =>
{
Console.WriteLine("Connected");