SAM: Rename 3.3 control session

This commit is contained in:
zzz
2020-07-09 17:05:06 +00:00
parent 31d7d7d9bb
commit 075ac7ab43
5 changed files with 71 additions and 71 deletions

View File

@@ -36,7 +36,7 @@ import net.i2p.util.Log;
*
* @since 0.9.25
*/
class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, SAMRawReceiver,
class PrimarySession extends SAMv3StreamSession implements SAMDatagramReceiver, SAMRawReceiver,
SAMMessageSess, I2PSessionMuxedListener {
private final SAMv3Handler handler;
private final SAMv3DatagramServer dgs;
@@ -55,13 +55,13 @@ class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, S
* @throws IOException
* @throws DataFormatException
*/
public MasterSession(String nick, SAMv3DatagramServer dgServer, SAMv3Handler handler, Properties props)
public PrimarySession(String nick, SAMv3DatagramServer dgServer, SAMv3Handler handler, Properties props)
throws IOException, DataFormatException, SAMException {
super(nick);
for (int i = 0; i < INVALID_OPTS.length; i++) {
String p = INVALID_OPTS[i];
if (props.containsKey(p))
throw new SAMException("MASTER session options may not contain " + p);
throw new SAMException("PRIMARY session options may not contain " + p);
}
dgs = dgServer;
sessions = new ConcurrentHashMap<String, SAMMessageSess>(4);
@@ -78,7 +78,7 @@ class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, S
*/
@Override
public void start() {
Thread t = new I2PAppThread(streamAcceptor, "SAMMasterAcceptor");
Thread t = new I2PAppThread(streamAcceptor, "SAMPrimaryAcceptor");
t.start();
}
@@ -208,7 +208,7 @@ class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, S
*/
public void receiveDatagramBytes(Destination sender, byte[] data, int proto,
int fromPort, int toPort) throws IOException {
throw new IOException("master session");
throw new IOException("primary session");
}
/**
@@ -220,7 +220,7 @@ class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, S
* @throws IOException always
*/
public void receiveRawBytes(byte[] data, int proto, int fromPort, int toPort) throws IOException {
throw new IOException("master session");
throw new IOException("primary session");
}
/**
@@ -235,19 +235,19 @@ class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, S
/** @throws I2PException always */
@Override
public void connect(SAMv3Handler handler, String dest, Properties props) throws I2PException {
throw new I2PException("master session");
throw new I2PException("primary session");
}
/** @throws SAMException always */
@Override
public void accept(SAMv3Handler handler, boolean verbose) throws SAMException {
throw new SAMException("master session");
throw new SAMException("primary session");
}
/** @throws SAMException always */
@Override
public void startForwardingIncoming(Properties props, boolean sendPorts) throws SAMException {
throw new SAMException("master session");
throw new SAMException("primary session");
}
/** does nothing */
@@ -268,7 +268,7 @@ class MasterSession extends SAMv3StreamSession implements SAMDatagramReceiver, S
}
/**
* Close the master session
* Close the primary session
* Overridden to stop the acceptor.
*/
@Override

View File

@@ -379,7 +379,7 @@ class SAMStreamSession implements SAMMessageSess {
* @since 0.9.25 moved from subclass SAMv3StreamSession to implement SAMMessageSess
*/
public boolean sendBytes(String s, byte[] b, int pr, int fp, int tp) throws I2PSessionException {
throw new I2PSessionException("Unsupported in STREAM or MASTER session");
throw new I2PSessionException("Unsupported in STREAM or PRIMARY session");
}
/**
@@ -391,7 +391,7 @@ class SAMStreamSession implements SAMMessageSess {
boolean sendLeaseSet, int sendTags,
int tagThreshold, int expiration)
throws I2PSessionException {
throw new I2PSessionException("Unsupported in STREAM or MASTER session");
throw new I2PSessionException("Unsupported in STREAM or PRIMARY session");
}
/**

View File

@@ -136,7 +136,7 @@ class SAMv3Handler extends SAMv1Handler
}
/**
* For subsessions created by MasterSession
* For subsessions created by PrimarySession
* @since 0.9.25
*/
void setSession(SAMv3RawSession sess) {
@@ -144,7 +144,7 @@ class SAMv3Handler extends SAMv1Handler
}
/**
* For subsessions created by MasterSession
* For subsessions created by PrimarySession
* @since 0.9.25
*/
void setSession(SAMv3DatagramSession sess) {
@@ -152,7 +152,7 @@ class SAMv3Handler extends SAMv1Handler
}
/**
* For subsessions created by MasterSession
* For subsessions created by PrimarySession
* @since 0.9.25
*/
void setSession(SAMv3StreamSession sess) {
@@ -463,7 +463,7 @@ class SAMv3Handler extends SAMv1Handler
allProps.putAll(i2cpProps);
allProps.putAll(props);
if (style.equals("MASTER")) {
if (style.equals("PRIMARY") || style.equals("MASTER")) {
// We must put these here, as SessionRecord.getProps() makes a copy,
// and the socket manager is instantiated in the
// SAMStreamSession constructor.
@@ -501,9 +501,9 @@ class SAMv3Handler extends SAMv1Handler
streamSession = v3;
this.session = v3;
v3.start();
} else if (style.equals("MASTER")) {
} else if (style.equals("PRIMARY") || style.equals("MASTER")) {
SAMv3DatagramServer dgs = bridge.getV3DatagramServer(props);
MasterSession v3 = new MasterSession(nick, dgs, this, allProps);
PrimarySession v3 = new PrimarySession(nick, dgs, this, allProps);
streamSession = v3;
datagramSession = v3;
rawSession = v3;
@@ -521,8 +521,8 @@ class SAMv3Handler extends SAMv1Handler
// prevent trouble in finally block
ok = true;
if (streamSession == null || datagramSession == null || rawSession == null)
return writeString(SESSION_ERROR, "Not a MASTER session");
MasterSession msess = (MasterSession) session;
return writeString(SESSION_ERROR, "Not a PRIMARY session");
PrimarySession msess = (PrimarySession) session;
String msg;
if (opcode.equals("ADD")) {
msg = msess.add(nick, style, props);

View File

@@ -55,19 +55,19 @@ public class SAMStreamSend {
private static I2PSSLSocketFactory _sslSocketFactory;
private static final int STREAM=0, DG=1, V1DG=2, RAW=3, V1RAW=4;
private static final int MASTER=8;
private static final int PRIMARY=8;
private static final String USAGE = "Usage: SAMStreamSend [-s] [-x] [-m mode] [-v version] [-b samHost] [-p samPort]\n" +
" [-o opt=val] [-u user] [-w password] peerDestFile dataDir\n" +
" modes: stream: 0; datagram: 1; v1datagram: 2; raw: 3; v1raw: 4\n" +
" default is stream\n" +
" -s: use SSL\n" +
" -x: use master session (forces -v 3.3)\n" +
" -x: use primary session (forces -v 3.3)\n" +
" multiple -o session options are allowed";
public static void main(String args[]) {
Getopt g = new Getopt("SAM", args, "sxhb:m:o:p:u:v:w:");
boolean isSSL = false;
boolean isMaster = false;
boolean isPrimary = false;
int mode = STREAM;
String version = "3.3";
String host = "127.0.0.1";
@@ -83,7 +83,7 @@ public class SAMStreamSend {
break;
case 'x':
isMaster = true;
isPrimary = true;
break;
case 'm':
@@ -132,8 +132,8 @@ public class SAMStreamSend {
System.err.println(USAGE);
return;
}
if (isMaster) {
mode += MASTER;
if (isPrimary) {
mode += PRIMARY;
version = "3.3";
}
if ((user == null && password != null) ||
@@ -175,8 +175,8 @@ public class SAMStreamSend {
_log.debug("Reader created");
OutputStream out = sock.getOutputStream();
String ourDest = handshake(out, version, true, eventHandler, mode, user, password, sessionOpts);
if (mode >= MASTER)
mode -= MASTER;
if (mode >= PRIMARY)
mode -= PRIMARY;
if (ourDest == null)
throw new IOException("handshake failed");
if (_log.shouldLog(Log.DEBUG))
@@ -246,10 +246,10 @@ public class SAMStreamSend {
}
/**
* @param isMaster is this the control socket
* @param isPrimary is this the control socket
* @return our b64 dest or null
*/
private String handshake(OutputStream samOut, String version, boolean isMaster,
private String handshake(OutputStream samOut, String version, boolean isPrimary,
SAMEventHandler eventHandler, int mode, String user, String password,
String opts) {
synchronized (samOut) {
@@ -267,7 +267,7 @@ public class SAMStreamSend {
_log.debug("Hello reply found: " + hisVersion);
if (hisVersion == null)
throw new IOException("Hello failed");
if (!isMaster)
if (!isPrimary)
return "OK";
_isV3 = VersionComparator.comp(hisVersion, "3") >= 0;
if (_isV3) {
@@ -279,14 +279,14 @@ public class SAMStreamSend {
_v3ID = "xx€€xx" + _v3ID;
_conOptions = "ID=" + _v3ID;
}
boolean masterMode; // are we using v3.3 master session
boolean primaryMode; // are we using v3.3 primary session
String command;
if (mode >= MASTER) {
masterMode = true;
if (mode >= PRIMARY) {
primaryMode = true;
command = "ADD";
mode -= MASTER;
mode -= PRIMARY;
} else {
masterMode = false;
primaryMode = false;
command = "CREATE DESTINATION=TRANSIENT";
}
String style;
@@ -297,19 +297,19 @@ public class SAMStreamSend {
else // RAW or V1RAW
style = "RAW";
if (masterMode) {
if (primaryMode) {
if (mode == V1DG || mode == V1RAW)
throw new IllegalArgumentException("v1 dg/raw incompatible with master session");
String req = "SESSION CREATE DESTINATION=TRANSIENT STYLE=MASTER ID=masterSend " + opts + '\n';
throw new IllegalArgumentException("v1 dg/raw incompatible with primary session");
String req = "SESSION CREATE DESTINATION=TRANSIENT STYLE=PRIMARY ID=primarySend " + opts + '\n';
samOut.write(req.getBytes("UTF-8"));
samOut.flush();
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION CREATE STYLE=MASTER sent");
_log.debug("SESSION CREATE STYLE=PRIMARY sent");
boolean ok = eventHandler.waitForSessionCreateReply();
if (!ok)
throw new IOException("SESSION CREATE STYLE=MASTER failed");
throw new IOException("SESSION CREATE STYLE=PRIMARY failed");
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION CREATE STYLE=MASTER reply found: " + ok);
_log.debug("SESSION CREATE STYLE=PRIMARY reply found: " + ok);
// PORT required even if we aren't listening for this test
if (mode != STREAM)
opts += " PORT=9999";
@@ -320,7 +320,7 @@ public class SAMStreamSend {
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION " + command + " sent");
boolean ok;
if (masterMode)
if (primaryMode)
ok = eventHandler.waitForSessionAddReply();
else
ok = eventHandler.waitForSessionCreateReply();
@@ -329,7 +329,7 @@ public class SAMStreamSend {
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION " + command + " reply found: " + ok);
if (masterMode) {
if (primaryMode) {
// do a bunch more
req = "SESSION ADD STYLE=STREAM FROM_PORT=99 ID=stream99\n";
samOut.write(req.getBytes("UTF-8"));
@@ -559,7 +559,7 @@ public class SAMStreamSend {
closed();
// stop the reader, since we're only doing this once for testing
// you wouldn't do this in a real application
// closing the master socket too fast will kill the data socket flushing through
// closing the primary socket too fast will kill the data socket flushing through
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {}

View File

@@ -58,7 +58,7 @@ public class SAMStreamSink {
private static I2PSSLSocketFactory _sslSocketFactory;
private static final int STREAM=0, DG=1, V1DG=2, RAW=3, V1RAW=4, RAWHDR = 5, FORWARD = 6, FORWARDSSL=7;
private static final int MASTER=8;
private static final int PRIMARY=8;
private static final String USAGE = "Usage: SAMStreamSink [-s] [-m mode] [-v version] [-b samHost] [-p samPort]\n" +
" [-o opt=val] [-u user] [-w password] myDestFile sinkDir\n" +
" modes: stream: 0; datagram: 1; v1datagram: 2;\n" +
@@ -66,7 +66,7 @@ public class SAMStreamSink {
" stream-forward: 6; stream-forward-ssl: 7\n" +
" default is stream\n" +
" -s: use SSL to connect to bridge\n" +
" -x: use master session (forces -v 3.3)\n" +
" -x: use primary session (forces -v 3.3)\n" +
" multiple -o session options are allowed";
private static final int V3FORWARDPORT=9998;
private static final int V3DGPORT=9999;
@@ -74,7 +74,7 @@ public class SAMStreamSink {
public static void main(String args[]) {
Getopt g = new Getopt("SAM", args, "sxhb:m:p:u:v:w:");
boolean isSSL = false;
boolean isMaster = false;
boolean isPrimary = false;
int mode = STREAM;
String version = "3.3";
String host = "127.0.0.1";
@@ -90,7 +90,7 @@ public class SAMStreamSink {
break;
case 'x':
isMaster = true;
isPrimary = true;
break;
case 'm':
@@ -139,8 +139,8 @@ public class SAMStreamSink {
System.err.println(USAGE);
return;
}
if (isMaster) {
mode += MASTER;
if (isPrimary) {
mode += PRIMARY;
version = "3.3";
}
if ((user == null && password != null) ||
@@ -181,8 +181,8 @@ public class SAMStreamSink {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Reader created");
String ourDest = handshake(out, version, true, eventHandler, mode, user, password, sessionOpts);
if (mode >= MASTER)
mode -= MASTER;
if (mode >= PRIMARY)
mode -= PRIMARY;
if (ourDest == null)
throw new IOException("handshake failed");
if (_log.shouldLog(Log.DEBUG))
@@ -578,10 +578,10 @@ public class SAMStreamSink {
}
/**
* @param isMaster is this the control socket
* @param isPrimary is this the control socket
* @return our b64 dest or null
*/
private String handshake(OutputStream samOut, String version, boolean isMaster,
private String handshake(OutputStream samOut, String version, boolean isPrimary,
SAMEventHandler eventHandler, int mode, String user, String password,
String sopts) {
synchronized (samOut) {
@@ -598,7 +598,7 @@ public class SAMStreamSink {
_log.debug("Hello reply found: " + hisVersion);
if (hisVersion == null)
throw new IOException("Hello failed");
if (!isMaster) {
if (!isPrimary) {
// only for v3
//String req = "STREAM ACCEPT SILENT=true ID=" + _v3ID + "\n";
// TO_PORT not supported until 3.2 but 3.0-3.1 will ignore
@@ -650,7 +650,7 @@ public class SAMStreamSink {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Requesting new transient destination");
}
if (isMaster) {
if (isPrimary) {
byte[] id = new byte[5];
_context.random().nextBytes(id);
_v3ID = Base32.encode(id);
@@ -661,14 +661,14 @@ public class SAMStreamSink {
// and give it to the SAM server
dest = _destFile;
}
boolean masterMode; // are we using v3.3 master session
boolean primaryMode; // are we using v3.3 primary session
String command;
if (mode >= MASTER) {
masterMode = true;
if (mode >= PRIMARY) {
primaryMode = true;
command = "ADD";
mode -= MASTER;
mode -= PRIMARY;
} else {
masterMode = false;
primaryMode = false;
command = "CREATE DESTINATION=" + dest;
}
String style;
@@ -685,19 +685,19 @@ public class SAMStreamSink {
else
style = "RAW HEADER=true PORT=" + V3DGPORT;
if (masterMode) {
if (primaryMode) {
if (mode == V1DG || mode == V1RAW)
throw new IllegalArgumentException("v1 dg/raw incompatible with master session");
String req = "SESSION CREATE DESTINATION=" + dest + " STYLE=MASTER ID=masterSink " + sopts + '\n';
throw new IllegalArgumentException("v1 dg/raw incompatible with primary session");
String req = "SESSION CREATE DESTINATION=" + dest + " STYLE=PRIMARY ID=primarySink " + sopts + '\n';
samOut.write(req.getBytes("UTF-8"));
samOut.flush();
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION CREATE STYLE=MASTER sent");
_log.debug("SESSION CREATE STYLE=PRIMARY sent");
boolean ok = eventHandler.waitForSessionCreateReply();
if (!ok)
throw new IOException("SESSION CREATE STYLE=MASTER failed");
throw new IOException("SESSION CREATE STYLE=PRIMARY failed");
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION CREATE STYLE=MASTER reply found: " + ok);
_log.debug("SESSION CREATE STYLE=PRIMARY reply found: " + ok);
}
String req = "SESSION " + command + " STYLE=" + style + ' ' + _conOptions + ' ' + sopts + '\n';
@@ -707,7 +707,7 @@ public class SAMStreamSink {
_log.debug("SESSION " + command + " sent");
//if (mode == STREAM) {
boolean ok;
if (masterMode)
if (primaryMode)
ok = eventHandler.waitForSessionAddReply();
else
ok = eventHandler.waitForSessionCreateReply();
@@ -716,7 +716,7 @@ public class SAMStreamSink {
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION " + command + " reply found: " + ok);
//}
if (masterMode) {
if (primaryMode) {
// do a bunch more
req = "SESSION ADD STYLE=STREAM FROM_PORT=99 ID=stream99\n";
samOut.write(req.getBytes("UTF-8"));
@@ -736,7 +736,7 @@ public class SAMStreamSink {
samOut.write(req.getBytes("UTF-8"));
req = "SESSION REMOVE ID=notfound\n";
samOut.write(req.getBytes("UTF-8"));
req = "SESSION REMOVE ID=masterSink\n"; // shouldn't remove ourselves
req = "SESSION REMOVE ID=primarySink\n"; // shouldn't remove ourselves
samOut.write(req.getBytes("UTF-8"));
samOut.flush();
}