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