9ef6a38387ec8039577a8c6fe4b68255b523e7d7
[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
220
221 /**
222  * A multihashmap to store all addresses
223  */
224 struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
225
226
227 /**
228  * Value we pass for zero bandwidth.
229  */
230 static const struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
231
232
233 /**
234  * Disassemble ATS information and update performance information in address
235  *
236  * Updates existing information and adds new information
237  *
238  * @param dest destination address
239  * @param update source ATS information
240  * @param update_count number of ATS information in @a update
241  * @param delta_dest ats performance information which were updated
242  *                              including previous value
243  * @param delta_count number of ATS information in the @a delta_dest
244  * @return #GNUNET_YES if address was address updated, GNUNET_NO otherwise
245  */
246 static unsigned int
247 disassemble_ats_information (struct ATS_Address *dest,
248                              const struct GNUNET_ATS_Information *update,
249                              uint32_t update_count,
250                              struct GNUNET_ATS_Information **delta_dest,
251                              uint32_t *delta_count)
252 {
253   int c1;
254   int c2;
255   int found;
256   int change;
257   struct GNUNET_ATS_Information add_atsi[update_count];
258   struct GNUNET_ATS_Information delta_atsi[update_count];
259   struct GNUNET_ATS_Information *tmp_atsi;
260   uint32_t add_atsi_count;
261   uint32_t delta_atsi_count;
262
263   change = GNUNET_NO;
264   add_atsi_count = 0;
265   delta_atsi_count = 0;
266
267   if (0 == update_count)
268     return GNUNET_NO;
269
270   if (NULL == dest->atsi)
271   {
272     /* Create performance information */
273     dest->atsi =
274         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
275     dest->atsi_count = update_count;
276     memcpy (dest->atsi, update,
277         update_count * sizeof(struct GNUNET_ATS_Information));
278     (*delta_dest) =
279         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
280     for (c1 = 0; c1 < update_count; c1++)
281     {
282       (*delta_dest)[c1].type = update[c1].type;
283       (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
284     }
285     (*delta_count) = update_count;
286     return GNUNET_YES;
287   }
288
289   for (c1 = 0; c1 < update_count; c1++)
290   {
291     /* Update existing performance information */
292     found = GNUNET_NO;
293     for (c2 = 0; c2 < dest->atsi_count; c2++)
294     {
295       if (update[c1].type == dest->atsi[c2].type)
296       {
297         if (update[c1].value != dest->atsi[c2].value)
298         {
299           /* Save previous value in delta */
300           delta_atsi[delta_atsi_count] = dest->atsi[c2];
301           delta_atsi_count++;
302           /* Set new value */
303           dest->atsi[c2].value = update[c1].value;
304           change = GNUNET_YES;
305         }
306         found = GNUNET_YES;
307         break;
308       }
309     }
310     if (GNUNET_NO == found)
311     {
312       add_atsi[add_atsi_count] = update[c1];
313       add_atsi_count++;
314       delta_atsi[delta_atsi_count].type = update[c1].type;
315       delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
316       delta_atsi_count++;
317     }
318   }
319
320   if (add_atsi_count > 0)
321   {
322     /* Extend ats performance information */
323
324     tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
325         (sizeof (struct GNUNET_ATS_Information)));
326     memcpy (tmp_atsi, dest->atsi,
327         dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
328     memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
329         add_atsi_count * sizeof(struct GNUNET_ATS_Information));
330     GNUNET_free(dest->atsi);
331     dest->atsi = tmp_atsi;
332     dest->atsi_count = dest->atsi_count + add_atsi_count;
333     change = GNUNET_YES;
334   }
335
336   if (delta_atsi_count > 0)
337   {
338     /* Copy delta */
339     (*delta_dest) =
340         GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
341     memcpy ((*delta_dest), delta_atsi,
342         delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
343     (*delta_count) = delta_atsi_count;
344   }
345
346   return change;
347 }
348
349
350 /**
351  * Free the given address
352  *
353  * @param addr address to destroy
354  */
355 static void
356 free_address (struct ATS_Address *addr)
357 {
358   GNUNET_free (addr->plugin);
359   GNUNET_free_non_null (addr->atsi);
360   GNUNET_free (addr);
361 }
362
363
364 /**
365  * Create a ATS_address with the given information
366  *
367  * @param peer peer
368  * @param plugin_name plugin
369  * @param plugin_addr address
370  * @param plugin_addr_len address length
371  * @param local_address_info additional local info for the address
372  * @param session_id session identifier, can never be 0
373  * @return the ATS_Address
374  */
375 static struct ATS_Address *
376 create_address (const struct GNUNET_PeerIdentity *peer,
377                 const char *plugin_name,
378                 const void *plugin_addr,
379                 size_t plugin_addr_len,
380                 uint32_t local_address_info,
381                 uint32_t session_id)
382 {
383   struct ATS_Address *aa;
384   unsigned int c1;
385   unsigned int c2;
386
387   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
388   aa->peer = *peer;
389   aa->addr_len = plugin_addr_len;
390   aa->addr = &aa[1];
391   memcpy (&aa[1], plugin_addr, plugin_addr_len);
392   aa->plugin = GNUNET_strdup (plugin_name);
393   aa->session_id = session_id;
394   aa->local_address_info = local_address_info;
395
396   for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
397   {
398     aa->atsin[c1].avg_queue_index = 0;
399     for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
400       aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
401   }
402   return aa;
403 }
404
405
406 /**
407  * Closure for #compare_address_it()
408  */
409 struct CompareAddressContext
410 {
411   const struct ATS_Address *search;
412
413   /* exact_address != NULL if address and session is equal */
414   struct ATS_Address *exact_address;
415   /* exact_address != NULL if address and session is 0 */
416   struct ATS_Address *base_address;
417 };
418
419
420 /**
421  * Comapre addresses.
422  *
423  * @param cls a CompareAddressContext containin the source address
424  * @param key peer id
425  * @param value the address to compare with
426  * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
427  */
428 static int
429 compare_address_it (void *cls,
430                     const struct GNUNET_PeerIdentity *key,
431                     void *value)
432 {
433   struct CompareAddressContext *cac = cls;
434   struct ATS_Address *aa = value;
435
436   /* Find an matching exact address:
437    *
438    * Compare by:
439    * aa->addr_len == cac->search->addr_len
440    * aa->plugin == cac->search->plugin
441    * aa->addr == cac->search->addr
442    * aa->session == cac->search->session
443    *
444    * return as exact address
445    */
446   if ((aa->addr_len == cac->search->addr_len)
447       && (0 == strcmp (aa->plugin, cac->search->plugin)))
448   {
449     if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
450         && (aa->session_id == cac->search->session_id))
451       cac->exact_address = aa;
452   }
453
454   /* Find an matching base address:
455    *
456    * Properties:
457    *
458    * aa->session_id == 0
459    *
460    * Compare by:
461    * aa->addr_len == cac->search->addr_len
462    * aa->plugin == cac->search->plugin
463    * aa->addr == cac->search->addr
464    *
465    * return as base address
466    */
467   if ((aa->addr_len == cac->search->addr_len)
468       && (0 == strcmp (aa->plugin, cac->search->plugin)))
469   {
470     if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
471         && (aa->session_id == 0))
472       cac->base_address = aa;
473   }
474
475   /* Find an matching exact address based on session:
476    *
477    * Properties:
478    *
479    * cac->search->addr_len == 0
480    *
481    * Compare by:
482    * aa->plugin == cac->search->plugin
483    * aa->session_id == cac->search->session_id
484    *
485    * return as exact address
486    */
487   if (0 == cac->search->addr_len)
488   {
489     if ((0 == strcmp (aa->plugin, cac->search->plugin))
490         && (aa->session_id == cac->search->session_id))
491       cac->exact_address = aa;
492   }
493
494   if (cac->exact_address == NULL )
495     return GNUNET_YES; /* Continue iteration to find exact address */
496   else
497     return GNUNET_NO; /* Stop iteration since we have an exact address */
498 }
499
500
501 /**
502  * Find an existing equivalent address record.
503  * Compares by peer identity and network address OR by session ID
504  * (one of the two must match).
505  *
506  * @param peer peer to lookup addresses for
507  * @param addr existing address record
508  * @return existing address record, NULL for none
509  */
510 struct ATS_Address *
511 find_equivalent_address (const struct GNUNET_PeerIdentity *peer,
512                          const struct ATS_Address *addr)
513 {
514   struct CompareAddressContext cac;
515
516   cac.exact_address = NULL;
517   cac.base_address = NULL;
518   cac.search = addr;
519   GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
520                                               peer,
521                                               &compare_address_it, &cac);
522
523   if (NULL == cac.exact_address)
524     return cac.base_address;
525   return cac.exact_address;
526 }
527
528
529 /**
530  * Closure for #find_address_cb()
531  */
532 struct FindAddressContext
533 {
534   /**
535    * Session Id to look for.
536    */
537   uint32_t session_id;
538
539   /**
540    * Where to store matching address result.
541    */
542   struct ATS_Address *exact_address;
543
544 };
545
546
547 /**
548  * Find session matching given session ID.
549  *
550  * @param cls a `struct FindAddressContext`
551  * @param key peer id
552  * @param value the address to compare with
553  * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
554  */
555 static int
556 find_address_cb (void *cls,
557                  const struct GNUNET_PeerIdentity *key,
558                  void *value)
559 {
560   struct FindAddressContext *fac = cls;
561   struct ATS_Address *aa = value;
562
563   if (aa->session_id == fac->session_id)
564   {
565     fac->exact_address = aa;
566     return GNUNET_NO;
567   }
568   return GNUNET_YES;
569 }
570
571
572 /**
573  * Find the exact address
574  *
575  * @param peer peer
576  * @param session_id session id, can never be 0
577  * @return an ATS_address or NULL
578  */
579 static struct ATS_Address *
580 find_exact_address (const struct GNUNET_PeerIdentity *peer,
581                     uint32_t session_id)
582 {
583   struct FindAddressContext fac;
584
585   fac.exact_address = NULL;
586   fac.session_id = session_id;
587   GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
588                                               peer,
589                                               &find_address_cb, &fac);
590   return fac.exact_address;
591 }
592
593
594 /**
595  * Extract an ATS performance info from an address
596  *
597  * @param address the address
598  * @param type the type to extract in HBO
599  * @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
600  */
601 static int
602 get_performance_info (struct ATS_Address *address, uint32_t type)
603 {
604   int c1;
605   GNUNET_assert(NULL != address);
606
607   if ((NULL == address->atsi) || (0 == address->atsi_count))
608     return GNUNET_ATS_VALUE_UNDEFINED;
609
610   for (c1 = 0; c1 < address->atsi_count; c1++)
611   {
612     if (ntohl (address->atsi[c1].type) == type)
613       return ntohl (address->atsi[c1].value);
614   }
615   return GNUNET_ATS_VALUE_UNDEFINED;
616 }
617
618
619 /**
620  * Add a new address for a peer.
621  *
622  * @param peer peer
623  * @param plugin_name transport plugin name
624  * @param plugin_addr plugin address
625  * @param plugin_addr_len length of the plugin address in @a plugin_addr
626  * @param local_address_info the local address for the address
627  * @param session_id session id, can be 0
628  * @param atsi performance information for this address
629  * @param atsi_count number of performance information contained in @a atsi
630  */
631 void
632 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
633                    const char *plugin_name,
634                    const void *plugin_addr,
635                    size_t plugin_addr_len,
636                    uint32_t local_address_info,
637                    uint32_t session_id,
638                    const struct GNUNET_ATS_Information *atsi,
639                    uint32_t atsi_count)
640 {
641   struct ATS_Address *new_address;
642   struct ATS_Address *existing_address;
643   struct GNUNET_ATS_Information *atsi_delta;
644   uint32_t atsi_delta_count;
645   uint32_t addr_net;
646
647   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648               "Received `%s' for peer `%s'\n",
649               "ADDRESS ADD",
650               GNUNET_i2s (peer));
651   new_address = create_address (peer,
652                                 plugin_name,
653                                 plugin_addr,
654                                 plugin_addr_len,
655                                 local_address_info,
656                                 session_id);
657   atsi_delta = NULL;
658   disassemble_ats_information (new_address,
659                                atsi, atsi_count,
660                                &atsi_delta,
661                                &atsi_delta_count);
662   GNUNET_free_non_null (atsi_delta);
663   addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
664   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
665     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
666
667   /* Get existing address or address with session == 0 */
668   existing_address = find_equivalent_address (peer, new_address);
669   if (NULL != existing_address)
670   {
671     GNUNET_break (0);
672     GNUNET_free(new_address->plugin);
673     GNUNET_free_non_null(new_address->atsi);
674     GNUNET_free(new_address);
675     return;
676   }
677   /* Add a new address */
678   new_address->t_added = GNUNET_TIME_absolute_get();
679   new_address->t_last_activity = GNUNET_TIME_absolute_get();
680   GNUNET_assert(GNUNET_OK ==
681                 GNUNET_CONTAINER_multipeermap_put (GSA_addresses,
682                                                    peer,
683                                                    new_address,
684                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
685   
686   GNUNET_STATISTICS_set (GSA_stats,
687                          "# addresses",
688                          GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
689                          GNUNET_NO);
690   
691   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
692               "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
693               new_address,
694               GNUNET_i2s (peer),
695               plugin_addr_len,
696               session_id,
697               GNUNET_ATS_print_network_type (addr_net));
698   
699   /* Tell solver about new address */
700   GAS_plugin_new_address (new_address,
701                           addr_net,
702                           atsi,
703                           atsi_count);
704   /* Notify performance clients about new address */
705   GAS_performance_notify_all_clients (&new_address->peer, 
706                                       new_address->plugin,
707                                       new_address->addr,
708                                       new_address->addr_len, 
709                                       new_address->active,
710                                       new_address->atsi, 
711                                       new_address->atsi_count,
712                                       GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
713                                       GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
714 }
715
716
717 /**
718  * Update an address with new performance information for a peer.
719  *
720  * @param peer peer
721  * @param session_id session id, never 0
722  * @param atsi performance information for this address
723  * @param atsi_count number of performance information contained in @a atsi
724  */
725 void
726 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
727                       uint32_t session_id,
728                       const struct GNUNET_ATS_Information *atsi,
729                       uint32_t atsi_count)
730 {
731   struct ATS_Address *aa;
732   struct GNUNET_ATS_Information *atsi_delta;
733   uint32_t atsi_delta_count;
734
735   /* Get existing address */
736   aa = find_exact_address (peer,
737                            session_id);
738   if (NULL == aa)
739   {
740     GNUNET_break (0);
741     return;
742   }
743   if (NULL == aa->solver_information)
744   {
745     GNUNET_break (0);
746     return;
747   }
748   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749               "Received `%s' for peer `%s' address \n",
750               "ADDRESS UPDATE",
751               GNUNET_i2s (peer),
752               aa);
753
754   /* Update address */
755   aa->t_last_activity = GNUNET_TIME_absolute_get();
756   atsi_delta = NULL;
757   atsi_delta_count = 0;
758   if (GNUNET_YES ==
759       disassemble_ats_information (aa, atsi,
760                                    atsi_count,
761                                    &atsi_delta,
762                                    &atsi_delta_count))
763   {
764     /* Notify performance clients about updated address */
765     GAS_performance_notify_all_clients (&aa->peer, 
766                                         aa->plugin,
767                                         aa->addr,
768                                         aa->addr_len, 
769                                         aa->active, 
770                                         aa->atsi,
771                                         aa->atsi_count,
772                                         GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
773                                         GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
774
775     GAS_plugin_update_address (aa,
776                                atsi,
777                                atsi_count);
778   }
779   GNUNET_free_non_null (atsi_delta);
780 }
781
782
783 /**
784  * Remove an address or just a session for a peer.
785  *
786  * @param peer peer
787  * @param session_id session id, can never be 0
788  */
789 void
790 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
791                        uint32_t session_id)
792 {
793   struct ATS_Address *ea;
794
795   /* Get existing address */
796   ea = find_exact_address (peer,
797                            session_id);
798   if (NULL == ea)
799   {
800     GNUNET_break (0);
801     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
802                 "Tried to destroy unknown address for peer `%s' session id %u\n",
803                 GNUNET_i2s (peer),
804                 session_id);
805     return;
806   }
807
808   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809               "Received `%s' for peer `%s' address %p session %u\n",
810               "ADDRESS DESTROYED",
811               GNUNET_i2s (peer),
812               ea,
813               session_id);
814   GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
815                                         peer,
816                                         ea);
817   GAS_plugin_delete_address (ea);
818   GAS_performance_notify_all_clients (peer,
819                                       ea->plugin,
820                                       ea->addr,
821                                       ea->addr_len,
822                                       GNUNET_SYSERR,
823                                       NULL, 0,
824                                       zero_bw,
825                                       zero_bw);
826   free_address (ea);
827   GNUNET_STATISTICS_set (GSA_stats,
828                          "# addresses",
829                          GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
830                          GNUNET_NO);
831 }
832
833
834
835
836
837 /**
838  * Initialize address subsystem. The addresses subsystem manages the addresses
839  * known and current performance information. It has a solver component
840  * responsible for the resource allocation. It tells the solver about changes
841  * and receives updates when the solver changes the resource allocation.
842  */
843 void
844 GAS_addresses_init ()
845 {
846   /* Initialize the addresses database */
847   GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
848   GNUNET_STATISTICS_set (GSA_stats,
849                          "# addresses",
850                          GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
851                          GNUNET_NO);
852 }
853
854
855 /**
856  * Destroy all addresses iterator
857  *
858  * @param cls NULL
859  * @param key peer identity (unused)
860  * @param value the 'struct ATS_Address' to free
861  * @return #GNUNET_OK (continue to iterate)
862  */
863 static int
864 destroy_all_address_it (void *cls,
865                         const struct GNUNET_PeerIdentity *key,
866                         void *value)
867 {
868   struct ATS_Address *aa = value;
869
870   /* Remove */
871   GNUNET_assert (GNUNET_YES ==
872                  GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
873                                                        key,
874                                                        value));
875   /* Notify */
876   GAS_plugin_delete_address (aa);
877   /* Destroy */
878   GAS_performance_notify_all_clients (&aa->peer,
879                                       aa->plugin,
880                                       aa->addr,
881                                       aa->addr_len,
882                                       GNUNET_NO,
883                                       NULL, 0,
884                                       zero_bw,
885                                       zero_bw);
886   free_address (aa);
887   return GNUNET_OK;
888 }
889
890
891 /**
892  * Remove all addresses
893  */
894 void
895 GAS_addresses_destroy_all ()
896 {
897   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
898               "Destroying all addresses\n");
899   GAS_plugin_solver_lock ();
900   if (NULL != GSA_addresses)
901     GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
902                                            &destroy_all_address_it,
903                                            NULL);
904   GAS_plugin_solver_unlock ();
905 }
906
907
908 /**
909  * Shutdown address subsystem.
910  */
911 void
912 GAS_addresses_done ()
913 {
914   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
915              "Shutting down addresses\n");
916   GAS_addresses_destroy_all ();
917   GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
918   GSA_addresses = NULL;
919 }
920
921
922 /**
923  * Closure for #peerinfo_it().
924  */
925 struct PeerInfoIteratorContext
926 {
927   /**
928    * Function to call for each address.
929    */
930   GNUNET_ATS_PeerInfo_Iterator it;
931
932   /**
933    * Closure for @e it.
934    */
935   void *it_cls;
936 };
937
938
939 /**
940  * Iterator to iterate over a peer's addresses
941  *
942  * @param cls a `struct PeerInfoIteratorContext`
943  * @param key the peer id
944  * @param value the `struct ATS_address`
945  * @return #GNUNET_OK to continue
946  */
947 static int
948 peerinfo_it (void *cls,
949              const struct GNUNET_PeerIdentity *key,
950              void *value)
951 {
952   struct PeerInfoIteratorContext *pi_ctx = cls;
953   struct ATS_Address *addr = value;
954
955   pi_ctx->it (pi_ctx->it_cls,
956               &addr->peer,
957               addr->plugin,
958               addr->addr,
959               addr->addr_len,
960               addr->active,
961               addr->atsi, addr->atsi_count,
962               GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
963               GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
964   return GNUNET_OK;
965 }
966
967
968 /**
969  * Return information all peers currently known to ATS
970  *
971  * @param peer the respective peer, NULL for 'all' peers
972  * @param pi_it the iterator to call for every peer
973  * @param pi_it_cls the closure for @a pi_it
974  */
975 void
976 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
977                              GNUNET_ATS_PeerInfo_Iterator pi_it,
978                              void *pi_it_cls)
979 {
980   struct PeerInfoIteratorContext pi_ctx;
981
982   if (NULL == pi_it)
983   {
984     /* does not make sense without callback */
985     GNUNET_break (0);
986     return;
987   }
988   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989               "Returning information for %s from a total of %u known addresses\n",
990               (NULL == peer)
991               ? "all peers"
992               : GNUNET_i2s (peer),
993               (unsigned int) GNUNET_CONTAINER_multipeermap_size (GSA_addresses));
994   pi_ctx.it = pi_it;
995   pi_ctx.it_cls = pi_it_cls;
996   if (NULL == peer)
997     GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
998                                            &peerinfo_it,
999                                            &pi_ctx);
1000   else
1001     GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
1002                                                 peer,
1003                                                 &peerinfo_it, &pi_ctx);
1004   pi_it (pi_it_cls,
1005          NULL, NULL, NULL, 0,
1006          GNUNET_NO,
1007          NULL, 0,
1008          zero_bw,
1009          zero_bw);
1010 }
1011
1012 /* end of gnunet-service-ats_addresses.c */