From 5a38154e152c110a0d0d1e03e660d6e0406e05ae Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 20 Sep 2020 18:42:26 +0100 Subject: [PATCH] make sure the pongs uuid matches the last sent ping uuid --- .../muwire/core/connection/Connection.groovy | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy index fa1b2ea5..b90c6054 100644 --- a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy @@ -49,6 +49,8 @@ abstract class Connection implements Closeable { protected final String name long lastPingSentTime, lastPongReceivedTime + + private volatile UUID lastPingUUID Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache, TrustService trustService, MuWireSettings settings) { @@ -132,7 +134,8 @@ abstract class Connection implements Closeable { def ping = [:] ping.type = "Ping" ping.version = 1 - ping.uuid = UUID.randomUUID().toString() + lastPingUUID = UUID.randomUUID() + ping.uuid = lastPingUUID.toString() messages.put(ping) lastPingSentTime = System.currentTimeMillis() } @@ -177,6 +180,22 @@ abstract class Connection implements Closeable { lastPongReceivedTime = System.currentTimeMillis() if (pong.pongs == null) throw new Exception("Pong doesn't have pongs") + + if (lastPingUUID == null) { + log.fine "$name received an unexpected pong" + return + } + if (pong.uuid == null) { + log.fine "$name pong doesn't have uuid" + return + } + UUID pongUUID = UUID.fromString(pong.uuid) + if (pongUUID != lastPingUUID) { + log.fine "$name ping/pong uuid mismatch" + return + } + lastPingUUID = null + pong.pongs.stream().limit(2).forEach { def dest = new Destination(it) eventBus.publish(new HostDiscoveredEvent(destination: dest))