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