Skip to content
Merged
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,7 +31,7 @@
<!--
Disable nullable warnings on old frameworks because of missing annotations.
-->
<PropertyGroup Condition=" !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0')) ">
<PropertyGroup Condition=" !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0')) ">
Comment thread
mus65 marked this conversation as resolved.
<NoWarn>$(NoWarn);CS8602;CS8604;CS8777</NoWarn>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,7 +181,7 @@ Private keys in PuTTY private key format can be encrypted using the following ci
**SSH.NET** supports the following target frameworks:
* .NETFramework 4.6.2 (and higher)
* .NET Standard 2.0 and 2.1
* .NET 6 (and higher)
* .NET 8 (and higher)

## Building the library

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
#if !NET
using System.Threading;
using System.Threading.Tasks;

namespace Renci.SshNet.Abstractions
{
internal static class CancellationTokenSourceExtensions
{
#if !NET8_OR_GREATER
public static Task CancelAsync(this CancellationTokenSource cancellationTokenSource)
{
cancellationTokenSource.Cancel();
return Task.CompletedTask;
}
#endif
}
}
#endif
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Abstractions/SocketAbstraction.Async.cs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET

using System;
using System.Diagnostics;
Expand DownExpand Up@@ -47,4 +47,4 @@ static async ValueTask SendAsyncCore(Socket socket, ReadOnlyMemory<byte> data, C
}
}
}
#endif // NET6_0_OR_GREATER
#endif // NET
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Abstractions/SocketAbstraction.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -311,7 +311,7 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
return totalBytesRead;
}

#if NET6_0_OR_GREATER == false
#if !NET
public static Task<int> ReadAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken)
{
return socket.ReceiveAsync(buffer, 0, buffer.Length, cancellationToken);
Expand Down
10 changes: 5 additions & 5 deletions src/Renci.SshNet/Abstractions/SocketExtensions.cs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
#if !NET6_0_OR_GREATER
#if !NET
using System;
using System.Net;
using System.Net.Sockets;
Expand DownExpand Up@@ -93,11 +93,11 @@ public static async Task ConnectAsync(this Socket socket, EndPoint remoteEndpoin
{
args.RemoteEndPoint = remoteEndpoint;

#if NET || NETSTANDARD2_1_OR_GREATER
#if NETSTANDARD2_1
await using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs)o).SetCancelled(), args, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false))
#else
using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs)o).SetCancelled(), args, useSynchronizationContext: false))
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
{
await args.ExecuteAsync(socket.ConnectAsync);
}
Expand All@@ -112,11 +112,11 @@ public static async Task<int> ReceiveAsync(this Socket socket, byte[] buffer, in
{
args.SetBuffer(buffer, offset, length);

#if NET || NETSTANDARD2_1_OR_GREATER
#if NETSTANDARD2_1
await using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs)o).SetCancelled(), args, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false))
#else
using (cancellationToken.Register(o => ((AwaitableSocketAsyncEventArgs)o).SetCancelled(), args, useSynchronizationContext: false))
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
{
await args.ExecuteAsync(socket.ReceiveAsync);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Renci.SshNet/Abstractions/StreamExtensions.cs
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
#if !NET && !NETSTANDARD2_1_OR_GREATER
#if NETFRAMEWORK || NETSTANDARD2_0
using System.IO;
using System.Threading.Tasks;
#endif

namespace Renci.SshNet.Abstractions
{
internal static class StreamExtensions
{
#if !NET && !NETSTANDARD2_1_OR_GREATER
public static ValueTask DisposeAsync(this Stream stream)
{
stream.Dispose();
return default;
}
#endif
}
}
#endif
4 changes: 2 additions & 2 deletions src/Renci.SshNet/ClientAuthentication.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -105,11 +105,11 @@ private bool TryAuthenticate(ISession session,
{
authenticationException = new SshAuthenticationException(string.Format(CultureInfo.InvariantCulture,
"No suitable authentication method found to complete authentication ({0}).",
#if NET || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1
string.Join(',', allowedAuthenticationMethods)))
#else
string.Join(",", allowedAuthenticationMethods)))
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
;
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Renci.SshNet/Common/Extensions.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,7 +48,7 @@ internal static ServiceName ToServiceName(this byte[] data)

internal static BigInteger ToBigInteger(this byte[] data)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true);
#else
var reversed = new byte[data.Length];
Expand All@@ -62,7 +62,7 @@ internal static BigInteger ToBigInteger(this byte[] data)
/// </summary>
public static BigInteger ToBigInteger2(this byte[] data)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true, isUnsigned: true);
#else
if ((data[0] & (1 << 7)) != 0)
Expand DownExpand Up@@ -95,7 +95,7 @@ public static byte[] ToByteArray(this BigInteger bigInt, bool isUnsigned = false
}
#endif

