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
32 * - relay corking down to core
33 * - set ttl relative to path length
37 * - peer: other mesh instance. If there is direct connection it's a neighbor.
38 * - tunnel: encrypted connection to a peer, neighbor or not.
39 * - channel: connection between two clients, on the same or different peers.
40 * have properties like reliability.
41 * - path: series of directly connected peer from one peer to another.
42 * - connection: path which is being used in a tunnel.
46 #include "gnunet_crypto_lib.h"
48 #include "mesh_protocol_enc.h"
49 #include "mesh_path.h"
50 #include "block_mesh.h"
51 #include "gnunet_dht_service.h"
52 #include "gnunet_statistics_service.h"
54 #define MESH_BLOOM_SIZE 128
56 #define MESH_DEBUG_DHT GNUNET_NO
57 #define MESH_DEBUG_CONNECTION GNUNET_NO
58 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
60 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
61 GNUNET_TIME_UNIT_MINUTES,\
63 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
64 #define MESH_RETRANSMIT_MARGIN 4
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
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 created, waiting for ACK.
147 MESH_CONNECTION_SENT,
150 * Connection confirmed, ready to carry traffic..
152 MESH_CONNECTION_READY,
156 /******************************************************************************/
157 /************************ DATA STRUCTURES ****************************/
158 /******************************************************************************/
160 /** FWD declaration */
164 struct MeshConnection;
166 struct MeshChannelReliability;
170 * Struct containing info about a queued transmission to this peer
177 struct MeshPeerQueue *next;
182 struct MeshPeerQueue *prev;
185 * Peer this transmission is directed to.
187 struct MeshPeer *peer;
190 * Connection this message belongs to.
192 struct MeshConnection *c;
200 * Channel this message belongs to, if known.
202 struct MeshChannel *ch;
205 * Pointer to info stucture used as cls.
215 * Size of the message
222 * Struct to encapsulate all the Flow Control information to a peer to which
223 * we are directly connected (on a core level).
225 struct MeshFlowControl
228 * Connection this controls.
230 struct MeshConnection *c;
233 * How many messages are in the queue on this connection.
235 unsigned int queue_n;
238 * How many messages do we accept in the queue.
240 unsigned int queue_max;
243 * ID of the last packet sent towards the peer.
245 uint32_t last_pid_sent;
248 * ID of the last packet received from the peer.
250 uint32_t last_pid_recv;
253 * Last ACK sent to the peer (peer can't send more than this PID).
255 uint32_t last_ack_sent;
258 * Last ACK sent towards the origin (for traffic towards leaf node).
260 uint32_t last_ack_recv;
263 * Task to poll the peer in case of a lost ACK causes stall.
265 GNUNET_SCHEDULER_TaskIdentifier poll_task;
268 * How frequently to poll for ACKs.
270 struct GNUNET_TIME_Relative poll_time;
275 * Struct containing all information regarding a given peer
285 * Last time we heard from this peer
287 struct GNUNET_TIME_Absolute last_contact;
290 * Paths to reach the peer, ordered by ascending hop count
292 struct MeshPeerPath *path_head;
295 * Paths to reach the peer, ordered by ascending hop count
297 struct MeshPeerPath *path_tail;
300 * Handle to stop the DHT search for paths to this peer
302 struct GNUNET_DHT_GetHandle *dhtget;
305 * Tunnel to this peer, if any.
307 struct MeshTunnel2 *tunnel;
310 * Connections that go through this peer, indexed by tid;
312 struct GNUNET_CONTAINER_MultiHashMap *connections;
315 * Handle for queued transmissions
317 struct GNUNET_CORE_TransmitHandle *core_transmit;
320 * Transmission queue to core DLL head
322 struct MeshPeerQueue *queue_head;
325 * Transmission queue to core DLL tail
327 struct MeshPeerQueue *queue_tail;
330 * How many messages are in the queue to this peer.
332 unsigned int queue_n;
337 * Info needed to retry a message in case it gets lost.
339 struct MeshReliableMessage
342 * Double linked list, FIFO style
344 struct MeshReliableMessage *next;
345 struct MeshReliableMessage *prev;
348 * Tunnel Reliability queue this message is in.
350 struct MeshChannelReliability *rel;
353 * ID of the message (ACK needed to free)
358 * When was this message issued (to calculate ACK delay)
360 struct GNUNET_TIME_Absolute timestamp;
362 /* struct GNUNET_MESH_Data with payload */
366 struct MeshChannelReliability
369 * Channel this is about.
371 struct MeshChannel *ch;
374 * DLL of messages sent and not yet ACK'd.
376 struct MeshReliableMessage *head_sent;
377 struct MeshReliableMessage *tail_sent;
380 * Messages pending to send.
385 * DLL of messages received out of order.
387 struct MeshReliableMessage *head_recv;
388 struct MeshReliableMessage *tail_recv;
396 * Can we send data to the client?
401 * Task to resend/poll in case no ACK is received.
403 GNUNET_SCHEDULER_TaskIdentifier retry_task;
406 * Counter for exponential backoff.
408 struct GNUNET_TIME_Relative retry_timer;
411 * How long does it usually take to get an ACK.
413 struct GNUNET_TIME_Relative expected_delay;
418 * Struct containing all information regarding a channel to a remote client.
423 * Tunnel this channel is in.
425 struct MeshTunnel2 *t;
428 * Double linked list.
430 struct MeshChannel *next;
431 struct MeshChannel *prev;
434 * Destination port of the channel.
439 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
441 MESH_ChannelNumber gid;
444 * Local tunnel number for root (owner) client.
445 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
447 MESH_ChannelNumber lid_root;
450 * Local tunnel number for local destination clients (incoming number)
451 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
453 MESH_ChannelNumber lid_dest;
456 * Next MID to use for fwd traffic.
458 uint32_t mid_send_fwd;
461 * Next MID expected for fwd traffic.
463 uint32_t mid_recv_fwd;
466 * Next MID to use for bck traffic.
468 uint32_t mid_send_bck;
471 * Next MID expected for bck traffic.
473 uint32_t mid_recv_bck;
476 * Is the tunnel bufferless (minimum latency)?
481 * Is the tunnel reliable?
486 * Last time the channel was used
488 struct GNUNET_TIME_Absolute timestamp;
491 * Client owner of the tunnel, if any
493 struct MeshClient *root;
496 * Client destination of the tunnel, if any.
498 struct MeshClient *dest;
501 * Flag to signal the destruction of the channel.
502 * If this is set GNUNET_YES the channel will be destroyed
503 * when the queue is empty.
508 * Total messages pending for this channel, payload or not.
510 unsigned int pending_messages;
514 * Only present (non-NULL) at the owner of a tunnel.
516 struct MeshChannelReliability *fwd_rel;
520 * Only present (non-NULL) at the destination of a tunnel.
522 struct MeshChannelReliability *bck_rel;
527 * Struct containing all information regarding a connection to a peer.
529 struct MeshConnection
534 struct MeshConnection *next;
535 struct MeshConnection *prev;
538 * Tunnel this connection is part of.
540 struct MeshTunnel2 *t;
543 * Flow control information for traffic fwd.
545 struct MeshFlowControl fwd_fc;
548 * Flow control information for traffic bck.
550 struct MeshFlowControl bck_fc;
558 * State of the connection.
560 enum MeshConnectionState state;
563 * Path being used for the tunnel.
565 struct MeshPeerPath *path;
568 * Position of the local peer in the path.
570 unsigned int own_pos;
573 * Task to keep the used paths alive at the owner,
574 * time tunnel out on all the other peers.
576 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
579 * Task to keep the used paths alive at the destination,
580 * time tunnel out on all the other peers.
582 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
585 * Pending message count.
587 int pending_messages;
590 * Destroy flag: if true, destroy on last message.
597 * Struct used to queue messages in a tunnel.
599 struct MeshTunnelQueue
604 struct MeshTunnelQueue *next;
605 struct MeshTunnelQueue *prev;
610 struct MeshChannel *ch;
615 /* struct GNUNET_MessageHeader *msg; */
620 * Struct containing all information regarding a tunnel to a peer.
625 * Endpoint of the tunnel.
627 struct MeshPeer *peer;
632 struct GNUNET_HashCode id;
635 * State of the tunnel.
637 enum MeshTunnelState state;
640 * Local peer ephemeral private key
642 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
645 * Local peer ephemeral public key
647 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
650 * Remote peer's public key.
652 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
655 * Encryption ("our") key.
657 struct GNUNET_CRYPTO_AesSessionKey e_key;
660 * Decryption ("their") key.
662 struct GNUNET_CRYPTO_AesSessionKey d_key;
665 * Paths that are actively used to reach the destination peer.
667 struct MeshConnection *connection_head;
668 struct MeshConnection *connection_tail;
671 * Next connection number.
676 * Channels inside this tunnel.
678 struct MeshChannel *channel_head;
679 struct MeshChannel *channel_tail;
682 * Channel ID for the next created channel.
684 MESH_ChannelNumber next_chid;
687 * Channel ID for the next incoming channel.
689 MESH_ChannelNumber next_local_chid;
692 * Pending message count.
694 int pending_messages;
697 * Destroy flag: if true, destroy on last message.
702 * Queued messages, to transmit once tunnel gets connected.
704 struct MeshTunnelQueue *tq_head;
705 struct MeshTunnelQueue *tq_tail;
711 * Struct containing information about a client of the service
713 * TODO: add a list of 'waiting' ports
720 struct MeshClient *next;
725 struct MeshClient *prev;
728 * Tunnels that belong to this client, indexed by local id
730 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
733 * Tunnels this client has accepted, indexed by incoming local id
735 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
738 * Handle to communicate with the client
740 struct GNUNET_SERVER_Client *handle;
743 * Ports that this client has declared interest in.
744 * Indexed by port, contains *Client.
746 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
749 * Whether the client is active or shutting down (don't send confirmations
750 * to a client that is shutting down.
755 * ID of the client, mainly for debug messages
761 /******************************************************************************/
762 /************************ DEBUG FUNCTIONS ****************************/
763 /******************************************************************************/
767 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
768 * @param cls string to print
769 * @param success GNUNET_OK if the PUT was transmitted,
770 * GNUNET_NO on timeout,
771 * GNUNET_SYSERR on disconnect from service
772 * after the PUT message was transmitted
773 * (so we don't know if it was received or not)
778 mesh_debug (void *cls, int success)
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
788 /******************************************************************************/
789 /*********************** GLOBAL VARIABLES ****************************/
790 /******************************************************************************/
792 /************************** Configuration parameters **************************/
795 * How often to send path keepalives. Paths timeout after 4 missed.
797 static struct GNUNET_TIME_Relative refresh_connection_time;
800 * How often to PUT own ID in the DHT.
802 static struct GNUNET_TIME_Relative id_announce_time;
805 * Maximum time allowed to connect to a peer found by string.
807 static struct GNUNET_TIME_Relative connect_timeout;
810 * Default TTL for payload packets.
812 static unsigned long long default_ttl;
815 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
817 static unsigned long long dht_replication_level;
820 * How many connections are we willing to maintain.
821 * Local connections are always allowed, even if there are more connections than max.
823 static unsigned long long max_connections;
826 * How many messages *in total* are we willing to queue, divided by number of
827 * connections to get tunnel queue size.
829 static unsigned long long max_msgs_queue;
832 * How many peers do we want to remember?
834 static unsigned long long max_peers;
837 * Percentage of messages that will be dropped (for test purposes only).
839 static unsigned long long drop_percent;
841 /*************************** Static global variables **************************/
844 * DLL with all the clients, head.
846 static struct MeshClient *clients_head;
849 * DLL with all the clients, tail.
851 static struct MeshClient *clients_tail;
854 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
856 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
859 * Peers known, indexed by PeerIdentity (MeshPeer).
861 static struct GNUNET_CONTAINER_MultiHashMap *peers;
864 * Handle to communicate with core.
866 static struct GNUNET_CORE_Handle *core_handle;
871 static struct GNUNET_DHT_Handle *dht_handle;
874 * Handle to server lib.
876 static struct GNUNET_SERVER_Handle *server_handle;
879 * Handle to the statistics service.
881 static struct GNUNET_STATISTICS_Handle *stats;
884 * Notification context, to send messages to local clients.
886 static struct GNUNET_SERVER_NotificationContext *nc;
889 * Local peer own ID (memory efficient handle).
891 static GNUNET_PEER_Id myid;
894 * Local peer own ID (full value).
896 static struct GNUNET_PeerIdentity my_full_id;
901 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
906 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
909 * All ports clients of this peer have opened.
911 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
914 * Task to periodically announce itself in the network.
916 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
919 * Next ID to assign to a client.
921 unsigned int next_client_id;
924 /******************************************************************************/
925 /*********************** DECLARATIONS **************************/
926 /******************************************************************************/
929 * Function to process paths received for a new peer addition. The recorded
930 * paths form the initial tunnel, which can be optimized later.
931 * Called on each result obtained for the DHT search.
934 * @param exp when will this value expire
935 * @param key key of the result
936 * @param type type of the result
937 * @param size number of bytes in data
938 * @param data pointer to the result data
941 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
942 const struct GNUNET_HashCode * key,
943 const struct GNUNET_PeerIdentity *get_path,
944 unsigned int get_path_length,
945 const struct GNUNET_PeerIdentity *put_path,
946 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
947 size_t size, const void *data);
951 * Retrieve the MeshPeer stucture associated with the peer, create one
952 * and insert it in the appropriate structures if the peer is not known yet.
954 * @param peer Full identity of the peer.
956 * @return Existing or newly created peer info.
958 static struct MeshPeer *
959 peer_get (const struct GNUNET_PeerIdentity *peer);
963 * Retrieve the MeshPeer stucture associated with the peer, create one
964 * and insert it in the appropriate structures if the peer is not known yet.
966 * @param peer Short identity of the peer.
968 * @return Existing or newly created peer info.
970 static struct MeshPeer *
971 peer_get_short (const GNUNET_PEER_Id peer);
975 * Build a PeerPath from the paths returned from the DHT, reversing the paths
976 * to obtain a local peer -> destination path and interning the peer ids.
978 * @return Newly allocated and created path
980 static struct MeshPeerPath *
981 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
982 unsigned int get_path_length,
983 const struct GNUNET_PeerIdentity *put_path,
984 unsigned int put_path_length);
988 * Adds a path to the data structs of all the peers in the path
990 * @param p Path to process.
991 * @param confirmed Whether we know if the path works or not.
994 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
998 * Search for a tunnel by global ID using full PeerIdentities.
1000 * @param t Tunnel containing the channel.
1001 * @param chid Public channel number.
1003 * @return channel handler, NULL if doesn't exist
1005 static struct MeshChannel *
1006 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1010 * Change the tunnel state.
1012 * @param t Tunnel whose state to change.
1013 * @param state New state.
1016 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1020 * Notify a tunnel that a connection has broken that affects at least
1021 * some of its peers.
1023 * @param t Tunnel affected.
1024 * @param p1 Peer that got disconnected from p2.
1025 * @param p2 Peer that got disconnected from p1.
1027 * @return Short ID of the peer disconnected (either p1 or p2).
1028 * 0 if the tunnel remained unaffected.
1030 static GNUNET_PEER_Id
1031 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1032 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1035 * @brief Use the given path for the tunnel.
1036 * Update the next and prev hops (and RCs).
1037 * (Re)start the path refresh in case the tunnel is locally owned.
1039 * @param t Tunnel to update.
1040 * @param p Path to use.
1042 * @return Connection created.
1044 static struct MeshConnection *
1045 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1048 * Tunnel is empty: destroy it.
1050 * Notifies all participants (peers, cleints) about the destruction.
1052 * @param t Tunnel to destroy.
1055 tunnel_destroy_empty (struct MeshTunnel2 *t);
1058 * Destroy the tunnel.
1060 * This function does not generate any warning traffic to clients or peers.
1063 * Cancel messages belonging to this tunnel queued to neighbors.
1064 * Free any allocated resources linked to the tunnel.
1066 * @param t The tunnel to destroy.
1069 tunnel_destroy (struct MeshTunnel2 *t);
1072 * Create a connection.
1074 * @param tid Tunnel ID.
1075 * @param cid Connection ID.
1077 static struct MeshConnection *
1078 connection_new (struct GNUNET_HashCode *tid, uint32_t cid);
1081 * Connection is no longer needed: destroy it and remove from tunnel.
1083 * @param c Connection to destroy.
1086 connection_destroy (struct MeshConnection *c);
1089 * Send FWD keepalive packets for a connection.
1091 * @param cls Closure (connection for which to send the keepalive).
1092 * @param tc Notification context.
1095 connection_fwd_keepalive (void *cls,
1096 const struct GNUNET_SCHEDULER_TaskContext *tc);
1099 * Send BCK keepalive packets for a connection.
1101 * @param cls Closure (connection for which to send the keepalive).
1102 * @param tc Notification context.
1105 connection_bck_keepalive (void *cls,
1106 const struct GNUNET_SCHEDULER_TaskContext *tc);
1110 * Change the tunnel state.
1112 * @param c Connection whose state to change.
1113 * @param state New state.
1116 connection_change_state (struct MeshConnection* c,
1117 enum MeshConnectionState state);
1122 * @brief Queue and pass message to core when possible.
1124 * @param cls Closure (@c type dependant). It will be used by queue_send to
1125 * build the message to be sent if not already prebuilt.
1126 * @param type Type of the message, 0 for a raw message.
1127 * @param size Size of the message.
1128 * @param dst Neighbor to send message to.
1129 * @param c Connection this message belongs to, if any.
1130 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1133 queue_add (void *cls, uint16_t type, size_t size,
1134 struct MeshPeer *dst,
1135 struct MeshConnection *c,
1136 struct MeshChannel *ch);
1140 * Free a transmission that was already queued with all resources
1141 * associated to the request.
1143 * @param queue Queue handler to cancel.
1144 * @param clear_cls Is it necessary to free associated cls?
1147 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1151 * Core callback to write a queued packet to core buffer
1153 * @param cls Closure (peer info).
1154 * @param size Number of bytes available in buf.
1155 * @param buf Where the to write the message.
1157 * @return number of bytes written to buf
1160 queue_send (void *cls, size_t size, void *buf);
1164 * Dummy function to separate declarations from definitions in function list.
1167 __mesh_divider______________________________________________________________();
1171 * Get string description for tunnel state.
1173 * @param s Tunnel state.
1175 * @return String representation.
1178 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1180 static char buf[128];
1184 case MESH_TUNNEL_NEW:
1185 return "MESH_TUNNEL_NEW";
1186 case MESH_TUNNEL_SEARCHING:
1187 return "MESH_TUNNEL_SEARCHING";
1188 case MESH_TUNNEL_WAITING:
1189 return "MESH_TUNNEL_WAITING";
1190 case MESH_TUNNEL_READY:
1191 return "MESH_TUNNEL_READY";
1192 case MESH_TUNNEL_RECONNECTING:
1193 return "MESH_TUNNEL_RECONNECTING";
1196 sprintf (buf, "%u (UNKNOWN STATE)", s);
1203 * Get string description for tunnel state.
1205 * @param s Tunnel state.
1207 * @return String representation.
1210 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1214 case MESH_CONNECTION_NEW:
1215 return "MESH_CONNECTION_NEW";
1216 case MESH_CONNECTION_SENT:
1217 return "MESH_CONNECTION_SENT";
1218 case MESH_CONNECTION_READY:
1219 return "MESH_CONNECTION_READY";
1221 return "MESH_CONNECTION_STATE_ERROR";
1227 /******************************************************************************/
1228 /************************ PERIODIC FUNCTIONS ****************************/
1229 /******************************************************************************/
1232 * Periodically announce self id in the DHT
1234 * @param cls closure
1235 * @param tc task context
1238 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1240 struct PBlock block;
1242 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1244 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1248 * - Set data expiration in function of X
1249 * - Adapt X to churn
1251 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1253 block.id = my_full_id;
1254 GNUNET_DHT_put (dht_handle, /* DHT handle */
1255 &my_full_id.hashPubKey, /* Key to use */
1256 dht_replication_level, /* Replication level */
1257 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1258 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1259 sizeof (block), /* Size of the data */
1260 (const char *) &block, /* Data itself */
1261 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1262 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1263 NULL, /* Continuation */
1264 NULL); /* Continuation closure */
1266 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1270 /******************************************************************************/
1271 /****************** GENERAL HELPER FUNCTIONS ************************/
1272 /******************************************************************************/
1276 * Get the static string for a peer ID.
1280 * @return Static string for it's ID.
1283 peer2s (const struct MeshPeer *peer)
1287 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1292 * Get the previous hop in a connection
1294 * @param c Connection.
1296 * @return Previous peer in the connection.
1298 static struct MeshPeer *
1299 connection_get_prev_hop (struct MeshConnection *c)
1303 if (0 == c->own_pos || c->path->length < 2)
1304 id = c->path->peers[0];
1306 id = c->path->peers[c->own_pos - 1];
1308 return peer_get_short (id);
1313 * Get the next hop in a connection
1315 * @param c Connection.
1317 * @return Next peer in the connection.
1319 static struct MeshPeer *
1320 connection_get_next_hop (struct MeshConnection *c)
1324 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1325 id = c->path->peers[c->path->length - 1];
1327 id = c->path->peers[c->own_pos + 1];
1329 return peer_get_short (id);
1334 * Get the hop in a connection.
1336 * @param c Connection.
1337 * @param fwd Next hop?
1339 * @return Next peer in the connection.
1341 static struct MeshPeer *
1342 connection_get_hop (struct MeshConnection *c, int fwd)
1345 return connection_get_next_hop (c);
1346 return connection_get_prev_hop (c);
1350 * Check if client has registered with the service and has not disconnected
1352 * @param client the client to check
1354 * @return non-NULL if client exists in the global DLL
1356 static struct MeshClient *
1357 client_get (struct GNUNET_SERVER_Client *client)
1359 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1364 * Deletes a tunnel from a client (either owner or destination).
1366 * @param c Client whose tunnel to delete.
1367 * @param ch Channel which should be deleted.
1370 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1376 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1378 if (GNUNET_YES != res)
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1383 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1385 if (GNUNET_YES != res)
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1392 * Notify the appropriate client that a new incoming channel was created.
1394 * @param ch Channel that was created.
1397 send_local_channel_create (struct MeshChannel *ch)
1399 struct GNUNET_MESH_ChannelMessage msg;
1400 struct MeshTunnel2 *t = ch->t;
1402 if (NULL == ch->dest)
1404 msg.header.size = htons (sizeof (msg));
1405 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1406 msg.channel_id = htonl (ch->lid_dest);
1407 msg.port = htonl (ch->port);
1409 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1410 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1411 msg.opt = htonl (msg.opt);
1412 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1413 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1414 &msg.header, GNUNET_NO);
1419 * Notify a client that the incoming tunnel is no longer valid.
1421 * @param ch Channel that is destroyed.
1422 * @param fwd Forward notification (owner->dest)?
1425 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1427 struct GNUNET_MESH_ChannelMessage msg;
1428 struct MeshClient *c;
1430 c = fwd ? ch->dest : ch->root;
1436 msg.header.size = htons (sizeof (msg));
1437 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1438 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1439 msg.port = htonl (0);
1440 memset (&msg.peer, 0, sizeof (msg.peer));
1441 msg.opt = htonl (0);
1442 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1443 &msg.header, GNUNET_NO);
1448 * Build a local ACK message and send it to a local client.
1450 * @param ch Channel on which to send the ACK.
1451 * @param c Client to whom send the ACK.
1452 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1455 send_local_ack (struct MeshChannel *ch,
1456 struct MeshClient *c,
1459 struct GNUNET_MESH_LocalAck msg;
1461 msg.header.size = htons (sizeof (msg));
1462 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1463 msg.channel_id = htonl (is_fwd ? ch->lid_root : ch->lid_dest);
1464 GNUNET_SERVER_notification_context_unicast (nc,
1472 * Count established (ready) connections of a tunnel.
1474 * @param t Tunnel on which to send the message.
1476 * @return Number of connections.
1479 tunnel_count_connections (struct MeshTunnel2 *t)
1481 struct MeshConnection *c;
1484 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1491 * Pick a connection on which send the next data message.
1493 * @param t Tunnel on which to send the message.
1494 * @param fwd Is this a fwd message?
1496 * @return The connection on which to send the next message.
1498 static struct MeshConnection *
1499 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1501 struct MeshConnection *c;
1502 struct MeshConnection *best;
1503 struct MeshFlowControl *fc;
1504 unsigned int lowest_q;
1507 lowest_q = UINT_MAX;
1508 for (c = t->connection_head; NULL != c; c = c->next)
1510 if (MESH_CONNECTION_READY == c->state)
1512 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1518 if (fc->queue_n < lowest_q)
1521 lowest_q = fc->queue_n;
1530 * Get the total buffer space for a tunnel.
1533 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1535 struct MeshConnection *c;
1536 struct MeshFlowControl *fc;
1537 unsigned int buffer;
1539 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1541 if (c->state != MESH_CONNECTION_READY)
1544 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1545 buffer += fc->last_ack_recv - fc->last_pid_sent;
1553 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1554 * Encrypt data with the tunnel key.
1556 * @param t Tunnel whose key to use.
1557 * @param dst Destination for the encrypted data.
1558 * @param src Source of the plaintext.
1559 * @param size Size of the plaintext.
1560 * @param iv Initialization Vector to use.
1561 * @param fwd Is this a fwd message?
1564 tunnel_encrypt (struct MeshTunnel2 *t,
1565 void *dst, const void *src,
1566 size_t size, uint64_t iv, int fwd)
1568 memcpy (dst, src, size);
1573 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1574 * Decrypt data with the tunnel key.
1576 * @param t Tunnel whose key to use.
1577 * @param dst Destination for the plaintext.
1578 * @param src Source of the encrypted data.
1579 * @param size Size of the encrypted data.
1580 * @param iv Initialization Vector to use.
1581 * @param fwd Is this a fwd message?
1584 tunnel_decrypt (struct MeshTunnel2 *t,
1585 void *dst, const void *src,
1586 size_t size, uint64_t iv, int fwd)
1588 memcpy (dst, src, size);
1593 * Sends an already built message on a connection, properly registering
1594 * all used resources.
1596 * @param message Message to send. Function makes a copy of it.
1597 * If message is not hop-by-hop, decrements TTL of copy.
1598 * @param c Connection on which this message is transmitted.
1599 * @param ch Channel on which this message is transmitted, or NULL.
1600 * @param fwd Is this a fwd message?
1603 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1604 struct MeshConnection *c,
1605 struct MeshChannel *ch,
1608 struct MeshPeer *neighbor;
1613 neighbor = connection_get_hop (c, fwd);
1614 if (NULL == neighbor)
1620 size = ntohs (message->size);
1621 data = GNUNET_malloc (size);
1622 memcpy (data, message, size);
1623 type = ntohs(message->type);
1625 if (GNUNET_MESSAGE_TYPE_MESH_FWD == type ||
1626 GNUNET_MESSAGE_TYPE_MESH_BCK == type)
1628 struct GNUNET_MESH_Encrypted *msg;
1631 msg = (struct GNUNET_MESH_Encrypted *) data;
1632 ttl = ntohl (msg->ttl);
1635 GNUNET_break_op (0);
1638 msg->ttl = htonl (ttl - 1);
1651 * Sends an already built message on a tunnel, choosing the best connection.
1653 * @param message Message to send. Function modifies it.
1654 * @param t Tunnel on which this message is transmitted.
1655 * @param ch Channel on which this message is transmitted.
1656 * @param fwd Is this a fwd message?
1659 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1660 struct MeshTunnel2 *t,
1661 struct MeshChannel *ch,
1664 struct MeshConnection *c;
1667 c = tunnel_get_connection (t, fwd);
1673 type = ntohs (msg->header.size);
1676 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1677 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1678 msg->cid = htonl (c->id);
1680 msg->ttl = default_ttl;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1684 GNUNET_MESH_DEBUG_M2S (type));
1688 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1693 * Sends an already built message on a channel, properly registering
1694 * all used resources and encrypting the message with the tunnel's key.
1696 * @param message Message to send. Function makes a copy of it.
1697 * @param ch Channel on which this message is transmitted.
1698 * @param fwd Is this a fwd message?
1701 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1702 struct MeshChannel *ch,
1705 struct GNUNET_MESH_Encrypted *msg;
1706 size_t size = ntohs (message->size);
1707 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1711 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1712 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1714 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1715 msg->header.type = htons (type);
1716 msg->header.size = htons (size);
1717 msg->iv = GNUNET_htonll (iv);
1718 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1720 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1725 * Sends a CREATE CONNECTION message for a path to a peer.
1726 * Changes the connection and tunnel states if necessary.
1728 * @param connection Connection to create.
1731 send_connection_create (struct MeshConnection *connection)
1733 struct MeshPeer *neighbor;
1734 struct MeshTunnel2 *t;
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1738 neighbor = connection_get_next_hop (connection);
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to %s\n", peer2s (neighbor));
1740 queue_add (connection,
1741 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1742 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1743 (connection->path->length *
1744 sizeof (struct GNUNET_PeerIdentity)),
1748 if (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state)
1749 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1750 if (MESH_CONNECTION_NEW == connection->state)
1751 connection_change_state (connection, MESH_CONNECTION_SENT);
1756 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1759 * @param connection Connection to confirm.
1762 send_connection_ack (struct MeshConnection *connection)
1764 struct MeshPeer *neighbor;
1765 struct MeshTunnel2 *t;
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1769 neighbor = connection_get_prev_hop (connection);
1770 queue_add (connection,
1771 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1772 sizeof (struct GNUNET_MESH_ConnectionACK),
1776 if (MESH_TUNNEL_NEW == t->state)
1777 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1782 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1784 * @param c Which connection to send the hop-by-hop ACK.
1785 * @param ack Value of the ACK.
1786 * @param fwd Is this fwd?
1789 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1791 struct GNUNET_MESH_ACK msg;
1793 msg.header.size = htons (sizeof (msg));
1794 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1795 msg.ack = htonl (ack);
1797 msg.cid = htonl (c->id);
1799 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1804 * Core callback to write a pre-constructed data packet to core buffer
1806 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1807 * @param size Number of bytes available in buf.
1808 * @param buf Where the to write the message.
1810 * @return number of bytes written to buf
1813 send_core_data_raw (void *cls, size_t size, void *buf)
1815 struct GNUNET_MessageHeader *msg = cls;
1818 GNUNET_assert (NULL != msg);
1819 total_size = ntohs (msg->size);
1821 if (total_size > size)
1826 memcpy (buf, msg, total_size);
1833 * Function to send a create connection message to a peer.
1835 * @param cls closure
1836 * @param size number of bytes available in buf
1837 * @param buf where the callee should write the message
1838 * @return number of bytes written to buf
1841 send_core_connection_create (void *cls, size_t size, void *buf)
1843 struct MeshConnection *c = cls;
1844 struct GNUNET_MESH_ConnectionCreate *msg;
1845 struct GNUNET_PeerIdentity *peer_ptr;
1846 struct MeshPeerPath *p = c->path;
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1852 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1853 p->length * sizeof (struct GNUNET_PeerIdentity);
1855 if (size < size_needed || NULL == buf)
1860 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1861 msg->header.size = htons (size_needed);
1862 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1863 msg->cid = htonl (c->id);
1864 msg->tid = c->t->id;
1866 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1867 for (i = 0; i < p->length; i++)
1869 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1873 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1879 * Creates a path ack message in buf and frees all unused resources.
1881 * @param cls closure (MeshTransmissionDescriptor)
1882 * @param size number of bytes available in buf
1883 * @param buf where the callee should write the message
1884 * @return number of bytes written to buf
1887 send_core_connection_ack (void *cls, size_t size, void *buf)
1889 struct GNUNET_MESH_ConnectionACK *msg = buf;
1890 struct MeshConnection *c = cls;
1891 struct MeshTunnel2 *t = c->t;
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1894 GNUNET_assert (NULL != t);
1895 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1900 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1901 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1902 msg->cid = htonl (c->id);
1905 /* TODO add signature */
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1908 return sizeof (struct GNUNET_MESH_ConnectionACK);
1913 * Destroy the peer_info and free any allocated resources linked to it
1915 * @param peer The peer_info to destroy.
1917 * @return GNUNET_OK on success
1920 peer_destroy (struct MeshPeer *peer)
1922 struct GNUNET_PeerIdentity id;
1923 struct MeshPeerPath *p;
1924 struct MeshPeerPath *nextp;
1926 GNUNET_PEER_resolve (peer->id, &id);
1927 GNUNET_PEER_change_rc (peer->id, -1);
1930 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
1933 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1934 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1936 if (NULL != peer->dhtget)
1938 GNUNET_DHT_get_stop (peer->dhtget);
1940 p = peer->path_head;
1944 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1948 tunnel_destroy_empty (peer->tunnel);
1955 * Returns if peer is used (has a tunnel, is neighbor).
1957 * @peer Peer to check.
1959 * @return GNUNET_YES if peer is in use.
1962 peer_is_used (struct MeshPeer *peer)
1964 struct MeshPeerPath *p;
1966 if (NULL != peer->tunnel)
1969 for (p = peer->path_head; NULL != p; p = p->next)
1978 * Iterator over all the peers to get the oldest timestamp.
1980 * @param cls Closure (unsued).
1981 * @param key ID of the peer.
1982 * @param value Peer_Info of the peer.
1985 peer_get_oldest (void *cls,
1986 const struct GNUNET_HashCode *key,
1989 struct MeshPeer *p = value;
1990 struct GNUNET_TIME_Absolute *abs = cls;
1992 /* Don't count active peers */
1993 if (GNUNET_YES == peer_is_used (p))
1996 if (abs->abs_value_us < p->last_contact.abs_value_us)
1997 abs->abs_value_us = p->last_contact.abs_value_us;
2004 * Iterator over all the peers to remove the oldest entry.
2006 * @param cls Closure (unsued).
2007 * @param key ID of the peer.
2008 * @param value Peer_Info of the peer.
2011 peer_timeout (void *cls,
2012 const struct GNUNET_HashCode *key,
2015 struct MeshPeer *p = value;
2016 struct GNUNET_TIME_Absolute *abs = cls;
2018 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2019 GNUNET_NO == peer_is_used (p))
2029 * Delete oldest unused peer.
2032 peer_delete_oldest (void)
2034 struct GNUNET_TIME_Absolute abs;
2036 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2038 GNUNET_CONTAINER_multihashmap_iterate (peers,
2041 GNUNET_CONTAINER_multihashmap_iterate (peers,
2048 * Retrieve the MeshPeer stucture associated with the peer, create one
2049 * and insert it in the appropriate structures if the peer is not known yet.
2051 * @param peer Full identity of the peer.
2053 * @return Existing or newly created peer info.
2055 static struct MeshPeer *
2056 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2058 struct MeshPeer *peer;
2060 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2063 peer = GNUNET_new (struct MeshPeer);
2064 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2066 peer_delete_oldest ();
2068 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2069 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2070 peer->id = GNUNET_PEER_intern (peer_id);
2072 peer->last_contact = GNUNET_TIME_absolute_get();
2079 * Retrieve the MeshPeer stucture associated with the peer, create one
2080 * and insert it in the appropriate structures if the peer is not known yet.
2082 * @param peer Short identity of the peer.
2084 * @return Existing or newly created peer info.
2086 static struct MeshPeer *
2087 peer_get_short (const GNUNET_PEER_Id peer)
2089 return peer_get (GNUNET_PEER_resolve2 (peer));
2094 * Get a cost of a path for a peer considering existing tunnel connections.
2096 * @param peer Peer towards which the path is considered.
2097 * @param path Candidate path.
2099 * @return Cost of the path (path length + number of overlapping nodes)
2102 peer_get_path_cost (const struct MeshPeer *peer,
2103 const struct MeshPeerPath *path)
2105 struct MeshConnection *c;
2106 unsigned int overlap;
2114 GNUNET_assert (NULL != peer->tunnel);
2116 for (i = 0; i < path->length; i++)
2118 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2120 for (j = 0; j < c->path->length; j++)
2122 if (path->peers[i] == c->path->peers[j])
2130 return (path->length + overlap) * (path->score * -1);
2135 * Choose the best path towards a peer considering the tunnel properties.
2137 * @param peer The destination peer.
2139 * @return Best current known path towards the peer, if any.
2141 static struct MeshPeerPath *
2142 peer_get_best_path (const struct MeshPeer *peer)
2144 struct MeshPeerPath *best_p;
2145 struct MeshPeerPath *p;
2146 struct MeshConnection *c;
2147 unsigned int best_cost;
2150 best_cost = UINT_MAX;
2152 for (p = peer->path_head; NULL != p; p = p->next)
2154 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2158 continue; /* If path is in use in a connection, skip it. */
2160 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2170 queue_is_sendable (struct MeshPeerQueue *q)
2172 struct MeshFlowControl *fc;
2174 /* Is PID-independent? */
2177 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2178 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2182 /* Is PID allowed? */
2183 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2184 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2192 * Get first sendable message.
2194 * @param peer The destination peer.
2196 * @return Best current known path towards the peer, if any.
2198 static struct MeshPeerQueue *
2199 peer_get_first_message (const struct MeshPeer *peer)
2201 struct MeshPeerQueue *q;
2203 for (q = peer->queue_head; NULL != q; q = q->next)
2205 if (queue_is_sendable (q))
2214 * Try to establish a new connection to this peer in the given tunnel.
2215 * If the peer doesn't have any path to it yet, try to get one.
2216 * If the peer already has some path, send a CREATE CONNECTION towards it.
2218 * @param peer PeerInfo of the peer.
2221 peer_connect (struct MeshPeer *peer)
2223 struct MeshTunnel2 *t;
2224 struct MeshPeerPath *p;
2225 struct MeshConnection *c;
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2228 "peer_connect towards %s\n",
2231 if (NULL != peer->path_head)
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2234 p = peer_get_best_path (peer);
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2238 c = tunnel_use_path (t, p);
2239 send_connection_create (c);
2242 else if (NULL == peer->dhtget)
2244 const struct GNUNET_PeerIdentity *id;
2246 id = GNUNET_PEER_resolve2 (peer->id);
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 " Starting DHT GET for peer %s\n", peer2s (peer));
2249 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2250 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2251 &id->hashPubKey, /* key to search */
2252 dht_replication_level, /* replication level */
2253 GNUNET_DHT_RO_RECORD_ROUTE |
2254 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2256 0, /* xquery bits */
2257 &dht_get_id_handler, peer);
2258 if (MESH_TUNNEL_NEW == t->state)
2259 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "There is no path but the DHT GET is already started.\n");
2270 * Get the first transmittable message for a connection.
2272 * @param c Connection.
2273 * @param fwd Is this FWD?
2275 * @return First transmittable message.
2277 static struct MeshPeerQueue *
2278 connection_get_first_message (struct MeshConnection *c, int fwd)
2280 struct MeshPeerQueue *q;
2283 p = connection_get_hop (c, fwd);
2285 for (q = p->queue_head; NULL != q; q = q->next)
2289 if (queue_is_sendable (q))
2298 * Is this peer the last one on the connection?
2300 * @param c Connection.
2301 * @param fwd Is this about fwd traffic?
2302 * Note that the ROOT is the terminal for BCK traffic!
2304 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2307 connection_is_terminal (struct MeshConnection *c, int fwd)
2309 if (fwd && c->own_pos == c->path->length - 1)
2311 if (!fwd && c->own_pos == 0)
2318 * @brief Re-initiate traffic on this connection if necessary.
2320 * Check if there is traffic queued towards this peer
2321 * and the core transmit handle is NULL (traffic was stalled).
2322 * If so, call core tmt rdy.
2324 * @param c Connection on which initiate traffic.
2325 * @param fwd Is this about fwd traffic?
2328 connection_unlock_queue (struct MeshConnection *c, int fwd)
2330 struct MeshPeer *peer;
2331 struct MeshPeerQueue *q;
2334 peer = connection_get_hop (c, fwd);
2336 if (NULL != peer->core_transmit)
2337 return; /* Already unlocked */
2339 q = connection_get_first_message (c, fwd);
2341 return; /* Nothing to transmit */
2344 peer->core_transmit =
2345 GNUNET_CORE_notify_transmit_ready (core_handle,
2348 GNUNET_TIME_UNIT_FOREVER_REL,
2349 GNUNET_PEER_resolve2 (peer->id),
2357 * Cancel all transmissions that belong to a certain connection.
2359 * @param c Connection which to cancel.
2360 * @param fwd Cancel fwd traffic?
2363 connection_cancel_queues (struct MeshConnection *c, int fwd)
2365 struct MeshPeerQueue *q;
2366 struct MeshPeerQueue *next;
2367 struct MeshFlowControl *fc;
2368 struct MeshPeer *peer;
2375 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2376 peer = connection_get_hop (c, fwd);
2378 for (q = peer->queue_head; NULL != q; q = next)
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2384 "connection_cancel_queue %s\n",
2385 GNUNET_MESH_DEBUG_M2S (q->type));
2386 queue_destroy (q, GNUNET_YES);
2389 if (NULL == peer->queue_head)
2391 if (NULL != peer->core_transmit)
2393 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2394 peer->core_transmit = NULL;
2396 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2398 GNUNET_SCHEDULER_cancel (fc->poll_task);
2399 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2406 * Remove all paths that rely on a direct connection between p1 and p2
2407 * from the peer itself and notify all tunnels about it.
2409 * @param peer PeerInfo of affected peer.
2410 * @param p1 GNUNET_PEER_Id of one peer.
2411 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2414 * TODO: optimize (see below)
2417 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2420 struct MeshPeerPath *p;
2421 struct MeshPeerPath *next;
2422 struct MeshPeer *peer_d;
2424 unsigned int destroyed;
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2429 for (p = peer->path_head; NULL != p; p = next)
2432 for (i = 0; i < (p->length - 1); i++)
2434 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2435 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2437 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2448 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2450 peer_d = peer_get_short (d); // FIXME
2451 next = peer_get_best_path (peer_d);
2452 tunnel_use_path (peer->tunnel, next);
2453 peer_connect (peer_d);
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2460 * Add the path to the peer and update the path used to reach it in case this
2463 * @param peer_info Destination peer to add the path to.
2464 * @param path New path to add. Last peer must be the peer in arg 1.
2465 * Path will be either used of freed if already known.
2466 * @param trusted Do we trust that this path is real?
2469 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2472 struct MeshPeerPath *aux;
2476 if ((NULL == peer_info) || (NULL == path))
2479 path_destroy (path);
2482 if (path->peers[path->length - 1] != peer_info->id)
2485 path_destroy (path);
2488 if (2 >= path->length && GNUNET_NO == trusted)
2490 /* Only allow CORE to tell us about direct paths */
2491 path_destroy (path);
2494 for (l = 1; l < path->length; l++)
2496 if (path->peers[l] == myid)
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2499 for (l2 = 0; l2 < path->length - l; l2++)
2501 path->peers[l2] = path->peers[l + l2];
2506 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2511 path->length, peer2s (peer_info));
2513 l = path_get_length (path);
2516 path_destroy (path);
2520 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2521 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2523 l2 = path_get_length (aux);
2526 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2527 peer_info->path_tail, aux, path);
2532 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2534 path_destroy (path);
2539 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2546 * Add the path to the origin peer and update the path used to reach it in case
2547 * this is the shortest.
2548 * The path is given in peer_info -> destination, therefore we turn the path
2549 * upside down first.
2551 * @param peer_info Peer to add the path to, being the origin of the path.
2552 * @param path New path to add after being inversed.
2553 * Path will be either used or freed.
2554 * @param trusted Do we trust that this path is real?
2557 peer_add_path_to_origin (struct MeshPeer *peer_info,
2558 struct MeshPeerPath *path, int trusted)
2561 peer_add_path (peer_info, path, trusted);
2567 * Function called if a connection has been stalled for a while,
2568 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2570 * @param cls Closure (poll ctx).
2571 * @param tc TaskContext.
2574 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2576 struct MeshFlowControl *fc = cls;
2577 struct GNUNET_MESH_Poll msg;
2578 struct MeshConnection *c;
2580 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2589 peer2s (c->t->peer), c->id);
2591 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2592 msg.header.size = htons (sizeof (msg));
2593 msg.pid = htonl (fc->last_pid_sent);
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2595 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2596 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2597 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2598 &connection_poll, fc);
2603 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2604 * to obtain a local peer -> destination path and interning the peer ids.
2606 * @return Newly allocated and created path
2608 static struct MeshPeerPath *
2609 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2610 unsigned int get_path_length,
2611 const struct GNUNET_PeerIdentity *put_path,
2612 unsigned int put_path_length)
2614 struct MeshPeerPath *p;
2620 GNUNET_PEER_change_rc (myid, 1);
2621 i = get_path_length;
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2623 for (i--; i >= 0; i--)
2625 id = GNUNET_PEER_intern (&get_path[i]);
2626 if (p->length > 0 && id == p->peers[p->length - 1])
2628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2629 GNUNET_PEER_change_rc (id, -1);
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2634 GNUNET_i2s (&get_path[i]));
2636 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2637 p->peers[p->length - 1] = id;
2640 i = put_path_length;
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2642 for (i--; i >= 0; i--)
2644 id = GNUNET_PEER_intern (&put_path[i]);
2647 /* PUT path went through us, so discard the path up until now and start
2648 * from here to get a much shorter (and loop-free) path.
2653 if (p->length > 0 && id == p->peers[p->length - 1])
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2656 GNUNET_PEER_change_rc (id, -1);
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2661 GNUNET_i2s (&put_path[i]));
2663 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2664 p->peers[p->length - 1] = id;
2668 if (get_path_length > 0)
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2670 GNUNET_i2s (&get_path[0]));
2671 if (put_path_length > 0)
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2673 GNUNET_i2s (&put_path[0]));
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2676 for (i = 0; i < p->length; i++)
2678 struct GNUNET_PeerIdentity peer_id;
2680 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2682 GNUNET_i2s (&peer_id));
2690 * Adds a path to the peer_infos of all the peers in the path
2692 * @param p Path to process.
2693 * @param confirmed Whether we know if the path works or not.
2696 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2700 /* TODO: invert and add */
2701 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2702 for (i++; i < p->length; i++)
2704 struct MeshPeer *aux;
2705 struct MeshPeerPath *copy;
2707 aux = peer_get_short (p->peers[i]);
2708 copy = path_duplicate (p);
2709 copy->length = i + 1;
2710 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2716 * Search for a channel among the channels for a client
2718 * @param c the client whose channels to search in
2719 * @param chid the local id of the channel
2721 * @return channel handler, NULL if doesn't exist
2723 static struct MeshChannel *
2724 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2726 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2728 GNUNET_break_op (0);
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2732 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2733 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2734 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2738 * Search for a tunnel by global ID using full PeerIdentities.
2740 * @param t Tunnel containing the channel.
2741 * @param chid Public channel number.
2743 * @return channel handler, NULL if doesn't exist
2745 static struct MeshChannel *
2746 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2748 struct MeshChannel *ch;
2753 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2755 if (ch->gid == chid)
2764 * Change the tunnel state.
2766 * @param t Tunnel whose state to change.
2767 * @param state New state.
2770 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "Tunnel %s state was %s\n",
2775 GNUNET_MESH_DEBUG_TS2S (t->state));
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2777 "Tunnel %s state is now %s\n",
2779 GNUNET_MESH_DEBUG_TS2S (state));
2785 * Cache a message to be sent once tunnel is online.
2787 * @param t Tunnel to hold the message.
2788 * @param ch Channel the message is about.
2789 * @param msg Message itself (copy will be made).
2790 * @param fwd Is this fwd?
2793 tunnel_queue_data (struct MeshTunnel2 *t,
2794 struct MeshChannel *ch,
2795 struct GNUNET_MessageHeader *msg,
2798 struct MeshTunnelQueue *tq;
2799 uint16_t size = ntohs (msg->size);
2801 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2804 memcpy (&tq[1], msg, size);
2805 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2810 * Send all cached messages that we can, tunnel is online.
2812 * @param t Tunnel that holds the messages.
2813 * @param fwd Is this fwd?
2816 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2818 struct MeshTunnelQueue *tq;
2819 struct MeshTunnelQueue *next;
2822 room = tunnel_get_buffer (t, fwd);
2823 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2827 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2828 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2837 connection_change_state (struct MeshConnection* c,
2838 enum MeshConnectionState state)
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2841 "Connection %s[%X] state was %s\n",
2842 peer2s (c->t->peer), c->id,
2843 GNUNET_MESH_DEBUG_CS2S (c->state));
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 "Connection %s[%X] state is now %s\n",
2846 peer2s (c->t->peer), c->id,
2847 GNUNET_MESH_DEBUG_CS2S (state));
2853 * Add a client to a channel, initializing all needed data structures.
2855 * @param ch Channel to which add the client.
2856 * @param c Client which to add to the channel.
2859 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2861 struct MeshTunnel2 *t = ch->t;
2863 if (NULL != ch->dest)
2869 /* Assign local id as destination */
2870 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2871 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2872 ch->lid_dest = t->next_local_chid++;
2873 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2875 /* Store in client's hashmap */
2877 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2888 static struct MeshConnection *
2889 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2891 struct MeshConnection *c;
2892 unsigned int own_pos;
2894 c = connection_new (&t->id, t->next_cid++);
2895 for (own_pos = 0; own_pos < p->length; own_pos++)
2897 if (p->peers[own_pos] == myid)
2900 if (own_pos > p->length - 1)
2903 connection_destroy (c);
2906 c->own_pos = own_pos;
2911 c->fwd_maintenance_task =
2912 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
2913 &connection_fwd_keepalive, c);
2920 * Notifies a tunnel that a connection has broken that affects at least
2921 * some of its peers. Sends a notification towards the root of the tree.
2922 * In case the peer is the owner of the tree, notifies the client that owns
2923 * the tunnel and tries to reconnect.
2925 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
2927 * @param t Tunnel affected.
2928 * @param p1 Peer that got disconnected from p2.
2929 * @param p2 Peer that got disconnected from p1.
2931 * @return Short ID of the peer disconnected (either p1 or p2).
2932 * 0 if the tunnel remained unaffected.
2934 static GNUNET_PEER_Id
2935 tunnel_notify_connection_broken (struct MeshTunnel2* t,
2936 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
2938 // if (myid != p1 && myid != p2) FIXME
2943 // if (tree_get_predecessor (t->tree) != 0)
2945 // /* We are the peer still connected, notify owner of the disconnection. */
2946 // struct GNUNET_MESH_PathBroken msg;
2947 // struct GNUNET_PeerIdentity neighbor;
2949 // msg.header.size = htons (sizeof (msg));
2950 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2951 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2952 // msg.tid = htonl (t->id.tid);
2953 // msg.peer1 = my_full_id;
2954 // GNUNET_PEER_resolve (pid, &msg.peer2);
2955 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2956 // send_prebuilt_message (&msg.header, &neighbor, t);
2963 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2965 * @param ch Channel this is about.
2966 * @param fwd Is for FWD traffic? (ACK dest->owner)
2969 channel_send_data_ack (struct MeshChannel *ch, int fwd)
2971 struct GNUNET_MESH_DataACK msg;
2972 struct MeshChannelReliability *rel;
2973 struct MeshReliableMessage *copy;
2978 if (GNUNET_NO == ch->reliable)
2983 rel = fwd ? ch->bck_rel : ch->fwd_rel;
2984 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2986 "send_data_ack for %u\n",
2989 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2990 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2991 msg.header.size = htons (sizeof (msg));
2992 msg.chid = htonl (ch->gid);
2993 msg.mid = htonl (*mid - 1);
2995 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2997 delta = copy->mid - *mid;
3000 mask = 0x1LL << delta;
3001 msg.futures |= mask;
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3003 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3004 copy->mid, delta, mask, msg.futures);
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3008 send_prebuilt_message_channel (&msg.header, ch, fwd);
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3014 * Send an ACK informing the predecessor about the available buffer space.
3016 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3017 * the ACK itself goes "back" (dest->root).
3019 * @param c Connection on which to send the ACK.
3020 * @param fwd Is this FWD ACK? (Going dest->owner)
3023 connection_send_ack (struct MeshConnection *c, int fwd)
3025 struct MeshFlowControl *next_fc;
3026 struct MeshFlowControl *prev_fc;
3030 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3031 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3033 /* Check if we need to transmit the ACK */
3034 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 " last pid recv: %u, last ack sent: %u\n",
3039 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3043 /* Ok, ACK might be necessary, what PID to ACK? */
3044 delta = next_fc->queue_max - next_fc->queue_n;
3045 ack = prev_fc->last_pid_recv + delta;
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 " last pid %u, last ack %u, qmax %u, q %u\n",
3049 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3050 next_fc->queue_max, next_fc->queue_n);
3051 if (ack == prev_fc->last_ack_sent)
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3057 prev_fc->last_ack_sent = ack;
3058 send_ack (c, ack, fwd);
3063 * Send an ACK informing the client about available buffer space.
3065 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3066 * the ACK itself goes "back" (towards root).
3068 * @param ch Channel on which to send the ACK, NULL if unknown.
3069 * @param fwd Is this FWD ACK? (Going dest->owner)
3072 // channel_send_ack (struct MeshChannel *ch, uint16_t type, int fwd)
3074 // struct MeshChannelReliability *rel;
3075 // struct MeshFlowControl *next_fc;
3076 // struct MeshFlowControl *prev_fc;
3077 // struct MeshClient *c;
3078 // struct MeshClient *o;
3079 // GNUNET_PEER_Id hop;
3080 // uint32_t delta_mid;
3084 // rel = fwd ? ch->fwd_rel : ch->bck_rel;
3085 // c = fwd ? ch->client : ch->owner;
3086 // o = fwd ? ch->owner : ch->client;
3087 // hop = fwd ? connection_get_prev_hop (cn) : connection_get_next_hop (cn);
3088 // next_fc = fwd ? &t->next_fc : &t->prev_fc;
3089 // prev_fc = fwd ? &t->prev_fc : &t->next_fc;
3093 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3094 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3095 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3096 // "ACK due to %s\n",
3097 // GNUNET_MESH_DEBUG_M2S (type));
3098 // if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
3100 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3103 // if (GNUNET_YES == t->reliable && NULL != c)
3104 // tunnel_send_data_ack (t, fwd);
3106 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3107 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3108 // case GNUNET_MESSAGE_TYPE_MESH_ACK:
3109 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3111 // case GNUNET_MESSAGE_TYPE_MESH_POLL:
3112 // case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
3113 // t->force_ack = GNUNET_YES;
3116 // GNUNET_break (0);
3119 // /* Check if we need to transmit the ACK */
3121 // prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
3122 // GNUNET_NO == t->force_ack)
3124 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3125 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3126 // " last pid recv: %u, last ack sent: %u\n",
3127 // prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3131 // /* Ok, ACK might be necessary, what PID to ACK? */
3132 // delta = t->queue_max - next_fc->queue_n;
3133 // if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
3134 // delta_mid = rel->mid_sent - rel->head_sent->mid;
3137 // if (0 > delta || (GNUNET_YES == t->reliable &&
3139 // (10 < rel->n_sent || 64 <= delta_mid)))
3141 // if (NULL != o && delta > 1)
3143 // ack = prev_fc->last_pid_recv + delta;
3144 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3145 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3146 // " last pid %u, last ack %u, qmax %u, q %u\n",
3147 // prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3148 // t->queue_max, next_fc->queue_n);
3149 // if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
3151 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3155 // prev_fc->last_ack_sent = ack;
3157 // send_local_ack (t, o, fwd);
3158 // else if (0 != hop)
3159 // send_ack (t, hop, ack);
3161 // GNUNET_break (GNUNET_YES == t->destroy);
3162 // t->force_ack = GNUNET_NO;
3167 * Modify the mesh message TID from global to local and send to client.
3169 * @param ch Channel on which to send the message.
3170 * @param msg Message to modify and send.
3171 * @param c Client to send to.
3172 * @param tid Tunnel ID to use (c can be both owner and client).
3175 channel_send_client_to_tid (struct MeshChannel *ch,
3176 const struct GNUNET_MESH_Data *msg,
3177 struct MeshClient *c, MESH_ChannelNumber id)
3179 struct GNUNET_MESH_LocalData *copy;
3180 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3181 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3183 if (size < sizeof (struct GNUNET_MessageHeader))
3185 GNUNET_break_op (0);
3193 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3194 memcpy (©[1], &msg[1], size);
3195 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3196 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3197 copy->id = htonl (id);
3198 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3199 ©->header, GNUNET_NO);
3203 * Modify the data message ID from global to local and send to client.
3205 * @param ch Channel on which to send the message.
3206 * @param msg Message to modify and send.
3207 * @param fwd Forward?
3210 channel_send_client_data (struct MeshChannel *ch,
3211 const struct GNUNET_MESH_Data *msg,
3215 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3217 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3222 * Send a buffered message to the client, for in order delivery or
3223 * as result of client ACK.
3225 * @param ch Channel on which to empty the message buffer.
3226 * @param c Client to send to.
3227 * @param rel Reliability structure to corresponding peer.
3228 * If rel == bck_rel, this is FWD data.
3231 channel_send_client_buffered_data (struct MeshChannel *ch,
3232 struct MeshClient *c,
3233 struct MeshChannelReliability *rel)
3235 struct MeshReliableMessage *copy;
3238 if (GNUNET_NO == rel->client_ready)
3240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3244 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3247 copy = rel->head_recv;
3250 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3252 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 " have %u! now expecting %u\n",
3256 copy->mid, *mid + 1);
3257 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3260 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 " reliable && don't have %u, next is %u\n",
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3277 * We have received a message out of order, or the client is not ready.
3278 * Buffer it until we receive an ACK from the client or the missing
3279 * message from the channel.
3281 * @param msg Message to buffer.
3282 * @param rel Reliability data to the corresponding direction.
3285 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3286 struct MeshChannelReliability *rel)
3288 struct MeshReliableMessage *copy;
3289 struct MeshReliableMessage *prev;
3293 size = ntohs (msg->header.size);
3294 mid = ntohl (msg->mid);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3297 copy = GNUNET_malloc (sizeof (*copy) + size);
3300 memcpy (©[1], msg, size);
3304 // FIXME do something better than O(n), although n < 64...
3305 // FIXME start from the end (most messages are the latest ones)
3306 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3309 if (GMC_is_pid_bigger (prev->mid, mid))
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3312 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3318 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3324 * Destroy a reliable message after it has been acknowledged, either by
3325 * direct mid ACK or bitfield. Updates the appropriate data structures and
3326 * timers and frees all memory.
3328 * @param copy Message that is no longer needed: remote peer got it.
3331 rel_message_free (struct MeshReliableMessage *copy)
3333 struct MeshChannelReliability *rel;
3334 struct GNUNET_TIME_Relative time;
3337 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3338 rel->expected_delay.rel_value_us *= 7;
3339 rel->expected_delay.rel_value_us += time.rel_value_us;
3340 rel->expected_delay.rel_value_us /= 8;
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3345 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3347 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3349 rel->retry_timer = rel->expected_delay;
3350 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3356 * Destroy all reliable messages queued for a channel,
3357 * during a channel destruction.
3358 * Frees the reliability structure itself.
3360 * @param rel Reliability data for a channel.
3363 channel_rel_free_all (struct MeshChannelReliability *rel)
3365 struct MeshReliableMessage *copy;
3366 struct MeshReliableMessage *next;
3371 for (copy = rel->head_recv; NULL != copy; copy = next)
3374 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3377 for (copy = rel->head_sent; NULL != copy; copy = next)
3380 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3383 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3384 GNUNET_SCHEDULER_cancel (rel->retry_task);
3390 * Mark future messages as ACK'd.
3392 * @param rel Reliability data.
3393 * @param msg DataACK message with a bitfield of future ACK'd messages.
3396 channel_rel_free_sent (struct MeshChannelReliability *rel,
3397 const struct GNUNET_MESH_DataACK *msg)
3399 struct MeshReliableMessage *copy;
3400 struct MeshReliableMessage *next;
3407 bitfield = msg->futures;
3408 mid = ntohl (msg->mid);
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3410 "free_sent_reliable %u %llX\n",
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3413 " rel %p, head %p\n",
3414 rel, rel->head_sent);
3415 for (i = 0, copy = rel->head_sent;
3416 i < 64 && NULL != copy && 0 != bitfield;
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3420 " trying bit %u (mid %u)\n",
3423 if (0 == (bitfield & mask))
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3427 /* Bit was set, clear the bit from the bitfield */
3430 /* The i-th bit was set. Do we have that copy? */
3431 /* Skip copies with mid < target */
3432 target = mid + i + 1;
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3434 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3437 /* Did we run out of copies? (previously freed, it's ok) */
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3444 /* Did we overshoot the target? (previously freed, it's ok) */
3445 if (GMC_is_pid_bigger (copy->mid, target))
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3451 /* Now copy->mid == target, free it */
3453 rel_message_free (copy);
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3461 * We haven't received an ACK after a certain time: restransmit the message.
3463 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3464 * @param tc TaskContext.
3467 channel_retransmit_message (void *cls,
3468 const struct GNUNET_SCHEDULER_TaskContext *tc)
3470 struct MeshChannelReliability *rel = cls;
3471 struct MeshReliableMessage *copy;
3472 struct MeshPeerQueue *q;
3473 struct MeshChannel *ch;
3474 struct MeshConnection *c;
3475 struct GNUNET_MESH_Data *payload;
3476 struct MeshPeer *hop;
3479 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3480 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3484 copy = rel->head_sent;
3491 /* Search the message to be retransmitted in the outgoing queue.
3492 * Check only the queue for the connection that is going to be used,
3493 * if the message is stuck in some other connection's queue we shouldn't
3495 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3496 * the old connection could be temporary stalled or the queue happened to
3497 * be long at time of insertion.
3498 * - not sending the new one could cause terrible delays the old connection
3501 payload = (struct GNUNET_MESH_Data *) ©[1];
3502 fwd = (rel == ch->fwd_rel);
3503 c = tunnel_get_connection (ch->t, fwd);
3504 hop = connection_get_hop (c, fwd);
3505 for (q = hop->queue_head; NULL != q; q = q->next)
3507 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3509 struct GNUNET_MESH_Data *queued_data = q->cls;
3511 if (queued_data->mid == payload->mid)
3516 /* Message not found in the queue that we are going to use. */
3519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3521 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3522 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3529 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3530 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3531 &channel_retransmit_message,
3538 * Send ACK on one or more connections due to buffer space to the client.
3541 channel_send_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3543 struct MeshTunnel2 *t = ch->t;
3544 struct MeshConnection *c;
3545 struct MeshFlowControl *fc;
3550 /* Count connections, how many messages are already allowed */
3551 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3553 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3554 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3559 allowed += fc->last_ack_sent - fc->last_pid_recv;
3563 /* Make sure there is no overflow */
3564 if (allowed > buffer)
3570 /* Authorize connections to send more data */
3571 to_allow = buffer - allowed;
3572 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3574 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3575 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3579 send_ack (c, fc->last_ack_sent + 1, fwd);
3583 GNUNET_break (to_allow == 0);
3588 * Send keepalive packets for a connection.
3590 * @param c Connection to keep alive..
3591 * @param fwd Is this a FWD keepalive? (owner -> dest).
3594 connection_keepalive (struct MeshConnection *c, int fwd)
3596 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3597 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3601 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3602 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3605 "sending %s keepalive for connection %s[%d]\n",
3606 fwd ? "FWD" : "BCK",
3607 peer2s (c->t->peer),
3610 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3611 msg->header.size = htons (size);
3612 msg->header.type = htons (type);
3613 msg->cid = htonl (c->id);
3614 msg->tid = c->t->id;
3616 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3621 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3623 * @param c Connection for which to send the message.
3624 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3627 connection_recreate (struct MeshConnection *c, int fwd)
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3631 send_connection_create (c);
3633 send_connection_ack (c);
3638 * Generic connection timer management.
3639 * Depending on the role of the peer in the connection will send the
3640 * appropriate message (build or keepalive)
3642 * @param c Conncetion to maintain.
3643 * @param fwd Is FWD?
3646 connection_maintain (struct MeshConnection *c, int fwd)
3648 if (MESH_TUNNEL_SEARCHING == c->t->state)
3650 /* TODO DHT GET with RO_BART */
3655 case MESH_CONNECTION_NEW:
3657 case MESH_CONNECTION_SENT:
3658 connection_recreate (c, fwd);
3660 case MESH_CONNECTION_READY:
3661 connection_keepalive (c, fwd);
3670 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3672 struct MeshConnection *c = cls;
3674 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3675 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3678 connection_maintain (c, GNUNET_YES);
3679 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3680 &connection_fwd_keepalive,
3686 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3688 struct MeshConnection *c = cls;
3690 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3691 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3694 connection_maintain (c, GNUNET_NO);
3695 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3696 &connection_bck_keepalive,
3702 * Send a message to all peers in this connection that the connection
3703 * is no longer valid.
3705 * If some peer should not receive the message, it should be zero'ed out
3706 * before calling this function.
3708 * @param c The connection whose peers to notify.
3711 connection_send_destroy (struct MeshConnection *c)
3713 struct GNUNET_MESH_ConnectionDestroy msg;
3715 msg.header.size = htons (sizeof (msg));
3716 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3717 msg.cid = htonl (c->id);
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3720 " sending connection destroy for connection %s[%X]\n",
3721 peer2s (c->t->peer),
3724 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3725 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3726 c->destroy = GNUNET_YES;
3731 * Send a message to all clients (local and remote) of this channel
3732 * notifying that the channel is no longer valid.
3734 * If some peer or client should not receive the message,
3735 * should be zero'ed out before calling this function.
3737 * @param ch The channel whose clients to notify.
3740 channel_send_destroy (struct MeshChannel *ch)
3742 struct GNUNET_MESH_ChannelDestroy msg;
3744 msg.header.size = htons (sizeof (msg));
3745 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3747 " sending channel destroy for channel %s:%X\n",
3748 peer2s (ch->t->peer),
3751 if (NULL != ch->root)
3753 msg.chid = htonl (ch->lid_root);
3754 send_local_channel_destroy (ch, GNUNET_NO);
3758 msg.chid = htonl (ch->gid);
3759 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3762 if (NULL != ch->dest)
3764 msg.chid = htonl (ch->lid_dest);
3765 send_local_channel_destroy (ch, GNUNET_YES);
3769 msg.chid = htonl (ch->gid);
3770 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3778 * @param tid Tunnel ID.
3780 static struct MeshTunnel2 *
3781 tunnel_new (const struct GNUNET_HashCode *tid)
3783 struct MeshTunnel2 *t;
3785 t = GNUNET_new (struct MeshTunnel2);
3787 t->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3789 GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3790 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3804 * @param tid Tunnel ID.
3806 static struct MeshTunnel2 *
3807 tunnel_get (const struct GNUNET_HashCode *tid)
3809 return GNUNET_CONTAINER_multihashmap_get (tunnels, tid);
3814 * Add a connection to a tunnel.
3817 * @param c Connection.
3820 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3823 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3828 * Initialize a Flow Control structure to the initial state.
3830 * @param fc Flow Control structure to initialize.
3833 fc_init (struct MeshFlowControl *fc)
3835 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3836 fc->last_pid_recv = (uint32_t) -1;
3837 fc->last_ack_sent = (uint32_t) 0;
3838 fc->last_ack_recv = (uint32_t) 0;
3839 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3840 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3842 fc->queue_max = (max_msgs_queue / max_connections) + 1;
3846 static struct MeshConnection *
3847 connection_new (struct GNUNET_HashCode *tid, uint32_t cid)
3849 struct MeshConnection *c;
3850 struct MeshTunnel2 *t;
3852 t = tunnel_get (tid);
3855 t = tunnel_new (tid);
3863 c = GNUNET_new (struct MeshConnection);
3865 fc_init (&c->fwd_fc);
3866 fc_init (&c->bck_fc);
3867 tunnel_add_connection (t, c);
3874 * Find a connection.
3876 * @param tid Tunnel ID.
3877 * @param cid Connection ID.
3879 static struct MeshConnection *
3880 connection_get (const struct GNUNET_HashCode *tid, uint32_t cid)
3882 struct MeshConnection *c;
3883 struct MeshTunnel2 *t;
3885 t = tunnel_get (tid);
3888 for (c = t->connection_head; NULL != c; c = c->next)
3897 connection_destroy (struct MeshConnection *c)
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
3903 peer2s (c->t->peer),
3906 connection_cancel_queues (c, GNUNET_YES);
3907 connection_cancel_queues (c, GNUNET_NO);
3909 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
3910 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3911 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
3912 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3914 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
3916 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
3922 tunnel_destroy (struct MeshTunnel2 *t)
3924 struct MeshConnection *c;
3925 struct MeshConnection *next;
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
3933 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
3936 for (c = t->connection_head; NULL != c; c = next)
3939 connection_destroy (c);
3942 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3949 * Tunnel is empty: destroy it.
3951 * Notifies all connections about the destruction.
3953 * @param t Tunnel to destroy.
3956 tunnel_destroy_empty (struct MeshTunnel2 *t)
3958 struct MeshConnection *c;
3960 for (c = t->connection_head; NULL != c; c = c->next)
3962 if (GNUNET_NO == c->destroy)
3963 connection_send_destroy (c);
3966 if (0 == t->pending_messages)
3969 t->destroy = GNUNET_YES;
3974 * Destroy tunnel if empty (no more channels).
3976 * @param t Tunnel to destroy if empty.
3979 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
3981 if (NULL != t->channel_head)
3984 tunnel_destroy_empty (t);
3989 * Destroy a channel and free all resources.
3991 * @param ch Channel to destroy.
3994 channel_destroy (struct MeshChannel *ch)
3996 struct MeshClient *c;
4004 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4015 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4022 if (GNUNET_YES == ch->reliable)
4024 channel_rel_free_all (ch->fwd_rel);
4025 channel_rel_free_all (ch->bck_rel);
4028 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4029 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4035 * Create a new channel.
4037 * @param t Tunnel this channel is in.
4038 * @param owner Client that owns the channel, NULL for foreign channels.
4039 * @param lid_root Local ID for root client.
4041 * @return A new initialized channel. NULL on error.
4043 static struct MeshChannel *
4044 channel_new (struct MeshTunnel2 *t,
4045 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4047 struct MeshChannel *ch;
4049 ch = GNUNET_new (struct MeshChannel);
4051 ch->lid_root = lid_root;
4054 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4056 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4060 while (NULL != channel_get_by_local_id (owner, t->next_chid))
4061 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4062 ch->gid = t->next_chid;
4063 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4066 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4067 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4070 channel_destroy (ch);
4071 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4081 * Set options in a channel, extracted from a bit flag field
4083 * @param ch Channel to set options to.
4084 * @param options Bit array in host byte order.
4087 channel_set_options (struct MeshChannel *ch, uint32_t options)
4089 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4090 GNUNET_YES : GNUNET_NO;
4091 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4092 GNUNET_YES : GNUNET_NO;
4097 * Iterator for deleting each channel whose client endpoint disconnected.
4099 * @param cls Closure (client that has disconnected).
4100 * @param key The local channel id (used to access the hashmap).
4101 * @param value The value stored at the key (channel to destroy).
4103 * @return GNUNET_OK, keep iterating.
4106 channel_destroy_iterator (void *cls,
4110 struct MeshChannel *ch = value;
4111 struct MeshClient *c = cls;
4112 struct MeshTunnel2 *t;
4114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4115 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4116 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4130 channel_send_destroy (ch);
4131 channel_destroy (ch);
4132 tunnel_destroy_if_empty (t);
4139 * Remove client's ports from the global hashmap on disconnect.
4141 * @param cls Closure (unused).
4143 * @param value Client structure.
4145 * @return GNUNET_OK, keep iterating.
4148 client_release_ports (void *cls,
4154 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4155 if (GNUNET_YES != res)
4158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4159 "Port %u by client %p was not registered.\n",
4167 * Timeout function due to lack of keepalive/traffic from the owner.
4168 * Destroys connection if called.
4170 * @param cls Closure (connection to destroy).
4171 * @param tc TaskContext.
4174 connection_fwd_timeout (void *cls,
4175 const struct GNUNET_SCHEDULER_TaskContext *tc)
4177 struct MeshConnection *c = cls;
4179 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4180 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4183 "Connection %s[%X] FWD timed out. Destroying.\n",
4184 peer2s (c->t->peer),
4187 if (connection_is_terminal (c, GNUNET_NO)) /* If local, leave. */
4190 connection_destroy (c);
4195 * Timeout function due to lack of keepalive/traffic from the destination.
4196 * Destroys connection if called.
4198 * @param cls Closure (connection to destroy).
4199 * @param tc TaskContext
4202 connection_bck_timeout (void *cls,
4203 const struct GNUNET_SCHEDULER_TaskContext *tc)
4205 struct MeshConnection *c = cls;
4207 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4208 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4212 "Connection %s[%X] FWD timed out. Destroying.\n",
4213 peer2s (c->t->peer),
4216 if (connection_is_terminal (c, GNUNET_YES)) /* If local, leave. */
4219 connection_destroy (c);
4224 * Resets the connection timeout task, some other message has done the
4226 * - For the first peer on the direction this means to send
4227 * a keepalive or a path confirmation message (either create or ACK).
4228 * - For all other peers, this means to destroy the connection,
4229 * due to lack of activity.
4230 * Starts the tiemout if no timeout was running (connection just created).
4232 * @param c Connection whose timeout to reset.
4233 * @param fwd Is this forward?
4235 * TODO use heap to improve efficiency of scheduler.
4238 connection_reset_timeout (struct MeshConnection *c, int fwd)
4240 GNUNET_SCHEDULER_TaskIdentifier *ti;
4241 GNUNET_SCHEDULER_Task f;
4243 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4245 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4246 GNUNET_SCHEDULER_cancel (*ti);
4248 if (connection_is_terminal (c, !fwd)) /* Endpoint */
4250 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4251 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4255 struct GNUNET_TIME_Relative delay;
4257 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4258 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4259 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4265 * Iterator to notify all connections of a broken link. Mark connections
4266 * to destroy after all traffic has been sent.
4268 * @param cls Closure (peer disconnected).
4269 * @param key Current key code (tid).
4270 * @param value Value in the hash map (connection).
4272 * @return GNUNET_YES if we should continue to iterate,
4276 connection_broken (void *cls,
4277 const struct GNUNET_HashCode *key,
4280 struct MeshPeer *peer = cls;
4281 struct MeshConnection *c = value;
4282 struct GNUNET_MESH_ConnectionBroken msg;
4285 fwd = peer == connection_get_prev_hop (c);
4286 connection_cancel_queues (c, !fwd);
4288 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4289 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4290 msg.cid = htonl (c->id);
4292 msg.peer1 = my_full_id;
4293 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4294 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4295 c->destroy = GNUNET_YES;
4300 /******************************************************************************/
4301 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4302 /******************************************************************************/
4305 * Free a transmission that was already queued with all resources
4306 * associated to the request.
4308 * @param queue Queue handler to cancel.
4309 * @param clear_cls Is it necessary to free associated cls?
4312 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4314 struct MeshPeer *peer;
4315 struct MeshFlowControl *fc;
4320 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4322 if (GNUNET_YES == clear_cls)
4324 switch (queue->type)
4326 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4327 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying CONNECTION_DESTROY\n");
4328 GNUNET_break (GNUNET_YES == queue->c->destroy);
4330 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4331 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4332 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4333 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4334 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");
4336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4337 GNUNET_MESH_DEBUG_M2S (queue->type));
4340 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4347 GNUNET_MESH_DEBUG_M2S (queue->type));
4349 GNUNET_free_non_null (queue->cls);
4351 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4355 if (NULL != queue->c)
4357 queue->c->pending_messages--;
4358 if (NULL != queue->c->t)
4360 queue->c->t->pending_messages--;
4364 GNUNET_free (queue);
4369 queue_send (void *cls, size_t size, void *buf)
4371 struct MeshPeer *peer = cls;
4372 struct MeshFlowControl *fc;
4373 struct MeshConnection *c;
4374 struct GNUNET_MessageHeader *msg;
4375 struct MeshPeerQueue *queue;
4376 struct MeshTunnel2 *t;
4377 const struct GNUNET_PeerIdentity *dst_id;
4383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4385 if (NULL == buf || 0 == size)
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4392 queue = peer_get_first_message (peer);
4395 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4398 queue->peer->core_transmit = NULL;
4401 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4404 dst_id = GNUNET_PEER_resolve2 (peer->id);
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4406 /* Check if buffer size is enough for the message */
4407 if (queue->size > size)
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4410 peer->core_transmit =
4411 GNUNET_CORE_notify_transmit_ready (core_handle,
4414 GNUNET_TIME_UNIT_FOREVER_REL,
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4423 t = (NULL != c) ? c->t : NULL;
4427 switch (queue->type)
4429 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4430 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4431 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4432 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4433 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4434 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4437 GNUNET_MESH_DEBUG_M2S (queue->type));
4439 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4440 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4441 data_size = send_core_data_raw (queue->cls, size, buf);
4442 msg = (struct GNUNET_MessageHeader *) buf;
4443 type = ntohs (msg->type);
4445 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4447 if (connection_is_terminal (c, GNUNET_NO))
4448 data_size = send_core_connection_create (queue->cls, size, buf);
4450 data_size = send_core_data_raw (queue->cls, size, buf);
4452 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4454 if (connection_is_terminal (c, GNUNET_YES))
4455 data_size = send_core_connection_ack (queue->cls, size, buf);
4457 data_size = send_core_data_raw (queue->cls, size, buf);
4461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4466 if (0 < drop_percent &&
4467 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4470 "Dropping message of type %s\n",
4471 GNUNET_MESH_DEBUG_M2S (queue->type));
4474 /* Free queue, but cls was freed by send_core_* */
4475 queue_destroy (queue, GNUNET_NO);
4477 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4480 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4481 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4482 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4483 fc->last_pid_sent = pid;
4484 connection_send_ack (c, fwd);
4490 /* If more data in queue, send next */
4491 queue = peer_get_first_message (peer);
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4495 if (NULL == peer->core_transmit) {
4496 peer->core_transmit =
4497 GNUNET_CORE_notify_transmit_ready(core_handle,
4500 GNUNET_TIME_UNIT_FOREVER_REL,
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4509 "* tmt rdy called somewhere else\n");
4511 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4513 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4515 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4520 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4522 GNUNET_SCHEDULER_cancel (fc->poll_task);
4523 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4528 c->pending_messages--;
4529 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4532 connection_destroy (c);
4538 t->pending_messages--;
4539 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4551 queue_add (void *cls, uint16_t type, size_t size,
4552 struct MeshPeer *dst,
4553 struct MeshConnection *c,
4554 struct MeshChannel *ch)
4556 struct MeshPeerQueue *queue;
4557 struct MeshFlowControl *fc;
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4562 "queue add %s (%u bytes) on c %p, ch %p\n",
4563 GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4565 fwd = (dst == connection_get_next_hop (c));
4566 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4576 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4577 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4581 ( (NULL != ch->root && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4582 (NULL != ch->dest && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4585 if (fc->queue_n >= fc->queue_max && 0 == priority)
4587 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4591 "queue full: %u/%u\n",
4592 fc->queue_n, fc->queue_max);
4593 return; /* Drop this message */
4596 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
4597 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4598 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4601 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4609 if (100 <= priority)
4610 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
4612 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4614 if (NULL == dst->core_transmit)
4616 dst->core_transmit =
4617 GNUNET_CORE_notify_transmit_ready (core_handle,
4620 GNUNET_TIME_UNIT_FOREVER_REL,
4621 GNUNET_PEER_resolve2 (dst->id),
4626 c->pending_messages++;
4627 c->t->pending_messages++;
4633 /******************************************************************************/
4634 /******************** MESH NETWORK HANDLERS **************************/
4635 /******************************************************************************/
4639 * Generic handler for mesh network payload traffic.
4641 * @param t Tunnel on which we got this message.
4642 * @param message Unencryted data message.
4643 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4645 * @return GNUNET_OK to keep the connection open,
4646 * GNUNET_SYSERR to close it (signal serious error)
4649 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4651 struct MeshChannelReliability *rel;
4652 struct MeshChannel *ch;
4653 struct MeshClient *c;
4660 size = ntohs (msg->header.size);
4662 sizeof (struct GNUNET_MESH_Data) +
4663 sizeof (struct GNUNET_MessageHeader))
4668 type = ntohs (msg->header.type);
4669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4670 GNUNET_MESH_DEBUG_M2S (type));
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4672 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4675 ch = channel_get (t, ntohl (msg->chid));
4678 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4683 /* Initialize FWD/BCK data */
4684 c = fwd ? ch->dest : ch->root;
4685 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4686 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4694 tunnel_change_state (t, MESH_TUNNEL_READY);
4696 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4698 mid = ntohl (msg->mid);
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4701 if (GNUNET_NO == ch->reliable ||
4702 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4703 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4706 if (GNUNET_YES == ch->reliable)
4708 /* Is this the exact next expected messasge? */
4709 if (mid == *mid_recv)
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4712 *mid_recv = *mid_recv + 1;
4713 channel_send_client_data (ch, msg, fwd);
4714 channel_send_client_buffered_data (ch, c, rel);
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4719 channel_rel_add_buffered_data (msg, rel);
4722 else /* Tunnel unreliable, send to clients directly */
4724 channel_send_client_data (ch, msg, fwd);
4729 GNUNET_break_op (0);
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4731 " MID %u not expected (%u - %u), dropping!\n",
4732 mid, *mid_recv, *mid_recv + 64);
4735 channel_send_data_ack (ch, fwd);
4740 * Handler for mesh network traffic end-to-end ACKs.
4742 * @param t Tunnel on which we got this message.
4743 * @param message Data message.
4744 * @param fwd Is this a fwd ACK? (dest->orig)
4746 * @return GNUNET_OK to keep the connection open,
4747 * GNUNET_SYSERR to close it (signal serious error)
4750 handle_data_ack (struct MeshTunnel2 *t,
4751 const struct GNUNET_MESH_DataACK *msg, int fwd)
4753 struct MeshChannelReliability *rel;
4754 struct MeshReliableMessage *copy;
4755 struct MeshReliableMessage *next;
4756 struct MeshChannel *ch;
4761 type = ntohs (msg->header.type);
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4763 GNUNET_MESH_DEBUG_M2S (type));
4764 ch = channel_get (t, ntohl (msg->chid));
4767 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4770 ack = ntohl (msg->mid);
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4772 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4774 if (GNUNET_YES == fwd)
4787 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4789 if (GMC_is_pid_bigger (copy->mid, ack))
4791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4792 channel_rel_free_sent (rel, msg);
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4798 rel_message_free (copy);
4800 /* ACK client if needed */
4801 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4803 /* If some message was free'd, update the retransmission delay*/
4804 if (GNUNET_YES == work)
4806 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4808 GNUNET_SCHEDULER_cancel (rel->retry_task);
4809 if (NULL == rel->head_sent)
4811 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4815 struct GNUNET_TIME_Absolute new_target;
4816 struct GNUNET_TIME_Relative delay;
4818 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4819 MESH_RETRANSMIT_MARGIN);
4820 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4822 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4824 GNUNET_SCHEDULER_add_delayed (delay,
4825 &channel_retransmit_message,
4837 * Core handler for connection creation.
4839 * @param cls Closure (unused).
4840 * @param peer Sender (neighbor).
4841 * @param message Message.
4843 * @return GNUNET_OK to keep the connection open,
4844 * GNUNET_SYSERR to close it (signal serious error)
4847 handle_mesh_connection_create (void *cls,
4848 const struct GNUNET_PeerIdentity *peer,
4849 const struct GNUNET_MessageHeader *message)
4851 struct GNUNET_MESH_ConnectionCreate *msg;
4852 struct GNUNET_PeerIdentity *id;
4853 struct GNUNET_HashCode *tid;
4854 struct MeshPeerPath *path;
4855 struct MeshPeer *dest_peer;
4856 struct MeshPeer *orig_peer;
4857 struct MeshConnection *c;
4858 unsigned int own_pos;
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
4866 size = ntohs (message->size);
4867 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
4869 GNUNET_break_op (0);
4873 /* Calculate hops */
4874 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
4875 if (size % sizeof (struct GNUNET_PeerIdentity))
4877 GNUNET_break_op (0);
4880 size /= sizeof (struct GNUNET_PeerIdentity);
4883 GNUNET_break_op (0);
4886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4888 /* Get parameters */
4889 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
4890 cid = ntohl (msg->cid);
4892 id = (struct GNUNET_PeerIdentity *) &msg[1];
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4894 " connection %s[%X] (%s).\n",
4895 GNUNET_h2s (tid), cid, GNUNET_i2s (id));
4897 /* Create connection */
4898 c = connection_get (tid, cid);
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
4902 c = connection_new (tid, cid);
4906 connection_reset_timeout (c, GNUNET_YES);
4907 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
4909 /* Remember peers */
4910 dest_peer = peer_get (&id[size - 1]);
4911 orig_peer = peer_get (&id[0]);
4914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4915 path = path_new (size);
4917 for (i = 0; i < size; i++)
4919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4920 GNUNET_i2s (&id[i]));
4921 path->peers[i] = GNUNET_PEER_intern (&id[i]);
4922 if (path->peers[i] == myid)
4925 if (own_pos == 0 && path->peers[own_pos] != myid)
4927 /* create path: self not found in path through self */
4928 GNUNET_break_op (0);
4929 path_destroy (path);
4930 connection_destroy (c);
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4934 path_add_to_peers (path, GNUNET_NO);
4935 c->path = path_duplicate (path);
4936 c->own_pos = own_pos;
4938 /* Is it a connection to us? */
4939 if (own_pos == size - 1)
4941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4942 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
4944 send_connection_ack (c);
4946 /* Keep tunnel alive in direction dest->owner*/
4947 connection_reset_timeout (c, GNUNET_NO);
4951 /* It's for somebody else! Retransmit. */
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4953 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
4954 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
4955 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
4962 * Core handler for path ACKs
4964 * @param cls closure
4965 * @param message message
4966 * @param peer peer identity this notification is about
4968 * @return GNUNET_OK to keep the connection open,
4969 * GNUNET_SYSERR to close it (signal serious error)
4972 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4973 const struct GNUNET_MessageHeader *message)
4975 struct GNUNET_MESH_ConnectionACK *msg;
4976 struct MeshPeerPath *p;
4977 struct MeshConnection *c;
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
4981 msg = (struct GNUNET_MESH_ConnectionACK *) message;
4982 cid = ntohl(msg->cid);
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s[%X]\n",
4984 GNUNET_h2s (&msg->tid), cid);
4985 c = connection_get (&msg->tid, cid);
4988 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4997 /* Add path to peers? */
5001 path_add_to_peers (p, GNUNET_YES);
5007 connection_change_state (c, MESH_CONNECTION_READY);
5008 connection_reset_timeout (c, GNUNET_NO);
5009 if (MESH_TUNNEL_READY != c->t->state)
5010 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5011 tunnel_send_queued_data (c->t, GNUNET_YES);
5013 /* Message for us? */
5014 if (connection_is_terminal (c, GNUNET_NO))
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5017 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5019 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5020 c->t->peer->dhtget = NULL;
5022 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5027 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5033 * Core handler for notifications of broken paths
5035 * @param cls Closure (unused).
5036 * @param peer Peer identity of sending neighbor.
5037 * @param message Message.
5039 * @return GNUNET_OK to keep the connection open,
5040 * GNUNET_SYSERR to close it (signal serious error)
5043 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5044 const struct GNUNET_MessageHeader *message)
5046 struct GNUNET_MESH_ConnectionBroken *msg;
5047 struct MeshConnection *c;
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5050 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5051 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5053 GNUNET_i2s (&msg->peer1));
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5055 GNUNET_i2s (&msg->peer2));
5056 c = connection_get (&msg->tid, ntohl (msg->cid));
5059 GNUNET_break_op (0);
5062 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5063 GNUNET_PEER_search (&msg->peer2));
5070 * Core handler for tunnel destruction
5072 * @param cls Closure (unused).
5073 * @param peer Peer identity of sending neighbor.
5074 * @param message Message.
5076 * @return GNUNET_OK to keep the connection open,
5077 * GNUNET_SYSERR to close it (signal serious error)
5080 handle_mesh_connection_destroy (void *cls,
5081 const struct GNUNET_PeerIdentity *peer,
5082 const struct GNUNET_MessageHeader *message)
5084 struct GNUNET_MESH_ConnectionDestroy *msg;
5085 struct MeshConnection *c;
5089 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5091 "Got a CONNECTION DESTROY message from %s\n",
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5094 " for connection %s[%X]\n",
5095 GNUNET_h2s (&msg->tid), ntohl (msg->cid));
5096 c = connection_get (&msg->tid, ntohl (msg->cid));
5099 /* Probably already got the message from another path,
5100 * destroyed the tunnel and retransmitted to children.
5103 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5107 id = GNUNET_PEER_search (peer);
5108 if (id == connection_get_prev_hop (c)->id)
5110 else if (id == connection_get_next_hop (c)->id)
5114 GNUNET_break_op (0);
5117 send_prebuilt_message_connection (message, c, NULL, fwd);
5118 c->destroy = GNUNET_YES;
5124 * Handler for channel create messages.
5126 * @param t Tunnel this channel is to be created in.
5127 * @param msg Message.
5128 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5130 * @return GNUNET_OK to keep the connection open,
5131 * GNUNET_SYSERR to close it (signal serious error)
5134 handle_channel_create (struct MeshTunnel2 *t,
5135 struct GNUNET_MESH_ChannelCreate *msg,
5138 MESH_ChannelNumber chid;
5139 struct MeshChannel *ch;
5140 struct MeshClient *c;
5142 /* Check message size */
5143 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5145 GNUNET_break_op (0);
5149 /* Check if channel exists */
5150 chid = ntohl (msg->chid);
5151 ch = channel_get (t, chid);
5154 /* Probably a retransmission, safe to ignore */
5158 /* Find a destination client */
5159 c = GNUNET_CONTAINER_multihashmap32_get (ports, msg->port);
5162 /* TODO send reject */
5166 /* Create channel */
5167 ch = channel_new (t, NULL, 0);
5168 channel_set_options (ch, ntohl (msg->opt));
5169 channel_add_client (ch, c);
5170 if (GNUNET_YES == ch->reliable)
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5173 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5174 ch->bck_rel->ch = ch;
5175 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5178 send_local_channel_create (ch);
5185 * Handler for channel destroy messages.
5187 * @param t Tunnel this channel is to be destroyed of.
5188 * @param msg Message.
5189 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5191 * @return GNUNET_OK to keep the connection open,
5192 * GNUNET_SYSERR to close it (signal serious error)
5195 handle_channel_destroy (struct MeshTunnel2 *t,
5196 struct GNUNET_MESH_ChannelDestroy *msg,
5199 MESH_ChannelNumber chid;
5200 struct MeshChannel *ch;
5202 /* Check message size */
5203 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelDestroy))
5205 GNUNET_break_op (0);
5209 /* Check if channel exists */
5210 chid = ntohl (msg->chid);
5211 ch = channel_get (t, chid);
5214 /* Probably a retransmission, safe to ignore */
5218 send_local_channel_destroy (ch, fwd);
5219 channel_destroy (ch);
5226 * Generic handler for mesh network encrypted traffic.
5228 * @param peer Peer identity this notification is about.
5229 * @param message Encrypted message.
5230 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5232 * @return GNUNET_OK to keep the connection open,
5233 * GNUNET_SYSERR to close it (signal serious error)
5236 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5237 const struct GNUNET_MESH_Encrypted *msg,
5240 struct MeshConnection *c;
5241 struct MeshTunnel2 *t;
5242 struct MeshPeer *neighbor;
5243 struct MeshFlowControl *fc;
5250 size = ntohs (msg->header.size);
5252 sizeof (struct GNUNET_MESH_Encrypted) +
5253 sizeof (struct GNUNET_MessageHeader))
5258 type = ntohs (msg->header.type);
5259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5260 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5262 /* Check connection */
5263 c = connection_get (&msg->tid, ntohl (msg->cid));
5266 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5271 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5273 /* Check if origin is as expected */
5274 neighbor = connection_get_hop (c, fwd);
5275 if (peer_get (peer)->id != neighbor->id)
5277 GNUNET_break_op (0);
5282 pid = ntohl (msg->pid);
5283 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5285 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5287 "WARNING Received PID %u, (prev %u), ACK %u\n",
5288 pid, fc->last_pid_recv, fc->last_ack_sent);
5291 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5293 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5295 " Pid %u not expected (%u+), dropping!\n",
5296 pid, fc->last_pid_recv + 1);
5299 if (MESH_CONNECTION_SENT == c->state)
5300 connection_change_state (c, MESH_CONNECTION_READY);
5301 connection_reset_timeout (c, fwd);
5302 fc->last_pid_recv = pid;
5304 /* Is this message for us? */
5305 if (connection_is_terminal (c, fwd))
5307 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5309 struct GNUNET_MessageHeader *msgh;
5311 /* TODO signature verification */
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us!\n");
5313 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5315 fc->last_pid_recv = pid;
5316 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5317 msgh = (struct GNUNET_MessageHeader *) cbuf;
5318 switch (ntohs (msgh->type))
5320 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
5321 if (GNUNET_YES == fwd)
5322 return handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh,
5324 GNUNET_break_op (0);
5326 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
5327 if (GNUNET_NO == fwd)
5328 return handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh,
5330 GNUNET_break_op (0);
5332 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5333 if (GNUNET_YES == fwd)
5334 handle_data (t, (struct GNUNET_MESH_Data *) msgh, GNUNET_YES);
5335 GNUNET_break_op (0);
5337 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5338 if (GNUNET_NO == fwd)
5339 handle_data (t, (struct GNUNET_MESH_Data *) msgh, GNUNET_NO);
5340 GNUNET_break_op (0);
5342 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5343 return handle_channel_create (t,
5344 (struct GNUNET_MESH_ChannelCreate *) msgh,
5347 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5348 return handle_channel_destroy (t,
5349 (struct GNUNET_MESH_ChannelDestroy *)
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5355 "end-to-end message not known (%u)\n",
5356 ntohs (msgh->type));
5359 connection_send_ack (c, fwd);
5363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5364 ttl = ntohl (msg->ttl);
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5368 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5370 connection_send_ack (c, fwd);
5373 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5375 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5376 connection_send_ack (c, fwd);
5383 * Core handler for mesh network traffic going orig->dest.
5385 * @param cls Closure (unused).
5386 * @param message Message received.
5387 * @param peer Peer who sent the message.
5389 * @return GNUNET_OK to keep the connection open,
5390 * GNUNET_SYSERR to close it (signal serious error)
5393 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5394 const struct GNUNET_MessageHeader *message)
5396 return handle_mesh_encrypted (peer,
5397 (struct GNUNET_MESH_Encrypted *)message,
5402 * Core handler for mesh network traffic going dest->orig.
5404 * @param cls Closure (unused).
5405 * @param message Message received.
5406 * @param peer Peer who sent the message.
5408 * @return GNUNET_OK to keep the connection open,
5409 * GNUNET_SYSERR to close it (signal serious error)
5412 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5413 const struct GNUNET_MessageHeader *message)
5415 return handle_mesh_encrypted (peer,
5416 (struct GNUNET_MESH_Encrypted *)message,
5422 * Core handler for mesh network traffic point-to-point acks.
5424 * @param cls closure
5425 * @param message message
5426 * @param peer peer identity this notification is about
5428 * @return GNUNET_OK to keep the connection open,
5429 * GNUNET_SYSERR to close it (signal serious error)
5432 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5433 const struct GNUNET_MessageHeader *message)
5435 struct GNUNET_MESH_ACK *msg;
5436 struct MeshConnection *c;
5437 struct MeshFlowControl *fc;
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5443 msg = (struct GNUNET_MESH_ACK *) message;
5445 c = connection_get (&msg->tid, ntohl (msg->cid));
5449 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5454 ack = ntohl (msg->ack);
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5457 /* Is this a forward or backward ACK? */
5458 id = GNUNET_PEER_search (peer);
5459 if (connection_get_next_hop (c)->id == id)
5461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5464 else if (connection_get_prev_hop (c)->id == id)
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5471 GNUNET_break_op (0);
5475 /* Cancel polling if the ACK is bigger than before. */
5476 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5477 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5479 GNUNET_SCHEDULER_cancel (fc->poll_task);
5480 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5481 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5484 fc->last_ack_recv = ack;
5485 connection_unlock_queue (c, fc == &c->fwd_fc);
5492 * Core handler for mesh network traffic point-to-point ack polls.
5494 * @param cls closure
5495 * @param message message
5496 * @param peer peer identity this notification is about
5498 * @return GNUNET_OK to keep the connection open,
5499 * GNUNET_SYSERR to close it (signal serious error)
5502 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5503 const struct GNUNET_MessageHeader *message)
5505 struct GNUNET_MESH_Poll *msg;
5506 struct MeshConnection *c;
5507 struct MeshFlowControl *fc;
5511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5514 msg = (struct GNUNET_MESH_Poll *) message;
5516 c = connection_get (&msg->tid, ntohl (msg->cid));
5520 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5522 GNUNET_break_op (0);
5526 /* Is this a forward or backward ACK? */
5527 id = GNUNET_PEER_search (peer);
5528 if (connection_get_next_hop (c)->id == id)
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5533 else if (connection_get_prev_hop (c)->id == id)
5535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5540 GNUNET_break_op (0);
5544 pid = ntohl (msg->pid);
5545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5546 pid, fc->last_pid_recv);
5547 fc->last_pid_recv = pid;
5548 connection_send_ack (c, fc == &c->fwd_fc);
5555 * Core handler for mesh keepalives.
5557 * @param cls closure
5558 * @param message message
5559 * @param peer peer identity this notification is about
5560 * @return GNUNET_OK to keep the connection open,
5561 * GNUNET_SYSERR to close it (signal serious error)
5563 * TODO: Check who we got this from, to validate route.
5566 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5567 const struct GNUNET_MessageHeader *message)
5569 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5570 struct MeshConnection *c;
5571 struct MeshPeer *neighbor;
5574 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5578 c = connection_get (&msg->tid, ntohl (msg->cid));
5581 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5586 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5587 GNUNET_YES : GNUNET_NO;
5589 /* Check if origin is as expected */
5590 neighbor = connection_get_hop (c, fwd);
5591 if (peer_get (peer)->id != neighbor->id)
5593 GNUNET_break_op (0);
5597 connection_change_state (c, MESH_CONNECTION_READY);
5598 connection_reset_timeout (c, fwd);
5600 if (connection_is_terminal (c, fwd))
5603 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5604 send_prebuilt_message_connection (message, c, NULL, fwd);
5612 * Functions to handle messages from core
5614 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5615 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5617 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5618 sizeof (struct GNUNET_MESH_ConnectionACK)},
5619 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5620 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5621 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5622 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5623 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5624 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5625 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5626 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5627 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5628 sizeof (struct GNUNET_MESH_ACK)},
5629 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5630 sizeof (struct GNUNET_MESH_Poll)},
5631 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5632 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5638 * Function to process paths received for a new peer addition. The recorded
5639 * paths form the initial tunnel, which can be optimized later.
5640 * Called on each result obtained for the DHT search.
5642 * @param cls closure
5643 * @param exp when will this value expire
5644 * @param key key of the result
5645 * @param get_path path of the get request
5646 * @param get_path_length lenght of get_path
5647 * @param put_path path of the put request
5648 * @param put_path_length length of the put_path
5649 * @param type type of the result
5650 * @param size number of bytes in data
5651 * @param data pointer to the result data
5654 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5655 const struct GNUNET_HashCode * key,
5656 const struct GNUNET_PeerIdentity *get_path,
5657 unsigned int get_path_length,
5658 const struct GNUNET_PeerIdentity *put_path,
5659 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5660 size_t size, const void *data)
5662 struct MeshPeer *peer = cls;
5663 struct MeshPeerPath *p;
5664 struct MeshConnection *c;
5665 struct GNUNET_PeerIdentity pi;
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5669 GNUNET_PEER_resolve (peer->id, &pi);
5670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5672 p = path_build_from_dht (get_path, get_path_length,
5673 put_path, put_path_length);
5674 path_add_to_peers (p, GNUNET_NO);
5677 /* Count connections */
5678 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5680 /* If we already have 3 (or more (?!)) connections, it's enough */
5684 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5687 peer_connect (peer);
5693 /******************************************************************************/
5694 /********************* MESH LOCAL HANDLES **************************/
5695 /******************************************************************************/
5699 * Handler for client connection.
5701 * @param cls Closure (unused).
5702 * @param client Client handler.
5705 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5707 struct MeshClient *c;
5711 c = GNUNET_malloc (sizeof (struct MeshClient));
5713 c->id = next_client_id++; /* overflow not important: just for debug */
5714 GNUNET_SERVER_client_keep (client);
5715 GNUNET_SERVER_client_set_user_context (client, c);
5716 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5721 * Handler for client disconnection
5723 * @param cls closure
5724 * @param client identification of the client; NULL
5725 * for the last call when the server is destroyed
5728 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5730 struct MeshClient *c;
5731 struct MeshClient *next;
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5740 c = client_get (client);
5743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5745 GNUNET_SERVER_client_drop (c->handle);
5746 c->shutting_down = GNUNET_YES;
5747 if (NULL != c->own_channels)
5749 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
5750 &channel_destroy_iterator, c);
5751 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
5754 if (NULL != c->incoming_channels)
5756 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
5757 &channel_destroy_iterator, c);
5758 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
5761 if (NULL != c->ports)
5763 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5764 &client_release_ports, c);
5765 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5768 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5769 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5776 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
5778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
5784 * Handler for new clients
5786 * @param cls closure
5787 * @param client identification of the client
5788 * @param message the actual message, which includes messages the client wants
5791 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5792 const struct GNUNET_MessageHeader *message)
5794 struct GNUNET_MESH_ClientConnect *cc_msg;
5795 struct MeshClient *c;
5800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
5802 /* Check data sanity */
5803 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5804 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5805 if (0 != (size % sizeof (uint32_t)))
5808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5811 size /= sizeof (uint32_t);
5813 /* Initialize new client structure */
5814 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
5816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
5821 p = (uint32_t *) &cc_msg[1];
5822 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
5823 for (i = 0; i < size; i++)
5826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
5828 /* store in client's hashmap */
5829 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
5830 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5831 /* store in global hashmap */
5832 /* FIXME only allow one client to have the port open,
5833 * have a backup hashmap with waiting clients */
5834 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5839 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5840 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5841 GNUNET_SERVER_notification_context_add (nc, client);
5842 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5850 * Handler for requests of new tunnels
5852 * @param cls Closure.
5853 * @param client Identification of the client.
5854 * @param message The actual message.
5857 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
5858 const struct GNUNET_MessageHeader *message)
5860 struct GNUNET_MESH_ChannelMessage *msg;
5861 struct MeshPeer *peer;
5862 struct MeshTunnel2 *t;
5863 struct MeshChannel *ch;
5864 struct MeshClient *c;
5865 MESH_ChannelNumber chid;
5867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
5869 /* Sanity check for client registration */
5870 if (NULL == (c = client_get (client)))
5873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5878 /* Message size sanity check */
5879 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5882 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5886 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5888 GNUNET_i2s (&msg->peer), ntohl (msg->port));
5889 chid = ntohl (msg->channel_id);
5891 /* Sanity check for duplicate channel IDs */
5892 if (NULL != channel_get_by_local_id (c, chid))
5895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5899 peer = peer_get (&msg->peer);
5900 if (NULL == peer->tunnel)
5902 struct GNUNET_HashCode tid;
5904 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &tid);
5905 peer->tunnel = tunnel_new (&tid);
5906 peer->tunnel->peer = peer;
5910 /* Create channel */
5911 ch = channel_new (t, c, chid);
5915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5918 ch->port = ntohl (msg->port);
5919 channel_set_options (ch, ntohl (msg->opt));
5921 /* In unreliable channels, we'll use the DLL to buffer data for the root */
5922 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5923 ch->fwd_rel->ch = ch;
5924 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
5927 GNUNET_h2s (&t->id), ch->gid, ch->port, ch->lid_root);
5928 peer_connect (peer);
5930 /* Send create channel */
5932 struct GNUNET_MESH_ChannelCreate msgcc;
5934 msgcc.header.size = htons (sizeof (msgcc));
5935 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
5936 msgcc.chid = htonl (ch->gid);
5937 msgcc.port = msg->port;
5938 msgcc.opt = msg->opt;
5940 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
5943 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5949 * Handler for requests of deleting tunnels
5951 * @param cls closure
5952 * @param client identification of the client
5953 * @param message the actual message
5956 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5957 const struct GNUNET_MessageHeader *message)
5959 struct GNUNET_MESH_ChannelMessage *msg;
5960 struct MeshClient *c;
5961 struct MeshChannel *ch;
5962 struct MeshTunnel2 *t;
5963 MESH_ChannelNumber chid;
5965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5966 "Got a DESTROY CHANNEL from client!\n");
5968 /* Sanity check for client registration */
5969 if (NULL == (c = client_get (client)))
5972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5977 /* Message sanity check */
5978 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5981 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5985 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5987 /* Retrieve tunnel */
5988 chid = ntohl (msg->channel_id);
5989 ch = channel_get_by_local_id (c, chid);
5992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
5994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5998 /* Cleanup after the tunnel */
5999 client_delete_channel (c, ch);
6000 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6004 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6011 " channel %X client %p (%p, %p)\n",
6012 chid, c, ch->root, ch->dest);
6017 channel_destroy (ch);
6018 tunnel_destroy_if_empty (t);
6020 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6026 * Handler for client traffic
6028 * @param cls closure
6029 * @param client identification of the client
6030 * @param message the actual message
6033 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6034 const struct GNUNET_MessageHeader *message)
6036 struct GNUNET_MESH_LocalData *msg;
6037 struct MeshClient *c;
6038 struct MeshChannel *ch;
6039 MESH_ChannelNumber chid;
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6044 "Got data from a client!\n");
6046 /* Sanity check for client registration */
6047 if (NULL == (c = client_get (client)))
6050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6055 msg = (struct GNUNET_MESH_LocalData *) message;
6057 /* Sanity check for message size */
6058 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6059 if (size < sizeof (struct GNUNET_MessageHeader))
6062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6066 /* Channel exists? */
6067 chid = ntohl (msg->id);
6068 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6069 ch = channel_get_by_local_id (c, chid);
6073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6077 /* Is the client in the channel? */
6080 ch->root->handle == client)
6084 ch->dest->handle == client) ) )
6087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6091 /* Ok, everything is correct, send the message
6092 * (pretend we got it from a mesh peer)
6095 struct GNUNET_MESH_Data *payload;
6096 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
6099 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6100 if (GNUNET_YES == ch->reliable)
6102 struct MeshChannelReliability *rel;
6103 struct MeshReliableMessage *copy;
6105 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6106 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
6107 + sizeof(struct GNUNET_MESH_Data)
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
6111 copy->timestamp = GNUNET_TIME_absolute_get ();
6114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
6115 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
6116 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
6119 GNUNET_TIME_relative_multiply (rel->expected_delay,
6120 MESH_RETRANSMIT_MARGIN);
6122 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
6123 &channel_retransmit_message,
6126 payload = (struct GNUNET_MESH_Data *) ©[1];
6130 payload = (struct GNUNET_MESH_Data *) cbuf;
6132 payload->mid = htonl (*mid);
6134 memcpy (&payload[1], &msg[1], size);
6135 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
6136 payload->header.type = htons (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ?
6137 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
6138 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6139 payload->chid = htonl (ch->gid);
6140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6141 " calling generic handler...\n");
6142 send_prebuilt_message_channel (&payload->header, ch, fwd);
6144 if (tunnel_get_buffer (ch->t, fwd) > 0)
6145 send_local_ack (ch, c, fwd);
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6147 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6154 * Handler for client's ACKs for payload traffic.
6156 * @param cls Closure (unused).
6157 * @param client Identification of the client.
6158 * @param message The actual message.
6161 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6162 const struct GNUNET_MessageHeader *message)
6164 struct GNUNET_MESH_LocalAck *msg;
6165 struct MeshChannelReliability *rel;
6166 struct MeshChannel *ch;
6167 struct MeshClient *c;
6168 MESH_ChannelNumber chid;
6171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6173 /* Sanity check for client registration */
6174 if (NULL == (c = client_get (client)))
6177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6182 msg = (struct GNUNET_MESH_LocalAck *) message;
6184 /* Channel exists? */
6185 chid = ntohl (msg->channel_id);
6186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6187 ch = channel_get_by_local_id (c, chid);
6191 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6197 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6198 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6200 rel->client_ready = GNUNET_YES;
6201 channel_send_client_buffered_data (ch, c, rel);
6202 channel_send_ack (ch, 64 - rel->n_recv, fwd);
6204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6211 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6213 * @param cls Closure (client handle).
6214 * @param key Key (hashed tunnel ID, unused).
6215 * @param value Tunnel info.
6217 * @return GNUNET_YES, to keep iterating.
6220 monitor_all_tunnels_iterator (void *cls,
6221 const struct GNUNET_HashCode * key,
6224 struct GNUNET_SERVER_Client *client = cls;
6225 struct MeshChannel *ch = value;
6226 struct GNUNET_MESH_LocalMonitor *msg;
6228 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6229 msg->channel_id = htonl (ch->gid);
6230 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6231 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6233 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6234 "* sending info about tunnel %s\n",
6235 GNUNET_i2s (&msg->owner));
6237 GNUNET_SERVER_notification_context_unicast (nc, client,
6238 &msg->header, GNUNET_NO);
6244 * Handler for client's MONITOR request.
6246 * @param cls Closure (unused).
6247 * @param client Identification of the client.
6248 * @param message The actual message.
6251 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6252 const struct GNUNET_MessageHeader *message)
6254 struct MeshClient *c;
6256 /* Sanity check for client registration */
6257 if (NULL == (c = client_get (client)))
6260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6265 "Received get tunnels request from client %u\n",
6267 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6268 monitor_all_tunnels_iterator,
6270 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6271 "Get tunnels request from client %u completed\n",
6273 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6278 * Handler for client's MONITOR_TUNNEL request.
6280 * @param cls Closure (unused).
6281 * @param client Identification of the client.
6282 * @param message The actual message.
6285 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6286 const struct GNUNET_MessageHeader *message)
6288 const struct GNUNET_MESH_LocalMonitor *msg;
6289 struct GNUNET_MESH_LocalMonitor *resp;
6290 struct MeshClient *c;
6291 struct MeshChannel *ch;
6293 /* Sanity check for client registration */
6294 if (NULL == (c = client_get (client)))
6297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6301 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6303 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6306 ntohl (msg->channel_id));
6307 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6311 /* We don't know the tunnel */
6312 struct GNUNET_MESH_LocalMonitor warn;
6315 GNUNET_SERVER_notification_context_unicast (nc, client,
6318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6322 /* Initialize context */
6323 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6325 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6326 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6327 &resp->header, GNUNET_NO);
6330 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6331 "Monitor tunnel request from client %u completed\n",
6333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6338 * Functions to handle messages from clients
6340 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6341 {&handle_local_new_client, NULL,
6342 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6343 {&handle_local_channel_create, NULL,
6344 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6345 sizeof (struct GNUNET_MESH_ChannelMessage)},
6346 {&handle_local_channel_destroy, NULL,
6347 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6348 sizeof (struct GNUNET_MESH_ChannelMessage)},
6349 {&handle_local_data, NULL,
6350 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6351 {&handle_local_ack, NULL,
6352 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6353 sizeof (struct GNUNET_MESH_LocalAck)},
6354 {&handle_local_get_tunnels, NULL,
6355 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6356 sizeof (struct GNUNET_MessageHeader)},
6357 {&handle_local_show_tunnel, NULL,
6358 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6359 sizeof (struct GNUNET_MESH_LocalMonitor)},
6365 * Method called whenever a given peer connects.
6367 * @param cls closure
6368 * @param peer peer identity this notification is about
6371 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6373 struct MeshPeer *pi;
6374 struct MeshPeerPath *path;
6376 DEBUG_CONN ("Peer connected\n");
6377 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6378 pi = peer_get (peer);
6381 DEBUG_CONN (" (self)\n");
6382 path = path_new (1);
6386 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6387 path = path_new (2);
6388 path->peers[1] = pi->id;
6389 GNUNET_PEER_change_rc (pi->id, 1);
6390 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6392 path->peers[0] = myid;
6393 GNUNET_PEER_change_rc (myid, 1);
6394 peer_add_path (pi, path, GNUNET_YES);
6396 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6402 * Method called whenever a peer disconnects.
6404 * @param cls closure
6405 * @param peer peer identity this notification is about
6408 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6410 struct MeshPeer *pi;
6412 DEBUG_CONN ("Peer disconnected\n");
6413 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6420 peer_remove_path (pi, myid, pi->id);
6422 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6425 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6426 pi->connections = NULL;
6429 DEBUG_CONN (" (self)\n");
6431 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6438 * Install server (service) handlers and start listening to clients.
6443 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6444 GNUNET_SERVER_connect_notify (server_handle,
6445 &handle_local_client_connect, NULL);
6446 GNUNET_SERVER_disconnect_notify (server_handle,
6447 &handle_local_client_disconnect, NULL);
6448 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6450 clients_head = NULL;
6451 clients_tail = NULL;
6453 GNUNET_SERVER_resume (server_handle);
6458 * To be called on core init/fail.
6460 * @param cls Closure (config)
6461 * @param server handle to the server for this service
6462 * @param identity the public identity of this peer
6465 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6466 const struct GNUNET_PeerIdentity *identity)
6468 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6472 GNUNET_break (core_handle == server);
6473 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6479 GNUNET_i2s (identity));
6480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6482 GNUNET_i2s (&my_full_id));
6483 GNUNET_CORE_disconnect (core_handle);
6484 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6485 NULL, /* Closure passed to MESH functions */
6486 &core_init, /* Call core_init once connected */
6487 &core_connect, /* Handle connects */
6488 &core_disconnect, /* remove peers on disconnects */
6489 NULL, /* Don't notify about all incoming messages */
6490 GNUNET_NO, /* For header only in notification */
6491 NULL, /* Don't notify about all outbound messages */
6492 GNUNET_NO, /* For header-only out notification */
6493 core_handlers); /* Register these handlers */
6502 /******************************************************************************/
6503 /************************ MAIN FUNCTIONS ****************************/
6504 /******************************************************************************/
6507 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6509 * @param cls closure
6510 * @param key current key code
6511 * @param value value in the hash map
6512 * @return GNUNET_YES if we should continue to iterate,
6516 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6518 struct MeshTunnel2 *t = value;
6526 * Task run during shutdown.
6532 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6536 if (core_handle != NULL)
6538 GNUNET_CORE_disconnect (core_handle);
6541 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6542 if (dht_handle != NULL)
6544 GNUNET_DHT_disconnect (dht_handle);
6549 GNUNET_SERVER_notification_context_destroy (nc);
6552 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6554 GNUNET_SCHEDULER_cancel (announce_id_task);
6555 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6562 * Process mesh requests.
6564 * @param cls closure
6565 * @param server the initialized server
6566 * @param c configuration to use
6569 run (void *cls, struct GNUNET_SERVER_Handle *server,
6570 const struct GNUNET_CONFIGURATION_Handle *c)
6573 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6576 server_handle = server;
6577 GNUNET_SERVER_suspend (server_handle);
6580 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6585 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6586 "mesh", "peer/privatekey");
6587 GNUNET_SCHEDULER_shutdown ();
6592 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6593 &refresh_connection_time))
6595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6597 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6598 "mesh", "refresh path time");
6599 GNUNET_SCHEDULER_shutdown ();
6604 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6609 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6610 "mesh", "id announce time");
6611 GNUNET_SCHEDULER_shutdown ();
6616 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6621 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6622 "mesh", "connect timeout");
6623 GNUNET_SCHEDULER_shutdown ();
6628 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6631 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6633 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6634 "mesh", "max msgs queue");
6635 GNUNET_SCHEDULER_shutdown ();
6640 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6645 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6646 "mesh", "max tunnels");
6647 GNUNET_SCHEDULER_shutdown ();
6652 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6657 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6658 "mesh", "default ttl", 64);
6663 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6667 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6668 "mesh", "max peers", 1000);
6673 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6680 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6681 "Mesh is running with drop mode enabled. "
6682 "This is NOT a good idea! "
6683 "Remove the DROP_PERCENT option from your configuration.\n");
6687 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6688 &dht_replication_level))
6690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6692 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6693 "mesh", "dht replication level", 3);
6694 dht_replication_level = 3;
6697 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6698 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6699 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6701 dht_handle = GNUNET_DHT_connect (c, 64);
6702 if (NULL == dht_handle)
6706 stats = GNUNET_STATISTICS_create ("mesh", c);
6708 /* Scheduled the task to clean up when shutdown is called */
6709 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6711 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6712 GNUNET_free (keyfile);
6713 GNUNET_assert (NULL != pk);
6714 my_private_key = pk;
6715 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6716 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6717 &my_full_id.hashPubKey);
6718 myid = GNUNET_PEER_intern (&my_full_id);
6719 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6720 "Mesh for peer [%s] starting\n",
6721 GNUNET_i2s(&my_full_id));
6723 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6724 NULL, /* Closure passed to MESH functions */
6725 &core_init, /* Call core_init once connected */
6726 &core_connect, /* Handle connects */
6727 &core_disconnect, /* remove peers on disconnects */
6728 NULL, /* Don't notify about all incoming messages */
6729 GNUNET_NO, /* For header only in notification */
6730 NULL, /* Don't notify about all outbound messages */
6731 GNUNET_NO, /* For header-only out notification */
6732 core_handlers); /* Register these handlers */
6733 if (NULL == core_handle)
6736 GNUNET_SCHEDULER_shutdown ();
6739 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6745 * The main function for the mesh service.
6747 * @param argc number of arguments from the command line
6748 * @param argv command line arguments
6749 * @return 0 ok, 1 on error
6752 main (int argc, char *const *argv)
6757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6758 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6760 ret = (GNUNET_OK == r) ? 0 : 1;
6761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");