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