From 6e053689b90b49de60bf14a1c682d9d3fd41bc7c Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sun, 2 Dec 2018 19:14:36 +0000
Subject: [PATCH] Transport: Add methods to force-disconnect a peer

---
 router/java/src/net/i2p/router/CommSystemFacade.java |  7 +++++++
 .../i2p/router/transport/CommSystemFacadeImpl.java   | 10 ++++++++++
 .../java/src/net/i2p/router/transport/Transport.java |  7 +++++++
 .../net/i2p/router/transport/TransportManager.java   | 11 +++++++++++
 .../net/i2p/router/transport/ntcp/NTCPTransport.java | 12 ++++++++++++
 .../net/i2p/router/transport/udp/UDPTransport.java   | 12 ++++++++++++
 6 files changed, 59 insertions(+)

diff --git a/router/java/src/net/i2p/router/CommSystemFacade.java b/router/java/src/net/i2p/router/CommSystemFacade.java
index 378ac52b11..49760ae85a 100644
--- a/router/java/src/net/i2p/router/CommSystemFacade.java
+++ b/router/java/src/net/i2p/router/CommSystemFacade.java
@@ -102,6 +102,13 @@ public abstract class CommSystemFacade implements Service {
      * @since 0.9.24
      */
     public void mayDisconnect(Hash peer) {}
+    
+    /**
+     * Tell the comm system to disconnect from this peer.
+     *
+     * @since 0.9.38
+     */
+    public void forceDisconnect(Hash peer) {}
 
     /** @since 0.8.11 */
     public String getOurCountry() { return null; }
diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
index a411e06408..37ab3ac395 100644
--- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
+++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
@@ -195,6 +195,16 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         _manager.mayDisconnect(peer); 
     }
     
+    /**
+     * Tell the comm system to disconnect from this peer.
+     *
+     * @since 0.9.38
+     */
+    @Override
+    public void forceDisconnect(Hash peer) {
+        _manager.forceDisconnect(peer); 
+    }
+    
     @Override
     public List<String> getMostRecentErrorMessages() { 
         return _manager.getMostRecentErrorMessages(); 
diff --git a/router/java/src/net/i2p/router/transport/Transport.java b/router/java/src/net/i2p/router/transport/Transport.java
index efeb10ec71..a6fed0efa8 100644
--- a/router/java/src/net/i2p/router/transport/Transport.java
+++ b/router/java/src/net/i2p/router/transport/Transport.java
@@ -196,4 +196,11 @@ public interface Transport {
      * @since 0.9.24
      */
     public void mayDisconnect(Hash peer);
+
+    /**
+     * Tell the transport to disconnect from this peer.
+     *
+     * @since 0.9.38
+     */
+    public void forceDisconnect(Hash peer);
 }
diff --git a/router/java/src/net/i2p/router/transport/TransportManager.java b/router/java/src/net/i2p/router/transport/TransportManager.java
index f9ac847968..a09e8986c6 100644
--- a/router/java/src/net/i2p/router/transport/TransportManager.java
+++ b/router/java/src/net/i2p/router/transport/TransportManager.java
@@ -555,6 +555,17 @@ public class TransportManager implements TransportEventListener {
         }
     }
     
+    /**
+     * Tell the transports to disconnect from this peer.
+     *
+     * @since 0.9.38
+     */
+    void forceDisconnect(Hash peer) {
+        for (Transport t : _transports.values()) {
+             t.forceDisconnect(peer);
+        }
+    }
+    
     /**
      * Was the peer UNreachable (outbound only) on any transport,
      * based on the last time we tried it for each transport?
diff --git a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java
index c20aca596b..cf43fb8a5c 100644
--- a/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java
+++ b/router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java
@@ -640,6 +640,18 @@ public class NTCPTransport extends TransportImpl {
         }
     }
 
+    /**
+     * Tell the transport to disconnect from this peer.
+     *
+     * @since 0.9.38
+     */
+    public void forceDisconnect(Hash peer) {
+        NTCPConnection con = _conByIdent.remove(peer);
+        if (con != null) {
+            con.close();
+        }
+    }
+
     /**
      * @return usually the con passed in, but possibly a second connection with the same peer...
      *         only con or null as of 0.9.37
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 7f1c55ceec..93420c27c6 100644
--- a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java
+++ b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java
@@ -2679,6 +2679,18 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
         }
     }
 
+    /**
+     * Tell the transport to disconnect from this peer.
+     *
+     * @since 0.9.38
+     */
+    public void forceDisconnect(Hash peer) {
+        PeerState ps =  _peersByIdent.get(peer);
+        if (ps != null) {
+            dropPeer(ps, true, "router");
+        }
+    }
+
     public boolean allowConnection() {
             return _peersByIdent.size() < getMaxConnections();
     }
-- 
GitLab