From b3a38c366e0a63e1965cd84a98d2082370efac57 Mon Sep 17 00:00:00 2001 From: yaobangrui Date: Mon, 30 Nov 2026 17:17:05 +0800 Subject: [PATCH] Allow wildcard bound host for forwarded ports ForwardedPortLocal, ForwardedPortDynamic and ForwardedPortRemote resolve the bound host with Dns.GetHostAddresses, which throws ArgumentException for 0.0.0.0 and :: ("IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address"). The ports therefore could not be bound to all interfaces, even though Socket.Bind supports it. Parse IP literals directly and only fall back to name resolution for host names. Fixes #525 --- src/Renci.SshNet/ForwardedPortDynamic.cs | 2 +- src/Renci.SshNet/ForwardedPortLocal.cs | 2 +- src/Renci.SshNet/ForwardedPortRemote.cs | 2 +- ...edPortLocalTest_Start_WildcardBoundHost.cs | 44 +++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 test/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Start_WildcardBoundHost.cs diff --git a/src/Renci.SshNet/ForwardedPortDynamic.cs b/src/Renci.SshNet/ForwardedPortDynamic.cs index 7210b403a..8b499ae9b 100644 --- a/src/Renci.SshNet/ForwardedPortDynamic.cs +++ b/src/Renci.SshNet/ForwardedPortDynamic.cs @@ -157,7 +157,7 @@ private void InternalStart() var ip = IPAddress.Any; if (!string.IsNullOrEmpty(BoundHost)) { - ip = Dns.GetHostAddresses(BoundHost)[0]; + ip = IPAddress.TryParse(BoundHost, out var parsed) ? parsed : Dns.GetHostAddresses(BoundHost)[0]; } var ep = new IPEndPoint(ip, (int)BoundPort); diff --git a/src/Renci.SshNet/ForwardedPortLocal.cs b/src/Renci.SshNet/ForwardedPortLocal.cs index 9bf5c0583..b2ad4d75e 100644 --- a/src/Renci.SshNet/ForwardedPortLocal.cs +++ b/src/Renci.SshNet/ForwardedPortLocal.cs @@ -187,7 +187,7 @@ protected override void Dispose(bool disposing) private void InternalStart() { - var addr = Dns.GetHostAddresses(BoundHost)[0]; + var addr = IPAddress.TryParse(BoundHost, out var parsed) ? parsed : Dns.GetHostAddresses(BoundHost)[0]; var ep = new IPEndPoint(addr, (int)BoundPort); _listener = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; diff --git a/src/Renci.SshNet/ForwardedPortRemote.cs b/src/Renci.SshNet/ForwardedPortRemote.cs index 3d9393dd2..644c56d03 100644 --- a/src/Renci.SshNet/ForwardedPortRemote.cs +++ b/src/Renci.SshNet/ForwardedPortRemote.cs @@ -120,7 +120,7 @@ public ForwardedPortRemote(uint boundPort, string host, uint port) /// The host. /// The port. public ForwardedPortRemote(string boundHost, uint boundPort, string host, uint port) - : this(Dns.GetHostAddresses(boundHost)[0], + : this(IPAddress.TryParse(boundHost, out var parsedBoundHost) ? parsedBoundHost : Dns.GetHostAddresses(boundHost)[0], boundPort, Dns.GetHostAddresses(host)[0], port) diff --git a/test/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Start_WildcardBoundHost.cs b/test/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Start_WildcardBoundHost.cs new file mode 100644 index 000000000..1af806679 --- /dev/null +++ b/test/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Start_WildcardBoundHost.cs @@ -0,0 +1,44 @@ +using System; +using System.Net; + +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Moq; + +namespace Renci.SshNet.Tests.Classes +{ + /// + /// Binding a local forward to a wildcard address (0.0.0.0) used to fail because the bound host was + /// passed to , which rejects 0.0.0.0 and :: as input. + /// + [TestClass] + public class ForwardedPortLocalTest_Start_WildcardBoundHost + { + [TestMethod] + public void StartShouldListenOnAllIpv4Interfaces() + { + var connectionInfoMock = new Mock(MockBehavior.Strict); + var sessionMock = new Mock(MockBehavior.Strict); + + _ = sessionMock.Setup(p => p.SessionLoggerFactory) + .Returns(NullLoggerFactory.Instance); + _ = sessionMock.Setup(p => p.IsConnected) + .Returns(true); + _ = sessionMock.Setup(p => p.ConnectionInfo) + .Returns(connectionInfoMock.Object); + _ = connectionInfoMock.Setup(p => p.Timeout) + .Returns(TimeSpan.FromSeconds(15)); + + using (var forwardedPort = new ForwardedPortLocal("0.0.0.0", 0, "127.0.0.1", 80)) + { + forwardedPort.Session = sessionMock.Object; + + forwardedPort.Start(); + + Assert.IsTrue(forwardedPort.IsStarted); + Assert.AreNotEqual(0u, forwardedPort.BoundPort); + } + } + } +}