Skip to content

Commit 218e34f

Browse files
authored
Simplify SNIProxy (#934)
1 parent c99ce8d commit 218e34f

File tree

4 files changed

+87
-175
lines changed

4 files changed

+87
-175
lines changed

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

+7-158
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,9 @@ internal class SNIProxy
2121
private const int DefaultSqlServerDacPort = 1434;
2222
private const string SqlServerSpnHeader = "MSSQLSvc";
2323

24-
internal class SspiClientContextResult
25-
{
26-
internal const uint OK = 0;
27-
internal const uint Failed = 1;
28-
internal const uint KerberosTicketMissing = 2;
29-
}
30-
31-
internal static readonly SNIProxy s_singleton = new SNIProxy();
24+
private static readonly SNIProxy s_singleton = new SNIProxy();
3225

33-
internal static SNIProxy GetInstance() => s_singleton;
34-
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-
}
26+
internal static SNIProxy Instance => s_singleton;
6627

6728
/// <summary>
6829
/// Generate SSPI context
@@ -72,7 +33,7 @@ internal uint DisableSsl(SNIHandle handle)
7233
/// <param name="sendBuff">Send buffer</param>
7334
/// <param name="serverName">Service Principal Name buffer</param>
7435
/// <returns>SNI error code</returns>
75-
internal void GenSspiClientContext(SspiClientContextStatus sspiClientContextStatus, byte[] receivedBuff, ref byte[] sendBuff, byte[][] serverName)
36+
internal static void GenSspiClientContext(SspiClientContextStatus sspiClientContextStatus, byte[] receivedBuff, ref byte[] sendBuff, byte[][] serverName)
7637
{
7738
SafeDeleteContext securityContext = sspiClientContextStatus.SecurityContext;
7839
ContextFlagsPal contextFlags = sspiClientContextStatus.ContextFlags;
@@ -165,83 +126,6 @@ private static bool IsErrorStatus(SecurityStatusPalErrorCode errorCode)
165126
errorCode != SecurityStatusPalErrorCode.Renegotiate;
166127
}
167128

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-
245129
/// <summary>
246130
/// Create a SNI connection handle
247131
/// </summary>
@@ -258,7 +142,7 @@ internal uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync)
258142
/// <param name="cachedFQDN">Used for DNS Cache</param>
259143
/// <param name="pendingDNSInfo">Used for DNS Cache</param>
260144
/// <returns>SNI handle</returns>
261-
internal SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[][] spnBuffer,
145+
internal static SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[][] spnBuffer,
262146
bool flushCache, bool async, bool parallel, bool isIntegratedSecurity, SqlConnectionIPAddressPreference ipPreference, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
263147
{
264148
instanceName = new byte[1];
@@ -380,7 +264,7 @@ private static byte[][] GetSqlServerSPNs(string hostNameOrAddress, string portOr
380264
/// <param name="cachedFQDN">Key for DNS Cache</param>
381265
/// <param name="pendingDNSInfo">Used for DNS Cache</param>
382266
/// <returns>SNITCPHandle</returns>
383-
private SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, bool parallel, SqlConnectionIPAddressPreference ipPreference, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
267+
private static SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, bool parallel, SqlConnectionIPAddressPreference ipPreference, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo)
384268
{
385269
// TCP Format:
386270
// tcp:<host name>\<instance name>
@@ -421,16 +305,14 @@ private SNITCPHandle CreateTcpHandle(DataSource details, long timerExpire, bool
421305
return new SNITCPHandle(hostName, port, timerExpire, parallel, ipPreference, cachedFQDN, ref pendingDNSInfo);
422306
}
423307

424-
425-
426308
/// <summary>
427309
/// Creates an SNINpHandle object
428310
/// </summary>
429311
/// <param name="details">Data source</param>
430312
/// <param name="timerExpire">Timer expiration</param>
431313
/// <param name="parallel">Should MultiSubnetFailover be used. Only returns an error for named pipes.</param>
432314
/// <returns>SNINpHandle</returns>
433-
private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, bool parallel)
315+
private static SNINpHandle CreateNpHandle(DataSource details, long timerExpire, bool parallel)
434316
{
435317
if (parallel)
436318
{
@@ -441,39 +323,6 @@ private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, bool pa
441323
return new SNINpHandle(details.PipeHostName, details.PipeName, timerExpire);
442324
}
443325

444-
/// <summary>
445-
/// Read packet asynchronously
446-
/// </summary>
447-
/// <param name="handle">SNI handle</param>
448-
/// <param name="packet">Packet</param>
449-
/// <returns>SNI error status</returns>
450-
internal uint ReadAsync(SNIHandle handle, out SNIPacket packet)
451-
{
452-
packet = null;
453-
return handle.ReceiveAsync(ref packet);
454-
}
455-
456-
/// <summary>
457-
/// Set packet data
458-
/// </summary>
459-
/// <param name="packet">SNI packet</param>
460-
/// <param name="data">Data</param>
461-
/// <param name="length">Length</param>
462-
internal void PacketSetData(SNIPacket packet, byte[] data, int length)
463-
{
464-
packet.AppendData(data, length);
465-
}
466-
467-
/// <summary>
468-
/// Check SNI handle connection
469-
/// </summary>
470-
/// <param name="handle"></param>
471-
/// <returns>SNI error status</returns>
472-
internal uint CheckConnection(SNIHandle handle)
473-
{
474-
return handle.CheckConnection();
475-
}
476-
477326
/// <summary>
478327
/// Get last SNI error on this thread
479328
/// </summary>
@@ -489,7 +338,7 @@ internal SNIError GetLastError()
489338
/// <param name="fullServerName">The data source</param>
490339
/// <param name="error">Set true when an error occurred while getting LocalDB up</param>
491340
/// <returns></returns>
492-
private string GetLocalDBDataSource(string fullServerName, out bool error)
341+
private static string GetLocalDBDataSource(string fullServerName, out bool error)
493342
{
494343
string localDBConnectionString = null;
495344
bool isBadLocalDBDataSource;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void WaitForSSLHandShakeToComplete(ref uint error, ref int protocolVersi
2626
private SNIErrorDetails GetSniErrorDetails()
2727
{
2828
SNIErrorDetails details;
29-
SNIError sniError = SNIProxy.GetInstance().GetLastError();
29+
SNIError sniError = SNIProxy.Instance.GetLastError();
3030
details.sniErrorNumber = sniError.sniError;
3131
details.errorMessage = sniError.errorMessage;
3232
details.nativeError = sniError.nativeError;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private SNIErrorDetails GetSniErrorDetails()
9595

9696
if (TdsParserStateObjectFactory.UseManagedSNI)
9797
{
98-
SNIError sniError = SNIProxy.GetInstance().GetLastError();
98+
SNIError sniError = SNIProxy.Instance.GetLastError();
9999
details.sniErrorNumber = sniError.sniError;
100100
details.errorMessage = sniError.errorMessage;
101101
details.nativeError = sniError.nativeError;

0 commit comments

Comments
 (0)