-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / plugin_ats_proportional.c
index 6414e99efa17736b062108e2e74f60279660f845..b4cabb0676afe60074d05693ea704468fe7cb4fa 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
(C) 2011 Christian Grothoff (and other contributing authors)
Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  * @author Matthias Wachs
  * @author Christian Grothoff
  */
-#include "plugin_ats_proportional.h"
+#include "platform.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet_ats_plugin.h"
+#include "gnunet_ats_service.h"
+#include "gnunet-service-ats_addresses.h"
+
+#define PROP_STABILITY_FACTOR 1.25
+
 
 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
 
@@ -43,7 +50,7 @@
  *    outbound quota is configured and the bandwidth available in
  *    these networks is distributed over the addresses.  The solver
  *    first assigns every addresses the minimum amount of bandwidth
- *    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
+ *    #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
  *    remaining bandwidth available according to the preference
  *    values. For each peer only a single address gets bandwidth
  *    assigned and only one address marked as active.  The most
  *    solver is when an address switches network it is located
  *    in. This is common because addresses added by transport's
  *    validation mechanism are commonly located in
- *    GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
+ *    #GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
  *    in this network type and only if a connection is successful on
  *    return of payload data transport switches to the real network
  *    the address is located in.  When an address changes networks it
  *    is first of all removed from the old network using the solver
- *    API function GAS_proportional_address_delete and the network in
+ *    API function #GAS_proportional_address_delete() and the network in
  *    the address struct is updated. A lookup for the respective new
  *    proportional network is done and stored in the addresse's
  *    solver_information field. Next the address is re-added to the
  *    solver using the solver API function
- *    GAS_proportional_address_add. If the address was marked as in
+ *    #GAS_proportional_address_add(). If the address was marked as in
  *    active, the solver checks if bandwidth is available in the
  *    network and if yes sets the address to active and updates the
  *    bandwidth distribution in this network. If no bandwidth is
  *    the best available address.  Several checks are done when an
  *    address is selected. First if this address is currently blocked
  *    by addresses from being suggested. An address is blocked for the
- *    duration of ATS_BLOCKING_DELTA when it is suggested to
+ *    duration of #ATS_BLOCKING_DELTA when it is suggested to
  *    transport. Next it is checked if at least
- *    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
+ *    #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
  *    in the addresse's network, because suggesting an address without
  *    bandwidth does not make sense. This also ensures that all active
  *    addresses in this network get at least the minimum amount of
  */
 struct GAS_PROPORTIONAL_Handle
 {
-   struct GNUNET_ATS_PluginEnvironment *env;
 
   /**
-   * Statistics handle
+   * Our execution environment.
    */
-  struct GNUNET_STATISTICS_Handle *stats;
+  struct GNUNET_ATS_PluginEnvironment *env;
 
   /**
    * Hashmap containing all valid addresses
@@ -227,36 +233,6 @@ struct GAS_PROPORTIONAL_Handle
    */
   struct GNUNET_CONTAINER_MultiPeerMap *requests;
 
-  /**
-   * Bandwidth changed callback
-   */
-  GAS_bandwidth_changed_cb bw_changed;
-
-  /**
-   * Bandwidth changed callback cls
-   */
-  void *bw_changed_cls;
-
-  /**
-   * ATS function to get preferences
-   */
-  GAS_get_preferences get_preferences;
-
-  /**
-   * Closure for ATS function to get preferences
-   */
-  void *get_preferences_cls;
-
-  /**
-   * ATS function to get properties
-   */
-  GAS_get_properties get_properties;
-
-  /**
-   * Closure for ATS function to get properties
-   */
-  void *get_properties_cls;
-
   /**
    * Bulk lock
    */
@@ -306,22 +282,20 @@ struct Network
   /**
    * ATS network type
    */
-  unsigned int type;
+  enum GNUNET_ATS_Network_Type type;
 
   /**
    * Network description
    */
-  char *desc;
+  const char *desc;
 
   /**
    * Total inbound quota
-   *
    */
   unsigned long long total_quota_in;
 
   /**
    * Total outbound quota
-   *
    */
   unsigned long long total_quota_out;
 
@@ -345,7 +319,14 @@ struct Network
    */
   char *stat_active;
 
+  /**
+   * Linked list of addresses in this network: head
+   */
   struct AddressWrapper *head;
+
+  /**
+   * Linked list of addresses in this network: tail
+   */
   struct AddressWrapper *tail;
 };
 
