- adding sig purpose and test for lookup up
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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/gnunet-service-namestore.c
23  * @brief namestore for the GNUnet naming system
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_namestore_plugin.h"
31 #include "namestore.h"
32
33
34
35 /**
36  * A namestore operation.
37  */
38 struct GNUNET_NAMESTORE_ZoneIteration
39 {
40   struct GNUNET_NAMESTORE_ZoneIteration *next;
41   struct GNUNET_NAMESTORE_ZoneIteration *prev;
42
43   struct GNUNET_NAMESTORE_Client * client;
44
45   GNUNET_HashCode zone;
46
47   uint64_t op_id;
48   uint32_t offset;
49
50 };
51
52
53 /**
54  * A namestore client
55  */
56 struct GNUNET_NAMESTORE_Client
57 {
58   struct GNUNET_NAMESTORE_Client *next;
59   struct GNUNET_NAMESTORE_Client *prev;
60
61   struct GNUNET_SERVER_Client * client;
62
63   struct GNUNET_NAMESTORE_ZoneIteration *op_head;
64   struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
65 };
66
67
68
69 /**
70  * Configuration handle.
71  */
72 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
73
74 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
75
76 /**
77  * Our notification context.
78  */
79 static struct GNUNET_SERVER_NotificationContext *snc;
80
81 static char *db_lib_name;
82
83 static struct GNUNET_NAMESTORE_Client *client_head;
84 static struct GNUNET_NAMESTORE_Client *client_tail;
85
86
87 /**
88  * Task run during shutdown.
89  *
90  * @param cls unused
91  * @param tc unused
92  */
93 static void
94 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
97
98   struct GNUNET_NAMESTORE_ZoneIteration * no;
99   struct GNUNET_NAMESTORE_ZoneIteration * tmp;
100   struct GNUNET_NAMESTORE_Client * nc;
101   struct GNUNET_NAMESTORE_Client * next;
102
103   for (nc = client_head; nc != NULL; nc = next)
104   {
105     next = nc->next;
106     for (no = nc->op_head; no != NULL; no = tmp)
107     {
108       GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
109       tmp = no->next;
110       GNUNET_free (no);
111     }
112
113     GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
114     GNUNET_free (nc);
115
116   }
117
118   GNUNET_SERVER_notification_context_destroy (snc);
119   snc = NULL;
120
121   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
122   GNUNET_free (db_lib_name);
123 }
124
125 static struct GNUNET_NAMESTORE_Client *
126 client_lookup (struct GNUNET_SERVER_Client *client)
127 {
128   struct GNUNET_NAMESTORE_Client * nc;
129
130   GNUNET_assert (NULL != client);
131
132   for (nc = client_head; nc != NULL; nc = nc->next)
133   {
134     if (client == nc->client)
135       break;
136   }
137   return nc;
138 }
139
140
141 /**
142  * Called whenever a client is disconnected.  Frees our
143  * resources associated with that client.
144  *
145  * @param cls closure
146  * @param client identification of the client
147  */
148 static void
149 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
150 {
151   struct GNUNET_NAMESTORE_ZoneIteration * no;
152   struct GNUNET_NAMESTORE_Client * nc;
153   if (NULL == client)
154     return;
155
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
157
158   nc = client_lookup (client);
159
160   if ((NULL == client) || (NULL == nc))
161     return;
162
163   for (no = nc->op_head; no != NULL; no = no->next)
164   {
165     GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
166     GNUNET_free (no);
167   }
168
169   GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
170   GNUNET_free (nc);
171 }
172
173 static void handle_start (void *cls,
174                           struct GNUNET_SERVER_Client * client,
175                           const struct GNUNET_MessageHeader * message)
176 {
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
178
179   struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
180   nc->client = client;
181   GNUNET_SERVER_notification_context_add (snc, client);
182   GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
183
184   GNUNET_SERVER_receive_done (client, GNUNET_OK);
185 }
186
187 struct LookupNameContext
188 {
189   struct GNUNET_NAMESTORE_Client *nc;
190   uint32_t id;
191   uint32_t record_type;
192 };
193
194
195
196
197 static void
198 handle_lookup_name_it (void *cls,
199     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
200     struct GNUNET_TIME_Absolute expire,
201     const char *name,
202     unsigned int rd_count,
203     const struct GNUNET_NAMESTORE_RecordData *rd,
204     const struct GNUNET_CRYPTO_RsaSignature *signature)
205 {
206   /* send response */
207   struct LookupNameContext *lnc = cls;
208   struct LookupNameResponseMessage *lnr_msg;
209
210   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
211   struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
212   char *rd_tmp;
213   char *name_tmp;
214   char *rd_ser;
215   size_t rd_ser_len;
216   struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
219
220   size_t r_size = 0;
221
222   size_t name_len = 0;
223   if (NULL != name)
224     name_len = strlen(name) + 1;
225
226   int copied_elements = 0;
227   int contains_signature = 0;
228   int c;
229
230   /* count records to copy */
231   if (rd_count != 0)
232   {
233     if (lnc->record_type != 0)
234     {
235       /* special record type needed */
236       for (c = 0; c < rd_count; c ++)
237         if (rd[c].record_type == lnc->record_type)
238           copied_elements++; /* found matching record */
239       rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
240       copied_elements = 0;
241       for (c = 0; c < rd_count; c ++)
242       {
243         if (rd[c].record_type == lnc->record_type)
244         {
245           /* found matching record */
246           memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
247           copied_elements++;
248         }
249       }
250     }
251     else
252     {
253       copied_elements = rd_count;
254       rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
255     }
256     rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, copied_elements, rd_selected);
257   }
258   else
259   {
260     /* No results */
261     copied_elements = 0;
262     rd_selected = NULL;
263     rd_ser = NULL;
264     rd_ser_len = 0;
265   }
266
267   if ((copied_elements == rd_count) && (signature != NULL))
268       contains_signature = GNUNET_YES;
269
270   if (rd_selected != rd)
271     GNUNET_free (rd_selected);
272
273   r_size = sizeof (struct LookupNameResponseMessage) +
274            sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
275            name_len +
276            rd_ser_len +
277            contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
278
279   lnr_msg = GNUNET_malloc (r_size);
280
281   lnr_msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
282   lnr_msg->header.size = ntohs (r_size);
283   lnr_msg->op_id = htonl (lnc->id);
284   lnr_msg->rd_len = htonl (rd_ser_len);
285   lnr_msg->name_len = htons (name_len);
286   lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
287   lnr_msg->contains_sig = htons (contains_signature);
288
289   zone_key_tmp =  (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
290   name_tmp = (char *) &zone_key_tmp[1];
291   rd_tmp = &name_tmp[name_len];
292   signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_ser_len];
293
294   if (zone_key != NULL)
295     memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
296   else
297   {
298     struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
299     memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
300     memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
301   }
302   memcpy (name_tmp, name, name_len);
303   memcpy (rd_tmp, rd_ser, rd_ser_len);
304   GNUNET_free_non_null (rd_ser);
305
306   if (GNUNET_YES == contains_signature)
307     memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
308   GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
309
310   GNUNET_free (lnr_msg);
311 }
312
313 static void handle_lookup_name (void *cls,
314                           struct GNUNET_SERVER_Client * client,
315                           const struct GNUNET_MessageHeader * message)
316 {
317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
318   struct LookupNameContext lnc;
319   struct GNUNET_NAMESTORE_Client *nc;
320   size_t name_len;
321   char * name;
322   uint32_t id = 0;
323   uint32_t type = 0;
324   int res;
325
326   if (ntohs (message->size) < sizeof (struct LookupNameMessage))
327   {
328     GNUNET_break_op (0);
329     GNUNET_SERVER_receive_done (client, GNUNET_OK);
330     return;
331   }
332
333   nc = client_lookup(client);
334   if (nc == NULL)
335   {
336     GNUNET_break_op (0);
337     GNUNET_SERVER_receive_done (client, GNUNET_OK);
338     return;
339   }
340
341   struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
342   id = ntohl (ln_msg->op_id);
343   name_len = ntohl (ln_msg->name_len);
344   type = ntohl (ln_msg->record_type);
345
346   if ((name_len == 0) || (name_len > 256))
347   {
348     GNUNET_break_op (0);
349     GNUNET_SERVER_receive_done (client, GNUNET_OK);
350     return;
351   }
352
353   name = (char *) &ln_msg[1];
354   if (name[name_len -1] != '\0')
355   {
356     GNUNET_break_op (0);
357     GNUNET_SERVER_receive_done (client, GNUNET_OK);
358     return;
359   }
360
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s'\n", name);
362
363   /* do the actual lookup */
364   lnc.id = id;
365   lnc.nc = nc;
366   lnc.record_type = type;
367   res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
368
369   GNUNET_SERVER_receive_done (client, GNUNET_OK);
370 }
371
372 static void handle_record_put (void *cls,
373                           struct GNUNET_SERVER_Client * client,
374                           const struct GNUNET_MessageHeader * message)
375 {
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
377   struct GNUNET_NAMESTORE_Client *nc;
378   struct GNUNET_TIME_Absolute expire;
379   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
380   struct GNUNET_NAMESTORE_RecordData *rd;
381   struct GNUNET_CRYPTO_RsaSignature *signature;
382   struct RecordPutResponseMessage rpr_msg;
383   size_t name_len;
384   size_t msg_size;
385   size_t msg_size_exp;
386   char * name;
387   char * rd_ser;
388   uint32_t id = 0;
389   uint32_t rd_ser_len;
390   uint32_t rd_count;
391   int res = GNUNET_SYSERR;
392
393   if (ntohs (message->size) < sizeof (struct RecordPutMessage))
394   {
395     GNUNET_break_op (0);
396     GNUNET_SERVER_receive_done (client, GNUNET_OK);
397     return;
398   }
399
400   nc = client_lookup (client);
401   if (nc == NULL)
402   {
403     GNUNET_break_op (0);
404     GNUNET_SERVER_receive_done (client, GNUNET_OK);
405     return;
406   }
407
408   struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
409   id = ntohl (rp_msg->op_id);
410   name_len = ntohs (rp_msg->name_len);
411   rd_ser_len = ntohs(rp_msg->rd_len);
412   msg_size = ntohs (message->size);
413   msg_size_exp = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len  + rd_ser_len;
414
415   if (msg_size != msg_size_exp)
416   {
417     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
418     GNUNET_break_op (0);
419     GNUNET_SERVER_receive_done (client, GNUNET_OK);
420     return;
421   }
422
423
424   if ((name_len == 0) || (name_len > 256))
425   {
426     GNUNET_break_op (0);
427     GNUNET_SERVER_receive_done (client, GNUNET_OK);
428     return;
429   }
430
431   zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
432   name = (char *) &zone_key[1];
433
434   if (name[name_len -1] != '\0')
435   {
436     GNUNET_break_op (0);
437     GNUNET_SERVER_receive_done (client, GNUNET_OK);
438     return;
439   }
440
441   expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
442   signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
443   rd_ser = &name[name_len];
444   rd_count = GNUNET_NAMESTORE_records_deserialize(&rd, rd_ser, rd_ser_len);
445
446   /* Database operation */
447   res = GSN_database->put_records(GSN_database->cls,
448                                 zone_key,
449                                 expire,
450                                 name,
451                                 rd_count, rd,
452                                 signature);
453
454   GNUNET_NAMESTORE_records_free (rd_count, rd);
455
456   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
457       name, (res == GNUNET_OK) ? "OK" : "FAIL");
458
459   /* Send response */
460
461   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
462   rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
463   rpr_msg.op_id = rp_msg->op_id;
464   rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
465   if (GNUNET_OK == res)
466     rpr_msg.op_result = htons (GNUNET_OK);
467   else
468     rpr_msg.op_result = htons (GNUNET_NO);
469   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
470
471   GNUNET_SERVER_receive_done (client, GNUNET_OK);
472 }
473
474
475 static void handle_record_create (void *cls,
476                           struct GNUNET_SERVER_Client * client,
477                           const struct GNUNET_MessageHeader * message)
478 {
479   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
480   struct GNUNET_NAMESTORE_Client *nc;
481   struct RecordCreateResponseMessage rcr_msg;
482   size_t name_len;
483   size_t msg_size;
484   size_t msg_size_exp;
485   uint32_t id = 0;
486
487   int res = GNUNET_SYSERR;
488
489   if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
490   {
491     GNUNET_break_op (0);
492     GNUNET_SERVER_receive_done (client, GNUNET_OK);
493     return;
494   }
495
496   nc = client_lookup(client);
497   if (nc == NULL)
498   {
499     GNUNET_break_op (0);
500     GNUNET_SERVER_receive_done (client, GNUNET_OK);
501     return;
502   }
503
504   struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
505   id = ntohl (rp_msg->op_id);
506   name_len = ntohs (rp_msg->name_len);
507   msg_size = ntohs (message->size);
508   msg_size_exp = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
509
510   if (msg_size != msg_size_exp)
511   {
512     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
513     GNUNET_break_op (0);
514     GNUNET_SERVER_receive_done (client, GNUNET_OK);
515     return;
516   }
517
518
519   if ((name_len == 0) || (name_len > 256))
520   {
521     GNUNET_break_op (0);
522     GNUNET_SERVER_receive_done (client, GNUNET_OK);
523     return;
524   }
525 /*
526   if (name[name_len -1] != '\0')
527   {
528     GNUNET_break_op (0);
529     GNUNET_SERVER_receive_done (client, GNUNET_OK);
530     return;
531   }
532 */
533   /* DO WORK HERE */
534
535   /* Send response */
536
537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
538   rcr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
539   rcr_msg.op_id = rp_msg->op_id;
540   rcr_msg.header.size = htons (sizeof (struct RecordCreateResponseMessage));
541   if (GNUNET_OK == res)
542     rcr_msg.op_result = htons (GNUNET_OK);
543   else
544     rcr_msg.op_result = htons (GNUNET_NO);
545   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
546
547   GNUNET_SERVER_receive_done (client, GNUNET_OK);
548 }
549
550 static void handle_record_remove (void *cls,
551                           struct GNUNET_SERVER_Client * client,
552                           const struct GNUNET_MessageHeader * message)
553 {
554   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
555   struct GNUNET_NAMESTORE_Client *nc;
556   struct RecordRemoveResponseMessage rrr_msg;
557   size_t name_len;
558   size_t msg_size;
559   size_t msg_size_exp;
560   uint32_t id = 0;
561
562   int res = GNUNET_SYSERR;
563
564   if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
565   {
566     GNUNET_break_op (0);
567     GNUNET_SERVER_receive_done (client, GNUNET_OK);
568     return;
569   }
570
571   nc = client_lookup(client);
572   if (nc == NULL)
573   {
574     GNUNET_break_op (0);
575     GNUNET_SERVER_receive_done (client, GNUNET_OK);
576     return;
577   }
578
579   struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message;
580   id = ntohl (rp_msg->op_id);
581   name_len = ntohs (rp_msg->name_len);
582   msg_size = ntohs (message->size);
583   msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
584
585   if (msg_size != msg_size_exp)
586   {
587     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
588     GNUNET_break_op (0);
589     GNUNET_SERVER_receive_done (client, GNUNET_OK);
590     return;
591   }
592
593
594   if ((name_len == 0) || (name_len > 256))
595   {
596     GNUNET_break_op (0);
597     GNUNET_SERVER_receive_done (client, GNUNET_OK);
598     return;
599   }
600 /*
601   if (name[name_len -1] != '\0')
602   {
603     GNUNET_break_op (0);
604     GNUNET_SERVER_receive_done (client, GNUNET_OK);
605     return;
606   }
607   */
608   /* DO WORK HERE */
609
610   /* Send response */
611
612   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
613   rrr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
614   rrr_msg.op_id = rp_msg->op_id;
615   rrr_msg.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
616   if (GNUNET_OK == res)
617     rrr_msg.op_result = htons (GNUNET_OK);
618   else
619     rrr_msg.op_result = htons (GNUNET_NO);
620   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
621
622   GNUNET_SERVER_receive_done (client, GNUNET_OK);
623 }
624
625 struct ZoneIterationProcResult
626 {
627   int have_zone_key;
628   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
629
630   int have_signature;
631   struct GNUNET_CRYPTO_RsaSignature signature;
632   struct GNUNET_TIME_Absolute expire;
633
634   int have_name;
635   char name[256];
636
637   size_t rd_ser_len;
638   char *rd_ser;
639 };
640
641
642 void zone_iteration_proc (void *cls,
643                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
644                          struct GNUNET_TIME_Absolute expire,
645                          const char *name,
646                          unsigned int rd_count,
647                          const struct GNUNET_NAMESTORE_RecordData *rd,
648                          const struct GNUNET_CRYPTO_RsaSignature *signature)
649 {
650   struct ZoneIterationProcResult *zipr = cls;
651   size_t len;
652   if (zone_key != NULL)
653   {
654     zipr->zone_key = *zone_key;
655     zipr->have_zone_key = GNUNET_YES;
656   }
657   else
658     zipr->have_zone_key = GNUNET_NO;
659
660   zipr->expire = expire;
661
662   if (name != NULL)
663   {
664     memcpy (zipr->name, name, strlen(name) + 1);
665     zipr->have_name = GNUNET_YES;
666   }
667   else
668     zipr->have_name = GNUNET_NO;
669
670   if (signature != NULL)
671   {
672     zipr->signature = *signature;
673     zipr->have_signature = GNUNET_YES;
674   }
675   else
676     zipr->have_signature = GNUNET_NO;
677
678   if ((rd_count > 0) && (rd != NULL))
679   {
680     len = GNUNET_NAMESTORE_records_serialize (&zipr->rd_ser, rd_count, rd);
681     zipr->rd_ser_len = len;
682   }
683 }
684
685 static void handle_iteration_start (void *cls,
686                           struct GNUNET_SERVER_Client * client,
687                           const struct GNUNET_MessageHeader * message)
688 {
689   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
690
691   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
692   struct GNUNET_NAMESTORE_Client *nc;
693   struct GNUNET_NAMESTORE_ZoneIteration *zi;
694   struct ZoneIterationResponseMessage zir_msg;
695   struct ZoneIterationProcResult zipr;
696   int res;
697
698   nc = client_lookup(client);
699   if (nc == NULL)
700   {
701     GNUNET_break_op (0);
702     GNUNET_SERVER_receive_done (client, GNUNET_OK);
703     return;
704   }
705
706   zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
707   zi->op_id = ntohl (zis_msg->op_id);
708   zi->offset = 0;
709   zi->client = nc;
710   zi->zone = zis_msg->zone;
711
712   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
713
714   res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
715   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "iterate_records result %u\n", res);
716   switch (res) {
717     case GNUNET_OK:
718       /* GNUNET_OK on success */
719
720       break;
721     case GNUNET_SYSERR:
722       /* GNUNET_SYSERR on error */
723       break;
724     case GNUNET_NO:
725       /* GNUNET_NO if there were no results, */
726       break;
727     default:
728       break;
729   }
730
731
732
733   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
734   zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
735   zir_msg.op_id = htonl(zi->op_id);
736   zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
737
738
739   GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
740
741
742   GNUNET_SERVER_receive_done (client, GNUNET_OK);
743 }
744
745 static void handle_iteration_stop (void *cls,
746                           struct GNUNET_SERVER_Client * client,
747                           const struct GNUNET_MessageHeader * message)
748 {
749   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
750
751   struct GNUNET_NAMESTORE_Client *nc;
752   struct GNUNET_NAMESTORE_ZoneIteration *zi;
753   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
754   uint32_t id;
755
756   nc = client_lookup(client);
757   if (nc == NULL)
758   {
759     GNUNET_break_op (0);
760     GNUNET_SERVER_receive_done (client, GNUNET_OK);
761     return;
762   }
763
764   id = ntohl (zis_msg->op_id);
765   for (zi = nc->op_head; zi != NULL; zi = zi->next)
766   {
767     if (zi->op_id == id)
768       break;
769   }
770   if (zi == NULL)
771   {
772     GNUNET_break_op (0);
773     GNUNET_SERVER_receive_done (client, GNUNET_OK);
774     return;
775   }
776
777   GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
778   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
779   GNUNET_free (zi);
780
781   GNUNET_SERVER_receive_done (client, GNUNET_OK);
782 }
783
784 static void handle_iteration_next (void *cls,
785                           struct GNUNET_SERVER_Client * client,
786                           const struct GNUNET_MessageHeader * message)
787 {
788   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
789
790   struct GNUNET_NAMESTORE_Client *nc;
791   struct GNUNET_NAMESTORE_ZoneIteration *zi;
792   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
793   uint32_t id;
794   int res;
795
796   nc = client_lookup(client);
797   if (nc == NULL)
798   {
799     GNUNET_break_op (0);
800     GNUNET_SERVER_receive_done (client, GNUNET_OK);
801     return;
802   }
803
804   id = ntohl (zis_msg->op_id);
805   for (zi = nc->op_head; zi != NULL; zi = zi->next)
806   {
807     if (zi->op_id == id)
808       break;
809   }
810   if (zi == NULL)
811   {
812     GNUNET_break_op (0);
813     GNUNET_SERVER_receive_done (client, GNUNET_OK);
814     return;
815   }
816
817   zi->offset++;
818   res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
819 }
820
821
822
823 /**
824  * Process template requests.
825  *
826  * @param cls closure
827  * @param server the initialized server
828  * @param cfg configuration to use
829  */
830 static void
831 run (void *cls, struct GNUNET_SERVER_Handle *server,
832      const struct GNUNET_CONFIGURATION_Handle *cfg)
833 {
834   char * database;
835
836   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
837
838   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
839     {&handle_start, NULL,
840      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
841     {&handle_lookup_name, NULL,
842      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
843     {&handle_record_put, NULL,
844     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
845     {&handle_record_create, NULL,
846      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
847     {&handle_record_remove, NULL,
848      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
849     {&handle_iteration_start, NULL,
850      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
851     {&handle_iteration_stop, NULL,
852      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
853     {&handle_iteration_next, NULL,
854      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
855     {NULL, NULL, 0, 0}
856   };
857
858   GSN_cfg = cfg;
859
860   /* Loading database plugin */
861   if (GNUNET_OK !=
862       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
863                                              &database))
864     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
865
866   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
867   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
868   if (GSN_database == NULL)
869     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
870         db_lib_name);
871   GNUNET_free (database);
872
873   /* Configuring server handles */
874   GNUNET_SERVER_add_handlers (server, handlers);
875   snc = GNUNET_SERVER_notification_context_create (server, 16);
876   GNUNET_SERVER_disconnect_notify (server,
877                                    &client_disconnect_notification,
878                                    NULL);
879
880   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
881                                 NULL);
882
883 }
884
885
886 /**
887  * The main function for the template service.
888  *
889  * @param argc number of arguments from the command line
890  * @param argv command line arguments
891  * @return 0 ok, 1 on error
892  */
893 int
894 main (int argc, char *const *argv)
895 {
896   return (GNUNET_OK ==
897           GNUNET_SERVICE_run (argc, argv, "namestore",
898                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
899 }
900
901 /* end of gnunet-service-namestore.c */