gnunet-service-ats_connectivity.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2015 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 ats/gnunet-service-ats_connectivity.c
  18. * @brief ats service, interaction with 'connecivity' API
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet-service-ats.h"
  24. #include "gnunet-service-ats_addresses.h"
  25. #include "gnunet-service-ats_connectivity.h"
  26. #include "gnunet-service-ats_plugins.h"
  27. #include "ats.h"
  28. /**
  29. * Active connection requests.
  30. */
  31. struct ConnectionRequest {
  32. /**
  33. * Client that made the request.
  34. */
  35. struct GNUNET_SERVICE_Client *client;
  36. /* TODO: allow client to express a 'strength' for this request */
  37. };
  38. /**
  39. * Address suggestion requests by peer.
  40. */
  41. static struct GNUNET_CONTAINER_MultiPeerMap *connection_requests;
  42. /**
  43. * Is the given peer in the list of peers for which we
  44. * have an address request?
  45. *
  46. * @param cls unused, NULL
  47. * @param peer peer to query for
  48. * @return #GNUNET_YES if so, #GNUNET_NO if not
  49. */
  50. unsigned int
  51. GAS_connectivity_has_peer(void *cls,
  52. const struct GNUNET_PeerIdentity *peer)
  53. {
  54. if (NULL == connection_requests)
  55. return 0;
  56. /* TODO: return sum of 'strength's of connectivity requests */
  57. return GNUNET_CONTAINER_multipeermap_contains(connection_requests,
  58. peer);
  59. }
  60. /**
  61. * Handle #GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS messages from clients.
  62. *
  63. * @param client client that sent the request
  64. * @param message the request message
  65. */
  66. void
  67. GAS_handle_request_address(struct GNUNET_SERVICE_Client *client,
  68. const struct RequestAddressMessage *msg)
  69. {
  70. struct ConnectionRequest *cr;
  71. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  72. "Received `%s' message\n",
  73. "GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS");
  74. /* FIXME: should not ignore "msg->strength" */
  75. cr = GNUNET_new(struct ConnectionRequest);
  76. cr->client = client;
  77. (void)GNUNET_CONTAINER_multipeermap_put(connection_requests,
  78. &msg->peer,
  79. cr,
  80. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  81. GAS_plugin_request_connect_start(&msg->peer);
  82. }
  83. /**
  84. * Free the connection request from the map if the
  85. * closure matches the client.
  86. *
  87. * @param cls the client to match
  88. * @param pid peer for which the request was made
  89. * @param value the `struct ConnectionRequest`
  90. * @return #GNUNET_OK (continue to iterate)
  91. */
  92. static int
  93. free_matching_requests(void *cls,
  94. const struct GNUNET_PeerIdentity *pid,
  95. void *value)
  96. {
  97. struct GNUNET_SERVICE_Client *client = cls;
  98. struct ConnectionRequest *cr = value;
  99. if (cr->client == client)
  100. {
  101. GAS_plugin_request_connect_stop(pid);
  102. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  103. "Removed request pending for peer `%s\n",
  104. GNUNET_i2s(pid));
  105. GNUNET_assert(GNUNET_YES ==
  106. GNUNET_CONTAINER_multipeermap_remove(connection_requests,
  107. pid,
  108. cr));
  109. GNUNET_free(cr);
  110. }
  111. return GNUNET_OK;
  112. }
  113. /**
  114. * Handle #GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL messages
  115. * from clients.
  116. *
  117. * @param client the client that sent the request
  118. * @param msg the request message
  119. */
  120. void
  121. GAS_handle_request_address_cancel(struct GNUNET_SERVICE_Client *client,
  122. const struct RequestAddressMessage *msg)
  123. {
  124. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  125. "Received GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL message for peer %s\n",
  126. GNUNET_i2s(&msg->peer));
  127. GNUNET_break(0 == ntohl(msg->strength));
  128. GNUNET_CONTAINER_multipeermap_get_multiple(connection_requests,
  129. &msg->peer,
  130. &free_matching_requests,
  131. client);
  132. }
  133. /**
  134. * Unregister a client (which may have been a connectivity client,
  135. * but this is not assured).
  136. *
  137. * @param client handle of the (now dead) client
  138. */
  139. void
  140. GAS_connectivity_remove_client(struct GNUNET_SERVICE_Client *client)
  141. {
  142. GNUNET_CONTAINER_multipeermap_iterate(connection_requests,
  143. &free_matching_requests,
  144. client);
  145. }
  146. /**
  147. * Shutdown connectivity subsystem.
  148. */
  149. void
  150. GAS_connectivity_init()
  151. {
  152. connection_requests
  153. = GNUNET_CONTAINER_multipeermap_create(32,
  154. GNUNET_NO);
  155. }
  156. /**
  157. * Free the connection request from the map.
  158. *
  159. * @param cls NULL
  160. * @param pid peer for which the request was made
  161. * @param value the `struct ConnectionRequest`
  162. * @return #GNUNET_OK (continue to iterate)
  163. */
  164. static int
  165. free_request(void *cls,
  166. const struct GNUNET_PeerIdentity *pid,
  167. void *value)
  168. {
  169. struct ConnectionRequest *cr = value;
  170. free_matching_requests(cr->client,
  171. pid,
  172. cr);
  173. return GNUNET_OK;
  174. }
  175. /**
  176. * Shutdown connectivity subsystem.
  177. */
  178. void
  179. GAS_connectivity_done()
  180. {
  181. GAS_plugin_solver_lock();
  182. GNUNET_CONTAINER_multipeermap_iterate(connection_requests,
  183. &free_request,
  184. NULL);
  185. GAS_plugin_solver_unlock();
  186. GNUNET_CONTAINER_multipeermap_destroy(connection_requests);
  187. connection_requests = NULL;
  188. }
  189. /* end of gnunet-service-ats_connectivity.c */