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