@@ -354,19 +335,20 @@ struct Network
  */
 struct AddressSolverInformation
 {
+  /**
+   * Network scope this address is in
+   */
   struct Network *network;
 
   /**
    * Inbound quota
-   *
    */
-  unsigned long long calculated_quota_in_NBO;
+  uint32_t calculated_quota_in;
 
   /**
    * Outbound quota
-   *
    */
-  unsigned long long calculated_quota_out_NBO;
+  uint32_t calculated_quota_out;
 
   /**
    * When was this address activated
@@ -396,120 +378,21 @@ struct AddressWrapper
   struct ATS_Address *addr;
 };
 
+
 /**
- *  Important solver functions
- *  ---------------------------
+ * Function used to unload the plugin.
+ *
+ * @param cls return value from #libgnunet_plugin_ats_proportional_init()
  */
-
-void *
-libgnunet_plugin_ats_proportional_init (void *cls)
-{
-  struct GNUNET_ATS_PluginEnvironment *env = cls;
-  struct GAS_PROPORTIONAL_Handle *s;
-  struct Network * cur;
-  char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
-  float f_tmp;
-  int c;
-
-  GNUNET_assert (NULL != env);
-  GNUNET_assert(NULL != env->cfg);
-  GNUNET_assert(NULL != env->bandwidth_changed_cb);
-  GNUNET_assert(NULL != env->get_preferences);
-  GNUNET_assert(NULL != env->get_property);
-
-  s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
-  s->env = env;
-  env->sf.s_add = &GAS_proportional_address_add;
-  env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
-  env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
-  env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed;
-  env->sf.s_address_update_network = &GAS_proportional_address_change_network;
-  env->sf.s_get = &GAS_proportional_get_preferred_address;
-  env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
-  env->sf.s_pref = &GAS_proportional_address_change_preference;
-  env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
-  env->sf.s_del = &GAS_proportional_address_delete;
-  env->sf.s_bulk_start = &GAS_proportional_bulk_start;
-  env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
-
-  s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
-  s->bw_changed = env->bandwidth_changed_cb;
-  s->bw_changed_cls = env->bw_changed_cb_cls;
-  s->get_preferences = env->get_preferences;
-  s->get_preferences_cls = env->get_preference_cls;
-  s->get_properties = env->get_property;
-  s->get_properties_cls = env->get_property_cls;
-  s->network_count = env->network_count;
-  s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
-
-  /* Init */
-  s->active_addresses = 0;
-  s->total_addresses = 0;
-  s->bulk_lock = GNUNET_NO;
-  s->addresses = env->addresses;
-  s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
-
-  s->stability_factor = PROP_STABILITY_FACTOR;
-  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
-      "PROP_STABILITY_FACTOR", &f_tmp))
-  {
-    if ((f_tmp < 1.0) || (f_tmp > 2.0))
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
-          "PROP_STABILITY_FACTOR", f_tmp);
-    }
-    else
-    {
-      s->stability_factor = f_tmp;
-      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
-          "PROP_STABILITY_FACTOR", f_tmp);
-    }
-  }
-
-  s->prop_factor = PROPORTIONALITY_FACTOR;
-  if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
-      "PROP_STABILITY_FACTOR", &f_tmp))
-  {
-    if (f_tmp < 1.0)
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
-          "PROP_PROPORTIONALITY_FACTOR", f_tmp);
-    }
-    else
-    {
-      s->prop_factor = f_tmp;
-      LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
-          "PROP_PROPORTIONALITY_FACTOR", f_tmp);
-    }
-  }
-
-  for (c = 0; c < env->network_count; c++)
-  {
-    cur = &s->network_entries[c];
-    cur->total_addresses = 0;
-    cur->active_addresses = 0;
-    cur->type = env->networks[c];
-    cur->total_quota_in = env->in_quota[c];
-    cur->total_quota_out = env->out_quota[c];
-    cur->desc = net_str[c];
-    GNUNET_asprintf (&cur->stat_total,
-        "# ATS addresses %s total", cur->desc);
-    GNUNET_asprintf (&cur->stat_active,
-        "# ATS active addresses %s total", cur->desc);
-    LOG (GNUNET_ERROR_TYPE_INFO, "Added network %u `%s' (%llu/%llu)\n",
-        c, cur->desc, cur->total_quota_in, cur->total_quota_out);
-  }
-  return s;
-}
-
 void *
 libgnunet_plugin_ats_proportional_done (void *cls)
 {
-  struct GAS_PROPORTIONAL_Handle *s = cls;
+  struct GNUNET_ATS_SolverFunctions *sf = cls;
+  struct GAS_PROPORTIONAL_Handle *s = sf->cls;
   struct AddressWrapper *cur;
   struct AddressWrapper *next;
   int c;
-  GNUNET_assert(s != NULL);
+
   for (c = 0; c < s->network_count; c++)
   {
     if (s->network_entries[c].total_addresses > 0)
@@ -564,7 +447,7 @@ libgnunet_plugin_ats_proportional_done (void *cls)
  * Test if bandwidth is available in this network to add an additional address
  *
  * @param net the network type to update
- * @return GNUNET_YES or GNUNET_NO
+ * @return #GNUNET_YES or #GNUNET_NO
  */
 static int
 is_bandwidth_available_in_network (struct Network *net)
@@ -587,6 +470,7 @@ is_bandwidth_available_in_network (struct Network *net)
   return GNUNET_NO;
 }
 
+
 /**
  * Update bandwidth assigned to peers in this network
  *
@@ -596,11 +480,10 @@ is_bandwidth_available_in_network (struct Network *net)
  */
 static void
 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
-    struct Network *net)
+                      struct Network *net)
 {
   struct AddressSolverInformation *asi;
   struct AddressWrapper *cur_address;
-
   unsigned long long remaining_quota_in = 0;
   unsigned long long quota_out_used = 0;
   unsigned long long remaining_quota_out = 0;
@@ -614,14 +497,16 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
   double total_weight;
   const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */
 
-  unsigned long long assigned_quota_in = 0;
-  unsigned long long assigned_quota_out = 0;
+  uint32_t assigned_quota_in = 0;
+  uint32_t assigned_quota_out = 0;
 
 
-  LOG(GNUNET_ERROR_TYPE_INFO,
-      "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
-      net->desc, net->active_addresses, net->total_quota_in,
-      net->total_quota_in);
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
+       net->desc,
+       net->active_addresses,
+       net->total_quota_in,
+       net->total_quota_in);
 
   if (net->active_addresses == 0)
   {
@@ -657,15 +542,32 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
     if (GNUNET_YES != cur_address->addr->active)
       continue;
 
-    GNUNET_assert( NULL != (peer_relative_prefs = s->get_preferences (s->get_preferences_cls,
-        &cur_address->addr->peer)));
+    GNUNET_assert( NULL != (peer_relative_prefs = s->env->get_preferences (s->env->cls,
+                                                                           &cur_address->addr->peer)));
     relative_peer_prefence = 0.0;
     relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
     sum_relative_peer_prefences += relative_peer_prefence;
     count_addresses ++;
   }
 
-  GNUNET_assert (count_addresses == net->active_addresses);
+  if (count_addresses != net->active_addresses)
+  {
+    GNUNET_break (0);
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "%s: Counted %u active addresses, but network says to have %u active addresses\n",
+         net->desc, count_addresses, net->active_addresses);
+    for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
+    {
+      if (GNUNET_YES != cur_address->addr->active)
+        continue;
+
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "Active: `%s' `%s' length %u\n",
+           GNUNET_i2s (&cur_address->addr->peer),
+           cur_address->addr->plugin,
+           cur_address->addr->addr_len);
+    }
+  }
 
   LOG (GNUNET_ERROR_TYPE_INFO,
       "Total relative preference %.3f for %u addresses in network %s\n",
@@ -676,7 +578,8 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
     if (GNUNET_YES == cur_address->addr->active)
     {
       GNUNET_assert( NULL != (peer_relative_prefs =
-          s->get_preferences (s->get_preferences_cls, &cur_address->addr->peer)));
+                              s->env->get_preferences (s->env->cls,
+                                                       &cur_address->addr->peer)));
 
       cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
       total_weight = net->active_addresses +
@@ -709,8 +612,8 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
 
     /* Compare to current bandwidth assigned */
     asi = cur_address->addr->solver_information;
-    asi->calculated_quota_in_NBO = htonl (assigned_quota_in);
-    asi->calculated_quota_out_NBO = htonl (assigned_quota_out);
+    asi->calculated_quota_in = assigned_quota_in;
+    asi->calculated_quota_out = assigned_quota_out;
   }
   LOG(GNUNET_ERROR_TYPE_DEBUG,
       "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
@@ -729,13 +632,27 @@ distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
   }
 }
 
+
+/**
+ * Context for finding the best address* Linked list of addresses in this network: head
+ */
 struct FindBestAddressCtx
 {
+  /**
+   * The solver handle
+   */
   struct GAS_PROPORTIONAL_Handle *s;
+
+  /**
+   * The currently best address
+   */
   struct ATS_Address *best;
 };
 
 
