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;
526 struct MeshConnection
531 struct MeshConnection *next;
532 struct MeshConnection *prev;
535 * Tunnel this connection is part of.
537 struct MeshTunnel2 *t;
540 * Flow control information for traffic fwd.
542 struct MeshFlowControl fwd_fc;
545 * Flow control information for traffic bck.
547 struct MeshFlowControl bck_fc;
555 * State of the connection.
557 enum MeshConnectionState state;
560 * Path being used for the tunnel.
562 struct MeshPeerPath *path;
565 * Position of the local peer in the path.
567 unsigned int own_pos;
570 * Task to keep the used paths alive at the owner,
571 * time tunnel out on all the other peers.
573 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
576 * Task to keep the used paths alive at the destination,
577 * time tunnel out on all the other peers.
579 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
582 * Pending message count.
584 int pending_messages;
587 * Destroy flag: if true, destroy on last message.
594 * Struct containing all information regarding a tunnel to a peer.
599 * Endpoint of the tunnel.
601 struct MeshPeer *peer;
606 struct GNUNET_HashCode id;
609 * State of the tunnel.
611 enum MeshTunnelState state;
614 * Local peer ephemeral private key
616 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
619 * Local peer ephemeral public key
621 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
624 * Remote peer's public key.
626 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
629 * Encryption ("our") key.
631 struct GNUNET_CRYPTO_AesSessionKey e_key;
634 * Decryption ("their") key.
636 struct GNUNET_CRYPTO_AesSessionKey d_key;
639 * Paths that are actively used to reach the destination peer.
641 struct MeshConnection *connection_head;
642 struct MeshConnection *connection_tail;
645 * Next connection number.
650 * Channels inside this tunnel.
652 struct MeshChannel *channel_head;
653 struct MeshChannel *channel_tail;
656 * Channel ID for the next created channel.
658 MESH_ChannelNumber next_chid;
661 * Channel ID for the next incoming channel.
663 MESH_ChannelNumber next_local_chid;
666 * Pending message count.
668 int pending_messages;
671 * Destroy flag: if true, destroy on last message.
679 * Struct containing information about a client of the service
681 * TODO: add a list of 'waiting' ports
688 struct MeshClient *next;
693 struct MeshClient *prev;
696 * Tunnels that belong to this client, indexed by local id
698 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
701 * Tunnels this client has accepted, indexed by incoming local id
703 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
706 * Handle to communicate with the client
708 struct GNUNET_SERVER_Client *handle;
711 * Ports that this client has declared interest in.
712 * Indexed by port, contains *Client.
714 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
717 * Whether the client is active or shutting down (don't send confirmations
718 * to a client that is shutting down.
723 * ID of the client, mainly for debug messages
729 /******************************************************************************/
730 /************************ DEBUG FUNCTIONS ****************************/
731 /******************************************************************************/
735 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
736 * @param cls string to print
737 * @param success GNUNET_OK if the PUT was transmitted,
738 * GNUNET_NO on timeout,
739 * GNUNET_SYSERR on disconnect from service
740 * after the PUT message was transmitted
741 * (so we don't know if it was received or not)
746 mesh_debug (void *cls, int success)
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
756 /******************************************************************************/
757 /*********************** GLOBAL VARIABLES ****************************/
758 /******************************************************************************/
760 /************************** Configuration parameters **************************/
763 * How often to send path keepalives. Paths timeout after 4 missed.
765 static struct GNUNET_TIME_Relative refresh_connection_time;
768 * How often to PUT own ID in the DHT.
770 static struct GNUNET_TIME_Relative id_announce_time;
773 * Maximum time allowed to connect to a peer found by string.
775 static struct GNUNET_TIME_Relative connect_timeout;
778 * Default TTL for payload packets.
780 static unsigned long long default_ttl;
783 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
785 static unsigned long long dht_replication_level;
788 * How many tunnels are we willing to maintain.
789 * Local tunnels are always allowed, even if there are more tunnels than max.
791 static unsigned long long max_tunnels;
794 * How many messages *in total* are we willing to queue, divided by number of
795 * tunnels to get tunnel queue size.
797 static unsigned long long max_msgs_queue;
800 * How many peers do we want to remember?
802 static unsigned long long max_peers;
805 * Percentage of messages that will be dropped (for test purposes only).
807 static unsigned long long drop_percent;
809 /*************************** Static global variables **************************/
812 * DLL with all the clients, head.
814 static struct MeshClient *clients_head;
817 * DLL with all the clients, tail.
819 static struct MeshClient *clients_tail;
822 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
824 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
827 * Peers known, indexed by PeerIdentity (MeshPeer).
829 static struct GNUNET_CONTAINER_MultiHashMap *peers;
832 * Handle to communicate with core.
834 static struct GNUNET_CORE_Handle *core_handle;
839 static struct GNUNET_DHT_Handle *dht_handle;
842 * Handle to server lib.
844 static struct GNUNET_SERVER_Handle *server_handle;
847 * Handle to the statistics service.
849 static struct GNUNET_STATISTICS_Handle *stats;
852 * Notification context, to send messages to local clients.
854 static struct GNUNET_SERVER_NotificationContext *nc;
857 * Local peer own ID (memory efficient handle).
859 static GNUNET_PEER_Id myid;
862 * Local peer own ID (full value).
864 static struct GNUNET_PeerIdentity my_full_id;
869 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
874 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
877 * All ports clients of this peer have opened.
879 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
882 * Task to periodically announce itself in the network.
884 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
887 * Next ID to assign to a client.
889 unsigned int next_client_id;
892 /******************************************************************************/
893 /*********************** DECLARATIONS **************************/
894 /******************************************************************************/
897 * Function to process paths received for a new peer addition. The recorded
898 * paths form the initial tunnel, which can be optimized later.
899 * Called on each result obtained for the DHT search.
902 * @param exp when will this value expire
903 * @param key key of the result
904 * @param type type of the result
905 * @param size number of bytes in data
906 * @param data pointer to the result data
909 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
910 const struct GNUNET_HashCode * key,
911 const struct GNUNET_PeerIdentity *get_path,
912 unsigned int get_path_length,
913 const struct GNUNET_PeerIdentity *put_path,
914 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
915 size_t size, const void *data);
919 * Retrieve the MeshPeer stucture associated with the peer, create one
920 * and insert it in the appropriate structures if the peer is not known yet.
922 * @param peer Full identity of the peer.
924 * @return Existing or newly created peer info.
926 static struct MeshPeer *
927 peer_get (const struct GNUNET_PeerIdentity *peer);
931 * Retrieve the MeshPeer stucture associated with the peer, create one
932 * and insert it in the appropriate structures if the peer is not known yet.
934 * @param peer Short identity of the peer.
936 * @return Existing or newly created peer info.
938 static struct MeshPeer *
939 peer_get_short (const GNUNET_PEER_Id peer);
943 * Build a PeerPath from the paths returned from the DHT, reversing the paths
944 * to obtain a local peer -> destination path and interning the peer ids.
946 * @return Newly allocated and created path
948 static struct MeshPeerPath *
949 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
950 unsigned int get_path_length,
951 const struct GNUNET_PeerIdentity *put_path,
952 unsigned int put_path_length);
956 * Adds a path to the data structs of all the peers in the path
958 * @param p Path to process.
959 * @param confirmed Whether we know if the path works or not.
962 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
966 * Search for a tunnel by global ID using full PeerIdentities.
968 * @param t Tunnel containing the channel.
969 * @param chid Public channel number.
971 * @return channel handler, NULL if doesn't exist
973 static struct MeshChannel *
974 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
978 * Change the tunnel state.
980 * @param t Tunnel whose state to change.
981 * @param state New state.
984 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
988 * Notify a tunnel that a connection has broken that affects at least
991 * @param t Tunnel affected.
992 * @param p1 Peer that got disconnected from p2.
993 * @param p2 Peer that got disconnected from p1.
995 * @return Short ID of the peer disconnected (either p1 or p2).
996 * 0 if the tunnel remained unaffected.
998 static GNUNET_PEER_Id
999 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1000 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1003 * @brief Use the given path for the tunnel.
1004 * Update the next and prev hops (and RCs).
1005 * (Re)start the path refresh in case the tunnel is locally owned.
1007 * @param t Tunnel to update.
1008 * @param p Path to use.
1010 * @return Connection created.
1012 static struct MeshConnection *
1013 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1016 * Tunnel is empty: destroy it.
1018 * Notifies all participants (peers, cleints) about the destruction.
1020 * @param t Tunnel to destroy.
1023 tunnel_destroy_empty (struct MeshTunnel2 *t);
1026 * Destroy the tunnel.
1028 * This function does not generate any warning traffic to clients or peers.
1031 * Cancel messages belonging to this tunnel queued to neighbors.
1032 * Free any allocated resources linked to the tunnel.
1034 * @param t The tunnel to destroy.
1037 tunnel_destroy (struct MeshTunnel2 *t);
1040 * Send FWD keepalive packets for a connection.
1042 * @param cls Closure (connection for which to send the keepalive).
1043 * @param tc Notification context.
1046 connection_fwd_keepalive (void *cls,
1047 const struct GNUNET_SCHEDULER_TaskContext *tc);
1050 * Send BCK keepalive packets for a connection.
1052 * @param cls Closure (connection for which to send the keepalive).
1053 * @param tc Notification context.
1056 connection_bck_keepalive (void *cls,
1057 const struct GNUNET_SCHEDULER_TaskContext *tc);
1061 * Change the tunnel state.
1063 * @param c Connection whose state to change.
1064 * @param state New state.
1067 connection_change_state (struct MeshConnection* c,
1068 enum MeshConnectionState state);
1073 * @brief Queue and pass message to core when possible.
1075 * @param cls Closure (@c type dependant). It will be used by queue_send to
1076 * build the message to be sent if not already prebuilt.
1077 * @param type Type of the message, 0 for a raw message.
1078 * @param size Size of the message.
1079 * @param dst Neighbor to send message to.
1080 * @param c Connection this message belongs to, if any.
1081 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1084 queue_add (void *cls, uint16_t type, size_t size,
1085 struct MeshPeer *dst,
1086 struct MeshConnection *c,
1087 struct MeshChannel *ch);
1091 * Free a transmission that was already queued with all resources
1092 * associated to the request.
1094 * @param queue Queue handler to cancel.
1095 * @param clear_cls Is it necessary to free associated cls?
1098 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1102 * Core callback to write a queued packet to core buffer
1104 * @param cls Closure (peer info).
1105 * @param size Number of bytes available in buf.
1106 * @param buf Where the to write the message.
1108 * @return number of bytes written to buf
1111 queue_send (void *cls, size_t size, void *buf);
1115 * Dummy function to separate declarations from definitions in function list.
1118 __mesh_divider______________________________________________________________();
1122 * Get string description for tunnel state.
1124 * @param s Tunnel state.
1126 * @return String representation.
1129 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1131 static char buf[128];
1135 case MESH_TUNNEL_NEW:
1136 return "MESH_TUNNEL_NEW";
1137 case MESH_TUNNEL_SEARCHING:
1138 return "MESH_TUNNEL_SEARCHING";
1139 case MESH_TUNNEL_WAITING:
1140 return "MESH_TUNNEL_WAITING";
1141 case MESH_TUNNEL_READY:
1142 return "MESH_TUNNEL_READY";
1143 case MESH_TUNNEL_RECONNECTING:
1144 return "MESH_TUNNEL_RECONNECTING";
1147 sprintf (buf, "%u (UNKNOWN STATE)", s);
1154 * Get string description for tunnel state.
1156 * @param s Tunnel state.
1158 * @return String representation.
1161 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1165 case MESH_CONNECTION_NEW:
1166 return "MESH_CONNECTION_NEW";
1167 case MESH_CONNECTION_SENT:
1168 return "MESH_CONNECTION_SENT";
1169 case MESH_CONNECTION_READY:
1170 return "MESH_CONNECTION_READY";
1172 return "MESH_CONNECTION_STATE_ERROR";
1178 /******************************************************************************/
1179 /************************ PERIODIC FUNCTIONS ****************************/
1180 /******************************************************************************/
1183 * Periodically announce self id in the DHT
1185 * @param cls closure
1186 * @param tc task context
1189 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1191 struct PBlock block;
1193 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1195 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1199 * - Set data expiration in function of X
1200 * - Adapt X to churn
1202 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1204 block.id = my_full_id;
1205 GNUNET_DHT_put (dht_handle, /* DHT handle */
1206 &my_full_id.hashPubKey, /* Key to use */
1207 dht_replication_level, /* Replication level */
1208 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1209 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1210 sizeof (block), /* Size of the data */
1211 (const char *) &block, /* Data itself */
1212 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1213 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1214 NULL, /* Continuation */
1215 NULL); /* Continuation closure */
1217 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1221 /******************************************************************************/
1222 /****************** GENERAL HELPER FUNCTIONS ************************/
1223 /******************************************************************************/
1227 * Get the previous hop in a connection
1229 * @param c Connection.
1231 * @return Previous peer in the connection.
1233 static struct MeshPeer *
1234 connection_get_prev_hop (struct MeshConnection *c)
1238 if (0 == c->own_pos || c->path->length < 2)
1239 id = c->path->peers[0];
1241 id = c->path->peers[c->own_pos - 1];
1243 return peer_get_short (id);
1248 * Get the next hop in a connection
1250 * @param c Connection.
1252 * @return Next peer in the connection.
1254 static struct MeshPeer *
1255 connection_get_next_hop (struct MeshConnection *c)
1259 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1260 id = c->path->peers[c->path->length - 1];
1262 id = c->path->peers[c->own_pos + 1];
1264 return peer_get_short (id);
1269 * Get the hop in a connection.
1271 * @param c Connection.
1272 * @param fwd Next hop?
1274 * @return Next peer in the connection.
1276 static struct MeshPeer *
1277 connection_get_hop (struct MeshConnection *c, int fwd)
1280 return connection_get_next_hop (c);
1281 return connection_get_prev_hop (c);
1285 * Check if client has registered with the service and has not disconnected
1287 * @param client the client to check
1289 * @return non-NULL if client exists in the global DLL
1291 static struct MeshClient *
1292 client_get (struct GNUNET_SERVER_Client *client)
1294 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1299 * Deletes a tunnel from a client (either owner or destination).
1301 * @param c Client whose tunnel to delete.
1302 * @param ch Channel which should be deleted.
1305 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1311 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1313 if (GNUNET_YES != res)
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1318 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1320 if (GNUNET_YES != res)
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1327 * Notify the appropriate client that a new incoming channel was created.
1329 * @param ch Channel that was created.
1332 send_local_channel_create (struct MeshChannel *ch)
1334 struct GNUNET_MESH_ChannelMessage msg;
1335 struct MeshTunnel2 *t = ch->t;
1337 if (NULL == ch->dest)
1339 msg.header.size = htons (sizeof (msg));
1340 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1341 msg.channel_id = htonl (ch->lid_dest);
1342 msg.port = htonl (ch->port);
1344 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1345 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1346 msg.opt = htonl (msg.opt);
1347 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1348 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1349 &msg.header, GNUNET_NO);
1354 * Notify a client that the incoming tunnel is no longer valid.
1356 * @param ch Channel that is destroyed.
1357 * @param fwd Forward notification (owner->dest)?
1360 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1362 struct GNUNET_MESH_ChannelMessage msg;
1363 struct MeshClient *c;
1365 c = fwd ? ch->dest : ch->root;
1371 msg.header.size = htons (sizeof (msg));
1372 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1373 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1374 msg.port = htonl (0);
1375 memset (&msg.peer, 0, sizeof (msg.peer));
1376 msg.opt = htonl (0);
1377 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1378 &msg.header, GNUNET_NO);
1383 * Build a local ACK message and send it to a local client.
1385 * @param ch Channel on which to send the ACK.
1386 * @param c Client to whom send the ACK.
1387 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1390 send_local_ack (struct MeshChannel *ch,
1391 struct MeshClient *c,
1394 struct GNUNET_MESH_LocalAck msg;
1396 msg.header.size = htons (sizeof (msg));
1397 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1398 msg.channel_id = htonl (is_fwd ? ch->lid_root : ch->lid_dest);
1399 GNUNET_SERVER_notification_context_unicast (nc,
1407 * Count established (ready) connections of a tunnel.
1409 * @param t Tunnel on which to send the message.
1411 * @return Number of connections.
1414 tunnel_count_connections (struct MeshTunnel2 *t)
1416 struct MeshConnection *c;
1419 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1426 * Pick a connection on which send the next data message.
1428 * @param t Tunnel on which to send the message.
1429 * @param fwd Is this a fwd message?
1431 * @return The connection on which to send the next message.
1433 static struct MeshConnection *
1434 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1436 struct MeshConnection *c;
1437 struct MeshConnection *best;
1438 struct MeshFlowControl *fc;
1439 unsigned int lowest_q;
1442 lowest_q = UINT_MAX;
1443 for (c = t->connection_head; NULL != c; c = c->next)
1445 if (MESH_CONNECTION_READY == c->state)
1447 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1453 if (fc->queue_n < lowest_q)
1456 lowest_q = fc->queue_n;
1465 * Get the total buffer space for a tunnel
1468 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1470 struct MeshConnection *c;
1471 struct MeshFlowControl *fc;
1472 unsigned int buffer;
1474 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1476 if (c->state != MESH_CONNECTION_READY)
1479 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1480 buffer += fc->last_ack_recv - fc->last_pid_sent;
1488 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1489 * Encrypt data with the tunnel key.
1491 * @param t Tunnel whose key to use.
1492 * @param dst Destination for the encrypted data.
1493 * @param src Source of the plaintext.
1494 * @param size Size of the plaintext.
1495 * @param iv Initialization Vector to use.
1496 * @param fwd Is this a fwd message?
1499 tunnel_encrypt (struct MeshTunnel2 *t,
1500 void *dst, const void *src,
1501 size_t size, uint64_t iv, int fwd)
1503 memcpy (dst, src, size);
1508 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1509 * Decrypt data with the tunnel key.
1511 * @param t Tunnel whose key to use.
1512 * @param dst Destination for the plaintext.
1513 * @param src Source of the encrypted data.
1514 * @param size Size of the encrypted data.
1515 * @param iv Initialization Vector to use.
1516 * @param fwd Is this a fwd message?
1519 tunnel_decrypt (struct MeshTunnel2 *t,
1520 void *dst, const void *src,
1521 size_t size, uint64_t iv, int fwd)
1523 memcpy (dst, src, size);
1528 * Sends an already built message on a connection, properly registering
1529 * all used resources.
1531 * @param message Message to send. Function makes a copy of it.
1532 * If message is not hop-by-hop, decrements TTL of copy.
1533 * @param c Connection on which this message is transmitted.
1534 * @param ch Channel on which this message is transmitted, or NULL.
1535 * @param fwd Is this a fwd message?
1538 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1539 struct MeshConnection *c,
1540 struct MeshChannel *ch,
1543 struct MeshPeer *neighbor;
1548 neighbor = connection_get_hop (c, fwd);
1549 if (NULL == neighbor)
1555 size = ntohs (message->size);
1556 data = GNUNET_malloc (size);
1557 memcpy (data, message, size);
1558 type = ntohs(message->type);
1560 if (GNUNET_MESSAGE_TYPE_MESH_FWD == type ||
1561 GNUNET_MESSAGE_TYPE_MESH_BCK == type)
1563 struct GNUNET_MESH_Encrypted *msg;
1566 msg = (struct GNUNET_MESH_Encrypted *) data;
1567 ttl = ntohl (msg->ttl);
1570 GNUNET_break_op (0);
1573 msg->ttl = htonl (ttl - 1);
1586 * Sends an already built message on a tunnel, choosing the best connection.
1588 * @param message Message to send. Function modifies it.
1589 * @param t Tunnel on which this message is transmitted.
1590 * @param ch Channel on which this message is transmitted.
1591 * @param fwd Is this a fwd message?
1594 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1595 struct MeshTunnel2 *t,
1596 struct MeshChannel *ch,
1599 struct MeshConnection *c;
1602 c = tunnel_get_connection (t, fwd);
1608 type = ntohs (msg->header.size);
1611 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1612 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1613 msg->cid = htonl (c->id);
1615 msg->ttl = default_ttl;
1621 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1626 * Sends an already built message on a channel, properly registering
1627 * all used resources and encrypting the message with the tunnel's key.
1629 * @param message Message to send. Function makes a copy of it.
1630 * @param ch Channel on which this message is transmitted.
1631 * @param fwd Is this a fwd message?
1634 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1635 struct MeshChannel *ch,
1638 struct GNUNET_MESH_Encrypted *msg;
1639 size_t size = ntohs (message->size);
1640 char *cbuf[size + sizeof (struct GNUNET_MESH_Encrypted)];
1644 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1645 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1647 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1648 msg->header.type = htons (type);
1649 msg->header.size = htons (size);
1650 msg->iv = GNUNET_htonll (iv);
1651 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1653 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1658 * Sends a CREATE CONNECTION message for a path to a peer.
1659 * Changes the connection and tunnel states if necessary.
1661 * @param connection Connection to create.
1664 send_connection_create (struct MeshConnection *connection)
1666 struct MeshPeer *neighbor;
1667 struct MeshTunnel2 *t;
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1671 neighbor = connection_get_next_hop (connection);
1672 queue_add (connection,
1673 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1674 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1675 (connection->path->length *
1676 sizeof (struct GNUNET_PeerIdentity)),
1680 if (MESH_TUNNEL_SEARCHING == t->state)
1681 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1682 if (MESH_CONNECTION_NEW == connection->state)
1683 connection_change_state (connection, MESH_CONNECTION_SENT);
1688 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1691 * @param connection Connection to confirm.
1694 send_connection_ack (struct MeshConnection *connection)
1696 struct MeshPeer *neighbor;
1697 struct MeshTunnel2 *t;
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1701 neighbor = connection_get_prev_hop (connection);
1702 queue_add (connection,
1703 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1704 sizeof (struct GNUNET_MESH_ConnectionACK),
1708 if (MESH_TUNNEL_NEW == t->state)
1709 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1714 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1716 * @param c Which connection to send the hop-by-hop ACK.
1717 * @param ack Value of the ACK.
1718 * @param fwd Is this fwd?
1721 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1723 struct GNUNET_MESH_ACK msg;
1725 msg.header.size = htons (sizeof (msg));
1726 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1727 msg.ack = htonl (ack);
1729 msg.cid = htonl (c->id);
1731 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1736 * Core callback to write a pre-constructed data packet to core buffer
1738 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1739 * @param size Number of bytes available in buf.
1740 * @param buf Where the to write the message.
1742 * @return number of bytes written to buf
1745 send_core_data_raw (void *cls, size_t size, void *buf)
1747 struct GNUNET_MessageHeader *msg = cls;
1750 GNUNET_assert (NULL != msg);
1751 total_size = ntohs (msg->size);
1753 if (total_size > size)
1758 memcpy (buf, msg, total_size);
1765 * Function to send a create path packet to a peer.
1767 * @param cls closure
1768 * @param size number of bytes available in buf
1769 * @param buf where the callee should write the message
1770 * @return number of bytes written to buf
1773 send_core_connection_create (void *cls, size_t size, void *buf)
1775 struct MeshConnection *c = cls;
1776 struct GNUNET_MESH_ConnectionCreate *msg;
1777 struct GNUNET_PeerIdentity *peer_ptr;
1778 struct MeshPeerPath *p = c->path;
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1784 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1785 p->length * sizeof (struct GNUNET_PeerIdentity);
1787 if (size < size_needed || NULL == buf)
1792 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1793 msg->header.size = htons (size_needed);
1794 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1795 msg->cid = htonl (c->id);
1797 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1798 for (i = 0; i < p->length; i++)
1800 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1810 * Creates a path ack message in buf and frees all unused resources.
1812 * @param cls closure (MeshTransmissionDescriptor)
1813 * @param size number of bytes available in buf
1814 * @param buf where the callee should write the message
1815 * @return number of bytes written to buf
1818 send_core_connection_ack (void *cls, size_t size, void *buf)
1820 struct GNUNET_MESH_ConnectionACK *msg = buf;
1821 struct MeshConnection *c = cls;
1822 struct MeshTunnel2 *t = c->t;
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1825 GNUNET_assert (NULL != t);
1826 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1831 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1832 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1833 GNUNET_CRYPTO_hash_xor (&GNUNET_PEER_resolve2 (t->peer->id)->hashPubKey,
1834 &my_full_id.hashPubKey,
1836 msg->cid = htonl (c->id);
1838 /* TODO add signature */
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1841 return sizeof (struct GNUNET_MESH_ConnectionACK);
1846 * Destroy the peer_info and free any allocated resources linked to it
1848 * @param peer The peer_info to destroy.
1850 * @return GNUNET_OK on success
1853 peer_destroy (struct MeshPeer *peer)
1855 struct GNUNET_PeerIdentity id;
1856 struct MeshPeerPath *p;
1857 struct MeshPeerPath *nextp;
1859 GNUNET_PEER_resolve (peer->id, &id);
1860 GNUNET_PEER_change_rc (peer->id, -1);
1863 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
1866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1867 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1869 if (NULL != peer->dhtget)
1871 GNUNET_DHT_get_stop (peer->dhtget);
1873 p = peer->path_head;
1877 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1881 tunnel_destroy_empty (peer->tunnel);
1888 * Returns if peer is used (has a tunnel, is neighbor).
1890 * @peer Peer to check.
1892 * @return GNUNET_YES if peer is in use.
1895 peer_is_used (struct MeshPeer *peer)
1897 struct MeshPeerPath *p;
1899 if (NULL != peer->tunnel)
1902 for (p = peer->path_head; NULL != p; p = p->next)
1911 * Iterator over all the peers to get the oldest timestamp.
1913 * @param cls Closure (unsued).
1914 * @param key ID of the peer.
1915 * @param value Peer_Info of the peer.
1918 peer_get_oldest (void *cls,
1919 const struct GNUNET_HashCode *key,
1922 struct MeshPeer *p = value;
1923 struct GNUNET_TIME_Absolute *abs = cls;
1925 /* Don't count active peers */
1926 if (GNUNET_YES == peer_is_used (p))
1929 if (abs->abs_value_us < p->last_contact.abs_value_us)
1930 abs->abs_value_us = p->last_contact.abs_value_us;
1937 * Iterator over all the peers to remove the oldest entry.
1939 * @param cls Closure (unsued).
1940 * @param key ID of the peer.
1941 * @param value Peer_Info of the peer.
1944 peer_timeout (void *cls,
1945 const struct GNUNET_HashCode *key,
1948 struct MeshPeer *p = value;
1949 struct GNUNET_TIME_Absolute *abs = cls;
1951 if (p->last_contact.abs_value_us == abs->abs_value_us &&
1952 GNUNET_NO == peer_is_used (p))
1962 * Delete oldest unused peer.
1965 peer_delete_oldest (void)
1967 struct GNUNET_TIME_Absolute abs;
1969 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
1971 GNUNET_CONTAINER_multihashmap_iterate (peers,
1974 GNUNET_CONTAINER_multihashmap_iterate (peers,
1981 * Get the static string for a peer ID.
1985 * @return Static string for it's ID.
1988 peer2s (const struct MeshPeer *peer)
1990 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1995 * Retrieve the MeshPeer stucture associated with the peer, create one
1996 * and insert it in the appropriate structures if the peer is not known yet.
1998 * @param peer Full identity of the peer.
2000 * @return Existing or newly created peer info.
2002 static struct MeshPeer *
2003 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2005 struct MeshPeer *peer;
2007 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2010 peer = GNUNET_new (struct MeshPeer);
2011 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2013 peer_delete_oldest ();
2015 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2016 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2017 peer->id = GNUNET_PEER_intern (peer_id);
2019 peer->last_contact = GNUNET_TIME_absolute_get();
2026 * Retrieve the MeshPeer stucture associated with the peer, create one
2027 * and insert it in the appropriate structures if the peer is not known yet.
2029 * @param peer Short identity of the peer.
2031 * @return Existing or newly created peer info.
2033 static struct MeshPeer *
2034 peer_get_short (const GNUNET_PEER_Id peer)
2036 return peer_get (GNUNET_PEER_resolve2 (peer));
2041 * Get a cost of a path for a peer considering existing tunnel connections.
2043 * @param peer Peer towards which the path is considered.
2044 * @param path Candidate path.
2046 * @return Cost of the path (path length + number of overlapping nodes)
2049 peer_get_path_cost (const struct MeshPeer *peer,
2050 const struct MeshPeerPath *path)
2052 struct MeshConnection *c;
2053 unsigned int overlap;
2061 GNUNET_assert (NULL != peer->tunnel);
2063 for (i = 0; i < path->length; i++)
2065 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2067 for (j = 0; j < c->path->length; j++)
2069 if (path->peers[i] == c->path->peers[j])
2077 return (path->length + overlap) * (path->score * -1);
2082 * Choose the best path towards a peer considering the tunnel properties.
2084 * @param peer The destination peer.
2086 * @return Best current known path towards the peer, if any.
2088 static struct MeshPeerPath *
2089 peer_get_best_path (const struct MeshPeer *peer)
2091 struct MeshPeerPath *best_p;
2092 struct MeshPeerPath *p;
2093 struct MeshConnection *c;
2094 unsigned int best_cost;
2097 best_cost = UINT_MAX;
2099 for (p = peer->path_head; NULL != p; p = p->next)
2101 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2105 continue; /* If path is in use in a connection, skip it. */
2107 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2117 queue_is_sendable (struct MeshPeerQueue *q)
2119 struct MeshFlowControl *fc;
2121 /* Is PID-independent? */
2124 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2125 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2129 /* Is PID allowed? */
2130 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2131 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2139 * Get first sendable message.
2141 * @param peer The destination peer.
2143 * @return Best current known path towards the peer, if any.
2145 static struct MeshPeerQueue *
2146 peer_get_first_message (const struct MeshPeer *peer)
2148 struct MeshPeerQueue *q;
2150 for (q = peer->queue_head; NULL != q; q = q->next)
2152 if (queue_is_sendable (q))
2161 * Try to establish a new connection to this peer in the given tunnel.
2162 * If the peer doesn't have any path to it yet, try to get one.
2163 * If the peer already has some path, send a CREATE CONNECTION towards it.
2165 * @param peer PeerInfo of the peer.
2168 peer_connect (struct MeshPeer *peer)
2170 struct MeshTunnel2 *t;
2171 struct MeshPeerPath *p;
2172 struct MeshConnection *c;
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "peer_connect towards %s\n",
2178 if (NULL != peer->path_head)
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2181 p = peer_get_best_path (peer);
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2185 c = tunnel_use_path (t, p);
2186 send_connection_create (c);
2189 else if (NULL == peer->dhtget)
2191 const struct GNUNET_PeerIdentity *id;
2193 id = GNUNET_PEER_resolve2 (peer->id);
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 " Starting DHT GET for peer %s\n", peer2s (peer));
2196 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2197 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2198 &id->hashPubKey, /* key to search */
2199 dht_replication_level, /* replication level */
2200 GNUNET_DHT_RO_RECORD_ROUTE |
2201 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2203 0, /* xquery bits */
2204 &dht_get_id_handler, peer);
2205 if (MESH_TUNNEL_NEW == t->state)
2206 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2211 "There is no path but the DHT GET is already started.\n");
2217 * Get the first transmittable message for a connection.
2219 * @param c Connection.
2220 * @param fwd Is this FWD?
2222 * @return First transmittable message.
2224 static struct MeshPeerQueue *
2225 connection_get_first_message (struct MeshConnection *c, int fwd)
2227 struct MeshPeerQueue *q;
2230 p = connection_get_hop (c, fwd);
2232 for (q = p->queue_head; NULL != q; q = q->next)
2236 if (queue_is_sendable (q))
2246 * @brief Re-initiate traffic on this connection if necessary.
2248 * Check if there is traffic queued towards this peer
2249 * and the core transmit handle is NULL (traffic was stalled).
2250 * If so, call core tmt rdy.
2252 * @param c Connection on which initiate traffic.
2253 * @param fwd Is this about fwd traffic?
2256 connection_unlock_queue (struct MeshConnection *c, int fwd)
2258 struct MeshPeer *peer;
2259 struct MeshPeerQueue *q;
2262 peer = connection_get_hop (c, fwd);
2264 if (NULL != peer->core_transmit)
2265 return; /* Already unlocked */
2267 q = connection_get_first_message (c, fwd);
2269 return; /* Nothing to transmit */
2272 peer->core_transmit =
2273 GNUNET_CORE_notify_transmit_ready (core_handle,
2276 GNUNET_TIME_UNIT_FOREVER_REL,
2277 GNUNET_PEER_resolve2 (peer->id),
2285 * Cancel all transmissions that belong to a certain connection.
2287 * @param c Connection which to cancel.
2288 * @param fwd Cancel fwd traffic?
2291 connection_cancel_queues (struct MeshConnection *c, int fwd)
2293 struct MeshPeerQueue *q;
2294 struct MeshPeerQueue *next;
2295 struct MeshFlowControl *fc;
2296 struct MeshPeer *peer;
2303 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2304 peer = connection_get_hop (c, fwd);
2306 for (q = peer->queue_head; NULL != q; q = next)
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "connection_cancel_queue %s\n",
2313 GNUNET_MESH_DEBUG_M2S (q->type));
2314 queue_destroy (q, GNUNET_YES);
2317 if (NULL == peer->queue_head)
2319 if (NULL != peer->core_transmit)
2321 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2322 peer->core_transmit = NULL;
2324 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2326 GNUNET_SCHEDULER_cancel (fc->poll_task);
2327 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2334 * Remove all paths that rely on a direct connection between p1 and p2
2335 * from the peer itself and notify all tunnels about it.
2337 * @param peer PeerInfo of affected peer.
2338 * @param p1 GNUNET_PEER_Id of one peer.
2339 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2342 * TODO: optimize (see below)
2345 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2348 struct MeshPeerPath *p;
2349 struct MeshPeerPath *next;
2350 struct MeshPeer *peer_d;
2352 unsigned int destroyed;
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2357 for (p = peer->path_head; NULL != p; p = next)
2360 for (i = 0; i < (p->length - 1); i++)
2362 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2363 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2365 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2376 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2378 peer_d = peer_get_short (d); // FIXME
2379 next = peer_get_best_path (peer_d);
2380 tunnel_use_path (peer->tunnel, next);
2381 peer_connect (peer_d);
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2388 * Add the path to the peer and update the path used to reach it in case this
2391 * @param peer_info Destination peer to add the path to.
2392 * @param path New path to add. Last peer must be the peer in arg 1.
2393 * Path will be either used of freed if already known.
2394 * @param trusted Do we trust that this path is real?
2397 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2400 struct MeshPeerPath *aux;
2404 if ((NULL == peer_info) || (NULL == path))
2407 path_destroy (path);
2410 if (path->peers[path->length - 1] != peer_info->id)
2413 path_destroy (path);
2416 if (2 >= path->length && GNUNET_NO == trusted)
2418 /* Only allow CORE to tell us about direct paths */
2419 path_destroy (path);
2422 for (l = 1; l < path->length; l++)
2424 if (path->peers[l] == myid)
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2427 for (l2 = 0; l2 < path->length - l; l2++)
2429 path->peers[l2] = path->peers[l + l2];
2434 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2439 path->length, peer2s (peer_info));
2441 l = path_get_length (path);
2444 path_destroy (path);
2448 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2449 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2451 l2 = path_get_length (aux);
2454 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2455 peer_info->path_tail, aux, path);
2460 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2462 path_destroy (path);
2467 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2474 * Add the path to the origin peer and update the path used to reach it in case
2475 * this is the shortest.
2476 * The path is given in peer_info -> destination, therefore we turn the path
2477 * upside down first.
2479 * @param peer_info Peer to add the path to, being the origin of the path.
2480 * @param path New path to add after being inversed.
2481 * Path will be either used or freed.
2482 * @param trusted Do we trust that this path is real?
2485 peer_add_path_to_origin (struct MeshPeer *peer_info,
2486 struct MeshPeerPath *path, int trusted)
2489 peer_add_path (peer_info, path, trusted);
2495 * Function called if a connection has been stalled for a while,
2496 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2498 * @param cls Closure (poll ctx).
2499 * @param tc TaskContext.
2502 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2504 struct MeshFlowControl *fc = cls;
2505 struct GNUNET_MESH_Poll msg;
2506 struct MeshConnection *c;
2508 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2509 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2517 peer2s (c->t->peer), c->id);
2519 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2520 msg.header.size = htons (sizeof (msg));
2521 msg.pid = htonl (fc->last_pid_sent);
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2523 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2524 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2525 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2526 &connection_poll, fc);
2531 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2532 * to obtain a local peer -> destination path and interning the peer ids.
2534 * @return Newly allocated and created path
2536 static struct MeshPeerPath *
2537 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2538 unsigned int get_path_length,
2539 const struct GNUNET_PeerIdentity *put_path,
2540 unsigned int put_path_length)
2542 struct MeshPeerPath *p;
2548 GNUNET_PEER_change_rc (myid, 1);
2549 i = get_path_length;
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2551 for (i--; i >= 0; i--)
2553 id = GNUNET_PEER_intern (&get_path[i]);
2554 if (p->length > 0 && id == p->peers[p->length - 1])
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2557 GNUNET_PEER_change_rc (id, -1);
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2562 GNUNET_i2s (&get_path[i]));
2564 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2565 p->peers[p->length - 1] = id;
2568 i = put_path_length;
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2570 for (i--; i >= 0; i--)
2572 id = GNUNET_PEER_intern (&put_path[i]);
2575 /* PUT path went through us, so discard the path up until now and start
2576 * from here to get a much shorter (and loop-free) path.
2581 if (p->length > 0 && id == p->peers[p->length - 1])
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2584 GNUNET_PEER_change_rc (id, -1);
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2589 GNUNET_i2s (&put_path[i]));
2591 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2592 p->peers[p->length - 1] = id;
2596 if (get_path_length > 0)
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2598 GNUNET_i2s (&get_path[0]));
2599 if (put_path_length > 0)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2601 GNUNET_i2s (&put_path[0]));
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2604 for (i = 0; i < p->length; i++)
2606 struct GNUNET_PeerIdentity peer_id;
2608 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2610 GNUNET_i2s (&peer_id));
2618 * Adds a path to the peer_infos of all the peers in the path
2620 * @param p Path to process.
2621 * @param confirmed Whether we know if the path works or not.
2624 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2628 /* TODO: invert and add */
2629 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2630 for (i++; i < p->length; i++)
2632 struct MeshPeer *aux;
2633 struct MeshPeerPath *copy;
2635 aux = peer_get_short (p->peers[i]);
2636 copy = path_duplicate (p);
2637 copy->length = i + 1;
2638 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2644 * Search for a channel among the channels for a client
2646 * @param c the client whose channels to search in
2647 * @param chid the local id of the channel
2649 * @return channel handler, NULL if doesn't exist
2651 static struct MeshChannel *
2652 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2654 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2656 GNUNET_break_op (0);
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2660 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2661 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2662 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2666 * Search for a tunnel by global ID using full PeerIdentities.
2668 * @param t Tunnel containing the channel.
2669 * @param chid Public channel number.
2671 * @return channel handler, NULL if doesn't exist
2673 static struct MeshChannel *
2674 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2676 struct MeshChannel *ch;
2681 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2683 if (ch->gid == chid)
2692 * Change the tunnel state.
2694 * @param t Tunnel whose state to change.
2695 * @param state New state.
2698 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "Tunnel %s state was %s\n",
2703 GNUNET_MESH_DEBUG_TS2S (t->state));
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2705 "Tunnel %s state is now %s\n",
2707 GNUNET_MESH_DEBUG_TS2S (state));
2713 connection_change_state (struct MeshConnection* c,
2714 enum MeshConnectionState state)
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2717 "Connection %s[%X] state was %s\n",
2718 peer2s (c->t->peer), c->id,
2719 GNUNET_MESH_DEBUG_CS2S (c->state));
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2721 "Connection %s[%X] state is now %s\n",
2722 peer2s (c->t->peer), c->id,
2723 GNUNET_MESH_DEBUG_CS2S (state));
2729 * Add a client to a channel, initializing all needed data structures.
2731 * @param ch Channel to which add the client.
2732 * @param c Client which to add to the channel.
2735 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2737 struct MeshTunnel2 *t = ch->t;
2739 if (NULL != ch->dest)
2745 /* Assign local id as destination */
2746 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2747 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2748 ch->lid_dest = t->next_local_chid++;
2749 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2751 /* Store in client's hashmap */
2753 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2764 static struct MeshConnection *
2765 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2767 struct MeshConnection *c;
2768 unsigned int own_pos;
2770 c = GNUNET_new (struct MeshConnection);
2771 for (own_pos = 0; own_pos < p->length; own_pos++)
2773 if (p->peers[own_pos] == myid)
2776 if (own_pos > p->length - 1)
2781 c->own_pos = own_pos;
2783 c->id = t->next_cid++;
2785 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
2789 c->fwd_maintenance_task =
2790 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
2791 &connection_fwd_keepalive, c);
2798 * Notifies a tunnel that a connection has broken that affects at least
2799 * some of its peers. Sends a notification towards the root of the tree.
2800 * In case the peer is the owner of the tree, notifies the client that owns
2801 * the tunnel and tries to reconnect.
2803 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
2805 * @param t Tunnel affected.
2806 * @param p1 Peer that got disconnected from p2.
2807 * @param p2 Peer that got disconnected from p1.
2809 * @return Short ID of the peer disconnected (either p1 or p2).
2810 * 0 if the tunnel remained unaffected.
2812 static GNUNET_PEER_Id
2813 tunnel_notify_connection_broken (struct MeshTunnel2* t,
2814 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
2816 // if (myid != p1 && myid != p2) FIXME
2821 // if (tree_get_predecessor (t->tree) != 0)
2823 // /* We are the peer still connected, notify owner of the disconnection. */
2824 // struct GNUNET_MESH_PathBroken msg;
2825 // struct GNUNET_PeerIdentity neighbor;
2827 // msg.header.size = htons (sizeof (msg));
2828 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2829 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2830 // msg.tid = htonl (t->id.tid);
2831 // msg.peer1 = my_full_id;
2832 // GNUNET_PEER_resolve (pid, &msg.peer2);
2833 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2834 // send_prebuilt_message (&msg.header, &neighbor, t);
2841 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2843 * @param ch Channel this is about.
2844 * @param fwd Is for FWD traffic? (ACK dest->owner)
2847 channel_send_data_ack (struct MeshChannel *ch, int fwd)
2849 struct GNUNET_MESH_DataACK msg;
2850 struct MeshChannelReliability *rel;
2851 struct MeshReliableMessage *copy;
2856 if (GNUNET_NO == ch->reliable)
2861 rel = fwd ? ch->bck_rel : ch->fwd_rel;
2862 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2864 "send_data_ack for %u\n",
2867 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2868 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2869 msg.header.size = htons (sizeof (msg));
2870 msg.chid = htonl (ch->gid);
2871 msg.mid = htonl (*mid - 1);
2873 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2875 delta = copy->mid - *mid;
2878 mask = 0x1LL << delta;
2879 msg.futures |= mask;
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2881 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2882 copy->mid, delta, mask, msg.futures);
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2886 send_prebuilt_message_channel (&msg.header, ch, fwd);
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2892 * Send an ACK informing the predecessor about the available buffer space.
2894 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
2895 * the ACK itself goes "back" (dest->root).
2897 * @param c Connection on which to send the ACK.
2898 * @param fwd Is this FWD ACK? (Going dest->owner)
2901 connection_send_ack (struct MeshConnection *c, int fwd)
2903 struct MeshFlowControl *next_fc;
2904 struct MeshFlowControl *prev_fc;
2908 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
2909 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
2911 /* Check if we need to transmit the ACK */
2912 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2916 " last pid recv: %u, last ack sent: %u\n",
2917 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2921 /* Ok, ACK might be necessary, what PID to ACK? */
2922 delta = next_fc->queue_max - next_fc->queue_n;
2923 ack = prev_fc->last_pid_recv + delta;
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2926 " last pid %u, last ack %u, qmax %u, q %u\n",
2927 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2928 next_fc->queue_max, next_fc->queue_n);
2929 if (ack == prev_fc->last_ack_sent)
2931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2935 prev_fc->last_ack_sent = ack;
2936 send_ack (c, ack, fwd);
2941 * Send an ACK informing the client about available buffer space.
2943 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2944 * the ACK itself goes "back" (towards root).
2946 * @param ch Channel on which to send the ACK, NULL if unknown.
2947 * @param fwd Is this FWD ACK? (Going dest->owner)
2950 // channel_send_ack (struct MeshChannel *ch, uint16_t type, int fwd)
2952 // struct MeshChannelReliability *rel;
2953 // struct MeshFlowControl *next_fc;
2954 // struct MeshFlowControl *prev_fc;
2955 // struct MeshClient *c;
2956 // struct MeshClient *o;
2957 // GNUNET_PEER_Id hop;
2958 // uint32_t delta_mid;
2962 // rel = fwd ? ch->fwd_rel : ch->bck_rel;
2963 // c = fwd ? ch->client : ch->owner;
2964 // o = fwd ? ch->owner : ch->client;
2965 // hop = fwd ? connection_get_prev_hop (cn) : connection_get_next_hop (cn);
2966 // next_fc = fwd ? &t->next_fc : &t->prev_fc;
2967 // prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2971 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2972 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2973 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2974 // "ACK due to %s\n",
2975 // GNUNET_MESH_DEBUG_M2S (type));
2976 // if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2978 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2981 // if (GNUNET_YES == t->reliable && NULL != c)
2982 // tunnel_send_data_ack (t, fwd);
2984 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2985 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2986 // case GNUNET_MESSAGE_TYPE_MESH_ACK:
2987 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2989 // case GNUNET_MESSAGE_TYPE_MESH_POLL:
2990 // case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2991 // t->force_ack = GNUNET_YES;
2994 // GNUNET_break (0);
2997 // /* Check if we need to transmit the ACK */
2999 // prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
3000 // GNUNET_NO == t->force_ack)
3002 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3003 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3004 // " last pid recv: %u, last ack sent: %u\n",
3005 // prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3009 // /* Ok, ACK might be necessary, what PID to ACK? */
3010 // delta = t->queue_max - next_fc->queue_n;
3011 // if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
3012 // delta_mid = rel->mid_sent - rel->head_sent->mid;
3015 // if (0 > delta || (GNUNET_YES == t->reliable &&
3017 // (10 < rel->n_sent || 64 <= delta_mid)))
3019 // if (NULL != o && delta > 1)
3021 // ack = prev_fc->last_pid_recv + delta;
3022 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3023 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3024 // " last pid %u, last ack %u, qmax %u, q %u\n",
3025 // prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3026 // t->queue_max, next_fc->queue_n);
3027 // if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
3029 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3033 // prev_fc->last_ack_sent = ack;
3035 // send_local_ack (t, o, fwd);
3036 // else if (0 != hop)
3037 // send_ack (t, hop, ack);
3039 // GNUNET_break (GNUNET_YES == t->destroy);
3040 // t->force_ack = GNUNET_NO;
3045 * Modify the mesh message TID from global to local and send to client.
3047 * @param ch Channel on which to send the message.
3048 * @param msg Message to modify and send.
3049 * @param c Client to send to.
3050 * @param tid Tunnel ID to use (c can be both owner and client).
3053 channel_send_client_to_tid (struct MeshChannel *ch,
3054 const struct GNUNET_MESH_Data *msg,
3055 struct MeshClient *c, MESH_ChannelNumber id)
3057 struct GNUNET_MESH_LocalData *copy;
3058 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3059 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3061 if (size < sizeof (struct GNUNET_MessageHeader))
3063 GNUNET_break_op (0);
3071 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3072 memcpy (©[1], &msg[1], size);
3073 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3074 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3075 copy->id = htonl (id);
3076 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3077 ©->header, GNUNET_NO);
3081 * Modify the data message ID from global to local and send to client.
3083 * @param ch Channel on which to send the message.
3084 * @param msg Message to modify and send.
3085 * @param fwd Forward?
3088 channel_send_client_data (struct MeshChannel *ch,
3089 const struct GNUNET_MESH_Data *msg,
3093 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3095 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3100 * Send a buffered message to the client, for in order delivery or
3101 * as result of client ACK.
3103 * @param ch Channel on which to empty the message buffer.
3104 * @param c Client to send to.
3105 * @param rel Reliability structure to corresponding peer.
3106 * If rel == bck_rel, this is FWD data.
3109 channel_send_client_buffered_data (struct MeshChannel *ch,
3110 struct MeshClient *c,
3111 struct MeshChannelReliability *rel)
3113 struct MeshReliableMessage *copy;
3116 if (GNUNET_NO == rel->client_ready)
3118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3122 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3125 copy = rel->head_recv;
3128 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3130 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133 " have %u! now expecting %u\n",
3134 copy->mid, *mid + 1);
3135 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3138 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3144 " reliable && don't have %u, next is %u\n",
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3155 * We have received a message out of order, or the client is not ready.
3156 * Buffer it until we receive an ACK from the client or the missing
3157 * message from the channel.
3159 * @param msg Message to buffer.
3160 * @param rel Reliability data to the corresponding direction.
3163 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3164 struct MeshChannelReliability *rel)
3166 struct MeshReliableMessage *copy;
3167 struct MeshReliableMessage *prev;
3171 size = ntohs (msg->header.size);
3172 mid = ntohl (msg->mid);
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3175 copy = GNUNET_malloc (sizeof (*copy) + size);
3178 memcpy (©[1], msg, size);
3182 // FIXME do something better than O(n), although n < 64...
3183 // FIXME start from the end (most messages are the latest ones)
3184 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3187 if (GMC_is_pid_bigger (prev->mid, mid))
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3190 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3196 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3202 * Destroy a reliable message after it has been acknowledged, either by
3203 * direct mid ACK or bitfield. Updates the appropriate data structures and
3204 * timers and frees all memory.
3206 * @param copy Message that is no longer needed: remote peer got it.
3209 rel_message_free (struct MeshReliableMessage *copy)
3211 struct MeshChannelReliability *rel;
3212 struct GNUNET_TIME_Relative time;
3215 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3216 rel->expected_delay.rel_value_us *= 7;
3217 rel->expected_delay.rel_value_us += time.rel_value_us;
3218 rel->expected_delay.rel_value_us /= 8;
3220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3223 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3225 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3227 rel->retry_timer = rel->expected_delay;
3228 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3234 * Destroy all reliable messages queued for a channel,
3235 * during a channel destruction.
3236 * Frees the reliability structure itself.
3238 * @param rel Reliability data for a channel.
3241 channel_rel_free_all (struct MeshChannelReliability *rel)
3243 struct MeshReliableMessage *copy;
3244 struct MeshReliableMessage *next;
3249 for (copy = rel->head_recv; NULL != copy; copy = next)
3252 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3255 for (copy = rel->head_sent; NULL != copy; copy = next)
3258 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3261 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3262 GNUNET_SCHEDULER_cancel (rel->retry_task);
3268 * Mark future messages as ACK'd.
3270 * @param rel Reliability data.
3271 * @param msg DataACK message with a bitfield of future ACK'd messages.
3274 channel_rel_free_sent (struct MeshChannelReliability *rel,
3275 const struct GNUNET_MESH_DataACK *msg)
3277 struct MeshReliableMessage *copy;
3278 struct MeshReliableMessage *next;
3285 bitfield = msg->futures;
3286 mid = ntohl (msg->mid);
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "free_sent_reliable %u %llX\n",
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3291 " rel %p, head %p\n",
3292 rel, rel->head_sent);
3293 for (i = 0, copy = rel->head_sent;
3294 i < 64 && NULL != copy && 0 != bitfield;
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 " trying bit %u (mid %u)\n",
3301 if (0 == (bitfield & mask))
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3305 /* Bit was set, clear the bit from the bitfield */
3308 /* The i-th bit was set. Do we have that copy? */
3309 /* Skip copies with mid < target */
3310 target = mid + i + 1;
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3312 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3315 /* Did we run out of copies? (previously freed, it's ok) */
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3322 /* Did we overshoot the target? (previously freed, it's ok) */
3323 if (GMC_is_pid_bigger (copy->mid, target))
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3329 /* Now copy->mid == target, free it */
3331 rel_message_free (copy);
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3339 * We haven't received an ACK after a certain time: restransmit the message.
3341 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3342 * @param tc TaskContext.
3345 channel_retransmit_message (void *cls,
3346 const struct GNUNET_SCHEDULER_TaskContext *tc)
3348 struct MeshChannelReliability *rel = cls;
3349 struct MeshReliableMessage *copy;
3350 struct MeshPeerQueue *q;
3351 struct MeshChannel *ch;
3352 struct MeshConnection *c;
3353 struct GNUNET_MESH_Data *payload;
3354 struct MeshPeer *hop;
3357 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3358 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3362 copy = rel->head_sent;
3369 /* Search the message to be retransmitted in the outgoing queue.
3370 * Check only the queue for the connection that is going to be used,
3371 * if the message is stuck in some other connection's queue we shouldn't
3373 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3374 * the old connection could be temporary stalled or the queue happened to
3375 * be long at time of insertion.
3376 * - not sending the new one could cause terrible delays the old connection
3379 payload = (struct GNUNET_MESH_Data *) ©[1];
3380 fwd = (rel == ch->fwd_rel);
3381 c = tunnel_get_connection (ch->t, fwd);
3382 hop = connection_get_hop (c, fwd);
3383 for (q = hop->queue_head; NULL != q; q = q->next)
3385 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3387 struct GNUNET_MESH_Data *queued_data = q->cls;
3389 if (queued_data->mid == payload->mid)
3394 /* Message not found in the queue that we are going to use. */
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3399 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3400 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3407 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3408 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3409 &channel_retransmit_message,
3416 * Send ACK on one or more connections due to buffer space to the client.
3419 channel_send_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3421 struct MeshTunnel2 *t = ch->t;
3422 struct MeshConnection *c;
3423 struct MeshFlowControl *fc;
3428 /* Count connections, how many messages are already allowed */
3429 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3431 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3432 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3437 allowed += fc->last_ack_sent - fc->last_pid_recv;
3441 /* Make sure there is no overflow */
3442 if (allowed > buffer)
3448 /* Authorize connections to send more data */
3449 to_allow = buffer - allowed;
3450 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3452 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3453 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3457 send_ack (c, fc->last_ack_sent + 1, fwd);
3461 GNUNET_break (to_allow == 0);
3466 * Send keepalive packets for a connection.
3468 * @param c Connection to keep alive..
3469 * @param fwd Is this a FWD keepalive? (owner -> dest).
3472 connection_keepalive (struct MeshConnection *c, int fwd)
3474 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3475 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3479 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3480 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3483 "sending %s keepalive for connection %s[%d]\n",
3484 fwd ? "FWD" : "BCK",
3485 peer2s (c->t->peer),
3488 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3489 msg->header.size = htons (size);
3490 msg->header.type = htons (type);
3491 msg->cid = htonl (c->id);
3492 msg->tid = c->t->id;
3494 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3499 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3501 * @param c Connection for which to send the message.
3502 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3505 connection_recreate (struct MeshConnection *c, int fwd)
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3509 send_connection_create (c);
3511 send_connection_ack (c);
3516 * Generic connection timer management.
3517 * Depending on the role of the peer in the connection will send the
3518 * appropriate message (build or keepalive)
3520 * @param c Conncetion to maintain.
3521 * @param fwd Is FWD?
3524 connection_maintain (struct MeshConnection *c, int fwd)
3526 if (MESH_TUNNEL_SEARCHING == c->t->state)
3528 /* TODO DHT GET with RO_BART */
3533 case MESH_CONNECTION_NEW:
3535 case MESH_CONNECTION_SENT:
3536 connection_recreate (c, fwd);
3538 case MESH_CONNECTION_READY:
3539 connection_keepalive (c, fwd);
3548 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3550 struct MeshConnection *c = cls;
3552 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3553 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3556 connection_maintain (c, GNUNET_YES);
3557 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3558 &connection_fwd_keepalive,
3564 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3566 struct MeshConnection *c = cls;
3568 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3569 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3572 connection_maintain (c, GNUNET_NO);
3573 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3574 &connection_bck_keepalive,
3580 * Send a message to all peers in this connection that the connection
3581 * is no longer valid.
3583 * If some peer should not receive the message, it should be zero'ed out
3584 * before calling this function.
3586 * @param c The connection whose peers to notify.
3589 connection_send_destroy (struct MeshConnection *c)
3591 struct GNUNET_MESH_ConnectionDestroy msg;
3593 msg.header.size = htons (sizeof (msg));
3594 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3595 msg.cid = htonl (c->id);
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3598 " sending tunnel destroy for connection %s[%X]\n",
3599 peer2s (c->t->peer),
3602 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3603 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3604 c->destroy = GNUNET_YES;
3609 * Send a message to all clients (local and remote) of this channel
3610 * notifying that the channel is no longer valid.
3612 * If some peer or client should not receive the message,
3613 * should be zero'ed out before calling this function.
3615 * @param ch The channel whose clients to notify.
3618 channel_send_destroy (struct MeshChannel *ch)
3620 struct GNUNET_MESH_ChannelDestroy msg;
3622 msg.header.size = htons (sizeof (msg));
3623 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3625 " sending tunnel destroy for channel %s:%X\n",
3626 peer2s (ch->t->peer),
3629 if (NULL != ch->root)
3631 msg.chid = htonl (ch->lid_root);
3632 send_local_channel_destroy (ch, GNUNET_NO);
3636 msg.chid = htonl (ch->gid);
3637 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3640 if (NULL != ch->dest)
3642 msg.chid = htonl (ch->lid_dest);
3643 send_local_channel_destroy (ch, GNUNET_YES);
3647 msg.chid = htonl (ch->gid);
3648 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3656 * @param tid Tunnel ID.
3658 static struct MeshTunnel2 *
3659 tunnel_new (const struct GNUNET_HashCode *tid)
3661 struct MeshTunnel2 *t;
3663 t = GNUNET_new (struct MeshTunnel2);
3665 t->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3667 GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3682 * @param tid Tunnel ID.
3684 static struct MeshTunnel2 *
3685 tunnel_get (const struct GNUNET_HashCode *tid)
3687 return GNUNET_CONTAINER_multihashmap_get (tunnels, tid);
3692 * Add a connection to a tunnel.
3695 * @param c Connection.
3698 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3701 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3706 * Initialize a Flow Control structure to the initial state.
3708 * @param fc Flow Control structure to initialize.
3711 fc_init (struct MeshFlowControl *fc)
3713 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3714 fc->last_pid_recv = (uint32_t) -1;
3715 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3716 fc->last_ack_recv = (uint32_t) -1;
3717 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3718 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3724 * Create a connection.
3726 * @param tid Tunnel ID.
3727 * @param cid Connection ID.
3729 static struct MeshConnection *
3730 connection_new (struct GNUNET_HashCode *tid, uint32_t cid)
3732 struct MeshConnection *c;
3733 struct MeshTunnel2 *t;
3735 t = tunnel_get (tid);
3738 t = tunnel_new (tid);
3746 c = GNUNET_new (struct MeshConnection);
3748 fc_init (&c->fwd_fc);
3749 fc_init (&c->bck_fc);
3750 tunnel_add_connection (t, c);
3757 * Find a connection.
3759 * @param tid Tunnel ID.
3760 * @param cid Connection ID.
3762 static struct MeshConnection *
3763 connection_get (const struct GNUNET_HashCode *tid, uint32_t cid)
3765 struct MeshConnection *c;
3766 struct MeshTunnel2 *t;
3768 t = tunnel_get (tid);
3769 for (c = t->connection_head; NULL != c; c = c->next)
3778 * Connection is no longer needed: destroy it and remove from tunnel.
3780 * @param c Connection to destroy.
3783 connection_destroy (struct MeshConnection *c)
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
3789 peer2s (c->t->peer),
3792 connection_cancel_queues (c, GNUNET_YES);
3793 connection_cancel_queues (c, GNUNET_NO);
3795 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
3796 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3797 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
3798 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3800 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
3802 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
3808 tunnel_destroy (struct MeshTunnel2 *t)
3810 struct MeshConnection *c;
3811 struct MeshConnection *next;
3816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
3819 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
3822 for (c = t->connection_head; NULL != c; c = next)
3825 connection_destroy (c);
3828 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3835 * Tunnel is empty: destroy it.
3837 * Notifies all connections about the destruction.
3839 * @param t Tunnel to destroy.
3842 tunnel_destroy_empty (struct MeshTunnel2 *t)
3844 struct MeshConnection *c;
3846 for (c = t->connection_head; NULL != c; c = c->next)
3848 if (GNUNET_NO == c->destroy)
3849 connection_send_destroy (c);
3852 if (0 == t->pending_messages)
3855 t->destroy = GNUNET_YES;
3860 * Destroy tunnel if empty (no more channels).
3862 * @param t Tunnel to destroy if empty.
3865 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
3867 if (NULL != t->channel_head)
3870 tunnel_destroy_empty (t);
3875 * Destroy a channel and free all resources.
3877 * @param ch Channel to destroy.
3880 channel_destroy (struct MeshChannel *ch)
3882 struct MeshClient *c;
3890 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
3901 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
3908 if (GNUNET_YES == ch->reliable)
3910 channel_rel_free_all (ch->fwd_rel);
3911 channel_rel_free_all (ch->bck_rel);
3914 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
3915 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
3921 * Create a new channel.
3923 * @param t Tunnel this channel is in.
3924 * @param owner Client that owns the channel, NULL for foreign channels.
3925 * @param lid_root Local ID for root client.
3927 * @return A new initialized channel. NULL on error.
3929 static struct MeshChannel *
3930 channel_new (struct MeshTunnel2 *t,
3931 struct MeshClient *owner, MESH_ChannelNumber lid_root)
3933 struct MeshChannel *ch;
3935 ch = GNUNET_new (struct MeshChannel);
3937 ch->lid_root = lid_root;
3940 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
3944 while (NULL != channel_get_by_local_id (owner, t->next_chid))
3945 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
3946 ch->gid = t->next_chid;
3947 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
3950 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
3951 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3954 channel_destroy (ch);
3955 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
3965 * Set options in a channel, extracted from a bit flag field
3967 * @param ch Channel to set options to.
3968 * @param options Bit array in host byte order.
3971 channel_set_options (struct MeshChannel *ch, uint32_t options)
3973 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3974 GNUNET_YES : GNUNET_NO;
3975 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3976 GNUNET_YES : GNUNET_NO;
3981 * Iterator for deleting each channel whose client endpoint disconnected.
3983 * @param cls Closure (client that has disconnected).
3984 * @param key The local channel id (used to access the hashmap).
3985 * @param value The value stored at the key (channel to destroy).
3987 * @return GNUNET_OK, keep iterating.
3990 channel_destroy_iterator (void *cls,
3994 struct MeshChannel *ch = value;
3995 struct MeshClient *c = cls;
3996 struct MeshTunnel2 *t;
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3999 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4000 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4014 channel_send_destroy (ch);
4015 channel_destroy (ch);
4016 tunnel_destroy_if_empty (t);
4023 * Remove client's ports from the global hashmap on disconnect.
4025 * @param cls Closure (unused).
4027 * @param value Client structure.
4029 * @return GNUNET_OK, keep iterating.
4032 client_release_ports (void *cls,
4038 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4039 if (GNUNET_YES != res)
4042 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4043 "Port %u by client %p was not registered.\n",
4051 * Timeout function due to lack of keepalive/traffic from the owner.
4052 * Destroys connection if called.
4054 * @param cls Closure (connection to destroy).
4055 * @param tc TaskContext.
4058 connection_fwd_timeout (void *cls,
4059 const struct GNUNET_SCHEDULER_TaskContext *tc)
4061 struct MeshConnection *c = cls;
4063 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4064 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4067 "Connection %s[%X] FWD timed out. Destroying.\n",
4068 peer2s (c->t->peer),
4071 if (NULL != c->t->channel_head) /* If local, leave TODO review */
4074 connection_destroy (c);
4079 * Timeout function due to lack of keepalive/traffic from the destination.
4080 * Destroys connection if called.
4082 * @param cls Closure (connection to destroy).
4083 * @param tc TaskContext
4086 connection_bck_timeout (void *cls,
4087 const struct GNUNET_SCHEDULER_TaskContext *tc)
4089 struct MeshConnection *c = cls;
4091 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4092 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4096 "Connection %s[%X] FWD timed out. Destroying.\n",
4097 peer2s (c->t->peer),
4100 if (NULL != c->t->channel_head) /* If local, leave TODO review */
4103 connection_destroy (c);
4108 * Resets the connection timeout task, some other message has done the
4110 * - For the first peer on the direction this means to send
4111 * a keepalive or a path confirmation message (either create or ACK).
4112 * - For all other peers, this means to destroy the connection,
4113 * due to lack of activity.
4114 * Starts the tiemout if no timeout was running (connection just created).
4116 * @param c Connection whose timeout to reset.
4117 * @param fwd Is this forward?
4119 * TODO use heap to improve efficiency of scheduler.
4122 connection_reset_timeout (struct MeshConnection *c, int fwd)
4124 GNUNET_SCHEDULER_TaskIdentifier *ti;
4125 GNUNET_SCHEDULER_Task f;
4127 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4129 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4130 GNUNET_SCHEDULER_cancel (*ti);
4132 if (NULL != c->t->channel_head) /* Endpoint */
4134 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4135 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4139 struct GNUNET_TIME_Relative delay;
4141 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4142 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4143 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4149 * Iterator to notify all connections of a broken link. Mark connections
4150 * to destroy after all traffic has been sent.
4152 * @param cls Closure (peer disconnected).
4153 * @param key Current key code (tid).
4154 * @param value Value in the hash map (connection).
4156 * @return GNUNET_YES if we should continue to iterate,
4160 connection_broken (void *cls,
4161 const struct GNUNET_HashCode *key,
4164 struct MeshPeer *peer = cls;
4165 struct MeshConnection *c = value;
4166 struct GNUNET_MESH_ConnectionBroken msg;
4169 fwd = peer == connection_get_prev_hop (c);
4170 connection_cancel_queues (c, !fwd);
4172 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4173 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4174 msg.cid = htonl (c->id);
4176 msg.peer1 = my_full_id;
4177 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4178 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4179 c->destroy = GNUNET_YES;
4184 /******************************************************************************/
4185 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4186 /******************************************************************************/
4189 * Free a transmission that was already queued with all resources
4190 * associated to the request.
4192 * @param queue Queue handler to cancel.
4193 * @param clear_cls Is it necessary to free associated cls?
4196 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4198 struct MeshPeer *peer;
4199 struct MeshFlowControl *fc;
4204 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4206 if (GNUNET_YES == clear_cls)
4208 switch (queue->type)
4210 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4211 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying CONNECTION_DESTROY\n");
4212 GNUNET_break (GNUNET_YES == queue->c->destroy);
4214 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4215 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4216 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4217 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4218 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4221 GNUNET_MESH_DEBUG_M2S (queue->type));
4224 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4231 GNUNET_MESH_DEBUG_M2S (queue->type));
4233 GNUNET_free_non_null (queue->cls);
4235 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4239 if (NULL != queue->c)
4241 queue->c->pending_messages--;
4242 if (NULL != queue->c->t)
4244 queue->c->t->pending_messages--;
4248 GNUNET_free (queue);
4253 queue_send (void *cls, size_t size, void *buf)
4255 struct MeshPeer *peer = cls;
4256 struct MeshFlowControl *fc;
4257 struct MeshConnection *c;
4258 struct GNUNET_MessageHeader *msg;
4259 struct MeshPeerQueue *queue;
4260 struct MeshTunnel2 *t;
4261 const struct GNUNET_PeerIdentity *dst_id;
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4269 if (NULL == buf || 0 == size)
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4276 queue = peer_get_first_message (peer);
4279 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4282 queue->peer->core_transmit = NULL;
4285 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4288 dst_id = GNUNET_PEER_resolve2 (peer->id);
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4290 /* Check if buffer size is enough for the message */
4291 if (queue->size > size)
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4294 peer->core_transmit =
4295 GNUNET_CORE_notify_transmit_ready (core_handle,
4298 GNUNET_TIME_UNIT_FOREVER_REL,
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4307 t = (NULL != c) ? c->t : NULL;
4311 switch (queue->type)
4313 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4314 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4315 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4316 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4317 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4318 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4321 GNUNET_MESH_DEBUG_M2S (queue->type));
4323 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4324 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4325 data_size = send_core_data_raw (queue->cls, size, buf);
4326 msg = (struct GNUNET_MessageHeader *) buf;
4327 type = ntohs (msg->type);
4329 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4331 if (NULL != c->t->channel_head)
4332 data_size = send_core_connection_create (queue->cls, size, buf);
4334 data_size = send_core_data_raw (queue->cls, size, buf);
4336 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4338 if (NULL != c->t->channel_head)
4339 data_size = send_core_connection_ack (queue->cls, size, buf);
4341 data_size = send_core_data_raw (queue->cls, size, buf);
4345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4350 if (0 < drop_percent &&
4351 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4354 "Dropping message of type %s\n",
4355 GNUNET_MESH_DEBUG_M2S (queue->type));
4358 /* Free queue, but cls was freed by send_core_* */
4359 queue_destroy (queue, GNUNET_NO);
4361 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4364 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4365 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4366 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4367 fc->last_pid_sent = pid;
4368 connection_send_ack (c, fwd);
4374 /* If more data in queue, send next */
4375 queue = peer_get_first_message (peer);
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4379 if (NULL == peer->core_transmit) {
4380 peer->core_transmit =
4381 GNUNET_CORE_notify_transmit_ready(core_handle,
4384 GNUNET_TIME_UNIT_FOREVER_REL,
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4393 "* tmt rdy called somewhere else\n");
4395 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4397 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4399 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4404 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4406 GNUNET_SCHEDULER_cancel (fc->poll_task);
4407 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4412 c->pending_messages--;
4413 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4416 connection_destroy (c);
4422 t->pending_messages--;
4423 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4435 queue_add (void *cls, uint16_t type, size_t size,
4436 struct MeshPeer *dst,
4437 struct MeshConnection *c,
4438 struct MeshChannel *ch)
4440 struct MeshPeerQueue *queue;
4441 struct MeshFlowControl *fc;
4445 fwd = (dst == connection_get_next_hop (c));
4446 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4456 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4457 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4461 ( (NULL != ch->root && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4462 (NULL != ch->dest && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4465 if (fc->queue_n >= fc->queue_max && 0 == priority)
4467 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4471 "queue full: %u/%u\n",
4472 fc->queue_n, fc->queue_max);
4473 return; /* Drop this message */
4476 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
4477 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4478 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4481 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4489 if (100 <= priority)
4490 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
4492 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4494 if (NULL == dst->core_transmit)
4496 dst->core_transmit =
4497 GNUNET_CORE_notify_transmit_ready (core_handle,
4500 GNUNET_TIME_UNIT_FOREVER_REL,
4501 GNUNET_PEER_resolve2 (dst->id),
4506 c->pending_messages++;
4507 c->t->pending_messages++;
4513 /******************************************************************************/
4514 /******************** MESH NETWORK HANDLERS **************************/
4515 /******************************************************************************/
4519 * Generic handler for mesh network payload traffic.
4521 * @param t Tunnel on which we got this message.
4522 * @param message Unencryted data message.
4523 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4525 * @return GNUNET_OK to keep the connection open,
4526 * GNUNET_SYSERR to close it (signal serious error)
4529 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4531 struct MeshChannelReliability *rel;
4532 struct MeshChannel *ch;
4533 struct MeshClient *c;
4540 size = ntohs (msg->header.size);
4542 sizeof (struct GNUNET_MESH_Data) +
4543 sizeof (struct GNUNET_MessageHeader))
4548 type = ntohs (msg->header.type);
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4550 GNUNET_MESH_DEBUG_M2S (type));
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4552 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4555 ch = channel_get (t, ntohl (msg->chid));
4558 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4563 /* Initialize FWD/BCK data */
4564 c = fwd ? ch->dest : ch->root;
4565 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4566 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4574 tunnel_change_state (t, MESH_TUNNEL_READY);
4576 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4578 mid = ntohl (msg->mid);
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4581 if (GNUNET_NO == ch->reliable ||
4582 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4583 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4586 if (GNUNET_YES == ch->reliable)
4588 /* Is this the exact next expected messasge? */
4589 if (mid == *mid_recv)
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4592 *mid_recv = *mid_recv + 1;
4593 channel_send_client_data (ch, msg, fwd);
4594 channel_send_client_buffered_data (ch, c, rel);
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4599 channel_rel_add_buffered_data (msg, rel);
4602 else /* Tunnel unreliable, send to clients directly */
4604 channel_send_client_data (ch, msg, fwd);
4609 GNUNET_break_op (0);
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4611 " MID %u not expected (%u - %u), dropping!\n",
4612 mid, *mid_recv, *mid_recv + 64);
4615 channel_send_data_ack (ch, fwd);
4620 * Handler for mesh network traffic end-to-end ACKs.
4622 * @param t Tunnel on which we got this message.
4623 * @param message Data message.
4624 * @param fwd Is this a fwd ACK? (dest->orig)
4626 * @return GNUNET_OK to keep the connection open,
4627 * GNUNET_SYSERR to close it (signal serious error)
4630 handle_data_ack (struct MeshTunnel2 *t,
4631 const struct GNUNET_MESH_DataACK *msg, int fwd)
4633 struct MeshChannelReliability *rel;
4634 struct MeshReliableMessage *copy;
4635 struct MeshReliableMessage *next;
4636 struct MeshChannel *ch;
4641 type = ntohs (msg->header.type);
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4643 GNUNET_MESH_DEBUG_M2S (type));
4644 ch = channel_get (t, ntohl (msg->chid));
4647 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4650 ack = ntohl (msg->mid);
4651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4652 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4654 if (GNUNET_YES == fwd)
4667 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4669 if (GMC_is_pid_bigger (copy->mid, ack))
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4672 channel_rel_free_sent (rel, msg);
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4678 rel_message_free (copy);
4680 /* ACK client if needed */
4681 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4683 /* If some message was free'd, update the retransmission delay*/
4684 if (GNUNET_YES == work)
4686 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4688 GNUNET_SCHEDULER_cancel (rel->retry_task);
4689 if (NULL == rel->head_sent)
4691 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4695 struct GNUNET_TIME_Absolute new_target;
4696 struct GNUNET_TIME_Relative delay;
4698 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4699 MESH_RETRANSMIT_MARGIN);
4700 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4702 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4704 GNUNET_SCHEDULER_add_delayed (delay,
4705 &channel_retransmit_message,
4717 * Core handler for connection creation.
4719 * @param cls Closure (unused).
4720 * @param peer Sender (neighbor).
4721 * @param message Message.
4723 * @return GNUNET_OK to keep the connection open,
4724 * GNUNET_SYSERR to close it (signal serious error)
4727 handle_mesh_connection_create (void *cls,
4728 const struct GNUNET_PeerIdentity *peer,
4729 const struct GNUNET_MessageHeader *message)
4731 struct GNUNET_MESH_ConnectionCreate *msg;
4732 struct GNUNET_PeerIdentity *id;
4733 struct GNUNET_HashCode *tid;
4734 struct MeshPeerPath *path;
4735 struct MeshPeer *dest_peer;
4736 struct MeshPeer *orig_peer;
4737 struct MeshConnection *c;
4738 unsigned int own_pos;
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
4746 size = ntohs (message->size);
4747 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
4749 GNUNET_break_op (0);
4753 /* Calculate hops */
4754 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
4755 if (size % sizeof (struct GNUNET_PeerIdentity))
4757 GNUNET_break_op (0);
4760 size /= sizeof (struct GNUNET_PeerIdentity);
4763 GNUNET_break_op (0);
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4768 /* Get parameters */
4769 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
4770 cid = ntohl (msg->cid);
4772 id = (struct GNUNET_PeerIdentity *) &msg[1];
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4774 " connection %s[%X] (%s).\n",
4775 GNUNET_h2s (tid), cid, GNUNET_i2s (id));
4777 /* Create connection */
4778 c = connection_get (tid, cid);
4781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
4782 c = connection_new (tid, cid);
4786 connection_reset_timeout (c, GNUNET_YES);
4787 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
4789 /* Remember peers */
4790 dest_peer = peer_get (&id[size - 1]);
4791 orig_peer = peer_get (&id[0]);
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4795 path = path_new (size);
4797 for (i = 0; i < size; i++)
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4800 GNUNET_i2s (&id[i]));
4801 path->peers[i] = GNUNET_PEER_intern (&id[i]);
4802 if (path->peers[i] == myid)
4805 if (own_pos == 0 && path->peers[own_pos] != myid)
4807 /* create path: self not found in path through self */
4808 GNUNET_break_op (0);
4809 path_destroy (path);
4810 connection_destroy (c);
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4814 path_add_to_peers (path, GNUNET_NO);
4816 c->own_pos = own_pos;
4818 /* Is it a connection to us? */
4819 if (own_pos == size - 1)
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4822 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
4824 send_connection_ack (c);
4826 /* Keep tunnel alive in direction dest->owner*/
4827 connection_reset_timeout (c, GNUNET_NO);
4831 /* It's for somebody else! Retransmit. */
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4833 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
4834 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
4835 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
4842 * Core handler for path ACKs
4844 * @param cls closure
4845 * @param message message
4846 * @param peer peer identity this notification is about
4848 * @return GNUNET_OK to keep the connection open,
4849 * GNUNET_SYSERR to close it (signal serious error)
4852 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4853 const struct GNUNET_MessageHeader *message)
4855 struct GNUNET_MESH_ConnectionACK *msg;
4856 struct MeshPeerPath *p;
4857 struct MeshConnection *c;
4860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
4861 msg = (struct GNUNET_MESH_ConnectionACK *) message;
4862 cid = ntohl(msg->cid);
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s[%X]\n",
4864 GNUNET_h2s (&msg->tid), cid);
4865 c = connection_get (&msg->tid, cid);
4868 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
4870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
4874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4877 /* Add path to peers? */
4881 path_add_to_peers (p, GNUNET_YES);
4887 connection_change_state (c, MESH_CONNECTION_READY);
4888 connection_reset_timeout (c, GNUNET_NO);
4890 /* Message for us? */
4891 if (NULL != c->t->channel_head)
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4894 if (3 <= tunnel_count_connections(c->t) && NULL != c->t->peer->dhtget)
4896 GNUNET_DHT_get_stop (c->t->peer->dhtget);
4897 c->t->peer->dhtget = NULL;
4899 //connection_send_ack (c, GNUNET_NO); /* FIXME */
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4904 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
4910 * Core handler for notifications of broken paths
4912 * @param cls Closure (unused).
4913 * @param peer Peer identity of sending neighbor.
4914 * @param message Message.
4916 * @return GNUNET_OK to keep the connection open,
4917 * GNUNET_SYSERR to close it (signal serious error)
4920 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4921 const struct GNUNET_MessageHeader *message)
4923 struct GNUNET_MESH_ConnectionBroken *msg;
4924 struct MeshConnection *c;
4926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4927 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
4928 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4930 GNUNET_i2s (&msg->peer1));
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4932 GNUNET_i2s (&msg->peer2));
4933 c = connection_get (&msg->tid, ntohl (msg->cid));
4936 GNUNET_break_op (0);
4939 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
4940 GNUNET_PEER_search (&msg->peer2));
4947 * Core handler for tunnel destruction
4949 * @param cls Closure (unused).
4950 * @param peer Peer identity of sending neighbor.
4951 * @param message Message.
4953 * @return GNUNET_OK to keep the connection open,
4954 * GNUNET_SYSERR to close it (signal serious error)
4957 handle_mesh_connection_destroy (void *cls,
4958 const struct GNUNET_PeerIdentity *peer,
4959 const struct GNUNET_MessageHeader *message)
4961 struct GNUNET_MESH_ConnectionDestroy *msg;
4962 struct MeshConnection *c;
4966 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4968 "Got a CONNECTION DESTROY message from %s\n",
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4971 " for connection %s[%X]\n",
4972 GNUNET_h2s (&msg->tid), ntohl (msg->cid));
4973 c = connection_get (&msg->tid, ntohl (msg->cid));
4976 /* Probably already got the message from another path,
4977 * destroyed the tunnel and retransmitted to children.
4980 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4984 id = GNUNET_PEER_search (peer);
4985 if (id == connection_get_prev_hop (c)->id)
4987 else if (id == connection_get_next_hop (c)->id)
4991 GNUNET_break_op (0);
4994 send_prebuilt_message_connection (message, c, NULL, fwd);
4995 c->destroy = GNUNET_YES;
5001 * Handler for channel create messages.
5003 * @param t Tunnel this channel is to be created in.
5004 * @param msg Message.
5005 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5007 * @return GNUNET_OK to keep the connection open,
5008 * GNUNET_SYSERR to close it (signal serious error)
5011 handle_channel_create (struct MeshTunnel2 *t,
5012 struct GNUNET_MESH_ChannelCreate *msg,
5015 MESH_ChannelNumber chid;
5016 struct MeshChannel *ch;
5017 struct MeshClient *c;
5019 /* Check message size */
5020 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5022 GNUNET_break_op (0);
5026 /* Check if channel exists */
5027 chid = ntohl (msg->chid);
5028 ch = channel_get (t, chid);
5031 /* Probably a retransmission, safe to ignore */
5035 /* Find a destination client */
5036 c = GNUNET_CONTAINER_multihashmap32_get (ports, msg->port);
5039 /* TODO send reject */
5043 /* Create channel */
5044 ch = channel_new (t, NULL, 0);
5045 channel_set_options (ch, ntohl (msg->opt));
5046 channel_add_client (ch, c);
5047 if (GNUNET_YES == ch->reliable)
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5050 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5051 ch->bck_rel->ch = ch;
5052 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5055 send_local_channel_create (ch);
5062 * Handler for channel destroy messages.
5064 * @param t Tunnel this channel is to be destroyed of.
5065 * @param msg Message.
5066 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5068 * @return GNUNET_OK to keep the connection open,
5069 * GNUNET_SYSERR to close it (signal serious error)
5072 handle_channel_destroy (struct MeshTunnel2 *t,
5073 struct GNUNET_MESH_ChannelDestroy *msg,
5076 MESH_ChannelNumber chid;
5077 struct MeshChannel *ch;
5079 /* Check message size */
5080 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelDestroy))
5082 GNUNET_break_op (0);
5086 /* Check if channel exists */
5087 chid = ntohl (msg->chid);
5088 ch = channel_get (t, chid);
5091 /* Probably a retransmission, safe to ignore */
5095 send_local_channel_destroy (ch, fwd);
5096 channel_destroy (ch);
5103 * Generic handler for mesh network encrypted traffic.
5105 * @param peer Peer identity this notification is about.
5106 * @param message Encrypted message.
5107 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5109 * @return GNUNET_OK to keep the connection open,
5110 * GNUNET_SYSERR to close it (signal serious error)
5113 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5114 const struct GNUNET_MESH_Encrypted *msg,
5117 struct MeshConnection *c;
5118 struct MeshTunnel2 *t;
5119 struct MeshPeer *neighbor;
5120 struct MeshFlowControl *fc;
5127 size = ntohs (msg->header.size);
5129 sizeof (struct GNUNET_MESH_Encrypted) +
5130 sizeof (struct GNUNET_MessageHeader))
5135 type = ntohs (msg->header.type);
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5137 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5139 /* Check connection */
5140 c = connection_get (&msg->tid, ntohl (msg->cid));
5143 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5148 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5150 /* Check if origin is as expected */
5151 neighbor = connection_get_hop (c, fwd);
5152 if (peer_get (peer)->id != neighbor->id)
5154 GNUNET_break_op (0);
5159 pid = ntohl (msg->pid);
5160 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5162 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5164 "WARNING Received PID %u, (prev %u), ACK %u\n",
5165 pid, fc->last_pid_recv, fc->last_ack_sent);
5168 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5170 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5172 " Pid %u not expected (%u+), dropping!\n",
5173 pid, fc->last_pid_recv + 1);
5176 if (MESH_CONNECTION_SENT == c->state)
5177 connection_change_state (c, MESH_CONNECTION_READY);
5178 connection_reset_timeout (c, fwd);
5179 fc->last_pid_recv = pid;
5181 /* Is this message for us? */
5182 if (NULL != c->t->channel_head)
5184 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5186 struct GNUNET_MessageHeader *msgh;
5188 /* TODO signature verification */
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us!\n");
5190 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5192 fc->last_pid_recv = pid;
5193 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5194 msgh = (struct GNUNET_MessageHeader *) cbuf;
5195 switch (ntohs (msgh->type))
5197 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
5198 if (GNUNET_YES == fwd)
5199 return handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh,
5201 GNUNET_break_op (0);
5203 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
5204 if (GNUNET_NO == fwd)
5205 return handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh,
5207 GNUNET_break_op (0);
5209 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5210 if (GNUNET_YES == fwd)
5211 handle_data (t, (struct GNUNET_MESH_Data *) msgh, GNUNET_YES);
5212 GNUNET_break_op (0);
5214 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5215 if (GNUNET_NO == fwd)
5216 handle_data (t, (struct GNUNET_MESH_Data *) msgh, GNUNET_NO);
5217 GNUNET_break_op (0);
5219 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5220 return handle_channel_create (t,
5221 (struct GNUNET_MESH_ChannelCreate *) msgh,
5224 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5225 return handle_channel_destroy (t,
5226 (struct GNUNET_MESH_ChannelDestroy *)
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5232 "end-to-end message not known (%u)\n",
5233 ntohs (msgh->type));
5236 connection_send_ack (c, fwd);
5240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5241 ttl = ntohl (msg->ttl);
5242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5245 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5247 connection_send_ack (c, fwd);
5250 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5252 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5253 connection_send_ack (c, fwd);
5260 * Core handler for mesh network traffic going orig->dest.
5262 * @param cls Closure (unused).
5263 * @param message Message received.
5264 * @param peer Peer who sent the message.
5266 * @return GNUNET_OK to keep the connection open,
5267 * GNUNET_SYSERR to close it (signal serious error)
5270 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5271 const struct GNUNET_MessageHeader *message)
5273 return handle_mesh_encrypted (peer,
5274 (struct GNUNET_MESH_Encrypted *)message,
5279 * Core handler for mesh network traffic going dest->orig.
5281 * @param cls Closure (unused).
5282 * @param message Message received.
5283 * @param peer Peer who sent the message.
5285 * @return GNUNET_OK to keep the connection open,
5286 * GNUNET_SYSERR to close it (signal serious error)
5289 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5290 const struct GNUNET_MessageHeader *message)
5292 return handle_mesh_encrypted (peer,
5293 (struct GNUNET_MESH_Encrypted *)message,
5299 * Core handler for mesh network traffic point-to-point acks.
5301 * @param cls closure
5302 * @param message message
5303 * @param peer peer identity this notification is about
5305 * @return GNUNET_OK to keep the connection open,
5306 * GNUNET_SYSERR to close it (signal serious error)
5309 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5310 const struct GNUNET_MessageHeader *message)
5312 struct GNUNET_MESH_ACK *msg;
5313 struct MeshConnection *c;
5314 struct MeshFlowControl *fc;
5318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5320 msg = (struct GNUNET_MESH_ACK *) message;
5322 c = connection_get (&msg->tid, ntohl (msg->cid));
5326 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5331 ack = ntohl (msg->ack);
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5334 /* Is this a forward or backward ACK? */
5335 id = GNUNET_PEER_search (peer);
5336 if (connection_get_next_hop (c)->id == id)
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5341 else if (connection_get_prev_hop (c)->id == id)
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5348 GNUNET_break_op (0);
5352 /* Cancel polling if the ACK is bigger than before. */
5353 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5354 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5356 GNUNET_SCHEDULER_cancel (fc->poll_task);
5357 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5358 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5361 fc->last_ack_recv = ack;
5362 connection_unlock_queue (c, fc == &c->fwd_fc);
5369 * Core handler for mesh network traffic point-to-point ack polls.
5371 * @param cls closure
5372 * @param message message
5373 * @param peer peer identity this notification is about
5375 * @return GNUNET_OK to keep the connection open,
5376 * GNUNET_SYSERR to close it (signal serious error)
5379 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5380 const struct GNUNET_MessageHeader *message)
5382 struct GNUNET_MESH_Poll *msg;
5383 struct MeshConnection *c;
5384 struct MeshFlowControl *fc;
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5391 msg = (struct GNUNET_MESH_Poll *) message;
5393 c = connection_get (&msg->tid, ntohl (msg->cid));
5397 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5399 GNUNET_break_op (0);
5403 /* Is this a forward or backward ACK? */
5404 id = GNUNET_PEER_search (peer);
5405 if (connection_get_next_hop (c)->id == id)
5407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5410 else if (connection_get_prev_hop (c)->id == id)
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5417 GNUNET_break_op (0);
5421 pid = ntohl (msg->pid);
5422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5423 pid, fc->last_pid_recv);
5424 fc->last_pid_recv = pid;
5425 connection_send_ack (c, fc == &c->fwd_fc);
5432 * Core handler for mesh keepalives.
5434 * @param cls closure
5435 * @param message message
5436 * @param peer peer identity this notification is about
5437 * @return GNUNET_OK to keep the connection open,
5438 * GNUNET_SYSERR to close it (signal serious error)
5440 * TODO: Check who we got this from, to validate route.
5443 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5444 const struct GNUNET_MessageHeader *message)
5446 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5447 struct MeshConnection *c;
5448 struct MeshPeer *neighbor;
5451 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5455 c = connection_get (&msg->tid, ntohl (msg->cid));
5458 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5463 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5464 GNUNET_YES : GNUNET_NO;
5466 /* Check if origin is as expected */
5467 neighbor = connection_get_hop (c, fwd);
5468 if (peer_get (peer)->id != neighbor->id)
5470 GNUNET_break_op (0);
5474 connection_change_state (c, MESH_CONNECTION_READY);
5475 connection_reset_timeout (c, fwd);
5477 if (NULL != c->t->channel_head)
5480 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5481 send_prebuilt_message_connection (message, c, NULL, fwd);
5489 * Functions to handle messages from core
5491 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5492 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5494 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5495 sizeof (struct GNUNET_MESH_ConnectionACK)},
5496 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5497 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5498 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5499 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5500 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5501 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5502 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5503 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5504 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5505 sizeof (struct GNUNET_MESH_ACK)},
5506 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5507 sizeof (struct GNUNET_MESH_Poll)},
5508 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5509 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5515 * Function to process paths received for a new peer addition. The recorded
5516 * paths form the initial tunnel, which can be optimized later.
5517 * Called on each result obtained for the DHT search.
5519 * @param cls closure
5520 * @param exp when will this value expire
5521 * @param key key of the result
5522 * @param get_path path of the get request
5523 * @param get_path_length lenght of get_path
5524 * @param put_path path of the put request
5525 * @param put_path_length length of the put_path
5526 * @param type type of the result
5527 * @param size number of bytes in data
5528 * @param data pointer to the result data
5531 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5532 const struct GNUNET_HashCode * key,
5533 const struct GNUNET_PeerIdentity *get_path,
5534 unsigned int get_path_length,
5535 const struct GNUNET_PeerIdentity *put_path,
5536 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5537 size_t size, const void *data)
5539 struct MeshPeer *peer = cls;
5540 struct MeshPeerPath *p;
5541 struct MeshConnection *c;
5542 struct GNUNET_PeerIdentity pi;
5545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5546 GNUNET_PEER_resolve (peer->id, &pi);
5547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5549 p = path_build_from_dht (get_path, get_path_length,
5550 put_path, put_path_length);
5551 path_add_to_peers (p, GNUNET_NO);
5554 /* Count connections */
5555 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5557 /* If we already have 3 (or more (?!)) connections, it's enough */
5561 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5564 peer_connect (peer);
5570 /******************************************************************************/
5571 /********************* MESH LOCAL HANDLES **************************/
5572 /******************************************************************************/
5576 * Handler for client connection.
5578 * @param cls Closure (unused).
5579 * @param client Client handler.
5582 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5584 struct MeshClient *c;
5588 c = GNUNET_malloc (sizeof (struct MeshClient));
5590 c->id = next_client_id++; /* overflow not important: just for debug */
5591 GNUNET_SERVER_client_keep (client);
5592 GNUNET_SERVER_client_set_user_context (client, c);
5593 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5598 * Handler for client disconnection
5600 * @param cls closure
5601 * @param client identification of the client; NULL
5602 * for the last call when the server is destroyed
5605 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5607 struct MeshClient *c;
5608 struct MeshClient *next;
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5617 c = client_get (client);
5620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5622 GNUNET_SERVER_client_drop (c->handle);
5623 c->shutting_down = GNUNET_YES;
5624 if (NULL != c->own_channels)
5626 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
5627 &channel_destroy_iterator, c);
5628 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
5631 if (NULL != c->incoming_channels)
5633 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
5634 &channel_destroy_iterator, c);
5635 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
5638 if (NULL != c->ports)
5640 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5641 &client_release_ports, c);
5642 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5645 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5646 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
5655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
5661 * Handler for new clients
5663 * @param cls closure
5664 * @param client identification of the client
5665 * @param message the actual message, which includes messages the client wants
5668 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5669 const struct GNUNET_MessageHeader *message)
5671 struct GNUNET_MESH_ClientConnect *cc_msg;
5672 struct MeshClient *c;
5677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
5679 /* Check data sanity */
5680 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5681 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5682 if (0 != (size % sizeof (uint32_t)))
5685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5688 size /= sizeof (uint32_t);
5690 /* Initialize new client structure */
5691 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
5692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
5698 p = (uint32_t *) &cc_msg[1];
5699 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
5700 for (i = 0; i < size; i++)
5703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
5705 /* store in client's hashmap */
5706 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
5707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5708 /* store in global hashmap */
5709 /* FIXME only allow one client to have the port open,
5710 * have a backup hashmap with waiting clients */
5711 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5716 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5717 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5718 GNUNET_SERVER_notification_context_add (nc, client);
5719 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5721 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5727 * Handler for requests of new tunnels
5729 * @param cls Closure.
5730 * @param client Identification of the client.
5731 * @param message The actual message.
5734 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
5735 const struct GNUNET_MessageHeader *message)
5737 struct GNUNET_MESH_ChannelMessage *msg;
5738 struct MeshPeer *peer;
5739 struct MeshTunnel2 *t;
5740 struct MeshChannel *ch;
5741 struct MeshClient *c;
5742 MESH_ChannelNumber chid;
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
5746 /* Sanity check for client registration */
5747 if (NULL == (c = client_get (client)))
5750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5755 /* Message size sanity check */
5756 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5759 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5763 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5765 GNUNET_i2s (&msg->peer), ntohl (msg->port));
5766 chid = ntohl (msg->channel_id);
5768 /* Sanity check for duplicate channel IDs */
5769 if (NULL != channel_get_by_local_id (c, chid))
5772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5776 peer = peer_get (&msg->peer);
5777 if (NULL == peer->tunnel)
5779 struct GNUNET_HashCode tid;
5781 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &tid);
5782 peer->tunnel = tunnel_new (&tid);
5786 /* Create channel */
5787 ch = channel_new (t, c, chid);
5791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5794 ch->port = ntohl (msg->port);
5795 channel_set_options (ch, ntohl (msg->opt));
5797 /* In unreliable channels, we'll use the DLL to buffer data for the root */
5798 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5799 ch->fwd_rel->ch = ch;
5800 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
5803 GNUNET_h2s (&t->id), ch->gid, ch->port, ch->lid_root);
5804 peer_connect (peer);
5805 /* FIXME send create channel */
5807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5813 * Handler for requests of deleting tunnels
5815 * @param cls closure
5816 * @param client identification of the client
5817 * @param message the actual message
5820 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5821 const struct GNUNET_MessageHeader *message)
5823 struct GNUNET_MESH_ChannelMessage *msg;
5824 struct MeshClient *c;
5825 struct MeshChannel *ch;
5826 struct MeshTunnel2 *t;
5827 MESH_ChannelNumber chid;
5829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5830 "Got a DESTROY CHANNEL from client!\n");
5832 /* Sanity check for client registration */
5833 if (NULL == (c = client_get (client)))
5836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5841 /* Message sanity check */
5842 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5849 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5851 /* Retrieve tunnel */
5852 chid = ntohl (msg->channel_id);
5853 ch = channel_get_by_local_id (c, chid);
5856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
5858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5862 /* Cleanup after the tunnel */
5863 client_delete_channel (c, ch);
5864 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
5868 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
5874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5875 " channel %X client %p (%p, %p)\n",
5876 chid, c, ch->root, ch->dest);
5881 channel_destroy (ch);
5882 tunnel_destroy_if_empty (t);
5884 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5890 * Handler for client traffic
5892 * @param cls closure
5893 * @param client identification of the client
5894 * @param message the actual message
5897 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5898 const struct GNUNET_MessageHeader *message)
5900 struct GNUNET_MESH_LocalData *msg;
5901 struct MeshClient *c;
5902 struct MeshChannel *ch;
5903 MESH_ChannelNumber chid;
5907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5908 "Got data from a client!\n");
5910 /* Sanity check for client registration */
5911 if (NULL == (c = client_get (client)))
5914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5919 msg = (struct GNUNET_MESH_LocalData *) message;
5921 /* Sanity check for message size */
5922 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5923 if (size < sizeof (struct GNUNET_MessageHeader))
5926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5930 /* Channel exists? */
5931 chid = ntohl (msg->id);
5932 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
5933 ch = channel_get_by_local_id (c, chid);
5937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5941 /* Is the client in the channel? */
5944 ch->root->handle == client)
5948 ch->dest->handle == client) ) )
5951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5955 /* Ok, everything is correct, send the message
5956 * (pretend we got it from a mesh peer)
5959 struct GNUNET_MESH_Data *payload;
5960 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5963 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
5964 if (GNUNET_YES == ch->reliable)
5966 struct MeshChannelReliability *rel;
5967 struct MeshReliableMessage *copy;
5969 rel = fwd ? ch->fwd_rel : ch->bck_rel;
5970 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5971 + sizeof(struct GNUNET_MESH_Data)
5974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5975 copy->timestamp = GNUNET_TIME_absolute_get ();
5978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5979 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5980 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5983 GNUNET_TIME_relative_multiply (rel->expected_delay,
5984 MESH_RETRANSMIT_MARGIN);
5986 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5987 &channel_retransmit_message,
5990 payload = (struct GNUNET_MESH_Data *) ©[1];
5994 payload = (struct GNUNET_MESH_Data *) cbuf;
5996 payload->mid = htonl (*mid);
5998 memcpy (&payload[1], &msg[1], size);
5999 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
6000 payload->header.type = htons (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ?
6001 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
6002 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6003 payload->chid = htonl (ch->gid);
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6005 " calling generic handler...\n");
6006 send_prebuilt_message_channel (&payload->header, ch, fwd);
6008 if (tunnel_get_buffer (ch->t, fwd) > 0)
6009 send_local_ack (ch, c, fwd);
6010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6011 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6018 * Handler for client's ACKs for payload traffic.
6020 * @param cls Closure (unused).
6021 * @param client Identification of the client.
6022 * @param message The actual message.
6025 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6026 const struct GNUNET_MessageHeader *message)
6028 struct GNUNET_MESH_LocalAck *msg;
6029 struct MeshChannelReliability *rel;
6030 struct MeshChannel *ch;
6031 struct MeshClient *c;
6032 MESH_ChannelNumber chid;
6035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6037 /* Sanity check for client registration */
6038 if (NULL == (c = client_get (client)))
6041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6046 msg = (struct GNUNET_MESH_LocalAck *) message;
6048 /* Channel exists? */
6049 chid = ntohl (msg->channel_id);
6050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6051 ch = channel_get_by_local_id (c, chid);
6055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6061 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6062 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6064 rel->client_ready = GNUNET_YES;
6065 channel_send_client_buffered_data (ch, c, rel);
6066 channel_send_ack (ch, 64 - rel->n_recv, fwd);
6068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6075 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6077 * @param cls Closure (client handle).
6078 * @param key Key (hashed tunnel ID, unused).
6079 * @param value Tunnel info.
6081 * @return GNUNET_YES, to keep iterating.
6084 monitor_all_tunnels_iterator (void *cls,
6085 const struct GNUNET_HashCode * key,
6088 struct GNUNET_SERVER_Client *client = cls;
6089 struct MeshChannel *ch = value;
6090 struct GNUNET_MESH_LocalMonitor *msg;
6092 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6093 msg->channel_id = htonl (ch->gid);
6094 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6095 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6097 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6098 "* sending info about tunnel %s\n",
6099 GNUNET_i2s (&msg->owner));
6101 GNUNET_SERVER_notification_context_unicast (nc, client,
6102 &msg->header, GNUNET_NO);
6108 * Handler for client's MONITOR request.
6110 * @param cls Closure (unused).
6111 * @param client Identification of the client.
6112 * @param message The actual message.
6115 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6116 const struct GNUNET_MessageHeader *message)
6118 struct MeshClient *c;
6120 /* Sanity check for client registration */
6121 if (NULL == (c = client_get (client)))
6124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6129 "Received get tunnels request from client %u\n",
6131 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6132 monitor_all_tunnels_iterator,
6134 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6135 "Get tunnels request from client %u completed\n",
6137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6142 * Handler for client's MONITOR_TUNNEL request.
6144 * @param cls Closure (unused).
6145 * @param client Identification of the client.
6146 * @param message The actual message.
6149 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6150 const struct GNUNET_MessageHeader *message)
6152 const struct GNUNET_MESH_LocalMonitor *msg;
6153 struct GNUNET_MESH_LocalMonitor *resp;
6154 struct MeshClient *c;
6155 struct MeshChannel *ch;
6157 /* Sanity check for client registration */
6158 if (NULL == (c = client_get (client)))
6161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6165 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6166 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6167 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6170 ntohl (msg->channel_id));
6171 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6175 /* We don't know the tunnel */
6176 struct GNUNET_MESH_LocalMonitor warn;
6179 GNUNET_SERVER_notification_context_unicast (nc, client,
6182 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6186 /* Initialize context */
6187 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6189 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6190 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6191 &resp->header, GNUNET_NO);
6194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6195 "Monitor tunnel request from client %u completed\n",
6197 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6202 * Functions to handle messages from clients
6204 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6205 {&handle_local_new_client, NULL,
6206 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6207 {&handle_local_channel_create, NULL,
6208 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6209 sizeof (struct GNUNET_MESH_ChannelMessage)},
6210 {&handle_local_channel_destroy, NULL,
6211 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6212 sizeof (struct GNUNET_MESH_ChannelMessage)},
6213 {&handle_local_data, NULL,
6214 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6215 {&handle_local_ack, NULL,
6216 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6217 sizeof (struct GNUNET_MESH_LocalAck)},
6218 {&handle_local_get_tunnels, NULL,
6219 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6220 sizeof (struct GNUNET_MessageHeader)},
6221 {&handle_local_show_tunnel, NULL,
6222 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6223 sizeof (struct GNUNET_MESH_LocalMonitor)},
6229 * Method called whenever a given peer connects.
6231 * @param cls closure
6232 * @param peer peer identity this notification is about
6235 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6237 struct MeshPeer *pi;
6238 struct MeshPeerPath *path;
6240 DEBUG_CONN ("Peer connected\n");
6241 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6242 pi = peer_get (peer);
6245 DEBUG_CONN (" (self)\n");
6246 path = path_new (1);
6250 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6251 path = path_new (2);
6252 path->peers[1] = pi->id;
6253 GNUNET_PEER_change_rc (pi->id, 1);
6254 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6256 path->peers[0] = myid;
6257 GNUNET_PEER_change_rc (myid, 1);
6258 peer_add_path (pi, path, GNUNET_YES);
6260 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6266 * Method called whenever a peer disconnects.
6268 * @param cls closure
6269 * @param peer peer identity this notification is about
6272 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6274 struct MeshPeer *pi;
6276 DEBUG_CONN ("Peer disconnected\n");
6277 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6284 peer_remove_path (pi, myid, pi->id);
6286 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6289 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6290 pi->connections = NULL;
6293 DEBUG_CONN (" (self)\n");
6295 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6302 * Install server (service) handlers and start listening to clients.
6307 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6308 GNUNET_SERVER_connect_notify (server_handle,
6309 &handle_local_client_connect, NULL);
6310 GNUNET_SERVER_disconnect_notify (server_handle,
6311 &handle_local_client_disconnect, NULL);
6312 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6314 clients_head = NULL;
6315 clients_tail = NULL;
6317 GNUNET_SERVER_resume (server_handle);
6322 * To be called on core init/fail.
6324 * @param cls Closure (config)
6325 * @param server handle to the server for this service
6326 * @param identity the public identity of this peer
6329 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6330 const struct GNUNET_PeerIdentity *identity)
6332 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6336 GNUNET_break (core_handle == server);
6337 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6343 GNUNET_i2s (identity));
6344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6346 GNUNET_i2s (&my_full_id));
6347 GNUNET_CORE_disconnect (core_handle);
6348 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6349 NULL, /* Closure passed to MESH functions */
6350 &core_init, /* Call core_init once connected */
6351 &core_connect, /* Handle connects */
6352 &core_disconnect, /* remove peers on disconnects */
6353 NULL, /* Don't notify about all incoming messages */
6354 GNUNET_NO, /* For header only in notification */
6355 NULL, /* Don't notify about all outbound messages */
6356 GNUNET_NO, /* For header-only out notification */
6357 core_handlers); /* Register these handlers */
6366 /******************************************************************************/
6367 /************************ MAIN FUNCTIONS ****************************/
6368 /******************************************************************************/
6371 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6373 * @param cls closure
6374 * @param key current key code
6375 * @param value value in the hash map
6376 * @return GNUNET_YES if we should continue to iterate,
6380 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6382 struct MeshTunnel2 *t = value;
6390 * Task run during shutdown.
6396 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6400 if (core_handle != NULL)
6402 GNUNET_CORE_disconnect (core_handle);
6405 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6406 if (dht_handle != NULL)
6408 GNUNET_DHT_disconnect (dht_handle);
6413 GNUNET_SERVER_notification_context_destroy (nc);
6416 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6418 GNUNET_SCHEDULER_cancel (announce_id_task);
6419 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6426 * Process mesh requests.
6428 * @param cls closure
6429 * @param server the initialized server
6430 * @param c configuration to use
6433 run (void *cls, struct GNUNET_SERVER_Handle *server,
6434 const struct GNUNET_CONFIGURATION_Handle *c)
6437 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6440 server_handle = server;
6441 GNUNET_SERVER_suspend (server_handle);
6444 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6449 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6450 "mesh", "peer/privatekey");
6451 GNUNET_SCHEDULER_shutdown ();
6456 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6457 &refresh_connection_time))
6459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6461 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6462 "mesh", "refresh path time");
6463 GNUNET_SCHEDULER_shutdown ();
6468 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6473 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6474 "mesh", "id announce time");
6475 GNUNET_SCHEDULER_shutdown ();
6480 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6485 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6486 "mesh", "connect timeout");
6487 GNUNET_SCHEDULER_shutdown ();
6492 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6497 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6498 "mesh", "max msgs queue");
6499 GNUNET_SCHEDULER_shutdown ();
6504 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6509 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6510 "mesh", "max tunnels");
6511 GNUNET_SCHEDULER_shutdown ();
6516 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6521 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6522 "mesh", "default ttl", 64);
6527 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6531 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6532 "mesh", "max peers", 1000);
6537 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6545 "Mesh is running with drop mode enabled. "
6546 "This is NOT a good idea! "
6547 "Remove the DROP_PERCENT option from your configuration.\n");
6551 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6552 &dht_replication_level))
6554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6556 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6557 "mesh", "dht replication level", 3);
6558 dht_replication_level = 3;
6561 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6562 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6563 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6565 dht_handle = GNUNET_DHT_connect (c, 64);
6566 if (NULL == dht_handle)
6570 stats = GNUNET_STATISTICS_create ("mesh", c);
6572 /* Scheduled the task to clean up when shutdown is called */
6573 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6575 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6576 GNUNET_free (keyfile);
6577 GNUNET_assert (NULL != pk);
6578 my_private_key = pk;
6579 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6580 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6581 &my_full_id.hashPubKey);
6582 myid = GNUNET_PEER_intern (&my_full_id);
6583 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6584 "Mesh for peer [%s] starting\n",
6585 GNUNET_i2s(&my_full_id));
6587 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6588 NULL, /* Closure passed to MESH functions */
6589 &core_init, /* Call core_init once connected */
6590 &core_connect, /* Handle connects */
6591 &core_disconnect, /* remove peers on disconnects */
6592 NULL, /* Don't notify about all incoming messages */
6593 GNUNET_NO, /* For header only in notification */
6594 NULL, /* Don't notify about all outbound messages */
6595 GNUNET_NO, /* For header-only out notification */
6596 core_handlers); /* Register these handlers */
6597 if (NULL == core_handle)
6600 GNUNET_SCHEDULER_shutdown ();
6603 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6609 * The main function for the mesh service.
6611 * @param argc number of arguments from the command line
6612 * @param argv command line arguments
6613 * @return 0 ok, 1 on error
6616 main (int argc, char *const *argv)
6621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6622 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6624 ret = (GNUNET_OK == r) ? 0 : 1;
6625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");