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