Skip to content

Commit 13a6b5d

Browse files
authored
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
1 parent 765da93 commit 13a6b5d

File tree

9 files changed

+16
-136
lines changed

9 files changed

+16
-136
lines changed

src/Renci.SshNet/Abstractions/DnsAbstraction.cs

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static async Task ConnectAsync(Socket socket, IPEndPoint remoteEndpoint,
5959

6060
private static void ConnectCore(Socket socket, IPEndPoint remoteEndpoint, TimeSpan connectTimeout, bool ownsSocket)
6161
{
62-
#if FEATURE_SOCKET_EAP
6362
var connectCompleted = new ManualResetEvent(initialState: false);
6463
var args = new SocketAsyncEventArgs
6564
{
@@ -113,19 +112,6 @@ private static void ConnectCore(Socket socket, IPEndPoint remoteEndpoint, TimeSp
113112

114113
// dispose SocketAsyncEventArgs
115114
args.Dispose();
116-
#elif FEATURE_SOCKET_APM
117-
var connectResult = socket.BeginConnect(remoteEndpoint, null, null);
118-
if (!connectResult.AsyncWaitHandle.WaitOne(connectTimeout, false))
119-
throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
120-
"Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds));
121-
socket.EndConnect(connectResult);
122-
#elif FEATURE_SOCKET_TAP
123-
if (!socket.ConnectAsync(remoteEndpoint).Wait(connectTimeout))
124-
throw new SshOperationTimeoutException(string.Format(CultureInfo.InvariantCulture,
125-
"Connection failed to establish within {0:F0} milliseconds.", connectTimeout.TotalMilliseconds));
126-
#else
127-
#error Connecting to a remote endpoint is not implemented.
128-
#endif
129115
}
130116

131117
public static void ClearReadBuffer(Socket socket)
@@ -391,12 +377,10 @@ public static bool IsErrorResumable(SocketError socketError)
391377
#pragma warning restore IDE0010 // Add missing cases
392378
}
393379

394-
#if FEATURE_SOCKET_EAP
395380
private static void ConnectCompleted(object sender, SocketAsyncEventArgs e)
396381
{
397382
var eventWaitHandle = (ManualResetEvent) e.UserToken;
398383
_ = eventWaitHandle?.Set();
399384
}
400-
#endif // FEATURE_SOCKET_EAP
401385
}
402386
}

src/Renci.SshNet/Connection/ConnectorBase.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected ConnectorBase(ISocketFactory socketFactory)
3838
/// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
3939
protected Socket SocketConnect(string host, int port, TimeSpan timeout)
4040
{
41-
var ipAddress = DnsAbstraction.GetHostAddresses(host)[0];
41+
var ipAddress = Dns.GetHostAddresses(host)[0];
4242
var ep = new IPEndPoint(ipAddress, port);
4343

4444
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));
@@ -73,7 +73,12 @@ protected async Task<Socket> SocketConnectAsync(string host, int port, Cancellat
7373
{
7474
cancellationToken.ThrowIfCancellationRequested();
7575

76-
var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host).ConfigureAwait(false))[0];
76+
#if NET6_0_OR_GREATER
77+
var ipAddress = (await Dns.GetHostAddressesAsync(host, cancellationToken).ConfigureAwait(false))[0];
78+
#else
79+
var ipAddress = (await Dns.GetHostAddressesAsync(host).ConfigureAwait(false))[0];
80+
#endif
81+
7782
var ep = new IPEndPoint(ipAddress, port);
7883

7984
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));

src/Renci.SshNet/Connection/Socks4Connector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net;
23
using System.Net.Sockets;
34
using System.Text;
45

@@ -108,7 +109,7 @@ private static byte[] CreateSocks4ConnectionRequest(string hostname, ushort port
108109

109110
private static byte[] GetSocks4DestinationAddress(string hostname)
110111
{
111-
var addresses = DnsAbstraction.GetHostAddresses(hostname);
112+
var addresses = Dns.GetHostAddresses(hostname);
112113

113114
for (var i = 0; i < addresses.Length; i++)
114115
{

src/Renci.SshNet/Connection/Socks5Connector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net;
23
using System.Net.Sockets;
34

45
using Renci.SshNet.Abstractions;
@@ -243,7 +244,7 @@ private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port
243244

244245
private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)
245246
{
246-
var ip = DnsAbstraction.GetHostAddresses(hostname)[0];
247+
var ip = Dns.GetHostAddresses(hostname)[0];
247248

248249
byte[] address;
249250

src/Renci.SshNet/ForwardedPortDynamic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void InternalStart()
159159
var ip = IPAddress.Any;
160160
if (!string.IsNullOrEmpty(BoundHost))
161161
{
162-
ip = DnsAbstraction.GetHostAddresses(BoundHost)[0];
162+
ip = Dns.GetHostAddresses(BoundHost)[0];
163163
}
164164

165165
var ep = new IPEndPoint(ip, (int) BoundPort);

src/Renci.SshNet/ForwardedPortLocal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected override void Dispose(bool disposing)
198198

199199
private void InternalStart()
200200
{
201-
var addr = DnsAbstraction.GetHostAddresses(BoundHost)[0];
201+
var addr = Dns.GetHostAddresses(BoundHost)[0];
202202
var ep = new IPEndPoint(addr, (int) BoundPort);
203203

204204
_listener = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };

src/Renci.SshNet/ForwardedPortRemote.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ public ForwardedPortRemote(uint boundPort, string host, uint port)
125125
/// <param name="host">The host.</param>
126126
/// <param name="port">The port.</param>
127127
public ForwardedPortRemote(string boundHost, uint boundPort, string host, uint port)
128-
: this(DnsAbstraction.GetHostAddresses(boundHost)[0],
128+
: this(Dns.GetHostAddresses(boundHost)[0],
129129
boundPort,
130-
DnsAbstraction.GetHostAddresses(host)[0],
130+
Dns.GetHostAddresses(host)[0],
131131
port)
132132
{
133133
}

src/Renci.SshNet/Renci.SshNet.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
9-
<DefineConstants>$(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_SOCKET_EAP;FEATURE_SOCKET_APM;FEATURE_DNS_SYNC;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160</DefineConstants>
9+
<DefineConstants>$(DefineConstants);FEATURE_BINARY_SERIALIZATION;FEATURE_HASH_RIPEMD160_CREATE;FEATURE_HMAC_RIPEMD160</DefineConstants>
1010
</PropertyGroup>
1111

1212
<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 @@
1616
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
1717
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
1818
</ItemGroup>
19-
20-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">
21-
<DefineConstants>$(DefineConstants);FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
22-
</PropertyGroup>
2319
</Project>

0 commit comments

Comments
 (0)