-
Notifications
You must be signed in to change notification settings - Fork 331
Feature | Implement SSRP packet parsing #4481
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
Open
edwardneal
wants to merge
11
commits into
dotnet:main
Choose a base branch
from
edwardneal:feat/ssrp/02-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0526e31
Update DAC response test data
edwardneal fa505b0
Add IndexOf utility function
edwardneal 9f951f0
Add more test cases to DAC response test data
edwardneal 863d1f4
Implement DacResponse and DacResponseReader
edwardneal 7a7cec1
Implement DacResponseReader tests
edwardneal d648939
Documentation updates and debug assertion
edwardneal 3695a00
Implement SqlDataSourceResponse and SqlDataSourceResponseReader
edwardneal 3806b1c
Add new test cases
edwardneal 68edbb3
Implement and rename SqlDataSourceResponseReaderTest
edwardneal 8df8cd8
Follow-ups
edwardneal 11185a5
Add additional test cases
edwardneal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/DacResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.Data.Common; | ||
| using System; | ||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Data.Sql; | ||
|
|
||
| /// <summary> | ||
| /// A single parsed SSRP response. | ||
| /// </summary> | ||
| /// <seealso href="https://learn.microsoft.com/en-us/openspecs/windows_protocols/mc-sqlr/45b52721-7a48-45cf-9c84-e6db905ad6df"/> | ||
| /// <remarks> | ||
| /// <para>This corresponds to an SVR_RESP (DAC) structure within the MC-SQLR specification.</para> | ||
| /// <para>An SVR_RESP (DAC) structure is a byte array with the following layout:</para> | ||
| /// <list type="number"> | ||
| /// <item>SVR_RESP: 1 byte, always 0x05.</item> | ||
| /// <item>RESP_SIZE: 2 bytes, always 0x06. Written in little-endian byte order.</item> | ||
| /// <item>PROTOCOLVERSION: 1 byte, always 0x01.</item> | ||
| /// <item>TCP_DAC_PORT: 2 bytes, the TCP port number that is used for the DAC. Written in little-endian byte order.</item> | ||
| /// </list> | ||
| /// </remarks> | ||
| internal readonly ref struct DacResponse | ||
| { | ||
| private const int ResponseHeaderOffset = 0; | ||
| private const int ResponseSizeOffset = ResponseHeaderOffset + sizeof(byte); | ||
| private const int ProtocolVersionOffset = ResponseSizeOffset + sizeof(ushort); | ||
| private const int DacPortOffset = ProtocolVersionOffset + sizeof(byte); | ||
| private const int TotalResponseSize = DacPortOffset + sizeof(ushort); | ||
|
|
||
| private const byte ResponseHeaderValue = 0x05; | ||
| private const ushort ResponseSizeValue = 0x06; | ||
| private const byte ProtocolVersionValue = 0x01; | ||
|
|
||
| public ushort DacPort { get; } | ||
|
|
||
| private DacResponse(ushort tcpDacPort) | ||
| { | ||
| DacPort = tcpDacPort; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to parse a single SSRP response from the start of the provided source sequence. | ||
| /// If an SSRP response cannot be found, supplies the maximum number of bytes to advance the | ||
| /// sequence by before attempting to parse another response. | ||
| /// </summary> | ||
| /// <param name="sourceSequence">The source buffer to read from.</param> | ||
| /// <param name="response">The populated SSRP response (or default, if one cannot be found.)</param> | ||
| /// <param name="bytesRead">The number of bytes to advance <paramref name="sourceSequence"/> by.</param> | ||
| /// <returns><c>true</c> if the response was processed, <c>false</c> if not.</returns> | ||
| /// <remarks> | ||
| /// If the sequence does not start with an SSRP response, <paramref name="bytesRead"/> will | ||
| /// contain the position of the next possible <c>SVR_RESP</c> header byte (<c>0x05</c>), or | ||
| /// the length of <paramref name="sourceSequence"/> if this header byte is not present in the | ||
| /// sequence. | ||
| /// </remarks> | ||
| public static bool TryParse(ReadOnlySequence<byte> sourceSequence, out DacResponse response, out long bytesRead) | ||
| { | ||
| // Make sure we have enough data to read the header. | ||
| if (sourceSequence.Length < TotalResponseSize) | ||
| { | ||
| bytesRead = sourceSequence.Length; | ||
| response = default; | ||
| return false; | ||
| } | ||
|
|
||
| ReadOnlySequence<byte> currSequence = sourceSequence; | ||
| ReadOnlySpan<byte> currSpan = currSequence.First.Span; | ||
| long currOffset = 0; | ||
|
|
||
| // Read and validate the response header. | ||
| if (currSequence.ReadByte(ref currSpan, ref currOffset, out byte responseHeader) | ||
| // RESP_SVR must be 0x05. | ||
| && responseHeader == ResponseHeaderValue | ||
| && currSequence.ReadLittleEndian(ref currSpan, ref currOffset, out ushort responseSize) | ||
| // RESP_SIZE must be 0x0006. | ||
| && responseSize == ResponseSizeValue | ||
| && currSequence.ReadByte(ref currSpan, ref currOffset, out byte protocolVersion) | ||
| // PROTOCOLVERSION must be 0x01. | ||
| && protocolVersion == ProtocolVersionValue | ||
| && currSequence.ReadLittleEndian(ref currSpan, ref currOffset, out ushort tcpDacPort) | ||
| // TCP_DAC_PORT must be non-zero. | ||
| && tcpDacPort != 0) | ||
| { | ||
| Debug.Assert(currOffset == TotalResponseSize); | ||
|
|
||
| bytesRead = currOffset; | ||
| response = new DacResponse(tcpDacPort); | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| // Find the next possible response start across the sequence. Always advance by one byte (to skip the current | ||
| // header byte.) | ||
| long idx = sourceSequence.Slice(1).IndexOf(ResponseHeaderValue); | ||
|
|
||
| bytesRead = idx == -1 | ||
| ? sourceSequence.Length | ||
| : idx + 1; | ||
| response = default; | ||
| return false; | ||
| } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/DacResponseReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // 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.Buffers; | ||
| using System.Diagnostics; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Data.Sql; | ||
|
|
||
| /// <summary> | ||
| /// Utilities used to extract zero or more parsed SSRP DAC responses from a set of packet buffers. | ||
| /// </summary> | ||
| internal static class DacResponseReader | ||
| { | ||
| /// <summary> | ||
| /// Returns the first SSRP response from the provided source sequence, if one exists. | ||
| /// </summary> | ||
| /// <param name="sourceSequence">The source sequence of buffers from the network.</param> | ||
| /// <param name="firstResponse">The first SSRP response (if available.)</param> | ||
| /// <returns><c>true</c> if an SSRP response can be located in the source sequence, <c>false</c> otherwise.</returns> | ||
| public static bool TryReadFirst(ReadOnlySequence<byte> sourceSequence, out DacResponse firstResponse) | ||
| { | ||
| ReadOnlySequence<byte> remainingSequence = sourceSequence; | ||
|
|
||
| while (!remainingSequence.IsEmpty) | ||
| { | ||
| bool parsedCurrentRequest = DacResponse.TryParse(remainingSequence, out DacResponse current, out long bytesRead); | ||
|
|
||
| if (parsedCurrentRequest) | ||
| { | ||
| firstResponse = current; | ||
| return true; | ||
| } | ||
|
|
||
| // If the current request cannot be parsed, advance by bytesRead. | ||
| // bytesRead will always be greater than zero - it'll be the next possibly-viable | ||
| // position of a DAC response, or the end of the sequence if no viable DAC responses | ||
| // can be found. | ||
| Debug.Assert(bytesRead > 0 && bytesRead <= remainingSequence.Length); | ||
| remainingSequence = remainingSequence.Slice(bytesRead); | ||
| } | ||
|
|
||
| firstResponse = default; | ||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The name change here and in SqlDataSourceResponseReader is purely a layering point: