2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - MESH NETWORK MESSAGES
30 * - MESH NETWORK HANDLES
31 * - MESH LOCAL HANDLES
32 * - MAIN FUNCTIONS (main & run)
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
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"
51 /******************************************************************************/
52 /******************** MESH NETWORK MESSAGES **************************/
53 /******************************************************************************/
56 * Message for mesh path management
58 struct GNUNET_MESH_ManipulatePath
61 * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
63 * Size: sizeof(struct GNUNET_MESH_ManipulatePath) +
64 * path_length * sizeof (struct GNUNET_PeerIdentity)
66 struct GNUNET_MessageHeader header;
69 * Global id of the tunnel this path belongs to,
70 * unique in conjunction with the origin.
72 uint32_t tid GNUNET_PACKED;
75 * Information about speed requirements. If the tunnel cannot sustain the
76 * minimum bandwidth, packets are to be dropped.
78 uint32_t speed_min GNUNET_PACKED;
83 uint32_t reserved GNUNET_PACKED;
86 * path_length structs defining the *whole* path from the origin [0] to the
87 * final destination [path_length-1].
89 /* struct GNUNET_PeerIdentity peers[path_length]; */
93 * Message for mesh data traffic to all tunnel targets.
95 struct GNUNET_MESH_OriginMulticast
98 * Type: GNUNET_MESSAGE_TYPE_DATA_MULTICAST
100 struct GNUNET_MessageHeader header;
105 uint32_t tid GNUNET_PACKED;
110 struct GNUNET_PeerIdentity oid;
119 * Message for mesh data traffic to a particular destination from origin.
121 struct GNUNET_MESH_DataMessageFromOrigin
124 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN
126 struct GNUNET_MessageHeader header;
131 uint32_t tid GNUNET_PACKED;
136 struct GNUNET_PeerIdentity oid;
141 struct GNUNET_PeerIdentity destination;
150 * Message for mesh data traffic from a tunnel participant to origin.
152 struct GNUNET_MESH_DataMessageToOrigin
155 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN
157 struct GNUNET_MessageHeader header;
162 uint32_t tid GNUNET_PACKED;
167 struct GNUNET_PeerIdentity oid;
170 * Sender of the message.
172 struct GNUNET_PeerIdentity sender;
180 * Message for mesh flow control
182 struct GNUNET_MESH_SpeedNotify
185 * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
187 struct GNUNET_MessageHeader header;
192 uint32_t tid GNUNET_PACKED;
197 struct GNUNET_PeerIdentity oid;
200 * Slowest link down the path (above minimum speed requirement).
206 /******************************************************************************/
207 /************************ DATA STRUCTURES ****************************/
208 /******************************************************************************/
211 * All the states a peer participating in a tunnel can be in.
216 * Request sent, not yet answered.
221 * Peer connected and ready to accept data
226 * Peer connected previosly but not responding
228 MESH_PEER_RECONNECTING,
233 * Struct containing all information regarding a given peer
243 * Is the peer reachable? Is the peer even connected?
245 enum PeerState state;
248 * Who to send the data to --- FIXME what about multiple (alternate) paths?
250 GNUNET_PEER_Id first_hop;
253 * Max data rate to this peer
259 typedef uint32_t MESH_PathID;
261 * Information regarding a path
271 * Id of the path, in case it's needed
276 * Whether the path is serving traffic in a tunnel or is a backup
281 * List of all the peers that form the path from origin to target
283 GNUNET_PEER_Id *peers;
287 struct Client; /* FWD declaration */
289 * Struct containing all information regarding a tunnel
290 * For an intermediate node the improtant info used will be:
291 * - OID \ To identify
293 * - paths[0] | To know where to send it next
294 * - metainfo: ready, speeds, accounting
295 * For an end node more fields will be needed (client-handling)
303 struct MESH_tunnel *next;
304 struct MESH_tunnel *prev;
307 * Origin ID: Node that created the tunnel
312 * Tunnel number (unique for a given oid)
317 * Minimal speed for this tunnel in kb/s
322 * Maximal speed for this tunnel in kb/s
327 * Last time the tunnel was used
329 struct GNUNET_TIME_Absolute timestamp;
332 * Peers in the tunnel, for future optimizations
334 struct PeerInfo *peers_head;
335 struct PeerInfo *peers_tail;
338 * Number of peers that are connected and potentially ready to receive data
340 unsigned int peers_ready;
343 * Number of peers that have been added to the tunnel
345 unsigned int peers_total;
348 * Paths (used and backup)
350 struct Path *paths_head;
351 struct Path *paths_tail;
354 * If this tunnel was created by a local client, what's its handle?
356 struct Client *client;
359 * Messages ready to transmit??? -- FIXME real queues needed
361 struct GNUNET_MessageHeader *msg_out;
364 * Messages received and not processed??? -- FIXME real queues needed
366 struct GNUNET_MessageHeader *msg_in;
371 * Struct containing information about a client of the service
382 * Tunnels that belong to this client, for convenience on disconnect
384 struct MESH_tunnel *tunnels_head;
385 struct MESH_tunnel *tunnels_tail;
388 * Handle to communicate with the client
390 struct GNUNET_SERVER_Client *handle;
393 * Messages that this client has declared interest in
395 GNUNET_MESH_ApplicationType *messages_subscribed;
396 unsigned int subscription_counter;
400 /******************************************************************************/
401 /*********************** GLOBAL VARIABLES ****************************/
402 /******************************************************************************/
407 static struct Client *clients_head;
408 static struct Client *clients_tail;
413 static struct MESH_tunnel *tunnels_head;
414 static struct MESH_tunnel *tunnels_tail;
417 * All the paths (for future path optimization)
419 // static struct Path *paths_head;
420 // static struct Path *paths_tail;
422 /******************************************************************************/
423 /******************** MESH NETWORK HANDLERS **************************/
424 /******************************************************************************/
427 * Core handler for path creation
428 * struct GNUNET_CORE_MessageHandler
431 * @param message message
432 * @param peer peer identity this notification is about
433 * @param atsi performance data
434 * @return GNUNET_OK to keep the connection open,
435 * GNUNET_SYSERR to close it (signal serious error)
439 handle_mesh_path_create (void *cls,
440 const struct GNUNET_PeerIdentity *peer,
441 const struct GNUNET_MessageHeader *message,
442 const struct GNUNET_TRANSPORT_ATS_Information
446 * EXAMPLE OF USING THE API
447 * NOT ACTUAL CODE!!!!!
453 GNUNET_CONTAINER_DLL_insert (c->my_tunnels_head,
457 while (NULL != (t = c->my_tunnels_head))
459 GNUNET_CONTAINER_DLL_remove (c->my_tunnels_head,
468 /* Find origin & self */
469 /* Search for origin in local tunnels */
470 /* Create tunnel / add path */
471 /* Retransmit to next link in chain, if any (core_notify + callback) */
476 * Core handler for mesh network traffic
479 * @param message message
480 * @param peer peer identity this notification is about
481 * @param atsi performance data
482 * @return GNUNET_OK to keep the connection open,
483 * GNUNET_SYSERR to close it (signal serious error)
486 handle_mesh_network_traffic (void *cls,
487 const struct GNUNET_PeerIdentity *peer,
488 const struct GNUNET_MessageHeader *message,
489 const struct GNUNET_TRANSPORT_ATS_Information
492 if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
493 /* Retransmit to next in path of tunnel identified by message */
495 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
496 /* Retransmit to previous in path of tunnel identified by message */
502 * Functions to handle messages from core
504 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
505 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
506 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
507 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
513 /******************************************************************************/
514 /********************* MESH LOCAL HANDLES **************************/
515 /******************************************************************************/
518 * Handler for client disconnection
521 * @param client identification of the client; NULL
522 * for the last call when the server is destroyed
525 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
527 struct Client *c, *next;
528 struct MESH_tunnel *t;
530 /* If there are no clients registered, something is wrong... or is it?
531 * FIXME: what happens if a client connects, doesn't send a MESH_Connect
532 * and disconnects? Does the service get a disconnect notification anyway?
534 GNUNET_assert(NULL != clients_head);
535 for (c = clients_head; c != clients_head; c = next) {
536 if (c->handle == client) {
537 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
538 while (NULL != (t = c->tunnels_head)) {
539 GNUNET_CONTAINER_DLL_remove (c->tunnels_head, c->tunnels_tail, t);
540 GNUNET_CONTAINER_DLL_remove (tunnels_head, tunnels_tail, t);
541 /* TODO free paths and other tunnel dynamic structures */
544 GNUNET_free (c->messages_subscribed);
556 * Handler for new clients
559 * @param client identification of the client
560 * @param message the actual message, which includes messages the client wants
563 handle_local_new_client (void *cls,
564 struct GNUNET_SERVER_Client *client,
565 const struct GNUNET_MessageHeader *message)
568 unsigned int payload_size;
570 /* Check data sanity */
571 payload_size = message->size - sizeof(struct GNUNET_MessageHeader);
572 if (0 != payload_size % sizeof(GNUNET_MESH_ApplicationType)) {
574 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
578 /* Create new client structure */
579 c = GNUNET_malloc(sizeof(struct Client));
581 c->tunnels_head = NULL;
582 c->tunnels_tail = NULL;
583 if(payload_size != 0) {
584 c->messages_subscribed = GNUNET_malloc(payload_size);
585 memcpy(c->messages_subscribed, &message[1], payload_size);
587 c->messages_subscribed = NULL;
589 c->subscription_counter = payload_size/sizeof(GNUNET_MESH_ApplicationType);
591 /* Insert new client in DLL */
592 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
594 GNUNET_SERVER_receive_done(client, GNUNET_OK);
598 * Handler for requests of new tunnels
601 * @param client identification of the client
602 * @param message the actual message
605 handle_local_tunnel_create (void *cls,
606 struct GNUNET_SERVER_Client *client,
607 const struct GNUNET_MessageHeader *message)
610 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
611 struct MESH_tunnel *t;
613 /* Sanity check for client registration */
614 /* TODO: refactor into new function */
615 for (c = clients_head; c != clients_head; c = c->next) {
616 if(c->handle == client) break;
618 if(c->handle != client) { /* Client hasn't registered, not a good thing */
620 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
624 /* Message sanity check */
625 if(sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
627 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
631 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
632 /* Sanity check for tunnel numbering */
633 if(0 == (ntohl(tunnel_msg->tunnel_id) & 0x80000000)) {
635 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
638 /* Sanity check for duplicate tunnel IDs */
639 for (t = tunnels_head; t != tunnels_head; t = t->next) {
640 /* TODO - maybe this is not enough, need to consider the whole
641 * local/global numbering system, but probably it's ok (WiP)
643 if(t->tid == tunnel_msg->tunnel_id) {
645 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
649 //tunnel_msg->tunnel_id;
656 * Handler for requests of deleting tunnels
659 * @param client identification of the client
660 * @param message the actual message
663 handle_local_tunnel_destroy (void *cls,
664 struct GNUNET_SERVER_Client *client,
665 const struct GNUNET_MessageHeader *message)
671 * Handler for connection requests to new peers
674 * @param client identification of the client
675 * @param message the actual message
678 handle_local_connect (void *cls,
679 struct GNUNET_SERVER_Client *client,
680 const struct GNUNET_MessageHeader *message)
686 * Handler for client traffic
689 * @param client identification of the client
690 * @param message the actual message
693 handle_local_network_traffic (void *cls,
694 struct GNUNET_SERVER_Client *client,
695 const struct GNUNET_MessageHeader *message)
701 * Functions to handle messages from clients
703 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
704 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
705 {&handle_local_tunnel_create, NULL,
706 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE, 0},
707 {&handle_local_tunnel_destroy, NULL,
708 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY, 0},
709 {&handle_local_connect, NULL,
710 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
711 {&handle_local_connect, NULL,
712 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
713 {&handle_local_connect, NULL,
714 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
715 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
716 {&handle_local_connect, NULL,
717 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL, 0},
718 {&handle_local_network_traffic, NULL,
719 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, /* FIXME needed? */
720 {&handle_local_network_traffic, NULL,
721 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0}, /* FIXME needed? */
727 * To be called on core init/fail.
729 * @param cls service closure
730 * @param server handle to the server for this service
731 * @param identity the public identity of this peer
732 * @param publicKey the public key of this peer
735 core_init (void *cls,
736 struct GNUNET_CORE_Handle *server,
737 const struct GNUNET_PeerIdentity *identity,
738 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
744 * Method called whenever a given peer connects.
747 * @param peer peer identity this notification is about
748 * @param atsi performance data for the connection
751 core_connect (void *cls,
752 const struct GNUNET_PeerIdentity *peer,
753 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
759 * Method called whenever a peer disconnects.
762 * @param peer peer identity this notification is about
765 core_disconnect (void *cls,
767 GNUNET_PeerIdentity *peer)
772 /******************************************************************************/
773 /************************ MAIN FUNCTIONS ****************************/
774 /******************************************************************************/
777 * Process mesh requests. FIXME NON FUNCTIONAL, SKELETON
780 * @param server the initialized server
781 * @param c configuration to use
785 struct GNUNET_SERVER_Handle *server,
786 const struct GNUNET_CONFIGURATION_Handle *c)
788 struct GNUNET_CORE_Handle *core;
790 GNUNET_SERVER_add_handlers (server, plugin_handlers);
791 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
792 core = GNUNET_CORE_connect (c, /* Main configuration */
794 NULL, /* Closure passed to MESH functions */
795 &core_init, /* Call core_init once connected */
796 &core_connect, /* Handle connects */
797 &core_disconnect, /* remove peers on disconnects */
798 NULL, /* Do we care about "status" updates? */
799 NULL, /* Don't notify about all incoming messages */
800 GNUNET_NO, /* For header only in notification */
801 NULL, /* Don't notify about all outbound messages */
802 GNUNET_NO, /* For header-only out notification */
803 core_handlers); /* Register these handlers */
810 * The main function for the mesh service.
812 * @param argc number of arguments from the command line
813 * @param argv command line arguments
814 * @return 0 ok, 1 on error
817 main (int argc, char *const *argv)
822 GNUNET_SERVICE_run (argc,
825 GNUNET_SERVICE_OPTION_NONE,
826 &run, NULL)) ? 0 : 1;