Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ src_libbitcoin_network_la_SOURCES = \
src/messages/rpc/model.cpp \
src/net/acceptor.cpp \
src/net/connector.cpp \
src/net/connector_socks.cpp \
src/net/deadline.cpp \
src/net/hosts.cpp \
src/net/proxy.cpp \
Expand Down Expand Up @@ -231,6 +232,7 @@ test_libbitcoin_network_test_SOURCES = \
test/messages/rpc/types.cpp \
test/net/acceptor.cpp \
test/net/connector.cpp \
test/net/connector_socks.cpp \
test/net/deadline.cpp \
test/net/hosts.cpp \
test/net/proxy.cpp \
Expand Down Expand Up @@ -460,6 +462,7 @@ include_bitcoin_network_netdir = ${includedir}/bitcoin/network/net
include_bitcoin_network_net_HEADERS = \
include/bitcoin/network/net/acceptor.hpp \
include/bitcoin/network/net/connector.hpp \
include/bitcoin/network/net/connector_socks.hpp \
include/bitcoin/network/net/deadline.hpp \
include/bitcoin/network/net/hosts.hpp \
include/bitcoin/network/net/net.hpp \
Expand Down
2 changes: 2 additions & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/messages/rpc/model.cpp"
"../../src/net/acceptor.cpp"
"../../src/net/connector.cpp"
"../../src/net/connector_socks.cpp"
"../../src/net/deadline.cpp"
"../../src/net/hosts.cpp"
"../../src/net/proxy.cpp"
Expand Down Expand Up @@ -437,6 +438,7 @@ if (with-tests)
"../../test/messages/rpc/types.cpp"
"../../test/net/acceptor.cpp"
"../../test/net/connector.cpp"
"../../test/net/connector_socks.cpp"
"../../test/net/deadline.cpp"
"../../test/net/hosts.cpp"
"../../test/net/proxy.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
<ClCompile Include="..\..\..\..\test\net.cpp" />
<ClCompile Include="..\..\..\..\test\net\acceptor.cpp" />
<ClCompile Include="..\..\..\..\test\net\connector.cpp" />
<ClCompile Include="..\..\..\..\test\net\connector_socks.cpp" />
<ClCompile Include="..\..\..\..\test\net\deadline.cpp" />
<ClCompile Include="..\..\..\..\test\net\hosts.cpp" />
<ClCompile Include="..\..\..\..\test\net\proxy.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@
<ClCompile Include="..\..\..\..\test\net\connector.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\net\connector_socks.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\net\deadline.cpp">
<Filter>src\net</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
<ClCompile Include="..\..\..\..\src\net.cpp" />
<ClCompile Include="..\..\..\..\src\net\acceptor.cpp" />
<ClCompile Include="..\..\..\..\src\net\connector.cpp" />
<ClCompile Include="..\..\..\..\src\net\connector_socks.cpp" />
<ClCompile Include="..\..\..\..\src\net\deadline.cpp" />
<ClCompile Include="..\..\..\..\src\net\hosts.cpp" />
<ClCompile Include="..\..\..\..\src\net\proxy.cpp" />
Expand Down Expand Up @@ -334,6 +335,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\net.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\acceptor.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\connector.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\connector_socks.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\deadline.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\hosts.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\net.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@
<ClCompile Include="..\..\..\..\src\net\connector.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\net\connector_socks.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\net\deadline.cpp">
<Filter>src\net</Filter>
</ClCompile>
Expand Down Expand Up @@ -758,6 +761,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\connector.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\connector_socks.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\deadline.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
#include <bitcoin/network/messages/rpc/enums/version.hpp>
#include <bitcoin/network/net/acceptor.hpp>
#include <bitcoin/network/net/connector.hpp>
#include <bitcoin/network/net/connector_socks.hpp>
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/hosts.hpp>
#include <bitcoin/network/net/net.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/bitcoin/network/net/acceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ class BCT_API acceptor
virtual void accept(socket_handler&& handler) NOEXCEPT;

protected:
/// Start listening on the endpoint.
virtual code start(const asio::endpoint& point) NOEXCEPT;

