diff options
Diffstat (limited to 'src/service/namecache/namecache_api.c')
-rw-r--r-- | src/service/namecache/namecache_api.c | 564 |
1 files changed, 564 insertions, 0 deletions
diff --git a/src/service/namecache/namecache_api.c b/src/service/namecache/namecache_api.c new file mode 100644 index 000000000..fb4b0e304 --- /dev/null +++ b/src/service/namecache/namecache_api.c | |||
@@ -0,0 +1,564 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2010-2013, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file namecache/namecache_api.c | ||
23 | * @brief API to access the NAMECACHE service | ||
24 | * @author Martin Schanzenbach | ||
25 | * @author Matthias Wachs | ||
26 | * @author Christian Grothoff | ||
27 | */ | ||
28 | |||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_constants.h" | ||
32 | #include "gnunet_gnsrecord_lib.h" | ||
33 | #include "gnunet_signatures.h" | ||
34 | #include "gnunet_namecache_service.h" | ||
35 | #include "namecache.h" | ||
36 | |||
37 | |||
38 | #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-api", __VA_ARGS__) | ||
39 | |||
40 | |||
41 | /** | ||
42 | * An QueueEntry used to store information for a pending | ||
43 | * NAMECACHE record operation | ||
44 | */ | ||
45 | struct GNUNET_NAMECACHE_QueueEntry | ||
46 | { | ||
47 | /** | ||
48 | * Kept in a DLL. | ||
49 | */ | ||
50 | struct GNUNET_NAMECACHE_QueueEntry *next; | ||
51 | |||
52 | /** | ||
53 | * Kept in a DLL. | ||
54 | */ | ||
55 | struct GNUNET_NAMECACHE_QueueEntry *prev; | ||
56 | |||
57 | /** | ||
58 | * Main handle to access the namecache. | ||
59 | */ | ||
60 | struct GNUNET_NAMECACHE_Handle *nsh; | ||
61 | |||
62 | /** | ||
63 | * Continuation to call | ||
64 | */ | ||
65 | GNUNET_NAMECACHE_ContinuationWithStatus cont; | ||
66 | |||
67 | /** | ||
68 | * Closure for @e cont. | ||
69 | */ | ||
70 | void *cont_cls; | ||
71 | |||
72 | /** | ||
73 | * Function to call with the blocks we get back; or NULL. | ||
74 | */ | ||
75 | GNUNET_NAMECACHE_BlockProcessor block_proc; | ||
76 | |||
77 | /** | ||
78 | * Closure for @e block_proc. | ||
79 | */ | ||
80 | void *block_proc_cls; | ||
81 | |||
82 | /** | ||
83 | * The operation id this zone iteration operation has | ||
84 | */ | ||
85 | uint32_t op_id; | ||
86 | }; | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Connection to the NAMECACHE service. | ||
91 | */ | ||
92 | struct GNUNET_NAMECACHE_Handle | ||
93 | { | ||
94 | /** | ||
95 | * Configuration to use. | ||
96 | */ | ||
97 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
98 | |||
99 | /** | ||
100 | * Message queue to service. | ||
101 | */ | ||
102 | struct GNUNET_MQ_Handle *mq; | ||
103 | |||
104 | /** | ||
105 | * Currently pending transmission request (or NULL). | ||
106 | */ | ||
107 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
108 | |||
109 | /** | ||
110 | * Head of pending namecache queue entries | ||
111 | */ | ||
112 | struct GNUNET_NAMECACHE_QueueEntry *op_head; | ||
113 | |||
114 | /** | ||
115 | * Tail of pending namecache queue entries | ||
116 | */ | ||
117 | struct GNUNET_NAMECACHE_QueueEntry *op_tail; | ||
118 | |||
119 | /** | ||
120 | * Reconnect task | ||
121 | */ | ||
122 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
123 | |||
124 | /** | ||
125 | * Delay introduced before we reconnect. | ||
126 | */ | ||
127 | struct GNUNET_TIME_Relative reconnect_delay; | ||
128 | |||
129 | /** | ||
130 | * Should we reconnect to service due to some serious error? | ||
131 | */ | ||
132 | int reconnect; | ||
133 | |||
134 | /** | ||
135 | * The last operation id used for a NAMECACHE operation | ||
136 | */ | ||
137 | uint32_t last_op_id_used; | ||
138 | }; | ||
139 | |||
140 | |||
141 | /** | ||
142 | * Disconnect from service and then reconnect. | ||
143 | * | ||
144 | * @param h our handle | ||
145 | */ | ||
146 | static void | ||
147 | force_reconnect (struct GNUNET_NAMECACHE_Handle *h); | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Find queue entry for the given @a rid. | ||
152 | * | ||
153 | * @param h handle to search | ||
154 | * @param rid request ID to look for | ||
155 | * @return NULL if not found, otherwise the queue entry (removed from the queue) | ||
156 | */ | ||
157 | static struct GNUNET_NAMECACHE_QueueEntry * | ||
158 | find_qe (struct GNUNET_NAMECACHE_Handle *h, | ||
159 | uint32_t rid) | ||
160 | { | ||
161 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
162 | |||
163 | for (qe = h->op_head; qe != NULL; qe = qe->next) | ||
164 | { | ||
165 | if (qe->op_id == rid) | ||
166 | { | ||
167 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
168 | h->op_tail, | ||
169 | qe); | ||
170 | return qe; | ||
171 | } | ||
172 | } | ||
173 | return NULL; | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Handle an incoming message of type | ||
179 | * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE. | ||
180 | * | ||
181 | * @param cls the `struct GNUNET_NAMECACHE_Handle` | ||
182 | * @param msg the message we received | ||
183 | */ | ||
184 | static int | ||
185 | check_lookup_block_response (void *cls, | ||
186 | const struct LookupBlockResponseMessage *msg) | ||
187 | { | ||
188 | /* any length will do, format validation is in handler */ | ||
189 | return GNUNET_OK; | ||
190 | } | ||
191 | |||
192 | |||
193 | /** | ||
194 | * Handle an incoming message of type | ||
195 | * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE. | ||
196 | * | ||
197 | * @param cls the `struct GNUNET_NAMECACHE_Handle` | ||
198 | * @param msg the message we received | ||
199 | */ | ||
200 | static void | ||
201 | handle_lookup_block_response (void *cls, | ||
202 | const struct LookupBlockResponseMessage *msg) | ||
203 | { | ||
204 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
205 | size_t size; | ||
206 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
207 | |||
208 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
209 | "Received LOOKUP_BLOCK_RESPONSE\n"); | ||
210 | qe = find_qe (h, | ||
211 | ntohl (msg->gns_header.r_id)); | ||
212 | if (NULL == qe) | ||
213 | return; | ||
214 | if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us) | ||
215 | { | ||
216 | /* no match found */ | ||
217 | if (NULL != qe->block_proc) | ||
218 | qe->block_proc (qe->block_proc_cls, | ||
219 | NULL); | ||
220 | GNUNET_free (qe); | ||
221 | return; | ||
222 | } | ||
223 | size = ntohs (msg->gns_header.header.size) | ||
224 | - sizeof(struct LookupBlockResponseMessage); | ||
225 | { | ||
226 | char buf[size] GNUNET_ALIGN; | ||
227 | struct GNUNET_GNSRECORD_Block *block; | ||
228 | |||
229 | memset (buf, 0, size); | ||
230 | block = (struct GNUNET_GNSRECORD_Block *) buf; | ||
231 | GNUNET_memcpy (block, | ||
232 | &msg[1], | ||
233 | size); | ||
234 | if (GNUNET_OK != | ||
235 | GNUNET_GNSRECORD_block_verify (block)) | ||
236 | { | ||
237 | GNUNET_break (0); | ||
238 | if (NULL != qe->block_proc) | ||
239 | qe->block_proc (qe->block_proc_cls, | ||
240 | NULL); | ||
241 | force_reconnect (h); | ||
242 | } | ||
243 | else | ||
244 | { | ||
245 | if (NULL != qe->block_proc) | ||
246 | qe->block_proc (qe->block_proc_cls, | ||
247 | block); | ||
248 | } | ||
249 | } | ||
250 | GNUNET_free (qe); | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Handle an incoming message of type | ||
256 | * #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE | ||
257 | * | ||
258 | * @param cls the `struct GNUNET_NAMECACHE_Handle` | ||
259 | * @param msg the message we received | ||
260 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client | ||
261 | */ | ||
262 | static void | ||
263 | handle_block_cache_response (void *cls, | ||
264 | const struct BlockCacheResponseMessage *msg) | ||
265 | { | ||
266 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
267 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
268 | int res; | ||
269 | |||
270 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
271 | "Received BLOCK_CACHE_RESPONSE\n"); | ||
272 | qe = find_qe (h, | ||
273 | ntohl (msg->gns_header.r_id)); | ||
274 | if (NULL == qe) | ||
275 | return; | ||
276 | res = ntohl (msg->op_result); | ||
277 | /* TODO: add actual error message from namecache to response... */ | ||
278 | if (NULL != qe->cont) | ||
279 | qe->cont (qe->cont_cls, | ||
280 | res, | ||
281 | (GNUNET_OK == res) | ||
282 | ? NULL | ||
283 | : _ ("Namecache failed to cache block")); | ||
284 | GNUNET_free (qe); | ||
285 | } | ||
286 | |||
287 | |||
288 | /** | ||
289 | * Generic error handler, called with the appropriate error code and | ||
290 | * the same closure specified at the creation of the message queue. | ||
291 | * Not every message queue implementation supports an error handler. | ||
292 | * | ||
293 | * @param cls closure with the `struct GNUNET_NAMECACHE_Handle *` | ||
294 | * @param error error code | ||
295 | */ | ||
296 | static void | ||
297 | mq_error_handler (void *cls, | ||
298 | enum GNUNET_MQ_Error error) | ||
299 | { | ||
300 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
301 | |||
302 | force_reconnect (h); | ||
303 | } | ||
304 | |||
305 | |||
306 | /** | ||
307 | * Reconnect to namecache service. | ||
308 | * | ||
309 | * @param h the handle to the NAMECACHE service | ||
310 | */ | ||
311 | static void | ||
312 | reconnect (struct GNUNET_NAMECACHE_Handle *h) | ||
313 | { | ||
314 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
315 | GNUNET_MQ_hd_var_size (lookup_block_response, | ||
316 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE, | ||
317 | struct LookupBlockResponseMessage, | ||
318 | h), | ||
319 | GNUNET_MQ_hd_fixed_size (block_cache_response, | ||
320 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE, | ||
321 | struct BlockCacheResponseMessage, | ||
322 | h), | ||
323 | GNUNET_MQ_handler_end () | ||
324 | }; | ||
325 | |||
326 | GNUNET_assert (NULL == h->mq); | ||
327 | h->mq = GNUNET_CLIENT_connect (h->cfg, | ||
328 | "namecache", | ||
329 | handlers, | ||
330 | &mq_error_handler, | ||
331 | h); | ||
332 | } | ||
333 | |||
334 | |||
335 | /** | ||
336 | * Re-establish the connection to the service. | ||
337 | * | ||
338 | * @param cls handle to use to re-connect. | ||
339 | */ | ||
340 | static void | ||
341 | reconnect_task (void *cls) | ||
342 | { | ||
343 | struct GNUNET_NAMECACHE_Handle *h = cls; | ||
344 | |||
345 | h->reconnect_task = NULL; | ||
346 | reconnect (h); | ||
347 | } | ||
348 | |||
349 | |||
350 | /** | ||
351 | * Disconnect from service and then reconnect. | ||
352 | * | ||
353 | * @param h our handle | ||
354 | */ | ||
355 | static void | ||
356 | force_reconnect (struct GNUNET_NAMECACHE_Handle *h) | ||
357 | { | ||
358 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
359 | |||
360 | h->reconnect = GNUNET_NO; | ||
361 | GNUNET_MQ_destroy (h->mq); | ||
362 | h->mq = NULL; | ||
363 | while (NULL != (qe = h->op_head)) | ||
364 | { | ||
365 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
366 | h->op_tail, | ||
367 | qe); | ||
368 | if (NULL != qe->cont) | ||
369 | qe->cont (qe->cont_cls, | ||
370 | GNUNET_SYSERR, | ||
371 | _ ("Error communicating with namecache service")); | ||
372 | GNUNET_free (qe); | ||
373 | } | ||
374 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
375 | "Reconnecting to namecache\n"); | ||
376 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
377 | h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, | ||
378 | &reconnect_task, | ||
379 | h); | ||
380 | } | ||
381 | |||
382 | |||
383 | /** | ||
384 | * Get a fresh operation id to distinguish between namecache requests | ||
385 | * | ||
386 | * @param h the namecache handle | ||
387 | * @return next operation id to use | ||
388 | */ | ||
389 | static uint32_t | ||
390 | get_op_id (struct GNUNET_NAMECACHE_Handle *h) | ||
391 | { | ||
392 | return h->last_op_id_used++; | ||
393 | } | ||
394 | |||
395 | |||
396 | /** | ||
397 | * Initialize the connection with the NAMECACHE service. | ||
398 | * | ||
399 | * @param cfg configuration to use | ||
400 | * @return handle to the GNS service, or NULL on error | ||
401 | */ | ||
402 | struct GNUNET_NAMECACHE_Handle * | ||
403 | GNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
404 | { | ||
405 | struct GNUNET_NAMECACHE_Handle *h; | ||
406 | |||
407 | h = GNUNET_new (struct GNUNET_NAMECACHE_Handle); | ||
408 | h->cfg = cfg; | ||
409 | reconnect (h); | ||
410 | if (NULL == h->mq) | ||
411 | { | ||
412 | GNUNET_free (h); | ||
413 | return NULL; | ||
414 | } | ||
415 | return h; | ||
416 | } | ||
417 | |||
418 | |||
419 | /** | ||
420 | * Disconnect from the namecache service (and free associated | ||
421 | * resources). | ||
422 | * | ||
423 | * @param h handle to the namecache | ||
424 | */ | ||
425 | void | ||
426 | GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h) | ||
427 | { | ||
428 | struct GNUNET_NAMECACHE_QueueEntry *q; | ||
429 | |||
430 | GNUNET_break (NULL == h->op_head); | ||
431 | while (NULL != (q = h->op_head)) | ||
432 | { | ||
433 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
434 | h->op_tail, | ||
435 | q); | ||
436 | GNUNET_free (q); | ||
437 | } | ||
438 | if (NULL != h->mq) | ||
439 | { | ||
440 | GNUNET_MQ_destroy (h->mq); | ||
441 | h->mq = NULL; | ||
442 | } | ||
443 | if (NULL != h->reconnect_task) | ||
444 | { | ||
445 | GNUNET_SCHEDULER_cancel (h->reconnect_task); | ||
446 | h->reconnect_task = NULL; | ||
447 | } | ||
448 | GNUNET_free (h); | ||
449 | } | ||
450 | |||
451 | |||
452 | /** | ||
453 | * Store an item in the namecache. If the item is already present, | ||
454 | * it is replaced with the new record. | ||
455 | * | ||
456 | * @param h handle to the namecache | ||
457 | * @param block block to store | ||
458 | * @param cont continuation to call when done | ||
459 | * @param cont_cls closure for @a cont | ||
460 | * @return handle to abort the request, NULL on error | ||
461 | */ | ||
462 | struct GNUNET_NAMECACHE_QueueEntry * | ||
463 | GNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h, | ||
464 | const struct GNUNET_GNSRECORD_Block *block, | ||
465 | GNUNET_NAMECACHE_ContinuationWithStatus cont, | ||
466 | void *cont_cls) | ||
467 | { | ||
468 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
469 | struct BlockCacheMessage *msg; | ||
470 | struct GNUNET_MQ_Envelope *env; | ||
471 | uint32_t rid; | ||
472 | size_t blen; | ||
473 | |||
474 | if (NULL == h->mq) | ||
475 | return NULL; | ||
476 | blen = GNUNET_GNSRECORD_block_get_size (block); | ||
477 | rid = get_op_id (h); | ||
478 | qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry); | ||
479 | qe->nsh = h; | ||
480 | qe->cont = cont; | ||
481 | qe->cont_cls = cont_cls; | ||
482 | qe->op_id = rid; | ||
483 | GNUNET_CONTAINER_DLL_insert_tail (h->op_head, | ||
484 | h->op_tail, | ||
485 | qe); | ||
486 | /* send msg */ | ||
487 | env = GNUNET_MQ_msg_extra (msg, | ||
488 | blen, | ||
489 | GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE); | ||
490 | msg->gns_header.r_id = htonl (rid); | ||
491 | GNUNET_memcpy (&msg[1], | ||
492 | block, | ||
493 | blen); | ||
494 | GNUNET_MQ_send (h->mq, | ||
495 | env); | ||
496 | return qe; | ||
497 | } | ||
498 | |||
499 | |||
500 | /** | ||
501 | * Get a result for a particular key from the namecache. The processor | ||
502 | * will only be called once. | ||
503 | * | ||
504 | * @param h handle to the namecache | ||
505 | * @param derived_hash hash of zone key combined with name to lookup | ||
506 | * @param proc function to call on the matching block, or with | ||
507 | * NULL if there is no matching block | ||
508 | * @param proc_cls closure for @a proc | ||
509 | * @return a handle that can be used to cancel, NULL on error | ||
510 | */ | ||
511 | struct GNUNET_NAMECACHE_QueueEntry * | ||
512 | GNUNET_NAMECACHE_lookup_block (struct GNUNET_NAMECACHE_Handle *h, | ||
513 | const struct GNUNET_HashCode *derived_hash, | ||
514 | GNUNET_NAMECACHE_BlockProcessor proc, | ||
515 | void *proc_cls) | ||
516 | { | ||
517 | struct GNUNET_NAMECACHE_QueueEntry *qe; | ||
518 | struct LookupBlockMessage *msg; | ||
519 | struct GNUNET_MQ_Envelope *env; | ||
520 | uint32_t rid; | ||
521 | |||
522 | if (NULL == h->mq) | ||
523 | return NULL; | ||
524 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
525 | "Looking for block under %s\n", | ||
526 | GNUNET_h2s (derived_hash)); | ||
527 | rid = get_op_id (h); | ||
528 | qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry); | ||
529 | qe->nsh = h; | ||
530 | qe->block_proc = proc; | ||
531 | qe->block_proc_cls = proc_cls; | ||
532 | qe->op_id = rid; | ||
533 | GNUNET_CONTAINER_DLL_insert_tail (h->op_head, | ||
534 | h->op_tail, | ||
535 | qe); | ||
536 | env = GNUNET_MQ_msg (msg, | ||
537 | GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK); | ||
538 | msg->gns_header.r_id = htonl (rid); | ||
539 | msg->query = *derived_hash; | ||
540 | GNUNET_MQ_send (h->mq, | ||
541 | env); | ||
542 | return qe; | ||
543 | } | ||
544 | |||
545 | |||
546 | /** | ||
547 | * Cancel a namecache operation. The final callback from the | ||
548 | * operation must not have been done yet. | ||
549 | * | ||
550 | * @param qe operation to cancel | ||
551 | */ | ||
552 | void | ||
553 | GNUNET_NAMECACHE_cancel (struct GNUNET_NAMECACHE_QueueEntry *qe) | ||
554 | { | ||
555 | struct GNUNET_NAMECACHE_Handle *h = qe->nsh; | ||
556 | |||
557 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
558 | h->op_tail, | ||
559 | qe); | ||
560 | GNUNET_free (qe); | ||
561 | } | ||
562 | |||
563 | |||
564 | /* end of namecache_api.c */ | ||