From 49e1e1c8a473114d1a7397a4f34dbcb7f63cefa1 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sun, 8 Mar 2015 20:02:28 +0000
Subject: [PATCH] minor transport cleanup

---
 .../i2p/router/transport/ntcp/EstablishState.java    |  2 +-
 .../router/transport/udp/IntroductionManager.java    | 12 ++++++++----
 .../net/i2p/router/transport/udp/PacketBuilder.java  |  3 ++-
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java b/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java
index c7684993d1..e53d966669 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/EstablishState.java
@@ -849,8 +849,8 @@ class EstablishState {
                     _log.debug(prefix()+"Clock skew: " + diff + " ms");
                 }
 
-                sendInboundConfirm(_aliceIdent, tsA);
                 _con.setRemotePeer(_aliceIdent);
+                sendInboundConfirm(_aliceIdent, tsA);
                 if (_log.shouldLog(Log.DEBUG))
                     _log.debug(prefix()+"e_bobSig is " + _e_bobSig.length + " bytes long");
                 byte iv[] = _curEncrypted;  // reuse buf
diff --git a/router/java/src/net/i2p/router/transport/udp/IntroductionManager.java b/router/java/src/net/i2p/router/transport/udp/IntroductionManager.java
index 53c9cff0c7..6a0b412533 100644
--- a/router/java/src/net/i2p/router/transport/udp/IntroductionManager.java
+++ b/router/java/src/net/i2p/router/transport/udp/IntroductionManager.java
@@ -141,8 +141,9 @@ class IntroductionManager {
         if (_log.shouldLog(Log.DEBUG))
             _log.debug("removing peer " + peer.getRemoteHostId() + ", weRelayToThemAs " 
                        + peer.getWeRelayToThemAs() + ", theyRelayToUsAs " + peer.getTheyRelayToUsAs());
-        if (peer.getWeRelayToThemAs() > 0) 
-            _outbound.remove(Long.valueOf(peer.getWeRelayToThemAs()));
+        long id = peer.getWeRelayToThemAs(); 
+        if (id > 0) 
+            _outbound.remove(Long.valueOf(id));
         if (peer.getTheyRelayToUsAs() > 0) {
             _inbound.remove(peer);
         }
@@ -306,6 +307,9 @@ class IntroductionManager {
      *  Send a HolePunch to Alice, who will soon be sending us a RelayRequest.
      *  We should already have a session with Bob, but probably not with Alice.
      *
+     *  If we don't have a session with Bob, we removed the relay tag from
+     *  our _outbound table, so this won't work.
+     *
      *  We do some throttling here.
      */
     void receiveRelayIntro(RemoteHostId bob, UDPPacketReader reader) {
@@ -432,7 +436,7 @@ class IntroductionManager {
             if (_log.shouldLog(Log.INFO))
                 _log.info("Receive relay request from " + alice 
                       + " with unknown tag");
-            _context.statManager().addRateData("udp.receiveRelayRequestBadTag", 1, 0);
+            _context.statManager().addRateData("udp.receiveRelayRequestBadTag", 1);
             return;
         }
         if (_log.shouldLog(Log.INFO))
@@ -442,7 +446,7 @@ class IntroductionManager {
 
         // TODO throttle based on alice identity and/or intro tag?
 
-        _context.statManager().addRateData("udp.receiveRelayRequest", 1, 0);
+        _context.statManager().addRateData("udp.receiveRelayRequest", 1);
 
         // send that peer an introduction for alice
         _transport.send(_builder.buildRelayIntro(alice, charlie, reader.getRelayRequestReader()));
diff --git a/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java b/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java
index 64a96dcf04..8fb465aead 100644
--- a/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java
+++ b/router/java/src/net/i2p/router/transport/udp/PacketBuilder.java
@@ -16,6 +16,7 @@ import net.i2p.data.Hash;
 import net.i2p.data.router.RouterIdentity;
 import net.i2p.data.SessionKey;
 import net.i2p.data.Signature;
+import net.i2p.router.transport.TransportUtil;
 import net.i2p.util.Addresses;
 import net.i2p.util.Log;
 import net.i2p.util.SimpleByteCache;
@@ -1222,7 +1223,7 @@ class PacketBuilder {
             byte ikey[] = addr.getIntroducerKey(i);
             long tag = addr.getIntroducerTag(i);
             // let's not use an introducer on a privileged port, sounds like trouble
-            if (ikey == null || iport < 1024 || iport > 65535 ||
+            if (ikey == null || !TransportUtil.isValidPort(iport) ||
                 iaddr == null || tag <= 0 ||
                 // must be IPv4 for now as we don't send Alice IP/port, see below
                 iaddr.getAddress().length != 4 ||
-- 
GitLab