diff options
Diffstat (limited to 'src/hostlist/gnunet-daemon-hostlist_server.c')
-rw-r--r-- | src/hostlist/gnunet-daemon-hostlist_server.c | 151 |
1 files changed, 42 insertions, 109 deletions
diff --git a/src/hostlist/gnunet-daemon-hostlist_server.c b/src/hostlist/gnunet-daemon-hostlist_server.c index 40820e557..b01dbc09e 100644 --- a/src/hostlist/gnunet-daemon-hostlist_server.c +++ b/src/hostlist/gnunet-daemon-hostlist_server.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2008, 2009, 2010, 2014 GNUnet e.V. | 3 | Copyright (C) 2008, 2009, 2010, 2014, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -101,12 +101,6 @@ static int advertising; | |||
101 | */ | 101 | */ |
102 | static char *hostlist_uri; | 102 | static char *hostlist_uri; |
103 | 103 | ||
104 | /** | ||
105 | * Map of peer identities to `struct GNUNET_CORE_TransmitHandle *` for | ||
106 | * pending hostlist server advertisements. | ||
107 | */ | ||
108 | static struct GNUNET_CONTAINER_MultiPeerMap *advertisements; | ||
109 | |||
110 | 104 | ||
111 | /** | 105 | /** |
112 | * Context for #host_processor(). | 106 | * Context for #host_processor(). |
@@ -436,47 +430,34 @@ access_handler_callback (void *cls, | |||
436 | * @param buf buffer to copy message to | 430 | * @param buf buffer to copy message to |
437 | * @return number of bytes copied to @a buf | 431 | * @return number of bytes copied to @a buf |
438 | */ | 432 | */ |
439 | static size_t | 433 | static void |
440 | adv_transmit_ready (void *cls, | 434 | adv_transmit (struct GNUNET_MQ_Handle *mq) |
441 | size_t size, | ||
442 | void *buf) | ||
443 | { | 435 | { |
444 | const struct GNUNET_PeerIdentity *peer = cls; | ||
445 | static uint64_t hostlist_adv_count; | 436 | static uint64_t hostlist_adv_count; |
446 | size_t transmission_size; | 437 | const void *extra; |
438 | uint64_t flags; | ||
447 | size_t uri_size; /* Including \0 termination! */ | 439 | size_t uri_size; /* Including \0 termination! */ |
448 | struct GNUNET_MessageHeader header; | 440 | struct GNUNET_MessageHeader *header; |
449 | char *cbuf; | 441 | struct GNUNET_MQ_Envelope *env; |
450 | struct GNUNET_CORE_TransmitHandle *th; | 442 | |
451 | 443 | extra = GNUNET_CORE_get_mq_options (GNUNET_YES, | |
452 | th = GNUNET_CONTAINER_multipeermap_get (advertisements, | 444 | GNUNET_CORE_PRIO_BEST_EFFORT, |
453 | peer); | 445 | &flags); |
454 | GNUNET_assert (NULL != th); | ||
455 | GNUNET_assert (GNUNET_YES == | ||
456 | GNUNET_CONTAINER_multipeermap_remove (advertisements, | ||
457 | peer, | ||
458 | th)); | ||
459 | if (NULL == buf) | ||
460 | { | ||
461 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
462 | "Transmission failed, buffer invalid!\n"); | ||
463 | return 0; | ||
464 | } | ||
465 | uri_size = strlen (hostlist_uri) + 1; | 446 | uri_size = strlen (hostlist_uri) + 1; |
466 | transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size; | 447 | env = GNUNET_MQ_msg_extra (header, |
467 | header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT); | 448 | uri_size, |
468 | header.size = htons (transmission_size); | 449 | GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT); |
469 | GNUNET_assert (size >= transmission_size); | 450 | GNUNET_memcpy (&header[1], |
470 | GNUNET_memcpy (buf, | 451 | hostlist_uri, |
471 | &header, | 452 | uri_size); |
472 | sizeof (struct GNUNET_MessageHeader)); | 453 | GNUNET_MQ_env_set_options (env, |
473 | cbuf = buf; | 454 | flags, |
474 | GNUNET_memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)], | 455 | extra); |
475 | hostlist_uri, | 456 | GNUNET_MQ_send (mq, |
476 | uri_size); | 457 | env); |
477 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 458 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
478 | "Sent advertisement message: Copied %u bytes into buffer!\n", | 459 | "Sent advertisement message: Copied %u bytes into buffer!\n", |
479 | (unsigned int) transmission_size); | 460 | (unsigned int) uri_size); |
480 | hostlist_adv_count++; | 461 | hostlist_adv_count++; |
481 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 462 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
482 | " # Sent advertisement message: %llu\n", | 463 | " # Sent advertisement message: %llu\n", |
@@ -484,7 +465,6 @@ adv_transmit_ready (void *cls, | |||
484 | GNUNET_STATISTICS_update (stats, | 465 | GNUNET_STATISTICS_update (stats, |
485 | gettext_noop ("# hostlist advertisements send"), 1, | 466 | gettext_noop ("# hostlist advertisements send"), 1, |
486 | GNUNET_NO); | 467 | GNUNET_NO); |
487 | return transmission_size; | ||
488 | } | 468 | } |
489 | 469 | ||
490 | 470 | ||
@@ -493,78 +473,39 @@ adv_transmit_ready (void *cls, | |||
493 | * | 473 | * |
494 | * @param cls closure | 474 | * @param cls closure |
495 | * @param peer peer identity this notification is about | 475 | * @param peer peer identity this notification is about |
476 | * @param mq queue for transmission to @a peer | ||
477 | * @return NULL (must!) | ||
496 | */ | 478 | */ |
497 | static void | 479 | static void * |
498 | connect_handler (void *cls, | 480 | connect_handler (void *cls, |
499 | const struct GNUNET_PeerIdentity *peer) | 481 | const struct GNUNET_PeerIdentity *peer, |
482 | struct GNUNET_MQ_Handle *mq) | ||
500 | { | 483 | { |
501 | size_t size; | 484 | size_t size; |
502 | struct GNUNET_CORE_TransmitHandle *th; | ||
503 | 485 | ||
504 | if (! advertising) | 486 | if (! advertising) |
505 | return; | 487 | return NULL; |
506 | if (NULL == hostlist_uri) | 488 | if (NULL == hostlist_uri) |
507 | return; | 489 | return NULL; |
508 | size = strlen (hostlist_uri) + 1; | 490 | size = strlen (hostlist_uri) + 1; |
509 | if (size + sizeof (struct GNUNET_MessageHeader) >= | 491 | if (size + sizeof (struct GNUNET_MessageHeader) >= |
510 | GNUNET_SERVER_MAX_MESSAGE_SIZE) | 492 | GNUNET_SERVER_MAX_MESSAGE_SIZE) |
511 | { | 493 | { |
512 | GNUNET_break (0); | 494 | GNUNET_break (0); |
513 | return; | 495 | return NULL; |
514 | } | 496 | } |
515 | size += sizeof (struct GNUNET_MessageHeader); | 497 | size += sizeof (struct GNUNET_MessageHeader); |
516 | if (NULL == core) | 498 | if (NULL == core) |
517 | { | 499 | { |
518 | GNUNET_break (0); | 500 | GNUNET_break (0); |
519 | return; | 501 | return NULL; |
520 | } | 502 | } |
521 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 503 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
522 | "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n", | 504 | "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n", |
523 | (unsigned int) size, | 505 | (unsigned int) size, |
524 | GNUNET_i2s (peer)); | 506 | GNUNET_i2s (peer)); |
525 | if (NULL == | 507 | adv_transmit (mq); |
526 | (th = GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, | 508 | return NULL; |
527 | GNUNET_CORE_PRIO_BEST_EFFORT, | ||
528 | GNUNET_ADV_TIMEOUT, | ||
529 | peer, | ||
530 | size, | ||
531 | &adv_transmit_ready, | ||
532 | (void *) peer)) ) | ||
533 | { | ||
534 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
535 | _("Advertisement message could not be queued by core\n")); | ||
536 | } | ||
537 | GNUNET_assert (GNUNET_YES == | ||
538 | GNUNET_CONTAINER_multipeermap_put (advertisements, | ||
539 | peer, | ||
540 | th, | ||
541 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
542 | } | ||
543 | |||
544 | |||
545 | /** | ||
546 | * Method called whenever a given peer disconnects. | ||
547 | * | ||
548 | * @param cls closure | ||
549 | * @param peer peer identity this notification is about | ||
550 | */ | ||
551 | static void | ||
552 | disconnect_handler (void *cls, | ||
553 | const struct GNUNET_PeerIdentity *peer) | ||
554 | { | ||
555 | struct GNUNET_CORE_TransmitHandle *th; | ||
556 | |||
557 | if (! advertising) | ||
558 | return; | ||
559 | th = GNUNET_CONTAINER_multipeermap_get (advertisements, | ||
560 | peer); | ||
561 | if (NULL == th) | ||
562 | return; | ||
563 | GNUNET_assert (GNUNET_YES == | ||
564 | GNUNET_CONTAINER_multipeermap_remove (advertisements, | ||
565 | peer, | ||
566 | th)); | ||
567 | GNUNET_CORE_notify_transmit_ready_cancel (th); | ||
568 | } | 509 | } |
569 | 510 | ||
570 | 511 | ||
@@ -696,7 +637,6 @@ prepare_daemon (struct MHD_Daemon *daemon_handle) | |||
696 | * @param st statistics handle to use | 637 | * @param st statistics handle to use |
697 | * @param co core handle to use | 638 | * @param co core handle to use |
698 | * @param[out] server_ch set to handler for CORE connect events | 639 | * @param[out] server_ch set to handler for CORE connect events |
699 | * @param[out] server_dh set to handler for CORE disconnect events | ||
700 | * @param advertise #GNUNET_YES if we should advertise our hostlist | 640 | * @param advertise #GNUNET_YES if we should advertise our hostlist |
701 | * @return #GNUNET_OK on success | 641 | * @return #GNUNET_OK on success |
702 | */ | 642 | */ |
@@ -704,8 +644,7 @@ int | |||
704 | GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, | 644 | GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, |
705 | struct GNUNET_STATISTICS_Handle *st, | 645 | struct GNUNET_STATISTICS_Handle *st, |
706 | struct GNUNET_CORE_Handle *co, | 646 | struct GNUNET_CORE_Handle *co, |
707 | GNUNET_CORE_ConnectEventHandler *server_ch, | 647 | GNUNET_CORE_ConnecTEventHandler *server_ch, |
708 | GNUNET_CORE_DisconnectEventHandler *server_dh, | ||
709 | int advertise) | 648 | int advertise) |
710 | { | 649 | { |
711 | unsigned long long port; | 650 | unsigned long long port; |
@@ -730,8 +669,6 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, | |||
730 | { | 669 | { |
731 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 670 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
732 | "Advertising enabled on this hostlist server\n"); | 671 | "Advertising enabled on this hostlist server\n"); |
733 | advertisements = GNUNET_CONTAINER_multipeermap_create (8, | ||
734 | GNUNET_NO); | ||
735 | } | 672 | } |
736 | cfg = c; | 673 | cfg = c; |
737 | stats = st; | 674 | stats = st; |
@@ -797,11 +734,15 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, | |||
797 | } | 734 | } |
798 | else | 735 | else |
799 | ipv4 = NULL; | 736 | ipv4 = NULL; |
800 | if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV6")) | 737 | if (GNUNET_CONFIGURATION_have_value (cfg, |
738 | "HOSTLIST", | ||
739 | "BINDTOIPV6")) | ||
801 | { | 740 | { |
802 | if (GNUNET_OK != | 741 | if (GNUNET_OK != |
803 | GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST", | 742 | GNUNET_CONFIGURATION_get_value_string (cfg, |
804 | "BINDTOIP", &ipv6)) | 743 | "HOSTLIST", |
744 | "BINDTOIP", | ||
745 | &ipv6)) | ||
805 | { | 746 | { |
806 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 747 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
807 | _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n")); | 748 | _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n")); |
@@ -892,7 +833,6 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, | |||
892 | 833 | ||
893 | core = co; | 834 | core = co; |
894 | *server_ch = &connect_handler; | 835 | *server_ch = &connect_handler; |
895 | *server_dh = &disconnect_handler; | ||
896 | if (NULL != daemon_handle_v4) | 836 | if (NULL != daemon_handle_v4) |
897 | hostlist_task_v4 = prepare_daemon (daemon_handle_v4); | 837 | hostlist_task_v4 = prepare_daemon (daemon_handle_v4); |
898 | if (NULL != daemon_handle_v6) | 838 | if (NULL != daemon_handle_v6) |
@@ -958,13 +898,6 @@ GNUNET_HOSTLIST_server_stop () | |||
958 | GNUNET_PEERINFO_disconnect (peerinfo); | 898 | GNUNET_PEERINFO_disconnect (peerinfo); |
959 | peerinfo = NULL; | 899 | peerinfo = NULL; |
960 | } | 900 | } |
961 | if (NULL != advertisements) | ||
962 | { | ||
963 | GNUNET_break (0 == | ||
964 | GNUNET_CONTAINER_multipeermap_size (advertisements)); | ||
965 | GNUNET_CONTAINER_multipeermap_destroy (advertisements); | ||
966 | advertisements = NULL; | ||
967 | } | ||
968 | cfg = NULL; | 901 | cfg = NULL; |
969 | stats = NULL; | 902 | stats = NULL; |
970 | core = NULL; | 903 | core = NULL; |