-fix 0 record zone iteration
[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     if (0 == num_public_records)
348     {
349       /**
350        * If no records are known (startup) or none present
351        * we can safely set the interval to the value for a single
352        * record
353        */
354       zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
355                                                              1);
356
357       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
358                  "No records in db.\n");
359     }
360     else
361     {
362       zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
363                                                              num_public_records);
364     }
365
366     zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
367                                                         zone_iteration_interval);
368
369     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
370                "Adjusted zone iteration interval to %llus!\n",
371                zone_iteration_interval.rel_value);
372     GNUNET_STATISTICS_set (statistics,
373                            "Current zone iteration interval [msec]",
374                            zone_iteration_interval.rel_value,
375                            GNUNET_NO);
376     
377     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
378                "Zone iteration finished. Rescheduling zone iteration\n");
379
380     namestore_iter = NULL;
381     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
382     GNUNET_STATISTICS_update (statistics,
383                               "Number of zone iterations", 1, GNUNET_NO);
384
385     last_num_public_records = num_public_records;
386     GNUNET_STATISTICS_set (statistics,
387                            "Number of public records in DHT",
388                            last_num_public_records,
389                            GNUNET_NO);
390     return;
391   }
392   
393   namelen = strlen(name) + 1;
394
395   if (rd_count == 0)
396   {
397     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
398                "No records given for name %s! Skipping...\n",
399                name);
400     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
401                                                    NULL);
402     return;
403   }
404   
405   if (signature == NULL)
406   {
407     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
408                "No signature for %s record data provided! Skipping...\n",
409                name);
410     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
411                                                    NULL);
412     return;
413
414   }
415   
416   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
417              "Putting records for %s into the DHT\n", name);
418   
419   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
420   
421   nrb = GNUNET_malloc(rd_payload_length + namelen
422                       + sizeof(struct GNSNameRecordBlock));
423   
424   nrb->signature = *signature;
425   
426   nrb->public_key = *key;
427
428   nrb->rd_count = htonl(rd_count);
429   
430   memcpy(&nrb[1], name, namelen);
431
432   nrb_data = (char*)&nrb[1];
433   nrb_data += namelen;
434
435   rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
436
437   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
438                                                 rd,
439                                                 rd_payload_length,
440                                                 nrb_data))
441   {
442     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
443                "Record serialization failed! Skipping...\n");
444     GNUNET_free(nrb);
445     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
446                                                    NULL);
447     return;
448   }
449
450
451   /*
452    * calculate DHT key: H(name) xor H(pubkey)
453    */
454   GNUNET_CRYPTO_short_hash(key,
455                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
456                      &zhash);
457   GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
458   GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
459   GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
460   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
461
462   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
463              "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
464
465   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
466              "putting %d records for %s under key: %s with size %d\n",
467              rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
468   
469   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
470              "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
471
472   GNUNET_STATISTICS_update (statistics,
473                             "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
474
475   /* FIXME: keep return value to possibly cancel? */
476   GNUNET_DHT_put (dht_handle, &xor_hash,
477                   DHT_GNS_REPLICATION_LEVEL,
478                   GNUNET_DHT_RO_NONE,
479                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
480                   rd_payload_length,
481                   (char*)nrb,
482                   expiration,
483                   DHT_OPERATION_TIMEOUT,
484                   &record_dht_put,
485                   NULL); //cls for cont
486   
487   num_public_records++;
488   
489   if (num_public_records > last_num_public_records)
490   {
491     interval_adjustment = ceil ((double)num_public_records / (double)last_num_public_records);
492     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
493                "Last record count was lower than current record count... increasing.\n");
494     next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
495                                                      interval_adjustment);
496
497   }
498   else
499     next_put_interval = zone_iteration_interval;
500
501   GNUNET_STATISTICS_set (statistics,
502                             "Current zone iteration interval [msec]",
503                             next_put_interval.rel_value,
504                             GNUNET_NO);
505   
506   /**
507    * Reschedule periodic put
508    */
509   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
510                                 &update_zone_dht_next,
511                                 NULL);
512
513   GNUNET_free(nrb);
514
515 }
516
517 /**
518  * Periodically iterate over our zone and store everything in dht
519  *
520  * @param cls NULL
521  * @param tc task context
522  */
523 static void
524 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
525 {
526   zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
527
528   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
529   
530   /* start counting again */
531   num_public_records = 0;
532   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
533                                                  NULL, //All zones
534                                                  GNUNET_NAMESTORE_RF_AUTHORITY,
535                                                  GNUNET_NAMESTORE_RF_PRIVATE,
536                                                  &put_gns_record,
537                                                  NULL);
538 }
539
540 /* END DHT ZONE PROPAGATION */
541
542 /**
543  * Send shorten response back to client
544  * 
545  * @param cls the closure containing a client shorten handle
546  * @param name the shortened name result or NULL if cannot be shortened
547  */
548 static void
549 send_shorten_response(void* cls, const char* name)
550 {
551   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
552               "SHORTEN_RESULT", name);
553   struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
554   struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
555   
556   if (name == NULL)
557   {
558     name = "";
559   }
560
561   GNUNET_STATISTICS_update (statistics,
562                             "Name shorten results", 1, GNUNET_NO);
563
564   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
565                        + strlen(name) + 1);
566   
567   rmsg->id = csh->unique_id;
568   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
569   rmsg->header.size = 
570     htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
571           strlen(name) + 1);
572
573   strcpy((char*)&rmsg[1], name);
574
575   GNUNET_SERVER_notification_context_unicast (nc, csh->client,
576                               (const struct GNUNET_MessageHeader *) rmsg,
577                               GNUNET_NO);
578   GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
579
580   if (NULL != csh->namestore_task)
581     GNUNET_NAMESTORE_cancel (csh->namestore_task);
582   
583   GNUNET_free(rmsg);
584   GNUNET_free(csh);
585
586 }
587
588
589 /**
590  * Lookup the zone infos and shorten name
591  *
592  * @param cls the client shorten handle
593  * @param key key of the zone
594  * @param expiration expiration of record
595  * @param name name found or null if no result
596  * @param rd_count number of records found
597  * @param rd record data
598  * @param signature
599  *
600  */
601 static void
602 process_shorten_in_private_zone_lookup (void *cls,
603                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
604                       struct GNUNET_TIME_Absolute expiration,
605                       const char *name,
606                       unsigned int rd_count,
607                       const struct GNUNET_NAMESTORE_RecordData *rd,
608                       const struct GNUNET_CRYPTO_RsaSignature *signature)
609 {
610   struct ClientShortenHandle *csh = cls;
611   csh->namestore_task = NULL;
612   struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
613   struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
614
615   if (0 == strcmp (csh->private_zone_id, ""))
616     pzone = NULL;
617   
618   if (rd_count == 0)
619   {
620     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621                 "No shorten zone in private zone!\n");
622
623     strcpy (csh->shorten_zone_id, "");
624     szone = NULL;
625
626   }
627   else
628   {
629
630     GNUNET_assert (rd_count == 1);
631
632     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633                 "Shorten zone %s found in private zone %s\n",
634                 name, csh->private_zone_id);
635
636     sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
637   }
638   
639   GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
640
641   gns_resolver_shorten_name (&csh->root_zone,
642                              pzone,
643                              szone,
644                              csh->name,
645                              csh->private_zone_id,
646                              csh->shorten_zone_id,
647                              &send_shorten_response, csh);
648
649 }
650
651
652 /**
653  * Lookup the zone infos and shorten name
654  *
655  * @param cls the shorten handle
656  * @param key key of the zone
657  * @param expiration expiration of record
658  * @param name name found or null if no result
659  * @param rd_count number of records found
660  * @param rd record data
661  * @param signature
662  *
663  */
664 static void
665 process_shorten_in_root_zone_lookup (void *cls,
666                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
667                       struct GNUNET_TIME_Absolute expiration,
668                       const char *name,
669                       unsigned int rd_count,
670                       const struct GNUNET_NAMESTORE_RecordData *rd,
671                       const struct GNUNET_CRYPTO_RsaSignature *signature)
672 {
673   struct ClientShortenHandle *csh = cls;
674   csh->namestore_task = NULL;
675   struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
676   struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
677   
678   if (0 == strcmp (csh->private_zone_id, ""))
679     pzone = NULL;
680
681   if (rd_count == 0)
682   {
683     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684                 "No shorten zone in zone and no private zone!\n");
685
686     strcpy (csh->shorten_zone_id, "");
687
688     GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
689     szone = NULL;
690
691     gns_resolver_shorten_name (&csh->root_zone,
692                                pzone,
693                                szone,
694                                csh->name,
695                                csh->private_zone_id,
696                                csh->shorten_zone_id,
697                                &send_shorten_response, csh);
698     return;
699   }
700
701   GNUNET_assert (rd_count == 1);
702
703   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704               "Private zone %s found in root zone\n", name);
705
706   strcpy (csh->private_zone_id, name);
707
708   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
709                                   pzone,
710                                   szone,
711                                   &process_shorten_in_private_zone_lookup,
712                                   csh);
713 }
714
715
716 /**
717  * Lookup the zone infos and shorten name
718  *
719  * @param cls the shorten handle
720  * @param key key of the zone
721  * @param expiration expiration of record
722  * @param name name found or null if no result
723  * @param rd_count number of records found
724  * @param rd record data
725  * @param signature
726  *
727  */
728 static void
729 process_private_in_root_zone_lookup (void *cls,
730                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
731                       struct GNUNET_TIME_Absolute expiration,
732                       const char *name,
733                       unsigned int rd_count,
734                       const struct GNUNET_NAMESTORE_RecordData *rd,
735                       const struct GNUNET_CRYPTO_RsaSignature *signature)
736 {
737   struct ClientShortenHandle *csh = cls;
738   csh->namestore_task = NULL;
739
740   if (rd_count == 0)
741   {
742     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743                 "No private zone in root zone\n");
744
745     strcpy (csh->private_zone_id, "");
746   
747     csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
748                                   &csh->root_zone,
749                                   &csh->shorten_zone,
750                                   &process_shorten_in_root_zone_lookup,
751                                   csh);
752     return;
753   }
754
755   GNUNET_assert (rd_count == 1);
756
757   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758               "Private zone %s found in root zone\n", name);
759
760   strcpy (csh->private_zone_id, name);
761
762   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
763                                   &csh->private_zone,
764                                   &csh->shorten_zone,
765                                   &process_shorten_in_private_zone_lookup,
766                                   csh);
767 }
768
769 /**
770  * Lookup the zone infos and shorten name
771  *
772  * @param csh the shorten handle
773  *
774  */
775 static void
776 start_shorten_name (struct ClientShortenHandle *csh)
777 {
778   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
779               "Looking for private zone name in root zone\n");
780
781   csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
782                                   &csh->root_zone,
783                                   &csh->private_zone,
784                                   &process_private_in_root_zone_lookup,
785                                   csh);
786 }
787
788
789 /**
790  * Handle a shorten message from the api
791  *
792  * @param cls the closure
793  * @param client the client
794  * @param message the message
795  */
796 static void handle_shorten (void *cls,
797                             struct GNUNET_SERVER_Client * client,
798                             const struct GNUNET_MessageHeader * message)
799 {
800   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
801
802   size_t msg_size = 0;
803   struct ClientShortenHandle *csh;
804   char name[MAX_DNS_NAME_LENGTH];
805   char* nameptr = name;
806
807   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
808   {
809     GNUNET_break_op (0);
810     GNUNET_SERVER_receive_done (client, GNUNET_OK);
811     return;
812   }
813
814
815   struct GNUNET_GNS_ClientShortenMessage *sh_msg =
816     (struct GNUNET_GNS_ClientShortenMessage *) message;
817   
818   msg_size = ntohs(message->size);
819
820   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
821   {
822     GNUNET_break_op (0);
823     GNUNET_SERVER_receive_done (client, GNUNET_OK);
824     return;
825   }
826
827   csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
828   csh->client = client;
829   csh->unique_id = sh_msg->id;
830
831   GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
832   
833   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
834
835   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
836     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
837                "SHORTEN: %s is too short", name);
838     send_shorten_response(csh, name);
839     return;
840   }
841
842   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
843     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
844                "SHORTEN: %s is too long", name);
845     send_shorten_response(csh, name);
846     return;
847   }
848   
849   if (!is_gnunet_tld(name) && !is_zkey_tld(name))
850   {
851     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852                 "%s is not our domain. Returning\n", name);
853     send_shorten_response(csh, name);
854     return;
855   }
856
857   csh->shorten_zone = sh_msg->shorten_zone;
858   csh->private_zone = sh_msg->private_zone;
859
860   strcpy (csh->name, name);
861   
862   GNUNET_SERVER_notification_context_add (nc, client);
863   
864   if (1 == ntohl(sh_msg->use_default_zone))
865     csh->root_zone = zone_hash; //Default zone
866   else
867     csh->root_zone = sh_msg->zone;
868
869   start_shorten_name (csh);
870
871   GNUNET_STATISTICS_update (statistics,
872                             "Name shorten attempts", 1, GNUNET_NO);
873   
874 }
875
876
877 /**
878  * Send get authority response back to client
879  * 
880  * @param cls the closure containing a client get auth handle
881  * @param name the shortened name result or NULL if cannot be shortened
882  */
883 static void
884 send_get_auth_response(void *cls, const char* name)
885 {
886   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
887               "GET_AUTH_RESULT", name);
888   struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
889   struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
890   
891   if (name != NULL)
892   {
893     GNUNET_STATISTICS_update (statistics,
894                               "Authorities resolved", 1, GNUNET_NO);
895   }
896   
897   if (name == NULL)
898   {
899     name = "";
900   }
901
902   rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
903                        + strlen(name) + 1);
904   
905   rmsg->id = cah->unique_id;
906   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
907   rmsg->header.size = 
908     htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
909           strlen(name) + 1);
910
911   strcpy((char*)&rmsg[1], name);
912
913   GNUNET_SERVER_notification_context_unicast (nc, cah->client,
914                               (const struct GNUNET_MessageHeader *) rmsg,
915                               GNUNET_NO);
916   GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
917   
918   GNUNET_free(rmsg);
919   GNUNET_free_non_null(cah->name);
920   GNUNET_free(cah);
921
922   
923   
924 }
925
926
927 /**
928  * Handle a get authority message from the api
929  *
930  * @param cls the closure
931  * @param client the client
932  * @param message the message
933  */
934 static void handle_get_authority(void *cls,
935                            struct GNUNET_SERVER_Client * client,
936                            const struct GNUNET_MessageHeader * message)
937 {
938   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
939
940   size_t msg_size = 0;
941   struct ClientGetAuthHandle *cah;
942   char name[MAX_DNS_NAME_LENGTH];
943   char* nameptr = name;
944
945
946   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
947   {
948     GNUNET_break_op (0);
949     GNUNET_SERVER_receive_done (client, GNUNET_OK);
950     return;
951   }
952
953   GNUNET_SERVER_notification_context_add (nc, client);
954
955   struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
956     (struct GNUNET_GNS_ClientGetAuthMessage *) message;
957   
958   msg_size = ntohs(message->size);
959
960   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
961   {
962     GNUNET_break_op (0);
963     GNUNET_SERVER_receive_done (client, GNUNET_OK);
964     return;
965   }
966   
967   GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
968
969
970   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
971   cah->client = client;
972   cah->unique_id = sh_msg->id;
973
974   if (strlen(name) < strlen(GNUNET_GNS_TLD))
975   {
976     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977                 "GET_AUTH: %s is too short. Returning\n", name);
978     cah->name = NULL;
979     send_get_auth_response(cah, name);
980     return;
981   }
982   
983   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
984     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
985                "GET_AUTH: %s is too long", name);
986     cah->name = NULL;
987     send_get_auth_response(cah, name);
988     return;
989   }
990   
991   if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
992              GNUNET_GNS_TLD) != 0)
993   {
994     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995                 "GET_AUTH: %s is not our domain. Returning\n", name);
996     cah->name = NULL;
997     send_get_auth_response(cah, name);
998     return;
999   }
1000
1001   if (strcmp(name, GNUNET_GNS_TLD) == 0)
1002   {
1003     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004                 "GET_AUTH: %s is us. Returning\n", name);
1005     cah->name = NULL;
1006     send_get_auth_response(cah, name);
1007     return;
1008   }
1009   
1010   cah->name = GNUNET_malloc(strlen(name)
1011                             - strlen(GNUNET_GNS_TLD) + 1);
1012   memset(cah->name, 0,
1013          strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1014   memcpy(cah->name, name,
1015          strlen(name)-strlen(GNUNET_GNS_TLD));
1016
1017   /* Start delegation resolution in our namestore */
1018   gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1019
1020   GNUNET_STATISTICS_update (statistics,
1021                             "Authority lookup attempts", 1, GNUNET_NO);
1022 }
1023
1024
1025
1026 /**
1027  * Reply to client with the result from our lookup.
1028  *
1029  * @param cls the closure (our client lookup handle)
1030  * @param rd_count the number of records
1031  * @param rd the record data
1032  */
1033 static void
1034 send_lookup_response(void* cls,
1035                      uint32_t rd_count,
1036                      const struct GNUNET_NAMESTORE_RecordData *rd)
1037 {
1038   struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1039   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1040   size_t len;
1041   
1042   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1043               "LOOKUP_RESULT", rd_count);
1044   
1045   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1046   rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1047   
1048   rmsg->id = clh->unique_id;
1049   rmsg->rd_count = htonl(rd_count);
1050   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1051   rmsg->header.size = 
1052     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1053   
1054   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1055   
1056   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1057                                 (const struct GNUNET_MessageHeader *) rmsg,
1058                                 GNUNET_NO);
1059   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1060   
1061   GNUNET_free(rmsg);
1062   GNUNET_free(clh->name);
1063   
1064   if (NULL != clh->shorten_key)
1065     GNUNET_free(clh->shorten_key);
1066
1067   GNUNET_free(clh);
1068
1069   GNUNET_STATISTICS_update (statistics,
1070                             "Completed lookups", 1, GNUNET_NO);
1071
1072   if (rd != NULL)
1073   {
1074     GNUNET_STATISTICS_update (statistics,
1075                               "Records resolved", rd_count, GNUNET_NO);
1076   }
1077
1078 }
1079
1080
1081 /**
1082  * Handle lookup requests from client
1083  *
1084  * @param cls the closure
1085  * @param client the client
1086  * @param message the message
1087  */
1088 static void
1089 handle_lookup(void *cls,
1090               struct GNUNET_SERVER_Client * client,
1091               const struct GNUNET_MessageHeader * message)
1092 {
1093   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1094
1095   size_t msg_size = 0;
1096   size_t namelen;
1097   char name[MAX_DNS_NAME_LENGTH];
1098   struct ClientLookupHandle *clh;
1099   char* nameptr = name;
1100   int only_cached;
1101   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1102   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1103   char* tmp_pkey;
1104
1105   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1106   {
1107     GNUNET_break_op (0);
1108     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1109     return;
1110   }
1111
1112   GNUNET_SERVER_notification_context_add (nc, client);
1113
1114   struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1115     (struct GNUNET_GNS_ClientLookupMessage *) message;
1116   
1117   msg_size = ntohs(message->size);
1118
1119   if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1120   {
1121     GNUNET_break_op (0);
1122     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1123     return;
1124   }
1125
1126   if (1 == ntohl(sh_msg->have_key))
1127   {
1128     pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1129     tmp_pkey = (char*)&sh_msg[1];
1130     key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1131     GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1132   }
1133   else
1134   {
1135     key = NULL;
1136     GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1137   }
1138   
1139   namelen = strlen(name)+1;
1140   clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1141   memset (clh, 0, sizeof (struct ClientLookupHandle));
1142   clh->client = client;
1143   clh->name = GNUNET_malloc(namelen);
1144   strcpy(clh->name, name);
1145   clh->unique_id = sh_msg->id;
1146   clh->type = ntohl(sh_msg->type);
1147   clh->shorten_key = key;
1148
1149   only_cached = ntohl(sh_msg->only_cached);
1150   
1151   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1152     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1153                "LOOKUP: %s is too long", name);
1154     clh->name = NULL;
1155     send_lookup_response(clh, 0, NULL);
1156     return;
1157   }
1158
1159   if ((clh->type == GNUNET_GNS_RECORD_A) &&
1160       (GNUNET_OK != v4_enabled))
1161   {
1162     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1163                "LOOKUP: Query for A record but AF_INET not supported!");
1164     clh->name = NULL;
1165     send_lookup_response(clh, 0, NULL);
1166     return;
1167   }
1168   
1169   if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1170       (GNUNET_OK != v6_enabled))
1171   {
1172     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1173                "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1174     clh->name = NULL;
1175     send_lookup_response(clh, 0, NULL);
1176     return;
1177   }
1178   
1179   if (1 == ntohl(sh_msg->use_default_zone))
1180     clh->zone = zone_hash; //Default zone
1181   else
1182     clh->zone = sh_msg->zone;
1183   
1184   if (GNUNET_YES == auto_import_pkey)
1185   {
1186     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1187                                 clh->shorten_key,
1188                                 default_lookup_timeout,
1189                                 clh->only_cached,
1190                                 &send_lookup_response, clh);  
1191   }
1192   else
1193   {
1194     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1195                                 NULL,
1196                                 default_lookup_timeout,
1197                                 only_cached,
1198                                 &send_lookup_response, clh);
1199   }
1200
1201   GNUNET_STATISTICS_update (statistics,
1202                             "Record lookup attempts", 1, GNUNET_NO);
1203 }
1204
1205 /**
1206  * Test if the given AF is supported by this system.
1207  *
1208  * @param af to test
1209  * @return GNUNET_OK if the AF is supported
1210  */
1211 static int
1212 test_af (int af)
1213 {
1214   int s;
1215
1216   s = socket (af, SOCK_STREAM, 0);
1217   if (-1 == s)
1218   {
1219     if (EAFNOSUPPORT == errno)
1220       return GNUNET_NO;
1221     fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1222     return GNUNET_SYSERR;
1223   }
1224 #if WINDOWS
1225   closesocket (s);
1226 #else
1227   close (s);
1228 #endif
1229   return GNUNET_OK;
1230 }
1231
1232 /**
1233  * Process GNS requests.
1234  *
1235  * @param cls closure)
1236  * @param server the initialized server
1237  * @param c configuration to use
1238  */
1239 static void
1240 run (void *cls, struct GNUNET_SERVER_Handle *server,
1241      const struct GNUNET_CONFIGURATION_Handle *c)
1242 {
1243
1244   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1245   
1246   char* keyfile;
1247   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1248   unsigned long long max_parallel_bg_queries = 0;
1249   unsigned long long default_lookup_timeout_secs = 0;
1250   int ignore_pending = GNUNET_NO;
1251
1252   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1253     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1254     {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1255     {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1256   };
1257
1258   GNS_cfg = c;
1259
1260   v6_enabled = test_af (AF_INET6);
1261   v4_enabled = test_af (AF_INET);
1262
1263   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1264                                              "ZONEKEY", &keyfile))
1265   {
1266     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1267                 "No private key for root zone specified!\n");
1268     GNUNET_SCHEDULER_shutdown ();
1269     return;
1270   }
1271
1272   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1273              "Using keyfile %s for root zone.\n", keyfile);
1274
1275   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1276   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1277
1278   GNUNET_CRYPTO_short_hash(&pkey,
1279                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1280                      &zone_hash);
1281   GNUNET_free(keyfile);
1282   
1283   /**
1284    * handle to our local namestore
1285    */
1286   namestore_handle = GNUNET_NAMESTORE_connect(c);
1287
1288   if (NULL == namestore_handle)
1289   {
1290     //FIXME do error handling;
1291     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1292                "Failed to connect to the namestore!\n");
1293     GNUNET_SCHEDULER_shutdown ();
1294     return;
1295   }
1296   
1297   
1298
1299   auto_import_pkey = GNUNET_NO;
1300
1301   if (GNUNET_YES ==
1302       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1303                                             "AUTO_IMPORT_PKEY"))
1304   {
1305     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1306                "Automatic PKEY import is enabled.\n");
1307     auto_import_pkey = GNUNET_YES;
1308
1309   }
1310
1311   zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1312
1313   record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1314
1315   if (GNUNET_OK ==
1316       GNUNET_CONFIGURATION_get_value_time (c, "gns",
1317                                              "RECORD_PUT_INTERVAL",
1318                                              &record_put_interval))
1319   {
1320     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1321                "Record put interval: %llu\n",
1322                record_put_interval);
1323   }
1324
1325   if (GNUNET_OK ==
1326       GNUNET_CONFIGURATION_get_value_number (c, "gns",
1327                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
1328                                             &max_parallel_bg_queries))
1329   {
1330     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1331                "Number of allowed parallel background queries: %llu\n",
1332                max_parallel_bg_queries);
1333   }
1334
1335   if (GNUNET_YES ==
1336       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1337                                             "AUTO_IMPORT_CONFIRMATION_REQ"))
1338   {
1339     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1340                "Auto import requires user confirmation\n");
1341     ignore_pending = GNUNET_YES;
1342   }
1343
1344   if (GNUNET_OK ==
1345       GNUNET_CONFIGURATION_get_value_number(c, "gns",
1346                                             "DEFAULT_LOOKUP_TIMEOUT",
1347                                             &default_lookup_timeout_secs))
1348   {
1349     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1350                "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1351     default_lookup_timeout = GNUNET_TIME_relative_multiply(
1352                                             GNUNET_TIME_UNIT_SECONDS,
1353                                             default_lookup_timeout_secs);
1354   }
1355   
1356   /**
1357    * handle to the dht
1358    */
1359   dht_handle = GNUNET_DHT_connect(c,
1360                        //max_parallel_bg_queries); //FIXME get ht_len from cfg
1361                        1024);
1362
1363   if (NULL == dht_handle)
1364   {
1365     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1366   }
1367   
1368   if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1369                         max_parallel_bg_queries,
1370                         ignore_pending)
1371       == GNUNET_SYSERR)
1372   {
1373     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1374                "Unable to initialize resolver!\n");
1375     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1376     return;
1377   }
1378
1379   if (GNUNET_YES ==
1380       GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1381   {
1382     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1383                "DNS hijacking enabled... connecting to service.\n");
1384
1385     if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1386     {
1387       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1388                "Failed to enable the dns interceptor!\n");
1389     }
1390   }
1391   
1392   /**
1393    * Schedule periodic put
1394    * for our records
1395    * We have roughly an hour for all records;
1396    */
1397   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1398
1399   GNUNET_SERVER_add_handlers (server, handlers);
1400   
1401   //FIXME
1402   //GNUNET_SERVER_disconnect_notify (server,
1403   //                                 &client_disconnect_notification,
1404   //                                 NULL);
1405
1406   statistics = GNUNET_STATISTICS_create ("gns", c);
1407
1408   nc = GNUNET_SERVER_notification_context_create (server, 1);
1409
1410   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1411                                 NULL);
1412 }
1413
1414
1415 /**
1416  * The main function for the GNS service.
1417  *
1418  * @param argc number of arguments from the command line
1419  * @param argv command line arguments
1420  * @return 0 ok, 1 on error
1421  */
1422 int
1423 main (int argc, char *const *argv)
1424 {
1425   int ret;
1426
1427   ret =
1428       (GNUNET_OK ==
1429        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1430                            NULL)) ? 0 : 1;
1431   return ret;
1432 }
1433
1434 /* end of gnunet-service-gns.c */