2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V.
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file ats/gnunet-service-ats_connectivity.c
23 * @brief ats service, interaction with 'connecivity' API
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_connectivity.h"
31 #include "gnunet-service-ats_plugins.h"
36 * Active connection requests.
38 struct ConnectionRequest
41 * Client that made the request.
43 struct GNUNET_SERVER_Client *client;
45 /* TODO: allow client to express a 'strength' for this request */
50 * Address suggestion requests by peer.
52 static struct GNUNET_CONTAINER_MultiPeerMap *connection_requests;
56 * Is the given peer in the list of peers for which we
57 * have an address request?
59 * @param cls unused, NULL
60 * @param peer peer to query for
61 * @return #GNUNET_YES if so, #GNUNET_NO if not
64 GAS_connectivity_has_peer (void *cls,
65 const struct GNUNET_PeerIdentity *peer)
67 if (NULL == connection_requests)
69 /* TODO: return sum of 'strength's of connectivity requests */
70 return GNUNET_CONTAINER_multipeermap_contains (connection_requests,
76 * Handle #GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS messages from clients.
78 * @param cls unused, NULL
79 * @param client client that sent the request
80 * @param message the request message
83 GAS_handle_request_address (void *cls,
84 struct GNUNET_SERVER_Client *client,
85 const struct GNUNET_MessageHeader *message)
87 const struct RequestAddressMessage *msg =
88 (const struct RequestAddressMessage *) message;
89 struct ConnectionRequest *cr;
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92 "Received `%s' message\n",
93 "GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS");
94 /* FIXME: should not ignore "msg->strength" */
95 cr = GNUNET_new (struct ConnectionRequest);
97 (void) GNUNET_CONTAINER_multipeermap_put (connection_requests,
100 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
101 GAS_plugin_request_connect_start (&msg->peer);
102 GNUNET_SERVER_receive_done (client, GNUNET_OK);
107 * Free the connection request from the map if the
108 * closure matches the client.
110 * @param cls the client to match
111 * @param pid peer for which the request was made
112 * @param value the `struct ConnectionRequest`
113 * @return #GNUNET_OK (continue to iterate)
116 free_matching_requests (void *cls,
117 const struct GNUNET_PeerIdentity *pid,
120 struct GNUNET_SERVER_Client *client = cls;
121 struct ConnectionRequest *cr = value;
123 if (cr->client == client)
125 GAS_plugin_request_connect_stop (pid);
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Removed request pending for peer `%s\n",
129 GNUNET_assert (GNUNET_YES ==
130 GNUNET_CONTAINER_multipeermap_remove (connection_requests,
140 * Handle #GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL messages
143 * @param cls unused, NULL
144 * @param client client that sent the request
145 * @param message the request message
148 GAS_handle_request_address_cancel (void *cls,
149 struct GNUNET_SERVER_Client *client,
150 const struct GNUNET_MessageHeader *message)
152 const struct RequestAddressMessage *msg =
153 (const struct RequestAddressMessage *) message;
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Received GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL message for peer %s\n",
157 GNUNET_i2s (&msg->peer));
158 GNUNET_break (0 == ntohl (msg->strength));
159 GNUNET_CONTAINER_multipeermap_get_multiple (connection_requests,
161 &free_matching_requests,
163 GNUNET_SERVER_receive_done (client, GNUNET_OK);
168 * Unregister a client (which may have been a connectivity client,
169 * but this is not assured).
171 * @param client handle of the (now dead) client
174 GAS_connectivity_remove_client (struct GNUNET_SERVER_Client *client)
176 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
177 &free_matching_requests,
183 * Shutdown connectivity subsystem.
186 GAS_connectivity_init ()
188 connection_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO);
193 * Free the connection request from the map.
196 * @param pid peer for which the request was made
197 * @param value the `struct ConnectionRequest`
198 * @return #GNUNET_OK (continue to iterate)
201 free_request (void *cls,
202 const struct GNUNET_PeerIdentity *pid,
205 struct ConnectionRequest *cr = value;
207 free_matching_requests (cr->client,
215 * Shutdown connectivity subsystem.
218 GAS_connectivity_done ()
220 GAS_plugin_solver_lock ();
221 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
224 GAS_plugin_solver_unlock ();
225 GNUNET_CONTAINER_multipeermap_destroy (connection_requests);
226 connection_requests = NULL;
230 /* end of gnunet-service-ats_connectivity.c */