Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions include/libp2p/host/basic_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ namespace libp2p::host {
*/
event::Bus &getBus();

/**
* Get list of protocols that were passed to `listenProtocol`.
*/
StreamProtocols getSupportedProtocols() const;

private:
std::shared_ptr<peer::IdentityManager> idmgr_;
std::shared_ptr<network::ListenerManager> listener_;
Expand Down
77 changes: 77 additions & 0 deletions include/libp2p/protocol/identify.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <libp2p/coro/coro.hpp>
#include <libp2p/event/bus.hpp>
#include <libp2p/peer/stream_protocols.hpp>
#include <libp2p/protocol/base_protocol.hpp>

namespace boost::asio {
class io_context;
} // namespace boost::asio

namespace libp2p::connection {
class CapableConnection;
} // namespace libp2p::connection

namespace libp2p::host {
class BasicHost;
} // namespace libp2p::host

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

namespace libp2p::protocol {
struct IdentifyInfo {
PeerId peer_id;
std::string protocol_version;
std::string agent_version;
std::vector<Multiaddress> listen_addresses;
Multiaddress observed_address;
StreamProtocols protocols;
};

using OnIdentifyChannel = event::channel_decl<IdentifyInfo, IdentifyInfo>;

struct IdentifyConfig {
// Fixes default field values with boost::di.
IdentifyConfig() = default;

std::string protocol_version;
std::string agent_version;
std::vector<Multiaddress> listen_addresses;
};

class Identify : public std::enable_shared_from_this<Identify>,
public BaseProtocol {
public:
Identify(std::shared_ptr<boost::asio::io_context> io_context,
std::shared_ptr<host::BasicHost> host,
std::shared_ptr<peer::IdentityManager> id_mgr,
IdentifyConfig config);

// Adaptor
StreamProtocols getProtocolIds() const override;

// BaseProtocol
void handle(std::shared_ptr<connection::Stream> stream) override;

void start();

private:
Coro<void> recv_identify(
std::shared_ptr<connection::CapableConnection> connection);

std::shared_ptr<boost::asio::io_context> io_context_;
std::shared_ptr<host::BasicHost> host_;
std::shared_ptr<peer::IdentityManager> id_mgr_;
IdentifyConfig config_;
event::Handle on_peer_connected_sub_;
};
} // namespace libp2p::protocol
80 changes: 80 additions & 0 deletions include/libp2p/protocol/ping.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <libp2p/coro/coro.hpp>
#include <libp2p/event/bus.hpp>
#include <libp2p/protocol/base_protocol.hpp>

namespace boost::asio {
class io_context;
} // namespace boost::asio

namespace libp2p::connection {
class CapableConnection;
} // namespace libp2p::connection

namespace libp2p::crypto::random {
class RandomGenerator;
} // namespace libp2p::crypto::random

namespace libp2p::host {
class BasicHost;
} // namespace libp2p::host

namespace libp2p::protocol {
struct PingConfig {
// Fixes default field values with boost::di.
PingConfig() = default;

/**
* Time to wait for response.
*/
std::chrono::seconds timeout{20};
/**
* Time between ping requests.
*/
std::chrono::seconds interval{15};
};

class Ping : public std::enable_shared_from_this<Ping>, public BaseProtocol {
public:
enum Error {
INVALID_RESPONSE,
};
Q_ENUM_ERROR_CODE_FRIEND(Error) {
using E = decltype(e);
switch (e) {
case E::INVALID_RESPONSE:
return "Ping received invalid response";
}
abort();
}

Ping(std::shared_ptr<boost::asio::io_context> io_context,
std::shared_ptr<host::BasicHost> host,
std::shared_ptr<libp2p::crypto::random::RandomGenerator> random,
PingConfig config);

// Adaptor
StreamProtocols getProtocolIds() const override;

// BaseProtocol
void handle(std::shared_ptr<connection::Stream> stream) override;

void start();

private:
Coro<void> ping(std::shared_ptr<connection::CapableConnection> connection);

std::shared_ptr<boost::asio::io_context> io_context_;
std::shared_ptr<host::BasicHost> host_;
std::shared_ptr<libp2p::crypto::random::RandomGenerator> random_;
PingConfig config_;
event::Handle on_peer_connected_sub_;
};
} // namespace libp2p::protocol
56 changes: 29 additions & 27 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,50 @@ libp2p_add_library(libp2p INTERFACE)
# Start with basic component libraries and essential dependencies
target_link_libraries(libp2p
INTERFACE
Boost::boost
Boost::Boost.DI
fmt::fmt
libsecp256k1::secp256k1
lsquic::lsquic
OpenSSL::Crypto
OpenSSL::SSL
p2p_basic_host
p2p_byteutil
p2p_connection_manager
p2p_crypto_provider
p2p_dialer
p2p_ecdsa_provider
p2p_ed25519_provider
p2p_gossip
p2p_peer_repository
p2p_hmac_provider
p2p_identity_manager
p2p_inmem_address_repository
p2p_inmem_key_repository
p2p_read_buffer
p2p_literals
p2p_varint_prefix_reader
p2p_dialer
p2p_connection_manager
p2p_transport_manager
p2p_inmem_protocol_repository
p2p_key_validator
p2p_listener_manager
p2p_identity_manager
p2p_literals
p2p_logger
p2p_peer_id
p2p_multiaddress
p2p_byteutil
p2p_multibase_codec
p2p_multiselect
p2p_peer_address
p2p_multibase_codec
p2p_peer_id
p2p_peer_repository
p2p_protocol_identify
p2p_protocol_ping
p2p_quic
p2p_random_generator
p2p_crypto_provider
p2p_secp256k1_provider
p2p_ecdsa_provider
p2p_key_validator
p2p_ed25519_provider
p2p_inmem_protocol_repository
p2p_read_buffer
p2p_rsa_provider
p2p_hmac_provider
p2p_secp256k1_provider
p2p_tls
p2p_quic
Boost::boost
fmt::fmt
OpenSSL::SSL
OpenSSL::Crypto
p2p_transport_manager
p2p_varint_prefix_reader
protobuf::libprotobuf
soralog::soralog
yaml-cpp::yaml-cpp
lsquic::lsquic
ZLIB::ZLIB
libsecp256k1::secp256k1
protobuf::libprotobuf
Boost::Boost.DI
)

target_include_directories(libp2p
Expand Down
3 changes: 3 additions & 0 deletions src/host/basic_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,7 @@ namespace libp2p::host {
connection_manager_->closeConnectionsToPeer(peer_id);
}

StreamProtocols BasicHost::getSupportedProtocols() const {
return listener_->getSupportedProtocols();
}
} // namespace libp2p::host
9 changes: 9 additions & 0 deletions src/protocol/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
add_subdirectory(echo)
add_subdirectory(gossip)
add_subdirectory(identify)

libp2p_add_library(p2p_protocol_ping
ping.cpp
)
target_link_libraries(p2p_protocol_ping
Boost::boost
p2p_logger
)
19 changes: 19 additions & 0 deletions src/protocol/identify/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright Quadrivium LLC
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#


add_proto_library(p2p_identify_proto
identify.proto
)

libp2p_add_library(p2p_protocol_identify
identify.cpp
)
target_link_libraries(p2p_protocol_identify
Boost::boost
p2p_identify_proto
p2p_logger
)
Loading
Loading