2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 '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",
94 GNUNET_break (0 == ntohl (msg->reserved));
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 'request address cancel' messages from clients.
142 * @param cls unused, NULL
143 * @param client client that sent the request
144 * @param message the request message
147 GAS_handle_request_address_cancel (void *cls,
148 struct GNUNET_SERVER_Client *client,
149 const struct GNUNET_MessageHeader *message)
151 const struct RequestAddressMessage *msg =
152 (const struct RequestAddressMessage *) message;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Received REQUEST_ADDRESS_CANCEL message for peer %s\n",
156 GNUNET_i2s (&msg->peer));
157 GNUNET_break (0 == ntohl (msg->reserved));
158 GNUNET_CONTAINER_multipeermap_get_multiple (connection_requests,
160 &free_matching_requests,
162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
167 * Unregister a client (which may have been a connectivity client,
168 * but this is not assured).
170 * @param client handle of the (now dead) client
173 GAS_connectivity_remove_client (struct GNUNET_SERVER_Client *client)
175 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
176 &free_matching_requests,
182 * Shutdown connectivity subsystem.
185 GAS_connectivity_init ()
187 connection_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO);
192 * Free the connection request from the map.
195 * @param pid peer for which the request was made
196 * @param value the `struct ConnectionRequest`
197 * @return #GNUNET_OK (continue to iterate)
200 free_request (void *cls,
201 const struct GNUNET_PeerIdentity *pid,
204 struct ConnectionRequest *cr = value;
206 free_matching_requests (cr->client,
214 * Shutdown connectivity subsystem.
217 GAS_connectivity_done ()
219 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
222 GNUNET_CONTAINER_multipeermap_destroy (connection_requests);
223 connection_requests = NULL;
227 /* end of gnunet-service-ats_connectivity.c */