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