2 This file is part of GNUnet.
3 (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;
48 * Address suggestion requests by peer.
50 static struct GNUNET_CONTAINER_MultiPeerMap *connection_requests;
54 * Is the given peer in the list of peers for which we
55 * have an address request?
57 * @param peer peer to query for
58 * @return #GNUNET_YES if so, #GNUNET_NO if not
61 GAS_connectivity_has_peer (const struct GNUNET_PeerIdentity *peer)
63 return GNUNET_CONTAINER_multipeermap_contains (connection_requests,
69 * Handle 'request address' messages from clients.
71 * @param cls unused, NULL
72 * @param client client that sent the request
73 * @param message the request message
76 GAS_handle_request_address (void *cls,
77 struct GNUNET_SERVER_Client *client,
78 const struct GNUNET_MessageHeader *message)
80 const struct RequestAddressMessage *msg =
81 (const struct RequestAddressMessage *) message;
82 struct ConnectionRequest *cr;
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Received `%s' message\n",
87 GNUNET_break (0 == ntohl (msg->reserved));
88 cr = GNUNET_new (struct ConnectionRequest);
90 (void) GNUNET_CONTAINER_multipeermap_put (connection_requests,
93 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
94 GAS_plugin_request_connect_start (&msg->peer);
95 GNUNET_SERVER_receive_done (client, GNUNET_OK);
100 * Free the connection request from the map if the
101 * closure matches the client.
103 * @param cls the client to match
104 * @param pid peer for which the request was made
105 * @param value the `struct ConnectionRequest`
106 * @return #GNUNET_OK (continue to iterate)
109 free_matching_requests (void *cls,
110 const struct GNUNET_PeerIdentity *pid,
113 struct GNUNET_SERVER_Client *client = cls;
114 struct ConnectionRequest *cr = value;
116 if (cr->client == client)
118 GAS_plugin_request_connect_stop (pid);
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "Removed request pending for peer `%s\n",
122 GNUNET_assert (GNUNET_YES ==
123 GNUNET_CONTAINER_multipeermap_remove (connection_requests,
133 * Handle 'request address cancel' messages from clients.
135 * @param cls unused, NULL
136 * @param client client that sent the request
137 * @param message the request message
140 GAS_handle_request_address_cancel (void *cls,
141 struct GNUNET_SERVER_Client *client,
142 const struct GNUNET_MessageHeader *message)
144 const struct RequestAddressMessage *msg =
145 (const struct RequestAddressMessage *) message;
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Received REQUEST_ADDRESS_CANCEL message for peer %s\n",
149 GNUNET_i2s (&msg->peer));
150 GNUNET_break (0 == ntohl (msg->reserved));
151 GNUNET_CONTAINER_multipeermap_get_multiple (connection_requests,
153 &free_matching_requests,
155 GNUNET_SERVER_receive_done (client, GNUNET_OK);
160 * Unregister a client (which may have been a connectivity client,
161 * but this is not assured).
163 * @param client handle of the (now dead) client
166 GAS_connectivity_remove_client (struct GNUNET_SERVER_Client *client)
168 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
169 &free_matching_requests,
175 * Shutdown connectivity subsystem.
178 GAS_connectivity_init ()
180 connection_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO);
185 * Free the connection request from the map.
188 * @param pid peer for which the request was made
189 * @param value the `struct ConnectionRequest`
190 * @return #GNUNET_OK (continue to iterate)
193 free_request (void *cls,
194 const struct GNUNET_PeerIdentity *pid,
197 struct ConnectionRequest *cr = value;
199 free_matching_requests (cr->client,
207 * Shutdown connectivity subsystem.
210 GAS_connectivity_done ()
212 GNUNET_CONTAINER_multipeermap_iterate (connection_requests,
215 GNUNET_CONTAINER_multipeermap_destroy (connection_requests);
219 /* end of gnunet-service-ats_connectivity.c */