Skip to content

Commit 4de9744

Browse files
committed
move single callsite SNIProxy methods into the caller
1 parent ca2fe25 commit 4de9744

File tree

2 files changed

+76
-164
lines changed

2 files changed

+76
-164
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs

+5-149
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,6 @@ internal class SspiClientContextResult
3232

3333
internal static SNIProxy GetInstance() => s_singleton;
3434

35-
/// <summary>
36-
/// Enable SSL on a connection
37-
/// </summary>
38-
/// <param name="handle">Connection handle</param>
39-
/// <param name="options"></param>
40-
/// <returns>SNI error code</returns>
41-
internal uint EnableSsl(SNIHandle handle, uint options)
42-
{
43-
try
44-
{
45-
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.EnableSsl | Info | Session Id {0}", handle?.ConnectionId);
46-
return handle.EnableSsl(options);
47-
}
48-
catch (Exception e)
49-
{
50-
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1}", handle?.ConnectionId, e?.Message);
51-
return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, SNICommon.HandshakeFailureError, e);
52-
}
53-
}
54-
55-
/// <summary>
56-
/// Disable SSL on a connection
57-
/// </summary>
58-
/// <param name="handle">Connection handle</param>
59-
/// <returns>SNI error code</returns>
60-
internal uint DisableSsl(SNIHandle handle)
61-
{
62-
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.DisableSsl | Info | Session Id {0}", handle?.ConnectionId);
63-
handle.DisableSsl();
64-
return TdsEnums.SNI_SUCCESS;
65-
}
66-
6735
/// <summary>
6836
/// Generate SSPI context
6937
/// </summary>
@@ -72,7 +40,7 @@ internal uint DisableSsl(SNIHandle handle)
7240
/// <param name="sendBuff">Send buffer</param>
7341
/// <param name="serverName">Service Principal Name buffer</param>
7442
/// <returns>SNI error code</returns>
75-
internal void GenSspiClientContext(SspiClientContextStatus sspiClientContextStatus, byte[] receivedBuff, ref byte[] sendBuff, byte[][] serverName)
43+
internal static void GenSspiClientContext(SspiClientContextStatus sspiClientContextStatus, byte[] receivedBuff, ref byte[] sendBuff, byte[][] serverName)
7644
{
7745
SafeDeleteContext securityContext = sspiClientContextStatus.SecurityContext;
7846
ContextFlagsPal contextFlags = sspiClientContextStatus.ContextFlags;
@@ -165,83 +133,6 @@ private static bool IsErrorStatus(SecurityStatusPalErrorCode errorCode)
165133
errorCode != SecurityStatusPalErrorCode.Renegotiate;
166134
}
167135

