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, \n",
1594 GNUNET_h2s (&c->id));
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:
1904 * Sends an already built message on a tunnel, choosing the best connection.
1906 * @param message Message to send. Function modifies it.
1907 * @param t Tunnel on which this message is transmitted.
1908 * @param ch Channel on which this message is transmitted.
1909 * @param fwd Is this a fwd message?
1912 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1913 struct MeshTunnel2 *t,
1914 struct MeshChannel *ch,
1917 struct MeshConnection *c;
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1922 c = tunnel_get_connection (t, fwd);
1925 GNUNET_break (GNUNET_YES == t->destroy);
1928 type = ntohs (msg->header.type);
1931 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1932 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1933 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1934 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1936 msg->ttl = htonl (default_ttl);
1939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1940 GNUNET_MESH_DEBUG_M2S (type));
1945 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1950 * Sends an already built message on a channel, properly registering
1951 * all used resources and encrypting the message with the tunnel's key.
1953 * @param message Message to send. Function makes a copy of it.
1954 * @param ch Channel on which this message is transmitted.
1955 * @param fwd Is this a fwd message?
1958 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1959 struct MeshChannel *ch,
1962 struct GNUNET_MESH_Encrypted *msg;
1963 size_t size = ntohs (message->size);
1964 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1969 peer2s (ch->t->peer), ch->gid);
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1971 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1973 if (channel_is_terminal (ch, fwd))
1975 handle_decrypted (ch->t, message, fwd);
1979 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1980 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1982 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1983 msg->header.type = htons (type);
1984 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1985 msg->iv = GNUNET_htonll (iv);
1986 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1987 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1992 * Sends a CREATE CONNECTION message for a path to a peer.
1993 * Changes the connection and tunnel states if necessary.
1995 * @param connection Connection to create.
1998 send_connection_create (struct MeshConnection *connection)
2000 struct MeshTunnel2 *t;
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
2005 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
2006 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2007 (connection->path->length *
2008 sizeof (struct GNUNET_PeerIdentity)),
2013 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
2014 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2015 if (MESH_CONNECTION_NEW == connection->state)
2016 connection_change_state (connection, MESH_CONNECTION_SENT);
2021 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
2024 * @param connection Connection to confirm.
2025 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
2028 send_connection_ack (struct MeshConnection *connection, int fwd)
2030 struct MeshTunnel2 *t;
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
2035 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
2036 sizeof (struct GNUNET_MESH_ConnectionACK),
2040 if (MESH_TUNNEL_NEW == t->state)
2041 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2046 * Core callback to write a pre-constructed data packet to core buffer
2048 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2049 * @param size Number of bytes available in buf.
2050 * @param buf Where the to write the message.
2052 * @return number of bytes written to buf
2055 send_core_data_raw (void *cls, size_t size, void *buf)
2057 struct GNUNET_MessageHeader *msg = cls;
2060 GNUNET_assert (NULL != msg);
2061 total_size = ntohs (msg->size);
2063 if (total_size > size)
2068 memcpy (buf, msg, total_size);
2075 * Function to send a create connection message to a peer.
2077 * @param c Connection to create.
2078 * @param size number of bytes available in buf
2079 * @param buf where the callee should write the message
2080 * @return number of bytes written to buf
2083 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
2085 struct GNUNET_MESH_ConnectionCreate *msg;
2086 struct GNUNET_PeerIdentity *peer_ptr;
2087 struct MeshPeerPath *p = c->path;
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
2093 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2094 p->length * sizeof (struct GNUNET_PeerIdentity);
2096 if (size < size_needed || NULL == buf)
2101 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
2102 msg->header.size = htons (size_needed);
2103 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
2106 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2107 for (i = 0; i < p->length; i++)
2109 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2113 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
2119 * Creates a path ack message in buf and frees all unused resources.
2121 * @param c Connection to send an ACK on.
2122 * @param size number of bytes available in buf
2123 * @param buf where the callee should write the message
2125 * @return number of bytes written to buf
2128 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
2130 struct GNUNET_MESH_ConnectionACK *msg = buf;
2131 struct MeshTunnel2 *t = c->t;
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
2134 GNUNET_assert (NULL != t);
2135 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
2140 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
2141 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
2144 /* TODO add signature */
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2147 return sizeof (struct GNUNET_MESH_ConnectionACK);
2152 * Destroy the peer_info and free any allocated resources linked to it
2154 * @param peer The peer_info to destroy.
2156 * @return GNUNET_OK on success
2159 peer_destroy (struct MeshPeer *peer)
2161 struct GNUNET_PeerIdentity id;
2162 struct MeshPeerPath *p;
2163 struct MeshPeerPath *nextp;
2165 GNUNET_PEER_resolve (peer->id, &id);
2166 GNUNET_PEER_change_rc (peer->id, -1);
2169 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2173 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2175 if (NULL != peer->dhtget)
2177 GNUNET_DHT_get_stop (peer->dhtget);
2179 p = peer->path_head;
2183 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2187 tunnel_destroy_empty (peer->tunnel);
2194 * Returns if peer is used (has a tunnel, is neighbor).
2196 * @peer Peer to check.
2198 * @return GNUNET_YES if peer is in use.
2201 peer_is_used (struct MeshPeer *peer)
2203 struct MeshPeerPath *p;
2205 if (NULL != peer->tunnel)
2208 for (p = peer->path_head; NULL != p; p = p->next)
2217 * Iterator over all the peers to get the oldest timestamp.
2219 * @param cls Closure (unsued).
2220 * @param key ID of the peer.
2221 * @param value Peer_Info of the peer.
2224 peer_get_oldest (void *cls,
2225 const struct GNUNET_HashCode *key,
2228 struct MeshPeer *p = value;
2229 struct GNUNET_TIME_Absolute *abs = cls;
2231 /* Don't count active peers */
2232 if (GNUNET_YES == peer_is_used (p))
2235 if (abs->abs_value_us < p->last_contact.abs_value_us)
2236 abs->abs_value_us = p->last_contact.abs_value_us;
2243 * Iterator over all the peers to remove the oldest entry.
2245 * @param cls Closure (unsued).
2246 * @param key ID of the peer.
2247 * @param value Peer_Info of the peer.
2250 peer_timeout (void *cls,
2251 const struct GNUNET_HashCode *key,
2254 struct MeshPeer *p = value;
2255 struct GNUNET_TIME_Absolute *abs = cls;
2257 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2258 GNUNET_NO == peer_is_used (p))
2268 * Delete oldest unused peer.
2271 peer_delete_oldest (void)
2273 struct GNUNET_TIME_Absolute abs;
2275 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2277 GNUNET_CONTAINER_multihashmap_iterate (peers,
2280 GNUNET_CONTAINER_multihashmap_iterate (peers,
2287 * Retrieve the MeshPeer stucture associated with the peer, create one
2288 * and insert it in the appropriate structures if the peer is not known yet.
2290 * @param peer Full identity of the peer.
2292 * @return Existing or newly created peer info.
2294 static struct MeshPeer *
2295 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2297 struct MeshPeer *peer;
2299 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2302 peer = GNUNET_new (struct MeshPeer);
2303 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2305 peer_delete_oldest ();
2307 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2309 peer->id = GNUNET_PEER_intern (peer_id);
2311 peer->last_contact = GNUNET_TIME_absolute_get();
2318 * Retrieve the MeshPeer stucture associated with the peer, create one
2319 * and insert it in the appropriate structures if the peer is not known yet.
2321 * @param peer Short identity of the peer.
2323 * @return Existing or newly created peer info.
2325 static struct MeshPeer *
2326 peer_get_short (const GNUNET_PEER_Id peer)
2328 return peer_get (GNUNET_PEER_resolve2 (peer));
2333 * Get a cost of a path for a peer considering existing tunnel connections.
2335 * @param peer Peer towards which the path is considered.
2336 * @param path Candidate path.
2338 * @return Cost of the path (path length + number of overlapping nodes)
2341 peer_get_path_cost (const struct MeshPeer *peer,
2342 const struct MeshPeerPath *path)
2344 struct MeshConnection *c;
2345 unsigned int overlap;
2353 GNUNET_assert (NULL != peer->tunnel);
2355 for (i = 0; i < path->length; i++)
2357 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2359 for (j = 0; j < c->path->length; j++)
2361 if (path->peers[i] == c->path->peers[j])
2369 return (path->length + overlap) * (path->score * -1);
2374 * Choose the best path towards a peer considering the tunnel properties.
2376 * @param peer The destination peer.
2378 * @return Best current known path towards the peer, if any.
2380 static struct MeshPeerPath *
2381 peer_get_best_path (const struct MeshPeer *peer)
2383 struct MeshPeerPath *best_p;
2384 struct MeshPeerPath *p;
2385 struct MeshConnection *c;
2386 unsigned int best_cost;
2389 best_cost = UINT_MAX;
2391 for (p = peer->path_head; NULL != p; p = p->next)
2393 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2397 continue; /* If path is in use in a connection, skip it. */
2399 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2409 queue_is_sendable (struct MeshPeerQueue *q)
2411 struct MeshFlowControl *fc;
2413 /* Is PID-independent? */
2416 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2417 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2421 /* Is PID allowed? */
2422 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2423 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2431 * Get first sendable message.
2433 * @param peer The destination peer.
2435 * @return Best current known path towards the peer, if any.
2437 static struct MeshPeerQueue *
2438 peer_get_first_message (const struct MeshPeer *peer)
2440 struct MeshPeerQueue *q;
2442 for (q = peer->queue_head; NULL != q; q = q->next)
2444 if (queue_is_sendable (q))
2453 * Try to establish a new connection to this peer in the given tunnel.
2454 * If the peer doesn't have any path to it yet, try to get one.
2455 * If the peer already has some path, send a CREATE CONNECTION towards it.
2457 * @param peer PeerInfo of the peer.
2460 peer_connect (struct MeshPeer *peer)
2462 struct MeshTunnel2 *t;
2463 struct MeshPeerPath *p;
2464 struct MeshConnection *c;
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "peer_connect towards %s\n",
2470 if (NULL != peer->path_head)
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2473 p = peer_get_best_path (peer);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2477 c = tunnel_use_path (t, p);
2478 send_connection_create (c);
2481 else if (NULL == peer->dhtget)
2483 const struct GNUNET_PeerIdentity *id;
2485 id = GNUNET_PEER_resolve2 (peer->id);
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 " Starting DHT GET for peer %s\n", peer2s (peer));
2488 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2489 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2490 &id->hashPubKey, /* key to search */
2491 dht_replication_level, /* replication level */
2492 GNUNET_DHT_RO_RECORD_ROUTE |
2493 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2495 0, /* xquery bits */
2496 &dht_get_id_handler, peer);
2497 if (MESH_TUNNEL_NEW == t->state)
2498 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "There is no path but the DHT GET is already started.\n");
2509 * Get the first transmittable message for a connection.
2511 * @param c Connection.
2512 * @param fwd Is this FWD?
2514 * @return First transmittable message.
2516 static struct MeshPeerQueue *
2517 connection_get_first_message (struct MeshConnection *c, int fwd)
2519 struct MeshPeerQueue *q;
2522 p = connection_get_hop (c, fwd);
2524 for (q = p->queue_head; NULL != q; q = q->next)
2528 if (queue_is_sendable (q))
2537 * @brief Re-initiate traffic on this connection if necessary.
2539 * Check if there is traffic queued towards this peer
2540 * and the core transmit handle is NULL (traffic was stalled).
2541 * If so, call core tmt rdy.
2543 * @param c Connection on which initiate traffic.
2544 * @param fwd Is this about fwd traffic?
2547 connection_unlock_queue (struct MeshConnection *c, int fwd)
2549 struct MeshPeer *peer;
2550 struct MeshPeerQueue *q;
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "connection_unlock_queue %s on %s\n",
2555 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2557 if (connection_is_origin (c, fwd))
2559 struct MeshTunnel2 *t = c->t;
2560 struct MeshChannel *ch;
2561 struct MeshChannelReliability *rel;
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2564 /* FIXME randomize channel selection, not always first channel */
2565 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2567 rel = fwd ? ch->root_rel : ch->dest_rel;
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2570 ch->gid, rel->client_ready ? "ready " : "not ready");
2571 if (GNUNET_NO == rel->client_ready)
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2574 send_local_ack (ch, fwd);
2575 return; /* FIXME authorize all channels? */
2581 peer = connection_get_hop (c, fwd);
2583 if (NULL != peer->core_transmit)
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2586 return; /* Already unlocked */
2589 q = connection_get_first_message (c, fwd);
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2593 return; /* Nothing to transmit */
2597 peer->core_transmit =
2598 GNUNET_CORE_notify_transmit_ready (core_handle,
2601 GNUNET_TIME_UNIT_FOREVER_REL,
2602 GNUNET_PEER_resolve2 (peer->id),
2610 * Cancel all transmissions that belong to a certain connection.
2612 * @param c Connection which to cancel.
2613 * @param fwd Cancel fwd traffic?
2616 connection_cancel_queues (struct MeshConnection *c, int fwd)
2618 struct MeshPeerQueue *q;
2619 struct MeshPeerQueue *next;
2620 struct MeshFlowControl *fc;
2621 struct MeshPeer *peer;
2628 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2629 peer = connection_get_hop (c, fwd);
2631 for (q = peer->queue_head; NULL != q; q = next)
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "connection_cancel_queue %s\n",
2638 GNUNET_MESH_DEBUG_M2S (q->type));
2639 queue_destroy (q, GNUNET_YES);
2642 if (NULL == peer->queue_head)
2644 if (NULL != peer->core_transmit)
2646 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2647 peer->core_transmit = NULL;
2649 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2651 GNUNET_SCHEDULER_cancel (fc->poll_task);
2652 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2659 * Remove all paths that rely on a direct connection between p1 and p2
2660 * from the peer itself and notify all tunnels about it.
2662 * @param peer PeerInfo of affected peer.
2663 * @param p1 GNUNET_PEER_Id of one peer.
2664 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2667 * TODO: optimize (see below)
2670 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2673 struct MeshPeerPath *p;
2674 struct MeshPeerPath *next;
2675 struct MeshPeer *peer_d;
2677 unsigned int destroyed;
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2682 for (p = peer->path_head; NULL != p; p = next)
2685 for (i = 0; i < (p->length - 1); i++)
2687 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2688 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2690 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2701 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2703 peer_d = peer_get_short (d); // FIXME
2704 next = peer_get_best_path (peer_d);
2705 tunnel_use_path (peer->tunnel, next);
2706 peer_connect (peer_d);
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2713 * Add the path to the peer and update the path used to reach it in case this
2716 * @param peer_info Destination peer to add the path to.
2717 * @param path New path to add. Last peer must be the peer in arg 1.
2718 * Path will be either used of freed if already known.
2719 * @param trusted Do we trust that this path is real?
2722 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2725 struct MeshPeerPath *aux;
2729 if ((NULL == peer_info) || (NULL == path))
2732 path_destroy (path);
2735 if (path->peers[path->length - 1] != peer_info->id)
2738 path_destroy (path);
2741 if (2 >= path->length && GNUNET_NO == trusted)
2743 /* Only allow CORE to tell us about direct paths */
2744 path_destroy (path);
2747 for (l = 1; l < path->length; l++)
2749 if (path->peers[l] == myid)
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2752 for (l2 = 0; l2 < path->length - l; l2++)
2754 path->peers[l2] = path->peers[l + l2];
2759 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2764 path->length, peer2s (peer_info));
2766 l = path_get_length (path);
2769 path_destroy (path);
2773 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2774 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2776 l2 = path_get_length (aux);
2779 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2780 peer_info->path_tail, aux, path);
2785 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2787 path_destroy (path);
2792 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2799 * Add the path to the origin peer and update the path used to reach it in case
2800 * this is the shortest.
2801 * The path is given in peer_info -> destination, therefore we turn the path
2802 * upside down first.
2804 * @param peer_info Peer to add the path to, being the origin of the path.
2805 * @param path New path to add after being inversed.
2806 * Path will be either used or freed.
2807 * @param trusted Do we trust that this path is real?
2810 peer_add_path_to_origin (struct MeshPeer *peer_info,
2811 struct MeshPeerPath *path, int trusted)
2816 peer_add_path (peer_info, path, trusted);
2822 * Function called if a connection has been stalled for a while,
2823 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2825 * @param cls Closure (poll ctx).
2826 * @param tc TaskContext.
2829 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2831 struct MeshFlowControl *fc = cls;
2832 struct GNUNET_MESH_Poll msg;
2833 struct MeshConnection *c;
2835 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2836 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2844 peer2s (c->t->peer), c->id);
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
2846 fc == &c->fwd_fc ? "FWD" : "BCK");
2848 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2849 msg.header.size = htons (sizeof (msg));
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2851 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2852 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2853 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2854 &connection_poll, fc);
2859 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2860 * to obtain a local peer -> destination path and interning the peer ids.
2862 * @return Newly allocated and created path
2864 static struct MeshPeerPath *
2865 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2866 unsigned int get_path_length,
2867 const struct GNUNET_PeerIdentity *put_path,
2868 unsigned int put_path_length)
2870 struct MeshPeerPath *p;
2876 GNUNET_PEER_change_rc (myid, 1);
2877 i = get_path_length;
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2879 for (i--; i >= 0; i--)
2881 id = GNUNET_PEER_intern (&get_path[i]);
2882 if (p->length > 0 && id == p->peers[p->length - 1])
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2885 GNUNET_PEER_change_rc (id, -1);
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2890 GNUNET_i2s (&get_path[i]));
2892 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2893 p->peers[p->length - 1] = id;
2896 i = put_path_length;
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2898 for (i--; i >= 0; i--)
2900 id = GNUNET_PEER_intern (&put_path[i]);
2903 /* PUT path went through us, so discard the path up until now and start
2904 * from here to get a much shorter (and loop-free) path.
2909 if (p->length > 0 && id == p->peers[p->length - 1])
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2912 GNUNET_PEER_change_rc (id, -1);
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2917 GNUNET_i2s (&put_path[i]));
2919 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2920 p->peers[p->length - 1] = id;
2924 if (get_path_length > 0)
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2926 GNUNET_i2s (&get_path[0]));
2927 if (put_path_length > 0)
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2929 GNUNET_i2s (&put_path[0]));
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2932 for (i = 0; i < p->length; i++)
2934 struct GNUNET_PeerIdentity peer_id;
2936 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2938 GNUNET_i2s (&peer_id));
2946 * Adds a path to the peer_infos of all the peers in the path
2948 * @param p Path to process.
2949 * @param confirmed Whether we know if the path works or not.
2952 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2956 /* TODO: invert and add */
2957 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2958 for (i++; i < p->length; i++)
2960 struct MeshPeer *aux;
2961 struct MeshPeerPath *copy;
2963 aux = peer_get_short (p->peers[i]);
2964 copy = path_duplicate (p);
2965 copy->length = i + 1;
2966 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2972 * Search for a channel among the channels for a client
2974 * @param c the client whose channels to search in
2975 * @param chid the local id of the channel
2977 * @return channel handler, NULL if doesn't exist
2979 static struct MeshChannel *
2980 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2983 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2985 GNUNET_break_op (0);
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2989 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2990 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2991 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2997 channel_debug (struct MeshChannel *ch)
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X\n",
3005 peer2s (ch->t->peer), ch->gid);
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
3007 ch->root, ch->root_rel);
3008 if (NULL != ch->root)
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
3011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
3012 ch->root_rel->client_ready ? "YES" : "NO");
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
3016 ch->dest, ch->dest_rel);
3017 if (NULL != ch->dest)
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
3020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
3021 ch->dest_rel->client_ready ? "YES" : "NO");
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
3027 fc_debug (struct MeshFlowControl *fc)
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
3030 fc->last_pid_recv, fc->last_ack_sent);
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
3032 fc->last_pid_sent, fc->last_ack_recv);
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
3034 fc->queue_n, fc->queue_max);
3038 connection_debug (struct MeshConnection *c)
3042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
3046 peer2s (c->t->peer), GNUNET_h2s (&c->id));
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
3048 c->state, c->pending_messages);
3049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
3050 fc_debug (&c->fwd_fc);
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
3052 fc_debug (&c->bck_fc);
3057 static struct MeshChannel *
3058 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
3060 struct MeshChannel *ch;
3065 for (ch = t->channel_head; NULL != ch; ch = ch->next)
3067 if (ch->gid == chid)
3076 * Change the tunnel state.
3078 * @param t Tunnel whose state to change.
3079 * @param state New state.
3082 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3087 "Tunnel %s state was %s\n",
3089 GNUNET_MESH_DEBUG_TS2S (t->state));
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 "Tunnel %s state is now %s\n",
3093 GNUNET_MESH_DEBUG_TS2S (state));
3099 * Cache a message to be sent once tunnel is online.
3101 * @param t Tunnel to hold the message.
3102 * @param ch Channel the message is about.
3103 * @param msg Message itself (copy will be made).
3104 * @param fwd Is this fwd?
3107 tunnel_queue_data (struct MeshTunnel2 *t,
3108 struct MeshChannel *ch,
3109 struct GNUNET_MessageHeader *msg,
3112 struct MeshTunnelQueue *tq;
3113 uint16_t size = ntohs (msg->size);
3115 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
3118 memcpy (&tq[1], msg, size);
3119 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
3124 * Send all cached messages that we can, tunnel is online.
3126 * @param t Tunnel that holds the messages.
3127 * @param fwd Is this fwd?
3130 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
3132 struct MeshTunnelQueue *tq;
3133 struct MeshTunnelQueue *next;
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3137 "tunnel_send_queued_data on tunnel %s\n",
3139 room = tunnel_get_buffer (t, fwd);
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
3141 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
3145 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
3146 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
3155 connection_change_state (struct MeshConnection* c,
3156 enum MeshConnectionState state)
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 "Connection %s state was %s\n",
3160 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3162 "Connection %s state is now %s\n",
3163 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
3169 * Add a client to a channel, initializing all needed data structures.
3171 * @param ch Channel to which add the client.
3172 * @param c Client which to add to the channel.
3175 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3177 struct MeshTunnel2 *t = ch->t;
3179 if (NULL != ch->dest)
3185 /* Assign local id as destination */
3186 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3187 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3188 ch->lid_dest = t->next_local_chid++;
3189 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3191 /* Store in client's hashmap */
3193 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3195 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3201 GNUNET_break (NULL == ch->dest_rel);
3202 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3203 ch->dest_rel->ch = ch;
3204 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3210 static struct MeshConnection *
3211 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3213 struct MeshConnection *c;
3214 struct GNUNET_HashCode cid;
3215 struct MeshPeer *peer;
3216 unsigned int own_pos;
3218 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3220 c = connection_new (&cid);
3222 for (own_pos = 0; own_pos < p->length; own_pos++)
3224 if (p->peers[own_pos] == myid)
3227 if (own_pos > p->length - 1)
3230 connection_destroy (c);
3233 c->own_pos = own_pos;
3238 c->fwd_maintenance_task =
3239 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3240 &connection_fwd_keepalive, c);
3243 peer = connection_get_next_hop (c);
3244 if (NULL == peer->connections)
3246 connection_destroy (c);
3249 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3251 peer = connection_get_prev_hop (c);
3252 if (NULL == peer->connections)
3254 connection_destroy (c);
3257 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3264 * Notifies a tunnel that a connection has broken that affects at least
3265 * some of its peers. Sends a notification towards the root of the tree.
3266 * In case the peer is the owner of the tree, notifies the client that owns
3267 * the tunnel and tries to reconnect.
3269 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3271 * @param t Tunnel affected.
3272 * @param p1 Peer that got disconnected from p2.
3273 * @param p2 Peer that got disconnected from p1.
3275 * @return Short ID of the peer disconnected (either p1 or p2).
3276 * 0 if the tunnel remained unaffected.
3278 static GNUNET_PEER_Id
3279 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3280 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3282 // if (myid != p1 && myid != p2) FIXME
3287 // if (tree_get_predecessor (t->tree) != 0)
3289 // /* We are the peer still connected, notify owner of the disconnection. */
3290 // struct GNUNET_MESH_PathBroken msg;
3291 // struct GNUNET_PeerIdentity neighbor;
3293 // msg.header.size = htons (sizeof (msg));
3294 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3295 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3296 // msg.tid = htonl (t->id.tid);
3297 // msg.peer1 = my_full_id;
3298 // GNUNET_PEER_resolve (pid, &msg.peer2);
3299 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3300 // send_prebuilt_message (&msg.header, &neighbor, t);
3307 * Send an end-to-end ACK message for the most recent in-sequence payload.
3309 * If channel is not reliable, do nothing.
3311 * @param ch Channel this is about.
3312 * @param fwd Is for FWD traffic? (ACK dest->owner)
3315 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3317 struct GNUNET_MESH_DataACK msg;
3318 struct MeshChannelReliability *rel;
3319 struct MeshReliableMessage *copy;
3324 if (GNUNET_NO == ch->reliable)
3328 rel = fwd ? ch->dest_rel : ch->root_rel;
3329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3330 "send_data_ack for %u\n",
3333 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3334 msg.header.type = htons (type);
3335 msg.header.size = htons (sizeof (msg));
3336 msg.chid = htonl (ch->gid);
3337 msg.mid = htonl (rel->mid_recv - 1);
3339 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3341 if (copy->type != type)
3343 delta = copy->mid - rel->mid_recv;
3346 mask = 0x1LL << delta;
3347 msg.futures |= mask;
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3349 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3350 copy->mid, delta, mask, msg.futures);
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3354 send_prebuilt_message_channel (&msg.header, ch, fwd);
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3360 * Send an ACK informing the predecessor about the available buffer space.
3362 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3363 * the ACK itself goes "back" (dest->root).
3365 * @param c Connection on which to send the ACK.
3366 * @param buffer How much space free to advertise?
3367 * @param fwd Is this FWD ACK? (Going dest->owner)
3370 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3372 struct MeshFlowControl *next_fc;
3373 struct MeshFlowControl *prev_fc;
3374 struct GNUNET_MESH_ACK msg;
3378 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3379 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3382 "connection send %s ack on %s\n",
3383 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3385 /* Check if we need to transmit the ACK */
3386 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3390 " last pid recv: %u, last ack sent: %u\n",
3391 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3395 /* Ok, ACK might be necessary, what PID to ACK? */
3396 delta = next_fc->queue_max - next_fc->queue_n;
3397 ack = prev_fc->last_pid_recv + delta;
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3400 " last pid %u, last ack %u, qmax %u, q %u\n",
3401 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3402 next_fc->queue_max, next_fc->queue_n);
3403 if (ack == prev_fc->last_ack_sent)
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3409 prev_fc->last_ack_sent = ack;
3411 /* Build ACK message and send on connection */
3412 msg.header.size = htons (sizeof (msg));
3413 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3414 msg.ack = htonl (ack);
3417 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3422 * Modify the mesh message TID from global to local and send to client.
3424 * @param ch Channel on which to send the message.
3425 * @param msg Message to modify and send.
3426 * @param c Client to send to.
3427 * @param tid Tunnel ID to use (c can be both owner and client).
3430 channel_send_client_to_tid (struct MeshChannel *ch,
3431 const struct GNUNET_MESH_Data *msg,
3432 struct MeshClient *c, MESH_ChannelNumber id)
3434 struct GNUNET_MESH_LocalData *copy;
3435 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3436 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3438 if (size < sizeof (struct GNUNET_MessageHeader))
3440 GNUNET_break_op (0);
3448 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3449 memcpy (©[1], &msg[1], size);
3450 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3451 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3452 copy->id = htonl (id);
3453 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3454 ©->header, GNUNET_NO);
3459 * We have received a message out of order, or the client is not ready.
3460 * Buffer it until we receive an ACK from the client or the missing
3461 * message from the channel.
3463 * @param msg Message to buffer (MUST be of type MESH_DATA).
3464 * @param rel Reliability data to the corresponding direction.
3467 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3468 struct MeshChannelReliability *rel)
3470 struct MeshReliableMessage *copy;
3471 struct MeshReliableMessage *prev;
3475 size = ntohs (msg->header.size);
3476 mid = ntohl (msg->mid);
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3480 copy = GNUNET_malloc (sizeof (*copy) + size);
3483 memcpy (©[1], msg, size);
3487 // FIXME do something better than O(n), although n < 64...
3488 // FIXME start from the end (most messages are the latest ones)
3489 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3492 if (GMC_is_pid_bigger (prev->mid, mid))
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3495 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3501 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3507 channel_send_client_data (struct MeshChannel *ch,
3508 const struct GNUNET_MESH_Data *msg,
3513 if (ch->dest_rel->client_ready)
3514 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3516 channel_rel_add_buffered_data (msg, ch->dest_rel);
3520 if (ch->root_rel->client_ready)
3521 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3523 channel_rel_add_buffered_data (msg, ch->root_rel);
3529 * Send a buffered message to the client, for in order delivery or
3530 * as result of client ACK.
3532 * @param ch Channel on which to empty the message buffer.
3533 * @param c Client to send to.
3534 * @param rel Reliability structure to corresponding peer.
3535 * If rel == bck_rel, this is FWD data.
3538 channel_send_client_buffered_data (struct MeshChannel *ch,
3539 struct MeshClient *c,
3542 struct MeshReliableMessage *copy;
3543 struct MeshChannelReliability *rel;
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3546 rel = fwd ? ch->dest_rel : ch->root_rel;
3547 if (GNUNET_NO == rel->client_ready)
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3553 copy = rel->head_recv;
3554 /* We never buffer channel management messages */
3557 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3559 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3562 " have %u! now expecting %u\n",
3563 copy->mid, rel->mid_recv + 1);
3564 channel_send_client_data (ch, msg, fwd);
3567 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 " reliable && don't have %u, next is %u\n",
3579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3584 * Destroy a reliable message after it has been acknowledged, either by
3585 * direct mid ACK or bitfield. Updates the appropriate data structures and
3586 * timers and frees all memory.
3588 * @param copy Message that is no longer needed: remote peer got it.
3591 rel_message_free (struct MeshReliableMessage *copy)
3593 struct MeshChannelReliability *rel;
3594 struct GNUNET_TIME_Relative time;
3597 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3598 rel->expected_delay.rel_value_us *= 7;
3599 rel->expected_delay.rel_value_us += time.rel_value_us;
3600 rel->expected_delay.rel_value_us /= 8;
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3605 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3607 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3609 rel->retry_timer = rel->expected_delay;
3610 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3616 * Destroy all reliable messages queued for a channel,
3617 * during a channel destruction.
3618 * Frees the reliability structure itself.
3620 * @param rel Reliability data for a channel.
3623 channel_rel_free_all (struct MeshChannelReliability *rel)
3625 struct MeshReliableMessage *copy;
3626 struct MeshReliableMessage *next;
3631 for (copy = rel->head_recv; NULL != copy; copy = next)
3634 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3637 for (copy = rel->head_sent; NULL != copy; copy = next)
3640 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3643 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3644 GNUNET_SCHEDULER_cancel (rel->retry_task);
3650 * Mark future messages as ACK'd.
3652 * @param rel Reliability data.
3653 * @param msg DataACK message with a bitfield of future ACK'd messages.
3656 channel_rel_free_sent (struct MeshChannelReliability *rel,
3657 const struct GNUNET_MESH_DataACK *msg)
3659 struct MeshReliableMessage *copy;
3660 struct MeshReliableMessage *next;
3667 bitfield = msg->futures;
3668 mid = ntohl (msg->mid);
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3670 "free_sent_reliable %u %llX\n",
3672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3673 " rel %p, head %p\n",
3674 rel, rel->head_sent);
3675 for (i = 0, copy = rel->head_sent;
3676 i < 64 && NULL != copy && 0 != bitfield;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3680 " trying bit %u (mid %u)\n",
3683 if (0 == (bitfield & mask))
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3687 /* Bit was set, clear the bit from the bitfield */
3690 /* The i-th bit was set. Do we have that copy? */
3691 /* Skip copies with mid < target */
3692 target = mid + i + 1;
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3694 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3697 /* Did we run out of copies? (previously freed, it's ok) */
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3704 /* Did we overshoot the target? (previously freed, it's ok) */
3705 if (GMC_is_pid_bigger (copy->mid, target))
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3711 /* Now copy->mid == target, free it */
3713 rel_message_free (copy);
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3721 * We haven't received an ACK after a certain time: restransmit the message.
3723 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3724 * @param tc TaskContext.
3727 channel_retransmit_message (void *cls,
3728 const struct GNUNET_SCHEDULER_TaskContext *tc)
3730 struct MeshChannelReliability *rel = cls;
3731 struct MeshReliableMessage *copy;
3732 struct MeshPeerQueue *q;
3733 struct MeshChannel *ch;
3734 struct MeshConnection *c;
3735 struct GNUNET_MESH_Data *payload;
3736 struct MeshPeer *hop;
3739 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3740 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3744 copy = rel->head_sent;
3751 /* Search the message to be retransmitted in the outgoing queue.
3752 * Check only the queue for the connection that is going to be used,
3753 * if the message is stuck in some other connection's queue we shouldn't
3755 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3756 * the old connection could be temporary stalled or the queue happened to
3757 * be long at time of insertion.
3758 * - not sending the new one could cause terrible delays the old connection
3761 payload = (struct GNUNET_MESH_Data *) ©[1];
3762 fwd = (rel == ch->root_rel);
3763 c = tunnel_get_connection (ch->t, fwd);
3764 hop = connection_get_hop (c, fwd);
3765 for (q = hop->queue_head; NULL != q; q = q->next)
3767 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3769 struct GNUNET_MESH_Data *queued_data = q->cls;
3771 if (queued_data->mid == payload->mid)
3776 /* Message not found in the queue that we are going to use. */
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3781 send_prebuilt_message_channel (&payload->header, ch, fwd);
3782 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3789 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3790 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3791 &channel_retransmit_message,
3797 * Send ACK on one or more connections due to buffer space to the client.
3799 * Iterates all connections of the tunnel and sends ACKs appropriately.
3801 * @param ch Channel which has some free buffer space.
3802 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3805 channel_send_connections_ack (struct MeshChannel *ch,
3806 unsigned int buffer,
3809 struct MeshTunnel2 *t = ch->t;
3810 struct MeshConnection *c;
3811 struct MeshFlowControl *fc;
3814 uint32_t allow_per_connection;
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 "Channel send connection %s ack on %s:%X\n",
3819 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3821 /* Count connections, how many messages are already allowed */
3822 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3824 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3825 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3830 allowed += fc->last_ack_sent - fc->last_pid_recv;
3834 /* Make sure there is no overflow */
3835 if (allowed > buffer)
3841 /* Authorize connections to send more data */
3842 to_allow = buffer - allowed;
3844 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3846 allow_per_connection = to_allow/cs;
3847 to_allow -= allow_per_connection;
3849 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3850 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3854 connection_send_ack (c, allow_per_connection, fwd);
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3858 "Channel send connection %s ack on %s:%X\n",
3859 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3860 GNUNET_break (to_allow == 0);
3865 * Send an ACK on the appropriate connection/channel, depending on
3866 * the direction and the position of the peer.
3868 * @param c Which connection to send the hop-by-hop ACK.
3869 * @param ch Channel, if any.
3870 * @param fwd Is this a fwd ACK? (will go dest->root)
3873 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3875 unsigned int buffer;
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3878 "send ack %s on %p %p\n",
3879 fwd ? "FWD" : "BCK", c, ch);
3880 if (NULL == c || connection_is_terminal (c, fwd))
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Channel\n");
3883 buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Connection\n");
3888 GNUNET_assert (NULL != c);
3889 buffer = connection_get_buffer (c, fwd);
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
3896 GNUNET_assert (NULL != ch);
3897 channel_send_connections_ack (ch, buffer, fwd);
3899 else if (connection_is_origin (c, fwd))
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
3905 GNUNET_assert (NULL != ch);
3906 send_local_ack (ch, fwd);
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3912 connection_send_ack (c, buffer, fwd);
3918 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3920 * @param ch Channel to mark as ready.
3921 * @param fwd Was the CREATE message sent fwd?
3924 channel_confirm (struct MeshChannel *ch, int fwd)
3926 struct MeshChannelReliability *rel;
3927 struct MeshReliableMessage *copy;
3928 struct MeshReliableMessage *next;
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931 " channel confirm %s %s:%X\n",
3932 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3933 ch->state = MESH_CHANNEL_READY;
3935 rel = fwd ? ch->root_rel : ch->dest_rel;
3936 for (copy = rel->head_sent; NULL != copy; copy = next)
3938 struct GNUNET_MessageHeader *msg;
3941 msg = (struct GNUNET_MessageHeader *) ©[1];
3942 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3944 rel_message_free (copy);
3948 if (GNUNET_NO == rel->client_ready)
3949 send_local_ack (ch, fwd);
3954 * Save a copy to retransmit in case it gets lost.
3956 * Initializes all needed callbacks and timers.
3958 * @param ch Channel this message goes on.
3959 * @param msg Message to copy.
3960 * @param fwd Is this fwd traffic?
3963 channel_save_copy (struct MeshChannel *ch,
3964 const struct GNUNET_MessageHeader *msg,
3967 struct MeshChannelReliability *rel;
3968 struct MeshReliableMessage *copy;
3973 rel = fwd ? ch->root_rel : ch->dest_rel;
3974 mid = rel->mid_send;
3975 type = ntohs (msg->type);
3976 size = ntohs (msg->size);
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3979 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3981 copy->timestamp = GNUNET_TIME_absolute_get ();
3984 memcpy (©[1], msg, size);
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3987 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3988 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3991 GNUNET_TIME_relative_multiply (rel->expected_delay,
3992 MESH_RETRANSMIT_MARGIN);
3994 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3995 &channel_retransmit_message,
4002 * Send keepalive packets for a connection.
4004 * @param c Connection to keep alive..
4005 * @param fwd Is this a FWD keepalive? (owner -> dest).
4008 connection_keepalive (struct MeshConnection *c, int fwd)
4010 struct GNUNET_MESH_ConnectionKeepAlive *msg;
4011 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
4015 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
4016 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4019 "sending %s keepalive for connection %s[%d]\n",
4020 fwd ? "FWD" : "BCK",
4021 peer2s (c->t->peer),
4024 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
4025 msg->header.size = htons (size);
4026 msg->header.type = htons (type);
4029 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
4034 * Send CONNECTION_{CREATE/ACK} packets for a connection.
4036 * @param c Connection for which to send the message.
4037 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
4040 connection_recreate (struct MeshConnection *c, int fwd)
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
4044 send_connection_create (c);
4046 send_connection_ack (c, GNUNET_NO);
4051 * Generic connection timer management.
4052 * Depending on the role of the peer in the connection will send the
4053 * appropriate message (build or keepalive)
4055 * @param c Conncetion to maintain.
4056 * @param fwd Is FWD?
4059 connection_maintain (struct MeshConnection *c, int fwd)
4061 if (MESH_TUNNEL_SEARCHING == c->t->state)
4063 /* TODO DHT GET with RO_BART */
4068 case MESH_CONNECTION_NEW:
4070 case MESH_CONNECTION_SENT:
4071 connection_recreate (c, fwd);
4073 case MESH_CONNECTION_READY:
4074 connection_keepalive (c, fwd);
4083 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4085 struct MeshConnection *c = cls;
4087 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4088 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4091 connection_maintain (c, GNUNET_YES);
4092 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4093 &connection_fwd_keepalive,
4099 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4101 struct MeshConnection *c = cls;
4103 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4104 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4107 connection_maintain (c, GNUNET_NO);
4108 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4109 &connection_bck_keepalive,
4115 * Send a message to all peers in this connection that the connection
4116 * is no longer valid.
4118 * If some peer should not receive the message, it should be zero'ed out
4119 * before calling this function.
4121 * @param c The connection whose peers to notify.
4124 connection_send_destroy (struct MeshConnection *c)
4126 struct GNUNET_MESH_ConnectionDestroy msg;
4128 msg.header.size = htons (sizeof (msg));
4129 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4132 " sending connection destroy for connection %s[%X]\n",
4133 peer2s (c->t->peer),
4136 if (GNUNET_NO == connection_is_terminal (c, GNUNET_YES))
4137 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
4138 if (GNUNET_NO == connection_is_terminal (c, GNUNET_NO))
4139 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
4140 c->destroy = GNUNET_YES;
4145 * Confirm we got a channel create.
4147 * @param ch The channel to confirm.
4148 * @param fwd Should we send the ACK fwd?
4151 channel_send_ack (struct MeshChannel *ch, int fwd)
4153 struct GNUNET_MESH_ChannelManage msg;
4155 msg.header.size = htons (sizeof (msg));
4156 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4158 " sending channel ack for channel %s:%X\n",
4159 peer2s (ch->t->peer),
4162 msg.chid = htonl (ch->gid);
4163 send_prebuilt_message_channel (&msg.header, ch, fwd);
4168 * Send a message to all clients (local and remote) of this channel
4169 * notifying that the channel is no longer valid.
4171 * If some peer or client should not receive the message,
4172 * should be zero'ed out before calling this function.
4174 * @param ch The channel whose clients to notify.
4177 channel_send_destroy (struct MeshChannel *ch)
4179 struct GNUNET_MESH_ChannelManage msg;
4181 msg.header.size = htons (sizeof (msg));
4182 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4184 " sending channel destroy for channel %s:%X\n",
4185 peer2s (ch->t->peer),
4188 if (channel_is_terminal (ch, GNUNET_NO))
4190 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
4192 msg.chid = htonl (ch->lid_root);
4193 send_local_channel_destroy (ch, GNUNET_NO);
4198 msg.chid = htonl (ch->gid);
4199 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4202 if (channel_is_terminal (ch, GNUNET_YES))
4204 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
4206 msg.chid = htonl (ch->lid_dest);
4207 send_local_channel_destroy (ch, GNUNET_YES);
4212 msg.chid = htonl (ch->gid);
4213 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4221 static struct MeshTunnel2 *
4224 struct MeshTunnel2 *t;
4226 t = GNUNET_new (struct MeshTunnel2);
4228 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4230 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4231 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4233 // GNUNET_break (0);
4234 // tunnel_destroy (t);
4238 // char salt[] = "salt";
4239 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4240 // salt, sizeof (salt),
4241 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4242 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4243 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4245 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4246 // salt, sizeof (salt),
4247 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4248 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4249 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4257 * Add a connection to a tunnel.
4260 * @param c Connection.
4263 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4265 struct MeshConnection *aux;
4267 for (aux = t->connection_head; aux != NULL; aux = aux->next)
4270 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4275 * Initialize a Flow Control structure to the initial state.
4277 * @param fc Flow Control structure to initialize.
4280 fc_init (struct MeshFlowControl *fc)
4283 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4284 fc->last_pid_recv = (uint32_t) -1;
4285 fc->last_ack_sent = (uint32_t) 0;
4286 fc->last_ack_recv = (uint32_t) 0;
4287 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4288 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4290 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4294 static struct MeshConnection *
4295 connection_new (const struct GNUNET_HashCode *cid)
4297 struct MeshConnection *c;
4299 c = GNUNET_new (struct MeshConnection);
4301 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4303 fc_init (&c->fwd_fc);
4304 fc_init (&c->bck_fc);
4313 * Find a connection.
4315 * @param cid Connection ID.
4317 static struct MeshConnection *
4318 connection_get (const struct GNUNET_HashCode *cid)
4320 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4325 connection_destroy (struct MeshConnection *c)
4327 struct MeshPeer *peer;
4332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4333 peer2s (c->t->peer),
4336 /* Cancel all traffic */
4337 connection_cancel_queues (c, GNUNET_YES);
4338 connection_cancel_queues (c, GNUNET_NO);
4340 /* Cancel maintainance task (keepalive/timeout) */
4341 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4342 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4343 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4344 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4346 /* Deregister from neighbors */
4347 peer = connection_get_next_hop (c);
4349 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4350 peer = connection_get_prev_hop (c);
4352 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4355 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4356 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4362 tunnel_destroy (struct MeshTunnel2 *t)
4364 struct MeshConnection *c;
4365 struct MeshConnection *next;
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4373 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4374 // GNUNET_break (0);
4376 for (c = t->connection_head; NULL != c; c = next)
4379 connection_destroy (c);
4382 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4389 * Tunnel is empty: destroy it.
4391 * Notifies all connections about the destruction.
4393 * @param t Tunnel to destroy.
4396 tunnel_destroy_empty (struct MeshTunnel2 *t)
4398 struct MeshConnection *c;
4400 for (c = t->connection_head; NULL != c; c = c->next)
4402 if (GNUNET_NO == c->destroy)
4403 connection_send_destroy (c);
4406 if (0 == t->pending_messages)
4409 t->destroy = GNUNET_YES;
4414 * Destroy tunnel if empty (no more channels).
4416 * @param t Tunnel to destroy if empty.
4419 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4421 if (NULL != t->channel_head)
4424 tunnel_destroy_empty (t);
4429 * Destroy a channel and free all resources.
4431 * @param ch Channel to destroy.
4434 channel_destroy (struct MeshChannel *ch)
4436 struct MeshClient *c;
4444 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4455 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4462 channel_rel_free_all (ch->root_rel);
4463 channel_rel_free_all (ch->dest_rel);
4465 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4466 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4472 * Create a new channel.
4474 * @param t Tunnel this channel is in.
4475 * @param owner Client that owns the channel, NULL for foreign channels.
4476 * @param lid_root Local ID for root client.
4478 * @return A new initialized channel. NULL on error.
4480 static struct MeshChannel *
4481 channel_new (struct MeshTunnel2 *t,
4482 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4484 struct MeshChannel *ch;
4486 ch = GNUNET_new (struct MeshChannel);
4488 ch->lid_root = lid_root;
4491 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4493 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4497 while (NULL != channel_get (t, t->next_chid))
4498 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4499 ch->gid = t->next_chid;
4500 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4503 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4504 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4507 channel_destroy (ch);
4508 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4518 * Set options in a channel, extracted from a bit flag field
4520 * @param ch Channel to set options to.
4521 * @param options Bit array in host byte order.
4524 channel_set_options (struct MeshChannel *ch, uint32_t options)
4526 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4527 GNUNET_YES : GNUNET_NO;
4528 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4529 GNUNET_YES : GNUNET_NO;
4534 * Iterator for deleting each channel whose client endpoint disconnected.
4536 * @param cls Closure (client that has disconnected).
4537 * @param key The local channel id (used to access the hashmap).
4538 * @param value The value stored at the key (channel to destroy).
4540 * @return GNUNET_OK, keep iterating.
4543 channel_destroy_iterator (void *cls,
4547 struct MeshChannel *ch = value;
4548 struct MeshClient *c = cls;
4549 struct MeshTunnel2 *t;
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4552 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4553 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4565 channel_send_destroy (ch);
4566 channel_destroy (ch);
4567 tunnel_destroy_if_empty (t);
4574 * Remove client's ports from the global hashmap on disconnect.
4576 * @param cls Closure (unused).
4578 * @param value Client structure.
4580 * @return GNUNET_OK, keep iterating.
4583 client_release_ports (void *cls,
4589 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4590 if (GNUNET_YES != res)
4593 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4594 "Port %u by client %p was not registered.\n",
4602 * Timeout function due to lack of keepalive/traffic from the owner.
4603 * Destroys connection if called.
4605 * @param cls Closure (connection to destroy).
4606 * @param tc TaskContext.
4609 connection_fwd_timeout (void *cls,
4610 const struct GNUNET_SCHEDULER_TaskContext *tc)
4612 struct MeshConnection *c = cls;
4614 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4615 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4618 "Connection %s[%X] FWD timed out. Destroying.\n",
4619 peer2s (c->t->peer),
4622 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4625 connection_destroy (c);
4630 * Timeout function due to lack of keepalive/traffic from the destination.
4631 * Destroys connection if called.
4633 * @param cls Closure (connection to destroy).
4634 * @param tc TaskContext
4637 connection_bck_timeout (void *cls,
4638 const struct GNUNET_SCHEDULER_TaskContext *tc)
4640 struct MeshConnection *c = cls;
4642 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4643 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4647 "Connection %s[%X] FWD timed out. Destroying.\n",
4648 peer2s (c->t->peer),
4651 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4654 connection_destroy (c);
4659 * Resets the connection timeout task, some other message has done the
4661 * - For the first peer on the direction this means to send
4662 * a keepalive or a path confirmation message (either create or ACK).
4663 * - For all other peers, this means to destroy the connection,
4664 * due to lack of activity.
4665 * Starts the tiemout if no timeout was running (connection just created).
4667 * @param c Connection whose timeout to reset.
4668 * @param fwd Is this forward?
4670 * TODO use heap to improve efficiency of scheduler.
4673 connection_reset_timeout (struct MeshConnection *c, int fwd)
4675 GNUNET_SCHEDULER_TaskIdentifier *ti;
4676 GNUNET_SCHEDULER_Task f;
4678 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4680 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4681 GNUNET_SCHEDULER_cancel (*ti);
4683 if (connection_is_origin (c, fwd)) /* Endpoint */
4685 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4686 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4690 struct GNUNET_TIME_Relative delay;
4692 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4693 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4694 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4700 * Iterator to notify all connections of a broken link. Mark connections
4701 * to destroy after all traffic has been sent.
4703 * @param cls Closure (peer disconnected).
4704 * @param key Current key code (tid).
4705 * @param value Value in the hash map (connection).
4707 * @return GNUNET_YES if we should continue to iterate,
4711 connection_broken (void *cls,
4712 const struct GNUNET_HashCode *key,
4715 struct MeshPeer *peer = cls;
4716 struct MeshConnection *c = value;
4717 struct GNUNET_MESH_ConnectionBroken msg;
4720 fwd = peer == connection_get_prev_hop (c);
4722 if (connection_is_terminal (c, fwd))
4724 /* Local shutdown: no point in iterating anymore */
4725 connection_destroy (c);
4728 connection_cancel_queues (c, !fwd);
4730 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4731 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4733 msg.peer1 = my_full_id;
4734 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4735 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4736 c->destroy = GNUNET_YES;
4741 /******************************************************************************/
4742 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4743 /******************************************************************************/
4746 * Free a transmission that was already queued with all resources
4747 * associated to the request.
4749 * @param queue Queue handler to cancel.
4750 * @param clear_cls Is it necessary to free associated cls?
4753 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4755 struct MeshPeer *peer;
4756 struct MeshFlowControl *fc;
4761 GNUNET_assert (NULL != queue->c);
4762 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4764 if (GNUNET_YES == clear_cls)
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4767 GNUNET_MESH_DEBUG_M2S (queue->type));
4768 switch (queue->type)
4770 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4771 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4772 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4773 GNUNET_break (GNUNET_YES == queue->c->destroy);
4775 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4776 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4777 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4778 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4779 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4780 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4781 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4783 GNUNET_free_non_null (queue->cls);
4788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4789 GNUNET_MESH_DEBUG_M2S (queue->type));
4793 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4795 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4796 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4802 if (NULL != queue->c)
4804 queue->c->pending_messages--;
4805 if (NULL != queue->c->t)
4807 queue->c->t->pending_messages--;
4811 GNUNET_free (queue);
4816 queue_send (void *cls, size_t size, void *buf)
4818 struct MeshPeer *peer = cls;
4819 struct MeshFlowControl *fc;
4820 struct MeshConnection *c;
4821 struct GNUNET_MessageHeader *msg;
4822 struct MeshPeerQueue *queue;
4823 struct MeshTunnel2 *t;
4824 struct MeshChannel *ch;
4825 const struct GNUNET_PeerIdentity *dst_id;
4831 peer->core_transmit = NULL;
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4834 if (NULL == buf || 0 == size)
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4841 queue = peer_get_first_message (peer);
4844 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4849 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4852 dst_id = GNUNET_PEER_resolve2 (peer->id);
4853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4854 /* Check if buffer size is enough for the message */
4855 if (queue->size > size)
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4858 peer->core_transmit =
4859 GNUNET_CORE_notify_transmit_ready (core_handle,
4862 GNUNET_TIME_UNIT_FOREVER_REL,
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4871 t = (NULL != c) ? c->t : NULL;
4875 switch (queue->type)
4877 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4878 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4879 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4880 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4881 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4882 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4885 GNUNET_MESH_DEBUG_M2S (queue->type));
4886 data_size = send_core_data_raw (queue->cls, size, buf);
4887 msg = (struct GNUNET_MessageHeader *) buf;
4888 type = ntohs (msg->type);
4890 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4892 if (connection_is_origin (c, GNUNET_YES))
4893 data_size = send_core_connection_create (queue->c, size, buf);
4895 data_size = send_core_data_raw (queue->cls, size, buf);
4897 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4899 if (connection_is_origin (c, GNUNET_NO))
4900 data_size = send_core_connection_ack (queue->c, size, buf);
4902 data_size = send_core_data_raw (queue->cls, size, buf);
4904 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4905 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4906 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4907 /* This should be encapsulted */
4912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4917 if (0 < drop_percent &&
4918 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4921 "Dropping message of type %s\n",
4922 GNUNET_MESH_DEBUG_M2S (queue->type));
4926 /* Free queue, but cls was freed by send_core_* */
4928 queue_destroy (queue, GNUNET_NO);
4930 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4933 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4934 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4935 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4936 fc->last_pid_sent = pid;
4937 send_ack (c, ch, fwd);
4943 /* If more data in queue, send next */
4944 queue = peer_get_first_message (peer);
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4948 if (NULL == peer->core_transmit) {
4949 peer->core_transmit =
4950 GNUNET_CORE_notify_transmit_ready(core_handle,
4953 GNUNET_TIME_UNIT_FOREVER_REL,
4961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4962 "* tmt rdy called somewhere else\n");
4964 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4968 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4973 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4975 GNUNET_SCHEDULER_cancel (fc->poll_task);
4976 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4981 c->pending_messages--;
4982 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4985 connection_destroy (c);
4991 t->pending_messages--;
4992 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4994 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
5004 queue_add (void *cls, uint16_t type, size_t size,
5005 struct MeshConnection *c,
5006 struct MeshChannel *ch,
5009 struct MeshPeerQueue *queue;
5010 struct MeshFlowControl *fc;
5011 struct MeshPeer *peer;
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 "queue add %s %s (%u) on c %p, ch %p\n",
5016 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
5017 GNUNET_assert (NULL != c);
5019 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5020 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
5030 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
5031 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
5037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
5038 if (fc->queue_n >= fc->queue_max && 0 == priority)
5040 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5044 "queue full: %u/%u\n",
5045 fc->queue_n, fc->queue_max);
5046 return; /* Drop this message */
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
5050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
5051 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
5052 GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
5053 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5056 "no buffer space (%u > %u): starting poll\n",
5057 fc->last_pid_sent + 1, fc->last_ack_recv);
5058 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
5062 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5070 if (100 <= priority)
5072 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
5076 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
5082 if (NULL == peer->core_transmit)
5084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5085 "calling core tmt rdy towards %s for %u bytes\n",
5086 peer2s (peer), size);
5087 peer->core_transmit =
5088 GNUNET_CORE_notify_transmit_ready (core_handle,
5091 GNUNET_TIME_UNIT_FOREVER_REL,
5092 GNUNET_PEER_resolve2 (peer->id),
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5100 "core tmt rdy towards %s already called\n",
5104 c->pending_messages++;
5106 c->t->pending_messages++;
5110 /******************************************************************************/
5111 /******************** MESH NETWORK HANDLERS **************************/
5112 /******************************************************************************/
5116 * Generic handler for mesh network payload traffic.
5118 * @param t Tunnel on which we got this message.
5119 * @param message Unencryted data message.
5120 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5123 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
5125 struct MeshChannelReliability *rel;
5126 struct MeshChannel *ch;
5127 struct MeshClient *c;
5133 size = ntohs (msg->header.size);
5135 sizeof (struct GNUNET_MESH_Data) +
5136 sizeof (struct GNUNET_MessageHeader))
5141 type = ntohs (msg->header.type);
5142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
5143 GNUNET_MESH_DEBUG_M2S (type));
5144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
5145 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5148 ch = channel_get (t, ntohl (msg->chid));
5151 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
5156 /* Initialize FWD/BCK data */
5157 c = fwd ? ch->dest : ch->root;
5158 rel = fwd ? ch->dest_rel : ch->root_rel;
5166 tunnel_change_state (t, MESH_TUNNEL_READY);
5168 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
5170 mid = ntohl (msg->mid);
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
5173 if (GNUNET_NO == ch->reliable ||
5174 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
5175 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
5177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
5178 if (GNUNET_YES == ch->reliable)
5180 /* Is this the exact next expected messasge? */
5181 if (mid == rel->mid_recv)
5183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
5185 channel_send_client_data (ch, msg, fwd);
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
5190 channel_rel_add_buffered_data (msg, rel);
5195 /* Tunnel is unreliable: send to clients directly */
5196 /* FIXME: accept Out Of Order traffic */
5197 rel->mid_recv = mid + 1;
5198 channel_send_client_data (ch, msg, fwd);
5203 GNUNET_break_op (0);
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5205 " MID %u not expected (%u - %u), dropping!\n",
5206 mid, rel->mid_recv, rel->mid_recv + 64);
5209 channel_send_data_ack (ch, fwd);
5213 * Handler for mesh network traffic end-to-end ACKs.
5215 * @param t Tunnel on which we got this message.
5216 * @param message Data message.
5217 * @param fwd Is this a fwd ACK? (dest->orig)
5220 handle_data_ack (struct MeshTunnel2 *t,
5221 const struct GNUNET_MESH_DataACK *msg, int fwd)
5223 struct MeshChannelReliability *rel;
5224 struct MeshReliableMessage *copy;
5225 struct MeshReliableMessage *next;
5226 struct MeshChannel *ch;
5231 type = ntohs (msg->header.type);
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5233 GNUNET_MESH_DEBUG_M2S (type));
5234 ch = channel_get (t, ntohl (msg->chid));
5237 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5240 ack = ntohl (msg->mid);
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5242 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5244 if (GNUNET_YES == fwd)
5258 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5260 if (GMC_is_pid_bigger (copy->mid, ack))
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5263 channel_rel_free_sent (rel, msg);
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5269 rel_message_free (copy);
5271 /* ACK client if needed */
5272 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5274 /* If some message was free'd, update the retransmission delay*/
5275 if (GNUNET_YES == work)
5277 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5279 GNUNET_SCHEDULER_cancel (rel->retry_task);
5280 if (NULL == rel->head_sent)
5282 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5286 struct GNUNET_TIME_Absolute new_target;
5287 struct GNUNET_TIME_Relative delay;
5289 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5290 MESH_RETRANSMIT_MARGIN);
5291 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5293 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5295 GNUNET_SCHEDULER_add_delayed (delay,
5296 &channel_retransmit_message,
5307 * Core handler for connection creation.
5309 * @param cls Closure (unused).
5310 * @param peer Sender (neighbor).
5311 * @param message Message.
5313 * @return GNUNET_OK to keep the connection open,
5314 * GNUNET_SYSERR to close it (signal serious error)
5317 handle_mesh_connection_create (void *cls,
5318 const struct GNUNET_PeerIdentity *peer,
5319 const struct GNUNET_MessageHeader *message)
5321 struct GNUNET_MESH_ConnectionCreate *msg;
5322 struct GNUNET_PeerIdentity *id;
5323 struct GNUNET_HashCode *cid;
5324 struct MeshPeerPath *path;
5325 struct MeshPeer *dest_peer;
5326 struct MeshPeer *orig_peer;
5327 struct MeshConnection *c;
5328 unsigned int own_pos;
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5336 size = ntohs (message->size);
5337 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5339 GNUNET_break_op (0);
5343 /* Calculate hops */
5344 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5345 if (size % sizeof (struct GNUNET_PeerIdentity))
5347 GNUNET_break_op (0);
5350 size /= sizeof (struct GNUNET_PeerIdentity);
5353 GNUNET_break_op (0);
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5358 /* Get parameters */
5359 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5361 id = (struct GNUNET_PeerIdentity *) &msg[1];
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5363 " connection %s (%s).\n",
5364 GNUNET_h2s (cid), GNUNET_i2s (id));
5366 /* Create connection */
5367 c = connection_get (cid);
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5371 c = connection_new (cid);
5373 return GNUNET_OK; connection_reset_timeout (c, GNUNET_YES);
5374 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5378 path = path_new (size);
5380 for (i = 0; i < size; i++)
5382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5383 GNUNET_i2s (&id[i]));
5384 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5385 if (path->peers[i] == myid)
5388 if (own_pos == 0 && path->peers[own_pos] != myid)
5390 /* create path: self not found in path through self */
5391 GNUNET_break_op (0);
5392 path_destroy (path);
5393 connection_destroy (c);
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5397 path_add_to_peers (path, GNUNET_NO);
5398 c->path = path_duplicate (path);
5399 c->own_pos = own_pos;
5406 /* Remember peers */
5407 dest_peer = peer_get (&id[size - 1]);
5408 orig_peer = peer_get (&id[0]);
5410 /* Is it a connection to us? */
5411 if (c->own_pos == size - 1)
5413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5414 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5416 if (NULL == orig_peer->tunnel)
5417 orig_peer->tunnel = tunnel_new ();
5418 tunnel_add_connection (orig_peer->tunnel, c);
5420 send_connection_ack (c, GNUNET_NO);
5422 /* Keep tunnel alive in direction dest->owner*/
5423 connection_reset_timeout (c, GNUNET_NO);
5427 /* It's for somebody else! Retransmit. */
5428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5429 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5430 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5431 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5438 * Core handler for path ACKs
5440 * @param cls closure
5441 * @param message message
5442 * @param peer peer identity this notification is about
5444 * @return GNUNET_OK to keep the connection open,
5445 * GNUNET_SYSERR to close it (signal serious error)
5448 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5449 const struct GNUNET_MessageHeader *message)
5451 struct GNUNET_MESH_ConnectionACK *msg;
5452 struct MeshPeerPath *p;
5453 struct MeshConnection *c;
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5457 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5459 GNUNET_h2s (&msg->cid));
5460 c = connection_get (&msg->cid);
5463 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5472 /* Add path to peers? */
5476 path_add_to_peers (p, GNUNET_YES);
5482 connection_change_state (c, MESH_CONNECTION_READY);
5483 connection_reset_timeout (c, GNUNET_NO);
5484 if (MESH_TUNNEL_READY != c->t->state)
5485 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5486 tunnel_send_queued_data (c->t, GNUNET_YES);
5488 /* Message for us? */
5489 if (connection_is_terminal (c, GNUNET_NO))
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5492 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5494 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5495 c->t->peer->dhtget = NULL;
5497 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5502 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5508 * Core handler for notifications of broken paths
5510 * @param cls Closure (unused).
5511 * @param peer Peer identity of sending neighbor.
5512 * @param message Message.
5514 * @return GNUNET_OK to keep the connection open,
5515 * GNUNET_SYSERR to close it (signal serious error)
5518 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5519 const struct GNUNET_MessageHeader *message)
5521 struct GNUNET_MESH_ConnectionBroken *msg;
5522 struct MeshConnection *c;
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5525 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5526 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5528 GNUNET_i2s (&msg->peer1));
5529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5530 GNUNET_i2s (&msg->peer2));
5531 c = connection_get (&msg->cid);
5534 GNUNET_break_op (0);
5537 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5538 GNUNET_PEER_search (&msg->peer2));
5545 * Core handler for tunnel destruction
5547 * @param cls Closure (unused).
5548 * @param peer Peer identity of sending neighbor.
5549 * @param message Message.
5551 * @return GNUNET_OK to keep the connection open,
5552 * GNUNET_SYSERR to close it (signal serious error)
5555 handle_mesh_connection_destroy (void *cls,
5556 const struct GNUNET_PeerIdentity *peer,
5557 const struct GNUNET_MessageHeader *message)
5559 struct GNUNET_MESH_ConnectionDestroy *msg;
5560 struct MeshConnection *c;
5564 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5566 "Got a CONNECTION DESTROY message from %s\n",
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5569 " for connection %s\n",
5570 GNUNET_h2s (&msg->cid));
5571 c = connection_get (&msg->cid);
5574 /* Probably already got the message from another path,
5575 * destroyed the tunnel and retransmitted to children.
5578 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5582 id = GNUNET_PEER_search (peer);
5583 if (id == connection_get_prev_hop (c)->id)
5585 else if (id == connection_get_next_hop (c)->id)
5589 GNUNET_break_op (0);
5592 send_prebuilt_message_connection (message, c, NULL, fwd);
5593 c->destroy = GNUNET_YES;
5600 * Handler for channel create messages.
5602 * @param t Tunnel this channel is to be created in.
5603 * @param msg Message.
5604 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5607 handle_channel_create (struct MeshTunnel2 *t,
5608 struct GNUNET_MESH_ChannelCreate *msg,
5611 MESH_ChannelNumber chid;
5612 struct MeshChannel *ch;
5613 struct MeshClient *c;
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5617 /* Check message size */
5618 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5620 GNUNET_break_op (0);
5624 /* Check if channel exists */
5625 chid = ntohl (msg->chid);
5626 ch = channel_get (t, chid);
5629 /* Probably a retransmission, safe to ignore */
5630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5631 if (NULL != ch->dest)
5633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5634 channel_send_ack (ch, !fwd);
5640 /* Create channel */
5641 ch = channel_new (t, NULL, 0);
5642 channel_set_options (ch, ntohl (msg->opt));
5645 /* Find a destination client */
5646 port = ntohl (msg->port);
5647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5648 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5651 /* TODO send reject */
5652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5657 channel_add_client (ch, c);
5658 if (GNUNET_YES == ch->reliable)
5659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5661 send_local_channel_create (ch);
5662 channel_send_ack (ch, !fwd);
5663 send_local_ack (ch, !fwd);
5668 * Handler for channel ack messages.
5670 * @param t Tunnel this channel is to be created in.
5671 * @param msg Message.
5672 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5675 handle_channel_ack (struct MeshTunnel2 *t,
5676 struct GNUNET_MESH_ChannelManage *msg,
5679 MESH_ChannelNumber chid;
5680 struct MeshChannel *ch;
5682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5683 /* Check message size */
5684 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5686 GNUNET_break_op (0);
5690 /* Check if channel exists */
5691 chid = ntohl (msg->chid);
5692 ch = channel_get (t, chid);
5695 GNUNET_break_op (0);
5696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5700 channel_confirm (ch, !fwd);
5705 * Handler for channel destroy messages.
5707 * @param t Tunnel this channel is to be destroyed of.
5708 * @param msg Message.
5709 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5712 handle_channel_destroy (struct MeshTunnel2 *t,
5713 struct GNUNET_MESH_ChannelManage *msg,
5716 MESH_ChannelNumber chid;
5717 struct MeshChannel *ch;
5719 /* Check message size */
5720 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5722 GNUNET_break_op (0);
5726 /* Check if channel exists */
5727 chid = ntohl (msg->chid);
5728 ch = channel_get (t, chid);
5731 /* Probably a retransmission, safe to ignore */
5735 send_local_channel_destroy (ch, fwd);
5736 channel_destroy (ch);
5741 handle_decrypted (struct MeshTunnel2 *t,
5742 const struct GNUNET_MessageHeader *msgh,
5745 switch (ntohs (msgh->type))
5747 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5748 /* Don't send hop ACK, wait for client to ACK */
5749 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5752 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5753 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5756 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5757 handle_channel_create (t,
5758 (struct GNUNET_MESH_ChannelCreate *) msgh,
5762 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5763 handle_channel_ack (t,
5764 (struct GNUNET_MESH_ChannelManage *) msgh,
5768 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5769 handle_channel_destroy (t,
5770 (struct GNUNET_MESH_ChannelManage *) msgh,
5775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5776 "end-to-end message not known (%u)\n",
5777 ntohs (msgh->type));
5783 * Generic handler for mesh network encrypted traffic.
5785 * @param peer Peer identity this notification is about.
5786 * @param message Encrypted message.
5787 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5789 * @return GNUNET_OK to keep the connection open,
5790 * GNUNET_SYSERR to close it (signal serious error)
5793 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5794 const struct GNUNET_MESH_Encrypted *msg,
5797 struct MeshConnection *c;
5798 struct MeshTunnel2 *t;
5799 struct MeshPeer *neighbor;
5800 struct MeshFlowControl *fc;
5807 size = ntohs (msg->header.size);
5809 sizeof (struct GNUNET_MESH_Encrypted) +
5810 sizeof (struct GNUNET_MessageHeader))
5812 GNUNET_break_op (0);
5815 type = ntohs (msg->header.type);
5816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5818 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5820 /* Check connection */
5821 c = connection_get (&msg->cid);
5824 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5829 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5831 /* Check if origin is as expected */
5832 neighbor = connection_get_hop (c, fwd);
5833 if (peer_get (peer)->id != neighbor->id)
5835 GNUNET_break_op (0);
5840 pid = ntohl (msg->pid);
5841 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5843 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5845 "WARNING Received PID %u, (prev %u), ACK %u\n",
5846 pid, fc->last_pid_recv, fc->last_ack_sent);
5849 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5851 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5853 " Pid %u not expected (%u+), dropping!\n",
5854 pid, fc->last_pid_recv + 1);
5857 if (MESH_CONNECTION_SENT == c->state)
5858 connection_change_state (c, MESH_CONNECTION_READY);
5859 connection_reset_timeout (c, fwd);
5860 fc->last_pid_recv = pid;
5862 /* Is this message for us? */
5863 if (connection_is_terminal (c, fwd))
5865 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5867 struct GNUNET_MessageHeader *msgh;
5870 /* TODO signature verification */
5871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5872 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5874 fc->last_pid_recv = pid;
5875 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5879 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
5880 handle_decrypted (t, msgh, fwd);
5881 off += ntohs (msgh->size);
5883 send_ack (c, NULL, fwd);
5887 /* Message not for us: forward to next hop */
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5889 ttl = ntohl (msg->ttl);
5890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5893 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5894 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5895 send_ack (c, NULL, fwd);
5898 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5900 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5907 * Core handler for mesh network traffic going orig->dest.
5909 * @param cls Closure (unused).
5910 * @param message Message received.
5911 * @param peer Peer who sent the message.
5913 * @return GNUNET_OK to keep the connection open,
5914 * GNUNET_SYSERR to close it (signal serious error)
5917 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5918 const struct GNUNET_MessageHeader *message)
5920 return handle_mesh_encrypted (peer,
5921 (struct GNUNET_MESH_Encrypted *)message,
5926 * Core handler for mesh network traffic going dest->orig.
5928 * @param cls Closure (unused).
5929 * @param message Message received.
5930 * @param peer Peer who sent the message.
5932 * @return GNUNET_OK to keep the connection open,
5933 * GNUNET_SYSERR to close it (signal serious error)
5936 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5937 const struct GNUNET_MessageHeader *message)
5939 return handle_mesh_encrypted (peer,
5940 (struct GNUNET_MESH_Encrypted *)message,
5946 * Core handler for mesh network traffic point-to-point acks.
5948 * @param cls closure
5949 * @param message message
5950 * @param peer peer identity this notification is about
5952 * @return GNUNET_OK to keep the connection open,
5953 * GNUNET_SYSERR to close it (signal serious error)
5956 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5957 const struct GNUNET_MessageHeader *message)
5959 struct GNUNET_MESH_ACK *msg;
5960 struct MeshConnection *c;
5961 struct MeshFlowControl *fc;
5965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5968 msg = (struct GNUNET_MESH_ACK *) message;
5970 c = connection_get (&msg->cid);
5974 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5979 /* Is this a forward or backward ACK? */
5980 id = GNUNET_PEER_search (peer);
5981 if (connection_get_next_hop (c)->id == id)
5983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5986 else if (connection_get_prev_hop (c)->id == id)
5988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5993 GNUNET_break_op (0);
5997 ack = ntohl (msg->ack);
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6000 /* Cancel polling if the ACK is bigger than before. */
6001 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
6002 GMC_is_pid_bigger (ack, fc->last_ack_recv))
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
6005 GNUNET_SCHEDULER_cancel (fc->poll_task);
6006 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
6007 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
6010 fc->last_ack_recv = ack;
6011 connection_unlock_queue (c, fc == &c->fwd_fc);
6018 * Core handler for mesh network traffic point-to-point ack polls.
6020 * @param cls closure
6021 * @param message message
6022 * @param peer peer identity this notification is about
6024 * @return GNUNET_OK to keep the connection open,
6025 * GNUNET_SYSERR to close it (signal serious error)
6028 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6029 const struct GNUNET_MessageHeader *message)
6031 struct GNUNET_MESH_Poll *msg;
6032 struct MeshConnection *c;
6033 struct MeshFlowControl *fc;
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
6039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
6042 msg = (struct GNUNET_MESH_Poll *) message;
6044 c = connection_get (&msg->cid);
6048 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
6050 GNUNET_break_op (0);
6054 /* Is this a forward or backward ACK?
6055 * Note: a poll should never be needed in a loopback case,
6056 * since there is no possiblility of packet loss there, so
6057 * this way of discerining FWD/BCK should not be a problem.
6059 id = GNUNET_PEER_search (peer);
6060 if (connection_get_next_hop (c)->id == id)
6062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6065 else if (connection_get_prev_hop (c)->id == id)
6067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6072 GNUNET_break_op (0);
6076 pid = ntohl (msg->pid);
6077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
6078 pid, fc->last_pid_recv);
6079 fc->last_pid_recv = pid;
6080 fwd = fc == &c->fwd_fc;
6081 send_ack (c, NULL, fwd);
6088 * Core handler for mesh keepalives.
6090 * @param cls closure
6091 * @param message message
6092 * @param peer peer identity this notification is about
6093 * @return GNUNET_OK to keep the connection open,
6094 * GNUNET_SYSERR to close it (signal serious error)
6096 * TODO: Check who we got this from, to validate route.
6099 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6100 const struct GNUNET_MessageHeader *message)
6102 struct GNUNET_MESH_ConnectionKeepAlive *msg;
6103 struct MeshConnection *c;
6104 struct MeshPeer *neighbor;
6107 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6111 c = connection_get (&msg->cid);
6114 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
6119 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
6120 GNUNET_YES : GNUNET_NO;
6122 /* Check if origin is as expected */
6123 neighbor = connection_get_hop (c, fwd);
6124 if (peer_get (peer)->id != neighbor->id)
6126 GNUNET_break_op (0);
6130 connection_change_state (c, MESH_CONNECTION_READY);
6131 connection_reset_timeout (c, fwd);
6133 if (connection_is_terminal (c, fwd))
6136 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6137 send_prebuilt_message_connection (message, c, NULL, fwd);
6145 * Functions to handle messages from core
6147 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6148 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
6150 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
6151 sizeof (struct GNUNET_MESH_ConnectionACK)},
6152 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
6153 sizeof (struct GNUNET_MESH_ConnectionBroken)},
6154 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
6155 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
6156 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
6157 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6158 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
6159 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6160 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6161 sizeof (struct GNUNET_MESH_ACK)},
6162 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6163 sizeof (struct GNUNET_MESH_Poll)},
6164 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
6165 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
6171 * Function to process paths received for a new peer addition. The recorded
6172 * paths form the initial tunnel, which can be optimized later.
6173 * Called on each result obtained for the DHT search.
6175 * @param cls closure
6176 * @param exp when will this value expire
6177 * @param key key of the result
6178 * @param get_path path of the get request
6179 * @param get_path_length lenght of get_path
6180 * @param put_path path of the put request
6181 * @param put_path_length length of the put_path
6182 * @param type type of the result
6183 * @param size number of bytes in data
6184 * @param data pointer to the result data
6187 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6188 const struct GNUNET_HashCode * key,
6189 const struct GNUNET_PeerIdentity *get_path,
6190 unsigned int get_path_length,
6191 const struct GNUNET_PeerIdentity *put_path,
6192 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6193 size_t size, const void *data)
6195 struct MeshPeer *peer = cls;
6196 struct MeshPeerPath *p;
6197 struct MeshConnection *c;
6198 struct GNUNET_PeerIdentity pi;
6201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6202 GNUNET_PEER_resolve (peer->id, &pi);
6203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6205 p = path_build_from_dht (get_path, get_path_length,
6206 put_path, put_path_length);
6207 path_add_to_peers (p, GNUNET_NO);
6210 /* Count connections */
6211 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6213 /* If we already have 3 (or more (?!)) connections, it's enough */
6217 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6220 peer_connect (peer);
6226 /******************************************************************************/
6227 /********************* MESH LOCAL HANDLES **************************/
6228 /******************************************************************************/
6232 * Handler for client connection.
6234 * @param cls Closure (unused).
6235 * @param client Client handler.
6238 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6240 struct MeshClient *c;
6244 c = GNUNET_malloc (sizeof (struct MeshClient));
6246 c->id = next_client_id++; /* overflow not important: just for debug */
6247 GNUNET_SERVER_client_keep (client);
6248 GNUNET_SERVER_client_set_user_context (client, c);
6249 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6254 * Handler for client disconnection
6256 * @param cls closure
6257 * @param client identification of the client; NULL
6258 * for the last call when the server is destroyed
6261 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6263 struct MeshClient *c;
6265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6272 c = client_get (client);
6275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6277 GNUNET_SERVER_client_drop (c->handle);
6278 c->shutting_down = GNUNET_YES;
6279 if (NULL != c->own_channels)
6281 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6282 &channel_destroy_iterator, c);
6283 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6286 if (NULL != c->incoming_channels)
6288 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6289 &channel_destroy_iterator, c);
6290 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6293 if (NULL != c->ports)
6295 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6296 &client_release_ports, c);
6297 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6299 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6300 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6314 * Handler for new clients
6316 * @param cls closure
6317 * @param client identification of the client
6318 * @param message the actual message, which includes messages the client wants
6321 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6322 const struct GNUNET_MessageHeader *message)
6324 struct GNUNET_MESH_ClientConnect *cc_msg;
6325 struct MeshClient *c;
6330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6332 /* Check data sanity */
6333 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6334 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6335 if (0 != (size % sizeof (uint32_t)))
6338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6341 size /= sizeof (uint32_t);
6343 /* Initialize new client structure */
6344 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6351 p = (uint32_t *) &cc_msg[1];
6352 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6353 for (i = 0; i < size; i++)
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6358 /* store in client's hashmap */
6359 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6360 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6361 /* store in global hashmap */
6362 /* FIXME only allow one client to have the port open,
6363 * have a backup hashmap with waiting clients */
6364 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6369 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6370 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6371 GNUNET_SERVER_notification_context_add (nc, client);
6372 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6374 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6380 * Handler for requests of new tunnels
6382 * @param cls Closure.
6383 * @param client Identification of the client.
6384 * @param message The actual message.
6387 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6388 const struct GNUNET_MessageHeader *message)
6390 struct GNUNET_MESH_ChannelMessage *msg;
6391 struct MeshPeer *peer;
6392 struct MeshTunnel2 *t;
6393 struct MeshChannel *ch;
6394 struct MeshClient *c;
6395 MESH_ChannelNumber chid;
6397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6399 /* Sanity check for client registration */
6400 if (NULL == (c = client_get (client)))
6403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6408 /* Message size sanity check */
6409 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6416 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6418 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6419 chid = ntohl (msg->channel_id);
6421 /* Sanity check for duplicate channel IDs */
6422 if (NULL != channel_get_by_local_id (c, chid))
6425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6429 peer = peer_get (&msg->peer);
6430 if (NULL == peer->tunnel)
6432 peer->tunnel = tunnel_new ();
6433 peer->tunnel->peer = peer;
6437 /* Create channel */
6438 ch = channel_new (t, c, chid);
6442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445 ch->port = ntohl (msg->port);
6446 channel_set_options (ch, ntohl (msg->opt));
6448 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6449 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6450 ch->root_rel->ch = ch;
6451 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6454 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6455 peer_connect (peer);
6457 /* Send create channel */
6459 struct GNUNET_MESH_ChannelCreate msgcc;
6461 msgcc.header.size = htons (sizeof (msgcc));
6462 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6463 msgcc.chid = htonl (ch->gid);
6464 msgcc.port = msg->port;
6465 msgcc.opt = msg->opt;
6467 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6470 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6476 * Handler for requests of deleting tunnels
6478 * @param cls closure
6479 * @param client identification of the client
6480 * @param message the actual message
6483 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6484 const struct GNUNET_MessageHeader *message)
6486 struct GNUNET_MESH_ChannelMessage *msg;
6487 struct MeshClient *c;
6488 struct MeshChannel *ch;
6489 struct MeshTunnel2 *t;
6490 MESH_ChannelNumber chid;
6492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6493 "Got a DESTROY CHANNEL from client!\n");
6495 /* Sanity check for client registration */
6496 if (NULL == (c = client_get (client)))
6499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6504 /* Message sanity check */
6505 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6512 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6514 /* Retrieve tunnel */
6515 chid = ntohl (msg->channel_id);
6516 ch = channel_get_by_local_id (c, chid);
6519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6525 /* Cleanup after the tunnel */
6526 client_delete_channel (c, ch);
6527 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6531 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6538 " channel %X client %p (%p, %p)\n",
6539 chid, c, ch->root, ch->dest);
6544 channel_destroy (ch);
6545 tunnel_destroy_if_empty (t);
6547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6553 * Handler for client traffic
6555 * @param cls closure
6556 * @param client identification of the client
6557 * @param message the actual message
6560 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6561 const struct GNUNET_MessageHeader *message)
6563 struct GNUNET_MESH_LocalData *msg;
6564 struct MeshClient *c;
6565 struct MeshChannel *ch;
6566 struct MeshChannelReliability *rel;
6567 MESH_ChannelNumber chid;
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6572 "Got data from a client!\n");
6574 /* Sanity check for client registration */
6575 if (NULL == (c = client_get (client)))
6578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6583 msg = (struct GNUNET_MESH_LocalData *) message;
6585 /* Sanity check for message size */
6586 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6587 if (size < sizeof (struct GNUNET_MessageHeader))
6590 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6594 /* Channel exists? */
6595 chid = ntohl (msg->id);
6596 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6597 ch = channel_get_by_local_id (c, chid);
6601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6605 /* Is the client in the channel? */
6608 ch->root->handle == client)
6612 ch->dest->handle == client) ) )
6615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6619 rel = fwd ? ch->root_rel : ch->dest_rel;
6620 rel->client_ready = GNUNET_NO;
6622 /* Ok, everything is correct, send the message. */
6624 struct GNUNET_MESH_Data *payload;
6625 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6626 unsigned char cbuf[p2p_size];
6628 payload = (struct GNUNET_MESH_Data *) cbuf;
6629 payload->mid = htonl (rel->mid_send);
6631 memcpy (&payload[1], &msg[1], size);
6632 payload->header.size = htons (p2p_size);
6633 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6634 payload->chid = htonl (ch->gid);
6635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6636 send_prebuilt_message_channel (&payload->header, ch, fwd);
6638 if (GNUNET_YES == ch->reliable)
6639 channel_save_copy (ch, &payload->header, fwd);
6641 if (tunnel_get_buffer (ch->t, fwd) > 0)
6642 send_local_ack (ch, fwd);
6643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6644 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6651 * Handler for client's ACKs for payload traffic.
6653 * @param cls Closure (unused).
6654 * @param client Identification of the client.
6655 * @param message The actual message.
6658 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6659 const struct GNUNET_MessageHeader *message)
6661 struct GNUNET_MESH_LocalAck *msg;
6662 struct MeshChannelReliability *rel;
6663 struct MeshChannel *ch;
6664 struct MeshClient *c;
6665 MESH_ChannelNumber chid;
6668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6670 /* Sanity check for client registration */
6671 if (NULL == (c = client_get (client)))
6674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6679 msg = (struct GNUNET_MESH_LocalAck *) message;
6681 /* Channel exists? */
6682 chid = ntohl (msg->channel_id);
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6684 ch = channel_get_by_local_id (c, chid);
6685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6695 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6696 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6697 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6698 rel = fwd ? ch->dest_rel : ch->root_rel;
6700 rel->client_ready = GNUNET_YES;
6701 channel_send_client_buffered_data (ch, c, fwd);
6702 send_ack (NULL, ch, fwd);
6704 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6711 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6713 * @param cls Closure (client handle).
6714 * @param key Key (hashed tunnel ID, unused).
6715 * @param value Tunnel info.
6717 * @return GNUNET_YES, to keep iterating.
6720 // monitor_all_tunnels_iterator (void *cls,
6721 // const struct GNUNET_HashCode * key,
6724 // struct GNUNET_SERVER_Client *client = cls;
6725 // struct MeshChannel *ch = value;
6726 // struct GNUNET_MESH_LocalMonitor *msg;
6728 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6729 // msg->channel_id = htonl (ch->gid);
6730 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6731 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6733 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6734 // "* sending info about tunnel %s\n",
6735 // GNUNET_i2s (&msg->owner));
6737 // GNUNET_SERVER_notification_context_unicast (nc, client,
6738 // &msg->header, GNUNET_NO);
6739 // return GNUNET_YES;
6744 * Handler for client's MONITOR request.
6746 * @param cls Closure (unused).
6747 * @param client Identification of the client.
6748 * @param message The actual message.
6751 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6752 const struct GNUNET_MessageHeader *message)
6754 struct MeshClient *c;
6756 /* Sanity check for client registration */
6757 if (NULL == (c = client_get (client)))
6760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6764 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6765 "Received get tunnels request from client %u\n",
6767 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6768 // monitor_all_tunnels_iterator,
6770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6771 "Get tunnels request from client %u completed\n",
6773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6778 * Handler for client's MONITOR_TUNNEL request.
6780 * @param cls Closure (unused).
6781 * @param client Identification of the client.
6782 * @param message The actual message.
6785 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6786 const struct GNUNET_MessageHeader *message)
6788 const struct GNUNET_MESH_LocalMonitor *msg;
6789 struct GNUNET_MESH_LocalMonitor *resp;
6790 struct MeshClient *c;
6791 struct MeshChannel *ch;
6793 /* Sanity check for client registration */
6794 if (NULL == (c = client_get (client)))
6797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6801 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6803 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6806 ntohl (msg->channel_id));
6807 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6811 /* We don't know the tunnel */
6812 struct GNUNET_MESH_LocalMonitor warn;
6815 GNUNET_SERVER_notification_context_unicast (nc, client,
6818 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6822 /* Initialize context */
6823 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6825 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6826 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6827 &resp->header, GNUNET_NO);
6830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6831 "Monitor tunnel request from client %u completed\n",
6833 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6838 * Functions to handle messages from clients
6840 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6841 {&handle_local_new_client, NULL,
6842 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6843 {&handle_local_channel_create, NULL,
6844 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6845 sizeof (struct GNUNET_MESH_ChannelMessage)},
6846 {&handle_local_channel_destroy, NULL,
6847 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6848 sizeof (struct GNUNET_MESH_ChannelMessage)},
6849 {&handle_local_data, NULL,
6850 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6851 {&handle_local_ack, NULL,
6852 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6853 sizeof (struct GNUNET_MESH_LocalAck)},
6854 {&handle_local_get_tunnels, NULL,
6855 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6856 sizeof (struct GNUNET_MessageHeader)},
6857 {&handle_local_show_tunnel, NULL,
6858 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6859 sizeof (struct GNUNET_MESH_LocalMonitor)},
6865 * Method called whenever a given peer connects.
6867 * @param cls closure
6868 * @param peer peer identity this notification is about
6871 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6873 struct MeshPeer *pi;
6874 struct MeshPeerPath *path;
6876 DEBUG_CONN ("Peer connected\n");
6877 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6878 pi = peer_get (peer);
6881 DEBUG_CONN (" (self)\n");
6882 path = path_new (1);
6886 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6887 path = path_new (2);
6888 path->peers[1] = pi->id;
6889 GNUNET_PEER_change_rc (pi->id, 1);
6890 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6892 path->peers[0] = myid;
6893 GNUNET_PEER_change_rc (myid, 1);
6894 peer_add_path (pi, path, GNUNET_YES);
6896 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6902 * Method called whenever a peer disconnects.
6904 * @param cls closure
6905 * @param peer peer identity this notification is about
6908 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6910 struct MeshPeer *pi;
6912 DEBUG_CONN ("Peer disconnected\n");
6913 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6920 peer_remove_path (pi, myid, pi->id);
6922 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6925 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6926 pi->connections = NULL;
6929 DEBUG_CONN (" (self)\n");
6931 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6938 * Install server (service) handlers and start listening to clients.
6943 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6944 GNUNET_SERVER_connect_notify (server_handle,
6945 &handle_local_client_connect, NULL);
6946 GNUNET_SERVER_disconnect_notify (server_handle,
6947 &handle_local_client_disconnect, NULL);
6948 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6950 clients_head = NULL;
6951 clients_tail = NULL;
6953 GNUNET_SERVER_resume (server_handle);
6958 * To be called on core init/fail.
6960 * @param cls Closure (config)
6961 * @param identity the public identity of this peer
6964 core_init (void *cls,
6965 const struct GNUNET_PeerIdentity *identity)
6967 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6971 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6973 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6974 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6976 GNUNET_i2s (identity));
6977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6979 GNUNET_i2s (&my_full_id));
6980 GNUNET_CORE_disconnect (core_handle);
6981 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6982 NULL, /* Closure passed to MESH functions */
6983 &core_init, /* Call core_init once connected */
6984 &core_connect, /* Handle connects */
6985 &core_disconnect, /* remove peers on disconnects */
6986 NULL, /* Don't notify about all incoming messages */
6987 GNUNET_NO, /* For header only in notification */
6988 NULL, /* Don't notify about all outbound messages */
6989 GNUNET_NO, /* For header-only out notification */
6990 core_handlers); /* Register these handlers */
6999 /******************************************************************************/
7000 /************************ MAIN FUNCTIONS ****************************/
7001 /******************************************************************************/
7004 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7006 * @param cls closure
7007 * @param key current key code
7008 * @param value value in the hash map
7009 * @return GNUNET_YES if we should continue to iterate,
7013 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7015 struct MeshPeer *p = value;
7016 struct MeshTunnel2 *t = p->tunnel;
7025 * Task run during shutdown.
7031 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7035 if (core_handle != NULL)
7037 GNUNET_CORE_disconnect (core_handle);
7040 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
7041 if (dht_handle != NULL)
7043 GNUNET_DHT_disconnect (dht_handle);
7048 GNUNET_SERVER_notification_context_destroy (nc);
7051 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7053 GNUNET_SCHEDULER_cancel (announce_id_task);
7054 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7061 * Process mesh requests.
7063 * @param cls closure
7064 * @param server the initialized server
7065 * @param c configuration to use
7068 run (void *cls, struct GNUNET_SERVER_Handle *server,
7069 const struct GNUNET_CONFIGURATION_Handle *c)
7072 struct GNUNET_CRYPTO_EccPrivateKey *pk;
7074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7075 server_handle = server;
7076 GNUNET_SERVER_suspend (server_handle);
7079 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
7082 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7084 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7085 "mesh", "peer/privatekey");
7086 GNUNET_SCHEDULER_shutdown ();
7091 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
7092 &refresh_connection_time))
7094 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7096 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7097 "mesh", "refresh path time");
7098 GNUNET_SCHEDULER_shutdown ();
7103 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7108 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7109 "mesh", "id announce time");
7110 GNUNET_SCHEDULER_shutdown ();
7115 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7120 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7121 "mesh", "connect timeout");
7122 GNUNET_SCHEDULER_shutdown ();
7127 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7132 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7133 "mesh", "max msgs queue");
7134 GNUNET_SCHEDULER_shutdown ();
7139 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
7142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7144 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7145 "mesh", "max tunnels");
7146 GNUNET_SCHEDULER_shutdown ();
7151 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7156 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7157 "mesh", "default ttl", 64);
7162 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7165 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7166 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7167 "mesh", "max peers", 1000);
7172 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
7179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7180 "Mesh is running with drop mode enabled. "
7181 "This is NOT a good idea! "
7182 "Remove the DROP_PERCENT option from your configuration.\n");
7186 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7187 &dht_replication_level))
7189 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7191 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7192 "mesh", "dht replication level", 3);
7193 dht_replication_level = 3;
7196 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7197 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7198 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7200 dht_handle = GNUNET_DHT_connect (c, 64);
7201 if (NULL == dht_handle)
7205 stats = GNUNET_STATISTICS_create ("mesh", c);
7207 /* Scheduled the task to clean up when shutdown is called */
7208 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7210 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7211 GNUNET_free (keyfile);
7212 GNUNET_assert (NULL != pk);
7213 my_private_key = pk;
7214 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7215 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7216 &my_full_id.hashPubKey);
7217 myid = GNUNET_PEER_intern (&my_full_id);
7218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7219 "Mesh for peer [%s] starting\n",
7220 GNUNET_i2s(&my_full_id));
7222 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7223 NULL, /* Closure passed to MESH functions */
7224 &core_init, /* Call core_init once connected */
7225 &core_connect, /* Handle connects */
7226 &core_disconnect, /* remove peers on disconnects */
7227 NULL, /* Don't notify about all incoming messages */
7228 GNUNET_NO, /* For header only in notification */
7229 NULL, /* Don't notify about all outbound messages */
7230 GNUNET_NO, /* For header-only out notification */
7231 core_handlers); /* Register these handlers */
7232 if (NULL == core_handle)
7235 GNUNET_SCHEDULER_shutdown ();
7238 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7244 * The main function for the mesh service.
7246 * @param argc number of arguments from the command line
7247 * @param argv command line arguments
7248 * @return 0 ok, 1 on error
7251 main (int argc, char *const *argv)
7256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7257 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7259 ret = (GNUNET_OK == r) ? 0 : 1;
7260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");