Class I2NPMessageImpl

    • Constructor Detail

      • I2NPMessageImpl

        public I2NPMessageImpl​(I2PAppContext context)
    • Method Detail

      • readBytes

        public int readBytes​(byte[] data,
                             int type,
                             int offset)
                      throws I2NPMessageException
        Read the header, then read the rest into buffer, then call readMessage in the implemented message type
          Specifically:
            1 byte type (if caller didn't read already, as specified by the type param
            4 byte ID
            8 byte expiration
            2 byte size
            1 byte checksum
            size bytes of payload (read by readMessage() in implementation)
        
        Specified by:
        readBytes in interface I2NPMessage
        Parameters:
        type - the message type or -1 if we should read it here
        data - the data
        offset - where to start starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
        Returns:
        total length of the message
        Throws:
        I2NPMessageException - if there is no valid message
      • readBytes

        public int readBytes​(byte[] data,
                             int type,
                             int offset,
                             int maxLen)
                      throws I2NPMessageException
        Set a limit on the max to read from the data buffer, so that we can use a large buffer but prevent the reader from reading off the end.
        Specified by:
        readBytes in interface I2NPMessage
        Parameters:
        type - the message type or -1 if we should read it here
        maxLen - read no more than this many bytes from data starting at offset, even if it is longer This includes the type byte only if type < 0
        data - the data, may or may not include the type
        offset - where to start starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
        Returns:
        total length of the message
        Throws:
        I2NPMessageException - if there is no valid message
        Since:
        0.8.12
      • getUniqueId

        public long getUniqueId()
        Replay resistant message Id
        Specified by:
        getUniqueId in interface I2NPMessage
      • setUniqueId

        public void setUniqueId​(long id)
        The ID is set to a random value when written but it can be overridden here.
        Specified by:
        setUniqueId in interface I2NPMessage
      • getMessageExpiration

        public long getMessageExpiration()
        Date after which the message should be dropped (and the associated uniqueId forgotten)
        Specified by:
        getMessageExpiration in interface I2NPMessage
      • setMessageExpiration

        public void setMessageExpiration​(long exp)
        The expiration is set to one minute from now in the constructor but it can be overridden here.
        Specified by:
        setMessageExpiration in interface I2NPMessage
      • getMessageSize

        public int getMessageSize()
        Description copied from interface: I2NPMessage
        How large the message is, including any checksums, i.e. full 16 byte header
        Specified by:
        getMessageSize in interface I2NPMessage
      • getRawMessageSize

        public int getRawMessageSize()
        The raw header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!
        Specified by:
        getRawMessageSize in interface I2NPMessage
      • toByteArray

        public int toByteArray​(byte[] buffer)
        write the message to the buffer, returning the number of bytes written. the data is formatted so as to be self contained, with the type, size, expiration, unique id, as well as a checksum bundled along. Full 16 byte header for NTCP 1.
        Specified by:
        toByteArray in interface I2NPMessage
        Returns:
        the length written
      • toByteArray

        public int toByteArray​(byte[] buffer,
                               int off)
        Write the message to the buffer, returning the new offset (NOT the length). the data is formatted so as to be self contained, with the type, size, expiration, unique id, as well as a checksum bundled along. Full 16 byte header for NTCP 1.
        Specified by:
        toByteArray in interface I2NPMessage
        Parameters:
        off - the offset to start writing at
        Returns:
        the new offset (NOT the length)
        Since:
        0.9.36 with off param
      • calculateWrittenLength

        protected abstract int calculateWrittenLength()
        calculate the message body's length (not including the header and footer
      • writeMessageBody

        protected abstract int writeMessageBody​(byte[] out,
                                                int curIndex)
                                         throws I2NPMessageException
        write the message body to the output array, starting at the given index.
        Returns:
        the index into the array after the last byte written (NOT the length)
        Throws:
        I2NPMessageException
      • toRawByteArray

        public int toRawByteArray​(byte[] buffer)
        Write the message with a short 5-byte header. THe header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!
        Specified by:
        toRawByteArray in interface I2NPMessage
        Returns:
        the new offset (NOT the length)
      • toRawByteArrayNTCP2

        public int toRawByteArrayNTCP2​(byte[] buffer,
                                       int off)
        Write the message to the buffer, returning the new offset (NOT the length). the data is is not self contained - it does not include the size, unique id, or any checksum, but does include the type and expiration. Short 9 byte header for NTCP 2.
        Specified by:
        toRawByteArrayNTCP2 in interface I2NPMessage
        Parameters:
        off - the offset to start writing at
        Returns:
        the new offset (NOT the length)
        Since:
        0.9.36