/*
This file is part of GNUnet.
- (C) 2009 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011 GNUnet e.V.
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 3, or (at your
- option) any later version.
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ SPDX-License-Identifier: AGPL3.0-or-later
*/
-
/**
* @file ats/gnunet-service-ats.c
* @brief ats service
* @author Matthias Wachs
+ * @author Christian Grothoff
*/
#include "platform.h"
-#include "gnunet_getopt_lib.h"
-#include "gnunet_service_lib.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_ats_service.h"
+#include "gnunet_util_lib.h"
+#include "gnunet-service-ats.h"
+#include "gnunet-service-ats_addresses.h"
+#include "gnunet-service-ats_connectivity.h"
+#include "gnunet-service-ats_normalization.h"
+#include "gnunet-service-ats_performance.h"
+#include "gnunet-service-ats_preferences.h"
+#include "gnunet-service-ats_scheduling.h"
+#include "gnunet-service-ats_reservations.h"
+#include "gnunet-service-ats_plugins.h"
#include "ats.h"
-struct ATS_Clients
-{
- struct ATS_Clients * next;
-
- struct ATS_Clients * prev;
-
- struct GNUNET_SERVER_Client *client;
+/**
+ * Handle for statistics.
+ */
+struct GNUNET_STATISTICS_Handle *GSA_stats;
- uint32_t flags;
-};
-struct ATS_Address
+/**
+ * We have received a `struct ClientStartMessage` from a client. Find
+ * out which type of client it is and notify the respective subsystem.
+ *
+ * @param cls handle to the client
+ * @param msg the start message
+ */
+static void
+handle_ats_start (void *cls,
+ const struct ClientStartMessage *msg)
{
- struct GNUNET_PeerIdentity peer;
-
- size_t addr_len;
-
- uint32_t session_id;
-
- uint32_t ats_count;
-
- void * addr;
+ struct GNUNET_SERVICE_Client *client = cls;
+ enum StartFlag flag;
+
+ flag = ntohl (msg->start_flag);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received ATS_START (%d) message\n",
+ (int) flag);
+ switch (flag)
+ {
+ case START_FLAG_SCHEDULING:
+ if (GNUNET_OK !=
+ GAS_scheduling_add_client (client))
+ {
+ GNUNET_SERVICE_client_drop (client);
+ return;
+ }
+ break;
+ case START_FLAG_PERFORMANCE_WITH_PIC:
+ GAS_performance_add_client (client,
+ flag);
+ break;
+ case START_FLAG_PERFORMANCE_NO_PIC:
+ GAS_performance_add_client (client,
+ flag);
+ break;
+ case START_FLAG_CONNECTION_SUGGESTION:
+ /* This client won't receive messages from us, no need to 'add' */
+ break;
+ default:
+ GNUNET_break (0);
+ GNUNET_SERVICE_client_drop (client);
+ return;
+ }
+ GNUNET_SERVICE_client_continue (client);
+}
- char * plugin;
- struct GNUNET_TRANSPORT_ATS_Information * ats;
-};
/**
- * Head of linked list of all clients to this service.
+ * Handle 'reservation request' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
*/
-static struct ATS_Clients *ac_head;
+static void
+handle_reservation_request (void *cls,
+ const struct ReservationRequestMessage *message)
+{
+ struct GNUNET_SERVICE_Client *client = cls;
-/**
- * Tail of linked list of all clients to this service.
- */
-static struct ATS_Clients *ac_tail;
+ GAS_handle_reservation_request (client,
+ message);
+ GNUNET_SERVICE_client_continue (client);
+}
-static struct GNUNET_CONTAINER_MultiHashMap * addresses;
-int address_it (void *cls,
- const GNUNET_HashCode * key,
- void *value)
+/**
+ * Check 'preference feedback' message is well-formed
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ * @return #GNUNET_OK if @a message is well-formed
+ */
+static int
+check_feedback (void *cls,
+ const struct FeedbackPreferenceMessage *message)
{
- struct ATS_Address * aa = cls;
- GNUNET_free (aa);
+ uint16_t msize;
+ uint32_t nump;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received PREFERENCE_FEEDBACK message\n");
+ msize = ntohs (message->header.size);
+ nump = ntohl (message->num_feedback);
+ if (msize !=
+ sizeof (struct FeedbackPreferenceMessage) +
+ nump * sizeof (struct PreferenceInformation))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
return GNUNET_OK;
}
+
/**
- * Task run during shutdown.
+ * Handle 'preference feedback' messages from clients.
*
- * @param cls unused
- * @param tc unused
+ * @param cls client that sent the request
+ * @param msg the request message
*/
static void
-cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+handle_feedback (void *cls,
+ const struct FeedbackPreferenceMessage *msg)
{
- struct ATS_Clients * t;
-
- while (ac_head != NULL)
+ struct GNUNET_SERVICE_Client *client = cls;
+ const struct PreferenceInformation *pi;
+ uint32_t nump;
+
+ nump = ntohl (msg->num_feedback);
+ if (GNUNET_NO ==
+ GNUNET_CONTAINER_multipeermap_contains (GSA_addresses,
+ &msg->peer))
{
- t = ac_head;
- GNUNET_CONTAINER_DLL_remove(ac_head,ac_tail, t);
- GNUNET_free (t);
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+ "Received PREFERENCE FEEDBACK for unknown peer `%s'\n",
+ GNUNET_i2s (&msg->peer));
+ GNUNET_SERVICE_client_continue (client);
+ return;
}
- GNUNET_CONTAINER_multihashmap_iterate (addresses, address_it, NULL);
-
- GNUNET_CONTAINER_multihashmap_destroy (addresses);
-}
-
-static struct ATS_Clients * find_client (struct GNUNET_SERVER_Client *client)
-{
- struct ATS_Clients * ac = ac_head;
- while (ac != NULL)
+ GNUNET_STATISTICS_update (GSA_stats,
+ "# preference feedbacks requests processed",
+ 1,
+ GNUNET_NO);
+ pi = (const struct PreferenceInformation *) &msg[1];
+ for (uint32_t i = 0; i < nump; i++)
{
- if (ac->client == client)
- break;
- ac = ac->next;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received PREFERENCE FEEDBACK for peer `%s'\n",
+ GNUNET_i2s (&msg->peer));
+ GAS_plugin_notify_feedback (client,
+ &msg->peer,
+ GNUNET_TIME_relative_ntoh (msg->scope),
+ (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind),
+ pi[i].preference_value);
}
- return ac;
+ GNUNET_SERVICE_client_continue (client);
}
-static void
-handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *message)
+/**
+ * Handle 'request address list' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_request_address_list (void *cls,
+ const struct AddressListRequestMessage *message)
{
- struct ClientStartMessage * msg = (struct ClientStartMessage *) message;
- struct ATS_Clients * ac = NULL;
+ struct GNUNET_SERVICE_Client *client = cls;
+ GAS_handle_request_address_list (client,
+ message);
+ GNUNET_SERVICE_client_continue (client);
+}
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
-
- GNUNET_assert (find_client(client) == NULL);
- ac = GNUNET_malloc (sizeof (struct ATS_Clients));
- ac->client = client;
- ac->flags = ntohl (msg->start_flag);
+/**
+ * Handle 'request address' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_request_address (void *cls,
+ const struct RequestAddressMessage * message)
+{
+ struct GNUNET_SERVICE_Client *client = cls;
- GNUNET_CONTAINER_DLL_insert(ac_head, ac_tail, ac);
+ GAS_handle_request_address (client,
+ message);
+ GNUNET_SERVICE_client_continue (client);
}
-static void
-handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *message)
+/**
+ * Cancel 'request address' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_request_address_cancel (void *cls,
+ const struct RequestAddressMessage *message)
{
- // struct RequestAddressMessage * msg = (struct RequestAddressMessage *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
+ struct GNUNET_SERVICE_Client *client = cls;
+ GAS_handle_request_address_cancel (client,
+ message);
+ GNUNET_SERVICE_client_continue (client);
+}
+
+
+/**
+ * Handle 'address add' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param m the request message
+ */
+static int
+check_address_add (void *cls,
+ const struct AddressAddMessage *m)
+{
+ const char *address;
+ const char *plugin_name;
+ uint16_t address_length;
+ uint16_t plugin_name_length;
+ uint16_t size;
+
+ size = ntohs (m->header.size);
+ address_length = ntohs (m->address_length);
+ plugin_name_length = ntohs (m->plugin_name_length);
+ address = (const char *) &m[1];
+ if (plugin_name_length != 0)
+ plugin_name = &address[address_length];
+ else
+ plugin_name = "";
+
+ if ( (address_length + plugin_name_length +
+ sizeof (struct AddressAddMessage) != size) ||
+ ( (plugin_name_length > 0) &&
+ (plugin_name[plugin_name_length - 1] != '\0') ) )
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
}
-static void
-handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *message)
+/**
+ * Handle 'address add' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_address_add (void *cls,
+ const struct AddressAddMessage *message)
{
- struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_UPDATE");
+ struct GNUNET_SERVICE_Client *client = cls;
- struct GNUNET_TRANSPORT_ATS_Information *am;
- char *pm;
+ GAS_handle_address_add (message);
+ GNUNET_SERVICE_client_continue (client);
+}
- size_t size = ntohs (msg->header.size);
- if (size <= sizeof (struct AddressUpdateMessage))
- GNUNET_break (0);
- size_t ats_count = ntohs (msg->ats_count);
- size_t addr_len = ntohs (msg->address_length);
- size_t plugin_len = ntohs (msg->plugin_name_length) + 1 ;
+/**
+ * Handle 'address update' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_address_update (void *cls,
+ const struct AddressUpdateMessage *message)
+{
+ struct GNUNET_SERVICE_Client *client = cls;
- struct ATS_Address * aa = GNUNET_malloc (sizeof (struct ATS_Address) +
- ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
- addr_len +
- plugin_len);
+ GAS_handle_address_update (message);
+ GNUNET_SERVICE_client_continue (client);
+}
+/**
+ * Handle 'address destroyed' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_address_destroyed (void *cls,
+ const struct AddressDestroyedMessage *message)
+{
+ struct GNUNET_SERVICE_Client *client = cls;
- memcpy (&aa->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
- aa->addr_len = addr_len;
- aa->ats_count = ats_count;
- aa->ats = (struct GNUNET_TRANSPORT_ATS_Information *) &aa[1];
+ GAS_handle_address_destroyed (message);
+ GNUNET_SERVICE_client_continue (client);
+}
- am = (struct GNUNET_TRANSPORT_ATS_Information*) &msg[1];
- memcpy (&aa->ats, am, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
- pm = (char *) &am[ats_count];
- memcpy (aa->addr, pm, addr_len);
- memcpy (aa->plugin, &pm[plugin_len], plugin_len);
- aa->session_id = ntohl(msg->session_id);
- GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(addresses, &aa->peer.hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+/**
+ * Check that 'change preference' message is well-formed.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ * @return #GNUNET_OK if @a message is well-formed
+ */
+static int
+check_preference_change (void *cls,
+ const struct ChangePreferenceMessage *message)
+{
+ uint16_t msize;
+ uint32_t nump;
+
+ msize = ntohs (message->header.size);
+ nump = ntohl (message->num_preferences);
+ if ( (msize !=
+ sizeof (struct ChangePreferenceMessage) +
+ nump * sizeof (struct PreferenceInformation)) ||
+ (UINT16_MAX / sizeof (struct PreferenceInformation) < nump) )
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
}
-static void
-handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *message)
+/**
+ * Handle 'change preference' messages from clients.
+ *
+ * @param cls client that sent the request
+ * @param message the request message
+ */
+static void
+handle_preference_change (void *cls,
+ const struct ChangePreferenceMessage *message)
{
- // struct AddressDestroyedMessage * msg = (struct AddressDestroyedMessage *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_DESTROYED");
+ struct GNUNET_SERVICE_Client *client = cls;
+ GAS_handle_preference_change (client,
+ message);
+ GNUNET_SERVICE_client_continue (client);
}
-static void
-handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *message)
+/**
+ * A client connected to us. Setup the local client
+ * record.
+ *
+ * @param cls unused
+ * @param client handle of the client
+ * @param mq message queue to talk to @a client
+ * @return @a client
+ */
+static void *
+client_connect_cb (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ struct GNUNET_MQ_Handle *mq)
{
- // struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
+ return client;
}
+
+/**
+ * A client disconnected from us. Tear down the local client
+ * record.
+ *
+ * @param cls unused
+ * @param client handle of the client
+ * @param app_ctx
+ */
static void
-handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
- const struct GNUNET_MessageHeader *message)
+client_disconnect_cb (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ void *app_ctx)
+{
+ if (NULL == client)
+ return;
+ GAS_scheduling_remove_client (client);
+ GAS_connectivity_remove_client (client);
+ GAS_preference_client_disconnect (client);
+}
+
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ */
+static void
+cleanup_task (void *cls)
{
- // struct ChangePreferenceMessage * msg = (struct ChangePreferenceMessage *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ATS shutdown initiated\n");
+ GAS_connectivity_done ();
+ GAS_addresses_done ();
+ GAS_plugin_done ();
+ GAS_normalization_stop ();
+ GAS_performance_done ();
+ GAS_preference_done ();
+ GAS_reservations_done ();
+ if (NULL != GSA_stats)
+ {
+ GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
+ GSA_stats = NULL;
+ }
}
+
/**
* Process template requests.
*
* @param cls closure
- * @param server the initialized server
* @param cfg configuration to use
+ * @param service the initialized service
*/
static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
+run (void *cls,
+ const struct GNUNET_CONFIGURATION_Handle *cfg,
+ struct GNUNET_SERVICE_Handle *service)
{
- static const struct GNUNET_SERVER_MessageHandler handlers[] = {
- {&handle_ats_start, NULL, GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
- {&handle_request_address, NULL, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
- {&handle_address_update, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
- {&handle_address_destroyed, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
- {&handle_reservation_request, NULL, GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
- {&handle_preference_change, NULL, GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
- {NULL, NULL, 0, 0}
- };
-
- addresses = GNUNET_CONTAINER_multihashmap_create(100);
-
- GNUNET_SERVER_add_handlers (server, handlers);
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
- NULL);
+ GSA_stats = GNUNET_STATISTICS_create ("ats",
+ cfg);
+ GAS_reservations_init ();
+ GAS_connectivity_init ();
+ GAS_preference_init ();
+ GAS_normalization_start ();
+ GAS_addresses_init ();
+ if (GNUNET_OK !=
+ GAS_plugin_init (cfg))
+ {
+ GNUNET_break (0);
+ GAS_addresses_done ();
+ GAS_normalization_stop ();
+ GAS_reservations_done ();
+ GAS_connectivity_done ();
+ GAS_preference_done ();
+ if (NULL != GSA_stats)
+ {
+ GNUNET_STATISTICS_destroy (GSA_stats,
+ GNUNET_NO);
+ GSA_stats = NULL;
+ }
+ return;
+ }
+ GAS_performance_init ();
+ GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
+ NULL);
}
/**
- * The main function for the ats service.
- *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * Define "main" method using service macro.
*/
-int
-main (int argc, char *const *argv)
-{
- return (GNUNET_OK ==
- GNUNET_SERVICE_run (argc, argv, "ats",
- GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
-}
+GNUNET_SERVICE_MAIN
+("ats",
+ GNUNET_SERVICE_OPTION_NONE,
+ &run,
+ &client_connect_cb,
+ &client_disconnect_cb,
+ NULL,
+ GNUNET_MQ_hd_fixed_size (ats_start,
+ GNUNET_MESSAGE_TYPE_ATS_START,
+ struct ClientStartMessage,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (request_address,
+ GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
+ struct RequestAddressMessage,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (request_address_cancel,
+ GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
+ struct RequestAddressMessage,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (request_address_list,
+ GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
+ struct AddressListRequestMessage,
+ NULL),
+ GNUNET_MQ_hd_var_size (address_add,
+ GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD,
+ struct AddressAddMessage,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (address_update,
+ GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE,
+ struct AddressUpdateMessage,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (address_destroyed,
+ GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED,
+ struct AddressDestroyedMessage,
+ NULL),
+ GNUNET_MQ_hd_fixed_size (reservation_request,
+ GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
+ struct ReservationRequestMessage,
+ NULL),
+ GNUNET_MQ_hd_var_size (preference_change,
+ GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE,
+ struct ChangePreferenceMessage,
+ NULL),
+ GNUNET_MQ_hd_var_size (feedback,
+ GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK,
+ struct FeedbackPreferenceMessage,
+ NULL),
+ GNUNET_MQ_handler_end ());
+
/* end of gnunet-service-ats.c */