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 * Register a new scheduling client.
48 * @param client handle of the new client
49 * @return GNUNET_OK on success, GNUNET_SYSERR on error
52 GAS_scheduling_add_client (struct GNUNET_SERVER_Client *client)
54 if (my_client != NULL)
56 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57 "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
61 GNUNET_SERVER_notification_context_add (nc, client);
62 GNUNET_SERVER_client_keep (client);
68 * Unregister a client (which may have been a scheduling client,
69 * but this is not assured).
71 * @param client handle of the (now dead) client
74 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
76 if (my_client != client)
78 GAS_addresses_destroy_all ();
79 GNUNET_SERVER_client_drop (client);
85 * Transmit the given address suggestion and bandwidth update to all scheduling
88 * @param peer peer for which this is an address suggestion
89 * @param plugin_name 0-termintated string specifying the transport plugin
90 * @param plugin_addr binary address for the plugin to use
91 * @param plugin_addr_len number of bytes in plugin_addr
92 * @param session_id session ID to use for the given client (other clients will see 0)
93 * @param atsi performance data for the address
94 * @param atsi_count number of performance records in 'ats'
95 * @param bandwidth_out assigned outbound bandwidth
96 * @param bandwidth_in assigned inbound bandwidth
99 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity
100 *peer, const char *plugin_name,
101 const void *plugin_addr,
102 size_t plugin_addr_len,
104 const struct GNUNET_ATS_Information
105 *atsi, uint32_t atsi_count,
106 struct GNUNET_BANDWIDTH_Value32NBO
108 struct GNUNET_BANDWIDTH_Value32NBO
111 struct AddressSuggestionMessage *msg;
112 size_t plugin_name_length = strlen (plugin_name) + 1;
114 sizeof (struct AddressSuggestionMessage) +
115 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
118 struct GNUNET_ATS_Information *atsp;
121 if (my_client == NULL)
123 GNUNET_STATISTICS_update (GSA_stats, "# address suggestions made", 1,
125 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
126 GNUNET_assert (atsi_count <
127 GNUNET_SERVER_MAX_MESSAGE_SIZE /
128 sizeof (struct GNUNET_ATS_Information));
129 msg = (struct AddressSuggestionMessage *) buf;
130 msg->header.size = htons (msize);
131 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
132 msg->ats_count = htonl (atsi_count);
134 msg->address_length = htons (plugin_addr_len);
135 msg->plugin_name_length = htons (plugin_name_length);
136 msg->session_id = htonl (session_id);
137 msg->bandwidth_out = bandwidth_out;
138 msg->bandwidth_in = bandwidth_in;
139 atsp = (struct GNUNET_ATS_Information *) &msg[1];
140 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
141 addrp = (char *) &atsp[atsi_count];
142 memcpy (addrp, plugin_addr, plugin_addr_len);
143 strcpy (&addrp[plugin_addr_len], plugin_name);
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "ATS sends quota for peer `%s': (in/out) %u/%u\n",
147 GNUNET_i2s (peer), ntohl (bandwidth_in.value__),
148 ntohl (bandwidth_out.value__));
150 GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
156 * Handle 'request address' messages from clients.
158 * @param cls unused, NULL
159 * @param client client that sent the request
160 * @param message the request message
163 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
164 const struct GNUNET_MessageHeader *message)
166 const struct RequestAddressMessage *msg =
167 (const struct RequestAddressMessage *) message;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
171 GNUNET_STATISTICS_update (GSA_stats, "# address requests received", 1,
173 GNUNET_break (0 == ntohl (msg->reserved));
174 GAS_addresses_request_address (&msg->peer);
175 GNUNET_SERVER_receive_done (client, GNUNET_OK);
180 * Handle 'request address' messages from clients.
182 * @param cls unused, NULL
183 * @param client client that sent the request
184 * @param message the request message
187 GAS_handle_request_address_cancel (void *cls,
188 struct GNUNET_SERVER_Client *client,
189 const struct GNUNET_MessageHeader *message)
191 const struct RequestAddressMessage *msg =
192 (const struct RequestAddressMessage *) message;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
195 "REQUEST_ADDRESS_CANCEL");
196 GNUNET_break (0 == ntohl (msg->reserved));
200 GNUNET_SERVER_receive_done (client, GNUNET_OK);
205 * Handle 'address update' 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_address_update (void *cls, struct GNUNET_SERVER_Client *client,
213 const struct GNUNET_MessageHeader *message)
215 const struct AddressUpdateMessage *m;
216 const struct GNUNET_ATS_Information *atsi;
218 const char *plugin_name;
219 uint16_t address_length;
220 uint16_t plugin_name_length;
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
226 size = ntohs (message->size);
227 if (size < sizeof (struct AddressUpdateMessage))
230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
233 m = (const struct AddressUpdateMessage *) message;
234 ats_count = ntohl (m->ats_count);
235 address_length = ntohs (m->address_length);
236 plugin_name_length = ntohs (m->plugin_name_length);
237 atsi = (const struct GNUNET_ATS_Information *) &m[1];
238 address = (const char *) &atsi[ats_count];
239 if (plugin_name_length != 0)
240 plugin_name = &address[address_length];
243 if ((address_length + plugin_name_length +
244 ats_count * sizeof (struct GNUNET_ATS_Information) +
245 sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
247 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
248 || (plugin_name[plugin_name_length - 1] != '\0'))
251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
254 GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
256 GAS_addresses_update (&m->peer, plugin_name, address, address_length,
257 ntohl (m->session_id), atsi, ats_count);
258 GNUNET_SERVER_receive_done (client, GNUNET_OK);
263 * Handle 'address in use' messages from clients.
265 * @param cls unused, NULL
266 * @param client client that sent the request
267 * @param message the request message
270 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
271 const struct GNUNET_MessageHeader *message)
273 const struct AddressUseMessage *m;
275 const char *plugin_name;
276 uint16_t address_length;
277 uint16_t plugin_name_length;
282 size = ntohs (message->size);
283 if (size < sizeof (struct AddressUseMessage))
286 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
289 m = (const struct AddressUseMessage *) message;
291 address_length = ntohs (m->address_length);
292 plugin_name_length = ntohs (m->plugin_name_length);
294 address = (const char *) &m[1];
295 if (plugin_name_length != 0)
296 plugin_name = &address[address_length];
300 if ((address_length + plugin_name_length +
301 sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
302 (plugin_name[plugin_name_length - 1] != '\0'))
305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
309 in_use = ntohs (m->in_use);
310 GAS_addresses_in_use (&m->peer, plugin_name, address, address_length,
311 ntohl (m->session_id), in_use);
313 GNUNET_SERVER_receive_done (client, GNUNET_OK);
317 * Handle 'address destroyed' messages from clients.
319 * @param cls unused, NULL
320 * @param client client that sent the request
321 * @param message the request message
324 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
325 const struct GNUNET_MessageHeader *message)
327 const struct AddressDestroyedMessage *m;
328 struct SessionReleaseMessage srm;
330 const char *plugin_name;
331 uint16_t address_length;
332 uint16_t plugin_name_length;
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
336 "ADDRESS_DESTROYED", ntohs (message->size),
337 sizeof (struct AddressDestroyedMessage));
338 size = ntohs (message->size);
339 if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
345 m = (const struct AddressDestroyedMessage *) message;
346 GNUNET_break (0 == ntohl (m->reserved));
347 address_length = ntohs (m->address_length);
348 plugin_name_length = ntohs (m->plugin_name_length);
349 address = (const char *) &m[1];
350 if (plugin_name_length != 0)
351 plugin_name = &address[address_length];
354 if ((address_length + plugin_name_length +
355 sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
361 if ((plugin_name_length == 0) ||
362 (plugin_name[plugin_name_length - 1] != '\0'))
365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
368 GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
369 GAS_addresses_destroy (&m->peer, plugin_name, address, address_length,
370 ntohl (m->session_id));
371 if (0 != ntohl (m->session_id))
373 srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
374 srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
375 srm.session_id = m->session_id;
377 GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
385 * Initialize scheduling subsystem.
387 * @param server handle to our server
390 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)
392 nc = GNUNET_SERVER_notification_context_create (server, 128);
397 * Shutdown scheduling subsystem.
400 GAS_scheduling_done ()
402 GNUNET_SERVER_notification_context_destroy (nc);
407 /* end of gnunet-service-ats_scheduling.c */