C++ Distributed Hash Table
callbacks.h
1 /*
2  * Copyright (C) 2014-2022 Savoir-faire Linux Inc.
3  * Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4  * Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
5  * Sébastien Blin <sebastien.blin@savoirfairelinux.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include "infohash.h"
24 #include "value.h"
25 
26 #include <vector>
27 #include <memory>
28 #include <functional>
29 #include <string>
30 
31 #ifdef OPENDHT_JSONCPP
32 #include <json/json.h>
33 #endif
34 
35 namespace dht {
36 
37 struct Node;
38 
42 enum class NodeStatus {
43  Disconnected, // 0 nodes
44  Connecting, // 1+ nodes
45  Connected // 1+ good nodes
46 };
47 
48 inline constexpr const char*
49 statusToStr(NodeStatus status) {
50  return status == NodeStatus::Connected ? "connected" : (
51  status == NodeStatus::Connecting ? "connecting" :
52  "disconnected");
53 }
54 
55 struct OPENDHT_PUBLIC NodeStats {
56  unsigned good_nodes {0},
57  dubious_nodes {0},
58  cached_nodes {0},
59  incoming_nodes {0};
60  unsigned table_depth {0};
61  unsigned searches {0};
62  unsigned node_cache_size {0};
63  unsigned getKnownNodes() const { return good_nodes + dubious_nodes; }
64  unsigned long getNetworkSizeEstimation() const { return 8 * std::exp2(table_depth); }
65  std::string toString() const;
66 
67 #ifdef OPENDHT_JSONCPP
68 
71  Json::Value toJson() const;
72  NodeStats() {};
73  explicit NodeStats(const Json::Value& v);
74 #endif
75 
76  MSGPACK_DEFINE_MAP(good_nodes, dubious_nodes, cached_nodes, incoming_nodes, table_depth, searches, node_cache_size)
77 };
78 
79 struct OPENDHT_PUBLIC NodeInfo {
80  InfoHash id;
81  InfoHash node_id;
82  NodeStats ipv4 {};
83  NodeStats ipv6 {};
84  size_t ongoing_ops {0};
85  size_t storage_values {0};
86  size_t storage_size {0};
87  in_port_t bound4 {0};
88  in_port_t bound6 {0};
89 
90 #ifdef OPENDHT_JSONCPP
91 
94  Json::Value toJson() const;
95  NodeInfo() {};
96  explicit NodeInfo(const Json::Value& v);
97 #endif
98 
99  MSGPACK_DEFINE_MAP(id, node_id, ipv4, ipv6)
100 };
101 
105 struct OPENDHT_PUBLIC Config {
107  InfoHash node_id {};
108 
114  NetId network {0};
115 
117  bool is_bootstrap {false};
118 
120  bool maintain_storage {false};
121 
123  std::string persist_path {};
124 
126  ssize_t max_req_per_sec {0};
127 
129  ssize_t max_peer_req_per_sec {0};
130 
131  /* If non-0, overrides the default maximum number of searches. -1 means no limit. */
132  ssize_t max_searches {0};
133 
134  /* If non-0, overrides the default maximum store size. -1 means no limit. */
135  ssize_t max_store_size {0};
136 
137  /* If non-0, overrides the default maximum store key count. -1 means no limit. */
138  ssize_t max_store_keys {0};
139 
145  bool public_stable {false};
146 };
147 
151 struct OPENDHT_PUBLIC SecureDhtConfig
152 {
153  Config node_config {};
154  crypto::Identity id {};
155 
160  bool cert_cache_all {false};
161 };
162 
163 static constexpr size_t DEFAULT_STORAGE_LIMIT {1024 * 1024 * 64};
164 
165 using ValuesExport = std::pair<InfoHash, Blob>;
166 
167 using QueryCallback = std::function<bool(const std::vector<std::shared_ptr<FieldValueIndex>>& fields)>;
168 using GetCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values)>;
169 using ValueCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired)>;
170 using GetCallbackSimple = std::function<bool(std::shared_ptr<Value> value)>;
171 using ShutdownCallback = std::function<void()>;
172 using IdentityAnnouncedCb = std::function<void(bool)>;
173 
174 using CertificateStoreQuery = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
175 
176 typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void *user_data);
177 typedef bool (*ValueCallbackRaw)(std::shared_ptr<Value>, bool expired, void *user_data);
178 
179 using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>;
180 typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void *user_data);
181 typedef void (*ShutdownCallbackRaw)(void *user_data);
182 typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data);
183 typedef bool (*FilterRaw)(const Value&, void *user_data);
184 
185 using DoneCallbackSimple = std::function<void(bool success)>;
186 
187 OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data);
188 OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb);
189 OPENDHT_PUBLIC ValueCallback bindValueCb(ValueCallbackRaw raw_cb, void* user_data);
190 OPENDHT_PUBLIC ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data);
191 OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackSimple donecb);
192 OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data);
193 OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data);
194 OPENDHT_PUBLIC Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data);
195 
196 }
Definition: node.h:47
NodeStatus
Definition: callbacks.h:42
Definition: callbacks.h:35