Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Server.Kestrel/Http/TcpListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override UvStreamHandle CreateListenSocket(string host, int port)
{
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override UvStreamHandle CreateListenSocket(string host, int port)
{
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;
}
Expand Down
28 changes: 27 additions & 1 deletion src/Microsoft.AspNet.Server.Kestrel/Networking/UvTcpHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public void Init(UvLoopHandle loop, Action<Action<IntPtr>, IntPtr> queueCloseHan
_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();

Expand All @@ -58,5 +60,29 @@ public void Open(IntPtr hSocket)
{
_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);
}
}
}
25 changes: 25 additions & 0 deletions test/Microsoft.AspNet.Server.KestrelTests/CreateIPEndpointTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void ServerPipeDispatchConnections()

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);
Expand Down
8 changes: 4 additions & 4 deletions test/Microsoft.AspNet.Server.KestrelTests/NetworkingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void SocketCanBeInitAndClose()
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();
Expand All @@ -95,7 +95,7 @@ public async Task SocketCanListenAndAccept()
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);
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task SocketCanRead()
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");
Expand Down Expand Up @@ -188,7 +188,7 @@ public async Task SocketCanReadAndWrite()
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");
Expand Down