Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
139 changes: 139 additions & 0 deletions TechnitiumLibrary.Net/Dns/EDnsOptions/EDnsCookieOptionData.cs
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
Comment thread
zbalkan marked this conversation as resolved.
Outdated
{
/// <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>
Comment thread
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");
Comment thread
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));
Comment thread
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();
}
}
}
Comment thread
zbalkan marked this conversation as resolved.
Outdated
34 changes: 26 additions & 8 deletions TechnitiumLibrary.Net/Dns/EDnsOptions/EDnsOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public EDnsOption(Stream s)
_data = new EDnsExtendedDnsErrorOptionData(s);
break;

case EDnsOptionCode.COOKIE:
_data = new EDnsCookieOptionData(s);
break;

default:
_data = new EDnsUnknownOptionData(s);
break;
Expand All @@ -92,6 +96,14 @@ public void WriteTo(Stream s)
{
DnsDatagram.WriteUInt16NetworkOrder((ushort)_code, s);

// OPTION-LENGTH=0 is valid; represent with null data.
if (_data is null)
{
DnsDatagram.WriteUInt16NetworkOrder(0, s);
return;
}

// EDnsOptionData.WriteTo writes OPTION-LENGTH + option bytes.
_data.WriteTo(s);
}

Expand Down Expand Up @@ -128,27 +140,33 @@ public void SerializeTo(Utf8JsonWriter jsonWriter)
{
jsonWriter.WriteStartObject();

jsonWriter.WriteString("Code", _code.ToString());
jsonWriter.WriteString("Length", _data.Length + " bytes");
jsonWriter.WriteString(nameof(Code), _code.ToString());

jsonWriter.WritePropertyName("Data");
_data.SerializeTo(jsonWriter);
if (_data is null)
{
jsonWriter.WriteString("Length", "0 bytes");
jsonWriter.WriteNull(nameof(Data));
}
else
{
jsonWriter.WriteString("Length", _data.Length + " bytes");
jsonWriter.WritePropertyName(nameof(Data));
_data.SerializeTo(jsonWriter);
}

Comment thread
zbalkan marked this conversation as resolved.
Outdated
jsonWriter.WriteEndObject();
}

#endregion

#region properties
#region properties
Comment thread
zbalkan marked this conversation as resolved.
Outdated

public EDnsOptionCode Code
{ get { return _code; } }

public EDnsOptionData Data
{ get { return _data; } }

public int UncompressedLength
{ get { return 2 + 2 + _data.UncompressedLength; } }
public int UncompressedLength => 2 + 2 + (_data?.UncompressedLength ?? 0);

#endregion
}
Expand Down
Loading