- fix doxygen
[oweals/gnunet.git] / src / gns / namestore_stub_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_stub_api.c
23  * @brief stub library to access the NAMESTORE service
24  * @author Martin Schanzenbach
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_namestore_service.h"
32
33 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
36
37 /**
38  * A QueueEntry.
39  */
40 struct GNUNET_NAMESTORE_QueueEntry
41 {
42   char *data; /*stub data pointer*/
43 };
44
45 /**
46  * Connection to the NAMESTORE service.
47  */
48 struct GNUNET_NAMESTORE_Handle
49 {
50
51   /**
52    * Configuration to use.
53    */
54   const struct GNUNET_CONFIGURATION_Handle *cfg;
55
56   /**
57    * Socket (if available).
58    */
59   struct GNUNET_CLIENT_Connection *client;
60
61   /**
62    * Currently pending transmission request (or NULL).
63    */
64   struct GNUNET_CLIENT_TransmitHandle *th;
65
66   /* dll to use for records */
67   struct GNUNET_NAMESTORE_SimpleRecord * records_head;
68   struct GNUNET_NAMESTORE_SimpleRecord * records_tail;
69
70   uint32_t locked;
71
72 };
73
74 struct GNUNET_NAMESTORE_ZoneIterator
75 {
76   struct GNUNET_NAMESTORE_Handle *handle;
77   GNUNET_NAMESTORE_RecordProcessor proc;
78   void* proc_cls;
79   const GNUNET_HashCode * zone;
80   uint32_t no_flags;
81   uint32_t flags;
82   struct GNUNET_NAMESTORE_Handle *h;
83   struct GNUNET_NAMESTORE_SimpleRecord *sr;
84 };
85
86 struct GNUNET_NAMESTORE_SimpleRecord
87 {
88   /**
89    * DLL
90    */
91   struct GNUNET_NAMESTORE_SimpleRecord *next;
92
93   /**
94    * DLL
95    */
96   struct GNUNET_NAMESTORE_SimpleRecord *prev;
97   
98   const char *name;
99   const GNUNET_HashCode *zone;
100   const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
101   uint32_t rd_count;
102   struct GNUNET_NAMESTORE_RecordData rd[100];
103 };
104
105
106 /**
107  * Initialize the connection with the NAMESTORE service.
108  *
109  * @param cfg configuration to use
110  * @return handle to the GNS service, or NULL on error
111  */
112 struct GNUNET_NAMESTORE_Handle *
113 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
114 {
115   struct GNUNET_NAMESTORE_Handle *handle;
116
117   handle = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
118   handle->cfg = cfg;
119   handle->records_head = NULL;
120   handle->records_tail = NULL;
121   return handle;
122 }
123
124 /**
125  * Shutdown connection with the NAMESTORE service.
126  *
127  * @param handle handle of the NAMESTORE connection to stop
128  */
129 void
130 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *handle, int drop)
131 {
132   GNUNET_free(handle);
133 }
134
135 /**
136  * Store an item in the namestore.  If the item is already present,
137  * the expiration time is updated to the max of the existing time and
138  * the new time.  The operation must fail if there is no matching
139  * entry in the signature tree.
140  *
141  * @param h handle to the namestore
142  * @param zone hash of the public key of the zone
143  * @param name name that is being mapped (at most 255 characters long)
144  * @param record_type type of the record (A, AAAA, PKEY, etc.)
145  * @param expiration expiration time for the content
146  * @param flags flags for the content
147  * @param data_size number of bytes in data
148  * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
149  *             GNS specification for GNS extensions)
150  * @param cont continuation to call when done
151  * @param cont_cls closure for cont
152  * @return handle to abort the request
153  */
154 struct GNUNET_NAMESTORE_QueueEntry *
155 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
156            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
157                              const char *name,
158                              struct GNUNET_TIME_Absolute expiration,
159                              unsigned int rd_count,
160            const struct GNUNET_NAMESTORE_RecordData *rd,
161            const struct GNUNET_CRYPTO_RsaSignature *signature,
162            GNUNET_NAMESTORE_ContinuationWithStatus cont,
163                              void *cont_cls)
164 {
165   struct GNUNET_NAMESTORE_QueueEntry *qe;
166   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
167   struct GNUNET_NAMESTORE_SimpleRecord* sr;
168   GNUNET_HashCode *zone;
169   int i;
170
171   zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
172   GNUNET_CRYPTO_hash(public_key,
173                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
174                      zone);
175
176   sr = h->records_head;
177   for (; sr != NULL; sr = sr->next)
178   {
179     if (GNUNET_CRYPTO_hash_cmp(zone, sr->zone) == 0)
180     {
181       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "records for %s already present ow\n",
182                  name);
183       sr->rd_count = rd_count;
184       for (i=0; i<rd_count; i++)
185       {
186         sr->rd[i] = rd[i];
187       }
188       //Expiration, Signature etc
189       return qe;
190     }
191   }
192   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "new records for %s\n", name);
193   // Not present
194   sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
195   sr->rd_count = rd_count;
196   sr->name = GNUNET_malloc(strlen(name));
197   sr->zone = zone;
198   sr->zone_key = public_key; //pkey FIXME;
199   sr->next = NULL;
200   sr->prev = NULL;
201   strcpy((char*)sr->name, name);
202   
203   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "copying \n");
204   for (i=0; i<rd_count; i++)
205     sr->rd[i] = rd[i];
206   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "done \n");
207   
208   if (h->records_head == NULL && h->records_tail == NULL)
209   {
210     h->records_head = sr;
211     h->records_tail = sr;
212   }
213   else
214   {
215     GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
216   }
217
218   return qe;
219 }
220
221 int
222 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
223                                    const char *name,
224                                    unsigned int rd_count,
225                                    const struct GNUNET_NAMESTORE_RecordData *rd,
226                                    const struct GNUNET_CRYPTO_RsaSignature *signature)
227 {
228   return GNUNET_OK;
229 }
230
231 struct GNUNET_NAMESTORE_QueueEntry *
232 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
233                              const struct GNUNET_CRYPTO_RsaPrivateKey *key,
234                              const char *name,
235            const struct GNUNET_NAMESTORE_RecordData *rd,
236            GNUNET_NAMESTORE_ContinuationWithStatus cont,
237                              void *cont_cls)
238 {
239   struct GNUNET_NAMESTORE_QueueEntry *qe;
240   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
241   struct GNUNET_NAMESTORE_SimpleRecord* sr;
242
243   GNUNET_HashCode *zone_hash;
244   
245   //memleakage.. but only stub so w/e
246   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
247   pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
248   GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
249
250   zone_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
251
252   GNUNET_CRYPTO_hash(pkey, GNUNET_CRYPTO_RSA_KEY_LENGTH, zone_hash);
253   
254   sr = h->records_head;
255   for (; sr != NULL; sr = sr->next)
256   {
257     if (strcmp(sr->name, name) && (sr->zone == zone_hash))
258     {
259       //Dangerous
260       memcpy (&(sr->rd[sr->rd_count-1]), rd,
261               sizeof(struct GNUNET_NAMESTORE_RecordData));
262
263       sr->rd_count++;
264       return qe;
265     }
266   }
267       
268   sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
269   
270   sr->rd_count = 1;
271   sr->name = GNUNET_malloc(strlen(name));
272   sr->zone = zone_hash;
273   sr->zone_key = pkey;
274   sr->next = NULL;
275   sr->prev = NULL;
276   strcpy((char*)sr->name, name);
277
278   memcpy (&(sr->rd), rd,
279           sizeof(struct GNUNET_NAMESTORE_RecordData));
280   if (h->records_head == NULL && h->records_tail == NULL)
281   {
282     h->records_head = sr;
283     h->records_tail = sr;
284   }
285   else
286   {
287     GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
288   }
289
290   return qe;
291 }
292
293 /**
294  * Explicitly remove some content from the database.  The
295  * "cont"inuation will be called with status "GNUNET_OK" if content
296  * was removed, "GNUNET_NO" if no matching entry was found and
297  * "GNUNET_SYSERR" on all other types of errors.
298  *
299  * @param h handle to the namestore
300  * @param zone hash of the public key of the zone
301  * @param name name that is being mapped (at most 255 characters long)
302  * @param record_type type of the record (A, AAAA, PKEY, etc.)
303  * @param size number of bytes in data
304  * @param data content stored
305  * @param cont continuation to call when done
306  * @param cont_cls closure for cont
307  * @return handle to abort the request
308  */
309 struct GNUNET_NAMESTORE_QueueEntry *
310 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
311                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
312                                 const char *name,
313                                 const struct GNUNET_NAMESTORE_RecordData *rd,
314         GNUNET_NAMESTORE_ContinuationWithStatus cont,
315                                 void *cont_cls)
316 {
317   struct GNUNET_NAMESTORE_QueueEntry *qe;
318   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
319   
320   //FIXME
321   return qe;
322 }
323
324 /**
325  * Get a result for a particular key from the namestore.  The processor
326  * will only be called once.
327  *
328  * @param h handle to the namestore
329  * @param zone zone to look up a record from
330  * @param name name to look up
331  * @param record_type desired record type
332  * @param proc function to call on each matching value;
333  *        will be called once with a NULL value at the end
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_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
340                               const GNUNET_HashCode *zone,
341                               const char *name,
342                               uint32_t record_type,
343                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
344 {
345   struct GNUNET_NAMESTORE_QueueEntry *qe;
346   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
347   struct GNUNET_NAMESTORE_SimpleRecord *sr;
348   
349   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Looking up %s\n", name);
350   sr = h->records_head;
351   for (; sr != NULL; sr = sr->next)
352   {
353     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Got %s\n", sr->name);
354     if ((strcmp(sr->name, name) == 0) &&
355         (0 == (GNUNET_CRYPTO_hash_cmp(sr->zone, zone))))
356     {
357       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
358                  "Found match for %s with %d entries\n",
359                  sr->name, sr->rd_count);
360       //Simply always return all records
361       proc(proc_cls, sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
362            name, sr->rd_count, sr->rd, NULL);
363       return qe;
364     }
365     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No match\n");
366   }
367   proc(proc_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, name, 0, NULL, NULL);
368   //FIXME
369   return qe;
370 }
371
372 struct GNUNET_NAMESTORE_ZoneIterator *
373 GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h,
374                                       const GNUNET_HashCode *zone,
375                                       enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
376                                       enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
377                                       GNUNET_NAMESTORE_RecordProcessor proc,
378                                       void *proc_cls)
379 {
380   struct GNUNET_NAMESTORE_ZoneIterator *it;
381   h->locked = 1;
382   it = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_ZoneIterator));
383   it->h = h;
384   it->sr = h->records_head;
385   it->proc = proc;
386   it->proc_cls = proc_cls;
387   it->zone = zone;
388   it->no_flags = must_not_have_flags;
389   it->flags = must_have_flags;
390   GNUNET_NAMESTORE_zone_iterator_next(it);
391   return it;
392 }
393
394 void
395 GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it)
396 {
397   
398   if (it->h->locked == 0)
399     return;
400   if (it->sr == NULL)
401   {
402     it->proc(it->proc_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS,
403              NULL, 0, NULL, NULL);
404     return;
405   }
406
407   if (GNUNET_CRYPTO_hash_cmp(it->sr->zone, it->zone))
408   {
409     //Simply always return all records
410     //check flags
411     it->proc(it->proc_cls, it->sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS,
412          it->sr->name, it->sr->rd_count, it->sr->rd, NULL);
413   }
414   it->sr = it->sr->next;
415 }
416
417 void
418 GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
419 {
420   //it->h->locked = 0;
421 }
422
423 /**
424  * Cancel a namestore operation.  The final callback from the
425  * operation must not have been done yet.
426  *
427  * @param qe operation to cancel
428  */
429 void
430 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
431 {
432   if (qe)
433     GNUNET_free(qe);
434 }
435
436
437
438
439 /* end of namestore_stub_api.c */