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());
61 return key_ ? key_->getPublicKey().getId() :
InfoHash();
63 PkId getLongId()
const {
64 return key_ ? key_->getPublicKey().getLongId() :
PkId();
71 return secureType(std::move(tmp_type));
74 void registerType(
const ValueType& type)
override {
76 dht_->registerType(secureType(type));
80 dht_->registerType(secureType(std::forward<ValueType>(type)));
82 void registerInsecureType(
const ValueType& type) {
84 dht_->registerType(type);
94 get(id, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
97 get(key, bindGetCb(cb), donecb, std::forward<Value::Filter>(f), std::forward<Where>(w));
99 void get(
const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb,
Value::Filter&& f={},
Where&& w = {})
override {
100 get(key, bindGetCb(cb), bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
106 void putSigned(
const InfoHash& hash, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
107 void putSigned(
const InfoHash& hash,
Value&& v, DoneCallback callback,
bool permanent =
false) {
108 putSigned(hash, std::make_shared<Value>(std::move(v)), callback, permanent);
116 void putEncrypted(
const InfoHash& hash,
const InfoHash& to, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
117 void putEncrypted(
const InfoHash& hash,
const InfoHash& to,
Value&& v, DoneCallback callback,
bool permanent =
false) {
118 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
124 void sign(
Value& v)
const;
130 void findCertificate(
const InfoHash& node, std::function<
void(
const Sp<crypto::Certificate>)> cb);
131 void findPublicKey(
const InfoHash& node, std::function<
void(
const Sp<const crypto::PublicKey>)> cb);
133 const Sp<crypto::Certificate> registerCertificate(
const InfoHash& node,
const Blob& cert);
134 void registerCertificate(Sp<crypto::Certificate>& cert);
136 const Sp<crypto::Certificate> getCertificate(
const InfoHash& node)
const;
137 const Sp<const crypto::PublicKey> getPublicKey(
const InfoHash& node)
const;
145 localQueryMethod_ = std::move(query_method);
154 void dumpTables()
const override {
160 return dht_->getStoreSize();
162 std::string getStorageLog()
const override {
163 return dht_->getStorageLog();
165 std::string getStorageLog(
const InfoHash& h)
const override {
166 return dht_->getStorageLog(h);
169 dht_->setStorageLimit(limit);
172 return dht_->exportNodes();
174 std::vector<ValuesExport> exportValues()
const override {
175 return dht_->exportValues();
177 void importValues(
const std::vector<ValuesExport>& v)
override {
178 dht_->importValues(v);
180 NodeStats getNodesStats(sa_family_t af)
const override {
181 return dht_->getNodesStats(af);
183 std::vector<unsigned> getNodeMessageStats(
bool in =
false)
override {
184 return dht_->getNodeMessageStats(in);
186 std::string getRoutingTablesLog(sa_family_t af)
const override {
187 return dht_->getRoutingTablesLog(af);
189 std::string getSearchesLog(sa_family_t af)
const override {
190 return dht_->getSearchesLog(af);
192 std::string getSearchLog(
const InfoHash& h, sa_family_t af = AF_UNSPEC)
const override {
193 return dht_->getSearchLog(h, af);
195 std::vector<SockAddr> getPublicAddress(sa_family_t family = 0)
override {
196 return dht_->getPublicAddress(family);
198 time_point periodic(
const uint8_t *buf,
size_t buflen,
const SockAddr& sa)
override {
199 return dht_->periodic(buf, buflen, sa);
201 time_point periodic(
const uint8_t *buf,
size_t buflen,
const sockaddr* from, socklen_t fromlen)
override {
202 return dht_->periodic(buf, buflen, from, fromlen);
205 return dht_->getStatus(af);
208 return dht_->getStatus();
211 return dht_->isRunning(af);
213 const ValueType& getType(ValueType::Id type_id)
const override {
214 return dht_->getType(type_id);
217 dht_->insertNode(
id, sa);
219 void insertNode(
const InfoHash&
id,
const sockaddr* sa, socklen_t salen)
override {
220 dht_->insertNode(
id, sa, salen);
222 void insertNode(
const NodeExport& n)
override {
225 void pingNode(
const sockaddr* sa, socklen_t salen, DoneCallbackSimple&& cb={})
override {
226 dht_->pingNode(sa, salen, std::move(cb));
228 void query(
const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {},
Query&& q = {})
override {
229 dht_->query(key, cb, done_cb, std::move(q));
231 void query(
const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {})
override {
232 dht_->query(key, cb, done_cb, std::move(q));
235 return dht_->getLocal(key, f);
238 return dht_->getLocalById(key, vid);
242 DoneCallback cb=
nullptr,
243 time_point created=time_point::max(),
244 bool permanent =
false)
override 246 dht_->put(key, v, cb, created, permanent);
250 DoneCallbackSimple cb,
251 time_point created=time_point::max(),
252 bool permanent =
false)
override 254 dht_->put(key, v, cb, created, permanent);
257 void put(
const InfoHash& key,
259 DoneCallback cb=
nullptr,
260 time_point created=time_point::max(),
261 bool permanent =
false)
override 263 dht_->put(key, std::move(v), cb, created, permanent);
265 void put(
const InfoHash& key,
267 DoneCallbackSimple cb,
268 time_point created=time_point::max(),
269 bool permanent =
false)
override 271 dht_->put(key, std::move(v), cb, created, permanent);
274 return dht_->getPut(h);
277 return dht_->getPut(h, vid);
280 return dht_->cancelPut(h, vid);
284 size_t listen(
const InfoHash& key, GetCallback cb, Value::Filter = {}, Where w = {})
override;
285 size_t listen(
const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w = {})
override {
286 return listen(key, bindGetCb(cb), f, w);
288 bool cancelListen(
const InfoHash& h,
size_t token)
override {
289 return dht_->cancelListen(h, token);
292 dht_->connectivityChanged(af);
294 void connectivityChanged()
override {
295 dht_->connectivityChanged();
298 void forwardAllMessages(
bool forward) {
299 forward_all_ = forward;
302 void setPushNotificationToken(
const std::string& token =
"")
override {
303 dht_->setPushNotificationToken(token);
311 dht_->pushNotificationReceived(notification);
317 dht_->setLoggers(error, warn, debug);
324 DHT_LOG.setFilter(f);
325 dht_->setLogFilter(f);
329 std::unique_ptr<DhtInterface> dht_;
334 Sp<Value> checkValue(
const Sp<Value>& v);
335 ValueCallback getCallbackFilter(ValueCallback,
Value::Filter&&);
338 Sp<crypto::PrivateKey> key_ {};
339 Sp<crypto::Certificate> certificate_ {};
342 CertificateStoreQuery localQueryMethod_ {};
345 std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
346 std::map<InfoHash, Sp<const crypto::PublicKey>> nodesPubKeys_ {};
348 std::atomic_bool forward_all_ {
false};
351 const ValueType CERTIFICATE_TYPE = {
352 8,
"Certificate", std::chrono::hours(24 * 7),
354 [](InfoHash id, Sp<Value>& v,
const InfoHash&,
const SockAddr&) {
356 crypto::Certificate crt(v->data);
358 return crt.getPublicKey().getId() == id;
359 }
catch (
const std::exception& e) {}
362 [](InfoHash,
const Sp<Value>& o, Sp<Value>& n,
const InfoHash&,
const SockAddr&) {
364 return crypto::Certificate(o->data).getPublicKey().getId() == crypto::Certificate(n->data).getPublicKey().getId();
365 }
catch (
const std::exception& e) {}
std::vector< Sp< Value > > getLocal(const InfoHash &key, Value::Filter f=Value::AllFilter()) const override
void NOLOG(char const *, va_list)
const InfoHash & getNodeId() const override
std::vector< NodeExport > exportNodes() override
void connectivityChanged(sa_family_t af) override
void setLocalCertificateStore(CertificateStoreQuery &&query_method)
void insertNode(const InfoHash &id, const SockAddr &sa) override
void setLogFilter(const InfoHash &f) override
std::vector< Sp< Value > > getPut(const InfoHash &h) override
void shutdown(ShutdownCallback cb) override
std::vector< uint8_t > Blob
Sp< Value > getPut(const InfoHash &h, const Value::Id &vid) override
std::pair< size_t, size_t > getStoreSize() const override
NodeStatus getStatus(sa_family_t af) const override
void setStorageLimit(size_t limit=DEFAULT_STORAGE_LIMIT) override
void pushNotificationReceived(const std::map< std::string, std::string > ¬ification) override
bool cancelPut(const InfoHash &h, const Value::Id &vid) override
Describes a query destined to another peer.
void query(const InfoHash &key, QueryCallback cb, DoneCallback done_cb={}, Query &&q={}) override
Serializable dht::Value filter.
void setLoggers(LogMethod error=NOLOG, LogMethod warn=NOLOG, LogMethod debug=NOLOG) override
virtual void setLoggers(LogMethod error=NOLOG, LogMethod warn=NOLOG, LogMethod debug=NOLOG)
void put(const InfoHash &key, Sp< Value > v, DoneCallback cb=nullptr, time_point created=time_point::max(), bool permanent=false) override
Sp< Value > getLocalById(const InfoHash &key, Value::Id vid) const override
bool isRunning(sa_family_t af=0) const override