- All Implemented Interfaces:
Serializable, Comparable<ByteArray>
Tokens are used in the BitTorrent DHT protocol to prevent certain types of attacks. When a peer announces itself to a DHT node, it receives a token that must be included in subsequent get_peer requests. This verifies that the requesting peer has previously communicated with the DHT node from the same IP address.
This class handles both outgoing tokens (generated locally for sending to peers) and incoming tokens (received from remote DHT nodes). Tokens have limited lifetimes and are automatically expired to prevent replay attacks.
Security considerations:
- Outgoing tokens are 8 bytes of cryptographically secure random data
- Incoming tokens may be up to 64 bytes (to accommodate various implementations)
- Tokens are timestamped for automatic expiration
- Lookup-only tokens don't store timestamps to save memory
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final longTimestamp when this token was created or last seen.private static final intMaximum allowed token length for incoming tokens (64 bytes).private static final intDefault token length for outgoing tokens (8 bytes).private static final longPinned for serialized-form compatibility.Constructor Summary
ConstructorsConstructorDescriptionToken(byte[] data) Creates a lookup-only token without timestamp.Token(I2PAppContext ctx) Creates a new outgoing token for sending to remote peers.Token(I2PAppContext ctx, byte[] data) Creates an incoming token received from a remote DHT node.Method Summary
All MethodsMethods inherited from class ByteArray
compareTo, equals, getData, getOffset, getValid, hashCode, setData, setOffset, setValid, toBase64Modifier and TypeMethodDescriptionfinal intCompares this ByteArray with another ByteArray.final booleanCompares this ByteArray with another object for equality.byte[]getData()Returns the underlying byte array.intReturns the offset into the byte array.intgetValid()Count how many of the bytes in the array are 'valid'.final inthashCode()Hash code based on the underlying byte array data.voidsetData(byte[] data) Sets the underlying byte array.voidsetOffset(int offset) Offset into the byte array.voidsetValid(int valid) Number of valid bytes.final StringtoBase64()Returns this ByteArray as a Base64 encoded string.
Field Details
serialVersionUID
private static final long serialVersionUIDPinned for serialized-form compatibility.- See Also:
MY_TOK_LEN
private static final int MY_TOK_LENDefault token length for outgoing tokens (8 bytes).- See Also:
MAX_TOK_LEN
private static final int MAX_TOK_LENMaximum allowed token length for incoming tokens (64 bytes).- See Also:
lastSeen
private final long lastSeenTimestamp when this token was created or last seen.
Constructor Details
Token
Creates a new outgoing token for sending to remote peers.Generates 8 bytes of cryptographically secure random data that will be used as a token. The token is timestamped for expiration tracking. This constructor is used when we are the DHT node responding to a get_peer request.
- Parameters:
ctx- the I2P application context for random number generation and clock access
Token
Creates an incoming token received from a remote DHT node.Stores a token received from a remote DHT node for later use in announce_peer requests. The token is timestamped for expiration tracking. Tokens longer than 64 bytes are rejected to prevent memory exhaustion attacks.
- Parameters:
ctx- the I2P application context for clock accessdata- the token data received from the remote DHT node- Throws:
IllegalArgumentException- if the token data exceeds 64 bytes
Token
public Token(byte[] data) Creates a lookup-only token without timestamp.Creates a token instance for comparison purposes only, without storing a timestamp. This is useful when checking if a received token matches an expected value without needing to track its age for expiration.
- Parameters:
data- the token data to use for lookup comparison
Method Details
lastSeen
public long lastSeen()Returns the timestamp when this token was created or last seen.- Returns:
- the timestamp in milliseconds since epoch, or 0 for lookup-only tokens
toString
Returns a string representation of the token for debugging and logging.Formats the token data as hexadecimal and includes the creation timestamp if available. The format is consistent with other DHT debugging output.