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