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