d0028f9b0c8333df08189f543e09b5b932aa3ccd
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
1 /*
2  This file is part of GNUnet.
3  (C) 2011-2015 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_normalization.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "gnunet-service-ats_plugins.h"
34 #include "gnunet-service-ats_scheduling.h"
35 #include "gnunet-service-ats_reservations.h"
36
37
38 /**
39  * NOTE: Do not change this documentation. This documentation is based on
40  * gnunet.org:/vcs/fsnsg/2014-p2p-ats.git/tech-doku/ats-tech-guide.tex
41  * use build_txt.sh to generate plaintext output
42  *
43  *   1 ATS addresses : ATS address management
44  *
45  *    This ATS addresses ("addresses") component manages the addresses known to
46  *    ATS service and suggests addresses to transport service when it is
47  *    interested in address suggestion for a peer. ATS addresses also
48  *    instantiates the bandwidth assignment mechanism (solver), notifies it
49  *    about changes to addresses and forwards changes to bandwidth assignments
50  *    to transport, depending if transport is interested in this change.
51  *
52  *     1.1 Input data
53  *
54  *       1.1.1 Addresses
55  *
56  *    Addresses are added by specifying peer ID, plugin, address, address length
57  *    and session, if available. ATS information can be specified if available.
58  *
59  *       1.1.2 Networks
60  *
61  *    ATS specifies a fix set of networks an address can belong to. For each
62  *    network an inbound and outbound quota will be specified. The available
63  *    networks and addtional helper varaibles are defined in
64  *    gnunet_ats_service.h. At the moment 5 networks are defined:
65  *      * GNUNET_ATS_NET_UNSPECIFIED
66  *      * GNUNET_ATS_NET_LOOPBACK
67  *      * GNUNET_ATS_NET_LAN
68  *      * GNUNET_ATS_NET_WAN
69  *      * GNUNET_ATS_NET_WLAN
70  *
71  *    The total number of networks defined is stored in
72  *    GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
73  *    initializer for an int array, while GNUNET_ATS_NetworkType is an
74  *    initializer for a char array containing a string description of all
75  *    networks
76  *
77  *       1.1.3 Quotas
78  *
79  *    An inbound and outbound quota for each of the networks mentioned in 1.1.2
80  *    is loaded from ats configuration during initialization. This quota defines
81  *    to total amount of inbound and outbound traffic allowed for a specific
82  *    network. The configuration values used are in section ats:
83  *      * "NETWORK"_QUOTA_IN = <value>
84  *      * "NETWORK"_QUOTA_IN = <value>
85  *
86  *    You can specify quotas by setting the <value> to a:
87  *      * unrestricted: unlimited
88  *      * number of bytes: e.g. 10240
89  *      * fancy value: e.g. 64 Kib
90  *
91  *    unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
92  *    the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
93  *    are:
94  *      * GNUNET_ATS_DefaultBandwidth: 65536
95  *      * GNUNET_ATS_MaxBandwidth: UINT32_MAX
96  *      * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
97  *
98  *    Details of loading quotas and default values will be described on
99  *
100  *       1.1.4 Preference values
101  *
102  *     1.2 Data structures used
103  *
104  *    Addresse uses struct ATS_Address for each address. The structs are stored
105  *    in a linked list and provides a pointer void *solver_information for the
106  *    solver to store address specific information. It provides the int values
107  *    active which is set to GNUNET_YES if the address is select for transport
108  *    use and used, representing that transport service is actively using this
109  *    address. Address information are stored in peer, addr, addr_len, plugin.
110  *
111  *     1.3 Initialization
112  *
113  *    During initialization a hashmap to store addresses is created. The quotas
114  *    for all networks defined for ATS are loaded from configuration. For each
115  *    network first the logic will check if the string
116  *    GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
117  *    the configured value as a fancy size and if this fails it will try to use
118  *    it as a value_number. If no configuration value is found it will assign
119  *    GNUNET_ATS_DefaultBandwidth. The most important step is to load the
120  *    configured solver using configuration "[ats]:MODE". Current solvers are
121  *    MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
122  *
123  *     1.4 Solver API
124  *
125  *    Solver functions:
126  *      * s_init: init the solver with required information
127  *      * s_add: add a new address
128  *      * s_update: update ATS values or session for an address
129  *      * s_get: get prefered address for a peer
130  *      * s_del: delete an address
131  *      * s_pref: change preference value for a peer
132  *      * s_done: shutdown solver
133  *
134  *    Callbacks: addresses provides a bandwidth_changed_cb callback to the
135  *    solver which is called when bandwidth assigned to peer has changed
136  *
137  *     1.5 Shutdown
138  *
139  *    During shutdown all addresses are freed and the solver told to shutdown
140  *
141  *     1.6 Addresses and sessions
142  *
143  *    Addresses consist of the address itself and a numerical session. When a
144  *    new address without a session is added it has no session, so it gets
145  *    session 0 assigned. When an address with a session is added and an address
146  *    object with session 0 is found, this object is updated with the session
147  *    otherwise a new address object with this session assigned is created.
148  *
149  *       1.6.1 Terminology
150  *
151  *    Addresses a1,a2 with session s1, s2 are "exact" if:
152  *    (a1 == a2)&&(s1 == s2)
153  *    Addresses a1,a2 with session s1, s2 are "equivalent" if:
154  *    (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
155  *
156  *     1.7 Address management
157  *
158  *    Transport service notifies ATS about changes to the addresses known to
159  *    him.
160  *
161  *       1.7.1 Adding an address
162  *
163  *    When transport learns a new address it tells ATS and ATS is telling
164  *    addresses about it using GAS_address_add. If not known to addresses it
165  *    creates a new address object and calls solver's s_add. ATS information are
166  *    deserialized and solver is notified about the session and ATS information
167  *    using s_update.
168  *
169  *       1.7.2 Updating an address
170  *
171  *    Addresses does an lookup up for the existing address with the given
172  *    session. If disassembles included ATS information and notifies the solver
173  *    using s_update about the update.
174  *
175  *       1.7.3 Deleting an address
176  *
177  *    Addresses does an lookup for the exact address and session and if removes
178  *    this address. If session != 0 the session is set to 0 and the address is
179  *    kept. If session == 0, the addresses is removed.
180  *
181  *       1.7.4 Requesting an address suggestion
182  *
183  *    The address client issues a request address message to be notified about
184  *    address suggestions for a specific peer. Addresses asks the solver with
185  *    s_get. If no address is available, it will not send a response, otherwise
186  *    it will respond with the choosen address.
187  *
188  *       1.7.5 Address suggestions
189  *
190  *    Addresses will notify the client automatically on any bandwidth_changed_cb
191  *    by the solver if a address suggestion request is pending. If no address is
192  *    available it will not respond at all If the client is not interested
193  *    anymore, it has to cancel the address suggestion request.
194  *
195  *       1.7.6 Address lifecycle
196  *
197  *      * (add address)
198  *      * (updated address)
199  *      * (delete address)
200  *
201  *     1.8 Bandwidth assignment
202  *
203  *    The addresses are used to perform resource allocation operations. ATS
204  *    addresses takes care of instantiating the solver configured and notifies
205  *    the respective solver about address changes and receives changes to the
206  *    bandwidth assignment from the solver. The current bandwidth assignment is
207  *    sent to transport. The specific solvers will be described in the specific
208  *    section.
209  *
210  *     1.9 Changing peer preferences
211  *
212  *    The bandwidth assigned to a peer can be influenced by setting a preference
213  *    for a peer. The prefernce will be given to to the solver with s_pref which
214  *    has to take care of the preference value
215  */
216
217
218 /**
219  * A multihashmap to store all addresses
220  */
221 struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
222
223
224 /**
225  * Update statistic on number of addresses.
226  */
227 static void
228 update_addresses_stat ()
229 {
230   GNUNET_STATISTICS_set (GSA_stats,
231                          "# addresses",
232                          GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
233                          GNUNET_NO);
234 }
235
236
237 /**
238  * Disassemble ATS information and update performance information in address
239  *
240  * Updates existing information and adds new information
241  *
242  * @param dest destination address
243  * @param update source ATS information
244  * @param update_count number of ATS information in @a update
245  * @param delta_dest ats performance information which were updated
246  *                              including previous value
247  * @param delta_count number of ATS information in the @a delta_dest
248  * @return #GNUNET_YES if address was address updated, GNUNET_NO otherwise
249  */
250 static unsigned int
251 disassemble_ats_information (struct ATS_Address *dest,
252                              const struct GNUNET_ATS_Information *update,
253                              uint32_t update_count,
254                              struct GNUNET_ATS_Information **delta_dest,
255                              uint32_t *delta_count)
256 {
257   int c1;
258   int c2;
259   int found;
260   int change;
261   struct GNUNET_ATS_Information add_atsi[update_count];
262   struct GNUNET_ATS_Information delta_atsi[update_count];
263   struct GNUNET_ATS_Information *tmp_atsi;
264   uint32_t add_atsi_count;
265   uint32_t delta_atsi_count;
266
267   change = GNUNET_NO;
268   add_atsi_count = 0;
269   delta_atsi_count = 0;
270
271   if (0 == update_count)
272     return GNUNET_NO;
273
274   if (NULL == dest->atsi)
275   {
276     /* Create performance information */
277     dest->atsi =
278         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
279     dest->atsi_count = update_count;
280     memcpy (dest->atsi,
281             update,
282             update_count * sizeof(struct GNUNET_ATS_Information));
283     *delta_dest =
284         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
285     for (c1 = 0; c1 < update_count; c1++)
286     {
287       (*delta_dest)[c1].type = update[c1].type;
288       (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
289     }
290     (*delta_count) = update_count;
291     return GNUNET_YES;
292   }
293
294   for (c1 = 0; c1 < update_count; c1++)
295   {
296     /* Update existing performance information */
297     found = GNUNET_NO;
298     for (c2 = 0; c2 < dest->atsi_count; c2++)
299     {
300       if (update[c1].type == dest->atsi[c2].type)
301       {
302         if (update[c1].value != dest->atsi[c2].value)
303         {
304           /* Save previous value in delta */
305           delta_atsi[delta_atsi_count] = dest->atsi[c2];
306           delta_atsi_count++;
307           /* Set new value */
308           dest->atsi[c2].value = update[c1].value;
309           change = GNUNET_YES;
310         }
311         found = GNUNET_YES;
312         break;
313       }
314     }
315     if (GNUNET_NO == found)
316     {
317       add_atsi[add_atsi_count] = update[c1];
318       add_atsi_count++;
319       delta_atsi[delta_atsi_count].type = update[c1].type;
320       delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
321       delta_atsi_count++;
322     }
323   }
324
325   if (add_atsi_count > 0)
326   {
327     /* Extend ats performance information */
328
329     tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
330         (sizeof (struct GNUNET_ATS_Information)));
331     memcpy (tmp_atsi, dest->atsi,
332         dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
333     memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
334         add_atsi_count * sizeof(struct GNUNET_ATS_Information));
335     GNUNET_free (dest->atsi);
336     dest->atsi = tmp_atsi;
337     dest->atsi_count = dest->atsi_count + add_atsi_count;
338     change = GNUNET_YES;
339   }
340
341   if (delta_atsi_count > 0)
342   {
343     /* Copy delta */
344     (*delta_dest) =
345         GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
346     memcpy ((*delta_dest), delta_atsi,
347         delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
348     (*delta_count) = delta_atsi_count;
349   }
350
351   return change;
352 }
353
354
355 /**
356  * Free the given address
357  *
358  * @param addr address to destroy
359  */
360 static void
361 free_address (struct ATS_Address *addr)
362 {
363   GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
364                                         &addr->peer,
365                                         addr);
366   update_addresses_stat ();
367   GAS_plugin_delete_address (addr);
368   GAS_performance_notify_all_clients (&addr->peer,
369                                       addr->plugin,
370                                       addr->addr,
371                                       addr->addr_len,
372                                       GNUNET_NO,
373                                       NULL, 0,
374                                       GNUNET_BANDWIDTH_ZERO,
375                                       GNUNET_BANDWIDTH_ZERO);
376   GNUNET_free (addr->plugin);
377   GNUNET_free_non_null (addr->atsi);
378   GNUNET_free (addr);
379 }
380
381
382 /**
383  * Create a ATS_address with the given information
384  *
385  * @param peer peer
386  * @param plugin_name plugin
387  * @param plugin_addr address
388  * @param plugin_addr_len address length
389  * @param local_address_info additional local info for the address
390  * @param session_id session identifier, can never be 0
391  * @return the ATS_Address
392  */
393 static struct ATS_Address *
394 create_address (const struct GNUNET_PeerIdentity *peer,
395                 const char *plugin_name,
396                 const void *plugin_addr,
397                 size_t plugin_addr_len,
398                 uint32_t local_address_info,
399                 uint32_t session_id)
400 {
401   struct ATS_Address *aa;
402   unsigned int c1;
403   unsigned int c2;
404
405   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
406   aa->peer = *peer;
407   aa->addr_len = plugin_addr_len;
408   aa->addr = &aa[1];
409   memcpy (&aa[1],
410           plugin_addr,
411           plugin_addr_len);
412   aa->plugin = GNUNET_strdup (plugin_name);
413   aa->session_id = session_id;
414   aa->local_address_info = local_address_info;
415
416   for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
417   {
418     aa->atsin[c1].avg_queue_index = 0;
419     for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
420       aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
421   }
422   return aa;
423 }
424
425
426 /**
427  * Closure for #find_address_cb()
428  */
429 struct FindAddressContext
430 {
431   /**
432    * Session Id to look for.
433    */
434   uint32_t session_id;
435
436   /**
437    * Where to store matching address result.
438    */
439   struct ATS_Address *exact_address;
440
441 };
442
443
444 /**
445  * Find session matching given session ID.
446  *
447  * @param cls a `struct FindAddressContext`
448  * @param key peer id
449  * @param value the address to compare with
450  * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
451  */
452 static int
453 find_address_cb (void *cls,
454                  const struct GNUNET_PeerIdentity *key,
455                  void *value)
456 {
457   struct FindAddressContext *fac = cls;
458   struct ATS_Address *aa = value;
459
460   if (aa->session_id == fac->session_id)
461   {
462     fac->exact_address = aa;
463     return GNUNET_NO;
464   }
465   return GNUNET_YES;
466 }
467
468
469 /**
470  * Find the exact address
471  *
472  * @param peer peer
473  * @param session_id session id, can never be 0
474  * @return an ATS_address or NULL
475  */
476 static struct ATS_Address *
477 find_exact_address (const struct GNUNET_PeerIdentity *peer,
478                     uint32_t session_id)
479 {
480   struct FindAddressContext fac;
481
482   fac.exact_address = NULL;
483   fac.session_id = session_id;
484   GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
485                                               peer,
486                                               &find_address_cb, &fac);
487   return fac.exact_address;
488 }
489
490
491 /**
492  * Extract an ATS performance info from an address
493  *
494  * @param address the address
495  * @param type the type to extract in HBO
496  * @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
497  */
498 static int
499 get_performance_info (struct ATS_Address *address, uint32_t type)
500 {
501   int c1;
502   GNUNET_assert(NULL != address);
503
504   if ((NULL == address->atsi) || (0 == address->atsi_count))
505     return GNUNET_ATS_VALUE_UNDEFINED;
506
507   for (c1 = 0; c1 < address->atsi_count; c1++)
508   {
509     if (ntohl (address->atsi[c1].type) == type)
510       return ntohl (address->atsi[c1].value);
511   }
512   return GNUNET_ATS_VALUE_UNDEFINED;
513 }
514
515
516 /**
517  * Add a new address for a peer.
518  *
519  * @param peer peer
520  * @param plugin_name transport plugin name
521  * @param plugin_addr plugin address
522  * @param plugin_addr_len length of the plugin address in @a plugin_addr
523  * @param local_address_info the local address for the address
524  * @param session_id session id, can be 0
525  * @param atsi performance information for this address
526  * @param atsi_count number of performance information contained in @a atsi
527  */
528 void
529 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
530                    const char *plugin_name,
531                    const void *plugin_addr,
532                    size_t plugin_addr_len,
533                    uint32_t local_address_info,
534                    uint32_t session_id,
535                    const struct GNUNET_ATS_Information *atsi,
536                    uint32_t atsi_count)
537 {
538   struct ATS_Address *new_address;
539   struct GNUNET_ATS_Information *atsi_delta;
540   uint32_t atsi_delta_count;
541   uint32_t addr_net;
542
543   if (NULL != find_exact_address (peer, session_id))
544   {
545     GNUNET_break (0);
546     return;
547   }
548
549   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550               "Received `%s' for peer `%s'\n",
551               "ADDRESS ADD",
552               GNUNET_i2s (peer));
553   new_address = create_address (peer,
554                                 plugin_name,
555                                 plugin_addr,
556                                 plugin_addr_len,
557                                 local_address_info,
558                                 session_id);
559   atsi_delta = NULL;
560   disassemble_ats_information (new_address,
561                                atsi, atsi_count,
562                                &atsi_delta,
563                                &atsi_delta_count);
564   GNUNET_free_non_null (atsi_delta);
565   addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
566   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
567     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
568
569   /* Add a new address */
570   new_address->t_added = GNUNET_TIME_absolute_get();
571   new_address->t_last_activity = GNUNET_TIME_absolute_get();
572   GNUNET_assert(GNUNET_OK ==
573                 GNUNET_CONTAINER_multipeermap_put (GSA_addresses,
574                                                    peer,
575                                                    new_address,
576                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
577   update_addresses_stat ();
578   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
579               "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
580               new_address,
581               GNUNET_i2s (peer),
582               plugin_addr_len,
583               session_id,
584               GNUNET_ATS_print_network_type (addr_net));
585
586   /* Tell solver about new address */
587   GAS_plugin_new_address (new_address,
588                           addr_net,
589                           atsi,
590                           atsi_count);
591   /* Notify performance clients about new address */
592   GAS_performance_notify_all_clients (&new_address->peer,
593                                       new_address->plugin,
594                                       new_address->addr,
595                                       new_address->addr_len,
596                                       new_address->active,
597                                       new_address->atsi,
598                                       new_address->atsi_count,
599                                       GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
600                                       GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
601 }
602
603
604 /**
605  * Update an address with new performance information for a peer.
606  *
607  * @param peer peer
608  * @param session_id session id, never 0
609  * @param atsi performance information for this address
610  * @param atsi_count number of performance information contained in @a atsi
611  */
612 void
613 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
614                       uint32_t session_id,
615                       const struct GNUNET_ATS_Information *atsi,
616                       uint32_t atsi_count)
617 {
618   struct ATS_Address *aa;
619   struct GNUNET_ATS_Information *atsi_delta;
620   uint32_t atsi_delta_count;
621
622   /* Get existing address */
623   aa = find_exact_address (peer,
624                            session_id);
625   if (NULL == aa)
626   {
627     GNUNET_break (0);
628     return;
629   }
630   if (NULL == aa->solver_information)
631   {
632     GNUNET_break (0);
633     return;
634   }
635   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636               "Received `%s' for peer `%s' address \n",
637               "ADDRESS UPDATE",
638               GNUNET_i2s (peer),
639               aa);
640
641   /* Update address */
642   aa->t_last_activity = GNUNET_TIME_absolute_get();
643   atsi_delta = NULL;
644   atsi_delta_count = 0;
645   if (GNUNET_YES ==
646       disassemble_ats_information (aa, atsi,
647                                    atsi_count,
648                                    &atsi_delta,
649                                    &atsi_delta_count))
650   {
651     /* Notify performance clients about updated address */
652     GAS_performance_notify_all_clients (&aa->peer,
653                                         aa->plugin,
654                                         aa->addr,
655                                         aa->addr_len,
656                                         aa->active,
657                                         aa->atsi,
658                                         aa->atsi_count,
659                                         GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
660                                         GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
661
662     GAS_plugin_update_address (aa,
663                                atsi,
664                                atsi_count);
665   }
666   GNUNET_free_non_null (atsi_delta);
667 }
668
669
670 /**
671  * Remove an address or just a session for a peer.
672  *
673  * @param peer peer
674  * @param session_id session id, can never be 0
675  */
676 void
677 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
678                        uint32_t session_id)
679 {
680   struct ATS_Address *ea;
681
682   /* Get existing address */
683   ea = find_exact_address (peer,
684                            session_id);
685   if (NULL == ea)
686   {
687     GNUNET_break (0);
688     return;
689   }
690   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691               "Received ADDRESS_DESTROYED for peer `%s' address %p session %u\n",
692               GNUNET_i2s (peer),
693               ea,
694               session_id);
695   free_address (ea);
696 }
697
698
699 /**
700  * Initialize address subsystem. The addresses subsystem manages the addresses
701  * known and current performance information. It has a solver component
702  * responsible for the resource allocation. It tells the solver about changes
703  * and receives updates when the solver changes the resource allocation.
704  */
705 void
706 GAS_addresses_init ()
707 {
708   GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
709   update_addresses_stat ();
710 }
711
712
713 /**
714  * Destroy all addresses iterator
715  *
716  * @param cls NULL
717  * @param key peer identity (unused)
718  * @param value the 'struct ATS_Address' to free
719  * @return #GNUNET_OK (continue to iterate)
720  */
721 static int
722 destroy_all_address_it (void *cls,
723                         const struct GNUNET_PeerIdentity *key,
724                         void *value)
725 {
726   struct ATS_Address *aa = value;
727
728   free_address (aa);
729   return GNUNET_OK;
730 }
731
732
733 /**
734  * Remove all addresses
735  */
736 void
737 GAS_addresses_destroy_all ()
738 {
739   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
740               "Destroying all addresses\n");
741   GAS_plugin_solver_lock ();
742   GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
743                                          &destroy_all_address_it,
744                                          NULL);
745   GAS_plugin_solver_unlock ();
746 }
747
748
749 /**
750  * Shutdown address subsystem.
751  */
752 void
753 GAS_addresses_done ()
754 {
755   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
756              "Shutting down addresses\n");
757   GAS_addresses_destroy_all ();
758   GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
759   GSA_addresses = NULL;
760 }
761
762
763 /**
764  * Closure for #peerinfo_it().
765  */
766 struct PeerInfoIteratorContext
767 {
768   /**
769    * Function to call for each address.
770    */
771   GNUNET_ATS_PeerInfo_Iterator it;
772
773   /**
774    * Closure for @e it.
775    */
776   void *it_cls;
777 };
778
779
780 /**
781  * Iterator to iterate over a peer's addresses
782  *
783  * @param cls a `struct PeerInfoIteratorContext`
784  * @param key the peer id
785  * @param value the `struct ATS_address`
786  * @return #GNUNET_OK to continue
787  */
788 static int
789 peerinfo_it (void *cls,
790              const struct GNUNET_PeerIdentity *key,
791              void *value)
792 {
793   struct PeerInfoIteratorContext *pi_ctx = cls;
794   struct ATS_Address *addr = value;
795
796   pi_ctx->it (pi_ctx->it_cls,
797               &addr->peer,
798               addr->plugin,
799               addr->addr,
800               addr->addr_len,
801               addr->active,
802               addr->atsi, addr->atsi_count,
803               GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
804               GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
805   return GNUNET_OK;
806 }
807
808
809 /**
810  * Return information all peers currently known to ATS
811  *
812  * @param peer the respective peer, NULL for 'all' peers
813  * @param pi_it the iterator to call for every peer
814  * @param pi_it_cls the closure for @a pi_it
815  */
816 void
817 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
818                              GNUNET_ATS_PeerInfo_Iterator pi_it,
819                              void *pi_it_cls)
820 {
821   struct PeerInfoIteratorContext pi_ctx;
822
823   if (NULL == pi_it)
824   {
825     /* does not make sense without callback */
826     GNUNET_break (0);
827     return;
828   }
829   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830               "Returning information for %s from a total of %u known addresses\n",
831               (NULL == peer)
832               ? "all peers"
833               : GNUNET_i2s (peer),
834               (unsigned int) GNUNET_CONTAINER_multipeermap_size (GSA_addresses));
835   pi_ctx.it = pi_it;
836   pi_ctx.it_cls = pi_it_cls;
837   if (NULL == peer)
838     GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
839                                            &peerinfo_it,
840                                            &pi_ctx);
841   else
842     GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
843                                                 peer,
844                                                 &peerinfo_it, &pi_ctx);
845   pi_it (pi_it_cls,
846          NULL, NULL, NULL, 0,
847          GNUNET_NO,
848          NULL, 0,
849          GNUNET_BANDWIDTH_ZERO,
850          GNUNET_BANDWIDTH_ZERO);
851 }
852
853 /* end of gnunet-service-ats_addresses.c */