From ac7c1a4a6bc18fdc4892118a2d07211f990c6286 Mon Sep 17 00:00:00 2001
From: Lawrence Cheung <31262254+lcheunglci@users.noreply.github.com>
Date: Tue, 4 Oct 2022 17:45:22 -0700
Subject: [PATCH] Merge to shared - TdsParserSafeHandles (#1604)
---
.../src/Microsoft.Data.SqlClient.csproj | 4 ++-
.../netfx/src/Microsoft.Data.SqlClient.csproj | 4 ++-
.../src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +-
.../TdsParserSafeHandles.Windows.cs} | 35 ++++++++++++++++---
4 files changed, 38 insertions(+), 7 deletions(-)
rename src/Microsoft.Data.SqlClient/{netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs => src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs} (85%)
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
index 5b72e28c18..611018d788 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
@@ -660,6 +660,9 @@
+
+ Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs
+
@@ -745,7 +748,6 @@
-
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
index 12323365bf..95ff75b838 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
@@ -554,6 +554,9 @@
Microsoft\Data\SqlClient\TdsParameterSetter.cs
+
+ Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs
+
Microsoft\Data\SqlClient\TdsParserStaticMethods.cs
@@ -648,7 +651,6 @@
-
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs
index cd0fab3e2d..1edad799ae 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs
@@ -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));
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs
similarity index 85%
rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs
rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs
index 3db0f07fcf..8d2716c63e 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs
@@ -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
{
@@ -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
@@ -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;
@@ -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
}
}
}
@@ -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
}
}
}
@@ -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
@@ -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
}
}