+ /* Get existing address */
+ old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
+ session_id, NULL, 0);
+ if (old == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
+ GNUNET_i2s (peer), plugin_name, session_id);
+ return;
+ }
+
+
+ GNUNET_break (0 < strlen (plugin_name));
+ aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
+
+ GNUNET_CONTAINER_multihashmap_get_multiple (handle->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 struct GNUNET_HashCode * key, void *value)
+{
+ struct ATS_Address **ap = cls;
+ struct ATS_Address *aa = (struct ATS_Address *) value;
+ struct ATS_Address *ab = *ap;
+ struct GNUNET_TIME_Absolute now;
+
+ now = GNUNET_TIME_absolute_get();
+
+ if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
+ {
+ /* This address is blocked for suggestion */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Address %p blocked for suggestion for %llu ms \n",
+ aa,
+ GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
+ return GNUNET_OK;
+ }
+
+ aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
+ aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
+
+ /* FIXME this is a hack */
+
+
+ if (NULL != ab)
+ {
+ if ((0 == strcmp (ab->plugin, "tcp")) &&
+ (0 == strcmp (aa->plugin, "tcp")))
+ {
+ if ((0 != ab->addr_len) &&
+ (0 == aa->addr_len))
+ {
+ /* saved address was an outbound address, but we have an inbound address */
+ *ap = aa;
+ return GNUNET_OK;
+ }
+ if (0 == ab->addr_len)
+ {
+ /* saved address was an inbound address, so do not overwrite */
+ return GNUNET_OK;
+ }
+ }
+ }
+ /* FIXME end of hack */
+
+ 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;
+}
+
+
+int
+GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
+ const char *plugin_name, const void *plugin_addr,
+ size_t plugin_addr_len, uint32_t session_id, int in_use)
+{
+#if DEBUG_ATS
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
+ GNUNET_i2s (peer), in_use);
+#endif
+
+ struct ATS_Address *old;
+
+ if (GNUNET_NO == handle->running)
+ return GNUNET_SYSERR;
+
+ old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
+ if (NULL == old)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Trying to set unknown address `%s', %s %u %s \n",
+ GNUNET_i2s (peer),
+ plugin_name, session_id,
+ (GNUNET_NO == in_use) ? "NO" : "YES");
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (old->used == in_use)
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Address in use called multiple times for peer `%s': %s -> %s \n",
+ GNUNET_i2s (peer),
+ (GNUNET_NO == old->used) ? "NO" : "YES",
+ (GNUNET_NO == in_use) ? "NO" : "YES");
+ return GNUNET_SYSERR;
+ }
+ old->used = in_use;
+
+ /* Tell solver about update */
+ handle->s_update (handle->solver, handle->addresses, old);
+
+ return GNUNET_OK;
+}
+
+
+static void
+request_address_mlp (const struct GNUNET_PeerIdentity *peer)
+{
+ struct ATS_Address *aa;
+ aa = NULL;
+
+#if HAVE_GLPK
+ /* Get preferred address from MODE_MLP */
+ struct ATS_PreferedAddress * paddr = NULL;
+ paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
+ aa = paddr->address;
+ aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
+ /* FIXME use bw in value */
+ paddr->bandwidth_in = paddr->bandwidth_out;
+ aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
+ GNUNET_free (paddr);
+#endif
+
+ if (aa == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
+ "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
+ return;
+ }
+ if (aa->active == GNUNET_NO)
+ {
+ aa->active = GNUNET_YES;
+ handle->active_addr_count++;
+ send_bw_notification (aa);
+ }
+ else
+ {
+ /* just to be sure... */
+ GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
+ aa->addr_len, aa->session_id,
+ aa->ats, aa->ats_count,
+ aa->assigned_bw_out,
+ aa->assigned_bw_in);
+ }
+
+}
+
+
+static void
+request_address_simple (const struct GNUNET_PeerIdentity *peer)
+{
+ struct ATS_Address *aa;
+ aa = NULL;