/// Running in the strand.
bool stranded() const NOEXCEPT;

// These are thread safe.
const size_t maximum_;
asio::io_context& service_;
Expand Down
19 changes: 11 additions & 8 deletions include/bitcoin/network/net/connector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
#include <bitcoin/network/config/config.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/messages/messages.hpp>
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/socket.hpp>
#include <bitcoin/network/settings.hpp>

namespace libbitcoin {
namespace network {
Expand All @@ -38,7 +36,7 @@ namespace network {
/// All public/protected methods must be called from strand.
/// Stop is thread safe and idempotent, may be called multiple times.
class BCT_API connector
: public std::enable_shared_from_this<connector>, public reporter,
: public enable_shared_from_base<connector>, public reporter,
protected tracker<connector>
{
public:
Expand Down Expand Up @@ -82,11 +80,20 @@ class BCT_API connector

protected:
typedef race_speed<two, const code&, const socket::ptr&> racer;
typedef std::shared_ptr<bool> finish_ptr;

/// Try to connect to host:port, starts timer.
virtual void start(const std::string& hostname, uint16_t port,
const config::address& host, socket_handler&& handler) NOEXCEPT;

virtual void handle_connected(const code& ec, const finish_ptr& finish,
socket::ptr socket) NOEXCEPT;
virtual void handle_timer(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;

/// Running in the strand.
bool stranded() NOEXCEPT;

// These are thread safe
const size_t maximum_;
asio::io_context& service_;
Expand All @@ -99,16 +106,12 @@ class BCT_API connector
racer racer_{};

private:
typedef std::shared_ptr<bool> finish_ptr;

void handle_resolve(const boost_code& ec,
const asio::endpoints& range, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
void do_handle_connect(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
void handle_connect(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
void handle_timer(const code& ec, const finish_ptr& finish,
void handle_connect(code ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
};

Expand Down
96 changes: 96 additions & 0 deletions include/bitcoin/network/net/connector_socks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_NET_CONNECTOR_SOCKS_HPP
#define LIBBITCOIN_NETWORK_NET_CONNECTOR_SOCKS_HPP

#include <atomic>
#include <memory>
#include <bitcoin/network/async/async.hpp>
#include <bitcoin/network/config/config.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/net/connector.hpp>
#include <bitcoin/network/net/socket.hpp>

namespace libbitcoin {
namespace network {

/// Not thread safe, virtual.
/// Create outbound socket connections via a socks5 proxy.
/// All public/protected methods must be called from strand.
/// Stop is thread safe and idempotent, may be called multiple times.
class BCT_API connector_socks
: public connector,
protected tracker<connector_socks>
{
public:
typedef std::shared_ptr<connector_socks> ptr;

DELETE_COPY_MOVE_DESTRUCT(connector_socks);

/// Resolves socks5 endpoint and stores address as member for each connect.
connector_socks(const logger& log, asio::strand& strand,
asio::io_context& service, const config::endpoint& socks5_proxy,
const steady_clock::duration& timeout, size_t maximum_request,
std::atomic_bool& suspended) NOEXCEPT;

protected:
void start(const std::string& hostname, uint16_t port,
const config::address& host, socket_handler&& handler) NOEXCEPT override;

/// Connector overrides.
void handle_connected(const code& ec, const finish_ptr& finish,
socket::ptr socket) NOEXCEPT override;
void handle_timer(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT override;

private:
template <size_t Size>
using data_ptr = std::shared_ptr<system::data_array<Size>>;
template <size_t Size>
using data_cptr = std::shared_ptr<const system::data_array<Size>>;

// socks5 handshake
void do_socks(const code& ec, const socket::ptr& socket) NOEXCEPT;
void handle_socks_greeting_write(const code& ec, size_t size,
const socket::ptr& socket, const data_cptr<3>& greeting) NOEXCEPT;
void handle_socks_method_read(const code& ec, size_t size,
const socket::ptr& socket, const data_ptr<2>& response) NOEXCEPT;
void handle_socks_connect_write(const code& ec, size_t size,
const socket::ptr& socket, const system::chunk_ptr& request) NOEXCEPT;
void handle_socks_response_read(const code& ec, size_t size,
const socket::ptr& socket, const data_ptr<4>& response) NOEXCEPT;
void handle_socks_length_read(const code& ec, size_t size,
const socket::ptr& socket, const data_ptr<1>& host_length) NOEXCEPT;
void handle_socks_address_read(const code& ec, size_t size,
const socket::ptr& socket, const system::chunk_ptr& address) NOEXCEPT;
void do_socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT;
void socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT;

// This is protected by strand.
const config::endpoint socks5_;
};

typedef std_vector<connector_socks::ptr> socks_connectors;
typedef std::shared_ptr<socks_connectors> socks_connectors_ptr;

} // namespace network
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/network/net/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <bitcoin/network/net/acceptor.hpp>
#include <bitcoin/network/net/connector.hpp>
#include <bitcoin/network/net/connector_socks.hpp>
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/hosts.hpp>
#include <bitcoin/network/net/proxy.hpp>
Expand Down
67 changes: 43 additions & 24 deletions include/bitcoin/network/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace libbitcoin {
namespace network {

/// The largest p2p payload request when configured for witness blocks.
constexpr uint32_t maximum_request_
constexpr uint32_t maximum_request_default
{
system::possible_narrow_cast<uint32_t>(
messages::peer::heading::maximum_payload(
Expand All @@ -41,6 +41,25 @@ constexpr uint32_t maximum_request_
/// Common network configuration settings, properties not thread safe.
struct BCT_API settings
{
struct socks5_client
{
DEFAULT_COPY_MOVE_DESTRUCT(socks5_client);
socks5_client() NOEXCEPT;

/// Proxy credentials are stored and passed in cleartext.
std::string username{};
std::string password{};

/// Socks5 proxy (default port convention is 1080, but not defaulted).
config::endpoint socks{};

/// True if socks::port is non-zero.
virtual bool proxied() const NOEXCEPT;

/// False if both username and password are empty.
virtual bool secured() const NOEXCEPT;
};

struct tcp_server
{
DEFAULT_COPY_MOVE_DESTRUCT(tcp_server);
Expand All @@ -54,8 +73,8 @@ struct BCT_API settings
uint16_t connections{ 0 };
uint32_t inactivity_minutes{ 10 };
uint32_t expiration_minutes{ 60 };
uint32_t maximum_request{ maximum_request_ };
uint32_t minimum_buffer{ maximum_request_ };
uint32_t maximum_request{ maximum_request_default };
uint32_t minimum_buffer{ maximum_request_default };

/// Helpers.
virtual bool enabled() const NOEXCEPT;
Expand Down Expand Up @@ -91,11 +110,30 @@ struct BCT_API settings
// TODO: settings unique to the websocket aspect.
};

struct peer_manual
: public tcp_server, public socks5_client
{
// The friends field must be initialized after peers is set.
peer_manual(system::chain::selection) NOEXCEPT
: tcp_server("manual"), socks5_client()
{
}

config::endpoints peers{};
config::authorities friends{};

/// Helpers.
void initialize() NOEXCEPT;
bool enabled() const NOEXCEPT override;
virtual bool peered(
const messages::peer::address_item& item) const NOEXCEPT;
};

struct peer_outbound
: public tcp_server
: public tcp_server, public socks5_client
{
peer_outbound(system::chain::selection context) NOEXCEPT
: tcp_server("outbound")
: tcp_server("outbound"), socks5_client()
{
connections = 10;

Expand Down Expand Up @@ -178,25 +216,6 @@ struct BCT_API settings
config::authority first_self() const NOEXCEPT;
};

struct peer_manual
: public tcp_server
{
// The friends field must be initialized after peers is set.
peer_manual(system::chain::selection) NOEXCEPT
: tcp_server("manual")
{
}

config::endpoints peers{};
config::authorities friends{};

/// Helpers.
void initialize() NOEXCEPT;
bool enabled() const NOEXCEPT override;
virtual bool peered(
const messages::peer::address_item& item) const NOEXCEPT;
};

// [network]
// ----------------------------------------------------------------------------
// bitcoin p2p network common settings.
Expand Down
Loading
Loading