Skip to content

Commit 596ee25

Browse files
committed
Move AssignPendingDNSInfo from TdsParser, merge
1 parent 469d840 commit 596ee25

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ internal abstract void CreatePhysicalSNIHandle(
8484
string hostNameInCertificate = "",
8585
string serverCertificateFilename = "");
8686

87-
internal abstract void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo);
88-
8987
protected abstract void FreeGcHandle(int remaining, bool release);
9088

9189
internal abstract uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename);

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ protected override void CreateSessionHandle(TdsParserStateObject physicalConnect
8383
_sessionHandle = new SNIHandle(myInfo, nativeSNIObject.Handle, _parser.Connection.ConnectionOptions.IPAddressPreference, cachedDNSInfo);
8484
}
8585

86+
// Retrieve the IP and port number from native SNI for TCP protocol. The IP information is stored temporarily in the
87+
// pendingSQLDNSObject but not in the DNS Cache at this point. We only add items to the DNS Cache after we receive the
88+
// IsSupported flag as true in the feature ext ack from server.
8689
internal override void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo)
8790
{
8891
uint result;

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ internal void Connect(ServerInfo serverInfo,
559559
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
560560

561561
// for DNS Caching phase 1
562-
AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache);
562+
_physicalStateObj.AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache, ref _connHandler.pendingSQLDNSObject);
563563

564564
if (!ClientOSEncryptionSupport)
565565
{
@@ -626,7 +626,7 @@ internal void Connect(ServerInfo serverInfo,
626626
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Sending prelogin handshake");
627627

628628
// for DNS Caching phase 1
629-
AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache);
629+
_physicalStateObj.AssignPendingDNSInfo(serverInfo.UserProtocol, FQDNforDNSCache, ref _connHandler.pendingSQLDNSObject);
630630

631631
SendPreLoginHandshake(instanceName, encrypt, integratedSecurity, serverCertificateFilename);
632632
status = ConsumePreLoginHandshake(

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.netfx.cs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,60 +44,6 @@ internal void BestEffortCleanup()
4444
}
4545
}
4646

47-
// Retrieve the IP and port number from native SNI for TCP protocol. The IP information is stored temporarily in the
48-
// pendingSQLDNSObject but not in the DNS Cache at this point. We only add items to the DNS Cache after we receive the
49-
// IsSupported flag as true in the feature ext ack from server.
50-
internal void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey)
51-
{
52-
uint result;
53-
ushort portFromSNI = 0;
54-
string IPStringFromSNI = string.Empty;
55-
IPAddress IPFromSNI;
56-
isTcpProtocol = false;
57-
Provider providerNumber = Provider.INVALID_PROV;
58-
59-
if (string.IsNullOrEmpty(userProtocol))
60-
{
61-
62-
result = SniNativeWrapper.SniGetProviderNumber(_physicalStateObj.Handle, ref providerNumber);
63-
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber");
64-
isTcpProtocol = (providerNumber == Provider.TCP_PROV);
65-
}
66-
else if (userProtocol == TdsEnums.TCP)
67-
{
68-
isTcpProtocol = true;
69-
}
70-
71-
// serverInfo.UserProtocol could be empty
72-
if (isTcpProtocol)
73-
{
74-
result = SniNativeWrapper.SniGetConnectionPort(_physicalStateObj.Handle, ref portFromSNI);
75-
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort");
76-
77-
78-
result = SniNativeWrapper.SniGetConnectionIpString(_physicalStateObj.Handle, ref IPStringFromSNI);
79-
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString");
80-
81-
_connHandler.pendingSQLDNSObject = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString());
82-
83-
if (IPAddress.TryParse(IPStringFromSNI, out IPFromSNI))
84-
{
85-
if (System.Net.Sockets.AddressFamily.InterNetwork == IPFromSNI.AddressFamily)
86-
{
87-
_connHandler.pendingSQLDNSObject.AddrIPv4 = IPStringFromSNI;
88-
}
89-
else if (System.Net.Sockets.AddressFamily.InterNetworkV6 == IPFromSNI.AddressFamily)
90-
{
91-
_connHandler.pendingSQLDNSObject.AddrIPv6 = IPStringFromSNI;
92-
}
93-
}
94-
}
95-
else
96-
{
97-
_connHandler.pendingSQLDNSObject = null;
98-
}
99-
}
100-
10147
internal bool RunReliably(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
10248
{
10349
RuntimeHelpers.PrepareConstrainedRegions();

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Net;
89
using System.Runtime.CompilerServices;
910
using System.Threading.Tasks;
1011
using Interop.Windows.Sni;
@@ -52,6 +53,59 @@ protected override void CreateSessionHandle(TdsParserStateObject physicalConnect
5253
_sessionHandle = new SNIHandle(myInfo, nativeSNIObject.Handle, _parser.Connection.ConnectionOptions.IPAddressPreference, cachedDNSInfo);
5354
}
5455

56+
// Retrieve the IP and port number from native SNI for TCP protocol. The IP information is stored temporarily in the
57+
// pendingSQLDNSObject but not in the DNS Cache at this point. We only add items to the DNS Cache after we receive the
58+
// IsSupported flag as true in the feature ext ack from server.
59+
internal override void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo)
60+
{
61+
uint result;
62+
ushort portFromSNI = 0;
63+
string IPStringFromSNI = string.Empty;
64+
IPAddress IPFromSNI;
65+
_parser.isTcpProtocol = false;
66+
Provider providerNumber = Provider.INVALID_PROV;
67+
68+
if (string.IsNullOrEmpty(userProtocol))
69+
{
70+
71+
result = SniNativeWrapper.SniGetProviderNumber(Handle, ref providerNumber);
72+
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber");
73+
_parser.isTcpProtocol = (providerNumber == Provider.TCP_PROV);
74+
}
75+
else if (userProtocol == TdsEnums.TCP)
76+
{
77+
_parser.isTcpProtocol = true;
78+
}
79+
80+
// serverInfo.UserProtocol could be empty
81+
if (_parser.isTcpProtocol)
82+
{
83+
result = SniNativeWrapper.SniGetConnectionPort(Handle, ref portFromSNI);
84+
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort");
85+
86+
result = SniNativeWrapper.SniGetConnectionIpString(Handle, ref IPStringFromSNI);
87+
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString");
88+
89+
pendingDNSInfo = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString());
90+
91+
if (IPAddress.TryParse(IPStringFromSNI, out IPFromSNI))
92+
{
93+
if (System.Net.Sockets.AddressFamily.InterNetwork == IPFromSNI.AddressFamily)
94+
{
95+
pendingDNSInfo.AddrIPv4 = IPStringFromSNI;
96+
}
97+
else if (System.Net.Sockets.AddressFamily.InterNetworkV6 == IPFromSNI.AddressFamily)
98+
{
99+
pendingDNSInfo.AddrIPv6 = IPStringFromSNI;
100+
}
101+
}
102+
}
103+
else
104+
{
105+
pendingDNSInfo = null;
106+
}
107+
}
108+
55109
protected override uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
56110
{
57111
Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ internal long TimeoutTime
485485

486486
protected abstract void CreateSessionHandle(TdsParserStateObject physicalConnection, bool async);
487487

488+
internal abstract void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey, ref SQLDNSInfo pendingDNSInfo);
489+
488490
internal abstract PacketHandle GetResetWritePacket(int dataSize);
489491

490492
protected abstract uint SniPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize);

0 commit comments

Comments
 (0)