java.lang.Object
net.i2p.data.DataStructureImpl
net.i2p.data.DatabaseEntry
- All Implemented Interfaces:
Serializable, DataStructure
- Direct Known Subclasses:
LeaseSet, RouterInfo
Base class for data structures stored in the I2P network database (NetDb).
DatabaseEntry provides common infrastructure for NetDb-storable objects:
- Core Types: Base for
LeaseSetandRouterInfo - Unified Interface: Simplifies NetDB and I2NP implementation
- Common Operations: Shared methods for hashing, routing, and signing
- Integrity Protection: Prevents modification after signing
Key Features:
- SHA-256 Hash: Unique identifier for database storage and lookup
- Routing Key: Modified hash for distributed hash Table (DHT) routing
- Timestamp: Creation or last-modified time for cache management
- Signatures: Cryptographic signatures for authenticity verification
- Type Constants: Standardized type identifiers for all NetDb entries
Supported Entry Types:
KEY_TYPE_ROUTERINFO- Router information and capabilitiesKEY_TYPE_LEASESET- Legacy LeaseSet formatKEY_TYPE_LS2- Modern LeaseSet2 formatKEY_TYPE_META_LS2- MetaLeaseSet for advanced routing
Integrity Protection:
- Immutable After Signing: Objects cannot be modified once signed
- IllegalStateException: Thrown when modification is attempted
- NetDb Protection: Prevents corruption of network database
- Message Safety: Protects I2NP messages from tampering
Usage Patterns:
- Network Storage: Entries stored in distributed hash table
- Lookup Operations: Fast retrieval by hash or routing key
- Cache Management: Timestamp-based expiration and refresh
- Floodfill: Distribution of new and updated entries
Security Considerations:
- Signature Verification: Always verify before trusting entries
- Timestamp Validation: Check for stale or expired entries
- Hash Collision: Verify SHA-256 hash matches content
- Routing Key Security: Protect against routing attacks
Implementation Notes:
- 0.8.2: Initial implementation with retrofitting
- Code Consolidation: Reduces instanceof usage throughout codebase
- Thread Safety: Protection not guaranteed to be thread-safe
- Object Reuse: Do not reuse DatabaseEntry instances
Warning:
- Direct Modification: Avoid modifying internal byte[] objects
- Corruption Avenues: Multiple paths can bypass protection
- Careful Usage: Protection is not foolproof against all attacks
- See Also:
Field Summary
Modifier and TypeFieldDescriptionprivate HashCurrent routing key, cached.private booleanWhether this entry was received via a remote publish.private booleanWhether this entry was received as a reply to a search.private HashHash of the client receiving the routerinfo, or null if it was sent directly.private longRouting key generation modifier, for cache invalidation.protected SignatureCryptographic signature.static final intEncrypted LeaseSet 2 type.static final intLeaseSet (v1) type.static final intLeaseSet 2 type.static final intMeta LeaseSet 2 type.static final intRouter info type.static final intService list type.static final intService record type.Constructor Summary
ConstructorsConstructorDescriptionConstructor for NetDb entries; a subclass passes its entry type to the constructor that takes one.Method Summary
Modifier and TypeMethodDescriptionprotected abstract byte[]getBytes()Returns the raw payload data, excluding the signature, to be signed bysign(SigningPrivateKey).abstract longgetDate()A common interface to the timestamp of the two subclasses.getHash()A common interface to the Hash of the two subclasses.abstract KeysAndCertKeys and certificate for this entry.booleanIf true, we received this LeaseSet by a remote peer publishing it to us, rather than by searching for it ourselves or locally creating it.booleanIf true, we received this LeaseSet by searching for it Default false.The Hash of the local client that received this LS, null if the router or unknown.Routing key for the structure using the current modifier in the RoutingKeyGenerator.Retrieve the proof that the identity stands behind the info here.protected SigningPublicKeyIdentical to getDestination().getSigningPublicKey()in LeaseSet, and getIdentity().getSigningPublicKey() in RouterInfo.abstract intgetType()Type of the data structure.booleanConvenience method, is the type any variant of leaseset?static booleanisLeaseSet(int type) Convenience method, is the type any variant of leaseset?booleanConvenience method, is the type any variant of router info?voidMarks this entry as received via published mode.voidMarks this entry as received via reply.voidsetReceivedBy(Hash receivedBy) Local client that received this entry.voidsetSignature(Signature signature) Configure the proof that the entity stands behind the info here.voidsign(SigningPrivateKey key) Sign the structure using the supplied signing key.booleanValidates the routing key for this entry.booleanThis is the same as isValid() in RouterInfo orverifySignature()in LeaseSet.Methods inherited from class DataStructureImpl
calculateHash, fromBase64, fromByteArray, read, toBase64, toByteArrayModifier and TypeMethodDescriptionCalculate the SHA256 value of this object (useful for a few scenarios).voidfromBase64(String data) Load the structure from the provided base64 string.voidfromByteArray(byte[] data) Loads structure from the data providedprotected intread(InputStream in, byte[] target) Repeated reads until buffer is full or IOException is thrown.toBase64()Render the structure into modified base 64 notation.byte[]Convert the structure to a byte array.Methods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DataStructure
readBytes, writeBytesModifier and TypeMethodDescriptionvoidreadBytes(InputStream in) Load up the current object with data from the given stream.voidwriteBytes(OutputStream out) Write out the data structure to the stream, using the format defined in the I2P data structure specification.
Field Details
KEY_TYPE_ROUTERINFO
public static final int KEY_TYPE_ROUTERINFORouter info type. These are the same as in i2np's DatabaseStoreMessage.- See Also:
KEY_TYPE_LEASESET
KEY_TYPE_LS2
KEY_TYPE_ENCRYPTED_LS2
public static final int KEY_TYPE_ENCRYPTED_LS2Encrypted LeaseSet 2 type.- See Also:
KEY_TYPE_META_LS2
KEY_TYPE_SERVICE_RECORD
KEY_TYPE_SERVICE_LIST
_signature
Cryptographic signature._currentRoutingKey
Current routing key, cached. Synchronize on this object._routingKeyGenMod
private long _routingKeyGenModRouting key generation modifier, for cache invalidation._receivedAsPublished
private volatile boolean _receivedAsPublishedWhether this entry was received via a remote publish._receivedAsReply
private volatile boolean _receivedAsReplyWhether this entry was received as a reply to a search._receivedBy
Hash of the client receiving the routerinfo, or null if it was sent directly.
Constructor Details
DatabaseEntry
public DatabaseEntry()Constructor for NetDb entries; a subclass passes its entry type to the constructor that takes one.
Method Details
getReceivedBy
The Hash of the local client that received this LS, null if the router or unknown.- Returns:
- the hash of the receiving client, or null
setReceivedBy
Local client that received this entry.- Parameters:
receivedBy- the hash of the receiving client
getDate
public abstract long getDate()A common interface to the timestamp of the two subclasses. Identical to getEarliestLeaseDate() inLeaseSet, and getPublished() in RouterInfo. Note that for a LeaseSet this will be in the future, and for a RouterInfo it will be in the past. Either way, it's a timestamp.- Returns:
- the date this entry takes effect, in milliseconds since the epoch: the expiration of the earliest lease for a LeaseSet, the publication time for a RouterInfo
getKeysAndCert
Keys and certificate for this entry. Identical to getDestination() in LeaseSet, and getIdentity() in RouterInfo.- Returns:
- KAC or null
getHash
getType
public abstract int getType()Type of the data structure. This should be faster than instanceof.- Returns:
- KEY_TYPE_ROUTERINFO or KEY_TYPE_LEASESET or LS2 types
isLeaseSet
public boolean isLeaseSet()Convenience method, is the type any variant of leaseset?- Returns:
- true for any type of LeaseSet, false for RouterInfo, false for others
isLeaseSet
public static boolean isLeaseSet(int type) Convenience method, is the type any variant of leaseset?- Parameters:
type- one of the DatabaseEntry key type constants- Returns:
- true for any type of LeaseSet, false for RouterInfo, false for others
isRouterInfo
public boolean isRouterInfo()Convenience method, is the type any variant of router info?- Returns:
- true for any type of RouterInfo, false for LeaseSet, false for others
getBytes
Returns the raw payload data, excluding the signature, to be signed bysign(SigningPrivateKey).Most callers should use writeBytes() or toByteArray() instead.
- Returns:
- the raw payload data, or null on error (for example, if this is a RouterInfo, a DataFormatException is thrown instead)
- Throws:
DataFormatException- if the data cannot be serialized
getRoutingKey
Routing key for the structure using the current modifier in the RoutingKeyGenerator. This only calculates a new one when necessary though (if the generator's key modifier changes).- Returns:
- the routing key
- Throws:
IllegalStateException- if not in RouterContext
validateRoutingKey
public boolean validateRoutingKey()Validates the routing key for this entry.- Returns:
- true if the routing key is valid
- Throws:
IllegalStateException- if not in RouterContext
getSignature
Retrieve the proof that the identity stands behind the info here.- Returns:
- the signature, or null if not signed
setSignature
Configure the proof that the entity stands behind the info here.- Parameters:
signature- the signature to set- Throws:
IllegalStateException- if already signed
sign
Sign the structure using the supplied signing key.- Parameters:
key- the signing private key- Throws:
IllegalStateException- if already signedDataFormatException- if signing fails
getSigningPublicKey
Identical to getDestination().getSigningPublicKey()in LeaseSet, and getIdentity().getSigningPublicKey() in RouterInfo.- Returns:
- SPK or null
verifySignature
public boolean verifySignature()This is the same as isValid() in RouterInfo orverifySignature()in LeaseSet.- Returns:
- true if the entry carries a signature that verifies over its own data
getReceivedAsPublished
public boolean getReceivedAsPublished()If true, we received this LeaseSet by a remote peer publishing it to us, rather than by searching for it ourselves or locally creating it. Default false.- Returns:
- true if received as published
setReceivedAsPublished
public void setReceivedAsPublished()Marks this entry as received via published mode. Use this carefully, when updating the flag make sure the old and new leaseSet are actually equivalent, or simply copy the reply value, as copyStoreMetadata() in KademliaNetworkDatabaseFacade does.getReceivedAsReply
public boolean getReceivedAsReply()If true, we received this LeaseSet by searching for it Default false.- Returns:
- true if received as reply
setReceivedAsReply
public void setReceivedAsReply()Marks this entry as received via reply.