From 4ff3ad2363c111d8723821687b14c1189ecac675 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 5 Nov 2022 23:04:19 +0700 Subject: Add Span-based variants in DTLS --- .../test/src/tls/test/LoggingDatagramTransport.cs | 62 ++++++++++++++++++++++ .../test/src/tls/test/MockDatagramAssociation.cs | 59 ++++++++++++++++++++ .../src/tls/test/UnreliableDatagramTransport.cs | 47 ++++++++++++++++ 3 files changed, 168 insertions(+) (limited to 'crypto/test') diff --git a/crypto/test/src/tls/test/LoggingDatagramTransport.cs b/crypto/test/src/tls/test/LoggingDatagramTransport.cs index f675b72fc..0ad15e065 100644 --- a/crypto/test/src/tls/test/LoggingDatagramTransport.cs +++ b/crypto/test/src/tls/test/LoggingDatagramTransport.cs @@ -34,25 +34,86 @@ namespace Org.BouncyCastle.Tls.Tests public virtual int Receive(byte[] buf, int off, int len, int waitMillis) { +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + return Receive(buf.AsSpan(off, len), waitMillis); +#else int length = m_transport.Receive(buf, off, len, waitMillis); if (length >= 0) { DumpDatagram("Received", buf, off, length); } return length; +#endif } +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + public virtual int Receive(Span buffer, int waitMillis) + { + int length = m_transport.Receive(buffer, waitMillis); + if (length >= 0) + { + DumpDatagram("Received", buffer[..length]); + } + return length; + } +#endif + public virtual void Send(byte[] buf, int off, int len) { +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + Send(buf.AsSpan(off, len)); +#else DumpDatagram("Sending", buf, off, len); m_transport.Send(buf, off, len); +#endif } +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + public virtual void Send(ReadOnlySpan buffer) + { + DumpDatagram("Sending", buffer); + m_transport.Send(buffer); + } +#endif + public virtual void Close() { m_transport.Close(); } +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + private void DumpDatagram(string verb, ReadOnlySpan buffer) + { + int len = buffer.Length; + long timestamp = DateTimeUtilities.CurrentUnixMs() - m_launchTimestamp; + StringBuilder sb = new StringBuilder("(+" + timestamp + "ms) " + verb + " " + len + " byte datagram:"); + for (int pos = 0; pos < len; ++pos) + { + if (pos % 16 == 0) + { + sb.Append(Environment.NewLine); + sb.Append(" "); + } + else if (pos % 16 == 8) + { + sb.Append('-'); + } + else + { + sb.Append(' '); + } + int val = buffer[pos] & 0xFF; + sb.Append(HEX_CHARS[val >> 4]); + sb.Append(HEX_CHARS[val & 0xF]); + } + Dump(sb.ToString()); + } +#else private void DumpDatagram(string verb, byte[] buf, int off, int len) { long timestamp = DateTimeUtilities.CurrentUnixMs() - m_launchTimestamp; @@ -78,6 +139,7 @@ namespace Org.BouncyCastle.Tls.Tests } Dump(sb.ToString()); } +#endif private void Dump(string s) { diff --git a/crypto/test/src/tls/test/MockDatagramAssociation.cs b/crypto/test/src/tls/test/MockDatagramAssociation.cs index ef317c7b6..3612bec40 100644 --- a/crypto/test/src/tls/test/MockDatagramAssociation.cs +++ b/crypto/test/src/tls/test/MockDatagramAssociation.cs @@ -58,6 +58,10 @@ namespace Org.BouncyCastle.Tls.Tests public virtual int Receive(byte[] buf, int off, int len, int waitMillis) { +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + return Receive(buf.AsSpan(off, len), waitMillis); +#else lock (m_receiveQueue) { if (m_receiveQueue.Count < 1) @@ -81,10 +85,45 @@ namespace Org.BouncyCastle.Tls.Tests Array.Copy(packet, 0, buf, off, copyLength); return copyLength; } +#endif } +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + public virtual int Receive(Span buffer, int waitMillis) + { + lock (m_receiveQueue) + { + if (m_receiveQueue.Count < 1) + { + try + { + Monitor.Wait(m_receiveQueue, waitMillis); + } + catch (ThreadInterruptedException) + { + // TODO Keep waiting until full wait expired? + } + + if (m_receiveQueue.Count < 1) + return -1; + } + + byte[] packet = m_receiveQueue[0]; + m_receiveQueue.RemoveAt(0); + int copyLength = System.Math.Min(buffer.Length, packet.Length); + packet.AsSpan(0, copyLength).CopyTo(buffer); + return copyLength; + } + } +#endif + public virtual void Send(byte[] buf, int off, int len) { +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + Send(buf.AsSpan(off, len)); +#else if (len > m_outer.m_mtu) { // TODO Simulate rejection? @@ -92,12 +131,32 @@ namespace Org.BouncyCastle.Tls.Tests byte[] packet = Arrays.CopyOfRange(buf, off, off + len); + lock (m_sendQueue) + { + m_sendQueue.Add(packet); + Monitor.PulseAll(m_sendQueue); + } +#endif + } + +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + public virtual void Send(ReadOnlySpan buffer) + { + if (buffer.Length > m_outer.m_mtu) + { + // TODO Simulate rejection? + } + + byte[] packet = buffer.ToArray(); + lock (m_sendQueue) { m_sendQueue.Add(packet); Monitor.PulseAll(m_sendQueue); } } +#endif public virtual void Close() { diff --git a/crypto/test/src/tls/test/UnreliableDatagramTransport.cs b/crypto/test/src/tls/test/UnreliableDatagramTransport.cs index bdbfd6e67..7769db9d1 100644 --- a/crypto/test/src/tls/test/UnreliableDatagramTransport.cs +++ b/crypto/test/src/tls/test/UnreliableDatagramTransport.cs @@ -37,6 +37,10 @@ namespace Org.BouncyCastle.Tls.Tests public virtual int Receive(byte[] buf, int off, int len, int waitMillis) { +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + return Receive(buf.AsSpan(off, len), waitMillis); +#else long endMillis = DateTimeUtilities.CurrentUnixMs() + waitMillis; for (;;) { @@ -52,10 +56,37 @@ namespace Org.BouncyCastle.Tls.Tests waitMillis = (int)(endMillis - now); } +#endif } +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + public virtual int Receive(Span buffer, int waitMillis) + { + long endMillis = DateTimeUtilities.CurrentUnixMs() + waitMillis; + for (;;) + { + int length = m_transport.Receive(buffer, waitMillis); + if (length < 0 || !LostPacket(m_percentPacketLossReceiving)) + return length; + + Console.WriteLine("PACKET LOSS (" + length + " byte packet not received)"); + + long now = DateTimeUtilities.CurrentUnixMs(); + if (now >= endMillis) + return -1; + + waitMillis = (int)(endMillis - now); + } + } +#endif + public virtual void Send(byte[] buf, int off, int len) { +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + Send(buf.AsSpan(off, len)); +#else if (LostPacket(m_percentPacketLossSending)) { Console.WriteLine("PACKET LOSS (" + len + " byte packet not sent)"); @@ -64,7 +95,23 @@ namespace Org.BouncyCastle.Tls.Tests { m_transport.Send(buf, off, len); } +#endif + } + +//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +#if NET6_0_OR_GREATER + public virtual void Send(ReadOnlySpan buffer) + { + if (LostPacket(m_percentPacketLossSending)) + { + Console.WriteLine("PACKET LOSS (" + buffer.Length + " byte packet not sent)"); + } + else + { + m_transport.Send(buffer); + } } +#endif public virtual void Close() { -- cgit 1.4.1