Skip to content

Commit

Permalink
LocalForward: when binding IPv6Any set Socket to DualMode. (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds authored Dec 14, 2024
1 parent ea3a348 commit 75e0ab6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Tmds.Ssh/LocalForward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ private void Start(EndPoint bindEP, string remoteEndPoint, Func<CancellationToke
if (bindEP is IPEndPoint ipEndPoint)
{
_serverSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

if (ipEndPoint.Address.Equals(IPAddress.IPv6Any))
{
_serverSocket.DualMode = true;
}
}
else if (bindEP is UnixDomainSocketEndPoint unixEndPoint)
{
Expand Down
14 changes: 13 additions & 1 deletion test/Tmds.Ssh.Tests/LocalForwardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,17 @@ public async Task StopsWhenClientDisconnects()

using var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
await Assert.ThrowsAnyAsync<SocketException>(async () => await socket.ConnectAsync(endPoint));
}
}

[Fact]
public async Task IPv6IsDualMode()
{
using var client = await _sshServer.CreateClientAsync();

using var localForward = await client.StartForwardTcpAsync(new IPEndPoint(IPAddress.IPv6Any, 0), "nowhere", 10);

using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

await socket.ConnectAsync(new IPEndPoint(IPAddress.Loopback, (localForward.EndPoint as IPEndPoint)!.Port));
}
}

0 comments on commit 75e0ab6

Please sign in to comment.