- using de/serialization functionality
[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_tmp;
212   char *name_tmp;
213   struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
214
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
216
217   size_t r_size = 0;
218
219   size_t name_len = 0;
220   if (NULL != name)
221     name_len = strlen(name) + 1;
222
223   int copied_elements = 0;
224   int contains_signature = 0;
225   int c;
226
227   /* count records to copy */
228   if (rd_count != 0)
229   {
230     if (lnc->record_type != 0)
231     {
232       /* special record type needed */
233       for (c = 0; c < rd_count; c ++)
234         if (rd[c].record_type == lnc->record_type)
235           copied_elements++; /* found matching record */
236     }
237     else
238       copied_elements = rd_count;
239   }
240
241   if ((copied_elements == rd_count) && (signature != NULL))
242       contains_signature = GNUNET_YES;
243
244   r_size = sizeof (struct LookupNameResponseMessage) +
245            sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
246            name_len +
247            copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData) +
248            contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
249
250   lnr_msg = GNUNET_malloc (r_size);
251
252   lnr_msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
253   lnr_msg->header.size = ntohs (r_size);
254   lnr_msg->op_id = htonl (lnc->id);
255   lnr_msg->rc_count = htonl (copied_elements);
256   lnr_msg->name_len = htons (name_len);
257   lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
258   lnr_msg->contains_sig = htons (contains_signature);
259
260
261   zone_key_tmp =  (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
262   name_tmp = (char *) &zone_key_tmp[1];
263   rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len];
264   signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[copied_elements];
265
266   if (zone_key != NULL)
267     memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
268   else
269   {
270     struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
271     memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
272     memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
273   }
274   memcpy (name_tmp, name, name_len);
275   /* copy records */
276   copied_elements = 0;
277   if (rd_count != 0)
278   {
279     if (lnc->record_type != 0)
280     {
281       /* special record type needed */
282       for (c = 0; c < rd_count; c ++)
283         if (rd[c].record_type == lnc->record_type)
284         {
285           /* found matching record */
286           memcpy (&rd_tmp[copied_elements], &rd[c], rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
287           copied_elements++;
288         }
289     }
290     else
291       memcpy (rd_tmp, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
292   }
293
294   if (GNUNET_YES == contains_signature)
295     memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
296   GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
297
298   GNUNET_free (lnr_msg);
299 }
300
301 static void handle_lookup_name (void *cls,
302                           struct GNUNET_SERVER_Client * client,
303                           const struct GNUNET_MessageHeader * message)
304 {
305   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
306   struct LookupNameContext lnc;
307   struct GNUNET_NAMESTORE_Client *nc;
308   GNUNET_HashCode name_hash;
309   size_t name_len;
310   char * name;
311   uint32_t id = 0;
312   uint32_t type = 0;
313
314
315   if (ntohs (message->size) < sizeof (struct LookupNameMessage))
316   {
317     GNUNET_break_op (0);
318     GNUNET_SERVER_receive_done (client, GNUNET_OK);
319     return;
320   }
321
322   nc = client_lookup(client);
323   if (nc == NULL)
324   {
325     GNUNET_break_op (0);
326     GNUNET_SERVER_receive_done (client, GNUNET_OK);
327     return;
328   }
329
330   struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
331   id = ntohl (ln_msg->op_id);
332   name_len = ntohl (ln_msg->name_len);
333   type = ntohl (ln_msg->record_type);
334
335   if ((name_len == 0) || (name_len > 256))
336   {
337     GNUNET_break_op (0);
338     GNUNET_SERVER_receive_done (client, GNUNET_OK);
339     return;
340   }
341
342   name = GNUNET_malloc (name_len);
343   memcpy (name, &ln_msg[1], name_len);
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s'\n", name);
345   GNUNET_CRYPTO_hash(name, name_len-1, &name_hash);
346   GNUNET_free (name);
347
348   /* do the actual lookup */
349   lnc.id = id;
350   lnc.nc = nc;
351   lnc.record_type = type;
352   GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, &ln_msg->zone, 0, &handle_lookup_name_it, &lnc);
353
354   GNUNET_SERVER_receive_done (client, GNUNET_OK);
355 }
356
357 static void handle_record_put (void *cls,
358                           struct GNUNET_SERVER_Client * client,
359                           const struct GNUNET_MessageHeader * message)
360 {
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
362   struct GNUNET_NAMESTORE_Client *nc;
363   struct GNUNET_TIME_Absolute expire;
364   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
365   struct GNUNET_NAMESTORE_RecordData *rd;
366   struct GNUNET_CRYPTO_RsaSignature *signature;
367   struct RecordPutResponseMessage rpr_msg;
368   size_t name_len;
369   size_t msg_size;
370   size_t msg_size_exp;
371   char * name;
372   char * rd_ser;
373   uint32_t id = 0;
374   uint32_t rd_ser_len;
375   uint32_t rd_count;
376   int res = GNUNET_SYSERR;
377
378   if (ntohs (message->size) < sizeof (struct RecordPutMessage))
379   {
380     GNUNET_break_op (0);
381     GNUNET_SERVER_receive_done (client, GNUNET_OK);
382     return;
383   }
384
385   nc = client_lookup (client);
386   if (nc == NULL)
387   {
388     GNUNET_break_op (0);
389     GNUNET_SERVER_receive_done (client, GNUNET_OK);
390     return;
391   }
392
393   struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
394   id = ntohl (rp_msg->op_id);
395   name_len = ntohs (rp_msg->name_len);
396   rd_ser_len = ntohs(rp_msg->rd_len);
397   msg_size = ntohs (message->size);
398   msg_size_exp = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len  + rd_ser_len;
399
400   if (msg_size != msg_size_exp)
401   {
402     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
403     GNUNET_break_op (0);
404     GNUNET_SERVER_receive_done (client, GNUNET_OK);
405     return;
406   }
407
408
409   if ((name_len == 0) || (name_len > 256))
410   {
411     GNUNET_break_op (0);
412     GNUNET_SERVER_receive_done (client, GNUNET_OK);
413     return;
414   }
415
416   zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
417   name = (char *) &zone_key[1];
418   expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
419   signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
420   rd_ser = &name[name_len];
421   rd_count = GNUNET_NAMESTORE_records_deserialize(&rd, rd_ser, rd_ser_len);
422
423   /* Database operation */
424   res = GSN_database->put_records(GSN_database->cls,
425                                 zone_key,
426                                 expire,
427                                 name,
428                                 rd_count, rd,
429                                 signature);
430
431   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
432       name, (res == GNUNET_OK) ? "OK" : "FAIL");
433
434   /* Send response */
435
436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
437   rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
438   rpr_msg.op_id = rp_msg->op_id;
439   rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
440   if (GNUNET_OK == res)
441     rpr_msg.op_result = htons (GNUNET_OK);
442   else
443     rpr_msg.op_result = htons (GNUNET_NO);
444   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
445
446   GNUNET_SERVER_receive_done (client, GNUNET_OK);
447 }
448
449
450 static void handle_record_create (void *cls,
451                           struct GNUNET_SERVER_Client * client,
452                           const struct GNUNET_MessageHeader * message)
453 {
454   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
455   struct GNUNET_NAMESTORE_Client *nc;
456   struct RecordCreateResponseMessage rcr_msg;
457   size_t name_len;
458   size_t msg_size;
459   size_t msg_size_exp;
460   uint32_t id = 0;
461
462   int res = GNUNET_SYSERR;
463
464   if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
465   {
466     GNUNET_break_op (0);
467     GNUNET_SERVER_receive_done (client, GNUNET_OK);
468     return;
469   }
470
471   nc = client_lookup(client);
472   if (nc == NULL)
473   {
474     GNUNET_break_op (0);
475     GNUNET_SERVER_receive_done (client, GNUNET_OK);
476     return;
477   }
478
479   struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
480   id = ntohl (rp_msg->op_id);
481   name_len = ntohs (rp_msg->name_len);
482   msg_size = ntohs (message->size);
483   msg_size_exp = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
484
485   if (msg_size != msg_size_exp)
486   {
487     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
488     GNUNET_break_op (0);
489     GNUNET_SERVER_receive_done (client, GNUNET_OK);
490     return;
491   }
492
493
494   if ((name_len == 0) || (name_len > 256))
495   {
496     GNUNET_break_op (0);
497     GNUNET_SERVER_receive_done (client, GNUNET_OK);
498     return;
499   }
500
501   /* DO WORK HERE */
502
503   /* Send response */
504
505   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
506   rcr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
507   rcr_msg.op_id = rp_msg->op_id;
508   rcr_msg.header.size = htons (sizeof (struct RecordCreateResponseMessage));
509   if (GNUNET_OK == res)
510     rcr_msg.op_result = htons (GNUNET_OK);
511   else
512     rcr_msg.op_result = htons (GNUNET_NO);
513   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
514
515   GNUNET_SERVER_receive_done (client, GNUNET_OK);
516 }
517
518 static void handle_record_remove (void *cls,
519                           struct GNUNET_SERVER_Client * client,
520                           const struct GNUNET_MessageHeader * message)
521 {
522   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
523   struct GNUNET_NAMESTORE_Client *nc;
524   struct RecordRemoveResponseMessage rrr_msg;
525   size_t name_len;
526   size_t msg_size;
527   size_t msg_size_exp;
528   uint32_t id = 0;
529
530   int res = GNUNET_SYSERR;
531
532   if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
533   {
534     GNUNET_break_op (0);
535     GNUNET_SERVER_receive_done (client, GNUNET_OK);
536     return;
537   }
538
539   nc = client_lookup(client);
540   if (nc == NULL)
541   {
542     GNUNET_break_op (0);
543     GNUNET_SERVER_receive_done (client, GNUNET_OK);
544     return;
545   }
546
547   struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message;
548   id = ntohl (rp_msg->op_id);
549   name_len = ntohs (rp_msg->name_len);
550   msg_size = ntohs (message->size);
551   msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
552
553   if (msg_size != msg_size_exp)
554   {
555     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
556     GNUNET_break_op (0);
557     GNUNET_SERVER_receive_done (client, GNUNET_OK);
558     return;
559   }
560
561
562   if ((name_len == 0) || (name_len > 256))
563   {
564     GNUNET_break_op (0);
565     GNUNET_SERVER_receive_done (client, GNUNET_OK);
566     return;
567   }
568
569   /* DO WORK HERE */
570
571   /* Send response */
572
573   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
574   rrr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
575   rrr_msg.op_id = rp_msg->op_id;
576   rrr_msg.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
577   if (GNUNET_OK == res)
578     rrr_msg.op_result = htons (GNUNET_OK);
579   else
580     rrr_msg.op_result = htons (GNUNET_NO);
581   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
582
583   GNUNET_SERVER_receive_done (client, GNUNET_OK);
584 }
585
586 struct ZoneIterationProcResult
587 {
588   int have_zone_key;
589   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
590
591   int have_signature;
592   struct GNUNET_CRYPTO_RsaSignature signature;
593   struct GNUNET_TIME_Absolute expire;
594
595   int have_name;
596   char name[256];
597
598   unsigned int rd_count;
599   char *rd_ser;
600 };
601
602
603 void zone_iteration_proc (void *cls,
604                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
605                          struct GNUNET_TIME_Absolute expire,
606                          const char *name,
607                          unsigned int rd_count,
608                          const struct GNUNET_NAMESTORE_RecordData *rd,
609                          const struct GNUNET_CRYPTO_RsaSignature *signature)
610 {
611   struct ZoneIterationProcResult *zipr = cls;
612   size_t len;
613   if (zone_key != NULL)
614   {
615     zipr->zone_key = *zone_key;
616     zipr->have_zone_key = GNUNET_YES;
617   }
618   else
619     zipr->have_zone_key = GNUNET_NO;
620
621   zipr->expire = expire;
622
623   if (name != NULL)
624   {
625     memcpy (zipr->name, name, strlen(name) + 1);
626     zipr->have_name = GNUNET_YES;
627   }
628   else
629     zipr->have_name = GNUNET_NO;
630
631   zipr->rd_count = rd_count;
632
633   if (signature != NULL)
634   {
635     zipr->signature = *signature;
636     zipr->have_signature = GNUNET_YES;
637   }
638   else
639     zipr->have_signature = GNUNET_NO;
640
641   if ((rd_count > 0) && (rd != NULL))
642   {
643     len = GNUNET_NAMESTORE_records_serialize (&zipr->rd_ser, rd_count, rd);
644   }
645 }
646
647 static void handle_iteration_start (void *cls,
648                           struct GNUNET_SERVER_Client * client,
649                           const struct GNUNET_MessageHeader * message)
650 {
651   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
652
653   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
654   struct GNUNET_NAMESTORE_Client *nc;
655   struct GNUNET_NAMESTORE_ZoneIteration *zi;
656   struct ZoneIterationResponseMessage zir_msg;
657   struct ZoneIterationProcResult zipr;
658   int res;
659
660   nc = client_lookup(client);
661   if (nc == NULL)
662   {
663     GNUNET_break_op (0);
664     GNUNET_SERVER_receive_done (client, GNUNET_OK);
665     return;
666   }
667
668   zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
669   zi->op_id = ntohl (zis_msg->op_id);
670   zi->offset = 0;
671   zi->client = nc;
672   zi->zone = zis_msg->zone;
673
674   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
675
676   res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
677
678   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
679   zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
680   zir_msg.op_id = htonl(zi->op_id);
681   zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
682
683   GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
684
685
686   GNUNET_SERVER_receive_done (client, GNUNET_OK);
687 }
688
689 static void handle_iteration_stop (void *cls,
690                           struct GNUNET_SERVER_Client * client,
691                           const struct GNUNET_MessageHeader * message)
692 {
693   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
694
695   struct GNUNET_NAMESTORE_Client *nc;
696   struct GNUNET_NAMESTORE_ZoneIteration *zi;
697   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
698   uint32_t id;
699
700   nc = client_lookup(client);
701   if (nc == NULL)
702   {
703     GNUNET_break_op (0);
704     GNUNET_SERVER_receive_done (client, GNUNET_OK);
705     return;
706   }
707
708   id = ntohl (zis_msg->op_id);
709   for (zi = nc->op_head; zi != NULL; zi = zi->next)
710   {
711     if (zi->op_id == id)
712       break;
713   }
714   if (zi == NULL)
715   {
716     GNUNET_break_op (0);
717     GNUNET_SERVER_receive_done (client, GNUNET_OK);
718     return;
719   }
720
721   GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
722   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
723   GNUNET_free (zi);
724
725   GNUNET_SERVER_receive_done (client, GNUNET_OK);
726 }
727
728 static void handle_iteration_next (void *cls,
729                           struct GNUNET_SERVER_Client * client,
730                           const struct GNUNET_MessageHeader * message)
731 {
732   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
733
734   struct GNUNET_NAMESTORE_Client *nc;
735   struct GNUNET_NAMESTORE_ZoneIteration *zi;
736   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
737   uint32_t id;
738   int res;
739
740   nc = client_lookup(client);
741   if (nc == NULL)
742   {
743     GNUNET_break_op (0);
744     GNUNET_SERVER_receive_done (client, GNUNET_OK);
745     return;
746   }
747
748   id = ntohl (zis_msg->op_id);
749   for (zi = nc->op_head; zi != NULL; zi = zi->next)
750   {
751     if (zi->op_id == id)
752       break;
753   }
754   if (zi == NULL)
755   {
756     GNUNET_break_op (0);
757     GNUNET_SERVER_receive_done (client, GNUNET_OK);
758     return;
759   }
760
761   zi->offset++;
762   res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
763 }
764
765
766
767 /**
768  * Process template requests.
769  *
770  * @param cls closure
771  * @param server the initialized server
772  * @param cfg configuration to use
773  */
774 static void
775 run (void *cls, struct GNUNET_SERVER_Handle *server,
776      const struct GNUNET_CONFIGURATION_Handle *cfg)
777 {
778   char * database;
779
780   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
781
782   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
783     {&handle_start, NULL,
784      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
785     {&handle_lookup_name, NULL,
786      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
787     {&handle_record_put, NULL,
788     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
789     {&handle_record_create, NULL,
790      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
791     {&handle_record_remove, NULL,
792      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
793     {&handle_iteration_start, NULL,
794      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
795     {&handle_iteration_stop, NULL,
796      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
797     {&handle_iteration_next, NULL,
798      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
799     {NULL, NULL, 0, 0}
800   };
801
802   GSN_cfg = cfg;
803
804   /* Loading database plugin */
805   if (GNUNET_OK !=
806       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
807                                              &database))
808     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
809
810   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
811   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
812   if (GSN_database == NULL)
813     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
814         db_lib_name);
815   GNUNET_free (database);
816
817   /* Configuring server handles */
818   GNUNET_SERVER_add_handlers (server, handlers);
819   snc = GNUNET_SERVER_notification_context_create (server, 16);
820   GNUNET_SERVER_disconnect_notify (server,
821                                    &client_disconnect_notification,
822                                    NULL);
823
824   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
825                                 NULL);
826
827 }
828
829
830 /**
831  * The main function for the template service.
832  *
833  * @param argc number of arguments from the command line
834  * @param argv command line arguments
835  * @return 0 ok, 1 on error
836  */
837 int
838 main (int argc, char *const *argv)
839 {
840   return (GNUNET_OK ==
841           GNUNET_SERVICE_run (argc, argv, "namestore",
842                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
843 }
844
845 /* end of gnunet-service-namestore.c */