add cork option to core api:
[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 mesh network traffic
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_network_traffic (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     if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
230         /* Retransmit to next in path of tunnel identified by message */
231         return 0;
232     } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
233         /* Retransmit to previous in path of tunnel identified by message */
234         return 0;
235     }
236 }
237
238 /**
239  * Functions to handle messages from core
240  */
241 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
242   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
243   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
244   {NULL, 0, 0}
245 };
246
247 /**
248  * Functions to handle messages from clients
249  */
250 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
251   {&handle_mesh_path_create, NULL, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
252   {NULL, NULL, 0, 0}
253 };
254
255 /**
256  * Process mesh requests. FIXME NON FUNCTIONAL, COPIED FROM DHT!!
257  *
258  * @param cls closure
259  * @param server the initialized server
260  * @param c configuration to use
261  */
262 static void
263 run (void *cls,
264      struct GNUNET_SERVER_Handle *server,
265      const struct GNUNET_CONFIGURATION_Handle *c)
266 {
267   struct GNUNET_TIME_Relative next_send_time;
268   unsigned long long temp_config_num;
269   char *converge_modifier_buf;
270   GNUNET_CORE_Handle *coreAPI;
271
272   GNUNET_SERVER_add_handlers (server, plugin_handlers);
273   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
274   coreAPI = GNUNET_CORE_connect (c,   /* Main configuration */
275                                  32,       /* queue size */
276                                  NULL,  /* Closure passed to DHT functions */
277                                  NULL,    /* Call core_init once connected */
278                                  NULL,  /* Handle connects */
279                                  NULL,       /* remove peers on disconnects */
280                                  NULL,  /* Do we care about "status" updates? */
281                                  NULL,  /* Don't want notified about all incoming messages */
282                                  GNUNET_NO,     /* For header only inbound notification */
283                                  NULL,  /* Don't want notified about all outbound messages */
284                                  GNUNET_NO,     /* For header only outbound notification */
285                                  core_handlers);        /* Register these handlers */
286
287   if (coreAPI == NULL)
288     return;
289 }
290
291 /**
292  * The main function for the mesh service.
293  *
294  * @param argc number of arguments from the command line
295  * @param argv command line arguments
296  * @return 0 ok, 1 on error
297  */
298 int
299 main (int argc, char *const *argv)
300 {
301   int ret;
302
303   ret = (GNUNET_OK ==
304          GNUNET_SERVICE_run (argc,
305                              argv,
306                              "mesh",
307                              GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
308   return ret;
309 }