Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 Small Changes #2594

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Microsoft.Data.SqlClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4F3CD363-B1E6-4D6D-9466-97D78A56BE45}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
NuGet.config = NuGet.config
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlServer.Server", "Microsoft.SqlServer.Server\Microsoft.SqlServer.Server.csproj", "{A314812A-7820-4565-A2A8-ABBE391C11E4}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Product>Core $(BaseProduct)</Product>
<EnableTrimAnalyzer Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</EnableTrimAnalyzer>
<NoWarn>$(NoWarn);IL2026;IL2057;IL2072;IL2075</NoWarn>
<RootNamespace />
</PropertyGroup>
<PropertyGroup>
<TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
Expand Down Expand Up @@ -928,6 +929,7 @@
</EmbeddedResource>
<EmbeddedResource Include="$(CommonSourceRoot)Resources\$(ResxFileName).*.resx">
<Link>Resources\%(RecursiveDir)%(Filename)%(Extension)</Link>
<LogicalName>Microsoft.Data.SqlClient.Resources.%(Filename).resources</LogicalName>
benrr101 marked this conversation as resolved.
Show resolved Hide resolved
</EmbeddedResource>
<EmbeddedResource Include="Resources\Microsoft.Data.SqlClient.SqlMetaData.xml">
<LogicalName>Microsoft.Data.SqlClient.SqlMetaData.xml</LogicalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ internal static SNIHandle CreateConnectionHandle(
}

SNIHandle sniHandle = null;
switch (details._connectionProtocol)
switch (details.ResolvedProtocol)
{
case DataSource.Protocol.Admin:
case DataSource.Protocol.None: // default to using tcp if no protocol is provided
Expand All @@ -209,7 +209,7 @@ internal static SNIHandle CreateConnectionHandle(
sniHandle = CreateNpHandle(details, timeout, parallel, tlsFirst, hostNameInCertificate, serverCertificateFilename);
break;
default:
Debug.Fail($"Unexpected connection protocol: {details._connectionProtocol}");
Debug.Fail($"Unexpected connection protocol: {details.ResolvedProtocol}");
break;
}

Expand Down Expand Up @@ -245,11 +245,11 @@ private static byte[][] GetSqlServerSPNs(DataSource dataSource, string serverSPN
}
else if (!string.IsNullOrWhiteSpace(dataSource.InstanceName))
{
postfix = dataSource._connectionProtocol == DataSource.Protocol.TCP ? dataSource.ResolvedPort.ToString() : dataSource.InstanceName;
postfix = dataSource.ResolvedProtocol == DataSource.Protocol.TCP ? dataSource.ResolvedPort.ToString() : dataSource.InstanceName;
}

SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}", dataSource?.ServerName, dataSource?.InstanceName, dataSource?.Port, postfix);
return GetSqlServerSPNs(hostName, postfix, dataSource._connectionProtocol);
return GetSqlServerSPNs(hostName, postfix, dataSource.ResolvedProtocol);
}

private static byte[][] GetSqlServerSPNs(string hostNameOrAddress, string portOrInstanceName, DataSource.Protocol protocol)
Expand Down Expand Up @@ -327,7 +327,7 @@ private static SNITCPHandle CreateTcpHandle(
}

int port = -1;
bool isAdminConnection = details._connectionProtocol == DataSource.Protocol.Admin;
bool isAdminConnection = details.ResolvedProtocol == DataSource.Protocol.Admin;
if (details.IsSsrpRequired)
{
try
Expand Down Expand Up @@ -440,8 +440,6 @@ internal class DataSource

internal enum Protocol { TCP, NP, None, Admin };

internal Protocol _connectionProtocol = Protocol.None;

/// <summary>
/// Provides the HostName of the server to connect to for TCP protocol.
/// This information is also used for finding the SPN of SqlServer
Expand Down Expand Up @@ -473,6 +471,12 @@ internal enum Protocol { TCP, NP, None, Admin };
/// </summary>
internal string PipeHostName { get; private set; }

/// <summary>
/// Gets or sets the protocol that was resolved from the connection string. If this is
/// <see cref="Protocol.None"/>, the protocol could not reliably be determined.
/// </summary>
internal Protocol ResolvedProtocol { get; private set; }

private string _workingDataSource;
private string _dataSourceAfterTrimmingProtocol;

Expand All @@ -489,16 +493,16 @@ private DataSource(string dataSource)

PopulateProtocol();

_dataSourceAfterTrimmingProtocol = (firstIndexOfColon > -1) && _connectionProtocol != Protocol.None
_dataSourceAfterTrimmingProtocol = (firstIndexOfColon > -1) && ResolvedProtocol != Protocol.None
? _workingDataSource.Substring(firstIndexOfColon + 1).Trim() : _workingDataSource;

if (_dataSourceAfterTrimmingProtocol.Contains(Slash)) // Pipe paths only allow back slashes
{
if (_connectionProtocol == Protocol.None)
if (ResolvedProtocol == Protocol.None)
ReportSNIError(SNIProviders.INVALID_PROV);
else if (_connectionProtocol == Protocol.NP)
else if (ResolvedProtocol == Protocol.NP)
ReportSNIError(SNIProviders.NP_PROV);
else if (_connectionProtocol == Protocol.TCP)
else if (ResolvedProtocol == Protocol.TCP)
ReportSNIError(SNIProviders.TCP_PROV);
}
}
Expand All @@ -509,25 +513,25 @@ private void PopulateProtocol()

