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