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 * - 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
35 #include "gnunet_common.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_peer_lib.h"
38 #include "gnunet_core_service.h"
39 #include "gnunet_protocols.h"
43 /******************************************************************************/
44 /******************** MESH NETWORK MESSAGES **************************/
45 /******************************************************************************/
48 * Message for mesh path management
50 struct GNUNET_MESH_ManipulatePath
53 * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
55 * Size: sizeof(struct GNUNET_MESH_ManipulatePath) + path_length * sizeof (struct GNUNET_PeerIdentity)
57 struct GNUNET_MessageHeader header;
60 * (global) Id of the tunnel this path belongs to, unique in conjunction with the origin.
62 uint32_t tid GNUNET_PACKED;
65 * Information about speed requirements. If the tunnel cannot sustain the
66 * minimum bandwidth, packets are to be dropped.
68 uint32_t speed_min GNUNET_PACKED;
73 uint32_t reserved GNUNET_PACKED;
76 * path_length structs defining the *whole* path from the origin [0] to the
77 * final destination [path_length-1].
79 // struct GNUNET_PeerIdentity peers[path_length];
83 * Message for mesh data traffic to all tunnel targets.
85 struct GNUNET_MESH_OriginMulticast
88 * Type: GNUNET_MESSAGE_TYPE_DATA_MULTICAST
90 struct GNUNET_MessageHeader header;
95 uint32_t tid GNUNET_PACKED;
100 struct GNUNET_PeerIdentity oid;
109 * Message for mesh data traffic to a particular destination from origin.
111 struct GNUNET_MESH_DataMessageFromOrigin
114 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN
116 struct GNUNET_MessageHeader header;
121 uint32_t tid GNUNET_PACKED;
126 struct GNUNET_PeerIdentity oid;
131 struct GNUNET_PeerIdentity destination;
140 * Message for mesh data traffic from a tunnel participant to origin.
142 struct GNUNET_MESH_DataMessageToOrigin
145 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN
147 struct GNUNET_MessageHeader header;
152 uint32_t tid GNUNET_PACKED;
157 struct GNUNET_PeerIdentity oid;
160 * Sender of the message.
162 struct GNUNET_PeerIdentity sender;
170 * Message for mesh flow control
172 struct GNUNET_MESH_SpeedNotify
175 * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
177 struct GNUNET_MessageHeader header;
182 uint32_t tid GNUNET_PACKED;
187 struct GNUNET_PeerIdentity oid;
190 * Slowest link down the path (above minimum speed requirement).
196 /******************************************************************************/
197 /************************ DATA STRUCTURES ****************************/
198 /******************************************************************************/
201 * All the states a peer participating in a tunnel can be in.
206 * Request sent, not yet answered.
211 * Peer connected and ready to accept data
216 * Peer connected previosly but not responding
218 MESH_PEER_UNAVAILABLE,
221 * Peer requested but not ever connected
223 MESH_PEER_UNREACHABLE
227 * Struct containing all information regarding a given peer
237 * Is the peer reachable? Is the peer even connected?
239 enum PeerState state;
242 * Who to send the data to --- what about multiple (alternate) paths?
244 GNUNET_PEER_Id first_hop;
247 * Max data rate to this peer
253 * Information regarding a path
258 * Id of the path, in case it's needed
263 * Whether the path is serving traffic in a tunnel or is a backup
268 * List of all the peers that form the path from origin to target
270 GNUNET_PEER_Id *peers;
274 * Struct containing all information regarding a tunnel
275 * For an intermediate node the improtant info used will be:
276 * - OID \ To identify
278 * - paths[0] | To know where to send it next
279 * - metainfo: ready, speeds, accounting
280 * For an end node more fields will be needed (client-handling)
285 struct MESH_tunnel *next;
287 struct MESH_tunnel *prev;
290 * Origin ID: Node that created the tunnel
295 * Tunnel number (unique for a given oid)
300 * Whether the tunnel is in a state to transmit data
305 * Minimal speed for this tunnel in kb/s
310 * Maximal speed for this tunnel in kb/s
315 * Last time the tunnel was used
317 struct GNUNET_TIME_Absolute timestamp;
320 * Peers in the tunnel, for future optimizations
322 struct PeerInfo *peers;
325 * Paths (used and backup)
330 * Messages ready to transmit??? -- real queues needed
332 struct GNUNET_MessageHeader *msg_out;
335 * Messages received and not processed??? -- real queues needed
337 struct GNUNET_MessageHeader *msg_in;
340 * If this tunnel was created by a local client, what's its handle?
342 struct GNUNET_SERVER_Client *initiator;
346 * So, I'm an endpoint. Why am I receiveing traffic?
347 * Who is interested in this? How to communicate with them?
356 struct MESH_tunnel *my_tunnels_head;
358 struct MESH_tunnel *my_tunnels_tail;
361 * If this tunnel was created by a local client, what's its handle?
363 struct GNUNET_SERVER_Client *handle;
365 unsigned int messages_subscribed_counter;
367 uint16_t *messages_subscribed;
372 // static struct MESH_tunnel *tunnel_participation_head;
374 // static struct MESH_tunnel *tunnel_participation_tail;
379 /******************************************************************************/
380 /******************** MESH NETWORK HANDLERS **************************/
381 /******************************************************************************/
384 * Core handler for path creation
385 * struct GNUNET_CORE_MessageHandler
388 * @param message message
389 * @param peer peer identity this notification is about
390 * @param atsi performance data
391 * @return GNUNET_OK to keep the connection open,
392 * GNUNET_SYSERR to close it (signal serious error)
396 handle_mesh_path_create (void *cls,
397 const struct GNUNET_PeerIdentity *peer,
398 const struct GNUNET_MessageHeader *message,
399 const struct GNUNET_TRANSPORT_ATS_Information
403 * EXAMPLE OF USING THE API
404 * NOT ACTUAL CODE!!!!!
410 GNUNET_CONTAINER_DLL_insert (c->my_tunnels_head,
414 while (NULL != (t = c->my_tunnels_head))
416 GNUNET_CONTAINER_DLL_remove (c->my_tunnels_head,
425 /* Find origin & self */
426 /* Search for origin in local tunnels */
427 /* Create tunnel / add path */
428 /* Retransmit to next link in chain, if any (core_notify + callback) */
433 * Core handler for mesh network traffic
436 * @param message message
437 * @param peer peer identity this notification is about
438 * @param atsi performance data
439 * @return GNUNET_OK to keep the connection open,
440 * GNUNET_SYSERR to close it (signal serious error)
443 handle_mesh_network_traffic (void *cls,
444 const struct GNUNET_PeerIdentity *peer,
445 const struct GNUNET_MessageHeader *message,
446 const struct GNUNET_TRANSPORT_ATS_Information
449 if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
450 /* Retransmit to next in path of tunnel identified by message */
452 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
453 /* Retransmit to previous in path of tunnel identified by message */
459 * Functions to handle messages from core
461 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
462 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
463 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
464 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
470 /******************************************************************************/
471 /********************* MESH LOCAL HANDLES **************************/
472 /******************************************************************************/
475 * Handler for client disconnection
478 * @param client identification of the client; NULL
479 * for the last call when the server is destroyed
482 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
484 /* Remove client from list, delete all timers and queues associated */
489 * Handler for new clients
492 * @param client identification of the client
493 * @param message the actual message, which includes messages the client wants
496 handle_local_new_client (void *cls,
497 struct GNUNET_SERVER_Client *client,
498 const struct GNUNET_MessageHeader *message)
504 * Handler for connection requests
507 * @param client identification of the client
508 * @param message the actual message
511 handle_local_connect (void *cls,
512 struct GNUNET_SERVER_Client *client,
513 const struct GNUNET_MessageHeader *message)
519 * Handler for client traffic
522 * @param client identification of the client
523 * @param message the actual message
526 handle_local_network_traffic (void *cls,
527 struct GNUNET_SERVER_Client *client,
528 const struct GNUNET_MessageHeader *message)
534 * Functions to handle messages from clients
537 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT 272
538 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY 273
539 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL 274
540 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD 275
541 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL 276
542 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 277
543 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL 278
544 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY 279
545 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATED 280
546 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROYED 281
547 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA 282
548 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST 283
550 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
551 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
552 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY, 0},
553 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL, 0},
554 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
555 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
556 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE, sizeof(struct GNUNET_MESH_ConnectPeerByType)},
557 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL, 0},
558 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY, 0},
559 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, /* FIXME needed? */
560 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0}, /* FIXME needed? */
566 * To be called on core init/fail.
568 * @param cls service closure
569 * @param server handle to the server for this service
570 * @param identity the public identity of this peer
571 * @param publicKey the public key of this peer
574 core_init (void *cls,
575 struct GNUNET_CORE_Handle *server,
576 const struct GNUNET_PeerIdentity *identity,
577 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
583 * Method called whenever a given peer connects.
586 * @param peer peer identity this notification is about
587 * @param atsi performance data for the connection
590 core_connect (void *cls,
591 const struct GNUNET_PeerIdentity *peer,
592 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
598 * Method called whenever a peer disconnects.
601 * @param peer peer identity this notification is about
604 core_disconnect (void *cls,
606 GNUNET_PeerIdentity *peer)
612 * Process mesh requests. FIXME NON FUNCTIONAL, SKELETON
615 * @param server the initialized server
616 * @param c configuration to use
620 struct GNUNET_SERVER_Handle *server,
621 const struct GNUNET_CONFIGURATION_Handle *c)
623 struct GNUNET_CORE_Handle *core;
625 GNUNET_SERVER_add_handlers (server, plugin_handlers);
626 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
627 core = GNUNET_CORE_connect (c, /* Main configuration */
629 NULL, /* Closure passed to MESH functions */
630 &core_init, /* Call core_init once connected */
631 &core_connect, /* Handle connects */
632 &core_disconnect, /* remove peers on disconnects */
633 NULL, /* Do we care about "status" updates? */
634 NULL, /* Don't want notified about all incoming messages */
635 GNUNET_NO, /* For header only inbound notification */
636 NULL, /* Don't want notified about all outbound messages */
637 GNUNET_NO, /* For header only outbound notification */
638 core_handlers); /* Register these handlers */
645 * The main function for the mesh service.
647 * @param argc number of arguments from the command line
648 * @param argv command line arguments
649 * @return 0 ok, 1 on error
652 main (int argc, char *const *argv)
657 GNUNET_SERVICE_run (argc,
660 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;