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"
33 struct GNUNET_PeerIdentity peer;
37 struct GNUNET_SERVER_Client *session_client;
47 struct GNUNET_TRANSPORT_ATS_Information * ats;
49 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
51 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
55 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
58 struct CompareAddressContext
60 struct ATS_Address * search;
61 struct ATS_Address * result;
65 destroy_address (struct ATS_Address * addr)
67 GNUNET_free_non_null (addr->ats);
68 GNUNET_free (addr->plugin);
73 compare_address_it (void *cls,
74 const GNUNET_HashCode * key,
77 struct CompareAddressContext * cac = cls;
78 struct ATS_Address * aa = (struct ATS_Address *) value;
80 /* compare sessions */
81 if ((aa->session_client != cac->search->session_client) ||
82 (aa->session_id != cac->search->session_id))
85 if (0 == strcmp(aa->plugin, cac->search->plugin))
87 if (aa->addr_len != cac->search->addr_len)
89 if (aa->addr_len == 0)
91 if (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
99 find_address (const struct GNUNET_PeerIdentity *peer,
100 struct ATS_Address * addr)
102 struct CompareAddressContext cac;
106 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
115 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
116 const char *plugin_name,
117 const void *plugin_addr, size_t plugin_addr_len,
118 struct GNUNET_SERVER_Client *session_client,
120 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
123 struct ATS_Address * aa;
124 struct ATS_Address * old;
126 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
127 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
130 aa->addr_len = plugin_addr_len;
131 aa->ats_count = atsi_count;
132 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
134 memcpy (aa->addr, plugin_addr, plugin_addr_len);
135 aa->plugin = GNUNET_strdup (plugin_name);
136 aa->session_client = session_client;
137 aa->session_id = session_id;
139 old = find_address (peer, aa);
142 GNUNET_assert (GNUNET_OK ==
143 GNUNET_CONTAINER_multihashmap_put(addresses,
146 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Added new address for peer `%s' %X\n",
149 GNUNET_i2s (peer), aa);
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Updated existing address for peer `%s' %X \n",
155 GNUNET_i2s (peer), old);
156 GNUNET_free_non_null(old->ats);
160 old->ats_count = aa->ats_count;
161 GNUNET_free (aa->plugin);
168 remove_client (void *cls,
169 const GNUNET_HashCode * key,
172 struct GNUNET_SERVER_Client *client = cls;
173 struct ATS_Address * aa = (struct ATS_Address * ) value;
175 if (aa->session_client == client)
177 GNUNET_CONTAINER_multihashmap_remove(addresses, key, value);
178 destroy_address (aa);
186 GAS_address_client_disconnected (struct GNUNET_SERVER_Client *client)
188 if (addresses != NULL)
189 GNUNET_CONTAINER_multihashmap_iterate(addresses, &remove_client, client);
194 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
195 const char *plugin_name,
196 const void *plugin_addr, size_t plugin_addr_len,
197 struct GNUNET_SERVER_Client *session_client,
201 struct ATS_Address *aa;
202 struct ATS_Address *res;
204 aa = GNUNET_malloc (sizeof (struct ATS_Address) +
208 aa->addr_len = plugin_addr_len;
210 memcpy (aa->addr, plugin_addr, plugin_addr_len);
211 aa->plugin = GNUNET_strdup (plugin_name);
212 aa->session_client = session_client;
213 aa->session_id = session_id;
215 res = find_address (peer, aa);
216 GNUNET_assert (res != 0);
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Deleting address for peer `%s'\n",
222 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(addresses, &peer->hashPubKey, res));
223 destroy_address (aa);
224 destroy_address (res);
229 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
231 struct ATS_Address * aa = NULL;
232 aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
234 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr, aa->addr_len, aa->session_client, aa->session_id, aa->ats, aa->ats_count, aa->bw_out, aa->bw_in);
239 * Initialize address subsystem.
242 GAS_addresses_init ()
244 addresses = GNUNET_CONTAINER_multihashmap_create(128);
249 * Free memory of address.
252 * @param key peer identity (unused)
253 * @param value the 'struct ATS_Address' to free
254 * @return GNUNET_OK (continue to iterate)
257 free_address_it (void *cls,
258 const GNUNET_HashCode * key,
261 struct ATS_Address * aa = value;
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Freeing address for peer `%s' %X\n",
264 GNUNET_i2s (&aa->peer), aa);
265 GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
266 destroy_address (aa);
273 * Shutdown address subsystem.
276 GAS_addresses_done ()
278 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
279 GNUNET_CONTAINER_multihashmap_destroy (addresses);
284 /* end of gnunet-service-ats_addresses.c */