+/**
+ * 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)
+{
+ struct ATS_Address **ap = cls;
+ struct ATS_Address *aa = (struct ATS_Address *) value;
+ struct ATS_Address *ab = *ap;
+
+ if (NULL == ab)
+ {
+ *ap = aa;
+ return GNUNET_OK;
+ }
+ if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
+ (ntohl (aa->assigned_bw_in.value__) > 0))
+ {
+ /* stick to existing connection */
+ *ap = aa;
+ return GNUNET_OK;
+ }
+ if (ab->atsp_distance > aa->atsp_distance)
+ {
+ /* user shorter distance */
+ *ap = aa;
+ return GNUNET_OK;
+ }
+ if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
+ {
+ /* user lower latency */
+ *ap = aa;
+ return GNUNET_OK;
+ }
+ /* don't care */
+ return GNUNET_OK;