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_scheduling.h"
30 #include "gnunet-service-ats_reservations.h"
34 struct GNUNET_PeerIdentity peer;
38 struct GNUNET_SERVER_Client *session_client;
48 struct GNUNET_TRANSPORT_ATS_Information * ats;
50 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
52 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
57 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
59 static unsigned long long total_quota_in;
61 static unsigned long long total_quota_out;
63 static unsigned int active_addr_count;
66 struct CompareAddressContext
68 struct ATS_Address * search;
69 struct ATS_Address * result;
74 destroy_address (struct ATS_Address *addr)
76 GNUNET_assert (GNUNET_YES ==
77 GNUNET_CONTAINER_multihashmap_remove(addresses,
78 &addr->peer.hashPubKey,
80 if (ntohl (addr->bw_in.value__) > 0)
83 // FIXME: update address assignment for other peers...
85 GNUNET_free_non_null (addr->ats);
86 GNUNET_free (addr->plugin);
92 compare_address_it (void *cls,
93 const GNUNET_HashCode * key,
96 struct CompareAddressContext * cac = cls;
97 struct ATS_Address * aa = (struct ATS_Address *) value;
99 /* compare sessions */
100 if ((aa->session_client != cac->search->session_client) ||
101 (aa->session_id != cac->search->session_id))
104 if (aa->addr_len != cac->search->addr_len)
109 if (0 == strcmp(aa->plugin, cac->search->plugin))
114 if (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
124 find_address (const struct GNUNET_PeerIdentity *peer,
125 struct ATS_Address * addr)
127 struct CompareAddressContext cac;
131 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
141 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
142 const char *plugin_name,
143 const void *plugin_addr, size_t plugin_addr_len,
144 struct GNUNET_SERVER_Client *session_client,
146 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
149 struct ATS_Address * aa;
150 struct ATS_Address * old;
152 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
153 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
155 aa->addr_len = plugin_addr_len;
156 aa->ats_count = atsi_count;
157 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
159 memcpy (&aa[1], plugin_addr, plugin_addr_len);
160 aa->plugin = GNUNET_strdup (plugin_name);
161 aa->session_client = session_client;
162 aa->session_id = session_id;
163 old = find_address (peer, aa);
166 GNUNET_assert (GNUNET_OK ==
167 GNUNET_CONTAINER_multihashmap_put (addresses,
170 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Added new address for peer `%s' %X\n",
173 GNUNET_i2s (peer), aa);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Updated existing address for peer `%s' %X \n",
178 GNUNET_i2s (peer), old);
179 GNUNET_free_non_null (old->ats);
183 old->ats_count = aa->ats_count;
184 GNUNET_free (aa->plugin);
190 remove_address_by_client (void *cls,
191 const GNUNET_HashCode * key,
194 struct GNUNET_SERVER_Client *client = cls;
195 struct ATS_Address * aa = value;
197 if (aa->session_client == client)
198 destroy_address (aa);
204 GAS_address_client_disconnected (struct GNUNET_SERVER_Client *client)
206 if (addresses != NULL)
207 GNUNET_CONTAINER_multihashmap_iterate(addresses,
208 &remove_address_by_client, client);
213 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
214 const char *plugin_name,
215 const void *plugin_addr, size_t plugin_addr_len,
216 struct GNUNET_SERVER_Client *session_client,
220 struct ATS_Address aa;
221 struct ATS_Address *res;
224 aa.addr_len = plugin_addr_len;
225 aa.addr = plugin_addr;
226 aa.plugin = (char*) plugin_name;
227 aa.session_client = session_client;
228 aa.session_id = session_id;
230 res = find_address (peer, &aa);
233 /* we don't even know this one, can this happen? */
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Asked to delete unknown address for peer `%s'\n",
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Deleting address for peer `%s': `%s'\n",
241 GNUNET_i2s (peer), plugin_name);
242 destroy_address (res);
247 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
249 struct ATS_Address * aa;
251 aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Cannot suggest address for peer `%s'\n",
259 /* FIXME: ensure that we don't do this multiple times per peer! */
260 if (ntohl (aa->bw_in.value__) == 0)
263 aa->bw_in.value__ = htonl (total_quota_in / active_addr_count);
264 aa->bw_out.value__ = htonl (total_quota_out / active_addr_count);
265 /* FIXME: update bw assignments for other addresses... */
267 GAS_reservations_set_bandwidth (peer,
269 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin,
270 aa->addr, aa->addr_len,
271 aa->session_client, aa->session_id,
272 aa->ats, aa->ats_count,
273 aa->bw_out, aa->bw_in);
278 * Initialize address subsystem.
280 * @param cfg configuration to use
283 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
285 GNUNET_assert (GNUNET_OK ==
286 GNUNET_CONFIGURATION_get_value_number (cfg,
290 GNUNET_assert (GNUNET_OK ==
291 GNUNET_CONFIGURATION_get_value_number (cfg,
295 addresses = GNUNET_CONTAINER_multihashmap_create(128);
300 * Free memory of address.
303 * @param key peer identity (unused)
304 * @param value the 'struct ATS_Address' to free
305 * @return GNUNET_OK (continue to iterate)
308 free_address_it (void *cls,
309 const GNUNET_HashCode * key,
312 struct ATS_Address * aa = value;
314 destroy_address (aa);
321 * Shutdown address subsystem.
324 GAS_addresses_done ()
326 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
327 GNUNET_CONTAINER_multihashmap_destroy (addresses);
332 /* end of gnunet-service-ats_addresses.c */