Skip to content

Commit

Permalink
refactor tcp (#253)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <[email protected]>
  • Loading branch information
turuslan committed Jun 18, 2024
1 parent 1e68917 commit b195aa5
Show file tree
Hide file tree
Showing 24 changed files with 400 additions and 327 deletions.
5 changes: 4 additions & 1 deletion include/libp2p/common/asio_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ namespace libp2p {
return {s.data(), s.size()};
}

inline boost::asio::mutable_buffer asioBuffer(BytesOut s) {
boost::asio::mutable_buffer asioBuffer(auto &&t)
requires(requires { BytesOut{t}; })
{
BytesOut s{t};
return {s.data(), s.size()};
}

Expand Down
1 change: 1 addition & 0 deletions include/libp2p/injector/network_injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <libp2p/security/secio/exchange_message_marshaller_impl.hpp>
#include <libp2p/security/secio/propose_message_marshaller_impl.hpp>
#include <libp2p/security/tls.hpp>
#include <libp2p/security/tls/ssl_context.hpp>
#include <libp2p/transport/impl/upgrader_impl.hpp>
#include <libp2p/transport/tcp.hpp>

Expand Down
8 changes: 8 additions & 0 deletions include/libp2p/multi/multiaddress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,16 @@ namespace libp2p::multi {

boost::optional<std::string> peer_id_;
};

inline auto format_as(const Multiaddress &ma) {
return ma.getStringAddress();
}
} // namespace libp2p::multi

namespace libp2p {
using multi::Multiaddress;
} // namespace libp2p

namespace std {
template <>
struct hash<libp2p::multi::Multiaddress> {
Expand Down
14 changes: 8 additions & 6 deletions include/libp2p/multi/multiaddress_protocol_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace libp2p::multi {
ONION3 = 445,
GARLIC64 = 446,
QUIC = 460,
QUIC_V1 = 461,
HTTP = 480,
HTTPS = 443,
WS = 477,
Expand All @@ -69,9 +70,9 @@ namespace libp2p::multi {
return code == p.code;
}

const Code code;
const ssize_t size;
const std::string_view name;
Code code;
ssize_t size;
std::string_view name;
};

/**
Expand All @@ -82,11 +83,11 @@ namespace libp2p::multi {
/**
* The total number of known protocols
*/
static constexpr size_t kProtocolsNum = 31
#ifndef NDEBUG
static const std::size_t kProtocolsNum = 34;
#else
static const std::size_t kProtocolsNum = 30;
+ 4
#endif
;

/**
* Returns a protocol with the corresponding name if it exists, or nullptr
Expand Down Expand Up @@ -151,6 +152,7 @@ namespace libp2p::multi {
{Protocol::Code::ONION3, 296, "onion3"},
{Protocol::Code::GARLIC64, Protocol::kVarLen, "garlic64"},
{Protocol::Code::QUIC, 0, "quic"},
{Protocol::Code::QUIC_V1, 0, "quic-v1"},
{Protocol::Code::HTTP, 0, "http"},
{Protocol::Code::HTTPS, 0, "https"},
{Protocol::Code::WS, 0, "ws"},
Expand Down
4 changes: 4 additions & 0 deletions include/libp2p/peer/peer_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ namespace libp2p::peer {

} // namespace libp2p::peer

namespace libp2p {
using peer::PeerId;
} // namespace libp2p

namespace std {
template <>
struct hash<libp2p::peer::PeerId> {
Expand Down
34 changes: 34 additions & 0 deletions include/libp2p/security/tls/ssl_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <memory>

namespace boost::asio::ssl {
class context;
}

namespace libp2p::peer {
class IdentityManager;
} // namespace libp2p::peer

namespace libp2p::crypto::marshaller {
class KeyMarshaller;
} // namespace libp2p::crypto::marshaller

namespace libp2p::security {
/**
* SSL context with libp2p TLS 1.3 certificate
*/
struct SslContext {
SslContext(const peer::IdentityManager &idmgr,
const crypto::marshaller::KeyMarshaller &key_marshaller);

std::shared_ptr<boost::asio::ssl::context> tls;
std::shared_ptr<boost::asio::ssl::context> quic;
};
} // namespace libp2p::security
5 changes: 2 additions & 3 deletions include/libp2p/security/tls/tls_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <libp2p/security/tls/tls_errors.hpp>

namespace libp2p::security {
struct SslContext;

/// TLS 1.3 security adaptor
class TlsAdaptor : public SecurityAdaptor,
Expand All @@ -27,6 +28,7 @@ namespace libp2p::security {
TlsAdaptor(
std::shared_ptr<peer::IdentityManager> idmgr,
std::shared_ptr<boost::asio::io_context> io_context,
const SslContext &ssl_context,
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller);

/// Returns "/tls/1.0.0"
Expand All @@ -42,9 +44,6 @@ namespace libp2p::security {
SecConnCallbackFunc cb) override;

private:
/// Creates shared SSL context, generates certificate and private key
outcome::result<void> setupContext();

/// Creates TLSConnection and starts handshake
void asyncHandshake(std::shared_ptr<connection::LayerConnection> conn,
boost::optional<peer::PeerId> remote_peer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include <libp2p/log/logger.hpp>
#include <libp2p/peer/peer_id.hpp>

struct x509_st;

namespace boost::asio::ssl {
class verify_context;
} // namespace boost::asio::ssl

namespace libp2p::security::tls_details {

/// Returns "tls" logger
Expand Down Expand Up @@ -51,7 +57,7 @@ namespace libp2p::security::tls_details {
/// \param key_marshaller key marshaller (needed to deal with extension data)
/// \return pubkey and peer id, or error
outcome::result<PubkeyAndPeerId> verifyPeerAndExtractIdentity(
X509 *peer_certificate,
x509_st *peer_certificate,
const crypto::marshaller::KeyMarshaller &key_marshaller);

} // namespace libp2p::security::tls_details
30 changes: 0 additions & 30 deletions include/libp2p/transport/tcp/tcp_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ namespace libp2p::transport {
using Tcp = boost::asio::ip::tcp;
using ErrorCode = boost::system::error_code;
using ResolverResultsType = Tcp::resolver::results_type;
using ResolveCallback = void(const ErrorCode &,
const ResolverResultsType &);
using ResolveCallbackFunc = std::function<ResolveCallback>;
using ConnectCallback = void(const ErrorCode &, const Tcp::endpoint &);
using ConnectCallbackFunc = std::function<ConnectCallback>;

Expand All @@ -46,33 +43,6 @@ namespace libp2p::transport {
ProtoAddrVec layers,
Tcp::socket &&socket);

/**
* @brief Resolve service name (DNS).
* @param endpoint endpoint to resolve.
* @param cb callback executed on operation completion.
*/
void resolve(const Tcp::endpoint &endpoint, ResolveCallbackFunc cb);

/**
* @brief Resolve service name (DNS).
* @param host_name host name to resolve
* @param cb callback executed on operation completion.
*/
void resolve(const std::string &host_name,
const std::string &port,
ResolveCallbackFunc cb);

/**
* @brief Resolve service name (DNS).
* @param protocol is either Tcp::ip4 or Tcp::ip6 protocol
* @param host_name host name to resolve
* @param cb callback executed on operation completion.
*/
void resolve(const Tcp &protocol,
const std::string &host_name,
const std::string &port,
ResolveCallbackFunc cb);

/**
* @brief Connect to a remote service.
* @param iterator list of resolved IP addresses of remote service.
Expand Down
1 change: 0 additions & 1 deletion include/libp2p/transport/tcp/tcp_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <boost/asio.hpp>
#include <libp2p/transport/tcp/tcp_connection.hpp>
#include <libp2p/transport/tcp/tcp_util.hpp>
#include <libp2p/transport/transport_listener.hpp>
#include <libp2p/transport/upgrader.hpp>

Expand Down
7 changes: 3 additions & 4 deletions include/libp2p/transport/tcp/tcp_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

#pragma once

#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <libp2p/transport/tcp/tcp_listener.hpp>
#include <libp2p/transport/tcp/tcp_util.hpp>
#include <libp2p/transport/transport_adaptor.hpp>
#include <libp2p/transport/upgrader.hpp>

Expand Down Expand Up @@ -44,6 +43,6 @@ namespace libp2p::transport {
private:
std::shared_ptr<boost::asio::io_context> context_;
std::shared_ptr<Upgrader> upgrader_;
}; // namespace libp2p::transport

boost::asio::ip::tcp::resolver resolver_;
};
} // namespace libp2p::transport
Loading

0 comments on commit b195aa5

Please sign in to comment.