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 free_address_it (void *cls,
74 const GNUNET_HashCode * key,
77 struct ATS_Address * aa = cls;
82 struct CompareAddressContext
84 struct ATS_Address * search;
85 struct ATS_Address * result;
88 int compare_address_it (void *cls,
89 const GNUNET_HashCode * key,
92 struct CompareAddressContext * cac = cls;
93 struct ATS_Address * aa = (struct ATS_Address *) value;
94 if (0 == strcmp(aa->plugin, cac->search->plugin))
96 if ((aa->addr_len == cac->search->addr_len) &&
97 (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)))
105 * Task run during shutdown.
111 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 struct ATS_Clients * t;
115 while (ac_head != NULL)
118 GNUNET_CONTAINER_DLL_remove(ac_head,ac_tail, t);
122 GNUNET_CONTAINER_multihashmap_iterate (addresses, free_address_it, NULL);
124 GNUNET_CONTAINER_multihashmap_destroy (addresses);
127 static struct ATS_Clients * find_client (struct GNUNET_SERVER_Client *client)
129 struct ATS_Clients * ac = ac_head;
132 if (ac->client == client)
140 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
141 const struct GNUNET_MessageHeader *message)
144 struct ClientStartMessage * msg = (struct ClientStartMessage *) message;
145 struct ATS_Clients * ac = NULL;
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
150 GNUNET_assert (find_client(client) == NULL);
152 ac = GNUNET_malloc (sizeof (struct ATS_Clients));
154 ac->flags = ntohl (msg->start_flag);
156 GNUNET_CONTAINER_DLL_insert(ac_head, ac_tail, ac);
160 handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
161 const struct GNUNET_MessageHeader *message)
164 // struct RequestAddressMessage * msg = (struct RequestAddressMessage *) message;
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
170 handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
171 const struct GNUNET_MessageHeader *message)
174 struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_UPDATE");
177 struct GNUNET_TRANSPORT_ATS_Information *am;
180 size_t size = ntohs (msg->header.size);
181 if ((size <= sizeof (struct AddressUpdateMessage)) || (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
187 size_t ats_count = ntohs (msg->ats_count);
188 size_t addr_len = ntohs (msg->address_length);
189 size_t plugin_len = ntohs (msg->plugin_name_length) + 1 ;
192 (plugin_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
193 (addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
194 (addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
200 struct ATS_Address * aa = GNUNET_malloc (sizeof (struct ATS_Address) +
201 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
207 memcpy (&aa->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
208 aa->addr_len = addr_len;
209 aa->ats_count = ats_count;
210 aa->ats = (struct GNUNET_TRANSPORT_ATS_Information *) &aa[1];
212 am = (struct GNUNET_TRANSPORT_ATS_Information*) &msg[1];
213 memcpy (&aa->ats, am, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
214 pm = (char *) &am[ats_count];
215 memcpy (aa->addr, pm, addr_len);
216 memcpy (aa->plugin, &pm[plugin_len], plugin_len);
217 aa->session_id = ntohl(msg->session_id);
219 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(addresses, &aa->peer.hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
223 handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
224 const struct GNUNET_MessageHeader *message)
227 // struct AddressDestroyedMessage * msg = (struct AddressDestroyedMessage *) message;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_DESTROYED");
230 struct GNUNET_PeerIdentity *peer = &msg->peer;
231 struct ATS_Address * aa = find_address_by_addr (peer);
232 GNUNET_CONTAINER_multihashmap_remove(addresses, peer, aa);
237 handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
238 const struct GNUNET_MessageHeader *message)
241 // struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
246 handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
247 const struct GNUNET_MessageHeader *message)
250 // struct ChangePreferenceMessage * msg = (struct ChangePreferenceMessage *) message;
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
255 * Process template requests.
258 * @param server the initialized server
259 * @param cfg configuration to use
262 run (void *cls, struct GNUNET_SERVER_Handle *server,
263 const struct GNUNET_CONFIGURATION_Handle *cfg)
265 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
266 {&handle_ats_start, NULL, GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
267 {&handle_request_address, NULL, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
268 {&handle_address_update, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
269 {&handle_address_destroyed, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
270 {&handle_reservation_request, NULL, GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
271 {&handle_preference_change, NULL, GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
275 addresses = GNUNET_CONTAINER_multihashmap_create(100);
277 GNUNET_SERVER_add_handlers (server, handlers);
278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
284 * The main function for the ats service.
286 * @param argc number of arguments from the command line
287 * @param argv command line arguments
288 * @return 0 ok, 1 on error
291 main (int argc, char *const *argv)
294 GNUNET_SERVICE_run (argc, argv, "ats",
295 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
298 /* end of gnunet-service-ats.c */