adding additional INFO
[oweals/gnunet.git] / src / ats / libgnunet_plugin_ats_proportional.c
1 /*
2  This file is part of GNUnet.
3  (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file ats/libgnunet_plugin_ats_proportional.c
23  * @brief ATS proportional solver
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "libgnunet_plugin_ats_proportional.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
30
31
32 /**
33  *
34  * NOTE: Do not change this documentation. This documentation is based
35  * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
36  * use build_txt.sh to generate plaintext output
37  *
38  * ATS addresses : proportional solver
39  *
40  *    The proportional solver ("proportional") distributes the available
41  *    bandwidth fair over all the addresses influenced by the
42  *    preference values. For each available network type an in- and
43  *    outbound quota is configured and the bandwidth available in
44  *    these networks is distributed over the addresses.  The solver
45  *    first assigns every addresses the minimum amount of bandwidth
46  *    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
47  *    remaining bandwidth available according to the preference
48  *    values. For each peer only a single address gets bandwidth
49  *    assigned and only one address marked as active.  The most
50  *    important functionality for the solver is implemented in: *
51  *    find_address_it is an hashmap iterator returning the prefered
52  *    address for an peer * update_quota_per_network distributes
53  *    available bandwidth for a network over active addresses
54  *
55  *    Changes to addresses automatically have an impact on the the
56  *    bandwidth assigned to other addresses in the same network since
57  *    the solver distributes the remaining bandwidth over the
58  *    addresses in the network.  When changes to the addresses occur,
59  *    the solver first performs the changes, like adding or deleting
60  *    addresses, and then updates bandwidth assignment for the
61  *    affected network. Bandwidth assignment is only recalculated on
62  *    demand when an address is requested by a client for a peer or
63  *    when the addresses available have changed or an address changed
64  *    the network it is located in. When the bandwidth assignment has
65  *    changed the callback is called with the new bandwidth
66  *    assignments. The bandwidth distribution for a network is
67  *    recalculated due to: * address suggestion requests * address
68  *    deletions * address switching networks during address update *
69  *    preference changes
70  *
71  *     3.1 Data structures used
72  *
73  *    For each ATS network (e.g. WAN, LAN, loopback) a struct Network
74  *    is used to specify network related information as total adresses
75  *    and active addresses in this network and the configured in- and
76  *    outbound quota. Each network also contains a list of addresses
77  *    added to the solver located in this network. The proportional
78  *    solver uses the addresses' solver_information field to store the
79  *    proportional network it belongs to for each address.
80  *
81  *     3.2 Initializing
82  *
83  *    When the proportional solver is initialized the solver creates a
84  *    new solver handle and initializes the network structures with
85  *    the quotas passed from addresses and returns the handle solver.
86  *
87  *     3.3 Adding an address
88  *
89  *    When a new address is added to the solver using s_add, a lookup
90  *    for the network for this address is done and the address is
91  *    enqueued in in the linked list of the network.
92  *
93  *     3.4 Updating an address
94  *
95  *    The main purpose of address updates is to update the ATS
96  *    information for addresse selection. Important for the proportional
97  *    solver is when an address switches network it is located
98  *    in. This is common because addresses added by transport's
99  *    validation mechanism are commonly located in
100  *    GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
101  *    in this network type and only if a connection is successful on
102  *    return of payload data transport switches to the real network
103  *    the address is located in.  When an address changes networks it
104  *    is first of all removed from the old network using the solver
105  *    API function GAS_proportional_address_delete and the network in
106  *    the address struct is updated. A lookup for the respective new
107  *    proportional network is done and stored in the addresse's
108  *    solver_information field. Next the address is re-added to the
109  *    solver using the solver API function
110  *    GAS_proportional_address_add. If the address was marked as in
111  *    active, the solver checks if bandwidth is available in the
112  *    network and if yes sets the address to active and updates the
113  *    bandwidth distribution in this network. If no bandwidth is
114  *    available it sets the bandwidth for this address to 0 and tries
115  *    to suggest an alternative address. If an alternative address was
116  *    found, addresses' callback is called for this address.
117  *
118  *     3.5 Deleting an address
119  *
120  *    When an address is removed from the solver, it removes the
121  *    respective address from the network and if the address was
122  *    marked as active, it updates the bandwidth distribution for this
123  *    network.
124  *
125  *     3.6 Requesting addresses
126  *
127  *    When an address is requested for a peer the solver performs a
128  *    lookup for the peer entry in addresses address hashmap and
129  *    selects the best address.  The selection of the most suitable
130  *    address is done in the find_address_it hashmap iterator
131  *    described in detail in section 3.7. If no address is returned,
132  *    no address can be suggested at the moment. If the address
133  *    returned is marked as active, the solver can return this
134  *    address. If the address is not marked as active, the solver
135  *    checks if another address belongign to this peer is marked as
136  *    active and marks the address as inactive, updates the bandwidth
137  *    for this address to 0, call the bandwidth changed callback for
138  *    this address due to the change and updates quota assignment for
139  *    the addresse's network. the now in-active address is belonging
140  *    to. The solver marks the new address as active and updates the
141  *    bandwidth assignment for this network.
142  *
143  *     3.7 Choosing addresses
144  *
145  *    Choosing the best possible address for suggestion is done by
146  *    iterating over all addresses of a peer stored in addresses'
147  *    hashmap and using the hashmap iterator find_address_it to select
148  *    the best available address.  Several checks are done when an
149  *    address is selected. First if this address is currently blocked
150  *    by addresses from being suggested. An address is blocked for the
151  *    duration of ATS_BLOCKING_DELTA when it is suggested to
152  *    transport. Next it is checked if at least
153  *    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
154  *    in the addresse's network, because suggesting an address without
155  *    bandwidth does not make sense. This also ensures that all active
156  *    addresses in this network get at least the minimum amount of
157  *    bandwidth assigned. In the next step the solver ensures that for
158  *    tcp connections inbound connections are prefered over outbound
159  *    connections. In the next stet the solver ensures that
160  *    connections are prefered in the following order: * connections
161  *    are already established and have bandwidth assigned *
162  *    connections with a shorter distance * connectes have a shorter
163  *    latency
164  *
165  *     3.8 Changing preferences
166  *
167  *     3.9 Shutdown
168  *
169  *    During shutdown all network entries and aging processes are
170  *    destroyed and freed.
171  *
172  *
173  * OLD DOCUMENTATION
174  *
175  * This solver assigns in and outbound bandwidth equally for all
176  * addresses in specific network type (WAN, LAN) based on configured
177  * in and outbound quota for this network.
178  *
179  * The solver is notified by addresses about changes to the addresses
180  * and recalculates the bandwith assigned if required. The solver
181  * notifies addresses by calling the GAS_bandwidth_changed_cb
182  * callback.
183  *
184  * - Initialization
185  *
186  *
187  *
188  *
189  * For each peer only a single is selected and marked as "active" in the address
190  * struct.
191  *
192  * E.g.:
193  *
194  * You have the networks WAN and LAN and quotas
195  * WAN_TOTAL_IN, WAN_TOTAL_OUT
196  * LAN_TOTAL_IN, LAN_TOTAL_OUT
197  *
198  * If you have x addresses in the network segment LAN, the quotas are
199  * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
200  *
201  * Quotas are automatically recalculated and reported back when addresses are
202  * - requested
203  *
204  */
205
206 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
207 #define PREF_AGING_FACTOR 0.95
208
209 #define DEFAULT_REL_PREFERENCE 1.0
210 #define DEFAULT_ABS_PREFERENCE 0.0
211 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
212
213 /**
214  * A handle for the proportional solver
215  */
216 struct GAS_PROPORTIONAL_Handle
217 {
218    struct GNUNET_ATS_PluginEnvironment *env;
219
220   /**
221    * Statistics handle
222    */
223   struct GNUNET_STATISTICS_Handle *stats;
224
225   /**
226    * Hashmap containing all valid addresses
227    */
228   const struct GNUNET_CONTAINER_MultiPeerMap *addresses;
229
230   /**
231    * Pending address requests
232    */
233   struct GNUNET_CONTAINER_MultiPeerMap *requests;
234
235   /**
236    * Bandwidth changed callback
237    */
238   GAS_bandwidth_changed_cb bw_changed;
239
240   /**
241    * Bandwidth changed callback cls
242    */
243   void *bw_changed_cls;
244
245   /**
246    * ATS function to get preferences
247    */
248   GAS_get_preferences get_preferences;
249
250   /**
251    * Closure for ATS function to get preferences
252    */
253   void *get_preferences_cls;
254
255   /**
256    * ATS function to get properties
257    */
258   GAS_get_properties get_properties;
259
260   /**
261    * Closure for ATS function to get properties
262    */
263   void *get_properties_cls;
264
265   /**
266    * Bulk lock
267    */
268   int bulk_lock;
269
270   /**
271    * Number of changes while solver was locked
272    */
273   int bulk_requests;
274
275   /**
276    * Total number of addresses for solver
277    */
278   unsigned int total_addresses;
279
280   /**
281    * Number of active addresses for solver
282    */
283   unsigned int active_addresses;
284
285   /**
286    * Networks array
287    */
288   struct Network *network_entries;
289
290   /**
291    * Number of networks
292    */
293   unsigned int network_count;
294
295 };
296
297 /**
298  * Representation of a network
299  */
300 struct Network
301 {
302   /**
303    * ATS network type
304    */
305   unsigned int type;
306
307   /**
308    * Network description
309    */
310   char *desc;
311
312   /**
313    * Total inbound quota
314    *
315    */
316   unsigned long long total_quota_in;
317
318   /**
319    * Total outbound quota
320    *
321    */
322   unsigned long long total_quota_out;
323
324   /**
325    * Number of active addresses for this network
326    */
327   unsigned int active_addresses;
328
329   /**
330    * Number of total addresses for this network
331    */
332   unsigned int total_addresses;
333
334   /**
335    * String for statistics total addresses
336    */
337   char *stat_total;
338
339   /**
340    * String for statistics active addresses
341    */
342   char *stat_active;
343
344   struct AddressWrapper *head;
345   struct AddressWrapper *tail;
346 };
347
348 /**
349  * Wrapper for addresses to store them in network's linked list
350  */
351 struct AddressWrapper
352 {
353   /**
354    * Next in DLL
355    */
356   struct AddressWrapper *next;
357
358   /**
359    * Previous in DLL
360    */
361   struct AddressWrapper *prev;
362
363   /**
364    * The address
365    */
366   struct ATS_Address *addr;
367 };
368
369 /**
370  *  Important solver functions
371  *  ---------------------------
372  */
373
374 void *
375 libgnunet_plugin_ats_proportional_init (void *cls)
376 {
377   struct GNUNET_ATS_PluginEnvironment *env = cls;
378   struct GAS_PROPORTIONAL_Handle *s;
379   struct Network * cur;
380   char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
381   int c;
382
383   GNUNET_assert (NULL != env);
384   GNUNET_assert(NULL != env->cfg);
385   GNUNET_assert(NULL != env->stats);
386   GNUNET_assert(NULL != env->bandwidth_changed_cb);
387   GNUNET_assert(NULL != env->get_preferences);
388   GNUNET_assert(NULL != env->get_property);
389
390   s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
391   s->env = env;
392   env->sf.s_add = &GAS_proportional_address_add;
393   env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
394   env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
395   env->sf.s_address_update_inuse = &GAS_proportional_address_inuse_changed;
396   env->sf.s_address_update_network = &GAS_proportional_address_change_network;
397   env->sf.s_get = &GAS_proportional_get_preferred_address;
398   env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
399   env->sf.s_pref = &GAS_proportional_address_change_preference;
400   env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
401   env->sf.s_del = &GAS_proportional_address_delete;
402   env->sf.s_bulk_start = &GAS_proportional_bulk_start;
403   env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
404
405   s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
406   s->bw_changed = env->bandwidth_changed_cb;
407   s->bw_changed_cls = env->bw_changed_cb_cls;
408   s->get_preferences = env->get_preferences;
409   s->get_preferences_cls = env->get_preference_cls;
410   s->get_properties = env->get_property;
411   s->get_properties_cls = env->get_property_cls;
412   s->network_count = env->network_count;
413   s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
414
415   /* Init */
416   s->active_addresses = 0;
417   s->total_addresses = 0;
418   s->bulk_lock = GNUNET_NO;
419   s->addresses = env->addresses;
420   s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
421
422   for (c = 0; c < env->network_count; c++)
423   {
424     cur = &s->network_entries[c];
425     cur->total_addresses = 0;
426     cur->active_addresses = 0;
427     cur->type = env->networks[c];
428     cur->total_quota_in = env->in_quota[c];
429     cur->total_quota_out = env->out_quota[c];
430     cur->desc = net_str[c];
431     GNUNET_asprintf (&cur->stat_total,
432         "# ATS addresses %s total", cur->desc);
433     GNUNET_asprintf (&cur->stat_active,
434         "# ATS active addresses %s total", cur->desc);
435     LOG (GNUNET_ERROR_TYPE_INFO, "Added network %u `%s' %p\n", c, cur->desc, s);
436   }
437   return s;
438 }
439
440 void *
441 libgnunet_plugin_ats_proportional_done (void *cls)
442 {
443   struct GAS_PROPORTIONAL_Handle *s = cls;
444   struct AddressWrapper *cur;
445   struct AddressWrapper *next;
446   int c;
447   GNUNET_assert(s != NULL);
448   for (c = 0; c < s->network_count; c++)
449   {
450     if (s->network_entries[c].total_addresses > 0)
451     {
452       LOG(GNUNET_ERROR_TYPE_DEBUG,
453           "Had %u addresses for network `%s' not deleted during shutdown\n",
454           s->network_entries[c].total_addresses, s->network_entries[c].desc);
455       GNUNET_break(0);
456     }
457
458     if (s->network_entries[c].active_addresses > 0)
459     {
460       LOG(GNUNET_ERROR_TYPE_ERROR,
461           "Had %u active addresses for network `%s' not deleted during shutdown\n",
462           s->network_entries[c].active_addresses, s->network_entries[c].desc);
463       GNUNET_break(0);
464     }
465
466     next = s->network_entries[c].head;
467     while (NULL != (cur = next))
468     {
469       next = cur->next;
470       GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
471           s->network_entries[c].tail, cur);
472       GNUNET_free(cur);
473     }
474     GNUNET_free(s->network_entries[c].stat_total);
475     GNUNET_free(s->network_entries[c].stat_active);
476   }
477   if (s->total_addresses > 0)
478   {
479     LOG(GNUNET_ERROR_TYPE_ERROR,
480         "Had %u addresses not deleted during shutdown\n", s->total_addresses);
481     GNUNET_break(0);
482   }
483   if (s->active_addresses > 0)
484   {
485     LOG(GNUNET_ERROR_TYPE_ERROR,
486         "Had %u active addresses not deleted during shutdown\n",
487         s->active_addresses);
488     GNUNET_break (0);
489   }
490   GNUNET_free (s->network_entries);
491   GNUNET_CONTAINER_multipeermap_destroy (s->requests);
492   GNUNET_free (s);
493   return NULL;
494 }
495
496
497 /**
498  * Test if bandwidth is available in this network to add an additional address
499  *
500  * @param net the network type to update
501  * @return GNUNET_YES or GNUNET_NO
502  */
503 static int
504 is_bandwidth_available_in_network (struct Network *net)
505 {
506   GNUNET_assert(NULL != net);
507   unsigned int na = net->active_addresses + 1;
508   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
509   if (((net->total_quota_in / na) > min_bw)
510       && ((net->total_quota_out / na) > min_bw))
511   {
512     LOG(GNUNET_ERROR_TYPE_DEBUG,
513         "Enough bandwidth available for %u active addresses in network `%s'\n",
514         na, net->desc);
515
516     return GNUNET_YES;
517   }
518   LOG(GNUNET_ERROR_TYPE_DEBUG,
519       "Not enough bandwidth available for %u active addresses in network `%s'\n",
520       na, net->desc);
521   return GNUNET_NO;
522 }
523
524 /**
525  * Update bandwidth assigned to peers in this network
526  *
527  * @param s the solver handle
528  * @param net the network type to update
529  * @param address_except address excluded from notification, since we suggest
530  * this address
531  */
532 static void
533 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
534     struct Network *net, struct ATS_Address *address_except)
535 {
536   unsigned long long remaining_quota_in = 0;
537   unsigned long long quota_out_used = 0;
538
539   unsigned long long remaining_quota_out = 0;
540   unsigned long long quota_in_used = 0;
541   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
542   double peer_prefs;
543   double total_prefs; /* Important: has to be double not float due to precision */
544   double cur_pref; /* Important: has to be double not float due to precision */
545   const double *t = NULL; /* Important: has to be double not float due to precision */
546   int c;
547
548   unsigned long long assigned_quota_in = 0;
549   unsigned long long assigned_quota_out = 0;
550   struct AddressWrapper *cur;
551
552   LOG(GNUNET_ERROR_TYPE_DEBUG,
553       "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
554       net->desc, net->active_addresses, net->total_quota_in,
555       net->total_quota_in);
556
557   if (net->active_addresses == 0)
558     return; /* no addresses to update */
559
560   /* Idea
561    * Assign every peer in network minimum Bandwidth
562    * Distribute bandwidth left according to preference
563    */
564
565   if ((net->active_addresses * min_bw) > net->total_quota_in)
566   {
567     GNUNET_break(0);
568     return;
569   }
570   if ((net->active_addresses * min_bw) > net->total_quota_out)
571   {
572     GNUNET_break(0);
573     return;
574   }
575
576   remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
577   remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
578   LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
579       remaining_quota_in, remaining_quota_out);
580   total_prefs = 0.0;
581   for (cur = net->head; NULL != cur; cur = cur->next)
582   {
583     if (GNUNET_YES == cur->addr->active)
584     {
585       GNUNET_assert(
586           NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
587
588       peer_prefs = 0.0;
589       for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
590       {
591         if (c != GNUNET_ATS_PREFERENCE_END)
592         {
593           //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
594           peer_prefs += t[c];
595         }
596       }
597       total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount - 1));
598     }
599   }
600   for (cur = net->head; NULL != cur; cur = cur->next)
601   {
602     if (GNUNET_YES == cur->addr->active)
603     {
604       cur_pref = 0.0;
605       GNUNET_assert(
606           NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
607
608       for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
609       {
610         if (c != GNUNET_ATS_PREFERENCE_END)
611           cur_pref += t[c];
612       }
613       cur_pref /= 2;
614
615       assigned_quota_in = min_bw
616           + ((cur_pref / total_prefs) * remaining_quota_in);
617       assigned_quota_out = min_bw
618           + ((cur_pref / total_prefs) * remaining_quota_out);
619
620       LOG(GNUNET_ERROR_TYPE_DEBUG,
621           "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
622           GNUNET_i2s (&cur->addr->peer), cur_pref, total_prefs,
623           assigned_quota_in, assigned_quota_out);
624     }
625     else
626     {
627       assigned_quota_in = 0;
628       assigned_quota_out = 0;
629     }
630
631     quota_in_used += assigned_quota_in;
632     quota_out_used += assigned_quota_out;
633     /* Prevent overflow due to rounding errors */
634     if (assigned_quota_in > UINT32_MAX)
635       assigned_quota_in = UINT32_MAX;
636     if (assigned_quota_out > UINT32_MAX)
637       assigned_quota_out = UINT32_MAX;
638
639     /* Compare to current bandwidth assigned */
640     if ((assigned_quota_in != ntohl (cur->addr->assigned_bw_in.value__))
641         || (assigned_quota_out != ntohl (cur->addr->assigned_bw_out.value__)))
642     {
643       cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
644       cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
645       /* Notify on change */
646       if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
647         s->bw_changed (s->bw_changed_cls, cur->addr);
648     }
649
650   }
651   LOG(GNUNET_ERROR_TYPE_DEBUG,
652       "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
653       quota_out_used);
654   if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
655   {
656     LOG(GNUNET_ERROR_TYPE_ERROR,
657         "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
658         net->active_addresses, quota_out_used, net->total_quota_out);
659   }
660   if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
661   {
662     LOG(GNUNET_ERROR_TYPE_ERROR,
663         "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
664         net->active_addresses, quota_in_used, net->total_quota_in);
665   }
666 }
667
668 struct FindBestAddressCtx
669 {
670   struct GAS_PROPORTIONAL_Handle *s;
671   struct ATS_Address *best;
672 };
673
674 static int
675 find_property_index (uint32_t type)
676 {
677   int existing_types[] = GNUNET_ATS_QualityProperties;
678   int c;
679   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
680     if (existing_types[c] == type)
681       return c;
682   return GNUNET_SYSERR;
683 }
684
685 /**
686  * Find a "good" address to use for a peer by iterating over the addresses for this peer.
687  * If we already have an existing address, we stick to it.
688  * Otherwise, we pick by lowest distance and then by lowest latency.
689  *
690  * @param cls the 'struct ATS_Address**' where we store the result
691  * @param key unused
692  * @param value another 'struct ATS_Address*' to consider using
693  * @return GNUNET_OK (continue to iterate)
694  */
695 static int
696 find_best_address_it (void *cls,
697                       const struct GNUNET_PeerIdentity *key,
698                       void *value)
699 {
700   struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
701   struct ATS_Address *current = (struct ATS_Address *) value;
702   struct GNUNET_TIME_Absolute now;
703   struct Network *net = (struct Network *) current->solver_information;
704   const double *norm_prop_cur;
705   const double *norm_prop_prev;
706   int index;
707
708   now = GNUNET_TIME_absolute_get ();
709
710   if (current->blocked_until.abs_value_us
711       == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us)
712   {
713     /* This address is blocked for suggestion */
714     LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
715         current,
716         GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
717     return GNUNET_OK;
718   }
719   if (GNUNET_NO == is_bandwidth_available_in_network (net))
720     return GNUNET_OK; /* There's no bandwidth available in this network */
721   if (NULL != fba_ctx->best)
722   {
723     GNUNET_assert(NULL != fba_ctx->best->plugin);
724     GNUNET_assert(NULL != current->plugin);
725     if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
726     {
727       if ((0 != fba_ctx->best->addr_len) && (0 == current->addr_len))
728       {
729         /* saved address was an outbound address, but we have an inbound address */
730         fba_ctx->best = current;
731         return GNUNET_OK;
732       }
733       if (0 == fba_ctx->best->addr_len)
734       {
735         /* saved address was an inbound address, so do not overwrite */
736         return GNUNET_OK;
737       }
738     }
739   }
740   if (NULL == fba_ctx->best)
741   {
742     fba_ctx->best = current;
743     return GNUNET_OK;
744   }
745   if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0)
746       && (ntohl (current->assigned_bw_in.value__) > 0))
747   {
748     /* stick to existing connection */
749     fba_ctx->best = current;
750     return GNUNET_OK;
751   }
752
753   norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
754       (const struct ATS_Address *) current);
755   norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
756       (const struct ATS_Address *) fba_ctx->best);
757   /*
758    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s previous %.2f current %.2f\n",
759    "DISTANCE", norm_prop_cur[1], norm_prop_cur[1]);
760    */
761   index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
762   if (GNUNET_SYSERR == index)
763   {
764     GNUNET_break(0);
765     return GNUNET_OK;
766   }
767   if (norm_prop_cur[index] < norm_prop_prev[index])
768   {
769     /* user shorter distance */
770     fba_ctx->best = current;
771     return GNUNET_OK;
772   }
773   /*
774    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s previous %.2f current %.2f\n",
775    "DELAY", norm_prop_cur[1], norm_prop_cur[1]);
776    */
777   index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
778   if (GNUNET_SYSERR == index)
779   {
780     GNUNET_break(0);
781     return GNUNET_OK;
782   }
783   if (norm_prop_cur[index] < norm_prop_prev[index])
784   {
785     /* user shorter delay */
786     fba_ctx->best = current;
787     return GNUNET_OK;
788   }
789
790   /* don't care */
791   return GNUNET_OK;
792 }
793
794 /**
795  *  Helper functions
796  *  ---------------------------
797  */
798
799 /**
800  * Distribibute bandwidth
801  *
802  * @param n the network, can be NULL for all networksa
803  */
804
805 static void distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
806     struct Network *n,
807     struct ATS_Address *address_except)
808 {
809   if (GNUNET_YES == s->bulk_lock)
810   {
811     s->bulk_requests++;
812     return;
813   }
814
815   if (NULL != n)
816   {
817     if (NULL != s->env->info_cb)
818       s->env->info_cb (s->env->info_cb_cls, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
819     distribute_bandwidth (s, n, address_except);
820     if (NULL != s->env->info_cb)
821       s->env->info_cb (s->env->info_cb_cls, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
822   }
823   else
824   {
825     int i;
826     if (NULL != s->env->info_cb)
827       s->env->info_cb (s->env->info_cb_cls, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
828     for (i = 0; i < s->network_count; i++)
829       distribute_bandwidth (s, &s->network_entries[i], NULL );
830     if (NULL != s->env->info_cb)
831       s->env->info_cb (s->env->info_cb_cls, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
832   }
833
834 }
835
836 /**
837  * Lookup network struct by type
838  *
839  * @param s the solver handle
840  * @param type the network type
841  * @return the network struct
842  */
843 static struct Network *
844 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
845 {
846   int c;
847   for (c = 0; c < s->network_count; c++)
848   {
849     if (s->network_entries[c].type == type)
850       return &s->network_entries[c];
851   }
852   return NULL ;
853 }
854
855 /**
856  * Hashmap Iterator to find current active address for peer
857  *
858  * @param cls last active address
859  * @param key peer's key
860  * @param value address to check
861  * @return #GNUNET_NO on double active address else #GNUNET_YES;
862  */
863 static int
864 get_active_address_it (void *cls,
865                        const struct GNUNET_PeerIdentity *key,
866                        void *value)
867 {
868   struct ATS_Address **dest = cls;
869   struct ATS_Address *aa = (struct ATS_Address *) value;
870
871   if (GNUNET_YES == aa->active)
872   {
873
874     if (NULL != (*dest))
875     {
876       /* should never happen */
877       LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
878           GNUNET_i2s (&aa->peer));
879       GNUNET_break(0);
880       return GNUNET_NO;
881     }
882     (*dest) = aa;
883   }
884   return GNUNET_OK;
885 }
886
887 /**
888  * Find current active address for peer
889  *
890  * @param solver the solver handle
891  * @param addresses the address set
892  * @param peer the peer
893  * @return active address or NULL
894  */
895 static struct ATS_Address *
896 get_active_address (void *solver,
897                     const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
898                     const struct GNUNET_PeerIdentity *peer)
899 {
900   struct ATS_Address * dest = NULL;
901
902   GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
903                                               &get_active_address_it, &dest);
904   return dest;
905 }
906
907
908 static void
909 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
910     int total, int active)
911 {
912   if (GNUNET_YES == total)
913   {
914     s->total_addresses++;
915     net->total_addresses++;
916     GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
917     GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
918   }
919   if (GNUNET_YES == active)
920   {
921     net->active_addresses++;
922     s->active_addresses++;
923     GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
924         GNUNET_NO);
925     GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
926   }
927
928 }
929
930
931 static int
932 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
933     int total, int active)
934 {
935   int res = GNUNET_OK;
936   if (GNUNET_YES == total)
937   {
938     if (s->total_addresses < 1)
939     {
940       GNUNET_break(0);
941       res = GNUNET_SYSERR;
942     }
943     else
944     {
945       s->total_addresses--;
946       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
947           GNUNET_NO);
948     }
949     if (net->total_addresses < 1)
950     {
951       GNUNET_break(0);
952       res = GNUNET_SYSERR;
953     }
954     else
955     {
956       net->total_addresses--;
957       GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
958     }
959   }
960
961   if (GNUNET_YES == active)
962   {
963     if (net->active_addresses < 1)
964     {
965       GNUNET_break(0);
966       res = GNUNET_SYSERR;
967     }
968     else
969     {
970       net->active_addresses--;
971       GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
972     }
973     if (s->active_addresses < 1)
974     {
975       GNUNET_break(0);
976       res = GNUNET_SYSERR;
977     }
978     else
979     {
980       s->active_addresses--;
981       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
982           GNUNET_NO);
983     }
984   }
985   return res;
986 }
987
988 /**
989  *  Solver API functions
990  *  ---------------------------
991  */
992
993 /**
994  * Changes the preferences for a peer in the problem
995  *
996  * @param solver the solver handle
997  * @param peer the peer to change the preference for
998  * @param kind the kind to change the preference
999  * @param pref_rel the normalized preference value for this kind over all clients
1000  */
1001 void
1002 GAS_proportional_address_change_preference (void *solver,
1003                                             const struct GNUNET_PeerIdentity *peer,
1004                                             enum GNUNET_ATS_PreferenceKind kind,
1005                                             double pref_rel)
1006 {
1007   struct GAS_PROPORTIONAL_Handle *s = solver;
1008   GNUNET_assert(NULL != solver);
1009   GNUNET_assert(NULL != peer);
1010
1011   distribute_bandwidth_in_network (s, NULL, NULL);
1012 }
1013
1014
1015 /**
1016  * Get application feedback for a peer
1017  *
1018  * @param solver the solver handle
1019  * @param application the application
1020  * @param peer the peer to change the preference for
1021  * @param scope the time interval for this feedback: [now - scope .. now]
1022  * @param kind the kind to change the preference
1023  * @param score the score
1024  */
1025 void
1026 GAS_proportional_address_preference_feedback (void *solver, void *application,
1027     const struct GNUNET_PeerIdentity *peer,
1028     const struct GNUNET_TIME_Relative scope,
1029     enum GNUNET_ATS_PreferenceKind kind, double score)
1030 {
1031   struct GAS_PROPORTIONAL_Handle *s = solver;
1032   GNUNET_assert(NULL != solver);
1033   GNUNET_assert(NULL != peer);
1034
1035   GNUNET_assert(NULL != s);
1036   GNUNET_break(0);
1037 }
1038
1039 /**
1040  * Get the preferred address for a specific peer
1041  *
1042  * @param solver the solver handle
1043  * @param peer the identity of the peer
1044  */
1045 const struct ATS_Address *
1046 GAS_proportional_get_preferred_address (void *solver,
1047     const struct GNUNET_PeerIdentity *peer)
1048 {
1049   struct GAS_PROPORTIONAL_Handle *s = solver;
1050   struct Network *net_prev;
1051   struct Network *net_cur;
1052   struct ATS_Address *prev;
1053   struct FindBestAddressCtx fba_ctx;
1054
1055   GNUNET_assert(s != NULL);
1056   GNUNET_assert(peer != NULL);
1057
1058   /* Add to list of pending requests */
1059   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1060                                                            peer))
1061   {
1062     GNUNET_assert (GNUNET_OK ==
1063                    GNUNET_CONTAINER_multipeermap_put (s->requests,
1064                                                       peer, NULL,
1065                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1066   }
1067
1068   /* Get address with: stick to current address, lower distance, lower latency */
1069   fba_ctx.s = s;
1070   fba_ctx.best = NULL;
1071
1072   GNUNET_CONTAINER_multipeermap_get_multiple (s->addresses, peer,
1073                                               &find_best_address_it, &fba_ctx);
1074   if (NULL == fba_ctx.best)
1075   {
1076     LOG(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1077         GNUNET_i2s (peer));
1078     return NULL ;
1079   }
1080
1081   LOG(GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
1082       (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active", fba_ctx.best,
1083       GNUNET_i2s (peer));
1084   net_cur = (struct Network *) fba_ctx.best->solver_information;
1085   if (NULL == fba_ctx.best)
1086   {
1087     LOG(GNUNET_ERROR_TYPE_ERROR,
1088         "Trying to suggesting unknown address peer `%s'\n", GNUNET_i2s (peer));
1089     GNUNET_break(0);
1090     return NULL ;
1091   }
1092   if (GNUNET_YES == fba_ctx.best->active)
1093   {
1094     /* This address was selected previously, so no need to update quotas */
1095     return fba_ctx.best;
1096   }
1097
1098   /* This address was not active, so we have to:
1099    *
1100    * - mark previous active address as not active
1101    * - update quota for previous address network
1102    * - update quota for this address network
1103    */
1104   prev = get_active_address (s, s->addresses, peer);
1105   if (NULL != prev)
1106   {
1107     net_prev = (struct Network *) prev->solver_information;
1108     prev->active = GNUNET_NO; /* No active any longer */
1109     prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1110     prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1111     if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1112       GNUNET_break(0);
1113     distribute_bandwidth_in_network (s, net_prev, NULL);
1114   }
1115
1116   if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
1117   {
1118     GNUNET_break(0); /* This should never happen*/
1119     return NULL ;
1120   }
1121
1122   fba_ctx.best->active = GNUNET_YES;
1123   addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES);
1124   distribute_bandwidth_in_network (s, net_cur, fba_ctx.best);
1125   return fba_ctx.best;
1126 }
1127
1128 /**
1129  * Stop notifying about address and bandwidth changes for this peer
1130  *
1131  * @param solver the solver handle
1132  * @param peer the peer
1133  */
1134 void
1135 GAS_proportional_stop_get_preferred_address (void *solver,
1136     const struct GNUNET_PeerIdentity *peer)
1137 {
1138   struct GAS_PROPORTIONAL_Handle *s = solver;
1139   struct ATS_Address *cur;
1140   struct Network *cur_net;
1141
1142   if (GNUNET_YES
1143       == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1144                                                  peer))
1145     GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (s->requests, peer,
1146                                           NULL));
1147
1148   cur = get_active_address (s,
1149                             s->addresses, peer);
1150   if (NULL != cur)
1151   {
1152     /* Disabling current address */
1153     cur_net = (struct Network *) cur->solver_information;
1154     cur->active = GNUNET_NO; /* No active any longer */
1155     cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1156     cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1157     if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1158       GNUNET_break(0);
1159     distribute_bandwidth_in_network (s, cur_net, NULL );
1160   }
1161   return;
1162 }
1163
1164 /**
1165  * Remove an address from the solver
1166  *
1167  * @param solver the solver handle
1168  * @param address the address to remove
1169  * @param session_only delete only session not whole address
1170  */
1171 void
1172 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1173     int session_only)
1174 {
1175   struct GAS_PROPORTIONAL_Handle *s = solver;
1176   struct Network *net;
1177   struct AddressWrapper *aw;
1178   const struct ATS_Address *new_address;
1179
1180
1181   /* Remove an adress completely, we have to:
1182    * - Remove from specific network
1183    * - Decrease number of total addresses
1184    * - If active:
1185    *   - decrease number of active addreses
1186    *   - update quotas
1187    */
1188
1189   net = (struct Network *) address->solver_information;
1190
1191   if (GNUNET_NO == session_only)
1192   {
1193     LOG(GNUNET_ERROR_TYPE_INFO,
1194         "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1195         (GNUNET_NO == address->active) ? "inactive" : "active", address,
1196         GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1197         net->active_addresses);
1198
1199     /* Remove address */
1200     addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1201     for (aw = net->head; NULL != aw; aw = aw->next)
1202     {
1203       if (aw->addr == address)
1204         break;
1205     }
1206     if (NULL == aw)
1207     {
1208       GNUNET_break(0);
1209       return;
1210     }
1211     GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1212     GNUNET_free(aw);
1213   }
1214   else
1215   {
1216     /* Remove session only: remove if active and update */
1217     LOG(GNUNET_ERROR_TYPE_INFO,
1218         "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1219         (GNUNET_NO == address->active) ? "inactive" : "active", address,
1220         GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1221         net->active_addresses);
1222   }
1223
1224   if (GNUNET_YES == address->active)
1225   {
1226     /* Address was active, remove from network and update quotas*/
1227     address->active = GNUNET_NO;
1228     address->assigned_bw_in = BANDWIDTH_ZERO;
1229     address->assigned_bw_out = BANDWIDTH_ZERO;
1230
1231     if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1232       GNUNET_break(0);
1233     distribute_bandwidth_in_network (s, net, NULL);
1234     if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1235     {
1236       /* No alternative address found, disconnect peer */
1237       s->bw_changed (s->bw_changed_cls, address);
1238     }
1239     else
1240     {
1241       s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address);
1242     }
1243   }
1244   LOG(GNUNET_ERROR_TYPE_INFO,
1245       "After deleting address now total %u and active %u addresses in network `%s'\n",
1246       net->total_addresses, net->active_addresses, net->desc);
1247
1248 }
1249
1250 /**
1251  * Start a bulk operation
1252  *
1253  * @param solver the solver
1254  */
1255 void
1256 GAS_proportional_bulk_start (void *solver)
1257 {
1258   LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1259   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1260
1261   GNUNET_assert(NULL != solver);
1262   s->bulk_lock++;
1263 }
1264
1265
1266 /**
1267  * Bulk operation done
1268  */
1269 void
1270 GAS_proportional_bulk_stop (void *solver)
1271 {
1272   LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1273
1274   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1275   GNUNET_assert(NULL != solver);
1276
1277   if (s->bulk_lock < 1)
1278   {
1279     GNUNET_break(0);
1280     return;
1281   }
1282   s->bulk_lock--;
1283   if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1284   {
1285     LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1286     distribute_bandwidth_in_network (s, NULL, NULL);
1287     s->bulk_requests = 0;
1288   }
1289 }
1290
1291
1292 /**
1293  * Add a new single address to a network
1294  *
1295  * @param solver the solver Handle
1296  * @param address the address to add
1297  * @param network network type of this address
1298  */
1299 void
1300 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1301     uint32_t network);
1302
1303 /**
1304  * Transport properties for this address have changed
1305  *
1306  * @param solver solver handle
1307  * @param address the address
1308  * @param type the ATSI type in HBO
1309  * @param abs_value the absolute value of the property
1310  * @param rel_value the normalized value
1311  */
1312 void
1313 GAS_proportional_address_property_changed (void *solver,
1314     struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1315     double rel_value)
1316 {
1317   struct GAS_PROPORTIONAL_Handle *s;
1318   struct Network *n;
1319
1320   GNUNET_assert(NULL != solver);
1321   GNUNET_assert(NULL != address);
1322
1323   s = (struct GAS_PROPORTIONAL_Handle *) solver;
1324   n = (struct Network *) address->solver_information;
1325
1326   if (NULL == n)
1327   {
1328     GNUNET_break(0);
1329     return;
1330   }
1331
1332   LOG(GNUNET_ERROR_TYPE_DEBUG,
1333       "Property `%s' for peer `%s' address %p changed to %.2f \n",
1334       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1335       address, rel_value);
1336   switch (type)
1337   {
1338   case GNUNET_ATS_UTILIZATION_OUT:
1339   case GNUNET_ATS_UTILIZATION_IN:
1340   case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
1341   case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT:
1342   case GNUNET_ATS_QUALITY_NET_DELAY:
1343   case GNUNET_ATS_QUALITY_NET_DISTANCE:
1344   case GNUNET_ATS_COST_WAN:
1345   case GNUNET_ATS_COST_LAN:
1346   case GNUNET_ATS_COST_WLAN:
1347     distribute_bandwidth_in_network (s, n, NULL);
1348     break;
1349   }
1350 }
1351
1352 /**
1353  * Transport session for this address has changed
1354  *
1355  * NOTE: values in addresses are already updated
1356  *
1357  * @param solver solver handle
1358  * @param address the address
1359  * @param cur_session the current session
1360  * @param new_session the new session
1361  */
1362 void
1363 GAS_proportional_address_session_changed (void *solver,
1364     struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1365 {
1366   if (cur_session != new_session)
1367   {
1368     LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1369         new_session);
1370   }
1371 }
1372
1373 /**
1374  * Usage for this address has changed
1375  *
1376  * NOTE: values in addresses are already updated
1377  *
1378  * @param solver solver handle
1379  * @param address the address
1380  * @param in_use usage state
1381  */
1382 void
1383 GAS_proportional_address_inuse_changed (void *solver,
1384     struct ATS_Address *address, int in_use)
1385 {
1386   LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n",
1387       (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1388 }
1389
1390 /**
1391  * Network scope for this address has changed
1392  *
1393  * NOTE: values in addresses are already updated
1394  *
1395  * @param solver solver handle
1396  * @param address the address
1397  * @param current_network the current network
1398  * @param new_network the new network
1399  */
1400 void
1401 GAS_proportional_address_change_network (void *solver,
1402     struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1403 {
1404   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1405   int save_active = GNUNET_NO;
1406   struct Network *new_net = NULL;
1407
1408   if (current_network == new_network)
1409   {
1410     GNUNET_break(0);
1411     return;
1412   }
1413
1414   /* Network changed */
1415   LOG(GNUNET_ERROR_TYPE_DEBUG,
1416       "Network type changed, moving %s address from `%s' to `%s'\n",
1417       (GNUNET_YES == address->active) ? "active" : "inactive",
1418       GNUNET_ATS_print_network_type (current_network),
1419       GNUNET_ATS_print_network_type (new_network));
1420
1421   save_active = address->active;
1422
1423   /* Disable and assign no bandwidth */
1424   address->active = GNUNET_NO;
1425   address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1426   address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1427
1428   /* Remove from old network */
1429   GAS_proportional_address_delete (solver, address, GNUNET_NO);
1430
1431   /* Set new network type */
1432   if (NULL == (new_net = get_network (solver, new_network)))
1433   {
1434     /* Address changed to invalid network... */
1435     LOG(GNUNET_ERROR_TYPE_ERROR,
1436         _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1437         GNUNET_ATS_print_network_type (new_network));
1438
1439     /* Find new address to suggest since no bandwidth in network*/
1440     if (NULL == GAS_proportional_get_preferred_address (s, &address->peer))
1441     {
1442       /* No alternative address found, disconnect peer */
1443       s->bw_changed (s->bw_changed_cls, address);
1444     }
1445     return;
1446   }
1447
1448   /* Add to new network and update*/
1449   address->solver_information = new_net;
1450   GAS_proportional_address_add (solver, address, new_network);
1451   if (GNUNET_YES == save_active)
1452   {
1453     /* check if bandwidth available in new network */
1454     if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1455     {
1456       /* Assign bandwidth to updated address */
1457       address->active = GNUNET_YES;
1458       addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1459       distribute_bandwidth_in_network (solver, new_net, NULL);
1460     }
1461     else
1462     {
1463       LOG(GNUNET_ERROR_TYPE_DEBUG,
1464           "Not enough bandwidth in new network, suggesting alternative address ..\n");
1465       /* Find new address to suggest since no bandwidth in network*/
1466       if (NULL == GAS_proportional_get_preferred_address (s, &address->peer))
1467       {
1468         /* No alternative address found, disconnect peer */
1469         s->bw_changed (s->bw_changed_cls, address);
1470       }
1471     }
1472   }
1473 }
1474
1475 /**
1476  * Add a new single address to a network
1477  *
1478  * @param solver the solver Handle
1479  * @param address the address to add
1480  * @param network network type of this address
1481  */
1482 void
1483 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1484     uint32_t network)
1485 {
1486   struct GAS_PROPORTIONAL_Handle *s = solver;
1487   struct Network *net = NULL;
1488   struct AddressWrapper *aw = NULL;
1489
1490   GNUNET_assert(NULL != s);
1491
1492   net = get_network (s, network);
1493   if (NULL == net)
1494   {
1495     GNUNET_break(0);
1496     return;
1497   }
1498
1499   aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1500   aw->addr = address;
1501   GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1502   addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1503   aw->addr->solver_information = net;
1504
1505   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1506   {
1507     if (NULL == get_active_address (s, s->addresses, &address->peer))
1508     {
1509       if (NULL != GAS_proportional_get_preferred_address (s, &address->peer))
1510           s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) address);
1511     }
1512   }
1513
1514   LOG(GNUNET_ERROR_TYPE_INFO,
1515       "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1516       address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
1517 }
1518
1519
1520 /* end of gnunet-service-ats-solver_proportional.c */