/* Copyright (c) 2015, Arvid Norberg All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef SIMULATION_FAKE_PEER_HPP #define SIMULATION_FAKE_PEER_HPP #include #include // for snprintf #include "test.hpp" #include "simulator/simulator.hpp" #include "libtorrent/session.hpp" #include "libtorrent/socket_io.hpp" #include "libtorrent/torrent_handle.hpp" #include "libtorrent/sha1_hash.hpp" #include "libtorrent/torrent_info.hpp" using namespace sim; namespace lt = libtorrent; struct fake_peer { fake_peer(simulation& sim, char const* ip) : m_ios(sim, asio::ip::address::from_string(ip)) , m_acceptor(m_ios) , m_socket(m_ios) , m_info_hash(0) , m_accepted(false) , m_connected(false) , m_disconnected(false) { boost::system::error_code ec; m_acceptor.open(asio::ip::tcp::v4(), ec); TEST_CHECK(!ec); m_acceptor.bind(asio::ip::tcp::endpoint(asio::ip::address_v4::any(), 6881), ec); TEST_CHECK(!ec); m_acceptor.listen(10, ec); TEST_CHECK(!ec); m_acceptor.async_accept(m_socket, [&] (boost::system::error_code const& ec) { using namespace std::placeholders; if (ec) return; asio::async_read(m_socket, asio::mutable_buffers_1(&m_out_buffer[0], 68) , std::bind(&fake_peer::read_handshake, this, _1, _2)); m_accepted = true; }); } void close() { m_acceptor.close(); m_socket.close(); } void connect_to(asio::ip::tcp::endpoint ep, lt::sha1_hash const& ih) { using namespace std::placeholders; m_info_hash = ih; boost::system::error_code ec; m_socket.async_connect(ep, std::bind(&fake_peer::write_handshake , this, _1, ih)); } bool accepted() const { return m_accepted; } bool connected() const { return m_connected; } bool disconnected() const { return m_disconnected; } private: void write_handshake(boost::system::error_code const& ec, lt::sha1_hash ih) { using namespace std::placeholders; asio::ip::tcp::endpoint ep = m_socket.remote_endpoint(); std::printf("fake_peer::connect (%s) -> (%d) %s\n" , lt::print_endpoint(ep).c_str(), ec.value() , ec.message().c_str()); static char const handshake[] = "\x13" "BitTorrent protocol\0\0\0\0\0\0\0\x04" " " // space for info-hash "aaaaaaaaaaaaaaaaaaaa" // peer-id "\0\0\0\x01\x02"; // interested int const len = sizeof(handshake) - 1; memcpy(m_out_buffer, handshake, len); memcpy(&m_out_buffer[28], ih.data(), 20); asio::async_write(m_socket, asio::const_buffers_1(&m_out_buffer[0] , len), [this, ep](boost::system::error_code const& ec, size_t bytes_transferred) { std::printf("fake_peer::write_handshake(%s) -> (%d) %s\n" , lt::print_endpoint(ep).c_str(), ec.value() , ec.message().c_str()); asio::async_read(m_socket, asio::mutable_buffers_1(&m_out_buffer[0], 68) , std::bind(&fake_peer::read_handshake, this, _1, _2)); }); } void read_handshake(lt::error_code const& ec, size_t bytes_transferred) { using namespace std::placeholders; std::printf("fake_peer::read_handshake -> (%d) %s\n" , ec.value(), ec.message().c_str()); if (ec) { m_socket.close(); return; } if (memcmp(&m_out_buffer[0], "\x13" "BitTorrent protocol", 20) != 0) { std::printf(" invalid protocol specifier\n"); m_socket.close(); return; } // if this peer accepted an incoming connection, we don't know what the // info hash is supposed to be if (!m_info_hash.is_all_zeros() && memcmp(&m_out_buffer[28], m_info_hash.data(), 20) != 0) { std::printf(" invalid info hash\n"); m_socket.close(); return; } m_connected = true; // keep reading until we receie EOF, then set m_disconnected = true m_socket.async_read_some(asio::mutable_buffers_1(&m_out_buffer[0] , sizeof(m_out_buffer)) , std::bind(&fake_peer::on_read, this, _1, _2)); } void on_read(lt::error_code const& ec, size_t bytes_transferred) { using namespace std::placeholders; std::printf("fake_peer::on_read -> (%d) %s\n" , ec.value(), ec.message().c_str()); if (ec) { std::printf(" closing\n"); m_disconnected = true; m_socket.close(); return; } m_socket.async_read_some(asio::mutable_buffers_1(&m_out_buffer[0] , sizeof(m_out_buffer)) , std::bind(&fake_peer::on_read, this, _1, _2)); } char m_out_buffer[300]; asio::io_service m_ios; asio::ip::tcp::acceptor m_acceptor; asio::ip::tcp::socket m_socket; lt::sha1_hash m_info_hash; // set to true if this peer received an incoming connection // if this is an outgoing connection, this will always be false bool m_accepted; // set to true if this peer completed a bittorrent handshake bool m_connected; // set to true if this peer has been disconnected by the other end bool m_disconnected; }; inline void add_fake_peer(lt::torrent_handle& h, int const i) { char ep[30]; std::snprintf(ep, sizeof(ep), "60.0.0.%d", i); h.connect_peer(lt::tcp::endpoint( lt::address_v4::from_string(ep), 6881)); } inline void add_fake_peers(lt::torrent_handle& h, int const n = 5) { // add the fake peers for (int i = 0; i < n; ++i) { add_fake_peer(h, i); } } template void check_accepted(std::array& test_peers , std::array expected) { int idx = 0; for (auto p : test_peers) { TEST_EQUAL(p->accepted(), expected[idx]); ++idx; } } template void check_connected(std::array& test_peers , std::array expected) { int idx = 0; for (auto p : test_peers) { TEST_EQUAL(p->connected(), expected[idx]); ++idx; } } template void check_disconnected(std::array& test_peers , std::array expected) { int idx = 0; for (auto p : test_peers) { TEST_EQUAL(p->disconnected(), expected[idx]); ++idx; } } #endif