- fix in api
[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_dnsparser_lib.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_namestore_service.h"
36 #include "namestore.h"
37
38 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
39
40 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41
42 /**
43  * A QueueEntry.
44  */
45 struct GNUNET_NAMESTORE_QueueEntry
46 {
47
48   /**
49    * Kept in a DLL.
50    */
51   struct GNUNET_NAMESTORE_QueueEntry *next;
52
53   /**
54    * Kept in a DLL.
55    */
56   struct GNUNET_NAMESTORE_QueueEntry *prev;
57
58   struct GNUNET_NAMESTORE_Handle *nsh;
59
60   uint32_t op_id;
61
62   GNUNET_NAMESTORE_ContinuationWithStatus cont;
63   void *cont_cls;
64
65   GNUNET_NAMESTORE_RecordProcessor proc;
66   void *proc_cls;
67
68   char *data; /*stub data pointer*/
69 };
70
71
72 /**
73  * Zone iterator
74  */
75 struct GNUNET_NAMESTORE_ZoneIterator
76 {
77
78   /**
79    * Kept in a DLL.
80    */
81   struct GNUNET_NAMESTORE_ZoneIterator *next;
82
83   /**
84    * Kept in a DLL.
85    */
86   struct GNUNET_NAMESTORE_ZoneIterator *prev;
87
88   uint32_t op_id;
89
90   struct GNUNET_NAMESTORE_Handle *h;
91   GNUNET_NAMESTORE_RecordProcessor proc;
92   void* proc_cls;
93   GNUNET_HashCode zone;
94   uint32_t no_flags;
95   uint32_t flags;
96   int has_zone;
97 };
98
99
100 /**
101  * Message in linked list we should send to the service.  The
102  * actual binary message follows this struct.
103  */
104 struct PendingMessage
105 {
106
107   /**
108    * Kept in a DLL.
109    */
110   struct PendingMessage *next;
111
112   /**
113    * Kept in a DLL.
114    */
115   struct PendingMessage *prev;
116
117   /**
118    * Size of the message.
119    */
120   size_t size;
121
122   /**
123    * Is this the 'START' message?
124    */
125   int is_init;
126 };
127
128
129 /**
130  * Connection to the NAMESTORE service.
131  */
132 struct GNUNET_NAMESTORE_Handle
133 {
134
135   /**
136    * Configuration to use.
137    */
138   const struct GNUNET_CONFIGURATION_Handle *cfg;
139
140   /**
141    * Socket (if available).
142    */
143   struct GNUNET_CLIENT_Connection *client;
144
145   /**
146    * Currently pending transmission request (or NULL).
147    */
148   struct GNUNET_CLIENT_TransmitHandle *th;
149
150   /**
151    * Reconnect task
152    */
153   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
154
155   /**
156    * Pending messages to send to the service
157    */
158
159   struct PendingMessage * pending_head;
160   struct PendingMessage * pending_tail;
161
162   /**
163    * Should we reconnect to service due to some serious error?
164    */
165   int reconnect;
166
167
168   /**
169    * Pending namestore queue entries
170    */
171   struct GNUNET_NAMESTORE_QueueEntry * op_head;
172   struct GNUNET_NAMESTORE_QueueEntry * op_tail;
173
174   uint32_t op_id;
175
176   /**
177    * Pending namestore zone iterator entries
178    */
179   struct GNUNET_NAMESTORE_ZoneIterator * z_head;
180   struct GNUNET_NAMESTORE_ZoneIterator * z_tail;
181 };
182
183 struct GNUNET_NAMESTORE_SimpleRecord
184 {
185   /**
186    * DLL
187    */
188   struct GNUNET_NAMESTORE_SimpleRecord *next;
189
190   /**
191    * DLL
192    */
193   struct GNUNET_NAMESTORE_SimpleRecord *prev;
194   
195   const char *name;
196   const GNUNET_HashCode *zone;
197   uint32_t record_type;
198   struct GNUNET_TIME_Absolute expiration;
199   enum GNUNET_NAMESTORE_RecordFlags flags;
200   size_t data_size;
201   const void *data;
202 };
203
204
205
206 /**
207  * Convert a type name (i.e. "AAAA") to the corresponding number.
208  *
209  * @param typename name to convert
210  * @return corresponding number, UINT32_MAX on error
211  */
212 uint32_t
213 GNUNET_NAMESTORE_typename_to_number (const char *typename)
214 {
215   static struct { 
216     const char *name; 
217     uint32_t number; 
218   } map[] = {
219     { "A", GNUNET_DNSPARSER_TYPE_A },
220     { "NS", GNUNET_DNSPARSER_TYPE_NS },
221     { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
222     { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
223     { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
224     { "MX", GNUNET_DNSPARSER_TYPE_MX },
225     { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
226     { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
227     { "PKEY",  GNUNET_NAMESTORE_TYPE_PKEY },
228     { "PSEU",  GNUNET_NAMESTORE_TYPE_PSEU },
229     { NULL, UINT32_MAX }
230   };
231   unsigned int i;
232
233   i=0;
234   while ( (map[i].name != NULL) &&
235           (0 != strcasecmp (typename, map[i].name)) )
236     i++;
237   return map[i].number;  
238 }
239
240
241 /**
242  * Disconnect from service and then reconnect.
243  *
244  * @param h our handle
245  */
246 static void
247 force_reconnect (struct GNUNET_NAMESTORE_Handle *h);
248
249 static void
250 handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
251                              struct LookupNameResponseMessage * msg,
252                              size_t size)
253 {
254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
255               "LOOKUP_NAME_RESPONSE");
256
257   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
258   char *name;
259   char * rd_tmp;
260
261   struct GNUNET_CRYPTO_RsaSignature *signature = NULL;
262   struct GNUNET_TIME_Absolute expire;
263   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key_tmp;
264   size_t exp_msg_len;
265   size_t msg_len = 0;
266   size_t name_len = 0;
267   size_t rd_len = 0;
268   int contains_sig = GNUNET_NO;
269   int rd_count = 0;
270
271   rd_len = ntohs (msg->rd_len);
272   rd_count = ntohs (msg->rd_count);
273   msg_len = ntohs (msg->gns_header.header.size);
274   name_len = ntohs (msg->name_len);
275   contains_sig = ntohs (msg->contains_sig);
276   expire = GNUNET_TIME_absolute_ntoh(msg->expire);
277
278   exp_msg_len = sizeof (struct LookupNameResponseMessage) +
279       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
280       name_len + rd_len;
281
282   if (msg_len != exp_msg_len)
283   {
284     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
285                 msg_len, exp_msg_len);
286     GNUNET_break_op (0);
287     return;
288   }
289
290   name = (char *) &msg[1];
291   rd_tmp = &name[name_len];
292
293   /* deserialize records */
294   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
295   GNUNET_NAMESTORE_records_deserialize(rd_len, rd_tmp, rd_count, rd);
296
297   /* reset values if values not contained */
298   if (contains_sig == GNUNET_NO)
299     signature = NULL;
300   else
301     signature = &msg->signature;
302   if (name_len == 0)
303     name = NULL;
304
305   if (name != NULL)
306       public_key_tmp =  &msg->public_key;
307   else
308       public_key_tmp = NULL;
309
310   if (qe->proc != NULL)
311   {
312     qe->proc (qe->proc_cls, public_key_tmp, expire, name, rd_count, (rd_count > 0) ? rd : NULL, signature);
313   }
314
315   /* Operation done, remove */
316   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
317   GNUNET_free (qe);
318 }
319
320
321 static void
322 handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
323                              struct RecordPutResponseMessage* msg,
324                              size_t size)
325 {
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
327               "RECORD_PUT_RESPONSE");
328   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
329   int res = ntohl (msg->op_result);
330
331   if (res == GNUNET_OK)
332   {
333     if (qe->cont != NULL)
334     {
335       qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
336     }
337
338   }
339   else if (res == GNUNET_SYSERR)
340   {
341     if (qe->cont != NULL)
342     {
343       qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
344     }
345   }
346   else
347   {
348     GNUNET_break_op (0);
349     return;
350   }
351
352   /* Operation done, remove */
353   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
354
355   GNUNET_free (qe);
356 }
357
358
359 static void
360 handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
361                              struct RecordCreateResponseMessage* msg,
362                              size_t size)
363 {
364   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
365               "RECORD_CREATE_RESPONSE");
366   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
367   int res = ntohl (msg->op_result);
368
369   if (res == GNUNET_YES)
370   {
371     if (qe->cont != NULL)
372     {
373       qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
374     }
375
376   }
377   else if (res == GNUNET_NO)
378   {
379     if (qe->cont != NULL)
380     {
381       qe->cont (qe->cont_cls, res, _("Namestore record already existed"));
382     }
383   }
384   else
385   {
386     if (qe->cont != NULL)
387     {
388       qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
389     }
390   }
391
392   /* Operation done, remove */
393   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
394
395   GNUNET_free (qe);
396 }
397
398
399 static void
400 handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
401                              struct RecordRemoveResponseMessage* msg,
402                              size_t size)
403 {
404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
405               "RECORD_REMOVE_RESPONSE");
406
407   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
408   int res = ntohl (msg->op_result);
409
410   /**
411    *  result:
412    *  0 : successful
413    *  1 : No records for entry
414    *  2 : Could not find record to remove
415    *  3 : Failed to create new signature
416    *  4 : Failed to put new set of records in database
417    */
418   switch (res) {
419     case 0:
420       if (qe->cont != NULL)
421       {
422         qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
423       }
424
425       break;
426     case 1:
427       if (qe->cont != NULL)
428       {
429         qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
430       }
431
432       break;
433     case 2:
434       if (qe->cont != NULL)
435       {
436         qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
437       }
438
439       break;
440     case 3:
441       if (qe->cont != NULL)
442       {
443         qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
444       }
445
446       break;
447     case 4:
448       if (qe->cont != NULL)
449       {
450         qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
451       }
452       break;
453     default:
454         GNUNET_break_op (0);
455       break;
456   }
457
458   /* Operation done, remove */
459   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
460
461   GNUNET_free (qe);
462 }
463
464 static void
465 handle_zone_to_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
466                              struct ZoneToNameResponseMessage* msg,
467                              size_t size)
468 {
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
470               "ZONE_TO_NAME_RESPONSE");
471
472   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
473   int res = ntohs (msg->res);
474
475   struct GNUNET_TIME_Absolute expire;
476   size_t name_len;
477   size_t rd_ser_len;
478   unsigned int rd_count;
479
480   char * name_tmp;
481   char * rd_tmp;
482
483   if (res == GNUNET_SYSERR)
484   {
485     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "An error occured during zone to name operation\n");
486     if (qe->proc != NULL)
487       qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
488   }
489   else if (res == GNUNET_NO)
490   {
491       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has no result for zone to name mapping \n");
492       if (qe->proc != NULL)
493         qe->proc (qe->proc_cls, NULL, GNUNET_TIME_absolute_get_zero(), NULL, 0, NULL, NULL);
494   }
495   else if (res == GNUNET_YES)
496   {
497     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore has result for zone to name mapping \n");
498
499     name_len = ntohs (msg->name_len);
500     rd_count = ntohs (msg->rd_count);
501     rd_ser_len = ntohs (msg->rd_len);
502     expire = GNUNET_TIME_absolute_ntoh(msg->expire);
503
504     name_tmp = (char *) &msg[1];
505     rd_tmp = &name_tmp[name_len];
506
507     struct GNUNET_NAMESTORE_RecordData rd[rd_count];
508     GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_tmp, rd_count, rd);
509
510     if (qe->proc != NULL)
511       qe->proc (qe->proc_cls, &msg->zone_key, expire, name_tmp, rd_count, rd, &msg->signature);
512   }
513   else
514     GNUNET_break_op (0);
515
516   /* Operation done, remove */
517   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
518   GNUNET_free (qe);
519 }
520
521
522 static void
523 manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
524                           const struct GNUNET_MessageHeader *msg,
525                           int type, size_t size)
526 {
527
528   /* handle different message type */
529   switch (type) {
530     case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE:
531         if (size < sizeof (struct LookupNameResponseMessage))
532         {
533           GNUNET_break_op (0);
534           break;
535         }
536         handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size);
537       break;
538     case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE:
539         if (size != sizeof (struct RecordPutResponseMessage))
540         {
541           GNUNET_break_op (0);
542           break;
543         }
544         handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size);
545       break;
546     case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE:
547         if (size != sizeof (struct RecordCreateResponseMessage))
548         {
549           GNUNET_break_op (0);
550           break;
551         }
552         handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
553       break;
554     case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
555         if (size != sizeof (struct RecordRemoveResponseMessage))
556         {
557           GNUNET_break_op (0);
558           break;
559         }
560         handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
561       break;
562     case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
563         if (size < sizeof (struct ZoneToNameResponseMessage))
564         {
565           GNUNET_break_op (0);
566           break;
567         }
568         handle_zone_to_name_response (qe, (struct ZoneToNameResponseMessage *) msg, size);
569       break;
570     default:
571       GNUNET_break_op (0);
572       break;
573   }
574 }
575
576 static void
577 handle_zone_iteration_response (struct GNUNET_NAMESTORE_ZoneIterator *ze,
578                                 struct ZoneIterationResponseMessage *msg,
579                                 size_t size)
580 {
581   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
582               "ZONE_ITERATION_RESPONSE");
583
584   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubdummy;
585   size_t msg_len = 0;
586   size_t exp_msg_len = 0;
587   size_t name_len = 0;
588   size_t rd_len = 0;
589   unsigned rd_count = 0;
590
591   char *name_tmp;
592   char *rd_ser_tmp;
593   struct GNUNET_TIME_Absolute expire;
594
595   msg_len = ntohs (msg->gns_header.header.size);
596   rd_len = ntohs (msg->rd_len);
597   rd_count = ntohs (msg->rd_count);
598   name_len = ntohs (msg->name_len);
599   expire = GNUNET_TIME_absolute_ntoh (msg->expire);
600
601   exp_msg_len = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_len;
602   if (msg_len != exp_msg_len)
603   {
604     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message size describes with `%u' bytes but calculated size is %u bytes \n",
605                 msg_len, exp_msg_len);
606     GNUNET_break_op (0);
607     return;
608   }
609   if (0 != ntohs (msg->reserved))
610   {
611     GNUNET_break_op (0);
612     return;
613   }
614
615   memset (&pubdummy, '\0', sizeof (pubdummy));
616   if ((0 == name_len) && (0 == (memcmp (&msg->public_key, &pubdummy, sizeof (pubdummy)))))
617   {
618     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration is completed!\n");
619
620     if (ze->proc != NULL)
621       ze->proc(ze->proc_cls, NULL, GNUNET_TIME_absolute_get_zero (), NULL , 0, NULL, NULL);
622
623     GNUNET_CONTAINER_DLL_remove(ze->h->z_head, ze->h->z_tail, ze);
624     GNUNET_free (ze);
625     return;
626   }
627
628   name_tmp = (char *) &msg[1];
629   if ((name_tmp[name_len -1] != '\0') || (name_len > 256))
630   {
631     GNUNET_break_op (0);
632     return;
633   }
634   rd_ser_tmp = (char *) &name_tmp[name_len];
635   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
636   if (GNUNET_OK != GNUNET_NAMESTORE_records_deserialize (rd_len, rd_ser_tmp, rd_count, rd))
637   {
638     GNUNET_break_op (0);
639     return;
640   }
641
642   if (ze->proc != NULL)
643     ze->proc(ze->proc_cls, &msg->public_key, expire, name_tmp, rd_count, rd, &msg->signature);
644 }
645
646
647 static void
648 manage_zone_operations (struct GNUNET_NAMESTORE_ZoneIterator *ze,
649                         const struct GNUNET_MessageHeader *msg,
650                         int type, size_t size)
651 {
652
653   /* handle different message type */
654   switch (type) {
655     case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE:
656         if (size < sizeof (struct ZoneIterationResponseMessage))
657         {
658           GNUNET_break_op (0);
659           break;
660         }
661         handle_zone_iteration_response (ze, (struct ZoneIterationResponseMessage *) msg, size);
662       break;
663     default:
664       GNUNET_break_op (0);
665       break;
666   }
667 }
668
669 /**
670  * Type of a function to call when we receive a message
671  * from the service.
672  *
673  * @param cls the 'struct GNUNET_NAMESTORE_SchedulingHandle'
674  * @param msg message received, NULL on timeout or fatal error
675  */
676 static void
677 process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg)
678 {
679   struct GNUNET_NAMESTORE_Handle *h = cls;
680   struct GNUNET_NAMESTORE_Header * gm;
681   struct GNUNET_NAMESTORE_QueueEntry *qe;
682   struct GNUNET_NAMESTORE_ZoneIterator *ze;
683   uint16_t size;
684   uint16_t type;
685   uint32_t r_id = UINT32_MAX;
686
687   if (NULL == msg)
688   {
689     force_reconnect (h);
690     return;
691   }
692
693   size = ntohs (msg->size);
694   type = ntohs (msg->type);
695
696   if (size < sizeof (struct GNUNET_NAMESTORE_Header))
697   {
698     GNUNET_break_op (0);
699     GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
700                            GNUNET_TIME_UNIT_FOREVER_REL);
701     return;
702   }
703
704   gm = (struct GNUNET_NAMESTORE_Header *) msg;
705   r_id = ntohl (gm->r_id);
706
707   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, r_id);
708
709   /* Find matching operation */
710   if (r_id > h->op_id)
711   {
712     /* No matching pending operation found */
713     GNUNET_break_op (0);
714     GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
715                            GNUNET_TIME_UNIT_FOREVER_REL);
716     return;
717   }
718
719   /* Is it a record related operation ? */
720   for (qe = h->op_head; qe != NULL; qe = qe->next)
721   {
722     if (qe->op_id == r_id)
723       break;
724   }
725   if (qe != NULL)
726   {
727     manage_record_operations (qe, msg, type, size);
728   }
729
730   /* Is it a zone iteration operation ? */
731   for (ze = h->z_head; ze != NULL; ze = ze->next)
732   {
733     if (ze->op_id == r_id)
734       break;
735   }
736   if (ze != NULL)
737   {
738     manage_zone_operations (ze, msg, type, size);
739   }
740
741   GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
742                          GNUNET_TIME_UNIT_FOREVER_REL);
743
744   if (GNUNET_YES == h->reconnect)
745     force_reconnect (h);
746
747 }
748
749
750 /**
751  * Transmit messages from the message queue to the service
752  * (if there are any, and if we are not already trying).
753  *
754  * @param h handle to use
755  */
756 static void
757 do_transmit (struct GNUNET_NAMESTORE_Handle *h);
758
759
760 /**
761  * We can now transmit a message to NAMESTORE. Do it.
762  *
763  * @param cls the 'struct GNUNET_NAMESTORE_Handle'
764  * @param size number of bytes we can transmit
765  * @param buf where to copy the messages
766  * @return number of bytes copied into buf
767  */
768 static size_t
769 transmit_message_to_namestore (void *cls, size_t size, void *buf)
770 {
771   struct GNUNET_NAMESTORE_Handle *h = cls;
772   struct PendingMessage *p;
773   size_t ret;
774   char *cbuf;
775
776   h->th = NULL;
777   if ((size == 0) || (buf == NULL))
778   {
779     force_reconnect (h);
780     return 0;
781   }
782   ret = 0;
783   cbuf = buf;
784   while ((NULL != (p = h->pending_head)) && (p->size <= size))
785   {
786     memcpy (&cbuf[ret], &p[1], p->size);
787     ret += p->size;
788     size -= p->size;
789     GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
790     if (GNUNET_YES == p->is_init)
791       GNUNET_CLIENT_receive (h->client, &process_namestore_message, h,
792                              GNUNET_TIME_UNIT_FOREVER_REL);
793     GNUNET_free (p);
794   }
795   do_transmit (h);
796   return ret;
797 }
798
799
800 /**
801  * Transmit messages from the message queue to the service
802  * (if there are any, and if we are not already trying).
803  *
804  * @param h handle to use
805  */
806 static void
807 do_transmit (struct GNUNET_NAMESTORE_Handle *h)
808 {
809   struct PendingMessage *p;
810
811   if (NULL != h->th)
812     return;
813   if (NULL == (p = h->pending_head))
814     return;
815   if (NULL == h->client)
816     return;                     /* currently reconnecting */
817
818   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, p->size,
819                                            GNUNET_TIME_UNIT_FOREVER_REL,
820                                            GNUNET_NO, &transmit_message_to_namestore,
821                                            h);
822 }
823
824
825 /**
826  * Reconnect to namestore service.
827  *
828  * @param h the handle to the namestore service
829  */
830 static void
831 reconnect (struct GNUNET_NAMESTORE_Handle *h)
832 {
833   struct PendingMessage *p;
834   struct StartMessage *init;
835
836   GNUNET_assert (NULL == h->client);
837   h->client = GNUNET_CLIENT_connect ("namestore", h->cfg);
838   GNUNET_assert (NULL != h->client);
839
840   if ((NULL == (p = h->pending_head)) || (GNUNET_YES != p->is_init))
841   {
842     p = GNUNET_malloc (sizeof (struct PendingMessage) +
843                        sizeof (struct StartMessage));
844     p->size = sizeof (struct StartMessage);
845     p->is_init = GNUNET_YES;
846     init = (struct StartMessage *) &p[1];
847     init->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_START);
848     init->header.size = htons (sizeof (struct StartMessage));
849     GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, p);
850   }
851   do_transmit (h);
852 }
853
854 /**
855  * Re-establish the connection to the service.
856  *
857  * @param cls handle to use to re-connect.
858  * @param tc scheduler context
859  */
860 static void
861 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
862 {
863   struct GNUNET_NAMESTORE_Handle *h = cls;
864
865   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
866   reconnect (h);
867 }
868
869
870 /**
871  * Disconnect from service and then reconnect.
872  *
873  * @param h our handle
874  */
875 static void
876 force_reconnect (struct GNUNET_NAMESTORE_Handle *h)
877 {
878   h->reconnect = GNUNET_NO;
879   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
880   h->client = NULL;
881   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
882                                     &reconnect_task,
883                                     h);
884 }
885
886 static uint32_t
887 get_op_id (struct GNUNET_NAMESTORE_Handle *h)
888 {
889   uint32_t op_id = h->op_id;
890   h->op_id ++;
891   return op_id;
892 }
893
894 /**
895  * Initialize the connection with the NAMESTORE service.
896  *
897  * @param cfg configuration to use
898  * @return handle to the GNS service, or NULL on error
899  */
900 struct GNUNET_NAMESTORE_Handle *
901 GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
902 {
903   struct GNUNET_NAMESTORE_Handle *h;
904
905   h = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Handle));
906   h->cfg = cfg;
907   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, h);
908   h->op_id = 0;
909   return h;
910 }
911
912 static void
913 clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
914 {
915   struct PendingMessage *p;
916   struct GNUNET_NAMESTORE_QueueEntry *q;
917   struct GNUNET_NAMESTORE_ZoneIterator *z;
918   struct GNUNET_NAMESTORE_Handle *h = cls;
919   GNUNET_assert (h != NULL);
920   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
921   while (NULL != (p = h->pending_head))
922   {
923     GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
924     GNUNET_free (p);
925   }
926
927   while (NULL != (q = h->op_head))
928   {
929     GNUNET_break (0);
930     GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
931     GNUNET_free (q);
932   }
933
934   while (NULL != (z = h->z_head))
935   {
936     GNUNET_CONTAINER_DLL_remove (h->z_head, h->z_tail, z);
937     GNUNET_free (z);
938   }
939
940   if (NULL != h->client)
941   {
942     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
943     h->client = NULL;
944   }
945   if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
946   {
947     GNUNET_SCHEDULER_cancel (h->reconnect_task);
948     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
949   }
950   GNUNET_free(h);
951   h = NULL;
952 };
953
954 /**
955  * Disconnect from the namestore service (and free associated
956  * resources).
957  *
958  * @param h handle to the namestore
959  * @param drop set to GNUNET_YES to delete all data in namestore (!)
960  */
961 void
962 GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
963 {
964   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from namestore service\n");
965   GNUNET_SCHEDULER_add_now (&clean_up_task, h);
966 }
967
968
969 /**
970  * Store an item in the namestore.  If the item is already present,
971  * the expiration time is updated to the max of the existing time and
972  * the new time.  This API is used when we cache signatures from other
973  * authorities.
974  *
975  * @param h handle to the namestore
976  * @param zone_key public key of the zone
977  * @param name name that is being mapped (at most 255 characters long)
978  * @param expire when does the corresponding block in the DHT expire (until
979  *               when should we never do a DHT lookup for the same name again)?
980  * @param rd_count number of entries in 'rd' array
981  * @param rd array of records with data to store
982  * @param signature signature for all the records in the zone under the given name
983  * @param cont continuation to call when done
984  * @param cont_cls closure for cont
985  * @return handle to abort the request
986  */
987 struct GNUNET_NAMESTORE_QueueEntry *
988 GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
989                              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
990                              const char *name,
991                              struct GNUNET_TIME_Absolute expire,
992                              unsigned int rd_count,
993                              const struct GNUNET_NAMESTORE_RecordData *rd,
994                              const struct GNUNET_CRYPTO_RsaSignature *signature,
995                              GNUNET_NAMESTORE_ContinuationWithStatus cont,
996                              void *cont_cls)
997 {
998   struct GNUNET_NAMESTORE_QueueEntry *qe;
999   struct PendingMessage *pe;
1000
1001   /* pointer to elements */
1002   char * rd_tmp;
1003   char * name_tmp;
1004
1005   size_t msg_size = 0;
1006   size_t name_len = 0;
1007   size_t rd_ser_len = 0;
1008   uint32_t rid = 0;
1009
1010   GNUNET_assert (NULL != h);
1011   GNUNET_assert (NULL != zone_key);
1012   GNUNET_assert (NULL != name);
1013   GNUNET_assert (NULL != rd);
1014   GNUNET_assert (NULL != signature);
1015
1016   name_len = strlen(name) + 1;
1017   if (name_len > 256)
1018   {
1019     GNUNET_break (0);
1020     return NULL;
1021   }
1022
1023   rid = get_op_id(h);
1024   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1025   qe->nsh = h;
1026   qe->cont = cont;
1027   qe->cont_cls = cont_cls;
1028   qe->op_id = rid;
1029   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1030
1031   /* set msg_size*/
1032   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1033   char rd_ser[rd_ser_len];
1034   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1035
1036   struct RecordPutMessage * msg;
1037   msg_size = sizeof (struct RecordPutMessage) + name_len  + rd_ser_len;
1038
1039   /* create msg here */
1040   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1041   pe->size = msg_size;
1042   pe->is_init = GNUNET_NO;
1043   msg = (struct RecordPutMessage *) &pe[1];
1044   name_tmp = (char *) &msg[1];
1045   rd_tmp = &name_tmp[name_len];
1046
1047   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT);
1048   msg->gns_header.header.size = htons (msg_size);
1049   msg->gns_header.r_id = htonl (rid);
1050   msg->signature = *signature;
1051   msg->name_len = htons (name_len);
1052   msg->expire = GNUNET_TIME_absolute_hton (expire);
1053   msg->rd_len = htons (rd_ser_len);
1054   msg->rd_count = htons (rd_count);
1055
1056   msg->public_key = *zone_key;
1057   memcpy (name_tmp, name, name_len);
1058   memcpy (rd_tmp, rd_ser, rd_ser_len);
1059
1060   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
1061
1062   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1063   do_transmit(h);
1064
1065   return qe;
1066 }
1067
1068
1069 /**
1070  * Check if a signature is valid.  This API is used by the GNS Block
1071  * to validate signatures received from the network.
1072  *
1073  * @param public_key public key of the zone
1074  * @param name name that is being mapped (at most 255 characters long)
1075  * @param rd_count number of entries in 'rd' array
1076  * @param rd array of records with data to store
1077  * @param signature signature for all the records in the zone under the given name
1078  * @return GNUNET_OK if the signature is valid
1079  */
1080 int
1081 GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
1082                                    const char *name,
1083                                    unsigned int rd_count,
1084                                    const struct GNUNET_NAMESTORE_RecordData *rd,
1085                                    const struct GNUNET_CRYPTO_RsaSignature *signature)
1086 {
1087   int res = GNUNET_SYSERR;
1088   size_t rd_ser_len = 0;
1089   size_t name_len = 0;
1090   char * name_tmp;
1091   char * rd_tmp;
1092   struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
1093
1094   GNUNET_assert (public_key != NULL);
1095   GNUNET_assert (name != NULL);
1096   GNUNET_assert (rd != NULL);
1097   GNUNET_assert (signature != NULL);
1098
1099
1100   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
1101   char rd_ser[rd_ser_len];
1102   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
1103
1104   name_len = strlen (name) + 1;
1105   if (name_len > 256)
1106   {
1107     GNUNET_break (0);
1108     return GNUNET_SYSERR;
1109   }
1110
1111   sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
1112   sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
1113   sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
1114   name_tmp = (char *) &sig_purpose[1];
1115   rd_tmp = &name_tmp[name_len];
1116   memcpy (name_tmp, name, name_len);
1117   memcpy (rd_tmp, rd_ser, rd_ser_len);
1118
1119   res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
1120
1121   GNUNET_free (sig_purpose);
1122
1123   return res;
1124 }
1125
1126 /**
1127  * Store an item in the namestore.  If the item is already present,
1128  * the expiration time is updated to the max of the existing time and
1129  * the new time.  This API is used by the authority of a zone.
1130  *
1131  * @param h handle to the namestore
1132  * @param pkey private key of the zone
1133  * @param name name that is being mapped (at most 255 characters long)
1134  * @param rd record data to store
1135  * @param cont continuation to call when done
1136  * @param cont_cls closure for cont
1137  * @return handle to abort the request
1138  */
1139 struct GNUNET_NAMESTORE_QueueEntry *
1140 GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1141                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1142                                 const char *name,
1143                                 const struct GNUNET_NAMESTORE_RecordData *rd,
1144                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1145                                 void *cont_cls)
1146 {
1147   struct GNUNET_NAMESTORE_QueueEntry *qe;
1148   struct PendingMessage *pe;
1149   char * name_tmp;
1150   char * pkey_tmp;
1151   char * rd_tmp;
1152   size_t rd_ser_len = 0;
1153   size_t msg_size = 0;
1154   size_t name_len = 0;
1155   size_t key_len = 0;
1156   uint32_t rid = 0;
1157
1158   GNUNET_assert (NULL != h);
1159   GNUNET_assert (NULL != pkey);
1160   GNUNET_assert (NULL != name);
1161   GNUNET_assert (NULL != rd);
1162
1163   name_len = strlen(name) + 1;
1164   if (name_len > 256)
1165   {
1166     GNUNET_break (0);
1167     return NULL;
1168   }
1169
1170   rid = get_op_id(h);
1171   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1172   qe->nsh = h;
1173   qe->cont = cont;
1174   qe->cont_cls = cont_cls;
1175   qe->op_id = rid;
1176   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1177
1178   /* set msg_size*/
1179   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1180   GNUNET_assert (pkey_enc != NULL);
1181   key_len = ntohs (pkey_enc->len);
1182
1183   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1184   char rd_ser[rd_ser_len];
1185   GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1186
1187   struct RecordCreateMessage * msg;
1188   msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1189
1190   /* create msg here */
1191   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1192   pe->size = msg_size;
1193   pe->is_init = GNUNET_NO;
1194   msg = (struct RecordCreateMessage *) &pe[1];
1195
1196   pkey_tmp = (char *) &msg[1];
1197   name_tmp = &pkey_tmp[key_len];
1198   rd_tmp = &name_tmp[name_len];
1199
1200   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE);
1201   msg->gns_header.header.size = htons (msg_size);
1202   msg->gns_header.r_id = htonl (rid);
1203   msg->name_len = htons (name_len);
1204   msg->rd_count = htons (1);
1205   msg->rd_len = htons (rd_ser_len);
1206   msg->pkey_len = htons (key_len);
1207   memcpy (pkey_tmp, pkey_enc, key_len);
1208   memcpy (name_tmp, name, name_len);
1209   memcpy (rd_tmp, rd_ser, rd_ser_len);
1210   GNUNET_free (pkey_enc);
1211
1212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
1213
1214   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1215   do_transmit(h);
1216   return qe;
1217 }
1218
1219
1220 /**
1221  * Explicitly remove some content from the database.  The
1222  * "cont"inuation will be called with status "GNUNET_OK" if content
1223  * was removed, "GNUNET_NO" if no matching entry was found and
1224  * "GNUNET_SYSERR" on all other types of errors.
1225  * This API is used by the authority of a zone.
1226  *
1227  * @param h handle to the namestore
1228  * @param pkey private key of the zone
1229  * @param name name that is being mapped (at most 255 characters long)
1230  * @param rd record data
1231  * @param cont continuation to call when done
1232  * @param cont_cls closure for cont
1233  * @return handle to abort the request
1234  */
1235 struct GNUNET_NAMESTORE_QueueEntry *
1236 GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1237                                 const struct GNUNET_CRYPTO_RsaPrivateKey *pkey,
1238                                 const char *name,
1239                                 const struct GNUNET_NAMESTORE_RecordData *rd,
1240                                 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1241                                 void *cont_cls)
1242 {
1243   struct GNUNET_NAMESTORE_QueueEntry *qe;
1244   struct PendingMessage *pe;
1245   char *pkey_tmp;
1246   char *rd_tmp;
1247   char *name_tmp;
1248   size_t rd_ser_len = 0;
1249   size_t msg_size = 0;
1250   size_t name_len = 0;
1251   size_t key_len = 0;
1252   uint32_t rid = 0;
1253
1254   GNUNET_assert (NULL != h);
1255
1256   rid = get_op_id(h);
1257   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1258   qe->nsh = h;
1259   qe->cont = cont;
1260   qe->cont_cls = cont_cls;
1261   qe->op_id = rid;
1262   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1263
1264   /* set msg_size*/
1265   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
1266   GNUNET_assert (pkey_enc != NULL);
1267   key_len = ntohs (pkey_enc->len);
1268
1269   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
1270   char rd_ser[rd_ser_len];
1271   GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1272
1273   name_len = strlen (name) + 1;
1274
1275   struct RecordRemoveMessage * msg;
1276   msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1277
1278   /* create msg here */
1279   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1280   pe->size = msg_size;
1281   pe->is_init = GNUNET_NO;
1282   msg = (struct RecordRemoveMessage *) &pe[1];
1283
1284   pkey_tmp = (char *) &msg[1];
1285   name_tmp = &pkey_tmp[key_len];
1286   rd_tmp = &name_tmp[name_len];
1287
1288   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1289   msg->gns_header.header.size = htons (msg_size);
1290   msg->gns_header.r_id = htonl (rid);
1291   msg->name_len = htons (name_len);
1292   msg->rd_len = htons (rd_ser_len);
1293   msg->rd_count = htons (1);
1294   msg->pkey_len = htons (key_len);
1295   memcpy (pkey_tmp, pkey_enc, key_len);
1296   memcpy (name_tmp, name, name_len);
1297   memcpy (rd_tmp, rd_ser, rd_ser_len);
1298
1299   GNUNET_free (pkey_enc);
1300
1301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);
1302
1303   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1304   do_transmit(h);
1305   return qe;
1306 }
1307
1308
1309 /**
1310  * Get a result for a particular key from the namestore.  The processor
1311  * will only be called once.  
1312  *
1313  * @param h handle to the namestore
1314  * @param zone zone to look up a record from
1315  * @param name name to look up
1316  * @param record_type desired record type, 0 for all
1317  * @param proc function to call on the matching records, or with
1318  *        NULL (rd_count == 0) if there are no matching records
1319  * @param proc_cls closure for proc
1320  * @return a handle that can be used to
1321  *         cancel
1322  */
1323 struct GNUNET_NAMESTORE_QueueEntry *
1324 GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, 
1325                               const GNUNET_HashCode *zone,
1326                               const char *name,
1327                               uint32_t record_type,
1328                               GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1329 {
1330   struct GNUNET_NAMESTORE_QueueEntry *qe;
1331   struct PendingMessage *pe;
1332   size_t msg_size = 0;
1333   size_t name_len = 0;
1334   uint32_t rid = 0;
1335
1336   GNUNET_assert (NULL != h);
1337   GNUNET_assert (NULL != zone);
1338   GNUNET_assert (NULL != name);
1339
1340   name_len = strlen (name) + 1;
1341   if ((name_len == 0) || (name_len > 256))
1342   {
1343     GNUNET_break (0);
1344     return NULL;
1345   }
1346
1347   rid = get_op_id(h);
1348   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1349   qe->nsh = h;
1350   qe->proc = proc;
1351   qe->proc_cls = proc_cls;
1352   qe->op_id = rid;
1353   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1354
1355   /* set msg_size*/
1356   msg_size = sizeof (struct LookupNameMessage) + name_len;
1357   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1358
1359   /* create msg here */
1360   struct LookupNameMessage * msg;
1361   pe->size = msg_size;
1362   pe->is_init = GNUNET_NO;
1363   msg = (struct LookupNameMessage *) &pe[1];
1364   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
1365   msg->gns_header.header.size = htons (msg_size);
1366   msg->gns_header.r_id = htonl (rid);
1367   msg->record_type = htonl (record_type);
1368   msg->name_len = htonl (name_len);
1369   msg->zone = *zone;
1370   memcpy (&msg[1], name, name_len);
1371
1372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "NAMESTORE_LOOKUP_NAME", name);
1373
1374   /* transmit message */
1375   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1376   do_transmit(h);
1377
1378   return qe;
1379 }
1380
1381
1382 /**
1383  * Look for an existing PKEY delegation record for a given public key.
1384  * Returns at most one result to the processor.
1385  *
1386  * @param h handle to the namestore
1387  * @param zone hash of public key of the zone to look up in, never NULL
1388  * @param value_zone hash of the public key of the target zone (value), never NULL
1389  * @param proc function to call on the matching records, or with
1390  *        NULL (rd_count == 0) if there are no matching records
1391  * @param proc_cls closure for proc
1392  * @return a handle that can be used to
1393  *         cancel
1394  */
1395 struct GNUNET_NAMESTORE_QueueEntry *
1396 GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h,
1397                                const GNUNET_HashCode *zone,
1398                                const GNUNET_HashCode *value_zone,
1399                                GNUNET_NAMESTORE_RecordProcessor proc, void *proc_cls)
1400 {
1401   struct GNUNET_NAMESTORE_QueueEntry *qe;
1402   struct PendingMessage *pe;
1403   size_t msg_size = 0;
1404   uint32_t rid = 0;
1405
1406   GNUNET_assert (NULL != h);
1407   GNUNET_assert (NULL != zone);
1408   GNUNET_assert (NULL != value_zone);
1409
1410   rid = get_op_id(h);
1411   qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1412   qe->nsh = h;
1413   qe->proc = proc;
1414   qe->proc_cls = proc_cls;
1415   qe->op_id = rid;
1416   GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
1417
1418   /* set msg_size*/
1419   msg_size = sizeof (struct ZoneToNameMessage);
1420   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1421
1422   /* create msg here */
1423   struct ZoneToNameMessage * msg;
1424   pe->size = msg_size;
1425   pe->is_init = GNUNET_NO;
1426   msg = (struct ZoneToNameMessage *) &pe[1];
1427   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME);
1428   msg->gns_header.header.size = htons (msg_size);
1429   msg->gns_header.r_id = htonl (rid);
1430   msg->zone = *zone;
1431   msg->value_zone = *value_zone;
1432
1433   char * z_tmp = strdup (GNUNET_h2s (zone));
1434   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s' in zone `%s'\n",
1435       "NAMESTORE_ZONE_TO_NAME",
1436       z_tmp,
1437       GNUNET_h2s (value_zone));
1438   GNUNET_free (z_tmp);
1439
1440   /* transmit message */
1441   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1442   do_transmit(h);
1443
1444   return qe;
1445 }
1446
1447
1448
1449 /**
1450  * Starts a new zone iteration (used to periodically PUT all of our
1451  * records into our DHT). This MUST lock the GNUNET_NAMESTORE_Handle
1452  * for any other calls than GNUNET_NAMESTORE_zone_iterator_next and
1453  * GNUNET_NAMESTORE_zone_iteration_stop.  "proc" will be called once
1454  * immediately, and then again after
1455  * "GNUNET_NAMESTORE_zone_iterator_next" is invoked.
1456  *
1457  * @param h handle to the namestore
1458  * @param zone zone to access, NULL for all zones
1459  * @param must_have_flags flags that must be set for the record to be returned
1460  * @param must_not_have_flags flags that must NOT be set for the record to be returned
1461  * @param proc function to call on each name from the zone; it
1462  *        will be called repeatedly with a value (if available)
1463  *        and always once at the end with a name of NULL.
1464  * @param proc_cls closure for proc
1465  * @return an iterator handle to use for iteration
1466  */
1467 struct GNUNET_NAMESTORE_ZoneIterator *
1468 GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1469                                        const GNUNET_HashCode *zone,
1470                                        enum GNUNET_NAMESTORE_RecordFlags must_have_flags,
1471                                        enum GNUNET_NAMESTORE_RecordFlags must_not_have_flags,
1472                                        GNUNET_NAMESTORE_RecordProcessor proc,
1473                                        void *proc_cls)
1474 {
1475   struct GNUNET_NAMESTORE_ZoneIterator *it;
1476   struct PendingMessage *pe;
1477   size_t msg_size = 0;
1478   uint32_t rid = 0;
1479
1480   GNUNET_assert (NULL != h);
1481
1482
1483   rid = get_op_id(h);
1484   it = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIterator));
1485   it->h = h;
1486   it->proc = proc;
1487   it->proc_cls = proc;
1488   it->op_id = rid;
1489
1490   if (NULL != zone)
1491   {
1492     it->zone = *zone;
1493     it->has_zone = GNUNET_YES;
1494   }
1495   else
1496   {
1497     memset (&it->zone, '\0', sizeof (it->zone));
1498     it->has_zone = GNUNET_NO;
1499   }
1500   GNUNET_CONTAINER_DLL_insert_tail(h->z_head, h->z_tail, it);
1501
1502   /* set msg_size*/
1503   msg_size = sizeof (struct ZoneIterationStartMessage);
1504   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1505
1506   /* create msg here */
1507   struct ZoneIterationStartMessage * msg;
1508   pe->size = msg_size;
1509   pe->is_init = GNUNET_NO;
1510   msg = (struct ZoneIterationStartMessage *) &pe[1];
1511   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START);
1512   msg->gns_header.header.size = htons (msg_size);
1513   msg->gns_header.r_id = htonl (rid);
1514   if (NULL != zone)
1515     msg->zone = *zone;
1516   else
1517     memset (&msg->zone, '\0', sizeof (msg->zone));
1518   msg->must_have_flags = ntohs (must_have_flags);
1519   msg->must_not_have_flags = ntohs (must_not_have_flags);
1520
1521   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_START", GNUNET_h2s(zone));
1522
1523   /* transmit message */
1524   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1525   do_transmit(h);
1526
1527   return it;
1528 }
1529
1530
1531 /**
1532  * Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start
1533  * for the next record.
1534  *
1535  * @param it the iterator
1536  */
1537 void
1538 GNUNET_NAMESTORE_zone_iterator_next (struct GNUNET_NAMESTORE_ZoneIterator *it)
1539 {
1540   struct GNUNET_NAMESTORE_Handle *h;
1541   struct PendingMessage *pe;
1542   size_t msg_size = 0;
1543
1544   GNUNET_assert (NULL != it);
1545   h = it->h;
1546
1547   /* set msg_size*/
1548   msg_size = sizeof (struct ZoneIterationNextMessage);
1549   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1550
1551   /* create msg here */
1552   struct ZoneIterationNextMessage * msg;
1553   pe->size = msg_size;
1554   pe->is_init = GNUNET_NO;
1555   msg = (struct ZoneIterationNextMessage *) &pe[1];
1556   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT);
1557   msg->gns_header.header.size = htons (msg_size);
1558   msg->gns_header.r_id = htonl (it->op_id);
1559
1560   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_NEXT", GNUNET_h2s(&it->zone));
1561
1562   /* transmit message */
1563   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1564   do_transmit(h);
1565 }
1566
1567
1568 /**
1569  * Stops iteration and releases the namestore handle for further calls.
1570  *
1571  * @param it the iterator
1572  */
1573 void
1574 GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1575 {
1576   GNUNET_assert (NULL != it);
1577   struct PendingMessage *pe;
1578   size_t msg_size = 0;
1579   struct GNUNET_NAMESTORE_Handle *h = it->h;
1580
1581   /* set msg_size*/
1582   msg_size = sizeof (struct ZoneIterationStopMessage);
1583   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
1584
1585   /* create msg here */
1586   struct ZoneIterationStopMessage * msg;
1587   pe->size = msg_size;
1588   pe->is_init = GNUNET_NO;
1589   msg = (struct ZoneIterationStopMessage *) &pe[1];
1590   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP);
1591   msg->gns_header.header.size = htons (msg_size);
1592   msg->gns_header.r_id = htonl (it->op_id);
1593
1594   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s'\n", "ZONE_ITERATION_STOP", GNUNET_h2s(&it->zone));
1595
1596   /* transmit message */
1597   GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1598   do_transmit(h);
1599 }
1600
1601
1602 /**
1603  * Cancel a namestore operation.  The final callback from the
1604  * operation must not have been done yet.
1605  *
1606  * @param qe operation to cancel
1607  */
1608 void
1609 GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1610 {
1611   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
1612
1613   GNUNET_assert (qe != NULL);
1614
1615   GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
1616   GNUNET_free(qe);
1617
1618 }
1619
1620 /* end of namestore_api.c */