-add bg lookups for shorten
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  *
23  *
24  * @file gns/gnunet-service-gns_resolver.c
25  * @brief GNUnet GNS resolver logic
26  * @author Martin Schanzenbach
27  */
28 #include "platform.h"
29 #include <unicase.h>
30 #include "gnunet_util_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_dns_service.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_namestore_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_gns_service.h"
38 #include "block_gns.h"
39 #include "gns.h"
40 #include "gnunet-service-gns_resolver.h"
41
42 #define DHT_OPERATION_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
46
47
48 /**
49  * Our handle to the namestore service
50  */
51 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
52
53 /**
54  * Resolver handle to the dht
55  */
56 static struct GNUNET_DHT_Handle *dht_handle;
57
58 /**
59  * Heap for parallel DHT lookups
60  */
61 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
62
63 /**
64  * Maximum amount of parallel queries in background
65  */
66 static unsigned long long max_allowed_background_queries;
67
68 /**
69  * Our local zone
70  */
71 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
72
73 /**
74  * Namestore calls this function if we have record for this name.
75  * (or with rd_count=0 to indicate no matches)
76  *
77  * @param cls the pending query
78  * @param key the key of the zone we did the lookup
79  * @param expiration expiration date of the namestore entry
80  * @param name the name for which we need an authority
81  * @param rd_count the number of records with 'name'
82  * @param rd the record data
83  * @param signature the signature of the authority for the record data
84  */
85 static void
86 process_pseu_lookup_ns(void* cls,
87                       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
88                       struct GNUNET_TIME_Absolute expiration,
89                       const char *name, unsigned int rd_count,
90                       const struct GNUNET_NAMESTORE_RecordData *rd,
91                       const struct GNUNET_CRYPTO_RsaSignature *signature)
92 {
93   struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
94   struct GNUNET_NAMESTORE_RecordData new_pkey;
95
96   if (rd_count > 0)
97   {
98     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
99                "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
100     if (0 == strcmp(gph->name, name))
101     {
102       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
103                  "GNS_AUTO_PSEU: Intelligent replacement not implemented\n",
104                  name);
105       GNUNET_free(gph);
106       return;
107     }
108
109     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
110                "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
111     memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
112     GNUNET_NAMESTORE_lookup_record(namestore_handle,
113                                    &gph->zone,
114                                    gph->new_name,
115                                    GNUNET_GNS_RECORD_PSEU,
116                                    &process_pseu_lookup_ns,
117                                    gph);
118     return;
119   }
120
121   /** name is free */
122   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
123              "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->new_name);
124
125   new_pkey.expiration = GNUNET_TIME_absolute_get_forever ();
126   new_pkey.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
127   new_pkey.data = &gph->new_zone;
128   new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
129   GNUNET_NAMESTORE_record_create (namestore_handle,
130                                   gph->key,
131                                   gph->new_name,
132                                   &new_pkey,
133                                   NULL, //cont
134                                   NULL); //cls
135   GNUNET_free(gph);
136
137 }
138
139 /**
140  * process result of a dht pseu lookup
141  *
142  * @param gph the handle
143  * @param name the pseu result or NULL
144  */
145 static void
146 process_pseu_result(struct GetPseuAuthorityHandle* gph, char* name)
147 {
148   if (NULL == name)
149   {
150     memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
151   }
152   else
153   {
154     memcpy(gph->new_name, name, strlen(name)+1);
155   }
156
157   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
158              "GNS_AUTO_PSEU: Checking %s for collision in NS\n", gph->new_name);
159
160   /**
161    * Check for collision
162    */
163   GNUNET_NAMESTORE_lookup_record(namestore_handle,
164                                  &gph->zone,
165                                  gph->new_name,
166                                  GNUNET_GNS_RECORD_PSEU,
167                                  &process_pseu_lookup_ns,
168                                  gph);
169 }
170
171 /**
172  * Handle timeout for dht request
173  *
174  * @param cls the request handle as closure
175  * @param tc the task context
176  */
177 static void
178 handle_auth_discovery_timeout(void *cls,
179                               const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
182
183   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
184              "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
185   GNUNET_DHT_get_stop (gph->get_handle);
186   gph->get_handle = NULL;
187   process_pseu_result(gph, NULL);
188 }
189
190 /**
191  * Function called when we find a PSEU entry in the DHT
192  *
193  * @param cls the request handle
194  * @param exp lifetime
195  * @param key the key the record was stored under
196  * @param get_path get path
197  * @param get_path_length get path length
198  * @param put_path put path
199  * @param put_path_length put path length
200  * @param type the block type
201  * @param size the size of the record
202  * @param data the record data
203  */
204 static void
205 process_auth_discovery_dht_result(void* cls,
206                                   struct GNUNET_TIME_Absolute exp,
207                                   const GNUNET_HashCode * key,
208                                   const struct GNUNET_PeerIdentity *get_path,
209                                   unsigned int get_path_length,
210                                   const struct GNUNET_PeerIdentity *put_path,
211                                   unsigned int put_path_length,
212                                   enum GNUNET_BLOCK_Type type,
213                                   size_t size, const void *data)
214 {
215   struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
216   struct GNSNameRecordBlock *nrb;
217   char* rd_data = (char*)data;
218   char* name;
219   int num_records;
220   size_t rd_size;
221   int i;
222
223   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
224              "GNS_GET_AUTH: got dht result (size=%d)\n", size);
225
226   if (data == NULL)
227   {
228     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
229                "GNS_GET_AUTH: got dht result null!\n", size);
230     GNUNET_break(0);
231     GNUNET_free(gph);
232     return;
233   }
234   
235   nrb = (struct GNSNameRecordBlock*)data;
236
237   /* stop lookup and timeout task */
238   GNUNET_DHT_get_stop (gph->get_handle);
239   gph->get_handle = NULL;
240   GNUNET_SCHEDULER_cancel(gph->timeout);
241
242   gph->get_handle = NULL;
243
244   nrb = (struct GNSNameRecordBlock*)data;
245   
246   name = (char*)&nrb[1];
247   num_records = ntohl(nrb->rd_count);
248   {
249     struct GNUNET_NAMESTORE_RecordData rd[num_records];
250
251     rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
252     rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
253
254     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
255                                                                rd_data,
256                                                                num_records,
257                                                                rd))
258     {
259       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
260                  "GNS_GET_AUTH: Error deserializing data!\n");
261       GNUNET_break(0);
262       GNUNET_free(gph);
263       return;
264     }
265
266     for (i=0; i<num_records; i++)
267     {
268       if ((strcmp(name, "+") == 0) &&
269           (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
270       {
271         /* found pseu */
272         process_pseu_result(gph, (char*)rd[i].data);
273         return;
274       }
275     }
276   }
277
278   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
279   process_pseu_result(gph, NULL);
280 }
281
282 /**
283  * Callback called by namestore for a zone to name
284  * result
285  *
286  * @param cls the closure
287  * @param zone_key the zone we queried
288  * @param expire the expiration time of the name
289  * @param name the name found or NULL
290  * @param rd_len number of records for the name
291  * @param rd the record data (PKEY) for the name
292  * @param signature the signature for the record data
293  */
294 static void
295 process_zone_to_name_discover(void *cls,
296                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
297                  struct GNUNET_TIME_Absolute expire,
298                  const char *name,
299                  unsigned int rd_len,
300                  const struct GNUNET_NAMESTORE_RecordData *rd,
301                  const struct GNUNET_CRYPTO_RsaSignature *signature)
302 {
303   struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
304
305   /* we found a match in our own zone */
306   if (rd_len != 0)
307   {
308     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
309                "GNS_AUTO_PSEU: name for zone in our root %d\n", strlen(name));
310     GNUNET_free(gph);
311   }
312   else
313   {
314     /**
315      * No name found.
316      * check dht
317      */
318     uint32_t xquery;
319     struct GNUNET_CRYPTO_ShortHashCode name_hash;
320     GNUNET_HashCode lookup_key;
321     struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
322     GNUNET_HashCode name_hash_double;
323     GNUNET_HashCode zone_hash_double;
324
325     GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
326     GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
327     GNUNET_CRYPTO_short_hash_double (&gph->new_zone, &zone_hash_double);
328     GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
329     GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
330
331     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
332                "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
333                "+", (char*)&lookup_key_string);
334
335     gph->timeout = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
336                                          &handle_auth_discovery_timeout, gph);
337
338     xquery = htonl(GNUNET_GNS_RECORD_PSEU);
339     
340     GNUNET_assert(gph->get_handle == NULL);
341     gph->get_handle = GNUNET_DHT_get_start(dht_handle,
342                                            GNUNET_TIME_UNIT_FOREVER_REL,
343                                            GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
344                                            &lookup_key,
345                                            DHT_GNS_REPLICATION_LEVEL,
346                                            GNUNET_DHT_RO_NONE,
347                                            &xquery,
348                                            sizeof(xquery),
349                                            &process_auth_discovery_dht_result,
350                                            gph);
351
352   }
353 }
354
355
356 /**
357  * Callback for new authories
358  *
359  * @param name the name given by delegation
360  * @param zone the authority
361  * @param our_zone our local zone
362  * @param key the private key of our authority
363  */
364 static void process_discovered_authority(char* name,
365                                     struct GNUNET_CRYPTO_ShortHashCode zone,
366                                     struct GNUNET_CRYPTO_ShortHashCode our_zone,
367                                     struct GNUNET_CRYPTO_RsaPrivateKey *key)
368 {
369   struct GetPseuAuthorityHandle *gph;
370   size_t namelen;
371
372   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
373              "GNS_AUTO_PSEU: New authority %s discovered\n",
374              name);
375
376   gph = GNUNET_malloc(sizeof(struct GetPseuAuthorityHandle));
377   namelen = strlen(name) + 1;
378   memcpy(gph->name, name, namelen);
379   
380   gph->new_zone = zone;
381   gph->zone = our_zone;
382   gph->key = key;
383
384   GNUNET_NAMESTORE_zone_to_name (namestore_handle,
385                                  &our_zone,
386                                  &gph->new_zone,
387                                  &process_zone_to_name_discover,
388                                  gph);
389
390 }
391
392 /**
393  * Initialize the resolver
394  *
395  * @param nh the namestore handle
396  * @param dh the dht handle
397  * @param lz the local zone's hash
398  * @param max_bg_queries maximum number of parallel background queries in dht
399  * @return GNUNET_OK on success
400  */
401 int
402 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
403                   struct GNUNET_DHT_Handle *dh,
404                   struct GNUNET_CRYPTO_ShortHashCode lz,
405                   unsigned long long max_bg_queries)
406 {
407   namestore_handle = nh;
408   dht_handle = dh;
409   local_zone = lz;
410   dht_lookup_heap =
411     GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
412   max_allowed_background_queries = max_bg_queries;
413
414   if ((namestore_handle != NULL) && (dht_handle != NULL))
415   {
416     return GNUNET_OK;
417   }
418   return GNUNET_SYSERR;
419 }
420
421 /**
422  * Cleanup background lookups
423  *
424  * @param cls closure to iterator
425  * @param node heap nodes
426  * @param element the resolver handle
427  * @param cost heap cost
428  * @return always GNUNET_YES
429  */
430 static int
431 cleanup_pending_background_queries(void* cls,
432                                    struct GNUNET_CONTAINER_HeapNode *node,
433                                    void *element,
434                                    GNUNET_CONTAINER_HeapCostType cost)
435 {
436   struct ResolverHandle *rh = (struct ResolverHandle *)element;
437   ResolverCleanupContinuation cont = cls;
438   
439   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
440              "GNS_CLEANUP: Terminating background lookup for %s\n",
441              rh->name);
442   GNUNET_DHT_get_stop(rh->get_handle);
443   rh->get_handle = NULL;
444   rh->proc(rh->proc_cls, rh, 0, NULL);
445
446   GNUNET_CONTAINER_heap_remove_node(node);
447
448   if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
449     cont();
450
451
452   return GNUNET_YES;
453 }
454
455
456 /**
457  * Shutdown resolver
458  */
459 void
460 gns_resolver_cleanup(ResolverCleanupContinuation cont)
461 {
462   unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
463   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
464              "GNS_CLEANUP: %d pending background queries to terminate\n", s);
465
466   if (0 != s)
467     GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
468                                    &cleanup_pending_background_queries,
469                                    cont);
470   else
471     cont();
472 }
473
474
475 /**
476  * Helper function to free resolver handle
477  *
478  * @param rh the handle to free
479  */
480 static void
481 free_resolver_handle(struct ResolverHandle* rh)
482 {
483   struct AuthorityChain *ac;
484   struct AuthorityChain *ac_next;
485
486   if (NULL == rh)
487     return;
488
489   ac = rh->authority_chain_head;
490
491   while (NULL != ac)
492   {
493     ac_next = ac->next;
494     GNUNET_free(ac);
495     ac = ac_next;
496   }
497   GNUNET_free(rh);
498 }
499
500
501 /**
502  * Callback when record data is put into namestore
503  *
504  * @param cls the closure
505  * @param success GNUNET_OK on success
506  * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
507  */
508 void
509 on_namestore_record_put_result(void *cls,
510                                int32_t success,
511                                const char *emsg)
512 {
513   if (GNUNET_NO == success)
514   {
515     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
516                "GNS_NS: records already in namestore\n");
517     return;
518   }
519   else if (GNUNET_YES == success)
520   {
521     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
522                "GNS_NS: records successfully put in namestore\n");
523     return;
524   }
525
526   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
527              "GNS_NS: Error putting records into namestore: %s\n", emsg);
528 }
529
530 static void
531 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 {
533   struct ResolverHandle *rh = cls;
534
535   if (rh->timeout_cont)
536     rh->timeout_cont(rh->timeout_cont_cls, tc);
537 }
538
539 /**
540  * Processor for background lookups in the DHT
541  *
542  * @param cls closure (NULL)
543  * @param rd_count number of records found (not 0)
544  * @param rd record data
545  */
546 static void
547 background_lookup_result_processor(void *cls,
548                                    uint32_t rd_count,
549                                    const struct GNUNET_NAMESTORE_RecordData *rd)
550 {
551   //We could do sth verbose/more useful here but it doesn't make any difference
552   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
553              "GNS_BG: background dht lookup for finished.\n");
554 }
555
556 /**
557  * Handle timeout for DHT requests
558  *
559  * @param cls the request handle as closure
560  * @param tc the task context
561  */
562 static void
563 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
564 {
565   struct ResolverHandle *rh = cls;
566   struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
567   char new_name[MAX_DNS_NAME_LENGTH];
568
569   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
570              "GNS_PHASE_REC: dht lookup for query %s timed out.\n",
571              rh->name);
572   /**
573    * Start resolution in bg
574    */
575   //strcpy(new_name, rh->name);
576   //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
577   GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
578                   rh->name, GNUNET_GNS_TLD);
579
580   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
581              "GNS_PHASE_REC: Starting background lookup for %s type %d\n",
582              new_name, rlh->record_type);
583
584   gns_resolver_lookup_record(rh->authority,
585                              rlh->record_type,
586                              new_name,
587                              rh->priv_key,
588                              GNUNET_TIME_UNIT_FOREVER_REL,
589                              &background_lookup_result_processor,
590                              NULL);
591   rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
592   
593   GNUNET_DHT_get_stop (rh->get_handle);
594   rh->get_handle = NULL;
595   rh->proc(rh->proc_cls, rh, 0, NULL);
596 }
597
598
599 /**
600  * Function called when we get a result from the dht
601  * for our record query
602  *
603  * @param cls the request handle
604  * @param exp lifetime
605  * @param key the key the record was stored under
606  * @param get_path get path
607  * @param get_path_length get path length
608  * @param put_path put path
609  * @param put_path_length put path length
610  * @param type the block type
611  * @param size the size of the record
612  * @param data the record data
613  */
614 static void
615 process_record_result_dht(void* cls,
616                  struct GNUNET_TIME_Absolute exp,
617                  const GNUNET_HashCode * key,
618                  const struct GNUNET_PeerIdentity *get_path,
619                  unsigned int get_path_length,
620                  const struct GNUNET_PeerIdentity *put_path,
621                  unsigned int put_path_length,
622                  enum GNUNET_BLOCK_Type type,
623                  size_t size, const void *data)
624 {
625   struct ResolverHandle *rh;
626   struct RecordLookupHandle *rlh;
627   struct GNSNameRecordBlock *nrb;
628   uint32_t num_records;
629   char* name = NULL;
630   char* rd_data = (char*)data;
631   int i;
632   int rd_size;
633   
634   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
635              "GNS_PHASE_REC: got dht result (size=%d)\n", size);
636   
637   if (data == NULL)
638     return;
639
640   //FIXME maybe check expiration here, check block type
641   
642   rh = (struct ResolverHandle *)cls;
643   rlh = (struct RecordLookupHandle *) rh->proc_cls;
644   nrb = (struct GNSNameRecordBlock*)data;
645   
646   /* stop lookup and timeout task */
647   GNUNET_DHT_get_stop (rh->get_handle);
648   rh->get_handle = NULL;
649   
650   if (rh->dht_heap_node != NULL)
651   {
652     GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
653     rh->dht_heap_node = NULL;
654   }
655   
656   if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
657   {
658     GNUNET_SCHEDULER_cancel(rh->timeout_task);
659     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
660   }
661
662   rh->get_handle = NULL;
663   name = (char*)&nrb[1];
664   num_records = ntohl(nrb->rd_count);
665   {
666     struct GNUNET_NAMESTORE_RecordData rd[num_records];
667
668     rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
669     rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
670   
671     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
672                                                                rd_data,
673                                                                num_records,
674                                                                rd))
675     {
676       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
677       return;
678     }
679
680     for (i=0; i<num_records; i++)
681     {
682       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
683                "GNS_PHASE_REC: Got name: %s (wanted %s)\n", name, rh->name);
684       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
685                "GNS_PHASE_REC: Got type: %d\n",
686                rd[i].record_type);
687       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
688                "GNS_PHASE_REC: Got data length: %d\n", rd[i].data_size);
689       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
690                "GNS_PHASE_REC: Got flag %d\n", rd[i].flags);
691     
692      if ((strcmp(name, rh->name) == 0) &&
693          (rd[i].record_type == rlh->record_type))
694       {
695         rh->answered++;
696       }
697
698     }
699
700     /**
701      * FIXME check pubkey against existing key in namestore?
702      * https://gnunet.org/bugs/view.php?id=2179
703      */
704
705     /* Save to namestore */
706     GNUNET_NAMESTORE_record_put (namestore_handle,
707                                  &nrb->public_key,
708                                  name,
709                                  exp,
710                                  num_records,
711                                  rd,
712                                  &nrb->signature,
713                                  &on_namestore_record_put_result, //cont
714                                  NULL); //cls
715
716   
717     if (rh->answered)
718       rh->proc(rh->proc_cls, rh, num_records, rd);
719     else
720       rh->proc(rh->proc_cls, rh, 0, NULL);
721   }
722
723 }
724
725
726 /**
727  * Start DHT lookup for a (name -> query->record_type) record in
728  * rh->authority's zone
729  *
730  * @param rh the pending gns query context
731  */
732 static void
733 resolve_record_dht(struct ResolverHandle *rh)
734 {
735   uint32_t xquery;
736   struct GNUNET_CRYPTO_ShortHashCode name_hash;
737   GNUNET_HashCode lookup_key;
738   GNUNET_HashCode name_hash_double;
739   GNUNET_HashCode zone_hash_double;
740   struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
741   struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
742   struct ResolverHandle *rh_heap_root;
743   
744   GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
745   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
746   GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
747   GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
748   GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
749   
750   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
751              "GNS_PHASE_REC: starting dht lookup for %s with key: %s\n",
752              rh->name, (char*)&lookup_key_string);
753
754   //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
755   rh->dht_heap_node = NULL;
756
757   if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
758   {
759     /**
760      * Update timeout if necessary
761      */
762     if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
763     {
764
765     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
766                "GNS_PHASE_REC: Adjusting timeout\n");
767     /*
768      * Set timeout for authority lookup phase to 1/2
769      */
770       rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
771                                 GNUNET_TIME_relative_divide(rh->timeout, 2),
772                                                 &handle_lookup_timeout,
773                                                 rh);
774     }
775     //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
776     //                                                   &dht_lookup_timeout,
777     //                                                   rh);
778     rh->timeout_cont = &dht_lookup_timeout;
779     rh->timeout_cont_cls = rh;
780   }
781   else 
782   {
783     if (max_allowed_background_queries <=
784         GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
785     {
786       rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
787       GNUNET_DHT_get_stop(rh_heap_root->get_handle);
788       rh_heap_root->get_handle = NULL;
789       rh_heap_root->dht_heap_node = NULL;
790       
791       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
792            "GNS_PHASE_REC: Replacing oldest background query for %s\n",
793                  rh_heap_root->name);
794       rh_heap_root->proc(rh_heap_root->proc_cls,
795                          rh_heap_root,
796                          0,
797                          NULL);
798     }
799     rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
800                                          rh,
801                                          GNUNET_TIME_absolute_get().abs_value);
802   }
803   
804   xquery = htonl(rlh->record_type);
805   
806   GNUNET_assert(rh->get_handle == NULL);
807   rh->get_handle = GNUNET_DHT_get_start(dht_handle, 
808                        GNUNET_TIME_UNIT_FOREVER_REL,
809                        GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
810                        &lookup_key,
811                        DHT_GNS_REPLICATION_LEVEL,
812                        GNUNET_DHT_RO_NONE,
813                        &xquery, 
814                        sizeof(xquery),
815                        &process_record_result_dht,
816                        rh);
817
818 }
819
820
821 /**
822  * Namestore calls this function if we have record for this name.
823  * (or with rd_count=0 to indicate no matches)
824  *
825  * @param cls the pending query
826  * @param key the key of the zone we did the lookup
827  * @param expiration expiration date of the namestore entry
828  * @param name the name for which we need an authority
829  * @param rd_count the number of records with 'name'
830  * @param rd the record data
831  * @param signature the signature of the authority for the record data
832  */
833 static void
834 process_record_result_ns(void* cls,
835                   const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
836                   struct GNUNET_TIME_Absolute expiration,
837                   const char *name, unsigned int rd_count,
838                   const struct GNUNET_NAMESTORE_RecordData *rd,
839                   const struct GNUNET_CRYPTO_RsaSignature *signature)
840 {
841   struct ResolverHandle *rh;
842   struct RecordLookupHandle *rlh;
843   struct GNUNET_TIME_Relative remaining_time;
844   struct GNUNET_CRYPTO_ShortHashCode zone;
845
846   rh = (struct ResolverHandle *) cls;
847   rlh = (struct RecordLookupHandle *)rh->proc_cls;
848   GNUNET_CRYPTO_short_hash(key,
849                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
850                      &zone);
851   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
852   
853   
854
855   rh->status = 0;
856   
857   if (name != NULL)
858   {
859     rh->status |= EXISTS;
860   }
861   
862   if (remaining_time.rel_value == 0)
863   {
864     rh->status |= EXPIRED;
865   }
866   
867   if (rd_count == 0)
868   {
869     /**
870      * Lookup terminated and no results
871      */
872     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
873          "GNS_PHASE_REC: Namestore lookup for %s terminated without results\n",
874          name);
875
876     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
877                "GNS_PHASE_REC: Record %s unknown in namestore\n",
878                rh->name);
879     /**
880      * Our zone and no result? Cannot resolve TT
881      */
882     rh->proc(rh->proc_cls, rh, 0, NULL);
883     return;
884
885   }
886   else
887   {
888     
889     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
890               "GNS_PHASE_REC: Processing additional result %s from namestore\n",
891               name);
892     int i;
893     for (i=0; i<rd_count;i++)
894     {
895
896       if (rd[i].record_type != rlh->record_type)
897         continue;
898       
899       if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
900           == 0)
901       {
902         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
903                    "GNS_PHASE_REC: This record is expired. Skipping\n");
904         continue;
905       }
906       
907       rh->answered++;
908       
909     }
910     
911     /**
912      * no answers found
913      */
914     if (rh->answered == 0)
915     {
916       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
917                  "GNS_PHASE_REC: No answers found. This is odd!\n");
918       rh->proc(rh->proc_cls, rh, 0, NULL);
919       return;
920     }
921     
922     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
923                "GNS_PHASE_REC: Found %d answer(s) to query in %d records!\n",
924                rh->answered, rd_count);
925
926     rh->proc(rh->proc_cls, rh, rd_count, rd);
927   }
928 }
929
930
931 /**
932  * The final phase of resolution.
933  * rh->name is a name that is canonical and we do not have a delegation.
934  * Query namestore for this record
935  *
936  * @param rh the pending lookup
937  */
938 static void
939 resolve_record_ns(struct ResolverHandle *rh)
940 {
941   struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
942   
943   /* We cancel here as to not include the ns lookup in the timeout */
944   if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
945   {
946     GNUNET_SCHEDULER_cancel(rh->timeout_task);
947     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
948   }
949   
950   /**
951    * Try to resolve this record in our namestore.
952    * The name to resolve is now in rh->authority_name
953    * since we tried to resolve it to an authority
954    * and failed.
955    **/
956   GNUNET_NAMESTORE_lookup_record(namestore_handle,
957                                  &rh->authority,
958                                  rh->name,
959                                  rlh->record_type,
960                                  &process_record_result_ns,
961                                  rh);
962 }
963
964
965
966 /**
967  * Handle timeout for DHT requests
968  *
969  * @param cls the request handle as closure
970  * @param tc the task context
971  */
972 static void
973 dht_authority_lookup_timeout(void *cls,
974                              const struct GNUNET_SCHEDULER_TaskContext *tc)
975 {
976   struct ResolverHandle *rh = cls;
977   struct RecordLookupHandle *rlh = rh->proc_cls;
978   char new_name[MAX_DNS_NAME_LENGTH];
979
980   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
981              "GNS_PHASE_DELEGATE: dht lookup for query %s timed out.\n",
982              rh->authority_name);
983
984   rh->status |= TIMED_OUT;
985
986   rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
987   
988   GNUNET_DHT_get_stop (rh->get_handle);
989   rh->get_handle = NULL;
990   
991   if (strcmp(rh->name, "") == 0)
992   {
993     /*
994      * promote authority back to name and try to resolve record
995      */
996     strcpy(rh->name, rh->authority_name);
997     rh->proc(rh->proc_cls, rh, 0, NULL);
998     return;
999   }
1000   
1001   /**
1002    * Start resolution in bg
1003    */
1004   GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1005                   "%s.%s", rh->name, GNUNET_GNS_TLD);
1006   //strcpy(new_name, rh->name);
1007   //strcpy(new_name+strlen(new_name), ".");
1008   //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1009   
1010   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1011              "GNS_PHASE_DELEGATE: Starting background query for %s type %d\n",
1012              new_name, rlh->record_type);
1013
1014   gns_resolver_lookup_record(rh->authority,
1015                              rlh->record_type,
1016                              new_name,
1017                              rh->priv_key,
1018                              GNUNET_TIME_UNIT_FOREVER_REL,
1019                              &background_lookup_result_processor,
1020                              NULL);
1021
1022   rh->proc(rh->proc_cls, rh, 0, NULL);
1023 }
1024
1025 /* Prototype */
1026 static void resolve_delegation_dht(struct ResolverHandle *rh);
1027
1028 /* Prototype */
1029 static void resolve_delegation_ns(struct ResolverHandle *rh);
1030
1031 /**
1032  * Function called when we get a result from the dht
1033  * for our query. Recursively tries to resolve authorities
1034  * for name in DHT.
1035  *
1036  * @param cls the request handle
1037  * @param exp lifetime
1038  * @param key the key the record was stored under
1039  * @param get_path get path
1040  * @param get_path_length get path length
1041  * @param put_path put path
1042  * @param put_path_length put path length
1043  * @param type the block type
1044  * @param size the size of the record
1045  * @param data the record data
1046  */
1047 static void
1048 process_delegation_result_dht(void* cls,
1049                  struct GNUNET_TIME_Absolute exp,
1050                  const GNUNET_HashCode * key,
1051                  const struct GNUNET_PeerIdentity *get_path,
1052                  unsigned int get_path_length,
1053                  const struct GNUNET_PeerIdentity *put_path,
1054                  unsigned int put_path_length,
1055                  enum GNUNET_BLOCK_Type type,
1056                  size_t size, const void *data)
1057 {
1058   struct ResolverHandle *rh;
1059   struct GNSNameRecordBlock *nrb;
1060   uint32_t num_records;
1061   char* name = NULL;
1062   char* rd_data = (char*) data;
1063   int i;
1064   int rd_size;
1065   struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1066   GNUNET_HashCode zone_hash_double, name_hash_double;
1067   
1068   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GNS_PHASE_DELEGATE: Got DHT result\n");
1069
1070   if (data == NULL)
1071     return;
1072   
1073   //FIXME check expiration?
1074   
1075   rh = (struct ResolverHandle *)cls;
1076   nrb = (struct GNSNameRecordBlock*)data;
1077   
1078   /* stop dht lookup and timeout task */
1079   GNUNET_DHT_get_stop (rh->get_handle);
1080
1081   rh->get_handle = NULL;
1082
1083   if (rh->dht_heap_node != NULL)
1084   {
1085     GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1086     rh->dht_heap_node = NULL;
1087   }
1088
1089   num_records = ntohl(nrb->rd_count);
1090   name = (char*)&nrb[1];
1091   {
1092     struct GNUNET_NAMESTORE_RecordData rd[num_records];
1093     
1094     rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1095     rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1096   
1097     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1098                                                                rd_data,
1099                                                                num_records,
1100                                                                rd))
1101     {
1102       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1103                  "GNS_PHASE_DELEGATE: Error deserializing data!\n");
1104       return;
1105     }
1106
1107     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1108                "GNS_PHASE_DELEGATE: Got name: %s (wanted %s)\n",
1109                name, rh->authority_name);
1110     for (i=0; i<num_records; i++)
1111     {
1112     
1113       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1114                 "GNS_PHASE_DELEGATE: Got name: %s (wanted %s)\n",
1115                 name, rh->authority_name);
1116       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1117                  "GNS_PHASE_DELEGATE: Got type: %d (wanted %d)\n",
1118                  rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
1119       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1120                  "GNS_PHASE_DELEGATE: Got data length: %d\n",
1121                  rd[i].data_size);
1122       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1123                  "GNS_PHASE_DELEGATE: Got flag %d\n", rd[i].flags);
1124
1125       if ((strcmp(name, rh->authority_name) == 0) &&
1126           (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
1127       {
1128         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1129                    "GNS_PHASE_DELEGATE: Authority found in DHT\n");
1130         rh->answered = 1;
1131         memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1132         struct AuthorityChain *auth =
1133           GNUNET_malloc(sizeof(struct AuthorityChain));
1134         auth->zone = rh->authority;
1135         memset(auth->name, 0, strlen(rh->authority_name)+1);
1136         strcpy(auth->name, rh->authority_name);
1137         GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1138                                      rh->authority_chain_tail,
1139                                      auth);
1140
1141         /** try to import pkey if private key available */
1142         if (rh->priv_key)
1143           process_discovered_authority(name, auth->zone,
1144                                        rh->authority_chain_tail->zone,
1145                                        rh->priv_key);
1146       }
1147
1148     }
1149
1150
1151     GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
1152     GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1153     GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
1154     GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
1155
1156     /* Save to namestore */
1157     if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
1158                                           &zone))
1159     {
1160       GNUNET_NAMESTORE_record_put (namestore_handle,
1161                                  &nrb->public_key,
1162                                  name,
1163                                  exp,
1164                                  num_records,
1165                                  rd,
1166                                  &nrb->signature,
1167                                  &on_namestore_record_put_result, //cont
1168                                  NULL); //cls
1169     }
1170   }
1171   
1172   if (rh->answered)
1173   {
1174     rh->answered = 0;
1175     /**
1176      * delegate
1177      * FIXME in this case. should we ask namestore again?
1178      */
1179     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1180                "GNS_PHASE_DELEGATE: Answer from DHT for %s to resolve: %s\n",
1181                rh->authority_name, rh->name);
1182     if (strcmp(rh->name, "") == 0)
1183       rh->proc(rh->proc_cls, rh, 0, NULL);
1184     else
1185       resolve_delegation_ns(rh);
1186     return;
1187   }
1188   
1189   /**
1190    * No pkey but name exists
1191    * promote back
1192    */
1193   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1194              "GNS_PHASE_DELEGATE: Adding %s back to %s\n",
1195              rh->authority_name, rh->name);
1196   if (strcmp(rh->name, "") == 0)
1197     strcpy(rh->name, rh->authority_name);
1198   else
1199     GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
1200                   rh->name, rh->authority_name); //FIXME ret
1201   
1202   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1203              "GNS_PHASE_DELEGATE: %s restored\n", rh->name);
1204   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1205              "GNS_PHASE_DELEGATE: DHT authority lookup found no match!\n");
1206   rh->proc(rh->proc_cls, rh, 0, NULL);
1207 }
1208
1209 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1210                         +(MAX_DNS_NAME_LENGTH*2)
1211 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
1212
1213
1214 static void
1215 expand_plus(char** dest, char* src, char* repl)
1216 {
1217   char* pos;
1218   unsigned int s_len = strlen(src)+1;
1219
1220   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1221              "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
1222
1223   if (s_len < 3)
1224   {
1225     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226                "GNS_POSTPROCESS: %s to short\n", src);
1227
1228     /* no postprocessing */
1229     memcpy(*dest, src, s_len+1);
1230     return;
1231   }
1232   
1233   if (0 == strcmp(src+s_len-3, ".+"))
1234   {
1235     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1236                "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
1237     memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
1238     strcpy(*dest, src);
1239     pos = *dest+s_len-2;
1240     strcpy(pos, repl);
1241     pos += strlen(repl);
1242     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1243                "GNS_POSTPROCESS: Expanded to %s\n", *dest);
1244   }
1245   else
1246   {
1247     memcpy(*dest, src, s_len+1);
1248   }
1249 }
1250
1251 /**
1252  * finish lookup
1253  */
1254 static void
1255 finish_lookup(struct ResolverHandle *rh,
1256               struct RecordLookupHandle* rlh,
1257               unsigned int rd_count,
1258               const struct GNUNET_NAMESTORE_RecordData *rd)
1259 {
1260   int i;
1261   char new_rr_data[MAX_DNS_NAME_LENGTH];
1262   char new_mx_data[MAX_MX_LENGTH];
1263   char new_soa_data[MAX_SOA_LENGTH];
1264   struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
1265   char* repl_string;
1266   char* pos;
1267   unsigned int offset;
1268
1269   if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1270     GNUNET_SCHEDULER_cancel(rh->timeout_task);
1271
1272   if (rd_count > 0)
1273     memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
1274
1275   for (i = 0; i < rd_count; i++)
1276   {
1277     
1278     if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
1279         rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
1280         rd[i].record_type != GNUNET_GNS_RECORD_MX &&
1281         rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
1282     {
1283       p_rd[i].data = rd[i].data;
1284       continue;
1285     }
1286
1287     /**
1288      * for all those records we 'should'
1289      * also try to resolve the A/AAAA records (RFC1035)
1290      * This is a feature and not important
1291      */
1292     
1293     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1294                "GNS_POSTPROCESS: Postprocessing\n");
1295
1296     if (strcmp(rh->name, "+") == 0)
1297       repl_string = rlh->name;
1298     else
1299       repl_string = rlh->name+strlen(rh->name)+1;
1300
1301     offset = 0;
1302     if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
1303     {
1304       memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
1305       offset = sizeof(uint16_t);
1306       pos = new_mx_data+offset;
1307       expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
1308                   repl_string);
1309       offset += strlen(new_mx_data+sizeof(uint16_t))+1;
1310       p_rd[i].data = new_mx_data;
1311       p_rd[i].data_size = offset;
1312     }
1313     else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
1314     {
1315       /* expand mname and rname */
1316       pos = new_soa_data;
1317       expand_plus(&pos, (char*)rd[i].data, repl_string);
1318       offset = strlen(new_soa_data)+1;
1319       pos = new_soa_data+offset;
1320       expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
1321       offset += strlen(new_soa_data+offset)+1;
1322       /* cpy the 4 numbers serial refresh retry and expire */
1323       memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
1324       offset += sizeof(uint32_t)*5;
1325       p_rd[i].data_size = offset;
1326       p_rd[i].data = new_soa_data;
1327     }
1328     else
1329     {
1330       pos = new_rr_data;
1331       expand_plus(&pos, (char*)rd[i].data, repl_string);
1332       p_rd[i].data_size = strlen(new_rr_data)+1;
1333       p_rd[i].data = new_rr_data;
1334     }
1335     
1336   }
1337
1338   rlh->proc(rlh->proc_cls, rd_count, p_rd);
1339   GNUNET_free(rlh);
1340   
1341 }
1342
1343 /**
1344  * Process DHT lookup result for record.
1345  *
1346  * @param cls the closure
1347  * @param rh resolver handle
1348  * @param rd_count number of results
1349  * @param rd record data
1350  */
1351 static void
1352 handle_record_dht(void* cls, struct ResolverHandle *rh,
1353                        unsigned int rd_count,
1354                        const struct GNUNET_NAMESTORE_RecordData *rd)
1355 {
1356   struct RecordLookupHandle* rlh;
1357
1358   rlh = (struct RecordLookupHandle*)cls;
1359   if (rd_count == 0)
1360   {
1361     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1362                "GNS_PHASE_REC: No records for %s found in DHT. Aborting\n",
1363                rh->name);
1364     /* give up, cannot resolve */
1365     finish_lookup(rh, rlh, 0, NULL);
1366     free_resolver_handle(rh);
1367     return;
1368   }
1369
1370   /* results found yay */
1371   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1372              "GNS_PHASE_REC: Record resolved from DHT!");
1373
1374   finish_lookup(rh, rlh, rd_count, rd);
1375   free_resolver_handle(rh);
1376
1377 }
1378
1379
1380 /**
1381  * Process namestore lookup result for record.
1382  *
1383  * @param cls the closure
1384  * @param rh resolver handle
1385  * @param rd_count number of results
1386  * @param rd record data
1387  */
1388 static void
1389 handle_record_ns(void* cls, struct ResolverHandle *rh,
1390                        unsigned int rd_count,
1391                        const struct GNUNET_NAMESTORE_RecordData *rd)
1392 {
1393   struct RecordLookupHandle* rlh;
1394   rlh = (struct RecordLookupHandle*) cls;
1395   if (rd_count == 0)
1396   {
1397     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1398                "GNS_PHASE_REC: Resolution status: %d!\n", rh->status);
1399     
1400     /* ns entry expired and not ours. try dht */
1401     if (rh->status & (EXPIRED | !EXISTS) &&
1402         GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1403                                      &local_zone))
1404     {
1405       rh->proc = &handle_record_dht;
1406       resolve_record_dht(rh);
1407       return;
1408     }
1409     /* give up, cannot resolve */
1410     finish_lookup(rh, rlh, 0, NULL);
1411     free_resolver_handle(rh);
1412     return;
1413   }
1414
1415   /* results found yay */
1416   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1417              "GNS_PHASE_REC: Record resolved from namestore!");
1418
1419   finish_lookup(rh, rlh, rd_count, rd);
1420
1421   free_resolver_handle(rh);
1422
1423 }
1424
1425
1426 /**
1427  * Determine if this name is canonical.
1428  * i.e.
1429  * a.b.gnunet  = not canonical
1430  * a           = canonical
1431  *
1432  * @param name the name to test
1433  * @return 1 if canonical
1434  */
1435 static int
1436 is_canonical(char* name)
1437 {
1438   uint32_t len = strlen(name);
1439   int i;
1440
1441   for (i=0; i<len; i++)
1442   {
1443     if (*(name+i) == '.')
1444       return 0;
1445   }
1446   return 1;
1447 }
1448
1449 /**
1450  * Move one level up in the domain hierarchy and return the
1451  * passed top level domain.
1452  *
1453  * @param name the domain
1454  * @param dest the destination where the tld will be put
1455  */
1456 void
1457 pop_tld(char* name, char* dest)
1458 {
1459   uint32_t len;
1460
1461   if (is_canonical(name))
1462   {
1463     strcpy(dest, name);
1464     strcpy(name, "");
1465     return;
1466   }
1467
1468   for (len = strlen(name); len > 0; len--)
1469   {
1470     if (*(name+len) == '.')
1471       break;
1472   }
1473   
1474   //Was canonical?
1475   if (len == 0)
1476     return;
1477
1478   name[len] = '\0';
1479
1480   strcpy(dest, (name+len+1));
1481 }
1482
1483 /**
1484  * Checks if name is in tld
1485  *
1486  * @param name the name to check
1487  * @param tld the TLD to check for
1488  * @return GNUNET_YES or GNUNET_NO
1489  */
1490 int
1491 is_tld(const char* name, const char* tld)
1492 {
1493   int offset = 0;
1494
1495   if (strlen(name) <= strlen(tld))
1496   {
1497     return GNUNET_NO;
1498   }
1499   
1500   offset = strlen(name)-strlen(tld);
1501   if (strcmp(name+offset, tld) != 0)
1502   {
1503     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1504                "%s is not in .%s TLD\n", name, tld);
1505     return GNUNET_NO;
1506   }
1507   return GNUNET_YES;
1508 }
1509
1510 /**
1511  * DHT resolution for delegation finished. Processing result.
1512  *
1513  * @param cls the closure
1514  * @param rh resolver handle
1515  * @param rd_count number of results (always 0)
1516  * @param rd record data (always NULL)
1517  */
1518 static void
1519 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
1520                           unsigned int rd_count,
1521                           const struct GNUNET_NAMESTORE_RecordData *rd)
1522 {
1523   struct RecordLookupHandle* rlh;
1524   rlh = (struct RecordLookupHandle*) cls;
1525   
1526
1527   if (strcmp(rh->name, "") == 0)
1528   {
1529     if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1530     {
1531       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1532                  "GNS_PHASE_DELEGATE: Resolved queried PKEY via DHT.\n");
1533       finish_lookup(rh, rlh, rd_count, rd);
1534       free_resolver_handle(rh);
1535       return;
1536     }
1537     /* We resolved full name for delegation. resolving record */
1538     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1539       "GNS_PHASE_DELEGATE: Resolved full name for delegation via DHT.\n");
1540     strcpy(rh->name, "+\0");
1541     rh->proc = &handle_record_ns;
1542     resolve_record_ns(rh);
1543     return;
1544   }
1545
1546   /**
1547    * we still have some left
1548    **/
1549   if (is_canonical(rh->name))
1550   {
1551     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1552                "GNS_PHASE_DELEGATE: Resolving canonical record %s in ns\n",
1553                rh->name);
1554     rh->proc = &handle_record_ns;
1555     resolve_record_ns(rh);
1556     return;
1557   }
1558   /* give up, cannot resolve */
1559   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1560         "GNS_PHASE_DELEGATE: Cannot fully resolve delegation for %s via DHT!\n",
1561              rh->name);
1562   finish_lookup(rh, rlh, 0, NULL);
1563   free_resolver_handle(rh);
1564 }
1565
1566
1567 /**
1568  * Start DHT lookup for a name -> PKEY (compare NS) record in
1569  * rh->authority's zone
1570  *
1571  * @param rh the pending gns query
1572  */
1573 static void
1574 resolve_delegation_dht(struct ResolverHandle *rh)
1575 {
1576   uint32_t xquery;
1577   struct GNUNET_CRYPTO_ShortHashCode name_hash;
1578   GNUNET_HashCode name_hash_double;
1579   GNUNET_HashCode zone_hash_double;
1580   GNUNET_HashCode lookup_key;
1581   struct ResolverHandle *rh_heap_root;
1582   
1583   pop_tld(rh->name, rh->authority_name); 
1584   GNUNET_CRYPTO_short_hash(rh->authority_name,
1585                      strlen(rh->authority_name),
1586                      &name_hash);
1587   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1588   GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1589   GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1590   
1591   rh->dht_heap_node = NULL;
1592
1593   if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1594   {
1595     //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1596     //                                          &dht_authority_lookup_timeout,
1597     //                                                   rh);
1598     rh->timeout_cont = &dht_authority_lookup_timeout;
1599     rh->timeout_cont_cls = rh;
1600   }
1601   else 
1602   {
1603     if (max_allowed_background_queries <=
1604         GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1605     {
1606       /* terminate oldest lookup */
1607       rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1608       GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1609       rh_heap_root->dht_heap_node = NULL;
1610       
1611       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1612              "GNS_PHASE_DELEGATE: Replacing oldest background query for %s\n",
1613                  rh_heap_root->authority_name);
1614       
1615       rh_heap_root->proc(rh_heap_root->proc_cls,
1616                          rh_heap_root,
1617                          0,
1618                          NULL);
1619     }
1620     rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1621                                          rh,
1622                                          GNUNET_TIME_absolute_get().abs_value);
1623   }
1624   
1625   xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1626   
1627   GNUNET_assert(rh->get_handle == NULL);
1628   rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1629                        GNUNET_TIME_UNIT_FOREVER_REL,
1630                        GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1631                        &lookup_key,
1632                        DHT_GNS_REPLICATION_LEVEL,
1633                        GNUNET_DHT_RO_NONE,
1634                        &xquery,
1635                        sizeof(xquery),
1636                        &process_delegation_result_dht,
1637                        rh);
1638
1639 }
1640
1641
1642 /**
1643  * Namestore resolution for delegation finished. Processing result.
1644  *
1645  * @param cls the closure
1646  * @param rh resolver handle
1647  * @param rd_count number of results (always 0)
1648  * @param rd record data (always NULL)
1649  */
1650 static void
1651 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1652                           unsigned int rd_count,
1653                           const struct GNUNET_NAMESTORE_RecordData *rd)
1654 {
1655   struct RecordLookupHandle* rlh;
1656   rlh = (struct RecordLookupHandle*) cls;
1657   
1658   if (strcmp(rh->name, "") == 0)
1659   {
1660     if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1661     {
1662       GNUNET_assert(rd_count == 1);
1663       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1664                  "GNS_PHASE_DELEGATE: Resolved queried PKEY in NS.\n");
1665       finish_lookup(rh, rlh, rd_count, rd);
1666       free_resolver_handle(rh);
1667       return;
1668     }
1669     /* We resolved full name for delegation. resolving record */
1670     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1671                "GNS_PHASE_DELEGATE: Resolved full name for delegation.\n");
1672     strcpy(rh->name, "+\0");
1673     rh->proc = &handle_record_ns;
1674     resolve_record_ns(rh);
1675     return;
1676   }
1677
1678   /**
1679    * we still have some left
1680    * check if authority in ns is fresh
1681    * and exists
1682    * or we are authority
1683    **/
1684   if ((rh->status & (EXISTS | !EXPIRED)) ||
1685       !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1686                              &rh->authority_chain_tail->zone))
1687   {
1688     if (is_canonical(rh->name))
1689     {
1690       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1691                  "GNS_PHASE_DELEGATE: Resolving canonical record %s\n",
1692                  rh->name);
1693       rh->proc = &handle_record_ns;
1694       resolve_record_ns(rh);
1695     }
1696     else
1697     {
1698       /* give up, cannot resolve */
1699       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1700                 "GNS_PHASE_DELEGATE: Cannot fully resolve delegation for %s!\n",
1701                 rh->name);
1702       rlh->proc(rlh->proc_cls, 0, NULL);
1703     }
1704     return;
1705   }
1706   
1707   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1708             "GNS_PHASE_DELEGATE: Trying to resolve delegation for %s via DHT\n",
1709             rh->name);
1710   rh->proc = &handle_delegation_dht;
1711   resolve_delegation_dht(rh);
1712 }
1713
1714
1715
1716 /**
1717  * This is a callback function that should give us only PKEY
1718  * records. Used to query the namestore for the authority (PKEY)
1719  * for 'name'. It will recursively try to resolve the
1720  * authority for a given name from the namestore.
1721  *
1722  * @param cls the pending query
1723  * @param key the key of the zone we did the lookup
1724  * @param expiration expiration date of the record data set in the namestore
1725  * @param name the name for which we need an authority
1726  * @param rd_count the number of records with 'name'
1727  * @param rd the record data
1728  * @param signature the signature of the authority for the record data
1729  */
1730 static void
1731 process_delegation_result_ns(void* cls,
1732                    const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1733                    struct GNUNET_TIME_Absolute expiration,
1734                    const char *name,
1735                    unsigned int rd_count,
1736                    const struct GNUNET_NAMESTORE_RecordData *rd,
1737                    const struct GNUNET_CRYPTO_RsaSignature *signature)
1738 {
1739   struct ResolverHandle *rh;
1740   struct GNUNET_TIME_Relative remaining_time;
1741   struct GNUNET_CRYPTO_ShortHashCode zone;
1742   char new_name[MAX_DNS_NAME_LENGTH];
1743   
1744   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1745              "GNS_PHASE_DELEGATE: Got %d records from authority lookup\n",
1746              rd_count);
1747
1748   rh = (struct ResolverHandle *)cls;
1749   GNUNET_CRYPTO_short_hash(key,
1750                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1751                      &zone);
1752   remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1753   
1754   rh->status = 0;
1755   
1756   if (name != NULL)
1757   {
1758     rh->status |= EXISTS;
1759   }
1760   
1761   if (remaining_time.rel_value == 0)
1762   {
1763     rh->status |= EXPIRED;
1764   }
1765   
1766   /**
1767    * No authority found in namestore.
1768    */
1769   if (rd_count == 0)
1770   {
1771     /**
1772      * We did not find an authority in the namestore
1773      */
1774     
1775     /**
1776      * No PKEY in zone.
1777      * Promote this authority back to a name maybe it is
1778      * our record.
1779      */
1780     if (strcmp(rh->name, "") == 0)
1781     {
1782       /* simply promote back */
1783       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1784                  "GNS_PHASE_DELEGATE: Promoting %s back to name\n",
1785                  rh->authority_name);
1786       strcpy(rh->name, rh->authority_name);
1787     }
1788     else
1789     {
1790       /* add back to existing name */
1791       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1792                  "GNS_PHASE_DELEGATE: Adding %s back to %s\n",
1793                  rh->authority_name, rh->name);
1794       //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1795       strcpy(new_name, rh->name);
1796       strcpy(new_name+strlen(new_name), ".");
1797       strcpy(new_name+strlen(new_name), rh->authority_name);
1798       strcpy(rh->name, new_name);
1799       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1800                  "GNS_PHASE_DELEGATE: %s restored\n", rh->name);
1801     }
1802     rh->proc(rh->proc_cls, rh, 0, NULL);
1803     return;
1804   }
1805
1806   /**
1807    * We found an authority that may be able to help us
1808    * move on with query
1809    * Note only 1 pkey should have been returned.. anything else would be strange
1810    */
1811   int i;
1812   for (i=0; i<rd_count;i++)
1813   {
1814   
1815     if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1816       continue;
1817     
1818     if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1819          == 0)
1820     {
1821       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1822                  "GNS_PHASE_DELEGATE: This pkey is expired.\n");
1823       if (remaining_time.rel_value == 0)
1824       {
1825         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1826                    "GNS_PHASE_DELEGATE: This dht entry is expired.\n");
1827         rh->authority_chain_head->fresh = 0;
1828         rh->proc(rh->proc_cls, rh, 0, NULL);
1829         return;
1830       }
1831
1832       continue;
1833     }
1834
1835     /**
1836      * Resolve rest of query with new authority
1837      */
1838     GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1839     memcpy(&rh->authority, rd[i].data,
1840            sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1841     struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1842     auth->zone = rh->authority;
1843     memset(auth->name, 0, strlen(rh->authority_name)+1);
1844     strcpy(auth->name, rh->authority_name);
1845     GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1846                                  rh->authority_chain_tail,
1847                                  auth);
1848     
1849     /**
1850      * We are done with PKEY resolution if name is empty
1851      * else resolve again with new authority
1852      */
1853     if (strcmp(rh->name, "") == 0)
1854       rh->proc(rh->proc_cls, rh, rd_count, rd);
1855     else
1856       resolve_delegation_ns(rh);
1857     return;
1858   }
1859     
1860   /**
1861    * no answers found
1862    */
1863   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1864     "GNS_PHASE_DELEGATE: Authority lookup and but no PKEY... never get here\n");
1865   rh->proc(rh->proc_cls, rh, 0, NULL);
1866 }
1867
1868
1869 /**
1870  * Resolve the delegation chain for the request in our namestore
1871  *
1872  * @param rh the resolver handle
1873  */
1874 static void
1875 resolve_delegation_ns(struct ResolverHandle *rh)
1876 {
1877   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1878              "GNS_PHASE_DELEGATE: Resolving delegation for %s\n", rh->name);
1879   pop_tld(rh->name, rh->authority_name);
1880   GNUNET_NAMESTORE_lookup_record(namestore_handle,
1881                                  &rh->authority,
1882                                  rh->authority_name,
1883                                  GNUNET_GNS_RECORD_PKEY,
1884                                  &process_delegation_result_ns,
1885                                  rh);
1886
1887 }
1888
1889
1890 /**
1891  * Lookup of a record in a specific zone
1892  * calls lookup result processor on result
1893  *
1894  * @param zone the root zone
1895  * @param record_type the record type to look up
1896  * @param name the name to look up
1897  * @param key a private key for use with PSEU import (can be NULL)
1898  * @param timeout timeout for resolution
1899  * @param proc the processor to call on result
1900  * @param cls the closure to pass to proc
1901  */
1902 void
1903 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
1904                            uint32_t record_type,
1905                            const char* name,
1906                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
1907                            struct GNUNET_TIME_Relative timeout,
1908                            RecordLookupProcessor proc,
1909                            void* cls)
1910 {
1911   struct ResolverHandle *rh;
1912   struct RecordLookupHandle* rlh;
1913   char string_hash[MAX_DNS_LABEL_LENGTH];
1914   uint8_t* normalized_zkey;
1915   char nzkey[MAX_DNS_LABEL_LENGTH];
1916   size_t normal_len;
1917
1918   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919               "Starting resolution for %s (type=%d)!\n",
1920               name, record_type);
1921
1922   
1923   if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
1924   {
1925     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926                 "%s is canonical and not gnunet -> cannot resolve!\n", name);
1927     proc(cls, 0, NULL);
1928     return;
1929   }
1930   
1931   rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1932   rh = GNUNET_malloc(sizeof (struct ResolverHandle));
1933
1934   rh->authority = zone;
1935   rh->proc_cls = rlh;
1936   rh->priv_key = key;
1937   rh->timeout = timeout;
1938   rh->get_handle = NULL;
1939   if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1940   {
1941     /*
1942      * Set timeout for authority lookup phase to 1/2
1943      */
1944     rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1945                                 GNUNET_TIME_relative_divide(timeout, 2),
1946                                                 &handle_lookup_timeout,
1947                                                 rh);
1948     rh->timeout_cont = &dht_authority_lookup_timeout;
1949     rh->timeout_cont_cls = rh;
1950   }
1951   else
1952   {
1953     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
1954     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1955   }
1956   
1957   if (strcmp(GNUNET_GNS_TLD, name) == 0)
1958   {
1959     /**
1960      * Only 'gnunet' given
1961      */
1962     strcpy(rh->name, "\0");
1963   }
1964   else
1965   {
1966     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967                 "Checking for TLD...\n");
1968     if (is_zkey_tld(name) == GNUNET_YES)
1969     {
1970       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1971                   "TLD is zkey\n");
1972       /**
1973        * This is a zkey tld
1974        * build hash and use as initial authority
1975        */
1976       memset(rh->name, 0,
1977              strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
1978       memcpy(rh->name, name,
1979              strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
1980       pop_tld(rh->name, string_hash);
1981
1982       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1983                   "ZKEY is %s!\n", string_hash);
1984       
1985       normalized_zkey = u8_toupper ((uint8_t*)string_hash, strlen ((char *) string_hash),
1986                                   NULL, UNINORM_NFD, NULL, &normal_len);
1987
1988       memcpy(nzkey, normalized_zkey, normal_len);
1989       nzkey[normal_len] = '\0';
1990       free(normalized_zkey);
1991       
1992       if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
1993                                                       &rh->authority))
1994       {
1995         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1996                     "Cannot convert ZKEY %s to hash!\n", string_hash);
1997         GNUNET_free(rh);
1998         GNUNET_free(rlh);
1999         proc(cls, 0, NULL);
2000         return;
2001       }
2002
2003     }
2004     else
2005     {
2006       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007                   "TLD is gnunet\n");
2008       /**
2009        * Presumably GNUNET tld
2010        */
2011       memset(rh->name, 0,
2012              strlen(name)-strlen(GNUNET_GNS_TLD));
2013       memcpy(rh->name, name,
2014              strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2015     }
2016   }
2017   
2018   /**
2019    * Initialize authority chain
2020    */
2021   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2022   rh->authority_chain_head->prev = NULL;
2023   rh->authority_chain_head->next = NULL;
2024   rh->authority_chain_tail = rh->authority_chain_head;
2025   rh->authority_chain_head->zone = rh->authority;
2026   
2027   /**
2028    * Copy original query into lookup handle
2029    */
2030   rlh->record_type = record_type;
2031   memset(rlh->name, 0, strlen(name) + 1);
2032   strcpy(rlh->name, name);
2033   rlh->proc = proc;
2034   rlh->proc_cls = cls;
2035
2036   rh->proc = &handle_delegation_ns;
2037   resolve_delegation_ns(rh);
2038 }
2039
2040 /******** END Record Resolver ***********/
2041
2042
2043 /**
2044  * Callback calles by namestore for a zone to name
2045  * result
2046  *
2047  * @param cls the closure
2048  * @param zone_key the zone we queried
2049  * @param expire the expiration time of the name
2050  * @param name the name found or NULL
2051  * @param rd_len number of records for the name
2052  * @param rd the record data (PKEY) for the name
2053  * @param signature the signature for the record data
2054  */
2055 static void
2056 process_zone_to_name_shorten(void *cls,
2057                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2058                  struct GNUNET_TIME_Absolute expire,
2059                  const char *name,
2060                  unsigned int rd_len,
2061                  const struct GNUNET_NAMESTORE_RecordData *rd,
2062                  const struct GNUNET_CRYPTO_RsaSignature *signature)
2063 {
2064   struct ResolverHandle *rh = (struct ResolverHandle *)cls;
2065   struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
2066   struct AuthorityChain *next_authority;
2067
2068   char result[MAX_DNS_NAME_LENGTH];
2069   char next_authority_name[MAX_DNS_LABEL_LENGTH];
2070   size_t answer_len;
2071   
2072   /* we found a match in our own zone */
2073   if (rd_len != 0)
2074   {
2075     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2076                "result strlen %d\n", strlen(name));
2077     answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
2078     memset(result, 0, answer_len);
2079     if (strlen(rh->name) > 0)
2080     {
2081       strcpy(result, rh->name);
2082       strcpy(result+strlen(rh->name), ".");
2083     }
2084     
2085     strcpy(result+strlen(result), name);
2086     strcpy(result+strlen(result), ".");
2087     strcpy(result+strlen(result), GNUNET_GNS_TLD);
2088     
2089     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2090                "Sending shorten result %s\n", result);
2091
2092     nsh->proc(nsh->proc_cls, result);
2093     GNUNET_free(nsh);
2094     free_resolver_handle(rh);
2095   }
2096   else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2097                                         &local_zone))
2098   {
2099     /* our zone, just append .gnunet */
2100     answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2101     memset(result, 0, answer_len);
2102     strcpy(result, rh->name);
2103     strcpy(result+strlen(rh->name), ".");
2104     strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2105
2106     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2107                "Our zone: Sending name as shorten result %s\n", rh->name);
2108     
2109     nsh->proc(nsh->proc_cls, result);
2110     GNUNET_free(nsh);
2111     free_resolver_handle(rh);
2112   }
2113   else
2114   {
2115     /**
2116      * No PSEU found.
2117      * continue with next authority
2118      */
2119     next_authority = rh->authority_chain_head;
2120     //                         strlen(next_authority->name) + 2);
2121     memset(next_authority_name, 0, strlen(rh->name)+
2122                       strlen(next_authority->name) + 2);
2123     strcpy(next_authority_name, rh->name);
2124     strcpy(next_authority_name+strlen(rh->name)+1, ".");
2125     strcpy(next_authority_name+strlen(rh->name)+2, next_authority->name);
2126   
2127     strcpy(rh->name, next_authority_name);
2128     GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2129                               rh->authority_chain_tail,
2130                               next_authority);
2131
2132     GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2133                                    &rh->authority_chain_tail->zone,
2134                                    &rh->authority_chain_head->zone,
2135                                    &process_zone_to_name_shorten,
2136                                    rh);
2137   }
2138 }
2139
2140 /**
2141  * DHT resolution for delegation finished. Processing result.
2142  *
2143  * @param cls the closure
2144  * @param rh resolver handle
2145  * @param rd_count number of results (always 0)
2146  * @param rd record data (always NULL)
2147  */
2148 static void
2149 handle_delegation_dht_bg_shorten(void* cls, struct ResolverHandle *rh,
2150                           unsigned int rd_count,
2151                           const struct GNUNET_NAMESTORE_RecordData *rd)
2152 {
2153   
2154   /* We resolved full name for delegation. resolving record */
2155   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2156     "GNS_SHORTEN: Resolved up to %s for delegation via DHT in background.\n",
2157     rh->name);
2158   free_resolver_handle(rh);
2159 }
2160
2161 /**
2162  * Process result from namestore delegation lookup
2163  * for shorten operation
2164  *
2165  * @param cls the client shorten handle
2166  * @param rh the resolver handle
2167  * @param rd_count number of results (0)
2168  * @param rd data (NULL)
2169  */
2170 void
2171 handle_delegation_ns_shorten(void* cls,
2172                       struct ResolverHandle *rh,
2173                       uint32_t rd_count,
2174                       const struct GNUNET_NAMESTORE_RecordData *rd)
2175 {
2176   struct NameShortenHandle *nsh;
2177   char result[MAX_DNS_NAME_LENGTH];
2178   size_t answer_len;
2179   struct ResolverHandle *rh_bg;
2180
2181   nsh = (struct NameShortenHandle *)cls;
2182   
2183   /**
2184    * At this point rh->name contains the part of the name
2185    * that we do not have a PKEY in our namestore to resolve.
2186    * The authority chain in the resolver handle is now
2187    * useful to backtrack if needed
2188    */
2189   
2190   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2191              "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2192
2193   if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2194                                    &local_zone) == 0)
2195   {
2196     /**
2197      * This is our zone append .gnunet unless name is empty
2198      * (it shouldn't be, usually FIXME what happens if we
2199      * shorten to our zone to a "" record??)
2200      */
2201     
2202     answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2203     memset(result, 0, answer_len);
2204     strcpy(result, rh->name);
2205     strcpy(result+strlen(rh->name), ".");
2206     strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2207
2208     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2209                "Our zone: Sending name as shorten result %s\n", rh->name);
2210     
2211     nsh->proc(nsh->proc_cls, result);
2212     GNUNET_free(nsh);
2213     free_resolver_handle(rh);
2214     return;
2215   }
2216   
2217   /**
2218    * we have to this before zone to name for rh might
2219    * be freed by then
2220    */
2221   rh_bg = NULL;
2222   if (!is_canonical(rh->name))
2223   {
2224     rh_bg = GNUNET_malloc(sizeof(struct ResolverHandle));
2225     memcpy(rh_bg, rh, sizeof(struct ResolverHandle));
2226   }
2227
2228   /* backtrack authorities for names */
2229   GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2230                                  &rh->authority_chain_tail->zone, //ours
2231                                  &rh->authority_chain_head->zone,
2232                                  &process_zone_to_name_shorten,
2233                                  rh);
2234   
2235   if (rh_bg == NULL)
2236   {
2237     return;
2238   }
2239
2240   /**
2241    * If authority resolution is incomplete we can do a background lookup
2242    * of the full name so that next time we can (likely) fully or at least
2243    * further shorten the name
2244    */
2245   rh_bg->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2246   rh_bg->authority_chain_tail = rh_bg->authority_chain_head;
2247   rh_bg->authority_chain_head->zone = rh_bg->authority;
2248   
2249   rh_bg->proc = &handle_delegation_dht_bg_shorten;
2250   rh_bg->proc_cls = NULL;
2251   
2252   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2253              "GNS_SHORTEN: Starting background lookup for %s\n",
2254              rh_bg->name);
2255
2256   resolve_delegation_dht(rh_bg);
2257
2258 }
2259
2260
2261 /**
2262  * Callback calles by namestore for a zone to name
2263  * result
2264  *
2265  * @param cls the closure
2266  * @param zone_key the zone we queried
2267  * @param expire the expiration time of the name
2268  * @param name the name found or NULL
2269  * @param rd_len number of records for the name
2270  * @param rd the record data (PKEY) for the name
2271  * @param signature the signature for the record data
2272  */
2273 static void
2274 process_zone_to_name_zkey(void *cls,
2275                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2276                  struct GNUNET_TIME_Absolute expire,
2277                  const char *name,
2278                  unsigned int rd_len,
2279                  const struct GNUNET_NAMESTORE_RecordData *rd,
2280                  const struct GNUNET_CRYPTO_RsaSignature *signature)
2281 {
2282   struct ResolverHandle *rh = cls;
2283   struct NameShortenHandle *nsh = rh->proc_cls;
2284   struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
2285   char new_name[MAX_DNS_NAME_LENGTH];
2286
2287   /* zkey not in our zone */
2288   if (name == NULL)
2289   {
2290     /**
2291      * In this case we have not given this PKEY a name (yet)
2292      * It is either just not in our zone or not even cached
2293      * Since we do not know at this point we will not try to shorten
2294      * because PKEY import will happen if the user follows the zkey
2295      * link.
2296      */
2297     GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
2298                                      &enc);
2299     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2300                "No name found for zkey %s returning verbatim!\n", enc);
2301     if (strcmp(rh->name, "") != 0)
2302       GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
2303                       rh->name, enc, GNUNET_GNS_TLD_ZKEY);
2304     else
2305       GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2306                       enc, GNUNET_GNS_TLD_ZKEY);
2307     nsh->proc(nsh->proc_cls, new_name);
2308     GNUNET_free(nsh);
2309     free_resolver_handle(rh);
2310     return;
2311   }
2312   
2313   if (strcmp(rh->name, "") != 0)
2314     GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2315                     rh->name, name);
2316   else
2317     strcpy(new_name, name);
2318
2319   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2320              "Continue shorten for %s!\n", new_name);
2321
2322   strcpy(rh->name, new_name);
2323   
2324   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2325   rh->authority_chain_tail = rh->authority_chain_head;
2326   rh->authority_chain_head->zone = rh->authority;
2327   
2328   
2329   /* Start delegation resolution in our namestore */
2330   resolve_delegation_ns(rh);
2331 }
2332   
2333 /**
2334  * Shorten api from resolver
2335  *
2336  * @param zone the zone to use
2337  * @param name the name to shorten
2338  * @param key optional private key for background lookups and PSEU import
2339  * @param proc the processor to call with result
2340  * @param proc_cls closure to pass to proc
2341  */
2342 void
2343 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
2344                           const char* name,
2345                           struct GNUNET_CRYPTO_RsaPrivateKey *key,
2346                           ShortenResultProcessor proc,
2347                           void* proc_cls)
2348 {
2349   struct ResolverHandle *rh;
2350   struct NameShortenHandle *nsh;
2351   char string_hash[MAX_DNS_LABEL_LENGTH];
2352   struct GNUNET_CRYPTO_ShortHashCode zkey;
2353   uint8_t* normalized_zkey;
2354   size_t normal_len;
2355   char nzkey[MAX_DNS_LABEL_LENGTH];
2356
2357
2358   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359               "Starting shorten for %s!\n", name);
2360   
2361   if (is_canonical((char*)name))
2362   {
2363     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2364                 "%s is canonical. Returning verbatim\n", name);
2365     proc(proc_cls, name);
2366     return;
2367   }
2368
2369   nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
2370
2371   nsh->proc = proc;
2372   nsh->proc_cls = proc_cls;
2373   
2374   rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2375   rh->authority = zone;
2376   rh->priv_key = key;
2377   rh->proc = &handle_delegation_ns_shorten;
2378   rh->proc_cls = nsh;
2379   
2380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381                 "Checking for TLD...\n");
2382   if (is_zkey_tld(name) == GNUNET_YES)
2383   {
2384     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385                 "TLD is zkey\n");
2386     /**
2387      * This is a zkey tld
2388      * build hash and use as initial authority
2389      * FIXME sscanf
2390      */
2391     memset(rh->name, 0,
2392            strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2393     memcpy(rh->name, name,
2394            strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2395     pop_tld(rh->name, string_hash);
2396
2397     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2398                 "ZKEY is %s!\n", string_hash);
2399
2400     normalized_zkey = u8_toupper ((uint8_t*)string_hash, strlen ((char *) string_hash),
2401                                   NULL, UNINORM_NFD, NULL, &normal_len);
2402
2403     memcpy(nzkey, normalized_zkey, normal_len);
2404     nzkey[normal_len] = '\0';
2405     free(normalized_zkey);
2406
2407     if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2408                                                           &zkey))
2409     {
2410       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2411                   "Cannot convert ZKEY %s to hash!\n", nzkey);
2412       GNUNET_free(rh);
2413       GNUNET_free(nsh);
2414       proc(proc_cls, name);
2415       return;
2416     }
2417
2418     GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2419                                    &zone, //ours
2420                                    &zkey,
2421                                    &process_zone_to_name_zkey,
2422                                    rh);
2423     return;
2424
2425   }
2426   else
2427   {
2428     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429                 "TLD is gnunet\n");
2430     /**
2431      * Presumably GNUNET tld
2432      */
2433     memset(rh->name, 0,
2434            strlen(name)-strlen(GNUNET_GNS_TLD));
2435     memcpy(rh->name, name,
2436            strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2437   }
2438
2439   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2440   rh->authority_chain_tail = rh->authority_chain_head;
2441   rh->authority_chain_head->zone = zone;
2442   
2443   
2444   /* Start delegation resolution in our namestore */
2445   resolve_delegation_ns(rh);
2446 }
2447
2448 /*********** END NAME SHORTEN ********************/
2449
2450
2451 /**
2452  * Process result from namestore delegation lookup
2453  * for get authority operation
2454  *
2455  * @param cls the client get auth handle
2456  * @param rh the resolver handle
2457  * @param rd_count number of results (0)
2458  * @param rd data (NULL)
2459  */
2460 void
2461 handle_delegation_result_ns_get_auth(void* cls,
2462                       struct ResolverHandle *rh,
2463                       uint32_t rd_count,
2464                       const struct GNUNET_NAMESTORE_RecordData *rd)
2465 {
2466   struct GetNameAuthorityHandle* nah;
2467   char result[MAX_DNS_NAME_LENGTH];
2468   size_t answer_len;
2469
2470   nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
2471   
2472   /**
2473    * At this point rh->name contains the part of the name
2474    * that we do not have a PKEY in our namestore to resolve.
2475    * The authority chain in the resolver handle is now
2476    * useful to backtrack if needed
2477    */
2478   
2479   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2480              "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2481
2482   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2483              "Building response!\n");
2484   if (is_canonical(rh->name))
2485   {
2486     /**
2487      * We successfully resolved the authority in the ns
2488      * FIXME for our purposes this is fine
2489      * but maybe we want to have an api that also looks
2490      * into the dht (i.e. option in message)
2491      **/
2492     if (strlen(rh->name) > strlen(nah->name))
2493     {
2494       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2495                  "Record name longer than original lookup name... odd!\n");
2496       //FIXME to sth here
2497     }
2498
2499     answer_len = strlen(nah->name) - strlen(rh->name)
2500       + strlen(GNUNET_GNS_TLD) + 1;
2501     memset(result, 0, answer_len);
2502     strcpy(result, nah->name + strlen(rh->name) + 1);
2503
2504     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2505                "Got authority result %s\n", result);
2506     
2507     nah->proc(nah->proc_cls, result);
2508     GNUNET_free(nah);
2509     free_resolver_handle(rh);
2510   }
2511   else
2512   {
2513     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2514                "Unable to resolve authority for remaining %s!\n", rh->name);
2515     nah->proc(nah->proc_cls, "");
2516     GNUNET_free(nah);
2517     free_resolver_handle(rh);
2518   }
2519
2520
2521 }
2522
2523
2524 /**
2525  * Tries to resolve the authority for name
2526  * in our namestore
2527  *
2528  * @param zone the root zone to look up for
2529  * @param name the name to lookup up
2530  * @param proc the processor to call when finished
2531  * @param proc_cls the closure to pass to the processor
2532  */
2533 void
2534 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2535                            const char* name,
2536                            GetAuthorityResultProcessor proc,
2537                            void* proc_cls)
2538 {
2539   struct ResolverHandle *rh;
2540   struct GetNameAuthorityHandle *nah;
2541
2542   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543               "Starting authority resolution for %s!\n", name);
2544
2545   nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2546   rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2547   rh->authority = zone;
2548   
2549   if (strcmp(GNUNET_GNS_TLD, name) == 0)
2550   {
2551     strcpy(rh->name, "\0");
2552   }
2553   else
2554   {
2555     memset(rh->name, 0,
2556            strlen(name)-strlen(GNUNET_GNS_TLD));
2557     memcpy(rh->name, name,
2558            strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2559   }
2560
2561   memset(nah->name, 0,
2562          strlen(name)+1);
2563   strcpy(nah->name, name);
2564   
2565   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2566   rh->authority_chain_tail = rh->authority_chain_head;
2567   rh->authority_chain_head->zone = zone;
2568   rh->proc = &handle_delegation_result_ns_get_auth;
2569   rh->proc_cls = (void*)nah;
2570
2571   nah->proc = proc;
2572   nah->proc_cls = proc_cls;
2573
2574   /* Start delegation resolution in our namestore */
2575   resolve_delegation_ns(rh);
2576
2577 }
2578
2579 /******** END GET AUTHORITY *************/
2580
2581 /* end of gnunet-service-gns_resolver.c */