-add logic to print expiration times for records
[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\n", 
376               "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
377   GNUNET_SERVER_notification_context_unicast (snc, 
378                                               lnc->nc->client, 
379                                               &r->gns_header.header, 
380                                               GNUNET_NO);
381   GNUNET_free (r);
382 }
383
384
385 /**
386  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK message
387  *
388  * @param cls unused
389  * @param client client sending the message
390  * @param message message of type 'struct LookupNameMessage'
391  */
392 static void
393 handle_lookup_block (void *cls,
394                     struct GNUNET_SERVER_Client *client,
395                     const struct GNUNET_MessageHeader *message)
396 {
397   const struct LookupBlockMessage *ln_msg;
398   struct LookupBlockContext lnc;
399   struct NamestoreClient *nc;
400   struct LookupBlockResponseMessage zir_end;
401   int ret;
402
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
404               "Received `%s' message\n", 
405               "NAMESTORE_LOOKUP_BLOCK");
406   nc = client_lookup(client);
407   ln_msg = (const struct LookupBlockMessage *) message;
408   lnc.request_id = ntohl (ln_msg->gns_header.r_id);
409   lnc.nc = nc;
410   if (GNUNET_SYSERR ==
411       (ret = GSN_database->lookup_block (GSN_database->cls, 
412                                          &ln_msg->query,
413                                          &handle_lookup_block_it, &lnc)))
414   {
415     /* internal error (in database plugin); might be best to just hang up on
416        plugin rather than to signal that there are 'no' results, which 
417        might also be false... */
418     GNUNET_break (0); 
419     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
420     return;
421   }  
422   if (0 == ret)
423   {
424     /* no records match at all, generate empty response */
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
426                 "Sending empty `%s' message\n", 
427                 "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
428     memset (&zir_end, 0, sizeof (zir_end));
429     zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
430     zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
431     zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
432     GNUNET_SERVER_notification_context_unicast (snc, 
433                                                 client, 
434                                                 &zir_end.gns_header.header, 
435                                                 GNUNET_NO);
436
437   }
438   GNUNET_SERVER_receive_done (client, GNUNET_OK);
439 }
440
441
442 /**
443  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE message
444  *
445  * @param cls unused
446  * @param client client sending the message
447  * @param message message of type 'struct BlockCacheMessage'
448  */
449 static void
450 handle_block_cache (void *cls,
451                      struct GNUNET_SERVER_Client *client,
452                      const struct GNUNET_MessageHeader *message)
453 {
454   struct NamestoreClient *nc;
455   const struct BlockCacheMessage *rp_msg;  
456   struct BlockCacheResponseMessage rpr_msg;
457   struct GNUNET_NAMESTORE_Block *block;
458   size_t esize;
459   int res;
460
461   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
462               "Received `%s' message\n",
463               "NAMESTORE_BLOCK_CACHE");
464   nc = client_lookup (client);
465   if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
466   {
467     GNUNET_break (0);
468     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
469     return;
470   }
471   rp_msg = (const struct BlockCacheMessage *) message;
472   esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
473   block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) + esize);
474   block->signature = rp_msg->signature;
475   block->derived_key = rp_msg->derived_key;
476   block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
477                                sizeof (struct GNUNET_TIME_AbsoluteNBO) +
478                                esize);
479   block->expiration_time = rp_msg->expire;
480   memcpy (&block[1], &rp_msg[1], esize);
481   res = GSN_database->cache_block (GSN_database->cls,
482                                    block);
483   GNUNET_free (block);
484
485   rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE);
486   rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
487   rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
488   rpr_msg.op_result = htonl (res);
489   GNUNET_SERVER_notification_context_unicast (snc, 
490                                               nc->client, 
491                                               &rpr_msg.gns_header.header, 
492                                               GNUNET_NO);
493   GNUNET_SERVER_receive_done (client, GNUNET_OK);
494 }
495
496
497 /**
498  * Generate a 'struct LookupNameResponseMessage' and send it to the
499  * given client using the given notification context.
500  *
501  * @param nc notification context to use
502  * @param client client to unicast to
503  * @param request_id request ID to use
504  * @param zone_key zone key of the zone
505  * @param name name
506  * @param rd_count number of records in @a rd
507  * @param rd array of records
508  */
509 static void
510 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,                     
511                       struct GNUNET_SERVER_Client *client,
512                       uint32_t request_id,
513                       const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
514                       const char *name,
515                       unsigned int rd_count,
516                       const struct GNUNET_NAMESTORE_RecordData *rd)
517 {
518   struct RecordResultMessage *zir_msg;
519   size_t name_len;
520   size_t rd_ser_len;
521   size_t msg_size;
522   char *name_tmp;
523   char *rd_ser;
524
525   name_len = strlen (name) + 1;
526   rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);  
527   msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
528
529   zir_msg = GNUNET_malloc (msg_size);
530   zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
531   zir_msg->gns_header.header.size = htons (msg_size);
532   zir_msg->gns_header.r_id = htonl (request_id);
533   zir_msg->name_len = htons (name_len);
534   zir_msg->rd_count = htons (rd_count);
535   zir_msg->rd_len = htons (rd_ser_len);
536   zir_msg->private_key = *zone_key;
537   name_tmp = (char *) &zir_msg[1];
538   memcpy (name_tmp, name, name_len);
539   rd_ser = &name_tmp[name_len];
540   GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
542               "Sending `%s' message with %u records and size %u\n", 
543               "RECORD_RESULT",
544               rd_count,
545               msg_size);
546   GNUNET_SERVER_notification_context_unicast (nc,
547                                               client, 
548                                               &zir_msg->gns_header.header,
549                                               GNUNET_NO);
550   GNUNET_free (zir_msg);
551 }
552
553
554 /**
555  * We just touched the plaintext information about a name in our zone;
556  * refresh the corresponding (encrypted) block in the namestore.
557  *
558  * @param zone_key private key of the zone
559  * @param name label for the records
560  * @param rd_count number of records
561  * @param rd records stored under the given @a name
562  */
563 static void
564 refresh_block (const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
565                const char *name,
566                unsigned int rd_count,
567                const struct GNUNET_NAMESTORE_RecordData *rd)
568 {
569   struct GNUNET_NAMESTORE_Block *block;
570   
571   if (0 == rd_count)
572     block = GNUNET_NAMESTORE_block_create (zone_key,
573                                            GNUNET_TIME_UNIT_ZERO_ABS,
574                                            name,
575                                            rd, rd_count);
576   else
577     block = GNUNET_NAMESTORE_block_create (zone_key,
578                                            GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
579                                                                                         rd),
580                                            name,
581                                            rd, rd_count);
582   if (GNUNET_OK !=
583       GSN_database->cache_block (GSN_database->cls,
584                                  block))
585   {
586     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587                 _("Failed to cache encrypted block of my own zone!\n"));
588   }
589   GNUNET_free (block);
590 }    
591
592
593 /**
594  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
595  *
596  * @param cls unused
597  * @param client client sending the message
598  * @param message message of type 'struct RecordCreateMessage'
599  */
600 static void
601 handle_record_store (void *cls,
602                       struct GNUNET_SERVER_Client *client,
603                       const struct GNUNET_MessageHeader *message)
604 {
605   struct NamestoreClient *nc;
606   const struct RecordStoreMessage *rp_msg;
607   struct RecordStoreResponseMessage rcr_msg;
608   size_t name_len;
609   size_t msg_size;
610   size_t msg_size_exp;
611   size_t rd_ser_len;
612   uint32_t rid;
613   const char *name_tmp;
614   char *conv_name;
615   const char *rd_ser;
616   unsigned int rd_count;
617   int res;
618   struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
619   struct ZoneMonitor *zm;
620
621   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
622               "Received `%s' message\n", 
623               "NAMESTORE_RECORD_STORE");
624   if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
625   {
626     GNUNET_break (0);
627     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
628     return;
629   }
630   nc = client_lookup (client);
631   rp_msg = (const struct RecordStoreMessage *) message;
632   rid = ntohl (rp_msg->gns_header.r_id);
633   name_len = ntohs (rp_msg->name_len);
634   msg_size = ntohs (message->size);
635   rd_count = ntohs (rp_msg->rd_count);
636   rd_ser_len = ntohs (rp_msg->rd_len);
637   GNUNET_break (0 == ntohs (rp_msg->reserved));
638   msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
639   if (msg_size != msg_size_exp)
640   {
641     GNUNET_break (0);
642     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
643     return;
644   }
645   if ((0 == name_len) || (name_len > MAX_NAME_LEN))
646   {
647     GNUNET_break (0);
648     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
649     return;
650   }
651   name_tmp = (const char *) &rp_msg[1];
652   rd_ser = &name_tmp[name_len];
653   if ('\0' != name_tmp[name_len -1])
654   {
655     GNUNET_break (0);
656     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
657     return;
658   }
659   {
660     struct GNUNET_NAMESTORE_RecordData rd[rd_count];
661
662     if (GNUNET_OK !=
663         GNUNET_NAMESTORE_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
664     {
665       GNUNET_break (0);
666       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
667       return;
668     }
669
670     /* Extracting and converting private key */
671     GNUNET_CRYPTO_ecc_key_get_public_for_signature (&rp_msg->private_key,
672                                       &pubkey);
673     conv_name = GNUNET_NAMESTORE_normalize_string (name_tmp);
674     if (NULL == conv_name)
675     {
676       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677                   "Error converting name `%s'\n", name_tmp);
678       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
679       return;
680     }
681     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682                 "Creating %u records for name `%s' in zone `%s'\n",
683                 (unsigned int) rd_count,
684                 conv_name,
685                 GNUNET_NAMESTORE_z2s (&pubkey));
686
687     if ( (0 == rd_count) && 
688          (GNUNET_NO == 
689           GSN_database->iterate_records (GSN_database->cls, 
690                                          &rp_msg->private_key, 0, NULL, 0)) )
691     {
692       /* This name does not exist, so cannot be removed */
693       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694                   "Name `%s' does not exist, no deletion required\n",
695                   conv_name);
696       res = GNUNET_NO;
697     }
698     else
699     {
700       res = GSN_database->store_records (GSN_database->cls,
701                                          &rp_msg->private_key,
702                                          conv_name,                                    
703                                          rd_count, rd);
704       if (GNUNET_OK == res)
705       {
706         refresh_block (&rp_msg->private_key,
707                        conv_name,
708                        rd_count, rd);
709   
710         for (zm = monitor_head; NULL != zm; zm = zm->next)    
711           if (0 == memcmp (&rp_msg->private_key, &zm->zone,
712                            sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
713             send_lookup_response (monitor_nc,
714                                   zm->nc->client,
715                                   zm->request_id,
716                                   &rp_msg->private_key,
717                                   conv_name,
718                                   rd_count, rd);
719       }
720       GNUNET_free (conv_name);
721     }
722   }
723   
724   /* Send response */
725   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
726               "Sending `%s' message\n", 
727               "RECORD_STORE_RESPONSE");
728   rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
729   rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
730   rcr_msg.gns_header.r_id = htonl (rid);
731   rcr_msg.op_result = htonl (res);
732   GNUNET_SERVER_notification_context_unicast (snc, nc->client,
733                                               &rcr_msg.gns_header.header,
734                                               GNUNET_NO);
735   GNUNET_SERVER_receive_done (client, GNUNET_OK);
736 }
737
738
739 /**
740  * Context for record remove operations passed from #handle_zone_to_name to
741  * #handle_zone_to_name_it as closure
742  */
743 struct ZoneToNameCtx
744 {
745   /**
746    * Namestore client
747    */
748   struct NamestoreClient *nc;
749
750   /**
751    * Request id (to be used in the response to the client).
752    */
753   uint32_t rid;
754
755   /**
756    * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error.  Note that
757    * not finding a name for the zone still counts as a 'success' here,
758    * as this field is about the success of executing the IPC protocol.
759    */
760   int success;
761 };
762
763
764 /**
765  * Zone to name iterator
766  *
767  * @param cls struct ZoneToNameCtx *
768  * @param zone_key the zone key
769  * @param name name
770  * @param rd_count number of records in @a rd
771  * @param rd record data
772  */
773 static void
774 handle_zone_to_name_it (void *cls,
775                         const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
776                         const char *name,
777                         unsigned int rd_count,
778                         const struct GNUNET_NAMESTORE_RecordData *rd)
779 {
780   struct ZoneToNameCtx *ztn_ctx = cls;
781   struct ZoneToNameResponseMessage *ztnr_msg;
782   int16_t res;
783   size_t name_len;
784   size_t rd_ser_len;
785   size_t msg_size;
786   char *name_tmp;
787   char *rd_tmp;
788
789   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
790               "Found result for zone-to-name lookup: `%s'\n", 
791               name);
792   res = GNUNET_YES;
793   name_len = (NULL == name) ? 0 : strlen (name) + 1;
794   rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
795   msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
796   if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
797   {
798     GNUNET_break (0);
799     ztn_ctx->success = GNUNET_SYSERR;
800     return;
801   }
802   ztnr_msg = GNUNET_malloc (msg_size);
803   ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
804   ztnr_msg->gns_header.header.size = htons (msg_size);
805   ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
806   ztnr_msg->res = htons (res);
807   ztnr_msg->rd_len = htons (rd_ser_len);
808   ztnr_msg->rd_count = htons (rd_count);
809   ztnr_msg->name_len = htons (name_len);
810   ztnr_msg->zone = *zone_key;
811   name_tmp = (char *) &ztnr_msg[1];
812   if (NULL != name)
813     memcpy (name_tmp, name, name_len);
814   rd_tmp = &name_tmp[name_len];
815   GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
816   ztn_ctx->success = GNUNET_OK;
817   GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
818                                               &ztnr_msg->gns_header.header,
819                                               GNUNET_NO);
820   GNUNET_free (ztnr_msg);
821 }
822
823
824 /**
825  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
826  *
827  * @param cls unused
828  * @param client client sending the message
829  * @param message message of type 'struct ZoneToNameMessage'
830  */
831 static void
832 handle_zone_to_name (void *cls,
833                      struct GNUNET_SERVER_Client *client,
834                      const struct GNUNET_MessageHeader *message)
835 {
836   struct NamestoreClient *nc;
837   const struct ZoneToNameMessage *ztn_msg;
838   struct ZoneToNameCtx ztn_ctx;
839   struct ZoneToNameResponseMessage ztnr_msg;
840
841   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
842               "Received `%s' message\n",
843               "ZONE_TO_NAME");
844   ztn_msg = (const struct ZoneToNameMessage *) message;
845   nc = client_lookup (client);
846   ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
847   ztn_ctx.nc = nc;
848   ztn_ctx.success = GNUNET_NO;
849   if (GNUNET_SYSERR ==
850       GSN_database->zone_to_name (GSN_database->cls, 
851                                   &ztn_msg->zone,
852                                   &ztn_msg->value_zone,
853                                   &handle_zone_to_name_it, &ztn_ctx))
854   {
855     /* internal error, hang up instead of signalling something
856        that might be wrong */
857     GNUNET_break (0);
858     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
859     return;    
860   }
861   if (GNUNET_NO == ztn_ctx.success)
862   {
863     /* no result found, send empty response */
864     memset (&ztnr_msg, 0, sizeof (ztnr_msg));
865     ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
866     ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
867     ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
868     ztnr_msg.res = htons (GNUNET_NO);
869     GNUNET_SERVER_notification_context_unicast (snc,
870                                                 client,
871                                                 &ztnr_msg.gns_header.header,
872                                                 GNUNET_NO);
873   }
874   GNUNET_SERVER_receive_done (client, GNUNET_OK);
875 }
876
877
878 /**
879  * Zone iteration processor result
880  */
881 enum ZoneIterationResult
882 {
883   /**
884    * Iteration start.
885    */
886   IT_START = 0,
887
888   /**
889    * Found records,
890    * Continue to iterate with next iteration_next call
891    */
892   IT_SUCCESS_MORE_AVAILABLE = 1,
893
894   /**
895    * Iteration complete
896    */
897   IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
898 };
899
900
901 /**
902  * Context for record remove operations passed from
903  * #run_zone_iteration_round to #zone_iteraterate_proc as closure
904  */
905 struct ZoneIterationProcResult
906 {
907   /**
908    * The zone iteration handle
909    */
910   struct ZoneIteration *zi;
911
912   /**
913    * Iteration result: iteration done?
914    * #IT_SUCCESS_MORE_AVAILABLE:  if there may be more results overall but
915    * we got one for now and have sent it to the client
916    * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
917    * #IT_START: if we are still trying to find a result.
918    */
919   int res_iteration_finished;
920
921 };
922
923
924 /**
925  * Process results for zone iteration from database
926  *
927  * @param cls struct ZoneIterationProcResult *proc
928  * @param zone_key the zone key
929  * @param name name
930  * @param rd_count number of records for this name
931  * @param rd record data
932  */
933 static void
934 zone_iteraterate_proc (void *cls,
935                        const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
936                        const char *name,
937                        unsigned int rd_count,
938                        const struct GNUNET_NAMESTORE_RecordData *rd)
939 {
940   struct ZoneIterationProcResult *proc = cls;
941   unsigned int i;
942   int do_refresh_block;
943
944   if ((NULL == zone_key) && (NULL == name))
945   {
946     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
947                 "Iteration done\n");
948     proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
949     return;
950   }
951   if ((NULL == zone_key) || (NULL == name)) 
952   {
953     /* what is this!? should never happen */
954     proc->res_iteration_finished = IT_START;
955     GNUNET_break (0);
956     return;    
957   }
958   proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
959   send_lookup_response (snc,
960                         proc->zi->client->client,
961                         proc->zi->request_id,
962                         zone_key,
963                         name,
964                         rd_count,
965                         rd);
966   do_refresh_block = GNUNET_NO;
967   for (i=0;i<rd_count;i++)
968     if(  (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) &&
969          (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING)) )
970     {
971       do_refresh_block = GNUNET_YES;
972       break;
973     }
974   if (GNUNET_YES == do_refresh_block)
975     refresh_block (zone_key,
976                    name,
977                    rd_count,
978                    rd);
979
980 }
981
982
983 /**
984  * Perform the next round of the zone iteration.
985  *
986  * @param zi zone iterator to process
987  */
988 static void
989 run_zone_iteration_round (struct ZoneIteration *zi)
990 {
991   static struct GNUNET_CRYPTO_EccPrivateKey zero;
992   struct ZoneIterationProcResult proc;
993   struct RecordResultMessage rrm;
994   int ret;
995
996   memset (&proc, 0, sizeof (proc));
997   proc.zi = zi;
998   proc.res_iteration_finished = IT_START;
999   while (IT_START == proc.res_iteration_finished)
1000   {
1001     if (GNUNET_SYSERR ==
1002         (ret = GSN_database->iterate_records (GSN_database->cls, 
1003                                               (0 == memcmp (&zi->zone, &zero, sizeof (zero))) 
1004                                               ? NULL 
1005                                               : &zi->zone,
1006                                               zi->offset, 
1007                                               &zone_iteraterate_proc, &proc)))
1008     {
1009       GNUNET_break (0);
1010       break;
1011     }
1012     if (GNUNET_NO == ret)
1013       proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1014     zi->offset++;
1015   }
1016   if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1017   {
1018     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019                 "More results available\n");
1020     return; /* more results later */
1021   }
1022   /* send empty response to indicate end of list */
1023   memset (&rrm, 0, sizeof (rrm));
1024   rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1025   rrm.gns_header.header.size = htons (sizeof (rrm));
1026   rrm.gns_header.r_id = htonl (zi->request_id);
1027   GNUNET_SERVER_notification_context_unicast (snc,
1028                                               zi->client->client, 
1029                                               &rrm.gns_header.header,
1030                                               GNUNET_NO);
1031   GNUNET_CONTAINER_DLL_remove (zi->client->op_head, 
1032                                zi->client->op_tail,
1033                                zi);
1034   GNUNET_free (zi);
1035 }
1036
1037
1038 /**
1039  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1040  *
1041  * @param cls unused
1042  * @param client the client sending the message
1043  * @param message message of type 'struct ZoneIterationStartMessage'
1044  */
1045 static void
1046 handle_iteration_start (void *cls,
1047                         struct GNUNET_SERVER_Client *client,
1048                         const struct GNUNET_MessageHeader *message)
1049 {
1050   const struct ZoneIterationStartMessage *zis_msg;
1051   struct NamestoreClient *nc;
1052   struct ZoneIteration *zi;
1053
1054   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1055   if (NULL == (nc = client_lookup (client)))
1056   {
1057     GNUNET_break (0);
1058     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1059     return;
1060   }
1061   zis_msg = (const struct ZoneIterationStartMessage *) message;
1062   zi = GNUNET_new (struct ZoneIteration);
1063   zi->request_id = ntohl (zis_msg->gns_header.r_id);
1064   zi->offset = 0;
1065   zi->client = nc;
1066   zi->zone = zis_msg->zone;
1067   GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1068   run_zone_iteration_round (zi);
1069   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1070 }
1071
1072
1073 /**
1074  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1075  *
1076  * @param cls unused
1077  * @param client GNUNET_SERVER_Client sending the message
1078  * @param message message of type 'struct ZoneIterationStopMessage'
1079  */
1080 static void
1081 handle_iteration_stop (void *cls,
1082                        struct GNUNET_SERVER_Client *client,
1083                        const struct GNUNET_MessageHeader *message)
1084 {
1085   struct NamestoreClient *nc;
1086   struct ZoneIteration *zi;
1087   const struct ZoneIterationStopMessage *zis_msg;
1088   uint32_t rid;
1089
1090   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091               "Received `%s' message\n",
1092               "ZONE_ITERATION_STOP");
1093   if (NULL == (nc = client_lookup(client)))
1094   {
1095     GNUNET_break (0);
1096     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1097     return;
1098   }
1099   zis_msg = (const struct ZoneIterationStopMessage *) message;
1100   rid = ntohl (zis_msg->gns_header.r_id);
1101   for (zi = nc->op_head; NULL != zi; zi = zi->next)
1102     if (zi->request_id == rid)
1103       break;
1104   if (NULL == zi)
1105   {
1106     GNUNET_break (0);
1107     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1108     return;
1109   }
1110   GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1111   GNUNET_free (zi);
1112   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1113 }
1114
1115
1116 /**
1117  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1118  *
1119  * @param cls unused
1120  * @param client GNUNET_SERVER_Client sending the message
1121  * @param message message of type 'struct ZoneIterationNextMessage'
1122  */
1123 static void
1124 handle_iteration_next (void *cls,
1125                        struct GNUNET_SERVER_Client *client,
1126                        const struct GNUNET_MessageHeader *message)
1127 {
1128   struct NamestoreClient *nc;
1129   struct ZoneIteration *zi;
1130   const struct ZoneIterationNextMessage *zis_msg;
1131   uint32_t rid;
1132
1133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1134   if (NULL == (nc = client_lookup(client)))
1135   {
1136     GNUNET_break (0);
1137     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1138     return;
1139   }
1140   zis_msg = (const struct ZoneIterationNextMessage *) message;
1141   rid = ntohl (zis_msg->gns_header.r_id);
1142   for (zi = nc->op_head; NULL != zi; zi = zi->next)
1143     if (zi->request_id == rid)
1144       break;
1145   if (NULL == zi)
1146   {
1147     GNUNET_break (0);
1148     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1149     return;
1150   }
1151   run_zone_iteration_round (zi);
1152   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1153 }
1154
1155
1156 /**
1157  * Send 'sync' message to zone monitor, we're now in sync.
1158  *
1159  * @param zm monitor that is now in sync
1160  */ 
1161 static void
1162 monitor_sync (struct ZoneMonitor *zm)
1163 {
1164   struct GNUNET_MessageHeader sync;
1165
1166   sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1167   sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1168   GNUNET_SERVER_notification_context_unicast (monitor_nc,
1169                                               zm->nc->client,
1170                                               &sync,
1171                                               GNUNET_NO);
1172 }
1173
1174
1175 /**
1176  * Obtain the next datum during the zone monitor's zone intiial iteration.
1177  *
1178  * @param cls zone monitor that does its initial iteration
1179  * @param tc scheduler context
1180  */
1181 static void
1182 monitor_next (void *cls,
1183               const struct GNUNET_SCHEDULER_TaskContext *tc);
1184
1185
1186 /**
1187  * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1188  *
1189  * @param cls a 'struct ZoneMonitor *' with information about the monitor
1190  * @param zone_key zone key of the zone
1191  * @param name name
1192  * @param rd_count number of records in @a rd
1193  * @param rd array of records
1194  */
1195 static void
1196 monitor_iterate_cb (void *cls,
1197                     const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1198                     const char *name,
1199                     unsigned int rd_count,
1200                     const struct GNUNET_NAMESTORE_RecordData *rd)
1201 {
1202   struct ZoneMonitor *zm = cls;
1203
1204   if (NULL == name)
1205   {
1206     /* finished with iteration */
1207     monitor_sync (zm);
1208     return;
1209   }
1210   send_lookup_response (monitor_nc,
1211                         zm->nc->client,
1212                         zm->request_id,
1213                         zone_key,
1214                         name,
1215                         rd_count,
1216                         rd);
1217   zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1218 }
1219
1220
1221 /**
1222  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1223  *
1224  * @param cls unused
1225  * @param client GNUNET_SERVER_Client sending the message
1226  * @param message message of type 'struct ZoneMonitorStartMessage'
1227  */
1228 static void
1229 handle_monitor_start (void *cls,
1230                       struct GNUNET_SERVER_Client *client,
1231                       const struct GNUNET_MessageHeader *message)
1232 {
1233   const struct ZoneMonitorStartMessage *zis_msg;
1234   struct ZoneMonitor *zm;
1235   
1236   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
1237               "Received `%s' message\n",
1238               "ZONE_MONITOR_START");
1239   zis_msg = (const struct ZoneMonitorStartMessage *) message;
1240   zm = GNUNET_new (struct ZoneMonitor);
1241   zm->request_id = ntohl (zis_msg->gns_header.r_id);
1242   zm->offset = 0;
1243   zm->nc = client_lookup (client);
1244   zm->zone = zis_msg->zone;
1245   GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1246   GNUNET_SERVER_client_mark_monitor (client);
1247   GNUNET_SERVER_disable_receive_done_warning (client);
1248   GNUNET_SERVER_notification_context_add (monitor_nc,
1249                                           client);
1250   zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);  
1251 }
1252
1253
1254 /**
1255  * Obtain the next datum during the zone monitor's zone intiial iteration.
1256  *
1257  * @param cls zone monitor that does its initial iteration
1258  * @param tc scheduler context
1259  */
1260 static void
1261 monitor_next (void *cls,
1262               const struct GNUNET_SCHEDULER_TaskContext *tc)
1263 {
1264   struct ZoneMonitor *zm = cls;
1265   int ret;
1266   
1267   zm->task = GNUNET_SCHEDULER_NO_TASK;
1268   ret = GSN_database->iterate_records (GSN_database->cls,
1269                                        &zm->zone,
1270                                        zm->offset++,
1271                                        &monitor_iterate_cb, zm);
1272   if (GNUNET_SYSERR == ret)
1273   {
1274     GNUNET_SERVER_client_disconnect (zm->nc->client);
1275     return;
1276   }
1277   if (GNUNET_NO == ret)
1278   {
1279     /* empty zone */
1280     monitor_sync (zm);
1281     return;
1282   }
1283 }
1284
1285
1286 /**
1287  * Process namestore requests.
1288  *
1289  * @param cls closure
1290  * @param server the initialized server
1291  * @param cfg configuration to use
1292  */
1293 static void
1294 run (void *cls, struct GNUNET_SERVER_Handle *server,
1295      const struct GNUNET_CONFIGURATION_Handle *cfg)
1296 {
1297   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1298     {&handle_lookup_block, NULL,
1299      GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1300     {&handle_block_cache, NULL,
1301     GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1302     {&handle_record_store, NULL,
1303      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1304     {&handle_zone_to_name, NULL,
1305      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1306     {&handle_iteration_start, NULL,
1307      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1308     {&handle_iteration_next, NULL,
1309      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1310     {&handle_iteration_stop, NULL,
1311      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1312     {&handle_monitor_start, NULL,
1313      GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1314     {NULL, NULL, 0, 0}
1315   };
1316   char *database;
1317
1318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1319   GSN_cfg = cfg;
1320   monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1321
1322   /* Loading database plugin */
1323   if (GNUNET_OK !=
1324       GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1325                                              &database))
1326     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1327
1328   GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1329   GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1330   GNUNET_free (database);
1331   if (NULL == GSN_database)
1332   {
1333     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
1334                 "Could not load database backend `%s'\n",
1335                 db_lib_name);
1336     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1337     return;
1338   }
1339
1340   /* Configuring server handles */
1341   GNUNET_SERVER_add_handlers (server, handlers);
1342   snc = GNUNET_SERVER_notification_context_create (server, 16);
1343   GNUNET_SERVER_disconnect_notify (server,
1344                                    &client_disconnect_notification,
1345                                    NULL);
1346   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1347                                 NULL);
1348 }
1349
1350
1351 /**
1352  * The main function for the template service.
1353  *
1354  * @param argc number of arguments from the command line
1355  * @param argv command line arguments
1356  * @return 0 ok, 1 on error
1357  */
1358 int
1359 main (int argc, char *const *argv)
1360 {
1361   return (GNUNET_OK ==
1362           GNUNET_SERVICE_run (argc, argv, "namestore",
1363                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1364 }
1365
1366 /* end of gnunet-service-namestore.c */
1367