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