- cleanup dead code
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 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  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_namestore_plugin.h"
32 #include "gnunet_signatures.h"
33 #include "namestore.h"
34
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36
37
38 /**
39  * A namestore client
40  */
41 struct NamestoreClient;
42
43
44 /**
45  * A namestore iteration operation.
46  */
47 struct ZoneIteration
48 {
49   /**
50    * Next element in the DLL
51    */
52   struct ZoneIteration *next;
53
54   /**
55    * Previous element in the DLL
56    */
57   struct ZoneIteration *prev;
58
59   /**
60    * Namestore client which intiated this zone iteration
61    */
62   struct NamestoreClient *client;
63
64   /**
65    * Key of the zone we are iterating over.
66    */
67   struct GNUNET_CRYPTO_EccPrivateKey zone;
68
69   /**
70    * The operation id fot the zone iteration in the response for the client
71    */
72   uint32_t request_id;
73
74   /**
75    * Offset of the zone iteration used to address next result of the zone
76    * iteration in the store
77    *
78    * Initialy set to 0 in handle_iteration_start
79    * Incremented with by every call to handle_iteration_next
80    */
81   uint32_t offset;
82
83 };
84
85
86 /**
87  * A namestore client
88  */
89 struct NamestoreClient
90 {
91   /**
92    * Next element in the DLL
93    */
94   struct NamestoreClient *next;
95
96   /**
97    * Previous element in the DLL
98    */
99   struct NamestoreClient *prev;
100
101   /**
102    * The client
103    */
104   struct GNUNET_SERVER_Client *client;
105
106   /**
107    * Head of the DLL of
108    * Zone iteration operations in progress initiated by this client
109    */
110   struct ZoneIteration *op_head;
111
112   /**
113    * Tail of the DLL of
114    * Zone iteration operations in progress initiated by this client
115    */
116   struct ZoneIteration *op_tail;
117 };
118
119
120 /**
121  * A namestore monitor.
122  */
123 struct ZoneMonitor
124 {
125   /**
126    * Next element in the DLL
127    */
128   struct ZoneMonitor *next;
129
130   /**
131    * Previous element in the DLL
132    */
133   struct ZoneMonitor *prev;
134
135   /**
136    * Namestore client which intiated this zone monitor
137    */
138   struct NamestoreClient *nc;
139
140   /**
141    * Private key of the zone.
142    */
143   struct GNUNET_CRYPTO_EccPrivateKey zone;
144
145   /**
146    * The operation id fot the zone iteration in the response for the client
147    */
148   uint32_t request_id;
149
150   /**
151    * Task active during initial iteration.
152    */
153   GNUNET_SCHEDULER_TaskIdentifier task;
154
155   /**
156    * Offset of the zone iteration used to address next result of the zone
157    * iteration in the store
158    *
159    * Initialy set to 0.
160    * Incremented with by every call to #handle_iteration_next
161    */
162   uint32_t offset;
163
164 };
165
166
167 /**
168  * Configuration handle.
169  */
170 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
171
172 /**
173  * Database handle
174  */
175 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
176
177 /**
178  * Name of the database plugin
179  */
180 static char *db_lib_name;
181
182 /**
183  * Our notification context.
184  */
185 static struct GNUNET_SERVER_NotificationContext *snc;
186
187 /**
188  * Head of the Client DLL
189  */
190 static struct NamestoreClient *client_head;
191
192 /**
193  * Tail of the Client DLL
194  */
195 static struct NamestoreClient *client_tail;
196
197 /**
198  * First active zone monitor.
199  */
200 static struct ZoneMonitor *monitor_head;
201
202 /**
203  * Last active zone monitor.
204  */
205 static struct ZoneMonitor *monitor_tail;
206
207 /**
208  * Notification context shared by all monitors.
209  */
210 static struct GNUNET_SERVER_NotificationContext *monitor_nc;
211
212
213
214 /**
215  * Task run during shutdown.
216  *
217  * @param cls unused
218  * @param tc unused
219  */
220 static void
221 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 {
223   struct ZoneIteration *no;
224   struct NamestoreClient *nc;
225
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227               "Stopping namestore service\n");
228   if (NULL != snc)
229   {
230     GNUNET_SERVER_notification_context_destroy (snc);
231     snc = NULL;
232   }
233   while (NULL != (nc = client_head))
234   {
235     while (NULL != (no = nc->op_head))
236     {
237       GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
238       GNUNET_free (no);
239     }
240     GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
241     GNUNET_SERVER_client_set_user_context (nc->client, NULL);
242     GNUNET_free (nc);
243   }
244   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
245   GNUNET_free (db_lib_name);
246   db_lib_name = NULL;
247   if (NULL != monitor_nc)
248   {
249     GNUNET_SERVER_notification_context_destroy (monitor_nc);
250     monitor_nc = NULL;
251   }
252 }
253
254
255 /**
256  * Called whenever a client is disconnected.
257  * Frees our resources associated with that client.
258  *
259  * @param cls closure
260  * @param client identification of the client
261  */
262 static void
263 client_disconnect_notification (void *cls,
264                                 struct GNUNET_SERVER_Client *client)
265 {
266   struct ZoneIteration *no;
267   struct NamestoreClient *nc;
268   struct ZoneMonitor *zm;
269
270   if (NULL == client)
271     return;
272   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273               "Client %p disconnected\n",
274               client);
275   if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient)))
276     return;
277   while (NULL != (no = nc->op_head))
278   {
279     GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
280     GNUNET_free (no);
281   }
282   GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
283   GNUNET_free (nc);
284   for (zm = monitor_head; NULL != zm; zm = zm->next)
285   {
286     if (client == zm->nc->client)
287     {
288       GNUNET_CONTAINER_DLL_remove (monitor_head,
289                                    monitor_tail,
290                                    zm);
291       if (GNUNET_SCHEDULER_NO_TASK != zm->task)
292       {
293         GNUNET_SCHEDULER_cancel (zm->task);
294         zm->task = GNUNET_SCHEDULER_NO_TASK;
295       }
296       GNUNET_free (zm);
297       break;
298     }
299   }
300 }
301
302
303 /**
304  * Add a client to our list of active clients, if it is not yet
305  * in there.
306  *
307  * @param client client to add
308  * @return internal namestore client structure for this client
309  */
310 static struct NamestoreClient *
311 client_lookup (struct GNUNET_SERVER_Client *client)
312 {
313   struct NamestoreClient *nc;
314
315   nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient);
316   if (NULL != nc)
317     return nc;
318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319               "Client %p connected\n",
320               client);
321   nc = GNUNET_new (struct NamestoreClient);
322   nc->client = client;
323   GNUNET_SERVER_notification_context_add (snc, client);
324   GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
325   GNUNET_SERVER_client_set_user_context (client, nc);
326   return nc;
327 }
328
329
330 /**
331  * Context for name lookups passed from #handle_lookup_block to
332  * #handle_lookup_block_it as closure
333  */
334 struct LookupBlockContext
335 {
336   /**
337    * The client to send the response to
338    */
339   struct NamestoreClient *nc;
340
341   /**
342    * Operation id for the name lookup
343    */
344   uint32_t request_id;
345
346 };
347
348
349 /**
350  * A #GNUNET_NAMESTORE_BlockCallback for name lookups in #handle_lookup_block
351  *
352  * @param cls a `struct LookupNameContext *` with information about the request
353  * @param block the block
354  */
355 static void
356 handle_lookup_block_it (void *cls,
357                         const struct GNUNET_NAMESTORE_Block *block)
358 {
359   struct LookupBlockContext *lnc = cls;
360   struct LookupBlockResponseMessage *r;
361   size_t esize;
362
363   esize = ntohl (block->purpose.size)
364     - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
365     - sizeof (struct GNUNET_TIME_AbsoluteNBO);
366   r = GNUNET_malloc (sizeof (struct LookupBlockResponseMessage) + esize);
367   r->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
368   r->gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage) + esize);
369   r->gns_header.r_id = htonl (lnc->request_id);
370   r->expire = block->expiration_time;
371   r->signature = block->signature;
372   r->derived_key = block->derived_key;
373   memcpy (&r[1], &block[1], esize);
374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375               "Sending `%s' message with expiration time %s\n",
376               "NAMESTORE_LOOKUP_BLOCK_RESPONSE",
377               GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (r->expire)));
378   GNUNET_SERVER_notification_context_unicast (snc,
379                                               lnc->nc->client,
380                                               &r->gns_header.header,
381                                               GNUNET_NO);
382   GNUNET_free (r);
383 }
384
385
386 /**
387  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK message
388  *
389  * @param cls unused
390  * @param client client sending the message
391  * @param message message of type 'struct LookupNameMessage'
392  */
393 static void
394 handle_lookup_block (void *cls,
395                     struct GNUNET_SERVER_Client *client,
396                     const struct GNUNET_MessageHeader *message)
397 {
398   const struct LookupBlockMessage *ln_msg;
399   struct LookupBlockContext lnc;
400   struct NamestoreClient *nc;
401   struct LookupBlockResponseMessage zir_end;
402   int ret;
403
404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405               "Received `%s' message\n",
406               "NAMESTORE_LOOKUP_BLOCK");
407   nc = client_lookup(client);
408   ln_msg = (const struct LookupBlockMessage *) message;
409   lnc.request_id = ntohl (ln_msg->gns_header.r_id);
410   lnc.nc = nc;
411   if (GNUNET_SYSERR ==
412       (ret = GSN_database->lookup_block (GSN_database->cls,
413                                          &ln_msg->query,
414                                          &handle_lookup_block_it, &lnc)))
415   {
416     /* internal error (in database plugin); might be best to just hang up on
417        plugin rather than to signal that there are 'no' results, which
418        might also be false... */
419     GNUNET_break (0);
420     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
421     return;
422   }
423   if (0 == ret)
424   {
425     /* no records match at all, generate empty response */
426     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427                 "Sending empty `%s' message\n",
428                 "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
429     memset (&zir_end, 0, sizeof (zir_end));
430     zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
431     zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
432     zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
433     GNUNET_SERVER_notification_context_unicast (snc,
434                                                 client,
435                                                 &zir_end.gns_header.header,
436                                                 GNUNET_NO);
437
438   }
439   GNUNET_SERVER_receive_done (client, GNUNET_OK);
440 }
441
442
443 /**
444  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE message
445  *
446  * @param cls unused
447  * @param client client sending the message
448  * @param message message of type 'struct BlockCacheMessage'
449  */
450 static void
451 handle_block_cache (void *cls,
452                      struct GNUNET_SERVER_Client *client,
453                      const struct GNUNET_MessageHeader *message)
454 {
455   struct NamestoreClient *nc;
456   const struct BlockCacheMessage *rp_msg;
457   struct BlockCacheResponseMessage rpr_msg;
458   struct GNUNET_NAMESTORE_Block *block;
459   size_t esize;
460   int res;
461
462   nc = client_lookup (client);
463   if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
464   {
465     GNUNET_break (0);
466     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
467     return;
468   }
469   rp_msg = (const struct BlockCacheMessage *) message;
470   esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
471   block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) + esize);
472   block->signature = rp_msg->signature;
473   block->derived_key = rp_msg->derived_key;
474   block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
475                                sizeof (struct GNUNET_TIME_AbsoluteNBO) +
476                                esize);
477   block->expiration_time = rp_msg->expire;
478   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479               "Received `%s' message with expiration time %s\n",
480               "NAMESTORE_BLOCK_CACHE",
481               GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (block->expiration_time)));
482   memcpy (&block[1], &rp_msg[1], esize);
483   res = GSN_database->cache_block (GSN_database->cls,
484                                    block);
485   GNUNET_free (block);
486
487   rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE);
488   rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
489   rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
490   rpr_msg.op_result = htonl (res);
491   GNUNET_SERVER_notification_context_unicast (snc,
492                                               nc->client,
493                                               &rpr_msg.gns_header.header,
494                                               GNUNET_NO);
495   GNUNET_SERVER_receive_done (client, GNUNET_OK);
496 }
497
498
499 /**
500  * Generate a 'struct LookupNameResponseMessage' and send it to the
501  * given client using the given notification context.
502  *
503  * @param nc notification context to use
504  * @param client client to unicast to
505  * @param request_id request ID to use
506  * @param zone_key zone key of the zone
507  * @param name name
508  * @param rd_count number of records in @a rd
509  * @param rd array of records
510  */
511 static void
512 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,                     
513                       struct GNUNET_SERVER_Client *client,
514                       uint32_t request_id,
515                       const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
516                       const char *name,
517                       unsigned int rd_count,
518                       const struct GNUNET_NAMESTORE_RecordData *rd)
519 {
520   struct RecordResultMessage *zir_msg;
521   size_t name_len;
522   size_t rd_ser_len;
523   size_t msg_size;
524   char *name_tmp;
525   char *rd_ser;
526
527   name_len = strlen (name) + 1;
528   rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
529   msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
530
531   zir_msg = GNUNET_malloc (msg_size);
532   zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
533   zir_msg->gns_header.header.size = htons (msg_size);
534   zir_msg->gns_header.r_id = htonl (request_id);
535   zir_msg->name_len = htons (name_len);
536   zir_msg->rd_count = htons (rd_count);
537   zir_msg->rd_len = htons (rd_ser_len);
538   zir_msg->private_key = *zone_key;
539   name_tmp = (char *) &zir_msg[1];
540   memcpy (name_tmp, name, name_len);
541   rd_ser = &name_tmp[name_len];
542   GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
543   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544               "Sending `%s' message with %u records and size %u\n",
545               "RECORD_RESULT",
546               rd_count,
547               msg_size);
548   GNUNET_SERVER_notification_context_unicast (nc,
549                                               client,
550                                               &zir_msg->gns_header.header,
551                                               GNUNET_NO);
552   GNUNET_free (zir_msg);
553 }
554
555
556 /**
557  * We just touched the plaintext information about a name in our zone;
558  * refresh the corresponding (encrypted) block in the namestore.
559  *
560  * @param zone_key private key of the zone
561  * @param name label for the records
562  * @param rd_count number of records
563  * @param rd records stored under the given @a name
564  */
565 static void
566 refresh_block (const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
567                const char *name,
568                unsigned int rd_count,
569                const struct GNUNET_NAMESTORE_RecordData *rd)
570 {
571   struct GNUNET_NAMESTORE_Block *block;
572
573   if (0 == rd_count)
574     block = GNUNET_NAMESTORE_block_create (zone_key,
575                                            GNUNET_TIME_UNIT_ZERO_ABS,
576                                            name,
577                                            rd, rd_count);
578   else
579     block = GNUNET_NAMESTORE_block_create (zone_key,
580                                            GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
581                                                                                         rd),
582                                            name,
583                                            rd, rd_count);
584   if (GNUNET_OK !=
585       GSN_database->cache_block (GSN_database->cls,
586                                  block))
587   {
588     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589                 _("Failed to cache encrypted block of my own zone!\n"));
590   }
591   GNUNET_free (block);
592 }
593
594
595 /**
596  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
597  *
598  * @param cls unused
599  * @param client client sending the message
600  * @param message message of type 'struct RecordCreateMessage'
601  */
602 static void
603 handle_record_store (void *cls,
604                       struct GNUNET_SERVER_Client *client,
605                       const struct GNUNET_MessageHeader *message)
606 {
607   struct NamestoreClient *nc;
608   const struct RecordStoreMessage *rp_msg;
609   struct RecordStoreResponseMessage rcr_msg;
610   size_t name_len;
611   size_t msg_size;
612   size_t msg_size_exp;
613   size_t rd_ser_len;
614   uint32_t rid;
615   const char *name_tmp;
616   char *conv_name;
617   const char *rd_ser;
618   unsigned int rd_count;
619   int res;
620   struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
621   struct ZoneMonitor *zm;
622
623   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624               "Received `%s' message\n",
625               "NAMESTORE_RECORD_STORE");
626   if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
627   {
628     GNUNET_break (0);
629     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
630     return;
631   }
632   nc = client_lookup (client);
633   rp_msg = (const struct RecordStoreMessage *) message;
634   rid = ntohl (rp_msg->gns_header.r_id);
635   name_len = ntohs (rp_msg->name_len);
636   msg_size = ntohs (message->size);
637   rd_count = ntohs (rp_msg->rd_count);
638   rd_ser_len = ntohs (rp_msg->rd_len);
639   GNUNET_break (0 == ntohs (rp_msg->reserved));
640   msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
641   if (msg_size != msg_size_exp)
642   {
643     GNUNET_break (0);
644     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
645     return;
646   }
647   if ((0 == name_len) || (name_len > MAX_NAME_LEN))
648   {
649     GNUNET_break (0);
650     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
651     return;
652   }
653   name_tmp = (const char *) &rp_msg[1];
654   rd_ser = &name_tmp[name_len];
655   if ('\0' != name_tmp[name_len -1])
656   {
657     GNUNET_break (0);
658     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
659     return;
660   }
661   {
662     struct GNUNET_NAMESTORE_RecordData rd[rd_count];
663
664     if (GNUNET_OK !=
665         GNUNET_NAMESTORE_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
666     {
667       GNUNET_break (0);
668       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
669       return;
670     }
671
672     /* Extracting and converting private key */
673     GNUNET_CRYPTO_ecc_key_get_public_for_signature (&rp_msg->private_key,
674                                       &pubkey);
675     conv_name = GNUNET_NAMESTORE_normalize_string (name_tmp);
676     if (NULL == conv_name)
677     {
678       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
679                   "Error converting name `%s'\n", name_tmp);
680       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
681       return;
682     }
683     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684                 "Creating %u records for name `%s' in zone `%s'\n",
685                 (unsigned int) rd_count,
686                 conv_name,
687                 GNUNET_NAMESTORE_z2s (&pubkey));
688
689     if ( (0 == rd_count) &&
690          (GNUNET_NO ==
691           GSN_database->iterate_records (GSN_database->cls,
692                                          &rp_msg->private_key, 0, NULL, 0)) )
693     {
694       /* This name does not exist, so cannot be removed */
695       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696                   "Name `%s' does not exist, no deletion required\n",
697                   conv_name);
698       res = GNUNET_NO;
699     }
700     else
701     {
702       res = GSN_database->store_records (GSN_database->cls,
703                                          &rp_msg->private_key,
704                                          conv_name,                             
705                                          rd_count, rd);
706       if (GNUNET_OK == res)
707       {
708         refresh_block (&rp_msg->private_key,
709                        conv_name,
710                        rd_count, rd);
711
712         for (zm = monitor_head; NULL != zm; zm = zm->next)
713           if (0 == memcmp (&rp_msg->private_key, &zm->zone,
714                            sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
715             send_lookup_response (monitor_nc,
716                                   zm->nc->client,
717                                   zm->request_id,
718                                   &rp_msg->private_key,
719                                   conv_name,
720                                   rd_count, rd);
721       }
722       GNUNET_free (conv_name);
723     }
724   }
725
726   /* Send response */
727   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728               "Sending `%s' message\n",
729               "RECORD_STORE_RESPONSE");
730   rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
731   rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
732   rcr_msg.gns_header.r_id = htonl (rid);
733   rcr_msg.op_result = htonl (res);
734   GNUNET_SERVER_notification_context_unicast (snc, nc->client,
735                                               &rcr_msg.gns_header.header,
736                                               GNUNET_NO);
737   GNUNET_SERVER_receive_done (client, GNUNET_OK);
738 }
739
740
741 /**
742  * Context for record remove operations passed from #handle_zone_to_name to
743  * #handle_zone_to_name_it as closure
744  */
745 struct ZoneToNameCtx
746 {
747   /**
748    * Namestore client
749    */
750   struct NamestoreClient *nc;
751
752   /**
753    * Request id (to be used in the response to the client).
754    */
755   uint32_t rid;
756
757   /**
758    * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error.  Note that
759    * not finding a name for the zone still counts as a 'success' here,
760    * as this field is about the success of executing the IPC protocol.
761    */
762   int success;
763 };
764
765
766 /**
767  * Zone to name iterator
768  *
769  * @param cls struct ZoneToNameCtx *
770  * @param zone_key the zone key
771  * @param name name
772  * @param rd_count number of records in @a rd
773  * @param rd record data
774  */
775 static void
776 handle_zone_to_name_it (void *cls,
777                         const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
778                         const char *name,
779                         unsigned int rd_count,
780                         const struct GNUNET_NAMESTORE_RecordData *rd)
781 {
782   struct ZoneToNameCtx *ztn_ctx = cls;
783   struct ZoneToNameResponseMessage *ztnr_msg;
784   int16_t res;
785   size_t name_len;
786   size_t rd_ser_len;
787   size_t msg_size;
788   char *name_tmp;
789   char *rd_tmp;
790
791   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792               "Found result for zone-to-name lookup: `%s'\n",
793               name);
794   res = GNUNET_YES;
795   name_len = (NULL == name) ? 0 : strlen (name) + 1;
796   rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
797   msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
798   if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
799   {
800     GNUNET_break (0);
801     ztn_ctx->success = GNUNET_SYSERR;
802     return;
803   }
804   ztnr_msg = GNUNET_malloc (msg_size);
805   ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
806   ztnr_msg->gns_header.header.size = htons (msg_size);
807   ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
808   ztnr_msg->res = htons (res);
809   ztnr_msg->rd_len = htons (rd_ser_len);
810   ztnr_msg->rd_count = htons (rd_count);
811   ztnr_msg->name_len = htons (name_len);
812   ztnr_msg->zone = *zone_key;
813   name_tmp = (char *) &ztnr_msg[1];
814   if (NULL != name)
815     memcpy (name_tmp, name, name_len);
816   rd_tmp = &name_tmp[name_len];
817   GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
818   ztn_ctx->success = GNUNET_OK;
819   GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
820                                               &ztnr_msg->gns_header.header,
821                                               GNUNET_NO);
822   GNUNET_free (ztnr_msg);
823 }
824
825
826 /**
827  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
828  *
829  * @param cls unused
830  * @param client client sending the message
831  * @param message message of type 'struct ZoneToNameMessage'
832  */
833 static void
834 handle_zone_to_name (void *cls,
835                      struct GNUNET_SERVER_Client *client,
836                      const struct GNUNET_MessageHeader *message)
837 {
838   struct NamestoreClient *nc;
839   const struct ZoneToNameMessage *ztn_msg;
840   struct ZoneToNameCtx ztn_ctx;
841   struct ZoneToNameResponseMessage ztnr_msg;
842
843   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844               "Received `%s' message\n",
845               "ZONE_TO_NAME");
846   ztn_msg = (const struct ZoneToNameMessage *) message;
847   nc = client_lookup (client);
848   ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
849   ztn_ctx.nc = nc;
850   ztn_ctx.success = GNUNET_NO;
851   if (GNUNET_SYSERR ==
852       GSN_database->zone_to_name (GSN_database->cls,
853                                   &ztn_msg->zone,
854                                   &ztn_msg->value_zone,
855                                   &handle_zone_to_name_it, &ztn_ctx))
856   {
857     /* internal error, hang up instead of signalling something
858        that might be wrong */
859     GNUNET_break (0);
860     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
861     return;
862   }
863   if (GNUNET_NO == ztn_ctx.success)
864   {
865     /* no result found, send empty response */
866     memset (&ztnr_msg, 0, sizeof (ztnr_msg));
867     ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
868     ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
869     ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
870     ztnr_msg.res = htons (GNUNET_NO);
871     GNUNET_SERVER_notification_context_unicast (snc,
872                                                 client,
873                                                 &ztnr_msg.gns_header.header,
874                                                 GNUNET_NO);
875   }
876   GNUNET_SERVER_receive_done (client, GNUNET_OK);
877 }
878
879
880 /**
881  * Zone iteration processor result
882  */
883 enum ZoneIterationResult
884 {
885   /**
886    * Iteration start.
887    */
888   IT_START = 0,
889
890   /**
891    * Found records,
892    * Continue to iterate with next iteration_next call
893    */
894   IT_SUCCESS_MORE_AVAILABLE = 1,
895
896   /**
897    * Iteration complete
898    */
899   IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
900 };
901
902
903 /**
904  * Context for record remove operations passed from
905  * #run_zone_iteration_round to #zone_iteraterate_proc as closure
906  */
907 struct ZoneIterationProcResult
908 {
909   /**
910    * The zone iteration handle
911    */
912   struct ZoneIteration *zi;
913
914   /**
915    * Iteration result: iteration done?
916    * #IT_SUCCESS_MORE_AVAILABLE:  if there may be more results overall but
917    * we got one for now and have sent it to the client
918    * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
919    * #IT_START: if we are still trying to find a result.
920    */
921   int res_iteration_finished;
922
923 };
924
925
926 /**
927  * Process results for zone iteration from database
928  *
929  * @param cls struct ZoneIterationProcResult *proc
930  * @param zone_key the zone key
931  * @param name name
932  * @param rd_count number of records for this name
933  * @param rd record data
934  */
935 static void
936 zone_iteraterate_proc (void *cls,
937                        const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
938                        const char *name,
939                        unsigned int rd_count,
940                        const struct GNUNET_NAMESTORE_RecordData *rd)
941 {
942   struct ZoneIterationProcResult *proc = cls;
943   unsigned int i;
944   int do_refresh_block;
945
946   if ((NULL == zone_key) && (NULL == name))
947   {
948     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949                 "Iteration done\n");
950     proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
951     return;
952   }
953   if ((NULL == zone_key) || (NULL == name))
954   {
955     /* what is this!? should never happen */
956     proc->res_iteration_finished = IT_START;
957     GNUNET_break (0);
958     return;
959   }
960   proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
961   send_lookup_response (snc,
962                         proc->zi->client->client,
963                         proc->zi->request_id,
964                         zone_key,
965                         name,
966                         rd_count,
967                         rd);
968   do_refresh_block = GNUNET_NO;
969   for (i=0;i<rd_count;i++)
970     if(  (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) &&
971          (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING)) )
972     {
973       do_refresh_block = GNUNET_YES;
974       break;
975     }
976   if (GNUNET_YES == do_refresh_block)
977     refresh_block (zone_key,
978                    name,
979                    rd_count,
980                    rd);
981
982 }
983
984
985 /**
986  * Perform the next round of the zone iteration.
987  *
988  * @param zi zone iterator to process
989  */
990 static void
991 run_zone_iteration_round (struct ZoneIteration *zi)
992 {
993   static struct GNUNET_CRYPTO_EccPrivateKey zero;
994   struct ZoneIterationProcResult proc;
995   struct RecordResultMessage rrm;
996   int ret;
997
998   memset (&proc, 0, sizeof (proc));
999   proc.zi = zi;
1000   proc.res_iteration_finished = IT_START;
1001   while (IT_START == proc.res_iteration_finished)
1002   {
1003     if (GNUNET_SYSERR ==
1004         (ret = GSN_database->iterate_records (GSN_database->cls,
1005                                               (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1006                                               ? NULL
1007                                               : &zi->zone,
1008                                               zi->offset,
1009                                               &zone_iteraterate_proc, &proc)))
1010     {
1011       GNUNET_break (0);
1012       break;
1013     }
1014     if (GNUNET_NO == ret)
1015       proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1016     zi->offset++;
1017   }
1018   if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1019   {
1020     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021                 "More results available\n");
1022     return; /* more results later */
1023   }
1024   /* send empty response to indicate end of list */
1025   memset (&rrm, 0, sizeof (rrm));
1026   rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1027   rrm.gns_header.header.size = htons (sizeof (rrm));
1028   rrm.gns_header.r_id = htonl (zi->request_id);
1029   GNUNET_SERVER_notification_context_unicast (snc,
1030                                               zi->client->client,
1031                                               &rrm.gns_header.header,
1032                                               GNUNET_NO);
1033   GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
1034                                zi->client->op_tail,
1035                                zi);
1036   GNUNET_free (zi);
1037 }
1038
1039
1040 /**
1041  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1042  *
1043  * @param cls unused
1044  * @param client the client sending the message
1045  * @param message message of type 'struct ZoneIterationStartMessage'
1046  */
1047 static void
1048 handle_iteration_start (void *cls,
1049                         struct GNUNET_SERVER_Client *client,
1050                         const struct GNUNET_MessageHeader *message)
1051 {
1052   const struct ZoneIterationStartMessage *zis_msg;
1053   struct NamestoreClient *nc;
1054   struct ZoneIteration *zi;
1055
1056   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1057   if (NULL == (nc = client_lookup (client)))
1058   {
1059     GNUNET_break (0);
1060     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1061     return;
1062   }
1063   zis_msg = (const struct ZoneIterationStartMessage *) message;
1064   zi = GNUNET_new (struct ZoneIteration);
1065   zi->request_id = ntohl (zis_msg->gns_header.r_id);
1066   zi->offset = 0;
1067   zi->client = nc;
1068   zi->zone = zis_msg->zone;
1069   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1070   run_zone_iteration_round (zi);
1071   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1072 }
1073
1074
1075 /**
1076  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1077  *
1078  * @param cls unused
1079  * @param client GNUNET_SERVER_Client sending the message
1080  * @param message message of type 'struct ZoneIterationStopMessage'
1081  */
1082 static void
1083 handle_iteration_stop (void *cls,
1084                        struct GNUNET_SERVER_Client *client,
1085                        const struct GNUNET_MessageHeader *message)
1086 {
1087   struct NamestoreClient *nc;
1088   struct ZoneIteration *zi;
1089   const struct ZoneIterationStopMessage *zis_msg;
1090   uint32_t rid;
1091
1092   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093               "Received `%s' message\n",
1094               "ZONE_ITERATION_STOP");
1095   if (NULL == (nc = client_lookup(client)))
1096   {
1097     GNUNET_break (0);
1098     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1099     return;
1100   }
1101   zis_msg = (const struct ZoneIterationStopMessage *) message;
1102   rid = ntohl (zis_msg->gns_header.r_id);
1103   for (zi = nc->op_head; NULL != zi; zi = zi->next)
1104     if (zi->request_id == rid)
1105       break;
1106   if (NULL == zi)
1107   {
1108     GNUNET_break (0);
1109     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1110     return;
1111   }
1112   GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1113   GNUNET_free (zi);
1114   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1115 }
1116
1117
1118 /**
1119  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1120  *
1121  * @param cls unused
1122  * @param client GNUNET_SERVER_Client sending the message
1123  * @param message message of type 'struct ZoneIterationNextMessage'
1124  */
1125 static void
1126 handle_iteration_next (void *cls,
1127                        struct GNUNET_SERVER_Client *client,
1128                        const struct GNUNET_MessageHeader *message)
1129 {
1130   struct NamestoreClient *nc;
1131   struct ZoneIteration *zi;
1132   const struct ZoneIterationNextMessage *zis_msg;
1133   uint32_t rid;
1134
1135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1136   if (NULL == (nc = client_lookup(client)))
1137   {
1138     GNUNET_break (0);
1139     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1140     return;
1141   }
1142   zis_msg = (const struct ZoneIterationNextMessage *) message;
1143   rid = ntohl (zis_msg->gns_header.r_id);
1144   for (zi = nc->op_head; NULL != zi; zi = zi->next)
1145     if (zi->request_id == rid)
1146       break;
1147   if (NULL == zi)
1148   {
1149     GNUNET_break (0);
1150     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1151     return;
1152   }
1153   run_zone_iteration_round (zi);
1154   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1155 }
1156
1157
1158 /**
1159  * Send 'sync' message to zone monitor, we're now in sync.
1160  *
1161  * @param zm monitor that is now in sync
1162  */
1163 static void
1164 monitor_sync (struct ZoneMonitor *zm)
1165 {
1166   struct GNUNET_MessageHeader sync;
1167
1168   sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1169   sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1170   GNUNET_SERVER_notification_context_unicast (monitor_nc,
1171                                               zm->nc->client,
1172                                               &sync,
1173                                               GNUNET_NO);
1174 }
1175
1176
1177 /**
1178  * Obtain the next datum during the zone monitor's zone intiial iteration.
1179  *
1180  * @param cls zone monitor that does its initial iteration
1181  * @param tc scheduler context
1182  */
1183 static void
1184 monitor_next (void *cls,
1185               const struct GNUNET_SCHEDULER_TaskContext *tc);
1186
1187
1188 /**
1189  * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1190  *
1191  * @param cls a 'struct ZoneMonitor *' with information about the monitor
1192  * @param zone_key zone key of the zone
1193  * @param name name
1194  * @param rd_count number of records in @a rd
1195  * @param rd array of records
1196  */
1197 static void
1198 monitor_iterate_cb (void *cls,
1199                     const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1200                     const char *name,
1201                     unsigned int rd_count,
1202                     const struct GNUNET_NAMESTORE_RecordData *rd)
1203 {
1204   struct ZoneMonitor *zm = cls;
1205
1206   if (NULL == name)
1207   {
1208     /* finished with iteration */
1209     monitor_sync (zm);
1210     return;
1211   }
1212   send_lookup_response (monitor_nc,
1213                         zm->nc->client,
1214                         zm->request_id,
1215                         zone_key,
1216                         name,
1217                         rd_count,
1218                         rd);
1219   zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1220 }
1221
1222
1223 /**
1224  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1225  *
1226  * @param cls unused
1227  * @param client GNUNET_SERVER_Client sending the message
1228  * @param message message of type 'struct ZoneMonitorStartMessage'
1229  */
1230 static void
1231 handle_monitor_start (void *cls,
1232                       struct GNUNET_SERVER_Client *client,
1233                       const struct GNUNET_MessageHeader *message)
1234 {
1235   const struct ZoneMonitorStartMessage *zis_msg;
1236   struct ZoneMonitor *zm;
1237
1238   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239               "Received `%s' message\n",
1240               "ZONE_MONITOR_START");
1241   zis_msg = (const struct ZoneMonitorStartMessage *) message;
1242   zm = GNUNET_new (struct ZoneMonitor);
1243   zm->request_id = ntohl (zis_msg->gns_header.r_id);
1244   zm->offset = 0;
1245   zm->nc = client_lookup (client);
1246   zm->zone = zis_msg->zone;
1247   GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1248   GNUNET_SERVER_client_mark_monitor (client);
1249   GNUNET_SERVER_disable_receive_done_warning (client);
1250   GNUNET_SERVER_notification_context_add (monitor_nc,
1251                                           client);
1252   zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1253 }
1254
1255
1256 /**
1257  * Obtain the next datum during the zone monitor's zone intiial iteration.
1258  *
1259  * @param cls zone monitor that does its initial iteration
1260  * @param tc scheduler context
1261  */
1262 static void
1263 monitor_next (void *cls,
1264               const struct GNUNET_SCHEDULER_TaskContext *tc)
1265 {
1266   struct ZoneMonitor *zm = cls;
1267   int ret;
1268
1269   zm->task = GNUNET_SCHEDULER_NO_TASK;
1270   ret = GSN_database->iterate_records (GSN_database->cls,
1271                                        &zm->zone,
1272                                        zm->offset++,
1273                                        &monitor_iterate_cb, zm);
1274   if (GNUNET_SYSERR == ret)
1275   {
1276     GNUNET_SERVER_client_disconnect (zm->nc->client);
1277     return;
1278   }
1279   if (GNUNET_NO == ret)
1280   {
1281     /* empty zone */
1282     monitor_sync (zm);
1283     return;
1284   }
1285 }
1286
1287
1288 /**
1289  * Process namestore requests.
1290  *
1291  * @param cls closure
1292  * @param server the initialized server
1293  * @param cfg configuration to use
1294  */
1295 static void
1296 run (void *cls, struct GNUNET_SERVER_Handle *server,
1297      const struct GNUNET_CONFIGURATION_Handle *cfg)
1298 {
1299   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1300     {&handle_lookup_block, NULL,
1301      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1302     {&handle_block_cache, NULL,
1303     GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1304     {&handle_record_store, NULL,
1305      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1306     {&handle_zone_to_name, NULL,
1307      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1308     {&handle_iteration_start, NULL,
1309      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1310     {&handle_iteration_next, NULL,
1311      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1312     {&handle_iteration_stop, NULL,
1313      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1314     {&handle_monitor_start, NULL,
1315      GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1316     {NULL, NULL, 0, 0}
1317   };
1318   char *database;
1319
1320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1321   GSN_cfg = cfg;
1322   monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1323
1324   /* Loading database plugin */
1325   if (GNUNET_OK !=
1326       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1327                                              &database))
1328     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1329
1330   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1331   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1332   GNUNET_free (database);
1333   if (NULL == GSN_database)
1334   {
1335     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1336                 "Could not load database backend `%s'\n",
1337                 db_lib_name);
1338     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1339     return;
1340   }
1341
1342   /* Configuring server handles */
1343   GNUNET_SERVER_add_handlers (server, handlers);
1344   snc = GNUNET_SERVER_notification_context_create (server, 16);
1345   GNUNET_SERVER_disconnect_notify (server,
1346                                    &client_disconnect_notification,
1347                                    NULL);
1348   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1349                                 NULL);
1350 }
1351
1352
1353 /**
1354  * The main function for the template service.
1355  *
1356  * @param argc number of arguments from the command line
1357  * @param argv command line arguments
1358  * @return 0 ok, 1 on error
1359  */
1360 int
1361 main (int argc, char *const *argv)
1362 {
1363   return (GNUNET_OK ==
1364           GNUNET_SERVICE_run (argc, argv, "namestore",
1365                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1366 }
1367
1368 /* end of gnunet-service-namestore.c */
1369