simplify logic
[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-service-ats_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_plugins.h"
31
32
33 /**
34  * NOTE: Do not change this documentation. This documentation is based on
35  * gnunet.org:/vcs/fsnsg/2014-p2p-ats.git/tech-doku/ats-tech-guide.tex
36  * use build_txt.sh to generate plaintext output
37  *
38  *   1 ATS addresses : ATS address management
39  *
40  *    This ATS addresses ("addresses") component manages the addresses known to
41  *    ATS service and suggests addresses to transport service when it is
42  *    interested in address suggestion for a peer. ATS addresses also
43  *    instantiates the bandwidth assignment mechanism (solver), notifies it
44  *    about changes to addresses and forwards changes to bandwidth assignments
45  *    to transport, depending if transport is interested in this change.
46  *
47  *     1.1 Input data
48  *
49  *       1.1.1 Addresses
50  *
51  *    Addresses are added by specifying peer ID, plugin, address, address length
52  *    and session, if available. ATS information can be specified if available.
53  *
54  *       1.1.2 Networks
55  *
56  *    ATS specifies a fix set of networks an address can belong to. For each
57  *    network an inbound and outbound quota will be specified. The available
58  *    networks and addtional helper varaibles are defined in
59  *    gnunet_ats_service.h. At the moment 5 networks are defined:
60  *      * GNUNET_ATS_NET_UNSPECIFIED
61  *      * GNUNET_ATS_NET_LOOPBACK
62  *      * GNUNET_ATS_NET_LAN
63  *      * GNUNET_ATS_NET_WAN
64  *      * GNUNET_ATS_NET_WLAN
65  *
66  *    The total number of networks defined is stored in
67  *    GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
68  *    initializer for an int array, while GNUNET_ATS_NetworkType is an
69  *    initializer for a char array containing a string description of all
70  *    networks
71  *
72  *       1.1.3 Quotas
73  *
74  *    An inbound and outbound quota for each of the networks mentioned in 1.1.2
75  *    is loaded from ats configuration during initialization. This quota defines
76  *    to total amount of inbound and outbound traffic allowed for a specific
77  *    network. The configuration values used are in section ats:
78  *      * "NETWORK"_QUOTA_IN = <value>
79  *      * "NETWORK"_QUOTA_IN = <value>
80  *
81  *    You can specify quotas by setting the <value> to a:
82  *      * unrestricted: unlimited
83  *      * number of bytes: e.g. 10240
84  *      * fancy value: e.g. 64 Kib
85  *
86  *    unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
87  *    the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
88  *    are:
89  *      * GNUNET_ATS_DefaultBandwidth: 65536
90  *      * GNUNET_ATS_MaxBandwidth: UINT32_MAX
91  *      * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
92  *
93  *    Details of loading quotas and default values will be described on
94  *
95  *       1.1.4 Preference values
96  *
97  *     1.2 Data structures used
98  *
99  *    Addresse uses struct ATS_Address for each address. The structs are stored
100  *    in a linked list and provides a pointer void *solver_information for the
101  *    solver to store address specific information. It provides the int values
102  *    active which is set to GNUNET_YES if the address is select for transport
103  *    use and used, representing that transport service is actively using this
104  *    address. Address information are stored in peer, addr, addr_len, plugin.
105  *
106  *     1.3 Initialization
107  *
108  *    During initialization a hashmap to store addresses is created. The quotas
109  *    for all networks defined for ATS are loaded from configuration. For each
110  *    network first the logic will check if the string
111  *    GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
112  *    the configured value as a fancy size and if this fails it will try to use
113  *    it as a value_number. If no configuration value is found it will assign
114  *    GNUNET_ATS_DefaultBandwidth. The most important step is to load the
115  *    configured solver using configuration "[ats]:MODE". Current solvers are
116  *    MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
117  *
118  *     1.4 Solver API
119  *
120  *    Solver functions:
121  *      * s_init: init the solver with required information
122  *      * s_add: add a new address
123  *      * s_update: update ATS values or session for an address
124  *      * s_get: get prefered address for a peer
125  *      * s_del: delete an address
126  *      * s_pref: change preference value for a peer
127  *      * s_done: shutdown solver
128  *
129  *    Callbacks: addresses provides a bandwidth_changed_cb callback to the
130  *    solver which is called when bandwidth assigned to peer has changed
131  *
132  *     1.5 Shutdown
133  *
134  *    During shutdown all addresses are freed and the solver told to shutdown
135  *
136  *     1.6 Addresses and sessions
137  *
138  *    Addresses consist of the address itself and a numerical session. When a
139  *    new address without a session is added it has no session, so it gets
140  *    session 0 assigned. When an address with a session is added and an address
141  *    object with session 0 is found, this object is updated with the session
142  *    otherwise a new address object with this session assigned is created.
143  *
144  *       1.6.1 Terminology
145  *
146  *    Addresses a1,a2 with session s1, s2 are "exact" if:
147  *    (a1 == a2)&&(s1 == s2)
148  *    Addresses a1,a2 with session s1, s2 are "equivalent" if:
149  *    (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
150  *
151  *     1.7 Address management
152  *
153  *    Transport service notifies ATS about changes to the addresses known to
154  *    him.
155  *
156  *       1.7.1 Adding an address
157  *
158  *    When transport learns a new address it tells ATS and ATS is telling
159  *    addresses about it using GAS_address_add. If not known to addresses it
160  *    creates a new address object and calls solver's s_add. ATS information are
161  *    deserialized and solver is notified about the session and ATS information
162  *    using s_update.
163  *
164  *       1.7.2 Updating an address
165  *
166  *    Addresses does an lookup up for the existing address with the given
167  *    session. If disassembles included ATS information and notifies the solver
168  *    using s_update about the update.
169  *
170  *       1.7.3 Deleting an address
171  *
172  *    Addresses does an lookup for the exact address and session and if removes
173  *    this address. If session != 0 the session is set to 0 and the address is
174  *    kept. If session == 0, the addresses is removed.
175  *
176  *       1.7.4 Requesting an address suggestion
177  *
178  *    The address client issues a request address message to be notified about
179  *    address suggestions for a specific peer. Addresses asks the solver with
180  *    s_get. If no address is available, it will not send a response, otherwise
181  *    it will respond with the choosen address.
182  *
183  *       1.7.5 Address suggestions
184  *
185  *    Addresses will notify the client automatically on any bandwidth_changed_cb
186  *    by the solver if a address suggestion request is pending. If no address is
187  *    available it will not respond at all If the client is not interested
188  *    anymore, it has to cancel the address suggestion request.
189  *
190  *       1.7.6 Address lifecycle
191  *
192  *      * (add address)
193  *      * (updated address)
194  *      * (delete address)
195  *
196  *     1.8 Bandwidth assignment
197  *
198  *    The addresses are used to perform resource allocation operations. ATS
199  *    addresses takes care of instantiating the solver configured and notifies
200  *    the respective solver about address changes and receives changes to the
201  *    bandwidth assignment from the solver. The current bandwidth assignment is
202  *    sent to transport. The specific solvers will be described in the specific
203  *    section.
204  *
205  *     1.9 Changing peer preferences
206  *
207  *    The bandwidth assigned to a peer can be influenced by setting a preference
208  *    for a peer. The prefernce will be given to to the solver with s_pref which
209  *    has to take care of the preference value
210  */
211
212
213 /**
214  * A multihashmap to store all addresses
215  */
216 struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
217
218
219 /**
220  * Update statistic on number of addresses.
221  */
222 static void
223 update_addresses_stat ()
224 {
225   GNUNET_STATISTICS_set (GSA_stats,
226                          "# addresses",
227                          GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
228                          GNUNET_NO);
229 }
230
231
232 /**
233  * Disassemble ATS information and update performance information in address
234  *
235  * Updates existing information and adds new information
236  *
237  * @param dest destination address
238  * @param update source ATS information
239  * @param update_count number of ATS information in @a update
240  * @param delta_dest ats performance information which were updated
241  *                              including previous value
242  * @param delta_count number of ATS information in the @a delta_dest
243  * @return #GNUNET_YES if address was address updated, GNUNET_NO otherwise
244  */
245 static unsigned int
246 disassemble_ats_information (struct ATS_Address *dest,
247                              const struct GNUNET_ATS_Information *update,
248                              uint32_t update_count,
249                              struct GNUNET_ATS_Information **delta_dest,
250                              uint32_t *delta_count)
251 {
252   int c1;
253   int c2;
254   int found;
255   int change;
256   struct GNUNET_ATS_Information add_atsi[update_count];
257   struct GNUNET_ATS_Information delta_atsi[update_count];
258   struct GNUNET_ATS_Information *tmp_atsi;
259   uint32_t add_atsi_count;
260   uint32_t delta_atsi_count;
261
262   change = GNUNET_NO;
263   add_atsi_count = 0;
264   delta_atsi_count = 0;
265
266   if (0 == update_count)
267     return GNUNET_NO;
268
269   if (NULL == dest->atsi)
270   {
271     /* Create performance information */
272     dest->atsi =
273         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
274     dest->atsi_count = update_count;
275     memcpy (dest->atsi,
276             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_CONTAINER_multipeermap_remove (GSA_addresses,
359                                         &addr->peer,
360                                         addr);
361   update_addresses_stat ();
362   GAS_plugin_delete_address (addr);
363   GAS_performance_notify_all_clients (&addr->peer,
364                                       addr->plugin,
365                                       addr->addr,
366                                       addr->addr_len,
367                                       GNUNET_NO,
368                                       NULL, 0,
369                                       GNUNET_BANDWIDTH_ZERO,
370                                       GNUNET_BANDWIDTH_ZERO);
371   GNUNET_free (addr->plugin);
372   GNUNET_free_non_null (addr->atsi);
373   GNUNET_free (addr);
374 }
375
376
377 /**
378  * Create a ATS_address with the given information
379  *
380  * @param peer peer
381  * @param plugin_name plugin
382  * @param plugin_addr address
383  * @param plugin_addr_len address length
384  * @param local_address_info additional local info for the address
385  * @param session_id session identifier, can never be 0
386  * @return the ATS_Address
387  */
388 static struct ATS_Address *
389 create_address (const struct GNUNET_PeerIdentity *peer,
390                 const char *plugin_name,
391                 const void *plugin_addr,
392                 size_t plugin_addr_len,
393                 uint32_t local_address_info,
394                 uint32_t session_id)
395 {
396   struct ATS_Address *aa;
397   unsigned int c1;
398   unsigned int c2;
399
400   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
401   aa->peer = *peer;
402   aa->addr_len = plugin_addr_len;
403   aa->addr = &aa[1];
404   memcpy (&aa[1],
405           plugin_addr,
406           plugin_addr_len);
407   aa->plugin = GNUNET_strdup (plugin_name);
408   aa->session_id = session_id;
409   aa->local_address_info = local_address_info;
410
411   for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
412   {
413     aa->atsin[c1].avg_queue_index = 0;
414     for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
415       aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
416   }
417   return aa;
418 }
419
420
421 /**
422  * Closure for #find_address_cb()
423  */
424 struct FindAddressContext
425 {
426   /**
427    * Session Id to look for.
428    */
429   uint32_t session_id;
430
431   /**
432    * Where to store matching address result.
433    */
434   struct ATS_Address *exact_address;
435
436 };
437
438
439 /**
440  * Find session matching given session ID.
441  *
442  * @param cls a `struct FindAddressContext`
443  * @param key peer id
444  * @param value the address to compare with
445  * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
446  */
447 static int
448 find_address_cb (void *cls,
449                  const struct GNUNET_PeerIdentity *key,
450                  void *value)
451 {
452   struct FindAddressContext *fac = cls;
453   struct ATS_Address *aa = value;
454
455   if (aa->session_id == fac->session_id)
456   {
457     fac->exact_address = aa;
458     return GNUNET_NO;
459   }
460   return GNUNET_YES;
461 }
462
463
464 /**
465  * Find the exact address
466  *
467  * @param peer peer
468  * @param session_id session id, can never be 0
469  * @return an ATS_address or NULL
470  */
471 static struct ATS_Address *
472 find_exact_address (const struct GNUNET_PeerIdentity *peer,
473                     uint32_t session_id)
474 {
475   struct FindAddressContext fac;
476
477   fac.exact_address = NULL;
478   fac.session_id = session_id;
479   GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
480                                               peer,
481                                               &find_address_cb, &fac);
482   return fac.exact_address;
483 }
484
485
486 /**
487  * Extract an ATS performance info from an address
488  *
489  * @param address the address
490  * @param type the type to extract in HBO
491  * @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
492  */
493 static int
494 get_performance_info (struct ATS_Address *address,
495                       uint32_t type)
496 {
497   uint32_t c1;
498
499   if ((NULL == address->atsi) || (0 == address->atsi_count))
500     return GNUNET_ATS_VALUE_UNDEFINED;
501
502   for (c1 = 0; c1 < address->atsi_count; c1++)
503   {
504     if (ntohl (address->atsi[c1].type) == type)
505       return ntohl (address->atsi[c1].value);
506   }
507   return GNUNET_ATS_VALUE_UNDEFINED;
508 }
509
510
511 /**
512  * Add a new address for a peer.
513  *
514  * @param peer peer
515  * @param plugin_name transport plugin name
516  * @param plugin_addr plugin address
517  * @param plugin_addr_len length of the plugin address in @a plugin_addr
518  * @param local_address_info the local address for the address
519  * @param session_id session id, can be 0
520  * @param atsi performance information for this address
521  * @param atsi_count number of performance information contained in @a atsi
522  */
523 void
524 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
525                    const char *plugin_name,
526                    const void *plugin_addr,
527                    size_t plugin_addr_len,
528                    uint32_t local_address_info,
529                    uint32_t session_id,
530                    const struct GNUNET_ATS_Information *atsi,
531                    uint32_t atsi_count)
532 {
533   struct ATS_Address *new_address;
534   struct GNUNET_ATS_Information *atsi_delta;
535   uint32_t atsi_delta_count;
536   uint32_t addr_net;
537
538   if (NULL != find_exact_address (peer, session_id))
539   {
540     GNUNET_break (0);
541     return;
542   }
543   new_address = create_address (peer,
544                                 plugin_name,
545                                 plugin_addr,
546                                 plugin_addr_len,
547                                 local_address_info,
548                                 session_id);
549   atsi_delta = NULL;
550   disassemble_ats_information (new_address,
551                                atsi, atsi_count,
552                                &atsi_delta,
553                                &atsi_delta_count);
554   GNUNET_free_non_null (atsi_delta);
555   addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
556   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
557     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
558
559   /* Add a new address */
560   new_address->t_added = GNUNET_TIME_absolute_get();
561   new_address->t_last_activity = GNUNET_TIME_absolute_get();
562   GNUNET_assert(GNUNET_OK ==
563                 GNUNET_CONTAINER_multipeermap_put (GSA_addresses,
564                                                    peer,
565                                                    new_address,
566                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
567   update_addresses_stat ();
568   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
569               "Adding new address for peer `%s' slot %u\n",
570               GNUNET_i2s (peer),
571               session_id);
572   /* Tell solver about new address */
573   GAS_plugin_new_address (new_address,
574                           addr_net,
575                           atsi,
576                           atsi_count);
577   /* Notify performance clients about new address */
578   GAS_performance_notify_all_clients (&new_address->peer,
579                                       new_address->plugin,
580                                       new_address->addr,
581                                       new_address->addr_len,
582                                       new_address->active,
583                                       new_address->atsi,
584                                       new_address->atsi_count,
585                                       GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
586                                       GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
587 }
588
589
590 /**
591  * Update an address with new performance information for a peer.
592  *
593  * @param peer peer
594  * @param session_id session id, never 0
595  * @param atsi performance information for this address
596  * @param atsi_count number of performance information contained in @a atsi
597  */
598 void
599 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
600                       uint32_t session_id,
601                       const struct GNUNET_ATS_Information *atsi,
602                       uint32_t atsi_count)
603 {
604   struct ATS_Address *aa;
605   struct GNUNET_ATS_Information *atsi_delta;
606   uint32_t atsi_delta_count;
607
608   /* Get existing address */
609   aa = find_exact_address (peer,
610                            session_id);
611   if (NULL == aa)
612   {
613     GNUNET_break (0);
614     return;
615   }
616   if (NULL == aa->solver_information)
617   {
618     GNUNET_break (0);
619     return;
620   }
621   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622               "Received ADDRESS_UPDATE for peer `%s' slot %u\n",
623               GNUNET_i2s (peer),
624               (unsigned int) session_id);
625
626   /* Update address */
627   aa->t_last_activity = GNUNET_TIME_absolute_get();
628   atsi_delta = NULL;
629   atsi_delta_count = 0;
630   if (GNUNET_YES ==
631       disassemble_ats_information (aa, atsi,
632                                    atsi_count,
633                                    &atsi_delta,
634                                    &atsi_delta_count))
635   {
636     /* Notify performance clients about updated address */
637     GAS_performance_notify_all_clients (&aa->peer,
638                                         aa->plugin,
639                                         aa->addr,
640                                         aa->addr_len,
641                                         aa->active,
642                                         aa->atsi,
643                                         aa->atsi_count,
644                                         GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
645                                         GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
646
647     GAS_plugin_update_address (aa,
648                                atsi,
649                                atsi_count);
650   }
651   GNUNET_free_non_null (atsi_delta);
652 }
653
654
655 /**
656  * Remove an address for a peer.
657  *
658  * @param peer peer
659  * @param session_id session id, can never be 0
660  */
661 void
662 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
663                        uint32_t session_id)
664 {
665   struct ATS_Address *ea;
666
667   /* Get existing address */
668   ea = find_exact_address (peer,
669                            session_id);
670   if (NULL == ea)
671   {
672     GNUNET_break (0);
673     return;
674   }
675   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676               "Received ADDRESS_DESTROYED for peer `%s' session %u\n",
677               GNUNET_i2s (peer),
678               session_id);
679   free_address (ea);
680 }
681
682
683 /**
684  * Initialize address subsystem. The addresses subsystem manages the addresses
685  * known and current performance information. It has a solver component
686  * responsible for the resource allocation. It tells the solver about changes
687  * and receives updates when the solver changes the resource allocation.
688  *
689  * @param server handle to our server
690  */
691 void
692 GAS_addresses_init (struct GNUNET_SERVER_Handle *server)
693 {
694   GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
695   update_addresses_stat ();
696 }
697
698
699 /**
700  * Destroy all addresses iterator
701  *
702  * @param cls NULL
703  * @param key peer identity (unused)
704  * @param value the 'struct ATS_Address' to free
705  * @return #GNUNET_OK (continue to iterate)
706  */
707 static int
708 destroy_all_address_it (void *cls,
709                         const struct GNUNET_PeerIdentity *key,
710                         void *value)
711 {
712   struct ATS_Address *aa = value;
713
714   free_address (aa);
715   return GNUNET_OK;
716 }
717
718
719 /**
720  * Remove all addresses
721  */
722 void
723 GAS_addresses_destroy_all ()
724 {
725   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
726               "Destroying all addresses\n");
727   GAS_plugin_solver_lock ();
728   GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
729                                          &destroy_all_address_it,
730                                          NULL);
731   GAS_plugin_solver_unlock ();
732 }
733
734
735 /**
736  * Shutdown address subsystem.
737  */
738 void
739 GAS_addresses_done ()
740 {
741   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
742              "Shutting down addresses\n");
743   GAS_addresses_destroy_all ();
744   GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
745   GSA_addresses = NULL;
746 }
747
748
749 /**
750  * Closure for #peerinfo_it().
751  */
752 struct PeerInfoIteratorContext
753 {
754   /**
755    * Function to call for each address.
756    */
757   GNUNET_ATS_PeerInfo_Iterator it;
758
759   /**
760    * Closure for @e it.
761    */
762   void *it_cls;
763 };
764
765
766 /**
767  * Iterator to iterate over a peer's addresses
768  *
769  * @param cls a `struct PeerInfoIteratorContext`
770  * @param key the peer id
771  * @param value the `struct ATS_address`
772  * @return #GNUNET_OK to continue
773  */
774 static int
775 peerinfo_it (void *cls,
776              const struct GNUNET_PeerIdentity *key,
777              void *value)
778 {
779   struct PeerInfoIteratorContext *pi_ctx = cls;
780   struct ATS_Address *addr = value;
781
782   pi_ctx->it (pi_ctx->it_cls,
783               &addr->peer,
784               addr->plugin,
785               addr->addr,
786               addr->addr_len,
787               addr->active,
788               addr->atsi, addr->atsi_count,
789               GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
790               GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
791   return GNUNET_OK;
792 }
793
794
795 /**
796  * Return information all peers currently known to ATS
797  *
798  * @param peer the respective peer, NULL for 'all' peers
799  * @param pi_it the iterator to call for every peer
800  * @param pi_it_cls the closure for @a pi_it
801  */
802 void
803 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
804                              GNUNET_ATS_PeerInfo_Iterator pi_it,
805                              void *pi_it_cls)
806 {
807   struct PeerInfoIteratorContext pi_ctx;
808
809   if (NULL == pi_it)
810   {
811     /* does not make sense without callback */
812     GNUNET_break (0);
813     return;
814   }
815   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816               "Returning information for %s from a total of %u known addresses\n",
817               (NULL == peer)
818               ? "all peers"
819               : GNUNET_i2s (peer),
820               (unsigned int) GNUNET_CONTAINER_multipeermap_size (GSA_addresses));
821   pi_ctx.it = pi_it;
822   pi_ctx.it_cls = pi_it_cls;
823   if (NULL == peer)
824     GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
825                                            &peerinfo_it,
826                                            &pi_ctx);
827   else
828     GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
829                                                 peer,
830                                                 &peerinfo_it, &pi_ctx);
831   pi_it (pi_it_cls,
832          NULL, NULL, NULL, 0,
833          GNUNET_NO,
834          NULL, 0,
835          GNUNET_BANDWIDTH_ZERO,
836          GNUNET_BANDWIDTH_ZERO);
837 }
838
839
840 /**
841  * Information we need for the callbacks to return a list of addresses
842  * back to the client.
843  */
844 struct AddressIteration
845 {
846   /**
847    * Actual handle to the client.
848    */
849   struct GNUNET_SERVER_Client *client;
850
851   /**
852    * Are we sending all addresses, or only those that are active?
853    */
854   int all;
855
856   /**
857    * Which ID should be included in the response?
858    */
859   uint32_t id;
860
861 };
862
863
864 /**
865  * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
866  * given address details to the client identified in @a ai.
867  *
868  * @param ai our address information context (identifies the client)
869  * @param id the peer id this address is for
870  * @param plugin_name name of the plugin that supports this address
871  * @param plugin_addr address
872  * @param plugin_addr_len length of @a plugin_addr
873  * @param active #GNUNET_YES if this address is actively used
874  * @param atsi ats performance information
875  * @param atsi_count number of ats performance elements in @a atsi
876  * @param bandwidth_out current outbound bandwidth assigned to address
877  * @param bandwidth_in current inbound bandwidth assigned to address
878  */
879 static void
880 transmit_req_addr (struct AddressIteration *ai,
881                    const struct GNUNET_PeerIdentity *id,
882                    const char *plugin_name,
883                    const void *plugin_addr,
884                    size_t plugin_addr_len,
885                    int active,
886                    const struct GNUNET_ATS_Information *atsi,
887                    uint32_t atsi_count,
888                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
889                    struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
890
891 {
892   struct GNUNET_ATS_Information *atsp;
893   struct PeerInformationMessage *msg;
894   char *addrp;
895   size_t plugin_name_length;
896   size_t msize;
897   struct GNUNET_SERVER_NotificationContext *nc;
898
899   if (NULL != plugin_name)
900     plugin_name_length = strlen (plugin_name) + 1;
901   else
902     plugin_name_length = 0;
903   msize = sizeof (struct PeerInformationMessage) +
904           atsi_count * sizeof (struct GNUNET_ATS_Information) +
905           plugin_addr_len + plugin_name_length;
906   char buf[msize] GNUNET_ALIGN;
907
908   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
909   GNUNET_assert (atsi_count <
910                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
911                  sizeof (struct GNUNET_ATS_Information));
912   msg = (struct PeerInformationMessage *) buf;
913   msg->header.size = htons (msize);
914   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
915   msg->ats_count = htonl (atsi_count);
916   msg->id = htonl (ai->id);
917   if (NULL != id)
918     msg->peer = *id;
919   else
920     memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
921   msg->address_length = htons (plugin_addr_len);
922   msg->address_active = ntohl (active);
923   msg->plugin_name_length = htons (plugin_name_length);
924   msg->bandwidth_out = bandwidth_out;
925   msg->bandwidth_in = bandwidth_in;
926   atsp = (struct GNUNET_ATS_Information *) &msg[1];
927   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
928   addrp = (char *) &atsp[atsi_count];
929   if (NULL != plugin_addr)
930     memcpy (addrp, plugin_addr, plugin_addr_len);
931   if (NULL != plugin_name)
932     strcpy (&addrp[plugin_addr_len], plugin_name);
933   nc = *GNUNET_SERVER_client_get_user_context (ai->client,
934                                                struct GNUNET_SERVER_NotificationContext *);
935   if (NULL == nc)
936   {
937     GNUNET_break (0);
938     return;
939   }
940   GNUNET_SERVER_notification_context_unicast (nc,
941                                               ai->client,
942                                               &msg->header,
943                                               GNUNET_NO);
944 }
945
946
947 /**
948  * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
949  * information to be passed back to the client.
950  *
951  * @param cls closure with our `struct AddressIteration *`
952  * @param id the peer id
953  * @param plugin_name plugin name
954  * @param plugin_addr address
955  * @param plugin_addr_len length of @a plugin_addr
956  * @param active is address actively used
957  * @param atsi ats performance information
958  * @param atsi_count number of ats performance elements in @a atsi
959  * @param bandwidth_out current outbound bandwidth assigned to address
960  * @param bandwidth_in current inbound bandwidth assigned to address
961  */
962 static void
963 req_addr_peerinfo_it (void *cls,
964                       const struct GNUNET_PeerIdentity *id,
965                       const char *plugin_name,
966                       const void *plugin_addr,
967                       size_t plugin_addr_len,
968                       int active,
969                       const struct GNUNET_ATS_Information *atsi,
970                       uint32_t atsi_count,
971                       struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
972                       struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
973 {
974   struct AddressIteration *ai = cls;
975
976   if ( (NULL == id) &&
977        (NULL == plugin_name) &&
978        (NULL == plugin_addr) )
979   {
980     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981                 "Address iteration done for one peer\n");
982     return;
983   }
984   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985               "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
986               (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
987               GNUNET_i2s (id),
988               plugin_name,
989               (unsigned int) ntohl (bandwidth_out.value__),
990               (unsigned int) ntohl (bandwidth_in.value__));
991   /* Transmit result (either if address is active, or if
992      client wanted all addresses) */
993   if ( (GNUNET_YES != ai->all) &&
994        (GNUNET_YES != active))
995     return;
996   transmit_req_addr (ai,
997                      id,
998                      plugin_name,
999                      plugin_addr, plugin_addr_len,
1000                      active,
1001                      atsi,
1002                      atsi_count,
1003                      bandwidth_out,
1004                      bandwidth_in);
1005 }
1006
1007
1008 /**
1009  * Handle 'address list request' messages from clients.
1010  *
1011  * @param cls unused, NULL
1012  * @param client client that sent the request
1013  * @param message the request message
1014  */
1015 void
1016 GAS_handle_request_address_list (void *cls,
1017                                  struct GNUNET_SERVER_Client *client,
1018                                  const struct GNUNET_MessageHeader *message)
1019 {
1020   struct AddressIteration ai;
1021   const struct AddressListRequestMessage *alrm;
1022   struct GNUNET_PeerIdentity allzeros;
1023
1024   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025               "Received ADDRESSLIST_REQUEST message\n");
1026   alrm = (const struct AddressListRequestMessage *) message;
1027   ai.all = ntohl (alrm->all);
1028   ai.id = ntohl (alrm->id);
1029   ai.client = client;
1030
1031   memset (&allzeros,
1032           '\0',
1033           sizeof (struct GNUNET_PeerIdentity));
1034   if (0 == memcmp (&alrm->peer,
1035                    &allzeros,
1036                    sizeof (struct GNUNET_PeerIdentity)))
1037   {
1038     /* Return addresses for all peers */
1039     GAS_addresses_get_peer_info (NULL,
1040                                  &req_addr_peerinfo_it,
1041                                  &ai);
1042   }
1043   else
1044   {
1045     /* Return addresses for a specific peer */
1046     GAS_addresses_get_peer_info (&alrm->peer,
1047                                  &req_addr_peerinfo_it,
1048                                  &ai);
1049   }
1050   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051               "Finished handling `%s' message\n",
1052               "ADDRESSLIST_REQUEST");
1053   transmit_req_addr (&ai,
1054                      NULL, NULL, NULL,
1055                      0, GNUNET_NO,
1056                      NULL, 0,
1057                      GNUNET_BANDWIDTH_ZERO,
1058                      GNUNET_BANDWIDTH_ZERO);
1059   GNUNET_SERVER_receive_done (client,
1060                               GNUNET_OK);
1061 }
1062
1063
1064
1065 /* end of gnunet-service-ats_addresses.c */