-licenses, dce, etc
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.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  * @file gns/gnunet-service-gns_resolver.c
23  * @brief GNUnet GNS resolver logic
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gns_protocol.h"
37 #include "gnunet_gns_service.h"
38 #include "gns_common.h"
39 #include "block_gns.h"
40 #include "gns.h"
41 #include "gnunet-service-gns_resolver.h"
42
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
46
47
48 /**
49  * Our handle to the namestore service
50  */
51 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
52
53 /**
54  * Our handle to the vpn service
55  */
56 static struct GNUNET_VPN_Handle *vpn_handle;
57
58 /**
59  * Resolver handle to the dht
60  */
61 static struct GNUNET_DHT_Handle *dht_handle;
62
63 /**
64  * Heap for parallel DHT lookups
65  */
66 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
67
68 /**
69  * Heap for namestore queues
70  */
71 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
72
73 /**
74  * Maximum amount of parallel queries in background
75  */
76 static unsigned long long max_allowed_background_queries;
77
78 /**
79  * Maximum amount of parallel namestore tasks in background
80  */
81 static unsigned long long max_allowed_ns_tasks;
82
83 /**
84  * Wheather or not to ignore pending records
85  */
86 static int ignore_pending_records;
87
88 /**
89  * Our local zone
90  */
91 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
92
93 /**
94  * Background shortening handles
95  */
96 static struct GetPseuAuthorityHandle *gph_head;
97
98 /**
99  * Background shortening handles
100  */
101 static struct GetPseuAuthorityHandle *gph_tail;
102
103 /**
104  * Resolver lookup list
105  */
106 static struct ResolverHandle *rlh_head;
107
108 /**
109  * Resolver lookup list
110  */
111 static struct ResolverHandle *rlh_tail;
112
113 /**
114  * Resolver shorten list
115  */
116 static struct ResolverHandle *nsh_head;
117
118 /**
119  * Resolver shorten list
120  */
121 static struct ResolverHandle *nsh_tail;
122
123 /**
124  * Resolver get auth list
125  */
126 static struct ResolverHandle *nah_head;
127
128 /**
129  * Resolver get auth list
130  */
131 static struct ResolverHandle *nah_tail;
132
133 /**
134  * Global configuration.
135  */
136 static const struct GNUNET_CONFIGURATION_Handle *cfg;
137
138 /**
139  * a resolution identifier pool variable
140  * FIXME overflow?
141  * This is a non critical identifier useful for debugging
142  */
143 static unsigned long long rid = 0;
144
145 /*
146  * Check if name is in srv format (_x._y.xxx)
147  *
148  * @param name
149  * @return GNUNET_YES if true
150  */
151 static int
152 is_srv (char* name)
153 {
154   char* ndup;
155   int ret = GNUNET_YES;
156
157   if (*name != '_')
158     return GNUNET_NO;
159   if (NULL == strstr (name, "._"))
160     return GNUNET_NO;
161
162   ndup = GNUNET_strdup (name);
163   strtok (ndup, ".");
164
165   if (NULL == strtok (NULL, "."))
166     ret = GNUNET_NO;
167
168   if (NULL == strtok (NULL, "."))
169     ret = GNUNET_NO;
170
171   if (NULL != strtok (NULL, "."))
172     ret = GNUNET_NO;
173
174   GNUNET_free (ndup);
175
176   return ret;
177 }
178
179 /**
180  * Determine if this name is canonical (is a legal name in a zone, without delegation);
181  * note that we do not test that the name does not contain illegal characters, we only
182  * test for delegation.  Note that service records (i.e. _foo._srv) are canonical names
183  * even though they consist of multiple labels.
184  *
185  * Examples:
186  * a.b.gads  = not canonical
187  * a         = canonical
188  * _foo._srv = canonical
189  * _f.bar    = not canonical
190  *
191  * @param name the name to test
192  * @return GNUNET_YES if canonical
193  */
194 static int
195 is_canonical (const char* name)
196 {
197   const char *pos;
198   const char *dot;
199
200   if (NULL == strchr (name, '.'))
201     return GNUNET_YES;
202   if ('_' != name[0])
203     return GNUNET_NO;
204   pos = &name[1];
205   while (NULL != (dot = strchr (pos, '.')))    
206     if ('_' != dot[1])
207       return GNUNET_NO;
208     else
209       pos = dot + 1;
210   return GNUNET_YES;
211 }
212
213
214 static void
215 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
216 {
217   gph->namestore_task = NULL;
218   GNUNET_free (gph->auth);
219   GNUNET_CRYPTO_rsa_key_free (gph->key);
220   GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
221   GNUNET_free (gph);
222 }
223
224
225 /**
226  * Callback that shortens authorities
227  *
228  * @param gph the handle containing the name to shorten
229  */
230 static void
231 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
232
233
234 /**
235  * Continueation for pkey record creation (shorten)
236  *
237  * @param cls a GetPseuAuthorityHandle
238  * @param success unused
239  * @param emsg unused
240  */
241 static void
242 create_pkey_cont (void* cls, int32_t success, const char* emsg)
243 {
244   //FIXME do sth with error
245   struct GetPseuAuthorityHandle* gph = cls;
246
247   free_get_pseu_authority_handle (gph);
248 }
249
250
251 /**
252  * Namestore calls this function if we have record for this name.
253  * (or with rd_count=0 to indicate no matches)
254  *
255  * @param cls the pending query
256  * @param key the key of the zone we did the lookup
257  * @param expiration expiration date of the namestore entry
258  * @param name the name for which we need an authority
259  * @param rd_count the number of records with 'name'
260  * @param rd the record data
261  * @param signature the signature of the authority for the record data
262  */
263 static void
264 process_pseu_lookup_ns (void* cls,
265                         const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
266                         struct GNUNET_TIME_Absolute expiration,
267                         const char *name, unsigned int rd_count,
268                         const struct GNUNET_NAMESTORE_RecordData *rd,
269                         const struct GNUNET_CRYPTO_RsaSignature *signature)
270 {
271   struct GetPseuAuthorityHandle* gph = cls;
272   struct GNUNET_NAMESTORE_RecordData new_pkey;
273
274   gph->namestore_task = NULL;
275   if (rd_count > 0)
276   {
277     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
278                "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
279     free_get_pseu_authority_handle (gph);
280     return;
281   }
282
283   /* name is free */
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285             "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
286
287   new_pkey.expiration_time = UINT64_MAX;
288   new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
289   new_pkey.data = &gph->auth->zone;
290   new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
291   new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
292                  | GNUNET_NAMESTORE_RF_PRIVATE
293                  | GNUNET_NAMESTORE_RF_PENDING;
294   gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
295                                                         gph->key,
296                                                         gph->test_name,
297                                                         &new_pkey,
298                                                         &create_pkey_cont, 
299                                                         gph);
300 }
301
302 /**
303  * process result of a dht pseu lookup
304  *
305  * @param gph the handle
306  * @param name the pseu result or NULL
307  */
308 static void
309 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
310 {
311   if (NULL == name)
312   {
313     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314                 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
315     free_get_pseu_authority_handle (gph);
316     return;
317   }
318   
319   memcpy (gph->test_name, name, strlen(name) + 1);
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321               "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
322               gph->test_name);
323   /**
324    * Check for collision
325    */
326   gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
327                                                         &gph->our_zone,
328                                                         gph->test_name,
329                                                         GNUNET_NAMESTORE_TYPE_ANY,
330                                                         &process_pseu_lookup_ns,
331                                                         gph);
332 }
333
334
335 /**
336  * Handle timeout for dht request
337  *
338  * @param cls the request handle as closure
339  * @param tc the task context
340  */
341 static void
342 handle_auth_discovery_timeout (void *cls,
343                                const struct GNUNET_SCHEDULER_TaskContext *tc)
344 {
345   struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
346
347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348               "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
349   GNUNET_DHT_get_stop (gph->get_handle);
350   gph->get_handle = NULL;
351   process_pseu_result (gph, NULL);
352 }
353
354 /**
355  * Function called when we find a PSEU entry in the DHT
356  *
357  * @param cls the request handle
358  * @param exp lifetime
359  * @param key the key the record was stored under
360  * @param get_path get path
361  * @param get_path_length get path length
362  * @param put_path put path
363  * @param put_path_length put path length
364  * @param type the block type
365  * @param size the size of the record
366  * @param data the record data
367  */
368 static void
369 process_auth_discovery_dht_result (void* cls,
370                                    struct GNUNET_TIME_Absolute exp,
371                                    const struct GNUNET_HashCode * key,
372                                    const struct GNUNET_PeerIdentity *get_path,
373                                    unsigned int get_path_length,
374                                    const struct GNUNET_PeerIdentity *put_path,
375                                    unsigned int put_path_length,
376                                    enum GNUNET_BLOCK_Type type,
377                                    size_t size,
378                                    const void *data)
379 {
380   struct GetPseuAuthorityHandle* gph = cls;
381   struct GNSNameRecordBlock *nrb;
382   const char* rd_data = data;
383   char* name;
384   int num_records;
385   size_t rd_size;
386   int i;
387
388   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389               "GNS_GET_AUTH: got dht result (size=%d)\n", size);
390
391   /* stop lookup and timeout task */
392   GNUNET_DHT_get_stop (gph->get_handle);
393   gph->get_handle = NULL;
394   GNUNET_SCHEDULER_cancel (gph->timeout);
395   
396   if (NULL == data)
397   {
398     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399                 "GNS_GET_AUTH: got dht result null!\n", size);
400     free_get_pseu_authority_handle (gph);
401     return;
402   }
403   
404   nrb = (struct GNSNameRecordBlock*)data;
405   name = (char*)&nrb[1];
406   num_records = ntohl (nrb->rd_count);
407   {
408     struct GNUNET_NAMESTORE_RecordData rd[num_records];
409
410     rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
411     rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
412
413     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
414                                                                rd_data,
415                                                                num_records,
416                                                                rd))
417     {
418       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419                   "GNS_GET_AUTH: Error deserializing data!\n");
420     }
421     else
422     {
423       for (i=0; i < num_records; i++)
424       {
425         if ((strcmp (name, "+") == 0) &&
426             (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
427         {
428           /* found pseu */
429           process_pseu_result (gph, (char*)rd[i].data);
430           return;
431         }
432       }
433     }
434   }
435   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436               "GNS_GET_AUTH: finished shorten, no results!\n");
437   process_pseu_result (gph, NULL);
438 }
439
440 /**
441  * Process PSEU discovery for shorten via namestore
442  *
443  * @param cls the GetPseuAuthorityHandle
444  * @param key the public key
445  * @param expiration recorddata expiration
446  * @param name the looked up name
447  * @param rd_count number of records in set
448  * @param rd record data
449  * @param signature the signature
450  */
451 static void
452 process_auth_discovery_ns_result (void* cls,
453                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
454                       struct GNUNET_TIME_Absolute expiration,
455                       const char *name,
456                       unsigned int rd_count,
457                       const struct GNUNET_NAMESTORE_RecordData *rd,
458                       const struct GNUNET_CRYPTO_RsaSignature *signature)
459 {
460   struct GetPseuAuthorityHandle* gph = cls;
461   struct GNUNET_HashCode lookup_key;
462   unsigned int i;
463   uint32_t xquery;
464   
465   gph->namestore_task = NULL;
466   /* no pseu found */
467   if (0 == rd_count)
468   {
469     GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
470     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471                 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
472                 GNUNET_GNS_TLD_PLUS, 
473                 GNUNET_h2s (&lookup_key));
474
475     gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
476                                          &handle_auth_discovery_timeout, gph);
477
478     xquery = htonl (GNUNET_GNS_RECORD_PSEU);
479     
480     GNUNET_assert (gph->get_handle == NULL);
481
482     gph->get_handle = GNUNET_DHT_get_start (dht_handle,
483                                            GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
484                                            &lookup_key,
485                                            DHT_GNS_REPLICATION_LEVEL,
486                                            GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
487                                            &xquery,
488                                            sizeof(xquery),
489                                            &process_auth_discovery_dht_result,
490                                            gph);
491     return;
492   }
493
494   for (i=0; i < rd_count; i++)
495   {
496     if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
497       continue;
498
499     if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
500       continue;
501
502     /* found pseu */
503     process_pseu_result (gph, (char*)rd[i].data);
504     return;
505   }
506
507   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
508   process_pseu_result (gph, NULL);
509 }
510
511 /**
512  * Callback called by namestore for a zone to name
513  * result
514  *
515  * @param cls the closure
516  * @param zone_key the zone we queried
517  * @param expire the expiration time of the name
518  * @param name the name found or NULL
519  * @param rd_len number of records for the name
520  * @param rd the record data (PKEY) for the name
521  * @param signature the signature for the record data
522  */
523 static void
524 process_zone_to_name_discover (void *cls,
525                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
526                  struct GNUNET_TIME_Absolute expire,
527                  const char *name,
528                  unsigned int rd_len,
529                  const struct GNUNET_NAMESTORE_RecordData *rd,
530                  const struct GNUNET_CRYPTO_RsaSignature *signature)
531 {
532   struct GetPseuAuthorityHandle* gph = cls;
533   
534   gph->namestore_task = NULL;
535   if (0 == rd_len)
536   {
537     gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
538                                     &gph->auth->zone,
539                                     "+",
540                                     GNUNET_GNS_RECORD_PSEU,
541                                     &process_auth_discovery_ns_result,
542                                     gph);
543     return;
544   }
545   /* we found a match in our own zone */
546   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547               "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
548   free_get_pseu_authority_handle (gph);
549 }
550
551
552 /**
553  * Callback that shortens authorities
554  *
555  * @param gph the handle to the shorten request
556  */
557 static void
558 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
559 {
560
561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562               "GNS_AUTO_PSEU: New authority %s discovered\n",
563               gph->auth->name);
564
565   gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
566                                  &gph->our_zone,
567                                  &gph->auth->zone,
568                                  &process_zone_to_name_discover,
569                                  gph);
570
571 }
572
573
574 static void
575 start_shorten (struct AuthorityChain *auth,
576                const struct GNUNET_CRYPTO_RsaPrivateKey *key)
577 {
578   struct GetPseuAuthorityHandle *gph;
579   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
580   struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
581   
582   GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
583   if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
584   {
585     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
586                 "Failed to encode RSA key on shorten\n");
587     return;
588   }
589   gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
590   gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
591   GNUNET_free (pb_key);
592   if (NULL == gph->key)
593   {
594     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
595                 "Failed to decode RSA key on shorten\n");
596     GNUNET_free (gph);
597     return;
598   }
599   GNUNET_CRYPTO_short_hash (&pkey,
600                         sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
601                         &gph->our_zone);
602   gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
603   memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
604   GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
605   shorten_authority_chain (gph);
606 }
607
608 /**
609  * Initialize the resolver
610  *
611  * @param nh the namestore handle
612  * @param dh the dht handle
613  * @param lz the local zone's hash
614  * @param c configuration handle
615  * @param max_bg_queries maximum number of parallel background queries in dht
616  * @param ignore_pending ignore records that still require user confirmation
617  *        on lookup
618  * @return GNUNET_OK on success
619  */
620 int
621 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
622                    struct GNUNET_DHT_Handle *dh,
623                    struct GNUNET_CRYPTO_ShortHashCode lz,
624                    const struct GNUNET_CONFIGURATION_Handle *c,
625                    unsigned long long max_bg_queries,
626                    int ignore_pending)
627 {
628   if ( (NULL == nh) ||
629        (NULL == dh) )
630     return GNUNET_SYSERR;
631   
632   cfg = c;
633   namestore_handle = nh;
634   dht_handle = dh;
635   local_zone = lz;
636   dht_lookup_heap =
637     GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
638   ns_task_heap =
639     GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
640   max_allowed_background_queries = max_bg_queries;
641   max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
642   ignore_pending_records = ignore_pending; 
643   GNUNET_RESOLVER_connect (cfg);
644   return GNUNET_OK;
645 }
646
647
648 /**
649  * Cleanup ns tasks
650  *
651  * @param cls closure to iterator
652  * @param node heap nodes
653  * @param element the namestorebgtask
654  * @param cost heap cost
655  * @return always GNUNET_YES
656  */
657 static int
658 cleanup_pending_ns_tasks (void* cls,
659                           struct GNUNET_CONTAINER_HeapNode *node,
660                           void *element,
661                           GNUNET_CONTAINER_HeapCostType cost)
662 {
663   struct NamestoreBGTask *nbg = element;
664
665   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
666              "GNS_CLEANUP: Terminating ns task\n");
667   GNUNET_NAMESTORE_cancel (nbg->qe);
668
669   GNUNET_CONTAINER_heap_remove_node (node);
670   return GNUNET_YES;
671 }
672
673
674 /**
675  * finish lookup
676  *
677  * @param rh resolver handle
678  * @param rlh record lookup handle
679  * @param rd_count number of results
680  * @param rd results
681  */
682 static void
683 finish_lookup (struct ResolverHandle *rh,
684                struct RecordLookupHandle* rlh,
685                unsigned int rd_count,
686                const struct GNUNET_NAMESTORE_RecordData *rd);
687
688
689 /**
690  * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
691  * anything! => find in code and remove all references to the heap
692  *
693  * @param cls closure to iterator
694  * @param node heap nodes
695  * @param element the resolver handle
696  * @param cost heap cost
697  * @return always GNUNET_YES
698  */
699 static int
700 cleanup_pending_background_queries (void* cls,
701                                     struct GNUNET_CONTAINER_HeapNode *node,
702                                     void *element,
703                                     GNUNET_CONTAINER_HeapCostType cost)
704 {
705   struct ResolverHandle *rh = element;
706
707   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
708              "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
709              rh->id, rh->name);
710   GNUNET_CONTAINER_heap_remove_node (node);
711   if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
712   {
713     GNUNET_CONTAINER_heap_iterate (ns_task_heap,
714                                    &cleanup_pending_ns_tasks,
715                                    NULL);    
716   }
717   return GNUNET_YES;
718 }
719
720
721 /**
722  * Helper function to free resolver handle
723  *
724  * @param rh the handle to free
725  */
726 static void
727 free_resolver_handle (struct ResolverHandle* rh)
728 {
729   struct AuthorityChain *ac;
730   struct AuthorityChain *ac_next;
731
732   if (NULL == rh)
733     return;
734
735   ac = rh->authority_chain_head;
736
737   while (NULL != ac)
738   {
739     ac_next = ac->next;
740     GNUNET_free (ac);
741     ac = ac_next;
742   }
743   
744   if (NULL != rh->get_handle)
745     GNUNET_DHT_get_stop (rh->get_handle);
746
747   if (NULL != rh->dns_raw_packet)
748     GNUNET_free (rh->dns_raw_packet);
749
750   if (NULL != rh->namestore_task)
751     GNUNET_NAMESTORE_cancel (rh->namestore_task);
752   rh->namestore_task = NULL;
753
754   if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
755     GNUNET_SCHEDULER_cancel (rh->dns_read_task);
756
757   if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
758     GNUNET_SCHEDULER_cancel (rh->timeout_task);
759
760   if (NULL != rh->dns_sock)
761     GNUNET_NETWORK_socket_close (rh->dns_sock);
762   if (NULL != rh->dns_resolver_handle)
763     GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
764
765   if (NULL != rh->rd.data)
766     GNUNET_free ((void*)(rh->rd.data));
767   GNUNET_free (rh);
768 }
769
770
771 /**
772  * finish shorten
773  *
774  * @param rh resolver handle
775  * @param nsh name shorten handle
776  */
777 static void
778 finish_shorten (struct ResolverHandle *rh,
779                 struct NameShortenHandle *nsh);
780 /**
781  * finish get auth
782  *
783  * @param rh resolver handle
784  * @param nah get name authority handle
785  */
786 static void
787 finish_get_auth (struct ResolverHandle *rh,
788                  struct GetNameAuthorityHandle* rlh);
789
790
791 /**
792  * Shutdown resolver
793  */
794 void
795 gns_resolver_cleanup ()
796 {
797   unsigned int s;
798   struct GetPseuAuthorityHandle *tmp;
799
800   while (NULL != (tmp = gph_head))
801   {
802     if (tmp->get_handle != NULL)
803     {
804       GNUNET_DHT_get_stop (tmp->get_handle);
805       tmp->get_handle = NULL;
806     }
807     if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
808     {
809       GNUNET_SCHEDULER_cancel (tmp->timeout);
810       tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
811     }
812     if (NULL != tmp->namestore_task)
813     {
814       GNUNET_NAMESTORE_cancel (tmp->namestore_task);
815       tmp->namestore_task = NULL;
816     }
817     free_get_pseu_authority_handle (tmp);
818   }
819
820   while (NULL != rlh_head)
821   {
822     finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
823   }
824   while (NULL != nsh_head)
825   {
826     finish_shorten (nsh_head, nsh_head->proc_cls);
827   }
828   while (NULL != nah_head)
829   {
830     finish_get_auth (nah_head, nah_head->proc_cls);
831   }
832
833   s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
834   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
835              "GNS_CLEANUP: %d pending background queries to terminate\n", s);
836   if (0 != s)
837     GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
838                                    &cleanup_pending_background_queries,
839                                    NULL);
840   else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
841   {
842     GNUNET_CONTAINER_heap_iterate (ns_task_heap,
843                                    &cleanup_pending_ns_tasks,
844                                    NULL);
845   }
846 }
847
848
849
850
851 /**
852  * Callback when record data is put into namestore
853  *
854  * @param cls the closure
855  * @param success GNUNET_OK on success
856  * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
857  */
858 void
859 on_namestore_record_put_result (void *cls,
860                                 int32_t success,
861                                 const char *emsg)
862 {
863   struct NamestoreBGTask *nbg = cls;
864
865   GNUNET_CONTAINER_heap_remove_node (nbg->node);
866   GNUNET_free (nbg);
867
868   if (GNUNET_NO == success)
869   {
870     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
871                "GNS_NS: records already in namestore\n");
872     return;
873   }
874   else if (GNUNET_YES == success)
875   {
876     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
877                "GNS_NS: records successfully put in namestore\n");
878     return;
879   }
880
881   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
882              "GNS_NS: Error putting records into namestore: %s\n", emsg);
883 }
884
885
886 static void
887 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
888 {
889   struct ResolverHandle *rh = cls;
890
891   if (NULL != rh->timeout_cont)
892     rh->timeout_cont (rh->timeout_cont_cls, tc);
893   /* FIXME: does this leak memory? */
894 }
895
896
897 /**
898  * Processor for background lookups in the DHT
899  *
900  * @param cls closure (NULL)
901  * @param rd_count number of records found (not 0)
902  * @param rd record data
903  */
904 static void
905 background_lookup_result_processor (void *cls,
906                                    uint32_t rd_count,
907                                    const struct GNUNET_NAMESTORE_RecordData *rd)
908 {
909   //We could do sth verbose/more useful here but it doesn't make any difference
910   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911               "GNS_BG: background dht lookup finished. (%d results)\n",
912               rd_count);
913 }
914
915 /**
916  * Handle timeout for DHT requests
917  *
918  * @param cls the request handle as closure
919  * @param tc the task context
920  */
921 static void
922 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
923 {
924   struct ResolverHandle *rh = cls;
925   struct RecordLookupHandle *rlh = rh->proc_cls;
926   char new_name[MAX_DNS_NAME_LENGTH];
927
928   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929               "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
930               rh->id, rh->name, rh->timeout.rel_value);
931   /**
932    * Start resolution in bg
933    */
934   GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
935                    rh->name, GNUNET_GNS_TLD);
936
937   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938               "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
939               rh->id, new_name, rlh->record_type);
940   
941   gns_resolver_lookup_record (rh->authority,
942                               rh->private_local_zone,
943                               rlh->record_type,
944                               new_name,
945                               NULL,
946                               GNUNET_TIME_UNIT_FOREVER_REL,
947                               GNUNET_NO,
948                               &background_lookup_result_processor,
949                               NULL);
950                               
951   rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
952
953   GNUNET_DHT_get_stop (rh->get_handle);
954   rh->get_handle = NULL;
955   rh->proc (rh->proc_cls, rh, 0, NULL);
956 }
957
958
959 /**
960  * Function called when we get a result from the dht
961  * for our record query
962  *
963  * @param cls the request handle
964  * @param exp lifetime
965  * @param key the key the record was stored under
966  * @param get_path get path
967  * @param get_path_length get path length
968  * @param put_path put path
969  * @param put_path_length put path length
970  * @param type the block type
971  * @param size the size of the record
972  * @param data the record data
973  */
974 static void
975 process_record_result_dht (void* cls,
976                            struct GNUNET_TIME_Absolute exp,
977                            const struct GNUNET_HashCode * key,
978                            const struct GNUNET_PeerIdentity *get_path,
979                            unsigned int get_path_length,
980                            const struct GNUNET_PeerIdentity *put_path,
981                            unsigned int put_path_length,
982                            enum GNUNET_BLOCK_Type type,
983                            size_t size, const void *data)
984 {
985   struct ResolverHandle *rh;
986   struct RecordLookupHandle *rlh;
987   struct GNSNameRecordBlock *nrb;
988   uint32_t num_records;
989   char* name = NULL;
990   char* rd_data = (char*)data;
991   int i;
992   int rd_size;
993
994   rh = (struct ResolverHandle *)cls;
995   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996               "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
997
998   rlh = (struct RecordLookupHandle *) rh->proc_cls;
999   nrb = (struct GNSNameRecordBlock*)data;
1000
1001   /* stop lookup and timeout task */
1002   GNUNET_DHT_get_stop (rh->get_handle);
1003   rh->get_handle = NULL;
1004
1005   if (rh->dht_heap_node != NULL)
1006   {
1007     GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1008     rh->dht_heap_node = NULL;
1009   }
1010
1011   if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1012   {
1013     GNUNET_SCHEDULER_cancel (rh->timeout_task);
1014     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1015   }
1016
1017   rh->get_handle = NULL;
1018   name = (char*)&nrb[1];
1019   num_records = ntohl (nrb->rd_count);
1020   {
1021     struct GNUNET_NAMESTORE_RecordData rd[num_records];
1022     struct NamestoreBGTask *ns_heap_root;
1023     struct NamestoreBGTask *namestore_bg_task;
1024
1025     rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1026     rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1027
1028     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1029                                                                rd_data,
1030                                                                num_records,
1031                                                                rd))
1032     {
1033       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1034                   "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1035       rh->proc (rh->proc_cls, rh, 0, NULL);
1036       return;
1037     }
1038
1039     for (i=0; i<num_records; i++)
1040     {
1041       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042                   "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1043                   rh->id, name, rh->name);
1044       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045                   "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1046                   rh->id, rd[i].record_type, rlh->record_type);
1047       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048                   "GNS_PHASE_REC-%llu: Got data length: %d\n",
1049                   rh->id, rd[i].data_size);
1050       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051                   "GNS_PHASE_REC-%llu: Got flag %d\n",
1052                   rh->id, rd[i].flags);
1053
1054       if ((strcmp (name, rh->name) == 0) &&
1055           (rd[i].record_type == rlh->record_type))
1056       {
1057         rh->answered++;
1058       }
1059
1060     }
1061
1062     /**
1063      * FIXME check pubkey against existing key in namestore?
1064      * https://gnunet.org/bugs/view.php?id=2179
1065      */
1066     if (max_allowed_ns_tasks <=
1067         GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1068     {
1069       ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1070       GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1071
1072       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073                   "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1074                   rh->id);
1075     }
1076     
1077     /* Save to namestore */
1078     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079                 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1080                 rh->id, name);
1081     namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1082     namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1083                                  &nrb->public_key,
1084                                  name,
1085                                  exp,
1086                                  num_records,
1087                                  rd,
1088                                  &nrb->signature,
1089                                  &on_namestore_record_put_result, //cont
1090                                  namestore_bg_task);
1091
1092     namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1093                                   namestore_bg_task,
1094                                   GNUNET_TIME_absolute_get().abs_value);
1095
1096   
1097     if (0 < rh->answered)
1098      rh->proc (rh->proc_cls, rh, num_records, rd);
1099    else
1100      rh->proc (rh->proc_cls, rh, 0, NULL);
1101   }
1102
1103 }
1104
1105
1106 /**
1107  * Start DHT lookup for a (name -> query->record_type) record in
1108  * rh->authority's zone
1109  *
1110  * @param rh the pending gns query context
1111  */
1112 static void
1113 resolve_record_dht (struct ResolverHandle *rh)
1114 {
1115   uint32_t xquery;
1116   struct GNUNET_HashCode lookup_key;
1117   struct RecordLookupHandle *rlh = rh->proc_cls;
1118   struct ResolverHandle *rh_heap_root;
1119
1120   GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122               "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1123               rh->id, rh->name, GNUNET_h2s (&lookup_key));
1124
1125   //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1126   rh->dht_heap_node = NULL;
1127
1128   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1129   {
1130     /**
1131      * Update timeout if necessary
1132      */
1133     if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1134     {
1135       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136                   "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1137       /*
1138        * Set timeout for authority lookup phase to 1/2
1139        */
1140       rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1141                                    GNUNET_TIME_relative_divide (rh->timeout, 2),
1142                                    &handle_lookup_timeout,
1143                                    rh);
1144     }
1145     rh->timeout_cont = &dht_lookup_timeout;
1146     rh->timeout_cont_cls = rh;
1147   }
1148   else 
1149   {
1150     if (max_allowed_background_queries <=
1151         GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1152     {
1153       rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1154       GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1155       rh_heap_root->get_handle = NULL;
1156       rh_heap_root->dht_heap_node = NULL;
1157
1158       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159                  "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1160                  rh->id, rh_heap_root->name);
1161       rh_heap_root->proc (rh_heap_root->proc_cls,
1162                           rh_heap_root,
1163                           0,
1164                           NULL);
1165     }
1166     rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1167                                          rh,
1168                                          GNUNET_TIME_absolute_get ().abs_value);
1169   }
1170
1171   xquery = htonl (rlh->record_type);
1172
1173   GNUNET_assert (rh->get_handle == NULL);
1174   rh->get_handle = GNUNET_DHT_get_start (dht_handle, 
1175                                          GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1176                                          &lookup_key,
1177                                          DHT_GNS_REPLICATION_LEVEL,
1178                                          GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1179                                          &xquery,
1180                                          sizeof (xquery),
1181                                          &process_record_result_dht,
1182                                          rh);
1183
1184 }
1185
1186
1187 /**
1188  * Namestore calls this function if we have record for this name.
1189  * (or with rd_count=0 to indicate no matches)
1190  *
1191  * @param cls the pending query
1192  * @param key the key of the zone we did the lookup
1193  * @param expiration expiration date of the namestore entry
1194  * @param name the name for which we need an authority
1195  * @param rd_count the number of records with 'name'
1196  * @param rd the record data
1197  * @param signature the signature of the authority for the record data
1198  */
1199 static void
1200 process_record_result_ns (void* cls,
1201                           const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1202                           struct GNUNET_TIME_Absolute expiration,
1203                           const char *name, unsigned int rd_count,
1204                           const struct GNUNET_NAMESTORE_RecordData *rd,
1205                           const struct GNUNET_CRYPTO_RsaSignature *signature)
1206 {
1207   struct ResolverHandle *rh;
1208   struct RecordLookupHandle *rlh;
1209   struct GNUNET_TIME_Relative remaining_time;
1210   struct GNUNET_CRYPTO_ShortHashCode zone;
1211   struct GNUNET_TIME_Absolute et;
1212   unsigned int i;
1213
1214   rh = cls;
1215   rlh = rh->proc_cls;
1216
1217   rh->namestore_task = NULL;
1218   GNUNET_CRYPTO_short_hash (key,
1219                         sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1220                         &zone);
1221   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1222
1223
1224
1225   rh->status = 0;
1226
1227   if (NULL != name)
1228   {
1229     rh->status |= RSL_RECORD_EXISTS;
1230
1231     if (remaining_time.rel_value == 0)
1232     {
1233       rh->status |= RSL_RECORD_EXPIRED;
1234     }
1235   }
1236
1237   if (rd_count == 0)
1238   {
1239     /**
1240      * Lookup terminated and no results
1241      */
1242     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1243                "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1244                rh->id, name);
1245
1246     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1247                "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1248                rh->id, rh->name);
1249     /**
1250      * Our zone and no result? Cannot resolve TT
1251      */
1252     rh->proc(rh->proc_cls, rh, 0, NULL);
1253     return;
1254
1255   }
1256   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1257              "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1258              rh->id, name);
1259   for (i=0; i<rd_count;i++)
1260   {
1261     if (rd[i].record_type != rlh->record_type)
1262       continue;
1263
1264     if (ignore_pending_records &&
1265         (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1266     {
1267       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1268                  "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1269                  rh->id, name);
1270       continue;
1271     }
1272     
1273     //FIXME: eh? do I have to handle this here?
1274     GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1275     et.abs_value = rd[i].expiration_time;
1276     if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1277     {
1278       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1279                  "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1280                  rh->id);
1281       continue;
1282     }
1283     rh->answered++;
1284   }
1285
1286   /**
1287    * no answers found
1288    */
1289   if (0 == rh->answered)
1290   {
1291     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
1292                "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1293     rh->proc(rh->proc_cls, rh, 0, NULL);
1294     return;
1295   }
1296
1297   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1298              "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1299              rh->id, rh->answered, rd_count);
1300   rh->proc(rh->proc_cls, rh, rd_count, rd);
1301 }
1302
1303
1304 /**
1305  * VPN redirect result callback
1306  *
1307  * @param cls the resolver handle
1308  * @param af the requested address family
1309  * @param address in_addr(6) respectively
1310  */
1311 static void
1312 process_record_result_vpn (void* cls, int af, const void *address)
1313 {
1314   struct ResolverHandle *rh = cls;
1315   struct RecordLookupHandle *rlh;
1316   struct GNUNET_NAMESTORE_RecordData rd;
1317
1318   rlh = rh->proc_cls;
1319
1320   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1321              "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1322              rh->id);
1323   if (AF_INET == af)
1324   {
1325     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1326                "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1327                rh->id);
1328     if (GNUNET_GNS_RECORD_A != rlh->record_type)
1329     {
1330       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331                  "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1332                  rh->id);
1333       rh->proc (rh->proc_cls, rh, 0, NULL);
1334       return;
1335     }
1336     rd.record_type = GNUNET_GNS_RECORD_A;
1337     rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1338     rd.data = address;
1339     rd.data_size = sizeof (struct in_addr);
1340     rd.flags = 0;
1341     rh->proc (rh->proc_cls, rh, 1, &rd);
1342     return;
1343   }
1344   else if (AF_INET6 == af)
1345   {
1346     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1347                "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1348                rh->id);
1349     if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1350     {
1351       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1352                  "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1353                  rh->id);
1354       rh->proc (rh->proc_cls, rh, 0, NULL);
1355       return;
1356     }
1357     rd.record_type = GNUNET_GNS_RECORD_AAAA;
1358     rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1359     rd.data = address;
1360     rd.data_size = sizeof (struct in6_addr);
1361     rd.flags = 0;
1362     rh->proc (rh->proc_cls, rh, 1, &rd);
1363     return;
1364   }
1365
1366   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1367              "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1368              rh->id);
1369   rh->proc (rh->proc_cls, rh, 0, NULL);
1370 }
1371
1372
1373
1374
1375 /**
1376  * Process VPN lookup result for record
1377  *
1378  * @param cls the record lookup handle
1379  * @param rh resolver handle
1380  * @param rd_count number of results (1)
1381  * @param rd record data containing the result
1382  */
1383 static void
1384 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1385                    unsigned int rd_count,
1386                    const struct GNUNET_NAMESTORE_RecordData *rd)
1387 {
1388   struct RecordLookupHandle* rlh = cls;
1389   
1390   if (0 == rd_count)
1391   {
1392     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1393                "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1394                rh->id,
1395                rh->status);
1396     /* give up, cannot resolve */
1397     finish_lookup(rh, rlh, 0, NULL);
1398     return;
1399   }
1400
1401   /* results found yay */
1402   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1403              "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1404
1405   finish_lookup(rh, rlh, rd_count, rd);
1406 }
1407
1408
1409 /**
1410  * Sends a UDP dns query to a nameserver specified in the rh
1411  * 
1412  * @param rh the resolver handle
1413  */
1414 static void
1415 send_dns_packet (struct ResolverHandle *rh);
1416
1417
1418 static void
1419 handle_dns_resolver (void *cls,
1420                      const struct sockaddr *addr,
1421                      socklen_t addrlen)
1422 {
1423   struct ResolverHandle *rh = cls;
1424   struct RecordLookupHandle *rlh = rh->proc_cls;
1425   struct GNUNET_NAMESTORE_RecordData rd;
1426   struct sockaddr_in *sai;
1427   struct sockaddr_in6 *sai6;
1428
1429   if (NULL == addr)
1430   {
1431     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1432                 "No address found in DNS!\n");
1433     finish_lookup (rh, rlh, 0, NULL);
1434     return;
1435   }
1436   
1437   if (sizeof (struct sockaddr_in) == addrlen)
1438   {
1439     sai = (struct sockaddr_in*) addr;
1440     rd.record_type = GNUNET_GNS_RECORD_A;
1441     rd.data_size = sizeof (struct in_addr);
1442     rd.data = &sai->sin_addr;
1443   }
1444   else if (sizeof (struct sockaddr_in6) == addrlen)
1445   {
1446     sai6 = (struct sockaddr_in6*) addr;
1447     rd.record_type = GNUNET_GNS_RECORD_AAAA;
1448     rd.data_size = sizeof (struct in6_addr);
1449     rd.data = &sai6->sin6_addr;
1450   }
1451   else
1452   {
1453     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1454                 "Address length is garbage!\n");
1455     finish_lookup (rh, rlh, 0, NULL);
1456     return;
1457   }
1458   rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1459   rd.flags = 0;
1460   finish_lookup (rh, rlh, 1, &rd);
1461 }
1462
1463 /**
1464  * Resolve DNS name via local stub resolver
1465  *
1466  * @param rh the resolver handle
1467  */
1468 static void
1469 resolve_dns_name (struct ResolverHandle *rh)
1470 {
1471   struct RecordLookupHandle *rlh = rh->proc_cls;
1472   int af;
1473
1474   if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1475       (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1476   {
1477     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478                 "Can only resolve A/AAAA via stub... abort\n");
1479     finish_lookup (rh, rlh, 0, NULL);
1480     return;
1481   }
1482
1483   if (GNUNET_GNS_RECORD_A == rlh->record_type)
1484     af = AF_INET;
1485   else
1486     af = AF_INET6;
1487
1488   rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1489                                                     af,
1490                                                     rh->timeout,
1491                                                     &handle_dns_resolver,
1492                                                     rh);
1493 }
1494
1495
1496 /**
1497  * Read DNS udp packet from socket
1498  *
1499  * @param cls the resolver handle
1500  * @param tc task context
1501  */
1502 static void
1503 read_dns_response (void *cls,
1504                    const struct GNUNET_SCHEDULER_TaskContext *tc)
1505 {
1506   struct ResolverHandle *rh = cls;
1507   struct RecordLookupHandle *rlh = rh->proc_cls;
1508   char buf[UINT16_MAX];
1509   ssize_t r;
1510   struct sockaddr_in addr;
1511   socklen_t addrlen;
1512   struct GNUNET_DNSPARSER_Packet *packet;
1513   struct GNUNET_NAMESTORE_RecordData rd;
1514   int found_delegation = GNUNET_NO;
1515   int found_cname = GNUNET_NO;
1516   char* delegation_name = NULL;
1517   int zone_offset = 0;
1518   int i;
1519
1520   rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1521   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1522   {
1523     /* timeout or shutdown */
1524     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525                 "Terminating DNS query %d\n", tc->reason);
1526     finish_lookup (rh, rlh, 0, NULL);
1527     return;
1528   }
1529
1530   addrlen = sizeof (addr);
1531   r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1532                                       buf, sizeof (buf),
1533                                       (struct sockaddr*) &addr,
1534                                       &addrlen);
1535
1536   if (-1 == r)
1537   {
1538     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1539     finish_lookup (rh, rlh, 0, NULL);
1540     return;
1541   }
1542
1543   packet = GNUNET_DNSPARSER_parse (buf, r);
1544   
1545   if (NULL == packet)
1546   {
1547     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548                 "Failed to parse DNS reply!\n");
1549     finish_lookup (rh, rlh, 0, NULL);
1550     return;
1551   }
1552
1553   for (i = 0; i < packet->num_answers; i++)
1554   {
1555     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556                "Got record type %d (want %d)\n",
1557                packet->answers[i].type,
1558                rlh->record_type);
1559     /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1560     if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1561     {
1562       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563                   "CNAME... restarting query with %s\n",
1564                   packet->answers[i].data.hostname
1565                  );
1566       strcpy (rh->dns_name, packet->answers[i].data.hostname);
1567       found_cname = GNUNET_YES;
1568       continue;
1569     }
1570     
1571     if ((packet->answers[i].type == rlh->record_type) &&
1572         (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1573     {
1574       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575                   "Found record!\n");
1576       rd.data = packet->answers[i].data.raw.data;
1577       rd.data_size = packet->answers[i].data.raw.data_len;
1578       rd.record_type = packet->answers[i].type;
1579       rd.flags = 0;
1580       rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1581       finish_lookup (rh, rlh, 1, &rd);
1582       GNUNET_DNSPARSER_free_packet (packet);
1583       return;
1584     }
1585   }
1586
1587   if (GNUNET_YES == found_cname)
1588   {
1589     zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1590     
1591     if (0 > zone_offset)
1592       zone_offset = 0;
1593
1594     /* restart query with CNAME */
1595     if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1596     {
1597       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598                   "Asking same server for %s\n", rh->dns_name);
1599       send_dns_packet (rh);
1600     }
1601     else
1602     {
1603       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604                   "Trying system resolver for %s\n", rh->dns_name);
1605       resolve_dns_name (rh);
1606     }
1607
1608     GNUNET_DNSPARSER_free_packet (packet);
1609     return;
1610   }
1611
1612   for (i = 0; i < packet->num_authority_records; i++)
1613   {
1614     
1615     if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1616     {
1617       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618                   "Found NS delegation!\n");
1619       found_delegation = GNUNET_YES;
1620       delegation_name = packet->authority_records[i].data.hostname;
1621       break;
1622     }
1623   }
1624
1625   for (i = 0; i < packet->num_additional_records; i++)
1626   {
1627     if (GNUNET_NO == found_delegation)
1628       break;
1629
1630     if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1631         (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1632     {
1633       GNUNET_assert (sizeof (struct in_addr) ==
1634                      packet->authority_records[i].data.raw.data_len);
1635       
1636       rh->dns_addr.sin_addr =
1637         *((struct in_addr*)packet->authority_records[i].data.raw.data);
1638       send_dns_packet (rh);
1639       GNUNET_DNSPARSER_free_packet (packet);
1640       return;
1641     }
1642   }
1643
1644   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645               "Nothing useful in DNS reply!\n");
1646   finish_lookup (rh, rlh, 0, NULL);
1647   GNUNET_DNSPARSER_free_packet (packet);
1648   return;
1649 }
1650
1651 /**
1652  * Sends a UDP dns query to a nameserver specified in the rh
1653  * 
1654  * @param rh the request handle
1655  */
1656 static void
1657 send_dns_packet (struct ResolverHandle *rh)
1658 {
1659   struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1660   GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1661
1662   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663               "Sending %dbyte DNS query\n",
1664               rh->dns_raw_packet_size);
1665   
1666   if (GNUNET_SYSERR ==
1667       GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1668                                     rh->dns_raw_packet,
1669                                     rh->dns_raw_packet_size,
1670                                     (struct sockaddr*)&rh->dns_addr,
1671                                     sizeof (struct sockaddr_in)))
1672     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1673                 _("Failed to send DNS request to %s\n"),
1674                 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr, 
1675                             sizeof (struct sockaddr_in)));
1676
1677   rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1678                                                     rh->timeout, //FIXME less?
1679                                                     rset,
1680                                                     NULL,
1681                                                     &read_dns_response,
1682                                                     rh);
1683
1684   GNUNET_NETWORK_fdset_destroy (rset);
1685
1686 }
1687
1688 /**
1689  * The final phase of resoution.
1690  * We found a NS RR and want to resolve via DNS
1691  *
1692  * @param rh the pending lookup handle
1693  * @param rd_count length of record data
1694  * @param rd record data containing VPN RR
1695  */
1696 static void
1697 resolve_record_dns (struct ResolverHandle *rh,
1698                     int rd_count,
1699                     const struct GNUNET_NAMESTORE_RecordData *rd)
1700 {
1701   struct GNUNET_DNSPARSER_Query query;
1702   struct GNUNET_DNSPARSER_Packet packet;
1703   struct GNUNET_DNSPARSER_Flags flags;
1704   struct in_addr dnsip;
1705   struct sockaddr_in addr;
1706   struct sockaddr *sa;
1707   int i;
1708   struct RecordLookupHandle *rlh = rh->proc_cls;
1709
1710   memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1711   memset (rh->dns_name, 0, sizeof (rh->dns_name));
1712   
1713   /* We cancel here as to not include the ns lookup in the timeout */
1714   if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1715   {
1716     GNUNET_SCHEDULER_cancel(rh->timeout_task);
1717     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1718   }
1719   /* Start shortening */
1720   if ((NULL != rh->priv_key) &&
1721       (GNUNET_YES == is_canonical (rh->name)))
1722   {
1723     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724              "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1725              rh->id);
1726     start_shorten (rh->authority_chain_head,
1727                    rh->priv_key);
1728   }
1729
1730   for (i = 0; i < rd_count; i++)
1731   {
1732     /* Synthesize dns name */
1733     if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1734     {
1735       strcpy (rh->dns_zone, (char*)rd[i].data);
1736       if (0 == strcmp (rh->name, ""))
1737         strcpy (rh->dns_name, (char*)rd[i].data);
1738       else
1739         sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1740     }
1741     /* The glue */
1742     if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1743          /* need to use memcpy as .data may be unaligned */
1744         memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1745   }
1746   
1747   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748               "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1749               rh->id,
1750               rh->dns_name,
1751               inet_ntoa (dnsip));
1752   rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1753   if (NULL == rh->dns_sock)
1754   {
1755     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756                 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1757                 rh->id);
1758     finish_lookup (rh, rlh, 0, NULL);
1759     return;
1760   }
1761
1762   memset (&addr, 0, sizeof (struct sockaddr_in));
1763   sa = (struct sockaddr *) &addr;
1764   sa->sa_family = AF_INET;
1765   if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1766                                                sa,
1767                                                sizeof (struct sockaddr_in)))
1768   {
1769     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770                 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1771                 rh->id);
1772     finish_lookup (rh, rlh, 0, NULL);
1773     return;
1774   }
1775   query.name = rh->dns_name;
1776   query.type = rlh->record_type;
1777   query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1778   memset (&flags, 0, sizeof (flags));
1779   flags.recursion_desired = 1;
1780   flags.checking_disabled = 1;
1781   packet.queries = &query;
1782   packet.answers = NULL;
1783   packet.authority_records = NULL;
1784   packet.num_queries = 1;
1785   packet.num_answers = 0;
1786   packet.num_authority_records = 0;
1787   packet.num_additional_records = 0;
1788   packet.flags = flags;
1789   packet.id = rh->id;
1790   if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1791                                           UINT16_MAX,
1792                                           &rh->dns_raw_packet,
1793                                           &rh->dns_raw_packet_size))
1794   {
1795     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1796                 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1797                 rh->id);
1798     GNUNET_NETWORK_socket_close (rh->dns_sock);
1799     finish_lookup (rh, rlh, 0, NULL);
1800     return;
1801   }
1802
1803   rh->dns_addr.sin_family = AF_INET;
1804   rh->dns_addr.sin_port = htons (53); //domain
1805   rh->dns_addr.sin_addr = dnsip;
1806 #if HAVE_SOCKADDR_IN_SIN_LEN
1807   rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1808 #endif
1809
1810   send_dns_packet (rh);
1811 }
1812
1813
1814 /**
1815  * The final phase of resoution.
1816  * We found a VPN RR and want to request an IPv4/6 address
1817  *
1818  * @param rh the pending lookup handle
1819  * @param rd_count length of record data
1820  * @param rd record data containing VPN RR
1821  */
1822 static void
1823 resolve_record_vpn (struct ResolverHandle *rh,
1824                     int rd_count,
1825                     const struct GNUNET_NAMESTORE_RecordData *rd)
1826 {
1827   struct RecordLookupHandle *rlh = rh->proc_cls;
1828   struct GNUNET_HashCode serv_desc;
1829   struct vpn_data* vpn;
1830   int af;
1831   
1832   /* We cancel here as to not include the ns lookup in the timeout */
1833   if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1834   {
1835     GNUNET_SCHEDULER_cancel(rh->timeout_task);
1836     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1837   }
1838   /* Start shortening */
1839   if ((NULL != rh->priv_key) &&
1840       (GNUNET_YES == is_canonical (rh->name)))
1841   {
1842     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843              "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1844              rh->id);
1845     start_shorten (rh->authority_chain_head,
1846                    rh->priv_key);
1847   }
1848
1849   vpn = (struct vpn_data*)rd->data;
1850
1851
1852   GNUNET_CRYPTO_hash ((char*)&vpn[1],
1853                       strlen ((char*)&vpn[1]) + 1,
1854                       &serv_desc);
1855   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856               "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1857               rh->id,
1858               ntohs (vpn->proto),
1859               GNUNET_h2s (&vpn->peer));
1860
1861   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862               "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1863               rh->id,
1864               (char*)&vpn[1],
1865               GNUNET_h2s (&serv_desc));
1866   rh->proc = &handle_record_vpn;
1867   if (GNUNET_GNS_RECORD_A == rlh->record_type)
1868     af = AF_INET;
1869   else
1870     af = AF_INET6;
1871   if (NULL == vpn_handle)
1872   {
1873     vpn_handle = GNUNET_VPN_connect (cfg);
1874     if (NULL == vpn_handle)
1875     {
1876       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877                   "GNS_PHASE_INIT: Error connecting to VPN!\n");
1878       finish_lookup (rh, rh->proc_cls, 0, NULL);
1879       return;
1880     }
1881   }
1882
1883   rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1884                                                 af, ntohs (vpn->proto),
1885                                                 (struct GNUNET_PeerIdentity *)&vpn->peer,
1886                                                 &serv_desc,
1887                                                 GNUNET_NO, //nac
1888                                                 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1889                                                 &process_record_result_vpn,
1890                                                 rh);
1891
1892 }
1893
1894 /**
1895  * The final phase of resolution.
1896  * rh->name is a name that is canonical and we do not have a delegation.
1897  * Query namestore for this record
1898  *
1899  * @param rh the pending lookup handle
1900  */
1901 static void
1902 resolve_record_ns(struct ResolverHandle *rh)
1903 {
1904   struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1905   
1906   /* We cancel here as to not include the ns lookup in the timeout */
1907   if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1908   {
1909     GNUNET_SCHEDULER_cancel(rh->timeout_task);
1910     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1911   }
1912   /* Start shortening */
1913   if ((NULL != rh->priv_key) &&
1914      (GNUNET_YES == is_canonical (rh->name)))
1915   {
1916     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917              "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1918              rh->id);
1919     start_shorten (rh->authority_chain_head,
1920                    rh->priv_key);
1921   }
1922   
1923   /**
1924    * Try to resolve this record in our namestore.
1925    * The name to resolve is now in rh->authority_name
1926    * since we tried to resolve it to an authority
1927    * and failed.
1928    **/
1929   rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1930                                  &rh->authority,
1931                                  rh->name,
1932                                  rlh->record_type,
1933                                  &process_record_result_ns,
1934                                  rh);
1935 }
1936
1937
1938
1939 /**
1940  * Handle timeout for DHT requests
1941  *
1942  * @param cls the request handle as closure
1943  * @param tc the task context
1944  */
1945 static void
1946 dht_authority_lookup_timeout(void *cls,
1947                              const struct GNUNET_SCHEDULER_TaskContext *tc)
1948 {
1949   struct ResolverHandle *rh = cls;
1950   struct RecordLookupHandle *rlh = rh->proc_cls;
1951   char new_name[MAX_DNS_NAME_LENGTH];
1952
1953   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1954          "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1955          rh->id, rh->authority_name, rh->timeout.rel_value);
1956
1957   rh->status |= RSL_TIMED_OUT;
1958   rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1959   if (NULL != rh->get_handle)
1960     GNUNET_DHT_get_stop (rh->get_handle);
1961
1962   rh->get_handle = NULL;
1963   if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1964   {
1965     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1966                "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1967                rh->id);
1968     rh->proc(rh->proc_cls, rh, 0, NULL);
1969     return;
1970   }
1971
1972   if (0 == strcmp(rh->name, ""))
1973   {
1974     /*
1975      * promote authority back to name and try to resolve record
1976      */
1977     strcpy(rh->name, rh->authority_name);
1978     rh->proc(rh->proc_cls, rh, 0, NULL);
1979     return;
1980   }
1981   
1982   /**
1983    * Start resolution in bg
1984    */
1985   GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1986                   "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
1987   
1988   strcpy(rh->name, new_name);
1989
1990   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1991         "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1992         rh->id, rh->name, rlh->record_type);
1993
1994   gns_resolver_lookup_record (rh->authority,
1995                               rh->private_local_zone,
1996                               rlh->record_type,
1997                               new_name,
1998                               NULL,
1999                               GNUNET_TIME_UNIT_FOREVER_REL,
2000                               GNUNET_NO,
2001                               &background_lookup_result_processor,
2002                               NULL);
2003   rh->proc(rh->proc_cls, rh, 0, NULL);
2004 }
2005
2006 /* Prototype */
2007 static void resolve_delegation_dht(struct ResolverHandle *rh);
2008
2009 /* Prototype */
2010 static void resolve_delegation_ns(struct ResolverHandle *rh);
2011
2012
2013 /**
2014  * Namestore resolution for delegation finished. Processing result.
2015  *
2016  * @param cls the closure
2017  * @param rh resolver handle
2018  * @param rd_count number of results (always 0)
2019  * @param rd record data (always NULL)
2020  */
2021 static void
2022 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2023                           unsigned int rd_count,
2024                           const struct GNUNET_NAMESTORE_RecordData *rd);
2025
2026
2027 /**
2028  * This is a callback function that checks for key revocation
2029  *
2030  * @param cls the pending query
2031  * @param key the key of the zone we did the lookup
2032  * @param expiration expiration date of the record data set in the namestore
2033  * @param name the name for which we need an authority
2034  * @param rd_count the number of records with 'name'
2035  * @param rd the record data
2036  * @param signature the signature of the authority for the record data
2037  */
2038 static void
2039 process_pkey_revocation_result_ns (void *cls,
2040                     const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2041                     struct GNUNET_TIME_Absolute expiration,
2042                     const char *name,
2043                     unsigned int rd_count,
2044                     const struct GNUNET_NAMESTORE_RecordData *rd,
2045                     const struct GNUNET_CRYPTO_RsaSignature *signature)
2046 {
2047   struct ResolverHandle *rh = cls;
2048   struct GNUNET_TIME_Relative remaining_time;
2049   int i;
2050   
2051   rh->namestore_task = NULL;
2052   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2053   
2054   for (i = 0; i < rd_count; i++)
2055   {
2056     if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2057     {
2058       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2059                  "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2060                  rh->id);
2061       rh->status |= RSL_PKEY_REVOKED;
2062       rh->proc (rh->proc_cls, rh, 0, NULL);
2063       return;
2064     }
2065   }
2066   
2067   if ((NULL == name) ||
2068       (0 == remaining_time.rel_value))
2069   {
2070     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2071           "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2072           rh->id, name);
2073
2074     if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2075     {
2076       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2077         "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2078         rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2079
2080       gns_resolver_lookup_record(rh->authority,
2081                                  rh->private_local_zone,
2082                                  GNUNET_GNS_RECORD_REV,
2083                                  GNUNET_GNS_TLD,
2084                                  NULL,
2085                                  GNUNET_TIME_UNIT_FOREVER_REL,
2086                                  GNUNET_NO,
2087                                  &background_lookup_result_processor,
2088                                  NULL);
2089     }
2090   }
2091  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2092              "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2093              rh->id);
2094   /**
2095    * We are done with PKEY resolution if name is empty
2096    * else resolve again with new authority
2097    */
2098   if (strcmp (rh->name, "") == 0)
2099     rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2100   else
2101     resolve_delegation_ns (rh);
2102 }
2103
2104
2105 /**
2106  * Callback when record data is put into namestore
2107  *
2108  * @param cls the closure
2109  * @param success GNUNET_OK on success
2110  * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2111  */
2112 void
2113 on_namestore_delegation_put_result(void *cls,
2114                                    int32_t success,
2115                                    const char *emsg)
2116 {
2117   struct NamestoreBGTask *nbg = cls;
2118
2119   GNUNET_CONTAINER_heap_remove_node (nbg->node);
2120   GNUNET_free (nbg);
2121
2122   if (GNUNET_NO == success)
2123   {
2124     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2125                "GNS_NS: records already in namestore\n");
2126     return;
2127   }
2128   else if (GNUNET_YES == success)
2129   {
2130     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2131                "GNS_NS: records successfully put in namestore\n");
2132     return;
2133   }
2134
2135   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2136              "GNS_NS: Error putting records into namestore: %s\n", emsg);
2137 }
2138
2139 /**
2140  * Function called when we get a result from the dht
2141  * for our query. Recursively tries to resolve authorities
2142  * for name in DHT.
2143  *
2144  * @param cls the request handle
2145  * @param exp lifetime
2146  * @param key the key the record was stored under
2147  * @param get_path get path
2148  * @param get_path_length get path length
2149  * @param put_path put path
2150  * @param put_path_length put path length
2151  * @param type the block type
2152  * @param size the size of the record
2153  * @param data the record data
2154  */
2155 static void
2156 process_delegation_result_dht(void* cls,
2157                  struct GNUNET_TIME_Absolute exp,
2158                  const struct GNUNET_HashCode * key,
2159                  const struct GNUNET_PeerIdentity *get_path,
2160                  unsigned int get_path_length,
2161                  const struct GNUNET_PeerIdentity *put_path,
2162                  unsigned int put_path_length,
2163                  enum GNUNET_BLOCK_Type type,
2164                  size_t size, const void *data)
2165 {
2166   struct ResolverHandle *rh = cls;
2167   struct GNSNameRecordBlock *nrb;
2168   uint32_t num_records;
2169   char* name = NULL;
2170   char* rd_data = (char*) data;
2171   uint32_t i;
2172   int rd_size;
2173   struct GNUNET_CRYPTO_ShortHashCode zone;
2174
2175   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2176              "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2177
2178   if (data == NULL)
2179     return;
2180   
2181   nrb = (struct GNSNameRecordBlock*)data;
2182   
2183   /* stop dht lookup and timeout task */
2184   GNUNET_DHT_get_stop (rh->get_handle);
2185   rh->get_handle = NULL;
2186   if (rh->dht_heap_node != NULL)
2187   {
2188     GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2189     rh->dht_heap_node = NULL;
2190   }
2191
2192   num_records = ntohl(nrb->rd_count);
2193   name = (char*)&nrb[1];
2194   {
2195     struct GNUNET_NAMESTORE_RecordData rd[num_records];
2196     struct NamestoreBGTask *ns_heap_root;
2197     struct NamestoreBGTask *namestore_bg_task;
2198     
2199     rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2200     rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2201     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2202                                                                rd_data,
2203                                                                num_records,
2204                                                                rd))
2205     {
2206       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2207                  "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2208                  rh->id);
2209       return;
2210     }
2211
2212     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2213                "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2214                rh->id, name, rh->authority_name);
2215     for (i=0; i<num_records; i++)
2216     {
2217     
2218       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2219                 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2220                 rh->id, name, rh->authority_name);
2221       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2222                  "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2223                  rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2224       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2225                  "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2226                  rh->id, rd[i].data_size);
2227       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2228                  "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2229                  rh->id, rd[i].flags);
2230       
2231       if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2232           (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2233           (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2234       {
2235         /**
2236          * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2237          */
2238         if (0 == strcmp(rh->name, ""))
2239           strcpy(rh->name, rh->authority_name);
2240         else
2241           GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2242                  rh->name, rh->authority_name); //FIXME ret
2243         rh->answered = 1;
2244         break;
2245       }
2246
2247       if ((0 == strcmp(name, rh->authority_name)) &&
2248           (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2249       {
2250         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2251                    "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2252                    rh->id);
2253         rh->answered = 1;
2254         memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2255         struct AuthorityChain *auth =
2256           GNUNET_malloc(sizeof(struct AuthorityChain));
2257         auth->zone = rh->authority;
2258         memset(auth->name, 0, strlen(rh->authority_name)+1);
2259         strcpy(auth->name, rh->authority_name);
2260         GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2261                                      rh->authority_chain_tail,
2262                                      auth);
2263
2264         if (NULL != rh->rd.data)
2265           GNUNET_free ((void*)rh->rd.data);
2266         
2267         memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2268         rh->rd.data = GNUNET_malloc (rd[i].data_size);
2269         memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2270         rh->rd_count = 1;
2271
2272         /** try to import pkey if private key available */
2273         //if (rh->priv_key && is_canonical (rh->name))
2274         //  process_discovered_authority(name, auth->zone,
2275         //                               rh->authority_chain_tail->zone,
2276         //                               rh->priv_key);
2277       }
2278
2279     }
2280     GNUNET_GNS_get_zone_from_key (name, key, &zone);
2281
2282
2283     /* Save to namestore
2284     if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2285                                           &zone))
2286     {*/
2287       if (max_allowed_ns_tasks <=
2288           GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2289       {
2290         ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2291         GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2292
2293         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2294                    "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2295                    rh->id);
2296       }
2297       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298                   "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2299                   rh->id, name);
2300       namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2301
2302       namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2303                                     namestore_bg_task,
2304                                     GNUNET_TIME_absolute_get().abs_value);
2305       namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2306                                  &nrb->public_key,
2307                                  name,
2308                                  exp,
2309                                  num_records,
2310                                  rd,
2311                                  &nrb->signature,
2312                                  &on_namestore_delegation_put_result, //cont
2313                                  namestore_bg_task); //cls
2314     }
2315   //}
2316
2317   if (0 != rh->answered)
2318   {
2319     rh->answered = 0;
2320     /**
2321      * delegate
2322      * FIXME in this case. should we ask namestore again?
2323      */
2324     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2325     "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2326     rh->id, rh->authority_name, rh->name);
2327
2328     if (0 == strcmp(rh->name, ""))
2329     {
2330       /* Start shortening */
2331       if ((NULL != rh->priv_key) &&
2332           (GNUNET_YES == is_canonical (rh->name)))
2333       {
2334         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335              "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2336              rh->id);
2337         start_shorten (rh->authority_chain_head,
2338                        rh->priv_key);
2339       }
2340     }
2341     else
2342       rh->proc = &handle_delegation_ns;
2343
2344
2345     /* Check for key revocation and delegate */
2346     rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2347                                     &rh->authority,
2348                                     "+",
2349                                     GNUNET_GNS_RECORD_REV,
2350                                     &process_pkey_revocation_result_ns,
2351                                     rh);
2352
2353     return;
2354   }
2355   
2356   /**
2357    * No pkey but name exists
2358    * promote back
2359    */
2360   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2361              "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2362              rh->id, rh->authority_name, rh->name);
2363   if (0 == strcmp(rh->name, ""))
2364     strcpy(rh->name, rh->authority_name);
2365   else
2366     GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2367                   rh->name, rh->authority_name); //FIXME ret
2368   
2369   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2370              "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2371   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2372           "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2373            rh->id);
2374   rh->proc(rh->proc_cls, rh, 0, NULL);
2375 }
2376
2377 //FIXME maybe define somewhere else?
2378 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2379                         +(MAX_DNS_NAME_LENGTH*2)
2380 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2381 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2382
2383
2384 static void
2385 expand_plus(char* dest, char* src, char* repl)
2386 {
2387   char* pos;
2388   unsigned int s_len = strlen(src)+1;
2389
2390   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2391              "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2392   //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2393   if (3 > s_len)
2394   {
2395     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2396                "GNS_POSTPROCESS: %s too short\n", src);
2397
2398     /* no postprocessing */
2399     memcpy(dest, src, s_len+1);
2400     return;
2401   }
2402   
2403   if (0 == strcmp(src+s_len-3, ".+"))
2404   {
2405     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2406                "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2407     memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2408     strcpy(dest, src);
2409     pos = dest+s_len-2;
2410     strcpy(pos, repl);
2411     pos += strlen(repl);
2412     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2413                "GNS_POSTPROCESS: Expanded to %s\n", dest);
2414   }
2415   else
2416   {
2417     memcpy(dest, src, s_len+1);
2418   }
2419 }
2420
2421 /**
2422  * finish lookup
2423  */
2424 static void
2425 finish_lookup (struct ResolverHandle *rh,
2426                struct RecordLookupHandle* rlh,
2427                unsigned int rd_count,
2428                const struct GNUNET_NAMESTORE_RecordData *rd)
2429 {
2430   int i;
2431   char new_rr_data[MAX_DNS_NAME_LENGTH];
2432   char new_mx_data[MAX_MX_LENGTH];
2433   char new_soa_data[MAX_SOA_LENGTH];
2434   char new_srv_data[MAX_SRV_LENGTH];
2435   struct srv_data *old_srv;
2436   struct srv_data *new_srv;
2437   struct soa_data *old_soa;
2438   struct soa_data *new_soa;
2439   struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2440   char* repl_string;
2441   char* pos;
2442   unsigned int offset;
2443
2444   if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2445   {
2446     GNUNET_SCHEDULER_cancel(rh->timeout_task);
2447     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2448   }
2449
2450   GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2451
2452   if (0 < rd_count)
2453     memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2454
2455   for (i = 0; i < rd_count; i++)
2456   {
2457     
2458     if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2459         (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2460         (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2461         (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2462         (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2463         (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2464     {
2465       p_rd[i].data = rd[i].data;
2466       continue;
2467     }
2468
2469     /**
2470      * for all those records we 'should'
2471      * also try to resolve the A/AAAA records (RFC1035)
2472      * This is a feature and not important
2473      */
2474     
2475     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2476                "GNS_POSTPROCESS: Postprocessing\n");
2477     if (0 == strcmp(rh->name, "+"))
2478       repl_string = rlh->name;
2479     else
2480       repl_string = rlh->name+strlen(rh->name)+1;
2481
2482     offset = 0;
2483     if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2484     {
2485       memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2486       offset = sizeof(uint16_t);
2487       pos = new_mx_data+offset;
2488       expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2489                   repl_string);
2490       offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2491       p_rd[i].data = new_mx_data;
2492       p_rd[i].data_size = offset;
2493     }
2494     else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2495     {
2496       /*
2497        * Prio, weight and port
2498        */
2499       new_srv = (struct srv_data*)new_srv_data;
2500       old_srv = (struct srv_data*)rd[i].data;
2501       new_srv->prio = old_srv->prio;
2502       new_srv->weight = old_srv->weight;
2503       new_srv->port = old_srv->port;
2504       expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2505                   repl_string);
2506       p_rd[i].data = new_srv_data;
2507       p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2508     }
2509     else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2510     {
2511       /* expand mname and rname */
2512       old_soa = (struct soa_data*)rd[i].data;
2513       new_soa = (struct soa_data*)new_soa_data;
2514       memcpy (new_soa, old_soa, sizeof (struct soa_data));
2515       expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2516       offset = strlen ((char*)&new_soa[1]) + 1;
2517       expand_plus((char*)&new_soa[1] + offset,
2518                   (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2519                   repl_string);
2520       p_rd[i].data_size = sizeof (struct soa_data)
2521                           + offset
2522                           + strlen ((char*)&new_soa[1] + offset);
2523       p_rd[i].data = new_soa_data;
2524     }
2525     else
2526     {
2527       pos = new_rr_data;
2528       expand_plus(pos, (char*)rd[i].data, repl_string);
2529       p_rd[i].data_size = strlen(new_rr_data)+1;
2530       p_rd[i].data = new_rr_data;
2531     }
2532     
2533   }
2534
2535   rlh->proc(rlh->proc_cls, rd_count, p_rd);
2536   GNUNET_free(rlh);
2537   free_resolver_handle (rh);
2538 }
2539
2540 /**
2541  * Process DHT lookup result for record.
2542  *
2543  * @param cls the closure
2544  * @param rh resolver handle
2545  * @param rd_count number of results
2546  * @param rd record data
2547  */
2548 static void
2549 handle_record_dht(void* cls, struct ResolverHandle *rh,
2550                        unsigned int rd_count,
2551                        const struct GNUNET_NAMESTORE_RecordData *rd)
2552 {
2553   struct RecordLookupHandle* rlh = cls;
2554
2555   if (0 == rd_count)
2556   {
2557     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558                "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2559                rh->id, rh->name);
2560     /* give up, cannot resolve */
2561     finish_lookup (rh, rlh, 0, NULL);
2562     return;
2563   }
2564   /* results found yay */
2565   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566              "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2567   finish_lookup (rh, rlh, rd_count, rd);
2568 }
2569
2570
2571
2572
2573 /**
2574  * Process namestore lookup result for record.
2575  *
2576  * @param cls the closure
2577  * @param rh resolver handle
2578  * @param rd_count number of results
2579  * @param rd record data
2580  */
2581 static void
2582 handle_record_ns (void* cls, struct ResolverHandle *rh,
2583                   unsigned int rd_count,
2584                   const struct GNUNET_NAMESTORE_RecordData *rd)
2585 {
2586   struct RecordLookupHandle* rlh = cls;
2587   int check_dht = GNUNET_YES;
2588   
2589   if (0 != rd_count)
2590   {
2591     /* results found yay */
2592     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593                "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2594     finish_lookup (rh, rlh, rd_count, rd);
2595     return;
2596   }
2597   
2598   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599               "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2600               rh->id,
2601               rh->status);
2602   /**
2603    * There are 5 conditions that have to met for us to consult the DHT:
2604    * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2605    * 2. No entry in the NS existed AND
2606    * 3. The zone queried is not the local resolver's zone AND
2607    * 4. The name that was looked up is '+'
2608    *    because if it was any other canonical name we either already queried
2609    *    the DHT for the authority in the authority lookup phase (and thus
2610    *    would already have an entry in the NS for the record)
2611    * 5. We are not in cache only mode
2612    */
2613   if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2614       (0 == (rh->status & RSL_RECORD_EXISTS)) )
2615   {
2616     
2617     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618               "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2619               rh->id);
2620     check_dht = GNUNET_NO;
2621   }
2622   
2623   if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2624                                         &rh->private_local_zone))
2625   {
2626
2627     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628               "GNS_PHASE_REC-%llu: Our zone!\n",
2629               rh->id);
2630     check_dht = GNUNET_NO;
2631   }
2632   
2633   if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2634       check_dht = GNUNET_NO;
2635
2636   if (GNUNET_YES == rh->only_cached)
2637     check_dht = GNUNET_NO;
2638   
2639   if (GNUNET_YES == check_dht)
2640   {
2641     rh->proc = &handle_record_dht;
2642     resolve_record_dht(rh);
2643     return;
2644   }
2645   /* give up, cannot resolve */
2646   finish_lookup (rh, rlh, 0, NULL);
2647 }
2648
2649
2650 /**
2651  * Move one level up in the domain hierarchy and return the
2652  * passed top level domain.
2653  *
2654  * @param name the domain
2655  * @param dest the destination where the tld will be put
2656  */
2657 void
2658 pop_tld(char* name, char* dest)
2659 {
2660   uint32_t len;
2661
2662   if (GNUNET_YES == is_canonical (name))
2663   {
2664     strcpy(dest, name);
2665     strcpy(name, "");
2666     return;
2667   }
2668
2669   for (len = strlen(name); 0 < len; len--)
2670   {
2671     if (*(name+len) == '.')
2672       break;
2673   }
2674   
2675   //Was canonical?
2676   if (0 == len)
2677     return;
2678
2679   name[len] = '\0';
2680
2681   strcpy(dest, (name+len+1));
2682 }
2683
2684 /**
2685  * Checks if name is in tld
2686  *
2687  * @param name the name to check
2688  * @param tld the TLD to check for
2689  * @return GNUNET_YES or GNUNET_NO
2690  */
2691 int
2692 is_tld(const char* name, const char* tld)
2693 {
2694   int offset = 0;
2695
2696   if (strlen(name) <= strlen(tld))
2697     return GNUNET_NO;
2698   
2699   offset = strlen(name)-strlen(tld);
2700   if (0 != strcmp(name+offset, tld))
2701   {
2702     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2703                "%s is not in .%s TLD\n", name, tld);
2704     return GNUNET_NO;
2705   }
2706   return GNUNET_YES;
2707 }
2708
2709 /**
2710  * DHT resolution for delegation finished. Processing result.
2711  *
2712  * @param cls the closure
2713  * @param rh resolver handle
2714  * @param rd_count number of results (always 0)
2715  * @param rd record data (always NULL)
2716  */
2717 static void
2718 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2719                           unsigned int rd_count,
2720                           const struct GNUNET_NAMESTORE_RecordData *rd)
2721 {
2722   struct RecordLookupHandle* rlh;
2723   rlh = cls;
2724   
2725
2726   if (0 == strcmp(rh->name, ""))
2727   {
2728     if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2729     {
2730       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2731                  "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2732                  rh->id);
2733       finish_lookup(rh, rlh, rd_count, rd);
2734       return;
2735     }
2736     /* We resolved full name for delegation. resolving record */
2737     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2738      "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2739      rh->id);
2740     strcpy(rh->name, "+\0");
2741     rh->proc = &handle_record_ns;
2742     resolve_record_ns(rh);
2743     return;
2744   }
2745
2746   /**
2747    * we still have some left
2748    **/
2749   if (GNUNET_YES == is_canonical (rh->name))
2750   {
2751     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2752              "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2753              rh->id,
2754              rh->name);
2755     rh->proc = &handle_record_ns;
2756     resolve_record_ns(rh);
2757     return;
2758   }
2759   /* give up, cannot resolve */
2760   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2761  "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2762  rh->id, rh->name);
2763   finish_lookup(rh, rlh, 0, NULL);
2764 }
2765
2766
2767 /**
2768  * Start DHT lookup for a name -> PKEY (compare NS) record in
2769  * rh->authority's zone
2770  *
2771  * @param rh the pending gns query
2772  */
2773 static void
2774 resolve_delegation_dht(struct ResolverHandle *rh)
2775 {
2776   uint32_t xquery;
2777   struct GNUNET_HashCode lookup_key;
2778   struct ResolverHandle *rh_heap_root;
2779   
2780   pop_tld(rh->name, rh->authority_name);
2781
2782   GNUNET_GNS_get_key_for_record (rh->authority_name, &rh->authority, &lookup_key);
2783   
2784   rh->dht_heap_node = NULL;
2785
2786   if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2787   {
2788     rh->timeout_cont = &dht_authority_lookup_timeout;
2789     rh->timeout_cont_cls = rh;
2790   }
2791   else 
2792   {
2793     if (max_allowed_background_queries <=
2794         GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2795     {
2796       /* terminate oldest lookup */
2797       rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2798       GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2799       rh_heap_root->get_handle = NULL;
2800       rh_heap_root->dht_heap_node = NULL;
2801       
2802       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2803         "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2804         rh->id, rh_heap_root->authority_name);
2805       
2806       rh_heap_root->proc(rh_heap_root->proc_cls,
2807                          rh_heap_root,
2808                          0,
2809                          NULL);
2810     }
2811     rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2812                                          rh,
2813                                          GNUNET_TIME_absolute_get().abs_value);
2814   }
2815   
2816   xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2817   GNUNET_assert(rh->get_handle == NULL);
2818   rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2819                        GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2820                        &lookup_key,
2821                        DHT_GNS_REPLICATION_LEVEL,
2822                        GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2823                        &xquery,
2824                        sizeof(xquery),
2825                        &process_delegation_result_dht,
2826                        rh);
2827 }
2828
2829
2830 /**
2831  * Namestore resolution for delegation finished. Processing result.
2832  *
2833  * @param cls the closure
2834  * @param rh resolver handle
2835  * @param rd_count number of results (always 0)
2836  * @param rd record data (always NULL)
2837  */
2838 static void
2839 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2840                       unsigned int rd_count,
2841                       const struct GNUNET_NAMESTORE_RecordData *rd)
2842 {
2843   struct RecordLookupHandle* rlh;
2844   rlh = cls;
2845   int check_dht = GNUNET_YES;
2846   int s_len = 0;
2847
2848   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2849              "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2850              rh->id, rh->status);
2851
2852   if (rh->status & RSL_PKEY_REVOKED)
2853   {
2854     finish_lookup (rh, rlh, 0, NULL);
2855     return;
2856   }
2857   
2858   if (0 == strcmp(rh->name, ""))
2859   {
2860     
2861     /* We resolved full name for delegation. resolving record */
2862     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2863               "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2864               rh->id);
2865     if (rh->status & RSL_CNAME_FOUND)
2866     {
2867       if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2868       {
2869         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2870                   "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2871                   rh->id);
2872         strcpy (rh->name, rh->authority_name);
2873         finish_lookup (rh, rlh, rd_count, rd);
2874         return;
2875       }
2876       
2877       /* A .+ CNAME  */
2878       if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2879       {
2880         s_len = strlen (rd->data) - 2;
2881         memcpy (rh->name, rd->data, s_len);
2882         rh->name[s_len] = '\0';
2883         resolve_delegation_ns (rh);
2884         return;
2885       }
2886       else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2887       {
2888         gns_resolver_lookup_record (rh->authority,
2889                                     rh->private_local_zone,
2890                                     rlh->record_type,
2891                                     (char*)rd->data,
2892                                     rh->priv_key,
2893                                     rh->timeout,
2894                                     rh->only_cached,
2895                                     rlh->proc,
2896                                     rlh->proc_cls);
2897         GNUNET_free (rlh);
2898         GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2899         free_resolver_handle (rh);
2900         return;
2901       }
2902       else
2903       {
2904         //Try DNS resolver
2905         strcpy (rh->dns_name, (char*)rd->data);
2906         resolve_dns_name (rh);
2907         return;
2908       }
2909
2910     }
2911     else if (rh->status & RSL_DELEGATE_VPN)
2912     {
2913       if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2914       {
2915         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2916                  "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2917                  rh->id);
2918         finish_lookup(rh, rlh, rd_count, rd);
2919         return;
2920       }
2921       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922              "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2923              rh->id);
2924       GNUNET_assert (NULL != rd);
2925       rh->proc = &handle_record_vpn;
2926       resolve_record_vpn (rh, rd_count, rd);
2927       return;
2928     }
2929     else if (rh->status & RSL_DELEGATE_NS)
2930     {
2931       if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2932       {
2933         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2934                    "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2935                    rh->id);
2936         finish_lookup(rh, rlh, rd_count, rd);
2937         return;
2938       }
2939       
2940       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2941                  "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2942                  rh->id);
2943       GNUNET_assert (NULL != rd);
2944       rh->proc = &handle_record_ns;
2945       resolve_record_dns (rh, rd_count, rd);
2946       return;
2947     }
2948     else if (rh->status & RSL_DELEGATE_PKEY)
2949     {
2950       if (rh->status & RSL_PKEY_REVOKED)
2951       {
2952         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2953                    "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2954                    rh->id);
2955         finish_lookup (rh, rlh, 0, NULL);
2956         return;
2957       }
2958       else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2959       {
2960         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2961                    "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2962                    rh->id);
2963         finish_lookup(rh, rlh, rd_count, rd);
2964         return;
2965       }
2966     }
2967     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2968                "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2969                rh->id);
2970     strcpy(rh->name, "+\0");
2971     rh->proc = &handle_record_ns;
2972     resolve_record_ns(rh);
2973     return;
2974   }
2975   
2976   if (rh->status & RSL_DELEGATE_NS)
2977   {
2978     if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2979     {
2980       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2981                  "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2982                  rh->id);
2983       finish_lookup(rh, rlh, rd_count, rd);
2984       return;
2985     }
2986     
2987     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2988                "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2989                rh->id);
2990     GNUNET_assert (NULL != rd);
2991     rh->proc = &handle_record_ns;
2992     resolve_record_dns (rh, rd_count, rd);
2993     return;
2994   }
2995   
2996   /**
2997    * we still have some left
2998    * check if authority in ns is fresh
2999    * and exists
3000    * or we are authority
3001    **/
3002
3003   if ((rh->status & RSL_RECORD_EXISTS) &&
3004        !(rh->status & RSL_RECORD_EXPIRED))
3005     check_dht = GNUNET_NO;
3006
3007   if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3008                                         &rh->private_local_zone))
3009     check_dht = GNUNET_NO;
3010
3011   if (GNUNET_YES == rh->only_cached)
3012     check_dht = GNUNET_NO;
3013
3014   if (GNUNET_YES == check_dht)
3015   {
3016
3017     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3018         "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3019         rh->id, rh->name);
3020     rh->proc = &handle_delegation_dht;
3021     resolve_delegation_dht(rh);
3022     return;
3023   }
3024   
3025   if (GNUNET_NO == is_canonical (rh->name))
3026   {
3027     /* give up, cannot resolve */
3028     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3029         "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3030         rh->id,
3031         rh->name);
3032     finish_lookup(rh, rlh, rd_count, rd);
3033     return;
3034   }
3035   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3036              "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3037              rh->id,
3038              rh->name);
3039   rh->proc = &handle_record_ns;
3040   resolve_record_ns(rh);
3041 }
3042
3043
3044 /**
3045  * This is a callback function that should give us only PKEY
3046  * records. Used to query the namestore for the authority (PKEY)
3047  * for 'name'. It will recursively try to resolve the
3048  * authority for a given name from the namestore.
3049  *
3050  * @param cls the pending query
3051  * @param key the key of the zone we did the lookup
3052  * @param expiration expiration date of the record data set in the namestore
3053  * @param name the name for which we need an authority
3054  * @param rd_count the number of records with 'name'
3055  * @param rd the record data
3056  * @param signature the signature of the authority for the record data
3057  */
3058 static void
3059 process_delegation_result_ns (void* cls,
3060                    const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3061                    struct GNUNET_TIME_Absolute expiration,
3062                    const char *name,
3063                    unsigned int rd_count,
3064                    const struct GNUNET_NAMESTORE_RecordData *rd,
3065                    const struct GNUNET_CRYPTO_RsaSignature *signature)
3066 {
3067   struct ResolverHandle *rh;
3068   struct GNUNET_TIME_Relative remaining_time;
3069   struct GNUNET_CRYPTO_ShortHashCode zone;
3070   char new_name[MAX_DNS_NAME_LENGTH];
3071   unsigned int i;
3072   struct GNUNET_TIME_Absolute et;
3073  
3074   rh = (struct ResolverHandle *)cls;
3075   rh->namestore_task = NULL;
3076   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3077           "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3078           rh->id, rd_count);
3079
3080   GNUNET_CRYPTO_short_hash (key,
3081                       sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3082                       &zone);
3083   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3084   
3085   rh->status = 0;
3086   
3087   if (name != NULL)
3088   {
3089     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090                 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3091                 rh->id, name);
3092     rh->status |= RSL_RECORD_EXISTS;
3093   
3094     if (remaining_time.rel_value == 0)
3095     {
3096       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3097                   "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3098                   rh->id, name);
3099       rh->status |= RSL_RECORD_EXPIRED;
3100     }
3101   }
3102   
3103   /**
3104    * No authority found in namestore.
3105    */
3106   if (rd_count == 0)
3107   {
3108     /**
3109      * We did not find an authority in the namestore
3110      */
3111     
3112     /**
3113      * No PKEY in zone.
3114      * Promote this authority back to a name maybe it is
3115      * our record.
3116      */
3117     if (strcmp (rh->name, "") == 0)
3118     {
3119       /* simply promote back */
3120       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3121                   "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3122                   rh->id, rh->authority_name);
3123       strcpy (rh->name, rh->authority_name);
3124     }
3125     else
3126     {
3127       /* add back to existing name */
3128       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129                   "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3130                   rh->id, rh->authority_name, rh->name);
3131       GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3132                        rh->name, rh->authority_name);
3133       strcpy (rh->name, new_name);
3134       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3135                   "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3136                   rh->id, rh->name);
3137     }
3138
3139     rh->proc (rh->proc_cls, rh, 0, NULL);
3140     return;
3141   }
3142
3143   /**
3144    * We found an authority that may be able to help us
3145    * move on with query
3146    * Note only 1 pkey should have been returned.. anything else would be strange
3147    */
3148   for (i=0; i < rd_count;i++)
3149   {
3150     
3151     /**
3152      * A CNAME. Like regular DNS this means the is no other record for this
3153      * name.
3154      */
3155     if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3156     {
3157       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3158                  "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3159                  rh->id);
3160
3161       rh->status |= RSL_CNAME_FOUND;
3162       rh->proc (rh->proc_cls, rh, rd_count, rd);
3163       return;
3164     }
3165
3166     /**
3167      * Redirect via VPN
3168      */
3169     if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3170     {
3171       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3172                  "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3173                  rh->id);
3174       rh->status |= RSL_DELEGATE_VPN;
3175       rh->proc (rh->proc_cls, rh, rd_count, rd);
3176       return;
3177     }
3178
3179     /**
3180      * Redirect via NS
3181      * FIXME make optional
3182      */
3183     if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3184     {
3185       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3186                  "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3187                  rh->id);
3188       rh->status |= RSL_DELEGATE_NS;
3189       rh->proc (rh->proc_cls, rh, rd_count, rd);
3190       return;
3191     }
3192   
3193     if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3194       continue;
3195
3196     rh->status |= RSL_DELEGATE_PKEY;
3197
3198     if ((ignore_pending_records != 0) &&
3199         (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3200     {
3201       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3202      "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3203         rh->id,
3204         name);
3205       continue;
3206     }
3207     
3208     GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3209     et.abs_value = rd[i].expiration_time;
3210     if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3211          == 0)
3212     {
3213       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3214                   "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3215                   rh->id);
3216       if (remaining_time.rel_value == 0)
3217       {
3218         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219                     "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3220                     rh->id);
3221         rh->authority_chain_head->fresh = 0;
3222         rh->proc (rh->proc_cls, rh, 0, NULL);
3223         return;
3224       }
3225
3226       continue;
3227     }
3228
3229     /**
3230      * Resolve rest of query with new authority
3231      */
3232     GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3233     memcpy (&rh->authority, rd[i].data,
3234             sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3235     struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3236     auth->zone = rh->authority;
3237     memset (auth->name, 0, strlen (rh->authority_name)+1);
3238     strcpy (auth->name, rh->authority_name);
3239     GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3240                                  rh->authority_chain_tail,
3241                                  auth);
3242     if (NULL != rh->rd.data)
3243       GNUNET_free ((void*)(rh->rd.data));
3244     
3245     memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3246     rh->rd.data = GNUNET_malloc (rd[i].data_size);
3247     memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3248     rh->rd_count = 1;
3249     /* Check for key revocation and delegate */
3250     rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3251                                     &rh->authority,
3252                                     "+",
3253                                     GNUNET_GNS_RECORD_REV,
3254                                     &process_pkey_revocation_result_ns,
3255                                     rh);
3256     return;
3257   
3258   }
3259   
3260   /**
3261    * no answers found
3262    */
3263   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3264     "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3265   /**
3266    * If we have found some records for the LAST label
3267    * we return the results. Else null.
3268    */
3269   if (strcmp (rh->name, "") == 0)
3270   {
3271     /* Start shortening */
3272     if ((rh->priv_key != NULL) &&
3273         (is_canonical (rh->name) == GNUNET_YES))
3274     {
3275       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3276               "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3277               rh->id);
3278       start_shorten (rh->authority_chain_head,
3279                     rh->priv_key);
3280     }
3281     /* simply promote back */
3282     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3283                 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3284                 rh->id, rh->authority_name);
3285     strcpy (rh->name, rh->authority_name);
3286     rh->proc (rh->proc_cls, rh, rd_count, rd);
3287   }
3288   else
3289   {
3290     GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3291                      "%s.%s", rh->name, rh->authority_name);
3292     strcpy (rh->name, new_name);
3293     rh->proc (rh->proc_cls, rh, 0, NULL);
3294   }
3295 }
3296
3297
3298 /**
3299  * Resolve the delegation chain for the request in our namestore
3300  *
3301  * @param rh the resolver handle
3302  */
3303 static void
3304 resolve_delegation_ns (struct ResolverHandle *rh)
3305 {
3306   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3307              "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3308              rh->id, rh->name);
3309   pop_tld(rh->name, rh->authority_name);
3310   rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3311                                  &rh->authority,
3312                                  rh->authority_name,
3313                                  GNUNET_GNS_RECORD_ANY,
3314                                  &process_delegation_result_ns,
3315                                  rh);
3316
3317 }
3318
3319
3320 /**
3321  * Lookup of a record in a specific zone
3322  * calls lookup result processor on result
3323  *
3324  * @param zone the root zone
3325  * @param pzone the private local zone
3326  * @param record_type the record type to look up
3327  * @param name the name to look up
3328  * @param key a private key for use with PSEU import (can be NULL)
3329  * @param timeout timeout for resolution
3330  * @param only_cached GNUNET_NO to only check locally not DHT for performance
3331  * @param proc the processor to call on result
3332  * @param cls the closure to pass to proc
3333  */
3334 void
3335 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3336                             struct GNUNET_CRYPTO_ShortHashCode pzone,
3337                             uint32_t record_type,
3338                             const char* name,
3339                             struct GNUNET_CRYPTO_RsaPrivateKey *key,
3340                             struct GNUNET_TIME_Relative timeout,
3341                             int only_cached,
3342                             RecordLookupProcessor proc,
3343                             void* cls)
3344 {
3345   struct ResolverHandle *rh;
3346   struct RecordLookupHandle* rlh;
3347   char string_hash[MAX_DNS_LABEL_LENGTH];
3348   char nzkey[MAX_DNS_LABEL_LENGTH];
3349   char* nzkey_ptr = nzkey;
3350
3351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352               "Starting resolution for %s (type=%d)!\n",
3353               name, record_type);
3354
3355   
3356   if ((is_canonical ((char*)name) == GNUNET_YES) &&
3357       (strcmp(GNUNET_GNS_TLD, name) != 0))
3358   {
3359     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3360                 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3361     proc(cls, 0, NULL);
3362     return;
3363   }
3364   
3365   rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3366   rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3367   
3368   memset (rh, 0, sizeof (struct ResolverHandle));
3369   rh->authority = zone;
3370   rh->id = rid++;
3371   rh->proc_cls = rlh;
3372   rh->priv_key = key;
3373   rh->timeout = timeout;
3374   rh->get_handle = NULL;
3375   rh->private_local_zone = pzone;
3376   rh->only_cached = only_cached;
3377   rh->namestore_task = NULL;
3378   rh->rd.data = NULL;
3379
3380   GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3381   
3382   if (NULL == key)
3383   {
3384     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3385                 "No shorten key for resolution\n");
3386   }
3387
3388   if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3389   {
3390     /*
3391      * Set timeout for authority lookup phase to 1/2
3392      */
3393     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394                 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3395     rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3396                                 GNUNET_TIME_relative_divide(timeout, 2),
3397                                                 &handle_lookup_timeout,
3398                                                 rh);
3399     rh->timeout_cont = &dht_authority_lookup_timeout;
3400     rh->timeout_cont_cls = rh;
3401   }
3402   else
3403   {
3404     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3405     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3406   }
3407   
3408   if (strcmp(GNUNET_GNS_TLD, name) == 0)
3409   {
3410     /**
3411      * Only 'gnunet' given
3412      */
3413     strcpy(rh->name, "\0");
3414   }
3415   else
3416   {
3417     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3418                 "Checking for TLD...\n");
3419     if (is_zkey_tld(name) == GNUNET_YES)
3420     {
3421       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3422                   "TLD is zkey\n");
3423       /**
3424        * This is a zkey tld
3425        * build hash and use as initial authority
3426        */
3427       memset(rh->name, 0,
3428              strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3429       memcpy(rh->name, name,
3430              strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3431       pop_tld(rh->name, string_hash);
3432
3433       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3434                   "ZKEY is %s!\n", string_hash);
3435       
3436       GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3437
3438       if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3439                                                       &rh->authority))
3440       {
3441         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3442                     "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3443         
3444         if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3445           GNUNET_SCHEDULER_cancel (rh->timeout_task);
3446         GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3447         GNUNET_free (rh);
3448         GNUNET_free (rlh);
3449         proc (cls, 0, NULL);
3450         return;
3451       }
3452
3453     }
3454     else if (is_gnunet_tld (name) == GNUNET_YES)
3455     {
3456       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3457                   "TLD is gnunet\n");
3458       /**
3459        * Presumably GNUNET tld
3460        */
3461       memset (rh->name, 0,
3462               strlen(name)-strlen(GNUNET_GNS_TLD));
3463       memcpy (rh->name, name,
3464               strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3465     }
3466     else
3467     {
3468       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3469                   "Cannot handle this TLD %s\n", string_hash);
3470       
3471       if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3472         GNUNET_SCHEDULER_cancel (rh->timeout_task);
3473       GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3474       GNUNET_free (rh);
3475       GNUNET_free (rlh);
3476       proc (cls, 0, NULL);
3477       return;
3478     }
3479   }
3480   
3481   /**
3482    * Initialize authority chain
3483    */
3484   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3485   rh->authority_chain_head->prev = NULL;
3486   rh->authority_chain_head->next = NULL;
3487   rh->authority_chain_tail = rh->authority_chain_head;
3488   rh->authority_chain_head->zone = rh->authority;
3489   strcpy (rh->authority_chain_head->name, "");
3490   
3491   /**
3492    * Copy original query into lookup handle
3493    */
3494   rlh->record_type = record_type;
3495   memset(rlh->name, 0, strlen(name) + 1);
3496   strcpy(rlh->name, name);
3497   rlh->proc = proc;
3498   rlh->proc_cls = cls;
3499
3500   rh->proc = &handle_delegation_ns;
3501   resolve_delegation_ns(rh);
3502 }
3503
3504 /******** END Record Resolver ***********/
3505
3506 static void
3507 finish_shorten (struct ResolverHandle *rh,
3508                 struct NameShortenHandle *nsh)
3509 {
3510   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3511               "Sending %s as shorten result\n", nsh->result);
3512   nsh->proc (nsh->proc_cls, nsh->result);
3513   GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3514   GNUNET_free (nsh);
3515   free_resolver_handle (rh);
3516 }
3517
3518
3519 /**
3520  * Callback calles by namestore for a zone to name
3521  * result
3522  *
3523  * @param cls the closure
3524  * @param zone_key the zone we queried
3525  * @param expire the expiration time of the name
3526  * @param name the name found or NULL
3527  * @param rd_len number of records for the name
3528  * @param rd the record data (PKEY) for the name
3529  * @param signature the signature for the record data
3530  */
3531 static void
3532 process_zone_to_name_shorten_root (void *cls,
3533                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3534                  struct GNUNET_TIME_Absolute expire,
3535                  const char *name,
3536                  unsigned int rd_len,
3537                  const struct GNUNET_NAMESTORE_RecordData *rd,
3538                  const struct GNUNET_CRYPTO_RsaSignature *signature);
3539
3540
3541 /**
3542  * Callback called by namestore for a zone to name
3543  * result
3544  *
3545  * @param cls the closure
3546  * @param zone_key the zone we queried
3547  * @param expire the expiration time of the name
3548  * @param name the name found or NULL
3549  * @param rd_len number of records for the name
3550  * @param rd the record data (PKEY) for the name
3551  * @param signature the signature for the record data
3552  */
3553 static void
3554 process_zone_to_name_shorten_shorten (void *cls,
3555                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3556                  struct GNUNET_TIME_Absolute expire,
3557                  const char *name,
3558                  unsigned int rd_len,
3559                  const struct GNUNET_NAMESTORE_RecordData *rd,
3560                  const struct GNUNET_CRYPTO_RsaSignature *signature)
3561 {
3562   struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3563   struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3564   struct AuthorityChain *next_authority;
3565
3566   char result[MAX_DNS_NAME_LENGTH];
3567   char tmp_name[MAX_DNS_NAME_LENGTH];
3568   size_t answer_len;
3569   
3570   rh->namestore_task = NULL;
3571   /* we found a match in our own root zone */
3572   if (rd_len != 0)
3573   {
3574     answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3575     memset(result, 0, answer_len);
3576
3577     if (strlen(rh->name) > 0)
3578     {
3579       sprintf (result, "%s.%s.%s.%s",
3580                rh->name, name,
3581                nsh->shorten_zone_name,
3582                GNUNET_GNS_TLD);
3583     }
3584     else
3585     {
3586       sprintf (result, "%s.%s.%s", name,
3587                nsh->shorten_zone_name,
3588                GNUNET_GNS_TLD);
3589     }
3590     
3591     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3592                "Found shorten result %s\n", result);
3593     if (strlen (nsh->result) > strlen (result))
3594       strcpy (nsh->result, result);
3595   }
3596   else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3597                                         nsh->shorten_zone) == 0)
3598   {
3599     /**
3600      * This is our zone append .gads unless name is empty
3601      * (it shouldn't be, usually FIXME what happens if we
3602      * shorten to our zone to a "" record??)
3603      */
3604     
3605     sprintf (result, "%s.%s.%s",
3606              rh->name,
3607              nsh->shorten_zone_name,
3608              GNUNET_GNS_TLD);
3609     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3610                "Our zone: Found %s as shorten result\n", result);
3611     
3612     if (strlen (nsh->result) > strlen (result))
3613       strcpy (nsh->result, result);
3614     //nsh->proc(nsh->proc_cls, result);
3615     //GNUNET_free(nsh);
3616     //free_resolver_handle(rh);
3617     //return;
3618   }
3619   
3620   
3621   /**
3622    * No PSEU found.
3623    * continue with next authority if exists
3624    */
3625   if ((rh->authority_chain_head->next == NULL))
3626   {
3627     finish_shorten (rh, nsh);
3628     return;
3629   }
3630   next_authority = rh->authority_chain_head;
3631   
3632   if (0 == strcmp (rh->name, ""))
3633     strcpy (tmp_name, next_authority->name);
3634   else
3635     GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3636                     "%s.%s", rh->name, next_authority->name);
3637   
3638   strcpy(rh->name, tmp_name);
3639   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3640              "No PSEU found for authority %s. Promoting back: %s\n",
3641              next_authority->name, rh->name);
3642   
3643   GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3644                             rh->authority_chain_tail,
3645                             next_authority);
3646
3647   GNUNET_free (next_authority);
3648
3649   rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3650                                  &rh->authority_chain_tail->zone,
3651                                  &rh->authority_chain_head->zone,
3652                                  &process_zone_to_name_shorten_root,
3653                                  rh);
3654 }
3655
3656 /**
3657  * Callback calles by namestore for a zone to name
3658  * result
3659  *
3660  * @param cls the closure
3661  * @param zone_key the zone we queried
3662  * @param expire the expiration time of the name
3663  * @param name the name found or NULL
3664  * @param rd_len number of records for the name
3665  * @param rd the record data (PKEY) for the name
3666  * @param signature the signature for the record data
3667  */
3668 static void
3669 process_zone_to_name_shorten_private (void *cls,
3670                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3671                  struct GNUNET_TIME_Absolute expire,
3672                  const char *name,
3673                  unsigned int rd_len,
3674                  const struct GNUNET_NAMESTORE_RecordData *rd,
3675                  const struct GNUNET_CRYPTO_RsaSignature *signature)
3676 {
3677   struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3678   struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3679   struct AuthorityChain *next_authority;
3680
3681   char result[MAX_DNS_NAME_LENGTH];
3682   char tmp_name[MAX_DNS_NAME_LENGTH];
3683   size_t answer_len;
3684   
3685   rh->namestore_task = NULL;
3686   /* we found a match in our own root zone */
3687   if (rd_len != 0)
3688   {
3689     answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3690     memset(result, 0, answer_len);
3691
3692     if (strlen(rh->name) > 0)
3693     {
3694       sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3695     }
3696     else
3697     {
3698       sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3699     }
3700     
3701     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3702                "Found shorten result %s\n", result);
3703     if (strlen (nsh->result) > strlen (result))
3704       strcpy (nsh->result, result);
3705   }
3706   else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3707                                         nsh->private_zone) == 0)
3708   {
3709     /**
3710      * This is our zone append .gads unless name is empty
3711      * (it shouldn't be, usually FIXME what happens if we
3712      * shorten to our zone to a "" record??)
3713      */
3714     
3715     sprintf (result, "%s.%s.%s",
3716              rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3717     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3718                "Our private zone: Found %s as shorten result %s\n", result);
3719     if (strlen (nsh->result) > strlen (result))
3720       strcpy (nsh->result, result);
3721   }
3722   
3723   if (0 != strcmp (nsh->shorten_zone_name, ""))
3724   {
3725     /* backtrack authorities for names in priv zone */
3726     rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3727                                    nsh->shorten_zone,
3728                                    &rh->authority_chain_head->zone,
3729                                    &process_zone_to_name_shorten_shorten,
3730                                    rh);
3731   }
3732   else
3733   {
3734     /**
3735      * No PSEU found.
3736      * continue with next authority if exists
3737      */
3738     if ((rh->authority_chain_head->next == NULL))
3739     {
3740       finish_shorten (rh, nsh);
3741       return;
3742     }
3743     next_authority = rh->authority_chain_head;
3744     
3745     if (0 == strcmp (rh->name, ""))
3746       strcpy (tmp_name, next_authority->name);
3747     else
3748       GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3749                       "%s.%s", rh->name, next_authority->name);
3750     
3751     strcpy(rh->name, tmp_name);
3752     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3753                "No PSEU found for authority %s. Promoting back: %s\n",
3754                next_authority->name, rh->name);
3755     
3756     GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3757                               rh->authority_chain_tail,
3758                               next_authority);
3759
3760     GNUNET_free (next_authority);
3761
3762     rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3763                                    &rh->authority_chain_tail->zone,
3764                                    &rh->authority_chain_head->zone,
3765                                    &process_zone_to_name_shorten_root,
3766                                    rh);
3767   }
3768 }
3769
3770 /**
3771  * Callback calles by namestore for a zone to name
3772  * result
3773  *
3774  * @param cls the closure
3775  * @param zone_key the zone we queried
3776  * @param expire the expiration time of the name
3777  * @param name the name found or NULL
3778  * @param rd_len number of records for the name
3779  * @param rd the record data (PKEY) for the name
3780  * @param signature the signature for the record data
3781  */
3782 static void
3783 process_zone_to_name_shorten_root (void *cls,
3784                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3785                  struct GNUNET_TIME_Absolute expire,
3786                  const char *name,
3787                  unsigned int rd_len,
3788                  const struct GNUNET_NAMESTORE_RecordData *rd,
3789                  const struct GNUNET_CRYPTO_RsaSignature *signature)
3790 {
3791   struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3792   struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3793   struct AuthorityChain *next_authority;
3794
3795   char result[MAX_DNS_NAME_LENGTH];
3796   char tmp_name[MAX_DNS_NAME_LENGTH];
3797   size_t answer_len;
3798   
3799   rh->namestore_task = NULL;
3800   /* we found a match in our own root zone */
3801   if (rd_len != 0)
3802   {
3803     answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3804     memset(result, 0, answer_len);
3805
3806     if (strlen(rh->name) > 0)
3807     {
3808       sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3809     }
3810     else
3811     {
3812       sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3813     }
3814     
3815     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3816                "Found shorten result %s\n", result);
3817     if (strlen (nsh->result) > strlen (result))
3818       strcpy (nsh->result, result);
3819   }
3820   else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3821                                         nsh->root_zone) == 0)
3822   {
3823     /**
3824      * This is our zone append .gads unless name is empty
3825      * (it shouldn't be, usually FIXME what happens if we
3826      * shorten to our zone to a "" record??)
3827      */
3828     
3829     sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3830     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3831                "Our zone: Found %s as shorten result\n", result);
3832     if (strlen (nsh->result) > strlen (result))
3833       strcpy (nsh->result, result);
3834   }
3835   
3836   if (NULL != nsh->private_zone)
3837   {
3838     /* backtrack authorities for names in priv zone */
3839     rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3840                                    nsh->private_zone,
3841                                    &rh->authority_chain_head->zone,
3842                                    &process_zone_to_name_shorten_private,
3843                                    rh);
3844   }
3845   else if (NULL != nsh->shorten_zone)
3846   {
3847     /* backtrack authorities for names in shorten zone */
3848     rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3849                                           nsh->shorten_zone,
3850                                           &rh->authority_chain_head->zone,
3851                                           &process_zone_to_name_shorten_shorten,
3852                                           rh);
3853   }
3854   else
3855   {
3856     /**
3857      * No PSEU found.
3858      * continue with next authority if exists
3859      */
3860     if ((rh->authority_chain_head->next == NULL))
3861     {
3862       finish_shorten (rh, nsh);
3863       return;
3864     }
3865     next_authority = rh->authority_chain_head;
3866     
3867     if (0 == strcmp (rh->name, ""))
3868       strcpy (tmp_name, next_authority->name);
3869     else
3870       GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3871                       "%s.%s", rh->name, next_authority->name);
3872     
3873     strcpy(rh->name, tmp_name);
3874     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3875                "No PSEU found for authority %s. Promoting back: %s\n",
3876                next_authority->name, rh->name);
3877     
3878     GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3879                               rh->authority_chain_tail,
3880                               next_authority);
3881
3882     GNUNET_free (next_authority);
3883
3884     rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3885                                    &rh->authority_chain_tail->zone,
3886                                    &rh->authority_chain_head->zone,
3887                                    &process_zone_to_name_shorten_root,
3888                                    rh);
3889   }
3890 }
3891
3892
3893 /**
3894  * Process result from namestore delegation lookup
3895  * for shorten operation
3896  *
3897  * @param cls the client shorten handle
3898  * @param rh the resolver handle
3899  * @param rd_count number of results (0)
3900  * @param rd data (NULL)
3901  */
3902 void
3903 handle_delegation_ns_shorten (void* cls,
3904                       struct ResolverHandle *rh,
3905                       uint32_t rd_count,
3906                       const struct GNUNET_NAMESTORE_RecordData *rd)
3907 {
3908   struct NameShortenHandle *nsh;
3909   char result[MAX_DNS_NAME_LENGTH];
3910
3911   nsh = (struct NameShortenHandle *)cls;
3912   rh->namestore_task = NULL;
3913   /**
3914    * At this point rh->name contains the part of the name
3915    * that we do not have a PKEY in our namestore to resolve.
3916    * The authority chain in the resolver handle is now
3917    * useful to backtrack if needed
3918    */
3919   
3920   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3921              "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3922   memset(result, 0, sizeof (result));
3923
3924   if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3925                                    nsh->root_zone))
3926   {
3927     /**
3928      * This is our zone append .gads unless name is empty
3929      * (it shouldn't be, usually FIXME what happens if we
3930      * shorten to our zone to a "" record??)
3931      */
3932     
3933     sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3934     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3935                "Our zone: Found %s as shorten result\n", result);
3936     
3937     if (strlen (nsh->result) > strlen (result))
3938       strcpy (nsh->result, result);
3939
3940   }
3941   else if (NULL != nsh->private_zone)
3942   {
3943     /**
3944      * This is our zone append .gads unless name is empty
3945      * (it shouldn't be, usually FIXME what happens if we
3946      * shorten to our zone to a "" record??)
3947      */
3948     if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3949                                      nsh->private_zone) == 0)
3950     {
3951     
3952       sprintf (result, "%s.%s.%s",
3953                rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3954       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3955                  "Our zone: Found %s as shorten result in private zone %s\n",
3956                  result);
3957     
3958       if (strlen (nsh->result) > strlen (result))
3959         strcpy (nsh->result, result);
3960     }
3961   }
3962   else if (NULL != nsh->shorten_zone)
3963   {
3964     /**
3965      * This is our zone append .gads unless name is empty
3966      * (it shouldn't be, usually FIXME what happens if we
3967      * shorten to our zone to a "" record??)
3968      */
3969     if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3970                                      nsh->shorten_zone) == 0)
3971     {
3972       sprintf (result, "%s.%s.%s",
3973                rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3974       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3975                  "Our zone: Found %s as shorten result in shorten zone\n",
3976                  result);
3977     
3978       if (strlen (nsh->result) > strlen (result))
3979         strcpy (nsh->result, result);
3980     }
3981   }
3982   
3983   
3984   /* backtrack authorities for names */
3985   rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3986                                  nsh->root_zone,
3987                                  &rh->authority_chain_head->zone,
3988                                  &process_zone_to_name_shorten_root,
3989                                  rh);
3990   
3991 }
3992
3993
3994 /**
3995  * Callback calles by namestore for a zone to name
3996  * result
3997  *
3998  * @param cls the closure
3999  * @param zone_key the zone we queried
4000  * @param expire the expiration time of the name
4001  * @param name the name found or NULL
4002  * @param rd_len number of records for the name
4003  * @param rd the record data (PKEY) for the name
4004  * @param signature the signature for the record data
4005  */
4006 static void
4007 process_zone_to_name_zkey(void *cls,
4008                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4009                  struct GNUNET_TIME_Absolute expire,
4010                  const char *name,
4011                  unsigned int rd_len,
4012                  const struct GNUNET_NAMESTORE_RecordData *rd,
4013                  const struct GNUNET_CRYPTO_RsaSignature *signature)
4014 {
4015   struct ResolverHandle *rh = cls;
4016   struct NameShortenHandle *nsh = rh->proc_cls;
4017   char new_name[MAX_DNS_NAME_LENGTH];
4018
4019   rh->namestore_task = NULL;
4020
4021   /* zkey not in our zone */
4022   if (name == NULL)
4023   {
4024     /**
4025      * In this case we have not given this PKEY a name (yet)
4026      * It is either just not in our zone or not even cached
4027      * Since we do not know at this point we will not try to shorten
4028      * because PKEY import will happen if the user follows the zkey
4029      * link.
4030      */
4031     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4032                "No name found for zkey %s returning verbatim!\n", nsh->result);
4033     /*if (strcmp(rh->name, "") != 0)
4034       GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4035                       rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4036     else
4037       GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4038                       enc, GNUNET_GNS_TLD_ZKEY);
4039
4040     strcpy (nsh->result, new_name);*/
4041
4042     finish_shorten (rh, nsh);
4043     return;
4044   }
4045   
4046   if (strcmp(rh->name, "") != 0)
4047     GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4048                     rh->name, name);
4049   else
4050     strcpy(new_name, name);
4051
4052   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4053              "Continue shorten for %s!\n", new_name);
4054
4055   strcpy(rh->name, new_name);
4056   
4057   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4058   rh->authority_chain_tail = rh->authority_chain_head;
4059   rh->authority_chain_head->zone = rh->authority;
4060   
4061   
4062   /* Start delegation resolution in our namestore */
4063   resolve_delegation_ns (rh);
4064 }
4065
4066
4067 /**
4068  * Shorten api from resolver
4069  *
4070  * @param zone the root zone to use
4071  * @param pzone the private zone to use
4072  * @param szone the shorten zone to use
4073  * @param name the name to shorten
4074  * @param private_zone_name name of the private zone
4075  * @param shorten_zone_name name of the shorten zone
4076  * @param proc the processor to call with result
4077  * @param proc_cls closure to pass to proc
4078  */
4079 void
4080 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4081                            struct GNUNET_CRYPTO_ShortHashCode *pzone,
4082                            struct GNUNET_CRYPTO_ShortHashCode *szone,
4083                            const char* name,
4084                            const char* private_zone_name,
4085                            const char* shorten_zone_name,
4086                            ShortenResultProcessor proc,
4087                            void* proc_cls)
4088 {
4089   struct ResolverHandle *rh;
4090   struct NameShortenHandle *nsh;
4091   char string_hash[MAX_DNS_LABEL_LENGTH];
4092   struct GNUNET_CRYPTO_ShortHashCode zkey;
4093   char nzkey[MAX_DNS_LABEL_LENGTH];
4094   char* nzkey_ptr = nzkey;
4095
4096
4097   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098               "Starting shorten for %s!\n", name);
4099   
4100   if (is_canonical ((char*)name) == GNUNET_YES)
4101   {
4102     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4103                 "%s is canonical. Returning verbatim\n", name);
4104     proc (proc_cls, name);
4105     return;
4106   }
4107
4108   nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4109
4110   nsh->proc = proc;
4111   nsh->proc_cls = proc_cls;
4112   nsh->root_zone = zone;
4113   nsh->private_zone = pzone;
4114   nsh->shorten_zone = szone;
4115   strcpy (nsh->private_zone_name, private_zone_name);
4116   strcpy (nsh->shorten_zone_name, shorten_zone_name);
4117   strcpy (nsh->result, name);
4118   
4119
4120   rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4121   rh->authority = *zone;
4122   rh->id = rid++;
4123   rh->priv_key = NULL;
4124   rh->namestore_task = NULL;
4125   rh->proc = &handle_delegation_ns_shorten;
4126   rh->proc_cls = nsh;
4127   rh->id = rid++;
4128   rh->private_local_zone = *zone;
4129
4130   GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4131   
4132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4133                 "Checking for TLD...\n");
4134   if (is_zkey_tld (name) == GNUNET_YES)
4135   {
4136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137                 "TLD is zkey\n");
4138     /**
4139      * This is a zkey tld
4140      * build hash and use as initial authority
4141      * FIXME sscanf
4142      */
4143     memset (rh->name, 0,
4144             strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4145     memcpy (rh->name, name,
4146             strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4147     pop_tld (rh->name, string_hash);
4148
4149     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150                 "ZKEY is %s!\n", string_hash);
4151     
4152     GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4153
4154     if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4155                                                            &zkey))
4156     {
4157       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4158                   "Cannot convert ZKEY %s to hash!\n", nzkey);
4159       GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4160       GNUNET_free (rh);
4161       GNUNET_free (nsh);
4162       proc (proc_cls, name);
4163       return;
4164     }
4165
4166     rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4167                                    zone, //ours
4168                                    &zkey,
4169                                    &process_zone_to_name_zkey,
4170                                    rh);
4171     return;
4172
4173   }
4174   else if (is_gnunet_tld (name) == GNUNET_YES)
4175   {
4176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4177                 "TLD is gnunet\n");
4178     /**
4179      * Presumably GNUNET tld
4180      */
4181     memset (rh->name, 0,
4182             strlen (name)-strlen (GNUNET_GNS_TLD));
4183     memcpy (rh->name, name,
4184             strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4185   }
4186   else
4187   {
4188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4189     GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4190     GNUNET_free (rh);
4191     GNUNET_free (nsh);
4192     proc (proc_cls, name);
4193     return;
4194   }
4195
4196   rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4197   rh->authority_chain_tail = rh->authority_chain_head;
4198   rh->authority_chain_head->zone = *zone;
4199   
4200   
4201   /* Start delegation resolution in our namestore */
4202   resolve_delegation_ns (rh);
4203 }
4204
4205 /*********** END NAME SHORTEN ********************/
4206
4207 /**
4208  * Conclude get authority lookup
4209  *
4210  * @param rh resolver handle
4211  * @param nah get authority lookup handle
4212  */
4213 static void
4214 finish_get_auth (struct ResolverHandle *rh,
4215                  struct GetNameAuthorityHandle *nah)
4216 {
4217   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4218              "Got authority result %s\n", nah->result);
4219   
4220   nah->proc (nah->proc_cls, nah->result);
4221   GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4222   GNUNET_free (nah);
4223   free_resolver_handle (rh);
4224 }
4225
4226 /**
4227  * Process result from namestore delegation lookup
4228  * for get authority operation
4229  *
4230  * @param cls the client get auth handle
4231  * @param rh the resolver handle
4232  * @param rd_count number of results (0)
4233  * @param rd data (NULL)
4234  */
4235 void
4236 handle_delegation_result_ns_get_auth(void* cls,
4237                       struct ResolverHandle *rh,
4238                       uint32_t rd_count,
4239                       const struct GNUNET_NAMESTORE_RecordData *rd)
4240 {
4241   struct GetNameAuthorityHandle* nah;
4242   size_t answer_len;
4243
4244   nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4245   
4246   /**
4247    * At this point rh->name contains the part of the name
4248    * that we do not have a PKEY in our namestore to resolve.
4249    * The authority chain in the resolver handle is now
4250    * useful to backtrack if needed
4251    */
4252   
4253   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4254              "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4255
4256   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4257              "Building response!\n");
4258   if (is_canonical (rh->name) == GNUNET_YES)
4259   {
4260     /**
4261      * We successfully resolved the authority in the ns
4262      * FIXME for our purposes this is fine
4263      * but maybe we want to have an api that also looks
4264      * into the dht (i.e. option in message)
4265      **/
4266     if (strlen(rh->name) > strlen(nah->name))
4267     {
4268       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4269                  "Record name longer than original lookup name... odd!\n");
4270       //FIXME to sth here
4271     }
4272
4273     answer_len = strlen(nah->name) - strlen(rh->name)
4274       + strlen(GNUNET_GNS_TLD) + 1;
4275     memset(nah->result, 0, answer_len);
4276     if (0 != strcmp (rh->name, ""))
4277       strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4278     else
4279       strcpy(nah->result, nah->name);
4280
4281     finish_get_auth (rh, nah);
4282   }
4283   else
4284   {
4285     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4286                "Unable to resolve authority for remaining %s!\n", rh->name);
4287     strcpy(nah->result, "");
4288     finish_get_auth (rh, nah);
4289   }
4290
4291
4292 }
4293
4294
4295 /**
4296  * Tries to resolve the authority for name
4297  * in our namestore
4298  *
4299  * @param zone the root zone to look up for
4300  * @param pzone the private local zone
4301  * @param name the name to lookup up
4302  * @param proc the processor to call when finished
4303  * @param proc_cls the closure to pass to the processor
4304  */
4305 void
4306 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4307                            struct GNUNET_CRYPTO_ShortHashCode pzone,
4308                            const char* name,
4309                            GetAuthorityResultProcessor proc,
4310                            void* proc_cls)
4311 {
4312   struct ResolverHandle *rh;
4313   struct GetNameAuthorityHandle *nah;
4314
4315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4316               "Starting authority resolution for %s!\n", name);
4317
4318   nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4319   rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4320   rh->authority = zone;
4321   rh->id = rid++;
4322   rh->private_local_zone = pzone;
4323   rh->namestore_task = NULL;
4324
4325   GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4326   
4327   if (strcmp(GNUNET_GNS_TLD, name) == 0)
4328   {
4329     strcpy(rh->name, "\0");
4330   }
4331   else
4332   {
4333     memset(rh->name, 0,
4334            strlen(name)-strlen(GNUNET_GNS_TLD));
4335     memcpy(rh->name, name,
4336            strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4337   }
4338
4339   memset(nah->name, 0,
4340          strlen(name)+1);
4341   strcpy(nah->name, name);
4342   
4343   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4344   rh->authority_chain_tail = rh->authority_chain_head;
4345   rh->authority_chain_head->zone = zone;
4346   rh->proc = &handle_delegation_result_ns_get_auth;
4347   rh->proc_cls = (void*)nah;
4348
4349   nah->proc = proc;
4350   nah->proc_cls = proc_cls;
4351   strcpy (nah->result, "");
4352
4353   /* Start delegation resolution in our namestore */
4354   resolve_delegation_ns(rh);
4355
4356 }
4357
4358 /******** END GET AUTHORITY *************/
4359
4360 /* end of gnunet-service-gns_resolver.c */