a484601a5a29d75e142172b55f1c210539c125fa
[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  * - convenience function to gather record and the full affilliated stree
29  *   in one shot
30  */
31
32 #ifndef GNUNET_NAMESTORE_SERVICE_H
33 #define GNUNET_NAMESTORE_SERVICE_H
34
35 #include "gnunet_util_lib.h"
36 #include "gnunet_block_lib.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 /**
47  * Record type indicating any record/'*'
48  */
49 #define GNUNET_NAMESTORE_TYPE_ANY 0
50
51 /**
52  * Record type for GNS zone transfer ("PKEY").
53  */
54 #define GNUNET_NAMESTORE_TYPE_PKEY 65536
55
56 /**
57  * Record type for GNS zone transfer ("PSEU").
58  */
59 #define GNUNET_NAMESTORE_TYPE_PSEU 65537
60
61 /**
62  * Record type for GNS legacy hostnames ("LEHO").
63  */
64 #define GNUNET_NAMESTORE_TYPE_LEHO 65538
65
66 /**
67  * Entry in the queue.
68  */
69 struct GNUNET_NAMESTORE_QueueEntry;
70
71 /**
72  * Handle to the namestore service.
73  */
74 struct GNUNET_NAMESTORE_Handle;
75
76 /**
77  * Handle to the namestore zone iterator.
78  */
79 struct GNUNET_NAMESTORE_ZoneIterator;
80
81 /**
82  * Maximum size of a value that can be stored in the namestore.
83  */
84 #define GNUNET_NAMESTORE_MAX_VALUE_SIZE (63 * 1024)
85
86
87
88 /**
89  * Connect to the namestore service.
90  *
91  * @param cfg configuration to use
92  * @return handle to use to access the service
93  */
94 struct GNUNET_NAMESTORE_Handle *
95 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
96
97
98 /**
99  * Disconnect from the namestore service (and free associated
100  * resources).
101  *
102  * @param h handle to the namestore
103  * @param drop set to GNUNET_YES to delete all data in namestore (!)
104  */
105 void
106 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop);
107
108
109 /**
110  * Continuation called to notify client about result of the
111  * operation.
112  *
113  * @param cls closure
114  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
115  *                GNUNET_NO if content was already there or not found
116  *                GNUNET_YES (or other positive value) on success
117  * @param emsg NULL on success, otherwise an error message
118  */
119 typedef void (*GNUNET_NAMESTORE_ContinuationWithStatus) (void *cls,
120                                                          int32_t success,
121                                                          const char *emsg);
122
123
124 /**
125  * Flags that can be set for a record.
126  */
127 enum GNUNET_NAMESTORE_RecordFlags
128 {
129   
130   /**
131    * No special options.
132    */
133   GNUNET_NAMESTORE_RF_NONE = 0,
134
135   /**
136    * This peer is the authority for this record; it must thus
137    * not be deleted (other records can be deleted if we run
138    * out of space).
139    */
140   GNUNET_NAMESTORE_RF_AUTHORITY = 1,
141
142   /**
143    * This is a private record of this peer and it should
144    * thus not be handed out to other peers.
145    */
146   GNUNET_NAMESTORE_RF_PRIVATE = 2,
147
148   /**
149    * This record was added by the system
150    * and is pending user confimation
151    */
152   GNUNET_NAMESTORE_RF_PENDING = 4
153
154 };
155
156
157 /**
158  * A GNS record.
159  */
160 struct GNUNET_NAMESTORE_RecordData
161 {
162
163   /**
164    * Binary value stored in the DNS record.
165    */
166   const void *data;
167
168   /**
169    * Expiration time for the DNS record.
170    */
171   struct GNUNET_TIME_Absolute expiration;
172
173   /**
174    * Number of bytes in 'data'.
175    */
176   size_t data_size;
177
178   /**
179    * Type of the GNS/DNS record.
180    */
181   uint32_t record_type;
182
183   /**
184    * Flags for the record.
185    */
186   enum GNUNET_NAMESTORE_RecordFlags flags;
187 };
188
189
190 /**
191  * Store an item in the namestore.  If the item is already present,
192  * the expiration time is updated to the max of the existing time and
193  * the new time.  This API is used when we cache signatures from other
194  * authorities.
195  *
196  * @param h handle to the namestore
197  * @param zone_key public key of the zone
198  * @param name name that is being mapped (at most 255 characters long)
199  * @param freshness when does the corresponding block in the DHT expire (until
200  *               when should we never do a DHT lookup for the same name again)?
201  * @param rd_count number of entries in 'rd' array
202  * @param rd array of records with data to store
203  * @param signature signature for all the records in the zone under the given name
204  * @param cont continuation to call when done
205  * @param cont_cls closure for cont
206  * @return handle to abort the request
207  */
208 struct GNUNET_NAMESTORE_QueueEntry *
209 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
210                              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
211                              const char *name,
212                              struct GNUNET_TIME_Absolute freshness,
213                              unsigned int rd_count,
214                              const struct GNUNET_NAMESTORE_RecordData *rd,
215                              const struct GNUNET_CRYPTO_RsaSignature *signature,
216                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
217                              void *cont_cls);
218
219
220 /**
221  * Check if a signature is valid.  This API is used by the GNS Block
222  * to validate signatures received from the network.
223  *
224  * @param public_key public key of the zone
225  * @param expire block expiration
226  * @param name name that is being mapped (at most 255 characters long)
227  * @param rd_count number of entries in 'rd' array
228  * @param rd array of records with data to store
229  * @param signature signature for all the records in the zone under the given name
230  * @return GNUNET_OK if the signature is valid
231  */
232 int
233 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
234                                    const struct GNUNET_TIME_Absolute freshness,
235                                    const char *name,
236                                    unsigned int rd_count,
237                                    const struct GNUNET_NAMESTORE_RecordData *rd,
238                                    const struct GNUNET_CRYPTO_RsaSignature *signature);
239
240
241 /**
242  * Store an item in the namestore.  If the item is already present,
243  * the expiration time is updated to the max of the existing time and
244  * the new time.  This API is used by the authority of a zone.
245  *
246  * @param h handle to the namestore
247  * @param pkey private key of the zone
248  * @param name name that is being mapped (at most 255 characters long)
249  * @param rd record data to store
250  * @param cont continuation to call when done
251  * @param cont_cls closure for cont
252  * @return handle to abort the request
253  */
254 struct GNUNET_NAMESTORE_QueueEntry *
255 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
256                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
257                                 const char *name,
258                                 const struct GNUNET_NAMESTORE_RecordData *rd,
259                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
260                                 void *cont_cls);
261
262
263 /**
264  * Explicitly remove some content from the database.  The
265  * "cont"inuation will be called with status "GNUNET_OK" if content
266  * was removed, "GNUNET_NO" if no matching entry was found and
267  * "GNUNET_SYSERR" on all other types of errors.
268  * This API is used by the authority of a zone.
269  *
270  * @param h handle to the namestore
271  * @param pkey private key of the zone
272  * @param name name that is being mapped (at most 255 characters long)
273  * @param rd record data, remove specific record,  NULL to remove the name and all records
274  * @param cont continuation to call when done
275  * @param cont_cls closure for cont
276  * @return handle to abort the request
277  */
278 struct GNUNET_NAMESTORE_QueueEntry *
279 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
280                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
281                                 const char *name,
282                                 const struct GNUNET_NAMESTORE_RecordData *rd,
283                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
284                                 void *cont_cls);
285
286
287 /**
288  * Process a record that was stored in the namestore.
289  *
290  * @param cls closure
291  * @param zone_key public key of the zone
292  * @param expire when does the corresponding block in the DHT expire (until
293  *               when should we never do a DHT lookup for the same name again)?; 
294  *               GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
295  *               or the expiration time of the block in the namestore (even if there are zero
296  *               records matching the desired record type)
297  * @param name name that is being mapped (at most 255 characters long)
298  * @param rd_count number of entries in 'rd' array
299  * @param rd array of records with data to store
300  * @param signature signature of the record block, NULL if signature is unavailable (i.e. 
301  *        because the user queried for a particular record type only)
302  */
303 typedef void (*GNUNET_NAMESTORE_RecordProcessor) (void *cls,
304                                                   const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
305                                                   struct GNUNET_TIME_Absolute freshness,                            
306                                                   const char *name,
307                                                   unsigned int rd_len,
308                                                   const struct GNUNET_NAMESTORE_RecordData *rd,
309                                                   const struct GNUNET_CRYPTO_RsaSignature *signature);
310
311
312 /**
313  * Get a result for a particular key from the namestore.  The processor
314  * will only be called once.  
315  *
316  * @param h handle to the namestore
317  * @param zone zone to look up a record from
318  * @param name name to look up
319  * @param record_type desired record type, 0 for all
320  * @param proc function to call on the matching records, or with
321  *        NULL (rd_count == 0) if there are no matching records
322  * @param proc_cls closure for proc
323  * @return a handle that can be used to
324  *         cancel
325  */
326 struct GNUNET_NAMESTORE_QueueEntry *
327 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
328                               const struct GNUNET_CRYPTO_ShortHashCode *zone,
329                               const char *name,
330                               uint32_t record_type,
331                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
332
333
334 /**
335  * Look for an existing PKEY delegation record for a given public key.
336  * Returns at most one result to the processor.
337  *
338  * @param h handle to the namestore
339  * @param zone hash of public key of the zone to look up in, never NULL
340  * @param value_zone hash of the public key of the target zone (value), never NULL
341  * @param proc function to call on the matching records, or with
342  *        NULL (rd_count == 0) if there are no matching records
343  * @param proc_cls closure for proc
344  * @return a handle that can be used to
345  *         cancel
346  */
347 struct GNUNET_NAMESTORE_QueueEntry *
348 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, 
349                                const struct GNUNET_CRYPTO_ShortHashCode *zone,
350                                const struct GNUNET_CRYPTO_ShortHashCode *value_zone,
351                                GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls);
352
353
354
355 /**
356  * Starts a new zone iteration (used to periodically PUT all of our
357  * records into our DHT). "proc" will be called once
358  * immediately, and then again after
359  * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
360  *
361  * @param h handle to the namestore
362  * @param zone zone to access, NULL for all zones
363  * @param must_have_flags flags that must be set for the record to be returned
364  * @param must_not_have_flags flags that must NOT be set for the record to be returned
365  * @param proc function to call on each name from the zone; it
366  *        will be called repeatedly with a value (if available)
367  *        and always once at the end with a name of NULL.
368  * @param proc_cls closure for proc
369  * @return an iterator handle to use for iteration
370  */
371 struct GNUNET_NAMESTORE_ZoneIterator *
372 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
373                                        const struct GNUNET_CRYPTO_ShortHashCode *zone,
374                                        enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
375                                        enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
376                                        GNUNET_NAMESTORE_RecordProcessor proc,
377                                        void *proc_cls);
378
379
380 /**
381  * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
382  * for the next record.
383  *
384  * @param it the iterator
385  */
386 void
387 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it);
388
389
390 /**
391  * Stops iteration and releases the namestore handle for further calls.
392  *
393  * @param it the iterator
394  */
395 void
396 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it);
397
398
399 /**
400  * Cancel a namestore operation.  The final callback from the
401  * operation must not have been done yet.
402  *
403  * @param qe operation to cancel
404  */
405 void
406 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe);
407
408
409
410 /* convenience APIs for serializing / deserializing GNS records */
411
412 /**
413  * Calculate how many bytes we will need to serialize the given
414  * records.
415  *
416  * @param rd_count number of records in the rd array
417  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
418  *
419  * @return the required size to serialize
420  *
421  */
422 size_t
423 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
424                                    const struct GNUNET_NAMESTORE_RecordData *rd);
425
426 /**
427  * Serialize the given records to the given destination buffer.
428  *
429  * @param rd_count number of records in the rd array
430  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
431  * @param dest_size size of the destination array
432  * @param dest where to write the result
433  *
434  * @return the size of serialized records
435  */
436 ssize_t
437 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
438                                     const struct GNUNET_NAMESTORE_RecordData *rd,
439                                     size_t dest_size,
440                                     char *dest);
441
442
443 /**
444  * Deserialize the given records to the given destination.
445  *
446  * @param len size of the serialized record data
447  * @param src the serialized record data
448  * @param rd_count number of records in the rd array
449  * @param dest where to put the data
450  *
451  * @return GNUNET_OK on success, GNUNET_SYSERR on error
452  */
453 int
454 GNUNET_NAMESTORE_records_deserialize (size_t len,
455                                       const char *src,
456                                       unsigned int rd_count,
457                                       struct GNUNET_NAMESTORE_RecordData *dest);
458
459
460 /**
461  * Checks if a name is wellformed
462  *
463  * @param name the name to check
464  * @return GNUNET_OK on success, GNUNET_SYSERR on error
465  */
466 int
467 GNUNET_NAMESTORE_check_name (const char * name);
468
469 /**
470  * Convert the 'value' of a record to a string.
471  *
472  * @param type type of the record
473  * @param data value in binary encoding
474  * @param data_size number of bytes in data
475  * @return NULL on error, otherwise human-readable representation of the value
476  */
477 char *
478 GNUNET_NAMESTORE_value_to_string (uint32_t type,
479                                   const void *data,
480                                   size_t data_size);
481
482
483 /**
484  * Convert human-readable version of a 'value' of a record to the binary
485  * representation.
486  *
487  * @param type type of the record
488  * @param s human-readable string
489  * @param data set to value in binary encoding (will be allocated)
490  * @param data_size set to number of bytes in data
491  * @return GNUNET_OK on success
492  */
493 int
494 GNUNET_NAMESTORE_string_to_value (uint32_t type,
495                                   const char *s,
496                                   void **data,
497                                   size_t *data_size);
498
499
500 /**
501  * Convert a type name (i.e. "AAAA") to the corresponding number.
502  *
503  * @param typename name to convert
504  * @return corresponding number, UINT32_MAX on error
505  */
506 uint32_t
507 GNUNET_NAMESTORE_typename_to_number (const char *typename);
508
509
510 /**
511  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
512  *
513  * @param type number of a type to convert
514  * @return corresponding typestring, NULL on error
515  */
516 const char *
517 GNUNET_NAMESTORE_number_to_typename (uint32_t type);
518
519
520 #if 0                           /* keep Emacsens' auto-indent happy */
521 {
522 #endif
523 #ifdef __cplusplus
524 }
525 #endif
526
527 /* end of gnunet_namestore_service.h */
528 #endif