From 6e0d1b08237cba5ce900a95f231cb0164ff3606f Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Tue, 1 Oct 2013 11:05:12 +0000 Subject: [PATCH] - too much uncommited work --- src/mesh/gnunet-service-mesh-enc.c | 5831 ++++----------------- src/mesh/gnunet-service-mesh_channel.c | 1252 +++++ src/mesh/gnunet-service-mesh_channel.h | 109 + src/mesh/gnunet-service-mesh_connection.c | 1583 ++++++ src/mesh/gnunet-service-mesh_connection.h | 197 + src/mesh/gnunet-service-mesh_local.c | 1017 ++++ src/mesh/gnunet-service-mesh_local.h | 145 + 7 files changed, 5371 insertions(+), 4763 deletions(-) create mode 100644 src/mesh/gnunet-service-mesh_channel.c create mode 100644 src/mesh/gnunet-service-mesh_channel.h create mode 100644 src/mesh/gnunet-service-mesh_connection.c create mode 100644 src/mesh/gnunet-service-mesh_connection.h create mode 100644 src/mesh/gnunet-service-mesh_local.c create mode 100644 src/mesh/gnunet-service-mesh_local.h diff --git a/src/mesh/gnunet-service-mesh-enc.c b/src/mesh/gnunet-service-mesh-enc.c index bd6179a44..a5670fff3 100644 --- a/src/mesh/gnunet-service-mesh-enc.c +++ b/src/mesh/gnunet-service-mesh-enc.c @@ -44,7 +44,7 @@ */ #include "platform.h" -#include "gnunet_crypto_lib.h" +#include "gnunet_util_lib.h" #include "mesh_enc.h" #include "mesh_protocol_enc.h" #include "mesh_path.h" @@ -52,6 +52,10 @@ #include "gnunet_dht_service.h" #include "gnunet_statistics_service.h" +#include "gnunet-service-mesh_local.h" +#include "gnunet-service-mesh_channel.h" +#include "gnunet-service-mesh_connection.h" + #define MESH_BLOOM_SIZE 128 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\ GNUNET_TIME_UNIT_MINUTES,\ @@ -131,54 +135,6 @@ enum MeshTunnelState }; -/** - * All the states a connection can be in. - */ -enum MeshConnectionState -{ - /** - * Uninitialized status, should never appear in operation. - */ - MESH_CONNECTION_NEW, - - /** - * Connection create message sent, waiting for ACK. - */ - MESH_CONNECTION_SENT, - - /** - * Connection ACK sent, waiting for ACK. - */ - MESH_CONNECTION_ACK, - - /** - * Connection confirmed, ready to carry traffic. - */ - MESH_CONNECTION_READY, -}; - - -/** - * All the states a connection can be in. - */ -enum MeshChannelState -{ - /** - * Uninitialized status, should never appear in operation. - */ - MESH_CHANNEL_NEW, - - /** - * Connection create message sent, waiting for ACK. - */ - MESH_CHANNEL_SENT, - - /** - * Connection confirmed, ready to carry traffic.. - */ - MESH_CHANNEL_READY, -}; - /******************************************************************************/ /************************ DATA STRUCTURES ****************************/ @@ -189,118 +145,7 @@ struct MeshClient; struct MeshPeer; struct MeshTunnel2; struct MeshConnection; -struct MeshChannel; -struct MeshChannelReliability; - - -/** - * Struct containing info about a queued transmission to this peer - */ -struct MeshPeerQueue -{ - /** - * DLL next - */ - struct MeshPeerQueue *next; - - /** - * DLL previous - */ - struct MeshPeerQueue *prev; - - /** - * Peer this transmission is directed to. - */ - struct MeshPeer *peer; - - /** - * Connection this message belongs to. - */ - struct MeshConnection *c; - - /** - * Is FWD in c? - */ - int fwd; - - /** - * Channel this message belongs to, if known. - */ - struct MeshChannel *ch; - - /** - * Pointer to info stucture used as cls. - */ - void *cls; - - /** - * Type of message - */ - uint16_t type; - - /** - * Size of the message - */ - size_t size; -}; - - -/** - * Struct to encapsulate all the Flow Control information to a peer to which - * we are directly connected (on a core level). - */ -struct MeshFlowControl -{ - /** - * Connection this controls. - */ - struct MeshConnection *c; - - /** - * How many messages are in the queue on this connection. - */ - unsigned int queue_n; - - /** - * How many messages do we accept in the queue. - */ - unsigned int queue_max; - - /** - * Next ID to use. - */ - uint32_t next_pid; - - /** - * ID of the last packet sent towards the peer. - */ - uint32_t last_pid_sent; - - /** - * ID of the last packet received from the peer. - */ - uint32_t last_pid_recv; - - /** - * Last ACK sent to the peer (peer can't send more than this PID). - */ - uint32_t last_ack_sent; - - /** - * Last ACK sent towards the origin (for traffic towards leaf node). - */ - uint32_t last_ack_recv; - - /** - * Task to poll the peer in case of a lost ACK causes stall. - */ - GNUNET_SCHEDULER_TaskIdentifier poll_task; - /** - * How frequently to poll for ACKs. - */ - struct GNUNET_TIME_Relative poll_time; -}; /** @@ -365,359 +210,97 @@ struct MeshPeer }; -/** - * Info needed to retry a message in case it gets lost. - */ -struct MeshReliableMessage -{ - /** - * Double linked list, FIFO style - */ - struct MeshReliableMessage *next; - struct MeshReliableMessage *prev; - - /** - * Type of message (payload, channel management). - */ - int16_t type; - - /** - * Tunnel Reliability queue this message is in. - */ - struct MeshChannelReliability *rel; - - /** - * ID of the message (ACK needed to free) - */ - uint32_t mid; - - /** - * When was this message issued (to calculate ACK delay) - */ - struct GNUNET_TIME_Absolute timestamp; - - /* struct GNUNET_MESH_Data with payload */ -}; /** - * Info about the traffic state for a client in a channel. + * Struct used to queue messages in a tunnel. */ -struct MeshChannelReliability +struct MeshTunnelQueue { - /** - * Channel this is about. - */ - struct MeshChannel *ch; - - /** - * DLL of messages sent and not yet ACK'd. - */ - struct MeshReliableMessage *head_sent; - struct MeshReliableMessage *tail_sent; - - /** - * Messages pending to send. - */ - unsigned int n_sent; - - /** - * DLL of messages received out of order. - */ - struct MeshReliableMessage *head_recv; - struct MeshReliableMessage *tail_recv; - - /** - * Messages received. - */ - unsigned int n_recv; - - /** - * Next MID to use for outgoing traffic. - */ - uint32_t mid_send; - - /** - * Next MID expected for incoming traffic. - */ - uint32_t mid_recv; - - /** - * Can we send data to the client? - */ - int client_ready; - - /** - * Task to resend/poll in case no ACK is received. - */ - GNUNET_SCHEDULER_TaskIdentifier retry_task; + /** + * DLL + */ + struct MeshTunnelQueue *next; + struct MeshTunnelQueue *prev; - /** - * Counter for exponential backoff. - */ - struct GNUNET_TIME_Relative retry_timer; + /** + * Channel. + */ + struct MeshChannel *ch; - /** - * How long does it usually take to get an ACK. - */ - struct GNUNET_TIME_Relative expected_delay; + /** + * Message to send. + */ + /* struct GNUNET_MessageHeader *msg; */ }; /** - * Struct containing all information regarding a channel to a remote client. + * Struct containing all information regarding a tunnel to a peer. */ -struct MeshChannel +struct MeshTunnel2 { /** - * Tunnel this channel is in. - */ - struct MeshTunnel2 *t; - - /** - * Double linked list. - */ - struct MeshChannel *next; - struct MeshChannel *prev; - - /** - * Destination port of the channel. - */ - uint32_t port; - - /** - * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI) - */ - MESH_ChannelNumber gid; - - /** - * Local tunnel number for root (owner) client. - * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 ) - */ - MESH_ChannelNumber lid_root; - - /** - * Local tunnel number for local destination clients (incoming number) - * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0). - */ - MESH_ChannelNumber lid_dest; - - /** - * Channel state. - */ - enum MeshChannelState state; - - /** - * Is the tunnel bufferless (minimum latency)? - */ - int nobuffer; - - /** - * Is the tunnel reliable? - */ - int reliable; - - /** - * Last time the channel was used - */ - struct GNUNET_TIME_Absolute timestamp; - - /** - * Client owner of the tunnel, if any - */ - struct MeshClient *root; - - /** - * Client destination of the tunnel, if any. - */ - struct MeshClient *dest; - - /** - * Flag to signal the destruction of the channel. - * If this is set GNUNET_YES the channel will be destroyed - * when the queue is empty. - */ - int destroy; - - /** - * Total messages pending for this channel, payload or not. - */ - unsigned int pending_messages; - - /** - * Reliability data. - * Only present (non-NULL) at the owner of a tunnel. + * Endpoint of the tunnel. */ - struct MeshChannelReliability *root_rel; + struct MeshPeer *peer; /** - * Reliability data. - * Only present (non-NULL) at the destination of a tunnel. + * State of the tunnel. */ - struct MeshChannelReliability *dest_rel; - -}; - + enum MeshTunnelState state; -/** - * Struct containing all information regarding a connection to a peer. - */ -struct MeshConnection -{ /** - * DLL + * Local peer ephemeral private key */ - struct MeshConnection *next; - struct MeshConnection *prev; + struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key; /** - * Tunnel this connection is part of. + * Local peer ephemeral public key */ - struct MeshTunnel2 *t; + struct GNUNET_CRYPTO_EccPublicSignKey *my_eph; /** - * Flow control information for traffic fwd. + * Remote peer's public key. */ - struct MeshFlowControl fwd_fc; + struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph; /** - * Flow control information for traffic bck. + * Encryption ("our") key. */ - struct MeshFlowControl bck_fc; + struct GNUNET_CRYPTO_SymmetricSessionKey e_key; /** - * ID of the connection. + * Decryption ("their") key. */ - struct GNUNET_HashCode id; + struct GNUNET_CRYPTO_SymmetricSessionKey d_key; /** - * State of the connection. + * Paths that are actively used to reach the destination peer. */ - enum MeshConnectionState state; + struct MeshConnection *connection_head; + struct MeshConnection *connection_tail; /** - * Path being used for the tunnel. + * Next connection number. */ - struct MeshPeerPath *path; + uint32_t next_cid; /** - * Position of the local peer in the path. + * Channels inside this tunnel. */ - unsigned int own_pos; + struct MeshChannel *channel_head; + struct MeshChannel *channel_tail; /** - * Task to keep the used paths alive at the owner, - * time tunnel out on all the other peers. + * Channel ID for the next created channel. */ - GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task; + MESH_ChannelNumber next_chid; /** - * Task to keep the used paths alive at the destination, - * time tunnel out on all the other peers. + * Channel ID for the next incoming channel. */ - GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task; - - /** - * Pending message count. - */ - int pending_messages; - - /** - * Destroy flag: if true, destroy on last message. - */ - int destroy; -}; - - -/** - * Struct used to queue messages in a tunnel. - */ -struct MeshTunnelQueue -{ - /** - * DLL - */ - struct MeshTunnelQueue *next; - struct MeshTunnelQueue *prev; - - /** - * Channel. - */ - struct MeshChannel *ch; - - /** - * Message to send. - */ - /* struct GNUNET_MessageHeader *msg; */ -}; - - -/** - * Struct containing all information regarding a tunnel to a peer. - */ -struct MeshTunnel2 -{ - /** - * Endpoint of the tunnel. - */ - struct MeshPeer *peer; - - /** - * State of the tunnel. - */ - enum MeshTunnelState state; - - /** - * Local peer ephemeral private key - */ - struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key; - - /** - * Local peer ephemeral public key - */ - struct GNUNET_CRYPTO_EccPublicSignKey *my_eph; - - /** - * Remote peer's public key. - */ - struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph; - - /** - * Encryption ("our") key. - */ - struct GNUNET_CRYPTO_SymmetricSessionKey e_key; - - /** - * Decryption ("their") key. - */ - struct GNUNET_CRYPTO_SymmetricSessionKey d_key; - - /** - * Paths that are actively used to reach the destination peer. - */ - struct MeshConnection *connection_head; - struct MeshConnection *connection_tail; - - /** - * Next connection number. - */ - uint32_t next_cid; - - /** - * Channels inside this tunnel. - */ - struct MeshChannel *channel_head; - struct MeshChannel *channel_tail; - - /** - * Channel ID for the next created channel. - */ - MESH_ChannelNumber next_chid; - - /** - * Channel ID for the next incoming channel. - */ - MESH_ChannelNumber next_local_chid; + MESH_ChannelNumber next_local_chid; /** * Pending message count. @@ -738,57 +321,6 @@ struct MeshTunnel2 -/** - * Struct containing information about a client of the service - * - * TODO: add a list of 'waiting' ports - */ -struct MeshClient -{ - /** - * Linked list next - */ - struct MeshClient *next; - - /** - * Linked list prev - */ - struct MeshClient *prev; - - /** - * Tunnels that belong to this client, indexed by local id - */ - struct GNUNET_CONTAINER_MultiHashMap32 *own_channels; - - /** - * Tunnels this client has accepted, indexed by incoming local id - */ - struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels; - - /** - * Handle to communicate with the client - */ - struct GNUNET_SERVER_Client *handle; - - /** - * Ports that this client has declared interest in. - * Indexed by port, contains *Client. - */ - struct GNUNET_CONTAINER_MultiHashMap32 *ports; - - /** - * Whether the client is active or shutting down (don't send confirmations - * to a client that is shutting down. - */ - int shutting_down; - - /** - * ID of the client, mainly for debug messages - */ - unsigned int id; -}; - - /******************************************************************************/ /************************ DEBUG FUNCTIONS ****************************/ /******************************************************************************/ @@ -822,11 +354,6 @@ mesh_debug (void *cls, int success) /************************** Configuration parameters **************************/ -/** - * How often to send path keepalives. Paths timeout after 4 missed. - */ -static struct GNUNET_TIME_Relative refresh_connection_time; - /** * How often to PUT own ID in the DHT. */ @@ -847,18 +374,6 @@ static unsigned long long default_ttl; */ static unsigned long long dht_replication_level; -/** - * How many connections are we willing to maintain. - * Local connections are always allowed, even if there are more connections than max. - */ -static unsigned long long max_connections; - -/** - * How many messages *in total* are we willing to queue, divide by number of - * connections to get connection queue size. - */ -static unsigned long long max_msgs_queue; - /** * How many peers do we want to remember? */ @@ -871,21 +386,6 @@ static unsigned long long drop_percent; /*************************** Static global variables **************************/ -/** - * DLL with all the clients, head. - */ -static struct MeshClient *clients_head; - -/** - * DLL with all the clients, tail. - */ -static struct MeshClient *clients_tail; - -/** - * Connections known, indexed by cid (MeshConnection). - */ -static struct GNUNET_CONTAINER_MultiHashMap *connections; - /** * Peers known, indexed by PeerIdentity (MeshPeer). */ @@ -901,21 +401,11 @@ static struct GNUNET_CORE_Handle *core_handle; */ static struct GNUNET_DHT_Handle *dht_handle; -/** - * Handle to server lib. - */ -static struct GNUNET_SERVER_Handle *server_handle; - /** * Handle to the statistics service. */ static struct GNUNET_STATISTICS_Handle *stats; -/** - * Notification context, to send messages to local clients. - */ -static struct GNUNET_SERVER_NotificationContext *nc; - /** * Local peer own ID (memory efficient handle). */ @@ -931,21 +421,11 @@ static struct GNUNET_PeerIdentity my_full_id; */ static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key; -/** - * All ports clients of this peer have opened. - */ -static struct GNUNET_CONTAINER_MultiHashMap32 *ports; - /** * Task to periodically announce itself in the network. */ GNUNET_SCHEDULER_TaskIdentifier announce_id_task; -/** - * Next ID to assign to a client. - */ -unsigned int next_client_id; - /******************************************************************************/ /*********************** DECLARATIONS **************************/ @@ -1020,30 +500,6 @@ static void path_add_to_peers (struct MeshPeerPath *p, int confirmed); -/** - * Search for a tunnel by global ID using full PeerIdentities. - * - * @param t Tunnel containing the channel. - * @param chid Public channel number. - * - * @return channel handler, NULL if doesn't exist - */ -static struct MeshChannel * -channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid); - -/** - * Modify the data message ID from global to local and send to client. - * - * @param ch Channel on which to send the message. - * @param msg Message to modify and send. - * @param fwd Forward? - */ -static void -channel_send_client_data (struct MeshChannel *ch, - const struct GNUNET_MESH_Data *msg, - int fwd); - - /** * Change the tunnel state. * @@ -1106,98 +562,6 @@ tunnel_destroy_empty (struct MeshTunnel2 *t); static void tunnel_destroy (struct MeshTunnel2 *t); -/** - * Create a connection. - * - * @param cid Connection ID. - */ -static struct MeshConnection * -connection_new (const struct GNUNET_HashCode *cid); - -/** - * Connection is no longer needed: destroy it and remove from tunnel. - * - * @param c Connection to destroy. - */ -static void -connection_destroy (struct MeshConnection *c); - -/** - * Send FWD keepalive packets for a connection. - * - * @param cls Closure (connection for which to send the keepalive). - * @param tc Notification context. - */ -static void -connection_fwd_keepalive (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc); - -/** - * Send BCK keepalive packets for a connection. - * - * @param cls Closure (connection for which to send the keepalive). - * @param tc Notification context. - */ -static void -connection_bck_keepalive (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc); - - -/** - * Change the tunnel state. - * - * @param c Connection whose state to change. - * @param state New state. - */ -static void -connection_change_state (struct MeshConnection* c, - enum MeshConnectionState state); - - - -/** - * @brief Queue and pass message to core when possible. - * - * @param cls Closure (@c type dependant). It will be used by queue_send to - * build the message to be sent if not already prebuilt. - * @param type Type of the message, 0 for a raw message. - * @param size Size of the message. - * @param c Connection this message belongs to (cannot be NULL). - * @param ch Channel this message belongs to, if applicable (otherwise NULL). - * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!) - */ -static void -queue_add (void* cls, - uint16_t type, - size_t size, - struct MeshConnection* c, - struct MeshChannel* ch, - int fwd); - - -/** - * Free a transmission that was already queued with all resources - * associated to the request. - * - * @param queue Queue handler to cancel. - * @param clear_cls Is it necessary to free associated cls? - */ -static void -queue_destroy (struct MeshPeerQueue *queue, int clear_cls); - - -/** - * Core callback to write a queued packet to core buffer - * - * @param cls Closure (peer info). - * @param size Number of bytes available in buf. - * @param buf Where the to write the message. - * - * @return number of bytes written to buf - */ -static size_t -queue_send (void *cls, size_t size, void *buf); - /** * Demultiplex by message type and call appropriate handler for a message @@ -1345,242 +709,41 @@ peer2s (const struct MeshPeer *peer) } -/** - * Get the previous hop in a connection - * - * @param c Connection. - * - * @return Previous peer in the connection. - */ -static struct MeshPeer * -connection_get_prev_hop (struct MeshConnection *c) -{ - GNUNET_PEER_Id id; - - if (0 == c->own_pos || c->path->length < 2) - id = c->path->peers[0]; - else - id = c->path->peers[c->own_pos - 1]; - - return peer_get_short (id); -} - /** - * Get the next hop in a connection + * Count established (ready) connections of a tunnel. * - * @param c Connection. + * @param t Tunnel on which to send the message. * - * @return Next peer in the connection. + * @return Number of connections. */ -static struct MeshPeer * -connection_get_next_hop (struct MeshConnection *c) +static unsigned int +tunnel_count_connections (struct MeshTunnel2 *t) { - GNUNET_PEER_Id id; + struct MeshConnection *c; + unsigned int i; - if ((c->path->length - 1) == c->own_pos || c->path->length < 2) - id = c->path->peers[c->path->length - 1]; - else - id = c->path->peers[c->own_pos + 1]; + for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++); - return peer_get_short (id); + return i; } /** - * Get the hop in a connection. + * Pick a connection on which send the next data message. * - * @param c Connection. - * @param fwd Next hop? + * @param t Tunnel on which to send the message. + * @param fwd Is this a fwd message? * - * @return Next peer in the connection. + * @return The connection on which to send the next message. */ -static struct MeshPeer * -connection_get_hop (struct MeshConnection *c, int fwd) +static struct MeshConnection * +tunnel_get_connection (struct MeshTunnel2 *t, int fwd) { - if (fwd) - return connection_get_next_hop (c); - return connection_get_prev_hop (c); -} - -/** - * Check if client has registered with the service and has not disconnected - * - * @param client the client to check - * - * @return non-NULL if client exists in the global DLL - */ -static struct MeshClient * -client_get (struct GNUNET_SERVER_Client *client) -{ - return GNUNET_SERVER_client_get_user_context (client, struct MeshClient); -} - - -/** - * Deletes a tunnel from a client (either owner or destination). - * - * @param c Client whose tunnel to delete. - * @param ch Channel which should be deleted. - */ -static void -client_delete_channel (struct MeshClient *c, struct MeshChannel *ch) -{ - int res; - - if (c == ch->root) - { - res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels, - ch->lid_root, ch); - if (GNUNET_YES != res) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n"); - } - if (c == ch->dest) - { - res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels, - ch->lid_dest, ch); - if (GNUNET_YES != res) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n"); - } -} - - -/** - * Notify the appropriate client that a new incoming channel was created. - * - * @param ch Channel that was created. - */ -static void -send_local_channel_create (struct MeshChannel *ch) -{ - struct GNUNET_MESH_ChannelMessage msg; - struct MeshTunnel2 *t = ch->t; - - if (NULL == ch->dest) - return; - msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE); - msg.channel_id = htonl (ch->lid_dest); - msg.port = htonl (ch->port); - msg.opt = 0; - msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0; - msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0; - msg.opt = htonl (msg.opt); - GNUNET_PEER_resolve (t->peer->id, &msg.peer); - GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle, - &msg.header, GNUNET_NO); -} - - -/** - * Notify a client that the incoming tunnel is no longer valid. - * - * @param ch Channel that is destroyed. - * @param fwd Forward notification (owner->dest)? - */ -static void -send_local_channel_destroy (struct MeshChannel *ch, int fwd) -{ - struct GNUNET_MESH_ChannelMessage msg; - struct MeshClient *c; - - c = fwd ? ch->dest : ch->root; - if (NULL == c) - { - GNUNET_break (0); - return; - } - msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); - msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root); - msg.port = htonl (0); - memset (&msg.peer, 0, sizeof (msg.peer)); - msg.opt = htonl (0); - GNUNET_SERVER_notification_context_unicast (nc, c->handle, - &msg.header, GNUNET_NO); -} - - -/** - * Build a local ACK message and send it to a local client, if needed. - * - * If the client was already allowed to send data, do nothing. - * - * @param ch Channel on which to send the ACK. - * @param c Client to whom send the ACK. - * @param fwd Set to GNUNET_YES for FWD ACK (dest->root) - */ -static void -send_local_ack (struct MeshChannel *ch, int fwd) -{ - struct GNUNET_MESH_LocalAck msg; - struct MeshChannelReliability *rel; - struct MeshClient *c; - - c = fwd ? ch->root : ch->dest; - rel = fwd ? ch->root_rel : ch->dest_rel; - - if (GNUNET_YES == rel->client_ready) - return; /* don't send double ACKs to client */ - - rel->client_ready = GNUNET_YES; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "send local %s ack on %s:%X towards %p\n", - fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c); - - if (NULL == c - || ( fwd && (0 == ch->lid_root || c != ch->root)) - || (!fwd && (0 == ch->lid_dest || c != ch->dest)) ) - { - GNUNET_break (0); - return; - } - msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK); - msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest); - GNUNET_SERVER_notification_context_unicast (nc, - c->handle, - &msg.header, - GNUNET_NO); - -} - - -/** - * Count established (ready) connections of a tunnel. - * - * @param t Tunnel on which to send the message. - * - * @return Number of connections. - */ -static unsigned int -tunnel_count_connections (struct MeshTunnel2 *t) -{ - struct MeshConnection *c; - unsigned int i; - - for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++); - - return i; -} - - -/** - * Pick a connection on which send the next data message. - * - * @param t Tunnel on which to send the message. - * @param fwd Is this a fwd message? - * - * @return The connection on which to send the next message. - */ -static struct MeshConnection * -tunnel_get_connection (struct MeshTunnel2 *t, int fwd) -{ - struct MeshConnection *c; - struct MeshConnection *best; - struct MeshFlowControl *fc; - unsigned int lowest_q; + struct MeshConnection *c; + struct MeshConnection *best; + struct MeshFlowControl *fc; + unsigned int lowest_q; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", peer2s (t->peer)); @@ -1610,120 +773,6 @@ tunnel_get_connection (struct MeshTunnel2 *t, int fwd) } -/** - * Is this peer the first one on the connection? - * - * @param c Connection. - * @param fwd Is this about fwd traffic? - * - * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal. - */ -static int -connection_is_origin (struct MeshConnection *c, int fwd) -{ - if (!fwd && c->path->length - 1 == c->own_pos ) - return GNUNET_YES; - if (fwd && 0 == c->own_pos) - return GNUNET_YES; - return GNUNET_NO; -} - - -/** - * Is this peer the last one on the connection? - * - * @param c Connection. - * @param fwd Is this about fwd traffic? - * Note that the ROOT is the terminal for BCK traffic! - * - * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin. - */ -static int -connection_is_terminal (struct MeshConnection *c, int fwd) -{ - return connection_is_origin (c, !fwd); -} - - -/** - * Is the root client for this channel on this peer? - * - * @param ch Channel. - * @param fwd Is this for fwd traffic? - * - * @return GNUNET_YES in case it is. - */ -static int -channel_is_origin (struct MeshChannel *ch, int fwd) -{ - struct MeshClient *c; - - c = fwd ? ch->root : ch->dest; - return NULL != c; -} - - -/** - * Is the destination client for this channel on this peer? - * - * @param ch Channel. - * @param fwd Is this for fwd traffic? - * - * @return GNUNET_YES in case it is. - */ -static int -channel_is_terminal (struct MeshChannel *ch, int fwd) -{ - struct MeshClient *c; - - c = fwd ? ch->dest : ch->root; - return NULL != c; -} - - -/** - * Get free buffer space towards the client on a specific channel. - * - * @param ch Channel. - * @param fwd Is query about FWD traffic? - * - * @return Free buffer space [0 - 64] - */ -static unsigned int -channel_get_buffer (struct MeshChannel *ch, int fwd) -{ - struct MeshChannelReliability *rel; - - rel = fwd ? ch->dest_rel : ch->root_rel; - - /* If rel is NULL it means that the end is not yet created, - * most probably is a loopback channel at the point of sending - * the ChannelCreate to itself. - */ - if (NULL == rel) - return 64; - - return (64 - rel->n_recv); -} - - -/** - * Get free buffer space in a connection. - * - * @param c Connection. - * @param fwd Is query about FWD traffic? - * - * @return Free buffer space [0 - max_msgs_queue/max_connections] - */ -static unsigned int -connection_get_buffer (struct MeshConnection *c, int fwd) -{ - struct MeshFlowControl *fc; - - fc = fwd ? &c->fwd_fc : &c->bck_fc; - - return (fc->queue_max - fc->queue_n); -} /** @@ -1745,7 +794,7 @@ tunnel_get_buffer (struct MeshTunnel2 *t, int fwd) buffer = 0; /* If terminal, return biggest channel buffer */ - if (NULL == c || connection_is_terminal (c, fwd)) + if (NULL == c || GMC_is_terminal (c, fwd)) { struct MeshChannel *ch; unsigned int ch_buf; @@ -1820,99 +869,6 @@ tunnel_decrypt (struct MeshTunnel2 *t, } -/** - * Sends an already built message on a connection, properly registering - * all used resources. - * - * @param message Message to send. Function makes a copy of it. - * If message is not hop-by-hop, decrements TTL of copy. - * @param c Connection on which this message is transmitted. - * @param ch Channel on which this message is transmitted, or NULL. - * @param fwd Is this a fwd message? - */ -static void -send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message, - struct MeshConnection *c, - struct MeshChannel *ch, - int fwd) -{ - void *data; - size_t size; - uint16_t type; - - size = ntohs (message->size); - data = GNUNET_malloc (size); - memcpy (data, message, size); - type = ntohs (message->type); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n", - GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id)); - - switch (type) - { - struct GNUNET_MESH_Encrypted *emsg; - struct GNUNET_MESH_ACK *amsg; - struct GNUNET_MESH_Poll *pmsg; - struct GNUNET_MESH_ConnectionDestroy *dmsg; - struct GNUNET_MESH_ConnectionBroken *bmsg; - uint32_t ttl; - - case GNUNET_MESSAGE_TYPE_MESH_FWD: - case GNUNET_MESSAGE_TYPE_MESH_BCK: - emsg = (struct GNUNET_MESH_Encrypted *) data; - ttl = ntohl (emsg->ttl); - if (0 == ttl) - { - GNUNET_break_op (0); - return; - } - emsg->cid = c->id; - emsg->ttl = htonl (ttl - 1); - emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid)); - break; - - case GNUNET_MESSAGE_TYPE_MESH_ACK: - amsg = (struct GNUNET_MESH_ACK *) data; - amsg->cid = c->id; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack)); - break; - - case GNUNET_MESSAGE_TYPE_MESH_POLL: - pmsg = (struct GNUNET_MESH_Poll *) data; - pmsg->cid = c->id; - pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid)); - break; - - case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: - dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data; - dmsg->cid = c->id; - dmsg->reserved = 0; - break; - - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN: - bmsg = (struct GNUNET_MESH_ConnectionBroken *) data; - bmsg->cid = c->id; - bmsg->reserved = 0; - break; - - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE: - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK: - break; - - default: - GNUNET_break (0); - } - - queue_add (data, - type, - size, - c, - ch, - fwd); -} - - /** * Sends an already built message on a tunnel, choosing the best connection. * @@ -1959,47 +915,6 @@ send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg, } -/** - * Sends an already built message on a channel, properly registering - * all used resources and encrypting the message with the tunnel's key. - * - * @param message Message to send. Function makes a copy of it. - * @param ch Channel on which this message is transmitted. - * @param fwd Is this a fwd message? - */ -static void -send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message, - struct MeshChannel *ch, - int fwd) -{ - struct GNUNET_MESH_Encrypted *msg; - size_t size = ntohs (message->size); - char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size]; - uint16_t type; - uint64_t iv; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n", - peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", - GNUNET_MESH_DEBUG_M2S (ntohs (message->type))); - - if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid) - { - handle_decrypted (ch->t, message, fwd); - return; - } - - type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK; - iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); - - msg = (struct GNUNET_MESH_Encrypted *) cbuf; - msg->header.type = htons (type); - msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size); - msg->iv = GNUNET_htonll (iv); - tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd); - send_prebuilt_message_tunnel (msg, ch->t, ch, fwd); -} - /** * Sends a CREATE CONNECTION message for a path to a peer. @@ -2422,59 +1337,17 @@ peer_get_best_path (const struct MeshPeer *peer) return best_p; } -static int -queue_is_sendable (struct MeshPeerQueue *q) -{ - struct MeshFlowControl *fc; - - /* Is PID-independent? */ - switch (q->type) - { - case GNUNET_MESSAGE_TYPE_MESH_ACK: - case GNUNET_MESSAGE_TYPE_MESH_POLL: - return GNUNET_YES; - } - - /* Is PID allowed? */ - fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc; - if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent)) - return GNUNET_YES; - - return GNUNET_NO; -} /** - * Get first sendable message. - * - * @param peer The destination peer. + * Try to establish a new connection to this peer in the given tunnel. + * If the peer doesn't have any path to it yet, try to get one. + * If the peer already has some path, send a CREATE CONNECTION towards it. * - * @return Best current known path towards the peer, if any. + * @param peer PeerInfo of the peer. */ -static struct MeshPeerQueue * -peer_get_first_message (const struct MeshPeer *peer) -{ - struct MeshPeerQueue *q; - - for (q = peer->queue_head; NULL != q; q = q->next) - { - if (queue_is_sendable (q)) - return q; - } - - return NULL; -} - - -/** - * Try to establish a new connection to this peer in the given tunnel. - * If the peer doesn't have any path to it yet, try to get one. - * If the peer already has some path, send a CREATE CONNECTION towards it. - * - * @param peer PeerInfo of the peer. - */ -static void -peer_connect (struct MeshPeer *peer) +static void +peer_connect (struct MeshPeer *peer) { struct MeshTunnel2 *t; struct MeshPeerPath *p; @@ -2553,137 +1426,6 @@ peer_connect (struct MeshPeer *peer) } -/** - * Get the first transmittable message for a connection. - * - * @param c Connection. - * @param fwd Is this FWD? - * - * @return First transmittable message. - */ -static struct MeshPeerQueue * -connection_get_first_message (struct MeshConnection *c, int fwd) -{ - struct MeshPeerQueue *q; - struct MeshPeer *p; - - p = connection_get_hop (c, fwd); - - for (q = p->queue_head; NULL != q; q = q->next) - { - if (q->c != c) - continue; - if (queue_is_sendable (q)) - return q; - } - - return NULL; -} - - -/** - * @brief Re-initiate traffic on this connection if necessary. - * - * Check if there is traffic queued towards this peer - * and the core transmit handle is NULL (traffic was stalled). - * If so, call core tmt rdy. - * - * @param c Connection on which initiate traffic. - * @param fwd Is this about fwd traffic? - */ -static void -connection_unlock_queue (struct MeshConnection *c, int fwd) -{ - struct MeshPeer *peer; - struct MeshPeerQueue *q; - size_t size; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connection_unlock_queue %s on %s\n", - fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id)); - - if (connection_is_terminal (c, fwd)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n"); - return; - } - - peer = connection_get_hop (c, fwd); - - if (NULL != peer->core_transmit) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n"); - return; /* Already unlocked */ - } - - q = connection_get_first_message (c, fwd); - if (NULL == q) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n"); - return; /* Nothing to transmit */ - } - - size = q->size; - peer->core_transmit = - GNUNET_CORE_notify_transmit_ready (core_handle, - GNUNET_NO, - 0, - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_PEER_resolve2 (peer->id), - size, - &queue_send, - peer); -} - - -/** - * Cancel all transmissions that belong to a certain connection. - * - * @param c Connection which to cancel. - * @param fwd Cancel fwd traffic? - */ -static void -connection_cancel_queues (struct MeshConnection *c, int fwd) -{ - struct MeshPeerQueue *q; - struct MeshPeerQueue *next; - struct MeshFlowControl *fc; - struct MeshPeer *peer; - - if (NULL == c) - { - GNUNET_break (0); - return; - } - fc = fwd ? &c->fwd_fc : &c->bck_fc; - peer = connection_get_hop (c, fwd); - - for (q = peer->queue_head; NULL != q; q = next) - { - next = q->next; - if (q->c == c) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connection_cancel_queue %s\n", - GNUNET_MESH_DEBUG_M2S (q->type)); - queue_destroy (q, GNUNET_YES); - } - } - if (NULL == peer->queue_head) - { - if (NULL != peer->core_transmit) - { - GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit); - peer->core_transmit = NULL; - } - if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task) - { - GNUNET_SCHEDULER_cancel (fc->poll_task); - fc->poll_task = GNUNET_SCHEDULER_NO_TASK; - } - } -} - /** * Add the path to the peer and update the path used to reach it in case this @@ -2792,45 +1534,6 @@ peer_add_path_to_origin (struct MeshPeer *peer_info, peer_add_path (peer_info, path, trusted); } - - -/** - * Function called if a connection has been stalled for a while, - * possibly due to a missed ACK. Poll the neighbor about its ACK status. - * - * @param cls Closure (poll ctx). - * @param tc TaskContext. - */ -static void -connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct MeshFlowControl *fc = cls; - struct GNUNET_MESH_Poll msg; - struct MeshConnection *c; - - fc->poll_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - { - return; - } - - c = fc->c; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n", - GNUNET_h2s (&c->id)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n", - fc == &c->fwd_fc ? "FWD" : "BCK"); - - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL); - msg.header.size = htons (sizeof (msg)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent); - send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc); - fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time); - fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, - &connection_poll, fc); -} - - /** * Build a PeerPath from the paths returned from the DHT, reversing the paths * to obtain a local peer -> destination path and interning the peer ids. @@ -2944,60 +1647,6 @@ path_add_to_peers (struct MeshPeerPath *p, int confirmed) } -/** - * Search for a channel among the channels for a client - * - * @param c the client whose channels to search in - * @param chid the local id of the channel - * - * @return channel handler, NULL if doesn't exist - */ -static struct MeshChannel * -channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid); - if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)) - { - GNUNET_break_op (0); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid); - return NULL; - } - if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV) - return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid); - return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid); -} - - -static void -channel_debug (struct MeshChannel *ch) -{ - if (NULL == ch) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n"); - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n", - peer2s (ch->t->peer), ch->gid, ch); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n", - ch->root, ch->root_rel); - if (NULL != ch->root) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n", - ch->root_rel->client_ready ? "YES" : "NO"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root); - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n", - ch->dest, ch->dest_rel); - if (NULL != ch->dest) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n", - ch->dest_rel->client_ready ? "YES" : "NO"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest); - } -} - #if 0 static void fc_debug (struct MeshFlowControl *fc) @@ -3030,23 +1679,6 @@ connection_debug (struct MeshConnection *c) #endif -static struct MeshChannel * -channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid) -{ - struct MeshChannel *ch; - - if (NULL == t) - return NULL; - - for (ch = t->channel_head; NULL != ch; ch = ch->next) - { - if (ch->gid == chid) - break; - } - - return ch; -} - /** * Change the tunnel state. @@ -3130,60 +1762,7 @@ tunnel_queue_data (struct MeshTunnel2 *t, } -static void -connection_change_state (struct MeshConnection* c, - enum MeshConnectionState state) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Connection %s state was %s\n", - GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Connection %s state is now %s\n", - GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state)); - c->state = state; -} - - -/** - * Add a client to a channel, initializing all needed data structures. - * - * @param ch Channel to which add the client. - * @param c Client which to add to the channel. - */ -static void -channel_add_client (struct MeshChannel *ch, struct MeshClient *c) -{ - struct MeshTunnel2 *t = ch->t; - - if (NULL != ch->dest) - { - GNUNET_break (0); - return; - } - - /* Assign local id as destination */ - while (NULL != channel_get_by_local_id (c, t->next_local_chid)) - t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; - ch->lid_dest = t->next_local_chid++; - t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; - - /* Store in client's hashmap */ - if (GNUNET_OK != - GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels, - ch->lid_dest, ch, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) - { - GNUNET_break (0); - return; - } - - GNUNET_break (NULL == ch->dest_rel); - ch->dest_rel = GNUNET_new (struct MeshChannelReliability); - ch->dest_rel->ch = ch; - ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME; - ch->dest = c; -} static struct MeshConnection * @@ -3289,2733 +1868,610 @@ tunnel_notify_connection_broken (struct MeshTunnel2* t, } + + /** - * Send an end-to-end ACK message for the most recent in-sequence payload. - * - * If channel is not reliable, do nothing. + * Send an ACK on the appropriate connection/channel, depending on + * the direction and the position of the peer. * - * @param ch Channel this is about. - * @param fwd Is for FWD traffic? (ACK dest->owner) + * @param c Which connection to send the hop-by-hop ACK. + * @param ch Channel, if any. + * @param fwd Is this a fwd ACK? (will go dest->root) */ static void -channel_send_data_ack (struct MeshChannel *ch, int fwd) +send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd) { - struct GNUNET_MESH_DataACK msg; - struct MeshChannelReliability *rel; - struct MeshReliableMessage *copy; - unsigned int delta; - uint64_t mask; - uint16_t type; + unsigned int buffer; - if (GNUNET_NO == ch->reliable) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "send ack %s on %p %p\n", + fwd ? "FWD" : "BCK", c, ch); + if (NULL == c || GMC_is_terminal (c, fwd)) { - return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n"); + buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd); } - rel = fwd ? ch->dest_rel : ch->root_rel; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "send_data_ack for %u\n", - rel->mid_recv - 1); - - type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK; - msg.header.type = htons (type); - msg.header.size = htons (sizeof (msg)); - msg.chid = htonl (ch->gid); - msg.mid = htonl (rel->mid_recv - 1); - msg.futures = 0; - for (copy = rel->head_recv; NULL != copy; copy = copy->next) + else { - if (copy->type != type) - continue; - delta = copy->mid - rel->mid_recv; - if (63 < delta) - break; - mask = 0x1LL << delta; - msg.futures |= mask; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " setting bit for %u (delta %u) (%llX) -> %llX\n", - copy->mid, delta, mask, msg.futures); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n"); + buffer = connection_get_buffer (c, fwd); } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer); - send_prebuilt_message_channel (&msg.header, ch, fwd); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n"); + if ( (NULL != ch && channel_is_origin (ch, fwd)) || + (NULL != c && connection_is_origin (c, fwd)) ) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n"); + if (0 < buffer) + { + GNUNET_assert (NULL != ch); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n"); + send_local_ack (ch, fwd); + } + } + else if (NULL == c) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n"); + GNUNET_assert (NULL != ch); + channel_send_connections_ack (ch, buffer, fwd); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n"); + connection_send_ack (c, buffer, fwd); + } } + + /** - * Send an ACK informing the predecessor about the available buffer space. - * - * Note that for fwd ack, the FWD mean forward *traffic* (root->dest), - * the ACK itself goes "back" (dest->root). + * Confirm we got a channel create. * - * @param c Connection on which to send the ACK. - * @param buffer How much space free to advertise? - * @param fwd Is this FWD ACK? (Going dest->owner) + * @param ch The channel to confirm. + * @param fwd Should we send the ACK fwd? */ static void -connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd) +channel_send_ack (struct MeshChannel *ch, int fwd) { - struct MeshFlowControl *next_fc; - struct MeshFlowControl *prev_fc; - struct GNUNET_MESH_ACK msg; - uint32_t ack; - int delta; - - next_fc = fwd ? &c->fwd_fc : &c->bck_fc; - prev_fc = fwd ? &c->bck_fc : &c->fwd_fc; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "connection send %s ack on %s\n", - fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id)); - - /* Check if we need to transmit the ACK */ - if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " last pid recv: %u, last ack sent: %u\n", - prev_fc->last_pid_recv, prev_fc->last_ack_sent); - return; - } - - /* Ok, ACK might be necessary, what PID to ACK? */ - delta = next_fc->queue_max - next_fc->queue_n; - ack = prev_fc->last_pid_recv + delta; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " last pid %u, last ack %u, qmax %u, q %u\n", - prev_fc->last_pid_recv, prev_fc->last_ack_sent, - next_fc->queue_max, next_fc->queue_n); - if (ack == prev_fc->last_ack_sent) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n"); - return; - } - - prev_fc->last_ack_sent = ack; + struct GNUNET_MESH_ChannelManage msg; - /* Build ACK message and send on connection */ msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK); - msg.ack = htonl (ack); - msg.cid = c->id; + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " sending channel %s ack for channel %s:%X\n", + fwd ? "FWD" : "BCK", peer2s (ch->t->peer), + ch->gid); - send_prebuilt_message_connection (&msg.header, c, NULL, !fwd); + msg.chid = htonl (ch->gid); + send_prebuilt_message_channel (&msg.header, ch, !fwd); } /** - * Modify the mesh message TID from global to local and send to client. - * - * @param ch Channel on which to send the message. - * @param msg Message to modify and send. - * @param c Client to send to. - * @param tid Tunnel ID to use (c can be both owner and client). + * Send a message to all clients (local and remote) of this channel + * notifying that the channel is no longer valid. + * + * If some peer or client should not receive the message, + * should be zero'ed out before calling this function. + * + * @param ch The channel whose clients to notify. */ static void -channel_send_client_to_tid (struct MeshChannel *ch, - const struct GNUNET_MESH_Data *msg, - struct MeshClient *c, MESH_ChannelNumber id) +channel_send_destroy (struct MeshChannel *ch) { - struct GNUNET_MESH_LocalData *copy; - uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data); - char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)]; + struct GNUNET_MESH_ChannelManage msg; + + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " sending channel destroy for channel %s:%X\n", + peer2s (ch->t->peer), + ch->gid); - if (size < sizeof (struct GNUNET_MessageHeader)) + if (channel_is_terminal (ch, GNUNET_NO)) { - GNUNET_break_op (0); - return; + if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down) + { + msg.chid = htonl (ch->lid_root); + send_local_channel_destroy (ch, GNUNET_NO); + } } - if (NULL == c) + else { - GNUNET_break (0); - return; + msg.chid = htonl (ch->gid); + send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO); } - copy = (struct GNUNET_MESH_LocalData *) cbuf; - memcpy (©[1], &msg[1], size); - copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size); - copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA); - copy->id = htonl (id); - GNUNET_SERVER_notification_context_unicast (nc, c->handle, - ©->header, GNUNET_NO); -} - -/** - * We have received a message out of order, or the client is not ready. - * Buffer it until we receive an ACK from the client or the missing - * message from the channel. - * - * @param msg Message to buffer (MUST be of type MESH_DATA). - * @param rel Reliability data to the corresponding direction. - */ -static void -channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg, - struct MeshChannelReliability *rel) -{ - struct MeshReliableMessage *copy; - struct MeshReliableMessage *prev; - uint32_t mid; - uint16_t size; - - size = ntohs (msg->header.size); - mid = ntohl (msg->mid); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid); - - copy = GNUNET_malloc (sizeof (*copy) + size); - copy->mid = mid; - copy->rel = rel; - memcpy (©[1], msg, size); - - rel->n_recv++; - - // FIXME do something better than O(n), although n < 64... - // FIXME start from the end (most messages are the latest ones) - for (prev = rel->head_recv; NULL != prev; prev = prev->next) + if (channel_is_terminal (ch, GNUNET_YES)) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid); - if (GMC_is_pid_bigger (prev->mid, mid)) + if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n"); - GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv, - prev, copy); - return; + msg.chid = htonl (ch->lid_dest); + send_local_channel_destroy (ch, GNUNET_YES); } - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n"); - GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n"); -} - - -static void -channel_send_client_data (struct MeshChannel *ch, - const struct GNUNET_MESH_Data *msg, - int fwd) -{ - if (fwd) - { - if (ch->dest_rel->client_ready) - channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest); - else - channel_rel_add_buffered_data (msg, ch->dest_rel); } else { - if (ch->root_rel->client_ready) - channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root); - else - channel_rel_add_buffered_data (msg, ch->root_rel); + msg.chid = htonl (ch->gid); + send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES); } } /** - * Send a buffered message to the client, for in order delivery or - * as result of client ACK. - * - * @param ch Channel on which to empty the message buffer. - * @param c Client to send to. - * @param rel Reliability structure to corresponding peer. - * If rel == bck_rel, this is FWD data. + * Create a tunnel. */ -static void -channel_send_client_buffered_data (struct MeshChannel *ch, - struct MeshClient *c, - int fwd) +static struct MeshTunnel2 * +tunnel_new (void) { - struct MeshReliableMessage *copy; - struct MeshChannelReliability *rel; + struct MeshTunnel2 *t; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n"); - rel = fwd ? ch->dest_rel : ch->root_rel; - if (GNUNET_NO == rel->client_ready) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n"); - return; - } + t = GNUNET_new (struct MeshTunnel2); + t->next_chid = 0; + t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; +// if (GNUNET_OK != +// GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t, +// GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) +// { +// GNUNET_break (0); +// tunnel_destroy (t); +// return NULL; +// } - copy = rel->head_recv; - /* We never buffer channel management messages */ - if (NULL != copy) - { - if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable) - { - struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1]; +// char salt[] = "salt"; +// GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), +// salt, sizeof (salt), +// &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), +// &my_full_id, sizeof (struct GNUNET_PeerIdentity), +// GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity), +// NULL); +// GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), +// salt, sizeof (salt), +// &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), +// GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity), +// &my_full_id, sizeof (struct GNUNET_PeerIdentity), +// NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " have %u! now expecting %u\n", - copy->mid, rel->mid_recv + 1); - channel_send_client_data (ch, msg, fwd); - rel->n_recv--; - rel->mid_recv++; - GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); - GNUNET_free (copy); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " reliable && don't have %u, next is %u\n", - rel->mid_recv, - copy->mid); - return; - } - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n"); + return t; } /** - * Destroy a reliable message after it has been acknowledged, either by - * direct mid ACK or bitfield. Updates the appropriate data structures and - * timers and frees all memory. - * - * @param copy Message that is no longer needed: remote peer got it. + * Add a connection to a tunnel. + * + * @param t Tunnel. + * @param c Connection. */ static void -rel_message_free (struct MeshReliableMessage *copy) +tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c) { - struct MeshChannelReliability *rel; - struct GNUNET_TIME_Relative time; - - rel = copy->rel; - time = GNUNET_TIME_absolute_get_duration (copy->timestamp); - rel->expected_delay.rel_value_us *= 7; - rel->expected_delay.rel_value_us += time.rel_value_us; - rel->expected_delay.rel_value_us /= 8; - rel->n_sent--; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n", - GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n", - GNUNET_STRINGS_relative_time_to_string (rel->expected_delay, - GNUNET_NO)); - rel->retry_timer = rel->expected_delay; - GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy); - GNUNET_free (copy); + struct MeshConnection *aux; + c->t = t; + for (aux = t->connection_head; aux != NULL; aux = aux->next) + if (aux == c) + return; + GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c); } -/** - * Destroy all reliable messages queued for a channel, - * during a channel destruction. - * Frees the reliability structure itself. - * - * @param rel Reliability data for a channel. - */ + static void -channel_rel_free_all (struct MeshChannelReliability *rel) +tunnel_destroy (struct MeshTunnel2 *t) { - struct MeshReliableMessage *copy; - struct MeshReliableMessage *next; + struct MeshConnection *c; + struct MeshConnection *next; - if (NULL == rel) + if (NULL == t) return; - for (copy = rel->head_recv; NULL != copy; copy = next) - { - next = copy->next; - GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); - GNUNET_free (copy); - } - for (copy = rel->head_sent; NULL != copy; copy = next) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", + peer2s (t->peer)); + +// if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t)) +// GNUNET_break (0); + + for (c = t->connection_head; NULL != c; c = next) { - next = copy->next; - GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy); - GNUNET_free (copy); + next = c->next; + connection_destroy (c); } - if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task) - GNUNET_SCHEDULER_cancel (rel->retry_task); - GNUNET_free (rel); + + GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO); + t->peer->tunnel = NULL; + + GNUNET_free (t); } /** - * Mark future messages as ACK'd. + * Tunnel is empty: destroy it. + * + * Notifies all connections about the destruction. * - * @param rel Reliability data. - * @param msg DataACK message with a bitfield of future ACK'd messages. + * @param t Tunnel to destroy. */ static void -channel_rel_free_sent (struct MeshChannelReliability *rel, - const struct GNUNET_MESH_DataACK *msg) +tunnel_destroy_empty (struct MeshTunnel2 *t) { - struct MeshReliableMessage *copy; - struct MeshReliableMessage *next; - uint64_t bitfield; - uint64_t mask; - uint32_t mid; - uint32_t target; - unsigned int i; + struct MeshConnection *c; - bitfield = msg->futures; - mid = ntohl (msg->mid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "free_sent_reliable %u %llX\n", - mid, bitfield); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " rel %p, head %p\n", - rel, rel->head_sent); - for (i = 0, copy = rel->head_sent; - i < 64 && NULL != copy && 0 != bitfield; - i++) + for (c = t->connection_head; NULL != c; c = c->next) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " trying bit %u (mid %u)\n", - i, mid + i + 1); - mask = 0x1LL << i; - if (0 == (bitfield & mask)) - continue; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n"); - /* Bit was set, clear the bit from the bitfield */ - bitfield &= ~mask; - - /* The i-th bit was set. Do we have that copy? */ - /* Skip copies with mid < target */ - target = mid + i + 1; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target); - while (NULL != copy && GMC_is_pid_bigger (target, copy->mid)) - copy = copy->next; - - /* Did we run out of copies? (previously freed, it's ok) */ - if (NULL == copy) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n"); - return; - } - - /* Did we overshoot the target? (previously freed, it's ok) */ - if (GMC_is_pid_bigger (copy->mid, target)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid); - continue; - } - - /* Now copy->mid == target, free it */ - next = copy->next; - rel_message_free (copy); - copy = next; + if (GNUNET_NO == c->destroy) + connection_send_destroy (c); } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n"); + + if (0 == t->pending_messages) + tunnel_destroy (t); + else + t->destroy = GNUNET_YES; } /** - * We haven't received an ACK after a certain time: restransmit the message. + * Destroy tunnel if empty (no more channels). * - * @param cls Closure (MeshReliableMessage with the message to restransmit) - * @param tc TaskContext. + * @param t Tunnel to destroy if empty. */ static void -channel_retransmit_message (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +tunnel_destroy_if_empty (struct MeshTunnel2 *t) { - struct MeshChannelReliability *rel = cls; - struct MeshReliableMessage *copy; - struct MeshPeerQueue *q; - struct MeshChannel *ch; - struct MeshConnection *c; - struct GNUNET_MESH_Data *payload; - struct MeshPeer *hop; - int fwd; - - rel->retry_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + if (NULL != t->channel_head) return; - ch = rel->ch; - copy = rel->head_sent; - if (NULL == copy) - { - GNUNET_break (0); - return; - } + tunnel_destroy_empty (t); +} + + +/******************************************************************************/ +/**************** MESH NETWORK HANDLER HELPERS ***********************/ +/******************************************************************************/ - /* Search the message to be retransmitted in the outgoing queue. - * Check only the queue for the connection that is going to be used, - * if the message is stuck in some other connection's queue we shouldn't - * act upon it: - * - cancelling it and sending the new one doesn't guarantee it's delivery, - * the old connection could be temporary stalled or the queue happened to - * be long at time of insertion. - * - not sending the new one could cause terrible delays the old connection - * is stalled. - */ - payload = (struct GNUNET_MESH_Data *) ©[1]; - fwd = (rel == ch->root_rel); - c = tunnel_get_connection (ch->t, fwd); - hop = connection_get_hop (c, fwd); - for (q = hop->queue_head; NULL != q; q = q->next) - { - if (ntohs (payload->header.type) == q->type && ch == q->ch) - { - struct GNUNET_MESH_Data *queued_data = q->cls; - if (queued_data->mid == payload->mid) - break; - } - } - /* Message not found in the queue that we are going to use. */ - if (NULL == q) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid); - send_prebuilt_message_channel (&payload->header, ch, fwd); - GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid); - } - rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer); - rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer, - &channel_retransmit_message, - cls); -} + +/******************************************************************************/ +/******************** MESH NETWORK HANDLERS **************************/ +/******************************************************************************/ /** - * Send ACK on one or more connections due to buffer space to the client. - * - * Iterates all connections of the tunnel and sends ACKs appropriately. + * Generic handler for mesh network payload traffic. * - * @param ch Channel which has some free buffer space. - * @param fwd Is this in for FWD traffic? (ACK goes dest->root) + * @param t Tunnel on which we got this message. + * @param message Unencryted data message. + * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; */ static void -channel_send_connections_ack (struct MeshChannel *ch, - unsigned int buffer, - int fwd) +handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd) { - struct MeshTunnel2 *t = ch->t; - struct MeshConnection *c; - struct MeshFlowControl *fc; - uint32_t allowed; - uint32_t to_allow; - uint32_t allow_per_connection; - unsigned int cs; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Channel send connection %s ack on %s:%X\n", - fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid); + struct MeshChannelReliability *rel; + struct MeshChannel *ch; + struct MeshClient *c; + uint32_t mid; + uint16_t type; + size_t size; - /* Count connections, how many messages are already allowed */ - for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next) + /* Check size */ + size = ntohs (msg->header.size); + if (size < + sizeof (struct GNUNET_MESH_Data) + + sizeof (struct GNUNET_MessageHeader)) { - fc = fwd ? &c->fwd_fc : &c->bck_fc; - if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent)) - { - GNUNET_break (0); - continue; - } - allowed += fc->last_ack_sent - fc->last_pid_recv; - cs++; + GNUNET_break (0); + return; } + type = ntohs (msg->header.type); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n", + GNUNET_MESH_DEBUG_M2S (type)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n", + GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type))); - /* Make sure there is no overflow */ - if (allowed > buffer) + /* Check channel */ + ch = channel_get (t, ntohl (msg->chid)); + if (NULL == ch) { - GNUNET_break (0); + GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n", + ntohl (msg->chid)); return; } - /* Authorize connections to send more data */ - to_allow = buffer - allowed; + /* Initialize FWD/BCK data */ + c = fwd ? ch->dest : ch->root; + rel = fwd ? ch->dest_rel : ch->root_rel; - for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next) + if (NULL == c) { - allow_per_connection = to_allow/cs; - to_allow -= allow_per_connection; - cs--; - fc = fwd ? &c->fwd_fc : &c->bck_fc; - if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3) - { - continue; - } - connection_send_ack (c, allow_per_connection, fwd); + GNUNET_break (0); + return; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Channel send connection %s ack on %s:%X\n", - fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid); - GNUNET_break (to_allow == 0); -} - + tunnel_change_state (t, MESH_TUNNEL_READY); -/** - * Send an ACK on the appropriate connection/channel, depending on - * the direction and the position of the peer. - * - * @param c Which connection to send the hop-by-hop ACK. - * @param ch Channel, if any. - * @param fwd Is this a fwd ACK? (will go dest->root) - */ -static void -send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd) -{ - unsigned int buffer; + GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "send ack %s on %p %p\n", - fwd ? "FWD" : "BCK", c, ch); - if (NULL == c || connection_is_terminal (c, fwd)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n"); - buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n"); - buffer = connection_get_buffer (c, fwd); - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer); + mid = ntohl (msg->mid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid); - if ( (NULL != ch && channel_is_origin (ch, fwd)) || - (NULL != c && connection_is_origin (c, fwd)) ) + if (GNUNET_NO == ch->reliable || + ( !GMC_is_pid_bigger (rel->mid_recv, mid) && + GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) ) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n"); - if (0 < buffer) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid); + if (GNUNET_YES == ch->reliable) { - GNUNET_assert (NULL != ch); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n"); - send_local_ack (ch, fwd); + /* Is this the exact next expected messasge? */ + if (mid == rel->mid_recv) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n"); + rel->mid_recv++; + channel_send_client_data (ch, msg, fwd); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n"); + channel_rel_add_buffered_data (msg, rel); + } } - } - else if (NULL == c) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n"); - GNUNET_assert (NULL != ch); - channel_send_connections_ack (ch, buffer, fwd); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n"); - connection_send_ack (c, buffer, fwd); - } -} - - -/** - * Channel was ACK'd by remote peer, mark as ready and cancel retransmission. - * - * @param ch Channel to mark as ready. - * @param fwd Was the CREATE message sent fwd? - */ -static void -channel_confirm (struct MeshChannel *ch, int fwd) -{ - struct MeshChannelReliability *rel; - struct MeshReliableMessage *copy; - struct MeshReliableMessage *next; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " channel confirm %s %s:%X\n", - fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid); - ch->state = MESH_CHANNEL_READY; - - rel = fwd ? ch->root_rel : ch->dest_rel; - for (copy = rel->head_sent; NULL != copy; copy = next) - { - struct GNUNET_MessageHeader *msg; - - next = copy->next; - msg = (struct GNUNET_MessageHeader *) ©[1]; - if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE) + else { - rel_message_free (copy); - /* TODO return? */ + /* Tunnel is unreliable: send to clients directly */ + /* FIXME: accept Out Of Order traffic */ + rel->mid_recv = mid + 1; + channel_send_client_data (ch, msg, fwd); } } - send_ack (NULL, ch, fwd); -} - - -/** - * Save a copy to retransmit in case it gets lost. - * - * Initializes all needed callbacks and timers. - * - * @param ch Channel this message goes on. - * @param msg Message to copy. - * @param fwd Is this fwd traffic? - */ -static void -channel_save_copy (struct MeshChannel *ch, - const struct GNUNET_MessageHeader *msg, - int fwd) -{ - struct MeshChannelReliability *rel; - struct MeshReliableMessage *copy; - uint32_t mid; - uint16_t type; - uint16_t size; - - rel = fwd ? ch->root_rel : ch->dest_rel; - mid = rel->mid_send; - type = ntohs (msg->type); - size = ntohs (msg->size); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid); - copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size); - copy->mid = mid; - copy->timestamp = GNUNET_TIME_absolute_get (); - copy->rel = rel; - copy->type = type; - memcpy (©[1], msg, size); - rel->n_sent++; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent); - GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy); - if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task) - { - rel->retry_timer = - GNUNET_TIME_relative_multiply (rel->expected_delay, - MESH_RETRANSMIT_MARGIN); - rel->retry_task = - GNUNET_SCHEDULER_add_delayed (rel->retry_timer, - &channel_retransmit_message, - rel); - } -} - - -/** - * Send keepalive packets for a connection. - * - * @param c Connection to keep alive.. - * @param fwd Is this a FWD keepalive? (owner -> dest). - */ -static void -connection_keepalive (struct MeshConnection *c, int fwd) -{ - struct GNUNET_MESH_ConnectionKeepAlive *msg; - size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive); - char cbuf[size]; - uint16_t type; - - type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE : - GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "sending %s keepalive for connection %s[%d]\n", - fwd ? "FWD" : "BCK", - peer2s (c->t->peer), - c->id); - - msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf; - msg->header.size = htons (size); - msg->header.type = htons (type); - msg->cid = c->id; - - send_prebuilt_message_connection (&msg->header, c, NULL, fwd); -} - - -/** - * Send CONNECTION_{CREATE/ACK} packets for a connection. - * - * @param c Connection for which to send the message. - * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK. - */ -static void -connection_recreate (struct MeshConnection *c, int fwd) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n"); - if (fwd) - send_connection_create (c); else - send_connection_ack (c, GNUNET_NO); -} - - -/** - * Generic connection timer management. - * Depending on the role of the peer in the connection will send the - * appropriate message (build or keepalive) - * - * @param c Conncetion to maintain. - * @param fwd Is FWD? - */ -static void -connection_maintain (struct MeshConnection *c, int fwd) -{ - if (MESH_TUNNEL_SEARCHING == c->t->state) - { - /* TODO DHT GET with RO_BART */ - return; - } - switch (c->state) { - case MESH_CONNECTION_NEW: - GNUNET_break (0); - case MESH_CONNECTION_SENT: - connection_recreate (c, fwd); - break; - case MESH_CONNECTION_READY: - connection_keepalive (c, fwd); - break; - default: - break; + GNUNET_break_op (0); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " MID %u not expected (%u - %u), dropping!\n", + mid, rel->mid_recv, rel->mid_recv + 64); } -} - - -static void -connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct MeshConnection *c = cls; - - c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - - connection_maintain (c, GNUNET_YES); - c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, - &connection_fwd_keepalive, - c); -} - - -static void -connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct MeshConnection *c = cls; - - c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - - connection_maintain (c, GNUNET_NO); - c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, - &connection_bck_keepalive, - c); -} - - -/** - * Send a message to all peers in this connection that the connection - * is no longer valid. - * - * If some peer should not receive the message, it should be zero'ed out - * before calling this function. - * - * @param c The connection whose peers to notify. - */ -static void -connection_send_destroy (struct MeshConnection *c) -{ - struct GNUNET_MESH_ConnectionDestroy msg; - - msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);; - msg.cid = c->id; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " sending connection destroy for connection %s[%X]\n", - peer2s (c->t->peer), - c->id); - - if (GNUNET_NO == connection_is_terminal (c, GNUNET_YES)) - send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES); - if (GNUNET_NO == connection_is_terminal (c, GNUNET_NO)) - send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO); - c->destroy = GNUNET_YES; -} - - -/** - * Confirm we got a channel create. - * - * @param ch The channel to confirm. - * @param fwd Should we send the ACK fwd? - */ -static void -channel_send_ack (struct MeshChannel *ch, int fwd) -{ - struct GNUNET_MESH_ChannelManage msg; - - msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " sending channel %s ack for channel %s:%X\n", - fwd ? "FWD" : "BCK", peer2s (ch->t->peer), - ch->gid); - msg.chid = htonl (ch->gid); - send_prebuilt_message_channel (&msg.header, ch, !fwd); + channel_send_data_ack (ch, fwd); } - /** - * Send a message to all clients (local and remote) of this channel - * notifying that the channel is no longer valid. - * - * If some peer or client should not receive the message, - * should be zero'ed out before calling this function. + * Handler for mesh network traffic end-to-end ACKs. * - * @param ch The channel whose clients to notify. + * @param t Tunnel on which we got this message. + * @param message Data message. + * @param fwd Is this a fwd ACK? (dest->orig) */ static void -channel_send_destroy (struct MeshChannel *ch) -{ - struct GNUNET_MESH_ChannelManage msg; - - msg.header.size = htons (sizeof (msg)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " sending channel destroy for channel %s:%X\n", - peer2s (ch->t->peer), - ch->gid); - - if (channel_is_terminal (ch, GNUNET_NO)) - { - if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down) - { - msg.chid = htonl (ch->lid_root); - send_local_channel_destroy (ch, GNUNET_NO); - } - } - else - { - msg.chid = htonl (ch->gid); - send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO); - } - - if (channel_is_terminal (ch, GNUNET_YES)) - { - if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down) - { - msg.chid = htonl (ch->lid_dest); - send_local_channel_destroy (ch, GNUNET_YES); - } - } - else - { - msg.chid = htonl (ch->gid); - send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES); - } -} - - -/** - * Create a tunnel. - */ -static struct MeshTunnel2 * -tunnel_new (void) -{ - struct MeshTunnel2 *t; - - t = GNUNET_new (struct MeshTunnel2); - t->next_chid = 0; - t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; -// if (GNUNET_OK != -// GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t, -// GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) -// { -// GNUNET_break (0); -// tunnel_destroy (t); -// return NULL; -// } - -// char salt[] = "salt"; -// GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), -// salt, sizeof (salt), -// &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), -// &my_full_id, sizeof (struct GNUNET_PeerIdentity), -// GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity), -// NULL); -// GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), -// salt, sizeof (salt), -// &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey), -// GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity), -// &my_full_id, sizeof (struct GNUNET_PeerIdentity), -// NULL); - - return t; -} - - -/** - * Add a connection to a tunnel. - * - * @param t Tunnel. - * @param c Connection. - */ -static void -tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c) -{ - struct MeshConnection *aux; - c->t = t; - for (aux = t->connection_head; aux != NULL; aux = aux->next) - if (aux == c) - return; - GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c); -} - - -/** - * Initialize a Flow Control structure to the initial state. - * - * @param fc Flow Control structure to initialize. - */ -static void -fc_init (struct MeshFlowControl *fc) -{ - fc->next_pid = 0; - fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */ - fc->last_pid_recv = (uint32_t) -1; - fc->last_ack_sent = (uint32_t) 0; - fc->last_ack_recv = (uint32_t) 0; - fc->poll_task = GNUNET_SCHEDULER_NO_TASK; - fc->poll_time = GNUNET_TIME_UNIT_SECONDS; - fc->queue_n = 0; - fc->queue_max = (max_msgs_queue / max_connections) + 1; -} - - -static struct MeshConnection * -connection_new (const struct GNUNET_HashCode *cid) -{ - struct MeshConnection *c; - - c = GNUNET_new (struct MeshConnection); - c->id = *cid; - GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); - fc_init (&c->fwd_fc); - fc_init (&c->bck_fc); - c->fwd_fc.c = c; - c->bck_fc.c = c; - - return c; -} - - -/** - * Find a connection. - * - * @param cid Connection ID. - */ -static struct MeshConnection * -connection_get (const struct GNUNET_HashCode *cid) -{ - return GNUNET_CONTAINER_multihashmap_get (connections, cid); -} - - -static void -connection_destroy (struct MeshConnection *c) -{ - struct MeshPeer *peer; - - if (NULL == c) - return; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n", - peer2s (c->t->peer), - c->id); - - /* Cancel all traffic */ - connection_cancel_queues (c, GNUNET_YES); - connection_cancel_queues (c, GNUNET_NO); - - /* Cancel maintainance task (keepalive/timeout) */ - if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task) - GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task); - if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task) - GNUNET_SCHEDULER_cancel (c->bck_maintenance_task); - - /* Deregister from neighbors */ - peer = connection_get_next_hop (c); - if (NULL != peer && NULL != peer->connections) - GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c); - peer = connection_get_prev_hop (c); - if (NULL != peer && NULL != peer->connections) - GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c); - - /* Delete */ - GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO); - GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c); - GNUNET_free (c); -} - - -static void -tunnel_destroy (struct MeshTunnel2 *t) -{ - struct MeshConnection *c; - struct MeshConnection *next; - - if (NULL == t) - return; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", - peer2s (t->peer)); - -// if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t)) -// GNUNET_break (0); - - for (c = t->connection_head; NULL != c; c = next) - { - next = c->next; - connection_destroy (c); - } - - GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO); - t->peer->tunnel = NULL; - - GNUNET_free (t); -} - - -/** - * Tunnel is empty: destroy it. - * - * Notifies all connections about the destruction. - * - * @param t Tunnel to destroy. - */ -static void -tunnel_destroy_empty (struct MeshTunnel2 *t) -{ - struct MeshConnection *c; - - for (c = t->connection_head; NULL != c; c = c->next) - { - if (GNUNET_NO == c->destroy) - connection_send_destroy (c); - } - - if (0 == t->pending_messages) - tunnel_destroy (t); - else - t->destroy = GNUNET_YES; -} - - -/** - * Destroy tunnel if empty (no more channels). - * - * @param t Tunnel to destroy if empty. - */ -static void -tunnel_destroy_if_empty (struct MeshTunnel2 *t) -{ - if (NULL != t->channel_head) - return; - - tunnel_destroy_empty (t); -} - - -/** - * Destroy a channel and free all resources. - * - * @param ch Channel to destroy. - */ -static void -channel_destroy (struct MeshChannel *ch) -{ - struct MeshClient *c; - - if (NULL == ch) - return; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n", - peer2s (ch->t->peer), ch->gid); - channel_debug (ch); - - c = ch->root; - if (NULL != c) - { - if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels, - ch->lid_root, ch)) - { - GNUNET_break (0); - } - } - - c = ch->dest; - if (NULL != c) - { - if (GNUNET_YES != - GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels, - ch->lid_dest, ch)) - { - GNUNET_break (0); - } - } - - channel_rel_free_all (ch->root_rel); - channel_rel_free_all (ch->dest_rel); - - GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch); - GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO); - - GNUNET_free (ch); -} - -/** - * Create a new channel. - * - * @param t Tunnel this channel is in. - * @param owner Client that owns the channel, NULL for foreign channels. - * @param lid_root Local ID for root client. - * - * @return A new initialized channel. NULL on error. - */ -static struct MeshChannel * -channel_new (struct MeshTunnel2 *t, - struct MeshClient *owner, MESH_ChannelNumber lid_root) -{ - struct MeshChannel *ch; - - ch = GNUNET_new (struct MeshChannel); - ch->root = owner; - ch->lid_root = lid_root; - ch->t = t; - - GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO); - - if (NULL != owner) - { - while (NULL != channel_get (t, t->next_chid)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n", - t->next_chid, channel_get (t, t->next_chid)); - t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI; - } - ch->gid = t->next_chid; - t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI; - - if (GNUNET_OK != - GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) - { - GNUNET_break (0); - channel_destroy (ch); - GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR); - return NULL; - } - } - GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch); - - return ch; -} - - -/** - * Set options in a channel, extracted from a bit flag field - * - * @param ch Channel to set options to. - * @param options Bit array in host byte order. - */ -static void -channel_set_options (struct MeshChannel *ch, uint32_t options) -{ - ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ? - GNUNET_YES : GNUNET_NO; - ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ? - GNUNET_YES : GNUNET_NO; -} - - -/** - * Iterator for deleting each channel whose client endpoint disconnected. - * - * @param cls Closure (client that has disconnected). - * @param key The local channel id (used to access the hashmap). - * @param value The value stored at the key (channel to destroy). - * - * @return GNUNET_OK, keep iterating. - */ -static int -channel_destroy_iterator (void *cls, - uint32_t key, - void *value) -{ - struct MeshChannel *ch = value; - struct MeshClient *c = cls; - struct MeshTunnel2 *t; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " Channel %X (%X / %X) destroy, due to client %u shutdown.\n", - ch->gid, ch->lid_root, ch->lid_dest, c->id); - channel_debug (ch); - - if (c == ch->dest) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id); - } - if (c == ch->root) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id); - } - - t = ch->t; - channel_send_destroy (ch); - channel_destroy (ch); - tunnel_destroy_if_empty (t); - - return GNUNET_OK; -} - - -/** - * Remove client's ports from the global hashmap on disconnect. - * - * @param cls Closure (unused). - * @param key Port. - * @param value Client structure. - * - * @return GNUNET_OK, keep iterating. - */ -static int -client_release_ports (void *cls, - uint32_t key, - void *value) -{ - int res; - - res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value); - if (GNUNET_YES != res) - { - GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Port %u by client %p was not registered.\n", - key, value); - } - return GNUNET_OK; -} - - -/** - * Timeout function due to lack of keepalive/traffic from the owner. - * Destroys connection if called. - * - * @param cls Closure (connection to destroy). - * @param tc TaskContext. - */ -static void -connection_fwd_timeout (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct MeshConnection *c = cls; - - c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Connection %s[%X] FWD timed out. Destroying.\n", - peer2s (c->t->peer), - c->id); - - if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */ - return; - - connection_destroy (c); -} - - -/** - * Timeout function due to lack of keepalive/traffic from the destination. - * Destroys connection if called. - * - * @param cls Closure (connection to destroy). - * @param tc TaskContext - */ -static void -connection_bck_timeout (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct MeshConnection *c = cls; - - c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK; - if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - return; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Connection %s[%X] FWD timed out. Destroying.\n", - peer2s (c->t->peer), - c->id); - - if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */ - return; - - connection_destroy (c); -} - - -/** - * Resets the connection timeout task, some other message has done the - * task's job. - * - For the first peer on the direction this means to send - * a keepalive or a path confirmation message (either create or ACK). - * - For all other peers, this means to destroy the connection, - * due to lack of activity. - * Starts the tiemout if no timeout was running (connection just created). - * - * @param c Connection whose timeout to reset. - * @param fwd Is this forward? - * - * TODO use heap to improve efficiency of scheduler. - */ -static void -connection_reset_timeout (struct MeshConnection *c, int fwd) -{ - GNUNET_SCHEDULER_TaskIdentifier *ti; - GNUNET_SCHEDULER_Task f; - - ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task; - - if (GNUNET_SCHEDULER_NO_TASK != *ti) - GNUNET_SCHEDULER_cancel (*ti); - - if (connection_is_origin (c, fwd)) /* Endpoint */ - { - f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive; - *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c); - } - else /* Relay */ - { - struct GNUNET_TIME_Relative delay; - - delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4); - f = fwd ? &connection_fwd_timeout : &connection_bck_timeout; - *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c); - } -} - - -/** - * Iterator to notify all connections of a broken link. Mark connections - * to destroy after all traffic has been sent. - * - * @param cls Closure (peer disconnected). - * @param key Current key code (tid). - * @param value Value in the hash map (connection). - * - * @return GNUNET_YES if we should continue to iterate, - * GNUNET_NO if not. - */ -static int -connection_broken (void *cls, - const struct GNUNET_HashCode *key, - void *value) -{ - struct MeshPeer *peer = cls; - struct MeshConnection *c = value; - struct GNUNET_MESH_ConnectionBroken msg; - int fwd; - - fwd = peer == connection_get_prev_hop (c); - - connection_cancel_queues (c, !fwd); - if (connection_is_terminal (c, fwd)) - { - /* Local shutdown, no one to notify about this. */ - connection_destroy (c); - return GNUNET_YES; - } - - msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN); - msg.cid = c->id; - msg.peer1 = my_full_id; - msg.peer2 = *GNUNET_PEER_resolve2 (peer->id); - send_prebuilt_message_connection (&msg.header, c, NULL, fwd); - c->destroy = GNUNET_YES; - - return GNUNET_YES; -} - -/******************************************************************************/ -/**************** MESH NETWORK HANDLER HELPERS ***********************/ -/******************************************************************************/ - -/** - * Free a transmission that was already queued with all resources - * associated to the request. - * - * @param queue Queue handler to cancel. - * @param clear_cls Is it necessary to free associated cls? - */ -static void -queue_destroy (struct MeshPeerQueue *queue, int clear_cls) -{ - struct MeshPeer *peer; - struct MeshFlowControl *fc; - int fwd; - - fwd = queue->fwd; - peer = queue->peer; - GNUNET_assert (NULL != queue->c); - fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc; - - if (GNUNET_YES == clear_cls) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n", - GNUNET_MESH_DEBUG_M2S (queue->type)); - switch (queue->type) - { - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY: - case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n"); - GNUNET_break (GNUNET_YES == queue->c->destroy); - /* fall through */ - case GNUNET_MESSAGE_TYPE_MESH_FWD: - case GNUNET_MESSAGE_TYPE_MESH_BCK: - case GNUNET_MESSAGE_TYPE_MESH_ACK: - case GNUNET_MESSAGE_TYPE_MESH_POLL: - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK: - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE: - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");; - GNUNET_free_non_null (queue->cls); - break; - - default: - GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n", - GNUNET_MESH_DEBUG_M2S (queue->type)); - } - - } - GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue); - - if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK && - queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n); - fc->queue_n--; - peer->queue_n--; - } - if (NULL != queue->c) - { - queue->c->pending_messages--; - if (NULL != queue->c->t) - { - queue->c->t->pending_messages--; - } - } - - GNUNET_free (queue); -} - - -static size_t -queue_send (void *cls, size_t size, void *buf) -{ - struct MeshPeer *peer = cls; - struct MeshFlowControl *fc; - struct MeshConnection *c; - struct GNUNET_MessageHeader *msg; - struct MeshPeerQueue *queue; - struct MeshTunnel2 *t; - struct MeshChannel *ch; - const struct GNUNET_PeerIdentity *dst_id; - size_t data_size; - uint32_t pid; - uint16_t type; - int fwd; - - peer->core_transmit = NULL; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size); - - if (NULL == buf || 0 == size) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n"); - return 0; - } - - /* Initialize */ - queue = peer_get_first_message (peer); - if (NULL == queue) - { - GNUNET_break (0); /* Core tmt_rdy should've been canceled */ - return 0; - } - c = queue->c; - fwd = queue->fwd; - fc = fwd ? &c->fwd_fc : &c->bck_fc; - - dst_id = GNUNET_PEER_resolve2 (peer->id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id)); - /* Check if buffer size is enough for the message */ - if (queue->size > size) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n"); - peer->core_transmit = - GNUNET_CORE_notify_transmit_ready (core_handle, - GNUNET_NO, - 0, - GNUNET_TIME_UNIT_FOREVER_REL, - dst_id, - queue->size, - &queue_send, - peer); - return 0; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size); - - t = (NULL != c) ? c->t : NULL; - type = 0; - - /* Fill buf */ - switch (queue->type) - { - case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY: - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN: - case GNUNET_MESSAGE_TYPE_MESH_FWD: - case GNUNET_MESSAGE_TYPE_MESH_BCK: - case GNUNET_MESSAGE_TYPE_MESH_ACK: - case GNUNET_MESSAGE_TYPE_MESH_POLL: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "* raw: %s\n", - GNUNET_MESH_DEBUG_M2S (queue->type)); - data_size = send_core_data_raw (queue->cls, size, buf); - msg = (struct GNUNET_MessageHeader *) buf; - type = ntohs (msg->type); - break; - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n"); - if (connection_is_origin (c, GNUNET_YES)) - data_size = send_core_connection_create (queue->c, size, buf); - else - data_size = send_core_data_raw (queue->cls, size, buf); - break; - case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n"); - if (connection_is_origin (c, GNUNET_NO) || - connection_is_origin (c, GNUNET_YES)) - data_size = send_core_connection_ack (queue->c, size, buf); - else - data_size = send_core_data_raw (queue->cls, size, buf); - break; - case GNUNET_MESSAGE_TYPE_MESH_DATA: - case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE: - case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: - /* This should be encapsulted */ - GNUNET_break (0); - data_size = 0; - break; - default: - GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", - queue->type); - data_size = 0; - } - - if (0 < drop_percent && - GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Dropping message of type %s\n", - GNUNET_MESH_DEBUG_M2S (queue->type)); - data_size = 0; - } - - /* Free queue, but cls was freed by send_core_* */ - ch = queue->ch; - queue_destroy (queue, GNUNET_NO); - - /* Send ACK if needed, after accounting for sent ID in fc->queue_n */ - switch (type) - { - case GNUNET_MESSAGE_TYPE_MESH_FWD: - case GNUNET_MESSAGE_TYPE_MESH_BCK: - pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid ); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid); - fc->last_pid_sent = pid; - send_ack (c, ch, fwd); - break; - default: - break; - } - - /* If more data in queue, send next */ - queue = peer_get_first_message (peer); - if (NULL != queue) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n"); - if (NULL == peer->core_transmit) { - peer->core_transmit = - GNUNET_CORE_notify_transmit_ready(core_handle, - 0, - 0, - GNUNET_TIME_UNIT_FOREVER_REL, - dst_id, - queue->size, - &queue_send, - peer); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "* tmt rdy called somewhere else\n"); - } - if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n"); - fc->poll_task = - GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc); - } - } - else - { - if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task) - { - GNUNET_SCHEDULER_cancel (fc->poll_task); - fc->poll_task = GNUNET_SCHEDULER_NO_TASK; - } - } - if (NULL != c) - { - c->pending_messages--; - if (GNUNET_YES == c->destroy && 0 == c->pending_messages) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n"); - connection_destroy (c); - } - } - - if (NULL != t) - { - t->pending_messages--; - if (GNUNET_YES == t->destroy && 0 == t->pending_messages) - { -// GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n"); - tunnel_destroy (t); - } - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size); - return data_size; -} - - -static void -queue_add (void *cls, uint16_t type, size_t size, - struct MeshConnection *c, - struct MeshChannel *ch, - int fwd) -{ - struct MeshPeerQueue *queue; - struct MeshFlowControl *fc; - struct MeshPeer *peer; - int priority; - int call_core; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "queue add %s %s (%u) on c %p, ch %p\n", - fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch); - GNUNET_assert (NULL != c); - - fc = fwd ? &c->fwd_fc : &c->bck_fc; - peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c); - - if (NULL == fc) - { - GNUNET_break (0); - return; - } - - if (NULL == peer->connections) - { - /* We are not connected to this peer, ignore request. */ - GNUNET_break_op (0); - return; - } - - priority = 0; - - if (GNUNET_MESSAGE_TYPE_MESH_POLL == type || - GNUNET_MESSAGE_TYPE_MESH_ACK == type) - { - priority = 100; - } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc); - if (fc->queue_n >= fc->queue_max && 0 == priority) - { - GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)", - 1, GNUNET_NO); - GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "queue full: %u/%u\n", - fc->queue_n, fc->queue_max); - return; /* Drop this message */ - } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv); - if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv)) - { - call_core = GNUNET_NO; - if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && - GNUNET_MESSAGE_TYPE_MESH_POLL != type) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "no buffer space (%u > %u): starting poll\n", - fc->last_pid_sent + 1, fc->last_ack_recv); - fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, - &connection_poll, - fc); - } - } - else - call_core = GNUNET_YES; - queue = GNUNET_malloc (sizeof (struct MeshPeerQueue)); - queue->cls = cls; - queue->type = type; - queue->size = size; - queue->peer = peer; - queue->c = c; - queue->ch = ch; - queue->fwd = fwd; - if (100 <= priority) - { - struct MeshPeerQueue *copy; - struct MeshPeerQueue *next; - - for (copy = peer->queue_head; NULL != copy; copy = next) - { - next = copy->next; - if (copy->type == type && copy->c == c && copy->fwd == fwd) - { - /* Example: also a FWD ACK for connection XYZ */ - queue_destroy (copy, GNUNET_YES); - } - } - GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue); - } - else - { - GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n); - fc->queue_n++; - peer->queue_n++; - } - - if (NULL == peer->core_transmit && GNUNET_YES == call_core) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "calling core tmt rdy towards %s for %u bytes\n", - peer2s (peer), size); - peer->core_transmit = - GNUNET_CORE_notify_transmit_ready (core_handle, - 0, - 0, - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_PEER_resolve2 (peer->id), - size, - &queue_send, - peer); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "core tmt rdy towards %s already called\n", - peer2s (peer)); - - } - c->pending_messages++; - if (NULL != c->t) - c->t->pending_messages++; -} - - -/******************************************************************************/ -/******************** MESH NETWORK HANDLERS **************************/ -/******************************************************************************/ - - -/** - * Generic handler for mesh network payload traffic. - * - * @param t Tunnel on which we got this message. - * @param message Unencryted data message. - * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; - */ -static void -handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd) -{ - struct MeshChannelReliability *rel; - struct MeshChannel *ch; - struct MeshClient *c; - uint32_t mid; - uint16_t type; - size_t size; - - /* Check size */ - size = ntohs (msg->header.size); - if (size < - sizeof (struct GNUNET_MESH_Data) + - sizeof (struct GNUNET_MessageHeader)) - { - GNUNET_break (0); - return; - } - type = ntohs (msg->header.type); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n", - GNUNET_MESH_DEBUG_M2S (type)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n", - GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type))); - - /* Check channel */ - ch = channel_get (t, ntohl (msg->chid)); - if (NULL == ch) - { - GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n", - ntohl (msg->chid)); - return; - } - - /* Initialize FWD/BCK data */ - c = fwd ? ch->dest : ch->root; - rel = fwd ? ch->dest_rel : ch->root_rel; - - if (NULL == c) - { - GNUNET_break (0); - return; - } - - tunnel_change_state (t, MESH_TUNNEL_READY); - - GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO); - - mid = ntohl (msg->mid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid); - - if (GNUNET_NO == ch->reliable || - ( !GMC_is_pid_bigger (rel->mid_recv, mid) && - GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid); - if (GNUNET_YES == ch->reliable) - { - /* Is this the exact next expected messasge? */ - if (mid == rel->mid_recv) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n"); - rel->mid_recv++; - channel_send_client_data (ch, msg, fwd); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n"); - channel_rel_add_buffered_data (msg, rel); - } - } - else - { - /* Tunnel is unreliable: send to clients directly */ - /* FIXME: accept Out Of Order traffic */ - rel->mid_recv = mid + 1; - channel_send_client_data (ch, msg, fwd); - } - } - else - { - GNUNET_break_op (0); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " MID %u not expected (%u - %u), dropping!\n", - mid, rel->mid_recv, rel->mid_recv + 64); - } - - channel_send_data_ack (ch, fwd); -} - -/** - * Handler for mesh network traffic end-to-end ACKs. - * - * @param t Tunnel on which we got this message. - * @param message Data message. - * @param fwd Is this a fwd ACK? (dest->orig) - */ -static void -handle_data_ack (struct MeshTunnel2 *t, - const struct GNUNET_MESH_DataACK *msg, int fwd) -{ - struct MeshChannelReliability *rel; - struct MeshReliableMessage *copy; - struct MeshReliableMessage *next; - struct MeshChannel *ch; - uint32_t ack; - uint16_t type; - int work; - - type = ntohs (msg->header.type); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n", - GNUNET_MESH_DEBUG_M2S (type)); - ch = channel_get (t, ntohl (msg->chid)); - if (NULL == ch) - { - GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO); - return; - } - ack = ntohl (msg->mid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n", - (GNUNET_YES == fwd) ? "FWD" : "BCK", ack); - - if (GNUNET_YES == fwd) - { - rel = ch->root_rel; - } - else - { - rel = ch->dest_rel; - } - if (NULL == rel) - { - GNUNET_break (0); - return; - } - - for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next) - { - if (GMC_is_pid_bigger (copy->mid, ack)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid); - channel_rel_free_sent (rel, msg); - break; - } - work = GNUNET_YES; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid); - next = copy->next; - rel_message_free (copy); - } - /* ACK client if needed */ -// channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type); - - /* If some message was free'd, update the retransmission delay*/ - if (GNUNET_YES == work) - { - if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task) - { - GNUNET_SCHEDULER_cancel (rel->retry_task); - if (NULL == rel->head_sent) - { - rel->retry_task = GNUNET_SCHEDULER_NO_TASK; - } - else - { - struct GNUNET_TIME_Absolute new_target; - struct GNUNET_TIME_Relative delay; - - delay = GNUNET_TIME_relative_multiply (rel->retry_timer, - MESH_RETRANSMIT_MARGIN); - new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp, - delay); - delay = GNUNET_TIME_absolute_get_remaining (new_target); - rel->retry_task = - GNUNET_SCHEDULER_add_delayed (delay, - &channel_retransmit_message, - rel); - } - } - else - GNUNET_break (0); - } -} - - -/** - * Core handler for connection creation. - * - * @param cls Closure (unused). - * @param peer Sender (neighbor). - * @param message Message. - * - * @return GNUNET_OK to keep the connection open, - * GNUNET_SYSERR to close it (signal serious error) - */ -static int -handle_mesh_connection_create (void *cls, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) -{ - struct GNUNET_MESH_ConnectionCreate *msg; - struct GNUNET_PeerIdentity *id; - struct GNUNET_HashCode *cid; - struct MeshPeerPath *path; - struct MeshPeer *dest_peer; - struct MeshPeer *orig_peer; - struct MeshConnection *c; - unsigned int own_pos; - uint16_t size; - uint16_t i; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n"); - - /* Check size */ - size = ntohs (message->size); - if (size < sizeof (struct GNUNET_MESH_ConnectionCreate)) - { - GNUNET_break_op (0); - return GNUNET_OK; - } - - /* Calculate hops */ - size -= sizeof (struct GNUNET_MESH_ConnectionCreate); - if (size % sizeof (struct GNUNET_PeerIdentity)) - { - GNUNET_break_op (0); - return GNUNET_OK; - } - size /= sizeof (struct GNUNET_PeerIdentity); - if (1 > size) - { - GNUNET_break_op (0); - return GNUNET_OK; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size); - - /* Get parameters */ - msg = (struct GNUNET_MESH_ConnectionCreate *) message; - cid = &msg->cid; - id = (struct GNUNET_PeerIdentity *) &msg[1]; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " connection %s (%s).\n", - GNUNET_h2s (cid), GNUNET_i2s (id)); - - /* Create connection */ - c = connection_get (cid); - if (NULL == c) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n"); - c = connection_new (cid); - if (NULL == c) - return GNUNET_OK; - connection_reset_timeout (c, GNUNET_YES); - - /* Create path */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n"); - path = path_new (size); - own_pos = 0; - for (i = 0; i < size; i++) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n", - GNUNET_i2s (&id[i])); - path->peers[i] = GNUNET_PEER_intern (&id[i]); - if (path->peers[i] == myid) - own_pos = i; - } - if (own_pos == 0 && path->peers[own_pos] != myid) - { - /* create path: self not found in path through self */ - GNUNET_break_op (0); - path_destroy (path); - connection_destroy (c); - return GNUNET_OK; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos); - path_add_to_peers (path, GNUNET_NO); - c->path = path_duplicate (path); - c->own_pos = own_pos; - } - else - { - path = NULL; - } - if (MESH_CONNECTION_NEW == c->state) - connection_change_state (c, MESH_CONNECTION_SENT); - - /* Remember peers */ - dest_peer = peer_get (&id[size - 1]); - orig_peer = peer_get (&id[0]); - - /* Is it a connection to us? */ - if (c->own_pos == size - 1) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n"); - peer_add_path_to_origin (orig_peer, path, GNUNET_YES); - - if (NULL == orig_peer->tunnel) - { - orig_peer->tunnel = tunnel_new (); - orig_peer->tunnel->peer = orig_peer; - } - tunnel_add_connection (orig_peer->tunnel, c); - if (MESH_TUNNEL_NEW == c->t->state) - tunnel_change_state (c->t, MESH_TUNNEL_WAITING); - - send_connection_ack (c, GNUNET_NO); - if (MESH_CONNECTION_SENT == c->state) - connection_change_state (c, MESH_CONNECTION_ACK); - - /* Keep tunnel alive in direction dest->owner*/ - connection_reset_timeout (c, GNUNET_NO); - } - else - { - /* It's for somebody else! Retransmit. */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n"); - peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO); - peer_add_path_to_origin (orig_peer, path, GNUNET_NO); - send_prebuilt_message_connection (message, c, NULL, GNUNET_YES); - } - return GNUNET_OK; -} - - -/** - * Core handler for path ACKs - * - * @param cls closure - * @param message message - * @param peer peer identity this notification is about - * - * @return GNUNET_OK to keep the connection open, - * GNUNET_SYSERR to close it (signal serious error) - */ -static int -handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) -{ - struct GNUNET_MESH_ConnectionACK *msg; - struct MeshConnection *c; - struct MeshPeerPath *p; - struct MeshPeer *pi; - int fwd; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n"); - msg = (struct GNUNET_MESH_ConnectionACK *) message; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", - GNUNET_h2s (&msg->cid)); - c = connection_get (&msg->cid); - if (NULL == c) - { - GNUNET_STATISTICS_update (stats, "# control on unknown connection", - 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n"); - return GNUNET_OK; - } - - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", - GNUNET_i2s (peer)); - pi = peer_get (peer); - if (connection_get_next_hop (c) == pi) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n"); - fwd = GNUNET_NO; - if (MESH_CONNECTION_SENT == c->state) - connection_change_state (c, MESH_CONNECTION_ACK); - } - else if (connection_get_prev_hop (c) == pi) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK\n"); - fwd = GNUNET_YES; - connection_change_state (c, MESH_CONNECTION_READY); - } - else - { - GNUNET_break_op (0); - return GNUNET_OK; - } - connection_reset_timeout (c, fwd); - - /* Add path to peers? */ - p = c->path; - if (NULL != p) - { - path_add_to_peers (p, GNUNET_YES); - } - else - { - GNUNET_break (0); - } - - /* Message for us as creator? */ - if (connection_is_origin (c, GNUNET_YES)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n"); - connection_change_state (c, MESH_CONNECTION_READY); - if (MESH_TUNNEL_READY != c->t->state) - tunnel_change_state (c->t, MESH_TUNNEL_READY); - send_connection_ack (c, GNUNET_YES); - tunnel_send_queued_data (c->t, GNUNET_YES); - if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget) - { - GNUNET_DHT_get_stop (c->t->peer->dhtget); - c->t->peer->dhtget = NULL; - } - return GNUNET_OK; - } - - /* Message for us as destination? */ - if (connection_is_terminal (c, GNUNET_YES)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n"); - if (MESH_TUNNEL_READY != c->t->state) - tunnel_change_state (c->t, MESH_TUNNEL_READY); - connection_change_state (c, MESH_CONNECTION_READY); - tunnel_send_queued_data (c->t, GNUNET_NO); - return GNUNET_OK; - } - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); - send_prebuilt_message_connection (message, c, NULL, fwd); - return GNUNET_OK; -} - - -/** - * Core handler for notifications of broken paths - * - * @param cls Closure (unused). - * @param peer Peer identity of sending neighbor. - * @param message Message. - * - * @return GNUNET_OK to keep the connection open, - * GNUNET_SYSERR to close it (signal serious error) - */ -static int -handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) -{ - struct GNUNET_MESH_ConnectionBroken *msg; - struct MeshConnection *c; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer)); - msg = (struct GNUNET_MESH_ConnectionBroken *) message; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", - GNUNET_i2s (&msg->peer1)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", - GNUNET_i2s (&msg->peer2)); - c = connection_get (&msg->cid); - if (NULL == c) - { - GNUNET_break_op (0); - return GNUNET_OK; - } - tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1), - GNUNET_PEER_search (&msg->peer2)); - return GNUNET_OK; - -} - - -/** - * Core handler for tunnel destruction - * - * @param cls Closure (unused). - * @param peer Peer identity of sending neighbor. - * @param message Message. - * - * @return GNUNET_OK to keep the connection open, - * GNUNET_SYSERR to close it (signal serious error) - */ -static int -handle_mesh_connection_destroy (void *cls, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) -{ - struct GNUNET_MESH_ConnectionDestroy *msg; - struct MeshConnection *c; - GNUNET_PEER_Id id; - int fwd; - - msg = (struct GNUNET_MESH_ConnectionDestroy *) message; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got a CONNECTION DESTROY message from %s\n", - GNUNET_i2s (peer)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " for connection %s\n", - GNUNET_h2s (&msg->cid)); - c = connection_get (&msg->cid); - if (NULL == c) - { - /* Probably already got the message from another path, - * destroyed the tunnel and retransmitted to children. - * Safe to ignore. - */ - GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", - 1, GNUNET_NO); - return GNUNET_OK; - } - id = GNUNET_PEER_search (peer); - if (id == connection_get_prev_hop (c)->id) - fwd = GNUNET_YES; - else if (id == connection_get_next_hop (c)->id) - fwd = GNUNET_NO; - else - { - GNUNET_break_op (0); - return GNUNET_OK; - } - send_prebuilt_message_connection (message, c, NULL, fwd); - c->destroy = GNUNET_YES; - - return GNUNET_OK; -} - - -/** - * Handler for channel create messages. - * - * @param t Tunnel this channel is to be created in. - * @param msg Message. - * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; - */ -static void -handle_channel_create (struct MeshTunnel2 *t, - struct GNUNET_MESH_ChannelCreate *msg, - int fwd) -{ - MESH_ChannelNumber chid; - struct MeshChannel *ch; - struct MeshClient *c; - uint32_t port; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n"); - /* Check message size */ - if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate)) - { - GNUNET_break_op (0); - return; - } - - /* Check if channel exists */ - chid = ntohl (msg->chid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid); - ch = channel_get (t, chid); - if (NULL != ch) - { - /* Probably a retransmission, safe to ignore */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n"); - if (NULL != ch->dest) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n"); - channel_send_ack (ch, !fwd); - return; - } - } - else - { - /* Create channel */ - ch = channel_new (t, NULL, 0); - ch->gid = chid; - channel_set_options (ch, ntohl (msg->opt)); - } - - /* Find a destination client */ - port = ntohl (msg->port); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port); - c = GNUNET_CONTAINER_multihashmap32_get (ports, port); - if (NULL == c) - { - /* TODO send reject */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n"); - /* TODO free ch */ - return; - } - - channel_add_client (ch, c); - if (GNUNET_YES == ch->reliable) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n"); - - send_local_channel_create (ch); - channel_send_ack (ch, fwd); - send_local_ack (ch, !fwd); -} - - -/** - * Handler for channel ack messages. - * - * @param t Tunnel this channel is to be created in. - * @param msg Message. - * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; - */ -static void -handle_channel_ack (struct MeshTunnel2 *t, - struct GNUNET_MESH_ChannelManage *msg, - int fwd) -{ - MESH_ChannelNumber chid; - struct MeshChannel *ch; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n"); - /* Check message size */ - if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage)) - { - GNUNET_break_op (0); - return; - } - - /* Check if channel exists */ - chid = ntohl (msg->chid); - ch = channel_get (t, chid); - if (NULL == ch) - { - GNUNET_break_op (0); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid); - return; - } - - channel_confirm (ch, !fwd); -} - - -/** - * Handler for channel destroy messages. - * - * @param t Tunnel this channel is to be destroyed of. - * @param msg Message. - * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; - */ -static void -handle_channel_destroy (struct MeshTunnel2 *t, - struct GNUNET_MESH_ChannelManage *msg, - int fwd) +handle_data_ack (struct MeshTunnel2 *t, + const struct GNUNET_MESH_DataACK *msg, int fwd) { - MESH_ChannelNumber chid; + struct MeshChannelReliability *rel; + struct MeshReliableMessage *copy; + struct MeshReliableMessage *next; struct MeshChannel *ch; + uint32_t ack; + uint16_t type; + int work; - /* Check message size */ - if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage)) + type = ntohs (msg->header.type); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n", + GNUNET_MESH_DEBUG_M2S (type)); + ch = channel_get (t, ntohl (msg->chid)); + if (NULL == ch) { - GNUNET_break_op (0); + GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO); return; } + ack = ntohl (msg->mid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n", + (GNUNET_YES == fwd) ? "FWD" : "BCK", ack); - /* Check if channel exists */ - chid = ntohl (msg->chid); - ch = channel_get (t, chid); - if (NULL == ch) + if (GNUNET_YES == fwd) { - /* Probably a retransmission, safe to ignore */ - return; + rel = ch->root_rel; } - if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) ) + else { - /* Not for us (don't destroy twice a half-open loopback channel) */ + rel = ch->dest_rel; + } + if (NULL == rel) + { + GNUNET_break (0); return; } - send_local_channel_destroy (ch, fwd); - channel_destroy (ch); -} - - -static void -handle_decrypted (struct MeshTunnel2 *t, - const struct GNUNET_MessageHeader *msgh, - int fwd) -{ - switch (ntohs (msgh->type)) + for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next) { - case GNUNET_MESSAGE_TYPE_MESH_DATA: - /* Don't send hop ACK, wait for client to ACK */ - handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd); - break; - - case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK: - handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd); - break; - - case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE: - handle_channel_create (t, - (struct GNUNET_MESH_ChannelCreate *) msgh, - fwd); - break; - - case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK: - handle_channel_ack (t, - (struct GNUNET_MESH_ChannelManage *) msgh, - fwd); + if (GMC_is_pid_bigger (copy->mid, ack)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid); + channel_rel_free_sent (rel, msg); break; + } + work = GNUNET_YES; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid); + next = copy->next; + rel_message_free (copy); + } + /* ACK client if needed */ +// channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type); - case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: - handle_channel_destroy (t, - (struct GNUNET_MESH_ChannelManage *) msgh, - fwd); - break; + /* If some message was free'd, update the retransmission delay*/ + if (GNUNET_YES == work) + { + if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task) + { + GNUNET_SCHEDULER_cancel (rel->retry_task); + if (NULL == rel->head_sent) + { + rel->retry_task = GNUNET_SCHEDULER_NO_TASK; + } + else + { + struct GNUNET_TIME_Absolute new_target; + struct GNUNET_TIME_Relative delay; - default: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "end-to-end message not known (%u)\n", - ntohs (msgh->type)); + delay = GNUNET_TIME_relative_multiply (rel->retry_timer, + MESH_RETRANSMIT_MARGIN); + new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp, + delay); + delay = GNUNET_TIME_absolute_get_remaining (new_target); + rel->retry_task = + GNUNET_SCHEDULER_add_delayed (delay, + &channel_retransmit_message, + rel); + } + } + else + GNUNET_break (0); } } /** - * Generic handler for mesh network encrypted traffic. + * Core handler for connection creation. * - * @param peer Peer identity this notification is about. - * @param message Encrypted message. - * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; + * @param cls Closure (unused). + * @param peer Sender (neighbor). + * @param message Message. * * @return GNUNET_OK to keep the connection open, * GNUNET_SYSERR to close it (signal serious error) */ static int -handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MESH_Encrypted *msg, - int fwd) +handle_mesh_connection_create (void *cls, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { + struct GNUNET_MESH_ConnectionCreate *msg; + struct GNUNET_PeerIdentity *id; + struct GNUNET_HashCode *cid; + struct MeshPeerPath *path; + struct MeshPeer *dest_peer; + struct MeshPeer *orig_peer; struct MeshConnection *c; - struct MeshTunnel2 *t; - struct MeshPeer *neighbor; - struct MeshFlowControl *fc; - uint32_t pid; - uint32_t ttl; - uint16_t type; - size_t size; + unsigned int own_pos; + uint16_t size; + uint16_t i; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n"); /* Check size */ - size = ntohs (msg->header.size); - if (size < - sizeof (struct GNUNET_MESH_Encrypted) + - sizeof (struct GNUNET_MessageHeader)) + size = ntohs (message->size); + if (size < sizeof (struct GNUNET_MESH_ConnectionCreate)) { GNUNET_break_op (0); return GNUNET_OK; } - type = ntohs (msg->header.type); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n", - GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer)); - /* Check connection */ - c = connection_get (&msg->cid); - if (NULL == c) + /* Calculate hops */ + size -= sizeof (struct GNUNET_MESH_ConnectionCreate); + if (size % sizeof (struct GNUNET_PeerIdentity)) { - GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n"); + GNUNET_break_op (0); return GNUNET_OK; } - t = c->t; - fc = fwd ? &c->bck_fc : &c->fwd_fc; - - /* Check if origin is as expected */ - neighbor = connection_get_hop (c, !fwd); - if (peer_get (peer)->id != neighbor->id) + size /= sizeof (struct GNUNET_PeerIdentity); + if (1 > size) { GNUNET_break_op (0); return GNUNET_OK; } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size); - /* Check PID */ - pid = ntohl (msg->pid); - if (GMC_is_pid_bigger (pid, fc->last_ack_sent)) - { - GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "WARNING Received PID %u, (prev %u), ACK %u\n", - pid, fc->last_pid_recv, fc->last_ack_sent); - return GNUNET_OK; - } - if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv)) - { - GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " Pid %u not expected (%u+), dropping!\n", - pid, fc->last_pid_recv + 1); - return GNUNET_OK; - } - if (MESH_CONNECTION_SENT == c->state) - connection_change_state (c, MESH_CONNECTION_READY); - connection_reset_timeout (c, fwd); - fc->last_pid_recv = pid; + /* Get parameters */ + msg = (struct GNUNET_MESH_ConnectionCreate *) message; + cid = &msg->cid; + id = (struct GNUNET_PeerIdentity *) &msg[1]; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " connection %s (%s).\n", + GNUNET_h2s (cid), GNUNET_i2s (id)); - /* Is this message for us? */ - if (connection_is_terminal (c, fwd)) + /* Create connection */ + c = connection_get (cid); + if (NULL == c) { - size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted); - char cbuf[dsize]; - struct GNUNET_MessageHeader *msgh; - unsigned int off; - - /* TODO signature verification */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n"); - GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n"); + c = connection_new (cid); + if (NULL == c) + return GNUNET_OK; + connection_reset_timeout (c, GNUNET_YES); - fc->last_pid_recv = pid; - tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd); - off = 0; - while (off < dsize) + /* Create path */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n"); + path = path_new (size); + own_pos = 0; + for (i = 0; i < size; i++) { - msgh = (struct GNUNET_MessageHeader *) &cbuf[off]; - handle_decrypted (t, msgh, fwd); - off += ntohs (msgh->size); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n", + GNUNET_i2s (&id[i])); + path->peers[i] = GNUNET_PEER_intern (&id[i]); + if (path->peers[i] == myid) + own_pos = i; } - send_ack (c, NULL, fwd); - return GNUNET_OK; - } - - /* Message not for us: forward to next hop */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); - ttl = ntohl (msg->ttl); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl); - if (ttl == 0) + if (own_pos == 0 && path->peers[own_pos] != myid) + { + /* create path: self not found in path through self */ + GNUNET_break_op (0); + path_destroy (path); + connection_destroy (c); + return GNUNET_OK; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos); + path_add_to_peers (path, GNUNET_NO); + c->path = path_duplicate (path); + c->own_pos = own_pos; + } + else { - GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n"); - send_ack (c, NULL, fwd); - return GNUNET_OK; + path = NULL; } - GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO); + if (MESH_CONNECTION_NEW == c->state) + connection_change_state (c, MESH_CONNECTION_SENT); - send_prebuilt_message_connection (&msg->header, c, NULL, fwd); + /* Remember peers */ + dest_peer = peer_get (&id[size - 1]); + orig_peer = peer_get (&id[0]); - return GNUNET_OK; -} + /* Is it a connection to us? */ + if (c->own_pos == size - 1) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n"); + peer_add_path_to_origin (orig_peer, path, GNUNET_YES); + if (NULL == orig_peer->tunnel) + { + orig_peer->tunnel = tunnel_new (); + orig_peer->tunnel->peer = orig_peer; + } + tunnel_add_connection (orig_peer->tunnel, c); + if (MESH_TUNNEL_NEW == c->t->state) + tunnel_change_state (c->t, MESH_TUNNEL_WAITING); -/** - * Core handler for mesh network traffic going orig->dest. - * - * @param cls Closure (unused). - * @param message Message received. - * @param peer Peer who sent the message. - * - * @return GNUNET_OK to keep the connection open, - * GNUNET_SYSERR to close it (signal serious error) - */ -static int -handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) -{ - return handle_mesh_encrypted (peer, - (struct GNUNET_MESH_Encrypted *)message, - GNUNET_YES); -} + send_connection_ack (c, GNUNET_NO); + if (MESH_CONNECTION_SENT == c->state) + connection_change_state (c, MESH_CONNECTION_ACK); -/** - * Core handler for mesh network traffic going dest->orig. - * - * @param cls Closure (unused). - * @param message Message received. - * @param peer Peer who sent the message. - * - * @return GNUNET_OK to keep the connection open, - * GNUNET_SYSERR to close it (signal serious error) - */ -static int -handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) -{ - return handle_mesh_encrypted (peer, - (struct GNUNET_MESH_Encrypted *)message, - GNUNET_NO); + /* Keep tunnel alive in direction dest->owner*/ + connection_reset_timeout (c, GNUNET_NO); + } + else + { + /* It's for somebody else! Retransmit. */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n"); + peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO); + peer_add_path_to_origin (orig_peer, path, GNUNET_NO); + send_prebuilt_message_connection (message, c, NULL, GNUNET_YES); + } + return GNUNET_OK; } /** - * Core handler for mesh network traffic point-to-point acks. + * Core handler for path ACKs * * @param cls closure * @param message message @@ -6025,925 +2481,829 @@ handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer, * GNUNET_SYSERR to close it (signal serious error) */ static int -handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) +handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_ACK *msg; + struct GNUNET_MESH_ConnectionACK *msg; struct MeshConnection *c; - struct MeshFlowControl *fc; - GNUNET_PEER_Id id; - uint32_t ack; + struct MeshPeerPath *p; + struct MeshPeer *pi; int fwd; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n", - GNUNET_i2s (peer)); - msg = (struct GNUNET_MESH_ACK *) message; - + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n"); + msg = (struct GNUNET_MESH_ConnectionACK *) message; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", + GNUNET_h2s (&msg->cid)); c = connection_get (&msg->cid); - if (NULL == c) { - GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1, - GNUNET_NO); + GNUNET_STATISTICS_update (stats, "# control on unknown connection", + 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n"); return GNUNET_OK; } - /* Is this a forward or backward ACK? */ - id = GNUNET_PEER_search (peer); - if (connection_get_next_hop (c)->id == id) + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", + GNUNET_i2s (peer)); + pi = peer_get (peer); + if (connection_get_next_hop (c) == pi) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); - fc = &c->fwd_fc; - fwd = GNUNET_YES; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n"); + fwd = GNUNET_NO; + if (MESH_CONNECTION_SENT == c->state) + connection_change_state (c, MESH_CONNECTION_ACK); } - else if (connection_get_prev_hop (c)->id == id) + else if (connection_get_prev_hop (c) == pi) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); - fc = &c->bck_fc; - fwd = GNUNET_NO; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK\n"); + fwd = GNUNET_YES; + connection_change_state (c, MESH_CONNECTION_READY); } else { GNUNET_break_op (0); return GNUNET_OK; } + connection_reset_timeout (c, fwd); - ack = ntohl (msg->ack); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n", - ack, fc->last_ack_recv); - if (GMC_is_pid_bigger (ack, fc->last_ack_recv)) - fc->last_ack_recv = ack; + /* Add path to peers? */ + p = c->path; + if (NULL != p) + { + path_add_to_peers (p, GNUNET_YES); + } + else + { + GNUNET_break (0); + } - /* Cancel polling if the ACK is big enough. */ - if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task && - GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent)) + /* Message for us as creator? */ + if (connection_is_origin (c, GNUNET_YES)) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n"); - GNUNET_SCHEDULER_cancel (fc->poll_task); - fc->poll_task = GNUNET_SCHEDULER_NO_TASK; - fc->poll_time = GNUNET_TIME_UNIT_SECONDS; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n"); + connection_change_state (c, MESH_CONNECTION_READY); + if (MESH_TUNNEL_READY != c->t->state) + tunnel_change_state (c->t, MESH_TUNNEL_READY); + send_connection_ack (c, GNUNET_YES); + tunnel_send_queued_data (c->t, GNUNET_YES); + if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget) + { + GNUNET_DHT_get_stop (c->t->peer->dhtget); + c->t->peer->dhtget = NULL; + } + return GNUNET_OK; } - connection_unlock_queue (c, fwd); + /* Message for us as destination? */ + if (GMC_is_terminal (c, GNUNET_YES)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n"); + if (MESH_TUNNEL_READY != c->t->state) + tunnel_change_state (c->t, MESH_TUNNEL_READY); + connection_change_state (c, MESH_CONNECTION_READY); + tunnel_send_queued_data (c->t, GNUNET_NO); + return GNUNET_OK; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); + send_prebuilt_message_connection (message, c, NULL, fwd); return GNUNET_OK; } /** - * Core handler for mesh network traffic point-to-point ack polls. + * Core handler for notifications of broken paths * - * @param cls closure - * @param message message - * @param peer peer identity this notification is about + * @param cls Closure (unused). + * @param peer Peer identity of sending neighbor. + * @param message Message. * * @return GNUNET_OK to keep the connection open, * GNUNET_SYSERR to close it (signal serious error) */ static int -handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) +handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_Poll *msg; + struct GNUNET_MESH_ConnectionBroken *msg; struct MeshConnection *c; - struct MeshFlowControl *fc; - GNUNET_PEER_Id id; - uint32_t pid; - int fwd; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n", - GNUNET_i2s (peer)); - - msg = (struct GNUNET_MESH_Poll *) message; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer)); + msg = (struct GNUNET_MESH_ConnectionBroken *) message; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", + GNUNET_i2s (&msg->peer1)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", + GNUNET_i2s (&msg->peer2)); c = connection_get (&msg->cid); - if (NULL == c) - { - GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1, - GNUNET_NO); - GNUNET_break_op (0); - return GNUNET_OK; - } - - /* Is this a forward or backward ACK? - * Note: a poll should never be needed in a loopback case, - * since there is no possiblility of packet loss there, so - * this way of discerining FWD/BCK should not be a problem. - */ - id = GNUNET_PEER_search (peer); - if (connection_get_next_hop (c)->id == id) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); - fc = &c->fwd_fc; - } - else if (connection_get_prev_hop (c)->id == id) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); - fc = &c->bck_fc; - } - else { GNUNET_break_op (0); return GNUNET_OK; } - - pid = ntohl (msg->pid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", - pid, fc->last_pid_recv); - fc->last_pid_recv = pid; - fwd = fc == &c->fwd_fc; - send_ack (c, NULL, fwd); - + tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1), + GNUNET_PEER_search (&msg->peer2)); return GNUNET_OK; + } /** - * Core handler for mesh keepalives. + * Core handler for tunnel destruction + * + * @param cls Closure (unused). + * @param peer Peer identity of sending neighbor. + * @param message Message. * - * @param cls closure - * @param message message - * @param peer peer identity this notification is about * @return GNUNET_OK to keep the connection open, * GNUNET_SYSERR to close it (signal serious error) - * - * TODO: Check who we got this from, to validate route. */ static int -handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_MessageHeader *message) +handle_mesh_connection_destroy (void *cls, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_ConnectionKeepAlive *msg; + struct GNUNET_MESH_ConnectionDestroy *msg; struct MeshConnection *c; - struct MeshPeer *neighbor; + GNUNET_PEER_Id id; int fwd; - msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n", + msg = (struct GNUNET_MESH_ConnectionDestroy *) message; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got a CONNECTION DESTROY message from %s\n", GNUNET_i2s (peer)); - + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " for connection %s\n", + GNUNET_h2s (&msg->cid)); c = connection_get (&msg->cid); if (NULL == c) { - GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1, - GNUNET_NO); + /* Probably already got the message from another path, + * destroyed the tunnel and retransmitted to children. + * Safe to ignore. + */ + GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", + 1, GNUNET_NO); return GNUNET_OK; } - - fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ? - GNUNET_YES : GNUNET_NO; - - /* Check if origin is as expected */ - neighbor = connection_get_hop (c, fwd); - if (peer_get (peer)->id != neighbor->id) + id = GNUNET_PEER_search (peer); + if (id == connection_get_prev_hop (c)->id) + fwd = GNUNET_YES; + else if (id == connection_get_next_hop (c)->id) + fwd = GNUNET_NO; + else { GNUNET_break_op (0); return GNUNET_OK; } - - connection_change_state (c, MESH_CONNECTION_READY); - connection_reset_timeout (c, fwd); - - if (connection_is_terminal (c, fwd)) - return GNUNET_OK; - - GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO); send_prebuilt_message_connection (message, c, NULL, fwd); + c->destroy = GNUNET_YES; return GNUNET_OK; } - -/** - * Functions to handle messages from core - */ -static struct GNUNET_CORE_MessageHandler core_handlers[] = { - {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE, - 0}, - {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK, - sizeof (struct GNUNET_MESH_ConnectionACK)}, - {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN, - sizeof (struct GNUNET_MESH_ConnectionBroken)}, - {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY, - sizeof (struct GNUNET_MESH_ConnectionDestroy)}, - {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE, - sizeof (struct GNUNET_MESH_ConnectionKeepAlive)}, - {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE, - sizeof (struct GNUNET_MESH_ConnectionKeepAlive)}, - {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK, - sizeof (struct GNUNET_MESH_ACK)}, - {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL, - sizeof (struct GNUNET_MESH_Poll)}, - {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0}, - {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0}, - {NULL, 0, 0} -}; - - /** - * Function to process paths received for a new peer addition. The recorded - * paths form the initial tunnel, which can be optimized later. - * Called on each result obtained for the DHT search. + * Handler for channel create messages. * - * @param cls closure - * @param exp when will this value expire - * @param key key of the result - * @param get_path path of the get request - * @param get_path_length lenght of get_path - * @param put_path path of the put request - * @param put_path_length length of the put_path - * @param type type of the result - * @param size number of bytes in data - * @param data pointer to the result data + * @param t Tunnel this channel is to be created in. + * @param msg Message. + * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; */ static void -dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, - const struct GNUNET_HashCode * key, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length, enum GNUNET_BLOCK_Type type, - size_t size, const void *data) +handle_channel_create (struct MeshTunnel2 *t, + struct GNUNET_MESH_ChannelCreate *msg, + int fwd) { - struct MeshPeer *peer = cls; - struct MeshPeerPath *p; - struct MeshConnection *c; - struct GNUNET_PeerIdentity pi; - int i; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n"); - GNUNET_PEER_resolve (peer->id, &pi); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi)); - - p = path_build_from_dht (get_path, get_path_length, - put_path, put_path_length); - path_add_to_peers (p, GNUNET_NO); - path_destroy (p); - - /* Count connections */ - for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++); + MESH_ChannelNumber chid; + struct MeshChannel *ch; + struct MeshClient *c; + uint32_t port; - /* If we already have 3 (or more (?!)) connections, it's enough */ - if (3 <= i) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n"); + /* Check message size */ + if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate)) + { + GNUNET_break_op (0); return; + } - if (peer->tunnel->state == MESH_TUNNEL_SEARCHING) + /* Check if channel exists */ + chid = ntohl (msg->chid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid); + ch = channel_get (t, chid); + if (NULL != ch) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n"); - peer_connect (peer); + /* Probably a retransmission, safe to ignore */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n"); + if (NULL != ch->dest) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n"); + channel_send_ack (ch, !fwd); + return; + } + } + else + { + /* Create channel */ + ch = channel_new (t, NULL, 0); + ch->gid = chid; + channel_set_options (ch, ntohl (msg->opt)); + } + + /* Find a destination client */ + port = ntohl (msg->port); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port); + c = GNUNET_CONTAINER_multihashmap32_get (ports, port); + if (NULL == c) + { + /* TODO send reject */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n"); + /* TODO free ch */ + return; } - return; -} + channel_add_client (ch, c); + if (GNUNET_YES == ch->reliable) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n"); -/******************************************************************************/ -/********************* MESH LOCAL HANDLES **************************/ -/******************************************************************************/ + send_local_channel_create (ch); + channel_send_ack (ch, fwd); + send_local_ack (ch, !fwd); +} /** - * Handler for client connection. + * Handler for channel ack messages. * - * @param cls Closure (unused). - * @param client Client handler. + * @param t Tunnel this channel is to be created in. + * @param msg Message. + * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; */ static void -handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client) +handle_channel_ack (struct MeshTunnel2 *t, + struct GNUNET_MESH_ChannelManage *msg, + int fwd) { - struct MeshClient *c; + MESH_ChannelNumber chid; + struct MeshChannel *ch; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n"); + /* Check message size */ + if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage)) + { + GNUNET_break_op (0); + return; + } - if (NULL == client) + /* Check if channel exists */ + chid = ntohl (msg->chid); + ch = channel_get (t, chid); + if (NULL == ch) + { + GNUNET_break_op (0); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid); return; - c = GNUNET_malloc (sizeof (struct MeshClient)); - c->handle = client; - c->id = next_client_id++; /* overflow not important: just for debug */ - GNUNET_SERVER_client_keep (client); - GNUNET_SERVER_client_set_user_context (client, c); - GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c); + } + + channel_confirm (ch, !fwd); } /** - * Handler for client disconnection + * Handler for channel destroy messages. * - * @param cls closure - * @param client identification of the client; NULL - * for the last call when the server is destroyed + * @param t Tunnel this channel is to be destroyed of. + * @param msg Message. + * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; */ static void -handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) +handle_channel_destroy (struct MeshTunnel2 *t, + struct GNUNET_MESH_ChannelManage *msg, + int fwd) { - struct MeshClient *c; + MESH_ChannelNumber chid; + struct MeshChannel *ch; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client); - if (client == NULL) + /* Check message size */ + if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage)) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n"); + GNUNET_break_op (0); return; } - c = client_get (client); - if (NULL != c) + /* Check if channel exists */ + chid = ntohl (msg->chid); + ch = channel_get (t, chid); + if (NULL == ch) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n", - c->id, c); - GNUNET_SERVER_client_drop (c->handle); - c->shutting_down = GNUNET_YES; - if (NULL != c->own_channels) - { - GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels, - &channel_destroy_iterator, c); - GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels); - } - - if (NULL != c->incoming_channels) - { - GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels, - &channel_destroy_iterator, c); - GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels); - } - - if (NULL != c->ports) - { - GNUNET_CONTAINER_multihashmap32_iterate (c->ports, - &client_release_ports, c); - GNUNET_CONTAINER_multihashmap32_destroy (c->ports); - } - GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c); - GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c); - GNUNET_free (c); + /* Probably a retransmission, safe to ignore */ + return; } - else + if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) ) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n"); + /* Not for us (don't destroy twice a half-open loopback channel) */ + return; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n"); - return; + + send_local_channel_destroy (ch, fwd); + channel_destroy (ch); } -/** - * Handler for new clients - * - * @param cls closure - * @param client identification of the client - * @param message the actual message, which includes messages the client wants - */ static void -handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +handle_decrypted (struct MeshTunnel2 *t, + const struct GNUNET_MessageHeader *msgh, + int fwd) { - struct GNUNET_MESH_ClientConnect *cc_msg; - struct MeshClient *c; - unsigned int size; - uint32_t *p; - unsigned int i; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client); + switch (ntohs (msgh->type)) + { + case GNUNET_MESSAGE_TYPE_MESH_DATA: + /* Don't send hop ACK, wait for client to ACK */ + handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd); + break; - /* Check data sanity */ - size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect); - cc_msg = (struct GNUNET_MESH_ClientConnect *) message; - if (0 != (size % sizeof (uint32_t))) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - size /= sizeof (uint32_t); + case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK: + handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd); + break; - /* Initialize new client structure */ - c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size); - if (size > 0) - { - uint32_t u32; + case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE: + handle_channel_create (t, + (struct GNUNET_MESH_ChannelCreate *) msgh, + fwd); + break; - p = (uint32_t *) &cc_msg[1]; - c->ports = GNUNET_CONTAINER_multihashmap32_create (size); - for (i = 0; i < size; i++) - { - u32 = ntohl (p[i]); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32); - - /* store in client's hashmap */ - GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); - /* store in global hashmap */ - /* FIXME only allow one client to have the port open, - * have a backup hashmap with waiting clients */ - GNUNET_CONTAINER_multihashmap32_put (ports, u32, c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); - } - } + case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK: + handle_channel_ack (t, + (struct GNUNET_MESH_ChannelManage *) msgh, + fwd); + break; - c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32); - c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32); - GNUNET_SERVER_notification_context_add (nc, client); - GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO); + case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: + handle_channel_destroy (t, + (struct GNUNET_MESH_ChannelManage *) msgh, + fwd); + break; - GNUNET_SERVER_receive_done (client, GNUNET_OK); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n"); + default: + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "end-to-end message not known (%u)\n", + ntohs (msgh->type)); + } } /** - * Handler for requests of new tunnels + * Generic handler for mesh network encrypted traffic. + * + * @param peer Peer identity this notification is about. + * @param message Encrypted message. + * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; * - * @param cls Closure. - * @param client Identification of the client. - * @param message The actual message. + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) */ -static void -handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static int +handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MESH_Encrypted *msg, + int fwd) { - struct GNUNET_MESH_ChannelMessage *msg; - struct MeshPeer *peer; + struct MeshConnection *c; struct MeshTunnel2 *t; - struct MeshChannel *ch; - struct MeshClient *c; - MESH_ChannelNumber chid; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n"); + struct MeshPeer *neighbor; + struct MeshFlowControl *fc; + uint32_t pid; + uint32_t ttl; + uint16_t type; + size_t size; - /* Sanity check for client registration */ - if (NULL == (c = client_get (client))) + /* Check size */ + size = ntohs (msg->header.size); + if (size < + sizeof (struct GNUNET_MESH_Encrypted) + + sizeof (struct GNUNET_MessageHeader)) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_break_op (0); + return GNUNET_OK; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + type = ntohs (msg->header.type); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n", + GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer)); - /* Message size sanity check */ - if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size)) + /* Check connection */ + c = connection_get (&msg->cid); + if (NULL == c) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n"); + return GNUNET_OK; } + t = c->t; + fc = fwd ? &c->bck_fc : &c->fwd_fc; - msg = (struct GNUNET_MESH_ChannelMessage *) message; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n", - GNUNET_i2s (&msg->peer), ntohl (msg->port)); - chid = ntohl (msg->channel_id); + /* Check if origin is as expected */ + neighbor = connection_get_hop (c, !fwd); + if (peer_get (peer)->id != neighbor->id) + { + GNUNET_break_op (0); + return GNUNET_OK; + } - /* Sanity check for duplicate channel IDs */ - if (NULL != channel_get_by_local_id (c, chid)) + /* Check PID */ + pid = ntohl (msg->pid); + if (GMC_is_pid_bigger (pid, fc->last_ack_sent)) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "WARNING Received PID %u, (prev %u), ACK %u\n", + pid, fc->last_pid_recv, fc->last_ack_sent); + return GNUNET_OK; + } + if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv)) + { + GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " Pid %u not expected (%u+), dropping!\n", + pid, fc->last_pid_recv + 1); + return GNUNET_OK; } + if (MESH_CONNECTION_SENT == c->state) + connection_change_state (c, MESH_CONNECTION_READY); + connection_reset_timeout (c, fwd); + fc->last_pid_recv = pid; - peer = peer_get (&msg->peer); - if (NULL == peer->tunnel) + /* Is this message for us? */ + if (GMC_is_terminal (c, fwd)) { - peer->tunnel = tunnel_new (); - peer->tunnel->peer = peer; - if (peer->id == myid) - { - tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY); - } - else + size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted); + char cbuf[dsize]; + struct GNUNET_MessageHeader *msgh; + unsigned int off; + + /* TODO signature verification */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n"); + GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO); + + fc->last_pid_recv = pid; + tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd); + off = 0; + while (off < dsize) { - peer_connect (peer); + msgh = (struct GNUNET_MessageHeader *) &cbuf[off]; + handle_decrypted (t, msgh, fwd); + off += ntohs (msgh->size); } + send_ack (c, NULL, fwd); + return GNUNET_OK; } - t = peer->tunnel; - /* Create channel */ - ch = channel_new (t, c, chid); - if (NULL == ch) + /* Message not for us: forward to next hop */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); + ttl = ntohl (msg->ttl); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl); + if (ttl == 0) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n"); + send_ack (c, NULL, fwd); + return GNUNET_OK; } - ch->port = ntohl (msg->port); - channel_set_options (ch, ntohl (msg->opt)); - - /* In unreliable channels, we'll use the DLL to buffer BCK data */ - ch->root_rel = GNUNET_new (struct MeshChannelReliability); - ch->root_rel->ch = ch; - ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME; + GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n", - peer2s (t->peer), ch->gid, ch->port, ch->lid_root); + send_prebuilt_message_connection (&msg->header, c, NULL, fwd); - /* Send create channel */ - { - struct GNUNET_MESH_ChannelCreate msgcc; + return GNUNET_OK; +} - msgcc.header.size = htons (sizeof (msgcc)); - msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE); - msgcc.chid = htonl (ch->gid); - msgcc.port = msg->port; - msgcc.opt = msg->opt; - tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES); - } +/** + * Core handler for mesh network traffic going orig->dest. + * + * @param cls Closure (unused). + * @param message Message received. + * @param peer Peer who sent the message. + * + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) + */ +static int +handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) +{ + return handle_mesh_encrypted (peer, + (struct GNUNET_MESH_Encrypted *)message, + GNUNET_YES); +} - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; +/** + * Core handler for mesh network traffic going dest->orig. + * + * @param cls Closure (unused). + * @param message Message received. + * @param peer Peer who sent the message. + * + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) + */ +static int +handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) +{ + return handle_mesh_encrypted (peer, + (struct GNUNET_MESH_Encrypted *)message, + GNUNET_NO); } /** - * Handler for requests of deleting tunnels + * Core handler for mesh network traffic point-to-point acks. * * @param cls closure - * @param client identification of the client - * @param message the actual message + * @param message message + * @param peer peer identity this notification is about + * + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) */ -static void -handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static int +handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_ChannelMessage *msg; - struct MeshClient *c; - struct MeshChannel *ch; - struct MeshTunnel2 *t; - MESH_ChannelNumber chid; + struct GNUNET_MESH_ACK *msg; + struct MeshConnection *c; + struct MeshFlowControl *fc; + GNUNET_PEER_Id id; + uint32_t ack; + int fwd; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got a DESTROY CHANNEL from client!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n", + GNUNET_i2s (peer)); + msg = (struct GNUNET_MESH_ACK *) message; - /* Sanity check for client registration */ - if (NULL == (c = client_get (client))) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + c = connection_get (&msg->cid); - /* Message sanity check */ - if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size)) + if (NULL == c) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1, + GNUNET_NO); + return GNUNET_OK; } - msg = (struct GNUNET_MESH_ChannelMessage *) message; - - /* Retrieve tunnel */ - chid = ntohl (msg->channel_id); - ch = channel_get_by_local_id (c, chid); - if (NULL == ch) + /* Is this a forward or backward ACK? */ + id = GNUNET_PEER_search (peer); + if (connection_get_next_hop (c)->id == id) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid); - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); + fc = &c->fwd_fc; + fwd = GNUNET_YES; } - - /* Cleanup after the tunnel */ - client_delete_channel (c, ch); - if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid) + else if (connection_get_prev_hop (c)->id == id) { - ch->dest = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); + fc = &c->bck_fc; + fwd = GNUNET_NO; } - else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid) + else { - ch->root = NULL; + GNUNET_break_op (0); + return GNUNET_OK; } - else + + ack = ntohl (msg->ack); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n", + ack, fc->last_ack_recv); + if (GMC_is_pid_bigger (ack, fc->last_ack_recv)) + fc->last_ack_recv = ack; + + /* Cancel polling if the ACK is big enough. */ + if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task && + GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - " channel %X client %p (%p, %p)\n", - chid, c, ch->root, ch->dest); - GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n"); + GNUNET_SCHEDULER_cancel (fc->poll_task); + fc->poll_task = GNUNET_SCHEDULER_NO_TASK; + fc->poll_time = GNUNET_TIME_UNIT_SECONDS; } - t = ch->t; - channel_destroy (ch); - tunnel_destroy_if_empty (t); + connection_unlock_queue (c, fwd); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; + return GNUNET_OK; } /** - * Handler for client traffic + * Core handler for mesh network traffic point-to-point ack polls. * * @param cls closure - * @param client identification of the client - * @param message the actual message + * @param message message + * @param peer peer identity this notification is about + * + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) */ -static void -handle_local_data (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static int +handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_LocalData *msg; - struct MeshClient *c; - struct MeshChannel *ch; - struct MeshChannelReliability *rel; - MESH_ChannelNumber chid; - size_t size; + struct GNUNET_MESH_Poll *msg; + struct MeshConnection *c; + struct MeshFlowControl *fc; + GNUNET_PEER_Id id; + uint32_t pid; int fwd; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got data from a client!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n", + GNUNET_i2s (peer)); - /* Sanity check for client registration */ - if (NULL == (c = client_get (client))) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + msg = (struct GNUNET_MESH_Poll *) message; - msg = (struct GNUNET_MESH_LocalData *) message; + c = connection_get (&msg->cid); - /* Sanity check for message size */ - size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData); - if (size < sizeof (struct GNUNET_MessageHeader)) + if (NULL == c) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1, + GNUNET_NO); + GNUNET_break_op (0); + return GNUNET_OK; } - /* Channel exists? */ - chid = ntohl (msg->id); - fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; - ch = channel_get_by_local_id (c, chid); - if (NULL == ch) + /* Is this a forward or backward ACK? + * Note: a poll should never be needed in a loopback case, + * since there is no possiblility of packet loss there, so + * this way of discerining FWD/BCK should not be a problem. + */ + id = GNUNET_PEER_search (peer); + if (connection_get_next_hop (c)->id == id) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); + fc = &c->fwd_fc; } - - /* Is the client in the channel? */ - if ( !( (fwd && - ch->root && - ch->root->handle == client) - || - (!fwd && - ch->dest && - ch->dest->handle == client) ) ) + else if (connection_get_prev_hop (c)->id == id) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); + fc = &c->bck_fc; } - - rel = fwd ? ch->root_rel : ch->dest_rel; - rel->client_ready = GNUNET_NO; - - /* Ok, everything is correct, send the message. */ + else { - struct GNUNET_MESH_Data *payload; - uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size; - unsigned char cbuf[p2p_size]; - - payload = (struct GNUNET_MESH_Data *) cbuf; - payload->mid = htonl (rel->mid_send); - rel->mid_send++; - memcpy (&payload[1], &msg[1], size); - payload->header.size = htons (p2p_size); - payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA); - payload->chid = htonl (ch->gid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n"); - send_prebuilt_message_channel (&payload->header, ch, fwd); - - if (GNUNET_YES == ch->reliable) - channel_save_copy (ch, &payload->header, fwd); + GNUNET_break_op (0); + return GNUNET_OK; } - if (tunnel_get_buffer (ch->t, fwd) > 0) - send_local_ack (ch, fwd); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n"); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; + pid = ntohl (msg->pid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", + pid, fc->last_pid_recv); + fc->last_pid_recv = pid; + fwd = fc == &c->fwd_fc; + send_ack (c, NULL, fwd); + + return GNUNET_OK; } /** - * Handler for client's ACKs for payload traffic. + * Core handler for mesh keepalives. * - * @param cls Closure (unused). - * @param client Identification of the client. - * @param message The actual message. + * @param cls closure + * @param message message + * @param peer peer identity this notification is about + * @return GNUNET_OK to keep the connection open, + * GNUNET_SYSERR to close it (signal serious error) + * + * TODO: Check who we got this from, to validate route. */ -static void -handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static int +handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_LocalAck *msg; - struct MeshChannelReliability *rel; - struct MeshChannel *ch; - struct MeshClient *c; - MESH_ChannelNumber chid; + struct GNUNET_MESH_ConnectionKeepAlive *msg; + struct MeshConnection *c; + struct MeshPeer *neighbor; int fwd; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n"); + msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n", + GNUNET_i2s (peer)); - /* Sanity check for client registration */ - if (NULL == (c = client_get (client))) + c = connection_get (&msg->cid); + if (NULL == c) { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1, + GNUNET_NO); + return GNUNET_OK; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); - msg = (struct GNUNET_MESH_LocalAck *) message; + fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ? + GNUNET_YES : GNUNET_NO; - /* Channel exists? */ - chid = ntohl (msg->channel_id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid); - ch = channel_get_by_local_id (c, chid); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch); - if (NULL == ch) + /* Check if origin is as expected */ + neighbor = connection_get_hop (c, fwd); + if (peer_get (peer)->id != neighbor->id) { - GNUNET_break (0); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid); - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; + GNUNET_break_op (0); + return GNUNET_OK; } - /* If client is root, the ACK is going FWD, therefore this is "BCK". */ - /* If client is dest, the ACK is going BCK, therefore this is "FWD" */ - fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; - rel = fwd ? ch->dest_rel : ch->root_rel; + connection_change_state (c, MESH_CONNECTION_READY); + connection_reset_timeout (c, fwd); - rel->client_ready = GNUNET_YES; - channel_send_client_buffered_data (ch, c, fwd); - send_ack (NULL, ch, fwd); + if (GMC_is_terminal (c, fwd)) + return GNUNET_OK; - GNUNET_SERVER_receive_done (client, GNUNET_OK); + GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO); + send_prebuilt_message_connection (message, c, NULL, fwd); - return; + return GNUNET_OK; } + /** - * Iterator over all tunnels to send a monitoring client info about each tunnel. - * - * @param cls Closure (client handle). - * @param key Key (hashed tunnel ID, unused). - * @param value Tunnel info. - * - * @return GNUNET_YES, to keep iterating. + * Functions to handle messages from core */ -// static int -// monitor_all_tunnels_iterator (void *cls, -// const struct GNUNET_HashCode * key, -// void *value) -// { -// struct GNUNET_SERVER_Client *client = cls; -// struct MeshChannel *ch = value; -// struct GNUNET_MESH_LocalMonitor *msg; -// -// msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor)); -// msg->channel_id = htonl (ch->gid); -// msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor)); -// msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS); -// -// GNUNET_log (GNUNET_ERROR_TYPE_INFO, -// "* sending info about tunnel %s\n", -// GNUNET_i2s (&msg->owner)); -// -// GNUNET_SERVER_notification_context_unicast (nc, client, -// &msg->header, GNUNET_NO); -// return GNUNET_YES; -// } +static struct GNUNET_CORE_MessageHandler core_handlers[] = { + {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE, + 0}, + {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK, + sizeof (struct GNUNET_MESH_ConnectionACK)}, + {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN, + sizeof (struct GNUNET_MESH_ConnectionBroken)}, + {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY, + sizeof (struct GNUNET_MESH_ConnectionDestroy)}, + {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE, + sizeof (struct GNUNET_MESH_ConnectionKeepAlive)}, + {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE, + sizeof (struct GNUNET_MESH_ConnectionKeepAlive)}, + {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK, + sizeof (struct GNUNET_MESH_ACK)}, + {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL, + sizeof (struct GNUNET_MESH_Poll)}, + {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0}, + {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0}, + {NULL, 0, 0} +}; /** - * Handler for client's MONITOR request. + * Function to process paths received for a new peer addition. The recorded + * paths form the initial tunnel, which can be optimized later. + * Called on each result obtained for the DHT search. * - * @param cls Closure (unused). - * @param client Identification of the client. - * @param message The actual message. + * @param cls closure + * @param exp when will this value expire + * @param key key of the result + * @param get_path path of the get request + * @param get_path_length lenght of get_path + * @param put_path path of the put request + * @param put_path_length length of the put_path + * @param type type of the result + * @param size number of bytes in data + * @param data pointer to the result data */ static void -handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, + const struct GNUNET_HashCode * key, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length, enum GNUNET_BLOCK_Type type, + size_t size, const void *data) { - struct MeshClient *c; - - /* Sanity check for client registration */ - if (NULL == (c = client_get (client))) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); - return; - } + struct MeshPeer *peer = cls; + struct MeshPeerPath *p; + struct MeshConnection *c; + struct GNUNET_PeerIdentity pi; + unsigned int connection_count; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Received get tunnels request from client %u\n", - c->id); -// GNUNET_CONTAINER_multihashmap_iterate (tunnels, -// monitor_all_tunnels_iterator, -// client); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Get tunnels request from client %u completed\n", - c->id); - GNUNET_SERVER_receive_done (client, GNUNET_OK); -} + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n"); + GNUNET_PEER_resolve (peer->id, &pi); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi)); + p = path_build_from_dht (get_path, get_path_length, + put_path, put_path_length); + path_add_to_peers (p, GNUNET_NO); + path_destroy (p); -/** - * Handler for client's MONITOR_TUNNEL request. - * - * @param cls Closure (unused). - * @param client Identification of the client. - * @param message The actual message. - */ -static void -handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) -{ - const struct GNUNET_MESH_LocalMonitor *msg; - struct GNUNET_MESH_LocalMonitor *resp; - struct MeshClient *c; - struct MeshChannel *ch; + /* Count connections */ + connection_count = GMC_count (peer->tunnel->connection_head); - /* Sanity check for client registration */ - if (NULL == (c = client_get (client))) - { - GNUNET_break (0); - GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + /* If we already have 3 (or more (?!)) connections, it's enough */ + if (3 <= connection_count) return; - } - msg = (struct GNUNET_MESH_LocalMonitor *) message; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Received tunnel info request from client %u for tunnel %s[%X]\n", - c->id, - &msg->owner, - ntohl (msg->channel_id)); -// ch = channel_get (&msg->owner, ntohl (msg->channel_id)); - ch = NULL; // FIXME - if (NULL == ch) + if (peer->tunnel->state == MESH_TUNNEL_SEARCHING) { - /* We don't know the tunnel */ - struct GNUNET_MESH_LocalMonitor warn; - - warn = *msg; - GNUNET_SERVER_notification_context_unicast (nc, client, - &warn.header, - GNUNET_NO); - GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n"); + peer_connect (peer); } - - /* Initialize context */ - resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor)); - *resp = *msg; - resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor)); - GNUNET_SERVER_notification_context_unicast (nc, c->handle, - &resp->header, GNUNET_NO); - GNUNET_free (resp); - - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Monitor tunnel request from client %u completed\n", - c->id); - GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; } -/** - * Functions to handle messages from clients - */ -static struct GNUNET_SERVER_MessageHandler client_handlers[] = { - {&handle_local_new_client, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0}, - {&handle_local_channel_create, NULL, - GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE, - sizeof (struct GNUNET_MESH_ChannelMessage)}, - {&handle_local_channel_destroy, NULL, - GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY, - sizeof (struct GNUNET_MESH_ChannelMessage)}, - {&handle_local_data, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, - {&handle_local_ack, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, - sizeof (struct GNUNET_MESH_LocalAck)}, - {&handle_local_get_tunnels, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS, - sizeof (struct GNUNET_MessageHeader)}, - {&handle_local_show_tunnel, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL, - sizeof (struct GNUNET_MESH_LocalMonitor)}, - {NULL, NULL, 0, 0} -}; - /** * Method called whenever a given peer connects. @@ -7002,7 +3362,7 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) } GNUNET_CONTAINER_multihashmap_iterate (pi->connections, - connection_broken, + GMC_notify_broken, pi); GNUNET_CONTAINER_multihashmap_destroy (pi->connections); pi->connections = NULL; @@ -7021,25 +3381,6 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) } -/** - * Install server (service) handlers and start listening to clients. - */ -static void -server_init (void) -{ - GNUNET_SERVER_add_handlers (server_handle, client_handlers); - GNUNET_SERVER_connect_notify (server_handle, - &handle_local_client_connect, NULL); - GNUNET_SERVER_disconnect_notify (server_handle, - &handle_local_client_disconnect, NULL); - nc = GNUNET_SERVER_notification_context_create (server_handle, 1); - - clients_head = NULL; - clients_tail = NULL; - next_client_id = 0; - GNUNET_SERVER_resume (server_handle); -} - /** * To be called on core init/fail. @@ -7132,11 +3473,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_DHT_disconnect (dht_handle); dht_handle = NULL; } - if (nc != NULL) - { - GNUNET_SERVER_notification_context_destroy (nc); - nc = NULL; - } + GML_shutdown (); if (GNUNET_SCHEDULER_NO_TASK != announce_id_task) { GNUNET_SCHEDULER_cancel (announce_id_task); @@ -7160,19 +3497,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, struct GNUNET_CRYPTO_EccPrivateKey *pk; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n"); - server_handle = server; - GNUNET_SERVER_suspend (server_handle); - - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME", - &refresh_connection_time)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - "MESH", "REFRESH_CONNECTION_TIME", "MISSING"); - GNUNET_SCHEDULER_shutdown (); - return; - } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME", @@ -7194,26 +3518,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, return; } - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE", - &max_msgs_queue)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - "MESH", "MAX_MSGS_QUEUE", "MISSING"); - GNUNET_SCHEDULER_shutdown (); - return; - } - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS", - &max_connections)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - "MESH", "MAX_CONNECTIONS", "MISSING"); - GNUNET_SCHEDULER_shutdown (); - return; - } - if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL", &default_ttl)) @@ -7257,9 +3561,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, dht_replication_level = 3; } - connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES); peers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO); - ports = GNUNET_CONTAINER_multihashmap32_create (32); dht_handle = GNUNET_DHT_connect (c, 64); if (NULL == dht_handle) @@ -7282,6 +3584,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, "Mesh for peer [%s] starting\n", GNUNET_i2s(&my_full_id)); + GML_init (server); + GMC_init (c); + core_handle = GNUNET_CORE_connect (c, /* Main configuration */ NULL, /* Closure passed to MESH functions */ &core_init, /* Call core_init once connected */ diff --git a/src/mesh/gnunet-service-mesh_channel.c b/src/mesh/gnunet-service-mesh_channel.c new file mode 100644 index 000000000..971b8279f --- /dev/null +++ b/src/mesh/gnunet-service-mesh_channel.c @@ -0,0 +1,1252 @@ +/* + This file is part of GNUnet. + (C) 2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#include "platform.h" +#include "mesh_enc.h" +#include "mesh_protocol_enc.h" +#include "gnunet-service-mesh_channel.h" +#include "gnunet-service-mesh_local.h" + + + +/** + * All the states a connection can be in. + */ +enum MeshChannelState +{ + /** + * Uninitialized status, should never appear in operation. + */ + MESH_CHANNEL_NEW, + + /** + * Connection create message sent, waiting for ACK. + */ + MESH_CHANNEL_SENT, + + /** + * Connection confirmed, ready to carry traffic.. + */ + MESH_CHANNEL_READY, +}; + + + +/** + * Info needed to retry a message in case it gets lost. + */ +struct MeshReliableMessage +{ + /** + * Double linked list, FIFO style + */ + struct MeshReliableMessage *next; + struct MeshReliableMessage *prev; + + /** + * Type of message (payload, channel management). + */ + int16_t type; + + /** + * Tunnel Reliability queue this message is in. + */ + struct MeshChannelReliability *rel; + + /** + * ID of the message (ACK needed to free) + */ + uint32_t mid; + + /** + * When was this message issued (to calculate ACK delay) + */ + struct GNUNET_TIME_Absolute timestamp; + + /* struct GNUNET_MESH_Data with payload */ +}; + + +/** + * Info about the traffic state for a client in a channel. + */ +struct MeshChannelReliability +{ + /** + * Channel this is about. + */ + struct MeshChannel *ch; + + /** + * DLL of messages sent and not yet ACK'd. + */ + struct MeshReliableMessage *head_sent; + struct MeshReliableMessage *tail_sent; + + /** + * Messages pending to send. + */ + unsigned int n_sent; + + /** + * DLL of messages received out of order. + */ + struct MeshReliableMessage *head_recv; + struct MeshReliableMessage *tail_recv; + + /** + * Messages received. + */ + unsigned int n_recv; + + /** + * Next MID to use for outgoing traffic. + */ + uint32_t mid_send; + + /** + * Next MID expected for incoming traffic. + */ + uint32_t mid_recv; + + /** + * Can we send data to the client? + */ + int client_ready; + + /** + * Task to resend/poll in case no ACK is received. + */ + GNUNET_SCHEDULER_TaskIdentifier retry_task; + + /** + * Counter for exponential backoff. + */ + struct GNUNET_TIME_Relative retry_timer; + + /** + * How long does it usually take to get an ACK. + */ + struct GNUNET_TIME_Relative expected_delay; +}; + + +/** + * Struct containing all information regarding a channel to a remote client. + */ +struct MeshChannel +{ + /** + * Tunnel this channel is in. + */ + struct MeshTunnel2 *t; + + /** + * Double linked list. + */ + struct MeshChannel *next; + struct MeshChannel *prev; + + /** + * Destination port of the channel. + */ + uint32_t port; + + /** + * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI) + */ + MESH_ChannelNumber gid; + + /** + * Local tunnel number for root (owner) client. + * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 ) + */ + MESH_ChannelNumber lid_root; + + /** + * Local tunnel number for local destination clients (incoming number) + * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0). + */ + MESH_ChannelNumber lid_dest; + + /** + * Channel state. + */ + enum MeshChannelState state; + + /** + * Is the tunnel bufferless (minimum latency)? + */ + int nobuffer; + + /** + * Is the tunnel reliable? + */ + int reliable; + + /** + * Last time the channel was used + */ + struct GNUNET_TIME_Absolute timestamp; + + /** + * Client owner of the tunnel, if any + */ + struct MeshClient *root; + + /** + * Client destination of the tunnel, if any. + */ + struct MeshClient *dest; + + /** + * Flag to signal the destruction of the channel. + * If this is set GNUNET_YES the channel will be destroyed + * when the queue is empty. + */ + int destroy; + + /** + * Total messages pending for this channel, payload or not. + */ + unsigned int pending_messages; + + /** + * Reliability data. + * Only present (non-NULL) at the owner of a tunnel. + */ + struct MeshChannelReliability *root_rel; + + /** + * Reliability data. + * Only present (non-NULL) at the destination of a tunnel. + */ + struct MeshChannelReliability *dest_rel; + +}; + + +/** + * We have received a message out of order, or the client is not ready. + * Buffer it until we receive an ACK from the client or the missing + * message from the channel. + * + * @param msg Message to buffer (MUST be of type MESH_DATA). + * @param rel Reliability data to the corresponding direction. + */ +static void +add_buffered_data (const struct GNUNET_MESH_Data *msg, + struct MeshChannelReliability *rel) +{ + struct MeshReliableMessage *copy; + struct MeshReliableMessage *prev; + uint32_t mid; + uint16_t size; + + size = ntohs (msg->header.size); + mid = ntohl (msg->mid); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid); + + copy = GNUNET_malloc (sizeof (*copy) + size); + copy->mid = mid; + copy->rel = rel; + memcpy (©[1], msg, size); + + rel->n_recv++; + + // FIXME do something better than O(n), although n < 64... + // FIXME start from the end (most messages are the latest ones) + for (prev = rel->head_recv; NULL != prev; prev = prev->next) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid); + if (GMC_is_pid_bigger (prev->mid, mid)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n"); + GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv, + prev, copy); + return; + } + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n"); + GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n"); +} + + +/** + * Send data to a client. + * + * If the client is ready, send directly, otherwise buffer while listening + * for a local ACK. + * + * @param ch Channel + * @param msg Message. + * @param fwd Is this a fwd (root->dest) message? + */ +static void +send_client_data (struct MeshChannel *ch, + const struct GNUNET_MESH_Data *msg, + int fwd) +{ + if (fwd) + { + if (ch->dest_rel->client_ready) + GML_send_data (ch, msg, ch->dest, ch->lid_dest); + else + add_buffered_data (msg, ch->dest_rel); + } + else + { + if (ch->root_rel->client_ready) + GML_send_data (ch, msg, ch->root, ch->lid_root); + else + add_buffered_data (msg, ch->root_rel); + } +} + + +/** + * Search for a channel among the channels for a client + * + * @param c the client whose channels to search in + * @param chid the local id of the channel + * + * @return channel handler, NULL if doesn't exist + */ +static struct MeshChannel * +channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid); + if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)) + { + GNUNET_break_op (0); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid); + return NULL; + } + if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV) + return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid); + return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid); +} + + + +/** + * Search for a channel by global ID using full PeerIdentities. + * + * @param t Tunnel containing the channel. + * @param chid Public channel number. + * + * @return channel handler, NULL if doesn't exist + */ +static struct MeshChannel * +channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid) +{ + struct MeshChannel *ch; + + if (NULL == t) + return NULL; + + for (ch = t->channel_head; NULL != ch; ch = ch->next) + { + if (ch->gid == chid) + break; + } + + return ch; +} + + +/** + * Add a client to a channel, initializing all needed data structures. + * + * @param ch Channel to which add the client. + * @param c Client which to add to the channel. + */ +static void +channel_add_client (struct MeshChannel *ch, struct MeshClient *c) +{ + struct MeshTunnel2 *t = ch->t; + + if (NULL != ch->dest) + { + GNUNET_break (0); + return; + } + + /* Assign local id as destination */ + while (NULL != channel_get_by_local_id (c, t->next_local_chid)) + t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; + ch->lid_dest = t->next_local_chid++; + t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; + + /* Store in client's hashmap */ + if (GNUNET_OK != + GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels, + ch->lid_dest, ch, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) + { + GNUNET_break (0); + return; + } + + GNUNET_break (NULL == ch->dest_rel); + ch->dest_rel = GNUNET_new (struct MeshChannelReliability); + ch->dest_rel->ch = ch; + ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME; + + ch->dest = c; +} + + + +/** + * Is the root client for this channel on this peer? + * + * @param ch Channel. + * @param fwd Is this for fwd traffic? + * + * @return GNUNET_YES in case it is. + */ +static int +channel_is_origin (struct MeshChannel *ch, int fwd) +{ + struct MeshClient *c; + + c = fwd ? ch->root : ch->dest; + return NULL != c; +} + + +/** + * Is the destination client for this channel on this peer? + * + * @param ch Channel. + * @param fwd Is this for fwd traffic? + * + * @return GNUNET_YES in case it is. + */ +static int +channel_is_terminal (struct MeshChannel *ch, int fwd) +{ + struct MeshClient *c; + + c = fwd ? ch->dest : ch->root; + return NULL != c; +} + + +/** + * Get free buffer space towards the client on a specific channel. + * + * @param ch Channel. + * @param fwd Is query about FWD traffic? + * + * @return Free buffer space [0 - 64] + */ +static unsigned int +channel_get_buffer (struct MeshChannel *ch, int fwd) +{ + struct MeshChannelReliability *rel; + + rel = fwd ? ch->dest_rel : ch->root_rel; + + /* If rel is NULL it means that the end is not yet created, + * most probably is a loopback channel at the point of sending + * the ChannelCreate to itself. + */ + if (NULL == rel) + return 64; + + return (64 - rel->n_recv); +} + + + + +/** + * Destroy all reliable messages queued for a channel, + * during a channel destruction. + * Frees the reliability structure itself. + * + * @param rel Reliability data for a channel. + */ +static void +channel_rel_free_all (struct MeshChannelReliability *rel) +{ + struct MeshReliableMessage *copy; + struct MeshReliableMessage *next; + + if (NULL == rel) + return; + + for (copy = rel->head_recv; NULL != copy; copy = next) + { + next = copy->next; + GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); + GNUNET_free (copy); + } + for (copy = rel->head_sent; NULL != copy; copy = next) + { + next = copy->next; + GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy); + GNUNET_free (copy); + } + if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task) + GNUNET_SCHEDULER_cancel (rel->retry_task); + GNUNET_free (rel); +} + + +/** + * Mark future messages as ACK'd. + * + * @param rel Reliability data. + * @param msg DataACK message with a bitfield of future ACK'd messages. + */ +static void +channel_rel_free_sent (struct MeshChannelReliability *rel, + const struct GNUNET_MESH_DataACK *msg) +{ + struct MeshReliableMessage *copy; + struct MeshReliableMessage *next; + uint64_t bitfield; + uint64_t mask; + uint32_t mid; + uint32_t target; + unsigned int i; + + bitfield = msg->futures; + mid = ntohl (msg->mid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "free_sent_reliable %u %llX\n", + mid, bitfield); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " rel %p, head %p\n", + rel, rel->head_sent); + for (i = 0, copy = rel->head_sent; + i < 64 && NULL != copy && 0 != bitfield; + i++) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " trying bit %u (mid %u)\n", + i, mid + i + 1); + mask = 0x1LL << i; + if (0 == (bitfield & mask)) + continue; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n"); + /* Bit was set, clear the bit from the bitfield */ + bitfield &= ~mask; + + /* The i-th bit was set. Do we have that copy? */ + /* Skip copies with mid < target */ + target = mid + i + 1; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target); + while (NULL != copy && GMC_is_pid_bigger (target, copy->mid)) + copy = copy->next; + + /* Did we run out of copies? (previously freed, it's ok) */ + if (NULL == copy) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n"); + return; + } + + /* Did we overshoot the target? (previously freed, it's ok) */ + if (GMC_is_pid_bigger (copy->mid, target)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid); + continue; + } + + /* Now copy->mid == target, free it */ + next = copy->next; + rel_message_free (copy); + copy = next; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n"); +} + + +/** + * We haven't received an ACK after a certain time: restransmit the message. + * + * @param cls Closure (MeshReliableMessage with the message to restransmit) + * @param tc TaskContext. + */ +static void +channel_retransmit_message (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshChannelReliability *rel = cls; + struct MeshReliableMessage *copy; + struct MeshPeerQueue *q; + struct MeshChannel *ch; + struct MeshConnection *c; + struct GNUNET_MESH_Data *payload; + struct MeshPeer *hop; + int fwd; + + rel->retry_task = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + + ch = rel->ch; + copy = rel->head_sent; + if (NULL == copy) + { + GNUNET_break (0); + return; + } + + /* Search the message to be retransmitted in the outgoing queue. + * Check only the queue for the connection that is going to be used, + * if the message is stuck in some other connection's queue we shouldn't + * act upon it: + * - cancelling it and sending the new one doesn't guarantee it's delivery, + * the old connection could be temporary stalled or the queue happened to + * be long at time of insertion. + * - not sending the new one could cause terrible delays the old connection + * is stalled. + */ + payload = (struct GNUNET_MESH_Data *) ©[1]; + fwd = (rel == ch->root_rel); + c = tunnel_get_connection (ch->t, fwd); + hop = connection_get_hop (c, fwd); + for (q = hop->queue_head; NULL != q; q = q->next) + { + if (ntohs (payload->header.type) == q->type && ch == q->ch) + { + struct GNUNET_MESH_Data *queued_data = q->cls; + + if (queued_data->mid == payload->mid) + break; + } + } + + /* Message not found in the queue that we are going to use. */ + if (NULL == q) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid); + + send_prebuilt_message_channel (&payload->header, ch, fwd); + GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid); + } + + rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer); + rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer, + &channel_retransmit_message, + cls); +} + + +/** + * Send ACK on one or more connections due to buffer space to the client. + * + * Iterates all connections of the tunnel and sends ACKs appropriately. + * + * @param ch Channel which has some free buffer space. + * @param fwd Is this in for FWD traffic? (ACK goes dest->root) + */ +static void +channel_send_connections_ack (struct MeshChannel *ch, + unsigned int buffer, + int fwd) +{ + struct MeshTunnel2 *t = ch->t; + struct MeshConnection *c; + struct MeshFlowControl *fc; + uint32_t allowed; + uint32_t to_allow; + uint32_t allow_per_connection; + unsigned int cs; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Channel send connection %s ack on %s:%X\n", + fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid); + + /* Count connections, how many messages are already allowed */ + for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next) + { + fc = fwd ? &c->fwd_fc : &c->bck_fc; + if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent)) + { + GNUNET_break (0); + continue; + } + allowed += fc->last_ack_sent - fc->last_pid_recv; + cs++; + } + + /* Make sure there is no overflow */ + if (allowed > buffer) + { + GNUNET_break (0); + return; + } + + /* Authorize connections to send more data */ + to_allow = buffer - allowed; + + for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next) + { + allow_per_connection = to_allow/cs; + to_allow -= allow_per_connection; + cs--; + fc = fwd ? &c->fwd_fc : &c->bck_fc; + if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3) + { + continue; + } + connection_send_ack (c, allow_per_connection, fwd); + } + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Channel send connection %s ack on %s:%X\n", + fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid); + GNUNET_break (to_allow == 0); +} + + +/** + * Destroy a reliable message after it has been acknowledged, either by + * direct mid ACK or bitfield. Updates the appropriate data structures and + * timers and frees all memory. + * + * @param copy Message that is no longer needed: remote peer got it. + */ +static void +rel_message_free (struct MeshReliableMessage *copy) +{ + struct MeshChannelReliability *rel; + struct GNUNET_TIME_Relative time; + + rel = copy->rel; + time = GNUNET_TIME_absolute_get_duration (copy->timestamp); + rel->expected_delay.rel_value_us *= 7; + rel->expected_delay.rel_value_us += time.rel_value_us; + rel->expected_delay.rel_value_us /= 8; + rel->n_sent--; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n", + GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n", + GNUNET_STRINGS_relative_time_to_string (rel->expected_delay, + GNUNET_NO)); + rel->retry_timer = rel->expected_delay; + GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy); + GNUNET_free (copy); +} + + + +/** + * Channel was ACK'd by remote peer, mark as ready and cancel retransmission. + * + * @param ch Channel to mark as ready. + * @param fwd Was the CREATE message sent fwd? + */ +static void +channel_confirm (struct MeshChannel *ch, int fwd) +{ + struct MeshChannelReliability *rel; + struct MeshReliableMessage *copy; + struct MeshReliableMessage *next; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " channel confirm %s %s:%X\n", + fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid); + ch->state = MESH_CHANNEL_READY; + + rel = fwd ? ch->root_rel : ch->dest_rel; + for (copy = rel->head_sent; NULL != copy; copy = next) + { + struct GNUNET_MessageHeader *msg; + + next = copy->next; + msg = (struct GNUNET_MessageHeader *) ©[1]; + if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE) + { + rel_message_free (copy); + /* TODO return? */ + } + } + send_ack (NULL, ch, fwd); +} + + +/** + * Save a copy to retransmit in case it gets lost. + * + * Initializes all needed callbacks and timers. + * + * @param ch Channel this message goes on. + * @param msg Message to copy. + * @param fwd Is this fwd traffic? + */ +static void +channel_save_copy (struct MeshChannel *ch, + const struct GNUNET_MessageHeader *msg, + int fwd) +{ + struct MeshChannelReliability *rel; + struct MeshReliableMessage *copy; + uint32_t mid; + uint16_t type; + uint16_t size; + + rel = fwd ? ch->root_rel : ch->dest_rel; + mid = rel->mid_send; + type = ntohs (msg->type); + size = ntohs (msg->size); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid); + copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size); + copy->mid = mid; + copy->timestamp = GNUNET_TIME_absolute_get (); + copy->rel = rel; + copy->type = type; + memcpy (©[1], msg, size); + rel->n_sent++; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent); + GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy); + if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task) + { + rel->retry_timer = + GNUNET_TIME_relative_multiply (rel->expected_delay, + MESH_RETRANSMIT_MARGIN); + rel->retry_task = + GNUNET_SCHEDULER_add_delayed (rel->retry_timer, + &channel_retransmit_message, + rel); + } +} + + + +/** + * Send a buffered message to the client, for in order delivery or + * as result of client ACK. + * + * @param ch Channel on which to empty the message buffer. + * @param c Client to send to. + * @param rel Reliability structure to corresponding peer. + * If rel == bck_rel, this is FWD data. + */ +static void +send_client_buffered_data (struct MeshChannel *ch, + struct MeshClient *c, + int fwd) +{ + struct MeshReliableMessage *copy; + struct MeshChannelReliability *rel; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n"); + rel = fwd ? ch->dest_rel : ch->root_rel; + if (GNUNET_NO == rel->client_ready) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n"); + return; + } + + copy = rel->head_recv; + /* We never buffer channel management messages */ + if (NULL != copy) + { + if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable) + { + struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1]; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " have %u! now expecting %u\n", + copy->mid, rel->mid_recv + 1); + send_client_data (ch, msg, fwd); + rel->n_recv--; + rel->mid_recv++; + GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); + GNUNET_free (copy); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " reliable && don't have %u, next is %u\n", + rel->mid_recv, + copy->mid); + return; + } + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n"); +} + + + + +/** + * Destroy a channel and free all resources. + * + * @param ch Channel to destroy. + */ +static void +channel_destroy (struct MeshChannel *ch) +{ + struct MeshClient *c; + + if (NULL == ch) + return; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n", + peer2s (ch->t->peer), ch->gid); + channel_debug (ch); + + c = ch->root; + if (NULL != c) + { + if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels, + ch->lid_root, ch)) + { + GNUNET_break (0); + } + } + + c = ch->dest; + if (NULL != c) + { + if (GNUNET_YES != + GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels, + ch->lid_dest, ch)) + { + GNUNET_break (0); + } + } + + channel_rel_free_all (ch->root_rel); + channel_rel_free_all (ch->dest_rel); + + GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch); + GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO); + + GNUNET_free (ch); +} + + +/** + * Create a new channel. + * + * @param t Tunnel this channel is in. + * @param owner Client that owns the channel, NULL for foreign channels. + * @param lid_root Local ID for root client. + * + * @return A new initialized channel. NULL on error. + */ +static struct MeshChannel * +channel_new (struct MeshTunnel2 *t, + struct MeshClient *owner, MESH_ChannelNumber lid_root) +{ + struct MeshChannel *ch; + + ch = GNUNET_new (struct MeshChannel); + ch->root = owner; + ch->lid_root = lid_root; + ch->t = t; + + GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO); + + if (NULL != owner) + { + while (NULL != channel_get (t, t->next_chid)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n", + t->next_chid, channel_get (t, t->next_chid)); + t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI; + } + ch->gid = t->next_chid; + t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI; + + if (GNUNET_OK != + GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) + { + GNUNET_break (0); + channel_destroy (ch); + GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR); + return NULL; + } + } + GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch); + + return ch; +} + + +/** + * Set options in a channel, extracted from a bit flag field + * + * @param ch Channel to set options to. + * @param options Bit array in host byte order. + */ +static void +channel_set_options (struct MeshChannel *ch, uint32_t options) +{ + ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ? + GNUNET_YES : GNUNET_NO; + ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ? + GNUNET_YES : GNUNET_NO; +} + + +/** + * Iterator for deleting each channel whose client endpoint disconnected. + * + * @param cls Closure (client that has disconnected). + * @param key The local channel id (used to access the hashmap). + * @param value The value stored at the key (channel to destroy). + * + * @return GNUNET_OK, keep iterating. + */ +static int +channel_destroy_iterator (void *cls, + uint32_t key, + void *value) +{ + struct MeshChannel *ch = value; + struct MeshClient *c = cls; + struct MeshTunnel2 *t; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " Channel %X (%X / %X) destroy, due to client %u shutdown.\n", + ch->gid, ch->lid_root, ch->lid_dest, c->id); + channel_debug (ch); + + if (c == ch->dest) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id); + } + if (c == ch->root) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id); + } + + t = ch->t; + channel_send_destroy (ch); + channel_destroy (ch); + tunnel_destroy_if_empty (t); + + return GNUNET_OK; +} + + +/** + * Sends an already built message on a channel, properly registering + * all used resources and encrypting the message with the tunnel's key. + * + * @param message Message to send. Function makes a copy of it. + * @param ch Channel on which this message is transmitted. + * @param fwd Is this a fwd message? + */ +static void +send (const struct GNUNET_MessageHeader *message, + struct MeshChannel *ch, int fwd) +{ + struct GNUNET_MESH_Encrypted *msg; + size_t size = ntohs (message->size); + char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size]; + uint16_t type; + uint64_t iv; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n", + peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", + GNUNET_MESH_DEBUG_M2S (ntohs (message->type))); + + if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid) + { + handle_decrypted (ch->t, message, fwd); + return; + } + + type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK; + iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); + + msg = (struct GNUNET_MESH_Encrypted *) cbuf; + msg->header.type = htons (type); + msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size); + msg->iv = GNUNET_htonll (iv); + tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd); + send_prebuilt_message_tunnel (msg, ch->t, ch, fwd); +} + + + +/** + * Notify the destination client that a new incoming channel was created. + * + * @param ch Channel that was created. + */ +void +GMCH_send_channel_create (struct MeshChannel *ch) +{ + struct GNUNET_MESH_ChannelMessage msg; + struct MeshTunnel2 *t = ch->t; + uint32_t opt; + + if (NULL == ch->dest) + return; + + opt = 0; + opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0; + opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0; + GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt, + GNUNET_PEER_resolve2 (t->peer->id)); + +} + +/** + * Notify a client that the channel is no longer valid. + * + * @param ch Channel that is destroyed. + * @param fwd Forward notification (owner->dest)? + */ +void +GMCH_send_channel_destroy (struct MeshChannel *ch, int fwd) +{ + struct GNUNET_MeshClient *c = fwd ? ch->dest : ch->root; + uint32_t id = fwd ? ch->lid_dest : ch->lid_root; + + if (NULL == c) + return; + + GML_send_channel_destroy (c, id); +} + + +/** + * Send data on a channel. + * + * If the destination is local, send it to client, otherwise encrypt and + * send to next hop. + * + * @param ch Channel + * @param msg Message. + * @param fwd Is this a fwd (root->dest) message? + */ +void +GMCH_send_data (struct MeshChannel *ch, + const struct GNUNET_MESH_Data *msg, + int fwd) +{ +} + + +/** + * Send an end-to-end ACK message for the most recent in-sequence payload. + * + * If channel is not reliable, do nothing. + * + * @param ch Channel this is about. + * @param fwd Is for FWD traffic? (ACK dest->owner) + */ +static void +GMCH_send_ack (struct MeshChannel *ch, int fwd) +{ + struct GNUNET_MESH_DataACK msg; + struct MeshChannelReliability *rel; + struct MeshReliableMessage *copy; + unsigned int delta; + uint64_t mask; + uint16_t type; + + if (GNUNET_NO == ch->reliable) + { + return; + } + rel = fwd ? ch->dest_rel : ch->root_rel; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "send_data_ack for %u\n", + rel->mid_recv - 1); + + type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK; + msg.header.type = htons (type); + msg.header.size = htons (sizeof (msg)); + msg.chid = htonl (ch->gid); + msg.mid = htonl (rel->mid_recv - 1); + msg.futures = 0; + for (copy = rel->head_recv; NULL != copy; copy = copy->next) + { + if (copy->type != type) + continue; + delta = copy->mid - rel->mid_recv; + if (63 < delta) + break; + mask = 0x1LL << delta; + msg.futures |= mask; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " setting bit for %u (delta %u) (%llX) -> %llX\n", + copy->mid, delta, mask, msg.futures); + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures); + + send_prebuilt_message_channel (&msg.header, ch, fwd); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n"); +} + + +/** + * Log channel info. + * + * @param ch Channel. + */ +void +GMCH_debug (struct MeshChannel *ch) +{ + if (NULL == ch) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n"); + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n", + peer2s (ch->t->peer), ch->gid, ch); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n", + ch->root, ch->root_rel); + if (NULL != ch->root) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n", + ch->root_rel->client_ready ? "YES" : "NO"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root); + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " dest %p/%p\n", + ch->dest, ch->dest_rel); + if (NULL != ch->dest) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n", + ch->dest_rel->client_ready ? "YES" : "NO"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest); + } +} + + diff --git a/src/mesh/gnunet-service-mesh_channel.h b/src/mesh/gnunet-service-mesh_channel.h new file mode 100644 index 000000000..4886e4a09 --- /dev/null +++ b/src/mesh/gnunet-service-mesh_channel.h @@ -0,0 +1,109 @@ +/* + This file is part of GNUnet. + (C) 2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file mesh/gnunet-service-mesh_channel.h + * @brief mesh service; dealing with end-to-end channels + * @author Bartlomiej Polot + * + * All functions in this file should use the prefix GMCH (Gnunet Mesh CHannel) + */ + +#ifndef GNUNET_SERVICE_MESH_CHANNEL_H +#define GNUNET_SERVICE_MESH_CHANNEL_H + +#ifdef __cplusplus +extern "C" +{ +#if 0 /* keep Emacsens' auto-indent happy */ +} +#endif +#endif + +#include "gnunet_util_lib.h" + +/** + * Struct containing all information regarding a channel to a remote client. + */ +struct MeshChannel; + +/** + * Send an end-to-end ACK message for the most recent in-sequence payload. + * + * If channel is not reliable, do nothing. + * + * @param ch Channel this is about. + * @param fwd Is for FWD traffic? (ACK dest->owner) + */ +void +GMCH_send_ack (struct MeshChannel *ch, int fwd); + +/** + * Send data on a channel. + * + * If the destination is local, send it to client, otherwise encrypt and + * send to next hop. + * + * @param ch Channel + * @param msg Message. + * @param fwd Is this a fwd (root->dest) message? + */ +void +GMCH_send_data (struct MeshChannel *ch, + const struct GNUNET_MESH_Data *msg, + int fwd); + +/** + * Notify the destination client that a new incoming channel was created. + * + * @param ch Channel that was created. + */ +void +GMCH_send_channel_create (struct MeshChannel *ch); + + +/** + * Notify a client that the channel is no longer valid. + * + * @param ch Channel that is destroyed. + * @param fwd Forward notification (owner->dest)? + */ +void +GMCH_send_channel_destroy (struct MeshChannel *ch, int fwd); + + +/** + * Log channel info. + * + * @param ch Channel. + */ +void +GMCH_debug (struct MeshChannel *ch); + +#if 0 /* keep Emacsens' auto-indent happy */ +{ +#endif +#ifdef __cplusplus +} +#endif + +/* ifndef GNUNET_SERVICE_MESH_CHANNEL_H */ +#endif +/* end of gnunet-service-mesh_channel.h */ \ No newline at end of file diff --git a/src/mesh/gnunet-service-mesh_connection.c b/src/mesh/gnunet-service-mesh_connection.c new file mode 100644 index 000000000..93ba8766d --- /dev/null +++ b/src/mesh/gnunet-service-mesh_connection.c @@ -0,0 +1,1583 @@ +/* + This file is part of GNUnet. + (C) 2001-2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file mesh/gnunet-service-mesh_connection.c + * @brief GNUnet MESH service connection handling + * @author Bartlomiej Polot + */ + +#include "platform.h" +#include "gnunet_core_service.h" +#include "gnunet-service-mesh_connection.h" + + + + +/** + * All the states a connection can be in. + */ +enum MeshConnectionState +{ + /** + * Uninitialized status, should never appear in operation. + */ + MESH_CONNECTION_NEW, + + /** + * Connection create message sent, waiting for ACK. + */ + MESH_CONNECTION_SENT, + + /** + * Connection ACK sent, waiting for ACK. + */ + MESH_CONNECTION_ACK, + + /** + * Connection confirmed, ready to carry traffic. + */ + MESH_CONNECTION_READY, +}; + + + +/** + * Struct containing info about a queued transmission to this peer + */ +struct MeshPeerQueue +{ + /** + * DLL next + */ + struct MeshPeerQueue *next; + + /** + * DLL previous + */ + struct MeshPeerQueue *prev; + + /** + * Peer this transmission is directed to. + */ + struct MeshPeer *peer; + + /** + * Connection this message belongs to. + */ + struct MeshConnection *c; + + /** + * Is FWD in c? + */ + int fwd; + + /** + * Channel this message belongs to, if known. + */ + struct MeshChannel *ch; + + /** + * Pointer to info stucture used as cls. + */ + void *cls; + + /** + * Type of message + */ + uint16_t type; + + /** + * Size of the message + */ + size_t size; +}; + + +/** + * Struct to encapsulate all the Flow Control information to a peer to which + * we are directly connected (on a core level). + */ +struct MeshFlowControl +{ + /** + * Connection this controls. + */ + struct MeshConnection *c; + + /** + * How many messages are in the queue on this connection. + */ + unsigned int queue_n; + + /** + * How many messages do we accept in the queue. + */ + unsigned int queue_max; + + /** + * Next ID to use. + */ + uint32_t next_pid; + + /** + * ID of the last packet sent towards the peer. + */ + uint32_t last_pid_sent; + + /** + * ID of the last packet received from the peer. + */ + uint32_t last_pid_recv; + + /** + * Last ACK sent to the peer (peer can't send more than this PID). + */ + uint32_t last_ack_sent; + + /** + * Last ACK sent towards the origin (for traffic towards leaf node). + */ + uint32_t last_ack_recv; + + /** + * Task to poll the peer in case of a lost ACK causes stall. + */ + GNUNET_SCHEDULER_TaskIdentifier poll_task; + + /** + * How frequently to poll for ACKs. + */ + struct GNUNET_TIME_Relative poll_time; +}; + + +/** + * Struct containing all information regarding a connection to a peer. + */ +struct MeshConnection +{ + /** + * DLL + */ + struct MeshConnection *next; + struct MeshConnection *prev; + + /** + * Tunnel this connection is part of. + */ + struct MeshTunnel2 *t; + + /** + * Flow control information for traffic fwd. + */ + struct MeshFlowControl fwd_fc; + + /** + * Flow control information for traffic bck. + */ + struct MeshFlowControl bck_fc; + + /** + * ID of the connection. + */ + struct GNUNET_HashCode id; + + /** + * State of the connection. + */ + enum MeshConnectionState state; + + /** + * Path being used for the tunnel. + */ + struct MeshPeerPath *path; + + /** + * Position of the local peer in the path. + */ + unsigned int own_pos; + + /** + * Task to keep the used paths alive at the owner, + * time tunnel out on all the other peers. + */ + GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task; + + /** + * Task to keep the used paths alive at the destination, + * time tunnel out on all the other peers. + */ + GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task; + + /** + * Pending message count. + */ + int pending_messages; + + /** + * Destroy flag: if true, destroy on last message. + */ + int destroy; +}; + + + + + + +/** + * Connections known, indexed by cid (MeshConnection). + */ +static struct GNUNET_CONTAINER_MultiHashMap *connections; + +/** + * How many connections are we willing to maintain. + * Local connections are always allowed, even if there are more connections than max. + */ +static unsigned long long max_connections; + +/** + * How many messages *in total* are we willing to queue, divide by number of + * connections to get connection queue size. + */ +static unsigned long long max_msgs_queue; + +/** + * How often to send path keepalives. Paths timeout after 4 missed. + */ +static struct GNUNET_TIME_Relative refresh_connection_time; + + + + +/** + * Initialize a Flow Control structure to the initial state. + * + * @param fc Flow Control structure to initialize. + */ +static void +fc_init (struct MeshFlowControl *fc) +{ + fc->next_pid = 0; + fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */ + fc->last_pid_recv = (uint32_t) -1; + fc->last_ack_sent = (uint32_t) 0; + fc->last_ack_recv = (uint32_t) 0; + fc->poll_task = GNUNET_SCHEDULER_NO_TASK; + fc->poll_time = GNUNET_TIME_UNIT_SECONDS; + fc->queue_n = 0; + fc->queue_max = (max_msgs_queue / max_connections) + 1; +} + + +/** + * Find a connection. + * + * @param cid Connection ID. + */ +static struct MeshConnection * +connection_get (const struct GNUNET_HashCode *cid) +{ + return GNUNET_CONTAINER_multihashmap_get (connections, cid); +} + + +/** + * Get first sendable message. + * + * @param peer The destination peer. + * + * @return Best current known path towards the peer, if any. + */ +static struct MeshPeerQueue * +peer_get_first_message (const struct MeshPeer *peer) +{ + struct MeshPeerQueue *q; + + for (q = peer->queue_head; NULL != q; q = q->next) + { + if (queue_is_sendable (q)) + return q; + } + + return NULL; +} + + +static int +queue_is_sendable (struct MeshPeerQueue *q) +{ + struct MeshFlowControl *fc; + + /* Is PID-independent? */ + switch (q->type) + { + case GNUNET_MESSAGE_TYPE_MESH_ACK: + case GNUNET_MESSAGE_TYPE_MESH_POLL: + return GNUNET_YES; + } + + /* Is PID allowed? */ + fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc; + if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent)) + return GNUNET_YES; + + return GNUNET_NO; +} + + + +/** + * Free a transmission that was already queued with all resources + * associated to the request. + * + * @param queue Queue handler to cancel. + * @param clear_cls Is it necessary to free associated cls? + */ +static void +queue_destroy (struct MeshPeerQueue *queue, int clear_cls) +{ + struct MeshPeer *peer; + struct MeshFlowControl *fc; + int fwd; + + fwd = queue->fwd; + peer = queue->peer; + GNUNET_assert (NULL != queue->c); + fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc; + + if (GNUNET_YES == clear_cls) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n", + GNUNET_MESH_DEBUG_M2S (queue->type)); + switch (queue->type) + { + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY: + case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n"); + GNUNET_break (GNUNET_YES == queue->c->destroy); + /* fall through */ + case GNUNET_MESSAGE_TYPE_MESH_FWD: + case GNUNET_MESSAGE_TYPE_MESH_BCK: + case GNUNET_MESSAGE_TYPE_MESH_ACK: + case GNUNET_MESSAGE_TYPE_MESH_POLL: + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK: + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE: + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN: + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");; + GNUNET_free_non_null (queue->cls); + break; + + default: + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n", + GNUNET_MESH_DEBUG_M2S (queue->type)); + } + + } + GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue); + + if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK && + queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n); + fc->queue_n--; + peer->queue_n--; + } + if (NULL != queue->c) + { + queue->c->pending_messages--; + if (NULL != queue->c->t) + { + queue->c->t->pending_messages--; + } + } + + GNUNET_free (queue); +} + + + +static size_t +queue_send (void *cls, size_t size, void *buf) +{ + struct MeshPeer *peer = cls; + struct MeshFlowControl *fc; + struct MeshConnection *c; + struct GNUNET_MessageHeader *msg; + struct MeshPeerQueue *queue; + struct MeshTunnel2 *t; + struct MeshChannel *ch; + const struct GNUNET_PeerIdentity *dst_id; + size_t data_size; + uint32_t pid; + uint16_t type; + int fwd; + + peer->core_transmit = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size); + + if (NULL == buf || 0 == size) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n"); + return 0; + } + + /* Initialize */ + queue = peer_get_first_message (peer); + if (NULL == queue) + { + GNUNET_break (0); /* Core tmt_rdy should've been canceled */ + return 0; + } + c = queue->c; + fwd = queue->fwd; + fc = fwd ? &c->fwd_fc : &c->bck_fc; + + dst_id = GNUNET_PEER_resolve2 (peer->id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id)); + /* Check if buffer size is enough for the message */ + if (queue->size > size) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n"); + peer->core_transmit = + GNUNET_CORE_notify_transmit_ready (core_handle, + GNUNET_NO, + 0, + GNUNET_TIME_UNIT_FOREVER_REL, + dst_id, + queue->size, + &queue_send, + peer); + return 0; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size); + + t = (NULL != c) ? c->t : NULL; + type = 0; + + /* Fill buf */ + switch (queue->type) + { + case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY: + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN: + case GNUNET_MESSAGE_TYPE_MESH_FWD: + case GNUNET_MESSAGE_TYPE_MESH_BCK: + case GNUNET_MESSAGE_TYPE_MESH_ACK: + case GNUNET_MESSAGE_TYPE_MESH_POLL: + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "* raw: %s\n", + GNUNET_MESH_DEBUG_M2S (queue->type)); + data_size = send_core_data_raw (queue->cls, size, buf); + msg = (struct GNUNET_MessageHeader *) buf; + type = ntohs (msg->type); + break; + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE: + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n"); + if (GMC_is_origin (c, GNUNET_YES)) + data_size = send_core_connection_create (queue->c, size, buf); + else + data_size = send_core_data_raw (queue->cls, size, buf); + break; + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK: + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n"); + if (GMC_is_origin (c, GNUNET_NO) || + GMC_is_origin (c, GNUNET_YES)) + data_size = send_core_connection_ack (queue->c, size, buf); + else + data_size = send_core_data_raw (queue->cls, size, buf); + break; + case GNUNET_MESSAGE_TYPE_MESH_DATA: + case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE: + case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: + /* This should be encapsulted */ + GNUNET_break (0); + data_size = 0; + break; + default: + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n", + queue->type); + data_size = 0; + } + + if (0 < drop_percent && + GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Dropping message of type %s\n", + GNUNET_MESH_DEBUG_M2S (queue->type)); + data_size = 0; + } + + /* Free queue, but cls was freed by send_core_* */ + ch = queue->ch; + queue_destroy (queue, GNUNET_NO); + + /* Send ACK if needed, after accounting for sent ID in fc->queue_n */ + switch (type) + { + case GNUNET_MESSAGE_TYPE_MESH_FWD: + case GNUNET_MESSAGE_TYPE_MESH_BCK: + pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid ); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid); + fc->last_pid_sent = pid; + send_ack (c, ch, fwd); + break; + default: + break; + } + + /* If more data in queue, send next */ + queue = peer_get_first_message (peer); + if (NULL != queue) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n"); + if (NULL == peer->core_transmit) { + peer->core_transmit = + GNUNET_CORE_notify_transmit_ready(core_handle, + 0, + 0, + GNUNET_TIME_UNIT_FOREVER_REL, + dst_id, + queue->size, + &queue_send, + peer); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "* tmt rdy called somewhere else\n"); + } + if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n"); + fc->poll_task = + GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc); + } + } + else + { + if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task) + { + GNUNET_SCHEDULER_cancel (fc->poll_task); + fc->poll_task = GNUNET_SCHEDULER_NO_TASK; + } + } + if (NULL != c) + { + c->pending_messages--; + if (GNUNET_YES == c->destroy && 0 == c->pending_messages) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n"); + GMC_destroy (c); + } + } + + if (NULL != t) + { + t->pending_messages--; + if (GNUNET_YES == t->destroy && 0 == t->pending_messages) + { +// GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n"); + tunnel_destroy (t); + } + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size); + return data_size; +} + + + +static void +queue_add (void *cls, uint16_t type, size_t size, + struct MeshConnection *c, + struct MeshChannel *ch, + int fwd) +{ + struct MeshPeerQueue *queue; + struct MeshFlowControl *fc; + struct MeshPeer *peer; + int priority; + int call_core; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "queue add %s %s (%u) on c %p, ch %p\n", + fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch); + GNUNET_assert (NULL != c); + + fc = fwd ? &c->fwd_fc : &c->bck_fc; + peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c); + + if (NULL == fc) + { + GNUNET_break (0); + return; + } + + if (NULL == peer->connections) + { + /* We are not connected to this peer, ignore request. */ + GNUNET_break_op (0); + return; + } + + priority = 0; + + if (GNUNET_MESSAGE_TYPE_MESH_POLL == type || + GNUNET_MESSAGE_TYPE_MESH_ACK == type) + { + priority = 100; + } + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc); + if (fc->queue_n >= fc->queue_max && 0 == priority) + { + GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)", + 1, GNUNET_NO); + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "queue full: %u/%u\n", + fc->queue_n, fc->queue_max); + return; /* Drop this message */ + } + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv); + if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv)) + { + call_core = GNUNET_NO; + if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && + GNUNET_MESSAGE_TYPE_MESH_POLL != type) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "no buffer space (%u > %u): starting poll\n", + fc->last_pid_sent + 1, fc->last_ack_recv); + fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, + &connection_poll, + fc); + } + } + else + call_core = GNUNET_YES; + queue = GNUNET_malloc (sizeof (struct MeshPeerQueue)); + queue->cls = cls; + queue->type = type; + queue->size = size; + queue->peer = peer; + queue->c = c; + queue->ch = ch; + queue->fwd = fwd; + if (100 <= priority) + { + struct MeshPeerQueue *copy; + struct MeshPeerQueue *next; + + for (copy = peer->queue_head; NULL != copy; copy = next) + { + next = copy->next; + if (copy->type == type && copy->c == c && copy->fwd == fwd) + { + /* Example: also a FWD ACK for connection XYZ */ + queue_destroy (copy, GNUNET_YES); + } + } + GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue); + } + else + { + GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n); + fc->queue_n++; + peer->queue_n++; + } + + if (NULL == peer->core_transmit && GNUNET_YES == call_core) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "calling core tmt rdy towards %s for %u bytes\n", + peer2s (peer), size); + peer->core_transmit = + GNUNET_CORE_notify_transmit_ready (core_handle, + 0, + 0, + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_PEER_resolve2 (peer->id), + size, + &queue_send, + peer); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "core tmt rdy towards %s already called\n", + peer2s (peer)); + + } + c->pending_messages++; + if (NULL != c->t) + c->t->pending_messages++; +} + + + + +/** + * Sends an already built message on a connection, properly registering + * all used resources. + * + * @param message Message to send. Function makes a copy of it. + * If message is not hop-by-hop, decrements TTL of copy. + * @param c Connection on which this message is transmitted. + * @param ch Channel on which this message is transmitted, or NULL. + * @param fwd Is this a fwd message? + */ +static void +send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message, + struct MeshConnection *c, + struct MeshChannel *ch, + int fwd) +{ + void *data; + size_t size; + uint16_t type; + + size = ntohs (message->size); + data = GNUNET_malloc (size); + memcpy (data, message, size); + type = ntohs (message->type); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n", + GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id)); + + switch (type) + { + struct GNUNET_MESH_Encrypted *emsg; + struct GNUNET_MESH_ACK *amsg; + struct GNUNET_MESH_Poll *pmsg; + struct GNUNET_MESH_ConnectionDestroy *dmsg; + struct GNUNET_MESH_ConnectionBroken *bmsg; + uint32_t ttl; + + case GNUNET_MESSAGE_TYPE_MESH_FWD: + case GNUNET_MESSAGE_TYPE_MESH_BCK: + emsg = (struct GNUNET_MESH_Encrypted *) data; + ttl = ntohl (emsg->ttl); + if (0 == ttl) + { + GNUNET_break_op (0); + return; + } + emsg->cid = c->id; + emsg->ttl = htonl (ttl - 1); + emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid)); + break; + + case GNUNET_MESSAGE_TYPE_MESH_ACK: + amsg = (struct GNUNET_MESH_ACK *) data; + amsg->cid = c->id; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack)); + break; + + case GNUNET_MESSAGE_TYPE_MESH_POLL: + pmsg = (struct GNUNET_MESH_Poll *) data; + pmsg->cid = c->id; + pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid)); + break; + + case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: + dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data; + dmsg->cid = c->id; + dmsg->reserved = 0; + break; + + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN: + bmsg = (struct GNUNET_MESH_ConnectionBroken *) data; + bmsg->cid = c->id; + bmsg->reserved = 0; + break; + + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE: + case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK: + break; + + default: + GNUNET_break (0); + } + + queue_add (data, + type, + size, + c, + ch, + fwd); +} + + + + +struct MeshConnection * +GMC_new (const struct GNUNET_HashCode *cid) +{ + struct MeshConnection *c; + + c = GNUNET_new (struct MeshConnection); + c->id = *cid; + GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); + fc_init (&c->fwd_fc); + fc_init (&c->bck_fc); + c->fwd_fc.c = c; + c->bck_fc.c = c; + + return c; +} + + +static void +GMC_destroy (struct MeshConnection *c) +{ + struct MeshPeer *peer; + + if (NULL == c) + return; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n", + peer2s (c->t->peer), + c->id); + + /* Cancel all traffic */ + connection_cancel_queues (c, GNUNET_YES); + connection_cancel_queues (c, GNUNET_NO); + + /* Cancel maintainance task (keepalive/timeout) */ + if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task) + GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task); + if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task) + GNUNET_SCHEDULER_cancel (c->bck_maintenance_task); + + /* Deregister from neighbors */ + peer = connection_get_next_hop (c); + if (NULL != peer && NULL != peer->connections) + GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c); + peer = connection_get_prev_hop (c); + if (NULL != peer && NULL != peer->connections) + GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c); + + /* Delete */ + GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO); + GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c); + GNUNET_free (c); +} + + + +/** + * Send an ACK informing the predecessor about the available buffer space. + * + * Note that for fwd ack, the FWD mean forward *traffic* (root->dest), + * the ACK itself goes "back" (dest->root). + * + * @param c Connection on which to send the ACK. + * @param buffer How much space free to advertise? + * @param fwd Is this FWD ACK? (Going dest->owner) + */ +static void +connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd) +{ + struct MeshFlowControl *next_fc; + struct MeshFlowControl *prev_fc; + struct GNUNET_MESH_ACK msg; + uint32_t ack; + int delta; + + next_fc = fwd ? &c->fwd_fc : &c->bck_fc; + prev_fc = fwd ? &c->bck_fc : &c->fwd_fc; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "connection send %s ack on %s\n", + fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id)); + + /* Check if we need to transmit the ACK */ + if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " last pid recv: %u, last ack sent: %u\n", + prev_fc->last_pid_recv, prev_fc->last_ack_sent); + return; + } + + /* Ok, ACK might be necessary, what PID to ACK? */ + delta = next_fc->queue_max - next_fc->queue_n; + ack = prev_fc->last_pid_recv + delta; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " last pid %u, last ack %u, qmax %u, q %u\n", + prev_fc->last_pid_recv, prev_fc->last_ack_sent, + next_fc->queue_max, next_fc->queue_n); + if (ack == prev_fc->last_ack_sent) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n"); + return; + } + + prev_fc->last_ack_sent = ack; + + /* Build ACK message and send on connection */ + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK); + msg.ack = htonl (ack); + msg.cid = c->id; + + send_prebuilt_message_connection (&msg.header, c, NULL, !fwd); +} + + +static void +connection_change_state (struct MeshConnection* c, + enum MeshConnectionState state) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Connection %s state was %s\n", + GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Connection %s state is now %s\n", + GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state)); + c->state = state; +} + + + +/** + * Send keepalive packets for a connection. + * + * @param c Connection to keep alive.. + * @param fwd Is this a FWD keepalive? (owner -> dest). + */ +static void +connection_keepalive (struct MeshConnection *c, int fwd) +{ + struct GNUNET_MESH_ConnectionKeepAlive *msg; + size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive); + char cbuf[size]; + uint16_t type; + + type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE : + GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "sending %s keepalive for connection %s[%d]\n", + fwd ? "FWD" : "BCK", + peer2s (c->t->peer), + c->id); + + msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf; + msg->header.size = htons (size); + msg->header.type = htons (type); + msg->cid = c->id; + + send_prebuilt_message_connection (&msg->header, c, NULL, fwd); +} + + +/** + * Send CONNECTION_{CREATE/ACK} packets for a connection. + * + * @param c Connection for which to send the message. + * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK. + */ +static void +connection_recreate (struct MeshConnection *c, int fwd) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n"); + if (fwd) + send_connection_create (c); + else + send_connection_ack (c, GNUNET_NO); +} + + +/** + * Generic connection timer management. + * Depending on the role of the peer in the connection will send the + * appropriate message (build or keepalive) + * + * @param c Conncetion to maintain. + * @param fwd Is FWD? + */ +static void +connection_maintain (struct MeshConnection *c, int fwd) +{ + if (MESH_TUNNEL_SEARCHING == c->t->state) + { + /* TODO DHT GET with RO_BART */ + return; + } + switch (c->state) + { + case MESH_CONNECTION_NEW: + GNUNET_break (0); + case MESH_CONNECTION_SENT: + connection_recreate (c, fwd); + break; + case MESH_CONNECTION_READY: + connection_keepalive (c, fwd); + break; + default: + break; + } +} + + +static void +connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshConnection *c = cls; + + c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + + connection_maintain (c, GNUNET_YES); + c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, + &connection_fwd_keepalive, + c); +} + + +static void +connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshConnection *c = cls; + + c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + + connection_maintain (c, GNUNET_NO); + c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, + &connection_bck_keepalive, + c); +} + + +/** + * Send a message to all peers in this connection that the connection + * is no longer valid. + * + * If some peer should not receive the message, it should be zero'ed out + * before calling this function. + * + * @param c The connection whose peers to notify. + */ +static void +connection_send_destroy (struct MeshConnection *c) +{ + struct GNUNET_MESH_ConnectionDestroy msg; + + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);; + msg.cid = c->id; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " sending connection destroy for connection %s[%X]\n", + peer2s (c->t->peer), + c->id); + + if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES)) + send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES); + if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO)) + send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO); + c->destroy = GNUNET_YES; +} + + +/** + * Get free buffer space in a connection. + * + * @param c Connection. + * @param fwd Is query about FWD traffic? + * + * @return Free buffer space [0 - max_msgs_queue/max_connections] + */ +static unsigned int +connection_get_buffer (struct MeshConnection *c, int fwd) +{ + struct MeshFlowControl *fc; + + fc = fwd ? &c->fwd_fc : &c->bck_fc; + + return (fc->queue_max - fc->queue_n); +} + + +/** + * Get the first transmittable message for a connection. + * + * @param c Connection. + * @param fwd Is this FWD? + * + * @return First transmittable message. + */ +static struct MeshPeerQueue * +connection_get_first_message (struct MeshConnection *c, int fwd) +{ + struct MeshPeerQueue *q; + struct MeshPeer *p; + + p = connection_get_hop (c, fwd); + + for (q = p->queue_head; NULL != q; q = q->next) + { + if (q->c != c) + continue; + if (queue_is_sendable (q)) + return q; + } + + return NULL; +} + + +/** + * @brief Re-initiate traffic on this connection if necessary. + * + * Check if there is traffic queued towards this peer + * and the core transmit handle is NULL (traffic was stalled). + * If so, call core tmt rdy. + * + * @param c Connection on which initiate traffic. + * @param fwd Is this about fwd traffic? + */ +static void +connection_unlock_queue (struct MeshConnection *c, int fwd) +{ + struct MeshPeer *peer; + struct MeshPeerQueue *q; + size_t size; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "connection_unlock_queue %s on %s\n", + fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id)); + + if (GMC_is_terminal (c, fwd)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n"); + return; + } + + peer = connection_get_hop (c, fwd); + + if (NULL != peer->core_transmit) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n"); + return; /* Already unlocked */ + } + + q = connection_get_first_message (c, fwd); + if (NULL == q) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n"); + return; /* Nothing to transmit */ + } + + size = q->size; + peer->core_transmit = + GNUNET_CORE_notify_transmit_ready (core_handle, + GNUNET_NO, + 0, + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_PEER_resolve2 (peer->id), + size, + &queue_send, + peer); +} + + +/** + * Cancel all transmissions that belong to a certain connection. + * + * @param c Connection which to cancel. + * @param fwd Cancel fwd traffic? + */ +static void +connection_cancel_queues (struct MeshConnection *c, int fwd) +{ + struct MeshPeerQueue *q; + struct MeshPeerQueue *next; + struct MeshFlowControl *fc; + struct MeshPeer *peer; + + if (NULL == c) + { + GNUNET_break (0); + return; + } + fc = fwd ? &c->fwd_fc : &c->bck_fc; + peer = connection_get_hop (c, fwd); + + for (q = peer->queue_head; NULL != q; q = next) + { + next = q->next; + if (q->c == c) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "connection_cancel_queue %s\n", + GNUNET_MESH_DEBUG_M2S (q->type)); + queue_destroy (q, GNUNET_YES); + } + } + if (NULL == peer->queue_head) + { + if (NULL != peer->core_transmit) + { + GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit); + peer->core_transmit = NULL; + } + if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task) + { + GNUNET_SCHEDULER_cancel (fc->poll_task); + fc->poll_task = GNUNET_SCHEDULER_NO_TASK; + } + } +} + + + + +/** + * Function called if a connection has been stalled for a while, + * possibly due to a missed ACK. Poll the neighbor about its ACK status. + * + * @param cls Closure (poll ctx). + * @param tc TaskContext. + */ +static void +connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshFlowControl *fc = cls; + struct GNUNET_MESH_Poll msg; + struct MeshConnection *c; + + fc->poll_task = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + { + return; + } + + c = fc->c; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n", + GNUNET_h2s (&c->id)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n", + fc == &c->fwd_fc ? "FWD" : "BCK"); + + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL); + msg.header.size = htons (sizeof (msg)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent); + send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc); + fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time); + fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, + &connection_poll, fc); +} + + + + +/** + * Get the previous hop in a connection + * + * @param c Connection. + * + * @return Previous peer in the connection. + */ +static struct MeshPeer * +connection_get_prev_hop (struct MeshConnection *c) +{ + GNUNET_PEER_Id id; + + if (0 == c->own_pos || c->path->length < 2) + id = c->path->peers[0]; + else + id = c->path->peers[c->own_pos - 1]; + + return peer_get_short (id); +} + + +/** + * Get the next hop in a connection + * + * @param c Connection. + * + * @return Next peer in the connection. + */ +static struct MeshPeer * +connection_get_next_hop (struct MeshConnection *c) +{ + GNUNET_PEER_Id id; + + if ((c->path->length - 1) == c->own_pos || c->path->length < 2) + id = c->path->peers[c->path->length - 1]; + else + id = c->path->peers[c->own_pos + 1]; + + return peer_get_short (id); +} + + +/** + * Get the hop in a connection. + * + * @param c Connection. + * @param fwd Next hop? + * + * @return Next peer in the connection. + */ +static struct MeshPeer * +connection_get_hop (struct MeshConnection *c, int fwd) +{ + if (fwd) + return connection_get_next_hop (c); + return connection_get_prev_hop (c); +} + + + + +/** + * Timeout function due to lack of keepalive/traffic from the owner. + * Destroys connection if called. + * + * @param cls Closure (connection to destroy). + * @param tc TaskContext. + */ +static void +connection_fwd_timeout (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshConnection *c = cls; + + c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Connection %s[%X] FWD timed out. Destroying.\n", + peer2s (c->t->peer), + c->id); + + if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */ + return; + + GMC_destroy (c); +} + + +/** + * Timeout function due to lack of keepalive/traffic from the destination. + * Destroys connection if called. + * + * @param cls Closure (connection to destroy). + * @param tc TaskContext + */ +static void +connection_bck_timeout (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshConnection *c = cls; + + c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Connection %s[%X] FWD timed out. Destroying.\n", + peer2s (c->t->peer), + c->id); + + if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */ + return; + + GMC_destroy (c); +} + + +/** + * Resets the connection timeout task, some other message has done the + * task's job. + * - For the first peer on the direction this means to send + * a keepalive or a path confirmation message (either create or ACK). + * - For all other peers, this means to destroy the connection, + * due to lack of activity. + * Starts the tiemout if no timeout was running (connection just created). + * + * @param c Connection whose timeout to reset. + * @param fwd Is this forward? + * + * TODO use heap to improve efficiency of scheduler. + */ +static void +connection_reset_timeout (struct MeshConnection *c, int fwd) +{ + GNUNET_SCHEDULER_TaskIdentifier *ti; + GNUNET_SCHEDULER_Task f; + + ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task; + + if (GNUNET_SCHEDULER_NO_TASK != *ti) + GNUNET_SCHEDULER_cancel (*ti); + + if (GMC_is_origin (c, fwd)) /* Endpoint */ + { + f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive; + *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c); + } + else /* Relay */ + { + struct GNUNET_TIME_Relative delay; + + delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4); + f = fwd ? &connection_fwd_timeout : &connection_bck_timeout; + *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c); + } +} + + +/** + * Iterator to notify all connections of a broken link. Mark connections + * to destroy after all traffic has been sent. + * + * @param cls Closure (peer disconnected). + * @param key Current key code (tid). + * @param value Value in the hash map (connection). + * + * @return GNUNET_YES if we should continue to iterate, + * GNUNET_NO if not. + */ +int +GMC_notify_broken (void *cls, + const struct GNUNET_HashCode *key, + void *value) +{ + struct MeshPeer *peer = cls; + struct MeshConnection *c = value; + struct GNUNET_MESH_ConnectionBroken msg; + int fwd; + + fwd = peer == connection_get_prev_hop (c); + + connection_cancel_queues (c, !fwd); + if (GMC_is_terminal (c, fwd)) + { + /* Local shutdown, no one to notify about this. */ + GMC_destroy (c); + return GNUNET_YES; + } + + msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN); + msg.cid = c->id; + msg.peer1 = my_full_id; + msg.peer2 = *GNUNET_PEER_resolve2 (peer->id); + send_prebuilt_message_connection (&msg.header, c, NULL, fwd); + c->destroy = GNUNET_YES; + + return GNUNET_YES; +} + + +/** + * Initialize the connections subsystem + * + * @param c Configuration handle. + */ +void +GMC_init (struct GNUNET_CONFIGURATION_Handle *c) +{ + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE", + &max_msgs_queue)) + { + GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, + "MESH", "MAX_MSGS_QUEUE", "MISSING"); + GNUNET_SCHEDULER_shutdown (); + return; + } + + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS", + &max_connections)) + { + GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, + "MESH", "MAX_CONNECTIONS", "MISSING"); + GNUNET_SCHEDULER_shutdown (); + return; + } + + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME", + &refresh_connection_time)) + { + GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, + "MESH", "REFRESH_CONNECTION_TIME", "MISSING"); + GNUNET_SCHEDULER_shutdown (); + return; + } + connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES); +} + + +/** + * Is this peer the first one on the connection? + * + * @param c Connection. + * @param fwd Is this about fwd traffic? + * + * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal. + */ +int +GMC_is_origin (struct MeshConnection *c, int fwd) +{ + if (!fwd && c->path->length - 1 == c->own_pos ) + return GNUNET_YES; + if (fwd && 0 == c->own_pos) + return GNUNET_YES; + return GNUNET_NO; +} + + +/** + * Is this peer the last one on the connection? + * + * @param c Connection. + * @param fwd Is this about fwd traffic? + * Note that the ROOT is the terminal for BCK traffic! + * + * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin. + */ +int +GMC_is_terminal (struct MeshConnection *c, int fwd) +{ + return GMC_is_origin (c, !fwd); +} \ No newline at end of file diff --git a/src/mesh/gnunet-service-mesh_connection.h b/src/mesh/gnunet-service-mesh_connection.h new file mode 100644 index 000000000..a1715e3cd --- /dev/null +++ b/src/mesh/gnunet-service-mesh_connection.h @@ -0,0 +1,197 @@ +/* + This file is part of GNUnet. + (C) 2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file mesh/gnunet-service-mesh_connection.h + * @brief mesh service; dealing with connections + * @author Bartlomiej Polot + * + * All functions in this file should use the prefix GMC (Gnunet Mesh Connection) + */ + +#ifndef GNUNET_SERVICE_MESH_CONNECTION_H +#define GNUNET_SERVICE_MESH_CONNECTION_H + +#ifdef __cplusplus +extern "C" +{ +#if 0 /* keep Emacsens' auto-indent happy */ +} +#endif +#endif + +#include "gnunet_util_lib.h" + + +struct MeshConnection; + +/** + * Initialize the connections subsystem + * + * @param c Configuration handle. + */ +void +GMC_init (struct GNUNET_CONFIGURATION_Handle *c); + +/** + * Create a connection. + * + * @param cid Connection ID. + */ +struct MeshConnection * +GMC_new (const struct GNUNET_HashCode *cid); + +/** + * Connection is no longer needed: destroy it and remove from tunnel. + * + * @param c Connection to destroy. + */ +void +GMC_destroy (struct MeshConnection *c); + +/** + * Count connections in a DLL. + */ +unsigned int +GMC_count (const struct MeshConnection *head); + +/** + * Send FWD keepalive packets for a connection. + * + * @param cls Closure (connection for which to send the keepalive). + * @param tc Notification context. + */ +void +GMC_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); + +/** + * Send BCK keepalive packets for a connection. + * + * @param cls Closure (connection for which to send the keepalive). + * @param tc Notification context. + */ +void +GMC_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); + + +/** + * Change the tunnel state. + * + * @param c Connection whose state to change. + * @param state New state. + */ +void +GMC_change_state (struct MeshConnection* c, enum MeshConnectionState state); + +/** + * Iterator to notify all connections of a broken link. Mark connections + * to destroy after all traffic has been sent. + * + * @param cls Closure (peer disconnected). + * @param key Current key code (tid). + * @param value Value in the hash map (connection). + * + * @return GNUNET_YES if we should continue to iterate, + * GNUNET_NO if not. + */ +int +GMC_notify_broken (void *cls, + const struct GNUNET_HashCode *key, + void *value); + +/** + * @brief Queue and pass message to core when possible. + * + * @param cls Closure (@c type dependant). It will be used by queue_send to + * build the message to be sent if not already prebuilt. + * @param type Type of the message, 0 for a raw message. + * @param size Size of the message. + * @param c Connection this message belongs to (cannot be NULL). + * @param ch Channel this message belongs to, if applicable (otherwise NULL). + * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!) + */ +void +GMC_queue_add (void* cls, + uint16_t type, + size_t size, + struct MeshConnection* c, + struct MeshChannel* ch, + int fwd); + + +/** + * Free a transmission that was already queued with all resources + * associated to the request. + * + * @param queue Queue handler to cancel. + * @param clear_cls Is it necessary to free associated cls? + */ +void +GMC_queue_destroy (struct MeshPeerQueue *queue, int clear_cls); + + +/** + * Core callback to write a queued packet to core buffer + * + * @param cls Closure (peer info). + * @param size Number of bytes available in buf. + * @param buf Where the to write the message. + * + * @return number of bytes written to buf + */ +size_t +GMC_queue_send (void *cls, size_t size, void *buf); + + + +/** + * Is this peer the first one on the connection? + * + * @param c Connection. + * @param fwd Is this about fwd traffic? + * + * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal. + */ +int +GMC_is_origin (struct MeshConnection *c, int fwd); + +/** + * Is this peer the last one on the connection? + * + * @param c Connection. + * @param fwd Is this about fwd traffic? + * Note that the ROOT is the terminal for BCK traffic! + * + * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin. + */ +int +GMC_is_terminal (struct MeshConnection *c, int fwd); + + +#if 0 /* keep Emacsens' auto-indent happy */ +{ +#endif +#ifdef __cplusplus +} +#endif + +/* ifndef GNUNET_SERVICE_MESH_CONNECTION_H */ +#endif +/* end of gnunet-service-mesh_connection.h */ \ No newline at end of file diff --git a/src/mesh/gnunet-service-mesh_local.c b/src/mesh/gnunet-service-mesh_local.c new file mode 100644 index 000000000..762264e0c --- /dev/null +++ b/src/mesh/gnunet-service-mesh_local.c @@ -0,0 +1,1017 @@ +/* + This file is part of GNUnet. + (C) 2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#include "platform.h" +#include "mesh_enc.h" +#include "mesh_protocol_enc.h" // GNUNET_MESH_Data is shared +#include "gnunet-service-mesh_local.h" + +/******************************************************************************/ +/******************************** STRUCTS **********************************/ +/******************************************************************************/ + +/** + * Struct containing information about a client of the service + * + * TODO: add a list of 'waiting' ports + */ +struct MeshClient +{ + /** + * Linked list next + */ + struct MeshClient *next; + + /** + * Linked list prev + */ + struct MeshClient *prev; + + /** + * Tunnels that belong to this client, indexed by local id + */ + struct GNUNET_CONTAINER_MultiHashMap32 *own_channels; + + /** + * Tunnels this client has accepted, indexed by incoming local id + */ + struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels; + + /** + * Handle to communicate with the client + */ + struct GNUNET_SERVER_Client *handle; + + /** + * Ports that this client has declared interest in. + * Indexed by port, contains *Client. + */ + struct GNUNET_CONTAINER_MultiHashMap32 *ports; + + /** + * Whether the client is active or shutting down (don't send confirmations + * to a client that is shutting down. + */ + int shutting_down; + + /** + * ID of the client, mainly for debug messages + */ + unsigned int id; +}; + +/******************************************************************************/ +/******************************* GLOBALS ***********************************/ +/******************************************************************************/ + +/** + * Handle to server lib. + */ +static struct GNUNET_SERVER_Handle *server_handle; + +/** + * DLL with all the clients, head. + */ +static struct MeshClient *clients_head; + +/** + * DLL with all the clients, tail. + */ +static struct MeshClient *clients_tail; + +/** + * Next ID to assign to a client. + */ +unsigned int next_client_id; + +/** + * All ports clients of this peer have opened. + */ +static struct GNUNET_CONTAINER_MultiHashMap32 *ports; + +/** + * Notification context, to send messages to local clients. + */ +static struct GNUNET_SERVER_NotificationContext *nc; + + +/******************************************************************************/ +/******************************** STATIC ***********************************/ +/******************************************************************************/ + +/** + * Remove client's ports from the global hashmap on disconnect. + * + * @param cls Closure (unused). + * @param key Port. + * @param value Client structure. + * + * @return GNUNET_OK, keep iterating. + */ +static int +client_release_ports (void *cls, + uint32_t key, + void *value) +{ + int res; + + res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value); + if (GNUNET_YES != res) + { + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Port %u by client %p was not registered.\n", + key, value); + } + return GNUNET_OK; +} + + + +/******************************************************************************/ +/******************************** HANDLES ***********************************/ +/******************************************************************************/ + + +/** + * Handler for client connection. + * + * @param cls Closure (unused). + * @param client Client handler. + */ +void +GMLH_client_connect (void *cls, struct GNUNET_SERVER_Client *client) +{ + struct MeshClient *c; + + if (NULL == client) + return; + c = GNUNET_malloc (sizeof (struct MeshClient)); + c->handle = client; + c->id = next_client_id++; /* overflow not important: just for debug */ + GNUNET_SERVER_client_keep (client); + GNUNET_SERVER_client_set_user_context (client, c); + GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c); +} + + +/** + * Handler for client disconnection + * + * @param cls closure + * @param client identification of the client; NULL + * for the last call when the server is destroyed + */ +void +GMLH_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) +{ + struct MeshClient *c; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client); + if (client == NULL) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n"); + return; + } + + c = client_get (client); + if (NULL != c) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n", + c->id, c); + GNUNET_SERVER_client_drop (c->handle); + c->shutting_down = GNUNET_YES; + if (NULL != c->own_channels) + { + GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels, + &channel_destroy_iterator, c); + GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels); + } + + if (NULL != c->incoming_channels) + { + GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels, + &channel_destroy_iterator, c); + GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels); + } + + if (NULL != c->ports) + { + GNUNET_CONTAINER_multihashmap32_iterate (c->ports, + &client_release_ports, c); + GNUNET_CONTAINER_multihashmap32_destroy (c->ports); + } + GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c); + GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c); + GNUNET_free (c); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n"); + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n"); + return; +} + + +/** + * Handler for new clients + * + * @param cls closure + * @param client identification of the client + * @param message the actual message, which includes messages the client wants + */ +void +GMLH_new_client (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct GNUNET_MESH_ClientConnect *cc_msg; + struct MeshClient *c; + unsigned int size; + uint32_t *p; + unsigned int i; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client); + + /* Check data sanity */ + size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect); + cc_msg = (struct GNUNET_MESH_ClientConnect *) message; + if (0 != (size % sizeof (uint32_t))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + size /= sizeof (uint32_t); + + /* Initialize new client structure */ + c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size); + if (size > 0) + { + uint32_t u32; + + p = (uint32_t *) &cc_msg[1]; + c->ports = GNUNET_CONTAINER_multihashmap32_create (size); + for (i = 0; i < size; i++) + { + u32 = ntohl (p[i]); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32); + + /* store in client's hashmap */ + GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); + /* store in global hashmap */ + /* FIXME only allow one client to have the port open, + * have a backup hashmap with waiting clients */ + GNUNET_CONTAINER_multihashmap32_put (ports, u32, c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); + } + } + + c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32); + c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32); + GNUNET_SERVER_notification_context_add (nc, client); + GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO); + + GNUNET_SERVER_receive_done (client, GNUNET_OK); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n"); +} + + +/** + * Handler for requests of new tunnels + * + * @param cls Closure. + * @param client Identification of the client. + * @param message The actual message. + */ +void +GMLH_channel_create (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct GNUNET_MESH_ChannelMessage *msg; + struct MeshPeer *peer; + struct MeshTunnel2 *t; + struct MeshChannel *ch; + struct MeshClient *c; + MESH_ChannelNumber chid; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n"); + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + + /* Message size sanity check */ + if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + msg = (struct GNUNET_MESH_ChannelMessage *) message; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n", + GNUNET_i2s (&msg->peer), ntohl (msg->port)); + chid = ntohl (msg->channel_id); + + /* Sanity check for duplicate channel IDs */ + if (NULL != channel_get_by_local_id (c, chid)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + peer = peer_get (&msg->peer); + if (NULL == peer->tunnel) + { + peer->tunnel = tunnel_new (); + peer->tunnel->peer = peer; + if (peer->id == myid) + { + tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY); + } + else + { + peer_connect (peer); + } + } + t = peer->tunnel; + + /* Create channel */ + ch = channel_new (t, c, chid); + if (NULL == ch) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + ch->port = ntohl (msg->port); + channel_set_options (ch, ntohl (msg->opt)); + + /* In unreliable channels, we'll use the DLL to buffer BCK data */ + ch->root_rel = GNUNET_new (struct MeshChannelReliability); + ch->root_rel->ch = ch; + ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n", + peer2s (t->peer), ch->gid, ch->port, ch->lid_root); + + /* Send create channel */ + { + struct GNUNET_MESH_ChannelCreate msgcc; + + msgcc.header.size = htons (sizeof (msgcc)); + msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE); + msgcc.chid = htonl (ch->gid); + msgcc.port = msg->port; + msgcc.opt = msg->opt; + + tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES); + } + + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; +} + + +/** + * Handler for requests of deleting tunnels + * + * @param cls closure + * @param client identification of the client + * @param message the actual message + */ +void +GMLH_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct GNUNET_MESH_ChannelMessage *msg; + struct MeshClient *c; + struct MeshChannel *ch; + struct MeshTunnel2 *t; + MESH_ChannelNumber chid; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got a DESTROY CHANNEL from client!\n"); + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + + /* Message sanity check */ + if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + msg = (struct GNUNET_MESH_ChannelMessage *) message; + + /* Retrieve tunnel */ + chid = ntohl (msg->channel_id); + ch = channel_get_by_local_id (c, chid); + if (NULL == ch) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid); + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + /* Cleanup after the tunnel */ + client_delete_channel (c, ch); + if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid) + { + ch->dest = NULL; + } + else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid) + { + ch->root = NULL; + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + " channel %X client %p (%p, %p)\n", + chid, c, ch->root, ch->dest); + GNUNET_break (0); + } + + t = ch->t; + channel_destroy (ch); + tunnel_destroy_if_empty (t); + + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; +} + + +/** + * Handler for client traffic + * + * @param cls closure + * @param client identification of the client + * @param message the actual message + */ +void +GMLH_data (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct GNUNET_MESH_LocalData *msg; + struct MeshClient *c; + struct MeshChannel *ch; + struct MeshChannelReliability *rel; + MESH_ChannelNumber chid; + size_t size; + int fwd; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got data from a client!\n"); + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + + msg = (struct GNUNET_MESH_LocalData *) message; + + /* Sanity check for message size */ + size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData); + if (size < sizeof (struct GNUNET_MessageHeader)) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + /* Channel exists? */ + chid = ntohl (msg->id); + fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; + ch = channel_get_by_local_id (c, chid); + if (NULL == ch) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + /* Is the client in the channel? */ + if ( !( (fwd && + ch->root && + ch->root->handle == client) + || + (!fwd && + ch->dest && + ch->dest->handle == client) ) ) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + rel = fwd ? ch->root_rel : ch->dest_rel; + rel->client_ready = GNUNET_NO; + + /* Ok, everything is correct, send the message. */ + { + struct GNUNET_MESH_Data *payload; + uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size; + unsigned char cbuf[p2p_size]; + + payload = (struct GNUNET_MESH_Data *) cbuf; + payload->mid = htonl (rel->mid_send); + rel->mid_send++; + memcpy (&payload[1], &msg[1], size); + payload->header.size = htons (p2p_size); + payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA); + payload->chid = htonl (ch->gid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n"); + send_prebuilt_message_channel (&payload->header, ch, fwd); + + if (GNUNET_YES == ch->reliable) + channel_save_copy (ch, &payload->header, fwd); + } + if (tunnel_get_buffer (ch->t, fwd) > 0) + send_local_ack (ch, fwd); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n"); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + + return; +} + + +/** + * Handler for client's ACKs for payload traffic. + * + * @param cls Closure (unused). + * @param client Identification of the client. + * @param message The actual message. + */ +void +GMLH_ack (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct GNUNET_MESH_LocalAck *msg; + struct MeshChannelReliability *rel; + struct MeshChannel *ch; + struct MeshClient *c; + MESH_ChannelNumber chid; + int fwd; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n"); + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + + msg = (struct GNUNET_MESH_LocalAck *) message; + + /* Channel exists? */ + chid = ntohl (msg->channel_id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid); + ch = channel_get_by_local_id (c, chid); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch); + if (NULL == ch) + { + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + /* If client is root, the ACK is going FWD, therefore this is "BCK". */ + /* If client is dest, the ACK is going BCK, therefore this is "FWD" */ + fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV; + rel = fwd ? ch->dest_rel : ch->root_rel; + + rel->client_ready = GNUNET_YES; + channel_send_client_buffered_data (ch, c, fwd); + send_ack (NULL, ch, fwd); + + GNUNET_SERVER_receive_done (client, GNUNET_OK); + + return; +} + + +/* + * Iterator over all tunnels to send a monitoring client info about each tunnel. + * + * @param cls Closure (client handle). + * @param key Key (hashed tunnel ID, unused). + * @param value Tunnel info. + * + * @return GNUNET_YES, to keep iterating. + */ +// static int +// monitor_all_tunnels_iterator (void *cls, +// const struct GNUNET_HashCode * key, +// void *value) +// { +// struct GNUNET_SERVER_Client *client = cls; +// struct MeshChannel *ch = value; +// struct GNUNET_MESH_LocalMonitor *msg; +// +// msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor)); +// msg->channel_id = htonl (ch->gid); +// msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor)); +// msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS); +// +// GNUNET_log (GNUNET_ERROR_TYPE_INFO, +// "* sending info about tunnel %s\n", +// GNUNET_i2s (&msg->owner)); +// +// GNUNET_SERVER_notification_context_unicast (nc, client, +// &msg->header, GNUNET_NO); +// return GNUNET_YES; +// } + + +/** + * Handler for client's MONITOR request. + * + * @param cls Closure (unused). + * @param client Identification of the client. + * @param message The actual message. + */ +void +GMLH_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct MeshClient *c; + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Received get tunnels request from client %u\n", + c->id); +// GNUNET_CONTAINER_multihashmap_iterate (tunnels, +// monitor_all_tunnels_iterator, +// client); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Get tunnels request from client %u completed\n", + c->id); + GNUNET_SERVER_receive_done (client, GNUNET_OK); +} + + +/** + * Handler for client's MONITOR_TUNNEL request. + * + * @param cls Closure (unused). + * @param client Identification of the client. + * @param message The actual message. + */ +void +GMLH_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + const struct GNUNET_MESH_LocalMonitor *msg; + struct GNUNET_MESH_LocalMonitor *resp; + struct MeshClient *c; + struct MeshChannel *ch; + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + msg = (struct GNUNET_MESH_LocalMonitor *) message; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Received tunnel info request from client %u for tunnel %s[%X]\n", + c->id, + &msg->owner, + ntohl (msg->channel_id)); +// ch = channel_get (&msg->owner, ntohl (msg->channel_id)); + ch = NULL; // FIXME + if (NULL == ch) + { + /* We don't know the tunnel */ + struct GNUNET_MESH_LocalMonitor warn; + + warn = *msg; + GNUNET_SERVER_notification_context_unicast (nc, client, + &warn.header, + GNUNET_NO); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + /* Initialize context */ + resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor)); + *resp = *msg; + resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor)); + GNUNET_SERVER_notification_context_unicast (nc, c->handle, + &resp->header, GNUNET_NO); + GNUNET_free (resp); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Monitor tunnel request from client %u completed\n", + c->id); + GNUNET_SERVER_receive_done (client, GNUNET_OK); +} + + +/** + * Functions to handle messages from clients + */ +static struct GNUNET_SERVER_MessageHandler client_handlers[] = { + {&GMLH_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0}, + {&GMLH_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE, + sizeof (struct GNUNET_MESH_ChannelMessage)}, + {&GMLH_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY, + sizeof (struct GNUNET_MESH_ChannelMessage)}, + {&GMLH_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, + {&GMLH_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, + sizeof (struct GNUNET_MESH_LocalAck)}, + {&GMLH_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS, + sizeof (struct GNUNET_MessageHeader)}, + {&GMLH_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL, + sizeof (struct GNUNET_MESH_LocalMonitor)}, + {NULL, NULL, 0, 0} +}; + + + +/******************************************************************************/ +/******************************** API ***********************************/ +/******************************************************************************/ + +void +GML_init (void) +{ + ports = GNUNET_CONTAINER_multihashmap32_create (32); +} + + +/** + * Initialize server subsystem. + * + * @param handle Server handle. + */ +void +GML_init (struct GNUNET_SERVER_Handle *handle) +{ + server_handle = handle; + GNUNET_SERVER_suspend (server_handle); +} + +/** + * Install server (service) handlers and start listening to clients. + */ +void +GML_start (void) +{ + GNUNET_SERVER_add_handlers (server_handle, client_handlers); + GNUNET_SERVER_connect_notify (server_handle, &GMLH_client_connect, NULL); + GNUNET_SERVER_disconnect_notify (server_handle, &GMLH_client_disconnect, + NULL); + nc = GNUNET_SERVER_notification_context_create (server_handle, 1); + + clients_head = NULL; + clients_tail = NULL; + next_client_id = 0; + GNUNET_SERVER_resume (server_handle); +} + + +/** + * Shutdown server. + */ +void +GML_shutdown (void) +{ + if (nc != NULL) + { + GNUNET_SERVER_notification_context_destroy (nc); + nc = NULL; + } +} + + +/** + * Check if client has registered with the service and has not disconnected + * + * @param client the client to check + * + * @return non-NULL if client exists in the global DLL + */ +struct MeshClient * +GML_client_get (struct GNUNET_SERVER_Client *client) +{ + return GNUNET_SERVER_client_get_user_context (client, struct MeshClient); +} + + +/** + * Deletes a tunnel from a client (either owner or destination). + * + * @param c Client whose tunnel to delete. + * @param ch Channel which should be deleted. + */ +void +GML_client_delete_channel (struct MeshClient *c, struct MeshChannel *ch) +{ + int res; + + if (c == ch->root) + { + res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels, + ch->lid_root, ch); + if (GNUNET_YES != res) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n"); + } + if (c == ch->dest) + { + res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels, + ch->lid_dest, ch); + if (GNUNET_YES != res) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n"); + } +} + +/** + * Build a local ACK message and send it to a local client, if needed. + * + * If the client was already allowed to send data, do nothing. + * + * @param ch Channel on which to send the ACK. + * @param c Client to whom send the ACK. + * @param fwd Set to GNUNET_YES for FWD ACK (dest->root) + */ +void +GML_send_ack (struct MeshChannel *ch, int fwd) +{ + struct GNUNET_MESH_LocalAck msg; + struct MeshChannelReliability *rel; + struct MeshClient *c; + + c = fwd ? ch->root : ch->dest; + rel = fwd ? ch->root_rel : ch->dest_rel; + + if (GNUNET_YES == rel->client_ready) + return; /* don't send double ACKs to client */ + + rel->client_ready = GNUNET_YES; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "send local %s ack on %s:%X towards %p\n", + fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c); + + if (NULL == c + || ( fwd && (0 == ch->lid_root || c != ch->root)) + || (!fwd && (0 == ch->lid_dest || c != ch->dest)) ) + { + GNUNET_break (0); + return; + } + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK); + msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest); + GNUNET_SERVER_notification_context_unicast (nc, + c->handle, + &msg.header, + GNUNET_NO); + +} + + +/** + * Notify the client that a new incoming channel was created. + * + * @param ch Channel that was created. + */ +void +GML_send_channel_create (struct MeshClient *c, + uint32_t id, uint32_t port, uint32_t opt, + struct GNUNET_PeerIdentity *peer) +{ + struct GNUNET_MESH_ChannelMessage msg; + + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE); + msg.channel_id = htonl (id); + msg.port = htonl (port); + msg.opt = htonl (opt); + msg.peer = *peer; + GNUNET_SERVER_notification_context_unicast (nc, c->handle, + &msg.header, GNUNET_NO); +} + + +/** + * Notify a client that a channel is no longer valid. + * + * @param c Client. + * @param id ID of the channel that is destroyed. + */ +void +GML_send_channel_destroy (struct MeshClient *c, uint32_t id) +{ + struct GNUNET_MESH_ChannelMessage msg; + + if (NULL == c) + { + GNUNET_break (0); + return; + } + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); + msg.channel_id = htonl (id); + msg.port = htonl (0); + memset (&msg.peer, 0, sizeof (msg.peer)); + msg.opt = htonl (0); + GNUNET_SERVER_notification_context_unicast (nc, c->handle, + &msg.header, GNUNET_NO); +} + + +/** + * Modify the mesh message ID from global to local and send to client. + * + * @param msg Message to modify and send. + * @param c Client to send to. + * @param tid Tunnel ID to use (c can be both owner and client). + */ +static void +GML_send_data (struct MeshClient *c, + const struct GNUNET_MESH_Data *msg, + MESH_ChannelNumber id) +{ + struct GNUNET_MESH_LocalData *copy; + uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data); + char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)]; + + if (size < sizeof (struct GNUNET_MessageHeader)) + { + GNUNET_break_op (0); + return; + } + if (NULL == c) + { + GNUNET_break (0); + return; + } + copy = (struct GNUNET_MESH_LocalData *) cbuf; + memcpy (©[1], &msg[1], size); + copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size); + copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA); + copy->id = htonl (id); + GNUNET_SERVER_notification_context_unicast (nc, c->handle, + ©->header, GNUNET_NO); +} + + + diff --git a/src/mesh/gnunet-service-mesh_local.h b/src/mesh/gnunet-service-mesh_local.h new file mode 100644 index 000000000..7779c6507 --- /dev/null +++ b/src/mesh/gnunet-service-mesh_local.h @@ -0,0 +1,145 @@ +/* + This file is part of GNUnet. + (C) 2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file mesh/gnunet-service-mesh_local.h + * @brief mesh service; dealing with local clients + * @author Bart Polot + * + * All functions in this file should use the prefix GML (Gnunet Mesh Local) + */ + +#ifndef GNUNET_SERVICE_MESH_LOCAL_H +#define GNUNET_SERVICE_MESH_LOCAL_H + +#ifdef __cplusplus +extern "C" +{ +#if 0 /* keep Emacsens' auto-indent happy */ +} +#endif +#endif + +#include "gnunet_util_lib.h" +#include "gnunet-service-mesh_channel.h" + +/** + * Struct containing information about a client of the service + */ +struct MeshClient; + +/******************************************************************************/ +/******************************** API ***********************************/ +/******************************************************************************/ + +/** + * Initialize server subsystem. + * + * @param handle Server handle. + */ +void +GML_init (struct GNUNET_SERVER_Handle *handle); + +/** + * Install server (service) handlers and start listening to clients. + */ +void +GML_start (void); + +/** + * Shutdown server. + */ +void +GML_shutdown (void); + +/** + * Check if client has registered with the service and has not disconnected + * + * @param client the client to check + * + * @return non-NULL if client exists in the global DLL + */ +struct MeshClient * +GML_client_get (struct GNUNET_SERVER_Client *client); + +/** + * Deletes a tunnel from a client (either owner or destination). + * + * @param c Client whose tunnel to delete. + * @param ch Channel which should be deleted. + */ +void +GML_client_delete_channel (struct MeshClient *c, struct MeshChannel *ch); + +/** + * Build a local ACK message and send it to a local client, if needed. + * + * If the client was already allowed to send data, do nothing. + * + * @param ch Channel on which to send the ACK. + * @param c Client to whom send the ACK. + * @param fwd Set to GNUNET_YES for FWD ACK (dest->root) + */ +void +GML_send_ack (struct MeshChannel *ch, int fwd); + +/** + * Notify the appropriate client that a new incoming channel was created. + * + * @param ch Channel that was created. + */ +void +GML_send_channel_create (struct MeshClient *c, + uint32_t id, uint32_t port, uint32_t opt, + struct GNUNET_PeerIdentity *peer); + +/** + * Notify a client that a channel is no longer valid. + * + * @param c Client. + * @param id ID of the channel that is destroyed. + */ +void +GML_send_channel_destroy (struct MeshClient *c, uint32_t id); + +/** + * Modify the mesh message TID from global to local and send to client. + * + * @param ch Channel on which to send the message. + * @param msg Message to modify and send. + * @param c Client to send to. + * @param tid Tunnel ID to use (c can be both owner and client). + */ +void +GML_send_data (struct MeshChannel *ch, + const struct GNUNET_MESH_Data *msg, + struct MeshClient *c, MESH_ChannelNumber id); + + +#if 0 /* keep Emacsens' auto-indent happy */ +{ +#endif +#ifdef __cplusplus +} +#endif + +/* ifndef GNUNET_MESH_SERVICE_LOCAL_H */ +#endif +/* end of gnunet-mesh-service_LOCAL.h */ \ No newline at end of file -- 2.25.1