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->path->length - 1 == c->own_pos )
1629 if (fwd && 0 == c->own_pos)
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 return connection_is_origin (c, !fwd);
1652 * Is the root client for this channel on this peer?
1654 * @param ch Channel.
1655 * @param fwd Is this for fwd traffic?
1657 * @return GNUNET_YES in case it is.
1660 channel_is_origin (struct MeshChannel *ch, int fwd)
1662 struct MeshClient *c;
1664 c = fwd ? ch->root : ch->dest;
1670 * Is the destination client for this channel on this peer?
1672 * @param ch Channel.
1673 * @param fwd Is this for fwd traffic?
1675 * @return GNUNET_YES in case it is.
1678 channel_is_terminal (struct MeshChannel *ch, int fwd)
1680 struct MeshClient *c;
1682 c = fwd ? ch->dest : ch->root;
1688 * Get free buffer space towards the client on a specific channel.
1690 * @param ch Channel.
1691 * @param fwd Is query about FWD traffic?
1693 * @return Free buffer space [0 - 64]
1696 channel_get_buffer (struct MeshChannel *ch, int fwd)
1698 struct MeshChannelReliability *rel;
1700 rel = fwd ? ch->dest_rel : ch->root_rel;
1702 /* If rel is NULL it means that the end is not yet created,
1703 * most probably is a loopback channel at the point of sending
1704 * the ChannelCreate to itself.
1709 return (64 - rel->n_recv);
1714 * Get free buffer space in a connection.
1716 * @param c Connection.
1717 * @param fwd Is query about FWD traffic?
1719 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1722 connection_get_buffer (struct MeshConnection *c, int fwd)
1724 struct MeshFlowControl *fc;
1726 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1728 return (fc->queue_max - fc->queue_n);
1733 * Get the total buffer space for a tunnel.
1736 * @param fwd Is this for FWD traffic?
1738 * @return Buffer space offered by all connections in the tunnel.
1741 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1743 struct MeshConnection *c;
1744 struct MeshFlowControl *fc;
1745 unsigned int buffer;
1747 c = t->connection_head;
1750 /* If terminal, return biggest channel buffer */
1751 if (NULL == c || connection_is_terminal (c, fwd))
1753 struct MeshChannel *ch;
1754 unsigned int ch_buf;
1756 if (NULL == t->channel_head)
1759 for (ch = t->channel_head; NULL != ch; ch = ch->next)
1761 ch_buf = channel_get_buffer (ch, fwd);
1762 if (ch_buf > buffer)
1768 /* If not terminal, return sum of connection buffers */
1771 if (c->state != MESH_CONNECTION_READY)
1777 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1778 buffer += fc->queue_max - fc->queue_n;
1787 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1788 * Encrypt data with the tunnel key.
1790 * @param t Tunnel whose key to use.
1791 * @param dst Destination for the encrypted data.
1792 * @param src Source of the plaintext.
1793 * @param size Size of the plaintext.
1794 * @param iv Initialization Vector to use.
1795 * @param fwd Is this a fwd message?
1798 tunnel_encrypt (struct MeshTunnel2 *t,
1799 void *dst, const void *src,
1800 size_t size, uint64_t iv, int fwd)
1802 memcpy (dst, src, size);
1807 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1808 * Decrypt data with the tunnel key.
1810 * @param t Tunnel whose key to use.
1811 * @param dst Destination for the plaintext.
1812 * @param src Source of the encrypted data.
1813 * @param size Size of the encrypted data.
1814 * @param iv Initialization Vector to use.
1815 * @param fwd Is this a fwd message?
1818 tunnel_decrypt (struct MeshTunnel2 *t,
1819 void *dst, const void *src,
1820 size_t size, uint64_t iv, int fwd)
1822 memcpy (dst, src, size);
1827 * Sends an already built message on a connection, properly registering
1828 * all used resources.
1830 * @param message Message to send. Function makes a copy of it.
1831 * If message is not hop-by-hop, decrements TTL of copy.
1832 * @param c Connection on which this message is transmitted.
1833 * @param ch Channel on which this message is transmitted, or NULL.
1834 * @param fwd Is this a fwd message?
1837 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1838 struct MeshConnection *c,
1839 struct MeshChannel *ch,
1846 size = ntohs (message->size);
1847 data = GNUNET_malloc (size);
1848 memcpy (data, message, size);
1849 type = ntohs (message->type);
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1851 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1855 struct GNUNET_MESH_Encrypted *emsg;
1856 struct GNUNET_MESH_ACK *amsg;
1857 struct GNUNET_MESH_Poll *pmsg;
1858 struct GNUNET_MESH_ConnectionDestroy *dmsg;
1859 struct GNUNET_MESH_ConnectionBroken *bmsg;
1862 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1863 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1864 emsg = (struct GNUNET_MESH_Encrypted *) data;
1865 ttl = ntohl (emsg->ttl);
1868 GNUNET_break_op (0);
1872 emsg->ttl = htonl (ttl - 1);
1873 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1877 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1878 amsg = (struct GNUNET_MESH_ACK *) data;
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1883 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1884 pmsg = (struct GNUNET_MESH_Poll *) data;
1886 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1890 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1891 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
1896 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1897 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
1902 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1903 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1920 * Sends an already built message on a tunnel, choosing the best connection.
1922 * @param message Message to send. Function modifies it.
1923 * @param t Tunnel on which this message is transmitted.
1924 * @param ch Channel on which this message is transmitted.
1925 * @param fwd Is this a fwd message?
1928 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1929 struct MeshTunnel2 *t,
1930 struct MeshChannel *ch,
1933 struct MeshConnection *c;
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1938 c = tunnel_get_connection (t, fwd);
1941 GNUNET_break (GNUNET_YES == t->destroy);
1944 type = ntohs (msg->header.type);
1947 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1948 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1949 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1950 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1952 msg->ttl = htonl (default_ttl);
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1956 GNUNET_MESH_DEBUG_M2S (type));
1961 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1966 * Sends an already built message on a channel, properly registering
1967 * all used resources and encrypting the message with the tunnel's key.
1969 * @param message Message to send. Function makes a copy of it.
1970 * @param ch Channel on which this message is transmitted.
1971 * @param fwd Is this a fwd message?
1974 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1975 struct MeshChannel *ch,
1978 struct GNUNET_MESH_Encrypted *msg;
1979 size_t size = ntohs (message->size);
1980 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1985 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1987 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1989 if (channel_is_terminal (ch, fwd) || ch->t->peer->id == myid)
1991 handle_decrypted (ch->t, message, fwd);
1995 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1996 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1998 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1999 msg->header.type = htons (type);
2000 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
2001 msg->iv = GNUNET_htonll (iv);
2002 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
2003 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
2008 * Sends a CREATE CONNECTION message for a path to a peer.
2009 * Changes the connection and tunnel states if necessary.
2011 * @param connection Connection to create.
2014 send_connection_create (struct MeshConnection *connection)
2016 struct MeshTunnel2 *t;
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2021 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2022 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2023 (connection->path->length *
2024 sizeof (struct GNUNET_PeerIdentity)),
2029 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
2030 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2031 if (MESH_CONNECTION_NEW == connection->state)
2032 connection_change_state (connection, MESH_CONNECTION_SENT);
2037 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
2040 * @param connection Connection to confirm.
2041 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
2044 send_connection_ack (struct MeshConnection *connection, int fwd)
2046 struct MeshTunnel2 *t;
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
2051 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
2052 sizeof (struct GNUNET_MESH_ConnectionACK),
2056 if (MESH_TUNNEL_NEW == t->state)
2057 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2058 if (MESH_CONNECTION_READY != connection->state)
2059 connection_change_state (connection, MESH_CONNECTION_SENT);
2064 * Core callback to write a pre-constructed data packet to core buffer
2066 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2067 * @param size Number of bytes available in buf.
2068 * @param buf Where the to write the message.
2070 * @return number of bytes written to buf
2073 send_core_data_raw (void *cls, size_t size, void *buf)
2075 struct GNUNET_MessageHeader *msg = cls;
2078 GNUNET_assert (NULL != msg);
2079 total_size = ntohs (msg->size);
2081 if (total_size > size)
2086 memcpy (buf, msg, total_size);
2093 * Function to send a create connection message to a peer.
2095 * @param c Connection to create.
2096 * @param size number of bytes available in buf
2097 * @param buf where the callee should write the message
2098 * @return number of bytes written to buf
2101 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
2103 struct GNUNET_MESH_ConnectionCreate *msg;
2104 struct GNUNET_PeerIdentity *peer_ptr;
2105 struct MeshPeerPath *p = c->path;
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
2111 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2112 p->length * sizeof (struct GNUNET_PeerIdentity);
2114 if (size < size_needed || NULL == buf)
2119 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
2120 msg->header.size = htons (size_needed);
2121 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
2124 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2125 for (i = 0; i < p->length; i++)
2127 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
2137 * Creates a path ack message in buf and frees all unused resources.
2139 * @param c Connection to send an ACK on.
2140 * @param size number of bytes available in buf
2141 * @param buf where the callee should write the message
2143 * @return number of bytes written to buf
2146 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
2148 struct GNUNET_MESH_ConnectionACK *msg = buf;
2149 struct MeshTunnel2 *t = c->t;
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
2152 GNUNET_assert (NULL != t);
2153 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
2158 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
2159 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
2163 /* TODO add signature */
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2166 return sizeof (struct GNUNET_MESH_ConnectionACK);
2171 * Destroy the peer_info and free any allocated resources linked to it
2173 * @param peer The peer_info to destroy.
2175 * @return GNUNET_OK on success
2178 peer_destroy (struct MeshPeer *peer)
2180 struct GNUNET_PeerIdentity id;
2181 struct MeshPeerPath *p;
2182 struct MeshPeerPath *nextp;
2184 GNUNET_PEER_resolve (peer->id, &id);
2185 GNUNET_PEER_change_rc (peer->id, -1);
2188 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2191 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2192 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2194 if (NULL != peer->dhtget)
2196 GNUNET_DHT_get_stop (peer->dhtget);
2198 p = peer->path_head;
2202 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2206 tunnel_destroy_empty (peer->tunnel);
2213 * Returns if peer is used (has a tunnel, is neighbor).
2215 * @peer Peer to check.
2217 * @return GNUNET_YES if peer is in use.
2220 peer_is_used (struct MeshPeer *peer)
2222 struct MeshPeerPath *p;
2224 if (NULL != peer->tunnel)
2227 for (p = peer->path_head; NULL != p; p = p->next)
2236 * Iterator over all the peers to get the oldest timestamp.
2238 * @param cls Closure (unsued).
2239 * @param key ID of the peer.
2240 * @param value Peer_Info of the peer.
2243 peer_get_oldest (void *cls,
2244 const struct GNUNET_HashCode *key,
2247 struct MeshPeer *p = value;
2248 struct GNUNET_TIME_Absolute *abs = cls;
2250 /* Don't count active peers */
2251 if (GNUNET_YES == peer_is_used (p))
2254 if (abs->abs_value_us < p->last_contact.abs_value_us)
2255 abs->abs_value_us = p->last_contact.abs_value_us;
2262 * Iterator over all the peers to remove the oldest entry.
2264 * @param cls Closure (unsued).
2265 * @param key ID of the peer.
2266 * @param value Peer_Info of the peer.
2269 peer_timeout (void *cls,
2270 const struct GNUNET_HashCode *key,
2273 struct MeshPeer *p = value;
2274 struct GNUNET_TIME_Absolute *abs = cls;
2276 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2277 GNUNET_NO == peer_is_used (p))
2287 * Delete oldest unused peer.
2290 peer_delete_oldest (void)
2292 struct GNUNET_TIME_Absolute abs;
2294 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2296 GNUNET_CONTAINER_multihashmap_iterate (peers,
2299 GNUNET_CONTAINER_multihashmap_iterate (peers,
2306 * Retrieve the MeshPeer stucture associated with the peer, create one
2307 * and insert it in the appropriate structures if the peer is not known yet.
2309 * @param peer Full identity of the peer.
2311 * @return Existing or newly created peer info.
2313 static struct MeshPeer *
2314 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2316 struct MeshPeer *peer;
2318 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2321 peer = GNUNET_new (struct MeshPeer);
2322 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2324 peer_delete_oldest ();
2326 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2328 peer->id = GNUNET_PEER_intern (peer_id);
2330 peer->last_contact = GNUNET_TIME_absolute_get();
2337 * Retrieve the MeshPeer stucture associated with the peer, create one
2338 * and insert it in the appropriate structures if the peer is not known yet.
2340 * @param peer Short identity of the peer.
2342 * @return Existing or newly created peer info.
2344 static struct MeshPeer *
2345 peer_get_short (const GNUNET_PEER_Id peer)
2347 return peer_get (GNUNET_PEER_resolve2 (peer));
2352 * Get a cost of a path for a peer considering existing tunnel connections.
2354 * @param peer Peer towards which the path is considered.
2355 * @param path Candidate path.
2357 * @return Cost of the path (path length + number of overlapping nodes)
2360 peer_get_path_cost (const struct MeshPeer *peer,
2361 const struct MeshPeerPath *path)
2363 struct MeshConnection *c;
2364 unsigned int overlap;
2372 GNUNET_assert (NULL != peer->tunnel);
2374 for (i = 0; i < path->length; i++)
2376 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2378 for (j = 0; j < c->path->length; j++)
2380 if (path->peers[i] == c->path->peers[j])
2388 return (path->length + overlap) * (path->score * -1);
2393 * Choose the best path towards a peer considering the tunnel properties.
2395 * @param peer The destination peer.
2397 * @return Best current known path towards the peer, if any.
2399 static struct MeshPeerPath *
2400 peer_get_best_path (const struct MeshPeer *peer)
2402 struct MeshPeerPath *best_p;
2403 struct MeshPeerPath *p;
2404 struct MeshConnection *c;
2405 unsigned int best_cost;
2408 best_cost = UINT_MAX;
2410 for (p = peer->path_head; NULL != p; p = p->next)
2412 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2416 continue; /* If path is in use in a connection, skip it. */
2418 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2428 queue_is_sendable (struct MeshPeerQueue *q)
2430 struct MeshFlowControl *fc;
2432 /* Is PID-independent? */
2435 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2436 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2440 /* Is PID allowed? */
2441 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2442 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2450 * Get first sendable message.
2452 * @param peer The destination peer.
2454 * @return Best current known path towards the peer, if any.
2456 static struct MeshPeerQueue *
2457 peer_get_first_message (const struct MeshPeer *peer)
2459 struct MeshPeerQueue *q;
2461 for (q = peer->queue_head; NULL != q; q = q->next)
2463 if (queue_is_sendable (q))
2472 * Try to establish a new connection to this peer in the given tunnel.
2473 * If the peer doesn't have any path to it yet, try to get one.
2474 * If the peer already has some path, send a CREATE CONNECTION towards it.
2476 * @param peer PeerInfo of the peer.
2479 peer_connect (struct MeshPeer *peer)
2481 struct MeshTunnel2 *t;
2482 struct MeshPeerPath *p;
2483 struct MeshConnection *c;
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "peer_connect towards %s\n",
2491 rerun_dhtget = GNUNET_NO;
2493 if (NULL != peer->path_head)
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2496 p = peer_get_best_path (peer);
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2500 c = tunnel_use_path (t, p);
2503 /* This case can happen when the path includes a first hop that is
2504 * not yet known to be connected.
2506 * This happens quite often during testing when running mesh
2507 * under valgrind: core connect notifications come very late and the
2508 * DHT result has already come and created a valid path.
2509 * In this case, the peer->connections hashmap will be NULL and
2510 * tunnel_use_path will not be able to create a connection from that
2513 * Re-running the DHT GET should give core time to callback.
2516 rerun_dhtget = GNUNET_YES;
2520 send_connection_create (c);
2526 if (NULL != peer->dhtget && GNUNET_YES == rerun_dhtget)
2528 GNUNET_DHT_get_stop (peer->dhtget);
2529 peer->dhtget = NULL;
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2531 " Stopping DHT GET for peer %s\n", peer2s (peer));
2534 if (NULL == peer->dhtget)
2536 const struct GNUNET_PeerIdentity *id;
2538 id = GNUNET_PEER_resolve2 (peer->id);
2539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 " Starting DHT GET for peer %s\n", peer2s (peer));
2541 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2542 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2543 &id->hashPubKey, /* key to search */
2544 dht_replication_level, /* replication level */
2545 GNUNET_DHT_RO_RECORD_ROUTE |
2546 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2548 0, /* xquery bits */
2549 &dht_get_id_handler, peer);
2550 if (MESH_TUNNEL_NEW == t->state)
2551 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2557 * Get the first transmittable message for a connection.
2559 * @param c Connection.
2560 * @param fwd Is this FWD?
2562 * @return First transmittable message.
2564 static struct MeshPeerQueue *
2565 connection_get_first_message (struct MeshConnection *c, int fwd)
2567 struct MeshPeerQueue *q;
2570 p = connection_get_hop (c, fwd);
2572 for (q = p->queue_head; NULL != q; q = q->next)
2576 if (queue_is_sendable (q))
2585 * @brief Re-initiate traffic on this connection if necessary.
2587 * Check if there is traffic queued towards this peer
2588 * and the core transmit handle is NULL (traffic was stalled).
2589 * If so, call core tmt rdy.
2591 * @param c Connection on which initiate traffic.
2592 * @param fwd Is this about fwd traffic?
2595 connection_unlock_queue (struct MeshConnection *c, int fwd)
2597 struct MeshPeer *peer;
2598 struct MeshPeerQueue *q;
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2602 "connection_unlock_queue %s on %s\n",
2603 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2605 if (connection_is_terminal (c, fwd))
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
2611 peer = connection_get_hop (c, fwd);
2613 if (NULL != peer->core_transmit)
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2616 return; /* Already unlocked */
2619 q = connection_get_first_message (c, fwd);
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2623 return; /* Nothing to transmit */
2627 peer->core_transmit =
2628 GNUNET_CORE_notify_transmit_ready (core_handle,
2631 GNUNET_TIME_UNIT_FOREVER_REL,
2632 GNUNET_PEER_resolve2 (peer->id),
2640 * Cancel all transmissions that belong to a certain connection.
2642 * @param c Connection which to cancel.
2643 * @param fwd Cancel fwd traffic?
2646 connection_cancel_queues (struct MeshConnection *c, int fwd)
2648 struct MeshPeerQueue *q;
2649 struct MeshPeerQueue *next;
2650 struct MeshFlowControl *fc;
2651 struct MeshPeer *peer;
2658 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2659 peer = connection_get_hop (c, fwd);
2661 for (q = peer->queue_head; NULL != q; q = next)
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 "connection_cancel_queue %s\n",
2668 GNUNET_MESH_DEBUG_M2S (q->type));
2669 queue_destroy (q, GNUNET_YES);
2672 if (NULL == peer->queue_head)
2674 if (NULL != peer->core_transmit)
2676 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2677 peer->core_transmit = NULL;
2679 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2681 GNUNET_SCHEDULER_cancel (fc->poll_task);
2682 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2689 * Add the path to the peer and update the path used to reach it in case this
2692 * @param peer_info Destination peer to add the path to.
2693 * @param path New path to add. Last peer must be the peer in arg 1.
2694 * Path will be either used of freed if already known.
2695 * @param trusted Do we trust that this path is real?
2698 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2701 struct MeshPeerPath *aux;
2705 if ((NULL == peer_info) || (NULL == path))
2708 path_destroy (path);
2711 if (path->peers[path->length - 1] != peer_info->id)
2714 path_destroy (path);
2717 if (2 >= path->length && GNUNET_NO == trusted)
2719 /* Only allow CORE to tell us about direct paths */
2720 path_destroy (path);
2723 for (l = 1; l < path->length; l++)
2725 if (path->peers[l] == myid)
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2728 for (l2 = 0; l2 < path->length - l; l2++)
2730 path->peers[l2] = path->peers[l + l2];
2735 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2740 path->length, peer2s (peer_info));
2742 l = path_get_length (path);
2745 path_destroy (path);
2749 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2750 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2752 l2 = path_get_length (aux);
2755 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2756 peer_info->path_tail, aux, path);
2761 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2763 path_destroy (path);
2768 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2775 * Add the path to the origin peer and update the path used to reach it in case
2776 * this is the shortest.
2777 * The path is given in peer_info -> destination, therefore we turn the path
2778 * upside down first.
2780 * @param peer_info Peer to add the path to, being the origin of the path.
2781 * @param path New path to add after being inversed.
2782 * Path will be either used or freed.
2783 * @param trusted Do we trust that this path is real?
2786 peer_add_path_to_origin (struct MeshPeer *peer_info,
2787 struct MeshPeerPath *path, int trusted)
2792 peer_add_path (peer_info, path, trusted);
2798 * Function called if a connection has been stalled for a while,
2799 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2801 * @param cls Closure (poll ctx).
2802 * @param tc TaskContext.
2805 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2807 struct MeshFlowControl *fc = cls;
2808 struct GNUNET_MESH_Poll msg;
2809 struct MeshConnection *c;
2811 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2812 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
2820 GNUNET_h2s (&c->id));
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
2822 fc == &c->fwd_fc ? "FWD" : "BCK");
2824 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2825 msg.header.size = htons (sizeof (msg));
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2827 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2828 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2829 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2830 &connection_poll, fc);
2835 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2836 * to obtain a local peer -> destination path and interning the peer ids.
2838 * @return Newly allocated and created path
2840 static struct MeshPeerPath *
2841 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2842 unsigned int get_path_length,
2843 const struct GNUNET_PeerIdentity *put_path,
2844 unsigned int put_path_length)
2846 struct MeshPeerPath *p;
2852 GNUNET_PEER_change_rc (myid, 1);
2853 i = get_path_length;
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2855 for (i--; i >= 0; i--)
2857 id = GNUNET_PEER_intern (&get_path[i]);
2858 if (p->length > 0 && id == p->peers[p->length - 1])
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2861 GNUNET_PEER_change_rc (id, -1);
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2866 GNUNET_i2s (&get_path[i]));
2868 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2869 p->peers[p->length - 1] = id;
2872 i = put_path_length;
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2874 for (i--; i >= 0; i--)
2876 id = GNUNET_PEER_intern (&put_path[i]);
2879 /* PUT path went through us, so discard the path up until now and start
2880 * from here to get a much shorter (and loop-free) path.
2885 if (p->length > 0 && id == p->peers[p->length - 1])
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2888 GNUNET_PEER_change_rc (id, -1);
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2893 GNUNET_i2s (&put_path[i]));
2895 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2896 p->peers[p->length - 1] = id;
2900 if (get_path_length > 0)
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2902 GNUNET_i2s (&get_path[0]));
2903 if (put_path_length > 0)
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2905 GNUNET_i2s (&put_path[0]));
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2908 for (i = 0; i < p->length; i++)
2910 struct GNUNET_PeerIdentity peer_id;
2912 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2914 GNUNET_i2s (&peer_id));
2922 * Adds a path to the peer_infos of all the peers in the path
2924 * @param p Path to process.
2925 * @param confirmed Whether we know if the path works or not.
2928 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2932 /* TODO: invert and add */
2933 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2934 for (i++; i < p->length; i++)
2936 struct MeshPeer *aux;
2937 struct MeshPeerPath *copy;
2939 aux = peer_get_short (p->peers[i]);
2940 copy = path_duplicate (p);
2941 copy->length = i + 1;
2942 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2948 * Search for a channel among the channels for a client
2950 * @param c the client whose channels to search in
2951 * @param chid the local id of the channel
2953 * @return channel handler, NULL if doesn't exist
2955 static struct MeshChannel *
2956 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2959 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2961 GNUNET_break_op (0);
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2965 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2966 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2967 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2973 channel_debug (struct MeshChannel *ch)
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X\n",
2981 peer2s (ch->t->peer), ch->gid);
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2983 ch->root, ch->root_rel);
2984 if (NULL != ch->root)
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
2987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
2988 ch->root_rel->client_ready ? "YES" : "NO");
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2992 ch->dest, ch->dest_rel);
2993 if (NULL != ch->dest)
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
2997 ch->dest_rel->client_ready ? "YES" : "NO");
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
3003 fc_debug (struct MeshFlowControl *fc)
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
3006 fc->last_pid_recv, fc->last_ack_sent);
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
3008 fc->last_pid_sent, fc->last_ack_recv);
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
3010 fc->queue_n, fc->queue_max);
3014 connection_debug (struct MeshConnection *c)
3018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
3022 peer2s (c->t->peer), GNUNET_h2s (&c->id));
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
3024 c->state, c->pending_messages);
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
3026 fc_debug (&c->fwd_fc);
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
3028 fc_debug (&c->bck_fc);
3033 static struct MeshChannel *
3034 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
3036 struct MeshChannel *ch;
3041 for (ch = t->channel_head; NULL != ch; ch = ch->next)
3043 if (ch->gid == chid)
3052 * Change the tunnel state.
3054 * @param t Tunnel whose state to change.
3055 * @param state New state.
3058 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3063 "Tunnel %s state was %s\n",
3065 GNUNET_MESH_DEBUG_TS2S (t->state));
3066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3067 "Tunnel %s state is now %s\n",
3069 GNUNET_MESH_DEBUG_TS2S (state));
3075 * Send all cached messages that we can, tunnel is online.
3077 * @param t Tunnel that holds the messages.
3078 * @param fwd Is this fwd?
3081 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
3083 struct MeshTunnelQueue *tq;
3084 struct MeshTunnelQueue *next;
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3088 "tunnel_send_queued_data on tunnel %s\n",
3090 room = tunnel_get_buffer (t, fwd);
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
3092 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
3096 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
3097 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
3106 * Cache a message to be sent once tunnel is online.
3108 * @param t Tunnel to hold the message.
3109 * @param ch Channel the message is about.
3110 * @param msg Message itself (copy will be made).
3111 * @param fwd Is this fwd?
3114 tunnel_queue_data (struct MeshTunnel2 *t,
3115 struct MeshChannel *ch,
3116 struct GNUNET_MessageHeader *msg,
3119 struct MeshTunnelQueue *tq;
3120 uint16_t size = ntohs (msg->size);
3122 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
3125 memcpy (&tq[1], msg, size);
3126 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
3128 if (MESH_TUNNEL_READY == t->state)
3129 tunnel_send_queued_data (t, fwd);
3134 connection_change_state (struct MeshConnection* c,
3135 enum MeshConnectionState state)
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 "Connection %s state was %s\n",
3139 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "Connection %s state is now %s\n",
3142 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
3148 * Add a client to a channel, initializing all needed data structures.
3150 * @param ch Channel to which add the client.
3151 * @param c Client which to add to the channel.
3154 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3156 struct MeshTunnel2 *t = ch->t;
3158 if (NULL != ch->dest)
3164 /* Assign local id as destination */
3165 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3166 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3167 ch->lid_dest = t->next_local_chid++;
3168 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3170 /* Store in client's hashmap */
3172 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3174 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3180 GNUNET_break (NULL == ch->dest_rel);
3181 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3182 ch->dest_rel->ch = ch;
3183 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3189 static struct MeshConnection *
3190 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3192 struct MeshConnection *c;
3193 struct GNUNET_HashCode cid;
3194 struct MeshPeer *peer;
3195 unsigned int own_pos;
3197 if (NULL == t || NULL == p)
3203 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3205 c = connection_new (&cid);
3207 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
3208 for (own_pos = 0; own_pos < p->length; own_pos++)
3210 if (p->peers[own_pos] == myid)
3213 if (own_pos > p->length - 1)
3216 connection_destroy (c);
3219 c->own_pos = own_pos;
3224 c->fwd_maintenance_task =
3225 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3226 &connection_fwd_keepalive, c);
3229 peer = connection_get_next_hop (c);
3230 if (NULL == peer->connections)
3232 connection_destroy (c);
3235 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3237 peer = connection_get_prev_hop (c);
3238 if (NULL == peer->connections)
3240 connection_destroy (c);
3243 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3244 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3250 * Notifies a tunnel that a connection has broken that affects at least
3251 * some of its peers. Sends a notification towards the root of the tree.
3252 * In case the peer is the owner of the tree, notifies the client that owns
3253 * the tunnel and tries to reconnect.
3255 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3257 * @param t Tunnel affected.
3258 * @param p1 Peer that got disconnected from p2.
3259 * @param p2 Peer that got disconnected from p1.
3261 * @return Short ID of the peer disconnected (either p1 or p2).
3262 * 0 if the tunnel remained unaffected.
3264 static GNUNET_PEER_Id
3265 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3266 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3268 // if (myid != p1 && myid != p2) FIXME
3273 // if (tree_get_predecessor (t->tree) != 0)
3275 // /* We are the peer still connected, notify owner of the disconnection. */
3276 // struct GNUNET_MESH_PathBroken msg;
3277 // struct GNUNET_PeerIdentity neighbor;
3279 // msg.header.size = htons (sizeof (msg));
3280 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3281 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3282 // msg.tid = htonl (t->id.tid);
3283 // msg.peer1 = my_full_id;
3284 // GNUNET_PEER_resolve (pid, &msg.peer2);
3285 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3286 // send_prebuilt_message (&msg.header, &neighbor, t);
3293 * Send an end-to-end ACK message for the most recent in-sequence payload.
3295 * If channel is not reliable, do nothing.
3297 * @param ch Channel this is about.
3298 * @param fwd Is for FWD traffic? (ACK dest->owner)
3301 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3303 struct GNUNET_MESH_DataACK msg;
3304 struct MeshChannelReliability *rel;
3305 struct MeshReliableMessage *copy;
3310 if (GNUNET_NO == ch->reliable)
3314 rel = fwd ? ch->dest_rel : ch->root_rel;
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3316 "send_data_ack for %u\n",
3319 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3320 msg.header.type = htons (type);
3321 msg.header.size = htons (sizeof (msg));
3322 msg.chid = htonl (ch->gid);
3323 msg.mid = htonl (rel->mid_recv - 1);
3325 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3327 if (copy->type != type)
3329 delta = copy->mid - rel->mid_recv;
3332 mask = 0x1LL << delta;
3333 msg.futures |= mask;
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3336 copy->mid, delta, mask, msg.futures);
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3340 send_prebuilt_message_channel (&msg.header, ch, fwd);
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3346 * Send an ACK informing the predecessor about the available buffer space.
3348 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3349 * the ACK itself goes "back" (dest->root).
3351 * @param c Connection on which to send the ACK.
3352 * @param buffer How much space free to advertise?
3353 * @param fwd Is this FWD ACK? (Going dest->owner)
3356 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3358 struct MeshFlowControl *next_fc;
3359 struct MeshFlowControl *prev_fc;
3360 struct GNUNET_MESH_ACK msg;
3364 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3365 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3368 "connection send %s ack on %s\n",
3369 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3371 /* Check if we need to transmit the ACK */
3372 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3376 " last pid recv: %u, last ack sent: %u\n",
3377 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3381 /* Ok, ACK might be necessary, what PID to ACK? */
3382 delta = next_fc->queue_max - next_fc->queue_n;
3383 ack = prev_fc->last_pid_recv + delta;
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386 " last pid %u, last ack %u, qmax %u, q %u\n",
3387 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3388 next_fc->queue_max, next_fc->queue_n);
3389 if (ack == prev_fc->last_ack_sent)
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3395 prev_fc->last_ack_sent = ack;
3397 /* Build ACK message and send on connection */
3398 msg.header.size = htons (sizeof (msg));
3399 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3400 msg.ack = htonl (ack);
3403 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3408 * Modify the mesh message TID from global to local and send to client.
3410 * @param ch Channel on which to send the message.
3411 * @param msg Message to modify and send.
3412 * @param c Client to send to.
3413 * @param tid Tunnel ID to use (c can be both owner and client).
3416 channel_send_client_to_tid (struct MeshChannel *ch,
3417 const struct GNUNET_MESH_Data *msg,
3418 struct MeshClient *c, MESH_ChannelNumber id)
3420 struct GNUNET_MESH_LocalData *copy;
3421 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3422 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3424 if (size < sizeof (struct GNUNET_MessageHeader))
3426 GNUNET_break_op (0);
3434 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3435 memcpy (©[1], &msg[1], size);
3436 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3437 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3438 copy->id = htonl (id);
3439 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3440 ©->header, GNUNET_NO);
3445 * We have received a message out of order, or the client is not ready.
3446 * Buffer it until we receive an ACK from the client or the missing
3447 * message from the channel.
3449 * @param msg Message to buffer (MUST be of type MESH_DATA).
3450 * @param rel Reliability data to the corresponding direction.
3453 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3454 struct MeshChannelReliability *rel)
3456 struct MeshReliableMessage *copy;
3457 struct MeshReliableMessage *prev;
3461 size = ntohs (msg->header.size);
3462 mid = ntohl (msg->mid);
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3466 copy = GNUNET_malloc (sizeof (*copy) + size);
3469 memcpy (©[1], msg, size);
3473 // FIXME do something better than O(n), although n < 64...
3474 // FIXME start from the end (most messages are the latest ones)
3475 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3478 if (GMC_is_pid_bigger (prev->mid, mid))
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3481 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3487 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3493 channel_send_client_data (struct MeshChannel *ch,
3494 const struct GNUNET_MESH_Data *msg,
3499 if (ch->dest_rel->client_ready)
3500 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3502 channel_rel_add_buffered_data (msg, ch->dest_rel);
3506 if (ch->root_rel->client_ready)
3507 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3509 channel_rel_add_buffered_data (msg, ch->root_rel);
3515 * Send a buffered message to the client, for in order delivery or
3516 * as result of client ACK.
3518 * @param ch Channel on which to empty the message buffer.
3519 * @param c Client to send to.
3520 * @param rel Reliability structure to corresponding peer.
3521 * If rel == bck_rel, this is FWD data.
3524 channel_send_client_buffered_data (struct MeshChannel *ch,
3525 struct MeshClient *c,
3528 struct MeshReliableMessage *copy;
3529 struct MeshChannelReliability *rel;
3531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3532 rel = fwd ? ch->dest_rel : ch->root_rel;
3533 if (GNUNET_NO == rel->client_ready)
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3539 copy = rel->head_recv;
3540 /* We never buffer channel management messages */
3543 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3545 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3548 " have %u! now expecting %u\n",
3549 copy->mid, rel->mid_recv + 1);
3550 channel_send_client_data (ch, msg, fwd);
3553 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3559 " reliable && don't have %u, next is %u\n",
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3570 * Destroy a reliable message after it has been acknowledged, either by
3571 * direct mid ACK or bitfield. Updates the appropriate data structures and
3572 * timers and frees all memory.
3574 * @param copy Message that is no longer needed: remote peer got it.
3577 rel_message_free (struct MeshReliableMessage *copy)
3579 struct MeshChannelReliability *rel;
3580 struct GNUNET_TIME_Relative time;
3583 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3584 rel->expected_delay.rel_value_us *= 7;
3585 rel->expected_delay.rel_value_us += time.rel_value_us;
3586 rel->expected_delay.rel_value_us /= 8;
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3591 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3593 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3595 rel->retry_timer = rel->expected_delay;
3596 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3602 * Destroy all reliable messages queued for a channel,
3603 * during a channel destruction.
3604 * Frees the reliability structure itself.
3606 * @param rel Reliability data for a channel.
3609 channel_rel_free_all (struct MeshChannelReliability *rel)
3611 struct MeshReliableMessage *copy;
3612 struct MeshReliableMessage *next;
3617 for (copy = rel->head_recv; NULL != copy; copy = next)
3620 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3623 for (copy = rel->head_sent; NULL != copy; copy = next)
3626 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3629 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3630 GNUNET_SCHEDULER_cancel (rel->retry_task);
3636 * Mark future messages as ACK'd.
3638 * @param rel Reliability data.
3639 * @param msg DataACK message with a bitfield of future ACK'd messages.
3642 channel_rel_free_sent (struct MeshChannelReliability *rel,
3643 const struct GNUNET_MESH_DataACK *msg)
3645 struct MeshReliableMessage *copy;
3646 struct MeshReliableMessage *next;
3653 bitfield = msg->futures;
3654 mid = ntohl (msg->mid);
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3656 "free_sent_reliable %u %llX\n",
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3659 " rel %p, head %p\n",
3660 rel, rel->head_sent);
3661 for (i = 0, copy = rel->head_sent;
3662 i < 64 && NULL != copy && 0 != bitfield;
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3666 " trying bit %u (mid %u)\n",
3669 if (0 == (bitfield & mask))
3672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3673 /* Bit was set, clear the bit from the bitfield */
3676 /* The i-th bit was set. Do we have that copy? */
3677 /* Skip copies with mid < target */
3678 target = mid + i + 1;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3680 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3683 /* Did we run out of copies? (previously freed, it's ok) */
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3690 /* Did we overshoot the target? (previously freed, it's ok) */
3691 if (GMC_is_pid_bigger (copy->mid, target))
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3697 /* Now copy->mid == target, free it */
3699 rel_message_free (copy);
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3707 * We haven't received an ACK after a certain time: restransmit the message.
3709 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3710 * @param tc TaskContext.
3713 channel_retransmit_message (void *cls,
3714 const struct GNUNET_SCHEDULER_TaskContext *tc)
3716 struct MeshChannelReliability *rel = cls;
3717 struct MeshReliableMessage *copy;
3718 struct MeshPeerQueue *q;
3719 struct MeshChannel *ch;
3720 struct MeshConnection *c;
3721 struct GNUNET_MESH_Data *payload;
3722 struct MeshPeer *hop;
3725 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3726 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3730 copy = rel->head_sent;
3737 /* Search the message to be retransmitted in the outgoing queue.
3738 * Check only the queue for the connection that is going to be used,
3739 * if the message is stuck in some other connection's queue we shouldn't
3741 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3742 * the old connection could be temporary stalled or the queue happened to
3743 * be long at time of insertion.
3744 * - not sending the new one could cause terrible delays the old connection
3747 payload = (struct GNUNET_MESH_Data *) ©[1];
3748 fwd = (rel == ch->root_rel);
3749 c = tunnel_get_connection (ch->t, fwd);
3750 hop = connection_get_hop (c, fwd);
3751 for (q = hop->queue_head; NULL != q; q = q->next)
3753 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3755 struct GNUNET_MESH_Data *queued_data = q->cls;
3757 if (queued_data->mid == payload->mid)
3762 /* Message not found in the queue that we are going to use. */
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3767 send_prebuilt_message_channel (&payload->header, ch, fwd);
3768 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3775 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3776 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3777 &channel_retransmit_message,
3783 * Send ACK on one or more connections due to buffer space to the client.
3785 * Iterates all connections of the tunnel and sends ACKs appropriately.
3787 * @param ch Channel which has some free buffer space.
3788 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3791 channel_send_connections_ack (struct MeshChannel *ch,
3792 unsigned int buffer,
3795 struct MeshTunnel2 *t = ch->t;
3796 struct MeshConnection *c;
3797 struct MeshFlowControl *fc;
3800 uint32_t allow_per_connection;
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3804 "Channel send connection %s ack on %s:%X\n",
3805 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3807 /* Count connections, how many messages are already allowed */
3808 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3810 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3811 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3816 allowed += fc->last_ack_sent - fc->last_pid_recv;
3820 /* Make sure there is no overflow */
3821 if (allowed > buffer)
3827 /* Authorize connections to send more data */
3828 to_allow = buffer - allowed;
3830 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3832 allow_per_connection = to_allow/cs;
3833 to_allow -= allow_per_connection;
3835 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3836 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3840 connection_send_ack (c, allow_per_connection, fwd);
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3844 "Channel send connection %s ack on %s:%X\n",
3845 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3846 GNUNET_break (to_allow == 0);
3851 * Send an ACK on the appropriate connection/channel, depending on
3852 * the direction and the position of the peer.
3854 * @param c Which connection to send the hop-by-hop ACK.
3855 * @param ch Channel, if any.
3856 * @param fwd Is this a fwd ACK? (will go dest->root)
3859 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3861 unsigned int buffer;
3863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3864 "send ack %s on %p %p\n",
3865 fwd ? "FWD" : "BCK", c, ch);
3866 if (NULL == c || connection_is_terminal (c, fwd))
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from all connections\n");
3869 buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
3874 buffer = connection_get_buffer (c, fwd);
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3878 if ( (NULL != ch && channel_is_origin (ch, fwd)) ||
3879 (NULL != c && connection_is_origin (c, fwd)) )
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
3884 GNUNET_assert (NULL != ch);
3885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
3886 send_local_ack (ch, fwd);
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
3892 GNUNET_assert (NULL != ch);
3893 channel_send_connections_ack (ch, buffer, fwd);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3898 connection_send_ack (c, buffer, fwd);
3904 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3906 * @param ch Channel to mark as ready.
3907 * @param fwd Was the CREATE message sent fwd?
3910 channel_confirm (struct MeshChannel *ch, int fwd)
3912 struct MeshChannelReliability *rel;
3913 struct MeshReliableMessage *copy;
3914 struct MeshReliableMessage *next;
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3917 " channel confirm %s %s:%X\n",
3918 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3919 ch->state = MESH_CHANNEL_READY;
3921 rel = fwd ? ch->root_rel : ch->dest_rel;
3922 for (copy = rel->head_sent; NULL != copy; copy = next)
3924 struct GNUNET_MessageHeader *msg;
3927 msg = (struct GNUNET_MessageHeader *) ©[1];
3928 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3930 rel_message_free (copy);
3934 send_ack (NULL, ch, fwd);
3939 * Save a copy to retransmit in case it gets lost.
3941 * Initializes all needed callbacks and timers.
3943 * @param ch Channel this message goes on.
3944 * @param msg Message to copy.
3945 * @param fwd Is this fwd traffic?
3948 channel_save_copy (struct MeshChannel *ch,
3949 const struct GNUNET_MessageHeader *msg,
3952 struct MeshChannelReliability *rel;
3953 struct MeshReliableMessage *copy;
3958 rel = fwd ? ch->root_rel : ch->dest_rel;
3959 mid = rel->mid_send;
3960 type = ntohs (msg->type);
3961 size = ntohs (msg->size);
3963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3964 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3966 copy->timestamp = GNUNET_TIME_absolute_get ();
3969 memcpy (©[1], msg, size);
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3972 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3973 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3976 GNUNET_TIME_relative_multiply (rel->expected_delay,
3977 MESH_RETRANSMIT_MARGIN);
3979 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3980 &channel_retransmit_message,
3987 * Send keepalive packets for a connection.
3989 * @param c Connection to keep alive..
3990 * @param fwd Is this a FWD keepalive? (owner -> dest).
3993 connection_keepalive (struct MeshConnection *c, int fwd)
3995 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3996 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
4000 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
4001 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4004 "sending %s keepalive for connection %s[%d]\n",
4005 fwd ? "FWD" : "BCK",
4006 peer2s (c->t->peer),
4009 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
4010 msg->header.size = htons (size);
4011 msg->header.type = htons (type);
4014 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
4019 * Send CONNECTION_{CREATE/ACK} packets for a connection.
4021 * @param c Connection for which to send the message.
4022 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
4025 connection_recreate (struct MeshConnection *c, int fwd)
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
4029 send_connection_create (c);
4031 send_connection_ack (c, GNUNET_NO);
4036 * Generic connection timer management.
4037 * Depending on the role of the peer in the connection will send the
4038 * appropriate message (build or keepalive)
4040 * @param c Conncetion to maintain.
4041 * @param fwd Is FWD?
4044 connection_maintain (struct MeshConnection *c, int fwd)
4046 if (MESH_TUNNEL_SEARCHING == c->t->state)
4048 /* TODO DHT GET with RO_BART */
4053 case MESH_CONNECTION_NEW:
4055 case MESH_CONNECTION_SENT:
4056 connection_recreate (c, fwd);
4058 case MESH_CONNECTION_READY:
4059 connection_keepalive (c, fwd);
4068 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4070 struct MeshConnection *c = cls;
4072 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4073 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4076 connection_maintain (c, GNUNET_YES);
4077 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4078 &connection_fwd_keepalive,
4084 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4086 struct MeshConnection *c = cls;
4088 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4089 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4092 connection_maintain (c, GNUNET_NO);
4093 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4094 &connection_bck_keepalive,
4100 * Send a message to all peers in this connection that the connection
4101 * is no longer valid.
4103 * If some peer should not receive the message, it should be zero'ed out
4104 * before calling this function.
4106 * @param c The connection whose peers to notify.
4109 connection_send_destroy (struct MeshConnection *c)
4111 struct GNUNET_MESH_ConnectionDestroy msg;
4113 msg.header.size = htons (sizeof (msg));
4114 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4117 " sending connection destroy for connection %s[%X]\n",
4118 peer2s (c->t->peer),
4121 if (GNUNET_NO == connection_is_terminal (c, GNUNET_YES))
4122 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
4123 if (GNUNET_NO == connection_is_terminal (c, GNUNET_NO))
4124 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
4125 c->destroy = GNUNET_YES;
4130 * Confirm we got a channel create.
4132 * @param ch The channel to confirm.
4133 * @param fwd Should we send the ACK fwd?
4136 channel_send_ack (struct MeshChannel *ch, int fwd)
4138 struct GNUNET_MESH_ChannelManage msg;
4140 msg.header.size = htons (sizeof (msg));
4141 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4143 " sending channel %s ack for channel %s:%X\n",
4144 fwd ? "FWD" : "BCK", peer2s (ch->t->peer),
4147 msg.chid = htonl (ch->gid);
4148 send_prebuilt_message_channel (&msg.header, ch, !fwd);
4153 * Send a message to all clients (local and remote) of this channel
4154 * notifying that the channel is no longer valid.
4156 * If some peer or client should not receive the message,
4157 * should be zero'ed out before calling this function.
4159 * @param ch The channel whose clients to notify.
4162 channel_send_destroy (struct MeshChannel *ch)
4164 struct GNUNET_MESH_ChannelManage msg;
4166 msg.header.size = htons (sizeof (msg));
4167 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4169 " sending channel destroy for channel %s:%X\n",
4170 peer2s (ch->t->peer),
4173 if (channel_is_terminal (ch, GNUNET_NO))
4175 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
4177 msg.chid = htonl (ch->lid_root);
4178 send_local_channel_destroy (ch, GNUNET_NO);
4183 msg.chid = htonl (ch->gid);
4184 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4187 if (channel_is_terminal (ch, GNUNET_YES))
4189 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
4191 msg.chid = htonl (ch->lid_dest);
4192 send_local_channel_destroy (ch, GNUNET_YES);
4197 msg.chid = htonl (ch->gid);
4198 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4206 static struct MeshTunnel2 *
4209 struct MeshTunnel2 *t;
4211 t = GNUNET_new (struct MeshTunnel2);
4213 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4215 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4216 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4218 // GNUNET_break (0);
4219 // tunnel_destroy (t);
4223 // char salt[] = "salt";
4224 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4225 // salt, sizeof (salt),
4226 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4227 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4228 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4230 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4231 // salt, sizeof (salt),
4232 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4233 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4234 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4242 * Add a connection to a tunnel.
4245 * @param c Connection.
4248 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4250 struct MeshConnection *aux;
4252 for (aux = t->connection_head; aux != NULL; aux = aux->next)
4255 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4260 * Initialize a Flow Control structure to the initial state.
4262 * @param fc Flow Control structure to initialize.
4265 fc_init (struct MeshFlowControl *fc)
4268 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4269 fc->last_pid_recv = (uint32_t) -1;
4270 fc->last_ack_sent = (uint32_t) 0;
4271 fc->last_ack_recv = (uint32_t) 0;
4272 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4273 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4275 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4279 static struct MeshConnection *
4280 connection_new (const struct GNUNET_HashCode *cid)
4282 struct MeshConnection *c;
4284 c = GNUNET_new (struct MeshConnection);
4286 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4287 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4288 fc_init (&c->fwd_fc);
4289 fc_init (&c->bck_fc);
4298 * Find a connection.
4300 * @param cid Connection ID.
4302 static struct MeshConnection *
4303 connection_get (const struct GNUNET_HashCode *cid)
4305 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4310 connection_destroy (struct MeshConnection *c)
4312 struct MeshPeer *peer;
4317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4318 peer2s (c->t->peer),
4321 /* Cancel all traffic */
4322 connection_cancel_queues (c, GNUNET_YES);
4323 connection_cancel_queues (c, GNUNET_NO);
4325 /* Cancel maintainance task (keepalive/timeout) */
4326 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4327 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4328 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4329 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4331 /* Deregister from neighbors */
4332 peer = connection_get_next_hop (c);
4333 if (NULL != peer && NULL != peer->connections)
4334 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4335 peer = connection_get_prev_hop (c);
4336 if (NULL != peer && NULL != peer->connections)
4337 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4340 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4341 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4347 tunnel_destroy (struct MeshTunnel2 *t)
4349 struct MeshConnection *c;
4350 struct MeshConnection *next;
4355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4358 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4359 // GNUNET_break (0);
4361 for (c = t->connection_head; NULL != c; c = next)
4364 connection_destroy (c);
4367 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4368 t->peer->tunnel = NULL;
4375 * Tunnel is empty: destroy it.
4377 * Notifies all connections about the destruction.
4379 * @param t Tunnel to destroy.
4382 tunnel_destroy_empty (struct MeshTunnel2 *t)
4384 struct MeshConnection *c;
4386 for (c = t->connection_head; NULL != c; c = c->next)
4388 if (GNUNET_NO == c->destroy)
4389 connection_send_destroy (c);
4392 if (0 == t->pending_messages)
4395 t->destroy = GNUNET_YES;
4400 * Destroy tunnel if empty (no more channels).
4402 * @param t Tunnel to destroy if empty.
4405 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4407 if (NULL != t->channel_head)
4410 tunnel_destroy_empty (t);
4415 * Destroy a channel and free all resources.
4417 * @param ch Channel to destroy.
4420 channel_destroy (struct MeshChannel *ch)
4422 struct MeshClient *c;
4430 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4441 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4448 channel_rel_free_all (ch->root_rel);
4449 channel_rel_free_all (ch->dest_rel);
4451 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4452 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4458 * Create a new channel.
4460 * @param t Tunnel this channel is in.
4461 * @param owner Client that owns the channel, NULL for foreign channels.
4462 * @param lid_root Local ID for root client.
4464 * @return A new initialized channel. NULL on error.
4466 static struct MeshChannel *
4467 channel_new (struct MeshTunnel2 *t,
4468 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4470 struct MeshChannel *ch;
4472 ch = GNUNET_new (struct MeshChannel);
4474 ch->lid_root = lid_root;
4477 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4481 while (NULL != channel_get (t, t->next_chid))
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists (%p)...\n",
4484 t->next_chid, channel_get (t, t->next_chid));
4485 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4487 ch->gid = t->next_chid;
4488 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4491 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4492 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4495 channel_destroy (ch);
4496 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4500 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4507 * Set options in a channel, extracted from a bit flag field
4509 * @param ch Channel to set options to.
4510 * @param options Bit array in host byte order.
4513 channel_set_options (struct MeshChannel *ch, uint32_t options)
4515 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4516 GNUNET_YES : GNUNET_NO;
4517 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4518 GNUNET_YES : GNUNET_NO;
4523 * Iterator for deleting each channel whose client endpoint disconnected.
4525 * @param cls Closure (client that has disconnected).
4526 * @param key The local channel id (used to access the hashmap).
4527 * @param value The value stored at the key (channel to destroy).
4529 * @return GNUNET_OK, keep iterating.
4532 channel_destroy_iterator (void *cls,
4536 struct MeshChannel *ch = value;
4537 struct MeshClient *c = cls;
4538 struct MeshTunnel2 *t;
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4541 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4542 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4554 channel_send_destroy (ch);
4555 channel_destroy (ch);
4556 tunnel_destroy_if_empty (t);
4563 * Remove client's ports from the global hashmap on disconnect.
4565 * @param cls Closure (unused).
4567 * @param value Client structure.
4569 * @return GNUNET_OK, keep iterating.
4572 client_release_ports (void *cls,
4578 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4579 if (GNUNET_YES != res)
4582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4583 "Port %u by client %p was not registered.\n",
4591 * Timeout function due to lack of keepalive/traffic from the owner.
4592 * Destroys connection if called.
4594 * @param cls Closure (connection to destroy).
4595 * @param tc TaskContext.
4598 connection_fwd_timeout (void *cls,
4599 const struct GNUNET_SCHEDULER_TaskContext *tc)
4601 struct MeshConnection *c = cls;
4603 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4604 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4607 "Connection %s[%X] FWD timed out. Destroying.\n",
4608 peer2s (c->t->peer),
4611 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4614 connection_destroy (c);
4619 * Timeout function due to lack of keepalive/traffic from the destination.
4620 * Destroys connection if called.
4622 * @param cls Closure (connection to destroy).
4623 * @param tc TaskContext
4626 connection_bck_timeout (void *cls,
4627 const struct GNUNET_SCHEDULER_TaskContext *tc)
4629 struct MeshConnection *c = cls;
4631 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4632 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4636 "Connection %s[%X] FWD timed out. Destroying.\n",
4637 peer2s (c->t->peer),
4640 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4643 connection_destroy (c);
4648 * Resets the connection timeout task, some other message has done the
4650 * - For the first peer on the direction this means to send
4651 * a keepalive or a path confirmation message (either create or ACK).
4652 * - For all other peers, this means to destroy the connection,
4653 * due to lack of activity.
4654 * Starts the tiemout if no timeout was running (connection just created).
4656 * @param c Connection whose timeout to reset.
4657 * @param fwd Is this forward?
4659 * TODO use heap to improve efficiency of scheduler.
4662 connection_reset_timeout (struct MeshConnection *c, int fwd)
4664 GNUNET_SCHEDULER_TaskIdentifier *ti;
4665 GNUNET_SCHEDULER_Task f;
4667 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4669 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4670 GNUNET_SCHEDULER_cancel (*ti);
4672 if (connection_is_origin (c, fwd)) /* Endpoint */
4674 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4675 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4679 struct GNUNET_TIME_Relative delay;
4681 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4682 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4683 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4689 * Iterator to notify all connections of a broken link. Mark connections
4690 * to destroy after all traffic has been sent.
4692 * @param cls Closure (peer disconnected).
4693 * @param key Current key code (tid).
4694 * @param value Value in the hash map (connection).
4696 * @return GNUNET_YES if we should continue to iterate,
4700 connection_broken (void *cls,
4701 const struct GNUNET_HashCode *key,
4704 struct MeshPeer *peer = cls;
4705 struct MeshConnection *c = value;
4706 struct GNUNET_MESH_ConnectionBroken msg;
4709 fwd = peer == connection_get_prev_hop (c);
4711 connection_cancel_queues (c, !fwd);
4712 if (connection_is_terminal (c, fwd))
4714 /* Local shutdown, no one to notify about this. */
4715 connection_destroy (c);
4719 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4720 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4722 msg.peer1 = my_full_id;
4723 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4724 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4725 c->destroy = GNUNET_YES;
4730 /******************************************************************************/
4731 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4732 /******************************************************************************/
4735 * Free a transmission that was already queued with all resources
4736 * associated to the request.
4738 * @param queue Queue handler to cancel.
4739 * @param clear_cls Is it necessary to free associated cls?
4742 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4744 struct MeshPeer *peer;
4745 struct MeshFlowControl *fc;
4750 GNUNET_assert (NULL != queue->c);
4751 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4753 if (GNUNET_YES == clear_cls)
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4756 GNUNET_MESH_DEBUG_M2S (queue->type));
4757 switch (queue->type)
4759 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4760 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4761 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4762 GNUNET_break (GNUNET_YES == queue->c->destroy);
4764 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4765 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4766 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4767 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4768 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4769 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4770 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4772 GNUNET_free_non_null (queue->cls);
4777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4778 GNUNET_MESH_DEBUG_M2S (queue->type));
4782 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4784 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4785 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
4791 if (NULL != queue->c)
4793 queue->c->pending_messages--;
4794 if (NULL != queue->c->t)
4796 queue->c->t->pending_messages--;
4800 GNUNET_free (queue);
4805 queue_send (void *cls, size_t size, void *buf)
4807 struct MeshPeer *peer = cls;
4808 struct MeshFlowControl *fc;
4809 struct MeshConnection *c;
4810 struct GNUNET_MessageHeader *msg;
4811 struct MeshPeerQueue *queue;
4812 struct MeshTunnel2 *t;
4813 struct MeshChannel *ch;
4814 const struct GNUNET_PeerIdentity *dst_id;
4820 peer->core_transmit = NULL;
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4823 if (NULL == buf || 0 == size)
4825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4830 queue = peer_get_first_message (peer);
4833 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4838 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4840 dst_id = GNUNET_PEER_resolve2 (peer->id);
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4842 /* Check if buffer size is enough for the message */
4843 if (queue->size > size)
4845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4846 peer->core_transmit =
4847 GNUNET_CORE_notify_transmit_ready (core_handle,
4850 GNUNET_TIME_UNIT_FOREVER_REL,
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4859 t = (NULL != c) ? c->t : NULL;
4863 switch (queue->type)
4865 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4866 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4867 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4868 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4869 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4870 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4871 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4874 GNUNET_MESH_DEBUG_M2S (queue->type));
4875 data_size = send_core_data_raw (queue->cls, size, buf);
4876 msg = (struct GNUNET_MessageHeader *) buf;
4877 type = ntohs (msg->type);
4879 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4881 if (connection_is_origin (c, GNUNET_YES))
4882 data_size = send_core_connection_create (queue->c, size, buf);
4884 data_size = send_core_data_raw (queue->cls, size, buf);
4886 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4888 if (connection_is_origin (c, GNUNET_NO) ||
4889 connection_is_origin (c, GNUNET_YES))
4890 data_size = send_core_connection_ack (queue->c, size, buf);
4892 data_size = send_core_data_raw (queue->cls, size, buf);
4894 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4895 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4896 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4897 /* This should be encapsulted */
4903 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4908 if (0 < drop_percent &&
4909 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4911 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4912 "Dropping message of type %s\n",
4913 GNUNET_MESH_DEBUG_M2S (queue->type));
4917 /* Free queue, but cls was freed by send_core_* */
4919 queue_destroy (queue, GNUNET_NO);
4921 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4924 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4925 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4926 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
4928 fc->last_pid_sent = pid;
4929 send_ack (c, ch, fwd);
4935 /* If more data in queue, send next */
4936 queue = peer_get_first_message (peer);
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4940 if (NULL == peer->core_transmit) {
4941 peer->core_transmit =
4942 GNUNET_CORE_notify_transmit_ready(core_handle,
4945 GNUNET_TIME_UNIT_FOREVER_REL,
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 "* tmt rdy called somewhere else\n");
4956 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4960 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4965 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4967 GNUNET_SCHEDULER_cancel (fc->poll_task);
4968 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4973 c->pending_messages--;
4974 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4977 connection_destroy (c);
4983 t->pending_messages--;
4984 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4986 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4996 queue_add (void *cls, uint16_t type, size_t size,
4997 struct MeshConnection *c,
4998 struct MeshChannel *ch,
5001 struct MeshPeerQueue *queue;
5002 struct MeshFlowControl *fc;
5003 struct MeshPeer *peer;
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5008 "queue add %s %s (%u) on c %p, ch %p\n",
5009 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
5010 GNUNET_assert (NULL != c);
5012 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5013 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
5021 if (NULL == peer->connections)
5023 /* We are not connected to this peer, ignore request. */
5024 GNUNET_break_op (0);
5030 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
5031 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
5037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
5038 if (fc->queue_n >= fc->queue_max && 0 == priority)
5040 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5044 "queue full: %u/%u\n",
5045 fc->queue_n, fc->queue_max);
5046 return; /* Drop this message */
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
5050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
5051 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
5053 call_core = GNUNET_NO;
5054 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
5055 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
5057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5058 "no buffer space (%u > %u): starting poll\n",
5059 fc->last_pid_sent + 1, fc->last_ack_recv);
5060 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
5066 call_core = GNUNET_YES;
5067 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5075 if (100 <= priority)
5077 struct MeshPeerQueue *copy;
5078 struct MeshPeerQueue *next;
5080 for (copy = peer->queue_head; NULL != copy; copy = next)
5083 if (copy->type == type && copy->c == c && copy->fwd == fwd)
5085 /* Example: also a FWD ACK for connection XYZ */
5086 queue_destroy (copy, GNUNET_YES);
5089 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
5093 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
5099 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5102 "calling core tmt rdy towards %s for %u bytes\n",
5103 peer2s (peer), size);
5104 peer->core_transmit =
5105 GNUNET_CORE_notify_transmit_ready (core_handle,
5108 GNUNET_TIME_UNIT_FOREVER_REL,
5109 GNUNET_PEER_resolve2 (peer->id),
5116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5117 "core tmt rdy towards %s already called\n",
5121 c->pending_messages++;
5123 c->t->pending_messages++;
5127 /******************************************************************************/
5128 /******************** MESH NETWORK HANDLERS **************************/
5129 /******************************************************************************/
5133 * Generic handler for mesh network payload traffic.
5135 * @param t Tunnel on which we got this message.
5136 * @param message Unencryted data message.
5137 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5140 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
5142 struct MeshChannelReliability *rel;
5143 struct MeshChannel *ch;
5144 struct MeshClient *c;
5150 size = ntohs (msg->header.size);
5152 sizeof (struct GNUNET_MESH_Data) +
5153 sizeof (struct GNUNET_MessageHeader))
5158 type = ntohs (msg->header.type);
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
5160 GNUNET_MESH_DEBUG_M2S (type));
5161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
5162 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5165 ch = channel_get (t, ntohl (msg->chid));
5168 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
5174 /* Initialize FWD/BCK data */
5175 c = fwd ? ch->dest : ch->root;
5176 rel = fwd ? ch->dest_rel : ch->root_rel;
5184 tunnel_change_state (t, MESH_TUNNEL_READY);
5186 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
5188 mid = ntohl (msg->mid);
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
5191 if (GNUNET_NO == ch->reliable ||
5192 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
5193 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
5196 if (GNUNET_YES == ch->reliable)
5198 /* Is this the exact next expected messasge? */
5199 if (mid == rel->mid_recv)
5201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
5203 channel_send_client_data (ch, msg, fwd);
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
5208 channel_rel_add_buffered_data (msg, rel);
5213 /* Tunnel is unreliable: send to clients directly */
5214 /* FIXME: accept Out Of Order traffic */
5215 rel->mid_recv = mid + 1;
5216 channel_send_client_data (ch, msg, fwd);
5221 GNUNET_break_op (0);
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5223 " MID %u not expected (%u - %u), dropping!\n",
5224 mid, rel->mid_recv, rel->mid_recv + 64);
5227 channel_send_data_ack (ch, fwd);
5231 * Handler for mesh network traffic end-to-end ACKs.
5233 * @param t Tunnel on which we got this message.
5234 * @param message Data message.
5235 * @param fwd Is this a fwd ACK? (dest->orig)
5238 handle_data_ack (struct MeshTunnel2 *t,
5239 const struct GNUNET_MESH_DataACK *msg, int fwd)
5241 struct MeshChannelReliability *rel;
5242 struct MeshReliableMessage *copy;
5243 struct MeshReliableMessage *next;
5244 struct MeshChannel *ch;
5249 type = ntohs (msg->header.type);
5250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5251 GNUNET_MESH_DEBUG_M2S (type));
5252 ch = channel_get (t, ntohl (msg->chid));
5255 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5258 ack = ntohl (msg->mid);
5259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5260 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5262 if (GNUNET_YES == fwd)
5276 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5278 if (GMC_is_pid_bigger (copy->mid, ack))
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5281 channel_rel_free_sent (rel, msg);
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5287 rel_message_free (copy);
5289 /* ACK client if needed */
5290 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5292 /* If some message was free'd, update the retransmission delay*/
5293 if (GNUNET_YES == work)
5295 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5297 GNUNET_SCHEDULER_cancel (rel->retry_task);
5298 if (NULL == rel->head_sent)
5300 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5304 struct GNUNET_TIME_Absolute new_target;
5305 struct GNUNET_TIME_Relative delay;
5307 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5308 MESH_RETRANSMIT_MARGIN);
5309 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5311 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5313 GNUNET_SCHEDULER_add_delayed (delay,
5314 &channel_retransmit_message,
5325 * Core handler for connection creation.
5327 * @param cls Closure (unused).
5328 * @param peer Sender (neighbor).
5329 * @param message Message.
5331 * @return GNUNET_OK to keep the connection open,
5332 * GNUNET_SYSERR to close it (signal serious error)
5335 handle_mesh_connection_create (void *cls,
5336 const struct GNUNET_PeerIdentity *peer,
5337 const struct GNUNET_MessageHeader *message)
5339 struct GNUNET_MESH_ConnectionCreate *msg;
5340 struct GNUNET_PeerIdentity *id;
5341 struct GNUNET_HashCode *cid;
5342 struct MeshPeerPath *path;
5343 struct MeshPeer *dest_peer;
5344 struct MeshPeer *orig_peer;
5345 struct MeshConnection *c;
5346 unsigned int own_pos;
5350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5354 size = ntohs (message->size);
5355 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5357 GNUNET_break_op (0);
5361 /* Calculate hops */
5362 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5363 if (size % sizeof (struct GNUNET_PeerIdentity))
5365 GNUNET_break_op (0);
5368 size /= sizeof (struct GNUNET_PeerIdentity);
5371 GNUNET_break_op (0);
5374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5376 /* Get parameters */
5377 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5379 id = (struct GNUNET_PeerIdentity *) &msg[1];
5380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5381 " connection %s (%s).\n",
5382 GNUNET_h2s (cid), GNUNET_i2s (id));
5384 /* Create connection */
5385 c = connection_get (cid);
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5389 c = connection_new (cid);
5392 connection_reset_timeout (c, GNUNET_YES);
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5396 path = path_new (size);
5398 for (i = 0; i < size; i++)
5400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5401 GNUNET_i2s (&id[i]));
5402 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5403 if (path->peers[i] == myid)
5406 if (own_pos == 0 && path->peers[own_pos] != myid)
5408 /* create path: self not found in path through self */
5409 GNUNET_break_op (0);
5410 path_destroy (path);
5411 connection_destroy (c);
5414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5415 path_add_to_peers (path, GNUNET_NO);
5416 c->path = path_duplicate (path);
5417 c->own_pos = own_pos;
5423 if (MESH_CONNECTION_NEW == c->state)
5424 connection_change_state (c, MESH_CONNECTION_SENT);
5426 /* Remember peers */
5427 dest_peer = peer_get (&id[size - 1]);
5428 orig_peer = peer_get (&id[0]);
5430 /* Is it a connection to us? */
5431 if (c->own_pos == size - 1)
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5434 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5436 if (NULL == orig_peer->tunnel)
5438 orig_peer->tunnel = tunnel_new ();
5439 orig_peer->tunnel->peer = orig_peer;
5441 tunnel_add_connection (orig_peer->tunnel, c);
5442 if (MESH_TUNNEL_NEW == c->t->state)
5443 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5445 send_connection_ack (c, GNUNET_NO);
5446 if (MESH_CONNECTION_SENT == c->state)
5447 connection_change_state (c, MESH_CONNECTION_ACK);
5449 /* Keep tunnel alive in direction dest->owner*/
5450 connection_reset_timeout (c, GNUNET_NO);
5454 /* It's for somebody else! Retransmit. */
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5456 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5457 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5458 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5465 * Core handler for path ACKs
5467 * @param cls closure
5468 * @param message message
5469 * @param peer peer identity this notification is about
5471 * @return GNUNET_OK to keep the connection open,
5472 * GNUNET_SYSERR to close it (signal serious error)
5475 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5476 const struct GNUNET_MessageHeader *message)
5478 struct GNUNET_MESH_ConnectionACK *msg;
5479 struct MeshConnection *c;
5480 struct MeshPeerPath *p;
5481 struct MeshPeer *pi;
5484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5486 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5488 GNUNET_h2s (&msg->cid));
5489 c = connection_get (&msg->cid);
5492 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5501 pi = peer_get (peer);
5502 if (connection_get_next_hop (c) == pi)
5504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
5506 if (MESH_CONNECTION_SENT == c->state)
5507 connection_change_state (c, MESH_CONNECTION_ACK);
5509 else if (connection_get_prev_hop (c) == pi)
5511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK\n");
5513 connection_change_state (c, MESH_CONNECTION_READY);
5517 GNUNET_break_op (0);
5520 connection_reset_timeout (c, fwd);
5522 /* Add path to peers? */
5526 path_add_to_peers (p, GNUNET_YES);
5533 /* Message for us as creator? */
5534 if (connection_is_origin (c, GNUNET_YES))
5536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
5537 connection_change_state (c, MESH_CONNECTION_READY);
5538 if (MESH_TUNNEL_READY != c->t->state)
5539 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5540 send_connection_ack (c, GNUNET_YES);
5541 tunnel_send_queued_data (c->t, GNUNET_YES);
5542 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5544 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5545 c->t->peer->dhtget = NULL;
5550 /* Message for us as destination? */
5551 if (connection_is_terminal (c, GNUNET_YES))
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5554 if (MESH_TUNNEL_READY != c->t->state)
5555 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5556 connection_change_state (c, MESH_CONNECTION_READY);
5557 tunnel_send_queued_data (c->t, GNUNET_NO);
5561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5562 send_prebuilt_message_connection (message, c, NULL, fwd);
5568 * Core handler for notifications of broken paths
5570 * @param cls Closure (unused).
5571 * @param peer Peer identity of sending neighbor.
5572 * @param message Message.
5574 * @return GNUNET_OK to keep the connection open,
5575 * GNUNET_SYSERR to close it (signal serious error)
5578 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5579 const struct GNUNET_MessageHeader *message)
5581 struct GNUNET_MESH_ConnectionBroken *msg;
5582 struct MeshConnection *c;
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5585 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5586 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5588 GNUNET_i2s (&msg->peer1));
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5590 GNUNET_i2s (&msg->peer2));
5591 c = connection_get (&msg->cid);
5594 GNUNET_break_op (0);
5597 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5598 GNUNET_PEER_search (&msg->peer2));
5605 * Core handler for tunnel destruction
5607 * @param cls Closure (unused).
5608 * @param peer Peer identity of sending neighbor.
5609 * @param message Message.
5611 * @return GNUNET_OK to keep the connection open,
5612 * GNUNET_SYSERR to close it (signal serious error)
5615 handle_mesh_connection_destroy (void *cls,
5616 const struct GNUNET_PeerIdentity *peer,
5617 const struct GNUNET_MessageHeader *message)
5619 struct GNUNET_MESH_ConnectionDestroy *msg;
5620 struct MeshConnection *c;
5624 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5626 "Got a CONNECTION DESTROY message from %s\n",
5628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5629 " for connection %s\n",
5630 GNUNET_h2s (&msg->cid));
5631 c = connection_get (&msg->cid);
5634 /* Probably already got the message from another path,
5635 * destroyed the tunnel and retransmitted to children.
5638 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5642 id = GNUNET_PEER_search (peer);
5643 if (id == connection_get_prev_hop (c)->id)
5645 else if (id == connection_get_next_hop (c)->id)
5649 GNUNET_break_op (0);
5652 send_prebuilt_message_connection (message, c, NULL, fwd);
5653 c->destroy = GNUNET_YES;
5660 * Handler for channel create messages.
5662 * @param t Tunnel this channel is to be created in.
5663 * @param msg Message.
5664 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5667 handle_channel_create (struct MeshTunnel2 *t,
5668 struct GNUNET_MESH_ChannelCreate *msg,
5671 MESH_ChannelNumber chid;
5672 struct MeshChannel *ch;
5673 struct MeshClient *c;
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5677 /* Check message size */
5678 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5680 GNUNET_break_op (0);
5684 /* Check if channel exists */
5685 chid = ntohl (msg->chid);
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " chid %u\n", chid);
5687 ch = channel_get (t, chid);
5690 /* Probably a retransmission, safe to ignore */
5691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5692 if (NULL != ch->dest)
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5695 channel_send_ack (ch, !fwd);
5701 /* Create channel */
5702 ch = channel_new (t, NULL, 0);
5704 channel_set_options (ch, ntohl (msg->opt));
5707 /* Find a destination client */
5708 port = ntohl (msg->port);
5709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5710 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5713 /* TODO send reject */
5714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5719 channel_add_client (ch, c);
5720 if (GNUNET_YES == ch->reliable)
5721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5723 send_local_channel_create (ch);
5724 channel_send_ack (ch, fwd);
5725 send_local_ack (ch, !fwd);
5730 * Handler for channel ack messages.
5732 * @param t Tunnel this channel is to be created in.
5733 * @param msg Message.
5734 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5737 handle_channel_ack (struct MeshTunnel2 *t,
5738 struct GNUNET_MESH_ChannelManage *msg,
5741 MESH_ChannelNumber chid;
5742 struct MeshChannel *ch;
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5745 /* Check message size */
5746 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5748 GNUNET_break_op (0);
5752 /* Check if channel exists */
5753 chid = ntohl (msg->chid);
5754 ch = channel_get (t, chid);
5757 GNUNET_break_op (0);
5758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5762 channel_confirm (ch, !fwd);
5767 * Handler for channel destroy messages.
5769 * @param t Tunnel this channel is to be destroyed of.
5770 * @param msg Message.
5771 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5774 handle_channel_destroy (struct MeshTunnel2 *t,
5775 struct GNUNET_MESH_ChannelManage *msg,
5778 MESH_ChannelNumber chid;
5779 struct MeshChannel *ch;
5781 /* Check message size */
5782 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5784 GNUNET_break_op (0);
5788 /* Check if channel exists */
5789 chid = ntohl (msg->chid);
5790 ch = channel_get (t, chid);
5793 /* Probably a retransmission, safe to ignore */
5797 send_local_channel_destroy (ch, fwd);
5798 channel_destroy (ch);
5803 handle_decrypted (struct MeshTunnel2 *t,
5804 const struct GNUNET_MessageHeader *msgh,
5807 switch (ntohs (msgh->type))
5809 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5810 /* Don't send hop ACK, wait for client to ACK */
5811 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5814 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5815 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5818 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5819 handle_channel_create (t,
5820 (struct GNUNET_MESH_ChannelCreate *) msgh,
5824 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5825 handle_channel_ack (t,
5826 (struct GNUNET_MESH_ChannelManage *) msgh,
5830 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5831 handle_channel_destroy (t,
5832 (struct GNUNET_MESH_ChannelManage *) msgh,
5837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5838 "end-to-end message not known (%u)\n",
5839 ntohs (msgh->type));
5845 * Generic handler for mesh network encrypted traffic.
5847 * @param peer Peer identity this notification is about.
5848 * @param message Encrypted message.
5849 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5851 * @return GNUNET_OK to keep the connection open,
5852 * GNUNET_SYSERR to close it (signal serious error)
5855 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5856 const struct GNUNET_MESH_Encrypted *msg,
5859 struct MeshConnection *c;
5860 struct MeshTunnel2 *t;
5861 struct MeshPeer *neighbor;
5862 struct MeshFlowControl *fc;
5869 size = ntohs (msg->header.size);
5871 sizeof (struct GNUNET_MESH_Encrypted) +
5872 sizeof (struct GNUNET_MessageHeader))
5874 GNUNET_break_op (0);
5877 type = ntohs (msg->header.type);
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5880 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5882 /* Check connection */
5883 c = connection_get (&msg->cid);
5886 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5891 fc = fwd ? &c->bck_fc : &c->fwd_fc;
5893 /* Check if origin is as expected */
5894 neighbor = connection_get_hop (c, !fwd);
5895 if (peer_get (peer)->id != neighbor->id)
5897 GNUNET_break_op (0);
5902 pid = ntohl (msg->pid);
5903 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5905 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5907 "WARNING Received PID %u, (prev %u), ACK %u\n",
5908 pid, fc->last_pid_recv, fc->last_ack_sent);
5911 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5913 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5915 " Pid %u not expected (%u+), dropping!\n",
5916 pid, fc->last_pid_recv + 1);
5919 if (MESH_CONNECTION_SENT == c->state)
5920 connection_change_state (c, MESH_CONNECTION_READY);
5921 connection_reset_timeout (c, fwd);
5922 fc->last_pid_recv = pid;
5924 /* Is this message for us? */
5925 if (connection_is_terminal (c, fwd))
5927 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5929 struct GNUNET_MessageHeader *msgh;
5932 /* TODO signature verification */
5933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5934 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5936 fc->last_pid_recv = pid;
5937 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5941 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
5942 handle_decrypted (t, msgh, fwd);
5943 off += ntohs (msgh->size);
5945 send_ack (c, NULL, fwd);
5949 /* Message not for us: forward to next hop */
5950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5951 ttl = ntohl (msg->ttl);
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5955 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5956 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5957 send_ack (c, NULL, fwd);
5960 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5962 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5969 * Core handler for mesh network traffic going orig->dest.
5971 * @param cls Closure (unused).
5972 * @param message Message received.
5973 * @param peer Peer who sent the message.
5975 * @return GNUNET_OK to keep the connection open,
5976 * GNUNET_SYSERR to close it (signal serious error)
5979 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5980 const struct GNUNET_MessageHeader *message)
5982 return handle_mesh_encrypted (peer,
5983 (struct GNUNET_MESH_Encrypted *)message,
5988 * Core handler for mesh network traffic going dest->orig.
5990 * @param cls Closure (unused).
5991 * @param message Message received.
5992 * @param peer Peer who sent the message.
5994 * @return GNUNET_OK to keep the connection open,
5995 * GNUNET_SYSERR to close it (signal serious error)
5998 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5999 const struct GNUNET_MessageHeader *message)
6001 return handle_mesh_encrypted (peer,
6002 (struct GNUNET_MESH_Encrypted *)message,
6008 * Core handler for mesh network traffic point-to-point acks.
6010 * @param cls closure
6011 * @param message message
6012 * @param peer peer identity this notification is about
6014 * @return GNUNET_OK to keep the connection open,
6015 * GNUNET_SYSERR to close it (signal serious error)
6018 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6019 const struct GNUNET_MessageHeader *message)
6021 struct GNUNET_MESH_ACK *msg;
6022 struct MeshConnection *c;
6023 struct MeshFlowControl *fc;
6028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
6029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6031 msg = (struct GNUNET_MESH_ACK *) message;
6033 c = connection_get (&msg->cid);
6037 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
6042 /* Is this a forward or backward ACK? */
6043 id = GNUNET_PEER_search (peer);
6044 if (connection_get_next_hop (c)->id == id)
6046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6050 else if (connection_get_prev_hop (c)->id == id)
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6058 GNUNET_break_op (0);
6062 ack = ntohl (msg->ack);
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u (was %u)\n",
6064 ack, fc->last_ack_recv);
6065 if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
6066 fc->last_ack_recv = ack;
6068 /* Cancel polling if the ACK is big enough. */
6069 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
6070 GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
6073 GNUNET_SCHEDULER_cancel (fc->poll_task);
6074 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
6075 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
6078 connection_unlock_queue (c, fwd);
6085 * Core handler for mesh network traffic point-to-point ack polls.
6087 * @param cls closure
6088 * @param message message
6089 * @param peer peer identity this notification is about
6091 * @return GNUNET_OK to keep the connection open,
6092 * GNUNET_SYSERR to close it (signal serious error)
6095 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6096 const struct GNUNET_MessageHeader *message)
6098 struct GNUNET_MESH_Poll *msg;
6099 struct MeshConnection *c;
6100 struct MeshFlowControl *fc;
6105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
6106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
6109 msg = (struct GNUNET_MESH_Poll *) message;
6111 c = connection_get (&msg->cid);
6115 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
6117 GNUNET_break_op (0);
6121 /* Is this a forward or backward ACK?
6122 * Note: a poll should never be needed in a loopback case,
6123 * since there is no possiblility of packet loss there, so
6124 * this way of discerining FWD/BCK should not be a problem.
6126 id = GNUNET_PEER_search (peer);
6127 if (connection_get_next_hop (c)->id == id)
6129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6132 else if (connection_get_prev_hop (c)->id == id)
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6139 GNUNET_break_op (0);
6143 pid = ntohl (msg->pid);
6144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
6145 pid, fc->last_pid_recv);
6146 fc->last_pid_recv = pid;
6147 fwd = fc == &c->fwd_fc;
6148 send_ack (c, NULL, fwd);
6155 * Core handler for mesh keepalives.
6157 * @param cls closure
6158 * @param message message
6159 * @param peer peer identity this notification is about
6160 * @return GNUNET_OK to keep the connection open,
6161 * GNUNET_SYSERR to close it (signal serious error)
6163 * TODO: Check who we got this from, to validate route.
6166 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6167 const struct GNUNET_MessageHeader *message)
6169 struct GNUNET_MESH_ConnectionKeepAlive *msg;
6170 struct MeshConnection *c;
6171 struct MeshPeer *neighbor;
6174 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
6175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6178 c = connection_get (&msg->cid);
6181 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
6186 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
6187 GNUNET_YES : GNUNET_NO;
6189 /* Check if origin is as expected */
6190 neighbor = connection_get_hop (c, fwd);
6191 if (peer_get (peer)->id != neighbor->id)
6193 GNUNET_break_op (0);
6197 connection_change_state (c, MESH_CONNECTION_READY);
6198 connection_reset_timeout (c, fwd);
6200 if (connection_is_terminal (c, fwd))
6203 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6204 send_prebuilt_message_connection (message, c, NULL, fwd);
6212 * Functions to handle messages from core
6214 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6215 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
6217 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
6218 sizeof (struct GNUNET_MESH_ConnectionACK)},
6219 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
6220 sizeof (struct GNUNET_MESH_ConnectionBroken)},
6221 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
6222 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
6223 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
6224 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6225 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
6226 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6227 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6228 sizeof (struct GNUNET_MESH_ACK)},
6229 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6230 sizeof (struct GNUNET_MESH_Poll)},
6231 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
6232 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
6238 * Function to process paths received for a new peer addition. The recorded
6239 * paths form the initial tunnel, which can be optimized later.
6240 * Called on each result obtained for the DHT search.
6242 * @param cls closure
6243 * @param exp when will this value expire
6244 * @param key key of the result
6245 * @param get_path path of the get request
6246 * @param get_path_length lenght of get_path
6247 * @param put_path path of the put request
6248 * @param put_path_length length of the put_path
6249 * @param type type of the result
6250 * @param size number of bytes in data
6251 * @param data pointer to the result data
6254 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6255 const struct GNUNET_HashCode * key,
6256 const struct GNUNET_PeerIdentity *get_path,
6257 unsigned int get_path_length,
6258 const struct GNUNET_PeerIdentity *put_path,
6259 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6260 size_t size, const void *data)
6262 struct MeshPeer *peer = cls;
6263 struct MeshPeerPath *p;
6264 struct MeshConnection *c;
6265 struct GNUNET_PeerIdentity pi;
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6269 GNUNET_PEER_resolve (peer->id, &pi);
6270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6272 p = path_build_from_dht (get_path, get_path_length,
6273 put_path, put_path_length);
6274 path_add_to_peers (p, GNUNET_NO);
6277 /* Count connections */
6278 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6280 /* If we already have 3 (or more (?!)) connections, it's enough */
6284 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6287 peer_connect (peer);
6293 /******************************************************************************/
6294 /********************* MESH LOCAL HANDLES **************************/
6295 /******************************************************************************/
6299 * Handler for client connection.
6301 * @param cls Closure (unused).
6302 * @param client Client handler.
6305 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6307 struct MeshClient *c;
6311 c = GNUNET_malloc (sizeof (struct MeshClient));
6313 c->id = next_client_id++; /* overflow not important: just for debug */
6314 GNUNET_SERVER_client_keep (client);
6315 GNUNET_SERVER_client_set_user_context (client, c);
6316 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6321 * Handler for client disconnection
6323 * @param cls closure
6324 * @param client identification of the client; NULL
6325 * for the last call when the server is destroyed
6328 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6330 struct MeshClient *c;
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6339 c = client_get (client);
6342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6344 GNUNET_SERVER_client_drop (c->handle);
6345 c->shutting_down = GNUNET_YES;
6346 if (NULL != c->own_channels)
6348 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6349 &channel_destroy_iterator, c);
6350 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6353 if (NULL != c->incoming_channels)
6355 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6356 &channel_destroy_iterator, c);
6357 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6360 if (NULL != c->ports)
6362 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6363 &client_release_ports, c);
6364 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6366 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6367 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6381 * Handler for new clients
6383 * @param cls closure
6384 * @param client identification of the client
6385 * @param message the actual message, which includes messages the client wants
6388 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6389 const struct GNUNET_MessageHeader *message)
6391 struct GNUNET_MESH_ClientConnect *cc_msg;
6392 struct MeshClient *c;
6397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6399 /* Check data sanity */
6400 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6401 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6402 if (0 != (size % sizeof (uint32_t)))
6405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6408 size /= sizeof (uint32_t);
6410 /* Initialize new client structure */
6411 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6418 p = (uint32_t *) &cc_msg[1];
6419 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6420 for (i = 0; i < size; i++)
6423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6425 /* store in client's hashmap */
6426 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6427 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6428 /* store in global hashmap */
6429 /* FIXME only allow one client to have the port open,
6430 * have a backup hashmap with waiting clients */
6431 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6432 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6436 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6437 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6438 GNUNET_SERVER_notification_context_add (nc, client);
6439 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6447 * Handler for requests of new tunnels
6449 * @param cls Closure.
6450 * @param client Identification of the client.
6451 * @param message The actual message.
6454 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6455 const struct GNUNET_MessageHeader *message)
6457 struct GNUNET_MESH_ChannelMessage *msg;
6458 struct MeshPeer *peer;
6459 struct MeshTunnel2 *t;
6460 struct MeshChannel *ch;
6461 struct MeshClient *c;
6462 MESH_ChannelNumber chid;
6464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6466 /* Sanity check for client registration */
6467 if (NULL == (c = client_get (client)))
6470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6475 /* Message size sanity check */
6476 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6483 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6485 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6486 chid = ntohl (msg->channel_id);
6488 /* Sanity check for duplicate channel IDs */
6489 if (NULL != channel_get_by_local_id (c, chid))
6492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6496 peer = peer_get (&msg->peer);
6497 if (NULL == peer->tunnel)
6499 peer->tunnel = tunnel_new ();
6500 peer->tunnel->peer = peer;
6501 if (peer->id == myid)
6503 tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
6507 peer_connect (peer);
6512 /* Create channel */
6513 ch = channel_new (t, c, chid);
6517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6520 ch->port = ntohl (msg->port);
6521 channel_set_options (ch, ntohl (msg->opt));
6523 /* In unreliable channels, we'll use the DLL to buffer BCK data */
6524 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6525 ch->root_rel->ch = ch;
6526 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6529 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6531 /* Send create channel */
6533 struct GNUNET_MESH_ChannelCreate msgcc;
6535 msgcc.header.size = htons (sizeof (msgcc));
6536 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6537 msgcc.chid = htonl (ch->gid);
6538 msgcc.port = msg->port;
6539 msgcc.opt = msg->opt;
6541 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6550 * Handler for requests of deleting tunnels
6552 * @param cls closure
6553 * @param client identification of the client
6554 * @param message the actual message
6557 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6558 const struct GNUNET_MessageHeader *message)
6560 struct GNUNET_MESH_ChannelMessage *msg;
6561 struct MeshClient *c;
6562 struct MeshChannel *ch;
6563 struct MeshTunnel2 *t;
6564 MESH_ChannelNumber chid;
6566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6567 "Got a DESTROY CHANNEL from client!\n");
6569 /* Sanity check for client registration */
6570 if (NULL == (c = client_get (client)))
6573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6578 /* Message sanity check */
6579 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6586 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6588 /* Retrieve tunnel */
6589 chid = ntohl (msg->channel_id);
6590 ch = channel_get_by_local_id (c, chid);
6593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6599 /* Cleanup after the tunnel */
6600 client_delete_channel (c, ch);
6601 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6605 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6612 " channel %X client %p (%p, %p)\n",
6613 chid, c, ch->root, ch->dest);
6618 channel_destroy (ch);
6619 tunnel_destroy_if_empty (t);
6621 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6627 * Handler for client traffic
6629 * @param cls closure
6630 * @param client identification of the client
6631 * @param message the actual message
6634 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6635 const struct GNUNET_MessageHeader *message)
6637 struct GNUNET_MESH_LocalData *msg;
6638 struct MeshClient *c;
6639 struct MeshChannel *ch;
6640 struct MeshChannelReliability *rel;
6641 MESH_ChannelNumber chid;
6645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6646 "Got data from a client!\n");
6648 /* Sanity check for client registration */
6649 if (NULL == (c = client_get (client)))
6652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6657 msg = (struct GNUNET_MESH_LocalData *) message;
6659 /* Sanity check for message size */
6660 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6661 if (size < sizeof (struct GNUNET_MessageHeader))
6664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6668 /* Channel exists? */
6669 chid = ntohl (msg->id);
6670 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6671 ch = channel_get_by_local_id (c, chid);
6675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6679 /* Is the client in the channel? */
6682 ch->root->handle == client)
6686 ch->dest->handle == client) ) )
6689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6693 rel = fwd ? ch->root_rel : ch->dest_rel;
6694 rel->client_ready = GNUNET_NO;
6696 /* Ok, everything is correct, send the message. */
6698 struct GNUNET_MESH_Data *payload;
6699 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6700 unsigned char cbuf[p2p_size];
6702 payload = (struct GNUNET_MESH_Data *) cbuf;
6703 payload->mid = htonl (rel->mid_send);
6705 memcpy (&payload[1], &msg[1], size);
6706 payload->header.size = htons (p2p_size);
6707 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6708 payload->chid = htonl (ch->gid);
6709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6710 send_prebuilt_message_channel (&payload->header, ch, fwd);
6712 if (GNUNET_YES == ch->reliable)
6713 channel_save_copy (ch, &payload->header, fwd);
6715 if (tunnel_get_buffer (ch->t, fwd) > 0)
6716 send_local_ack (ch, fwd);
6717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6725 * Handler for client's ACKs for payload traffic.
6727 * @param cls Closure (unused).
6728 * @param client Identification of the client.
6729 * @param message The actual message.
6732 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6733 const struct GNUNET_MessageHeader *message)
6735 struct GNUNET_MESH_LocalAck *msg;
6736 struct MeshChannelReliability *rel;
6737 struct MeshChannel *ch;
6738 struct MeshClient *c;
6739 MESH_ChannelNumber chid;
6742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6744 /* Sanity check for client registration */
6745 if (NULL == (c = client_get (client)))
6748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6753 msg = (struct GNUNET_MESH_LocalAck *) message;
6755 /* Channel exists? */
6756 chid = ntohl (msg->channel_id);
6757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6758 ch = channel_get_by_local_id (c, chid);
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6764 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6769 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6770 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6771 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6772 rel = fwd ? ch->dest_rel : ch->root_rel;
6774 rel->client_ready = GNUNET_YES;
6775 channel_send_client_buffered_data (ch, c, fwd);
6776 send_ack (NULL, ch, fwd);
6778 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6785 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6787 * @param cls Closure (client handle).
6788 * @param key Key (hashed tunnel ID, unused).
6789 * @param value Tunnel info.
6791 * @return GNUNET_YES, to keep iterating.
6794 // monitor_all_tunnels_iterator (void *cls,
6795 // const struct GNUNET_HashCode * key,
6798 // struct GNUNET_SERVER_Client *client = cls;
6799 // struct MeshChannel *ch = value;
6800 // struct GNUNET_MESH_LocalMonitor *msg;
6802 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6803 // msg->channel_id = htonl (ch->gid);
6804 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6805 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6807 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6808 // "* sending info about tunnel %s\n",
6809 // GNUNET_i2s (&msg->owner));
6811 // GNUNET_SERVER_notification_context_unicast (nc, client,
6812 // &msg->header, GNUNET_NO);
6813 // return GNUNET_YES;
6818 * Handler for client's MONITOR request.
6820 * @param cls Closure (unused).
6821 * @param client Identification of the client.
6822 * @param message The actual message.
6825 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6826 const struct GNUNET_MessageHeader *message)
6828 struct MeshClient *c;
6830 /* Sanity check for client registration */
6831 if (NULL == (c = client_get (client)))
6834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6839 "Received get tunnels request from client %u\n",
6841 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6842 // monitor_all_tunnels_iterator,
6844 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6845 "Get tunnels request from client %u completed\n",
6847 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6852 * Handler for client's MONITOR_TUNNEL request.
6854 * @param cls Closure (unused).
6855 * @param client Identification of the client.
6856 * @param message The actual message.
6859 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6860 const struct GNUNET_MessageHeader *message)
6862 const struct GNUNET_MESH_LocalMonitor *msg;
6863 struct GNUNET_MESH_LocalMonitor *resp;
6864 struct MeshClient *c;
6865 struct MeshChannel *ch;
6867 /* Sanity check for client registration */
6868 if (NULL == (c = client_get (client)))
6871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6875 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6876 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6877 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6880 ntohl (msg->channel_id));
6881 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6885 /* We don't know the tunnel */
6886 struct GNUNET_MESH_LocalMonitor warn;
6889 GNUNET_SERVER_notification_context_unicast (nc, client,
6892 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6896 /* Initialize context */
6897 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6899 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6900 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6901 &resp->header, GNUNET_NO);
6904 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6905 "Monitor tunnel request from client %u completed\n",
6907 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6912 * Functions to handle messages from clients
6914 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6915 {&handle_local_new_client, NULL,
6916 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6917 {&handle_local_channel_create, NULL,
6918 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6919 sizeof (struct GNUNET_MESH_ChannelMessage)},
6920 {&handle_local_channel_destroy, NULL,
6921 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6922 sizeof (struct GNUNET_MESH_ChannelMessage)},
6923 {&handle_local_data, NULL,
6924 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6925 {&handle_local_ack, NULL,
6926 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6927 sizeof (struct GNUNET_MESH_LocalAck)},
6928 {&handle_local_get_tunnels, NULL,
6929 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6930 sizeof (struct GNUNET_MessageHeader)},
6931 {&handle_local_show_tunnel, NULL,
6932 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6933 sizeof (struct GNUNET_MESH_LocalMonitor)},
6939 * Method called whenever a given peer connects.
6941 * @param cls closure
6942 * @param peer peer identity this notification is about
6945 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6947 struct MeshPeer *pi;
6948 struct MeshPeerPath *path;
6950 DEBUG_CONN ("Peer connected\n");
6951 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6952 pi = peer_get (peer);
6955 DEBUG_CONN (" (self)\n");
6956 path = path_new (1);
6960 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6961 path = path_new (2);
6962 path->peers[1] = pi->id;
6963 GNUNET_PEER_change_rc (pi->id, 1);
6964 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6966 path->peers[0] = myid;
6967 GNUNET_PEER_change_rc (myid, 1);
6968 peer_add_path (pi, path, GNUNET_YES);
6970 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6976 * Method called whenever a peer disconnects.
6978 * @param cls closure
6979 * @param peer peer identity this notification is about
6982 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6984 struct MeshPeer *pi;
6986 DEBUG_CONN ("Peer disconnected\n");
6987 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6994 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6997 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6998 pi->connections = NULL;
7001 DEBUG_CONN (" (self)\n");
7003 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7010 * Install server (service) handlers and start listening to clients.
7015 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7016 GNUNET_SERVER_connect_notify (server_handle,
7017 &handle_local_client_connect, NULL);
7018 GNUNET_SERVER_disconnect_notify (server_handle,
7019 &handle_local_client_disconnect, NULL);
7020 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7022 clients_head = NULL;
7023 clients_tail = NULL;
7025 GNUNET_SERVER_resume (server_handle);
7030 * To be called on core init/fail.
7032 * @param cls Closure (config)
7033 * @param identity the public identity of this peer
7036 core_init (void *cls,
7037 const struct GNUNET_PeerIdentity *identity)
7039 const struct GNUNET_CONFIGURATION_Handle *c = cls;
7042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7043 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
7045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7048 GNUNET_i2s (identity));
7049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7051 GNUNET_i2s (&my_full_id));
7052 GNUNET_CORE_disconnect (core_handle);
7053 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7054 NULL, /* Closure passed to MESH functions */
7055 &core_init, /* Call core_init once connected */
7056 &core_connect, /* Handle connects */
7057 &core_disconnect, /* remove peers on disconnects */
7058 NULL, /* Don't notify about all incoming messages */
7059 GNUNET_NO, /* For header only in notification */
7060 NULL, /* Don't notify about all outbound messages */
7061 GNUNET_NO, /* For header-only out notification */
7062 core_handlers); /* Register these handlers */
7071 /******************************************************************************/
7072 /************************ MAIN FUNCTIONS ****************************/
7073 /******************************************************************************/
7076 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7078 * @param cls closure
7079 * @param key current key code
7080 * @param value value in the hash map
7081 * @return GNUNET_YES if we should continue to iterate,
7085 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7087 struct MeshPeer *p = value;
7088 struct MeshTunnel2 *t = p->tunnel;
7097 * Task run during shutdown.
7103 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7107 if (core_handle != NULL)
7109 GNUNET_CORE_disconnect (core_handle);
7112 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
7113 if (dht_handle != NULL)
7115 GNUNET_DHT_disconnect (dht_handle);
7120 GNUNET_SERVER_notification_context_destroy (nc);
7123 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7125 GNUNET_SCHEDULER_cancel (announce_id_task);
7126 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7133 * Process mesh requests.
7135 * @param cls closure
7136 * @param server the initialized server
7137 * @param c configuration to use
7140 run (void *cls, struct GNUNET_SERVER_Handle *server,
7141 const struct GNUNET_CONFIGURATION_Handle *c)
7144 struct GNUNET_CRYPTO_EccPrivateKey *pk;
7146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7147 server_handle = server;
7148 GNUNET_SERVER_suspend (server_handle);
7151 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
7154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7156 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7157 "mesh", "peer/privatekey");
7158 GNUNET_SCHEDULER_shutdown ();
7163 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
7164 &refresh_connection_time))
7166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7168 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7169 "mesh", "refresh path time");
7170 GNUNET_SCHEDULER_shutdown ();
7175 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7180 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7181 "mesh", "id announce time");
7182 GNUNET_SCHEDULER_shutdown ();
7187 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7192 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7193 "mesh", "connect timeout");
7194 GNUNET_SCHEDULER_shutdown ();
7199 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7204 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7205 "mesh", "max msgs queue");
7206 GNUNET_SCHEDULER_shutdown ();
7211 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
7214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7216 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7217 "mesh", "max tunnels");
7218 GNUNET_SCHEDULER_shutdown ();
7223 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7228 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7229 "mesh", "default ttl", 64);
7234 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7238 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7239 "mesh", "max peers", 1000);
7244 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
7251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7252 "Mesh is running with drop mode enabled. "
7253 "This is NOT a good idea! "
7254 "Remove the DROP_PERCENT option from your configuration.\n");
7258 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7259 &dht_replication_level))
7261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7263 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7264 "mesh", "dht replication level", 3);
7265 dht_replication_level = 3;
7268 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7269 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7270 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7272 dht_handle = GNUNET_DHT_connect (c, 64);
7273 if (NULL == dht_handle)
7277 stats = GNUNET_STATISTICS_create ("mesh", c);
7279 /* Scheduled the task to clean up when shutdown is called */
7280 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7282 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7283 GNUNET_free (keyfile);
7284 GNUNET_assert (NULL != pk);
7285 my_private_key = pk;
7286 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7287 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7288 &my_full_id.hashPubKey);
7289 myid = GNUNET_PEER_intern (&my_full_id);
7290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7291 "Mesh for peer [%s] starting\n",
7292 GNUNET_i2s(&my_full_id));
7294 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7295 NULL, /* Closure passed to MESH functions */
7296 &core_init, /* Call core_init once connected */
7297 &core_connect, /* Handle connects */
7298 &core_disconnect, /* remove peers on disconnects */
7299 NULL, /* Don't notify about all incoming messages */
7300 GNUNET_NO, /* For header only in notification */
7301 NULL, /* Don't notify about all outbound messages */
7302 GNUNET_NO, /* For header-only out notification */
7303 core_handlers); /* Register these handlers */
7304 if (NULL == core_handle)
7307 GNUNET_SCHEDULER_shutdown ();
7310 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7316 * The main function for the mesh service.
7318 * @param argc number of arguments from the command line
7319 * @param argv command line arguments
7320 * @return 0 ok, 1 on error
7323 main (int argc, char *const *argv)
7328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7329 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7331 ret = (GNUNET_OK == r) ? 0 : 1;
7332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");