-fix duh
[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  * TODO:
24  *    - Write xquery and block plugin
25  *    - The smaller FIXME issues all around
26  *
27  * @file gns/gnunet-service-gns.c
28  * @brief GNUnet GNS service
29  * @author Martin Schanzenbach
30  */
31 #include "platform.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_gns_service.h"
39 #include "block_gns.h"
40 #include "gns.h"
41
42 /* Ignore for now not used anyway and probably never will */
43 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
45
46 /**
47  * A result list for namestore queries
48  */
49 struct GNUNET_GNS_ResolverHandle
50 {
51   /* The name to resolve */
52   char *name;
53
54   /* the request handle to reply to */
55   struct GNUNET_DNS_RequestHandle *request_handle;
56   
57   /* the dns parser packet received */
58   struct GNUNET_DNSPARSER_Packet *packet;
59   
60   /* the query parsed from the packet */
61
62   struct GNUNET_DNSPARSER_Query *query;
63   
64   /* has this query been answered? how many matches */
65   int answered;
66
67   /* the authoritative zone to query */
68   GNUNET_HashCode authority;
69
70   /**
71    * we have an authority in namestore that
72    * may be able to resolve
73    */
74   int authority_found;
75
76   /* a handle for dht lookups. should be NULL if no lookups are in progress */
77   struct GNUNET_DHT_GetHandle *get_handle;
78
79 };
80
81
82 /**
83  * Our handle to the DNS handler library
84  */
85 struct GNUNET_DNS_Handle *dns_handle;
86
87 /**
88  * Our handle to the DHT
89  */
90 struct GNUNET_DHT_Handle *dht_handle;
91
92 /**
93  * Our zone's private key
94  */
95 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
96
97 /**
98  * Our handle to the namestore service
99  * FIXME maybe need a second handle for iteration
100  */
101 struct GNUNET_NAMESTORE_Handle *namestore_handle;
102
103 /**
104  * Handle to iterate over our authoritative zone in namestore
105  */
106 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
107
108 /**
109  * The configuration the GNS service is running with
110  */
111 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
112
113 /**
114  * Our notification context.
115  */
116 static struct GNUNET_SERVER_NotificationContext *nc;
117
118 /**
119  * Our zone hash
120  */
121 GNUNET_HashCode zone_hash;
122
123 /**
124  * Our tld. Maybe get from config file
125  */
126 const char* gnunet_tld = ".gnunet";
127
128 /**
129  * Useful for zone update for DHT put
130  */
131 static int num_public_records =  3600;
132 struct GNUNET_TIME_Relative dht_update_interval;
133 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
134
135 //Prototypes
136 void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer, uint32_t rd_count,
137                   const struct GNUNET_NAMESTORE_RecordData *rd);
138 void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
139
140 /**
141  * Task run during shutdown.
142  *
143  * @param cls unused
144  * @param tc unused
145  */
146 static void
147 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 {
149   //Kill zone task for it may make the scheduler hang
150   GNUNET_SCHEDULER_cancel(zone_update_taskid);
151   GNUNET_DNS_disconnect(dns_handle);
152   GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
153   GNUNET_DHT_disconnect(dht_handle);
154 }
155
156 /**
157  * Callback when record data is put into namestore
158  *
159  * @param cls the closure
160  * @param success GNUNET_OK on success
161  * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
162  */
163 void
164 on_namestore_record_put_result(void *cls,
165                                int32_t success,
166                                const char *emsg)
167 {
168   if (GNUNET_NO == success)
169   {
170     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
171     return;
172   }
173   else if (GNUNET_YES == success)
174   {
175     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
176                "records successfully put in namestore\n");
177     return;
178   }
179
180   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
181              "Error putting records into namestore: %s\n", emsg);
182 }
183
184 /**
185  * Function called when we get a result from the dht
186  * for our query
187  *
188  * @param cls the request handle
189  * @param exp lifetime
190  * @param key the key the record was stored under
191  * @param get_path get path
192  * @param get_path_length get path length
193  * @param put_path put path
194  * @param put_path_length put path length
195  * @param type the block type
196  * @param size the size of the record
197  * @param data the record data
198  */
199 void
200 process_authority_dht_result(void* cls,
201                  struct GNUNET_TIME_Absolute exp,
202                  const GNUNET_HashCode * key,
203                  const struct GNUNET_PeerIdentity *get_path,
204                  unsigned int get_path_length,
205                  const struct GNUNET_PeerIdentity *put_path,
206                  unsigned int put_path_length,
207                  enum GNUNET_BLOCK_Type type,
208                  size_t size, const void *data)
209 {
210   struct GNUNET_GNS_ResolverHandle *rh;
211   struct GNSNameRecordBlock *nrb;
212   struct GNSRecordBlock *rb;
213   uint32_t num_records;
214   char* name = NULL;
215   int i;
216   GNUNET_HashCode zone, name_hash;
217   
218   if (data == NULL)
219     return;
220   
221   //FIXME check expiration?
222   rh = (struct GNUNET_GNS_ResolverHandle *)cls;
223   nrb = (struct GNSNameRecordBlock*)data;
224   
225   GNUNET_DHT_get_stop (rh->get_handle);
226   rh->get_handle = NULL;
227   num_records = ntohl(nrb->rd_count);
228   struct GNUNET_NAMESTORE_RecordData rd[num_records];
229   name = (char*)&nrb[1];
230   rb = (struct GNSRecordBlock *)(&nrb[1] + strlen(name) + 1);
231
232   for (i=0; i<num_records; i++)
233   {
234   
235     rd[i].record_type = ntohl(rb->type);
236     rd[i].data_size = ntohl(rb->data_length);
237     rd[i].data = &rb[1];
238     rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
239     rd[i].flags = ntohl(rb->flags);
240     
241     if (strcmp(name, rh->query->name) &&
242         (rd[i].record_type == rh->query->type))
243     {
244       rh->answered = 1;
245     }
246
247   }
248
249   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
250   GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
251
252   //Save to namestore
253   GNUNET_NAMESTORE_record_put (namestore_handle,
254                                &nrb->public_key,
255                                name,
256                                exp,
257                                num_records,
258                                rd,
259                                &nrb->signature,
260                                &on_namestore_record_put_result, //cont
261                                NULL); //cls
262   
263   if (rh->answered)
264   {
265     rh->answered = 0;
266     rh->authority = zone;
267     resolve_name(rh);
268     return;
269   }
270   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No authority in records\n");
271   reply_to_dns(rh, 0, NULL);
272 }
273
274 /**
275  * Start DHT lookup for a name -> PKEY (compare NS) record in
276  * query->authority's zone
277  *
278  * @param rh the pending gns query
279  * @param name the name of the PKEY record
280  */
281 void
282 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
283 {
284   uint32_t xquery;
285   struct GNUNET_TIME_Relative timeout;
286   GNUNET_HashCode name_hash;
287   GNUNET_HashCode lookup_key;
288
289   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
290   GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
291
292   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
293   
294   xquery = htonl(GNUNET_GNS_RECORD_PKEY);
295   //FIXME how long to wait for results?
296   rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
297                        GNUNET_BLOCK_TYPE_TEST, //FIXME todo
298                        &lookup_key,
299                        5, //Replication level FIXME
300                        GNUNET_DHT_RO_NONE,
301                        &xquery, //xquery FIXME is this bad?
302                        0, // for test bp sizeof(GNUNET_GNS_RECORD_PKEY),
303                        &process_authority_dht_result,
304                        rh);
305
306 }
307
308 /**
309  * Function called when we get a result from the dht
310  * for our query
311  *
312  * @param cls the request handle
313  * @param exp lifetime
314  * @param key the key the record was stored under
315  * @param get_path get path
316  * @param get_path_length get path length
317  * @param put_path put path
318  * @param put_path_length put path length
319  * @param type the block type
320  * @param size the size of the record
321  * @param data the record data
322  */
323 void
324 process_name_dht_result(void* cls,
325                  struct GNUNET_TIME_Absolute exp,
326                  const GNUNET_HashCode * key,
327                  const struct GNUNET_PeerIdentity *get_path,
328                  unsigned int get_path_length,
329                  const struct GNUNET_PeerIdentity *put_path,
330                  unsigned int put_path_length,
331                  enum GNUNET_BLOCK_Type type,
332                  size_t size, const void *data)
333 {
334   struct GNUNET_GNS_ResolverHandle *rh;
335   struct GNSNameRecordBlock *nrb;
336   struct GNSRecordBlock *rb;
337   uint32_t num_records;
338   char* name = NULL;
339   int i;
340   GNUNET_HashCode zone, name_hash;
341   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "got dht result (size=%d)\n", size);
342   if (data == NULL)
343     return;
344
345   //FIXME maybe check expiration here, check block type
346   
347   rh = (struct GNUNET_GNS_ResolverHandle *)cls;
348   nrb = (struct GNSNameRecordBlock*)data;
349
350   GNUNET_DHT_get_stop (rh->get_handle);
351   rh->get_handle = NULL;
352   num_records = ntohl(nrb->rd_count);
353   struct GNUNET_NAMESTORE_RecordData rd[num_records];
354
355   name = (char*)&nrb[1];
356   rb = (struct GNSRecordBlock*)&name[strlen(name) + 1];
357   
358   for (i=0; i<num_records; i++)
359   {
360     rd[i].record_type = ntohl(rb->type);
361     rd[i].data_size = ntohl(rb->data_length);
362     rd[i].data = (char*)&rb[1];
363     rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
364     rd[i].flags = ntohl(rb->flags);
365     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
366                "Got name: %s (wanted %s)\n", name, rh->name);
367     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
368                "Got type: %d raw %d (wanted %d)\n",
369                rd[i].record_type, rb->type, rh->query->type);
370     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
371                "Got data length: %d\n", rd[i].data_size);
372     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
373                "Got flag %d\n", rd[i].flags);
374     //FIXME class?
375     if ((strcmp(name, rh->name) == 0) &&
376        (rd[i].record_type == rh->query->type))
377     {
378       rh->answered++;
379     }
380
381     rb = &rb[1] + rd[i].data_size;
382
383   }
384
385   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
386   GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
387   
388   //FIXME check pubkey against existing key in namestore?
389   //https://gnunet.org/bugs/view.php?id=2179
390
391   //Save to namestore
392   GNUNET_NAMESTORE_record_put (namestore_handle,
393                                &nrb->public_key,
394                                name,
395                                exp,
396                                num_records,
397                                rd,
398                                &nrb->signature,
399                                &on_namestore_record_put_result, //cont
400                                NULL); //cls
401   
402   if (rh->answered)
403     reply_to_dns(rh, num_records, rd);
404   else
405     reply_to_dns(rh, 0, NULL);
406
407 }
408
409 /**
410  * Start DHT lookup for a (name -> query->record_type) record in
411  * query->authority's zone
412  *
413  * @param rh the pending gns query context
414  * @param name the name to query record
415  */
416 void
417 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
418 {
419   uint32_t xquery;
420   struct GNUNET_TIME_Relative timeout;
421   GNUNET_HashCode name_hash;
422   GNUNET_HashCode lookup_key;
423   struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
424
425   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
426   GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
427   GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
428   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
429              "starting dht lookup for %s with key: %s\n",
430              name, (char*)&lookup_key_string);
431
432   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
433   
434   xquery = htonl(rh->query->type);
435   //FIXME how long to wait for results?
436   rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
437                        GNUNET_BLOCK_TYPE_TEST, //FIXME todo
438                        &lookup_key,
439                        5, //Replication level FIXME
440                        GNUNET_DHT_RO_NONE,
441                        &xquery, //xquery FIXME is this bad?
442                        0, //for test bp sizeof(rh->query->type),
443                        &process_name_dht_result,
444                        rh);
445
446 }
447
448 //Prototype
449 void
450 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
451
452 /**
453  * This is a callback function that should give us only PKEY
454  * records. Used to query the namestore for the authority (PKEY)
455  * for 'name'
456  *
457  * @param cls the pending query
458  * @param key the key of the zone we did the lookup
459  * @param expiration expiration date of the record data set in the namestore
460  * @param name the name for which we need an authority
461  * @param rd_count the number of records with 'name'
462  * @param rd the record data
463  * @param signature the signature of the authority for the record data
464  */
465 void
466 process_authority_lookup(void* cls,
467                    const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
468                    struct GNUNET_TIME_Absolute expiration,
469                    const char *name,
470                    unsigned int rd_count,
471                    const struct GNUNET_NAMESTORE_RecordData *rd,
472                    const struct GNUNET_CRYPTO_RsaSignature *signature)
473 {
474   struct GNUNET_GNS_ResolverHandle *rh;
475   struct GNUNET_TIME_Relative remaining_time;
476   GNUNET_HashCode zone;
477
478   rh = (struct GNUNET_GNS_ResolverHandle *)cls;
479   GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
480   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
481   
482   /**
483    * No authority found in namestore.
484    */
485   if (rd_count == 0)
486   {
487     /**
488      * We did not find an authority in the namestore
489      * _IF_ the current authoritative zone is us we cannot resolve
490      * _ELSE_ we can still check the _expired_ dht
491      */
492     if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
493         (remaining_time.rel_value == 0))
494     {
495       resolve_authority_dht(rh, name);
496       return;
497     }
498     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
499     reply_to_dns(rh, 0, NULL);
500     return;
501   }
502
503   //Note only 1 pkey should have been returned.. anything else would be strange
504   /**
505    * We found an authority that may be able to help us
506    * move on with query
507    */
508   int i;
509   for (i=0; i<rd_count;i++)
510   {
511   
512     if (strcmp(name, rh->query->name) && rd[i].record_type
513         != GNUNET_GNS_RECORD_PKEY)
514       continue;
515     
516       if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
517           == 0)
518       {
519         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
520         if (remaining_time.rel_value == 0)
521         {
522           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
523                      "This dht entry is expired. Refreshing\n");
524           resolve_authority_dht(rh, name);
525         }
526
527         continue;
528       }
529
530       GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
531       GNUNET_CRYPTO_hash(rd[i].data, GNUNET_CRYPTO_RSA_KEY_LENGTH,
532                          &rh->authority);
533       resolve_name(rh);
534       return;
535       
536   }
537     
538   /**
539    * no answers found
540    */
541   
542   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
543              "Authority lookup successful but no PKEY... never get here?\n");
544   reply_to_dns(rh, 0, NULL);
545 }
546
547
548 /**
549  * Reply to client with the result from our lookup.
550  *
551  * @param rh the request handle of the lookup
552  * @param rd_count the number of records to return
553  * @param rd the record data
554  */
555 void
556 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
557              const struct GNUNET_NAMESTORE_RecordData *rd)
558 {
559   int i;
560   size_t len;
561   int ret;
562   char *buf;
563   struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
564   struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
565   struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
566   packet->answers = answer_records;
567   packet->additional_records = additional_records;
568   
569   len = sizeof(struct GNUNET_DNSPARSER_Record*);
570   for (i=0; i < rd_count; i++)
571   {
572     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
573                "Adding type %d to DNS response\n", rd[i].record_type);
574     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
575     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
576     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
577     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
578     if (rd[i].record_type == rh->query->type)
579     {
580       answer_records[i].name = rh->query->name;
581       answer_records[i].type = rd[i].record_type;
582       answer_records[i].data.raw.data_len = rd[i].data_size;
583       answer_records[i].data.raw.data = (char*)rd[i].data;
584       answer_records[i].expiration_time = rd[i].expiration;
585       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
586     }
587     else
588     {
589       additional_records[i].name = rh->query->name;
590       additional_records[i].type = rd[i].record_type;
591       additional_records[i].data.raw.data_len = rd[i].data_size;
592       additional_records[i].data.raw.data = (char*)rd[i].data;
593       additional_records[i].expiration_time = rd[i].expiration;
594       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
595     }
596     //GNUNET_free(i->record); DO this later!
597   }
598   
599   packet->num_answers = rh->answered; //answer->num_records;
600   packet->num_additional_records = rd_count-(rh->answered);
601   
602   if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
603     packet->flags.authoritative_answer = 1;
604   else
605    packet->flags.authoritative_answer = 0;
606
607   if (rd == NULL)
608     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
609   else
610     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
611   
612   packet->flags.query_or_response = 1;
613
614   //FIXME this is silently discarded
615   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
616              "Building DNS response\n");
617   ret = GNUNET_DNSPARSER_pack (packet,
618                                1024, /* FIXME magic from dns redirector */
619                                &buf,
620                                &len);
621   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
622              "Built DNS response! (ret=%d,len=%d)\n", ret, len);
623   if (ret == GNUNET_OK)
624   {
625     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
626                "Answering DNS request\n");
627     GNUNET_DNS_request_answer(rh->request_handle,
628                               len,
629                               buf);
630     //GNUNET_free(answer);
631     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
632     //FIXME return code, free datastructures
633   }
634   else
635   {
636     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
637                "Error building DNS response! (ret=%d)", ret);
638   }
639
640   //FIXME into free_resolver(rh)
641   GNUNET_free(rh->name);
642   GNUNET_free(rh);
643 }
644
645
646 /**
647  * Namestore calls this function if we have an entry for this name.
648  * (or data=null to indicate the lookup has finished
649  *
650  * @param cls the pending query
651  * @param key the key of the zone we did the lookup
652  * @param expiration expiration date of the namestore entry
653  * @param name the name for which we need an authority
654  * @param rd_count the number of records with 'name'
655  * @param rd the record data
656  * @param signature the signature of the authority for the record data
657  */
658 static void
659 process_authoritative_result(void* cls,
660                   const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
661                   struct GNUNET_TIME_Absolute expiration,
662                   const char *name, unsigned int rd_count,
663                   const struct GNUNET_NAMESTORE_RecordData *rd,
664                   const struct GNUNET_CRYPTO_RsaSignature *signature)
665 {
666   struct GNUNET_GNS_ResolverHandle *rh;
667   struct GNUNET_TIME_Relative remaining_time;
668   GNUNET_HashCode zone;
669
670   rh = (struct GNUNET_GNS_ResolverHandle *) cls;
671   GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
672   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
673
674   //FIXME Handle results in rd
675
676   if (rd_count == 0)
677   {
678     /**
679      * Lookup terminated and no results
680      * -> DHT Phase unless data is recent
681      */
682     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
683                "Namestore lookup for %s terminated without results\n", name);
684     
685     /**
686      * if this is not our zone we cannot rely on the namestore to be
687      * complete. -> Query DHT
688      */
689     if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
690     {
691       if (remaining_time.rel_value == 0)
692       {
693         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
694                    "trying dht...\n");
695         resolve_name_dht(rh, name);
696         return;
697       }
698       else
699       {
700         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
701                    "Record is still recent. No DHT lookup\n");
702       }
703     }
704
705     /**
706      * Our zone and no result? Cannot resolve TT
707      */
708     GNUNET_assert(rh->answered == 0);
709     reply_to_dns(rh, 0, NULL); //answered should be 0
710     return;
711
712   }
713   else
714   {
715     
716     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
717                "Processing additional result %s from namestore\n", name);
718     int i;
719     for (i=0; i<rd_count;i++)
720     {
721       
722       if (strcmp(name, rh->query->name) && rd[i].record_type != rh->query->type)
723         continue;
724       
725       if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
726           == 0)
727       {
728         //FIXME there is a catch here...
729         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
730         continue;
731       }
732       
733       rh->answered++;
734       
735     }
736     
737     /**
738      * no answers found
739      * consult dht if expired
740      */
741     if ((remaining_time.rel_value == 0) && (rh->answered == 0))
742     {
743       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
744                  "This dht entry is old. Refreshing.\n");
745       resolve_name_dht(rh, name);
746       return;
747     }
748     
749     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found %d answer(s) to query!\n",
750                rh->answered);
751
752     reply_to_dns(rh, rd_count, rd);
753   }
754 }
755
756 /**
757  * Determine if this name is canonical.
758  * i.e.
759  * a.b.gnunet  = not canonical
760  * a           = canonical
761  *
762  * @param name the name to test
763  * @return 1 if canonical
764  */
765 int
766 is_canonical(char* name)
767 {
768   uint32_t len = strlen(name);
769   int i;
770
771   for (i=0; i<len; i++)
772   {
773     if (*(name+i) == '.')
774       return 0;
775   }
776   return 1;
777 }
778
779 /**
780  * Move one level up in the domain hierarchy and return the
781  * passed top level domain.
782  *
783  * @param name the domain
784  * @return the tld
785  */
786 char* pop_tld(char* name)
787 {
788   uint32_t len;
789
790   if (is_canonical(name))
791     return NULL;
792
793   for (len = strlen(name); len > 0; len--)
794   {
795     if (*(name+len) == '.')
796       break;
797   }
798
799   if (len == 0)
800     return NULL; //Error
801
802   name[len] = '\0'; //terminate string
803
804   return (name+len+1);
805 }
806
807
808 /**
809  * The first phase of resolution.
810  * First check if the name is canonical.
811  * If it is then try to resolve directly.
812  * If not then first have to resolve the authoritative entities.
813  *
814  * @param rh the pending lookup
815  * @param zone the zone we are currently resolving in
816  */
817 void
818 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
819 {
820   if (is_canonical(rh->name))
821   {
822     //We only need to check this zone's ns
823     GNUNET_NAMESTORE_lookup_record(namestore_handle,
824                                &rh->authority,
825                                rh->name,
826                                rh->query->type,
827                                &process_authoritative_result,
828                                rh);
829   }
830   else
831   {
832     //We have to resolve the authoritative entity
833     char *new_authority = pop_tld(rh->name);
834     GNUNET_NAMESTORE_lookup_record(namestore_handle,
835                                  &rh->authority,
836                                  new_authority,
837                                  GNUNET_GNS_RECORD_PKEY,
838                                  &process_authority_lookup,
839                                  rh);
840   }
841 }
842
843 /**
844  * Entry point for name resolution
845  * Lookup local namestore of our zone.
846  *
847  * Setup a new query and try to resolve
848  *
849  * @param request the request handle of the DNS request from a client
850  * @param p the DNS query packet we received
851  * @param q the DNS query we received parsed from p
852  */
853 void
854 start_resolution(struct GNUNET_DNS_RequestHandle *request,
855                  struct GNUNET_DNSPARSER_Packet *p,
856                  struct GNUNET_DNSPARSER_Query *q)
857 {
858   struct GNUNET_GNS_ResolverHandle *rh;
859   
860   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
861               q->name);
862   
863   rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
864   rh->packet = p;
865   rh->query = q;
866   rh->authority = zone_hash;
867   
868   //FIXME do not forget to free!!
869   rh->name = GNUNET_malloc(strlen(q->name)
870                               - strlen(gnunet_tld) + 1);
871   memset(rh->name, 0,
872          strlen(q->name)-strlen(gnunet_tld) + 1);
873   memcpy(rh->name, q->name,
874          strlen(q->name)-strlen(gnunet_tld));
875
876   rh->request_handle = request;
877
878   //Start resolution in our zone
879   resolve_name(rh);
880 }
881
882 /**
883  * The DNS request handler
884  * Called for every incoming DNS request.
885  *
886  * @param cls closure
887  * @param rh request handle to user for reply
888  * @param request_length number of bytes in request
889  * @param request udp payload of the DNS request
890  */
891 void
892 handle_dns_request(void *cls,
893                    struct GNUNET_DNS_RequestHandle *rh,
894                    size_t request_length,
895                    const char *request)
896 {
897   struct GNUNET_DNSPARSER_Packet *p;
898   char *tldoffset;
899
900   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
901   p = GNUNET_DNSPARSER_parse (request, request_length);
902   
903   if (NULL == p)
904   {
905     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
906                 "Received malformed DNS packet, leaving it untouched\n");
907     GNUNET_DNS_request_forward (rh);
908     return;
909   }
910   
911   /**
912    * Check tld and decide if we or
913    * legacy dns is responsible
914    *
915    * FIXME now in theory there could be more than 1 query in the request
916    * but if this is case we get into trouble:
917    * either we query the GNS or the DNS. We cannot do both!
918    * So I suggest to either only allow a single query per request or
919    * only allow GNS or DNS requests.
920    * The way it is implemented here now is buggy and will lead to erratic
921    * behaviour (if multiple queries are present).
922    */
923   if (p->num_queries == 0)
924   {
925     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926                 "No Queries in DNS packet... forwarding\n");
927     GNUNET_DNS_request_forward (rh);
928   }
929
930   if (p->num_queries > 1)
931   {
932     //Note: We could also look for .gnunet
933     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934                 ">1 queriy in DNS packet... odd. We only process #1\n");
935   }
936
937
938   tldoffset = p->queries[0].name + strlen(p->queries[0].name);
939
940   while ((*tldoffset) != '.')
941     tldoffset--;
942   
943   if (0 == strcmp(tldoffset, gnunet_tld))
944   {
945     start_resolution(rh, p, p->queries);
946   }
947   else
948   {
949     /**
950      * This request does not concern us. Forward to real DNS.
951      */
952     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
953                "Request for %s is forwarded to DNS\n", p->queries[0].name);
954     GNUNET_DNS_request_forward (rh);
955   }
956
957 }
958
959 /**
960  * test function that stores some data in the namestore
961  */
962 void
963 put_some_records(void)
964 {
965   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
966   /* put a few records into namestore */
967   char* ipA = "1.2.3.4";
968   char* ipB = "5.6.7.8";
969   //struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create ();
970   //struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
971   //bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
972
973   //GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
974
975   //GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
976
977   //GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
978
979   struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
980   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
981   struct GNUNET_NAMESTORE_RecordData rda;
982   //struct GNUNET_NAMESTORE_RecordData rdb;
983   struct GNUNET_NAMESTORE_RecordData rdb_web;
984
985   GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
986   GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
987
988   rda.data_size = sizeof(struct in_addr);
989   rdb_web.data_size = sizeof(struct in_addr);
990   //rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
991   rda.data = alice;
992   //rdb.data = bob;
993   rdb_web.data = web;
994   rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
995   rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
996   //rdb.record_type = GNUNET_GNS_RECORD_PKEY;
997   rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
998   rda.expiration = GNUNET_TIME_absolute_get_forever ();
999   //rdb.expiration = GNUNET_TIME_absolute_get_forever ();
1000   
1001   //alice.gnunet A IN 1.2.3.4
1002   GNUNET_NAMESTORE_record_create (namestore_handle,
1003                                zone_key,
1004                                "alice",
1005                                &rda,
1006                                NULL,
1007                                NULL);
1008   GNUNET_NAMESTORE_record_create (namestore_handle,
1009                                   zone_key,
1010                                   "www",
1011                                   &rdb_web,
1012                                   NULL,
1013                                   NULL);
1014 /*
1015   //www.bob.gnunet A IN 5.6.7.8
1016   GNUNET_NAMESTORE_record_create (namestore_handle,
1017                                zone_key,
1018                                "bob",
1019                                &rdb,
1020                                NULL,
1021                                NULL);*/
1022   /*GNUNET_NAMESTORE_record_put(namestore_handle,
1023                               zone_key,
1024                               "www",
1025                               GNUNET_TIME_absolute_get_forever (),
1026                               1,
1027                               &rdb_web,
1028                               NULL, //Signature
1029                               NULL, //Cont
1030                               NULL); //cls*/
1031 }
1032
1033 void
1034 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1035 {
1036   GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1037 }
1038
1039 void
1040 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1041 {
1042   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "put request transmitted\n");
1043 }
1044
1045 /* prototype */
1046 static void
1047 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1048
1049 /**
1050  * Function used to put all records successively into the DHT.
1051  * FIXME bug here
1052  *
1053  * @param cls the closure (NULL)
1054  * @param key the public key of the authority (ours)
1055  * @param expiration lifetime of the namestore entry
1056  * @param name the name of the records
1057  * @param rd_count the number of records in data
1058  * @param rd the record data
1059  * @param signature the signature for the record data
1060  */
1061 void
1062 put_gns_record(void *cls,
1063                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1064                 struct GNUNET_TIME_Absolute expiration,
1065                 const char *name,
1066                 unsigned int rd_count,
1067                 const struct GNUNET_NAMESTORE_RecordData *rd,
1068                 const struct GNUNET_CRYPTO_RsaSignature *signature)
1069 {
1070   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1071   struct GNUNET_TIME_Relative timeout;
1072   struct GNSNameRecordBlock *nrb;
1073   struct GNSRecordBlock *rb;
1074   GNUNET_HashCode name_hash;
1075   GNUNET_HashCode xor_hash;
1076   struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1077   int i;
1078   uint32_t rd_payload_length;
1079
1080   if (NULL == name) //We're done
1081   {
1082     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Zone iteration finished\n");
1083     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1084     zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1085                                                    NULL);
1086     return;
1087   }
1088   
1089   rd_payload_length = rd_count * sizeof(struct GNSRecordBlock);
1090   rd_payload_length += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1091   //Calculate payload size
1092   for (i=0; i<rd_count; i++)
1093   {
1094     rd_payload_length += rd[i].data_size;
1095   }
1096   
1097   nrb = GNUNET_malloc(rd_payload_length);
1098   
1099   if (signature != NULL)
1100     memcpy(&nrb->signature, signature,
1101          sizeof(struct GNUNET_CRYPTO_RsaSignature));
1102   //FIXME signature purpose
1103   memcpy(&nrb->public_key, key,
1104          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1105
1106   nrb->rd_count = htonl(rd_count);
1107
1108   memcpy(&nrb[1], name, strlen(name) + 1); //FIXME is this 0 terminated??-sure hope so for we use strlen
1109
1110   rb = (struct GNSRecordBlock *)((char*)&nrb[1] + strlen(name) + 1);
1111
1112   for (i=0; i<rd_count; i++)
1113   {
1114     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "putting record with type %d\n",
1115                rd[i].record_type);
1116     rb->type = htonl(rd[i].record_type);
1117     rb->expiration = GNUNET_TIME_absolute_hton(rd[i].expiration);
1118     rb->data_length = htonl(rd[i].data_size);
1119     rb->flags = htonl(rd[i].flags);
1120     memcpy(&rb[1], rd[i].data, rd[i].data_size);
1121     rb = &rb[1] + rd[i].data_size;
1122   }
1123
1124   /**
1125    * FIXME magic number 20 move to config file
1126    * DHT_WAIT_TIMEOUT
1127    */
1128   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1129   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1130   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1131   GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1132   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1133              "putting records for %s under key: %s with size %d\n",
1134              name, (char*)&xor_hash_string, rd_payload_length);
1135
1136   GNUNET_DHT_put (dht_handle, &xor_hash,
1137                   5, //replication level
1138                   GNUNET_DHT_RO_NONE,
1139                   GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1140                   rd_payload_length,
1141                   (char*)nrb,
1142                   expiration,
1143                   timeout,
1144                   &record_dht_put, //FIXME continuation needed? success check? yes ofc
1145                   NULL); //cls for cont
1146   
1147   num_public_records++;
1148
1149   /**
1150    * Reschedule periodic put
1151    */
1152   zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1153                                 &update_zone_dht_next,
1154                                 NULL);
1155
1156 }
1157
1158 void
1159 put_trusted(char* name, char* keyfile)
1160 {
1161   struct GNUNET_NAMESTORE_RecordData rd;
1162   struct GNUNET_CRYPTO_RsaPrivateKey *key;
1163   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1164   pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1165
1166   key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1167   GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1168   rd.data = pkey;
1169   rd.expiration = GNUNET_TIME_absolute_get_forever ();
1170   rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1171   rd.record_type = GNUNET_GNS_RECORD_PKEY;
1172
1173   GNUNET_NAMESTORE_record_create (namestore_handle,
1174                                   zone_key,
1175                                   name,
1176                                   &rd,
1177                                   NULL,
1178                                   NULL);
1179 }
1180
1181
1182
1183 /**
1184  * Periodically iterate over our zone and store everything in dht
1185  *
1186  * @param cls NULL
1187  * @param tc task context
1188  */
1189 static void
1190 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1191 {
1192   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1193   if (0 == num_public_records)
1194   {
1195     dht_update_interval = GNUNET_TIME_relative_multiply(
1196                                                       GNUNET_TIME_UNIT_SECONDS,
1197                                                       1);
1198   }
1199   else
1200   {
1201     dht_update_interval = GNUNET_TIME_relative_multiply(
1202                                                       GNUNET_TIME_UNIT_SECONDS,
1203                                                      (3600/num_public_records));
1204   }
1205   num_public_records = 0; //start counting again
1206   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1207                                                           &zone_hash,
1208                                                           GNUNET_NAMESTORE_RF_AUTHORITY,
1209                                                           GNUNET_NAMESTORE_RF_PRIVATE,
1210                                                           &put_gns_record,
1211                                                           NULL);
1212 }
1213
1214 /**
1215  * Process GNS requests.
1216  *
1217  * @param cls closure
1218  * @param server the initialized server
1219  * @param c configuration to use
1220  */
1221 static void
1222 run (void *cls, struct GNUNET_SERVER_Handle *server,
1223      const struct GNUNET_CONFIGURATION_Handle *c)
1224 {
1225   
1226   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1227   char* keyfile;
1228   char* trusted_entities;
1229   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1230
1231   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1232                                              "ZONEKEY", &keyfile))
1233   {
1234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1235                 "No private key for root zone specified%s!\n", keyfile);
1236     GNUNET_SCHEDULER_shutdown(0);
1237     return;
1238   }
1239
1240   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1241   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1242   //zone_key = GNUNET_CRYPTO_rsa_key_create ();
1243
1244   GNUNET_CRYPTO_hash(&pkey, GNUNET_CRYPTO_RSA_KEY_LENGTH,
1245                      &zone_hash);
1246   
1247   nc = GNUNET_SERVER_notification_context_create (server, 1);
1248
1249   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1250                                 NULL);
1251
1252   if (GNUNET_YES ==
1253       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1254                                             "HIJACK_DNS"))
1255   {
1256     /**
1257      * Do gnunet dns init here
1258      */
1259     dns_handle = GNUNET_DNS_connect(c,
1260                                     GNUNET_DNS_FLAG_PRE_RESOLUTION,
1261                                     &handle_dns_request, /* rh */
1262                                     NULL); /* Closure */
1263     if (NULL == dns_handle)
1264     {
1265       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1266                "Failed to connect to the dnsservice!\n");
1267     }
1268   }
1269
1270   
1271
1272   /**
1273    * handle to our local namestore
1274    */
1275   namestore_handle = GNUNET_NAMESTORE_connect(c);
1276
1277   if (NULL == namestore_handle)
1278   {
1279     //FIXME do error handling;
1280     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1281                "Failed to connect to the namestore!\n");
1282     GNUNET_SCHEDULER_shutdown(0);
1283     return;
1284   }
1285   
1286   char* trusted_start;
1287   char* trusted_name;
1288   char *trusted_key;
1289   int trusted_len;
1290   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1291                                                       "TRUSTED",
1292                                                       &trusted_entities))
1293   {
1294     trusted_start = trusted_entities;
1295     trusted_len = strlen(trusted_entities);
1296     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1297                "Found trusted entities in config file, importing\n");
1298     while ((trusted_entities-trusted_start) < trusted_len)
1299     {
1300       trusted_name = trusted_entities;
1301       while (*trusted_entities != ':')
1302         trusted_entities++;
1303       *trusted_entities = '\0';
1304       trusted_entities++;
1305       trusted_key = trusted_entities;
1306       while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1307         trusted_entities++;
1308       *trusted_entities = '\0';
1309       trusted_entities++;
1310       
1311       if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1312       {
1313         GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1314                  trusted_name,
1315                  trusted_key);
1316         put_trusted(trusted_name, trusted_key);
1317       }
1318       else
1319       {
1320         GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Keyfile %s does not exist!\n",
1321                    trusted_key);
1322         //put_trusted(trusted_name, trusted_key); //FIXME for testing
1323       }
1324     }
1325
1326   }
1327
1328   /**
1329    * handle to the dht
1330    */
1331   dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1332
1333   if (NULL == dht_handle)
1334   {
1335     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1336   }
1337
1338   put_some_records(); //FIXME for testing
1339   
1340   /**
1341    * Schedule periodic put
1342    * for our records
1343    * We have roughly an hour for all records;
1344    */
1345   dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1346                                                       1);
1347   zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1348   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1349
1350 }
1351
1352
1353 /**
1354  * The main function for the GNS service.
1355  *
1356  * @param argc number of arguments from the command line
1357  * @param argv command line arguments
1358  * @return 0 ok, 1 on error
1359  */
1360 int
1361 main (int argc, char *const *argv)
1362 {
1363   int ret;
1364
1365   ret =
1366       (GNUNET_OK ==
1367        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1368                            NULL)) ? 0 : 1;
1369   return ret;
1370 }
1371
1372 /* end of gnunet-service-gns.c */