doxygen
[oweals/gnunet.git] / src / include / gnunet_namestore_service.h
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_namestore_service.h
23  * @brief API that can be used to store naming information on a GNUnet node;
24  * @author Christian Grothoff
25  *
26  * Other functions we might want:
27  * - enumerate all known zones
28  */
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  * Entry in the queue.
76  */
77 struct GNUNET_NAMESTORE_QueueEntry;
78
79 /**
80  * Handle to the namestore service.
81  */
82 struct GNUNET_NAMESTORE_Handle;
83
84 /**
85  * Handle to the namestore zone iterator.
86  */
87 struct GNUNET_NAMESTORE_ZoneIterator;
88
89 /**
90  * Maximum size of a value that can be stored in the namestore.
91  */
92 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
93
94
95
96 /**
97  * Connect to the namestore service.
98  *
99  * @param cfg configuration to use
100  * @return handle to use to access the service
101  */
102 struct GNUNET_NAMESTORE_Handle *
103 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
104
105
106 /**
107  * Disconnect from the namestore service (and free associated
108  * resources).
109  *
110  * @param h handle to the namestore
111  */
112 void
113 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h);
114
115
116 /**
117  * Continuation called to notify client about result of the
118  * operation.
119  *
120  * @param cls closure
121  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
122  *                GNUNET_NO if content was already there or not found
123  *                GNUNET_YES (or other positive value) on success
124  * @param emsg NULL on success, otherwise an error message
125  */
126 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
127                                                          int32_t success,
128                                                          const char *emsg);
129
130
131 /**
132  * Flags that can be set for a record.
133  */
134 enum GNUNET_NAMESTORE_RecordFlags
135 {
136   
137   /**
138    * No special options.
139    */
140   GNUNET_NAMESTORE_RF_NONE = 0,
141
142   /**
143    * This peer is the authority for this record; it must thus
144    * not be deleted (other records can be deleted if we run
145    * out of space).
146    */
147   GNUNET_NAMESTORE_RF_AUTHORITY = 1,
148
149   /**
150    * This is a private record of this peer and it should
151    * thus not be handed out to other peers.
152    */
153   GNUNET_NAMESTORE_RF_PRIVATE = 2,
154
155   /**
156    * This record was added by the system
157    * and is pending user confimation
158    */
159   GNUNET_NAMESTORE_RF_PENDING = 4,
160
161   /**
162    * This expiration time of the record is a relative
163    * time (not an absolute time).
164    */
165   GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION = 8,
166
167   /**
168    * This record should not be used unless all (other) records with an absolute
169    * expiration time have expired.
170    */
171   GNUNET_NAMESTORE_RF_SHADOW_RECORD = 16
172
173   /**
174    * When comparing flags for record equality for removal,
175    * which flags should must match (in addition to the type,
176    * name, expiration value and data of the record)?  All flags
177    * that are not listed here will be ignored for this purpose.
178    * (for example, we don't expect that users will remember to
179    * pass the '--private' option when removing a record from
180    * the namestore, hence we don't require this particular option
181    * to match upon removal).  See also
182    * 'GNUNET_NAMESTORE_records_cmp'.
183    */
184 #define GNUNET_NAMESTORE_RF_RCMP_FLAGS (GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)
185 };
186
187
188 /**
189  * A GNS record.
190  */
191 struct GNUNET_NAMESTORE_RecordData
192 {
193
194   /**
195    * Binary value stored in the DNS record.
196    * FIXME: goofy API: sometimes 'data' is individually
197    * 'malloc'ed, sometimes it points into some existing
198    * data area (so sometimes this should be a 'void *',
199    * sometimes a 'const void *').  This is unclean.
200    */
201   const void *data;
202
203   /**
204    * Expiration time for the DNS record.  Can be relative
205    * or absolute, depending on 'flags'.
206    */
207   uint64_t expiration_time;
208
209   /**
210    * Number of bytes in 'data'.
211    */
212   size_t data_size;
213
214   /**
215    * Type of the GNS/DNS record.
216    */
217   uint32_t record_type;
218
219   /**
220    * Flags for the record.
221    */
222   enum GNUNET_NAMESTORE_RecordFlags flags;
223 };
224
225
226 /**
227  * Store an item in the namestore.  If the item is already present,
228  * the expiration time is updated to the max of the existing time and
229  * the new time.  This API is used when we cache signatures from other
230  * authorities.
231  *
232  * @param h handle to the namestore
233  * @param zone_key public key of the zone
234  * @param name name that is being mapped (at most 255 characters long)
235  * @param freshness when does the corresponding block in the DHT expire (until
236  *               when should we never do a DHT lookup for the same name again)?
237  * @param rd_count number of entries in 'rd' array
238  * @param rd array of records with data to store
239  * @param signature signature for all the records in the zone under the given name
240  * @param cont continuation to call when done
241  * @param cont_cls closure for cont
242  * @return handle to abort the request
243  */
244 struct GNUNET_NAMESTORE_QueueEntry *
245 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
246                              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
247                              const char *name,
248                              struct GNUNET_TIME_Absolute freshness,
249                              unsigned int rd_count,
250                              const struct GNUNET_NAMESTORE_RecordData *rd,
251                              const struct GNUNET_CRYPTO_RsaSignature *signature,
252                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
253                              void *cont_cls);
254
255
256 /**
257  * Check if a signature is valid.  This API is used by the GNS Block
258  * to validate signatures received from the network.
259  *
260  * @param public_key public key of the zone
261  * @param freshness time set for block expiration
262  * @param name name that is being mapped (at most 255 characters long)
263  * @param rd_count number of entries in 'rd' array
264  * @param rd array of records with data to store
265  * @param signature signature for all the records in the zone under the given name
266  * @return GNUNET_OK if the signature is valid
267  */
268 int
269 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
270                                    const struct GNUNET_TIME_Absolute freshness,
271                                    const char *name,
272                                    unsigned int rd_count,
273                                    const struct GNUNET_NAMESTORE_RecordData *rd,
274                                    const struct GNUNET_CRYPTO_RsaSignature *signature);
275
276
277 /**
278  * Store an item in the namestore.  If the item is already present,
279  * the expiration time is updated to the max of the existing time and
280  * the new time.  This API is used by the authority of a zone.
281  * FIXME: consider allowing to pass multiple records in one call!
282  *
283  * @param h handle to the namestore
284  * @param pkey private key of the zone
285  * @param name name that is being mapped (at most 255 characters long)
286  * @param rd record 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_record_create (struct GNUNET_NAMESTORE_Handle *h,
293                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
294                                 const char *name,
295                                 const struct GNUNET_NAMESTORE_RecordData *rd,
296                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
297                                 void *cont_cls);
298
299
300 /**
301  * Explicitly remove some content from the database.  The
302  * "cont"inuation will be called with status "GNUNET_OK" if content
303  * was removed, "GNUNET_NO" if no matching entry was found and
304  * "GNUNET_SYSERR" on all other types of errors.
305  * This API is used by the authority of a zone.
306  *
307  * @param h handle to the namestore
308  * @param pkey private key of the zone
309  * @param name name that is being mapped (at most 255 characters long)
310  * @param rd record data, remove specific record,  NULL to remove the name and all records
311  * @param cont continuation to call when done
312  * @param cont_cls closure for cont
313  * @return handle to abort the request
314  */
315 struct GNUNET_NAMESTORE_QueueEntry *
316 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
317                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
318                                 const char *name,
319                                 const struct GNUNET_NAMESTORE_RecordData *rd,
320                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
321                                 void *cont_cls);
322
323
324 /**
325  * Process a record that was stored in the namestore.
326  *
327  * @param cls closure
328  * @param zone_key public key of the zone
329  * @param freshness when does the corresponding block in the DHT expire (until
330  *               when should we never do a DHT lookup for the same name again)?; 
331  *               GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
332  *               or the expiration time of the block in the namestore (even if there are zero
333  *               records matching the desired record type)
334  * @param name name that is being mapped (at most 255 characters long)
335  * @param rd_count number of entries in 'rd' array
336  * @param rd array of records with data to store
337  * @param signature signature of the record block, NULL if signature is unavailable (i.e. 
338  *        because the user queried for a particular record type only)
339  */
340 typedef void (*GNUNET_NAMESTORE_RecordProcessor) (void *cls,
341                                                   const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
342                                                   struct GNUNET_TIME_Absolute freshness,                            
343                                                   const char *name,
344                                                   unsigned int rd_len,
345                                                   const struct GNUNET_NAMESTORE_RecordData *rd,
346                                                   const struct GNUNET_CRYPTO_RsaSignature *signature);
347
348
349 /**
350  * Get a result for a particular key from the namestore.  The processor
351  * will only be called once.  When using this functions, relative expiration
352  * times will be converted to absolute expiration times and a signature
353  * will be created if we are the authority.  The record data and signature
354  * passed to 'proc' is thus always suitable for passing on to other peers
355  * (if we are the authority).  If the record type is NOT set to 'ANY' and
356  * if we are NOT the authority, then non-matching records may be omitted
357  * from the result and no valid signature can be created; in this case,
358  * 'signature' will be NULL and the result cannot be given to other peers.
359  *
360  * @param h handle to the namestore
361  * @param zone zone to look up a record from
362  * @param name name to look up
363  * @param record_type desired record type, 0 for all
364  * @param proc function to call on the matching records, or with
365  *        NULL (rd_count == 0) if there are no matching records
366  * @param proc_cls closure for proc
367  * @return a handle that can be used to
368  *         cancel
369  */
370 struct GNUNET_NAMESTORE_QueueEntry *
371 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
372                                 const struct GNUNET_CRYPTO_ShortHashCode *zone,
373                                 const char *name,
374                                 uint32_t record_type,
375                                 GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
376
377
378 /**
379  * Look for an existing PKEY delegation record for a given public key.
380  * Returns at most one result to the processor.
381  *
382  * @param h handle to the namestore
383  * @param zone hash of public key of the zone to look up in, never NULL
384  * @param value_zone hash of the public key of the target zone (value), never NULL
385  * @param proc function to call on the matching records, or with
386  *        NULL (rd_count == 0) if there are no matching records
387  * @param proc_cls closure for proc
388  * @return a handle that can be used to
389  *         cancel
390  */
391 struct GNUNET_NAMESTORE_QueueEntry *
392 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, 
393                                const struct GNUNET_CRYPTO_ShortHashCode *zone,
394                                const struct GNUNET_CRYPTO_ShortHashCode *value_zone,
395                                GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
396
397
398
399 /**
400  * Starts a new zone iteration (used to periodically PUT all of our
401  * records into our DHT). "proc" will be called once immediately, and
402  * then again after "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
403  *
404  * By specifying a 'zone' of NULL and setting 'GNUNET_NAMESTORE_RF_AUTHORITY'
405  * in 'must_have_flags', we can iterate over all records for which we are
406  * the authority (the 'authority' flag will NOT be set in the returned
407  * records anyway).  
408  *
409  * The 'GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION'
410  * bit in 'must_have_flags' has a special meaning:
411  *
412  * 0) If the bit is clear, all relative expriation times are converted to
413  *    absolute expiration times.  This is useful for performing DHT PUT
414  *    operations (and zone transfers) of our zone.  The generated signatures
415  *    will be valid for other peers.
416  * 1) if it is set, it means that relative expiration times should be
417  *    preserved when returned (this is useful for the zone editor user 
418  *    interface).  No signatures will be created in this case, as 
419  *    signatures must not cover records with relative expiration times.
420  *
421  * Note that not all queries against this interface are equally performant
422  * as for some combinations no efficient index may exist.
423  *
424  * @param h handle to the namestore
425  * @param zone zone to access, NULL for all zones
426  * @param must_have_flags flags that must be set for the record to be returned
427  * @param must_not_have_flags flags that must NOT be set for the record to be returned
428  * @param proc function to call on each name from the zone; it
429  *        will be called repeatedly with a value (if available)
430  *        and always once at the end with a name of NULL.
431  * @param proc_cls closure for proc
432  * @return an iterator handle to use for iteration
433  */
434 struct GNUNET_NAMESTORE_ZoneIterator *
435 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
436                                        const struct GNUNET_CRYPTO_ShortHashCode *zone,
437                                        enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
438                                        enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
439                                        GNUNET_NAMESTORE_RecordProcessor proc,
440                                        void *proc_cls);
441
442
443 /**
444  * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
445  * for the next record.
446  *
447  * @param it the iterator
448  */
449 void
450 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
451
452
453 /**
454  * Stops iteration and releases the namestore handle for further calls.  Must
455  * be called on any iteration that has not yet completed prior to calling
456  * 'GNUNET_NAMESTORE_disconnect'.
457  *
458  * @param it the iterator
459  */
460 void
461 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
462
463
464 /**
465  * Cancel a namestore operation.  The final callback from the
466  * operation must not have been done yet.  Must be called on any
467  * namestore operation that has not yet completed prior to calling
468  * 'GNUNET_NAMESTORE_disconnect'.
469  *
470  * @param qe operation to cancel
471  */
472 void
473 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
474
475
476
477 /* convenience APIs for serializing / deserializing GNS records */
478
479 /**
480  * Calculate how many bytes we will need to serialize the given
481  * records.
482  *
483  * @param rd_count number of records in the rd array
484  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
485  *
486  * @return the required size to serialize
487  *
488  */
489 size_t
490 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
491                                    const struct GNUNET_NAMESTORE_RecordData *rd);
492
493
494 /**
495  * Serialize the given records to the given destination buffer.
496  *
497  * @param rd_count number of records in the rd array
498  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
499  * @param dest_size size of the destination array
500  * @param dest where to write the result
501  *
502  * @return the size of serialized records, -1 if records do not fit
503  */
504 ssize_t
505 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
506                                     const struct GNUNET_NAMESTORE_RecordData *rd,
507                                     size_t dest_size,
508                                     char *dest);
509
510
511 /**
512  * Deserialize the given records to the given destination.
513  *
514  * @param len size of the serialized record data
515  * @param src the serialized record data
516  * @param rd_count number of records in the rd array
517  * @param dest where to put the data
518  *
519  * @return GNUNET_OK on success, GNUNET_SYSERR on error
520  */
521 int
522 GNUNET_NAMESTORE_records_deserialize (size_t len,
523                                       const char *src,
524                                       unsigned int rd_count,
525                                       struct GNUNET_NAMESTORE_RecordData *dest);
526
527
528 /**
529  * Convert the 'value' of a record to a string.
530  *
531  * @param type type of the record
532  * @param data value in binary encoding
533  * @param data_size number of bytes in data
534  * @return NULL on error, otherwise human-readable representation of the value
535  */
536 char *
537 GNUNET_NAMESTORE_value_to_string (uint32_t type,
538                                   const void *data,
539                                   size_t data_size);
540
541
542 /**
543  * Convert human-readable version of a 'value' of a record to the binary
544  * representation.
545  *
546  * @param type type of the record
547  * @param s human-readable string
548  * @param data set to value in binary encoding (will be allocated)
549  * @param data_size set to number of bytes in data
550  * @return GNUNET_OK on success
551  */
552 int
553 GNUNET_NAMESTORE_string_to_value (uint32_t type,
554                                   const char *s,
555                                   void **data,
556                                   size_t *data_size);
557
558
559 /**
560  * Convert a type name (i.e. "AAAA") to the corresponding number.
561  *
562  * @param typename name to convert
563  * @return corresponding number, UINT32_MAX on error
564  */
565 uint32_t
566 GNUNET_NAMESTORE_typename_to_number (const char *typename);
567
568
569 /**
570  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
571  *
572  * @param type number of a type to convert
573  * @return corresponding typestring, NULL on error
574  */
575 const char *
576 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
577
578
579 /**
580  * Test if a given record is expired.
581  * 
582  * @return GNUNET_YES if the record is expired,
583  *         GNUNET_NO if not
584  */
585 int
586 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd);
587
588
589 #if 0                           /* keep Emacsens' auto-indent happy */
590 {
591 #endif
592 #ifdef __cplusplus
593 }
594 #endif
595
596 /* end of gnunet_namestore_service.h */
597 #endif