3d53c1e42953877f69c9df6ce595be0560d65b0c
[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  * Context for sending messages to clients.
35  */
36 static struct GNUNET_SERVER_NotificationContext *nc;
37
38 /**
39  * Actual handle to the client.
40  */
41 static struct GNUNET_SERVER_Client *my_client;
42
43
44 /**
45  * Register a new scheduling client.
46  *
47  * @param client handle of the new client
48  * @return GNUNET_OK on success, GNUNET_SYSERR on error 
49  */
50 int
51 GAS_scheduling_add_client (struct GNUNET_SERVER_Client *client)
52 {
53   if (my_client != NULL)
54   {
55     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
56                 "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
57     return GNUNET_SYSERR;
58   }
59   my_client = client;
60   GNUNET_SERVER_notification_context_add (nc, client);
61   GNUNET_SERVER_client_keep (client);
62   return GNUNET_OK;
63 }
64
65
66 /**
67  * Unregister a client (which may have been a scheduling client,
68  * but this is not assured).
69  *
70  * @param client handle of the (now dead) client
71  */
72 void
73 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
74 {
75   if (my_client != client)
76     return;
77   GAS_addresses_destroy_all ();
78   GNUNET_SERVER_client_drop (client);
79   my_client = NULL;
80 }
81
82
83 /**
84  * Transmit the given address suggestion and bandwidth update to all scheduling
85  * clients.
86  *
87  * @param peer peer for which this is an address suggestion
88  * @param plugin_name 0-termintated string specifying the transport plugin
89  * @param plugin_addr binary address for the plugin to use
90  * @param plugin_addr_len number of bytes in plugin_addr
91  * @param session_id session ID to use for the given client (other clients will see 0)
92  * @param atsi performance data for the address
93  * @param atsi_count number of performance records in 'ats'
94  * @param bandwidth_out assigned outbound bandwidth
95  * @param bandwidth_in assigned inbound bandwidth
96  */
97 void
98 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity *peer,
99                                             const char *plugin_name,
100                                             const void *plugin_addr, size_t plugin_addr_len,
101                                             uint32_t session_id,
102                                             const struct GNUNET_ATS_Information *atsi,
103                                             uint32_t atsi_count,                                
104                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
105                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
106 {
107   struct AddressSuggestionMessage *msg;
108   size_t plugin_name_length = strlen (plugin_name) + 1;
109   size_t msize = sizeof (struct AddressSuggestionMessage) + atsi_count * sizeof (struct GNUNET_ATS_Information) 
110     + plugin_addr_len + plugin_name_length;
111   char buf[msize];
112   struct GNUNET_ATS_Information *atsp;
113   char *addrp;
114
115   if (my_client == NULL)
116     return;
117   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
118   GNUNET_assert (atsi_count < GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information));
119   msg = (struct AddressSuggestionMessage*) buf;
120   msg->header.size = htons (msize);
121   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
122   msg->ats_count = htonl (atsi_count);
123   msg->peer = *peer;
124   msg->address_length = htons (plugin_addr_len);
125   msg->plugin_name_length = htons (plugin_name_length);
126   msg->session_id = htonl (session_id);
127   msg->bandwidth_out = bandwidth_out;
128   msg->bandwidth_in = bandwidth_in;
129   atsp = (struct GNUNET_ATS_Information* ) &msg[1];
130   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
131   addrp = (char*) &atsp[atsi_count];
132   memcpy (addrp, plugin_addr, plugin_addr_len);
133   strcpy (&addrp[plugin_addr_len], plugin_name);
134   GNUNET_SERVER_notification_context_unicast (nc,
135                                               my_client,
136                                               &msg->header,
137                                               GNUNET_YES);
138 }
139
140
141 /**
142  * Handle 'request address' messages from clients.
143  *
144  * @param cls unused, NULL
145  * @param client client that sent the request
146  * @param message the request message
147  */
148 void
149 GAS_handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
150                             const struct GNUNET_MessageHeader *message)
151
152 {
153   const struct RequestAddressMessage * msg = (const struct RequestAddressMessage *) message;
154
155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
156   GNUNET_break (0 == ntohl (msg->reserved));
157   GAS_addresses_request_address (&msg->peer);
158   GNUNET_SERVER_receive_done (client, GNUNET_OK);
159 }
160
161
162 /**
163  * Handle 'address update' messages from clients.
164  *
165  * @param cls unused, NULL
166  * @param client client that sent the request
167  * @param message the request message
168  */
169 void
170 GAS_handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
171                            const struct GNUNET_MessageHeader *message)
172 {
173   const struct AddressUpdateMessage * m;
174   const struct GNUNET_ATS_Information *atsi;
175   const char *address;
176   const char *plugin_name;
177   uint16_t address_length;
178   uint16_t plugin_name_length;
179   uint32_t ats_count;
180   uint16_t size;
181
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
183               "Received `%s' message\n",
184               "ADDRESS_UPDATE");
185   size = ntohs (message->size);
186   if (size <= sizeof (struct AddressUpdateMessage))
187   {
188     GNUNET_break (0);
189     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
190     return;
191   }
192   m = (const struct AddressUpdateMessage*) message;
193   ats_count = ntohl (m->ats_count);
194   address_length = ntohs (m->address_length);
195   plugin_name_length = ntohs (m->plugin_name_length);  
196   atsi = (const struct GNUNET_ATS_Information*) &m[1];
197   address = (const char*) &atsi[ats_count];
198   if (plugin_name_length != 0)
199     plugin_name = &address[address_length];
200   else
201     plugin_name = "";
202   if ( (address_length +
203         plugin_name_length +
204         ats_count * sizeof (struct GNUNET_ATS_Information) +
205         sizeof (struct AddressUpdateMessage) != ntohs (message->size))  ||
206        (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
207        (plugin_name[plugin_name_length - 1] != '\0') )
208   {
209     GNUNET_break (0);
210     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
211     return;
212   }
213   GAS_addresses_update (&m->peer,
214                         plugin_name,
215                         address,
216                         address_length,
217                         ntohl (m->session_id),
218                         atsi,
219                         ats_count);
220   GNUNET_SERVER_receive_done (client, GNUNET_OK);
221 }
222
223
224 /**
225  * Handle 'address destroyed' 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_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
233                               const struct GNUNET_MessageHeader *message)
234
235 {
236   const struct AddressDestroyedMessage * m;
237   const char *address;
238   const char *plugin_name;
239   uint16_t address_length;
240   uint16_t plugin_name_length;
241   uint16_t size;
242
243   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244               "Received `%s' message of size %u %u\n",
245               "ADDRESS_DESTROYED", ntohs (message->size), sizeof (struct AddressDestroyedMessage));
246   size = ntohs (message->size);
247   if ( (size < sizeof (struct AddressDestroyedMessage)) ||
248        (client != my_client) )
249   {
250     GNUNET_break (0);
251     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
252     return;
253   }
254   m = (const struct AddressDestroyedMessage*) message;
255   GNUNET_break (0 == ntohl (m->reserved));
256   address_length = ntohs (m->address_length);
257   plugin_name_length = ntohs (m->plugin_name_length);  
258   address = (const char*) &m[1];
259   if (plugin_name_length != 0)
260     plugin_name = &address[address_length];
261   else
262     plugin_name = "";
263   if ( (address_length +
264         plugin_name_length +
265         sizeof (struct AddressDestroyedMessage) != ntohs (message->size)))
266   {
267     GNUNET_break (0);
268     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
269     return;
270   }  
271   if ( (plugin_name_length != 0) &&
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   GAS_addresses_destroy (&m->peer,
279                          plugin_name,
280                          address,
281                          address_length,
282                          ntohl (m->session_id));
283   GNUNET_SERVER_receive_done (client, GNUNET_OK);
284 }
285
286
287 /**
288  * Initialize scheduling subsystem.
289  *
290  * @param server handle to our server
291  */
292 void
293 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)                    
294 {
295   nc = GNUNET_SERVER_notification_context_create (server, 128);
296 }
297
298
299 /**
300  * Shutdown scheduling subsystem.
301  */
302 void
303 GAS_scheduling_done ()
304 {
305   GNUNET_SERVER_notification_context_destroy (nc);
306   nc = NULL;
307 }
308
309
310 /* end of gnunet-service-ats_scheduling.c */