]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Modify ConnectionId to key on both an endpoint and serial
authorMatthew Endsley <mendsley@gmail.com>
Wed, 3 Feb 2021 10:02:12 +0000 (02:02 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Wed, 3 Feb 2021 22:40:56 +0000 (14:40 -0800)
Hazel/Dtls/DtlsConnectionListener.cs
Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs

index 088a1133b93c726487add1ef707e0479cabeb06b..b7a805657234dd0f80764098a6c6269bb1e53b88 100644 (file)
@@ -97,7 +97,7 @@ namespace Hazel.Dtls
                 this.CurrentEpoch.ServerFinishedVerification = block.Slice(0, Finished.Size);
                 this.CurrentEpoch.ExpectedClientFinishedVerification = block.Slice(Finished.Size, Finished.Size);
 
-                ResetPeer(ConnectionId.Create(0), 1);
+                ResetPeer(ConnectionId.Create(new IPEndPoint(0,0), 0), 1);
             }
 
             public void ResetPeer(ConnectionId connectionId, ulong nextExpectedSequenceNumber)
@@ -147,7 +147,7 @@ namespace Hazel.Dtls
 
         private readonly ConcurrentDictionary<IPEndPoint, PeerData> existingPeers = new ConcurrentDictionary<IPEndPoint, PeerData>();
 
-        private long connectionId_unsafe =  0;
+        private int connectionSerial_unsafe =  0;
 
         /// <summary>
         /// Create a new instance of the DTLS listener
@@ -765,7 +765,7 @@ namespace Hazel.Dtls
             if (record.Epoch == 0 && peer.Epoch != 0)
             {
                 ConnectionId oldConnectionId = peer.ConnectionId;
-                peer.ResetPeer(this.AllocateConnectionId(), record.SequenceNumber + 1);
+                peer.ResetPeer(this.AllocateConnectionId(peerAddress), record.SequenceNumber + 1);
 
                 // Inform the parent layer that the existing
                 // connection should be abandoned.
@@ -1102,7 +1102,7 @@ namespace Hazel.Dtls
 
             // Allocate state for the new peer and register it
             PeerData peer = new PeerData();
-            peer.ResetPeer(this.AllocateConnectionId(), record.SequenceNumber + 1);
+            peer.ResetPeer(this.AllocateConnectionId(peerAddress), record.SequenceNumber + 1);
 
             this.existingPeers[peerAddress] = peer;
 
@@ -1232,10 +1232,10 @@ namespace Hazel.Dtls
         /// <summary>
         /// Allocate a new connection id
         /// </summary>
-        private ConnectionId AllocateConnectionId()
+        private ConnectionId AllocateConnectionId(IPEndPoint endPoint)
         {
-            ulong rawConnectionId = (ulong)Interlocked.Increment(ref this.connectionId_unsafe);
-            return ConnectionId.Create(rawConnectionId);
+            int rawSerialId = Interlocked.Increment(ref this.connectionSerial_unsafe);
+            return ConnectionId.Create(endPoint, rawSerialId);
         }
     }
 }
index 339cb5b2db15372449436a26875271d4c55e6242..46a723a6555286cd4316d82f2f39627f3f5db9d8 100644 (file)
@@ -52,30 +52,22 @@ namespace Hazel.Udp.FewerThreads
 
         public struct ConnectionId : IEquatable<ConnectionId>
         {
-            public ulong Id;
+            public IPEndPoint EndPoint;
+            public int Serial;
 
-            public static ConnectionId Create(ulong id)
+            public static ConnectionId Create(IPEndPoint endPoint, int serial)
             {
-                ConnectionId result = new ConnectionId();
-                result.Id = id;
-                return result;
-            }
-
-            public static ConnectionId CreateFromEndPoint(IPEndPoint endPoint)
-            {
-                if (endPoint.AddressFamily != AddressFamily.InterNetwork)
-                {
-                    throw new ArgumentException("ConnectionId only supports IPv4");
-                }
-
-                ulong port = (ulong)endPoint.Port;
-                ulong address = (ulong)endPoint.Address.Address;
-                return Create((address << 32) | port);
+                return new ConnectionId{
+                    EndPoint = endPoint,
+                    Serial = serial,
+                };
             }
 
             public bool Equals(ConnectionId other)
             {
-                return this.Id == other.Id;
+                return this.Serial == other.Serial
+                    && this.EndPoint.Equals(other.EndPoint)
+                    ;
             }
 
             public override bool Equals(object obj)
@@ -90,7 +82,10 @@ namespace Hazel.Udp.FewerThreads
 
             public override int GetHashCode()
             {
-                return this.Id.GetHashCode();
+                ///NOTE(mendsley): We're only hashing the endpoint
+                /// here, as the common case will have one
+                /// connection per address+port tuple.
+                return this.EndPoint.GetHashCode();
             }
         }
 
@@ -235,7 +230,7 @@ namespace Hazel.Udp.FewerThreads
                         return;
                     }
 
-                    ConnectionId connectionId = ConnectionId.CreateFromEndPoint((IPEndPoint)remoteEP);
+                    ConnectionId connectionId = ConnectionId.Create((IPEndPoint)remoteEP, 0);
                     this.ProcessIncomingMessageFromOtherThread(message, (IPEndPoint)remoteEP, connectionId);
                 }
             }