--- /dev/null
+namespace Hazel.Dtls
+{
+ /// <summary>
+ /// DTLS version constants
+ /// </summary>
+ public enum ProtocolVersion : ushort
+ {
+ /// <summary>
+ /// DTLS 1.2
+ /// </summary>
+ DTLS1_2 = 0xFEFD,
+ }
+
+ /// <summary>
+ /// DTLS record content type
+ /// </summary>
+ public enum ContentType : byte
+ {
+ ChangeCipherSpec = 20,
+ Alert = 21,
+ Handshake = 22,
+ ApplicationData = 23,
+ }
+
+ /// <summary>
+ /// Encode/decode DTLS record header
+ /// </summary>
+ public struct Record
+ {
+ public ContentType ContentType;
+ public ushort Epoch;
+ public ulong SequenceNumber;
+ public ushort Length;
+
+ public const int Size = 13;
+
+ /// <summary>
+ /// Parse a DTLS record from wire format
+ /// </summary>
+ /// <returns>True if we successfully parse the record header. Otherwise false</returns>
+ public static bool Parse(out Record record, ByteSpan span)
+ {
+ record = new Record();
+
+ if (span.Length < Size)
+ {
+ return false;
+ }
+
+ record.ContentType = (ContentType)span[0];
+ ProtocolVersion version = (ProtocolVersion)span.ReadBigEndian16(1);
+ record.Epoch = span.ReadBigEndian16(3);
+ record.SequenceNumber = span.ReadBigEndian48(5);
+ record.Length = span.ReadBigEndian16(11);
+
+ if (version != ProtocolVersion.DTLS1_2)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Encode a DTLS record to wire format
+ /// </summary>
+ public void Encode(ByteSpan span)
+ {
+ span[0] = (byte)this.ContentType;
+ span.WriteBigEndian16((ushort)ProtocolVersion.DTLS1_2);
+ span.WriteBigEndian16(this.Epoch, 3);
+ span.WriteBigEndian48(this.SequenceNumber, 5);
+ span.WriteBigEndian16(this.Length, 11);
+ }
+ }
+}
<Compile Include="Dtls\Handshake.cs" />
<Compile Include="Dtls\IHandshakeCipherSuite.cs" />
<Compile Include="Dtls\PrfSha256.cs" />
+ <Compile Include="Dtls\Record.cs" />
<Compile Include="Dtls\X25519EcdheRsaSha256.cs" />
<Compile Include="FewerThreads\HazelThreadPool.cs" />
<Compile Include="FewerThreads\ThreadLimitedUdpConnectionListener.cs" />