168-
/// <summary>
169-
/// Set connection buffer size
170-
/// </summary>
171-
/// <param name="handle">SNI handle</param>
172-
/// <param name="bufferSize">Buffer size</param>
173-
/// <returns>SNI error code</returns>
174-
internal uint SetConnectionBufferSize(SNIHandle handle, uint bufferSize)
175-
{
176-
handle.SetBufferSize((int)bufferSize);
177-
return TdsEnums.SNI_SUCCESS;
178-
}
179-
180-
/// <summary>
181-
/// Copies data in SNIPacket to given byte array parameter
182-
/// </summary>
183-
/// <param name="packet">SNIPacket object containing data packets</param>
184-
/// <param name="inBuff">Destination byte array where data packets are copied to</param>
185-
/// <param name="dataSize">Length of data packets</param>
186-
/// <returns>SNI error status</returns>
187-
internal uint PacketGetData(SNIPacket packet, byte[] inBuff, ref uint dataSize)
188-
{
189-
int dataSizeInt = 0;
190-
packet.GetData(inBuff, ref dataSizeInt);
191-
dataSize = (uint)dataSizeInt;
192-
193-
return TdsEnums.SNI_SUCCESS;
194-
}
195-
196-
/// <summary>
197-
/// Read synchronously
198-
/// </summary>
199-
/// <param name="handle">SNI handle</param>
200-
/// <param name="packet">SNI packet</param>
201-
/// <param name="timeout">Timeout</param>
202-
/// <returns>SNI error status</returns>
203-
internal uint ReadSyncOverAsync(SNIHandle handle, out SNIPacket packet, int timeout)
204-
{
205-
return handle.Receive(out packet, timeout);
206-
}
207-
208-
/// <summary>
209-
/// Get SNI connection ID
210-
/// </summary>
211-
/// <param name="handle">SNI handle</param>
212-
/// <param name="clientConnectionId">Client connection ID</param>
213-
/// <returns>SNI error status</returns>
214-
internal uint GetConnectionId(SNIHandle handle, ref Guid clientConnectionId)
215-
{
216-
clientConnectionId = handle.ConnectionId;
217-
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetConnectionId | Info | Session Id {0}", clientConnectionId);
218-
return TdsEnums.SNI_SUCCESS;
219-
}
220-
221-
/// <summary>
222-
/// Send a packet
223-
/// </summary>
224-
/// <param name="handle">SNI handle</param>
225-
/// <param name="packet">SNI packet</param>
226-
/// <param name="sync">true if synchronous, false if asynchronous</param>
227-
/// <returns>SNI error status</returns>
228-
internal uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync)
229-
{
230-
uint result;
231-
if (sync)
232-
{
233-
result = handle.Send(packet);
234-
handle.ReturnPacket(packet);
235-
}
236-
else
237-
{
238-
result = handle.SendAsync(packet);
239-
}
240-
241-
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.WritePacket | Info | Session Id {0}, SendAsync Result {1}", handle?.ConnectionId, result);
242-
return result;
243-
}
244-
245136
/// <summary>
246137
/// Create a SNI connection handle
247138
/// </summary>
@@ -257,7 +148,7 @@ internal uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync)
257148
/// <param name="cachedFQDN">Used for DNS Cache</param>
258149
/// <param name="pendingDNSInfo">Used for DNS Cache</param>
259150
/// <returns>SNI handle</returns>
260-
internal SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[][] spnBuffer, bool flushCache, bool async, bool parallel, bool isIntegratedSecurity, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
151+
internal static SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[][] spnBuffer, bool flushCache, bool async, bool parallel, bool isIntegratedSecurity, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
261152
{
262153
instanceName = new byte[1];
263154

@@ -377,7 +268,7 @@ private static byte[][] GetSqlServerSPNs(string hostNameOrAddress, string portOr
377268
/// <param name="cachedFQDN">Key for DNS Cache</param>
378269
/// <param name="pendingDNSInfo">Used for DNS Cache</param>
379270
/// <returns>SNITCPHandle</returns>
380-
private SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, bool parallel, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
271+
private static SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, bool parallel, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
381272
{
382273
// TCP Format:
383274
// tcp:<host name>\<instance name>
@@ -418,16 +309,14 @@ private SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, bool
418309
return new SNITCPHandle(hostName, port, timerExpire, parallel, cachedFQDN, ref pendingDNSInfo);
419310
}
420311

421-
422-
423312
/// <summary>
424313
/// Creates an SNINpHandle object
425314
/// </summary>
426315
/// <param name="details">Data source</param>
427316
/// <param name="timerExpire">Timer expiration</param>
428317
/// <param name="parallel">Should MultiSubnetFailover be used. Only returns an error for named pipes.</param>
429318
/// <returns>SNINpHandle</returns>
430-
private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, bool parallel)
319+
private static SNINpHandle CreateNpHandle(DataSource details, long timerExpire, bool parallel)
431320
{
432321
if (parallel)
433322
{
@@ -438,39 +327,6 @@ private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, bool pa
438327
return new SNINpHandle(details.PipeHostName, details.PipeName, timerExpire);
439328
}
440329

441-
/// <summary>
442-
/// Read packet asynchronously
443-
/// </summary>
444-
/// <param name="handle">SNI handle</param>
445-
/// <param name="packet">Packet</param>
446-
/// <returns>SNI error status</returns>
447-
internal uint ReadAsync(SNIHandle handle, out SNIPacket packet)
448-
{
449-
packet = null;
450-
return handle.ReceiveAsync(ref packet);
451-
}
452-
453-
/// <summary>
454-
/// Set packet data
455-
/// </summary>
456-
/// <param name="packet">SNI packet</param>
457-
/// <param name="data">Data</param>
458-
/// <param name="length">Length</param>
459-
internal void PacketSetData(SNIPacket packet, byte[] data, int length)
460-
{
461-
packet.AppendData(data, length);
462-
}
463-
464-
/// <summary>
465-
/// Check SNI handle connection
466-
/// </summary>
467-
/// <param name="handle"></param>
468-
/// <returns>SNI error status</returns>
469-
internal uint CheckConnection(SNIHandle handle)
470-
{
471-
return handle.CheckConnection();
472-
}
473-
474330
/// <summary>
475331
/// Get last SNI error on this thread
476332
/// </summary>
@@ -486,7 +342,7 @@ internal SNIError GetLastError()
486342
/// <param name="fullServerName">The data source</param>
487343
/// <param name="error">Set true when an error occurred while getting LocalDB up</param>
488344
/// <returns></returns>
489-
private string GetLocalDBDataSource(string fullServerName, out bool error)
345+
private static string GetLocalDBDataSource(string fullServerName, out bool error)
490346
{
491347
string localDBConnectionString = null;
492348
bool isBadLocalDBDataSource;

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

+71-15
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@ internal SNIMarsHandle CreateMarsSession(object callbackObject, bool async)
4747
return _marsConnection.CreateMarsSession(callbackObject, async);
4848
}
4949

50-
protected override uint SNIPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
51-
=> SNIProxy.GetInstance().PacketGetData(packet.ManagedPacket, _inBuff, ref dataSize);
50+
protected override uint SNIPacketGetData(PacketHandle packet, byte[] inBuff, ref uint dataSize)
51+
{
52+
int dataSizeInt = 0;
53+
packet.ManagedPacket.GetData(inBuff, ref dataSizeInt);
54+
dataSize = (uint)dataSizeInt;
55+
return TdsEnums.SNI_SUCCESS;
56+
}
5257

5358
internal override void CreatePhysicalSNIHandle(string serverName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[][] spnBuffer, bool flushCache, bool async, bool parallel, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo, bool isIntegratedSecurity)
5459
{
55-
_sessionHandle = SNIProxy.GetInstance().CreateConnectionHandle(serverName, ignoreSniOpenTimeout, timerExpire, out instanceName, ref spnBuffer, flushCache, async, parallel, isIntegratedSecurity, cachedFQDN, ref pendingDNSInfo);
60+
_sessionHandle = SNIProxy.CreateConnectionHandle(serverName, ignoreSniOpenTimeout, timerExpire, out instanceName, ref spnBuffer, flushCache, async, parallel, isIntegratedSecurity, cachedFQDN, ref pendingDNSInfo);
5661
if (_sessionHandle == null)
5762
{
5863
_parser.ProcessSNIError(this);
@@ -160,7 +165,9 @@ internal override PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint
160165
{
161166
throw ADP.ClosedConnectionError();
162167
}
163-
error = SNIProxy.GetInstance().ReadSyncOverAsync(handle, out SNIPacket packet, timeoutRemaining);
168+
169+
error = handle.Receive(out SNIPacket packet, timeoutRemaining);
170+
164171
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.ReadSyncOverAsync | Info | State Object Id {0}, Session Id {1}", _objectID, _sessionHandle?.ConnectionId);
165172
#if DEBUG
166173
SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObjectManaged.ReadSyncOverAsync | TRC | State Object Id {0}, Session Id {1}, Packet {2} received, Packet owner Id {3}, Packet dataLeft {4}", _objectID, _sessionHandle?.ConnectionId, packet?._id, packet?._owner.ConnectionId, packet?.DataLeft);
@@ -189,12 +196,14 @@ internal override void ReleasePacket(PacketHandle syncReadPacket)
189196
internal override uint CheckConnection()
190197
{
191198
SNIHandle handle = Handle;
192-
return handle == null ? TdsEnums.SNI_SUCCESS : SNIProxy.GetInstance().CheckConnection(handle);
199+
return handle == null ? TdsEnums.SNI_SUCCESS : handle.CheckConnection();
193200
}
194201

195202
internal override PacketHandle ReadAsync(SessionHandle handle, out uint error)
196203
{
197-
error = SNIProxy.GetInstance().ReadAsync(handle.ManagedHandle, out SNIPacket packet);
204+
SNIPacket packet = null;
205+
error = handle.ManagedHandle.ReceiveAsync(ref packet);
206+
198207
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.ReadAsync | Info | State Object Id {0}, Session Id {1}, Packet DataLeft {2}", _objectID, _sessionHandle?.ConnectionId, packet?.DataLeft);
199208
return PacketHandle.FromManagedPacket(packet);
200209
}
@@ -212,8 +221,24 @@ internal override PacketHandle CreateAndSetAttentionPacket()
212221
return packetHandle;
213222
}
214223

215-
internal override uint WritePacket(PacketHandle packet, bool sync) =>
216-
SNIProxy.GetInstance().WritePacket(Handle, packet.ManagedPacket, sync);
224+
internal override uint WritePacket(PacketHandle packetHandle, bool sync)
225+
{
226+
uint result;
227+
SNIHandle handle = Handle;
228+
SNIPacket packet = packetHandle.ManagedPacket;
229+
if (sync)
230+
{
231+
result = handle.Send(packet);
232+
handle.ReturnPacket(packet);
233+
}
234+
else
235+
{
236+
result = handle.SendAsync(packet);
237+
}
238+
239+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.WritePacket | Info | Session Id {0}, SendAsync Result {1}", handle?.ConnectionId, result);
240+
return result;
241+
}
217242

218243
// No- Op in managed SNI
219244
internal override PacketHandle AddPacketToPendingList(PacketHandle packet) => packet;
@@ -244,11 +269,25 @@ internal override void ClearAllWritePackets()
244269
Debug.Assert(_asyncWriteCount == 0, "Should not clear all write packets if there are packets pending");
245270
}
246271

247-
internal override void SetPacketData(PacketHandle packet, byte[] buffer, int bytesUsed) => SNIProxy.GetInstance().PacketSetData(packet.ManagedPacket, buffer, bytesUsed);
272+
internal override void SetPacketData(PacketHandle packet, byte[] buffer, int bytesUsed)
273+
{
274+
packet.ManagedPacket.AppendData(buffer, bytesUsed);
275+
}
248276

249-
internal override uint SniGetConnectionId(ref Guid clientConnectionId) => SNIProxy.GetInstance().GetConnectionId(Handle, ref clientConnectionId);
277+
internal override uint SniGetConnectionId(ref Guid clientConnectionId)
278+
{
279+
clientConnectionId = Handle.ConnectionId;
280+
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetConnectionId | Info | Session Id {0}", clientConnectionId);
281+
return TdsEnums.SNI_SUCCESS;
282+
}
250283

251-
internal override uint DisableSsl() => SNIProxy.GetInstance().DisableSsl(Handle);
284+
internal override uint DisableSsl()
285+
{
286+
SNIHandle handle = Handle;
287+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.DisableSsl | Info | Session Id {0}", handle?.ConnectionId);
288+
handle.DisableSsl();
289+
return TdsEnums.SNI_SUCCESS;
290+
}
252291

253292
internal override uint EnableMars(ref uint info)
254293
{
@@ -263,9 +302,26 @@ internal override uint EnableMars(ref uint info)
263302
return TdsEnums.SNI_ERROR;
264303
}
265304

266-
internal override uint EnableSsl(ref uint info) => SNIProxy.GetInstance().EnableSsl(Handle, info);
305+
internal override uint EnableSsl(ref uint info)
306+
{
307+
SNIHandle handle = Handle;
308+
try
309+
{
310+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.EnableSsl | Info | Session Id {0}", handle?.ConnectionId);
311+
return handle.EnableSsl(info);
312+
}
313+
catch (Exception e)
314+
{
315+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1}", handle?.ConnectionId, e?.Message);
316+
return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, SNICommon.HandshakeFailureError, e);
317+
}
318+
}
267319

