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