2 This file is part of GNUnet.
3 (C) 2011-2014 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_scheduling.c
23 * @brief ats service, interaction with 'scheduling' API
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_scheduling.h"
35 * Context for sending messages to clients.
37 static struct GNUNET_SERVER_NotificationContext *nc;
40 * Actual handle to the client.
42 static struct GNUNET_SERVER_Client *my_client;
45 * Handle to address subsystem
47 static struct GAS_Addresses_Handle *address_handle;
51 * Register a new scheduling client.
53 * @param client handle of the new client
54 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
57 GAS_scheduling_add_client (struct GNUNET_SERVER_Client *client)
59 if (my_client != NULL)
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
66 GNUNET_SERVER_notification_context_add (nc, client);
72 * Unregister a client (which may have been a scheduling client,
73 * but this is not assured).
75 * @param client handle of the (now dead) client
78 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
80 if (my_client != client)
82 GAS_addresses_destroy_all (address_handle);
88 * Transmit the given address suggestion and bandwidth update to all scheduling
91 * @param peer peer for which this is an address suggestion
92 * @param plugin_name 0-termintated string specifying the transport plugin
93 * @param plugin_addr binary address for the plugin to use
94 * @param plugin_addr_len number of bytes in @a plugin_addr
95 * @param local_address_info the local address for the address
96 * @param session_id session ID to use for the given client (other clients will see 0)
97 * @param atsi performance data for the address
98 * @param atsi_count number of performance records in @a atsi
99 * @param bandwidth_out assigned outbound bandwidth
100 * @param bandwidth_in assigned inbound bandwidth
103 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity *peer,
104 const char *plugin_name,
105 const void *plugin_addr,
106 size_t plugin_addr_len,
107 uint32_t local_address_info,
109 const struct GNUNET_ATS_Information *atsi,
111 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
112 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
114 struct AddressSuggestionMessage *msg;
115 size_t plugin_name_length = strlen (plugin_name) + 1;
117 sizeof (struct AddressSuggestionMessage) +
118 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
120 char buf[msize] GNUNET_ALIGN;
121 struct GNUNET_ATS_Information *atsp;
124 if (my_client == NULL)
126 GNUNET_STATISTICS_update (GSA_stats, "# address suggestions made", 1,
128 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
129 GNUNET_assert (atsi_count <
130 GNUNET_SERVER_MAX_MESSAGE_SIZE /
131 sizeof (struct GNUNET_ATS_Information));
132 msg = (struct AddressSuggestionMessage *) buf;
133 msg->header.size = htons (msize);
134 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
135 msg->ats_count = htonl (atsi_count);
137 msg->address_length = htons (plugin_addr_len);
138 msg->plugin_name_length = htons (plugin_name_length);
139 msg->address_local_info = htonl (local_address_info);
140 msg->session_id = htonl (session_id);
141 msg->bandwidth_out = bandwidth_out;
142 msg->bandwidth_in = bandwidth_in;
143 atsp = (struct GNUNET_ATS_Information *) &msg[1];
144 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
145 addrp = (char *) &atsp[atsi_count];
146 memcpy (addrp, plugin_addr, plugin_addr_len);
147 strcpy (&addrp[plugin_addr_len], plugin_name);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "ATS sends quota for peer `%s': (in/out) %u/%u\n",
152 (unsigned int) ntohl (bandwidth_in.value__),
153 (unsigned int) ntohl (bandwidth_out.value__));
155 GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
161 * Handle 'request address' messages from clients.
163 * @param cls unused, NULL
164 * @param client client that sent the request
165 * @param message the request message
168 GAS_handle_request_address (void *cls,
169 struct GNUNET_SERVER_Client *client,
170 const struct GNUNET_MessageHeader *message)
172 const struct RequestAddressMessage *msg =
173 (const struct RequestAddressMessage *) message;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
177 GNUNET_break (0 == ntohl (msg->reserved));
178 GAS_addresses_request_address (address_handle, &msg->peer);
179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
184 * Handle 'request address' messages from clients.
186 * @param cls unused, NULL
187 * @param client client that sent the request
188 * @param message the request message
191 GAS_handle_request_address_cancel (void *cls,
192 struct GNUNET_SERVER_Client *client,
193 const struct GNUNET_MessageHeader *message)
195 const struct RequestAddressMessage *msg =
196 (const struct RequestAddressMessage *) message;
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
199 "REQUEST_ADDRESS_CANCEL");
200 GNUNET_break (0 == ntohl (msg->reserved));
202 GAS_addresses_request_address_cancel (address_handle, &msg->peer);
204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
209 * Handle 'reset backoff' messages from clients.
211 * @param cls unused, NULL
212 * @param client client that sent the request
213 * @param message the request message
216 GAS_handle_reset_backoff (void *cls,
217 struct GNUNET_SERVER_Client *client,
218 const struct GNUNET_MessageHeader *message)
220 const struct ResetBackoffMessage *msg =
221 (const struct ResetBackoffMessage *) message;
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Received `%s' message\n",
226 GNUNET_break (0 == ntohl (msg->reserved));
227 GAS_addresses_handle_backoff_reset (address_handle, &msg->peer);
228 GNUNET_SERVER_receive_done (client, GNUNET_OK);
233 * Handle 'address add' messages from clients.
235 * @param cls unused, NULL
236 * @param client client that sent the request
237 * @param message the request message
240 GAS_handle_address_add (void *cls,
241 struct GNUNET_SERVER_Client *client,
242 const struct GNUNET_MessageHeader *message)
244 const struct AddressUpdateMessage *m;
245 const struct GNUNET_ATS_Information *atsi;
247 const char *plugin_name;
248 uint16_t address_length;
249 uint16_t plugin_name_length;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Received `%s' message\n",
256 size = ntohs (message->size);
257 if (size < sizeof (struct AddressUpdateMessage))
260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
263 m = (const struct AddressUpdateMessage *) message;
264 ats_count = ntohl (m->ats_count);
265 address_length = ntohs (m->address_length);
266 plugin_name_length = ntohs (m->plugin_name_length);
267 atsi = (const struct GNUNET_ATS_Information *) &m[1];
268 address = (const char *) &atsi[ats_count];
269 if (plugin_name_length != 0)
270 plugin_name = &address[address_length];
274 if ((address_length + plugin_name_length +
275 ats_count * sizeof (struct GNUNET_ATS_Information) +
276 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
278 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
279 ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
282 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
285 GNUNET_STATISTICS_update (GSA_stats,
286 "# addresses created", 1,
288 GAS_addresses_add (address_handle,
293 ntohl(m->address_local_info),
294 ntohl (m->session_id),
296 GNUNET_SERVER_receive_done (client, GNUNET_OK);
301 * Handle 'address update' messages from clients.
303 * @param cls unused, NULL
304 * @param client client that sent the request
305 * @param message the request message
308 GAS_handle_address_update (void *cls,
309 struct GNUNET_SERVER_Client *client,
310 const struct GNUNET_MessageHeader *message)
312 const struct AddressUpdateMessage *m;
313 const struct GNUNET_ATS_Information *atsi;
315 const char *plugin_name;
316 uint16_t address_length;
317 uint16_t plugin_name_length;
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Received `%s' message\n",
324 size = ntohs (message->size);
325 if (size < sizeof (struct AddressUpdateMessage))
328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
331 m = (const struct AddressUpdateMessage *) message;
332 ats_count = ntohl (m->ats_count);
333 address_length = ntohs (m->address_length);
334 plugin_name_length = ntohs (m->plugin_name_length);
335 atsi = (const struct GNUNET_ATS_Information *) &m[1];
336 address = (const char *) &atsi[ats_count];
337 if (plugin_name_length != 0)
338 plugin_name = &address[address_length];
342 if ((address_length + plugin_name_length +
343 ats_count * sizeof (struct GNUNET_ATS_Information) +
344 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
346 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
347 ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
353 GNUNET_STATISTICS_update (GSA_stats,
354 "# address updates received",
357 GAS_addresses_update (address_handle,
362 ntohl (m->address_local_info),
363 ntohl (m->session_id), atsi, ats_count);
364 GNUNET_SERVER_receive_done (client, GNUNET_OK);
369 * Handle 'address in use' messages from clients.
371 * @param cls unused, NULL
372 * @param client client that sent the request
373 * @param message the request message
376 GAS_handle_address_in_use (void *cls,
377 struct GNUNET_SERVER_Client *client,
378 const struct GNUNET_MessageHeader *message)
380 const struct AddressUseMessage *m;
382 const char *plugin_name;
384 uint16_t address_length;
385 uint16_t plugin_name_length;
389 size = ntohs (message->size);
390 if (size < sizeof (struct AddressUseMessage))
393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
396 m = (const struct AddressUseMessage *) message;
398 address_length = ntohs (m->address_length);
399 plugin_name_length = ntohs (m->plugin_name_length);
401 address = (const char *) &m[1];
402 if (plugin_name_length != 0)
403 plugin_name = &address[address_length];
407 if ((address_length + plugin_name_length +
408 sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
409 ((plugin_name_length > 0) &&
410 (plugin_name[plugin_name_length - 1] != '\0')))
413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
417 in_use = ntohs (m->in_use);
418 res = GAS_addresses_in_use (address_handle,
423 ntohl (m->address_local_info),
424 ntohl (m->session_id),
427 if (GNUNET_OK != res)
430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
438 * Handle 'address destroyed' messages from clients.
440 * @param cls unused, NULL
441 * @param client client that sent the request
442 * @param message the request message
445 GAS_handle_address_destroyed (void *cls,
446 struct GNUNET_SERVER_Client *client,
447 const struct GNUNET_MessageHeader *message)
449 const struct AddressDestroyedMessage *m;
450 struct SessionReleaseMessage srm;
452 const char *plugin_name;
453 uint16_t address_length;
454 uint16_t plugin_name_length;
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Received `%s' message of size %u %u\n",
460 ntohs (message->size),
461 sizeof (struct AddressDestroyedMessage));
462 size = ntohs (message->size);
463 if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
469 m = (const struct AddressDestroyedMessage *) message;
470 GNUNET_break (0 == ntohl (m->reserved));
471 address_length = ntohs (m->address_length);
472 plugin_name_length = ntohs (m->plugin_name_length);
473 address = (const char *) &m[1];
474 if (plugin_name_length != 0)
475 plugin_name = &address[address_length];
478 if ((address_length + plugin_name_length +
479 sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
485 if ((plugin_name_length == 0) ||
486 (plugin_name[plugin_name_length - 1] != '\0'))
489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
492 GNUNET_STATISTICS_update (GSA_stats,
493 "# addresses destroyed",
496 GAS_addresses_destroy (address_handle, &m->peer, plugin_name,
497 address, address_length,
498 ntohl (m->address_local_info),
499 ntohl (m->session_id));
500 if (0 != ntohl (m->session_id))
502 srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
503 srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
504 srm.session_id = m->session_id;
506 GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
509 GNUNET_SERVER_receive_done (client, GNUNET_OK);
514 * Initialize scheduling subsystem.
516 * @param server handle to our server
517 * @param ah the address handle to use
520 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server,
521 struct GAS_Addresses_Handle *ah)
523 GNUNET_assert (NULL != ah);
525 nc = GNUNET_SERVER_notification_context_create (server, 128);
530 * Shutdown scheduling subsystem.
533 GAS_scheduling_done ()
535 if (NULL != my_client)
539 GNUNET_SERVER_notification_context_destroy (nc);
544 /* end of gnunet-service-ats_scheduling.c */