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