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