missing check
[oweals/gnunet.git] / src / include / gnunet_namestore_service.h
1
2 /*
3      This file is part of GNUnet
4      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5
6      GNUnet is free software; you can redistribute it and/or modify
7      it under the terms of the GNU General Public License as published
8      by the Free Software Foundation; either version 3, or (at your
9      option) any later version.
10
11      GNUnet is distributed in the hope that it will be useful, but
12      WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      General Public License for more details.
15
16      You should have received a copy of the GNU General Public License
17      along with GNUnet; see the file COPYING.  If not, write to the
18      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19      Boston, MA 02111-1307, USA.
20 */
21
22 /**
23  * @file include/gnunet_namestore_service.h
24  * @brief API that can be used to store naming information on a GNUnet node;
25  *        Naming information can either be records for which this peer/user
26  *        is authoritative, or blocks which are cached, encrypted naming
27  *        data from other peers.
28  * @author Christian Grothoff
29  */
30 #ifndef GNUNET_NAMESTORE_SERVICE_H
31 #define GNUNET_NAMESTORE_SERVICE_H
32
33 #include "gnunet_util_lib.h"
34 #include "gnunet_block_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  * Record type indicating any record/'*'
46  */
47 #define GNUNET_NAMESTORE_TYPE_ANY 0
48
49 /**
50  * Record type for GNS zone transfer ("PKEY").
51  */
52 #define GNUNET_NAMESTORE_TYPE_PKEY 65536
53
54 /**
55  * Record type for GNS zone transfer ("PSEU").
56  */
57 #define GNUNET_NAMESTORE_TYPE_PSEU 65537
58
59 /**
60  * Record type for GNS legacy hostnames ("LEHO").
61  */
62 #define GNUNET_NAMESTORE_TYPE_LEHO 65538
63
64 /**
65  * Record type for VPN resolution
66  */
67 #define GNUNET_NAMESTORE_TYPE_VPN 65539
68
69 /**
70  * Record type for zone revocation
71  */
72 #define GNUNET_NAMESTORE_TYPE_REV 65540
73
74 /**
75  * Record type for a social place.
76  */
77 #define GNUNET_NAMESTORE_TYPE_PLACE 65541
78
79 /**
80  * Entry in the queue.
81  */
82 struct GNUNET_NAMESTORE_QueueEntry;
83
84 /**
85  * Handle to the namestore service.
86  */
87 struct GNUNET_NAMESTORE_Handle;
88
89 /**
90  * Handle to the namestore zone iterator.
91  */
92 struct GNUNET_NAMESTORE_ZoneIterator;
93
94 /**
95  * Maximum size of a value that can be stored in the namestore.
96  */
97 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
98
99
100 /**
101  * Connect to the namestore service.
102  *
103  * @param cfg configuration to use
104  * @return handle to use to access the service
105  */
106 struct GNUNET_NAMESTORE_Handle *
107 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
108
109
110 /**
111  * Disconnect from the namestore service (and free associated
112  * resources).
113  *
114  * @param h handle to the namestore
115  */
116 void
117 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
118
119
120 /**
121  * Continuation called to notify client about result of the
122  * operation.
123  *
124  * @param cls closure
125  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
126  *                GNUNET_NO if content was already there or not found
127  *                GNUNET_YES (or other positive value) on success
128  * @param emsg NULL on success, otherwise an error message
129  */
130 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
131                                                          int32_t success,
132                                                          const char *emsg);
133
134
135 /**
136  * Flags that can be set for a record.
137  */
138 enum GNUNET_NAMESTORE_RecordFlags
139 {
140   
141   /**
142    * No special options.
143    */
144   GNUNET_NAMESTORE_RF_NONE = 0,
145
146   /**
147    * This peer is the authority for this record; it must thus
148    * not be deleted (other records can be deleted if we run
149    * out of space).
150    */
151   GNUNET_NAMESTORE_RF_AUTHORITY = 1,
152
153   /**
154    * This is a private record of this peer and it should
155    * thus not be handed out to other peers.
156    */
157   GNUNET_NAMESTORE_RF_PRIVATE = 2,
158
159   /**
160    * This record was added by the system
161    * and is pending user confimation
162    */
163   GNUNET_NAMESTORE_RF_PENDING = 4,
164
165   /**
166    * This expiration time of the record is a relative
167    * time (not an absolute time).
168    */
169   GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
170
171   /**
172    * This record should not be used unless all (other) records with an absolute
173    * expiration time have expired.
174    */
175   GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
176
177   /**
178    * When comparing flags for record equality for removal,
179    * which flags should must match (in addition to the type,
180    * name, expiration value and data of the record)?  All flags
181    * that are not listed here will be ignored for this purpose.
182    * (for example, we don't expect that users will remember to
183    * pass the '--private' option when removing a record from
184    * the namestore, hence we don't require this particular option
185    * to match upon removal).  See also
186    * 'GNUNET_NAMESTORE_records_cmp'.
187    */
188 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
189 };
190
191
192 /**
193  * A GNS record.
194  */
195 struct GNUNET_NAMESTORE_RecordData
196 {
197
198   /**
199    * Binary value stored in the DNS record.
200    * FIXME: goofy API: sometimes 'data' is individually
201    * 'malloc'ed, sometimes it points into some existing
202    * data area (so sometimes this should be a 'void *',
203    * sometimes a 'const void *').  This is unclean.  We
204    * should go over the code and make sure that 'data'
205    * is NEVER individually alloc'ed here but always points
206    * to some "other" location.
207    */
208   const void *data;
209
210   /**
211    * Expiration time for the DNS record.  Can be relative
212    * or absolute, depending on 'flags'.
213    */
214   uint64_t expiration_time;
215
216   /**
217    * Number of bytes in 'data'.
218    */
219   size_t data_size;
220
221   /**
222    * Type of the GNS/DNS record.
223    */
224   uint32_t record_type;
225
226   /**
227    * Flags for the record.
228    */
229   enum GNUNET_NAMESTORE_RecordFlags flags;
230 };
231
232
233
234 GNUNET_NETWORK_STRUCT_BEGIN
235
236
237 /**
238  * Information we have in an encrypted block with record data (i.e. in the DHT).
239  */
240 struct GNUNET_NAMESTORE_Block
241 {
242
243   /**
244    * Signature of the block.
245    */
246   struct GNUNET_CRYPTO_EccSignature signature;
247
248   /**
249    * Derived key used for signing; hash of this is the query.
250    */
251   struct GNUNET_CRYPTO_EccPublicKey derived_key;
252
253   /**
254    * Number of bytes signed; also specifies the number of bytes
255    * of encrypted data that follow.
256    */
257   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
258   
259   /**
260    * Expiration time of the block.
261    */
262   struct GNUNET_TIME_AbsoluteNBO expiration_time;
263
264   /* followed by encrypted data */
265 };
266
267 GNUNET_NETWORK_STRUCT_END
268
269 /**
270  * Store an item in the namestore.  If the item is already present,
271  * it is replaced with the new record.  
272  *
273  * @param h handle to the namestore
274  * @param block block to store
275  * @param cont continuation to call when done
276  * @param cont_cls closure for @a cont
277  * @return handle to abort the request
278  */
279 struct GNUNET_NAMESTORE_QueueEntry *
280 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
281                               const struct GNUNET_NAMESTORE_Block *block,
282                               GNUNET_NAMESTORE_ContinuationWithStatus cont,
283                               void *cont_cls);
284
285
286 /**
287  * Store an item in the namestore.  If the item is already present,
288  * it is replaced with the new record.  Use an empty array to
289  * remove all records under the given name.
290  *
291  * @param h handle to the namestore
292  * @param pkey private key of the zone
293  * @param label name that is being mapped (at most 255 characters long)
294  * @param rd_count number of records in the 'rd' array
295  * @param rd array of records with data to store
296  * @param cont continuation to call when done
297  * @param cont_cls closure for @a cont
298  * @return handle to abort the request
299  */
300 struct GNUNET_NAMESTORE_QueueEntry *
301 GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h,
302                                 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
303                                 const char *label,
304                                 unsigned int rd_count,
305                                 const struct GNUNET_NAMESTORE_RecordData *rd,
306                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
307                                 void *cont_cls);
308
309
310 /**
311  * Process a record that was stored in the namestore.
312  *
313  * @param cls closure
314  * @param block block that was stored in the namestore
315  */
316 typedef void (*GNUNET_NAMESTORE_BlockProcessor) (void *cls,
317                                                  const struct GNUNET_NAMESTORE_Block *block);
318
319
320 /**
321  * Get a result for a particular key from the namestore.  The processor
322  * will only be called once.  
323  *
324  * @param h handle to the namestore
325  * @param derived_hash hash of zone key combined with name to lookup
326  * @param proc function to call on the matching block, or with
327  *        NULL if there is no matching block
328  * @param proc_cls closure for proc
329  * @return a handle that can be used to cancel
330  */
331 struct GNUNET_NAMESTORE_QueueEntry *
332 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h, 
333                                const struct GNUNET_HashCode *derived_hash,
334                                GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls);
335
336
337 /**
338  * Process a record that was stored in the namestore.
339  *
340  * @param cls closure
341  * @param zone private key of the zone
342  * @param label label of the records
343  * @param rd_count number of entries in @a rd array
344  * @param rd array of records with data to store
345  */
346 typedef void (*GNUNET_NAMESTORE_RecordMonitor) (void *cls,
347                                                 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
348                                                 const char *label,
349                                                 unsigned int rd_count,
350                                                 const struct GNUNET_NAMESTORE_RecordData *rd);
351
352
353 /**
354  * Look for an existing PKEY delegation record for a given public key.
355  * Returns at most one result to the processor.
356  *
357  * @param h handle to the namestore
358  * @param zone public key of the zone to look up in, never NULL
359  * @param value_zone public key of the target zone (value), never NULL
360  * @param proc function to call on the matching records, or with
361  *        NULL (rd_count == 0) if there are no matching records
362  * @param proc_cls closure for proc
363  * @return a handle that can be used to
364  *         cancel
365  */
366 struct GNUNET_NAMESTORE_QueueEntry *
367 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, 
368                                const struct GNUNET_CRYPTO_EccPrivateKey *zone,
369                                const struct GNUNET_CRYPTO_EccPublicKey *value_zone,
370                                GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls);
371
372
373 /**
374  * Process a records that were decrypted from a block.
375  *
376  * @param cls closure
377  * @param rd_count number of entries in @a rd array
378  * @param rd array of records with data to store
379  */
380 typedef void (*GNUNET_NAMESTORE_RecordCallback) (void *cls,
381                                                  unsigned int rd_count,
382                                                  const struct GNUNET_NAMESTORE_RecordData *rd);
383
384
385 /**
386  * Cancel a namestore operation.  The final callback from the
387  * operation must not have been done yet.  Must be called on any
388  * namestore operation that has not yet completed prior to calling
389  * #GNUNET_NAMESTORE_disconnect.
390  *
391  * @param qe operation to cancel
392  */
393 void
394 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
395
396
397 /**
398  * Starts a new zone iteration (used to periodically PUT all of our
399  * records into our DHT). @a proc will be called once immediately, and
400  * then again after #GNUNET_NAMESTORE_zone_iterator_next is invoked.
401  *
402  * @param h handle to the namestore
403  * @param zone zone to access
404  * @param proc function to call on each name from the zone; it
405  *        will be called repeatedly with a value (if available)
406  *        and always once at the end with a name of NULL.
407  * @param proc_cls closure for proc
408  * @return an iterator handle to use for iteration
409  */
410 struct GNUNET_NAMESTORE_ZoneIterator *
411 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
412                                        const struct GNUNET_CRYPTO_EccPrivateKey *zone,
413                                        GNUNET_NAMESTORE_RecordMonitor proc,
414                                        void *proc_cls);
415
416
417 /**
418  * Calls the record processor specified in #GNUNET_NAMESTORE_zone_iteration_start
419  * for the next record.
420  *
421  * @param it the iterator
422  */
423 void
424 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
425
426
427 /**
428  * Stops iteration and releases the namestore handle for further calls.  Must
429  * be called on any iteration that has not yet completed prior to calling
430  * #GNUNET_NAMESTORE_disconnect.
431  *
432  * @param it the iterator
433  */
434 void
435 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
436
437
438 /**
439  * Handle for a monitoring activity.
440  */
441 struct GNUNET_NAMESTORE_ZoneMonitor;
442
443
444 /**
445  * Function called once the monitor has caught up with the current 
446  * state of the database.  Will be called AGAIN after each disconnect
447  * (record monitor called with 'NULL' for zone_key) once we're again
448  * in sync.
449  *
450  * @param cls closure
451  */
452 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
453
454
455 /**
456  * Begin monitoring a zone for changes.  Will first call the @a monitor function
457  * on all existing records in the selected zone(s), then calls @a sync_cb,
458  * and then calls the @a monitor whenever a record changes.  If the namestore
459  * disconnects, the @a monitor function is called with a disconnect event; if
460  * the connection is re-established, the process begins from the start (all
461  * existing records, sync, then updates).
462  *
463  * @param cfg configuration to use to connect to namestore
464  * @param zone zone to monitor
465  * @param monitor function to call on zone changes
466  * @param sync_cb function called when we're in sync with the namestore
467  * @param cls closure for @a monitor' and @a sync_cb
468  * @return handle to stop monitoring
469  */
470 struct GNUNET_NAMESTORE_ZoneMonitor *
471 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
472                                      const struct GNUNET_CRYPTO_EccPrivateKey *zone,
473                                      GNUNET_NAMESTORE_RecordMonitor monitor,
474                                      GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
475                                      void *cls);
476
477
478 /**
479  * Stop monitoring a zone for changes.
480  *
481  * @param zm handle to the monitor activity to stop
482  */
483 void
484 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
485
486
487 /* convenience APIs for serializing / deserializing GNS records */
488
489 /**
490  * Calculate how many bytes we will need to serialize the given
491  * records.
492  *
493  * @param rd_count number of records in the @a rd array
494  * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
495  * @return the required size to serialize
496  */
497 size_t
498 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
499                                    const struct GNUNET_NAMESTORE_RecordData *rd);
500
501
502 /**
503  * Serialize the given records to the given destination buffer.
504  *
505  * @param rd_count number of records in the @a rd array
506  * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
507  * @param dest_size size of the destination array @a dst
508  * @param dest where to write the result
509  * @return the size of serialized records, -1 if records do not fit
510  */
511 ssize_t
512 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
513                                     const struct GNUNET_NAMESTORE_RecordData *rd,
514                                     size_t dest_size,
515                                     char *dest);
516
517
518 /**
519  * Deserialize the given records to the given destination.
520  *
521  * @param len size of the serialized record data
522  * @param src the serialized record data
523  * @param rd_count number of records in the @a dest array
524  * @param dest where to put the data
525  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
526  */
527 int
528 GNUNET_NAMESTORE_records_deserialize (size_t len,
529                                       const char *src,
530                                       unsigned int rd_count,
531                                       struct GNUNET_NAMESTORE_RecordData *dest);
532
533
534 /**
535  * Convert the binary value @a data of a record of 
536  * type @a type to a human-readable string.
537  *
538  * @param type type of the record
539  * @param data value in binary encoding
540  * @param data_size number of bytes in @a data
541  * @return NULL on error, otherwise human-readable representation of the value
542  */
543 char *
544 GNUNET_NAMESTORE_value_to_string (uint32_t type,
545                                   const void *data,
546                                   size_t data_size);
547
548
549 /**
550  * Convert human-readable version of the value @a s of a record
551  * of type @a type to the respective binary representation.
552  *
553  * @param type type of the record
554  * @param s human-readable string
555  * @param data set to value in binary encoding (will be allocated)
556  * @param data_size set to number of bytes in @a data
557  * @return #GNUNET_OK on success
558  */
559 int
560 GNUNET_NAMESTORE_string_to_value (uint32_t type,
561                                   const char *s,
562                                   void **data,
563                                   size_t *data_size);
564
565
566 /**
567  * Convert a type name (i.e. "AAAA") to the corresponding number.
568  *
569  * @param typename name to convert
570  * @return corresponding number, UINT32_MAX on error
571  */
572 uint32_t
573 GNUNET_NAMESTORE_typename_to_number (const char *typename);
574
575
576 /**
577  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
578  *
579  * @param type number of a type to convert
580  * @return corresponding typestring, NULL on error
581  */
582 const char *
583 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
584
585
586 /**
587  * Test if a given record is expired.
588  * 
589  * @param rd record to test
590  * @return #GNUNET_YES if the record is expired,
591  *         #GNUNET_NO if not
592  */
593 int
594 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
595
596
597 /**
598  * Convert a UTF-8 string to UTF-8 lowercase
599  * @param src source string
600  * @return converted result
601  */
602 char *
603 GNUNET_NAMESTORE_normalize_string (const char *src);
604
605
606 /**
607  * Convert a zone to a string (for printing debug messages).
608  * This is one of the very few calls in the entire API that is
609  * NOT reentrant!
610  *
611  * @param z public key of a zone
612  * @return string form; will be overwritten by next call to #GNUNET_NAMESTORE_z2s.
613  */
614 const char *
615 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EccPublicKey *z);
616
617
618 /**
619  * Calculate the DHT query for a given @a label in a given @a zone.
620  * 
621  * @param zone private key of the zone
622  * @param label label of the record
623  * @param query hash to use for the query
624  */
625 void
626 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *zone,
627                                          const char *label,
628                                          struct GNUNET_HashCode *query);
629
630
631 /**
632  * Calculate the DHT query for a given @a label in a given @a zone.
633  * 
634  * @param pub public key of the zone
635  * @param label label of the record
636  * @param query hash to use for the query
637  */
638 void
639 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EccPublicKey *pub,
640                                         const char *label,
641                                         struct GNUNET_HashCode *query);
642
643
644 /**
645  * Sign name and records
646  *
647  * @param key the private key
648  * @param expire block expiration
649  * @param label the name for the records
650  * @param rd record data
651  * @param rd_count number of records in @a rd
652  */
653 struct GNUNET_NAMESTORE_Block *
654 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EccPrivateKey *key,
655                                struct GNUNET_TIME_Absolute expire,
656                                const char *label,
657                                const struct GNUNET_NAMESTORE_RecordData *rd,
658                                unsigned int rd_count);
659
660
661 /**
662  * Check if a signature is valid.  This API is used by the GNS Block
663  * to validate signatures received from the network.
664  *
665  * @param block block to verify
666  * @return #GNUNET_OK if the signature is valid
667  */
668 int
669 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block);
670
671
672 /**
673  * Decrypt block.
674  *
675  * @param block block to decrypt
676  * @param zone_key public key of the zone
677  * @param label the name for the records
678  * @param proc function to call with the result
679  * @param proc_cls closure for @a proc
680  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was 
681  *        not well-formed
682  */
683 int
684 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
685                                 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
686                                 const char *label,
687                                 GNUNET_NAMESTORE_RecordCallback proc,
688                                 void *proc_cls);
689
690
691 /**
692  * Compares if two records are equal
693  *
694  * @param a a record
695  * @param b another record 
696  * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
697  */
698 int
699 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
700                               const struct GNUNET_NAMESTORE_RecordData *b);
701
702
703 /**
704  * Returns the expiration time of the given block of records. The block
705  * expiration time is the expiration time of the block with smallest
706  * expiration time.
707  *
708  * @param rd_count number of records given in @a rd
709  * @param rd array of records 
710  * @return absolute expiration time
711  */
712 struct GNUNET_TIME_Absolute
713 GNUNET_NAMESTORE_record_get_expiration_time (unsigned int rd_count, 
714                                              const struct GNUNET_NAMESTORE_RecordData *rd);
715
716
717 #if 0                           /* keep Emacsens' auto-indent happy */
718 {
719 #endif
720 #ifdef __cplusplus
721 }
722 #endif
723
724 /* end of gnunet_namestore_service.h */
725 #endif