-
Notifications
You must be signed in to change notification settings - Fork 61
Added basic support for DNS cookies #56
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
zbalkan
wants to merge
6
commits into
TechnitiumSoftware:master
Choose a base branch
from
zbalkan:feat/add-dns-cookie-support
base: master
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2114ba1
Added basic support for DNS cookies
zbalkan 332c3b1
Added WriteEndObject to SerializeTo
zbalkan 8508fea
Added missing copyright
zbalkan 137b84a
Improved exception message
zbalkan 1877b52
Used regions
zbalkan 8463b50
Fixed JSON field names
zbalkan 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
139 changes: 139 additions & 0 deletions
139
TechnitiumLibrary.Net/Dns/EDnsOptions/EDnsCookieOptionData.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,139 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
|
|
||
| namespace TechnitiumLibrary.Net.Dns.EDnsOptions | ||
| { | ||
| /// <summary> | ||
| /// RFC 7873 DNS COOKIE EDNS option. | ||
| /// Option data: | ||
| /// - Client cookie: 8 bytes (MUST) | ||
| /// - Server cookie: 0 or 8-32 bytes (MAY) | ||
| /// Total option data length: 8 OR 16-40 bytes. | ||
| /// </summary> | ||
| public sealed class EDnsCookieOptionData : EDnsOptionData, IEquatable<EDnsCookieOptionData> | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
| { | ||
| public const int CLIENT_COOKIE_LENGTH = 8; | ||
| public const int SERVER_COOKIE_MIN_LENGTH = 8; | ||
| public const int SERVER_COOKIE_MAX_LENGTH = 32; | ||
|
|
||
| byte[] _clientCookie; | ||
| byte[] _serverCookie; // null means absent (client-cookie-only) | ||
|
|
||
| public ReadOnlySpan<byte> ClientCookie => _clientCookie; | ||
| public ReadOnlySpan<byte> ServerCookie => _serverCookie is null ? ReadOnlySpan<byte>.Empty : _serverCookie; | ||
|
|
||
| public bool HasServerCookie => _serverCookie is not null; | ||
|
|
||
| public EDnsCookieOptionData(byte[] clientCookie, byte[] serverCookie = null) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(clientCookie); | ||
|
|
||
| if (clientCookie.Length != CLIENT_COOKIE_LENGTH) | ||
| throw new ArgumentException("Client cookie must be 8 bytes.", nameof(clientCookie)); | ||
|
|
||
| if (serverCookie is not null && | ||
| (serverCookie.Length < SERVER_COOKIE_MIN_LENGTH || serverCookie.Length > SERVER_COOKIE_MAX_LENGTH)) | ||
| throw new ArgumentException("Server cookie must be 8-32 bytes.", nameof(serverCookie)); | ||
|
|
||
| _clientCookie = (byte[])clientCookie.Clone(); | ||
| _serverCookie = serverCookie is null ? null : (byte[])serverCookie.Clone(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parsing ctor. The stream is positioned at OPTION-LENGTH (immediately after OPTION-CODE), | ||
| /// because EDnsOption(Stream) already read OPTION-CODE. | ||
| /// </summary> | ||
| public EDnsCookieOptionData(Stream s) | ||
| : base(s) | ||
| { } | ||
|
|
||
| public override int UncompressedLength => CLIENT_COOKIE_LENGTH + (_serverCookie?.Length ?? 0); | ||
|
|
||
| protected override void ReadOptionData(Stream s) | ||
| { | ||
| // _length is OPTION-LENGTH (bytes of option data). | ||
| if (_length < CLIENT_COOKIE_LENGTH) | ||
| throw new InvalidDataException($"Invalid COOKIE option length: {_length} bytes"); | ||
|
|
||
| int serverLen = _length - CLIENT_COOKIE_LENGTH; | ||
|
|
||
| // Valid serverLen: 0 OR 8..32. | ||
| if (serverLen != 0 && (serverLen < SERVER_COOKIE_MIN_LENGTH || serverLen > SERVER_COOKIE_MAX_LENGTH)) | ||
| throw new InvalidDataException($"Invalid server cookie length: {serverLen} bytes"); | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
|
|
||
| _clientCookie = new byte[CLIENT_COOKIE_LENGTH]; | ||
| s.ReadExactly(_clientCookie); | ||
|
|
||
| if (serverLen == 0) | ||
| { | ||
| _serverCookie = null; | ||
| return; | ||
| } | ||
|
|
||
| _serverCookie = new byte[serverLen]; | ||
| s.ReadExactly(_serverCookie); | ||
| } | ||
|
|
||
| protected override void WriteOptionData(Stream s) | ||
| { | ||
| s.Write(_clientCookie); | ||
|
|
||
| if (_serverCookie is not null) | ||
| s.Write(_serverCookie); | ||
| } | ||
|
|
||
| public override void SerializeTo(Utf8JsonWriter writer) | ||
| { | ||
| writer.WriteStartObject(); | ||
|
|
||
| writer.WriteString("client", Convert.ToHexString(_clientCookie)); | ||
|
|
||
| if (_serverCookie is not null) | ||
| writer.WriteString("server", Convert.ToHexString(_serverCookie)); | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
|
|
||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| public override string ToString() | ||
| { | ||
| if (_serverCookie is null) | ||
| return $"COOKIE client={Convert.ToHexString(_clientCookie)}"; | ||
|
|
||
| return $"COOKIE client={Convert.ToHexString(_clientCookie)} server={Convert.ToHexString(_serverCookie)}"; | ||
| } | ||
|
|
||
| public bool Equals(EDnsCookieOptionData other) | ||
| { | ||
| if (other is null) | ||
| return false; | ||
|
|
||
| if (!_clientCookie.AsSpan().SequenceEqual(other._clientCookie)) | ||
| return false; | ||
|
|
||
| if (_serverCookie is null && other._serverCookie is null) | ||
| return true; | ||
|
|
||
| if (_serverCookie is null || other._serverCookie is null) | ||
| return false; | ||
|
|
||
| return _serverCookie.AsSpan().SequenceEqual(other._serverCookie); | ||
| } | ||
|
|
||
| public override bool Equals(object obj) => Equals(obj as EDnsCookieOptionData); | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| HashCode hash = new(); | ||
|
|
||
| foreach (byte b in _clientCookie) | ||
| hash.Add(b); | ||
|
|
||
| if (_serverCookie is not null) | ||
| foreach (byte b in _serverCookie) | ||
| hash.Add(b); | ||
|
|
||
| return hash.ToHashCode(); | ||
| } | ||
| } | ||
| } | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.