+/**
+ * Find index of a ATS property type in the array.
+ */
 static int
 find_property_index (uint32_t type)
 {
@@ -767,12 +684,9 @@ find_best_address_it (void *cls,
   struct FindBestAddressCtx *ctx = cls;
   struct ATS_Address *current = value;
   struct ATS_Address *current_best = current;
-  struct GNUNET_TIME_Absolute now;
   struct AddressSolverInformation *asi;
   struct GNUNET_TIME_Relative active_time;
   struct GNUNET_TIME_Relative min_active_time;
-  const double *norm_prop_cur;
-  const double *norm_prop_best;
   double best_delay;
   double best_distance;
   double cur_delay;
@@ -781,20 +695,6 @@ find_best_address_it (void *cls,
 
   current_best = NULL;
   asi = current->solver_information;
-  now = GNUNET_TIME_absolute_get ();
-
-  if ((current->active == GNUNET_NO)
-      && (current->blocked_until.abs_value_us
-          == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us))
-  {
-    /* This address is blocked for suggestion */
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Address %p blocked for suggestion for %s \n",
-         current,
-         GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until),
-                                                 GNUNET_YES));
-    return GNUNET_OK;
-  }
   if (NULL == asi)
   {
     GNUNET_break (0);
@@ -826,7 +726,7 @@ find_best_address_it (void *cls,
     GNUNET_assert (asi->activated.abs_value_us != GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us);
     active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
     min_active_time.rel_value_us =  ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) *
-        PROP_STABILITY_FACTOR;
+        ctx->s->stability_factor;
     if (active_time.rel_value_us <= min_active_time.rel_value_us)
     {
       /* Keep active address for stability reasons */
@@ -836,24 +736,16 @@ find_best_address_it (void *cls,
   }
 
   /* Now compare ATS information */
-  norm_prop_cur = ctx->s->get_properties (ctx->s->get_properties_cls,
-      (const struct ATS_Address *) current);
-  index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
-  cur_distance = norm_prop_cur[index];
-  index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
-  cur_delay = norm_prop_cur[index];
-
-  norm_prop_best = ctx->s->get_properties (ctx->s->get_properties_cls,
-      (const struct ATS_Address *) ctx->best);
   index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
-  best_distance = norm_prop_best[index];
+  cur_distance = current->atsin[index].norm;
+  best_distance = ctx->best->atsin[index].norm;
   index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
-  best_delay = norm_prop_best[index];
+  cur_delay = current->atsin[index].norm;
+  best_delay = ctx->best->atsin[index].norm;
 
   /* user shorter distance */
   if (cur_distance < best_distance)
   {
-
     if (GNUNET_NO == ctx->best->active)
     {
       current_best = current; /* Use current */
@@ -895,21 +787,33 @@ end:
   return GNUNET_OK;
 }
 
+
+/**
+ * Find the currently best address for a peer from the set of addresses available
+ * or return NULL of no address is available
+ *
+ * @param s the proportional handle
+ * @param addresses the address hashmap
+ * @param id the peer id
+ * @return the address or NULL
+ */
 struct ATS_Address *
 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
-    struct GNUNET_CONTAINER_MultiPeerMap *addresses,
-    const struct GNUNET_PeerIdentity *id)
+                  struct GNUNET_CONTAINER_MultiPeerMap *addresses,
+                  const struct GNUNET_PeerIdentity *id)
 {
   struct FindBestAddressCtx fba_ctx;
+
   fba_ctx.best = NULL;
   fba_ctx.s = s;
-
-  GNUNET_CONTAINER_multipeermap_get_multiple (addresses, id,
-      &find_best_address_it, &fba_ctx);
-
+  GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
+                                              id,
+                                              &find_best_address_it,
+                                              &fba_ctx);
   return fba_ctx.best;
 }
 
+
 /**
  * Hashmap Iterator to find current active address for peer
  *
@@ -926,22 +830,29 @@ get_active_address_it (void *cls,
   struct ATS_Address **dest = cls;
   struct ATS_Address *aa = (struct ATS_Address *) value;
 
+  LOG (GNUNET_ERROR_TYPE_INFO,
+         "Checking address %p\n", aa);
+
   if (GNUNET_YES == aa->active)
   {
-
+  LOG (GNUNET_ERROR_TYPE_INFO,
+         "Address %p is active\n", aa);
     if (NULL != (*dest))
     {
       /* should never happen */
-      LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
-          GNUNET_i2s (&aa->peer));
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           "Multiple active addresses for peer `%s'\n",
+           GNUNET_i2s (&aa->peer));
       GNUNET_break(0);
       return GNUNET_NO;
     }
     (*dest) = aa;
+
   }
   return GNUNET_OK;
 }
 
+
 /**
  * Find current active address for peer
  *
@@ -955,10 +866,12 @@ get_active_address (void *solver,
                     const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
                     const struct GNUNET_PeerIdentity *peer)
 {
-  struct ATS_Address * dest = NULL;
+  static struct ATS_Address *dest;
 
+  dest = NULL;
   GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
-                                              &get_active_address_it, &dest);
+                                              &get_active_address_it,
+                                              &dest);
   return dest;
 }
 
@@ -971,46 +884,71 @@ get_active_address (void *solver,
  * @return the network struct
  */
 static struct Network *
-get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
+get_network (struct GAS_PROPORTIONAL_Handle *s,
+             enum GNUNET_ATS_Network_Type type)
 {
-  int c;
-  for (c = 0; c < s->network_count; c++)
+  if (type >= s->env->network_count)
   {
-    if (s->network_entries[c].type == type)
-      return &s->network_entries[c];
+    GNUNET_break (0);
+    return NULL;
   }
-  return NULL ;
+  return &s->network_entries[type];
 }
 
 
+/**
+ * Increase address count in network
+ *
+ * @param s the solver handle
+ * @param net the network type
+ * @param total increase total addresses
+ * @param active increase active addresses
+ */
 static void
-addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
-    int total, int active)
+address_increment (struct GAS_PROPORTIONAL_Handle *s,
+                    struct Network *net,
+                    int total,
+                    int active)
 {
   if (GNUNET_YES == total)
   {
     s->total_addresses++;
     net->total_addresses++;
-    GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
-    GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
+    GNUNET_STATISTICS_update (s->env->stats,
+                              "# ATS addresses total", 1, GNUNET_NO);
+    GNUNET_STATISTICS_update (s->env->stats,
+                              net->stat_total, 1, GNUNET_NO);
   }
   if (GNUNET_YES == active)
   {
     net->active_addresses++;
     s->active_addresses++;
-    GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
+    GNUNET_STATISTICS_update (s->env->stats,
+                              "# ATS active addresses total", 1,
         GNUNET_NO);
-    GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
+    GNUNET_STATISTICS_update (s->env->stats,
+                              net->stat_active, 1, GNUNET_NO);
   }
 
 }
 
 
+/**
+ * Decrease address count in network
+ *
+ * @param s the solver handle
+ * @param net the network type
+ * @param total decrease total addresses
+ * @param active decrease active addresses
+ */
 static int
-addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
-    int total, int active)
+addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
+                    struct Network *net,
+                    int total,
+                    int active)
 {
   int res = GNUNET_OK;
+
   if (GNUNET_YES == total)
   {
     if (s->total_addresses < 1)
@@ -1021,7 +959,8 @@ addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
     else
     {
       s->total_addresses--;
-      GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
+      GNUNET_STATISTICS_update (s->env->stats,
+                                "# ATS addresses total", -1,
           GNUNET_NO);
     }
     if (net->total_addresses < 1)
@@ -1032,7 +971,8 @@ addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
     else
     {
       net->total_addresses--;
-      GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
+      GNUNET_STATISTICS_update (s->env->stats,
+                                net->stat_total, -1, GNUNET_NO);
     }
   }
 
@@ -1046,7 +986,8 @@ addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
     else
     {
       net->active_addresses--;
-      GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
+      GNUNET_STATISTICS_update (s->env->stats,
+                                net->stat_active, -1, GNUNET_NO);
     }
     if (s->active_addresses < 1)
     {
@@ -1056,13 +997,22 @@ addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
     else
     {
       s->active_addresses--;
-      GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
+      GNUNET_STATISTICS_update (s->env->stats,
+                                "# ATS addresses total", -1,
           GNUNET_NO);
     }
   }
   return res;
 }
 
+
+/**
+ * Compares addresses
+ *
+ * @param a address a
+ * @param b address b
+ * @return GNUNET_YES if equal, GNUNET_NO else
+ */
 static int
 address_eq (struct ATS_Address *a, struct ATS_Address *b)
 {
@@ -1079,56 +1029,60 @@ address_eq (struct ATS_Address *a, struct ATS_Address *b)
   return GNUNET_YES;
 }
 
+
 /**
- *  Helper functions
- *  ---------------------------
+ * Notify bandwidth changes to addresses
+ *
+ * @param s solver handle
+ * @param net the network to propagate changes in
  */
 static void
 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
-    struct Network *net)
+                     struct Network *net)
 {
   struct AddressWrapper *cur;
   struct AddressSolverInformation *asi;
+
   for (cur = net->head; NULL != cur; cur = cur->next)
   {
       asi = cur->addr->solver_information;
-      if ( (cur->addr->assigned_bw_in.value__ != asi->calculated_quota_in_NBO) ||
-           (cur->addr->assigned_bw_out.value__ != asi->calculated_quota_out_NBO) )
+      if ( (cur->addr->assigned_bw_in != asi->calculated_quota_in) ||
+           (cur->addr->assigned_bw_out != asi->calculated_quota_out) )
       {
-        cur->addr->assigned_bw_in.value__ = asi->calculated_quota_in_NBO;
-        cur->addr->assigned_bw_out.value__ = asi->calculated_quota_out_NBO;
+        cur->addr->assigned_bw_in = asi->calculated_quota_in;
+        cur->addr->assigned_bw_out = asi->calculated_quota_out;
 
         /* Reset for next iteration */
-        asi->calculated_quota_in_NBO = htonl (0);
-        asi->calculated_quota_out_NBO = htonl (0);
-
+        asi->calculated_quota_in = 0;
+        asi->calculated_quota_out = 0;
         LOG (GNUNET_ERROR_TYPE_DEBUG,
             "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
             (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
             cur->addr,
             GNUNET_i2s (&cur->addr->peer),
-            ntohl (cur->addr->assigned_bw_in.value__),
-            ntohl (cur->addr->assigned_bw_out.value__ ));
+            cur->addr->assigned_bw_in,
+            cur->addr->assigned_bw_out);
 
         /* Notify on change */
         if ((GNUNET_YES == cur->addr->active))
         {
-          s->bw_changed (s->bw_changed_cls, cur->addr);
+          s->env->bandwidth_changed_cb (s->env->cls,
+                                        cur->addr);
         }
       }
   }
 }
 
+
 /**
  * Distribibute bandwidth
  *
  * @param s the solver handle
  * @param n the network, can be NULL for all network
- * @param address_except do not notify for this address
  */
 static void
 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
-    struct Network *n)
+                                 struct Network *n)
 {
   if (GNUNET_YES == s->bulk_lock)
   {
@@ -1143,83 +1097,120 @@ distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
         GNUNET_ATS_print_network_type(n->type),
         n->active_addresses, n->total_addresses);
 
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_START,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_SINGLE);
 
     /* Distribute  */
     distribute_bandwidth(s, n);
 
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_STOP,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_SINGLE);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_SINGLE);
 
     /* Do propagation */
     propagate_bandwidth (s, n);
 
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_SINGLE);
   }
   else
   {
     int i;
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_START,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_ALL);
     for (i = 0; i < s->network_count; i++)
     {
       /* Distribute */
       distribute_bandwidth(s, &s->network_entries[i]);
     }
 
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_STOP,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_ALL);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_ALL);
     for (i = 0; i < s->network_count; i++)
     {
       /* Do propagation */
       propagate_bandwidth(s, &s->network_entries[i]);
     }
-    if (NULL != s->env->info_cb)
-      s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
-          GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
+    s->env->info_cb (s->env->cls,
+                     GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
+                     GAS_STAT_SUCCESS,
+                     GAS_INFO_PROP_ALL);
   }
 }
 
-static const struct ATS_Address *
+
+/**
+ * Update active address for a peer:
+ * Check if active address exists and what the best address is, if addresses
+ * are different switch
+ *
+ * @param s solver handle
+ * @param peer the peer to check
+ * return the new address or NULL if no update was performed
+  */
+static struct ATS_Address *
 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
-    const struct GNUNET_PeerIdentity *peer)
+                       const struct GNUNET_PeerIdentity *peer)
 {
   struct ATS_Address *best_address;
   struct ATS_Address *current_address;
   struct AddressSolverInformation *asi;
   struct Network *net;
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "Updating active address for peer `%s'\n",
-      GNUNET_i2s (peer));
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Updating active address for peer `%s'\n",
+       GNUNET_i2s (peer));
 
   /* Find active address */
-  current_address = get_active_address (s, s->addresses, peer);
+  current_address = get_active_address (s,
+                                        s->addresses,
+                                        peer);
+
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Peer `%s' has active address %p\n",
+       GNUNET_i2s (peer),
+       current_address);
+
   /* Find best address */