if (splitByColon.Length <= 1)
{
_connectionProtocol = Protocol.None;
ResolvedProtocol = Protocol.None;
}
else
{
// We trim before switching because " tcp : server , 1433 " is a valid data source
switch (splitByColon[0].Trim())
{
case TdsEnums.TCP:
_connectionProtocol = Protocol.TCP;
ResolvedProtocol = Protocol.TCP;
break;
case TdsEnums.NP:
_connectionProtocol = Protocol.NP;
ResolvedProtocol = Protocol.NP;
break;
case TdsEnums.ADMIN:
_connectionProtocol = Protocol.Admin;
ResolvedProtocol = Protocol.Admin;
break;
default:
// None of the supported protocols were found. This may be a IPv6 address
_connectionProtocol = Protocol.None;
ResolvedProtocol = Protocol.None;
break;
}
}
Expand Down Expand Up @@ -611,7 +615,7 @@ private void InferLocalServerName()
// If Server name is empty or localhost, then use "localhost"
if (string.IsNullOrEmpty(ServerName) || IsLocalHost(ServerName) ||
(Environment.MachineName.Equals(ServerName, StringComparison.CurrentCultureIgnoreCase) &&
_connectionProtocol == Protocol.Admin))
ResolvedProtocol == Protocol.Admin))
{
// For DAC use "localhost" instead of the server name.
ServerName = DefaultHostName;
Expand Down Expand Up @@ -643,11 +647,11 @@ private bool InferConnectionDetails()
}

// For Tcp and Only Tcp are parameters allowed.
if (_connectionProtocol == Protocol.None)
if (ResolvedProtocol == Protocol.None)
{
_connectionProtocol = Protocol.TCP;
ResolvedProtocol = Protocol.TCP;
}
else if (_connectionProtocol != Protocol.TCP)
else if (ResolvedProtocol != Protocol.TCP)
{
// Parameter has been specified for non-TCP protocol. This is not allowed.
ReportSNIError(SNIProviders.INVALID_PROV);
Expand Down Expand Up @@ -705,15 +709,15 @@ private void ReportSNIError(SNIProviders provider)
private bool InferNamedPipesInformation()
{
// If we have a datasource beginning with a pipe or we have already determined that the protocol is Named Pipe
if (_dataSourceAfterTrimmingProtocol.StartsWith(PipeBeginning, StringComparison.Ordinal) || _connectionProtocol == Protocol.NP)
if (_dataSourceAfterTrimmingProtocol.StartsWith(PipeBeginning, StringComparison.Ordinal) || ResolvedProtocol == Protocol.NP)
{
// If the data source starts with "np:servername"
if (!_dataSourceAfterTrimmingProtocol.Contains(PipeBeginning))
{
// Assuming that user did not change default NamedPipe name, if the datasource is in the format servername\instance,
// separate servername and instance and prepend instance with MSSQL$ and append default pipe path
// https://learn.microsoft.com/en-us/sql/tools/configuration-manager/named-pipes-properties?view=sql-server-ver16
if (_dataSourceAfterTrimmingProtocol.Contains(PathSeparator) && _connectionProtocol == Protocol.NP)
if (_dataSourceAfterTrimmingProtocol.Contains(PathSeparator) && ResolvedProtocol == Protocol.NP)
{
string[] tokensByBackSlash = _dataSourceAfterTrimmingProtocol.Split(BackSlashCharacter);
if (tokensByBackSlash.Length == 2)
Expand Down Expand Up @@ -800,11 +804,11 @@ private bool InferNamedPipesInformation()
}

// DataSource is something like "\\pipename"
if (_connectionProtocol == Protocol.None)
if (ResolvedProtocol == Protocol.None)
{
_connectionProtocol = Protocol.NP;
ResolvedProtocol = Protocol.NP;
}
else if (_connectionProtocol != Protocol.NP)
else if (ResolvedProtocol != Protocol.NP)
{
// In case the path began with a "\\" and protocol was not Named Pipes
ReportSNIError(SNIProviders.NP_PROV);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.Net.Sdk">
<PropertyGroup>
<ProjectGuid>{407890AC-9876-4FEF-A6F1-F36A876BAADE}</ProjectGuid>
<RootNamespace>Microsoft.Data.SqlClient</RootNamespace>
<RootNamespace></RootNamespace>
<TargetFramework>net462</TargetFramework>
<EnableLocalAppContext>true</EnableLocalAppContext>
<AssemblyName>Microsoft.Data.SqlClient</AssemblyName>
Expand Down Expand Up @@ -721,6 +721,7 @@
</EmbeddedResource>
<EmbeddedResource Include="$(CommonSourceRoot)Resources\$(ResxFileName).*.resx">
<Link>Resources\%(RecursiveDir)%(Filename)%(Extension)</Link>
<LogicalName>Microsoft.Data.SqlClient.Resources.%(Filename).resources</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Microsoft.Data.SqlClient.SqlMetaData.xml">
<LogicalName>Microsoft.Data.SqlClient.SqlMetaData.xml</LogicalName>
Expand Down
Loading