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