gnunet_dht_service.h 15 KB

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