@@ -197,7 +197,7 @@ internal static SNIHandle CreateConnectionHandle(
197197 }
198198
199199 SNIHandle sniHandle = null ;
200- switch ( details . _connectionProtocol )
200+ switch ( details . ResolvedProtocol )
201201 {
202202 case DataSource . Protocol . Admin :
203203 case DataSource . Protocol . None : // default to using tcp if no protocol is provided
@@ -209,7 +209,7 @@ internal static SNIHandle CreateConnectionHandle(
209209 sniHandle = CreateNpHandle ( details , timeout , parallel , tlsFirst , hostNameInCertificate , serverCertificateFilename ) ;
210210 break ;
211211 default :
212- Debug . Fail ( $ "Unexpected connection protocol: { details . _connectionProtocol } ") ;
212+ Debug . Fail ( $ "Unexpected connection protocol: { details . ResolvedProtocol } ") ;
213213 break ;
214214 }
215215
@@ -245,11 +245,11 @@ private static byte[][] GetSqlServerSPNs(DataSource dataSource, string serverSPN
245245 }
246246 else if ( ! string . IsNullOrWhiteSpace ( dataSource . InstanceName ) )
247247 {
248- postfix = dataSource . _connectionProtocol == DataSource . Protocol . TCP ? dataSource . ResolvedPort . ToString ( ) : dataSource . InstanceName ;
248+ postfix = dataSource . ResolvedProtocol == DataSource . Protocol . TCP ? dataSource . ResolvedPort . ToString ( ) : dataSource . InstanceName ;
249249 }
250250
251251 SqlClientEventSource . Log . TryTraceEvent ( "SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}" , dataSource ? . ServerName , dataSource ? . InstanceName , dataSource ? . Port , postfix ) ;
252- return GetSqlServerSPNs ( hostName , postfix , dataSource . _connectionProtocol ) ;
252+ return GetSqlServerSPNs ( hostName , postfix , dataSource . ResolvedProtocol ) ;
253253 }
254254
255255 private static byte [ ] [ ] GetSqlServerSPNs ( string hostNameOrAddress , string portOrInstanceName , DataSource . Protocol protocol )
@@ -327,7 +327,7 @@ private static SNITCPHandle CreateTcpHandle(
327327 }
328328
329329 int port = - 1 ;
330- bool isAdminConnection = details . _connectionProtocol == DataSource . Protocol . Admin ;
330+ bool isAdminConnection = details . ResolvedProtocol == DataSource . Protocol . Admin ;
331331 if ( details . IsSsrpRequired )
332332 {
333333 try
@@ -440,8 +440,6 @@ internal class DataSource
440440
441441 internal enum Protocol { TCP , NP , None , Admin } ;
442442
443- internal Protocol _connectionProtocol = Protocol . None ;
444-
445443 /// <summary>
446444 /// Provides the HostName of the server to connect to for TCP protocol.
447445 /// This information is also used for finding the SPN of SqlServer
@@ -473,6 +471,12 @@ internal enum Protocol { TCP, NP, None, Admin };
473471 /// </summary>
474472 internal string PipeHostName { get ; private set ; }
475473
474+ /// <summary>
475+ /// Gets or sets the protocol that was resolved from the connection string. If this is
476+ /// <see cref="Protocol.None"/>, the protocol could not reliably be determined.
477+ /// </summary>
478+ internal Protocol ResolvedProtocol { get ; private set ; }
479+
476480 private string _workingDataSource ;
477481 private string _dataSourceAfterTrimmingProtocol ;
478482
@@ -489,16 +493,16 @@ private DataSource(string dataSource)
489493
490494 PopulateProtocol ( ) ;
491495
492- _dataSourceAfterTrimmingProtocol = ( firstIndexOfColon > - 1 ) && _connectionProtocol != Protocol . None
496+ _dataSourceAfterTrimmingProtocol = ( firstIndexOfColon > - 1 ) && ResolvedProtocol != Protocol . None
493497 ? _workingDataSource . Substring ( firstIndexOfColon + 1 ) . Trim ( ) : _workingDataSource ;
494498
495499 if ( _dataSourceAfterTrimmingProtocol . Contains ( Slash ) ) // Pipe paths only allow back slashes
496500 {
497- if ( _connectionProtocol == Protocol . None )
501+ if ( ResolvedProtocol == Protocol . None )
498502 ReportSNIError ( SNIProviders . INVALID_PROV ) ;
499- else if ( _connectionProtocol == Protocol . NP )
503+ else if ( ResolvedProtocol == Protocol . NP )
500504 ReportSNIError ( SNIProviders . NP_PROV ) ;
501- else if ( _connectionProtocol == Protocol . TCP )
505+ else if ( ResolvedProtocol == Protocol . TCP )
502506 ReportSNIError ( SNIProviders . TCP_PROV ) ;
503507 }
504508 }
@@ -509,25 +513,25 @@ private void PopulateProtocol()
509513
510514 if ( splitByColon . Length <= 1 )
511515 {
512- _connectionProtocol = Protocol . None ;
516+ ResolvedProtocol = Protocol . None ;
513517 }
514518 else
515519 {
516520 // We trim before switching because " tcp : server , 1433 " is a valid data source
517521 switch ( splitByColon [ 0 ] . Trim ( ) )
518522 {
519523 case TdsEnums . TCP :
520- _connectionProtocol = Protocol . TCP ;
524+ ResolvedProtocol = Protocol . TCP ;
521525 break ;
522526 case TdsEnums . NP :
523- _connectionProtocol = Protocol . NP ;
527+ ResolvedProtocol = Protocol . NP ;
524528 break ;
525529 case TdsEnums . ADMIN :
526- _connectionProtocol = Protocol . Admin ;
530+ ResolvedProtocol = Protocol . Admin ;
527531 break ;
528532 default :
529533 // None of the supported protocols were found. This may be a IPv6 address
530- _connectionProtocol = Protocol . None ;
534+ ResolvedProtocol = Protocol . None ;
531535 break ;
532536 }
533537 }
@@ -611,7 +615,7 @@ private void InferLocalServerName()
611615 // If Server name is empty or localhost, then use "localhost"
612616 if ( string . IsNullOrEmpty ( ServerName ) || IsLocalHost ( ServerName ) ||
613617 ( Environment . MachineName . Equals ( ServerName , StringComparison . CurrentCultureIgnoreCase ) &&
614- _connectionProtocol == Protocol . Admin ) )
618+ ResolvedProtocol == Protocol . Admin ) )
615619 {
616620 // For DAC use "localhost" instead of the server name.
617621 ServerName = DefaultHostName ;
@@ -643,11 +647,11 @@ private bool InferConnectionDetails()
643647 }
644648
645649 // For Tcp and Only Tcp are parameters allowed.
646- if ( _connectionProtocol == Protocol . None )
650+ if ( ResolvedProtocol == Protocol . None )
647651 {
648- _connectionProtocol = Protocol . TCP ;
652+ ResolvedProtocol = Protocol . TCP ;
649653 }
650- else if ( _connectionProtocol != Protocol . TCP )
654+ else if ( ResolvedProtocol != Protocol . TCP )
651655 {
652656 // Parameter has been specified for non-TCP protocol. This is not allowed.
653657 ReportSNIError ( SNIProviders . INVALID_PROV ) ;
@@ -705,15 +709,15 @@ private void ReportSNIError(SNIProviders provider)
705709 private bool InferNamedPipesInformation ( )
706710 {
707711 // If we have a datasource beginning with a pipe or we have already determined that the protocol is Named Pipe
708- if ( _dataSourceAfterTrimmingProtocol . StartsWith ( PipeBeginning , StringComparison . Ordinal ) || _connectionProtocol == Protocol . NP )
712+ if ( _dataSourceAfterTrimmingProtocol . StartsWith ( PipeBeginning , StringComparison . Ordinal ) || ResolvedProtocol == Protocol . NP )
709713 {
710714 // If the data source starts with "np:servername"
711715 if ( ! _dataSourceAfterTrimmingProtocol . Contains ( PipeBeginning ) )
712716 {
713717 // Assuming that user did not change default NamedPipe name, if the datasource is in the format servername\instance,
714718 // separate servername and instance and prepend instance with MSSQL$ and append default pipe path
715719 // https://learn.microsoft.com/en-us/sql/tools/configuration-manager/named-pipes-properties?view=sql-server-ver16
716- if ( _dataSourceAfterTrimmingProtocol . Contains ( PathSeparator ) && _connectionProtocol == Protocol . NP )
720+ if ( _dataSourceAfterTrimmingProtocol . Contains ( PathSeparator ) && ResolvedProtocol == Protocol . NP )
717721 {
718722 string [ ] tokensByBackSlash = _dataSourceAfterTrimmingProtocol . Split ( BackSlashCharacter ) ;
719723 if ( tokensByBackSlash . Length == 2 )
@@ -800,11 +804,11 @@ private bool InferNamedPipesInformation()
800804 }
801805
802806 // DataSource is something like "\\pipename"
803- if ( _connectionProtocol == Protocol . None )
807+ if ( ResolvedProtocol == Protocol . None )
804808 {
805- _connectionProtocol = Protocol . NP ;
809+ ResolvedProtocol = Protocol . NP ;
806810 }
807- else if ( _connectionProtocol != Protocol . NP )
811+ else if ( ResolvedProtocol != Protocol . NP )
808812 {
809813 // In case the path began with a "\\" and protocol was not Named Pipes
810814 ReportSNIError ( SNIProviders . NP_PROV ) ;
0 commit comments