From 04509f593a0a75ed1b6cb86848553901a122181b Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Tue, 11 Nov 2008 02:30:49 +0000
Subject: [PATCH]  * Streaming: Enforce a minimum MTU of 512

---
 .../net/i2p/client/streaming/ConnectionOptions.java |  3 ++-
 .../client/streaming/ConnectionPacketHandler.java   | 13 +++++++++----
 2 files changed, 11 insertions(+), 5 deletions(-)

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 757d9d2535..f397521d78 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionOptions.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionOptions.java
@@ -162,6 +162,7 @@ public class ConnectionOptions extends I2PSocketOptionsImpl {
      *
      */
     public static final int DEFAULT_MAX_MESSAGE_SIZE = 1730;
+    public static final int MIN_MESSAGE_SIZE = 512;
 
     public ConnectionOptions() {
         super();
@@ -389,7 +390,7 @@ public class ConnectionOptions extends I2PSocketOptionsImpl {
      * @return Maximum message size (MTU/MRU)
      */
     public int getMaxMessageSize() { return _maxMessageSize; }
-    public void setMaxMessageSize(int bytes) { _maxMessageSize = bytes; }
+    public void setMaxMessageSize(int bytes) { _maxMessageSize = Math.max(bytes, MIN_MESSAGE_SIZE); }
     
     /** 
      * how long we want to wait before any data is transferred on the
diff --git a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionPacketHandler.java b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionPacketHandler.java
index 28146ce727..2d41aad8b4 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionPacketHandler.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionPacketHandler.java
@@ -67,12 +67,17 @@ public class ConnectionPacketHandler {
         }
 
         if (packet.isFlagSet(Packet.FLAG_MAX_PACKET_SIZE_INCLUDED)) {
-            if (packet.getOptionalMaxSize() < con.getOptions().getMaxMessageSize()) {
+            int size = packet.getOptionalMaxSize();
+            if (size < ConnectionOptions.MIN_MESSAGE_SIZE) {
+                // log.error? connection reset?
+                size = ConnectionOptions.MIN_MESSAGE_SIZE;
+            }
+            if (size < con.getOptions().getMaxMessageSize()) {
                 if (_log.shouldLog(Log.INFO))
-                    _log.info("Reducing our max message size to " + packet.getOptionalMaxSize() 
+                    _log.info("Reducing our max message size to " + size 
                               + " from " + con.getOptions().getMaxMessageSize());
-                con.getOptions().setMaxMessageSize(packet.getOptionalMaxSize());
-                con.getOutputStream().setBufferSize(packet.getOptionalMaxSize());
+                con.getOptions().setMaxMessageSize(size);
+                con.getOutputStream().setBufferSize(size);
             }
         }
         
-- 
GitLab