Skip to content

Commit b195aa5

Browse files
authored
refactor tcp (#253)
Signed-off-by: turuslan <[email protected]>
1 parent 1e68917 commit b195aa5

24 files changed

Lines changed: 400 additions & 327 deletions

include/libp2p/common/asio_buffer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ namespace libp2p {
1515
return {s.data(), s.size()};
1616
}
1717

18-
inline boost::asio::mutable_buffer asioBuffer(BytesOut s) {
18+
boost::asio::mutable_buffer asioBuffer(auto &&t)
19+
requires(requires { BytesOut{t}; })
20+
{
21+
BytesOut s{t};
1922
return {s.data(), s.size()};
2023
}
2124

include/libp2p/injector/network_injector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <libp2p/security/secio/exchange_message_marshaller_impl.hpp>
4141
#include <libp2p/security/secio/propose_message_marshaller_impl.hpp>
4242
#include <libp2p/security/tls.hpp>
43+
#include <libp2p/security/tls/ssl_context.hpp>
4344
#include <libp2p/transport/impl/upgrader_impl.hpp>
4445
#include <libp2p/transport/tcp.hpp>
4546

include/libp2p/multi/multiaddress.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,16 @@ namespace libp2p::multi {
194194

195195
boost::optional<std::string> peer_id_;
196196
};
197+
198+
inline auto format_as(const Multiaddress &ma) {
199+
return ma.getStringAddress();
200+
}
197201
} // namespace libp2p::multi
198202

203+
namespace libp2p {
204+
using multi::Multiaddress;
205+
} // namespace libp2p
206+
199207
namespace std {
200208
template <>
201209
struct hash<libp2p::multi::Multiaddress> {

include/libp2p/multi/multiaddress_protocol_list.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace libp2p::multi {
4444
ONION3 = 445,
4545
GARLIC64 = 446,
4646
QUIC = 460,
47+
QUIC_V1 = 461,
4748
HTTP = 480,
4849
HTTPS = 443,
4950
WS = 477,
@@ -69,9 +70,9 @@ namespace libp2p::multi {
6970
return code == p.code;
7071
}
7172

72-
const Code code;
73-
const ssize_t size;
74-
const std::string_view name;
73+
Code code;
74+
ssize_t size;
75+
std::string_view name;
7576
};
7677

7778
/**
@@ -82,11 +83,11 @@ namespace libp2p::multi {
8283
/**
8384
* The total number of known protocols
8485
*/
86+
static constexpr size_t kProtocolsNum = 31
8587
#ifndef NDEBUG
86-
static const std::size_t kProtocolsNum = 34;
87-
#else
88-
static const std::size_t kProtocolsNum = 30;
88+
+ 4
8989
#endif
90+
;
9091

9192
/**
9293
* Returns a protocol with the corresponding name if it exists, or nullptr
@@ -151,6 +152,7 @@ namespace libp2p::multi {
151152
{Protocol::Code::ONION3, 296, "onion3"},
152153
{Protocol::Code::GARLIC64, Protocol::kVarLen, "garlic64"},
153154
{Protocol::Code::QUIC, 0, "quic"},
155+
{Protocol::Code::QUIC_V1, 0, "quic-v1"},
154156
{Protocol::Code::HTTP, 0, "http"},
155157
{Protocol::Code::HTTPS, 0, "https"},
156158
{Protocol::Code::WS, 0, "ws"},

include/libp2p/peer/peer_id.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ namespace libp2p::peer {
9999

100100
} // namespace libp2p::peer
101101

102+
namespace libp2p {
103+
using peer::PeerId;
104+
} // namespace libp2p
105+
102106
namespace std {
103107
template <>
104108
struct hash<libp2p::peer::PeerId> {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <memory>
10+
11+
namespace boost::asio::ssl {
12+
class context;
13+
}
14+
15+
namespace libp2p::peer {
16+
class IdentityManager;
17+
} // namespace libp2p::peer
18+
19+
namespace libp2p::crypto::marshaller {
20+
class KeyMarshaller;
21+
} // namespace libp2p::crypto::marshaller
22+
23+
namespace libp2p::security {
24+
/**
25+
* SSL context with libp2p TLS 1.3 certificate
26+
*/
27+
struct SslContext {
28+
SslContext(const peer::IdentityManager &idmgr,
29+
const crypto::marshaller::KeyMarshaller &key_marshaller);
30+
31+
std::shared_ptr<boost::asio::ssl::context> tls;
32+
std::shared_ptr<boost::asio::ssl::context> quic;
33+
};
34+
} // namespace libp2p::security

include/libp2p/security/tls/tls_adaptor.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <libp2p/security/tls/tls_errors.hpp>
1616

1717
namespace libp2p::security {
18+
struct SslContext;
1819

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

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

4446
private:
45-
/// Creates shared SSL context, generates certificate and private key
46-
outcome::result<void> setupContext();
47-
4847
/// Creates TLSConnection and starts handshake
4948
void asyncHandshake(std::shared_ptr<connection::LayerConnection> conn,
5049
boost::optional<peer::PeerId> remote_peer,

src/security/tls/tls_details.hpp renamed to include/libp2p/security/tls/tls_details.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
#include <libp2p/log/logger.hpp>
1111
#include <libp2p/peer/peer_id.hpp>
1212

13+
struct x509_st;
14+
15+
namespace boost::asio::ssl {
16+
class verify_context;
17+
} // namespace boost::asio::ssl
18+
1319
namespace libp2p::security::tls_details {
1420

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

5763
} // namespace libp2p::security::tls_details

include/libp2p/transport/tcp/tcp_connection.hpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ namespace libp2p::transport {
3434
using Tcp = boost::asio::ip::tcp;
3535
using ErrorCode = boost::system::error_code;
3636
using ResolverResultsType = Tcp::resolver::results_type;
37-
using ResolveCallback = void(const ErrorCode &,
38-
const ResolverResultsType &);
39-
using ResolveCallbackFunc = std::function<ResolveCallback>;
4037
using ConnectCallback = void(const ErrorCode &, const Tcp::endpoint &);
4138
using ConnectCallbackFunc = std::function<ConnectCallback>;
4239

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

49-
/**
50-
* @brief Resolve service name (DNS).
51-
* @param endpoint endpoint to resolve.
52-
* @param cb callback executed on operation completion.
53-
*/
54-
void resolve(const Tcp::endpoint &endpoint, ResolveCallbackFunc cb);
55-
56-
/**
57-
* @brief Resolve service name (DNS).
58-
* @param host_name host name to resolve
59-
* @param cb callback executed on operation completion.
60-
*/
61-
void resolve(const std::string &host_name,
62-
const std::string &port,
63-
ResolveCallbackFunc cb);
64-
65-
/**
66-
* @brief Resolve service name (DNS).
67-
* @param protocol is either Tcp::ip4 or Tcp::ip6 protocol
68-
* @param host_name host name to resolve
69-
* @param cb callback executed on operation completion.
70-
*/
71-
void resolve(const Tcp &protocol,
72-
const std::string &host_name,
73-
const std::string &port,
74-
ResolveCallbackFunc cb);
75-
7646
/**
7747
* @brief Connect to a remote service.
7848
* @param iterator list of resolved IP addresses of remote service.

include/libp2p/transport/tcp/tcp_listener.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <boost/asio.hpp>
1010
#include <libp2p/transport/tcp/tcp_connection.hpp>
11-
#include <libp2p/transport/tcp/tcp_util.hpp>
1211
#include <libp2p/transport/transport_listener.hpp>
1312
#include <libp2p/transport/upgrader.hpp>
1413

0 commit comments

Comments
 (0)