@@ -197,7 +197,7 @@ internal static SNIHandle CreateConnectionHandle(
197
197
}
198
198
199
199
SNIHandle sniHandle = null ;
200
- switch ( details . _connectionProtocol )
200
+ switch ( details . ResolvedProtocol )
201
201
{
202
202
case DataSource . Protocol . Admin :
203
203
case DataSource . Protocol . None : // default to using tcp if no protocol is provided
@@ -209,7 +209,7 @@ internal static SNIHandle CreateConnectionHandle(
209
209
sniHandle = CreateNpHandle ( details , timeout , parallel , tlsFirst , hostNameInCertificate , serverCertificateFilename ) ;
210
210
break ;
211
211
default :
212
- Debug . Fail ( $ "Unexpected connection protocol: { details . _connectionProtocol } ") ;
212
+ Debug . Fail ( $ "Unexpected connection protocol: { details . ResolvedProtocol } ") ;
213
213
break ;
214
214
}
215
215
@@ -245,11 +245,11 @@ private static byte[][] GetSqlServerSPNs(DataSource dataSource, string serverSPN
245
245
}
246
246
else if ( ! string . IsNullOrWhiteSpace ( dataSource . InstanceName ) )
247
247
{
248
- postfix = dataSource . _connectionProtocol == DataSource . Protocol . TCP ? dataSource . ResolvedPort . ToString ( ) : dataSource . InstanceName ;
248
+ postfix = dataSource . ResolvedProtocol == DataSource . Protocol . TCP ? dataSource . ResolvedPort . ToString ( ) : dataSource . InstanceName ;
249
249
}
250
250
251
251
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 ) ;
253
253
}
254
254
255
255
private static byte [ ] [ ] GetSqlServerSPNs ( string hostNameOrAddress , string portOrInstanceName , DataSource . Protocol protocol )
@@ -327,7 +327,7 @@ private static SNITCPHandle CreateTcpHandle(
327
327
}
328
328
329
329
int port = - 1 ;
330
- bool isAdminConnection = details . _connectionProtocol == DataSource . Protocol . Admin ;
330
+ bool isAdminConnection = details . ResolvedProtocol == DataSource . Protocol . Admin ;
331
331
if ( details . IsSsrpRequired )
332
332
{
333
333
try
@@ -440,8 +440,6 @@ internal class DataSource
440
440
441
441
internal enum Protocol { TCP , NP , None , Admin } ;
442
442
443
- internal Protocol _connectionProtocol = Protocol . None ;
444
-
445
443
/// <summary>
446
444
/// Provides the HostName of the server to connect to for TCP protocol.
447
445
/// This information is also used for finding the SPN of SqlServer
@@ -473,6 +471,12 @@ internal enum Protocol { TCP, NP, None, Admin };
473
471
/// </summary>
474
472
internal string PipeHostName { get ; private set ; }
475
473
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
+
476
480
private string _workingDataSource ;
477
481
private string _dataSourceAfterTrimmingProtocol ;
478
482
@@ -489,16 +493,16 @@ private DataSource(string dataSource)
489
493
490
494
PopulateProtocol ( ) ;
491
495
492
- _dataSourceAfterTrimmingProtocol = ( firstIndexOfColon > - 1 ) && _connectionProtocol != Protocol . None
496
+ _dataSourceAfterTrimmingProtocol = ( firstIndexOfColon > - 1 ) && ResolvedProtocol != Protocol . None
493
497
? _workingDataSource . Substring ( firstIndexOfColon + 1 ) . Trim ( ) : _workingDataSource ;
494
498
495
499
if ( _dataSourceAfterTrimmingProtocol . Contains ( Slash ) ) // Pipe paths only allow back slashes
496
500
{
497
- if ( _connectionProtocol == Protocol . None )
501
+ if ( ResolvedProtocol == Protocol . None )
498
502
ReportSNIError ( SNIProviders . INVALID_PROV ) ;
499
- else if ( _connectionProtocol == Protocol . NP )
503
+ else if ( ResolvedProtocol == Protocol . NP )
500
504
ReportSNIError ( SNIProviders . NP_PROV ) ;
501
- else if ( _connectionProtocol == Protocol . TCP )
505
+ else if ( ResolvedProtocol == Protocol . TCP )
502
506
ReportSNIError ( SNIProviders . TCP_PROV ) ;
503
507
}
504
508
}
@@ -509,25 +513,25 @@ private void PopulateProtocol()
509
513
510
514
if ( splitByColon . Length <= 1 )
511
515
{
512
- _connectionProtocol = Protocol . None ;
516
+ ResolvedProtocol = Protocol . None ;
513
517
}
514
518
else
515
519
{
516
520
// We trim before switching because " tcp : server , 1433 " is a valid data source
517
521
switch ( splitByColon [ 0 ] . Trim ( ) )
518
522
{
519
523
case TdsEnums . TCP :
520
- _connectionProtocol = Protocol . TCP ;
524
+ ResolvedProtocol = Protocol . TCP ;
521
525
break ;
522
526
case TdsEnums . NP :
523
- _connectionProtocol = Protocol . NP ;
527
+ ResolvedProtocol = Protocol . NP ;
524
528
break ;
525
529
case TdsEnums . ADMIN :
526
- _connectionProtocol = Protocol . Admin ;
530
+ ResolvedProtocol = Protocol . Admin ;
527
531
break ;
528
532
default :
529
533
// None of the supported protocols were found. This may be a IPv6 address
530
- _connectionProtocol = Protocol . None ;
534
+ ResolvedProtocol = Protocol . None ;
531
535
break ;
532
536
}
533
537
}
@@ -611,7 +615,7 @@ private void InferLocalServerName()
611
615
// If Server name is empty or localhost, then use "localhost"
612
616
if ( string . IsNullOrEmpty ( ServerName ) || IsLocalHost ( ServerName ) ||
613
617
( Environment . MachineName . Equals ( ServerName , StringComparison . CurrentCultureIgnoreCase ) &&
614
- _connectionProtocol == Protocol . Admin ) )
618
+ ResolvedProtocol == Protocol . Admin ) )
615
619
{
616
620
// For DAC use "localhost" instead of the server name.
617
621
ServerName = DefaultHostName ;
@@ -643,11 +647,11 @@ private bool InferConnectionDetails()
643
647
}
644
648
645
649
// For Tcp and Only Tcp are parameters allowed.
646
- if ( _connectionProtocol == Protocol . None )
650
+ if ( ResolvedProtocol == Protocol . None )
647
651
{
648
- _connectionProtocol = Protocol . TCP ;
652
+ ResolvedProtocol = Protocol . TCP ;
649
653
}
650
- else if ( _connectionProtocol != Protocol . TCP )
654
+ else if ( ResolvedProtocol != Protocol . TCP )
651
655
{
652
656
// Parameter has been specified for non-TCP protocol. This is not allowed.
653
657
ReportSNIError ( SNIProviders . INVALID_PROV ) ;
@@ -705,15 +709,15 @@ private void ReportSNIError(SNIProviders provider)
705
709
private bool InferNamedPipesInformation ( )
706
710
{
707
711
// 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 )
709
713
{
710
714
// If the data source starts with "np:servername"
711
715
if ( ! _dataSourceAfterTrimmingProtocol . Contains ( PipeBeginning ) )
712
716
{
713
717
// Assuming that user did not change default NamedPipe name, if the datasource is in the format servername\instance,
714
718
// separate servername and instance and prepend instance with MSSQL$ and append default pipe path
715
719
// 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 )
717
721
{
718
722
string [ ] tokensByBackSlash = _dataSourceAfterTrimmingProtocol . Split ( BackSlashCharacter ) ;
719
723
if ( tokensByBackSlash . Length == 2 )
@@ -800,11 +804,11 @@ private bool InferNamedPipesInformation()
800
804
}
801
805
802
806
// DataSource is something like "\\pipename"
803
- if ( _connectionProtocol == Protocol . None )
807
+ if ( ResolvedProtocol == Protocol . None )
804
808
{
805
- _connectionProtocol = Protocol . NP ;
809
+ ResolvedProtocol = Protocol . NP ;
806
810
}
807
- else if ( _connectionProtocol != Protocol . NP )
811
+ else if ( ResolvedProtocol != Protocol . NP )
808
812
{
809
813
// In case the path began with a "\\" and protocol was not Named Pipes
810
814
ReportSNIError ( SNIProviders . NP_PROV ) ;
0 commit comments