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();
67 ValueType secureType(ValueType&& type);
69 ValueType secureType(
const ValueType& type) {
70 ValueType tmp_type = type;
71 return secureType(std::move(tmp_type));
74 void registerType(
const ValueType& type) {
76 dht_->registerType(secureType(type));
78 void registerType(ValueType&& type) {
80 dht_->registerType(secureType(std::forward<ValueType>(type)));
82 void registerInsecureType(
const ValueType& type) {
84 dht_->registerType(type);
93 void get(
const InfoHash&
id, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f = {}, Where&& w = {}) {
94 get(
id, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
96 void get(
const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, 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 = {}) {
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);
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);
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 {
159 return dht_->getStoreSize();
161 std::string getStorageLog()
const {
162 return dht_->getStorageLog();
164 std::string getStorageLog(
const InfoHash& h)
const {
165 return dht_->getStorageLog(h);
168 dht_->setStorageLimit(limit);
171 return dht_->exportNodes();
173 std::vector<ValuesExport> exportValues()
const {
174 return dht_->exportValues();
176 void importValues(
const std::vector<ValuesExport>& v) {
177 dht_->importValues(v);
179 NodeStats getNodesStats(sa_family_t af)
const {
180 return dht_->getNodesStats(af);
182 std::vector<unsigned> getNodeMessageStats(
bool in =
false) {
183 return dht_->getNodeMessageStats(in);
185 std::string getRoutingTablesLog(sa_family_t af)
const {
186 return dht_->getRoutingTablesLog(af);
188 std::string getSearchesLog(sa_family_t af)
const {
189 return dht_->getSearchesLog(af);
191 std::string getSearchLog(
const InfoHash& h, sa_family_t af = AF_UNSPEC)
const {
192 return dht_->getSearchLog(h, af);
194 std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) {
195 return dht_->getPublicAddress(family);
197 time_point periodic(
const uint8_t *buf,
size_t buflen,
const SockAddr& sa) {
198 return dht_->periodic(buf, buflen, sa);
200 time_point periodic(
const uint8_t *buf,
size_t buflen,
const sockaddr* from, socklen_t fromlen) {
201 return dht_->periodic(buf, buflen, from, fromlen);
204 return dht_->getStatus(af);
207 return dht_->getStatus();
210 return dht_->isRunning(af);
212 const ValueType& getType(ValueType::Id type_id)
const {
213 return dht_->getType(type_id);
216 dht_->insertNode(
id, sa);
218 void insertNode(
const InfoHash&
id,
const sockaddr* sa, socklen_t salen) {
219 dht_->insertNode(
id, sa, salen);
221 void insertNode(
const NodeExport& n) {
224 void pingNode(
const sockaddr* sa, socklen_t salen, DoneCallbackSimple&& cb={}) {
225 dht_->pingNode(sa, salen, std::move(cb));
228 dht_->query(key, cb, done_cb, std::move(q));
230 void query(
const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) {
231 dht_->query(key, cb, done_cb, std::move(q));
234 return dht_->getLocal(key, f);
237 return dht_->getLocalById(key, vid);
241 DoneCallback cb=
nullptr,
242 time_point created=time_point::max(),
243 bool permanent =
false)
245 dht_->put(key, v, cb, created, permanent);
249 DoneCallbackSimple cb,
250 time_point created=time_point::max(),
251 bool permanent =
false)
253 dht_->put(key, v, cb, created, permanent);
256 void put(
const InfoHash& key,
258 DoneCallback cb=
nullptr,
259 time_point created=time_point::max(),
260 bool permanent =
false)
262 dht_->put(key, std::move(v), cb, created, permanent);
264 void put(
const InfoHash& key,
266 DoneCallbackSimple cb,
267 time_point created=time_point::max(),
268 bool permanent =
false)
270 dht_->put(key, std::move(v), cb, created, permanent);
273 return dht_->getPut(h);
276 return dht_->getPut(h, vid);
279 return dht_->cancelPut(h, vid);
284 size_t listen(
const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w = {}) {
285 return listen(key, bindGetCb(cb), f, w);
287 bool cancelListen(
const InfoHash& h,
size_t token) {
288 return dht_->cancelListen(h, token);
291 dht_->connectivityChanged(af);
293 void connectivityChanged() {
294 dht_->connectivityChanged();
297 void forwardAllMessages(
bool forward) {
298 forward_all_ = forward;
301 void setPushNotificationToken(
const std::string& token =
"") {
302 dht_->setPushNotificationToken(token);
310 dht_->pushNotificationReceived(notification);
315 DHT_LOG.DEBUG = debug;
318 dht_->setLoggers(error, warn, debug);
325 DHT_LOG.setFilter(f);
326 dht_->setLogFilter(f);
330 std::unique_ptr<DhtInterface> dht_;
335 Sp<Value> checkValue(
const Sp<Value>& v);
336 ValueCallback getCallbackFilter(ValueCallback,
Value::Filter&&);
339 Sp<crypto::PrivateKey> key_ {};
340 Sp<crypto::Certificate> certificate_ {};
343 CertificateStoreQuery localQueryMethod_ {};
346 std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
347 std::map<InfoHash, Sp<const crypto::PublicKey>> nodesPubKeys_ {};
349 std::atomic_bool forward_all_ {
false};
352const ValueType CERTIFICATE_TYPE = {
353 8,
"Certificate", std::chrono::hours(24 * 7),
355 [](InfoHash id, Sp<Value>& v,
const InfoHash&,
const SockAddr&) {
357 crypto::Certificate crt(v->data);
359 return crt.getPublicKey().getId() == id;
360 }
catch (
const std::exception& e) {}
363 [](InfoHash,
const Sp<Value>& o, Sp<Value>& n,
const InfoHash&,
const SockAddr&) {
365 return crypto::Certificate(o->data).getPublicKey().getId() == crypto::Certificate(n->data).getPublicKey().getId();
366 }
catch (
const std::exception& e) {}
size_t listen(const InfoHash &key, GetCallback cb, Value::Filter={}, Where w={})
void setLoggers(LogMethod error=NOLOG, LogMethod warn=NOLOG, LogMethod debug=NOLOG)
void setStorageLimit(size_t limit=DEFAULT_STORAGE_LIMIT)
std::vector< Sp< Value > > getLocal(const InfoHash &key, Value::Filter f=Value::AllFilter()) const
void putSigned(const InfoHash &hash, Sp< Value > val, DoneCallback callback, bool permanent=false)
bool isRunning(sa_family_t af=0) const
void shutdown(ShutdownCallback cb)
SecureDht(std::unique_ptr< DhtInterface > dht, Config config)
std::pair< size_t, size_t > getStoreSize() const
void insertNode(const InfoHash &id, const SockAddr &sa)
void sign(Value &v) const
void connectivityChanged(sa_family_t af)
NodeStatus getStatus(sa_family_t af) const
Sp< Value > getLocalById(const InfoHash &key, Value::Id vid) const
std::vector< NodeExport > exportNodes()
void pushNotificationReceived(const std::map< std::string, std::string > ¬ification)
void setLogFilter(const InfoHash &f)
void put(const InfoHash &key, Sp< Value > v, DoneCallback cb=nullptr, time_point created=time_point::max(), bool permanent=false)
void get(const InfoHash &id, GetCallback cb, DoneCallback donecb={}, Value::Filter &&={}, Where &&w={})
void putEncrypted(const InfoHash &hash, const InfoHash &to, Sp< Value > val, DoneCallback callback, bool permanent=false)
void query(const InfoHash &key, QueryCallback cb, DoneCallback done_cb={}, Query &&q={})
Sp< Value > getPut(const InfoHash &h, const Value::Id &vid)
bool cancelPut(const InfoHash &h, const Value::Id &vid)
void setLocalCertificateStore(CertificateStoreQuery &&query_method)
const InfoHash & getNodeId() const
std::vector< Sp< Value > > getPut(const InfoHash &h)
void NOLOG(char const *, va_list)
std::vector< uint8_t > Blob
Describes a query destined to another peer.
Serializable dht::Value filter.