2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file ats/gnunet-service-ats_addresses.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_normalization.h"
31 #include "gnunet-service-ats_plugins.h"
35 * A multihashmap to store all addresses
37 struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
41 * Update statistic on number of addresses.
44 update_addresses_stat ()
46 GNUNET_STATISTICS_set (GSA_stats,
48 GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
54 * Free the given address
56 * @param addr address to destroy
59 free_address (struct ATS_Address *addr)
61 GNUNET_assert (GNUNET_YES ==
62 GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
65 update_addresses_stat ();
66 GAS_plugin_delete_address (addr);
67 GAS_performance_notify_all_clients (&addr->peer,
73 addr->local_address_info,
74 GNUNET_BANDWIDTH_ZERO,
75 GNUNET_BANDWIDTH_ZERO);
76 GNUNET_free (addr->plugin);
82 * Initialize @a norm. Sets all historic values to undefined.
84 * @param norm normalization data to initialize
87 init_norm (struct GAS_NormalizationInfo *norm)
91 for (c = 0; c < GAS_normalization_queue_length; c++)
92 norm->atsi_abs[c] = UINT64_MAX;
97 * Create a ATS_address with the given information
100 * @param plugin_name plugin
101 * @param plugin_addr address
102 * @param plugin_addr_len address length
103 * @param local_address_info additional local info for the address
104 * @param session_id session identifier, can never be 0
105 * @return the ATS_Address
107 static struct ATS_Address *
108 create_address (const struct GNUNET_PeerIdentity *peer,
109 const char *plugin_name,
110 const void *plugin_addr,
111 size_t plugin_addr_len,
112 uint32_t local_address_info,
115 struct ATS_Address *aa;
117 aa = GNUNET_malloc (sizeof(struct ATS_Address) + plugin_addr_len);
119 aa->addr_len = plugin_addr_len;
121 GNUNET_memcpy (&aa[1],
124 aa->plugin = GNUNET_strdup (plugin_name);
125 aa->session_id = session_id;
126 aa->local_address_info = local_address_info;
127 init_norm (&aa->norm_delay);
128 init_norm (&aa->norm_distance);
129 init_norm (&aa->norm_utilization_in);
130 init_norm (&aa->norm_utilization_out);
136 * Closure for #find_address_cb()
138 struct FindAddressContext
141 * Session Id to look for.
146 * Where to store matching address result.
148 struct ATS_Address *exact_address;
153 * Find session matching given session ID.
155 * @param cls a `struct FindAddressContext`
157 * @param value the address to compare with
158 * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
161 find_address_cb (void *cls,
162 const struct GNUNET_PeerIdentity *key,
165 struct FindAddressContext *fac = cls;
166 struct ATS_Address *aa = value;
168 if (aa->session_id == fac->session_id)
170 fac->exact_address = aa;
178 * Find the exact address
181 * @param session_id session id, can never be 0
182 * @return an ATS_address or NULL
184 static struct ATS_Address *
185 find_exact_address (const struct GNUNET_PeerIdentity *peer,
188 struct FindAddressContext fac;
190 fac.exact_address = NULL;
191 fac.session_id = session_id;
192 GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
194 &find_address_cb, &fac);
195 return fac.exact_address;
200 * Add a new address for a peer.
203 * @param plugin_name transport plugin name
204 * @param plugin_addr plugin address
205 * @param plugin_addr_len length of the plugin address in @a plugin_addr
206 * @param local_address_info the local address for the address
207 * @param session_id session id, can be 0
208 * @param prop performance information for this address
211 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
212 const char *plugin_name,
213 const void *plugin_addr,
214 size_t plugin_addr_len,
215 uint32_t local_address_info,
217 const struct GNUNET_ATS_Properties *prop)
219 struct ATS_Address *new_address;
221 if (NULL != find_exact_address (peer,
227 GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
228 new_address = create_address (peer,
234 /* Add a new address */
235 new_address->properties = *prop;
236 new_address->t_added = GNUNET_TIME_absolute_get ();
237 new_address->t_last_activity = GNUNET_TIME_absolute_get ();
238 GNUNET_assert (GNUNET_OK ==
239 GNUNET_CONTAINER_multipeermap_put (GSA_addresses,
242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
243 update_addresses_stat ();
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245 "Adding new address for peer `%s' slot %u\n",
248 /* Tell solver about new address */
249 GAS_plugin_solver_lock ();
250 GAS_plugin_new_address (new_address);
251 GAS_normalization_update_property (new_address); // FIXME: needed?
252 GAS_plugin_solver_unlock ();
253 /* Notify performance clients about new address */
254 GAS_performance_notify_all_clients (&new_address->peer,
257 new_address->addr_len,
259 &new_address->properties,
260 new_address->local_address_info,
261 GNUNET_BANDWIDTH_value_init (
262 new_address->assigned_bw_out),
263 GNUNET_BANDWIDTH_value_init (
264 new_address->assigned_bw_in));
269 * Update an address with new performance information for a peer.
272 * @param session_id session id, never 0
273 * @param prop performance information for this address
276 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
278 const struct GNUNET_ATS_Properties *prop)
280 struct ATS_Address *aa;
282 /* Get existing address */
283 aa = find_exact_address (peer,
290 if (NULL == aa->solver_information)
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Received ADDRESS_UPDATE for peer `%s' slot %u\n",
298 (unsigned int) session_id);
299 GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
301 aa->t_last_activity = GNUNET_TIME_absolute_get ();
302 aa->properties = *prop;
303 /* Notify performance clients about updated address */
304 GAS_performance_notify_all_clients (&aa->peer,
310 aa->local_address_info,
311 GNUNET_BANDWIDTH_value_init (
312 aa->assigned_bw_out),
313 GNUNET_BANDWIDTH_value_init (
314 aa->assigned_bw_in));
316 GAS_normalization_update_property (aa);
321 * Remove an address for a peer.
324 * @param session_id session id, can never be 0
327 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
330 struct ATS_Address *ea;
332 /* Get existing address */
333 ea = find_exact_address (peer,
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Received ADDRESS_DESTROYED for peer `%s' session %u\n",
349 * Initialize address subsystem. The addresses subsystem manages the addresses
350 * known and current performance information. It has a solver component
351 * responsible for the resource allocation. It tells the solver about changes
352 * and receives updates when the solver changes the resource allocation.
355 GAS_addresses_init ()
358 = GNUNET_CONTAINER_multipeermap_create (128,
360 update_addresses_stat ();
365 * Destroy all addresses iterator
368 * @param key peer identity (unused)
369 * @param value the 'struct ATS_Address' to free
370 * @return #GNUNET_OK (continue to iterate)
373 destroy_all_address_it (void *cls,
374 const struct GNUNET_PeerIdentity *key,
377 struct ATS_Address *aa = value;
385 * Remove all addresses
388 GAS_addresses_destroy_all ()
390 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
391 "Destroying all addresses\n");
393 GNUNET_CONTAINER_multipeermap_size (GSA_addresses))
395 GAS_plugin_solver_lock ();
396 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
397 &destroy_all_address_it,
399 GAS_plugin_solver_unlock ();
404 * Shutdown address subsystem.
407 GAS_addresses_done ()
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410 "Shutting down addresses\n");
411 GAS_plugin_solver_lock ();
412 GAS_addresses_destroy_all ();
413 GAS_plugin_solver_unlock ();
414 GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
415 GSA_addresses = NULL;
420 * Closure for #peerinfo_it().
422 struct PeerInfoIteratorContext
425 * Function to call for each address.
427 GNUNET_ATS_PeerInfo_Iterator it;
437 * Iterator to iterate over a peer's addresses
439 * @param cls a `struct PeerInfoIteratorContext`
440 * @param key the peer id
441 * @param value the `struct ATS_address`
442 * @return #GNUNET_OK to continue
445 peerinfo_it (void *cls,
446 const struct GNUNET_PeerIdentity *key,
449 struct PeerInfoIteratorContext *pi_ctx = cls;
450 struct ATS_Address *addr = value;
452 pi_ctx->it (pi_ctx->it_cls,
459 addr->local_address_info,
460 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
461 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
467 * Return information all peers currently known to ATS
469 * @param peer the respective peer, NULL for 'all' peers
470 * @param pi_it the iterator to call for every peer
471 * @param pi_it_cls the closure for @a pi_it
474 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
475 GNUNET_ATS_PeerInfo_Iterator pi_it,
478 struct PeerInfoIteratorContext pi_ctx;
482 /* does not make sense without callback */
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Returning information for %s from a total of %u known addresses\n",
491 (unsigned int) GNUNET_CONTAINER_multipeermap_size (
494 pi_ctx.it_cls = pi_it_cls;
496 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
500 GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
502 &peerinfo_it, &pi_ctx);
507 GNUNET_HELLO_ADDRESS_INFO_NONE,
508 GNUNET_BANDWIDTH_ZERO,
509 GNUNET_BANDWIDTH_ZERO);
514 * Information we need for the callbacks to return a list of addresses
515 * back to the client.
517 struct AddressIteration
520 * Actual handle to the client.
522 struct GNUNET_SERVICE_Client *client;
525 * Are we sending all addresses, or only those that are active?
530 * Which ID should be included in the response?
537 * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
538 * given address details to the client identified in @a ai.
540 * @param ai our address information context (identifies the client)
541 * @param id the peer id this address is for
542 * @param plugin_name name of the plugin that supports this address
543 * @param plugin_addr address
544 * @param plugin_addr_len length of @a plugin_addr
545 * @param active #GNUNET_YES if this address is actively used
546 * @param prop performance information
547 * @param local_address_info flags for the address
548 * @param bandwidth_out current outbound bandwidth assigned to address
549 * @param bandwidth_in current inbound bandwidth assigned to address
552 transmit_req_addr (struct AddressIteration *ai,
553 const struct GNUNET_PeerIdentity *id,
554 const char *plugin_name,
555 const void *plugin_addr,
556 size_t plugin_addr_len,
558 const struct GNUNET_ATS_Properties *prop,
559 enum GNUNET_HELLO_AddressInfo local_address_info,
560 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
561 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
564 struct GNUNET_MQ_Envelope *env;
565 struct PeerInformationMessage *msg;
567 size_t plugin_name_length;
570 if (NULL != plugin_name)
571 plugin_name_length = strlen (plugin_name) + 1;
573 plugin_name_length = 0;
574 msize = plugin_addr_len + plugin_name_length;
576 GNUNET_assert (sizeof(struct PeerInformationMessage) + msize
577 < GNUNET_MAX_MESSAGE_SIZE);
578 env = GNUNET_MQ_msg_extra (msg,
580 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
581 msg->id = htonl (ai->id);
584 msg->address_length = htons (plugin_addr_len);
585 msg->address_active = ntohl (active);
586 msg->plugin_name_length = htons (plugin_name_length);
587 msg->bandwidth_out = bandwidth_out;
588 msg->bandwidth_in = bandwidth_in;
590 GNUNET_ATS_properties_hton (&msg->properties,
592 msg->address_local_info = htonl ((uint32_t) local_address_info);
593 addrp = (char *) &msg[1];
594 GNUNET_memcpy (addrp,
597 if (NULL != plugin_name)
598 strcpy (&addrp[plugin_addr_len],
600 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (ai->client),
606 * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
607 * information to be passed back to the client.
609 * @param cls closure with our `struct AddressIteration *`
610 * @param id the peer id
611 * @param plugin_name plugin name
612 * @param plugin_addr address
613 * @param plugin_addr_len length of @a plugin_addr
614 * @param active is address actively used
615 * @param prop performance information
616 * @param local_address_info additional local info for the address
617 * @param bandwidth_out current outbound bandwidth assigned to address
618 * @param bandwidth_in current inbound bandwidth assigned to address
621 req_addr_peerinfo_it (void *cls,
622 const struct GNUNET_PeerIdentity *id,
623 const char *plugin_name,
624 const void *plugin_addr,
625 size_t plugin_addr_len,
627 const struct GNUNET_ATS_Properties *prop,
628 enum GNUNET_HELLO_AddressInfo local_address_info,
629 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
630 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
632 struct AddressIteration *ai = cls;
635 (NULL == plugin_name) &&
636 (NULL == plugin_addr))
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Address iteration done for one peer\n");
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
644 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
647 (unsigned int) ntohl (bandwidth_out.value__),
648 (unsigned int) ntohl (bandwidth_in.value__));
649 /* Transmit result (either if address is active, or if
650 client wanted all addresses) */
651 if ((GNUNET_YES != ai->all) &&
652 (GNUNET_YES != active))
654 transmit_req_addr (ai,
657 plugin_addr, plugin_addr_len,
667 * Handle 'address list request' messages from clients.
669 * @param cls client that sent the request
670 * @param alrm the request message
673 GAS_handle_request_address_list (struct GNUNET_SERVICE_Client *client,
674 const struct AddressListRequestMessage *alrm)
676 struct AddressIteration ai;
677 struct GNUNET_PeerIdentity allzeros;
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Received ADDRESSLIST_REQUEST message\n");
681 ai.all = ntohl (alrm->all);
682 ai.id = ntohl (alrm->id);
687 sizeof(struct GNUNET_PeerIdentity));
688 if (0 == GNUNET_is_zero (&alrm->peer))
690 /* Return addresses for all peers */
691 GAS_addresses_get_peer_info (NULL,
692 &req_addr_peerinfo_it,
697 /* Return addresses for a specific peer */
698 GAS_addresses_get_peer_info (&alrm->peer,
699 &req_addr_peerinfo_it,
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "Finished handling `%s' message\n",
704 "ADDRESSLIST_REQUEST");
705 transmit_req_addr (&ai,
709 GNUNET_HELLO_ADDRESS_INFO_NONE,
710 GNUNET_BANDWIDTH_ZERO,
711 GNUNET_BANDWIDTH_ZERO);
715 /* end of gnunet-service-ats_addresses.c */