2004-12-05 jrandom

* Default the I2CP listener to localhost only, unless overridden by
      i2cp.tcp.bindAllInterfaces=true (thanks dm!)
    * More SAM fixes for things recently broken (whee)
This commit is contained in:
jrandom
2004-12-06 00:54:07 +00:00
committed by zzz
parent 499eeb275b
commit 88bb176f3b
8 changed files with 70 additions and 37 deletions

View File

@ -119,6 +119,8 @@ public abstract class SAMHandler implements Runnable {
* @return True is the string was successfully written, false otherwise
*/
protected final boolean writeString(String str) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Sending the client: [" + str + "]");
try {
writeBytes(str.getBytes("ISO-8859-1"));
} catch (IOException e) {

View File

@ -17,6 +17,7 @@ import java.net.Socket;
import java.util.Properties;
import java.util.StringTokenizer;
import net.i2p.data.DataHelper;
import net.i2p.util.Log;
/**
@ -36,14 +37,11 @@ public class SAMHandlerFactory {
* @return A SAM protocol handler, or null if the client closed before the handshake
*/
public static SAMHandler createSAMHandler(Socket s, Properties i2cpProps) throws SAMException {
BufferedReader br;
String line;
StringTokenizer tok;
try {
br = new BufferedReader(new InputStreamReader(s.getInputStream(),
"ISO-8859-1"));
line = br.readLine();
line = DataHelper.readLine(s.getInputStream());
if (line == null) {
_log.debug("Connection closed by client");
return null;

View File

@ -201,12 +201,17 @@ public class SAMStreamSession {
*
* @return True if the data was sent, false otherwise
*/
public boolean sendBytes(int id, InputStream in, int size) {
Destination d = new Destination();
public boolean sendBytes(int id, InputStream in, int size) throws IOException {
SAMStreamSessionSocketHandler handler = getSocketHandler(id);
if (handler == null) {
_log.error("Trying to send bytes through inexistent handler " +id);
// even though it failed, we need to read those bytes!
for (int i = 0; i < size; i++) {
int c = in.read();
if (c == -1)
break;
}
return false;
}
@ -420,6 +425,8 @@ public class SAMStreamSession {
} catch (I2PException e) {
_log.debug("Caught I2PException", e);
}
close();
_log.debug("Shutting down SAM STREAM session server");
}
@ -463,27 +470,25 @@ public class SAMStreamSession {
*
* @return True if data has been sent without errors, false otherwise
*/
public boolean sendBytes(InputStream in, int size) { // byte[] data) {
public boolean sendBytes(InputStream in, int size) throws IOException {
if (_log.shouldLog(Log.DEBUG)) {
_log.debug("Handler " + id + ": sending " + size
+ " bytes");
}
ByteCache cache = ByteCache.getInstance(1024, 4);
ByteArray ba = cache.acquire();
int remaining = size;
try {
int remaining = size;
byte buf[] = ba.getData();
while (remaining > 0) {
int read = in.read(buf, 0, remaining > buf.length ? buf.length : remaining);
if (read == -1)
throw new IOException("Insufficient data from the SAM client (" + remaining + "/" + size + ")");
i2pSocketOS.write(buf, 0, read);
else if (read > 0)
i2pSocketOS.write(buf, 0, read);
remaining -= read;
}
//i2pSocketOS.flush();
} catch (IOException e) {
_log.error("Error sending data through I2P socket", e);
return false;
} finally {
cache.release(ba);
}

View File

@ -26,6 +26,7 @@ import net.i2p.I2PException;
import net.i2p.client.I2PSessionException;
import net.i2p.data.Base64;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
import net.i2p.util.Log;
@ -80,9 +81,10 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
}
public void handle() {
String msg, domain, opcode;
String msg = null;
String domain = null;
String opcode = null;
boolean canContinue = false;
ByteArrayOutputStream buf = new ByteArrayOutputStream(IN_BUFSIZE);
StringTokenizer tok;
Properties props;
@ -99,22 +101,15 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
break;
}
while ((b = in.read()) != -1) {
if (b == '\n') {
break;
}
buf.write(b);
}
if (b == -1) {
msg = DataHelper.readLine(in);
if (msg == null) {
_log.debug("Connection closed by client");
break;
}
msg = buf.toString("ISO-8859-1").trim();
if (_log.shouldLog(Log.DEBUG)) {
_log.debug("New message received: [" + msg + "]");
}
buf.reset();
tok = new StringTokenizer(msg, " ");
if (tok.countTokens() < 2) {
@ -154,14 +149,11 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
break;
}
}
} catch (UnsupportedEncodingException e) {
_log.error("Caught UnsupportedEncodingException ("
+ e.getMessage() + ")", e);
} catch (IOException e) {
_log.debug("Caught IOException ("
+ e.getMessage() + ")", e);
+ e.getMessage() + ") for message [" + msg + "]", e);
} catch (Exception e) {
_log.error("Unexpected exception", e);
_log.error("Unexpected exception for message [" + msg + "]", e);
} finally {
_log.debug("Stopping handler");
try {
@ -555,7 +547,7 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
try {
if (!streamSession.sendBytes(id, getClientSocketInputStream(), size)) { // data)) {
_log.error("STREAM SEND failed");
_log.error("STREAM SEND [" + size + "] failed");
boolean rv = writeString("STREAM CLOSED RESULT=CANT_REACH_PEER ID=" + id + " MESSAGE=\"Send of " + size + " bytes failed\"\n");
streamSession.closeConnection(id);
return rv;
@ -563,11 +555,11 @@ public class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatag
return true;
} catch (EOFException e) {
_log.debug("Too few bytes with RAW SEND message (expected: "
_log.debug("Too few bytes with STREAM SEND message (expected: "
+ size);
return false;
} catch (IOException e) {
_log.debug("Caught IOException while parsing RAW SEND message",
_log.debug("Caught IOException while parsing STREAM SEND message",
e);
return false;
}