e796fe530d97a7d45cbfab7f635caa0be02461fe
[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   uint64_t op_id;
46   uint32_t offset;
47
48 };
49
50
51 /**
52  * A namestore client
53  */
54 struct GNUNET_NAMESTORE_Client
55 {
56   struct GNUNET_NAMESTORE_Client *next;
57   struct GNUNET_NAMESTORE_Client *prev;
58
59   struct GNUNET_SERVER_Client * client;
60
61   struct GNUNET_NAMESTORE_ZoneIteration *op_head;
62   struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
63 };
64
65
66
67 /**
68  * Configuration handle.
69  */
70 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
71
72 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
73
74 /**
75  * Our notification context.
76  */
77 static struct GNUNET_SERVER_NotificationContext *snc;
78
79 static char *db_lib_name;
80
81 static struct GNUNET_NAMESTORE_Client *client_head;
82 static struct GNUNET_NAMESTORE_Client *client_tail;
83
84
85 /**
86  * Task run during shutdown.
87  *
88  * @param cls unused
89  * @param tc unused
90  */
91 static void
92 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
95
96   struct GNUNET_NAMESTORE_ZoneIteration * no;
97   struct GNUNET_NAMESTORE_ZoneIteration * tmp;
98   struct GNUNET_NAMESTORE_Client * nc;
99   struct GNUNET_NAMESTORE_Client * next;
100
101   for (nc = client_head; nc != NULL; nc = next)
102   {
103     next = nc->next;
104     for (no = nc->op_head; no != NULL; no = tmp)
105     {
106       GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
107       tmp = no->next;
108       GNUNET_free (no);
109     }
110
111     GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
112     GNUNET_free (nc);
113
114   }
115
116   GNUNET_SERVER_notification_context_destroy (snc);
117   snc = NULL;
118
119   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
120   GNUNET_free (db_lib_name);
121 }
122
123 static struct GNUNET_NAMESTORE_Client *
124 client_lookup (struct GNUNET_SERVER_Client *client)
125 {
126   struct GNUNET_NAMESTORE_Client * nc;
127
128   GNUNET_assert (NULL != client);
129
130   for (nc = client_head; nc != NULL; nc = nc->next)
131   {
132     if (client == nc->client)
133       break;
134   }
135   return nc;
136 }
137
138
139 /**
140  * Called whenever a client is disconnected.  Frees our
141  * resources associated with that client.
142  *
143  * @param cls closure
144  * @param client identification of the client
145  */
146 static void
147 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
148 {
149   struct GNUNET_NAMESTORE_ZoneIteration * no;
150   struct GNUNET_NAMESTORE_Client * nc;
151   if (NULL == client)
152     return;
153
154   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
155
156   nc = client_lookup (client);
157
158   if ((NULL == client) || (NULL == nc))
159     return;
160
161   for (no = nc->op_head; no != NULL; no = no->next)
162   {
163     GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
164     GNUNET_free (no);
165   }
166
167   GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
168   GNUNET_free (nc);
169 }
170
171 static void handle_start (void *cls,
172                           struct GNUNET_SERVER_Client * client,
173                           const struct GNUNET_MessageHeader * message)
174 {
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
176
177   struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
178   nc->client = client;
179   GNUNET_SERVER_notification_context_add (snc, client);
180   GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
181
182   GNUNET_SERVER_receive_done (client, GNUNET_OK);
183 }
184
185 struct LookupNameContext
186 {
187   struct GNUNET_NAMESTORE_Client *nc;
188   uint32_t id;
189   uint32_t record_type;
190 };
191
192
193 static void
194 handle_lookup_name_it (void *cls,
195     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
196     struct GNUNET_TIME_Absolute expire,
197     const char *name,
198     unsigned int rd_count,
199     const struct GNUNET_NAMESTORE_RecordData *rd,
200     const struct GNUNET_CRYPTO_RsaSignature *signature)
201 {
202   /* send response */
203   struct LookupNameContext *lnc = cls;
204   struct LookupNameResponseMessage *lnr_msg;
205
206   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
207   struct GNUNET_NAMESTORE_RecordData * rd_tmp;
208   char *name_tmp;
209   struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
212
213   size_t r_size = 0;
214
215   size_t name_len = 0;
216   if (NULL != name)
217     name_len = strlen(name) + 1;
218
219   int copied_elements = 0;
220   int contains_signature = 0;
221   int c;
222
223   /* count records to copy */
224   if (rd_count != 0)
225   {
226     if (lnc->record_type != 0)
227     {
228       /* special record type needed */
229       for (c = 0; c < rd_count; c ++)
230         if (rd[c].record_type == lnc->record_type)
231           copied_elements++; /* found matching record */
232     }
233     else
234       copied_elements = rd_count;
235   }
236
237   if ((copied_elements == rd_count) && (signature != NULL))
238       contains_signature = GNUNET_YES;
239
240   r_size = sizeof (struct LookupNameResponseMessage) +
241            sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
242            name_len +
243            copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData) +
244            contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
245
246   lnr_msg = GNUNET_malloc (r_size);
247
248   lnr_msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
249   lnr_msg->header.size = ntohs (r_size);
250   lnr_msg->op_id = htonl (lnc->id);
251   lnr_msg->rc_count = htonl (copied_elements);
252   lnr_msg->name_len = htons (name_len);
253   lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
254   lnr_msg->contains_sig = htons (contains_signature);
255
256
257   zone_key_tmp =  (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
258   name_tmp = (char *) &zone_key_tmp[1];
259   rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len];
260   signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[copied_elements];
261
262   if (zone_key != NULL)
263     memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
264   else
265   {
266     struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
267     memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
268     memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
269   }
270   memcpy (name_tmp, name, name_len);
271   /* copy records */
272   copied_elements = 0;
273   if (rd_count != 0)
274   {
275     if (lnc->record_type != 0)
276     {
277       /* special record type needed */
278       for (c = 0; c < rd_count; c ++)
279         if (rd[c].record_type == lnc->record_type)
280         {
281           /* found matching record */
282           memcpy (&rd_tmp[copied_elements], &rd[c], rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
283           copied_elements++;
284         }
285     }
286     else
287       memcpy (rd_tmp, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
288   }
289
290   if (GNUNET_YES == contains_signature)
291     memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
292   GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
293
294   GNUNET_free (lnr_msg);
295 }
296
297 static void handle_lookup_name (void *cls,
298                           struct GNUNET_SERVER_Client * client,
299                           const struct GNUNET_MessageHeader * message)
300 {
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
302   struct LookupNameContext lnc;
303   struct GNUNET_NAMESTORE_Client *nc;
304   GNUNET_HashCode name_hash;
305   size_t name_len;
306   char * name;
307   uint32_t id = 0;
308   uint32_t type = 0;
309
310
311   if (ntohs (message->size) < sizeof (struct LookupNameMessage))
312   {
313     GNUNET_break_op (0);
314     GNUNET_SERVER_receive_done (client, GNUNET_OK);
315     return;
316   }
317
318   nc = client_lookup(client);
319   if (nc == NULL)
320   {
321     GNUNET_break_op (0);
322     GNUNET_SERVER_receive_done (client, GNUNET_OK);
323     return;
324   }
325
326   struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
327   id = ntohl (ln_msg->op_id);
328   name_len = ntohl (ln_msg->name_len);
329   type = ntohl (ln_msg->record_type);
330
331   if ((name_len == 0) || (name_len > 256))
332   {
333     GNUNET_break_op (0);
334     GNUNET_SERVER_receive_done (client, GNUNET_OK);
335     return;
336   }
337
338   name = GNUNET_malloc (name_len);
339   memcpy (name, &ln_msg[1], name_len);
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s'\n", name);
341   GNUNET_CRYPTO_hash(name, name_len-1, &name_hash);
342   GNUNET_free (name);
343
344   /* do the actual lookup */
345   lnc.id = id;
346   lnc.nc = nc;
347   lnc.record_type = type;
348   GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, &ln_msg->zone, 0, &handle_lookup_name_it, &lnc);
349
350   GNUNET_SERVER_receive_done (client, GNUNET_OK);
351 }
352
353 static void handle_record_put (void *cls,
354                           struct GNUNET_SERVER_Client * client,
355                           const struct GNUNET_MessageHeader * message)
356 {
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
358   struct GNUNET_NAMESTORE_Client *nc;
359   struct GNUNET_TIME_Absolute expire;
360   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
361   struct GNUNET_NAMESTORE_RecordData *rd;
362   struct GNUNET_CRYPTO_RsaSignature *signature;
363   struct RecordPutResponseMessage rpr_msg;
364   size_t name_len;
365   size_t msg_size;
366   size_t msg_size_exp;
367   char * name;
368   uint32_t id = 0;
369   uint32_t rd_count;
370   int res = GNUNET_SYSERR;
371
372   if (ntohs (message->size) < sizeof (struct RecordPutMessage))
373   {
374     GNUNET_break_op (0);
375     GNUNET_SERVER_receive_done (client, GNUNET_OK);
376     return;
377   }
378
379   nc = client_lookup (client);
380   if (nc == NULL)
381   {
382     GNUNET_break_op (0);
383     GNUNET_SERVER_receive_done (client, GNUNET_OK);
384     return;
385   }
386
387   struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
388   id = ntohl (rp_msg->op_id);
389   name_len = ntohs (rp_msg->name_len);
390   rd_count = ntohl(rp_msg->rd_count);
391   msg_size = ntohs (message->size);
392   msg_size_exp = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len  + rd_count * (sizeof (struct GNUNET_NAMESTORE_RecordData));
393
394   if (msg_size != msg_size_exp)
395   {
396     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
397     GNUNET_break_op (0);
398     GNUNET_SERVER_receive_done (client, GNUNET_OK);
399     return;
400   }
401
402
403   if ((name_len == 0) || (name_len > 256))
404   {
405     GNUNET_break_op (0);
406     GNUNET_SERVER_receive_done (client, GNUNET_OK);
407     return;
408   }
409
410   zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
411   name = (char *) &zone_key[1];
412   expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
413   signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
414   rd = (struct GNUNET_NAMESTORE_RecordData *) &name[name_len];
415
416   /* Database operation */
417   res = GSN_database->put_records(GSN_database->cls,
418                                 zone_key,
419                                 expire,
420                                 name,
421                                 rd_count, rd,
422                                 signature);
423
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
425       name, (res == GNUNET_OK) ? "OK" : "FAIL");
426
427   /* Send response */
428
429   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
430   rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
431   rpr_msg.op_id = rp_msg->op_id;
432   rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
433   if (GNUNET_OK == res)
434     rpr_msg.op_result = htons (GNUNET_OK);
435   else
436     rpr_msg.op_result = htons (GNUNET_NO);
437   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
438
439   GNUNET_SERVER_receive_done (client, GNUNET_OK);
440 }
441
442
443 static void handle_record_create (void *cls,
444                           struct GNUNET_SERVER_Client * client,
445                           const struct GNUNET_MessageHeader * message)
446 {
447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
448   struct GNUNET_NAMESTORE_Client *nc;
449   struct RecordCreateResponseMessage rcr_msg;
450   size_t name_len;
451   size_t msg_size;
452   size_t msg_size_exp;
453   uint32_t id = 0;
454
455   int res = GNUNET_SYSERR;
456
457   if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
458   {
459     GNUNET_break_op (0);
460     GNUNET_SERVER_receive_done (client, GNUNET_OK);
461     return;
462   }
463
464   nc = client_lookup(client);
465   if (nc == NULL)
466   {
467     GNUNET_break_op (0);
468     GNUNET_SERVER_receive_done (client, GNUNET_OK);
469     return;
470   }
471
472   struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
473   id = ntohl (rp_msg->op_id);
474   name_len = ntohs (rp_msg->name_len);
475   msg_size = ntohs (message->size);
476   msg_size_exp = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
477
478   if (msg_size != msg_size_exp)
479   {
480     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
481     GNUNET_break_op (0);
482     GNUNET_SERVER_receive_done (client, GNUNET_OK);
483     return;
484   }
485
486
487   if ((name_len == 0) || (name_len > 256))
488   {
489     GNUNET_break_op (0);
490     GNUNET_SERVER_receive_done (client, GNUNET_OK);
491     return;
492   }
493
494   /* DO WORK HERE */
495
496   /* Send response */
497
498   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
499   rcr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
500   rcr_msg.op_id = rp_msg->op_id;
501   rcr_msg.header.size = htons (sizeof (struct RecordCreateResponseMessage));
502   if (GNUNET_OK == res)
503     rcr_msg.op_result = htons (GNUNET_OK);
504   else
505     rcr_msg.op_result = htons (GNUNET_NO);
506   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
507
508   GNUNET_SERVER_receive_done (client, GNUNET_OK);
509 }
510
511 static void handle_record_remove (void *cls,
512                           struct GNUNET_SERVER_Client * client,
513                           const struct GNUNET_MessageHeader * message)
514 {
515   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
516   struct GNUNET_NAMESTORE_Client *nc;
517   struct RecordRemoveResponseMessage rrr_msg;
518   size_t name_len;
519   size_t msg_size;
520   size_t msg_size_exp;
521   uint32_t id = 0;
522
523   int res = GNUNET_SYSERR;
524
525   if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
526   {
527     GNUNET_break_op (0);
528     GNUNET_SERVER_receive_done (client, GNUNET_OK);
529     return;
530   }
531
532   nc = client_lookup(client);
533   if (nc == NULL)
534   {
535     GNUNET_break_op (0);
536     GNUNET_SERVER_receive_done (client, GNUNET_OK);
537     return;
538   }
539
540   struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message;
541   id = ntohl (rp_msg->op_id);
542   name_len = ntohs (rp_msg->name_len);
543   msg_size = ntohs (message->size);
544   msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
545
546   if (msg_size != msg_size_exp)
547   {
548     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
549     GNUNET_break_op (0);
550     GNUNET_SERVER_receive_done (client, GNUNET_OK);
551     return;
552   }
553
554
555   if ((name_len == 0) || (name_len > 256))
556   {
557     GNUNET_break_op (0);
558     GNUNET_SERVER_receive_done (client, GNUNET_OK);
559     return;
560   }
561
562   /* DO WORK HERE */
563
564   /* Send response */
565
566   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
567   rrr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
568   rrr_msg.op_id = rp_msg->op_id;
569   rrr_msg.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
570   if (GNUNET_OK == res)
571     rrr_msg.op_result = htons (GNUNET_OK);
572   else
573     rrr_msg.op_result = htons (GNUNET_NO);
574   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
575
576   GNUNET_SERVER_receive_done (client, GNUNET_OK);
577 }
578
579
580 void zone_iteration_proc (void *cls,
581                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
582                          struct GNUNET_TIME_Absolute expire,
583                          const char *name,
584                          unsigned int rd_count,
585                          const struct GNUNET_NAMESTORE_RecordData *rd,
586                          const struct GNUNET_CRYPTO_RsaSignature *signature)
587 {
588   struct ZoneIterationResponseMessage zir_msg;
589   struct GNUNET_NAMESTORE_ZoneIteration * zi = cls;
590
591   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
592   zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
593   zir_msg.op_id = htonl(zi->op_id);
594   zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
595
596
597   GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
598 }
599
600 static void handle_iteration_start (void *cls,
601                           struct GNUNET_SERVER_Client * client,
602                           const struct GNUNET_MessageHeader * message)
603 {
604   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
605
606   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
607   struct GNUNET_NAMESTORE_Client *nc;
608   struct GNUNET_NAMESTORE_ZoneIteration *zi;
609
610   nc = client_lookup(client);
611   if (nc == NULL)
612   {
613     GNUNET_break_op (0);
614     GNUNET_SERVER_receive_done (client, GNUNET_OK);
615     return;
616   }
617
618   zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
619   zi->op_id = ntohl (zis_msg->op_id);
620   zi->offset = 0;
621   zi->client = nc;
622
623   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
624
625   GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, zi);
626   GNUNET_SERVER_receive_done (client, GNUNET_OK);
627 }
628
629 static void handle_iteration_stop (void *cls,
630                           struct GNUNET_SERVER_Client * client,
631                           const struct GNUNET_MessageHeader * message)
632 {
633   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
634 }
635
636 static void handle_iteration_next (void *cls,
637                           struct GNUNET_SERVER_Client * client,
638                           const struct GNUNET_MessageHeader * message)
639 {
640   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
641 }
642
643
644
645 /**
646  * Process template requests.
647  *
648  * @param cls closure
649  * @param server the initialized server
650  * @param cfg configuration to use
651  */
652 static void
653 run (void *cls, struct GNUNET_SERVER_Handle *server,
654      const struct GNUNET_CONFIGURATION_Handle *cfg)
655 {
656   char * database;
657
658   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
659
660   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
661     {&handle_start, NULL,
662      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
663     {&handle_lookup_name, NULL,
664      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
665     {&handle_record_put, NULL,
666     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
667     {&handle_record_create, NULL,
668      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
669     {&handle_record_remove, NULL,
670      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
671     {&handle_iteration_start, NULL,
672      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
673     {&handle_iteration_stop, NULL,
674      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, 0},
675     {&handle_iteration_next, NULL,
676      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
677     {NULL, NULL, 0, 0}
678   };
679
680   GSN_cfg = cfg;
681
682   /* Loading database plugin */
683   if (GNUNET_OK !=
684       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
685                                              &database))
686     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
687
688   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
689   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
690   if (GSN_database == NULL)
691     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
692         db_lib_name);
693   GNUNET_free (database);
694
695   /* Configuring server handles */
696   GNUNET_SERVER_add_handlers (server, handlers);
697   snc = GNUNET_SERVER_notification_context_create (server, 16);
698   GNUNET_SERVER_disconnect_notify (server,
699                                    &client_disconnect_notification,
700                                    NULL);
701
702   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
703                                 NULL);
704
705 }
706
707
708 /**
709  * The main function for the template service.
710  *
711  * @param argc number of arguments from the command line
712  * @param argv command line arguments
713  * @return 0 ok, 1 on error
714  */
715 int
716 main (int argc, char *const *argv)
717 {
718   return (GNUNET_OK ==
719           GNUNET_SERVICE_run (argc, argv, "namestore",
720                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
721 }
722
723 /* end of gnunet-service-namestore.c */