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