changing solver api: remove address hashmap from functions and pass instead with...
[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 : simplistic solver
41  *
42  *    The simplistic solver ("simplistic") 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 simplistic
80  *    solver uses the addresses' solver_information field to store the
81  *    simplistic network it belongs to for each address.
82  *
83  *     3.2 Initializing
84  *
85  *    When the simplistic 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 simplistic
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_simplistic_address_delete and the network in
108  *    the address struct is updated. A lookup for the respective new
109  *    simplistic 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_simplistic_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 /**
217  * A handle for the proportional solver
218  */
219 struct GAS_PROPORTIONAL_Handle
220 {
221   /**
222    * Statistics handle
223    */
224   struct GNUNET_STATISTICS_Handle *stats;
225
226   /**
227    * Hashmap containing all valid addresses
228    */
229   const struct GNUNET_CONTAINER_MultiHashMap *addresses;
230
231   /**
232    * Bandwidth changed callback
233    */
234   GAS_bandwidth_changed_cb bw_changed;
235
236   /**
237    * Bandwidth changed callback cls
238    */
239   void *bw_changed_cls;
240
241   /**
242    * ATS function to get preferences
243    */
244   GAS_get_preferences get_preferences;
245
246   /**
247    * Closure for ATS function to get preferences
248    */
249   void *get_preferences_cls;
250
251   /**
252    * ATS function to get properties
253    */
254   GAS_get_properties get_properties;
255
256   /**
257    * Closure for ATS function to get properties
258    */
259   void *get_properties_cls;
260
261   /**
262    * Bulk lock
263    */
264   int bulk_lock;
265
266   /**
267    * Number of changes while solver was locked
268    */
269   int bulk_requests;
270
271
272   /**
273    * Total number of addresses for solver
274    */
275   unsigned int total_addresses;
276
277   /**
278    * Number of active addresses for solver
279    */
280   unsigned int active_addresses;
281
282   /**
283    * Networks array
284    */
285   struct Network *network_entries;
286
287   /**
288    * Number of networks
289    */
290   unsigned int networks;
291
292 };
293
294
295 /**
296  * Representation of a network
297  */
298 struct Network
299 {
300   /**
301    * ATS network type
302    */
303   unsigned int type;
304
305   /**
306    * Network description
307    */
308   char *desc;
309
310   /**
311    * Total inbound quota
312    *
313    */
314   unsigned long long total_quota_in;
315
316   /**
317    * Total outbound quota
318    *
319    */
320   unsigned long long total_quota_out;
321
322   /**
323    * Number of active addresses for this network
324    */
325   unsigned int active_addresses;
326
327   /**
328    * Number of total addresses for this network
329    */
330   unsigned int total_addresses;
331
332   /**
333    * String for statistics total addresses
334    */
335   char *stat_total;
336
337   /**
338    * String for statistics active addresses
339    */
340   char *stat_active;
341
342   struct AddressWrapper *head;
343   struct AddressWrapper *tail;
344 };
345
346
347 /**
348  * Wrapper for addresses to store them in network's linked list
349  */
350 struct AddressWrapper
351 {
352   /**
353    * Next in DLL
354    */
355   struct AddressWrapper *next;
356
357   /**
358    * Previous in DLL
359    */
360   struct AddressWrapper *prev;
361
362   /**
363    * The address
364    */
365   struct ATS_Address *addr;
366 };
367
368
369
370 /**
371  *  Important solver functions
372  *  ---------------------------
373  */
374
375 /**
376  * Test if bandwidth is available in this network to add an additional address
377  *
378  * @param net the network type to update
379  * @return GNUNET_YES or GNUNET_NO
380  */
381 static int
382 is_bandwidth_available_in_network (struct Network *net)
383 {
384         GNUNET_assert (NULL != net);
385   unsigned int na = net->active_addresses + 1;
386   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
387   if (((net->total_quota_in / na) > min_bw) &&
388       ((net->total_quota_out / na) > min_bw))
389   {
390     LOG (GNUNET_ERROR_TYPE_DEBUG,
391          "Enough bandwidth available for %u active addresses in network `%s'\n",
392          na,
393          net->desc);
394
395     return GNUNET_YES;
396   }
397     LOG (GNUNET_ERROR_TYPE_DEBUG,
398          "Not enough bandwidth available for %u active addresses in network `%s'\n",
399          na,
400          net->desc);
401   return GNUNET_NO;
402 }
403
404
405 /**
406  * Update bandwidth assigned to peers in this network
407  *
408  * @param s the solver handle
409  * @param net the network type to update
410  * @param address_except address excluded from notification, since we suggest
411  * this address
412  */
413 static void
414 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
415                           struct Network *net,
416                           struct ATS_Address *address_except)
417 {
418   unsigned long long remaining_quota_in = 0;
419   unsigned long long quota_out_used = 0;
420
421   unsigned long long remaining_quota_out = 0;
422   unsigned long long quota_in_used = 0;
423   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
424   double peer_prefs;
425   double total_prefs; /* Important: has to be double not float due to precision */
426   double cur_pref; /* Important: has to be double not float due to precision */
427   const double *t = NULL; /* Important: has to be double not float due to precision */
428   int c;
429
430   unsigned long long assigned_quota_in = 0;
431   unsigned long long assigned_quota_out = 0;
432   struct AddressWrapper *cur;
433
434
435         if (GNUNET_YES == s->bulk_lock)
436         {
437                 s->bulk_requests++;
438                 return;
439         }
440
441   LOG (GNUNET_ERROR_TYPE_DEBUG,
442               "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
443               net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
444
445   if (net->active_addresses == 0)
446     return; /* no addresses to update */
447
448   /* Idea
449    * Assign every peer in network minimum Bandwidth
450    * Distribute bandwidth left according to preference
451    */
452
453   if ((net->active_addresses * min_bw) > net->total_quota_in)
454   {
455     GNUNET_break (0);
456     return;
457   }
458   if ((net->active_addresses * min_bw) > net->total_quota_out)
459   {
460     GNUNET_break (0);
461     return;
462   }
463
464   remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
465   remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
466   LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
467               remaining_quota_in, remaining_quota_out);
468   total_prefs = 0.0;
469   for (cur = net->head; NULL != cur; cur = cur->next)
470   {
471       if (GNUNET_YES == cur->addr->active)
472       {
473         GNUNET_assert (NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
474
475                                 peer_prefs = 0.0;
476                                 for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
477                                 {
478                                         if (c != GNUNET_ATS_PREFERENCE_END)
479                                         {
480                                                 //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
481                                                 peer_prefs += t[c];
482                                         }
483                                 }
484                                 total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount -1));
485       }
486   }
487   for (cur = net->head; NULL != cur; cur = cur->next)
488   {
489      if (GNUNET_YES == cur->addr->active)
490      {
491        cur_pref = 0.0;
492        GNUNET_assert (NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
493
494                          for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
495                          {
496                                  if (c != GNUNET_ATS_PREFERENCE_END)
497                                          cur_pref += t[c];
498                          }
499                          cur_pref /= 2;
500
501        assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
502        assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
503
504        LOG (GNUNET_ERROR_TYPE_DEBUG,
505                    "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
506                    GNUNET_i2s (&cur->addr->peer),
507                    cur_pref, total_prefs,
508                    assigned_quota_in, assigned_quota_out);
509      }
510      else
511      {
512        assigned_quota_in = 0;
513        assigned_quota_out = 0;
514      }
515
516      quota_in_used += assigned_quota_in;
517      quota_out_used += assigned_quota_out;
518      /* Prevent overflow due to rounding errors */
519      if (assigned_quota_in > UINT32_MAX)
520        assigned_quota_in = UINT32_MAX;
521      if (assigned_quota_out > UINT32_MAX)
522        assigned_quota_out = UINT32_MAX;
523
524      /* Compare to current bandwidth assigned */
525      if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
526          (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
527      {
528        cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
529        cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
530        /* Notify on change */
531        if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
532          s->bw_changed (s->bw_changed_cls, cur->addr);
533      }
534
535   }
536   LOG (GNUNET_ERROR_TYPE_DEBUG,
537                           "Total bandwidth assigned is (in/out): %llu /%llu\n",
538                           quota_in_used,
539                           quota_out_used);
540   if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
541   {
542       LOG (GNUNET_ERROR_TYPE_ERROR,
543                             "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
544                             net->active_addresses,
545                             quota_out_used,
546                             net->total_quota_out);
547   }
548   if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
549   {
550       LOG (GNUNET_ERROR_TYPE_ERROR,
551                             "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
552                             net->active_addresses,
553                             quota_in_used,
554                             net->total_quota_in);
555   }
556 }
557
558
559 /**
560  * Extract an ATS performance info from an address
561  *
562  * @param address the address
563  * @param type the type to extract in HBO
564  * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
565  */
566 static int
567 get_performance_info (struct ATS_Address *address, uint32_t type);
568
569
570
571 struct FindBestAddressCtx
572 {
573         struct GAS_PROPORTIONAL_Handle *s;
574         struct ATS_Address *best;
575 };
576
577
578 /**
579  * Find a "good" address to use for a peer by iterating over the addresses for this peer.
580  * If we already have an existing address, we stick to it.
581  * Otherwise, we pick by lowest distance and then by lowest latency.
582  *
583  * @param cls the 'struct ATS_Address**' where we store the result
584  * @param key unused
585  * @param value another 'struct ATS_Address*' to consider using
586  * @return GNUNET_OK (continue to iterate)
587  */
588 static int
589 find_best_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
590 {
591         struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
592   struct ATS_Address *current = (struct ATS_Address *) value;
593   struct GNUNET_TIME_Absolute now;
594   struct Network *net = (struct Network *) current->solver_information;
595   const double *norm_prop_cur;
596   const double *norm_prop_prev;
597
598   now = GNUNET_TIME_absolute_get();
599
600   if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
601   {
602     /* This address is blocked for suggestion */
603     LOG (GNUNET_ERROR_TYPE_DEBUG,
604                 "Address %p blocked for suggestion for %llu ms \n",
605                 current,
606                 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
607     return GNUNET_OK;
608   }
609   if (GNUNET_NO == is_bandwidth_available_in_network (net))
610     return GNUNET_OK; /* There's no bandwidth available in this network */
611   if (NULL != fba_ctx->best)
612   {
613         GNUNET_assert (NULL != fba_ctx->best->plugin);
614         GNUNET_assert (NULL != current->plugin);
615     if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
616     {
617       if ((0 != fba_ctx->best->addr_len) &&
618           (0 == current->addr_len))
619       {
620         /* saved address was an outbound address, but we have an inbound address */
621         fba_ctx->best = current;
622         return GNUNET_OK;
623       }
624       if (0 == fba_ctx->best->addr_len)
625       {
626         /* saved address was an inbound address, so do not overwrite */
627         return GNUNET_OK;
628       }
629     }
630   }
631   if (NULL == fba_ctx->best)
632   {
633         fba_ctx->best = current;
634     return GNUNET_OK;
635   }
636   if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0) &&
637       (ntohl (current->assigned_bw_in.value__) > 0))
638   {
639     /* stick to existing connection */
640         fba_ctx->best = current;
641     return GNUNET_OK;
642   }
643
644   norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
645                 (const struct ATS_Address *) current);
646   GNUNET_break (0);
647   norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
648                 (const struct ATS_Address *) fba_ctx->best);
649
650   if (norm_prop_cur[1] < norm_prop_prev[1])
651   {
652     /* user shorter distance */
653     fba_ctx->best = current;
654     return GNUNET_OK;
655   }
656   if (norm_prop_cur[0] < norm_prop_prev[0])
657   {
658     /* user shorter distance */
659         fba_ctx->best = current;
660     return GNUNET_OK;
661   }
662
663   /* don't care */
664   return GNUNET_OK;
665 }
666
667 /**
668  *  Helper functions
669  *  ---------------------------
670  */
671
672 /**
673  * Update bandwidth assignment for all networks
674  *
675  * @param s the solver handle
676  */
677 static void
678 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
679 {
680         int i;
681
682         for (i = 0; i < s->networks; i++)
683                 distribute_bandwidth_in_network (s, &s->network_entries[i], NULL);
684
685 }
686
687
688 /**
689  * Lookup network struct by type
690  *
691  * @param s the solver handle
692  * @param type the network type
693  * @return the network struct
694  */
695 static struct Network *
696 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
697 {
698   int c;
699   for (c = 0 ; c < s->networks; c++)
700   {
701       if (s->network_entries[c].type == type)
702         return &s->network_entries[c];
703
704   }
705   return NULL;
706 }
707
708
709 /**
710  * Hashmap Iterator to find current active address for peer
711  *
712  * @param cls last active address
713  * @param key peer's key
714  * @param value address to check
715  * @return GNUNET_NO on double active address else GNUNET_YES
716  */
717 static int
718 get_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
719 {
720   struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
721   struct ATS_Address * aa = (struct ATS_Address *) value;
722
723   if (GNUNET_YES == aa->active)
724   {
725       if (dest != NULL)
726       {
727           /* should never happen */
728           LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
729           GNUNET_break (0);
730           return GNUNET_NO;
731       }
732       dest = aa;
733   }
734   return GNUNET_OK;
735 }
736
737
738 /**
739  * Find current active address for peer
740  *
741  * @param solver the solver handle
742  * @param addresses the address set
743  * @param peer the peer
744  * @return active address or NULL
745  */
746 static struct ATS_Address *
747 get_active_address (void *solver,
748                      struct GNUNET_CONTAINER_MultiHashMap * addresses,
749                      const struct GNUNET_PeerIdentity *peer)
750 {
751   struct ATS_Address * dest = NULL;
752
753   GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
754        &peer->hashPubKey,
755        &get_active_address_it, &dest);
756   return dest;
757 }
758
759
760
761 static void
762 addresse_increment (struct GAS_PROPORTIONAL_Handle *s,
763                                 struct Network *net,
764                                 int total,
765                                 int active)
766 {
767   if (GNUNET_YES == total)
768   {
769       s->total_addresses ++;
770       net->total_addresses ++;
771       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
772       GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
773   }
774   if (GNUNET_YES == active)
775   {
776     net->active_addresses ++;
777     s->active_addresses ++;
778     GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
779     GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
780   }
781
782 }
783
784
785 static int
786 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
787                     struct Network *net,
788                     int total,
789                     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, GNUNET_NO);
803     }
804     if (net->total_addresses < 1)
805     {
806       GNUNET_break (0);
807       res = GNUNET_SYSERR;
808     }
809     else
810     {
811       net->total_addresses --;
812       GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
813     }
814   }
815
816   if (GNUNET_YES == active)
817   {
818     if (net->active_addresses < 1)
819     {
820       GNUNET_break (0);
821       res = GNUNET_SYSERR;
822     }
823     else
824     {
825       net->active_addresses --;
826       GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
827     }
828     if (s->active_addresses < 1)
829     {
830       GNUNET_break (0);
831       res = GNUNET_SYSERR;
832     }
833     else
834     {
835       s->active_addresses --;
836       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
837     }
838   }
839   return res;
840 }
841
842
843 /**
844  * Extract an ATS performance info from an address
845  *
846  * @param address the address
847  * @param type the type to extract in HBO
848  * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
849  */
850 static int
851 get_performance_info (struct ATS_Address *address, uint32_t type)
852 {
853         int c1;
854         GNUNET_assert (NULL != address);
855
856         if ((NULL == address->atsi) || (0 == address->atsi_count))
857                         return GNUNET_ATS_VALUE_UNDEFINED;
858
859         for (c1 = 0; c1 < address->atsi_count; c1++)
860         {
861                         if (ntohl(address->atsi[c1].type) == type)
862                                 return ntohl(address->atsi[c1].value);
863         }
864         return GNUNET_ATS_VALUE_UNDEFINED;
865 }
866
867
868 /**
869  *  Solver API functions
870  *  ---------------------------
871  */
872
873 /**
874  * Changes the preferences for a peer in the problem
875  *
876  * @param solver the solver handle
877  * @param peer the peer to change the preference for
878  * @param kind the kind to change the preference
879  * @param pref_rel the normalized preference value for this kind over all clients
880  */
881 void
882 GAS_proportional_address_change_preference (void *solver,
883                                                                                         const struct GNUNET_PeerIdentity *peer,
884                                                                                         enum GNUNET_ATS_PreferenceKind kind,
885                                                                                         double pref_rel)
886 {
887   struct GAS_PROPORTIONAL_Handle *s = solver;
888   GNUNET_assert (NULL != solver);
889   GNUNET_assert (NULL != peer);
890
891         distribute_bandwidth_in_all_networks (s);
892 }
893
894
895 /**
896  * Get the preferred address for a specific peer
897  *
898  * @param solver the solver handle
899  * @param addresses the address hashmap containing all addresses
900  * @param peer the identity of the peer
901  */
902 const struct ATS_Address *
903 GAS_proportional_get_preferred_address (void *solver,
904                                const struct GNUNET_PeerIdentity *peer)
905 {
906   struct GAS_PROPORTIONAL_Handle *s = solver;
907   struct Network *net_prev;
908   struct Network *net_cur;
909   struct ATS_Address *prev;
910   struct FindBestAddressCtx fba_ctx;
911
912   GNUNET_assert (s != NULL);
913   GNUNET_assert (peer != NULL);
914
915   /* Get address with: stick to current address, lower distance, lower latency */
916   fba_ctx.s = s;
917   fba_ctx.best = NULL;
918   GNUNET_break (0);
919   GNUNET_CONTAINER_multihashmap_get_multiple (s->addresses, &peer->hashPubKey,
920                                               &find_best_address_it, &fba_ctx);
921   if (NULL == fba_ctx.best)
922   {
923     LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
924     return NULL;
925   }
926
927   LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
928       (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active",
929                 fba_ctx.best, GNUNET_i2s (peer));
930   net_cur = (struct Network *) fba_ctx.best->solver_information;
931   if (NULL == fba_ctx.best)
932   {
933     LOG (GNUNET_ERROR_TYPE_ERROR, "Trying to suggesting unknown address peer `%s'\n",
934         GNUNET_i2s (peer));
935     GNUNET_break (0);
936     return NULL;
937   }
938   if (GNUNET_YES == fba_ctx.best->active)
939   {
940       /* This address was selected previously, so no need to update quotas */
941       return fba_ctx.best;
942   }
943
944   /* This address was not active, so we have to:
945    *
946    * - mark previous active address as not active
947    * - update quota for previous address network
948    * - update quota for this address network
949    */
950
951   prev = get_active_address (s, (struct GNUNET_CONTAINER_MultiHashMap *) s->addresses, peer);
952   if (NULL != prev)
953   {
954       net_prev = (struct Network *) prev->solver_information;
955       prev->active = GNUNET_NO; /* No active any longer */
956       prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
957       prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
958       s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
959       if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
960         GNUNET_break (0);
961         distribute_bandwidth_in_network (s, net_prev, NULL);
962   }
963
964   if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
965   {
966     GNUNET_break (0); /* This should never happen*/
967     return NULL;
968   }
969
970   fba_ctx.best->active = GNUNET_YES;
971   addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
972   distribute_bandwidth_in_network (s, net_cur, fba_ctx.best);
973   return fba_ctx.best;
974 }
975
976
977 /**
978  * Stop notifying about address and bandwidth changes for this peer
979  *
980  * @param solver the solver handle
981  * @param addresses address hashmap
982  * @param peer the peer
983  */
984 void
985 GAS_proportional_stop_get_preferred_address (void *solver,
986                                      const struct GNUNET_PeerIdentity *peer)
987 {
988         return;
989 }
990
991
992 /**
993  * Remove an address from the solver
994  *
995  * @param solver the solver handle
996  * @param addresses the address hashmap containing all addresses
997  * @param address the address to remove
998  * @param session_only delete only session not whole address
999  */
1000 void
1001 GAS_proportional_address_delete (void *solver,
1002                                                                                                                  struct ATS_Address *address,
1003                                                                                                                  int session_only)
1004 {
1005   struct GAS_PROPORTIONAL_Handle *s = solver;
1006   struct Network *net;
1007   struct AddressWrapper *aw;
1008
1009   /* Remove an adress completely, we have to:
1010    * - Remove from specific network
1011    * - Decrease number of total addresses
1012    * - If active:
1013    *   - decrease number of active addreses
1014    *   - update quotas
1015    */
1016
1017   net = (struct Network *) address->solver_information;
1018
1019   if (GNUNET_NO == session_only)
1020   {
1021     LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1022         (GNUNET_NO == address->active) ? "inactive" : "active",
1023         address, GNUNET_i2s (&address->peer),
1024         net->desc, net->total_addresses, net->active_addresses);
1025
1026     /* Remove address */
1027     addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1028     for (aw = net->head; NULL != aw; aw = aw->next)
1029     {
1030         if (aw->addr == address)
1031           break;
1032     }
1033     if (NULL == aw )
1034     {
1035         GNUNET_break (0);
1036         return;
1037     }
1038     GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
1039     GNUNET_free (aw);
1040   }
1041   else
1042   {
1043       /* Remove session only: remove if active and update */
1044       LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1045           (GNUNET_NO == address->active) ? "inactive" : "active",
1046           address, GNUNET_i2s (&address->peer),
1047           net->desc, net->total_addresses, net->active_addresses);
1048   }
1049
1050   if (GNUNET_YES == address->active)
1051   {
1052       /* Address was active, remove from network and update quotas*/
1053       address->active = GNUNET_NO;
1054       if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1055         GNUNET_break (0);
1056       distribute_bandwidth_in_network (s, net, NULL);
1057   }
1058   LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
1059       net->total_addresses,
1060       net->active_addresses,
1061       net->desc);
1062
1063 }
1064
1065
1066 /**
1067  * Start a bulk operation
1068  *
1069  * @param solver the solver
1070  */
1071 void
1072 GAS_proportional_bulk_start (void *solver)
1073 {
1074   LOG (GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1075   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1076
1077   GNUNET_assert (NULL != solver);
1078   s->bulk_lock ++;
1079 }
1080
1081 /**
1082  * Bulk operation done
1083  */
1084 void
1085 GAS_proportional_bulk_stop (void *solver)
1086 {
1087         LOG (GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1088
1089   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1090   GNUNET_assert (NULL != solver);
1091
1092   if (s->bulk_lock < 1)
1093   {
1094         GNUNET_break (0);
1095         return;
1096   }
1097   s->bulk_lock --;
1098   if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1099   {
1100         LOG (GNUNET_ERROR_TYPE_ERROR, "No lock pending, recalculating\n");
1101         distribute_bandwidth_in_all_networks (s);
1102         s->bulk_requests = 0;
1103   }
1104 }
1105
1106
1107 /**
1108  * Add a new single address to a network
1109  *
1110  * @param solver the solver Handle
1111  * @param address the address to add
1112  * @param network network type of this address
1113  */
1114 void
1115 GAS_proportional_address_add (void *solver,
1116                                                                                                                         struct ATS_Address *address,
1117                                                                                                                         uint32_t network);
1118
1119 /**
1120  * Updates a single address in the solver and checks previous values
1121  *
1122  * @param solver the solver Handle
1123  * @param addresses the address hashmap containing all addresses
1124  * @param address the update address
1125  * @param session the previous session
1126  * @param in_use the previous address in use state
1127  * @param prev_ats previous ATS information
1128  * @param prev_atsi_count the previous atsi count
1129  */
1130 void
1131 GAS_proportional_address_update (void *solver,
1132                               struct ATS_Address *address,
1133                               uint32_t session,
1134                               int in_use,
1135                               const struct GNUNET_ATS_Information *prev_ats,
1136                               uint32_t prev_atsi_count)
1137 {
1138   struct ATS_Address *new;
1139   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1140   int i;
1141   uint32_t prev_value;
1142   uint32_t prev_type;
1143   uint32_t addr_net;
1144   int save_active = GNUNET_NO;
1145   struct Network *new_net = NULL;
1146
1147   /* Check updates to performance information */
1148   for (i = 0; i < prev_atsi_count; i++)
1149   {
1150     prev_type = ntohl (prev_ats[i].type);
1151     prev_value = ntohl (prev_ats[i].value);
1152     switch (prev_type)
1153     {
1154     case GNUNET_ATS_UTILIZATION_UP:
1155     case GNUNET_ATS_UTILIZATION_DOWN:
1156     case GNUNET_ATS_QUALITY_NET_DELAY:
1157     case GNUNET_ATS_QUALITY_NET_DISTANCE:
1158     case GNUNET_ATS_COST_WAN:
1159     case GNUNET_ATS_COST_LAN:
1160     case GNUNET_ATS_COST_WLAN:
1161         /* No actions required here*/
1162         break;
1163     case GNUNET_ATS_NETWORK_TYPE:
1164
1165       addr_net = get_performance_info (address, GNUNET_ATS_NETWORK_TYPE);
1166       if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
1167       {
1168         GNUNET_break (0);
1169         addr_net = GNUNET_ATS_NET_UNSPECIFIED;
1170       }
1171       if (addr_net != prev_value)
1172       {
1173         /* Network changed */
1174         LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
1175             (GNUNET_YES == address->active) ? "active" : "inactive",
1176              GNUNET_ATS_print_network_type(prev_value),
1177              GNUNET_ATS_print_network_type(addr_net));
1178
1179         save_active = address->active;
1180         /* remove from old network */
1181         GAS_proportional_address_delete (solver, address, GNUNET_NO);
1182
1183         /* set new network type */
1184         new_net = get_network (solver, addr_net);
1185         if (NULL == new_net)
1186         {
1187           /* Address changed to invalid network... */
1188           LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot find network of type `%u' %s\n"),
1189                         addr_net, GNUNET_ATS_print_network_type (addr_net));
1190           address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1191           address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1192           s->bw_changed  (s->bw_changed_cls, address);
1193           return;
1194         }
1195         address->solver_information = new_net;
1196
1197         /* Add to new network and update*/
1198         GAS_proportional_address_add (solver, address, addr_net);
1199         if (GNUNET_YES == save_active)
1200         {
1201           /* check if bandwidth available in new network */
1202           if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1203           {
1204               /* Suggest updated address */
1205               address->active = GNUNET_YES;
1206               addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1207               distribute_bandwidth_in_network (solver, new_net, NULL);
1208           }
1209           else
1210           {
1211             LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
1212
1213             /* Set old address to zero bw */
1214             address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
1215             address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
1216             s->bw_changed  (s->bw_changed_cls, address);
1217
1218             /* Find new address to suggest since no bandwidth in network*/
1219             new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer);
1220             if (NULL != new)
1221             {
1222                 /* Have an alternative address to suggest */
1223                 s->bw_changed  (s->bw_changed_cls, new);
1224             }
1225
1226           }
1227         }
1228       }
1229
1230       break;
1231     case GNUNET_ATS_ARRAY_TERMINATOR:
1232       break;
1233     default:
1234       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1235                   "Received unsupported ATS type %u\n", prev_type);
1236       GNUNET_break (0);
1237       break;
1238
1239     }
1240
1241   }
1242   if (address->session_id != session)
1243   {
1244       LOG (GNUNET_ERROR_TYPE_DEBUG,
1245                   "Session changed from %u to %u\n", session, address->session_id);
1246   }
1247   if (address->used != in_use)
1248   {
1249       LOG (GNUNET_ERROR_TYPE_DEBUG,
1250                   "Usage changed from %u to %u\n", in_use, address->used);
1251   }
1252
1253 }
1254
1255
1256 /**
1257  * Add a new single address to a network
1258  *
1259  * @param solver the solver Handle
1260  * @param addresses the address hashmap containing all addresses
1261  * @param address the address to add
1262  * @param network network type of this address
1263  */
1264 void
1265 GAS_proportional_address_add (void *solver,
1266                                                                                                                         struct ATS_Address *address,
1267                                                                                                                         uint32_t network)
1268 {
1269   struct GAS_PROPORTIONAL_Handle *s = solver;
1270   struct Network *net = NULL;
1271   struct AddressWrapper *aw = NULL;
1272   GNUNET_assert (NULL != s);
1273
1274   net = get_network (s, network);
1275   if (NULL == net)
1276   {
1277     GNUNET_break (0);
1278     return;
1279   }
1280
1281   aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1282   aw->addr = address;
1283   GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
1284   addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1285   aw->addr->solver_information = net;
1286
1287   LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
1288       net->total_addresses,
1289       net->active_addresses,
1290       net->desc);
1291 }
1292
1293
1294 /**
1295  * Init the proportional problem solver
1296  *
1297  * Quotas:
1298  * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1299  * out_quota[i] contains outbound quota for network type i
1300  * in_quota[i] contains inbound quota for network type i
1301  *
1302  * Example
1303  * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1304  * network[2]   == GNUNET_ATS_NET_LAN
1305  * out_quota[2] == 65353
1306  * in_quota[2]  == 65353
1307  *
1308  * @param cfg configuration handle
1309  * @param stats the GNUNET_STATISTICS handle
1310  * @param addresses hashmap containing all addresses
1311  * @param network array of GNUNET_ATS_NetworkType with length dest_length
1312  * @param out_quota array of outbound quotas
1313  * @param in_quota array of outbound quota
1314  * @param dest_length array length for quota arrays
1315  * @param bw_changed_cb callback for changed bandwidth amounts
1316  * @param bw_changed_cb_cls cls for callback
1317  * @param get_preference callback to get relative preferences for a peer
1318  * @param get_preference_cls cls for callback to get relative preferences
1319  * @return handle for the solver on success, NULL on fail
1320  */
1321 void *
1322 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1323                        const struct GNUNET_STATISTICS_Handle *stats,
1324                        const struct GNUNET_CONTAINER_MultiHashMap *addresses,
1325                        int *network,
1326                        unsigned long long *out_quota,
1327                        unsigned long long *in_quota,
1328                        int dest_length,
1329                        GAS_bandwidth_changed_cb bw_changed_cb,
1330                        void *bw_changed_cb_cls,
1331                        GAS_get_preferences get_preference,
1332                        void *get_preference_cls,
1333                        GAS_get_properties get_properties,
1334                        void *get_properties_cls)
1335 {
1336   int c;
1337   struct GAS_PROPORTIONAL_Handle *s = GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1338   struct Network * cur;
1339   char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1340
1341   GNUNET_assert (NULL != cfg);
1342   GNUNET_assert (NULL != stats);
1343   GNUNET_assert (NULL != network);
1344   GNUNET_assert (NULL != bw_changed_cb);
1345   GNUNET_assert (NULL != get_preference);
1346   GNUNET_assert (NULL != get_properties);
1347
1348   s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1349   s->bw_changed = bw_changed_cb;
1350   s->bw_changed_cls = bw_changed_cb_cls;
1351   s->get_preferences = get_preference;
1352   s->get_preferences_cls = get_preference_cls;
1353   s->get_properties = get_properties;
1354   s->get_properties_cls = get_properties_cls;
1355   s->networks = dest_length;
1356   s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1357   s->active_addresses = 0;
1358   s->total_addresses = 0;
1359   s->bulk_lock = GNUNET_NO;
1360   s->addresses = addresses;
1361
1362   for (c = 0; c < dest_length; c++)
1363   {
1364       cur = &s->network_entries[c];
1365       cur->total_addresses = 0;
1366       cur->active_addresses = 0;
1367       cur->type = network[c];
1368       cur->total_quota_in = in_quota[c];
1369       cur->total_quota_out = out_quota[c];
1370       cur->desc = net_str[c];
1371       GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1372       GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
1373   }
1374   return s;
1375 }
1376
1377
1378 /**
1379  * Shutdown the proportional problem solver
1380  *
1381  * @param solver the respective handle to shutdown
1382  */
1383 void
1384 GAS_proportional_done (void *solver)
1385 {
1386   struct GAS_PROPORTIONAL_Handle *s = solver;
1387   struct AddressWrapper *cur;
1388   struct AddressWrapper *next;
1389   int c;
1390   GNUNET_assert (s != NULL);
1391
1392   for (c = 0; c < s->networks; c++)
1393   {
1394       if (s->network_entries[c].total_addresses > 0)
1395       {
1396         LOG (GNUNET_ERROR_TYPE_ERROR,
1397                     "Had %u addresses for network `%s' not deleted during shutdown\n",
1398                     s->network_entries[c].total_addresses,
1399                     s->network_entries[c].desc);
1400         GNUNET_break (0);
1401       }
1402
1403       if (s->network_entries[c].active_addresses > 0)
1404       {
1405         LOG (GNUNET_ERROR_TYPE_ERROR,
1406                     "Had %u active addresses for network `%s' not deleted during shutdown\n",
1407                     s->network_entries[c].active_addresses,
1408                     s->network_entries[c].desc);
1409         GNUNET_break (0);
1410       }
1411
1412       next = s->network_entries[c].head;
1413       while (NULL != (cur = next))
1414       {
1415           next = cur->next;
1416           GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1417                                        s->network_entries[c].tail,
1418                                        cur);
1419           GNUNET_free (cur);
1420       }
1421       GNUNET_free (s->network_entries[c].stat_total);
1422       GNUNET_free (s->network_entries[c].stat_active);
1423   }
1424   if (s->total_addresses > 0)
1425   {
1426     LOG (GNUNET_ERROR_TYPE_ERROR,
1427                 "Had %u addresses not deleted during shutdown\n",
1428                 s->total_addresses);
1429     GNUNET_break (0);
1430   }
1431   if (s->active_addresses > 0)
1432   {
1433     LOG (GNUNET_ERROR_TYPE_ERROR,
1434                 "Had %u active addresses not deleted during shutdown\n",
1435                 s->active_addresses);
1436     GNUNET_break (0);
1437   }
1438   GNUNET_free (s->network_entries);
1439   GNUNET_free (s);
1440 }
1441
1442
1443 /* end of gnunet-service-ats-solver_proportional.c */