java.lang.Object
org.klomp.snark.ExtensionHandler

abstract class ExtensionHandlerextends Object
REF: BEP 10 Extension Protocol.
  • Field Summary

    Modifier and Type
    Field
    Description
    private static final byte[]
    Filter saying comment requests are not wanted.
    private static final int
    Length of the piece hashes, in bytes.
    static final int
    Comment extension message ID (4).
    static final int
    DHT extension message ID (3).
    static final int
    Handshake extension message ID (0).
    static final int
    BEP 54 lt_donthave extension message ID (5).
    static final int
    Metadata extension message ID (1).
    static final int
    PEX extension message ID (2).
    private static final int
    Pieces * SHA1 Hash length, + 25% extra for file names, bencoding overhead, etc.
    private static final int
    Extension requests in flight per peer.
    static final String
    Comment extension type string, carried in the handshake's type map.
    private static final int
    Extension message carrying data.
    static final String
    Not using the option bit since the compact format is different.
    static final String
    BEP 54 lt_donthave extension type string.
    static final String
    Metadata extension type string.
    static final String
    Not ut_pex since the compact format is different.
    private static final int
    Extension message refusing a request.
    private static final int
    Extension message asking for data.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for subclasses, each of which handles one BEP 10 extension protocol.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    getHandshake(int metasize, boolean pexAndMetadata, boolean dht, boolean uploadOnly, boolean comment, String clientName)
    Creates a bencoded extension handshake message.
    private static void
    handleComment(Peer peer, PeerListener listener, byte[] bs, Log log)
    Handles an incoming comment request or response message.
    private static void
    handleDHT(Peer peer, PeerListener listener, byte[] bs, Log log)
    Handles an incoming DHT port message.
    private static void
    handleHandshake(Peer peer, PeerListener listener, byte[] bs, Log log)
    Handles an incoming extension handshake message.
    static void
    handleMessage(Peer peer, PeerListener listener, int id, byte[] bs)
    Handles an incoming extension message from a peer.
    private static void
    handleMetadata(Peer peer, PeerListener listener, byte[] bs, Log log)
    Handles an incoming metadata extension message (BEP 9).
    private static void
    handlePEX(Peer peer, PeerListener listener, byte[] bs, Log log)
    Handles an incoming PEX message.
    static void
    locked_sendComments(Peer peer, int num, CommentSet comments)
    Sends comments to a peer.
    static void
    sendCommentReq(Peer peer, int num)
    Sends a comment request to a peer.
    static void
    sendDHT(Peer peer, int qport, int rport)
    Sends DHT port numbers to a peer.
    static void
    sendDontHave(Peer peer, int piece)
    Send a BEP 54 lt_donthave message to a peer that advertised support: the piece is no longer available.
    private static void
    sendExtension(Peer peer, String type, byte[] payload)
    Send an extension message to a peer, looking up its message code for the given type in the handshake.
    private static void
    sendMessage(Peer peer, int type, int piece)
    Sends a metadata request or reject message to a peer.
    static void
    sendPEX(Peer peer, List<Peer> pList)
    Sends a PEX message with the given peer list.
    private static void
    sendPiece(Peer peer, int piece, byte[] data, int totalSize)
    Sends a metadata chunk data message to a peer.
    private static void
    sendRequest(Peer peer, int piece)
    Sends a metadata chunk request to a peer.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ID_HANDSHAKE

      public static final int ID_HANDSHAKE
      Handshake extension message ID (0).
      See Also:
    • ID_METADATA

      public static final int ID_METADATA
      Metadata extension message ID (1).
      See Also:
    • TYPE_METADATA

      public static final String TYPE_METADATA
      Metadata extension type string.
      See Also:
    • ID_PEX

      public static final int ID_PEX
      PEX extension message ID (2).
      See Also:
    • TYPE_PEX

      public static final String TYPE_PEX
      Not ut_pex since the compact format is different.
      See Also:
    • ID_DHT

      public static final int ID_DHT
      DHT extension message ID (3).
      See Also:
    • TYPE_DHT

      public static final String TYPE_DHT
      Not using the option bit since the compact format is different.
      See Also:
    • ID_COMMENT

      public static final int ID_COMMENT
      Comment extension message ID (4).
      See Also:
    • TYPE_COMMENT

      public static final String TYPE_COMMENT
      Comment extension type string, carried in the handshake's type map.
      See Also:
    • ID_LT_DONTHAVE

      public static final int ID_LT_DONTHAVE
      BEP 54 lt_donthave extension message ID (5).
      See Also:
    • TYPE_LT_DONTHAVE

      public static final String TYPE_LT_DONTHAVE
      BEP 54 lt_donthave extension type string.
      See Also:
    • MAX_METADATA_SIZE

      private static final int MAX_METADATA_SIZE
      Pieces * SHA1 Hash length, + 25% extra for file names, bencoding overhead, etc.
      See Also:
    • PARALLEL_REQUESTS

      private static final int PARALLEL_REQUESTS
      Extension requests in flight per peer.
      See Also:
    • TYPE_REQUEST

      private static final int TYPE_REQUEST
      Extension message asking for data.
      See Also:
    • TYPE_DATA

      private static final int TYPE_DATA
      Extension message carrying data.
      See Also:
    • TYPE_REJECT

      private static final int TYPE_REJECT
      Extension message refusing a request.
      See Also:
    • HASH_LENGTH

      private static final int HASH_LENGTH
      Length of the piece hashes, in bytes.
      See Also:
    • COMMENTS_FILTER

      private static final byte[] COMMENTS_FILTER
      Filter saying comment requests are not wanted.
  • Constructor Details

    • ExtensionHandler

      ExtensionHandler()
      Constructor for subclasses, each of which handles one BEP 10 extension protocol.
  • Method Details

    • sendExtension

      private static void sendExtension(Peer peer, String type, byte[] payload)
      Send an extension message to a peer, looking up its message code for the given type in the handshake. Silently ignores peers that did not advertise the extension.
      Parameters:
      peer - the peer to send to
      type - the extension type name (TYPE_* constant)
      payload - the bencoded message payload
    • getHandshake

      public static byte[] getHandshake(int metasize, boolean pexAndMetadata, boolean dht, boolean uploadOnly, boolean comment, String clientName)
      Creates a bencoded extension handshake message.
      Parameters:
      metasize - the metadata size in bytes, or -1 if unknown
      pexAndMetadata - true to advertise PEX and metadata extensions
      dht - true to advertise DHT capability
      uploadOnly - true to advertise upload-only mode (BEP 21)
      comment - true to advertise ut_comment extension
      clientName - the "v" value, or null for the default "I2PSnark"
      Returns:
      bencoded outgoing handshake message
    • sendDontHave

      public static void sendDontHave(Peer peer, int piece)
      Send a BEP 54 lt_donthave message to a peer that advertised support: the piece is no longer available. The payload is the raw 4-byte piece index.
      Parameters:
      peer - the peer to send to
      piece - the piece index
    • handleMessage

      public static void handleMessage(Peer peer, PeerListener listener, int id, byte[] bs)
      Handles an incoming extension message from a peer.
      Parameters:
      peer - the peer the message arrived from
      listener - the peer listener for callbacks
      id - the extension message ID
      bs - the raw message bytes
    • handleHandshake

      private static void handleHandshake(Peer peer, PeerListener listener, byte[] bs, Log log)
      Handles an incoming extension handshake message.
      Parameters:
      peer - the peer the message arrived from
      listener - the peer listener for callbacks
      bs - the raw message bytes
      log - the logger instance
    • handleMetadata

      private static void handleMetadata(Peer peer, PeerListener listener, byte[] bs, Log log)
      Handles an incoming metadata extension message (BEP 9).
      Parameters:
      peer - the peer the message arrived from
      listener - the peer listener for callbacks
      bs - the raw message bytes
      log - the logger instance
    • sendRequest

      private static void sendRequest(Peer peer, int piece)
      Sends a metadata chunk request to a peer.
      Parameters:
      peer - the peer the message arrived from
      piece - the chunk number to request
    • sendMessage

      private static void sendMessage(Peer peer, int type, int piece)
      Sends a metadata request or reject message to a peer.
      Parameters:
      peer - the peer the message arrived from
      type - the message type (TYPE_REQUEST or TYPE_REJECT)
      piece - the chunk number
    • sendPiece

      private static void sendPiece(Peer peer, int piece, byte[] data, int totalSize)
      Sends a metadata chunk data message to a peer.
      Parameters:
      peer - the peer the message arrived from
      piece - the chunk number
      data - the chunk data bytes
      totalSize - the total metadata size
    • handlePEX

      private static void handlePEX(Peer peer, PeerListener listener, byte[] bs, Log log)
      Handles an incoming PEX message. Uses the "added" key as a single string of concatenated 32-byte peer hashes. added.f and dropped are unsupported.
      Parameters:
      peer - the peer the message arrived from
      listener - the peer listener for callbacks
      bs - the raw message bytes
      log - the logger instance
    • handleDHT

      private static void handleDHT(Peer peer, PeerListener listener, byte[] bs, Log log)
      Handles an incoming DHT port message.
      Parameters:
      peer - the peer the message arrived from
      listener - the peer listener for callbacks
      bs - the raw message bytes
      log - the logger instance
    • sendPEX

      public static void sendPEX(Peer peer, List<Peer> pList)
      Sends a PEX message with the given peer list. added.f and dropped are unsupported.
      Parameters:
      peer - the peer the message arrived from
      pList - non-null list of peers to share
    • sendDHT

      public static void sendDHT(Peer peer, int qport, int rport)
      Sends DHT port numbers to a peer.
      Parameters:
      peer - the peer the message arrived from
      qport - the query port
      rport - the response port
    • handleComment

      private static void handleComment(Peer peer, PeerListener listener, byte[] bs, Log log)
      Handles an incoming comment request or response message.
      Parameters:
      peer - the peer the message arrived from
      listener - the peer listener for callbacks
      bs - the raw message bytes
      log - the logger instance
    • sendCommentReq

      public static void sendCommentReq(Peer peer, int num)
      Sends a comment request to a peer.
      Parameters:
      peer - the peer the message arrived from
      num - the maximum number of comments to request
    • locked_sendComments

      public static void locked_sendComments(Peer peer, int num, CommentSet comments)
      Sends comments to a peer. Caller must synchronize on comments.
      Parameters:
      peer - the peer the message arrived from
      num - the maximum number of comments to send
      comments - non-null set of comments to send