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
4 changes: 4 additions & 0 deletions include/bitcoin/network/config/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class BCT_API endpoint

using system::config::endpoint::endpoint;

/// Properties.
/// -----------------------------------------------------------------------
bool is_address() const NOEXCEPT;

/// Operators.
/// -----------------------------------------------------------------------

Expand Down
17 changes: 17 additions & 0 deletions include/bitcoin/network/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ enum error_t : uint8_t
subscriber_stopped,
desubscribed,

// socks5
socks_method,
socks_username,
socks_password,
socks_server_name,
socks_authentication,
socks_failure,
socks_disallowed,
socks_net_unreachable,
socks_host_unreachable,
socks_connection_refused,
socks_connection_expired,
socks_unsupported_command,
socks_unsupported_address,
socks_unassigned_failure,
socks_response_invalid,

////// http 4xx client error
bad_request,
////unauthorized,
Expand Down
8 changes: 5 additions & 3 deletions include/bitcoin/network/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ class BCT_API net
friend class session;

/// I/O factories.
virtual acceptor::ptr create_acceptor(size_t maximum) NOEXCEPT;
virtual acceptor::ptr create_acceptor() NOEXCEPT;
virtual connector::ptr create_seed_connector() NOEXCEPT;
virtual connector::ptr create_manual_connector() NOEXCEPT;
virtual connectors_ptr create_connectors(size_t count) NOEXCEPT;
virtual connector::ptr create_connector(size_t maximum) NOEXCEPT;
virtual connector::ptr create_connector() NOEXCEPT;
virtual connector::ptr create_connector(const settings::socks5& socks,
const steady_clock::duration& timeout, uint32_t maximum) NOEXCEPT;

