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
1 change: 1 addition & 0 deletions include/bitcoin/network/config/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BCT_API endpoint
DEFAULT_COPY_MOVE_DESTRUCT(endpoint);

using system::config::endpoint::endpoint;
endpoint(const address& address) NOEXCEPT;

/// Properties.
/// -----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/network/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ enum error_t : uint8_t
socks_method,
socks_username,
socks_password,
socks_server_name,
socks_host_name,
socks_authentication,
socks_failure,
socks_disallowed,
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/network/impl/channels/channel_rpc.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ inline void CLASS::handle_receive(const code& ec, size_t bytes,

if (stopped())
{
LOGQ("Rpc read abort [" << authority() << "]");
LOGQ("Rpc read abort [" << endpoint() << "]");
return;
}

Expand All @@ -93,7 +93,7 @@ inline void CLASS::handle_receive(const code& ec, size_t bytes,
// Don't log common conditions.
if (ec != error::end_of_stream && ec != error::operation_canceled)
{
LOGF("Rpc read failure [" << authority() << "] "
LOGF("Rpc read failure [" << endpoint() << "] "
<< ec.message());
}

Expand Down
9 changes: 3 additions & 6 deletions include/bitcoin/network/net/connector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ class BCT_API connector
/// The socket parameter is nullptr unless success is returned.

/// Try to connect to the address, starts timer.
virtual void connect(const config::address& host,
socket_handler&& handler) NOEXCEPT;

/// Try to connect to the authority, starts timer.
virtual void connect(const config::authority& host,
virtual void connect(const config::address& address,
socket_handler&& handler) NOEXCEPT;

/// Try to connect to the endpoint, starts timer.
Expand All @@ -84,7 +80,8 @@ class BCT_API connector

/// 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;
const config::address& address, const config::endpoint& endpoint,
socket_handler&& handler) NOEXCEPT;

/// Handlers, overridable for proxied connector.
virtual void handle_connect(const code& ec, const finish_ptr& finish,
Expand Down
47 changes: 21 additions & 26 deletions include/bitcoin/network/net/connector_socks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class BCT_API connector_socks
/// Connector overrides.
bool proxied() const NOEXCEPT override;
void start(const std::string& hostname, uint16_t port,
const config::address& host, socket_handler&& handler) NOEXCEPT override;
const config::address& address, const config::endpoint& endpoint,
socket_handler&& handler) NOEXCEPT override;
void handle_connect(const code& ec, const finish_ptr& finish,
const socket::ptr& socket) NOEXCEPT override;

Expand All @@ -67,48 +68,42 @@ class BCT_API connector_socks
template <size_t Size>
using data_cptr = std::shared_ptr<const system::data_array<Size>>;

// utility

// socks5 handshake
void do_socks_greeting_write(const code& ec, const finish_ptr& finish,
void do_socks_greeting_write(const code& ec,
const socket::ptr& socket) NOEXCEPT;
void handle_socks_greeting_write(const code& ec, size_t size,
const finish_ptr& finish, const socket::ptr& socket,
const data_cptr<3>& greeting) NOEXCEPT;
const socket::ptr& socket, const data_cptr<3>& greeting) NOEXCEPT;

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

void handle_socks_response_read(const code& ec, size_t size,
const finish_ptr& finish, const socket::ptr& socket,
const data_ptr<4>& response) NOEXCEPT;
const socket::ptr& socket, const data_ptr<4>& response) NOEXCEPT;
void handle_socks_length_read(const code& ec, size_t size,
const finish_ptr& finish, const socket::ptr& socket,
const data_ptr<1>& host_length) NOEXCEPT;
const socket::ptr& socket, const data_ptr<1>& host_length) NOEXCEPT;
void handle_socks_address_read(const code& ec, size_t size,
const finish_ptr& finish, const socket::ptr& socket,
const system::chunk_ptr& address) NOEXCEPT;
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;
void do_socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT;
void socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT;

// These are thread safe.
const settings::socks5& socks5_;
const uint8_t method_;
const bool proxied_;

// This is protected by mutex.
finish_ptr finish_{};
};

