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