Skip to content

Commit 1e59b88

Browse files
authored
Cleanup | SNI Native Wrapper (#3156)
* Cleanup member variables of SniNativeWrapper * Sort public methods (as per target names) * Sort private methods (most of these will be removed in next commit) * Remove unneeded private methods * Rename methods as per naming rules * Cleanup suggestions and follow naming conventions * Remove AppDomain method from SniNativeWrapper * Remove Win32NativeMethods * Remove sqldependency process storage methods from SniNativeWrapper * Fix extraneous using statements * Revert a name change in the test harness * Removing no-longer-used error code
1 parent b242b89 commit 1e59b88

19 files changed

+429
-556
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private SNIErrorDetails GetSniErrorDetails()
7979
}
8080
else
8181
{
82-
SniNativeWrapper.SNIGetLastError(out SniError sniError);
82+
SniNativeWrapper.SniGetLastError(out SniError sniError);
8383
details.sniErrorNumber = sniError.sniError;
8484
details.errorMessage = sniError.errorMessage;
8585
details.nativeError = sniError.nativeError;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ internal abstract void CreatePhysicalSNIHandle(
229229

230230
internal abstract void ReleasePacket(PacketHandle syncReadPacket);
231231

232-
protected abstract uint SNIPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize);
232+
protected abstract uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize);
233233

234234
internal abstract PacketHandle GetResetWritePacket(int dataSize);
235235

@@ -401,7 +401,7 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
401401

402402
private uint GetSniPacket(PacketHandle packet, ref uint dataSize)
403403
{
404-
return SNIPacketGetData(packet, _inBuff, ref dataSize);
404+
return SniPacketGetData(packet, _inBuff, ref dataSize);
405405
}
406406

407407
private void ChangeNetworkPacketTimeout(int dueTime, int period)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal SNIMarsHandle CreateMarsSession(object callbackObject, bool async)
6969
/// <param name="inBuff">Destination byte array where data packets are copied to</param>
7070
/// <param name="dataSize">Length of data packets</param>
7171
/// <returns>SNI error status</returns>
72-
protected override uint SNIPacketGetData(PacketHandle packet, byte[] inBuff, ref uint dataSize)
72+
protected override uint SniPacketGetData(PacketHandle packet, byte[] inBuff, ref uint dataSize)
7373
{
7474
int dataSizeInt = 0;
7575
packet.ManagedPacket.GetData(inBuff, ref dataSizeInt);

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache
9999
result = SniNativeWrapper.SniGetConnectionPort(Handle, ref portFromSNI);
100100
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort");
101101

102-
result = SniNativeWrapper.SniGetConnectionIPString(Handle, ref IPStringFromSNI);
102+
result = SniNativeWrapper.SniGetConnectionIpString(Handle, ref IPStringFromSNI);
103103
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString");
104104

105105
pendingDNSInfo = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString());
@@ -181,10 +181,10 @@ internal override void CreatePhysicalSNIHandle(
181181
spns = new[] { serverSPN.TrimEnd() };
182182
}
183183

184-
protected override uint SNIPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
184+
protected override uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
185185
{
186186
Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
187-
return SniNativeWrapper.SNIPacketGetData(packet.NativePointer, _inBuff, ref dataSize);
187+
return SniNativeWrapper.SniPacketGetData(packet.NativePointer, _inBuff, ref dataSize);
188188
}
189189

190190
protected override bool CheckPacket(PacketHandle packet, TaskCompletionSource<object> source)
@@ -264,7 +264,7 @@ internal override PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint
264264
throw ADP.ClosedConnectionError();
265265
}
266266
IntPtr readPacketPtr = IntPtr.Zero;
267-
error = SniNativeWrapper.SNIReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining());
267+
error = SniNativeWrapper.SniReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining());
268268
return PacketHandle.FromNativePointer(readPacketPtr);
269269
}
270270

