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