36 typedef std::function<void(
bool)> SignatureCheckCallback;
42 auto c = conf.node_config;
43 if (not c.node_id and conf.id.second)
44 c.
node_id = InfoHash::get(
"node:"+conf.id.second->getId().toString());
54 SecureDht(std::unique_ptr<DhtInterface>
dht,
Config config, IdentityAnnouncedCb iacb = {},
const std::shared_ptr<Logger>& l = {});
58 InfoHash getId()
const {
59 return key_ ? key_->getPublicKey().getId() : InfoHash();
61 PkId getLongId()
const {
62 return key_ ? key_->getPublicKey().getLongId() : PkId();
64 Sp<crypto::PublicKey> getPublicKey()
const {
65 return key_ ? key_->getSharedPublicKey() : Sp<crypto::PublicKey>{};
68 ValueType secureType(ValueType&& type);
70 ValueType secureType(
const ValueType& type) {
71 ValueType tmp_type = type;
72 return secureType(std::move(tmp_type));
75 void registerType(
const ValueType& type)
override {
77 dht_->registerType(secureType(type));
79 void registerType(ValueType&& type) {
81 dht_->registerType(secureType(std::forward<ValueType>(type)));
83 void registerInsecureType(
const ValueType& type) {
85 dht_->registerType(type);
94 void get(
const InfoHash&
id, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f = {}, Where&& w = {})
override {
95 get(
id, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
97 void get(
const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {})
override {
98 get(key, bindGetCb(cb), donecb, std::forward<Value::Filter>(f), std::forward<Where>(w));
100 void get(
const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f={}, Where&& w = {})
override {
101 get(key, bindGetCb(cb), bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
107 void putSigned(
const InfoHash& hash, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
108 void putSigned(
const InfoHash& hash,
Value&& v, DoneCallback callback,
bool permanent =
false) {
109 putSigned(hash, std::make_shared<Value>(std::move(v)), callback, permanent);
118 void putEncrypted(
const InfoHash& hash,
const InfoHash& to,
Value&& v, DoneCallback callback,
bool permanent =
false) {
119 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
121 void putEncrypted(
const InfoHash& hash,
const crypto::PublicKey& to, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
123 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
135 void findCertificate(
const InfoHash& node,
const std::function<
void(
const Sp<crypto::Certificate>)>& cb);
136 void findPublicKey(
const InfoHash& node,
const std::function<
void(
const Sp<crypto::PublicKey>)>& cb);
138 Sp<crypto::Certificate> registerCertificate(
const InfoHash& node,
const Blob& cert);
139 void registerCertificate(Sp<crypto::Certificate>& cert);
141 Sp<crypto::Certificate> getCertificate(
const InfoHash& node)
const;
142 Sp<crypto::PublicKey> getPublicKey(
const InfoHash& node)
const;
150 localQueryMethod_ = std::move(query_method);
156 void shutdown(ShutdownCallback cb,
bool stop =
false)
override {
157 dht_->shutdown(cb, stop);
159 void dumpTables()
const override {
165 return dht_->getStoreSize();
167 std::string getStorageLog()
const override {
168 return dht_->getStorageLog();
170 std::string getStorageLog(
const InfoHash& h)
const override {
171 return dht_->getStorageLog(h);
174 dht_->setStorageLimit(limit);
176 size_t getStorageLimit()
const override {
177 return dht_->getStorageLimit();
181 return dht_->exportNodes();
183 std::vector<ValuesExport> exportValues()
const override {
184 return dht_->exportValues();
186 void importValues(
const std::vector<ValuesExport>& v)
override {
187 dht_->importValues(v);
189 NodeStats getNodesStats(sa_family_t af)
const override {
190 return dht_->getNodesStats(af);
192 std::vector<unsigned> getNodeMessageStats(
bool in =
false)
override {
193 return dht_->getNodeMessageStats(in);
195 std::string getRoutingTablesLog(sa_family_t af)
const override {
196 return dht_->getRoutingTablesLog(af);
198 std::string getSearchesLog(sa_family_t af)
const override {
199 return dht_->getSearchesLog(af);
201 std::string getSearchLog(
const InfoHash& h, sa_family_t af = AF_UNSPEC)
const override {
202 return dht_->getSearchLog(h, af);
204 std::vector<SockAddr> getPublicAddress(sa_family_t family = 0)
override {
205 return dht_->getPublicAddress(family);
207 time_point periodic(
const uint8_t *buf,
size_t buflen, SockAddr sa,
const time_point& now)
override {
208 return dht_->periodic(buf, buflen, std::move(sa), now);
210 time_point periodic(
const uint8_t *buf,
size_t buflen,
const sockaddr* from, socklen_t fromlen,
const time_point& now)
override {
211 return dht_->periodic(buf, buflen, from, fromlen, now);
214 return dht_->updateStatus(af);
216 NodeStatus getStatus(sa_family_t af)
const override {
217 return dht_->getStatus(af);
219 NodeStatus getStatus()
const override {
220 return dht_->getStatus();
222 net::DatagramSocket* getSocket()
const override {
223 return dht_->getSocket();
226 return dht_->isRunning(af);
228 const ValueType& getType(ValueType::Id type_id)
const override {
229 return dht_->getType(type_id);
231 void addBootstrap(
const std::string& host,
const std::string& service)
override {
232 dht_->addBootstrap(host, service);
234 void clearBootstrap()
override {
235 dht_->clearBootstrap();
238 dht_->insertNode(
id, sa);
240 void insertNode(
const NodeExport& n)
override {
243 void pingNode(SockAddr sa, DoneCallbackSimple&& cb={})
override {
244 dht_->pingNode(std::move(sa), std::move(cb));
246 void query(
const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {},
Query&& q = {})
override {
247 dht_->query(key, cb, done_cb, std::move(q));
249 void query(
const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {})
override {
250 dht_->query(key, cb, done_cb, std::move(q));
253 return dht_->getLocal(key, f);
256 return dht_->getLocalById(key, vid);
260 DoneCallback cb=
nullptr,
261 time_point created=time_point::max(),
262 bool permanent =
false)
override
264 dht_->put(key, v, cb, created, permanent);
268 DoneCallbackSimple cb,
269 time_point created=time_point::max(),
270 bool permanent =
false)
override
272 dht_->put(key, v, cb, created, permanent);
275 void put(
const InfoHash& key,
277 DoneCallback cb=
nullptr,
278 time_point created=time_point::max(),
279 bool permanent =
false)
override
281 dht_->put(key, std::move(v), cb, created, permanent);
283 void put(
const InfoHash& key,
285 DoneCallbackSimple cb,
286 time_point created=time_point::max(),
287 bool permanent =
false)
override
289 dht_->put(key, std::move(v), cb, created, permanent);
292 return dht_->getPut(h);
295 return dht_->getPut(h, vid);
298 return dht_->cancelPut(h, vid);
303 size_t listen(
const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w = {})
override {
304 return listen(key, bindGetCb(cb), f, w);
306 bool cancelListen(
const InfoHash& h,
size_t token)
override {
307 return dht_->cancelListen(h, token);
310 dht_->connectivityChanged(af);
312 void connectivityChanged()
override {
313 dht_->connectivityChanged();
316 void forwardAllMessages(
bool forward) {
317 forward_all_ = forward;
320 void setPushNotificationToken(
const std::string& token =
"")
override {
321 dht_->setPushNotificationToken(token);
329 dht_->pushNotificationReceived(notification);
332 void setLogger(
const Logger& logger)
override {
333 DhtInterface::setLogger(logger);
334 dht_->setLogger(logger);
337 void setLogger(
const std::shared_ptr<Logger>& logger)
override {
338 DhtInterface::setLogger(logger);
339 dht_->setLogger(logger);
346 DhtInterface::setLogFilter(f);
347 dht_->setLogFilter(f);
351 std::unique_ptr<DhtInterface> dht_;
356 Sp<Value> checkValue(
const Sp<Value>& v);
357 ValueCallback getCallbackFilter(
const ValueCallback&,
Value::Filter&&);
358 GetCallback getCallbackFilter(
const GetCallback&,
Value::Filter&&);
360 Sp<crypto::PrivateKey> key_ {};
361 Sp<crypto::Certificate> certificate_ {};
364 CertificateStoreQuery localQueryMethod_ {};
367 std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
368 std::map<InfoHash, Sp<crypto::PublicKey>> nodesPubKeys_ {};
370 std::atomic_bool forward_all_ {
false};
371 bool enableCache_ {
false};
374const ValueType CERTIFICATE_TYPE = {
375 8,
"Certificate", std::chrono::hours(24 * 7),
377 [](InfoHash id, Sp<Value>& v,
const InfoHash&,
const SockAddr&) {
379 crypto::Certificate crt(v->data);
381 return crt.getPublicKey().getId() == id;
382 }
catch (
const std::exception& e) {}
385 [](InfoHash,
const Sp<Value>& o, Sp<Value>& n,
const InfoHash&,
const SockAddr&) {
387 return crypto::Certificate(o->data).getPublicKey().getId() == crypto::Certificate(n->data).getPublicKey().getId();
388 }
catch (
const std::exception& e) {}
void putSigned(const InfoHash &hash, Sp< Value > val, DoneCallback callback, bool permanent=false)
size_t listen(const InfoHash &key, GetCallback cb, Value::Filter={}, Where w={}) override
bool isRunning(sa_family_t af=0) const override
void insertNode(const InfoHash &id, const SockAddr &sa) override
void query(const InfoHash &key, QueryCallback cb, DoneCallback done_cb={}, Query &&q={}) override
void get(const InfoHash &id, GetCallback cb, DoneCallback donecb={}, Value::Filter &&={}, Where &&w={}) override
void sign(Value &v) const
SecureDht(std::unique_ptr< DhtInterface > dht, Config config, IdentityAnnouncedCb iacb={}, const std::shared_ptr< Logger > &l={})
void setLogFilter(const InfoHash &f) override
void shutdown(ShutdownCallback cb, bool stop=false) override
std::vector< NodeExport > exportNodes() const override
std::vector< Sp< Value > > getLocal(const InfoHash &key, const Value::Filter &f={}) const override
void pushNotificationReceived(const std::map< std::string, std::string > ¬ification) override
bool cancelPut(const InfoHash &h, const Value::Id &vid) override
std::vector< Sp< Value > > getPut(const InfoHash &h) const override
void put(const InfoHash &key, Sp< Value > v, DoneCallback cb=nullptr, time_point created=time_point::max(), bool permanent=false) override
std::pair< size_t, size_t > getStoreSize() const override
Sp< Value > getLocalById(const InfoHash &key, Value::Id vid) const override
void setStorageLimit(size_t limit=DEFAULT_STORAGE_LIMIT) override
Sp< Value > getPut(const InfoHash &h, const Value::Id &vid) const override
void putEncrypted(const InfoHash &hash, const InfoHash &to, Sp< Value > val, DoneCallback callback, bool permanent=false)
const InfoHash & getNodeId() const override
NodeStatus updateStatus(sa_family_t af) override
void setLocalCertificateStore(CertificateStoreQuery &&query_method)
void connectivityChanged(sa_family_t af) override
std::vector< uint8_t > Blob
Describes a query destined to another peer.
Serializable dht::Value filter.