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