#ifndef MESH_H_
#define MESH_H_
+
+#ifdef __cplusplus
+extern "C"
+{
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
#include <stdint.h>
#define MESH_DEBUG GNUNET_YES
-
#include "platform.h"
-#include "gnunet_common.h"
#include "gnunet_util_lib.h"
#include "gnunet_peer_lib.h"
#include "gnunet_core_service.h"
#include "gnunet_protocols.h"
-#include <gnunet_mesh_service_new.h>
-
-/******************************************************************************/
-/******************** MESH LOCAL MESSAGES *************************/
-/******************************************************************************/
-/* Any API call should be documented in the folowing table under API CALL.
- * Also, any message type should be documented in the following table, with the
- * associated event.
- *
- * API CALL (GNUNET_MESH_*) MESSAGE USED
- * ------------------------ ------------
- * connect GNUNET_MESH_ClientConnect
- * disconnect None (network level disconnect)
- *
- * tunnel_create GNUNET_MESH_TunnelMessage
- * tunnel_destroy GNUNET_MESH_TunnelMessage
- *
- * peer_request_connect_add GNUNET_MESH_PeerControl
- * peer_request_connect_del GNUNET_MESH_PeerControl
- * peer_request_connect_by_type GNUNET_MESH_ConnectPeerByType
- *
- * notify_transmit_ready *GNUNET_MESH_TransmitReady?*
- * notify_transmit_ready_cancel None (clear of internal data structures)
- *
- *
- *
- * EVENT MESSAGE USED
- * ----- ------------
- * data GNUNET_MESH_Data OR
- * GNUNET_MESH_DataBroadcast
- * new incoming tunnel GNUNET_MESH_PeerControl
- * peer connects to a tunnel GNUNET_MESH_PeerControl
- * peer disconnects from a tunnel GNUNET_MESH_PeerControl
- */
+#include <gnunet_mesh_service.h>
/******************************************************************************/
/************************** CONSTANTS ******************************/
/******************************************************************************/
-#define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI 0x80000000
-#define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV 0xB0000000
+#define GNUNET_MESH_LOCAL_CHANNEL_ID_CLI 0x80000000
+#define GNUNET_MESH_LOCAL_CHANNEL_ID_SERV 0xB0000000
+
+#define HIGH_PID 0xFFFF0000
+#define LOW_PID 0x0000FFFF
-#define CORE_QUEUE_SIZE 10
-#define LOCAL_QUEUE_SIZE 100
+#define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
/******************************************************************************/
/************************** MESSAGES ******************************/
/******************************************************************************/
+GNUNET_NETWORK_STRUCT_BEGIN
+
/**
* Message for a client to register to the service
*/
* sizeof(uint16_t) * types
*/
struct GNUNET_MessageHeader header;
- uint16_t applications GNUNET_PACKED;
- uint16_t types GNUNET_PACKED;
- /* uint16_t list_apps[applications] */
- /* uint16_t list_types[types] */
+ /* uint32_t list_ports[] */
};
/**
- * Type for tunnel numbering.
- * - Local tunnel numbers are >= 0x80000000
- * - Global tunnel numbers are < 0x80000000
+ * Type for channel numbering.
+ * - Local channel numbers given by the service (incoming) are >= 0xB0000000
+ * - Local channel numbers given by the client (created) are >= 0x80000000
+ * - Global channel numbers are < 0x80000000
*/
-typedef uint32_t MESH_TunnelNumber;
+typedef uint32_t MESH_ChannelNumber;
+
/**
- * Message for a client to create and destroy tunnels.
+ * Hash to be used in Mesh communication. Only 256 bits needed,
+ * instead of the 512 from @c GNUNET_HashCode.
+ *
*/
-struct GNUNET_MESH_TunnelMessage
+struct GNUNET_MeshHash
{
- /**
- * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
- *
- * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
- */
- struct GNUNET_MessageHeader header;
-
- /**
- * ID of a tunnel controlled by this client.
- */
- MESH_TunnelNumber tunnel_id GNUNET_PACKED;
+ unsigned char bits[256 / 8];
};
/**
- * Message for the service to let a client know about created tunnels.
+ * Message for a client to create and destroy channels.
*/
-struct GNUNET_MESH_TunnelNotification
+struct GNUNET_MESH_ChannelMessage
{
/**
- * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
*
- * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
+ * Size: sizeof(struct GNUNET_MESH_ChannelMessage)
*/
struct GNUNET_MessageHeader header;
/**
- * ID of a tunnel controlled by this client.
+ * ID of a channel controlled by this client.
*/
- MESH_TunnelNumber tunnel_id GNUNET_PACKED;
+ MESH_ChannelNumber channel_id GNUNET_PACKED;
/**
- * Peer at the other end, if any
+ * Channel's peer
*/
struct GNUNET_PeerIdentity peer;
-};
-/**
- * Message for:
- * - request adding and deleting peers from a tunnel
- * - notify the client that peers have connected:
- * -- requested
- * -- unrequested (new incoming tunnels)
- * - notify the client that peers have disconnected
- */
-struct GNUNET_MESH_PeerControl
-{
-
- /**
- * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL]
- * (client to service, client created tunnel)
- * GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
- * (service to client)
- *
- * Size: sizeof(struct GNUNET_MESH_PeerControl)
- */
- struct GNUNET_MessageHeader header;
-
- /**
- * ID of a tunnel controlled by this client.
- */
- MESH_TunnelNumber tunnel_id GNUNET_PACKED;
-
- /**
- * Peer to connect/disconnect.
- */
- struct GNUNET_PeerIdentity peer;
-};
-
-
-/**
- * Message for connecting to peers offering a certain service.
- */
-struct GNUNET_MESH_ConnectPeerByType
-{
/**
- * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
- * GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
+ * Port of the channel.
*/
- struct GNUNET_MessageHeader header;
+ uint32_t port GNUNET_PACKED;
- /**
- * ID of a tunnel controlled by this client.
- */
- MESH_TunnelNumber tunnel_id GNUNET_PACKED;
-
- /**
- * Type specification
- */
- GNUNET_MESH_ApplicationType type GNUNET_PACKED;
+ /**
+ * Options.
+ */
+ uint32_t opt GNUNET_PACKED;
};
-/******************************************************************************/
-/************************ ENUMERATIONS ****************************/
-/******************************************************************************/
-
/**
- * All the states a peer participating in a tunnel can be in.
+ * Message for mesh data traffic.
*/
-enum MeshPeerState
+struct GNUNET_MESH_LocalData
{
/**
- * Peer only retransmits traffic, is not a final destination
- */
- MESH_PEER_RELAY,
-
- /**
- * Path to the peer not known yet
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
*/
- MESH_PEER_SEARCHING,
-
- /**
- * Request sent, not yet answered.
- */
- MESH_PEER_WAITING,
+ struct GNUNET_MessageHeader header;
/**
- * Peer connected and ready to accept data
+ * ID of the channel
*/
- MESH_PEER_READY,
+ uint32_t id GNUNET_PACKED;
/**
- * Peer connected previosly but not responding
+ * Payload follows
*/
- MESH_PEER_RECONNECTING
};
-/******************************************************************************/
-/************************ DATA STRUCTURES ****************************/
-/******************************************************************************/
-
/**
- * Information regarding a possible path to reach a single peer
+ * Message to allow the client send more data to the service
+ * (always service -> client).
*/
-struct MeshPeerPath
+struct GNUNET_MESH_LocalAck
{
-
- /**
- * Linked list
- */
- struct MeshPeerPath *next;
- struct MeshPeerPath *prev;
-
/**
- * List of all the peers that form the path from origin to target.
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
*/
- GNUNET_PEER_Id *peers;
+ struct GNUNET_MessageHeader header;
/**
- * Number of peers (hops) in the path
+ * ID of the channel allowed to send more data.
*/
- unsigned int length;
+ MESH_ChannelNumber channel_id GNUNET_PACKED;
};
/**
- * Node of path tree for a tunnel
+ * Message to inform the client about channels in the service.
*/
-struct MeshTunnelPathNode
+struct GNUNET_MESH_LocalInfo
{
/**
- * Tunnel this node belongs to (and therefore tree)
- */
- struct MeshTunnel *t;
-
- /**
- * Peer this node describes
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO[_TUNNEL,_PEER]
*/
- GNUNET_PEER_Id peer;
+ struct GNUNET_MessageHeader header;
/**
- * Parent node in the tree
+ * ID of the channel allowed to send more data.
*/
- struct MeshTunnelPathNode *parent;
+ MESH_ChannelNumber channel_id GNUNET_PACKED;
/**
- * Array of children
+ * ID of the owner of the channel (can be local peer).
*/
- struct MeshTunnelPathNode *children;
+// struct GNUNET_PeerIdentity owner;
/**
- * Number of children
+ * ID of the destination of the channel (can be local peer).
*/
- unsigned int nchildren;
-
- /**
- * Status of the peer in the tunnel
- */
- enum MeshPeerState status;
+ struct GNUNET_PeerIdentity peer;
};
/**
- * Tree to reach all peers in the tunnel
+ * Message to inform the client about one of the peers in the service.
*/
-struct MeshTunnelPath
+struct GNUNET_MESH_LocalInfoPeer
{
/**
- * How often to refresh the path
- */
- struct GNUNET_TIME_Relative refresh;
-
- /**
- * Tunnel this path belongs to
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEER[S]
*/
- struct MeshTunnel *t;
+ struct GNUNET_MessageHeader header;
/**
- * Root node of peer tree
+ * Number of paths.
*/
- struct MeshTunnelPathNode *root;
+ uint16_t paths GNUNET_PACKED;
/**
- * Node that represents our position in the tree (for non local tunnels)
+ * Do we have a tunnel toward this peer?
*/
- struct MeshTunnelPathNode *me;
+ int16_t tunnel GNUNET_PACKED;
/**
- * Cache of all peers and the first hop to them.
- * Indexed by Peer_Identity, contains a pointer to the PeerIdentity
- * of 1st hop.
+ * ID of the destination of the tunnel (can be local peer).
*/
- struct GNUNET_CONTAINER_MultiHashMap *first_hops;
+ struct GNUNET_PeerIdentity destination;
+ /* If type == PEER (no 'S'): GNUNET_PeerIdentity paths[]
+ * (each path ends in destination) */
};
-
-/** FWD declaration */
-struct MeshPeerInfo;
-
/**
- * Struct containing all info possibly needed to build a package when called
- * back by core.
+ * Message to inform the client about one of the tunnels in the service.
*/
-struct MeshDataDescriptor
+struct GNUNET_MESH_LocalInfoTunnel
{
- /** ID of the tunnel this packet travels in */
- struct MESH_TunnelID *origin;
-
- /** Ultimate destination of the packet */
- GNUNET_PEER_Id destination;
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL[S]
+ */
+ struct GNUNET_MessageHeader header;
- /** Number of identical messages sent to different hops (multicast) */
- unsigned int copies;
+ /**
+ * Number of channels.
+ */
+ uint32_t channels GNUNET_PACKED;
- /** Size of the data */
- size_t size;
+ /**
+ * ID of the destination of the tunnel (can be local peer).
+ */
+ struct GNUNET_PeerIdentity destination;
- /** Client that asked for the transmission, if any */
- struct GNUNET_SERVER_Client *client;
+ /**
+ * Number of connections.
+ */
+ uint32_t connections GNUNET_PACKED;
- /** Who was is message being sent to */
- struct MeshPeerInfo *peer;
+ /**
+ * Encryption state.
+ */
+ uint16_t estate GNUNET_PACKED;
- /** Which handler was used to request the transmission */
- unsigned int handler_n;
+ /**
+ * Connection state.
+ */
+ uint16_t cstate GNUNET_PACKED;
- /* Data at the end */
+ /* If TUNNEL (no 'S'): GNUNET_PeerIdentity connection_ids[connections] */
+ /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
};
-/**
- * Struct containing all information regarding a given peer
- */
-struct MeshPeerInfo
-{
- /**
- * ID of the peer
- */
- GNUNET_PEER_Id id;
-
- /**
- * Last time we heard from this peer
- */
- struct GNUNET_TIME_Absolute last_contact;
+GNUNET_NETWORK_STRUCT_END
- /**
- * Number of attempts to reconnect so far
- */
- int n_reconnect_attempts;
-
- /**
- * Paths to reach the peer, ordered by ascending hop count
- */
- struct MeshPeerPath *path_head;
-
- /**
- * Paths to reach the peer, ordered by ascending hop count
- */
- struct MeshPeerPath *path_tail;
-
- /**
- * Handle to stop the DHT search for a path to this peer
- */
- struct GNUNET_DHT_GetHandle *dhtget;
-
- /**
- * Handles to stop queued transmissions for this peer
- */
- struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
-
- /**
- * Pointer to info stuctures used as cls for queued transmissions
- */
- struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
-
- /**
- * Array of tunnels this peer participates in
- * (most probably a small amount, therefore not a hashmap)
- * When the path to the peer changes, notify these tunnels to let them
- * re-adjust their path trees.
- */
- struct MeshTunnel **tunnels;
-
- /**
- * Number of tunnels above
- */
- unsigned int ntunnels;
-};
/**
- * Data scheduled to transmit (to local client or remote peer)
+ * @brief Translate a fwd variable into a string representation, for logging.
+ *
+ * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
+ *
+ * @return String representing FWD or BCK.
*/
-struct MeshQueue
-{
- /**
- * Double linked list
- */
- struct MeshQueue *next;
- struct MeshQueue *prev;
-
- /**
- * Target of the data (NULL if target is client)
- */
- struct MeshPeerInfo *peer;
-
- /**
- * Client to send the data to (NULL if target is peer)
- */
- struct MeshClient *client;
-
- /**
- * Size of the message to transmit
- */
- unsigned int size;
+char *
+GM_f2s (int fwd);
- /**
- * How old is the data?
- */
- struct GNUNET_TIME_Absolute timestamp;
-
- /**
- * Data itself
- */
- struct GNUNET_MessageHeader *data;
-};
/**
- * Globally unique tunnel identification (owner + number)
- * DO NOT USE OVER THE NETWORK
+ * Check if one pid is bigger than other, accounting for overflow.
+ *
+ * @param bigger Argument that should be bigger.
+ * @param smaller Argument that should be smaller.
+ *
+ * @return True if bigger (arg1) has a higher value than smaller (arg 2).
*/
-struct MESH_TunnelID
-{
- /**
- * Node that owns the tunnel
- */
- GNUNET_PEER_Id oid;
-
- /**
- * Tunnel number to differentiate all the tunnels owned by the node oid
- * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
- */
- MESH_TunnelNumber tid;
-};
+int
+GM_is_pid_bigger (uint32_t bigger, uint32_t smaller);
-struct MeshClient; /* FWD declaration */
-
/**
- * Struct containing all information regarding a tunnel
- * For an intermediate node the improtant info used will be:
- * - id Tunnel unique identification
- * - paths[0] To know where to send it next
- * - metainfo: ready, speeds, accounting
+ * Get the higher ACK value out of two values, taking in account overflow.
+ *
+ * @param a First ACK value.
+ * @param b Second ACK value.
+ *
+ * @return Highest ACK value from the two.
*/
-struct MeshTunnel
-{
- /**
- * Tunnel ID
- */
- struct MESH_TunnelID id;
-
- /**
- * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
- */
- MESH_TunnelNumber local_tid;
-
- /**
- * Last time the tunnel was used
- */
- struct GNUNET_TIME_Absolute timestamp;
-
- /**
- * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
- */
- struct GNUNET_CONTAINER_MultiHashMap *peers;
-
- /**
- * Number of peers that are connected and potentially ready to receive data
- */
- unsigned int peers_ready;
-
- /**
- * Number of peers that have been added to the tunnel
- */
- unsigned int peers_total;
-
- /**
- * Client owner of the tunnel, if any
- */
- struct MeshClient *client;
-
- /**
- * Messages ready to transmit
- */
- struct MeshQueue *queue_head;
- struct MeshQueue *queue_tail;
-
- /**
- * Tunnel paths
- */
- struct MeshTunnelPath *tree;
-
- /**
- * Task to keep the used paths alive
- */
- GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
-};
+uint32_t
+GM_max_pid (uint32_t a, uint32_t b);
/**
- * Info needed to work with tunnel paths and peers
+ * Get the lower ACK value out of two values, taking in account overflow.
+ *
+ * @param a First ACK value.
+ * @param b Second ACK value.
+ *
+ * @return Lowest ACK value from the two.
*/
-struct MeshPathInfo
-{
- /**
- * Tunnel
- */
- struct MeshTunnel *t;
-
- /**
- * Destination peer
- */
- struct MeshPeerInfo *peer;
-
- /**
- * Path itself
- */
- struct MeshPeerPath *path;
-};
+uint32_t
+GM_min_pid (uint32_t a, uint32_t b);
/**
- * Struct containing information about a client of the service
+ * Convert a 256 bit MeshHash into a 512 HashCode to use in GNUNET_h2s,
+ * multihashmap, and other HashCode-based functions.
+ *
+ * @param id A 256 bit hash to expand.
+ *
+ * @return A HashCode containing the original 256 bit hash right-padded with 0.
*/
-struct MeshClient
-{
- /**
- * Linked list
- */
- struct MeshClient *next;
- struct MeshClient *prev;
-
- /**
- * Tunnels that belong to this client, indexed by local id
- */
- struct GNUNET_CONTAINER_MultiHashMap *tunnels;
-
- /**
- * Handle to communicate with the client
- */
- struct GNUNET_SERVER_Client *handle;
-
- /**
- * Applications that this client has claimed to provide
- */
- struct GNUNET_CONTAINER_MultiHashMap *apps;
+const struct GNUNET_HashCode *
+GM_h2hc (const struct GNUNET_MeshHash *id);
- /**
- * Messages that this client has declared interest in
- */
- struct GNUNET_CONTAINER_MultiHashMap *types;
-
- /**
- * Used to search peers offering a service
- */
- struct GNUNET_DHT_GetHandle *dht_get_type;
+/**
+ * Convert a message type into a string to help debug
+ * Generated with:
+ * FIND: "#define ([^ ]+)[ ]*([0-9]+)"
+ * REPLACE: " case \2: return "\1"; break;"
+ *
+ * @param m Message type.
+ *
+ * @return Human readable string description.
+ */
+const char *
+GM_m2s (uint16_t m);
-#if MESH_DEBUG
- /**
- * ID of the client, for debug messages
- */
- unsigned int id;
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
#endif
-
-};
#endif