All Implemented Interfaces:
Runnable, Handler, SAMDatagramReceiver, SAMRawReceiver, SAMStreamReceiver
Direct Known Subclasses:
SAMv2Handler, SAMv3Handler

class SAMv1Handlerextends SAMHandler implements SAMRawReceiver, SAMDatagramReceiver, SAMStreamReceiver
Class able to handle a SAM version 1 client connections.
  • Field Details

    • rawSession

      protected SAMMessageSess rawSession
      The SAM raw and datagram session, held after a successful HELLO.
    • datagramSession

      protected SAMMessageSess datagramSession
      The session used for datagram style messages.
    • streamSession

      protected SAMStreamSession streamSession
      The session used for STREAM CONNECT connections.
    • _id

      protected final long _id
      Identifier of this connection, unique among a router's SAM handlers.
    • __id

      private static final AtomicLong __id
      Counts the sessions created.
    • FIRST_READ_TIMEOUT

      private static final int FIRST_READ_TIMEOUT
      How long the first read may wait - 60 seconds.
      See Also:
    • SESSION_ERROR

      protected static final String SESSION_ERROR
      Reply sent to a SESSION CREATE whose stream could not be established.
      See Also:
  • Constructor Details

    • SAMv1Handler

      public SAMv1Handler(SocketChannel s, int verMajor, int verMinor, SAMBridge parent) throws SAMException, IOException
      Create a new SAM version 1 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 client
      verMajor - SAM major version to manage (should be 1)
      verMinor - SAM minor version to manage
      parent - the SAM bridge managing this handler
      Throws:
      SAMException - if the version is not supported
      IOException - if an I/O error occurs
    • SAMv1Handler

      public SAMv1Handler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) throws SAMException, IOException
      Create a new SAM version 1 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 client
      verMajor - SAM major version to manage (should be 1)
      verMinor - SAM minor version to manage
      i2cpProps - properties to configure the I2CP connection (host, port, etc)
      parent - the SAM bridge managing this handler
      Throws:
      SAMException - if the version is not supported
      IOException - if an I/O error occurs
  • Method Details

    • getRawSession

      protected final SAMMessageSess getRawSession()
      Return the raw session.
      Returns:
      the raw session
    • getDatagramSession

      protected final SAMMessageSess getDatagramSession()
      Return the datagram session.
      Returns:
      the datagram session
    • getStreamSession

      protected final SAMStreamSession getStreamSession()
      Return the stream session.
      Returns:
      the stream session
    • verifVersion

      public boolean verifVersion()
      Verify the SAM protocol version is supported by this handler.
      Returns:
      true if the major version matches this handler's version
    • handle

      public void handle()
      Handle a SAM v1 client connection by reading and dispatching commands in a loop until the connection is closed or an error occurs.
      Specified by:
      handle in class SAMHandler
    • execSessionMessage

      protected boolean execSessionMessage(String opcode, Properties props)
      Parse and execute a SESSION message.
      Parameters:
      opcode - the SESSION sub-command (e.g. CREATE)
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • newSAMStreamSession

      private SAMStreamSession newSAMStreamSession(String destKeystream, String direction, Properties props) throws IOException, DataFormatException, SAMException
      Opens a stream session for the destination named on the command line.
      Parameters:
      destKeystream - the destination and key parameters from the command line
      direction - whether the session is client or server
      props - the parameters of the session
      Returns:
      the new stream session
      Throws:
      IOException - if the session cannot talk to the router
      DataFormatException - if the destination keys are invalid
      SAMException - if the session cannot be set up
    • execDestMessage

      protected boolean execDestMessage(String opcode, Properties props)
      Parse and execute a DEST message.
      Parameters:
      opcode - the DEST sub-command (e.g. GENERATE)
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execNamingMessage

      protected boolean execNamingMessage(String opcode, Properties props)
      Parse and execute a NAMING message.
      Parameters:
      opcode - the NAMING sub-command (e.g. LOOKUP)
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execDatagramMessage

      protected boolean execDatagramMessage(String opcode, Properties props)
      Parse and execute a DATAGRAM message.
      Parameters:
      opcode - the DATAGRAM sub-command (e.g. SEND)
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execRawMessage

      protected boolean execRawMessage(String opcode, Properties props)
      Parse and execute a RAW message.
      Parameters:
      opcode - the RAW sub-command (e.g. SEND)
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execDgOrRawMessage

      private boolean execDgOrRawMessage(boolean isRaw, String opcode, Properties props)
      Runs a datagram or raw message command.
      Parameters:
      isRaw - true for the raw message form
      opcode - the name of the command
      props - the parameters of the command
      Returns:
      true if the command ran
    • execStreamMessage

      protected boolean execStreamMessage(String opcode, Properties props)
      Parse and execute a STREAM message.
      Parameters:
      opcode - the STREAM sub-command (e.g. SEND, CONNECT, CLOSE)
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execStreamSend

      protected boolean execStreamSend(Properties props)
      Parse and execute a STREAM SEND message.
      Parameters:
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execStreamConnect

      protected boolean execStreamConnect(Properties props)
      Parse and execute a STREAM CONNECT message.
      Parameters:
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • execStreamClose

      protected boolean execStreamClose(Properties props)
      Parse and execute a STREAM CLOSE message.
      Parameters:
      props - the parsed message properties
      Returns:
      true if the handler should continue processing commands
    • checkSize

      private boolean checkSize(int size)
      Check whether a size is inside the limits allowed by this protocol.
      Parameters:
      size - the payload length in bytes, which must fall between 1 and 32768 inclusive
      Returns:
      true if the size is within stream protocol limits
    • checkDatagramSize

      private boolean checkDatagramSize(int size)
      Check whether a size is inside the limits allowed by this protocol.
      Parameters:
      size - the payload length in bytes, which must fall between 1 and 31744 inclusive
      Returns:
      true if the size is within datagram protocol limits
    • receiveRawBytes

      public void receiveRawBytes(byte[] data, int proto, int fromPort, int toPort) throws IOException
      Receive raw bytes from the network and forward them to the SAM client.
      Specified by:
      receiveRawBytes in interface SAMRawReceiver
      Parameters:
      data - the received raw data
      proto - the protocol number
      fromPort - the source port
      toPort - the destination port
      Throws:
      IOException - if writing to the client socket fails
    • stopRawReceiving

      public void stopRawReceiving()
      Stop receiving raw bytes and close the client socket.
      Specified by:
      stopRawReceiving in interface SAMRawReceiver
    • receiveDatagramBytes

      public void receiveDatagramBytes(Destination sender, byte[] data, int proto, int fromPort, int toPort) throws IOException
      Receive datagram bytes from the network and forward them to the SAM client.
      Specified by:
      receiveDatagramBytes in interface SAMDatagramReceiver
      Parameters:
      sender - the destination of the datagram sender
      data - the received datagram data
      proto - the protocol number
      fromPort - the source port
      toPort - the destination port
      Throws:
      IOException - if writing to the client socket fails
    • stopDatagramReceiving

      public void stopDatagramReceiving()
      Stop receiving datagram bytes and close the client socket.
      Specified by:
      stopDatagramReceiving in interface SAMDatagramReceiver
    • streamSendAnswer

      public void streamSendAnswer(int id, String result, String bufferState) throws IOException
      Notify the SAM client of the result of a stream SEND operation.
      Specified by:
      streamSendAnswer in interface SAMStreamReceiver
      Parameters:
      id - the stream connection ID
      result - the result code (e.g. OK, TIMEOUT)
      bufferState - the current buffer state
      Throws:
      IOException - if writing to the client socket fails
    • notifyStreamSendBufferFree

      public void notifyStreamSendBufferFree(int id) throws IOException
      Notify the SAM client that the stream send buffer is free for more data.
      Specified by:
      notifyStreamSendBufferFree in interface SAMStreamReceiver
      Parameters:
      id - the stream connection ID
      Throws:
      IOException - if writing to the client socket fails
    • notifyStreamIncomingConnection

      public void notifyStreamIncomingConnection(int id, Destination d) throws IOException
      Notify the SAM client of an incoming stream connection.
      Specified by:
      notifyStreamIncomingConnection in interface SAMStreamReceiver
      Parameters:
      id - the stream connection ID
      d - the destination of the remote peer
      Throws:
      IOException - if writing to the client socket fails
    • notifyStreamOutgoingConnection

      public void notifyStreamOutgoingConnection(int id, String result, String msg) throws IOException
      Report the result of an outgoing stream connection.
      Specified by:
      notifyStreamOutgoingConnection in interface SAMStreamReceiver
      Parameters:
      id - New connection id
      result - status code sent in the STREAM STATUS reply
      msg - may be null
      Throws:
      IOException - if writing to the client socket fails
    • createMessageString

      protected static String createMessageString(String msg)
      Create a string to be appended to a status.
      Parameters:
      msg - may be null
      Returns:
      non-null, "" if msg is null, MESSAGE=msg or MESSAGE="msg a BC" with leading space if msg is non-null
    • writeString

      protected boolean writeString(String s, String msg)
      Write a string and message, escaping the message. Writes s + createMessageString(msg) + \n.
      Parameters:
      s - The string, non-null
      msg - the message to escape and append as a MESSAGE= field, or null for no message
      Returns:
      true if the composed line was written to the client socket
    • receiveStreamBytes

      public void receiveStreamBytes(int id, ByteBuffer data) throws IOException
      Receive stream bytes from the network and forward them to the SAM client.
      Specified by:
      receiveStreamBytes in interface SAMStreamReceiver
      Parameters:
      id - the stream connection ID
      data - the received data
      Throws:
      IOException - if writing to the client socket fails
    • notifyStreamDisconnection

      public void notifyStreamDisconnection(int id, String result, String msg) throws IOException
      Notify the SAM client that a stream connection has been disconnected.
      Specified by:
      notifyStreamDisconnection in interface SAMStreamReceiver
      Parameters:
      id - the stream connection ID
      result - the result code (e.g. OK, TIMEOUT)
      msg - optional message, may be null
      Throws:
      IOException - if writing to the client socket fails
    • stopStreamReceiving

      public void stopStreamReceiving()
      Stop receiving stream bytes and close the client socket.
      Specified by:
      stopStreamReceiving in interface SAMStreamReceiver