gnunet_dht_service.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2004-2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. *
  19. * @file
  20. * API to the DHT service
  21. *
  22. * @defgroup dht DHT service
  23. * Distributed Hash Table
  24. *
  25. * @see [Documentation](https://gnunet.org/developer-handbook-dht)
  26. *
  27. * @{
  28. */
  29. #ifndef GNUNET_DHT_SERVICE_H
  30. #define GNUNET_DHT_SERVICE_H
  31. #include "gnunet_util_lib.h"
  32. #include "gnunet_block_lib.h"
  33. #include "gnunet_hello_lib.h"
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #if 0 /* keep Emacsens' auto-indent happy */
  38. }
  39. #endif
  40. #endif
  41. /**
  42. * Default republication frequency for stored data in the DHT.
  43. */
  44. #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
  45. /**
  46. * Connection to the DHT service.
  47. */
  48. struct GNUNET_DHT_Handle;
  49. /**
  50. * Handle to control a get operation.
  51. */
  52. struct GNUNET_DHT_GetHandle;
  53. /**
  54. * Handle to control a find peer operation.
  55. */
  56. struct GNUNET_DHT_FindPeerHandle;
  57. /**
  58. * Options for routing.
  59. */
  60. enum GNUNET_DHT_RouteOption
  61. {
  62. /**
  63. * Default. Do nothing special.
  64. */
  65. GNUNET_DHT_RO_NONE = 0,
  66. /**
  67. * Each peer along the way should look at 'enc' (otherwise
  68. * only the k-peers closest to the key should look at it).
  69. */
  70. GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
  71. /**
  72. * We should keep track of the route that the message
  73. * took in the P2P network.
  74. */
  75. GNUNET_DHT_RO_RECORD_ROUTE = 2,
  76. /**
  77. * This is a 'FIND-PEER' request, so approximate results are fine.
  78. */
  79. GNUNET_DHT_RO_FIND_PEER = 4,
  80. /**
  81. * Possible message option for query key randomization.
  82. */
  83. GNUNET_DHT_RO_BART = 8,
  84. /**
  85. * Flag given to monitors if this was the last hop for a GET/PUT.
  86. */
  87. GNUNET_DHT_RO_LAST_HOP = 16
  88. };
  89. /**
  90. * Initialize the connection with the DHT service.
  91. *
  92. * @param cfg configuration to use
  93. * @param ht_len size of the internal hash table to use for
  94. * processing multiple GET/FIND requests in parallel
  95. * @return NULL on error
  96. */
  97. struct GNUNET_DHT_Handle *
  98. GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  99. unsigned int ht_len);
  100. /**
  101. * Shutdown connection with the DHT service.
  102. *
  103. * @param handle connection to shut down
  104. */
  105. void
  106. GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
  107. /* *************** Standard API: get and put ******************* */
  108. /**
  109. * Opaque handle to cancel a PUT operation.
  110. */
  111. struct GNUNET_DHT_PutHandle;
  112. /**
  113. * Perform a PUT operation storing data in the DHT.
  114. *
  115. * @param handle handle to DHT service
  116. * @param key the key to store under
  117. * @param desired_replication_level estimate of how many
  118. * nearest peers this request should reach
  119. * @param options routing options for this message
  120. * @param type type of the value
  121. * @param size number of bytes in @a data; must be less than 64k
  122. * @param data the data to store
  123. * @param exp desired expiration time for the value
  124. * @param cont continuation to call when done (transmitting request to service)
  125. * You must not call #GNUNET_DHT_disconnect in this continuation
  126. * @param cont_cls closure for @a cont
  127. * @return handle to cancel the "PUT" operation, NULL on error
  128. * (size too big)
  129. */
  130. struct GNUNET_DHT_PutHandle *
  131. GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
  132. const struct GNUNET_HashCode *key,
  133. uint32_t desired_replication_level,
  134. enum GNUNET_DHT_RouteOption options,
  135. enum GNUNET_BLOCK_Type type,
  136. size_t size,
  137. const void *data,
  138. struct GNUNET_TIME_Absolute exp,
  139. GNUNET_SCHEDULER_TaskCallback cont,
  140. void *cont_cls);
  141. /**
  142. * Cancels a DHT PUT operation. Note that the PUT request may still
  143. * go out over the network (we can't stop that); However, if the PUT
  144. * has not yet been sent to the service, cancelling the PUT will stop
  145. * this from happening (but there is no way for the user of this API
  146. * to tell if that is the case). The only use for this API is to
  147. * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
  148. * the system is shutting down).
  149. *
  150. * @param ph put operation to cancel ('cont' will no longer be called)
  151. */
  152. void
  153. GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
  154. /**
  155. * Iterator called on each result obtained for a DHT
  156. * operation that expects a reply
  157. *
  158. * @param cls closure
  159. * @param exp when will this value expire
  160. * @param key key of the result
  161. * @param get_path peers on reply path (or NULL if not recorded)
  162. * [0] = datastore's first neighbor, [length - 1] = local peer
  163. * @param get_path_length number of entries in @a get_path
  164. * @param put_path peers on the PUT path (or NULL if not recorded)
  165. * [0] = origin, [length - 1] = datastore
  166. * @param put_path_length number of entries in @a put_path
  167. * @param type type of the result
  168. * @param size number of bytes in @a data
  169. * @param data pointer to the result data
  170. */
  171. typedef void
  172. (*GNUNET_DHT_GetIterator) (void *cls,
  173. struct GNUNET_TIME_Absolute exp,
  174. const struct GNUNET_HashCode *key,
  175. const struct GNUNET_PeerIdentity *get_path,
  176. unsigned int get_path_length,
  177. const struct GNUNET_PeerIdentity *put_path,
  178. unsigned int put_path_length,
  179. enum GNUNET_BLOCK_Type type,
  180. size_t size,
  181. const void *data);
  182. /**
  183. * Perform an asynchronous GET operation on the DHT identified. See
  184. * also #GNUNET_BLOCK_evaluate.
  185. *
  186. * @param handle handle to the DHT service
  187. * @param type expected type of the response object
  188. * @param key the key to look up
  189. * @param desired_replication_level estimate of how many
  190. nearest peers this request should reach
  191. * @param options routing options for this message
  192. * @param xquery extended query data (can be NULL, depending on type)
  193. * @param xquery_size number of bytes in @a xquery
  194. * @param iter function to call on each result
  195. * @param iter_cls closure for @a iter
  196. * @return handle to stop the async get
  197. */
  198. struct GNUNET_DHT_GetHandle *
  199. GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
  200. enum GNUNET_BLOCK_Type type,
  201. const struct GNUNET_HashCode *key,
  202. uint32_t desired_replication_level,
  203. enum GNUNET_DHT_RouteOption options,
  204. const void *xquery,
  205. size_t xquery_size,
  206. GNUNET_DHT_GetIterator iter,
  207. void *iter_cls);
  208. /**
  209. * Tell the DHT not to return any of the following known results
  210. * to this client.
  211. *
  212. * @param get_handle get operation for which results should be filtered
  213. * @param num_results number of results to be blocked that are
  214. * provided in this call (size of the @a results array)
  215. * @param results array of hash codes over the `data` of the results
  216. * to be blocked
  217. */
  218. void
  219. GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
  220. unsigned int num_results,
  221. const struct GNUNET_HashCode *results);
  222. /**
  223. * Stop async DHT-get. Frees associated resources.
  224. *
  225. * @param get_handle GET operation to stop.
  226. */
  227. void
  228. GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
  229. /* *************** Extended API: monitor ******************* */
  230. /**
  231. * Handle to monitor requests
  232. */
  233. struct GNUNET_DHT_MonitorHandle;
  234. /**
  235. * Callback called on each GET request going through the DHT.
  236. *
  237. * @param cls Closure.
  238. * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
  239. * @param type The type of data in the request.
  240. * @param hop_count Hop count so far.
  241. * @param path_length number of entries in @a path (or 0 if not recorded).
  242. * @param path peers on the GET path (or NULL if not recorded).
  243. * @param desired_replication_level Desired replication level.
  244. * @param key Key of the requested data.
  245. */
  246. typedef void
  247. (*GNUNET_DHT_MonitorGetCB) (void *cls,
  248. enum GNUNET_DHT_RouteOption options,
  249. enum GNUNET_BLOCK_Type type,
  250. uint32_t hop_count,
  251. uint32_t desired_replication_level,
  252. unsigned int path_length,
  253. const struct GNUNET_PeerIdentity *path,
  254. const struct GNUNET_HashCode *key);
  255. /**
  256. * Callback called on each GET reply going through the DHT.
  257. *
  258. * @param cls Closure.
  259. * @param type The type of data in the result.
  260. * @param get_path Peers on GET path (or NULL if not recorded).
  261. * @param get_path_length number of entries in @a get_path.
  262. * @param put_path peers on the PUT path (or NULL if not recorded).
  263. * @param put_path_length number of entries in @a get_path.
  264. * @param exp Expiration time of the data.
  265. * @param key Key of the data.
  266. * @param data Pointer to the result data.
  267. * @param size Number of bytes in @a data.
  268. */
  269. typedef void
  270. (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
  271. enum GNUNET_BLOCK_Type type,
  272. const struct GNUNET_PeerIdentity *get_path,
  273. unsigned int get_path_length,
  274. const struct GNUNET_PeerIdentity *put_path,
  275. unsigned int put_path_length,
  276. struct GNUNET_TIME_Absolute exp,
  277. const struct GNUNET_HashCode *key,
  278. const void *data,
  279. size_t size);
  280. /**
  281. * Callback called on each PUT request going through the DHT.
  282. *
  283. * @param cls Closure.
  284. * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
  285. * @param type The type of data in the request.
  286. * @param hop_count Hop count so far.
  287. * @param path_length number of entries in @a path (or 0 if not recorded).
  288. * @param path peers on the PUT path (or NULL if not recorded).
  289. * @param desired_replication_level Desired replication level.
  290. * @param exp Expiration time of the data.
  291. * @param key Key under which data is to be stored.
  292. * @param data Pointer to the data carried.
  293. * @param size Number of bytes in @a data.
  294. */
  295. typedef void
  296. (*GNUNET_DHT_MonitorPutCB) (void *cls,
  297. enum GNUNET_DHT_RouteOption options,
  298. enum GNUNET_BLOCK_Type type,
  299. uint32_t hop_count,
  300. uint32_t desired_replication_level,
  301. unsigned int path_length,
  302. const struct GNUNET_PeerIdentity *path,
  303. struct GNUNET_TIME_Absolute exp,
  304. const struct GNUNET_HashCode *key,
  305. const void *data,
  306. size_t size);
  307. /**
  308. * Start monitoring the local DHT service.
  309. *
  310. * @param handle Handle to the DHT service.
  311. * @param type Type of blocks that are of interest.
  312. * @param key Key of data of interest, NULL for all.
  313. * @param get_cb Callback to process monitored get messages.
  314. * @param get_resp_cb Callback to process monitored get response messages.
  315. * @param put_cb Callback to process monitored put messages.
  316. * @param cb_cls Closure for callbacks
  317. * @return Handle to stop monitoring.
  318. */
  319. struct GNUNET_DHT_MonitorHandle *
  320. GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
  321. enum GNUNET_BLOCK_Type type,
  322. const struct GNUNET_HashCode *key,
  323. GNUNET_DHT_MonitorGetCB get_cb,
  324. GNUNET_DHT_MonitorGetRespCB get_resp_cb,
  325. GNUNET_DHT_MonitorPutCB put_cb,
  326. void *cb_cls);
  327. /**
  328. * Stop monitoring.
  329. * On return handle will no longer be valid, caller must not use it anymore.
  330. *
  331. * @param handle The handle to the monitor request returned by
  332. * #GNUNET_DHT_monitor_start().
  333. */
  334. void
  335. GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
  336. #if 0 /* keep Emacsens' auto-indent happy */
  337. {
  338. #endif
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif
  343. /** @} */ /* end of group dht */