2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-enc.c
23 * @brief GNUnet MESH service with encryption
24 * @author Bartlomiej Polot
27 * - when sending in-order buffered data, wait for client ACKs
30 * - set connection IDs independently from tunnel, tunnel has no ID
33 * - relay corking down to core
34 * - set ttl relative to path length
38 * - peer: other mesh instance. If there is direct connection it's a neighbor.
39 * - tunnel: encrypted connection to a peer, neighbor or not.
40 * - channel: connection between two clients, on the same or different peers.
41 * have properties like reliability.
42 * - path: series of directly connected peer from one peer to another.
43 * - connection: path which is being used in a tunnel.
47 #include "gnunet_crypto_lib.h"
49 #include "mesh_protocol_enc.h"
50 #include "mesh_path.h"
51 #include "block_mesh.h"
52 #include "gnunet_dht_service.h"
53 #include "gnunet_statistics_service.h"
55 #define MESH_BLOOM_SIZE 128
56 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
57 GNUNET_TIME_UNIT_MINUTES,\
59 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
60 #define MESH_RETRANSMIT_MARGIN 4
62 #define MESH_DEBUG_DHT GNUNET_NO
63 #define MESH_DEBUG_CONNECTION GNUNET_NO
64 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
67 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_DHT(...)
72 #if MESH_DEBUG_CONNECTION
73 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_CONN(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
103 * All the states a tunnel can be in.
108 * Uninitialized status, should never appear in operation.
113 * Path to the peer not known yet
115 MESH_TUNNEL_SEARCHING,
118 * Request sent, not yet answered.
123 * Peer connected and ready to accept data
128 * Peer connected previosly but not responding
130 MESH_TUNNEL_RECONNECTING
135 * All the states a connection can be in.
137 enum MeshConnectionState
140 * Uninitialized status, should never appear in operation.
145 * Connection create message sent, waiting for ACK.
147 MESH_CONNECTION_SENT,
150 * Connection ACK sent, waiting for ACK.
155 * Connection confirmed, ready to carry traffic.
157 MESH_CONNECTION_READY,
162 * All the states a connection can be in.
164 enum MeshChannelState
167 * Uninitialized status, should never appear in operation.
172 * Connection create message sent, waiting for ACK.
177 * Connection confirmed, ready to carry traffic..
183 /******************************************************************************/
184 /************************ DATA STRUCTURES ****************************/
185 /******************************************************************************/
187 /** FWD declaration */
191 struct MeshConnection;
193 struct MeshChannelReliability;
197 * Struct containing info about a queued transmission to this peer
204 struct MeshPeerQueue *next;
209 struct MeshPeerQueue *prev;
212 * Peer this transmission is directed to.
214 struct MeshPeer *peer;
217 * Connection this message belongs to.
219 struct MeshConnection *c;
227 * Channel this message belongs to, if known.
229 struct MeshChannel *ch;
232 * Pointer to info stucture used as cls.
242 * Size of the message
249 * Struct to encapsulate all the Flow Control information to a peer to which
250 * we are directly connected (on a core level).
252 struct MeshFlowControl
255 * Connection this controls.
257 struct MeshConnection *c;
260 * How many messages are in the queue on this connection.
262 unsigned int queue_n;
265 * How many messages do we accept in the queue.
267 unsigned int queue_max;
275 * ID of the last packet sent towards the peer.
277 uint32_t last_pid_sent;
280 * ID of the last packet received from the peer.
282 uint32_t last_pid_recv;
285 * Last ACK sent to the peer (peer can't send more than this PID).
287 uint32_t last_ack_sent;
290 * Last ACK sent towards the origin (for traffic towards leaf node).
292 uint32_t last_ack_recv;
295 * Task to poll the peer in case of a lost ACK causes stall.
297 GNUNET_SCHEDULER_TaskIdentifier poll_task;
300 * How frequently to poll for ACKs.
302 struct GNUNET_TIME_Relative poll_time;
307 * Struct containing all information regarding a given peer
317 * Last time we heard from this peer
319 struct GNUNET_TIME_Absolute last_contact;
322 * Paths to reach the peer, ordered by ascending hop count
324 struct MeshPeerPath *path_head;
327 * Paths to reach the peer, ordered by ascending hop count
329 struct MeshPeerPath *path_tail;
332 * Handle to stop the DHT search for paths to this peer
334 struct GNUNET_DHT_GetHandle *dhtget;
337 * Tunnel to this peer, if any.
339 struct MeshTunnel2 *tunnel;
342 * Connections that go through this peer, indexed by tid;
344 struct GNUNET_CONTAINER_MultiHashMap *connections;
347 * Handle for queued transmissions
349 struct GNUNET_CORE_TransmitHandle *core_transmit;
352 * Transmission queue to core DLL head
354 struct MeshPeerQueue *queue_head;
357 * Transmission queue to core DLL tail
359 struct MeshPeerQueue *queue_tail;
362 * How many messages are in the queue to this peer.
364 unsigned int queue_n;
369 * Info needed to retry a message in case it gets lost.
371 struct MeshReliableMessage
374 * Double linked list, FIFO style
376 struct MeshReliableMessage *next;
377 struct MeshReliableMessage *prev;
380 * Type of message (payload, channel management).
385 * Tunnel Reliability queue this message is in.
387 struct MeshChannelReliability *rel;
390 * ID of the message (ACK needed to free)
395 * When was this message issued (to calculate ACK delay)
397 struct GNUNET_TIME_Absolute timestamp;
399 /* struct GNUNET_MESH_Data with payload */
404 * Info about the traffic state for a client in a channel.
406 struct MeshChannelReliability
409 * Channel this is about.
411 struct MeshChannel *ch;
414 * DLL of messages sent and not yet ACK'd.
416 struct MeshReliableMessage *head_sent;
417 struct MeshReliableMessage *tail_sent;
420 * Messages pending to send.
425 * DLL of messages received out of order.
427 struct MeshReliableMessage *head_recv;
428 struct MeshReliableMessage *tail_recv;
436 * Next MID to use for outgoing traffic.
441 * Next MID expected for incoming traffic.
446 * Can we send data to the client?
451 * Task to resend/poll in case no ACK is received.
453 GNUNET_SCHEDULER_TaskIdentifier retry_task;
456 * Counter for exponential backoff.
458 struct GNUNET_TIME_Relative retry_timer;
461 * How long does it usually take to get an ACK.
463 struct GNUNET_TIME_Relative expected_delay;
468 * Struct containing all information regarding a channel to a remote client.
473 * Tunnel this channel is in.
475 struct MeshTunnel2 *t;
478 * Double linked list.
480 struct MeshChannel *next;
481 struct MeshChannel *prev;
484 * Destination port of the channel.
489 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
491 MESH_ChannelNumber gid;
494 * Local tunnel number for root (owner) client.
495 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
497 MESH_ChannelNumber lid_root;
500 * Local tunnel number for local destination clients (incoming number)
501 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
503 MESH_ChannelNumber lid_dest;
508 enum MeshChannelState state;
511 * Is the tunnel bufferless (minimum latency)?
516 * Is the tunnel reliable?
521 * Last time the channel was used
523 struct GNUNET_TIME_Absolute timestamp;
526 * Client owner of the tunnel, if any
528 struct MeshClient *root;
531 * Client destination of the tunnel, if any.
533 struct MeshClient *dest;
536 * Flag to signal the destruction of the channel.
537 * If this is set GNUNET_YES the channel will be destroyed
538 * when the queue is empty.
543 * Total messages pending for this channel, payload or not.
545 unsigned int pending_messages;
549 * Only present (non-NULL) at the owner of a tunnel.
551 struct MeshChannelReliability *root_rel;
555 * Only present (non-NULL) at the destination of a tunnel.
557 struct MeshChannelReliability *dest_rel;
563 * Struct containing all information regarding a connection to a peer.
565 struct MeshConnection
570 struct MeshConnection *next;
571 struct MeshConnection *prev;
574 * Tunnel this connection is part of.
576 struct MeshTunnel2 *t;
579 * Flow control information for traffic fwd.
581 struct MeshFlowControl fwd_fc;
584 * Flow control information for traffic bck.
586 struct MeshFlowControl bck_fc;
589 * ID of the connection.
591 struct GNUNET_HashCode id;
594 * State of the connection.
596 enum MeshConnectionState state;
599 * Path being used for the tunnel.
601 struct MeshPeerPath *path;
604 * Position of the local peer in the path.
606 unsigned int own_pos;
609 * Task to keep the used paths alive at the owner,
610 * time tunnel out on all the other peers.
612 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
615 * Task to keep the used paths alive at the destination,
616 * time tunnel out on all the other peers.
618 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
621 * Pending message count.
623 int pending_messages;
626 * Destroy flag: if true, destroy on last message.
633 * Struct used to queue messages in a tunnel.
635 struct MeshTunnelQueue
640 struct MeshTunnelQueue *next;
641 struct MeshTunnelQueue *prev;
646 struct MeshChannel *ch;
651 /* struct GNUNET_MessageHeader *msg; */
656 * Struct containing all information regarding a tunnel to a peer.
661 * Endpoint of the tunnel.
663 struct MeshPeer *peer;
666 * State of the tunnel.
668 enum MeshTunnelState state;
671 * Local peer ephemeral private key
673 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
676 * Local peer ephemeral public key
678 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
681 * Remote peer's public key.
683 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
686 * Encryption ("our") key.
688 struct GNUNET_CRYPTO_AesSessionKey e_key;
691 * Decryption ("their") key.
693 struct GNUNET_CRYPTO_AesSessionKey d_key;
696 * Paths that are actively used to reach the destination peer.
698 struct MeshConnection *connection_head;
699 struct MeshConnection *connection_tail;
702 * Next connection number.
707 * Channels inside this tunnel.
709 struct MeshChannel *channel_head;
710 struct MeshChannel *channel_tail;
713 * Channel ID for the next created channel.
715 MESH_ChannelNumber next_chid;
718 * Channel ID for the next incoming channel.
720 MESH_ChannelNumber next_local_chid;
723 * Pending message count.
725 int pending_messages;
728 * Destroy flag: if true, destroy on last message.
733 * Queued messages, to transmit once tunnel gets connected.
735 struct MeshTunnelQueue *tq_head;
736 struct MeshTunnelQueue *tq_tail;
742 * Struct containing information about a client of the service
744 * TODO: add a list of 'waiting' ports
751 struct MeshClient *next;
756 struct MeshClient *prev;
759 * Tunnels that belong to this client, indexed by local id
761 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
764 * Tunnels this client has accepted, indexed by incoming local id
766 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
769 * Handle to communicate with the client
771 struct GNUNET_SERVER_Client *handle;
774 * Ports that this client has declared interest in.
775 * Indexed by port, contains *Client.
777 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
780 * Whether the client is active or shutting down (don't send confirmations
781 * to a client that is shutting down.
786 * ID of the client, mainly for debug messages
792 /******************************************************************************/
793 /************************ DEBUG FUNCTIONS ****************************/
794 /******************************************************************************/
798 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
799 * @param cls string to print
800 * @param success GNUNET_OK if the PUT was transmitted,
801 * GNUNET_NO on timeout,
802 * GNUNET_SYSERR on disconnect from service
803 * after the PUT message was transmitted
804 * (so we don't know if it was received or not)
809 mesh_debug (void *cls, int success)
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
819 /******************************************************************************/
820 /*********************** GLOBAL VARIABLES ****************************/
821 /******************************************************************************/
823 /************************** Configuration parameters **************************/
826 * How often to send path keepalives. Paths timeout after 4 missed.
828 static struct GNUNET_TIME_Relative refresh_connection_time;
831 * How often to PUT own ID in the DHT.
833 static struct GNUNET_TIME_Relative id_announce_time;
836 * Maximum time allowed to connect to a peer found by string.
838 static struct GNUNET_TIME_Relative connect_timeout;
841 * Default TTL for payload packets.
843 static unsigned long long default_ttl;
846 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
848 static unsigned long long dht_replication_level;
851 * How many connections are we willing to maintain.
852 * Local connections are always allowed, even if there are more connections than max.
854 static unsigned long long max_connections;
857 * How many messages *in total* are we willing to queue, divide by number of
858 * connections to get connection queue size.
860 static unsigned long long max_msgs_queue;
863 * How many peers do we want to remember?
865 static unsigned long long max_peers;
868 * Percentage of messages that will be dropped (for test purposes only).
870 static unsigned long long drop_percent;
872 /*************************** Static global variables **************************/
875 * DLL with all the clients, head.
877 static struct MeshClient *clients_head;
880 * DLL with all the clients, tail.
882 static struct MeshClient *clients_tail;
885 * Connections known, indexed by cid (MeshConnection).
887 static struct GNUNET_CONTAINER_MultiHashMap *connections;
890 * Peers known, indexed by PeerIdentity (MeshPeer).
892 static struct GNUNET_CONTAINER_MultiHashMap *peers;
895 * Handle to communicate with core.
897 static struct GNUNET_CORE_Handle *core_handle;
902 static struct GNUNET_DHT_Handle *dht_handle;
905 * Handle to server lib.
907 static struct GNUNET_SERVER_Handle *server_handle;
910 * Handle to the statistics service.
912 static struct GNUNET_STATISTICS_Handle *stats;
915 * Notification context, to send messages to local clients.
917 static struct GNUNET_SERVER_NotificationContext *nc;
920 * Local peer own ID (memory efficient handle).
922 static GNUNET_PEER_Id myid;
925 * Local peer own ID (full value).
927 static struct GNUNET_PeerIdentity my_full_id;
932 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
937 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
940 * All ports clients of this peer have opened.
942 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
945 * Task to periodically announce itself in the network.
947 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
950 * Next ID to assign to a client.
952 unsigned int next_client_id;
955 /******************************************************************************/
956 /*********************** DECLARATIONS **************************/
957 /******************************************************************************/
960 * Function to process paths received for a new peer addition. The recorded
961 * paths form the initial tunnel, which can be optimized later.
962 * Called on each result obtained for the DHT search.
965 * @param exp when will this value expire
966 * @param key key of the result
967 * @param type type of the result
968 * @param size number of bytes in data
969 * @param data pointer to the result data
972 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
973 const struct GNUNET_HashCode * key,
974 const struct GNUNET_PeerIdentity *get_path,
975 unsigned int get_path_length,
976 const struct GNUNET_PeerIdentity *put_path,
977 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
978 size_t size, const void *data);
982 * Retrieve the MeshPeer stucture associated with the peer, create one
983 * and insert it in the appropriate structures if the peer is not known yet.
985 * @param peer Full identity of the peer.
987 * @return Existing or newly created peer info.
989 static struct MeshPeer *
990 peer_get (const struct GNUNET_PeerIdentity *peer);
994 * Retrieve the MeshPeer stucture associated with the peer, create one
995 * and insert it in the appropriate structures if the peer is not known yet.
997 * @param peer Short identity of the peer.
999 * @return Existing or newly created peer info.
1001 static struct MeshPeer *
1002 peer_get_short (const GNUNET_PEER_Id peer);
1006 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1007 * to obtain a local peer -> destination path and interning the peer ids.
1009 * @return Newly allocated and created path
1011 static struct MeshPeerPath *
1012 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1013 unsigned int get_path_length,
1014 const struct GNUNET_PeerIdentity *put_path,
1015 unsigned int put_path_length);
1019 * Adds a path to the data structs of all the peers in the path
1021 * @param p Path to process.
1022 * @param confirmed Whether we know if the path works or not.
1025 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1029 * Search for a tunnel by global ID using full PeerIdentities.
1031 * @param t Tunnel containing the channel.
1032 * @param chid Public channel number.
1034 * @return channel handler, NULL if doesn't exist
1036 static struct MeshChannel *
1037 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1040 * Modify the data message ID from global to local and send to client.
1042 * @param ch Channel on which to send the message.
1043 * @param msg Message to modify and send.
1044 * @param fwd Forward?
1047 channel_send_client_data (struct MeshChannel *ch,
1048 const struct GNUNET_MESH_Data *msg,
1053 * Change the tunnel state.
1055 * @param t Tunnel whose state to change.
1056 * @param state New state.
1059 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1063 * Notify a tunnel that a connection has broken that affects at least
1064 * some of its peers.
1066 * @param t Tunnel affected.
1067 * @param p1 Peer that got disconnected from p2.
1068 * @param p2 Peer that got disconnected from p1.
1070 * @return Short ID of the peer disconnected (either p1 or p2).
1071 * 0 if the tunnel remained unaffected.
1073 static GNUNET_PEER_Id
1074 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1075 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1078 * @brief Use the given path for the tunnel.
1079 * Update the next and prev hops (and RCs).
1080 * (Re)start the path refresh in case the tunnel is locally owned.
1082 * @param t Tunnel to update.
1083 * @param p Path to use.
1085 * @return Connection created.
1087 static struct MeshConnection *
1088 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1091 * Tunnel is empty: destroy it.
1093 * Notifies all participants (peers, cleints) about the destruction.
1095 * @param t Tunnel to destroy.
1098 tunnel_destroy_empty (struct MeshTunnel2 *t);
1101 * Destroy the tunnel.
1103 * This function does not generate any warning traffic to clients or peers.
1106 * Cancel messages belonging to this tunnel queued to neighbors.
1107 * Free any allocated resources linked to the tunnel.
1109 * @param t The tunnel to destroy.
1112 tunnel_destroy (struct MeshTunnel2 *t);
1115 * Create a connection.
1117 * @param cid Connection ID.
1119 static struct MeshConnection *
1120 connection_new (const struct GNUNET_HashCode *cid);
1123 * Connection is no longer needed: destroy it and remove from tunnel.
1125 * @param c Connection to destroy.
1128 connection_destroy (struct MeshConnection *c);
1131 * Send FWD keepalive packets for a connection.
1133 * @param cls Closure (connection for which to send the keepalive).
1134 * @param tc Notification context.
1137 connection_fwd_keepalive (void *cls,
1138 const struct GNUNET_SCHEDULER_TaskContext *tc);
1141 * Send BCK keepalive packets for a connection.
1143 * @param cls Closure (connection for which to send the keepalive).
1144 * @param tc Notification context.
1147 connection_bck_keepalive (void *cls,
1148 const struct GNUNET_SCHEDULER_TaskContext *tc);
1152 * Change the tunnel state.
1154 * @param c Connection whose state to change.
1155 * @param state New state.
1158 connection_change_state (struct MeshConnection* c,
1159 enum MeshConnectionState state);
1164 * @brief Queue and pass message to core when possible.
1166 * @param cls Closure (@c type dependant). It will be used by queue_send to
1167 * build the message to be sent if not already prebuilt.
1168 * @param type Type of the message, 0 for a raw message.
1169 * @param size Size of the message.
1170 * @param c Connection this message belongs to (cannot be NULL).
1171 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1172 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1175 queue_add (void* cls,
1178 struct MeshConnection* c,
1179 struct MeshChannel* ch,
1184 * Free a transmission that was already queued with all resources
1185 * associated to the request.
1187 * @param queue Queue handler to cancel.
1188 * @param clear_cls Is it necessary to free associated cls?
1191 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1195 * Core callback to write a queued packet to core buffer
1197 * @param cls Closure (peer info).
1198 * @param size Number of bytes available in buf.
1199 * @param buf Where the to write the message.
1201 * @return number of bytes written to buf
1204 queue_send (void *cls, size_t size, void *buf);
1208 * Demultiplex by message type and call appropriate handler for a message
1209 * towards a channel of a local tunnel.
1211 * @param t Tunnel this message came on.
1212 * @param msgh Message header.
1213 * @param fwd Is this message fwd?
1216 handle_decrypted (struct MeshTunnel2 *t,
1217 const struct GNUNET_MessageHeader *msgh,
1222 * Dummy function to separate declarations from definitions in function list.
1225 __mesh_divider______________________________________________________________();
1229 * Get string description for tunnel state.
1231 * @param s Tunnel state.
1233 * @return String representation.
1236 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1238 static char buf[128];
1242 case MESH_TUNNEL_NEW:
1243 return "MESH_TUNNEL_NEW";
1244 case MESH_TUNNEL_SEARCHING:
1245 return "MESH_TUNNEL_SEARCHING";
1246 case MESH_TUNNEL_WAITING:
1247 return "MESH_TUNNEL_WAITING";
1248 case MESH_TUNNEL_READY:
1249 return "MESH_TUNNEL_READY";
1250 case MESH_TUNNEL_RECONNECTING:
1251 return "MESH_TUNNEL_RECONNECTING";
1254 sprintf (buf, "%u (UNKNOWN STATE)", s);
1261 * Get string description for tunnel state.
1263 * @param s Tunnel state.
1265 * @return String representation.
1268 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1272 case MESH_CONNECTION_NEW:
1273 return "MESH_CONNECTION_NEW";
1274 case MESH_CONNECTION_SENT:
1275 return "MESH_CONNECTION_SENT";
1276 case MESH_CONNECTION_ACK:
1277 return "MESH_CONNECTION_ACK";
1278 case MESH_CONNECTION_READY:
1279 return "MESH_CONNECTION_READY";
1281 return "MESH_CONNECTION_STATE_ERROR";
1287 /******************************************************************************/
1288 /************************ PERIODIC FUNCTIONS ****************************/
1289 /******************************************************************************/
1292 * Periodically announce self id in the DHT
1294 * @param cls closure
1295 * @param tc task context
1298 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1300 struct PBlock block;
1302 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1304 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1308 * - Set data expiration in function of X
1309 * - Adapt X to churn
1311 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1313 block.id = my_full_id;
1314 GNUNET_DHT_put (dht_handle, /* DHT handle */
1315 &my_full_id.hashPubKey, /* Key to use */
1316 dht_replication_level, /* Replication level */
1317 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1318 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1319 sizeof (block), /* Size of the data */
1320 (const char *) &block, /* Data itself */
1321 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1322 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1323 NULL, /* Continuation */
1324 NULL); /* Continuation closure */
1326 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1330 /******************************************************************************/
1331 /****************** GENERAL HELPER FUNCTIONS ************************/
1332 /******************************************************************************/
1336 * Get the static string for a peer ID.
1340 * @return Static string for it's ID.
1343 peer2s (const struct MeshPeer *peer)
1347 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1352 * Get the previous hop in a connection
1354 * @param c Connection.
1356 * @return Previous peer in the connection.
1358 static struct MeshPeer *
1359 connection_get_prev_hop (struct MeshConnection *c)
1363 if (0 == c->own_pos || c->path->length < 2)
1364 id = c->path->peers[0];
1366 id = c->path->peers[c->own_pos - 1];
1368 return peer_get_short (id);
1373 * Get the next hop in a connection
1375 * @param c Connection.
1377 * @return Next peer in the connection.
1379 static struct MeshPeer *
1380 connection_get_next_hop (struct MeshConnection *c)
1384 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1385 id = c->path->peers[c->path->length - 1];
1387 id = c->path->peers[c->own_pos + 1];
1389 return peer_get_short (id);
1394 * Get the hop in a connection.
1396 * @param c Connection.
1397 * @param fwd Next hop?
1399 * @return Next peer in the connection.
1401 static struct MeshPeer *
1402 connection_get_hop (struct MeshConnection *c, int fwd)
1405 return connection_get_next_hop (c);
1406 return connection_get_prev_hop (c);
1410 * Check if client has registered with the service and has not disconnected
1412 * @param client the client to check
1414 * @return non-NULL if client exists in the global DLL
1416 static struct MeshClient *
1417 client_get (struct GNUNET_SERVER_Client *client)
1419 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1424 * Deletes a tunnel from a client (either owner or destination).
1426 * @param c Client whose tunnel to delete.
1427 * @param ch Channel which should be deleted.
1430 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1436 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1438 if (GNUNET_YES != res)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1443 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1445 if (GNUNET_YES != res)
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1452 * Notify the appropriate client that a new incoming channel was created.
1454 * @param ch Channel that was created.
1457 send_local_channel_create (struct MeshChannel *ch)
1459 struct GNUNET_MESH_ChannelMessage msg;
1460 struct MeshTunnel2 *t = ch->t;
1462 if (NULL == ch->dest)
1464 msg.header.size = htons (sizeof (msg));
1465 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1466 msg.channel_id = htonl (ch->lid_dest);
1467 msg.port = htonl (ch->port);
1469 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1470 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1471 msg.opt = htonl (msg.opt);
1472 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1473 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1474 &msg.header, GNUNET_NO);
1479 * Notify a client that the incoming tunnel is no longer valid.
1481 * @param ch Channel that is destroyed.
1482 * @param fwd Forward notification (owner->dest)?
1485 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1487 struct GNUNET_MESH_ChannelMessage msg;
1488 struct MeshClient *c;
1490 c = fwd ? ch->dest : ch->root;
1496 msg.header.size = htons (sizeof (msg));
1497 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1498 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1499 msg.port = htonl (0);
1500 memset (&msg.peer, 0, sizeof (msg.peer));
1501 msg.opt = htonl (0);
1502 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1503 &msg.header, GNUNET_NO);
1508 * Build a local ACK message and send it to a local client, if needed.
1510 * If the client was already allowed to send data, do nothing.
1512 * @param ch Channel on which to send the ACK.
1513 * @param c Client to whom send the ACK.
1514 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
1517 send_local_ack (struct MeshChannel *ch, int fwd)
1519 struct GNUNET_MESH_LocalAck msg;
1520 struct MeshChannelReliability *rel;
1521 struct MeshClient *c;
1523 c = fwd ? ch->root : ch->dest;
1524 rel = fwd ? ch->root_rel : ch->dest_rel;
1526 if (GNUNET_YES == rel->client_ready)
1527 return; /* don't send double ACKs to client */
1529 rel->client_ready = GNUNET_YES;
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "send local %s ack on %s:%X towards %p\n",
1533 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1536 || ( fwd && (0 == ch->lid_root || c != ch->root))
1537 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1542 msg.header.size = htons (sizeof (msg));
1543 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1544 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1545 GNUNET_SERVER_notification_context_unicast (nc,
1554 * Count established (ready) connections of a tunnel.
1556 * @param t Tunnel on which to send the message.
1558 * @return Number of connections.
1561 tunnel_count_connections (struct MeshTunnel2 *t)
1563 struct MeshConnection *c;
1566 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1573 * Pick a connection on which send the next data message.
1575 * @param t Tunnel on which to send the message.
1576 * @param fwd Is this a fwd message?
1578 * @return The connection on which to send the next message.
1580 static struct MeshConnection *
1581 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1583 struct MeshConnection *c;
1584 struct MeshConnection *best;
1585 struct MeshFlowControl *fc;
1586 unsigned int lowest_q;
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
1591 lowest_q = UINT_MAX;
1592 for (c = t->connection_head; NULL != c; c = c->next)
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
1595 GNUNET_h2s (&c->id), c->state);
1596 if (MESH_CONNECTION_READY == c->state)
1598 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
1605 if (fc->queue_n < lowest_q)
1608 lowest_q = fc->queue_n;
1617 * Is this peer the first one on the connection?
1619 * @param c Connection.
1620 * @param fwd Is this about fwd traffic?
1622 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1625 connection_is_origin (struct MeshConnection *c, int fwd)
1627 if (!fwd && c->own_pos == c->path->length - 1)
1629 if (fwd && c->own_pos == 0)
1636 * Is this peer the last one on the connection?
1638 * @param c Connection.
1639 * @param fwd Is this about fwd traffic?
1640 * Note that the ROOT is the terminal for BCK traffic!
1642 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1645 connection_is_terminal (struct MeshConnection *c, int fwd)
1647 if (fwd && c->own_pos == c->path->length - 1)
1649 if (!fwd && c->own_pos == 0)
1656 * Is the recipient client for this channel on this peer?
1658 * @param ch Channel.
1659 * @param fwd Is this for fwd traffic?
1661 * @return GNUNET_YES in case it is.
1664 channel_is_terminal (struct MeshChannel *ch, int fwd)
1666 if (NULL == ch->t || NULL == ch->t->connection_head)
1671 return connection_is_terminal (ch->t->connection_head, fwd);
1676 * Get free buffer space towards the client on a specific channel.
1678 * @param ch Channel.
1679 * @param fwd Is query about FWD traffic?
1681 * @return Free buffer space [0 - 64]
1684 channel_get_buffer (struct MeshChannel *ch, int fwd)
1686 struct MeshChannelReliability *rel;
1688 rel = fwd ? ch->dest_rel : ch->root_rel;
1690 /* If rel is NULL it means that the end is not yet created,
1691 * most probably is a loopback channel at the point of sending
1692 * the ChannelCreate to itself.
1697 return (64 - rel->n_recv);
1702 * Get free buffer space in a connection.
1704 * @param c Connection.
1705 * @param fwd Is query about FWD traffic?
1707 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1710 connection_get_buffer (struct MeshConnection *c, int fwd)
1712 struct MeshFlowControl *fc;
1714 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1716 return (fc->queue_max - fc->queue_n);
1721 * Get the total buffer space for a tunnel.
1724 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1726 struct MeshConnection *c;
1727 struct MeshFlowControl *fc;
1728 unsigned int buffer;
1730 c = t->connection_head;
1739 /* If terminal, return biggest channel buffer */
1740 if (connection_is_terminal (c, fwd))
1742 struct MeshChannel *ch;
1743 unsigned int ch_buf;
1745 if (NULL == t->channel_head)
1748 for (ch = t->channel_head; NULL != ch; ch = ch->next)
1750 ch_buf = channel_get_buffer (ch, fwd);
1751 if (ch_buf > buffer)
1757 /* If not terminal, return sum of connection buffers */
1760 if (c->state != MESH_CONNECTION_READY)
1766 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1767 buffer += fc->last_ack_recv - fc->last_pid_sent;
1776 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1777 * Encrypt data with the tunnel key.
1779 * @param t Tunnel whose key to use.
1780 * @param dst Destination for the encrypted data.
1781 * @param src Source of the plaintext.
1782 * @param size Size of the plaintext.
1783 * @param iv Initialization Vector to use.
1784 * @param fwd Is this a fwd message?
1787 tunnel_encrypt (struct MeshTunnel2 *t,
1788 void *dst, const void *src,
1789 size_t size, uint64_t iv, int fwd)
1791 memcpy (dst, src, size);
1796 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1797 * Decrypt data with the tunnel key.
1799 * @param t Tunnel whose key to use.
1800 * @param dst Destination for the plaintext.
1801 * @param src Source of the encrypted data.
1802 * @param size Size of the encrypted data.
1803 * @param iv Initialization Vector to use.
1804 * @param fwd Is this a fwd message?
1807 tunnel_decrypt (struct MeshTunnel2 *t,
1808 void *dst, const void *src,
1809 size_t size, uint64_t iv, int fwd)
1811 memcpy (dst, src, size);
1816 * Sends an already built message on a connection, properly registering
1817 * all used resources.
1819 * @param message Message to send. Function makes a copy of it.
1820 * If message is not hop-by-hop, decrements TTL of copy.
1821 * @param c Connection on which this message is transmitted.
1822 * @param ch Channel on which this message is transmitted, or NULL.
1823 * @param fwd Is this a fwd message?
1826 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1827 struct MeshConnection *c,
1828 struct MeshChannel *ch,
1835 size = ntohs (message->size);
1836 data = GNUNET_malloc (size);
1837 memcpy (data, message, size);
1838 type = ntohs (message->type);
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1840 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1844 struct GNUNET_MESH_Encrypted *emsg;
1845 struct GNUNET_MESH_ACK *amsg;
1846 struct GNUNET_MESH_Poll *pmsg;
1847 struct GNUNET_MESH_ConnectionDestroy *dmsg;
1848 struct GNUNET_MESH_ConnectionBroken *bmsg;
1851 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1852 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1853 emsg = (struct GNUNET_MESH_Encrypted *) data;
1854 ttl = ntohl (emsg->ttl);
1857 GNUNET_break_op (0);
1861 emsg->ttl = htonl (ttl - 1);
1862 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1866 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1867 amsg = (struct GNUNET_MESH_ACK *) data;
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1872 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1873 pmsg = (struct GNUNET_MESH_Poll *) data;
1875 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1879 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1880 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
1885 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1886 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
1891 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1892 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1909 * Sends an already built message on a tunnel, choosing the best connection.
1911 * @param message Message to send. Function modifies it.
1912 * @param t Tunnel on which this message is transmitted.
1913 * @param ch Channel on which this message is transmitted.
1914 * @param fwd Is this a fwd message?
1917 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1918 struct MeshTunnel2 *t,
1919 struct MeshChannel *ch,
1922 struct MeshConnection *c;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1927 c = tunnel_get_connection (t, fwd);
1930 GNUNET_break (GNUNET_YES == t->destroy);
1933 type = ntohs (msg->header.type);
1936 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1937 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1938 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1939 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1941 msg->ttl = htonl (default_ttl);
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1945 GNUNET_MESH_DEBUG_M2S (type));
1950 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1955 * Sends an already built message on a channel, properly registering
1956 * all used resources and encrypting the message with the tunnel's key.
1958 * @param message Message to send. Function makes a copy of it.
1959 * @param ch Channel on which this message is transmitted.
1960 * @param fwd Is this a fwd message?
1963 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1964 struct MeshChannel *ch,
1967 struct GNUNET_MESH_Encrypted *msg;
1968 size_t size = ntohs (message->size);
1969 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1974 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1976 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1978 if (channel_is_terminal (ch, fwd))
1980 handle_decrypted (ch->t, message, fwd);
1984 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1985 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1987 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1988 msg->header.type = htons (type);
1989 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1990 msg->iv = GNUNET_htonll (iv);
1991 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1992 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1997 * Sends a CREATE CONNECTION message for a path to a peer.
1998 * Changes the connection and tunnel states if necessary.
2000 * @param connection Connection to create.
2003 send_connection_create (struct MeshConnection *connection)
2005 struct MeshTunnel2 *t;
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2010 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2011 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2012 (connection->path->length *
2013 sizeof (struct GNUNET_PeerIdentity)),
2018 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
2019 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2020 if (MESH_CONNECTION_NEW == connection->state)
2021 connection_change_state (connection, MESH_CONNECTION_SENT);
2026 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
2029 * @param connection Connection to confirm.
2030 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
2033 send_connection_ack (struct MeshConnection *connection, int fwd)
2035 struct MeshTunnel2 *t;
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
2040 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
2041 sizeof (struct GNUNET_MESH_ConnectionACK),
2045 if (MESH_TUNNEL_NEW == t->state)
2046 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2047 if (MESH_CONNECTION_READY != connection->state)
2048 connection_change_state (connection, MESH_CONNECTION_SENT);
2053 * Core callback to write a pre-constructed data packet to core buffer
2055 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2056 * @param size Number of bytes available in buf.
2057 * @param buf Where the to write the message.
2059 * @return number of bytes written to buf
2062 send_core_data_raw (void *cls, size_t size, void *buf)
2064 struct GNUNET_MessageHeader *msg = cls;
2067 GNUNET_assert (NULL != msg);
2068 total_size = ntohs (msg->size);
2070 if (total_size > size)
2075 memcpy (buf, msg, total_size);
2082 * Function to send a create connection message to a peer.
2084 * @param c Connection to create.
2085 * @param size number of bytes available in buf
2086 * @param buf where the callee should write the message
2087 * @return number of bytes written to buf
2090 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
2092 struct GNUNET_MESH_ConnectionCreate *msg;
2093 struct GNUNET_PeerIdentity *peer_ptr;
2094 struct MeshPeerPath *p = c->path;
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
2100 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2101 p->length * sizeof (struct GNUNET_PeerIdentity);
2103 if (size < size_needed || NULL == buf)
2108 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
2109 msg->header.size = htons (size_needed);
2110 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
2113 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2114 for (i = 0; i < p->length; i++)
2116 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2120 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
2126 * Creates a path ack message in buf and frees all unused resources.
2128 * @param c Connection to send an ACK on.
2129 * @param size number of bytes available in buf
2130 * @param buf where the callee should write the message
2132 * @return number of bytes written to buf
2135 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
2137 struct GNUNET_MESH_ConnectionACK *msg = buf;
2138 struct MeshTunnel2 *t = c->t;
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
2141 GNUNET_assert (NULL != t);
2142 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
2147 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
2148 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
2152 /* TODO add signature */
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2155 return sizeof (struct GNUNET_MESH_ConnectionACK);
2160 * Destroy the peer_info and free any allocated resources linked to it
2162 * @param peer The peer_info to destroy.
2164 * @return GNUNET_OK on success
2167 peer_destroy (struct MeshPeer *peer)
2169 struct GNUNET_PeerIdentity id;
2170 struct MeshPeerPath *p;
2171 struct MeshPeerPath *nextp;
2173 GNUNET_PEER_resolve (peer->id, &id);
2174 GNUNET_PEER_change_rc (peer->id, -1);
2177 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2181 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2183 if (NULL != peer->dhtget)
2185 GNUNET_DHT_get_stop (peer->dhtget);
2187 p = peer->path_head;
2191 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2195 tunnel_destroy_empty (peer->tunnel);
2202 * Returns if peer is used (has a tunnel, is neighbor).
2204 * @peer Peer to check.
2206 * @return GNUNET_YES if peer is in use.
2209 peer_is_used (struct MeshPeer *peer)
2211 struct MeshPeerPath *p;
2213 if (NULL != peer->tunnel)
2216 for (p = peer->path_head; NULL != p; p = p->next)
2225 * Iterator over all the peers to get the oldest timestamp.
2227 * @param cls Closure (unsued).
2228 * @param key ID of the peer.
2229 * @param value Peer_Info of the peer.
2232 peer_get_oldest (void *cls,
2233 const struct GNUNET_HashCode *key,
2236 struct MeshPeer *p = value;
2237 struct GNUNET_TIME_Absolute *abs = cls;
2239 /* Don't count active peers */
2240 if (GNUNET_YES == peer_is_used (p))
2243 if (abs->abs_value_us < p->last_contact.abs_value_us)
2244 abs->abs_value_us = p->last_contact.abs_value_us;
2251 * Iterator over all the peers to remove the oldest entry.
2253 * @param cls Closure (unsued).
2254 * @param key ID of the peer.
2255 * @param value Peer_Info of the peer.
2258 peer_timeout (void *cls,
2259 const struct GNUNET_HashCode *key,
2262 struct MeshPeer *p = value;
2263 struct GNUNET_TIME_Absolute *abs = cls;
2265 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2266 GNUNET_NO == peer_is_used (p))
2276 * Delete oldest unused peer.
2279 peer_delete_oldest (void)
2281 struct GNUNET_TIME_Absolute abs;
2283 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2285 GNUNET_CONTAINER_multihashmap_iterate (peers,
2288 GNUNET_CONTAINER_multihashmap_iterate (peers,
2295 * Retrieve the MeshPeer stucture associated with the peer, create one
2296 * and insert it in the appropriate structures if the peer is not known yet.
2298 * @param peer Full identity of the peer.
2300 * @return Existing or newly created peer info.
2302 static struct MeshPeer *
2303 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2305 struct MeshPeer *peer;
2307 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2310 peer = GNUNET_new (struct MeshPeer);
2311 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2313 peer_delete_oldest ();
2315 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2316 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2317 peer->id = GNUNET_PEER_intern (peer_id);
2319 peer->last_contact = GNUNET_TIME_absolute_get();
2326 * Retrieve the MeshPeer stucture associated with the peer, create one
2327 * and insert it in the appropriate structures if the peer is not known yet.
2329 * @param peer Short identity of the peer.
2331 * @return Existing or newly created peer info.
2333 static struct MeshPeer *
2334 peer_get_short (const GNUNET_PEER_Id peer)
2336 return peer_get (GNUNET_PEER_resolve2 (peer));
2341 * Get a cost of a path for a peer considering existing tunnel connections.
2343 * @param peer Peer towards which the path is considered.
2344 * @param path Candidate path.
2346 * @return Cost of the path (path length + number of overlapping nodes)
2349 peer_get_path_cost (const struct MeshPeer *peer,
2350 const struct MeshPeerPath *path)
2352 struct MeshConnection *c;
2353 unsigned int overlap;
2361 GNUNET_assert (NULL != peer->tunnel);
2363 for (i = 0; i < path->length; i++)
2365 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2367 for (j = 0; j < c->path->length; j++)
2369 if (path->peers[i] == c->path->peers[j])
2377 return (path->length + overlap) * (path->score * -1);
2382 * Choose the best path towards a peer considering the tunnel properties.
2384 * @param peer The destination peer.
2386 * @return Best current known path towards the peer, if any.
2388 static struct MeshPeerPath *
2389 peer_get_best_path (const struct MeshPeer *peer)
2391 struct MeshPeerPath *best_p;
2392 struct MeshPeerPath *p;
2393 struct MeshConnection *c;
2394 unsigned int best_cost;
2397 best_cost = UINT_MAX;
2399 for (p = peer->path_head; NULL != p; p = p->next)
2401 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2405 continue; /* If path is in use in a connection, skip it. */
2407 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2417 queue_is_sendable (struct MeshPeerQueue *q)
2419 struct MeshFlowControl *fc;
2421 /* Is PID-independent? */
2424 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2425 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2429 /* Is PID allowed? */
2430 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2431 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2439 * Get first sendable message.
2441 * @param peer The destination peer.
2443 * @return Best current known path towards the peer, if any.
2445 static struct MeshPeerQueue *
2446 peer_get_first_message (const struct MeshPeer *peer)
2448 struct MeshPeerQueue *q;
2450 for (q = peer->queue_head; NULL != q; q = q->next)
2452 if (queue_is_sendable (q))
2461 * Try to establish a new connection to this peer in the given tunnel.
2462 * If the peer doesn't have any path to it yet, try to get one.
2463 * If the peer already has some path, send a CREATE CONNECTION towards it.
2465 * @param peer PeerInfo of the peer.
2468 peer_connect (struct MeshPeer *peer)
2470 struct MeshTunnel2 *t;
2471 struct MeshPeerPath *p;
2472 struct MeshConnection *c;
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "peer_connect towards %s\n",
2480 rerun_dhtget = GNUNET_NO;
2482 if (NULL != peer->path_head)
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2485 p = peer_get_best_path (peer);
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2489 c = tunnel_use_path (t, p);
2492 /* This case can happen when the path includes a first hop that is
2493 * not yet known to be connected.
2495 * This happens quite often during testing when running mesh
2496 * under valgrind: core connect notifications come very late and the
2497 * DHT result has already come and created a valid path.
2498 * In this case, the peer->connections hashmap will be NULL and
2499 * tunnel_use_path will not be able to create a connection from that
2502 * Re-running the DHT GET should give core time to callback.
2505 rerun_dhtget = GNUNET_YES;
2509 send_connection_create (c);
2515 if (NULL != peer->dhtget && GNUNET_YES == rerun_dhtget)
2517 GNUNET_DHT_get_stop (peer->dhtget);
2518 peer->dhtget = NULL;
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 " Stopping DHT GET for peer %s\n", peer2s (peer));
2523 if (NULL == peer->dhtget)
2525 const struct GNUNET_PeerIdentity *id;
2527 id = GNUNET_PEER_resolve2 (peer->id);
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 " Starting DHT GET for peer %s\n", peer2s (peer));
2530 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2531 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2532 &id->hashPubKey, /* key to search */
2533 dht_replication_level, /* replication level */
2534 GNUNET_DHT_RO_RECORD_ROUTE |
2535 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2537 0, /* xquery bits */
2538 &dht_get_id_handler, peer);
2539 if (MESH_TUNNEL_NEW == t->state)
2540 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2546 * Get the first transmittable message for a connection.
2548 * @param c Connection.
2549 * @param fwd Is this FWD?
2551 * @return First transmittable message.
2553 static struct MeshPeerQueue *
2554 connection_get_first_message (struct MeshConnection *c, int fwd)
2556 struct MeshPeerQueue *q;
2559 p = connection_get_hop (c, fwd);
2561 for (q = p->queue_head; NULL != q; q = q->next)
2565 if (queue_is_sendable (q))
2574 * @brief Re-initiate traffic on this connection if necessary.
2576 * Check if there is traffic queued towards this peer
2577 * and the core transmit handle is NULL (traffic was stalled).
2578 * If so, call core tmt rdy.
2580 * @param c Connection on which initiate traffic.
2581 * @param fwd Is this about fwd traffic?
2584 connection_unlock_queue (struct MeshConnection *c, int fwd)
2586 struct MeshPeer *peer;
2587 struct MeshPeerQueue *q;
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 "connection_unlock_queue %s on %s\n",
2592 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2594 if (connection_is_origin (c, fwd))
2596 struct MeshTunnel2 *t = c->t;
2597 struct MeshChannel *ch;
2598 struct MeshChannelReliability *rel;
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2601 /* FIXME randomize channel selection, not always first channel */
2602 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2604 rel = fwd ? ch->root_rel : ch->dest_rel;
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2607 ch->gid, rel->client_ready ? "ready " : "not ready");
2608 if (GNUNET_NO == rel->client_ready)
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2611 send_local_ack (ch, fwd);
2612 return; /* FIXME authorize all channels? */
2618 peer = connection_get_hop (c, fwd);
2620 if (NULL != peer->core_transmit)
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2623 return; /* Already unlocked */
2626 q = connection_get_first_message (c, fwd);
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2630 return; /* Nothing to transmit */
2634 peer->core_transmit =
2635 GNUNET_CORE_notify_transmit_ready (core_handle,
2638 GNUNET_TIME_UNIT_FOREVER_REL,
2639 GNUNET_PEER_resolve2 (peer->id),
2647 * Cancel all transmissions that belong to a certain connection.
2649 * @param c Connection which to cancel.
2650 * @param fwd Cancel fwd traffic?
2653 connection_cancel_queues (struct MeshConnection *c, int fwd)
2655 struct MeshPeerQueue *q;
2656 struct MeshPeerQueue *next;
2657 struct MeshFlowControl *fc;
2658 struct MeshPeer *peer;
2665 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2666 peer = connection_get_hop (c, fwd);
2668 for (q = peer->queue_head; NULL != q; q = next)
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 "connection_cancel_queue %s\n",
2675 GNUNET_MESH_DEBUG_M2S (q->type));
2676 queue_destroy (q, GNUNET_YES);
2679 if (NULL == peer->queue_head)
2681 if (NULL != peer->core_transmit)
2683 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2684 peer->core_transmit = NULL;
2686 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2688 GNUNET_SCHEDULER_cancel (fc->poll_task);
2689 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2696 * Add the path to the peer and update the path used to reach it in case this
2699 * @param peer_info Destination peer to add the path to.
2700 * @param path New path to add. Last peer must be the peer in arg 1.
2701 * Path will be either used of freed if already known.
2702 * @param trusted Do we trust that this path is real?
2705 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2708 struct MeshPeerPath *aux;
2712 if ((NULL == peer_info) || (NULL == path))
2715 path_destroy (path);
2718 if (path->peers[path->length - 1] != peer_info->id)
2721 path_destroy (path);
2724 if (2 >= path->length && GNUNET_NO == trusted)
2726 /* Only allow CORE to tell us about direct paths */
2727 path_destroy (path);
2730 for (l = 1; l < path->length; l++)
2732 if (path->peers[l] == myid)
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2735 for (l2 = 0; l2 < path->length - l; l2++)
2737 path->peers[l2] = path->peers[l + l2];
2742 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2747 path->length, peer2s (peer_info));
2749 l = path_get_length (path);
2752 path_destroy (path);
2756 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2757 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2759 l2 = path_get_length (aux);
2762 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2763 peer_info->path_tail, aux, path);
2768 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2770 path_destroy (path);
2775 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2782 * Add the path to the origin peer and update the path used to reach it in case
2783 * this is the shortest.
2784 * The path is given in peer_info -> destination, therefore we turn the path
2785 * upside down first.
2787 * @param peer_info Peer to add the path to, being the origin of the path.
2788 * @param path New path to add after being inversed.
2789 * Path will be either used or freed.
2790 * @param trusted Do we trust that this path is real?
2793 peer_add_path_to_origin (struct MeshPeer *peer_info,
2794 struct MeshPeerPath *path, int trusted)
2799 peer_add_path (peer_info, path, trusted);
2805 * Function called if a connection has been stalled for a while,
2806 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2808 * @param cls Closure (poll ctx).
2809 * @param tc TaskContext.
2812 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2814 struct MeshFlowControl *fc = cls;
2815 struct GNUNET_MESH_Poll msg;
2816 struct MeshConnection *c;
2818 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2819 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2827 peer2s (c->t->peer), c->id);
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
2829 fc == &c->fwd_fc ? "FWD" : "BCK");
2831 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2832 msg.header.size = htons (sizeof (msg));
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2834 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2835 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2836 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2837 &connection_poll, fc);
2842 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2843 * to obtain a local peer -> destination path and interning the peer ids.
2845 * @return Newly allocated and created path
2847 static struct MeshPeerPath *
2848 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2849 unsigned int get_path_length,
2850 const struct GNUNET_PeerIdentity *put_path,
2851 unsigned int put_path_length)
2853 struct MeshPeerPath *p;
2859 GNUNET_PEER_change_rc (myid, 1);
2860 i = get_path_length;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2862 for (i--; i >= 0; i--)
2864 id = GNUNET_PEER_intern (&get_path[i]);
2865 if (p->length > 0 && id == p->peers[p->length - 1])
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2868 GNUNET_PEER_change_rc (id, -1);
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2873 GNUNET_i2s (&get_path[i]));
2875 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2876 p->peers[p->length - 1] = id;
2879 i = put_path_length;
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2881 for (i--; i >= 0; i--)
2883 id = GNUNET_PEER_intern (&put_path[i]);
2886 /* PUT path went through us, so discard the path up until now and start
2887 * from here to get a much shorter (and loop-free) path.
2892 if (p->length > 0 && id == p->peers[p->length - 1])
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2895 GNUNET_PEER_change_rc (id, -1);
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2900 GNUNET_i2s (&put_path[i]));
2902 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2903 p->peers[p->length - 1] = id;
2907 if (get_path_length > 0)
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2909 GNUNET_i2s (&get_path[0]));
2910 if (put_path_length > 0)
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2912 GNUNET_i2s (&put_path[0]));
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2915 for (i = 0; i < p->length; i++)
2917 struct GNUNET_PeerIdentity peer_id;
2919 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2921 GNUNET_i2s (&peer_id));
2929 * Adds a path to the peer_infos of all the peers in the path
2931 * @param p Path to process.
2932 * @param confirmed Whether we know if the path works or not.
2935 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2939 /* TODO: invert and add */
2940 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2941 for (i++; i < p->length; i++)
2943 struct MeshPeer *aux;
2944 struct MeshPeerPath *copy;
2946 aux = peer_get_short (p->peers[i]);
2947 copy = path_duplicate (p);
2948 copy->length = i + 1;
2949 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2955 * Search for a channel among the channels for a client
2957 * @param c the client whose channels to search in
2958 * @param chid the local id of the channel
2960 * @return channel handler, NULL if doesn't exist
2962 static struct MeshChannel *
2963 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2966 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2968 GNUNET_break_op (0);
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2972 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2973 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2974 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2980 channel_debug (struct MeshChannel *ch)
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
2987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X\n",
2988 peer2s (ch->t->peer), ch->gid);
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2990 ch->root, ch->root_rel);
2991 if (NULL != ch->root)
2993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
2995 ch->root_rel->client_ready ? "YES" : "NO");
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2999 ch->dest, ch->dest_rel);
3000 if (NULL != ch->dest)
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
3004 ch->dest_rel->client_ready ? "YES" : "NO");
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
3010 fc_debug (struct MeshFlowControl *fc)
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
3013 fc->last_pid_recv, fc->last_ack_sent);
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
3015 fc->last_pid_sent, fc->last_ack_recv);
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
3017 fc->queue_n, fc->queue_max);
3021 connection_debug (struct MeshConnection *c)
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
3029 peer2s (c->t->peer), GNUNET_h2s (&c->id));
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
3031 c->state, c->pending_messages);
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
3033 fc_debug (&c->fwd_fc);
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
3035 fc_debug (&c->bck_fc);
3040 static struct MeshChannel *
3041 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
3043 struct MeshChannel *ch;
3048 for (ch = t->channel_head; NULL != ch; ch = ch->next)
3050 if (ch->gid == chid)
3059 * Change the tunnel state.
3061 * @param t Tunnel whose state to change.
3062 * @param state New state.
3065 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3070 "Tunnel %s state was %s\n",
3072 GNUNET_MESH_DEBUG_TS2S (t->state));
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3074 "Tunnel %s state is now %s\n",
3076 GNUNET_MESH_DEBUG_TS2S (state));
3082 * Cache a message to be sent once tunnel is online.
3084 * @param t Tunnel to hold the message.
3085 * @param ch Channel the message is about.
3086 * @param msg Message itself (copy will be made).
3087 * @param fwd Is this fwd?
3090 tunnel_queue_data (struct MeshTunnel2 *t,
3091 struct MeshChannel *ch,
3092 struct GNUNET_MessageHeader *msg,
3095 struct MeshTunnelQueue *tq;
3096 uint16_t size = ntohs (msg->size);
3098 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
3101 memcpy (&tq[1], msg, size);
3102 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
3107 * Send all cached messages that we can, tunnel is online.
3109 * @param t Tunnel that holds the messages.
3110 * @param fwd Is this fwd?
3113 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
3115 struct MeshTunnelQueue *tq;
3116 struct MeshTunnelQueue *next;
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3120 "tunnel_send_queued_data on tunnel %s\n",
3122 room = tunnel_get_buffer (t, fwd);
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
3124 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
3128 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
3129 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
3138 connection_change_state (struct MeshConnection* c,
3139 enum MeshConnectionState state)
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "Connection %s state was %s\n",
3143 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 "Connection %s state is now %s\n",
3146 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
3152 * Add a client to a channel, initializing all needed data structures.
3154 * @param ch Channel to which add the client.
3155 * @param c Client which to add to the channel.
3158 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3160 struct MeshTunnel2 *t = ch->t;
3162 if (NULL != ch->dest)
3168 /* Assign local id as destination */
3169 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3170 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3171 ch->lid_dest = t->next_local_chid++;
3172 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3174 /* Store in client's hashmap */
3176 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3178 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3184 GNUNET_break (NULL == ch->dest_rel);
3185 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3186 ch->dest_rel->ch = ch;
3187 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3193 static struct MeshConnection *
3194 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3196 struct MeshConnection *c;
3197 struct GNUNET_HashCode cid;
3198 struct MeshPeer *peer;
3199 unsigned int own_pos;
3201 if (NULL == t || NULL == p)
3207 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3209 c = connection_new (&cid);
3211 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
3212 for (own_pos = 0; own_pos < p->length; own_pos++)
3214 if (p->peers[own_pos] == myid)
3217 if (own_pos > p->length - 1)
3220 connection_destroy (c);
3223 c->own_pos = own_pos;
3228 c->fwd_maintenance_task =
3229 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3230 &connection_fwd_keepalive, c);
3233 peer = connection_get_next_hop (c);
3234 if (NULL == peer->connections)
3236 connection_destroy (c);
3239 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3241 peer = connection_get_prev_hop (c);
3242 if (NULL == peer->connections)
3244 connection_destroy (c);
3247 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3248 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3254 * Notifies a tunnel that a connection has broken that affects at least
3255 * some of its peers. Sends a notification towards the root of the tree.
3256 * In case the peer is the owner of the tree, notifies the client that owns
3257 * the tunnel and tries to reconnect.
3259 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3261 * @param t Tunnel affected.
3262 * @param p1 Peer that got disconnected from p2.
3263 * @param p2 Peer that got disconnected from p1.
3265 * @return Short ID of the peer disconnected (either p1 or p2).
3266 * 0 if the tunnel remained unaffected.
3268 static GNUNET_PEER_Id
3269 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3270 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3272 // if (myid != p1 && myid != p2) FIXME
3277 // if (tree_get_predecessor (t->tree) != 0)
3279 // /* We are the peer still connected, notify owner of the disconnection. */
3280 // struct GNUNET_MESH_PathBroken msg;
3281 // struct GNUNET_PeerIdentity neighbor;
3283 // msg.header.size = htons (sizeof (msg));
3284 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3285 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3286 // msg.tid = htonl (t->id.tid);
3287 // msg.peer1 = my_full_id;
3288 // GNUNET_PEER_resolve (pid, &msg.peer2);
3289 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3290 // send_prebuilt_message (&msg.header, &neighbor, t);
3297 * Send an end-to-end ACK message for the most recent in-sequence payload.
3299 * If channel is not reliable, do nothing.
3301 * @param ch Channel this is about.
3302 * @param fwd Is for FWD traffic? (ACK dest->owner)
3305 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3307 struct GNUNET_MESH_DataACK msg;
3308 struct MeshChannelReliability *rel;
3309 struct MeshReliableMessage *copy;
3314 if (GNUNET_NO == ch->reliable)
3318 rel = fwd ? ch->dest_rel : ch->root_rel;
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 "send_data_ack for %u\n",
3323 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3324 msg.header.type = htons (type);
3325 msg.header.size = htons (sizeof (msg));
3326 msg.chid = htonl (ch->gid);
3327 msg.mid = htonl (rel->mid_recv - 1);
3329 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3331 if (copy->type != type)
3333 delta = copy->mid - rel->mid_recv;
3336 mask = 0x1LL << delta;
3337 msg.futures |= mask;
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3339 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3340 copy->mid, delta, mask, msg.futures);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3344 send_prebuilt_message_channel (&msg.header, ch, fwd);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3350 * Send an ACK informing the predecessor about the available buffer space.
3352 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3353 * the ACK itself goes "back" (dest->root).
3355 * @param c Connection on which to send the ACK.
3356 * @param buffer How much space free to advertise?
3357 * @param fwd Is this FWD ACK? (Going dest->owner)
3360 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3362 struct MeshFlowControl *next_fc;
3363 struct MeshFlowControl *prev_fc;
3364 struct GNUNET_MESH_ACK msg;
3368 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3369 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 "connection send %s ack on %s\n",
3373 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3375 /* Check if we need to transmit the ACK */
3376 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 " last pid recv: %u, last ack sent: %u\n",
3381 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3385 /* Ok, ACK might be necessary, what PID to ACK? */
3386 delta = next_fc->queue_max - next_fc->queue_n;
3387 ack = prev_fc->last_pid_recv + delta;
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3390 " last pid %u, last ack %u, qmax %u, q %u\n",
3391 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3392 next_fc->queue_max, next_fc->queue_n);
3393 if (ack == prev_fc->last_ack_sent)
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3399 prev_fc->last_ack_sent = ack;
3401 /* Build ACK message and send on connection */
3402 msg.header.size = htons (sizeof (msg));
3403 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3404 msg.ack = htonl (ack);
3407 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3412 * Modify the mesh message TID from global to local and send to client.
3414 * @param ch Channel on which to send the message.
3415 * @param msg Message to modify and send.
3416 * @param c Client to send to.
3417 * @param tid Tunnel ID to use (c can be both owner and client).
3420 channel_send_client_to_tid (struct MeshChannel *ch,
3421 const struct GNUNET_MESH_Data *msg,
3422 struct MeshClient *c, MESH_ChannelNumber id)
3424 struct GNUNET_MESH_LocalData *copy;
3425 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3426 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3428 if (size < sizeof (struct GNUNET_MessageHeader))
3430 GNUNET_break_op (0);
3438 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3439 memcpy (©[1], &msg[1], size);
3440 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3441 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3442 copy->id = htonl (id);
3443 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3444 ©->header, GNUNET_NO);
3449 * We have received a message out of order, or the client is not ready.
3450 * Buffer it until we receive an ACK from the client or the missing
3451 * message from the channel.
3453 * @param msg Message to buffer (MUST be of type MESH_DATA).
3454 * @param rel Reliability data to the corresponding direction.
3457 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3458 struct MeshChannelReliability *rel)
3460 struct MeshReliableMessage *copy;
3461 struct MeshReliableMessage *prev;
3465 size = ntohs (msg->header.size);
3466 mid = ntohl (msg->mid);
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3470 copy = GNUNET_malloc (sizeof (*copy) + size);
3473 memcpy (©[1], msg, size);
3477 // FIXME do something better than O(n), although n < 64...
3478 // FIXME start from the end (most messages are the latest ones)
3479 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3482 if (GMC_is_pid_bigger (prev->mid, mid))
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3485 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3491 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3497 channel_send_client_data (struct MeshChannel *ch,
3498 const struct GNUNET_MESH_Data *msg,
3503 if (ch->dest_rel->client_ready)
3504 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3506 channel_rel_add_buffered_data (msg, ch->dest_rel);
3510 if (ch->root_rel->client_ready)
3511 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3513 channel_rel_add_buffered_data (msg, ch->root_rel);
3519 * Send a buffered message to the client, for in order delivery or
3520 * as result of client ACK.
3522 * @param ch Channel on which to empty the message buffer.
3523 * @param c Client to send to.
3524 * @param rel Reliability structure to corresponding peer.
3525 * If rel == bck_rel, this is FWD data.
3528 channel_send_client_buffered_data (struct MeshChannel *ch,
3529 struct MeshClient *c,
3532 struct MeshReliableMessage *copy;
3533 struct MeshChannelReliability *rel;
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3536 rel = fwd ? ch->dest_rel : ch->root_rel;
3537 if (GNUNET_NO == rel->client_ready)
3539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3543 copy = rel->head_recv;
3544 /* We never buffer channel management messages */
3547 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3549 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3552 " have %u! now expecting %u\n",
3553 copy->mid, rel->mid_recv + 1);
3554 channel_send_client_data (ch, msg, fwd);
3557 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 " reliable && don't have %u, next is %u\n",
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3574 * Destroy a reliable message after it has been acknowledged, either by
3575 * direct mid ACK or bitfield. Updates the appropriate data structures and
3576 * timers and frees all memory.
3578 * @param copy Message that is no longer needed: remote peer got it.
3581 rel_message_free (struct MeshReliableMessage *copy)
3583 struct MeshChannelReliability *rel;
3584 struct GNUNET_TIME_Relative time;
3587 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3588 rel->expected_delay.rel_value_us *= 7;
3589 rel->expected_delay.rel_value_us += time.rel_value_us;
3590 rel->expected_delay.rel_value_us /= 8;
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3595 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3597 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3599 rel->retry_timer = rel->expected_delay;
3600 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3606 * Destroy all reliable messages queued for a channel,
3607 * during a channel destruction.
3608 * Frees the reliability structure itself.
3610 * @param rel Reliability data for a channel.
3613 channel_rel_free_all (struct MeshChannelReliability *rel)
3615 struct MeshReliableMessage *copy;
3616 struct MeshReliableMessage *next;
3621 for (copy = rel->head_recv; NULL != copy; copy = next)
3624 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3627 for (copy = rel->head_sent; NULL != copy; copy = next)
3630 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3633 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3634 GNUNET_SCHEDULER_cancel (rel->retry_task);
3640 * Mark future messages as ACK'd.
3642 * @param rel Reliability data.
3643 * @param msg DataACK message with a bitfield of future ACK'd messages.
3646 channel_rel_free_sent (struct MeshChannelReliability *rel,
3647 const struct GNUNET_MESH_DataACK *msg)
3649 struct MeshReliableMessage *copy;
3650 struct MeshReliableMessage *next;
3657 bitfield = msg->futures;
3658 mid = ntohl (msg->mid);
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3660 "free_sent_reliable %u %llX\n",
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 " rel %p, head %p\n",
3664 rel, rel->head_sent);
3665 for (i = 0, copy = rel->head_sent;
3666 i < 64 && NULL != copy && 0 != bitfield;
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3670 " trying bit %u (mid %u)\n",
3673 if (0 == (bitfield & mask))
3676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3677 /* Bit was set, clear the bit from the bitfield */
3680 /* The i-th bit was set. Do we have that copy? */
3681 /* Skip copies with mid < target */
3682 target = mid + i + 1;
3683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3684 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3687 /* Did we run out of copies? (previously freed, it's ok) */
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3694 /* Did we overshoot the target? (previously freed, it's ok) */
3695 if (GMC_is_pid_bigger (copy->mid, target))
3697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3701 /* Now copy->mid == target, free it */
3703 rel_message_free (copy);
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3711 * We haven't received an ACK after a certain time: restransmit the message.
3713 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3714 * @param tc TaskContext.
3717 channel_retransmit_message (void *cls,
3718 const struct GNUNET_SCHEDULER_TaskContext *tc)
3720 struct MeshChannelReliability *rel = cls;
3721 struct MeshReliableMessage *copy;
3722 struct MeshPeerQueue *q;
3723 struct MeshChannel *ch;
3724 struct MeshConnection *c;
3725 struct GNUNET_MESH_Data *payload;
3726 struct MeshPeer *hop;
3729 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3730 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3734 copy = rel->head_sent;
3741 /* Search the message to be retransmitted in the outgoing queue.
3742 * Check only the queue for the connection that is going to be used,
3743 * if the message is stuck in some other connection's queue we shouldn't
3745 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3746 * the old connection could be temporary stalled or the queue happened to
3747 * be long at time of insertion.
3748 * - not sending the new one could cause terrible delays the old connection
3751 payload = (struct GNUNET_MESH_Data *) ©[1];
3752 fwd = (rel == ch->root_rel);
3753 c = tunnel_get_connection (ch->t, fwd);
3754 hop = connection_get_hop (c, fwd);
3755 for (q = hop->queue_head; NULL != q; q = q->next)
3757 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3759 struct GNUNET_MESH_Data *queued_data = q->cls;
3761 if (queued_data->mid == payload->mid)
3766 /* Message not found in the queue that we are going to use. */
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3771 send_prebuilt_message_channel (&payload->header, ch, fwd);
3772 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3779 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3780 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3781 &channel_retransmit_message,
3787 * Send ACK on one or more connections due to buffer space to the client.
3789 * Iterates all connections of the tunnel and sends ACKs appropriately.
3791 * @param ch Channel which has some free buffer space.
3792 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3795 channel_send_connections_ack (struct MeshChannel *ch,
3796 unsigned int buffer,
3799 struct MeshTunnel2 *t = ch->t;
3800 struct MeshConnection *c;
3801 struct MeshFlowControl *fc;
3804 uint32_t allow_per_connection;
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3808 "Channel send connection %s ack on %s:%X\n",
3809 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3811 /* Count connections, how many messages are already allowed */
3812 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3814 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3815 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3820 allowed += fc->last_ack_sent - fc->last_pid_recv;
3824 /* Make sure there is no overflow */
3825 if (allowed > buffer)
3831 /* Authorize connections to send more data */
3832 to_allow = buffer - allowed;
3834 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3836 allow_per_connection = to_allow/cs;
3837 to_allow -= allow_per_connection;
3839 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3840 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3844 connection_send_ack (c, allow_per_connection, fwd);
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "Channel send connection %s ack on %s:%X\n",
3849 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3850 GNUNET_break (to_allow == 0);
3855 * Send an ACK on the appropriate connection/channel, depending on
3856 * the direction and the position of the peer.
3858 * @param c Which connection to send the hop-by-hop ACK.
3859 * @param ch Channel, if any.
3860 * @param fwd Is this a fwd ACK? (will go dest->root)
3863 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3865 unsigned int buffer;
3867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3868 "send ack %s on %p %p\n",
3869 fwd ? "FWD" : "BCK", c, ch);
3870 if (NULL == c || connection_is_terminal (c, fwd))
3872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Channel\n");
3873 buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Connection\n");
3878 GNUNET_assert (NULL != c);
3879 buffer = connection_get_buffer (c, fwd);
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
3886 GNUNET_assert (NULL != ch);
3887 channel_send_connections_ack (ch, buffer, fwd);
3889 else if (connection_is_origin (c, fwd))
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
3895 GNUNET_assert (NULL != ch);
3896 send_local_ack (ch, fwd);
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3902 connection_send_ack (c, buffer, fwd);
3908 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3910 * @param ch Channel to mark as ready.
3911 * @param fwd Was the CREATE message sent fwd?
3914 channel_confirm (struct MeshChannel *ch, int fwd)
3916 struct MeshChannelReliability *rel;
3917 struct MeshReliableMessage *copy;
3918 struct MeshReliableMessage *next;
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3921 " channel confirm %s %s:%X\n",
3922 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3923 ch->state = MESH_CHANNEL_READY;
3925 rel = fwd ? ch->root_rel : ch->dest_rel;
3926 for (copy = rel->head_sent; NULL != copy; copy = next)
3928 struct GNUNET_MessageHeader *msg;
3931 msg = (struct GNUNET_MessageHeader *) ©[1];
3932 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3934 rel_message_free (copy);
3938 if (GNUNET_NO == rel->client_ready)
3939 send_local_ack (ch, fwd);
3944 * Save a copy to retransmit in case it gets lost.
3946 * Initializes all needed callbacks and timers.
3948 * @param ch Channel this message goes on.
3949 * @param msg Message to copy.
3950 * @param fwd Is this fwd traffic?
3953 channel_save_copy (struct MeshChannel *ch,
3954 const struct GNUNET_MessageHeader *msg,
3957 struct MeshChannelReliability *rel;
3958 struct MeshReliableMessage *copy;
3963 rel = fwd ? ch->root_rel : ch->dest_rel;
3964 mid = rel->mid_send;
3965 type = ntohs (msg->type);
3966 size = ntohs (msg->size);
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3969 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3971 copy->timestamp = GNUNET_TIME_absolute_get ();
3974 memcpy (©[1], msg, size);
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3977 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3978 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3981 GNUNET_TIME_relative_multiply (rel->expected_delay,
3982 MESH_RETRANSMIT_MARGIN);
3984 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3985 &channel_retransmit_message,
3992 * Send keepalive packets for a connection.
3994 * @param c Connection to keep alive..
3995 * @param fwd Is this a FWD keepalive? (owner -> dest).
3998 connection_keepalive (struct MeshConnection *c, int fwd)
4000 struct GNUNET_MESH_ConnectionKeepAlive *msg;
4001 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
4005 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
4006 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 "sending %s keepalive for connection %s[%d]\n",
4010 fwd ? "FWD" : "BCK",
4011 peer2s (c->t->peer),
4014 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
4015 msg->header.size = htons (size);
4016 msg->header.type = htons (type);
4019 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
4024 * Send CONNECTION_{CREATE/ACK} packets for a connection.
4026 * @param c Connection for which to send the message.
4027 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
4030 connection_recreate (struct MeshConnection *c, int fwd)
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
4034 send_connection_create (c);
4036 send_connection_ack (c, GNUNET_NO);
4041 * Generic connection timer management.
4042 * Depending on the role of the peer in the connection will send the
4043 * appropriate message (build or keepalive)
4045 * @param c Conncetion to maintain.
4046 * @param fwd Is FWD?
4049 connection_maintain (struct MeshConnection *c, int fwd)
4051 if (MESH_TUNNEL_SEARCHING == c->t->state)
4053 /* TODO DHT GET with RO_BART */
4058 case MESH_CONNECTION_NEW:
4060 case MESH_CONNECTION_SENT:
4061 connection_recreate (c, fwd);
4063 case MESH_CONNECTION_READY:
4064 connection_keepalive (c, fwd);
4073 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4075 struct MeshConnection *c = cls;
4077 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4078 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4081 connection_maintain (c, GNUNET_YES);
4082 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4083 &connection_fwd_keepalive,
4089 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4091 struct MeshConnection *c = cls;
4093 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4094 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4097 connection_maintain (c, GNUNET_NO);
4098 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4099 &connection_bck_keepalive,
4105 * Send a message to all peers in this connection that the connection
4106 * is no longer valid.
4108 * If some peer should not receive the message, it should be zero'ed out
4109 * before calling this function.
4111 * @param c The connection whose peers to notify.
4114 connection_send_destroy (struct MeshConnection *c)
4116 struct GNUNET_MESH_ConnectionDestroy msg;
4118 msg.header.size = htons (sizeof (msg));
4119 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4122 " sending connection destroy for connection %s[%X]\n",
4123 peer2s (c->t->peer),
4126 if (GNUNET_NO == connection_is_terminal (c, GNUNET_YES))
4127 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
4128 if (GNUNET_NO == connection_is_terminal (c, GNUNET_NO))
4129 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
4130 c->destroy = GNUNET_YES;
4135 * Confirm we got a channel create.
4137 * @param ch The channel to confirm.
4138 * @param fwd Should we send the ACK fwd?
4141 channel_send_ack (struct MeshChannel *ch, int fwd)
4143 struct GNUNET_MESH_ChannelManage msg;
4145 msg.header.size = htons (sizeof (msg));
4146 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4148 " sending channel %s ack for channel %s:%X\n",
4149 fwd ? "FWD" : "BCK", peer2s (ch->t->peer),
4152 msg.chid = htonl (ch->gid);
4153 send_prebuilt_message_channel (&msg.header, ch, !fwd);
4158 * Send a message to all clients (local and remote) of this channel
4159 * notifying that the channel is no longer valid.
4161 * If some peer or client should not receive the message,
4162 * should be zero'ed out before calling this function.
4164 * @param ch The channel whose clients to notify.
4167 channel_send_destroy (struct MeshChannel *ch)
4169 struct GNUNET_MESH_ChannelManage msg;
4171 msg.header.size = htons (sizeof (msg));
4172 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4174 " sending channel destroy for channel %s:%X\n",
4175 peer2s (ch->t->peer),
4178 if (channel_is_terminal (ch, GNUNET_NO))
4180 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
4182 msg.chid = htonl (ch->lid_root);
4183 send_local_channel_destroy (ch, GNUNET_NO);
4188 msg.chid = htonl (ch->gid);
4189 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4192 if (channel_is_terminal (ch, GNUNET_YES))
4194 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
4196 msg.chid = htonl (ch->lid_dest);
4197 send_local_channel_destroy (ch, GNUNET_YES);
4202 msg.chid = htonl (ch->gid);
4203 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4211 static struct MeshTunnel2 *
4214 struct MeshTunnel2 *t;
4216 t = GNUNET_new (struct MeshTunnel2);
4218 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4220 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4221 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4223 // GNUNET_break (0);
4224 // tunnel_destroy (t);
4228 // char salt[] = "salt";
4229 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4230 // salt, sizeof (salt),
4231 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4232 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4233 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4235 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4236 // salt, sizeof (salt),
4237 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4238 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4239 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4247 * Add a connection to a tunnel.
4250 * @param c Connection.
4253 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4255 struct MeshConnection *aux;
4257 for (aux = t->connection_head; aux != NULL; aux = aux->next)
4260 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4265 * Initialize a Flow Control structure to the initial state.
4267 * @param fc Flow Control structure to initialize.
4270 fc_init (struct MeshFlowControl *fc)
4273 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4274 fc->last_pid_recv = (uint32_t) -1;
4275 fc->last_ack_sent = (uint32_t) 0;
4276 fc->last_ack_recv = (uint32_t) 0;
4277 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4278 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4280 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4284 static struct MeshConnection *
4285 connection_new (const struct GNUNET_HashCode *cid)
4287 struct MeshConnection *c;
4289 c = GNUNET_new (struct MeshConnection);
4291 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4293 fc_init (&c->fwd_fc);
4294 fc_init (&c->bck_fc);
4303 * Find a connection.
4305 * @param cid Connection ID.
4307 static struct MeshConnection *
4308 connection_get (const struct GNUNET_HashCode *cid)
4310 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4315 connection_destroy (struct MeshConnection *c)
4317 struct MeshPeer *peer;
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4323 peer2s (c->t->peer),
4326 /* Cancel all traffic */
4327 connection_cancel_queues (c, GNUNET_YES);
4328 connection_cancel_queues (c, GNUNET_NO);
4330 /* Cancel maintainance task (keepalive/timeout) */
4331 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4332 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4333 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4334 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4336 /* Deregister from neighbors */
4337 peer = connection_get_next_hop (c);
4338 if (NULL != peer && NULL != peer->connections)
4339 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4340 peer = connection_get_prev_hop (c);
4341 if (NULL != peer && NULL != peer->connections)
4342 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4345 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4346 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4352 tunnel_destroy (struct MeshTunnel2 *t)
4354 struct MeshConnection *c;
4355 struct MeshConnection *next;
4360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4363 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4364 // GNUNET_break (0);
4366 for (c = t->connection_head; NULL != c; c = next)
4369 connection_destroy (c);
4372 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4379 * Tunnel is empty: destroy it.
4381 * Notifies all connections about the destruction.
4383 * @param t Tunnel to destroy.
4386 tunnel_destroy_empty (struct MeshTunnel2 *t)
4388 struct MeshConnection *c;
4390 for (c = t->connection_head; NULL != c; c = c->next)
4392 if (GNUNET_NO == c->destroy)
4393 connection_send_destroy (c);
4396 if (0 == t->pending_messages)
4399 t->destroy = GNUNET_YES;
4404 * Destroy tunnel if empty (no more channels).
4406 * @param t Tunnel to destroy if empty.
4409 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4411 if (NULL != t->channel_head)
4414 tunnel_destroy_empty (t);
4419 * Destroy a channel and free all resources.
4421 * @param ch Channel to destroy.
4424 channel_destroy (struct MeshChannel *ch)
4426 struct MeshClient *c;
4434 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4445 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4452 channel_rel_free_all (ch->root_rel);
4453 channel_rel_free_all (ch->dest_rel);
4455 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4456 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4462 * Create a new channel.
4464 * @param t Tunnel this channel is in.
4465 * @param owner Client that owns the channel, NULL for foreign channels.
4466 * @param lid_root Local ID for root client.
4468 * @return A new initialized channel. NULL on error.
4470 static struct MeshChannel *
4471 channel_new (struct MeshTunnel2 *t,
4472 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4474 struct MeshChannel *ch;
4476 ch = GNUNET_new (struct MeshChannel);
4478 ch->lid_root = lid_root;
4481 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4483 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4487 while (NULL != channel_get (t, t->next_chid))
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n",
4490 t->next_chid, channel_get (t, t->next_chid));
4491 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4493 ch->gid = t->next_chid;
4494 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4497 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4501 channel_destroy (ch);
4502 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4512 * Set options in a channel, extracted from a bit flag field
4514 * @param ch Channel to set options to.
4515 * @param options Bit array in host byte order.
4518 channel_set_options (struct MeshChannel *ch, uint32_t options)
4520 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4521 GNUNET_YES : GNUNET_NO;
4522 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4523 GNUNET_YES : GNUNET_NO;
4528 * Iterator for deleting each channel whose client endpoint disconnected.
4530 * @param cls Closure (client that has disconnected).
4531 * @param key The local channel id (used to access the hashmap).
4532 * @param value The value stored at the key (channel to destroy).
4534 * @return GNUNET_OK, keep iterating.
4537 channel_destroy_iterator (void *cls,
4541 struct MeshChannel *ch = value;
4542 struct MeshClient *c = cls;
4543 struct MeshTunnel2 *t;
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4546 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4547 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4559 channel_send_destroy (ch);
4560 channel_destroy (ch);
4561 tunnel_destroy_if_empty (t);
4568 * Remove client's ports from the global hashmap on disconnect.
4570 * @param cls Closure (unused).
4572 * @param value Client structure.
4574 * @return GNUNET_OK, keep iterating.
4577 client_release_ports (void *cls,
4583 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4584 if (GNUNET_YES != res)
4587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4588 "Port %u by client %p was not registered.\n",
4596 * Timeout function due to lack of keepalive/traffic from the owner.
4597 * Destroys connection if called.
4599 * @param cls Closure (connection to destroy).
4600 * @param tc TaskContext.
4603 connection_fwd_timeout (void *cls,
4604 const struct GNUNET_SCHEDULER_TaskContext *tc)
4606 struct MeshConnection *c = cls;
4608 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4609 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4612 "Connection %s[%X] FWD timed out. Destroying.\n",
4613 peer2s (c->t->peer),
4616 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4619 connection_destroy (c);
4624 * Timeout function due to lack of keepalive/traffic from the destination.
4625 * Destroys connection if called.
4627 * @param cls Closure (connection to destroy).
4628 * @param tc TaskContext
4631 connection_bck_timeout (void *cls,
4632 const struct GNUNET_SCHEDULER_TaskContext *tc)
4634 struct MeshConnection *c = cls;
4636 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4637 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4641 "Connection %s[%X] FWD timed out. Destroying.\n",
4642 peer2s (c->t->peer),
4645 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4648 connection_destroy (c);
4653 * Resets the connection timeout task, some other message has done the
4655 * - For the first peer on the direction this means to send
4656 * a keepalive or a path confirmation message (either create or ACK).
4657 * - For all other peers, this means to destroy the connection,
4658 * due to lack of activity.
4659 * Starts the tiemout if no timeout was running (connection just created).
4661 * @param c Connection whose timeout to reset.
4662 * @param fwd Is this forward?
4664 * TODO use heap to improve efficiency of scheduler.
4667 connection_reset_timeout (struct MeshConnection *c, int fwd)
4669 GNUNET_SCHEDULER_TaskIdentifier *ti;
4670 GNUNET_SCHEDULER_Task f;
4672 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4674 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4675 GNUNET_SCHEDULER_cancel (*ti);
4677 if (connection_is_origin (c, fwd)) /* Endpoint */
4679 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4680 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4684 struct GNUNET_TIME_Relative delay;
4686 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4687 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4688 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4694 * Iterator to notify all connections of a broken link. Mark connections
4695 * to destroy after all traffic has been sent.
4697 * @param cls Closure (peer disconnected).
4698 * @param key Current key code (tid).
4699 * @param value Value in the hash map (connection).
4701 * @return GNUNET_YES if we should continue to iterate,
4705 connection_broken (void *cls,
4706 const struct GNUNET_HashCode *key,
4709 struct MeshPeer *peer = cls;
4710 struct MeshConnection *c = value;
4711 struct GNUNET_MESH_ConnectionBroken msg;
4714 fwd = peer == connection_get_prev_hop (c);
4716 connection_cancel_queues (c, !fwd);
4717 if (connection_is_terminal (c, fwd))
4719 /* Local shutdown, no one to notify about this. */
4720 connection_destroy (c);
4724 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4725 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4727 msg.peer1 = my_full_id;
4728 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4729 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4730 c->destroy = GNUNET_YES;
4735 /******************************************************************************/
4736 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4737 /******************************************************************************/
4740 * Free a transmission that was already queued with all resources
4741 * associated to the request.
4743 * @param queue Queue handler to cancel.
4744 * @param clear_cls Is it necessary to free associated cls?
4747 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4749 struct MeshPeer *peer;
4750 struct MeshFlowControl *fc;
4755 GNUNET_assert (NULL != queue->c);
4756 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4758 if (GNUNET_YES == clear_cls)
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4761 GNUNET_MESH_DEBUG_M2S (queue->type));
4762 switch (queue->type)
4764 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4765 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4766 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4767 GNUNET_break (GNUNET_YES == queue->c->destroy);
4769 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4770 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4771 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4772 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4773 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4774 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4775 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4777 GNUNET_free_non_null (queue->cls);
4782 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4783 GNUNET_MESH_DEBUG_M2S (queue->type));
4787 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4789 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4790 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
4796 if (NULL != queue->c)
4798 queue->c->pending_messages--;
4799 if (NULL != queue->c->t)
4801 queue->c->t->pending_messages--;
4805 GNUNET_free (queue);
4810 queue_send (void *cls, size_t size, void *buf)
4812 struct MeshPeer *peer = cls;
4813 struct MeshFlowControl *fc;
4814 struct MeshConnection *c;
4815 struct GNUNET_MessageHeader *msg;
4816 struct MeshPeerQueue *queue;
4817 struct MeshTunnel2 *t;
4818 struct MeshChannel *ch;
4819 const struct GNUNET_PeerIdentity *dst_id;
4825 peer->core_transmit = NULL;
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4828 if (NULL == buf || 0 == size)
4830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4835 queue = peer_get_first_message (peer);
4838 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4843 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4846 dst_id = GNUNET_PEER_resolve2 (peer->id);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4848 /* Check if buffer size is enough for the message */
4849 if (queue->size > size)
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4852 peer->core_transmit =
4853 GNUNET_CORE_notify_transmit_ready (core_handle,
4856 GNUNET_TIME_UNIT_FOREVER_REL,
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4865 t = (NULL != c) ? c->t : NULL;
4869 switch (queue->type)
4871 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4872 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4873 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4874 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4875 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4876 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4879 GNUNET_MESH_DEBUG_M2S (queue->type));
4880 data_size = send_core_data_raw (queue->cls, size, buf);
4881 msg = (struct GNUNET_MessageHeader *) buf;
4882 type = ntohs (msg->type);
4884 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4886 if (connection_is_origin (c, GNUNET_YES))
4887 data_size = send_core_connection_create (queue->c, size, buf);
4889 data_size = send_core_data_raw (queue->cls, size, buf);
4891 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4893 if (connection_is_origin (c, GNUNET_NO) ||
4894 connection_is_origin (c, GNUNET_YES))
4895 data_size = send_core_connection_ack (queue->c, size, buf);
4897 data_size = send_core_data_raw (queue->cls, size, buf);
4899 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4900 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4901 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4902 /* This should be encapsulted */
4908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4913 if (0 < drop_percent &&
4914 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4916 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4917 "Dropping message of type %s\n",
4918 GNUNET_MESH_DEBUG_M2S (queue->type));
4922 /* Free queue, but cls was freed by send_core_* */
4924 queue_destroy (queue, GNUNET_NO);
4926 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4929 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4930 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4931 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
4933 fc->last_pid_sent = pid;
4934 send_ack (c, ch, fwd);
4940 /* If more data in queue, send next */
4941 queue = peer_get_first_message (peer);
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4945 if (NULL == peer->core_transmit) {
4946 peer->core_transmit =
4947 GNUNET_CORE_notify_transmit_ready(core_handle,
4950 GNUNET_TIME_UNIT_FOREVER_REL,
4958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4959 "* tmt rdy called somewhere else\n");
4961 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4965 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4970 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4972 GNUNET_SCHEDULER_cancel (fc->poll_task);
4973 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4978 c->pending_messages--;
4979 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4982 connection_destroy (c);
4988 t->pending_messages--;
4989 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4991 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
5001 queue_add (void *cls, uint16_t type, size_t size,
5002 struct MeshConnection *c,
5003 struct MeshChannel *ch,
5006 struct MeshPeerQueue *queue;
5007 struct MeshFlowControl *fc;
5008 struct MeshPeer *peer;
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5013 "queue add %s %s (%u) on c %p, ch %p\n",
5014 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
5015 GNUNET_assert (NULL != c);
5017 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5018 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
5028 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
5029 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
5036 if (fc->queue_n >= fc->queue_max && 0 == priority)
5038 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5042 "queue full: %u/%u\n",
5043 fc->queue_n, fc->queue_max);
5044 return; /* Drop this message */
5047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
5048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
5049 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
5051 call_core = GNUNET_NO;
5052 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
5053 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5056 "no buffer space (%u > %u): starting poll\n",
5057 fc->last_pid_sent + 1, fc->last_ack_recv);
5058 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
5064 call_core = GNUNET_YES;
5065 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5073 if (100 <= priority)
5075 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
5079 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
5080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
5085 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5088 "calling core tmt rdy towards %s for %u bytes\n",
5089 peer2s (peer), size);
5090 peer->core_transmit =
5091 GNUNET_CORE_notify_transmit_ready (core_handle,
5094 GNUNET_TIME_UNIT_FOREVER_REL,
5095 GNUNET_PEER_resolve2 (peer->id),
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5103 "core tmt rdy towards %s already called\n",
5107 c->pending_messages++;
5109 c->t->pending_messages++;
5113 /******************************************************************************/
5114 /******************** MESH NETWORK HANDLERS **************************/
5115 /******************************************************************************/
5119 * Generic handler for mesh network payload traffic.
5121 * @param t Tunnel on which we got this message.
5122 * @param message Unencryted data message.
5123 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5126 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
5128 struct MeshChannelReliability *rel;
5129 struct MeshChannel *ch;
5130 struct MeshClient *c;
5136 size = ntohs (msg->header.size);
5138 sizeof (struct GNUNET_MESH_Data) +
5139 sizeof (struct GNUNET_MessageHeader))
5144 type = ntohs (msg->header.type);
5145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
5146 GNUNET_MESH_DEBUG_M2S (type));
5147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
5148 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5151 ch = channel_get (t, ntohl (msg->chid));
5154 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
5155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
5160 /* Initialize FWD/BCK data */
5161 c = fwd ? ch->dest : ch->root;
5162 rel = fwd ? ch->dest_rel : ch->root_rel;
5170 tunnel_change_state (t, MESH_TUNNEL_READY);
5172 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
5174 mid = ntohl (msg->mid);
5175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
5177 if (GNUNET_NO == ch->reliable ||
5178 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
5179 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
5182 if (GNUNET_YES == ch->reliable)
5184 /* Is this the exact next expected messasge? */
5185 if (mid == rel->mid_recv)
5187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
5189 channel_send_client_data (ch, msg, fwd);
5193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
5194 channel_rel_add_buffered_data (msg, rel);
5199 /* Tunnel is unreliable: send to clients directly */
5200 /* FIXME: accept Out Of Order traffic */
5201 rel->mid_recv = mid + 1;
5202 channel_send_client_data (ch, msg, fwd);
5207 GNUNET_break_op (0);
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5209 " MID %u not expected (%u - %u), dropping!\n",
5210 mid, rel->mid_recv, rel->mid_recv + 64);
5213 channel_send_data_ack (ch, fwd);
5217 * Handler for mesh network traffic end-to-end ACKs.
5219 * @param t Tunnel on which we got this message.
5220 * @param message Data message.
5221 * @param fwd Is this a fwd ACK? (dest->orig)
5224 handle_data_ack (struct MeshTunnel2 *t,
5225 const struct GNUNET_MESH_DataACK *msg, int fwd)
5227 struct MeshChannelReliability *rel;
5228 struct MeshReliableMessage *copy;
5229 struct MeshReliableMessage *next;
5230 struct MeshChannel *ch;
5235 type = ntohs (msg->header.type);
5236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5237 GNUNET_MESH_DEBUG_M2S (type));
5238 ch = channel_get (t, ntohl (msg->chid));
5241 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5244 ack = ntohl (msg->mid);
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5246 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5248 if (GNUNET_YES == fwd)
5262 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5264 if (GMC_is_pid_bigger (copy->mid, ack))
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5267 channel_rel_free_sent (rel, msg);
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5273 rel_message_free (copy);
5275 /* ACK client if needed */
5276 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5278 /* If some message was free'd, update the retransmission delay*/
5279 if (GNUNET_YES == work)
5281 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5283 GNUNET_SCHEDULER_cancel (rel->retry_task);
5284 if (NULL == rel->head_sent)
5286 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5290 struct GNUNET_TIME_Absolute new_target;
5291 struct GNUNET_TIME_Relative delay;
5293 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5294 MESH_RETRANSMIT_MARGIN);
5295 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5297 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5299 GNUNET_SCHEDULER_add_delayed (delay,
5300 &channel_retransmit_message,
5311 * Core handler for connection creation.
5313 * @param cls Closure (unused).
5314 * @param peer Sender (neighbor).
5315 * @param message Message.
5317 * @return GNUNET_OK to keep the connection open,
5318 * GNUNET_SYSERR to close it (signal serious error)
5321 handle_mesh_connection_create (void *cls,
5322 const struct GNUNET_PeerIdentity *peer,
5323 const struct GNUNET_MessageHeader *message)
5325 struct GNUNET_MESH_ConnectionCreate *msg;
5326 struct GNUNET_PeerIdentity *id;
5327 struct GNUNET_HashCode *cid;
5328 struct MeshPeerPath *path;
5329 struct MeshPeer *dest_peer;
5330 struct MeshPeer *orig_peer;
5331 struct MeshConnection *c;
5332 unsigned int own_pos;
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5340 size = ntohs (message->size);
5341 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5343 GNUNET_break_op (0);
5347 /* Calculate hops */
5348 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5349 if (size % sizeof (struct GNUNET_PeerIdentity))
5351 GNUNET_break_op (0);
5354 size /= sizeof (struct GNUNET_PeerIdentity);
5357 GNUNET_break_op (0);
5360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5362 /* Get parameters */
5363 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5365 id = (struct GNUNET_PeerIdentity *) &msg[1];
5366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5367 " connection %s (%s).\n",
5368 GNUNET_h2s (cid), GNUNET_i2s (id));
5370 /* Create connection */
5371 c = connection_get (cid);
5374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5375 c = connection_new (cid);
5378 connection_reset_timeout (c, GNUNET_YES);
5381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5382 path = path_new (size);
5384 for (i = 0; i < size; i++)
5386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5387 GNUNET_i2s (&id[i]));
5388 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5389 if (path->peers[i] == myid)
5392 if (own_pos == 0 && path->peers[own_pos] != myid)
5394 /* create path: self not found in path through self */
5395 GNUNET_break_op (0);
5396 path_destroy (path);
5397 connection_destroy (c);
5400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5401 path_add_to_peers (path, GNUNET_NO);
5402 c->path = path_duplicate (path);
5403 c->own_pos = own_pos;
5409 if (MESH_CONNECTION_NEW == c->state)
5410 connection_change_state (c, MESH_CONNECTION_SENT);
5412 /* Remember peers */
5413 dest_peer = peer_get (&id[size - 1]);
5414 orig_peer = peer_get (&id[0]);
5416 /* Is it a connection to us? */
5417 if (c->own_pos == size - 1)
5419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5420 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5422 if (NULL == orig_peer->tunnel)
5424 orig_peer->tunnel = tunnel_new ();
5425 orig_peer->tunnel->peer = orig_peer;
5427 tunnel_add_connection (orig_peer->tunnel, c);
5428 if (MESH_TUNNEL_NEW == c->t->state)
5429 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5431 send_connection_ack (c, GNUNET_NO);
5432 if (MESH_CONNECTION_SENT == c->state)
5433 connection_change_state (c, MESH_CONNECTION_ACK);
5435 /* Keep tunnel alive in direction dest->owner*/
5436 connection_reset_timeout (c, GNUNET_NO);
5440 /* It's for somebody else! Retransmit. */
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5442 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5443 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5444 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5451 * Core handler for path ACKs
5453 * @param cls closure
5454 * @param message message
5455 * @param peer peer identity this notification is about
5457 * @return GNUNET_OK to keep the connection open,
5458 * GNUNET_SYSERR to close it (signal serious error)
5461 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5462 const struct GNUNET_MessageHeader *message)
5464 struct GNUNET_MESH_ConnectionACK *msg;
5465 struct MeshConnection *c;
5466 struct MeshPeerPath *p;
5467 struct MeshPeer *pi;
5470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5472 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5474 GNUNET_h2s (&msg->cid));
5475 c = connection_get (&msg->cid);
5478 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5487 pi = peer_get (peer);
5488 if (connection_get_next_hop (c) == pi)
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
5492 if (MESH_CONNECTION_SENT == c->state)
5493 connection_change_state (c, MESH_CONNECTION_ACK);
5495 else if (connection_get_prev_hop (c) == pi)
5497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
5499 connection_change_state (c, MESH_CONNECTION_READY);
5503 GNUNET_break_op (0);
5506 connection_reset_timeout (c, fwd);
5508 /* Add path to peers? */
5512 path_add_to_peers (p, GNUNET_YES);
5519 /* Message for us as creator? */
5520 if (connection_is_origin (c, GNUNET_YES))
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
5523 connection_change_state (c, MESH_CONNECTION_READY);
5524 if (MESH_TUNNEL_READY != c->t->state)
5525 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5526 send_connection_ack (c, GNUNET_YES);
5527 tunnel_send_queued_data (c->t, GNUNET_YES);
5528 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5530 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5531 c->t->peer->dhtget = NULL;
5536 /* Message for us as destination? */
5537 if (connection_is_terminal (c, GNUNET_YES))
5539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5540 if (MESH_TUNNEL_READY != c->t->state)
5541 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5542 connection_change_state (c, MESH_CONNECTION_READY);
5543 tunnel_send_queued_data (c->t, GNUNET_NO);
5547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5548 send_prebuilt_message_connection (message, c, NULL, fwd);
5554 * Core handler for notifications of broken paths
5556 * @param cls Closure (unused).
5557 * @param peer Peer identity of sending neighbor.
5558 * @param message Message.
5560 * @return GNUNET_OK to keep the connection open,
5561 * GNUNET_SYSERR to close it (signal serious error)
5564 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5565 const struct GNUNET_MessageHeader *message)
5567 struct GNUNET_MESH_ConnectionBroken *msg;
5568 struct MeshConnection *c;
5570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5571 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5572 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5574 GNUNET_i2s (&msg->peer1));
5575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5576 GNUNET_i2s (&msg->peer2));
5577 c = connection_get (&msg->cid);
5580 GNUNET_break_op (0);
5583 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5584 GNUNET_PEER_search (&msg->peer2));
5591 * Core handler for tunnel destruction
5593 * @param cls Closure (unused).
5594 * @param peer Peer identity of sending neighbor.
5595 * @param message Message.
5597 * @return GNUNET_OK to keep the connection open,
5598 * GNUNET_SYSERR to close it (signal serious error)
5601 handle_mesh_connection_destroy (void *cls,
5602 const struct GNUNET_PeerIdentity *peer,
5603 const struct GNUNET_MessageHeader *message)
5605 struct GNUNET_MESH_ConnectionDestroy *msg;
5606 struct MeshConnection *c;
5610 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5612 "Got a CONNECTION DESTROY message from %s\n",
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5615 " for connection %s\n",
5616 GNUNET_h2s (&msg->cid));
5617 c = connection_get (&msg->cid);
5620 /* Probably already got the message from another path,
5621 * destroyed the tunnel and retransmitted to children.
5624 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5628 id = GNUNET_PEER_search (peer);
5629 if (id == connection_get_prev_hop (c)->id)
5631 else if (id == connection_get_next_hop (c)->id)
5635 GNUNET_break_op (0);
5638 send_prebuilt_message_connection (message, c, NULL, fwd);
5639 c->destroy = GNUNET_YES;
5646 * Handler for channel create messages.
5648 * @param t Tunnel this channel is to be created in.
5649 * @param msg Message.
5650 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5653 handle_channel_create (struct MeshTunnel2 *t,
5654 struct GNUNET_MESH_ChannelCreate *msg,
5657 MESH_ChannelNumber chid;
5658 struct MeshChannel *ch;
5659 struct MeshClient *c;
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5663 /* Check message size */
5664 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5666 GNUNET_break_op (0);
5670 /* Check if channel exists */
5671 chid = ntohl (msg->chid);
5672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid);
5673 ch = channel_get (t, chid);
5676 /* Probably a retransmission, safe to ignore */
5677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5678 if (NULL != ch->dest)
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5681 channel_send_ack (ch, !fwd);
5687 /* Create channel */
5688 ch = channel_new (t, NULL, 0);
5690 channel_set_options (ch, ntohl (msg->opt));
5693 /* Find a destination client */
5694 port = ntohl (msg->port);
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5696 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5699 /* TODO send reject */
5700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5705 channel_add_client (ch, c);
5706 if (GNUNET_YES == ch->reliable)
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5709 send_local_channel_create (ch);
5710 channel_send_ack (ch, fwd);
5711 send_local_ack (ch, !fwd);
5716 * Handler for channel ack messages.
5718 * @param t Tunnel this channel is to be created in.
5719 * @param msg Message.
5720 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5723 handle_channel_ack (struct MeshTunnel2 *t,
5724 struct GNUNET_MESH_ChannelManage *msg,
5727 MESH_ChannelNumber chid;
5728 struct MeshChannel *ch;
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5731 /* Check message size */
5732 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5734 GNUNET_break_op (0);
5738 /* Check if channel exists */
5739 chid = ntohl (msg->chid);
5740 ch = channel_get (t, chid);
5743 GNUNET_break_op (0);
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5748 channel_confirm (ch, !fwd);
5753 * Handler for channel destroy messages.
5755 * @param t Tunnel this channel is to be destroyed of.
5756 * @param msg Message.
5757 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5760 handle_channel_destroy (struct MeshTunnel2 *t,
5761 struct GNUNET_MESH_ChannelManage *msg,
5764 MESH_ChannelNumber chid;
5765 struct MeshChannel *ch;
5767 /* Check message size */
5768 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5770 GNUNET_break_op (0);
5774 /* Check if channel exists */
5775 chid = ntohl (msg->chid);
5776 ch = channel_get (t, chid);
5779 /* Probably a retransmission, safe to ignore */
5783 send_local_channel_destroy (ch, fwd);
5784 channel_destroy (ch);
5789 handle_decrypted (struct MeshTunnel2 *t,
5790 const struct GNUNET_MessageHeader *msgh,
5793 switch (ntohs (msgh->type))
5795 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5796 /* Don't send hop ACK, wait for client to ACK */
5797 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5800 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5801 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5804 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5805 handle_channel_create (t,
5806 (struct GNUNET_MESH_ChannelCreate *) msgh,
5810 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5811 handle_channel_ack (t,
5812 (struct GNUNET_MESH_ChannelManage *) msgh,
5816 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5817 handle_channel_destroy (t,
5818 (struct GNUNET_MESH_ChannelManage *) msgh,
5823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5824 "end-to-end message not known (%u)\n",
5825 ntohs (msgh->type));
5831 * Generic handler for mesh network encrypted traffic.
5833 * @param peer Peer identity this notification is about.
5834 * @param message Encrypted message.
5835 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5837 * @return GNUNET_OK to keep the connection open,
5838 * GNUNET_SYSERR to close it (signal serious error)
5841 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5842 const struct GNUNET_MESH_Encrypted *msg,
5845 struct MeshConnection *c;
5846 struct MeshTunnel2 *t;
5847 struct MeshPeer *neighbor;
5848 struct MeshFlowControl *fc;
5855 size = ntohs (msg->header.size);
5857 sizeof (struct GNUNET_MESH_Encrypted) +
5858 sizeof (struct GNUNET_MessageHeader))
5860 GNUNET_break_op (0);
5863 type = ntohs (msg->header.type);
5864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5866 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5868 /* Check connection */
5869 c = connection_get (&msg->cid);
5872 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5877 fc = fwd ? &c->bck_fc : &c->fwd_fc;
5879 /* Check if origin is as expected */
5880 neighbor = connection_get_hop (c, !fwd);
5881 if (peer_get (peer)->id != neighbor->id)
5883 GNUNET_break_op (0);
5888 pid = ntohl (msg->pid);
5889 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5891 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5893 "WARNING Received PID %u, (prev %u), ACK %u\n",
5894 pid, fc->last_pid_recv, fc->last_ack_sent);
5897 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5899 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5901 " Pid %u not expected (%u+), dropping!\n",
5902 pid, fc->last_pid_recv + 1);
5905 if (MESH_CONNECTION_SENT == c->state)
5906 connection_change_state (c, MESH_CONNECTION_READY);
5907 connection_reset_timeout (c, fwd);
5908 fc->last_pid_recv = pid;
5910 /* Is this message for us? */
5911 if (connection_is_terminal (c, fwd))
5913 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5915 struct GNUNET_MessageHeader *msgh;
5918 /* TODO signature verification */
5919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5920 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5922 fc->last_pid_recv = pid;
5923 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5927 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
5928 handle_decrypted (t, msgh, fwd);
5929 off += ntohs (msgh->size);
5931 send_ack (c, NULL, fwd);
5935 /* Message not for us: forward to next hop */
5936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5937 ttl = ntohl (msg->ttl);
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5941 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5942 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5943 send_ack (c, NULL, fwd);
5946 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5948 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5955 * Core handler for mesh network traffic going orig->dest.
5957 * @param cls Closure (unused).
5958 * @param message Message received.
5959 * @param peer Peer who sent the message.
5961 * @return GNUNET_OK to keep the connection open,
5962 * GNUNET_SYSERR to close it (signal serious error)
5965 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5966 const struct GNUNET_MessageHeader *message)
5968 return handle_mesh_encrypted (peer,
5969 (struct GNUNET_MESH_Encrypted *)message,
5974 * Core handler for mesh network traffic going dest->orig.
5976 * @param cls Closure (unused).
5977 * @param message Message received.
5978 * @param peer Peer who sent the message.
5980 * @return GNUNET_OK to keep the connection open,
5981 * GNUNET_SYSERR to close it (signal serious error)
5984 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5985 const struct GNUNET_MessageHeader *message)
5987 return handle_mesh_encrypted (peer,
5988 (struct GNUNET_MESH_Encrypted *)message,
5994 * Core handler for mesh network traffic point-to-point acks.
5996 * @param cls closure
5997 * @param message message
5998 * @param peer peer identity this notification is about
6000 * @return GNUNET_OK to keep the connection open,
6001 * GNUNET_SYSERR to close it (signal serious error)
6004 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6005 const struct GNUNET_MessageHeader *message)
6007 struct GNUNET_MESH_ACK *msg;
6008 struct MeshConnection *c;
6009 struct MeshFlowControl *fc;
6013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
6014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6016 msg = (struct GNUNET_MESH_ACK *) message;
6018 c = connection_get (&msg->cid);
6022 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
6027 /* Is this a forward or backward ACK? */
6028 id = GNUNET_PEER_search (peer);
6029 if (connection_get_next_hop (c)->id == id)
6031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6034 else if (connection_get_prev_hop (c)->id == id)
6036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6041 GNUNET_break_op (0);
6045 ack = ntohl (msg->ack);
6046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6048 /* Cancel polling if the ACK is bigger than before. */
6049 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
6050 GMC_is_pid_bigger (ack, fc->last_ack_recv))
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
6053 GNUNET_SCHEDULER_cancel (fc->poll_task);
6054 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
6055 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
6058 fc->last_ack_recv = ack;
6059 connection_unlock_queue (c, fc == &c->fwd_fc);
6066 * Core handler for mesh network traffic point-to-point ack polls.
6068 * @param cls closure
6069 * @param message message
6070 * @param peer peer identity this notification is about
6072 * @return GNUNET_OK to keep the connection open,
6073 * GNUNET_SYSERR to close it (signal serious error)
6076 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6077 const struct GNUNET_MessageHeader *message)
6079 struct GNUNET_MESH_Poll *msg;
6080 struct MeshConnection *c;
6081 struct MeshFlowControl *fc;
6086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
6087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
6090 msg = (struct GNUNET_MESH_Poll *) message;
6092 c = connection_get (&msg->cid);
6096 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
6098 GNUNET_break_op (0);
6102 /* Is this a forward or backward ACK?
6103 * Note: a poll should never be needed in a loopback case,
6104 * since there is no possiblility of packet loss there, so
6105 * this way of discerining FWD/BCK should not be a problem.
6107 id = GNUNET_PEER_search (peer);
6108 if (connection_get_next_hop (c)->id == id)
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6113 else if (connection_get_prev_hop (c)->id == id)
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6120 GNUNET_break_op (0);
6124 pid = ntohl (msg->pid);
6125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
6126 pid, fc->last_pid_recv);
6127 fc->last_pid_recv = pid;
6128 fwd = fc == &c->fwd_fc;
6129 send_ack (c, NULL, fwd);
6136 * Core handler for mesh keepalives.
6138 * @param cls closure
6139 * @param message message
6140 * @param peer peer identity this notification is about
6141 * @return GNUNET_OK to keep the connection open,
6142 * GNUNET_SYSERR to close it (signal serious error)
6144 * TODO: Check who we got this from, to validate route.
6147 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6148 const struct GNUNET_MessageHeader *message)
6150 struct GNUNET_MESH_ConnectionKeepAlive *msg;
6151 struct MeshConnection *c;
6152 struct MeshPeer *neighbor;
6155 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
6156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6159 c = connection_get (&msg->cid);
6162 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
6167 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
6168 GNUNET_YES : GNUNET_NO;
6170 /* Check if origin is as expected */
6171 neighbor = connection_get_hop (c, fwd);
6172 if (peer_get (peer)->id != neighbor->id)
6174 GNUNET_break_op (0);
6178 connection_change_state (c, MESH_CONNECTION_READY);
6179 connection_reset_timeout (c, fwd);
6181 if (connection_is_terminal (c, fwd))
6184 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6185 send_prebuilt_message_connection (message, c, NULL, fwd);
6193 * Functions to handle messages from core
6195 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6196 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
6198 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
6199 sizeof (struct GNUNET_MESH_ConnectionACK)},
6200 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
6201 sizeof (struct GNUNET_MESH_ConnectionBroken)},
6202 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
6203 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
6204 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
6205 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6206 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
6207 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6208 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6209 sizeof (struct GNUNET_MESH_ACK)},
6210 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6211 sizeof (struct GNUNET_MESH_Poll)},
6212 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
6213 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
6219 * Function to process paths received for a new peer addition. The recorded
6220 * paths form the initial tunnel, which can be optimized later.
6221 * Called on each result obtained for the DHT search.
6223 * @param cls closure
6224 * @param exp when will this value expire
6225 * @param key key of the result
6226 * @param get_path path of the get request
6227 * @param get_path_length lenght of get_path
6228 * @param put_path path of the put request
6229 * @param put_path_length length of the put_path
6230 * @param type type of the result
6231 * @param size number of bytes in data
6232 * @param data pointer to the result data
6235 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6236 const struct GNUNET_HashCode * key,
6237 const struct GNUNET_PeerIdentity *get_path,
6238 unsigned int get_path_length,
6239 const struct GNUNET_PeerIdentity *put_path,
6240 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6241 size_t size, const void *data)
6243 struct MeshPeer *peer = cls;
6244 struct MeshPeerPath *p;
6245 struct MeshConnection *c;
6246 struct GNUNET_PeerIdentity pi;
6249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6250 GNUNET_PEER_resolve (peer->id, &pi);
6251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6253 p = path_build_from_dht (get_path, get_path_length,
6254 put_path, put_path_length);
6255 path_add_to_peers (p, GNUNET_NO);
6258 /* Count connections */
6259 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6261 /* If we already have 3 (or more (?!)) connections, it's enough */
6265 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6268 peer_connect (peer);
6274 /******************************************************************************/
6275 /********************* MESH LOCAL HANDLES **************************/
6276 /******************************************************************************/
6280 * Handler for client connection.
6282 * @param cls Closure (unused).
6283 * @param client Client handler.
6286 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6288 struct MeshClient *c;
6292 c = GNUNET_malloc (sizeof (struct MeshClient));
6294 c->id = next_client_id++; /* overflow not important: just for debug */
6295 GNUNET_SERVER_client_keep (client);
6296 GNUNET_SERVER_client_set_user_context (client, c);
6297 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6302 * Handler for client disconnection
6304 * @param cls closure
6305 * @param client identification of the client; NULL
6306 * for the last call when the server is destroyed
6309 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6311 struct MeshClient *c;
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6320 c = client_get (client);
6323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6325 GNUNET_SERVER_client_drop (c->handle);
6326 c->shutting_down = GNUNET_YES;
6327 if (NULL != c->own_channels)
6329 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6330 &channel_destroy_iterator, c);
6331 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6334 if (NULL != c->incoming_channels)
6336 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6337 &channel_destroy_iterator, c);
6338 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6341 if (NULL != c->ports)
6343 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6344 &client_release_ports, c);
6345 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6347 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6348 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6362 * Handler for new clients
6364 * @param cls closure
6365 * @param client identification of the client
6366 * @param message the actual message, which includes messages the client wants
6369 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6370 const struct GNUNET_MessageHeader *message)
6372 struct GNUNET_MESH_ClientConnect *cc_msg;
6373 struct MeshClient *c;
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6380 /* Check data sanity */
6381 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6382 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6383 if (0 != (size % sizeof (uint32_t)))
6386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6389 size /= sizeof (uint32_t);
6391 /* Initialize new client structure */
6392 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6399 p = (uint32_t *) &cc_msg[1];
6400 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6401 for (i = 0; i < size; i++)
6404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6406 /* store in client's hashmap */
6407 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6408 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6409 /* store in global hashmap */
6410 /* FIXME only allow one client to have the port open,
6411 * have a backup hashmap with waiting clients */
6412 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6413 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6417 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6418 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6419 GNUNET_SERVER_notification_context_add (nc, client);
6420 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6422 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6428 * Handler for requests of new tunnels
6430 * @param cls Closure.
6431 * @param client Identification of the client.
6432 * @param message The actual message.
6435 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6436 const struct GNUNET_MessageHeader *message)
6438 struct GNUNET_MESH_ChannelMessage *msg;
6439 struct MeshPeer *peer;
6440 struct MeshTunnel2 *t;
6441 struct MeshChannel *ch;
6442 struct MeshClient *c;
6443 MESH_ChannelNumber chid;
6445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6447 /* Sanity check for client registration */
6448 if (NULL == (c = client_get (client)))
6451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6456 /* Message size sanity check */
6457 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6464 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6466 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6467 chid = ntohl (msg->channel_id);
6469 /* Sanity check for duplicate channel IDs */
6470 if (NULL != channel_get_by_local_id (c, chid))
6473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6477 peer = peer_get (&msg->peer);
6478 if (NULL == peer->tunnel)
6480 peer->tunnel = tunnel_new ();
6481 peer->tunnel->peer = peer;
6485 /* Create channel */
6486 ch = channel_new (t, c, chid);
6490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 ch->port = ntohl (msg->port);
6494 channel_set_options (ch, ntohl (msg->opt));
6496 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6497 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6498 ch->root_rel->ch = ch;
6499 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6502 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6503 peer_connect (peer);
6505 /* Send create channel */
6507 struct GNUNET_MESH_ChannelCreate msgcc;
6509 msgcc.header.size = htons (sizeof (msgcc));
6510 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6511 msgcc.chid = htonl (ch->gid);
6512 msgcc.port = msg->port;
6513 msgcc.opt = msg->opt;
6515 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6518 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6524 * Handler for requests of deleting tunnels
6526 * @param cls closure
6527 * @param client identification of the client
6528 * @param message the actual message
6531 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6532 const struct GNUNET_MessageHeader *message)
6534 struct GNUNET_MESH_ChannelMessage *msg;
6535 struct MeshClient *c;
6536 struct MeshChannel *ch;
6537 struct MeshTunnel2 *t;
6538 MESH_ChannelNumber chid;
6540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6541 "Got a DESTROY CHANNEL from client!\n");
6543 /* Sanity check for client registration */
6544 if (NULL == (c = client_get (client)))
6547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6552 /* Message sanity check */
6553 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6560 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6562 /* Retrieve tunnel */
6563 chid = ntohl (msg->channel_id);
6564 ch = channel_get_by_local_id (c, chid);
6567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6573 /* Cleanup after the tunnel */
6574 client_delete_channel (c, ch);
6575 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6579 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6586 " channel %X client %p (%p, %p)\n",
6587 chid, c, ch->root, ch->dest);
6592 channel_destroy (ch);
6593 tunnel_destroy_if_empty (t);
6595 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6601 * Handler for client traffic
6603 * @param cls closure
6604 * @param client identification of the client
6605 * @param message the actual message
6608 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6609 const struct GNUNET_MessageHeader *message)
6611 struct GNUNET_MESH_LocalData *msg;
6612 struct MeshClient *c;
6613 struct MeshChannel *ch;
6614 struct MeshChannelReliability *rel;
6615 MESH_ChannelNumber chid;
6619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6620 "Got data from a client!\n");
6622 /* Sanity check for client registration */
6623 if (NULL == (c = client_get (client)))
6626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6631 msg = (struct GNUNET_MESH_LocalData *) message;
6633 /* Sanity check for message size */
6634 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6635 if (size < sizeof (struct GNUNET_MessageHeader))
6638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6642 /* Channel exists? */
6643 chid = ntohl (msg->id);
6644 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6645 ch = channel_get_by_local_id (c, chid);
6649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6653 /* Is the client in the channel? */
6656 ch->root->handle == client)
6660 ch->dest->handle == client) ) )
6663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6667 rel = fwd ? ch->root_rel : ch->dest_rel;
6668 rel->client_ready = GNUNET_NO;
6670 /* Ok, everything is correct, send the message. */
6672 struct GNUNET_MESH_Data *payload;
6673 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6674 unsigned char cbuf[p2p_size];
6676 payload = (struct GNUNET_MESH_Data *) cbuf;
6677 payload->mid = htonl (rel->mid_send);
6679 memcpy (&payload[1], &msg[1], size);
6680 payload->header.size = htons (p2p_size);
6681 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6682 payload->chid = htonl (ch->gid);
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6684 send_prebuilt_message_channel (&payload->header, ch, fwd);
6686 if (GNUNET_YES == ch->reliable)
6687 channel_save_copy (ch, &payload->header, fwd);
6689 if (tunnel_get_buffer (ch->t, fwd) > 0)
6690 send_local_ack (ch, fwd);
6691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6699 * Handler for client's ACKs for payload traffic.
6701 * @param cls Closure (unused).
6702 * @param client Identification of the client.
6703 * @param message The actual message.
6706 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6707 const struct GNUNET_MessageHeader *message)
6709 struct GNUNET_MESH_LocalAck *msg;
6710 struct MeshChannelReliability *rel;
6711 struct MeshChannel *ch;
6712 struct MeshClient *c;
6713 MESH_ChannelNumber chid;
6716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6718 /* Sanity check for client registration */
6719 if (NULL == (c = client_get (client)))
6722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6727 msg = (struct GNUNET_MESH_LocalAck *) message;
6729 /* Channel exists? */
6730 chid = ntohl (msg->channel_id);
6731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6732 ch = channel_get_by_local_id (c, chid);
6733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6737 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6743 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6744 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6745 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6746 rel = fwd ? ch->dest_rel : ch->root_rel;
6748 rel->client_ready = GNUNET_YES;
6749 channel_send_client_buffered_data (ch, c, fwd);
6750 send_ack (NULL, ch, fwd);
6752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6759 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6761 * @param cls Closure (client handle).
6762 * @param key Key (hashed tunnel ID, unused).
6763 * @param value Tunnel info.
6765 * @return GNUNET_YES, to keep iterating.
6768 // monitor_all_tunnels_iterator (void *cls,
6769 // const struct GNUNET_HashCode * key,
6772 // struct GNUNET_SERVER_Client *client = cls;
6773 // struct MeshChannel *ch = value;
6774 // struct GNUNET_MESH_LocalMonitor *msg;
6776 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6777 // msg->channel_id = htonl (ch->gid);
6778 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6779 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6781 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6782 // "* sending info about tunnel %s\n",
6783 // GNUNET_i2s (&msg->owner));
6785 // GNUNET_SERVER_notification_context_unicast (nc, client,
6786 // &msg->header, GNUNET_NO);
6787 // return GNUNET_YES;
6792 * Handler for client's MONITOR request.
6794 * @param cls Closure (unused).
6795 * @param client Identification of the client.
6796 * @param message The actual message.
6799 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6800 const struct GNUNET_MessageHeader *message)
6802 struct MeshClient *c;
6804 /* Sanity check for client registration */
6805 if (NULL == (c = client_get (client)))
6808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6812 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6813 "Received get tunnels request from client %u\n",
6815 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6816 // monitor_all_tunnels_iterator,
6818 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6819 "Get tunnels request from client %u completed\n",
6821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6826 * Handler for client's MONITOR_TUNNEL request.
6828 * @param cls Closure (unused).
6829 * @param client Identification of the client.
6830 * @param message The actual message.
6833 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6834 const struct GNUNET_MessageHeader *message)
6836 const struct GNUNET_MESH_LocalMonitor *msg;
6837 struct GNUNET_MESH_LocalMonitor *resp;
6838 struct MeshClient *c;
6839 struct MeshChannel *ch;
6841 /* Sanity check for client registration */
6842 if (NULL == (c = client_get (client)))
6845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6849 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6850 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6851 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6854 ntohl (msg->channel_id));
6855 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6859 /* We don't know the tunnel */
6860 struct GNUNET_MESH_LocalMonitor warn;
6863 GNUNET_SERVER_notification_context_unicast (nc, client,
6866 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6870 /* Initialize context */
6871 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6873 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6874 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6875 &resp->header, GNUNET_NO);
6878 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6879 "Monitor tunnel request from client %u completed\n",
6881 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6886 * Functions to handle messages from clients
6888 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6889 {&handle_local_new_client, NULL,
6890 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6891 {&handle_local_channel_create, NULL,
6892 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6893 sizeof (struct GNUNET_MESH_ChannelMessage)},
6894 {&handle_local_channel_destroy, NULL,
6895 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6896 sizeof (struct GNUNET_MESH_ChannelMessage)},
6897 {&handle_local_data, NULL,
6898 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6899 {&handle_local_ack, NULL,
6900 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6901 sizeof (struct GNUNET_MESH_LocalAck)},
6902 {&handle_local_get_tunnels, NULL,
6903 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6904 sizeof (struct GNUNET_MessageHeader)},
6905 {&handle_local_show_tunnel, NULL,
6906 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6907 sizeof (struct GNUNET_MESH_LocalMonitor)},
6913 * Method called whenever a given peer connects.
6915 * @param cls closure
6916 * @param peer peer identity this notification is about
6919 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6921 struct MeshPeer *pi;
6922 struct MeshPeerPath *path;
6924 DEBUG_CONN ("Peer connected\n");
6925 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6926 pi = peer_get (peer);
6929 DEBUG_CONN (" (self)\n");
6930 path = path_new (1);
6934 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6935 path = path_new (2);
6936 path->peers[1] = pi->id;
6937 GNUNET_PEER_change_rc (pi->id, 1);
6938 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6940 path->peers[0] = myid;
6941 GNUNET_PEER_change_rc (myid, 1);
6942 peer_add_path (pi, path, GNUNET_YES);
6944 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6950 * Method called whenever a peer disconnects.
6952 * @param cls closure
6953 * @param peer peer identity this notification is about
6956 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6958 struct MeshPeer *pi;
6960 DEBUG_CONN ("Peer disconnected\n");
6961 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6968 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6971 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6972 pi->connections = NULL;
6975 DEBUG_CONN (" (self)\n");
6977 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6984 * Install server (service) handlers and start listening to clients.
6989 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6990 GNUNET_SERVER_connect_notify (server_handle,
6991 &handle_local_client_connect, NULL);
6992 GNUNET_SERVER_disconnect_notify (server_handle,
6993 &handle_local_client_disconnect, NULL);
6994 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6996 clients_head = NULL;
6997 clients_tail = NULL;
6999 GNUNET_SERVER_resume (server_handle);
7004 * To be called on core init/fail.
7006 * @param cls Closure (config)
7007 * @param identity the public identity of this peer
7010 core_init (void *cls,
7011 const struct GNUNET_PeerIdentity *identity)
7013 const struct GNUNET_CONFIGURATION_Handle *c = cls;
7016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7017 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
7019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7020 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7022 GNUNET_i2s (identity));
7023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7025 GNUNET_i2s (&my_full_id));
7026 GNUNET_CORE_disconnect (core_handle);
7027 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7028 NULL, /* Closure passed to MESH functions */
7029 &core_init, /* Call core_init once connected */
7030 &core_connect, /* Handle connects */
7031 &core_disconnect, /* remove peers on disconnects */
7032 NULL, /* Don't notify about all incoming messages */
7033 GNUNET_NO, /* For header only in notification */
7034 NULL, /* Don't notify about all outbound messages */
7035 GNUNET_NO, /* For header-only out notification */
7036 core_handlers); /* Register these handlers */
7045 /******************************************************************************/
7046 /************************ MAIN FUNCTIONS ****************************/
7047 /******************************************************************************/
7050 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7052 * @param cls closure
7053 * @param key current key code
7054 * @param value value in the hash map
7055 * @return GNUNET_YES if we should continue to iterate,
7059 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7061 struct MeshPeer *p = value;
7062 struct MeshTunnel2 *t = p->tunnel;
7071 * Task run during shutdown.
7077 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7081 if (core_handle != NULL)
7083 GNUNET_CORE_disconnect (core_handle);
7086 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
7087 if (dht_handle != NULL)
7089 GNUNET_DHT_disconnect (dht_handle);
7094 GNUNET_SERVER_notification_context_destroy (nc);
7097 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7099 GNUNET_SCHEDULER_cancel (announce_id_task);
7100 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7107 * Process mesh requests.
7109 * @param cls closure
7110 * @param server the initialized server
7111 * @param c configuration to use
7114 run (void *cls, struct GNUNET_SERVER_Handle *server,
7115 const struct GNUNET_CONFIGURATION_Handle *c)
7118 struct GNUNET_CRYPTO_EccPrivateKey *pk;
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7121 server_handle = server;
7122 GNUNET_SERVER_suspend (server_handle);
7125 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
7128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7130 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7131 "mesh", "peer/privatekey");
7132 GNUNET_SCHEDULER_shutdown ();
7137 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
7138 &refresh_connection_time))
7140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7142 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7143 "mesh", "refresh path time");
7144 GNUNET_SCHEDULER_shutdown ();
7149 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7154 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7155 "mesh", "id announce time");
7156 GNUNET_SCHEDULER_shutdown ();
7161 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7166 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7167 "mesh", "connect timeout");
7168 GNUNET_SCHEDULER_shutdown ();
7173 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7178 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7179 "mesh", "max msgs queue");
7180 GNUNET_SCHEDULER_shutdown ();
7185 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
7188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7190 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7191 "mesh", "max tunnels");
7192 GNUNET_SCHEDULER_shutdown ();
7197 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7200 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7202 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7203 "mesh", "default ttl", 64);
7208 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7212 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7213 "mesh", "max peers", 1000);
7218 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
7225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7226 "Mesh is running with drop mode enabled. "
7227 "This is NOT a good idea! "
7228 "Remove the DROP_PERCENT option from your configuration.\n");
7232 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7233 &dht_replication_level))
7235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7237 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7238 "mesh", "dht replication level", 3);
7239 dht_replication_level = 3;
7242 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7243 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7244 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7246 dht_handle = GNUNET_DHT_connect (c, 64);
7247 if (NULL == dht_handle)
7251 stats = GNUNET_STATISTICS_create ("mesh", c);
7253 /* Scheduled the task to clean up when shutdown is called */
7254 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7256 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7257 GNUNET_free (keyfile);
7258 GNUNET_assert (NULL != pk);
7259 my_private_key = pk;
7260 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7261 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7262 &my_full_id.hashPubKey);
7263 myid = GNUNET_PEER_intern (&my_full_id);
7264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7265 "Mesh for peer [%s] starting\n",
7266 GNUNET_i2s(&my_full_id));
7268 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7269 NULL, /* Closure passed to MESH functions */
7270 &core_init, /* Call core_init once connected */
7271 &core_connect, /* Handle connects */
7272 &core_disconnect, /* remove peers on disconnects */
7273 NULL, /* Don't notify about all incoming messages */
7274 GNUNET_NO, /* For header only in notification */
7275 NULL, /* Don't notify about all outbound messages */
7276 GNUNET_NO, /* For header-only out notification */
7277 core_handlers); /* Register these handlers */
7278 if (NULL == core_handle)
7281 GNUNET_SCHEDULER_shutdown ();
7284 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7290 * The main function for the mesh service.
7292 * @param argc number of arguments from the command line
7293 * @param argv command line arguments
7294 * @return 0 ok, 1 on error
7297 main (int argc, char *const *argv)
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7303 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7305 ret = (GNUNET_OK == r) ? 0 : 1;
7306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");