no valgrind
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses_simplistic.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_addresses_simplistic.h
23  * @brief ats simplistic ressource assignment
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-simplistic",__VA_ARGS__)
33
34 /**
35  * ATS simplistic solver
36  *
37  * Assigns in and outbound bandwidth equally for all addresses in specific
38  * network type (WAN, LAN) based on configured in and outbound quota for this
39  * network.
40  *
41  * For each peer only a single is selected and marked as "active" in the address
42  * struct.
43  *
44  * E.g.:
45  *
46  * You have the networks WAN and LAN and quotas
47  * WAN_TOTAL_IN, WAN_TOTAL_OUT
48  * LAN_TOTAL_IN, LAN_TOTAL_OUT
49  *
50  * If you have x addresses in the network segment LAN, the quotas are
51  * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
52  *
53  * Quotas are automatically recalculated and reported back when addresses are
54  * - requested
55  *
56  */
57
58
59 /**
60  * A handle for the simplistic solver
61  */
62 struct GAS_SIMPLISTIC_Handle
63 {
64   /**
65    * Statistics handle
66    */
67
68   struct GNUNET_STATISTICS_Handle *stats;
69
70   /**
71    * Total number of addresses for solver
72    */
73   unsigned int total_addresses;
74
75   /**
76    * Number of active addresses for solver
77    */
78   unsigned int active_addresses;
79
80   /**
81    * Networks array
82    */
83   struct Network *network_entries;
84
85   /**
86    * Number of networks
87    */
88   unsigned int networks;
89
90   /**
91    * Callback
92    */
93   GAS_bandwidth_changed_cb bw_changed;
94
95   /**
96    * Callback cls
97    */
98   void *bw_changed_cls;
99 };
100
101 struct Network
102 {
103   /**
104    * ATS network type
105    */
106   unsigned int type;
107
108   /**
109    * Network description
110    */
111   char *desc;
112
113   /**
114    * Total inbound quota
115    *
116    */
117   unsigned long long total_quota_in;
118
119   /**
120    * Total outbound quota
121    *
122    */
123   unsigned long long total_quota_out;
124
125   /**
126    * Number of active addresses for this network
127    */
128   unsigned int active_addresses;
129
130   /**
131    * Number of total addresses for this network
132    */
133   unsigned int total_addresses;
134
135   /**
136    * String for statistics total addresses
137    */
138   char *stat_total;
139
140   /**
141    * String for statistics active addresses
142    */
143   char *stat_active;
144
145   struct AddressWrapper *head;
146   struct AddressWrapper *tail;
147 };
148
149 struct AddressWrapper
150 {
151   struct AddressWrapper *next;
152   struct AddressWrapper *prev;
153
154   struct ATS_Address *addr;
155 };
156
157 /**
158  * Init the simplistic problem solving component
159  *
160  * Quotas:
161  * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
162  * out_quota[i] contains outbound quota for network type i
163  * in_quota[i] contains inbound quota for network type i
164  *
165  * Example
166  * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
167  * network[2]   == GNUNET_ATS_NET_LAN
168  * out_quota[2] == 65353
169  * in_quota[2]  == 65353
170  *
171  * @param cfg configuration handle
172  * @param stats the GNUNET_STATISTICS handle
173  * @param network array of GNUNET_ATS_NetworkType with length dest_length
174  * @param out_quota array of outbound quotas
175  * @param in_quota array of outbound quota
176  * @param dest_length array length for quota arrays
177  * @param bw_changed_cb callback for changed bandwidth amounts
178  * @param bw_changed_cb_cls cls for callback
179  * @return handle for the solver on success, NULL on fail
180  */
181 void *
182 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
183                      const struct GNUNET_STATISTICS_Handle *stats,
184                      int *network,
185                      unsigned long long *out_quota,
186                      unsigned long long *in_quota,
187                      int dest_length,
188                      GAS_bandwidth_changed_cb bw_changed_cb,
189                      void *bw_changed_cb_cls)
190 {
191   int c;
192   struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
193   struct Network * cur;
194   char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
195
196
197   s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
198   s->bw_changed = bw_changed_cb;
199   s->bw_changed_cls = bw_changed_cb_cls;
200   s->networks = dest_length;
201   s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
202   s->active_addresses = 0;
203   s->total_addresses = 0;
204
205   for (c = 0; c < dest_length; c++)
206   {
207       cur = &s->network_entries[c];
208       cur->total_addresses = 0;
209       cur->active_addresses = 0;
210       cur->type = network[c];
211       cur->total_quota_in = in_quota[c];
212       cur->total_quota_out = out_quota[c];
213       cur->desc = net_str[c];
214       GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
215       GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
216   }
217   return s;
218 }
219
220
221 /**
222  * Shutdown the simplistic problem solving component
223  *
224  * @param solver the respective handle to shutdown
225  */
226 void
227 GAS_simplistic_done (void *solver)
228 {
229   struct GAS_SIMPLISTIC_Handle *s = solver;
230   struct AddressWrapper *cur;
231   struct AddressWrapper *next;
232   int c;
233   GNUNET_assert (s != NULL);
234
235   for (c = 0; c < s->networks; c++)
236   {
237       if (s->network_entries[c].total_addresses > 0)
238       {
239         LOG (GNUNET_ERROR_TYPE_ERROR,
240                     "Had %u addresses for network `%s' not deleted during shutdown\n",
241                     s->network_entries[c].total_addresses,
242                     s->network_entries[c].desc);
243         GNUNET_break (0);
244       }
245
246       if (s->network_entries[c].active_addresses > 0)
247       {
248         LOG (GNUNET_ERROR_TYPE_ERROR,
249                     "Had %u active addresses for network `%s' not deleted during shutdown\n",
250                     s->network_entries[c].active_addresses,
251                     s->network_entries[c].desc);
252         GNUNET_break (0);
253       }
254
255       next = s->network_entries[c].head;
256       while (NULL != (cur = next))
257       {
258           next = cur->next;
259           GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
260                                        s->network_entries[c].tail,
261                                        cur);
262           GNUNET_free (cur);
263       }
264       GNUNET_free (s->network_entries[c].stat_total);
265       GNUNET_free (s->network_entries[c].stat_active);
266   }
267   if (s->total_addresses > 0)
268   {
269     LOG (GNUNET_ERROR_TYPE_ERROR,
270                 "Had %u addresses not deleted during shutdown\n",
271                 s->total_addresses);
272     GNUNET_break (0);
273   }
274   if (s->active_addresses > 0)
275   {
276     LOG (GNUNET_ERROR_TYPE_ERROR,
277                 "Had %u active addresses not deleted during shutdown\n",
278                 s->active_addresses);
279     GNUNET_break (0);
280   }
281
282   GNUNET_free (s->network_entries);
283   GNUNET_free (s);
284 }
285
286 /**
287  * Update the quotas for a network type
288  *
289  * @param s the solver handle
290  * @param net the network type to update
291  * @param address_except address excluded from notifcation, since we suggest
292  * this address
293  */
294
295 static void
296 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
297                           struct Network *net,
298                           struct ATS_Address *address_except)
299 {
300   unsigned long long quota_in = 0;
301   unsigned long long quota_out = 0;
302   struct AddressWrapper *cur;
303
304   LOG (GNUNET_ERROR_TYPE_DEBUG,
305               "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
306               net->desc, net->active_addresses, quota_in, quota_out);
307
308   if (net->active_addresses == 0)
309     return; /* no addresses to update */
310
311   quota_in = net->total_quota_in / net->active_addresses;
312   quota_out = net->total_quota_out / net->active_addresses;
313
314   LOG (GNUNET_ERROR_TYPE_DEBUG,
315               "New per address quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
316               net->desc, net->active_addresses, quota_in, quota_out);
317
318   cur = net->head;
319   while (NULL != cur)
320   {
321       /* Compare to current bandwidth assigned */
322       if ((quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
323           (quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
324       {
325         cur->addr->assigned_bw_in.value__ = htonl (quota_in);
326         cur->addr->assigned_bw_out.value__ = htonl (quota_out);
327         /* Notify on change */
328         if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
329           s->bw_changed (s->bw_changed_cls, cur->addr);
330       }
331       cur = cur->next;
332   }
333 }
334
335 static void
336 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
337                                 struct Network *net,
338                                 int total,
339                                 int active)
340 {
341   if (GNUNET_YES == total)
342   {
343       s->total_addresses ++;
344       net->total_addresses ++;
345       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
346       GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
347   }
348   if (GNUNET_YES == active)
349   {
350     net->active_addresses ++;
351     s->active_addresses ++;
352     GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
353     GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
354   }
355
356 }
357
358 static int
359 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
360                     struct Network *net,
361                     int total,
362                     int active)
363 {
364   int res = GNUNET_OK;
365   if (GNUNET_YES == total)
366   {
367     if (s->total_addresses < 1)
368     {
369       GNUNET_break (0);
370       res = GNUNET_SYSERR;
371     }
372     else
373     {
374       s->total_addresses --;
375       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
376     }
377     if (net->total_addresses < 1)
378     {
379       GNUNET_break (0);
380       res = GNUNET_SYSERR;
381     }
382     else
383     {
384       net->total_addresses --;
385       GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
386     }
387   }
388
389   if (GNUNET_YES == active)
390   {
391     if (net->active_addresses < 1)
392     {
393       GNUNET_break (0);
394       res = GNUNET_SYSERR;
395     }
396     else
397     {
398       net->active_addresses --;
399       GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
400     }
401     if (s->active_addresses < 1)
402     {
403       GNUNET_break (0);
404       res = GNUNET_SYSERR;
405     }
406     else
407     {
408       s->active_addresses --;
409       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
410     }
411   }
412   return res;
413 }
414
415
416 /**
417  * Add a single address to the solve
418  *
419  * @param solver the solver Handle
420  * @param addresses the address hashmap containing all addresses
421  * @param address the address to add
422  */
423 void
424 GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
425 {
426   struct GAS_SIMPLISTIC_Handle *s = solver;
427   struct Network *net = NULL;
428   struct AddressWrapper *aw = NULL;
429   GNUNET_assert (NULL != s);
430   int c;
431   for (c = 0; c < s->networks; c++)
432   {
433       net = &s->network_entries[c];
434       if (address->atsp_network_type == net->type)
435           break;
436   }
437   if (NULL == net)
438   {
439     GNUNET_break (0);
440     return;
441   }
442
443   aw = GNUNET_malloc (sizeof (struct AddressWrapper));
444   aw->addr = address;
445   GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
446   addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
447   aw->addr->solver_information = net;
448
449
450   LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
451       net->total_addresses,
452       net->active_addresses,
453       net->desc);
454 }
455
456 /**
457  * Remove an address from the solver
458  *
459  * @param solver the solver handle
460  * @param addresses the address hashmap containing all addresses
461  * @param address the address to remove
462  * @param session_only delete only session not whole address
463  */
464 void
465 GAS_simplistic_address_delete (void *solver,
466     struct GNUNET_CONTAINER_MultiHashMap * addresses,
467     struct ATS_Address *address, int session_only)
468 {
469   struct GAS_SIMPLISTIC_Handle *s = solver;
470   struct Network *net;
471   struct AddressWrapper *aw;
472
473   /* Remove an adress completely, we have to:
474    * - Remove from specific network
475    * - Decrease number of total addresses
476    * - If active:
477    *   - decrease number of active addreses
478    *   - update quotas
479    */
480
481   net = (struct Network *) address->solver_information;
482
483   if (GNUNET_NO == session_only)
484   {
485     LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
486         (GNUNET_NO == address->active) ? "inactive" : "active",
487         address, GNUNET_i2s (&address->peer),
488         net->desc, net->total_addresses, net->active_addresses);
489
490     /* Remove address */
491     addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
492     for (aw = net->head; NULL != aw; aw = aw->next)
493     {
494         if (aw->addr == address)
495           break;
496     }
497     if (NULL == aw )
498     {
499         GNUNET_break (0);
500         return;
501     }
502     GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
503     GNUNET_free (aw);
504   }
505   else
506   {
507       /* Remove session only: remove if active and update */
508       LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
509           (GNUNET_NO == address->active) ? "inactive" : "active",
510           address, GNUNET_i2s (&address->peer),
511           net->desc, net->total_addresses, net->active_addresses);
512   }
513
514
515   if (GNUNET_YES == address->active)
516   {
517       /* Address was active, remove from network and update quotas*/
518       address->active = GNUNET_NO;
519       if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
520         GNUNET_break (0);
521       update_quota_per_network (s, net, NULL);
522   }
523   LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
524       net->total_addresses,
525       net->active_addresses,
526       net->desc);
527
528 }
529
530 static struct Network *
531 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
532 {
533   int c;
534   for (c = 0 ; c < s->networks; c++)
535   {
536       if (s->network_entries[c].type == type)
537         return &s->network_entries[c];
538   }
539   return NULL;
540 }
541
542 /**
543  * Updates a single address in the solve
544  *
545  * @param solver the solver Handle
546  * @param addresses the address hashmap containing all addresses
547  * @param address the update address
548  * @param session the new session (if changed otherwise current)
549  * @param in_use the new address in use state (if changed otherwise current)
550  * @param atsi the latest ATS information
551  * @param atsi_count the atsi count
552  */
553 void
554 GAS_simplistic_address_update (void *solver,
555                               struct GNUNET_CONTAINER_MultiHashMap *addresses,
556                               struct ATS_Address *address,
557                               uint32_t session,
558                               int in_use,
559                               const struct GNUNET_ATS_Information *atsi,
560                               uint32_t atsi_count)
561 {
562   struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
563   int i;
564   uint32_t value;
565   uint32_t type;
566   int save_active = GNUNET_NO;
567   struct Network *new_net = NULL;
568   for (i = 0; i < atsi_count; i++)
569   {
570     type = ntohl (atsi[i].type);
571     value = ntohl (atsi[i].value);
572     switch (type)
573     {
574     case GNUNET_ATS_UTILIZATION_UP:
575       //if (address->atsp_utilization_out.value__ != atsi[i].value)
576
577       break;
578     case GNUNET_ATS_UTILIZATION_DOWN:
579       //if (address->atsp_utilization_in.value__ != atsi[i].value)
580
581       break;
582     case GNUNET_ATS_QUALITY_NET_DELAY:
583       //if (address->atsp_latency.rel_value != value)
584
585       break;
586     case GNUNET_ATS_QUALITY_NET_DISTANCE:
587       //if (address->atsp_distance != value)
588
589       break;
590     case GNUNET_ATS_COST_WAN:
591       //if (address->atsp_cost_wan != value)
592
593       break;
594     case GNUNET_ATS_COST_LAN:
595       //if (address->atsp_cost_lan != value)
596
597       break;
598     case GNUNET_ATS_COST_WLAN:
599       //if (address->atsp_cost_wlan != value)
600
601       break;
602     case GNUNET_ATS_NETWORK_TYPE:
603       if (address->atsp_network_type != value)
604       {
605
606         LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
607             (GNUNET_YES == address->active) ? "active" : "inactive",
608             GNUNET_ATS_print_network_type(address->atsp_network_type),
609             GNUNET_ATS_print_network_type(value));
610
611         save_active = address->active;
612         /* remove from old network */
613         GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
614
615         /* set new network type */
616         address->atsp_network_type = value;
617         new_net = find_network (solver, value);
618         address->solver_information = new_net;
619         if (address->solver_information == NULL)
620         {
621             GNUNET_break (0);
622             address->atsp_network_type = GNUNET_ATS_NET_UNSPECIFIED;
623             return;
624         }
625
626         /* restore active state, add to new network and update*/
627         address->active = save_active;
628         GAS_simplistic_address_add (solver, addresses, address);
629         if (GNUNET_YES == save_active)
630         {
631           addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
632           update_quota_per_network (solver, new_net, NULL);
633         }
634       }
635       break;
636     case GNUNET_ATS_ARRAY_TERMINATOR:
637       break;
638     default:
639       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640                   "Received unsupported ATS type %u\n", type);
641       GNUNET_break (0);
642       break;
643
644     }
645
646   }
647   if (address->session_id != session)
648   {
649       LOG (GNUNET_ERROR_TYPE_DEBUG,
650                   "Session changed from %u to %u\n", address->session_id, session);
651       address->session_id = session;
652   }
653   if (address->used != in_use)
654   {
655       LOG (GNUNET_ERROR_TYPE_DEBUG,
656                   "Usage changed from %u to %u\n", address->used, in_use);
657       address->used = in_use;
658   }
659
660 }
661
662
663
664 /**
665  * Find a "good" address to use for a peer.  If we already have an existing
666  * address, we stick to it.  Otherwise, we pick by lowest distance and then
667  * by lowest latency.
668  *
669  * @param cls the 'struct ATS_Address**' where we store the result
670  * @param key unused
671  * @param value another 'struct ATS_Address*' to consider using
672  * @return GNUNET_OK (continue to iterate)
673  */
674 static int
675 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
676 {
677   struct ATS_Address **previous_p = cls;
678   struct ATS_Address *current = (struct ATS_Address *) value;
679   struct ATS_Address *previous = *previous_p;
680   struct GNUNET_TIME_Absolute now;
681
682   now = GNUNET_TIME_absolute_get();
683
684   if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
685   {
686     /* This address is blocked for suggestion */
687     LOG (GNUNET_ERROR_TYPE_DEBUG,
688                 "Address %p blocked for suggestion for %llu ms \n",
689                 current,
690                 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
691     return GNUNET_OK;
692   }
693
694   if (NULL != previous)
695   {
696     if ((0 == strcmp (previous->plugin, "tcp")) &&
697         (0 == strcmp (current->plugin, "tcp")))
698     {
699       if ((0 != previous->addr_len) &&
700           (0 == current->addr_len))
701       {
702         /* saved address was an outbound address, but we have an inbound address */
703         *previous_p = current;
704         return GNUNET_OK;
705       }
706       if (0 == previous->addr_len)
707       {
708         /* saved address was an inbound address, so do not overwrite */
709         return GNUNET_OK;
710       }
711     }
712   }
713
714   if (NULL == previous)
715   {
716     *previous_p = current;
717     return GNUNET_OK;
718   }
719   if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
720       (ntohl (current->assigned_bw_in.value__) > 0))
721   {
722     /* stick to existing connection */
723     *previous_p = current;
724     return GNUNET_OK;
725   }
726   if (previous->atsp_distance > current->atsp_distance)
727   {
728     /* user shorter distance */
729     *previous_p = current;
730     return GNUNET_OK;
731   }
732   if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
733   {
734     /* user lower latency */
735     *previous_p = current;
736     return GNUNET_OK;
737   }
738   /* don't care */
739   return GNUNET_OK;
740 }
741
742 static int
743 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
744 {
745   struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
746   struct ATS_Address * aa = (struct ATS_Address *) value;
747
748   if (GNUNET_YES == aa->active)
749   {
750       if (dest != NULL)
751       {
752           /* should never happen */
753           LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
754           GNUNET_break (0);
755           return GNUNET_NO;
756       }
757       dest = aa;
758   }
759   return GNUNET_OK;
760 }
761
762 static struct ATS_Address *
763 find_active_address (void *solver,
764                      struct GNUNET_CONTAINER_MultiHashMap * addresses,
765                      const struct GNUNET_PeerIdentity *peer)
766 {
767   struct ATS_Address * dest = NULL;
768
769   GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
770        &peer->hashPubKey,
771        &find_active_address_it, &dest);
772   return dest;
773 }
774
775 /**
776  * Get the prefered address for a specific peer
777  *
778  * @param solver the solver handle
779  * @param addresses the address hashmap containing all addresses
780  * @param peer the identity of the peer
781  */
782 const struct ATS_Address *
783 GAS_simplistic_get_preferred_address (void *solver,
784                                struct GNUNET_CONTAINER_MultiHashMap * addresses,
785                                const struct GNUNET_PeerIdentity *peer)
786 {
787   struct GAS_SIMPLISTIC_Handle *s = solver;
788   struct Network *net_prev;
789   struct Network *net_cur;
790   struct ATS_Address *cur;
791   struct ATS_Address *prev;
792
793   GNUNET_assert (s != NULL);
794   cur = NULL;
795   /* Get address with: stick to current address, lower distance, lower latency */
796   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
797                                               &find_address_it, &cur);
798   if (NULL == cur)
799   {
800     LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
801     return NULL;
802   }
803
804   LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
805       (GNUNET_NO == cur->active) ? "inactive" : "active",
806       cur, GNUNET_i2s (peer));
807   net_cur = (struct Network *) cur->solver_information;
808   if (GNUNET_YES == cur->active)
809   {
810       /* This address was selected previously, so no need to update quotas */
811       return cur;
812   }
813
814   /* This address was not active, so we have to:
815    *
816    * - mark previous active address as not active
817    * - update quota for previous address network
818    * - update quota for this address network
819    */
820
821   prev = find_active_address (s, addresses, peer);
822   if (NULL != prev)
823   {
824       net_prev = (struct Network *) prev->solver_information;
825       prev->active = GNUNET_NO; /* No active any longer */
826       prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
827       prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
828       s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
829       if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
830         GNUNET_break (0);
831       update_quota_per_network (s, net_prev, NULL);
832   }
833
834   cur->active = GNUNET_YES;
835   addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
836   update_quota_per_network (s, net_cur, cur);
837
838   return cur;
839 }
840
841
842 /**
843  * Changes the preferences for a peer in the problem
844  *
845  * @param solver the solver handle
846  * @param client the client with this preference
847  * @param peer the peer to change the preference for
848  * @param kind the kind to change the preference
849  * @param score the score
850  */
851 void
852 GAS_simplistic_address_change_preference (void *solver,
853                                    void *client,
854                                    const struct GNUNET_PeerIdentity *peer,
855                                    enum GNUNET_ATS_PreferenceKind kind,
856                                    float score)
857 {
858
859 }
860
861 /* end of gnunet-service-ats_addresses_simplistic.c */