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