Skip to content

Allow binding forwarded ports to a wildcard address (0.0.0.0 / ::) - #1876

Open
gluoncat wants to merge 1 commit into
sshnet:developfrom
b1ankcat:fix/port-bind
Open

gluoncat wants to merge 1 commit into
sshnet:developfrom
b1ankcat:fix/port-bind

Conversation

@gluoncat

Copy link
Copy Markdown

Summary

ForwardedPortLocal, ForwardedPortDynamic and ForwardedPortRemote resolve the bound host via
Dns.GetHostAddresses. .NET rejects the unspecified addresses for name resolution, so 0.0.0.0 / ::
can never be used as the bound host — even though Socket.Bind supports them:

System.ArgumentException: IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses
that cannot be used as a target address. (Parameter 'hostNameOrAddress')
   at System.Net.Dns.GetHostAddresses(String hostNameOrAddress, AddressFamily family)
   at Renci.SshNet.ForwardedPortLocal.InternalStart()

Reproduction (fails with the exception above):

using var port = new ForwardedPortLocal("0.0.0.0", 8080, "127.0.0.1", 80);
port.Start();

The only workaround today is to construct ForwardedPortRemote through its IPAddress overload
(ForwardedPortLocal and ForwardedPortDynamic have none) or to enumerate the local interface
addresses and start one forward per address.

Change

Parse the bound host as an IP literal first, and only fall back to name resolution for host names:

-ip = Dns.GetHostAddresses(BoundHost)[0];
+ip = IPAddress.TryParse(BoundHost, out var parsed) ? parsed : Dns.GetHostAddresses(BoundHost)[0];

Applied to:

  • ForwardedPortLocal.InternalStart()
  • ForwardedPortDynamic.InternalStart()
  • ForwardedPortRemote(string boundHost, uint boundPort, string host, uint port)

Host names still go through Dns.GetHostAddresses, and an empty bound host takes the previous code
path unchanged (IPAddress.TryParse("") returns false). No public API change; the ForwardedPortRemote
target host is untouched.

Tests

Added ForwardedPortLocalTest_Start_WildcardBoundHost: starts a ForwardedPortLocal with bound host
0.0.0.0 on an ephemeral port against a mocked session, and asserts the port is started and has a
non-zero bound port. It fails with the ArgumentException above before the change.

Notes

  • Also fixes the equivalent failure for ssh -R 0.0.0.0:port:host:port-style remote forwards.
  • Disclosure, per CONTRIBUTING.md: this change was drafted with AI assistance and reviewed by a human.

Fixes #525

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 sshnet#525

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot listen to port on ALL interfaces "0.0.0.0"

1 participant