- fixes
[oweals/gnunet.git] / src / ats / gnunet-service-ats_scheduling.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file ats/gnunet-service-ats_scheduling.c
23  * @brief ats service, interaction with 'scheduling' API
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_scheduling.h"
31 #include "ats.h"
32
33
34 /**
35  * Context for sending messages to clients.
36  */
37 static struct GNUNET_SERVER_NotificationContext *nc;
38
39 /**
40  * Actual handle to the client.
41  */
42 static struct GNUNET_SERVER_Client *my_client;
43
44
45 /**
46  * Handle to address subsystem
47  */
48 static struct GAS_Addresses_Handle *address_handle;
49
50 /**
51  * Register a new scheduling client.
52  *
53  * @param client handle of the new client
54  * @return GNUNET_OK on success, GNUNET_SYSERR on error
55  */
56 int
57 GAS_scheduling_add_client (struct GNUNET_SERVER_Client *client)
58 {
59   if (my_client != NULL)
60   {
61     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62                 "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
63     return GNUNET_SYSERR;
64   }
65   my_client = client;
66   GNUNET_SERVER_notification_context_add (nc, client);
67   return GNUNET_OK;
68 }
69
70
71 /**
72  * Unregister a client (which may have been a scheduling client,
73  * but this is not assured).
74  *
75  * @param client handle of the (now dead) client
76  */
77 void
78 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
79 {
80   if (my_client != client)
81     return;
82   GAS_addresses_destroy_all (address_handle);
83   my_client = NULL;
84 }
85
86
87 /**
88  * Transmit the given address suggestion and bandwidth update to all scheduling
89  * clients.
90  *
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
100  */
101 void
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,
106                                             uint32_t session_id,
107                                             const struct GNUNET_ATS_Information
108                                             *atsi, uint32_t atsi_count,
109                                             struct GNUNET_BANDWIDTH_Value32NBO
110                                             bandwidth_out,
111                                             struct GNUNET_BANDWIDTH_Value32NBO
112                                             bandwidth_in)
113 {
114   struct AddressSuggestionMessage *msg;
115   size_t plugin_name_length = strlen (plugin_name) + 1;
116   size_t msize =
117       sizeof (struct AddressSuggestionMessage) +
118       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
119       plugin_name_length;
120   char buf[msize] GNUNET_ALIGN;
121   struct GNUNET_ATS_Information *atsp;
122   char *addrp;
123
124   if (my_client == NULL)
125     return;
126   GNUNET_STATISTICS_update (GSA_stats, "# address suggestions made", 1,
127                             GNUNET_NO);
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);
136   msg->peer = *peer;
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);
147
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "ATS sends quota for peer `%s': (in/out) %u/%u\n",
150               GNUNET_i2s (peer),
151               (unsigned int) ntohl (bandwidth_in.value__),
152               (unsigned int) ntohl (bandwidth_out.value__));
153
154   GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
155                                               GNUNET_YES);
156 }
157
158
159 /**
160  * Handle 'request address' messages from clients.
161  *
162  * @param cls unused, NULL
163  * @param client client that sent the request
164  * @param message the request message
165  */
166 void
167 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
168                             const struct GNUNET_MessageHeader *message)
169 {
170   const struct RequestAddressMessage *msg =
171       (const struct RequestAddressMessage *) message;
172
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
174               "REQUEST_ADDRESS");
175   GNUNET_break (0 == ntohl (msg->reserved));
176   GAS_addresses_request_address (address_handle, &msg->peer);
177   GNUNET_SERVER_receive_done (client, GNUNET_OK);
178 }
179
180
181 /**
182  * Handle 'request address' messages from clients.
183  *
184  * @param cls unused, NULL
185  * @param client client that sent the request
186  * @param message the request message
187  */
188 void
189 GAS_handle_request_address_cancel (void *cls,
190                                    struct GNUNET_SERVER_Client *client,
191                                    const struct GNUNET_MessageHeader *message)
192 {
193   const struct RequestAddressMessage *msg =
194       (const struct RequestAddressMessage *) message;
195
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
197               "REQUEST_ADDRESS_CANCEL");
198   GNUNET_break (0 == ntohl (msg->reserved));
199
200   GAS_addresses_request_address_cancel (address_handle, &msg->peer);
201
202   GNUNET_SERVER_receive_done (client, GNUNET_OK);
203 }
204
205 /**
206  * Handle 'reset backoff' messages from clients.
207  *
208  * @param cls unused, NULL
209  * @param client client that sent the request
210  * @param message the request message
211  */
212 void
213 GAS_handle_reset_backoff (void *cls,
214                           struct GNUNET_SERVER_Client *client,
215                           const struct GNUNET_MessageHeader *message)
216 {
217   const struct ResetBackoffMessage *msg =
218       (const struct ResetBackoffMessage *) message;
219
220   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
221               "RESET_BACKOFF");
222   GNUNET_break (0 == ntohl (msg->reserved));
223   GAS_addresses_handle_backoff_reset (address_handle, &msg->peer);
224   GNUNET_SERVER_receive_done (client, GNUNET_OK);
225 }
226
227 /**
228  * Handle 'address add' messages from clients.
229  *
230  * @param cls unused, NULL
231  * @param client client that sent the request
232  * @param message the request message
233  */
234 void
235 GAS_handle_address_add (void *cls, struct GNUNET_SERVER_Client *client,
236                         const struct GNUNET_MessageHeader *message)
237 {
238   const struct AddressUpdateMessage *m;
239   const struct GNUNET_ATS_Information *atsi;
240   const char *address;
241   const char *plugin_name;
242   uint16_t address_length;
243   uint16_t plugin_name_length;
244   uint32_t ats_count;
245   uint16_t size;
246
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
248               "ADDRESS_ADD");
249   size = ntohs (message->size);
250   if (size < sizeof (struct AddressUpdateMessage))
251   {
252     GNUNET_break (0);
253     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
254     return;
255   }
256   m = (const struct AddressUpdateMessage *) message;
257   ats_count = ntohl (m->ats_count);
258   address_length = ntohs (m->address_length);
259   plugin_name_length = ntohs (m->plugin_name_length);
260   atsi = (const struct GNUNET_ATS_Information *) &m[1];
261   address = (const char *) &atsi[ats_count];
262   if (plugin_name_length != 0)
263     plugin_name = &address[address_length];
264   else
265     plugin_name = "";
266
267   if ((address_length + plugin_name_length +
268        ats_count * sizeof (struct GNUNET_ATS_Information) +
269        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
270       (ats_count >
271        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
272        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
273   {
274     GNUNET_break (0);
275     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
276     return;
277   }
278   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
279                             GNUNET_NO);
280   GAS_addresses_add (address_handle, &m->peer, plugin_name, address, address_length,
281                         ntohl (m->session_id), atsi, ats_count);
282   GNUNET_SERVER_receive_done (client, GNUNET_OK);
283 }
284
285
286 /**
287  * Handle 'address update' messages from clients.
288  *
289  * @param cls unused, NULL
290  * @param client client that sent the request
291  * @param message the request message
292  */
293 void
294 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
295                            const struct GNUNET_MessageHeader *message)
296 {
297   const struct AddressUpdateMessage *m;
298   const struct GNUNET_ATS_Information *atsi;
299   const char *address;
300   const char *plugin_name;
301   uint16_t address_length;
302   uint16_t plugin_name_length;
303   uint32_t ats_count;
304   uint16_t size;
305
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
307               "ADDRESS_UPDATE");
308   size = ntohs (message->size);
309   if (size < sizeof (struct AddressUpdateMessage))
310   {
311     GNUNET_break (0);
312     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
313     return;
314   }
315   m = (const struct AddressUpdateMessage *) message;
316   ats_count = ntohl (m->ats_count);
317   address_length = ntohs (m->address_length);
318   plugin_name_length = ntohs (m->plugin_name_length);
319   atsi = (const struct GNUNET_ATS_Information *) &m[1];
320   address = (const char *) &atsi[ats_count];
321   if (plugin_name_length != 0)
322     plugin_name = &address[address_length];
323   else
324     plugin_name = "";
325
326   if ((address_length + plugin_name_length +
327        ats_count * sizeof (struct GNUNET_ATS_Information) +
328        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
329       (ats_count >
330        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
331        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
332   {
333     GNUNET_break (0);
334     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
335     return;
336   }
337   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
338                             GNUNET_NO);
339   GAS_addresses_update (address_handle, &m->peer, plugin_name, address, address_length,
340                         ntohl (m->session_id), atsi, ats_count);
341   GNUNET_SERVER_receive_done (client, GNUNET_OK);
342 }
343
344
345 /**
346  * Handle 'address in use' messages from clients.
347  *
348  * @param cls unused, NULL
349  * @param client client that sent the request
350  * @param message the request message
351  */
352 void
353 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
354                            const struct GNUNET_MessageHeader *message)
355 {
356   const struct AddressUseMessage *m;
357   const char *address;
358   const char *plugin_name;
359   int res;
360   uint16_t address_length;
361   uint16_t plugin_name_length;
362
363   uint16_t size;
364   uint16_t in_use;
365
366   size = ntohs (message->size);
367   if (size < sizeof (struct AddressUseMessage))
368   {
369     GNUNET_break (0);
370     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
371     return;
372   }
373   m = (const struct AddressUseMessage *) message;
374
375   address_length = ntohs (m->address_length);
376   plugin_name_length = ntohs (m->plugin_name_length);
377
378   address = (const char *) &m[1];
379   if (plugin_name_length != 0)
380     plugin_name = &address[address_length];
381   else
382     plugin_name = "";
383
384   if ((address_length + plugin_name_length +
385        sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
386       ((plugin_name_length > 0) &&
387       (plugin_name[plugin_name_length - 1] != '\0')))
388   {
389     GNUNET_break (0);
390     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
391     return;
392   }
393
394   in_use = ntohs (m->in_use);
395   res = GAS_addresses_in_use (address_handle,
396                               &m->peer,
397                               plugin_name,
398                               address,
399                               address_length,
400                               ntohl (m->session_id),
401                               in_use);
402
403   if (res == GNUNET_OK)
404     GNUNET_SERVER_receive_done (client, GNUNET_OK);
405   else
406   {
407     GNUNET_break (0);
408     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
409   }
410
411 }
412
413 /**
414  * Handle 'address destroyed' messages from clients.
415  *
416  * @param cls unused, NULL
417  * @param client client that sent the request
418  * @param message the request message
419  */
420 void
421 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
422                               const struct GNUNET_MessageHeader *message)
423 {
424   const struct AddressDestroyedMessage *m;
425   struct SessionReleaseMessage srm;
426   const char *address;
427   const char *plugin_name;
428   uint16_t address_length;
429   uint16_t plugin_name_length;
430   uint16_t size;
431
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
433               "ADDRESS_DESTROYED", ntohs (message->size),
434               sizeof (struct AddressDestroyedMessage));
435   size = ntohs (message->size);
436   if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
437   {
438     GNUNET_break (0);
439     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
440     return;
441   }
442   m = (const struct AddressDestroyedMessage *) message;
443   GNUNET_break (0 == ntohl (m->reserved));
444   address_length = ntohs (m->address_length);
445   plugin_name_length = ntohs (m->plugin_name_length);
446   address = (const char *) &m[1];
447   if (plugin_name_length != 0)
448     plugin_name = &address[address_length];
449   else
450     plugin_name = "";
451   if ((address_length + plugin_name_length +
452        sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
453   {
454     GNUNET_break (0);
455     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
456     return;
457   }
458   if ((plugin_name_length == 0) ||
459       (plugin_name[plugin_name_length - 1] != '\0'))
460   {
461     GNUNET_break (0);
462     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
463     return;
464   }
465   GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
466   GAS_addresses_destroy (address_handle, &m->peer, plugin_name,
467                          address, address_length,
468                          ntohl (m->session_id));
469   if (0 != ntohl (m->session_id))
470   {
471     srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
472     srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
473     srm.session_id = m->session_id;
474     srm.peer = m->peer;
475     GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
476                                                 GNUNET_NO);
477   }
478   GNUNET_SERVER_receive_done (client, GNUNET_OK);
479 }
480
481
482 /**
483  * Initialize scheduling subsystem.
484  *
485  * @param server handle to our server
486  * @param ah the address handle to use
487  */
488 void
489 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server, struct GAS_Addresses_Handle *ah)
490 {
491   GNUNET_assert (NULL != ah);
492   address_handle = ah;
493   nc = GNUNET_SERVER_notification_context_create (server, 128);
494 }
495
496
497 /**
498  * Shutdown scheduling subsystem.
499  */
500 void
501 GAS_scheduling_done ()
502 {
503   if (NULL != my_client)
504   {
505     my_client = NULL;
506   }
507   GNUNET_SERVER_notification_context_destroy (nc);
508   nc = NULL;
509
510 }
511
512
513 /* end of gnunet-service-ats_scheduling.c */