fix for 0002392
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.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.c
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_performance.h"
32 #include "gnunet-service-ats_scheduling.h"
33 #include "gnunet-service-ats_reservations.h"
34 #if HAVE_LIBGLPK
35 #include "gnunet-service-ats_addresses_mlp.h"
36 #endif
37
38 #define VERBOSE GNUNET_NO
39
40 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
41
42 enum ATS_Mode
43 {
44   /*
45    * Assign each peer an equal amount of bandwidth (bw)
46    *
47    * bw_per_peer = bw_total / #active addresses
48    */
49   SIMPLE,
50
51   /*
52    * Use MLP solver to assign bandwidth
53    */
54   MLP
55 };
56
57 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
58
59 #if HAVE_LIBGLPK
60 static struct GAS_MLP_Handle *mlp;
61 #endif
62
63 static unsigned long long wan_quota_in;
64
65 static unsigned long long wan_quota_out;
66
67 static unsigned int active_addr_count;
68
69 static int ats_mode;
70
71 static int running;
72
73
74 static void
75 send_bw_notification (struct ATS_Address *aa)
76 {
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
78               GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
79               ntohl (aa->assigned_bw_out.value__));
80   GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
81                                               aa->addr_len, aa->session_id,
82                                               aa->ats, aa->ats_count,
83                                               aa->assigned_bw_out,
84                                               aa->assigned_bw_in);
85   GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
86   GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
87                                   aa->ats, aa->ats_count, aa->assigned_bw_out,
88                                   aa->assigned_bw_in);
89 }
90
91 /**
92  * Update a bandwidth assignment for a peer.  This trivial method currently
93  * simply assigns the same share to all active connections.
94  *
95  * @param cls unused
96  * @param key unused
97  * @param value the 'struct ATS_Address'
98  * @return GNUNET_OK (continue to iterate)
99  */
100 static int
101 update_bw_simple_it (void *cls, const GNUNET_HashCode * key, void *value)
102 {
103   struct ATS_Address *aa = value;
104
105   if (GNUNET_YES != aa->active)
106     return GNUNET_OK;
107   GNUNET_assert (active_addr_count > 0);
108
109
110   /* Simple method */
111   aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
112   aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
113
114   send_bw_notification (aa);
115
116   return GNUNET_OK;
117 }
118
119
120 /**
121  * Some (significant) input changed, recalculate bandwidth assignment
122  * for all peers.
123  */
124 static void
125 recalculate_assigned_bw ()
126 {
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Recalculating bandwidth for all active connections\n");
129   GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
130                             1, GNUNET_NO);
131   GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
132                          GNUNET_NO);
133
134   GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
135 }
136
137 /**
138  * Free the given address
139  * @param addr address to destroy
140  */
141 static void
142 free_address (struct ATS_Address *addr)
143 {
144   GNUNET_free_non_null (addr->ats);
145   GNUNET_free (addr->plugin);
146   GNUNET_free (addr);
147 }
148
149 /**
150  * Create a ATS_address with the given information
151  * @param peer peer
152  * @param plugin_name plugin
153  * @param plugin_addr address
154  * @param plugin_addr_len address length
155  * @param session_id session
156  * @return the ATS_Address
157  */
158 static struct ATS_Address *
159 create_address (const struct GNUNET_PeerIdentity *peer,
160                 const char *plugin_name,
161                 const void *plugin_addr, size_t plugin_addr_len,
162                 uint32_t session_id)
163 {
164   struct ATS_Address *aa = NULL;
165
166   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
167   aa->peer = *peer;
168   aa->addr_len = plugin_addr_len;
169   aa->addr = &aa[1];
170   memcpy (&aa[1], plugin_addr, plugin_addr_len);
171   aa->plugin = GNUNET_strdup (plugin_name);
172   aa->session_id = session_id;
173   return aa;
174 }
175
176
177 /**
178  * Destroy the given address.
179  *
180  * @param addr address to destroy
181  * @return GNUNET_YES if bandwidth allocations should be recalcualted
182  */
183 static int
184 destroy_address (struct ATS_Address *addr)
185 {
186   int ret;
187
188   ret = GNUNET_NO;
189   GNUNET_assert (GNUNET_YES ==
190                  GNUNET_CONTAINER_multihashmap_remove (addresses,
191                                                        &addr->peer.hashPubKey,
192                                                        addr));
193
194 #if HAVE_LIBGLPK
195   if (ats_mode == MLP)
196     GAS_mlp_address_delete (mlp, addresses, addr);
197 #endif
198
199   if (GNUNET_YES == addr->active)
200   {
201     active_addr_count--;
202     addr->active = GNUNET_NO;
203     ret = GNUNET_YES;
204   }
205   free_address (addr);
206   return ret;
207 }
208
209
210 struct CompareAddressContext
211 {
212   const struct ATS_Address *search;
213
214   /* exact_address != NULL if address and session is equal */
215   struct ATS_Address *exact_address;
216   /* exact_address != NULL if address and session is 0 */
217   struct ATS_Address *base_address;
218 };
219
220
221 static int
222 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
223 {
224   struct CompareAddressContext *cac = cls;
225   struct ATS_Address *aa = value;
226 /*
227   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228               "Comparing to: %s %s %u session %u\n",
229               GNUNET_i2s (&aa->peer), aa->plugin, aa->addr_len, aa->session_id);
230
231 */
232   /* find an exact matching address: aa->addr == cac->search->addr && aa->session == cac->search->session */
233   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
234   {
235       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
236       {
237         cac->exact_address = aa;
238       }
239   }
240
241   /* find an matching address: aa->addr == cac->search->addr && aa->session == 0 */
242   /* this address can be used to be updated */
243   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
244   {
245       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
246       {
247         cac->base_address = aa;
248       }
249   }
250
251   if (cac->exact_address == NULL)
252     return GNUNET_YES;
253   else
254     return GNUNET_NO;
255 }
256
257
258 /**
259  * Find an existing equivalent address record.
260  * Compares by peer identity and network address OR by session ID
261  * (one of the two must match).
262  *
263  * @param peer peer to lookup addresses for
264  * @param addr existing address record
265  * @return existing address record, NULL for none
266  */
267 struct ATS_Address *
268 find_address (const struct GNUNET_PeerIdentity *peer,
269               const struct ATS_Address *addr)
270 {
271   struct CompareAddressContext cac;
272
273   cac.exact_address = NULL;
274   cac.base_address = NULL;
275   cac.search = addr;
276   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
277                                               &compare_address_it, &cac);
278
279 /*
280   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281               "exact address: %s           base address: %s\n",
282               (cac.exact_address != NULL) ? "YES" : "NO",
283               (cac.base_address != NULL) ? "YES" : "NO");
284 */
285   if (cac.exact_address == NULL)
286     return cac.base_address;
287   return cac.exact_address;
288 }
289
290
291 static int
292 compare_address_session_it (void *cls, const GNUNET_HashCode * key, void *value)
293 {
294   struct CompareAddressContext *cac = cls;
295   struct ATS_Address *aa = value;
296
297   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
298   {
299       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
300       {
301         cac->exact_address = aa;
302         return GNUNET_NO;
303       }
304   }
305   return GNUNET_YES;
306 }
307
308
309 /**
310  * Find an existing equivalent address record.
311  * Compares by peer identity and network address AND by session ID
312  * (one of the two must match).
313  *
314  * @param peer peer to lookup addresses for
315  * @param addr existing address record
316  * @return existing address record, NULL for none
317  */
318 struct ATS_Address *
319 find_exact_address (const struct GNUNET_PeerIdentity *peer,
320               const struct ATS_Address *addr)
321 {
322   struct CompareAddressContext cac;
323
324   cac.exact_address = NULL;
325   cac.search = addr;
326   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
327                                               &compare_address_session_it, &cac);
328   return cac.exact_address;
329 }
330
331
332 void
333 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
334                       const char *plugin_name, const void *plugin_addr,
335                       size_t plugin_addr_len, uint32_t session_id,
336                       const struct GNUNET_ATS_Information *atsi,
337                       uint32_t atsi_count)
338 {
339   struct ATS_Address *aa;
340   struct ATS_Address *old;
341   uint32_t i;
342
343   if (GNUNET_NO == running)
344     return;
345
346   GNUNET_assert (NULL != addresses);
347
348   aa = create_address (peer,
349                        plugin_name,
350                        plugin_addr, plugin_addr_len,
351                        session_id);
352
353   aa->mlp_information = NULL;
354   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
355   aa->ats_count = atsi_count;
356   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
357
358   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n",
359               GNUNET_i2s (peer),
360               session_id);
361
362   /* Get existing address or address with session == 0 */
363   old = find_address (peer, aa);
364   if (old == NULL)
365   {
366     GNUNET_assert (GNUNET_OK ==
367                    GNUNET_CONTAINER_multihashmap_put (addresses,
368                                                       &peer->hashPubKey, aa,
369                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
370 #if DEBUG_ATS
371     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
372                 GNUNET_i2s (peer), aa);
373 #endif
374     old = aa;
375   }
376   else
377   {
378 #if DEBUG_ATS
379       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380                 "Updated existing address for peer `%s' %p old session %u new session %u\n",
381                 GNUNET_i2s (peer), old,
382                 old->session_id, session_id);
383 #endif
384     GNUNET_free_non_null (old->ats);
385     old->session_id = session_id;
386     old->ats = NULL;
387     old->ats_count = 0;
388     old->ats = aa->ats;
389     old->ats_count = aa->ats_count;
390     GNUNET_free (aa->plugin);
391     GNUNET_free (aa);
392   }
393   for (i = 0; i < atsi_count; i++)
394     switch (ntohl (atsi[i].type))
395     {
396     case GNUNET_ATS_UTILIZATION_UP:
397       old->atsp_utilization_out.value__ = atsi[i].value;
398       break;
399     case GNUNET_ATS_UTILIZATION_DOWN:
400       old->atsp_utilization_in.value__ = atsi[i].value;
401       break;
402     case GNUNET_ATS_QUALITY_NET_DELAY:
403       old->atsp_latency.rel_value = ntohl (atsi[i].value);
404       break;
405     case GNUNET_ATS_QUALITY_NET_DISTANCE:
406       old->atsp_distance = ntohl (atsi[i].value);
407       break;
408     case GNUNET_ATS_COST_WAN:
409       old->atsp_cost_wan = ntohl (atsi[i].value);
410       break;
411     case GNUNET_ATS_COST_LAN:
412       old->atsp_cost_lan = ntohl (atsi[i].value);
413       break;
414     case GNUNET_ATS_COST_WLAN:
415       old->atsp_cost_wlan = ntohl (atsi[i].value);
416       break;
417     case GNUNET_ATS_NETWORK_TYPE:
418       old->atsp_network_type = ntohl (atsi[i].value);
419       break;
420
421     default:
422       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
423                   "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
424       GNUNET_break (0);
425       break;
426     }
427 #if HAVE_LIBGLPK
428   if (ats_mode == MLP)
429     GAS_mlp_address_update (mlp, addresses, old);
430 #endif
431 }
432
433
434 /**
435  * Delete an address
436  *
437  * If session != 0, just the session is deleted, the address itself still exists
438  * If session == 0, remove full address
439  * If session == 0 and addrlen == 0, destroy inbound address
440  *
441  * @param cls unused
442  * @param key unused
443  * @param value the 'struct ATS_Address'
444  * @return GNUNET_OK (continue to iterate)
445  */
446 static int
447 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
448 {
449   const struct ATS_Address *info = cls;
450   struct ATS_Address *aa = value;
451
452   GNUNET_assert (0 ==
453                  memcmp (&aa->peer, &info->peer,
454                          sizeof (struct GNUNET_PeerIdentity)));
455   /* session == 0, remove full address  */
456   if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
457       (aa->addr_len == info->addr_len) &&
458       (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
459   {
460
461     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462                 "Deleting address for peer `%s': `%s' %u\n",
463                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
464
465     if (GNUNET_YES == destroy_address (aa))
466       recalculate_assigned_bw ();
467     return GNUNET_OK;
468   }
469   /* session != 0, just remove session */
470   if (aa->session_id != info->session_id)
471     return GNUNET_OK;           /* irrelevant */
472   if (aa->session_id != 0)
473     GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
474   /* session died */
475 #if VERBOSE
476   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477               "Deleting session for peer `%s': `%s' %u\n",
478               GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
479 #endif
480   aa->session_id = 0;
481
482   if (GNUNET_YES == aa->active)
483   {
484     aa->active = GNUNET_NO;
485     active_addr_count--;
486     recalculate_assigned_bw ();
487   }
488
489   /* session == 0 and addrlen == 0 : destroy address */
490   if (aa->addr_len == 0)
491   {
492 #if VERBOSE
493     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494                 "Deleting session and address for peer `%s': `%s' %u\n",
495                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
496 #endif
497     (void) destroy_address (aa);
498   }
499   else
500   {
501     /* session was set to 0, update address */
502 #if HAVE_LIBGLPK
503   if (ats_mode == MLP)
504     GAS_mlp_address_update (mlp, addresses, aa);
505 #endif
506   }
507
508   return GNUNET_OK;
509 }
510
511 void
512 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
513                        const char *plugin_name, const void *plugin_addr,
514                        size_t plugin_addr_len, uint32_t session_id)
515 {
516   struct ATS_Address *aa;
517
518   if (GNUNET_NO == running)
519     return;
520
521   GNUNET_break (0 < strlen (plugin_name));
522   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
523
524   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
525                                               &destroy_by_session_id, aa);
526
527   free_address (aa);
528 }
529
530
531 /**
532  * Find a "good" address to use for a peer.  If we already have an existing
533  * address, we stick to it.  Otherwise, we pick by lowest distance and then
534  * by lowest latency.
535  *
536  * @param cls the 'struct ATS_Address**' where we store the result
537  * @param key unused
538  * @param value another 'struct ATS_Address*' to consider using
539  * @return GNUNET_OK (continue to iterate)
540  */
541 static int
542 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
543 {
544   struct ATS_Address **ap = cls;
545   struct ATS_Address *aa = (struct ATS_Address *) value;
546   struct ATS_Address *ab = *ap;
547   struct GNUNET_TIME_Absolute now;
548
549   now = GNUNET_TIME_absolute_get();
550
551   if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
552   {
553     /* This address is blocked for suggestion */
554     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555                 "Address %p blocked for suggestion for %llu ms \n",
556                 aa,
557                 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
558     return GNUNET_OK;
559   }
560
561   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
562   aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
563
564   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565               "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
566
567   /* FIXME this is a hack */
568
569
570   if (NULL != ab)
571   {
572     if ((0 == strcmp (ab->plugin, "tcp")) &&
573         (0 == strcmp (aa->plugin, "tcp")))
574     {
575       if ((0 != ab->addr_len) &&
576           (0 == aa->addr_len))
577       {
578         /* saved address was an outbound address, but we have an inbound address */
579         *ap = aa;
580         return GNUNET_OK;
581       }
582       if (0 == ab->addr_len)
583       {
584         /* saved address was an inbound address, so do not overwrite */
585         return GNUNET_OK;
586       }
587     }
588   }
589   /* FIXME end of hack */
590
591   if (NULL == ab)
592   {
593     *ap = aa;
594     return GNUNET_OK;
595   }
596   if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
597       (ntohl (aa->assigned_bw_in.value__) > 0))
598   {
599     /* stick to existing connection */
600     *ap = aa;
601     return GNUNET_OK;
602   }
603   if (ab->atsp_distance > aa->atsp_distance)
604   {
605     /* user shorter distance */
606     *ap = aa;
607     return GNUNET_OK;
608   }
609   if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
610   {
611     /* user lower latency */
612     *ap = aa;
613     return GNUNET_OK;
614   }
615   /* don't care */
616   return GNUNET_OK;
617 }
618
619
620 int
621 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
622                       const char *plugin_name, const void *plugin_addr,
623                       size_t plugin_addr_len, uint32_t session_id, int in_use)
624 {
625 #if DEBUG_ATS
626   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627               "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
628               GNUNET_i2s (peer), in_use);
629 #endif
630
631   struct ATS_Address *aa;
632   struct ATS_Address *old;
633
634   if (GNUNET_NO == running)
635     return GNUNET_SYSERR;
636
637   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
638   old = find_exact_address (peer, aa);
639   free_address (aa);
640
641   if (NULL == old)
642   {
643     GNUNET_break (0);
644     return GNUNET_SYSERR;
645   }
646   if (old->used == in_use)
647   {
648     GNUNET_break (0);
649     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
650                 "Address in use called multiple times for peer `%s': %s -> %s \n",
651                 GNUNET_i2s (peer),
652                 (GNUNET_NO == old->used) ? "NO" : "YES",
653                 (GNUNET_NO == in_use) ? "NO" : "YES");
654     return GNUNET_SYSERR;
655   }
656   old->used = in_use;
657 #if HAVE_LIBGLPK
658   if (ats_mode == MLP)
659      GAS_mlp_address_update (mlp, addresses, old);
660 #endif
661   return GNUNET_OK;
662 }
663
664
665 void request_address_mlp (const struct GNUNET_PeerIdentity *peer)
666 {
667   struct ATS_Address *aa;
668   aa = NULL;
669
670 #if HAVE_GLPK
671   /* Get preferred address from MLP */
672   struct ATS_PreferedAddress * paddr = NULL;
673   paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
674   aa = paddr->address;
675   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
676   /* FIXME use bw in value */
677   paddr->bandwidth_in = paddr->bandwidth_out;
678   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
679   GNUNET_free (paddr);
680 #endif
681
682   if (aa == NULL)
683   {
684     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
685                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
686     return;
687   }
688   if (aa->active == GNUNET_NO)
689   {
690     aa->active = GNUNET_YES;
691     active_addr_count++;
692
693     send_bw_notification (aa);
694   }
695   else
696   {
697     /* just to be sure... */
698     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
699                                                 aa->addr_len, aa->session_id,
700                                                 aa->ats, aa->ats_count,
701                                                 aa->assigned_bw_out,
702                                                 aa->assigned_bw_in);
703   }
704
705 }
706
707 void request_address_simple (const struct GNUNET_PeerIdentity *peer)
708 {
709   struct ATS_Address *aa;
710   aa = NULL;
711
712   /* Get address with: stick to current address, lower distance, lower latency */
713   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
714                                               &find_address_it, &aa);
715   if (aa == NULL)
716   {
717     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
718                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
719     return;
720   }
721
722   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
723               "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
724
725   if (aa->active == GNUNET_NO)
726   {
727     aa->active = GNUNET_YES;
728     active_addr_count++;
729     if (ats_mode == SIMPLE)
730     {
731       recalculate_assigned_bw ();
732     }
733   }
734   else
735   {
736     /* just to be sure... */
737     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
738                                                 aa->addr_len, aa->session_id,
739                                                 aa->ats, aa->ats_count,
740                                                 aa->assigned_bw_out,
741                                                 aa->assigned_bw_in);
742   }
743 }
744
745
746 void
747 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
748 {
749   if (GNUNET_NO == running)
750     return;
751
752   if (ats_mode == SIMPLE)
753   {
754     request_address_simple (peer);
755   }
756   if (ats_mode == MLP)
757   {
758     request_address_mlp(peer);
759   }
760 }
761
762
763 static int
764 reset_address_it (void *cls, const GNUNET_HashCode * key, void *value)
765 {
766   struct ATS_Address *aa = value;
767
768   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769               "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
770
771   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
772   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
773   return GNUNET_OK;
774 }
775
776 void
777 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
778 {
779   GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
780                                               &peer->hashPubKey,
781                                               &reset_address_it,
782                                               NULL));
783 }
784
785
786
787 // FIXME: this function should likely end up in the LP-subsystem and
788 // not with 'addresses' in the future...
789 void
790 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
791                                  enum GNUNET_ATS_PreferenceKind kind,
792                                  float score)
793 {
794   if (GNUNET_NO == running)
795     return;
796 #if HAVE_LIBGLPK
797   if (ats_mode == MLP)
798     GAS_mlp_address_change_preference (mlp, peer, kind, score);
799 #endif
800 }
801
802
803
804 /**
805  * Initialize address subsystem.
806  *
807  * @param cfg configuration to use
808  * @param stats the statistics handle to use
809  */
810 void
811 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
812                     const struct GNUNET_STATISTICS_Handle *stats)
813 {
814   int mode;
815
816   char *quota_wan_in_str;
817   char *quota_wan_out_str;
818
819   running = GNUNET_NO;
820
821   addresses = GNUNET_CONTAINER_multihashmap_create (128);
822   GNUNET_assert (NULL != addresses);
823
824   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_wan_in_str))
825   {
826     if (0 == strcmp(quota_wan_in_str, "unlimited") ||
827         (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
828       wan_quota_in = (UINT32_MAX) /10;
829
830     GNUNET_free (quota_wan_in_str);
831     quota_wan_in_str = NULL;
832   }
833   else
834   {
835     wan_quota_in = (UINT32_MAX) /10;
836   }
837
838   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_wan_out_str))
839   {
840     if (0 == strcmp(quota_wan_out_str, "unlimited") ||
841         (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
842       wan_quota_out = (UINT32_MAX) /10;
843
844     GNUNET_free (quota_wan_out_str);
845     quota_wan_out_str = NULL;
846   }
847   else
848   {
849     wan_quota_out = (UINT32_MAX) /10;
850   }
851
852   mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
853   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
854   switch (mode)
855   {
856     /* MLP = YES */
857     case GNUNET_YES:
858 #if HAVE_LIBGLPK
859       ats_mode = MLP;
860       /* Init the MLP solver with default values */
861       mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
862       if (NULL == mlp)
863       {
864         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
865         GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
866         break;
867       }
868       else
869       {
870         GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
871         break;
872       }
873 #else
874       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
875       GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
876       ats_mode = SIMPLE;
877       break;
878 #endif
879     /* MLP = NO */
880     case GNUNET_NO:
881       GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
882       ats_mode = SIMPLE;
883       break;
884     /* No configuration value */
885     case GNUNET_SYSERR:
886       GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
887       ats_mode = SIMPLE;
888       break;
889     default:
890       break;
891   }
892   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
893   running = GNUNET_YES;
894 }
895
896
897 /**
898  * Free memory of address.
899  *
900  * @param cls NULL
901  * @param key peer identity (unused)
902  * @param value the 'struct ATS_Address' to free
903  * @return GNUNET_OK (continue to iterate)
904  */
905 static int
906 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
907 {
908   struct ATS_Address *aa = value;
909
910   destroy_address (aa);
911   return GNUNET_OK;
912 }
913
914
915 void
916 GAS_addresses_destroy_all ()
917 {
918   if (GNUNET_NO == running)
919     return;
920
921   if (addresses != NULL)
922     GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
923   GNUNET_assert (active_addr_count == 0);
924 }
925
926
927 /**
928  * Shutdown address subsystem.
929  */
930 void
931 GAS_addresses_done ()
932 {
933   GAS_addresses_destroy_all ();
934   running = GNUNET_NO;
935   GNUNET_CONTAINER_multihashmap_destroy (addresses);
936   addresses = NULL;
937 #if HAVE_LIBGLPK
938   if (ats_mode == MLP)
939   {
940     GAS_mlp_done (mlp);
941   }
942 #endif
943
944 }
945
946
947 /* end of gnunet-service-ats_addresses.c */