-  best_address = get_best_address (s,s->addresses, peer);
+  best_address = get_best_address (s,
+                                   s->addresses,
+                                   peer);
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Peer `%s' has best address %p\n",
+       GNUNET_i2s (peer),
+       best_address);
 
   if (NULL != current_address)
   {
-    if ((NULL == best_address) || ((NULL != best_address) && (GNUNET_NO == address_eq (current_address, best_address))))
+    if ( (NULL == best_address) ||
+         ( (NULL != best_address) &&
+           (GNUNET_NO == address_eq (current_address,
+                                     best_address)) ) )
     {
-      /* We switch to a new address, mark old address as inactive */
+      /* We switch to a new address (or to none),
+         mark old address as inactive */
       LOG (GNUNET_ERROR_TYPE_INFO,
-          "Disabling previous %s address %p for peer `%s'\n",
-          (GNUNET_NO == current_address->active) ? "inactive" : "active",
-          current_address,
-          GNUNET_i2s (peer));
+           "Disabling previous %s address %p for peer `%s'\n",
+           (GNUNET_NO == current_address->active) ? "inactive" : "active",
+           current_address,
+           GNUNET_i2s (peer));
 
       asi = current_address->solver_information;
       GNUNET_assert (NULL != asi);
@@ -1227,8 +1218,8 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
       net = asi->network;
       asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
       current_address->active = GNUNET_NO; /* No active any longer */
-      current_address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
-      current_address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
+      current_address->assigned_bw_in = 0; /* no bandwidth assigned */
+      current_address->assigned_bw_out = 0; /* no bandwidth assigned */
 
       if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
         GNUNET_break(0);
@@ -1240,32 +1231,31 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
     {
       /* We previously had an active address, but now we cannot suggest one
        * Therefore we have to disconnect the peer */
-
       LOG (GNUNET_ERROR_TYPE_INFO,
-          "Disconnecting peer `%s' with previous %s address %p\n",
-          GNUNET_i2s (peer),
-          (GNUNET_NO == current_address->active) ? "inactive" : "active",
-          current_address);
-      s->bw_changed (s->bw_changed_cls, current_address);
+           "Disconnecting peer `%s' with previous address %p\n",
+           GNUNET_i2s (peer),
+           current_address);
+      s->env->bandwidth_changed_cb (s->env->cls,
+                                    current_address);
     }
   }
-
   if (NULL == best_address)
   {
-    LOG (GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
-        GNUNET_i2s (peer));
-    return NULL ;
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Cannot suggest address for peer `%s'\n",
+         GNUNET_i2s (peer));
+    return NULL;
   }
 
-  LOG (GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
-      (GNUNET_NO == best_address->active) ? "inactive" : "active", best_address,
-      GNUNET_i2s (peer));
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Suggesting new address %p for peer `%s'\n",
+       best_address,
+       GNUNET_i2s (peer));
 
-  if ((NULL != current_address) &&
-      (GNUNET_YES == address_eq (best_address, current_address)))
+  if ( (NULL != current_address) &&
+       (GNUNET_YES == address_eq (best_address, current_address)) )
   {
-    if (current_address->active == GNUNET_NO)
-      GNUNET_break (0); /* Should never happen */
+    GNUNET_break (GNUNET_NO != current_address->active);
     return best_address; /* Same same */
   }
 
@@ -1274,19 +1264,18 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
   net = asi->network;
 
   /* Mark address as active */
-  asi->activated = GNUNET_TIME_absolute_get();
+  asi->activated = GNUNET_TIME_absolute_get ();
   best_address->active = GNUNET_YES;
-  addresse_increment (s, net, GNUNET_NO, GNUNET_YES);
-
+  address_increment (s, net, GNUNET_NO, GNUNET_YES);
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Address %p for peer `%s' is now active\n",
+       best_address,
+       GNUNET_i2s (peer));
   /* Distribute bandwidth */
   distribute_bandwidth_in_network (s, net);
   return best_address;
 }
 
-/**
- *  Solver API functions
- *  ---------------------------
- */
 
 /**
  * Changes the preferences for a peer in the problem
@@ -1296,7 +1285,7 @@ update_active_address (struct GAS_PROPORTIONAL_Handle *s,
  * @param kind the kind to change the preference
  * @param pref_rel the normalized preference value for this kind over all clients
  */
-void
+static void
 GAS_proportional_address_change_preference (void *solver,
                                            const struct GNUNET_PeerIdentity *peer,
                                            enum GNUNET_ATS_PreferenceKind kind,
@@ -1307,15 +1296,13 @@ GAS_proportional_address_change_preference (void *solver,
   struct ATS_Address *active_address;
   struct AddressSolverInformation *asi;
 
-  GNUNET_assert(NULL != solver);
-  GNUNET_assert(NULL != peer);
-
-  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
+  if (GNUNET_NO ==
+      GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
     return; /* Peer is not requested */
 
   /* This peer is requested, find best address */
-  active_address = get_active_address(s, s->addresses, peer);
-  best_address = (struct ATS_Address *) update_active_address (s, peer);
+  active_address = get_active_address (s, s->addresses, peer);
+  best_address = update_active_address (s, peer);
 
   if ((NULL != best_address) && ((NULL != active_address) &&
       (GNUNET_YES == address_eq (active_address, best_address))))
@@ -1339,16 +1326,17 @@ GAS_proportional_address_change_preference (void *solver,
  * @param kind the kind to change the preference
  * @param score the score
  */
-void
-GAS_proportional_address_preference_feedback (void *solver, void *application,
-    const struct GNUNET_PeerIdentity *peer,
-    const struct GNUNET_TIME_Relative scope,
-    enum GNUNET_ATS_PreferenceKind kind, double score)
+static void
+GAS_proportional_address_preference_feedback (void *solver,
+                                              struct GNUNET_SERVER_Client *application,
+                                              const struct GNUNET_PeerIdentity *peer,
+                                              const struct GNUNET_TIME_Relative scope,
+                                              enum GNUNET_ATS_PreferenceKind kind,
+                                              double score)
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
-  GNUNET_assert(NULL != solver);
-  GNUNET_assert(NULL != peer);
 
+  GNUNET_assert(NULL != peer);
   GNUNET_assert(NULL != s);
 }
 
@@ -1356,30 +1344,29 @@ GAS_proportional_address_preference_feedback (void *solver, void *application,
 /**
  * Get the preferred address for a specific peer
  *
- *
- *
  * @param solver the solver handle
  * @param peer the identity of the peer
+ * @return best address
  */
-const struct ATS_Address *
+static const struct ATS_Address *
 GAS_proportional_get_preferred_address (void *solver,
-    const struct GNUNET_PeerIdentity *peer)
+                                        const struct GNUNET_PeerIdentity *peer)
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
   const struct ATS_Address *best_address;
 
-
-  GNUNET_assert(s != NULL);
-  GNUNET_assert(peer != NULL);
-
   /* Add to list of pending requests */
-  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
+  if (GNUNET_NO ==
+      GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
   {
-    GNUNET_assert(
-        GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (s->requests, peer, NULL,
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_CONTAINER_multipeermap_put (s->requests,
+                                                     peer,
+                                                     NULL,
             GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-    LOG (GNUNET_ERROR_TYPE_INFO, "Start suggesting addresses for peer `%s'\n",