268-
internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize) => SNIProxy.GetInstance().SetConnectionBufferSize(Handle, unsignedPacketSize);
320+
internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
321+
{
322+
Handle.SetBufferSize((int)unsignedPacketSize);
323+
return TdsEnums.SNI_SUCCESS;
324+
}
269325

270326
internal override uint GenerateSspiClientContext(byte[] receivedBuff, uint receivedLength, ref byte[] sendBuff, ref uint sendLength, byte[][] _sniSpnBuffer)
271327
{
@@ -274,8 +330,8 @@ internal override uint GenerateSspiClientContext(byte[] receivedBuff, uint recei
274330
_sspiClientContextStatus = new SspiClientContextStatus();
275331
}
276332

277-
SNIProxy.GetInstance().GenSspiClientContext(_sspiClientContextStatus, receivedBuff, ref sendBuff, _sniSpnBuffer);
278-
SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GenerateSspiClientContext | Info | Session Id {0}", _sessionHandle?.ConnectionId);
333+
SNIProxy.GenSspiClientContext(_sspiClientContextStatus, receivedBuff, ref sendBuff, _sniSpnBuffer);
334+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObjectManaged.GenerateSspiClientContext | Info | Session Id {0}", _sessionHandle?.ConnectionId);
279335
sendLength = (uint)(sendBuff != null ? sendBuff.Length : 0);
280336
return 0;
281337
}

0 commit comments

Comments
 (0)