-fix for #688590: allow user to specify how to install nsslibs
[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 /**
41  * processor for a record lookup result
42  *
43  * @param cls the closure
44  * @param rd_count number of results
45  * @param rd result data
46  */
47 typedef void (*RecordLookupProcessor) (void *cls,
48                                   uint32_t rd_count,
49                                   const struct GNUNET_NAMESTORE_RecordData *rd);
50
51
52 /**
53  * processor for a shorten result
54  *
55  * @param cls the closure
56  * @param name shortened name
57  */
58 typedef void (*ShortenResultProcessor) (void *cls, const char* name);
59
60
61 /**
62  * processor for an authority result
63  *
64  * @param cls the closure
65  * @param name name
66  */
67 typedef void (*GetAuthorityResultProcessor) (void *cls, const char* name);
68
69 /**
70  * processor for a resolution result
71  *
72  * @param cls the closure
73  * @param rh the resolution handle
74  * @param rd_count number of results
75  * @param rd result data
76  */
77 typedef void (*ResolutionResultProcessor) (void *cls,
78                                   struct ResolverHandle *rh,
79                                   uint32_t rd_count,
80                                   const struct GNUNET_NAMESTORE_RecordData *rd);
81
82
83 /**
84  * Resolution status indicator
85  * RSL_RECORD_EXISTS: the name to lookup exists
86  * RSL_RECORD_EXPIRED: the name in the record expired
87  * RSL_TIMED_OUT: resolution timed out
88  * RSL_DELEGATE_VPN: Found VPN delegation
89  * RSL_DELEGATE_NS: Found NS delegation
90  * RSL_DELEGATE_PKEY: Found PKEY delegation
91  * RSL_CNAME_FOUND: Found CNAME record
92  * RSL_PKEY_REVOKED: Found PKEY has been revoked
93  */
94 enum ResolutionStatus
95 {
96   RSL_RECORD_EXISTS = 1,
97   RSL_RECORD_EXPIRED = 2,
98   RSL_TIMED_OUT = 4,
99   RSL_DELEGATE_VPN = 8,
100   RSL_DELEGATE_NS = 16,
101   RSL_DELEGATE_PKEY = 32,
102   RSL_CNAME_FOUND = 64,
103   RSL_PKEY_REVOKED = 128
104 };
105
106 /**
107  * Handle to a currenty pending resolution
108  * a ResolverHandle is passed to, for example
109  * resolve_record_ns to resolve a record in the namestore.
110  * On result (positive or negative) the ResolutionResultProcessor
111  * is called.
112  * If a timeout is set timeout_cont will be called.
113  * If no timeout is set (ie timeout forever) then background resolutions
114  * might be triggered.
115  */
116 struct ResolverHandle
117 {
118
119   /* DLL */
120   struct ResolverHandle *next;
121
122   /* DLL */
123   struct ResolverHandle *prev;
124
125   /* Last record data found */
126   struct GNUNET_NAMESTORE_RecordData rd;
127
128   /* Number of last record data found */
129   unsigned int rd_count;
130
131   /* The name to resolve */
132   char name[MAX_DNS_NAME_LENGTH];
133
134   /* has this query been answered? how many matches */
135   int answered;
136
137   /* Use only cache */
138   int only_cached;
139
140   /* the authoritative zone to query */
141   struct GNUNET_CRYPTO_ShortHashCode authority;
142
143   /* the name of the authoritative zone to query */
144   char authority_name[MAX_DNS_LABEL_LENGTH];
145
146   /* a handle for dht lookups. should be NULL if no lookups are in progress */
147   struct GNUNET_DHT_GetHandle *get_handle;
148
149   /* timeout set for this lookup task */
150   struct GNUNET_TIME_Relative timeout;
151
152   /* a handle to a vpn request */
153   struct GNUNET_VPN_RedirectionRequest *vpn_handle;
154
155   /* a socket for a dns request */
156   struct GNUNET_NETWORK_Handle *dns_sock;
157
158   /* a synthesized dns name */
159   char dns_name[MAX_DNS_NAME_LENGTH];
160
161   /* the authoritative dns zone */
162   char dns_zone[MAX_DNS_NAME_LENGTH];
163
164   /* the address of the DNS server FIXME not needed? */
165   struct sockaddr_in dns_addr;
166
167   /* handle to the local stub resolver request */
168   struct GNUNET_RESOLVER_RequestHandle *dns_resolver_handle;
169
170   /* select task for DNS */
171   GNUNET_SCHEDULER_TaskIdentifier dns_read_task;
172
173   /* pointer to raw dns query payload FIXME needs to be freed/NULL */
174   char *dns_raw_packet;
175
176   /* size of the raw dns query */
177   size_t dns_raw_packet_size;
178
179   /* timeout task for the lookup */
180   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
181
182   /* continuation to call on timeout */
183   GNUNET_SCHEDULER_Task timeout_cont;
184
185   /* closure for timeout cont */
186   void* timeout_cont_cls;
187
188   /* called when resolution phase finishes */
189   ResolutionResultProcessor proc;
190   
191   /* closure passed to proc */
192   void* proc_cls;
193
194   /* DLL to store the authority chain */
195   struct AuthorityChain *authority_chain_head;
196
197   /* DLL to store the authority chain */
198   struct AuthorityChain *authority_chain_tail;
199
200   /* status of the resolution result */
201   enum ResolutionStatus status;
202
203   /* The provate local zone of this request */
204   struct GNUNET_CRYPTO_ShortHashCode private_local_zone;
205
206   /**
207    * private key of an/our authoritative zone
208    * can be NULL but automatical PKEY import will not work
209    */
210   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
211
212   /**
213    * the heap node associated with this lookup, null if timeout is set
214    * used for DHT background lookups.
215    */
216   struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
217
218   /**
219    * Id for resolution process
220    */
221   unsigned long long id;
222
223   /**
224    * Pending Namestore task
225    */
226   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
227
228 };
229
230
231 /**
232  * Handle to a record lookup
233  */
234 struct RecordLookupHandle
235 {
236   /* the record type to look up */
237   enum GNUNET_GNS_RecordType record_type;
238
239   /* the name to look up */
240   char name[MAX_DNS_NAME_LENGTH];
241
242   /* Method to call on record resolution result */
243   RecordLookupProcessor proc;
244
245   /* closure to pass to proc */
246   void* proc_cls;
247
248 };
249
250
251 /**
252  * Handle to a shorten context
253  */
254 struct NameShortenHandle
255 {
256   /* Method to call on shorten result */
257   ShortenResultProcessor proc;
258
259   /* closure to pass to proc */
260   void* proc_cls;
261
262   /* result of shorten */
263   char result[MAX_DNS_NAME_LENGTH];
264
265   /* root zone */
266   struct GNUNET_CRYPTO_ShortHashCode *root_zone;
267
268   /* private zone */
269   struct GNUNET_CRYPTO_ShortHashCode *private_zone;
270
271   /* name of private zone */
272   char private_zone_name[MAX_DNS_LABEL_LENGTH];
273
274   /* shorten zone */
275   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone;
276
277   /* name of shorten zone */
278   char shorten_zone_name[MAX_DNS_LABEL_LENGTH];
279
280 };
281
282 /**
283  * Handle to a get authority context
284  */
285 struct GetNameAuthorityHandle
286 {
287   /* the name to look up authority for */
288   char name[MAX_DNS_NAME_LENGTH];
289
290   /* the result */
291   char result[MAX_DNS_NAME_LENGTH];
292   
293   /* Method to call on result */
294   GetAuthorityResultProcessor proc;
295
296   /* closure to pass to proc */
297   void* proc_cls;
298 };
299
300 /**
301  * Handle to a pseu lookup
302  */
303 struct GetPseuAuthorityHandle
304 {
305   /* DLL */
306   struct GetPseuAuthorityHandle *next;
307
308   /* DLL */
309   struct GetPseuAuthorityHandle *prev;
310
311   /* the name to store the zone under */
312   char name[MAX_DNS_LABEL_LENGTH];
313
314   /* test name to store the zone under */
315   char test_name[MAX_DNS_LABEL_LENGTH];
316   
317   /* the zone of our authority */
318   struct GNUNET_CRYPTO_ShortHashCode our_zone;
319
320   /* the private key of the zone to store the pseu in */
321   struct GNUNET_CRYPTO_RsaPrivateKey *key;
322
323   /* a handle for dht lookups. should be NULL if no lookups are in progress */
324   struct GNUNET_DHT_GetHandle *get_handle;
325
326   /* timeout task for lookup */
327   GNUNET_SCHEDULER_TaskIdentifier timeout;
328
329   /* Authority to shorten */
330   struct AuthorityChain *auth;
331
332   /* handle to namestore request */
333   struct GNUNET_NAMESTORE_QueueEntry* namestore_task;
334 };
335
336 /**
337  * Namestore queue entries in background
338  */
339 struct NamestoreBGTask
340 {
341   /* node in heap */
342   struct GNUNET_CONTAINER_HeapNode *node;
343
344   /* queue entry */
345   struct GNUNET_NAMESTORE_QueueEntry *qe;
346 };
347
348 /**
349  * Initialize the resolver
350  * MUST be called before other gns_resolver_* methods
351  *
352  * @param nh handle to the namestore
353  * @param dh handle to the dht
354  * @param lz the local zone
355  * @param c configuration handle
356  * @param max_bg_queries maximum amount of background queries
357  * @param ignore_pending ignore records that still require user confirmation
358  *        on lookup
359  * @returns GNUNET_OK on success
360  */
361 int
362 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
363                   struct GNUNET_DHT_Handle *dh,
364                   struct GNUNET_CRYPTO_ShortHashCode lz,
365                   const struct GNUNET_CONFIGURATION_Handle *c,
366                   unsigned long long max_bg_queries,
367                   int ignore_pending);
368
369 /**
370  * Cleanup resolver: Terminate pending lookups
371  */
372 void
373 gns_resolver_cleanup(void);
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