From: Matthew Endsley Date: Sat, 6 Mar 2021 01:53:23 +0000 (-0800) Subject: Remove DTLS peers that fail to negotiate a valid epoch X-Git-Tag: 1.0.0~17^2 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=4b2bec46c202e904ff9814585b97a5bd8d3d4422;p=rhonda%2Fimpostor.hazel.git Remove DTLS peers that fail to negotiate a valid epoch --- diff --git a/Hazel/Dtls/DtlsConnectionListener.cs b/Hazel/Dtls/DtlsConnectionListener.cs index 25794eb..32c7835 100644 --- a/Hazel/Dtls/DtlsConnectionListener.cs +++ b/Hazel/Dtls/DtlsConnectionListener.cs @@ -92,6 +92,8 @@ namespace Hazel.Dtls public readonly List QueuedApplicationDataMessage = new List(); + public DateTime StartOfNegotiation; + public PeerData() { ByteSpan block = new byte[2 * Finished.Size]; @@ -126,6 +128,8 @@ namespace Hazel.Dtls this.NextEpoch.ServerVerification = new byte[Finished.Size]; this.ConnectionId = connectionId; + + this.StartOfNegotiation = DateTime.UtcNow; } public void Dispose() @@ -1283,6 +1287,29 @@ namespace Hazel.Dtls } } + /// + public override void DisconnectOldConnections(TimeSpan maxAge, MessageWriter disconnectMessage) + { + DateTime now = DateTime.UtcNow; + foreach (KeyValuePair kvp in this.existingPeers) + { + PeerData peer = kvp.Value; + lock(peer) + { + if (peer.Epoch == 0 || peer.NextEpoch.State != HandshakeState.ExpectingHello) + { + TimeSpan negotiationAge = now - peer.StartOfNegotiation; + if (negotiationAge > maxAge) + { + base.MarkConnectionAsStale(peer.ConnectionId); + } + } + } + } + + base.DisconnectOldConnections(maxAge, disconnectMessage); + } + /// /// Allocate a new connection id /// diff --git a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs index 84ac5a4..deda91d 100644 --- a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs +++ b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs @@ -151,7 +151,7 @@ namespace Hazel.Udp.FewerThreads } } - public void DisconnectOldConnections(TimeSpan maxAge, MessageWriter disconnectMessage) + public virtual void DisconnectOldConnections(TimeSpan maxAge, MessageWriter disconnectMessage) { var now = DateTime.UtcNow; foreach (var conn in this.allConnections.Values)