diff options
Diffstat (limited to 'src/peerinfo-tool/plugin_rest_peerinfo.c')
-rw-r--r-- | src/peerinfo-tool/plugin_rest_peerinfo.c | 520 |
1 files changed, 257 insertions, 263 deletions
diff --git a/src/peerinfo-tool/plugin_rest_peerinfo.c b/src/peerinfo-tool/plugin_rest_peerinfo.c index 24af2c193..238946d54 100644 --- a/src/peerinfo-tool/plugin_rest_peerinfo.c +++ b/src/peerinfo-tool/plugin_rest_peerinfo.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @author Martin Schanzenbach | 21 | * @author Martin Schanzenbach |
22 | * @author Philippe Buschmann | 22 | * @author Philippe Buschmann |
@@ -61,7 +61,7 @@ | |||
61 | /** | 61 | /** |
62 | * How long until we time out during address lookup? | 62 | * How long until we time out during address lookup? |
63 | */ | 63 | */ |
64 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | 64 | #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) |
65 | /** | 65 | /** |
66 | * The configuration handle | 66 | * The configuration handle |
67 | */ | 67 | */ |
@@ -75,8 +75,7 @@ static char* allow_methods; | |||
75 | /** | 75 | /** |
76 | * @brief struct returned by the initialization function of the plugin | 76 | * @brief struct returned by the initialization function of the plugin |
77 | */ | 77 | */ |
78 | struct Plugin | 78 | struct Plugin { |
79 | { | ||
80 | const struct GNUNET_CONFIGURATION_Handle *cfg; | 79 | const struct GNUNET_CONFIGURATION_Handle *cfg; |
81 | }; | 80 | }; |
82 | 81 | ||
@@ -84,8 +83,7 @@ struct Plugin | |||
84 | /** | 83 | /** |
85 | * Record we keep for each printable address. | 84 | * Record we keep for each printable address. |
86 | */ | 85 | */ |
87 | struct AddressRecord | 86 | struct AddressRecord { |
88 | { | ||
89 | /** | 87 | /** |
90 | * Current address-to-string context (if active, otherwise NULL). | 88 | * Current address-to-string context (if active, otherwise NULL). |
91 | */ | 89 | */ |
@@ -111,8 +109,7 @@ struct AddressRecord | |||
111 | /** | 109 | /** |
112 | * Structure we use to collect printable address information. | 110 | * Structure we use to collect printable address information. |
113 | */ | 111 | */ |
114 | struct PrintContext | 112 | struct PrintContext { |
115 | { | ||
116 | /** | 113 | /** |
117 | * Kept in DLL. | 114 | * Kept in DLL. |
118 | */ | 115 | */ |
@@ -157,7 +154,6 @@ struct PrintContext | |||
157 | * RequestHandle | 154 | * RequestHandle |
158 | */ | 155 | */ |
159 | struct RequestHandle *handle; | 156 | struct RequestHandle *handle; |
160 | |||
161 | }; | 157 | }; |
162 | 158 | ||
163 | /** | 159 | /** |
@@ -173,8 +169,7 @@ static struct PrintContext *pc_tail; | |||
173 | /** | 169 | /** |
174 | * The request handle | 170 | * The request handle |
175 | */ | 171 | */ |
176 | struct RequestHandle | 172 | struct RequestHandle { |
177 | { | ||
178 | /** | 173 | /** |
179 | * JSON temporary array | 174 | * JSON temporary array |
180 | */ | 175 | */ |
@@ -214,7 +209,7 @@ struct RequestHandle | |||
214 | * Rest connection | 209 | * Rest connection |
215 | */ | 210 | */ |
216 | struct GNUNET_REST_RequestHandle *rest_handle; | 211 | struct GNUNET_REST_RequestHandle *rest_handle; |
217 | 212 | ||
218 | /** | 213 | /** |
219 | * Desired timeout for the lookup (default is no timeout). | 214 | * Desired timeout for the lookup (default is no timeout). |
220 | */ | 215 | */ |
@@ -249,7 +244,6 @@ struct RequestHandle | |||
249 | * Reponse code | 244 | * Reponse code |
250 | */ | 245 | */ |
251 | int response_code; | 246 | int response_code; |
252 | |||
253 | }; | 247 | }; |
254 | 248 | ||
255 | 249 | ||
@@ -258,51 +252,51 @@ struct RequestHandle | |||
258 | * @param handle Handle to clean up | 252 | * @param handle Handle to clean up |
259 | */ | 253 | */ |
260 | static void | 254 | static void |
261 | cleanup_handle (void *cls) | 255 | cleanup_handle(void *cls) |
262 | { | 256 | { |
263 | struct RequestHandle *handle = cls; | 257 | struct RequestHandle *handle = cls; |
264 | 258 | ||
265 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 259 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
266 | "Cleaning up\n"); | 260 | "Cleaning up\n"); |
267 | if (NULL != handle->timeout_task) | 261 | if (NULL != handle->timeout_task) |
268 | { | 262 | { |
269 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | 263 | GNUNET_SCHEDULER_cancel(handle->timeout_task); |
270 | handle->timeout_task = NULL; | 264 | handle->timeout_task = NULL; |
271 | } | 265 | } |
272 | if (NULL != handle->url) | 266 | if (NULL != handle->url) |
273 | GNUNET_free (handle->url); | 267 | GNUNET_free(handle->url); |
274 | if (NULL != handle->emsg) | 268 | if (NULL != handle->emsg) |
275 | GNUNET_free (handle->emsg); | 269 | GNUNET_free(handle->emsg); |
276 | if (NULL != handle->address) | 270 | if (NULL != handle->address) |
277 | GNUNET_free ((char*)handle->address); | 271 | GNUNET_free((char*)handle->address); |
278 | if (NULL != handle->expiration_str) | 272 | if (NULL != handle->expiration_str) |
279 | GNUNET_free (handle->expiration_str); | 273 | GNUNET_free(handle->expiration_str); |
280 | if (NULL != handle->pubkey) | 274 | if (NULL != handle->pubkey) |
281 | GNUNET_free (handle->pubkey); | 275 | GNUNET_free(handle->pubkey); |
282 | 276 | ||
283 | if (NULL != handle->temp_array) | 277 | if (NULL != handle->temp_array) |
284 | { | 278 | { |
285 | json_decref(handle->temp_array); | 279 | json_decref(handle->temp_array); |
286 | handle->temp_array = NULL; | 280 | handle->temp_array = NULL; |
287 | } | 281 | } |
288 | if (NULL != handle->response) | 282 | if (NULL != handle->response) |
289 | { | 283 | { |
290 | json_decref(handle->response); | 284 | json_decref(handle->response); |
291 | handle->response = NULL; | 285 | handle->response = NULL; |
292 | } | 286 | } |
293 | 287 | ||
294 | if (NULL != handle->list_it) | 288 | if (NULL != handle->list_it) |
295 | { | 289 | { |
296 | GNUNET_PEERINFO_iterate_cancel(handle->list_it); | 290 | GNUNET_PEERINFO_iterate_cancel(handle->list_it); |
297 | handle->list_it = NULL; | 291 | handle->list_it = NULL; |
298 | } | 292 | } |
299 | if (NULL != handle->peerinfo_handle) | 293 | if (NULL != handle->peerinfo_handle) |
300 | { | 294 | { |
301 | GNUNET_PEERINFO_disconnect(handle->peerinfo_handle); | 295 | GNUNET_PEERINFO_disconnect(handle->peerinfo_handle); |
302 | handle->peerinfo_handle = NULL; | 296 | handle->peerinfo_handle = NULL; |
303 | } | 297 | } |
304 | 298 | ||
305 | GNUNET_free (handle); | 299 | GNUNET_free(handle); |
306 | } | 300 | } |
307 | 301 | ||
308 | 302 | ||
@@ -312,7 +306,7 @@ cleanup_handle (void *cls) | |||
312 | * @param cls the `struct RequestHandle` | 306 | * @param cls the `struct RequestHandle` |
313 | */ | 307 | */ |
314 | static void | 308 | static void |
315 | do_error (void *cls) | 309 | do_error(void *cls) |
316 | { | 310 | { |
317 | struct RequestHandle *handle = cls; | 311 | struct RequestHandle *handle = cls; |
318 | struct MHD_Response *resp; | 312 | struct MHD_Response *resp; |
@@ -322,16 +316,16 @@ do_error (void *cls) | |||
322 | if (NULL == handle->emsg) | 316 | if (NULL == handle->emsg) |
323 | handle->emsg = GNUNET_strdup(GNUNET_REST_PEERINFO_ERROR_UNKNOWN); | 317 | handle->emsg = GNUNET_strdup(GNUNET_REST_PEERINFO_ERROR_UNKNOWN); |
324 | 318 | ||
325 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | 319 | json_object_set_new(json_error, "error", json_string(handle->emsg)); |
326 | 320 | ||
327 | if (0 == handle->response_code) | 321 | if (0 == handle->response_code) |
328 | handle->response_code = MHD_HTTP_OK; | 322 | handle->response_code = MHD_HTTP_OK; |
329 | response = json_dumps (json_error, 0); | 323 | response = json_dumps(json_error, 0); |
330 | resp = GNUNET_REST_create_response (response); | 324 | resp = GNUNET_REST_create_response(response); |
331 | handle->proc (handle->proc_cls, resp, handle->response_code); | 325 | handle->proc(handle->proc_cls, resp, handle->response_code); |
332 | json_decref(json_error); | 326 | json_decref(json_error); |
333 | GNUNET_free(response); | 327 | GNUNET_free(response); |
334 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 328 | GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); |
335 | } | 329 | } |
336 | 330 | ||
337 | 331 | ||
@@ -341,26 +335,26 @@ do_error (void *cls) | |||
341 | * @param cls the `struct RequestHandle` | 335 | * @param cls the `struct RequestHandle` |
342 | */ | 336 | */ |
343 | static void | 337 | static void |
344 | peerinfo_list_finished (void *cls) | 338 | peerinfo_list_finished(void *cls) |
345 | { | 339 | { |
346 | struct RequestHandle *handle = cls; | 340 | struct RequestHandle *handle = cls; |
347 | char *result_str; | 341 | char *result_str; |
348 | struct MHD_Response *resp; | 342 | struct MHD_Response *resp; |
349 | 343 | ||
350 | if (NULL == handle->response) | 344 | if (NULL == handle->response) |
351 | { | 345 | { |
352 | handle->response_code = MHD_HTTP_NOT_FOUND; | 346 | handle->response_code = MHD_HTTP_NOT_FOUND; |
353 | handle->emsg = GNUNET_strdup ("No peers found"); | 347 | handle->emsg = GNUNET_strdup("No peers found"); |
354 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 348 | GNUNET_SCHEDULER_add_now(&do_error, handle); |
355 | return; | 349 | return; |
356 | } | 350 | } |
357 | 351 | ||
358 | result_str = json_dumps (handle->response, 0); | 352 | result_str = json_dumps(handle->response, 0); |
359 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 353 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
360 | resp = GNUNET_REST_create_response (result_str); | 354 | resp = GNUNET_REST_create_response(result_str); |
361 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 355 | handle->proc(handle->proc_cls, resp, MHD_HTTP_OK); |
362 | GNUNET_free_non_null (result_str); | 356 | GNUNET_free_non_null(result_str); |
363 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 357 | GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); |
364 | } | 358 | } |
365 | 359 | ||
366 | 360 | ||
@@ -373,16 +367,16 @@ peerinfo_list_finished (void *cls) | |||
373 | * @return #GNUNET_OK to keep the address and continue | 367 | * @return #GNUNET_OK to keep the address and continue |
374 | */ | 368 | */ |
375 | static int | 369 | static int |
376 | count_address (void *cls, | 370 | count_address(void *cls, |
377 | const struct GNUNET_HELLO_Address *address, | 371 | const struct GNUNET_HELLO_Address *address, |
378 | struct GNUNET_TIME_Absolute expiration) | 372 | struct GNUNET_TIME_Absolute expiration) |
379 | { | 373 | { |
380 | struct PrintContext *pc = cls; | 374 | struct PrintContext *pc = cls; |
381 | 375 | ||
382 | if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) | 376 | if (0 == GNUNET_TIME_absolute_get_remaining(expiration).rel_value_us) |
383 | { | 377 | { |
384 | return GNUNET_OK; /* ignore expired address */ | 378 | return GNUNET_OK; /* ignore expired address */ |
385 | } | 379 | } |
386 | 380 | ||
387 | pc->off++; | 381 | pc->off++; |
388 | return GNUNET_OK; | 382 | return GNUNET_OK; |
@@ -395,7 +389,7 @@ count_address (void *cls, | |||
395 | * @param pc printing context | 389 | * @param pc printing context |
396 | */ | 390 | */ |
397 | static void | 391 | static void |
398 | dump_pc (struct PrintContext *pc) | 392 | dump_pc(struct PrintContext *pc) |
399 | { | 393 | { |
400 | struct RequestHandle *handle; | 394 | struct RequestHandle *handle; |
401 | unsigned int i; | 395 | unsigned int i; |
@@ -411,59 +405,58 @@ dump_pc (struct PrintContext *pc) | |||
411 | response_entry = json_object(); | 405 | response_entry = json_object(); |
412 | 406 | ||
413 | for (i = 0; i < pc->num_addresses; i++) | 407 | for (i = 0; i < pc->num_addresses; i++) |
414 | { | ||
415 | if (NULL != pc->address_list[i].result) | ||
416 | { | 408 | { |
417 | object = json_object (); | 409 | if (NULL != pc->address_list[i].result) |
418 | address = json_string(pc->address_list[i].result); | 410 | { |
419 | expires = json_string( | 411 | object = json_object(); |
420 | GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration)); | 412 | address = json_string(pc->address_list[i].result); |
421 | json_object_set (object, "address", address); | 413 | expires = json_string( |
422 | json_object_set (object, "expires", expires); | 414 | GNUNET_STRINGS_absolute_time_to_string(pc->address_list[i].expiration)); |
423 | 415 | json_object_set(object, "address", address); | |
424 | json_decref(address); | 416 | json_object_set(object, "expires", expires); |
425 | json_decref(expires); | 417 | |
426 | 418 | json_decref(address); | |
427 | json_array_append(temp_array, object); | 419 | json_decref(expires); |
428 | json_decref(object); | 420 | |
429 | GNUNET_free (pc->address_list[i].result); | 421 | json_array_append(temp_array, object); |
422 | json_decref(object); | ||
423 | GNUNET_free(pc->address_list[i].result); | ||
424 | } | ||
430 | } | 425 | } |
431 | } | ||
432 | 426 | ||
433 | if (0 < json_array_size(temp_array)) | 427 | if (0 < json_array_size(temp_array)) |
434 | { | 428 | { |
435 | GNUNET_asprintf(&friend_and_peer, | 429 | GNUNET_asprintf(&friend_and_peer, |
436 | "%s%s", | 430 | "%s%s", |
437 | (GNUNET_YES == pc->friend_only) ? "F2F:" : "", | 431 | (GNUNET_YES == pc->friend_only) ? "F2F:" : "", |
438 | GNUNET_i2s_full (&pc->peer)); | 432 | GNUNET_i2s_full(&pc->peer)); |
439 | friend_and_peer_json = json_string(friend_and_peer); | 433 | friend_and_peer_json = json_string(friend_and_peer); |
440 | json_object_set(response_entry, | 434 | json_object_set(response_entry, |
441 | GNUNET_REST_PEERINFO_PEER, | 435 | GNUNET_REST_PEERINFO_PEER, |
442 | friend_and_peer_json); | 436 | friend_and_peer_json); |
443 | json_object_set(response_entry, | 437 | json_object_set(response_entry, |
444 | GNUNET_REST_PEERINFO_ARRAY, | 438 | GNUNET_REST_PEERINFO_ARRAY, |
445 | temp_array); | 439 | temp_array); |
446 | json_array_append(pc->handle->response, response_entry); | 440 | json_array_append(pc->handle->response, response_entry); |
447 | json_decref(friend_and_peer_json); | 441 | json_decref(friend_and_peer_json); |
448 | GNUNET_free(friend_and_peer); | 442 | GNUNET_free(friend_and_peer); |
449 | } | 443 | } |
450 | 444 | ||
451 | json_decref (temp_array); | 445 | json_decref(temp_array); |
452 | json_decref(response_entry); | 446 | json_decref(response_entry); |
453 | 447 | ||
454 | GNUNET_free_non_null (pc->address_list); | 448 | GNUNET_free_non_null(pc->address_list); |
455 | GNUNET_CONTAINER_DLL_remove (pc_head, | 449 | GNUNET_CONTAINER_DLL_remove(pc_head, |
456 | pc_tail, | 450 | pc_tail, |
457 | pc); | 451 | pc); |
458 | handle = pc->handle; | 452 | handle = pc->handle; |
459 | GNUNET_free (pc); | 453 | GNUNET_free(pc); |
460 | |||
461 | if ( (NULL == pc_head) && | ||
462 | (NULL == handle->list_it) ) | ||
463 | { | ||
464 | GNUNET_SCHEDULER_add_now (&peerinfo_list_finished, handle); | ||
465 | } | ||
466 | 454 | ||
455 | if ((NULL == pc_head) && | ||
456 | (NULL == handle->list_it)) | ||
457 | { | ||
458 | GNUNET_SCHEDULER_add_now(&peerinfo_list_finished, handle); | ||
459 | } | ||
467 | } | 460 | } |
468 | 461 | ||
469 | 462 | ||
@@ -478,31 +471,31 @@ dump_pc (struct PrintContext *pc) | |||
478 | * if #GNUNET_SYSERR: address is invalid | 471 | * if #GNUNET_SYSERR: address is invalid |
479 | */ | 472 | */ |
480 | static void | 473 | static void |
481 | process_resolved_address (void *cls, | 474 | process_resolved_address(void *cls, |
482 | const char *address, | 475 | const char *address, |
483 | int res) | 476 | int res) |
484 | { | 477 | { |
485 | struct AddressRecord *ar = cls; | 478 | struct AddressRecord *ar = cls; |
486 | struct PrintContext *pc = ar->pc; | 479 | struct PrintContext *pc = ar->pc; |
487 | 480 | ||
488 | if (NULL != address) | 481 | if (NULL != address) |
489 | { | ||
490 | if (0 != strlen (address)) | ||
491 | { | 482 | { |
492 | if (NULL != ar->result) | 483 | if (0 != strlen(address)) |
493 | GNUNET_free (ar->result); | 484 | { |
494 | ar->result = GNUNET_strdup (address); | 485 | if (NULL != ar->result) |
486 | GNUNET_free(ar->result); | ||
487 | ar->result = GNUNET_strdup(address); | ||
488 | } | ||
489 | return; | ||
495 | } | 490 | } |
496 | return; | ||
497 | } | ||
498 | ar->atsc = NULL; | 491 | ar->atsc = NULL; |
499 | if (GNUNET_SYSERR == res) | 492 | if (GNUNET_SYSERR == res) |
500 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 493 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
501 | _("Failure: Cannot convert address to string for peer `%s'\n"), | 494 | _("Failure: Cannot convert address to string for peer `%s'\n"), |
502 | GNUNET_i2s (&ar->pc->peer)); | 495 | GNUNET_i2s(&ar->pc->peer)); |
503 | pc->num_addresses++; | 496 | pc->num_addresses++; |
504 | if (pc->num_addresses == pc->address_list_size) | 497 | if (pc->num_addresses == pc->address_list_size) |
505 | dump_pc (ar->pc); | 498 | dump_pc(ar->pc); |
506 | } | 499 | } |
507 | 500 | ||
508 | 501 | ||
@@ -515,33 +508,33 @@ process_resolved_address (void *cls, | |||
515 | * @return #GNUNET_OK to keep the address and continue | 508 | * @return #GNUNET_OK to keep the address and continue |
516 | */ | 509 | */ |
517 | static int | 510 | static int |
518 | print_address (void *cls, | 511 | print_address(void *cls, |
519 | const struct GNUNET_HELLO_Address *address, | 512 | const struct GNUNET_HELLO_Address *address, |
520 | struct GNUNET_TIME_Absolute expiration) | 513 | struct GNUNET_TIME_Absolute expiration) |
521 | { | 514 | { |
522 | struct PrintContext *pc = cls; | 515 | struct PrintContext *pc = cls; |
523 | struct AddressRecord *ar; | 516 | struct AddressRecord *ar; |
524 | 517 | ||
525 | if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) | 518 | if (0 == GNUNET_TIME_absolute_get_remaining(expiration).rel_value_us) |
526 | { | 519 | { |
527 | return GNUNET_OK; /* ignore expired address */ | 520 | return GNUNET_OK; /* ignore expired address */ |
528 | } | 521 | } |
529 | 522 | ||
530 | GNUNET_assert (0 < pc->off); | 523 | GNUNET_assert(0 < pc->off); |
531 | ar = &pc->address_list[--pc->off]; | 524 | ar = &pc->address_list[--pc->off]; |
532 | ar->pc = pc; | 525 | ar->pc = pc; |
533 | ar->expiration = expiration; | 526 | ar->expiration = expiration; |
534 | GNUNET_asprintf (&ar->result, | 527 | GNUNET_asprintf(&ar->result, |
535 | "%s:%u:%u", | 528 | "%s:%u:%u", |
536 | address->transport_name, | 529 | address->transport_name, |
537 | address->address_length, | 530 | address->address_length, |
538 | address->local_info); | 531 | address->local_info); |
539 | ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, | 532 | ar->atsc = GNUNET_TRANSPORT_address_to_string(cfg, |
540 | address, | 533 | address, |
541 | GNUNET_NO, | 534 | GNUNET_NO, |
542 | TIMEOUT, | 535 | TIMEOUT, |
543 | &process_resolved_address, | 536 | &process_resolved_address, |
544 | ar); | 537 | ar); |
545 | return GNUNET_OK; | 538 | return GNUNET_OK; |
546 | } | 539 | } |
547 | 540 | ||
@@ -557,63 +550,63 @@ print_address (void *cls, | |||
557 | */ | 550 | */ |
558 | void | 551 | void |
559 | peerinfo_list_iteration(void *cls, | 552 | peerinfo_list_iteration(void *cls, |
560 | const struct GNUNET_PeerIdentity *peer, | 553 | const struct GNUNET_PeerIdentity *peer, |
561 | const struct GNUNET_HELLO_Message *hello, | 554 | const struct GNUNET_HELLO_Message *hello, |
562 | const char *err_msg) | 555 | const char *err_msg) |
563 | { | 556 | { |
564 | struct RequestHandle *handle = cls; | 557 | struct RequestHandle *handle = cls; |
565 | struct PrintContext *pc; | 558 | struct PrintContext *pc; |
566 | int friend_only; | 559 | int friend_only; |
567 | 560 | ||
568 | if (NULL == handle->response) | 561 | if (NULL == handle->response) |
569 | { | 562 | { |
570 | handle->response = json_array(); | 563 | handle->response = json_array(); |
571 | } | 564 | } |
572 | 565 | ||
573 | if (NULL == peer) | 566 | if (NULL == peer) |
574 | { | ||
575 | handle->list_it = NULL; | ||
576 | handle->emsg = GNUNET_strdup ("Error in communication with peerinfo"); | ||
577 | if (NULL != err_msg) | ||
578 | { | 567 | { |
579 | GNUNET_free(handle->emsg); | 568 | handle->list_it = NULL; |
580 | handle->emsg = GNUNET_strdup (err_msg); | 569 | handle->emsg = GNUNET_strdup("Error in communication with peerinfo"); |
581 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | 570 | if (NULL != err_msg) |
571 | { | ||
572 | GNUNET_free(handle->emsg); | ||
573 | handle->emsg = GNUNET_strdup(err_msg); | ||
574 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
575 | } | ||
576 | if (NULL == pc_head) | ||
577 | GNUNET_SCHEDULER_add_now(&do_error, handle); | ||
578 | return; | ||
582 | } | 579 | } |
583 | if (NULL == pc_head) | ||
584 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
585 | return; | ||
586 | } | ||
587 | if (NULL == hello) | 580 | if (NULL == hello) |
588 | return; | 581 | return; |
589 | 582 | ||
590 | friend_only = GNUNET_NO; | 583 | friend_only = GNUNET_NO; |
591 | if (NULL != hello) | 584 | if (NULL != hello) |
592 | friend_only = GNUNET_HELLO_is_friend_only (hello); | 585 | friend_only = GNUNET_HELLO_is_friend_only(hello); |
593 | 586 | ||
594 | pc = GNUNET_new(struct PrintContext); | 587 | pc = GNUNET_new(struct PrintContext); |
595 | GNUNET_CONTAINER_DLL_insert (pc_head, | 588 | GNUNET_CONTAINER_DLL_insert(pc_head, |
596 | pc_tail, | 589 | pc_tail, |
597 | pc); | 590 | pc); |
598 | pc->peer = *peer; | 591 | pc->peer = *peer; |
599 | pc->friend_only = friend_only; | 592 | pc->friend_only = friend_only; |
600 | pc->handle = handle; | 593 | pc->handle = handle; |
601 | GNUNET_HELLO_iterate_addresses (hello, | 594 | GNUNET_HELLO_iterate_addresses(hello, |
602 | GNUNET_NO, | 595 | GNUNET_NO, |
603 | &count_address, | 596 | &count_address, |
604 | pc); | 597 | pc); |
605 | if (0 == pc->off) | 598 | if (0 == pc->off) |
606 | { | 599 | { |
607 | dump_pc (pc); | 600 | dump_pc(pc); |
608 | return; | 601 | return; |
609 | } | 602 | } |
610 | pc->address_list_size = pc->off; | 603 | pc->address_list_size = pc->off; |
611 | pc->address_list = GNUNET_malloc( | 604 | pc->address_list = GNUNET_malloc( |
612 | sizeof(struct AddressRecord) * pc->off); | 605 | sizeof(struct AddressRecord) * pc->off); |
613 | GNUNET_HELLO_iterate_addresses (hello, | 606 | GNUNET_HELLO_iterate_addresses(hello, |
614 | GNUNET_NO, | 607 | GNUNET_NO, |
615 | &print_address, | 608 | &print_address, |
616 | pc); | 609 | pc); |
617 | } | 610 | } |
618 | 611 | ||
619 | /** | 612 | /** |
@@ -624,9 +617,9 @@ peerinfo_list_iteration(void *cls, | |||
624 | * @param cls the RequestHandle | 617 | * @param cls the RequestHandle |
625 | */ | 618 | */ |
626 | void | 619 | void |
627 | peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle, | 620 | peerinfo_get(struct GNUNET_REST_RequestHandle *con_handle, |
628 | const char* url, | 621 | const char* url, |
629 | void *cls) | 622 | void *cls) |
630 | { | 623 | { |
631 | struct RequestHandle *handle = cls; | 624 | struct RequestHandle *handle = cls; |
632 | struct GNUNET_HashCode key; | 625 | struct GNUNET_HashCode key; |
@@ -636,38 +629,38 @@ peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
636 | char* include_friend_only_str; | 629 | char* include_friend_only_str; |
637 | 630 | ||
638 | include_friend_only = GNUNET_NO; | 631 | include_friend_only = GNUNET_NO; |
639 | GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_FRIEND, | 632 | GNUNET_CRYPTO_hash(GNUNET_REST_PEERINFO_FRIEND, |
640 | strlen (GNUNET_REST_PEERINFO_FRIEND), | 633 | strlen(GNUNET_REST_PEERINFO_FRIEND), |
641 | &key); | 634 | &key); |
642 | if ( GNUNET_YES | 635 | if (GNUNET_YES |
643 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | 636 | == GNUNET_CONTAINER_multihashmap_contains(con_handle->url_param_map, |
644 | &key)) | 637 | &key)) |
645 | { | ||
646 | include_friend_only_str = GNUNET_CONTAINER_multihashmap_get ( | ||
647 | con_handle->url_param_map, &key); | ||
648 | if (0 == strcmp(include_friend_only_str, "yes")) | ||
649 | { | 638 | { |
650 | include_friend_only = GNUNET_YES; | 639 | include_friend_only_str = GNUNET_CONTAINER_multihashmap_get( |
640 | con_handle->url_param_map, &key); | ||
641 | if (0 == strcmp(include_friend_only_str, "yes")) | ||
642 | { | ||
643 | include_friend_only = GNUNET_YES; | ||
644 | } | ||
651 | } | 645 | } |
652 | } | ||
653 | 646 | ||
654 | specific_peer = NULL; | 647 | specific_peer = NULL; |
655 | GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_PEER, | 648 | GNUNET_CRYPTO_hash(GNUNET_REST_PEERINFO_PEER, |
656 | strlen (GNUNET_REST_PEERINFO_PEER), | 649 | strlen(GNUNET_REST_PEERINFO_PEER), |
657 | &key); | 650 | &key); |
658 | if ( GNUNET_YES | 651 | if (GNUNET_YES |
659 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | 652 | == GNUNET_CONTAINER_multihashmap_contains(con_handle->url_param_map, |
660 | &key)) | 653 | &key)) |
661 | { | 654 | { |
662 | //peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key); | 655 | //peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key); |
663 | //specific_peer = GNUNET_PEER_resolve2(peer_id); | 656 | //specific_peer = GNUNET_PEER_resolve2(peer_id); |
664 | } | 657 | } |
665 | 658 | ||
666 | handle->list_it = GNUNET_PEERINFO_iterate(handle->peerinfo_handle, | 659 | handle->list_it = GNUNET_PEERINFO_iterate(handle->peerinfo_handle, |
667 | include_friend_only, | 660 | include_friend_only, |
668 | specific_peer, | 661 | specific_peer, |
669 | &peerinfo_list_iteration, | 662 | &peerinfo_list_iteration, |
670 | handle); | 663 | handle); |
671 | } | 664 | } |
672 | 665 | ||
673 | 666 | ||
@@ -680,20 +673,20 @@ peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
680 | * @param cls the RequestHandle | 673 | * @param cls the RequestHandle |
681 | */ | 674 | */ |
682 | static void | 675 | static void |
683 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | 676 | options_cont(struct GNUNET_REST_RequestHandle *con_handle, |
684 | const char* url, | 677 | const char* url, |
685 | void *cls) | 678 | void *cls) |
686 | { | 679 | { |
687 | struct MHD_Response *resp; | 680 | struct MHD_Response *resp; |
688 | struct RequestHandle *handle = cls; | 681 | struct RequestHandle *handle = cls; |
689 | 682 | ||
690 | //independent of path return all options | 683 | //independent of path return all options |
691 | resp = GNUNET_REST_create_response (NULL); | 684 | resp = GNUNET_REST_create_response(NULL); |
692 | MHD_add_response_header (resp, | 685 | MHD_add_response_header(resp, |
693 | "Access-Control-Allow-Methods", | 686 | "Access-Control-Allow-Methods", |
694 | allow_methods); | 687 | allow_methods); |
695 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 688 | handle->proc(handle->proc_cls, resp, MHD_HTTP_OK); |
696 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 689 | GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); |
697 | return; | 690 | return; |
698 | } | 691 | } |
699 | 692 | ||
@@ -704,23 +697,23 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle, | |||
704 | * @param handle the request handle | 697 | * @param handle the request handle |
705 | */ | 698 | */ |
706 | static void | 699 | static void |
707 | init_cont (struct RequestHandle *handle) | 700 | init_cont(struct RequestHandle *handle) |
708 | { | 701 | { |
709 | struct GNUNET_REST_RequestHandlerError err; | 702 | struct GNUNET_REST_RequestHandlerError err; |
710 | static const struct GNUNET_REST_RequestHandler handlers[] = { | 703 | static const struct GNUNET_REST_RequestHandler handlers[] = { |
711 | {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get}, | 704 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get }, |
712 | {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont}, | 705 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont }, |
713 | GNUNET_REST_HANDLER_END | 706 | GNUNET_REST_HANDLER_END |
714 | }; | 707 | }; |
715 | 708 | ||
716 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | 709 | if (GNUNET_NO == GNUNET_REST_handle_request(handle->rest_handle, |
717 | handlers, | 710 | handlers, |
718 | &err, | 711 | &err, |
719 | handle)) | 712 | handle)) |
720 | { | 713 | { |
721 | handle->response_code = err.error_code; | 714 | handle->response_code = err.error_code; |
722 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 715 | GNUNET_SCHEDULER_add_now(&do_error, handle); |
723 | } | 716 | } |
724 | } | 717 | } |
725 | 718 | ||
726 | 719 | ||
@@ -737,29 +730,29 @@ init_cont (struct RequestHandle *handle) | |||
737 | */ | 730 | */ |
738 | static void | 731 | static void |
739 | rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | 732 | rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, |
740 | GNUNET_REST_ResultProcessor proc, | 733 | GNUNET_REST_ResultProcessor proc, |
741 | void *proc_cls) | 734 | void *proc_cls) |
742 | { | 735 | { |
743 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | 736 | struct RequestHandle *handle = GNUNET_new(struct RequestHandle); |
744 | 737 | ||
745 | handle->response_code = 0; | 738 | handle->response_code = 0; |
746 | handle->timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60); | 739 | handle->timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60); |
747 | handle->proc_cls = proc_cls; | 740 | handle->proc_cls = proc_cls; |
748 | handle->proc = proc; | 741 | handle->proc = proc; |
749 | handle->rest_handle = rest_handle; | 742 | handle->rest_handle = rest_handle; |
750 | 743 | ||
751 | handle->url = GNUNET_strdup (rest_handle->url); | 744 | handle->url = GNUNET_strdup(rest_handle->url); |
752 | if (handle->url[strlen (handle->url)-1] == '/') | 745 | if (handle->url[strlen(handle->url) - 1] == '/') |
753 | handle->url[strlen (handle->url)-1] = '\0'; | 746 | handle->url[strlen(handle->url) - 1] = '\0'; |
754 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | 747 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); |
755 | handle->peerinfo_handle = GNUNET_PEERINFO_connect(cfg); | 748 | handle->peerinfo_handle = GNUNET_PEERINFO_connect(cfg); |
756 | init_cont(handle); | 749 | init_cont(handle); |
757 | handle->timeout_task = | 750 | handle->timeout_task = |
758 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | 751 | GNUNET_SCHEDULER_add_delayed(handle->timeout, |
759 | &do_error, | 752 | &do_error, |
760 | handle); | 753 | handle); |
761 | 754 | ||
762 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | 755 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); |
763 | } | 756 | } |
764 | 757 | ||
765 | 758 | ||
@@ -770,7 +763,7 @@ rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, | |||
770 | * @return NULL on error, otherwise the plugin context | 763 | * @return NULL on error, otherwise the plugin context |
771 | */ | 764 | */ |
772 | void * | 765 | void * |
773 | libgnunet_plugin_rest_peerinfo_init (void *cls) | 766 | libgnunet_plugin_rest_peerinfo_init(void *cls) |
774 | { | 767 | { |
775 | static struct Plugin plugin; | 768 | static struct Plugin plugin; |
776 | struct GNUNET_REST_Plugin *api; | 769 | struct GNUNET_REST_Plugin *api; |
@@ -778,22 +771,22 @@ libgnunet_plugin_rest_peerinfo_init (void *cls) | |||
778 | cfg = cls; | 771 | cfg = cls; |
779 | if (NULL != plugin.cfg) | 772 | if (NULL != plugin.cfg) |
780 | return NULL; /* can only initialize once! */ | 773 | return NULL; /* can only initialize once! */ |
781 | memset (&plugin, 0, sizeof (struct Plugin)); | 774 | memset(&plugin, 0, sizeof(struct Plugin)); |
782 | plugin.cfg = cfg; | 775 | plugin.cfg = cfg; |
783 | api = GNUNET_new (struct GNUNET_REST_Plugin); | 776 | api = GNUNET_new(struct GNUNET_REST_Plugin); |
784 | api->cls = &plugin; | 777 | api->cls = &plugin; |
785 | api->name = GNUNET_REST_API_NS_PEERINFO; | 778 | api->name = GNUNET_REST_API_NS_PEERINFO; |
786 | api->process_request = &rest_process_request; | 779 | api->process_request = &rest_process_request; |
787 | GNUNET_asprintf (&allow_methods, | 780 | GNUNET_asprintf(&allow_methods, |
788 | "%s, %s, %s, %s, %s", | 781 | "%s, %s, %s, %s, %s", |
789 | MHD_HTTP_METHOD_GET, | 782 | MHD_HTTP_METHOD_GET, |
790 | MHD_HTTP_METHOD_POST, | 783 | MHD_HTTP_METHOD_POST, |
791 | MHD_HTTP_METHOD_PUT, | 784 | MHD_HTTP_METHOD_PUT, |
792 | MHD_HTTP_METHOD_DELETE, | 785 | MHD_HTTP_METHOD_DELETE, |
793 | MHD_HTTP_METHOD_OPTIONS); | 786 | MHD_HTTP_METHOD_OPTIONS); |
794 | 787 | ||
795 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 788 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
796 | _("Peerinfo REST API initialized\n")); | 789 | _("Peerinfo REST API initialized\n")); |
797 | return api; | 790 | return api; |
798 | } | 791 | } |
799 | 792 | ||
@@ -805,16 +798,17 @@ libgnunet_plugin_rest_peerinfo_init (void *cls) | |||
805 | * @return always NULL | 798 | * @return always NULL |
806 | */ | 799 | */ |
807 | void * | 800 | void * |
808 | libgnunet_plugin_rest_peerinfo_done (void *cls) | 801 | libgnunet_plugin_rest_peerinfo_done(void *cls) |
809 | { | 802 | { |
810 | struct GNUNET_REST_Plugin *api = cls; | 803 | struct GNUNET_REST_Plugin *api = cls; |
811 | struct Plugin *plugin = api->cls; | 804 | struct Plugin *plugin = api->cls; |
805 | |||
812 | plugin->cfg = NULL; | 806 | plugin->cfg = NULL; |
813 | 807 | ||
814 | GNUNET_free_non_null (allow_methods); | 808 | GNUNET_free_non_null(allow_methods); |
815 | GNUNET_free (api); | 809 | GNUNET_free(api); |
816 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 810 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
817 | "Peerinfo REST plugin is finished\n"); | 811 | "Peerinfo REST plugin is finished\n"); |
818 | return NULL; | 812 | return NULL; |
819 | } | 813 | } |
820 | 814 | ||