-add adv port
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011, 2012 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  * @file gns/gnunet-service-gns_resolver.h
22  * @brief GNUnet GNS service
23  * @author Martin Schanzenbach
24  */
25 #ifndef GNS_RESOLVER_H
26 #define GNS_RESOLVER_H
27
28 #include "gns.h"
29 #include "gnunet_dht_service.h"
30
31 #define DHT_OPERATION_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
32
33 #define GNUNET_GNS_DEFAULT_LOOKUP_TIMEOUT \
34   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35
36 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
37
38 #define DHT_GNS_REPLICATION_LEVEL 5
39
40 #define GNUNET_GNS_MAX_PARALLEL_LOOKUPS 500
41
42 #define GNUNET_GNS_MAX_NS_TASKS 500
43
44 /*
45  * DLL to hold the authority chain
46  * we had to pass in the resolution process
47  */
48 struct AuthorityChain
49 {
50   struct AuthorityChain *prev;
51
52   struct AuthorityChain *next;
53   
54   /**
55    * the zone hash of the authority 
56    */
57   struct GNUNET_CRYPTO_ShortHashCode zone;
58
59   /**
60    * (local) name of the authority 
61    */
62   char name[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
63
64   /**
65    * was the ns entry fresh 
66    */
67   int fresh;
68 };
69
70
71 /**
72  * handle to a resolution process 
73  */
74 struct ResolverHandle;
75
76
77 /**
78  * processor for a record lookup result
79  *
80  * @param cls the closure
81  * @param rd_count number of results
82  * @param rd result data
83  */
84 typedef void (*RecordLookupProcessor) (void *cls,
85                                        uint32_t rd_count,
86                                        const struct GNUNET_NAMESTORE_RecordData *rd);
87
88
89 /**
90  * processor for a shorten result
91  *
92  * @param cls the closure
93  * @param name shortened name
94  */
95 typedef void (*ShortenResultProcessor) (void *cls, 
96                                         const char* name);
97
98
99 /**
100  * processor for an authority result
101  *
102  * @param cls the closure
103  * @param name name of the authority
104  */
105 typedef void (*GetAuthorityResultProcessor) (void *cls, 
106                                              const char* name);
107
108 /**
109  * processor for a resolution result
110  *
111  * @param cls the closure
112  * @param rh the resolution handle
113  * @param rd_count number of results
114  * @param rd result data (array of 'rd_count' records)
115  */
116 typedef void (*ResolutionResultProcessor) (void *cls,
117                                            struct ResolverHandle *rh,
118                                            uint32_t rd_count,
119                                            const struct GNUNET_NAMESTORE_RecordData *rd);
120
121
122 /**
123  * Resolution status indicator
124  */
125 enum ResolutionStatus
126 {
127   /**
128    * the name to lookup exists
129    */
130   RSL_RECORD_EXISTS = 1,
131
132   /**
133    * the name in the record expired
134    */
135   RSL_RECORD_EXPIRED = 2,
136  
137   /**
138    * resolution timed out
139    */
140   RSL_TIMED_OUT = 4,
141  
142   /**
143    * Found VPN delegation
144    */
145   RSL_DELEGATE_VPN = 8,
146  
147   /**
148    * Found NS delegation
149    */
150   RSL_DELEGATE_NS = 16,
151  
152   /**
153    * Found PKEY delegation
154    */
155   RSL_DELEGATE_PKEY = 32,
156   
157   /**
158    * Found CNAME record
159    */
160   RSL_CNAME_FOUND = 64,
161   
162   /**
163    * Found PKEY has been revoked
164    */
165   RSL_PKEY_REVOKED = 128
166 };
167
168 /**
169  * Handle to a currenty pending resolution
170  * a ResolverHandle is passed to, for example
171  * resolve_record_ns to resolve a record in the namestore.
172  * On result (positive or negative) the ResolutionResultProcessor
173  * is called.
174  * If a timeout is set timeout_cont will be called.
175  * If no timeout is set (ie timeout forever) then background resolutions
176  * might be triggered.
177  */
178 struct ResolverHandle
179 {
180
181   /**
182    * DLL 
183    */
184   struct ResolverHandle *next;
185
186   /**
187    * DLL 
188    */
189   struct ResolverHandle *prev;
190
191   /**
192    * Last record data found 
193    */
194   struct GNUNET_NAMESTORE_RecordData rd;
195
196   /**
197    * Number of last record data found 
198    */
199   unsigned int rd_count;
200
201   /**
202    * The name to resolve 
203    */
204   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
205
206   /**
207    * has this query been answered? how many matches 
208    */
209   int answered;
210
211   /**
212    * Use only cache 
213    */
214   int only_cached;
215
216   /**
217    * the authoritative zone to query 
218    */
219   struct GNUNET_CRYPTO_ShortHashCode authority;
220
221   /**
222    * the name of the authoritative zone to query 
223    */
224   char authority_name[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
225
226   /**
227    * a handle for dht lookups. should be NULL if no lookups are in progress 
228    */
229   struct GNUNET_DHT_GetHandle *get_handle;
230
231   /**
232    * timeout set for this lookup task 
233    */
234   struct GNUNET_TIME_Relative timeout;
235
236   /**
237    * a handle to a vpn request 
238    */
239   struct GNUNET_VPN_RedirectionRequest *vpn_handle;
240
241   /**
242    * a socket for a dns request 
243    */
244   struct GNUNET_NETWORK_Handle *dns_sock;
245
246   /**
247    * a synthesized dns name 
248    */
249   char dns_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
250
251   /**
252    * the authoritative dns zone 
253    */
254   char dns_zone[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
255
256   /**
257    * the address of the DNS server FIXME not needed? 
258    */
259   struct sockaddr_in dns_addr;
260
261   /**
262    * handle to the local stub resolver request
263    */
264   struct GNUNET_RESOLVER_RequestHandle *dns_resolver_handle;
265
266   /**
267    * select task for DNS 
268    */
269   GNUNET_SCHEDULER_TaskIdentifier dns_read_task;
270
271   /**
272    * pointer to raw dns query payload FIXME needs to be freed/NULL 
273    */
274   char *dns_raw_packet;
275
276   /**
277    * size of the raw dns query 
278    */
279   size_t dns_raw_packet_size;
280
281   /**
282    * timeout task for the lookup 
283    */
284   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
285
286   /**
287    * continuation to call on timeout 
288    */
289   GNUNET_SCHEDULER_Task timeout_cont;
290
291   /**
292    * closure for timeout cont 
293    */
294   void* timeout_cont_cls;
295
296   /**
297    * called when resolution phase finishes 
298    */
299   ResolutionResultProcessor proc;
300   
301   /**
302    * closure passed to proc 
303    */
304   void* proc_cls;
305
306   /**
307    * DLL to store the authority chain 
308    */
309   struct AuthorityChain *authority_chain_head;
310
311   /**
312    * DLL to store the authority chain 
313    */
314   struct AuthorityChain *authority_chain_tail;
315
316   /**
317    * status of the resolution result 
318    */
319   enum ResolutionStatus status;
320
321   /**
322    * The provate local zone of this request 
323    */
324   struct GNUNET_CRYPTO_ShortHashCode private_local_zone;
325
326   /**
327    * private key of an/our authoritative zone
328    * can be NULL but automatical PKEY import will not work
329    */
330   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
331
332   /**
333    * the heap node associated with this lookup, null if timeout is set
334    * used for DHT background lookups.
335    */
336   struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
337
338   /**
339    * Id for resolution process
340    */
341   unsigned long long id;
342
343   /**
344    * Pending Namestore task
345    */
346   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
347
348 };
349
350
351 /**
352  * Handle to a record lookup
353  */
354 struct RecordLookupHandle
355 {
356   /**
357    * the record type to look up 
358    */
359   enum GNUNET_GNS_RecordType record_type;
360
361   /**
362    * the name to look up 
363    */
364   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
365
366   /**
367    * Method to call on record resolution result 
368    */
369   RecordLookupProcessor proc;
370
371   /**
372    * closure to pass to proc 
373    */
374   void* proc_cls;
375
376 };
377
378
379 /**
380  * Handle to a shorten context
381  */
382 struct NameShortenHandle
383 {
384   /**
385    * Method to call on shorten result 
386    */
387   ShortenResultProcessor proc;
388
389   /**
390    * closure to pass to proc 
391    */
392   void* proc_cls;
393
394   /**
395    * result of shorten 
396    */
397   char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
398
399   /**
400    * root zone 
401    */
402   struct GNUNET_CRYPTO_ShortHashCode *root_zone;
403
404   /**
405    * private zone 
406    */
407   struct GNUNET_CRYPTO_ShortHashCode *private_zone;
408
409   /**
410    * name of private zone 
411    */
412   char private_zone_name[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
413
414   /**
415    * shorten zone 
416    */
417   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone;
418
419   /**
420    * name of shorten zone 
421    */
422   char shorten_zone_name[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
423
424 };
425
426
427 /**
428  * Handle to a get authority context
429  */
430 struct GetNameAuthorityHandle
431 {
432   /**
433    * the name to look up authority for 
434    */
435   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
436
437   /**
438    * the result 
439    */
440   char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
441   
442   /**
443    * Method to call on result 
444    */
445   GetAuthorityResultProcessor proc;
446
447   /**
448    * closure to pass to proc 
449    */
450   void* proc_cls;
451 };
452
453
454 /**
455  * Handle to a pseu lookup
456  */
457 struct GetPseuAuthorityHandle
458 {
459   /**
460    * DLL
461    */
462   struct GetPseuAuthorityHandle *next;
463
464   /**
465    * DLL
466    */
467   struct GetPseuAuthorityHandle *prev;
468
469   /**
470    * the name to store the zone under 
471    */
472   char name[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
473
474   /**
475    * test name to store the zone under 
476    */
477   char test_name[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
478   
479   /**
480    * the zone of our authority 
481    */
482   struct GNUNET_CRYPTO_ShortHashCode our_zone;
483
484   /**
485    * the private key of the zone to store the pseu in 
486    */
487   struct GNUNET_CRYPTO_RsaPrivateKey *key;
488
489   /**
490    * a handle for dht lookups. should be NULL if no lookups are in progress 
491    */
492   struct GNUNET_DHT_GetHandle *get_handle;
493
494   /**
495    * timeout task for lookup 
496    */
497   GNUNET_SCHEDULER_TaskIdentifier timeout;
498
499   /**
500    * Authority to shorten 
501    */
502   struct AuthorityChain *auth;
503
504   /**
505    * handle to namestore request 
506    */
507   struct GNUNET_NAMESTORE_QueueEntry* namestore_task;
508 };
509
510
511 /**
512  * Namestore queue entries in background
513  */
514 struct NamestoreBGTask
515 {
516   /**
517    * node in heap 
518    */
519   struct GNUNET_CONTAINER_HeapNode *node;
520
521   /**
522    * queue entry 
523    */
524   struct GNUNET_NAMESTORE_QueueEntry *qe;
525 };
526
527
528 /**
529  * Initialize the resolver
530  * MUST be called before other gns_resolver_* methods
531  *
532  * @param nh handle to the namestore
533  * @param dh handle to the dht
534  * @param lz the local zone
535  * @param c configuration handle
536  * @param max_bg_queries maximum amount of background queries
537  * @param ignore_pending ignore records that still require user confirmation
538  *        on lookup
539  * @returns GNUNET_OK on success
540  */
541 int
542 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
543                    struct GNUNET_DHT_Handle *dh,
544                    struct GNUNET_CRYPTO_ShortHashCode lz,
545                    const struct GNUNET_CONFIGURATION_Handle *c,
546                    unsigned long long max_bg_queries,
547                    int ignore_pending);
548
549
550 /**
551  * Cleanup resolver: Terminate pending lookups
552  */
553 void
554 gns_resolver_cleanup (void);
555
556
557 /**
558  * Lookup of a record in a specific zone
559  * calls RecordLookupProcessor on result or timeout
560  *
561  * @param zone the root zone
562  * @param pzone the private local zone
563  * @param record_type the record type to look up
564  * @param name the name to look up
565  * @param key optional private key for authority caching
566  * @param timeout timeout for the resolution
567  * @param only_cached GNUNET_NO to only check locally not DHT for performance
568  * @param proc the processor to call
569  * @param cls the closure to pass to proc
570  */
571 void
572 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
573                             struct GNUNET_CRYPTO_ShortHashCode pzone,
574                             uint32_t record_type,
575                             const char* name,
576                             struct GNUNET_CRYPTO_RsaPrivateKey *key,
577                             struct GNUNET_TIME_Relative timeout,
578                             int only_cached,
579                             RecordLookupProcessor proc,
580                             void* cls);
581
582
583 /**
584  * Shortens a name if possible. If the shortening fails
585  * name will be returned as shortened string. Else
586  * a shorter version of the name will be returned.
587  * There is no guarantee that the shortened name will
588  * actually be canonical/short etc.
589  *
590  * @param zone the root zone to use
591  * @param pzone the private zone to use
592  * @param szone the shorten zone to use
593  * @param name name to shorten
594  * @param private_zone_name name of the private zone
595  * @param shorten_zone_name name of the shorten zone
596  * @param proc the processor to call on shorten result
597  * @param proc_cls the closure to pass to proc
598  */
599 void
600 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
601                            struct GNUNET_CRYPTO_ShortHashCode *pzone,
602                            struct GNUNET_CRYPTO_ShortHashCode *szone,
603                            const char* name,
604                            const char* private_zone_name,
605                            const char* shorten_zone_name,
606                            ShortenResultProcessor proc,
607                            void* proc_cls);
608
609
610 /**
611  * Tries to resolve the authority for name
612  * in our namestore
613  *
614  * @param zone the root zone to look up for
615  * @param pzone the private local zone
616  * @param name the name to lookup up
617  * @param proc the processor to call when finished
618  * @param proc_cls the closure to pass to the processor
619  */
620 void
621 gns_resolver_get_authority (struct GNUNET_CRYPTO_ShortHashCode zone,
622                             struct GNUNET_CRYPTO_ShortHashCode pzone,
623                             const char* name,
624                             GetAuthorityResultProcessor proc,
625                             void* proc_cls);
626
627 /**
628  * Generic function to check for TLDs
629  *
630  * @param name the name to check
631  * @param tld the tld to check
632  * @return GNUNET_YES or GNUNET_NO
633  */
634 int
635 is_tld (const char* name, 
636         const char* tld);
637
638
639 /**
640  * Checks for gads/zkey
641  */
642 #define is_gads_tld(name) is_tld(name, GNUNET_GNS_TLD)
643 #define is_zkey_tld(name) is_tld(name, GNUNET_GNS_TLD_ZKEY)
644
645
646 #endif