Skip to content

Commit

Permalink
SAM: Rename 3.3 control session
Browse files Browse the repository at this point in the history
  • Loading branch information
zzz committed Jul 9, 2020
1 parent 31d7d7d commit 075ac7a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @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;
Expand All @@ -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);
Expand All @@ -78,7 +78,7 @@ public MasterSession(String nick, SAMv3DatagramServer dgServer, SAMv3Handler han
*/
@Override
public void start() {
Thread t = new I2PAppThread(streamAcceptor, "SAMMasterAcceptor");
Thread t = new I2PAppThread(streamAcceptor, "SAMPrimaryAcceptor");
t.start();
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public synchronized String remove(String nick, Properties props) {
*/
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");
}

/**
Expand All @@ -220,7 +220,7 @@ public void stopDatagramReceiving() {}
* @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");
}

/**
Expand All @@ -235,19 +235,19 @@ public void stopRawReceiving() {}
/** @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 */
Expand All @@ -268,7 +268,7 @@ public int getListenPort() {
}

/**
* Close the master session
* Close the primary session
* Overridden to stop the acceptor.
*/
@Override
Expand Down
4 changes: 2 additions & 2 deletions apps/sam/java/src/net/i2p/sam/SAMStreamSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public boolean closeConnection(int id) {
* @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");
}

/**
Expand All @@ -391,7 +391,7 @@ public boolean sendBytes(String s, byte[] b, int pr, int fp, int tp,
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");
}

/**
Expand Down
16 changes: 8 additions & 8 deletions apps/sam/java/src/net/i2p/sam/SAMv3Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,23 @@ Session getSession() {
}

/**
* For subsessions created by MasterSession
* For subsessions created by PrimarySession
* @since 0.9.25
*/
void setSession(SAMv3RawSession sess) {
rawSession = sess; session = sess;
}

/**
* For subsessions created by MasterSession
* For subsessions created by PrimarySession
* @since 0.9.25
*/
void setSession(SAMv3DatagramSession sess) {
datagramSession = sess; session = sess;
}

/**
* For subsessions created by MasterSession
* For subsessions created by PrimarySession
* @since 0.9.25
*/
void setSession(SAMv3StreamSession sess) {
Expand Down Expand Up @@ -463,7 +463,7 @@ protected boolean execSessionMessage(String opcode, Properties props) {
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.
Expand Down Expand Up @@ -501,9 +501,9 @@ protected boolean execSessionMessage(String opcode, Properties props) {
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;
Expand All @@ -521,8 +521,8 @@ protected boolean execSessionMessage(String opcode, Properties props) {
// 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);
Expand Down
50 changes: 25 additions & 25 deletions apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -83,7 +83,7 @@ public static void main(String args[]) {
break;

case 'x':
isMaster = true;
isPrimary = true;
break;

case 'm':
Expand Down Expand Up @@ -132,8 +132,8 @@ public static void main(String args[]) {
System.err.println(USAGE);
return;
}
if (isMaster) {
mode += MASTER;
if (isPrimary) {
mode += PRIMARY;
version = "3.3";
}
if ((user == null && password != null) ||
Expand Down Expand Up @@ -175,8 +175,8 @@ public void startup(String version, boolean isSSL, int mode, String user, String
_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))
Expand Down Expand Up @@ -246,10 +246,10 @@ private Socket connect(boolean isSSL) throws IOException {
}

/**
* @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) {
Expand All @@ -267,7 +267,7 @@ private String handshake(OutputStream samOut, String version, boolean isMaster,
_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) {
Expand All @@ -279,14 +279,14 @@ private String handshake(OutputStream samOut, String version, boolean isMaster,
_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;
Expand All @@ -297,19 +297,19 @@ else if (mode == DG || mode == V1DG)
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";
Expand All @@ -320,7 +320,7 @@ else if (mode == DG || mode == V1DG)
if (_log.shouldLog(Log.DEBUG))
_log.debug("SESSION " + command + " sent");
boolean ok;
if (masterMode)
if (primaryMode)
ok = eventHandler.waitForSessionAddReply();
else
ok = eventHandler.waitForSessionCreateReply();
Expand All @@ -329,7 +329,7 @@ else if (mode == DG || mode == V1DG)
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"));
Expand Down Expand Up @@ -559,7 +559,7 @@ public void run() {
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) {}
Expand Down
Loading

1 comment on commit 075ac7a

@HaskuldrKrionskij
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you fight against censorship xD

Please sign in to comment.