From ebc3e05f23c9cb3a990bd7dcf498bc20d25ea618 Mon Sep 17 00:00:00 2001
From: human <human>
Date: Mon, 19 Apr 2004 21:52:52 +0000
Subject: [PATCH] * Made the SAM bridge aware of the new exceptions thrown by  
 I2PSocketManager.connect(), with better error reporting to SAM clients; *
 made SAMStreamSession destroy the associated I2PSocketManager when being  
 closed. (human)

---
 .../src/net/i2p/sam/SAMStreamSession.java     | 13 ++++++++++-
 .../java/src/net/i2p/sam/SAMv1Handler.java    | 22 +++++++++++++++----
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java b/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
index 1be21a9df2..ff9a1bc004 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
@@ -14,6 +14,8 @@ import java.io.InputStream;
 import java.io.IOException;
 import java.util.HashMap;
 import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.NoRouteToHostException;
 import java.util.Iterator;
 import java.util.Properties;
 import java.util.Set;
@@ -143,8 +145,16 @@ public class SAMStreamSession {
      * @param id Unique id for the connection
      * @param dest Base64-encoded Destination to connect to
      * @param props Options to be used for connection
+     *
+     * @throws DataFormatException if the destination is not valid
+     * @throws SAMInvalidDirectionException if trying to connect through a
+     *                                      receive-only session
+     * @throws ConnectException if the destination refuses connections
+     * @throws NoRouteToHostException if the destination can't be reached
+     * @throws InterruptedException if the connection timeouts
+     * @throws I2PException if there's another I2P-related error
      */
-    public boolean connect(int id, String dest, Properties props) throws I2PException, DataFormatException, SAMInvalidDirectionException {
+    public boolean connect(int id, String dest, Properties props) throws I2PException, ConnectException, NoRouteToHostException, DataFormatException, InterruptedException, SAMInvalidDirectionException {
         if (!canCreate) {
             _log.debug("Trying to create an outgoing connection using a receive-only session");
             throw new SAMInvalidDirectionException("Trying to create connections through a receive-only session");
@@ -199,6 +209,7 @@ public class SAMStreamSession {
         }
         removeAllSocketHandlers();
         recv.stopStreamReceiving();
+        socketMgr.destroySocketManager();
     }
 
     /**
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java b/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java
index cc45e7b667..b020ed86df 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java
@@ -17,6 +17,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.Socket;
+import java.net.ConnectException;
+import java.net.NoRouteToHostException;
 import java.util.Enumeration;
 import java.util.Properties;
 import java.util.StringTokenizer;
@@ -569,14 +571,26 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
                 _log.debug("Invalid destination in STREAM CONNECT message");
                 return writeString("STREAM STATUS RESULT=INVALID_KEY ID="
                                    + id + "\n");
-            } catch (I2PException e) {
-                _log.debug("STREAM CONNECT failed: " + e.getMessage());
-                return writeString("STREAM STATUS RESULT=I2P_ERROR ID="
-                                   + id + "\n");
             } catch (SAMInvalidDirectionException e) {
                 _log.debug("STREAM CONNECT failed: " + e.getMessage());
                 return writeString("STREAM STATUS RESULT=INVALID_DIRECTION ID="
                                    + id + "\n");
+            } catch (ConnectException e) {
+                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                return writeString("STREAM STATUS RESULT=CONNECTION_REFUSED ID="
+                                   + id + "\n");
+            } catch (NoRouteToHostException e) {
+                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                return writeString("STREAM STATUS RESULT=CANT_REACH_PEER ID="
+                                   + id + "\n");
+            } catch (InterruptedException e) {
+                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                return writeString("STREAM STATUS RESULT=TIMEOUT ID="
+                                   + id + "\n");
+            } catch (I2PException e) {
+                _log.debug("STREAM CONNECT failed: " + e.getMessage());
+                return writeString("STREAM STATUS RESULT=I2P_ERROR ID="
+                                   + id + "\n");
             }
         } else if (opcode.equals("CLOSE")) {
             if (props == null) {
-- 
GitLab