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"
33 struct GNUNET_PeerIdentity peer;
37 struct GNUNET_SERVER_Client *session_client;
47 struct GNUNET_TRANSPORT_ATS_Information * ats;
51 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
54 struct CompareAddressContext
56 struct ATS_Address * search;
57 struct ATS_Address * result;
62 compare_address_it (void *cls,
63 const GNUNET_HashCode * key,
66 struct CompareAddressContext * cac = cls;
67 struct ATS_Address * aa = (struct ATS_Address *) value;
69 /* compare sessions */
70 if ((aa->session_client != cac->search->session_client) ||
71 (aa->session_id != cac->search->session_id))
74 if (0 == strcmp(aa->plugin, cac->search->plugin))
76 if ((aa->addr_len == cac->search->addr_len) &&
77 (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)))
85 find_address (const struct GNUNET_PeerIdentity *peer,
86 struct ATS_Address * addr)
88 struct CompareAddressContext cac;
92 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
101 merge_ats (struct ATS_Address * dest, struct ATS_Address * source)
105 struct GNUNET_TRANSPORT_ATS_Information * a_src = source->ats;
106 struct GNUNET_TRANSPORT_ATS_Information * a_dest = dest->ats;
108 for (c_dest = 0; c_dest < dest->ats_count; c_dest ++)
110 for (c_src = 0; c_src < source->ats_count; c_src ++)
112 if (a_src[c_src].type == a_dest[c_dest].type)
113 a_src[c_src].value = a_dest[c_dest].value;
119 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
120 const char *plugin_name,
121 const void *plugin_addr, size_t plugin_addr_len,
122 struct GNUNET_SERVER_Client *session_client,
124 const struct GNUNET_TRANSPORT_ATS_Information *atsi,
127 struct ATS_Address * aa;
128 struct ATS_Address * old;
130 aa = GNUNET_malloc (sizeof (struct ATS_Address) +
131 atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
134 aa->addr_len = plugin_addr_len;
135 aa->ats_count = atsi_count;
136 aa->ats = (struct GNUNET_TRANSPORT_ATS_Information *) &aa[1];
137 memcpy (&aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
138 aa->addr = &aa->ats[atsi_count];
139 memcpy (&aa->addr, plugin_addr, plugin_addr_len);
140 aa->plugin = GNUNET_strdup (plugin_name);
141 aa->session_client = session_client;
142 aa->session_id = session_id;
144 old = find_address (peer, aa);
147 GNUNET_assert (GNUNET_OK ==
148 GNUNET_CONTAINER_multihashmap_put(addresses,
151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Added new address for peer `%s' \n",
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Updated existing address for peer `%s' \n",
169 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
170 const char *plugin_name,
171 const void *plugin_addr, size_t plugin_addr_len,
172 struct GNUNET_SERVER_Client *session_client,
176 struct ATS_Address *aa;
177 struct ATS_Address *res;
179 aa = GNUNET_malloc (sizeof (struct ATS_Address) +
183 aa->addr_len = plugin_addr_len;
185 memcpy (aa->addr, plugin_addr, plugin_addr_len);
186 aa->plugin = GNUNET_strdup (plugin_name);
187 aa->session_client = session_client;
188 aa->session_id = session_id;
190 res = find_address (peer, aa);
192 GNUNET_break (GNUNET_YES ==
193 GNUNET_CONTAINER_multihashmap_remove(addresses, &peer->hashPubKey, res));
194 GNUNET_free (res->plugin);
201 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
209 * Initialize address subsystem.
212 GAS_addresses_init ()
214 addresses = GNUNET_CONTAINER_multihashmap_create(128);
219 * Free memory of address.
222 * @param key peer identity (unused)
223 * @param value the 'struct ATS_Address' to free
224 * @return GNUNET_OK (continue to iterate)
227 free_address_it (void *cls,
228 const GNUNET_HashCode * key,
231 struct ATS_Address * aa = cls;
239 * Shutdown address subsystem.
242 GAS_addresses_done ()
244 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
245 GNUNET_CONTAINER_multihashmap_destroy (addresses);
249 /* end of gnunet-service-ats_addresses.c */