net.i2p.data

## Class TunnelId

- java.lang.Object
- - net.i2p.data.TunnelId

-     public class TunnelId
      extends Object

  Tunnel identifier for routing messages through a sequence of routers in I2P.

  TunnelId provides unique identification within tunnel routing:

  - Local Uniqueness: Must be unique on each router in tunnel
  - 4-Byte Value: 32-bit identifier (1 to 0xffffffff)
  - Random Generation: Typically generated from cryptographically secure random
  - Routing Coordination: Prevents message delivery to wrong tunnels

  Constraints and Validation:

  - Minimum Value: Must be greater than zero (ID > 0)
  - Maximum Value: Limited to 0xffffffff (32-bit unsigned integer)
  - Special Case: Zero reserved for direct replies in DatabaseStoreMessage
  - Uniqueness: Router must enforce uniqueness across its tunnels

  Usage in I2P:

  - Tunnel Routing: Messages forwarded through tunnel chain
  - Message Delivery: Ensures messages reach correct tunnel endpoint
  - Tunnel Management: Creation, configuration, and teardown
  - Load Balancing: Multiple tunnels for traffic distribution
  - Client Communication: I2CP tunnel establishment and management

  Performance Considerations:

  - Efficient Storage: Compact 4-byte representation
  - Fast Comparison: Optimized equals() and hashCode() methods
  - Minimal Overhead: No object inheritance since 0.9.48
  - Memory Efficiency: Primitive long storage

  Security Aspects:

  - ID Randomness: Use cryptographically secure random generation
  - ID Unpredictability: Prevent tunnel ID guessing attacks
  - Collision Avoidance: Local uniqueness prevents routing confusion
  - Isolation: Different tunnels have separate ID spaces

  Implementation Notes:

  - Space Optimization: Does not extend DataStructureImpl since 0.9.48
  - External Use: Not recommended for external use, subject to change
  - Thread Safety: Immutable objects are inherently thread-safe
  - Validation: Constructor enforces valid ID range

  Constants:

  - `MAX_ID_VALUE` - Maximum allowed tunnel ID (0xffffffff)

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static long` | `MAX_ID_VALUE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TunnelId()`
    `TunnelId(long id)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `equals(Object obj)`
    `long` | `getTunnelId()`
    `int` | `hashCode()`
    `void` | `readBytes(InputStream in)`
    `void` | `setTunnelId(long id)` Sets the tunnel ID for this lease.
    `String` | `toString()`
    `void` | `writeBytes(OutputStream out)`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### MAX_ID_VALUE

          public static final long MAX_ID_VALUE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### TunnelId

          public TunnelId()

    - #### TunnelId

          public TunnelId(long id)

      Parameters:
        `id` - 1 to 0xffffffff
      Throws:
        `IllegalArgumentException` - if less than or equal to zero or greater than max value

  - ### Method Detail

    - #### equals

          public boolean equals(Object obj)

      Overrides:
        `equals` in class `Object`

    - #### getTunnelId

          public long getTunnelId()

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### readBytes

          public void readBytes(InputStream in)
                         throws DataFormatException,
                                IOException

      Throws:
        `DataFormatException`
        `IOException`

    - #### setTunnelId

          public void setTunnelId(long id)

      Sets the tunnel ID for this lease.
      Parameters:
        `id` - 1 to 0xffffffff
      Throws:
        `IllegalArgumentException` - if less than or equal to zero or greater than max value

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### writeBytes

          public void writeBytes(OutputStream out)
                          throws DataFormatException,
                                 IOException

      Throws:
        `DataFormatException`
        `IOException`