-          GNUNET_i2s (peer));
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Start suggesting addresses for peer `%s'\n",
+         GNUNET_i2s (peer));
   }
 
   best_address = update_active_address (s, peer);
@@ -1389,49 +1376,62 @@ GAS_proportional_get_preferred_address (void *solver,
   return best_address;
 }
 
+
 /**
  * Stop notifying about address and bandwidth changes for this peer
  *
  * @param solver the solver handle
  * @param peer the peer
  */
-void
+static void
 GAS_proportional_stop_get_preferred_address (void *solver,
-    const struct GNUNET_PeerIdentity *peer)
+                                             const struct GNUNET_PeerIdentity *peer)
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
   struct ATS_Address *cur;
   struct AddressSolverInformation *asi;
   struct Network *cur_net;
 
-  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
-    GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (s->requests,
-        peer, NULL));
+  if (GNUNET_YES ==
+      GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
+  {
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_CONTAINER_multipeermap_remove (s->requests,
+                                                         peer,
+                                                         NULL));
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Stop suggesting addresses for peer `%s'\n",
+         GNUNET_i2s (peer));
+  }
 
-  cur = get_active_address (s, s->addresses, peer);
+  cur = get_active_address (s,
+                            s->addresses,
+                            peer);
   if (NULL != cur)
   {
-    LOG(GNUNET_ERROR_TYPE_INFO,
-        "Disabling %s address %p for peer `%s'\n",
-        (GNUNET_NO == cur->active) ? "inactive" : "active", cur,
-        GNUNET_i2s (&cur->peer));
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Disabling %s address %p for peer `%s'\n",
+         (GNUNET_NO == cur->active) ? "inactive" : "active",
+         cur,
+         GNUNET_i2s (&cur->peer));
 
     /* Disabling current address */
     asi = cur->solver_information;
     cur_net = asi->network ;
     asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
     cur->active = GNUNET_NO; /* No active any longer */
-    cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
-    cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
+    cur->assigned_bw_in = 0; /* no bandwidth assigned */
+    cur->assigned_bw_out = 0; /* no bandwidth assigned */
 
-    if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
+    if (GNUNET_SYSERR ==
+        addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
       GNUNET_break(0);
 
-    distribute_bandwidth_in_network (s, cur_net );
+    distribute_bandwidth_in_network (s, cur_net);
   }
-  return;
 }
 
+
 /**
  * Remove an address from the solver
  *
@@ -1439,9 +1439,10 @@ GAS_proportional_stop_get_preferred_address (void *solver,
  * @param address the address to remove
  * @param session_only delete only session not whole address
  */
-void
-GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
-    int session_only)
+static void
+GAS_proportional_address_delete (void *solver,
+                                 struct ATS_Address *address,
+                                 int session_only)
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
   struct Network *net;
@@ -1466,11 +1467,14 @@ GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
 
   if (GNUNET_NO == session_only)
   {
-    LOG(GNUNET_ERROR_TYPE_INFO,
-        "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
-        (GNUNET_NO == address->active) ? "inactive" : "active", address,
-        GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
-        net->active_addresses);
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
+         (GNUNET_NO == address->active) ? "inactive" : "active",
+         address,
+         GNUNET_i2s (&address->peer),
+         net->desc,
+         net->total_addresses,
+         net->active_addresses);
 
     /* Remove address */
     addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
@@ -1484,41 +1488,48 @@ GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
       GNUNET_break(0);
       return;
     }
-    GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
-    GNUNET_free(aw);
+    GNUNET_CONTAINER_DLL_remove (net->head,
+                                 net->tail,
+                                 aw);
+    GNUNET_free (aw);
   }
   else
   {
     /* Remove session only: remove if active and update */
-    LOG(GNUNET_ERROR_TYPE_INFO,
-        "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
-        (GNUNET_NO == address->active) ? "inactive" : "active", address,
-        GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
-        net->active_addresses);
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
+         (GNUNET_NO == address->active) ? "inactive" : "active",
+         address,
+         GNUNET_i2s (&address->peer),
+         net->desc,
+         net->total_addresses,
+         net->active_addresses);
   }
 
   if (GNUNET_YES == address->active)
   {
     /* Address was active, remove from network and update quotas*/
     address->active = GNUNET_NO;
-    address->assigned_bw_in = BANDWIDTH_ZERO;
-    address->assigned_bw_out = BANDWIDTH_ZERO;
-    asi->calculated_quota_in_NBO = htonl (0);
-    asi->calculated_quota_out_NBO = htonl (0);
+    address->assigned_bw_in = 0;
+    address->assigned_bw_out = 0;
+    asi->calculated_quota_in = 0;
+    asi->calculated_quota_out = 0;
 
-    if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
+    if (GNUNET_SYSERR ==
+        addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
       GNUNET_break(0);
     distribute_bandwidth_in_network (s, net);
 
-    if (NULL == update_active_address (s, &address->peer))
+    if (NULL ==
+        update_active_address (s, &address->peer))
     {
       /* No alternative address found, disconnect peer */
       LOG (GNUNET_ERROR_TYPE_INFO,
-          "Disconnecting peer `%s' after deleting previous %s address %p\n",
-          GNUNET_i2s (&address->peer),
-          (GNUNET_NO == address->active) ? "inactive" : "active",
-              address);
-      s->bw_changed (s->bw_changed_cls, address);
+           "Disconnecting peer `%s' after deleting previous address %p\n",
+           GNUNET_i2s (&address->peer),
+           address);
+      s->env->bandwidth_changed_cb (s->env->cls,
+                                    address);
     }
   }
   if (GNUNET_NO == session_only)
@@ -1527,24 +1538,27 @@ GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
     address->solver_information = NULL;
   }
 
-  LOG(GNUNET_ERROR_TYPE_INFO,
-      "After deleting address now total %u and active %u addresses in network `%s'\n",
-      net->total_addresses, net->active_addresses, net->desc);
-
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "After deleting address now total %u and active %u addresses in network `%s'\n",
+       net->total_addresses,
+       net->active_addresses,
+       net->desc);
 }
 
+
 /**
  * Start a bulk operation
  *
  * @param solver the solver
  */
-void
+static void
 GAS_proportional_bulk_start (void *solver)
 {
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
-  struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
+  struct GAS_PROPORTIONAL_Handle *s = solver;
 
-  GNUNET_assert(NULL != solver);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Locking solver for bulk operation ...\n");
+  GNUNET_assert (NULL != solver);
   s->bulk_lock++;
 }
 
@@ -1552,14 +1566,13 @@ GAS_proportional_bulk_start (void *solver)
 /**
  * Bulk operation done
  */
