2 This file is part of GNUnet.
3 (C) 2011 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;
46 * Handle to address subsystem
48 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 plugin_addr
95 * @param session_id session ID to use for the given client (other clients will see 0)
96 * @param atsi performance data for the address
97 * @param atsi_count number of performance records in 'ats'
98 * @param bandwidth_out assigned outbound bandwidth
99 * @param bandwidth_in assigned inbound bandwidth
102 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity
103 *peer, const char *plugin_name,
104 const void *plugin_addr,
105 size_t plugin_addr_len,
107 const struct GNUNET_ATS_Information
108 *atsi, uint32_t atsi_count,
109 struct GNUNET_BANDWIDTH_Value32NBO
111 struct GNUNET_BANDWIDTH_Value32NBO
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->session_id = htonl (session_id);
140 msg->bandwidth_out = bandwidth_out;
141 msg->bandwidth_in = bandwidth_in;
142 atsp = (struct GNUNET_ATS_Information *) &msg[1];
143 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
144 addrp = (char *) &atsp[atsi_count];
145 memcpy (addrp, plugin_addr, plugin_addr_len);
146 strcpy (&addrp[plugin_addr_len], plugin_name);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "ATS sends quota for peer `%s': (in/out) %u/%u\n",
150 GNUNET_i2s (peer), ntohl (bandwidth_in.value__),
151 ntohl (bandwidth_out.value__));
153 GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
159 * Handle 'request address' messages from clients.
161 * @param cls unused, NULL
162 * @param client client that sent the request
163 * @param message the request message
166 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
167 const struct GNUNET_MessageHeader *message)
169 const struct RequestAddressMessage *msg =
170 (const struct RequestAddressMessage *) message;
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
174 GNUNET_break (0 == ntohl (msg->reserved));
175 GAS_addresses_request_address (address_handle, &msg->peer);
176 GNUNET_SERVER_receive_done (client, GNUNET_OK);
181 * Handle 'request address' messages from clients.
183 * @param cls unused, NULL
184 * @param client client that sent the request
185 * @param message the request message
188 GAS_handle_request_address_cancel (void *cls,
189 struct GNUNET_SERVER_Client *client,
190 const struct GNUNET_MessageHeader *message)
192 const struct RequestAddressMessage *msg =
193 (const struct RequestAddressMessage *) message;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
196 "REQUEST_ADDRESS_CANCEL");
197 GNUNET_break (0 == ntohl (msg->reserved));
199 GAS_addresses_request_address_cancel (address_handle, &msg->peer);
201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
205 * Handle 'reset backoff' messages from clients.
207 * @param cls unused, NULL
208 * @param client client that sent the request
209 * @param message the request message
212 GAS_handle_reset_backoff (void *cls,
213 struct GNUNET_SERVER_Client *client,
214 const struct GNUNET_MessageHeader *message)
216 const struct ResetBackoffMessage *msg =
217 (const struct ResetBackoffMessage *) message;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
221 GNUNET_break (0 == ntohl (msg->reserved));
222 GAS_addresses_handle_backoff_reset (address_handle, &msg->peer);
223 GNUNET_SERVER_receive_done (client, GNUNET_OK);
227 * Handle 'address add' messages from clients.
229 * @param cls unused, NULL
230 * @param client client that sent the request
231 * @param message the request message
234 GAS_handle_address_add (void *cls, struct GNUNET_SERVER_Client *client,
235 const struct GNUNET_MessageHeader *message)
237 const struct AddressUpdateMessage *m;
238 const struct GNUNET_ATS_Information *atsi;
240 const char *plugin_name;
241 uint16_t address_length;
242 uint16_t plugin_name_length;
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
248 size = ntohs (message->size);
249 if (size < sizeof (struct AddressUpdateMessage))
252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
255 m = (const struct AddressUpdateMessage *) message;
256 ats_count = ntohl (m->ats_count);
257 address_length = ntohs (m->address_length);
258 plugin_name_length = ntohs (m->plugin_name_length);
259 atsi = (const struct GNUNET_ATS_Information *) &m[1];
260 address = (const char *) &atsi[ats_count];
261 if (plugin_name_length != 0)
262 plugin_name = &address[address_length];
266 if ((address_length + plugin_name_length +
267 ats_count * sizeof (struct GNUNET_ATS_Information) +
268 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
270 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
271 ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
277 GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
279 GAS_addresses_add (address_handle, &m->peer, plugin_name, address, address_length,
280 ntohl (m->session_id), atsi, ats_count);
281 GNUNET_SERVER_receive_done (client, GNUNET_OK);
286 * Handle 'address update' messages from clients.
288 * @param cls unused, NULL
289 * @param client client that sent the request
290 * @param message the request message
293 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
294 const struct GNUNET_MessageHeader *message)
296 const struct AddressUpdateMessage *m;
297 const struct GNUNET_ATS_Information *atsi;
299 const char *plugin_name;
300 uint16_t address_length;
301 uint16_t plugin_name_length;
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
307 size = ntohs (message->size);
308 if (size < sizeof (struct AddressUpdateMessage))
311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
314 m = (const struct AddressUpdateMessage *) message;
315 ats_count = ntohl (m->ats_count);
316 address_length = ntohs (m->address_length);
317 plugin_name_length = ntohs (m->plugin_name_length);
318 atsi = (const struct GNUNET_ATS_Information *) &m[1];
319 address = (const char *) &atsi[ats_count];
320 if (plugin_name_length != 0)
321 plugin_name = &address[address_length];
325 if ((address_length + plugin_name_length +
326 ats_count * sizeof (struct GNUNET_ATS_Information) +
327 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
329 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
330 ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
336 GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
338 GAS_addresses_update (address_handle, &m->peer, plugin_name, address, address_length,
339 ntohl (m->session_id), atsi, ats_count);
340 GNUNET_SERVER_receive_done (client, GNUNET_OK);
345 * Handle 'address in use' messages from clients.
347 * @param cls unused, NULL
348 * @param client client that sent the request
349 * @param message the request message
352 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
353 const struct GNUNET_MessageHeader *message)
355 const struct AddressUseMessage *m;
357 const char *plugin_name;
359 uint16_t address_length;
360 uint16_t plugin_name_length;
365 size = ntohs (message->size);
366 if (size < sizeof (struct AddressUseMessage))
369 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
372 m = (const struct AddressUseMessage *) message;
374 address_length = ntohs (m->address_length);
375 plugin_name_length = ntohs (m->plugin_name_length);
377 address = (const char *) &m[1];
378 if (plugin_name_length != 0)
379 plugin_name = &address[address_length];
383 if ((address_length + plugin_name_length +
384 sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
385 ((plugin_name_length > 0) &&
386 (plugin_name[plugin_name_length - 1] != '\0')))
389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393 in_use = ntohs (m->in_use);
394 res = GAS_addresses_in_use (address_handle,
399 ntohl (m->session_id),
402 if (res == GNUNET_OK)
403 GNUNET_SERVER_receive_done (client, GNUNET_OK);
407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
413 * Handle 'address destroyed' messages from clients.
415 * @param cls unused, NULL
416 * @param client client that sent the request
417 * @param message the request message
420 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
421 const struct GNUNET_MessageHeader *message)
423 const struct AddressDestroyedMessage *m;
424 struct SessionReleaseMessage srm;
426 const char *plugin_name;
427 uint16_t address_length;
428 uint16_t plugin_name_length;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
432 "ADDRESS_DESTROYED", ntohs (message->size),
433 sizeof (struct AddressDestroyedMessage));
434 size = ntohs (message->size);
435 if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
441 m = (const struct AddressDestroyedMessage *) message;
442 GNUNET_break (0 == ntohl (m->reserved));
443 address_length = ntohs (m->address_length);
444 plugin_name_length = ntohs (m->plugin_name_length);
445 address = (const char *) &m[1];
446 if (plugin_name_length != 0)
447 plugin_name = &address[address_length];
450 if ((address_length + plugin_name_length +
451 sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
457 if ((plugin_name_length == 0) ||
458 (plugin_name[plugin_name_length - 1] != '\0'))
461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
464 GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
465 GAS_addresses_destroy (address_handle, &m->peer, plugin_name,
466 address, address_length,
467 ntohl (m->session_id));
468 if (0 != ntohl (m->session_id))
470 srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
471 srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
472 srm.session_id = m->session_id;
474 GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
482 * Initialize scheduling subsystem.
484 * @param server handle to our server
485 * @param ah the address handle to use
488 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server, struct GAS_Addresses_Handle *ah)
491 nc = GNUNET_SERVER_notification_context_create (server, 128);
496 * Shutdown scheduling subsystem.
499 GAS_scheduling_done ()
501 if (NULL != my_client)
505 GNUNET_SERVER_notification_context_destroy (nc);
511 /* end of gnunet-service-ats_scheduling.c */