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 /* TODO: return sum of 'strength's of connectivity requests */
68 return GNUNET_CONTAINER_multipeermap_contains (connection_requests,
74 * Handle 'request address' messages from clients.
76 * @param cls unused, NULL
77 * @param client client that sent the request
78 * @param message the request message
81 GAS_handle_request_address (void *cls,
82 struct GNUNET_SERVER_Client *client,
83 const struct GNUNET_MessageHeader *message)
85 const struct RequestAddressMessage *msg =
86 (const struct RequestAddressMessage *) message;
87 struct ConnectionRequest *cr;
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Received `%s' message\n",
92 GNUNET_break (0 == ntohl (msg->reserved));
93 cr = GNUNET_new (struct ConnectionRequest);
95 (void) GNUNET_CONTAINER_multipeermap_put (connection_requests,
98 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
99 GAS_plugin_request_connect_start (&msg->peer);
100 GNUNET_SERVER_receive_done (client, GNUNET_OK);
105 * Free the connection request from the map if the
106 * closure matches the client.
108 * @param cls the client to match
109 * @param pid peer for which the request was made
110 * @param value the `struct ConnectionRequest`
111 * @return #GNUNET_OK (continue to iterate)
114 free_matching_requests (void *cls,
115 const struct GNUNET_PeerIdentity *pid,
118 struct GNUNET_SERVER_Client *client = cls;
119 struct ConnectionRequest *cr = value;
121 if (cr->client == client)
123 GAS_plugin_request_connect_stop (pid);
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "Removed request pending for peer `%s\n",
127 GNUNET_assert (GNUNET_YES ==
128 GNUNET_CONTAINER_multipeermap_remove (connection_requests,
138 * Handle 'request address cancel' messages from clients.
140 * @param cls unused, NULL
141 * @param client client that sent the request
142 * @param message the request message
145 GAS_handle_request_address_cancel (void *cls,
146 struct GNUNET_SERVER_Client *client,
147 const struct GNUNET_MessageHeader *message)
149 const struct RequestAddressMessage *msg =
150 (const struct RequestAddressMessage *) message;
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Received REQUEST_ADDRESS_CANCEL message for peer %s\n",
154 GNUNET_i2s (&msg->peer));
155 GNUNET_break (0 == ntohl (msg->reserved));
156 GNUNET_CONTAINER_multipeermap_get_multiple (connection_requests,
158 &free_matching_requests,
160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
165 * Unregister a client (which may have been a connectivity client,
166 * but this is not assured).
168 * @param client handle of the (now dead) client
171 GAS_connectivity_remove_client (struct GNUNET_SERVER_Client *client)
173 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
174 &free_matching_requests,
180 * Shutdown connectivity subsystem.
183 GAS_connectivity_init ()
185 connection_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO);
190 * Free the connection request from the map.
193 * @param pid peer for which the request was made
194 * @param value the `struct ConnectionRequest`
195 * @return #GNUNET_OK (continue to iterate)
198 free_request (void *cls,
199 const struct GNUNET_PeerIdentity *pid,
202 struct ConnectionRequest *cr = value;
204 free_matching_requests (cr->client,
212 * Shutdown connectivity subsystem.
215 GAS_connectivity_done ()
217 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
220 GNUNET_CONTAINER_multipeermap_destroy (connection_requests);
224 /* end of gnunet-service-ats_connectivity.c */