-fixes
[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
846   GNUNET_SERVER_notification_context_add (nc, client);
847   
848   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
849     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
850                "SHORTEN: %s is too short", name);
851     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
852     send_shorten_response(csh, name);
853     return;
854   }
855
856   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
857     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
858                "SHORTEN: %s is too long", name);
859     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
860     send_shorten_response(csh, name);
861     return;
862   }
863   
864   if (!is_gnunet_tld(name) && !is_zkey_tld(name))
865   {
866     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
867                 "%s is not our domain. Returning\n", name);
868     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
869     send_shorten_response(csh, name);
870     return;
871   }
872
873   csh->shorten_zone = sh_msg->shorten_zone;
874   csh->private_zone = sh_msg->private_zone;
875
876   strcpy (csh->name, name);
877   
878   
879   if (1 == ntohl(sh_msg->use_default_zone))
880     csh->root_zone = zone_hash; //Default zone
881   else
882     csh->root_zone = sh_msg->zone;
883
884   start_shorten_name (csh);
885
886   GNUNET_STATISTICS_update (statistics,
887                             "Name shorten attempts", 1, GNUNET_NO);
888   
889 }
890
891
892 /**
893  * Send get authority response back to client
894  * 
895  * @param cls the closure containing a client get auth handle
896  * @param name the shortened name result or NULL if cannot be shortened
897  */
898 static void
899 send_get_auth_response(void *cls, const char* name)
900 {
901   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
902               "GET_AUTH_RESULT", name);
903   struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
904   struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
905   
906   if (name != NULL)
907   {
908     GNUNET_STATISTICS_update (statistics,
909                               "Authorities resolved", 1, GNUNET_NO);
910   }
911   
912   if (name == NULL)
913   {
914     name = "";
915   }
916
917   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
918                        + strlen(name) + 1);
919   
920   rmsg->id = cah->unique_id;
921   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
922   rmsg->header.size = 
923     htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
924           strlen(name) + 1);
925
926   strcpy((char*)&rmsg[1], name);
927
928   GNUNET_SERVER_notification_context_unicast (nc, cah->client,
929                               (const struct GNUNET_MessageHeader *) rmsg,
930                               GNUNET_NO);
931   GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
932   
933   GNUNET_free(rmsg);
934   GNUNET_free_non_null(cah->name);
935   GNUNET_free(cah);
936
937   
938   
939 }
940
941
942 /**
943  * Handle a get authority message from the api
944  *
945  * @param cls the closure
946  * @param client the client
947  * @param message the message
948  */
949 static void handle_get_authority(void *cls,
950                            struct GNUNET_SERVER_Client * client,
951                            const struct GNUNET_MessageHeader * message)
952 {
953   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
954
955   size_t msg_size = 0;
956   struct ClientGetAuthHandle *cah;
957   char name[MAX_DNS_NAME_LENGTH];
958   char* nameptr = name;
959
960
961   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
962   {
963     GNUNET_break_op (0);
964     GNUNET_SERVER_receive_done (client, GNUNET_OK);
965     return;
966   }
967
968   GNUNET_SERVER_notification_context_add (nc, client);
969
970   struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
971     (struct GNUNET_GNS_ClientGetAuthMessage *) message;
972   
973   msg_size = ntohs(message->size);
974
975   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
976   {
977     GNUNET_break_op (0);
978     GNUNET_SERVER_receive_done (client, GNUNET_OK);
979     return;
980   }
981   
982   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
983
984
985   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
986   cah->client = client;
987   cah->unique_id = sh_msg->id;
988
989   if (strlen(name) < strlen(GNUNET_GNS_TLD))
990   {
991     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992                 "GET_AUTH: %s is too short. Returning\n", name);
993     cah->name = NULL;
994     send_get_auth_response(cah, name);
995     return;
996   }
997   
998   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
999     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1000                "GET_AUTH: %s is too long", name);
1001     cah->name = NULL;
1002     send_get_auth_response(cah, name);
1003     return;
1004   }
1005   
1006   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
1007              GNUNET_GNS_TLD) != 0)
1008   {
1009     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010                 "GET_AUTH: %s is not our domain. Returning\n", name);
1011     cah->name = NULL;
1012     send_get_auth_response(cah, name);
1013     return;
1014   }
1015
1016   if (strcmp(name, GNUNET_GNS_TLD) == 0)
1017   {
1018     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019                 "GET_AUTH: %s is us. Returning\n", name);
1020     cah->name = NULL;
1021     send_get_auth_response(cah, name);
1022     return;
1023   }
1024   
1025   cah->name = GNUNET_malloc(strlen(name)
1026                             - strlen(GNUNET_GNS_TLD) + 1);
1027   memset(cah->name, 0,
1028          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1029   memcpy(cah->name, name,
1030          strlen(name)-strlen(GNUNET_GNS_TLD));
1031
1032   /* Start delegation resolution in our namestore */
1033   gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1034
1035   GNUNET_STATISTICS_update (statistics,
1036                             "Authority lookup attempts", 1, GNUNET_NO);
1037 }
1038
1039
1040
1041 /**
1042  * Reply to client with the result from our lookup.
1043  *
1044  * @param cls the closure (our client lookup handle)
1045  * @param rd_count the number of records
1046  * @param rd the record data
1047  */
1048 static void
1049 send_lookup_response(void* cls,
1050                      uint32_t rd_count,
1051                      const struct GNUNET_NAMESTORE_RecordData *rd)
1052 {
1053   struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1054   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1055   size_t len;
1056   
1057   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1058               "LOOKUP_RESULT", rd_count);
1059   
1060   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1061   rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1062   
1063   rmsg->id = clh->unique_id;
1064   rmsg->rd_count = htonl(rd_count);
1065   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1066   rmsg->header.size = 
1067     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1068   
1069   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1070   
1071   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1072                                 (const struct GNUNET_MessageHeader *) rmsg,
1073                                 GNUNET_NO);
1074   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1075   
1076   GNUNET_free(rmsg);
1077   GNUNET_free(clh->name);
1078   
1079   if (NULL != clh->shorten_key)
1080     GNUNET_free(clh->shorten_key);
1081
1082   GNUNET_free(clh);
1083
1084   GNUNET_STATISTICS_update (statistics,
1085                             "Completed lookups", 1, GNUNET_NO);
1086
1087   if (rd != NULL)
1088   {
1089     GNUNET_STATISTICS_update (statistics,
1090                               "Records resolved", rd_count, GNUNET_NO);
1091   }
1092
1093 }
1094
1095
1096 /**
1097  * Handle lookup requests from client
1098  *
1099  * @param cls the closure
1100  * @param client the client
1101  * @param message the message
1102  */
1103 static void
1104 handle_lookup(void *cls,
1105               struct GNUNET_SERVER_Client * client,
1106               const struct GNUNET_MessageHeader * message)
1107 {
1108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1109
1110   size_t msg_size = 0;
1111   size_t namelen;
1112   char name[MAX_DNS_NAME_LENGTH];
1113   struct ClientLookupHandle *clh;
1114   char* nameptr = name;
1115   int only_cached;
1116   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1117   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1118   char* tmp_pkey;
1119
1120   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1121   {
1122     GNUNET_break_op (0);
1123     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1124     return;
1125   }
1126
1127   GNUNET_SERVER_notification_context_add (nc, client);
1128
1129   struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1130     (struct GNUNET_GNS_ClientLookupMessage *) message;
1131   
1132   msg_size = ntohs(message->size);
1133
1134   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1135   {
1136     GNUNET_break_op (0);
1137     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1138     return;
1139   }
1140
1141   if (1 == ntohl(sh_msg->have_key))
1142   {
1143     pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1144     tmp_pkey = (char*)&sh_msg[1];
1145     key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1146     GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1147   }
1148   else
1149   {
1150     key = NULL;
1151     GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1152   }
1153   
1154   namelen = strlen(name)+1;
1155   clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1156   memset (clh, 0, sizeof (struct ClientLookupHandle));
1157   clh->client = client;
1158   clh->name = GNUNET_malloc(namelen);
1159   strcpy(clh->name, name);
1160   clh->unique_id = sh_msg->id;
1161   clh->type = ntohl(sh_msg->type);
1162   clh->shorten_key = key;
1163
1164   only_cached = ntohl(sh_msg->only_cached);
1165   
1166   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1167     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1168                "LOOKUP: %s is too long", name);
1169     clh->name = NULL;
1170     send_lookup_response(clh, 0, NULL);
1171     return;
1172   }
1173
1174   if ((clh->type == GNUNET_GNS_RECORD_A) &&
1175       (GNUNET_OK != v4_enabled))
1176   {
1177     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1178                "LOOKUP: Query for A record but AF_INET not supported!");
1179     clh->name = NULL;
1180     send_lookup_response(clh, 0, NULL);
1181     return;
1182   }
1183   
1184   if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1185       (GNUNET_OK != v6_enabled))
1186   {
1187     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1188                "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1189     clh->name = NULL;
1190     send_lookup_response(clh, 0, NULL);
1191     return;
1192   }
1193   
1194   if (1 == ntohl(sh_msg->use_default_zone))
1195     clh->zone = zone_hash; //Default zone
1196   else
1197     clh->zone = sh_msg->zone;
1198   
1199   if (GNUNET_YES == auto_import_pkey)
1200   {
1201     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1202                                 clh->shorten_key,
1203                                 default_lookup_timeout,
1204                                 clh->only_cached,
1205                                 &send_lookup_response, clh);  
1206   }
1207   else
1208   {
1209     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1210                                 NULL,
1211                                 default_lookup_timeout,
1212                                 only_cached,
1213                                 &send_lookup_response, clh);
1214   }
1215
1216   GNUNET_STATISTICS_update (statistics,
1217                             "Record lookup attempts", 1, GNUNET_NO);
1218 }
1219
1220 /**
1221  * Test if the given AF is supported by this system.
1222  *
1223  * @param af to test
1224  * @return GNUNET_OK if the AF is supported
1225  */
1226 static int
1227 test_af (int af)
1228 {
1229   int s;
1230
1231   s = socket (af, SOCK_STREAM, 0);
1232   if (-1 == s)
1233   {
1234     if (EAFNOSUPPORT == errno)
1235       return GNUNET_NO;
1236     fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1237     return GNUNET_SYSERR;
1238   }
1239 #if WINDOWS
1240   closesocket (s);
1241 #else
1242   close (s);
1243 #endif
1244   return GNUNET_OK;
1245 }
1246
1247 /**
1248  * Process GNS requests.
1249  *
1250  * @param cls closure)
1251  * @param server the initialized server
1252  * @param c configuration to use
1253  */
1254 static void
1255 run (void *cls, struct GNUNET_SERVER_Handle *server,
1256      const struct GNUNET_CONFIGURATION_Handle *c)
1257 {
1258
1259   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1260   
1261   char* keyfile;
1262   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1263   unsigned long long max_parallel_bg_queries = 0;
1264   unsigned long long default_lookup_timeout_secs = 0;
1265   int ignore_pending = GNUNET_NO;
1266
1267   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1268     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1269     {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1270     {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1271   };
1272
1273   GNS_cfg = c;
1274
1275   v6_enabled = test_af (AF_INET6);
1276   v4_enabled = test_af (AF_INET);
1277
1278   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1279                                              "ZONEKEY", &keyfile))
1280   {
1281     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1282                 "No private key for root zone specified!\n");
1283     GNUNET_SCHEDULER_shutdown ();
1284     return;
1285   }
1286
1287   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1288              "Using keyfile %s for root zone.\n", keyfile);
1289
1290   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1291   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1292
1293   GNUNET_CRYPTO_short_hash(&pkey,
1294                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1295                      &zone_hash);
1296   GNUNET_free(keyfile);
1297   
1298   /**
1299    * handle to our local namestore
1300    */
1301   namestore_handle = GNUNET_NAMESTORE_connect(c);
1302
1303   if (NULL == namestore_handle)
1304   {
1305     //FIXME do error handling;
1306     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1307                "Failed to connect to the namestore!\n");
1308     GNUNET_SCHEDULER_shutdown ();
1309     return;
1310   }
1311   
1312   
1313
1314   auto_import_pkey = GNUNET_NO;
1315
1316   if (GNUNET_YES ==
1317       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1318                                             "AUTO_IMPORT_PKEY"))
1319   {
1320     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1321                "Automatic PKEY import is enabled.\n");
1322     auto_import_pkey = GNUNET_YES;
1323
1324   }
1325
1326   zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1327
1328   record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1329
1330   if (GNUNET_OK ==
1331       GNUNET_CONFIGURATION_get_value_time (c, "gns",
1332                                              "RECORD_PUT_INTERVAL",
1333                                              &record_put_interval))
1334   {
1335     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1336                "Record put interval: %llu\n",
1337                record_put_interval);
1338   }
1339
1340   if (GNUNET_OK ==
1341       GNUNET_CONFIGURATION_get_value_number (c, "gns",
1342                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
1343                                             &max_parallel_bg_queries))
1344   {
1345     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1346                "Number of allowed parallel background queries: %llu\n",
1347                max_parallel_bg_queries);
1348   }
1349
1350   if (GNUNET_YES ==
1351       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1352                                             "AUTO_IMPORT_CONFIRMATION_REQ"))
1353   {
1354     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1355                "Auto import requires user confirmation\n");
1356     ignore_pending = GNUNET_YES;
1357   }
1358
1359   if (GNUNET_OK ==
1360       GNUNET_CONFIGURATION_get_value_number(c, "gns",
1361                                             "DEFAULT_LOOKUP_TIMEOUT",
1362                                             &default_lookup_timeout_secs))
1363   {
1364     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1365                "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1366     default_lookup_timeout = GNUNET_TIME_relative_multiply(
1367                                             GNUNET_TIME_UNIT_SECONDS,
1368                                             default_lookup_timeout_secs);
1369   }
1370   
1371   /**
1372    * handle to the dht
1373    */
1374   dht_handle = GNUNET_DHT_connect(c,
1375                        //max_parallel_bg_queries); //FIXME get ht_len from cfg
1376                        1024);
1377
1378   if (NULL == dht_handle)
1379   {
1380     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1381   }
1382   
1383   if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1384                         max_parallel_bg_queries,
1385                         ignore_pending)
1386       == GNUNET_SYSERR)
1387   {
1388     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1389                "Unable to initialize resolver!\n");
1390     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1391     return;
1392   }
1393
1394   if (GNUNET_YES ==
1395       GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1396   {
1397     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1398                "DNS hijacking enabled... connecting to service.\n");
1399
1400     if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1401     {
1402       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1403                "Failed to enable the dns interceptor!\n");
1404     }
1405   }
1406   
1407   /**
1408    * Schedule periodic put
1409    * for our records
1410    * We have roughly an hour for all records;
1411    */
1412   first_zone_iteration = GNUNET_YES;
1413   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1414
1415   GNUNET_SERVER_add_handlers (server, handlers);
1416   
1417   //FIXME
1418   //GNUNET_SERVER_disconnect_notify (server,
1419   //                                 &client_disconnect_notification,
1420   //                                 NULL);
1421
1422   statistics = GNUNET_STATISTICS_create ("gns", c);
1423
1424   nc = GNUNET_SERVER_notification_context_create (server, 1);
1425
1426   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1427                                 NULL);
1428 }
1429
1430
1431 /**
1432  * The main function for the GNS service.
1433  *
1434  * @param argc number of arguments from the command line
1435  * @param argv command line arguments
1436  * @return 0 ok, 1 on error
1437  */
1438 int
1439 main (int argc, char *const *argv)
1440 {
1441   int ret;
1442
1443   ret =
1444       (GNUNET_OK ==
1445        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1446                            NULL)) ? 0 : 1;
1447   return ret;
1448 }
1449
1450 /* end of gnunet-service-gns.c */