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