2 This file is part of GNUnet.
3 (C) 2009 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.c
24 * @author Matthias Wachs
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_ats_service.h"
35 struct ATS_Clients * next;
37 struct ATS_Clients * prev;
39 struct GNUNET_SERVER_Client *client;
46 struct GNUNET_PeerIdentity peer;
58 struct GNUNET_TRANSPORT_ATS_Information * ats;
62 * Head of linked list of all clients to this service.
64 static struct ATS_Clients *ac_head;
67 * Tail of linked list of all clients to this service.
69 static struct ATS_Clients *ac_tail;
71 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
73 int address_it (void *cls,
74 const GNUNET_HashCode * key,
77 struct ATS_Address * aa = cls;
83 * Task run during shutdown.
89 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 struct ATS_Clients * t;
93 while (ac_head != NULL)
96 GNUNET_CONTAINER_DLL_remove(ac_head,ac_tail, t);
100 GNUNET_CONTAINER_multihashmap_iterate (addresses, address_it, NULL);
102 GNUNET_CONTAINER_multihashmap_destroy (addresses);
105 static struct ATS_Clients * find_client (struct GNUNET_SERVER_Client *client)
107 struct ATS_Clients * ac = ac_head;
110 if (ac->client == client)
118 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
119 const struct GNUNET_MessageHeader *message)
122 struct ClientStartMessage * msg = (struct ClientStartMessage *) message;
123 struct ATS_Clients * ac = NULL;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
128 GNUNET_assert (find_client(client) == NULL);
130 ac = GNUNET_malloc (sizeof (struct ATS_Clients));
132 ac->flags = ntohl (msg->start_flag);
134 GNUNET_CONTAINER_DLL_insert(ac_head, ac_tail, ac);
138 handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
139 const struct GNUNET_MessageHeader *message)
142 struct RequestAddressMessage * msg = (struct RequestAddressMessage *) message;
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
148 handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
149 const struct GNUNET_MessageHeader *message)
152 struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_UPDATE");
155 struct GNUNET_TRANSPORT_ATS_Information *am;
158 size_t size = ntohs (msg->header.size);
159 if (size <= sizeof (struct AddressUpdateMessage))
162 size_t ats_count = ntohs (msg->ats_count);
163 size_t addr_len = ntohs (msg->address_length);
164 size_t plugin_len = ntohs (msg->plugin_name_length) + 1 ;
166 struct ATS_Address * aa = GNUNET_malloc (sizeof (struct ATS_Address) +
167 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
173 memcpy (&aa->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
174 aa->addr_len = addr_len;
175 aa->ats_count = ats_count;
176 aa->ats = (struct GNUNET_TRANSPORT_ATS_Information *) &aa[1];
178 am = (struct GNUNET_TRANSPORT_ATS_Information*) &msg[1];
179 memcpy (&aa->ats, am, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
180 pm = (char *) &am[ats_count];
181 memcpy (aa->addr, pm, addr_len);
182 memcpy (aa->plugin, &pm[plugin_len], plugin_len);
183 aa->session_id = ntohl(msg->session_id);
185 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(addresses, &aa->peer.hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
189 handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
190 const struct GNUNET_MessageHeader *message)
193 struct AddressDestroyedMessage * msg = (struct AddressDestroyedMessage *) message;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_DESTROYED");
199 handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
200 const struct GNUNET_MessageHeader *message)
203 struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
208 handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
209 const struct GNUNET_MessageHeader *message)
212 struct ChangePreferenceMessage * msg = (struct ChangePreferenceMessage *) message;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
217 * Process template requests.
220 * @param server the initialized server
221 * @param cfg configuration to use
224 run (void *cls, struct GNUNET_SERVER_Handle *server,
225 const struct GNUNET_CONFIGURATION_Handle *cfg)
227 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
228 {&handle_ats_start, NULL, GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
229 {&handle_request_address, NULL, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
230 {&handle_address_update, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
231 {&handle_address_destroyed, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
232 {&handle_reservation_request, NULL, GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
233 {&handle_preference_change, NULL, GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
237 addresses = GNUNET_CONTAINER_multihashmap_create(100);
239 GNUNET_SERVER_add_handlers (server, handlers);
240 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
246 * The main function for the ats service.
248 * @param argc number of arguments from the command line
249 * @param argv command line arguments
250 * @return 0 ok, 1 on error
253 main (int argc, char *const *argv)
256 GNUNET_SERVICE_run (argc, argv, "ats",
257 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
260 /* end of gnunet-service-ats.c */