-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Allow . as hostname in pipe names #17284
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,14 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Security; | ||
| using System.Net.Sockets; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using System; | ||
| using System.Threading; | ||
| using System.Collections; | ||
| using System.IO; | ||
|
|
||
| namespace System.Data.SqlClient.SNI | ||
| { | ||
|
|
@@ -575,7 +572,6 @@ internal class DataSource | |
|
|
||
| private const char CommaSeparator = ','; | ||
| private const char BackSlashSeparator = '\\'; | ||
| private const char ForwardSlashSeparator = '/'; | ||
| private const string DefaultHostName = "localhost"; | ||
| private const string DefaultSqlServerInstanceName = "mssqlserver"; | ||
| private const string PipeBeginning = @"\\"; | ||
|
|
@@ -786,32 +782,44 @@ private bool InferNamedPipesInformation() | |
|
|
||
| try | ||
| { | ||
| Uri uri = new Uri(_dataSourceAfterTrimmingProtocol); | ||
| if (string.IsNullOrEmpty(uri.Host)) | ||
| string[] tokensByBackSlash = _dataSourceAfterTrimmingProtocol.Split(BackSlashSeparator); | ||
|
|
||
| // The datasource is of the format \\host\pipe\sql\query [0]\[1]\[2]\[3]\[4]\[5] | ||
| // It would at least have 6 parts. | ||
| // Another valid Sql named pipe for an named instance is \\.\pipe\MSSQL$MYINSTANCE\sql\query | ||
| if (tokensByBackSlash.Length < 6) | ||
| { | ||
| ReportSNIError(SNIProviders.NP_PROV); | ||
| return false; | ||
| } | ||
|
|
||
| string[] absolutePathParts = uri.AbsolutePath.Split(ForwardSlashSeparator); | ||
| string host = tokensByBackSlash[2]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. Np:\.\pipe\sql\query would lead to a successful connection but np:\. \pipe\sql\query doesn't lead to a successful connection in Native SNI. |
||
|
|
||
| //Check if the "pipe" keyword is the first part of path | ||
| if (PipeToken.CompareTo(absolutePathParts[1]) != 0) | ||
| if (string.IsNullOrEmpty(host)) | ||
| { | ||
| ReportSNIError(SNIProviders.NP_PROV); | ||
| return false; | ||
| } | ||
|
|
||
| // There should be at least 4 parts in the pipename e.g /pipe/sql/query [0]/[1]/[2]/[3] | ||
| // Another valid Sql named pipe for an named instance is \\.\pipe\MSSQL$MYINSTANCE\sql\query | ||
| if (absolutePathParts.Length < 4) | ||
| //Check if the "pipe" keyword is the first part of path | ||
| if (!PipeToken.Equals(tokensByBackSlash[3])) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No trimming of pipe paths. |
||
| { | ||
| ReportSNIError(SNIProviders.NP_PROV); | ||
| return false; | ||
| } | ||
|
|
||
| PipeName = uri.AbsolutePath.Substring(PipeToken.Length + 2); | ||
| ServerName = IsLocalHost(uri.Host) ? Environment.MachineName : uri.Host; | ||
| StringBuilder pipeNameBuilder = new StringBuilder(); | ||
|
|
||
| for ( int i = 4; i < tokensByBackSlash.Length-1; i++) | ||
| { | ||
| pipeNameBuilder.Append(tokensByBackSlash[i]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| pipeNameBuilder.Append(Path.PathSeparator); | ||
| } | ||
| // Append the last part without a "/" | ||
| pipeNameBuilder.Append(tokensByBackSlash[tokensByBackSlash.Length - 1]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if connection string is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SqlClient should fail to connect in this case. |
||
|
|
||
| PipeName = pipeNameBuilder.ToString(); | ||
| ServerName = IsLocalHost(host) ? Environment.MachineName : host; | ||
| } | ||
| catch (UriFormatException) | ||
| { | ||
|
|
@@ -820,7 +828,7 @@ private bool InferNamedPipesInformation() | |
| } | ||
|
|
||
| // DataSource is something like "\\pipename" | ||
| if (ConnectionProtocol != DataSource.Protocol.None) | ||
| if (ConnectionProtocol == DataSource.Protocol.None) | ||
| { | ||
| ConnectionProtocol = DataSource.Protocol.NP; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for leading backslashes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard this, there's a check for it higher up.