-fix shorten
[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   /* Last record data found */
130   struct GNUNET_NAMESTORE_RecordData rd;
131
132   /* Number of last record data found */
133   unsigned int rd_count;
134
135   /* The name to resolve */
136   char name[MAX_DNS_NAME_LENGTH];
137
138   /* has this query been answered? how many matches */
139   int answered;
140
141   /* Use only cache */
142   int only_cached;
143
144   /* the authoritative zone to query */
145   struct GNUNET_CRYPTO_ShortHashCode authority;
146
147   /* the name of the authoritative zone to query */
148   char authority_name[MAX_DNS_LABEL_LENGTH];
149
150   /* a handle for dht lookups. should be NULL if no lookups are in progress */
151   struct GNUNET_DHT_GetHandle *get_handle;
152
153   /* timeout set for this lookup task */
154   struct GNUNET_TIME_Relative timeout;
155
156   /* a handle to a vpn request */
157   struct GNUNET_VPN_RedirectionRequest *vpn_handle;
158
159   /* a socket for a dns request */
160   struct GNUNET_NETWORK_Handle *dns_sock;
161
162   /* a synthesized dns name */
163   char dns_name[MAX_DNS_NAME_LENGTH];
164
165   /* the authoritative dns zone */
166   char dns_zone[MAX_DNS_NAME_LENGTH];
167
168   /* the address of the DNS server FIXME not needed? */
169   struct sockaddr_in dns_addr;
170
171   /* handle to the local stub resolver request */
172   struct GNUNET_RESOLVER_RequestHandle *dns_resolver_handle;
173
174   /* select task for DNS */
175   GNUNET_SCHEDULER_TaskIdentifier dns_read_task;
176
177   /* pointer to raw dns query payload FIXME needs to be freed/NULL */
178   char *dns_raw_packet;
179
180   /* size of the raw dns query */
181   size_t dns_raw_packet_size;
182
183   /* timeout task for the lookup */
184   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
185
186   /* continuation to call on timeout */
187   GNUNET_SCHEDULER_Task timeout_cont;
188
189   /* closure for timeout cont */
190   void* timeout_cont_cls;
191
192   /* called when resolution phase finishes */
193   ResolutionResultProcessor proc;
194   
195   /* closure passed to proc */
196   void* proc_cls;
197
198   /* DLL to store the authority chain */
199   struct AuthorityChain *authority_chain_head;
200
201   /* DLL to store the authority chain */
202   struct AuthorityChain *authority_chain_tail;
203
204   /* status of the resolution result */
205   enum ResolutionStatus status;
206
207   /* The provate local zone of this request */
208   struct GNUNET_CRYPTO_ShortHashCode private_local_zone;
209
210   /**
211    * private key of an/our authoritative zone
212    * can be NULL but automatical PKEY import will not work
213    */
214   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
215
216   /**
217    * the heap node associated with this lookup, null if timeout is set
218    * used for DHT background lookups.
219    */
220   struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
221
222   /**
223    * Id for resolution process
224    */
225   unsigned long long id;
226
227   /**
228    * Pending Namestore task
229    */
230   struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
231
232 };
233
234
235 /**
236  * Handle to a record lookup
237  */
238 struct RecordLookupHandle
239 {
240   /* the record type to look up */
241   enum GNUNET_GNS_RecordType record_type;
242
243   /* the name to look up */
244   char name[MAX_DNS_NAME_LENGTH];
245
246   /* Method to call on record resolution result */
247   RecordLookupProcessor proc;
248
249   /* closure to pass to proc */
250   void* proc_cls;
251
252 };
253
254
255 /**
256  * Handle to a shorten context
257  */
258 struct NameShortenHandle
259 {
260   /* Method to call on shorten result */
261   ShortenResultProcessor proc;
262
263   /* closure to pass to proc */
264   void* proc_cls;
265
266   /* result of shorten */
267   char result[MAX_DNS_NAME_LENGTH];
268
269   /* root zone */
270   struct GNUNET_CRYPTO_ShortHashCode *root_zone;
271
272   /* private zone */
273   struct GNUNET_CRYPTO_ShortHashCode *private_zone;
274
275   /* name of private zone */
276   char private_zone_name[MAX_DNS_LABEL_LENGTH];
277
278   /* shorten zone */
279   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone;
280
281   /* name of shorten zone */
282   char shorten_zone_name[MAX_DNS_LABEL_LENGTH];
283
284 };
285
286 /**
287  * Handle to a get authority context
288  */
289 struct GetNameAuthorityHandle
290 {
291   /* the name to look up authority for */
292   char name[MAX_DNS_NAME_LENGTH];
293
294   /* the result */
295   char result[MAX_DNS_NAME_LENGTH];
296   
297   /* Method to call on result */
298   GetAuthorityResultProcessor proc;
299
300   /* closure to pass to proc */
301   void* proc_cls;
302 };
303
304 /**
305  * Handle to a pseu lookup
306  */
307 struct GetPseuAuthorityHandle
308 {
309   /* DLL */
310   struct GetPseuAuthorityHandle *next;
311
312   /* DLL */
313   struct GetPseuAuthorityHandle *prev;
314
315   /* the name to store the zone under */
316   char name[MAX_DNS_LABEL_LENGTH];
317
318   /* test name to store the zone under */
319   char test_name[MAX_DNS_LABEL_LENGTH];
320   
321   /* the zone of our authority */
322   struct GNUNET_CRYPTO_ShortHashCode our_zone;
323
324   /* the private key of the zone to store the pseu in */
325   struct GNUNET_CRYPTO_RsaPrivateKey *key;
326
327   /* a handle for dht lookups. should be NULL if no lookups are in progress */
328   struct GNUNET_DHT_GetHandle *get_handle;
329
330   /* timeout task for lookup */
331   GNUNET_SCHEDULER_TaskIdentifier timeout;
332
333   /* Authority to shorten */
334   struct AuthorityChain *auth;
335
336   /* handle to namestore request */
337   struct GNUNET_NAMESTORE_QueueEntry* namestore_task;
338 };
339
340 /**
341  * Namestore queue entries in background
342  */
343 struct NamestoreBGTask
344 {
345   /* node in heap */
346   struct GNUNET_CONTAINER_HeapNode *node;
347
348   /* queue entry */
349   struct GNUNET_NAMESTORE_QueueEntry *qe;
350 };
351
352 /**
353  * Initialize the resolver
354  * MUST be called before other gns_resolver_* methods
355  *
356  * @param nh handle to the namestore
357  * @param dh handle to the dht
358  * @param lz the local zone
359  * @param c configuration handle
360  * @param max_bg_queries maximum amount of background queries
361  * @param ignore_pending ignore records that still require user confirmation
362  *        on lookup
363  * @returns GNUNET_OK on success
364  */
365 int
366 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
367                   struct GNUNET_DHT_Handle *dh,
368                   struct GNUNET_CRYPTO_ShortHashCode lz,
369                   const struct GNUNET_CONFIGURATION_Handle *c,
370                   unsigned long long max_bg_queries,
371                   int ignore_pending);
372
373 /**
374  * Cleanup resolver: Terminate pending lookups
375  * 
376  * @param cont continuation to call when finished
377  */
378 void
379 gns_resolver_cleanup(ResolverCleanupContinuation cont);
380
381 /**
382  * Lookup of a record in a specific zone
383  * calls RecordLookupProcessor on result or timeout
384  *
385  * @param zone the root zone
386  * @param pzone the private local zone
387  * @param record_type the record type to look up
388  * @param name the name to look up
389  * @param key optional private key for authority caching
390  * @param timeout timeout for the resolution
391  * @param only_cached GNUNET_NO to only check locally not DHT for performance
392  * @param proc the processor to call
393  * @param cls the closure to pass to proc
394  */
395 void
396 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
397                            struct GNUNET_CRYPTO_ShortHashCode pzone,
398                            uint32_t record_type,
399                            const char* name,
400                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
401                            struct GNUNET_TIME_Relative timeout,
402                            int only_cached,
403                            RecordLookupProcessor proc,
404                            void* cls);
405
406 /**
407  * Shortens a name if possible. If the shortening fails
408  * name will be returned as shortened string. Else
409  * a shorter version of the name will be returned.
410  * There is no guarantee that the shortened name will
411  * actually be canonical/short etc.
412  *
413  * @param zone the root zone to use
414  * @param pzone the private zone to use
415  * @param szone the shorten zone to use
416  * @param name name to shorten
417  * @param private_zone_name name of the private zone
418  * @param shorten_zone_name name of the shorten zone
419  * @param proc the processor to call on shorten result
420  * @param proc_cls the closure to pass to proc
421  */
422 void
423 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode *zone,
424                           struct GNUNET_CRYPTO_ShortHashCode *pzone,
425                           struct GNUNET_CRYPTO_ShortHashCode *szone,
426                           const char* name,
427                           const char* private_zone_name,
428                           const char* shorten_zone_name,
429                           ShortenResultProcessor proc,
430                           void* proc_cls);
431
432 /**
433  * Tries to resolve the authority for name
434  * in our namestore
435  *
436  * @param zone the root zone to look up for
437  * @param pzone the private local zone
438  * @param name the name to lookup up
439  * @param proc the processor to call when finished
440  * @param proc_cls the closure to pass to the processor
441  */
442 void
443 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
444                            struct GNUNET_CRYPTO_ShortHashCode pzone,
445                            const char* name,
446                            GetAuthorityResultProcessor proc,
447                            void* proc_cls);
448
449 /**
450  * Generic function to check for TLDs
451  *
452  * @param name the name to check
453  * @param tld the tld to check
454  * @return GNUNET_YES or GNUNET_NO
455  */
456 int
457 is_tld(const char* name, const char* tld);
458
459 /**
460  * Checks for gnunet/zkey
461  */
462 #define is_gnunet_tld(name) is_tld(name, GNUNET_GNS_TLD)
463 #define is_zkey_tld(name) is_tld(name, GNUNET_GNS_TLD_ZKEY)
464
465
466 #endif