fix coverity 10390
[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), ntohl (bandwidth_in.value__),
151               ntohl (bandwidth_out.value__));
152
153   GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
154                                               GNUNET_YES);
155 }
156
157
158 /**
159  * Handle 'request address' messages from clients.
160  *
161  * @param cls unused, NULL
162  * @param client client that sent the request
163  * @param message the request message
164  */
165 void
166 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
167                             const struct GNUNET_MessageHeader *message)
168 {
169   const struct RequestAddressMessage *msg =
170       (const struct RequestAddressMessage *) message;
171
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
173               "REQUEST_ADDRESS");
174   GNUNET_STATISTICS_update (GSA_stats, "# address requests received", 1,
175                             GNUNET_NO);
176   GNUNET_break (0 == ntohl (msg->reserved));
177   GAS_addresses_request_address (&msg->peer);
178   GNUNET_SERVER_receive_done (client, GNUNET_OK);
179 }
180
181
182 /**
183  * Handle 'request address' messages from clients.
184  *
185  * @param cls unused, NULL
186  * @param client client that sent the request
187  * @param message the request message
188  */
189 void
190 GAS_handle_request_address_cancel (void *cls,
191                                    struct GNUNET_SERVER_Client *client,
192                                    const struct GNUNET_MessageHeader *message)
193 {
194   const struct RequestAddressMessage *msg =
195       (const struct RequestAddressMessage *) message;
196
197   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
198               "REQUEST_ADDRESS_CANCEL");
199   GNUNET_break (0 == ntohl (msg->reserved));
200
201   GAS_addresses_request_address_cancel (&msg->peer);
202
203   GNUNET_SERVER_receive_done (client, GNUNET_OK);
204 }
205
206 /**
207  * Handle 'reset backoff' messages from clients.
208  *
209  * @param cls unused, NULL
210  * @param client client that sent the request
211  * @param message the request message
212  */
213 void
214 GAS_handle_reset_backoff (void *cls,
215                           struct GNUNET_SERVER_Client *client,
216                           const struct GNUNET_MessageHeader *message)
217 {
218   const struct ResetBackoffMessage *msg =
219       (const struct ResetBackoffMessage *) message;
220
221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
222               "RESET_BACKOFF");
223   GNUNET_STATISTICS_update (GSA_stats, "# backoff reset requests received", 1,
224                             GNUNET_NO);
225   GNUNET_break (0 == ntohl (msg->reserved));
226   GAS_addresses_handle_backoff_reset (&msg->peer);
227   GNUNET_SERVER_receive_done (client, GNUNET_OK);
228 }
229
230 /**
231  * Handle 'address add' messages from clients.
232  *
233  * @param cls unused, NULL
234  * @param client client that sent the request
235  * @param message the request message
236  */
237 void
238 GAS_handle_address_add (void *cls, struct GNUNET_SERVER_Client *client,
239                         const struct GNUNET_MessageHeader *message)
240 {
241   const struct AddressUpdateMessage *m;
242   const struct GNUNET_ATS_Information *atsi;
243   const char *address;
244   const char *plugin_name;
245   uint16_t address_length;
246   uint16_t plugin_name_length;
247   uint32_t ats_count;
248   uint16_t size;
249
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
251               "ADDRESS_ADD");
252   size = ntohs (message->size);
253   if (size < sizeof (struct AddressUpdateMessage))
254   {
255     GNUNET_break (0);
256     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
257     return;
258   }
259   m = (const struct AddressUpdateMessage *) message;
260   ats_count = ntohl (m->ats_count);
261   address_length = ntohs (m->address_length);
262   plugin_name_length = ntohs (m->plugin_name_length);
263   atsi = (const struct GNUNET_ATS_Information *) &m[1];
264   address = (const char *) &atsi[ats_count];
265   if (plugin_name_length != 0)
266     plugin_name = &address[address_length];
267   else
268     plugin_name = "";
269
270   if ((address_length + plugin_name_length +
271        ats_count * sizeof (struct GNUNET_ATS_Information) +
272        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
273       (ats_count >
274        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
275        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
276   {
277     GNUNET_break (0);
278     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
279     return;
280   }
281   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
282                             GNUNET_NO);
283   GAS_addresses_add (&m->peer, plugin_name, address, address_length,
284                         ntohl (m->session_id), atsi, ats_count);
285   GNUNET_SERVER_receive_done (client, GNUNET_OK);
286 }
287
288
289 /**
290  * Handle 'address update' messages from clients.
291  *
292  * @param cls unused, NULL
293  * @param client client that sent the request
294  * @param message the request message
295  */
296 void
297 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
298                            const struct GNUNET_MessageHeader *message)
299 {
300   const struct AddressUpdateMessage *m;
301   const struct GNUNET_ATS_Information *atsi;
302   const char *address;
303   const char *plugin_name;
304   uint16_t address_length;
305   uint16_t plugin_name_length;
306   uint32_t ats_count;
307   uint16_t size;
308
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
310               "ADDRESS_UPDATE");
311   size = ntohs (message->size);
312   if (size < sizeof (struct AddressUpdateMessage))
313   {
314     GNUNET_break (0);
315     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
316     return;
317   }
318   m = (const struct AddressUpdateMessage *) message;
319   ats_count = ntohl (m->ats_count);
320   address_length = ntohs (m->address_length);
321   plugin_name_length = ntohs (m->plugin_name_length);
322   atsi = (const struct GNUNET_ATS_Information *) &m[1];
323   address = (const char *) &atsi[ats_count];
324   if (plugin_name_length != 0)
325     plugin_name = &address[address_length];
326   else
327     plugin_name = "";
328
329   if ((address_length + plugin_name_length +
330        ats_count * sizeof (struct GNUNET_ATS_Information) +
331        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
332       (ats_count >
333        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
334        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
335   {
336     GNUNET_break (0);
337     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
338     return;
339   }
340   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
341                             GNUNET_NO);
342   GAS_addresses_update (&m->peer, plugin_name, address, address_length,
343                         ntohl (m->session_id), atsi, ats_count);
344   GNUNET_SERVER_receive_done (client, GNUNET_OK);
345 }
346
347
348 /**
349  * Handle 'address in use' messages from clients.
350  *
351  * @param cls unused, NULL
352  * @param client client that sent the request
353  * @param message the request message
354  */
355 void
356 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
357                            const struct GNUNET_MessageHeader *message)
358 {
359   const struct AddressUseMessage *m;
360   const char *address;
361   const char *plugin_name;
362   int res;
363   uint16_t address_length;
364   uint16_t plugin_name_length;
365
366   uint16_t size;
367   uint16_t in_use;
368
369   size = ntohs (message->size);
370   if (size < sizeof (struct AddressUseMessage))
371   {
372     GNUNET_break (0);
373     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
374     return;
375   }
376   m = (const struct AddressUseMessage *) message;
377
378   address_length = ntohs (m->address_length);
379   plugin_name_length = ntohs (m->plugin_name_length);
380
381   address = (const char *) &m[1];
382   if (plugin_name_length != 0)
383     plugin_name = &address[address_length];
384   else
385     plugin_name = "";
386
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')))
391   {
392     GNUNET_break (0);
393     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
394     return;
395   }
396
397   in_use = ntohs (m->in_use);
398   res = GAS_addresses_in_use (&m->peer,
399                              plugin_name,
400                              address,
401                              address_length,
402                              ntohl (m->session_id),
403                              in_use);
404
405   if (res == GNUNET_OK)
406     GNUNET_SERVER_receive_done (client, GNUNET_OK);
407   else
408   {
409     GNUNET_break (0);
410     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
411   }
412
413 }
414
415 /**
416  * Handle 'address destroyed' messages from clients.
417  *
418  * @param cls unused, NULL
419  * @param client client that sent the request
420  * @param message the request message
421  */
422 void
423 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
424                               const struct GNUNET_MessageHeader *message)
425 {
426   const struct AddressDestroyedMessage *m;
427   struct SessionReleaseMessage srm;
428   const char *address;
429   const char *plugin_name;
430   uint16_t address_length;
431   uint16_t plugin_name_length;
432   uint16_t size;
433
434   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
435               "ADDRESS_DESTROYED", ntohs (message->size),
436               sizeof (struct AddressDestroyedMessage));
437   size = ntohs (message->size);
438   if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
439   {
440     GNUNET_break (0);
441     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
442     return;
443   }
444   m = (const struct AddressDestroyedMessage *) message;
445   GNUNET_break (0 == ntohl (m->reserved));
446   address_length = ntohs (m->address_length);
447   plugin_name_length = ntohs (m->plugin_name_length);
448   address = (const char *) &m[1];
449   if (plugin_name_length != 0)
450     plugin_name = &address[address_length];
451   else
452     plugin_name = "";
453   if ((address_length + plugin_name_length +
454        sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
455   {
456     GNUNET_break (0);
457     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
458     return;
459   }
460   if ((plugin_name_length == 0) ||
461       (plugin_name[plugin_name_length - 1] != '\0'))
462   {
463     GNUNET_break (0);
464     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
465     return;
466   }
467   GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
468   GAS_addresses_destroy (&m->peer, plugin_name, address, address_length,
469                          ntohl (m->session_id));
470   if (0 != ntohl (m->session_id))
471   {
472     srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
473     srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
474     srm.session_id = m->session_id;
475     srm.peer = m->peer;
476     GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
477                                                 GNUNET_NO);
478   }
479   GNUNET_SERVER_receive_done (client, GNUNET_OK);
480 }
481
482
483 /**
484  * Initialize scheduling subsystem.
485  *
486  * @param server handle to our server
487  */
488 void
489 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server, struct GAS_Addresses_Handle *ah)
490 {
491   address_handle = ah;
492   nc = GNUNET_SERVER_notification_context_create (server, 128);
493 }
494
495
496 /**
497  * Shutdown scheduling subsystem.
498  */
499 void
500 GAS_scheduling_done ()
501 {
502   if (NULL != my_client)
503   {
504     my_client = NULL;
505   }
506   GNUNET_SERVER_notification_context_destroy (nc);
507   nc = NULL;
508
509 }
510
511
512 /* end of gnunet-service-ats_scheduling.c */