Skip to content

Commit ac7c1a4

Browse files
authored
Merge to shared - TdsParserSafeHandles (#1604)
1 parent a4974d4 commit ac7c1a4

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@
660660
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCertificateStoreProvider.Windows.cs" />
661661
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCngProvider.Windows.cs" />
662662
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.Windows.cs" />
663+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs">
664+
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs</Link>
665+
</Compile>
663666
</ItemGroup>
664667
<!-- Unix only -->
665668
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
@@ -745,7 +748,6 @@
745748
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Windows.cs" />
746749
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs" />
747750
<Compile Include="Microsoft\Data\SqlClient\SNI\LocalDB.Windows.cs" />
748-
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
749751
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
750752
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Windows.cs" />
751753
</ItemGroup>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,9 @@
554554
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParameterSetter.cs">
555555
<Link>Microsoft\Data\SqlClient\TdsParameterSetter.cs</Link>
556556
</Compile>
557+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs">
558+
<Link>Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs</Link>
559+
</Compile>
557560
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
558561
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
559562
</Compile>
@@ -648,7 +651,6 @@
648651
<Compile Include="Microsoft\Data\SqlClient\SqlUtil.cs" />
649652
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
650653
<Compile Include="Microsoft\Data\SqlClient\TdsParserHelperClasses.cs" />
651-
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
652654
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
653655
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
654656
<Compile Include="Microsoft\Data\SqlTypes\SqlFileStream.cs" />

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ internal void Connect(ServerInfo serverInfo,
523523
// Clean up IsSQLDNSCachingSupported flag from previous status
524524
_connHandler.IsSQLDNSCachingSupported = false;
525525

526-
UInt32 sniStatus = SNILoadHandle.SingletonInstance.SNIStatus;
526+
UInt32 sniStatus = SNILoadHandle.SingletonInstance.Status;
527527
if (sniStatus != TdsEnums.SNI_SUCCESS)
528528
{
529529
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Runtime.CompilerServices;
89
using System.Runtime.InteropServices;
910
using System.Threading;
11+
#if NETFRAMEWORK
12+
using Microsoft.Data.Common;
13+
#endif
1014

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

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

102106
if (null != stateObj)
103107
{
108+
#if NETFRAMEWORK
109+
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
110+
#else
104111
stateObj.ReadAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
112+
#endif // NETFRAMEWORK
105113
}
106114
}
107115
}
@@ -122,7 +130,11 @@ private static void WriteDispatcher(IntPtr key, IntPtr packet, uint error)
122130

123131
if (null != stateObj)
124132
{
133+
#if NETFRAMEWORK
134+
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
135+
#else
125136
stateObj.WriteAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error);
137+
#endif // NETFRAMEWORK
126138
}
127139
}
128140
}
@@ -144,12 +156,17 @@ internal SNIHandle(
144156
bool flushCache,
145157
bool fSync,
146158
bool fParallel,
159+
#if NETFRAMEWORK
160+
TransparentNetworkResolutionState transparentNetworkResolutionState,
161+
int totalTimeout,
162+
#endif
147163
SqlConnectionIPAddressPreference ipPreference,
148164
SQLDNSInfo cachedDNSInfo,
149165
bool tlsFirst,
150166
string hostNameInCertificate)
151167
: base(IntPtr.Zero, true)
152168
{
169+
RuntimeHelpers.PrepareConstrainedRegions();
153170
try
154171
{ }
155172
finally
@@ -158,11 +175,21 @@ internal SNIHandle(
158175
instanceName = new byte[256]; // Size as specified by netlibs.
159176
if (ignoreSniOpenTimeout)
160177
{
178+
// UNDONE: ITEM12001110 (DB Mirroring Reconnect) Old behavior of not truly honoring timeout presevered
179+
// for non-failover scenarios to avoid breaking changes as part of a QFE. Consider fixing timeout
180+
// handling in next full release and removing ignoreSniOpenTimeout parameter.
161181
timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE
162182
}
163183

164-
_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle, spnBuffer, instanceName, flushCache,
165-
fSync, timeout, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate);
184+
#if NETFRAMEWORK
185+
int transparentNetworkResolutionStateNo = (int)transparentNetworkResolutionState;
186+
_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
187+
spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, transparentNetworkResolutionStateNo, totalTimeout,
188+
ADP.IsAzureSqlServerEndpoint(serverName), ipPreference, cachedDNSInfo, hostNameInCertificate);
189+
#else
190+
_status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
191+
spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate);
192+
#endif // NETFRAMEWORK
166193
}
167194
}
168195

0 commit comments

Comments
 (0)