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 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 'ats'
99 * @param bandwidth_out assigned outbound bandwidth
100 * @param bandwidth_in assigned inbound bandwidth
103 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity
105 const char *plugin_name,
106 const void *plugin_addr,
107 size_t plugin_addr_len,
108 uint32_t local_address_info,
110 const struct GNUNET_ATS_Information *atsi,
112 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
113 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
115 struct AddressSuggestionMessage *msg;
116 size_t plugin_name_length = strlen (plugin_name) + 1;
118 sizeof (struct AddressSuggestionMessage) +
119 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
121 char buf[msize] GNUNET_ALIGN;
122 struct GNUNET_ATS_Information *atsp;
125 if (my_client == NULL)
127 GNUNET_STATISTICS_update (GSA_stats, "# address suggestions made", 1,
129 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
130 GNUNET_assert (atsi_count <
131 GNUNET_SERVER_MAX_MESSAGE_SIZE /
132 sizeof (struct GNUNET_ATS_Information));
133 msg = (struct AddressSuggestionMessage *) buf;
134 msg->header.size = htons (msize);
135 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
136 msg->ats_count = htonl (atsi_count);
138 msg->address_length = htons (plugin_addr_len);
139 msg->plugin_name_length = htons (plugin_name_length);
140 msg->address_local_info = htonl (local_address_info);
141 msg->session_id = htonl (session_id);
142 msg->bandwidth_out = bandwidth_out;
143 msg->bandwidth_in = bandwidth_in;
144 atsp = (struct GNUNET_ATS_Information *) &msg[1];
145 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
146 addrp = (char *) &atsp[atsi_count];
147 memcpy (addrp, plugin_addr, plugin_addr_len);
148 strcpy (&addrp[plugin_addr_len], plugin_name);
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "ATS sends quota for peer `%s': (in/out) %u/%u\n",
153 (unsigned int) ntohl (bandwidth_in.value__),
154 (unsigned int) ntohl (bandwidth_out.value__));
156 GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
162 * Handle 'request address' messages from clients.
164 * @param cls unused, NULL
165 * @param client client that sent the request
166 * @param message the request message
169 GAS_handle_request_address (void *cls, 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);
208 * Handle 'reset backoff' messages from clients.
210 * @param cls unused, NULL
211 * @param client client that sent the request
212 * @param message the request message
215 GAS_handle_reset_backoff (void *cls,
216 struct GNUNET_SERVER_Client *client,
217 const struct GNUNET_MessageHeader *message)
219 const struct ResetBackoffMessage *msg =
220 (const struct ResetBackoffMessage *) message;
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
224 GNUNET_break (0 == ntohl (msg->reserved));
225 GAS_addresses_handle_backoff_reset (address_handle, &msg->peer);
226 GNUNET_SERVER_receive_done (client, GNUNET_OK);
230 * Handle 'address add' messages from clients.
232 * @param cls unused, NULL
233 * @param client client that sent the request
234 * @param message the request message
237 GAS_handle_address_add (void *cls, struct GNUNET_SERVER_Client *client,
238 const struct GNUNET_MessageHeader *message)
240 const struct AddressUpdateMessage *m;
241 const struct GNUNET_ATS_Information *atsi;
243 const char *plugin_name;
244 uint16_t address_length;
245 uint16_t plugin_name_length;
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
251 size = ntohs (message->size);
252 if (size < sizeof (struct AddressUpdateMessage))
255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
258 m = (const struct AddressUpdateMessage *) message;
259 ats_count = ntohl (m->ats_count);
260 address_length = ntohs (m->address_length);
261 plugin_name_length = ntohs (m->plugin_name_length);
262 atsi = (const struct GNUNET_ATS_Information *) &m[1];
263 address = (const char *) &atsi[ats_count];
264 if (plugin_name_length != 0)
265 plugin_name = &address[address_length];
269 if ((address_length + plugin_name_length +
270 ats_count * sizeof (struct GNUNET_ATS_Information) +
271 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
273 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
274 ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
280 GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
282 GAS_addresses_add (address_handle, &m->peer, plugin_name, address, address_length,
283 ntohl(m->address_local_info), ntohl (m->session_id), atsi, ats_count);
284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
289 * Handle 'address update' messages from clients.
291 * @param cls unused, NULL
292 * @param client client that sent the request
293 * @param message the request message
296 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
297 const struct GNUNET_MessageHeader *message)
299 const struct AddressUpdateMessage *m;
300 const struct GNUNET_ATS_Information *atsi;
302 const char *plugin_name;
303 uint16_t address_length;
304 uint16_t plugin_name_length;
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
310 size = ntohs (message->size);
311 if (size < sizeof (struct AddressUpdateMessage))
314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
317 m = (const struct AddressUpdateMessage *) message;
318 ats_count = ntohl (m->ats_count);
319 address_length = ntohs (m->address_length);
320 plugin_name_length = ntohs (m->plugin_name_length);
321 atsi = (const struct GNUNET_ATS_Information *) &m[1];
322 address = (const char *) &atsi[ats_count];
323 if (plugin_name_length != 0)
324 plugin_name = &address[address_length];
328 if ((address_length + plugin_name_length +
329 ats_count * sizeof (struct GNUNET_ATS_Information) +
330 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
332 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
333 ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
339 GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
341 GAS_addresses_update (address_handle, &m->peer, plugin_name, address, address_length,
342 ntohl (m->address_local_info),
343 ntohl (m->session_id), atsi, ats_count);
344 GNUNET_SERVER_receive_done (client, GNUNET_OK);
349 * Handle 'address in use' messages from clients.
351 * @param cls unused, NULL
352 * @param client client that sent the request
353 * @param message the request message
356 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
357 const struct GNUNET_MessageHeader *message)
359 const struct AddressUseMessage *m;
361 const char *plugin_name;
363 uint16_t address_length;
364 uint16_t plugin_name_length;
369 size = ntohs (message->size);
370 if (size < sizeof (struct AddressUseMessage))
373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
376 m = (const struct AddressUseMessage *) message;
378 address_length = ntohs (m->address_length);
379 plugin_name_length = ntohs (m->plugin_name_length);
381 address = (const char *) &m[1];
382 if (plugin_name_length != 0)
383 plugin_name = &address[address_length];
387 if ((address_length + plugin_name_length +
388 sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
389 ((plugin_name_length > 0) &&
390 (plugin_name[plugin_name_length - 1] != '\0')))
393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
397 in_use = ntohs (m->in_use);
398 res = GAS_addresses_in_use (address_handle,
403 ntohl (m->address_local_info),
404 ntohl (m->session_id),
407 if (res == GNUNET_OK)
408 GNUNET_SERVER_receive_done (client, GNUNET_OK);
412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
418 * Handle 'address destroyed' messages from clients.
420 * @param cls unused, NULL
421 * @param client client that sent the request
422 * @param message the request message
425 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
426 const struct GNUNET_MessageHeader *message)
428 const struct AddressDestroyedMessage *m;
429 struct SessionReleaseMessage srm;
431 const char *plugin_name;
432 uint16_t address_length;
433 uint16_t plugin_name_length;
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
437 "ADDRESS_DESTROYED", ntohs (message->size),
438 sizeof (struct AddressDestroyedMessage));
439 size = ntohs (message->size);
440 if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
446 m = (const struct AddressDestroyedMessage *) message;
447 GNUNET_break (0 == ntohl (m->reserved));
448 address_length = ntohs (m->address_length);
449 plugin_name_length = ntohs (m->plugin_name_length);
450 address = (const char *) &m[1];
451 if (plugin_name_length != 0)
452 plugin_name = &address[address_length];
455 if ((address_length + plugin_name_length +
456 sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
462 if ((plugin_name_length == 0) ||
463 (plugin_name[plugin_name_length - 1] != '\0'))
466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
469 GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
470 GAS_addresses_destroy (address_handle, &m->peer, plugin_name,
471 address, address_length,
472 ntohl (m->address_local_info),
473 ntohl (m->session_id));
474 if (0 != ntohl (m->session_id))
476 srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
477 srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
478 srm.session_id = m->session_id;
480 GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
483 GNUNET_SERVER_receive_done (client, GNUNET_OK);
488 * Initialize scheduling subsystem.
490 * @param server handle to our server
491 * @param ah the address handle to use
494 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server, struct GAS_Addresses_Handle *ah)
496 GNUNET_assert (NULL != ah);
498 nc = GNUNET_SERVER_notification_context_create (server, 128);
503 * Shutdown scheduling subsystem.
506 GAS_scheduling_done ()
508 if (NULL != my_client)
512 GNUNET_SERVER_notification_context_destroy (nc);
518 /* end of gnunet-service-ats_scheduling.c */