typedef std_vector<connector_socks::ptr> socks_connectors;
Expand Down
16 changes: 7 additions & 9 deletions include/bitcoin/network/net/hosts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ class BCT_API hosts
/// Reservation.
/// -----------------------------------------------------------------------

/// Reserve the address (currently connected), false if was reserved.
virtual bool reserve(const config::authority& host) NOEXCEPT;
/// Reserve the endpoint (currently connected), false if was reserved.
virtual bool reserve(const config::endpoint& host) NOEXCEPT;

/// Unreserve the address (no longer connected), false if was not reserved.
virtual bool unreserve(const config::authority& host) NOEXCEPT;
/// Unreserve the endpoint (no longer connected), false if was not reserved.
virtual bool unreserve(const config::endpoint& host) NOEXCEPT;

private:
typedef boost::circular_buffer<messages::peer::address_item> buffer;
Expand All @@ -120,7 +120,7 @@ class BCT_API hosts
// Inlines local to translation unit.
inline messages::peer::address_item::cptr pop() NOEXCEPT;
inline void push(const std::string& line) NOEXCEPT;
inline bool is_reserved(const config::authority& host) const NOEXCEPT;
inline bool is_reserved(const config::endpoint& host) const NOEXCEPT;

void do_take(const address_item_handler& handler) NOEXCEPT;
void do_restore(const address_item_cptr& host,
Expand All @@ -132,14 +132,12 @@ class BCT_API hosts
// These are thread safe.
const settings& settings_;
std::atomic<size_t> hosts_count_{};
std::atomic<size_t> authorities_count_{};
std::atomic<size_t> endpoints_count_{};

// These are not thread safe.
buffer buffer_;
bool stopped_{ true };

// TODO: optimize, default bucket count is around 8.
std::unordered_set<config::authority> authorities_{};
std::unordered_set<config::endpoint> endpoints_{};
};

} // namespace network
Expand Down
8 changes: 4 additions & 4 deletions include/bitcoin/network/net/proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ class BCT_API proxy
/// The socket was accepted (vs. connected).
bool inbound() const NOEXCEPT;

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

/// Get the address (outgoing) of the remote endpoint.
/// Get the address of the outgoing endpoint passed via construct.
const config::address& address() const NOEXCEPT;

/// Get the endpoint of the remote host.
const config::endpoint& endpoint() const NOEXCEPT;

protected:
proxy(const socket::ptr& socket) NOEXCEPT;

Expand Down
25 changes: 17 additions & 8 deletions include/bitcoin/network/net/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ class BCT_API socket

DELETE_COPY_MOVE(socket);

/// Use only for incoming connections (defaults outgoing address).
/// Use only for incoming connections.
socket(const logger& log, asio::io_context& service,
size_t maximum_request) NOEXCEPT;

/// Use only for outgoing connections (retains outgoing address).
/// Use only for outgoing connections. Endpoint represents the peer or
/// client (non-proxy) that the connector attempted to reach. Address holds
/// a copy of the p2p address associated with the connection (or empty).
socket(const logger& log, asio::io_context& service,
size_t maximum_request, const config::address& address,
bool proxied=false) NOEXCEPT;
const config::endpoint& endpoint, bool proxied=false) NOEXCEPT;

/// Asserts/logs stopped.
virtual ~socket() NOEXCEPT;
Expand Down Expand Up @@ -142,13 +144,15 @@ class BCT_API socket
/// Properties.
/// -----------------------------------------------------------------------

/// 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.
/// Get the address of the outgoing endpoint passed via construct.
virtual const config::address& address() const NOEXCEPT;

/// Get the endpoint of the remote host. Established by connection
/// resolution for incoming and non-proxied outgoing. For a proxied
/// connection (outgoing only) this is the value passed via construct.
virtual const config::endpoint& endpoint() const NOEXCEPT;

