From ac78b275989b65ef1632530a37b2d45ec27e133c Mon Sep 17 00:00:00 2001 From: Johnny Pham Date: Mon, 21 Jun 2021 13:42:20 -0700 Subject: [PATCH 1/3] modify error code --- .../Microsoft/Data/SqlClient/SNI/SNICommon.cs | 7 ++++--- .../Microsoft/Data/SqlClient/SNI/SNIError.cs | 7 +++++-- .../Data/SqlClient/SNI/SNITcpHandle.cs | 19 +++++++++++-------- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 6 +++--- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs index 8ae171fc68..6373a57242 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs @@ -102,7 +102,7 @@ internal enum SNISMUXFlags internal class SNICommon { private const string s_className = nameof(SNICommon); - + // Each error number maps to SNI_ERROR_* in String.resx internal const int ConnTerminatedError = 2; internal const int InvalidParameterError = 5; @@ -220,11 +220,12 @@ internal static uint ReportSNIError(SNIProviders provider, uint nativeError, uin /// SNI provider /// SNI error code /// SNI Exception + /// Native SNI error code /// - internal static uint ReportSNIError(SNIProviders provider, uint sniError, Exception sniException) + internal static uint ReportSNIError(SNIProviders provider, uint sniError, Exception sniException, uint nativeErrorCode = 0) { SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Provider = {0}, SNI Error = {1}, Exception = {2}", args0: provider, args1: sniError, args2: sniException?.Message); - return ReportSNIError(new SNIError(provider, sniError, sniException)); + return ReportSNIError(new SNIError(provider, sniError, sniException, nativeErrorCode)); } /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs index 412efac189..c7481838b0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs @@ -11,6 +11,9 @@ namespace Microsoft.Data.SqlClient.SNI /// internal class SNIError { + // Error numbers from native SNI implementation + internal const uint CertificateValidationErrorCode = 2148074277; + public readonly SNIProviders provider; public readonly string errorMessage; public readonly uint nativeError; @@ -30,12 +33,12 @@ public SNIError(SNIProviders provider, uint nativeError, uint sniErrorCode, stri this.exception = null; } - public SNIError(SNIProviders provider, uint sniErrorCode, Exception sniException) + public SNIError(SNIProviders provider, uint sniErrorCode, Exception sniException, uint nativeErrorCode = 0) { this.lineNumber = 0; this.function = string.Empty; this.provider = provider; - this.nativeError = 0; + this.nativeError = nativeErrorCode; this.sniError = sniErrorCode; this.errorMessage = string.Empty; this.exception = sniException; diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index d2a8341c0f..ae99218314 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -179,10 +179,13 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel string firstCachedIP; string secondCachedIP; - if (SqlConnectionIPAddressPreference.IPv6First == ipPreference) { + if (SqlConnectionIPAddressPreference.IPv6First == ipPreference) + { firstCachedIP = cachedDNSInfo.AddrIPv6; secondCachedIP = cachedDNSInfo.AddrIPv4; - } else { + } + else + { firstCachedIP = cachedDNSInfo.AddrIPv4; secondCachedIP = cachedDNSInfo.AddrIPv6; } @@ -339,8 +342,8 @@ private static Socket Connect(string serverName, int port, TimeSpan timeout, boo IPAddress[] ipAddresses = Dns.GetHostAddresses(serverName); string IPv4String = null; - string IPv6String = null; - + string IPv6String = null; + // Returning null socket is handled by the caller function. if (ipAddresses == null || ipAddresses.Length == 0) { @@ -434,7 +437,7 @@ private static Socket Connect(string serverName, int port, TimeSpan timeout, boo // If we have already got a valid Socket, or the platform default was prefered // we won't do the second traversal. - if (availableSocket != null || ipPreference == SqlConnectionIPAddressPreference.UsePlatformDefault) + if (availableSocket != null || ipPreference == SqlConnectionIPAddressPreference.UsePlatformDefault) { break; } @@ -590,7 +593,7 @@ public override uint EnableSsl(uint options) catch (AuthenticationException aue) { SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Authentication exception occurred: {1}", args0: _connectionId, args1: aue?.Message); - return ReportTcpSNIError(aue); + return ReportTcpSNIError(aue, SNIError.CertificateValidationErrorCode); } catch (InvalidOperationException ioe) { @@ -882,10 +885,10 @@ public override uint CheckConnection() return TdsEnums.SNI_SUCCESS; } - private uint ReportTcpSNIError(Exception sniException) + private uint ReportTcpSNIError(Exception sniException, uint nativeErrorCode = 0) { _status = TdsEnums.SNI_ERROR; - return SNICommon.ReportSNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, sniException); + return SNICommon.ReportSNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, sniException, nativeErrorCode); } private uint ReportTcpSNIError(uint nativeError, uint sniError, string errorMessage) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 204b736715..f6efc81c74 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -482,7 +482,7 @@ internal void Connect( // On Instance failure re-connect and flush SNI named instance cache. _physicalStateObj.SniContext = SniContext.Snix_Connect; - _physicalStateObj.CreatePhysicalSNIHandle(serverInfo.ExtendedServerName, ignoreSniOpenTimeout, timerExpire, out instanceName, ref _sniSpnBuffer, true, true, fParallel, + _physicalStateObj.CreatePhysicalSNIHandle(serverInfo.ExtendedServerName, ignoreSniOpenTimeout, timerExpire, out instanceName, ref _sniSpnBuffer, true, true, fParallel, _connHandler.ConnectionOptions.IPAddressPreference, FQDNforDNSCahce, ref _connHandler.pendingSQLDNSObject, integratedSecurity); if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status) @@ -1432,8 +1432,8 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj) SqlClientEventSource.Log.TryAdvancedTraceErrorEvent(" SNI Error Message. Native Error = {0}, Line Number ={1}, Function ={2}, Exception ={3}, Server = {4}", (int)details.nativeError, (int)details.lineNumber, details.function, details.exception, _server); - return new SqlError((int)details.nativeError, 0x00, TdsEnums.FATAL_ERROR_CLASS, - _server, errorMessage, details.function, (int)details.lineNumber, details.nativeError, details.exception); + return new SqlError(infoNumber: (int)details.nativeError, errorState: 0x00, TdsEnums.FATAL_ERROR_CLASS, _server, + errorMessage, details.function, (int)details.lineNumber, win32ErrorCode: details.nativeError, details.exception); } finally { From 40984d287cf25d150164f3e10db6d5cbd9a542b8 Mon Sep 17 00:00:00 2001 From: Johnny Pham Date: Tue, 6 Jul 2021 13:18:08 -0700 Subject: [PATCH 2/3] Update SNIError.cs --- .../Microsoft/Data/SqlClient/SNI/SNIError.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs index c7481838b0..080e274f94 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIError.cs @@ -24,24 +24,24 @@ internal class SNIError public SNIError(SNIProviders provider, uint nativeError, uint sniErrorCode, string errorMessage) { - this.lineNumber = 0; - this.function = string.Empty; + lineNumber = 0; + function = string.Empty; this.provider = provider; this.nativeError = nativeError; - this.sniError = sniErrorCode; + sniError = sniErrorCode; this.errorMessage = errorMessage; - this.exception = null; + exception = null; } public SNIError(SNIProviders provider, uint sniErrorCode, Exception sniException, uint nativeErrorCode = 0) { - this.lineNumber = 0; - this.function = string.Empty; + lineNumber = 0; + function = string.Empty; this.provider = provider; - this.nativeError = nativeErrorCode; - this.sniError = sniErrorCode; - this.errorMessage = string.Empty; - this.exception = sniException; + nativeError = nativeErrorCode; + sniError = sniErrorCode; + errorMessage = string.Empty; + exception = sniException; } } } From ae5561779acd2e464279b0813fea6b4cd3accf62 Mon Sep 17 00:00:00 2001 From: Johnny Pham Date: Tue, 6 Jul 2021 13:18:29 -0700 Subject: [PATCH 3/3] Update src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs Co-authored-by: Javad --- .../netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index ae99218314..a5c91bd778 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -437,7 +437,7 @@ private static Socket Connect(string serverName, int port, TimeSpan timeout, boo // If we have already got a valid Socket, or the platform default was prefered // we won't do the second traversal. - if (availableSocket != null || ipPreference == SqlConnectionIPAddressPreference.UsePlatformDefault) + if (availableSocket is not null || ipPreference == SqlConnectionIPAddressPreference.UsePlatformDefault) { break; }