From 630ec49e020f82ab1e39daa65838970c84c63013 Mon Sep 17 00:00:00 2001 From: Marius Thesing Date: Sat, 6 Jan 2024 11:44:44 +0100 Subject: [PATCH 1/3] remove unneeded feature flags FEATURE_SOCKET_EAP and FEATURE_DNS_SYNC were set for all frameworks, so unneeded. The other feature flags were only used in elif of FEATURE_SOCKET_EAP and FEATURE_DNS_SYNC, so also unneeded. --- .../Abstractions/DnsAbstraction.cs | 66 +------------------ .../Abstractions/SocketAbstraction.cs | 16 ----- src/Renci.SshNet/Renci.SshNet.csproj | 6 +- 3 files changed, 2 insertions(+), 86 deletions(-) diff --git a/src/Renci.SshNet/Abstractions/DnsAbstraction.cs b/src/Renci.SshNet/Abstractions/DnsAbstraction.cs index 30a10fe87..2ebfa34b1 100644 --- a/src/Renci.SshNet/Abstractions/DnsAbstraction.cs +++ b/src/Renci.SshNet/Abstractions/DnsAbstraction.cs @@ -1,22 +1,8 @@ using System; using System.Net; using System.Net.Sockets; -using System.Threading.Tasks; - -#if FEATURE_DNS_SYNC -#elif FEATURE_DNS_APM -using Renci.SshNet.Common; -#elif FEATURE_DNS_TAP -#elif FEATURE_DEVICEINFORMATION_APM -using System.Collections.Generic; -using System.Linq; using System.Threading; -using Microsoft.Phone.Net.NetworkInformation; -#elif FEATURE_DATAGRAMSOCKET -using System.Collections.Generic; -using Windows.Networking; -using Windows.Networking.Sockets; -#endif +using System.Threading.Tasks; namespace Renci.SshNet.Abstractions { @@ -36,57 +22,7 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress) { /* TODO Eliminate sync variant, and implement timeout */ -#if FEATURE_DNS_SYNC return Dns.GetHostAddresses(hostNameOrAddress); -#elif FEATURE_DNS_APM - var asyncResult = Dns.BeginGetHostAddresses(hostNameOrAddress, null, null); - if (!asyncResult.AsyncWaitHandle.WaitOne(Session.InfiniteTimeSpan)) - throw new SshOperationTimeoutException("Timeout resolving host name."); - return Dns.EndGetHostAddresses(asyncResult); -#elif FEATURE_DNS_TAP - return Dns.GetHostAddressesAsync(hostNameOrAddress).GetAwaiter().GetResult(); -#else - IPAddress address; - if (IPAddress.TryParse(hostNameOrAddress, out address)) - return new [] { address}; - -#if FEATURE_DEVICEINFORMATION_APM - var resolveCompleted = new ManualResetEvent(false); - NameResolutionResult nameResolutionResult = null; - DeviceNetworkInformation.ResolveHostNameAsync(new DnsEndPoint(hostNameOrAddress, 0), result => - { - nameResolutionResult = result; - resolveCompleted.Set(); - }, null); - - // wait until address is resolved - resolveCompleted.WaitOne(); - - if (nameResolutionResult.NetworkErrorCode == NetworkError.Success) - { - var addresses = new List(nameResolutionResult.IPEndPoints.Select(p => p.Address).Distinct()); - return addresses.ToArray(); - } - throw new SocketException((int)nameResolutionResult.NetworkErrorCode); -#elif FEATURE_DATAGRAMSOCKET - - // TODO we may need to only return those IP addresses that are supported on the current system - // TODO http://wojciechkulik.pl/csharp/winrt-how-to-detect-supported-ip-versions - - var endpointPairs = DatagramSocket.GetEndpointPairsAsync(new HostName(hostNameOrAddress), "").GetAwaiter().GetResult(); - var addresses = new List(); - foreach (var endpointPair in endpointPairs) - { - if (endpointPair.RemoteHostName.Type == HostNameType.Ipv4 || endpointPair.RemoteHostName.Type == HostNameType.Ipv6) - addresses.Add(IPAddress.Parse(endpointPair.RemoteHostName.CanonicalName)); - } - if (addresses.Count == 0) - throw new SocketException((int) System.Net.Sockets.SocketError.HostNotFound); - return addresses.ToArray(); -#else - throw new NotSupportedException("Resolving hostname to IP address is not implemented."); -#endif // FEATURE_DEVICEINFORMATION_APM -#endif } /// diff --git a/src/Renci.SshNet/Abstractions/SocketAbstraction.cs b/src/Renci.SshNet/Abstractions/SocketAbstraction.cs index f5f840336..b44de4163 100644 --- a/src/Renci.SshNet/Abstractions/SocketAbstraction.cs +++ b/src/Renci.SshNet/Abstractions/SocketAbstraction.cs @@ -59,7 +59,6 @@ public static async Task ConnectAsync(Socket socket, IPEndPoint remoteEndpoint, private static void ConnectCore(Socket socket, IPEndPoint remoteEndpoint, TimeSpan connectTimeout, bool ownsSocket) { -#if FEATURE_SOCKET_EAP var connectCompleted = new ManualResetEvent(initialState: false); var args = new SocketAsyncEventArgs { @@ -113,19 +112,6 @@ private static void ConnectCore(Socket socket, IPEndPoint remoteEndpoint, TimeSp // dispose SocketAsyncEventArgs args.Dispose(); -#elif FEATURE_SOCKET_APM - var connectResult = socket.BeginConnect(remoteEndpoint, null, null); - if (!connectResult.AsyncWaitHandle.WaitOne(connectTimeout, false)) - throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture, - "Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds)); - socket.EndConnect(connectResult); -#elif FEATURE_SOCKET_TAP - if (!socket.ConnectAsync(remoteEndpoint).Wait(connectTimeout)) - throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture, - "Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds)); -#else - #error Connecting to a remote endpoint is not implemented. -#endif } public static void ClearReadBuffer(Socket socket) @@ -391,12 +377,10 @@ public static bool IsErrorResumable(SocketError socketError) #pragma warning restore IDE0010 // Add missing cases } -#if FEATURE_SOCKET_EAP private static void ConnectCompleted(object sender, SocketAsyncEventArgs e) { var eventWaitHandle = (ManualResetEvent) e.UserToken; _ = eventWaitHandle?.Set(); } -#endif // FEATURE_SOCKET_EAP } } diff --git a/src/Renci.SshNet/Renci.SshNet.csproj b/src/Renci.SshNet/Renci.SshNet.csproj index 334df5083..adda89ad9 100644 --- a/src/Renci.SshNet/Renci.SshNet.csproj +++ b/src/Renci.SshNet/Renci.SshNet.csproj @@ -6,7 +6,7 @@ - $(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_SOCKET_EAP;FEATURE_SOCKET_APM;FEATURE_DNS_SYNC;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160 + $(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160 @@ -16,8 +16,4 @@ - - - $(DefineConstants);FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP - From 9d4a329e54ecb9b18975646f21019172eca3d99f Mon Sep 17 00:00:00 2001 From: Marius Thesing Date: Sat, 6 Jan 2024 11:44:59 +0100 Subject: [PATCH 2/3] pass cancellationToken to Dns.GetHostAddressesAsync --- src/Renci.SshNet/Abstractions/DnsAbstraction.cs | 7 ++++++- src/Renci.SshNet/Connection/ConnectorBase.cs | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Renci.SshNet/Abstractions/DnsAbstraction.cs b/src/Renci.SshNet/Abstractions/DnsAbstraction.cs index 2ebfa34b1..00868943d 100644 --- a/src/Renci.SshNet/Abstractions/DnsAbstraction.cs +++ b/src/Renci.SshNet/Abstractions/DnsAbstraction.cs @@ -29,15 +29,20 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress) /// Returns the Internet Protocol (IP) addresses for the specified host. /// /// The host name or IP address to resolve. + /// A cancellation token that can be used to signal the asynchronous operation should be canceled. /// /// A task with result of an array of type that holds the IP addresses for the host that /// is specified by the parameter. /// /// is . /// An error is encountered when resolving . - public static Task GetHostAddressesAsync(string hostNameOrAddress) + public static Task GetHostAddressesAsync(string hostNameOrAddress, CancellationToken cancellationToken) { +#if NET6_0_OR_GREATER + return Dns.GetHostAddressesAsync(hostNameOrAddress, cancellationToken); +#else return Dns.GetHostAddressesAsync(hostNameOrAddress); +#endif } } } diff --git a/src/Renci.SshNet/Connection/ConnectorBase.cs b/src/Renci.SshNet/Connection/ConnectorBase.cs index 58b45960a..3bd54444a 100644 --- a/src/Renci.SshNet/Connection/ConnectorBase.cs +++ b/src/Renci.SshNet/Connection/ConnectorBase.cs @@ -73,7 +73,7 @@ protected async Task SocketConnectAsync(string host, int port, Cancellat { cancellationToken.ThrowIfCancellationRequested(); - var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host).ConfigureAwait(false))[0]; + var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host, cancellationToken).ConfigureAwait(false))[0]; var ep = new IPEndPoint(ipAddress, port); DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port)); From 0cbcaf2f88b28305761928afa1568b5622828dd4 Mon Sep 17 00:00:00 2001 From: Marius Thesing Date: Sun, 7 Jan 2024 18:49:13 +0100 Subject: [PATCH 3/3] Removed DnsAbstractions --- .../Abstractions/DnsAbstraction.cs | 48 ------------------- src/Renci.SshNet/Connection/ConnectorBase.cs | 9 +++- .../Connection/Socks4Connector.cs | 3 +- .../Connection/Socks5Connector.cs | 3 +- src/Renci.SshNet/ForwardedPortDynamic.cs | 2 +- src/Renci.SshNet/ForwardedPortLocal.cs | 2 +- src/Renci.SshNet/ForwardedPortRemote.cs | 4 +- 7 files changed, 15 insertions(+), 56 deletions(-) delete mode 100644 src/Renci.SshNet/Abstractions/DnsAbstraction.cs diff --git a/src/Renci.SshNet/Abstractions/DnsAbstraction.cs b/src/Renci.SshNet/Abstractions/DnsAbstraction.cs deleted file mode 100644 index 00868943d..000000000 --- a/src/Renci.SshNet/Abstractions/DnsAbstraction.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Threading; -using System.Threading.Tasks; - -namespace Renci.SshNet.Abstractions -{ - internal static class DnsAbstraction - { - /// - /// Returns the Internet Protocol (IP) addresses for the specified host. - /// - /// The host name or IP address to resolve. - /// - /// An array of type that holds the IP addresses for the host that - /// is specified by the parameter. - /// - /// is . - /// An error is encountered when resolving . - public static IPAddress[] GetHostAddresses(string hostNameOrAddress) - { - /* TODO Eliminate sync variant, and implement timeout */ - - return Dns.GetHostAddresses(hostNameOrAddress); - } - - /// - /// Returns the Internet Protocol (IP) addresses for the specified host. - /// - /// The host name or IP address to resolve. - /// A cancellation token that can be used to signal the asynchronous operation should be canceled. - /// - /// A task with result of an array of type that holds the IP addresses for the host that - /// is specified by the parameter. - /// - /// is . - /// An error is encountered when resolving . - public static Task GetHostAddressesAsync(string hostNameOrAddress, CancellationToken cancellationToken) - { -#if NET6_0_OR_GREATER - return Dns.GetHostAddressesAsync(hostNameOrAddress, cancellationToken); -#else - return Dns.GetHostAddressesAsync(hostNameOrAddress); -#endif - } - } -} diff --git a/src/Renci.SshNet/Connection/ConnectorBase.cs b/src/Renci.SshNet/Connection/ConnectorBase.cs index 3bd54444a..384091b18 100644 --- a/src/Renci.SshNet/Connection/ConnectorBase.cs +++ b/src/Renci.SshNet/Connection/ConnectorBase.cs @@ -38,7 +38,7 @@ protected ConnectorBase(ISocketFactory socketFactory) /// An error occurred trying to establish the connection. protected Socket SocketConnect(string host, int port, TimeSpan timeout) { - var ipAddress = DnsAbstraction.GetHostAddresses(host)[0]; + var ipAddress = Dns.GetHostAddresses(host)[0]; var ep = new IPEndPoint(ipAddress, port); DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port)); @@ -73,7 +73,12 @@ protected async Task SocketConnectAsync(string host, int port, Cancellat { cancellationToken.ThrowIfCancellationRequested(); - var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host, cancellationToken).ConfigureAwait(false))[0]; +#if NET6_0_OR_GREATER + var ipAddress = (await Dns.GetHostAddressesAsync(host, cancellationToken).ConfigureAwait(false))[0]; +#else + var ipAddress = (await Dns.GetHostAddressesAsync(host).ConfigureAwait(false))[0]; +#endif + var ep = new IPEndPoint(ipAddress, port); DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port)); diff --git a/src/Renci.SshNet/Connection/Socks4Connector.cs b/src/Renci.SshNet/Connection/Socks4Connector.cs index d679c17a3..e3e9800f0 100644 --- a/src/Renci.SshNet/Connection/Socks4Connector.cs +++ b/src/Renci.SshNet/Connection/Socks4Connector.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Sockets; using System.Text; @@ -108,7 +109,7 @@ private static byte[] CreateSocks4ConnectionRequest(string hostname, ushort port private static byte[] GetSocks4DestinationAddress(string hostname) { - var addresses = DnsAbstraction.GetHostAddresses(hostname); + var addresses = Dns.GetHostAddresses(hostname); for (var i = 0; i < addresses.Length; i++) { diff --git a/src/Renci.SshNet/Connection/Socks5Connector.cs b/src/Renci.SshNet/Connection/Socks5Connector.cs index a1ed5f751..ecd286e00 100644 --- a/src/Renci.SshNet/Connection/Socks5Connector.cs +++ b/src/Renci.SshNet/Connection/Socks5Connector.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Sockets; using Renci.SshNet.Abstractions; @@ -243,7 +244,7 @@ private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType) { - var ip = DnsAbstraction.GetHostAddresses(hostname)[0]; + var ip = Dns.GetHostAddresses(hostname)[0]; byte[] address; diff --git a/src/Renci.SshNet/ForwardedPortDynamic.cs b/src/Renci.SshNet/ForwardedPortDynamic.cs index 21ab59a20..0c6e417a8 100644 --- a/src/Renci.SshNet/ForwardedPortDynamic.cs +++ b/src/Renci.SshNet/ForwardedPortDynamic.cs @@ -159,7 +159,7 @@ private void InternalStart() var ip = IPAddress.Any; if (!string.IsNullOrEmpty(BoundHost)) { - ip = DnsAbstraction.GetHostAddresses(BoundHost)[0]; + ip = 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 72bf08dc7..fce8f7fd7 100644 --- a/src/Renci.SshNet/ForwardedPortLocal.cs +++ b/src/Renci.SshNet/ForwardedPortLocal.cs @@ -198,7 +198,7 @@ protected override void Dispose(bool disposing) private void InternalStart() { - var addr = DnsAbstraction.GetHostAddresses(BoundHost)[0]; + var addr = 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 1b4a74eb4..10430abc3 100644 --- a/src/Renci.SshNet/ForwardedPortRemote.cs +++ b/src/Renci.SshNet/ForwardedPortRemote.cs @@ -125,9 +125,9 @@ public ForwardedPortRemote(uint boundPort, string host, uint port) /// The host. /// The port. public ForwardedPortRemote(string boundHost, uint boundPort, string host, uint port) - : this(DnsAbstraction.GetHostAddresses(boundHost)[0], + : this(Dns.GetHostAddresses(boundHost)[0], boundPort, - DnsAbstraction.GetHostAddresses(host)[0], + Dns.GetHostAddresses(host)[0], port) { }