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