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_core_service.h"
38 #include "gnunet_protocols.h"
42 /******************************************************************************/
43 /******************** MESH NETWORK MESSAGES **************************/
44 /******************************************************************************/
47 * Message for mesh path management
49 struct GNUNET_MESH_ManipulatePath
52 * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
54 * Size: sizeof(struct GNUNET_MESH_ManipulatePath) + path_length * sizeof (struct GNUNET_PeerIdentity)
56 struct GNUNET_MessageHeader header;
59 * Id of the tunnel this path belongs to, unique in conjunction with the origin.
61 uint32_t tid GNUNET_PACKED;
64 * Information about speed requirements. If the tunnel cannot sustain the
65 * minimum bandwidth, packets are to be dropped.
67 uint32_t speed_min GNUNET_PACKED;
70 * path_length structs defining the *whole* path from the origin [0] to the
71 * final destination [path_length-1].
73 // struct GNUNET_PeerIdentity peers[path_length];
77 * Message for mesh data traffic to all tunnel targets.
79 struct GNUNET_MESH_OriginMulticast
82 * Type: GNUNET_MESSAGE_TYPE_DATA_MULTICAST
84 struct GNUNET_MessageHeader header;
89 uint32_t tid GNUNET_PACKED;
94 struct GNUNET_PeerIdentity oid;
97 * FIXME: Some form of authentication
108 * Message for mesh data traffic to a particular destination from origin.
110 struct GNUNET_MESH_DataMessageFromOrigin
113 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN
115 struct GNUNET_MessageHeader header;
120 uint32_t tid GNUNET_PACKED;
125 struct GNUNET_PeerIdentity oid;
130 struct GNUNET_PeerIdentity destination;
133 * FIXME: Some form of authentication
144 * Message for mesh data traffic from a tunnel participant to origin.
146 struct GNUNET_MESH_DataMessageToOrigin
149 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN
151 struct GNUNET_MessageHeader header;
156 uint32_t tid GNUNET_PACKED;
161 struct GNUNET_PeerIdentity oid;
164 * Sender of the message.
166 struct GNUNET_PeerIdentity sender;
169 * FIXME: Some form of authentication
179 * Message for mesh flow control
181 struct GNUNET_MESH_SpeedNotify
184 * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
186 struct GNUNET_MessageHeader header;
191 uint32_t tid GNUNET_PACKED;
196 struct GNUNET_PeerIdentity oid;
199 * Slowest link down the path (above minimum speed requirement).
205 /******************************************************************************/
206 /************************ DATA STRUCTURES ****************************/
207 /******************************************************************************/
210 * All the states a peer participating in a tunnel can be in.
215 * Request sent, not yet answered.
220 * Peer connected and ready to accept data
225 * Peer connected previosly but not responding
227 MESH_PEER_UNAVAILABLE,
230 * Peer requested but not ever connected
232 MESH_PEER_UNREACHABLE
236 * Struct containing all information regarding a given peer
243 struct GNUNET_PeerIdentity id;
246 * Is the peer reachable? Is the peer even connected?
248 enum PeerState state;
251 * Who to send the data to
256 * Max data rate to this peer
262 * Information regarding a path
267 * Id of the path, in case it's needed
272 * Whether the path is serving traffic in a tunnel or is a backup
277 * List of all the peers that form the path from origin to target
279 struct PeerInfo *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)
294 * Origin ID: Node that created the tunnel
296 struct GNUNET_PeerIdentity oid;
299 * Tunnel number (unique for a given oid)
304 * Whether the tunnel is in state to transmit data
309 * Minimal speed for this tunnel in kb/s
314 * Maximal speed for this tunnel in kb/s
319 * Last time the tunnel was used
321 struct GNUNET_TIME_Absolute timestamp;
324 * Peers in the tunnel, for future optimizations
326 struct PeerInfo *peers;
329 * Paths (used and backup)
334 * Messages ready to transmit
336 struct GNUNET_MessageHeader *msg_out;
339 * Messages received and not processed
341 struct GNUNET_MessageHeader *msg_in;
344 * FIXME Clients. Is anyone to be notified for traffic here?
349 * So, I'm an endpoint. Why am I receiveing traffic?
350 * Who is interested in this? How to communicate with them?
355 * FIXME add structures needed to handle client connections
362 /******************************************************************************/
363 /******************** MESH NETWORK HANDLERS **************************/
364 /******************************************************************************/
367 * Core handler for path creation
368 * struct GNUNET_CORE_MessageHandler
371 * @param message message
372 * @param peer peer identity this notification is about
373 * @param atsi performance data
374 * @return GNUNET_OK to keep the connection open,
375 * GNUNET_SYSERR to close it (signal serious error)
379 handle_mesh_path_create (void *cls,
380 const struct GNUNET_PeerIdentity *peer,
381 const struct GNUNET_MessageHeader *message,
382 const struct GNUNET_TRANSPORT_ATS_Information
386 /* Find origin & self */
387 /* Search for origin in local tunnels */
388 /* Create tunnel / add path */
389 /* Retransmit to next link in chain, if any (core_notify + callback) */
394 * Core handler for mesh network traffic
397 * @param message message
398 * @param peer peer identity this notification is about
399 * @param atsi performance data
400 * @return GNUNET_OK to keep the connection open,
401 * GNUNET_SYSERR to close it (signal serious error)
404 handle_mesh_network_traffic (void *cls,
405 const struct GNUNET_PeerIdentity *peer,
406 const struct GNUNET_MessageHeader *message,
407 const struct GNUNET_TRANSPORT_ATS_Information
410 if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
411 /* Retransmit to next in path of tunnel identified by message */
413 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
414 /* Retransmit to previous in path of tunnel identified by message */
420 * Functions to handle messages from core
422 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
423 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
424 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
425 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
431 /******************************************************************************/
432 /********************* MESH LOCAL HANDLES **************************/
433 /******************************************************************************/
436 * Handler for client disconnection
439 * @param client identification of the client; NULL
440 * for the last call when the server is destroyed
443 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
445 /* Remove client from list, delete all timers and queues associated */
450 * Handler for new clients
453 * @param client identification of the client
454 * @param message the actual message, which includes messages the client wants
457 handle_local_new_client (void *cls,
458 struct GNUNET_SERVER_Client *client,
459 const struct GNUNET_MessageHeader *message)
465 * Handler for connection requests
468 * @param client identification of the client
469 * @param message the actual message
472 handle_local_connect (void *cls,
473 struct GNUNET_SERVER_Client *client,
474 const struct GNUNET_MessageHeader *message)
480 * Handler for client traffic
483 * @param client identification of the client
484 * @param message the actual message
487 handle_local_network_traffic (void *cls,
488 struct GNUNET_SERVER_Client *client,
489 const struct GNUNET_MessageHeader *message)
495 * Functions to handle messages from clients
498 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT 272
499 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY 273
500 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL 274
501 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD 275
502 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL 276
503 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 277
504 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL 278
505 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY 279
506 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATED 280
507 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROYED 281
508 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA 282
509 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST 283
511 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
512 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
513 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY, 0},
514 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL, 0},
515 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
516 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
517 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE, sizeof(struct GNUNET_MESH_ConnectPeerByType)},
518 {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL, sizeof(struct GNUNET_MESH_Control)},
519 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY, sizeof(struct GNUNET_MESH_Control)},
520 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, /* FIXME needed? */
521 {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0}, /* FIXME needed? */
527 * To be called on core init/fail.
529 * @param cls service closure
530 * @param server handle to the server for this service
531 * @param identity the public identity of this peer
532 * @param publicKey the public key of this peer
535 core_init (void *cls,
536 struct GNUNET_CORE_Handle *server,
537 const struct GNUNET_PeerIdentity *identity,
538 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
544 * Method called whenever a given peer connects.
547 * @param peer peer identity this notification is about
548 * @param atsi performance data for the connection
551 core_connect (void *cls,
552 const struct GNUNET_PeerIdentity *peer,
553 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
559 * Method called whenever a peer disconnects.
562 * @param peer peer identity this notification is about
565 core_disconnect (void *cls,
567 GNUNET_PeerIdentity *peer)
573 * Process mesh requests. FIXME NON FUNCTIONAL, SKELETON
576 * @param server the initialized server
577 * @param c configuration to use
581 struct GNUNET_SERVER_Handle *server,
582 const struct GNUNET_CONFIGURATION_Handle *c)
584 struct GNUNET_CORE_Handle *core;
586 GNUNET_SERVER_add_handlers (server, plugin_handlers);
587 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
588 core = GNUNET_CORE_connect (c, /* Main configuration */
590 NULL, /* Closure passed to MESH functions */
591 &core_init, /* Call core_init once connected */
592 &core_connect, /* Handle connects */
593 &core_disconnect, /* remove peers on disconnects */
594 NULL, /* Do we care about "status" updates? */
595 NULL, /* Don't want notified about all incoming messages */
596 GNUNET_NO, /* For header only inbound notification */
597 NULL, /* Don't want notified about all outbound messages */
598 GNUNET_NO, /* For header only outbound notification */
599 core_handlers); /* Register these handlers */
606 * The main function for the mesh service.
608 * @param argc number of arguments from the command line
609 * @param argv command line arguments
610 * @return 0 ok, 1 on error
613 main (int argc, char *const *argv)
618 GNUNET_SERVICE_run (argc,
621 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;