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)
82 if (aa->addr_len == 0)
84 if (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
92 find_address (const struct GNUNET_PeerIdentity *peer,
93 struct ATS_Address * addr)
95 struct CompareAddressContext cac;
99 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
108 merge_ats (struct ATS_Address * dest, struct ATS_Address * source)
112 struct GNUNET_TRANSPORT_ATS_Information * a_src = source->ats;
113 struct GNUNET_TRANSPORT_ATS_Information * a_dest = dest->ats;
114 struct ATS_Address * bigger = NULL;
116 bigger = (dest->ats_count > source->ats_count) ? dest : source;
117 int new_entries = bigger->ats_count;
119 if (new_entries == 0)
122 for (c_dest = 0; c_dest < dest->ats_count; c_dest ++)
124 for (c_src = 0; c_src < source->ats_count; c_src ++)
126 if (a_src[c_src].type == a_dest[c_dest].type)
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Have %u entries to update and %u new entries\n",bigger->ats_count,
137 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
138 const char *plugin_name,
139 const void *plugin_addr, size_t plugin_addr_len,
140 struct GNUNET_SERVER_Client *session_client,
142 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
145 struct ATS_Address * aa;
146 struct ATS_Address * old;
148 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
149 aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
152 aa->addr_len = plugin_addr_len;
153 aa->ats_count = atsi_count;
154 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
156 memcpy (aa->addr, plugin_addr, plugin_addr_len);
157 aa->plugin = GNUNET_strdup (plugin_name);
158 aa->session_client = session_client;
159 aa->session_id = session_id;
161 old = find_address (peer, aa);
164 GNUNET_assert (GNUNET_OK ==
165 GNUNET_CONTAINER_multihashmap_put(addresses,
168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Added new address for peer `%s' %X\n",
171 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 (aa->ats);
180 GNUNET_free (aa->plugin);
188 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
189 const char *plugin_name,
190 const void *plugin_addr, size_t plugin_addr_len,
191 struct GNUNET_SERVER_Client *session_client,
195 struct ATS_Address *aa;
196 struct ATS_Address *res;
198 aa = GNUNET_malloc (sizeof (struct ATS_Address) +
202 aa->addr_len = plugin_addr_len;
204 memcpy (aa->addr, plugin_addr, plugin_addr_len);
205 aa->plugin = GNUNET_strdup (plugin_name);
206 aa->session_client = session_client;
207 aa->session_id = session_id;
209 res = find_address (peer, aa);
210 GNUNET_assert (res != 0);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Deleting address for peer `%s'\n",
216 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(addresses, &peer->hashPubKey, res));
217 GNUNET_free (res->plugin);
218 GNUNET_free_non_null (res->ats);
225 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
227 struct ATS_Address * aa = NULL;
228 aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
230 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);
235 * Initialize address subsystem.
238 GAS_addresses_init ()
240 addresses = GNUNET_CONTAINER_multihashmap_create(128);
245 * Free memory of address.
248 * @param key peer identity (unused)
249 * @param value the 'struct ATS_Address' to free
250 * @return GNUNET_OK (continue to iterate)
253 free_address_it (void *cls,
254 const GNUNET_HashCode * key,
257 struct ATS_Address * aa = value;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Freeing address for peer `%s' %X\n",
260 GNUNET_i2s (&aa->peer), aa);
261 GNUNET_free (aa->plugin);
262 GNUNET_free_non_null (aa->ats);
270 * Shutdown address subsystem.
273 GAS_addresses_done ()
275 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
276 GNUNET_CONTAINER_multihashmap_destroy (addresses);
280 /* end of gnunet-service-ats_addresses.c */