From f57abe84bdcb0267165c3869124aee427935fc99 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Thu, 17 Mar 2022 16:04:28 -0400
Subject: [PATCH] SSU: Peer Test refactor

to store Bob's PeerState directly in PeerTestState
prep for SSU2
---
 .../router/transport/udp/PeerTestEvent.java   |  2 +-
 .../router/transport/udp/PeerTestManager.java | 17 ++++-----
 .../router/transport/udp/PeerTestState.java   | 35 +++++++------------
 3 files changed, 19 insertions(+), 35 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/udp/PeerTestEvent.java b/router/java/src/net/i2p/router/transport/udp/PeerTestEvent.java
index 063c8445a2..d402f0964c 100644
--- a/router/java/src/net/i2p/router/transport/udp/PeerTestEvent.java
+++ b/router/java/src/net/i2p/router/transport/udp/PeerTestEvent.java
@@ -83,7 +83,7 @@ class PeerTestEvent extends SimpleTimer2.TimedEvent {
         if (bob != null) {
             if (_log.shouldLog(Log.INFO))
                 _log.info("Running periodic test with bob = " + bob);
-            _testManager.runTest(bob.getRemoteIPAddress(), bob.getRemotePort(), bob.getCurrentCipherKey(), bob.getCurrentMACKey());
+            _testManager.runTest(bob);
             setLastTested(isIPv6);
         } else {
             if (_log.shouldLog(Log.WARN))
diff --git a/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java b/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
index 1897f5c47f..bb087192cf 100644
--- a/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
+++ b/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
@@ -177,23 +177,21 @@ class PeerTestManager {
      *
      *  @param bobIP IPv4 only
      */
-    public synchronized void runTest(InetAddress bobIP, int bobPort, SessionKey bobCipherKey, SessionKey bobMACKey) {
+    public synchronized void runTest(PeerState bob) {
         if (_currentTest != null) {
             if (_log.shouldLog(Log.WARN))
-                _log.warn("We are already running a test: " + _currentTest + ", aborting test with bob = " + bobIP);
+                _log.warn("We are already running a test: " + _currentTest + ", aborting test with bob = " + bob);
             return;
         }
+        InetAddress bobIP = bob.getRemoteIPAddress();
         if (_transport.isTooClose(bobIP.getAddress())) {
             if (_log.shouldLog(Log.WARN))
                 _log.warn("Not running test with Bob too close to us " + bobIP);
             return;
         }
-        PeerTestState test = new PeerTestState(ALICE, bobIP instanceof Inet6Address,
+        PeerTestState test = new PeerTestState(ALICE, bob, bobIP instanceof Inet6Address,
                                                _context.random().nextLong(MAX_NONCE),
                                                _context.clock().now());
-        test.setBobIP(bobIP);
-        test.setBobPort(bobPort);
-        test.setBobKeys(bobCipherKey, bobMACKey);
         _currentTest = test;
         _currentTestComplete = false;
         
@@ -702,7 +700,7 @@ class PeerTestManager {
         boolean isNew = false;
         if (state == null) {
             isNew = true;
-            state = new PeerTestState(CHARLIE, sz == 16, nonce, now);
+            state = new PeerTestState(CHARLIE, bob, sz == 16, nonce, now);
         } else {
             if (state.getReceiveBobTime() > now - (RESEND_TIMEOUT / 2)) {
                 if (_log.shouldLog(Log.WARN))
@@ -730,10 +728,7 @@ class PeerTestManager {
             state.setAliceIP(aliceIP);
             state.setAlicePort(alicePort);
             state.setAliceIntroKey(aliceIntroKey);
-            state.setBobIP(bobIP);
-            state.setBobPort(from.getPort());
             state.setReceiveBobTime(now);
-            state.setBobKeys(bob.getCurrentCipherKey(), bob.getCurrentMACKey());
             
             // we send two packets below, but increment just once
             if (state.incrementPacketsRelayed() > MAX_RELAYED_PER_TEST_CHARLIE) {
@@ -834,7 +829,7 @@ class PeerTestManager {
             boolean isNew = false;
             if (state == null) {
                 isNew = true;
-                state = new PeerTestState(BOB, isIPv6, nonce, now);
+                state = new PeerTestState(BOB, null, isIPv6, nonce, now);
             } else {
                 if (state.getReceiveAliceTime() > now - (RESEND_TIMEOUT / 2)) {
                     if (_log.shouldLog(Log.WARN))
diff --git a/router/java/src/net/i2p/router/transport/udp/PeerTestState.java b/router/java/src/net/i2p/router/transport/udp/PeerTestState.java
index 3d03f82609..f45962f695 100644
--- a/router/java/src/net/i2p/router/transport/udp/PeerTestState.java
+++ b/router/java/src/net/i2p/router/transport/udp/PeerTestState.java
@@ -16,8 +16,7 @@ class PeerTestState {
     private final boolean _isIPv6;
     private InetAddress _aliceIP;
     private int _alicePort;
-    private InetAddress _bobIP;
-    private int _bobPort;
+    private final PeerState _bob;
     private InetAddress _charlieIP;
     private int _charliePort;
     private InetAddress _aliceIPFromCharlie;
@@ -26,8 +25,6 @@ class PeerTestState {
     private SessionKey _aliceCipherKey;
     private SessionKey _aliceMACKey;
     private SessionKey _charlieIntroKey;
-    private SessionKey _bobCipherKey;
-    private SessionKey _bobMACKey;
     private final long _beginTime;
     private long _lastSendTime;
     private long _receiveAliceTime;
@@ -37,8 +34,12 @@ class PeerTestState {
     
     public enum Role {ALICE, BOB, CHARLIE};
     
-    public PeerTestState(Role role, boolean isIPv6, long nonce, long now) {
+    /**
+     * @param bob null if role is BOB
+     */
+    public PeerTestState(Role role, PeerState bob, boolean isIPv6, long nonce, long now) {
         _ourRole = role;
+        _bob = bob;
         _isIPv6 = isIPv6;
         _testNonce = nonce;
         _beginTime = now;
@@ -63,8 +64,7 @@ class PeerTestState {
      */
     public InetAddress getAliceIP() { return _aliceIP; }
     public void setAliceIP(InetAddress ip) { _aliceIP = ip; }
-    public InetAddress getBobIP() { return _bobIP; }
-    public void setBobIP(InetAddress ip) { _bobIP = ip; }
+    public InetAddress getBobIP() { return _bob.getRemoteIPAddress(); }
     public InetAddress getCharlieIP() { return _charlieIP; }
     public void setCharlieIP(InetAddress ip) { _charlieIP = ip; }
     public InetAddress getAliceIPFromCharlie() { return _aliceIPFromCharlie; }
@@ -77,8 +77,7 @@ class PeerTestState {
      */
     public int getAlicePort() { return _alicePort; }
     public void setAlicePort(int alicePort) { _alicePort = alicePort; }
-    public int getBobPort() { return _bobPort; }
-    public void setBobPort(int bobPort) { _bobPort = bobPort; }
+    public int getBobPort() { return _bob.getRemotePort(); }
     public int getCharliePort() { return _charliePort; }
     public void setCharliePort(int charliePort) { _charliePort = charliePort; }
     
@@ -111,19 +110,9 @@ class PeerTestState {
     public SessionKey getCharlieIntroKey() { return _charlieIntroKey; }
     public void setCharlieIntroKey(SessionKey key) { _charlieIntroKey = key; }
 
-    public SessionKey getBobCipherKey() { return _bobCipherKey; }
-    public SessionKey getBobMACKey() { return _bobMACKey; }
+    public SessionKey getBobCipherKey() { return _bob.getCurrentCipherKey(); }
+    public SessionKey getBobMACKey() { return _bob.getCurrentMACKey(); }
 
-    /**
-     *  @param ck cipher key
-     *  @param mk MAC key
-     *  @since 0.9.52
-     */
-    public void setBobKeys(SessionKey ck, SessionKey mk) {
-        _bobCipherKey = ck;
-        _bobMACKey = mk;
-    }
-    
     /** when did this test begin? */
     public long getBeginTime() { return _beginTime; }
 
@@ -159,8 +148,8 @@ class PeerTestState {
             buf.append("; Alice: ").append(_aliceIP).append(':').append(_alicePort);
         if (_aliceIPFromCharlie != null)
             buf.append(" (fromCharlie ").append(_aliceIPFromCharlie).append(':').append(_alicePortFromCharlie).append(')');
-        if (_bobIP != null)
-            buf.append("; Bob: ").append(_bobIP).append(':').append(_bobPort);
+        if (_bob != null)
+            buf.append("; Bob: ").append(_bob.toString());
         if (_charlieIP != null)
             buf.append(" Charlie: ").append(_charlieIP).append(':').append(_charliePort);
         if (_lastSendTime > 0)
-- 
GitLab