-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / gnunet-service-ats_scheduling.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-2014 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 (NULL != my_client)
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,
61                                           client);
62   GNUNET_SERVER_client_set_user_context (client,
63                                          &nc);
64   return GNUNET_OK;
65 }
66
67
68 /**
69  * Unregister a client (which may have been a scheduling client,
70  * but this is not assured).
71  *
72  * @param client handle of the (now dead) client
73  */
74 void
75 GAS_scheduling_remove_client (struct GNUNET_SERVER_Client *client)
76 {
77   if (my_client != client)
78     return;
79   GAS_addresses_destroy_all ();
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 session_id session ID to use for the given client
90  * @param bandwidth_out assigned outbound bandwidth
91  * @param bandwidth_in assigned inbound bandwidth
92  */
93 void
94 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity *peer,
95                                             uint32_t session_id,
96                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
97                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
98 {
99   struct AddressSuggestionMessage msg;
100
101   if (NULL == my_client)
102     return;
103   GNUNET_STATISTICS_update (GSA_stats,
104                             "# address suggestions made",
105                             1,
106                             GNUNET_NO);
107   msg.header.size = htons (sizeof (struct AddressSuggestionMessage));
108   msg.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
109   msg.peer = *peer;
110   msg.session_id = htonl (session_id);
111   msg.bandwidth_out = bandwidth_out;
112   msg.bandwidth_in = bandwidth_in;
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114               "ATS sends quota for peer `%s': (in/out) %u/%u\n",
115               GNUNET_i2s (peer),
116               (unsigned int) ntohl (bandwidth_in.value__),
117               (unsigned int) ntohl (bandwidth_out.value__));
118   GNUNET_SERVER_notification_context_unicast (nc,
119                                               my_client,
120                                               &msg.header,
121                                               GNUNET_YES);
122 }
123
124
125 /**
126  * Handle 'address add' messages from clients.
127  *
128  * @param cls unused, NULL
129  * @param client client that sent the request
130  * @param message the request message
131  */
132 void
133 GAS_handle_address_add (void *cls,
134                         struct GNUNET_SERVER_Client *client,
135                         const struct GNUNET_MessageHeader *message)
136 {
137   const struct AddressAddMessage *m;
138   const struct GNUNET_ATS_Information *atsi;
139   const char *address;
140   const char *plugin_name;
141   uint16_t address_length;
142   uint16_t plugin_name_length;
143   uint32_t ats_count;
144   uint16_t size;
145
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "Received `%s' message\n",
148               "ADDRESS_ADD");
149   size = ntohs (message->size);
150   if (size < sizeof (struct AddressAddMessage))
151   {
152     GNUNET_break (0);
153     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
154     return;
155   }
156   m = (const struct AddressAddMessage *) message;
157   ats_count = ntohl (m->ats_count);
158   address_length = ntohs (m->address_length);
159   plugin_name_length = ntohs (m->plugin_name_length);
160   atsi = (const struct GNUNET_ATS_Information *) &m[1];
161   address = (const char *) &atsi[ats_count];
162   if (plugin_name_length != 0)
163     plugin_name = &address[address_length];
164   else
165     plugin_name = "";
166
167   if ((address_length + plugin_name_length +
168        ats_count * sizeof (struct GNUNET_ATS_Information) +
169        sizeof (struct AddressAddMessage) != ntohs (message->size)) ||
170       (ats_count >
171        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
172        ((plugin_name_length > 0) && (plugin_name[plugin_name_length - 1] != '\0')))
173   {
174     GNUNET_break (0);
175     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
176     return;
177   }
178   GNUNET_STATISTICS_update (GSA_stats,
179                             "# addresses created", 1,
180                             GNUNET_NO);
181   GAS_addresses_add (&m->peer,
182                      plugin_name,
183                      address,
184                      address_length,
185                      ntohl (m->address_local_info),
186                      ntohl (m->session_id),
187                      atsi, ats_count);
188   GNUNET_SERVER_receive_done (client, GNUNET_OK);
189 }
190
191
192 /**
193  * Handle 'address update' messages from clients.
194  *
195  * @param cls unused, NULL
196  * @param client client that sent the request
197  * @param message the request message
198  */
199 void
200 GAS_handle_address_update (void *cls,
201                            struct GNUNET_SERVER_Client *client,
202                            const struct GNUNET_MessageHeader *message)
203 {
204   const struct AddressUpdateMessage *m;
205   const struct GNUNET_ATS_Information *atsi;
206   uint32_t ats_count;
207   uint16_t size;
208
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210               "Received `%s' message\n",
211               "ADDRESS_UPDATE");
212   size = ntohs (message->size);
213   if (size < sizeof (struct AddressUpdateMessage))
214   {
215     GNUNET_break (0);
216     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
217     return;
218   }
219   m = (const struct AddressUpdateMessage *) message;
220   ats_count = ntohl (m->ats_count);
221   atsi = (const struct GNUNET_ATS_Information *) &m[1];
222
223   if ((ats_count * sizeof (struct GNUNET_ATS_Information) +
224        sizeof (struct AddressUpdateMessage) != ntohs (message->size)) ||
225       (ats_count >
226        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
227   {
228     GNUNET_break (0);
229     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
230     return;
231   }
232   GNUNET_STATISTICS_update (GSA_stats,
233                             "# address updates received",
234                             1,
235                             GNUNET_NO);
236   GAS_addresses_update (&m->peer,
237                         ntohl (m->session_id),
238                         atsi, ats_count);
239   GNUNET_SERVER_receive_done (client, GNUNET_OK);
240 }
241
242
243 /**
244  * Handle 'address destroyed' messages from clients.
245  *
246  * @param cls unused, NULL
247  * @param client client that sent the request
248  * @param message the request message
249  */
250 void
251 GAS_handle_address_destroyed (void *cls,
252                               struct GNUNET_SERVER_Client *client,
253                               const struct GNUNET_MessageHeader *message)
254 {
255   const struct AddressDestroyedMessage *m;
256   struct SessionReleaseMessage srm;
257
258   m = (const struct AddressDestroyedMessage *) message;
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260               "Received `%s' message\n",
261               "ADDRESS_DESTROYED");
262   GNUNET_STATISTICS_update (GSA_stats,
263                             "# addresses destroyed",
264                             1,
265                             GNUNET_NO);
266   GAS_addresses_destroy (&m->peer,
267                          ntohl (m->session_id));
268   srm.header.type = ntohs (GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
269   srm.header.size = ntohs (sizeof (struct SessionReleaseMessage));
270   srm.session_id = m->session_id;
271   srm.peer = m->peer;
272   GNUNET_SERVER_notification_context_unicast (nc,
273                                               client,
274                                               &srm.header,
275                                               GNUNET_NO);
276   GNUNET_SERVER_receive_done (client, GNUNET_OK);
277 }
278
279
280 /**
281  * Initialize scheduling subsystem.
282  *
283  * @param server handle to our server
284  * @param ah the address handle to use
285  */
286 void
287 GAS_scheduling_init (struct GNUNET_SERVER_Handle *server)
288 {
289   nc = GNUNET_SERVER_notification_context_create (server, 128);
290 }
291
292
293 /**
294  * Shutdown scheduling subsystem.
295  */
296 void
297 GAS_scheduling_done ()
298 {
299   if (NULL != my_client)
300   {
301     my_client = NULL;
302   }
303   GNUNET_SERVER_notification_context_destroy (nc);
304   nc = NULL;
305 }
306
307
308 /* end of gnunet-service-ats_scheduling.c */