#if !NET6_0_OR_GREATER
#if !NET
public static long GetBitLength(this BigInteger bigint)
{
// Taken from https://github.com/dotnet/runtime/issues/31308
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Common/SshData.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -372,11 +372,11 @@ protected void Write(BigInteger data)
/// <param name="data">name-list data to write.</param>
protected void Write(string[] data)
{
#if NET || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1
Write(string.Join(',', data), Ascii);
#else
Write(string.Join(",", data), Ascii);
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Common/SshDataStream.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,7 +139,7 @@ public void Write(string s, Encoding encoding)
{
ThrowHelper.ThrowIfNull(encoding);

#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
ReadOnlySpan<char> value = s;
var count = encoding.GetByteCount(value);
var bytes = count <= 256 ? stackalloc byte[count] : new byte[count];
Expand DownExpand Up@@ -207,7 +207,7 @@ public BigInteger ReadBigInt()
{
var data = ReadBinary();

#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
return new BigInteger(data, isBigEndian: true);
#else
return new BigInteger(data.Reverse());
Expand Down
10 changes: 5 additions & 5 deletions src/Renci.SshNet/Common/TaskToAsyncResult.cs
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
#pragma warning disable
#if !NET8_0_OR_GREATER
// Copied verbatim from https://github.com/dotnet/runtime/blob/78bd7debe6d8b454294c673c9cb969c6b8a14692/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs
#if !NET
// Copied verbatim from https://github.com/dotnet/runtime/blob/261611930d6b436d7c4395450356b624d903d9bf/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
Expand DownExpand Up@@ -35,7 +35,7 @@ static class TaskToAsyncResult
/// </remarks>
public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? state)
{
#if NET6_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
Expand DownExpand Up@@ -72,7 +72,7 @@ public static TResult End<TResult>(IAsyncResult asyncResult) =>
/// <exception cref="ArgumentException"><paramref name="asyncResult"/> was not produced by a call to <see cref="Begin"/>.</exception>
public static Task Unwrap(IAsyncResult asyncResult)
{
#if NET6_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(asyncResult);
#else
if (asyncResult is null)
Expand DownExpand Up@@ -101,7 +101,7 @@ public static Task Unwrap(IAsyncResult asyncResult)
/// </exception>
public static Task<TResult> Unwrap<TResult>(IAsyncResult asyncResult)
{
#if NET6_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(asyncResult);
#else
if (asyncResult is null)
Expand Down
8 changes: 4 additions & 4 deletions src/Renci.SshNet/Common/ThrowHelper.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,7 @@ internal static class ThrowHelper
{
public static void ThrowObjectDisposedIf(bool condition, object instance)
{
#if NET7_0_OR_GREATER
#if NET
ObjectDisposedException.ThrowIf(condition, instance);
#else
if (condition)
Expand All@@ -26,7 +26,7 @@ static void Throw(object? instance)

public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET6_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(argument, paramName);
#else
if (argument is null)
Expand All@@ -44,7 +44,7 @@ static void Throw(string? paramName)

public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET8_0_OR_GREATER
#if NET
ArgumentException.ThrowIfNullOrWhiteSpace(argument, paramName);
#else
if (string.IsNullOrWhiteSpace(argument))
Expand All@@ -63,7 +63,7 @@ static void Throw(string? argument, string? paramName)

public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET7_0_OR_GREATER
#if NET
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
#else
if (string.IsNullOrEmpty(argument))
Expand Down
8 changes: 4 additions & 4 deletions src/Renci.SshNet/Compression/Zlib.cs
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
using System.IO;
#if NET6_0_OR_GREATER
#if NET
using System.IO.Compression;
#else
using Org.BouncyCastle.Utilities.Zlib;
Expand All@@ -14,7 +14,7 @@ namespace Renci.SshNet.Compression
public class Zlib : Compressor
#pragma warning restore CA1724 // Type names should not match namespaces
{
#if NET6_0_OR_GREATER
#if NET
private readonly ZLibStream _compressor;
private readonly ZLibStream _decompressor;
#else
Expand DownExpand Up@@ -45,7 +45,7 @@ protected Zlib(bool delayedCompression)
_compressorStream = new MemoryStream();
_decompressorStream = new MemoryStream();

#if NET6_0_OR_GREATER
#if NET
_compressor = new ZLibStream(_compressorStream, CompressionMode.Compress);
_decompressor = new ZLibStream(_decompressorStream, CompressionMode.Decompress);
#else
Expand DownExpand Up@@ -74,7 +74,7 @@ protected override byte[] CompressCore(byte[] data, int offset, int length)
/// <inheritdoc/>
protected override byte[] DecompressCore(byte[] data, int offset, int length)
{
#if NET6_0_OR_GREATER
#if NET
_decompressorStream.Write(data, offset, length);
_decompressorStream.Position = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Connection/HttpConnector.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@ internal sealed partial class HttpConnector : ProxyConnector
private const string HttpResponsePattern = @"HTTP/(?<version>\d[.]\d) (?<statusCode>\d{3}) (?<reasonPhrase>.+)$";
private const string HttpHeaderPattern = @"(?<fieldName>[^\[\]()<>@,;:\""/?={} \t]+):(?<fieldValue>.+)?";

#if NET7_0_OR_GREATER
#if NET
private static readonly Regex HttpResponseRegex = GetHttpResponseRegex();
private static readonly Regex HttpHeaderRegex = GetHttpHeaderRegex();

Expand Down
6 changes: 3 additions & 3 deletions src/Renci.SshNet/Connection/ProtocolVersionExchange.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ internal sealed partial class ProtocolVersionExchange : IProtocolVersionExchange
private const byte Null = 0x00;
private const string ServerVersionPattern = "^SSH-(?<protoversion>[^-]+)-(?<softwareversion>.*?)([ ](?<comments>.+))?$";

#if NET7_0_OR_GREATER
#if NET
private static readonly Regex ServerVersionRegex = GetServerVersionRegex();

[GeneratedRegex(ServerVersionPattern, RegexOptions.ExplicitCapture)]
Expand DownExpand Up@@ -89,11 +89,11 @@ public async Task<SshIdentification> StartAsync(string clientVersion, Socket soc
{
// Immediately send the identification string since the spec states both sides MUST send an identification string
// when the connection has been established
#if NET6_0_OR_GREATER
#if NET
await SocketAbstraction.SendAsync(socket, Encoding.UTF8.GetBytes(clientVersion + "\x0D\x0A"), cancellationToken).ConfigureAwait(false);
#else
SocketAbstraction.Send(socket, Encoding.UTF8.GetBytes(clientVersion + "\x0D\x0A"));
#endif // NET6_0_OR_GREATER
#endif // NET

var bytesReceived = new List<byte>();

Expand Down
12 changes: 6 additions & 6 deletions src/Renci.SshNet/Connection/ProxyConnector.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,27 +21,27 @@ protected ProxyConnector(ISocketFactory socketFactory)

// ToDo: Performs async/sync fallback, true async version should be implemented in derived classes
protected virtual
#if NET || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1
async
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
Task HandleProxyConnectAsync(IConnectionInfo connectionInfo, Socket socket, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

#if NET || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1
await using (cancellationToken.Register(o => ((Socket)o).Dispose(), socket, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false))
#else
using (cancellationToken.Register(o => ((Socket)o).Dispose(), socket, useSynchronizationContext: false))
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
{
#pragma warning disable MA0042 // Do not use blocking calls in an async method; false positive caused by https://github.com/meziantou/Meziantou.Analyzer/issues/613
HandleProxyConnect(connectionInfo, socket);
#pragma warning restore MA0042 // Do not use blocking calls in an async method
}

#if !NET && !NETSTANDARD2_1_OR_GREATER
#if !NET && !NETSTANDARD2_1
return Task.CompletedTask;
#endif // !NET && !NETSTANDARD2_1_OR_GREATER
#endif
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Messages/Authentication/FailureMessage.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,11 +55,11 @@ protected override void LoadData()
PartialSuccess = ReadBoolean();
if (PartialSuccess)
{
#if NET || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1
Message = string.Join(',', AllowedAuthentications);
#else
Message = string.Join(",", AllowedAuthentications);
#endif // NET || NETSTANDARD2_1_OR_GREATER
#endif
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Renci.SshNet/Netconf/NetConfSession.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ internal sealed partial class NetConfSession : SubsystemSession, INetConfSession
private StringBuilder _rpcReply = new StringBuilder();
private int _messageId;

#if NET7_0_OR_GREATER
#if NET
private static readonly Regex LengthRegex = GetLengthRegex();
private static readonly Regex ReplyRegex = GetReplyRegex();

Expand DownExpand Up@@ -181,11 +181,11 @@ protected override void OnDataReceived(byte[] data)
position += match.Index + match.Length + fractionLength;
}

#if NET7_0_OR_GREATER
#if NET
if (ReplyRegex.IsMatch(chunk.AsSpan(position)))
#else
if (ReplyRegex.IsMatch(chunk.Substring(position)))
#endif // NET7_0_OR_GREATER
#endif // NET
{
_ = _rpcReplyReceived.Set();
}
Expand Down
Loading