/// Sequences.
virtual void do_start(const result_handler& handler) NOEXCEPT;
Expand Down
10 changes: 6 additions & 4 deletions include/bitcoin/network/net/connector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ class BCT_API connector
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;
/// Handlers, overridable for proxied connector.
virtual void handle_connect(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
virtual void handle_timer(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;

/// Override to inform socket construction.
virtual bool proxied() const NOEXCEPT;

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

Expand All @@ -111,8 +115,6 @@ class BCT_API connector
const socket::ptr& socket) NOEXCEPT;
void do_handle_connect(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
void handle_connect(code ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
};

typedef std_vector<connector::ptr> connectors;
Expand Down
62 changes: 43 additions & 19 deletions include/bitcoin/network/net/connector_socks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/net/connector.hpp>
#include <bitcoin/network/net/socket.hpp>
#include <bitcoin/network/settings.hpp>

namespace libbitcoin {
namespace network {
Expand All @@ -46,18 +47,18 @@ class BCT_API 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;
asio::io_context& service, const steady_clock::duration& timeout,
size_t maximum_request, std::atomic_bool& suspended,
const settings::socks5& socks) NOEXCEPT;

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

/// 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,
bool proxied() const NOEXCEPT override;
void start(const std::string& hostname, uint16_t port,
const config::address& host, socket_handler&& handler) NOEXCEPT override;
void handle_connect(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT override;

private:
Expand All @@ -67,24 +68,47 @@ class BCT_API connector_socks
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 do_socks_greeting_write(const code& ec, const finish_ptr& finish,
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;
const finish_ptr& finish, 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;
const finish_ptr& finish, const socket::ptr& socket,
const data_ptr<2>& response) NOEXCEPT;
void handle_socks_authentication_write(const code& ec,
size_t size, const finish_ptr& finish, const socket::ptr& socket,
const system::chunk_ptr& authenticator) NOEXCEPT;
void handle_socks_authentication_read(const code& ec,
size_t size, const finish_ptr& finish, const socket::ptr& socket,
const data_ptr<2>& response) NOEXCEPT;

void do_socks_connect_write(const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
void handle_socks_connect_write(const code& ec, size_t size,
const socket::ptr& socket, const system::chunk_ptr& request) NOEXCEPT;
const finish_ptr& finish, 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;
const finish_ptr& finish, 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;
const finish_ptr& finish, 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;
const finish_ptr& finish, const socket::ptr& socket,
const system::chunk_ptr& address) NOEXCEPT;

void do_socks_finish(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;
void socks_finish(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT;

// This is protected by strand.
const config::endpoint socks5_;
// These are thread safe.
const settings::socks5& socks5_;
const uint8_t method_;
const bool proxied_;
};

typedef std_vector<connector_socks::ptr> socks_connectors;
Expand Down
8 changes: 6 additions & 2 deletions include/bitcoin/network/net/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class BCT_API socket

/// Use only for outgoing connections (retains outgoing address).
socket(const logger& log, asio::io_context& service,
size_t maximum_request, const config::address& address) NOEXCEPT;
size_t maximum_request, const config::address& address,
bool proxied=false) NOEXCEPT;

/// Asserts/logs stopped.
virtual ~socket() NOEXCEPT;
Expand Down Expand Up @@ -86,6 +87,7 @@ class BCT_API socket
result_handler&& handler) NOEXCEPT;

/// Create an outbound connection, handler posted to socket strand.
/// Authority will be set to the connected endpoint unless proxied is set.
virtual void connect(const asio::endpoints& range,
result_handler&& handler) NOEXCEPT;

Expand Down Expand Up @@ -140,9 +142,10 @@ class BCT_API socket
/// Properties.
/// -----------------------------------------------------------------------

/// Get the authority (incoming) of the remote endpoint.
/// Get the authority (outgoing/incoming) of the remote endpoint.
virtual const config::authority& authority() const NOEXCEPT;

/// TODO: this can be set to the binding for incoming sockets.
/// Get the address (outgoing) of the remote endpoint.
virtual const config::address& address() const NOEXCEPT;

Expand Down Expand Up @@ -285,6 +288,7 @@ class BCT_API socket

protected:
// These are thread safe.
const bool proxied_;
const size_t maximum_;
asio::strand strand_;
asio::io_context& service_;
Expand Down
14 changes: 7 additions & 7 deletions include/bitcoin/network/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ class BCT_API session
/// Factories.
/// -----------------------------------------------------------------------

/// Call to create channel acceptor.
virtual acceptor::ptr create_acceptor(size_t maximum) NOEXCEPT;
/// Create a channel acceptor (inbound).
virtual acceptor::ptr create_acceptor() NOEXCEPT;

/// Call to create channel connector for seed connections.
virtual connector::ptr create_connector() NOEXCEPT;
/// Create a seed channel connector.
virtual connector::ptr create_seed_connector() NOEXCEPT;

/// Call to create channel connector for manual/outbound connections.
virtual connector::ptr create_connector(size_t maximum) NOEXCEPT;
/// Create a manual channel connector.
virtual connector::ptr create_manual_connector() NOEXCEPT;

/// Call to create a set of channel connectors.
/// Create a batch of outbound channel connectors.
virtual connectors_ptr create_connectors(size_t count) NOEXCEPT;

/// Create a channel from the started socket.
Expand Down
16 changes: 8 additions & 8 deletions include/bitcoin/network/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ constexpr uint32_t maximum_request_default
/// Common network configuration settings, properties not thread safe.
struct BCT_API settings
{
struct socks5_client
struct socks5
{
DEFAULT_COPY_MOVE_DESTRUCT(socks5_client);
socks5_client() NOEXCEPT;
DEFAULT_COPY_MOVE_DESTRUCT(socks5);
socks5() NOEXCEPT;

/// Proxy credentials are stored and passed in cleartext.
std::string username{};
Expand All @@ -57,7 +57,7 @@ struct BCT_API settings
virtual bool proxied() const NOEXCEPT;

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

struct tcp_server
Expand Down Expand Up @@ -111,11 +111,11 @@ struct BCT_API settings
};

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

Expand All @@ -130,10 +130,10 @@ struct BCT_API settings
};

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

Expand Down
6 changes: 6 additions & 0 deletions src/config/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ namespace libbitcoin {
namespace network {
namespace config {

bool endpoint::is_address() const NOEXCEPT
{
// Serialize to address, cast to bool, true if not default (address).
return to_address();
}

// protected
address endpoint::to_address() const NOEXCEPT
{
Expand Down
17 changes: 17 additions & 0 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ DEFINE_ERROR_T_MESSAGE_MAP(error)
{ subscriber_stopped, "subscriber stopped" },
{ desubscribed, "subscriber desubscribed" },

// socks5
{ socks_method, "socks method not supported" },
{ socks_username, "socks username too long" },
{ socks_password, "socks password too long" },
{ socks_server_name, "socks server name too long" },
{ socks_authentication, "socks authentication failed" },
{ socks_failure, "socks failure" },
{ socks_disallowed, "socks connection disallowed" },
{ socks_net_unreachable, "socks network unreachable" },
{ socks_host_unreachable, "socks host unreachable" },
{ socks_connection_refused, "socks connection refused" },
{ socks_connection_expired, "socks connection expired" },
{ socks_unsupported_command, "socks unsupported command" },
{ socks_unsupported_address, "socks unsupported address" },
{ socks_unassigned_failure, "socks unassigned failure" },
{ socks_response_invalid, "socks response invalid" },

////// http 4xx client error
{ bad_request, "bad request" },
////{ unauthorized, "unauthorized" },
Expand Down
Loading
Loading