]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Remove DTLS peers that fail to negotiate a valid epoch
authorMatthew Endsley <mendsley@gmail.com>
Sat, 6 Mar 2021 01:53:23 +0000 (17:53 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Sat, 6 Mar 2021 01:59:43 +0000 (17:59 -0800)
Hazel/Dtls/DtlsConnectionListener.cs
Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs

index 25794eb604a5bcbf54267fd0a538757d3afa18bf..32c7835b7450c98feae0feb41fa8aedc2120db4f 100644 (file)
@@ -92,6 +92,8 @@ namespace Hazel.Dtls
 
             public readonly List<ByteSpan> QueuedApplicationDataMessage = new List<ByteSpan>();
 
+            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
             }
         }
 
+        /// <inheritdoc />
+        public override void DisconnectOldConnections(TimeSpan maxAge, MessageWriter disconnectMessage)
+        {
+            DateTime now = DateTime.UtcNow;
+            foreach (KeyValuePair<IPEndPoint, PeerData> 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);
+        }
+
         /// <summary>
         /// Allocate a new connection id
         /// </summary>
index 84ac5a44eca0642fccaf39765a2719d99db76756..deda91d25744de8c5a505df7dbe8f8036fa4c2cf 100644 (file)
@@ -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)