- changes
[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 namestore/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_crypto_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "namestore.h"
35
36 #include "platform.h"
37 #include <gcrypt.h>
38 #include "gnunet_common.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_disk_lib.h"
41
42 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
43
44 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
45
46 /**
47  * A QueueEntry.
48  */
49 struct GNUNET_NAMESTORE_QueueEntry
50 {
51   struct GNUNET_NAMESTORE_QueueEntry *next;
52   struct GNUNET_NAMESTORE_QueueEntry *prev;
53
54   struct GNUNET_NAMESTORE_Handle *nsh;
55
56   uint32_t op_id;
57
58   GNUNET_NAMESTORE_ContinuationWithStatus cont;
59   void *cont_cls;
60
61   GNUNET_NAMESTORE_RecordProcessor proc;
62   void *proc_cls;
63
64   char *data; /*stub data pointer*/
65 };
66
67
68 /**
69  * Zone iterator
70  */
71 struct GNUNET_NAMESTORE_ZoneIterator
72 {
73   struct GNUNET_NAMESTORE_ZoneIterator *next;
74   struct GNUNET_NAMESTORE_ZoneIterator *prev;
75
76   uint32_t op_id;
77
78   struct GNUNET_NAMESTORE_Handle *h;
79   GNUNET_NAMESTORE_RecordProcessor proc;
80   void* proc_cls;
81   GNUNET_HashCode zone;
82   uint32_t no_flags;
83   uint32_t flags;
84 };
85
86
87 /**
88  * Message in linked list we should send to the service.  The
89  * actual binary message follows this struct.
90  */
91 struct PendingMessage
92 {
93
94   /**
95    * Kept in a DLL.
96    */
97   struct PendingMessage *next;
98
99   /**
100    * Kept in a DLL.
101    */
102   struct PendingMessage *prev;
103
104   /**
105    * Size of the message.
106    */
107   size_t size;
108
109   /**
110    * Is this the 'START' message?
111    */
112   int is_init;
113 };
114
115
116 /**
117  * Connection to the NAMESTORE service.
118  */
119 struct GNUNET_NAMESTORE_Handle
120 {
121
122   /**
123    * Configuration to use.
124    */
125   const struct GNUNET_CONFIGURATION_Handle *cfg;
126
127   /**
128    * Socket (if available).
129    */
130   struct GNUNET_CLIENT_Connection *client;
131
132   /**
133    * Currently pending transmission request (or NULL).
134    */
135   struct GNUNET_CLIENT_TransmitHandle *th;
136
137   /**
138    * Reconnect task
139    */
140   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
141
142   /**
143    * Pending messages to send to the service
144    */
145
146   struct PendingMessage * pending_head;
147   struct PendingMessage * pending_tail;
148
149   /**
150    * Should we reconnect to service due to some serious error?
151    */
152   int reconnect;
153
154
155   /**
156    * Pending namestore queue entries
157    */
158   struct GNUNET_NAMESTORE_QueueEntry * op_head;
159   struct GNUNET_NAMESTORE_QueueEntry * op_tail;
160
161   uint32_t op_id;
162
163   /**
164    * Pending namestore zone iterator entries
165    */
166   struct GNUNET_NAMESTORE_ZoneIterator * z_head;
167   struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
168 };
169
170 struct GNUNET_NAMESTORE_SimpleRecord
171 {
172   /**
173    * DLL
174    */
175   struct GNUNET_NAMESTORE_SimpleRecord *next;
176
177   /**
178    * DLL
179    */
180   struct GNUNET_NAMESTORE_SimpleRecord *prev;
181   
182   const char *name;
183   const GNUNET_HashCode *zone;
184   uint32_t record_type;
185   struct GNUNET_TIME_Absolute expiration;
186   enum GNUNET_NAMESTORE_RecordFlags flags;
187   size_t data_size;
188   const void *data;
189 };
190
191
192 /**
193  * Disconnect from service and then reconnect.
194  *
195  * @param h our handle
196  */
197 static void
198 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
199
200 static void
201 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
202                              struct LookupNameResponseMessage * msg,
203                              size_t size)
204 {
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
206               "LOOKUP_NAME_RESPONSE");
207
208   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
209   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
210   char *name;
211   char * rd_tmp;
212
213   struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
214   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
215   struct GNUNET_TIME_Absolute expire;
216   size_t exp_msg_len;
217   size_t msg_len = 0;
218   size_t name_len = 0;
219   size_t rd_len = 0;
220   int contains_sig = GNUNET_NO;
221   int rd_count = 0;
222
223   rd_len = ntohs (msg->rd_len);
224   rd_count = ntohs (msg->rd_count);
225   msg_len = ntohs (msg->gns_header.header.size);
226   name_len = ntohs (msg->name_len);
227   contains_sig = ntohs (msg->contains_sig);
228   expire = GNUNET_TIME_absolute_ntoh(msg->expire);
229
230   exp_msg_len = sizeof (struct LookupNameResponseMessage) +
231       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
232       name_len +
233       rd_len +
234       contains_sig * sizeof (struct GNUNET_CRYPTO_RsaSignature);
235
236   if (msg_len != exp_msg_len)
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
239                 msg_len, exp_msg_len);
240     GNUNET_break_op (0);
241     return;
242   }
243
244   zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &msg[1];
245   name = (char *) &zone_key[1];
246   rd_tmp = &name[name_len];
247
248   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
249   GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
250
251   /* reset values if values not contained */
252   if (contains_sig == GNUNET_NO)
253     signature = NULL;
254   else
255     signature = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_len];
256   if (name_len == 0)
257     name = NULL;
258
259   memset (&dummy, '0', sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
260   if (0 == memcmp (zone_key, &dummy, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
261       zone_key = NULL;
262
263   if (qe->proc != NULL)
264   {
265     qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
266   }
267
268   /* Operation done, remove */
269   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
270   GNUNET_free (qe);
271 }
272
273
274 static void
275 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
276                              struct RecordPutResponseMessage* msg,
277                              size_t size)
278 {
279   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
280               "RECORD_PUT_RESPONSE");
281
282   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
283   int res = GNUNET_OK;
284
285   if (ntohs (msg->op_result) == GNUNET_OK)
286   {
287     res = GNUNET_OK;
288     if (qe->cont != NULL)
289     {
290       qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
291     }
292
293   }
294   else if (ntohs (msg->op_result) == GNUNET_NO)
295   {
296     res = GNUNET_SYSERR;
297     if (qe->cont != NULL)
298     {
299       qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
300     }
301   }
302   else
303   {
304     GNUNET_break_op (0);
305     return;
306   }
307
308   /* Operation done, remove */
309   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
310
311   GNUNET_free (qe);
312 }
313
314
315 static void
316 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
317                              struct RecordCreateResponseMessage* msg,
318                              size_t size)
319 {
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
321               "RECORD_CREATE_RESPONSE");
322
323   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
324   int res = GNUNET_OK;
325
326   if (ntohs (msg->op_result) == GNUNET_OK)
327   {
328     res = GNUNET_OK;
329     if (qe->cont != NULL)
330     {
331       qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
332     }
333
334   }
335   else if (ntohs (msg->op_result) == GNUNET_NO)
336   {
337     res = GNUNET_SYSERR;
338     if (qe->cont != NULL)
339     {
340       qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
341     }
342   }
343   else
344   {
345     GNUNET_break_op (0);
346     return;
347   }
348
349   /* Operation done, remove */
350   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
351
352   GNUNET_free (qe);
353 }
354
355
356 static void
357 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
358                           const struct GNUNET_MessageHeader *msg,
359                           int type, size_t size)
360 {
361
362   /* handle different message type */
363   switch (type) {
364     case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
365         if (size < sizeof (struct LookupNameResponseMessage))
366         {
367           GNUNET_break_op (0);
368           break;
369         }
370         handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
371       break;
372     case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
373         if (size != sizeof (struct RecordPutResponseMessage))
374         {
375           GNUNET_break_op (0);
376           break;
377         }
378         handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
379       break;
380     case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
381         if (size != sizeof (struct RecordCreateResponseMessage))
382         {
383           GNUNET_break_op (0);
384           break;
385         }
386         handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
387       break;
388     default:
389       GNUNET_break_op (0);
390       break;
391   }
392 }
393
394 static void
395 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
396                                 struct ZoneIterationResponseMessage *msg,
397                                 size_t size)
398 {
399   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
400               "ZONE_ITERATION_RESPONSE");
401
402
403   if (ze->proc != NULL)
404   {
405     // FIXME
406     ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_forever(), "dummy", 0, NULL, NULL);
407   }
408 }
409
410
411 static void
412 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
413                         const struct GNUNET_MessageHeader *msg,
414                         int type, size_t size)
415 {
416
417   /* handle different message type */
418   switch (type) {
419     case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
420         if (size < sizeof (struct ZoneIterationResponseMessage))
421         {
422           GNUNET_break_op (0);
423           break;
424         }
425         handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
426       break;
427     default:
428       GNUNET_break_op (0);
429       break;
430   }
431 }
432
433 /**
434  * Type of a function to call when we receive a message
435  * from the service.
436  *
437  * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
438  * @param msg message received, NULL on timeout or fatal error
439  */
440 static void
441 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
442 {
443   struct GNUNET_NAMESTORE_Handle *h = cls;
444   struct GNUNET_NAMESTORE_Header * gm;
445   struct GNUNET_NAMESTORE_QueueEntry *qe;
446   struct GNUNET_NAMESTORE_ZoneIterator *ze;
447   uint16_t size;
448   uint16_t type;
449   uint32_t r_id = UINT32_MAX;
450
451   if (NULL == msg)
452   {
453     force_reconnect (h);
454     return;
455   }
456
457   size = ntohs (msg->size);
458   type = ntohs (msg->type);
459
460   if (size < sizeof (struct GNUNET_NAMESTORE_Header))
461   {
462     GNUNET_break_op (0);
463     GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
464                            GNUNET_TIME_UNIT_FOREVER_REL);
465     return;
466   }
467
468   gm = (struct GNUNET_NAMESTORE_Header *) msg;
469   r_id = ntohl (gm->r_id);
470
471   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
472
473   /* Find matching operation */
474   if (r_id > h->op_id)
475   {
476     /* No matching pending operation found */
477     GNUNET_break_op (0);
478     GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
479                            GNUNET_TIME_UNIT_FOREVER_REL);
480     return;
481   }
482
483   /* Is it a record related operation ? */
484   for (qe = h->op_head; qe != NULL; qe = qe->next)
485   {
486     if (qe->op_id == r_id)
487       break;
488   }
489   if (qe != NULL)
490   {
491     manage_record_operations (qe, msg, type, size);
492   }
493
494   /* Is it a zone iteration operation ? */
495   for (ze = h->z_head; ze != NULL; ze = ze->next)
496   {
497     if (ze->op_id == r_id)
498       break;
499   }
500   if (ze != NULL)
501   {
502     manage_zone_operations (ze, msg, type, size);
503   }
504
505   GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
506                          GNUNET_TIME_UNIT_FOREVER_REL);
507
508   if (GNUNET_YES == h->reconnect)
509     force_reconnect (h);
510
511 }
512
513
514 /**
515  * Transmit messages from the message queue to the service
516  * (if there are any, and if we are not already trying).
517  *
518  * @param h handle to use
519  */
520 static void
521 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
522
523
524 /**
525  * We can now transmit a message to NAMESTORE. Do it.
526  *
527  * @param cls the 'struct GNUNET_NAMESTORE_Handle'
528  * @param size number of bytes we can transmit
529  * @param buf where to copy the messages
530  * @return number of bytes copied into buf
531  */
532 static size_t
533 transmit_message_to_namestore (void *cls, size_t size, void *buf)
534 {
535   struct GNUNET_NAMESTORE_Handle *h = cls;
536   struct PendingMessage *p;
537   size_t ret;
538   char *cbuf;
539
540   h->th = NULL;
541   if ((size == 0) || (buf == NULL))
542   {
543     force_reconnect (h);
544     return 0;
545   }
546   ret = 0;
547   cbuf = buf;
548   while ((NULL != (p = h->pending_head)) && (p->size <= size))
549   {
550     memcpy (&cbuf[ret], &p[1], p->size);
551     ret += p->size;
552     size -= p->size;
553     GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
554     if (GNUNET_YES == p->is_init)
555       GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
556                              GNUNET_TIME_UNIT_FOREVER_REL);
557     GNUNET_free (p);
558   }
559   do_transmit (h);
560   return ret;
561 }
562
563
564 /**
565  * Transmit messages from the message queue to the service
566  * (if there are any, and if we are not already trying).
567  *
568  * @param h handle to use
569  */
570 static void
571 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
572 {
573   struct PendingMessage *p;
574
575   if (NULL != h->th)
576     return;
577   if (NULL == (p = h->pending_head))
578     return;
579   if (NULL == h->client)
580     return;                     /* currently reconnecting */
581
582   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
583                                            GNUNET_TIME_UNIT_FOREVER_REL,
584                                            GNUNET_NO, &transmit_message_to_namestore,
585                                            h);
586 }
587
588
589 /**
590  * Reconnect to namestore service.
591  *
592  * @param h the handle to the namestore service
593  */
594 static void
595 reconnect (struct GNUNET_NAMESTORE_Handle *h)
596 {
597   struct PendingMessage *p;
598   struct StartMessage *init;
599
600   GNUNET_assert (NULL == h->client);
601   h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
602   GNUNET_assert (NULL != h->client);
603
604   if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
605   {
606     p = GNUNET_malloc (sizeof (struct PendingMessage) +
607                        sizeof (struct StartMessage));
608     p->size = sizeof (struct StartMessage);
609     p->is_init = GNUNET_YES;
610     init = (struct StartMessage *) &p[1];
611     init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
612     init->header.size = htons (sizeof (struct StartMessage));
613     GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
614   }
615   do_transmit (h);
616 }
617
618 /**
619  * Re-establish the connection to the service.
620  *
621  * @param cls handle to use to re-connect.
622  * @param tc scheduler context
623  */
624 static void
625 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
626 {
627   struct GNUNET_NAMESTORE_Handle *h = cls;
628
629   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
630   reconnect (h);
631 }
632
633
634 /**
635  * Disconnect from service and then reconnect.
636  *
637  * @param h our handle
638  */
639 static void
640 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
641 {
642   h->reconnect = GNUNET_NO;
643   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
644   h->client = NULL;
645   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
646                                     &reconnect_task,
647                                     h);
648 }
649
650 static uint32_t
651 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
652 {
653   uint32_t op_id = h->op_id;
654   h->op_id ++;
655   return op_id;
656 }
657
658 /**
659  * Initialize the connection with the NAMESTORE service.
660  *
661  * @param cfg configuration to use
662  * @return handle to the GNS service, or NULL on error
663  */
664 struct GNUNET_NAMESTORE_Handle *
665 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
666 {
667   struct GNUNET_NAMESTORE_Handle *h;
668
669   h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
670   h->cfg = cfg;
671   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
672   h->op_id = 0;
673   return h;
674 }
675
676
677 /**
678  * Disconnect from the namestore service (and free associated
679  * resources).
680  *
681  * @param h handle to the namestore
682  * @param drop set to GNUNET_YES to delete all data in namestore (!)
683  */
684 void
685 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
686 {
687   struct PendingMessage *p;
688   struct GNUNET_NAMESTORE_QueueEntry *q;
689   struct GNUNET_NAMESTORE_ZoneIterator *z;
690
691   GNUNET_assert (h != NULL);
692
693   while (NULL != (p = h->pending_head))
694   {
695     GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
696     GNUNET_free (p);
697   }
698
699   while (NULL != (q = h->op_head))
700   {
701     GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
702     GNUNET_free (q);
703   }
704
705   while (NULL != (z = h->z_head))
706   {
707     GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
708     GNUNET_free (z);
709   }
710
711   if (NULL != h->client)
712   {
713     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
714     h->client = NULL;
715   }
716   if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
717   {
718     GNUNET_SCHEDULER_cancel (h->reconnect_task);
719     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
720   }
721   GNUNET_free(h);
722   h = NULL;
723 }
724
725
726 /**
727  * Store an item in the namestore.  If the item is already present,
728  * the expiration time is updated to the max of the existing time and
729  * the new time.  This API is used when we cache signatures from other
730  * authorities.
731  *
732  * @param h handle to the namestore
733  * @param zone_key public key of the zone
734  * @param name name that is being mapped (at most 255 characters long)
735  * @param expire when does the corresponding block in the DHT expire (until
736  *               when should we never do a DHT lookup for the same name again)?
737  * @param rd_count number of entries in 'rd' array
738  * @param rd array of records with data to store
739  * @param signature signature for all the records in the zone under the given name
740  * @param cont continuation to call when done
741  * @param cont_cls closure for cont
742  * @return handle to abort the request
743  */
744 struct GNUNET_NAMESTORE_QueueEntry *
745 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
746                              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
747                              const char *name,
748                              struct GNUNET_TIME_Absolute expire,
749                              unsigned int rd_count,
750                              const struct GNUNET_NAMESTORE_RecordData *rd,
751                              const struct GNUNET_CRYPTO_RsaSignature *signature,
752                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
753                              void *cont_cls)
754 {
755   struct GNUNET_NAMESTORE_QueueEntry *qe;
756   struct PendingMessage *pe;
757
758   /* pointer to elements */
759   char * zone_key_tmp;
760   char * rd_tmp;
761   char * name_tmp;
762
763   size_t msg_size = 0;
764   size_t name_len = 0;
765   size_t rd_ser_len = 0;
766   size_t pubkey_len = 0;
767   uint32_t rid = 0;
768
769   GNUNET_assert (NULL != h);
770   GNUNET_assert (NULL != zone_key);
771   GNUNET_assert (NULL != name);
772   GNUNET_assert (NULL != rd);
773   GNUNET_assert (NULL != signature);
774
775   name_len = strlen(name) + 1;
776   if (name_len > 256)
777   {
778     GNUNET_break (0);
779     return NULL;
780   }
781
782   rid = get_op_id(h);
783   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
784   qe->nsh = h;
785   qe->cont = cont;
786   qe->cont_cls = cont_cls;
787   qe->op_id = rid;
788   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
789
790   /* set msg_size*/
791   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
792   char rd_ser[rd_ser_len];
793   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
794
795   pubkey_len = sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
796   struct RecordPutMessage * msg;
797   msg_size = sizeof (struct RecordPutMessage) + pubkey_len + name_len  + rd_ser_len;
798
799   /* create msg here */
800   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
801   pe->size = msg_size;
802   pe->is_init = GNUNET_NO;
803   msg = (struct RecordPutMessage *) &pe[1];
804   zone_key_tmp = (char *) &msg[1];
805   name_tmp = (char *) &zone_key_tmp[pubkey_len];
806   rd_tmp = &name_tmp[name_len];
807
808   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
809   msg->gns_header.header.size = htons (msg_size);
810   msg->gns_header.r_id = htonl (rid);
811   msg->key_len = htons (pubkey_len);
812   memcpy (zone_key_tmp, zone_key, pubkey_len);
813   msg->signature = *signature;
814   msg->name_len = htons (name_len);
815   memcpy (name_tmp, name, name_len);
816   msg->expire = GNUNET_TIME_absolute_hton (expire);
817   msg->rd_len = htons (rd_ser_len);
818
819
820   memcpy (rd_tmp, rd_ser, rd_ser_len);
821   GNUNET_free (rd_ser);
822
823   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
824
825   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
826   do_transmit(h);
827
828   return qe;
829 }
830
831
832 /**
833  * Check if a signature is valid.  This API is used by the GNS Block
834  * to validate signatures received from the network.
835  *
836  * @param public_key public key of the zone
837  * @param name name that is being mapped (at most 255 characters long)
838  * @param rd_count number of entries in 'rd' array
839  * @param rd array of records with data to store
840  * @param signature signature for all the records in the zone under the given name
841  * @return GNUNET_OK if the signature is valid
842  */
843 int
844 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
845                                    const char *name,
846                                    unsigned int rd_count,
847                                    const struct GNUNET_NAMESTORE_RecordData *rd,
848                                    const struct GNUNET_CRYPTO_RsaSignature *signature)
849 {
850   return GNUNET_SYSERR;
851 }
852
853 /**
854  * Store an item in the namestore.  If the item is already present,
855  * the expiration time is updated to the max of the existing time and
856  * the new time.  This API is used by the authority of a zone.
857  *
858  * @param h handle to the namestore
859  * @param pkey private key of the zone
860  * @param name name that is being mapped (at most 255 characters long)
861  * @param rd record data to store
862  * @param cont continuation to call when done
863  * @param cont_cls closure for cont
864  * @return handle to abort the request
865  */
866 struct GNUNET_NAMESTORE_QueueEntry *
867 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
868                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
869                                 const char *name,
870                                 const struct GNUNET_NAMESTORE_RecordData *rd,
871                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
872                                 void *cont_cls)
873 {
874   struct GNUNET_NAMESTORE_QueueEntry *qe;
875   struct PendingMessage *pe;
876   char * name_tmp;
877   char * pkey_tmp;
878   char * rd_tmp;
879   size_t rd_ser_len = 0;
880   size_t msg_size = 0;
881   size_t name_len = 0;
882   size_t key_len = 0;
883   uint32_t rid = 0;
884
885   GNUNET_assert (NULL != h);
886   GNUNET_assert (NULL != pkey);
887   GNUNET_assert (NULL != name);
888   GNUNET_assert (NULL != rd);
889
890   name_len = strlen(name) + 1;
891   if (name_len > 256)
892   {
893     GNUNET_break (0);
894     return NULL;
895   }
896
897   rid = get_op_id(h);
898   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
899   qe->nsh = h;
900   qe->cont = cont;
901   qe->cont_cls = cont_cls;
902   qe->op_id = rid;
903   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
904
905   /* set msg_size*/
906   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
907   GNUNET_assert (pkey_enc != NULL);
908   key_len = ntohs (pkey_enc->len);
909
910   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
911   char rd_ser[rd_ser_len];
912   GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
913
914   struct RecordCreateMessage * msg;
915   msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
916
917   /* create msg here */
918   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
919   pe->size = msg_size;
920   pe->is_init = GNUNET_NO;
921   msg = (struct RecordCreateMessage *) &pe[1];
922
923   pkey_tmp = (char *) &msg[1];
924   name_tmp = &pkey_tmp[key_len];
925   rd_tmp = &name_tmp[name_len];
926
927   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
928   msg->gns_header.header.size = htons (msg_size);
929   msg->gns_header.r_id = htonl (rid);
930   msg->name_len = htons (name_len);
931   msg->rd_len = htons (rd_ser_len);
932   msg->pkey_len = htons (key_len);
933   memcpy (pkey_tmp, pkey_enc, key_len);
934   memcpy (name_tmp, name, name_len);
935   memcpy (rd_tmp, rd_ser, rd_ser_len);
936   GNUNET_free (rd_ser);
937   GNUNET_free (pkey_enc);
938
939   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
940
941   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
942   do_transmit(h);
943   return qe;
944 }
945
946
947 /**
948  * Explicitly remove some content from the database.  The
949  * "cont"inuation will be called with status "GNUNET_OK" if content
950  * was removed, "GNUNET_NO" if no matching entry was found and
951  * "GNUNET_SYSERR" on all other types of errors.
952  * This API is used by the authority of a zone.
953  *
954  * @param h handle to the namestore
955  * @param pkey private key of the zone
956  * @param name name that is being mapped (at most 255 characters long)
957  * @param rd record data
958  * @param cont continuation to call when done
959  * @param cont_cls closure for cont
960  * @return handle to abort the request
961  */
962 struct GNUNET_NAMESTORE_QueueEntry *
963 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
964                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
965                                 const char *name,
966                                 const struct GNUNET_NAMESTORE_RecordData *rd,
967                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
968                                 void *cont_cls)
969 {
970   struct GNUNET_NAMESTORE_QueueEntry *qe;
971   struct PendingMessage *pe;
972   char * rd_tmp;
973   char * name_tmp;
974   size_t rd_ser_len = 0;
975   size_t msg_size = 0;
976   size_t name_len = 0;
977   uint32_t rid = 0;
978
979   GNUNET_assert (NULL != h);
980
981   rid = get_op_id(h);
982   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
983   qe->nsh = h;
984   qe->cont = cont;
985   qe->cont_cls = cont_cls;
986   qe->op_id = rid;
987
988   /* set msg_size*/
989   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
990   char rd_ser[rd_ser_len];
991   GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
992
993   struct RecordRemoveMessage * msg;
994   msg_size = sizeof (struct RecordRemoveMessage) + name_len + rd_ser_len;
995
996   /* create msg here */
997   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
998   pe->size = msg_size;
999   pe->is_init = GNUNET_NO;
1000   msg = (struct RecordRemoveMessage *) &pe[1];
1001
1002   name_tmp = (char *) &msg[1];
1003   rd_tmp = &name_tmp[name_len];
1004
1005   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1006   msg->gns_header.header.size = htons (msg_size);
1007   msg->gns_header.r_id = htonl (rid);
1008   //msg->signature = *signature;
1009   msg->name_len = htons (name_len);
1010   memcpy (name_tmp, name, name_len);
1011   memcpy (rd_tmp, rd_ser, rd_ser_len);
1012   GNUNET_free (rd_ser);
1013
1014   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1015
1016   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1017   do_transmit(h);
1018   return qe;
1019 }
1020
1021
1022 /**
1023  * Get a result for a particular key from the namestore.  The processor
1024  * will only be called once.  
1025  *
1026  * @param h handle to the namestore
1027  * @param zone zone to look up a record from
1028  * @param name name to look up
1029  * @param record_type desired record type, 0 for all
1030  * @param proc function to call on the matching records, or with
1031  *        NULL (rd_count == 0) if there are no matching records
1032  * @param proc_cls closure for proc
1033  * @return a handle that can be used to
1034  *         cancel
1035  */
1036 struct GNUNET_NAMESTORE_QueueEntry *
1037 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
1038                               const GNUNET_HashCode *zone,
1039                               const char *name,
1040                               uint32_t record_type,
1041                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1042 {
1043   struct GNUNET_NAMESTORE_QueueEntry *qe;
1044   struct PendingMessage *pe;
1045   size_t msg_size = 0;
1046   size_t name_len = 0;
1047   uint32_t rid = 0;
1048
1049   GNUNET_assert (NULL != h);
1050   GNUNET_assert (NULL != zone);
1051   GNUNET_assert (NULL != name);
1052
1053   name_len = strlen (name) + 1;
1054   if ((name_len == 0) || (name_len > 256))
1055   {
1056     GNUNET_break (0);
1057     return NULL;
1058   }
1059
1060   rid = get_op_id(h);
1061   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1062   qe->nsh = h;
1063   qe->proc = proc;
1064   qe->proc_cls = proc_cls;
1065   qe->op_id = rid;
1066   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1067
1068   /* set msg_size*/
1069   msg_size = sizeof (struct LookupNameMessage) + name_len;
1070   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1071
1072   /* create msg here */
1073   struct LookupNameMessage * msg;
1074   pe->size = msg_size;
1075   pe->is_init = GNUNET_NO;
1076   msg = (struct LookupNameMessage *) &pe[1];
1077   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1078   msg->gns_header.header.size = htons (msg_size);
1079   msg->gns_header.r_id = htonl (rid);
1080   msg->record_type = htonl (record_type);
1081   msg->zone = *zone;
1082   msg->name_len = htonl (name_len);
1083   memcpy (&msg[1], name, name_len);
1084
1085   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1086
1087   /* transmit message */
1088   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1089   do_transmit(h);
1090
1091   return qe;
1092 }
1093
1094
1095
1096 /**
1097  * Starts a new zone iteration (used to periodically PUT all of our
1098  * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1099  * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1100  * GNUNET_NAMESTORE_zone_iteration_stop.  "proc" will be called once
1101  * immediately, and then again after
1102  * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1103  *
1104  * @param h handle to the namestore
1105  * @param zone zone to access, NULL for all zones
1106  * @param must_have_flags flags that must be set for the record to be returned
1107  * @param must_not_have_flags flags that must NOT be set for the record to be returned
1108  * @param proc function to call on each name from the zone; it
1109  *        will be called repeatedly with a value (if available)
1110  *        and always once at the end with a name of NULL.
1111  * @param proc_cls closure for proc
1112  * @return an iterator handle to use for iteration
1113  */
1114 struct GNUNET_NAMESTORE_ZoneIterator *
1115 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1116                                        const GNUNET_HashCode *zone,
1117                                        enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1118                                        enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1119                                        GNUNET_NAMESTORE_RecordProcessor proc,
1120                                        void *proc_cls)
1121 {
1122   struct GNUNET_NAMESTORE_ZoneIterator *it;
1123   struct PendingMessage *pe;
1124   size_t msg_size = 0;
1125   uint32_t rid = 0;
1126
1127   GNUNET_assert (NULL != h);
1128   GNUNET_assert (NULL != zone);
1129
1130   rid = get_op_id(h);
1131   it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1132   it->h = h;
1133   it->proc = proc;
1134   it->proc_cls = proc;
1135   it->op_id = rid;
1136   it->zone = *zone;
1137   GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1138
1139   /* set msg_size*/
1140   msg_size = sizeof (struct ZoneIterationStartMessage);
1141   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1142
1143   /* create msg here */
1144   struct ZoneIterationStartMessage * msg;
1145   pe->size = msg_size;
1146   pe->is_init = GNUNET_NO;
1147   msg = (struct ZoneIterationStartMessage *) &pe[1];
1148   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1149   msg->gns_header.header.size = htons (msg_size);
1150   msg->gns_header.r_id = htonl (rid);
1151   msg->zone = *zone;
1152   msg->must_have_flags = ntohs (must_have_flags);
1153   msg->must_not_have_flags = ntohs (must_not_have_flags);
1154
1155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1156
1157   /* transmit message */
1158   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1159   do_transmit(h);
1160
1161   return it;
1162 }
1163
1164
1165 /**
1166  * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1167  * for the next record.
1168  *
1169  * @param it the iterator
1170  */
1171 void
1172 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1173 {
1174   struct GNUNET_NAMESTORE_Handle *h;
1175   struct PendingMessage *pe;
1176   size_t msg_size = 0;
1177
1178   GNUNET_assert (NULL != it);
1179   h = it->h;
1180
1181   /* set msg_size*/
1182   msg_size = sizeof (struct ZoneIterationNextMessage);
1183   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1184
1185   /* create msg here */
1186   struct ZoneIterationNextMessage * msg;
1187   pe->size = msg_size;
1188   pe->is_init = GNUNET_NO;
1189   msg = (struct ZoneIterationNextMessage *) &pe[1];
1190   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1191   msg->gns_header.header.size = htons (msg_size);
1192   msg->gns_header.r_id = htonl (it->op_id);
1193
1194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1195
1196   /* transmit message */
1197   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1198   do_transmit(h);
1199 }
1200
1201
1202 /**
1203  * Stops iteration and releases the namestore handle for further calls.
1204  *
1205  * @param it the iterator
1206  */
1207 void
1208 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1209 {
1210   GNUNET_assert (NULL != it);
1211   struct PendingMessage *pe;
1212   size_t msg_size = 0;
1213   struct GNUNET_NAMESTORE_Handle *h = it->h;
1214
1215   /* set msg_size*/
1216   msg_size = sizeof (struct ZoneIterationStopMessage);
1217   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1218
1219   /* create msg here */
1220   struct ZoneIterationStopMessage * msg;
1221   pe->size = msg_size;
1222   pe->is_init = GNUNET_NO;
1223   msg = (struct ZoneIterationStopMessage *) &pe[1];
1224   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1225   msg->gns_header.header.size = htons (msg_size);
1226   msg->gns_header.r_id = htonl (it->op_id);
1227
1228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1229
1230   /* transmit message */
1231   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1232   do_transmit(h);
1233 }
1234
1235
1236 /**
1237  * Cancel a namestore operation.  The final callback from the
1238  * operation must not have been done yet.
1239  *
1240  * @param qe operation to cancel
1241  */
1242 void
1243 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1244 {
1245   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1246
1247   GNUNET_assert (qe != NULL);
1248
1249   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1250   GNUNET_free(qe);
1251
1252 }
1253
1254 /* end of namestore_api.c */