All Implemented Interfaces:
DataStructure

class NodeInfoextends SimpleDataStructure
Comprehensive node information container for DHT operations.

This class encapsulates all essential information about a DHT node including its Node ID (NID), destination hash, port, and optionally the full I2P destination. It serves as the primary data structure stored in DHTNodes for routing table management and node discovery operations.

Data structure components:
- NID: 20-byte node identifier used for Kademlia distance calculations
- Hash: 32-byte destination hash for I2P routing
- Port: 2-byte network port for DHT communication
- Destination: Optional full I2P destination (may be added later)

The compact representation (54 bytes) is immutable and contains NID, hash, and port. This compact format is used for network transmission and efficient storage. The full destination may be added later when available, enabling direct communication with the node.

Design considerations:
- DHT exchanges often use only hashes initially, with destinations added later
- Compact format enables efficient network transmission and storage
- Immutable core data ensures consistency in routing operations
- Optional destination allows progressive information discovery

  • Field Details

    • nID

      private final NID nID
      The node's DHT id.
    • hash

      private final Hash hash
      The node's router hash.
    • dest

      private Destination dest
      The node's destination, resolved when needed.
    • port

      private final int port
      The KRPC port the node listens on.
    • LENGTH

      public static final int LENGTH
      Serialized length in bytes (NID + Hash + 2).
      See Also:
  • Constructor Details

    • NodeInfo

      public NodeInfo(Destination dest, int port)
      With a fake NID used for pings.
      Parameters:
      dest - the destination that will be resolved when the node is contacted
      port - the DHT query port, or 0 if unknown
    • NodeInfo

      public NodeInfo(NID nID, Destination dest, int port)
      Use this if we have the full destination.
      Parameters:
      nID - the node identifier
      dest - the full I2P destination
      port - the DHT query port
      Throws:
      IllegalArgumentException - if the NID does not match the destination hash
    • NodeInfo

      public NodeInfo(NID nID, Hash hash, int port)
      No Destination yet available.
      Parameters:
      nID - the node identifier
      hash - the destination hash
      port - the DHT query port
      Throws:
      IllegalArgumentException - if the NID does not match the hash or port is invalid
    • NodeInfo

      public NodeInfo(byte[] compactInfo, int offset)
      No Destination yet available. Creates from compact node info.
      Parameters:
      compactInfo - 20 byte node ID, 32 byte destHash, 2 byte port
      offset - starting at this offset in compactInfo
      Throws:
      IllegalArgumentException - if port is invalid
      ArrayIndexOutOfBoundsException - if compactInfo is too short
    • NodeInfo

      public NodeInfo(String s) throws DataFormatException
      Create from persistent storage string. Format: NID:Hash:Destination:port. First 3 in base 64; Destination may be empty string.
      Parameters:
      s - the persistent storage string
      Throws:
      DataFormatException - if the string format is invalid
      IllegalArgumentException - if the NID does not match the hash
  • Method Details

    • initialize

      private void initialize()
      Creates 54-byte compact info from the current fields.
      Throws:
      IllegalArgumentException - if port is invalid
    • generateNID

      public static NID generateNID(Hash h, int p, RandomSource random)
      Generate a secure NID that matches the Hash and port. Rules: First 4 bytes must match Hash. Next 2 bytes must match Hash ^ port. Remaining bytes may be random.
      Parameters:
      h - the destination hash
      p - the port number
      random - the source filling the bytes that follow the hash and port
      Returns:
      the generated NID
    • verify

      private void verify()
      Verify the NID matches the Hash. See generateNID(Hash, int, RandomSource) for requirements.
      Throws:
      IllegalArgumentException - if the NID does not match the hash, per generateNID()
    • length

      public int length()
      Description copied from class: SimpleDataStructure
      The legal length of the byte array in this data structure.
      Specified by:
      length in class SimpleDataStructure
      Returns:
      the legal length of the byte array
    • getNID

      public NID getNID()
      The node ID.
      Returns:
      the n i d
    • getDestination

      public Destination getDestination()
      The destination, may be null if we don't have it.
      Returns:
      may be null if we don't have it
    • getHash

      public Hash getHash()
      The hash.
      Returns:
      the hash this node is identified by
    • calculateHash

      public Hash calculateHash()
      Calculate the hash.
      Specified by:
      calculateHash in interface DataStructure
      Overrides:
      calculateHash in class SimpleDataStructure
      Returns:
      SHA256 hash, or null if there were problems (data format or io errors)
    • setDestination

      public void setDestination(Destination dest) throws IllegalArgumentException
      This can come in later but the hash must match.
      Parameters:
      dest - the destination to attach, which must hash to the current hash
      Throws:
      IllegalArgumentException - if hash of dest doesn't match previous hash
    • getPort

      public int getPort()
      The port.
      Returns:
      the UDP port this node was last heard from on
    • lastSeen

      public long lastSeen()
      When this node was last successfully heard from, taken from the node ID.
      Returns:
      the timestamp in milliseconds since epoch, or 0 if never seen
    • hashCode

      public int hashCode()
      Whether hash code is present.
      Overrides:
      hashCode in class SimpleDataStructure
      Returns:
      whether h code is present
    • equals

      public boolean equals(Object o)
      Whether this node equals the other.
      Overrides:
      equals in class SimpleDataStructure
      Parameters:
      o - the object to compare
      Returns:
      true if the data arrays are equal
    • toString

      public String toString()
      String form of the node.
      Overrides:
      toString in class SimpleDataStructure
    • toPersistentString

      public String toPersistentString()
      To persistent storage string. Format: NID:Hash:Destination:port First 3 in base 64; Destination may be empty string.
      Returns:
      the colon-separated persistence form, with an empty destination field when the full destination is not yet known