From 0560b3221d9e3ccd7ab5655cc15654c0c1e8c54d Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Thu, 24 Mar 2022 05:21:58 -0400
Subject: [PATCH] SSU2: Flush tokens on ip/port change

both inbound and outbound
---
 .../router/transport/udp/EstablishmentManager.java | 14 ++++++++++++--
 .../net/i2p/router/transport/udp/UDPTransport.java |  9 +++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/udp/EstablishmentManager.java b/router/java/src/net/i2p/router/transport/udp/EstablishmentManager.java
index cefb942c8f..bc6b576604 100644
--- a/router/java/src/net/i2p/router/transport/udp/EstablishmentManager.java
+++ b/router/java/src/net/i2p/router/transport/udp/EstablishmentManager.java
@@ -1828,7 +1828,7 @@ class EstablishmentManager {
     }
 
     /**
-     *  Remove our outbound tokens for this length
+     *  Remove our tokens for this length
      *
      *  @since 0.9.54
      */
@@ -1845,10 +1845,17 @@ class EstablishmentManager {
                     iter.remove();
             }
         }
+        synchronized(_inboundTokens) {
+            for (Iterator<Map.Entry<RemoteHostId, Token>> iter = _inboundTokens.entrySet().iterator(); iter.hasNext(); ) {
+                Map.Entry<RemoteHostId, Token> e = iter.next();
+                if (e.getKey().getIP().length == len || e.getValue().expires < now)
+                    iter.remove();
+            }
+        }
     }
 
     /**
-     *  Remove all outbound tokens
+     *  Remove all tokens
      *
      *  @since 0.9.54
      */
@@ -1858,6 +1865,9 @@ class EstablishmentManager {
         synchronized(_outboundTokens) {
             _outboundTokens.clear();
         }
+        synchronized(_inboundTokens) {
+            _inboundTokens.clear();
+        }
     }
 
     /**
diff --git a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java
index 415bacc39d..4913eda82a 100644
--- a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java
+++ b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java
@@ -1424,6 +1424,15 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
                     //     (_context.clock().now() - _reachabilityStatusLastUpdated > 2*TEST_FREQUENCY) ) {
                         // they told us something different and our tests are either old or failing
                     if (rebuild) {
+                            if (_enableSSU2) {
+                                // flush SSU2 tokens
+                                if (ourPort != externalListenPort) {
+                                    _establisher.portChanged();
+                                } else if (externalListenHost != null && !Arrays.equals(ourIP, externalListenHost)) {
+                                    _establisher.ipChanged(isIPv6);
+                                }
+                            }
+
                             if (_log.shouldLog(Log.WARN))
                                 _log.warn("Trying to change our external address to " +
                                           Addresses.toString(ourIP, ourPort));
-- 
GitLab