Переглянути джерело

Remove unneeded feature flags and pass cancellationToken to DNS request (#1290)

* 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.

* pass cancellationToken to Dns.GetHostAddressesAsync

* Removed DnsAbstractions
mus65 1 рік тому
батько
коміт
13a6b5df0e

+ 0 - 107
src/Renci.SshNet/Abstractions/DnsAbstraction.cs

@@ -1,107 +0,0 @@
-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
-
-namespace Renci.SshNet.Abstractions
-{
-    internal static class DnsAbstraction
-    {
-        /// <summary>
-        /// Returns the Internet Protocol (IP) addresses for the specified host.
-        /// </summary>
-        /// <param name="hostNameOrAddress">The host name or IP address to resolve.</param>
-        /// <returns>
-        /// An array of type <see cref="IPAddress"/> that holds the IP addresses for the host that
-        /// is specified by the <paramref name="hostNameOrAddress"/> parameter.
-        /// </returns>
-        /// <exception cref="ArgumentNullException"><paramref name="hostNameOrAddress"/> is <see langword="null"/>.</exception>
-        /// <exception cref="SocketException">An error is encountered when resolving <paramref name="hostNameOrAddress"/>.</exception>
-        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<IPAddress>(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<IPAddress>();
-            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
-        }
-
-        /// <summary>
-        /// Returns the Internet Protocol (IP) addresses for the specified host.
-        /// </summary>
-        /// <param name="hostNameOrAddress">The host name or IP address to resolve.</param>
-        /// <returns>
-        /// A task with result of an array of type <see cref="IPAddress"/> that holds the IP addresses for the host that
-        /// is specified by the <paramref name="hostNameOrAddress"/> parameter.
-        /// </returns>
-        /// <exception cref="ArgumentNullException"><paramref name="hostNameOrAddress"/> is <see langword="null"/>.</exception>
-        /// <exception cref="SocketException">An error is encountered when resolving <paramref name="hostNameOrAddress"/>.</exception>
-        public static Task<IPAddress[]> GetHostAddressesAsync(string hostNameOrAddress)
-        {
-            return Dns.GetHostAddressesAsync(hostNameOrAddress);
-        }
-    }
-}

+ 0 - 16
src/Renci.SshNet/Abstractions/SocketAbstraction.cs

@@ -59,7 +59,6 @@ namespace Renci.SshNet.Abstractions
 
         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 @@ namespace Renci.SshNet.Abstractions
 
             // 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 @@ namespace Renci.SshNet.Abstractions
 #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
     }
 }

+ 7 - 2
src/Renci.SshNet/Connection/ConnectorBase.cs

@@ -38,7 +38,7 @@ namespace Renci.SshNet.Connection
         /// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
         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 @@ namespace Renci.SshNet.Connection
         {
             cancellationToken.ThrowIfCancellationRequested();
 
-            var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host).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));

+ 2 - 1
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 @@ namespace Renci.SshNet.Connection
 
         private static byte[] GetSocks4DestinationAddress(string hostname)
         {
-            var addresses = DnsAbstraction.GetHostAddresses(hostname);
+            var addresses = Dns.GetHostAddresses(hostname);
 
             for (var i = 0; i < addresses.Length; i++)
             {

+ 2 - 1
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 @@ namespace Renci.SshNet.Connection
 
         private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)
         {
-            var ip = DnsAbstraction.GetHostAddresses(hostname)[0];
+            var ip = Dns.GetHostAddresses(hostname)[0];
 
             byte[] address;
 

+ 1 - 1
src/Renci.SshNet/ForwardedPortDynamic.cs

@@ -159,7 +159,7 @@ namespace Renci.SshNet
             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);

+ 1 - 1
src/Renci.SshNet/ForwardedPortLocal.cs

@@ -198,7 +198,7 @@ namespace Renci.SshNet
 
         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 };

+ 2 - 2
src/Renci.SshNet/ForwardedPortRemote.cs

@@ -125,9 +125,9 @@ namespace Renci.SshNet
         /// <param name="host">The host.</param>
         /// <param name="port">The port.</param>
         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)
         {
         }

+ 1 - 5
src/Renci.SshNet/Renci.SshNet.csproj

@@ -6,7 +6,7 @@
   </PropertyGroup>
 
   <PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
-    <DefineConstants>$(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_SOCKET_EAP;FEATURE_SOCKET_APM;FEATURE_DNS_SYNC;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160</DefineConstants>
+    <DefineConstants>$(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160</DefineConstants>
   </PropertyGroup>
 
   <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">
@@ -16,8 +16,4 @@
   <ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
     <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
   </ItemGroup>
-
-  <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">
-    <DefineConstants>$(DefineConstants);FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
-  </PropertyGroup>
 </Project>