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