gnunet-service-ats_connectivity.c 6.2 KB

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