propagate from branch 'i2p.i2p.zzz.sam' (head b328f0edb961263d7606ea964ecb3f7c319ca1cf)

to branch 'i2p.i2p' (head 7b4c0525be182722ef2cc7b564691f27d997da3b)
This commit is contained in:
zzz
2015-11-27 20:58:18 +00:00
30 changed files with 2753 additions and 689 deletions

View File

@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicLong;
import net.i2p.I2PException;
import net.i2p.client.I2PClient;
import net.i2p.client.I2PSession;
import net.i2p.client.I2PSessionException;
import net.i2p.crypto.SigType;
import net.i2p.data.Base64;
@@ -186,7 +187,9 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
} catch (IOException e) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Caught IOException for message [" + msg + "]", e);
} catch (Exception e) {
} catch (SAMException e) {
_log.error("Unexpected exception for message [" + msg + "]", e);
} catch (RuntimeException e) {
_log.error("Unexpected exception for message [" + msg + "]", e);
} finally {
if (_log.shouldLog(Log.DEBUG))
@@ -438,25 +441,44 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
}
int size;
{
String strsize = props.getProperty("SIZE");
if (strsize == null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Size not specified in DATAGRAM SEND message");
return false;
}
String strsize = props.getProperty("SIZE");
if (strsize == null) {
if (_log.shouldLog(Log.WARN))
_log.warn("Size not specified in DATAGRAM SEND message");
return false;
}
try {
size = Integer.parseInt(strsize);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid DATAGRAM SEND size specified: " + strsize);
return false;
}
if (!checkDatagramSize(size)) {
if (_log.shouldLog(Log.WARN))
_log.warn("Specified size (" + size
+ ") is out of protocol limits");
return false;
}
int proto = I2PSession.PROTO_DATAGRAM;
int fromPort = I2PSession.PORT_UNSPECIFIED;
int toPort = I2PSession.PORT_UNSPECIFIED;
String s = props.getProperty("FROM_PORT");
if (s != null) {
try {
size = Integer.parseInt(strsize);
fromPort = Integer.parseInt(s);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Invalid DATAGRAM SEND size specified: " + strsize);
return false;
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid DATAGRAM SEND port specified: " + s);
}
if (!checkDatagramSize(size)) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Specified size (" + size
+ ") is out of protocol limits");
return false;
}
s = props.getProperty("TO_PORT");
if (s != null) {
try {
toPort = Integer.parseInt(s);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid RAW SEND port specified: " + s);
}
}
@@ -466,7 +488,7 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
in.readFully(data);
if (!getDatagramSession().sendBytes(dest, data)) {
if (!getDatagramSession().sendBytes(dest, data, proto, fromPort, toPort)) {
_log.error("DATAGRAM SEND failed");
// a message send failure is no reason to drop the SAM session
// for raw and repliable datagrams, just carry on our merry way
@@ -523,25 +545,53 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
}
int size;
{
String strsize = props.getProperty("SIZE");
if (strsize == null) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Size not specified in RAW SEND message");
return false;
}
String strsize = props.getProperty("SIZE");
if (strsize == null) {
if (_log.shouldLog(Log.WARN))
_log.warn("Size not specified in RAW SEND message");
return false;
}
try {
size = Integer.parseInt(strsize);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid RAW SEND size specified: " + strsize);
return false;
}
if (!checkSize(size)) {
if (_log.shouldLog(Log.WARN))
_log.warn("Specified size (" + size
+ ") is out of protocol limits");
return false;
}
int proto = I2PSession.PROTO_DATAGRAM_RAW;
int fromPort = I2PSession.PORT_UNSPECIFIED;
int toPort = I2PSession.PORT_UNSPECIFIED;
String s = props.getProperty("PROTOCOL");
if (s != null) {
try {
size = Integer.parseInt(strsize);
proto = Integer.parseInt(s);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Invalid RAW SEND size specified: " + strsize);
return false;
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid RAW SEND protocol specified: " + s);
}
if (!checkSize(size)) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Specified size (" + size
+ ") is out of protocol limits");
return false;
}
s = props.getProperty("FROM_PORT");
if (s != null) {
try {
fromPort = Integer.parseInt(s);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid RAW SEND port specified: " + s);
}
}
s = props.getProperty("TO_PORT");
if (s != null) {
try {
toPort = Integer.parseInt(s);
} catch (NumberFormatException e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Invalid RAW SEND port specified: " + s);
}
}
@@ -551,7 +601,7 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
in.readFully(data);
if (!getRawSession().sendBytes(dest, data)) {
if (!getRawSession().sendBytes(dest, data, proto, fromPort, toPort)) {
_log.error("RAW SEND failed");
// a message send failure is no reason to drop the SAM session
// for raw and repliable datagrams, just carry on our merry way
@@ -796,16 +846,21 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
}
// SAMRawReceiver implementation
public void receiveRawBytes(byte data[]) throws IOException {
public void receiveRawBytes(byte data[], int proto, int fromPort, int toPort) throws IOException {
if (getRawSession() == null) {
_log.error("BUG! Received raw bytes, but session is null!");
return;
}
ByteArrayOutputStream msg = new ByteArrayOutputStream();
ByteArrayOutputStream msg = new ByteArrayOutputStream(64 + data.length);
String msgText = "RAW RECEIVED SIZE=" + data.length + "\n";
String msgText = "RAW RECEIVED SIZE=" + data.length;
msg.write(DataHelper.getASCII(msgText));
if ((verMajor == 3 && verMinor >= 2) || verMajor > 3) {
msgText = " PROTOCOL=" + proto + " FROM_PORT=" + fromPort + " TO_PORT=" + toPort;
msg.write(DataHelper.getASCII(msgText));
}
msg.write((byte) '\n');
msg.write(data);
if (_log.shouldLog(Log.DEBUG))
@@ -832,17 +887,23 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
}
// SAMDatagramReceiver implementation
public void receiveDatagramBytes(Destination sender, byte data[]) throws IOException {
public void receiveDatagramBytes(Destination sender, byte data[], int proto,
int fromPort, int toPort) throws IOException {
if (getDatagramSession() == null) {
_log.error("BUG! Received datagram bytes, but session is null!");
return;
}
ByteArrayOutputStream msg = new ByteArrayOutputStream();
ByteArrayOutputStream msg = new ByteArrayOutputStream(100 + data.length);
String msgText = "DATAGRAM RECEIVED DESTINATION=" + sender.toBase64()
+ " SIZE=" + data.length + "\n";
+ " SIZE=" + data.length;
msg.write(DataHelper.getASCII(msgText));
if ((verMajor == 3 && verMinor >= 2) || verMajor > 3) {
msgText = " FROM_PORT=" + fromPort + " TO_PORT=" + toPort;
msg.write(DataHelper.getASCII(msgText));
}
msg.write((byte) '\n');
if (_log.shouldLog(Log.DEBUG))
_log.debug("sending to client: " + msgText);