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