2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2017-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
#include <algorithm>
|
2022-03-10 05:51:14 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2022-10-01 14:12:49 +00:00
|
|
|
#include <chrono>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-04-01 23:00:51 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.h"
|
2022-07-11 23:29:48 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
#include "bitfield.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "clients.h"
|
2022-07-15 00:54:10 +00:00
|
|
|
#include "crypto-utils.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "handshake.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-io.h"
|
2022-10-01 14:12:49 +00:00
|
|
|
#include "timer.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2022-10-19 16:42:08 +00:00
|
|
|
#include "tr-buffer.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
#define tr_logAddTraceHand(handshake, msg) tr_logAddTrace(msg, (handshake)->peer_io_->display_name())
|
2022-05-26 17:17:03 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
using namespace std::literals;
|
2022-12-13 17:59:21 +00:00
|
|
|
using DH = tr_message_stream_encryption::DH;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
bool tr_handshake::build_handshake_message(tr_peerIo* io, uint8_t* buf) const
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = io->torrent_hash();
|
2022-12-13 17:59:21 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "build_handshake_message requires an info_hash");
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
auto const info = mediator_->torrent(info_hash);
|
2022-07-11 23:29:48 +00:00
|
|
|
if (!info)
|
2013-08-05 04:39:43 +00:00
|
|
|
{
|
2022-07-11 23:29:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
auto flags = tr_bitfield{ HandshakeFlagsBits };
|
|
|
|
flags.set(LtepFlag);
|
|
|
|
flags.set(FextFlag);
|
2022-12-13 17:59:21 +00:00
|
|
|
if (mediator_->allows_dht())
|
2022-07-11 23:29:48 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
flags.set(DhtFlag);
|
2013-08-05 04:39:43 +00:00
|
|
|
}
|
2022-12-14 01:58:39 +00:00
|
|
|
auto const flag_bytes = flags.raw();
|
2013-08-05 04:39:43 +00:00
|
|
|
|
2022-12-18 17:18:25 +00:00
|
|
|
[[maybe_unused]] auto* walk = buf;
|
2022-12-14 01:58:39 +00:00
|
|
|
walk = std::copy_n(reinterpret_cast<uint8_t const*>(std::data(HandshakeName)), std::size(HandshakeName), walk);
|
|
|
|
walk = std::copy(std::begin(flag_bytes), std::end(flag_bytes), walk);
|
2022-11-12 15:53:09 +00:00
|
|
|
walk = std::copy_n(reinterpret_cast<char const*>(std::data(info_hash)), std::size(info_hash), walk);
|
2022-12-14 01:58:39 +00:00
|
|
|
walk = std::copy(std::begin(info->client_peer_id), std::end(info->client_peer_id), walk);
|
|
|
|
TR_ASSERT(walk - buf == HandshakeSize);
|
2022-07-11 23:29:48 +00:00
|
|
|
|
|
|
|
return true;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_handshake::ParseResult tr_handshake::parse_handshake(tr_peerIo* peer_io)
|
2007-09-22 03:37:37 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("payload: need {}, got {}", HandshakeSize, peer_io->read_buffer_size()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < HandshakeSize)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-24 16:17:02 +00:00
|
|
|
return ParseResult::EncryptionWrong;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* confirm the protocol */
|
2022-09-09 18:12:47 +00:00
|
|
|
auto name = decltype(HandshakeName){};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(name), std::size(name));
|
2022-09-09 18:12:47 +00:00
|
|
|
if (name != HandshakeName)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-24 16:17:02 +00:00
|
|
|
return ParseResult::EncryptionWrong;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read the reserved bytes */
|
2022-12-14 01:58:39 +00:00
|
|
|
auto flags = tr_bitfield{ HandshakeFlagsBits };
|
|
|
|
auto reserved = std::array<uint8_t, HandshakeFlagsBytes>{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(reserved), std::size(reserved));
|
2022-12-14 01:58:39 +00:00
|
|
|
flags.setRaw(std::data(reserved), std::size(reserved));
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_supports_dht(flags.test(DhtFlag));
|
|
|
|
peer_io->set_supports_ltep(flags.test(LtepFlag));
|
|
|
|
peer_io->set_supports_fext(flags.test(FextFlag));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-12 15:53:09 +00:00
|
|
|
// torrent hash
|
|
|
|
auto info_hash = tr_sha1_digest_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(info_hash), std::size(info_hash));
|
|
|
|
if (info_hash == tr_sha1_digest_t{} || info_hash != peer_io->torrent_hash())
|
2008-06-05 16:25:22 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer returned the wrong hash. wtf?");
|
2022-11-24 16:17:02 +00:00
|
|
|
return ParseResult::BadTorrent;
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
// peer_id
|
|
|
|
auto peer_id = tr_peer_id_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_id), std::size(peer_id));
|
2022-12-13 17:59:21 +00:00
|
|
|
set_peer_id(peer_id);
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* peer id */
|
2022-03-14 04:43:35 +00:00
|
|
|
auto const peer_id_sv = std::string_view{ std::data(peer_id), std::size(peer_id) };
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("peer-id is '{}'", peer_id_sv));
|
2008-12-10 05:20:28 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
if (auto const info = mediator_->torrent(info_hash); info && info->client_peer_id == peer_id)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "streuth! we've connected to ourselves.");
|
2022-11-24 16:17:02 +00:00
|
|
|
return ParseResult::PeerIsSelf;
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 16:17:02 +00:00
|
|
|
return ParseResult::Ok;
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Outgoing Connections
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// 1 A->B: our public key (Ya) and some padding (PadA)
|
2022-12-13 17:59:21 +00:00
|
|
|
void tr_handshake::send_ya(tr_peerIo* io)
|
2022-07-15 00:54:10 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
send_public_key_and_pad<PadaMaxlen>(io);
|
|
|
|
set_state(tr_handshake::State::AwaitingYb);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
[[nodiscard]] uint32_t tr_handshake::get_crypto_select(tr_encryption_mode encryption_mode, uint32_t crypto_provide) noexcept
|
2007-11-08 21:20:08 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
auto choices = std::array<uint32_t, 2>{};
|
|
|
|
int n_choices = 0;
|
2007-11-08 21:20:08 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
switch (encryption_mode)
|
2007-11-08 21:20:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ENCRYPTION_PREFERRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
|
|
|
choices[n_choices++] = CryptoProvidePlaintext;
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_CLEAR_PREFERRED:
|
2022-09-07 16:04:28 +00:00
|
|
|
choices[n_choices++] = CryptoProvidePlaintext;
|
|
|
|
choices[n_choices++] = CryptoProvideCrypto;
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2007-11-08 21:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
for (auto const& choice : choices)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
if ((crypto_provide & choice) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
return choice;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-08 21:20:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2007-09-22 00:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_yb(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(HandshakeName))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
bool const is_encrypted = !peer_io->read_buffer_starts_with(HandshakeName);
|
2022-07-15 00:54:10 +00:00
|
|
|
auto peer_public_key = DH::key_bigend_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
if (is_encrypted && (peer_io->read_buffer_size() < std::size(peer_public_key)))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-09 18:12:47 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, is_encrypted ? "got an encrypted handshake" : "got a plain handshake");
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (!is_encrypted)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingHandshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_have_read_anything_from_peer(true);
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// get the peer's public key
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_public_key), std::size(peer_public_key));
|
2022-12-13 17:59:21 +00:00
|
|
|
dh_.setPeerPublicKey(peer_public_key);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* now send these: HASH('req1', S), HASH('req2', SKEY) xor HASH('req3', S),
|
|
|
|
* ENCRYPT(VC, crypto_provide, len(PadC), PadC, len(IA)), ENCRYPT(IA) */
|
2022-10-19 16:42:08 +00:00
|
|
|
auto outbuf = libtransmission::Buffer{};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* HASH('req1', S) */
|
2022-12-13 17:59:21 +00:00
|
|
|
outbuf.add(tr_sha1::digest("req1"sv, dh_.secret()));
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = peer_io->torrent_hash();
|
2022-11-12 15:53:09 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "readYb requires an info_hash");
|
2022-06-01 16:56:59 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
/* HASH('req2', SKEY) xor HASH('req3', S) */
|
|
|
|
{
|
2022-11-12 15:53:09 +00:00
|
|
|
auto const req2 = tr_sha1::digest("req2"sv, info_hash);
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const req3 = tr_sha1::digest("req3"sv, dh_.secret());
|
2022-10-19 16:42:08 +00:00
|
|
|
auto x_or = tr_sha1_digest_t{};
|
|
|
|
for (size_t i = 0, n = std::size(x_or); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
x_or[i] = req2[i] ^ req3[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(x_or);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT(VC, crypto_provide, len(PadC), PadC
|
2017-04-19 12:04:45 +00:00
|
|
|
* PadC is reserved for future extensions to the handshake...
|
|
|
|
* standard practice at this time is for it to be zero-length */
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->encrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(VC);
|
2023-01-28 02:12:09 +00:00
|
|
|
outbuf.add_uint32(crypto_provide());
|
|
|
|
outbuf.add_uint16(0);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT len(IA)), ENCRYPT(IA) */
|
2022-12-13 17:59:21 +00:00
|
|
|
if (auto msg = std::array<uint8_t, HandshakeSize>{}; build_handshake_message(peer_io, std::data(msg)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
outbuf.add_uint16(std::size(msg));
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(msg);
|
2022-12-13 17:59:21 +00:00
|
|
|
have_sent_bittorrent_handshake_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-07-15 00:54:10 +00:00
|
|
|
else
|
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2022-07-15 00:54:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send it */
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingVc);
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2022-07-16 20:25:44 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// MSE spec: "Since the length of [PadB is] unknown,
|
|
|
|
// A will be able to resynchronize on ENCRYPT(VC)"
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_vc(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const info_hash = peer_io->torrent_hash();
|
2022-11-12 15:53:09 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "readVC requires an info_hash");
|
|
|
|
|
2022-07-18 18:00:26 +00:00
|
|
|
// find the end of PadB by looking for `ENCRYPT(VC)`
|
|
|
|
auto needle = VC;
|
|
|
|
auto filter = tr_message_stream_encryption::Filter{};
|
2022-12-13 17:59:21 +00:00
|
|
|
filter.encryptInit(true, dh_, info_hash);
|
2022-07-18 18:00:26 +00:00
|
|
|
filter.encrypt(std::size(needle), std::data(needle));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0; i < PadbMaxlen; ++i)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(needle))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "not enough bytes... returning read_more");
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_LATER;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_starts_with(needle))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "got it!");
|
2022-07-18 18:00:26 +00:00
|
|
|
// We already know it's a match; now we just need to
|
|
|
|
// consume it from the read buffer.
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->decrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
|
|
|
peer_io->read_bytes(std::data(needle), std::size(needle));
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingCryptoSelect);
|
2022-07-18 18:00:26 +00:00
|
|
|
return READ_NOW;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_buffer_drain(1);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "couldn't find ENCRYPT(VC)");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_crypto_select(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (static size_t constexpr NeedLen = sizeof(uint32_t) + sizeof(uint16_t); peer_io->read_buffer_size() < NeedLen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto crypto_select = uint32_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_uint32(&crypto_select);
|
2022-12-13 17:59:21 +00:00
|
|
|
crypto_select_ = crypto_select;
|
|
|
|
tr_logAddTraceHand(this, fmt::format("crypto select is {}", crypto_select));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
if ((crypto_select & crypto_provide()) == 0)
|
2007-09-22 00:53:11 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer selected an encryption option we didn't offer");
|
|
|
|
return done(false);
|
2007-09-22 00:53:11 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
uint16_t pad_d_len = 0;
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_uint16(&pad_d_len);
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("pad_d_len is {}", pad_d_len));
|
2008-01-14 16:17:02 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pad_d_len > 512)
|
2008-01-14 16:17:02 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "encryption handshake: pad_d_len is too long");
|
|
|
|
return done(false);
|
2008-01-14 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
pad_d_len_ = pad_d_len;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingPadD);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_pad_d(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
size_t const needlen = pad_d_len_;
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("pad d: need {}, got {}", needlen, peer_io->read_buffer_size()));
|
|
|
|
if (peer_io->read_buffer_size() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_buffer_drain(needlen);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(tr_handshake::State::AwaitingHandshake);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Incoming Connections
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_handshake(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
static auto constexpr Needlen = IncomingHandshakeLen;
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("payload: need {}, got {}", Needlen, peer_io->read_buffer_size()));
|
|
|
|
if (peer_io->read_buffer_size() < Needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_have_read_anything_from_peer(true);
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_starts_with(HandshakeName)) // unencrypted
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
if (encryption_mode_ == TR_ENCRYPTION_REQUIRED)
|
2007-09-26 03:52:30 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer is unencrypted, and we're disallowing that");
|
|
|
|
return done(false);
|
2007-09-26 03:52:30 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2022-09-09 18:12:47 +00:00
|
|
|
else // either encrypted or corrupt
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
if (is_incoming())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "I think peer is sending us an encrypted handshake...");
|
|
|
|
set_state(tr_handshake::State::AwaitingYa);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:12:47 +00:00
|
|
|
auto name = decltype(HandshakeName){};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(name), std::size(name));
|
2022-09-09 18:12:47 +00:00
|
|
|
if (name != HandshakeName)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
// reserved bytes / flags
|
|
|
|
auto reserved = std::array<uint8_t, HandshakeFlagsBytes>{};
|
|
|
|
auto flags = tr_bitfield{ HandshakeFlagsBits };
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(reserved), std::size(reserved));
|
2022-12-14 01:58:39 +00:00
|
|
|
flags.setRaw(std::data(reserved), std::size(reserved));
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_supports_dht(flags.test(DhtFlag));
|
|
|
|
peer_io->set_supports_ltep(flags.test(LtepFlag));
|
|
|
|
peer_io->set_supports_fext(flags.test(FextFlag));
|
2008-01-01 00:20:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* torrent hash */
|
2021-12-21 22:14:15 +00:00
|
|
|
auto hash = tr_sha1_digest_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(hash), std::size(hash));
|
2008-12-02 17:10:54 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
if (is_incoming())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
if (!mediator_->torrent(hash))
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer is trying to connect to us for a torrent we don't have.");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_torrent_hash(hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2022-11-12 15:53:09 +00:00
|
|
|
else // outgoing
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->torrent_hash() != hash)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer returned the wrong hash. wtf?");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// If it's an incoming message, we need to send a response handshake
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
if (!have_sent_bittorrent_handshake_)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
if (!build_handshake_message(peer_io, std::data(msg)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->write_bytes(std::data(msg), std::size(msg), false);
|
2022-12-13 17:59:21 +00:00
|
|
|
have_sent_bittorrent_handshake_ = true;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingPeerId);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2008-01-01 00:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_peer_id(tr_peerIo* peer_io)
|
2008-01-01 00:20:07 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
// read the peer_id
|
|
|
|
auto peer_id = tr_peer_id_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(peer_id))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_id), std::size(peer_id));
|
2022-12-13 17:59:21 +00:00
|
|
|
set_peer_id(peer_id);
|
2008-01-01 00:20:07 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
auto client = std::array<char, 128>{};
|
|
|
|
tr_clientForId(std::data(client), std::size(client), peer_id);
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("peer-id is '{}' ... isIncoming is {}", std::data(client), is_incoming()));
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
// if we've somehow connected to ourselves, don't keep the connection
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const info_hash = peer_io_->torrent_hash();
|
2022-12-14 01:58:39 +00:00
|
|
|
auto const info = mediator_->torrent(info_hash);
|
2022-07-11 23:29:48 +00:00
|
|
|
auto const connected_to_self = info && info->client_peer_id == peer_id;
|
2013-02-02 05:31:43 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(!connected_to_self);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_ya(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-15 00:54:10 +00:00
|
|
|
auto peer_public_key = DH::key_bigend_t{};
|
|
|
|
tr_logAddTraceHand(
|
2022-12-13 17:59:21 +00:00
|
|
|
this,
|
2022-12-16 07:23:12 +00:00
|
|
|
fmt::format("in readYa... need {}, have {}", std::size(peer_public_key), peer_io->read_buffer_size()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(peer_public_key))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the incoming peer's public key */
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(peer_public_key), std::size(peer_public_key));
|
2022-12-13 17:59:21 +00:00
|
|
|
dh_.setPeerPublicKey(peer_public_key);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// send our public key to the peer
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "sending B->A: Diffie Hellman Yb, PadB");
|
|
|
|
send_public_key_and_pad<PadbMaxlen>(peer_io);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingPadA);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_pad_a(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-07-18 18:00:26 +00:00
|
|
|
// find the end of PadA by looking for HASH('req1', S)
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const needle = tr_sha1::digest("req1"sv, dh_.secret());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0; i < PadaMaxlen; ++i)
|
2021-10-14 19:26:38 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < std::size(needle))
|
2022-07-18 18:00:26 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "not enough bytes... returning read_more");
|
2022-07-18 18:00:26 +00:00
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_starts_with(needle))
|
2022-07-18 18:00:26 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "found it... looking setting to awaiting_crypto_provide");
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_buffer_drain(std::size(needle));
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingCryptoProvide);
|
2022-07-18 18:00:26 +00:00
|
|
|
return READ_NOW;
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_buffer_drain(1U);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "couldn't find HASH('req', S)");
|
|
|
|
return done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_crypto_provide(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
/* HASH('req2', SKEY) xor HASH('req3', S), ENCRYPT(VC, crypto_provide, len(PadC)) */
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
uint16_t padc_len = 0;
|
|
|
|
uint32_t crypto_provide = 0;
|
2022-09-05 13:55:17 +00:00
|
|
|
auto obfuscated_hash = tr_sha1_digest_t{};
|
|
|
|
size_t const needlen = sizeof(obfuscated_hash) + /* HASH('req2', SKEY) xor HASH('req3', S) */
|
2022-07-15 00:54:10 +00:00
|
|
|
std::size(VC) + sizeof(crypto_provide) + sizeof(padc_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* This next piece is HASH('req2', SKEY) xor HASH('req3', S) ...
|
2022-04-21 14:28:38 +00:00
|
|
|
* we can get the first half of that (the obfuscatedTorrentHash)
|
2017-04-19 12:04:45 +00:00
|
|
|
* by building the latter and xor'ing it with what the peer sent us */
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "reading obfuscated torrent hash...");
|
2021-12-21 22:14:15 +00:00
|
|
|
auto req2 = tr_sha1_digest_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(req2), std::size(req2));
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const req3 = tr_sha1::digest("req3"sv, dh_.secret());
|
2021-12-21 22:14:15 +00:00
|
|
|
for (size_t i = 0; i < std::size(obfuscated_hash); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
obfuscated_hash[i] = req2[i] ^ req3[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
if (auto const info = mediator_->torrent_from_obfuscated(obfuscated_hash); info)
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2022-07-11 23:29:48 +00:00
|
|
|
bool const client_is_seed = info->is_done;
|
2022-12-13 17:59:21 +00:00
|
|
|
bool const peer_is_seed = mediator_->is_peer_known_seed(info->id, peer_io->address());
|
|
|
|
tr_logAddTraceHand(this, fmt::format("got INCOMING connection's encrypted handshake for torrent [{}]", info->id));
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->set_torrent_hash(info->info_hash);
|
2009-01-07 22:06:40 +00:00
|
|
|
|
2022-07-11 23:29:48 +00:00
|
|
|
if (client_is_seed && peer_is_seed)
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "another seed tried to reconnect to us!");
|
|
|
|
return done(false);
|
2008-04-17 03:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-04-17 03:48:56 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "can't find that torrent...");
|
|
|
|
return done(false);
|
2007-09-26 03:52:30 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* next part: ENCRYPT(VC, crypto_provide, len(PadC), */
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = peer_io->torrent_hash();
|
2022-11-12 15:53:09 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "readCryptoProvide requires an info_hash");
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->decrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
auto vc_in = vc_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(vc_in), std::size(vc_in));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_uint32(&crypto_provide);
|
2022-12-13 17:59:21 +00:00
|
|
|
crypto_provide_ = crypto_provide;
|
|
|
|
tr_logAddTraceHand(this, fmt::format("crypto_provide is {}", crypto_provide));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_uint16(&padc_len);
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("padc is {}", padc_len));
|
2022-09-07 16:04:28 +00:00
|
|
|
if (padc_len > PadcMaxlen)
|
2022-07-28 03:17:05 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer's PadC is too big");
|
|
|
|
return done(false);
|
2022-07-28 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
pad_c_len_ = padc_len;
|
|
|
|
set_state(State::AwaitingPadC);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_pad_c(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
if (auto const needlen = pad_c_len_ + sizeof(uint16_t); peer_io->read_buffer_size() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-07-28 03:17:05 +00:00
|
|
|
// read the throwaway padc
|
2022-09-07 16:04:28 +00:00
|
|
|
auto pad_c = std::array<char, PadcMaxlen>{};
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_bytes(std::data(pad_c), pad_c_len_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* read ia_len */
|
2022-08-29 18:33:08 +00:00
|
|
|
uint16_t ia_len = 0;
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->read_uint16(&ia_len);
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("ia_len is {}", ia_len));
|
|
|
|
ia_len_ = ia_len;
|
|
|
|
set_state(State::AwaitingIa);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_ia(tr_peerIo* peer_io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
size_t const needlen = ia_len_;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("reading IA... have {}, need {}", peer_io->read_buffer_size(), needlen));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (peer_io->read_buffer_size() < needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload Stream)
|
2007-09-26 03:52:30 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const& info_hash = peer_io->torrent_hash();
|
2022-11-12 15:53:09 +00:00
|
|
|
TR_ASSERT_MSG(info_hash != tr_sha1_digest_t{}, "readIA requires an info_hash");
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io->encrypt_init(peer_io->is_incoming(), dh_, info_hash);
|
2022-10-19 16:42:08 +00:00
|
|
|
auto outbuf = libtransmission::Buffer{};
|
2007-09-26 03:52:30 +00:00
|
|
|
|
2022-07-15 00:54:10 +00:00
|
|
|
// send VC
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "sending vc");
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(VC);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send crypto_select */
|
2022-12-14 01:58:39 +00:00
|
|
|
uint32_t const crypto_select = get_crypto_select(encryption_mode_, crypto_provide_);
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (crypto_select != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("selecting crypto mode '{}'", crypto_select));
|
2023-01-28 02:12:09 +00:00
|
|
|
outbuf.add_uint32(crypto_select);
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "peer didn't offer an encryption mode we like.");
|
|
|
|
return done(false);
|
2007-09-22 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "sending pad d");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* ENCRYPT(VC, crypto_provide, len(PadD), PadD
|
2017-04-19 12:04:45 +00:00
|
|
|
* PadD is reserved for future extensions to the handshake...
|
|
|
|
* standard practice at this time is for it to be zero-length */
|
2023-01-28 02:12:09 +00:00
|
|
|
outbuf.add_uint16(0);
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* maybe de-encrypt our connection */
|
2022-09-07 16:04:28 +00:00
|
|
|
if (crypto_select == CryptoProvidePlaintext)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
|
|
|
TR_ASSERT(std::empty(outbuf));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-30 21:45:40 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(this, "sending handshake");
|
2007-09-22 03:37:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* send our handshake */
|
2022-12-13 17:59:21 +00:00
|
|
|
if (auto msg = std::array<uint8_t, HandshakeSize>{}; build_handshake_message(peer_io, std::data(msg)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.add(msg);
|
2022-12-13 17:59:21 +00:00
|
|
|
have_sent_bittorrent_handshake_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-07-15 00:54:10 +00:00
|
|
|
else
|
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2022-07-15 00:54:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* send it out */
|
2022-10-19 16:42:08 +00:00
|
|
|
peer_io->write(outbuf, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* now await the handshake */
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingPayloadStream);
|
2017-04-19 12:04:45 +00:00
|
|
|
return READ_NOW;
|
2008-10-29 20:18:56 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::read_payload_stream(tr_peerIo* peer_io)
|
2008-10-29 20:18:56 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
static auto constexpr Needlen = HandshakeSize;
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceHand(this, fmt::format("reading payload stream... have {}, need {}", peer_io->read_buffer_size(), Needlen));
|
|
|
|
if (peer_io->read_buffer_size() < Needlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return READ_LATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the handshake ... */
|
2022-12-13 17:59:21 +00:00
|
|
|
auto const i = parse_handshake(peer_io);
|
|
|
|
tr_logAddTraceHand(this, fmt::format("parseHandshake returned {}", static_cast<int>(i)));
|
2022-11-24 16:17:02 +00:00
|
|
|
if (i != ParseResult::Ok)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-10-29 20:18:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* we've completed the BT handshake... pass the work on to peer-msgs */
|
2022-12-13 17:59:21 +00:00
|
|
|
return done(true);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
ReadState tr_handshake::can_read(tr_peerIo* peer_io, void* vhandshake, size_t* piece)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handshake = static_cast<tr_handshake*>(vhandshake);
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
bool ready_for_more = true;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* no piece data in handshake */
|
|
|
|
*piece = 0;
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
tr_logAddTraceHand(handshake, fmt::format("handling canRead; state is [{}]", handshake->state_string()));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-29 22:43:25 +00:00
|
|
|
ReadState ret = READ_NOW;
|
2022-09-07 16:04:28 +00:00
|
|
|
while (ready_for_more)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
switch (handshake->state())
|
2008-10-29 19:30:17 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingHandshake:
|
|
|
|
ret = handshake->read_handshake(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPeerId:
|
|
|
|
ret = handshake->read_peer_id(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingYa:
|
|
|
|
ret = handshake->read_ya(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPadA:
|
|
|
|
ret = handshake->read_pad_a(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingCryptoProvide:
|
|
|
|
ret = handshake->read_crypto_provide(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPadC:
|
|
|
|
ret = handshake->read_pad_c(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingIa:
|
|
|
|
ret = handshake->read_ia(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPayloadStream:
|
|
|
|
ret = handshake->read_payload_stream(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-10-29 20:18:56 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingYb:
|
|
|
|
ret = handshake->read_yb(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingVc:
|
|
|
|
ret = handshake->read_vc(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingCryptoSelect:
|
|
|
|
ret = handshake->read_crypto_select(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
case State::AwaitingPadD:
|
|
|
|
ret = handshake->read_pad_d(peer_io);
|
2012-12-27 22:03:58 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2021-09-15 02:23:53 +00:00
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2022-12-13 17:59:21 +00:00
|
|
|
TR_ASSERT_MSG(
|
|
|
|
false,
|
|
|
|
fmt::format(FMT_STRING("unhandled handshake state {:d}"), static_cast<int>(handshake->state())));
|
2021-09-15 02:23:53 +00:00
|
|
|
#else
|
|
|
|
ret = READ_ERR;
|
|
|
|
break;
|
|
|
|
#endif
|
2008-10-29 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret != READ_NOW)
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
ready_for_more = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-12-13 17:59:21 +00:00
|
|
|
else if (handshake->is_state(State::AwaitingPadC))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
ready_for_more = peer_io->read_buffer_size() >= handshake->pad_c_len_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-12-13 17:59:21 +00:00
|
|
|
else if (handshake->is_state(State::AwaitingPadD))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
ready_for_more = peer_io->read_buffer_size() >= handshake->pad_d_len_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-12-13 17:59:21 +00:00
|
|
|
else if (handshake->is_state(State::AwaitingIa))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
ready_for_more = peer_io->read_buffer_size() >= handshake->ia_len_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_handshake::on_error(tr_peerIo* io, tr_error const& error, void* vhandshake)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handshake = static_cast<tr_handshake*>(vhandshake);
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (io->is_utp() && !io->is_incoming() && handshake->is_state(State::AwaitingYb))
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-10-11 15:39:41 +00:00
|
|
|
// the peer probably doesn't speak µTP.
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const info_hash = io->torrent_hash();
|
2022-12-14 01:58:39 +00:00
|
|
|
auto const info = handshake->mediator_->torrent(info_hash);
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-10-11 15:39:41 +00:00
|
|
|
/* Don't mark a peer as non-µTP unless it's really a connect failure. */
|
2022-12-16 07:23:12 +00:00
|
|
|
if ((error.code == ETIMEDOUT || error.code == ECONNREFUSED) && info)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-14 01:58:39 +00:00
|
|
|
handshake->mediator_->set_utp_failed(info_hash, io->address());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-27 22:03:58 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (handshake->mediator_->allows_tcp() && io->reconnect())
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2022-12-13 17:59:21 +00:00
|
|
|
handshake->build_handshake_message(io, std::data(msg));
|
|
|
|
handshake->have_sent_bittorrent_handshake_ = true;
|
|
|
|
handshake->set_state(State::AwaitingHandshake);
|
2022-12-16 07:23:12 +00:00
|
|
|
io->write_bytes(std::data(msg), std::size(msg), false);
|
2023-02-18 16:03:59 +00:00
|
|
|
return;
|
2011-02-18 00:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if the error happened while we were sending a public key, we might
|
|
|
|
* have encountered a peer that doesn't do encryption... reconnect and
|
|
|
|
* try a plaintext handshake */
|
2022-12-13 17:59:21 +00:00
|
|
|
if ((handshake->is_state(State::AwaitingYb) || handshake->is_state(State::AwaitingVc)) &&
|
2022-12-16 07:23:12 +00:00
|
|
|
handshake->encryption_mode_ != TR_ENCRYPTION_REQUIRED && handshake->mediator_->allows_tcp() && io->reconnect())
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceHand(handshake, "handshake failed, trying plaintext...");
|
2022-12-13 17:59:21 +00:00
|
|
|
handshake->build_handshake_message(io, std::data(msg));
|
|
|
|
handshake->have_sent_bittorrent_handshake_ = true;
|
|
|
|
handshake->set_state(State::AwaitingHandshake);
|
2022-12-16 07:23:12 +00:00
|
|
|
io->write_bytes(std::data(msg), std::size(msg), false);
|
2023-02-18 16:03:59 +00:00
|
|
|
return;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2023-02-18 16:03:59 +00:00
|
|
|
|
|
|
|
tr_logAddTraceHand(handshake, fmt::format("handshake socket err: {:s} ({:d})", error.message, error.code));
|
|
|
|
handshake->done(false);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
bool tr_handshake::fire_done(bool is_connected)
|
|
|
|
{
|
2022-12-19 22:49:26 +00:00
|
|
|
maybe_recycle_dh();
|
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
if (!on_done_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handshake could get destroyed inside on_done,
|
|
|
|
// so handle all our housekeeping *before* calling it
|
|
|
|
|
|
|
|
auto cb = DoneFunc{};
|
|
|
|
std::swap(cb, on_done_);
|
|
|
|
|
|
|
|
auto peer_io = std::shared_ptr<tr_peerIo>{};
|
|
|
|
std::swap(peer_io, peer_io_);
|
|
|
|
|
|
|
|
bool const success = (cb)(Result{ std::move(peer_io), peer_id_, have_read_anything_from_peer_, is_connected });
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view tr_handshake::state_string(State state) noexcept
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case State::AwaitingHandshake:
|
|
|
|
return "awaiting handshake";
|
|
|
|
case State::AwaitingPeerId:
|
|
|
|
return "awaiting peer id";
|
|
|
|
case State::AwaitingYa:
|
|
|
|
return "awaiting ya";
|
|
|
|
case State::AwaitingPadA:
|
|
|
|
return "awaiting pad a";
|
|
|
|
case State::AwaitingCryptoProvide:
|
|
|
|
return "awaiting crypto provide";
|
|
|
|
case State::AwaitingPadC:
|
|
|
|
return "awaiting pad c";
|
|
|
|
case State::AwaitingIa:
|
|
|
|
return "awaiting ia";
|
|
|
|
case State::AwaitingPayloadStream:
|
|
|
|
return "awaiting payload stream";
|
|
|
|
|
|
|
|
// outgoing
|
|
|
|
case State::AwaitingYb:
|
|
|
|
return "awaiting yb";
|
|
|
|
case State::AwaitingVc:
|
|
|
|
return "awaiting vc";
|
|
|
|
case State::AwaitingCryptoSelect:
|
|
|
|
return "awaiting crypto select";
|
|
|
|
case State::AwaitingPadD:
|
|
|
|
return "awaiting pad d";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown state";
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t tr_handshake::crypto_provide() const noexcept
|
|
|
|
{
|
|
|
|
auto provide = uint32_t{};
|
|
|
|
|
|
|
|
switch (encryption_mode_)
|
|
|
|
{
|
|
|
|
case TR_ENCRYPTION_REQUIRED:
|
|
|
|
case TR_ENCRYPTION_PREFERRED:
|
|
|
|
provide |= CryptoProvideCrypto;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_CLEAR_PREFERRED:
|
|
|
|
provide |= CryptoProvideCrypto | CryptoProvidePlaintext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return provide;
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-14 01:58:39 +00:00
|
|
|
tr_handshake::tr_handshake(Mediator* mediator, std::shared_ptr<tr_peerIo> peer_io, tr_encryption_mode mode, DoneFunc on_done)
|
2022-12-19 22:49:26 +00:00
|
|
|
: dh_{ tr_handshake::get_dh(mediator) }
|
2022-12-14 01:58:39 +00:00
|
|
|
, on_done_{ std::move(on_done) }
|
2022-12-13 17:59:21 +00:00
|
|
|
, peer_io_{ std::move(peer_io) }
|
|
|
|
, timeout_timer_{ mediator->timer_maker().create([this]() { fire_done(false); }) }
|
|
|
|
, mediator_{ mediator }
|
2022-12-14 01:58:39 +00:00
|
|
|
, encryption_mode_{ mode }
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
timeout_timer_->startSingleShot(HandshakeTimeoutSec);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io_->set_callbacks(&tr_handshake::can_read, nullptr, &tr_handshake::on_error, this);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
if (is_incoming())
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
set_state(State::AwaitingHandshake);
|
2012-12-27 22:03:58 +00:00
|
|
|
}
|
2022-12-13 17:59:21 +00:00
|
|
|
else if (encryption_mode_ != TR_CLEAR_PREFERRED)
|
2012-12-27 22:03:58 +00:00
|
|
|
{
|
2022-12-13 17:59:21 +00:00
|
|
|
send_ya(peer_io_.get());
|
2012-12-27 22:03:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto msg = std::array<uint8_t, HandshakeSize>{};
|
2022-12-13 17:59:21 +00:00
|
|
|
build_handshake_message(peer_io_.get(), std::data(msg));
|
2009-05-30 21:45:40 +00:00
|
|
|
|
2022-12-13 17:59:21 +00:00
|
|
|
have_sent_bittorrent_handshake_ = true;
|
|
|
|
set_state(State::AwaitingHandshake);
|
2022-12-16 07:23:12 +00:00
|
|
|
peer_io_->write_bytes(std::data(msg), std::size(msg), false);
|
2007-11-08 21:20:08 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|