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