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