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 */
404 struct MeshChannelReliability
407 * Channel this is about.
409 struct MeshChannel *ch;
412 * DLL of messages sent and not yet ACK'd.
414 struct MeshReliableMessage *head_sent;
415 struct MeshReliableMessage *tail_sent;
418 * Messages pending to send.
423 * DLL of messages received out of order.
425 struct MeshReliableMessage *head_recv;
426 struct MeshReliableMessage *tail_recv;
434 * Can we send data to the client?
439 * Task to resend/poll in case no ACK is received.
441 GNUNET_SCHEDULER_TaskIdentifier retry_task;
444 * Counter for exponential backoff.
446 struct GNUNET_TIME_Relative retry_timer;
449 * How long does it usually take to get an ACK.
451 struct GNUNET_TIME_Relative expected_delay;
456 * Struct containing all information regarding a channel to a remote client.
461 * Tunnel this channel is in.
463 struct MeshTunnel2 *t;
466 * Double linked list.
468 struct MeshChannel *next;
469 struct MeshChannel *prev;
472 * Destination port of the channel.
477 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
479 MESH_ChannelNumber gid;
482 * Local tunnel number for root (owner) client.
483 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
485 MESH_ChannelNumber lid_root;
488 * Local tunnel number for local destination clients (incoming number)
489 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
491 MESH_ChannelNumber lid_dest;
496 enum MeshChannelState state;
499 * Next MID to use for fwd traffic.
501 uint32_t mid_send_fwd;
504 * Next MID expected for fwd traffic.
506 uint32_t mid_recv_fwd;
509 * Next MID to use for bck traffic.
511 uint32_t mid_send_bck;
514 * Next MID expected for bck traffic.
516 uint32_t mid_recv_bck;
519 * Is the tunnel bufferless (minimum latency)?
524 * Is the tunnel reliable?
529 * Last time the channel was used
531 struct GNUNET_TIME_Absolute timestamp;
534 * Client owner of the tunnel, if any
536 struct MeshClient *root;
539 * Client destination of the tunnel, if any.
541 struct MeshClient *dest;
544 * Flag to signal the destruction of the channel.
545 * If this is set GNUNET_YES the channel will be destroyed
546 * when the queue is empty.
551 * Total messages pending for this channel, payload or not.
553 unsigned int pending_messages;
557 * Only present (non-NULL) at the owner of a tunnel.
559 struct MeshChannelReliability *fwd_rel;
563 * Only present (non-NULL) at the destination of a tunnel.
565 struct MeshChannelReliability *bck_rel;
568 * Is the root prevented from sending more data? (We "owe" him an ACK).
573 * Is the dest prevented from sending more data? (We "owe" him an ACK).
580 * Struct containing all information regarding a connection to a peer.
582 struct MeshConnection
587 struct MeshConnection *next;
588 struct MeshConnection *prev;
591 * Tunnel this connection is part of.
593 struct MeshTunnel2 *t;
596 * Flow control information for traffic fwd.
598 struct MeshFlowControl fwd_fc;
601 * Flow control information for traffic bck.
603 struct MeshFlowControl bck_fc;
611 * State of the connection.
613 enum MeshConnectionState state;
616 * Path being used for the tunnel.
618 struct MeshPeerPath *path;
621 * Position of the local peer in the path.
623 unsigned int own_pos;
626 * Task to keep the used paths alive at the owner,
627 * time tunnel out on all the other peers.
629 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
632 * Task to keep the used paths alive at the destination,
633 * time tunnel out on all the other peers.
635 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
638 * Pending message count.
640 int pending_messages;
643 * Destroy flag: if true, destroy on last message.
650 * Struct used to queue messages in a tunnel.
652 struct MeshTunnelQueue
657 struct MeshTunnelQueue *next;
658 struct MeshTunnelQueue *prev;
663 struct MeshChannel *ch;
668 /* struct GNUNET_MessageHeader *msg; */
673 * Struct containing all information regarding a tunnel to a peer.
678 * Endpoint of the tunnel.
680 struct MeshPeer *peer;
685 struct GNUNET_HashCode id;
688 * State of the tunnel.
690 enum MeshTunnelState state;
693 * Local peer ephemeral private key
695 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
698 * Local peer ephemeral public key
700 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
703 * Remote peer's public key.
705 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
708 * Encryption ("our") key.
710 struct GNUNET_CRYPTO_AesSessionKey e_key;
713 * Decryption ("their") key.
715 struct GNUNET_CRYPTO_AesSessionKey d_key;
718 * Paths that are actively used to reach the destination peer.
720 struct MeshConnection *connection_head;
721 struct MeshConnection *connection_tail;
724 * Next connection number.
729 * Channels inside this tunnel.
731 struct MeshChannel *channel_head;
732 struct MeshChannel *channel_tail;
735 * Channel ID for the next created channel.
737 MESH_ChannelNumber next_chid;
740 * Channel ID for the next incoming channel.
742 MESH_ChannelNumber next_local_chid;
745 * Pending message count.
747 int pending_messages;
750 * Destroy flag: if true, destroy on last message.
755 * Queued messages, to transmit once tunnel gets connected.
757 struct MeshTunnelQueue *tq_head;
758 struct MeshTunnelQueue *tq_tail;
764 * Struct containing information about a client of the service
766 * TODO: add a list of 'waiting' ports
773 struct MeshClient *next;
778 struct MeshClient *prev;
781 * Tunnels that belong to this client, indexed by local id
783 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
786 * Tunnels this client has accepted, indexed by incoming local id
788 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
791 * Handle to communicate with the client
793 struct GNUNET_SERVER_Client *handle;
796 * Ports that this client has declared interest in.
797 * Indexed by port, contains *Client.
799 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
802 * Whether the client is active or shutting down (don't send confirmations
803 * to a client that is shutting down.
808 * ID of the client, mainly for debug messages
814 /******************************************************************************/
815 /************************ DEBUG FUNCTIONS ****************************/
816 /******************************************************************************/
820 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
821 * @param cls string to print
822 * @param success GNUNET_OK if the PUT was transmitted,
823 * GNUNET_NO on timeout,
824 * GNUNET_SYSERR on disconnect from service
825 * after the PUT message was transmitted
826 * (so we don't know if it was received or not)
831 mesh_debug (void *cls, int success)
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
841 /******************************************************************************/
842 /*********************** GLOBAL VARIABLES ****************************/
843 /******************************************************************************/
845 /************************** Configuration parameters **************************/
848 * How often to send path keepalives. Paths timeout after 4 missed.
850 static struct GNUNET_TIME_Relative refresh_connection_time;
853 * How often to PUT own ID in the DHT.
855 static struct GNUNET_TIME_Relative id_announce_time;
858 * Maximum time allowed to connect to a peer found by string.
860 static struct GNUNET_TIME_Relative connect_timeout;
863 * Default TTL for payload packets.
865 static unsigned long long default_ttl;
868 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
870 static unsigned long long dht_replication_level;
873 * How many connections are we willing to maintain.
874 * Local connections are always allowed, even if there are more connections than max.
876 static unsigned long long max_connections;
879 * How many messages *in total* are we willing to queue, divide by number of
880 * connections to get connection queue size.
882 static unsigned long long max_msgs_queue;
885 * How many peers do we want to remember?
887 static unsigned long long max_peers;
890 * Percentage of messages that will be dropped (for test purposes only).
892 static unsigned long long drop_percent;
894 /*************************** Static global variables **************************/
897 * DLL with all the clients, head.
899 static struct MeshClient *clients_head;
902 * DLL with all the clients, tail.
904 static struct MeshClient *clients_tail;
907 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
909 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
912 * Peers known, indexed by PeerIdentity (MeshPeer).
914 static struct GNUNET_CONTAINER_MultiHashMap *peers;
917 * Handle to communicate with core.
919 static struct GNUNET_CORE_Handle *core_handle;
924 static struct GNUNET_DHT_Handle *dht_handle;
927 * Handle to server lib.
929 static struct GNUNET_SERVER_Handle *server_handle;
932 * Handle to the statistics service.
934 static struct GNUNET_STATISTICS_Handle *stats;
937 * Notification context, to send messages to local clients.
939 static struct GNUNET_SERVER_NotificationContext *nc;
942 * Local peer own ID (memory efficient handle).
944 static GNUNET_PEER_Id myid;
947 * Local peer own ID (full value).
949 static struct GNUNET_PeerIdentity my_full_id;
954 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
959 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
962 * All ports clients of this peer have opened.
964 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
967 * Task to periodically announce itself in the network.
969 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
972 * Next ID to assign to a client.
974 unsigned int next_client_id;
977 /******************************************************************************/
978 /*********************** DECLARATIONS **************************/
979 /******************************************************************************/
982 * Function to process paths received for a new peer addition. The recorded
983 * paths form the initial tunnel, which can be optimized later.
984 * Called on each result obtained for the DHT search.
987 * @param exp when will this value expire
988 * @param key key of the result
989 * @param type type of the result
990 * @param size number of bytes in data
991 * @param data pointer to the result data
994 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
995 const struct GNUNET_HashCode * key,
996 const struct GNUNET_PeerIdentity *get_path,
997 unsigned int get_path_length,
998 const struct GNUNET_PeerIdentity *put_path,
999 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1000 size_t size, const void *data);
1004 * Retrieve the MeshPeer stucture associated with the peer, create one
1005 * and insert it in the appropriate structures if the peer is not known yet.
1007 * @param peer Full identity of the peer.
1009 * @return Existing or newly created peer info.
1011 static struct MeshPeer *
1012 peer_get (const struct GNUNET_PeerIdentity *peer);
1016 * Retrieve the MeshPeer stucture associated with the peer, create one
1017 * and insert it in the appropriate structures if the peer is not known yet.
1019 * @param peer Short identity of the peer.
1021 * @return Existing or newly created peer info.
1023 static struct MeshPeer *
1024 peer_get_short (const GNUNET_PEER_Id peer);
1028 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1029 * to obtain a local peer -> destination path and interning the peer ids.
1031 * @return Newly allocated and created path
1033 static struct MeshPeerPath *
1034 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1035 unsigned int get_path_length,
1036 const struct GNUNET_PeerIdentity *put_path,
1037 unsigned int put_path_length);
1041 * Adds a path to the data structs of all the peers in the path
1043 * @param p Path to process.
1044 * @param confirmed Whether we know if the path works or not.
1047 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1051 * Search for a tunnel by global ID using full PeerIdentities.
1053 * @param t Tunnel containing the channel.
1054 * @param chid Public channel number.
1056 * @return channel handler, NULL if doesn't exist
1058 static struct MeshChannel *
1059 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1063 * Change the tunnel state.
1065 * @param t Tunnel whose state to change.
1066 * @param state New state.
1069 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1073 * Notify a tunnel that a connection has broken that affects at least
1074 * some of its peers.
1076 * @param t Tunnel affected.
1077 * @param p1 Peer that got disconnected from p2.
1078 * @param p2 Peer that got disconnected from p1.
1080 * @return Short ID of the peer disconnected (either p1 or p2).
1081 * 0 if the tunnel remained unaffected.
1083 static GNUNET_PEER_Id
1084 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1085 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1088 * @brief Use the given path for the tunnel.
1089 * Update the next and prev hops (and RCs).
1090 * (Re)start the path refresh in case the tunnel is locally owned.
1092 * @param t Tunnel to update.
1093 * @param p Path to use.
1095 * @return Connection created.
1097 static struct MeshConnection *
1098 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1101 * Tunnel is empty: destroy it.
1103 * Notifies all participants (peers, cleints) about the destruction.
1105 * @param t Tunnel to destroy.
1108 tunnel_destroy_empty (struct MeshTunnel2 *t);
1111 * Destroy the tunnel.
1113 * This function does not generate any warning traffic to clients or peers.
1116 * Cancel messages belonging to this tunnel queued to neighbors.
1117 * Free any allocated resources linked to the tunnel.
1119 * @param t The tunnel to destroy.
1122 tunnel_destroy (struct MeshTunnel2 *t);
1125 * Create a connection.
1127 * @param tid Tunnel ID.
1128 * @param cid Connection ID.
1130 static struct MeshConnection *
1131 connection_new (struct GNUNET_HashCode *tid, uint32_t cid);
1134 * Connection is no longer needed: destroy it and remove from tunnel.
1136 * @param c Connection to destroy.
1139 connection_destroy (struct MeshConnection *c);
1142 * Send FWD keepalive packets for a connection.
1144 * @param cls Closure (connection for which to send the keepalive).
1145 * @param tc Notification context.
1148 connection_fwd_keepalive (void *cls,
1149 const struct GNUNET_SCHEDULER_TaskContext *tc);
1152 * Send BCK keepalive packets for a connection.
1154 * @param cls Closure (connection for which to send the keepalive).
1155 * @param tc Notification context.
1158 connection_bck_keepalive (void *cls,
1159 const struct GNUNET_SCHEDULER_TaskContext *tc);
1163 * Change the tunnel state.
1165 * @param c Connection whose state to change.
1166 * @param state New state.
1169 connection_change_state (struct MeshConnection* c,
1170 enum MeshConnectionState state);
1175 * @brief Queue and pass message to core when possible.
1177 * @param cls Closure (@c type dependant). It will be used by queue_send to
1178 * build the message to be sent if not already prebuilt.
1179 * @param type Type of the message, 0 for a raw message.
1180 * @param size Size of the message.
1181 * @param c Connection this message belongs to (cannot be NULL).
1182 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1183 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1186 queue_add (void* cls,
1189 struct MeshConnection* c,
1190 struct MeshChannel* ch,
1195 * Free a transmission that was already queued with all resources
1196 * associated to the request.
1198 * @param queue Queue handler to cancel.
1199 * @param clear_cls Is it necessary to free associated cls?
1202 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1206 * Core callback to write a queued packet to core buffer
1208 * @param cls Closure (peer info).
1209 * @param size Number of bytes available in buf.
1210 * @param buf Where the to write the message.
1212 * @return number of bytes written to buf
1215 queue_send (void *cls, size_t size, void *buf);
1219 * Dummy function to separate declarations from definitions in function list.
1222 __mesh_divider______________________________________________________________();
1226 * Get string description for tunnel state.
1228 * @param s Tunnel state.
1230 * @return String representation.
1233 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1235 static char buf[128];
1239 case MESH_TUNNEL_NEW:
1240 return "MESH_TUNNEL_NEW";
1241 case MESH_TUNNEL_SEARCHING:
1242 return "MESH_TUNNEL_SEARCHING";
1243 case MESH_TUNNEL_WAITING:
1244 return "MESH_TUNNEL_WAITING";
1245 case MESH_TUNNEL_READY:
1246 return "MESH_TUNNEL_READY";
1247 case MESH_TUNNEL_RECONNECTING:
1248 return "MESH_TUNNEL_RECONNECTING";
1251 sprintf (buf, "%u (UNKNOWN STATE)", s);
1258 * Get string description for tunnel state.
1260 * @param s Tunnel state.
1262 * @return String representation.
1265 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1269 case MESH_CONNECTION_NEW:
1270 return "MESH_CONNECTION_NEW";
1271 case MESH_CONNECTION_SENT:
1272 return "MESH_CONNECTION_SENT";
1273 case MESH_CONNECTION_READY:
1274 return "MESH_CONNECTION_READY";
1276 return "MESH_CONNECTION_STATE_ERROR";
1282 /******************************************************************************/
1283 /************************ PERIODIC FUNCTIONS ****************************/
1284 /******************************************************************************/
1287 * Periodically announce self id in the DHT
1289 * @param cls closure
1290 * @param tc task context
1293 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1295 struct PBlock block;
1297 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1299 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1303 * - Set data expiration in function of X
1304 * - Adapt X to churn
1306 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1308 block.id = my_full_id;
1309 GNUNET_DHT_put (dht_handle, /* DHT handle */
1310 &my_full_id.hashPubKey, /* Key to use */
1311 dht_replication_level, /* Replication level */
1312 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1313 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1314 sizeof (block), /* Size of the data */
1315 (const char *) &block, /* Data itself */
1316 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1317 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1318 NULL, /* Continuation */
1319 NULL); /* Continuation closure */
1321 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1325 /******************************************************************************/
1326 /****************** GENERAL HELPER FUNCTIONS ************************/
1327 /******************************************************************************/
1331 * Get the static string for a peer ID.
1335 * @return Static string for it's ID.
1338 peer2s (const struct MeshPeer *peer)
1342 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1347 * Get the previous hop in a connection
1349 * @param c Connection.
1351 * @return Previous peer in the connection.
1353 static struct MeshPeer *
1354 connection_get_prev_hop (struct MeshConnection *c)
1358 if (0 == c->own_pos || c->path->length < 2)
1359 id = c->path->peers[0];
1361 id = c->path->peers[c->own_pos - 1];
1363 return peer_get_short (id);
1368 * Get the next hop in a connection
1370 * @param c Connection.
1372 * @return Next peer in the connection.
1374 static struct MeshPeer *
1375 connection_get_next_hop (struct MeshConnection *c)
1379 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1380 id = c->path->peers[c->path->length - 1];
1382 id = c->path->peers[c->own_pos + 1];
1384 return peer_get_short (id);
1389 * Get the hop in a connection.
1391 * @param c Connection.
1392 * @param fwd Next hop?
1394 * @return Next peer in the connection.
1396 static struct MeshPeer *
1397 connection_get_hop (struct MeshConnection *c, int fwd)
1400 return connection_get_next_hop (c);
1401 return connection_get_prev_hop (c);
1405 * Check if client has registered with the service and has not disconnected
1407 * @param client the client to check
1409 * @return non-NULL if client exists in the global DLL
1411 static struct MeshClient *
1412 client_get (struct GNUNET_SERVER_Client *client)
1414 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1419 * Deletes a tunnel from a client (either owner or destination).
1421 * @param c Client whose tunnel to delete.
1422 * @param ch Channel which should be deleted.
1425 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1431 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1433 if (GNUNET_YES != res)
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1438 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1440 if (GNUNET_YES != res)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1447 * Notify the appropriate client that a new incoming channel was created.
1449 * @param ch Channel that was created.
1452 send_local_channel_create (struct MeshChannel *ch)
1454 struct GNUNET_MESH_ChannelMessage msg;
1455 struct MeshTunnel2 *t = ch->t;
1457 if (NULL == ch->dest)
1459 msg.header.size = htons (sizeof (msg));
1460 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1461 msg.channel_id = htonl (ch->lid_dest);
1462 msg.port = htonl (ch->port);
1464 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1465 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1466 msg.opt = htonl (msg.opt);
1467 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1468 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1469 &msg.header, GNUNET_NO);
1474 * Notify a client that the incoming tunnel is no longer valid.
1476 * @param ch Channel that is destroyed.
1477 * @param fwd Forward notification (owner->dest)?
1480 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1482 struct GNUNET_MESH_ChannelMessage msg;
1483 struct MeshClient *c;
1485 c = fwd ? ch->dest : ch->root;
1491 msg.header.size = htons (sizeof (msg));
1492 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1493 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1494 msg.port = htonl (0);
1495 memset (&msg.peer, 0, sizeof (msg.peer));
1496 msg.opt = htonl (0);
1497 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1498 &msg.header, GNUNET_NO);
1503 * Build a local ACK message and send it to a local client.
1505 * @param ch Channel on which to send the ACK.
1506 * @param c Client to whom send the ACK.
1507 * @param fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1510 send_local_ack (struct MeshChannel *ch,
1511 struct MeshClient *c,
1514 struct GNUNET_MESH_LocalAck msg;
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "send local %s ack on %s:%X towards %p\n",
1518 fwd ? "FWD" : "BCK", GNUNET_h2s (&ch->t->id), ch->gid, c);
1521 || ( fwd && (0 == ch->lid_root || c != ch->root))
1522 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1527 msg.header.size = htons (sizeof (msg));
1528 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1529 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1530 GNUNET_SERVER_notification_context_unicast (nc,
1535 ch->blocked_fwd = GNUNET_NO;
1537 ch->blocked_bck = GNUNET_NO;
1542 * Count established (ready) connections of a tunnel.
1544 * @param t Tunnel on which to send the message.
1546 * @return Number of connections.
1549 tunnel_count_connections (struct MeshTunnel2 *t)
1551 struct MeshConnection *c;
1554 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1561 * Pick a connection on which send the next data message.
1563 * @param t Tunnel on which to send the message.
1564 * @param fwd Is this a fwd message?
1566 * @return The connection on which to send the next message.
1568 static struct MeshConnection *
1569 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1571 struct MeshConnection *c;
1572 struct MeshConnection *best;
1573 struct MeshFlowControl *fc;
1574 unsigned int lowest_q;
1577 lowest_q = UINT_MAX;
1578 for (c = t->connection_head; NULL != c; c = c->next)
1580 if (MESH_CONNECTION_READY == c->state)
1582 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1588 if (fc->queue_n < lowest_q)
1591 lowest_q = fc->queue_n;
1600 * Get the total buffer space for a tunnel.
1603 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1605 struct MeshConnection *c;
1606 struct MeshFlowControl *fc;
1607 unsigned int buffer;
1609 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1611 if (c->state != MESH_CONNECTION_READY)
1614 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1615 buffer += fc->last_ack_recv - fc->last_pid_sent;
1623 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1624 * Encrypt data with the tunnel key.
1626 * @param t Tunnel whose key to use.
1627 * @param dst Destination for the encrypted data.
1628 * @param src Source of the plaintext.
1629 * @param size Size of the plaintext.
1630 * @param iv Initialization Vector to use.
1631 * @param fwd Is this a fwd message?
1634 tunnel_encrypt (struct MeshTunnel2 *t,
1635 void *dst, const void *src,
1636 size_t size, uint64_t iv, int fwd)
1638 memcpy (dst, src, size);
1643 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1644 * Decrypt data with the tunnel key.
1646 * @param t Tunnel whose key to use.
1647 * @param dst Destination for the plaintext.
1648 * @param src Source of the encrypted data.
1649 * @param size Size of the encrypted data.
1650 * @param iv Initialization Vector to use.
1651 * @param fwd Is this a fwd message?
1654 tunnel_decrypt (struct MeshTunnel2 *t,
1655 void *dst, const void *src,
1656 size_t size, uint64_t iv, int fwd)
1658 memcpy (dst, src, size);
1663 * Sends an already built message on a connection, properly registering
1664 * all used resources.
1666 * @param message Message to send. Function makes a copy of it.
1667 * If message is not hop-by-hop, decrements TTL of copy.
1668 * @param c Connection on which this message is transmitted.
1669 * @param ch Channel on which this message is transmitted, or NULL.
1670 * @param fwd Is this a fwd message?
1673 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1674 struct MeshConnection *c,
1675 struct MeshChannel *ch,
1682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Connection %s[%X]\n",
1683 GNUNET_h2s (&c->t->id), c->id);
1685 size = ntohs (message->size);
1686 data = GNUNET_malloc (size);
1687 memcpy (data, message, size);
1688 type = ntohs (message->type);
1692 struct GNUNET_MESH_Encrypted *emsg;
1693 struct GNUNET_MESH_ACK *amsg;
1694 struct GNUNET_MESH_Poll *pmsg;
1697 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1698 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1699 emsg = (struct GNUNET_MESH_Encrypted *) data;
1700 ttl = ntohl (emsg->ttl);
1703 GNUNET_break_op (0);
1706 emsg->tid = c->t->id;
1707 emsg->cid = htonl (c->id);
1708 emsg->ttl = htonl (ttl - 1);
1709 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1713 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1714 amsg = (struct GNUNET_MESH_ACK *) data;
1715 amsg->tid = c->t->id;
1716 amsg->cid = htonl (c->id);
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1720 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1721 pmsg = (struct GNUNET_MESH_Poll *) data;
1722 pmsg->tid = c->t->id;
1723 pmsg->cid = htonl (c->id);
1724 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1742 * Sends an already built message on a tunnel, choosing the best connection.
1744 * @param message Message to send. Function modifies it.
1745 * @param t Tunnel on which this message is transmitted.
1746 * @param ch Channel on which this message is transmitted.
1747 * @param fwd Is this a fwd message?
1750 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1751 struct MeshTunnel2 *t,
1752 struct MeshChannel *ch,
1755 struct MeshConnection *c;
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1759 GNUNET_h2s (&t->id));
1760 c = tunnel_get_connection (t, fwd);
1763 GNUNET_break (GNUNET_YES == t->destroy);
1766 type = ntohs (msg->header.type);
1769 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1770 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1771 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1772 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1773 msg->cid = htonl (c->id);
1775 msg->ttl = htonl (default_ttl);
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1779 GNUNET_MESH_DEBUG_M2S (type));
1783 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1788 * Sends an already built message on a channel, properly registering
1789 * all used resources and encrypting the message with the tunnel's key.
1791 * @param message Message to send. Function makes a copy of it.
1792 * @param ch Channel on which this message is transmitted.
1793 * @param fwd Is this a fwd message?
1796 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1797 struct MeshChannel *ch,
1800 struct GNUNET_MESH_Encrypted *msg;
1801 size_t size = ntohs (message->size);
1802 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1807 GNUNET_h2s (&ch->t->id), ch->gid);
1808 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1809 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1811 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1812 msg->header.type = htons (type);
1813 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1814 msg->iv = GNUNET_htonll (iv);
1815 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1817 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1822 * Sends a CREATE CONNECTION message for a path to a peer.
1823 * Changes the connection and tunnel states if necessary.
1825 * @param connection Connection to create.
1828 send_connection_create (struct MeshConnection *connection)
1830 struct MeshTunnel2 *t;
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1835 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1836 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1837 (connection->path->length *
1838 sizeof (struct GNUNET_PeerIdentity)),
1842 if (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state)
1843 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1844 if (MESH_CONNECTION_NEW == connection->state)
1845 connection_change_state (connection, MESH_CONNECTION_SENT);
1850 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1853 * @param connection Connection to confirm.
1854 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1857 send_connection_ack (struct MeshConnection *connection, int fwd)
1859 struct MeshTunnel2 *t;
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1864 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1865 sizeof (struct GNUNET_MESH_ConnectionACK),
1869 if (MESH_TUNNEL_NEW == t->state)
1870 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1875 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1877 * @param c Which connection to send the hop-by-hop ACK.
1878 * @param ack Value of the ACK.
1879 * @param fwd Is this fwd?
1882 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1884 struct GNUNET_MESH_ACK msg;
1886 msg.header.size = htons (sizeof (msg));
1887 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1888 msg.ack = htonl (ack);
1890 msg.cid = htonl (c->id);
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1893 "connection send %s ack %u on %s[%X]\n",
1894 fwd ? "FWD" : "BCK", ack, GNUNET_h2s (&c->t->id), c->id);
1896 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1901 * Core callback to write a pre-constructed data packet to core buffer
1903 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1904 * @param size Number of bytes available in buf.
1905 * @param buf Where the to write the message.
1907 * @return number of bytes written to buf
1910 send_core_data_raw (void *cls, size_t size, void *buf)
1912 struct GNUNET_MessageHeader *msg = cls;
1915 GNUNET_assert (NULL != msg);
1916 total_size = ntohs (msg->size);
1918 if (total_size > size)
1923 memcpy (buf, msg, total_size);
1930 * Function to send a create connection message to a peer.
1932 * @param c Connection to create.
1933 * @param size number of bytes available in buf
1934 * @param buf where the callee should write the message
1935 * @return number of bytes written to buf
1938 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1940 struct GNUNET_MESH_ConnectionCreate *msg;
1941 struct GNUNET_PeerIdentity *peer_ptr;
1942 struct MeshPeerPath *p = c->path;
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1948 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1949 p->length * sizeof (struct GNUNET_PeerIdentity);
1951 if (size < size_needed || NULL == buf)
1956 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1957 msg->header.size = htons (size_needed);
1958 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1959 msg->cid = htonl (c->id);
1960 msg->tid = c->t->id;
1962 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1963 for (i = 0; i < p->length; i++)
1965 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1975 * Creates a path ack message in buf and frees all unused resources.
1977 * @param c Connection to send an ACK on.
1978 * @param size number of bytes available in buf
1979 * @param buf where the callee should write the message
1981 * @return number of bytes written to buf
1984 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1986 struct GNUNET_MESH_ConnectionACK *msg = buf;
1987 struct MeshTunnel2 *t = c->t;
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1990 GNUNET_assert (NULL != t);
1991 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1996 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1997 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1998 msg->cid = htonl (c->id);
2001 /* TODO add signature */
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2004 return sizeof (struct GNUNET_MESH_ConnectionACK);
2009 * Destroy the peer_info and free any allocated resources linked to it
2011 * @param peer The peer_info to destroy.
2013 * @return GNUNET_OK on success
2016 peer_destroy (struct MeshPeer *peer)
2018 struct GNUNET_PeerIdentity id;
2019 struct MeshPeerPath *p;
2020 struct MeshPeerPath *nextp;
2022 GNUNET_PEER_resolve (peer->id, &id);
2023 GNUNET_PEER_change_rc (peer->id, -1);
2026 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2029 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2030 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2032 if (NULL != peer->dhtget)
2034 GNUNET_DHT_get_stop (peer->dhtget);
2036 p = peer->path_head;
2040 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2044 tunnel_destroy_empty (peer->tunnel);
2051 * Returns if peer is used (has a tunnel, is neighbor).
2053 * @peer Peer to check.
2055 * @return GNUNET_YES if peer is in use.
2058 peer_is_used (struct MeshPeer *peer)
2060 struct MeshPeerPath *p;
2062 if (NULL != peer->tunnel)
2065 for (p = peer->path_head; NULL != p; p = p->next)
2074 * Iterator over all the peers to get the oldest timestamp.
2076 * @param cls Closure (unsued).
2077 * @param key ID of the peer.
2078 * @param value Peer_Info of the peer.
2081 peer_get_oldest (void *cls,
2082 const struct GNUNET_HashCode *key,
2085 struct MeshPeer *p = value;
2086 struct GNUNET_TIME_Absolute *abs = cls;
2088 /* Don't count active peers */
2089 if (GNUNET_YES == peer_is_used (p))
2092 if (abs->abs_value_us < p->last_contact.abs_value_us)
2093 abs->abs_value_us = p->last_contact.abs_value_us;
2100 * Iterator over all the peers to remove the oldest entry.
2102 * @param cls Closure (unsued).
2103 * @param key ID of the peer.
2104 * @param value Peer_Info of the peer.
2107 peer_timeout (void *cls,
2108 const struct GNUNET_HashCode *key,
2111 struct MeshPeer *p = value;
2112 struct GNUNET_TIME_Absolute *abs = cls;
2114 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2115 GNUNET_NO == peer_is_used (p))
2125 * Delete oldest unused peer.
2128 peer_delete_oldest (void)
2130 struct GNUNET_TIME_Absolute abs;
2132 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2134 GNUNET_CONTAINER_multihashmap_iterate (peers,
2137 GNUNET_CONTAINER_multihashmap_iterate (peers,
2144 * Retrieve the MeshPeer stucture associated with the peer, create one
2145 * and insert it in the appropriate structures if the peer is not known yet.
2147 * @param peer Full identity of the peer.
2149 * @return Existing or newly created peer info.
2151 static struct MeshPeer *
2152 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2154 struct MeshPeer *peer;
2156 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2159 peer = GNUNET_new (struct MeshPeer);
2160 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2162 peer_delete_oldest ();
2164 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2166 peer->id = GNUNET_PEER_intern (peer_id);
2168 peer->last_contact = GNUNET_TIME_absolute_get();
2175 * Retrieve the MeshPeer stucture associated with the peer, create one
2176 * and insert it in the appropriate structures if the peer is not known yet.
2178 * @param peer Short identity of the peer.
2180 * @return Existing or newly created peer info.
2182 static struct MeshPeer *
2183 peer_get_short (const GNUNET_PEER_Id peer)
2185 return peer_get (GNUNET_PEER_resolve2 (peer));
2190 * Get a cost of a path for a peer considering existing tunnel connections.
2192 * @param peer Peer towards which the path is considered.
2193 * @param path Candidate path.
2195 * @return Cost of the path (path length + number of overlapping nodes)
2198 peer_get_path_cost (const struct MeshPeer *peer,
2199 const struct MeshPeerPath *path)
2201 struct MeshConnection *c;
2202 unsigned int overlap;
2210 GNUNET_assert (NULL != peer->tunnel);
2212 for (i = 0; i < path->length; i++)
2214 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2216 for (j = 0; j < c->path->length; j++)
2218 if (path->peers[i] == c->path->peers[j])
2226 return (path->length + overlap) * (path->score * -1);
2231 * Choose the best path towards a peer considering the tunnel properties.
2233 * @param peer The destination peer.
2235 * @return Best current known path towards the peer, if any.
2237 static struct MeshPeerPath *
2238 peer_get_best_path (const struct MeshPeer *peer)
2240 struct MeshPeerPath *best_p;
2241 struct MeshPeerPath *p;
2242 struct MeshConnection *c;
2243 unsigned int best_cost;
2246 best_cost = UINT_MAX;
2248 for (p = peer->path_head; NULL != p; p = p->next)
2250 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2254 continue; /* If path is in use in a connection, skip it. */
2256 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2266 queue_is_sendable (struct MeshPeerQueue *q)
2268 struct MeshFlowControl *fc;
2270 /* Is PID-independent? */
2273 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2274 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2278 /* Is PID allowed? */
2279 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2280 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2288 * Get first sendable message.
2290 * @param peer The destination peer.
2292 * @return Best current known path towards the peer, if any.
2294 static struct MeshPeerQueue *
2295 peer_get_first_message (const struct MeshPeer *peer)
2297 struct MeshPeerQueue *q;
2299 for (q = peer->queue_head; NULL != q; q = q->next)
2301 if (queue_is_sendable (q))
2310 * Try to establish a new connection to this peer in the given tunnel.
2311 * If the peer doesn't have any path to it yet, try to get one.
2312 * If the peer already has some path, send a CREATE CONNECTION towards it.
2314 * @param peer PeerInfo of the peer.
2317 peer_connect (struct MeshPeer *peer)
2319 struct MeshTunnel2 *t;
2320 struct MeshPeerPath *p;
2321 struct MeshConnection *c;
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 "peer_connect towards %s\n",
2327 if (NULL != peer->path_head)
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2330 p = peer_get_best_path (peer);
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2334 c = tunnel_use_path (t, p);
2335 send_connection_create (c);
2338 else if (NULL == peer->dhtget)
2340 const struct GNUNET_PeerIdentity *id;
2342 id = GNUNET_PEER_resolve2 (peer->id);
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 " Starting DHT GET for peer %s\n", peer2s (peer));
2345 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2346 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2347 &id->hashPubKey, /* key to search */
2348 dht_replication_level, /* replication level */
2349 GNUNET_DHT_RO_RECORD_ROUTE |
2350 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2352 0, /* xquery bits */
2353 &dht_get_id_handler, peer);
2354 if (MESH_TUNNEL_NEW == t->state)
2355 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "There is no path but the DHT GET is already started.\n");
2366 * Get the first transmittable message for a connection.
2368 * @param c Connection.
2369 * @param fwd Is this FWD?
2371 * @return First transmittable message.
2373 static struct MeshPeerQueue *
2374 connection_get_first_message (struct MeshConnection *c, int fwd)
2376 struct MeshPeerQueue *q;
2379 p = connection_get_hop (c, fwd);
2381 for (q = p->queue_head; NULL != q; q = q->next)
2385 if (queue_is_sendable (q))
2394 * Is this peer the first one on the connection?
2396 * @param c Connection.
2397 * @param fwd Is this about fwd traffic?
2399 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2402 connection_is_origin (struct MeshConnection *c, int fwd)
2404 if (!fwd && c->own_pos == c->path->length - 1)
2406 if (fwd && c->own_pos == 0)
2413 * Is this peer the last one on the connection?
2415 * @param c Connection.
2416 * @param fwd Is this about fwd traffic?
2417 * Note that the ROOT is the terminal for BCK traffic!
2419 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2422 connection_is_terminal (struct MeshConnection *c, int fwd)
2424 if (fwd && c->own_pos == c->path->length - 1)
2426 if (!fwd && c->own_pos == 0)
2433 * @brief Re-initiate traffic on this connection if necessary.
2435 * Check if there is traffic queued towards this peer
2436 * and the core transmit handle is NULL (traffic was stalled).
2437 * If so, call core tmt rdy.
2439 * @param c Connection on which initiate traffic.
2440 * @param fwd Is this about fwd traffic?
2443 connection_unlock_queue (struct MeshConnection *c, int fwd)
2445 struct MeshPeer *peer;
2446 struct MeshPeerQueue *q;
2449 peer = connection_get_hop (c, fwd);
2451 if (NULL != peer->core_transmit)
2452 return; /* Already unlocked */
2454 q = connection_get_first_message (c, fwd);
2456 return; /* Nothing to transmit */
2459 peer->core_transmit =
2460 GNUNET_CORE_notify_transmit_ready (core_handle,
2463 GNUNET_TIME_UNIT_FOREVER_REL,
2464 GNUNET_PEER_resolve2 (peer->id),
2472 * Cancel all transmissions that belong to a certain connection.
2474 * @param c Connection which to cancel.
2475 * @param fwd Cancel fwd traffic?
2478 connection_cancel_queues (struct MeshConnection *c, int fwd)
2480 struct MeshPeerQueue *q;
2481 struct MeshPeerQueue *next;
2482 struct MeshFlowControl *fc;
2483 struct MeshPeer *peer;
2490 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2491 peer = connection_get_hop (c, fwd);
2493 for (q = peer->queue_head; NULL != q; q = next)
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "connection_cancel_queue %s\n",
2500 GNUNET_MESH_DEBUG_M2S (q->type));
2501 queue_destroy (q, GNUNET_YES);
2504 if (NULL == peer->queue_head)
2506 if (NULL != peer->core_transmit)
2508 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2509 peer->core_transmit = NULL;
2511 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2513 GNUNET_SCHEDULER_cancel (fc->poll_task);
2514 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2521 * Remove all paths that rely on a direct connection between p1 and p2
2522 * from the peer itself and notify all tunnels about it.
2524 * @param peer PeerInfo of affected peer.
2525 * @param p1 GNUNET_PEER_Id of one peer.
2526 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2529 * TODO: optimize (see below)
2532 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2535 struct MeshPeerPath *p;
2536 struct MeshPeerPath *next;
2537 struct MeshPeer *peer_d;
2539 unsigned int destroyed;
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2544 for (p = peer->path_head; NULL != p; p = next)
2547 for (i = 0; i < (p->length - 1); i++)
2549 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2550 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2552 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2563 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2565 peer_d = peer_get_short (d); // FIXME
2566 next = peer_get_best_path (peer_d);
2567 tunnel_use_path (peer->tunnel, next);
2568 peer_connect (peer_d);
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2575 * Add the path to the peer and update the path used to reach it in case this
2578 * @param peer_info Destination peer to add the path to.
2579 * @param path New path to add. Last peer must be the peer in arg 1.
2580 * Path will be either used of freed if already known.
2581 * @param trusted Do we trust that this path is real?
2584 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2587 struct MeshPeerPath *aux;
2591 if ((NULL == peer_info) || (NULL == path))
2594 path_destroy (path);
2597 if (path->peers[path->length - 1] != peer_info->id)
2600 path_destroy (path);
2603 if (2 >= path->length && GNUNET_NO == trusted)
2605 /* Only allow CORE to tell us about direct paths */
2606 path_destroy (path);
2609 for (l = 1; l < path->length; l++)
2611 if (path->peers[l] == myid)
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2614 for (l2 = 0; l2 < path->length - l; l2++)
2616 path->peers[l2] = path->peers[l + l2];
2621 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2626 path->length, peer2s (peer_info));
2628 l = path_get_length (path);
2631 path_destroy (path);
2635 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2636 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2638 l2 = path_get_length (aux);
2641 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2642 peer_info->path_tail, aux, path);
2647 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2649 path_destroy (path);
2654 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2661 * Add the path to the origin peer and update the path used to reach it in case
2662 * this is the shortest.
2663 * The path is given in peer_info -> destination, therefore we turn the path
2664 * upside down first.
2666 * @param peer_info Peer to add the path to, being the origin of the path.
2667 * @param path New path to add after being inversed.
2668 * Path will be either used or freed.
2669 * @param trusted Do we trust that this path is real?
2672 peer_add_path_to_origin (struct MeshPeer *peer_info,
2673 struct MeshPeerPath *path, int trusted)
2676 peer_add_path (peer_info, path, trusted);
2682 * Function called if a connection has been stalled for a while,
2683 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2685 * @param cls Closure (poll ctx).
2686 * @param tc TaskContext.
2689 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2691 struct MeshFlowControl *fc = cls;
2692 struct GNUNET_MESH_Poll msg;
2693 struct MeshConnection *c;
2695 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2696 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2704 peer2s (c->t->peer), c->id);
2706 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2707 msg.header.size = htons (sizeof (msg));
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2709 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2710 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2711 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2712 &connection_poll, fc);
2717 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2718 * to obtain a local peer -> destination path and interning the peer ids.
2720 * @return Newly allocated and created path
2722 static struct MeshPeerPath *
2723 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2724 unsigned int get_path_length,
2725 const struct GNUNET_PeerIdentity *put_path,
2726 unsigned int put_path_length)
2728 struct MeshPeerPath *p;
2734 GNUNET_PEER_change_rc (myid, 1);
2735 i = get_path_length;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2737 for (i--; i >= 0; i--)
2739 id = GNUNET_PEER_intern (&get_path[i]);
2740 if (p->length > 0 && id == p->peers[p->length - 1])
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2743 GNUNET_PEER_change_rc (id, -1);
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2748 GNUNET_i2s (&get_path[i]));
2750 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2751 p->peers[p->length - 1] = id;
2754 i = put_path_length;
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2756 for (i--; i >= 0; i--)
2758 id = GNUNET_PEER_intern (&put_path[i]);
2761 /* PUT path went through us, so discard the path up until now and start
2762 * from here to get a much shorter (and loop-free) path.
2767 if (p->length > 0 && id == p->peers[p->length - 1])
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2770 GNUNET_PEER_change_rc (id, -1);
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2775 GNUNET_i2s (&put_path[i]));
2777 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2778 p->peers[p->length - 1] = id;
2782 if (get_path_length > 0)
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2784 GNUNET_i2s (&get_path[0]));
2785 if (put_path_length > 0)
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2787 GNUNET_i2s (&put_path[0]));
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2790 for (i = 0; i < p->length; i++)
2792 struct GNUNET_PeerIdentity peer_id;
2794 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2796 GNUNET_i2s (&peer_id));
2804 * Adds a path to the peer_infos of all the peers in the path
2806 * @param p Path to process.
2807 * @param confirmed Whether we know if the path works or not.
2810 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2814 /* TODO: invert and add */
2815 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2816 for (i++; i < p->length; i++)
2818 struct MeshPeer *aux;
2819 struct MeshPeerPath *copy;
2821 aux = peer_get_short (p->peers[i]);
2822 copy = path_duplicate (p);
2823 copy->length = i + 1;
2824 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2830 * Search for a channel among the channels for a client
2832 * @param c the client whose channels to search in
2833 * @param chid the local id of the channel
2835 * @return channel handler, NULL if doesn't exist
2837 static struct MeshChannel *
2838 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2840 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2842 GNUNET_break_op (0);
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2846 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2847 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2848 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2852 * Search for a tunnel by global ID using full PeerIdentities.
2854 * @param t Tunnel containing the channel.
2855 * @param chid Public channel number.
2857 * @return channel handler, NULL if doesn't exist
2859 static struct MeshChannel *
2860 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2862 struct MeshChannel *ch;
2867 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2869 if (ch->gid == chid)
2878 * Change the tunnel state.
2880 * @param t Tunnel whose state to change.
2881 * @param state New state.
2884 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2887 "Tunnel %s state was %s\n",
2889 GNUNET_MESH_DEBUG_TS2S (t->state));
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 "Tunnel %s state is now %s\n",
2893 GNUNET_MESH_DEBUG_TS2S (state));
2899 * Cache a message to be sent once tunnel is online.
2901 * @param t Tunnel to hold the message.
2902 * @param ch Channel the message is about.
2903 * @param msg Message itself (copy will be made).
2904 * @param fwd Is this fwd?
2907 tunnel_queue_data (struct MeshTunnel2 *t,
2908 struct MeshChannel *ch,
2909 struct GNUNET_MessageHeader *msg,
2912 struct MeshTunnelQueue *tq;
2913 uint16_t size = ntohs (msg->size);
2915 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2918 memcpy (&tq[1], msg, size);
2919 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2924 * Send all cached messages that we can, tunnel is online.
2926 * @param t Tunnel that holds the messages.
2927 * @param fwd Is this fwd?
2930 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2932 struct MeshTunnelQueue *tq;
2933 struct MeshTunnelQueue *next;
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2937 "tunnel_send_queued_data on %s\n",
2938 GNUNET_h2s (&t->id));
2939 room = tunnel_get_buffer (t, fwd);
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2941 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2945 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2946 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2955 connection_change_state (struct MeshConnection* c,
2956 enum MeshConnectionState state)
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2959 "Connection %s[%X] state was %s\n",
2960 peer2s (c->t->peer), c->id,
2961 GNUNET_MESH_DEBUG_CS2S (c->state));
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 "Connection %s[%X] state is now %s\n",
2964 peer2s (c->t->peer), c->id,
2965 GNUNET_MESH_DEBUG_CS2S (state));
2971 * Add a client to a channel, initializing all needed data structures.
2973 * @param ch Channel to which add the client.
2974 * @param c Client which to add to the channel.
2977 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2979 struct MeshTunnel2 *t = ch->t;
2981 if (NULL != ch->dest)
2987 /* Assign local id as destination */
2988 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2989 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2990 ch->lid_dest = t->next_local_chid++;
2991 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2993 /* Store in client's hashmap */
2995 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2997 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3006 static struct MeshConnection *
3007 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3009 struct MeshConnection *c;
3010 struct MeshPeer *peer;
3011 unsigned int own_pos;
3013 c = connection_new (&t->id, t->next_cid++);
3014 for (own_pos = 0; own_pos < p->length; own_pos++)
3016 if (p->peers[own_pos] == myid)
3019 if (own_pos > p->length - 1)
3022 connection_destroy (c);
3025 c->own_pos = own_pos;
3030 c->fwd_maintenance_task =
3031 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3032 &connection_fwd_keepalive, c);
3035 peer = connection_get_next_hop (c);
3036 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->t->id, c,
3037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3038 peer = connection_get_prev_hop (c);
3039 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->t->id, c,
3040 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3046 * Notifies a tunnel that a connection has broken that affects at least
3047 * some of its peers. Sends a notification towards the root of the tree.
3048 * In case the peer is the owner of the tree, notifies the client that owns
3049 * the tunnel and tries to reconnect.
3051 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3053 * @param t Tunnel affected.
3054 * @param p1 Peer that got disconnected from p2.
3055 * @param p2 Peer that got disconnected from p1.
3057 * @return Short ID of the peer disconnected (either p1 or p2).
3058 * 0 if the tunnel remained unaffected.
3060 static GNUNET_PEER_Id
3061 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3062 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3064 // if (myid != p1 && myid != p2) FIXME
3069 // if (tree_get_predecessor (t->tree) != 0)
3071 // /* We are the peer still connected, notify owner of the disconnection. */
3072 // struct GNUNET_MESH_PathBroken msg;
3073 // struct GNUNET_PeerIdentity neighbor;
3075 // msg.header.size = htons (sizeof (msg));
3076 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3077 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3078 // msg.tid = htonl (t->id.tid);
3079 // msg.peer1 = my_full_id;
3080 // GNUNET_PEER_resolve (pid, &msg.peer2);
3081 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3082 // send_prebuilt_message (&msg.header, &neighbor, t);
3089 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3091 * @param ch Channel this is about.
3092 * @param fwd Is for FWD traffic? (ACK dest->owner)
3095 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3097 struct GNUNET_MESH_DataACK msg;
3098 struct MeshChannelReliability *rel;
3099 struct MeshReliableMessage *copy;
3105 if (GNUNET_NO == ch->reliable)
3110 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3111 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3113 "send_data_ack for %u\n",
3116 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3117 msg.header.type = htons (type);
3118 msg.header.size = htons (sizeof (msg));
3119 msg.chid = htonl (ch->gid);
3120 msg.mid = htonl (*mid - 1);
3122 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3124 if (copy->type != type)
3126 delta = copy->mid - *mid;
3129 mask = 0x1LL << delta;
3130 msg.futures |= mask;
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3132 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3133 copy->mid, delta, mask, msg.futures);
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3137 send_prebuilt_message_channel (&msg.header, ch, fwd);
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3143 * Send an ACK informing the predecessor about the available buffer space.
3145 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3146 * the ACK itself goes "back" (dest->root).
3148 * @param c Connection on which to send the ACK.
3149 * @param fwd Is this FWD ACK? (Going dest->owner)
3152 connection_send_ack (struct MeshConnection *c, int fwd)
3154 struct MeshFlowControl *next_fc;
3155 struct MeshFlowControl *prev_fc;
3159 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3160 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3163 "connection send %s ack on %s[%X]\n",
3164 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->t->id), c->id);
3166 /* Check if we need to transmit the ACK */
3167 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171 " last pid recv: %u, last ack sent: %u\n",
3172 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3176 /* Ok, ACK might be necessary, what PID to ACK? */
3177 delta = next_fc->queue_max - next_fc->queue_n;
3178 ack = prev_fc->last_pid_recv + delta;
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 " last pid %u, last ack %u, qmax %u, q %u\n",
3182 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3183 next_fc->queue_max, next_fc->queue_n);
3184 if (ack == prev_fc->last_ack_sent)
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3190 prev_fc->last_ack_sent = ack;
3191 send_ack (c, ack, fwd);
3196 * Modify the mesh message TID from global to local and send to client.
3198 * @param ch Channel on which to send the message.
3199 * @param msg Message to modify and send.
3200 * @param c Client to send to.
3201 * @param tid Tunnel ID to use (c can be both owner and client).
3204 channel_send_client_to_tid (struct MeshChannel *ch,
3205 const struct GNUNET_MESH_Data *msg,
3206 struct MeshClient *c, MESH_ChannelNumber id)
3208 struct GNUNET_MESH_LocalData *copy;
3209 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3210 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3212 if (size < sizeof (struct GNUNET_MessageHeader))
3214 GNUNET_break_op (0);
3222 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3223 memcpy (©[1], &msg[1], size);
3224 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3225 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3226 copy->id = htonl (id);
3227 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3228 ©->header, GNUNET_NO);
3232 * Modify the data message ID from global to local and send to client.
3234 * @param ch Channel on which to send the message.
3235 * @param msg Message to modify and send.
3236 * @param fwd Forward?
3239 channel_send_client_data (struct MeshChannel *ch,
3240 const struct GNUNET_MESH_Data *msg,
3244 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3246 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3251 * Send a buffered message to the client, for in order delivery or
3252 * as result of client ACK.
3254 * @param ch Channel on which to empty the message buffer.
3255 * @param c Client to send to.
3256 * @param rel Reliability structure to corresponding peer.
3257 * If rel == bck_rel, this is FWD data.
3260 channel_send_client_buffered_data (struct MeshChannel *ch,
3261 struct MeshClient *c,
3262 struct MeshChannelReliability *rel)
3264 struct MeshReliableMessage *copy;
3267 if (GNUNET_NO == rel->client_ready)
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3273 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3276 copy = rel->head_recv;
3277 /* We never buffer channel management messages */
3280 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3282 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 " have %u! now expecting %u\n",
3286 copy->mid, *mid + 1);
3287 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3290 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 " reliable && don't have %u, next is %u\n",
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3307 * We have received a message out of order, or the client is not ready.
3308 * Buffer it until we receive an ACK from the client or the missing
3309 * message from the channel.
3311 * @param msg Message to buffer (MUST be of type MESH_DATA).
3312 * @param rel Reliability data to the corresponding direction.
3315 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3316 struct MeshChannelReliability *rel)
3318 struct MeshReliableMessage *copy;
3319 struct MeshReliableMessage *prev;
3323 size = ntohs (msg->header.size);
3324 mid = ntohl (msg->mid);
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3328 copy = GNUNET_malloc (sizeof (*copy) + size);
3331 memcpy (©[1], msg, size);
3335 // FIXME do something better than O(n), although n < 64...
3336 // FIXME start from the end (most messages are the latest ones)
3337 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3340 if (GMC_is_pid_bigger (prev->mid, mid))
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3343 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3349 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3355 * Destroy a reliable message after it has been acknowledged, either by
3356 * direct mid ACK or bitfield. Updates the appropriate data structures and
3357 * timers and frees all memory.
3359 * @param copy Message that is no longer needed: remote peer got it.
3362 rel_message_free (struct MeshReliableMessage *copy)
3364 struct MeshChannelReliability *rel;
3365 struct GNUNET_TIME_Relative time;
3368 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3369 rel->expected_delay.rel_value_us *= 7;
3370 rel->expected_delay.rel_value_us += time.rel_value_us;
3371 rel->expected_delay.rel_value_us /= 8;
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3376 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3378 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3380 rel->retry_timer = rel->expected_delay;
3381 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3387 * Destroy all reliable messages queued for a channel,
3388 * during a channel destruction.
3389 * Frees the reliability structure itself.
3391 * @param rel Reliability data for a channel.
3394 channel_rel_free_all (struct MeshChannelReliability *rel)
3396 struct MeshReliableMessage *copy;
3397 struct MeshReliableMessage *next;
3402 for (copy = rel->head_recv; NULL != copy; copy = next)
3405 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3408 for (copy = rel->head_sent; NULL != copy; copy = next)
3411 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3414 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3415 GNUNET_SCHEDULER_cancel (rel->retry_task);
3421 * Mark future messages as ACK'd.
3423 * @param rel Reliability data.
3424 * @param msg DataACK message with a bitfield of future ACK'd messages.
3427 channel_rel_free_sent (struct MeshChannelReliability *rel,
3428 const struct GNUNET_MESH_DataACK *msg)
3430 struct MeshReliableMessage *copy;
3431 struct MeshReliableMessage *next;
3438 bitfield = msg->futures;
3439 mid = ntohl (msg->mid);
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3441 "free_sent_reliable %u %llX\n",
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3444 " rel %p, head %p\n",
3445 rel, rel->head_sent);
3446 for (i = 0, copy = rel->head_sent;
3447 i < 64 && NULL != copy && 0 != bitfield;
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3451 " trying bit %u (mid %u)\n",
3454 if (0 == (bitfield & mask))
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3458 /* Bit was set, clear the bit from the bitfield */
3461 /* The i-th bit was set. Do we have that copy? */
3462 /* Skip copies with mid < target */
3463 target = mid + i + 1;
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3465 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3468 /* Did we run out of copies? (previously freed, it's ok) */
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3475 /* Did we overshoot the target? (previously freed, it's ok) */
3476 if (GMC_is_pid_bigger (copy->mid, target))
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3482 /* Now copy->mid == target, free it */
3484 rel_message_free (copy);
3487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3492 * We haven't received an ACK after a certain time: restransmit the message.
3494 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3495 * @param tc TaskContext.
3498 channel_retransmit_message (void *cls,
3499 const struct GNUNET_SCHEDULER_TaskContext *tc)
3501 struct MeshChannelReliability *rel = cls;
3502 struct MeshReliableMessage *copy;
3503 struct MeshPeerQueue *q;
3504 struct MeshChannel *ch;
3505 struct MeshConnection *c;
3506 struct GNUNET_MESH_Data *payload;
3507 struct MeshPeer *hop;
3510 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3515 copy = rel->head_sent;
3522 /* Search the message to be retransmitted in the outgoing queue.
3523 * Check only the queue for the connection that is going to be used,
3524 * if the message is stuck in some other connection's queue we shouldn't
3526 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3527 * the old connection could be temporary stalled or the queue happened to
3528 * be long at time of insertion.
3529 * - not sending the new one could cause terrible delays the old connection
3532 payload = (struct GNUNET_MESH_Data *) ©[1];
3533 fwd = (rel == ch->fwd_rel);
3534 c = tunnel_get_connection (ch->t, fwd);
3535 hop = connection_get_hop (c, fwd);
3536 for (q = hop->queue_head; NULL != q; q = q->next)
3538 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3540 struct GNUNET_MESH_Data *queued_data = q->cls;
3542 if (queued_data->mid == payload->mid)
3547 /* Message not found in the queue that we are going to use. */
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3552 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3553 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3560 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3561 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3562 &channel_retransmit_message,
3568 * Send an ACK to a client if needed.
3570 * @param ch Channel this is regarding.
3571 * @param fwd Is this about fwd traffic? (ACk goes the opposite direction).
3574 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3576 struct MeshClient *c;
3579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3580 "Channel send client %s ack on %s:%X\n",
3581 fwd ? "FWD" : "BCK", GNUNET_h2s (&ch->t->id), ch->gid);
3583 /* Check for buffer space */
3584 if (0 >= tunnel_get_buffer (ch->t, fwd))
3587 /* Client to receive the ACK (fwd indicates traffic to be ACK'd) */
3588 c = fwd ? ch->root : ch->dest;
3590 blocked = fwd ? ch->blocked_fwd : ch->blocked_bck;
3591 if (GNUNET_YES == blocked)
3592 send_local_ack (ch, c, fwd);
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client not blocked\n");
3599 * Send ACK on one or more connections due to buffer space to the client.
3601 * @param ch Channel which has some free buffer space.
3602 * @param buffer Buffer space.
3603 * @param fwd Is this in the FWD direction?
3606 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3608 struct MeshTunnel2 *t = ch->t;
3609 struct MeshConnection *c;
3610 struct MeshFlowControl *fc;
3615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3616 "Channel send connection %s ack on %s:%X\n",
3617 fwd ? "FWD" : "BCK", GNUNET_h2s (&ch->t->id), ch->gid);
3619 /* Count connections, how many messages are already allowed */
3620 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3622 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3623 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3628 allowed += fc->last_ack_sent - fc->last_pid_recv;
3632 /* Make sure there is no overflow */
3633 if (allowed > buffer)
3639 /* Authorize connections to send more data */
3640 to_allow = buffer - allowed;
3641 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3643 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3644 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3648 send_ack (c, fc->last_ack_sent + 1, fwd);
3652 GNUNET_break (to_allow == 0);
3657 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3659 * @param ch Channel to mark as ready.
3660 * @param fwd Was the CREATE message sent fwd?
3663 channel_confirm (struct MeshChannel *ch, int fwd)
3665 struct MeshChannelReliability *rel;
3666 struct MeshReliableMessage *copy;
3667 struct MeshReliableMessage *next;
3669 ch->state = MESH_CHANNEL_READY;
3671 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3672 for (copy = rel->head_sent; NULL != copy; copy = next)
3674 struct GNUNET_MessageHeader *msg;
3677 msg = (struct GNUNET_MessageHeader *) ©[1];
3678 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3680 rel_message_free (copy);
3688 * Save a copy to retransmit in case it gets lost.
3690 * Initializes all needed callbacks and timers.
3692 * @param ch Channel this message goes on.
3693 * @param msg Message to copy.
3694 * @param fwd Is this fwd traffic?
3697 channel_save_copy (struct MeshChannel *ch,
3698 const struct GNUNET_MessageHeader *msg,
3701 struct MeshChannelReliability *rel;
3702 struct MeshReliableMessage *copy;
3707 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3708 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3709 type = ntohs (msg->type);
3710 size = ntohs (msg->size);
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3713 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3715 copy->timestamp = GNUNET_TIME_absolute_get ();
3718 memcpy (©[1], msg, size);
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3721 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3722 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3725 GNUNET_TIME_relative_multiply (rel->expected_delay,
3726 MESH_RETRANSMIT_MARGIN);
3728 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3729 &channel_retransmit_message,
3736 * Send keepalive packets for a connection.
3738 * @param c Connection to keep alive..
3739 * @param fwd Is this a FWD keepalive? (owner -> dest).
3742 connection_keepalive (struct MeshConnection *c, int fwd)
3744 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3745 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3749 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3750 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3753 "sending %s keepalive for connection %s[%d]\n",
3754 fwd ? "FWD" : "BCK",
3755 peer2s (c->t->peer),
3758 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3759 msg->header.size = htons (size);
3760 msg->header.type = htons (type);
3761 msg->cid = htonl (c->id);
3762 msg->tid = c->t->id;
3764 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3769 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3771 * @param c Connection for which to send the message.
3772 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3775 connection_recreate (struct MeshConnection *c, int fwd)
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3779 send_connection_create (c);
3781 send_connection_ack (c, GNUNET_NO);
3786 * Generic connection timer management.
3787 * Depending on the role of the peer in the connection will send the
3788 * appropriate message (build or keepalive)
3790 * @param c Conncetion to maintain.
3791 * @param fwd Is FWD?
3794 connection_maintain (struct MeshConnection *c, int fwd)
3796 if (MESH_TUNNEL_SEARCHING == c->t->state)
3798 /* TODO DHT GET with RO_BART */
3803 case MESH_CONNECTION_NEW:
3805 case MESH_CONNECTION_SENT:
3806 connection_recreate (c, fwd);
3808 case MESH_CONNECTION_READY:
3809 connection_keepalive (c, fwd);
3818 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3820 struct MeshConnection *c = cls;
3822 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3823 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3826 connection_maintain (c, GNUNET_YES);
3827 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3828 &connection_fwd_keepalive,
3834 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3836 struct MeshConnection *c = cls;
3838 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3839 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3842 connection_maintain (c, GNUNET_NO);
3843 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3844 &connection_bck_keepalive,
3850 * Send a message to all peers in this connection that the connection
3851 * is no longer valid.
3853 * If some peer should not receive the message, it should be zero'ed out
3854 * before calling this function.
3856 * @param c The connection whose peers to notify.
3859 connection_send_destroy (struct MeshConnection *c)
3861 struct GNUNET_MESH_ConnectionDestroy msg;
3863 msg.header.size = htons (sizeof (msg));
3864 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3865 msg.cid = htonl (c->id);
3867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3868 " sending connection destroy for connection %s[%X]\n",
3869 peer2s (c->t->peer),
3872 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3873 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3874 c->destroy = GNUNET_YES;
3879 * Confirm we got a channel create.
3881 * @param ch The channel to confirm.
3882 * @param fwd Should we send the ACK fwd?
3885 channel_send_ack (struct MeshChannel *ch, int fwd)
3887 struct GNUNET_MESH_ChannelManage msg;
3889 msg.header.size = htons (sizeof (msg));
3890 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3892 " sending channel destroy for channel %s:%X\n",
3893 peer2s (ch->t->peer),
3896 msg.chid = htonl (ch->gid);
3897 send_prebuilt_message_channel (&msg.header, ch, fwd);
3902 * Send a message to all clients (local and remote) of this channel
3903 * notifying that the channel is no longer valid.
3905 * If some peer or client should not receive the message,
3906 * should be zero'ed out before calling this function.
3908 * @param ch The channel whose clients to notify.
3911 channel_send_destroy (struct MeshChannel *ch)
3913 struct GNUNET_MESH_ChannelManage msg;
3915 msg.header.size = htons (sizeof (msg));
3916 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3918 " sending channel destroy for channel %s:%X\n",
3919 peer2s (ch->t->peer),
3922 if (NULL != ch->root)
3924 msg.chid = htonl (ch->lid_root);
3925 send_local_channel_destroy (ch, GNUNET_NO);
3929 msg.chid = htonl (ch->gid);
3930 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3933 if (NULL != ch->dest)
3935 msg.chid = htonl (ch->lid_dest);
3936 send_local_channel_destroy (ch, GNUNET_YES);
3940 msg.chid = htonl (ch->gid);
3941 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3949 * @param tid Tunnel ID.
3951 static struct MeshTunnel2 *
3952 tunnel_new (const struct GNUNET_HashCode *tid)
3954 struct MeshTunnel2 *t;
3956 t = GNUNET_new (struct MeshTunnel2);
3959 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3961 GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3962 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3969 // char salt[] = "salt";
3970 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3971 // salt, sizeof (salt),
3972 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3973 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3974 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3976 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3977 // salt, sizeof (salt),
3978 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3979 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3980 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3990 * @param tid Tunnel ID.
3992 static struct MeshTunnel2 *
3993 tunnel_get (const struct GNUNET_HashCode *tid)
3995 return GNUNET_CONTAINER_multihashmap_get (tunnels, tid);
4000 * Add a connection to a tunnel.
4003 * @param c Connection.
4006 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4009 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4014 * Initialize a Flow Control structure to the initial state.
4016 * @param fc Flow Control structure to initialize.
4019 fc_init (struct MeshFlowControl *fc)
4022 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4023 fc->last_pid_recv = (uint32_t) -1;
4024 fc->last_ack_sent = (uint32_t) 0;
4025 fc->last_ack_recv = (uint32_t) 0;
4026 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4027 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4029 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4033 static struct MeshConnection *
4034 connection_new (struct GNUNET_HashCode *tid, uint32_t cid)
4036 struct MeshConnection *c;
4037 struct MeshTunnel2 *t;
4039 t = tunnel_get (tid);
4042 t = tunnel_new (tid);
4050 c = GNUNET_new (struct MeshConnection);
4052 fc_init (&c->fwd_fc);
4053 fc_init (&c->bck_fc);
4056 tunnel_add_connection (t, c);
4063 * Find a connection.
4065 * @param tid Tunnel ID.
4066 * @param cid Connection ID.
4068 static struct MeshConnection *
4069 connection_get (const struct GNUNET_HashCode *tid, uint32_t cid)
4071 struct MeshConnection *c;
4072 struct MeshTunnel2 *t;
4074 t = tunnel_get (tid);
4077 for (c = t->connection_head; NULL != c; c = c->next)
4086 connection_destroy (struct MeshConnection *c)
4088 struct MeshPeer *peer;
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4094 peer2s (c->t->peer),
4097 /* Cancel all traffic */
4098 connection_cancel_queues (c, GNUNET_YES);
4099 connection_cancel_queues (c, GNUNET_NO);
4101 /* Cancel maintainance task (keepalive/timeout) */
4102 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4103 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4104 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4105 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4107 /* Deregister from neighbors */
4108 peer = connection_get_next_hop (c);
4110 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->t->id, c);
4111 peer = connection_get_prev_hop (c);
4113 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->t->id, c);
4116 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4117 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4123 tunnel_destroy (struct MeshTunnel2 *t)
4125 struct MeshConnection *c;
4126 struct MeshConnection *next;
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4134 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4137 for (c = t->connection_head; NULL != c; c = next)
4140 connection_destroy (c);
4143 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4150 * Tunnel is empty: destroy it.
4152 * Notifies all connections about the destruction.
4154 * @param t Tunnel to destroy.
4157 tunnel_destroy_empty (struct MeshTunnel2 *t)
4159 struct MeshConnection *c;
4161 for (c = t->connection_head; NULL != c; c = c->next)
4163 if (GNUNET_NO == c->destroy)
4164 connection_send_destroy (c);
4167 if (0 == t->pending_messages)
4170 t->destroy = GNUNET_YES;
4175 * Destroy tunnel if empty (no more channels).
4177 * @param t Tunnel to destroy if empty.
4180 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4182 if (NULL != t->channel_head)
4185 tunnel_destroy_empty (t);
4190 * Destroy a channel and free all resources.
4192 * @param ch Channel to destroy.
4195 channel_destroy (struct MeshChannel *ch)
4197 struct MeshClient *c;
4205 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4216 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4223 channel_rel_free_all (ch->fwd_rel);
4224 channel_rel_free_all (ch->bck_rel);
4226 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4227 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4233 * Create a new channel.
4235 * @param t Tunnel this channel is in.
4236 * @param owner Client that owns the channel, NULL for foreign channels.
4237 * @param lid_root Local ID for root client.
4239 * @return A new initialized channel. NULL on error.
4241 static struct MeshChannel *
4242 channel_new (struct MeshTunnel2 *t,
4243 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4245 struct MeshChannel *ch;
4247 ch = GNUNET_new (struct MeshChannel);
4249 ch->lid_root = lid_root;
4251 ch->blocked_fwd = GNUNET_YES;
4252 ch->blocked_bck = GNUNET_YES;
4254 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4256 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4260 while (NULL != channel_get (t, t->next_chid))
4261 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4262 ch->gid = t->next_chid;
4263 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4266 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4270 channel_destroy (ch);
4271 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4281 * Set options in a channel, extracted from a bit flag field
4283 * @param ch Channel to set options to.
4284 * @param options Bit array in host byte order.
4287 channel_set_options (struct MeshChannel *ch, uint32_t options)
4289 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4290 GNUNET_YES : GNUNET_NO;
4291 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4292 GNUNET_YES : GNUNET_NO;
4297 * Iterator for deleting each channel whose client endpoint disconnected.
4299 * @param cls Closure (client that has disconnected).
4300 * @param key The local channel id (used to access the hashmap).
4301 * @param value The value stored at the key (channel to destroy).
4303 * @return GNUNET_OK, keep iterating.
4306 channel_destroy_iterator (void *cls,
4310 struct MeshChannel *ch = value;
4311 struct MeshClient *c = cls;
4312 struct MeshTunnel2 *t;
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4315 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4316 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4330 channel_send_destroy (ch);
4331 channel_destroy (ch);
4332 tunnel_destroy_if_empty (t);
4339 * Remove client's ports from the global hashmap on disconnect.
4341 * @param cls Closure (unused).
4343 * @param value Client structure.
4345 * @return GNUNET_OK, keep iterating.
4348 client_release_ports (void *cls,
4354 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4355 if (GNUNET_YES != res)
4358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4359 "Port %u by client %p was not registered.\n",
4367 * Timeout function due to lack of keepalive/traffic from the owner.
4368 * Destroys connection if called.
4370 * @param cls Closure (connection to destroy).
4371 * @param tc TaskContext.
4374 connection_fwd_timeout (void *cls,
4375 const struct GNUNET_SCHEDULER_TaskContext *tc)
4377 struct MeshConnection *c = cls;
4379 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4380 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4383 "Connection %s[%X] FWD timed out. Destroying.\n",
4384 peer2s (c->t->peer),
4387 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4390 connection_destroy (c);
4395 * Timeout function due to lack of keepalive/traffic from the destination.
4396 * Destroys connection if called.
4398 * @param cls Closure (connection to destroy).
4399 * @param tc TaskContext
4402 connection_bck_timeout (void *cls,
4403 const struct GNUNET_SCHEDULER_TaskContext *tc)
4405 struct MeshConnection *c = cls;
4407 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4408 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4412 "Connection %s[%X] FWD timed out. Destroying.\n",
4413 peer2s (c->t->peer),
4416 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4419 connection_destroy (c);
4424 * Resets the connection timeout task, some other message has done the
4426 * - For the first peer on the direction this means to send
4427 * a keepalive or a path confirmation message (either create or ACK).
4428 * - For all other peers, this means to destroy the connection,
4429 * due to lack of activity.
4430 * Starts the tiemout if no timeout was running (connection just created).
4432 * @param c Connection whose timeout to reset.
4433 * @param fwd Is this forward?
4435 * TODO use heap to improve efficiency of scheduler.
4438 connection_reset_timeout (struct MeshConnection *c, int fwd)
4440 GNUNET_SCHEDULER_TaskIdentifier *ti;
4441 GNUNET_SCHEDULER_Task f;
4443 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4445 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4446 GNUNET_SCHEDULER_cancel (*ti);
4448 if (connection_is_origin (c, fwd)) /* Endpoint */
4450 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4451 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4455 struct GNUNET_TIME_Relative delay;
4457 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4458 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4459 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4465 * Iterator to notify all connections of a broken link. Mark connections
4466 * to destroy after all traffic has been sent.
4468 * @param cls Closure (peer disconnected).
4469 * @param key Current key code (tid).
4470 * @param value Value in the hash map (connection).
4472 * @return GNUNET_YES if we should continue to iterate,
4476 connection_broken (void *cls,
4477 const struct GNUNET_HashCode *key,
4480 struct MeshPeer *peer = cls;
4481 struct MeshConnection *c = value;
4482 struct GNUNET_MESH_ConnectionBroken msg;
4485 fwd = peer == connection_get_prev_hop (c);
4486 connection_cancel_queues (c, !fwd);
4488 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4489 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4490 msg.cid = htonl (c->id);
4492 msg.peer1 = my_full_id;
4493 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4494 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4495 c->destroy = GNUNET_YES;
4500 /******************************************************************************/
4501 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4502 /******************************************************************************/
4505 * Free a transmission that was already queued with all resources
4506 * associated to the request.
4508 * @param queue Queue handler to cancel.
4509 * @param clear_cls Is it necessary to free associated cls?
4512 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4514 struct MeshPeer *peer;
4515 struct MeshFlowControl *fc;
4520 GNUNET_assert (NULL != queue->c);
4521 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4523 if (GNUNET_YES == clear_cls)
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4526 GNUNET_MESH_DEBUG_M2S (queue->type));
4527 switch (queue->type)
4529 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4530 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4531 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4532 GNUNET_break (GNUNET_YES == queue->c->destroy);
4534 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4535 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4536 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4537 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4538 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4539 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4541 GNUNET_free_non_null (queue->cls);
4546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4547 GNUNET_MESH_DEBUG_M2S (queue->type));
4551 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4555 if (NULL != queue->c)
4557 queue->c->pending_messages--;
4558 if (NULL != queue->c->t)
4560 queue->c->t->pending_messages--;
4564 GNUNET_free (queue);
4569 queue_send (void *cls, size_t size, void *buf)
4571 struct MeshPeer *peer = cls;
4572 struct MeshFlowControl *fc;
4573 struct MeshConnection *c;
4574 struct GNUNET_MessageHeader *msg;
4575 struct MeshPeerQueue *queue;
4576 struct MeshTunnel2 *t;
4577 const struct GNUNET_PeerIdentity *dst_id;
4583 peer->core_transmit = NULL;
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4586 if (NULL == buf || 0 == size)
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4593 queue = peer_get_first_message (peer);
4596 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4601 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4604 dst_id = GNUNET_PEER_resolve2 (peer->id);
4605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4606 /* Check if buffer size is enough for the message */
4607 if (queue->size > size)
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4610 peer->core_transmit =
4611 GNUNET_CORE_notify_transmit_ready (core_handle,
4614 GNUNET_TIME_UNIT_FOREVER_REL,
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4623 t = (NULL != c) ? c->t : NULL;
4627 switch (queue->type)
4629 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4630 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4631 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4632 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4633 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4634 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4637 GNUNET_MESH_DEBUG_M2S (queue->type));
4638 data_size = send_core_data_raw (queue->cls, size, buf);
4639 msg = (struct GNUNET_MessageHeader *) buf;
4640 type = ntohs (msg->type);
4642 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4644 if (connection_is_origin (c, GNUNET_YES))
4645 data_size = send_core_connection_create (queue->c, size, buf);
4647 data_size = send_core_data_raw (queue->cls, size, buf);
4649 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4651 if (connection_is_origin (c, GNUNET_NO))
4652 data_size = send_core_connection_ack (queue->c, size, buf);
4654 data_size = send_core_data_raw (queue->cls, size, buf);
4656 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4657 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4658 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4659 /* This should be encapsulted */
4664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4669 if (0 < drop_percent &&
4670 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4673 "Dropping message of type %s\n",
4674 GNUNET_MESH_DEBUG_M2S (queue->type));
4678 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4681 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4682 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4683 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4684 fc->last_pid_sent = pid;
4685 if (NULL != queue->ch)
4686 channel_send_client_ack (queue->ch, fwd);
4688 connection_send_ack (c, fwd);
4694 /* Free queue, but cls was freed by send_core_* */
4695 queue_destroy (queue, GNUNET_NO);
4697 /* If more data in queue, send next */
4698 queue = peer_get_first_message (peer);
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4702 if (NULL == peer->core_transmit) {
4703 peer->core_transmit =
4704 GNUNET_CORE_notify_transmit_ready(core_handle,
4707 GNUNET_TIME_UNIT_FOREVER_REL,
4715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4716 "* tmt rdy called somewhere else\n");
4718 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4720 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4722 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4727 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4729 GNUNET_SCHEDULER_cancel (fc->poll_task);
4730 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4735 c->pending_messages--;
4736 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4739 connection_destroy (c);
4745 t->pending_messages--;
4746 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4758 queue_add (void *cls, uint16_t type, size_t size,
4759 struct MeshConnection *c,
4760 struct MeshChannel *ch,
4763 struct MeshPeerQueue *queue;
4764 struct MeshFlowControl *fc;
4765 struct MeshPeer *peer;
4768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4769 "queue add %s %s (%u) on c %p, ch %p\n",
4770 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4771 GNUNET_assert (NULL != c);
4773 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4774 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4784 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4785 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4789 ( (NULL != ch->root && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4790 (NULL != ch->dest && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4795 if (fc->queue_n >= fc->queue_max && 0 == priority)
4797 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4801 "queue full: %u/%u\n",
4802 fc->queue_n, fc->queue_max);
4803 return; /* Drop this message */
4806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4808 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4809 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4812 "no buffer space (%u > %u): starting poll\n",
4813 fc->last_pid_sent + 1, fc->last_ack_recv);
4814 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4818 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4826 if (100 <= priority)
4827 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4829 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4831 if (NULL == peer->core_transmit)
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4834 "calling core tmt rdy towards %s\n",
4836 peer->core_transmit =
4837 GNUNET_CORE_notify_transmit_ready (core_handle,
4840 GNUNET_TIME_UNIT_FOREVER_REL,
4841 GNUNET_PEER_resolve2 (peer->id),
4848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4849 "core tmt rdy towards %s already called\n",
4853 c->pending_messages++;
4854 c->t->pending_messages++;
4860 /******************************************************************************/
4861 /******************** MESH NETWORK HANDLERS **************************/
4862 /******************************************************************************/
4866 * Generic handler for mesh network payload traffic.
4868 * @param t Tunnel on which we got this message.
4869 * @param message Unencryted data message.
4870 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4872 * @return GNUNET_OK to keep the connection open,
4873 * GNUNET_SYSERR to close it (signal serious error)
4876 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4878 struct MeshChannelReliability *rel;
4879 struct MeshChannel *ch;
4880 struct MeshClient *c;
4887 size = ntohs (msg->header.size);
4889 sizeof (struct GNUNET_MESH_Data) +
4890 sizeof (struct GNUNET_MessageHeader))
4895 type = ntohs (msg->header.type);
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4897 GNUNET_MESH_DEBUG_M2S (type));
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4899 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4902 ch = channel_get (t, ntohl (msg->chid));
4905 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4910 /* Initialize FWD/BCK data */
4911 c = fwd ? ch->dest : ch->root;
4912 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4913 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4921 tunnel_change_state (t, MESH_TUNNEL_READY);
4923 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4925 mid = ntohl (msg->mid);
4926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4928 if (GNUNET_NO == ch->reliable ||
4929 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4930 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4933 if (GNUNET_YES == ch->reliable)
4935 /* Is this the exact next expected messasge? */
4936 if (mid == *mid_recv)
4938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4939 *mid_recv = *mid_recv + 1;
4940 channel_send_client_data (ch, msg, fwd);
4941 channel_send_client_buffered_data (ch, c, rel);
4945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4946 channel_rel_add_buffered_data (msg, rel);
4949 else /* Tunnel unreliable, send to clients directly */
4951 channel_send_client_data (ch, msg, fwd);
4956 GNUNET_break_op (0);
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4958 " MID %u not expected (%u - %u), dropping!\n",
4959 mid, *mid_recv, *mid_recv + 64);
4962 channel_send_data_ack (ch, fwd);
4967 * Handler for mesh network traffic end-to-end ACKs.
4969 * @param t Tunnel on which we got this message.
4970 * @param message Data message.
4971 * @param fwd Is this a fwd ACK? (dest->orig)
4973 * @return GNUNET_OK to keep the connection open,
4974 * GNUNET_SYSERR to close it (signal serious error)
4977 handle_data_ack (struct MeshTunnel2 *t,
4978 const struct GNUNET_MESH_DataACK *msg, int fwd)
4980 struct MeshChannelReliability *rel;
4981 struct MeshReliableMessage *copy;
4982 struct MeshReliableMessage *next;
4983 struct MeshChannel *ch;
4988 type = ntohs (msg->header.type);
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4990 GNUNET_MESH_DEBUG_M2S (type));
4991 ch = channel_get (t, ntohl (msg->chid));
4994 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4997 ack = ntohl (msg->mid);
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4999 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5001 if (GNUNET_YES == fwd)
5014 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5016 if (GMC_is_pid_bigger (copy->mid, ack))
5018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5019 channel_rel_free_sent (rel, msg);
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5025 rel_message_free (copy);
5027 /* ACK client if needed */
5028 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5030 /* If some message was free'd, update the retransmission delay*/
5031 if (GNUNET_YES == work)
5033 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5035 GNUNET_SCHEDULER_cancel (rel->retry_task);
5036 if (NULL == rel->head_sent)
5038 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5042 struct GNUNET_TIME_Absolute new_target;
5043 struct GNUNET_TIME_Relative delay;
5045 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5046 MESH_RETRANSMIT_MARGIN);
5047 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5049 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5051 GNUNET_SCHEDULER_add_delayed (delay,
5052 &channel_retransmit_message,
5064 * Core handler for connection creation.
5066 * @param cls Closure (unused).
5067 * @param peer Sender (neighbor).
5068 * @param message Message.
5070 * @return GNUNET_OK to keep the connection open,
5071 * GNUNET_SYSERR to close it (signal serious error)
5074 handle_mesh_connection_create (void *cls,
5075 const struct GNUNET_PeerIdentity *peer,
5076 const struct GNUNET_MessageHeader *message)
5078 struct GNUNET_MESH_ConnectionCreate *msg;
5079 struct GNUNET_PeerIdentity *id;
5080 struct GNUNET_HashCode *tid;
5081 struct MeshPeerPath *path;
5082 struct MeshPeer *dest_peer;
5083 struct MeshPeer *orig_peer;
5084 struct MeshConnection *c;
5085 unsigned int own_pos;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5093 size = ntohs (message->size);
5094 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5096 GNUNET_break_op (0);
5100 /* Calculate hops */
5101 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5102 if (size % sizeof (struct GNUNET_PeerIdentity))
5104 GNUNET_break_op (0);
5107 size /= sizeof (struct GNUNET_PeerIdentity);
5110 GNUNET_break_op (0);
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5115 /* Get parameters */
5116 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5117 cid = ntohl (msg->cid);
5119 id = (struct GNUNET_PeerIdentity *) &msg[1];
5120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5121 " connection %s[%X] (%s).\n",
5122 GNUNET_h2s (tid), cid, GNUNET_i2s (id));
5124 /* Create connection */
5125 c = connection_get (tid, cid);
5128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5129 c = connection_new (tid, cid);
5133 connection_reset_timeout (c, GNUNET_YES);
5134 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5136 /* Remember peers */
5137 dest_peer = peer_get (&id[size - 1]);
5138 orig_peer = peer_get (&id[0]);
5141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5142 path = path_new (size);
5144 for (i = 0; i < size; i++)
5146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5147 GNUNET_i2s (&id[i]));
5148 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5149 if (path->peers[i] == myid)
5152 if (own_pos == 0 && path->peers[own_pos] != myid)
5154 /* create path: self not found in path through self */
5155 GNUNET_break_op (0);
5156 path_destroy (path);
5157 connection_destroy (c);
5160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5161 path_add_to_peers (path, GNUNET_NO);
5162 c->path = path_duplicate (path);
5163 c->own_pos = own_pos;
5165 /* Is it a connection to us? */
5166 if (own_pos == size - 1)
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5169 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5171 send_connection_ack (c, GNUNET_NO);
5173 /* Keep tunnel alive in direction dest->owner*/
5174 connection_reset_timeout (c, GNUNET_NO);
5178 /* It's for somebody else! Retransmit. */
5179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5180 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5181 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5182 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5189 * Core handler for path ACKs
5191 * @param cls closure
5192 * @param message message
5193 * @param peer peer identity this notification is about
5195 * @return GNUNET_OK to keep the connection open,
5196 * GNUNET_SYSERR to close it (signal serious error)
5199 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5200 const struct GNUNET_MessageHeader *message)
5202 struct GNUNET_MESH_ConnectionACK *msg;
5203 struct MeshPeerPath *p;
5204 struct MeshConnection *c;
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5208 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5209 cid = ntohl(msg->cid);
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s[%X]\n",
5211 GNUNET_h2s (&msg->tid), cid);
5212 c = connection_get (&msg->tid, cid);
5215 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5224 /* Add path to peers? */
5228 path_add_to_peers (p, GNUNET_YES);
5234 connection_change_state (c, MESH_CONNECTION_READY);
5235 connection_reset_timeout (c, GNUNET_NO);
5236 if (MESH_TUNNEL_READY != c->t->state)
5237 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5238 tunnel_send_queued_data (c->t, GNUNET_YES);
5240 /* Message for us? */
5241 if (connection_is_terminal (c, GNUNET_NO))
5243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5244 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5246 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5247 c->t->peer->dhtget = NULL;
5249 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5254 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5260 * Core handler for notifications of broken paths
5262 * @param cls Closure (unused).
5263 * @param peer Peer identity of sending neighbor.
5264 * @param message Message.
5266 * @return GNUNET_OK to keep the connection open,
5267 * GNUNET_SYSERR to close it (signal serious error)
5270 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5271 const struct GNUNET_MessageHeader *message)
5273 struct GNUNET_MESH_ConnectionBroken *msg;
5274 struct MeshConnection *c;
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5278 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5280 GNUNET_i2s (&msg->peer1));
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5282 GNUNET_i2s (&msg->peer2));
5283 c = connection_get (&msg->tid, ntohl (msg->cid));
5286 GNUNET_break_op (0);
5289 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5290 GNUNET_PEER_search (&msg->peer2));
5297 * Core handler for tunnel destruction
5299 * @param cls Closure (unused).
5300 * @param peer Peer identity of sending neighbor.
5301 * @param message Message.
5303 * @return GNUNET_OK to keep the connection open,
5304 * GNUNET_SYSERR to close it (signal serious error)
5307 handle_mesh_connection_destroy (void *cls,
5308 const struct GNUNET_PeerIdentity *peer,
5309 const struct GNUNET_MessageHeader *message)
5311 struct GNUNET_MESH_ConnectionDestroy *msg;
5312 struct MeshConnection *c;
5316 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5318 "Got a CONNECTION DESTROY message from %s\n",
5320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5321 " for connection %s[%X]\n",
5322 GNUNET_h2s (&msg->tid), ntohl (msg->cid));
5323 c = connection_get (&msg->tid, ntohl (msg->cid));
5326 /* Probably already got the message from another path,
5327 * destroyed the tunnel and retransmitted to children.
5330 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5334 id = GNUNET_PEER_search (peer);
5335 if (id == connection_get_prev_hop (c)->id)
5337 else if (id == connection_get_next_hop (c)->id)
5341 GNUNET_break_op (0);
5344 send_prebuilt_message_connection (message, c, NULL, fwd);
5345 c->destroy = GNUNET_YES;
5352 * Handler for channel create messages.
5354 * @param t Tunnel this channel is to be created in.
5355 * @param msg Message.
5356 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5358 * @return GNUNET_OK to keep the connection open,
5359 * GNUNET_SYSERR to close it (signal serious error)
5362 handle_channel_create (struct MeshTunnel2 *t,
5363 struct GNUNET_MESH_ChannelCreate *msg,
5366 MESH_ChannelNumber chid;
5367 struct MeshChannel *ch;
5368 struct MeshClient *c;
5371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5372 /* Check message size */
5373 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5375 GNUNET_break_op (0);
5379 /* Check if channel exists */
5380 chid = ntohl (msg->chid);
5381 ch = channel_get (t, chid);
5384 /* Probably a retransmission, safe to ignore */
5385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5389 /* Create channel */
5390 ch = channel_new (t, NULL, 0);
5391 channel_set_options (ch, ntohl (msg->opt));
5394 /* Find a destination client */
5395 port = ntohl (msg->port);
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5397 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5400 /* TODO send reject */
5401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5405 channel_add_client (ch, c);
5406 if (GNUNET_YES == ch->reliable)
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5409 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5410 ch->bck_rel->ch = ch;
5411 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5414 send_local_channel_create (ch);
5415 channel_send_ack (ch, !fwd);
5422 * Handler for channel ack messages.
5424 * @param t Tunnel this channel is to be created in.
5425 * @param msg Message.
5426 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5428 * @return GNUNET_OK to keep the connection open,
5429 * GNUNET_SYSERR to close it (signal serious error)
5432 handle_channel_ack (struct MeshTunnel2 *t,
5433 struct GNUNET_MESH_ChannelManage *msg,
5436 MESH_ChannelNumber chid;
5437 struct MeshChannel *ch;
5439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5440 /* Check message size */
5441 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5443 GNUNET_break_op (0);
5447 /* Check if channel exists */
5448 chid = ntohl (msg->chid);
5449 ch = channel_get (t, chid);
5452 GNUNET_break_op (0);
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5457 channel_confirm (ch, !fwd);
5463 * Handler for channel destroy messages.
5465 * @param t Tunnel this channel is to be destroyed of.
5466 * @param msg Message.
5467 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5469 * @return GNUNET_OK to keep the connection open,
5470 * GNUNET_SYSERR to close it (signal serious error)
5473 handle_channel_destroy (struct MeshTunnel2 *t,
5474 struct GNUNET_MESH_ChannelManage *msg,
5477 MESH_ChannelNumber chid;
5478 struct MeshChannel *ch;
5480 /* Check message size */
5481 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5483 GNUNET_break_op (0);
5487 /* Check if channel exists */
5488 chid = ntohl (msg->chid);
5489 ch = channel_get (t, chid);
5492 /* Probably a retransmission, safe to ignore */
5496 send_local_channel_destroy (ch, fwd);
5497 channel_destroy (ch);
5504 * Generic handler for mesh network encrypted traffic.
5506 * @param peer Peer identity this notification is about.
5507 * @param message Encrypted message.
5508 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5510 * @return GNUNET_OK to keep the connection open,
5511 * GNUNET_SYSERR to close it (signal serious error)
5514 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5515 const struct GNUNET_MESH_Encrypted *msg,
5518 struct MeshConnection *c;
5519 struct MeshTunnel2 *t;
5520 struct MeshPeer *neighbor;
5521 struct MeshFlowControl *fc;
5528 size = ntohs (msg->header.size);
5530 sizeof (struct GNUNET_MESH_Encrypted) +
5531 sizeof (struct GNUNET_MessageHeader))
5536 type = ntohs (msg->header.type);
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5538 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5540 /* Check connection */
5541 c = connection_get (&msg->tid, ntohl (msg->cid));
5544 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5549 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5551 /* Check if origin is as expected */
5552 neighbor = connection_get_hop (c, fwd);
5553 if (peer_get (peer)->id != neighbor->id)
5555 GNUNET_break_op (0);
5560 pid = ntohl (msg->pid);
5561 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5563 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5565 "WARNING Received PID %u, (prev %u), ACK %u\n",
5566 pid, fc->last_pid_recv, fc->last_ack_sent);
5569 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5571 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5573 " Pid %u not expected (%u+), dropping!\n",
5574 pid, fc->last_pid_recv + 1);
5577 if (MESH_CONNECTION_SENT == c->state)
5578 connection_change_state (c, MESH_CONNECTION_READY);
5579 connection_reset_timeout (c, fwd);
5580 fc->last_pid_recv = pid;
5582 /* Is this message for us? */
5583 if (connection_is_terminal (c, fwd))
5585 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5587 struct GNUNET_MessageHeader *msgh;
5590 /* TODO signature verification */
5591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us!\n");
5592 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5594 fc->last_pid_recv = pid;
5595 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5596 msgh = (struct GNUNET_MessageHeader *) cbuf;
5597 switch (ntohs (msgh->type))
5599 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5600 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5603 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5604 r = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5607 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5608 r = handle_channel_create (t,
5609 (struct GNUNET_MESH_ChannelCreate *) msgh,
5613 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5614 r = handle_channel_ack (t,
5615 (struct GNUNET_MESH_ChannelManage *) msgh,
5619 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5620 r = handle_channel_destroy (t,
5621 (struct GNUNET_MESH_ChannelManage *) msgh,
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5627 "end-to-end message not known (%u)\n",
5628 ntohs (msgh->type));
5632 connection_send_ack (c, fwd);
5636 /* Message not for us: forward to next hop */
5637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5638 ttl = ntohl (msg->ttl);
5639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5642 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5644 connection_send_ack (c, fwd);
5647 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5649 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5656 * Core handler for mesh network traffic going orig->dest.
5658 * @param cls Closure (unused).
5659 * @param message Message received.
5660 * @param peer Peer who sent the message.
5662 * @return GNUNET_OK to keep the connection open,
5663 * GNUNET_SYSERR to close it (signal serious error)
5666 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5667 const struct GNUNET_MessageHeader *message)
5669 return handle_mesh_encrypted (peer,
5670 (struct GNUNET_MESH_Encrypted *)message,
5675 * Core handler for mesh network traffic going dest->orig.
5677 * @param cls Closure (unused).
5678 * @param message Message received.
5679 * @param peer Peer who sent the message.
5681 * @return GNUNET_OK to keep the connection open,
5682 * GNUNET_SYSERR to close it (signal serious error)
5685 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5686 const struct GNUNET_MessageHeader *message)
5688 return handle_mesh_encrypted (peer,
5689 (struct GNUNET_MESH_Encrypted *)message,
5695 * Core handler for mesh network traffic point-to-point acks.
5697 * @param cls closure
5698 * @param message message
5699 * @param peer peer identity this notification is about
5701 * @return GNUNET_OK to keep the connection open,
5702 * GNUNET_SYSERR to close it (signal serious error)
5705 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5706 const struct GNUNET_MessageHeader *message)
5708 struct GNUNET_MESH_ACK *msg;
5709 struct MeshConnection *c;
5710 struct MeshFlowControl *fc;
5714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5716 msg = (struct GNUNET_MESH_ACK *) message;
5718 c = connection_get (&msg->tid, ntohl (msg->cid));
5722 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5727 ack = ntohl (msg->ack);
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5730 /* Is this a forward or backward ACK? */
5731 id = GNUNET_PEER_search (peer);
5732 if (connection_get_next_hop (c)->id == id)
5734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5737 else if (connection_get_prev_hop (c)->id == id)
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5744 GNUNET_break_op (0);
5748 /* Cancel polling if the ACK is bigger than before. */
5749 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5750 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5752 GNUNET_SCHEDULER_cancel (fc->poll_task);
5753 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5754 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5757 fc->last_ack_recv = ack;
5758 connection_unlock_queue (c, fc == &c->fwd_fc);
5765 * Core handler for mesh network traffic point-to-point ack polls.
5767 * @param cls closure
5768 * @param message message
5769 * @param peer peer identity this notification is about
5771 * @return GNUNET_OK to keep the connection open,
5772 * GNUNET_SYSERR to close it (signal serious error)
5775 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5776 const struct GNUNET_MessageHeader *message)
5778 struct GNUNET_MESH_Poll *msg;
5779 struct MeshConnection *c;
5780 struct MeshFlowControl *fc;
5784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5787 msg = (struct GNUNET_MESH_Poll *) message;
5789 c = connection_get (&msg->tid, ntohl (msg->cid));
5793 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5795 GNUNET_break_op (0);
5799 /* Is this a forward or backward ACK? */
5800 id = GNUNET_PEER_search (peer);
5801 if (connection_get_next_hop (c)->id == id)
5803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5806 else if (connection_get_prev_hop (c)->id == id)
5808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5813 GNUNET_break_op (0);
5817 pid = ntohl (msg->pid);
5818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5819 pid, fc->last_pid_recv);
5820 fc->last_pid_recv = pid;
5821 connection_send_ack (c, fc == &c->fwd_fc);
5828 * Core handler for mesh keepalives.
5830 * @param cls closure
5831 * @param message message
5832 * @param peer peer identity this notification is about
5833 * @return GNUNET_OK to keep the connection open,
5834 * GNUNET_SYSERR to close it (signal serious error)
5836 * TODO: Check who we got this from, to validate route.
5839 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5840 const struct GNUNET_MessageHeader *message)
5842 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5843 struct MeshConnection *c;
5844 struct MeshPeer *neighbor;
5847 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5851 c = connection_get (&msg->tid, ntohl (msg->cid));
5854 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5859 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5860 GNUNET_YES : GNUNET_NO;
5862 /* Check if origin is as expected */
5863 neighbor = connection_get_hop (c, fwd);
5864 if (peer_get (peer)->id != neighbor->id)
5866 GNUNET_break_op (0);
5870 connection_change_state (c, MESH_CONNECTION_READY);
5871 connection_reset_timeout (c, fwd);
5873 if (connection_is_terminal (c, fwd))
5876 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5877 send_prebuilt_message_connection (message, c, NULL, fwd);
5885 * Functions to handle messages from core
5887 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5888 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5890 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5891 sizeof (struct GNUNET_MESH_ConnectionACK)},
5892 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5893 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5894 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5895 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5896 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5897 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5898 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5899 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5900 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5901 sizeof (struct GNUNET_MESH_ACK)},
5902 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5903 sizeof (struct GNUNET_MESH_Poll)},
5904 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5905 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5911 * Function to process paths received for a new peer addition. The recorded
5912 * paths form the initial tunnel, which can be optimized later.
5913 * Called on each result obtained for the DHT search.
5915 * @param cls closure
5916 * @param exp when will this value expire
5917 * @param key key of the result
5918 * @param get_path path of the get request
5919 * @param get_path_length lenght of get_path
5920 * @param put_path path of the put request
5921 * @param put_path_length length of the put_path
5922 * @param type type of the result
5923 * @param size number of bytes in data
5924 * @param data pointer to the result data
5927 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5928 const struct GNUNET_HashCode * key,
5929 const struct GNUNET_PeerIdentity *get_path,
5930 unsigned int get_path_length,
5931 const struct GNUNET_PeerIdentity *put_path,
5932 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5933 size_t size, const void *data)
5935 struct MeshPeer *peer = cls;
5936 struct MeshPeerPath *p;
5937 struct MeshConnection *c;
5938 struct GNUNET_PeerIdentity pi;
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5942 GNUNET_PEER_resolve (peer->id, &pi);
5943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5945 p = path_build_from_dht (get_path, get_path_length,
5946 put_path, put_path_length);
5947 path_add_to_peers (p, GNUNET_NO);
5950 /* Count connections */
5951 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5953 /* If we already have 3 (or more (?!)) connections, it's enough */
5957 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5960 peer_connect (peer);
5966 /******************************************************************************/
5967 /********************* MESH LOCAL HANDLES **************************/
5968 /******************************************************************************/
5972 * Handler for client connection.
5974 * @param cls Closure (unused).
5975 * @param client Client handler.
5978 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5980 struct MeshClient *c;
5984 c = GNUNET_malloc (sizeof (struct MeshClient));
5986 c->id = next_client_id++; /* overflow not important: just for debug */
5987 GNUNET_SERVER_client_keep (client);
5988 GNUNET_SERVER_client_set_user_context (client, c);
5989 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5994 * Handler for client disconnection
5996 * @param cls closure
5997 * @param client identification of the client; NULL
5998 * for the last call when the server is destroyed
6001 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6003 struct MeshClient *c;
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6012 c = client_get (client);
6015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6017 GNUNET_SERVER_client_drop (c->handle);
6018 c->shutting_down = GNUNET_YES;
6019 if (NULL != c->own_channels)
6021 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6022 &channel_destroy_iterator, c);
6023 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6026 if (NULL != c->incoming_channels)
6028 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6029 &channel_destroy_iterator, c);
6030 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6033 if (NULL != c->ports)
6035 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6036 &client_release_ports, c);
6037 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6039 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6040 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6046 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6054 * Handler for new clients
6056 * @param cls closure
6057 * @param client identification of the client
6058 * @param message the actual message, which includes messages the client wants
6061 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6062 const struct GNUNET_MessageHeader *message)
6064 struct GNUNET_MESH_ClientConnect *cc_msg;
6065 struct MeshClient *c;
6070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6072 /* Check data sanity */
6073 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6074 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6075 if (0 != (size % sizeof (uint32_t)))
6078 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6081 size /= sizeof (uint32_t);
6083 /* Initialize new client structure */
6084 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6091 p = (uint32_t *) &cc_msg[1];
6092 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6093 for (i = 0; i < size; i++)
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6098 /* store in client's hashmap */
6099 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6100 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6101 /* store in global hashmap */
6102 /* FIXME only allow one client to have the port open,
6103 * have a backup hashmap with waiting clients */
6104 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6105 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6109 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6110 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6111 GNUNET_SERVER_notification_context_add (nc, client);
6112 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6120 * Handler for requests of new tunnels
6122 * @param cls Closure.
6123 * @param client Identification of the client.
6124 * @param message The actual message.
6127 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6128 const struct GNUNET_MessageHeader *message)
6130 struct GNUNET_MESH_ChannelMessage *msg;
6131 struct MeshPeer *peer;
6132 struct MeshTunnel2 *t;
6133 struct MeshChannel *ch;
6134 struct MeshClient *c;
6135 MESH_ChannelNumber chid;
6137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6139 /* Sanity check for client registration */
6140 if (NULL == (c = client_get (client)))
6143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6148 /* Message size sanity check */
6149 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6156 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6158 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6159 chid = ntohl (msg->channel_id);
6161 /* Sanity check for duplicate channel IDs */
6162 if (NULL != channel_get_by_local_id (c, chid))
6165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6169 peer = peer_get (&msg->peer);
6170 if (NULL == peer->tunnel)
6172 struct GNUNET_HashCode tid;
6174 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &tid);
6175 peer->tunnel = tunnel_new (&tid);
6176 peer->tunnel->peer = peer;
6180 /* Create channel */
6181 ch = channel_new (t, c, chid);
6185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6188 ch->port = ntohl (msg->port);
6189 channel_set_options (ch, ntohl (msg->opt));
6191 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6192 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
6193 ch->fwd_rel->ch = ch;
6194 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
6196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6197 GNUNET_h2s (&t->id), ch->gid, ch->port, ch->lid_root);
6198 peer_connect (peer);
6200 /* Send create channel */
6202 struct GNUNET_MESH_ChannelCreate msgcc;
6204 msgcc.header.size = htons (sizeof (msgcc));
6205 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6206 msgcc.chid = htonl (ch->gid);
6207 msgcc.port = msg->port;
6208 msgcc.opt = msg->opt;
6210 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6213 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6219 * Handler for requests of deleting tunnels
6221 * @param cls closure
6222 * @param client identification of the client
6223 * @param message the actual message
6226 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6227 const struct GNUNET_MessageHeader *message)
6229 struct GNUNET_MESH_ChannelMessage *msg;
6230 struct MeshClient *c;
6231 struct MeshChannel *ch;
6232 struct MeshTunnel2 *t;
6233 MESH_ChannelNumber chid;
6235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6236 "Got a DESTROY CHANNEL from client!\n");
6238 /* Sanity check for client registration */
6239 if (NULL == (c = client_get (client)))
6242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6247 /* Message sanity check */
6248 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6255 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6257 /* Retrieve tunnel */
6258 chid = ntohl (msg->channel_id);
6259 ch = channel_get_by_local_id (c, chid);
6262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6268 /* Cleanup after the tunnel */
6269 client_delete_channel (c, ch);
6270 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6274 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6281 " channel %X client %p (%p, %p)\n",
6282 chid, c, ch->root, ch->dest);
6287 channel_destroy (ch);
6288 tunnel_destroy_if_empty (t);
6290 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6296 * Handler for client traffic
6298 * @param cls closure
6299 * @param client identification of the client
6300 * @param message the actual message
6303 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6304 const struct GNUNET_MessageHeader *message)
6306 struct GNUNET_MESH_LocalData *msg;
6307 struct MeshClient *c;
6308 struct MeshChannel *ch;
6309 MESH_ChannelNumber chid;
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6314 "Got data from a client!\n");
6316 /* Sanity check for client registration */
6317 if (NULL == (c = client_get (client)))
6320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6325 msg = (struct GNUNET_MESH_LocalData *) message;
6327 /* Sanity check for message size */
6328 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6329 if (size < sizeof (struct GNUNET_MessageHeader))
6332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6336 /* Channel exists? */
6337 chid = ntohl (msg->id);
6338 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6339 ch = channel_get_by_local_id (c, chid);
6343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6347 /* Is the client in the channel? */
6350 ch->root->handle == client)
6354 ch->dest->handle == client) ) )
6357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6362 ch->blocked_fwd = GNUNET_YES;
6364 ch->blocked_bck = GNUNET_YES;
6366 /* Ok, everything is correct, send the message. */
6368 struct GNUNET_MESH_Data *payload;
6369 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6370 unsigned char cbuf[p2p_size];
6373 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6374 payload = (struct GNUNET_MESH_Data *) cbuf;
6375 payload->mid = htonl (*mid);
6377 memcpy (&payload[1], &msg[1], size);
6378 payload->header.size = htons (p2p_size);
6379 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6380 payload->chid = htonl (ch->gid);
6381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6382 send_prebuilt_message_channel (&payload->header, ch, fwd);
6384 if (GNUNET_YES == ch->reliable)
6385 channel_save_copy (ch, &payload->header, fwd);
6387 if (tunnel_get_buffer (ch->t, fwd) > 0)
6388 send_local_ack (ch, c, fwd);
6389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6390 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6397 * Handler for client's ACKs for payload traffic.
6399 * @param cls Closure (unused).
6400 * @param client Identification of the client.
6401 * @param message The actual message.
6404 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6405 const struct GNUNET_MessageHeader *message)
6407 struct GNUNET_MESH_LocalAck *msg;
6408 struct MeshChannelReliability *rel;
6409 struct MeshChannel *ch;
6410 struct MeshClient *c;
6411 MESH_ChannelNumber chid;
6414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6416 /* Sanity check for client registration */
6417 if (NULL == (c = client_get (client)))
6420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6425 msg = (struct GNUNET_MESH_LocalAck *) message;
6427 /* Channel exists? */
6428 chid = ntohl (msg->channel_id);
6429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6430 ch = channel_get_by_local_id (c, chid);
6434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6440 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6441 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6443 rel->client_ready = GNUNET_YES;
6444 channel_send_client_buffered_data (ch, c, rel);
6445 channel_send_connection_ack (ch, 64 - rel->n_recv, fwd);
6447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6454 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6456 * @param cls Closure (client handle).
6457 * @param key Key (hashed tunnel ID, unused).
6458 * @param value Tunnel info.
6460 * @return GNUNET_YES, to keep iterating.
6463 monitor_all_tunnels_iterator (void *cls,
6464 const struct GNUNET_HashCode * key,
6467 struct GNUNET_SERVER_Client *client = cls;
6468 struct MeshChannel *ch = value;
6469 struct GNUNET_MESH_LocalMonitor *msg;
6471 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6472 msg->channel_id = htonl (ch->gid);
6473 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6474 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6477 "* sending info about tunnel %s\n",
6478 GNUNET_i2s (&msg->owner));
6480 GNUNET_SERVER_notification_context_unicast (nc, client,
6481 &msg->header, GNUNET_NO);
6487 * Handler for client's MONITOR request.
6489 * @param cls Closure (unused).
6490 * @param client Identification of the client.
6491 * @param message The actual message.
6494 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6495 const struct GNUNET_MessageHeader *message)
6497 struct MeshClient *c;
6499 /* Sanity check for client registration */
6500 if (NULL == (c = client_get (client)))
6503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6507 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6508 "Received get tunnels request from client %u\n",
6510 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6511 monitor_all_tunnels_iterator,
6513 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6514 "Get tunnels request from client %u completed\n",
6516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6521 * Handler for client's MONITOR_TUNNEL request.
6523 * @param cls Closure (unused).
6524 * @param client Identification of the client.
6525 * @param message The actual message.
6528 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6529 const struct GNUNET_MessageHeader *message)
6531 const struct GNUNET_MESH_LocalMonitor *msg;
6532 struct GNUNET_MESH_LocalMonitor *resp;
6533 struct MeshClient *c;
6534 struct MeshChannel *ch;
6536 /* Sanity check for client registration */
6537 if (NULL == (c = client_get (client)))
6540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6544 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6545 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6546 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6549 ntohl (msg->channel_id));
6550 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6554 /* We don't know the tunnel */
6555 struct GNUNET_MESH_LocalMonitor warn;
6558 GNUNET_SERVER_notification_context_unicast (nc, client,
6561 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6565 /* Initialize context */
6566 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6568 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6569 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6570 &resp->header, GNUNET_NO);
6573 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6574 "Monitor tunnel request from client %u completed\n",
6576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6581 * Functions to handle messages from clients
6583 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6584 {&handle_local_new_client, NULL,
6585 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6586 {&handle_local_channel_create, NULL,
6587 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6588 sizeof (struct GNUNET_MESH_ChannelMessage)},
6589 {&handle_local_channel_destroy, NULL,
6590 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6591 sizeof (struct GNUNET_MESH_ChannelMessage)},
6592 {&handle_local_data, NULL,
6593 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6594 {&handle_local_ack, NULL,
6595 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6596 sizeof (struct GNUNET_MESH_LocalAck)},
6597 {&handle_local_get_tunnels, NULL,
6598 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6599 sizeof (struct GNUNET_MessageHeader)},
6600 {&handle_local_show_tunnel, NULL,
6601 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6602 sizeof (struct GNUNET_MESH_LocalMonitor)},
6608 * Method called whenever a given peer connects.
6610 * @param cls closure
6611 * @param peer peer identity this notification is about
6614 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6616 struct MeshPeer *pi;
6617 struct MeshPeerPath *path;
6619 DEBUG_CONN ("Peer connected\n");
6620 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6621 pi = peer_get (peer);
6624 DEBUG_CONN (" (self)\n");
6625 path = path_new (1);
6629 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6630 path = path_new (2);
6631 path->peers[1] = pi->id;
6632 GNUNET_PEER_change_rc (pi->id, 1);
6633 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6635 path->peers[0] = myid;
6636 GNUNET_PEER_change_rc (myid, 1);
6637 peer_add_path (pi, path, GNUNET_YES);
6639 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6645 * Method called whenever a peer disconnects.
6647 * @param cls closure
6648 * @param peer peer identity this notification is about
6651 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6653 struct MeshPeer *pi;
6655 DEBUG_CONN ("Peer disconnected\n");
6656 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6663 peer_remove_path (pi, myid, pi->id);
6665 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6668 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6669 pi->connections = NULL;
6672 DEBUG_CONN (" (self)\n");
6674 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6681 * Install server (service) handlers and start listening to clients.
6686 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6687 GNUNET_SERVER_connect_notify (server_handle,
6688 &handle_local_client_connect, NULL);
6689 GNUNET_SERVER_disconnect_notify (server_handle,
6690 &handle_local_client_disconnect, NULL);
6691 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6693 clients_head = NULL;
6694 clients_tail = NULL;
6696 GNUNET_SERVER_resume (server_handle);
6701 * To be called on core init/fail.
6703 * @param cls Closure (config)
6704 * @param server handle to the server for this service
6705 * @param identity the public identity of this peer
6708 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6709 const struct GNUNET_PeerIdentity *identity)
6711 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6715 GNUNET_break (core_handle == server);
6716 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6722 GNUNET_i2s (identity));
6723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6725 GNUNET_i2s (&my_full_id));
6726 GNUNET_CORE_disconnect (core_handle);
6727 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6728 NULL, /* Closure passed to MESH functions */
6729 &core_init, /* Call core_init once connected */
6730 &core_connect, /* Handle connects */
6731 &core_disconnect, /* remove peers on disconnects */
6732 NULL, /* Don't notify about all incoming messages */
6733 GNUNET_NO, /* For header only in notification */
6734 NULL, /* Don't notify about all outbound messages */
6735 GNUNET_NO, /* For header-only out notification */
6736 core_handlers); /* Register these handlers */
6745 /******************************************************************************/
6746 /************************ MAIN FUNCTIONS ****************************/
6747 /******************************************************************************/
6750 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6752 * @param cls closure
6753 * @param key current key code
6754 * @param value value in the hash map
6755 * @return GNUNET_YES if we should continue to iterate,
6759 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6761 struct MeshTunnel2 *t = value;
6769 * Task run during shutdown.
6775 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6779 if (core_handle != NULL)
6781 GNUNET_CORE_disconnect (core_handle);
6784 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6785 if (dht_handle != NULL)
6787 GNUNET_DHT_disconnect (dht_handle);
6792 GNUNET_SERVER_notification_context_destroy (nc);
6795 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6797 GNUNET_SCHEDULER_cancel (announce_id_task);
6798 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6805 * Process mesh requests.
6807 * @param cls closure
6808 * @param server the initialized server
6809 * @param c configuration to use
6812 run (void *cls, struct GNUNET_SERVER_Handle *server,
6813 const struct GNUNET_CONFIGURATION_Handle *c)
6816 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6819 server_handle = server;
6820 GNUNET_SERVER_suspend (server_handle);
6823 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6828 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6829 "mesh", "peer/privatekey");
6830 GNUNET_SCHEDULER_shutdown ();
6835 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6836 &refresh_connection_time))
6838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6840 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6841 "mesh", "refresh path time");
6842 GNUNET_SCHEDULER_shutdown ();
6847 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6852 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6853 "mesh", "id announce time");
6854 GNUNET_SCHEDULER_shutdown ();
6859 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6864 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6865 "mesh", "connect timeout");
6866 GNUNET_SCHEDULER_shutdown ();
6871 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6876 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6877 "mesh", "max msgs queue");
6878 GNUNET_SCHEDULER_shutdown ();
6883 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6886 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6888 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6889 "mesh", "max tunnels");
6890 GNUNET_SCHEDULER_shutdown ();
6895 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6900 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6901 "mesh", "default ttl", 64);
6906 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6909 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6910 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6911 "mesh", "max peers", 1000);
6916 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6923 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6924 "Mesh is running with drop mode enabled. "
6925 "This is NOT a good idea! "
6926 "Remove the DROP_PERCENT option from your configuration.\n");
6930 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6931 &dht_replication_level))
6933 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6935 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6936 "mesh", "dht replication level", 3);
6937 dht_replication_level = 3;
6940 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6941 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6942 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6944 dht_handle = GNUNET_DHT_connect (c, 64);
6945 if (NULL == dht_handle)
6949 stats = GNUNET_STATISTICS_create ("mesh", c);
6951 /* Scheduled the task to clean up when shutdown is called */
6952 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6954 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6955 GNUNET_free (keyfile);
6956 GNUNET_assert (NULL != pk);
6957 my_private_key = pk;
6958 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6959 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6960 &my_full_id.hashPubKey);
6961 myid = GNUNET_PEER_intern (&my_full_id);
6962 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6963 "Mesh for peer [%s] starting\n",
6964 GNUNET_i2s(&my_full_id));
6966 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6967 NULL, /* Closure passed to MESH functions */
6968 &core_init, /* Call core_init once connected */
6969 &core_connect, /* Handle connects */
6970 &core_disconnect, /* remove peers on disconnects */
6971 NULL, /* Don't notify about all incoming messages */
6972 GNUNET_NO, /* For header only in notification */
6973 NULL, /* Don't notify about all outbound messages */
6974 GNUNET_NO, /* For header-only out notification */
6975 core_handlers); /* Register these handlers */
6976 if (NULL == core_handle)
6979 GNUNET_SCHEDULER_shutdown ();
6982 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6988 * The main function for the mesh service.
6990 * @param argc number of arguments from the command line
6991 * @param argv command line arguments
6992 * @return 0 ok, 1 on error
6995 main (int argc, char *const *argv)
7000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7001 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7003 ret = (GNUNET_OK == r) ? 0 : 1;
7004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");