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