db74530636baceb05e3c3b4d464f0d396ef8e502
[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.
203    */
204   const void *data;
205
206   /**
207    * Expiration time for the DNS record.  Can be relative
208    * or absolute, depending on 'flags'.
209    */
210   uint64_t expiration_time;
211
212   /**
213    * Number of bytes in 'data'.
214    */
215   size_t data_size;
216
217   /**
218    * Type of the GNS/DNS record.
219    */
220   uint32_t record_type;
221
222   /**
223    * Flags for the record.
224    */
225   enum GNUNET_NAMESTORE_RecordFlags flags;
226 };
227
228
229 /**
230  * Information we have in an encrypted block with record data (i.e. in the DHT).
231  */
232 struct GNUNET_NAMESTORE_Block
233 {
234
235   /**
236    * Signature of the block.
237    */
238   struct GNUNET_CRYPTO_EccSignature signature;
239
240   /**
241    * Derived key used for signing; hash of this is the query.
242    */
243   struct GNUNET_CRYPTO_EccPublicKey derived_key;
244
245   /**
246    * Number of bytes signed; also specifies the number of bytes
247    * of encrypted data that follow.
248    */
249   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
250   
251   /**
252    * Expiration time of the block.
253    */
254   struct GNUNET_TIME_AbsoluteNBO expiration_time;
255
256   /* followed by encrypted data */
257 };
258
259
260 /**
261  * Store an item in the namestore.  If the item is already present,
262  * it is replaced with the new record.  
263  *
264  * @param h handle to the namestore
265  * @param block block to store
266  * @param cont continuation to call when done
267  * @param cont_cls closure for cont
268  * @return handle to abort the request
269  */
270 struct GNUNET_NAMESTORE_QueueEntry *
271 GNUNET_NAMESTORE_block_cache (struct GNUNET_NAMESTORE_Handle *h,
272                               const struct GNUNET_NAMESTORE_Block *block,
273                               GNUNET_NAMESTORE_ContinuationWithStatus cont,
274                               void *cont_cls);
275
276
277 /**
278  * Store an item in the namestore.  If the item is already present,
279  * it is replaced with the new record.  Use an empty array to
280  * remove all records under the given name.
281  *
282  * @param h handle to the namestore
283  * @param pkey private key of the zone
284  * @param label name that is being mapped (at most 255 characters long)
285  * @param rd_count number of records in the 'rd' array
286  * @param rd array of records with data to store
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_records_store (struct GNUNET_NAMESTORE_Handle *h,
293                                 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
294                                 const char *label,
295                                 unsigned int rd_count,
296                                 const struct GNUNET_NAMESTORE_RecordData *rd,
297                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
298                                 void *cont_cls);
299
300
301 /**
302  * Process a record that was stored in the namestore.
303  *
304  * @param cls closure
305  * @param block block that was stored in the namestore
306  */
307 typedef void (*GNUNET_NAMESTORE_BlockProcessor) (void *cls,
308                                                  const struct GNUNET_NAMESTORE_Block *block);
309
310
311 /**
312  * Get a result for a particular key from the namestore.  The processor
313  * will only be called once.  
314  *
315  * @param h handle to the namestore
316  * @param derived_hash hash of zone key combined with name to lookup
317  * @param proc function to call on the matching block, or with
318  *        NULL if there is no matching block
319  * @param proc_cls closure for proc
320  * @return a handle that can be used to cancel
321  */
322 struct GNUNET_NAMESTORE_QueueEntry *
323 GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h, 
324                                const struct GNUNET_HashCode *derived_hash,
325                                GNUNET_NAMESTORE_BlockProcessor proc, void *proc_cls);
326
327
328 /**
329  * Process a record that was stored in the namestore.
330  *
331  * @param cls closure
332  * @param zone private key of the zone
333  * @param label label of the records
334  * @param rd_count number of entries in 'rd' array
335  * @param rd array of records with data to store
336  */
337 typedef void (*GNUNET_NAMESTORE_RecordMonitor) (void *cls,
338                                                 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
339                                                 const char *label,
340                                                 unsigned int rd_count,
341                                                 const struct GNUNET_NAMESTORE_RecordData *rd);
342
343
344 /**
345  * Look for an existing PKEY delegation record for a given public key.
346  * Returns at most one result to the processor.
347  *
348  * @param h handle to the namestore
349  * @param zone public key of the zone to look up in, never NULL
350  * @param value_zone public key of the target zone (value), never NULL
351  * @param proc function to call on the matching records, or with
352  *        NULL (rd_count == 0) if there are no matching records
353  * @param proc_cls closure for proc
354  * @return a handle that can be used to
355  *         cancel
356  */
357 struct GNUNET_NAMESTORE_QueueEntry *
358 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, 
359                                const struct GNUNET_CRYPTO_EccPrivateKey *zone,
360                                const struct GNUNET_CRYPTO_EccPublicKey *value_zone,
361                                GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls);
362
363
364 /**
365  * Cancel a namestore operation.  The final callback from the
366  * operation must not have been done yet.  Must be called on any
367  * namestore operation that has not yet completed prior to calling
368  * 'GNUNET_NAMESTORE_disconnect'.
369  *
370  * @param qe operation to cancel
371  */
372 void
373 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
374
375
376 /**
377  * Starts a new zone iteration (used to periodically PUT all of our
378  * records into our DHT). "proc" will be called once immediately, and
379  * then again after "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
380  *
381  * @param h handle to the namestore
382  * @param zone zone to access
383  * @param must_have_flags flags that must be set for the record to be returned
384  * @param must_not_have_flags flags that must NOT be set for the record to be returned
385  * @param proc function to call on each name from the zone; it
386  *        will be called repeatedly with a value (if available)
387  *        and always once at the end with a name of NULL.
388  * @param proc_cls closure for proc
389  * @return an iterator handle to use for iteration
390  */
391 struct GNUNET_NAMESTORE_ZoneIterator *
392 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
393                                        const struct GNUNET_CRYPTO_EccPrivateKey *zone,
394                                        GNUNET_NAMESTORE_RecordMonitor proc,
395                                        void *proc_cls);
396
397
398 /**
399  * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
400  * for the next record.
401  *
402  * @param it the iterator
403  */
404 void
405 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
406
407
408 /**
409  * Stops iteration and releases the namestore handle for further calls.  Must
410  * be called on any iteration that has not yet completed prior to calling
411  * 'GNUNET_NAMESTORE_disconnect'.
412  *
413  * @param it the iterator
414  */
415 void
416 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
417
418
419 /**
420  * Handle for a monitoring activity.
421  */
422 struct GNUNET_NAMESTORE_ZoneMonitor;
423
424
425 /**
426  * Function called once the monitor has caught up with the current 
427  * state of the database.  Will be called AGAIN after each disconnect
428  * (record monitor called with 'NULL' for zone_key) once we're again
429  * in sync.
430  *
431  * @param cls closure
432  */
433 typedef void (*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls);
434
435
436 /**
437  * Begin monitoring a zone for changes.  Will first call the 'monitor' function
438  * on all existing records in the selected zone(s), then calls 'sync_cb',
439  * and then calls the 'monitor' whenever a record changes.  If the namestore
440  * disconnects, the 'monitor' function is called with a disconnect event; if
441  * the connection is re-established, the process begins from the start (all
442  * existing records, sync, then updates).
443  *
444  * Note that the 'signature' in the 'monitor' callback will always be
445  * NULL (as signatures are usually not valuable to monitoring clients,
446  * the service does not bother to generate a valid signature for
447  * monitoring).
448  *
449  * @param cfg configuration to use to connect to namestore
450  * @param zone zone to monitor
451  * @param monitor function to call on zone changes
452  * @param sync_cb function called when we're in sync with the namestore
453  * @param cls closure for 'monitor' and 'sync_cb'
454  * @return handle to stop monitoring
455  */
456 struct GNUNET_NAMESTORE_ZoneMonitor *
457 GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
458                                      const struct GNUNET_CRYPTO_EccPrivateKey *zone,
459                                      GNUNET_NAMESTORE_RecordMonitor monitor,
460                                      GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb,
461                                      void *cls);
462
463
464 /**
465  * Stop monitoring a zone for changes.
466  *
467  * @param zm handle to the monitor activity to stop
468  */
469 void
470 GNUNET_NAMESTORE_zone_monitor_stop (struct GNUNET_NAMESTORE_ZoneMonitor *zm);
471
472
473 /* convenience APIs for serializing / deserializing GNS records */
474
475 /**
476  * Calculate how many bytes we will need to serialize the given
477  * records.
478  *
479  * @param rd_count number of records in the rd array
480  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
481  *
482  * @return the required size to serialize
483  *
484  */
485 size_t
486 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
487                                    const struct GNUNET_NAMESTORE_RecordData *rd);
488
489
490 /**
491  * Serialize the given records to the given destination buffer.
492  *
493  * @param rd_count number of records in the rd array
494  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
495  * @param dest_size size of the destination array
496  * @param dest where to write the result
497  *
498  * @return the size of serialized records, -1 if records do not fit
499  */
500 ssize_t
501 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
502                                     const struct GNUNET_NAMESTORE_RecordData *rd,
503                                     size_t dest_size,
504                                     char *dest);
505
506
507 /**
508  * Deserialize the given records to the given destination.
509  *
510  * @param len size of the serialized record data
511  * @param src the serialized record data
512  * @param rd_count number of records in the rd array
513  * @param dest where to put the data
514  *
515  * @return GNUNET_OK on success, GNUNET_SYSERR on error
516  */
517 int
518 GNUNET_NAMESTORE_records_deserialize (size_t len,
519                                       const char *src,
520                                       unsigned int rd_count,
521                                       struct GNUNET_NAMESTORE_RecordData *dest);
522
523
524 /**
525  * Convert the 'value' of a record to a string.
526  *
527  * @param type type of the record
528  * @param data value in binary encoding
529  * @param data_size number of bytes in data
530  * @return NULL on error, otherwise human-readable representation of the value
531  */
532 char *
533 GNUNET_NAMESTORE_value_to_string (uint32_t type,
534                                   const void *data,
535                                   size_t data_size);
536
537
538 /**
539  * Convert human-readable version of a 'value' of a record to the binary
540  * representation.
541  *
542  * @param type type of the record
543  * @param s human-readable string
544  * @param data set to value in binary encoding (will be allocated)
545  * @param data_size set to number of bytes in data
546  * @return GNUNET_OK on success
547  */
548 int
549 GNUNET_NAMESTORE_string_to_value (uint32_t type,
550                                   const char *s,
551                                   void **data,
552                                   size_t *data_size);
553
554
555 /**
556  * Convert a type name (i.e. "AAAA") to the corresponding number.
557  *
558  * @param typename name to convert
559  * @return corresponding number, UINT32_MAX on error
560  */
561 uint32_t
562 GNUNET_NAMESTORE_typename_to_number (const char *typename);
563
564
565 /**
566  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
567  *
568  * @param type number of a type to convert
569  * @return corresponding typestring, NULL on error
570  */
571 const char *
572 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
573
574
575 /**
576  * Test if a given record is expired.
577  * 
578  * @param rd record to test
579  * @return GNUNET_YES if the record is expired,
580  *         GNUNET_NO if not
581  */
582 int
583 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
584
585
586 /**
587  * Convert a UTF-8 string to UTF-8 lowercase
588  * @param src source string
589  * @return converted result
590  */
591 char *
592 GNUNET_NAMESTORE_normalize_string (const char *src);
593
594
595 /**
596  * Convert a zone to a string (for printing debug messages).
597  * This is one of the very few calls in the entire API that is
598  * NOT reentrant!
599  *
600  * @param z public key of a zone
601  * @return string form; will be overwritten by next call to GNUNET_h2s.
602  */
603 const char *
604 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EccPublicKey *z);
605
606
607 /**
608  * Calculate the DHT query for a given 'label' in a given zone.
609  * 
610  * @param zone private key of the zone
611  * @param label label of the record
612  * @return query hash to use for the query
613  */
614 void
615 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *zone,
616                                          const char *label,
617                                          struct GNUNET_HashCode *query);
618
619
620 /**
621  * Calculate the DHT query for a given 'label' in a given zone.
622  * 
623  * @param pub public key of the zone
624  * @param label label of the record
625  * @return query hash to use for the query
626  */
627 void
628 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EccPublicKey *pub,
629                                         const char *label,
630                                         struct GNUNET_HashCode *query);
631
632
633 /**
634  * Sign name and records
635  *
636  * @param key the private key
637  * @param expire block expiration
638  * @param label the name for the records
639  * @param rd record data
640  * @param rd_count number of records
641  * @param signature where to store the signature
642  */
643 struct GNUNET_NAMESTORE_Block *
644 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EccPrivateKey *key,
645                                struct GNUNET_TIME_Absolute expire,
646                                const char *label,
647                                const struct GNUNET_NAMESTORE_RecordData *rd,
648                                unsigned int rd_count);
649
650
651 /**
652  * Check if a signature is valid.  This API is used by the GNS Block
653  * to validate signatures received from the network.
654  *
655  * @param block block to verify
656  * @return GNUNET_OK if the signature is valid
657  */
658 int
659 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block);
660
661
662 /**
663  * Decrypt block.
664  *
665  * @param block block to decrypt
666  * @param zone_key public key of the zone
667  * @param label the name for the records
668  * @param proc function to call with the result
669  * @param proc_cls closure for proc
670  * @param GNUNET_OK on success, GNUNET_SYSERR if the block was 
671  *        not well-formed
672  */
673 int
674 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
675                                 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
676                                 const char *label,
677                                 GNUNET_NAMESTORE_RecordMonitor proc,
678                                 void *proc_cls);
679
680
681 /**
682  * Compares if two records are equal
683  *
684  * @param a Record a
685  * @param b Record b
686  *
687  * @return GNUNET_YES or GNUNET_NO
688  */
689 int
690 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
691                               const struct GNUNET_NAMESTORE_RecordData *b);
692
693
694
695
696 #if 0                           /* keep Emacsens' auto-indent happy */
697 {
698 #endif
699 #ifdef __cplusplus
700 }
701 #endif
702
703 /* end of gnunet_namestore_service.h */
704 #endif