2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-enc.c
23 * @brief GNUnet MESH service with encryption
24 * @author Bartlomiej Polot
27 * - when sending in-order buffered data, wait for client ACKs
30 * - set connection IDs independently from tunnel, tunnel has no ID
33 * - relay corking down to core
34 * - set ttl relative to path length
38 * - peer: other mesh instance. If there is direct connection it's a neighbor.
39 * - tunnel: encrypted connection to a peer, neighbor or not.
40 * - channel: connection between two clients, on the same or different peers.
41 * have properties like reliability.
42 * - path: series of directly connected peer from one peer to another.
43 * - connection: path which is being used in a tunnel.
47 #include "gnunet_crypto_lib.h"
49 #include "mesh_protocol_enc.h"
50 #include "mesh_path.h"
51 #include "block_mesh.h"
52 #include "gnunet_dht_service.h"
53 #include "gnunet_statistics_service.h"
55 #define MESH_BLOOM_SIZE 128
57 #define MESH_DEBUG_DHT GNUNET_NO
58 #define MESH_DEBUG_CONNECTION GNUNET_YES
59 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
61 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
62 GNUNET_TIME_UNIT_MINUTES,\
64 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
65 #define MESH_RETRANSMIT_MARGIN 4
67 #if MESH_DEBUG_CONNECTION
68 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
70 #define DEBUG_CONN(...)
74 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
76 #define DEBUG_DHT(...)
83 struct timespec __mesh_start;
84 struct timespec __mesh_end;
85 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
86 #define INTERVAL_END \
88 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
89 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
90 if (__diff < 0) __diff += 1000000000;\
94 #define INTERVAL_SHOW \
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
98 #define INTERVAL_START
100 #define INTERVAL_SHOW
104 * All the states a tunnel can be in.
109 * Uninitialized status, should never appear in operation.
114 * Path to the peer not known yet
116 MESH_TUNNEL_SEARCHING,
119 * Request sent, not yet answered.
124 * Peer connected and ready to accept data
129 * Peer connected previosly but not responding
131 MESH_TUNNEL_RECONNECTING
136 * All the states a connection can be in.
138 enum MeshConnectionState
141 * Uninitialized status, should never appear in operation.
146 * Connection create message sent, waiting for ACK.
148 MESH_CONNECTION_SENT,
151 * Connection ACK sent, waiting for ACK.
156 * Connection confirmed, ready to carry traffic.
158 MESH_CONNECTION_READY,
163 * All the states a connection can be in.
165 enum MeshChannelState
168 * Uninitialized status, should never appear in operation.
173 * Connection create message sent, waiting for ACK.
178 * Connection confirmed, ready to carry traffic..
184 /******************************************************************************/
185 /************************ DATA STRUCTURES ****************************/
186 /******************************************************************************/
188 /** FWD declaration */
192 struct MeshConnection;
194 struct MeshChannelReliability;
198 * Struct containing info about a queued transmission to this peer
205 struct MeshPeerQueue *next;
210 struct MeshPeerQueue *prev;
213 * Peer this transmission is directed to.
215 struct MeshPeer *peer;
218 * Connection this message belongs to.
220 struct MeshConnection *c;
228 * Channel this message belongs to, if known.
230 struct MeshChannel *ch;
233 * Pointer to info stucture used as cls.
243 * Size of the message
250 * Struct to encapsulate all the Flow Control information to a peer to which
251 * we are directly connected (on a core level).
253 struct MeshFlowControl
256 * Connection this controls.
258 struct MeshConnection *c;
261 * How many messages are in the queue on this connection.
263 unsigned int queue_n;
266 * How many messages do we accept in the queue.
268 unsigned int queue_max;
276 * ID of the last packet sent towards the peer.
278 uint32_t last_pid_sent;
281 * ID of the last packet received from the peer.
283 uint32_t last_pid_recv;
286 * Last ACK sent to the peer (peer can't send more than this PID).
288 uint32_t last_ack_sent;
291 * Last ACK sent towards the origin (for traffic towards leaf node).
293 uint32_t last_ack_recv;
296 * Task to poll the peer in case of a lost ACK causes stall.
298 GNUNET_SCHEDULER_TaskIdentifier poll_task;
301 * How frequently to poll for ACKs.
303 struct GNUNET_TIME_Relative poll_time;
308 * Struct containing all information regarding a given peer
318 * Last time we heard from this peer
320 struct GNUNET_TIME_Absolute last_contact;
323 * Paths to reach the peer, ordered by ascending hop count
325 struct MeshPeerPath *path_head;
328 * Paths to reach the peer, ordered by ascending hop count
330 struct MeshPeerPath *path_tail;
333 * Handle to stop the DHT search for paths to this peer
335 struct GNUNET_DHT_GetHandle *dhtget;
338 * Tunnel to this peer, if any.
340 struct MeshTunnel2 *tunnel;
343 * Connections that go through this peer, indexed by tid;
345 struct GNUNET_CONTAINER_MultiHashMap *connections;
348 * Handle for queued transmissions
350 struct GNUNET_CORE_TransmitHandle *core_transmit;
353 * Transmission queue to core DLL head
355 struct MeshPeerQueue *queue_head;
358 * Transmission queue to core DLL tail
360 struct MeshPeerQueue *queue_tail;
363 * How many messages are in the queue to this peer.
365 unsigned int queue_n;
370 * Info needed to retry a message in case it gets lost.
372 struct MeshReliableMessage
375 * Double linked list, FIFO style
377 struct MeshReliableMessage *next;
378 struct MeshReliableMessage *prev;
381 * Type of message (payload, channel management).
386 * Tunnel Reliability queue this message is in.
388 struct MeshChannelReliability *rel;
391 * ID of the message (ACK needed to free)
396 * When was this message issued (to calculate ACK delay)
398 struct GNUNET_TIME_Absolute timestamp;
400 /* struct GNUNET_MESH_Data with payload */
405 * Info about the traffic state for a client in a channel.
407 struct MeshChannelReliability
410 * Channel this is about.
412 struct MeshChannel *ch;
415 * DLL of messages sent and not yet ACK'd.
417 struct MeshReliableMessage *head_sent;
418 struct MeshReliableMessage *tail_sent;
421 * Messages pending to send.
426 * DLL of messages received out of order.
428 struct MeshReliableMessage *head_recv;
429 struct MeshReliableMessage *tail_recv;
437 * Next MID to use for outgoing traffic.
442 * Next MID expected for incoming traffic.
447 * Can we send data to the client?
452 * Task to resend/poll in case no ACK is received.
454 GNUNET_SCHEDULER_TaskIdentifier retry_task;
457 * Counter for exponential backoff.
459 struct GNUNET_TIME_Relative retry_timer;
462 * How long does it usually take to get an ACK.
464 struct GNUNET_TIME_Relative expected_delay;
469 * Struct containing all information regarding a channel to a remote client.
474 * Tunnel this channel is in.
476 struct MeshTunnel2 *t;
479 * Double linked list.
481 struct MeshChannel *next;
482 struct MeshChannel *prev;
485 * Destination port of the channel.
490 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
492 MESH_ChannelNumber gid;
495 * Local tunnel number for root (owner) client.
496 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
498 MESH_ChannelNumber lid_root;
501 * Local tunnel number for local destination clients (incoming number)
502 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
504 MESH_ChannelNumber lid_dest;
509 enum MeshChannelState state;
512 * Is the tunnel bufferless (minimum latency)?
517 * Is the tunnel reliable?
522 * Last time the channel was used
524 struct GNUNET_TIME_Absolute timestamp;
527 * Client owner of the tunnel, if any
529 struct MeshClient *root;
532 * Client destination of the tunnel, if any.
534 struct MeshClient *dest;
537 * Flag to signal the destruction of the channel.
538 * If this is set GNUNET_YES the channel will be destroyed
539 * when the queue is empty.
544 * Total messages pending for this channel, payload or not.
546 unsigned int pending_messages;
550 * Only present (non-NULL) at the owner of a tunnel.
552 struct MeshChannelReliability *root_rel;
556 * Only present (non-NULL) at the destination of a tunnel.
558 struct MeshChannelReliability *dest_rel;
564 * Struct containing all information regarding a connection to a peer.
566 struct MeshConnection
571 struct MeshConnection *next;
572 struct MeshConnection *prev;
575 * Tunnel this connection is part of.
577 struct MeshTunnel2 *t;
580 * Flow control information for traffic fwd.
582 struct MeshFlowControl fwd_fc;
585 * Flow control information for traffic bck.
587 struct MeshFlowControl bck_fc;
590 * ID of the connection.
592 struct GNUNET_HashCode id;
595 * State of the connection.
597 enum MeshConnectionState state;
600 * Path being used for the tunnel.
602 struct MeshPeerPath *path;
605 * Position of the local peer in the path.
607 unsigned int own_pos;
610 * Task to keep the used paths alive at the owner,
611 * time tunnel out on all the other peers.
613 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
616 * Task to keep the used paths alive at the destination,
617 * time tunnel out on all the other peers.
619 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
622 * Pending message count.
624 int pending_messages;
627 * Destroy flag: if true, destroy on last message.
634 * Struct used to queue messages in a tunnel.
636 struct MeshTunnelQueue
641 struct MeshTunnelQueue *next;
642 struct MeshTunnelQueue *prev;
647 struct MeshChannel *ch;
652 /* struct GNUNET_MessageHeader *msg; */
657 * Struct containing all information regarding a tunnel to a peer.
662 * Endpoint of the tunnel.
664 struct MeshPeer *peer;
667 * State of the tunnel.
669 enum MeshTunnelState state;
672 * Local peer ephemeral private key
674 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
677 * Local peer ephemeral public key
679 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
682 * Remote peer's public key.
684 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
687 * Encryption ("our") key.
689 struct GNUNET_CRYPTO_AesSessionKey e_key;
692 * Decryption ("their") key.
694 struct GNUNET_CRYPTO_AesSessionKey d_key;
697 * Paths that are actively used to reach the destination peer.
699 struct MeshConnection *connection_head;
700 struct MeshConnection *connection_tail;
703 * Next connection number.
708 * Channels inside this tunnel.
710 struct MeshChannel *channel_head;
711 struct MeshChannel *channel_tail;
714 * Channel ID for the next created channel.
716 MESH_ChannelNumber next_chid;
719 * Channel ID for the next incoming channel.
721 MESH_ChannelNumber next_local_chid;
724 * Pending message count.
726 int pending_messages;
729 * Destroy flag: if true, destroy on last message.
734 * Queued messages, to transmit once tunnel gets connected.
736 struct MeshTunnelQueue *tq_head;
737 struct MeshTunnelQueue *tq_tail;
743 * Struct containing information about a client of the service
745 * TODO: add a list of 'waiting' ports
752 struct MeshClient *next;
757 struct MeshClient *prev;
760 * Tunnels that belong to this client, indexed by local id
762 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
765 * Tunnels this client has accepted, indexed by incoming local id
767 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
770 * Handle to communicate with the client
772 struct GNUNET_SERVER_Client *handle;
775 * Ports that this client has declared interest in.
776 * Indexed by port, contains *Client.
778 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
781 * Whether the client is active or shutting down (don't send confirmations
782 * to a client that is shutting down.
787 * ID of the client, mainly for debug messages
793 /******************************************************************************/
794 /************************ DEBUG FUNCTIONS ****************************/
795 /******************************************************************************/
799 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
800 * @param cls string to print
801 * @param success GNUNET_OK if the PUT was transmitted,
802 * GNUNET_NO on timeout,
803 * GNUNET_SYSERR on disconnect from service
804 * after the PUT message was transmitted
805 * (so we don't know if it was received or not)
810 mesh_debug (void *cls, int success)
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
820 /******************************************************************************/
821 /*********************** GLOBAL VARIABLES ****************************/
822 /******************************************************************************/
824 /************************** Configuration parameters **************************/
827 * How often to send path keepalives. Paths timeout after 4 missed.
829 static struct GNUNET_TIME_Relative refresh_connection_time;
832 * How often to PUT own ID in the DHT.
834 static struct GNUNET_TIME_Relative id_announce_time;
837 * Maximum time allowed to connect to a peer found by string.
839 static struct GNUNET_TIME_Relative connect_timeout;
842 * Default TTL for payload packets.
844 static unsigned long long default_ttl;
847 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
849 static unsigned long long dht_replication_level;
852 * How many connections are we willing to maintain.
853 * Local connections are always allowed, even if there are more connections than max.
855 static unsigned long long max_connections;
858 * How many messages *in total* are we willing to queue, divide by number of
859 * connections to get connection queue size.
861 static unsigned long long max_msgs_queue;
864 * How many peers do we want to remember?
866 static unsigned long long max_peers;
869 * Percentage of messages that will be dropped (for test purposes only).
871 static unsigned long long drop_percent;
873 /*************************** Static global variables **************************/
876 * DLL with all the clients, head.
878 static struct MeshClient *clients_head;
881 * DLL with all the clients, tail.
883 static struct MeshClient *clients_tail;
886 * Connections known, indexed by cid (MeshConnection).
888 static struct GNUNET_CONTAINER_MultiHashMap *connections;
891 * Peers known, indexed by PeerIdentity (MeshPeer).
893 static struct GNUNET_CONTAINER_MultiHashMap *peers;
896 * Handle to communicate with core.
898 static struct GNUNET_CORE_Handle *core_handle;
903 static struct GNUNET_DHT_Handle *dht_handle;
906 * Handle to server lib.
908 static struct GNUNET_SERVER_Handle *server_handle;
911 * Handle to the statistics service.
913 static struct GNUNET_STATISTICS_Handle *stats;
916 * Notification context, to send messages to local clients.
918 static struct GNUNET_SERVER_NotificationContext *nc;
921 * Local peer own ID (memory efficient handle).
923 static GNUNET_PEER_Id myid;
926 * Local peer own ID (full value).
928 static struct GNUNET_PeerIdentity my_full_id;
933 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
938 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
941 * All ports clients of this peer have opened.
943 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
946 * Task to periodically announce itself in the network.
948 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
951 * Next ID to assign to a client.
953 unsigned int next_client_id;
956 /******************************************************************************/
957 /*********************** DECLARATIONS **************************/
958 /******************************************************************************/
961 * Function to process paths received for a new peer addition. The recorded
962 * paths form the initial tunnel, which can be optimized later.
963 * Called on each result obtained for the DHT search.
966 * @param exp when will this value expire
967 * @param key key of the result
968 * @param type type of the result
969 * @param size number of bytes in data
970 * @param data pointer to the result data
973 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
974 const struct GNUNET_HashCode * key,
975 const struct GNUNET_PeerIdentity *get_path,
976 unsigned int get_path_length,
977 const struct GNUNET_PeerIdentity *put_path,
978 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
979 size_t size, const void *data);
983 * Retrieve the MeshPeer stucture associated with the peer, create one
984 * and insert it in the appropriate structures if the peer is not known yet.
986 * @param peer Full identity of the peer.
988 * @return Existing or newly created peer info.
990 static struct MeshPeer *
991 peer_get (const struct GNUNET_PeerIdentity *peer);
995 * Retrieve the MeshPeer stucture associated with the peer, create one
996 * and insert it in the appropriate structures if the peer is not known yet.
998 * @param peer Short identity of the peer.
1000 * @return Existing or newly created peer info.
1002 static struct MeshPeer *
1003 peer_get_short (const GNUNET_PEER_Id peer);
1007 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1008 * to obtain a local peer -> destination path and interning the peer ids.
1010 * @return Newly allocated and created path
1012 static struct MeshPeerPath *
1013 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1014 unsigned int get_path_length,
1015 const struct GNUNET_PeerIdentity *put_path,
1016 unsigned int put_path_length);
1020 * Adds a path to the data structs of all the peers in the path
1022 * @param p Path to process.
1023 * @param confirmed Whether we know if the path works or not.
1026 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1030 * Search for a tunnel by global ID using full PeerIdentities.
1032 * @param t Tunnel containing the channel.
1033 * @param chid Public channel number.
1035 * @return channel handler, NULL if doesn't exist
1037 static struct MeshChannel *
1038 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1041 * Modify the data message ID from global to local and send to client.
1043 * @param ch Channel on which to send the message.
1044 * @param msg Message to modify and send.
1045 * @param fwd Forward?
1048 channel_send_client_data (struct MeshChannel *ch,
1049 const struct GNUNET_MESH_Data *msg,
1054 * Change the tunnel state.
1056 * @param t Tunnel whose state to change.
1057 * @param state New state.
1060 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1064 * Notify a tunnel that a connection has broken that affects at least
1065 * some of its peers.
1067 * @param t Tunnel affected.
1068 * @param p1 Peer that got disconnected from p2.
1069 * @param p2 Peer that got disconnected from p1.
1071 * @return Short ID of the peer disconnected (either p1 or p2).
1072 * 0 if the tunnel remained unaffected.
1074 static GNUNET_PEER_Id
1075 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1076 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1079 * @brief Use the given path for the tunnel.
1080 * Update the next and prev hops (and RCs).
1081 * (Re)start the path refresh in case the tunnel is locally owned.
1083 * @param t Tunnel to update.
1084 * @param p Path to use.
1086 * @return Connection created.
1088 static struct MeshConnection *
1089 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1092 * Tunnel is empty: destroy it.
1094 * Notifies all participants (peers, cleints) about the destruction.
1096 * @param t Tunnel to destroy.
1099 tunnel_destroy_empty (struct MeshTunnel2 *t);
1102 * Destroy the tunnel.
1104 * This function does not generate any warning traffic to clients or peers.
1107 * Cancel messages belonging to this tunnel queued to neighbors.
1108 * Free any allocated resources linked to the tunnel.
1110 * @param t The tunnel to destroy.
1113 tunnel_destroy (struct MeshTunnel2 *t);
1116 * Create a connection.
1118 * @param cid Connection ID.
1120 static struct MeshConnection *
1121 connection_new (const struct GNUNET_HashCode *cid);
1124 * Connection is no longer needed: destroy it and remove from tunnel.
1126 * @param c Connection to destroy.
1129 connection_destroy (struct MeshConnection *c);
1132 * Send FWD keepalive packets for a connection.
1134 * @param cls Closure (connection for which to send the keepalive).
1135 * @param tc Notification context.
1138 connection_fwd_keepalive (void *cls,
1139 const struct GNUNET_SCHEDULER_TaskContext *tc);
1142 * Send BCK keepalive packets for a connection.
1144 * @param cls Closure (connection for which to send the keepalive).
1145 * @param tc Notification context.
1148 connection_bck_keepalive (void *cls,
1149 const struct GNUNET_SCHEDULER_TaskContext *tc);
1153 * Change the tunnel state.
1155 * @param c Connection whose state to change.
1156 * @param state New state.
1159 connection_change_state (struct MeshConnection* c,
1160 enum MeshConnectionState state);
1165 * @brief Queue and pass message to core when possible.
1167 * @param cls Closure (@c type dependant). It will be used by queue_send to
1168 * build the message to be sent if not already prebuilt.
1169 * @param type Type of the message, 0 for a raw message.
1170 * @param size Size of the message.
1171 * @param c Connection this message belongs to (cannot be NULL).
1172 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1173 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1176 queue_add (void* cls,
1179 struct MeshConnection* c,
1180 struct MeshChannel* ch,
1185 * Free a transmission that was already queued with all resources
1186 * associated to the request.
1188 * @param queue Queue handler to cancel.
1189 * @param clear_cls Is it necessary to free associated cls?
1192 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1196 * Core callback to write a queued packet to core buffer
1198 * @param cls Closure (peer info).
1199 * @param size Number of bytes available in buf.
1200 * @param buf Where the to write the message.
1202 * @return number of bytes written to buf
1205 queue_send (void *cls, size_t size, void *buf);
1209 * Demultiplex by message type and call appropriate handler for a message
1210 * towards a channel of a local tunnel.
1212 * @param t Tunnel this message came on.
1213 * @param msgh Message header.
1214 * @param fwd Is this message fwd?
1217 handle_decrypted (struct MeshTunnel2 *t,
1218 const struct GNUNET_MessageHeader *msgh,
1223 * Dummy function to separate declarations from definitions in function list.
1226 __mesh_divider______________________________________________________________();
1230 * Get string description for tunnel state.
1232 * @param s Tunnel state.
1234 * @return String representation.
1237 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1239 static char buf[128];
1243 case MESH_TUNNEL_NEW:
1244 return "MESH_TUNNEL_NEW";
1245 case MESH_TUNNEL_SEARCHING:
1246 return "MESH_TUNNEL_SEARCHING";
1247 case MESH_TUNNEL_WAITING:
1248 return "MESH_TUNNEL_WAITING";
1249 case MESH_TUNNEL_READY:
1250 return "MESH_TUNNEL_READY";
1251 case MESH_TUNNEL_RECONNECTING:
1252 return "MESH_TUNNEL_RECONNECTING";
1255 sprintf (buf, "%u (UNKNOWN STATE)", s);
1262 * Get string description for tunnel state.
1264 * @param s Tunnel state.
1266 * @return String representation.
1269 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1273 case MESH_CONNECTION_NEW:
1274 return "MESH_CONNECTION_NEW";
1275 case MESH_CONNECTION_SENT:
1276 return "MESH_CONNECTION_SENT";
1277 case MESH_CONNECTION_READY:
1278 return "MESH_CONNECTION_READY";
1280 return "MESH_CONNECTION_STATE_ERROR";
1286 /******************************************************************************/
1287 /************************ PERIODIC FUNCTIONS ****************************/
1288 /******************************************************************************/
1291 * Periodically announce self id in the DHT
1293 * @param cls closure
1294 * @param tc task context
1297 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1299 struct PBlock block;
1301 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1303 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1307 * - Set data expiration in function of X
1308 * - Adapt X to churn
1310 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1312 block.id = my_full_id;
1313 GNUNET_DHT_put (dht_handle, /* DHT handle */
1314 &my_full_id.hashPubKey, /* Key to use */
1315 dht_replication_level, /* Replication level */
1316 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1317 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1318 sizeof (block), /* Size of the data */
1319 (const char *) &block, /* Data itself */
1320 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1321 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1322 NULL, /* Continuation */
1323 NULL); /* Continuation closure */
1325 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1329 /******************************************************************************/
1330 /****************** GENERAL HELPER FUNCTIONS ************************/
1331 /******************************************************************************/
1335 * Get the static string for a peer ID.
1339 * @return Static string for it's ID.
1342 peer2s (const struct MeshPeer *peer)
1346 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1351 * Get the previous hop in a connection
1353 * @param c Connection.
1355 * @return Previous peer in the connection.
1357 static struct MeshPeer *
1358 connection_get_prev_hop (struct MeshConnection *c)
1362 if (0 == c->own_pos || c->path->length < 2)
1363 id = c->path->peers[0];
1365 id = c->path->peers[c->own_pos - 1];
1367 return peer_get_short (id);
1372 * Get the next hop in a connection
1374 * @param c Connection.
1376 * @return Next peer in the connection.
1378 static struct MeshPeer *
1379 connection_get_next_hop (struct MeshConnection *c)
1383 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1384 id = c->path->peers[c->path->length - 1];
1386 id = c->path->peers[c->own_pos + 1];
1388 return peer_get_short (id);
1393 * Get the hop in a connection.
1395 * @param c Connection.
1396 * @param fwd Next hop?
1398 * @return Next peer in the connection.
1400 static struct MeshPeer *
1401 connection_get_hop (struct MeshConnection *c, int fwd)
1404 return connection_get_next_hop (c);
1405 return connection_get_prev_hop (c);
1409 * Check if client has registered with the service and has not disconnected
1411 * @param client the client to check
1413 * @return non-NULL if client exists in the global DLL
1415 static struct MeshClient *
1416 client_get (struct GNUNET_SERVER_Client *client)
1418 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1423 * Deletes a tunnel from a client (either owner or destination).
1425 * @param c Client whose tunnel to delete.
1426 * @param ch Channel which should be deleted.
1429 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1435 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1437 if (GNUNET_YES != res)
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1442 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1444 if (GNUNET_YES != res)
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1451 * Notify the appropriate client that a new incoming channel was created.
1453 * @param ch Channel that was created.
1456 send_local_channel_create (struct MeshChannel *ch)
1458 struct GNUNET_MESH_ChannelMessage msg;
1459 struct MeshTunnel2 *t = ch->t;
1461 if (NULL == ch->dest)
1463 msg.header.size = htons (sizeof (msg));
1464 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1465 msg.channel_id = htonl (ch->lid_dest);
1466 msg.port = htonl (ch->port);
1468 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1469 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1470 msg.opt = htonl (msg.opt);
1471 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1472 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1473 &msg.header, GNUNET_NO);
1478 * Notify a client that the incoming tunnel is no longer valid.
1480 * @param ch Channel that is destroyed.
1481 * @param fwd Forward notification (owner->dest)?
1484 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1486 struct GNUNET_MESH_ChannelMessage msg;
1487 struct MeshClient *c;
1489 c = fwd ? ch->dest : ch->root;
1495 msg.header.size = htons (sizeof (msg));
1496 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1497 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1498 msg.port = htonl (0);
1499 memset (&msg.peer, 0, sizeof (msg.peer));
1500 msg.opt = htonl (0);
1501 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1502 &msg.header, GNUNET_NO);
1507 * Build a local ACK message and send it to a local client, if needed.
1509 * If the client was already allowed to send data, do nothing.
1511 * @param ch Channel on which to send the ACK.
1512 * @param c Client to whom send the ACK.
1513 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
1516 send_local_ack (struct MeshChannel *ch, int fwd)
1518 struct GNUNET_MESH_LocalAck msg;
1519 struct MeshChannelReliability *rel;
1520 struct MeshClient *c;
1522 c = fwd ? ch->root : ch->dest;
1523 rel = fwd ? ch->root_rel : ch->dest_rel;
1525 if (GNUNET_YES == rel->client_ready)
1526 return; /* don't send double ACKs to client */
1528 rel->client_ready = GNUNET_YES;
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "send local %s ack on %s:%X towards %p\n",
1532 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1535 || ( fwd && (0 == ch->lid_root || c != ch->root))
1536 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1541 msg.header.size = htons (sizeof (msg));
1542 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1543 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1544 GNUNET_SERVER_notification_context_unicast (nc,
1553 * Count established (ready) connections of a tunnel.
1555 * @param t Tunnel on which to send the message.
1557 * @return Number of connections.
1560 tunnel_count_connections (struct MeshTunnel2 *t)
1562 struct MeshConnection *c;
1565 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1572 * Pick a connection on which send the next data message.
1574 * @param t Tunnel on which to send the message.
1575 * @param fwd Is this a fwd message?
1577 * @return The connection on which to send the next message.
1579 static struct MeshConnection *
1580 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1582 struct MeshConnection *c;
1583 struct MeshConnection *best;
1584 struct MeshFlowControl *fc;
1585 unsigned int lowest_q;
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
1590 lowest_q = UINT_MAX;
1591 for (c = t->connection_head; NULL != c; c = c->next)
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
1594 GNUNET_h2s (&c->id), c->state);
1595 if (MESH_CONNECTION_READY == c->state)
1597 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
1604 if (fc->queue_n < lowest_q)
1607 lowest_q = fc->queue_n;
1616 * Is this peer the first one on the connection?
1618 * @param c Connection.
1619 * @param fwd Is this about fwd traffic?
1621 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1624 connection_is_origin (struct MeshConnection *c, int fwd)
1626 if (!fwd && c->own_pos == c->path->length - 1)
1628 if (fwd && c->own_pos == 0)
1635 * Is this peer the last one on the connection?
1637 * @param c Connection.
1638 * @param fwd Is this about fwd traffic?
1639 * Note that the ROOT is the terminal for BCK traffic!
1641 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1644 connection_is_terminal (struct MeshConnection *c, int fwd)
1646 if (fwd && c->own_pos == c->path->length - 1)
1648 if (!fwd && c->own_pos == 0)
1655 * Is the recipient client for this channel on this peer?
1657 * @param ch Channel.
1658 * @param fwd Is this for fwd traffic?
1660 * @return GNUNET_YES in case it is.
1663 channel_is_terminal (struct MeshChannel *ch, int fwd)
1665 if (NULL == ch->t || NULL == ch->t->connection_head)
1670 return connection_is_terminal (ch->t->connection_head, fwd);
1675 * Get free buffer space towards the client on a specific channel.
1677 * @param ch Channel.
1678 * @param fwd Is query about FWD traffic?
1680 * @return Free buffer space [0 - 64]
1683 channel_get_buffer (struct MeshChannel *ch, int fwd)
1685 struct MeshChannelReliability *rel;
1687 rel = fwd ? ch->dest_rel : ch->root_rel;
1689 /* If rel is NULL it means that the end is not yet created,
1690 * most probably is a loopback channel at the point of sending
1691 * the ChannelCreate to itself.
1696 return (64 - rel->n_recv);
1701 * Get free buffer space in a connection.
1703 * @param c Connection.
1704 * @param fwd Is query about FWD traffic?
1706 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1709 connection_get_buffer (struct MeshConnection *c, int fwd)
1711 struct MeshFlowControl *fc;
1713 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1715 return (fc->queue_max - fc->queue_n);
1720 * Get the total buffer space for a tunnel.
1723 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1725 struct MeshConnection *c;
1726 struct MeshFlowControl *fc;
1727 unsigned int buffer;
1729 c = t->connection_head;
1738 /* If terminal, return biggest channel buffer */
1739 if (connection_is_terminal (c, fwd))
1741 struct MeshChannel *ch;
1742 unsigned int ch_buf;
1744 if (NULL == t->channel_head)
1747 for (ch = t->channel_head; NULL != ch; ch = ch->next)
1749 ch_buf = channel_get_buffer (ch, fwd);
1750 if (ch_buf > buffer)
1756 /* If not terminal, return sum of connection buffers */
1759 if (c->state != MESH_CONNECTION_READY)
1762 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1763 buffer += fc->last_ack_recv - fc->last_pid_sent;
1772 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1773 * Encrypt data with the tunnel key.
1775 * @param t Tunnel whose key to use.
1776 * @param dst Destination for the encrypted data.
1777 * @param src Source of the plaintext.
1778 * @param size Size of the plaintext.
1779 * @param iv Initialization Vector to use.
1780 * @param fwd Is this a fwd message?
1783 tunnel_encrypt (struct MeshTunnel2 *t,
1784 void *dst, const void *src,
1785 size_t size, uint64_t iv, int fwd)
1787 memcpy (dst, src, size);
1792 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1793 * Decrypt data with the tunnel key.
1795 * @param t Tunnel whose key to use.
1796 * @param dst Destination for the plaintext.
1797 * @param src Source of the encrypted data.
1798 * @param size Size of the encrypted data.
1799 * @param iv Initialization Vector to use.
1800 * @param fwd Is this a fwd message?
1803 tunnel_decrypt (struct MeshTunnel2 *t,
1804 void *dst, const void *src,
1805 size_t size, uint64_t iv, int fwd)
1807 memcpy (dst, src, size);
1812 * Sends an already built message on a connection, properly registering
1813 * all used resources.
1815 * @param message Message to send. Function makes a copy of it.
1816 * If message is not hop-by-hop, decrements TTL of copy.
1817 * @param c Connection on which this message is transmitted.
1818 * @param ch Channel on which this message is transmitted, or NULL.
1819 * @param fwd Is this a fwd message?
1822 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1823 struct MeshConnection *c,
1824 struct MeshChannel *ch,
1831 size = ntohs (message->size);
1832 data = GNUNET_malloc (size);
1833 memcpy (data, message, size);
1834 type = ntohs (message->type);
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1836 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1840 struct GNUNET_MESH_Encrypted *emsg;
1841 struct GNUNET_MESH_ACK *amsg;
1842 struct GNUNET_MESH_Poll *pmsg;
1843 struct GNUNET_MESH_ConnectionDestroy *dmsg;
1844 struct GNUNET_MESH_ConnectionBroken *bmsg;
1847 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1848 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1849 emsg = (struct GNUNET_MESH_Encrypted *) data;
1850 ttl = ntohl (emsg->ttl);
1853 GNUNET_break_op (0);
1857 emsg->ttl = htonl (ttl - 1);
1858 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1862 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1863 amsg = (struct GNUNET_MESH_ACK *) data;
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1868 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1869 pmsg = (struct GNUNET_MESH_Poll *) data;
1871 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1875 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1876 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
1881 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1882 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
1887 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1888 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1905 * Sends an already built message on a tunnel, choosing the best connection.
1907 * @param message Message to send. Function modifies it.
1908 * @param t Tunnel on which this message is transmitted.
1909 * @param ch Channel on which this message is transmitted.
1910 * @param fwd Is this a fwd message?
1913 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1914 struct MeshTunnel2 *t,
1915 struct MeshChannel *ch,
1918 struct MeshConnection *c;
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1923 c = tunnel_get_connection (t, fwd);
1926 GNUNET_break (GNUNET_YES == t->destroy);
1929 type = ntohs (msg->header.type);
1932 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1933 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1934 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1935 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1937 msg->ttl = htonl (default_ttl);
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1941 GNUNET_MESH_DEBUG_M2S (type));
1946 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1951 * Sends an already built message on a channel, properly registering
1952 * all used resources and encrypting the message with the tunnel's key.
1954 * @param message Message to send. Function makes a copy of it.
1955 * @param ch Channel on which this message is transmitted.
1956 * @param fwd Is this a fwd message?
1959 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1960 struct MeshChannel *ch,
1963 struct GNUNET_MESH_Encrypted *msg;
1964 size_t size = ntohs (message->size);
1965 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
1970 peer2s (ch->t->peer), ch->gid, fwd ? "FWD" : "BCK");
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1972 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1974 if (channel_is_terminal (ch, fwd))
1976 handle_decrypted (ch->t, message, fwd);
1980 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1981 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1983 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1984 msg->header.type = htons (type);
1985 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1986 msg->iv = GNUNET_htonll (iv);
1987 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1988 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1993 * Sends a CREATE CONNECTION message for a path to a peer.
1994 * Changes the connection and tunnel states if necessary.
1996 * @param connection Connection to create.
1999 send_connection_create (struct MeshConnection *connection)
2001 struct MeshTunnel2 *t;
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2006 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2007 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2008 (connection->path->length *
2009 sizeof (struct GNUNET_PeerIdentity)),
2014 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
2015 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2016 if (MESH_CONNECTION_NEW == connection->state)
2017 connection_change_state (connection, MESH_CONNECTION_SENT);
2022 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
2025 * @param connection Connection to confirm.
2026 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
2029 send_connection_ack (struct MeshConnection *connection, int fwd)
2031 struct MeshTunnel2 *t;
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
2036 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
2037 sizeof (struct GNUNET_MESH_ConnectionACK),
2041 if (MESH_TUNNEL_NEW == t->state)
2042 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2043 if (MESH_CONNECTION_READY != connection->state)
2044 connection_change_state (connection, MESH_CONNECTION_SENT);
2049 * Core callback to write a pre-constructed data packet to core buffer
2051 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2052 * @param size Number of bytes available in buf.
2053 * @param buf Where the to write the message.
2055 * @return number of bytes written to buf
2058 send_core_data_raw (void *cls, size_t size, void *buf)
2060 struct GNUNET_MessageHeader *msg = cls;
2063 GNUNET_assert (NULL != msg);
2064 total_size = ntohs (msg->size);
2066 if (total_size > size)
2071 memcpy (buf, msg, total_size);
2078 * Function to send a create connection message to a peer.
2080 * @param c Connection to create.
2081 * @param size number of bytes available in buf
2082 * @param buf where the callee should write the message
2083 * @return number of bytes written to buf
2086 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
2088 struct GNUNET_MESH_ConnectionCreate *msg;
2089 struct GNUNET_PeerIdentity *peer_ptr;
2090 struct MeshPeerPath *p = c->path;
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
2096 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2097 p->length * sizeof (struct GNUNET_PeerIdentity);
2099 if (size < size_needed || NULL == buf)
2104 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
2105 msg->header.size = htons (size_needed);
2106 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
2109 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2110 for (i = 0; i < p->length; i++)
2112 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2116 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
2122 * Creates a path ack message in buf and frees all unused resources.
2124 * @param c Connection to send an ACK on.
2125 * @param size number of bytes available in buf
2126 * @param buf where the callee should write the message
2128 * @return number of bytes written to buf
2131 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
2133 struct GNUNET_MESH_ConnectionACK *msg = buf;
2134 struct MeshTunnel2 *t = c->t;
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
2137 GNUNET_assert (NULL != t);
2138 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
2143 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
2144 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
2148 /* TODO add signature */
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2151 return sizeof (struct GNUNET_MESH_ConnectionACK);
2156 * Destroy the peer_info and free any allocated resources linked to it
2158 * @param peer The peer_info to destroy.
2160 * @return GNUNET_OK on success
2163 peer_destroy (struct MeshPeer *peer)
2165 struct GNUNET_PeerIdentity id;
2166 struct MeshPeerPath *p;
2167 struct MeshPeerPath *nextp;
2169 GNUNET_PEER_resolve (peer->id, &id);
2170 GNUNET_PEER_change_rc (peer->id, -1);
2173 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2176 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2177 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2179 if (NULL != peer->dhtget)
2181 GNUNET_DHT_get_stop (peer->dhtget);
2183 p = peer->path_head;
2187 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2191 tunnel_destroy_empty (peer->tunnel);
2198 * Returns if peer is used (has a tunnel, is neighbor).
2200 * @peer Peer to check.
2202 * @return GNUNET_YES if peer is in use.
2205 peer_is_used (struct MeshPeer *peer)
2207 struct MeshPeerPath *p;
2209 if (NULL != peer->tunnel)
2212 for (p = peer->path_head; NULL != p; p = p->next)
2221 * Iterator over all the peers to get the oldest timestamp.
2223 * @param cls Closure (unsued).
2224 * @param key ID of the peer.
2225 * @param value Peer_Info of the peer.
2228 peer_get_oldest (void *cls,
2229 const struct GNUNET_HashCode *key,
2232 struct MeshPeer *p = value;
2233 struct GNUNET_TIME_Absolute *abs = cls;
2235 /* Don't count active peers */
2236 if (GNUNET_YES == peer_is_used (p))
2239 if (abs->abs_value_us < p->last_contact.abs_value_us)
2240 abs->abs_value_us = p->last_contact.abs_value_us;
2247 * Iterator over all the peers to remove the oldest entry.
2249 * @param cls Closure (unsued).
2250 * @param key ID of the peer.
2251 * @param value Peer_Info of the peer.
2254 peer_timeout (void *cls,
2255 const struct GNUNET_HashCode *key,
2258 struct MeshPeer *p = value;
2259 struct GNUNET_TIME_Absolute *abs = cls;
2261 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2262 GNUNET_NO == peer_is_used (p))
2272 * Delete oldest unused peer.
2275 peer_delete_oldest (void)
2277 struct GNUNET_TIME_Absolute abs;
2279 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2281 GNUNET_CONTAINER_multihashmap_iterate (peers,
2284 GNUNET_CONTAINER_multihashmap_iterate (peers,
2291 * Retrieve the MeshPeer stucture associated with the peer, create one
2292 * and insert it in the appropriate structures if the peer is not known yet.
2294 * @param peer Full identity of the peer.
2296 * @return Existing or newly created peer info.
2298 static struct MeshPeer *
2299 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2301 struct MeshPeer *peer;
2303 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2306 peer = GNUNET_new (struct MeshPeer);
2307 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2309 peer_delete_oldest ();
2311 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2312 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2313 peer->id = GNUNET_PEER_intern (peer_id);
2315 peer->last_contact = GNUNET_TIME_absolute_get();
2322 * Retrieve the MeshPeer stucture associated with the peer, create one
2323 * and insert it in the appropriate structures if the peer is not known yet.
2325 * @param peer Short identity of the peer.
2327 * @return Existing or newly created peer info.
2329 static struct MeshPeer *
2330 peer_get_short (const GNUNET_PEER_Id peer)
2332 return peer_get (GNUNET_PEER_resolve2 (peer));
2337 * Get a cost of a path for a peer considering existing tunnel connections.
2339 * @param peer Peer towards which the path is considered.
2340 * @param path Candidate path.
2342 * @return Cost of the path (path length + number of overlapping nodes)
2345 peer_get_path_cost (const struct MeshPeer *peer,
2346 const struct MeshPeerPath *path)
2348 struct MeshConnection *c;
2349 unsigned int overlap;
2357 GNUNET_assert (NULL != peer->tunnel);
2359 for (i = 0; i < path->length; i++)
2361 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2363 for (j = 0; j < c->path->length; j++)
2365 if (path->peers[i] == c->path->peers[j])
2373 return (path->length + overlap) * (path->score * -1);
2378 * Choose the best path towards a peer considering the tunnel properties.
2380 * @param peer The destination peer.
2382 * @return Best current known path towards the peer, if any.
2384 static struct MeshPeerPath *
2385 peer_get_best_path (const struct MeshPeer *peer)
2387 struct MeshPeerPath *best_p;
2388 struct MeshPeerPath *p;
2389 struct MeshConnection *c;
2390 unsigned int best_cost;
2393 best_cost = UINT_MAX;
2395 for (p = peer->path_head; NULL != p; p = p->next)
2397 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2401 continue; /* If path is in use in a connection, skip it. */
2403 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2413 queue_is_sendable (struct MeshPeerQueue *q)
2415 struct MeshFlowControl *fc;
2417 /* Is PID-independent? */
2420 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2421 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2425 /* Is PID allowed? */
2426 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2427 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2435 * Get first sendable message.
2437 * @param peer The destination peer.
2439 * @return Best current known path towards the peer, if any.
2441 static struct MeshPeerQueue *
2442 peer_get_first_message (const struct MeshPeer *peer)
2444 struct MeshPeerQueue *q;
2446 for (q = peer->queue_head; NULL != q; q = q->next)
2448 if (queue_is_sendable (q))
2457 * Try to establish a new connection to this peer in the given tunnel.
2458 * If the peer doesn't have any path to it yet, try to get one.
2459 * If the peer already has some path, send a CREATE CONNECTION towards it.
2461 * @param peer PeerInfo of the peer.
2464 peer_connect (struct MeshPeer *peer)
2466 struct MeshTunnel2 *t;
2467 struct MeshPeerPath *p;
2468 struct MeshConnection *c;
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "peer_connect towards %s\n",
2476 rerun_dhtget = GNUNET_NO;
2478 if (NULL != peer->path_head)
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2481 p = peer_get_best_path (peer);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2485 c = tunnel_use_path (t, p);
2488 /* This case can happen when the path includes a first hop that is
2489 * not yet known to be connected.
2491 * This happens quite often during testing when running mesh
2492 * under valgrind: core connect notifications come very late and the
2493 * DHT result has already come and created a valid path.
2494 * In this case, the peer->connections hashmap will be NULL and
2495 * tunnel_use_path will not be able to create a connection from that
2498 * Re-running the DHT GET should give core time to callback.
2501 rerun_dhtget = GNUNET_YES;
2505 send_connection_create (c);
2511 if (NULL != peer->dhtget && GNUNET_YES == rerun_dhtget)
2513 GNUNET_DHT_get_stop (peer->dhtget);
2514 peer->dhtget = NULL;
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 " Stopping DHT GET for peer %s\n", peer2s (peer));
2519 if (NULL == peer->dhtget)
2521 const struct GNUNET_PeerIdentity *id;
2523 id = GNUNET_PEER_resolve2 (peer->id);
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 " Starting DHT GET for peer %s\n", peer2s (peer));
2526 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2527 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2528 &id->hashPubKey, /* key to search */
2529 dht_replication_level, /* replication level */
2530 GNUNET_DHT_RO_RECORD_ROUTE |
2531 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2533 0, /* xquery bits */
2534 &dht_get_id_handler, peer);
2535 if (MESH_TUNNEL_NEW == t->state)
2536 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2542 * Get the first transmittable message for a connection.
2544 * @param c Connection.
2545 * @param fwd Is this FWD?
2547 * @return First transmittable message.
2549 static struct MeshPeerQueue *
2550 connection_get_first_message (struct MeshConnection *c, int fwd)
2552 struct MeshPeerQueue *q;
2555 p = connection_get_hop (c, fwd);
2557 for (q = p->queue_head; NULL != q; q = q->next)
2561 if (queue_is_sendable (q))
2570 * @brief Re-initiate traffic on this connection if necessary.
2572 * Check if there is traffic queued towards this peer
2573 * and the core transmit handle is NULL (traffic was stalled).
2574 * If so, call core tmt rdy.
2576 * @param c Connection on which initiate traffic.
2577 * @param fwd Is this about fwd traffic?
2580 connection_unlock_queue (struct MeshConnection *c, int fwd)
2582 struct MeshPeer *peer;
2583 struct MeshPeerQueue *q;
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 "connection_unlock_queue %s on %s\n",
2588 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2590 if (connection_is_origin (c, fwd))
2592 struct MeshTunnel2 *t = c->t;
2593 struct MeshChannel *ch;
2594 struct MeshChannelReliability *rel;
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2597 /* FIXME randomize channel selection, not always first channel */
2598 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2600 rel = fwd ? ch->root_rel : ch->dest_rel;
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2603 ch->gid, rel->client_ready ? "ready " : "not ready");
2604 if (GNUNET_NO == rel->client_ready)
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2607 send_local_ack (ch, fwd);
2608 return; /* FIXME authorize all channels? */
2614 peer = connection_get_hop (c, fwd);
2616 if (NULL != peer->core_transmit)
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2619 return; /* Already unlocked */
2622 q = connection_get_first_message (c, fwd);
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2626 return; /* Nothing to transmit */
2630 peer->core_transmit =
2631 GNUNET_CORE_notify_transmit_ready (core_handle,
2634 GNUNET_TIME_UNIT_FOREVER_REL,
2635 GNUNET_PEER_resolve2 (peer->id),
2643 * Cancel all transmissions that belong to a certain connection.
2645 * @param c Connection which to cancel.
2646 * @param fwd Cancel fwd traffic?
2649 connection_cancel_queues (struct MeshConnection *c, int fwd)
2651 struct MeshPeerQueue *q;
2652 struct MeshPeerQueue *next;
2653 struct MeshFlowControl *fc;
2654 struct MeshPeer *peer;
2661 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2662 peer = connection_get_hop (c, fwd);
2664 for (q = peer->queue_head; NULL != q; q = next)
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 "connection_cancel_queue %s\n",
2671 GNUNET_MESH_DEBUG_M2S (q->type));
2672 queue_destroy (q, GNUNET_YES);
2675 if (NULL == peer->queue_head)
2677 if (NULL != peer->core_transmit)
2679 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2680 peer->core_transmit = NULL;
2682 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2684 GNUNET_SCHEDULER_cancel (fc->poll_task);
2685 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2692 * Add the path to the peer and update the path used to reach it in case this
2695 * @param peer_info Destination peer to add the path to.
2696 * @param path New path to add. Last peer must be the peer in arg 1.
2697 * Path will be either used of freed if already known.
2698 * @param trusted Do we trust that this path is real?
2701 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2704 struct MeshPeerPath *aux;
2708 if ((NULL == peer_info) || (NULL == path))
2711 path_destroy (path);
2714 if (path->peers[path->length - 1] != peer_info->id)
2717 path_destroy (path);
2720 if (2 >= path->length && GNUNET_NO == trusted)
2722 /* Only allow CORE to tell us about direct paths */
2723 path_destroy (path);
2726 for (l = 1; l < path->length; l++)
2728 if (path->peers[l] == myid)
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2731 for (l2 = 0; l2 < path->length - l; l2++)
2733 path->peers[l2] = path->peers[l + l2];
2738 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2743 path->length, peer2s (peer_info));
2745 l = path_get_length (path);
2748 path_destroy (path);
2752 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2753 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2755 l2 = path_get_length (aux);
2758 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2759 peer_info->path_tail, aux, path);
2764 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2766 path_destroy (path);
2771 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2778 * Add the path to the origin peer and update the path used to reach it in case
2779 * this is the shortest.
2780 * The path is given in peer_info -> destination, therefore we turn the path
2781 * upside down first.
2783 * @param peer_info Peer to add the path to, being the origin of the path.
2784 * @param path New path to add after being inversed.
2785 * Path will be either used or freed.
2786 * @param trusted Do we trust that this path is real?
2789 peer_add_path_to_origin (struct MeshPeer *peer_info,
2790 struct MeshPeerPath *path, int trusted)
2795 peer_add_path (peer_info, path, trusted);
2801 * Function called if a connection has been stalled for a while,
2802 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2804 * @param cls Closure (poll ctx).
2805 * @param tc TaskContext.
2808 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2810 struct MeshFlowControl *fc = cls;
2811 struct GNUNET_MESH_Poll msg;
2812 struct MeshConnection *c;
2814 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2815 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2823 peer2s (c->t->peer), c->id);
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
2825 fc == &c->fwd_fc ? "FWD" : "BCK");
2827 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2828 msg.header.size = htons (sizeof (msg));
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2830 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2831 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2832 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2833 &connection_poll, fc);
2838 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2839 * to obtain a local peer -> destination path and interning the peer ids.
2841 * @return Newly allocated and created path
2843 static struct MeshPeerPath *
2844 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2845 unsigned int get_path_length,
2846 const struct GNUNET_PeerIdentity *put_path,
2847 unsigned int put_path_length)
2849 struct MeshPeerPath *p;
2855 GNUNET_PEER_change_rc (myid, 1);
2856 i = get_path_length;
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2858 for (i--; i >= 0; i--)
2860 id = GNUNET_PEER_intern (&get_path[i]);
2861 if (p->length > 0 && id == p->peers[p->length - 1])
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2864 GNUNET_PEER_change_rc (id, -1);
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2869 GNUNET_i2s (&get_path[i]));
2871 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2872 p->peers[p->length - 1] = id;
2875 i = put_path_length;
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2877 for (i--; i >= 0; i--)
2879 id = GNUNET_PEER_intern (&put_path[i]);
2882 /* PUT path went through us, so discard the path up until now and start
2883 * from here to get a much shorter (and loop-free) path.
2888 if (p->length > 0 && id == p->peers[p->length - 1])
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2891 GNUNET_PEER_change_rc (id, -1);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2896 GNUNET_i2s (&put_path[i]));
2898 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2899 p->peers[p->length - 1] = id;
2903 if (get_path_length > 0)
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2905 GNUNET_i2s (&get_path[0]));
2906 if (put_path_length > 0)
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2908 GNUNET_i2s (&put_path[0]));
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2911 for (i = 0; i < p->length; i++)
2913 struct GNUNET_PeerIdentity peer_id;
2915 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2917 GNUNET_i2s (&peer_id));
2925 * Adds a path to the peer_infos of all the peers in the path
2927 * @param p Path to process.
2928 * @param confirmed Whether we know if the path works or not.
2931 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2935 /* TODO: invert and add */
2936 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2937 for (i++; i < p->length; i++)
2939 struct MeshPeer *aux;
2940 struct MeshPeerPath *copy;
2942 aux = peer_get_short (p->peers[i]);
2943 copy = path_duplicate (p);
2944 copy->length = i + 1;
2945 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2951 * Search for a channel among the channels for a client
2953 * @param c the client whose channels to search in
2954 * @param chid the local id of the channel
2956 * @return channel handler, NULL if doesn't exist
2958 static struct MeshChannel *
2959 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2962 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2964 GNUNET_break_op (0);
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2968 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2969 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2970 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2976 channel_debug (struct MeshChannel *ch)
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X\n",
2984 peer2s (ch->t->peer), ch->gid);
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2986 ch->root, ch->root_rel);
2987 if (NULL != ch->root)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
2991 ch->root_rel->client_ready ? "YES" : "NO");
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2995 ch->dest, ch->dest_rel);
2996 if (NULL != ch->dest)
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
3000 ch->dest_rel->client_ready ? "YES" : "NO");
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
3006 fc_debug (struct MeshFlowControl *fc)
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
3009 fc->last_pid_recv, fc->last_ack_sent);
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
3011 fc->last_pid_sent, fc->last_ack_recv);
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
3013 fc->queue_n, fc->queue_max);
3017 connection_debug (struct MeshConnection *c)
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
3025 peer2s (c->t->peer), GNUNET_h2s (&c->id));
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
3027 c->state, c->pending_messages);
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
3029 fc_debug (&c->fwd_fc);
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
3031 fc_debug (&c->bck_fc);
3036 static struct MeshChannel *
3037 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
3039 struct MeshChannel *ch;
3044 for (ch = t->channel_head; NULL != ch; ch = ch->next)
3046 if (ch->gid == chid)
3055 * Change the tunnel state.
3057 * @param t Tunnel whose state to change.
3058 * @param state New state.
3061 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3066 "Tunnel %s state was %s\n",
3068 GNUNET_MESH_DEBUG_TS2S (t->state));
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3070 "Tunnel %s state is now %s\n",
3072 GNUNET_MESH_DEBUG_TS2S (state));
3078 * Cache a message to be sent once tunnel is online.
3080 * @param t Tunnel to hold the message.
3081 * @param ch Channel the message is about.
3082 * @param msg Message itself (copy will be made).
3083 * @param fwd Is this fwd?
3086 tunnel_queue_data (struct MeshTunnel2 *t,
3087 struct MeshChannel *ch,
3088 struct GNUNET_MessageHeader *msg,
3091 struct MeshTunnelQueue *tq;
3092 uint16_t size = ntohs (msg->size);
3094 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
3097 memcpy (&tq[1], msg, size);
3098 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
3103 * Send all cached messages that we can, tunnel is online.
3105 * @param t Tunnel that holds the messages.
3106 * @param fwd Is this fwd?
3109 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
3111 struct MeshTunnelQueue *tq;
3112 struct MeshTunnelQueue *next;
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3116 "tunnel_send_queued_data on tunnel %s\n",
3118 room = tunnel_get_buffer (t, fwd);
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
3120 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
3124 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
3125 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
3134 connection_change_state (struct MeshConnection* c,
3135 enum MeshConnectionState state)
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 "Connection %s state was %s\n",
3139 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "Connection %s state is now %s\n",
3142 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
3148 * Add a client to a channel, initializing all needed data structures.
3150 * @param ch Channel to which add the client.
3151 * @param c Client which to add to the channel.
3154 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3156 struct MeshTunnel2 *t = ch->t;
3158 if (NULL != ch->dest)
3164 /* Assign local id as destination */
3165 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3166 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3167 ch->lid_dest = t->next_local_chid++;
3168 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3170 /* Store in client's hashmap */
3172 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3174 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3180 GNUNET_break (NULL == ch->dest_rel);
3181 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3182 ch->dest_rel->ch = ch;
3183 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3189 static struct MeshConnection *
3190 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3192 struct MeshConnection *c;
3193 struct GNUNET_HashCode cid;
3194 struct MeshPeer *peer;
3195 unsigned int own_pos;
3197 if (NULL == t || NULL == p)
3203 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3205 c = connection_new (&cid);
3207 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
3208 for (own_pos = 0; own_pos < p->length; own_pos++)
3210 if (p->peers[own_pos] == myid)
3213 if (own_pos > p->length - 1)
3216 connection_destroy (c);
3219 c->own_pos = own_pos;
3224 c->fwd_maintenance_task =
3225 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3226 &connection_fwd_keepalive, c);
3229 peer = connection_get_next_hop (c);
3230 if (NULL == peer->connections)
3232 connection_destroy (c);
3235 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3237 peer = connection_get_prev_hop (c);
3238 if (NULL == peer->connections)
3240 connection_destroy (c);
3243 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3244 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3250 * Notifies a tunnel that a connection has broken that affects at least
3251 * some of its peers. Sends a notification towards the root of the tree.
3252 * In case the peer is the owner of the tree, notifies the client that owns
3253 * the tunnel and tries to reconnect.
3255 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3257 * @param t Tunnel affected.
3258 * @param p1 Peer that got disconnected from p2.
3259 * @param p2 Peer that got disconnected from p1.
3261 * @return Short ID of the peer disconnected (either p1 or p2).
3262 * 0 if the tunnel remained unaffected.
3264 static GNUNET_PEER_Id
3265 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3266 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3268 // if (myid != p1 && myid != p2) FIXME
3273 // if (tree_get_predecessor (t->tree) != 0)
3275 // /* We are the peer still connected, notify owner of the disconnection. */
3276 // struct GNUNET_MESH_PathBroken msg;
3277 // struct GNUNET_PeerIdentity neighbor;
3279 // msg.header.size = htons (sizeof (msg));
3280 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3281 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3282 // msg.tid = htonl (t->id.tid);
3283 // msg.peer1 = my_full_id;
3284 // GNUNET_PEER_resolve (pid, &msg.peer2);
3285 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3286 // send_prebuilt_message (&msg.header, &neighbor, t);
3293 * Send an end-to-end ACK message for the most recent in-sequence payload.
3295 * If channel is not reliable, do nothing.
3297 * @param ch Channel this is about.
3298 * @param fwd Is for FWD traffic? (ACK dest->owner)
3301 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3303 struct GNUNET_MESH_DataACK msg;
3304 struct MeshChannelReliability *rel;
3305 struct MeshReliableMessage *copy;
3310 if (GNUNET_NO == ch->reliable)
3314 rel = fwd ? ch->dest_rel : ch->root_rel;
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3316 "send_data_ack for %u\n",
3319 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3320 msg.header.type = htons (type);
3321 msg.header.size = htons (sizeof (msg));
3322 msg.chid = htonl (ch->gid);
3323 msg.mid = htonl (rel->mid_recv - 1);
3325 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3327 if (copy->type != type)
3329 delta = copy->mid - rel->mid_recv;
3332 mask = 0x1LL << delta;
3333 msg.futures |= mask;
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3336 copy->mid, delta, mask, msg.futures);
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3340 send_prebuilt_message_channel (&msg.header, ch, fwd);
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3346 * Send an ACK informing the predecessor about the available buffer space.
3348 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3349 * the ACK itself goes "back" (dest->root).
3351 * @param c Connection on which to send the ACK.
3352 * @param buffer How much space free to advertise?
3353 * @param fwd Is this FWD ACK? (Going dest->owner)
3356 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3358 struct MeshFlowControl *next_fc;
3359 struct MeshFlowControl *prev_fc;
3360 struct GNUNET_MESH_ACK msg;
3364 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3365 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3368 "connection send %s ack on %s\n",
3369 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3371 /* Check if we need to transmit the ACK */
3372 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3376 " last pid recv: %u, last ack sent: %u\n",
3377 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3381 /* Ok, ACK might be necessary, what PID to ACK? */
3382 delta = next_fc->queue_max - next_fc->queue_n;
3383 ack = prev_fc->last_pid_recv + delta;
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386 " last pid %u, last ack %u, qmax %u, q %u\n",
3387 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3388 next_fc->queue_max, next_fc->queue_n);
3389 if (ack == prev_fc->last_ack_sent)
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3395 prev_fc->last_ack_sent = ack;
3397 /* Build ACK message and send on connection */
3398 msg.header.size = htons (sizeof (msg));
3399 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3400 msg.ack = htonl (ack);
3403 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3408 * Modify the mesh message TID from global to local and send to client.
3410 * @param ch Channel on which to send the message.
3411 * @param msg Message to modify and send.
3412 * @param c Client to send to.
3413 * @param tid Tunnel ID to use (c can be both owner and client).
3416 channel_send_client_to_tid (struct MeshChannel *ch,
3417 const struct GNUNET_MESH_Data *msg,
3418 struct MeshClient *c, MESH_ChannelNumber id)
3420 struct GNUNET_MESH_LocalData *copy;
3421 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3422 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3424 if (size < sizeof (struct GNUNET_MessageHeader))
3426 GNUNET_break_op (0);
3434 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3435 memcpy (©[1], &msg[1], size);
3436 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3437 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3438 copy->id = htonl (id);
3439 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3440 ©->header, GNUNET_NO);
3445 * We have received a message out of order, or the client is not ready.
3446 * Buffer it until we receive an ACK from the client or the missing
3447 * message from the channel.
3449 * @param msg Message to buffer (MUST be of type MESH_DATA).
3450 * @param rel Reliability data to the corresponding direction.
3453 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3454 struct MeshChannelReliability *rel)
3456 struct MeshReliableMessage *copy;
3457 struct MeshReliableMessage *prev;
3461 size = ntohs (msg->header.size);
3462 mid = ntohl (msg->mid);
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3466 copy = GNUNET_malloc (sizeof (*copy) + size);
3469 memcpy (©[1], msg, size);
3473 // FIXME do something better than O(n), although n < 64...
3474 // FIXME start from the end (most messages are the latest ones)
3475 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3478 if (GMC_is_pid_bigger (prev->mid, mid))
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3481 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3487 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3493 channel_send_client_data (struct MeshChannel *ch,
3494 const struct GNUNET_MESH_Data *msg,
3499 if (ch->dest_rel->client_ready)
3500 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3502 channel_rel_add_buffered_data (msg, ch->dest_rel);
3506 if (ch->root_rel->client_ready)
3507 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3509 channel_rel_add_buffered_data (msg, ch->root_rel);
3515 * Send a buffered message to the client, for in order delivery or
3516 * as result of client ACK.
3518 * @param ch Channel on which to empty the message buffer.
3519 * @param c Client to send to.
3520 * @param rel Reliability structure to corresponding peer.
3521 * If rel == bck_rel, this is FWD data.
3524 channel_send_client_buffered_data (struct MeshChannel *ch,
3525 struct MeshClient *c,
3528 struct MeshReliableMessage *copy;
3529 struct MeshChannelReliability *rel;
3531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3532 rel = fwd ? ch->dest_rel : ch->root_rel;
3533 if (GNUNET_NO == rel->client_ready)
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3539 copy = rel->head_recv;
3540 /* We never buffer channel management messages */
3543 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3545 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3548 " have %u! now expecting %u\n",
3549 copy->mid, rel->mid_recv + 1);
3550 channel_send_client_data (ch, msg, fwd);
3553 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3559 " reliable && don't have %u, next is %u\n",
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3570 * Destroy a reliable message after it has been acknowledged, either by
3571 * direct mid ACK or bitfield. Updates the appropriate data structures and
3572 * timers and frees all memory.
3574 * @param copy Message that is no longer needed: remote peer got it.
3577 rel_message_free (struct MeshReliableMessage *copy)
3579 struct MeshChannelReliability *rel;
3580 struct GNUNET_TIME_Relative time;
3583 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3584 rel->expected_delay.rel_value_us *= 7;
3585 rel->expected_delay.rel_value_us += time.rel_value_us;
3586 rel->expected_delay.rel_value_us /= 8;
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3591 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3593 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3595 rel->retry_timer = rel->expected_delay;
3596 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3602 * Destroy all reliable messages queued for a channel,
3603 * during a channel destruction.
3604 * Frees the reliability structure itself.
3606 * @param rel Reliability data for a channel.
3609 channel_rel_free_all (struct MeshChannelReliability *rel)
3611 struct MeshReliableMessage *copy;
3612 struct MeshReliableMessage *next;
3617 for (copy = rel->head_recv; NULL != copy; copy = next)
3620 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3623 for (copy = rel->head_sent; NULL != copy; copy = next)
3626 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3629 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3630 GNUNET_SCHEDULER_cancel (rel->retry_task);
3636 * Mark future messages as ACK'd.
3638 * @param rel Reliability data.
3639 * @param msg DataACK message with a bitfield of future ACK'd messages.
3642 channel_rel_free_sent (struct MeshChannelReliability *rel,
3643 const struct GNUNET_MESH_DataACK *msg)
3645 struct MeshReliableMessage *copy;
3646 struct MeshReliableMessage *next;
3653 bitfield = msg->futures;
3654 mid = ntohl (msg->mid);
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3656 "free_sent_reliable %u %llX\n",
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3659 " rel %p, head %p\n",
3660 rel, rel->head_sent);
3661 for (i = 0, copy = rel->head_sent;
3662 i < 64 && NULL != copy && 0 != bitfield;
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3666 " trying bit %u (mid %u)\n",
3669 if (0 == (bitfield & mask))
3672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3673 /* Bit was set, clear the bit from the bitfield */
3676 /* The i-th bit was set. Do we have that copy? */
3677 /* Skip copies with mid < target */
3678 target = mid + i + 1;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3680 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3683 /* Did we run out of copies? (previously freed, it's ok) */
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3690 /* Did we overshoot the target? (previously freed, it's ok) */
3691 if (GMC_is_pid_bigger (copy->mid, target))
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3697 /* Now copy->mid == target, free it */
3699 rel_message_free (copy);
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3707 * We haven't received an ACK after a certain time: restransmit the message.
3709 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3710 * @param tc TaskContext.
3713 channel_retransmit_message (void *cls,
3714 const struct GNUNET_SCHEDULER_TaskContext *tc)
3716 struct MeshChannelReliability *rel = cls;
3717 struct MeshReliableMessage *copy;
3718 struct MeshPeerQueue *q;
3719 struct MeshChannel *ch;
3720 struct MeshConnection *c;
3721 struct GNUNET_MESH_Data *payload;
3722 struct MeshPeer *hop;
3725 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3726 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3730 copy = rel->head_sent;
3737 /* Search the message to be retransmitted in the outgoing queue.
3738 * Check only the queue for the connection that is going to be used,
3739 * if the message is stuck in some other connection's queue we shouldn't
3741 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3742 * the old connection could be temporary stalled or the queue happened to
3743 * be long at time of insertion.
3744 * - not sending the new one could cause terrible delays the old connection
3747 payload = (struct GNUNET_MESH_Data *) ©[1];
3748 fwd = (rel == ch->root_rel);
3749 c = tunnel_get_connection (ch->t, fwd);
3750 hop = connection_get_hop (c, fwd);
3751 for (q = hop->queue_head; NULL != q; q = q->next)
3753 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3755 struct GNUNET_MESH_Data *queued_data = q->cls;
3757 if (queued_data->mid == payload->mid)
3762 /* Message not found in the queue that we are going to use. */
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3767 send_prebuilt_message_channel (&payload->header, ch, fwd);
3768 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3775 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3776 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3777 &channel_retransmit_message,
3783 * Send ACK on one or more connections due to buffer space to the client.
3785 * Iterates all connections of the tunnel and sends ACKs appropriately.
3787 * @param ch Channel which has some free buffer space.
3788 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3791 channel_send_connections_ack (struct MeshChannel *ch,
3792 unsigned int buffer,
3795 struct MeshTunnel2 *t = ch->t;
3796 struct MeshConnection *c;
3797 struct MeshFlowControl *fc;
3800 uint32_t allow_per_connection;
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3804 "Channel send connection %s ack on %s:%X\n",
3805 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3807 /* Count connections, how many messages are already allowed */
3808 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3810 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3811 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3816 allowed += fc->last_ack_sent - fc->last_pid_recv;
3820 /* Make sure there is no overflow */
3821 if (allowed > buffer)
3827 /* Authorize connections to send more data */
3828 to_allow = buffer - allowed;
3830 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3832 allow_per_connection = to_allow/cs;
3833 to_allow -= allow_per_connection;
3835 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3836 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3840 connection_send_ack (c, allow_per_connection, fwd);
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3844 "Channel send connection %s ack on %s:%X\n",
3845 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3846 GNUNET_break (to_allow == 0);
3851 * Send an ACK on the appropriate connection/channel, depending on
3852 * the direction and the position of the peer.
3854 * @param c Which connection to send the hop-by-hop ACK.
3855 * @param ch Channel, if any.
3856 * @param fwd Is this a fwd ACK? (will go dest->root)
3859 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3861 unsigned int buffer;
3863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3864 "send ack %s on %p %p\n",
3865 fwd ? "FWD" : "BCK", c, ch);
3866 if (NULL == c || connection_is_terminal (c, fwd))
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Channel\n");
3869 buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Connection\n");
3874 GNUNET_assert (NULL != c);
3875 buffer = connection_get_buffer (c, fwd);
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
3882 GNUNET_assert (NULL != ch);
3883 channel_send_connections_ack (ch, buffer, fwd);
3885 else if (connection_is_origin (c, fwd))
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
3891 GNUNET_assert (NULL != ch);
3892 send_local_ack (ch, fwd);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3898 connection_send_ack (c, buffer, fwd);
3904 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3906 * @param ch Channel to mark as ready.
3907 * @param fwd Was the CREATE message sent fwd?
3910 channel_confirm (struct MeshChannel *ch, int fwd)
3912 struct MeshChannelReliability *rel;
3913 struct MeshReliableMessage *copy;
3914 struct MeshReliableMessage *next;
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3917 " channel confirm %s %s:%X\n",
3918 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3919 ch->state = MESH_CHANNEL_READY;
3921 rel = fwd ? ch->root_rel : ch->dest_rel;
3922 for (copy = rel->head_sent; NULL != copy; copy = next)
3924 struct GNUNET_MessageHeader *msg;
3927 msg = (struct GNUNET_MessageHeader *) ©[1];
3928 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3930 rel_message_free (copy);
3934 if (GNUNET_NO == rel->client_ready)
3935 send_local_ack (ch, fwd);
3940 * Save a copy to retransmit in case it gets lost.
3942 * Initializes all needed callbacks and timers.
3944 * @param ch Channel this message goes on.
3945 * @param msg Message to copy.
3946 * @param fwd Is this fwd traffic?
3949 channel_save_copy (struct MeshChannel *ch,
3950 const struct GNUNET_MessageHeader *msg,
3953 struct MeshChannelReliability *rel;
3954 struct MeshReliableMessage *copy;
3959 rel = fwd ? ch->root_rel : ch->dest_rel;
3960 mid = rel->mid_send;
3961 type = ntohs (msg->type);
3962 size = ntohs (msg->size);
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3965 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3967 copy->timestamp = GNUNET_TIME_absolute_get ();
3970 memcpy (©[1], msg, size);
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3973 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3974 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3977 GNUNET_TIME_relative_multiply (rel->expected_delay,
3978 MESH_RETRANSMIT_MARGIN);
3980 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3981 &channel_retransmit_message,
3988 * Send keepalive packets for a connection.
3990 * @param c Connection to keep alive..
3991 * @param fwd Is this a FWD keepalive? (owner -> dest).
3994 connection_keepalive (struct MeshConnection *c, int fwd)
3996 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3997 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
4001 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
4002 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4005 "sending %s keepalive for connection %s[%d]\n",
4006 fwd ? "FWD" : "BCK",
4007 peer2s (c->t->peer),
4010 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
4011 msg->header.size = htons (size);
4012 msg->header.type = htons (type);
4015 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
4020 * Send CONNECTION_{CREATE/ACK} packets for a connection.
4022 * @param c Connection for which to send the message.
4023 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
4026 connection_recreate (struct MeshConnection *c, int fwd)
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
4030 send_connection_create (c);
4032 send_connection_ack (c, GNUNET_NO);
4037 * Generic connection timer management.
4038 * Depending on the role of the peer in the connection will send the
4039 * appropriate message (build or keepalive)
4041 * @param c Conncetion to maintain.
4042 * @param fwd Is FWD?
4045 connection_maintain (struct MeshConnection *c, int fwd)
4047 if (MESH_TUNNEL_SEARCHING == c->t->state)
4049 /* TODO DHT GET with RO_BART */
4054 case MESH_CONNECTION_NEW:
4056 case MESH_CONNECTION_SENT:
4057 connection_recreate (c, fwd);
4059 case MESH_CONNECTION_READY:
4060 connection_keepalive (c, fwd);
4069 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4071 struct MeshConnection *c = cls;
4073 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4074 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4077 connection_maintain (c, GNUNET_YES);
4078 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4079 &connection_fwd_keepalive,
4085 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4087 struct MeshConnection *c = cls;
4089 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4090 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4093 connection_maintain (c, GNUNET_NO);
4094 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4095 &connection_bck_keepalive,
4101 * Send a message to all peers in this connection that the connection
4102 * is no longer valid.
4104 * If some peer should not receive the message, it should be zero'ed out
4105 * before calling this function.
4107 * @param c The connection whose peers to notify.
4110 connection_send_destroy (struct MeshConnection *c)
4112 struct GNUNET_MESH_ConnectionDestroy msg;
4114 msg.header.size = htons (sizeof (msg));
4115 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4118 " sending connection destroy for connection %s[%X]\n",
4119 peer2s (c->t->peer),
4122 if (GNUNET_NO == connection_is_terminal (c, GNUNET_YES))
4123 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
4124 if (GNUNET_NO == connection_is_terminal (c, GNUNET_NO))
4125 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
4126 c->destroy = GNUNET_YES;
4131 * Confirm we got a channel create.
4133 * @param ch The channel to confirm.
4134 * @param fwd Should we send the ACK fwd?
4137 channel_send_ack (struct MeshChannel *ch, int fwd)
4139 struct GNUNET_MESH_ChannelManage msg;
4141 msg.header.size = htons (sizeof (msg));
4142 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 " sending channel %s ack for channel %s:%X\n",
4145 fwd ? "FWD" : "BCK", peer2s (ch->t->peer),
4148 msg.chid = htonl (ch->gid);
4149 send_prebuilt_message_channel (&msg.header, ch, !fwd);
4154 * Send a message to all clients (local and remote) of this channel
4155 * notifying that the channel is no longer valid.
4157 * If some peer or client should not receive the message,
4158 * should be zero'ed out before calling this function.
4160 * @param ch The channel whose clients to notify.
4163 channel_send_destroy (struct MeshChannel *ch)
4165 struct GNUNET_MESH_ChannelManage msg;
4167 msg.header.size = htons (sizeof (msg));
4168 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 " sending channel destroy for channel %s:%X\n",
4171 peer2s (ch->t->peer),
4174 if (channel_is_terminal (ch, GNUNET_NO))
4176 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
4178 msg.chid = htonl (ch->lid_root);
4179 send_local_channel_destroy (ch, GNUNET_NO);
4184 msg.chid = htonl (ch->gid);
4185 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4188 if (channel_is_terminal (ch, GNUNET_YES))
4190 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
4192 msg.chid = htonl (ch->lid_dest);
4193 send_local_channel_destroy (ch, GNUNET_YES);
4198 msg.chid = htonl (ch->gid);
4199 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4207 static struct MeshTunnel2 *
4210 struct MeshTunnel2 *t;
4212 t = GNUNET_new (struct MeshTunnel2);
4214 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4216 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4217 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4219 // GNUNET_break (0);
4220 // tunnel_destroy (t);
4224 // char salt[] = "salt";
4225 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4226 // salt, sizeof (salt),
4227 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4228 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4229 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4231 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4232 // salt, sizeof (salt),
4233 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4234 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4235 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4243 * Add a connection to a tunnel.
4246 * @param c Connection.
4249 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4251 struct MeshConnection *aux;
4253 for (aux = t->connection_head; aux != NULL; aux = aux->next)
4256 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4261 * Initialize a Flow Control structure to the initial state.
4263 * @param fc Flow Control structure to initialize.
4266 fc_init (struct MeshFlowControl *fc)
4269 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4270 fc->last_pid_recv = (uint32_t) -1;
4271 fc->last_ack_sent = (uint32_t) 0;
4272 fc->last_ack_recv = (uint32_t) 0;
4273 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4274 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4276 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4280 static struct MeshConnection *
4281 connection_new (const struct GNUNET_HashCode *cid)
4283 struct MeshConnection *c;
4285 c = GNUNET_new (struct MeshConnection);
4287 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4289 fc_init (&c->fwd_fc);
4290 fc_init (&c->bck_fc);
4299 * Find a connection.
4301 * @param cid Connection ID.
4303 static struct MeshConnection *
4304 connection_get (const struct GNUNET_HashCode *cid)
4306 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4311 connection_destroy (struct MeshConnection *c)
4313 struct MeshPeer *peer;
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4319 peer2s (c->t->peer),
4322 /* Cancel all traffic */
4323 connection_cancel_queues (c, GNUNET_YES);
4324 connection_cancel_queues (c, GNUNET_NO);
4326 /* Cancel maintainance task (keepalive/timeout) */
4327 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4328 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4329 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4330 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4332 /* Deregister from neighbors */
4333 peer = connection_get_next_hop (c);
4334 if (NULL != peer && NULL != peer->connections)
4335 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4336 peer = connection_get_prev_hop (c);
4337 if (NULL != peer && NULL != peer->connections)
4338 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4341 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4342 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4348 tunnel_destroy (struct MeshTunnel2 *t)
4350 struct MeshConnection *c;
4351 struct MeshConnection *next;
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4359 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4360 // GNUNET_break (0);
4362 for (c = t->connection_head; NULL != c; c = next)
4365 connection_destroy (c);
4368 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4375 * Tunnel is empty: destroy it.
4377 * Notifies all connections about the destruction.
4379 * @param t Tunnel to destroy.
4382 tunnel_destroy_empty (struct MeshTunnel2 *t)
4384 struct MeshConnection *c;
4386 for (c = t->connection_head; NULL != c; c = c->next)
4388 if (GNUNET_NO == c->destroy)
4389 connection_send_destroy (c);
4392 if (0 == t->pending_messages)
4395 t->destroy = GNUNET_YES;
4400 * Destroy tunnel if empty (no more channels).
4402 * @param t Tunnel to destroy if empty.
4405 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4407 if (NULL != t->channel_head)
4410 tunnel_destroy_empty (t);
4415 * Destroy a channel and free all resources.
4417 * @param ch Channel to destroy.
4420 channel_destroy (struct MeshChannel *ch)
4422 struct MeshClient *c;
4430 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4441 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4448 channel_rel_free_all (ch->root_rel);
4449 channel_rel_free_all (ch->dest_rel);
4451 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4452 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4458 * Create a new channel.
4460 * @param t Tunnel this channel is in.
4461 * @param owner Client that owns the channel, NULL for foreign channels.
4462 * @param lid_root Local ID for root client.
4464 * @return A new initialized channel. NULL on error.
4466 static struct MeshChannel *
4467 channel_new (struct MeshTunnel2 *t,
4468 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4470 struct MeshChannel *ch;
4472 ch = GNUNET_new (struct MeshChannel);
4474 ch->lid_root = lid_root;
4477 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4479 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4483 while (NULL != channel_get (t, t->next_chid))
4484 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4485 ch->gid = t->next_chid;
4486 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4489 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4493 channel_destroy (ch);
4494 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4504 * Set options in a channel, extracted from a bit flag field
4506 * @param ch Channel to set options to.
4507 * @param options Bit array in host byte order.
4510 channel_set_options (struct MeshChannel *ch, uint32_t options)
4512 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4513 GNUNET_YES : GNUNET_NO;
4514 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4515 GNUNET_YES : GNUNET_NO;
4520 * Iterator for deleting each channel whose client endpoint disconnected.
4522 * @param cls Closure (client that has disconnected).
4523 * @param key The local channel id (used to access the hashmap).
4524 * @param value The value stored at the key (channel to destroy).
4526 * @return GNUNET_OK, keep iterating.
4529 channel_destroy_iterator (void *cls,
4533 struct MeshChannel *ch = value;
4534 struct MeshClient *c = cls;
4535 struct MeshTunnel2 *t;
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4538 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4539 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4551 channel_send_destroy (ch);
4552 channel_destroy (ch);
4553 tunnel_destroy_if_empty (t);
4560 * Remove client's ports from the global hashmap on disconnect.
4562 * @param cls Closure (unused).
4564 * @param value Client structure.
4566 * @return GNUNET_OK, keep iterating.
4569 client_release_ports (void *cls,
4575 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4576 if (GNUNET_YES != res)
4579 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4580 "Port %u by client %p was not registered.\n",
4588 * Timeout function due to lack of keepalive/traffic from the owner.
4589 * Destroys connection if called.
4591 * @param cls Closure (connection to destroy).
4592 * @param tc TaskContext.
4595 connection_fwd_timeout (void *cls,
4596 const struct GNUNET_SCHEDULER_TaskContext *tc)
4598 struct MeshConnection *c = cls;
4600 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4601 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4604 "Connection %s[%X] FWD timed out. Destroying.\n",
4605 peer2s (c->t->peer),
4608 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4611 connection_destroy (c);
4616 * Timeout function due to lack of keepalive/traffic from the destination.
4617 * Destroys connection if called.
4619 * @param cls Closure (connection to destroy).
4620 * @param tc TaskContext
4623 connection_bck_timeout (void *cls,
4624 const struct GNUNET_SCHEDULER_TaskContext *tc)
4626 struct MeshConnection *c = cls;
4628 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4629 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4633 "Connection %s[%X] FWD timed out. Destroying.\n",
4634 peer2s (c->t->peer),
4637 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4640 connection_destroy (c);
4645 * Resets the connection timeout task, some other message has done the
4647 * - For the first peer on the direction this means to send
4648 * a keepalive or a path confirmation message (either create or ACK).
4649 * - For all other peers, this means to destroy the connection,
4650 * due to lack of activity.
4651 * Starts the tiemout if no timeout was running (connection just created).
4653 * @param c Connection whose timeout to reset.
4654 * @param fwd Is this forward?
4656 * TODO use heap to improve efficiency of scheduler.
4659 connection_reset_timeout (struct MeshConnection *c, int fwd)
4661 GNUNET_SCHEDULER_TaskIdentifier *ti;
4662 GNUNET_SCHEDULER_Task f;
4664 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4666 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4667 GNUNET_SCHEDULER_cancel (*ti);
4669 if (connection_is_origin (c, fwd)) /* Endpoint */
4671 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4672 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4676 struct GNUNET_TIME_Relative delay;
4678 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4679 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4680 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4686 * Iterator to notify all connections of a broken link. Mark connections
4687 * to destroy after all traffic has been sent.
4689 * @param cls Closure (peer disconnected).
4690 * @param key Current key code (tid).
4691 * @param value Value in the hash map (connection).
4693 * @return GNUNET_YES if we should continue to iterate,
4697 connection_broken (void *cls,
4698 const struct GNUNET_HashCode *key,
4701 struct MeshPeer *peer = cls;
4702 struct MeshConnection *c = value;
4703 struct GNUNET_MESH_ConnectionBroken msg;
4706 fwd = peer == connection_get_prev_hop (c);
4708 connection_cancel_queues (c, !fwd);
4709 if (connection_is_terminal (c, fwd))
4711 /* Local shutdown, no one to notify about this. */
4712 connection_destroy (c);
4716 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4717 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4719 msg.peer1 = my_full_id;
4720 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4721 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4722 c->destroy = GNUNET_YES;
4727 /******************************************************************************/
4728 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4729 /******************************************************************************/
4732 * Free a transmission that was already queued with all resources
4733 * associated to the request.
4735 * @param queue Queue handler to cancel.
4736 * @param clear_cls Is it necessary to free associated cls?
4739 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4741 struct MeshPeer *peer;
4742 struct MeshFlowControl *fc;
4747 GNUNET_assert (NULL != queue->c);
4748 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4750 if (GNUNET_YES == clear_cls)
4752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4753 GNUNET_MESH_DEBUG_M2S (queue->type));
4754 switch (queue->type)
4756 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4757 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4758 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4759 GNUNET_break (GNUNET_YES == queue->c->destroy);
4761 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4762 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4763 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4764 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4765 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4766 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4767 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4769 GNUNET_free_non_null (queue->cls);
4774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4775 GNUNET_MESH_DEBUG_M2S (queue->type));
4779 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4781 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4782 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
4788 if (NULL != queue->c)
4790 queue->c->pending_messages--;
4791 if (NULL != queue->c->t)
4793 queue->c->t->pending_messages--;
4797 GNUNET_free (queue);
4802 queue_send (void *cls, size_t size, void *buf)
4804 struct MeshPeer *peer = cls;
4805 struct MeshFlowControl *fc;
4806 struct MeshConnection *c;
4807 struct GNUNET_MessageHeader *msg;
4808 struct MeshPeerQueue *queue;
4809 struct MeshTunnel2 *t;
4810 struct MeshChannel *ch;
4811 const struct GNUNET_PeerIdentity *dst_id;
4817 peer->core_transmit = NULL;
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4820 if (NULL == buf || 0 == size)
4822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4827 queue = peer_get_first_message (peer);
4830 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4835 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4838 dst_id = GNUNET_PEER_resolve2 (peer->id);
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4840 /* Check if buffer size is enough for the message */
4841 if (queue->size > size)
4843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4844 peer->core_transmit =
4845 GNUNET_CORE_notify_transmit_ready (core_handle,
4848 GNUNET_TIME_UNIT_FOREVER_REL,
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4857 t = (NULL != c) ? c->t : NULL;
4861 switch (queue->type)
4863 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4864 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4865 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4866 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4867 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4868 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4871 GNUNET_MESH_DEBUG_M2S (queue->type));
4872 data_size = send_core_data_raw (queue->cls, size, buf);
4873 msg = (struct GNUNET_MessageHeader *) buf;
4874 type = ntohs (msg->type);
4876 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4878 if (connection_is_origin (c, GNUNET_YES))
4879 data_size = send_core_connection_create (queue->c, size, buf);
4881 data_size = send_core_data_raw (queue->cls, size, buf);
4883 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4885 if (connection_is_origin (c, GNUNET_NO) ||
4886 connection_is_origin (c, GNUNET_YES))
4887 data_size = send_core_connection_ack (queue->c, size, buf);
4889 data_size = send_core_data_raw (queue->cls, size, buf);
4891 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4892 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4893 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4894 /* This should be encapsulted */
4900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4905 if (0 < drop_percent &&
4906 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4909 "Dropping message of type %s\n",
4910 GNUNET_MESH_DEBUG_M2S (queue->type));
4914 /* Free queue, but cls was freed by send_core_* */
4916 queue_destroy (queue, GNUNET_NO);
4918 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4921 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4922 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4923 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
4925 fc->last_pid_sent = pid;
4926 send_ack (c, ch, fwd);
4932 /* If more data in queue, send next */
4933 queue = peer_get_first_message (peer);
4936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4937 if (NULL == peer->core_transmit) {
4938 peer->core_transmit =
4939 GNUNET_CORE_notify_transmit_ready(core_handle,
4942 GNUNET_TIME_UNIT_FOREVER_REL,
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4951 "* tmt rdy called somewhere else\n");
4953 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4957 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4962 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4964 GNUNET_SCHEDULER_cancel (fc->poll_task);
4965 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4970 c->pending_messages--;
4971 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4974 connection_destroy (c);
4980 t->pending_messages--;
4981 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4983 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4993 queue_add (void *cls, uint16_t type, size_t size,
4994 struct MeshConnection *c,
4995 struct MeshChannel *ch,
4998 struct MeshPeerQueue *queue;
4999 struct MeshFlowControl *fc;
5000 struct MeshPeer *peer;
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5005 "queue add %s %s (%u) on c %p, ch %p\n",
5006 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
5007 GNUNET_assert (NULL != c);
5009 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5010 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
5020 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
5021 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
5026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
5028 if (fc->queue_n >= fc->queue_max && 0 == priority)
5030 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
5033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5034 "queue full: %u/%u\n",
5035 fc->queue_n, fc->queue_max);
5036 return; /* Drop this message */
5039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
5041 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
5043 call_core = GNUNET_NO;
5044 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
5045 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
5047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5048 "no buffer space (%u > %u): starting poll\n",
5049 fc->last_pid_sent + 1, fc->last_ack_recv);
5050 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
5056 call_core = GNUNET_YES;
5057 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5065 if (100 <= priority)
5067 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
5071 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
5072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
5077 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
5079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5080 "calling core tmt rdy towards %s for %u bytes\n",
5081 peer2s (peer), size);
5082 peer->core_transmit =
5083 GNUNET_CORE_notify_transmit_ready (core_handle,
5086 GNUNET_TIME_UNIT_FOREVER_REL,
5087 GNUNET_PEER_resolve2 (peer->id),
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5095 "core tmt rdy towards %s already called\n",
5099 c->pending_messages++;
5101 c->t->pending_messages++;
5105 /******************************************************************************/
5106 /******************** MESH NETWORK HANDLERS **************************/
5107 /******************************************************************************/
5111 * Generic handler for mesh network payload traffic.
5113 * @param t Tunnel on which we got this message.
5114 * @param message Unencryted data message.
5115 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5118 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
5120 struct MeshChannelReliability *rel;
5121 struct MeshChannel *ch;
5122 struct MeshClient *c;
5128 size = ntohs (msg->header.size);
5130 sizeof (struct GNUNET_MESH_Data) +
5131 sizeof (struct GNUNET_MessageHeader))
5136 type = ntohs (msg->header.type);
5137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
5138 GNUNET_MESH_DEBUG_M2S (type));
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
5140 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5143 ch = channel_get (t, ntohl (msg->chid));
5146 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
5147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
5151 /* Initialize FWD/BCK data */
5152 c = fwd ? ch->dest : ch->root;
5153 rel = fwd ? ch->dest_rel : ch->root_rel;
5161 tunnel_change_state (t, MESH_TUNNEL_READY);
5163 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
5165 mid = ntohl (msg->mid);
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
5168 if (GNUNET_NO == ch->reliable ||
5169 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
5170 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
5173 if (GNUNET_YES == ch->reliable)
5175 /* Is this the exact next expected messasge? */
5176 if (mid == rel->mid_recv)
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
5180 channel_send_client_data (ch, msg, fwd);
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
5185 channel_rel_add_buffered_data (msg, rel);
5190 /* Tunnel is unreliable: send to clients directly */
5191 /* FIXME: accept Out Of Order traffic */
5192 rel->mid_recv = mid + 1;
5193 channel_send_client_data (ch, msg, fwd);
5198 GNUNET_break_op (0);
5199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5200 " MID %u not expected (%u - %u), dropping!\n",
5201 mid, rel->mid_recv, rel->mid_recv + 64);
5204 channel_send_data_ack (ch, fwd);
5208 * Handler for mesh network traffic end-to-end ACKs.
5210 * @param t Tunnel on which we got this message.
5211 * @param message Data message.
5212 * @param fwd Is this a fwd ACK? (dest->orig)
5215 handle_data_ack (struct MeshTunnel2 *t,
5216 const struct GNUNET_MESH_DataACK *msg, int fwd)
5218 struct MeshChannelReliability *rel;
5219 struct MeshReliableMessage *copy;
5220 struct MeshReliableMessage *next;
5221 struct MeshChannel *ch;
5226 type = ntohs (msg->header.type);
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5228 GNUNET_MESH_DEBUG_M2S (type));
5229 ch = channel_get (t, ntohl (msg->chid));
5232 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5235 ack = ntohl (msg->mid);
5236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5237 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5239 if (GNUNET_YES == fwd)
5253 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5255 if (GMC_is_pid_bigger (copy->mid, ack))
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5258 channel_rel_free_sent (rel, msg);
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5264 rel_message_free (copy);
5266 /* ACK client if needed */
5267 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5269 /* If some message was free'd, update the retransmission delay*/
5270 if (GNUNET_YES == work)
5272 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5274 GNUNET_SCHEDULER_cancel (rel->retry_task);
5275 if (NULL == rel->head_sent)
5277 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5281 struct GNUNET_TIME_Absolute new_target;
5282 struct GNUNET_TIME_Relative delay;
5284 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5285 MESH_RETRANSMIT_MARGIN);
5286 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5288 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5290 GNUNET_SCHEDULER_add_delayed (delay,
5291 &channel_retransmit_message,
5302 * Core handler for connection creation.
5304 * @param cls Closure (unused).
5305 * @param peer Sender (neighbor).
5306 * @param message Message.
5308 * @return GNUNET_OK to keep the connection open,
5309 * GNUNET_SYSERR to close it (signal serious error)
5312 handle_mesh_connection_create (void *cls,
5313 const struct GNUNET_PeerIdentity *peer,
5314 const struct GNUNET_MessageHeader *message)
5316 struct GNUNET_MESH_ConnectionCreate *msg;
5317 struct GNUNET_PeerIdentity *id;
5318 struct GNUNET_HashCode *cid;
5319 struct MeshPeerPath *path;
5320 struct MeshPeer *dest_peer;
5321 struct MeshPeer *orig_peer;
5322 struct MeshConnection *c;
5323 unsigned int own_pos;
5327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5331 size = ntohs (message->size);
5332 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5334 GNUNET_break_op (0);
5338 /* Calculate hops */
5339 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5340 if (size % sizeof (struct GNUNET_PeerIdentity))
5342 GNUNET_break_op (0);
5345 size /= sizeof (struct GNUNET_PeerIdentity);
5348 GNUNET_break_op (0);
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5353 /* Get parameters */
5354 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5356 id = (struct GNUNET_PeerIdentity *) &msg[1];
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5358 " connection %s (%s).\n",
5359 GNUNET_h2s (cid), GNUNET_i2s (id));
5361 /* Create connection */
5362 c = connection_get (cid);
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5366 c = connection_new (cid);
5368 return GNUNET_OK; connection_reset_timeout (c, GNUNET_YES);
5369 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5373 path = path_new (size);
5375 for (i = 0; i < size; i++)
5377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5378 GNUNET_i2s (&id[i]));
5379 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5380 if (path->peers[i] == myid)
5383 if (own_pos == 0 && path->peers[own_pos] != myid)
5385 /* create path: self not found in path through self */
5386 GNUNET_break_op (0);
5387 path_destroy (path);
5388 connection_destroy (c);
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5392 path_add_to_peers (path, GNUNET_NO);
5393 c->path = path_duplicate (path);
5394 c->own_pos = own_pos;
5401 /* Remember peers */
5402 dest_peer = peer_get (&id[size - 1]);
5403 orig_peer = peer_get (&id[0]);
5405 /* Is it a connection to us? */
5406 if (c->own_pos == size - 1)
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5409 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5411 if (NULL == orig_peer->tunnel)
5413 orig_peer->tunnel = tunnel_new ();
5414 orig_peer->tunnel->peer = orig_peer;
5416 tunnel_add_connection (orig_peer->tunnel, c);
5418 send_connection_ack (c, GNUNET_NO);
5420 /* Keep tunnel alive in direction dest->owner*/
5421 connection_reset_timeout (c, GNUNET_NO);
5425 /* It's for somebody else! Retransmit. */
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5427 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5428 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5429 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5436 * Core handler for path ACKs
5438 * @param cls closure
5439 * @param message message
5440 * @param peer peer identity this notification is about
5442 * @return GNUNET_OK to keep the connection open,
5443 * GNUNET_SYSERR to close it (signal serious error)
5446 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5447 const struct GNUNET_MessageHeader *message)
5449 struct GNUNET_MESH_ConnectionACK *msg;
5450 struct MeshPeerPath *p;
5451 struct MeshConnection *c;
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5455 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5457 GNUNET_h2s (&msg->cid));
5458 c = connection_get (&msg->cid);
5461 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5470 /* Add path to peers? */
5474 path_add_to_peers (p, GNUNET_YES);
5480 connection_change_state (c, MESH_CONNECTION_READY);
5481 connection_reset_timeout (c, GNUNET_NO);
5483 /* Message for us as creator? */
5484 if (connection_is_origin (c, GNUNET_YES))
5486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
5487 if (MESH_TUNNEL_READY != c->t->state)
5488 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5489 send_connection_ack (c, GNUNET_NO);
5490 tunnel_send_queued_data (c->t, GNUNET_YES);
5491 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5493 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5494 c->t->peer->dhtget = NULL;
5496 connection_change_state (c, MESH_CONNECTION_READY);
5500 /* Message for us as destination? */
5501 if (connection_is_terminal (c, GNUNET_YES))
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5504 if (MESH_TUNNEL_READY != c->t->state)
5505 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5506 connection_change_state (c, MESH_CONNECTION_READY);
5507 tunnel_send_queued_data (c->t, GNUNET_NO);
5511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5512 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5518 * Core handler for notifications of broken paths
5520 * @param cls Closure (unused).
5521 * @param peer Peer identity of sending neighbor.
5522 * @param message Message.
5524 * @return GNUNET_OK to keep the connection open,
5525 * GNUNET_SYSERR to close it (signal serious error)
5528 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5529 const struct GNUNET_MessageHeader *message)
5531 struct GNUNET_MESH_ConnectionBroken *msg;
5532 struct MeshConnection *c;
5534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5535 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5536 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5538 GNUNET_i2s (&msg->peer1));
5539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5540 GNUNET_i2s (&msg->peer2));
5541 c = connection_get (&msg->cid);
5544 GNUNET_break_op (0);
5547 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5548 GNUNET_PEER_search (&msg->peer2));
5555 * Core handler for tunnel destruction
5557 * @param cls Closure (unused).
5558 * @param peer Peer identity of sending neighbor.
5559 * @param message Message.
5561 * @return GNUNET_OK to keep the connection open,
5562 * GNUNET_SYSERR to close it (signal serious error)
5565 handle_mesh_connection_destroy (void *cls,
5566 const struct GNUNET_PeerIdentity *peer,
5567 const struct GNUNET_MessageHeader *message)
5569 struct GNUNET_MESH_ConnectionDestroy *msg;
5570 struct MeshConnection *c;
5574 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5576 "Got a CONNECTION DESTROY message from %s\n",
5578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5579 " for connection %s\n",
5580 GNUNET_h2s (&msg->cid));
5581 c = connection_get (&msg->cid);
5584 /* Probably already got the message from another path,
5585 * destroyed the tunnel and retransmitted to children.
5588 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5592 id = GNUNET_PEER_search (peer);
5593 if (id == connection_get_prev_hop (c)->id)
5595 else if (id == connection_get_next_hop (c)->id)
5599 GNUNET_break_op (0);
5602 send_prebuilt_message_connection (message, c, NULL, fwd);
5603 c->destroy = GNUNET_YES;
5610 * Handler for channel create messages.
5612 * @param t Tunnel this channel is to be created in.
5613 * @param msg Message.
5614 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5617 handle_channel_create (struct MeshTunnel2 *t,
5618 struct GNUNET_MESH_ChannelCreate *msg,
5621 MESH_ChannelNumber chid;
5622 struct MeshChannel *ch;
5623 struct MeshClient *c;
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5627 /* Check message size */
5628 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5630 GNUNET_break_op (0);
5634 /* Check if channel exists */
5635 chid = ntohl (msg->chid);
5636 ch = channel_get (t, chid);
5639 /* Probably a retransmission, safe to ignore */
5640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5641 if (NULL != ch->dest)
5643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5644 channel_send_ack (ch, !fwd);
5650 /* Create channel */
5651 ch = channel_new (t, NULL, 0);
5652 channel_set_options (ch, ntohl (msg->opt));
5655 /* Find a destination client */
5656 port = ntohl (msg->port);
5657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5658 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5661 /* TODO send reject */
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5667 channel_add_client (ch, c);
5668 if (GNUNET_YES == ch->reliable)
5669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5671 send_local_channel_create (ch);
5672 channel_send_ack (ch, fwd);
5673 send_local_ack (ch, !fwd);
5678 * Handler for channel ack messages.
5680 * @param t Tunnel this channel is to be created in.
5681 * @param msg Message.
5682 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5685 handle_channel_ack (struct MeshTunnel2 *t,
5686 struct GNUNET_MESH_ChannelManage *msg,
5689 MESH_ChannelNumber chid;
5690 struct MeshChannel *ch;
5692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5693 /* Check message size */
5694 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5696 GNUNET_break_op (0);
5700 /* Check if channel exists */
5701 chid = ntohl (msg->chid);
5702 ch = channel_get (t, chid);
5705 GNUNET_break_op (0);
5706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5710 channel_confirm (ch, !fwd);
5715 * Handler for channel destroy messages.
5717 * @param t Tunnel this channel is to be destroyed of.
5718 * @param msg Message.
5719 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5722 handle_channel_destroy (struct MeshTunnel2 *t,
5723 struct GNUNET_MESH_ChannelManage *msg,
5726 MESH_ChannelNumber chid;
5727 struct MeshChannel *ch;
5729 /* Check message size */
5730 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5732 GNUNET_break_op (0);
5736 /* Check if channel exists */
5737 chid = ntohl (msg->chid);
5738 ch = channel_get (t, chid);
5741 /* Probably a retransmission, safe to ignore */
5745 send_local_channel_destroy (ch, fwd);
5746 channel_destroy (ch);
5751 handle_decrypted (struct MeshTunnel2 *t,
5752 const struct GNUNET_MessageHeader *msgh,
5755 switch (ntohs (msgh->type))
5757 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5758 /* Don't send hop ACK, wait for client to ACK */
5759 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5762 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5763 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5766 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5767 handle_channel_create (t,
5768 (struct GNUNET_MESH_ChannelCreate *) msgh,
5772 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5773 handle_channel_ack (t,
5774 (struct GNUNET_MESH_ChannelManage *) msgh,
5778 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5779 handle_channel_destroy (t,
5780 (struct GNUNET_MESH_ChannelManage *) msgh,
5785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5786 "end-to-end message not known (%u)\n",
5787 ntohs (msgh->type));
5793 * Generic handler for mesh network encrypted traffic.
5795 * @param peer Peer identity this notification is about.
5796 * @param message Encrypted message.
5797 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5799 * @return GNUNET_OK to keep the connection open,
5800 * GNUNET_SYSERR to close it (signal serious error)
5803 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5804 const struct GNUNET_MESH_Encrypted *msg,
5807 struct MeshConnection *c;
5808 struct MeshTunnel2 *t;
5809 struct MeshPeer *neighbor;
5810 struct MeshFlowControl *fc;
5817 size = ntohs (msg->header.size);
5819 sizeof (struct GNUNET_MESH_Encrypted) +
5820 sizeof (struct GNUNET_MessageHeader))
5822 GNUNET_break_op (0);
5825 type = ntohs (msg->header.type);
5826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5828 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5830 /* Check connection */
5831 c = connection_get (&msg->cid);
5834 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5839 fc = fwd ? &c->bck_fc : &c->fwd_fc;
5841 /* Check if origin is as expected */
5842 neighbor = connection_get_hop (c, !fwd);
5843 if (peer_get (peer)->id != neighbor->id)
5845 GNUNET_break_op (0);
5850 pid = ntohl (msg->pid);
5851 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5853 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5855 "WARNING Received PID %u, (prev %u), ACK %u\n",
5856 pid, fc->last_pid_recv, fc->last_ack_sent);
5859 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5861 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5863 " Pid %u not expected (%u+), dropping!\n",
5864 pid, fc->last_pid_recv + 1);
5867 if (MESH_CONNECTION_SENT == c->state)
5868 connection_change_state (c, MESH_CONNECTION_READY);
5869 connection_reset_timeout (c, fwd);
5870 fc->last_pid_recv = pid;
5872 /* Is this message for us? */
5873 if (connection_is_terminal (c, fwd))
5875 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5877 struct GNUNET_MessageHeader *msgh;
5880 /* TODO signature verification */
5881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5882 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5884 fc->last_pid_recv = pid;
5885 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5889 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
5890 handle_decrypted (t, msgh, fwd);
5891 off += ntohs (msgh->size);
5893 send_ack (c, NULL, fwd);
5897 /* Message not for us: forward to next hop */
5898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5899 ttl = ntohl (msg->ttl);
5900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5903 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5904 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5905 send_ack (c, NULL, fwd);
5908 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5910 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5917 * Core handler for mesh network traffic going orig->dest.
5919 * @param cls Closure (unused).
5920 * @param message Message received.
5921 * @param peer Peer who sent the message.
5923 * @return GNUNET_OK to keep the connection open,
5924 * GNUNET_SYSERR to close it (signal serious error)
5927 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5928 const struct GNUNET_MessageHeader *message)
5930 return handle_mesh_encrypted (peer,
5931 (struct GNUNET_MESH_Encrypted *)message,
5936 * Core handler for mesh network traffic going dest->orig.
5938 * @param cls Closure (unused).
5939 * @param message Message received.
5940 * @param peer Peer who sent the message.
5942 * @return GNUNET_OK to keep the connection open,
5943 * GNUNET_SYSERR to close it (signal serious error)
5946 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5947 const struct GNUNET_MessageHeader *message)
5949 return handle_mesh_encrypted (peer,
5950 (struct GNUNET_MESH_Encrypted *)message,
5956 * Core handler for mesh network traffic point-to-point acks.
5958 * @param cls closure
5959 * @param message message
5960 * @param peer peer identity this notification is about
5962 * @return GNUNET_OK to keep the connection open,
5963 * GNUNET_SYSERR to close it (signal serious error)
5966 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5967 const struct GNUNET_MessageHeader *message)
5969 struct GNUNET_MESH_ACK *msg;
5970 struct MeshConnection *c;
5971 struct MeshFlowControl *fc;
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5978 msg = (struct GNUNET_MESH_ACK *) message;
5980 c = connection_get (&msg->cid);
5984 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5989 /* Is this a forward or backward ACK? */
5990 id = GNUNET_PEER_search (peer);
5991 if (connection_get_next_hop (c)->id == id)
5993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5996 else if (connection_get_prev_hop (c)->id == id)
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6003 GNUNET_break_op (0);
6007 ack = ntohl (msg->ack);
6008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6010 /* Cancel polling if the ACK is bigger than before. */
6011 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
6012 GMC_is_pid_bigger (ack, fc->last_ack_recv))
6014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
6015 GNUNET_SCHEDULER_cancel (fc->poll_task);
6016 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
6017 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
6020 fc->last_ack_recv = ack;
6021 connection_unlock_queue (c, fc == &c->fwd_fc);
6028 * Core handler for mesh network traffic point-to-point ack polls.
6030 * @param cls closure
6031 * @param message message
6032 * @param peer peer identity this notification is about
6034 * @return GNUNET_OK to keep the connection open,
6035 * GNUNET_SYSERR to close it (signal serious error)
6038 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6039 const struct GNUNET_MessageHeader *message)
6041 struct GNUNET_MESH_Poll *msg;
6042 struct MeshConnection *c;
6043 struct MeshFlowControl *fc;
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
6049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
6052 msg = (struct GNUNET_MESH_Poll *) message;
6054 c = connection_get (&msg->cid);
6058 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
6060 GNUNET_break_op (0);
6064 /* Is this a forward or backward ACK?
6065 * Note: a poll should never be needed in a loopback case,
6066 * since there is no possiblility of packet loss there, so
6067 * this way of discerining FWD/BCK should not be a problem.
6069 id = GNUNET_PEER_search (peer);
6070 if (connection_get_next_hop (c)->id == id)
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6075 else if (connection_get_prev_hop (c)->id == id)
6077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6082 GNUNET_break_op (0);
6086 pid = ntohl (msg->pid);
6087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
6088 pid, fc->last_pid_recv);
6089 fc->last_pid_recv = pid;
6090 fwd = fc == &c->fwd_fc;
6091 send_ack (c, NULL, fwd);
6098 * Core handler for mesh keepalives.
6100 * @param cls closure
6101 * @param message message
6102 * @param peer peer identity this notification is about
6103 * @return GNUNET_OK to keep the connection open,
6104 * GNUNET_SYSERR to close it (signal serious error)
6106 * TODO: Check who we got this from, to validate route.
6109 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6110 const struct GNUNET_MessageHeader *message)
6112 struct GNUNET_MESH_ConnectionKeepAlive *msg;
6113 struct MeshConnection *c;
6114 struct MeshPeer *neighbor;
6117 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6121 c = connection_get (&msg->cid);
6124 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
6129 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
6130 GNUNET_YES : GNUNET_NO;
6132 /* Check if origin is as expected */
6133 neighbor = connection_get_hop (c, fwd);
6134 if (peer_get (peer)->id != neighbor->id)
6136 GNUNET_break_op (0);
6140 connection_change_state (c, MESH_CONNECTION_READY);
6141 connection_reset_timeout (c, fwd);
6143 if (connection_is_terminal (c, fwd))
6146 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6147 send_prebuilt_message_connection (message, c, NULL, fwd);
6155 * Functions to handle messages from core
6157 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6158 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
6160 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
6161 sizeof (struct GNUNET_MESH_ConnectionACK)},
6162 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
6163 sizeof (struct GNUNET_MESH_ConnectionBroken)},
6164 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
6165 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
6166 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
6167 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6168 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
6169 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6170 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6171 sizeof (struct GNUNET_MESH_ACK)},
6172 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6173 sizeof (struct GNUNET_MESH_Poll)},
6174 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
6175 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
6181 * Function to process paths received for a new peer addition. The recorded
6182 * paths form the initial tunnel, which can be optimized later.
6183 * Called on each result obtained for the DHT search.
6185 * @param cls closure
6186 * @param exp when will this value expire
6187 * @param key key of the result
6188 * @param get_path path of the get request
6189 * @param get_path_length lenght of get_path
6190 * @param put_path path of the put request
6191 * @param put_path_length length of the put_path
6192 * @param type type of the result
6193 * @param size number of bytes in data
6194 * @param data pointer to the result data
6197 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6198 const struct GNUNET_HashCode * key,
6199 const struct GNUNET_PeerIdentity *get_path,
6200 unsigned int get_path_length,
6201 const struct GNUNET_PeerIdentity *put_path,
6202 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6203 size_t size, const void *data)
6205 struct MeshPeer *peer = cls;
6206 struct MeshPeerPath *p;
6207 struct MeshConnection *c;
6208 struct GNUNET_PeerIdentity pi;
6211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6212 GNUNET_PEER_resolve (peer->id, &pi);
6213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6215 p = path_build_from_dht (get_path, get_path_length,
6216 put_path, put_path_length);
6217 path_add_to_peers (p, GNUNET_NO);
6220 /* Count connections */
6221 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6223 /* If we already have 3 (or more (?!)) connections, it's enough */
6227 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6230 peer_connect (peer);
6236 /******************************************************************************/
6237 /********************* MESH LOCAL HANDLES **************************/
6238 /******************************************************************************/
6242 * Handler for client connection.
6244 * @param cls Closure (unused).
6245 * @param client Client handler.
6248 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6250 struct MeshClient *c;
6254 c = GNUNET_malloc (sizeof (struct MeshClient));
6256 c->id = next_client_id++; /* overflow not important: just for debug */
6257 GNUNET_SERVER_client_keep (client);
6258 GNUNET_SERVER_client_set_user_context (client, c);
6259 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6264 * Handler for client disconnection
6266 * @param cls closure
6267 * @param client identification of the client; NULL
6268 * for the last call when the server is destroyed
6271 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6273 struct MeshClient *c;
6275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6282 c = client_get (client);
6285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6287 GNUNET_SERVER_client_drop (c->handle);
6288 c->shutting_down = GNUNET_YES;
6289 if (NULL != c->own_channels)
6291 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6292 &channel_destroy_iterator, c);
6293 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6296 if (NULL != c->incoming_channels)
6298 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6299 &channel_destroy_iterator, c);
6300 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6303 if (NULL != c->ports)
6305 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6306 &client_release_ports, c);
6307 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6309 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6310 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6324 * Handler for new clients
6326 * @param cls closure
6327 * @param client identification of the client
6328 * @param message the actual message, which includes messages the client wants
6331 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6332 const struct GNUNET_MessageHeader *message)
6334 struct GNUNET_MESH_ClientConnect *cc_msg;
6335 struct MeshClient *c;
6340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6342 /* Check data sanity */
6343 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6344 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6345 if (0 != (size % sizeof (uint32_t)))
6348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6351 size /= sizeof (uint32_t);
6353 /* Initialize new client structure */
6354 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6361 p = (uint32_t *) &cc_msg[1];
6362 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6363 for (i = 0; i < size; i++)
6366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6368 /* store in client's hashmap */
6369 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6371 /* store in global hashmap */
6372 /* FIXME only allow one client to have the port open,
6373 * have a backup hashmap with waiting clients */
6374 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6379 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6380 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6381 GNUNET_SERVER_notification_context_add (nc, client);
6382 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6390 * Handler for requests of new tunnels
6392 * @param cls Closure.
6393 * @param client Identification of the client.
6394 * @param message The actual message.
6397 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6398 const struct GNUNET_MessageHeader *message)
6400 struct GNUNET_MESH_ChannelMessage *msg;
6401 struct MeshPeer *peer;
6402 struct MeshTunnel2 *t;
6403 struct MeshChannel *ch;
6404 struct MeshClient *c;
6405 MESH_ChannelNumber chid;
6407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6409 /* Sanity check for client registration */
6410 if (NULL == (c = client_get (client)))
6413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6418 /* Message size sanity check */
6419 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6426 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6428 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6429 chid = ntohl (msg->channel_id);
6431 /* Sanity check for duplicate channel IDs */
6432 if (NULL != channel_get_by_local_id (c, chid))
6435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6439 peer = peer_get (&msg->peer);
6440 if (NULL == peer->tunnel)
6442 peer->tunnel = tunnel_new ();
6443 peer->tunnel->peer = peer;
6447 /* Create channel */
6448 ch = channel_new (t, c, chid);
6452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6455 ch->port = ntohl (msg->port);
6456 channel_set_options (ch, ntohl (msg->opt));
6458 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6459 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6460 ch->root_rel->ch = ch;
6461 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6464 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6465 peer_connect (peer);
6467 /* Send create channel */
6469 struct GNUNET_MESH_ChannelCreate msgcc;
6471 msgcc.header.size = htons (sizeof (msgcc));
6472 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6473 msgcc.chid = htonl (ch->gid);
6474 msgcc.port = msg->port;
6475 msgcc.opt = msg->opt;
6477 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6486 * Handler for requests of deleting tunnels
6488 * @param cls closure
6489 * @param client identification of the client
6490 * @param message the actual message
6493 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6494 const struct GNUNET_MessageHeader *message)
6496 struct GNUNET_MESH_ChannelMessage *msg;
6497 struct MeshClient *c;
6498 struct MeshChannel *ch;
6499 struct MeshTunnel2 *t;
6500 MESH_ChannelNumber chid;
6502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6503 "Got a DESTROY CHANNEL from client!\n");
6505 /* Sanity check for client registration */
6506 if (NULL == (c = client_get (client)))
6509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6514 /* Message sanity check */
6515 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6522 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6524 /* Retrieve tunnel */
6525 chid = ntohl (msg->channel_id);
6526 ch = channel_get_by_local_id (c, chid);
6529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6535 /* Cleanup after the tunnel */
6536 client_delete_channel (c, ch);
6537 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6541 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6548 " channel %X client %p (%p, %p)\n",
6549 chid, c, ch->root, ch->dest);
6554 channel_destroy (ch);
6555 tunnel_destroy_if_empty (t);
6557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6563 * Handler for client traffic
6565 * @param cls closure
6566 * @param client identification of the client
6567 * @param message the actual message
6570 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6571 const struct GNUNET_MessageHeader *message)
6573 struct GNUNET_MESH_LocalData *msg;
6574 struct MeshClient *c;
6575 struct MeshChannel *ch;
6576 struct MeshChannelReliability *rel;
6577 MESH_ChannelNumber chid;
6581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6582 "Got data from a client!\n");
6584 /* Sanity check for client registration */
6585 if (NULL == (c = client_get (client)))
6588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6593 msg = (struct GNUNET_MESH_LocalData *) message;
6595 /* Sanity check for message size */
6596 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6597 if (size < sizeof (struct GNUNET_MessageHeader))
6600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6604 /* Channel exists? */
6605 chid = ntohl (msg->id);
6606 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6607 ch = channel_get_by_local_id (c, chid);
6611 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6615 /* Is the client in the channel? */
6618 ch->root->handle == client)
6622 ch->dest->handle == client) ) )
6625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6629 rel = fwd ? ch->root_rel : ch->dest_rel;
6630 rel->client_ready = GNUNET_NO;
6632 /* Ok, everything is correct, send the message. */
6634 struct GNUNET_MESH_Data *payload;
6635 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6636 unsigned char cbuf[p2p_size];
6638 payload = (struct GNUNET_MESH_Data *) cbuf;
6639 payload->mid = htonl (rel->mid_send);
6641 memcpy (&payload[1], &msg[1], size);
6642 payload->header.size = htons (p2p_size);
6643 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6644 payload->chid = htonl (ch->gid);
6645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6646 send_prebuilt_message_channel (&payload->header, ch, fwd);
6648 if (GNUNET_YES == ch->reliable)
6649 channel_save_copy (ch, &payload->header, fwd);
6651 if (tunnel_get_buffer (ch->t, fwd) > 0)
6652 send_local_ack (ch, fwd);
6653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6654 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6661 * Handler for client's ACKs for payload traffic.
6663 * @param cls Closure (unused).
6664 * @param client Identification of the client.
6665 * @param message The actual message.
6668 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6669 const struct GNUNET_MessageHeader *message)
6671 struct GNUNET_MESH_LocalAck *msg;
6672 struct MeshChannelReliability *rel;
6673 struct MeshChannel *ch;
6674 struct MeshClient *c;
6675 MESH_ChannelNumber chid;
6678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6680 /* Sanity check for client registration */
6681 if (NULL == (c = client_get (client)))
6684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6689 msg = (struct GNUNET_MESH_LocalAck *) message;
6691 /* Channel exists? */
6692 chid = ntohl (msg->channel_id);
6693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6694 ch = channel_get_by_local_id (c, chid);
6695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6705 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6706 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6707 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6708 rel = fwd ? ch->dest_rel : ch->root_rel;
6710 rel->client_ready = GNUNET_YES;
6711 channel_send_client_buffered_data (ch, c, fwd);
6712 send_ack (NULL, ch, fwd);
6714 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6721 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6723 * @param cls Closure (client handle).
6724 * @param key Key (hashed tunnel ID, unused).
6725 * @param value Tunnel info.
6727 * @return GNUNET_YES, to keep iterating.
6730 // monitor_all_tunnels_iterator (void *cls,
6731 // const struct GNUNET_HashCode * key,
6734 // struct GNUNET_SERVER_Client *client = cls;
6735 // struct MeshChannel *ch = value;
6736 // struct GNUNET_MESH_LocalMonitor *msg;
6738 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6739 // msg->channel_id = htonl (ch->gid);
6740 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6741 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6743 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6744 // "* sending info about tunnel %s\n",
6745 // GNUNET_i2s (&msg->owner));
6747 // GNUNET_SERVER_notification_context_unicast (nc, client,
6748 // &msg->header, GNUNET_NO);
6749 // return GNUNET_YES;
6754 * Handler for client's MONITOR request.
6756 * @param cls Closure (unused).
6757 * @param client Identification of the client.
6758 * @param message The actual message.
6761 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6762 const struct GNUNET_MessageHeader *message)
6764 struct MeshClient *c;
6766 /* Sanity check for client registration */
6767 if (NULL == (c = client_get (client)))
6770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6774 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6775 "Received get tunnels request from client %u\n",
6777 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6778 // monitor_all_tunnels_iterator,
6780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6781 "Get tunnels request from client %u completed\n",
6783 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6788 * Handler for client's MONITOR_TUNNEL request.
6790 * @param cls Closure (unused).
6791 * @param client Identification of the client.
6792 * @param message The actual message.
6795 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6796 const struct GNUNET_MessageHeader *message)
6798 const struct GNUNET_MESH_LocalMonitor *msg;
6799 struct GNUNET_MESH_LocalMonitor *resp;
6800 struct MeshClient *c;
6801 struct MeshChannel *ch;
6803 /* Sanity check for client registration */
6804 if (NULL == (c = client_get (client)))
6807 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6811 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6812 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6813 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6816 ntohl (msg->channel_id));
6817 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6821 /* We don't know the tunnel */
6822 struct GNUNET_MESH_LocalMonitor warn;
6825 GNUNET_SERVER_notification_context_unicast (nc, client,
6828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6832 /* Initialize context */
6833 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6835 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6836 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6837 &resp->header, GNUNET_NO);
6840 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6841 "Monitor tunnel request from client %u completed\n",
6843 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6848 * Functions to handle messages from clients
6850 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6851 {&handle_local_new_client, NULL,
6852 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6853 {&handle_local_channel_create, NULL,
6854 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6855 sizeof (struct GNUNET_MESH_ChannelMessage)},
6856 {&handle_local_channel_destroy, NULL,
6857 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6858 sizeof (struct GNUNET_MESH_ChannelMessage)},
6859 {&handle_local_data, NULL,
6860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6861 {&handle_local_ack, NULL,
6862 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6863 sizeof (struct GNUNET_MESH_LocalAck)},
6864 {&handle_local_get_tunnels, NULL,
6865 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6866 sizeof (struct GNUNET_MessageHeader)},
6867 {&handle_local_show_tunnel, NULL,
6868 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6869 sizeof (struct GNUNET_MESH_LocalMonitor)},
6875 * Method called whenever a given peer connects.
6877 * @param cls closure
6878 * @param peer peer identity this notification is about
6881 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6883 struct MeshPeer *pi;
6884 struct MeshPeerPath *path;
6886 DEBUG_CONN ("Peer connected\n");
6887 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6888 pi = peer_get (peer);
6891 DEBUG_CONN (" (self)\n");
6892 path = path_new (1);
6896 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6897 path = path_new (2);
6898 path->peers[1] = pi->id;
6899 GNUNET_PEER_change_rc (pi->id, 1);
6900 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6902 path->peers[0] = myid;
6903 GNUNET_PEER_change_rc (myid, 1);
6904 peer_add_path (pi, path, GNUNET_YES);
6906 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6912 * Method called whenever a peer disconnects.
6914 * @param cls closure
6915 * @param peer peer identity this notification is about
6918 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6920 struct MeshPeer *pi;
6922 DEBUG_CONN ("Peer disconnected\n");
6923 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6930 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6933 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6934 pi->connections = NULL;
6937 DEBUG_CONN (" (self)\n");
6939 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6946 * Install server (service) handlers and start listening to clients.
6951 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6952 GNUNET_SERVER_connect_notify (server_handle,
6953 &handle_local_client_connect, NULL);
6954 GNUNET_SERVER_disconnect_notify (server_handle,
6955 &handle_local_client_disconnect, NULL);
6956 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6958 clients_head = NULL;
6959 clients_tail = NULL;
6961 GNUNET_SERVER_resume (server_handle);
6966 * To be called on core init/fail.
6968 * @param cls Closure (config)
6969 * @param identity the public identity of this peer
6972 core_init (void *cls,
6973 const struct GNUNET_PeerIdentity *identity)
6975 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6979 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6981 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6982 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6984 GNUNET_i2s (identity));
6985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6987 GNUNET_i2s (&my_full_id));
6988 GNUNET_CORE_disconnect (core_handle);
6989 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6990 NULL, /* Closure passed to MESH functions */
6991 &core_init, /* Call core_init once connected */
6992 &core_connect, /* Handle connects */
6993 &core_disconnect, /* remove peers on disconnects */
6994 NULL, /* Don't notify about all incoming messages */
6995 GNUNET_NO, /* For header only in notification */
6996 NULL, /* Don't notify about all outbound messages */
6997 GNUNET_NO, /* For header-only out notification */
6998 core_handlers); /* Register these handlers */
7007 /******************************************************************************/
7008 /************************ MAIN FUNCTIONS ****************************/
7009 /******************************************************************************/
7012 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7014 * @param cls closure
7015 * @param key current key code
7016 * @param value value in the hash map
7017 * @return GNUNET_YES if we should continue to iterate,
7021 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7023 struct MeshPeer *p = value;
7024 struct MeshTunnel2 *t = p->tunnel;
7033 * Task run during shutdown.
7039 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7043 if (core_handle != NULL)
7045 GNUNET_CORE_disconnect (core_handle);
7048 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
7049 if (dht_handle != NULL)
7051 GNUNET_DHT_disconnect (dht_handle);
7056 GNUNET_SERVER_notification_context_destroy (nc);
7059 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7061 GNUNET_SCHEDULER_cancel (announce_id_task);
7062 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7069 * Process mesh requests.
7071 * @param cls closure
7072 * @param server the initialized server
7073 * @param c configuration to use
7076 run (void *cls, struct GNUNET_SERVER_Handle *server,
7077 const struct GNUNET_CONFIGURATION_Handle *c)
7080 struct GNUNET_CRYPTO_EccPrivateKey *pk;
7082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7083 server_handle = server;
7084 GNUNET_SERVER_suspend (server_handle);
7087 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
7090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7092 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7093 "mesh", "peer/privatekey");
7094 GNUNET_SCHEDULER_shutdown ();
7099 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
7100 &refresh_connection_time))
7102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7104 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7105 "mesh", "refresh path time");
7106 GNUNET_SCHEDULER_shutdown ();
7111 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7116 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7117 "mesh", "id announce time");
7118 GNUNET_SCHEDULER_shutdown ();
7123 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7128 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7129 "mesh", "connect timeout");
7130 GNUNET_SCHEDULER_shutdown ();
7135 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7140 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7141 "mesh", "max msgs queue");
7142 GNUNET_SCHEDULER_shutdown ();
7147 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
7150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7152 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7153 "mesh", "max tunnels");
7154 GNUNET_SCHEDULER_shutdown ();
7159 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7164 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7165 "mesh", "default ttl", 64);
7170 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7173 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7174 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7175 "mesh", "max peers", 1000);
7180 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
7187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7188 "Mesh is running with drop mode enabled. "
7189 "This is NOT a good idea! "
7190 "Remove the DROP_PERCENT option from your configuration.\n");
7194 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7195 &dht_replication_level))
7197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7199 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7200 "mesh", "dht replication level", 3);
7201 dht_replication_level = 3;
7204 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7205 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7206 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7208 dht_handle = GNUNET_DHT_connect (c, 64);
7209 if (NULL == dht_handle)
7213 stats = GNUNET_STATISTICS_create ("mesh", c);
7215 /* Scheduled the task to clean up when shutdown is called */
7216 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7218 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7219 GNUNET_free (keyfile);
7220 GNUNET_assert (NULL != pk);
7221 my_private_key = pk;
7222 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7223 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7224 &my_full_id.hashPubKey);
7225 myid = GNUNET_PEER_intern (&my_full_id);
7226 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7227 "Mesh for peer [%s] starting\n",
7228 GNUNET_i2s(&my_full_id));
7230 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7231 NULL, /* Closure passed to MESH functions */
7232 &core_init, /* Call core_init once connected */
7233 &core_connect, /* Handle connects */
7234 &core_disconnect, /* remove peers on disconnects */
7235 NULL, /* Don't notify about all incoming messages */
7236 GNUNET_NO, /* For header only in notification */
7237 NULL, /* Don't notify about all outbound messages */
7238 GNUNET_NO, /* For header-only out notification */
7239 core_handlers); /* Register these handlers */
7240 if (NULL == core_handle)
7243 GNUNET_SCHEDULER_shutdown ();
7246 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7252 * The main function for the mesh service.
7254 * @param argc number of arguments from the command line
7255 * @param argv command line arguments
7256 * @return 0 ok, 1 on error
7259 main (int argc, char *const *argv)
7264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7265 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7267 ret = (GNUNET_OK == r) ? 0 : 1;
7268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");