- adding assertion for name
[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       sr->rd_count = rd_count;
182       for (i=0; i<rd_count; i++)
183       {
184         sr->rd[i] = rd[i];
185       }
186       //Expiration, Signature etc
187       return qe;
188     }
189   }
190   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new records for %s\n", name);
191   // Not present
192   sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
193   sr->rd_count = rd_count;
194   sr->name = GNUNET_malloc(strlen(name));
195   sr->zone = zone;
196   sr->zone_key = public_key; //pkey FIXME;
197   sr->next = NULL;
198   sr->prev = NULL;
199   strcpy((char*)sr->name, name);
200   
201   for (i=0; i<rd_count; i++)
202     sr->rd[i] = rd[i];
203   
204   if (h->records_head == NULL && h->records_tail == NULL)
205   {
206     h->records_head = sr;
207     h->records_tail = sr;
208   }
209   else
210   {
211     GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
212   }
213
214   return qe;
215 }
216
217 int
218 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
219                                    const char *name,
220                                    unsigned int rd_count,
221                                    const struct GNUNET_NAMESTORE_RecordData *rd,
222                                    const struct GNUNET_CRYPTO_RsaSignature *signature)
223 {
224   return GNUNET_OK;
225 }
226
227 struct GNUNET_NAMESTORE_QueueEntry *
228 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
229                              const struct GNUNET_CRYPTO_RsaPrivateKey *key,
230                              const char *name,
231            const struct GNUNET_NAMESTORE_RecordData *rd,
232            GNUNET_NAMESTORE_ContinuationWithStatus cont,
233                              void *cont_cls)
234 {
235   struct GNUNET_NAMESTORE_QueueEntry *qe;
236   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
237   struct GNUNET_NAMESTORE_SimpleRecord* sr;
238
239   GNUNET_HashCode *zone_hash;
240   
241   //memleakage.. but only stub so w/e
242   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
243   pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
244   GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
245
246   zone_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
247
248   GNUNET_CRYPTO_hash(pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
249                      zone_hash);
250   
251   sr = h->records_head;
252   for (; sr != NULL; sr = sr->next)
253   {
254     if ((strcmp(sr->name, name) == 0) &&
255         (0 == GNUNET_CRYPTO_hash_cmp(sr->zone, zone_hash)))
256     {
257       //Dangerous
258       memcpy (&(sr->rd[sr->rd_count-1]), rd,
259               sizeof(struct GNUNET_NAMESTORE_RecordData));
260
261       sr->rd_count++;
262       return qe;
263     }
264   }
265       
266   sr = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_SimpleRecord));
267   
268   sr->rd_count = 1;
269   sr->name = GNUNET_malloc(strlen(name));
270   sr->zone = zone_hash;
271   sr->zone_key = pkey;
272   sr->next = NULL;
273   sr->prev = NULL;
274   strcpy((char*)sr->name, name);
275
276   memcpy (&(sr->rd), rd,
277           sizeof(struct GNUNET_NAMESTORE_RecordData));
278   if (h->records_head == NULL && h->records_tail == NULL)
279   {
280     h->records_head = sr;
281     h->records_tail = sr;
282   }
283   else
284   {
285     GNUNET_CONTAINER_DLL_insert(h->records_head, h->records_tail, sr);
286   }
287
288   return qe;
289 }
290
291 /**
292  * Explicitly remove some content from the database.  The
293  * "cont"inuation will be called with status "GNUNET_OK" if content
294  * was removed, "GNUNET_NO" if no matching entry was found and
295  * "GNUNET_SYSERR" on all other types of errors.
296  *
297  * @param h handle to the namestore
298  * @param zone hash of the public key of the zone
299  * @param name name that is being mapped (at most 255 characters long)
300  * @param record_type type of the record (A, AAAA, PKEY, etc.)
301  * @param size number of bytes in data
302  * @param data content stored
303  * @param cont continuation to call when done
304  * @param cont_cls closure for cont
305  * @return handle to abort the request
306  */
307 struct GNUNET_NAMESTORE_QueueEntry *
308 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
309                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
310                                 const char *name,
311                                 const struct GNUNET_NAMESTORE_RecordData *rd,
312         GNUNET_NAMESTORE_ContinuationWithStatus cont,
313                                 void *cont_cls)
314 {
315   struct GNUNET_NAMESTORE_QueueEntry *qe;
316   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
317   
318   //FIXME
319   return qe;
320 }
321
322 /**
323  * Get a result for a particular key from the namestore.  The processor
324  * will only be called once.
325  *
326  * @param h handle to the namestore
327  * @param zone zone to look up a record from
328  * @param name name to look up
329  * @param record_type desired record type
330  * @param proc function to call on each matching value;
331  *        will be called once with a NULL value at the end
332  * @param proc_cls closure for proc
333  * @return a handle that can be used to
334  *         cancel
335  */
336 struct GNUNET_NAMESTORE_QueueEntry *
337 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
338                               const GNUNET_HashCode *zone,
339                               const char *name,
340                               uint32_t record_type,
341                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
342 {
343   struct GNUNET_NAMESTORE_QueueEntry *qe;
344   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
345   struct GNUNET_NAMESTORE_SimpleRecord *sr;
346   struct GNUNET_CRYPTO_HashAsciiEncoded zone_string, zone_string_ex;
347   
348   GNUNET_CRYPTO_hash_to_enc (zone, &zone_string);
349   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Looking up %s in %s\n", name, (char*)&zone_string);
350   sr = h->records_head;
351   for (; sr != NULL; sr = sr->next)
352   {
353     GNUNET_CRYPTO_hash_to_enc (sr->zone, &zone_string_ex);
354     if ((strcmp(sr->name, name) == 0) &&
355         (0 == (GNUNET_CRYPTO_hash_cmp(sr->zone, zone))))
356     {
357       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
358                  "Found match for %s in %s with %d entries\n",
359                  sr->name, (char*)&zone_string_ex, 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_DEBUG, "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   if (GNUNET_CRYPTO_hash_cmp(it->sr->zone, it->zone) == 0)
407   {
408     //Simply always return all records
409     //check flags
410     it->proc(it->proc_cls, it->sr->zone_key, GNUNET_TIME_UNIT_FOREVER_ABS,
411          it->sr->name, it->sr->rd_count, it->sr->rd, NULL);
412   }
413   it->sr = it->sr->next;
414 }
415
416 void
417 GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
418 {
419   //it->h->locked = 0;
420 }
421
422 /**
423  * Cancel a namestore operation.  The final callback from the
424  * operation must not have been done yet.
425  *
426  * @param qe operation to cancel
427  */
428 void
429 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
430 {
431   if (qe)
432     GNUNET_free(qe);
433 }
434
435
436
437
438 /* end of namestore_stub_api.c */