plugin name should never be NULL
[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   GNUNET_SERVER_client_keep (client);
63   return GNUNET_OK;
64 }
65
66
67 /**
68  * Unregister a client (which may have been a scheduling client,
69  * but this is not assured).
70  *
71  * @param client handle of the (now dead) client
72  */
73 void
74 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
75 {
76   if (my_client != client)
77     return;
78   GAS_addresses_destroy_all ();
79   GNUNET_SERVER_client_drop (client);
80   my_client = NULL;
81 }
82
83
84 /**
85  * Transmit the given address suggestion and bandwidth update to all scheduling
86  * clients.
87  *
88  * @param peer peer for which this is an address suggestion
89  * @param plugin_name 0-termintated string specifying the transport plugin
90  * @param plugin_addr binary address for the plugin to use
91  * @param plugin_addr_len number of bytes in plugin_addr
92  * @param session_id session ID to use for the given client (other clients will see 0)
93  * @param atsi performance data for the address
94  * @param atsi_count number of performance records in 'ats'
95  * @param bandwidth_out assigned outbound bandwidth
96  * @param bandwidth_in assigned inbound bandwidth
97  */
98 void
99 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity
100                                             *peer, const char *plugin_name,
101                                             const void *plugin_addr,
102                                             size_t plugin_addr_len,
103                                             uint32_t session_id,
104                                             const struct GNUNET_ATS_Information
105                                             *atsi, uint32_t atsi_count,
106                                             struct GNUNET_BANDWIDTH_Value32NBO
107                                             bandwidth_out,
108                                             struct GNUNET_BANDWIDTH_Value32NBO
109                                             bandwidth_in)
110 {
111   struct AddressSuggestionMessage *msg;
112   size_t plugin_name_length = strlen (plugin_name) + 1;
113   size_t msize =
114       sizeof (struct AddressSuggestionMessage) +
115       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
116       plugin_name_length;
117   char buf[msize];
118   struct GNUNET_ATS_Information *atsp;
119   char *addrp;
120
121   if (my_client == NULL)
122     return;
123   GNUNET_STATISTICS_update (GSA_stats, "# address suggestions made", 1,
124                             GNUNET_NO);
125   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
126   GNUNET_assert (atsi_count <
127                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
128                  sizeof (struct GNUNET_ATS_Information));
129   msg = (struct AddressSuggestionMessage *) buf;
130   msg->header.size = htons (msize);
131   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
132   msg->ats_count = htonl (atsi_count);
133   msg->peer = *peer;
134   msg->address_length = htons (plugin_addr_len);
135   msg->plugin_name_length = htons (plugin_name_length);
136   msg->session_id = htonl (session_id);
137   msg->bandwidth_out = bandwidth_out;
138   msg->bandwidth_in = bandwidth_in;
139   atsp = (struct GNUNET_ATS_Information *) &msg[1];
140   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
141   addrp = (char *) &atsp[atsi_count];
142   memcpy (addrp, plugin_addr, plugin_addr_len);
143   strcpy (&addrp[plugin_addr_len], plugin_name);
144
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS sends quota for peer `%s': (in/out) %u/%u\n",
146       GNUNET_i2s (peer), ntohl (bandwidth_in.value__), 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, struct GNUNET_SERVER_Client *client,
186                             const struct GNUNET_MessageHeader *message)
187 {
188   const struct RequestAddressMessage *msg =
189       (const struct RequestAddressMessage *) message;
190
191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
192               "REQUEST_ADDRESS_CANCEL");
193   GNUNET_break (0 == ntohl (msg->reserved));
194
195   /* TODO */
196
197   GNUNET_SERVER_receive_done (client, GNUNET_OK);
198 }
199
200
201 /**
202  * Handle 'address update' 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_address_update (void *cls, struct GNUNET_SERVER_Client *client,
210                            const struct GNUNET_MessageHeader *message)
211 {
212   const struct AddressUpdateMessage *m;
213   const struct GNUNET_ATS_Information *atsi;
214   const char *address;
215   const char *plugin_name;
216   uint16_t address_length;
217   uint16_t plugin_name_length;
218   uint32_t ats_count;
219   uint16_t size;
220
221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
222               "ADDRESS_UPDATE");
223   size = ntohs (message->size);
224   if (size < sizeof (struct AddressUpdateMessage))
225   {
226     GNUNET_break (0);
227     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
228     return;
229   }
230   m = (const struct AddressUpdateMessage *) message;
231   ats_count = ntohl (m->ats_count);
232   address_length = ntohs (m->address_length);
233   plugin_name_length = ntohs (m->plugin_name_length);
234   atsi = (const struct GNUNET_ATS_Information *) &m[1];
235   address = (const char *) &atsi[ats_count];
236   if (plugin_name_length != 0)
237     plugin_name = &address[address_length];
238   else
239     plugin_name = "";
240   if ((address_length + plugin_name_length +
241        ats_count * sizeof (struct GNUNET_ATS_Information) +
242        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
243       (ats_count >
244        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
245       || (plugin_name[plugin_name_length - 1] != '\0'))
246   {
247     GNUNET_break (0);
248     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
249     return;
250   }
251   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
252                             GNUNET_NO);
253   GAS_addresses_update (&m->peer, plugin_name, address, address_length,
254                         ntohl (m->session_id), atsi, ats_count);
255   GNUNET_SERVER_receive_done (client, GNUNET_OK);
256 }
257
258
259 /**
260  * Handle 'address in use' messages from clients.
261  *
262  * @param cls unused, NULL
263  * @param client client that sent the request
264  * @param message the request message
265  */
266 void
267 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
268                            const struct GNUNET_MessageHeader *message)
269 {
270   const struct AddressUseMessage *m;
271   const char *address;
272   const char *plugin_name;
273   uint16_t address_length;
274   uint16_t plugin_name_length;
275
276   uint16_t size;
277   uint16_t in_use;
278
279   size = ntohs (message->size);
280   if (size < sizeof (struct AddressUseMessage))
281   {
282     GNUNET_break (0);
283     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
284     return;
285   }
286   m = (const struct AddressUseMessage *) message;
287
288   address_length = ntohs (m->address_length);
289   plugin_name_length = ntohs (m->plugin_name_length);
290
291   address = (const char *) &m[1];
292   if (plugin_name_length != 0)
293     plugin_name = &address[address_length];
294   else
295     plugin_name = "";
296
297   if ((address_length + plugin_name_length +
298        sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
299       (plugin_name[plugin_name_length - 1] != '\0'))
300   {
301     GNUNET_break (0);
302     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
303     return;
304   }
305
306   in_use = ntohs (m->in_use);
307   GAS_addresses_in_use (&m->peer,
308                         plugin_name,
309                         address,
310                         address_length,
311                         ntohl (m->session_id),
312                         in_use);
313
314   GNUNET_SERVER_receive_done (client, GNUNET_OK);
315 }
316
317 /**
318  * Handle 'address destroyed' messages from clients.
319  *
320  * @param cls unused, NULL
321  * @param client client that sent the request
322  * @param message the request message
323  */
324 void
325 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
326                               const struct GNUNET_MessageHeader *message)
327 {
328   const struct AddressDestroyedMessage *m;
329   struct SessionReleaseMessage srm;
330   const char *address;
331   const char *plugin_name;
332   uint16_t address_length;
333   uint16_t plugin_name_length;
334   uint16_t size;
335
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
337               "ADDRESS_DESTROYED", ntohs (message->size),
338               sizeof (struct AddressDestroyedMessage));
339   size = ntohs (message->size);
340   if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
341   {
342     GNUNET_break (0);
343     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
344     return;
345   }
346   m = (const struct AddressDestroyedMessage *) message;
347   GNUNET_break (0 == ntohl (m->reserved));
348   address_length = ntohs (m->address_length);
349   plugin_name_length = ntohs (m->plugin_name_length);
350   address = (const char *) &m[1];
351   if (plugin_name_length != 0)
352     plugin_name = &address[address_length];
353   else
354     plugin_name = "";
355   if ((address_length + plugin_name_length +
356        sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
357   {
358     GNUNET_break (0);
359     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
360     return;
361   }
362   if ((plugin_name_length != 0) &&
363       (plugin_name[plugin_name_length - 1] != '\0'))
364   {
365     GNUNET_break (0);
366     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
367     return;
368   }
369   GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
370   GAS_addresses_destroy (&m->peer, plugin_name, address, address_length,
371                          ntohl (m->session_id));
372   if (0 != ntohl (m->session_id))
373   {
374     srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
375     srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
376     srm.session_id = m->session_id;
377     srm.peer = m->peer;
378     GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
379                                                 GNUNET_NO);
380   }
381   GNUNET_SERVER_receive_done (client, GNUNET_OK);
382 }
383
384
385 /**
386  * Initialize scheduling subsystem.
387  *
388  * @param server handle to our server
389  */
390 void
391 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)
392 {
393   nc = GNUNET_SERVER_notification_context_create (server, 128);
394 }
395
396
397 /**
398  * Shutdown scheduling subsystem.
399  */
400 void
401 GAS_scheduling_done ()
402 {
403   GNUNET_SERVER_notification_context_destroy (nc);
404   nc = NULL;
405 }
406
407
408 /* end of gnunet-service-ats_scheduling.c */