All Implemented Interfaces:
I2NPMessage

public class DatabaseLookupMessageextends FastI2NPMessageImpl
Defines the message a router sends to another router to search for a key in the network database.
  • Field Details

    • _log

      private static final Log _log
      Logger for the lookup messages.
    • MESSAGE_TYPE

      public static final int MESSAGE_TYPE
      The I2NP message type number identifying a database lookup.
      See Also:
    • _key

      private Hash _key
      The hash being looked up.
    • _fromHash

      private Hash _fromHash
      The hash of the requester.
    • _replyTunnel

      private TunnelId _replyTunnel
      Tunnel id to reply through.
    • _dontIncludePeers

      private List<Hash> _dontIncludePeers
      This must be kept as a list to preserve the order and not break the checksum.
    • _replyKey

      private SessionKey _replyKey
      Key to encrypt the reply with.
    • _replyTag

      private SessionTag _replyTag
      Tag identifying the reply.
    • _ratchetReplyTag

      private RatchetSessionTag _ratchetReplyTag
      Ratchet tag identifying the reply.
    • _ratchetPubKey

      private PublicKey _ratchetPubKey
      Ratchet key to encrypt the reply to.
    • _type

      The record type the requester wants.
    • USE_ECIES_FF

      public static final boolean USE_ECIES_FF
      Whether a reply session may use ECIES X25519 keys, not just ElGamal.
      See Also:
    • MAX_NUM_PEERS

      private static final int MAX_NUM_PEERS
      Insanely big. Not much more than 1500 will fit in a message. Have to prevent a huge alloc on rcv of a malicious msg though.
      See Also:
    • FLAG_TUNNEL

      private static final byte FLAG_TUNNEL
      Flag that the reply goes through a tunnel.
      See Also:
    • FLAG_ENCRYPT

      private static final byte FLAG_ENCRYPT
      Flag that the reply is encrypted, unknown to old routers.
      See Also:
    • FLAG_TYPE_MASK

      private static final byte FLAG_TYPE_MASK
      Mask of the record type bits.
      See Also:
    • FLAG_TYPE_ANY

      private static final byte FLAG_TYPE_ANY
      Type bits asking for any record.
      See Also:
    • FLAG_TYPE_LS

      private static final byte FLAG_TYPE_LS
      Type bits asking for a lease set.
      See Also:
    • FLAG_TYPE_RI

      private static final byte FLAG_TYPE_RI
      Type bits asking for a router info.
      See Also:
    • FLAG_TYPE_EXPL

      private static final byte FLAG_TYPE_EXPL
      Type bits asking for an exploratory search.
      See Also:
    • FLAG_RATCHET

      private static final byte FLAG_RATCHET
      Flag that the reply uses a ratchet key, 0.9.46 or newer.
      See Also:
    • MIN_ENCRYPTION_VERSION

      private static final String MIN_ENCRYPTION_VERSION
      It's not supported until 0.9.7, but as of 0.9.6 we can specify the bit in the flags without the receiver rejecting the whole message as invalid.
      See Also:
    • MIN_RATCHET_VERSION

      private static final String MIN_RATCHET_VERSION
      Oldest version understanding ratchet replies.
      See Also:
  • Constructor Details

    • DatabaseLookupMessage

      public DatabaseLookupMessage(I2PAppContext context)
      Creates a database lookup message for the given context.
      Parameters:
      context - the application context the message is created for
    • DatabaseLookupMessage

      public DatabaseLookupMessage(I2PAppContext context, boolean locallyCreated)
      Lookup message for the given context.
      Parameters:
      context - the application context the message is created for
      locallyCreated - ignored; this constructor always behaves the same
  • Method Details

    • getSearchKey

      public Hash getSearchKey()
      Defines the key being searched for.
      Returns:
      the search key
    • setSearchKey

      public void setSearchKey(Hash key)
      Assigns the key being searched for.
      Parameters:
      key - the key to look up, which may be set only once
      Throws:
      IllegalStateException - if key previously set, to protect saved checksum
    • getSearchType

      public DatabaseLookupMessage.Type getSearchType()
      Defines the type of data being searched for. Default ANY.
      Returns:
      non-null
    • setSearchType

      public void setSearchType(DatabaseLookupMessage.Type type)
      Defines the type of data being searched for. Default ANY. Must be ANY for queried routers 0.9.5 or lower, but there are few if any floodfills that old left, so not even worth checking.
      Parameters:
      type - non-null
    • getFrom

      public Hash getFrom()
      Contains the router who requested this lookup.
      Returns:
      the hash of the router that requested this lookup
    • setFrom

      public void setFrom(Hash from)
      Assigns the router who requested this lookup.
      Parameters:
      from - the requesting router, recorded once so a reply can be routed back to it
      Throws:
      IllegalStateException - if from previously set, to protect saved checksum
    • getReplyTunnel

      public TunnelId getReplyTunnel()
      Contains the tunnel ID a reply should be sent to.
      Returns:
      the reply tunnel
    • setReplyTunnel

      public void setReplyTunnel(TunnelId replyTunnel)
      Assigns the tunnel a reply should be sent to.
      Parameters:
      replyTunnel - the tunnel any reply to this lookup should be sent through
      Throws:
      IllegalStateException - if tunnel previously set, to protect saved checksum
    • supportsEncryptedReplies

      public static boolean supportsEncryptedReplies(RouterInfo to)
      Does this router support encrypted replies?
      Parameters:
      to - null OK returns true as of 0.9.69
      Returns:
      true if the router is new enough and its public key type can encrypt replies
    • supportsRatchetReplies

      public static boolean supportsRatchetReplies(RouterInfo to)
      Does this router support ratchet replies?
      Parameters:
      to - null OK returns true as of 0.9.69
      Returns:
      true if the router is new enough and its public key type supports ratchets
    • getReplyKey

      public SessionKey getReplyKey()
      The included session key or null if unset. If non-null, either getReplyTag() or getRatchetReplyTag() is non-null.
      Returns:
      the reply key
    • getReplyTag

      public SessionTag getReplyTag()
      The included session tag or null if unset.
      Returns:
      the reply tag
    • setReplySession

      public void setReplySession(SessionKey encryptKey, SessionTag encryptTag)
      Only worthwhile if sending reply via tunnel.
      Parameters:
      encryptKey - non-null
      encryptTag - non-null
      Throws:
      IllegalStateException - if key or tag previously set, to protect saved checksum
    • getRatchetReplyTag

      public RatchetSessionTag getRatchetReplyTag()
      The included session tag or null if unset.
      Returns:
      the ratchet reply tag
    • setReplySession

      public void setReplySession(SessionKey encryptKey, RatchetSessionTag encryptTag)
      Sets the reply session for ratchet encryption; only once per message.
      Parameters:
      encryptKey - non-null
      encryptTag - non-null
      Throws:
      IllegalStateException - if key or tag previously set, to protect saved checksum
    • getRatchetPublicKey

      public PublicKey getRatchetPublicKey()
      The included session key or null if unset. Preliminary, not fully supported, see proposal 154.
      Returns:
      the ratchet public key
    • setReplySession

      public void setReplySession(PublicKey pubKey)
      Sets the reply session's ratchet public key. Preliminary, not fully supported, see proposal 154.
      Parameters:
      pubKey - non-null
      Throws:
      IllegalStateException - if key or tag previously set, to protect saved checksum
    • getDontIncludePeers

      public Set<Hash> getDontIncludePeers()
      Collection of peers that a lookup reply should NOT include. WARNING - returns a copy.
      Returns:
      Set of Hash objects, each of which is the H(routerIdentity) to skip, or null
    • setDontIncludePeers

      public void setDontIncludePeers(Collection<Hash> peers)
      Replace the dontInclude set with this set. WARNING - makes a copy. Invalidates the checksum.
      Parameters:
      peers - may be null
    • addDontIncludePeer

      public void addDontIncludePeer(Hash peer)
      Add to the set. Invalidates the checksum.
      Parameters:
      peer - non-null
    • addDontIncludePeers

      public void addDontIncludePeers(Collection<Hash> peers)
      Add to the set. Invalidates the checksum.
      Parameters:
      peers - non-null
    • readMessage

      public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException
      Reads the message fields from the byte array.
      Parameters:
      data - data to read from
      offset - where to start in the data array
      dataSize - how long into the data to read
      type - I2NP message type
      Throws:
      I2NPMessageException - if the stream doesn't contain a valid message that this class can read.
    • calculateWrittenLength

      protected int calculateWrittenLength()
      Calculates the length of the message body.
      Specified by:
      calculateWrittenLength in class I2NPMessageImpl
      Returns:
      the number of bytes the body will occupy once serialized
    • writeMessageBody

      protected int writeMessageBody(byte[] out, int curIndex) throws I2NPMessageException
      Writes the message body to the output array, returning the new index.
      Specified by:
      writeMessageBody in class I2NPMessageImpl
      Parameters:
      out - the buffer to serialize the body into
      curIndex - the offset into out at which to begin writing; leave room for the signature where the header format calls for one
      Returns:
      the index into the array after the last byte written (NOT the length)
      Throws:
      I2NPMessageException - if the body cannot be serialized into out, because it is too small or a field is unserializable
    • getType

      public int getType()
      The message type.
      Returns:
      the DatabaseLookupMessage message type constant
    • hashCode

      public int hashCode()
      Hash code calculated from the message fields.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • equals

      public boolean equals(Object object)
      Compares this message to another for equality of all fields.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      String form of the message, suitable for debugging. The INFO form masks every hash to the house truncation and carries no key material; the full keys are DEBUG-only, as in BuildRequestRecord.toKeyString().
      Overrides:
      toString in class Object
    • shortKey

      static String shortKey(DatabaseLookupMessage.Type type, Hash key)
      The search key as the house truncation - 8 base32 characters for a lease set, 6 base64 for anything else - bracketed so the console links it.
      Parameters:
      type - what is being searched for, deciding the encoding used
      key - the search key to truncate
      Returns:
      the bracketed truncation, never null
    • toKeyString

      public String toKeyString()
      Key material for lookup interop debugging only. Never log this at INFO or above. Starts with a newline and an asterisk for multi-line log entries.
      Returns:
      the key material dump, never null