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"
34 struct GNUNET_SERVER_Client *client;
41 struct GNUNET_PeerIdentity peer;
53 struct GNUNET_TRANSPORT_ATS_Information * ats;
57 * Head of linked list of all clients to this service.
59 static struct ATS_Clients *ac_head;
62 * Tail of linked list of all clients to this service.
64 static struct ATS_Clients *ac_tail;
66 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
68 int address_it (void *cls,
69 const GNUNET_HashCode * key,
72 struct ATS_Address * aa = cls;
78 * Task run during shutdown.
84 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 struct ATS_Clients * t;
88 while (ac_head != NULL)
91 GNUNET_CONTAINER_DLL_remove(ac_head,ac_tail, t);
95 GNUNET_CONTAINER_multihashmap_iterate(addresses, address_it, NULL);
97 GNUNET_CONTAINER_multihashmap_create(addresses);
101 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
102 const struct GNUNET_MessageHeader *message)
105 struct ClientStartMessage * msg = (struct ClientStartMessage *) msg;
106 struct ATS_Clients * ac = NULL;
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
111 GNUNET_assert (find_client(client) == NULL);
113 ac = GNUNET_malloc (sizeof (struct ATS_Clients));
115 ac->flags = ntohl (msg->start_flag);
117 GNUNET_CONTAINER_DLL_insert(ac_head, ac_tail, ac);
121 handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
122 const struct GNUNET_MessageHeader *message)
125 struct RequestAddressMessage * msg = (struct RequestAddressMessage *) msg;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
131 handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
132 const struct GNUNET_MessageHeader *message)
135 struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) msg;
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_UPDATE");
138 struct GNUNET_TRANSPORT_ATS_Information *am;
141 if (size <= sizeof (struct AddressUpdateMessage))
144 struct ATS_Address * aa = GNUNET_malloc (sizeof (struct ATS_Address) +
145 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
149 size_t size = ntohs (msg->header.size);
150 size_t ats_count = ntohs (msg->ats_count);
151 size_t addr_len = ntohs (msg->address_length);
152 size_t plugin_len = ntohs (msg->plugin_name_length) + 1 ;
154 memcpy (&aa->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
155 aa->addr_len = addr_len;
156 aa->ats_count = ats_count;
159 am = (struct GNUNET_TRANSPORT_ATS_Information*) &msg[1];
160 memcpy (&aa->ats, am, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
161 pm = (char *) &am[ats_count];
162 memcpy (aa->addr, pm, addr_len);
163 memcpy (aa->plugin, &pm[plugin_addr_len], plugin_len);
164 aa->session_id = ntohl(msg->session_id);
166 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(addresses, aa->peer.hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
170 handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
171 const struct GNUNET_MessageHeader *message)
174 struct AddressDestroyedMessage * msg = (struct AddressDestroyedMessage *) msg;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_DESTROYED");
180 handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
181 const struct GNUNET_MessageHeader *message)
184 struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) msg;
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
189 handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
190 const struct GNUNET_MessageHeader *message)
193 struct ChangePreferenceMessage * msg = (struct ChangePreferenceMessage *) msg;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
198 * Process template requests.
201 * @param server the initialized server
202 * @param cfg configuration to use
205 run (void *cls, struct GNUNET_SERVER_Handle *server,
206 const struct GNUNET_CONFIGURATION_Handle *cfg)
208 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
209 {&handle_ats_start, NULL, GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
210 {&handle_request_address, NULL, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
211 {&handle_address_update, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
212 {&handle_address_destroyed, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
213 {&handle_reservation_request, NULL, GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
214 {&handle_preference_change, NULL, GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
218 addresses = GNUNET_CONTAINER_multihashmap_create(100);
220 GNUNET_SERVER_add_handlers (server, handlers);
221 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
227 * The main function for the ats service.
229 * @param argc number of arguments from the command line
230 * @param argv command line arguments
231 * @return 0 ok, 1 on error
234 main (int argc, char *const *argv)
237 GNUNET_SERVICE_run (argc, argv, "gnunet-service-ats",
238 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
241 /* end of gnunet-service-ats.c */