From 008943f6994f85fcfea7e47a4003721d45c7ed57 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Wed, 27 Apr 2022 08:30:22 -0400
Subject: [PATCH] SSU2: Peer Test fixes part 6

Change size/ip/port fields to match changes in proposal 159
---
 history.txt                                   | 10 +++++++
 .../src/net/i2p/router/RouterVersion.java     |  2 +-
 .../router/transport/udp/PeerTestManager.java | 26 +++++++++++--------
 .../i2p/router/transport/udp/SSU2Util.java    | 11 ++++----
 4 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/history.txt b/history.txt
index 8dd4aedd78..9f02ba5f27 100644
--- a/history.txt
+++ b/history.txt
@@ -1,3 +1,13 @@
+2022-04-27 zzz
+ * SSU2: Peer test updates and fixes
+
+2022-04-24 zzz
+ * SSU: Publish empty IPv6 address when missing introducers
+ * SSU2: Finish peer test implementation
+
+2022-04-17 zzz
+ * More soft restart fixes
+
 2022-04-14 zzz
  * Startup: Don't set our RI loaded from disk if too old
 
diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java
index 2f628a65d0..ad00f8fca9 100644
--- a/router/java/src/net/i2p/router/RouterVersion.java
+++ b/router/java/src/net/i2p/router/RouterVersion.java
@@ -18,7 +18,7 @@ public class RouterVersion {
     /** deprecated */
     public final static String ID = "Git";
     public final static String VERSION = CoreVersion.VERSION;
-    public final static long BUILD = 14;
+    public final static long BUILD = 15;
 
     /** for example "-test" */
     public final static String EXTRA = "";
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 7e33e014ff..51b3521812 100644
--- a/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
+++ b/router/java/src/net/i2p/router/transport/udp/PeerTestManager.java
@@ -358,9 +358,9 @@ class PeerTestManager {
                 data[1] = 2;  // version
                 DataHelper.toLong(data, 2, 4, nonce);
                 DataHelper.toLong(data, 6, 4, _context.clock().now() / 1000);
-                data[10] = (byte) iplen;
-                System.arraycopy(aliceIP, 0, data, 11, iplen);
-                DataHelper.toLong(data, 11 + iplen, 2, alicePort);
+                data[10] = (byte) (iplen + 2);
+                DataHelper.toLong(data, 11, 2, alicePort);
+                System.arraycopy(aliceIP, 0, data, 13, iplen);
                 packet = _packetBuilder2.buildPeerTestFromAlice(test.getCharlieIP(), test.getCharliePort(),
                                                                 test.getCharlieIntroKey(),
                                                                 sendId, rcvId, data);
@@ -842,20 +842,24 @@ class PeerTestManager {
         long nonce = DataHelper.fromLong(data, 2, 4);
         long time = DataHelper.fromLong(data, 6, 4) * 1000;
         int iplen = data[10] & 0xff;
-        if (iplen != 0 && iplen != 4 && iplen != 16) {
+        if (iplen != 0 && iplen != 6 && iplen != 18) {
             if (_log.shouldLog(Log.WARN))
                 _log.warn("Bad IP length " + iplen);
             return;
         }
-        boolean isIPv6 = iplen == 16;
+        boolean isIPv6 = iplen == 18;
+        int testPort;
         byte[] testIP;
         if (iplen != 0) {
-            testIP = new byte[iplen];
-            System.arraycopy(data, 11, testIP, 0, iplen);
+            testPort = (int) DataHelper.fromLong(data, 11, 2);
+            testIP = new byte[iplen - 2];
+            System.arraycopy(data, 13, testIP, 0, iplen - 2);
         } else {
+            testPort = 0;
             testIP = null;
+            if (status == 0)
+                status = 999;
         }
-        int testPort = (int) DataHelper.fromLong(data, 11 + iplen, 2);
         Long lNonce = Long.valueOf(nonce);
         PeerTestState state;
         if (msg == 4 || msg == 5 || msg == 7)
@@ -1285,9 +1289,9 @@ class PeerTestManager {
                 data[1] = 2;  // version
                 DataHelper.toLong(data, 2, 4, nonce);
                 DataHelper.toLong(data, 6, 4, now / 1000);
-                data[10] = (byte) iplen;
-                System.arraycopy(aliceIP, 0, data, 11, iplen);
-                DataHelper.toLong(data, 11 + iplen, 2, alicePort);
+                data[10] = (byte) (iplen + 2);
+                DataHelper.toLong(data, 11, 2, alicePort);
+                System.arraycopy(aliceIP, 0, data, 13, 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/SSU2Util.java b/router/java/src/net/i2p/router/transport/udp/SSU2Util.java
index bf7b3246db..3b404f9372 100644
--- a/router/java/src/net/i2p/router/transport/udp/SSU2Util.java
+++ b/router/java/src/net/i2p/router/transport/udp/SSU2Util.java
@@ -179,7 +179,7 @@ final class SSU2Util {
     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.length;
+        int datalen = 13 + (ip != null ? ip.length : 0);
         byte[] data = new byte[datalen + spk.getType().getSigLen()];
         if (role == PeerTestState.Role.BOB)
             throw new IllegalArgumentException();
@@ -188,10 +188,11 @@ final class SSU2Util {
         DataHelper.toLong(data, 2, 4, nonce);
         DataHelper.toLong(data, 6, 4, ctx.clock().now() / 1000);
         int iplen = (ip != null) ? ip.length : 0;
-        data[10] = (byte) iplen;
-        if (ip != null)
-            System.arraycopy(ip, 0, data, 11, iplen);
-        DataHelper.toLong(data, 11 + iplen, 2, port);
+        data[10] = (byte) (ip != null ? iplen + 2 : 0);
+        if (ip != null) {
+            DataHelper.toLong(data, 11, 2, port);
+            System.arraycopy(ip, 0, data, 13, iplen);
+        }
         Signature sig = sign(ctx, PEER_TEST_PROLOGUE, h, h2, data, datalen, spk);
         if (sig == null)
             return null;
-- 
GitLab