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 * @author Bartlomiej Polot
30 #define MESH_DEBUG GNUNET_YES
34 #include "gnunet_common.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_peer_lib.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_protocols.h"
39 #include <gnunet_mesh_service_new.h>
41 /******************************************************************************/
42 /******************** MESH LOCAL MESSAGES *************************/
43 /******************************************************************************/
44 /* Any API call should be documented in the folowing table under API CALL.
45 * Also, any message type should be documented in the following table, with the
48 * API CALL (GNUNET_MESH_*) MESSAGE USED
49 * ------------------------ ------------
50 * connect GNUNET_MESH_ClientConnect
51 * disconnect None (network level disconnect)
53 * tunnel_create GNUNET_MESH_TunnelMessage
54 * tunnel_destroy GNUNET_MESH_TunnelMessage
56 * peer_request_connect_add GNUNET_MESH_PeerControl
57 * peer_request_connect_del GNUNET_MESH_PeerControl
58 * peer_request_connect_by_type GNUNET_MESH_ConnectPeerByType
60 * notify_transmit_ready *GNUNET_MESH_TransmitReady?*
61 * notify_transmit_ready_cancel None (clear of internal data structures)
67 * data GNUNET_MESH_Data OR
68 * GNUNET_MESH_DataBroadcast
69 * new incoming tunnel GNUNET_MESH_PeerControl
70 * peer connects to a tunnel GNUNET_MESH_PeerControl
71 * peer disconnects from a tunnel GNUNET_MESH_PeerControl
74 /******************************************************************************/
75 /************************** CONSTANTS ******************************/
76 /******************************************************************************/
78 #define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI 0x80000000
79 #define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV 0xB0000000
81 #define CORE_QUEUE_SIZE 10
82 #define LOCAL_QUEUE_SIZE 100
84 /******************************************************************************/
85 /************************** MESSAGES ******************************/
86 /******************************************************************************/
89 * Message for a client to register to the service
91 struct GNUNET_MESH_ClientConnect
94 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
96 * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
97 * sizeof(MESH_ApplicationType) * applications +
98 * sizeof(uint16_t) * types
100 struct GNUNET_MessageHeader header;
101 uint16_t applications GNUNET_PACKED;
102 uint16_t types GNUNET_PACKED;
103 /* uint16_t list_apps[applications] */
104 /* uint16_t list_types[types] */
109 * Type for tunnel numbering.
110 * - Local tunnel numbers are >= 0x80000000
111 * - Global tunnel numbers are < 0x80000000
113 typedef uint32_t MESH_TunnelNumber;
116 * Message for a client to create and destroy tunnels.
118 struct GNUNET_MESH_TunnelMessage
121 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
123 * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
125 struct GNUNET_MessageHeader header;
128 * ID of a tunnel controlled by this client.
130 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
135 * Message for the service to let a client know about created tunnels.
137 struct GNUNET_MESH_TunnelNotification
140 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
142 * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
144 struct GNUNET_MessageHeader header;
147 * ID of a tunnel controlled by this client.
149 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
152 * Peer at the other end, if any
154 struct GNUNET_PeerIdentity peer;
159 * - request adding and deleting peers from a tunnel
160 * - notify the client that peers have connected:
162 * -- unrequested (new incoming tunnels)
163 * - notify the client that peers have disconnected
165 struct GNUNET_MESH_PeerControl
169 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL]
170 * (client to service, client created tunnel)
171 * GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
172 * (service to client)
174 * Size: sizeof(struct GNUNET_MESH_PeerControl)
176 struct GNUNET_MessageHeader header;
179 * ID of a tunnel controlled by this client.
181 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
184 * Peer to connect/disconnect.
186 struct GNUNET_PeerIdentity peer;
191 * Message for connecting to peers offering a certain service.
193 struct GNUNET_MESH_ConnectPeerByType
196 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
197 * GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
199 struct GNUNET_MessageHeader header;
202 * ID of a tunnel controlled by this client.
204 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
209 GNUNET_MESH_ApplicationType type GNUNET_PACKED;
213 /******************************************************************************/
214 /************************ ENUMERATIONS ****************************/
215 /******************************************************************************/
218 * All the states a peer participating in a tunnel can be in.
223 * Peer only retransmits traffic, is not a final destination
228 * Path to the peer not known yet
233 * Request sent, not yet answered.
238 * Peer connected and ready to accept data
243 * Peer connected previosly but not responding
245 MESH_PEER_RECONNECTING
249 /******************************************************************************/
250 /************************ DATA STRUCTURES ****************************/
251 /******************************************************************************/
254 * Information regarding a possible path to reach a single peer
262 struct MeshPeerPath *next;
263 struct MeshPeerPath *prev;
266 * List of all the peers that form the path from origin to target.
268 GNUNET_PEER_Id *peers;
271 * Number of peers (hops) in the path
279 * Node of path tree for a tunnel
281 struct MeshTunnelPathNode
284 * Tunnel this node belongs to (and therefore tree)
286 struct MeshTunnel *t;
289 * Peer this node describes
294 * Parent node in the tree
296 struct MeshTunnelPathNode *parent;
301 struct MeshTunnelPathNode *children;
306 unsigned int nchildren;
309 * Status of the peer in the tunnel
311 enum MeshPeerState status;
316 * Tree to reach all peers in the tunnel
318 struct MeshTunnelPath
321 * How often to refresh the path
323 struct GNUNET_TIME_Relative refresh;
326 * Tunnel this path belongs to
328 struct MeshTunnel *t;
331 * Root node of peer tree
333 struct MeshTunnelPathNode *root;
336 * Node that represents our position in the tree (for non local tunnels)
338 struct MeshTunnelPathNode *me;
341 * Cache of all peers and the first hop to them.
342 * Indexed by Peer_Identity, contains a pointer to the PeerIdentity
345 struct GNUNET_CONTAINER_MultiHashMap *first_hops;
350 /** FWD declaration */
354 * Struct containing all info possibly needed to build a package when called
357 struct MeshDataDescriptor
359 /** ID of the tunnel this packet travels in */
360 struct MESH_TunnelID *origin;
362 /** Ultimate destination of the packet */
363 GNUNET_PEER_Id destination;
365 /** Number of identical messages sent to different hops (multicast) */
368 /** Size of the data */
371 /** Client that asked for the transmission, if any */
372 struct GNUNET_SERVER_Client *client;
374 /** Who was is message being sent to */
375 struct MeshPeerInfo *peer;
377 /** Which handler was used to request the transmission */
378 unsigned int handler_n;
380 /* Data at the end */
385 * Struct containing all information regarding a given peer
395 * Last time we heard from this peer
397 struct GNUNET_TIME_Absolute last_contact;
400 * Number of attempts to reconnect so far
402 int n_reconnect_attempts;
405 * Paths to reach the peer, ordered by ascending hop count
407 struct MeshPeerPath *path_head;
410 * Paths to reach the peer, ordered by ascending hop count
412 struct MeshPeerPath *path_tail;
415 * Handle to stop the DHT search for a path to this peer
417 struct GNUNET_DHT_GetHandle *dhtget;
420 * Handles to stop queued transmissions for this peer
422 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
425 * Pointer to info stuctures used as cls for queued transmissions
427 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
430 * Array of tunnels this peer participates in
431 * (most probably a small amount, therefore not a hashmap)
432 * When the path to the peer changes, notify these tunnels to let them
433 * re-adjust their path trees.
435 struct MeshTunnel **tunnels;
438 * Number of tunnels above
440 unsigned int ntunnels;
445 * Data scheduled to transmit (to local client or remote peer)
452 struct MeshQueue *next;
453 struct MeshQueue *prev;
456 * Target of the data (NULL if target is client)
458 struct MeshPeerInfo *peer;
461 * Client to send the data to (NULL if target is peer)
463 struct MeshClient *client;
466 * Size of the message to transmit
471 * How old is the data?
473 struct GNUNET_TIME_Absolute timestamp;
478 struct GNUNET_MessageHeader *data;
482 * Globally unique tunnel identification (owner + number)
483 * DO NOT USE OVER THE NETWORK
488 * Node that owns the tunnel
493 * Tunnel number to differentiate all the tunnels owned by the node oid
494 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
496 MESH_TunnelNumber tid;
500 struct MeshClient; /* FWD declaration */
503 * Struct containing all information regarding a tunnel
504 * For an intermediate node the improtant info used will be:
505 * - id Tunnel unique identification
506 * - paths[0] To know where to send it next
507 * - metainfo: ready, speeds, accounting
514 struct MESH_TunnelID id;
517 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
519 MESH_TunnelNumber local_tid;
522 * Last time the tunnel was used
524 struct GNUNET_TIME_Absolute timestamp;
527 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
529 struct GNUNET_CONTAINER_MultiHashMap *peers;
532 * Number of peers that are connected and potentially ready to receive data
534 unsigned int peers_ready;
537 * Number of peers that have been added to the tunnel
539 unsigned int peers_total;
542 * Client owner of the tunnel, if any
544 struct MeshClient *client;
547 * Messages ready to transmit
549 struct MeshQueue *queue_head;
550 struct MeshQueue *queue_tail;
555 struct MeshTunnelPath *tree;
558 * Task to keep the used paths alive
560 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
565 * Info needed to work with tunnel paths and peers
572 struct MeshTunnel *t;
577 struct MeshPeerInfo *peer;
582 struct MeshPeerPath *path;
587 * Struct containing information about a client of the service
594 struct MeshClient *next;
595 struct MeshClient *prev;
598 * Tunnels that belong to this client, indexed by local id
600 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
603 * Handle to communicate with the client
605 struct GNUNET_SERVER_Client *handle;
608 * Applications that this client has claimed to provide
610 struct GNUNET_CONTAINER_MultiHashMap *apps;
613 * Messages that this client has declared interest in
615 struct GNUNET_CONTAINER_MultiHashMap *types;
618 * Used to search peers offering a service
620 struct GNUNET_DHT_GetHandle *dht_get_type;
624 * ID of the client, for debug messages