-fix
[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  * @pram 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
165
166 /**
167  * Handle to a record lookup
168  */
169 struct RecordLookupHandle
170 {
171   /* the record type to look up */
172   enum GNUNET_GNS_RecordType record_type;
173
174   /* the name to look up */
175   char name[MAX_DNS_NAME_LENGTH];
176
177   /* Method to call on record resolution result */
178   RecordLookupProcessor proc;
179
180   /* closure to pass to proc */
181   void* proc_cls;
182
183 };
184
185
186 /**
187  * Handle to a shorten context
188  */
189 struct NameShortenHandle
190 {
191
192
193   /* Method to call on shorten result */
194   ShortenResultProcessor proc;
195
196   /* closure to pass to proc */
197   void* proc_cls;
198
199 };
200
201 /**
202  * Handle to a get authority context
203  */
204 struct GetNameAuthorityHandle
205 {
206   
207   /* the name to look up authority for */
208   char name[MAX_DNS_NAME_LENGTH];
209   
210   /* Method to call on result */
211   GetAuthorityResultProcessor proc;
212
213   /* closure to pass to proc */
214   void* proc_cls;
215
216 };
217
218 /**
219  * Handle to a pseu lookup
220  */
221 struct GetPseuAuthorityHandle
222 {
223   /* the name given from delegation */
224   char name[MAX_DNS_LABEL_LENGTH];
225
226   /* name to store the pseu under */
227   char new_name[MAX_DNS_LABEL_LENGTH];
228   
229   /* the zone of discovered authority */
230   struct GNUNET_CRYPTO_ShortHashCode new_zone;
231
232   /* the zone of our authority */
233   struct GNUNET_CRYPTO_ShortHashCode zone;
234
235   /* the private key of the zone to store the pseu in */
236   struct GNUNET_CRYPTO_RsaPrivateKey *key;
237
238   /* a handle for dht lookups. should be NULL if no lookups are in progress */
239   struct GNUNET_DHT_GetHandle *get_handle;
240
241   /* timeout task for lookup */
242   GNUNET_SCHEDULER_TaskIdentifier timeout;
243 };
244
245 /**
246  * Initialize the resolver
247  * MUST be called before other gns_resolver_* methods
248  *
249  * @param nh handle to the namestore
250  * @param dh handle to the dht
251  * @param local_zone the local zone
252  * @param max_bg_queries maximum amount of background queries
253  * @returns GNUNET_OK on success
254  */
255 int
256 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
257                   struct GNUNET_DHT_Handle *dh,
258                   struct GNUNET_CRYPTO_ShortHashCode local_zone,
259                   unsigned long long max_bg_queries);
260
261 /**
262  * Cleanup resolver: Terminate pending lookups
263  * 
264  * @param cont continuation to call when finished
265  */
266 void
267 gns_resolver_cleanup(ResolverCleanupContinuation cont);
268
269 /**
270  * Lookup of a record in a specific zone
271  * calls lookup result processor on result
272  *
273  * @param zone the root zone
274  * @param record_type the record type to look up
275  * @param name the name to look up
276  * @param key optional private key for authority caching
277  * @param timeout timeout for the resolution
278  * @param proc the processor to call
279  * @param cls the closure to pass to proc
280  */
281 void
282 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
283                            uint32_t record_type,
284                            const char* name,
285                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
286                            struct GNUNET_TIME_Relative timeout,
287                            RecordLookupProcessor proc,
288                            void* cls);
289
290 /**
291  * Shortens a name if possible. If the shortening fails
292  * name will be returned as shortened string. Else
293  * a shorter version of the name will be returned.
294  * There is no guarantee that the shortened name will
295  * actually be canonical/short etc.
296  *
297  * @param zone the zone to perform the operation in
298  * @param name name to shorten
299  * @param proc the processor to call on shorten result
300  * @param proc_cls teh closure to pass to proc
301  */
302 void
303 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
304                           const char* name,
305                           ShortenResultProcessor proc,
306                           void* cls);
307
308 /**
309  * Tries to resolve the authority for name
310  * in our namestore
311  *
312  * @param zone the root zone to look up for
313  * @param name the name to lookup up
314  * @param proc the processor to call when finished
315  * @param cls the closure to pass to the processor
316  */
317 void
318 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
319                            const char* name,
320                            GetAuthorityResultProcessor proc,
321                            void* cls);
322
323 /**
324  * Generic function to check for TLDs
325  *
326  * @param name the name to check
327  * @param tld the tld to check
328  * @return GNUNET_YES or GNUNET_NO
329  */
330 int
331 is_tld(const char* name, const char* tld);
332
333 /**
334  * Checks for gnunet/zkey
335  */
336 #define is_gnunet_tld(name) is_tld(name, GNUNET_GNS_TLD)
337 #define is_zkey_tld(name) is_tld(name, GNUNET_GNS_TLD_ZKEY)
338
339
340 #endif