fe406abd97eceb5e613e75ade476eabd09e6e3b0
[oweals/gnunet.git] / src / include / gnunet_namestore_service.h
1 /*
2      This file is part of GNUnet
3      (C) 2012 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 2, 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  * @author Christian Grothoff
25  *
26  * Other functions we might want:
27  * - enumerate all known zones
28  * - convenience function to gather record and the full affilliated stree
29  *   in one shot
30  */
31
32 #ifndef GNUNET_NAMESTORE_SERVICE_H
33 #define GNUNET_NAMESTORE_SERVICE_H
34
35 #include "gnunet_util_lib.h"
36 #include "gnunet_block_lib.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 /**
47  * Entry in the queue.
48  */
49 struct GNUNET_NAMESTORE_QueueEntry;
50
51 /**
52  * Handle to the namestore service.
53  */
54 struct GNUNET_NAMESTORE_Handle;
55
56 /**
57  * Maximum size of a value that can be stored in the namestore.
58  */
59 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
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
73  * associated resources).
74  *
75  * @param h handle to the namestore
76  * @param drop set to GNUNET_YES to delete all data in namestore (!)
77  */
78 void
79 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop);
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
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  * Flags that can be set for a record.
99  */
100 enum GNUNET_NAMESTORE_RecordFlags
101 {
102   
103   /**
104    * No special options.
105    */
106   GNUNET_NAMESTORE_RF_NONE = 0,
107
108   /**
109    * This peer is the authority for this record; it must thus
110    * not be deleted (other records can be deleted if we run
111    * out of space).
112    */
113   GNUNET_NAMESTORE_RF_AUTHORITY = 1,
114
115   /**
116    * This is a private record of this peer and it should
117    * thus not be handed out to other peers.
118    */
119   GNUNET_NAMESTORE_RF_PRIVATE = 2
120
121 };
122
123
124 /**
125  * We formally store records in a B-tree for signing.  This struct
126  * identifies the location of a record in the B-tree.
127  */
128 struct GNUNET_NAMESTORE_SignatureLocation
129 {
130   /**
131    * Offset in the B-tree.
132    */
133   uint64_t offset;
134
135   /**
136    * Depth in the B-tree.
137    */
138   uint32_t depth;
139
140   /**
141    * Revision of the B-tree.
142    */
143   uint32_t revision;
144 };
145
146
147 /**
148  * Continuation called to notify client about result of the
149  * signing operation.
150  *
151  * @param cls closure
152  * @param sig where the signature is now located in the S-tree
153  */
154 typedef void (*GNUNET_NAMESTORE_ContinuationWithSignature) (void *cls,
155                                                             const struct GNUNET_NAMESTORE_SignatureLocation *sig);
156
157
158
159
160
161 /**
162  * Get the hash of a record (what will be signed in the Stree for
163  * the record).
164  *
165  * @param zone hash of the public key of the zone
166  * @param name name that is being mapped (at most 255 characters long)
167  * @param record_type type of the record (A, AAAA, PKEY, etc.)
168  * @param expiration expiration time for the content
169  * @param flags flags for the content
170  * @param data_size number of bytes in data
171  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
172  *             GNS specification for GNS extensions)
173  * @param record_hash hash of the record (set)
174  */
175 void
176 GNUNET_NAMESTORE_record_hash (struct GNUNET_NAMESTORE_Handle *h,
177                               const GNUNET_HashCode *zone,
178                               const char *name,
179                               uint32_t record_type,
180                               struct GNUNET_TIME_Absolute expiration,
181                               enum GNUNET_NAMESTORE_RecordFlags flags,
182                               size_t data_size,
183                               const void *data, 
184                               GNUNET_HashCode *record_hash);
185
186
187 /**
188  * Sign a record.  This function is used by the authority of the zone
189  * to add a record.
190  *
191  * @param h handle to the namestore
192  * @param zone_privkey private key of the zone
193  * @param record_hash hash of the record to be signed
194  * @param cont continuation to call when done
195  * @param cont_cls closure for cont
196  * @return handle to abort the request
197  */
198 struct GNUNET_NAMESTORE_QueueEntry *
199 GNUNET_NAMESTORE_stree_extend (struct GNUNET_NAMESTORE_Handle *h,
200                                const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
201                                const GNUNET_HashCode *record_hash,
202                                GNUNET_NAMESTORE_ContinuationWithSignature cont,
203                                void *cont_cls);
204
205
206 /**
207  * Rebalance the signature tree of our zone.  This function should
208  * be called "rarely" to rebalance the tree.
209  *
210  * @param h handle to the namestore
211  * @param zone_privkey private key for the zone to rebalance
212  * @param cont continuation to call when done
213  * @param cont_cls closure for cont
214  * @return handle to abort the request
215  */
216 struct GNUNET_NAMESTORE_QueueEntry *
217 GNUNET_NAMESTORE_stree_rebalance (struct GNUNET_NAMESTORE_Handle *h,
218                                   const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
219                                   GNUNET_NAMESTORE_ContinuationWithStatus cont,
220                                   void *cont_cls);
221
222
223 /**
224  * Provide the root of a signature tree.  This function is 
225  * used by non-authorities as the first operation when 
226  * adding a foreign zone.
227  *
228  * @param h handle to the namestore
229  * @param zone_key public key of the zone
230  * @param signature signature of the top-level entry of the zone
231  * @param revision revision number of the zone
232  * @param top_hash top-level hash of the zone
233  * @param cont continuation to call when done
234  * @param cont_cls closure for cont
235  * @return handle to abort the request
236  */
237 struct GNUNET_NAMESTORE_QueueEntry *
238 GNUNET_NAMESTORE_stree_start (struct GNUNET_NAMESTORE_Handle *h,
239                               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
240                               const struct GNUNET_CRYPTO_RsaSignature *signature,
241                               uint32_t revision,
242                               const GNUNET_HashCode *top_hash,
243                               GNUNET_NAMESTORE_ContinuationWithSignature cont,
244                               void *cont_cls);
245
246
247 /**
248  * Store part of a signature B-tree in the namestore.  This function
249  * is used by non-authorities to cache parts of a zone's signature tree.
250  * Note that the tree must be build top-down.  This function must check
251  * that the nodes being added are valid, and if not refuse the operation.
252  *
253  * @param h handle to the namestore
254  * @param zone_key public key of the zone
255  * @param loc location in the B-tree
256  * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1)
257  * @param num_entries number of entries at this node in the B-tree
258  * @param entries the 'num_entries' entries to store (hashes over the
259  *                records)
260  * @param cont continuation to call when done
261  * @param cont_cls closure for cont
262  * @return handle to abort the request
263  */
264 struct GNUNET_NAMESTORE_QueueEntry *
265 GNUNET_NAMESTORE_stree_put (struct GNUNET_NAMESTORE_Handle *h,
266                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
267                             const struct GNUNET_NAMESTORE_SignatureLocation *loc,
268                             const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
269                             unsigned int num_entries,
270                             const GNUNET_HashCode *entries,
271                             GNUNET_NAMESTORE_ContinuationWithStatus cont,
272                             void *cont_cls);
273
274
275 /**
276  * Store current zone signature in the namestore.  This function
277  * is used by non-authorities to cache the top of a zone's signature tree.
278  * Note that the tree must be build top-down, so this function is called
279  * first for a given zone and revision.
280  *
281  * @param h handle to the namestore
282  * @param zone_key public key of the zone
283  * @param loc identifies the top of the B-tree (depth and revision)
284  * @param time time of the signature creation
285  * @param top_sig signature at the top
286  * @param root_hash top level hash code in the Merkle-tree / stree
287  * @param cont continuation to call when done
288  * @param cont_cls closure for cont
289  * @return handle to abort the request
290  */
291 struct GNUNET_NAMESTORE_QueueEntry *
292 GNUNET_NAMESTORE_signature_put (struct GNUNET_NAMESTORE_Handle *h,
293                                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
294                                 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
295                                 struct GNUNET_TIME_Absolute time,
296                                 const struct GNUNET_CRYPTO_RsaSignature *top_sig,
297                                 const GNUNET_HashCode *root_hash,
298                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
299                                 void *cont_cls);
300
301
302 /**
303  * Store an item in the namestore.  If the item is already present,
304  * the expiration time is updated to the max of the existing time and
305  * the new time.  The operation must fail if there is no matching
306  * entry in the signature tree.
307  *
308  * @param h handle to the namestore
309  * @param zone hash of the public key of the zone
310  * @param name name that is being mapped (at most 255 characters long)
311  * @param record_type type of the record (A, AAAA, PKEY, etc.)
312  * @param expiration expiration time for the content
313  * @param flags flags for the content
314  * @param sig_loc where is the information about the signature for this record stored?
315  * @param data_size number of bytes in data
316  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
317  *             GNS specification for GNS extensions)
318  * @param cont continuation to call when done
319  * @param cont_cls closure for cont
320  * @return handle to abort the request
321  */
322 struct GNUNET_NAMESTORE_QueueEntry *
323 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
324                              const GNUNET_HashCode *zone,
325                              const char *name,
326                              uint32_t record_type,
327                              struct GNUNET_TIME_Absolute expiration,
328                              enum GNUNET_NAMESTORE_RecordFlags flags,
329                              const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
330                              size_t data_size,
331                              const void *data, 
332                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
333                              void *cont_cls);
334
335
336 /**
337  * Explicitly remove some content from the database.  The
338  * "cont"inuation will be called with status "GNUNET_OK" if content
339  * was removed, "GNUNET_NO" if no matching entry was found and
340  * "GNUNET_SYSERR" on all other types of errors.
341  *
342  * @param h handle to the namestore
343  * @param zone hash of the public key of the zone
344  * @param name name that is being mapped (at most 255 characters long)
345  * @param record_type type of the record (A, AAAA, PKEY, etc.)
346  * @param size number of bytes in data
347  * @param data content stored
348  * @param cont continuation to call when done
349  * @param cont_cls closure for cont
350  * @return handle to abort the request
351  */
352 struct GNUNET_NAMESTORE_QueueEntry *
353 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
354                                 const GNUNET_HashCode *zone, 
355                                 const char *name,
356                                 uint32_t record_type,
357                                 size_t size,
358                                 const void *data, 
359                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
360                                 void *cont_cls);
361
362
363 /**
364  * Process a record that was stored in the namestore.
365  *
366  * @param cls closure
367  * @param zone hash of the public key of the zone
368  * @param name name that is being mapped (at most 255 characters long)
369  * @param record_type type of the record (A, AAAA, PKEY, etc.)
370  * @param expiration expiration time for the content
371  * @param flags flags for the content
372  * @param sig_loc where is the information about the signature for this record stored?
373  * @param size number of bytes in data
374  * @param data content stored
375  */
376 typedef void (*GNUNET_NAMESTORE_RecordProcessor) (void *cls,
377                                                  const GNUNET_HashCode *zone,
378                                                  const char *name,
379                                                  uint32_t record_type,
380                                                  struct GNUNET_TIME_Absolute expiration,
381                                                  enum GNUNET_NAMESTORE_RecordFlags flags,
382                                                  const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
383                                                  size_t size, const void *data);
384
385
386 /**
387  * Get a result for a particular key from the namestore.  The processor
388  * will only be called once.
389  *
390  * @param h handle to the namestore
391  * @param zone zone to look up a record from
392  * @param name name to look up
393  * @param record_type desired record type
394  * @param proc function to call on each matching value;
395  *        will be called once with a NULL value at the end
396  * @param proc_cls closure for proc
397  * @return a handle that can be used to
398  *         cancel
399  */
400 struct GNUNET_NAMESTORE_QueueEntry *
401 GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *h, 
402                               const GNUNET_HashCode *zone,
403                               const char *name,
404                               uint32_t record_type,
405                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
406
407
408
409 /**
410  * Get the hash of a subtree in the STree (what will be signed in the parent
411  * location). FIXME naming conflict!
412  *
413  * @param zone hash of the public key of the zone
414  * @param loc where we are in the signature tree
415  * @param num_entries number of entries being stored here
416  * @param entries the entries themselves
417  * @param st_hash hash of the stree node (set)
418  */
419 void
420 GNUNET_NAMESTORE_record_hash_dup (struct GNUNET_NAMESTORE_Handle *h,
421                               const GNUNET_HashCode *zone,
422                               const struct GNUNET_NAMESTORE_SignatureLocation *loc,
423                               unsigned int num_entries,
424                               const GNUNET_HashCode *entries,
425                               GNUNET_HashCode *st_hash);
426
427
428 /**
429  * Process a Stree node that was stored in the namestore.
430  *
431  * @param cls closure
432  * @param zone hash of the public key of the zone
433  * @param loc where we are in the signature tree
434  * @param ploc location of our parent in the signature tree
435  * @param num_entries number of entries being stored here
436  * @param entries the entries themselves
437  */
438 typedef void (*GNUNET_NAMESTORE_StreeProcessor) (void *cls,
439                                                  const GNUNET_HashCode *zone,
440                                                  const struct GNUNET_NAMESTORE_SignatureLocation *loc,
441                                                  const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
442                                                  unsigned int num_entries,
443                                                  const GNUNET_HashCode *entries);
444
445
446 /**
447  * Obtain part of a signature B-tree.  The processor
448  * will only be called once.
449  *
450  * @param h handle to the namestore
451  * @param zone zone to look up a record from
452  * @param sig_loc location to look up
453  * @param proc function to call on each matching value;
454  *        will be called once with a NULL value at the end
455  * @param proc_cls closure for proc
456  * @return a handle that can be used to
457  *         cancel
458  */
459 struct GNUNET_NAMESTORE_QueueEntry *
460 GNUNET_NAMESTORE_lookup_stree (struct GNUNET_NAMESTORE_Handle *h, 
461                                const GNUNET_HashCode *zone,
462                                const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
463                                GNUNET_NAMESTORE_StreeProcessor proc, void *proc_cls);
464
465 /**
466  * Process zone signature information that was stored in the namestore.
467  *
468  * @param cls closure
469  * @param zone hash of the public key of the zone
470  * @param loc where we are in the signature tree (identifies top)
471  * @param top_sig signature at the root
472  * @param time timestamp of the signature
473  * @param top_hash hash at the top of the tree
474  */
475 typedef void (*GNUNET_NAMESTORE_SignatureProcessor) (void *cls,
476                                                      const GNUNET_HashCode *zone,
477                                                      const struct GNUNET_NAMESTORE_SignatureLocation *loc,
478                                                      const struct GNUNET_CRYPTO_RsaSignature *top_sig,
479                                                      struct GNUNET_TIME_Absolute time,
480                                                      const GNUNET_HashCode *top_hash);
481
482
483 /**
484  * Obtain latest/current signature of a zone.  The processor
485  * will only be called once.
486  *
487  * @param h handle to the namestore
488  * @param zone zone to look up a record from
489  * @param proc function to call on each matching value;
490  *        will be called once with a NULL value at the end
491  * @param proc_cls closure for proc
492  * @return a handle that can be used to
493  *         cancel
494  */
495 struct GNUNET_NAMESTORE_QueueEntry *
496 GNUNET_NAMESTORE_lookup_signature (struct GNUNET_NAMESTORE_Handle *h, 
497                                    const GNUNET_HashCode *zone,
498                                    GNUNET_NAMESTORE_SignatureProcessor proc, void *proc_cls);
499
500
501 /**
502  * Get all records of a zone.
503  *
504  * @param h handle to the namestore
505  * @param zone zone to access
506  * @param proc function to call on a random value; it
507  *        will be called repeatedly with a value (if available)
508  *        and always once at the end with a zone and name of NULL.
509  * @param proc_cls closure for proc
510  * @return a handle that can be used to
511  *         cancel
512  */
513 struct GNUNET_NAMESTORE_QueueEntry *
514 GNUNET_NAMESTORE_zone_transfer (struct GNUNET_NAMESTORE_Handle *h,
515                                 const GNUNET_HashCode *zone,
516                                 GNUNET_NAMESTORE_RecordProcessor proc,
517                                 void *proc_cls);
518
519
520
521
522 /**
523  * Cancel a namestore operation.  The final callback from the
524  * operation must not have been done yet.
525  *
526  * @param qe operation to cancel
527  */
528 void
529 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
530
531
532 #if 0                           /* keep Emacsens' auto-indent happy */
533 {
534 #endif
535 #ifdef __cplusplus
536 }
537 #endif
538
539 /* end of gnunet_namestore_service.h */
540 #endif