Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit f0137b7

Browse files
committed
Bind to specific IP addresses if provided with any
This still only applies to IPv4. #98
1 parent a7b65ef commit f0137b7

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/TcpListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override UvStreamHandle CreateListenSocket(string host, int port)
2525
{
2626
var socket = new UvTcpHandle(Log);
2727
socket.Init(Thread.Loop, Thread.QueueCloseHandle);
28-
socket.Bind(new IPEndPoint(IPAddress.Any, port));
28+
socket.Bind(host, port);
2929
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
3030
return socket;
3131
}

src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerPrimary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override UvStreamHandle CreateListenSocket(string host, int port)
2525
{
2626
var socket = new UvTcpHandle(Log);
2727
socket.Init(Thread.Loop, Thread.QueueCloseHandle);
28-
socket.Bind(new IPEndPoint(IPAddress.Any, port));
28+
socket.Bind(host, port);
2929
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
3030
return socket;
3131
}

src/Microsoft.AspNet.Server.Kestrel/Networking/UvTcpHandle.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public void Init(UvLoopHandle loop, Action<Action<IntPtr>, IntPtr> queueCloseHan
3333
_uv.tcp_init(loop, this);
3434
}
3535

36-
public void Bind(IPEndPoint endpoint)
36+
public void Bind(string host, int port)
3737
{
38+
var endpoint = CreateIPEndpoint(host, port);
39+
3840
Libuv.sockaddr addr;
3941
var addressText = endpoint.Address.ToString();
4042

@@ -58,5 +60,29 @@ public void Open(IntPtr hSocket)
5860
{
5961
_uv.tcp_open(this, hSocket);
6062
}
63+
64+
/// <summary>
65+
/// Returns an <see cref="IPEndPoint"/> for the given host an port.
66+
/// If the host parameter isn't "localhost" or an IP address, use IPAddress.Any.
67+
/// </summary>
68+
public static IPEndPoint CreateIPEndpoint(string host, int port)
69+
{
70+
// TODO: IPv6 support
71+
IPAddress ip;
72+
73+
if (!IPAddress.TryParse(host, out ip))
74+
{
75+
if (string.Equals(host, "localhost", StringComparison.OrdinalIgnoreCase))
76+
{
77+
ip = IPAddress.Loopback;
78+
}
79+
else
80+
{
81+
ip = IPAddress.Any;
82+
}
83+
}
84+
85+
return new IPEndPoint(ip, port);
86+
}
6187
}
6288
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Net;
5+
using Microsoft.AspNet.Server.Kestrel.Networking;
6+
using Xunit;
7+
8+
namespace Microsoft.AspNet.Server.KestrelTests
9+
{
10+
public class CreateIPEndpointTests
11+
{
12+
[Theory]
13+
[InlineData("localhost", "127.0.0.1")]
14+
[InlineData("10.10.10.10", "10.10.10.10")]
15+
[InlineData("randomhost", "0.0.0.0")]
16+
public void CorrectIPEndpointsAreCreated(string host, string expectedAddress)
17+
{
18+
// "0.0.0.0" is IPAddress.Any
19+
var endpoint = UvTcpHandle.CreateIPEndpoint(host, 5000);
20+
Assert.NotNull(endpoint);
21+
Assert.Equal(IPAddress.Parse(expectedAddress), endpoint.Address);
22+
Assert.Equal(5000, endpoint.Port);
23+
}
24+
}
25+
}

test/Microsoft.AspNet.Server.KestrelTests/MultipleLoopTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void ServerPipeDispatchConnections()
166166

167167
var serverListenTcp = new UvTcpHandle(_logger);
168168
serverListenTcp.Init(loop);
169-
serverListenTcp.Bind(new IPEndPoint(0, 54321));
169+
serverListenTcp.Bind("0.0.0.0", 54321);
170170
serverListenTcp.Listen(128, (_1, status, error, _2) =>
171171
{
172172
var serverConnectionTcp = new UvTcpHandle(_logger);

test/Microsoft.AspNet.Server.KestrelTests/NetworkingTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void SocketCanBeInitAndClose()
8181
loop.Init(_uv);
8282
var tcp = new UvTcpHandle(_logger);
8383
tcp.Init(loop);
84-
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 0));
84+
tcp.Bind("localhost", 0);
8585
tcp.Dispose();
8686
loop.Run();
8787
loop.Dispose();
@@ -95,7 +95,7 @@ public async Task SocketCanListenAndAccept()
9595
loop.Init(_uv);
9696
var tcp = new UvTcpHandle(_logger);
9797
tcp.Init(loop);
98-
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
98+
tcp.Bind("localhost", 54321);
9999
tcp.Listen(10, (stream, status, error, state) =>
100100
{
101101
var tcp2 = new UvTcpHandle(_logger);
@@ -132,7 +132,7 @@ public async Task SocketCanRead()
132132
loop.Init(_uv);
133133
var tcp = new UvTcpHandle(_logger);
134134
tcp.Init(loop);
135-
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
135+
tcp.Bind("localhost", 54321);
136136
tcp.Listen(10, (_, status, error, state) =>
137137
{
138138
Console.WriteLine("Connected");
@@ -188,7 +188,7 @@ public async Task SocketCanReadAndWrite()
188188
loop.Init(_uv);
189189
var tcp = new UvTcpHandle(_logger);
190190
tcp.Init(loop);
191-
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
191+
tcp.Bind("localhost", 54321);
192192
tcp.Listen(10, (_, status, error, state) =>
193193
{
194194
Console.WriteLine("Connected");

0 commit comments

Comments
 (0)