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