From 0c738e2c6f25dad858a729b376a84b6788e681c8 Mon Sep 17 00:00:00 2001
From: mkvore-commit <mkvore-commit@mail.i2p>
Date: Thu, 23 Apr 2009 00:55:12 +0000
Subject: [PATCH] SAMv3: stream session stop listening when the forwarding
 session closes. Doc has to be changed to reflect this.

---
 .../src/net/i2p/sam/SAMv3DatagramSession.java |  5 ++--
 .../java/src/net/i2p/sam/SAMv3Handler.java    | 19 +++++++++++-
 .../java/src/net/i2p/sam/SAMv3RawSession.java |  4 +--
 .../src/net/i2p/sam/SAMv3StreamSession.java   | 30 +++++++++++++++++--
 4 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3DatagramSession.java b/apps/sam/java/src/net/i2p/sam/SAMv3DatagramSession.java
index dcca03c141..c933e77cc3 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3DatagramSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3DatagramSession.java
@@ -61,10 +61,9 @@ public class SAMv3DatagramSession extends SAMDatagramSession implements SAMv3Han
     		int port = Integer.parseInt(portStr);
     	
     		String host = props.getProperty("HOST");
-    		if ( host==null ) {
-    			_log.debug("no host specified. Take from the client socket");
-    		
+    		if ( host==null ) {    		
     			host = rec.getHandler().getClientIP();
+    			_log.debug("no host specified. Taken from the client socket : " + host+':'+port);
     		}
 
     	
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java b/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
index b629cc09a1..b6a03a9ebf 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
@@ -323,6 +323,8 @@ public class SAMv3Handler extends SAMv1Handler
 	
 	boolean stolenSocket = false ;
 	
+	boolean streamForwardingSocket = false ;
+	
 	public void stealSocket()
 	{
 		stolenSocket = true ;
@@ -414,6 +416,20 @@ public class SAMv3Handler extends SAMv1Handler
 					_log.error("Error closing socket: " + e.getMessage());
 				}
 			}
+			if (streamForwardingSocket) 
+			{
+				if (this.streamSession!=null) {
+					try {
+						this.streamSession.stopForwardingIncoming();
+					} catch (SAMException e) {
+						_log.error("Error while stopping forwarding connections: " + e.getMessage());
+					} catch (InterruptedIOException e) {
+						_log.error("Interrupted while stopping forwarding connections: " + e.getMessage());
+					}
+				}
+			}
+		
+
 
 			die();
 		}
@@ -692,9 +708,10 @@ public class SAMv3Handler extends SAMv1Handler
 	protected boolean execStreamForwardIncoming( Properties props ) {
 		try {
 			try {
+				streamForwardingSocket = true ;
 				streamSession.startForwardingIncoming(props);
 				notifyStreamResult( true, "OK", null );
-				return false ;
+				return true ;
 			} catch (SAMException e) {
 				_log.debug("Forwarding STREAM connections failed: " + e.getMessage());
 				notifyStreamResult ( true, "I2P_ERROR", "Forwarding failed : " + e.getMessage() );
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java b/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
index 1bf7d18a0b..085293565a 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
@@ -63,9 +63,9 @@ public class SAMv3RawSession extends SAMRawSession  implements SAMv3Handler.Sess
     	
     		String host = props.getProperty("HOST");
     		if ( host==null ) {
-    			_log.debug("no host specified. Take from the client socket");
-    		
     			host = rec.getHandler().getClientIP();
+
+    			_log.debug("no host specified. Taken from the client socket : " + host +':'+port);
     		}
 
     	
diff --git a/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java b/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java
index f02591bb6e..0c2922fb19 100644
--- a/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java
+++ b/apps/sam/java/src/net/i2p/sam/SAMv3StreamSession.java
@@ -230,9 +230,8 @@ public class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handle
 	    	
 	    	String host = props.getProperty("HOST");
 	    	if ( host==null ) {
-	    		_log.debug("no host specified. Take from the client socket");
-	    		
 	    		host = rec.getHandler().getClientIP();
+	    		_log.debug("no host specified. Taken from the client socket : " + host +':'+port);
 	    	}
 
 	    	
@@ -368,7 +367,32 @@ public class SAMv3StreamSession  extends SAMStreamSession implements SAMv3Handle
 	    	}
 	    }
 	    
-
+	    /**
+	     * 
+	     * @param props
+	     * @throws SAMException
+	     * @throws InterruptedIOException
+	     */
+	    public void stopForwardingIncoming() throws SAMException, InterruptedIOException
+	    {
+	    	SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
+	        
+	        if ( rec==null ) throw new InterruptedIOException() ;
+	        
+	    	I2PServerSocket server = null ;
+	    	synchronized( this.socketServerLock )
+	    	{
+	    		if (this.socketServer==null) {
+	    			_log.debug("no socket server is defined for this destination");
+	    			throw new SAMException("no socket server is defined for this destination");
+    			}
+	    		server = this.socketServer ;
+	    		this.socketServer = null ;
+	    	}
+	    	try {
+	    		server.close();
+	    	} catch ( I2PException e) {}
+	    }
 
 	    /**
 	     * Close the stream session
-- 
GitLab