Resolved some weird conflicts
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001 - 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 mesh/gnunet-service-mesh.c
23  * @brief GNUnet MESH service
24  * @author Bartlomiej Polot
25  */
26
27 #include <stdint.h>
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include <netinet/in.h>
32
33 /**
34  * All the states a peer participating in a tunnel can be in.
35  */
36 enum PeerState
37 {
38     /**
39      * Request sent, not yet answered.
40      */
41     MESH_PEER_WAITING,
42
43     /**
44      * Peer connected and ready to accept data
45      */
46     MESH_PEER_READY,
47
48     /**
49      * Peer connected previosly but not responding
50      */
51     MESH_PEER_UNAVAILABLE,
52
53     /**
54      * Peer requested but not ever connected
55      */
56     MESH_PEER_UNREACHABLE
57 };
58
59 /**
60  * Struct containing all information regarding a given peer
61  */
62 struct PeerInfo
63 {
64     /**
65      * ID of the peer
66      */
67     struct GNUNET_PeerIdentity id;
68
69     /**
70      * Is the peer reachable? Is the peer even connected?
71      */
72     struct PeerState state;
73
74     /**
75      * Who to send the data to
76      */
77     uint32_t first_hop;
78
79     /**
80      * Max data rate to this peer
81      */
82     uint32_t max_speed;
83 };
84
85 /**
86  * Information regarding a path
87  */
88 struct Path
89 {
90     /**
91      * Id of the path, in case it's needed
92      */
93     uint32_t id;
94
95     /**
96      * Whether the path is serving traffic in a tunnel or is a backup
97      */
98     int in_use;
99
100     /**
101      * List of all the peers that form the path from origin to target
102      */
103     PeerInfo *peers;
104 };
105
106 /**
107  * Struct containing all information regarding a tunnel
108  * For an intermediate node the improtant info used will be:
109  * - OID        \ To identify
110  * - TID        / the tunnel
111  * - paths[0]   | To know where to send it next
112  * - metainfo: ready, speeds, accounting
113  * For an end node more fields will be needed (client-handling)
114  */
115 struct MESH_tunnel
116 {
117     /**
118      * Origin ID: Node that created the tunnel
119      */
120     struct GNUNET_PeerIdentity oid;
121
122     /**
123      * Tunnel number (unique for a given oid)
124      */
125     uint32_t tid;
126
127     /**
128      * Whether the tunnel is in state to transmit data
129      */
130     int ready;
131
132     /**
133      * Minimal speed for this tunnel in kb/s
134      */
135     uint32_t speed_min;
136
137     /**
138      * Maximal speed for this tunnel in kb/s
139      */
140     uint32_t speed_max;
141
142     /**
143      * Last time the tunnel was used
144      */
145     struct GNUNET_TIME_Absolute timestamp;
146
147     /**
148      * Peers in the tunnel, for future optimizations
149      */
150     struct PeerInfo *peers;
151
152     /**
153      * Paths (used and backup)
154      */
155     struct Path *paths;
156
157     /**
158      * Messages ready to transmit
159      */
160     struct GNUNET_MessageHeader *msg_out;
161
162     /**
163      * Messages received and not processed
164      */
165     struct GNUNET_MessageHeader *msg_in;
166
167     /**
168      * FIXME Clients. Is anyone to be notified for traffic here?
169      */
170 };
171
172 /**
173  * So, I'm an endpoint. Why am I receiveing traffic?
174  * Who is interested in this? How to communicate with them?
175  */
176 struct Clients
177 {
178     /**
179      * FIXME add structures needed to handle client connections
180      */
181     int fixme;
182 };
183
184 /**
185  * Handler for requests of creating new path
186  *
187  * @param cls closure
188  * @param client the client this message is from
189  * @param message the message received
190  */
191 static void
192 handle_mesh_path_create (void *cls,
193                          struct GNUNET_SERVER_Client *client,
194                          const struct GNUNET_MessageHeader *message)
195 {
196     return;
197 }
198
199 /**
200  * Handler for client disconnection
201  *
202  * @param cls closure
203  * @param client identification of the client; NULL
204  *        for the last call when the server is destroyed
205  */
206 static void
207 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
208 {
209     /* Remove client from list, delete all timers and queues associated */
210     return;
211 }
212
213 /**
214  * Core handler for path creation
215  *
216  * @param cls closure
217  * @param message message
218  * @param peer peer identity this notification is about
219  * @param atsi performance data
220  *
221  */
222 static int
223 handle_mesh_path_create (void *cls,
224                               const struct GNUNET_PeerIdentity *peer,
225                               const struct GNUNET_MessageHeader *message,
226                               const struct GNUNET_TRANSPORT_ATS_Information
227                               *atsi)
228 {
229     /* Extract path */
230     return GNUNET_OK;
231 }
232
233 /**
234  * Core handler for mesh network traffic
235  *
236  * @param cls closure
237  * @param message message
238  * @param peer peer identity this notification is about
239  * @param atsi performance data
240  *
241  */
242 static int
243 handle_mesh_network_traffic (void *cls,
244                               const struct GNUNET_PeerIdentity *peer,
245                               const struct GNUNET_MessageHeader *message,
246                               const struct GNUNET_TRANSPORT_ATS_Information
247                               *atsi)
248 {
249     if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
250         /* Retransmit to next in path of tunnel identified by message */
251         return GNUNET_OK;
252     } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
253         /* Retransmit to previous in path of tunnel identified by message */
254         return GNUNET_OK;
255     }
256 }
257
258 /**
259  * Functions to handle messages from core
260  */
261 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
262   {&handle_mesh_path_create, NULL, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
263   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
264   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
265   {NULL, 0, 0}
266 };
267
268 /**
269  * Functions to handle messages from clients
270  */
271 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
272   {&handle_local_path_create, NULL, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
273   {&handle_local_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
274   {&handle_local_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
275   {NULL, NULL, 0, 0}
276 };
277
278 /**
279  * Process mesh requests. FIXME NON FUNCTIONAL, COPIED FROM DHT!!
280  *
281  * @param cls closure
282  * @param server the initialized server
283  * @param c configuration to use
284  */
285 static void
286 run (void *cls,
287      struct GNUNET_SERVER_Handle *server,
288      const struct GNUNET_CONFIGURATION_Handle *c)
289 {
290   struct GNUNET_TIME_Relative next_send_time;
291   unsigned long long temp_config_num;
292   char *converge_modifier_buf;
293   GNUNET_CORE_Handle *coreAPI;
294
295   GNUNET_SERVER_add_handlers (server, plugin_handlers);
296   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
297   coreAPI = GNUNET_CORE_connect (c,   /* Main configuration */
298                                  32,       /* queue size */
299                                  NULL,  /* Closure passed to DHT functions */
300                                  NULL,    /* Call core_init once connected */
301                                  NULL,  /* Handle connects */
302                                  NULL,       /* remove peers on disconnects */
303                                  NULL,  /* Do we care about "status" updates? */
304                                  NULL,  /* Don't want notified about all incoming messages */
305                                  GNUNET_NO,     /* For header only inbound notification */
306                                  NULL,  /* Don't want notified about all outbound messages */
307                                  GNUNET_NO,     /* For header only outbound notification */
308                                  core_handlers);        /* Register these handlers */
309
310   if (coreAPI == NULL)
311     return;
312 }
313
314 /**
315  * The main function for the mesh service.
316  *
317  * @param argc number of arguments from the command line
318  * @param argv command line arguments
319  * @return 0 ok, 1 on error
320  */
321 int
322 main (int argc, char *const *argv)
323 {
324   int ret;
325
326   ret = (GNUNET_OK ==
327          GNUNET_SERVICE_run (argc,
328                              argv,
329                              "mesh",
330                              GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
331   return ret;
332 }