transport_api_offer_hello.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/transport_api_offer_hello.c
  18. * @brief library to offer HELLOs to transport service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_hello_lib.h"
  24. #include "gnunet_protocols.h"
  25. #include "gnunet_transport_service.h"
  26. /**
  27. * Entry in linked list for all offer-HELLO requests.
  28. */
  29. struct GNUNET_TRANSPORT_OfferHelloHandle
  30. {
  31. /**
  32. * Transport service handle we use for transmission.
  33. */
  34. struct GNUNET_MQ_Handle *mq;
  35. /**
  36. * Function to call once we are done.
  37. */
  38. GNUNET_SCHEDULER_TaskCallback cont;
  39. /**
  40. * Closure for @e cont
  41. */
  42. void *cls;
  43. };
  44. /**
  45. * Done sending HELLO message to the service, notify application.
  46. *
  47. * @param cls the handle for the operation
  48. */
  49. static void
  50. finished_hello (void *cls)
  51. {
  52. struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
  53. if (NULL != ohh->cont)
  54. ohh->cont (ohh->cls);
  55. GNUNET_TRANSPORT_offer_hello_cancel (ohh);
  56. }
  57. /**
  58. * Offer the transport service the HELLO of another peer. Note that
  59. * the transport service may just ignore this message if the HELLO is
  60. * malformed or useless due to our local configuration.
  61. *
  62. * @param cfg configuration
  63. * @param hello the hello message
  64. * @param cont continuation to call when HELLO has been sent,
  65. * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
  66. * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
  67. * @param cont_cls closure for @a cont
  68. * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
  69. * in case of failure @a cont will not be called
  70. *
  71. */
  72. struct GNUNET_TRANSPORT_OfferHelloHandle *
  73. GNUNET_TRANSPORT_offer_hello (const struct GNUNET_CONFIGURATION_Handle *cfg,
  74. const struct GNUNET_MessageHeader *hello,
  75. GNUNET_SCHEDULER_TaskCallback cont,
  76. void *cont_cls)
  77. {
  78. struct GNUNET_TRANSPORT_OfferHelloHandle *ohh
  79. = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
  80. struct GNUNET_MQ_Envelope *env;
  81. struct GNUNET_PeerIdentity peer;
  82. if (GNUNET_OK !=
  83. GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello,
  84. &peer))
  85. {
  86. GNUNET_break (0);
  87. GNUNET_free (ohh);
  88. return NULL;
  89. }
  90. ohh->mq = GNUNET_CLIENT_connect (cfg,
  91. "transport",
  92. NULL,
  93. NULL,
  94. ohh);
  95. if (NULL == ohh->mq)
  96. {
  97. GNUNET_free (ohh);
  98. return NULL;
  99. }
  100. ohh->cont = cont;
  101. ohh->cls = cont_cls;
  102. GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
  103. env = GNUNET_MQ_msg_copy (hello);
  104. GNUNET_MQ_notify_sent (env,
  105. &finished_hello,
  106. ohh);
  107. GNUNET_MQ_send (ohh->mq,
  108. env);
  109. return ohh;
  110. }
  111. /**
  112. * Cancel the request to transport to offer the HELLO message
  113. *
  114. * @param ohh the handle for the operation to cancel
  115. */
  116. void
  117. GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
  118. {
  119. GNUNET_MQ_destroy (ohh->mq);
  120. GNUNET_free (ohh);
  121. }
  122. /* end of transport_api_offer_hello.c */