gnunet_namestore_service.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2012, 2013, 2018 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 that can be used to store naming information on a GNUnet node;
  21. *
  22. * @defgroup namestore Name Store service
  23. * Store naming information on a GNUnet node.
  24. *
  25. * Naming information can either be records for which this peer/user is
  26. * authoritative, or blocks which are cached, encrypted naming data from other
  27. * peers.
  28. *
  29. * @see [Documentation](https://gnunet.org/namestore-subsystem)
  30. *
  31. * @{
  32. */
  33. #ifndef GNUNET_NAMESTORE_SERVICE_H
  34. #define GNUNET_NAMESTORE_SERVICE_H
  35. #include "gnunet_util_lib.h"
  36. #include "gnunet_block_lib.h"
  37. #include "gnunet_gnsrecord_lib.h"
  38. #ifdef __cplusplus
  39. extern "C"
  40. {
  41. #if 0 /* keep Emacsens' auto-indent happy */
  42. }
  43. #endif
  44. #endif
  45. /**
  46. * Entry in the queue.
  47. */
  48. struct GNUNET_NAMESTORE_QueueEntry;
  49. /**
  50. * Handle to the namestore service.
  51. */
  52. struct GNUNET_NAMESTORE_Handle;
  53. /**
  54. * Handle to the namestore zone iterator.
  55. */
  56. struct GNUNET_NAMESTORE_ZoneIterator;
  57. /**
  58. * Connect to the namestore service.
  59. *
  60. * @param cfg configuration to use
  61. * @return handle to use to access the service
  62. */
  63. struct GNUNET_NAMESTORE_Handle *
  64. GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
  65. /**
  66. * Disconnect from the namestore service (and free associated
  67. * resources). Must not be called from within operation callbacks of
  68. * the API.
  69. *
  70. * @param h handle to the namestore
  71. */
  72. void
  73. GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
  74. /**
  75. * Continuation called to notify client about result of the
  76. * operation.
  77. *
  78. * @param cls closure
  79. * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
  80. * #GNUNET_NO if content was already there or not found
  81. * #GNUNET_YES (or other positive value) on success
  82. * @param emsg NULL on success, otherwise an error message
  83. */
  84. typedef void
  85. (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
  86. int32_t success,
  87. const char *emsg);
  88. /**
  89. * Store an item in the namestore. If the item is already present,
  90. * it is replaced with the new record. Use an empty array to
  91. * remove all records under the given name.
  92. *
  93. * The continuation is called after the value has been stored in the
  94. * database. Monitors may be notified asynchronously (basically with
  95. * a buffer). However, if any monitor is consistently too slow to
  96. * keep up with the changes, calling @a cont will be delayed until the
  97. * monitors do keep up.
  98. *
  99. * @param h handle to the namestore
  100. * @param pkey private key of the zone
  101. * @param label name that is being mapped
  102. * @param rd_count number of records in the 'rd' array
  103. * @param rd array of records with data to store
  104. * @param cont continuation to call when done
  105. * @param cont_cls closure for @a cont
  106. * @return handle to abort the request
  107. */
  108. struct GNUNET_NAMESTORE_QueueEntry *
  109. GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
  110. const struct
  111. GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  112. const char *label,
  113. unsigned int rd_count,
  114. const struct GNUNET_GNSRECORD_Data *rd,
  115. GNUNET_NAMESTORE_ContinuationWithStatus cont,
  116. void *cont_cls);
  117. /**
  118. * Process a record that was stored in the namestore.
  119. *
  120. * @param cls closure
  121. * @param zone private key of the zone
  122. * @param label label of the records
  123. * @param rd_count number of entries in @a rd array, 0 if label was deleted
  124. * @param rd array of records with data to store
  125. */
  126. typedef void
  127. (*GNUNET_NAMESTORE_RecordMonitor) (void *cls,
  128. const struct
  129. GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  130. const char *label,
  131. unsigned int rd_count,
  132. const struct GNUNET_GNSRECORD_Data *rd);
  133. /**
  134. * Set the desired nick name for a zone
  135. *
  136. * @param h handle to the namestore
  137. * @param pkey private key of the zone
  138. * @param nick the nick name to set
  139. * @param cont continuation to call when done
  140. * @param cont_cls closure for @a cont
  141. * @return handle to abort the request
  142. */
  143. struct GNUNET_NAMESTORE_QueueEntry *
  144. GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
  145. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  146. const char *nick,
  147. GNUNET_NAMESTORE_ContinuationWithStatus cont,
  148. void *cont_cls);
  149. /**
  150. * Lookup an item in the namestore.
  151. *
  152. * @param h handle to the namestore
  153. * @param pkey private key of the zone
  154. * @param label name that is being mapped
  155. * @param error_cb function to call on error (i.e. disconnect)
  156. * the handle is afterwards invalid
  157. * @param error_cb_cls closure for @a error_cb
  158. * @param rm function to call with the result (with 0 records if we don't have that label);
  159. * the handle is afterwards invalid
  160. * @param rm_cls closure for @a rm
  161. * @return handle to abort the request
  162. */
  163. struct GNUNET_NAMESTORE_QueueEntry *
  164. GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
  165. const struct
  166. GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
  167. const char *label,
  168. GNUNET_SCHEDULER_TaskCallback error_cb,
  169. void *error_cb_cls,
  170. GNUNET_NAMESTORE_RecordMonitor rm,
  171. void *rm_cls);
  172. /**
  173. * Look for an existing PKEY delegation record for a given public key.
  174. * Returns at most one result to the processor.
  175. *
  176. * @param h handle to the namestore
  177. * @param zone private key of the zone to look up in, never NULL
  178. * @param value_zone public key of the target zone (value), never NULL
  179. * @param error_cb function to call on error (i.e. disconnect)
  180. * the handle is afterwards invalid
  181. * @param error_cb_cls closure for @a error_cb
  182. * @param proc function to call on the matching records, or with
  183. * NULL (rd_count == 0) if there are no matching records;
  184. * the handle is afterwards invalid
  185. * @param proc_cls closure for @a proc
  186. * @return a handle that can be used to
  187. * cancel
  188. */
  189. struct GNUNET_NAMESTORE_QueueEntry *
  190. GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
  191. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  192. const struct
  193. GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
  194. GNUNET_SCHEDULER_TaskCallback error_cb,
  195. void *error_cb_cls,
  196. GNUNET_NAMESTORE_RecordMonitor proc,
  197. void *proc_cls);
  198. /**
  199. * Cancel a namestore operation. The final callback from the
  200. * operation must not have been done yet. Must be called on any
  201. * namestore operation that has not yet completed prior to calling
  202. * #GNUNET_NAMESTORE_disconnect.
  203. *
  204. * @param qe operation to cancel
  205. */
  206. void
  207. GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
  208. /**
  209. * Starts a new zone iteration (used to periodically PUT all of our
  210. * records into our DHT). This MUST lock the `struct GNUNET_NAMESTORE_Handle`
  211. * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next() and
  212. * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
  213. * immediately, and then again after
  214. * #GNUNET_NAMESTORE_zone_iterator_next() is invoked.
  215. *
  216. * On error (disconnect), @a error_cb will be invoked.
  217. * On normal completion, @a finish_cb proc will be
  218. * invoked.
  219. *
  220. * @param h handle to the namestore
  221. * @param zone zone to access, NULL for all zones
  222. * @param error_cb function to call on error (i.e. disconnect),
  223. * the handle is afterwards invalid
  224. * @param error_cb_cls closure for @a error_cb
  225. * @param proc function to call on each name from the zone; it
  226. * will be called repeatedly with a value (if available)
  227. * @param proc_cls closure for @a proc
  228. * @param finish_cb function to call on completion
  229. * the handle is afterwards invalid
  230. * @param finish_cb_cls closure for @a finish_cb
  231. * @return an iterator handle to use for iteration
  232. */
  233. struct GNUNET_NAMESTORE_ZoneIterator *
  234. GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
  235. const struct
  236. GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  237. GNUNET_SCHEDULER_TaskCallback error_cb,
  238. void *error_cb_cls,
  239. GNUNET_NAMESTORE_RecordMonitor proc,
  240. void *proc_cls,
  241. GNUNET_SCHEDULER_TaskCallback finish_cb,
  242. void *finish_cb_cls);
  243. /**
  244. * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
  245. * for the next record.
  246. *
  247. * @param it the iterator
  248. * @param limit number of records to return to the iterator in one shot
  249. * (before #GNUNET_NAMESTORE_zone_iterator_next is to be called again)
  250. */
  251. void
  252. GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it,
  253. uint64_t limit);
  254. /**
  255. * Stops iteration and releases the namestore handle for further calls. Must
  256. * be called on any iteration that has not yet completed prior to calling
  257. * #GNUNET_NAMESTORE_disconnect.
  258. *
  259. * @param it the iterator
  260. */
  261. void
  262. GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
  263. /**
  264. * Handle for a monitoring activity.
  265. */
  266. struct GNUNET_NAMESTORE_ZoneMonitor;
  267. /**
  268. * Begin monitoring a zone for changes. Will first call the @a
  269. * monitor function on all existing records in the selected zone(s) if
  270. * @a iterate_first is #GNUNET_YES. In any case, we will then call @a
  271. * sync_cb, and then afterwards call the @a monitor whenever a record
  272. * changes. If the namestore disconnects, the @a error_cb function is
  273. * called with a disconnect event. Once the connection is
  274. * re-established, the process begins from the start (depending on @a
  275. * iterate_first, we will again first do all existing records, then @a
  276. * sync, then updates).
  277. *
  278. * @param cfg configuration to use to connect to namestore
  279. * @param zone zone to monitor, NULL for all zones
  280. * @param iterate_first #GNUNET_YES to first iterate over all existing records,
  281. * #GNUNET_NO to only return changes that happen from now on
  282. * @param error_cb function to call on error (i.e. disconnect); note that
  283. * unlike the other error callbacks in this API, a call to this
  284. * function does NOT destroy the monitor handle, it merely signals
  285. * that monitoring is down. You need to still explicitly call
  286. * #GNUNET_NAMESTORE_zone_monitor_stop().
  287. * @param error_cb_cls closure for @a error_cb
  288. * @param monitor function to call on zone changes, with an initial limit of 1
  289. * @param monitor_cls closure for @a monitor
  290. * @param sync_cb function called when we're in sync with the namestore
  291. * @param sync_cb_cls closure for @a sync_cb
  292. * @return handle to stop monitoring
  293. */
  294. struct GNUNET_NAMESTORE_ZoneMonitor *
  295. GNUNET_NAMESTORE_zone_monitor_start (const struct
  296. GNUNET_CONFIGURATION_Handle *cfg,
  297. const struct
  298. GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  299. int iterate_first,
  300. GNUNET_SCHEDULER_TaskCallback error_cb,
  301. void *error_cb_cls,
  302. GNUNET_NAMESTORE_RecordMonitor monitor,
  303. void *monitor_cls,
  304. GNUNET_SCHEDULER_TaskCallback sync_cb,
  305. void *sync_cb_cls);
  306. /**
  307. * Calls the monitor processor specified in #GNUNET_NAMESTORE_zone_monitor_start
  308. * for the next record(s). This function is used to allow clients that merely
  309. * monitor the NAMESTORE to still throttle namestore operations, so we can be
  310. * sure that the monitors can keep up.
  311. *
  312. * Note that #GNUNET_NAMESTORE_records_store() only waits for this
  313. * call if the previous limit set by the client was already reached.
  314. * Thus, by using a @a limit greater than 1, monitors basically enable
  315. * a queue of notifications to be processed asynchronously with some
  316. * delay. Note that even with a limit of 1 the
  317. * #GNUNET_NAMESTORE_records_store() function will run asynchronously
  318. * and the continuation may be invoked before the monitors completed
  319. * (or even started) processing the notification. Thus, monitors will
  320. * only closely track the current state of the namestore, but not
  321. * be involved in the transactions.
  322. *
  323. * @param zm the monitor
  324. * @param limit number of records to return to the iterator in one shot
  325. * (before #GNUNET_NAMESTORE_zone_monitor_next is to be called again)
  326. */
  327. void
  328. GNUNET_NAMESTORE_zone_monitor_next (struct GNUNET_NAMESTORE_ZoneMonitor *zm,
  329. uint64_t limit);
  330. /**
  331. * Stop monitoring a zone for changes.
  332. *
  333. * @param zm handle to the monitor activity to stop
  334. */
  335. void
  336. GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
  337. #if 0 /* keep Emacsens' auto-indent happy */
  338. {
  339. #endif
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. #endif
  344. /** @} */ /* end of group */