-fix
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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  *
23  * @file gns/gnunet-service-gns.c
24  * @brief GNUnet GNS service
25  * @author Martin Schanzenbach
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_statistics_service.h"
36 #include "block_gns.h"
37 #include "gns.h"
38 #include "gnunet-service-gns_resolver.h"
39 #include "gnunet-service-gns_interceptor.h"
40
41
42 /* FIXME move to proper header in include */
43 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
45 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
46 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
47 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
48 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
49
50
51 #define INITIAL_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
52 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
53 #define DEFAULT_RECORD_PUT_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
54 #define LATE_ITERATION_SPEEDUP_FACTOR 2
55
56 /**
57  * Handle to a shorten operation from api
58  */
59 struct ClientShortenHandle
60 {
61
62   /* DLL */
63   struct ClientShortenHandle *next;
64
65   /* DLL */
66   struct ClientShortenHandle *prev;
67
68   /* the requesting client that */
69   struct GNUNET_SERVER_Client *client;
70
71   /* request id */
72   uint64_t unique_id;
73
74   /* request type */
75   enum GNUNET_GNS_RecordType type;
76
77   /* name to shorten */
78   char name[MAX_DNS_NAME_LENGTH];
79
80   /* name of private zone (relative to root) */
81   char private_zone_id[MAX_DNS_NAME_LENGTH];
82   
83   /* name of shorten zone (relative to root) */
84   char shorten_zone_id[MAX_DNS_NAME_LENGTH];
85   
86   /* root zone */
87   struct GNUNET_CRYPTO_ShortHashCode root_zone;
88
89   /* private zone */
90   struct GNUNET_CRYPTO_ShortHashCode private_zone;
91   
92   /* shorten zone */
93   struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
94
95   /* Namestore lookup task */
96   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
97
98 };
99
100
101 /**
102  * Handle to a get auhtority operation from api
103  */
104 struct ClientGetAuthHandle
105 {
106   /* the requesting client that */
107   struct GNUNET_SERVER_Client *client;
108
109   /* request id */
110   uint64_t unique_id;
111
112   /* name to lookup authority */
113   char* name;
114
115 };
116
117
118 /**
119  * Handle to a lookup operation from api
120  */
121 struct ClientLookupHandle
122 {
123
124   /* the requesting client that */
125   struct GNUNET_SERVER_Client *client;
126
127   /* The zone we look up in */
128   struct GNUNET_CRYPTO_ShortHashCode zone;
129
130   /* Do we only want to lookup from local cache? */
131   int only_cached;
132
133   /* request id */
134   uint64_t unique_id;
135
136   /* request type */
137   enum GNUNET_GNS_RecordType type;
138
139   /* optional zone private key used for shorten */
140   struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
141
142   /* the name to look up */
143   char* name; //Needed?
144 };
145
146 /**
147  * Our handle to the DHT
148  */
149 static struct GNUNET_DHT_Handle *dht_handle;
150
151 /**
152  * Our zone's private key
153  */
154 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
155
156 /**
157  * Our handle to the namestore service
158  * FIXME maybe need a second handle for iteration
159  */
160 struct GNUNET_NAMESTORE_Handle *namestore_handle;
161
162 /**
163  * Handle to iterate over our authoritative zone in namestore
164  */
165 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
166
167 /**
168  * The configuration the GNS service is running with
169  */
170 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
171
172 /**
173  * Our notification context.
174  */
175 static struct GNUNET_SERVER_NotificationContext *nc;
176
177 /**
178  * Our zone hash
179  */
180 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
181
182 /**
183  * Useful for zone update for DHT put
184  */
185 static unsigned long long num_public_records;
186
187 /**
188  * Last seen record count
189  */
190 static unsigned long long last_num_public_records;
191
192 static struct GNUNET_TIME_Relative zone_iteration_interval;
193
194 /* dht update interval FIXME define? */
195 static struct GNUNET_TIME_Relative record_put_interval;
196
197 /* zone update task */
198 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
199
200 /* automatic pkey import for name shortening */
201 static int auto_import_pkey;
202
203 /* first zone iteration is specia */
204 static int first_zone_iteration;
205
206 /* lookup timeout */
207 static struct GNUNET_TIME_Relative default_lookup_timeout;
208
209 /* ipv6 support */
210 static int v6_enabled;
211
212 /* ipv4 support */
213 static int v4_enabled;
214
215 /* Shorten DLL for cancelling NS requests */
216 static struct ClientShortenHandle *csh_head;
217
218 /* Shorten DLL for cancelling NS requests */
219 static struct ClientShortenHandle *csh_tail;
220
221 /* Statistics handle */
222 static struct GNUNET_STATISTICS_Handle *statistics;
223
224 /**
225  * Send shorten response back to client
226  * 
227  * @param cls the closure containing a client shorten handle
228  * @param name the shortened name result or NULL if cannot be shortened
229  */
230 static void
231 send_shorten_response(void* cls, const char* name);
232
233
234 /**
235  * Continue shutdown
236  */
237 static void
238 on_resolver_cleanup(void)
239 {
240   if (NULL != statistics)
241     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
242
243   if (NULL != namestore_iter)
244     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
245   GNUNET_NAMESTORE_disconnect(namestore_handle);
246   GNUNET_DHT_disconnect(dht_handle);
247 }
248
249 /**
250  * Task run during shutdown.
251  *
252  * @param cls unused
253  * @param tc unused
254  */
255 static void
256 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
257 {
258   struct ClientShortenHandle *csh_tmp = csh_head;
259
260   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
261              "Shutting down!");
262   
263   while (csh_tmp != NULL)
264   {
265     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
266     send_shorten_response (csh_tmp, csh_tmp->name);
267   }
268   
269   GNUNET_SERVER_notification_context_destroy (nc);
270   
271   gns_interceptor_stop();
272   gns_resolver_cleanup(&on_resolver_cleanup);
273   /* Kill zone task for it may make the scheduler hang */
274   if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
275     GNUNET_SCHEDULER_cancel(zone_update_taskid);
276 }
277
278
279 /**
280  * Method called periodicattluy that triggers
281  * iteration over root zone
282  *
283  * @param cls closure
284  * @param tc task context
285  */
286 static void
287 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 {
289   zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
290   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
291 }
292
293 /**
294  * Continuation for DHT put
295  *
296  * @param cls closure
297  * @param success GNUNET_OK if the PUT was transmitted,
298  *                GNUNET_NO on timeout,
299  *                GNUNET_SYSERR on disconnect from service
300  *                after the PUT message was transmitted
301  *                (so we don't know if it was received or not)
302  */
303 static void
304 record_dht_put(void *cls, int success)
305 {
306   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
307 }
308
309 /* prototype */
310 static void
311 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
312
313 /**
314  * Function used to put all records successively into the DHT.
315  *
316  * @param cls the closure (NULL)
317  * @param key the public key of the authority (ours)
318  * @param expiration lifetime of the namestore entry
319  * @param name the name of the records
320  * @param rd_count the number of records in data
321  * @param rd the record data
322  * @param signature the signature for the record data
323  */
324 static void
325 put_gns_record(void *cls,
326                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
327                 struct GNUNET_TIME_Absolute expiration,
328                 const char *name,
329                 unsigned int rd_count,
330                 const struct GNUNET_NAMESTORE_RecordData *rd,
331                 const struct GNUNET_CRYPTO_RsaSignature *signature)
332 {
333   
334   struct GNSNameRecordBlock *nrb;
335   struct GNUNET_CRYPTO_ShortHashCode name_hash;
336   struct GNUNET_CRYPTO_ShortHashCode zhash;
337   struct GNUNET_HashCode xor_hash;
338   struct GNUNET_HashCode name_hash_double;
339   struct GNUNET_HashCode zone_hash_double;
340   uint32_t rd_payload_length;
341   char* nrb_data = NULL;
342   size_t namelen;
343   struct GNUNET_TIME_Relative next_put_interval;
344
345   
346
347   /* we're done */
348   if (NULL == name)
349   {
350     first_zone_iteration = GNUNET_NO;
351     if (0 == num_public_records)
352     {
353       /**
354        * If no records are known (startup) or none present
355        * we can safely set the interval to the value for a single
356        * record
357        */
358       zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
359                                                              1);
360
361       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
362                  "No records in db.\n");
363     }
364     else
365     {
366       zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
367                                                              num_public_records);
368     }
369
370     zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
371                                                         zone_iteration_interval);
372
373     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
374                "Adjusted zone iteration interval to %llus!\n",
375                zone_iteration_interval.rel_value);
376     GNUNET_STATISTICS_set (statistics,
377                            "Current zone iteration interval [msec]",
378                            zone_iteration_interval.rel_value,
379                            GNUNET_NO);
380     
381     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
382                "Zone iteration finished. Rescheduling zone iteration\n");
383
384     namestore_iter = NULL;
385     if (num_public_records == 0)
386       zone_update_taskid = GNUNET_SCHEDULER_add_delayed (zone_iteration_interval,
387                                                          &update_zone_dht_start,
388                                                          NULL);
389     else
390       zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
391     GNUNET_STATISTICS_update (statistics,
392                               "Number of zone iterations", 1, GNUNET_NO);
393
394     last_num_public_records = num_public_records;
395     GNUNET_STATISTICS_set (statistics,
396                            "Number of public records in DHT",
397                            last_num_public_records,
398                            GNUNET_NO);
399     return;
400   }
401   
402   namelen = strlen(name) + 1;
403
404   if (rd_count == 0)
405   {
406     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
407                "No records given for name %s! Skipping...\n",
408                name);
409     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
410                                                    NULL);
411     return;
412   }
413   
414   if (signature == NULL)
415   {
416     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
417                "No signature for %s record data provided! Skipping...\n",
418                name);
419     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
420                                                    NULL);
421     return;
422
423   }
424   
425   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
426              "Putting records for %s into the DHT\n", name);
427   
428   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
429   
430   nrb = GNUNET_malloc(rd_payload_length + namelen
431                       + sizeof(struct GNSNameRecordBlock));
432   
433   nrb->signature = *signature;
434   
435   nrb->public_key = *key;
436
437   nrb->rd_count = htonl(rd_count);
438   
439   memcpy(&nrb[1], name, namelen);
440
441   nrb_data = (char*)&nrb[1];
442   nrb_data += namelen;
443
444   rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
445
446   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
447                                                 rd,
448                                                 rd_payload_length,
449                                                 nrb_data))
450   {
451     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
452                "Record serialization failed! Skipping...\n");
453     GNUNET_free(nrb);
454     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
455                                                    NULL);
456     return;
457   }
458
459
460   /*
461    * calculate DHT key: H(name) xor H(pubkey)
462    */
463   GNUNET_CRYPTO_short_hash(key,
464                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
465                      &zhash);
466   GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
467   GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
468   GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
469   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
470
471   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
472              "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
473
474   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
475              "putting %d records for %s under key: %s with size %d\n",
476              rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
477   
478   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
479              "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
480
481   GNUNET_STATISTICS_update (statistics,
482                             "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
483
484   /* FIXME: keep return value to possibly cancel? */
485   GNUNET_DHT_put (dht_handle, &xor_hash,
486                   DHT_GNS_REPLICATION_LEVEL,
487                   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
488                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
489                   rd_payload_length,
490                   (char*)nrb,
491                   expiration,
492                   DHT_OPERATION_TIMEOUT,
493                   &record_dht_put,
494                   NULL); //cls for cont
495   
496   num_public_records++;
497   
498   if ((num_public_records > last_num_public_records)
499       && (first_zone_iteration == GNUNET_NO))
500   {
501     zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
502                                                            num_public_records);
503     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
504                "Last record count was lower than current record count... increasing.\n");
505     next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
506                                                  LATE_ITERATION_SPEEDUP_FACTOR);
507
508   }
509   else
510     next_put_interval = zone_iteration_interval;
511
512   GNUNET_STATISTICS_set (statistics,
513                             "Current zone iteration interval [msec]",
514                             next_put_interval.rel_value,
515                             GNUNET_NO);
516   
517   /**
518    * Reschedule periodic put
519    */
520   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
521                                 &update_zone_dht_next,
522                                 NULL);
523
524   GNUNET_free(nrb);
525
526 }
527
528 /**
529  * Periodically iterate over our zone and store everything in dht
530  *
531  * @param cls NULL
532  * @param tc task context
533  */
534 static void
535 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
536 {
537   zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
538
539   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
540   
541   /* start counting again */
542   num_public_records = 0;
543   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
544                                                  NULL, //All zones
545                                                  GNUNET_NAMESTORE_RF_AUTHORITY,
546                                                  GNUNET_NAMESTORE_RF_PRIVATE,
547                                                  &put_gns_record,
548                                                  NULL);
549 }
550
551 /* END DHT ZONE PROPAGATION */
552
553 /**
554  * Send shorten response back to client
555  * 
556  * @param cls the closure containing a client shorten handle
557  * @param name the shortened name result or NULL if cannot be shortened
558  */
559 static void
560 send_shorten_response(void* cls, const char* name)
561 {
562   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
563               "SHORTEN_RESULT", name);
564   struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
565   struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
566   
567   if (name == NULL)
568   {
569     name = "";
570   }
571
572   GNUNET_STATISTICS_update (statistics,
573                             "Name shorten results", 1, GNUNET_NO);
574
575   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
576                        + strlen(name) + 1);
577   
578   rmsg->id = csh->unique_id;
579   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
580   rmsg->header.size = 
581     htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
582           strlen(name) + 1);
583
584   strcpy((char*)&rmsg[1], name);
585
586   GNUNET_SERVER_notification_context_unicast (nc, csh->client,
587                               (const struct GNUNET_MessageHeader *) rmsg,
588                               GNUNET_NO);
589   GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
590
591   if (NULL != csh->namestore_task)
592     GNUNET_NAMESTORE_cancel (csh->namestore_task);
593   
594   GNUNET_free(rmsg);
595   GNUNET_free(csh);
596
597 }
598
599
600 /**
601  * Lookup the zone infos and shorten name
602  *
603  * @param cls the client shorten handle
604  * @param key key of the zone
605  * @param expiration expiration of record
606  * @param name name found or null if no result
607  * @param rd_count number of records found
608  * @param rd record data
609  * @param signature
610  *
611  */
612 static void
613 process_shorten_in_private_zone_lookup (void *cls,
614                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
615                       struct GNUNET_TIME_Absolute expiration,
616                       const char *name,
617                       unsigned int rd_count,
618                       const struct GNUNET_NAMESTORE_RecordData *rd,
619                       const struct GNUNET_CRYPTO_RsaSignature *signature)
620 {
621   struct ClientShortenHandle *csh = cls;
622   csh->namestore_task = NULL;
623   struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
624   struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
625
626   if (0 == strcmp (csh->private_zone_id, ""))
627     pzone = NULL;
628   
629   if (rd_count == 0)
630   {
631     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                 "No shorten zone in private zone!\n");
633
634     strcpy (csh->shorten_zone_id, "");
635     szone = NULL;
636
637   }
638   else
639   {
640
641     GNUNET_assert (rd_count == 1);
642
643     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644                 "Shorten zone %s found in private zone %s\n",
645                 name, csh->private_zone_id);
646
647     sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
648   }
649   
650   GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
651
652   gns_resolver_shorten_name (&csh->root_zone,
653                              pzone,
654                              szone,
655                              csh->name,
656                              csh->private_zone_id,
657                              csh->shorten_zone_id,
658                              &send_shorten_response, csh);
659
660 }
661
662
663 /**
664  * Lookup the zone infos and shorten name
665  *
666  * @param cls the shorten handle
667  * @param key key of the zone
668  * @param expiration expiration of record
669  * @param name name found or null if no result
670  * @param rd_count number of records found
671  * @param rd record data
672  * @param signature
673  *
674  */
675 static void
676 process_shorten_in_root_zone_lookup (void *cls,
677                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
678                       struct GNUNET_TIME_Absolute expiration,
679                       const char *name,
680                       unsigned int rd_count,
681                       const struct GNUNET_NAMESTORE_RecordData *rd,
682                       const struct GNUNET_CRYPTO_RsaSignature *signature)
683 {
684   struct ClientShortenHandle *csh = cls;
685   csh->namestore_task = NULL;
686   struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
687   struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
688   
689   if (0 == strcmp (csh->private_zone_id, ""))
690     pzone = NULL;
691
692   if (rd_count == 0)
693   {
694     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695                 "No shorten zone in zone and no private zone!\n");
696
697     strcpy (csh->shorten_zone_id, "");
698
699     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
700     szone = NULL;
701
702     gns_resolver_shorten_name (&csh->root_zone,
703                                pzone,
704                                szone,
705                                csh->name,
706                                csh->private_zone_id,
707                                csh->shorten_zone_id,
708                                &send_shorten_response, csh);
709     return;
710   }
711
712   GNUNET_assert (rd_count == 1);
713
714   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715               "Private zone %s found in root zone\n", name);
716
717   strcpy (csh->private_zone_id, name);
718
719   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
720                                   pzone,
721                                   szone,
722                                   &process_shorten_in_private_zone_lookup,
723                                   csh);
724 }
725
726
727 /**
728  * Lookup the zone infos and shorten name
729  *
730  * @param cls the shorten handle
731  * @param key key of the zone
732  * @param expiration expiration of record
733  * @param name name found or null if no result
734  * @param rd_count number of records found
735  * @param rd record data
736  * @param signature
737  *
738  */
739 static void
740 process_private_in_root_zone_lookup (void *cls,
741                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
742                       struct GNUNET_TIME_Absolute expiration,
743                       const char *name,
744                       unsigned int rd_count,
745                       const struct GNUNET_NAMESTORE_RecordData *rd,
746                       const struct GNUNET_CRYPTO_RsaSignature *signature)
747 {
748   struct ClientShortenHandle *csh = cls;
749   csh->namestore_task = NULL;
750
751   if (rd_count == 0)
752   {
753     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754                 "No private zone in root zone\n");
755
756     strcpy (csh->private_zone_id, "");
757   
758     csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
759                                   &csh->root_zone,
760                                   &csh->shorten_zone,
761                                   &process_shorten_in_root_zone_lookup,
762                                   csh);
763     return;
764   }
765
766   GNUNET_assert (rd_count == 1);
767
768   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769               "Private zone %s found in root zone\n", name);
770
771   strcpy (csh->private_zone_id, name);
772
773   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
774                                   &csh->private_zone,
775                                   &csh->shorten_zone,
776                                   &process_shorten_in_private_zone_lookup,
777                                   csh);
778 }
779
780 /**
781  * Lookup the zone infos and shorten name
782  *
783  * @param csh the shorten handle
784  *
785  */
786 static void
787 start_shorten_name (struct ClientShortenHandle *csh)
788 {
789   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
790               "Looking for private zone name in root zone\n");
791
792   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
793                                   &csh->root_zone,
794                                   &csh->private_zone,
795                                   &process_private_in_root_zone_lookup,
796                                   csh);
797 }
798
799
800 /**
801  * Handle a shorten message from the api
802  *
803  * @param cls the closure
804  * @param client the client
805  * @param message the message
806  */
807 static void handle_shorten (void *cls,
808                             struct GNUNET_SERVER_Client * client,
809                             const struct GNUNET_MessageHeader * message)
810 {
811   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
812
813   size_t msg_size = 0;
814   struct ClientShortenHandle *csh;
815   char name[MAX_DNS_NAME_LENGTH];
816   char* nameptr = name;
817
818   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
819   {
820     GNUNET_break_op (0);
821     GNUNET_SERVER_receive_done (client, GNUNET_OK);
822     return;
823   }
824
825
826   struct GNUNET_GNS_ClientShortenMessage *sh_msg =
827     (struct GNUNET_GNS_ClientShortenMessage *) message;
828   
829   msg_size = ntohs(message->size);
830
831   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
832   {
833     GNUNET_break_op (0);
834     GNUNET_SERVER_receive_done (client, GNUNET_OK);
835     return;
836   }
837
838   csh = GNUNET_malloc(sizeof (struct ClientShortenHandle));
839   csh->client = client;
840   csh->unique_id = sh_msg->id;
841
842   GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
843   
844   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
845   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
846                "SHORTEN: Converted %s to %s\n", (char*)&sh_msg[1], nameptr);
847   GNUNET_SERVER_notification_context_add (nc, client);
848   
849   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
850     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
851                "SHORTEN: %s is too short\n", name);
852     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
853     send_shorten_response(csh, name);
854     return;
855   }
856
857   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
858     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
859                "SHORTEN: %s is too long\n", name);
860     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
861     send_shorten_response(csh, name);
862     return;
863   }
864   
865   if (!is_gnunet_tld(name) && !is_zkey_tld(name))
866   {
867     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868                 "%s is not our domain. Returning\n", name);
869     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
870     send_shorten_response(csh, name);
871     return;
872   }
873
874   csh->shorten_zone = sh_msg->shorten_zone;
875   csh->private_zone = sh_msg->private_zone;
876
877   strcpy (csh->name, name);
878   
879   
880   if (1 == ntohl(sh_msg->use_default_zone))
881     csh->root_zone = zone_hash; //Default zone
882   else
883     csh->root_zone = sh_msg->zone;
884
885   start_shorten_name (csh);
886
887   GNUNET_STATISTICS_update (statistics,
888                             "Name shorten attempts", 1, GNUNET_NO);
889   
890 }
891
892
893 /**
894  * Send get authority response back to client
895  * 
896  * @param cls the closure containing a client get auth handle
897  * @param name the shortened name result or NULL if cannot be shortened
898  */
899 static void
900 send_get_auth_response(void *cls, const char* name)
901 {
902   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
903               "GET_AUTH_RESULT", name);
904   struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
905   struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
906   
907   if (name != NULL)
908   {
909     GNUNET_STATISTICS_update (statistics,
910                               "Authorities resolved", 1, GNUNET_NO);
911   }
912   
913   if (name == NULL)
914   {
915     name = "";
916   }
917
918   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
919                        + strlen(name) + 1);
920   
921   rmsg->id = cah->unique_id;
922   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
923   rmsg->header.size = 
924     htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
925           strlen(name) + 1);
926
927   strcpy((char*)&rmsg[1], name);
928
929   GNUNET_SERVER_notification_context_unicast (nc, cah->client,
930                               (const struct GNUNET_MessageHeader *) rmsg,
931                               GNUNET_NO);
932   GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
933   
934   GNUNET_free(rmsg);
935   GNUNET_free_non_null(cah->name);
936   GNUNET_free(cah);
937
938   
939   
940 }
941
942
943 /**
944  * Handle a get authority message from the api
945  *
946  * @param cls the closure
947  * @param client the client
948  * @param message the message
949  */
950 static void handle_get_authority(void *cls,
951                            struct GNUNET_SERVER_Client * client,
952                            const struct GNUNET_MessageHeader * message)
953 {
954   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
955
956   size_t msg_size = 0;
957   struct ClientGetAuthHandle *cah;
958   char name[MAX_DNS_NAME_LENGTH];
959   char* nameptr = name;
960
961
962   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
963   {
964     GNUNET_break_op (0);
965     GNUNET_SERVER_receive_done (client, GNUNET_OK);
966     return;
967   }
968
969   GNUNET_SERVER_notification_context_add (nc, client);
970
971   struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
972     (struct GNUNET_GNS_ClientGetAuthMessage *) message;
973   
974   msg_size = ntohs(message->size);
975
976   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
977   {
978     GNUNET_break_op (0);
979     GNUNET_SERVER_receive_done (client, GNUNET_OK);
980     return;
981   }
982   
983   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
984
985
986   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
987   cah->client = client;
988   cah->unique_id = sh_msg->id;
989
990   if (strlen(name) < strlen(GNUNET_GNS_TLD))
991   {
992     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993                 "GET_AUTH: %s is too short. Returning\n", name);
994     cah->name = NULL;
995     send_get_auth_response(cah, name);
996     return;
997   }
998   
999   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1000     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1001                "GET_AUTH: %s is too long", name);
1002     cah->name = NULL;
1003     send_get_auth_response(cah, name);
1004     return;
1005   }
1006   
1007   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
1008              GNUNET_GNS_TLD) != 0)
1009   {
1010     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011                 "GET_AUTH: %s is not our domain. Returning\n", name);
1012     cah->name = NULL;
1013     send_get_auth_response(cah, name);
1014     return;
1015   }
1016
1017   if (strcmp(name, GNUNET_GNS_TLD) == 0)
1018   {
1019     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020                 "GET_AUTH: %s is us. Returning\n", name);
1021     cah->name = NULL;
1022     send_get_auth_response(cah, name);
1023     return;
1024   }
1025   
1026   cah->name = GNUNET_malloc(strlen(name)
1027                             - strlen(GNUNET_GNS_TLD) + 1);
1028   memset(cah->name, 0,
1029          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1030   memcpy(cah->name, name,
1031          strlen(name)-strlen(GNUNET_GNS_TLD));
1032
1033   /* Start delegation resolution in our namestore */
1034   gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1035
1036   GNUNET_STATISTICS_update (statistics,
1037                             "Authority lookup attempts", 1, GNUNET_NO);
1038 }
1039
1040
1041
1042 /**
1043  * Reply to client with the result from our lookup.
1044  *
1045  * @param cls the closure (our client lookup handle)
1046  * @param rd_count the number of records
1047  * @param rd the record data
1048  */
1049 static void
1050 send_lookup_response(void* cls,
1051                      uint32_t rd_count,
1052                      const struct GNUNET_NAMESTORE_RecordData *rd)
1053 {
1054   struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1055   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1056   size_t len;
1057   
1058   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1059               "LOOKUP_RESULT", rd_count);
1060   
1061   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1062   rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1063   
1064   rmsg->id = clh->unique_id;
1065   rmsg->rd_count = htonl(rd_count);
1066   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1067   rmsg->header.size = 
1068     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1069   
1070   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1071   
1072   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1073                                 (const struct GNUNET_MessageHeader *) rmsg,
1074                                 GNUNET_NO);
1075   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1076   
1077   GNUNET_free(rmsg);
1078   GNUNET_free(clh->name);
1079   
1080   if (NULL != clh->shorten_key)
1081     GNUNET_free(clh->shorten_key);
1082
1083   GNUNET_free(clh);
1084
1085   GNUNET_STATISTICS_update (statistics,
1086                             "Completed lookups", 1, GNUNET_NO);
1087
1088   if (rd != NULL)
1089   {
1090     GNUNET_STATISTICS_update (statistics,
1091                               "Records resolved", rd_count, GNUNET_NO);
1092   }
1093
1094 }
1095
1096
1097 /**
1098  * Handle lookup requests from client
1099  *
1100  * @param cls the closure
1101  * @param client the client
1102  * @param message the message
1103  */
1104 static void
1105 handle_lookup(void *cls,
1106               struct GNUNET_SERVER_Client * client,
1107               const struct GNUNET_MessageHeader * message)
1108 {
1109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1110
1111   size_t msg_size = 0;
1112   size_t namelen;
1113   char name[MAX_DNS_NAME_LENGTH];
1114   struct ClientLookupHandle *clh;
1115   char* nameptr = name;
1116   int only_cached;
1117   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1118   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1119   char* tmp_pkey;
1120
1121   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1122   {
1123     GNUNET_break_op (0);
1124     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1125     return;
1126   }
1127
1128   GNUNET_SERVER_notification_context_add (nc, client);
1129
1130   struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1131     (struct GNUNET_GNS_ClientLookupMessage *) message;
1132   
1133   msg_size = ntohs(message->size);
1134
1135   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1136   {
1137     GNUNET_break_op (0);
1138     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1139     return;
1140   }
1141
1142   if (1 == ntohl(sh_msg->have_key))
1143   {
1144     pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1145     tmp_pkey = (char*)&sh_msg[1];
1146     key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1147     GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1148   }
1149   else
1150   {
1151     key = NULL;
1152     GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1153   }
1154   
1155   namelen = strlen(name)+1;
1156   clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1157   memset (clh, 0, sizeof (struct ClientLookupHandle));
1158   clh->client = client;
1159   clh->name = GNUNET_malloc(namelen);
1160   strcpy(clh->name, name);
1161   clh->unique_id = sh_msg->id;
1162   clh->type = ntohl(sh_msg->type);
1163   clh->shorten_key = key;
1164
1165   only_cached = ntohl(sh_msg->only_cached);
1166   
1167   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1168     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169                "LOOKUP: %s is too long", name);
1170     clh->name = NULL;
1171     send_lookup_response(clh, 0, NULL);
1172     return;
1173   }
1174
1175   if ((clh->type == GNUNET_GNS_RECORD_A) &&
1176       (GNUNET_OK != v4_enabled))
1177   {
1178     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1179                "LOOKUP: Query for A record but AF_INET not supported!");
1180     clh->name = NULL;
1181     send_lookup_response(clh, 0, NULL);
1182     return;
1183   }
1184   
1185   if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1186       (GNUNET_OK != v6_enabled))
1187   {
1188     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1189                "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1190     clh->name = NULL;
1191     send_lookup_response(clh, 0, NULL);
1192     return;
1193   }
1194   
1195   if (1 == ntohl(sh_msg->use_default_zone))
1196     clh->zone = zone_hash; //Default zone
1197   else
1198     clh->zone = sh_msg->zone;
1199   
1200   if (GNUNET_YES == auto_import_pkey)
1201   {
1202     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1203                                 clh->shorten_key,
1204                                 default_lookup_timeout,
1205                                 clh->only_cached,
1206                                 &send_lookup_response, clh);  
1207   }
1208   else
1209   {
1210     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1211                                 NULL,
1212                                 default_lookup_timeout,
1213                                 only_cached,
1214                                 &send_lookup_response, clh);
1215   }
1216
1217   GNUNET_STATISTICS_update (statistics,
1218                             "Record lookup attempts", 1, GNUNET_NO);
1219 }
1220
1221 /**
1222  * Test if the given AF is supported by this system.
1223  *
1224  * @param af to test
1225  * @return GNUNET_OK if the AF is supported
1226  */
1227 static int
1228 test_af (int af)
1229 {
1230   int s;
1231
1232   s = socket (af, SOCK_STREAM, 0);
1233   if (-1 == s)
1234   {
1235     if (EAFNOSUPPORT == errno)
1236       return GNUNET_NO;
1237     fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1238     return GNUNET_SYSERR;
1239   }
1240 #if WINDOWS
1241   closesocket (s);
1242 #else
1243   close (s);
1244 #endif
1245   return GNUNET_OK;
1246 }
1247
1248 /**
1249  * Process GNS requests.
1250  *
1251  * @param cls closure)
1252  * @param server the initialized server
1253  * @param c configuration to use
1254  */
1255 static void
1256 run (void *cls, struct GNUNET_SERVER_Handle *server,
1257      const struct GNUNET_CONFIGURATION_Handle *c)
1258 {
1259
1260   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1261   
1262   char* keyfile;
1263   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1264   unsigned long long max_parallel_bg_queries = 0;
1265   unsigned long long default_lookup_timeout_secs = 0;
1266   int ignore_pending = GNUNET_NO;
1267
1268   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1269     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1270     {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1271     {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1272   };
1273
1274   GNS_cfg = c;
1275
1276   v6_enabled = test_af (AF_INET6);
1277   v4_enabled = test_af (AF_INET);
1278
1279   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1280                                              "ZONEKEY", &keyfile))
1281   {
1282     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1283                 "No private key for root zone specified!\n");
1284     GNUNET_SCHEDULER_shutdown ();
1285     return;
1286   }
1287
1288   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1289              "Using keyfile %s for root zone.\n", keyfile);
1290
1291   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1292   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1293
1294   GNUNET_CRYPTO_short_hash(&pkey,
1295                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1296                      &zone_hash);
1297   GNUNET_free(keyfile);
1298   
1299   /**
1300    * handle to our local namestore
1301    */
1302   namestore_handle = GNUNET_NAMESTORE_connect(c);
1303
1304   if (NULL == namestore_handle)
1305   {
1306     //FIXME do error handling;
1307     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1308                "Failed to connect to the namestore!\n");
1309     GNUNET_SCHEDULER_shutdown ();
1310     return;
1311   }
1312   
1313   
1314
1315   auto_import_pkey = GNUNET_NO;
1316
1317   if (GNUNET_YES ==
1318       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1319                                             "AUTO_IMPORT_PKEY"))
1320   {
1321     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1322                "Automatic PKEY import is enabled.\n");
1323     auto_import_pkey = GNUNET_YES;
1324
1325   }
1326
1327   zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1328
1329   record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1330
1331   if (GNUNET_OK ==
1332       GNUNET_CONFIGURATION_get_value_time (c, "gns",
1333                                              "RECORD_PUT_INTERVAL",
1334                                              &record_put_interval))
1335   {
1336     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1337                "Record put interval: %llu\n",
1338                record_put_interval);
1339   }
1340
1341   if (GNUNET_OK ==
1342       GNUNET_CONFIGURATION_get_value_number (c, "gns",
1343                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
1344                                             &max_parallel_bg_queries))
1345   {
1346     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1347                "Number of allowed parallel background queries: %llu\n",
1348                max_parallel_bg_queries);
1349   }
1350
1351   if (GNUNET_YES ==
1352       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1353                                             "AUTO_IMPORT_CONFIRMATION_REQ"))
1354   {
1355     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1356                "Auto import requires user confirmation\n");
1357     ignore_pending = GNUNET_YES;
1358   }
1359
1360   if (GNUNET_OK ==
1361       GNUNET_CONFIGURATION_get_value_number(c, "gns",
1362                                             "DEFAULT_LOOKUP_TIMEOUT",
1363                                             &default_lookup_timeout_secs))
1364   {
1365     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1366                "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1367     default_lookup_timeout = GNUNET_TIME_relative_multiply(
1368                                             GNUNET_TIME_UNIT_SECONDS,
1369                                             default_lookup_timeout_secs);
1370   }
1371   
1372   /**
1373    * handle to the dht
1374    */
1375   dht_handle = GNUNET_DHT_connect(c,
1376                        //max_parallel_bg_queries); //FIXME get ht_len from cfg
1377                        1024);
1378
1379   if (NULL == dht_handle)
1380   {
1381     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1382   }
1383   
1384   if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1385                         max_parallel_bg_queries,
1386                         ignore_pending)
1387       == GNUNET_SYSERR)
1388   {
1389     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1390                "Unable to initialize resolver!\n");
1391     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1392     return;
1393   }
1394
1395   if (GNUNET_YES ==
1396       GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1397   {
1398     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1399                "DNS hijacking enabled... connecting to service.\n");
1400
1401     if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1402     {
1403       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1404                "Failed to enable the dns interceptor!\n");
1405     }
1406   }
1407   
1408   /**
1409    * Schedule periodic put
1410    * for our records
1411    * We have roughly an hour for all records;
1412    */
1413   first_zone_iteration = GNUNET_YES;
1414   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1415
1416   GNUNET_SERVER_add_handlers (server, handlers);
1417   
1418   //FIXME
1419   //GNUNET_SERVER_disconnect_notify (server,
1420   //                                 &client_disconnect_notification,
1421   //                                 NULL);
1422
1423   statistics = GNUNET_STATISTICS_create ("gns", c);
1424
1425   nc = GNUNET_SERVER_notification_context_create (server, 1);
1426
1427   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1428                                 NULL);
1429 }
1430
1431
1432 /**
1433  * The main function for the GNS service.
1434  *
1435  * @param argc number of arguments from the command line
1436  * @param argv command line arguments
1437  * @return 0 ok, 1 on error
1438  */
1439 int
1440 main (int argc, char *const *argv)
1441 {
1442   int ret;
1443
1444   ret =
1445       (GNUNET_OK ==
1446        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1447                            NULL)) ? 0 : 1;
1448   return ret;
1449 }
1450
1451 /* end of gnunet-service-gns.c */