Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 0 additions & 107 deletions src/Renci.SshNet/Abstractions/DnsAbstraction.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/Renci.SshNet/Abstractions/SocketAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
9 changes: 7 additions & 2 deletions src/Renci.SshNet/Connection/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected ConnectorBase(ISocketFactory socketFactory)
/// <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));
Expand Down Expand Up @@ -73,7 +73,12 @@ protected async Task<Socket> SocketConnectAsync(string host, int port, Cancellat
{
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));
Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/Connection/Socks4Connector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

Expand Down Expand Up @@ -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++)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/Connection/Socks5Connector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Sockets;

using Renci.SshNet.Abstractions;
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/ForwardedPortDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/ForwardedPortLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/ForwardedPortRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public ForwardedPortRemote(uint boundPort, string host, uint port)
/// <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)
{
}
Expand Down
6 changes: 1 addition & 5 deletions src/Renci.SshNet/Renci.SshNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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' ">
Expand All @@ -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>