@@ -281,20 +281,20 @@ internal override bool IsPacketEmpty(PacketHandle readPacket)
281281
internal override void ReleasePacket(PacketHandle syncReadPacket)
282282
{
283283
Debug.Assert(syncReadPacket.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
284-
SniNativeWrapper.SNIPacketRelease(syncReadPacket.NativePointer);
284+
SniNativeWrapper.SniPacketRelease(syncReadPacket.NativePointer);
285285
}
286286

287287
internal override uint CheckConnection()
288288
{
289289
SNIHandle handle = Handle;
290-
return handle == null ? TdsEnums.SNI_SUCCESS : SniNativeWrapper.SNICheckConnection(handle);
290+
return handle == null ? TdsEnums.SNI_SUCCESS : SniNativeWrapper.SniCheckConnection(handle);
291291
}
292292

293293
internal override PacketHandle ReadAsync(SessionHandle handle, out uint error)
294294
{
295295
Debug.Assert(handle.Type == SessionHandle.NativeHandleType, "unexpected handle type when requiring NativePointer");
296296
IntPtr readPacketPtr = IntPtr.Zero;
297-
error = SniNativeWrapper.SNIReadAsync(handle.NativeHandle, ref readPacketPtr);
297+
error = SniNativeWrapper.SniReadAsync(handle.NativeHandle, ref readPacketPtr);
298298
return PacketHandle.FromNativePointer(readPacketPtr);
299299
}
300300

@@ -310,7 +310,7 @@ internal override PacketHandle CreateAndSetAttentionPacket()
310310
internal override uint WritePacket(PacketHandle packet, bool sync)
311311
{
312312
Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
313-
return SniNativeWrapper.SNIWritePacket(Handle, packet.NativePacket, sync);
313+
return SniNativeWrapper.SniWritePacket(Handle, packet.NativePacket, sync);
314314
}
315315

316316
internal override PacketHandle AddPacketToPendingList(PacketHandle packetToAdd)
@@ -343,7 +343,7 @@ internal override PacketHandle GetResetWritePacket(int dataSize)
343343
{
344344
if (_sniPacket != null)
345345
{
346-
SniNativeWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
346+
SniNativeWrapper.SniPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
347347
}
348348
else
349349
{
@@ -372,17 +372,17 @@ internal override void ClearAllWritePackets()
372372
internal override void SetPacketData(PacketHandle packet, byte[] buffer, int bytesUsed)
373373
{
374374
Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
375-
SniNativeWrapper.SNIPacketSetData(packet.NativePacket, buffer, bytesUsed);
375+
SniNativeWrapper.SniPacketSetData(packet.NativePacket, buffer, bytesUsed);
376376
}
377377

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

381381
internal override uint DisableSsl()
382-
=> SniNativeWrapper.SNIRemoveProvider(Handle, Provider.SSL_PROV);
382+
=> SniNativeWrapper.SniRemoveProvider(Handle, Provider.SSL_PROV);
383383

384384
internal override uint EnableMars(ref uint info)
385-
=> SniNativeWrapper.SNIAddProvider(Handle, Provider.SMUX_PROV, ref info);
385+
=> SniNativeWrapper.SniAddProvider(Handle, Provider.SMUX_PROV, ref info);
386386

387387
internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename)
388388
{
@@ -392,15 +392,15 @@ internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCert
392392
authInfo.serverCertFileName = serverCertificateFilename;
393393

394394
// Add SSL (Encryption) SNI provider.
395-
return SniNativeWrapper.SNIAddProvider(Handle, Provider.SSL_PROV, ref authInfo);
395+
return SniNativeWrapper.SniAddProvider(Handle, Provider.SSL_PROV, ref authInfo);
396396
}
397397

398398
internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
399-
=> SniNativeWrapper.SNISetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
399+
=> SniNativeWrapper.SniSetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
400400

401401
internal override uint WaitForSSLHandShakeToComplete(out int protocolVersion)
402402
{
403-
uint returnValue = SniNativeWrapper.SNIWaitForSSLHandshakeToComplete(Handle, GetTimeoutRemaining(), out uint nativeProtocolVersion);
403+
uint returnValue = SniNativeWrapper.SniWaitForSslHandshakeToComplete(Handle, GetTimeoutRemaining(), out uint nativeProtocolVersion);
404404
var nativeProtocol = (NativeProtocols)nativeProtocolVersion;
405405

406406
#pragma warning disable CA5398 // Avoid hardcoded SslProtocols values
@@ -469,7 +469,7 @@ public SNIPacket Take(SNIHandle sniHandle)
469469
{
470470
// Success - reset the packet
471471
packet = _packets.Pop();
472-
SniNativeWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI);
472+
SniNativeWrapper.SniPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI);
473473
}
474474
else
475475
{

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ internal void RemoveEncryption()
699699
uint error = 0;
700700

701701
// Remove SSL (Encryption) SNI provider since we only wanted to encrypt login.
702-
error = SniNativeWrapper.SNIRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV);
702+
error = SniNativeWrapper.SniRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV);
703703
if (error != TdsEnums.SNI_SUCCESS)
704704
{
705705
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
@@ -726,7 +726,7 @@ internal void EnableMars()
726726
uint info = 0;
727727

728728
// Add SMUX (MARS) SNI provider.
729-
error = SniNativeWrapper.SNIAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info);
729+
error = SniNativeWrapper.SniAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info);
730730

