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