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;
1846 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1847 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1848 emsg = (struct GNUNET_MESH_Encrypted *) data;
1849 ttl = ntohl (emsg->ttl);
1852 GNUNET_break_op (0);
1856 emsg->ttl = htonl (ttl - 1);
1857 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1861 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1862 amsg = (struct GNUNET_MESH_ACK *) data;
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1867 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1868 pmsg = (struct GNUNET_MESH_Poll *) data;
1870 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1874 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1875 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
1894 * Sends an already built message on a tunnel, choosing the best connection.
1896 * @param message Message to send. Function modifies it.
1897 * @param t Tunnel on which this message is transmitted.
1898 * @param ch Channel on which this message is transmitted.
1899 * @param fwd Is this a fwd message?
1902 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1903 struct MeshTunnel2 *t,
1904 struct MeshChannel *ch,
1907 struct MeshConnection *c;
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1912 c = tunnel_get_connection (t, fwd);
1915 GNUNET_break (GNUNET_YES == t->destroy);
1918 type = ntohs (msg->header.type);
1921 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1922 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1923 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1924 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1926 msg->ttl = htonl (default_ttl);
1929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1930 GNUNET_MESH_DEBUG_M2S (type));
1935 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1940 * Sends an already built message on a channel, properly registering
1941 * all used resources and encrypting the message with the tunnel's key.
1943 * @param message Message to send. Function makes a copy of it.
1944 * @param ch Channel on which this message is transmitted.
1945 * @param fwd Is this a fwd message?
1948 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1949 struct MeshChannel *ch,
1952 struct GNUNET_MESH_Encrypted *msg;
1953 size_t size = ntohs (message->size);
1954 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1959 peer2s (ch->t->peer), ch->gid);
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1961 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1963 if (channel_is_terminal (ch, fwd))
1965 handle_decrypted (ch->t, message, fwd);
1969 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1970 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1972 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1973 msg->header.type = htons (type);
1974 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1975 msg->iv = GNUNET_htonll (iv);
1976 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1977 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1982 * Sends a CREATE CONNECTION message for a path to a peer.
1983 * Changes the connection and tunnel states if necessary.
1985 * @param connection Connection to create.
1988 send_connection_create (struct MeshConnection *connection)
1990 struct MeshTunnel2 *t;
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1995 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1996 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1997 (connection->path->length *
1998 sizeof (struct GNUNET_PeerIdentity)),
2003 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
2004 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2005 if (MESH_CONNECTION_NEW == connection->state)
2006 connection_change_state (connection, MESH_CONNECTION_SENT);
2011 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
2014 * @param connection Connection to confirm.
2015 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
2018 send_connection_ack (struct MeshConnection *connection, int fwd)
2020 struct MeshTunnel2 *t;
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
2025 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
2026 sizeof (struct GNUNET_MESH_ConnectionACK),
2030 if (MESH_TUNNEL_NEW == t->state)
2031 tunnel_change_state (t, MESH_TUNNEL_WAITING);
2036 * Core callback to write a pre-constructed data packet to core buffer
2038 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2039 * @param size Number of bytes available in buf.
2040 * @param buf Where the to write the message.
2042 * @return number of bytes written to buf
2045 send_core_data_raw (void *cls, size_t size, void *buf)
2047 struct GNUNET_MessageHeader *msg = cls;
2050 GNUNET_assert (NULL != msg);
2051 total_size = ntohs (msg->size);
2053 if (total_size > size)
2058 memcpy (buf, msg, total_size);
2065 * Function to send a create connection message to a peer.
2067 * @param c Connection to create.
2068 * @param size number of bytes available in buf
2069 * @param buf where the callee should write the message
2070 * @return number of bytes written to buf
2073 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
2075 struct GNUNET_MESH_ConnectionCreate *msg;
2076 struct GNUNET_PeerIdentity *peer_ptr;
2077 struct MeshPeerPath *p = c->path;
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
2083 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2084 p->length * sizeof (struct GNUNET_PeerIdentity);
2086 if (size < size_needed || NULL == buf)
2091 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
2092 msg->header.size = htons (size_needed);
2093 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
2096 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2097 for (i = 0; i < p->length; i++)
2099 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
2109 * Creates a path ack message in buf and frees all unused resources.
2111 * @param c Connection to send an ACK on.
2112 * @param size number of bytes available in buf
2113 * @param buf where the callee should write the message
2115 * @return number of bytes written to buf
2118 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
2120 struct GNUNET_MESH_ConnectionACK *msg = buf;
2121 struct MeshTunnel2 *t = c->t;
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
2124 GNUNET_assert (NULL != t);
2125 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
2130 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
2131 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
2134 /* TODO add signature */
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2137 return sizeof (struct GNUNET_MESH_ConnectionACK);
2142 * Destroy the peer_info and free any allocated resources linked to it
2144 * @param peer The peer_info to destroy.
2146 * @return GNUNET_OK on success
2149 peer_destroy (struct MeshPeer *peer)
2151 struct GNUNET_PeerIdentity id;
2152 struct MeshPeerPath *p;
2153 struct MeshPeerPath *nextp;
2155 GNUNET_PEER_resolve (peer->id, &id);
2156 GNUNET_PEER_change_rc (peer->id, -1);
2159 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2163 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2165 if (NULL != peer->dhtget)
2167 GNUNET_DHT_get_stop (peer->dhtget);
2169 p = peer->path_head;
2173 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2177 tunnel_destroy_empty (peer->tunnel);
2184 * Returns if peer is used (has a tunnel, is neighbor).
2186 * @peer Peer to check.
2188 * @return GNUNET_YES if peer is in use.
2191 peer_is_used (struct MeshPeer *peer)
2193 struct MeshPeerPath *p;
2195 if (NULL != peer->tunnel)
2198 for (p = peer->path_head; NULL != p; p = p->next)
2207 * Iterator over all the peers to get the oldest timestamp.
2209 * @param cls Closure (unsued).
2210 * @param key ID of the peer.
2211 * @param value Peer_Info of the peer.
2214 peer_get_oldest (void *cls,
2215 const struct GNUNET_HashCode *key,
2218 struct MeshPeer *p = value;
2219 struct GNUNET_TIME_Absolute *abs = cls;
2221 /* Don't count active peers */
2222 if (GNUNET_YES == peer_is_used (p))
2225 if (abs->abs_value_us < p->last_contact.abs_value_us)
2226 abs->abs_value_us = p->last_contact.abs_value_us;
2233 * Iterator over all the peers to remove the oldest entry.
2235 * @param cls Closure (unsued).
2236 * @param key ID of the peer.
2237 * @param value Peer_Info of the peer.
2240 peer_timeout (void *cls,
2241 const struct GNUNET_HashCode *key,
2244 struct MeshPeer *p = value;
2245 struct GNUNET_TIME_Absolute *abs = cls;
2247 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2248 GNUNET_NO == peer_is_used (p))
2258 * Delete oldest unused peer.
2261 peer_delete_oldest (void)
2263 struct GNUNET_TIME_Absolute abs;
2265 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2267 GNUNET_CONTAINER_multihashmap_iterate (peers,
2270 GNUNET_CONTAINER_multihashmap_iterate (peers,
2277 * Retrieve the MeshPeer stucture associated with the peer, create one
2278 * and insert it in the appropriate structures if the peer is not known yet.
2280 * @param peer Full identity of the peer.
2282 * @return Existing or newly created peer info.
2284 static struct MeshPeer *
2285 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2287 struct MeshPeer *peer;
2289 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2292 peer = GNUNET_new (struct MeshPeer);
2293 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2295 peer_delete_oldest ();
2297 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2298 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2299 peer->id = GNUNET_PEER_intern (peer_id);
2301 peer->last_contact = GNUNET_TIME_absolute_get();
2308 * Retrieve the MeshPeer stucture associated with the peer, create one
2309 * and insert it in the appropriate structures if the peer is not known yet.
2311 * @param peer Short identity of the peer.
2313 * @return Existing or newly created peer info.
2315 static struct MeshPeer *
2316 peer_get_short (const GNUNET_PEER_Id peer)
2318 return peer_get (GNUNET_PEER_resolve2 (peer));
2323 * Get a cost of a path for a peer considering existing tunnel connections.
2325 * @param peer Peer towards which the path is considered.
2326 * @param path Candidate path.
2328 * @return Cost of the path (path length + number of overlapping nodes)
2331 peer_get_path_cost (const struct MeshPeer *peer,
2332 const struct MeshPeerPath *path)
2334 struct MeshConnection *c;
2335 unsigned int overlap;
2343 GNUNET_assert (NULL != peer->tunnel);
2345 for (i = 0; i < path->length; i++)
2347 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2349 for (j = 0; j < c->path->length; j++)
2351 if (path->peers[i] == c->path->peers[j])
2359 return (path->length + overlap) * (path->score * -1);
2364 * Choose the best path towards a peer considering the tunnel properties.
2366 * @param peer The destination peer.
2368 * @return Best current known path towards the peer, if any.
2370 static struct MeshPeerPath *
2371 peer_get_best_path (const struct MeshPeer *peer)
2373 struct MeshPeerPath *best_p;
2374 struct MeshPeerPath *p;
2375 struct MeshConnection *c;
2376 unsigned int best_cost;
2379 best_cost = UINT_MAX;
2381 for (p = peer->path_head; NULL != p; p = p->next)
2383 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2387 continue; /* If path is in use in a connection, skip it. */
2389 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2399 queue_is_sendable (struct MeshPeerQueue *q)
2401 struct MeshFlowControl *fc;
2403 /* Is PID-independent? */
2406 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2407 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2411 /* Is PID allowed? */
2412 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2413 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2421 * Get first sendable message.
2423 * @param peer The destination peer.
2425 * @return Best current known path towards the peer, if any.
2427 static struct MeshPeerQueue *
2428 peer_get_first_message (const struct MeshPeer *peer)
2430 struct MeshPeerQueue *q;
2432 for (q = peer->queue_head; NULL != q; q = q->next)
2434 if (queue_is_sendable (q))
2443 * Try to establish a new connection to this peer in the given tunnel.
2444 * If the peer doesn't have any path to it yet, try to get one.
2445 * If the peer already has some path, send a CREATE CONNECTION towards it.
2447 * @param peer PeerInfo of the peer.
2450 peer_connect (struct MeshPeer *peer)
2452 struct MeshTunnel2 *t;
2453 struct MeshPeerPath *p;
2454 struct MeshConnection *c;
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2457 "peer_connect towards %s\n",
2460 if (NULL != peer->path_head)
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2463 p = peer_get_best_path (peer);
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2467 c = tunnel_use_path (t, p);
2468 send_connection_create (c);
2471 else if (NULL == peer->dhtget)
2473 const struct GNUNET_PeerIdentity *id;
2475 id = GNUNET_PEER_resolve2 (peer->id);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 " Starting DHT GET for peer %s\n", peer2s (peer));
2478 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2479 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2480 &id->hashPubKey, /* key to search */
2481 dht_replication_level, /* replication level */
2482 GNUNET_DHT_RO_RECORD_ROUTE |
2483 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2485 0, /* xquery bits */
2486 &dht_get_id_handler, peer);
2487 if (MESH_TUNNEL_NEW == t->state)
2488 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2493 "There is no path but the DHT GET is already started.\n");
2499 * Get the first transmittable message for a connection.
2501 * @param c Connection.
2502 * @param fwd Is this FWD?
2504 * @return First transmittable message.
2506 static struct MeshPeerQueue *
2507 connection_get_first_message (struct MeshConnection *c, int fwd)
2509 struct MeshPeerQueue *q;
2512 p = connection_get_hop (c, fwd);
2514 for (q = p->queue_head; NULL != q; q = q->next)
2518 if (queue_is_sendable (q))
2527 * @brief Re-initiate traffic on this connection if necessary.
2529 * Check if there is traffic queued towards this peer
2530 * and the core transmit handle is NULL (traffic was stalled).
2531 * If so, call core tmt rdy.
2533 * @param c Connection on which initiate traffic.
2534 * @param fwd Is this about fwd traffic?
2537 connection_unlock_queue (struct MeshConnection *c, int fwd)
2539 struct MeshPeer *peer;
2540 struct MeshPeerQueue *q;
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2544 "connection_unlock_queue %s on %s\n",
2545 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2547 if (connection_is_origin (c, fwd))
2549 struct MeshTunnel2 *t = c->t;
2550 struct MeshChannel *ch;
2551 struct MeshChannelReliability *rel;
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2554 /* FIXME randomize channel selection, not always first channel */
2555 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2557 rel = fwd ? ch->root_rel : ch->dest_rel;
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2560 ch->gid, rel->client_ready ? "ready " : "not ready");
2561 if (GNUNET_NO == rel->client_ready)
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2564 send_local_ack (ch, fwd);
2565 return; /* FIXME authorize all channels? */
2571 peer = connection_get_hop (c, fwd);
2573 if (NULL != peer->core_transmit)
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2576 return; /* Already unlocked */
2579 q = connection_get_first_message (c, fwd);
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2583 return; /* Nothing to transmit */
2587 peer->core_transmit =
2588 GNUNET_CORE_notify_transmit_ready (core_handle,
2591 GNUNET_TIME_UNIT_FOREVER_REL,
2592 GNUNET_PEER_resolve2 (peer->id),
2600 * Cancel all transmissions that belong to a certain connection.
2602 * @param c Connection which to cancel.
2603 * @param fwd Cancel fwd traffic?
2606 connection_cancel_queues (struct MeshConnection *c, int fwd)
2608 struct MeshPeerQueue *q;
2609 struct MeshPeerQueue *next;
2610 struct MeshFlowControl *fc;
2611 struct MeshPeer *peer;
2618 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2619 peer = connection_get_hop (c, fwd);
2621 for (q = peer->queue_head; NULL != q; q = next)
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 "connection_cancel_queue %s\n",
2628 GNUNET_MESH_DEBUG_M2S (q->type));
2629 queue_destroy (q, GNUNET_YES);
2632 if (NULL == peer->queue_head)
2634 if (NULL != peer->core_transmit)
2636 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2637 peer->core_transmit = NULL;
2639 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2641 GNUNET_SCHEDULER_cancel (fc->poll_task);
2642 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2649 * Remove all paths that rely on a direct connection between p1 and p2
2650 * from the peer itself and notify all tunnels about it.
2652 * @param peer PeerInfo of affected peer.
2653 * @param p1 GNUNET_PEER_Id of one peer.
2654 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2657 * TODO: optimize (see below)
2660 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2663 struct MeshPeerPath *p;
2664 struct MeshPeerPath *next;
2665 struct MeshPeer *peer_d;
2667 unsigned int destroyed;
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2672 for (p = peer->path_head; NULL != p; p = next)
2675 for (i = 0; i < (p->length - 1); i++)
2677 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2678 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2680 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2691 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2693 peer_d = peer_get_short (d); // FIXME
2694 next = peer_get_best_path (peer_d);
2695 tunnel_use_path (peer->tunnel, next);
2696 peer_connect (peer_d);
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2703 * Add the path to the peer and update the path used to reach it in case this
2706 * @param peer_info Destination peer to add the path to.
2707 * @param path New path to add. Last peer must be the peer in arg 1.
2708 * Path will be either used of freed if already known.
2709 * @param trusted Do we trust that this path is real?
2712 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2715 struct MeshPeerPath *aux;
2719 if ((NULL == peer_info) || (NULL == path))
2722 path_destroy (path);
2725 if (path->peers[path->length - 1] != peer_info->id)
2728 path_destroy (path);
2731 if (2 >= path->length && GNUNET_NO == trusted)
2733 /* Only allow CORE to tell us about direct paths */
2734 path_destroy (path);
2737 for (l = 1; l < path->length; l++)
2739 if (path->peers[l] == myid)
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2742 for (l2 = 0; l2 < path->length - l; l2++)
2744 path->peers[l2] = path->peers[l + l2];
2749 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2754 path->length, peer2s (peer_info));
2756 l = path_get_length (path);
2759 path_destroy (path);
2763 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2764 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2766 l2 = path_get_length (aux);
2769 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2770 peer_info->path_tail, aux, path);
2775 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2777 path_destroy (path);
2782 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2789 * Add the path to the origin peer and update the path used to reach it in case
2790 * this is the shortest.
2791 * The path is given in peer_info -> destination, therefore we turn the path
2792 * upside down first.
2794 * @param peer_info Peer to add the path to, being the origin of the path.
2795 * @param path New path to add after being inversed.
2796 * Path will be either used or freed.
2797 * @param trusted Do we trust that this path is real?
2800 peer_add_path_to_origin (struct MeshPeer *peer_info,
2801 struct MeshPeerPath *path, int trusted)
2804 peer_add_path (peer_info, path, trusted);
2810 * Function called if a connection has been stalled for a while,
2811 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2813 * @param cls Closure (poll ctx).
2814 * @param tc TaskContext.
2817 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2819 struct MeshFlowControl *fc = cls;
2820 struct GNUNET_MESH_Poll msg;
2821 struct MeshConnection *c;
2823 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2824 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2832 peer2s (c->t->peer), c->id);
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** %s\n",
2834 fc == &c->fwd_fc ? "FWD" : "BCK");
2836 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2837 msg.header.size = htons (sizeof (msg));
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2839 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2840 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2841 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2842 &connection_poll, fc);
2847 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2848 * to obtain a local peer -> destination path and interning the peer ids.
2850 * @return Newly allocated and created path
2852 static struct MeshPeerPath *
2853 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2854 unsigned int get_path_length,
2855 const struct GNUNET_PeerIdentity *put_path,
2856 unsigned int put_path_length)
2858 struct MeshPeerPath *p;
2864 GNUNET_PEER_change_rc (myid, 1);
2865 i = get_path_length;
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2867 for (i--; i >= 0; i--)
2869 id = GNUNET_PEER_intern (&get_path[i]);
2870 if (p->length > 0 && id == p->peers[p->length - 1])
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2873 GNUNET_PEER_change_rc (id, -1);
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2878 GNUNET_i2s (&get_path[i]));
2880 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2881 p->peers[p->length - 1] = id;
2884 i = put_path_length;
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2886 for (i--; i >= 0; i--)
2888 id = GNUNET_PEER_intern (&put_path[i]);
2891 /* PUT path went through us, so discard the path up until now and start
2892 * from here to get a much shorter (and loop-free) path.
2897 if (p->length > 0 && id == p->peers[p->length - 1])
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2900 GNUNET_PEER_change_rc (id, -1);
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2905 GNUNET_i2s (&put_path[i]));
2907 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2908 p->peers[p->length - 1] = id;
2912 if (get_path_length > 0)
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2914 GNUNET_i2s (&get_path[0]));
2915 if (put_path_length > 0)
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2917 GNUNET_i2s (&put_path[0]));
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2920 for (i = 0; i < p->length; i++)
2922 struct GNUNET_PeerIdentity peer_id;
2924 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2926 GNUNET_i2s (&peer_id));
2934 * Adds a path to the peer_infos of all the peers in the path
2936 * @param p Path to process.
2937 * @param confirmed Whether we know if the path works or not.
2940 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2944 /* TODO: invert and add */
2945 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2946 for (i++; i < p->length; i++)
2948 struct MeshPeer *aux;
2949 struct MeshPeerPath *copy;
2951 aux = peer_get_short (p->peers[i]);
2952 copy = path_duplicate (p);
2953 copy->length = i + 1;
2954 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2960 * Search for a channel among the channels for a client
2962 * @param c the client whose channels to search in
2963 * @param chid the local id of the channel
2965 * @return channel handler, NULL if doesn't exist
2967 static struct MeshChannel *
2968 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2971 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2973 GNUNET_break_op (0);
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2977 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2978 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2979 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2985 channel_debug (struct MeshChannel *ch)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X\n",
2993 peer2s (ch->t->peer), ch->gid);
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2995 ch->root, ch->root_rel);
2996 if (NULL != ch->root)
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
3000 ch->root_rel->client_ready ? "YES" : "NO");
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
3004 ch->dest, ch->dest_rel);
3005 if (NULL != ch->dest)
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
3009 ch->dest_rel->client_ready ? "YES" : "NO");
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
3015 fc_debug (struct MeshFlowControl *fc)
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
3018 fc->last_pid_recv, fc->last_ack_sent);
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
3020 fc->last_pid_sent, fc->last_ack_recv);
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
3022 fc->queue_n, fc->queue_max);
3026 connection_debug (struct MeshConnection *c)
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
3034 peer2s (c->t->peer), GNUNET_h2s (&c->id));
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " state: %u, pending msgs: %u\n",
3036 c->state, c->pending_messages);
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
3038 fc_debug (&c->fwd_fc);
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
3040 fc_debug (&c->bck_fc);
3045 static struct MeshChannel *
3046 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
3048 struct MeshChannel *ch;
3053 for (ch = t->channel_head; NULL != ch; ch = ch->next)
3055 if (ch->gid == chid)
3064 * Change the tunnel state.
3066 * @param t Tunnel whose state to change.
3067 * @param state New state.
3070 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075 "Tunnel %s state was %s\n",
3077 GNUNET_MESH_DEBUG_TS2S (t->state));
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3079 "Tunnel %s state is now %s\n",
3081 GNUNET_MESH_DEBUG_TS2S (state));
3087 * Cache a message to be sent once tunnel is online.
3089 * @param t Tunnel to hold the message.
3090 * @param ch Channel the message is about.
3091 * @param msg Message itself (copy will be made).
3092 * @param fwd Is this fwd?
3095 tunnel_queue_data (struct MeshTunnel2 *t,
3096 struct MeshChannel *ch,
3097 struct GNUNET_MessageHeader *msg,
3100 struct MeshTunnelQueue *tq;
3101 uint16_t size = ntohs (msg->size);
3103 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
3106 memcpy (&tq[1], msg, size);
3107 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
3112 * Send all cached messages that we can, tunnel is online.
3114 * @param t Tunnel that holds the messages.
3115 * @param fwd Is this fwd?
3118 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
3120 struct MeshTunnelQueue *tq;
3121 struct MeshTunnelQueue *next;
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3125 "tunnel_send_queued_data on tunnel %s\n",
3127 room = tunnel_get_buffer (t, fwd);
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
3129 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
3133 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
3134 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
3143 connection_change_state (struct MeshConnection* c,
3144 enum MeshConnectionState state)
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "Connection %s state was %s\n",
3148 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "Connection %s state is now %s\n",
3151 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
3157 * Add a client to a channel, initializing all needed data structures.
3159 * @param ch Channel to which add the client.
3160 * @param c Client which to add to the channel.
3163 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3165 struct MeshTunnel2 *t = ch->t;
3167 if (NULL != ch->dest)
3173 /* Assign local id as destination */
3174 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3175 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3176 ch->lid_dest = t->next_local_chid++;
3177 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3179 /* Store in client's hashmap */
3181 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3189 GNUNET_break (NULL == ch->dest_rel);
3190 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3191 ch->dest_rel->ch = ch;
3192 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3198 static struct MeshConnection *
3199 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3201 struct MeshConnection *c;
3202 struct GNUNET_HashCode cid;
3203 struct MeshPeer *peer;
3204 unsigned int own_pos;
3206 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3208 c = connection_new (&cid);
3210 for (own_pos = 0; own_pos < p->length; own_pos++)
3212 if (p->peers[own_pos] == myid)
3215 if (own_pos > p->length - 1)
3218 connection_destroy (c);
3221 c->own_pos = own_pos;
3226 c->fwd_maintenance_task =
3227 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3228 &connection_fwd_keepalive, c);
3231 peer = connection_get_next_hop (c);
3232 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3234 peer = connection_get_prev_hop (c);
3235 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3242 * Notifies a tunnel that a connection has broken that affects at least
3243 * some of its peers. Sends a notification towards the root of the tree.
3244 * In case the peer is the owner of the tree, notifies the client that owns
3245 * the tunnel and tries to reconnect.
3247 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3249 * @param t Tunnel affected.
3250 * @param p1 Peer that got disconnected from p2.
3251 * @param p2 Peer that got disconnected from p1.
3253 * @return Short ID of the peer disconnected (either p1 or p2).
3254 * 0 if the tunnel remained unaffected.
3256 static GNUNET_PEER_Id
3257 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3258 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3260 // if (myid != p1 && myid != p2) FIXME
3265 // if (tree_get_predecessor (t->tree) != 0)
3267 // /* We are the peer still connected, notify owner of the disconnection. */
3268 // struct GNUNET_MESH_PathBroken msg;
3269 // struct GNUNET_PeerIdentity neighbor;
3271 // msg.header.size = htons (sizeof (msg));
3272 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3273 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3274 // msg.tid = htonl (t->id.tid);
3275 // msg.peer1 = my_full_id;
3276 // GNUNET_PEER_resolve (pid, &msg.peer2);
3277 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3278 // send_prebuilt_message (&msg.header, &neighbor, t);
3285 * Send an end-to-end ACK message for the most recent in-sequence payload.
3287 * If channel is not reliable, do nothing.
3289 * @param ch Channel this is about.
3290 * @param fwd Is for FWD traffic? (ACK dest->owner)
3293 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3295 struct GNUNET_MESH_DataACK msg;
3296 struct MeshChannelReliability *rel;
3297 struct MeshReliableMessage *copy;
3302 if (GNUNET_NO == ch->reliable)
3306 rel = fwd ? ch->dest_rel : ch->root_rel;
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 "send_data_ack for %u\n",
3311 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3312 msg.header.type = htons (type);
3313 msg.header.size = htons (sizeof (msg));
3314 msg.chid = htonl (ch->gid);
3315 msg.mid = htonl (rel->mid_recv - 1);
3317 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3319 if (copy->type != type)
3321 delta = copy->mid - rel->mid_recv;
3324 mask = 0x1LL << delta;
3325 msg.futures |= mask;
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3327 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3328 copy->mid, delta, mask, msg.futures);
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3332 send_prebuilt_message_channel (&msg.header, ch, fwd);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3338 * Send an ACK informing the predecessor about the available buffer space.
3340 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3341 * the ACK itself goes "back" (dest->root).
3343 * @param c Connection on which to send the ACK.
3344 * @param buffer How much space free to advertise?
3345 * @param fwd Is this FWD ACK? (Going dest->owner)
3348 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3350 struct MeshFlowControl *next_fc;
3351 struct MeshFlowControl *prev_fc;
3352 struct GNUNET_MESH_ACK msg;
3356 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3357 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3360 "connection send %s ack on %s\n",
3361 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3363 /* Check if we need to transmit the ACK */
3364 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3368 " last pid recv: %u, last ack sent: %u\n",
3369 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3373 /* Ok, ACK might be necessary, what PID to ACK? */
3374 delta = next_fc->queue_max - next_fc->queue_n;
3375 ack = prev_fc->last_pid_recv + delta;
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3378 " last pid %u, last ack %u, qmax %u, q %u\n",
3379 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3380 next_fc->queue_max, next_fc->queue_n);
3381 if (ack == prev_fc->last_ack_sent)
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3387 prev_fc->last_ack_sent = ack;
3389 /* Build ACK message and send on connection */
3390 msg.header.size = htons (sizeof (msg));
3391 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3392 msg.ack = htonl (ack);
3395 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3400 * Modify the mesh message TID from global to local and send to client.
3402 * @param ch Channel on which to send the message.
3403 * @param msg Message to modify and send.
3404 * @param c Client to send to.
3405 * @param tid Tunnel ID to use (c can be both owner and client).
3408 channel_send_client_to_tid (struct MeshChannel *ch,
3409 const struct GNUNET_MESH_Data *msg,
3410 struct MeshClient *c, MESH_ChannelNumber id)
3412 struct GNUNET_MESH_LocalData *copy;
3413 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3414 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3416 if (size < sizeof (struct GNUNET_MessageHeader))
3418 GNUNET_break_op (0);
3426 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3427 memcpy (©[1], &msg[1], size);
3428 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3429 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3430 copy->id = htonl (id);
3431 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3432 ©->header, GNUNET_NO);
3437 * We have received a message out of order, or the client is not ready.
3438 * Buffer it until we receive an ACK from the client or the missing
3439 * message from the channel.
3441 * @param msg Message to buffer (MUST be of type MESH_DATA).
3442 * @param rel Reliability data to the corresponding direction.
3445 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3446 struct MeshChannelReliability *rel)
3448 struct MeshReliableMessage *copy;
3449 struct MeshReliableMessage *prev;
3453 size = ntohs (msg->header.size);
3454 mid = ntohl (msg->mid);
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3458 copy = GNUNET_malloc (sizeof (*copy) + size);
3461 memcpy (©[1], msg, size);
3465 // FIXME do something better than O(n), although n < 64...
3466 // FIXME start from the end (most messages are the latest ones)
3467 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3470 if (GMC_is_pid_bigger (prev->mid, mid))
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3473 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3479 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3485 channel_send_client_data (struct MeshChannel *ch,
3486 const struct GNUNET_MESH_Data *msg,
3491 if (ch->dest_rel->client_ready)
3492 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3494 channel_rel_add_buffered_data (msg, ch->dest_rel);
3498 if (ch->root_rel->client_ready)
3499 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3501 channel_rel_add_buffered_data (msg, ch->root_rel);
3507 * Send a buffered message to the client, for in order delivery or
3508 * as result of client ACK.
3510 * @param ch Channel on which to empty the message buffer.
3511 * @param c Client to send to.
3512 * @param rel Reliability structure to corresponding peer.
3513 * If rel == bck_rel, this is FWD data.
3516 channel_send_client_buffered_data (struct MeshChannel *ch,
3517 struct MeshClient *c,
3520 struct MeshReliableMessage *copy;
3521 struct MeshChannelReliability *rel;
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3524 rel = fwd ? ch->dest_rel : ch->root_rel;
3525 if (GNUNET_NO == rel->client_ready)
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3531 copy = rel->head_recv;
3532 /* We never buffer channel management messages */
3535 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3537 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3540 " have %u! now expecting %u\n",
3541 copy->mid, rel->mid_recv + 1);
3542 channel_send_client_data (ch, msg, fwd);
3545 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551 " reliable && don't have %u, next is %u\n",
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3562 * Destroy a reliable message after it has been acknowledged, either by
3563 * direct mid ACK or bitfield. Updates the appropriate data structures and
3564 * timers and frees all memory.
3566 * @param copy Message that is no longer needed: remote peer got it.
3569 rel_message_free (struct MeshReliableMessage *copy)
3571 struct MeshChannelReliability *rel;
3572 struct GNUNET_TIME_Relative time;
3575 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3576 rel->expected_delay.rel_value_us *= 7;
3577 rel->expected_delay.rel_value_us += time.rel_value_us;
3578 rel->expected_delay.rel_value_us /= 8;
3580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3583 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3585 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3587 rel->retry_timer = rel->expected_delay;
3588 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3594 * Destroy all reliable messages queued for a channel,
3595 * during a channel destruction.
3596 * Frees the reliability structure itself.
3598 * @param rel Reliability data for a channel.
3601 channel_rel_free_all (struct MeshChannelReliability *rel)
3603 struct MeshReliableMessage *copy;
3604 struct MeshReliableMessage *next;
3609 for (copy = rel->head_recv; NULL != copy; copy = next)
3612 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3615 for (copy = rel->head_sent; NULL != copy; copy = next)
3618 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3621 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3622 GNUNET_SCHEDULER_cancel (rel->retry_task);
3628 * Mark future messages as ACK'd.
3630 * @param rel Reliability data.
3631 * @param msg DataACK message with a bitfield of future ACK'd messages.
3634 channel_rel_free_sent (struct MeshChannelReliability *rel,
3635 const struct GNUNET_MESH_DataACK *msg)
3637 struct MeshReliableMessage *copy;
3638 struct MeshReliableMessage *next;
3645 bitfield = msg->futures;
3646 mid = ntohl (msg->mid);
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "free_sent_reliable %u %llX\n",
3650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3651 " rel %p, head %p\n",
3652 rel, rel->head_sent);
3653 for (i = 0, copy = rel->head_sent;
3654 i < 64 && NULL != copy && 0 != bitfield;
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3658 " trying bit %u (mid %u)\n",
3661 if (0 == (bitfield & mask))
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3665 /* Bit was set, clear the bit from the bitfield */
3668 /* The i-th bit was set. Do we have that copy? */
3669 /* Skip copies with mid < target */
3670 target = mid + i + 1;
3671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3672 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3675 /* Did we run out of copies? (previously freed, it's ok) */
3678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3682 /* Did we overshoot the target? (previously freed, it's ok) */
3683 if (GMC_is_pid_bigger (copy->mid, target))
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3689 /* Now copy->mid == target, free it */
3691 rel_message_free (copy);
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3699 * We haven't received an ACK after a certain time: restransmit the message.
3701 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3702 * @param tc TaskContext.
3705 channel_retransmit_message (void *cls,
3706 const struct GNUNET_SCHEDULER_TaskContext *tc)
3708 struct MeshChannelReliability *rel = cls;
3709 struct MeshReliableMessage *copy;
3710 struct MeshPeerQueue *q;
3711 struct MeshChannel *ch;
3712 struct MeshConnection *c;
3713 struct GNUNET_MESH_Data *payload;
3714 struct MeshPeer *hop;
3717 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3718 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3722 copy = rel->head_sent;
3729 /* Search the message to be retransmitted in the outgoing queue.
3730 * Check only the queue for the connection that is going to be used,
3731 * if the message is stuck in some other connection's queue we shouldn't
3733 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3734 * the old connection could be temporary stalled or the queue happened to
3735 * be long at time of insertion.
3736 * - not sending the new one could cause terrible delays the old connection
3739 payload = (struct GNUNET_MESH_Data *) ©[1];
3740 fwd = (rel == ch->root_rel);
3741 c = tunnel_get_connection (ch->t, fwd);
3742 hop = connection_get_hop (c, fwd);
3743 for (q = hop->queue_head; NULL != q; q = q->next)
3745 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3747 struct GNUNET_MESH_Data *queued_data = q->cls;
3749 if (queued_data->mid == payload->mid)
3754 /* Message not found in the queue that we are going to use. */
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3759 send_prebuilt_message_channel (&payload->header, ch, fwd);
3760 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3767 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3768 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3769 &channel_retransmit_message,
3775 * Send ACK on one or more connections due to buffer space to the client.
3777 * Iterates all connections of the tunnel and sends ACKs appropriately.
3779 * @param ch Channel which has some free buffer space.
3780 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3783 channel_send_connections_ack (struct MeshChannel *ch,
3784 unsigned int buffer,
3787 struct MeshTunnel2 *t = ch->t;
3788 struct MeshConnection *c;
3789 struct MeshFlowControl *fc;
3792 uint32_t allow_per_connection;
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3796 "Channel send connection %s ack on %s:%X\n",
3797 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3799 /* Count connections, how many messages are already allowed */
3800 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3802 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3803 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3808 allowed += fc->last_ack_sent - fc->last_pid_recv;
3812 /* Make sure there is no overflow */
3813 if (allowed > buffer)
3819 /* Authorize connections to send more data */
3820 to_allow = buffer - allowed;
3822 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3824 allow_per_connection = to_allow/cs;
3825 to_allow -= allow_per_connection;
3827 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3828 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3832 connection_send_ack (c, allow_per_connection, fwd);
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3836 "Channel send connection %s ack on %s:%X\n",
3837 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3838 GNUNET_break (to_allow == 0);
3843 * Send an ACK on the appropriate connection/channel, depending on
3844 * the direction and the position of the peer.
3846 * @param c Which connection to send the hop-by-hop ACK.
3847 * @param ch Channel, if any.
3848 * @param fwd Is this a fwd ACK? (will go dest->root)
3851 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3853 unsigned int buffer;
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3856 "send ack %s on %p %p\n",
3857 fwd ? "FWD" : "BCK", c, ch);
3858 if (NULL == c || connection_is_terminal (c, fwd))
3860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Channel\n");
3861 buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " getting from Connection\n");
3866 GNUNET_assert (NULL != c);
3867 buffer = connection_get_buffer (c, fwd);
3869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on all connections\n");
3874 GNUNET_assert (NULL != ch);
3875 channel_send_connections_ack (ch, buffer, fwd);
3877 else if (connection_is_origin (c, fwd))
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " really sending!\n");
3883 GNUNET_assert (NULL != ch);
3884 send_local_ack (ch, fwd);
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
3890 connection_send_ack (c, buffer, fwd);
3896 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3898 * @param ch Channel to mark as ready.
3899 * @param fwd Was the CREATE message sent fwd?
3902 channel_confirm (struct MeshChannel *ch, int fwd)
3904 struct MeshChannelReliability *rel;
3905 struct MeshReliableMessage *copy;
3906 struct MeshReliableMessage *next;
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 " channel confirm %s %s:%X\n",
3910 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3911 ch->state = MESH_CHANNEL_READY;
3913 rel = fwd ? ch->root_rel : ch->dest_rel;
3914 for (copy = rel->head_sent; NULL != copy; copy = next)
3916 struct GNUNET_MessageHeader *msg;
3919 msg = (struct GNUNET_MessageHeader *) ©[1];
3920 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3922 rel_message_free (copy);
3926 if (GNUNET_NO == rel->client_ready)
3927 send_local_ack (ch, fwd);
3932 * Save a copy to retransmit in case it gets lost.
3934 * Initializes all needed callbacks and timers.
3936 * @param ch Channel this message goes on.
3937 * @param msg Message to copy.
3938 * @param fwd Is this fwd traffic?
3941 channel_save_copy (struct MeshChannel *ch,
3942 const struct GNUNET_MessageHeader *msg,
3945 struct MeshChannelReliability *rel;
3946 struct MeshReliableMessage *copy;
3951 rel = fwd ? ch->root_rel : ch->dest_rel;
3952 mid = rel->mid_send;
3953 type = ntohs (msg->type);
3954 size = ntohs (msg->size);
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3957 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3959 copy->timestamp = GNUNET_TIME_absolute_get ();
3962 memcpy (©[1], msg, size);
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3965 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3966 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3969 GNUNET_TIME_relative_multiply (rel->expected_delay,
3970 MESH_RETRANSMIT_MARGIN);
3972 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3973 &channel_retransmit_message,
3980 * Send keepalive packets for a connection.
3982 * @param c Connection to keep alive..
3983 * @param fwd Is this a FWD keepalive? (owner -> dest).
3986 connection_keepalive (struct MeshConnection *c, int fwd)
3988 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3989 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3993 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3994 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3997 "sending %s keepalive for connection %s[%d]\n",
3998 fwd ? "FWD" : "BCK",
3999 peer2s (c->t->peer),
4002 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
4003 msg->header.size = htons (size);
4004 msg->header.type = htons (type);
4007 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
4012 * Send CONNECTION_{CREATE/ACK} packets for a connection.
4014 * @param c Connection for which to send the message.
4015 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
4018 connection_recreate (struct MeshConnection *c, int fwd)
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
4022 send_connection_create (c);
4024 send_connection_ack (c, GNUNET_NO);
4029 * Generic connection timer management.
4030 * Depending on the role of the peer in the connection will send the
4031 * appropriate message (build or keepalive)
4033 * @param c Conncetion to maintain.
4034 * @param fwd Is FWD?
4037 connection_maintain (struct MeshConnection *c, int fwd)
4039 if (MESH_TUNNEL_SEARCHING == c->t->state)
4041 /* TODO DHT GET with RO_BART */
4046 case MESH_CONNECTION_NEW:
4048 case MESH_CONNECTION_SENT:
4049 connection_recreate (c, fwd);
4051 case MESH_CONNECTION_READY:
4052 connection_keepalive (c, fwd);
4061 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4063 struct MeshConnection *c = cls;
4065 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4066 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4069 connection_maintain (c, GNUNET_YES);
4070 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4071 &connection_fwd_keepalive,
4077 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4079 struct MeshConnection *c = cls;
4081 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4082 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4085 connection_maintain (c, GNUNET_NO);
4086 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4087 &connection_bck_keepalive,
4093 * Send a message to all peers in this connection that the connection
4094 * is no longer valid.
4096 * If some peer should not receive the message, it should be zero'ed out
4097 * before calling this function.
4099 * @param c The connection whose peers to notify.
4102 connection_send_destroy (struct MeshConnection *c)
4104 struct GNUNET_MESH_ConnectionDestroy msg;
4106 msg.header.size = htons (sizeof (msg));
4107 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
4109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4110 " sending connection destroy for connection %s[%X]\n",
4111 peer2s (c->t->peer),
4114 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
4115 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
4116 c->destroy = GNUNET_YES;
4121 * Confirm we got a channel create.
4123 * @param ch The channel to confirm.
4124 * @param fwd Should we send the ACK fwd?
4127 channel_send_ack (struct MeshChannel *ch, int fwd)
4129 struct GNUNET_MESH_ChannelManage msg;
4131 msg.header.size = htons (sizeof (msg));
4132 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
4133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4134 " sending channel ack for channel %s:%X\n",
4135 peer2s (ch->t->peer),
4138 msg.chid = htonl (ch->gid);
4139 send_prebuilt_message_channel (&msg.header, ch, fwd);
4144 * Send a message to all clients (local and remote) of this channel
4145 * notifying that the channel is no longer valid.
4147 * If some peer or client should not receive the message,
4148 * should be zero'ed out before calling this function.
4150 * @param ch The channel whose clients to notify.
4153 channel_send_destroy (struct MeshChannel *ch)
4155 struct GNUNET_MESH_ChannelManage msg;
4157 msg.header.size = htons (sizeof (msg));
4158 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4160 " sending channel destroy for channel %s:%X\n",
4161 peer2s (ch->t->peer),
4164 if (channel_is_terminal (ch, GNUNET_NO))
4166 if (NULL != ch->root && GNUNET_NO == ch->root->shutting_down)
4168 msg.chid = htonl (ch->lid_root);
4169 send_local_channel_destroy (ch, GNUNET_NO);
4174 msg.chid = htonl (ch->gid);
4175 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4178 if (channel_is_terminal (ch, GNUNET_YES))
4180 if (NULL != ch->dest && GNUNET_NO == ch->dest->shutting_down)
4182 msg.chid = htonl (ch->lid_dest);
4183 send_local_channel_destroy (ch, GNUNET_YES);
4188 msg.chid = htonl (ch->gid);
4189 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4197 static struct MeshTunnel2 *
4200 struct MeshTunnel2 *t;
4202 t = GNUNET_new (struct MeshTunnel2);
4204 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4206 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4207 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4209 // GNUNET_break (0);
4210 // tunnel_destroy (t);
4214 // char salt[] = "salt";
4215 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4216 // salt, sizeof (salt),
4217 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4218 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4219 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4221 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4222 // salt, sizeof (salt),
4223 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4224 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4225 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4233 * Add a connection to a tunnel.
4236 * @param c Connection.
4239 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4242 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4247 * Initialize a Flow Control structure to the initial state.
4249 * @param fc Flow Control structure to initialize.
4252 fc_init (struct MeshFlowControl *fc)
4255 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4256 fc->last_pid_recv = (uint32_t) -1;
4257 fc->last_ack_sent = (uint32_t) 0;
4258 fc->last_ack_recv = (uint32_t) 0;
4259 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4260 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4262 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4266 static struct MeshConnection *
4267 connection_new (const struct GNUNET_HashCode *cid)
4269 struct MeshConnection *c;
4271 c = GNUNET_new (struct MeshConnection);
4273 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4274 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4275 fc_init (&c->fwd_fc);
4276 fc_init (&c->bck_fc);
4285 * Find a connection.
4287 * @param cid Connection ID.
4289 static struct MeshConnection *
4290 connection_get (const struct GNUNET_HashCode *cid)
4292 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4297 connection_destroy (struct MeshConnection *c)
4299 struct MeshPeer *peer;
4304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4305 peer2s (c->t->peer),
4308 /* Cancel all traffic */
4309 connection_cancel_queues (c, GNUNET_YES);
4310 connection_cancel_queues (c, GNUNET_NO);
4312 /* Cancel maintainance task (keepalive/timeout) */
4313 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4314 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4315 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4316 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4318 /* Deregister from neighbors */
4319 peer = connection_get_next_hop (c);
4321 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4322 peer = connection_get_prev_hop (c);
4324 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4327 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4328 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4334 tunnel_destroy (struct MeshTunnel2 *t)
4336 struct MeshConnection *c;
4337 struct MeshConnection *next;
4342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4345 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4346 // GNUNET_break (0);
4348 for (c = t->connection_head; NULL != c; c = next)
4351 connection_destroy (c);
4354 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4361 * Tunnel is empty: destroy it.
4363 * Notifies all connections about the destruction.
4365 * @param t Tunnel to destroy.
4368 tunnel_destroy_empty (struct MeshTunnel2 *t)
4370 struct MeshConnection *c;
4372 for (c = t->connection_head; NULL != c; c = c->next)
4374 if (GNUNET_NO == c->destroy)
4375 connection_send_destroy (c);
4378 if (0 == t->pending_messages)
4381 t->destroy = GNUNET_YES;
4386 * Destroy tunnel if empty (no more channels).
4388 * @param t Tunnel to destroy if empty.
4391 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4393 if (NULL != t->channel_head)
4396 tunnel_destroy_empty (t);
4401 * Destroy a channel and free all resources.
4403 * @param ch Channel to destroy.
4406 channel_destroy (struct MeshChannel *ch)
4408 struct MeshClient *c;
4416 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4427 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4434 channel_rel_free_all (ch->root_rel);
4435 channel_rel_free_all (ch->dest_rel);
4437 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4438 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4444 * Create a new channel.
4446 * @param t Tunnel this channel is in.
4447 * @param owner Client that owns the channel, NULL for foreign channels.
4448 * @param lid_root Local ID for root client.
4450 * @return A new initialized channel. NULL on error.
4452 static struct MeshChannel *
4453 channel_new (struct MeshTunnel2 *t,
4454 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4456 struct MeshChannel *ch;
4458 ch = GNUNET_new (struct MeshChannel);
4460 ch->lid_root = lid_root;
4463 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4465 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4469 while (NULL != channel_get (t, t->next_chid))
4470 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4471 ch->gid = t->next_chid;
4472 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4475 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4479 channel_destroy (ch);
4480 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4490 * Set options in a channel, extracted from a bit flag field
4492 * @param ch Channel to set options to.
4493 * @param options Bit array in host byte order.
4496 channel_set_options (struct MeshChannel *ch, uint32_t options)
4498 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4499 GNUNET_YES : GNUNET_NO;
4500 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4501 GNUNET_YES : GNUNET_NO;
4506 * Iterator for deleting each channel whose client endpoint disconnected.
4508 * @param cls Closure (client that has disconnected).
4509 * @param key The local channel id (used to access the hashmap).
4510 * @param value The value stored at the key (channel to destroy).
4512 * @return GNUNET_OK, keep iterating.
4515 channel_destroy_iterator (void *cls,
4519 struct MeshChannel *ch = value;
4520 struct MeshClient *c = cls;
4521 struct MeshTunnel2 *t;
4523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4524 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4525 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4537 channel_send_destroy (ch);
4538 channel_destroy (ch);
4539 tunnel_destroy_if_empty (t);
4546 * Remove client's ports from the global hashmap on disconnect.
4548 * @param cls Closure (unused).
4550 * @param value Client structure.
4552 * @return GNUNET_OK, keep iterating.
4555 client_release_ports (void *cls,
4561 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4562 if (GNUNET_YES != res)
4565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4566 "Port %u by client %p was not registered.\n",
4574 * Timeout function due to lack of keepalive/traffic from the owner.
4575 * Destroys connection if called.
4577 * @param cls Closure (connection to destroy).
4578 * @param tc TaskContext.
4581 connection_fwd_timeout (void *cls,
4582 const struct GNUNET_SCHEDULER_TaskContext *tc)
4584 struct MeshConnection *c = cls;
4586 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4587 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4590 "Connection %s[%X] FWD timed out. Destroying.\n",
4591 peer2s (c->t->peer),
4594 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4597 connection_destroy (c);
4602 * Timeout function due to lack of keepalive/traffic from the destination.
4603 * Destroys connection if called.
4605 * @param cls Closure (connection to destroy).
4606 * @param tc TaskContext
4609 connection_bck_timeout (void *cls,
4610 const struct GNUNET_SCHEDULER_TaskContext *tc)
4612 struct MeshConnection *c = cls;
4614 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4615 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4619 "Connection %s[%X] FWD timed out. Destroying.\n",
4620 peer2s (c->t->peer),
4623 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4626 connection_destroy (c);
4631 * Resets the connection timeout task, some other message has done the
4633 * - For the first peer on the direction this means to send
4634 * a keepalive or a path confirmation message (either create or ACK).
4635 * - For all other peers, this means to destroy the connection,
4636 * due to lack of activity.
4637 * Starts the tiemout if no timeout was running (connection just created).
4639 * @param c Connection whose timeout to reset.
4640 * @param fwd Is this forward?
4642 * TODO use heap to improve efficiency of scheduler.
4645 connection_reset_timeout (struct MeshConnection *c, int fwd)
4647 GNUNET_SCHEDULER_TaskIdentifier *ti;
4648 GNUNET_SCHEDULER_Task f;
4650 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4652 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4653 GNUNET_SCHEDULER_cancel (*ti);
4655 if (connection_is_origin (c, fwd)) /* Endpoint */
4657 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4658 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4662 struct GNUNET_TIME_Relative delay;
4664 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4665 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4666 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4672 * Iterator to notify all connections of a broken link. Mark connections
4673 * to destroy after all traffic has been sent.
4675 * @param cls Closure (peer disconnected).
4676 * @param key Current key code (tid).
4677 * @param value Value in the hash map (connection).
4679 * @return GNUNET_YES if we should continue to iterate,
4683 connection_broken (void *cls,
4684 const struct GNUNET_HashCode *key,
4687 struct MeshPeer *peer = cls;
4688 struct MeshConnection *c = value;
4689 struct GNUNET_MESH_ConnectionBroken msg;
4692 fwd = peer == connection_get_prev_hop (c);
4693 connection_cancel_queues (c, !fwd);
4695 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4696 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4698 msg.peer1 = my_full_id;
4699 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4700 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4701 c->destroy = GNUNET_YES;
4706 /******************************************************************************/
4707 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4708 /******************************************************************************/
4711 * Free a transmission that was already queued with all resources
4712 * associated to the request.
4714 * @param queue Queue handler to cancel.
4715 * @param clear_cls Is it necessary to free associated cls?
4718 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4720 struct MeshPeer *peer;
4721 struct MeshFlowControl *fc;
4726 GNUNET_assert (NULL != queue->c);
4727 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4729 if (GNUNET_YES == clear_cls)
4731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4732 GNUNET_MESH_DEBUG_M2S (queue->type));
4733 switch (queue->type)
4735 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4736 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4737 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4738 GNUNET_break (GNUNET_YES == queue->c->destroy);
4740 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4741 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4742 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4743 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4744 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4745 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4747 GNUNET_free_non_null (queue->cls);
4752 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4753 GNUNET_MESH_DEBUG_M2S (queue->type));
4757 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4759 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4760 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4766 if (NULL != queue->c)
4768 queue->c->pending_messages--;
4769 if (NULL != queue->c->t)
4771 queue->c->t->pending_messages--;
4775 GNUNET_free (queue);
4780 queue_send (void *cls, size_t size, void *buf)
4782 struct MeshPeer *peer = cls;
4783 struct MeshFlowControl *fc;
4784 struct MeshConnection *c;
4785 struct GNUNET_MessageHeader *msg;
4786 struct MeshPeerQueue *queue;
4787 struct MeshTunnel2 *t;
4788 struct MeshChannel *ch;
4789 const struct GNUNET_PeerIdentity *dst_id;
4795 peer->core_transmit = NULL;
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4798 if (NULL == buf || 0 == size)
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4805 queue = peer_get_first_message (peer);
4808 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4813 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4816 dst_id = GNUNET_PEER_resolve2 (peer->id);
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4818 /* Check if buffer size is enough for the message */
4819 if (queue->size > size)
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4822 peer->core_transmit =
4823 GNUNET_CORE_notify_transmit_ready (core_handle,
4826 GNUNET_TIME_UNIT_FOREVER_REL,
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4835 t = (NULL != c) ? c->t : NULL;
4839 switch (queue->type)
4841 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4842 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4843 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4844 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4845 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4846 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4849 GNUNET_MESH_DEBUG_M2S (queue->type));
4850 data_size = send_core_data_raw (queue->cls, size, buf);
4851 msg = (struct GNUNET_MessageHeader *) buf;
4852 type = ntohs (msg->type);
4854 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4856 if (connection_is_origin (c, GNUNET_YES))
4857 data_size = send_core_connection_create (queue->c, size, buf);
4859 data_size = send_core_data_raw (queue->cls, size, buf);
4861 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4863 if (connection_is_origin (c, GNUNET_NO))
4864 data_size = send_core_connection_ack (queue->c, size, buf);
4866 data_size = send_core_data_raw (queue->cls, size, buf);
4868 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4869 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4870 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4871 /* This should be encapsulted */
4876 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4881 if (0 < drop_percent &&
4882 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4884 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4885 "Dropping message of type %s\n",
4886 GNUNET_MESH_DEBUG_M2S (queue->type));
4890 /* Free queue, but cls was freed by send_core_* */
4892 queue_destroy (queue, GNUNET_NO);
4894 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4897 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4898 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4899 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4900 fc->last_pid_sent = pid;
4901 send_ack (c, ch, fwd);
4907 /* If more data in queue, send next */
4908 queue = peer_get_first_message (peer);
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4912 if (NULL == peer->core_transmit) {
4913 peer->core_transmit =
4914 GNUNET_CORE_notify_transmit_ready(core_handle,
4917 GNUNET_TIME_UNIT_FOREVER_REL,
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4926 "* tmt rdy called somewhere else\n");
4928 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4932 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4937 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4939 GNUNET_SCHEDULER_cancel (fc->poll_task);
4940 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4945 c->pending_messages--;
4946 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4949 connection_destroy (c);
4955 t->pending_messages--;
4956 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4958 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4968 queue_add (void *cls, uint16_t type, size_t size,
4969 struct MeshConnection *c,
4970 struct MeshChannel *ch,
4973 struct MeshPeerQueue *queue;
4974 struct MeshFlowControl *fc;
4975 struct MeshPeer *peer;
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4979 "queue add %s %s (%u) on c %p, ch %p\n",
4980 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4981 GNUNET_assert (NULL != c);
4983 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4984 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4994 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4995 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
5002 if (fc->queue_n >= fc->queue_max && 0 == priority)
5004 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5008 "queue full: %u/%u\n",
5009 fc->queue_n, fc->queue_max);
5010 return; /* Drop this message */
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
5015 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
5016 GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
5017 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5020 "no buffer space (%u > %u): starting poll\n",
5021 fc->last_pid_sent + 1, fc->last_ack_recv);
5022 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
5026 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5034 if (100 <= priority)
5036 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
5040 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
5046 if (NULL == peer->core_transmit)
5048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5049 "calling core tmt rdy towards %s for %u bytes\n",
5050 peer2s (peer), size);
5051 peer->core_transmit =
5052 GNUNET_CORE_notify_transmit_ready (core_handle,
5055 GNUNET_TIME_UNIT_FOREVER_REL,
5056 GNUNET_PEER_resolve2 (peer->id),
5063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5064 "core tmt rdy towards %s already called\n",
5068 c->pending_messages++;
5069 c->t->pending_messages++;
5073 /******************************************************************************/
5074 /******************** MESH NETWORK HANDLERS **************************/
5075 /******************************************************************************/
5079 * Generic handler for mesh network payload traffic.
5081 * @param t Tunnel on which we got this message.
5082 * @param message Unencryted data message.
5083 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5086 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
5088 struct MeshChannelReliability *rel;
5089 struct MeshChannel *ch;
5090 struct MeshClient *c;
5096 size = ntohs (msg->header.size);
5098 sizeof (struct GNUNET_MESH_Data) +
5099 sizeof (struct GNUNET_MessageHeader))
5104 type = ntohs (msg->header.type);
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
5106 GNUNET_MESH_DEBUG_M2S (type));
5107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
5108 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5111 ch = channel_get (t, ntohl (msg->chid));
5114 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
5115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
5119 /* Initialize FWD/BCK data */
5120 c = fwd ? ch->dest : ch->root;
5121 rel = fwd ? ch->dest_rel : ch->root_rel;
5129 tunnel_change_state (t, MESH_TUNNEL_READY);
5131 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
5133 mid = ntohl (msg->mid);
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
5136 if (GNUNET_NO == ch->reliable ||
5137 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
5138 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
5141 if (GNUNET_YES == ch->reliable)
5143 /* Is this the exact next expected messasge? */
5144 if (mid == rel->mid_recv)
5146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
5148 channel_send_client_data (ch, msg, fwd);
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
5153 channel_rel_add_buffered_data (msg, rel);
5158 /* Tunnel is unreliable: send to clients directly */
5159 /* FIXME: accept Out Of Order traffic */
5160 rel->mid_recv = mid + 1;
5161 channel_send_client_data (ch, msg, fwd);
5166 GNUNET_break_op (0);
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5168 " MID %u not expected (%u - %u), dropping!\n",
5169 mid, rel->mid_recv, rel->mid_recv + 64);
5172 channel_send_data_ack (ch, fwd);
5176 * Handler for mesh network traffic end-to-end ACKs.
5178 * @param t Tunnel on which we got this message.
5179 * @param message Data message.
5180 * @param fwd Is this a fwd ACK? (dest->orig)
5183 handle_data_ack (struct MeshTunnel2 *t,
5184 const struct GNUNET_MESH_DataACK *msg, int fwd)
5186 struct MeshChannelReliability *rel;
5187 struct MeshReliableMessage *copy;
5188 struct MeshReliableMessage *next;
5189 struct MeshChannel *ch;
5194 type = ntohs (msg->header.type);
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5196 GNUNET_MESH_DEBUG_M2S (type));
5197 ch = channel_get (t, ntohl (msg->chid));
5200 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5203 ack = ntohl (msg->mid);
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5205 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5207 if (GNUNET_YES == fwd)
5221 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5223 if (GMC_is_pid_bigger (copy->mid, ack))
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5226 channel_rel_free_sent (rel, msg);
5230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5232 rel_message_free (copy);
5234 /* ACK client if needed */
5235 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5237 /* If some message was free'd, update the retransmission delay*/
5238 if (GNUNET_YES == work)
5240 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5242 GNUNET_SCHEDULER_cancel (rel->retry_task);
5243 if (NULL == rel->head_sent)
5245 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5249 struct GNUNET_TIME_Absolute new_target;
5250 struct GNUNET_TIME_Relative delay;
5252 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5253 MESH_RETRANSMIT_MARGIN);
5254 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5256 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5258 GNUNET_SCHEDULER_add_delayed (delay,
5259 &channel_retransmit_message,
5270 * Core handler for connection creation.
5272 * @param cls Closure (unused).
5273 * @param peer Sender (neighbor).
5274 * @param message Message.
5276 * @return GNUNET_OK to keep the connection open,
5277 * GNUNET_SYSERR to close it (signal serious error)
5280 handle_mesh_connection_create (void *cls,
5281 const struct GNUNET_PeerIdentity *peer,
5282 const struct GNUNET_MessageHeader *message)
5284 struct GNUNET_MESH_ConnectionCreate *msg;
5285 struct GNUNET_PeerIdentity *id;
5286 struct GNUNET_HashCode *cid;
5287 struct MeshPeerPath *path;
5288 struct MeshPeer *dest_peer;
5289 struct MeshPeer *orig_peer;
5290 struct MeshConnection *c;
5291 unsigned int own_pos;
5295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5299 size = ntohs (message->size);
5300 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5302 GNUNET_break_op (0);
5306 /* Calculate hops */
5307 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5308 if (size % sizeof (struct GNUNET_PeerIdentity))
5310 GNUNET_break_op (0);
5313 size /= sizeof (struct GNUNET_PeerIdentity);
5316 GNUNET_break_op (0);
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5321 /* Get parameters */
5322 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5324 id = (struct GNUNET_PeerIdentity *) &msg[1];
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5326 " connection %s (%s).\n",
5327 GNUNET_h2s (cid), GNUNET_i2s (id));
5329 /* Create connection */
5330 c = connection_get (cid);
5333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5334 c = connection_new (cid);
5338 connection_reset_timeout (c, GNUNET_YES);
5339 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5341 /* Remember peers */
5342 dest_peer = peer_get (&id[size - 1]);
5343 orig_peer = peer_get (&id[0]);
5346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5347 path = path_new (size);
5349 for (i = 0; i < size; i++)
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5352 GNUNET_i2s (&id[i]));
5353 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5354 if (path->peers[i] == myid)
5357 if (own_pos == 0 && path->peers[own_pos] != myid)
5359 /* create path: self not found in path through self */
5360 GNUNET_break_op (0);
5361 path_destroy (path);
5362 connection_destroy (c);
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5366 path_add_to_peers (path, GNUNET_NO);
5367 c->path = path_duplicate (path);
5368 c->own_pos = own_pos;
5370 /* Is it a connection to us? */
5371 if (own_pos == size - 1)
5373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5374 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5376 if (NULL == orig_peer->tunnel)
5377 orig_peer->tunnel = tunnel_new ();
5378 tunnel_add_connection (orig_peer->tunnel, c);
5380 send_connection_ack (c, GNUNET_NO);
5382 /* Keep tunnel alive in direction dest->owner*/
5383 connection_reset_timeout (c, GNUNET_NO);
5387 /* It's for somebody else! Retransmit. */
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5389 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5390 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5391 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5398 * Core handler for path ACKs
5400 * @param cls closure
5401 * @param message message
5402 * @param peer peer identity this notification is about
5404 * @return GNUNET_OK to keep the connection open,
5405 * GNUNET_SYSERR to close it (signal serious error)
5408 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5409 const struct GNUNET_MessageHeader *message)
5411 struct GNUNET_MESH_ConnectionACK *msg;
5412 struct MeshPeerPath *p;
5413 struct MeshConnection *c;
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5417 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5419 GNUNET_h2s (&msg->cid));
5420 c = connection_get (&msg->cid);
5423 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5432 /* Add path to peers? */
5436 path_add_to_peers (p, GNUNET_YES);
5442 connection_change_state (c, MESH_CONNECTION_READY);
5443 connection_reset_timeout (c, GNUNET_NO);
5444 if (MESH_TUNNEL_READY != c->t->state)
5445 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5446 tunnel_send_queued_data (c->t, GNUNET_YES);
5448 /* Message for us? */
5449 if (connection_is_terminal (c, GNUNET_NO))
5451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5452 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5454 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5455 c->t->peer->dhtget = NULL;
5457 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5462 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5468 * Core handler for notifications of broken paths
5470 * @param cls Closure (unused).
5471 * @param peer Peer identity of sending neighbor.
5472 * @param message Message.
5474 * @return GNUNET_OK to keep the connection open,
5475 * GNUNET_SYSERR to close it (signal serious error)
5478 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5479 const struct GNUNET_MessageHeader *message)
5481 struct GNUNET_MESH_ConnectionBroken *msg;
5482 struct MeshConnection *c;
5484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5485 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5486 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5488 GNUNET_i2s (&msg->peer1));
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5490 GNUNET_i2s (&msg->peer2));
5491 c = connection_get (&msg->cid);
5494 GNUNET_break_op (0);
5497 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5498 GNUNET_PEER_search (&msg->peer2));
5505 * Core handler for tunnel destruction
5507 * @param cls Closure (unused).
5508 * @param peer Peer identity of sending neighbor.
5509 * @param message Message.
5511 * @return GNUNET_OK to keep the connection open,
5512 * GNUNET_SYSERR to close it (signal serious error)
5515 handle_mesh_connection_destroy (void *cls,
5516 const struct GNUNET_PeerIdentity *peer,
5517 const struct GNUNET_MessageHeader *message)
5519 struct GNUNET_MESH_ConnectionDestroy *msg;
5520 struct MeshConnection *c;
5524 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5526 "Got a CONNECTION DESTROY message from %s\n",
5528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5529 " for connection %s\n",
5530 GNUNET_h2s (&msg->cid));
5531 c = connection_get (&msg->cid);
5534 /* Probably already got the message from another path,
5535 * destroyed the tunnel and retransmitted to children.
5538 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5542 id = GNUNET_PEER_search (peer);
5543 if (id == connection_get_prev_hop (c)->id)
5545 else if (id == connection_get_next_hop (c)->id)
5549 GNUNET_break_op (0);
5552 send_prebuilt_message_connection (message, c, NULL, fwd);
5553 c->destroy = GNUNET_YES;
5560 * Handler for channel create messages.
5562 * @param t Tunnel this channel is to be created in.
5563 * @param msg Message.
5564 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5567 handle_channel_create (struct MeshTunnel2 *t,
5568 struct GNUNET_MESH_ChannelCreate *msg,
5571 MESH_ChannelNumber chid;
5572 struct MeshChannel *ch;
5573 struct MeshClient *c;
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5577 /* Check message size */
5578 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5580 GNUNET_break_op (0);
5584 /* Check if channel exists */
5585 chid = ntohl (msg->chid);
5586 ch = channel_get (t, chid);
5589 /* Probably a retransmission, safe to ignore */
5590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5591 if (NULL != ch->dest)
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5594 channel_send_ack (ch, !fwd);
5600 /* Create channel */
5601 ch = channel_new (t, NULL, 0);
5602 channel_set_options (ch, ntohl (msg->opt));
5605 /* Find a destination client */
5606 port = ntohl (msg->port);
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5608 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5611 /* TODO send reject */
5612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5617 channel_add_client (ch, c);
5618 if (GNUNET_YES == ch->reliable)
5619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5621 send_local_channel_create (ch);
5622 channel_send_ack (ch, !fwd);
5623 send_local_ack (ch, !fwd);
5628 * Handler for channel ack messages.
5630 * @param t Tunnel this channel is to be created in.
5631 * @param msg Message.
5632 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5635 handle_channel_ack (struct MeshTunnel2 *t,
5636 struct GNUNET_MESH_ChannelManage *msg,
5639 MESH_ChannelNumber chid;
5640 struct MeshChannel *ch;
5642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5643 /* Check message size */
5644 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5646 GNUNET_break_op (0);
5650 /* Check if channel exists */
5651 chid = ntohl (msg->chid);
5652 ch = channel_get (t, chid);
5655 GNUNET_break_op (0);
5656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5660 channel_confirm (ch, !fwd);
5665 * Handler for channel destroy messages.
5667 * @param t Tunnel this channel is to be destroyed of.
5668 * @param msg Message.
5669 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5672 handle_channel_destroy (struct MeshTunnel2 *t,
5673 struct GNUNET_MESH_ChannelManage *msg,
5676 MESH_ChannelNumber chid;
5677 struct MeshChannel *ch;
5679 /* Check message size */
5680 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5682 GNUNET_break_op (0);
5686 /* Check if channel exists */
5687 chid = ntohl (msg->chid);
5688 ch = channel_get (t, chid);
5691 /* Probably a retransmission, safe to ignore */
5695 send_local_channel_destroy (ch, fwd);
5696 channel_destroy (ch);
5701 handle_decrypted (struct MeshTunnel2 *t,
5702 const struct GNUNET_MessageHeader *msgh,
5705 switch (ntohs (msgh->type))
5707 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5708 /* Don't send hop ACK, wait for client to ACK */
5709 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5712 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5713 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5716 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5717 handle_channel_create (t,
5718 (struct GNUNET_MESH_ChannelCreate *) msgh,
5722 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5723 handle_channel_ack (t,
5724 (struct GNUNET_MESH_ChannelManage *) msgh,
5728 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5729 handle_channel_destroy (t,
5730 (struct GNUNET_MESH_ChannelManage *) msgh,
5735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5736 "end-to-end message not known (%u)\n",
5737 ntohs (msgh->type));
5743 * Generic handler for mesh network encrypted traffic.
5745 * @param peer Peer identity this notification is about.
5746 * @param message Encrypted message.
5747 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5749 * @return GNUNET_OK to keep the connection open,
5750 * GNUNET_SYSERR to close it (signal serious error)
5753 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5754 const struct GNUNET_MESH_Encrypted *msg,
5757 struct MeshConnection *c;
5758 struct MeshTunnel2 *t;
5759 struct MeshPeer *neighbor;
5760 struct MeshFlowControl *fc;
5767 size = ntohs (msg->header.size);
5769 sizeof (struct GNUNET_MESH_Encrypted) +
5770 sizeof (struct GNUNET_MessageHeader))
5772 GNUNET_break_op (0);
5775 type = ntohs (msg->header.type);
5776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5778 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5780 /* Check connection */
5781 c = connection_get (&msg->cid);
5784 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5789 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5791 /* Check if origin is as expected */
5792 neighbor = connection_get_hop (c, fwd);
5793 if (peer_get (peer)->id != neighbor->id)
5795 GNUNET_break_op (0);
5800 pid = ntohl (msg->pid);
5801 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5803 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5805 "WARNING Received PID %u, (prev %u), ACK %u\n",
5806 pid, fc->last_pid_recv, fc->last_ack_sent);
5809 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5811 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5813 " Pid %u not expected (%u+), dropping!\n",
5814 pid, fc->last_pid_recv + 1);
5817 if (MESH_CONNECTION_SENT == c->state)
5818 connection_change_state (c, MESH_CONNECTION_READY);
5819 connection_reset_timeout (c, fwd);
5820 fc->last_pid_recv = pid;
5822 /* Is this message for us? */
5823 if (connection_is_terminal (c, fwd))
5825 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5827 struct GNUNET_MessageHeader *msgh;
5830 /* TODO signature verification */
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5832 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5834 fc->last_pid_recv = pid;
5835 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5839 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
5840 handle_decrypted (t, msgh, fwd);
5841 off += ntohs (msgh->size);
5843 send_ack (c, NULL, fwd);
5847 /* Message not for us: forward to next hop */
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5849 ttl = ntohl (msg->ttl);
5850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5853 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5855 send_ack (c, NULL, fwd);
5858 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5860 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5867 * Core handler for mesh network traffic going orig->dest.
5869 * @param cls Closure (unused).
5870 * @param message Message received.
5871 * @param peer Peer who sent the message.
5873 * @return GNUNET_OK to keep the connection open,
5874 * GNUNET_SYSERR to close it (signal serious error)
5877 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5878 const struct GNUNET_MessageHeader *message)
5880 return handle_mesh_encrypted (peer,
5881 (struct GNUNET_MESH_Encrypted *)message,
5886 * Core handler for mesh network traffic going dest->orig.
5888 * @param cls Closure (unused).
5889 * @param message Message received.
5890 * @param peer Peer who sent the message.
5892 * @return GNUNET_OK to keep the connection open,
5893 * GNUNET_SYSERR to close it (signal serious error)
5896 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5897 const struct GNUNET_MessageHeader *message)
5899 return handle_mesh_encrypted (peer,
5900 (struct GNUNET_MESH_Encrypted *)message,
5906 * Core handler for mesh network traffic point-to-point acks.
5908 * @param cls closure
5909 * @param message message
5910 * @param peer peer identity this notification is about
5912 * @return GNUNET_OK to keep the connection open,
5913 * GNUNET_SYSERR to close it (signal serious error)
5916 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5917 const struct GNUNET_MessageHeader *message)
5919 struct GNUNET_MESH_ACK *msg;
5920 struct MeshConnection *c;
5921 struct MeshFlowControl *fc;
5925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5928 msg = (struct GNUNET_MESH_ACK *) message;
5930 c = connection_get (&msg->cid);
5934 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5939 /* Is this a forward or backward ACK? */
5940 id = GNUNET_PEER_search (peer);
5941 if (connection_get_next_hop (c)->id == id)
5943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5946 else if (connection_get_prev_hop (c)->id == id)
5948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5953 GNUNET_break_op (0);
5957 ack = ntohl (msg->ack);
5958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5960 /* Cancel polling if the ACK is bigger than before. */
5961 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5962 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5965 GNUNET_SCHEDULER_cancel (fc->poll_task);
5966 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5967 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5970 fc->last_ack_recv = ack;
5971 connection_unlock_queue (c, fc == &c->fwd_fc);
5978 * Core handler for mesh network traffic point-to-point ack polls.
5980 * @param cls closure
5981 * @param message message
5982 * @param peer peer identity this notification is about
5984 * @return GNUNET_OK to keep the connection open,
5985 * GNUNET_SYSERR to close it (signal serious error)
5988 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5989 const struct GNUNET_MessageHeader *message)
5991 struct GNUNET_MESH_Poll *msg;
5992 struct MeshConnection *c;
5993 struct MeshFlowControl *fc;
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
6002 msg = (struct GNUNET_MESH_Poll *) message;
6004 c = connection_get (&msg->cid);
6008 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
6010 GNUNET_break_op (0);
6014 /* Is this a forward or backward ACK?
6015 * Note: a poll should never be needed in a loopback case,
6016 * since there is no possiblility of packet loss there, so
6017 * this way of discerining FWD/BCK should not be a problem.
6019 id = GNUNET_PEER_search (peer);
6020 if (connection_get_next_hop (c)->id == id)
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6025 else if (connection_get_prev_hop (c)->id == id)
6027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6032 GNUNET_break_op (0);
6036 pid = ntohl (msg->pid);
6037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
6038 pid, fc->last_pid_recv);
6039 fc->last_pid_recv = pid;
6040 fwd = fc == &c->fwd_fc;
6041 send_ack (c, NULL, fwd);
6048 * Core handler for mesh keepalives.
6050 * @param cls closure
6051 * @param message message
6052 * @param peer peer identity this notification is about
6053 * @return GNUNET_OK to keep the connection open,
6054 * GNUNET_SYSERR to close it (signal serious error)
6056 * TODO: Check who we got this from, to validate route.
6059 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6060 const struct GNUNET_MessageHeader *message)
6062 struct GNUNET_MESH_ConnectionKeepAlive *msg;
6063 struct MeshConnection *c;
6064 struct MeshPeer *neighbor;
6067 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
6068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6071 c = connection_get (&msg->cid);
6074 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
6079 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
6080 GNUNET_YES : GNUNET_NO;
6082 /* Check if origin is as expected */
6083 neighbor = connection_get_hop (c, fwd);
6084 if (peer_get (peer)->id != neighbor->id)
6086 GNUNET_break_op (0);
6090 connection_change_state (c, MESH_CONNECTION_READY);
6091 connection_reset_timeout (c, fwd);
6093 if (connection_is_terminal (c, fwd))
6096 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6097 send_prebuilt_message_connection (message, c, NULL, fwd);
6105 * Functions to handle messages from core
6107 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6108 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
6110 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
6111 sizeof (struct GNUNET_MESH_ConnectionACK)},
6112 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
6113 sizeof (struct GNUNET_MESH_ConnectionBroken)},
6114 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
6115 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
6116 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
6117 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6118 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
6119 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6120 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6121 sizeof (struct GNUNET_MESH_ACK)},
6122 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6123 sizeof (struct GNUNET_MESH_Poll)},
6124 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
6125 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
6131 * Function to process paths received for a new peer addition. The recorded
6132 * paths form the initial tunnel, which can be optimized later.
6133 * Called on each result obtained for the DHT search.
6135 * @param cls closure
6136 * @param exp when will this value expire
6137 * @param key key of the result
6138 * @param get_path path of the get request
6139 * @param get_path_length lenght of get_path
6140 * @param put_path path of the put request
6141 * @param put_path_length length of the put_path
6142 * @param type type of the result
6143 * @param size number of bytes in data
6144 * @param data pointer to the result data
6147 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6148 const struct GNUNET_HashCode * key,
6149 const struct GNUNET_PeerIdentity *get_path,
6150 unsigned int get_path_length,
6151 const struct GNUNET_PeerIdentity *put_path,
6152 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6153 size_t size, const void *data)
6155 struct MeshPeer *peer = cls;
6156 struct MeshPeerPath *p;
6157 struct MeshConnection *c;
6158 struct GNUNET_PeerIdentity pi;
6161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6162 GNUNET_PEER_resolve (peer->id, &pi);
6163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6165 p = path_build_from_dht (get_path, get_path_length,
6166 put_path, put_path_length);
6167 path_add_to_peers (p, GNUNET_NO);
6170 /* Count connections */
6171 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6173 /* If we already have 3 (or more (?!)) connections, it's enough */
6177 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6180 peer_connect (peer);
6186 /******************************************************************************/
6187 /********************* MESH LOCAL HANDLES **************************/
6188 /******************************************************************************/
6192 * Handler for client connection.
6194 * @param cls Closure (unused).
6195 * @param client Client handler.
6198 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6200 struct MeshClient *c;
6204 c = GNUNET_malloc (sizeof (struct MeshClient));
6206 c->id = next_client_id++; /* overflow not important: just for debug */
6207 GNUNET_SERVER_client_keep (client);
6208 GNUNET_SERVER_client_set_user_context (client, c);
6209 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6214 * Handler for client disconnection
6216 * @param cls closure
6217 * @param client identification of the client; NULL
6218 * for the last call when the server is destroyed
6221 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6223 struct MeshClient *c;
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6232 c = client_get (client);
6235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6237 GNUNET_SERVER_client_drop (c->handle);
6238 c->shutting_down = GNUNET_YES;
6239 if (NULL != c->own_channels)
6241 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6242 &channel_destroy_iterator, c);
6243 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6246 if (NULL != c->incoming_channels)
6248 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6249 &channel_destroy_iterator, c);
6250 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6253 if (NULL != c->ports)
6255 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6256 &client_release_ports, c);
6257 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6259 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6260 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6274 * Handler for new clients
6276 * @param cls closure
6277 * @param client identification of the client
6278 * @param message the actual message, which includes messages the client wants
6281 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6282 const struct GNUNET_MessageHeader *message)
6284 struct GNUNET_MESH_ClientConnect *cc_msg;
6285 struct MeshClient *c;
6290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6292 /* Check data sanity */
6293 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6294 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6295 if (0 != (size % sizeof (uint32_t)))
6298 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6301 size /= sizeof (uint32_t);
6303 /* Initialize new client structure */
6304 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6311 p = (uint32_t *) &cc_msg[1];
6312 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6313 for (i = 0; i < size; i++)
6316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6318 /* store in client's hashmap */
6319 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6320 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6321 /* store in global hashmap */
6322 /* FIXME only allow one client to have the port open,
6323 * have a backup hashmap with waiting clients */
6324 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6325 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6329 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6330 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6331 GNUNET_SERVER_notification_context_add (nc, client);
6332 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6340 * Handler for requests of new tunnels
6342 * @param cls Closure.
6343 * @param client Identification of the client.
6344 * @param message The actual message.
6347 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6348 const struct GNUNET_MessageHeader *message)
6350 struct GNUNET_MESH_ChannelMessage *msg;
6351 struct MeshPeer *peer;
6352 struct MeshTunnel2 *t;
6353 struct MeshChannel *ch;
6354 struct MeshClient *c;
6355 MESH_ChannelNumber chid;
6357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6359 /* Sanity check for client registration */
6360 if (NULL == (c = client_get (client)))
6363 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6368 /* Message size sanity check */
6369 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6376 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6378 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6379 chid = ntohl (msg->channel_id);
6381 /* Sanity check for duplicate channel IDs */
6382 if (NULL != channel_get_by_local_id (c, chid))
6385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6389 peer = peer_get (&msg->peer);
6390 if (NULL == peer->tunnel)
6392 peer->tunnel = tunnel_new ();
6393 peer->tunnel->peer = peer;
6397 /* Create channel */
6398 ch = channel_new (t, c, chid);
6402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6405 ch->port = ntohl (msg->port);
6406 channel_set_options (ch, ntohl (msg->opt));
6408 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6409 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6410 ch->root_rel->ch = ch;
6411 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6414 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6415 peer_connect (peer);
6417 /* Send create channel */
6419 struct GNUNET_MESH_ChannelCreate msgcc;
6421 msgcc.header.size = htons (sizeof (msgcc));
6422 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6423 msgcc.chid = htonl (ch->gid);
6424 msgcc.port = msg->port;
6425 msgcc.opt = msg->opt;
6427 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6436 * Handler for requests of deleting tunnels
6438 * @param cls closure
6439 * @param client identification of the client
6440 * @param message the actual message
6443 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6444 const struct GNUNET_MessageHeader *message)
6446 struct GNUNET_MESH_ChannelMessage *msg;
6447 struct MeshClient *c;
6448 struct MeshChannel *ch;
6449 struct MeshTunnel2 *t;
6450 MESH_ChannelNumber chid;
6452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6453 "Got a DESTROY CHANNEL from client!\n");
6455 /* Sanity check for client registration */
6456 if (NULL == (c = client_get (client)))
6459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6464 /* Message sanity check */
6465 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6472 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6474 /* Retrieve tunnel */
6475 chid = ntohl (msg->channel_id);
6476 ch = channel_get_by_local_id (c, chid);
6479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6485 /* Cleanup after the tunnel */
6486 client_delete_channel (c, ch);
6487 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6491 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6498 " channel %X client %p (%p, %p)\n",
6499 chid, c, ch->root, ch->dest);
6504 channel_destroy (ch);
6505 tunnel_destroy_if_empty (t);
6507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6513 * Handler for client traffic
6515 * @param cls closure
6516 * @param client identification of the client
6517 * @param message the actual message
6520 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6521 const struct GNUNET_MessageHeader *message)
6523 struct GNUNET_MESH_LocalData *msg;
6524 struct MeshClient *c;
6525 struct MeshChannel *ch;
6526 struct MeshChannelReliability *rel;
6527 MESH_ChannelNumber chid;
6531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6532 "Got data from a client!\n");
6534 /* Sanity check for client registration */
6535 if (NULL == (c = client_get (client)))
6538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6543 msg = (struct GNUNET_MESH_LocalData *) message;
6545 /* Sanity check for message size */
6546 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6547 if (size < sizeof (struct GNUNET_MessageHeader))
6550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6554 /* Channel exists? */
6555 chid = ntohl (msg->id);
6556 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6557 ch = channel_get_by_local_id (c, chid);
6561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6565 /* Is the client in the channel? */
6568 ch->root->handle == client)
6572 ch->dest->handle == client) ) )
6575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6579 rel = fwd ? ch->root_rel : ch->dest_rel;
6580 rel->client_ready = GNUNET_NO;
6582 /* Ok, everything is correct, send the message. */
6584 struct GNUNET_MESH_Data *payload;
6585 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6586 unsigned char cbuf[p2p_size];
6588 payload = (struct GNUNET_MESH_Data *) cbuf;
6589 payload->mid = htonl (rel->mid_send);
6591 memcpy (&payload[1], &msg[1], size);
6592 payload->header.size = htons (p2p_size);
6593 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6594 payload->chid = htonl (ch->gid);
6595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6596 send_prebuilt_message_channel (&payload->header, ch, fwd);
6598 if (GNUNET_YES == ch->reliable)
6599 channel_save_copy (ch, &payload->header, fwd);
6601 if (tunnel_get_buffer (ch->t, fwd) > 0)
6602 send_local_ack (ch, fwd);
6603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6611 * Handler for client's ACKs for payload traffic.
6613 * @param cls Closure (unused).
6614 * @param client Identification of the client.
6615 * @param message The actual message.
6618 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6619 const struct GNUNET_MessageHeader *message)
6621 struct GNUNET_MESH_LocalAck *msg;
6622 struct MeshChannelReliability *rel;
6623 struct MeshChannel *ch;
6624 struct MeshClient *c;
6625 MESH_ChannelNumber chid;
6628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6630 /* Sanity check for client registration */
6631 if (NULL == (c = client_get (client)))
6634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6639 msg = (struct GNUNET_MESH_LocalAck *) message;
6641 /* Channel exists? */
6642 chid = ntohl (msg->channel_id);
6643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6644 ch = channel_get_by_local_id (c, chid);
6645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6655 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6656 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6657 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6658 rel = fwd ? ch->dest_rel : ch->root_rel;
6660 rel->client_ready = GNUNET_YES;
6661 channel_send_client_buffered_data (ch, c, fwd);
6662 send_ack (NULL, ch, fwd);
6664 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6671 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6673 * @param cls Closure (client handle).
6674 * @param key Key (hashed tunnel ID, unused).
6675 * @param value Tunnel info.
6677 * @return GNUNET_YES, to keep iterating.
6680 // monitor_all_tunnels_iterator (void *cls,
6681 // const struct GNUNET_HashCode * key,
6684 // struct GNUNET_SERVER_Client *client = cls;
6685 // struct MeshChannel *ch = value;
6686 // struct GNUNET_MESH_LocalMonitor *msg;
6688 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6689 // msg->channel_id = htonl (ch->gid);
6690 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6691 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6693 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6694 // "* sending info about tunnel %s\n",
6695 // GNUNET_i2s (&msg->owner));
6697 // GNUNET_SERVER_notification_context_unicast (nc, client,
6698 // &msg->header, GNUNET_NO);
6699 // return GNUNET_YES;
6704 * Handler for client's MONITOR request.
6706 * @param cls Closure (unused).
6707 * @param client Identification of the client.
6708 * @param message The actual message.
6711 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6712 const struct GNUNET_MessageHeader *message)
6714 struct MeshClient *c;
6716 /* Sanity check for client registration */
6717 if (NULL == (c = client_get (client)))
6720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6724 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6725 "Received get tunnels request from client %u\n",
6727 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6728 // monitor_all_tunnels_iterator,
6730 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6731 "Get tunnels request from client %u completed\n",
6733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6738 * Handler for client's MONITOR_TUNNEL request.
6740 * @param cls Closure (unused).
6741 * @param client Identification of the client.
6742 * @param message The actual message.
6745 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6746 const struct GNUNET_MessageHeader *message)
6748 const struct GNUNET_MESH_LocalMonitor *msg;
6749 struct GNUNET_MESH_LocalMonitor *resp;
6750 struct MeshClient *c;
6751 struct MeshChannel *ch;
6753 /* Sanity check for client registration */
6754 if (NULL == (c = client_get (client)))
6757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6761 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6762 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6763 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6766 ntohl (msg->channel_id));
6767 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6771 /* We don't know the tunnel */
6772 struct GNUNET_MESH_LocalMonitor warn;
6775 GNUNET_SERVER_notification_context_unicast (nc, client,
6778 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6782 /* Initialize context */
6783 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6785 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6786 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6787 &resp->header, GNUNET_NO);
6790 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6791 "Monitor tunnel request from client %u completed\n",
6793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6798 * Functions to handle messages from clients
6800 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6801 {&handle_local_new_client, NULL,
6802 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6803 {&handle_local_channel_create, NULL,
6804 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6805 sizeof (struct GNUNET_MESH_ChannelMessage)},
6806 {&handle_local_channel_destroy, NULL,
6807 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6808 sizeof (struct GNUNET_MESH_ChannelMessage)},
6809 {&handle_local_data, NULL,
6810 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6811 {&handle_local_ack, NULL,
6812 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6813 sizeof (struct GNUNET_MESH_LocalAck)},
6814 {&handle_local_get_tunnels, NULL,
6815 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6816 sizeof (struct GNUNET_MessageHeader)},
6817 {&handle_local_show_tunnel, NULL,
6818 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6819 sizeof (struct GNUNET_MESH_LocalMonitor)},
6825 * Method called whenever a given peer connects.
6827 * @param cls closure
6828 * @param peer peer identity this notification is about
6831 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6833 struct MeshPeer *pi;
6834 struct MeshPeerPath *path;
6836 DEBUG_CONN ("Peer connected\n");
6837 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6838 pi = peer_get (peer);
6841 DEBUG_CONN (" (self)\n");
6842 path = path_new (1);
6846 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6847 path = path_new (2);
6848 path->peers[1] = pi->id;
6849 GNUNET_PEER_change_rc (pi->id, 1);
6850 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6852 path->peers[0] = myid;
6853 GNUNET_PEER_change_rc (myid, 1);
6854 peer_add_path (pi, path, GNUNET_YES);
6856 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6862 * Method called whenever a peer disconnects.
6864 * @param cls closure
6865 * @param peer peer identity this notification is about
6868 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6870 struct MeshPeer *pi;
6872 DEBUG_CONN ("Peer disconnected\n");
6873 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6880 peer_remove_path (pi, myid, pi->id);
6882 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6885 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6886 pi->connections = NULL;
6889 DEBUG_CONN (" (self)\n");
6891 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6898 * Install server (service) handlers and start listening to clients.
6903 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6904 GNUNET_SERVER_connect_notify (server_handle,
6905 &handle_local_client_connect, NULL);
6906 GNUNET_SERVER_disconnect_notify (server_handle,
6907 &handle_local_client_disconnect, NULL);
6908 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6910 clients_head = NULL;
6911 clients_tail = NULL;
6913 GNUNET_SERVER_resume (server_handle);
6918 * To be called on core init/fail.
6920 * @param cls Closure (config)
6921 * @param identity the public identity of this peer
6924 core_init (void *cls,
6925 const struct GNUNET_PeerIdentity *identity)
6927 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6931 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6934 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6936 GNUNET_i2s (identity));
6937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6939 GNUNET_i2s (&my_full_id));
6940 GNUNET_CORE_disconnect (core_handle);
6941 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6942 NULL, /* Closure passed to MESH functions */
6943 &core_init, /* Call core_init once connected */
6944 &core_connect, /* Handle connects */
6945 &core_disconnect, /* remove peers on disconnects */
6946 NULL, /* Don't notify about all incoming messages */
6947 GNUNET_NO, /* For header only in notification */
6948 NULL, /* Don't notify about all outbound messages */
6949 GNUNET_NO, /* For header-only out notification */
6950 core_handlers); /* Register these handlers */
6959 /******************************************************************************/
6960 /************************ MAIN FUNCTIONS ****************************/
6961 /******************************************************************************/
6964 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6966 * @param cls closure
6967 * @param key current key code
6968 * @param value value in the hash map
6969 * @return GNUNET_YES if we should continue to iterate,
6973 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6975 struct MeshPeer *p = value;
6976 struct MeshTunnel2 *t = p->tunnel;
6985 * Task run during shutdown.
6991 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6995 if (core_handle != NULL)
6997 GNUNET_CORE_disconnect (core_handle);
7000 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
7001 if (dht_handle != NULL)
7003 GNUNET_DHT_disconnect (dht_handle);
7008 GNUNET_SERVER_notification_context_destroy (nc);
7011 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7013 GNUNET_SCHEDULER_cancel (announce_id_task);
7014 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7021 * Process mesh requests.
7023 * @param cls closure
7024 * @param server the initialized server
7025 * @param c configuration to use
7028 run (void *cls, struct GNUNET_SERVER_Handle *server,
7029 const struct GNUNET_CONFIGURATION_Handle *c)
7032 struct GNUNET_CRYPTO_EccPrivateKey *pk;
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7035 server_handle = server;
7036 GNUNET_SERVER_suspend (server_handle);
7039 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
7042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7044 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7045 "mesh", "peer/privatekey");
7046 GNUNET_SCHEDULER_shutdown ();
7051 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
7052 &refresh_connection_time))
7054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7056 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7057 "mesh", "refresh path time");
7058 GNUNET_SCHEDULER_shutdown ();
7063 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7068 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7069 "mesh", "id announce time");
7070 GNUNET_SCHEDULER_shutdown ();
7075 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7078 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7080 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7081 "mesh", "connect timeout");
7082 GNUNET_SCHEDULER_shutdown ();
7087 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7092 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7093 "mesh", "max msgs queue");
7094 GNUNET_SCHEDULER_shutdown ();
7099 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
7102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7104 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7105 "mesh", "max tunnels");
7106 GNUNET_SCHEDULER_shutdown ();
7111 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7116 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7117 "mesh", "default ttl", 64);
7122 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7126 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7127 "mesh", "max peers", 1000);
7132 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
7139 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7140 "Mesh is running with drop mode enabled. "
7141 "This is NOT a good idea! "
7142 "Remove the DROP_PERCENT option from your configuration.\n");
7146 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7147 &dht_replication_level))
7149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7151 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7152 "mesh", "dht replication level", 3);
7153 dht_replication_level = 3;
7156 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7157 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7158 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7160 dht_handle = GNUNET_DHT_connect (c, 64);
7161 if (NULL == dht_handle)
7165 stats = GNUNET_STATISTICS_create ("mesh", c);
7167 /* Scheduled the task to clean up when shutdown is called */
7168 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7170 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7171 GNUNET_free (keyfile);
7172 GNUNET_assert (NULL != pk);
7173 my_private_key = pk;
7174 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7175 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7176 &my_full_id.hashPubKey);
7177 myid = GNUNET_PEER_intern (&my_full_id);
7178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7179 "Mesh for peer [%s] starting\n",
7180 GNUNET_i2s(&my_full_id));
7182 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7183 NULL, /* Closure passed to MESH functions */
7184 &core_init, /* Call core_init once connected */
7185 &core_connect, /* Handle connects */
7186 &core_disconnect, /* remove peers on disconnects */
7187 NULL, /* Don't notify about all incoming messages */
7188 GNUNET_NO, /* For header only in notification */
7189 NULL, /* Don't notify about all outbound messages */
7190 GNUNET_NO, /* For header-only out notification */
7191 core_handlers); /* Register these handlers */
7192 if (NULL == core_handle)
7195 GNUNET_SCHEDULER_shutdown ();
7198 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7204 * The main function for the mesh service.
7206 * @param argc number of arguments from the command line
7207 * @param argv command line arguments
7208 * @return 0 ok, 1 on error
7211 main (int argc, char *const *argv)
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7217 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7219 ret = (GNUNET_OK == r) ? 0 : 1;
7220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");