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