e9cbf0d53d6adab47941d073081ef436856312fd
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.h
1 #ifndef GNS_RESOLVER_H
2 #define GNS_RESOLVER_H
3
4 #include "gns.h"
5 #include "gnunet_dht_service.h"
6
7 #define DHT_OPERATION_TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
8 #define GNUNET_GNS_DEFAULT_LOOKUP_TIMEOUT \
9   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
10 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
11 #define DHT_GNS_REPLICATION_LEVEL 5
12
13 #define GNUNET_GNS_MAX_PARALLEL_LOOKUPS 500
14 #define GNUNET_GNS_MAX_NS_TASKS 500
15
16 /*
17  * DLL to hold the authority chain
18  * we had to pass in the resolution process
19  */
20 struct AuthorityChain
21 {
22   struct AuthorityChain *prev;
23
24   struct AuthorityChain *next;
25   
26   /* the zone hash of the authority */
27   struct GNUNET_CRYPTO_ShortHashCode zone;
28
29   /* (local) name of the authority */
30   char name[MAX_DNS_LABEL_LENGTH];
31
32   /* was the ns entry fresh */
33   int fresh;
34 };
35
36 /* handle to a resolution process */
37 struct ResolverHandle;
38
39 /**
40  * continuation called when cleanup of resolver finishes
41  */
42 typedef void (*ResolverCleanupContinuation) (void);
43
44 /**
45  * processor for a record lookup result
46  *
47  * @param cls the closure
48  * @param rd_count number of results
49  * @param rd result data
50  */
51 typedef void (*RecordLookupProcessor) (void *cls,
52                                   uint32_t rd_count,
53                                   const struct GNUNET_NAMESTORE_RecordData *rd);
54
55
56 /**
57  * processor for a shorten result
58  *
59  * @param cls the closure
60  * @param name shortened name
61  */
62 typedef void (*ShortenResultProcessor) (void *cls, const char* name);
63
64
65 /**
66  * processor for an authority result
67  *
68  * @param cls the closure
69  * @param name name
70  */
71 typedef void (*GetAuthorityResultProcessor) (void *cls, const char* name);
72
73 /**
74  * processor for a resolution result
75  *
76  * @param cls the closure
77  * @param rh the resolution handle
78  * @param rd_count number of results
79  * @param rd result data
80  */
81 typedef void (*ResolutionResultProcessor) (void *cls,
82                                   struct ResolverHandle *rh,
83                                   uint32_t rd_count,
84                                   const struct GNUNET_NAMESTORE_RecordData *rd);
85
86
87 /**
88  * Resolution status indicator
89  * RSL_RECORD_EXISTS: the name to lookup exists
90  * RSL_RECORD_EXPIRED: the name in the record expired
91  * RSL_TIMED_OUT: resolution timed out
92  * RSL_DELEGATE_VPN: Found VPN delegation
93  * RSL_DELEGATE_NS: Found NS delegation
94  * RSL_DELEGATE_PKEY: Found PKEY delegation
95  * RSL_CNAME_FOUND: Found CNAME record
96  * RSL_PKEY_REVOKED: Found PKEY has been revoked
97  */
98 enum ResolutionStatus
99 {
100   RSL_RECORD_EXISTS = 1,
101   RSL_RECORD_EXPIRED = 2,
102   RSL_TIMED_OUT = 4,
103   RSL_DELEGATE_VPN = 8,
104   RSL_DELEGATE_NS = 16,
105   RSL_DELEGATE_PKEY = 32,
106   RSL_CNAME_FOUND = 64,
107   RSL_PKEY_REVOKED = 128
108 };
109
110 /**
111  * Handle to a currenty pending resolution
112  * a ResolverHandle is passed to, for example
113  * resolve_record_ns to resolve a record in the namestore.
114  * On result (positive or negative) the ResolutionResultProcessor
115  * is called.
116  * If a timeout is set timeout_cont will be called.
117  * If no timeout is set (ie timeout forever) then background resolutions
118  * might be triggered.
119  */
120 struct ResolverHandle
121 {
122
123   /* DLL */
124   struct ResolverHandle *next;
125
126   /* DLL */
127   struct ResolverHandle *prev;
128
129   /* The name to resolve */
130   char name[MAX_DNS_NAME_LENGTH];
131
132   /* has this query been answered? how many matches */
133   int answered;
134
135   /* Use only cache */
136   int only_cached;
137
138   /* the authoritative zone to query */
139   struct GNUNET_CRYPTO_ShortHashCode authority;
140
141   /* the name of the authoritative zone to query */
142   char authority_name[MAX_DNS_LABEL_LENGTH];
143
144   /* a handle for dht lookups. should be NULL if no lookups are in progress */
145   struct GNUNET_DHT_GetHandle *get_handle;
146
147   /* timeout set for this lookup task */
148   struct GNUNET_TIME_Relative timeout;
149
150   /* a handle to a vpn request */
151   struct GNUNET_VPN_RedirectionRequest *vpn_handle;
152
153   /* a socket for a dns request */
154   struct GNUNET_NETWORK_Handle *dns_sock;
155
156   /* a synthesized dns name */
157   char dns_name[MAX_DNS_NAME_LENGTH];
158
159   /* the authoritative dns zone */
160   char dns_zone[MAX_DNS_NAME_LENGTH];
161
162   /* the address of the DNS server FIXME not needed? */
163   struct sockaddr_in dns_addr;
164
165   /* handle to the local stub resolver request */
166   struct GNUNET_RESOLVER_RequestHandle *dns_resolver_handle;
167
168   /* select task for DNS */
169   GNUNET_SCHEDULER_TaskIdentifier dns_read_task;
170
171   /* pointer to raw dns query payload FIXME needs to be freed/NULL */
172   char *dns_raw_packet;
173
174   /* size of the raw dns query */
175   size_t dns_raw_packet_size;
176
177   /* timeout task for the lookup */
178   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
179
180   /* continuation to call on timeout */
181   GNUNET_SCHEDULER_Task timeout_cont;
182
183   /* closure for timeout cont */
184   void* timeout_cont_cls;
185
186   /* called when resolution phase finishes */
187   ResolutionResultProcessor proc;
188   
189   /* closure passed to proc */
190   void* proc_cls;
191
192   /* DLL to store the authority chain */
193   struct AuthorityChain *authority_chain_head;
194
195   /* DLL to store the authority chain */
196   struct AuthorityChain *authority_chain_tail;
197
198   /* status of the resolution result */
199   enum ResolutionStatus status;
200
201   /* The provate local zone of this request */
202   struct GNUNET_CRYPTO_ShortHashCode private_local_zone;
203
204   /**
205    * private key of an/our authoritative zone
206    * can be NULL but automatical PKEY import will not work
207    */
208   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
209
210   /**
211    * the heap node associated with this lookup, null if timeout is set
212    * used for DHT background lookups.
213    */
214   struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
215
216   /**
217    * Id for resolution process
218    */
219   unsigned long long id;
220
221   /**
222    * Pending Namestore task
223    */
224   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
225
226 };
227
228
229 /**
230  * Handle to a record lookup
231  */
232 struct RecordLookupHandle
233 {
234   /* the record type to look up */
235   enum GNUNET_GNS_RecordType record_type;
236
237   /* the name to look up */
238   char name[MAX_DNS_NAME_LENGTH];
239
240   /* Method to call on record resolution result */
241   RecordLookupProcessor proc;
242
243   /* closure to pass to proc */
244   void* proc_cls;
245
246 };
247
248
249 /**
250  * Handle to a shorten context
251  */
252 struct NameShortenHandle
253 {
254   /* Method to call on shorten result */
255   ShortenResultProcessor proc;
256
257   /* closure to pass to proc */
258   void* proc_cls;
259
260   /* result of shorten */
261   char result[MAX_DNS_NAME_LENGTH];
262
263   /* root zone */
264   struct GNUNET_CRYPTO_ShortHashCode *root_zone;
265
266   /* private zone */
267   struct GNUNET_CRYPTO_ShortHashCode *private_zone;
268
269   /* name of private zone */
270   char private_zone_name[MAX_DNS_LABEL_LENGTH];
271
272   /* shorten zone */
273   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone;
274
275   /* name of shorten zone */
276   char shorten_zone_name[MAX_DNS_LABEL_LENGTH];
277
278 };
279
280 /**
281  * Handle to a get authority context
282  */
283 struct GetNameAuthorityHandle
284 {
285   /* the name to look up authority for */
286   char name[MAX_DNS_NAME_LENGTH];
287
288   /* the result */
289   char result[MAX_DNS_NAME_LENGTH];
290   
291   /* Method to call on result */
292   GetAuthorityResultProcessor proc;
293
294   /* closure to pass to proc */
295   void* proc_cls;
296 };
297
298 /**
299  * Handle to a pseu lookup
300  */
301 struct GetPseuAuthorityHandle
302 {
303   /* DLL */
304   struct GetPseuAuthorityHandle *next;
305
306   /* DLL */
307   struct GetPseuAuthorityHandle *prev;
308
309   /* the name to store the zone under */
310   char name[MAX_DNS_LABEL_LENGTH];
311
312   /* test name to store the zone under */
313   char test_name[MAX_DNS_LABEL_LENGTH];
314   
315   /* the zone of our authority */
316   struct GNUNET_CRYPTO_ShortHashCode our_zone;
317
318   /* the private key of the zone to store the pseu in */
319   struct GNUNET_CRYPTO_RsaPrivateKey *key;
320
321   /* a handle for dht lookups. should be NULL if no lookups are in progress */
322   struct GNUNET_DHT_GetHandle *get_handle;
323
324   /* timeout task for lookup */
325   GNUNET_SCHEDULER_TaskIdentifier timeout;
326
327   /* Head of the authority list */
328   struct AuthorityChain *ahead;
329
330   /* handle to namestore request */
331   struct GNUNET_NAMESTORE_QueueEntry* namestore_task;
332 };
333
334 /**
335  * Namestore queue entries in background
336  */
337 struct NamestoreBGTask
338 {
339   /* node in heap */
340   struct GNUNET_CONTAINER_HeapNode *node;
341
342   /* queue entry */
343   struct GNUNET_NAMESTORE_QueueEntry *qe;
344 };
345
346 /**
347  * Initialize the resolver
348  * MUST be called before other gns_resolver_* methods
349  *
350  * @param nh handle to the namestore
351  * @param dh handle to the dht
352  * @param lz the local zone
353  * @param c configuration handle
354  * @param max_bg_queries maximum amount of background queries
355  * @param ignore_pending ignore records that still require user confirmation
356  *        on lookup
357  * @returns GNUNET_OK on success
358  */
359 int
360 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
361                   struct GNUNET_DHT_Handle *dh,
362                   struct GNUNET_CRYPTO_ShortHashCode lz,
363                   const struct GNUNET_CONFIGURATION_Handle *c,
364                   unsigned long long max_bg_queries,
365                   int ignore_pending);
366
367 /**
368  * Cleanup resolver: Terminate pending lookups
369  * 
370  * @param cont continuation to call when finished
371  */
372 void
373 gns_resolver_cleanup(ResolverCleanupContinuation cont);
374
375 /**
376  * Lookup of a record in a specific zone
377  * calls RecordLookupProcessor on result or timeout
378  *
379  * @param zone the root zone
380  * @param pzone the private local zone
381  * @param record_type the record type to look up
382  * @param name the name to look up
383  * @param key optional private key for authority caching
384  * @param timeout timeout for the resolution
385  * @param only_cached GNUNET_NO to only check locally not DHT for performance
386  * @param proc the processor to call
387  * @param cls the closure to pass to proc
388  */
389 void
390 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
391                            struct GNUNET_CRYPTO_ShortHashCode pzone,
392                            uint32_t record_type,
393                            const char* name,
394                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
395                            struct GNUNET_TIME_Relative timeout,
396                            int only_cached,
397                            RecordLookupProcessor proc,
398                            void* cls);
399
400 /**
401  * Shortens a name if possible. If the shortening fails
402  * name will be returned as shortened string. Else
403  * a shorter version of the name will be returned.
404  * There is no guarantee that the shortened name will
405  * actually be canonical/short etc.
406  *
407  * @param zone the root zone to use
408  * @param pzone the private zone to use
409  * @param szone the shorten zone to use
410  * @param name name to shorten
411  * @param private_zone_name name of the private zone
412  * @param shorten_zone_name name of the shorten zone
413  * @param proc the processor to call on shorten result
414  * @param proc_cls the closure to pass to proc
415  */
416 void
417 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode *zone,
418                           struct GNUNET_CRYPTO_ShortHashCode *pzone,
419                           struct GNUNET_CRYPTO_ShortHashCode *szone,
420                           const char* name,
421                           const char* private_zone_name,
422                           const char* shorten_zone_name,
423                           ShortenResultProcessor proc,
424                           void* proc_cls);
425
426 /**
427  * Tries to resolve the authority for name
428  * in our namestore
429  *
430  * @param zone the root zone to look up for
431  * @param pzone the private local zone
432  * @param name the name to lookup up
433  * @param proc the processor to call when finished
434  * @param proc_cls the closure to pass to the processor
435  */
436 void
437 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
438                            struct GNUNET_CRYPTO_ShortHashCode pzone,
439                            const char* name,
440                            GetAuthorityResultProcessor proc,
441                            void* proc_cls);
442
443 /**
444  * Generic function to check for TLDs
445  *
446  * @param name the name to check
447  * @param tld the tld to check
448  * @return GNUNET_YES or GNUNET_NO
449  */
450 int
451 is_tld(const char* name, const char* tld);
452
453 /**
454  * Checks for gnunet/zkey
455  */
456 #define is_gnunet_tld(name) is_tld(name, GNUNET_GNS_TLD)
457 #define is_zkey_tld(name) is_tld(name, GNUNET_GNS_TLD_ZKEY)
458
459
460 #endif