bd87cecaf9ee5482be937831977279f5e5a1f4a5
[oweals/gnunet.git] / src / ats / gnunet-service-ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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.c
23  * @brief ats service
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_ats_service.h"
31 #include "ats.h"
32
33 struct ATS_Clients
34 {
35   struct ATS_Clients * next;
36
37   struct ATS_Clients * prev;
38
39   struct GNUNET_SERVER_Client *client;
40
41   uint32_t flags;
42 };
43
44 struct ATS_Address
45 {
46   struct GNUNET_PeerIdentity peer;
47
48   size_t addr_len;
49
50   uint32_t session_id;
51
52   uint32_t ats_count;
53
54   void * addr;
55
56   char * plugin;
57
58   struct GNUNET_TRANSPORT_ATS_Information * ats;
59 };
60
61 /**
62  * Head of linked list of all clients to this service.
63  */
64 static struct ATS_Clients *ac_head;
65
66 /**
67  * Tail of linked list of all clients to this service.
68  */
69 static struct ATS_Clients *ac_tail;
70
71 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
72
73 int address_it (void *cls,
74                const GNUNET_HashCode * key,
75                void *value)
76 {
77   struct ATS_Address * aa = cls;
78   GNUNET_free (aa);
79   return GNUNET_OK;
80 }
81
82 /**
83  * Task run during shutdown.
84  *
85  * @param cls unused
86  * @param tc unused
87  */
88 static void
89 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91   struct ATS_Clients * t;
92
93   while (ac_head != NULL)
94   {
95     t = ac_head;
96     GNUNET_CONTAINER_DLL_remove(ac_head,ac_tail, t);
97     GNUNET_free (t);
98   }
99
100   GNUNET_CONTAINER_multihashmap_iterate (addresses, address_it, NULL);
101
102   GNUNET_CONTAINER_multihashmap_destroy (addresses);
103 }
104
105 static struct ATS_Clients * find_client (struct GNUNET_SERVER_Client *client)
106 {
107   struct ATS_Clients * ac = ac_head;
108   while (ac != NULL)
109   {
110   if (ac->client == client)
111     break;
112   ac = ac->next;
113   }
114   return ac;
115 }
116
117 static void
118 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
119                       const struct GNUNET_MessageHeader *message)
120
121 {
122   struct ClientStartMessage * msg = (struct ClientStartMessage *) msg;
123   struct ATS_Clients * ac = NULL;
124
125
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
127
128   GNUNET_assert (find_client(client) == NULL);
129
130   ac = GNUNET_malloc (sizeof (struct ATS_Clients));
131   ac->client = client;
132   ac->flags = ntohl (msg->start_flag);
133
134   GNUNET_CONTAINER_DLL_insert(ac_head, ac_tail, ac);
135 }
136
137 static void
138 handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
139                       const struct GNUNET_MessageHeader *message)
140
141 {
142   struct RequestAddressMessage * msg = (struct RequestAddressMessage *) msg;
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
144
145 }
146
147 static void
148 handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
149                       const struct GNUNET_MessageHeader *message)
150
151 {
152   struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) msg;
153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_UPDATE");
154
155   struct GNUNET_TRANSPORT_ATS_Information *am;
156   char *pm;
157
158   size_t size = ntohs (msg->header.size);
159   if (size <= sizeof (struct AddressUpdateMessage))
160       GNUNET_break (0);
161
162   size_t ats_count = ntohs (msg->ats_count);
163   size_t addr_len = ntohs (msg->address_length);
164   size_t plugin_len = ntohs (msg->plugin_name_length) + 1 ;
165
166   struct ATS_Address * aa = GNUNET_malloc (sizeof (struct ATS_Address) +
167                                            ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
168                                            addr_len +
169                                            plugin_len);
170
171
172
173   memcpy (&aa->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
174   aa->addr_len = addr_len;
175   aa->ats_count = ats_count;
176   aa->ats = (struct GNUNET_TRANSPORT_ATS_Information *) &aa[1];
177
178   am = (struct GNUNET_TRANSPORT_ATS_Information*) &msg[1];
179   memcpy (&aa->ats, am, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
180   pm = (char *) &am[ats_count];
181   memcpy (aa->addr, pm, addr_len);
182   memcpy (aa->plugin, &pm[plugin_len], plugin_len);
183   aa->session_id = ntohl(msg->session_id);
184
185   GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(addresses, &aa->peer.hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
186 }
187
188 static void
189 handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
190                       const struct GNUNET_MessageHeader *message)
191
192 {
193   struct AddressDestroyedMessage * msg = (struct AddressDestroyedMessage *) msg;
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_DESTROYED");
195
196 }
197
198 static void
199 handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
200                       const struct GNUNET_MessageHeader *message)
201
202 {
203   struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) msg;
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
205 }
206
207 static void
208 handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
209                       const struct GNUNET_MessageHeader *message)
210
211 {
212   struct ChangePreferenceMessage * msg = (struct ChangePreferenceMessage *) msg;
213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
214 }
215
216 /**
217  * Process template requests.
218  *
219  * @param cls closure
220  * @param server the initialized server
221  * @param cfg configuration to use
222  */
223 static void
224 run (void *cls, struct GNUNET_SERVER_Handle *server,
225      const struct GNUNET_CONFIGURATION_Handle *cfg)
226 {
227   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
228       {&handle_ats_start, NULL, GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
229       {&handle_request_address, NULL, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
230       {&handle_address_update, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
231       {&handle_address_destroyed, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
232       {&handle_reservation_request, NULL, GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
233       {&handle_preference_change, NULL, GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
234     {NULL, NULL, 0, 0}
235   };
236
237   addresses = GNUNET_CONTAINER_multihashmap_create(100);
238
239   GNUNET_SERVER_add_handlers (server, handlers);
240   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
241                                 NULL);
242 }
243
244
245 /**
246  * The main function for the ats service.
247  *
248  * @param argc number of arguments from the command line
249  * @param argv command line arguments
250  * @return 0 ok, 1 on error
251  */
252 int
253 main (int argc, char *const *argv)
254 {
255   return (GNUNET_OK ==
256           GNUNET_SERVICE_run (argc, argv, "ats",
257                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
258 }
259
260 /* end of gnunet-service-ats.c */