-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathbasic_host.cpp
More file actions
220 lines (183 loc) · 6.81 KB
/
basic_host.cpp
File metadata and controls
220 lines (183 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <libp2p/host/basic_host/basic_host.hpp>
#include <boost/assert.hpp>
#include <libp2p/crypto/key_marshaller/key_marshaller_impl.hpp>
namespace libp2p::host {
BasicHost::BasicHost(
std::shared_ptr<peer::IdentityManager> idmgr,
std::unique_ptr<network::Network> network,
std::unique_ptr<peer::PeerRepository> repo,
std::shared_ptr<event::Bus> bus,
std::shared_ptr<network::TransportManager> transport_manager,
Libp2pClientVersion libp2p_client_version)
: idmgr_{std::move(idmgr)},
network_{std::move(network)},
repo_{std::move(repo)},
bus_{std::move(bus)},
transport_manager_{std::move(transport_manager)},
libp2p_client_version_{std::move(libp2p_client_version)} {
BOOST_ASSERT(idmgr_ != nullptr);
BOOST_ASSERT(network_ != nullptr);
BOOST_ASSERT(repo_ != nullptr);
BOOST_ASSERT(bus_ != nullptr);
BOOST_ASSERT(transport_manager_ != nullptr);
}
std::string_view BasicHost::getLibp2pVersion() const {
return "0.0.0";
}
std::string_view BasicHost::getLibp2pClientVersion() const {
return libp2p_client_version_.version;
}
peer::PeerId BasicHost::getId() const {
return idmgr_->getId();
}
peer::PeerInfo BasicHost::getPeerInfo() const {
auto addresses = getAddresses();
auto observed = getObservedAddresses();
auto interfaces = getAddressesInterfaces();
std::set<multi::Multiaddress> unique_addresses;
unique_addresses.insert(std::make_move_iterator(addresses.begin()),
std::make_move_iterator(addresses.end()));
unique_addresses.insert(std::make_move_iterator(interfaces.begin()),
std::make_move_iterator(interfaces.end()));
unique_addresses.insert(std::make_move_iterator(observed.begin()),
std::make_move_iterator(observed.end()));
// TODO(xDimon): Needs to filter special interfaces (e.g. INADDR_ANY, etc.)
for (auto i = unique_addresses.begin(); i != unique_addresses.end();) {
bool is_good_addr = true;
for (auto &pv : i->getProtocolsWithValues()) {
if (pv.first.code == multi::Protocol::Code::IP4) {
if (pv.second == "0.0.0.0") {
is_good_addr = false;
break;
}
} else if (pv.first.code == multi::Protocol::Code::IP6) {
if (pv.second == "::") {
is_good_addr = false;
break;
}
}
}
if (!is_good_addr) {
i = unique_addresses.erase(i);
} else {
++i;
}
}
std::vector<multi::Multiaddress> unique_addr_list(
std::make_move_iterator(unique_addresses.begin()),
std::make_move_iterator(unique_addresses.end()));
return {getId(), std::move(unique_addr_list)};
}
std::vector<multi::Multiaddress> BasicHost::getAddresses() const {
return network_->getListener().getListenAddresses();
}
std::vector<multi::Multiaddress> BasicHost::getAddressesInterfaces() const {
return network_->getListener().getListenAddressesInterfaces();
}
std::vector<multi::Multiaddress> BasicHost::getObservedAddresses() const {
auto r = repo_->getAddressRepository().getAddresses(getId());
if (r) {
return r.value();
}
// we don't know our addresses
return {};
}
Host::Connectedness BasicHost::connectedness(const peer::PeerInfo &p) const {
auto conn = network_->getConnectionManager().getBestConnectionForPeer(p.id);
if (conn != nullptr) {
return Connectedness::CONNECTED;
}
// for each address, try to find transport to dial
for (auto &&ma : p.addresses) {
if (auto tr = transport_manager_->findBest(ma); tr != nullptr) {
// we can dial to the peer
return Connectedness::CAN_CONNECT;
}
}
auto res = repo_->getAddressRepository().getAddresses(p.id);
if (res.has_value()) {
for (auto &&ma : res.value()) {
if (auto tr = transport_manager_->findBest(ma); tr != nullptr) {
// we can dial to the peer
return Connectedness::CAN_CONNECT;
}
}
}
// we did not find available transports to dial
return Connectedness::CAN_NOT_CONNECT;
}
void BasicHost::setProtocolHandler(StreamProtocols protocols,
StreamAndProtocolCb cb,
ProtocolPredicate predicate) {
network_->getListener().getRouter().setProtocolHandler(
std::move(protocols), std::move(cb), std::move(predicate));
}
void BasicHost::newStream(const peer::PeerInfo &peer_info,
StreamProtocols protocols,
StreamAndProtocolOrErrorCb cb) {
network_->getDialer().newStream(
peer_info, std::move(protocols), std::move(cb));
}
outcome::result<void> BasicHost::listen(const multi::Multiaddress &ma) {
return network_->getListener().listen(ma);
}
outcome::result<void> BasicHost::closeListener(
const multi::Multiaddress &ma) {
return network_->getListener().closeListener(ma);
}
outcome::result<void> BasicHost::removeListener(
const multi::Multiaddress &ma) {
return network_->getListener().removeListener(ma);
}
void BasicHost::start() {
network_->getListener().start();
}
event::Handle BasicHost::setOnNewConnectionHandler(
const NewConnectionHandler &h) const {
return bus_->getChannel<event::network::OnNewConnectionChannel>().subscribe(
[h{std::move(h)}](auto &&conn) {
if (auto connection = conn.lock()) {
auto remote_peer_res = connection->remotePeer();
if (!remote_peer_res) {
return;
}
auto remote_peer_addr_res = connection->remoteMultiaddr();
if (!remote_peer_addr_res) {
return;
}
if (h != nullptr) {
h(peer::PeerInfo{std::move(remote_peer_res.value()),
std::vector<multi::Multiaddress>{
std::move(remote_peer_addr_res.value())}});
}
}
});
}
void BasicHost::stop() {
network_->getListener().stop();
}
network::Network &BasicHost::getNetwork() {
return *network_;
}
peer::PeerRepository &BasicHost::getPeerRepository() {
return *repo_;
}
network::Router &BasicHost::getRouter() {
return network_->getListener().getRouter();
}
event::Bus &BasicHost::getBus() {
return *bus_;
}
void BasicHost::connect(const peer::PeerInfo &peer_info,
const ConnectionResultHandler &handler) {
network_->getDialer().dial(peer_info, handler);
}
void BasicHost::disconnect(const peer::PeerId &peer_id) {
network_->closeConnections(peer_id);
}
} // namespace libp2p::host