-void
+static void
 GAS_proportional_bulk_stop (void *solver)
 {
-  LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
-
-  struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
-  GNUNET_assert(NULL != solver);
+  struct GAS_PROPORTIONAL_Handle *s = solver;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Unlocking solver from bulk operation ...\n");
   if (s->bulk_lock < 1)
   {
     GNUNET_break(0);
@@ -1582,9 +1595,11 @@ GAS_proportional_bulk_stop (void *solver)
  * @param address the address to add
  * @param network network type of this address
  */
-void
-GAS_proportional_address_add (void *solver, struct ATS_Address *address,
-    uint32_t network);
+static void
+GAS_proportional_address_add (void *solver,
+                              struct ATS_Address *address,
+                              uint32_t network);
+
 
 /**
  * Transport properties for this address have changed
@@ -1595,21 +1610,19 @@ GAS_proportional_address_add (void *solver, struct ATS_Address *address,
  * @param abs_value the absolute value of the property
  * @param rel_value the normalized value
  */
-void
+static void
 GAS_proportional_address_property_changed (void *solver,
-    struct ATS_Address *address, uint32_t type, uint32_t abs_value,
-    double rel_value)
+                                           struct ATS_Address *address,
+                                           enum GNUNET_ATS_Property type,
+                                           uint32_t abs_value,
+                                           double rel_value)
 {
-  struct GAS_PROPORTIONAL_Handle *s;
+  struct GAS_PROPORTIONAL_Handle *s = solver;
   struct Network *n;
   struct AddressSolverInformation *asi;
   struct ATS_Address *best_address;
   struct ATS_Address *active_address;
 
-  GNUNET_assert(NULL != solver);
-  GNUNET_assert(NULL != address);
-
-  s = (struct GAS_PROPORTIONAL_Handle *) solver;
   asi = address->solver_information;
   if (NULL == asi)
   {
@@ -1634,7 +1647,8 @@ GAS_proportional_address_property_changed (void *solver,
 
   /* This peer is requested, find active and best address */
   active_address = get_active_address(s, s->addresses, &address->peer);
-  best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
+  best_address = update_active_address (s,
+                                        &address->peer);
 
   if ((NULL != best_address) && ((NULL != active_address) &&
       (GNUNET_YES == address_eq (active_address, best_address))))
@@ -1647,249 +1661,168 @@ GAS_proportional_address_property_changed (void *solver,
   }
 }
 
+
 /**
- * Transport session for this address has changed
- *
- * NOTE: values in addresses are already updated
+ * Add a new single address to a network
  *
- * @param solver solver handle
- * @param address the address
- * @param cur_session the current session
- * @param new_session the new session
+ * @param solver the solver Handle
+ * @param address the address to add
+ * @param network network type of this address
  */
-void
-GAS_proportional_address_session_changed (void *solver,
-    struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
+static void
+GAS_proportional_address_add (void *solver,
+                              struct ATS_Address *address,
+                              enum GNUNET_ATS_Network_Type network)
 {
   struct GAS_PROPORTIONAL_Handle *s = solver;
-  struct ATS_Address *best_address;
-  struct ATS_Address *active_address;
+  struct Network *net;
+  struct AddressWrapper *aw;
   struct AddressSolverInformation *asi;
 
-  s = (struct GAS_PROPORTIONAL_Handle *) solver;
-  if (cur_session != new_session)
-  {
-    LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
-        new_session);
-  }
-
-  if (NULL == address->solver_information)
+  net = get_network (s, network);
+  if (NULL == net)
   {
     GNUNET_break (0);
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Unknown network %u `%s' for new address %p for peer `%s'\n",
+         network,
+         GNUNET_ATS_print_network_type (network),
+         address,
+         GNUNET_i2s (&address->peer));
     return;
   }
 
-  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
-    return; /* Peer is not requested */
-
-  /* This peer is requested, find active and best address */
-  active_address = get_active_address(s, s->addresses, &address->peer);
-  best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
-
-  if ((NULL != best_address) && ((NULL != active_address) &&
-      (GNUNET_YES == address_eq (active_address, best_address))))
-  {
-    asi = best_address->solver_information;
-    GNUNET_assert (NULL != asi);
-
-    /* We sticked to the same address, therefore redistribute  */
-    distribute_bandwidth_in_network (s, asi->network);
-  }
-}
-
-/**
- * Usage for this address has changed
- *
- * NOTE: values in addresses are already updated
- *
- * @param solver solver handle
- * @param address the address
- * @param in_use usage state
- */
-void
-GAS_proportional_address_inuse_changed (void *solver,
-    struct ATS_Address *address, int in_use)
-{
-  struct GAS_PROPORTIONAL_Handle *s;
-  struct Network *n;
-  struct AddressSolverInformation *asi;
-  struct ATS_Address *best_address;
-  struct ATS_Address *active_address;
-
-  GNUNET_assert(NULL != solver);
-  GNUNET_assert(NULL != address);
-
-  s = (struct GAS_PROPORTIONAL_Handle *) solver;
-  asi = address->solver_information;
-  if (NULL == asi)
-  {
-    GNUNET_break(0);
-    return;
-  }
+  aw = GNUNET_new (struct AddressWrapper);
+  aw->addr = address;
+  GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
+  address_increment (s, net, GNUNET_YES, GNUNET_NO);
 
-  n = asi->network;
-  if (NULL == n)
-  {
-    GNUNET_break(0);
-    return;
-  }
+  asi = GNUNET_new (struct AddressSolverInformation);
+  asi->network = net;
+  asi->calculated_quota_in = 0;
+  asi->calculated_quota_out = 0;
+  aw->addr->solver_information = asi;
 
   LOG(GNUNET_ERROR_TYPE_INFO,
-      "Usage of peer `%s' address %p changed to %s \n",
-      GNUNET_i2s (&address->peer), address,
-      (GNUNET_YES == in_use) ? "YES" : "NO");
+      "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
+      address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
 
   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
     return; /* Peer is not requested */
 
-  /* This peer is requested, find active and best address */
-  active_address = get_active_address(s, s->addresses, &address->peer);
-  best_address = (struct ATS_Address *) update_active_address (s, &address->peer);
+  /* This peer is requested, find best address */
+  update_active_address (s, &address->peer);
+}
 
-  if ((NULL != best_address) && ((NULL != active_address) &&
-      (GNUNET_YES == address_eq (active_address, best_address))))
-  {
-    asi = best_address->solver_information;
-    GNUNET_assert (NULL != asi);
 
-    /* We sticked to the same address, therefore redistribute  */
-    distribute_bandwidth_in_network (s, asi->network);
-  }
-}
 
 /**
- * Network scope for this address has changed
- *
- * NOTE: values in addresses are already updated
+ * Function invoked when the plugin is loaded.
  *
- * @param solver solver handle
- * @param address the address
- * @param current_network the current network
- * @param new_network the new network
+ * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
+ *            modified to return the API functions (ugh).
+ * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
  */
-void
-GAS_proportional_address_change_network (void *solver,
-    struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
+void *
+libgnunet_plugin_ats_proportional_init (void *cls)
 {
-  struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
-  struct AddressSolverInformation *asi;
-  int save_active = GNUNET_NO;
-
-  struct Network *new_net = NULL;
-
-  if (current_network == new_network)
-  {
-    GNUNET_break(0);
-    return;
-  }
-
-  asi = address->solver_information;
-  if (NULL == asi)
-  {
-    GNUNET_break(0);
-    return;
-  }
-
-  /* Network changed */
-  LOG(GNUNET_ERROR_TYPE_DEBUG,
-      "Network type changed, moving %s address from `%s' to `%s'\n",
-      (GNUNET_YES == address->active) ? "active" : "inactive",
-      GNUNET_ATS_print_network_type (current_network),
-      GNUNET_ATS_print_network_type (new_network));
-
+  static struct GNUNET_ATS_SolverFunctions sf;
+  struct GNUNET_ATS_PluginEnvironment *env = cls;
+  struct GAS_PROPORTIONAL_Handle *s;
+  struct Network * cur;
+  float f_tmp;
+  int c;
 
-  /* Start bulk to prevent disconnect */
-  GAS_proportional_bulk_start(s);
+  GNUNET_assert (NULL != env);
+  GNUNET_assert (NULL != env->cfg);
+  GNUNET_assert (NULL != env->bandwidth_changed_cb);
+  GNUNET_assert (NULL != env->get_preferences);
 
-  save_active = address->active;
+  s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
+  s->env = env;
+  sf.cls = s;
+  sf.s_add = &GAS_proportional_address_add;
+  sf.s_address_update_property = &GAS_proportional_address_property_changed;
+  sf.s_get = &GAS_proportional_get_preferred_address;
+  sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
+  sf.s_pref = &GAS_proportional_address_change_preference;
+  sf.s_feedback = &GAS_proportional_address_preference_feedback;
+  sf.s_del = &GAS_proportional_address_delete;
+  sf.s_bulk_start = &GAS_proportional_bulk_start;
+  sf.s_bulk_stop = &GAS_proportional_bulk_stop;
 
-  /* Disable and assign no bandwidth */
-  address->active = GNUNET_NO;
-  address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
-  address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
+  s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
 
-  /* Remove from old network */
-  GAS_proportional_address_delete (solver, address, GNUNET_NO);
+  /* Init */
+  s->active_addresses = 0;
+  s->total_addresses = 0;
+  s->bulk_lock = GNUNET_NO;
+  s->addresses = env->addresses;
+  s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
 
-  /* Set new network type */
-  if (NULL == (new_net = get_network (solver, new_network)))
-  {
-    /* Address changed to invalid network... */
-    LOG(GNUNET_ERROR_TYPE_ERROR,
-        _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
-        GNUNET_ATS_print_network_type (new_network));
-    s->bw_changed (s->bw_changed_cls, address);
-  }
-  else
+  s->stability_factor = PROP_STABILITY_FACTOR;
+  if (GNUNET_SYSERR !=
+      GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+                                            "PROP_STABILITY_FACTOR", &f_tmp))
   {
-    /* Add to new network and update*/
-    GAS_proportional_address_add (solver, address, new_network);
+    if ((f_tmp < 1.0) || (f_tmp > 2.0))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           _("Invalid %s configuration %f \n"),
+           "PROP_STABILITY_FACTOR",
+           f_tmp);
+    }
+    else
+    {
+      s->stability_factor = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO,
+           "Using %s of %.3f\n",
+           "PROP_STABILITY_FACTOR", f_tmp);
+    }
   }
