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;
66 compare_address_it (void *cls,
67 const GNUNET_HashCode * key,
70 struct CompareAddressContext * cac = cls;
71 struct ATS_Address * aa = (struct ATS_Address *) value;
73 /* compare sessions */
74 if ((aa->session_client != cac->search->session_client) ||
75 (aa->session_id != cac->search->session_id))
78 if (0 == strcmp(aa->plugin, cac->search->plugin))
80 if ((aa->addr_len == cac->search->addr_len) &&
81 (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)))
89 find_address (const struct GNUNET_PeerIdentity *peer,
90 struct ATS_Address * addr)
92 struct CompareAddressContext cac;
96 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
105 merge_ats (struct ATS_Address * dest, struct ATS_Address * source)
110 struct GNUNET_TRANSPORT_ATS_Information * a_src = source->ats;
111 struct GNUNET_TRANSPORT_ATS_Information * a_dest = dest->ats;
113 int new_entries = dest->ats_count;
115 for (c_dest = 0; c_dest < dest->ats_count; c_dest ++)
117 for (c_src = 0; c_src < source->ats_count; c_src ++)
119 if (a_src[c_src].type == a_dest[c_dest].type)
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "Have %u new entries\n",
131 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
132 const char *plugin_name,
133 const void *plugin_addr, size_t plugin_addr_len,
134 struct GNUNET_SERVER_Client *session_client,
136 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
139 struct ATS_Address * aa;
140 struct ATS_Address * old;
142 aa = GNUNET_malloc (sizeof (struct ATS_Address) +
144 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
147 aa->addr_len = plugin_addr_len;
148 aa->ats_count = atsi_count;
149 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
151 memcpy (&aa->addr, plugin_addr, plugin_addr_len);
152 aa->plugin = GNUNET_strdup (plugin_name);
153 aa->session_client = session_client;
154 aa->session_id = session_id;
156 old = find_address (peer, aa);
159 GNUNET_assert (GNUNET_OK ==
160 GNUNET_CONTAINER_multihashmap_put(addresses,
163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Added new address for peer `%s' %X\n",
166 GNUNET_i2s (peer), aa);
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Updated existing address for peer `%s' %X \n",
173 GNUNET_i2s (peer), old);
174 GNUNET_free (aa->ats);
182 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
183 const char *plugin_name,
184 const void *plugin_addr, size_t plugin_addr_len,
185 struct GNUNET_SERVER_Client *session_client,
189 struct ATS_Address *aa;
190 struct ATS_Address *res;
192 aa = GNUNET_malloc (sizeof (struct ATS_Address) +
196 aa->addr_len = plugin_addr_len;
198 memcpy (aa->addr, plugin_addr, plugin_addr_len);
199 aa->plugin = GNUNET_strdup (plugin_name);
200 aa->session_client = session_client;
201 aa->session_id = session_id;
203 res = find_address (peer, aa);
205 GNUNET_break (GNUNET_YES ==
206 GNUNET_CONTAINER_multihashmap_remove(addresses, &peer->hashPubKey, res));
207 GNUNET_free (res->plugin);
208 GNUNET_free_non_null (res->ats);
215 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
217 struct ATS_Address * aa = NULL;
218 aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
220 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);
225 * Initialize address subsystem.
228 GAS_addresses_init ()
230 addresses = GNUNET_CONTAINER_multihashmap_create(128);
235 * Free memory of address.
238 * @param key peer identity (unused)
239 * @param value the 'struct ATS_Address' to free
240 * @return GNUNET_OK (continue to iterate)
243 free_address_it (void *cls,
244 const GNUNET_HashCode * key,
247 struct ATS_Address * aa = value;
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Freeing address for peer `%s' %X\n",
250 GNUNET_i2s (&aa->peer), aa);
258 * Shutdown address subsystem.
261 GAS_addresses_done ()
263 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
264 GNUNET_CONTAINER_multihashmap_destroy (addresses);
268 /* end of gnunet-service-ats_addresses.c */