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 --- 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
266 * Id of the path, in case it's needed
271 * Whether the path is serving traffic in a tunnel or is a backup
276 * List of all the peers that form the path from origin to target
278 GNUNET_PEER_Id *peers;
283 * Struct containing all information regarding a tunnel
284 * For an intermediate node the improtant info used will be:
285 * - OID \ To identify
287 * - paths[0] | To know where to send it next
288 * - metainfo: ready, speeds, accounting
289 * For an end node more fields will be needed (client-handling)
297 struct MESH_tunnel *next;
298 struct MESH_tunnel *prev;
301 * Origin ID: Node that created the tunnel
306 * Tunnel number (unique for a given oid)
311 * Whether the tunnel is in a state to transmit data
316 * Minimal speed for this tunnel in kb/s
321 * Maximal speed for this tunnel in kb/s
326 * Last time the tunnel was used
328 struct GNUNET_TIME_Absolute timestamp;
331 * Peers in the tunnel, for future optimizations
333 struct PeerInfo *peers;
336 * Paths (used and backup)
341 * Messages ready to transmit??? -- FIXME real queues needed
343 struct GNUNET_MessageHeader *msg_out;
346 * Messages received and not processed??? -- FIXME real queues needed
348 struct GNUNET_MessageHeader *msg_in;
351 * If this tunnel was created by a local client, what's its handle?
353 struct GNUNET_SERVER_Client *initiator;
357 * So, I'm an endpoint. Why am I receiveing traffic?
358 * Who is interested in this? How to communicate with them?
369 * Tunnels that belong to this client
371 struct MESH_tunnel *my_tunnels_head;
372 struct MESH_tunnel *my_tunnels_tail;
375 * If this tunnel was created by a local client, what's its handle?
377 struct GNUNET_SERVER_Client *handle;
380 * Messages that this client has declared interest in
382 uint16_t *messages_subscribed;
383 unsigned int messages_subscribed_counter;
387 /******************************************************************************/
388 /*********************** GLOBAL VARIABLES ****************************/
389 /******************************************************************************/
394 // static struct Client clients_head;
395 // static struct Client clients_tail;
400 // static struct MESH_tunnel *tunnel_participation_head;
401 // static struct MESH_tunnel *tunnel_participation_tail;
404 /******************************************************************************/
405 /******************** MESH NETWORK HANDLERS **************************/
406 /******************************************************************************/
409 * Core handler for path creation
410 * struct GNUNET_CORE_MessageHandler
413 * @param message message
414 * @param peer peer identity this notification is about
415 * @param atsi performance data
416 * @return GNUNET_OK to keep the connection open,
417 * GNUNET_SYSERR to close it (signal serious error)
421 handle_mesh_path_create (void *cls,
422 const struct GNUNET_PeerIdentity *peer,
423 const struct GNUNET_MessageHeader *message,
424 const struct GNUNET_TRANSPORT_ATS_Information
428 * EXAMPLE OF USING THE API
429 * NOT ACTUAL CODE!!!!!
435 GNUNET_CONTAINER_DLL_insert (c->my_tunnels_head,
439 while (NULL != (t = c->my_tunnels_head))
441 GNUNET_CONTAINER_DLL_remove (c->my_tunnels_head,
450 /* Find origin & self */
451 /* Search for origin in local tunnels */
452 /* Create tunnel / add path */
453 /* Retransmit to next link in chain, if any (core_notify + callback) */
458 * Core handler for mesh network traffic
461 * @param message message
462 * @param peer peer identity this notification is about
463 * @param atsi performance data
464 * @return GNUNET_OK to keep the connection open,
465 * GNUNET_SYSERR to close it (signal serious error)
468 handle_mesh_network_traffic (void *cls,
469 const struct GNUNET_PeerIdentity *peer,
470 const struct GNUNET_MessageHeader *message,
471 const struct GNUNET_TRANSPORT_ATS_Information
474 if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
475 /* Retransmit to next in path of tunnel identified by message */
477 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
478 /* Retransmit to previous in path of tunnel identified by message */
484 * Functions to handle messages from core
486 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
487 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
488 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
489 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
495 /******************************************************************************/
496 /********************* MESH LOCAL HANDLES **************************/
497 /******************************************************************************/
500 * Handler for client disconnection
503 * @param client identification of the client; NULL
504 * for the last call when the server is destroyed
507 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
509 /* Remove client from list, delete all timers and queues associated */
514 * Handler for new clients
517 * @param client identification of the client
518 * @param message the actual message, which includes messages the client wants
521 handle_local_new_client (void *cls,
522 struct GNUNET_SERVER_Client *client,
523 const struct GNUNET_MessageHeader *message)
529 * Handler for connection requests
532 * @param client identification of the client
533 * @param message the actual message
536 handle_local_connect (void *cls,
537 struct GNUNET_SERVER_Client *client,
538 const struct GNUNET_MessageHeader *message)
544 * Handler for client traffic
547 * @param client identification of the client
548 * @param message the actual message
551 handle_local_network_traffic (void *cls,
552 struct GNUNET_SERVER_Client *client,
553 const struct GNUNET_MessageHeader *message)
559 * Functions to handle messages from clients
562 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT 272
563 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY 273
564 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL 274
565 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD 275
566 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL 276
567 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 277
568 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL 278
569 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY 279
570 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATED 280
571 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROYED 281
572 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA 282
573 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST 283
575 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
576 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
577 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
578 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
579 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE, sizeof(struct GNUNET_MESH_ConnectPeerByType)},
580 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL, 0},
581 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY, 0},
582 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, /* FIXME needed? */
583 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0}, /* FIXME needed? */
589 * To be called on core init/fail.
591 * @param cls service closure
592 * @param server handle to the server for this service
593 * @param identity the public identity of this peer
594 * @param publicKey the public key of this peer
597 core_init (void *cls,
598 struct GNUNET_CORE_Handle *server,
599 const struct GNUNET_PeerIdentity *identity,
600 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
606 * Method called whenever a given peer connects.
609 * @param peer peer identity this notification is about
610 * @param atsi performance data for the connection
613 core_connect (void *cls,
614 const struct GNUNET_PeerIdentity *peer,
615 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
621 * Method called whenever a peer disconnects.
624 * @param peer peer identity this notification is about
627 core_disconnect (void *cls,
629 GNUNET_PeerIdentity *peer)
634 /******************************************************************************/
635 /************************ MAIN FUNCTIONS ****************************/
636 /******************************************************************************/
639 * Process mesh requests. FIXME NON FUNCTIONAL, SKELETON
642 * @param server the initialized server
643 * @param c configuration to use
647 struct GNUNET_SERVER_Handle *server,
648 const struct GNUNET_CONFIGURATION_Handle *c)
650 struct GNUNET_CORE_Handle *core;
652 GNUNET_SERVER_add_handlers (server, plugin_handlers);
653 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
654 core = GNUNET_CORE_connect (c, /* Main configuration */
656 NULL, /* Closure passed to MESH functions */
657 &core_init, /* Call core_init once connected */
658 &core_connect, /* Handle connects */
659 &core_disconnect, /* remove peers on disconnects */
660 NULL, /* Do we care about "status" updates? */
661 NULL, /* Don't notify about all incoming messages */
662 GNUNET_NO, /* For header only in notification */
663 NULL, /* Don't notify about all outbound messages */
664 GNUNET_NO, /* For header-only out notification */
665 core_handlers); /* Register these handlers */
672 * The main function for the mesh service.
674 * @param argc number of arguments from the command line
675 * @param argv command line arguments
676 * @return 0 ok, 1 on error
679 main (int argc, char *const *argv)
684 GNUNET_SERVICE_run (argc,
687 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;