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