header
[oweals/gnunet.git] / src / include / gnunet_namestore_service.h
1 /*
2      This file is part of GNUnet
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_namestore_service.h
23  * @brief API that can be used to store naming information on a GNUnet node;
24  *        Naming information can either be records for which this peer/user
25  *        is authoritative, or blocks which are cached, encrypted naming
26  *        data from other peers.
27  * @author Christian Grothoff
28  */
29 #ifndef GNUNET_NAMESTORE_SERVICE_H
30 #define GNUNET_NAMESTORE_SERVICE_H
31
32 #include "gnunet_util_lib.h"
33 #include "gnunet_block_lib.h"
34 #include "gnunet_gnsrecord_lib.h"
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44
45 /**
46  * Entry in the queue.
47  */
48 struct GNUNET_NAMESTORE_QueueEntry;
49
50 /**
51  * Handle to the namestore service.
52  */
53 struct GNUNET_NAMESTORE_Handle;
54
55 /**
56  * Handle to the namestore zone iterator.
57  */
58 struct GNUNET_NAMESTORE_ZoneIterator;
59
60
61 /**
62  * Connect to the namestore service.
63  *
64  * @param cfg configuration to use
65  * @return handle to use to access the service
66  */
67 struct GNUNET_NAMESTORE_Handle *
68 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
69
70
71 /**
72  * Disconnect from the namestore service (and free associated
73  * resources).  Must not be called from within operation callbacks of
74  * the API.
75  *
76  * @param h handle to the namestore
77  */
78 void
79 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
80
81
82 /**
83  * Continuation called to notify client about result of the
84  * operation.
85  *
86  * @param cls closure
87  * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
88  *                #GNUNET_NO if content was already there or not found
89  *                #GNUNET_YES (or other positive value) on success
90  * @param emsg NULL on success, otherwise an error message
91  */
92 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
93                                                          int32_t success,
94                                                          const char *emsg);
95
96
97 /**
98  * Store an item in the namestore.  If the item is already present,
99  * it is replaced with the new record.  Use an empty array to
100  * remove all records under the given name.
101  *
102  * @param h handle to the namestore
103  * @param pkey private key of the zone
104  * @param label name that is being mapped
105  * @param rd_count number of records in the 'rd' array
106  * @param rd array of records with data to store
107  * @param cont continuation to call when done
108  * @param cont_cls closure for @a cont
109  * @return handle to abort the request
110  */
111 struct GNUNET_NAMESTORE_QueueEntry *
112 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
113                                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
114                                 const char *label,
115                                 unsigned int rd_count,
116                                 const struct GNUNET_GNSRECORD_Data *rd,
117                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
118                                 void *cont_cls);
119
120
121
122 /**
123  * Process a record that was stored in the namestore.
124  *
125  * @param cls closure
126  * @param zone private key of the zone; NULL on disconnect
127  * @param label label of the records; NULL on disconnect
128  * @param rd_count number of entries in @a rd array, 0 if label was deleted
129  * @param rd array of records with data to store
130  */
131 typedef void (*GNUNET_NAMESTORE_RecordMonitor) (void *cls,
132                                                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
133                                                 const char *label,
134                                                 unsigned int rd_count,
135                                                 const struct GNUNET_GNSRECORD_Data *rd);
136
137 /**
138  * Set the desired nick name for a zone
139  *
140  * @param h handle to the namestore
141  * @param pkey private key of the zone
142  * @param nick the nick name to set
143  * @param cont continuation to call when done
144  * @param cont_cls closure for 'cont'
145  * @return handle to abort the request
146  */
147 struct GNUNET_NAMESTORE_QueueEntry *
148 GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h,
149                            const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
150                            const char *nick,
151                            GNUNET_NAMESTORE_ContinuationWithStatus cont,
152                            void *cont_cls);
153
154 /**
155  * Lookup an item in the namestore.
156  *
157  * @param h handle to the namestore
158  * @param pkey private key of the zone
159  * @param label name that is being mapped
160  * @param rm function to call with the result (with 0 records if we don't have that label)
161  * @param rm_cls closure for @a rm
162  * @return handle to abort the request
163  */
164 struct GNUNET_NAMESTORE_QueueEntry *
165 GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h,
166                                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
167                                  const char *label,
168                                  GNUNET_NAMESTORE_RecordMonitor rm,
169                                  void *rm_cls);
170
171
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 public 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 proc function to call on the matching records, or with
180  *        NULL (rd_count == 0) if there are no matching records
181  * @param proc_cls closure for @a proc
182  * @return a handle that can be used to
183  *         cancel
184  */
185 struct GNUNET_NAMESTORE_QueueEntry *
186 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
187                                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
188                                const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
189                                GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls);
190
191
192 /**
193  * Cancel a namestore operation.  The final callback from the
194  * operation must not have been done yet.  Must be called on any
195  * namestore operation that has not yet completed prior to calling
196  * #GNUNET_NAMESTORE_disconnect.
197  *
198  * @param qe operation to cancel
199  */
200 void
201 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
202
203
204 /**
205  * Starts a new zone iteration (used to periodically PUT all of our
206  * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
207  * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
208  * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
209  * immediately, and then again after
210  * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
211  *
212  * @param h handle to the namestore
213  * @param zone zone to access, NULL for all zones
214  * @param proc function to call on each name from the zone; it
215  *        will be called repeatedly with a value (if available)
216  *        and always once at the end with a label of NULL.
217  * @param proc_cls closure for @a proc
218  * @return an iterator handle to use for iteration
219  */
220 struct GNUNET_NAMESTORE_ZoneIterator *
221 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
222                                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
223                                        GNUNET_NAMESTORE_RecordMonitor proc,
224                                        void *proc_cls);
225
226
227 /**
228  * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
229  * for the next record.
230  *
231  * @param it the iterator
232  */
233 void
234 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
235
236
237 /**
238  * Stops iteration and releases the namestore handle for further calls.  Must
239  * be called on any iteration that has not yet completed prior to calling
240  * #GNUNET_NAMESTORE_disconnect.
241  *
242  * @param it the iterator
243  */
244 void
245 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
246
247
248 /**
249  * Handle for a monitoring activity.
250  */
251 struct GNUNET_NAMESTORE_ZoneMonitor;
252
253
254 /**
255  * Function called once the monitor has caught up with the current
256  * state of the database.  Will be called AGAIN after each disconnect
257  * (record monitor called with 'NULL' for zone_key) once we're again
258  * in sync.
259  *
260  * @param cls closure
261  */
262 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
263
264
265 /**
266  * Begin monitoring a zone for changes.  Will first call the @a
267  * monitor function on all existing records in the selected zone(s) if
268  * @a iterate_first is #GNUNET_YES.  In any case, we will then call @a
269  * sync_cb, and then afterwards call the @a monitor whenever a record
270  * changes.  If the namestore disconnects, the @a monitor function is
271  * called with a disconnect event; if the connection is
272  * re-established, the process begins from the start (depending on @a
273  * iterate_first, we first do all existing records, then @a sync, then
274  * updates).
275  *
276  * @param cfg configuration to use to connect to namestore
277  * @param zone zone to monitor, NULL for all zones
278  * @param iterate_first #GNUNET_YES to first iterate over all existing records,
279  *                      #GNUNET_NO to only return changes that happen from now on
280  * @param monitor function to call on zone changes
281  * @param sync_cb function called when we're in sync with the namestore
282  * @param cls closure for @a monitor and @a sync_cb
283  * @return handle to stop monitoring
284  */
285 struct GNUNET_NAMESTORE_ZoneMonitor *
286 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
287                                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
288                                      int iterate_first,
289                                      GNUNET_NAMESTORE_RecordMonitor monitor,
290                                      GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
291                                      void *cls);
292
293
294 /**
295  * Stop monitoring a zone for changes.
296  *
297  * @param zm handle to the monitor activity to stop
298  */
299 void
300 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
301
302
303
304 #if 0                           /* keep Emacsens' auto-indent happy */
305 {
306 #endif
307 #ifdef __cplusplus
308 }
309 #endif
310
311 /* end of gnunet_namestore_service.h */
312 #endif