Exemple #1
0
static void
buildHandshakeMessage( tr_handshake * handshake, uint8_t * buf )
{
    uint8_t          * walk = buf;
    const uint8_t    * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto );
    const tr_torrent * tor = tr_torrentFindFromHash( handshake->session, torrentHash );
    const uint8_t    * peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );

    memcpy( walk, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
    walk += HANDSHAKE_NAME_LEN;
    memset( walk, 0, HANDSHAKE_FLAGS_LEN );
    HANDSHAKE_SET_LTEP( walk );
    HANDSHAKE_SET_FASTEXT( walk );

    /* Note that this doesn't depend on whether the torrent is private.
     * We don't accept DHT peers for a private torrent,
     * but we participate in the DHT regardless. */
    if( tr_dhtEnabled( handshake->session ) )
        HANDSHAKE_SET_DHT( walk );

    walk += HANDSHAKE_FLAGS_LEN;
    memcpy( walk, torrentHash, SHA_DIGEST_LENGTH );
    walk += SHA_DIGEST_LENGTH;
    memcpy( walk, peer_id, PEER_ID_LEN );
    walk += PEER_ID_LEN;

    assert( strlen( ( const char* )peer_id ) == PEER_ID_LEN );
    assert( walk - buf == HANDSHAKE_SIZE );
}
static bool
buildHandshakeMessage (tr_handshake * handshake, uint8_t * buf)
{
  const unsigned char * peer_id = NULL;
  const uint8_t * torrentHash;
  tr_torrent * tor;
  bool success;

  if ((torrentHash = tr_cryptoGetTorrentHash (handshake->crypto)))
    if ((tor = tr_torrentFindFromHash (handshake->session, torrentHash)))
      peer_id = tr_torrentGetPeerId (tor);

  if (peer_id == NULL)
    {
      success = false;
    }
  else
    {
      uint8_t * walk = buf;

      memcpy (walk, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN);
      walk += HANDSHAKE_NAME_LEN;
      memset (walk, 0, HANDSHAKE_FLAGS_LEN);
      HANDSHAKE_SET_LTEP (walk);
      HANDSHAKE_SET_FASTEXT (walk);

      /* Note that this doesn't depend on whether the torrent is private.
       * We don't accept DHT peers for a private torrent,
       * but we participate in the DHT regardless. */
      if (tr_dhtEnabled (handshake->session))
        HANDSHAKE_SET_DHT (walk);

      walk += HANDSHAKE_FLAGS_LEN;
      memcpy (walk, torrentHash, SHA_DIGEST_LENGTH);
      walk += SHA_DIGEST_LENGTH;
      memcpy (walk, peer_id, PEER_ID_LEN);
      walk += PEER_ID_LEN;

      assert (walk - buf == HANDSHAKE_SIZE);
      success = true;
    }

  return success;
}