java.lang.Object
net.i2p.sam.SAMHandler
net.i2p.sam.SAMv1Handler
net.i2p.sam.SAMv2Handler
- All Implemented Interfaces:
Runnable, Handler, SAMDatagramReceiver, SAMRawReceiver, SAMStreamReceiver
class SAMv2Handlerextends SAMv1Handler implements SAMRawReceiver, SAMDatagramReceiver, SAMStreamReceiver
SAM version 2 handler.
Field Summary
Fields inherited from class SAMv1Handler
_id, datagramSession, rawSession, SESSION_ERROR, streamSessionModifier and TypeFieldDescriptionprotected final longIdentifier of this connection, unique among a router's SAM handlers.protected SAMMessageSessThe session used for datagram style messages.protected SAMMessageSessThe SAM raw and datagram session, held after a successful HELLO.protected static final StringReply sent to a SESSION CREATE whose stream could not be established.protected SAMStreamSessionThe session used for STREAM CONNECT connections.Fields inherited from class SAMHandler
_log, bridge, i2cpProps, socket, stopHandler, stopLock, thread, verMajor, verMinorModifier and TypeFieldDescriptionprotected final LogLog scoped to this handler's class.protected final SAMBridgeRouter bridge this handler routes sessions over.protected final PropertiesI2CP options configuring the I2CP connection (port, host, numHops, etc).protected final SocketChannelClient channel this handler reads commands from.protected booleanWhether shutdown has been requested for this handler.protected final ObjectMonitor guarding the stop flag.protected I2PAppThreadThread running the SAM protocol loop for this connection.final intMajor version this handler was negotiated at.final intMinor version this handler was negotiated at.Constructor Summary
ConstructorsConstructorDescriptionSAMv2Handler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) Create a new SAM version 2 handler.SAMv2Handler(SocketChannel s, int verMajor, int verMinor, SAMBridge parent) Create a new SAM version 2 handler with default I2CP properties.Method Summary
Modifier and TypeMethodDescriptionprotected booleanexecStreamMessage(String opcode, Properties props) Parse and execute a STREAM message.private booleanexecStreamReceive(Properties props) Execute STREAM RECEIVE message.(package private) SAMStreamSessionnewSAMv2StreamSession(String destKeystream, String direction, Properties props) New SAMv2 stream session.booleanVerify the SAM protocol version is supported by this handler.Methods inherited from class SAMv1Handler
createMessageString, execDatagramMessage, execDestMessage, execNamingMessage, execRawMessage, execSessionMessage, execStreamClose, execStreamConnect, execStreamSend, getDatagramSession, getRawSession, getStreamSession, handle, notifyStreamDisconnection, notifyStreamIncomingConnection, notifyStreamOutgoingConnection, notifyStreamSendBufferFree, receiveDatagramBytes, receiveRawBytes, receiveStreamBytes, stopDatagramReceiving, stopRawReceiving, stopStreamReceiving, streamSendAnswer, writeStringModifier and TypeMethodDescriptionprotected static StringCreate a string to be appended to a status.protected booleanexecDatagramMessage(String opcode, Properties props) Parse and execute a DATAGRAM message.protected booleanexecDestMessage(String opcode, Properties props) Parse and execute a DEST message.protected booleanexecNamingMessage(String opcode, Properties props) Parse and execute a NAMING message.protected booleanexecRawMessage(String opcode, Properties props) Parse and execute a RAW message.protected booleanexecSessionMessage(String opcode, Properties props) Parse and execute a SESSION message.protected booleanexecStreamClose(Properties props) Parse and execute a STREAM CLOSE message.protected booleanexecStreamConnect(Properties props) Parse and execute a STREAM CONNECT message.protected booleanexecStreamSend(Properties props) Parse and execute a STREAM SEND message.protected final SAMMessageSessReturn the datagram session.protected final SAMMessageSessReturn the raw session.protected final SAMStreamSessionReturn the stream session.voidhandle()Handle a SAM v1 client connection by reading and dispatching commands in a loop until the connection is closed or an error occurs.voidnotifyStreamDisconnection(int id, String result, String msg) Notify the SAM client that a stream connection has been disconnected.voidnotifyStreamIncomingConnection(int id, Destination d) Notify the SAM client of an incoming stream connection.voidnotifyStreamOutgoingConnection(int id, String result, String msg) Report the result of an outgoing stream connection.voidnotifyStreamSendBufferFree(int id) Notify the SAM client that the stream send buffer is free for more data.voidreceiveDatagramBytes(Destination sender, byte[] data, int proto, int fromPort, int toPort) Receive datagram bytes from the network and forward them to the SAM client.voidreceiveRawBytes(byte[] data, int proto, int fromPort, int toPort) Receive raw bytes from the network and forward them to the SAM client.voidreceiveStreamBytes(int id, ByteBuffer data) Receive stream bytes from the network and forward them to the SAM client.voidStop receiving datagram bytes and close the client socket.voidStop receiving raw bytes and close the client socket.voidStop receiving stream bytes and close the client socket.voidstreamSendAnswer(int id, String result, String bufferState) Notify the SAM client of the result of a stream SEND operation.protected booleanwriteString(String s, String msg) Write a string and message, escaping the message.Methods inherited from class SAMHandler
closeClientSocket, getAuthUser, getClientSocket, getRemoteAddrPort, getWriteLock, isLoopbackAddress, logSessionError, run, sameClient, sameClient, setAuthUser, shouldStop, startHandling, stopHandling, toString, writeBytes, writeString, writeStringModifier and TypeMethodDescriptionprotected final voidClose the socket connected to the SAM client.(package private) final StringGet the username this connection authenticated with, if any.protected final SocketChannelGet the channel of the socket connected to the SAM client.(package private) final StringGet the remote address and port of this connection, bracketing IPv6, for rejection and ban logging.protected ObjectIf you're crazy enough to write to the raw socket, grab the write lock withSAMHandler.getWriteLock(), synchronize against it, and write to the getOut().(package private) static booleanisLoopbackAddress(String host) Determine whether an address is a loopback address, in any of the forms the platform may report it.protected voidlogSessionError(String msg, Exception e) Log a session startup failure.final voidrun()Register with the bridge, callSAMHandler.handle(), unregister with the bridge.(package private) static booleansameClient(String myAuthUser, String otherAuthUser, String myHost, String otherHost) Decide whether two SAM connections belong to the same client.(package private) final booleansameClient(SAMHandler other) Determine whether two connections belong to the same client, which is what decides whether one of them may use a session nickname owned by the other.(package private) final voidsetAuthUser(String user) Record the username this connection authenticated with.protected final booleanShould the handler be stopped?voidStart handling the SAM connection, detaching a handling thread.voidStop the SAM handler, close the client socket, unregister with the bridge.final StringtoString()Get a string describing the handler.protected final voidwriteBytes(ByteBuffer data) Write a byte array on the handler's socket.protected final booleanwriteString(String str) Write a string to the handler's socket.static booleanwriteString(String str, SocketChannel out) Unsynchronized, use with caution.
Constructor Details
SAMv2Handler
public SAMv2Handler(SocketChannel s, int verMajor, int verMinor, SAMBridge parent) throws SAMException, IOException Create a new SAM version 2 handler with default I2CP properties. This constructor expects that the SAM HELLO message has been still answered (and stripped) from the socket input stream.- Parameters:
s- Socket attached to a SAM clientverMajor- SAM major version to manage (should be 2)verMinor- SAM minor version to manageparent- bridge holding the shared SAM server state this handler joins- Throws:
SAMException- if the version is not supportedIOException- if an I/O error occurs
SAMv2Handler
public SAMv2Handler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) throws SAMException, IOException Create a new SAM version 2 handler. This constructor expects that the SAM HELLO message has been still answered (and stripped) from the socket input stream.- Parameters:
s- Socket attached to a SAM clientverMajor- SAM major version to manage (should be 2)verMinor- SAM minor version to managei2cpProps- properties to configure the I2CP connection (host, port, etc)parent- bridge holding the shared SAM server state this handler joins- Throws:
SAMException- if the version is not supportedIOException- if an I/O error occurs
Method Details
verifVersion
public boolean verifVersion()Verify the SAM protocol version is supported by this handler.- Overrides:
verifVersionin classSAMv1Handler- Returns:
- true if the major version matches this handler's version
newSAMv2StreamSession
SAMStreamSession newSAMv2StreamSession(String destKeystream, String direction, Properties props) throws IOException, DataFormatException, SAMException New SAMv2 stream session.- Parameters:
destKeystream- the destination and private key stream of the peerdirection- the STREAM connection direction, "INBOUND" or "OUTBOUND"props- the parsed STREAM message properties, including the port and options- Returns:
- the newly created STREAM session
- Throws:
IOException- if the destination cannot be read from the keystreamDataFormatException- if the keystream is not in the expected key file formatSAMException- if a session with this nickname is already registered
execStreamMessage
Parse and execute a STREAM message.- Overrides:
execStreamMessagein classSAMv1Handler- Parameters:
opcode- the STREAM sub-commandprops- the parsed message properties- Returns:
- true if the handler should continue processing commands
execStreamReceive
Execute STREAM RECEIVE message.- Parameters:
props- the parameters parsed from the message- Returns:
- true if the receive was set up, false if the parameters were bad