-ign
[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, 60)
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 record lookup result
45  *
46  * @param cls the closure
47  * @param rd_count number of results
48  * @param rd result 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 resolution 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  * Resolution status indicator
88  * RSL_RECORD_EXISTS: the name to lookup exists
89  * RSL_RECORD_EXPIRED: the name in the record expired
90  * RSL_TIMED_OUT: resolution timed out
91  * RSL_DELEGATE_VPN: Found VPN delegation
92  * RSL_DELEGATE_NS: Found NS delegation
93  */
94 enum ResolutionStatus
95 {
96   RSL_RECORD_EXISTS = 1,
97   RSL_RECORD_EXPIRED = 2,
98   RSL_TIMED_OUT = 4,
99   RSL_DELEGATE_VPN = 8,
100   RSL_DELEGATE_NS = 16
101 };
102
103 /**
104  * Handle to a currenty pending resolution
105  * a ResolverHandle is passed to, for example
106  * resolve_record_ns to resolve a record in the namestore.
107  * On result (positive or negative) the ResolutionResultProcessor
108  * is called.
109  * If a timeout is set timeout_cont will be called.
110  * If no timeout is set (ie timeout forever) then background resolutions
111  * might be triggered.
112  */
113 struct ResolverHandle
114 {
115   /* The name to resolve */
116   char name[MAX_DNS_NAME_LENGTH];
117
118   /* has this query been answered? how many matches */
119   int answered;
120
121   /* Use only cache */
122   int only_cached;
123
124   /* the authoritative zone to query */
125   struct GNUNET_CRYPTO_ShortHashCode authority;
126
127   /* the name of the authoritative zone to query */
128   char authority_name[MAX_DNS_LABEL_LENGTH];
129
130   /* a handle for dht lookups. should be NULL if no lookups are in progress */
131   struct GNUNET_DHT_GetHandle *get_handle;
132
133   /* timeout set for this lookup task */
134   struct GNUNET_TIME_Relative timeout;
135
136   /* a handle to a vpn request */
137   struct GNUNET_VPN_RedirectionRequest *vpn_handle;
138
139   /* a socket for a dns request */
140   struct GNUNET_NETWORK_Handle *dns_sock;
141
142   /* a synthesized dns name */
143   char dns_name[MAX_DNS_NAME_LENGTH];
144
145   /* the address of the DNS server FIXME not needed? */
146   struct sockaddr_in dns_addr;
147
148   /* select task for DNS */
149   GNUNET_SCHEDULER_TaskIdentifier dns_read_task;
150
151   /* pointer to raw dns query payload FIXME needs to be freed/NULL */
152   char *dns_raw_packet;
153
154   /* size of the raw dns query */
155   size_t dns_raw_packet_size;
156
157   /* timeout task for the lookup */
158   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
159
160   /* continuation to call on timeout */
161   GNUNET_SCHEDULER_Task timeout_cont;
162
163   /* closure for timeout cont */
164   void* timeout_cont_cls;
165
166   /* called when resolution phase finishes */
167   ResolutionResultProcessor proc;
168   
169   /* closure passed to proc */
170   void* proc_cls;
171
172   /* DLL to store the authority chain */
173   struct AuthorityChain *authority_chain_head;
174
175   /* DLL to store the authority chain */
176   struct AuthorityChain *authority_chain_tail;
177
178   /* status of the resolution result */
179   enum ResolutionStatus status;
180
181   /* The provate local zone of this request */
182   struct GNUNET_CRYPTO_ShortHashCode private_local_zone;
183
184   /**
185    * private key of an/our authoritative zone
186    * can be NULL but automatical PKEY import will not work
187    */
188   struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
189
190   /**
191    * the heap node associated with this lookup, null if timeout is set
192    * used for DHT background lookups.
193    */
194   struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
195
196   /**
197    * Id for resolution process
198    */
199   unsigned long long id;
200
201 };
202
203
204 /**
205  * Handle to a record lookup
206  */
207 struct RecordLookupHandle
208 {
209   /* the record type to look up */
210   enum GNUNET_GNS_RecordType record_type;
211
212   /* the name to look up */
213   char name[MAX_DNS_NAME_LENGTH];
214
215   /* Method to call on record resolution result */
216   RecordLookupProcessor proc;
217
218   /* closure to pass to proc */
219   void* proc_cls;
220
221 };
222
223
224 /**
225  * Handle to a shorten context
226  */
227 struct NameShortenHandle
228 {
229   /* Method to call on shorten result */
230   ShortenResultProcessor proc;
231
232   /* closure to pass to proc */
233   void* proc_cls;
234
235   /* result of shorten */
236   char result[MAX_DNS_NAME_LENGTH];
237
238   /* root zone */
239   struct GNUNET_CRYPTO_ShortHashCode *root_zone;
240
241   /* private zone */
242   struct GNUNET_CRYPTO_ShortHashCode *private_zone;
243
244   /* name of private zone */
245   char private_zone_name[MAX_DNS_LABEL_LENGTH];
246
247   /* shorten zone */
248   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone;
249
250   /* name of shorten zone */
251   char shorten_zone_name[MAX_DNS_LABEL_LENGTH];
252
253 };
254
255 /**
256  * Handle to a get authority context
257  */
258 struct GetNameAuthorityHandle
259 {
260   /* the name to look up authority for */
261   char name[MAX_DNS_NAME_LENGTH];
262   
263   /* Method to call on result */
264   GetAuthorityResultProcessor proc;
265
266   /* closure to pass to proc */
267   void* proc_cls;
268 };
269
270 /**
271  * Handle to a pseu lookup
272  */
273 struct GetPseuAuthorityHandle
274 {
275   /* the name to store the zone under */
276   char name[MAX_DNS_LABEL_LENGTH];
277
278   /* test name to store the zone under */
279   char test_name[MAX_DNS_LABEL_LENGTH];
280   
281   /* the zone of our authority */
282   struct GNUNET_CRYPTO_ShortHashCode our_zone;
283
284   /* the private key of the zone to store the pseu in */
285   struct GNUNET_CRYPTO_RsaPrivateKey *key;
286
287   /* a handle for dht lookups. should be NULL if no lookups are in progress */
288   struct GNUNET_DHT_GetHandle *get_handle;
289
290   /* timeout task for lookup */
291   GNUNET_SCHEDULER_TaskIdentifier timeout;
292
293   /* Head of the authority list */
294   struct AuthorityChain *ahead;
295 };
296
297 /**
298  * Initialize the resolver
299  * MUST be called before other gns_resolver_* methods
300  *
301  * @param nh handle to the namestore
302  * @param dh handle to the dht
303  * @param lz the local zone
304  * @param max_bg_queries maximum amount of background queries
305  * @param ignore_pending ignore records that still require user confirmation
306  *        on lookup
307  * @returns GNUNET_OK on success
308  */
309 int
310 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
311                   struct GNUNET_DHT_Handle *dh,
312                   struct GNUNET_CRYPTO_ShortHashCode lz,
313                   unsigned long long max_bg_queries,
314                   int ignore_pending);
315
316 /**
317  * Cleanup resolver: Terminate pending lookups
318  * 
319  * @param cont continuation to call when finished
320  */
321 void
322 gns_resolver_cleanup(ResolverCleanupContinuation cont);
323
324 /**
325  * Lookup of a record in a specific zone
326  * calls RecordLookupProcessor on result or timeout
327  *
328  * @param zone the root zone
329  * @param pzone the private local zone
330  * @param record_type the record type to look up
331  * @param name the name to look up
332  * @param key optional private key for authority caching
333  * @param timeout timeout for the resolution
334  * @param only_cached GNUNET_NO to only check locally not DHT for performance
335  * @param proc the processor to call
336  * @param cls the closure to pass to proc
337  */
338 void
339 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
340                            struct GNUNET_CRYPTO_ShortHashCode pzone,
341                            uint32_t record_type,
342                            const char* name,
343                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
344                            struct GNUNET_TIME_Relative timeout,
345                            int only_cached,
346                            RecordLookupProcessor proc,
347                            void* cls);
348
349 /**
350  * Shortens a name if possible. If the shortening fails
351  * name will be returned as shortened string. Else
352  * a shorter version of the name will be returned.
353  * There is no guarantee that the shortened name will
354  * actually be canonical/short etc.
355  *
356  * @param zone the root zone to use
357  * @param pzone the private zone to use
358  * @param szone the shorten zone to use
359  * @param name name to shorten
360  * @param private_zone_name name of the private zone
361  * @param shorten_zone_name name of the shorten zone
362  * @param proc the processor to call on shorten result
363  * @param proc_cls the closure to pass to proc
364  */
365 void
366 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode *zone,
367                           struct GNUNET_CRYPTO_ShortHashCode *pzone,
368                           struct GNUNET_CRYPTO_ShortHashCode *szone,
369                           const char* name,
370                           const char* private_zone_name,
371                           const char* shorten_zone_name,
372                           ShortenResultProcessor proc,
373                           void* proc_cls);
374
375 /**
376  * Tries to resolve the authority for name
377  * in our namestore
378  *
379  * @param zone the root zone to look up for
380  * @param pzone the private local zone
381  * @param name the name to lookup up
382  * @param proc the processor to call when finished
383  * @param proc_cls the closure to pass to the processor
384  */
385 void
386 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
387                            struct GNUNET_CRYPTO_ShortHashCode pzone,
388                            const char* name,
389                            GetAuthorityResultProcessor proc,
390                            void* proc_cls);
391
392 /**
393  * Generic function to check for TLDs
394  *
395  * @param name the name to check
396  * @param tld the tld to check
397  * @return GNUNET_YES or GNUNET_NO
398  */
399 int
400 is_tld(const char* name, const char* tld);
401
402 /**
403  * Checks for gnunet/zkey
404  */
405 #define is_gnunet_tld(name) is_tld(name, GNUNET_GNS_TLD)
406 #define is_zkey_tld(name) is_tld(name, GNUNET_GNS_TLD_ZKEY)
407
408
409 #endif