-timeouts, parallel lookups
[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 /**
40  * processor for a resultion result
41  *
42  * @param cls the closure
43  * @param rh the resolution handle
44  * @param rd_count number of results
45  * @pram rd resukt data
46  */
47 typedef void (*RecordLookupProcessor) (void *cls,
48                                   uint32_t rd_count,
49                                   const struct GNUNET_NAMESTORE_RecordData *rd);
50
51
52 /**
53  * processor for a shorten result
54  *
55  * @param cls the closure
56  * @param name shortened name
57  */
58 typedef void (*ShortenResultProcessor) (void *cls, const char* name);
59
60
61 /**
62  * processor for an authority result
63  *
64  * @param cls the closure
65  * @param name name
66  */
67 typedef void (*GetAuthorityResultProcessor) (void *cls, const char* name);
68
69 /**
70  * processor for a resultion result
71  *
72  * @param cls the closure
73  * @param rh the resolution handle
74  * @param rd_count number of results
75  * @param rd result data
76  */
77 typedef void (*ResolutionResultProcessor) (void *cls,
78                                   struct ResolverHandle *rh,
79                                   uint32_t rd_count,
80                                   const struct GNUNET_NAMESTORE_RecordData *rd);
81
82
83 /**
84  * Resoltion status indicator
85  * EXISTS: the name to lookup exists
86  * EXPIRED: the name in the record expired
87  */
88 enum ResolutionStatus
89 {
90   EXISTS = 1,
91   EXPIRED = 2
92 };
93
94 /**
95  * Handle to a currenty pending resolution
96  */
97 struct ResolverHandle
98 {
99   /* The name to resolve */
100   char name[MAX_DNS_NAME_LENGTH];
101
102   /* has this query been answered? how many matches */
103   int answered;
104
105   /* the authoritative zone to query */
106   struct GNUNET_CRYPTO_ShortHashCode authority;
107
108   /* the name of the authoritative zone to query */
109   char authority_name[MAX_DNS_LABEL_LENGTH];
110
111   /**
112    * we have an authority in namestore that
113    * may be able to resolve
114    */
115   int authority_found;
116
117   /* a handle for dht lookups. should be NULL if no lookups are in progress */
118   struct GNUNET_DHT_GetHandle *get_handle;
119
120   /* timeout set for this lookup task */
121   struct GNUNET_TIME_Relative timeout;
122
123   /* timeout task for the lookup */
124   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
125
126   /* continuation to call on timeout */
127   GNUNET_SCHEDULER_Task timeout_cont;
128
129   /* closure for timeout cont */
130   void* timeout_cont_cls;
131
132   /* called when resolution phase finishes */
133   ResolutionResultProcessor proc;
134   
135   /* closure passed to proc */
136   void* proc_cls;
137
138   /* DLL to store the authority chain */
139   struct AuthorityChain *authority_chain_head;
140
141   /* DLL to store the authority chain */
142   struct AuthorityChain *authority_chain_tail;
143
144   /* status of the resolution result */
145   enum ResolutionStatus status;
146
147   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
148
149   /* the heap node associated with this lookup, null if timeout is set */
150   struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
151
152 };
153
154
155 /**
156  * Handle to a record lookup
157  */
158 struct RecordLookupHandle
159 {
160   /* the record type to look up */
161   enum GNUNET_GNS_RecordType record_type;
162
163   /* the name to look up */
164   char name[MAX_DNS_NAME_LENGTH];
165
166   /* Method to call on record resolution result */
167   RecordLookupProcessor proc;
168
169   /* closure to pass to proc */
170   void* proc_cls;
171
172 };
173
174
175 /**
176  * Handle to a shorten context
177  */
178 struct NameShortenHandle
179 {
180
181
182   /* Method to call on shorten result */
183   ShortenResultProcessor proc;
184
185   /* closure to pass to proc */
186   void* proc_cls;
187
188 };
189
190 /**
191  * Handle to a get authority context
192  */
193 struct GetNameAuthorityHandle
194 {
195   
196   /* the name to look up authority for */
197   char name[MAX_DNS_NAME_LENGTH];
198   
199   /* Method to call on result */
200   GetAuthorityResultProcessor proc;
201
202   /* closure to pass to proc */
203   void* proc_cls;
204
205 };
206
207 /**
208  * Handle to a pseu lookup
209  */
210 struct GetPseuAuthorityHandle
211 {
212   /* the name given from delegation */
213   char name[MAX_DNS_LABEL_LENGTH];
214
215   /* name to store the pseu under */
216   char new_name[MAX_DNS_LABEL_LENGTH];
217   
218   /* the zone of discovered authority */
219   struct GNUNET_CRYPTO_ShortHashCode new_zone;
220
221   /* the zone of our authority */
222   struct GNUNET_CRYPTO_ShortHashCode zone;
223
224   /* the private key of the zone to store the pseu in */
225   struct GNUNET_CRYPTO_RsaPrivateKey *key;
226
227   /* a handle for dht lookups. should be NULL if no lookups are in progress */
228   struct GNUNET_DHT_GetHandle *get_handle;
229
230   /* timeout task for lookup */
231   GNUNET_SCHEDULER_TaskIdentifier timeout;
232 };
233
234 /**
235  * Initialize the resolver
236  *
237  * @param nh handle to the namestore
238  * @param dh handle to the dht
239  * @returns GNUNET_OK on success
240  */
241 int
242 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
243                   struct GNUNET_DHT_Handle *dh);
244
245 /**
246  * Lookup of a record in a specific zone
247  * calls lookup result processor on result
248  *
249  * @param zone the root zone
250  * @param record_type the record type to look up
251  * @param name the name to look up
252  * @param key optional private key for authority caching
253  * @param proc the processor to call
254  * @param cls the closure to pass to proc
255  */
256 void
257 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
258                            uint32_t record_type,
259                            const char* name,
260                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
261                            struct GNUNET_TIME_Relative timeout,
262                            RecordLookupProcessor proc,
263                            void* cls);
264
265 void
266 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
267                           const char* name,
268                           ShortenResultProcessor proc,
269                           void* cls);
270
271 /**
272  * Tries to resolve the authority for name
273  * in our namestore
274  *
275  * @param zone the root zone to look up for
276  * @param name the name to lookup up
277  * @param proc the processor to call when finished
278  * @param cls the closure to pass to the processor
279  */
280 void
281 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
282                            const char* name,
283                            GetAuthorityResultProcessor proc,
284                            void* cls);
285
286 /**
287  * Generic function to check for TLDs
288  *
289  * @param name the name to check
290  * @param tld the tld to check
291  * @return GNUNET_YES or GNUNET_NO
292  */
293 int
294 is_tld(const char* name, const char* tld);
295
296 /**
297  * Checks for gnunet/zkey
298  */
299 #define is_gnunet_tld(name) is_tld(name, GNUNET_GNS_TLD)
300 #define is_zkey_tld(name) is_tld(name, GNUNET_GNS_TLD_ZKEY)
301
302
303 #endif