+/**
+ * Find an existing equivalent address record.
+ * Compares by peer identity and network address AND by session ID
+ * (one of the two must match).
+ *
+ * @param peer peer to lookup addresses for
+ * @param addr existing address record
+ * @return existing address record, NULL for none
+ */
+struct ATS_Address *
+find_exact_address (const struct GNUNET_PeerIdentity *peer,
+ const struct ATS_Address *addr)
+{
+ struct CompareAddressContext cac;
+
+ cac.exact_address = NULL;
+ cac.search = addr;
+ GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
+ &compare_address_session_it, &cac);
+ return cac.exact_address;
+}
+
+
+void
+GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
+ const char *plugin_name, const void *plugin_addr,
+ size_t plugin_addr_len, uint32_t session_id,
+ const struct GNUNET_ATS_Information *atsi,
+ uint32_t atsi_count)
+{
+ struct ATS_Address *aa;
+ struct ATS_Address *old;
+ uint32_t i;
+
+ aa = create_address (peer,
+ plugin_name,
+ plugin_addr, plugin_addr_len,
+ session_id);
+
+ aa->mlp_information = NULL;
+ aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
+ aa->ats_count = atsi_count;
+ memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
+
+#if DEBUG_ATS
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n",
+ GNUNET_i2s (peer),
+ session_id);
+#endif
+ /* Get existing address or address with session == 0 */
+ old = find_address (peer, aa);
+ if (old == NULL)
+ {
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (addresses,
+ &peer->hashPubKey, aa,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+#if DEBUG_ATS
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
+ GNUNET_i2s (peer), aa);
+#endif
+ old = aa;
+ }
+ else
+ {
+#if DEBUG_ATS
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Updated existing address for peer `%s' %p old session %u new session %u\n",
+ GNUNET_i2s (peer), old,
+ old->session_id, session_id);
+#endif
+ GNUNET_free_non_null (old->ats);
+ old->session_id = session_id;
+ old->ats = NULL;
+ old->ats_count = 0;
+ old->ats = aa->ats;
+ old->ats_count = aa->ats_count;
+ GNUNET_free (aa->plugin);
+ GNUNET_free (aa);
+ }
+ for (i = 0; i < atsi_count; i++)
+ switch (ntohl (atsi[i].type))
+ {
+ case GNUNET_ATS_UTILIZATION_UP:
+ old->atsp_utilization_out.value__ = atsi[i].value;
+ break;
+ case GNUNET_ATS_UTILIZATION_DOWN:
+ old->atsp_utilization_in.value__ = atsi[i].value;
+ break;
+ case GNUNET_ATS_QUALITY_NET_DELAY:
+ old->atsp_latency.rel_value = ntohl (atsi[i].value);
+ break;
+ case GNUNET_ATS_QUALITY_NET_DISTANCE:
+ old->atsp_distance = ntohl (atsi[i].value);
+ break;
+ case GNUNET_ATS_COST_WAN:
+ old->atsp_cost_wan = ntohl (atsi[i].value);
+ break;
+ case GNUNET_ATS_COST_LAN:
+ old->atsp_cost_lan = ntohl (atsi[i].value);
+ break;
+ case GNUNET_ATS_COST_WLAN:
+ old->atsp_cost_wlan = ntohl (atsi[i].value);
+ break;
+ case GNUNET_ATS_NETWORK_TYPE:
+ old->atsp_network_type = ntohl (atsi[i].value);
+ break;
+
+ default:
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
+ GNUNET_break (0);
+ break;
+ }
+#if HAVE_LIBGLPK
+ if (ats_mode == MLP)
+ GAS_mlp_address_update (mlp, addresses, old);
+#endif
+}
+
+
+/**
+ * Delete an address
+ *
+ * If session != 0, just the session is deleted, the address itself still exists
+ * If session == 0, remove full address
+ * If session == 0 and addrlen == 0, destroy inbound address
+ *
+ * @param cls unused
+ * @param key unused
+ * @param value the 'struct ATS_Address'
+ * @return GNUNET_OK (continue to iterate)
+ */
+static int
+destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ const struct ATS_Address *info = cls;
+ struct ATS_Address *aa = value;
+
+ GNUNET_assert (0 ==
+ memcmp (&aa->peer, &info->peer,
+ sizeof (struct GNUNET_PeerIdentity)));
+ /* session == 0, remove full address */
+ if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
+ (aa->addr_len == info->addr_len) &&
+ (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
+ {
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Deleting address for peer `%s': `%s' %u\n",
+ GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
+#endif
+ if (GNUNET_YES == destroy_address (aa))
+ recalculate_assigned_bw ();
+ return GNUNET_OK;
+ }
+ /* session != 0, just remove session */
+ if (aa->session_id != info->session_id)
+ return GNUNET_OK; /* irrelevant */
+ if (aa->session_id != 0)
+ GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
+ /* session died */
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Deleting session for peer `%s': `%s' %u\n",
+ GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
+#endif
+ aa->session_id = 0;
+
+ if (GNUNET_YES == aa->active)
+ {
+ aa->active = GNUNET_NO;
+ active_addr_count--;
+ recalculate_assigned_bw ();
+ }
+
+ /* session == 0 and addrlen == 0 : destroy address */
+ if (aa->addr_len == 0)
+ {
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Deleting session and address for peer `%s': `%s' %u\n",
+ GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
+#endif
+ (void) destroy_address (aa);
+ }
+ else
+ {
+ /* session was set to 0, update address */
+#if HAVE_LIBGLPK
+ if (ats_mode == MLP)
+ GAS_mlp_address_update (mlp, addresses, aa);
+#endif
+ }
+
+ return GNUNET_OK;
+}
+
+void
+GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
+ const char *plugin_name, const void *plugin_addr,
+ size_t plugin_addr_len, uint32_t session_id)
+{
+ struct ATS_Address *aa;
+
+ GNUNET_break (0 < strlen (plugin_name));
+ aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
+
+ GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
+ &destroy_by_session_id, aa);
+
+ free_address (aa);
+}
+
+
+/**
+ * Find a "good" address to use for a peer. If we already have an existing
+ * address, we stick to it. Otherwise, we pick by lowest distance and then
+ * by lowest latency.
+ *
+ * @param cls the 'struct ATS_Address**' where we store the result
+ * @param key unused
+ * @param value another 'struct ATS_Address*' to consider using
+ * @return GNUNET_OK (continue to iterate)
+ */
+static int
+find_address_it (void *cls, const GNUNET_HashCode * key, void *value)