extending ats api to inform about addresses in use
[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 *peer,
100                                             const char *plugin_name,
101                                             const void *plugin_addr, size_t plugin_addr_len,
102                                             uint32_t session_id,
103                                             const struct GNUNET_ATS_Information *atsi,
104                                             uint32_t atsi_count,                                
105                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
106                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
107 {
108   struct AddressSuggestionMessage *msg;
109   size_t plugin_name_length = strlen (plugin_name) + 1;
110   size_t msize = sizeof (struct AddressSuggestionMessage) + atsi_count * sizeof (struct GNUNET_ATS_Information) 
111     + plugin_addr_len + plugin_name_length;
112   char buf[msize];
113   struct GNUNET_ATS_Information *atsp;
114   char *addrp;
115
116   if (my_client == NULL)
117     return;
118   GNUNET_STATISTICS_update (GSA_stats,
119                             "# address suggestions made",
120                             1,
121                             GNUNET_NO);
122   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
123   GNUNET_assert (atsi_count < GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information));
124   msg = (struct AddressSuggestionMessage*) buf;
125   msg->header.size = htons (msize);
126   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
127   msg->ats_count = htonl (atsi_count);
128   msg->peer = *peer;
129   msg->address_length = htons (plugin_addr_len);
130   msg->plugin_name_length = htons (plugin_name_length);
131   msg->session_id = htonl (session_id);
132   msg->bandwidth_out = bandwidth_out;
133   msg->bandwidth_in = bandwidth_in;
134   atsp = (struct GNUNET_ATS_Information* ) &msg[1];
135   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
136   addrp = (char*) &atsp[atsi_count];
137   memcpy (addrp, plugin_addr, plugin_addr_len);
138   strcpy (&addrp[plugin_addr_len], plugin_name);
139   GNUNET_SERVER_notification_context_unicast (nc,
140                                               my_client,
141                                               &msg->header,
142                                               GNUNET_YES);
143 }
144
145
146 /**
147  * Handle 'request address' messages from clients.
148  *
149  * @param cls unused, NULL
150  * @param client client that sent the request
151  * @param message the request message
152  */
153 void
154 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
155                             const struct GNUNET_MessageHeader *message)
156
157 {
158   const struct RequestAddressMessage * msg = (const struct RequestAddressMessage *) message;
159
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
161   GNUNET_STATISTICS_update (GSA_stats,
162                             "# address requests received",
163                             1,
164                             GNUNET_NO);
165   GNUNET_break (0 == ntohl (msg->reserved));
166   GAS_addresses_request_address (&msg->peer);
167   GNUNET_SERVER_receive_done (client, GNUNET_OK);
168 }
169
170
171 /**
172  * Handle 'address update' messages from clients.
173  *
174  * @param cls unused, NULL
175  * @param client client that sent the request
176  * @param message the request message
177  */
178 void
179 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
180                            const struct GNUNET_MessageHeader *message)
181 {
182   const struct AddressUpdateMessage * m;
183   const struct GNUNET_ATS_Information *atsi;
184   const char *address;
185   const char *plugin_name;
186   uint16_t address_length;
187   uint16_t plugin_name_length;
188   uint32_t ats_count;
189   uint16_t size;
190
191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
192               "Received `%s' message\n",
193               "ADDRESS_UPDATE");
194   size = ntohs (message->size);
195   if (size < sizeof (struct AddressUpdateMessage))
196   {
197     GNUNET_break (0);
198     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
199     return;
200   }
201   m = (const struct AddressUpdateMessage*) message;
202   ats_count = ntohl (m->ats_count);
203   address_length = ntohs (m->address_length);
204   plugin_name_length = ntohs (m->plugin_name_length);  
205   atsi = (const struct GNUNET_ATS_Information*) &m[1];
206   address = (const char*) &atsi[ats_count];
207   if (plugin_name_length != 0)
208     plugin_name = &address[address_length];
209   else
210     plugin_name = "";
211   if ( (address_length +
212         plugin_name_length +
213         ats_count * sizeof (struct GNUNET_ATS_Information) +
214         sizeof (struct AddressUpdateMessage) != ntohs (message->size))  ||
215        (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
216        (plugin_name[plugin_name_length - 1] != '\0') )
217   {
218     GNUNET_break (0);
219     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
220     return;
221   }
222   GNUNET_STATISTICS_update (GSA_stats,
223                             "# address updates received",
224                             1,
225                             GNUNET_NO);
226   GAS_addresses_update (&m->peer,
227                         plugin_name,
228                         address,
229                         address_length,
230                         ntohl (m->session_id),
231                         atsi,
232                         ats_count);
233   GNUNET_SERVER_receive_done (client, GNUNET_OK);
234 }
235
236
237 /**
238  * Handle 'address in use' messages from clients.
239  *
240  * @param cls unused, NULL
241  * @param client client that sent the request
242  * @param message the request message
243  */
244 void
245 GAS_handle_address_in_use (void *cls, struct GNUNET_SERVER_Client *client,
246                            const struct GNUNET_MessageHeader *message)
247 {
248   const struct AddressUseMessage * m;
249   const char *address;
250   const char *plugin_name;
251   uint16_t address_length;
252   uint16_t plugin_name_length;
253
254   uint16_t size;
255
256   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257               "Received `%s' message\n",
258               "ADDRESS_IN_USE");
259   size = ntohs (message->size);
260   if (size < sizeof (struct AddressUseMessage))
261   {
262     GNUNET_break (0);
263     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
264     return;
265   }
266   m = (const struct AddressUseMessage*) message;
267
268   address_length = ntohs (m->address_length);
269   plugin_name_length = ntohs (m->plugin_name_length);
270
271   address = (const char*) &m[1];
272   if (plugin_name_length != 0)
273     plugin_name = &address[address_length];
274   else
275     plugin_name = "";
276
277   if ( (address_length +
278         plugin_name_length +
279         sizeof (struct AddressUseMessage) != ntohs (message->size))  ||
280        (plugin_name[plugin_name_length - 1] != '\0') )
281   {
282     GNUNET_break (0);
283     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
284     return;
285   }
286
287
288 /*
289   GAS_addresses_update (&m->peer,
290                         plugin_name,
291                         address,
292                         address_length,
293                         ntohl (m->session_id),
294                         atsi,
295                         ats_count);
296 */
297   GNUNET_SERVER_receive_done (client, GNUNET_OK);
298 }
299
300 /**
301  * Handle 'address destroyed' messages from clients.
302  *
303  * @param cls unused, NULL
304  * @param client client that sent the request
305  * @param message the request message
306  */
307 void
308 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
309                               const struct GNUNET_MessageHeader *message)
310
311 {
312   const struct AddressDestroyedMessage * m;
313   struct SessionReleaseMessage srm;
314   const char *address;
315   const char *plugin_name;
316   uint16_t address_length;
317   uint16_t plugin_name_length;
318   uint16_t size;
319
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321               "Received `%s' message of size %u %u\n",
322               "ADDRESS_DESTROYED", ntohs (message->size), sizeof (struct AddressDestroyedMessage));
323   size = ntohs (message->size);
324   if ( (size < sizeof (struct AddressDestroyedMessage)) ||
325        (client != my_client) )
326   {
327     GNUNET_break (0);
328     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
329     return;
330   }
331   m = (const struct AddressDestroyedMessage*) message;
332   GNUNET_break (0 == ntohl (m->reserved));
333   address_length = ntohs (m->address_length);
334   plugin_name_length = ntohs (m->plugin_name_length);  
335   address = (const char*) &m[1];
336   if (plugin_name_length != 0)
337     plugin_name = &address[address_length];
338   else
339     plugin_name = "";
340   if ( (address_length +
341         plugin_name_length +
342         sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
343   {
344     GNUNET_break (0);
345     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
346     return;
347   }  
348   if ( (plugin_name_length != 0) &&
349        (plugin_name[plugin_name_length - 1] != '\0') )
350   {
351     GNUNET_break (0);
352     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
353     return;
354   }
355   GNUNET_STATISTICS_update (GSA_stats,
356                             "# addresses destroyed",
357                             1,
358                             GNUNET_NO);
359   GAS_addresses_destroy (&m->peer,
360                          plugin_name,
361                          address,
362                          address_length,
363                          ntohl (m->session_id));
364   if (0 != ntohl (m->session_id))
365   {
366     srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
367     srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
368     srm.session_id = m->session_id;
369     srm.peer = m->peer;
370     GNUNET_SERVER_notification_context_unicast (nc,
371                                                 client,
372                                                 &srm.header,
373                                                 GNUNET_NO);
374   }
375   GNUNET_SERVER_receive_done (client, GNUNET_OK);
376 }
377
378
379 /**
380  * Initialize scheduling subsystem.
381  *
382  * @param server handle to our server
383  */
384 void
385 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)                    
386 {
387   nc = GNUNET_SERVER_notification_context_create (server, 128);
388 }
389
390
391 /**
392  * Shutdown scheduling subsystem.
393  */
394 void
395 GAS_scheduling_done ()
396 {
397   GNUNET_SERVER_notification_context_destroy (nc);
398   nc = NULL;
399 }
400
401
402 /* end of gnunet-service-ats_scheduling.c */