30098832c607a28530e0104b8ecd60b9d8fb00d5
[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.
100  *
101  * @param h handle to the namestore
102  * @param block block to store
103  * @param cont continuation to call when done
104  * @param cont_cls closure for @a cont
105  * @return handle to abort the request
106  */
107 struct GNUNET_NAMESTORE_QueueEntry *
108 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
109                               const struct GNUNET_NAMESTORE_Block *block,
110                               GNUNET_NAMESTORE_ContinuationWithStatus cont,
111                               void *cont_cls);
112
113
114 /**
115  * Store an item in the namestore.  If the item is already present,
116  * it is replaced with the new record.  Use an empty array to
117  * remove all records under the given name.
118  *
119  * @param h handle to the namestore
120  * @param pkey private key of the zone
121  * @param label name that is being mapped (at most 255 characters long)
122  * @param rd_count number of records in the 'rd' array
123  * @param rd array of records with data to store
124  * @param cont continuation to call when done
125  * @param cont_cls closure for @a cont
126  * @return handle to abort the request
127  */
128 struct GNUNET_NAMESTORE_QueueEntry *
129 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
130                                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
131                                 const char *label,
132                                 unsigned int rd_count,
133                                 const struct GNUNET_NAMESTORE_RecordData *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 block block that was stored in the namestore
143  */
144 typedef void (*GNUNET_NAMESTORE_BlockProcessor) (void *cls,
145                                                  const struct GNUNET_NAMESTORE_Block *block);
146
147
148 /**
149  * Get a result for a particular key from the namestore.  The processor
150  * will only be called once.
151  *
152  * @param h handle to the namestore
153  * @param derived_hash hash of zone key combined with name to lookup
154  *        then at the end once with NULL
155  * @param proc function to call on the matching block, or with
156  *        NULL if there is no matching block
157  * @param proc_cls closure for @a proc
158  * @return a handle that can be used to cancel
159  */
160 struct GNUNET_NAMESTORE_QueueEntry *
161 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h,
162                                const struct GNUNET_HashCode *derived_hash,
163                                GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls);
164
165
166 /**
167  * Process a record that was stored in the namestore.
168  *
169  * @param cls closure
170  * @param zone private key of the zone; NULL on disconnect
171  * @param label label of the records; NULL on disconnect
172  * @param rd_count number of entries in @a rd array, 0 if label was deleted
173  * @param rd array of records with data to store
174  */
175 typedef void (*GNUNET_NAMESTORE_RecordMonitor) (void *cls,
176                                                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
177                                                 const char *label,
178                                                 unsigned int rd_count,
179                                                 const struct GNUNET_NAMESTORE_RecordData *rd);
180
181
182 /**
183  * Look for an existing PKEY delegation record for a given public key.
184  * Returns at most one result to the processor.
185  *
186  * @param h handle to the namestore
187  * @param zone public key of the zone to look up in, never NULL
188  * @param value_zone public key of the target zone (value), never NULL
189  * @param proc function to call on the matching records, or with
190  *        NULL (rd_count == 0) if there are no matching records
191  * @param proc_cls closure for @a proc
192  * @return a handle that can be used to
193  *         cancel
194  */
195 struct GNUNET_NAMESTORE_QueueEntry *
196 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
197                                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
198                                const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
199                                GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls);
200
201
202 /**
203  * Cancel a namestore operation.  The final callback from the
204  * operation must not have been done yet.  Must be called on any
205  * namestore operation that has not yet completed prior to calling
206  * #GNUNET_NAMESTORE_disconnect.
207  *
208  * @param qe operation to cancel
209  */
210 void
211 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
212
213
214 /**
215  * Starts a new zone iteration (used to periodically PUT all of our
216  * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle
217  * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and
218  * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once
219  * immediately, and then again after
220  * #GNUNET_NAMESTORE_zone_iterator_next is invoked.
221  *
222  * @param h handle to the namestore
223  * @param zone zone to access, NULL for all zones
224  * @param proc function to call on each name from the zone; it
225  *        will be called repeatedly with a value (if available)
226  *        and always once at the end with a name of NULL.
227  * @param proc_cls closure for @a proc
228  * @return an iterator handle to use for iteration
229  */
230 struct GNUNET_NAMESTORE_ZoneIterator *
231 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
232                                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
233                                        GNUNET_NAMESTORE_RecordMonitor proc,
234                                        void *proc_cls);
235
236
237 /**
238  * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
239  * for the next record.
240  *
241  * @param it the iterator
242  */
243 void
244 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
245
246
247 /**
248  * Stops iteration and releases the namestore handle for further calls.  Must
249  * be called on any iteration that has not yet completed prior to calling
250  * #GNUNET_NAMESTORE_disconnect.
251  *
252  * @param it the iterator
253  */
254 void
255 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
256
257
258 /**
259  * Handle for a monitoring activity.
260  */
261 struct GNUNET_NAMESTORE_ZoneMonitor;
262
263
264 /**
265  * Function called once the monitor has caught up with the current
266  * state of the database.  Will be called AGAIN after each disconnect
267  * (record monitor called with 'NULL' for zone_key) once we're again
268  * in sync.
269  *
270  * @param cls closure
271  */
272 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
273
274
275 /**
276  * Begin monitoring a zone for changes.  Will first call the @a monitor function
277  * on all existing records in the selected zone(s), then calls @a sync_cb,
278  * and then calls the @a monitor whenever a record changes.  If the namestore
279  * disconnects, the @a monitor function is called with a disconnect event; if
280  * the connection is re-established, the process begins from the start (all
281  * existing records, sync, then updates).
282  *
283  * @param cfg configuration to use to connect to namestore
284  * @param zone zone to monitor
285  * @param monitor function to call on zone changes
286  * @param sync_cb function called when we're in sync with the namestore
287  * @param cls closure for @a monitor and @a sync_cb
288  * @return handle to stop monitoring
289  */
290 struct GNUNET_NAMESTORE_ZoneMonitor *
291 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
292                                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
293                                      GNUNET_NAMESTORE_RecordMonitor monitor,
294                                      GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
295                                      void *cls);
296
297
298 /**
299  * Stop monitoring a zone for changes.
300  *
301  * @param zm handle to the monitor activity to stop
302  */
303 void
304 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
305
306
307
308 #if 0                           /* keep Emacsens' auto-indent happy */
309 {
310 #endif
311 #ifdef __cplusplus
312 }
313 #endif
314
315 /* end of gnunet_namestore_service.h */
316 #endif