2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
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.
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.
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.
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_CONTAINER_multipeermap_remove (GSA_addresses,
64 update_addresses_stat ();
65 GAS_plugin_delete_address (addr);
66 GAS_performance_notify_all_clients (&addr->peer,
72 GNUNET_BANDWIDTH_ZERO,
73 GNUNET_BANDWIDTH_ZERO);
74 GNUNET_free (addr->plugin);
80 * Initialize @a norm. Sets all historic values to undefined.
82 * @param norm normalization data to initialize
85 init_norm (struct GAS_NormalizationInfo *norm)
89 for (c = 0; c < GAS_normalization_queue_length; c++)
90 norm->atsi_abs[c] = UINT64_MAX;
95 * Create a ATS_address with the given information
98 * @param plugin_name plugin
99 * @param plugin_addr address
100 * @param plugin_addr_len address length
101 * @param local_address_info additional local info for the address
102 * @param session_id session identifier, can never be 0
103 * @return the ATS_Address
105 static struct ATS_Address *
106 create_address (const struct GNUNET_PeerIdentity *peer,
107 const char *plugin_name,
108 const void *plugin_addr,
109 size_t plugin_addr_len,
110 uint32_t local_address_info,
113 struct ATS_Address *aa;
115 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
117 aa->addr_len = plugin_addr_len;
122 aa->plugin = GNUNET_strdup (plugin_name);
123 aa->session_id = session_id;
124 aa->local_address_info = local_address_info;
125 init_norm (&aa->norm_delay);
126 init_norm (&aa->norm_distance);
127 init_norm (&aa->norm_utilization_in);
128 init_norm (&aa->norm_utilization_out);
134 * Closure for #find_address_cb()
136 struct FindAddressContext
139 * Session Id to look for.
144 * Where to store matching address result.
146 struct ATS_Address *exact_address;
152 * Find session matching given session ID.
154 * @param cls a `struct FindAddressContext`
156 * @param value the address to compare with
157 * @return #GNUNET_YES to continue, #GNUNET_NO if address is found
160 find_address_cb (void *cls,
161 const struct GNUNET_PeerIdentity *key,
164 struct FindAddressContext *fac = cls;
165 struct ATS_Address *aa = value;
167 if (aa->session_id == fac->session_id)
169 fac->exact_address = aa;
177 * Find the exact address
180 * @param session_id session id, can never be 0
181 * @return an ATS_address or NULL
183 static struct ATS_Address *
184 find_exact_address (const struct GNUNET_PeerIdentity *peer,
187 struct FindAddressContext fac;
189 fac.exact_address = NULL;
190 fac.session_id = session_id;
191 GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
193 &find_address_cb, &fac);
194 return fac.exact_address;
199 * Add a new address for a peer.
202 * @param plugin_name transport plugin name
203 * @param plugin_addr plugin address
204 * @param plugin_addr_len length of the plugin address in @a plugin_addr
205 * @param local_address_info the local address for the address
206 * @param session_id session id, can be 0
207 * @param prop performance information for this address
210 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
211 const char *plugin_name,
212 const void *plugin_addr,
213 size_t plugin_addr_len,
214 uint32_t local_address_info,
216 const struct GNUNET_ATS_Properties *prop)
218 struct ATS_Address *new_address;
220 if (NULL != find_exact_address (peer,
226 new_address = create_address (peer,
232 /* Add a new address */
233 new_address->properties = *prop;
234 new_address->t_added = GNUNET_TIME_absolute_get();
235 new_address->t_last_activity = GNUNET_TIME_absolute_get();
236 GNUNET_assert(GNUNET_OK ==
237 GNUNET_CONTAINER_multipeermap_put (GSA_addresses,
240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
241 update_addresses_stat ();
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 "Adding new address for peer `%s' slot %u\n",
246 /* Tell solver about new address */
247 GAS_plugin_solver_lock ();
248 GAS_plugin_new_address (new_address);
249 GAS_normalization_update_property (new_address); // FIXME: needed?
250 GAS_plugin_solver_unlock ();
251 /* Notify performance clients about new address */
252 GAS_performance_notify_all_clients (&new_address->peer,
255 new_address->addr_len,
257 &new_address->properties,
258 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
259 GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
264 * Update an address with new performance information for a peer.
267 * @param session_id session id, never 0
268 * @param prop performance information for this address
271 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
273 const struct GNUNET_ATS_Properties *prop)
275 struct ATS_Address *aa;
277 /* Get existing address */
278 aa = find_exact_address (peer,
285 if (NULL == aa->solver_information)
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Received ADDRESS_UPDATE for peer `%s' slot %u\n",
293 (unsigned int) session_id);
296 aa->t_last_activity = GNUNET_TIME_absolute_get();
297 aa->properties = *prop;
298 /* Notify performance clients about updated address */
299 GAS_performance_notify_all_clients (&aa->peer,
305 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
306 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
308 GAS_normalization_update_property (aa);
313 * Remove an address for a peer.
316 * @param session_id session id, can never be 0
319 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
322 struct ATS_Address *ea;
324 /* Get existing address */
325 ea = find_exact_address (peer,
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Received ADDRESS_DESTROYED for peer `%s' session %u\n",
341 * Initialize address subsystem. The addresses subsystem manages the addresses
342 * known and current performance information. It has a solver component
343 * responsible for the resource allocation. It tells the solver about changes
344 * and receives updates when the solver changes the resource allocation.
346 * @param server handle to our server
349 GAS_addresses_init (struct GNUNET_SERVER_Handle *server)
351 GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
352 update_addresses_stat ();
357 * Destroy all addresses iterator
360 * @param key peer identity (unused)
361 * @param value the 'struct ATS_Address' to free
362 * @return #GNUNET_OK (continue to iterate)
365 destroy_all_address_it (void *cls,
366 const struct GNUNET_PeerIdentity *key,
369 struct ATS_Address *aa = value;
377 * Remove all addresses
380 GAS_addresses_destroy_all ()
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
383 "Destroying all addresses\n");
385 GNUNET_CONTAINER_multipeermap_size (GSA_addresses))
387 GAS_plugin_solver_lock ();
388 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
389 &destroy_all_address_it,
391 GAS_plugin_solver_unlock ();
396 * Shutdown address subsystem.
399 GAS_addresses_done ()
401 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
402 "Shutting down addresses\n");
403 GAS_addresses_destroy_all ();
404 GNUNET_CONTAINER_multipeermap_destroy (GSA_addresses);
405 GSA_addresses = NULL;
410 * Closure for #peerinfo_it().
412 struct PeerInfoIteratorContext
415 * Function to call for each address.
417 GNUNET_ATS_PeerInfo_Iterator it;
427 * Iterator to iterate over a peer's addresses
429 * @param cls a `struct PeerInfoIteratorContext`
430 * @param key the peer id
431 * @param value the `struct ATS_address`
432 * @return #GNUNET_OK to continue
435 peerinfo_it (void *cls,
436 const struct GNUNET_PeerIdentity *key,
439 struct PeerInfoIteratorContext *pi_ctx = cls;
440 struct ATS_Address *addr = value;
442 pi_ctx->it (pi_ctx->it_cls,
449 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
450 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
456 * Return information all peers currently known to ATS
458 * @param peer the respective peer, NULL for 'all' peers
459 * @param pi_it the iterator to call for every peer
460 * @param pi_it_cls the closure for @a pi_it
463 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer,
464 GNUNET_ATS_PeerInfo_Iterator pi_it,
467 struct PeerInfoIteratorContext pi_ctx;
471 /* does not make sense without callback */
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Returning information for %s from a total of %u known addresses\n",
480 (unsigned int) GNUNET_CONTAINER_multipeermap_size (GSA_addresses));
482 pi_ctx.it_cls = pi_it_cls;
484 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
488 GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
490 &peerinfo_it, &pi_ctx);
495 GNUNET_BANDWIDTH_ZERO,
496 GNUNET_BANDWIDTH_ZERO);
501 * Information we need for the callbacks to return a list of addresses
502 * back to the client.
504 struct AddressIteration
507 * Actual handle to the client.
509 struct GNUNET_SERVER_Client *client;
512 * Are we sending all addresses, or only those that are active?
517 * Which ID should be included in the response?
525 * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
526 * given address details to the client identified in @a ai.
528 * @param ai our address information context (identifies the client)
529 * @param id the peer id this address is for
530 * @param plugin_name name of the plugin that supports this address
531 * @param plugin_addr address
532 * @param plugin_addr_len length of @a plugin_addr
533 * @param active #GNUNET_YES if this address is actively used
534 * @param prop performance information
535 * @param bandwidth_out current outbound bandwidth assigned to address
536 * @param bandwidth_in current inbound bandwidth assigned to address
539 transmit_req_addr (struct AddressIteration *ai,
540 const struct GNUNET_PeerIdentity *id,
541 const char *plugin_name,
542 const void *plugin_addr,
543 size_t plugin_addr_len,
545 const struct GNUNET_ATS_Properties *prop,
546 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
547 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
550 struct PeerInformationMessage *msg;
552 size_t plugin_name_length;
554 struct GNUNET_SERVER_NotificationContext *nc;
556 if (NULL != plugin_name)
557 plugin_name_length = strlen (plugin_name) + 1;
559 plugin_name_length = 0;
560 msize = sizeof (struct PeerInformationMessage) +
561 plugin_addr_len + plugin_name_length;
562 char buf[msize] GNUNET_ALIGN;
564 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
565 msg = (struct PeerInformationMessage *) buf;
566 msg->header.size = htons (msize);
567 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
568 msg->id = htonl (ai->id);
572 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
573 msg->address_length = htons (plugin_addr_len);
574 msg->address_active = ntohl (active);
575 msg->plugin_name_length = htons (plugin_name_length);
576 msg->bandwidth_out = bandwidth_out;
577 msg->bandwidth_in = bandwidth_in;
579 GNUNET_ATS_properties_hton (&msg->properties,
582 memset (&msg->properties,
584 sizeof (struct GNUNET_ATS_Properties));
585 addrp = (char *) &msg[1];
586 if (NULL != plugin_addr)
587 memcpy (addrp, plugin_addr, plugin_addr_len);
588 if (NULL != plugin_name)
589 strcpy (&addrp[plugin_addr_len], plugin_name);
590 nc = *GNUNET_SERVER_client_get_user_context (ai->client,
591 struct GNUNET_SERVER_NotificationContext *);
597 GNUNET_SERVER_notification_context_unicast (nc,
605 * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
606 * information to be passed back to the client.
608 * @param cls closure with our `struct AddressIteration *`
609 * @param id the peer id
610 * @param plugin_name plugin name
611 * @param plugin_addr address
612 * @param plugin_addr_len length of @a plugin_addr
613 * @param active is address actively used
614 * @param prop performance information
615 * @param bandwidth_out current outbound bandwidth assigned to address
616 * @param bandwidth_in current inbound bandwidth assigned to address
619 req_addr_peerinfo_it (void *cls,
620 const struct GNUNET_PeerIdentity *id,
621 const char *plugin_name,
622 const void *plugin_addr,
623 size_t plugin_addr_len,
625 const struct GNUNET_ATS_Properties *prop,
626 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
627 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
629 struct AddressIteration *ai = cls;
632 (NULL == plugin_name) &&
633 (NULL == plugin_addr) )
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Address iteration done for one peer\n");
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
641 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
644 (unsigned int) ntohl (bandwidth_out.value__),
645 (unsigned int) ntohl (bandwidth_in.value__));
646 /* Transmit result (either if address is active, or if
647 client wanted all addresses) */
648 if ( (GNUNET_YES != ai->all) &&
649 (GNUNET_YES != active))
651 transmit_req_addr (ai,
654 plugin_addr, plugin_addr_len,
663 * Handle 'address list request' messages from clients.
665 * @param cls unused, NULL
666 * @param client client that sent the request
667 * @param message the request message
670 GAS_handle_request_address_list (void *cls,
671 struct GNUNET_SERVER_Client *client,
672 const struct GNUNET_MessageHeader *message)
674 struct AddressIteration ai;
675 const struct AddressListRequestMessage *alrm;
676 struct GNUNET_PeerIdentity allzeros;
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Received ADDRESSLIST_REQUEST message\n");
680 alrm = (const struct AddressListRequestMessage *) message;
681 ai.all = ntohl (alrm->all);
682 ai.id = ntohl (alrm->id);
687 sizeof (struct GNUNET_PeerIdentity));
688 if (0 == memcmp (&alrm->peer,
690 sizeof (struct GNUNET_PeerIdentity)))
692 /* Return addresses for all peers */
693 GAS_addresses_get_peer_info (NULL,
694 &req_addr_peerinfo_it,
699 /* Return addresses for a specific peer */
700 GAS_addresses_get_peer_info (&alrm->peer,
701 &req_addr_peerinfo_it,
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Finished handling `%s' message\n",
706 "ADDRESSLIST_REQUEST");
707 transmit_req_addr (&ai,
711 GNUNET_BANDWIDTH_ZERO,
712 GNUNET_BANDWIDTH_ZERO);
713 GNUNET_SERVER_receive_done (client,
719 /* end of gnunet-service-ats_addresses.c */