My Project 3.7.1
C++ Distributed Hash Table
Loading...
Searching...
No Matches
callbacks.h
1// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "infohash.h"
6#include "value.h"
7
8#include <vector>
9#include <memory>
10#include <functional>
11#include <string>
12
13#ifdef OPENDHT_JSONCPP
14#include <json/json.h>
15#endif
16
17namespace dht {
18
19struct Node;
20
24enum class NodeStatus {
25 Disconnected, // 0 nodes
26 Connecting, // 1+ nodes
27 Connected // 1+ good nodes
28};
29
30inline constexpr const char*
31statusToStr(NodeStatus status)
32{
33 return status == NodeStatus::Connected ? "connected"
34 : (status == NodeStatus::Connecting ? "connecting" : "disconnected");
35}
36
37struct OPENDHT_PUBLIC NodeStats
38{
39 unsigned good_nodes {0}, dubious_nodes {0}, cached_nodes {0}, incoming_nodes {0};
40 unsigned table_depth {0};
41 unsigned searches {0};
42 unsigned node_cache_size {0};
43 unsigned getKnownNodes() const { return good_nodes + dubious_nodes; }
44 unsigned long getNetworkSizeEstimation() const { return 8 * std::exp2(table_depth); }
45 std::string toString() const;
46
47#ifdef OPENDHT_JSONCPP
51 Json::Value toJson() const;
52 NodeStats() {};
53 explicit NodeStats(const Json::Value& v);
54#endif
55
56 MSGPACK_DEFINE_MAP(good_nodes, dubious_nodes, cached_nodes, incoming_nodes, table_depth, searches, node_cache_size)
57};
58
59struct OPENDHT_PUBLIC NodeInfo
60{
61 InfoHash id;
62 InfoHash node_id;
63 NodeStats ipv4 {};
64 NodeStats ipv6 {};
65 size_t ongoing_ops {0};
66 size_t storage_values {0};
67 size_t storage_size {0};
68 size_t local_storage_values {0};
69 size_t local_storage_size {0};
70 in_port_t bound4 {0};
71 in_port_t bound6 {0};
72
73#ifdef OPENDHT_JSONCPP
77 Json::Value toJson() const;
78 NodeInfo() {};
79 explicit NodeInfo(const Json::Value& v);
80#endif
81
82 MSGPACK_DEFINE_MAP(id,
83 node_id,
84 ipv4,
85 ipv6,
86 ongoing_ops,
87 storage_values,
88 storage_size,
89 local_storage_values,
90 local_storage_size,
91 bound4,
92 bound6)
93};
94
98struct OPENDHT_PUBLIC Config
99{
101 InfoHash node_id {};
102
108 NetId network {0};
109
111 bool is_bootstrap {false};
112
114 bool maintain_storage {false};
115
117 std::string persist_path {};
118
120 ssize_t max_req_per_sec {0};
121
124
125 /* If non-0, overrides the default maximum number of searches. -1 means no limit. */
126 ssize_t max_searches {0};
127
128 /* If non-0, overrides the default maximum store size for remote values. -1 means no limit. */
129 ssize_t max_store_size {0};
130
131 /* If non-0, overrides the default maximum store size for locally stored values. -1 means no limit. */
132 ssize_t max_local_store_size {0};
133
134 /* If non-0, overrides the default maximum store key count. -1 means no limit. */
135 ssize_t max_store_keys {0};
136
142 bool public_stable {false};
143
144 /* Client mode, node will not be used by other nodes to store data. */
145 bool client_mode {false};
146};
147
151struct OPENDHT_PUBLIC SecureDhtConfig
152{
153 Config node_config {};
154 crypto::Identity id {};
155
160 bool cert_cache_all {false};
161};
162
163enum class OPENDHT_PUBLIC PushNotificationResult : uint8_t {
164 /* success codes */
165 PutRefresh,
166 ListenRefresh,
167 Values,
168 ValuesExpired,
169 /* ignored/error codes */
170 IgnoredWrongSession = 0x80,
171 IgnoredNoOp,
172 IgnoredStopped,
173 IgnoredDisabled,
174 Error
175};
176
177static constexpr size_t STORAGE_LIMIT_DEFAULT {1024 * 1024 * 64};
178static constexpr size_t STORAGE_LIMIT_UNLIMITED {(size_t) -1};
179
180using ValuesExport = std::pair<InfoHash, Blob>;
181
182using QueryCallback = std::function<bool(const std::vector<std::shared_ptr<FieldValueIndex>>& fields)>;
183using GetCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values)>;
184using ValueCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired)>;
185using GetCallbackSimple = std::function<bool(std::shared_ptr<Value> value)>;
186using ShutdownCallback = std::function<void()>;
187using IdentityAnnouncedCb = std::function<void(bool)>;
188using PublicAddressChangedCb = std::function<void(std::vector<SockAddr>)>;
189
190using CertificateStoreQueryLegacy
191 = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
192using CertificateStoreQuery = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const PkId& pk_id)>;
193
194using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>;
195using DoneCallbackSimple = std::function<void(bool success)>;
196
197typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void* user_data);
198typedef bool (*ValueCallbackRaw)(std::shared_ptr<Value>, bool expired, void* user_data);
199typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void* user_data);
200typedef void (*ShutdownCallbackRaw)(void* user_data);
201typedef void (*DoneCallbackSimpleRaw)(bool, void* user_data);
202typedef bool (*FilterRaw)(const Value&, void* user_data);
203
204OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data);
205OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb);
206OPENDHT_PUBLIC ValueCallback bindValueCb(ValueCallbackRaw raw_cb, void* user_data);
207OPENDHT_PUBLIC ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data);
208OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackSimple donecb);
209OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data);
210OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data);
211OPENDHT_PUBLIC Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data);
212
213} // namespace dht
NodeStatus
Definition callbacks.h:24
bool is_bootstrap
Definition callbacks.h:111
ssize_t max_peer_req_per_sec
Definition callbacks.h:123
std::string persist_path
Definition callbacks.h:117
bool public_stable
Definition callbacks.h:142
NetId network
Definition callbacks.h:108
InfoHash node_id
Definition callbacks.h:101
ssize_t max_req_per_sec
Definition callbacks.h:120
bool maintain_storage
Definition callbacks.h:114