-big change to resolver
[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  *    - Finish dht lookup
25  *    - Think about mixed dns queries (.gnunet and .org)
26  *    - The smaller FIXME issues all around
27  *
28  * @file gns/gnunet-service-gns.c
29  * @brief GNUnet GNS service
30  * @author Martin Schanzenbach
31  */
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_dht_service.h"
38 #include "gnunet_namestore_service.h"
39 #include "gnunet_gns_service.h"
40 #include "gns.h"
41
42 /* Ignore for now not used anyway and probably never will */
43 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
45
46 struct GNUNET_GNS_QueryRecordList
47 {
48   /**
49    * DLL
50    */
51   struct GNUNET_GNS_QueryRecordList * next;
52   struct GNUNET_GNS_QueryRecordList * prev;
53
54   struct GNUNET_DNSPARSER_Record * record;
55 };
56
57 /**
58  * A result list for namestore queries
59  */
60 struct GNUNET_GNS_PendingQuery
61 {
62   /* the answer packet */
63   struct GNUNET_DNSPARSER_Packet *answer;
64
65   /* records to put into answer packet */
66   struct GNUNET_GNS_QueryRecordList * records_head;
67   struct GNUNET_GNS_QueryRecordList * records_tail;
68
69   int num_records;
70   int num_authority_records; //FIXME are all of our replies auth?
71   
72   char *original_name;
73   char *name;
74
75   uint16_t type;
76   /* the dns request id */
77   int id; // FIXME can handle->request_id also be used here?
78
79   /* the request handle to reply to */
80   struct GNUNET_DNS_RequestHandle *request_handle;
81
82   /* hast this query been answered? */
83   int answered;
84
85   /* the authoritative zone to query */
86   GNUNET_HashCode *authority;
87
88   /* we have an authority in namestore that
89    * may be able to resolve
90    */
91   int authority_found;
92 };
93
94
95 /**
96  * Our handle to the DNS handler library
97  */
98 struct GNUNET_DNS_Handle *dns_handle;
99
100 /**
101  * Our handle to the DHT
102  */
103 struct GNUNET_DHT_Handle *dht_handle;
104
105 struct GNUNET_TIME_Relative dht_update_interval;
106
107 /**
108  * Our zone's private key
109  */
110 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
111
112 /**
113  * Our handle to the namestore service
114  */
115 struct GNUNET_NAMESTORE_Handle *namestore_handle;
116
117 /**
118  * The configuration the GNS service is running with
119  */
120 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
121
122 /**
123  * Our notification context.
124  */
125 static struct GNUNET_SERVER_NotificationContext *nc;
126
127 /**
128  * Our zone hash
129  */
130 GNUNET_HashCode *zone_hash;
131
132 /**
133  * Task run during shutdown.
134  *
135  * @param cls unused
136  * @param tc unused
137  */
138 static void
139 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 {
141   GNUNET_DNS_disconnect(dns_handle);
142   GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
143   GNUNET_DHT_disconnect(dht_handle);
144 }
145
146 /**
147  * FIXME
148  * This is where it gets tricky
149  * 1. we store (cache) all replies. Simple.
150  * 2. If we see an authority "closer" to the name
151  * we have to start a new query. Unless we get
152  * a resolution.
153  * It is important that the authority is closer
154  * because else we might end up in an endless loop
155  * (maybe keep track of queried keys?)
156  * Of course we could just limit the resolution
157  * with a timeout (makes sense for clients) but we need
158  * to know when to stop querying.
159  * 3. Also the name returned for the record here will probably
160  * not match our name. How do we check this?
161  */
162 void
163 handle_dht_reply(void* cls,
164                  struct GNUNET_TIME_Absolute exp,
165                  const GNUNET_HashCode * key,
166                  const struct GNUNET_PeerIdentity *get_path,
167                  unsigned int get_path_length,
168                  const struct GNUNET_PeerIdentity *put_path,
169                  unsigned int put_path_length,
170                  enum GNUNET_BLOCK_Type type,
171                  size_t size, const void *data)
172 {
173 }
174
175 void
176 resolve_authority_dht(struct GNUNET_GNS_PendingQuery *query)
177 {
178 }
179
180 /**
181  * This is a callback function that should give us only PKEY
182  * records. Used to iteratively query the namestore for 'closest'
183  * authority.
184  *
185  * @param cls the pending query
186  * @param zone our zone hash
187  * @param name the name for which we need an authority
188  * @param record_type the type of record (PKEY)
189  * @param expiration expiration date of the record
190  * @param flags namestore record flags
191  * @param sig_loc the location of the record in the signature tree
192  * @param size the size of the record
193  * @param data the record data
194  */
195 void
196 process_authority_lookup(void* cls, const GNUNET_HashCode *zone,
197                    const char *name, uint32_t record_type,
198                    struct GNUNET_TIME_Absolute expiration,
199                    enum GNUNET_NAMESTORE_RecordFlags flags,
200                    const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
201                    size_t size, const void *data)
202 {
203   struct GNUNET_GNS_PendingQuery *query;
204
205   query = (struct GNUNET_GNS_PendingQuery *)cls;
206   
207   /**
208    * No authority found in namestore.
209    */
210   if (NULL == data)
211   {
212     if (query->authority_found)
213     {
214       query->authority_found = 0;
215       //FIXME continue lookup
216       return;
217     }
218
219     /**
220      * We did not find an authority in the namestore
221      * _IF_ the current authoritative zone is not us. we can
222      * check the dht.
223      * _ELSE_ we cannot resolve
224      */
225     if (GNUNET_CRYPTO_hash_cmp(zone, zone_hash))
226     {
227       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "NX record\n");
228       //FIXME return NX answer
229       return;
230     }
231
232     resolve_authority_dht(query);
233     return;
234   }
235   
236   /**
237    * We found an authority that may be able to help us
238    * move on with query
239    */
240   query->authority_found = 1;
241   GNUNET_HashCode *key = (GNUNET_HashCode*) data; //FIXME i assume this works
242   query->authority = key;
243   
244 }
245
246
247 /**
248  * Reply to client with the result from our lookup.
249  *
250  * @param answer the pending query used in the lookup
251  */
252 void
253 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
254 {
255   struct GNUNET_GNS_QueryRecordList *i;
256   struct GNUNET_DNSPARSER_Packet *packet;
257   struct GNUNET_DNSPARSER_Flags dnsflags;
258   int j;
259   size_t len;
260   int ret;
261   char *buf;
262   
263   packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
264   packet->answers =
265     GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
266   
267   len = sizeof(struct GNUNET_DNSPARSER_Record*);
268   j = 0;
269   for (i=answer->records_head; i != NULL; i=i->next)
270   {
271     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
272                "Adding %s to DNS response\n", i->record->name);
273     memcpy(&packet->answers[j], 
274            i->record,
275            sizeof (struct GNUNET_DNSPARSER_Record));
276     GNUNET_free(i->record);
277     j++;
278   }
279   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
280   /* FIXME how to handle auth, additional etc */
281   packet->num_answers = answer->num_records;
282   packet->num_authority_records = answer->num_authority_records;
283
284   dnsflags.authoritative_answer = 1;
285   dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
286   dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
287   dnsflags.query_or_response = 1;
288   packet->flags = dnsflags;
289
290   packet->id = answer->id;
291   
292   //FIXME this is silently discarded
293   ret = GNUNET_DNSPARSER_pack (packet,
294                                1024, /* FIXME magic from dns redirector */
295                                &buf,
296                                &len);
297   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
298              "Built DNS response! (ret=%d)\n", ret);
299   if (ret == GNUNET_OK)
300   {
301     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
302                "Answering DNS request\n");
303     GNUNET_DNS_request_answer(answer->request_handle,
304                               len,
305                               buf);
306     GNUNET_free(answer);
307     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
308     //FIXME return code, free datastructures
309   }
310   else
311   {
312     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
313                "Error building DNS response! (ret=%d)", ret);
314   }
315 }
316
317 void
318 resolve_name_dht(struct GNUNET_GNS_PendingQuery *query)
319 {
320 }
321
322 /**
323  * Namestore calls this function if we have an entry for this name.
324  * (or data=null to indicate the lookup has finished)
325  *
326  * @param cls the pending query
327  * @param zone the zone of the lookup
328  * @param name the name looked up
329  * @param record_type the record type
330  * @param expiration lifetime of the record
331  * @param flags record flags
332  * @param sig_loc location of the record in the signature tree
333  * @param size the size of the record
334  * @param data the record data
335  */
336 static void
337 process_authoritative_result(void* cls, const GNUNET_HashCode *zone,
338                   const char *name, uint32_t record_type,
339                   struct GNUNET_TIME_Absolute expiration,
340                   enum GNUNET_NAMESTORE_RecordFlags flags,
341                   const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
342                   size_t size, const void *data)
343 {
344   struct GNUNET_GNS_PendingQuery *query;
345   struct GNUNET_GNS_QueryRecordList *qrecord;
346   struct GNUNET_DNSPARSER_Record *record;
347   query = (struct GNUNET_GNS_PendingQuery *) cls;
348
349
350   if (NULL == data)
351   {
352     /**
353      * FIXME
354      * Lookup terminated
355      * Do we have what we need to answer?
356      * If not -> DHT Phase
357      * if full_name == next_name and not anwered we cannot resolve
358      */
359     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
360                "Namestore lookup terminated. (answered=%d)", query->answered);
361     if (query->answered)
362     {
363       reply_to_dns(query);
364       return;
365     }
366
367     /**
368      * if this is not our zone we cannot rely on the namestore to be
369      * complete. -> Query DHT
370      */
371     if (!GNUNET_CRYPTO_hash_cmp(zone, zone_hash))
372     {
373       //FIXME todo
374       resolve_name_dht(query);
375       return;
376     }
377
378     /**
379      * Our zone and no result? Cannot resolve TT
380      * FIXME modify query to say NX
381      */
382     return;
383
384   }
385   else
386   {
387     /**
388      * Record found
389      */
390     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
391                "Processing additional result for %s from namestore\n", name);
392
393     qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
394     record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
395     qrecord->record = record;
396
397     record->name = (char*)query->original_name;
398
399     /**
400      * FIXME for gns records this requires the dnsparser to be modified!
401      * or use RAW. But RAW data need serialization!
402      * maybe store record data appropriately in namestore to avoid a
403      * huge switch statement?
404      */
405     if (record_type == GNUNET_DNSPARSER_TYPE_A)
406     {
407       record->data.raw.data = (char*)data;
408       record->data.raw.data_len = size;
409     }
410     record->expiration_time = expiration;
411     record->type = record_type;
412     record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
413     
414     //FIXME authoritative answer if we find a result in namestore
415     if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
416     {
417       //query->num_authority_records++;
418     }
419     
420     /**
421      * This seems to take into account that the result could
422      * be different in name and or record type...
423      * but to me this does not make sense
424      */
425     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
426     query->answered = 1;
427
428     query->num_records++;
429
430     /**
431      * FIXME watch for leaks
432      * properly free pendingquery when the time comes
433      */
434     GNUNET_CONTAINER_DLL_insert(query->records_head,
435                                 query->records_tail,
436                                 qrecord);
437   }
438 }
439
440 int
441 is_canonical(char* name)
442 {
443   return 0;
444 }
445
446 char* move_up(char* name)
447 {
448   return name;
449 }
450
451 void
452 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone)
453 {
454   if (is_canonical(query->name))
455   {
456     //We only need to check this zone's ns
457     GNUNET_NAMESTORE_lookup_name(namestore_handle,
458                                zone,
459                                query->name,
460                                query->type,
461                                &process_authoritative_result,
462                                query);
463   }
464   else
465   {
466     //We have to resolve the authoritative entity
467     char *new_authority = move_up(query->name);
468     GNUNET_NAMESTORE_lookup_name(namestore_handle,
469                                  zone,
470                                  new_authority,
471                                  GNUNET_GNS_RECORD_PKEY,
472                                  &process_authority_lookup,
473                                  query);
474   }
475 }
476
477 /**
478  * Phase 1 of name resolution
479  * Lookup local namestore. If we find a match there we can
480  * provide an authoritative answer without the dht.
481  * If we don't we have to start querying the dht.
482  *
483  * FIXME now it is possible that we have a foreign zone (or even the result)
484  * cached in our namestore. Look up as well? We need a list of cached zones
485  * then.
486  *
487  * @param rh the request handle of the DNS request from a client
488  * @param name the name to look up
489  * @param id the id of the dns request (for the reply)
490  * @param type the record type to look for
491  */
492 void
493 start_resolution(struct GNUNET_DNS_RequestHandle *rh,
494                  char* name, uint16_t id, uint16_t type)
495 {
496   struct GNUNET_GNS_PendingQuery *query;
497   
498   //FIXME remove .gnunet here from name
499   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
500   query = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
501   query->id = id;
502   query->original_name = name; //Full name of original query
503   query->name = name; // FIXME without tld
504   query->type = type;
505   query->request_handle = rh;
506
507   //Start resolution in our zone
508   resolve_name(query, zone_hash);
509 }
510
511 /**
512  * The DNS request handler
513  * Called for every incoming DNS request.
514  *
515  * @param cls closure
516  * @param rh request handle to user for reply
517  * @param request_length number of bytes in request
518  * @param request udp payload of the DNS request
519  */
520 void
521 handle_dns_request(void *cls,
522                    struct GNUNET_DNS_RequestHandle *rh,
523                    size_t request_length,
524                    const char *request)
525 {
526   struct GNUNET_DNSPARSER_Packet *p;
527   int i;
528   char *tldoffset;
529
530   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
531   p = GNUNET_DNSPARSER_parse (request, request_length);
532   
533   if (NULL == p)
534   {
535     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536                 "Received malformed DNS packet, leaving it untouched\n");
537     GNUNET_DNS_request_forward (rh);
538     return;
539   }
540   
541   /**
542    * Check tld and decide if we or
543    * legacy dns is responsible
544    *
545    * FIXME now in theory there could be more than 1 query in the request
546    * but if this is case we get into trouble:
547    * either we query the GNS or the DNS. We cannot do both!
548    * So I suggest to either only allow a single query per request or
549    * only allow GNS or DNS requests.
550    * The way it is implemented here now is buggy and will lead to erratic
551    * behaviour (if multiple queries are present).
552    */
553   for (i=0;i<p->num_queries;i++)
554   {
555     tldoffset = p->queries[i].name + strlen(p->queries[i].name);
556
557     while ((*tldoffset) != '.')
558       tldoffset--;
559     
560     /**
561      * FIXME Move our tld/root to config file
562      */
563     if (0 == strcmp(tldoffset, ".gnunet"))
564     {
565       start_resolution(rh, p->queries[i].name, p->id, p->queries[i].type);
566     }
567     else
568     {
569       /**
570        * This request does not concern us. Forward to real DNS.
571        */
572       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
573                  "Request for %s is forwarded to DNS\n", p->queries[i].name);
574       GNUNET_DNS_request_forward (rh);
575     }
576   }
577 }
578
579 /**
580  * test function that stores some data in the namestore
581  */
582 void
583 put_some_records(void)
584 {
585   /* put a few records into namestore */
586   char* ipA = "1.2.3.4";
587   char* ipB = "5.6.7.8";
588   struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
589   struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
590   GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
591   GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
592   GNUNET_NAMESTORE_record_put (namestore_handle,
593                                zone_hash,
594                                "alice",
595                                GNUNET_GNS_RECORD_TYPE_A,
596                                GNUNET_TIME_absolute_get_forever(),
597                                GNUNET_NAMESTORE_RF_AUTHORITY,
598                                NULL, //sig loc
599                                sizeof(struct in_addr),
600                                alice,
601                                NULL,
602                                NULL);
603   GNUNET_NAMESTORE_record_put (namestore_handle,
604                                zone_hash,
605                                "bob",
606                                GNUNET_GNS_RECORD_TYPE_A,
607                                GNUNET_TIME_absolute_get_forever(),
608                                GNUNET_NAMESTORE_RF_AUTHORITY,
609                                NULL, //sig loc
610                                sizeof(struct in_addr),
611                                bob,
612                                NULL,
613                                NULL);
614 }
615
616 //Prototype... needed in put function
617 static void
618 update_zone_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
619
620 /**
621  * Function used to put all records successively into the DHT.
622  * FIXME also serializes records. maybe do this somewhere else...
623  * FIXME don't store private records (maybe zone transfer does this)
624  *
625  * @param cls the closure (NULL)
626  * @param zone our root zone hash
627  * @param name the name of the record
628  * @param record_type the type of the record
629  * @param expiration lifetime of the record
630  * @param flags flags of the record
631  * @param sig_loc location of record in signature tree
632  * @param size size of the record
633  * @param record_data the record data
634  */
635 void
636 put_gns_record(void *cls, const GNUNET_HashCode *zone, const char *name,
637                uint32_t record_type, struct GNUNET_TIME_Absolute expiration,
638                enum GNUNET_NAMESTORE_RecordFlags flags,
639                const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
640                size_t size, const void *record_data)
641 {
642   struct GNUNET_TIME_Relative timeout;
643
644   char* data;
645   char* data_ptr;
646   struct GNUNET_TIME_AbsoluteNBO exp_nbo;
647   exp_nbo = GNUNET_TIME_absolute_hton (expiration);
648   uint32_t namelen = htonl(strlen(name));
649   uint16_t flags_nbo = htons(flags);
650   uint64_t offset = GNUNET_htonll(sig_loc->offset);
651   uint32_t depth = htonl(sig_loc->depth);
652   uint32_t revision = htonl(sig_loc->revision);
653   GNUNET_HashCode name_hash;
654   GNUNET_HashCode xor_hash;
655
656   /**
657    * I guess this can be done prettier
658    * FIXME extract into function, maybe even into different file
659    */
660   size_t record_len = sizeof(size_t) + sizeof(uint32_t) +
661     sizeof(uint16_t) +
662     sizeof(struct GNUNET_NAMESTORE_SignatureLocation) +
663     sizeof(uint32_t) + strlen(name) + size;
664   
665   record_type = htonl(record_type);
666
667   data = GNUNET_malloc(record_len);
668   
669   /* -_- */
670   data_ptr = data;
671   memcpy(data_ptr, &namelen, sizeof(size_t));
672   data_ptr += sizeof(size_t);
673
674   memcpy(data_ptr, name, namelen);
675   data_ptr += namelen;
676   
677   memcpy(data_ptr, &record_type, sizeof(uint32_t));
678   data_ptr += sizeof(uint32_t);
679
680   memcpy(data_ptr, &exp_nbo, sizeof(struct GNUNET_TIME_AbsoluteNBO));
681   data_ptr += sizeof(struct GNUNET_TIME_AbsoluteNBO);
682
683   memcpy(data_ptr, &flags_nbo, sizeof(uint16_t));
684   data_ptr += sizeof(uint16_t);
685
686   memcpy(data_ptr, &offset, sizeof(uint64_t));
687   data_ptr += sizeof(uint64_t);
688
689   memcpy(data_ptr, &depth, sizeof(uint32_t));
690   data_ptr += sizeof(uint32_t);
691   
692   memcpy(data_ptr, &revision, sizeof(uint32_t));
693   data_ptr += sizeof(uint32_t);
694
695   memcpy(data_ptr, &size, sizeof(uint32_t));
696   data_ptr += sizeof(uint32_t);
697
698   /**
699    * FIXME note that this only works with raw data in nbo
700    * write helper function that converts properly and returns buffer
701    */
702   memcpy(data_ptr, record_data, size);
703   data_ptr += size;
704   /*Doing this made me sad...*/
705
706   /**
707    * FIXME magic number 20 move to config file
708    */
709   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
710
711   GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
712   GNUNET_CRYPTO_hash_xor(zone_hash, &name_hash, &xor_hash);
713   GNUNET_DHT_put (dht_handle, &xor_hash,
714                   5, //replication level
715                   GNUNET_DHT_RO_NONE,
716                   GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
717                   (data_ptr-data),
718                   data,
719                   expiration, //FIXME from record makes sense? is absolute?
720                   timeout,
721                   NULL, //FIXME continuation needed? success check? yes ofc
722                   NULL); //cls for cont
723
724   /**
725    * Reschedule periodic put
726    */
727   GNUNET_SCHEDULER_add_delayed (dht_update_interval,
728                                 &update_zone_dht,
729                                 NULL);
730
731 }
732
733 /**
734  * Periodically iterate over our zone and store everything in dht
735  *
736  * @param cls NULL
737  * @param tc task context
738  */
739 static void
740 update_zone_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
741 {
742   GNUNET_NAMESTORE_zone_transfer (namestore_handle, zone_hash,
743                                   &put_gns_record,
744                                   NULL);
745 }
746
747 /**
748  * Process GNS requests.
749  *
750  * @param cls closure
751  * @param server the initialized server
752  * @param c configuration to use
753  */
754 static void
755 run (void *cls, struct GNUNET_SERVER_Handle *server,
756      const struct GNUNET_CONFIGURATION_Handle *c)
757 {
758   
759   zone_key = GNUNET_CRYPTO_rsa_key_create ();
760   GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
761                      zone_hash);
762
763   nc = GNUNET_SERVER_notification_context_create (server, 1);
764
765   /* FIXME - do some config parsing 
766    *       - Maybe only hijack dns if option is set (HIJACK_DNS=1)
767    */
768
769   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
770                                 NULL);
771   /**
772    * Do gnunet dns init here
773    */
774   dns_handle = GNUNET_DNS_connect(c,
775                                   GNUNET_DNS_FLAG_PRE_RESOLUTION,
776                                   &handle_dns_request, /* rh */
777                                   NULL); /* Closure */
778
779   if (NULL == dns_handle)
780   {
781     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
782                "Failed to connect to the dnsservice!\n");
783   }
784
785   /**
786    * handle to our local namestore
787    */
788   namestore_handle = GNUNET_NAMESTORE_connect(c);
789
790   if (NULL == namestore_handle)
791   {
792     //FIXME do error handling;
793     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
794                "Failed to connect to the namestore!\n");
795   }
796
797   /**
798    * handle to the dht
799    */
800   dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
801
802   if (NULL == dht_handle)
803   {
804     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
805   }
806
807   put_some_records(); //FIXME for testing
808   
809   /**
810    * Schedule periodic put
811    * for our records
812    */
813   dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
814                                                       60); //FIXME from cfg
815   GNUNET_SCHEDULER_add_delayed (dht_update_interval,
816                                 &update_zone_dht,
817                                 NULL);
818
819 }
820
821
822 /**
823  * The main function for the GNS service.
824  *
825  * @param argc number of arguments from the command line
826  * @param argv command line arguments
827  * @return 0 ok, 1 on error
828  */
829 int
830 main (int argc, char *const *argv)
831 {
832   int ret;
833
834   ret =
835       (GNUNET_OK ==
836        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
837                            NULL)) ? 0 : 1;
838   return ret;
839 }
840
841 /* end of gnunet-service-gns.c */