825f00085512f4ad27ef8c59000c51994152fa26
[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_addresses.h"
29 #include "gnunet-service-ats_scheduling.h"
30 #include "ats.h"
31
32
33 /**
34  * We keep clients that are interested in scheduling in a linked list.
35  * This list typically has only one entry (for the
36  * gnunet-service-transport process); however, it is possible that
37  * there is more than one (at least briefly) because after a crash a
38  * new one may connect before we've been notified to clean up the old
39  * process.
40  */
41 struct SchedulingClient
42 {
43   /**
44    * Next in doubly-linked list.
45    */
46   struct SchedulingClient * next;
47
48   /**
49    * Previous in doubly-linked list.
50    */
51   struct SchedulingClient * prev;
52
53   /**
54    * Actual handle to the client.
55    */
56   struct GNUNET_SERVER_Client *client;
57
58 };
59
60
61 /**
62  * Head of linked list of all clients to this service.
63  */
64 static struct SchedulingClient *sc_head;
65
66 /**
67  * Tail of linked list of all clients to this service.
68  */
69 static struct SchedulingClient *sc_tail;
70
71 /**
72  * Context for sending messages to clients.
73  */
74 static struct GNUNET_SERVER_NotificationContext *nc;
75
76
77 /**
78  * Find the scheduling client associated with the given
79  * handle.
80  *
81  * @param client server handle
82  * @return internal handle
83  */
84 static struct SchedulingClient * 
85 find_client (struct GNUNET_SERVER_Client *client)
86 {
87   struct SchedulingClient * sc;
88
89   for (sc = sc_head; sc != NULL; sc = sc->next)
90     if (sc->client == client)
91       return sc;
92   return NULL;
93 }
94
95
96 /**
97  * Register a new scheduling client.
98  *
99  * @param client handle of the new client
100  */
101 void
102 GAS_scheduling_add_client (struct GNUNET_SERVER_Client *client)
103 {
104   struct SchedulingClient *sc;
105
106   GNUNET_break (NULL == find_client (client));
107   sc = GNUNET_malloc (sizeof (struct SchedulingClient));
108   sc->client = client;
109   GNUNET_SERVER_notification_context_add (nc, client);
110   GNUNET_SERVER_client_keep (client);
111   GNUNET_CONTAINER_DLL_insert(sc_head, sc_tail, sc);
112 }
113
114
115
116 /**
117  * Unregister a client (which may have been a scheduling client,
118  * but this is not assured).
119  *
120  * @param client handle of the (now dead) client
121  */
122 void
123 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
124 {
125   struct SchedulingClient * sc;
126
127   sc = find_client (client);
128   if (NULL == sc)
129     return;
130   GNUNET_CONTAINER_DLL_remove (sc_head, sc_tail, sc);
131   GAS_address_client_disconnected (client);
132   GNUNET_SERVER_client_drop (client);
133   GNUNET_free (sc);
134 }
135
136
137 /**
138  * Transmit the given address suggestion and bandwidth update to all scheduling
139  * clients.
140  *
141  * @param peer peer for which this is an address suggestion
142  * @param plugin_name 0-termintated string specifying the transport plugin
143  * @param plugin_addr binary address for the plugin to use
144  * @param plugin_addr_len number of bytes in plugin_addr
145  * @param session_client which client gave us this session_id?
146  * @param session_id session ID to use for the given client (other clients will see 0)
147  * @param atsi performance data for the address
148  * @param atsi_count number of performance records in 'ats'
149  * @param bandwidth_out assigned outbound bandwidth
150  * @param bandwidth_in assigned inbound bandwidth
151  */
152 void
153 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity *peer,
154                                             const char *plugin_name,
155                                             const void *plugin_addr, size_t plugin_addr_len,
156                                             struct GNUNET_SERVER_Client *session_client,
157                                             uint32_t session_id,
158                                             const struct GNUNET_ATS_Information *atsi,
159                                             uint32_t atsi_count,                                
160                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
161                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
162 {
163   struct SchedulingClient *sc;
164   struct AddressSuggestionMessage *msg;
165   size_t plugin_name_length = strlen (plugin_name) + 1;
166   size_t msize = sizeof (struct AddressSuggestionMessage) + atsi_count * sizeof (struct GNUNET_ATS_Information) 
167     + plugin_addr_len + plugin_name_length;
168   char buf[msize];
169   struct GNUNET_ATS_Information *atsp;
170   char *addrp;
171
172   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
173   GNUNET_assert (atsi_count < GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information));
174   msg = (struct AddressSuggestionMessage*) buf;
175   msg->header.size = htons (msize);
176   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
177   msg->ats_count = htonl (atsi_count);
178   msg->peer = *peer;
179   msg->address_length = htons (plugin_addr_len);
180   msg->plugin_name_length = htons (plugin_name_length);
181   /* session ID is set only if 'client' is the same... */
182   msg->bandwidth_out = bandwidth_out;
183   msg->bandwidth_in = bandwidth_in;
184   atsp = (struct GNUNET_ATS_Information* ) &msg[1];
185   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
186   addrp = (char*) &atsp[atsi_count];
187   memcpy (addrp, plugin_addr, plugin_addr_len);
188   strcpy (&addrp[plugin_addr_len], plugin_name);
189   for (sc = sc_head; sc != NULL; sc = sc->next)
190   {
191     if (sc->client == session_client)
192       msg->session_id = htonl (session_id);
193     else
194       msg->session_id = htonl (0);
195     GNUNET_SERVER_notification_context_unicast (nc,
196                                                 sc->client,
197                                                 &msg->header,
198                                                 GNUNET_YES);
199   } 
200 }
201
202
203 /**
204  * Handle 'request address' messages from clients.
205  *
206  * @param cls unused, NULL
207  * @param client client that sent the request
208  * @param message the request message
209  */
210 void
211 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
212                             const struct GNUNET_MessageHeader *message)
213
214 {
215   const struct RequestAddressMessage * msg = (const struct RequestAddressMessage *) message;
216
217   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
218   GNUNET_break (0 == ntohl (msg->reserved));
219   GAS_addresses_request_address (&msg->peer);
220   GNUNET_SERVER_receive_done (client, GNUNET_OK);
221 }
222
223
224 /**
225  * Handle 'address update' messages from clients.
226  *
227  * @param cls unused, NULL
228  * @param client client that sent the request
229  * @param message the request message
230  */
231 void
232 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
233                       const struct GNUNET_MessageHeader *message)
234
235 {
236   const struct AddressUpdateMessage * m;
237   const struct GNUNET_ATS_Information *atsi;
238   const char *address;
239   const char *plugin_name;
240   uint16_t address_length;
241   uint16_t plugin_name_length;
242   uint32_t ats_count;
243   uint16_t size;
244
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
246               "Received `%s' message\n",
247               "ADDRESS_UPDATE");
248   size = ntohs (message->size);
249   if (size <= sizeof (struct AddressUpdateMessage))
250   {
251     GNUNET_break (0);
252     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
253     return;
254   }
255   m = (const struct AddressUpdateMessage*) message;
256   ats_count = ntohl (m->ats_count);
257   address_length = ntohs (m->address_length);
258   plugin_name_length = ntohs (m->plugin_name_length);  
259   atsi = (const struct GNUNET_ATS_Information*) &m[1];
260   address = (const char*) &atsi[ats_count];
261   if (plugin_name_length != 0)
262     plugin_name = &address[address_length];
263   else
264     plugin_name = "";
265   if ( (address_length +
266         plugin_name_length +
267         ats_count * sizeof (struct GNUNET_ATS_Information) +
268         sizeof (struct AddressUpdateMessage) != ntohs (message->size))  ||
269        (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
270        (plugin_name[plugin_name_length - 1] != '\0') )
271   {
272     GNUNET_break (0);
273     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
274     return;
275   }
276   GAS_address_update (&m->peer,
277                       plugin_name,
278                       address,
279                       address_length,
280                       client,
281                       ntohl (m->session_id),
282                       atsi,
283                       ats_count);
284   GNUNET_SERVER_receive_done (client, GNUNET_OK);
285 }
286
287
288 /**
289  * Handle 'address destroyed' 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_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
297                               const struct GNUNET_MessageHeader *message)
298
299 {
300   const struct AddressDestroyedMessage * m;
301   const char *address;
302   const char *plugin_name;
303   uint16_t address_length;
304   uint16_t plugin_name_length;
305   uint16_t size;
306
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "Received `%s' message of size %u %u\n",
309               "ADDRESS_DESTROYED", ntohs (message->size), sizeof (struct AddressDestroyedMessage));
310   size = ntohs (message->size);
311   if (size < sizeof (struct AddressDestroyedMessage))
312   {
313     GNUNET_break (0);
314     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
315     return;
316   }
317   m = (const struct AddressDestroyedMessage*) message;
318   GNUNET_break (0 == ntohl (m->reserved));
319   address_length = ntohs (m->address_length);
320   plugin_name_length = ntohs (m->plugin_name_length);  
321   address = (const char*) &m[1];
322   if (plugin_name_length != 0)
323     plugin_name = &address[address_length];
324   else
325     plugin_name = "";
326
327   if ( (address_length +
328         plugin_name_length +
329         sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
330   {
331     GNUNET_break (0);
332     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
333     return;
334   }
335
336   if (plugin_name_length != 0)
337     if (plugin_name[plugin_name_length - 1] != '\0')
338     {
339       GNUNET_break (0);
340       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
341       return;
342     }
343
344   GAS_address_destroyed (&m->peer,
345                          plugin_name,
346                          address,
347                          address_length,
348                          client,
349                          ntohl (m->session_id));
350   GNUNET_SERVER_receive_done (client, GNUNET_OK);
351 }
352
353
354 /**
355  * Initialize scheduling subsystem.
356  *
357  * @param server handle to our server
358  */
359 void
360 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)                    
361 {
362   nc = GNUNET_SERVER_notification_context_create (server, 128);
363 }
364
365
366 /**
367  * Shutdown scheduling subsystem.
368  */
369 void
370 GAS_scheduling_done ()
371 {
372   GNUNET_SERVER_notification_context_destroy (nc);
373   nc = NULL;
374 }
375
376
377 /* end of gnunet-service-ats_scheduling.c */