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