9a16269d9e1d0eed3e983ea1b794c974ce946cd9
[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  * Unregister a client (which may have been a scheduling client,
117  * but this is not assured).
118  *
119  * @param client handle of the (now dead) client
120  */
121 void
122 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
123 {
124   struct SchedulingClient * sc;
125
126   sc = find_client (client);
127   if (NULL == sc)
128     return;
129   GNUNET_CONTAINER_DLL_remove (sc_head, sc_tail, sc);
130   GNUNET_SERVER_client_drop (client);
131   GNUNET_free (sc);
132 }
133
134
135 /**
136  * Transmit the given address suggestion and bandwidth update to all scheduling
137  * clients.
138  *
139  * @param peer peer for which this is an address suggestion
140  * @param plugin_name 0-termintated string specifying the transport plugin
141  * @param plugin_addr binary address for the plugin to use
142  * @param plugin_addr_len number of bytes in plugin_addr
143  * @param session_client which client gave us this session_id?
144  * @param session_id session ID to use for the given client (other clients will see 0)
145  * @param atsi performance data for the address
146  * @param atsi_count number of performance records in 'ats'
147  * @param bandwidth_out assigned outbound bandwidth
148  * @param bandwidth_in assigned inbound bandwidth
149  */
150 void
151 GAS_scheduling_transmit_address_update (const struct GNUNET_PeerIdentity *peer,
152                                         const char *plugin_name,
153                                         const void *plugin_addr, size_t plugin_addr_len,
154                                         struct GNUNET_SERVER_Client *session_client,
155                                         uint32_t session_id,
156                                         const struct GNUNET_TRANSPORT_ATS_Information *atsi,
157                                         uint32_t atsi_count,                            
158                                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
159                                         struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
160 {
161   struct SchedulingClient *sc;
162   struct AddressSuggestionMessage *msg;
163   size_t plugin_name_length = strlen (plugin_name) + 1;
164   size_t msize = sizeof (struct AddressSuggestionMessage) + atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) 
165     + plugin_addr_len + plugin_name_length;
166   char buf[msize];
167   struct GNUNET_TRANSPORT_ATS_Information *atsp;
168   char *addrp;
169
170   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
171   GNUNET_assert (atsi_count < GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information));
172   msg = (struct AddressSuggestionMessage*) buf;
173   msg->header.size = htons (msize);
174   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
175   msg->ats_count = htonl (atsi_count);
176   msg->peer = *peer;
177   msg->address_length = htons (plugin_addr_len);
178   msg->plugin_name_length = htons (plugin_name_length);
179   /* session ID is set only if 'client' is the same... */
180   msg->bandwidth_out = bandwidth_out;
181   msg->bandwidth_in = bandwidth_in;
182   atsp = (struct GNUNET_TRANSPORT_ATS_Information* ) &msg[1];
183   memcpy (atsp, atsi, sizeof (struct GNUNET_TRANSPORT_ATS_Information) * atsi_count);
184   addrp = (char*) &atsp[atsi_count];
185   memcpy (addrp, plugin_addr, plugin_addr_len);
186   strcpy (&addrp[plugin_addr_len], plugin_name);
187   for (sc = sc_head; sc != NULL; sc = sc->next)
188   {
189     if (sc->client == session_client)
190       msg->session_id = htonl (session_id);
191     else
192       msg->session_id = htonl (0);
193     GNUNET_SERVER_notification_context_unicast (nc,
194                                                 sc->client,
195                                                 &msg->header,
196                                                 GNUNET_YES);
197   } 
198 }
199
200
201 /**
202  * Handle 'request address' 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_request_address (void *cls, struct GNUNET_SERVER_Client *client,
210                             const struct GNUNET_MessageHeader *message)
211
212 {
213   const struct RequestAddressMessage * msg = (const struct RequestAddressMessage *) message;
214
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
216   GNUNET_break (0 == ntohl (msg->reserved));
217   GAS_addresses_request_address (&msg->peer);
218   GNUNET_SERVER_receive_done (client, GNUNET_OK);
219 }
220
221
222 /**
223  * Handle 'address update' messages from clients.
224  *
225  * @param cls unused, NULL
226  * @param client client that sent the request
227  * @param message the request message
228  */
229 void
230 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
231                       const struct GNUNET_MessageHeader *message)
232
233 {
234   const struct AddressUpdateMessage * m;
235   const struct GNUNET_TRANSPORT_ATS_Information *atsi;
236   const char *address;
237   const char *plugin_name;
238   uint16_t address_length;
239   uint16_t plugin_name_length;
240   uint32_t ats_count;
241   uint16_t size;
242
243   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
244               "Received `%s' message\n",
245               "ADDRESS_UPDATE");
246   size = ntohs (message->size);
247   if (size <= sizeof (struct AddressUpdateMessage))
248   {
249     GNUNET_break (0);
250     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
251     return;
252   }
253   m = (const struct AddressUpdateMessage*) message;
254   ats_count = ntohl (m->ats_count);
255   address_length = ntohs (m->address_length);
256   plugin_name_length = ntohs (m->plugin_name_length);  
257   atsi = (const struct GNUNET_TRANSPORT_ATS_Information*) &m[1];
258   address = (const char*) &atsi[ats_count];
259   plugin_name = &address[address_length];
260   if ( (address_length +
261         plugin_name_length +
262         ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
263         sizeof (struct AddressSuggestionMessage) != ntohs (message->size))  ||
264        (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
265        (plugin_name[plugin_name_length - 1] != '\0') )
266   {
267     GNUNET_break (0);
268     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
269     return;
270   }
271   GAS_address_update (&m->peer,
272                       plugin_name,
273                       address,
274                       address_length,
275                       client,
276                       ntohl (m->session_id),
277                       atsi,
278                       ats_count);
279   GNUNET_SERVER_receive_done (client, GNUNET_OK);
280 }
281
282
283 /**
284  * Handle 'address destroyed' messages from clients.
285  *
286  * @param cls unused, NULL
287  * @param client client that sent the request
288  * @param message the request message
289  */
290 void
291 GAS_handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
292                               const struct GNUNET_MessageHeader *message)
293
294 {
295   const struct AddressDestroyedMessage * m;
296   const char *address;
297   const char *plugin_name;
298   uint16_t address_length;
299   uint16_t plugin_name_length;
300   uint16_t size;
301
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303               "Received `%s' message\n", 
304               "ADDRESS_DESTROYED");
305   size = ntohs (message->size);
306   if (size <= sizeof (struct AddressDestroyedMessage))
307   {
308     GNUNET_break (0);
309     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
310     return;
311   }
312   m = (const struct AddressDestroyedMessage*) message;
313   GNUNET_break (0 == ntohl (m->reserved));
314   address_length = ntohs (m->address_length);
315   plugin_name_length = ntohs (m->plugin_name_length);  
316   address = (const char*) &m[1];
317   plugin_name = &address[address_length];
318   if ( (address_length +
319         plugin_name_length +
320         sizeof (struct AddressSuggestionMessage) != ntohs (message->size))  ||
321        (plugin_name[plugin_name_length - 1] != '\0') )
322   {
323     GNUNET_break (0);
324     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
325     return;
326   }
327   GAS_address_destroyed (&m->peer,
328                          plugin_name,
329                          address,
330                          address_length,
331                          client,
332                          ntohl (m->session_id));
333   GNUNET_SERVER_receive_done (client, GNUNET_OK);
334 }
335
336
337 /**
338  * Initialize scheduling subsystem.
339  *
340  * @param server handle to our server
341  */
342 void
343 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)
344 {
345   nc = GNUNET_SERVER_notification_context_create (server, 128);
346 }
347
348
349 /**
350  * Shutdown scheduling subsystem.
351  */
352 void
353 GAS_scheduling_done ()
354 {
355   GNUNET_SERVER_notification_context_destroy (nc);
356   nc = NULL;
357 }
358
359
360 /* end of gnunet-service-ats_scheduling.c */