java.lang.Object
net.i2p.sam.SAMHandler
- Direct Known Subclasses:
SAMv1Handler
Field Summary
Modifier and TypeFieldDescriptionprotected final LogLog scoped to this handler's class.private StringUsername this connection authenticated with during the HELLO handshake, or null when the bridge has authentication disabled or the connection was never authenticated.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.private final ObjectHeld while writing to the SAM socket.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
ConstructorsModifierConstructorDescriptionprotectedSAMHandler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) SAMHandler constructor (to be called by subclasses).Method Summary
Modifier 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.private StringGet the remote address of this connection.protected ObjectIf you're crazy enough to write to the raw socket, grab the write lock withgetWriteLock(), synchronize against it, and write to the getOut().protected abstract voidhandle()Actually handle the SAM protocol.(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, callhandle(), 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.private static voidwriteBytes(ByteBuffer data, SocketChannel out) Caller must synch.protected final booleanwriteString(String str) Write a string to the handler's socket.static booleanwriteString(String str, SocketChannel out) Unsynchronized, use with caution.
Field Details
_log
Log scoped to this handler's class.thread
Thread running the SAM protocol loop for this connection.bridge
Router bridge this handler routes sessions over.socketWLock
Held while writing to the SAM socket.socket
Client channel this handler reads commands from.verMajor
public final int verMajorMajor version this handler was negotiated at.verMinor
public final int verMinorMinor version this handler was negotiated at.i2cpProps
I2CP options configuring the I2CP connection (port, host, numHops, etc).stopLock
Monitor guarding the stop flag.stopHandler
protected boolean stopHandlerWhether shutdown has been requested for this handler.authUser
Username this connection authenticated with during the HELLO handshake, or null when the bridge has authentication disabled or the connection was never authenticated. Records created for sub-sessions copy the username of the control connection that created them.
Constructor Details
SAMHandler
protected SAMHandler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) SAMHandler constructor (to be called by subclasses).- Parameters:
s- Socket attached to a SAM clientverMajor- SAM major version to manageverMinor- SAM minor version to managei2cpProps- properties to configure the I2CP connection (host, port, etc)parent- owning SAM bridge, which registers this handler as a live client on connect
Method Details
getAuthUser
Get the username this connection authenticated with, if any.- Returns:
- the authenticated username, or null if not authenticated
setAuthUser
Record the username this connection authenticated with.- Parameters:
user- the authenticated username, or null
getRemoteAddrPort
Get the remote address and port of this connection, bracketing IPv6, for rejection and ban logging.- Returns:
- e.g. "127.0.0.1:54321" or "[::1]:1234", "unknown" if unavailable
getRemoteHost
Get the remote address of this connection.- Returns:
- the remote IP, or null if unavailable
sameClient
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. Connections that authenticated share their username; otherwise the remote address decides, so a session may be used by a second connection from the same host but not by a host that merely knows the nickname. All loopback addresses count as the same host.- Parameters:
other- the other connection's handler, may be null- Returns:
- true if this connection may act on the other's sessions
sameClient
Decide whether two SAM connections belong to the same client. When both carry an authenticated user the user decides; otherwise the connections are the same only if they come from the same address, with any loopback address treated as the same host since the platform may report it in several forms.- Parameters:
myAuthUser- authenticated user of this connection, may be nullotherAuthUser- authenticated user of the other connection, may be nullmyHost- address of this connection, may be nullotherHost- address of the other connection, may be null- Returns:
- true if the two connections belong to the same client
isLoopbackAddress
Determine whether an address is a loopback address, in any of the forms the platform may report it.- Parameters:
host- the address, may be null- Returns:
- true if it is a loopback address
logSessionError
Log a session startup failure. During the first minute of uptime these are expected - the router's client manager may not be listening yet - so log at warn level; after that they are errors.- Parameters:
msg- the message receivede- the error that occurred
startHandling
public void startHandling()Start handling the SAM connection, detaching a handling thread. Subclasses (e.g. SAMv3Handler) may override to use a shared thread pool instead of a dedicated thread.handle
protected abstract void handle()Actually handle the SAM protocol.getClientSocket
Get the channel of the socket connected to the SAM client.- Returns:
- the client socket, or null before it is accepted
writeBytes
Write a byte array on the handler's socket. This method must always be used when writing data, unless you really know what you're doing.- Parameters:
data- A byte array to be written- Throws:
IOException- if the client socket refuses or fails the write
writeBytes
Caller must synch.- Parameters:
data- the bytes to writeout- the socket channel to write to- Throws:
IOException- on write errors
getWriteLock
If you're crazy enough to write to the raw socket, grab the write lock withgetWriteLock(), synchronize against it, and write to the getOut().- Returns:
- socket Write lock object
writeString
Write a string to the handler's socket. This method must always be used when writing strings, unless you really know what you're doing.- Parameters:
str- A string to be written- Returns:
- True if the string was successfully written, false otherwise
writeString
Unsynchronized, use with caution.- Parameters:
str- the string to writeout- the socket channel to write to- Returns:
- true on success
closeClientSocket
Close the socket connected to the SAM client.- Throws:
IOException- if the client socket cannot be closed
stopHandling
public void stopHandling()Stop the SAM handler, close the client socket, unregister with the bridge.- Specified by:
stopHandlingin interfaceHandler
shouldStop
protected final boolean shouldStop()Should the handler be stopped?- Returns:
- True if the handler should be stopped, false otherwise
toString
run