From 03b9143809ff73e157b6d7f4c1e086e0b3c7efc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kr=C3=A4mer?= Date: Mon, 25 Jul 2022 15:01:32 +0200 Subject: [PATCH 1/6] upgrade BouncyCastle to 1.9.0 and use new crypto API --- samples/DtlsClient/Program.cs | 24 +++++++------------ samples/DtlsServer/Program.cs | 14 ++++++----- .../Client/CoapDtlsClientEndPoint.cs | 4 ++-- .../Client/UdpDatagramTransport.cs | 2 +- src/CoAPNet.Dtls/CoAPNet.Dtls.csproj | 6 ++--- .../Server/CoapDtlsConnectionInformation.cs | 2 +- .../Server/CoapDtlsServerClientEndPoint.cs | 2 +- .../Server/CoapDtlsServerTransport.cs | 6 ++--- src/CoAPNet.Dtls/Server/IDtlsServerFactory.cs | 2 +- .../Server/IDtlsServerWithConnectionInfo.cs | 2 +- .../Server/QueueDatagramTransport.cs | 2 +- src/CoAPNet.Server/CoAPNet.Server.csproj | 4 ++-- src/CoAPNet.Udp/CoAPNet.Udp.csproj | 4 ++-- src/CoAPNet/CoAPNet.csproj | 4 ++-- 14 files changed, 36 insertions(+), 42 deletions(-) diff --git a/samples/DtlsClient/Program.cs b/samples/DtlsClient/Program.cs index c383370..a69bb91 100644 --- a/samples/DtlsClient/Program.cs +++ b/samples/DtlsClient/Program.cs @@ -4,7 +4,10 @@ using System.Threading.Tasks; using CoAPNet; using CoAPNet.Dtls.Client; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Security; +using Org.BouncyCastle.Tls; +using Org.BouncyCastle.Tls.Crypto; +using Org.BouncyCastle.Tls.Crypto.Impl.BC; namespace CoAPDevices { @@ -16,7 +19,6 @@ static async Task Main(string[] args) var port = Coap.PortDTLS; var identity = new BasicTlsPskIdentity("user", Encoding.UTF8.GetBytes("password")); - // Create a new client using a DTLS endpoint with the remote host and Identity var client = new CoapClient(new CoapDtlsClientEndPoint(host, port, new ExamplePskDtlsClient(identity))); // Create a cancelation token that cancels after 1 minute @@ -69,26 +71,18 @@ static async Task Main(string[] args) public class ExamplePskDtlsClient : PskTlsClient { public ExamplePskDtlsClient(TlsPskIdentity pskIdentity) - : base(pskIdentity) + : base(new BcTlsCrypto(new SecureRandom()), pskIdentity) { } - public ExamplePskDtlsClient(TlsCipherFactory cipherFactory, TlsPskIdentity pskIdentity) - : base(cipherFactory, pskIdentity) - { - } - - public ExamplePskDtlsClient(TlsCipherFactory cipherFactory, TlsDHVerifier dhVerifier, TlsPskIdentity pskIdentity) - : base(cipherFactory, dhVerifier, pskIdentity) + public override int GetHandshakeTimeoutMillis() { + return 30000; } - public override ProtocolVersion MinimumVersion => ProtocolVersion.DTLSv10; - public override ProtocolVersion ClientVersion => ProtocolVersion.DTLSv12; - - public override int GetHandshakeTimeoutMillis() + public override ProtocolVersion[] GetProtocolVersions() { - return 30000; + return ProtocolVersion.DTLSv12.DownTo(ProtocolVersion.DTLSv10); } } } diff --git a/samples/DtlsServer/Program.cs b/samples/DtlsServer/Program.cs index 5e3cac3..50f6e51 100644 --- a/samples/DtlsServer/Program.cs +++ b/samples/DtlsServer/Program.cs @@ -7,10 +7,10 @@ using CoAPNet.Server; using System.Threading.Tasks; using CoAPNet.Dtls.Server; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Console; -using Microsoft.Extensions.Options; +using Org.BouncyCastle.Tls.Crypto.Impl.BC; +using Org.BouncyCastle.Security; namespace CoAPDevices { @@ -82,7 +82,7 @@ public class ExamplePskDtlsServer : PskTlsServer private readonly ExamplePskIdentityManager pskIdentityManager; public ExamplePskDtlsServer(ExamplePskIdentityManager pskIdentityManager) - : base(pskIdentityManager) + : base(new BcTlsCrypto(new SecureRandom()), pskIdentityManager) { this.pskIdentityManager = pskIdentityManager; } @@ -97,8 +97,10 @@ public string GetIdentity() return this.pskIdentityManager.GetIdentity(); } - protected override ProtocolVersion MinimumVersion => ProtocolVersion.DTLSv10; - protected override ProtocolVersion MaximumVersion => ProtocolVersion.DTLSv12; + public override ProtocolVersion[] GetProtocolVersions() + { + return ProtocolVersion.DTLSv12.DownTo(ProtocolVersion.DTLSv10); + } } public class ExamplePskIdentityManager : TlsPskIdentityManager diff --git a/src/CoAPNet.Dtls/Client/CoapDtlsClientEndPoint.cs b/src/CoAPNet.Dtls/Client/CoapDtlsClientEndPoint.cs index eb1e335..337df4d 100644 --- a/src/CoAPNet.Dtls/Client/CoapDtlsClientEndPoint.cs +++ b/src/CoAPNet.Dtls/Client/CoapDtlsClientEndPoint.cs @@ -3,7 +3,7 @@ using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; using Org.BouncyCastle.Security; namespace CoAPNet.Dtls.Client @@ -82,7 +82,7 @@ private void EnsureConnected() var udpClient = new UdpClient(Server, Port); - var dtlsClientProtocol = new DtlsClientProtocol(new SecureRandom()); + var dtlsClientProtocol = new DtlsClientProtocol(); _datagramTransport = dtlsClientProtocol.Connect(_tlsClient, new UdpDatagramTransport(udpClient, NetworkMtu)); _isConnected = true; } diff --git a/src/CoAPNet.Dtls/Client/UdpDatagramTransport.cs b/src/CoAPNet.Dtls/Client/UdpDatagramTransport.cs index cc5ced3..2e79de6 100644 --- a/src/CoAPNet.Dtls/Client/UdpDatagramTransport.cs +++ b/src/CoAPNet.Dtls/Client/UdpDatagramTransport.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Net; using System.Net.Sockets; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; namespace CoAPNet.Dtls.Client { diff --git a/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj b/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj index 24966f3..21d963f 100644 --- a/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj +++ b/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj @@ -4,8 +4,8 @@ netstandard2.0 NZSmartie.CoAPNet.Dtls latest - 0.3.8 - 0.3.8 + 0.4.0 + 0.4.0 Roman Vaughan NZSmartie UDP DTLS implementation for NZSmartie.CoAPNet @@ -28,7 +28,7 @@ - + diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsConnectionInformation.cs b/src/CoAPNet.Dtls/Server/CoapDtlsConnectionInformation.cs index de3edce..0423491 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsConnectionInformation.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsConnectionInformation.cs @@ -1,4 +1,4 @@ -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; namespace CoAPNet.Dtls.Server { diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs index 140d44d..2dce0b5 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs @@ -3,7 +3,7 @@ using System.Net; using System.Threading; using System.Threading.Tasks; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; namespace CoAPNet.Dtls.Server { diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs index faac3c3..1a0c39e 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using CoAPNet.Dtls.Server.Statistics; using Microsoft.Extensions.Logging; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; using Org.BouncyCastle.Security; namespace CoAPNet.Dtls.Server @@ -35,9 +35,7 @@ public CoapDtlsServerTransport(CoapDtlsServerEndPoint endPoint, ICoapHandler coa _tlsServerFactory = tlsServerFactory ?? throw new ArgumentNullException(nameof(tlsServerFactory)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - SecureRandom random = new SecureRandom(); - - _serverProtocol = new DtlsServerProtocol(random); + _serverProtocol = new DtlsServerProtocol(); _sessions = new ConcurrentDictionary(); } diff --git a/src/CoAPNet.Dtls/Server/IDtlsServerFactory.cs b/src/CoAPNet.Dtls/Server/IDtlsServerFactory.cs index 9246834..a1afe39 100644 --- a/src/CoAPNet.Dtls/Server/IDtlsServerFactory.cs +++ b/src/CoAPNet.Dtls/Server/IDtlsServerFactory.cs @@ -1,4 +1,4 @@ -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; namespace CoAPNet.Dtls.Server { diff --git a/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs b/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs index a736225..188a9c7 100644 --- a/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs +++ b/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs @@ -1,4 +1,4 @@ -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; namespace CoAPNet.Dtls.Server { diff --git a/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs b/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs index 8ad7e48..f7d2c81 100644 --- a/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs +++ b/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs @@ -2,7 +2,7 @@ using System.Collections.Concurrent; using System.Linq; using System.Threading; -using Org.BouncyCastle.Crypto.Tls; +using Org.BouncyCastle.Tls; namespace CoAPNet.Dtls.Server { diff --git a/src/CoAPNet.Server/CoAPNet.Server.csproj b/src/CoAPNet.Server/CoAPNet.Server.csproj index 94de497..ccdb25c 100644 --- a/src/CoAPNet.Server/CoAPNet.Server.csproj +++ b/src/CoAPNet.Server/CoAPNet.Server.csproj @@ -6,8 +6,8 @@ True latest NZSmartie.CoAPNet.Server - 0.3.8 - 0.3.8 + 0.4.0 + 0.4.0 Roman Vaughan NZSmartie Server and handler implementation for NZSmartie.CoAPNet diff --git a/src/CoAPNet.Udp/CoAPNet.Udp.csproj b/src/CoAPNet.Udp/CoAPNet.Udp.csproj index 38b50d6..628fb9e 100644 --- a/src/CoAPNet.Udp/CoAPNet.Udp.csproj +++ b/src/CoAPNet.Udp/CoAPNet.Udp.csproj @@ -4,8 +4,8 @@ netstandard2.0 NZSmartie.CoAPNet.Udp latest - 0.3.8 - 0.3.8 + 0.4.0 + 0.4.0 Roman Vaughan NZSmartie UDP Socket implementation for NZSmartie.CoAPNet diff --git a/src/CoAPNet/CoAPNet.csproj b/src/CoAPNet/CoAPNet.csproj index 1a5c18f..8d8d5bf 100644 --- a/src/CoAPNet/CoAPNet.csproj +++ b/src/CoAPNet/CoAPNet.csproj @@ -6,8 +6,8 @@ True latest NZSmartie.CoAPNet - 0.3.8.0 - 0.3.8 + 0.4.0 + 0.4.0 Roman Vaughan NZSmartie CoAPNet From aae29de32c7900e20c23f75279356cfdebace316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kr=C3=A4mer?= Date: Mon, 25 Jul 2022 15:16:15 +0200 Subject: [PATCH 2/6] revert version change --- src/CoAPNet.Dtls/CoAPNet.Dtls.csproj | 4 ++-- src/CoAPNet.Server/CoAPNet.Server.csproj | 4 ++-- src/CoAPNet.Udp/CoAPNet.Udp.csproj | 4 ++-- src/CoAPNet/CoAPNet.csproj | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj b/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj index 21d963f..c3d5cef 100644 --- a/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj +++ b/src/CoAPNet.Dtls/CoAPNet.Dtls.csproj @@ -4,8 +4,8 @@ netstandard2.0 NZSmartie.CoAPNet.Dtls latest - 0.4.0 - 0.4.0 + 0.3.8 + 0.3.8 Roman Vaughan NZSmartie UDP DTLS implementation for NZSmartie.CoAPNet diff --git a/src/CoAPNet.Server/CoAPNet.Server.csproj b/src/CoAPNet.Server/CoAPNet.Server.csproj index ccdb25c..94de497 100644 --- a/src/CoAPNet.Server/CoAPNet.Server.csproj +++ b/src/CoAPNet.Server/CoAPNet.Server.csproj @@ -6,8 +6,8 @@ True latest NZSmartie.CoAPNet.Server - 0.4.0 - 0.4.0 + 0.3.8 + 0.3.8 Roman Vaughan NZSmartie Server and handler implementation for NZSmartie.CoAPNet diff --git a/src/CoAPNet.Udp/CoAPNet.Udp.csproj b/src/CoAPNet.Udp/CoAPNet.Udp.csproj index 628fb9e..38b50d6 100644 --- a/src/CoAPNet.Udp/CoAPNet.Udp.csproj +++ b/src/CoAPNet.Udp/CoAPNet.Udp.csproj @@ -4,8 +4,8 @@ netstandard2.0 NZSmartie.CoAPNet.Udp latest - 0.4.0 - 0.4.0 + 0.3.8 + 0.3.8 Roman Vaughan NZSmartie UDP Socket implementation for NZSmartie.CoAPNet diff --git a/src/CoAPNet/CoAPNet.csproj b/src/CoAPNet/CoAPNet.csproj index 8d8d5bf..1a5c18f 100644 --- a/src/CoAPNet/CoAPNet.csproj +++ b/src/CoAPNet/CoAPNet.csproj @@ -6,8 +6,8 @@ True latest NZSmartie.CoAPNet - 0.4.0 - 0.4.0 + 0.3.8.0 + 0.3.8 Roman Vaughan NZSmartie CoAPNet From b205f4a77da97fa31a778b310fbe8ca975d2baae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kr=C3=A4mer?= Date: Thu, 8 Sep 2022 13:26:32 +0200 Subject: [PATCH 3/6] Handle TlsTimeoutException properly --- .../Server/CoapDtlsServerTransport.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs index 1a0c39e..57cf9c9 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs @@ -198,18 +198,17 @@ private async Task HandleSession(CoapDtlsServerClientEndPoint session) _logger.LogDebug("CoAP request from {EndPoint} handled!", session.EndPoint); } } - catch (OperationCanceledException) + catch (Exception ex) when (IsCanceledException(ex)) { + _logger.LogInformation(ex, "Session was canceled"); } - catch (DtlsConnectionClosedException) + catch (TlsTimeoutException timeoutEx) { + _logger.LogWarning(timeoutEx, "Timeout while handling session"); } catch (TlsFatalAlert tlsAlert) { - if (!(tlsAlert.InnerException is DtlsConnectionClosedException) && tlsAlert.AlertDescription != AlertDescription.user_canceled) - { - _logger.LogWarning(tlsAlert, "TLS Error"); - } + _logger.LogWarning(tlsAlert, "TLS Error"); } catch (Exception ex) { @@ -223,6 +222,13 @@ private async Task HandleSession(CoapDtlsServerClientEndPoint session) } } + private bool IsCanceledException(Exception ex) + { + return ex is OperationCanceledException || + ex is DtlsConnectionClosedException || + (ex is TlsFatalAlert tlsAlert && (tlsAlert.InnerException is DtlsConnectionClosedException || tlsAlert.AlertDescription == AlertDescription.user_canceled)); + } + private async Task HandleCleanup() { while (!_cts.IsCancellationRequested) From d5b6221d6ecd31b050d986a70f46f32c5edd147b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kr=C3=A4mer?= Date: Thu, 8 Sep 2022 14:22:01 +0200 Subject: [PATCH 4/6] improve error handling (possible ObjectDisposedException when there is a TLS error) --- .../Server/CoapDtlsServerClientEndPoint.cs | 3 +-- .../Server/QueueDatagramTransport.cs | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs index 2dce0b5..0c7b343 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs @@ -102,8 +102,7 @@ public void Accept(DtlsServerProtocol serverProtocol, TlsServer server) public void EnqueueDatagram(byte[] datagram) { - if (!IsClosed) - _udpTransport.ReceiveQueue.Add(datagram); + _udpTransport.EnqueueReceived(datagram); LastReceivedTime = DateTime.UtcNow; } } diff --git a/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs b/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs index f7d2c81..9da11da 100644 --- a/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs +++ b/src/CoAPNet.Dtls/Server/QueueDatagramTransport.cs @@ -16,6 +16,7 @@ internal class QueueDatagramTransport : DatagramTransport private readonly int _sendLimit; private readonly Action _sendCallback; private readonly CancellationTokenSource _cts; + private BlockingCollection _receiveQueue; private const int MIN_IP_OVERHEAD = 20; private const int MAX_IP_OVERHEAD = MIN_IP_OVERHEAD + 64; @@ -27,20 +28,21 @@ public QueueDatagramTransport(int mtu, Action sendCallback) _sendLimit = mtu - MAX_IP_OVERHEAD - UDP_OVERHEAD; _sendCallback = sendCallback ?? throw new ArgumentNullException(nameof(sendCallback)); _cts = new CancellationTokenSource(); + _receiveQueue = new BlockingCollection(); } - public BlockingCollection ReceiveQueue { get; internal set; } = new BlockingCollection(); public bool IsClosed { get; private set; } public ReaderWriterLockSlim CloseLock { get; } = new ReaderWriterLockSlim(); public void Close() { + // Cancel this before locking so the lock gets released ASAP _cts.Cancel(); CloseLock.EnterWriteLock(); IsClosed = true; + _receiveQueue.Dispose(); CloseLock.ExitWriteLock(); - ReceiveQueue.Dispose(); } public int GetReceiveLimit() @@ -53,6 +55,21 @@ public int GetSendLimit() return _sendLimit; } + public void EnqueueReceived(byte[] datagram) + { + try + { + CloseLock.EnterReadLock(); + if (IsClosed) + return; + _receiveQueue.Add(datagram); + } + finally + { + CloseLock.ExitReadLock(); + } + } + public int Receive(byte[] buf, int off, int len, int waitMillis) { try @@ -61,7 +78,7 @@ public int Receive(byte[] buf, int off, int len, int waitMillis) if (IsClosed) throw new DtlsConnectionClosedException(); - var success = ReceiveQueue.TryTake(out var data, waitMillis, _cts.Token); + var success = _receiveQueue.TryTake(out var data, waitMillis, _cts.Token); if (!success) return -1; // DO NOT return 0. This will disable the wait timeout effectively for the caller and any abort logic will by bypassed! var readLen = Math.Min(len, data.Length); From 6b666c03fb08ad6a89740c532009886db0387052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kr=C3=A4mer?= Date: Thu, 8 Sep 2022 15:01:10 +0200 Subject: [PATCH 5/6] Handle TlsTimeoutException properly --- src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs index 57cf9c9..18c93ab 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs @@ -9,7 +9,6 @@ using CoAPNet.Dtls.Server.Statistics; using Microsoft.Extensions.Logging; using Org.BouncyCastle.Tls; -using Org.BouncyCastle.Security; namespace CoAPNet.Dtls.Server { @@ -200,7 +199,7 @@ private async Task HandleSession(CoapDtlsServerClientEndPoint session) } catch (Exception ex) when (IsCanceledException(ex)) { - _logger.LogInformation(ex, "Session was canceled"); + _logger.LogDebug(ex, "Session was canceled"); } catch (TlsTimeoutException timeoutEx) { From 89a26effeaf9eb4a017065f1bf870df5054e0bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kr=C3=A4mer?= Date: Wed, 9 Nov 2022 16:18:53 +0100 Subject: [PATCH 6/6] let dtls server add log context for connections --- .../Server/CoapDtlsServerClientEndPoint.cs | 3 +- .../Server/CoapDtlsServerTransport.cs | 32 ++++++++----------- .../Server/IDtlsServerWithConnectionInfo.cs | 3 +- .../Statistics/DtlsSessionStatistics.cs | 3 +- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs index 0c7b343..be3ed83 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerClientEndPoint.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading; @@ -29,7 +30,7 @@ public CoapDtlsServerClientEndPoint(IPEndPoint endPoint, QueueDatagramTransport public IPEndPoint EndPoint { get; } public Uri BaseUri { get; } - public string ConnectionInfo { get; private set; } + public IReadOnlyDictionary ConnectionInfo { get; private set; } public bool IsSecure => true; diff --git a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs index 18c93ab..475f4bf 100644 --- a/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs +++ b/src/CoAPNet.Dtls/Server/CoapDtlsServerTransport.cs @@ -173,28 +173,24 @@ private async Task HandleSession(CoapDtlsServerClientEndPoint session) session.Accept(_serverProtocol, server); - if (session.ConnectionInfo != null) - { - _logger.LogInformation("New TLS connection from {EndPoint}, Server Info: {ServerInfo}", session.EndPoint, session.ConnectionInfo); - } - else + using (session.ConnectionInfo != null ? _logger.BeginScope(session.ConnectionInfo) : null) { _logger.LogInformation("New TLS connection from {EndPoint}", session.EndPoint); - } - var connectionInfo = new CoapDtlsConnectionInformation - { - LocalEndpoint = _endPoint, - RemoteEndpoint = session, - TlsServer = server - }; + var connectionInfo = new CoapDtlsConnectionInformation + { + LocalEndpoint = _endPoint, + RemoteEndpoint = session, + TlsServer = server + }; - while (!session.IsClosed && !_cts.IsCancellationRequested) - { - var packet = await session.ReceiveAsync(_cts.Token); - _logger.LogDebug("Handling CoAP Packet from {EndPoint}", session.EndPoint); - await _coapHandler.ProcessRequestAsync(connectionInfo, packet.Payload); - _logger.LogDebug("CoAP request from {EndPoint} handled!", session.EndPoint); + while (!session.IsClosed && !_cts.IsCancellationRequested) + { + var packet = await session.ReceiveAsync(_cts.Token); + _logger.LogDebug("Handling CoAP Packet from {EndPoint}", session.EndPoint); + await _coapHandler.ProcessRequestAsync(connectionInfo, packet.Payload); + _logger.LogDebug("CoAP request from {EndPoint} handled!", session.EndPoint); + } } } catch (Exception ex) when (IsCanceledException(ex)) diff --git a/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs b/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs index 188a9c7..41b7012 100644 --- a/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs +++ b/src/CoAPNet.Dtls/Server/IDtlsServerWithConnectionInfo.cs @@ -1,4 +1,5 @@ using Org.BouncyCastle.Tls; +using System.Collections.Generic; namespace CoAPNet.Dtls.Server { @@ -12,6 +13,6 @@ public interface IDtlsServerWithConnectionInfo : TlsServer /// Get the connection information for the connection handled by this TLS server. /// /// Information about this connection that should be logged once the connection is established. - string GetConnectionInfo(); + IReadOnlyDictionary GetConnectionInfo(); } } diff --git a/src/CoAPNet.Dtls/Server/Statistics/DtlsSessionStatistics.cs b/src/CoAPNet.Dtls/Server/Statistics/DtlsSessionStatistics.cs index 2d5dfd2..1d50858 100644 --- a/src/CoAPNet.Dtls/Server/Statistics/DtlsSessionStatistics.cs +++ b/src/CoAPNet.Dtls/Server/Statistics/DtlsSessionStatistics.cs @@ -1,11 +1,12 @@ using System; +using System.Collections.Generic; namespace CoAPNet.Dtls.Server.Statistics { public class DtlsSessionStatistics { public string EndPoint { get; set; } - public string ConnectionInfo { get; set; } + public IReadOnlyDictionary ConnectionInfo { get; set; } public DateTime SessionStartTime { get; internal set; } public DateTime LastReceivedTime { get; internal set; } }