diff --git a/src/Renci.SshNet/ConnectionInfo.cs b/src/Renci.SshNet/ConnectionInfo.cs index ba8f38b9..6e13755a 100644 --- a/src/Renci.SshNet/ConnectionInfo.cs +++ b/src/Renci.SshNet/ConnectionInfo.cs @@ -9,6 +9,7 @@ using Renci.SshNet.Common; using Renci.SshNet.Messages.Authentication; using Renci.SshNet.Security.Cryptography.Ciphers.Modes; using Renci.SshNet.Security.Cryptography.Ciphers; +using System.Net; namespace Renci.SshNet { @@ -205,6 +206,14 @@ namespace Renci.SshNet /// public string ClientVersion { get; internal set; } + /// + /// Gets the client ip to use for connection when client has multiple IP addresses availble. + /// + /// + /// The client ip to use for connection. + /// + public IPAddress ClientIP { get; internal set; } + /// /// Gets the current client compression algorithm. /// @@ -263,6 +272,66 @@ namespace Renci.SshNet /// is null. /// No specified. public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods) + : this(host, DefaultPort, username, null, ProxyTypes.None, null, 0, null, null, authenticationMethods) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The host. + /// The username. + /// The client IP to be used. + /// The authentication methods. + /// is null. + /// is a zero-length string. + /// is a zero-length string. + /// is null. + /// is a zero-length string. + public ConnectionInfo(string host, string username, IPAddress clientIP, params AuthenticationMethod[] authenticationMethods) + : this(host, DefaultPort, username, clientIP, ProxyTypes.None, null, 0, null, null, authenticationMethods) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The host. + /// The port. + /// The username. + /// The client IP to be used. + /// The authentication methods. + /// is null. + /// is null, a zero-length string or contains only whitespace characters. + /// is not within and . + /// is null. + /// is null, a zero-length string or contains only whitespace characters. + public ConnectionInfo(string host, int port, string username, IPAddress clientIP, params AuthenticationMethod[] authenticationMethods) + : this(host, port, username, clientIP, ProxyTypes.None, null, 0, null, null, authenticationMethods) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Connection host. + /// Connection port. + /// Connection username. + /// The client IP to be used. + /// Type of the proxy. + /// The proxy host. + /// The proxy port. + /// The proxy username. + /// The proxy password. + /// The authentication methods. + /// is null. + /// is null, a zero-length string or contains only whitespace characters. + /// is not within and . + /// is not and is null. + /// is not and is not within and . + /// is null. + /// No specified. + public ConnectionInfo(string host, int port, string username, IPAddress clientIP, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods) { if (host == null) throw new ArgumentNullException("host"); @@ -386,6 +455,7 @@ namespace Renci.SshNet Host = host; Port = port; Username = username; + ClientIP = clientIP; ProxyType = proxyType; ProxyHost = proxyHost; diff --git a/src/Renci.SshNet/Session.cs b/src/Renci.SshNet/Session.cs index e2674309..0bebddfc 100644 --- a/src/Renci.SshNet/Session.cs +++ b/src/Renci.SshNet/Session.cs @@ -1600,7 +1600,7 @@ namespace Renci.SshNet /// An error occurred trying to establish the connection. private void SocketConnect(string host, int port) { - var ipAddress = DnsAbstraction.GetHostAddresses(host)[0]; + var ipAddress = ConnectionInfo.ClientIP ?? DnsAbstraction.GetHostAddresses(host)[0]; var ep = new IPEndPoint(ipAddress, port); DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));