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