Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013, 2014, 2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file namestore/gnunet-service-namestore.c
21  * @brief namestore for the GNUnet naming system
22  * @author Matthias Wachs
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - run testcases, make sure everything works!
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_gns_service.h"
32 #include "gnunet_namecache_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_namestore_plugin.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_signatures.h"
37 #include "namestore.h"
38
39 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
40
41 /**
42  * If a monitor takes more than 1 minute to process an event, print a warning.
43  */
44 #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
45
46
47 /**
48  * A namestore client
49  */
50 struct NamestoreClient;
51
52
53 /**
54  * A namestore iteration operation.
55  */
56 struct ZoneIteration
57 {
58   /**
59    * Next element in the DLL
60    */
61   struct ZoneIteration *next;
62
63   /**
64    * Previous element in the DLL
65    */
66   struct ZoneIteration *prev;
67
68   /**
69    * Namestore client which intiated this zone iteration
70    */
71   struct NamestoreClient *nc;
72
73   /**
74    * The nick to add to the records
75    */
76   struct GNUNET_GNSRECORD_Data *nick;
77
78   /**
79    * Key of the zone we are iterating over.
80    */
81   struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
82
83   /**
84    * Last sequence number in the zone iteration used to address next
85    * result of the zone iteration in the store
86    *
87    * Initialy set to 0.
88    * Updated in #zone_iterate_proc()
89    */
90   uint64_t seq;
91
92   /**
93    * The operation id fot the zone iteration in the response for the client
94    */
95   uint32_t request_id;
96
97   /**
98    * Offset of the zone iteration used to address next result of the zone
99    * iteration in the store
100    *
101    * Initialy set to 0 in #handle_iteration_start
102    * Incremented with by every call to #handle_iteration_next
103    */
104   uint32_t offset;
105
106 };
107
108
109 /**
110  * A namestore client
111  */
112 struct NamestoreClient
113 {
114
115   /**
116    * The client
117    */
118   struct GNUNET_SERVICE_Client *client;
119
120   /**
121    * Message queue for transmission to @e client
122    */
123   struct GNUNET_MQ_Handle *mq;
124
125   /**
126    * Head of the DLL of
127    * Zone iteration operations in progress initiated by this client
128    */
129   struct ZoneIteration *op_head;
130
131   /**
132    * Tail of the DLL of
133    * Zone iteration operations in progress initiated by this client
134    */
135   struct ZoneIteration *op_tail;
136 };
137
138
139 /**
140  * A namestore monitor.
141  */
142 struct ZoneMonitor
143 {
144   /**
145    * Next element in the DLL
146    */
147   struct ZoneMonitor *next;
148
149   /**
150    * Previous element in the DLL
151    */
152   struct ZoneMonitor *prev;
153
154   /**
155    * Namestore client which intiated this zone monitor
156    */
157   struct NamestoreClient *nc;
158
159   /**
160    * Private key of the zone.
161    */
162   struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
163
164   /**
165    * Task active during initial iteration.
166    */
167   struct GNUNET_SCHEDULER_Task *task;
168
169   /**
170    * Task to warn about slow monitors.
171    */
172   struct GNUNET_SCHEDULER_Task *sa_wait_warning;
173
174   /**
175    * Since when are we blocked on this monitor?
176    */
177   struct GNUNET_TIME_Absolute sa_waiting_start;
178
179   /**
180    * Last sequence number in the zone iteration used to address next
181    * result of the zone iteration in the store
182    *
183    * Initialy set to 0.
184    * Updated in #monitor_iterate_cb()
185    */
186   uint64_t seq;
187
188   /**
189    * Current limit of how many more messages we are allowed
190    * to queue to this monitor.
191    */
192   uint64_t limit;
193
194   /**
195    * How many more requests may we receive from the iterator
196    * before it is at the limit we gave it?  Will be below or
197    * equal to @e limit.  The effective limit for monitor
198    * events is thus @e iteration_cnt - @e limit!
199    */
200   uint64_t iteration_cnt;
201
202   /**
203    * Are we (still) in the initial iteration pass?
204    */
205   int in_first_iteration;
206
207   /**
208    * Is there a store activity waiting for this monitor?  We only raise the
209    * flag when it happens and search the DLL for the store activity when we
210    * had a limit increase.  If we cannot find any waiting store activity at
211    * that time, we clear the flag again.
212    */
213   int sa_waiting;
214
215 };
216
217
218 /**
219  * Pending operation on the namecache.
220  */
221 struct CacheOperation
222 {
223
224   /**
225    * Kept in a DLL.
226    */
227   struct CacheOperation *prev;
228
229   /**
230    * Kept in a DLL.
231    */
232   struct CacheOperation *next;
233
234   /**
235    * Handle to namecache queue.
236    */
237   struct GNUNET_NAMECACHE_QueueEntry *qe;
238
239   /**
240    * Client to notify about the result.
241    */
242   struct NamestoreClient *nc;
243
244   /**
245    * Client's request ID.
246    */
247   uint32_t rid;
248 };
249
250
251 /**
252  * Information for an ongoing #handle_record_store() operation.
253  * Needed as we may wait for monitors to be ready for the notification.
254  */
255 struct StoreActivity
256 {
257   /**
258    * Kept in a DLL.
259    */
260   struct StoreActivity *next;
261
262   /**
263    * Kept in a DLL.
264    */
265   struct StoreActivity *prev;
266
267   /**
268    * Which client triggered the store activity?
269    */
270   struct NamestoreClient *nc;
271
272   /**
273    * Copy of the original store message (as data fields in @e rd will
274    * point into it!).
275    */
276   const struct RecordStoreMessage *rsm;
277
278   /**
279    * Next zone monitor that still needs to be notified about this PUT.
280    */
281   struct ZoneMonitor *zm_pos;
282
283   /**
284    * Label nicely canonicalized (lower case).
285    */
286   char *conv_name;
287
288 };
289
290
291 /**
292  * Public key of all zeros.
293  */
294 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
295
296 /**
297  * Configuration handle.
298  */
299 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
300
301 /**
302  * Handle to the statistics service
303  */
304 static struct GNUNET_STATISTICS_Handle *statistics;
305
306 /**
307  * Namecache handle.
308  */
309 static struct GNUNET_NAMECACHE_Handle *namecache;
310
311 /**
312  * Database handle
313  */
314 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
315
316 /**
317  * Name of the database plugin
318  */
319 static char *db_lib_name;
320
321 /**
322  * Head of cop DLL.
323  */
324 static struct CacheOperation *cop_head;
325
326 /**
327  * Tail of cop DLL.
328  */
329 static struct CacheOperation *cop_tail;
330
331 /**
332  * First active zone monitor.
333  */
334 static struct ZoneMonitor *monitor_head;
335
336 /**
337  * Last active zone monitor.
338  */
339 static struct ZoneMonitor *monitor_tail;
340
341 /**
342  * Head of DLL of monitor-blocked store activities.
343  */
344 static struct StoreActivity *sa_head;
345
346 /**
347  * Tail of DLL of monitor-blocked store activities.
348  */
349 static struct StoreActivity *sa_tail;
350
351 /**
352  * Notification context shared by all monitors.
353  */
354 static struct GNUNET_NotificationContext *monitor_nc;
355
356 /**
357  * Optimize block insertion by caching map of private keys to
358  * public keys in memory?
359  */
360 static int cache_keys;
361
362 /**
363  * Use the namecache? Doing so creates additional cryptographic
364  * operations whenever we touch a record.
365  */
366 static int disable_namecache;
367
368
369 /**
370  * Task run during shutdown.
371  *
372  * @param cls unused
373  */
374 static void
375 cleanup_task (void *cls)
376 {
377   struct CacheOperation *cop;
378
379   (void) cls;
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381               "Stopping namestore service\n");
382   while (NULL != (cop = cop_head))
383   {
384     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385                 "Aborting incomplete namecache operation\n");
386     GNUNET_NAMECACHE_cancel (cop->qe);
387     GNUNET_CONTAINER_DLL_remove (cop_head,
388                                  cop_tail,
389                                  cop);
390     GNUNET_free (cop);
391   }
392   if (NULL != namecache)
393   {
394     GNUNET_NAMECACHE_disconnect (namecache);
395     namecache = NULL;
396   }
397   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
398                                               GSN_database));
399   GNUNET_free (db_lib_name);
400   db_lib_name = NULL;
401   if (NULL != monitor_nc)
402   {
403     GNUNET_notification_context_destroy (monitor_nc);
404     monitor_nc = NULL;
405   }
406   if (NULL != statistics)
407   {
408     GNUNET_STATISTICS_destroy (statistics,
409                                GNUNET_NO);
410     statistics = NULL;
411   }
412 }
413
414
415 /**
416  * Release memory used by @a sa.
417  *
418  * @param sa activity to free
419  */
420 static void
421 free_store_activity (struct StoreActivity *sa)
422 {
423   GNUNET_CONTAINER_DLL_remove (sa_head,
424                                sa_tail,
425                                sa);
426   GNUNET_free (sa->conv_name);
427   GNUNET_free (sa);
428 }
429
430
431 /**
432  * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
433  * label in the zone.  Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
434  * record, which (if found) is then copied to @a cls for future use.
435  *
436  * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
437  * @param seq sequence number of the record
438  * @param private_key the private key of the zone (unused)
439  * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
440  * @param rd_count number of records in @a rd
441  * @param rd records stored under @a label in the zone
442  */
443 static void
444 lookup_nick_it (void *cls,
445                 uint64_t seq,
446                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
447                 const char *label,
448                 unsigned int rd_count,
449                 const struct GNUNET_GNSRECORD_Data *rd)
450 {
451   struct GNUNET_GNSRECORD_Data **res = cls;
452
453   (void) private_key;
454   (void) seq;
455   if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
456   {
457     GNUNET_break (0);
458     return;
459   }
460   for (unsigned int c = 0; c < rd_count; c++)
461   {
462     if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
463     {
464       (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
465       (*res)->data = &(*res)[1];
466       GNUNET_memcpy ((void *) (*res)->data,
467                      rd[c].data,
468                      rd[c].data_size);
469       (*res)->data_size = rd[c].data_size;
470       (*res)->expiration_time = rd[c].expiration_time;
471       (*res)->flags = rd[c].flags;
472       (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
473       return;
474     }
475   }
476   (*res) = NULL;
477 }
478
479
480 /**
481  * Return the NICK record for the zone (if it exists).
482  *
483  * @param zone private key for the zone to look for nick
484  * @return NULL if no NICK record was found
485  */
486 static struct GNUNET_GNSRECORD_Data *
487 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
488 {
489   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
490   struct GNUNET_GNSRECORD_Data *nick;
491   int res;
492
493   nick = NULL;
494   res = GSN_database->lookup_records (GSN_database->cls,
495                                       zone,
496                                       GNUNET_GNS_EMPTY_LABEL_AT,
497                                       &lookup_nick_it,
498                                       &nick);
499   if ( (GNUNET_OK != res) ||
500        (NULL == nick) )
501   {
502     GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
503     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
504                 "No nick name set for zone `%s'\n",
505                 GNUNET_GNSRECORD_z2s (&pub));
506     return NULL;
507   }
508   return nick;
509 }
510
511
512 /**
513  * Merge the nick record @a nick_rd with the rest of the
514  * record set given in @a rd2.  Store the result in @a rdc_res
515  * and @a rd_res.  The @a nick_rd's expiration time is set to
516  * the maximum expiration time of all of the records in @a rd2.
517  *
518  * @param nick_rd the nick record to integrate
519  * @param rd2_length length of the @a rd2 array
520  * @param rd2 array of records
521  * @param rdc_res[out] length of the resulting @a rd_res array
522  * @param rd_res[out] set to an array of records,
523  *                    including @a nick_rd and @a rd2;
524  *           all of the variable-size 'data' fields in @a rd2 are
525  *           allocated in the same chunk of memory!
526  */
527 static void
528 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
529                          unsigned int rd2_length,
530                          const struct GNUNET_GNSRECORD_Data *rd2,
531                          unsigned int *rdc_res,
532                          struct GNUNET_GNSRECORD_Data **rd_res)
533 {
534   uint64_t latest_expiration;
535   size_t req;
536   char *data;
537   size_t data_offset;
538   struct GNUNET_GNSRECORD_Data *target;
539
540   (*rdc_res) = 1 + rd2_length;
541   if (0 == 1 + rd2_length)
542   {
543     GNUNET_break (0);
544     (*rd_res) = NULL;
545     return;
546   }
547   req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
548   for (unsigned int i=0; i<rd2_length; i++)
549   {
550     const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
551
552     if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
553     {
554       GNUNET_break (0);
555       (*rd_res) = NULL;
556       return;
557     }
558     req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
559   }
560   target = GNUNET_malloc (req);
561   (*rd_res) = target;
562   data = (char *) &target[1 + rd2_length];
563   data_offset = 0;
564   latest_expiration = 0;
565   for (unsigned int i=0;i<rd2_length;i++)
566   {
567     const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
568
569     if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
570     {
571       if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
572           latest_expiration)
573         latest_expiration = orig->expiration_time;
574     }
575     else if (orig->expiration_time > latest_expiration)
576       latest_expiration = orig->expiration_time;
577     target[i] = *orig;
578     target[i].data = (void *) &data[data_offset];
579     GNUNET_memcpy (&data[data_offset],
580                    orig->data,
581                    orig->data_size);
582     data_offset += orig->data_size;
583   }
584   /* append nick */
585   target[rd2_length] = *nick_rd;
586   target[rd2_length].expiration_time = latest_expiration;
587   target[rd2_length].data = (void *) &data[data_offset];
588   GNUNET_memcpy (&data[data_offset],
589                  nick_rd->data,
590                  nick_rd->data_size);
591   data_offset += nick_rd->data_size;
592   GNUNET_assert (req ==
593                  (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
594 }
595
596
597 /**
598  * Generate a `struct LookupNameResponseMessage` and send it to the
599  * given client using the given notification context.
600  *
601  * @param nc client to unicast to
602  * @param request_id request ID to use
603  * @param zone_key zone key of the zone
604  * @param name name
605  * @param rd_count number of records in @a rd
606  * @param rd array of records
607  */
608 static void
609 send_lookup_response (struct NamestoreClient *nc,
610                       uint32_t request_id,
611                       const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
612                       const char *name,
613                       unsigned int rd_count,
614                       const struct GNUNET_GNSRECORD_Data *rd)
615 {
616   struct GNUNET_MQ_Envelope *env;
617   struct RecordResultMessage *zir_msg;
618   struct GNUNET_GNSRECORD_Data *nick;
619   struct GNUNET_GNSRECORD_Data *res;
620   unsigned int res_count;
621   size_t name_len;
622   ssize_t rd_ser_len;
623   char *name_tmp;
624   char *rd_ser;
625
626   nick = get_nick_record (zone_key);
627   GNUNET_assert (-1 !=
628                  GNUNET_GNSRECORD_records_get_size (rd_count,
629                                                     rd));
630
631   if ( (NULL != nick) &&
632        (0 != strcmp (name,
633                      GNUNET_GNS_EMPTY_LABEL_AT)))
634   {
635     nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
636     merge_with_nick_records (nick,
637                              rd_count,
638                              rd,
639                              &res_count,
640                              &res);
641     GNUNET_free (nick);
642   }
643   else
644   {
645     res_count = rd_count;
646     res = (struct GNUNET_GNSRECORD_Data *) rd;
647   }
648
649   GNUNET_assert (-1 !=
650                  GNUNET_GNSRECORD_records_get_size (res_count,
651                                                     res));
652
653
654   name_len = strlen (name) + 1;
655   rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
656                                                   res);
657   if (rd_ser_len < 0)
658   {
659     GNUNET_break (0);
660     GNUNET_SERVICE_client_drop (nc->client);
661     return;
662   }
663   if (rd_ser_len >= UINT16_MAX - name_len - sizeof (*zir_msg))
664   {
665     GNUNET_break (0);
666     GNUNET_SERVICE_client_drop (nc->client);
667     return;
668   }
669   env = GNUNET_MQ_msg_extra (zir_msg,
670                              name_len + rd_ser_len,
671                              GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
672   zir_msg->gns_header.r_id = htonl (request_id);
673   zir_msg->name_len = htons (name_len);
674   zir_msg->rd_count = htons (res_count);
675   zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
676   zir_msg->private_key = *zone_key;
677   name_tmp = (char *) &zir_msg[1];
678   GNUNET_memcpy (name_tmp,
679                  name,
680                  name_len);
681   rd_ser = &name_tmp[name_len];
682   GNUNET_assert (rd_ser_len ==
683                  GNUNET_GNSRECORD_records_serialize (res_count,
684                                                      res,
685                                                      rd_ser_len,
686                                                      rd_ser));
687   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688               "Sending RECORD_RESULT message with %u records\n",
689               res_count);
690   GNUNET_STATISTICS_update (statistics,
691                             "Record sets sent to clients",
692                             1,
693                             GNUNET_NO);
694   GNUNET_MQ_send (nc->mq,
695                   env);
696   if (rd != res)
697     GNUNET_free (res);
698 }
699
700
701 /**
702  * Send response to the store request to the client.
703  *
704  * @param client client to talk to
705  * @param res status of the operation
706  * @param rid client's request ID
707  */
708 static void
709 send_store_response (struct NamestoreClient *nc,
710                      int res,
711                      uint32_t rid)
712 {
713   struct GNUNET_MQ_Envelope *env;
714   struct RecordStoreResponseMessage *rcr_msg;
715
716   GNUNET_assert (NULL != nc);
717   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718               "Sending RECORD_STORE_RESPONSE message\n");
719   GNUNET_STATISTICS_update (statistics,
720                             "Store requests completed",
721                             1,
722                             GNUNET_NO);
723   env = GNUNET_MQ_msg (rcr_msg,
724                        GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
725   rcr_msg->gns_header.r_id = htonl (rid);
726   rcr_msg->op_result = htonl (res);
727   GNUNET_MQ_send (nc->mq,
728                   env);
729 }
730
731
732 /**
733  * Cache operation complete, clean up.
734  *
735  * @param cls the `struct CacheOperation`
736  * @param success success
737  * @param emsg error messages
738  */
739 static void
740 finish_cache_operation (void *cls,
741                         int32_t success,
742                         const char *emsg)
743 {
744   struct CacheOperation *cop = cls;
745
746   if (NULL != emsg)
747     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
748                 _("Failed to replicate block in namecache: %s\n"),
749                 emsg);
750   else
751     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752                 "CACHE operation completed\n");
753   GNUNET_CONTAINER_DLL_remove (cop_head,
754                                cop_tail,
755                                cop);
756   if (NULL != cop->nc)
757     send_store_response (cop->nc,
758                          success,
759                          cop->rid);
760   GNUNET_free (cop);
761 }
762
763
764 /**
765  * We just touched the plaintext information about a name in our zone;
766  * refresh the corresponding (encrypted) block in the namecache.
767  *
768  * @param nc client responsible for the request, can be NULL
769  * @param rid request ID of the client
770  * @param zone_key private key of the zone
771  * @param name label for the records
772  * @param rd_count number of records
773  * @param rd records stored under the given @a name
774  */
775 static void
776 refresh_block (struct NamestoreClient *nc,
777                uint32_t rid,
778                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
779                const char *name,
780                unsigned int rd_count,
781                const struct GNUNET_GNSRECORD_Data *rd)
782 {
783   struct GNUNET_GNSRECORD_Block *block;
784   struct CacheOperation *cop;
785   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
786   struct GNUNET_GNSRECORD_Data *nick;
787   struct GNUNET_GNSRECORD_Data *res;
788   unsigned int res_count;
789   struct GNUNET_TIME_Absolute exp_time;
790
791   nick = get_nick_record (zone_key);
792   res_count = rd_count;
793   res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
794   if (NULL != nick)
795   {
796     nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
797     merge_with_nick_records (nick,
798                              rd_count,rd,
799                              &res_count,
800                              &res);
801     GNUNET_free (nick);
802   }
803   if (0 == res_count)
804   {
805     if (NULL != nc)
806       send_store_response (nc,
807                            GNUNET_OK,
808                            rid);
809     return; /* no data, no need to update cache */
810   }
811   if (GNUNET_YES == disable_namecache)
812   {
813     GNUNET_STATISTICS_update (statistics,
814                               "Namecache updates skipped (NC disabled)",
815                               1,
816                               GNUNET_NO);
817     if (NULL != nc)
818       send_store_response (nc,
819                            GNUNET_OK,
820                            rid);
821     return;
822   }
823   exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
824                                                           res);
825   if (cache_keys)
826     block = GNUNET_GNSRECORD_block_create2 (zone_key,
827                                             exp_time,
828                                             name,
829                                             res,
830                                             res_count);
831   else
832     block = GNUNET_GNSRECORD_block_create (zone_key,
833                                            exp_time,
834                                            name,
835                                            res,
836                                            res_count);
837   GNUNET_assert (NULL != block);
838   GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
839                                       &pkey);
840   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841               "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
842               name,
843               res_count,
844               GNUNET_STRINGS_absolute_time_to_string (exp_time),
845               GNUNET_GNSRECORD_z2s (&pkey));
846   GNUNET_STATISTICS_update (statistics,
847                             "Namecache updates pushed",
848                             1,
849                             GNUNET_NO);
850   cop = GNUNET_new (struct CacheOperation);
851   cop->nc = nc;
852   cop->rid = rid;
853   GNUNET_CONTAINER_DLL_insert (cop_head,
854                                cop_tail,
855                                cop);
856   cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
857                                           block,
858                                           &finish_cache_operation,
859                                           cop);
860   GNUNET_free (block);
861 }
862
863
864 /**
865  * Print a warning that one of our monitors is no longer reacting.
866  *
867  * @param cls a `struct ZoneMonitor` to warn about
868  */
869 static void
870 warn_monitor_slow (void *cls)
871 {
872   struct ZoneMonitor *zm = cls;
873
874   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
875               "No response from monitor since %s\n",
876               GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
877   zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
878                                                       &warn_monitor_slow,
879                                                       zm);
880 }
881
882
883 /**
884  * Continue processing the @a sa.
885  *
886  * @param sa store activity to process
887  */
888 static void
889 continue_store_activity (struct StoreActivity *sa)
890 {
891   const struct RecordStoreMessage *rp_msg = sa->rsm;
892   unsigned int rd_count;
893   size_t name_len;
894   size_t rd_ser_len;
895   uint32_t rid;
896   const char *name_tmp;
897   const char *rd_ser;
898
899   rid = ntohl (rp_msg->gns_header.r_id);
900   name_len = ntohs (rp_msg->name_len);
901   rd_count = ntohs (rp_msg->rd_count);
902   rd_ser_len = ntohs (rp_msg->rd_len);
903   name_tmp = (const char *) &rp_msg[1];
904   rd_ser = &name_tmp[name_len];
905   {
906     struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
907
908     /* We did this before, must succeed again */
909     GNUNET_assert (GNUNET_OK ==
910                    GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
911                                                          rd_ser,
912                                                          rd_count,
913                                                          rd));
914
915     for (struct ZoneMonitor *zm = sa->zm_pos;
916          NULL != zm;
917          zm = sa->zm_pos)
918     {
919       if ( (0 != memcmp (&rp_msg->private_key,
920                          &zm->zone,
921                          sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
922            (0 != memcmp (&zm->zone,
923                          &zero,
924                          sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
925         {
926           sa->zm_pos = zm->next; /* not interesting to this monitor */
927           continue;
928         }
929       if (zm->limit == zm->iteration_cnt)
930       {
931         zm->sa_waiting = GNUNET_YES;
932         zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
933         if (NULL != zm->sa_wait_warning)
934           GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
935         zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
936                                                             &warn_monitor_slow,
937                                                             zm);
938         return; /* blocked on zone monitor */
939       }
940       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941                   "Notifying monitor about changes under label `%s'\n",
942                   sa->conv_name);
943       zm->limit--;
944       send_lookup_response (zm->nc,
945                             0,
946                             &rp_msg->private_key,
947                             sa->conv_name,
948                             rd_count,
949                             rd);
950       sa->zm_pos = zm->next;
951     }
952     /* great, done with the monitors, unpack (again) for refresh_block operation */
953     refresh_block (sa->nc,
954                    rid,
955                    &rp_msg->private_key,
956                    sa->conv_name,
957                    rd_count,
958                    rd);
959   }
960   GNUNET_SERVICE_client_continue (sa->nc->client);
961   free_store_activity (sa);
962 }
963
964
965 /**
966  * Called whenever a client is disconnected.
967  * Frees our resources associated with that client.
968  *
969  * @param cls closure
970  * @param client identification of the client
971  * @param app_ctx the `struct NamestoreClient` of @a client
972  */
973 static void
974 client_disconnect_cb (void *cls,
975                       struct GNUNET_SERVICE_Client *client,
976                       void *app_ctx)
977 {
978   struct NamestoreClient *nc = app_ctx;
979   struct ZoneIteration *no;
980   struct CacheOperation *cop;
981
982   (void) cls;
983   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984               "Client %p disconnected\n",
985               client);
986   for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
987   {
988     struct StoreActivity *san;
989
990     if (nc != zm->nc)
991       continue;
992     GNUNET_CONTAINER_DLL_remove (monitor_head,
993                                  monitor_tail,
994                                  zm);
995     if (NULL != zm->task)
996     {
997       GNUNET_SCHEDULER_cancel (zm->task);
998       zm->task = NULL;
999     }
1000     if (NULL != zm->sa_wait_warning)
1001     {
1002       GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1003       zm->sa_wait_warning = NULL;
1004     }
1005     for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1006     {
1007       san = sa->next;
1008       if (zm == sa->zm_pos)
1009       {
1010         sa->zm_pos = zm->next;
1011         /* this may free sa */
1012         continue_store_activity (sa);
1013       }
1014     }
1015     GNUNET_free (zm);
1016     break;
1017   }
1018   for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1019   {
1020     if (sa->nc == nc)
1021     {
1022       /* this may free sa */
1023       free_store_activity (sa);
1024       break; /* there can only be one per nc */
1025     }
1026   }
1027   while (NULL != (no = nc->op_head))
1028   {
1029     GNUNET_CONTAINER_DLL_remove (nc->op_head,
1030                                  nc->op_tail,
1031                                  no);
1032     GNUNET_free (no);
1033   }
1034   for (cop = cop_head; NULL != cop; cop = cop->next)
1035     if (nc == cop->nc)
1036       cop->nc = NULL;
1037   GNUNET_free (nc);
1038 }
1039
1040
1041 /**
1042  * Add a client to our list of active clients.
1043  *
1044  * @param cls NULL
1045  * @param client client to add
1046  * @param mq message queue for @a client
1047  * @return internal namestore client structure for this client
1048  */
1049 static void *
1050 client_connect_cb (void *cls,
1051                    struct GNUNET_SERVICE_Client *client,
1052                    struct GNUNET_MQ_Handle *mq)
1053 {
1054   struct NamestoreClient *nc;
1055
1056   (void) cls;
1057   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058               "Client %p connected\n",
1059               client);
1060   nc = GNUNET_new (struct NamestoreClient);
1061   nc->client = client;
1062   nc->mq = mq;
1063   return nc;
1064 }
1065
1066
1067 /**
1068  * Closure for #lookup_it().
1069  */
1070 struct RecordLookupContext
1071 {
1072
1073   /**
1074    * FIXME.
1075    */
1076   const char *label;
1077
1078   /**
1079    * FIXME.
1080    */
1081   char *res_rd;
1082
1083   /**
1084    * FIXME.
1085    */
1086   struct GNUNET_GNSRECORD_Data *nick;
1087
1088   /**
1089    * FIXME.
1090    */
1091   int found;
1092
1093   /**
1094    * FIXME.
1095    */
1096   unsigned int res_rd_count;
1097
1098   /**
1099    * FIXME.
1100    */
1101   ssize_t rd_ser_len;
1102 };
1103
1104
1105 /**
1106  * FIXME.
1107  *
1108  * @param seq sequence number of the record
1109  */
1110 static void
1111 lookup_it (void *cls,
1112            uint64_t seq,
1113            const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1114            const char *label,
1115            unsigned int rd_count,
1116            const struct GNUNET_GNSRECORD_Data *rd)
1117 {
1118   struct RecordLookupContext *rlc = cls;
1119
1120   (void) private_key;
1121   (void) seq;
1122   if (0 != strcmp (label,
1123                    rlc->label))
1124     return;
1125   rlc->found = GNUNET_YES;
1126   if (0 == rd_count)
1127   {
1128     rlc->rd_ser_len = 0;
1129     rlc->res_rd_count = 0;
1130     rlc->res_rd = NULL;
1131     return;
1132   }
1133   if ( (NULL != rlc->nick) &&
1134        (0 != strcmp (label,
1135                      GNUNET_GNS_EMPTY_LABEL_AT)) )
1136   {
1137     /* Merge */
1138     struct GNUNET_GNSRECORD_Data *rd_res;
1139     unsigned int rdc_res;
1140
1141     rd_res = NULL;
1142     rdc_res = 0;
1143     rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1144     merge_with_nick_records (rlc->nick,
1145                              rd_count,
1146                              rd,
1147                              &rdc_res,
1148                              &rd_res);
1149     rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1150                                                          rd_res);
1151     if (rlc->rd_ser_len < 0)
1152     {
1153       GNUNET_break (0);
1154       GNUNET_free  (rd_res);
1155       rlc->found = GNUNET_NO;
1156       rlc->rd_ser_len = 0;
1157       return;
1158     }
1159     rlc->res_rd_count = rdc_res;
1160     rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1161     if (rlc->rd_ser_len !=
1162         GNUNET_GNSRECORD_records_serialize (rdc_res,
1163                                             rd_res,
1164                                             rlc->rd_ser_len,
1165                                             rlc->res_rd))
1166     {
1167       GNUNET_break (0);
1168       GNUNET_free  (rlc->res_rd);
1169       rlc->res_rd = NULL;
1170       rlc->res_rd_count = 0;
1171       rlc->rd_ser_len = 0;
1172       GNUNET_free  (rd_res);
1173       rlc->found = GNUNET_NO;
1174       return;
1175     }
1176     GNUNET_free (rd_res);
1177     GNUNET_free (rlc->nick);
1178     rlc->nick = NULL;
1179   }
1180   else
1181   {
1182     rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1183                                                          rd);
1184     if (rlc->rd_ser_len < 0)
1185     {
1186       GNUNET_break (0);
1187       rlc->found = GNUNET_NO;
1188       rlc->rd_ser_len = 0;
1189       return;
1190     }
1191     rlc->res_rd_count = rd_count;
1192     rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1193     if (rlc->rd_ser_len !=
1194         GNUNET_GNSRECORD_records_serialize (rd_count,
1195                                             rd,
1196                                             rlc->rd_ser_len,
1197                                             rlc->res_rd))
1198     {
1199       GNUNET_break (0);
1200       GNUNET_free  (rlc->res_rd);
1201       rlc->res_rd = NULL;
1202       rlc->res_rd_count = 0;
1203       rlc->rd_ser_len = 0;
1204       rlc->found = GNUNET_NO;
1205       return;
1206     }
1207   }
1208 }
1209
1210
1211 /**
1212  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1213  *
1214  * @param cls client sending the message
1215  * @param ll_msg message of type `struct LabelLookupMessage`
1216  * @return #GNUNET_OK if @a ll_msg is well-formed
1217  */
1218 static int
1219 check_record_lookup (void *cls,
1220                      const struct LabelLookupMessage *ll_msg)
1221 {
1222   uint32_t name_len;
1223   size_t src_size;
1224   const char *name_tmp;
1225
1226   (void) cls;
1227   name_len = ntohl (ll_msg->label_len);
1228   src_size = ntohs (ll_msg->gns_header.header.size);
1229   if (name_len != src_size - sizeof (struct LabelLookupMessage))
1230   {
1231     GNUNET_break (0);
1232     return GNUNET_SYSERR;
1233   }
1234
1235   name_tmp = (const char *) &ll_msg[1];
1236   if ('\0' != name_tmp[name_len -1])
1237   {
1238     GNUNET_break (0);
1239     return GNUNET_SYSERR;
1240   }
1241   return GNUNET_OK;
1242 }
1243
1244
1245 /**
1246  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1247  *
1248  * @param cls client sending the message
1249  * @param ll_msg message of type `struct LabelLookupMessage`
1250  */
1251 static void
1252 handle_record_lookup (void *cls,
1253                       const struct LabelLookupMessage *ll_msg)
1254 {
1255   struct NamestoreClient *nc = cls;
1256   struct GNUNET_MQ_Envelope *env;
1257   struct LabelLookupResponseMessage *llr_msg;
1258   struct RecordLookupContext rlc;
1259   const char *name_tmp;
1260   char *res_name;
1261   char *conv_name;
1262   uint32_t name_len;
1263   int res;
1264
1265   name_len = ntohl (ll_msg->label_len);
1266   name_tmp = (const char *) &ll_msg[1];
1267   GNUNET_SERVICE_client_continue (nc->client);
1268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269               "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1270               name_tmp);
1271
1272   conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1273   if (NULL == conv_name)
1274   {
1275     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1276                 "Error converting name `%s'\n",
1277                 name_tmp);
1278     GNUNET_SERVICE_client_drop (nc->client);
1279     return;
1280   }
1281   rlc.label = conv_name;
1282   rlc.found = GNUNET_NO;
1283   rlc.res_rd_count = 0;
1284   rlc.res_rd = NULL;
1285   rlc.rd_ser_len = 0;
1286   rlc.nick = get_nick_record (&ll_msg->zone);
1287   res = GSN_database->lookup_records (GSN_database->cls,
1288                                       &ll_msg->zone,
1289                                       conv_name,
1290                                       &lookup_it,
1291                                       &rlc);
1292   GNUNET_free (conv_name);
1293   env = GNUNET_MQ_msg_extra (llr_msg,
1294                              name_len + rlc.rd_ser_len,
1295                              GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1296   llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1297   llr_msg->private_key = ll_msg->zone;
1298   llr_msg->name_len = htons (name_len);
1299   llr_msg->rd_count = htons (rlc.res_rd_count);
1300   llr_msg->rd_len = htons (rlc.rd_ser_len);
1301   res_name = (char *) &llr_msg[1];
1302   if  ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1303     llr_msg->found = ntohs (GNUNET_YES);
1304   else
1305     llr_msg->found = ntohs (GNUNET_NO);
1306   GNUNET_memcpy (&llr_msg[1],
1307                  name_tmp,
1308                  name_len);
1309   GNUNET_memcpy (&res_name[name_len],
1310                  rlc.res_rd,
1311                  rlc.rd_ser_len);
1312   GNUNET_MQ_send (nc->mq,
1313                   env);
1314   GNUNET_free_non_null (rlc.res_rd);
1315 }
1316
1317
1318 /**
1319  * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1320  *
1321  * @param cls client sending the message
1322  * @param rp_msg message of type `struct RecordStoreMessage`
1323  * @return #GNUNET_OK if @a rp_msg is well-formed
1324  */
1325 static int
1326 check_record_store (void *cls,
1327                     const struct RecordStoreMessage *rp_msg)
1328 {
1329   size_t name_len;
1330   size_t msg_size;
1331   size_t msg_size_exp;
1332   size_t rd_ser_len;
1333   const char *name_tmp;
1334
1335   (void) cls;
1336   name_len = ntohs (rp_msg->name_len);
1337   msg_size = ntohs (rp_msg->gns_header.header.size);
1338   rd_ser_len = ntohs (rp_msg->rd_len);
1339   msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1340   if (msg_size != msg_size_exp)
1341   {
1342     GNUNET_break (0);
1343     return GNUNET_SYSERR;
1344   }
1345   if ( (0 == name_len) ||
1346        (name_len > MAX_NAME_LEN) )
1347   {
1348     GNUNET_break (0);
1349     return GNUNET_SYSERR;
1350   }
1351   name_tmp = (const char *) &rp_msg[1];
1352   if ('\0' != name_tmp[name_len -1])
1353   {
1354     GNUNET_break (0);
1355     return GNUNET_SYSERR;
1356   }
1357   return GNUNET_OK;
1358 }
1359
1360
1361 /**
1362  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1363  *
1364  * @param cls client sending the message
1365  * @param rp_msg message of type `struct RecordStoreMessage`
1366  */
1367 static void
1368 handle_record_store (void *cls,
1369                      const struct RecordStoreMessage *rp_msg)
1370 {
1371   struct NamestoreClient *nc = cls;
1372   size_t name_len;
1373   size_t rd_ser_len;
1374   uint32_t rid;
1375   const char *name_tmp;
1376   char *conv_name;
1377   const char *rd_ser;
1378   unsigned int rd_count;
1379   int res;
1380   struct StoreActivity *sa;
1381
1382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383               "Received NAMESTORE_RECORD_STORE message\n");
1384   rid = ntohl (rp_msg->gns_header.r_id);
1385   name_len = ntohs (rp_msg->name_len);
1386   rd_count = ntohs (rp_msg->rd_count);
1387   rd_ser_len = ntohs (rp_msg->rd_len);
1388   GNUNET_break (0 == ntohs (rp_msg->reserved));
1389   name_tmp = (const char *) &rp_msg[1];
1390   rd_ser = &name_tmp[name_len];
1391   {
1392     struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1393
1394     if (GNUNET_OK !=
1395         GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1396                                               rd_ser,
1397                                               rd_count,
1398                                               rd))
1399     {
1400       GNUNET_break (0);
1401       GNUNET_SERVICE_client_drop (nc->client);
1402       return;
1403     }
1404
1405     /* Extracting and converting private key */
1406     conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1407     if (NULL == conv_name)
1408     {
1409       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1410                   "Error converting name `%s'\n",
1411                   name_tmp);
1412       GNUNET_SERVICE_client_drop (nc->client);
1413       return;
1414     }
1415     GNUNET_STATISTICS_update (statistics,
1416                               "Well-formed store requests received",
1417                               1,
1418                               GNUNET_NO);
1419     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420                 "Creating %u records for name `%s'\n",
1421                 (unsigned int) rd_count,
1422                 conv_name);
1423     if ( (0 == rd_count) &&
1424          (GNUNET_NO ==
1425           GSN_database->lookup_records (GSN_database->cls,
1426                                         &rp_msg->private_key,
1427                                         conv_name,
1428                                         NULL,
1429                                         0)) )
1430     {
1431       /* This name does not exist, so cannot be removed */
1432       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433                   "Name `%s' does not exist, no deletion required\n",
1434                   conv_name);
1435       res = GNUNET_NO;
1436     }
1437     else
1438     {
1439       /* remove "NICK" records, unless this is for the
1440          #GNUNET_GNS_EMPTY_LABEL_AT label */
1441       struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1442       unsigned int rd_clean_off;
1443
1444       rd_clean_off = 0;
1445       for (unsigned int i=0;i<rd_count;i++)
1446       {
1447         rd_clean[rd_clean_off] = rd[i];
1448         if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1449                            conv_name)) ||
1450              (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1451           rd_clean_off++;
1452       }
1453       res = GSN_database->store_records (GSN_database->cls,
1454                                          &rp_msg->private_key,
1455                                          conv_name,
1456                                          rd_clean_off,
1457                                          rd_clean);
1458     }
1459
1460     if (GNUNET_OK != res)
1461     {
1462       /* store not successful, not need to tell monitors */
1463       send_store_response (nc,
1464                            res,
1465                            rid);
1466       GNUNET_SERVICE_client_continue (nc->client);
1467       GNUNET_free (conv_name);
1468       return;
1469     }
1470
1471     sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1472                         ntohs (rp_msg->gns_header.header.size));
1473     GNUNET_CONTAINER_DLL_insert (sa_head,
1474                                  sa_tail,
1475                                  sa);
1476     sa->nc = nc;
1477     sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1478     GNUNET_memcpy (&sa[1],
1479                    rp_msg,
1480                    ntohs (rp_msg->gns_header.header.size));
1481     sa->zm_pos = monitor_head;
1482     sa->conv_name = conv_name;
1483     continue_store_activity (sa);
1484   }
1485 }
1486
1487
1488 /**
1489  * Context for record remove operations passed from #handle_zone_to_name to
1490  * #handle_zone_to_name_it as closure
1491  */
1492 struct ZoneToNameCtx
1493 {
1494   /**
1495    * Namestore client
1496    */
1497   struct NamestoreClient *nc;
1498
1499   /**
1500    * Request id (to be used in the response to the client).
1501    */
1502   uint32_t rid;
1503
1504   /**
1505    * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error.  Note that
1506    * not finding a name for the zone still counts as a 'success' here,
1507    * as this field is about the success of executing the IPC protocol.
1508    */
1509   int success;
1510 };
1511
1512
1513 /**
1514  * Zone to name iterator
1515  *
1516  * @param cls struct ZoneToNameCtx *
1517  * @param seq sequence number of the record
1518  * @param zone_key the zone key
1519  * @param name name
1520  * @param rd_count number of records in @a rd
1521  * @param rd record data
1522  */
1523 static void
1524 handle_zone_to_name_it (void *cls,
1525                         uint64_t seq,
1526                         const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1527                         const char *name,
1528                         unsigned int rd_count,
1529                         const struct GNUNET_GNSRECORD_Data *rd)
1530 {
1531   struct ZoneToNameCtx *ztn_ctx = cls;
1532   struct GNUNET_MQ_Envelope *env;
1533   struct ZoneToNameResponseMessage *ztnr_msg;
1534   int16_t res;
1535   size_t name_len;
1536   ssize_t rd_ser_len;
1537   size_t msg_size;
1538   char *name_tmp;
1539   char *rd_tmp;
1540
1541   (void) seq;
1542   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543               "Found result for zone-to-name lookup: `%s'\n",
1544               name);
1545   res = GNUNET_YES;
1546   name_len = (NULL == name) ? 0 : strlen (name) + 1;
1547   rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1548                                                   rd);
1549   if (rd_ser_len < 0)
1550   {
1551     GNUNET_break (0);
1552     ztn_ctx->success = GNUNET_SYSERR;
1553     return;
1554   }
1555   msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1556   if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1557   {
1558     GNUNET_break (0);
1559     ztn_ctx->success = GNUNET_SYSERR;
1560     return;
1561   }
1562   env = GNUNET_MQ_msg_extra (ztnr_msg,
1563                              name_len + rd_ser_len,
1564                              GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1565   ztnr_msg->gns_header.header.size = htons (msg_size);
1566   ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1567   ztnr_msg->res = htons (res);
1568   ztnr_msg->rd_len = htons (rd_ser_len);
1569   ztnr_msg->rd_count = htons (rd_count);
1570   ztnr_msg->name_len = htons (name_len);
1571   ztnr_msg->zone = *zone_key;
1572   name_tmp = (char *) &ztnr_msg[1];
1573   GNUNET_memcpy (name_tmp,
1574                  name,
1575                  name_len);
1576   rd_tmp = &name_tmp[name_len];
1577   GNUNET_assert (rd_ser_len ==
1578                  GNUNET_GNSRECORD_records_serialize (rd_count,
1579                                                      rd,
1580                                                      rd_ser_len,
1581                                                      rd_tmp));
1582   ztn_ctx->success = GNUNET_OK;
1583   GNUNET_MQ_send (ztn_ctx->nc->mq,
1584                   env);
1585 }
1586
1587
1588 /**
1589  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1590  *
1591  * @param cls client client sending the message
1592  * @param ztn_msg message of type 'struct ZoneToNameMessage'
1593  */
1594 static void
1595 handle_zone_to_name (void *cls,
1596                      const struct ZoneToNameMessage *ztn_msg)
1597 {
1598   struct NamestoreClient *nc = cls;
1599   struct ZoneToNameCtx ztn_ctx;
1600   struct GNUNET_MQ_Envelope *env;
1601   struct ZoneToNameResponseMessage *ztnr_msg;
1602
1603   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604               "Received ZONE_TO_NAME message\n");
1605   ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1606   ztn_ctx.nc = nc;
1607   ztn_ctx.success = GNUNET_NO;
1608   if (GNUNET_SYSERR ==
1609       GSN_database->zone_to_name (GSN_database->cls,
1610                                   &ztn_msg->zone,
1611                                   &ztn_msg->value_zone,
1612                                   &handle_zone_to_name_it, &ztn_ctx))
1613   {
1614     /* internal error, hang up instead of signalling something
1615        that might be wrong */
1616     GNUNET_break (0);
1617     GNUNET_SERVICE_client_drop (nc->client);
1618     return;
1619   }
1620   if (GNUNET_NO == ztn_ctx.success)
1621   {
1622     /* no result found, send empty response */
1623     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624                 "Found no result for zone-to-name lookup.\n");
1625     env = GNUNET_MQ_msg (ztnr_msg,
1626                          GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1627     ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1628     ztnr_msg->res = htons (GNUNET_NO);
1629     GNUNET_MQ_send (nc->mq,
1630                     env);
1631   }
1632   GNUNET_SERVICE_client_continue (nc->client);
1633 }
1634
1635
1636 /**
1637  * Context for record remove operations passed from
1638  * #run_zone_iteration_round to #zone_iterate_proc as closure
1639  */
1640 struct ZoneIterationProcResult
1641 {
1642   /**
1643    * The zone iteration handle
1644    */
1645   struct ZoneIteration *zi;
1646
1647   /**
1648    * Number of results left to be returned in this iteration.
1649    */
1650   uint64_t limit;
1651
1652 };
1653
1654
1655 /**
1656  * Process results for zone iteration from database
1657  *
1658  * @param cls struct ZoneIterationProcResult
1659  * @param seq sequence number of the record
1660  * @param zone_key the zone key
1661  * @param name name
1662  * @param rd_count number of records for this name
1663  * @param rd record data
1664  */
1665 static void
1666 zone_iterate_proc (void *cls,
1667                    uint64_t seq,
1668                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1669                    const char *name,
1670                    unsigned int rd_count,
1671                    const struct GNUNET_GNSRECORD_Data *rd)
1672 {
1673   struct ZoneIterationProcResult *proc = cls;
1674   int do_refresh_block;
1675
1676   if ( (NULL == zone_key) &&
1677        (NULL == name) )
1678   {
1679     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680                 "Iteration done\n");
1681     return;
1682   }
1683   if ( (NULL == zone_key) ||
1684        (NULL == name) )
1685   {
1686     /* what is this!? should never happen */
1687     GNUNET_break (0);
1688     return;
1689   }
1690   if (0 == proc->limit)
1691   {
1692     /* what is this!? should never happen */
1693     GNUNET_break (0);
1694     return;
1695   }
1696   proc->limit--;
1697   proc->zi->seq = seq;
1698   send_lookup_response (proc->zi->nc,
1699                         proc->zi->request_id,
1700                         zone_key,
1701                         name,
1702                         rd_count,
1703                         rd);
1704
1705
1706   do_refresh_block = GNUNET_NO;
1707   for (unsigned int i=0;i<rd_count;i++)
1708     if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1709     {
1710       do_refresh_block = GNUNET_YES;
1711       break;
1712     }
1713   if (GNUNET_YES == do_refresh_block)
1714     refresh_block (NULL,
1715                    0,
1716                    zone_key,
1717                    name,
1718                    rd_count,
1719                    rd);
1720 }
1721
1722
1723 /**
1724  * Perform the next round of the zone iteration.
1725  *
1726  * @param zi zone iterator to process
1727  * @param limit number of results to return in one pass
1728  */
1729 static void
1730 run_zone_iteration_round (struct ZoneIteration *zi,
1731                           uint64_t limit)
1732 {
1733   struct ZoneIterationProcResult proc;
1734   struct GNUNET_MQ_Envelope *env;
1735   struct GNUNET_NAMESTORE_Header *em;
1736   struct GNUNET_TIME_Absolute start;
1737   struct GNUNET_TIME_Relative duration;
1738
1739   memset (&proc,
1740           0,
1741           sizeof (proc));
1742   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743               "Asked to return up to %llu records at position %llu\n",
1744               (unsigned long long) limit,
1745               (unsigned long long) zi->seq);
1746   proc.zi = zi;
1747   proc.limit = limit;
1748   start = GNUNET_TIME_absolute_get ();
1749   GNUNET_break (GNUNET_SYSERR !=
1750                 GSN_database->iterate_records (GSN_database->cls,
1751                                                (0 == memcmp (&zi->zone,
1752                                                              &zero,
1753                                                              sizeof (zero)))
1754                                                ? NULL
1755                                                : &zi->zone,
1756                                                zi->seq,
1757                                                limit,
1758                                                &zone_iterate_proc,
1759                                                &proc));
1760   duration = GNUNET_TIME_absolute_get_duration (start);
1761   duration = GNUNET_TIME_relative_divide (duration,
1762                                           limit - proc.limit);
1763   GNUNET_STATISTICS_set (statistics,
1764                          "NAMESTORE iteration delay (μs/record)",
1765                          duration.rel_value_us,
1766                          GNUNET_NO);
1767   if (0 == proc.limit)
1768   {
1769     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770                 "Returned %llu results, more results available\n",
1771                 (unsigned long long) limit);
1772     return; /* more results later after we get the
1773                #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message */
1774   }
1775   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776               "Completed iteration after %llu/%llu results\n",
1777               (unsigned long long) (limit - proc.limit),
1778               (unsigned long long) limit);
1779   /* send empty response to indicate end of list */
1780   env = GNUNET_MQ_msg (em,
1781                        GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
1782   em->r_id = htonl (zi->request_id);
1783   GNUNET_MQ_send (zi->nc->mq,
1784                   env);
1785   GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1786                                zi->nc->op_tail,
1787                                zi);
1788   GNUNET_free (zi);
1789 }
1790
1791
1792 /**
1793  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1794  *
1795  * @param cls the client sending the message
1796  * @param zis_msg message from the client
1797  */
1798 static void
1799 handle_iteration_start (void *cls,
1800                         const struct ZoneIterationStartMessage *zis_msg)
1801 {
1802   struct NamestoreClient *nc = cls;
1803   struct ZoneIteration *zi;
1804
1805   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806               "Received ZONE_ITERATION_START message\n");
1807   zi = GNUNET_new (struct ZoneIteration);
1808   zi->request_id = ntohl (zis_msg->gns_header.r_id);
1809   zi->offset = 0;
1810   zi->nc = nc;
1811   zi->zone = zis_msg->zone;
1812
1813   GNUNET_CONTAINER_DLL_insert (nc->op_head,
1814                                nc->op_tail,
1815                                zi);
1816   run_zone_iteration_round (zi,
1817                             1);
1818   GNUNET_SERVICE_client_continue (nc->client);
1819 }
1820
1821
1822 /**
1823  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1824  *
1825  * @param cls the client sending the message
1826  * @param zis_msg message from the client
1827  */
1828 static void
1829 handle_iteration_stop (void *cls,
1830                        const struct ZoneIterationStopMessage *zis_msg)
1831 {
1832   struct NamestoreClient *nc = cls;
1833   struct ZoneIteration *zi;
1834   uint32_t rid;
1835
1836   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1837               "Received ZONE_ITERATION_STOP message\n");
1838   rid = ntohl (zis_msg->gns_header.r_id);
1839   for (zi = nc->op_head; NULL != zi; zi = zi->next)
1840     if (zi->request_id == rid)
1841       break;
1842   if (NULL == zi)
1843   {
1844     GNUNET_break (0);
1845     GNUNET_SERVICE_client_drop (nc->client);
1846     return;
1847   }
1848   GNUNET_CONTAINER_DLL_remove (nc->op_head,
1849                                nc->op_tail,
1850                                zi);
1851   GNUNET_free (zi);
1852   GNUNET_SERVICE_client_continue (nc->client);
1853 }
1854
1855
1856 /**
1857  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1858  *
1859  * @param cls the client sending the message
1860  * @param message message from the client
1861  */
1862 static void
1863 handle_iteration_next (void *cls,
1864                        const struct ZoneIterationNextMessage *zis_msg)
1865 {
1866   struct NamestoreClient *nc = cls;
1867   struct ZoneIteration *zi;
1868   uint32_t rid;
1869   uint64_t limit;
1870
1871   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872               "Received ZONE_ITERATION_NEXT message\n");
1873   GNUNET_STATISTICS_update (statistics,
1874                             "Iteration NEXT messages received",
1875                             1,
1876                             GNUNET_NO);
1877   rid = ntohl (zis_msg->gns_header.r_id);
1878   limit = GNUNET_ntohll (zis_msg->limit);
1879   for (zi = nc->op_head; NULL != zi; zi = zi->next)
1880     if (zi->request_id == rid)
1881       break;
1882   if (NULL == zi)
1883   {
1884     GNUNET_break (0);
1885     GNUNET_SERVICE_client_drop (nc->client);
1886     return;
1887   }
1888   run_zone_iteration_round (zi,
1889                             limit);
1890   GNUNET_SERVICE_client_continue (nc->client);
1891 }
1892
1893
1894 /**
1895  * Function called when the monitor is ready for more data, and we
1896  * should thus unblock PUT operations that were blocked on the
1897  * monitor not being ready.
1898  */
1899 static void
1900 monitor_unblock (struct ZoneMonitor *zm)
1901 {
1902   struct StoreActivity *sa = sa_head;
1903
1904   while ( (NULL != sa) &&
1905           (zm->limit > zm->iteration_cnt) )
1906   {
1907     struct StoreActivity *sn = sa->next;
1908
1909     if (sa->zm_pos == zm)
1910       continue_store_activity (sa);
1911     sa = sn;
1912   }
1913   if (zm->limit > zm->iteration_cnt)
1914   {
1915     zm->sa_waiting = GNUNET_NO;
1916     if (NULL != zm->sa_wait_warning)
1917     {
1918       GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1919       zm->sa_wait_warning = NULL;
1920     }
1921   }
1922   else if (GNUNET_YES == zm->sa_waiting)
1923   {
1924     zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1925     if (NULL != zm->sa_wait_warning)
1926       GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1927     zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1928                                                         &warn_monitor_slow,
1929                                                         zm);
1930   }
1931 }
1932
1933
1934 /**
1935  * Send 'sync' message to zone monitor, we're now in sync.
1936  *
1937  * @param zm monitor that is now in sync
1938  */
1939 static void
1940 monitor_sync (struct ZoneMonitor *zm)
1941 {
1942   struct GNUNET_MQ_Envelope *env;
1943   struct GNUNET_MessageHeader *sync;
1944
1945   env = GNUNET_MQ_msg (sync,
1946                        GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1947   GNUNET_MQ_send (zm->nc->mq,
1948                   env);
1949   /* mark iteration done */
1950   zm->in_first_iteration = GNUNET_NO;
1951   zm->iteration_cnt = 0;
1952   if ( (zm->limit > 0) &&
1953        (zm->sa_waiting) )
1954     monitor_unblock (zm);
1955 }
1956
1957
1958 /**
1959  * Obtain the next datum during the zone monitor's zone initial iteration.
1960  *
1961  * @param cls zone monitor that does its initial iteration
1962  */
1963 static void
1964 monitor_iteration_next (void *cls);
1965
1966
1967 /**
1968  * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1969  *
1970  * @param cls a 'struct ZoneMonitor *' with information about the monitor
1971  * @param seq sequence number of the record
1972  * @param zone_key zone key of the zone
1973  * @param name name
1974  * @param rd_count number of records in @a rd
1975  * @param rd array of records
1976  */
1977 static void
1978 monitor_iterate_cb (void *cls,
1979                     uint64_t seq,
1980                     const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1981                     const char *name,
1982                     unsigned int rd_count,
1983                     const struct GNUNET_GNSRECORD_Data *rd)
1984 {
1985   struct ZoneMonitor *zm = cls;
1986
1987   zm->seq = seq;
1988   GNUNET_assert (NULL != name);
1989   GNUNET_STATISTICS_update (statistics,
1990                             "Monitor notifications sent",
1991                             1,
1992                             GNUNET_NO);
1993   zm->limit--;
1994   zm->iteration_cnt--;
1995   send_lookup_response (zm->nc,
1996                         0,
1997                         zone_key,
1998                         name,
1999                         rd_count,
2000                         rd);
2001   if ( (0 == zm->iteration_cnt) &&
2002        (0 != zm->limit) )
2003   {
2004     /* We are done with the current iteration batch, AND the
2005        client would right now accept more, so go again! */
2006     GNUNET_assert (NULL == zm->task);
2007     zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2008                                          zm);
2009   }
2010 }
2011
2012
2013 /**
2014  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
2015  *
2016  * @param cls the client sending the message
2017  * @param zis_msg message from the client
2018  */
2019 static void
2020 handle_monitor_start (void *cls,
2021                       const struct ZoneMonitorStartMessage *zis_msg)
2022 {
2023   struct NamestoreClient *nc = cls;
2024   struct ZoneMonitor *zm;
2025
2026   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027               "Received ZONE_MONITOR_START message\n");
2028   zm = GNUNET_new (struct ZoneMonitor);
2029   zm->nc = nc;
2030   zm->zone = zis_msg->zone;
2031   zm->limit = 1;
2032   zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
2033   GNUNET_CONTAINER_DLL_insert (monitor_head,
2034                                monitor_tail,
2035                                zm);
2036   GNUNET_SERVICE_client_mark_monitor (nc->client);
2037   GNUNET_SERVICE_client_continue (nc->client);
2038   GNUNET_notification_context_add (monitor_nc,
2039                                    nc->mq);
2040   if (zm->in_first_iteration)
2041     zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2042                                          zm);
2043   else
2044     monitor_sync (zm);
2045 }
2046
2047
2048 /**
2049  * Obtain the next datum during the zone monitor's zone initial iteration.
2050  *
2051  * @param cls zone monitor that does its initial iteration
2052  */
2053 static void
2054 monitor_iteration_next (void *cls)
2055 {
2056   struct ZoneMonitor *zm = cls;
2057   int ret;
2058
2059   zm->task = NULL;
2060   GNUNET_assert (0 == zm->iteration_cnt);
2061   if (zm->limit > 16)
2062     zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2063   else
2064     zm->iteration_cnt = zm->limit; /* use it all */
2065   ret = GSN_database->iterate_records (GSN_database->cls,
2066                                        (0 == memcmp (&zm->zone,
2067                                                      &zero,
2068                                                      sizeof (zero)))
2069                                        ? NULL
2070                                        : &zm->zone,
2071                                        zm->seq,
2072                                        zm->iteration_cnt,
2073                                        &monitor_iterate_cb,
2074                                        zm);
2075   if (GNUNET_SYSERR == ret)
2076   {
2077     GNUNET_SERVICE_client_drop (zm->nc->client);
2078     return;
2079   }
2080   if (GNUNET_NO == ret)
2081   {
2082     /* empty zone */
2083     monitor_sync (zm);
2084     return;
2085   }
2086 }
2087
2088
2089 /**
2090  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2091  *
2092  * @param cls the client sending the message
2093  * @param nm message from the client
2094  */
2095 static void
2096 handle_monitor_next (void *cls,
2097                      const struct ZoneMonitorNextMessage *nm)
2098 {
2099   struct NamestoreClient *nc = cls;
2100   struct ZoneMonitor *zm;
2101   uint64_t inc;
2102
2103   inc = GNUNET_ntohll (nm->limit);
2104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105               "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2106               (unsigned long long) inc);
2107   for (zm = monitor_head; NULL != zm; zm = zm->next)
2108     if (zm->nc == nc)
2109       break;
2110   if (NULL == zm)
2111   {
2112     GNUNET_break (0);
2113     GNUNET_SERVICE_client_drop (nc->client);
2114     return;
2115   }
2116   GNUNET_SERVICE_client_continue (nc->client);
2117   if (zm->limit + inc < zm->limit)
2118   {
2119     GNUNET_break (0);
2120     GNUNET_SERVICE_client_drop (nc->client);
2121     return;
2122   }
2123   zm->limit += inc;
2124   if ( (zm->in_first_iteration) &&
2125        (zm->limit == inc) )
2126   {
2127     /* We are still iterating, and the previous iteration must
2128        have stopped due to the client's limit, so continue it! */
2129     GNUNET_assert (NULL == zm->task);
2130     zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2131                                          zm);
2132   }
2133   GNUNET_assert (zm->iteration_cnt <= zm->limit);
2134   if ( (zm->limit > zm->iteration_cnt) &&
2135        (zm->sa_waiting) )
2136   {
2137     monitor_unblock (zm);
2138   }
2139   else if (GNUNET_YES == zm->sa_waiting)
2140   {
2141     if (NULL != zm->sa_wait_warning)
2142       GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2143     zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2144     zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2145                                                         &warn_monitor_slow,
2146                                                         zm);
2147   }
2148 }
2149
2150
2151 /**
2152  * Process namestore requests.
2153  *
2154  * @param cls closure
2155  * @param cfg configuration to use
2156  * @param service the initialized service
2157  */
2158 static void
2159 run (void *cls,
2160      const struct GNUNET_CONFIGURATION_Handle *cfg,
2161      struct GNUNET_SERVICE_Handle *service)
2162 {
2163   char *database;
2164
2165   (void) cls;
2166   (void) service;
2167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168               "Starting namestore service\n");
2169   cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2170                                                      "namestore",
2171                                                      "CACHE_KEYS");
2172   disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2173                                                             "namecache",
2174                                                             "DISABLE");
2175   GSN_cfg = cfg;
2176   monitor_nc = GNUNET_notification_context_create (1);
2177   if (GNUNET_YES != disable_namecache)
2178   {
2179     namecache = GNUNET_NAMECACHE_connect (cfg);
2180     GNUNET_assert (NULL != namecache);
2181   }
2182   /* Loading database plugin */
2183   if (GNUNET_OK !=
2184       GNUNET_CONFIGURATION_get_value_string (cfg,
2185                                              "namestore",
2186                                              "database",
2187                                              &database))
2188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2189                 "No database backend configured\n");
2190
2191   GNUNET_asprintf (&db_lib_name,
2192                    "libgnunet_plugin_namestore_%s",
2193                    database);
2194   GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2195                                      (void *) GSN_cfg);
2196   GNUNET_free (database);
2197   statistics = GNUNET_STATISTICS_create ("namestore",
2198                                          cfg);
2199   GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2200                                  NULL);
2201   if (NULL == GSN_database)
2202   {
2203     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2204                 "Could not load database backend `%s'\n",
2205                 db_lib_name);
2206     GNUNET_SCHEDULER_shutdown ();
2207     return;
2208   }
2209 }
2210
2211
2212 /**
2213  * Define "main" method using service macro.
2214  */
2215 GNUNET_SERVICE_MAIN
2216 ("namestore",
2217  GNUNET_SERVICE_OPTION_NONE,
2218  &run,
2219  &client_connect_cb,
2220  &client_disconnect_cb,
2221  NULL,
2222  GNUNET_MQ_hd_var_size (record_store,
2223                         GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2224                         struct RecordStoreMessage,
2225                         NULL),
2226  GNUNET_MQ_hd_var_size (record_lookup,
2227                         GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2228                         struct LabelLookupMessage,
2229                         NULL),
2230  GNUNET_MQ_hd_fixed_size (zone_to_name,
2231                           GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2232                           struct ZoneToNameMessage,
2233                           NULL),
2234  GNUNET_MQ_hd_fixed_size (iteration_start,
2235                           GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2236                           struct ZoneIterationStartMessage,
2237                           NULL),
2238  GNUNET_MQ_hd_fixed_size (iteration_next,
2239                           GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2240                           struct ZoneIterationNextMessage,
2241                           NULL),
2242  GNUNET_MQ_hd_fixed_size (iteration_stop,
2243                           GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2244                           struct ZoneIterationStopMessage,
2245                           NULL),
2246  GNUNET_MQ_hd_fixed_size (monitor_start,
2247                           GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2248                           struct ZoneMonitorStartMessage,
2249                           NULL),
2250  GNUNET_MQ_hd_fixed_size (monitor_next,
2251                           GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2252                           struct ZoneMonitorNextMessage,
2253                           NULL),
2254  GNUNET_MQ_handler_end ());
2255
2256
2257 /* end of gnunet-service-namestore.c */