- record serialization + test
[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   uint32_t id = 0;
373   uint32_t rd_count;
374   int res = GNUNET_SYSERR;
375
376   if (ntohs (message->size) < sizeof (struct RecordPutMessage))
377   {
378     GNUNET_break_op (0);
379     GNUNET_SERVER_receive_done (client, GNUNET_OK);
380     return;
381   }
382
383   nc = client_lookup (client);
384   if (nc == NULL)
385   {
386     GNUNET_break_op (0);
387     GNUNET_SERVER_receive_done (client, GNUNET_OK);
388     return;
389   }
390
391   struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
392   id = ntohl (rp_msg->op_id);
393   name_len = ntohs (rp_msg->name_len);
394   rd_count = ntohl(rp_msg->rd_count);
395   msg_size = ntohs (message->size);
396   msg_size_exp = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len  + rd_count * (sizeof (struct GNUNET_NAMESTORE_RecordData));
397
398   if (msg_size != msg_size_exp)
399   {
400     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
401     GNUNET_break_op (0);
402     GNUNET_SERVER_receive_done (client, GNUNET_OK);
403     return;
404   }
405
406
407   if ((name_len == 0) || (name_len > 256))
408   {
409     GNUNET_break_op (0);
410     GNUNET_SERVER_receive_done (client, GNUNET_OK);
411     return;
412   }
413
414   zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
415   name = (char *) &zone_key[1];
416   expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
417   signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
418   rd = (struct GNUNET_NAMESTORE_RecordData *) &name[name_len];
419
420   /* Database operation */
421   res = GSN_database->put_records(GSN_database->cls,
422                                 zone_key,
423                                 expire,
424                                 name,
425                                 rd_count, rd,
426                                 signature);
427
428   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
429       name, (res == GNUNET_OK) ? "OK" : "FAIL");
430
431   /* Send response */
432
433   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
434   rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
435   rpr_msg.op_id = rp_msg->op_id;
436   rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
437   if (GNUNET_OK == res)
438     rpr_msg.op_result = htons (GNUNET_OK);
439   else
440     rpr_msg.op_result = htons (GNUNET_NO);
441   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
442
443   GNUNET_SERVER_receive_done (client, GNUNET_OK);
444 }
445
446
447 static void handle_record_create (void *cls,
448                           struct GNUNET_SERVER_Client * client,
449                           const struct GNUNET_MessageHeader * message)
450 {
451   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
452   struct GNUNET_NAMESTORE_Client *nc;
453   struct RecordCreateResponseMessage rcr_msg;
454   size_t name_len;
455   size_t msg_size;
456   size_t msg_size_exp;
457   uint32_t id = 0;
458
459   int res = GNUNET_SYSERR;
460
461   if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
462   {
463     GNUNET_break_op (0);
464     GNUNET_SERVER_receive_done (client, GNUNET_OK);
465     return;
466   }
467
468   nc = client_lookup(client);
469   if (nc == NULL)
470   {
471     GNUNET_break_op (0);
472     GNUNET_SERVER_receive_done (client, GNUNET_OK);
473     return;
474   }
475
476   struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
477   id = ntohl (rp_msg->op_id);
478   name_len = ntohs (rp_msg->name_len);
479   msg_size = ntohs (message->size);
480   msg_size_exp = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
481
482   if (msg_size != msg_size_exp)
483   {
484     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
485     GNUNET_break_op (0);
486     GNUNET_SERVER_receive_done (client, GNUNET_OK);
487     return;
488   }
489
490
491   if ((name_len == 0) || (name_len > 256))
492   {
493     GNUNET_break_op (0);
494     GNUNET_SERVER_receive_done (client, GNUNET_OK);
495     return;
496   }
497
498   /* DO WORK HERE */
499
500   /* Send response */
501
502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
503   rcr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
504   rcr_msg.op_id = rp_msg->op_id;
505   rcr_msg.header.size = htons (sizeof (struct RecordCreateResponseMessage));
506   if (GNUNET_OK == res)
507     rcr_msg.op_result = htons (GNUNET_OK);
508   else
509     rcr_msg.op_result = htons (GNUNET_NO);
510   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
511
512   GNUNET_SERVER_receive_done (client, GNUNET_OK);
513 }
514
515 static void handle_record_remove (void *cls,
516                           struct GNUNET_SERVER_Client * client,
517                           const struct GNUNET_MessageHeader * message)
518 {
519   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
520   struct GNUNET_NAMESTORE_Client *nc;
521   struct RecordRemoveResponseMessage rrr_msg;
522   size_t name_len;
523   size_t msg_size;
524   size_t msg_size_exp;
525   uint32_t id = 0;
526
527   int res = GNUNET_SYSERR;
528
529   if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
530   {
531     GNUNET_break_op (0);
532     GNUNET_SERVER_receive_done (client, GNUNET_OK);
533     return;
534   }
535
536   nc = client_lookup(client);
537   if (nc == NULL)
538   {
539     GNUNET_break_op (0);
540     GNUNET_SERVER_receive_done (client, GNUNET_OK);
541     return;
542   }
543
544   struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message;
545   id = ntohl (rp_msg->op_id);
546   name_len = ntohs (rp_msg->name_len);
547   msg_size = ntohs (message->size);
548   msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
549
550   if (msg_size != msg_size_exp)
551   {
552     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
553     GNUNET_break_op (0);
554     GNUNET_SERVER_receive_done (client, GNUNET_OK);
555     return;
556   }
557
558
559   if ((name_len == 0) || (name_len > 256))
560   {
561     GNUNET_break_op (0);
562     GNUNET_SERVER_receive_done (client, GNUNET_OK);
563     return;
564   }
565
566   /* DO WORK HERE */
567
568   /* Send response */
569
570   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
571   rrr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
572   rrr_msg.op_id = rp_msg->op_id;
573   rrr_msg.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
574   if (GNUNET_OK == res)
575     rrr_msg.op_result = htons (GNUNET_OK);
576   else
577     rrr_msg.op_result = htons (GNUNET_NO);
578   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
579
580   GNUNET_SERVER_receive_done (client, GNUNET_OK);
581 }
582
583 struct ZoneIterationProcResult
584 {
585   int have_zone_key;
586   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
587
588   int have_signature;
589   struct GNUNET_CRYPTO_RsaSignature signature;
590   struct GNUNET_TIME_Absolute expire;
591
592   int have_name;
593   char name[256];
594
595   unsigned int rd_count;
596   char *rd_ser;
597 };
598
599
600 void zone_iteration_proc (void *cls,
601                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
602                          struct GNUNET_TIME_Absolute expire,
603                          const char *name,
604                          unsigned int rd_count,
605                          const struct GNUNET_NAMESTORE_RecordData *rd,
606                          const struct GNUNET_CRYPTO_RsaSignature *signature)
607 {
608   struct ZoneIterationProcResult *zipr = cls;
609   size_t len;
610   if (zone_key != NULL)
611   {
612     zipr->zone_key = *zone_key;
613     zipr->have_zone_key = GNUNET_YES;
614   }
615   else
616     zipr->have_zone_key = GNUNET_NO;
617
618   zipr->expire = expire;
619
620   if (name != NULL)
621   {
622     memcpy (zipr->name, name, strlen(name) + 1);
623     zipr->have_name = GNUNET_YES;
624   }
625   else
626     zipr->have_name = GNUNET_NO;
627
628   zipr->rd_count = rd_count;
629
630   if (signature != NULL)
631   {
632     zipr->signature = *signature;
633     zipr->have_signature = GNUNET_YES;
634   }
635   else
636     zipr->have_signature = GNUNET_NO;
637
638   if ((rd_count > 0) && (rd != NULL))
639   {
640     len = GNUNET_NAMESTORE_records_serialize (&zipr->rd_ser, rd_count, rd);
641   }
642 }
643
644 static void handle_iteration_start (void *cls,
645                           struct GNUNET_SERVER_Client * client,
646                           const struct GNUNET_MessageHeader * message)
647 {
648   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
649
650   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
651   struct GNUNET_NAMESTORE_Client *nc;
652   struct GNUNET_NAMESTORE_ZoneIteration *zi;
653   struct ZoneIterationResponseMessage zir_msg;
654   struct ZoneIterationProcResult zipr;
655   int res;
656
657   nc = client_lookup(client);
658   if (nc == NULL)
659   {
660     GNUNET_break_op (0);
661     GNUNET_SERVER_receive_done (client, GNUNET_OK);
662     return;
663   }
664
665   zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
666   zi->op_id = ntohl (zis_msg->op_id);
667   zi->offset = 0;
668   zi->client = nc;
669   zi->zone = zis_msg->zone;
670
671   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
672
673   res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
674
675   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
676   zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
677   zir_msg.op_id = htonl(zi->op_id);
678   zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
679
680   GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
681
682
683   GNUNET_SERVER_receive_done (client, GNUNET_OK);
684 }
685
686 static void handle_iteration_stop (void *cls,
687                           struct GNUNET_SERVER_Client * client,
688                           const struct GNUNET_MessageHeader * message)
689 {
690   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
691
692   struct GNUNET_NAMESTORE_Client *nc;
693   struct GNUNET_NAMESTORE_ZoneIteration *zi;
694   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
695   uint32_t id;
696
697   nc = client_lookup(client);
698   if (nc == NULL)
699   {
700     GNUNET_break_op (0);
701     GNUNET_SERVER_receive_done (client, GNUNET_OK);
702     return;
703   }
704
705   id = ntohl (zis_msg->op_id);
706   for (zi = nc->op_head; zi != NULL; zi = zi->next)
707   {
708     if (zi->op_id == id)
709       break;
710   }
711   if (zi == NULL)
712   {
713     GNUNET_break_op (0);
714     GNUNET_SERVER_receive_done (client, GNUNET_OK);
715     return;
716   }
717
718   GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
719   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
720   GNUNET_free (zi);
721
722   GNUNET_SERVER_receive_done (client, GNUNET_OK);
723 }
724
725 static void handle_iteration_next (void *cls,
726                           struct GNUNET_SERVER_Client * client,
727                           const struct GNUNET_MessageHeader * message)
728 {
729   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
730
731   struct GNUNET_NAMESTORE_Client *nc;
732   struct GNUNET_NAMESTORE_ZoneIteration *zi;
733   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
734   uint32_t id;
735   int res;
736
737   nc = client_lookup(client);
738   if (nc == NULL)
739   {
740     GNUNET_break_op (0);
741     GNUNET_SERVER_receive_done (client, GNUNET_OK);
742     return;
743   }
744
745   id = ntohl (zis_msg->op_id);
746   for (zi = nc->op_head; zi != NULL; zi = zi->next)
747   {
748     if (zi->op_id == id)
749       break;
750   }
751   if (zi == NULL)
752   {
753     GNUNET_break_op (0);
754     GNUNET_SERVER_receive_done (client, GNUNET_OK);
755     return;
756   }
757
758   zi->offset++;
759   res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
760 }
761
762
763
764 /**
765  * Process template requests.
766  *
767  * @param cls closure
768  * @param server the initialized server
769  * @param cfg configuration to use
770  */
771 static void
772 run (void *cls, struct GNUNET_SERVER_Handle *server,
773      const struct GNUNET_CONFIGURATION_Handle *cfg)
774 {
775   char * database;
776
777   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
778
779   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
780     {&handle_start, NULL,
781      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
782     {&handle_lookup_name, NULL,
783      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
784     {&handle_record_put, NULL,
785     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
786     {&handle_record_create, NULL,
787      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
788     {&handle_record_remove, NULL,
789      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
790     {&handle_iteration_start, NULL,
791      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
792     {&handle_iteration_stop, NULL,
793      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
794     {&handle_iteration_next, NULL,
795      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
796     {NULL, NULL, 0, 0}
797   };
798
799   GSN_cfg = cfg;
800
801   /* Loading database plugin */
802   if (GNUNET_OK !=
803       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
804                                              &database))
805     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
806
807   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
808   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
809   if (GSN_database == NULL)
810     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
811         db_lib_name);
812   GNUNET_free (database);
813
814   /* Configuring server handles */
815   GNUNET_SERVER_add_handlers (server, handlers);
816   snc = GNUNET_SERVER_notification_context_create (server, 16);
817   GNUNET_SERVER_disconnect_notify (server,
818                                    &client_disconnect_notification,
819                                    NULL);
820
821   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
822                                 NULL);
823
824 }
825
826
827 /**
828  * The main function for the template service.
829  *
830  * @param argc number of arguments from the command line
831  * @param argv command line arguments
832  * @return 0 ok, 1 on error
833  */
834 int
835 main (int argc, char *const *argv)
836 {
837   return (GNUNET_OK ==
838           GNUNET_SERVICE_run (argc, argv, "namestore",
839                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
840 }
841
842 /* end of gnunet-service-namestore.c */