From ef08dfad55d8b952342366dbdaf0cbe60507cd4d Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sun, 29 May 2022 10:18:19 -0400
Subject: [PATCH] SSU2: Remove role field from peer test block

to match spec change
---
 .../router/transport/udp/PeerTestManager.java | 55 +++++++------------
 .../i2p/router/transport/udp/SSU2Payload.java |  2 +-
 .../i2p/router/transport/udp/SSU2Util.java    | 18 +++---
 3 files changed, 30 insertions(+), 45 deletions(-)

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 aac9cef81a..a26f7c5aae 100644
--- a/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
+++ b/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
@@ -354,14 +354,13 @@ class PeerTestManager {
                 int alicePort = test.getAlicePort();
                 byte[] aliceIP = addr.getAddress();
                 int iplen = aliceIP.length;
-                byte[] data = new byte[13 + iplen];
-                data[0] = 1;  // alice
-                data[1] = 2;  // version
-                DataHelper.toLong(data, 2, 4, nonce);
-                DataHelper.toLong(data, 6, 4, _context.clock().now() / 1000);
-                data[10] = (byte) (iplen + 2);
-                DataHelper.toLong(data, 11, 2, alicePort);
-                System.arraycopy(aliceIP, 0, data, 13, iplen);
+                byte[] data = new byte[12 + iplen];
+                data[0] = 2;  // version
+                DataHelper.toLong(data, 1, 4, nonce);
+                DataHelper.toLong(data, 5, 4, _context.clock().now() / 1000);
+                data[9] = (byte) (iplen + 2);
+                DataHelper.toLong(data, 10, 2, alicePort);
+                System.arraycopy(aliceIP, 0, data, 12, iplen);
                 packet = _packetBuilder2.buildPeerTestFromAlice(test.getCharlieIP(), test.getCharliePort(),
                                                                 test.getCharlieIntroKey(),
                                                                 sendId, rcvId, data);
@@ -837,24 +836,14 @@ class PeerTestManager {
      * @since 0.9.54
      */
     public void receiveTest(RemoteHostId from, PeerState2 fromPeer, int msg, int status, Hash h, byte[] data) {
-        PeerTestState.Role role;
-        if (data[0] == 1) {
-            role = ALICE;
-        } else if (data[0] == 3) {
-            role = CHARLIE;
-        } else {
-            if (_log.shouldWarn())
-                _log.warn("Bad role " + (data[0] & 0xff) + " from " + from + ' ' + fromPeer);
-            return;
-        }
-        if (data[1] != 2) {
+        if (data[0] != 2) {
             if (_log.shouldWarn())
-                _log.warn("Bad version " + (data[1] & 0xff) + " from " + from + ' ' + fromPeer);
+                _log.warn("Bad version " + (data[0] & 0xff) + " from " + from + ' ' + fromPeer);
             return;
         }
-        long nonce = DataHelper.fromLong(data, 2, 4);
-        long time = DataHelper.fromLong(data, 6, 4) * 1000;
-        int iplen = data[10] & 0xff;
+        long nonce = DataHelper.fromLong(data, 1, 4);
+        long time = DataHelper.fromLong(data, 5, 4) * 1000;
+        int iplen = data[9] & 0xff;
         if (iplen != 0 && iplen != 6 && iplen != 18) {
             if (_log.shouldLog(Log.WARN))
                 _log.warn("Bad IP length " + iplen);
@@ -864,9 +853,9 @@ class PeerTestManager {
         int testPort;
         byte[] testIP;
         if (iplen != 0) {
-            testPort = (int) DataHelper.fromLong(data, 11, 2);
+            testPort = (int) DataHelper.fromLong(data, 10, 2);
             testIP = new byte[iplen - 2];
-            System.arraycopy(data, 13, testIP, 0, iplen - 2);
+            System.arraycopy(data, 12, testIP, 0, iplen - 2);
         } else {
             testPort = 0;
             testIP = null;
@@ -885,7 +874,6 @@ class PeerTestManager {
                        " msg: " + msg +
                        " status: " + status +
                        " hash: " + h +
-                       " role: " + role +
                        " nonce: " + nonce +
                        " time: " + DataHelper.formatTime(time) +
                        " ip/port: " + Addresses.toString(testIP, testPort) +
@@ -1297,14 +1285,13 @@ class PeerTestManager {
                 int alicePort = state.getAlicePort();
                 byte[] aliceIP = addr.getAddress();
                 iplen = aliceIP.length;
-                data = new byte[13 + iplen];
-                data[0] = 3;  // charlie
-                data[1] = 2;  // version
-                DataHelper.toLong(data, 2, 4, nonce);
-                DataHelper.toLong(data, 6, 4, now / 1000);
-                data[10] = (byte) (iplen + 2);
-                DataHelper.toLong(data, 11, 2, alicePort);
-                System.arraycopy(aliceIP, 0, data, 13, iplen);
+                data = new byte[12 + iplen];
+                data[0] = 2;  // version
+                DataHelper.toLong(data, 1, 4, nonce);
+                DataHelper.toLong(data, 5, 4, now / 1000);
+                data[9] = (byte) (iplen + 2);
+                DataHelper.toLong(data, 10, 2, alicePort);
+                System.arraycopy(aliceIP, 0, data, 12, iplen);
                 if (_log.shouldDebug())
                     _log.debug("Send msg 7 to alice on " + state);
                 UDPPacket packet = _packetBuilder2.buildPeerTestToAlice(addr, alicePort,
diff --git a/router/java/src/net/i2p/router/transport/udp/SSU2Payload.java b/router/java/src/net/i2p/router/transport/udp/SSU2Payload.java
index 4787d9d941..cbb1feb107 100644
--- a/router/java/src/net/i2p/router/transport/udp/SSU2Payload.java
+++ b/router/java/src/net/i2p/router/transport/udp/SSU2Payload.java
@@ -310,7 +310,7 @@ class SSU2Payload {
                 case BLOCK_PEERTEST: {
                     if (isHandshake)
                         throw new IOException("Illegal block in handshake: " + type);
-                    if (len < 20) // 20 byte data w/ IPv4 (hash and sig optional)
+                    if (len < 19) // 19 byte data w/ IPv4 (hash and sig optional)
                         throw new IOException("Bad length for PEERTEST: " + len);
                     int mnum = payload[i] & 0xff;
                     if (mnum == 0 || mnum > 7)
diff --git a/router/java/src/net/i2p/router/transport/udp/SSU2Util.java b/router/java/src/net/i2p/router/transport/udp/SSU2Util.java
index 3b404f9372..4ba69341d0 100644
--- a/router/java/src/net/i2p/router/transport/udp/SSU2Util.java
+++ b/router/java/src/net/i2p/router/transport/udp/SSU2Util.java
@@ -173,25 +173,23 @@ final class SSU2Util {
      *
      *  @param h to be included in sig, not included in data
      *  @param h2 may be null, to be included in sig, not included in data
+     *  @param role unused
      *  @param ip may be null
      *  @return null on failure
      */
     public static byte[] createPeerTestData(I2PAppContext ctx, Hash h, Hash h2,
                                             PeerTestState.Role role, long nonce, byte[] ip, int port,
                                             SigningPrivateKey spk) {
-        int datalen = 13 + (ip != null ? ip.length : 0);
+        int datalen = 12 + (ip != null ? ip.length : 0);
         byte[] data = new byte[datalen + spk.getType().getSigLen()];
-        if (role == PeerTestState.Role.BOB)
-            throw new IllegalArgumentException();
-        data[0] = (byte) ((role == PeerTestState.Role.ALICE) ? 1 : 3);
-        data[1] = 2;  // version
-        DataHelper.toLong(data, 2, 4, nonce);
-        DataHelper.toLong(data, 6, 4, ctx.clock().now() / 1000);
+        data[0] = 2;  // version
+        DataHelper.toLong(data, 1, 4, nonce);
+        DataHelper.toLong(data, 5, 4, ctx.clock().now() / 1000);
         int iplen = (ip != null) ? ip.length : 0;
-        data[10] = (byte) (ip != null ? iplen + 2 : 0);
+        data[9] = (byte) (ip != null ? iplen + 2 : 0);
         if (ip != null) {
-            DataHelper.toLong(data, 11, 2, port);
-            System.arraycopy(ip, 0, data, 13, iplen);
+            DataHelper.toLong(data, 10, 2, port);
+            System.arraycopy(ip, 0, data, 12, iplen);
         }
         Signature sig = sign(ctx, PEER_TEST_PROLOGUE, h, h2, data, datalen, spk);
         if (sig == null)
-- 
GitLab