-  GAS_proportional_bulk_stop (s);
-
-  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
-    return; /* Peer is not requested */
 
-  /* Find new address to suggest */
-  if (GNUNET_YES == save_active)
+  s->prop_factor = PROPORTIONALITY_FACTOR;
+  if (GNUNET_SYSERR !=
+      GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
+                                            "PROP_PROPORTIONALITY_FACTOR",
+                                            &f_tmp))
   {
-    /* No address available, therefore disconnect */
-    if (NULL == update_active_address (s, &address->peer))
-      s->bw_changed (s->bw_changed_cls, address);
+    if (f_tmp < 1.0)
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           _("Invalid %s configuration %f \n"),
+           "PROP_PROPORTIONALITY_FACTOR", f_tmp);
+    }
+    else
+    {
+      s->prop_factor = f_tmp;
+      LOG (GNUNET_ERROR_TYPE_INFO,
+           "Using %s of %.3f\n",
+           "PROP_PROPORTIONALITY_FACTOR", f_tmp);
+    }
   }
 
-}
-
-/**
- * Add a new single address to a network
- *
- * @param solver the solver Handle
- * @param address the address to add
- * @param network network type of this address
- */
-void
-GAS_proportional_address_add (void *solver, struct ATS_Address *address,
-    uint32_t network)
-{
-  struct GAS_PROPORTIONAL_Handle *s = solver;
-  struct Network *net = NULL;
-  struct AddressWrapper *aw = NULL;
-  struct AddressSolverInformation *asi;
-
-  GNUNET_assert(NULL != s);
-  net = get_network (s, network);
-  if (NULL == net)
+  for (c = 0; c < env->network_count; c++)
   {
-    GNUNET_break(0);
-
-    LOG(GNUNET_ERROR_TYPE_ERROR,
-        "Unknown network %u `%s' for new address %p for peer `%s'\n",
-        network, GNUNET_ATS_print_network_type(network),
-        address, GNUNET_i2s(&address->peer));
-
-    return;
+    cur = &s->network_entries[c];
+    cur->type = c;
+    cur->total_quota_in = env->in_quota[c];
+    cur->total_quota_out = env->out_quota[c];
+    cur->desc = GNUNET_ATS_print_network_type (c);
+    GNUNET_asprintf (&cur->stat_total,
+                     "# ATS addresses %s total",
+                     cur->desc);
+    GNUNET_asprintf (&cur->stat_active,
+                     "# ATS active addresses %s total",
+                     cur->desc);
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Added network %u `%s' (%llu/%llu)\n",
+         c,
+         cur->desc,
+         cur->total_quota_in,
+         cur->total_quota_out);
   }
-
-  aw = GNUNET_new (struct AddressWrapper);
-  aw->addr = address;
-  GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
-  addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
-
-  asi = GNUNET_new (struct AddressSolverInformation);
-  asi->network = net;
-  asi->calculated_quota_in_NBO = htonl (0);
-  asi->calculated_quota_out_NBO = htonl (0);
-  aw->addr->solver_information = asi;
-
-  LOG(GNUNET_ERROR_TYPE_INFO,
-      "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
-      address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
-
-  if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
-    return; /* Peer is not requested */
-
-  /* This peer is requested, find best address */
-  update_active_address (s, &address->peer);
+  return &sf;
 }