731731
if (error != TdsEnums.SNI_SUCCESS)
732732
{
@@ -747,12 +747,12 @@ internal void EnableMars()
747747
{
748748
_pMarsPhysicalConObj.IncrementPendingCallbacks();
749749

750-
error = SniNativeWrapper.SNIReadAsync(_pMarsPhysicalConObj.Handle, ref temp);
750+
error = SniNativeWrapper.SniReadAsync(_pMarsPhysicalConObj.Handle, ref temp);
751751

752752
if (temp != IntPtr.Zero)
753753
{
754754
// Be sure to release packet, otherwise it will be leaked by native.
755-
SniNativeWrapper.SNIPacketRelease(temp);
755+
SniNativeWrapper.SniPacketRelease(temp);
756756
}
757757
}
758758
Debug.Assert(IntPtr.Zero == temp, "unexpected syncReadPacket without corresponding SNIPacketRelease");
@@ -1025,7 +1025,7 @@ private void EnableSsl(uint info, SqlConnectionEncryptOption encrypt, bool integ
10251025

10261026
Debug.Assert((_encryptionOption & EncryptionOptions.CLIENT_CERT) == 0, "Client certificate authentication support has been removed");
10271027

1028-
error = SniNativeWrapper.SNIAddProvider(_physicalStateObj.Handle, Provider.SSL_PROV, authInfo);
1028+
error = SniNativeWrapper.SniAddProvider(_physicalStateObj.Handle, Provider.SSL_PROV, authInfo);
10291029

10301030
if (error != TdsEnums.SNI_SUCCESS)
10311031
{
@@ -1037,7 +1037,7 @@ private void EnableSsl(uint info, SqlConnectionEncryptOption encrypt, bool integ
10371037
// wait for SSL handshake to complete, so that the SSL context is fully negotiated before we try to use its
10381038
// Channel Bindings as part of the Windows Authentication context build (SSL handshake must complete
10391039
// before calling SNISecGenClientContext).
1040-
error = SniNativeWrapper.SNIWaitForSSLHandshakeToComplete(_physicalStateObj.Handle, _physicalStateObj.GetTimeoutRemaining(), out uint protocolVersion);
1040+
error = SniNativeWrapper.SniWaitForSslHandshakeToComplete(_physicalStateObj.Handle, _physicalStateObj.GetTimeoutRemaining(), out uint protocolVersion);
10411041

10421042
if (error != TdsEnums.SNI_SUCCESS)
10431043
{
@@ -1591,7 +1591,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
15911591
Debug.Assert(SniContext.Undefined != stateObj.DebugOnlyCopyOfSniContext || ((_fMARS) && ((_state == TdsParserState.Closed) || (_state == TdsParserState.Broken))), "SniContext must not be None");
15921592
#endif
15931593
SniError sniError = new SniError();
1594-
SniNativeWrapper.SNIGetLastError(out sniError);
1594+
SniNativeWrapper.SniGetLastError(out sniError);
15951595

15961596
if (sniError.sniError != 0)
15971597
{
@@ -2906,7 +2906,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb
29062906

29072907
// Update SNI ConsumerInfo value to be resulting packet size
29082908
uint unsignedPacketSize = (uint)packetSize;
2909-
uint bufferSizeResult = SniNativeWrapper.SNISetInfo(_physicalStateObj.Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
2909+
uint bufferSizeResult = SniNativeWrapper.SniSetInfo(_physicalStateObj.Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
29102910

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ internal void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey)
7575
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort");
7676

7777

78-
result = SniNativeWrapper.SniGetConnectionIPString(_physicalStateObj.Handle, ref IPStringFromSNI);
78+
result = SniNativeWrapper.SniGetConnectionIpString(_physicalStateObj.Handle, ref IPStringFromSNI);
7979
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString");
8080

8181
_connHandler.pendingSQLDNSObject = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString());

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,20 @@ internal PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint error)
270270
{
271271
SNIHandle handle = Handle ?? throw ADP.ClosedConnectionError();
272272
PacketHandle readPacket = default;
273-
error = SniNativeWrapper.SNIReadSyncOverAsync(handle, ref readPacket, timeoutRemaining);
273+
error = SniNativeWrapper.SniReadSyncOverAsync(handle, ref readPacket, timeoutRemaining);
274274
return readPacket;
275275
}
276276

277277
internal PacketHandle ReadAsync(SessionHandle handle, out uint error)
278278
{
279279
PacketHandle readPacket = default;
280-
error = SniNativeWrapper.SNIReadAsync(handle.NativeHandle, ref readPacket);
280+
error = SniNativeWrapper.SniReadAsync(handle.NativeHandle, ref readPacket);
281281
return readPacket;
282282
}
283283

284-
internal uint CheckConnection() => SniNativeWrapper.SNICheckConnection(Handle);
284+
internal uint CheckConnection() => SniNativeWrapper.SniCheckConnection(Handle);
285285

286-
internal void ReleasePacket(PacketHandle syncReadPacket) => SniNativeWrapper.SNIPacketRelease(syncReadPacket);
286+
internal void ReleasePacket(PacketHandle syncReadPacket) => SniNativeWrapper.SniPacketRelease(syncReadPacket);
287287

288288
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
289289
internal int DecrementPendingCallbacks(bool release)
@@ -401,7 +401,7 @@ internal bool ValidateSNIConnection()
401401
SNIHandle handle = Handle;
402402
if (handle != null)
403403
{
404-
error = SniNativeWrapper.SNICheckConnection(handle);
404+
error = SniNativeWrapper.SniCheckConnection(handle);
405405
}
406406
}
407407
finally
@@ -518,7 +518,7 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error)
518518

519519
private uint GetSniPacket(PacketHandle packet, ref uint dataSize)
520520
{
521-
return SniNativeWrapper.SNIPacketGetData(packet, _inBuff, ref dataSize);
521+
return SniNativeWrapper.SniPacketGetData(packet, _inBuff, ref dataSize);
522522
}
523523

524524
private void ChangeNetworkPacketTimeout(int dueTime, int period)
@@ -1007,7 +1007,7 @@ private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out uint sniErro
10071007
}
10081008
finally
10091009
{
1010-
sniError = SniNativeWrapper.SNIWritePacket(handle, packet, sync);
1010+
sniError = SniNativeWrapper.SniWritePacket(handle, packet, sync);
10111011
}
10121012

10131013
if (sniError == TdsEnums.SNI_SUCCESS_IO_PENDING)
@@ -1119,7 +1119,7 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa
11191119
SNIPacket attnPacket = new SNIPacket(Handle);
11201120
_sniAsyncAttnPacket = attnPacket;
11211121

1122-
SniNativeWrapper.SNIPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null);
1122+
SniNativeWrapper.SniPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null);
11231123

11241124
RuntimeHelpers.PrepareConstrainedRegions();
11251125
try
@@ -1183,7 +1183,7 @@ private Task WriteSni(bool canAccumulate)
11831183
{
11841184
// Prepare packet, and write to packet.
11851185
SNIPacket packet = GetResetWritePacket();
1186-
SniNativeWrapper.SNIPacketSetData(packet, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer);
1186+
SniNativeWrapper.SniPacketSetData(packet, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer);
11871187

11881188
Debug.Assert(Parser.Connection._parserLock.ThreadMayHaveLock(), "Thread is writing without taking the connection lock");
11891189
Task task = SNIWritePacket(Handle, packet, out _, canAccumulate, callerHasConnectionLock: true);
@@ -1238,7 +1238,7 @@ internal SNIPacket GetResetWritePacket()
12381238
{
12391239
if (_sniPacket != null)
12401240
{
1241-
SniNativeWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
1241+
SniNativeWrapper.SniPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
12421242
}
12431243
else
12441244
{

0 commit comments

Comments
 (0)