Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -898,14 +898,16 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs">
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs">
<Link>Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs</Link>
</Compile>

<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs" />
</ItemGroup>

<!-- Unix only -->
Expand Down Expand Up @@ -934,14 +936,16 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.netcore.Unix.cs">
<Link>Microsoft\Data\SqlClinet\SqlColumnEncryptionCspProvider.netcore.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Unix.cs">
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlFileStream.netcore.Unix.cs">
<Link>Microsoft\Data\SqlTypes\SqlFileStream.netcore.Unix.cs</Link>
</Compile>

<Compile Include="Microsoft\Data\Sql\SqlDataSourceEnumerator.Unix.cs" />
<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Unix.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Unix.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Managed.cs" />
</ItemGroup>

<!-- Resources -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb
// Update SNI ConsumerInfo value to be resulting packet size
uint unsignedPacketSize = (uint)packetSize;
uint bufferSizeResult = _physicalStateObj.SetConnectionBufferSize(ref unsignedPacketSize);

Debug.Assert(bufferSizeResult == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SNISetInfo");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ internal TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalCon
////////////////
// Properties //
////////////////
internal abstract uint DisableSsl();

internal abstract SSPIContextProvider CreateSSPIContextProvider();

internal abstract uint EnableMars(ref uint info);

internal abstract uint Status
{
Expand Down Expand Up @@ -201,8 +196,6 @@ internal abstract void CreatePhysicalSNIHandle(

internal abstract void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo);

internal abstract uint SniGetConnectionId(ref Guid clientConnectionId);

internal abstract bool IsFailedHandle();

protected abstract void CreateSessionHandle(TdsParserStateObject physicalConnection, bool async);
Expand All @@ -225,8 +218,6 @@ internal abstract void CreatePhysicalSNIHandle(

internal abstract uint CheckConnection();

internal abstract uint SetConnectionBufferSize(ref uint unsignedPacketSize);

internal abstract void ReleasePacket(PacketHandle syncReadPacket);

protected abstract uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs">
<Link>Microsoft\Data\SqlClient\TdsParserStateObject.Multiplexer.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs">
<Link>Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Windows.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
</Compile>
Expand Down Expand Up @@ -924,6 +927,7 @@
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netfx.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
<Compile Include="Microsoft\Data\SqlTypes\SqlStreamChars.cs" />
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netfx.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal sealed partial class TdsParser
/// <summary>
/// Verify client encryption possibility.
/// </summary>
private bool ClientOSEncryptionSupport => SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
private bool ClientOSEncryptionSupport => TdsParserStateObjectFactory.Singleton.ClientOSEncryptionSupport;

// Default state object for parser
internal TdsParserStateObject _physicalStateObj = null; // Default stateObj and connection for Dbnetlib and non-MARS SNI.
Expand Down Expand Up @@ -87,7 +87,7 @@ internal sealed partial class TdsParser

internal Encoding _defaultEncoding = null; // for sql character data

private static EncryptionOptions s_sniSupportedEncryptionOption = SNILoadHandle.SingletonInstance.Options;
private static EncryptionOptions s_sniSupportedEncryptionOption = TdsParserStateObjectFactory.Singleton.EncryptionOptions;

private EncryptionOptions _encryptionOption = s_sniSupportedEncryptionOption;

Expand Down Expand Up @@ -195,7 +195,7 @@ internal TdsParser(bool MARS, bool fAsynchronous)
{
_fMARS = MARS; // may change during Connect to pre 2005 servers

_physicalStateObj = new TdsParserStateObject(this);
_physicalStateObj = TdsParserStateObjectFactory.Singleton.CreateTdsParserStateObject(this);
DataClassificationVersion = TdsEnums.DATA_CLASSIFICATION_NOT_ENABLED;
}

Expand Down Expand Up @@ -386,7 +386,7 @@ internal void Connect(ServerInfo serverInfo,
// Clean up IsSQLDNSCachingSupported flag from previous status
_connHandler.IsSQLDNSCachingSupported = false;

uint sniStatus = SNILoadHandle.SingletonInstance.Status;
uint sniStatus = TdsParserStateObjectFactory.Singleton.SNIStatus;

if (sniStatus != TdsEnums.SNI_SUCCESS)
{
Expand Down Expand Up @@ -555,7 +555,7 @@ internal void Connect(ServerInfo serverInfo,
_connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.InitializeConnection);
_connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.SendPreLoginHandshake);

uint result = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
uint result = _physicalStateObj.SniGetConnectionId(ref _connHandler._clientConnectionId);
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");

// for DNS Caching phase 1
Expand Down Expand Up @@ -621,7 +621,7 @@ internal void Connect(ServerInfo serverInfo,
ThrowExceptionAndWarning(_physicalStateObj);
}

uint retCode = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
uint retCode = _physicalStateObj.SniGetConnectionId(ref _connHandler._clientConnectionId);
Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Sending prelogin handshake");

Expand Down Expand Up @@ -664,10 +664,8 @@ internal void RemoveEncryption()
{
Debug.Assert((_encryptionOption & EncryptionOptions.OPTIONS_MASK) == EncryptionOptions.LOGIN, "Invalid encryption option state");

uint error = 0;
uint error = _physicalStateObj.DisableSsl();

// Remove SSL (Encryption) SNI provider since we only wanted to encrypt login.
error = SniNativeWrapper.SniRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV);
if (error != TdsEnums.SNI_SUCCESS)
{
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
Expand All @@ -690,11 +688,8 @@ internal void EnableMars()
// Cache physical stateObj and connection.
_pMarsPhysicalConObj = _physicalStateObj;

uint error = 0;
uint info = 0;

// Add SMUX (MARS) SNI provider.
error = SniNativeWrapper.SniAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info);
uint error = _pMarsPhysicalConObj.EnableMars(ref info);

if (error != TdsEnums.SNI_SUCCESS)
{
Expand Down Expand Up @@ -737,7 +732,7 @@ internal void EnableMars()

internal TdsParserStateObject CreateSession()
{
TdsParserStateObject session = new TdsParserStateObject(this, (SNIHandle)_pMarsPhysicalConObj.Handle, true);
TdsParserStateObject session = TdsParserStateObjectFactory.Singleton.CreateSessionObject(this, _pMarsPhysicalConObj, true);
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParser.CreateSession|ADV> {0} created session {1}", ObjectID, session.ObjectID);
return session;
}
Expand Down Expand Up @@ -2836,7 +2831,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb

// Update SNI ConsumerInfo value to be resulting packet size
uint unsignedPacketSize = (uint)packetSize;
uint bufferSizeResult = SniNativeWrapper.SniSetInfo(_physicalStateObj.Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
uint bufferSizeResult = _physicalStateObj.SetConnectionBufferSize(ref unsignedPacketSize);

Debug.Assert(bufferSizeResult == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SNISetInfo");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ internal readonly ref struct SessionHandle

internal partial class TdsParserStateObject
{
private static class TdsParserStateObjectFactory
{
/// <summary>
/// Always false in case of netfx. Only needed for merging with netcore codebase.
/// </summary>
internal const bool UseManagedSNI = false;
}

private SNIHandle _sessionHandle = null; // the SNI handle we're to work on

// SNI variables // multiple resultsets in one batch.
Expand All @@ -62,7 +54,7 @@ private static class TdsParserStateObjectFactory
// Constructors //
//////////////////

internal TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bool async)
protected TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bool async)
{
// Construct a MARS session
Debug.Assert(parser != null, "no parser?");
Expand Down Expand Up @@ -98,8 +90,6 @@ internal TdsParserStateObject(TdsParser parser, SNIHandle physicalConnection, bo
_lastSuccessfulIOTimer = parser._physicalStateObj._lastSuccessfulIOTimer;
}

internal SSPIContextProvider CreateSSPIContextProvider() => new NativeSSPIContextProvider();

////////////////
// Properties //
////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Interop.Windows.Sni;

namespace Microsoft.Data.SqlClient
{
internal class TdsParserStateObjectNative : TdsParserStateObject
{
internal TdsParserStateObjectNative(TdsParser parser, TdsParserStateObject physicalConnection, bool async)
: base(parser, physicalConnection.Handle, async)
{
}

internal TdsParserStateObjectNative(TdsParser parser)
: base(parser)
{
}

internal override uint SniGetConnectionId(ref Guid clientConnectionId)
=> SniNativeWrapper.SniGetConnectionId(Handle, ref clientConnectionId);

internal override uint DisableSsl()
=> SniNativeWrapper.SniRemoveProvider(Handle, Provider.SSL_PROV);

internal override uint EnableMars(ref uint info)
=> SniNativeWrapper.SniAddProvider(Handle, Provider.SMUX_PROV, ref info);

internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
=> SniNativeWrapper.SniSetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);

internal override SSPIContextProvider CreateSSPIContextProvider() => new NativeSSPIContextProvider();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal enum TdsOperationStatus : int
InvalidData = 2
}

partial class TdsParserStateObject
internal abstract partial class TdsParserStateObject
{
private static int s_objectTypeCount; // EventSource counter
internal readonly int _objectID = Interlocked.Increment(ref s_objectTypeCount);
Expand Down Expand Up @@ -304,7 +304,7 @@ private enum SnapshotStatus
// Constructors //
//////////////////

internal TdsParserStateObject(TdsParser parser)
protected TdsParserStateObject(TdsParser parser)
{
// Construct a physical connection
Debug.Assert(parser != null, "no parser?");
Expand Down Expand Up @@ -466,6 +466,16 @@ internal long TimeoutTime
}
}

internal abstract uint SniGetConnectionId(ref Guid clientConnectionId);

internal abstract uint DisableSsl();

internal abstract SSPIContextProvider CreateSSPIContextProvider();

internal abstract uint EnableMars(ref uint info);

internal abstract uint SetConnectionBufferSize(ref uint unsignedPacketSize);

internal int GetTimeoutRemaining()
{
int remaining;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
#if NET
using Microsoft.Data.SqlClient.SNI;
#endif

namespace Microsoft.Data.SqlClient
{
Expand All @@ -13,23 +15,43 @@ internal sealed class TdsParserStateObjectFactory

private const string UseManagedNetworkingOnWindows = "Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows";

private static bool shouldUseManagedSNI;
#if NET
private static bool s_shouldUseManagedSNI;

// If the appcontext switch is set then Use Managed SNI based on the value. Otherwise Native SNI.dll will be used by default.
public static bool UseManagedSNI =>
AppContext.TryGetSwitch(UseManagedNetworkingOnWindows, out shouldUseManagedSNI) ? shouldUseManagedSNI : false;
public static bool UseManagedSNI =>
AppContext.TryGetSwitch(UseManagedNetworkingOnWindows, out s_shouldUseManagedSNI) ? s_shouldUseManagedSNI : false;
#else
public const bool UseManagedSNI = false;
#endif

public EncryptionOptions EncryptionOptions => UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Options : SNILoadHandle.SingletonInstance.Options;
public EncryptionOptions EncryptionOptions =>
#if NET
UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Options : SNILoadHandle.SingletonInstance.Options;
#else
SNILoadHandle.SingletonInstance.Options;
#endif

public uint SNIStatus => UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Status : SNILoadHandle.SingletonInstance.Status;
public uint SNIStatus =>
#if NET
UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.Status : SNILoadHandle.SingletonInstance.Status;
#else
SNILoadHandle.SingletonInstance.Status;
#endif

/// <summary>
/// Verify client encryption possibility.
/// </summary>
public bool ClientOSEncryptionSupport => UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport : SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
public bool ClientOSEncryptionSupport =>
#if NET
UseManagedSNI ? SNI.SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport : SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
#else
SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
#endif

public TdsParserStateObject CreateTdsParserStateObject(TdsParser parser)
{
#if NET
if (UseManagedSNI)
{
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectFactory.CreateTdsParserStateObject | Info | Found AppContext switch '{0}' enabled, managed networking implementation will be used."
Expand All @@ -42,15 +64,20 @@ public TdsParserStateObject CreateTdsParserStateObject(TdsParser parser)
, UseManagedNetworkingOnWindows);
return new TdsParserStateObjectNative(parser);
}
#else
return new TdsParserStateObjectNative(parser);
#endif
}

internal TdsParserStateObject CreateSessionObject(TdsParser tdsParser, TdsParserStateObject _pMarsPhysicalConObj, bool v)
{
#if NET
if (TdsParserStateObjectFactory.UseManagedSNI)
{
return new TdsParserStateObjectManaged(tdsParser, _pMarsPhysicalConObj, true);
}
else
#endif
{
return new TdsParserStateObjectNative(tdsParser, _pMarsPhysicalConObj, true);
}
Expand Down
Loading