2 This file is part of GNUnet.
3 (C) 2011 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_addresses.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_scheduling.h"
31 #include "gnunet-service-ats_reservations.h"
35 struct GNUNET_PeerIdentity peer;
39 struct GNUNET_SERVER_Client *session_client;
49 struct GNUNET_TRANSPORT_ATS_Information * ats;
51 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
53 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
58 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
60 static unsigned long long total_quota_in;
62 static unsigned long long total_quota_out;
64 static unsigned int active_addr_count;
67 struct CompareAddressContext
69 struct ATS_Address * search;
70 struct ATS_Address * result;
75 destroy_address (struct ATS_Address *addr)
77 GNUNET_assert (GNUNET_YES ==
78 GNUNET_CONTAINER_multihashmap_remove(addresses,
79 &addr->peer.hashPubKey,
81 if (ntohl (addr->bw_in.value__) > 0)
84 // FIXME: update address assignment for other peers...
86 GNUNET_free_non_null (addr->ats);
87 GNUNET_free (addr->plugin);
93 compare_address_it (void *cls,
94 const GNUNET_HashCode * key,
97 struct CompareAddressContext * cac = cls;
98 struct ATS_Address * aa = (struct ATS_Address *) value;
100 /* compare sessions */
101 if ((aa->session_client != cac->search->session_client) ||
102 (aa->session_id != cac->search->session_id))
105 if (aa->addr_len != cac->search->addr_len)
110 if (0 == strcmp(aa->plugin, cac->search->plugin))
115 if (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
125 find_address (const struct GNUNET_PeerIdentity *peer,
126 struct ATS_Address * addr)
128 struct CompareAddressContext cac;
132 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
142 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
143 const char *plugin_name,
144 const void *plugin_addr, size_t plugin_addr_len,
145 struct GNUNET_SERVER_Client *session_client,
147 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
150 struct ATS_Address * aa;
151 struct ATS_Address * old;
153 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
154 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
156 aa->addr_len = plugin_addr_len;
157 aa->ats_count = atsi_count;
158 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
160 memcpy (&aa[1], plugin_addr, plugin_addr_len);
161 aa->plugin = GNUNET_strdup (plugin_name);
162 aa->session_client = session_client;
163 aa->session_id = session_id;
164 old = find_address (peer, aa);
167 GNUNET_assert (GNUNET_OK ==
168 GNUNET_CONTAINER_multihashmap_put (addresses,
171 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Added new address for peer `%s' %X\n",
174 GNUNET_i2s (peer), aa);
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Updated existing address for peer `%s' %X \n",
179 GNUNET_i2s (peer), old);
180 GNUNET_free_non_null (old->ats);
184 old->ats_count = aa->ats_count;
185 GNUNET_free (aa->plugin);
191 remove_address_by_client (void *cls,
192 const GNUNET_HashCode * key,
195 struct GNUNET_SERVER_Client *client = cls;
196 struct ATS_Address * aa = value;
198 if (aa->session_client == client)
199 destroy_address (aa);
205 GAS_address_client_disconnected (struct GNUNET_SERVER_Client *client)
207 if (addresses != NULL)
208 GNUNET_CONTAINER_multihashmap_iterate(addresses,
209 &remove_address_by_client, client);
214 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
215 const char *plugin_name,
216 const void *plugin_addr, size_t plugin_addr_len,
217 struct GNUNET_SERVER_Client *session_client,
221 struct ATS_Address aa;
222 struct ATS_Address *res;
225 aa.addr_len = plugin_addr_len;
226 aa.addr = plugin_addr;
227 aa.plugin = (char*) plugin_name;
228 aa.session_client = session_client;
229 aa.session_id = session_id;
231 res = find_address (peer, &aa);
234 /* we don't even know this one, can this happen? */
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Asked to delete unknown address for peer `%s'\n",
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Deleting address for peer `%s': `%s'\n",
242 GNUNET_i2s (peer), plugin_name);
243 destroy_address (res);
248 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
250 struct ATS_Address * aa;
252 aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Cannot suggest address for peer `%s'\n",
260 /* FIXME: ensure that we don't do this multiple times per peer! */
261 if (ntohl (aa->bw_in.value__) == 0)
264 aa->bw_in.value__ = htonl (total_quota_in / active_addr_count);
265 aa->bw_out.value__ = htonl (total_quota_out / active_addr_count);
266 /* FIXME: update bw assignments for other addresses... */
268 GAS_reservations_set_bandwidth (peer,
270 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin,
271 aa->addr, aa->addr_len,
272 aa->session_client, aa->session_id,
273 aa->ats, aa->ats_count,
274 aa->bw_out, aa->bw_in);
275 GAS_performance_notify_clients (peer, aa->plugin,
276 aa->addr, aa->addr_len,
277 aa->ats, aa->ats_count,
278 aa->bw_out, aa->bw_in);
282 // FIXME: this function should likely end up in the LP-subsystem and
283 // not with 'addresses' in the future...
285 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
286 enum GNUNET_ATS_PreferenceKind kind,
289 // do nothing for now...
294 * Initialize address subsystem.
296 * @param cfg configuration to use
299 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
301 GNUNET_assert (GNUNET_OK ==
302 GNUNET_CONFIGURATION_get_value_number (cfg,
306 GNUNET_assert (GNUNET_OK ==
307 GNUNET_CONFIGURATION_get_value_number (cfg,
311 addresses = GNUNET_CONTAINER_multihashmap_create(128);
316 * Free memory of address.
319 * @param key peer identity (unused)
320 * @param value the 'struct ATS_Address' to free
321 * @return GNUNET_OK (continue to iterate)
324 free_address_it (void *cls,
325 const GNUNET_HashCode * key,
328 struct ATS_Address * aa = value;
330 destroy_address (aa);
336 * Shutdown address subsystem.
339 GAS_addresses_done ()
341 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
342 GNUNET_CONTAINER_multihashmap_destroy (addresses);
347 /* end of gnunet-service-ats_addresses.c */