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