minor changes for address conversion
[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 local_address_info the local address for the address
96  * @param session_id session ID to use for the given client (other clients will see 0)
97  * @param atsi performance data for the address
98  * @param atsi_count number of performance records in 'ats'
99  * @param bandwidth_out assigned outbound bandwidth
100  * @param bandwidth_in assigned inbound bandwidth
101  */
102 void
103 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity
104                                 *peer,
105                                 const char *plugin_name,
106                                 const void *plugin_addr,
107                                 size_t plugin_addr_len,
108                                 uint32_t local_address_info,
109                                 uint32_t session_id,
110                                 const struct GNUNET_ATS_Information *atsi,
111                                 uint32_t atsi_count,
112                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
113                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
114 {
115   struct AddressSuggestionMessage *msg;
116   size_t plugin_name_length = strlen (plugin_name) + 1;
117   size_t msize =
118       sizeof (struct AddressSuggestionMessage) +
119       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
120       plugin_name_length;
121   char buf[msize] GNUNET_ALIGN;
122   struct GNUNET_ATS_Information *atsp;
123   char *addrp;
124
125   if (my_client == NULL)
126     return;
127   GNUNET_STATISTICS_update (GSA_stats, "# address suggestions made", 1,
128                             GNUNET_NO);
129   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
130   GNUNET_assert (atsi_count <
131                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
132                  sizeof (struct GNUNET_ATS_Information));
133   msg = (struct AddressSuggestionMessage *) buf;
134   msg->header.size = htons (msize);
135   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
136   msg->ats_count = htonl (atsi_count);
137   msg->peer = *peer;
138   msg->address_length = htons (plugin_addr_len);
139   msg->plugin_name_length = htons (plugin_name_length);
140   msg->address_local_info = htonl (local_address_info);
141   msg->session_id = htonl (session_id);
142   msg->bandwidth_out = bandwidth_out;
143   msg->bandwidth_in = bandwidth_in;
144   atsp = (struct GNUNET_ATS_Information *) &msg[1];
145   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
146   addrp = (char *) &atsp[atsi_count];
147   memcpy (addrp, plugin_addr, plugin_addr_len);
148   strcpy (&addrp[plugin_addr_len], plugin_name);
149
150   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151               "ATS sends quota for peer `%s': (in/out) %u/%u\n",
152               GNUNET_i2s (peer),
153               (unsigned int) ntohl (bandwidth_in.value__),
154               (unsigned int) ntohl (bandwidth_out.value__));
155
156   GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
157                                               GNUNET_YES);
158 }
159
160
161 /**
162  * Handle 'request address' messages from clients.
163  *
164  * @param cls unused, NULL
165  * @param client client that sent the request
166  * @param message the request message
167  */
168 void
169 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
170                             const struct GNUNET_MessageHeader *message)
171 {
172   const struct RequestAddressMessage *msg =
173       (const struct RequestAddressMessage *) message;
174
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
176               "REQUEST_ADDRESS");
177   GNUNET_break (0 == ntohl (msg->reserved));
178   GAS_addresses_request_address (address_handle, &msg->peer);
179   GNUNET_SERVER_receive_done (client, GNUNET_OK);
180 }
181
182
183 /**
184  * Handle 'request address' messages from clients.
185  *
186  * @param cls unused, NULL
187  * @param client client that sent the request
188  * @param message the request message
189  */
190 void
191 GAS_handle_request_address_cancel (void *cls,
192                                    struct GNUNET_SERVER_Client *client,
193                                    const struct GNUNET_MessageHeader *message)
194 {
195   const struct RequestAddressMessage *msg =
196       (const struct RequestAddressMessage *) message;
197
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
199               "REQUEST_ADDRESS_CANCEL");
200   GNUNET_break (0 == ntohl (msg->reserved));
201
202   GAS_addresses_request_address_cancel (address_handle, &msg->peer);
203
204   GNUNET_SERVER_receive_done (client, GNUNET_OK);
205 }
206
207 /**
208  * Handle 'reset backoff' messages from clients.
209  *
210  * @param cls unused, NULL
211  * @param client client that sent the request
212  * @param message the request message
213  */
214 void
215 GAS_handle_reset_backoff (void *cls,
216                           struct GNUNET_SERVER_Client *client,
217                           const struct GNUNET_MessageHeader *message)
218 {
219   const struct ResetBackoffMessage *msg =
220       (const struct ResetBackoffMessage *) message;
221
222   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
223               "RESET_BACKOFF");
224   GNUNET_break (0 == ntohl (msg->reserved));
225   GAS_addresses_handle_backoff_reset (address_handle, &msg->peer);
226   GNUNET_SERVER_receive_done (client, GNUNET_OK);
227 }
228
229 /**
230  * Handle 'address add' messages from clients.
231  *
232  * @param cls unused, NULL
233  * @param client client that sent the request
234  * @param message the request message
235  */
236 void
237 GAS_handle_address_add (void *cls, struct GNUNET_SERVER_Client *client,
238                         const struct GNUNET_MessageHeader *message)
239 {
240   const struct AddressUpdateMessage *m;
241   const struct GNUNET_ATS_Information *atsi;
242   const char *address;
243   const char *plugin_name;
244   uint16_t address_length;
245   uint16_t plugin_name_length;
246   uint32_t ats_count;
247   uint16_t size;
248
249   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
250               "ADDRESS_ADD");
251   size = ntohs (message->size);
252   if (size < sizeof (struct AddressUpdateMessage))
253   {
254     GNUNET_break (0);
255     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
256     return;
257   }
258   m = (const struct AddressUpdateMessage *) message;
259   ats_count = ntohl (m->ats_count);
260   address_length = ntohs (m->address_length);
261   plugin_name_length = ntohs (m->plugin_name_length);
262   atsi = (const struct GNUNET_ATS_Information *) &m[1];
263   address = (const char *) &atsi[ats_count];
264   if (plugin_name_length != 0)
265     plugin_name = &address[address_length];
266   else
267     plugin_name = "";
268
269   if ((address_length + plugin_name_length +
270        ats_count * sizeof (struct GNUNET_ATS_Information) +
271        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
272       (ats_count >
273        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
274        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
275   {
276     GNUNET_break (0);
277     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
278     return;
279   }
280   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
281                             GNUNET_NO);
282   GAS_addresses_add (address_handle, &m->peer, plugin_name, address, address_length,
283       ntohl(m->address_local_info), ntohl (m->session_id), atsi, ats_count);
284   GNUNET_SERVER_receive_done (client, GNUNET_OK);
285 }
286
287
288 /**
289  * Handle 'address update' messages from clients.
290  *
291  * @param cls unused, NULL
292  * @param client client that sent the request
293  * @param message the request message
294  */
295 void
296 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
297                            const struct GNUNET_MessageHeader *message)
298 {
299   const struct AddressUpdateMessage *m;
300   const struct GNUNET_ATS_Information *atsi;
301   const char *address;
302   const char *plugin_name;
303   uint16_t address_length;
304   uint16_t plugin_name_length;
305   uint32_t ats_count;
306   uint16_t size;
307
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
309               "ADDRESS_UPDATE");
310   size = ntohs (message->size);
311   if (size < sizeof (struct AddressUpdateMessage))
312   {
313     GNUNET_break (0);
314     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
315     return;
316   }
317   m = (const struct AddressUpdateMessage *) message;
318   ats_count = ntohl (m->ats_count);
319   address_length = ntohs (m->address_length);
320   plugin_name_length = ntohs (m->plugin_name_length);
321   atsi = (const struct GNUNET_ATS_Information *) &m[1];
322   address = (const char *) &atsi[ats_count];
323   if (plugin_name_length != 0)
324     plugin_name = &address[address_length];
325   else
326     plugin_name = "";
327
328   if ((address_length + plugin_name_length +
329        ats_count * sizeof (struct GNUNET_ATS_Information) +
330        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
331       (ats_count >
332        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
333        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
334   {
335     GNUNET_break (0);
336     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
337     return;
338   }
339   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
340                             GNUNET_NO);
341   GAS_addresses_update (address_handle, &m->peer, plugin_name, address, address_length,
342                         ntohl (m->address_local_info),
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 (address_handle,
399                               &m->peer,
400                               plugin_name,
401                               address,
402                               address_length,
403                               ntohl (m->address_local_info),
404                               ntohl (m->session_id),
405                               in_use);
406
407   if (res == GNUNET_OK)
408     GNUNET_SERVER_receive_done (client, GNUNET_OK);
409   else
410   {
411     GNUNET_break (0);
412     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
413   }
414
415 }
416
417 /**
418  * Handle 'address destroyed' messages from clients.
419  *
420  * @param cls unused, NULL
421  * @param client client that sent the request
422  * @param message the request message
423  */
424 void
425 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
426                               const struct GNUNET_MessageHeader *message)
427 {
428   const struct AddressDestroyedMessage *m;
429   struct SessionReleaseMessage srm;
430   const char *address;
431   const char *plugin_name;
432   uint16_t address_length;
433   uint16_t plugin_name_length;
434   uint16_t size;
435
436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
437               "ADDRESS_DESTROYED", ntohs (message->size),
438               sizeof (struct AddressDestroyedMessage));
439   size = ntohs (message->size);
440   if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
441   {
442     GNUNET_break (0);
443     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
444     return;
445   }
446   m = (const struct AddressDestroyedMessage *) message;
447   GNUNET_break (0 == ntohl (m->reserved));
448   address_length = ntohs (m->address_length);
449   plugin_name_length = ntohs (m->plugin_name_length);
450   address = (const char *) &m[1];
451   if (plugin_name_length != 0)
452     plugin_name = &address[address_length];
453   else
454     plugin_name = "";
455   if ((address_length + plugin_name_length +
456        sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
457   {
458     GNUNET_break (0);
459     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
460     return;
461   }
462   if ((plugin_name_length == 0) ||
463       (plugin_name[plugin_name_length - 1] != '\0'))
464   {
465     GNUNET_break (0);
466     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
467     return;
468   }
469   GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
470   GAS_addresses_destroy (address_handle, &m->peer, plugin_name,
471                          address, address_length,
472                          ntohl (m->address_local_info),
473                          ntohl (m->session_id));
474   if (0 != ntohl (m->session_id))
475   {
476     srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
477     srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
478     srm.session_id = m->session_id;
479     srm.peer = m->peer;
480     GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
481                                                 GNUNET_NO);
482   }
483   GNUNET_SERVER_receive_done (client, GNUNET_OK);
484 }
485
486
487 /**
488  * Initialize scheduling subsystem.
489  *
490  * @param server handle to our server
491  * @param ah the address handle to use
492  */
493 void
494 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server, struct GAS_Addresses_Handle *ah)
495 {
496   GNUNET_assert (NULL != ah);
497   address_handle = ah;
498   nc = GNUNET_SERVER_notification_context_create (server, 128);
499 }
500
501
502 /**
503  * Shutdown scheduling subsystem.
504  */
505 void
506 GAS_scheduling_done ()
507 {
508   if (NULL != my_client)
509   {
510     my_client = NULL;
511   }
512   GNUNET_SERVER_notification_context_destroy (nc);
513   nc = NULL;
514
515 }
516
517
518 /* end of gnunet-service-ats_scheduling.c */