Skip to content

Commit

Permalink
Merge to shared - TdsParserSafeHandles (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcheunglci authored Oct 5, 2022
1 parent a4974d4 commit ac7c1a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCertificateStoreProvider.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCngProvider.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.Windows.cs" />
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs">
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs</Link>
</Compile>
</ItemGroup>
<!-- Unix only -->
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
Expand Down Expand Up @@ -745,7 +748,6 @@
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Windows.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Windows.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParameterSetter.cs">
<Link>Microsoft\Data\SqlClient\TdsParameterSetter.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs">
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
</Compile>
Expand Down Expand Up @@ -648,7 +651,6 @@
<Compile Include="Microsoft\Data\SqlClient\SqlUtil.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
<Compile Include="Microsoft\Data\SqlTypes\SqlFileStream.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ internal void Connect(ServerInfo serverInfo,
// Clean up IsSQLDNSCachingSupported flag from previous status
_connHandler.IsSQLDNSCachingSupported = false;

UInt32 sniStatus = SNILoadHandle.SingletonInstance.SNIStatus;
UInt32 sniStatus = SNILoadHandle.SingletonInstance.Status;
if (sniStatus != TdsEnums.SNI_SUCCESS)
{
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
#if NETFRAMEWORK
using Microsoft.Data.Common;
#endif

namespace Microsoft.Data.SqlClient
{
Expand All @@ -23,7 +27,7 @@ internal sealed partial class SNILoadHandle : SafeHandle

private SNILoadHandle() : base(IntPtr.Zero, true)
{
// From security review - SafeHandle guarantees this is only called once.
// SQL BU DT 346588 - from security review - SafeHandle guarantees this is only called once.
// The reason for the safehandle is guaranteed initialization and termination of SNI to
// ensure SNI terminates and cleans up properly.
try
Expand All @@ -49,7 +53,7 @@ public bool ClientOSEncryptionSupport
{
try
{
UInt32 value = 0;
uint value = 0;
// Query OS to find out whether encryption is supported.
SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value);
_clientOSEncryptionSupport = value != 0;
Expand Down Expand Up @@ -101,7 +105,11 @@ private static void ReadDispatcher(IntPtr key, IntPtr packet, uint error)

if (null != stateObj)
{
#if NETFRAMEWORK
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
#else
stateObj.ReadAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
#endif // NETFRAMEWORK
}
}
}
Expand All @@ -122,7 +130,11 @@ private static void WriteDispatcher(IntPtr key, IntPtr packet, uint error)

if (null != stateObj)
{
#if NETFRAMEWORK
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
#else
stateObj.WriteAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
#endif // NETFRAMEWORK
}
}
}
Expand All @@ -144,12 +156,17 @@ internal SNIHandle(
bool flushCache,
bool fSync,
bool fParallel,
#if NETFRAMEWORK
TransparentNetworkResolutionState transparentNetworkResolutionState,
int totalTimeout,
#endif
SqlConnectionIPAddressPreference ipPreference,
SQLDNSInfo cachedDNSInfo,
bool tlsFirst,
string hostNameInCertificate)
: base(IntPtr.Zero, true)
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{ }
finally
Expand All @@ -158,11 +175,21 @@ internal SNIHandle(
instanceName = new byte[256]; // Size as specified by netlibs.
if (ignoreSniOpenTimeout)
{
// UNDONE: ITEM12001110 (DB Mirroring Reconnect) Old behavior of not truly honoring timeout presevered
// for non-failover scenarios to avoid breaking changes as part of a QFE. Consider fixing timeout
// handling in next full release and removing ignoreSniOpenTimeout parameter.
timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE
}

_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle, spnBuffer, instanceName, flushCache,
fSync, timeout, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate);
#if NETFRAMEWORK
int transparentNetworkResolutionStateNo = (int)transparentNetworkResolutionState;
_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, transparentNetworkResolutionStateNo, totalTimeout,
ADP.IsAzureSqlServerEndpoint(serverName), ipPreference, cachedDNSInfo, hostNameInCertificate);
#else
_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate);
#endif // NETFRAMEWORK
}
}

Expand Down

0 comments on commit ac7c1a4

Please sign in to comment.