Skip to content

Commit 8b58d50

Browse files
edwardnealbenrr101
andauthored
Merge | TdsParserStateObject lifetime-related methods (#3394)
* Port PacketHandle to netfx * Remove PacketHandle alias shim * Align netfx use of PacketHandle * Merge IsPacketEmpty * Merge ReleasePacket * Merge ReadAsync, ReadSyncOverAsync Also move ReadSyncOverAsync down in netcore's TdsParserStateObjectNative to aid later merge * Merge IsFailedHandle * Merge SniPacketGetData * Merge EmptyReadPacket Also reorder member in TdsParserStateObjectNative to simplify later merge * Merge CheckPacket * Merge WritePacket, IsValidPacket * Merge GetResetWritePacket * Merge ClearAllWritePackets, AddPacketToPendingList, RemovePacketFromPendingList * Improve diff between versions of TdsParserStateObjectNative * PR feedback from #3353 - format IsValidPacket * Address merge conflicts * Merge DisposePacketCache This also allows _writePacketCache to be migrated to TdsParserStateObjectNative in netfx. * Merge CreateSessionHandle * Move AssignPendingDNSInfo from TdsParser, merge * Sync method signature for CreatePhysicalSNIHandle * Port netcore _serverSpn array to netfx * Merge CreatePhysicalSNIHandle * Refactor to insert FreeGcHandle * Move CreateConsumerInfo and _gcHandle to TdsParserStateObjectNative * Merge Dispose * Add region to both TdsParserStateObjectNative files * Make public constructor internal * Remove redundant assignment to _serverSpn * Remove uncalled BestEffortCleanup methods This allows TdsParser.netfx.cs to be deleted --------- Co-authored-by: Ben Russell <[email protected]>
1 parent cd3dbd1 commit 8b58d50

File tree

12 files changed

+270
-251
lines changed

12 files changed

+270
-251
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ internal void Connect(ServerInfo serverInfo,
434434
false,
435435
true,
436436
fParallel,
437+
TransparentNetworkResolutionState.DisabledMode,
438+
-1,
437439
_connHandler.ConnectionOptions.IPAddressPreference,
438440
FQDNforDNSCache,
439441
ref _connHandler.pendingSQLDNSObject,
@@ -532,6 +534,8 @@ internal void Connect(ServerInfo serverInfo,
532534
true,
533535
true,
534536
fParallel,
537+
TransparentNetworkResolutionState.DisabledMode,
538+
-1,
535539
_connHandler.ConnectionOptions.IPAddressPreference,
536540
FQDNforDNSCache,
537541
ref _connHandler.pendingSQLDNSObject,

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal abstract partial class TdsParserStateObject
2121
// Constructors //
2222
//////////////////
2323

24-
internal TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalConnection, bool async)
24+
protected TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalConnection, bool async)
2525
{
2626
// Construct a MARS session
2727
Debug.Assert(parser != null, "no parser?");
@@ -57,40 +57,17 @@ internal TdsParserStateObject(TdsParser parser, TdsParserStateObject physicalCon
5757
// General methods //
5858
/////////////////////
5959

60-
internal abstract void CreatePhysicalSNIHandle(
61-
string serverName,
62-
TimeoutTimer timeout,
63-
out byte[] instanceName,
64-
out ResolvedServerSpn resolvedSpn,
65-
bool flushCache,
66-
bool async,
67-
bool fParallel,
68-
SqlConnectionIPAddressPreference iPAddressPreference,
69-
string cachedFQDN,
70-
ref SQLDNSInfo pendingDNSInfo,
71-
string serverSPN,
72-
bool isIntegratedSecurity = false,
73-
bool tlsFirst = false,
74-
string hostNameInCertificate = "",
75-
string serverCertificateFilename = "");
76-
77-
internal abstract void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo);
78-
79-
protected abstract void CreateSessionHandle(TdsParserStateObject physicalConnection, bool async);
80-
81-
protected abstract void FreeGcHandle(int remaining, bool release);
82-
8360
internal abstract uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename);
8461

85-
internal abstract void Dispose();
86-
8762
internal abstract uint CheckConnection();
8863

8964
internal int DecrementPendingCallbacks(bool release)
9065
{
9166
int remaining = Interlocked.Decrement(ref _pendingCallbacks);
9267
SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.DecrementPendingCallbacks | ADV | State Object Id {0}, after decrementing _pendingCallbacks: {1}", _objectID, _pendingCallbacks);
68+
9369
FreeGcHandle(remaining, release);
70+
9471
// NOTE: TdsParserSessionPool may call DecrementPendingCallbacks on a TdsParserStateObject which is already disposed
9572
// This is not dangerous (since the stateObj is no longer in use), but we need to add a workaround in the assert for it
9673
Debug.Assert((remaining == -1 && SessionHandle.IsNull) || (0 <= remaining && remaining < 3), $"_pendingCallbacks values is invalid after decrementing: {remaining}");

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ internal override void CreatePhysicalSNIHandle(
8585
bool flushCache,
8686
bool async,
8787
bool parallel,
88+
TransparentNetworkResolutionState transparentNetworkResolutionState,
89+
int totalTimeout,
8890
SqlConnectionIPAddressPreference iPAddressPreference,
8991
string cachedFQDN,
9092
ref SQLDNSInfo pendingDNSInfo,

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ internal TdsParserStateObjectNative(TdsParser parser, TdsParserStateObject physi
5252
{
5353
}
5454

55-
public TdsParserStateObjectNative(TdsParser parser)
55+
internal TdsParserStateObjectNative(TdsParser parser)
5656
: base(parser)
5757
{
5858
}
5959

60-
////////////////
61-
// Properties //
62-
////////////////
60+
#region Properties
6361

6462
internal SNIHandle Handle => _sessionHandle;
6563

@@ -71,6 +69,8 @@ public TdsParserStateObjectNative(TdsParser parser)
7169

7270
internal override Guid? SessionId => default;
7371

72+
#endregion
73+
7474
protected override void CreateSessionHandle(TdsParserStateObject physicalConnection, bool async)
7575
{
7676
Debug.Assert(physicalConnection is TdsParserStateObjectNative, "Expected a stateObject of type " + this.GetType());
@@ -83,6 +83,9 @@ protected override void CreateSessionHandle(TdsParserStateObject physicalConnect
8383
_sessionHandle = new SNIHandle(myInfo, nativeSNIObject.Handle, _parser.Connection.ConnectionOptions.IPAddressPreference, cachedDNSInfo);
8484
}
8585

86+
// Retrieve the IP and port number from native SNI for TCP protocol. The IP information is stored temporarily in the
87+
// pendingSQLDNSObject but not in the DNS Cache at this point. We only add items to the DNS Cache after we receive the
88+
// IsSupported flag as true in the feature ext ack from server.
8689
internal override void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo)
8790
{
8891
uint result;
@@ -155,11 +158,13 @@ internal override void CreatePhysicalSNIHandle(
155158
string serverName,
156159
TimeoutTimer timeout,
157160
out byte[] instanceName,
158-
out Microsoft.Data.SqlClient.ManagedSni.ResolvedServerSpn resolvedSpn,
161+
out ManagedSni.ResolvedServerSpn resolvedSpn,
159162
bool flushCache,
160163
bool async,
161164
bool fParallel,
162-
SqlConnectionIPAddressPreference ipPreference,
165+
TransparentNetworkResolutionState transparentNetworkResolutionState,
166+
int totalTimeout,
167+
SqlConnectionIPAddressPreference iPAddressPreference,
163168
string cachedFQDN,
164169
ref SQLDNSInfo pendingDNSInfo,
165170
string serverSPN,
@@ -188,7 +193,7 @@ internal override void CreatePhysicalSNIHandle(
188193
bool ret = SQLFallbackDNSCache.Instance.GetDNSInfo(cachedFQDN, out cachedDNSInfo);
189194

190195
_sessionHandle = new SNIHandle(myInfo, serverName, ref serverSPN, timeout.MillisecondsRemainingInt, out instanceName,
191-
flushCache, !async, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate);
196+
flushCache, !async, fParallel, iPAddressPreference, cachedDNSInfo, hostNameInCertificate);
192197
resolvedSpn = new(serverSPN.TrimEnd());
193198
}
194199

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@
450450
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\LocalAppContextSwitches.cs">
451451
<Link>Microsoft\Data\SqlClient\LocalAppContextSwitches.cs</Link>
452452
</Compile>
453+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ManagedSni\ResolvedServerSpn.cs">
454+
<Link>Microsoft\Data\SqlClient\ManagedSni\ResolvedServerSpn.cs</Link>
455+
</Compile>
453456
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\NoneAttestationEnclaveProvider.cs">
454457
<Link>Microsoft\Data\SqlClient\NoneAttestationEnclaveProvider.cs</Link>
455458
</Compile>
@@ -924,7 +927,6 @@
924927
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionHelper.cs" />
925928
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
926929
<Compile Include="Microsoft\Data\SqlClient\TdsParser.cs" />
927-
<Compile Include="Microsoft\Data\SqlClient\TdsParser.netfx.cs" />
928930
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
929931
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
930932
</ItemGroup>

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,11 @@ internal void Connect(ServerInfo serverInfo,
381381
ThrowExceptionAndWarning(_physicalStateObj);
382382
Debug.Fail("SNI returned status != success, but no error thrown?");
383383
}
384-
385-
string serverSpn = null;
384+
else
385+
{
386+
SqlClientEventSource.Log.TryTraceEvent("TdsParser.Connect | SEC | Connection Object Id {0}, Authentication Mode: {1}", _connHandler.ObjectID,
387+
authType == SqlAuthenticationMethod.NotSpecified ? SqlAuthenticationMethod.SqlPassword.ToString() : authType.ToString());
388+
}
386389

387390
//Create LocalDB instance if necessary
388391
if (connHandler.ConnectionOptions.LocalDBInstance != null)
@@ -401,17 +404,6 @@ internal void Connect(ServerInfo serverInfo,
401404
{
402405
_authenticationProvider = Connection._sspiContextProvider ?? _physicalStateObj.CreateSspiContextProvider();
403406

404-
if (!string.IsNullOrEmpty(serverInfo.ServerSPN))
405-
{
406-
serverSpn = serverInfo.ServerSPN;
407-
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Server SPN `{0}` from the connection string is used.", serverInfo.ServerSPN);
408-
}
409-
else
410-
{
411-
// Empty signifies to interop layer that SPN needs to be generated
412-
serverSpn = string.Empty;
413-
}
414-
415407
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> SSPI or Active Directory Authentication Library for SQL Server based integrated authentication");
416408
}
417409
else
@@ -495,15 +487,20 @@ internal void Connect(ServerInfo serverInfo,
495487
serverInfo.ExtendedServerName,
496488
timeout,
497489
out instanceName,
498-
ref serverSpn,
490+
out var resolvedServerSpn,
499491
false,
500492
true,
501493
fParallel,
502494
transparentNetworkResolutionState,
503495
totalTimeout,
504496
_connHandler.ConnectionOptions.IPAddressPreference,
505497
FQDNforDNSCache,
506-
hostNameInCertificate);
498+
ref _connHandler.pendingSQLDNSObject,
499+
serverInfo.ServerSPN,
500+
integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated,
501+
isTlsFirst,
502+
hostNameInCertificate,
503+
serverCertificateFilename);
507504

508505
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
509506
{
@@ -544,7 +541,7 @@ internal void Connect(ServerInfo serverInfo,
544541
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
545542

546543
// for DNS Caching phase 1
547-
AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache);
544+
_physicalStateObj.AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache, ref _connHandler.pendingSQLDNSObject);
548545

549546
if (!ClientOSEncryptionSupport)
550547
{
@@ -587,15 +584,20 @@ internal void Connect(ServerInfo serverInfo,
587584
serverInfo.ExtendedServerName,
588585
timeout,
589586
out instanceName,
590-
ref serverSpn,
587+
out resolvedServerSpn,
591588
true,
592589
true,
593590
fParallel,
594591
transparentNetworkResolutionState,
595592
totalTimeout,
596593
_connHandler.ConnectionOptions.IPAddressPreference,
597-
serverInfo.ResolvedServerName,
598-
hostNameInCertificate);
594+
FQDNforDNSCache,
595+
ref _connHandler.pendingSQLDNSObject,
596+
serverInfo.ServerSPN,
597+
integratedSecurity,
598+
isTlsFirst,
599+
hostNameInCertificate,
600+
serverCertificateFilename);
599601

600602
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status)
601603
{
@@ -609,7 +611,7 @@ internal void Connect(ServerInfo serverInfo,
609611
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Sending prelogin handshake");
610612

611613
// for DNS Caching phase 1
612-
AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache);
614+
_physicalStateObj.AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache, ref _connHandler.pendingSQLDNSObject);
613615

614616
SendPreLoginHandshake(instanceName, encrypt, integratedSecurity, serverCertificateFilename);
615617
status = ConsumePreLoginHandshake(
@@ -631,7 +633,10 @@ internal void Connect(ServerInfo serverInfo,
631633
}
632634
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Prelogin handshake successful");
633635

634-
_authenticationProvider?.Initialize(serverInfo, _physicalStateObj, this, serverSpn);
636+
if (_authenticationProvider is { })
637+
{
638+
_authenticationProvider.Initialize(serverInfo, _physicalStateObj, this, resolvedServerSpn.Primary, resolvedServerSpn.Secondary);
639+
}
635640

636641
if (_fMARS && marsCapable)
637642
{

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

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)