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