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