-fix warning
[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 /**
2142  * Process result from namestore delegation lookup
2143  * for shorten operation
2144  *
2145  * @param cls the client shorten handle
2146  * @param rh the resolver handle
2147  * @param rd_count number of results (0)
2148  * @param rd data (NULL)
2149  */
2150 void
2151 handle_delegation_ns_shorten(void* cls,
2152                       struct ResolverHandle *rh,
2153                       uint32_t rd_count,
2154                       const struct GNUNET_NAMESTORE_RecordData *rd)
2155 {
2156   struct NameShortenHandle *nsh;
2157   char result[MAX_DNS_NAME_LENGTH];
2158   size_t answer_len;
2159
2160   nsh = (struct NameShortenHandle *)cls;
2161   
2162   /**
2163    * At this point rh->name contains the part of the name
2164    * that we do not have a PKEY in our namestore to resolve.
2165    * The authority chain in the resolver handle is now
2166    * useful to backtrack if needed
2167    */
2168   
2169   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2170              "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2171
2172   if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2173                                    &local_zone) == 0)
2174   {
2175     /**
2176      * This is our zone append .gnunet unless name is empty
2177      * (it shouldn't be, usually FIXME what happens if we
2178      * shorten to our zone to a "" record??)
2179      */
2180     
2181     answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2182     memset(result, 0, answer_len);
2183     strcpy(result, rh->name);
2184     strcpy(result+strlen(rh->name), ".");
2185     strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2186
2187     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2188                "Our zone: Sending name as shorten result %s\n", rh->name);
2189     
2190     nsh->proc(nsh->proc_cls, result);
2191     GNUNET_free(nsh);
2192     free_resolver_handle(rh);
2193     return;
2194   }
2195   
2196   /* backtrack authorities for names */
2197   GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2198                                  &rh->authority_chain_tail->zone, //ours
2199                                  &rh->authority_chain_head->zone,
2200                                  &process_zone_to_name_shorten,
2201                                  rh);
2202
2203 }
2204
2205
2206 /**
2207  * Callback calles by namestore for a zone to name
2208  * result
2209  *
2210  * @param cls the closure
2211  * @param zone_key the zone we queried
2212  * @param expire the expiration time of the name
2213  * @param name the name found or NULL
2214  * @param rd_len number of records for the name
2215  * @param rd the record data (PKEY) for the name
2216  * @param signature the signature for the record data
2217  */
2218 static void
2219 process_zone_to_name_zkey(void *cls,
2220                  const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2221                  struct GNUNET_TIME_Absolute expire,
2222                  const char *name,
2223                  unsigned int rd_len,
2224                  const struct GNUNET_NAMESTORE_RecordData *rd,
2225                  const struct GNUNET_CRYPTO_RsaSignature *signature)
2226 {
2227   struct ResolverHandle *rh = cls;
2228   struct NameShortenHandle *nsh = rh->proc_cls;
2229   struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
2230   char new_name[MAX_DNS_NAME_LENGTH];
2231
2232   /* zkey not in our zone */
2233   if (name == NULL)
2234   {
2235     GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
2236                                      &enc);
2237     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2238                "No name found for zkey %s returning verbatim!\n", enc);
2239     if (strcmp(rh->name, "") != 0)
2240       GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
2241                       rh->name, enc, GNUNET_GNS_TLD_ZKEY);
2242     else
2243       GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2244                       enc, GNUNET_GNS_TLD_ZKEY);
2245     nsh->proc(nsh->proc_cls, new_name);
2246     GNUNET_free(nsh);
2247     free_resolver_handle(rh);
2248     return;
2249   }
2250   
2251   if (strcmp(rh->name, "") != 0)
2252     GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2253                     rh->name, name);
2254   else
2255     strcpy(new_name, name);
2256
2257   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2258              "Continue shorten for %s!\n", new_name);
2259
2260   strcpy(rh->name, new_name);
2261   
2262   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2263   rh->authority_chain_tail = rh->authority_chain_head;
2264   rh->authority_chain_head->zone = rh->authority;
2265   
2266   
2267   /* Start delegation resolution in our namestore */
2268   resolve_delegation_ns(rh);
2269 }
2270   
2271 /**
2272  * Shorten api from resolver
2273  *
2274  * @param zone the zone to use
2275  * @param name the name to shorten
2276  * @param proc the processor to call with result
2277  * @param proc_cls closure to pass to proc
2278  */
2279 void
2280 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
2281                           const char* name,
2282                           ShortenResultProcessor proc,
2283                           void* proc_cls)
2284 {
2285   struct ResolverHandle *rh;
2286   struct NameShortenHandle *nsh;
2287   char string_hash[MAX_DNS_LABEL_LENGTH];
2288   struct GNUNET_CRYPTO_ShortHashCode zkey;
2289   uint8_t* normalized_zkey;
2290   size_t normal_len;
2291   char nzkey[MAX_DNS_LABEL_LENGTH];
2292
2293
2294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295               "Starting shorten for %s!\n", name);
2296   
2297   if (is_canonical((char*)name))
2298   {
2299     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300                 "%s is canonical. Returning verbatim\n", name);
2301     proc(proc_cls, name);
2302     return;
2303   }
2304
2305   nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
2306
2307   nsh->proc = proc;
2308   nsh->proc_cls = proc_cls;
2309   
2310   rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2311   rh->authority = zone;
2312   rh->proc = &handle_delegation_ns_shorten;
2313   rh->proc_cls = nsh;
2314   
2315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316                 "Checking for TLD...\n");
2317   if (is_zkey_tld(name) == GNUNET_YES)
2318   {
2319     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320                 "TLD is zkey\n");
2321     /**
2322      * This is a zkey tld
2323      * build hash and use as initial authority
2324      * FIXME sscanf
2325      */
2326     memset(rh->name, 0,
2327            strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2328     memcpy(rh->name, name,
2329            strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2330     pop_tld(rh->name, string_hash);
2331
2332     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333                 "ZKEY is %s!\n", string_hash);
2334
2335     normalized_zkey = u8_toupper ((uint8_t*)string_hash, strlen ((char *) string_hash),
2336                                   NULL, UNINORM_NFD, NULL, &normal_len);
2337
2338     memcpy(nzkey, normalized_zkey, normal_len);
2339     nzkey[normal_len] = '\0';
2340     free(normalized_zkey);
2341
2342     if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2343                                                           &zkey))
2344     {
2345       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2346                   "Cannot convert ZKEY %s to hash!\n", nzkey);
2347       GNUNET_free(rh);
2348       GNUNET_free(nsh);
2349       proc(proc_cls, name);
2350       return;
2351     }
2352
2353     GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2354                                    &zone, //ours
2355                                    &zkey,
2356                                    &process_zone_to_name_zkey,
2357                                    rh);
2358     return;
2359
2360   }
2361   else
2362   {
2363     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2364                 "TLD is gnunet\n");
2365     /**
2366      * Presumably GNUNET tld
2367      */
2368     memset(rh->name, 0,
2369            strlen(name)-strlen(GNUNET_GNS_TLD));
2370     memcpy(rh->name, name,
2371            strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2372   }
2373
2374   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2375   rh->authority_chain_tail = rh->authority_chain_head;
2376   rh->authority_chain_head->zone = zone;
2377   
2378   
2379   /* Start delegation resolution in our namestore */
2380   resolve_delegation_ns(rh);
2381 }
2382
2383 /*********** END NAME SHORTEN ********************/
2384
2385
2386 /**
2387  * Process result from namestore delegation lookup
2388  * for get authority operation
2389  *
2390  * @param cls the client get auth handle
2391  * @param rh the resolver handle
2392  * @param rd_count number of results (0)
2393  * @param rd data (NULL)
2394  */
2395 void
2396 handle_delegation_result_ns_get_auth(void* cls,
2397                       struct ResolverHandle *rh,
2398                       uint32_t rd_count,
2399                       const struct GNUNET_NAMESTORE_RecordData *rd)
2400 {
2401   struct GetNameAuthorityHandle* nah;
2402   char result[MAX_DNS_NAME_LENGTH];
2403   size_t answer_len;
2404
2405   nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
2406   
2407   /**
2408    * At this point rh->name contains the part of the name
2409    * that we do not have a PKEY in our namestore to resolve.
2410    * The authority chain in the resolver handle is now
2411    * useful to backtrack if needed
2412    */
2413   
2414   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2415              "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2416
2417   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2418              "Building response!\n");
2419   if (is_canonical(rh->name))
2420   {
2421     /**
2422      * We successfully resolved the authority in the ns
2423      * FIXME for our purposes this is fine
2424      * but maybe we want to have an api that also looks
2425      * into the dht (i.e. option in message)
2426      **/
2427     if (strlen(rh->name) > strlen(nah->name))
2428     {
2429       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2430                  "Record name longer than original lookup name... odd!\n");
2431       //FIXME to sth here
2432     }
2433
2434     answer_len = strlen(nah->name) - strlen(rh->name)
2435       + strlen(GNUNET_GNS_TLD) + 1;
2436     memset(result, 0, answer_len);
2437     strcpy(result, nah->name + strlen(rh->name) + 1);
2438
2439     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2440                "Got authority result %s\n", result);
2441     
2442     nah->proc(nah->proc_cls, result);
2443     GNUNET_free(nah);
2444     free_resolver_handle(rh);
2445   }
2446   else
2447   {
2448     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2449                "Unable to resolve authority for remaining %s!\n", rh->name);
2450     nah->proc(nah->proc_cls, "");
2451     GNUNET_free(nah);
2452     free_resolver_handle(rh);
2453   }
2454
2455
2456 }
2457
2458
2459 /**
2460  * Tries to resolve the authority for name
2461  * in our namestore
2462  *
2463  * @param zone the root zone to look up for
2464  * @param name the name to lookup up
2465  * @param proc the processor to call when finished
2466  * @param proc_cls the closure to pass to the processor
2467  */
2468 void
2469 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2470                            const char* name,
2471                            GetAuthorityResultProcessor proc,
2472                            void* proc_cls)
2473 {
2474   struct ResolverHandle *rh;
2475   struct GetNameAuthorityHandle *nah;
2476
2477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478               "Starting authority resolution for %s!\n", name);
2479
2480   nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2481   rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2482   rh->authority = zone;
2483   
2484   if (strcmp(GNUNET_GNS_TLD, name) == 0)
2485   {
2486     strcpy(rh->name, "\0");
2487   }
2488   else
2489   {
2490     memset(rh->name, 0,
2491            strlen(name)-strlen(GNUNET_GNS_TLD));
2492     memcpy(rh->name, name,
2493            strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2494   }
2495
2496   memset(nah->name, 0,
2497          strlen(name)+1);
2498   strcpy(nah->name, name);
2499   
2500   rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2501   rh->authority_chain_tail = rh->authority_chain_head;
2502   rh->authority_chain_head->zone = zone;
2503   rh->proc = &handle_delegation_result_ns_get_auth;
2504   rh->proc_cls = (void*)nah;
2505
2506   nah->proc = proc;
2507   nah->proc_cls = proc_cls;
2508
2509   /* Start delegation resolution in our namestore */
2510   resolve_delegation_ns(rh);
2511
2512 }
2513
2514 /******** END GET AUTHORITY *************/
2515
2516 /* end of gnunet-service-gns_resolver.c */