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