e0223589af83a47b26e48a270fe767082bfa67d2
[oweals/gnunet.git] / src / namestore / namestore_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 3, 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 gns/namestore_api.c
23  * @brief API to access the NAMESTORE service
24  * @author Martin Schanzenbach
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_namestore_service.h"
33
34 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
37
38 /**
39  * A QueueEntry.
40  */
41 struct GNUNET_NAMESTORE_QueueEntry
42 {
43   char *data; /*stub data pointer*/
44 };
45
46 /**
47  * Connection to the NAMESTORE service.
48  */
49 struct GNUNET_NAMESTORE_Handle
50 {
51
52   /**
53    * Configuration to use.
54    */
55   const struct GNUNET_CONFIGURATION_Handle *cfg;
56
57   /**
58    * Socket (if available).
59    */
60   struct GNUNET_CLIENT_Connection *client;
61
62   /**
63    * Currently pending transmission request (or NULL).
64    */
65   struct GNUNET_CLIENT_TransmitHandle *th;
66
67   /* dll to use for records */
68   struct GNUNET_NAMESTORE_SimpleRecord * records_head;
69   struct GNUNET_NAMESTORE_SimpleRecord * records_tail;
70
71 };
72
73 struct GNUNET_NAMESTORE_SimpleRecord
74 {
75   /**
76    * DLL
77    */
78   struct GNUNET_NAMESTORE_SimpleRecord *next;
79
80   /**
81    * DLL
82    */
83   struct GNUNET_NAMESTORE_SimpleRecord *prev;
84   
85   const char *name;
86   const GNUNET_HashCode *zone;
87   uint32_t record_type;
88   struct GNUNET_TIME_Absolute expiration;
89   enum GNUNET_NAMESTORE_RecordFlags flags;
90   size_t data_size;
91   const void *data;
92 };
93
94 /**
95  * Initialize the connection with the NAMESTORE service.
96  *
97  * @param cfg configuration to use
98  * @return handle to the GNS service, or NULL on error
99  */
100 struct GNUNET_NAMESTORE_Handle *
101 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
102 {
103   struct GNUNET_NAMESTORE_Handle *handle;
104
105   handle = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
106   handle->cfg = cfg;
107   return handle;
108 }
109
110
111 /**
112  * Shutdown connection with the NAMESTORE service.
113  *
114  * @param handle handle of the NAMESTORE connection to stop
115  */
116 void
117 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
118 {
119   GNUNET_free(handle);
120 }
121
122 /**
123  * Sign a record.  This function is used by the authority of the zone
124  * to add a record.
125  *
126  * @param h handle to the namestore
127  * @param zone_privkey private key of the zone
128  * @param record_hash hash of the record to be signed
129  * @param cont continuation to call when done
130  * @param cont_cls closure for cont
131  * @return handle to abort the request
132  */
133 struct GNUNET_NAMESTORE_QueueEntry *
134 GNUNET_NAMESTORE_stree_extend (struct GNUNET_NAMESTORE_Handle *h,
135              const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
136              const GNUNET_HashCode *record_hash,
137              GNUNET_NAMESTORE_ContinuationWithSignature cont,
138              void *cont_cls)
139 {
140   struct GNUNET_NAMESTORE_QueueEntry *qe;
141   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
142   return qe;
143 }
144
145 /**
146  * Rebalance the signature tree of our zone.  This function should
147  * be called "rarely" to rebalance the tree.
148  *
149  * @param h handle to the namestore
150  * @param zone_privkey private key for the zone to rebalance
151  * @param cont continuation to call when done
152  * @param cont_cls closure for cont
153  * @return handle to abort the request
154  */
155 struct GNUNET_NAMESTORE_QueueEntry *
156 GNUNET_NAMESTORE_stree_rebalance (struct GNUNET_NAMESTORE_Handle *h,
157           const struct GNUNET_CRYPTO_RsaPrivateKey *zone_privkey,
158           GNUNET_NAMESTORE_ContinuationWithStatus cont,
159           void *cont_cls)
160 {
161   struct GNUNET_NAMESTORE_QueueEntry *qe;
162   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
163   return qe;
164 }
165
166 /**
167  * Provide the root of a signature tree.  This function is 
168  * used by non-authorities as the first operation when 
169  * adding a foreign zone.
170  *
171  * @param h handle to the namestore
172  * @param zone_key public key of the zone
173  * @param signature signature of the top-level entry of the zone
174  * @param revision revision number of the zone
175  * @param top_hash top-level hash of the zone
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_stree_start (struct GNUNET_NAMESTORE_Handle *h,
182                               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
183                               const struct GNUNET_CRYPTO_RsaSignature *signature,
184                               uint32_t revision,
185                               const GNUNET_HashCode *top_hash,
186                               GNUNET_NAMESTORE_ContinuationWithSignature cont,
187                               void *cont_cls)
188 {
189   struct GNUNET_NAMESTORE_QueueEntry *qe;
190   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
191   return qe;
192 }
193
194 /**
195  * Store part of a signature B-tree in the namestore.  This function
196  * is used by non-authorities to cache parts of a zone's signature tree.
197  * Note that the tree must be build top-down.  This function must check
198  * that the nodes being added are valid, and if not refuse the operation.
199  *
200  * @param h handle to the namestore
201  * @param zone_key public key of the zone
202  * @param loc location in the B-tree
203  * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1), NULL for root
204  * @param top_sig signature at the top, NULL if 'loc.depth > 0'
205  * @param num_entries number of entries at this node in the B-tree
206  * @param entries the 'num_entries' entries to store (hashes over the
207  *                records)
208  * @param cont continuation to call when done
209  * @param cont_cls closure for cont
210  * @return handle to abort the request
211  */
212 struct GNUNET_NAMESTORE_QueueEntry *
213 GNUNET_NAMESTORE_stree_put (struct GNUNET_NAMESTORE_Handle *h,
214                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
215                             const struct GNUNET_NAMESTORE_SignatureLocation *loc,
216                             const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
217                             const struct GNUNET_CRYPTO_RsaSignature *sig,
218                             unsigned int num_entries,
219                             const GNUNET_HashCode *entries,
220                             GNUNET_NAMESTORE_ContinuationWithStatus cont,
221                             void *cont_cls)
222 {
223   struct GNUNET_NAMESTORE_QueueEntry *qe;
224   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
225   return qe;
226 }
227
228 /**
229  * Store an item in the namestore.  If the item is already present,
230  * the expiration time is updated to the max of the existing time and
231  * the new time.  The operation must fail if there is no matching
232  * entry in the signature tree.
233  *
234  * @param h handle to the namestore
235  * @param zone hash of the public key of the zone
236  * @param name name that is being mapped (at most 255 characters long)
237  * @param record_type type of the record (A, AAAA, PKEY, etc.)
238  * @param expiration expiration time for the content
239  * @param flags flags for the content
240  * @param sig_loc where is the information about the signature for this record stored?
241  * @param data_size number of bytes in data
242  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
243  *             GNS specification for GNS extensions)
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_put (struct GNUNET_NAMESTORE_Handle *h,
250                              const GNUNET_HashCode *zone,
251                              const char *name,
252                              uint32_t record_type,
253                              struct GNUNET_TIME_Absolute expiration,
254                              enum GNUNET_NAMESTORE_RecordFlags flags,
255                              const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
256                              size_t data_size,
257                              const void *data,
258                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
259                              void *cont_cls)
260 {
261   struct GNUNET_NAMESTORE_QueueEntry *qe;
262   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
263
264   struct GNUNET_NAMESTORE_SimpleRecord *sr;
265   sr = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_SimpleRecord));
266   sr->name = name;
267   sr->record_type = record_type;
268   sr->expiration = expiration;
269   sr->flags = flags;
270   sr->data_size = data_size;
271   sr->data = data;
272   GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
273   return qe;
274 }
275
276 /**
277  * Explicitly remove some content from the database.  The
278  * "cont"inuation will be called with status "GNUNET_OK" if content
279  * was removed, "GNUNET_NO" if no matching entry was found and
280  * "GNUNET_SYSERR" on all other types of errors.
281  *
282  * @param h handle to the namestore
283  * @param zone hash of the public key of the zone
284  * @param name name that is being mapped (at most 255 characters long)
285  * @param record_type type of the record (A, AAAA, PKEY, etc.)
286  * @param size number of bytes in data
287  * @param data content stored
288  * @param cont continuation to call when done
289  * @param cont_cls closure for cont
290  * @return handle to abort the request
291  */
292 struct GNUNET_NAMESTORE_QueueEntry *
293 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
294                                 const GNUNET_HashCode *zone,
295                                 const char *name,
296                                 uint32_t record_type,
297                                 size_t size,
298                                 const void *data,
299                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
300                                 void *cont_cls)
301 {
302   struct GNUNET_NAMESTORE_QueueEntry *qe;
303   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
304   
305   struct GNUNET_NAMESTORE_SimpleRecord *iter;
306   for (iter=h->records_head; iter != NULL; iter=iter->next)
307   {
308     if (strcmp ( iter->name, name ) &&
309         iter->record_type == record_type &&
310         GNUNET_CRYPTO_hash_cmp (iter->zone, zone))
311       break;
312   }
313   if (iter)
314     GNUNET_CONTAINER_DLL_remove(h->records_head,
315                                 h->records_tail,
316                                 iter);
317   
318   return qe;
319 }
320
321 /**
322  * Get a result for a particular key from the namestore.  The processor
323  * will only be called once.
324  *
325  * @param h handle to the namestore
326  * @param zone zone to look up a record from
327  * @param name name to look up
328  * @param record_type desired record type
329  * @param proc function to call on each matching value;
330  *        will be called once with a NULL value at the end
331  * @param proc_cls closure for proc
332  * @return a handle that can be used to
333  *         cancel
334  */
335 struct GNUNET_NAMESTORE_QueueEntry *
336 GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *h, 
337                               const GNUNET_HashCode *zone,
338                               const char *name,
339                               uint32_t record_type,
340                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
341 {
342   struct GNUNET_NAMESTORE_QueueEntry *qe;
343   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
344
345   struct GNUNET_NAMESTORE_SimpleRecord *iter;
346   for (iter=h->records_head; iter != NULL; iter=iter->next)
347   {
348     proc(proc_cls, iter->zone, iter->name, iter->record_type,
349        iter->expiration,
350        iter->flags,
351        NULL /*sig loc*/,
352        iter->data_size /*size*/,
353        iter->data /* data */);
354   }
355   proc(proc_cls, zone, name, record_type,
356        GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/
357
358   return qe;
359 }
360
361
362 /**
363  * Get the hash of a record (what will be signed in the Stree for
364  * the record).
365  *
366  * @param zone hash of the public key of the zone
367  * @param name name that is being mapped (at most 255 characters long)
368  * @param record_type type of the record (A, AAAA, PKEY, etc.)
369  * @param expiration expiration time for the content
370  * @param flags flags for the content
371  * @param data_size number of bytes in data
372  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and.
373  *             GNS specification for GNS extensions)
374  * @param record_hash hash of the record (set)
375  */
376 void
377 GNUNET_NAMESTORE_record_hash (struct GNUNET_NAMESTORE_Handle *h,
378                               const GNUNET_HashCode *zone,
379                               const char *name,
380                               uint32_t record_type,
381                               struct GNUNET_TIME_Absolute expiration,
382                               enum GNUNET_NAMESTORE_RecordFlags flags,
383                               size_t data_size,
384                               const void *data,
385                               GNUNET_HashCode *record_hash)
386 {
387   char* teststring = "namestore-stub";
388   GNUNET_CRYPTO_hash(teststring, strlen(teststring), record_hash);
389 }
390
391 /**
392  * Obtain part of a signature B-tree.  The processor
393  * will only be called once.
394  *
395  * @param h handle to the namestore
396  * @param zone zone to look up a record from
397  * @param sig_loc location to look up
398  * @param proc function to call on each matching value;
399  *        will be called once with a NULL value at the end
400  * @param proc_cls closure for proc
401  * @return a handle that can be used to
402  *         cancel
403  */
404 struct GNUNET_NAMESTORE_QueueEntry *
405 GNUNET_NAMESTORE_lookup_stree (struct GNUNET_NAMESTORE_Handle *h,
406                       const GNUNET_HashCode *zone,
407                       const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
408                       GNUNET_NAMESTORE_StreeProcessor proc, void *proc_cls)
409 {
410   struct GNUNET_NAMESTORE_QueueEntry *qe;
411   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
412   return qe;
413 }
414
415
416 /**
417  * Get all records of a zone.
418  *
419  * @param h handle to the namestore
420  * @param zone zone to access
421  * @param proc function to call on a random value; it
422  *        will be called repeatedly with a value (if available)
423  *        and always once at the end with a zone and name of NULL.
424  * @param proc_cls closure for proc
425  * @return a handle that can be used to
426  *         cancel
427  */
428 struct GNUNET_NAMESTORE_QueueEntry *
429 GNUNET_NAMESTORE_zone_transfer (struct GNUNET_NAMESTORE_Handle *h,
430                                 const GNUNET_HashCode *zone,
431                                 GNUNET_NAMESTORE_RecordProcessor proc,
432                                 void *proc_cls)
433 {
434   struct GNUNET_NAMESTORE_QueueEntry *qe;
435   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
436   return qe;
437 }
438
439
440
441
442 /**
443  * Cancel a namestore operation.  The final callback from the
444  * operation must not have been done yet.
445  *
446  * @param qe operation to cancel
447  */
448 void
449 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
450 {
451   if (qe)
452     GNUNET_free(qe);
453 }
454
455 /* end of namestore_api.c */