/*
This file is part of GNUnet.
- (C) 2011-2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
* @author Christian Grothoff
*/
#include "platform.h"
-#include "gnunet_ats_service.h"
-#include "gnunet-service-ats.h"
#include "gnunet-service-ats_addresses.h"
-#include "gnunet-service-ats_normalization.h"
#include "gnunet-service-ats_performance.h"
+#include "gnunet-service-ats_normalization.h"
#include "gnunet-service-ats_plugins.h"
-#include "gnunet-service-ats_scheduling.h"
-#include "gnunet-service-ats_reservations.h"
/**
* The bandwidth assigned to a peer can be influenced by setting a preference
* for a peer. The prefernce will be given to to the solver with s_pref which
* has to take care of the preference value
-
*/
-
-
/**
* A multihashmap to store all addresses
*/
/**
- * Value we pass for zero bandwidth.
+ * Update statistic on number of addresses.
*/
-static const struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
+static void
+update_addresses_stat ()
+{
+ GNUNET_STATISTICS_set (GSA_stats,
+ "# addresses",
+ GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
+ GNUNET_NO);
+}
/**
dest->atsi =
GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
dest->atsi_count = update_count;
- memcpy (dest->atsi, update,
- update_count * sizeof(struct GNUNET_ATS_Information));
- (*delta_dest) =
+ memcpy (dest->atsi,
+ update,
+ update_count * sizeof(struct GNUNET_ATS_Information));
+ *delta_dest =
GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
for (c1 = 0; c1 < update_count; c1++)
{
dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
add_atsi_count * sizeof(struct GNUNET_ATS_Information));
- GNUNET_free(dest->atsi);
+ GNUNET_free (dest->atsi);
dest->atsi = tmp_atsi;
dest->atsi_count = dest->atsi_count + add_atsi_count;
change = GNUNET_YES;
static void
free_address (struct ATS_Address *addr)
{
+ GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
+ &addr->peer,
+ addr);
+ update_addresses_stat ();
+ GAS_plugin_delete_address (addr);
+ GAS_performance_notify_all_clients (&addr->peer,
+ addr->plugin,
+ addr->addr,
+ addr->addr_len,
+ GNUNET_NO,
+ NULL, 0,
+ GNUNET_BANDWIDTH_ZERO,
+ GNUNET_BANDWIDTH_ZERO);
GNUNET_free (addr->plugin);
GNUNET_free_non_null (addr->atsi);
GNUNET_free (addr);
aa->peer = *peer;
aa->addr_len = plugin_addr_len;
aa->addr = &aa[1];
- memcpy (&aa[1], plugin_addr, plugin_addr_len);
+ memcpy (&aa[1],
+ plugin_addr,
+ plugin_addr_len);
aa->plugin = GNUNET_strdup (plugin_name);
aa->session_id = session_id;
aa->local_address_info = local_address_info;
for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
{
aa->atsin[c1].avg_queue_index = 0;
+ aa->atsin[c1].norm = DEFAULT_REL_QUALITY;
for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
}
}
-/**
- * Closure for #compare_address_it()
- */
-struct CompareAddressContext
-{
- const struct ATS_Address *search;
-
- /* exact_address != NULL if address and session is equal */
- struct ATS_Address *exact_address;
- /* exact_address != NULL if address and session is 0 */
- struct ATS_Address *base_address;
-};
-
-
-/**
- * Comapre addresses.
- *
- * @param cls a CompareAddressContext containin the source address
- * @param key peer id
- * @param value the address to compare with
- * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
- */
-static int
-compare_address_it (void *cls,
- const struct GNUNET_PeerIdentity *key,
- void *value)
-{
- struct CompareAddressContext *cac = cls;
- struct ATS_Address *aa = value;
-
- /* Find an matching exact address:
- *
- * Compare by:
- * aa->addr_len == cac->search->addr_len
- * aa->plugin == cac->search->plugin
- * aa->addr == cac->search->addr
- * aa->session == cac->search->session
- *
- * return as exact address
- */
- if ((aa->addr_len == cac->search->addr_len)
- && (0 == strcmp (aa->plugin, cac->search->plugin)))
- {
- if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
- && (aa->session_id == cac->search->session_id))
- cac->exact_address = aa;
- }
-
- /* Find an matching base address:
- *
- * Properties:
- *
- * aa->session_id == 0
- *
- * Compare by:
- * aa->addr_len == cac->search->addr_len
- * aa->plugin == cac->search->plugin
- * aa->addr == cac->search->addr
- *
- * return as base address
- */
- if ((aa->addr_len == cac->search->addr_len)
- && (0 == strcmp (aa->plugin, cac->search->plugin)))
- {
- if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
- && (aa->session_id == 0))
- cac->base_address = aa;
- }
-
- /* Find an matching exact address based on session:
- *
- * Properties:
- *
- * cac->search->addr_len == 0
- *
- * Compare by:
- * aa->plugin == cac->search->plugin
- * aa->session_id == cac->search->session_id
- *
- * return as exact address
- */
- if (0 == cac->search->addr_len)
- {
- if ((0 == strcmp (aa->plugin, cac->search->plugin))
- && (aa->session_id == cac->search->session_id))
- cac->exact_address = aa;
- }
-
- if (cac->exact_address == NULL )
- return GNUNET_YES; /* Continue iteration to find exact address */
- else
- return GNUNET_NO; /* Stop iteration since we have an exact address */
-}
-
-
-/**
- * Find an existing equivalent address record.
- * Compares by peer identity and network address OR by session ID
- * (one of the two must match).
- *
- * @param peer peer to lookup addresses for
- * @param addr existing address record
- * @return existing address record, NULL for none
- */
-struct ATS_Address *
-find_equivalent_address (const struct GNUNET_PeerIdentity *peer,
- const struct ATS_Address *addr)
-{
- struct CompareAddressContext cac;
-
- cac.exact_address = NULL;
- cac.base_address = NULL;
- cac.search = addr;
- GNUNET_CONTAINER_multipeermap_get_multiple (GSA_addresses,
- peer,
- &compare_address_it, &cac);
-
- if (NULL == cac.exact_address)
- return cac.base_address;
- return cac.exact_address;
-}
-
-
/**
* Closure for #find_address_cb()
*/
* @return the value in HBO or #GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
*/
static int
-get_performance_info (struct ATS_Address *address, uint32_t type)
+get_performance_info (struct ATS_Address *address,
+ uint32_t type)
{
- int c1;
- GNUNET_assert(NULL != address);
+ uint32_t c1;
if ((NULL == address->atsi) || (0 == address->atsi_count))
return GNUNET_ATS_VALUE_UNDEFINED;
uint32_t atsi_count)
{
struct ATS_Address *new_address;
- struct ATS_Address *existing_address;
struct GNUNET_ATS_Information *atsi_delta;
uint32_t atsi_delta_count;
uint32_t addr_net;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received `%s' for peer `%s'\n",
- "ADDRESS ADD",
- GNUNET_i2s (peer));
+ if (NULL != find_exact_address (peer, session_id))
+ {
+ GNUNET_break (0);
+ return;
+ }
new_address = create_address (peer,
plugin_name,
plugin_addr,
if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
addr_net = GNUNET_ATS_NET_UNSPECIFIED;
- /* Get existing address or address with session == 0 */
- existing_address = find_equivalent_address (peer, new_address);
- if (NULL != existing_address)
- {
- GNUNET_break (0);
- GNUNET_free(new_address->plugin);
- GNUNET_free_non_null(new_address->atsi);
- GNUNET_free(new_address);
- return;
- }
/* Add a new address */
new_address->t_added = GNUNET_TIME_absolute_get();
new_address->t_last_activity = GNUNET_TIME_absolute_get();
peer,
new_address,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
-
- GNUNET_STATISTICS_set (GSA_stats,
- "# addresses",
- GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
- GNUNET_NO);
-
+ update_addresses_stat ();
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
- new_address,
+ "Adding new address for peer `%s' slot %u\n",
GNUNET_i2s (peer),
- plugin_addr_len,
- session_id,
- GNUNET_ATS_print_network_type (addr_net));
-
+ session_id);
/* Tell solver about new address */
+ GAS_plugin_solver_lock ();
GAS_plugin_new_address (new_address,
- addr_net,
- atsi,
- atsi_count);
+ addr_net);
+ GAS_normalization_update_property (new_address,
+ atsi,
+ atsi_count);
+ GAS_plugin_solver_unlock ();
/* Notify performance clients about new address */
- GAS_performance_notify_all_clients (&new_address->peer,
+ GAS_performance_notify_all_clients (&new_address->peer,
new_address->plugin,
new_address->addr,
- new_address->addr_len,
+ new_address->addr_len,
new_address->active,
- new_address->atsi,
+ new_address->atsi,
new_address->atsi_count,
GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_out),
GNUNET_BANDWIDTH_value_init (new_address->assigned_bw_in));
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received `%s' for peer `%s' address \n",
- "ADDRESS UPDATE",
+ "Received ADDRESS_UPDATE for peer `%s' slot %u\n",
GNUNET_i2s (peer),
- aa);
+ (unsigned int) session_id);
/* Update address */
aa->t_last_activity = GNUNET_TIME_absolute_get();
&atsi_delta_count))
{
/* Notify performance clients about updated address */
- GAS_performance_notify_all_clients (&aa->peer,
+ GAS_performance_notify_all_clients (&aa->peer,
aa->plugin,
aa->addr,
- aa->addr_len,
- aa->active,
+ aa->addr_len,
+ aa->active,
aa->atsi,
aa->atsi_count,
GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
- GAS_plugin_update_address (aa,
- atsi,
- atsi_count);
+ GAS_normalization_update_property (aa,
+ atsi,
+ atsi_count);
}
GNUNET_free_non_null (atsi_delta);
}
/**
- * Remove an address or just a session for a peer.
+ * Remove an address for a peer.
*
* @param peer peer
* @param session_id session id, can never be 0
if (NULL == ea)
{
GNUNET_break (0);
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Tried to destroy unknown address for peer `%s' session id %u\n",
- GNUNET_i2s (peer),
- session_id);
return;
}
-
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received `%s' for peer `%s' address %p session %u\n",
- "ADDRESS DESTROYED",
+ "Received ADDRESS_DESTROYED for peer `%s' session %u\n",
GNUNET_i2s (peer),
- ea,
session_id);
- GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
- peer,
- ea);
- GAS_plugin_delete_address (ea);
- GAS_performance_notify_all_clients (peer,
- ea->plugin,
- ea->addr,
- ea->addr_len,
- GNUNET_SYSERR,
- NULL, 0,
- zero_bw,
- zero_bw);
free_address (ea);
- GNUNET_STATISTICS_set (GSA_stats,
- "# addresses",
- GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
- GNUNET_NO);
}
-
-
-
/**
* Initialize address subsystem. The addresses subsystem manages the addresses
* known and current performance information. It has a solver component
* responsible for the resource allocation. It tells the solver about changes
* and receives updates when the solver changes the resource allocation.
+ *
+ * @param server handle to our server
*/
void
-GAS_addresses_init ()
+GAS_addresses_init (struct GNUNET_SERVER_Handle *server)
{
- /* Initialize the addresses database */
GSA_addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
- GNUNET_STATISTICS_set (GSA_stats,
- "# addresses",
- GNUNET_CONTAINER_multipeermap_size (GSA_addresses),
- GNUNET_NO);
+ update_addresses_stat ();
}
{
struct ATS_Address *aa = value;
- /* Remove */
- GNUNET_assert (GNUNET_YES ==
- GNUNET_CONTAINER_multipeermap_remove (GSA_addresses,
- key,
- value));
- /* Notify */
- GAS_plugin_delete_address (aa);
- /* Destroy */
- GAS_performance_notify_all_clients (&aa->peer,
- aa->plugin,
- aa->addr,
- aa->addr_len,
- GNUNET_NO,
- NULL, 0,
- zero_bw,
- zero_bw);
free_address (aa);
return GNUNET_OK;
}
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Destroying all addresses\n");
+ if (0 ==
+ GNUNET_CONTAINER_multipeermap_size (GSA_addresses))
+ return;
GAS_plugin_solver_lock ();
- if (NULL != GSA_addresses)
- GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
- &destroy_all_address_it,
- NULL);
+ GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
+ &destroy_all_address_it,
+ NULL);
GAS_plugin_solver_unlock ();
}
NULL, NULL, NULL, 0,
GNUNET_NO,
NULL, 0,
- zero_bw,
- zero_bw);
+ GNUNET_BANDWIDTH_ZERO,
+ GNUNET_BANDWIDTH_ZERO);
+}
+
+
+/**
+ * Information we need for the callbacks to return a list of addresses
+ * back to the client.
+ */
+struct AddressIteration
+{
+ /**
+ * Actual handle to the client.
+ */
+ struct GNUNET_SERVER_Client *client;
+
+ /**
+ * Are we sending all addresses, or only those that are active?
+ */
+ int all;
+
+ /**
+ * Which ID should be included in the response?
+ */
+ uint32_t id;
+
+};
+
+
+/**
+ * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
+ * given address details to the client identified in @a ai.
+ *
+ * @param ai our address information context (identifies the client)
+ * @param id the peer id this address is for
+ * @param plugin_name name of the plugin that supports this address
+ * @param plugin_addr address
+ * @param plugin_addr_len length of @a plugin_addr
+ * @param active #GNUNET_YES if this address is actively used
+ * @param atsi ats performance information
+ * @param atsi_count number of ats performance elements in @a atsi
+ * @param bandwidth_out current outbound bandwidth assigned to address
+ * @param bandwidth_in current inbound bandwidth assigned to address
+ */
+static void
+transmit_req_addr (struct AddressIteration *ai,
+ const struct GNUNET_PeerIdentity *id,
+ const char *plugin_name,
+ const void *plugin_addr,
+ size_t plugin_addr_len,
+ int active,
+ const struct GNUNET_ATS_Information *atsi,
+ uint32_t atsi_count,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
+
+{
+ struct GNUNET_ATS_Information *atsp;
+ struct PeerInformationMessage *msg;
+ char *addrp;
+ size_t plugin_name_length;
+ size_t msize;
+ struct GNUNET_SERVER_NotificationContext *nc;
+
+ if (NULL != plugin_name)
+ plugin_name_length = strlen (plugin_name) + 1;
+ else
+ plugin_name_length = 0;
+ msize = sizeof (struct PeerInformationMessage) +
+ atsi_count * sizeof (struct GNUNET_ATS_Information) +
+ plugin_addr_len + plugin_name_length;
+ char buf[msize] GNUNET_ALIGN;
+
+ GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
+ GNUNET_assert (atsi_count <
+ GNUNET_SERVER_MAX_MESSAGE_SIZE /
+ sizeof (struct GNUNET_ATS_Information));
+ msg = (struct PeerInformationMessage *) buf;
+ msg->header.size = htons (msize);
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
+ msg->ats_count = htonl (atsi_count);
+ msg->id = htonl (ai->id);
+ if (NULL != id)
+ msg->peer = *id;
+ else
+ memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
+ msg->address_length = htons (plugin_addr_len);
+ msg->address_active = ntohl (active);
+ msg->plugin_name_length = htons (plugin_name_length);
+ msg->bandwidth_out = bandwidth_out;
+ msg->bandwidth_in = bandwidth_in;
+ atsp = (struct GNUNET_ATS_Information *) &msg[1];
+ memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
+ addrp = (char *) &atsp[atsi_count];
+ if (NULL != plugin_addr)
+ memcpy (addrp, plugin_addr, plugin_addr_len);
+ if (NULL != plugin_name)
+ strcpy (&addrp[plugin_addr_len], plugin_name);
+ nc = *GNUNET_SERVER_client_get_user_context (ai->client,
+ struct GNUNET_SERVER_NotificationContext *);
+ if (NULL == nc)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ GNUNET_SERVER_notification_context_unicast (nc,
+ ai->client,
+ &msg->header,
+ GNUNET_NO);
+}
+
+
+/**
+ * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
+ * information to be passed back to the client.
+ *
+ * @param cls closure with our `struct AddressIteration *`
+ * @param id the peer id
+ * @param plugin_name plugin name
+ * @param plugin_addr address
+ * @param plugin_addr_len length of @a plugin_addr
+ * @param active is address actively used
+ * @param atsi ats performance information
+ * @param atsi_count number of ats performance elements in @a atsi
+ * @param bandwidth_out current outbound bandwidth assigned to address
+ * @param bandwidth_in current inbound bandwidth assigned to address
+ */
+static void
+req_addr_peerinfo_it (void *cls,
+ const struct GNUNET_PeerIdentity *id,
+ const char *plugin_name,
+ const void *plugin_addr,
+ size_t plugin_addr_len,
+ int active,
+ const struct GNUNET_ATS_Information *atsi,
+ uint32_t atsi_count,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+ struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
+{
+ struct AddressIteration *ai = cls;
+
+ if ( (NULL == id) &&
+ (NULL == plugin_name) &&
+ (NULL == plugin_addr) )
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Address iteration done for one peer\n");
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
+ (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
+ GNUNET_i2s (id),
+ plugin_name,
+ (unsigned int) ntohl (bandwidth_out.value__),
+ (unsigned int) ntohl (bandwidth_in.value__));
+ /* Transmit result (either if address is active, or if
+ client wanted all addresses) */
+ if ( (GNUNET_YES != ai->all) &&
+ (GNUNET_YES != active))
+ return;
+ transmit_req_addr (ai,
+ id,
+ plugin_name,
+ plugin_addr, plugin_addr_len,
+ active,
+ atsi,
+ atsi_count,
+ bandwidth_out,
+ bandwidth_in);
+}
+
+
+/**
+ * Handle 'address list request' messages from clients.
+ *
+ * @param cls unused, NULL
+ * @param client client that sent the request
+ * @param message the request message
+ */
+void
+GAS_handle_request_address_list (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct AddressIteration ai;
+ const struct AddressListRequestMessage *alrm;
+ struct GNUNET_PeerIdentity allzeros;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received ADDRESSLIST_REQUEST message\n");
+ alrm = (const struct AddressListRequestMessage *) message;
+ ai.all = ntohl (alrm->all);
+ ai.id = ntohl (alrm->id);
+ ai.client = client;
+
+ memset (&allzeros,
+ '\0',
+ sizeof (struct GNUNET_PeerIdentity));
+ if (0 == memcmp (&alrm->peer,
+ &allzeros,
+ sizeof (struct GNUNET_PeerIdentity)))
+ {
+ /* Return addresses for all peers */
+ GAS_addresses_get_peer_info (NULL,
+ &req_addr_peerinfo_it,
+ &ai);
+ }
+ else
+ {
+ /* Return addresses for a specific peer */
+ GAS_addresses_get_peer_info (&alrm->peer,
+ &req_addr_peerinfo_it,
+ &ai);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Finished handling `%s' message\n",
+ "ADDRESSLIST_REQUEST");
+ transmit_req_addr (&ai,
+ NULL, NULL, NULL,
+ 0, GNUNET_NO,
+ NULL, 0,
+ GNUNET_BANDWIDTH_ZERO,
+ GNUNET_BANDWIDTH_ZERO);
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
}
+
+
/* end of gnunet-service-ats_addresses.c */