diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-10-06 15:54:30 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-10-06 15:54:30 +0000 |
commit | 810bc9ef12ddcc67cfc7cd762759ee13ecd14a8d (patch) | |
tree | e6950609e302c8cee5dd49cd75a890e660dde02a /src/gns/gns_api.c | |
parent | 16f524720ce08aadb35912731217eaeafc690dba (diff) | |
download | gnunet-810bc9ef12ddcc67cfc7cd762759ee13ecd14a8d.tar.gz gnunet-810bc9ef12ddcc67cfc7cd762759ee13ecd14a8d.zip |
- Add reverse resolution with limited functionality
Diffstat (limited to 'src/gns/gns_api.c')
-rw-r--r-- | src/gns/gns_api.c | 203 |
1 files changed, 193 insertions, 10 deletions
diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c index b9b95b7c2..3f6425b42 100644 --- a/src/gns/gns_api.c +++ b/src/gns/gns_api.c | |||
@@ -79,6 +79,49 @@ struct GNUNET_GNS_LookupRequest | |||
79 | 79 | ||
80 | }; | 80 | }; |
81 | 81 | ||
82 | /** | ||
83 | * Handle to a lookup request | ||
84 | */ | ||
85 | struct GNUNET_GNS_ReverseLookupRequest | ||
86 | { | ||
87 | |||
88 | /** | ||
89 | * DLL | ||
90 | */ | ||
91 | struct GNUNET_GNS_ReverseLookupRequest *next; | ||
92 | |||
93 | /** | ||
94 | * DLL | ||
95 | */ | ||
96 | struct GNUNET_GNS_ReverseLookupRequest *prev; | ||
97 | |||
98 | /** | ||
99 | * handle to gns | ||
100 | */ | ||
101 | struct GNUNET_GNS_Handle *gns_handle; | ||
102 | |||
103 | /** | ||
104 | * processor to call on lookup result | ||
105 | */ | ||
106 | GNUNET_GNS_ReverseLookupResultProcessor lookup_proc; | ||
107 | |||
108 | /** | ||
109 | * @e lookup_proc closure | ||
110 | */ | ||
111 | void *proc_cls; | ||
112 | |||
113 | /** | ||
114 | * Envelope with the message for this queue entry. | ||
115 | */ | ||
116 | struct GNUNET_MQ_Envelope *env; | ||
117 | |||
118 | /** | ||
119 | * request id | ||
120 | */ | ||
121 | uint32_t r_id; | ||
122 | |||
123 | }; | ||
124 | |||
82 | 125 | ||
83 | /** | 126 | /** |
84 | * Connection to the GNS service. | 127 | * Connection to the GNS service. |
@@ -107,6 +150,15 @@ struct GNUNET_GNS_Handle | |||
107 | struct GNUNET_GNS_LookupRequest *lookup_tail; | 150 | struct GNUNET_GNS_LookupRequest *lookup_tail; |
108 | 151 | ||
109 | /** | 152 | /** |
153 | * Head of linked list of active reverse lookup requests. | ||
154 | */ | ||
155 | struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_head; | ||
156 | |||
157 | /** | ||
158 | * Tail of linked list of active reverse lookup requests. | ||
159 | */ | ||
160 | struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_tail; | ||
161 | /** | ||
110 | * Reconnect task | 162 | * Reconnect task |
111 | */ | 163 | */ |
112 | struct GNUNET_SCHEDULER_Task *reconnect_task; | 164 | struct GNUNET_SCHEDULER_Task *reconnect_task; |
@@ -180,10 +232,71 @@ mq_error_handler (void *cls, | |||
180 | enum GNUNET_MQ_Error error) | 232 | enum GNUNET_MQ_Error error) |
181 | { | 233 | { |
182 | struct GNUNET_GNS_Handle *handle = cls; | 234 | struct GNUNET_GNS_Handle *handle = cls; |
183 | 235 | LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n", | |
236 | error); | ||
184 | force_reconnect (handle); | 237 | force_reconnect (handle); |
185 | } | 238 | } |
186 | 239 | ||
240 | /** | ||
241 | * Check validity of message received from the GNS service | ||
242 | * | ||
243 | * @param cls the `struct GNUNET_GNS_Handle *` | ||
244 | * @param loookup_msg the incoming message | ||
245 | */ | ||
246 | static int | ||
247 | check_rev_result (void *cls, | ||
248 | const struct ReverseLookupResultMessage *lookup_msg) | ||
249 | { | ||
250 | size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg); | ||
251 | char *name; | ||
252 | |||
253 | name = (char*) &lookup_msg[1]; | ||
254 | if ('\0' != name[mlen-1]) | ||
255 | { | ||
256 | GNUNET_break (0); | ||
257 | return GNUNET_SYSERR; | ||
258 | } | ||
259 | return GNUNET_OK; | ||
260 | } | ||
261 | |||
262 | |||
263 | /** | ||
264 | * Handler for messages received from the GNS service | ||
265 | * | ||
266 | * @param cls the `struct GNUNET_GNS_Handle *` | ||
267 | * @param loookup_msg the incoming message | ||
268 | */ | ||
269 | static void | ||
270 | handle_rev_result (void *cls, | ||
271 | const struct ReverseLookupResultMessage *lookup_msg) | ||
272 | { | ||
273 | struct GNUNET_GNS_Handle *handle = cls; | ||
274 | char *name; | ||
275 | uint32_t r_id = ntohl (lookup_msg->id); | ||
276 | struct GNUNET_GNS_ReverseLookupRequest *rlr; | ||
277 | GNUNET_GNS_ReverseLookupResultProcessor proc; | ||
278 | void *proc_cls; | ||
279 | |||
280 | name = (char*)&lookup_msg[1]; | ||
281 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
282 | "Received reverse lookup reply from GNS service (%s)\n", | ||
283 | name); | ||
284 | for (rlr = handle->rev_lookup_head; NULL != rlr; rlr = rlr->next) | ||
285 | if (rlr->r_id == r_id) | ||
286 | break; | ||
287 | if (NULL == rlr) | ||
288 | return; | ||
289 | proc = rlr->lookup_proc; | ||
290 | proc_cls = rlr->proc_cls; | ||
291 | GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head, | ||
292 | handle->rev_lookup_tail, | ||
293 | rlr); | ||
294 | GNUNET_free (rlr); | ||
295 | proc (proc_cls, | ||
296 | name); | ||
297 | } | ||
298 | |||
299 | |||
187 | 300 | ||
188 | /** | 301 | /** |
189 | * Check validity of message received from the GNS service | 302 | * Check validity of message received from the GNS service |
@@ -269,9 +382,14 @@ reconnect (struct GNUNET_GNS_Handle *handle) | |||
269 | GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT, | 382 | GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT, |
270 | struct LookupResultMessage, | 383 | struct LookupResultMessage, |
271 | handle), | 384 | handle), |
385 | GNUNET_MQ_hd_var_size (rev_result, | ||
386 | GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT, | ||
387 | struct ReverseLookupResultMessage, | ||
388 | handle), | ||
272 | GNUNET_MQ_handler_end () | 389 | GNUNET_MQ_handler_end () |
273 | }; | 390 | }; |
274 | struct GNUNET_GNS_LookupRequest *lh; | 391 | struct GNUNET_GNS_LookupRequest *lh; |
392 | struct GNUNET_GNS_ReverseLookupRequest *rlh; | ||
275 | 393 | ||
276 | GNUNET_assert (NULL == handle->mq); | 394 | GNUNET_assert (NULL == handle->mq); |
277 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 395 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
@@ -286,6 +404,9 @@ reconnect (struct GNUNET_GNS_Handle *handle) | |||
286 | for (lh = handle->lookup_head; NULL != lh; lh = lh->next) | 404 | for (lh = handle->lookup_head; NULL != lh; lh = lh->next) |
287 | GNUNET_MQ_send_copy (handle->mq, | 405 | GNUNET_MQ_send_copy (handle->mq, |
288 | lh->env); | 406 | lh->env); |
407 | for (rlh = handle->rev_lookup_head; NULL != rlh; rlh = rlh->next) | ||
408 | GNUNET_MQ_send_copy (handle->mq, | ||
409 | rlh->env); | ||
289 | } | 410 | } |
290 | 411 | ||
291 | 412 | ||
@@ -331,6 +452,7 @@ GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle) | |||
331 | handle->reconnect_task = NULL; | 452 | handle->reconnect_task = NULL; |
332 | } | 453 | } |
333 | GNUNET_assert (NULL == handle->lookup_head); | 454 | GNUNET_assert (NULL == handle->lookup_head); |
455 | GNUNET_assert (NULL == handle->rev_lookup_head); | ||
334 | GNUNET_free (handle); | 456 | GNUNET_free (handle); |
335 | } | 457 | } |
336 | 458 | ||
@@ -352,6 +474,22 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr) | |||
352 | GNUNET_free (lr); | 474 | GNUNET_free (lr); |
353 | } | 475 | } |
354 | 476 | ||
477 | /** | ||
478 | * Cancel pending reverse lookup request | ||
479 | * | ||
480 | * @param lr the lookup request to cancel | ||
481 | */ | ||
482 | void | ||
483 | GNUNET_GNS_reverse_lookup_cancel (struct GNUNET_GNS_ReverseLookupRequest *lr) | ||
484 | { | ||
485 | struct GNUNET_GNS_Handle *handle = lr->gns_handle; | ||
486 | |||
487 | GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head, | ||
488 | handle->rev_lookup_tail, | ||
489 | lr); | ||
490 | GNUNET_MQ_discard (lr->env); | ||
491 | GNUNET_free (lr); | ||
492 | } | ||
355 | 493 | ||
356 | /** | 494 | /** |
357 | * Perform an asynchronous lookup operation on the GNS. | 495 | * Perform an asynchronous lookup operation on the GNS. |
@@ -368,13 +506,13 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr) | |||
368 | */ | 506 | */ |
369 | struct GNUNET_GNS_LookupRequest* | 507 | struct GNUNET_GNS_LookupRequest* |
370 | GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, | 508 | GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, |
371 | const char *name, | 509 | const char *name, |
372 | const struct GNUNET_CRYPTO_EcdsaPublicKey *zone, | 510 | const struct GNUNET_CRYPTO_EcdsaPublicKey *zone, |
373 | uint32_t type, | 511 | uint32_t type, |
374 | enum GNUNET_GNS_LocalOptions options, | 512 | enum GNUNET_GNS_LocalOptions options, |
375 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key, | 513 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key, |
376 | GNUNET_GNS_LookupResultProcessor proc, | 514 | GNUNET_GNS_LookupResultProcessor proc, |
377 | void *proc_cls) | 515 | void *proc_cls) |
378 | { | 516 | { |
379 | /* IPC to shorten gns names, return shorten_handle */ | 517 | /* IPC to shorten gns names, return shorten_handle */ |
380 | struct LookupMessage *lookup_msg; | 518 | struct LookupMessage *lookup_msg; |
@@ -413,8 +551,8 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, | |||
413 | lookup_msg->shorten_key = *shorten_zone_key; | 551 | lookup_msg->shorten_key = *shorten_zone_key; |
414 | } | 552 | } |
415 | GNUNET_memcpy (&lookup_msg[1], | 553 | GNUNET_memcpy (&lookup_msg[1], |
416 | name, | 554 | name, |
417 | nlen); | 555 | nlen); |
418 | GNUNET_CONTAINER_DLL_insert (handle->lookup_head, | 556 | GNUNET_CONTAINER_DLL_insert (handle->lookup_head, |
419 | handle->lookup_tail, | 557 | handle->lookup_tail, |
420 | lr); | 558 | lr); |
@@ -424,5 +562,50 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, | |||
424 | return lr; | 562 | return lr; |
425 | } | 563 | } |
426 | 564 | ||
565 | /** | ||
566 | * Perform an asynchronous reverse lookup operation on the GNS. | ||
567 | * | ||
568 | * @param handle handle to the GNS service | ||
569 | * @param zone_key zone to find a name for | ||
570 | * @param root_key our zone | ||
571 | * @param proc processor to call on result | ||
572 | * @param proc_cls closure for @a proc | ||
573 | * @return handle to the request | ||
574 | */ | ||
575 | struct GNUNET_GNS_ReverseLookupRequest* | ||
576 | GNUNET_GNS_reverse_lookup (struct GNUNET_GNS_Handle *handle, | ||
577 | const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, | ||
578 | const struct GNUNET_CRYPTO_EcdsaPublicKey *root_key, | ||
579 | GNUNET_GNS_ReverseLookupResultProcessor proc, | ||
580 | void *proc_cls) | ||
581 | { | ||
582 | /* IPC to shorten gns names, return shorten_handle */ | ||
583 | struct ReverseLookupMessage *rev_lookup_msg; | ||
584 | struct GNUNET_GNS_ReverseLookupRequest *lr; | ||
427 | 585 | ||
586 | if ((NULL == zone_key) || (NULL == root_key)) | ||
587 | { | ||
588 | GNUNET_break (0); | ||
589 | return NULL; | ||
590 | } | ||
591 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
592 | "Trying to reverse lookup in GNS\n"); | ||
593 | lr = GNUNET_new (struct GNUNET_GNS_ReverseLookupRequest); | ||
594 | lr->gns_handle = handle; | ||
595 | lr->lookup_proc = proc; | ||
596 | lr->proc_cls = proc_cls; | ||
597 | lr->r_id = handle->r_id_gen++; | ||
598 | lr->env = GNUNET_MQ_msg (rev_lookup_msg, | ||
599 | GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP); | ||
600 | rev_lookup_msg->id = htonl (lr->r_id); | ||
601 | rev_lookup_msg->zone_pkey = *zone_key; | ||
602 | rev_lookup_msg->root_pkey = *root_key; | ||
603 | GNUNET_CONTAINER_DLL_insert (handle->rev_lookup_head, | ||
604 | handle->rev_lookup_tail, | ||
605 | lr); | ||
606 | if (NULL != handle->mq) | ||
607 | GNUNET_MQ_send_copy (handle->mq, | ||
608 | lr->env); | ||
609 | return lr; | ||
610 | } | ||
428 | /* end of gns_api.c */ | 611 | /* end of gns_api.c */ |