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