Skip to content

Enable User Agent Extension #3530

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

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlVector.cs">
<Link>Microsoft\Data\SqlTypes\SqlVector.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfo.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfo.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Resources\ResCategoryAttribute.cs">
<Link>Resources\ResCategoryAttribute.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ internal bool IsDNSCachingBeforeRedirectSupported
internal bool IsJsonSupportEnabled = false;

// User Agent Flag
internal bool IsUserAgentEnabled = true;
internal bool IsUserAgentSupportEnabled = true;

// Vector Support Flag
internal bool IsVectorSupportEnabled = false;
Expand Down Expand Up @@ -1432,6 +1432,8 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword,
requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching;
requestedFeatures |= TdsEnums.FeatureExtension.JsonSupport;
requestedFeatures |= TdsEnums.FeatureExtension.VectorSupport;
requestedFeatures |= TdsEnums.FeatureExtension.UserAgent;


#if DEBUG
requestedFeatures |= TdsEnums.FeatureExtension.UserAgent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
using Microsoft.Data.SqlClient.DataClassification;
using Microsoft.Data.SqlClient.LocalDb;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.SqlClient.UserAgent;

#if NETFRAMEWORK
using Microsoft.Data.SqlTypes;
#endif
Expand Down Expand Up @@ -8875,8 +8877,16 @@ private void WriteLoginData(SqlLogin rec,
_physicalStateObj.WriteByteArray(encryptedChangePassword, encryptedChangePasswordLengthInBytes, 0);
}
}

ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length, true);

ApplyFeatureExData(
requestedFeatures,
recoverySessionData,
fedAuthFeatureExtensionData,
UserAgentInfo.GetCachedPayload(),
useFeatureExt,
length,
true
);
}
catch (Exception e)
{
Expand All @@ -8895,12 +8905,13 @@ private void WriteLoginData(SqlLogin rec,
private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
SessionData recoverySessionData,
FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData,
byte[] userAgentJsonPayload,
bool useFeatureExt,
int length,
bool write = false)
{
if (useFeatureExt)
{
{
if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0)
{
length += WriteSessionRecoveryFeatureRequest(recoverySessionData, write);
Expand Down Expand Up @@ -8947,6 +8958,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
length += WriteVectorSupportFeatureRequest(write);
}

if ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0)
{
length += WriteUserAgentFeatureRequest(userAgentJsonPayload, write);
}

length++; // for terminator
if (write)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,12 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlVector.cs">
<Link>Microsoft\Data\SqlTypes\SqlVector.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfo.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfo.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Resources\ResDescriptionAttribute.cs">
<Link>Resources\ResDescriptionAttribute.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ internal bool IsDNSCachingBeforeRedirectSupported
internal bool IsVectorSupportEnabled = false;

// User Agent Flag
internal bool IsUserAgentEnabled = true;
internal bool IsUserAgentSupportEnabled = true;

// TCE flags
internal byte _tceVersionSupported;
Expand Down Expand Up @@ -1438,10 +1438,7 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword,
requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching;
requestedFeatures |= TdsEnums.FeatureExtension.JsonSupport;
requestedFeatures |= TdsEnums.FeatureExtension.VectorSupport;

#if DEBUG
requestedFeatures |= TdsEnums.FeatureExtension.UserAgent;
#endif

_parser.TdsLogin(login, requestedFeatures, _recoverySessionData, _fedAuthFeatureExtensionData, encrypt);
}
Expand Down Expand Up @@ -3072,6 +3069,25 @@ internal void OnFeatureExtAck(int featureId, byte[] data)
break;
}

case TdsEnums.FEATUREEXT_USERAGENT:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be no ack defined in the TDS spec, so we shouldn't do this. Just let the default case handle it.

{
// Note: We do not expect an ACK for USERAGENT feature extension, but if we receive it, we will log it.
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.SqlInternalConnectionTds.OnFeatureExtAck|ADV> {0}, Received feature extension acknowledgement for USERAGENT", ObjectID);
if (data.Length != 1)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlInternalConnectionTds.OnFeatureExtAck|ERR> {0}, Unknown token for USERAGENT", ObjectID);
throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream);
}
byte userAgentSupportVersion = data[0];
if (userAgentSupportVersion == 0 || userAgentSupportVersion > TdsEnums.SUPPORTED_USER_AGENT_VERSION)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlInternalConnectionTds.OnFeatureExtAck|ERR> {0}, Invalid version number {1} for USERAGENT, Max supported version is {2}", ObjectID, userAgentSupportVersion, TdsEnums.SUPPORTED_USER_AGENT_VERSION);
throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream);
}
IsUserAgentSupportEnabled = true;
break;
}

default:
{
// Unknown feature ack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
using Microsoft.Data.SqlClient.DataClassification;
using Microsoft.Data.SqlClient.LocalDb;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.SqlClient.UserAgent;

#if NETFRAMEWORK
using Microsoft.Data.SqlTypes;
#endif
Expand Down Expand Up @@ -9074,8 +9076,16 @@ private void WriteLoginData(SqlLogin rec,
_physicalStateObj.WriteByteArray(encryptedChangePassword, encryptedChangePasswordLengthInBytes, 0);
}
}

ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length, true);

ApplyFeatureExData(
requestedFeatures,
recoverySessionData,
fedAuthFeatureExtensionData,
UserAgentInfo.GetCachedPayload(),
useFeatureExt,
length,
true
);
}
catch (Exception e)
{
Expand All @@ -9094,6 +9104,7 @@ private void WriteLoginData(SqlLogin rec,
private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
SessionData recoverySessionData,
FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData,
byte[] userAgentJsonPayload,
bool useFeatureExt,
int length,
bool write = false)
Expand Down Expand Up @@ -9148,6 +9159,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
length += WriteVectorSupportFeatureRequest(write);
}

if ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0)
{
length += WriteUserAgentFeatureRequest(userAgentJsonPayload, write);
}

length++; // for terminator
if (write)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers;
using System.Diagnostics;
using System.Text;
using Microsoft.Data.SqlClient.UserAgent;
using Microsoft.Data.SqlClient.Utilities;

#nullable enable
Expand Down Expand Up @@ -191,8 +192,17 @@ internal void TdsLogin(
}

int feOffset = length;
// TODO: User Agent Json Payload will go here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this?

byte[] emptyBytes = new byte[0];
// calculate and reserve the required bytes for the featureEx
length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length);
length = ApplyFeatureExData(
requestedFeatures,
recoverySessionData,
fedAuthFeatureExtensionData,
UserAgentInfo.GetCachedPayload(),
useFeatureExt,
length
);

WriteLoginData(rec,
requestedFeatures,
Expand Down
Loading
Loading