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