Added local mesh messages
[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  * TODO:
27  * - soft stateing (keep-alive (CHANGE?) / timeout / disconnect) -- not a message issue
28  * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
29  * - partial disconnect reporting -- same as error reporting?
30  * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
31  * - speed requirement specification (change?) in mesh API -- API call
32  */
33
34 #include <stdint.h>
35 #include "gnunet_common.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_core_service.h"
38 #include <netinet/in.h>
39
40
41 /******************************************************************************/
42 /********************      MESH NETWORK MESSAGES     **************************/
43 /******************************************************************************/
44
45 /**
46  * Message for mesh path management
47  */
48 struct GNUNET_MESH_ManipulatePath
49 {
50     /**
51      * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
52      *
53      * Size: sizeof(struct GNUNET_MESH_ManipulatePath) + path_length * sizeof (struct GNUNET_PeerIdentity)
54      */
55     struct GNUNET_MessageHeader header;
56
57     /**
58      * Id of the tunnel this path belongs to, unique in conjunction with the origin.
59      */
60     uint32_t tid GNUNET_PACKED;
61
62     /**
63      * Information about speed requirements.  If the tunnel cannot sustain the 
64      * minimum bandwidth, packets are to be dropped.
65      */
66     uint32_t speed_min GNUNET_PACKED;
67
68     /**
69      * path_length structs defining the *whole* path from the origin [0] to the
70      * final destination [path_length-1].
71      */
72   // struct GNUNET_PeerIdentity peers[path_length];
73 };
74
75 /**
76  * Message for mesh data traffic to all tunnel targets.
77  */
78 struct GNUNET_MESH_OriginMulticast
79 {
80     /**
81      * Type: GNUNET_MESSAGE_TYPE_DATA_MULTICAST
82      */
83     struct GNUNET_MessageHeader header;
84
85     /**
86      * TID of the tunnel
87      */
88     uint32_t tid GNUNET_PACKED;
89
90     /**
91      * OID of the tunnel
92      */
93     struct GNUNET_PeerIdentity oid;
94
95     /**
96      * FIXME: Some form of authentication
97      */
98     // uint32_t token;
99
100     /**
101      * Payload follows
102      */
103 };
104
105
106 /**
107  * Message for mesh data traffic to a particular destination from origin.
108  */
109 struct GNUNET_MESH_DataMessageFromOrigin
110 {
111     /**
112      * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN
113      */
114     struct GNUNET_MessageHeader header;
115
116     /**
117      * TID of the tunnel
118      */
119     uint32_t tid GNUNET_PACKED;
120
121     /**
122      * OID of the tunnel
123      */
124     struct GNUNET_PeerIdentity oid;
125
126     /**
127      * Destination.
128      */
129     struct GNUNET_PeerIdentity destination;
130
131     /**
132      * FIXME: Some form of authentication
133      */
134     // uint32_t token;
135
136     /**
137      * Payload follows
138      */
139 };
140
141
142 /**
143  * Message for mesh data traffic from a tunnel participant to origin.
144  */
145 struct GNUNET_MESH_DataMessageToOrigin
146 {
147     /**
148      * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN
149      */
150     struct GNUNET_MessageHeader header;
151
152     /**
153      * TID of the tunnel
154      */
155     uint32_t tid GNUNET_PACKED;
156
157     /**
158      * OID of the tunnel
159      */
160     struct GNUNET_PeerIdentity oid;
161
162     /**
163      * Sender of the message.
164      */
165     struct GNUNET_PeerIdentity sender;
166
167     /**
168      * FIXME: Some form of authentication
169      */
170     // uint32_t token;
171
172     /**
173      * Payload follows
174      */
175 };
176
177 /**
178  * Message for mesh flow control
179  */
180 struct GNUNET_MESH_SpeedNotify
181 {
182     /**
183      * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
184      */
185     struct GNUNET_MessageHeader header;
186
187     /**
188      * TID of the tunnel
189      */
190     uint32_t tid GNUNET_PACKED;
191
192     /**
193      * OID of the tunnel
194      */
195     struct GNUNET_PeerIdentity oid;
196
197     /**
198      * Slowest link down the path (above minimum speed requirement).
199      */
200     uint32_t speed_min;
201
202 };
203
204 /******************************************************************************/
205 /************************      DATA STRUCTURES     ****************************/
206 /******************************************************************************/
207
208 /**
209  * All the states a peer participating in a tunnel can be in.
210  */
211 enum PeerState
212 {
213     /**
214      * Request sent, not yet answered.
215      */
216     MESH_PEER_WAITING,
217
218     /**
219      * Peer connected and ready to accept data
220      */
221     MESH_PEER_READY,
222
223     /**
224      * Peer connected previosly but not responding
225      */
226     MESH_PEER_UNAVAILABLE,
227
228     /**
229      * Peer requested but not ever connected
230      */
231     MESH_PEER_UNREACHABLE
232 };
233
234 /**
235  * Struct containing all information regarding a given peer
236  */
237 struct PeerInfo
238 {
239     /**
240      * ID of the peer
241      */
242     struct GNUNET_PeerIdentity id;
243
244     /**
245      * Is the peer reachable? Is the peer even connected?
246      */
247     struct PeerState state;
248
249     /**
250      * Who to send the data to
251      */
252     uint32_t first_hop;
253
254     /**
255      * Max data rate to this peer
256      */
257     uint32_t max_speed;
258 };
259
260 /**
261  * Information regarding a path
262  */
263 struct Path
264 {
265     /**
266      * Id of the path, in case it's needed
267      */
268     uint32_t id;
269
270     /**
271      * Whether the path is serving traffic in a tunnel or is a backup
272      */
273     int in_use;
274
275     /**
276      * List of all the peers that form the path from origin to target
277      */
278     PeerInfo *peers;
279 };
280
281 /**
282  * Struct containing all information regarding a tunnel
283  * For an intermediate node the improtant info used will be:
284  * - OID        \ To identify
285  * - TID        / the tunnel
286  * - paths[0]   | To know where to send it next
287  * - metainfo: ready, speeds, accounting
288  * For an end node more fields will be needed (client-handling)
289  */
290 struct MESH_tunnel
291 {
292     /**
293      * Origin ID: Node that created the tunnel
294      */
295     struct GNUNET_PeerIdentity oid;
296
297     /**
298      * Tunnel number (unique for a given oid)
299      */
300     uint32_t tid;
301
302     /**
303      * Whether the tunnel is in state to transmit data
304      */
305     int ready;
306
307     /**
308      * Minimal speed for this tunnel in kb/s
309      */
310     uint32_t speed_min;
311
312     /**
313      * Maximal speed for this tunnel in kb/s
314      */
315     uint32_t speed_max;
316
317     /**
318      * Last time the tunnel was used
319      */
320     struct GNUNET_TIME_Absolute timestamp;
321
322     /**
323      * Peers in the tunnel, for future optimizations
324      */
325     struct PeerInfo *peers;
326
327     /**
328      * Paths (used and backup)
329      */
330     struct Path *paths;
331
332     /**
333      * Messages ready to transmit
334      */
335     struct GNUNET_MessageHeader *msg_out;
336
337     /**
338      * Messages received and not processed
339      */
340     struct GNUNET_MessageHeader *msg_in;
341
342     /**
343      * FIXME Clients. Is anyone to be notified for traffic here?
344      */
345 };
346
347 /**
348  * So, I'm an endpoint. Why am I receiveing traffic?
349  * Who is interested in this? How to communicate with them?
350  */
351 struct Clients
352 {
353     /**
354      * FIXME add structures needed to handle client connections
355      */
356     int fixme;
357 };
358
359 /**
360  * Handler for requests of creating new path
361  * type: struct GNUNET_CORE_MessageHandler
362  *
363  * @param cls closure
364  * @param client the client this message is from
365  * @param message the message received
366  */
367 static void
368 handle_mesh_path_create (void *cls,
369                          struct GNUNET_SERVER_Client *client,
370                          const struct GNUNET_MessageHeader *message)
371 {
372     return;
373 }
374
375 /**
376  * Handler for client disconnection
377  *
378  * @param cls closure
379  * @param client identification of the client; NULL
380  *        for the last call when the server is destroyed
381  */
382 static void
383 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
384 {
385     /* Remove client from list, delete all timers and queues associated */
386     return;
387 }
388
389 /**
390  * Core handler for path creation
391  * struct GNUNET_CORE_MessageHandler
392  *
393  * @param cls closure
394  * @param message message
395  * @param peer peer identity this notification is about
396  * @param atsi performance data
397  *
398  */
399 static int
400 handle_mesh_path_create (void *cls,
401                               const struct GNUNET_PeerIdentity *peer,
402                               const struct GNUNET_MessageHeader *message,
403                               const struct GNUNET_TRANSPORT_ATS_Information
404                               *atsi)
405 {
406     /* Extract path */
407     /* Find origin & self */
408     /* Search for origin in local tunnels */
409     /* Create tunnel / add path */
410     /* Retransmit to next link in chain, if any (core_notify + callback) */
411     return GNUNET_OK;
412 }
413
414 /**
415  * Core handler for mesh network traffic
416  *
417  * @param cls closure
418  * @param message message
419  * @param peer peer identity this notification is about
420  * @param atsi performance data
421  *
422  */
423 static int
424 handle_mesh_network_traffic (void *cls,
425                              const struct GNUNET_PeerIdentity *peer,
426                              const struct GNUNET_MessageHeader *message,
427                              const struct GNUNET_TRANSPORT_ATS_Information
428                              *atsi)
429 {
430     if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
431         /* Retransmit to next in path of tunnel identified by message */
432         return GNUNET_OK;
433     } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
434         /* Retransmit to previous in path of tunnel identified by message */
435         return GNUNET_OK;
436     }
437 }
438
439 /**
440  * Functions to handle messages from core
441  */
442 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
443   {&handle_mesh_path_create, NULL, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
444   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
445   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
446   {NULL, 0, 0}
447 };
448
449 /**
450  * Functions to handle messages from clients
451  */
452 /* MESSAGES DEFINED:
453 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT              272
454 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY     273
455 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL     274
456 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD     275
457 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL     276
458 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 277
459 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL  278
460 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY       279
461 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATED       280
462 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROYED     281
463 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA                 282
464 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST       283
465  */
466 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
467   {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_LOCAL_CONNECT, 0},
468   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY, 0},
469   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL, 0},
470   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
471   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
472   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE, sizeof(struct GNUNET_MESH_ConnectPeerByType)},
473   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL, sizeof(struct GNUNET_MESH_Control)},
474   {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_LOCAL_TRANSMIT_READY, sizeof(struct GNUNET_MESH_Control)},
475   {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_LOCAL_DATA, 0}, /* FIXME needed? */
476   {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_LOCAL_DATA_BROADCAST, 0}, /* FIXME needed? */
477   {NULL, NULL, 0, 0}
478 };
479
480
481 /**
482  * To be called on core init/fail.
483  *
484  * @param cls service closure
485  * @param server handle to the server for this service
486  * @param identity the public identity of this peer
487  * @param publicKey the public key of this peer
488  */
489 static void
490 core_init (void *cls,
491            struct GNUNET_CORE_Handle *server,
492            const struct GNUNET_PeerIdentity *identity,
493            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
494 {
495     return;
496 }
497
498 /**
499  * Method called whenever a given peer connects.
500  *
501  * @param cls closure
502  * @param peer peer identity this notification is about
503  * @param atsi performance data for the connection
504  */
505 static void
506 core_connect (void *cls,
507               const struct GNUNET_PeerIdentity *peer,
508               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
509 {
510     return;
511 }
512
513 /**
514  * Method called whenever a peer disconnects.
515  *
516  * @param cls closure
517  * @param peer peer identity this notification is about
518  */
519 static void
520 core_disconnect (void *cls,
521                 const struct
522                 GNUNET_PeerIdentity *peer)
523 {
524     return;
525 }
526
527 /**
528  * Process mesh requests. FIXME NON FUNCTIONAL, COPIED FROM DHT!!
529  *
530  * @param cls closure
531  * @param server the initialized server
532  * @param c configuration to use
533  */
534 static void
535 run (void *cls,
536      struct GNUNET_SERVER_Handle *server,
537      const struct GNUNET_CONFIGURATION_Handle *c)
538 {
539   struct GNUNET_TIME_Relative next_send_time;
540   unsigned long long temp_config_num;
541   char *converge_modifier_buf;
542   GNUNET_CORE_Handle *core;
543
544   GNUNET_SERVER_add_handlers (server, plugin_handlers);
545   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
546   core = GNUNET_CORE_connect (c,   /* Main configuration */
547                             32,       /* queue size */
548                             NULL,  /* Closure passed to MESH functions */
549                             &core_init,    /* Call core_init once connected */
550                             &core_connect,  /* Handle connects */
551                             &core_disconnect,       /* remove peers on disconnects */
552                             NULL,  /* Do we care about "status" updates? */
553                             NULL,  /* Don't want notified about all incoming messages */
554                             GNUNET_NO,     /* For header only inbound notification */
555                             NULL,  /* Don't want notified about all outbound messages */
556                             GNUNET_NO,     /* For header only outbound notification */
557                             core_handlers);        /* Register these handlers */
558
559   if (core == NULL)
560     return;
561 }
562
563 /**
564  * The main function for the mesh service.
565  *
566  * @param argc number of arguments from the command line
567  * @param argv command line arguments
568  * @return 0 ok, 1 on error
569  */
570 int
571 main (int argc, char *const *argv)
572 {
573   int ret;
574
575   ret = (GNUNET_OK ==
576          GNUNET_SERVICE_run (argc,
577                              argv,
578                              "mesh",
579                              GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
580   return ret;
581 }