4af44e29bd4567598f6f09470c2c075168ea8414
[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   GNUNET_SERVER_notification_context_unicast (nc, my_client, &msg->header,
145                                               GNUNET_YES);
146 }
147
148
149 /**
150  * Handle 'request address' messages from clients.
151  *
152  * @param cls unused, NULL
153  * @param client client that sent the request
154  * @param message the request message
155  */
156 void
157 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
158                             const struct GNUNET_MessageHeader *message)
159 {
160   const struct RequestAddressMessage *msg =
161       (const struct RequestAddressMessage *) message;
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
164               "REQUEST_ADDRESS");
165   GNUNET_STATISTICS_update (GSA_stats, "# address requests received", 1,
166                             GNUNET_NO);
167   GNUNET_break (0 == ntohl (msg->reserved));
168   GAS_addresses_request_address (&msg->peer);
169   GNUNET_SERVER_receive_done (client, GNUNET_OK);
170 }
171
172
173 /**
174  * Handle 'address update' messages from clients.
175  *
176  * @param cls unused, NULL
177  * @param client client that sent the request
178  * @param message the request message
179  */
180 void
181 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
182                            const struct GNUNET_MessageHeader *message)
183 {
184   const struct AddressUpdateMessage *m;
185   const struct GNUNET_ATS_Information *atsi;
186   const char *address;
187   const char *plugin_name;
188   uint16_t address_length;
189   uint16_t plugin_name_length;
190   uint32_t ats_count;
191   uint16_t size;
192
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
194               "ADDRESS_UPDATE");
195   size = ntohs (message->size);
196   if (size < sizeof (struct AddressUpdateMessage))
197   {
198     GNUNET_break (0);
199     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
200     return;
201   }
202   m = (const struct AddressUpdateMessage *) message;
203   ats_count = ntohl (m->ats_count);
204   address_length = ntohs (m->address_length);
205   plugin_name_length = ntohs (m->plugin_name_length);
206   atsi = (const struct GNUNET_ATS_Information *) &m[1];
207   address = (const char *) &atsi[ats_count];
208   if (plugin_name_length != 0)
209     plugin_name = &address[address_length];
210   else
211     plugin_name = "";
212   if ((address_length + plugin_name_length +
213        ats_count * sizeof (struct GNUNET_ATS_Information) +
214        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
215       (ats_count >
216        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
217       || (plugin_name[plugin_name_length - 1] != '\0'))
218   {
219     GNUNET_break (0);
220     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
221     return;
222   }
223   GNUNET_STATISTICS_update (GSA_stats, "# address updates received", 1,
224                             GNUNET_NO);
225   GAS_addresses_update (&m->peer, plugin_name, address, address_length,
226                         ntohl (m->session_id), atsi, ats_count);
227   GNUNET_SERVER_receive_done (client, GNUNET_OK);
228 }
229
230
231 /**
232  * Handle 'address in use' messages from clients.
233  *
234  * @param cls unused, NULL
235  * @param client client that sent the request
236  * @param message the request message
237  */
238 void
239 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
240                            const struct GNUNET_MessageHeader *message)
241 {
242   const struct AddressUseMessage *m;
243   const char *address;
244   const char *plugin_name;
245   uint16_t address_length;
246   uint16_t plugin_name_length;
247
248   uint16_t size;
249
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
251               "ADDRESS_IN_USE");
252   size = ntohs (message->size);
253   if (size < sizeof (struct AddressUseMessage))
254   {
255     GNUNET_break (0);
256     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
257     return;
258   }
259   m = (const struct AddressUseMessage *) message;
260
261   address_length = ntohs (m->address_length);
262   plugin_name_length = ntohs (m->plugin_name_length);
263
264   address = (const char *) &m[1];
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        sizeof (struct AddressUseMessage) != ntohs (message->size)) ||
272       (plugin_name[plugin_name_length - 1] != '\0'))
273   {
274     GNUNET_break (0);
275     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
276     return;
277   }
278
279
280 /*
281   GAS_addresses_update (&m->peer,
282                         plugin_name,
283                         address,
284                         address_length,
285                         ntohl (m->session_id),
286                         atsi,
287                         ats_count);
288 */
289   GNUNET_SERVER_receive_done (client, GNUNET_OK);
290 }
291
292 /**
293  * Handle 'address destroyed' messages from clients.
294  *
295  * @param cls unused, NULL
296  * @param client client that sent the request
297  * @param message the request message
298  */
299 void
300 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
301                               const struct GNUNET_MessageHeader *message)
302 {
303   const struct AddressDestroyedMessage *m;
304   struct SessionReleaseMessage srm;
305   const char *address;
306   const char *plugin_name;
307   uint16_t address_length;
308   uint16_t plugin_name_length;
309   uint16_t size;
310
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message of size %u %u\n",
312               "ADDRESS_DESTROYED", ntohs (message->size),
313               sizeof (struct AddressDestroyedMessage));
314   size = ntohs (message->size);
315   if ((size < sizeof (struct AddressDestroyedMessage)) || (client != my_client))
316   {
317     GNUNET_break (0);
318     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
319     return;
320   }
321   m = (const struct AddressDestroyedMessage *) message;
322   GNUNET_break (0 == ntohl (m->reserved));
323   address_length = ntohs (m->address_length);
324   plugin_name_length = ntohs (m->plugin_name_length);
325   address = (const char *) &m[1];
326   if (plugin_name_length != 0)
327     plugin_name = &address[address_length];
328   else
329     plugin_name = "";
330   if ((address_length + plugin_name_length +
331        sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
332   {
333     GNUNET_break (0);
334     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
335     return;
336   }
337   if ((plugin_name_length != 0) &&
338       (plugin_name[plugin_name_length - 1] != '\0'))
339   {
340     GNUNET_break (0);
341     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
342     return;
343   }
344   GNUNET_STATISTICS_update (GSA_stats, "# addresses destroyed", 1, GNUNET_NO);
345   GAS_addresses_destroy (&m->peer, plugin_name, address, address_length,
346                          ntohl (m->session_id));
347   if (0 != ntohl (m->session_id))
348   {
349     srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
350     srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
351     srm.session_id = m->session_id;
352     srm.peer = m->peer;
353     GNUNET_SERVER_notification_context_unicast (nc, client, &srm.header,
354                                                 GNUNET_NO);
355   }
356   GNUNET_SERVER_receive_done (client, GNUNET_OK);
357 }
358
359
360 /**
361  * Initialize scheduling subsystem.
362  *
363  * @param server handle to our server
364  */
365 void
366 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)
367 {
368   nc = GNUNET_SERVER_notification_context_create (server, 128);
369 }
370
371
372 /**
373  * Shutdown scheduling subsystem.
374  */
375 void
376 GAS_scheduling_done ()
377 {
378   GNUNET_SERVER_notification_context_destroy (nc);
379   nc = NULL;
380 }
381
382
383 /* end of gnunet-service-ats_scheduling.c */