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