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