From 129b16d93d945a218faa6084abc0d1c02d8b7f8f Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Tue, 19 Jun 2012 20:26:46 +0000
Subject: [PATCH]  * Streaming:    - Listen only on local port if set    -
 Listen only for streaming protocol if configured (new option)    - Javadocs
 re: ports

---
 .../i2p/i2ptunnel/I2PTunnelHTTPServer.java    |  2 +-
 .../i2p/client/streaming/I2PSocketEepGet.java |  2 +-
 .../client/streaming/I2PSocketOptions.java    |  3 +++
 .../streaming/I2PSocketOptionsImpl.java       |  3 +++
 .../client/streaming/ConnectionManager.java   |  5 +++-
 .../client/streaming/ConnectionOptions.java   | 25 +++++++++++++++++++
 core/java/src/net/i2p/client/I2PSession.java  |  6 +++++
 7 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java
index 58f0c2f4b4..7e666e7dd2 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java
@@ -122,7 +122,7 @@ public class I2PTunnelHTTPServer extends I2PTunnelServer {
             Properties opts = getTunnel().getClientOptions();
             String spoofHost;
             int ourPort = socket.getLocalPort();
-            if (ourPort != 80 && ourPort > 0 && ourPort < 65535 && opts != null) {
+            if (ourPort != 80 && ourPort > 0 && ourPort <= 65535 && opts != null) {
                 String portSpoof = opts.getProperty("spoofedHost." + ourPort);
                 if (portSpoof != null)
                     spoofHost = portSpoof.trim();
diff --git a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java
index e548055c3d..4bad3f8891 100644
--- a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java
+++ b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java
@@ -111,7 +111,7 @@ public class I2PSocketEepGet extends EepGet {
             if ("http".equals(url.getProtocol())) {
                 String host = url.getHost();
                 int port = url.getPort();
-                if (port <= 0 || port >= 65535)
+                if (port <= 0 || port > 65535)
                     port = 80;
 
                 // HTTP Proxy compatibility http://i2p/B64KEY/blah
diff --git a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptions.java b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptions.java
index d926eb8313..c20a89a591 100644
--- a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptions.java
+++ b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptions.java
@@ -105,6 +105,9 @@ public interface I2PSocketOptions {
 
     /**
      *  The local port.
+     *  Zero (default) means you will receive traffic on all ports.
+     *  Nonzero means you will get traffic ONLY for that port, use with care,
+     *  as most applications do not specify a remote port.
      *  @param port 0 - 65535
      *  @since 0.8.9
      */
diff --git a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptionsImpl.java b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptionsImpl.java
index cb66b1486e..5d0636676b 100644
--- a/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptionsImpl.java
+++ b/apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketOptionsImpl.java
@@ -200,6 +200,9 @@ class I2PSocketOptionsImpl implements I2PSocketOptions {
 
     /**
      *  The local port.
+     *  Zero (default) means you will receive traffic on all ports.
+     *  Nonzero means you will get traffic ONLY for that port, use with care,
+     *  as most applications do not specify a remote port.
      *  @param port 0 - 65535
      *  @since 0.8.9
      */
diff --git a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java
index f9582f0b75..b2d9a201ac 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java
@@ -67,7 +67,10 @@ class ConnectionManager {
         // TODO change proto to PROTO_STREAMING someday.
         // Right now we get everything, and rely on Datagram to specify PROTO_UDP.
         // PacketQueue has sent PROTO_STREAMING since the beginning of mux support (0.7.1)
-        _session.addMuxedSessionListener(_messageHandler, I2PSession.PROTO_ANY, I2PSession.PORT_ANY);
+        // As of 0.9.1, new option to enforce streaming protocol, off by default
+        // As of 0.9.1, listen on configured port (default 0 = all)
+        int protocol = defaultOptions.getEnforceProtocol() ? I2PSession.PROTO_STREAMING : I2PSession.PROTO_ANY;
+        _session.addMuxedSessionListener(_messageHandler, protocol, defaultOptions.getLocalPort());
         _outboundQueue = new PacketQueue(_context, _session, this);
         /** Socket timeout for accept() */
         _soTimeout = -1;
diff --git a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionOptions.java b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionOptions.java
index 7f8616b3ef..6b30b2d4de 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionOptions.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionOptions.java
@@ -19,6 +19,7 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
     private int _connectDelay;
     private boolean _fullySigned;
     private boolean _answerPings;
+    private boolean _enforceProto;
     private volatile int _windowSize;
     private int _receiveWindow;
     private int _profile;
@@ -87,6 +88,8 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
     public static final String PROP_MAX_TOTAL_CONNS_MIN = "i2p.streaming.maxTotalConnsPerMinute";
     public static final String PROP_MAX_TOTAL_CONNS_HOUR = "i2p.streaming.maxTotalConnsPerHour";
     public static final String PROP_MAX_TOTAL_CONNS_DAY = "i2p.streaming.maxTotalConnsPerDay";
+    /** @since 0.9.1 */
+    public static final String PROP_ENFORCE_PROTO = "i2p.streaming.enforceProtocol";
     
     private static final int TREND_COUNT = 3;
     static final int INITIAL_WINDOW_SIZE = 6;
@@ -95,6 +98,11 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
     public static final int DEFAULT_INITIAL_ACK_DELAY = 2*1000;    
     static final int MIN_WINDOW_SIZE = 1;
     private static final boolean DEFAULT_ANSWER_PINGS = true;
+    /**
+     *  If PROTO is enforced, we cannot communicate with destinations earlier than version 0.7.1.
+     *  @since 0.9.1
+     */
+    private static final boolean DEFAULT_ENFORCE_PROTO = true;
 
     // Syncronization fix, but doing it this way causes NPE...
     // FIXME private final int _trend[] = new int[TREND_COUNT]; FIXME
@@ -284,6 +292,7 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
             //setWriteTimeout(opts.getWriteTimeout());
             //setReadTimeout(opts.getReadTimeout());
             setAnswerPings(opts.getAnswerPings());
+            setEnforceProtocol(opts.getEnforceProtocol());
             initLists(opts);
             _maxConnsPerMinute = opts.getMaxConnsPerMinute();
             _maxConnsPerHour = opts.getMaxConnsPerHour();
@@ -317,6 +326,7 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
         // overrides default in super()
         setConnectTimeout(getInt(opts, PROP_CONNECT_TIMEOUT, Connection.DISCONNECT_TIMEOUT));
         setAnswerPings(getBool(opts, PROP_ANSWER_PINGS, DEFAULT_ANSWER_PINGS));
+        setEnforceProtocol(getBool(opts, PROP_ENFORCE_PROTO, DEFAULT_ENFORCE_PROTO));
         initLists(opts);
         _maxConnsPerMinute = getInt(opts, PROP_MAX_CONNS_MIN, 0);
         _maxConnsPerHour = getInt(opts, PROP_MAX_CONNS_HOUR, 0);
@@ -371,6 +381,8 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
             setConnectTimeout(getInt(opts, PROP_CONNECT_TIMEOUT, Connection.DISCONNECT_TIMEOUT));
         if (opts.containsKey(PROP_ANSWER_PINGS))
             setAnswerPings(getBool(opts, PROP_ANSWER_PINGS, DEFAULT_ANSWER_PINGS));
+        if (opts.containsKey(PROP_ENFORCE_PROTO))
+            setEnforceProtocol(getBool(opts, PROP_ENFORCE_PROTO, DEFAULT_ENFORCE_PROTO));
         initLists(opts);
         if (opts.containsKey(PROP_MAX_CONNS_MIN))
             _maxConnsPerMinute = getInt(opts, PROP_MAX_CONNS_MIN, 0);
@@ -420,6 +432,19 @@ class ConnectionOptions extends I2PSocketOptionsImpl {
     public boolean getAnswerPings() { return _answerPings; }
     public void setAnswerPings(boolean yes) { _answerPings = yes; }
     
+    /**
+     * Do we receive all traffic, or only traffic marked with I2PSession.PROTO_STREAMING (6) ?
+     * Default false.
+     * If PROTO is enforced, we cannot communicate with destinations earlier than version 0.7.1
+     * (released March 2009), which is when streaming started sending the PROTO_STREAMING indication.
+     * Set to true if you are running multiple protocols on a single Destination.
+     *
+     * @return if we do
+     * @since 0.9.1
+     */
+    public boolean getEnforceProtocol() { return _enforceProto; }
+    public void setEnforceProtocol(boolean yes) { _enforceProto = yes; }
+    
     /** 
      * How many messages will we send before waiting for an ACK?
      *
diff --git a/core/java/src/net/i2p/client/I2PSession.java b/core/java/src/net/i2p/client/I2PSession.java
index 06b094488c..0e7f6269c2 100644
--- a/core/java/src/net/i2p/client/I2PSession.java
+++ b/core/java/src/net/i2p/client/I2PSession.java
@@ -38,6 +38,9 @@ public interface I2PSession {
     /** Send a new message to the given destination, containing the specified
      * payload, returning true if the router feels confident that the message
      * was delivered.
+     *
+     * WARNING: It is recommended that you use a method that specifies the protocol and ports.
+     *
      * @param dest location to send the message
      * @param payload body of the message to be sent (unencrypted)
      * @return whether it was accepted by the router for delivery or not
@@ -149,6 +152,9 @@ public interface I2PSession {
     public void reportAbuse(int msgId, int severity) throws I2PSessionException;
 
     /** Instruct the I2PSession where it should send event notifications
+     *
+     *  WARNING: It is recommended that you use a method that specifies the protocol and ports.
+     *
      * @param lsnr listener to retrieve events
      */
     public void setSessionListener(I2PSessionListener lsnr);
-- 
GitLab