Skip to content

Commit

Permalink
Add ClientIP to client info to allow select client IP that should be …
Browse files Browse the repository at this point in the history
…used for connection (#132)
  • Loading branch information
olegkap committed Dec 20, 2016
1 parent 5b724e5 commit 78679a3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
70 changes: 70 additions & 0 deletions src/Renci.SshNet/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Renci.SshNet.Messages.Authentication;
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
using Renci.SshNet.Security.Cryptography.Ciphers;
using System.Net;

namespace Renci.SshNet
{
Expand Down Expand Up @@ -205,6 +206,14 @@ public class ConnectionInfo : IConnectionInfoInternal
/// </summary>
public string ClientVersion { get; internal set; }

/// <summary>
/// Gets the client ip to use for connection when client has multiple IP addresses availble.
/// </summary>
/// <value>
/// The client ip to use for connection.
/// </value>
public IPAddress ClientIP { get; internal set; }

/// <summary>
/// Gets the current client compression algorithm.
/// </summary>
Expand Down Expand Up @@ -263,6 +272,66 @@ public ConnectionInfo(string host, int port, string username, params Authenticat
/// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
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)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="username">The username.</param>
/// <param name="clientIP">The client IP to be used.</param>
/// <param name="authenticationMethods">The authentication methods.</param>
/// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="host" /> is a zero-length string.</exception>
/// <exception cref="ArgumentException"><paramref name="host" /> is a zero-length string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="host" /> is a zero-length string.</exception>
public ConnectionInfo(string host, string username, IPAddress clientIP, params AuthenticationMethod[] authenticationMethods)
: this(host, DefaultPort, username, clientIP, ProxyTypes.None, null, 0, null, null, authenticationMethods)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="port">The port.</param>
/// <param name="username">The username.</param>
/// <param name="clientIP">The client IP to be used.</param>
/// <param name="authenticationMethods">The authentication methods.</param>
/// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
/// <exception cref="ArgumentNullException"><paramref name="host" /> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
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)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
/// </summary>
/// <param name="host">Connection host.</param>
/// <param name="port">Connection port.</param>
/// <param name="username">Connection username.</param>
/// <param name="clientIP">The client IP to be used.</param>
/// <param name="proxyType">Type of the proxy.</param>
/// <param name="proxyHost">The proxy host.</param>
/// <param name="proxyPort">The proxy port.</param>
/// <param name="proxyUsername">The proxy username.</param>
/// <param name="proxyPassword">The proxy password.</param>
/// <param name="authenticationMethods">The authentication methods.</param>
/// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
/// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyHost" /> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyPort" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
/// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
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");
Expand Down Expand Up @@ -386,6 +455,7 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy
Host = host;
Port = port;
Username = username;
ClientIP = clientIP;

ProxyType = proxyType;
ProxyHost = proxyHost;
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ internal static string ToHex(byte[] bytes)
/// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
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));
Expand Down

0 comments on commit 78679a3

Please sign in to comment.