/// The socket was accepted (vs. connected).
virtual bool inbound() const NOEXCEPT;

Expand Down Expand Up @@ -287,7 +291,12 @@ class BCT_API socket
const count_handler& handler) NOEXCEPT;

protected:
socket(const logger& log, asio::io_context& service,
size_t maximum_request, const config::address& address,
const config::endpoint& endpoint, bool proxied, bool inbound) NOEXCEPT;

// These are thread safe.
const bool inbound_;
const bool proxied_;
const size_t maximum_;
asio::strand strand_;
Expand All @@ -297,7 +306,7 @@ class BCT_API socket
// These are protected by strand (see also handle_accept).
asio::socket socket_;
config::address address_;
config::authority authority_{};
config::endpoint endpoint_;
std::optional<ws::websocket> websocket_{};
};

Expand Down
8 changes: 4 additions & 4 deletions include/bitcoin/network/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class BCT_API protocol
session_->unsubscribe(channel_->identifier());
}

/// Broadcast a message instance to peers (use BROADCAST).
/// Broadcast a message instance to channels (use BROADCAST).
/// Channel identifier allows recipient-sender to self-identify.
template <class Message>
inline void broadcast(const typename Message::cptr& message) NOEXCEPT
Expand Down Expand Up @@ -172,10 +172,10 @@ class BCT_API protocol
/// The current thread is on the channel strand.
virtual bool stranded() const NOEXCEPT;

/// The authority of the peer.
virtual config::authority authority() const NOEXCEPT;
/// The opposite endpoint of the channel.
virtual config::endpoint opposite() const NOEXCEPT;

/// The outbound address of the peer.
/// The outbound address of the channel.
virtual const config::address& outbound() const NOEXCEPT;

/// The nonce of the channel.
Expand Down
4 changes: 2 additions & 2 deletions src/channels/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void channel::handle_expiration(const code& ec) NOEXCEPT

if (ec)
{
LOGF("Lifetime timer fail [" << authority() << "] " << ec.message());
LOGF("Lifetime timer fail [" << endpoint() << "] " << ec.message());
stop(ec);
return;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ void channel::handle_inactivity(const code& ec) NOEXCEPT

if (ec)
{
LOGF("Inactivity timer fail [" << authority() << "] " << ec.message());
LOGF("Inactivity timer fail [" << endpoint() << "] " << ec.message());
stop(ec);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/channels/channel_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void channel_http::handle_receive(const code& ec, size_t bytes,

if (stopped())
{
LOGQ("Http read abort [" << authority() << "]");
LOGQ("Http read abort [" << endpoint() << "]");
return;
}

Expand All @@ -101,7 +101,7 @@ void channel_http::handle_receive(const code& ec, size_t bytes,
// Don't log common conditions.
if (ec != error::end_of_stream && ec != error::operation_canceled)
{
LOGF("Http read failure [" << authority() << "] "
LOGF("Http read failure [" << endpoint() << "] "
<< ec.message());
}

Expand Down Expand Up @@ -199,7 +199,7 @@ void channel_http::log_message(const request& LOG_ONLY(request),
<< "] " << version
<< " (" << (request.chunked() ? "c" : serialize(bytes))
<< ") " << (request.keep_alive() ? "keep" : "drop")
<< " [" << authority() << "]"
<< " [" << endpoint() << "]"
<< " {" << (split(request[field::accept], ",").front()) << "...}"
<< " " << request.target());
}
Expand All @@ -214,7 +214,7 @@ void channel_http::log_message(const response& LOG_ONLY(response),
<< "] " << version
<< " (" << (response.chunked() ? "c" : serialize(bytes))
<< ") " << (response.keep_alive() ? "keep" : "drop")
<< " [" << authority() << "]"
<< " [" << endpoint() << "]"
<< " {" << (response[field::content_type]) << "}");
}

Expand Down
Loading
Loading