2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-enc.c
23 * @brief GNUnet MESH service with encryption
24 * @author Bartlomiej Polot
27 * - when sending in-order buffered data, wait for client ACKs
30 * - set connection IDs independently from tunnel, tunnel has no ID
33 * - relay corking down to core
34 * - set ttl relative to path length
38 * - peer: other mesh instance. If there is direct connection it's a neighbor.
39 * - tunnel: encrypted connection to a peer, neighbor or not.
40 * - channel: connection between two clients, on the same or different peers.
41 * have properties like reliability.
42 * - path: series of directly connected peer from one peer to another.
43 * - connection: path which is being used in a tunnel.
47 #include "gnunet_crypto_lib.h"
49 #include "mesh_protocol_enc.h"
50 #include "mesh_path.h"
51 #include "block_mesh.h"
52 #include "gnunet_dht_service.h"
53 #include "gnunet_statistics_service.h"
55 #define MESH_BLOOM_SIZE 128
57 #define MESH_DEBUG_DHT GNUNET_NO
58 #define MESH_DEBUG_CONNECTION GNUNET_YES
59 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
61 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
62 GNUNET_TIME_UNIT_MINUTES,\
64 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
65 #define MESH_RETRANSMIT_MARGIN 4
67 #if MESH_DEBUG_CONNECTION
68 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
70 #define DEBUG_CONN(...)
74 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
76 #define DEBUG_DHT(...)
83 struct timespec __mesh_start;
84 struct timespec __mesh_end;
85 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
86 #define INTERVAL_END \
88 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
89 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
90 if (__diff < 0) __diff += 1000000000;\
94 #define INTERVAL_SHOW \
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
98 #define INTERVAL_START
100 #define INTERVAL_SHOW
104 * All the states a tunnel can be in.
109 * Uninitialized status, should never appear in operation.
114 * Path to the peer not known yet
116 MESH_TUNNEL_SEARCHING,
119 * Request sent, not yet answered.
124 * Peer connected and ready to accept data
129 * Peer connected previosly but not responding
131 MESH_TUNNEL_RECONNECTING
136 * All the states a connection can be in.
138 enum MeshConnectionState
141 * Uninitialized status, should never appear in operation.
146 * Connection create message sent, waiting for ACK.
148 MESH_CONNECTION_SENT,
151 * Connection ACK sent, waiting for ACK.
156 * Connection confirmed, ready to carry traffic.
158 MESH_CONNECTION_READY,
163 * All the states a connection can be in.
165 enum MeshChannelState
168 * Uninitialized status, should never appear in operation.
173 * Connection create message sent, waiting for ACK.
178 * Connection confirmed, ready to carry traffic..
184 /******************************************************************************/
185 /************************ DATA STRUCTURES ****************************/
186 /******************************************************************************/
188 /** FWD declaration */
192 struct MeshConnection;
194 struct MeshChannelReliability;
198 * Struct containing info about a queued transmission to this peer
205 struct MeshPeerQueue *next;
210 struct MeshPeerQueue *prev;
213 * Peer this transmission is directed to.
215 struct MeshPeer *peer;
218 * Connection this message belongs to.
220 struct MeshConnection *c;
228 * Channel this message belongs to, if known.
230 struct MeshChannel *ch;
233 * Pointer to info stucture used as cls.
243 * Size of the message
250 * Struct to encapsulate all the Flow Control information to a peer to which
251 * we are directly connected (on a core level).
253 struct MeshFlowControl
256 * Connection this controls.
258 struct MeshConnection *c;
261 * How many messages are in the queue on this connection.
263 unsigned int queue_n;
266 * How many messages do we accept in the queue.
268 unsigned int queue_max;
276 * ID of the last packet sent towards the peer.
278 uint32_t last_pid_sent;
281 * ID of the last packet received from the peer.
283 uint32_t last_pid_recv;
286 * Last ACK sent to the peer (peer can't send more than this PID).
288 uint32_t last_ack_sent;
291 * Last ACK sent towards the origin (for traffic towards leaf node).
293 uint32_t last_ack_recv;
296 * Task to poll the peer in case of a lost ACK causes stall.
298 GNUNET_SCHEDULER_TaskIdentifier poll_task;
301 * How frequently to poll for ACKs.
303 struct GNUNET_TIME_Relative poll_time;
308 * Struct containing all information regarding a given peer
318 * Last time we heard from this peer
320 struct GNUNET_TIME_Absolute last_contact;
323 * Paths to reach the peer, ordered by ascending hop count
325 struct MeshPeerPath *path_head;
328 * Paths to reach the peer, ordered by ascending hop count
330 struct MeshPeerPath *path_tail;
333 * Handle to stop the DHT search for paths to this peer
335 struct GNUNET_DHT_GetHandle *dhtget;
338 * Tunnel to this peer, if any.
340 struct MeshTunnel2 *tunnel;
343 * Connections that go through this peer, indexed by tid;
345 struct GNUNET_CONTAINER_MultiHashMap *connections;
348 * Handle for queued transmissions
350 struct GNUNET_CORE_TransmitHandle *core_transmit;
353 * Transmission queue to core DLL head
355 struct MeshPeerQueue *queue_head;
358 * Transmission queue to core DLL tail
360 struct MeshPeerQueue *queue_tail;
363 * How many messages are in the queue to this peer.
365 unsigned int queue_n;
370 * Info needed to retry a message in case it gets lost.
372 struct MeshReliableMessage
375 * Double linked list, FIFO style
377 struct MeshReliableMessage *next;
378 struct MeshReliableMessage *prev;
381 * Type of message (payload, channel management).
386 * Tunnel Reliability queue this message is in.
388 struct MeshChannelReliability *rel;
391 * ID of the message (ACK needed to free)
396 * When was this message issued (to calculate ACK delay)
398 struct GNUNET_TIME_Absolute timestamp;
400 /* struct GNUNET_MESH_Data with payload */
405 * Info about the traffic state for a client in a channel.
407 struct MeshChannelReliability
410 * Channel this is about.
412 struct MeshChannel *ch;
415 * DLL of messages sent and not yet ACK'd.
417 struct MeshReliableMessage *head_sent;
418 struct MeshReliableMessage *tail_sent;
421 * Messages pending to send.
426 * DLL of messages received out of order.
428 struct MeshReliableMessage *head_recv;
429 struct MeshReliableMessage *tail_recv;
437 * Next MID to use for outgoing traffic.
442 * Next MID expected for incoming traffic.
447 * Can we send data to the client?
452 * Task to resend/poll in case no ACK is received.
454 GNUNET_SCHEDULER_TaskIdentifier retry_task;
457 * Counter for exponential backoff.
459 struct GNUNET_TIME_Relative retry_timer;
462 * How long does it usually take to get an ACK.
464 struct GNUNET_TIME_Relative expected_delay;
469 * Struct containing all information regarding a channel to a remote client.
474 * Tunnel this channel is in.
476 struct MeshTunnel2 *t;
479 * Double linked list.
481 struct MeshChannel *next;
482 struct MeshChannel *prev;
485 * Destination port of the channel.
490 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
492 MESH_ChannelNumber gid;
495 * Local tunnel number for root (owner) client.
496 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
498 MESH_ChannelNumber lid_root;
501 * Local tunnel number for local destination clients (incoming number)
502 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
504 MESH_ChannelNumber lid_dest;
509 enum MeshChannelState state;
512 * Is the tunnel bufferless (minimum latency)?
517 * Is the tunnel reliable?
522 * Last time the channel was used
524 struct GNUNET_TIME_Absolute timestamp;
527 * Client owner of the tunnel, if any
529 struct MeshClient *root;
532 * Client destination of the tunnel, if any.
534 struct MeshClient *dest;
537 * Flag to signal the destruction of the channel.
538 * If this is set GNUNET_YES the channel will be destroyed
539 * when the queue is empty.
544 * Total messages pending for this channel, payload or not.
546 unsigned int pending_messages;
550 * Only present (non-NULL) at the owner of a tunnel.
552 struct MeshChannelReliability *root_rel;
556 * Only present (non-NULL) at the destination of a tunnel.
558 struct MeshChannelReliability *dest_rel;
564 * Struct containing all information regarding a connection to a peer.
566 struct MeshConnection
571 struct MeshConnection *next;
572 struct MeshConnection *prev;
575 * Tunnel this connection is part of.
577 struct MeshTunnel2 *t;
580 * Flow control information for traffic fwd.
582 struct MeshFlowControl fwd_fc;
585 * Flow control information for traffic bck.
587 struct MeshFlowControl bck_fc;
590 * ID of the connection.
592 struct GNUNET_HashCode id;
595 * State of the connection.
597 enum MeshConnectionState state;
600 * Path being used for the tunnel.
602 struct MeshPeerPath *path;
605 * Position of the local peer in the path.
607 unsigned int own_pos;
610 * Task to keep the used paths alive at the owner,
611 * time tunnel out on all the other peers.
613 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
616 * Task to keep the used paths alive at the destination,
617 * time tunnel out on all the other peers.
619 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
622 * Pending message count.
624 int pending_messages;
627 * Destroy flag: if true, destroy on last message.
634 * Struct used to queue messages in a tunnel.
636 struct MeshTunnelQueue
641 struct MeshTunnelQueue *next;
642 struct MeshTunnelQueue *prev;
647 struct MeshChannel *ch;
652 /* struct GNUNET_MessageHeader *msg; */
657 * Struct containing all information regarding a tunnel to a peer.
662 * Endpoint of the tunnel.
664 struct MeshPeer *peer;
667 * State of the tunnel.
669 enum MeshTunnelState state;
672 * Local peer ephemeral private key
674 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
677 * Local peer ephemeral public key
679 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
682 * Remote peer's public key.
684 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
687 * Encryption ("our") key.
689 struct GNUNET_CRYPTO_AesSessionKey e_key;
692 * Decryption ("their") key.
694 struct GNUNET_CRYPTO_AesSessionKey d_key;
697 * Paths that are actively used to reach the destination peer.
699 struct MeshConnection *connection_head;
700 struct MeshConnection *connection_tail;
703 * Next connection number.
708 * Channels inside this tunnel.
710 struct MeshChannel *channel_head;
711 struct MeshChannel *channel_tail;
714 * Channel ID for the next created channel.
716 MESH_ChannelNumber next_chid;
719 * Channel ID for the next incoming channel.
721 MESH_ChannelNumber next_local_chid;
724 * Pending message count.
726 int pending_messages;
729 * Destroy flag: if true, destroy on last message.
734 * Queued messages, to transmit once tunnel gets connected.
736 struct MeshTunnelQueue *tq_head;
737 struct MeshTunnelQueue *tq_tail;
743 * Struct containing information about a client of the service
745 * TODO: add a list of 'waiting' ports
752 struct MeshClient *next;
757 struct MeshClient *prev;
760 * Tunnels that belong to this client, indexed by local id
762 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
765 * Tunnels this client has accepted, indexed by incoming local id
767 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
770 * Handle to communicate with the client
772 struct GNUNET_SERVER_Client *handle;
775 * Ports that this client has declared interest in.
776 * Indexed by port, contains *Client.
778 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
781 * Whether the client is active or shutting down (don't send confirmations
782 * to a client that is shutting down.
787 * ID of the client, mainly for debug messages
793 /******************************************************************************/
794 /************************ DEBUG FUNCTIONS ****************************/
795 /******************************************************************************/
799 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
800 * @param cls string to print
801 * @param success GNUNET_OK if the PUT was transmitted,
802 * GNUNET_NO on timeout,
803 * GNUNET_SYSERR on disconnect from service
804 * after the PUT message was transmitted
805 * (so we don't know if it was received or not)
810 mesh_debug (void *cls, int success)
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
820 /******************************************************************************/
821 /*********************** GLOBAL VARIABLES ****************************/
822 /******************************************************************************/
824 /************************** Configuration parameters **************************/
827 * How often to send path keepalives. Paths timeout after 4 missed.
829 static struct GNUNET_TIME_Relative refresh_connection_time;
832 * How often to PUT own ID in the DHT.
834 static struct GNUNET_TIME_Relative id_announce_time;
837 * Maximum time allowed to connect to a peer found by string.
839 static struct GNUNET_TIME_Relative connect_timeout;
842 * Default TTL for payload packets.
844 static unsigned long long default_ttl;
847 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
849 static unsigned long long dht_replication_level;
852 * How many connections are we willing to maintain.
853 * Local connections are always allowed, even if there are more connections than max.
855 static unsigned long long max_connections;
858 * How many messages *in total* are we willing to queue, divide by number of
859 * connections to get connection queue size.
861 static unsigned long long max_msgs_queue;
864 * How many peers do we want to remember?
866 static unsigned long long max_peers;
869 * Percentage of messages that will be dropped (for test purposes only).
871 static unsigned long long drop_percent;
873 /*************************** Static global variables **************************/
876 * DLL with all the clients, head.
878 static struct MeshClient *clients_head;
881 * DLL with all the clients, tail.
883 static struct MeshClient *clients_tail;
886 * Connections known, indexed by cid (MeshConnection).
888 static struct GNUNET_CONTAINER_MultiHashMap *connections;
891 * Peers known, indexed by PeerIdentity (MeshPeer).
893 static struct GNUNET_CONTAINER_MultiHashMap *peers;
896 * Handle to communicate with core.
898 static struct GNUNET_CORE_Handle *core_handle;
903 static struct GNUNET_DHT_Handle *dht_handle;
906 * Handle to server lib.
908 static struct GNUNET_SERVER_Handle *server_handle;
911 * Handle to the statistics service.
913 static struct GNUNET_STATISTICS_Handle *stats;
916 * Notification context, to send messages to local clients.
918 static struct GNUNET_SERVER_NotificationContext *nc;
921 * Local peer own ID (memory efficient handle).
923 static GNUNET_PEER_Id myid;
926 * Local peer own ID (full value).
928 static struct GNUNET_PeerIdentity my_full_id;
933 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
938 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
941 * All ports clients of this peer have opened.
943 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
946 * Task to periodically announce itself in the network.
948 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
951 * Next ID to assign to a client.
953 unsigned int next_client_id;
956 /******************************************************************************/
957 /*********************** DECLARATIONS **************************/
958 /******************************************************************************/
961 * Function to process paths received for a new peer addition. The recorded
962 * paths form the initial tunnel, which can be optimized later.
963 * Called on each result obtained for the DHT search.
966 * @param exp when will this value expire
967 * @param key key of the result
968 * @param type type of the result
969 * @param size number of bytes in data
970 * @param data pointer to the result data
973 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
974 const struct GNUNET_HashCode * key,
975 const struct GNUNET_PeerIdentity *get_path,
976 unsigned int get_path_length,
977 const struct GNUNET_PeerIdentity *put_path,
978 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
979 size_t size, const void *data);
983 * Retrieve the MeshPeer stucture associated with the peer, create one
984 * and insert it in the appropriate structures if the peer is not known yet.
986 * @param peer Full identity of the peer.
988 * @return Existing or newly created peer info.
990 static struct MeshPeer *
991 peer_get (const struct GNUNET_PeerIdentity *peer);
995 * Retrieve the MeshPeer stucture associated with the peer, create one
996 * and insert it in the appropriate structures if the peer is not known yet.
998 * @param peer Short identity of the peer.
1000 * @return Existing or newly created peer info.
1002 static struct MeshPeer *
1003 peer_get_short (const GNUNET_PEER_Id peer);
1007 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1008 * to obtain a local peer -> destination path and interning the peer ids.
1010 * @return Newly allocated and created path
1012 static struct MeshPeerPath *
1013 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1014 unsigned int get_path_length,
1015 const struct GNUNET_PeerIdentity *put_path,
1016 unsigned int put_path_length);
1020 * Adds a path to the data structs of all the peers in the path
1022 * @param p Path to process.
1023 * @param confirmed Whether we know if the path works or not.
1026 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1030 * Search for a tunnel by global ID using full PeerIdentities.
1032 * @param t Tunnel containing the channel.
1033 * @param chid Public channel number.
1035 * @return channel handler, NULL if doesn't exist
1037 static struct MeshChannel *
1038 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1042 * Change the tunnel state.
1044 * @param t Tunnel whose state to change.
1045 * @param state New state.
1048 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1052 * Notify a tunnel that a connection has broken that affects at least
1053 * some of its peers.
1055 * @param t Tunnel affected.
1056 * @param p1 Peer that got disconnected from p2.
1057 * @param p2 Peer that got disconnected from p1.
1059 * @return Short ID of the peer disconnected (either p1 or p2).
1060 * 0 if the tunnel remained unaffected.
1062 static GNUNET_PEER_Id
1063 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1064 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1067 * @brief Use the given path for the tunnel.
1068 * Update the next and prev hops (and RCs).
1069 * (Re)start the path refresh in case the tunnel is locally owned.
1071 * @param t Tunnel to update.
1072 * @param p Path to use.
1074 * @return Connection created.
1076 static struct MeshConnection *
1077 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1080 * Tunnel is empty: destroy it.
1082 * Notifies all participants (peers, cleints) about the destruction.
1084 * @param t Tunnel to destroy.
1087 tunnel_destroy_empty (struct MeshTunnel2 *t);
1090 * Destroy the tunnel.
1092 * This function does not generate any warning traffic to clients or peers.
1095 * Cancel messages belonging to this tunnel queued to neighbors.
1096 * Free any allocated resources linked to the tunnel.
1098 * @param t The tunnel to destroy.
1101 tunnel_destroy (struct MeshTunnel2 *t);
1104 * Create a connection.
1106 * @param cid Connection ID.
1108 static struct MeshConnection *
1109 connection_new (const struct GNUNET_HashCode *cid);
1112 * Connection is no longer needed: destroy it and remove from tunnel.
1114 * @param c Connection to destroy.
1117 connection_destroy (struct MeshConnection *c);
1120 * Send FWD keepalive packets for a connection.
1122 * @param cls Closure (connection for which to send the keepalive).
1123 * @param tc Notification context.
1126 connection_fwd_keepalive (void *cls,
1127 const struct GNUNET_SCHEDULER_TaskContext *tc);
1130 * Send BCK keepalive packets for a connection.
1132 * @param cls Closure (connection for which to send the keepalive).
1133 * @param tc Notification context.
1136 connection_bck_keepalive (void *cls,
1137 const struct GNUNET_SCHEDULER_TaskContext *tc);
1141 * Change the tunnel state.
1143 * @param c Connection whose state to change.
1144 * @param state New state.
1147 connection_change_state (struct MeshConnection* c,
1148 enum MeshConnectionState state);
1153 * @brief Queue and pass message to core when possible.
1155 * @param cls Closure (@c type dependant). It will be used by queue_send to
1156 * build the message to be sent if not already prebuilt.
1157 * @param type Type of the message, 0 for a raw message.
1158 * @param size Size of the message.
1159 * @param c Connection this message belongs to (cannot be NULL).
1160 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1161 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1164 queue_add (void* cls,
1167 struct MeshConnection* c,
1168 struct MeshChannel* ch,
1173 * Free a transmission that was already queued with all resources
1174 * associated to the request.
1176 * @param queue Queue handler to cancel.
1177 * @param clear_cls Is it necessary to free associated cls?
1180 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1184 * Core callback to write a queued packet to core buffer
1186 * @param cls Closure (peer info).
1187 * @param size Number of bytes available in buf.
1188 * @param buf Where the to write the message.
1190 * @return number of bytes written to buf
1193 queue_send (void *cls, size_t size, void *buf);
1197 * Dummy function to separate declarations from definitions in function list.
1200 __mesh_divider______________________________________________________________();
1204 * Get string description for tunnel state.
1206 * @param s Tunnel state.
1208 * @return String representation.
1211 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1213 static char buf[128];
1217 case MESH_TUNNEL_NEW:
1218 return "MESH_TUNNEL_NEW";
1219 case MESH_TUNNEL_SEARCHING:
1220 return "MESH_TUNNEL_SEARCHING";
1221 case MESH_TUNNEL_WAITING:
1222 return "MESH_TUNNEL_WAITING";
1223 case MESH_TUNNEL_READY:
1224 return "MESH_TUNNEL_READY";
1225 case MESH_TUNNEL_RECONNECTING:
1226 return "MESH_TUNNEL_RECONNECTING";
1229 sprintf (buf, "%u (UNKNOWN STATE)", s);
1236 * Get string description for tunnel state.
1238 * @param s Tunnel state.
1240 * @return String representation.
1243 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1247 case MESH_CONNECTION_NEW:
1248 return "MESH_CONNECTION_NEW";
1249 case MESH_CONNECTION_SENT:
1250 return "MESH_CONNECTION_SENT";
1251 case MESH_CONNECTION_READY:
1252 return "MESH_CONNECTION_READY";
1254 return "MESH_CONNECTION_STATE_ERROR";
1260 /******************************************************************************/
1261 /************************ PERIODIC FUNCTIONS ****************************/
1262 /******************************************************************************/
1265 * Periodically announce self id in the DHT
1267 * @param cls closure
1268 * @param tc task context
1271 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1273 struct PBlock block;
1275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1277 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1281 * - Set data expiration in function of X
1282 * - Adapt X to churn
1284 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1286 block.id = my_full_id;
1287 GNUNET_DHT_put (dht_handle, /* DHT handle */
1288 &my_full_id.hashPubKey, /* Key to use */
1289 dht_replication_level, /* Replication level */
1290 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1291 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1292 sizeof (block), /* Size of the data */
1293 (const char *) &block, /* Data itself */
1294 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1295 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1296 NULL, /* Continuation */
1297 NULL); /* Continuation closure */
1299 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1303 /******************************************************************************/
1304 /****************** GENERAL HELPER FUNCTIONS ************************/
1305 /******************************************************************************/
1309 * Get the static string for a peer ID.
1313 * @return Static string for it's ID.
1316 peer2s (const struct MeshPeer *peer)
1320 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1325 * Get the previous hop in a connection
1327 * @param c Connection.
1329 * @return Previous peer in the connection.
1331 static struct MeshPeer *
1332 connection_get_prev_hop (struct MeshConnection *c)
1336 if (0 == c->own_pos || c->path->length < 2)
1337 id = c->path->peers[0];
1339 id = c->path->peers[c->own_pos - 1];
1341 return peer_get_short (id);
1346 * Get the next hop in a connection
1348 * @param c Connection.
1350 * @return Next peer in the connection.
1352 static struct MeshPeer *
1353 connection_get_next_hop (struct MeshConnection *c)
1357 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1358 id = c->path->peers[c->path->length - 1];
1360 id = c->path->peers[c->own_pos + 1];
1362 return peer_get_short (id);
1367 * Get the hop in a connection.
1369 * @param c Connection.
1370 * @param fwd Next hop?
1372 * @return Next peer in the connection.
1374 static struct MeshPeer *
1375 connection_get_hop (struct MeshConnection *c, int fwd)
1378 return connection_get_next_hop (c);
1379 return connection_get_prev_hop (c);
1383 * Check if client has registered with the service and has not disconnected
1385 * @param client the client to check
1387 * @return non-NULL if client exists in the global DLL
1389 static struct MeshClient *
1390 client_get (struct GNUNET_SERVER_Client *client)
1392 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1397 * Deletes a tunnel from a client (either owner or destination).
1399 * @param c Client whose tunnel to delete.
1400 * @param ch Channel which should be deleted.
1403 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1409 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1411 if (GNUNET_YES != res)
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1416 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1418 if (GNUNET_YES != res)
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1425 * Notify the appropriate client that a new incoming channel was created.
1427 * @param ch Channel that was created.
1430 send_local_channel_create (struct MeshChannel *ch)
1432 struct GNUNET_MESH_ChannelMessage msg;
1433 struct MeshTunnel2 *t = ch->t;
1435 if (NULL == ch->dest)
1437 msg.header.size = htons (sizeof (msg));
1438 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1439 msg.channel_id = htonl (ch->lid_dest);
1440 msg.port = htonl (ch->port);
1442 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1443 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1444 msg.opt = htonl (msg.opt);
1445 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1446 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1447 &msg.header, GNUNET_NO);
1452 * Notify a client that the incoming tunnel is no longer valid.
1454 * @param ch Channel that is destroyed.
1455 * @param fwd Forward notification (owner->dest)?
1458 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1460 struct GNUNET_MESH_ChannelMessage msg;
1461 struct MeshClient *c;
1463 c = fwd ? ch->dest : ch->root;
1469 msg.header.size = htons (sizeof (msg));
1470 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1471 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1472 msg.port = htonl (0);
1473 memset (&msg.peer, 0, sizeof (msg.peer));
1474 msg.opt = htonl (0);
1475 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1476 &msg.header, GNUNET_NO);
1481 * Build a local ACK message and send it to a local client, if needed.
1483 * If the client was already allowed to send data, do nothing.
1485 * @param ch Channel on which to send the ACK.
1486 * @param c Client to whom send the ACK.
1487 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
1490 send_local_ack (struct MeshChannel *ch, int fwd)
1492 struct GNUNET_MESH_LocalAck msg;
1493 struct MeshChannelReliability *rel;
1494 struct MeshClient *c;
1496 c = fwd ? ch->root : ch->dest;
1497 rel = fwd ? ch->root_rel : ch->dest_rel;
1499 if (GNUNET_YES == rel->client_ready)
1500 return; /* don't send double ACKs to client */
1502 rel->client_ready = GNUNET_YES;
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1505 "send local %s ack on %s:%X towards %p\n",
1506 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1509 || ( fwd && (0 == ch->lid_root || c != ch->root))
1510 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1515 msg.header.size = htons (sizeof (msg));
1516 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1517 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1518 GNUNET_SERVER_notification_context_unicast (nc,
1527 * Count established (ready) connections of a tunnel.
1529 * @param t Tunnel on which to send the message.
1531 * @return Number of connections.
1534 tunnel_count_connections (struct MeshTunnel2 *t)
1536 struct MeshConnection *c;
1539 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1546 * Pick a connection on which send the next data message.
1548 * @param t Tunnel on which to send the message.
1549 * @param fwd Is this a fwd message?
1551 * @return The connection on which to send the next message.
1553 static struct MeshConnection *
1554 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1556 struct MeshConnection *c;
1557 struct MeshConnection *best;
1558 struct MeshFlowControl *fc;
1559 unsigned int lowest_q;
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
1564 lowest_q = UINT_MAX;
1565 for (c = t->connection_head; NULL != c; c = c->next)
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s, \n",
1568 GNUNET_h2s (&c->id));
1569 if (MESH_CONNECTION_READY == c->state)
1571 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
1578 if (fc->queue_n < lowest_q)
1581 lowest_q = fc->queue_n;
1592 * Is this peer the first one on the connection?
1594 * @param c Connection.
1595 * @param fwd Is this about fwd traffic?
1597 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1600 connection_is_origin (struct MeshConnection *c, int fwd)
1602 if (!fwd && c->own_pos == c->path->length - 1)
1604 if (fwd && c->own_pos == 0)
1611 * Is this peer the last one on the connection?
1613 * @param c Connection.
1614 * @param fwd Is this about fwd traffic?
1615 * Note that the ROOT is the terminal for BCK traffic!
1617 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1620 connection_is_terminal (struct MeshConnection *c, int fwd)
1622 if (fwd && c->own_pos == c->path->length - 1)
1624 if (!fwd && c->own_pos == 0)
1631 * Get free buffer space towards the client on a specific channel.
1633 * @param ch Channel.
1634 * @param fwd Is query about FWD traffic?
1636 * @return Free buffer space [0 - 64]
1639 channel_get_buffer (struct MeshChannel *ch, int fwd)
1641 struct MeshChannelReliability *rel;
1643 rel = fwd ? ch->dest_rel : ch->root_rel;
1645 /* If rel is NULL it means that the end is not yet created,
1646 * most probably is a loopback channel at the point of sending
1647 * the ChannelCreate to itself.
1652 return (64 - rel->n_recv);
1657 * Get free buffer space in a connection.
1659 * @param c Connection.
1660 * @param fwd Is query about FWD traffic?
1662 * @return Free buffer space [0 - max_msgs_queue/max_connections]
1665 connection_get_buffer (struct MeshConnection *c, int fwd)
1667 struct MeshFlowControl *fc;
1669 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1671 return (fc->queue_max - fc->queue_n);
1676 * Get the total buffer space for a tunnel.
1679 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1681 struct MeshConnection *c;
1682 struct MeshFlowControl *fc;
1683 unsigned int buffer;
1685 c = t->connection_head;
1694 /* If terminal, return biggest channel buffer */
1695 if (connection_is_terminal (c, fwd))
1697 struct MeshChannel *ch;
1698 unsigned int ch_buf;
1700 if (NULL == t->channel_head)
1703 for (ch = t->channel_head; NULL != ch; ch = ch->next)
1705 ch_buf = channel_get_buffer (ch, fwd);
1706 if (ch_buf > buffer)
1712 /* If not terminal, return sum of connection buffers */
1715 if (c->state != MESH_CONNECTION_READY)
1718 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1719 buffer += fc->last_ack_recv - fc->last_pid_sent;
1728 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1729 * Encrypt data with the tunnel key.
1731 * @param t Tunnel whose key to use.
1732 * @param dst Destination for the encrypted data.
1733 * @param src Source of the plaintext.
1734 * @param size Size of the plaintext.
1735 * @param iv Initialization Vector to use.
1736 * @param fwd Is this a fwd message?
1739 tunnel_encrypt (struct MeshTunnel2 *t,
1740 void *dst, const void *src,
1741 size_t size, uint64_t iv, int fwd)
1743 memcpy (dst, src, size);
1748 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1749 * Decrypt data with the tunnel key.
1751 * @param t Tunnel whose key to use.
1752 * @param dst Destination for the plaintext.
1753 * @param src Source of the encrypted data.
1754 * @param size Size of the encrypted data.
1755 * @param iv Initialization Vector to use.
1756 * @param fwd Is this a fwd message?
1759 tunnel_decrypt (struct MeshTunnel2 *t,
1760 void *dst, const void *src,
1761 size_t size, uint64_t iv, int fwd)
1763 memcpy (dst, src, size);
1768 * Sends an already built message on a connection, properly registering
1769 * all used resources.
1771 * @param message Message to send. Function makes a copy of it.
1772 * If message is not hop-by-hop, decrements TTL of copy.
1773 * @param c Connection on which this message is transmitted.
1774 * @param ch Channel on which this message is transmitted, or NULL.
1775 * @param fwd Is this a fwd message?
1778 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1779 struct MeshConnection *c,
1780 struct MeshChannel *ch,
1787 size = ntohs (message->size);
1788 data = GNUNET_malloc (size);
1789 memcpy (data, message, size);
1790 type = ntohs (message->type);
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1792 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1796 struct GNUNET_MESH_Encrypted *emsg;
1797 struct GNUNET_MESH_ACK *amsg;
1798 struct GNUNET_MESH_Poll *pmsg;
1801 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1802 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1803 emsg = (struct GNUNET_MESH_Encrypted *) data;
1804 ttl = ntohl (emsg->ttl);
1807 GNUNET_break_op (0);
1811 emsg->ttl = htonl (ttl - 1);
1812 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1816 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1817 amsg = (struct GNUNET_MESH_ACK *) data;
1819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1822 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1823 pmsg = (struct GNUNET_MESH_Poll *) data;
1825 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1843 * Sends an already built message on a tunnel, choosing the best connection.
1845 * @param message Message to send. Function modifies it.
1846 * @param t Tunnel on which this message is transmitted.
1847 * @param ch Channel on which this message is transmitted.
1848 * @param fwd Is this a fwd message?
1851 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1852 struct MeshTunnel2 *t,
1853 struct MeshChannel *ch,
1856 struct MeshConnection *c;
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1861 c = tunnel_get_connection (t, fwd);
1864 GNUNET_break (GNUNET_YES == t->destroy);
1867 type = ntohs (msg->header.type);
1870 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1871 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1872 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1873 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1875 msg->ttl = htonl (default_ttl);
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1879 GNUNET_MESH_DEBUG_M2S (type));
1884 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1889 * Sends an already built message on a channel, properly registering
1890 * all used resources and encrypting the message with the tunnel's key.
1892 * @param message Message to send. Function makes a copy of it.
1893 * @param ch Channel on which this message is transmitted.
1894 * @param fwd Is this a fwd message?
1897 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1898 struct MeshChannel *ch,
1901 struct GNUNET_MESH_Encrypted *msg;
1902 size_t size = ntohs (message->size);
1903 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1908 peer2s (ch->t->peer), ch->gid);
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1910 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1911 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1912 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1914 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1915 msg->header.type = htons (type);
1916 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1917 msg->iv = GNUNET_htonll (iv);
1918 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1920 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1925 * Sends a CREATE CONNECTION message for a path to a peer.
1926 * Changes the connection and tunnel states if necessary.
1928 * @param connection Connection to create.
1931 send_connection_create (struct MeshConnection *connection)
1933 struct MeshTunnel2 *t;
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1938 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1939 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1940 (connection->path->length *
1941 sizeof (struct GNUNET_PeerIdentity)),
1946 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1947 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1948 if (MESH_CONNECTION_NEW == connection->state)
1949 connection_change_state (connection, MESH_CONNECTION_SENT);
1954 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1957 * @param connection Connection to confirm.
1958 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1961 send_connection_ack (struct MeshConnection *connection, int fwd)
1963 struct MeshTunnel2 *t;
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1968 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1969 sizeof (struct GNUNET_MESH_ConnectionACK),
1973 if (MESH_TUNNEL_NEW == t->state)
1974 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1979 * Core callback to write a pre-constructed data packet to core buffer
1981 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1982 * @param size Number of bytes available in buf.
1983 * @param buf Where the to write the message.
1985 * @return number of bytes written to buf
1988 send_core_data_raw (void *cls, size_t size, void *buf)
1990 struct GNUNET_MessageHeader *msg = cls;
1993 GNUNET_assert (NULL != msg);
1994 total_size = ntohs (msg->size);
1996 if (total_size > size)
2001 memcpy (buf, msg, total_size);
2008 * Function to send a create connection message to a peer.
2010 * @param c Connection to create.
2011 * @param size number of bytes available in buf
2012 * @param buf where the callee should write the message
2013 * @return number of bytes written to buf
2016 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
2018 struct GNUNET_MESH_ConnectionCreate *msg;
2019 struct GNUNET_PeerIdentity *peer_ptr;
2020 struct MeshPeerPath *p = c->path;
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
2026 sizeof (struct GNUNET_MESH_ConnectionCreate) +
2027 p->length * sizeof (struct GNUNET_PeerIdentity);
2029 if (size < size_needed || NULL == buf)
2034 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
2035 msg->header.size = htons (size_needed);
2036 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
2039 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2040 for (i = 0; i < p->length; i++)
2042 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
2052 * Creates a path ack message in buf and frees all unused resources.
2054 * @param c Connection to send an ACK on.
2055 * @param size number of bytes available in buf
2056 * @param buf where the callee should write the message
2058 * @return number of bytes written to buf
2061 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
2063 struct GNUNET_MESH_ConnectionACK *msg = buf;
2064 struct MeshTunnel2 *t = c->t;
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
2067 GNUNET_assert (NULL != t);
2068 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
2073 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
2074 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
2077 /* TODO add signature */
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
2080 return sizeof (struct GNUNET_MESH_ConnectionACK);
2085 * Destroy the peer_info and free any allocated resources linked to it
2087 * @param peer The peer_info to destroy.
2089 * @return GNUNET_OK on success
2092 peer_destroy (struct MeshPeer *peer)
2094 struct GNUNET_PeerIdentity id;
2095 struct MeshPeerPath *p;
2096 struct MeshPeerPath *nextp;
2098 GNUNET_PEER_resolve (peer->id, &id);
2099 GNUNET_PEER_change_rc (peer->id, -1);
2102 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2106 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2108 if (NULL != peer->dhtget)
2110 GNUNET_DHT_get_stop (peer->dhtget);
2112 p = peer->path_head;
2116 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2120 tunnel_destroy_empty (peer->tunnel);
2127 * Returns if peer is used (has a tunnel, is neighbor).
2129 * @peer Peer to check.
2131 * @return GNUNET_YES if peer is in use.
2134 peer_is_used (struct MeshPeer *peer)
2136 struct MeshPeerPath *p;
2138 if (NULL != peer->tunnel)
2141 for (p = peer->path_head; NULL != p; p = p->next)
2150 * Iterator over all the peers to get the oldest timestamp.
2152 * @param cls Closure (unsued).
2153 * @param key ID of the peer.
2154 * @param value Peer_Info of the peer.
2157 peer_get_oldest (void *cls,
2158 const struct GNUNET_HashCode *key,
2161 struct MeshPeer *p = value;
2162 struct GNUNET_TIME_Absolute *abs = cls;
2164 /* Don't count active peers */
2165 if (GNUNET_YES == peer_is_used (p))
2168 if (abs->abs_value_us < p->last_contact.abs_value_us)
2169 abs->abs_value_us = p->last_contact.abs_value_us;
2176 * Iterator over all the peers to remove the oldest entry.
2178 * @param cls Closure (unsued).
2179 * @param key ID of the peer.
2180 * @param value Peer_Info of the peer.
2183 peer_timeout (void *cls,
2184 const struct GNUNET_HashCode *key,
2187 struct MeshPeer *p = value;
2188 struct GNUNET_TIME_Absolute *abs = cls;
2190 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2191 GNUNET_NO == peer_is_used (p))
2201 * Delete oldest unused peer.
2204 peer_delete_oldest (void)
2206 struct GNUNET_TIME_Absolute abs;
2208 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2210 GNUNET_CONTAINER_multihashmap_iterate (peers,
2213 GNUNET_CONTAINER_multihashmap_iterate (peers,
2220 * Retrieve the MeshPeer stucture associated with the peer, create one
2221 * and insert it in the appropriate structures if the peer is not known yet.
2223 * @param peer Full identity of the peer.
2225 * @return Existing or newly created peer info.
2227 static struct MeshPeer *
2228 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2230 struct MeshPeer *peer;
2232 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2235 peer = GNUNET_new (struct MeshPeer);
2236 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2238 peer_delete_oldest ();
2240 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2242 peer->id = GNUNET_PEER_intern (peer_id);
2244 peer->last_contact = GNUNET_TIME_absolute_get();
2251 * Retrieve the MeshPeer stucture associated with the peer, create one
2252 * and insert it in the appropriate structures if the peer is not known yet.
2254 * @param peer Short identity of the peer.
2256 * @return Existing or newly created peer info.
2258 static struct MeshPeer *
2259 peer_get_short (const GNUNET_PEER_Id peer)
2261 return peer_get (GNUNET_PEER_resolve2 (peer));
2266 * Get a cost of a path for a peer considering existing tunnel connections.
2268 * @param peer Peer towards which the path is considered.
2269 * @param path Candidate path.
2271 * @return Cost of the path (path length + number of overlapping nodes)
2274 peer_get_path_cost (const struct MeshPeer *peer,
2275 const struct MeshPeerPath *path)
2277 struct MeshConnection *c;
2278 unsigned int overlap;
2286 GNUNET_assert (NULL != peer->tunnel);
2288 for (i = 0; i < path->length; i++)
2290 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2292 for (j = 0; j < c->path->length; j++)
2294 if (path->peers[i] == c->path->peers[j])
2302 return (path->length + overlap) * (path->score * -1);
2307 * Choose the best path towards a peer considering the tunnel properties.
2309 * @param peer The destination peer.
2311 * @return Best current known path towards the peer, if any.
2313 static struct MeshPeerPath *
2314 peer_get_best_path (const struct MeshPeer *peer)
2316 struct MeshPeerPath *best_p;
2317 struct MeshPeerPath *p;
2318 struct MeshConnection *c;
2319 unsigned int best_cost;
2322 best_cost = UINT_MAX;
2324 for (p = peer->path_head; NULL != p; p = p->next)
2326 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2330 continue; /* If path is in use in a connection, skip it. */
2332 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2342 queue_is_sendable (struct MeshPeerQueue *q)
2344 struct MeshFlowControl *fc;
2346 /* Is PID-independent? */
2349 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2350 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2354 /* Is PID allowed? */
2355 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2356 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2364 * Get first sendable message.
2366 * @param peer The destination peer.
2368 * @return Best current known path towards the peer, if any.
2370 static struct MeshPeerQueue *
2371 peer_get_first_message (const struct MeshPeer *peer)
2373 struct MeshPeerQueue *q;
2375 for (q = peer->queue_head; NULL != q; q = q->next)
2377 if (queue_is_sendable (q))
2386 * Try to establish a new connection to this peer in the given tunnel.
2387 * If the peer doesn't have any path to it yet, try to get one.
2388 * If the peer already has some path, send a CREATE CONNECTION towards it.
2390 * @param peer PeerInfo of the peer.
2393 peer_connect (struct MeshPeer *peer)
2395 struct MeshTunnel2 *t;
2396 struct MeshPeerPath *p;
2397 struct MeshConnection *c;
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "peer_connect towards %s\n",
2403 if (NULL != peer->path_head)
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2406 p = peer_get_best_path (peer);
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2410 c = tunnel_use_path (t, p);
2411 send_connection_create (c);
2414 else if (NULL == peer->dhtget)
2416 const struct GNUNET_PeerIdentity *id;
2418 id = GNUNET_PEER_resolve2 (peer->id);
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 " Starting DHT GET for peer %s\n", peer2s (peer));
2421 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2422 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2423 &id->hashPubKey, /* key to search */
2424 dht_replication_level, /* replication level */
2425 GNUNET_DHT_RO_RECORD_ROUTE |
2426 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2428 0, /* xquery bits */
2429 &dht_get_id_handler, peer);
2430 if (MESH_TUNNEL_NEW == t->state)
2431 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "There is no path but the DHT GET is already started.\n");
2442 * Get the first transmittable message for a connection.
2444 * @param c Connection.
2445 * @param fwd Is this FWD?
2447 * @return First transmittable message.
2449 static struct MeshPeerQueue *
2450 connection_get_first_message (struct MeshConnection *c, int fwd)
2452 struct MeshPeerQueue *q;
2455 p = connection_get_hop (c, fwd);
2457 for (q = p->queue_head; NULL != q; q = q->next)
2461 if (queue_is_sendable (q))
2470 * @brief Re-initiate traffic on this connection if necessary.
2472 * Check if there is traffic queued towards this peer
2473 * and the core transmit handle is NULL (traffic was stalled).
2474 * If so, call core tmt rdy.
2476 * @param c Connection on which initiate traffic.
2477 * @param fwd Is this about fwd traffic?
2480 connection_unlock_queue (struct MeshConnection *c, int fwd)
2482 struct MeshPeer *peer;
2483 struct MeshPeerQueue *q;
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "connection_unlock_queue %s on %s\n",
2488 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2490 if (connection_is_origin (c, fwd))
2492 struct MeshTunnel2 *t = c->t;
2493 struct MeshChannel *ch;
2494 struct MeshChannelReliability *rel;
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2497 /* FIXME randomize channel selection, not always first channel */
2498 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2500 rel = fwd ? ch->root_rel : ch->dest_rel;
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2503 ch->gid, rel->client_ready ? "ready " : "not ready");
2504 if (GNUNET_NO == rel->client_ready)
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2507 send_local_ack (ch, fwd);
2508 return; /* FIXME authorize all channels? */
2514 peer = connection_get_hop (c, fwd);
2516 if (NULL != peer->core_transmit)
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2519 return; /* Already unlocked */
2522 q = connection_get_first_message (c, fwd);
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2526 return; /* Nothing to transmit */
2530 peer->core_transmit =
2531 GNUNET_CORE_notify_transmit_ready (core_handle,
2534 GNUNET_TIME_UNIT_FOREVER_REL,
2535 GNUNET_PEER_resolve2 (peer->id),
2543 * Cancel all transmissions that belong to a certain connection.
2545 * @param c Connection which to cancel.
2546 * @param fwd Cancel fwd traffic?
2549 connection_cancel_queues (struct MeshConnection *c, int fwd)
2551 struct MeshPeerQueue *q;
2552 struct MeshPeerQueue *next;
2553 struct MeshFlowControl *fc;
2554 struct MeshPeer *peer;
2561 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2562 peer = connection_get_hop (c, fwd);
2564 for (q = peer->queue_head; NULL != q; q = next)
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2570 "connection_cancel_queue %s\n",
2571 GNUNET_MESH_DEBUG_M2S (q->type));
2572 queue_destroy (q, GNUNET_YES);
2575 if (NULL == peer->queue_head)
2577 if (NULL != peer->core_transmit)
2579 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2580 peer->core_transmit = NULL;
2582 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2584 GNUNET_SCHEDULER_cancel (fc->poll_task);
2585 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2592 * Remove all paths that rely on a direct connection between p1 and p2
2593 * from the peer itself and notify all tunnels about it.
2595 * @param peer PeerInfo of affected peer.
2596 * @param p1 GNUNET_PEER_Id of one peer.
2597 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2600 * TODO: optimize (see below)
2603 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2606 struct MeshPeerPath *p;
2607 struct MeshPeerPath *next;
2608 struct MeshPeer *peer_d;
2610 unsigned int destroyed;
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2615 for (p = peer->path_head; NULL != p; p = next)
2618 for (i = 0; i < (p->length - 1); i++)
2620 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2621 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2623 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2634 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2636 peer_d = peer_get_short (d); // FIXME
2637 next = peer_get_best_path (peer_d);
2638 tunnel_use_path (peer->tunnel, next);
2639 peer_connect (peer_d);
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2646 * Add the path to the peer and update the path used to reach it in case this
2649 * @param peer_info Destination peer to add the path to.
2650 * @param path New path to add. Last peer must be the peer in arg 1.
2651 * Path will be either used of freed if already known.
2652 * @param trusted Do we trust that this path is real?
2655 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2658 struct MeshPeerPath *aux;
2662 if ((NULL == peer_info) || (NULL == path))
2665 path_destroy (path);
2668 if (path->peers[path->length - 1] != peer_info->id)
2671 path_destroy (path);
2674 if (2 >= path->length && GNUNET_NO == trusted)
2676 /* Only allow CORE to tell us about direct paths */
2677 path_destroy (path);
2680 for (l = 1; l < path->length; l++)
2682 if (path->peers[l] == myid)
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2685 for (l2 = 0; l2 < path->length - l; l2++)
2687 path->peers[l2] = path->peers[l + l2];
2692 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2697 path->length, peer2s (peer_info));
2699 l = path_get_length (path);
2702 path_destroy (path);
2706 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2707 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2709 l2 = path_get_length (aux);
2712 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2713 peer_info->path_tail, aux, path);
2718 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2720 path_destroy (path);
2725 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2732 * Add the path to the origin peer and update the path used to reach it in case
2733 * this is the shortest.
2734 * The path is given in peer_info -> destination, therefore we turn the path
2735 * upside down first.
2737 * @param peer_info Peer to add the path to, being the origin of the path.
2738 * @param path New path to add after being inversed.
2739 * Path will be either used or freed.
2740 * @param trusted Do we trust that this path is real?
2743 peer_add_path_to_origin (struct MeshPeer *peer_info,
2744 struct MeshPeerPath *path, int trusted)
2747 peer_add_path (peer_info, path, trusted);
2753 * Function called if a connection has been stalled for a while,
2754 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2756 * @param cls Closure (poll ctx).
2757 * @param tc TaskContext.
2760 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2762 struct MeshFlowControl *fc = cls;
2763 struct GNUNET_MESH_Poll msg;
2764 struct MeshConnection *c;
2766 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2775 peer2s (c->t->peer), c->id);
2777 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2778 msg.header.size = htons (sizeof (msg));
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2780 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2781 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2782 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2783 &connection_poll, fc);
2788 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2789 * to obtain a local peer -> destination path and interning the peer ids.
2791 * @return Newly allocated and created path
2793 static struct MeshPeerPath *
2794 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2795 unsigned int get_path_length,
2796 const struct GNUNET_PeerIdentity *put_path,
2797 unsigned int put_path_length)
2799 struct MeshPeerPath *p;
2805 GNUNET_PEER_change_rc (myid, 1);
2806 i = get_path_length;
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2808 for (i--; i >= 0; i--)
2810 id = GNUNET_PEER_intern (&get_path[i]);
2811 if (p->length > 0 && id == p->peers[p->length - 1])
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2814 GNUNET_PEER_change_rc (id, -1);
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2819 GNUNET_i2s (&get_path[i]));
2821 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2822 p->peers[p->length - 1] = id;
2825 i = put_path_length;
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2827 for (i--; i >= 0; i--)
2829 id = GNUNET_PEER_intern (&put_path[i]);
2832 /* PUT path went through us, so discard the path up until now and start
2833 * from here to get a much shorter (and loop-free) path.
2838 if (p->length > 0 && id == p->peers[p->length - 1])
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2841 GNUNET_PEER_change_rc (id, -1);
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2846 GNUNET_i2s (&put_path[i]));
2848 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2849 p->peers[p->length - 1] = id;
2853 if (get_path_length > 0)
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2855 GNUNET_i2s (&get_path[0]));
2856 if (put_path_length > 0)
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2858 GNUNET_i2s (&put_path[0]));
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2861 for (i = 0; i < p->length; i++)
2863 struct GNUNET_PeerIdentity peer_id;
2865 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2867 GNUNET_i2s (&peer_id));
2875 * Adds a path to the peer_infos of all the peers in the path
2877 * @param p Path to process.
2878 * @param confirmed Whether we know if the path works or not.
2881 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2885 /* TODO: invert and add */
2886 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2887 for (i++; i < p->length; i++)
2889 struct MeshPeer *aux;
2890 struct MeshPeerPath *copy;
2892 aux = peer_get_short (p->peers[i]);
2893 copy = path_duplicate (p);
2894 copy->length = i + 1;
2895 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2901 * Search for a channel among the channels for a client
2903 * @param c the client whose channels to search in
2904 * @param chid the local id of the channel
2906 * @return channel handler, NULL if doesn't exist
2908 static struct MeshChannel *
2909 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2912 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2914 GNUNET_break_op (0);
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2918 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2919 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2920 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2925 channel_debug (struct MeshChannel *ch)
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel %X\n", ch->gid);
2933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2934 ch->root, ch->root_rel);
2935 if (NULL != ch->root)
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->root->id);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
2939 ch->root_rel->client_ready ? "YES" : "NO");
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_root);
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " root %p/%p\n",
2943 ch->dest, ch->dest_rel);
2944 if (NULL != ch->dest)
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cli %u\n", ch->dest->id);
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ready %s\n",
2948 ch->dest_rel->client_ready ? "YES" : "NO");
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id %X\n", ch->lid_dest);
2956 * Search for a tunnel by global ID using full PeerIdentities.
2958 * @param t Tunnel containing the channel.
2959 * @param chid Public channel number.
2961 * @return channel handler, NULL if doesn't exist
2963 static struct MeshChannel *
2964 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2966 struct MeshChannel *ch;
2971 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2973 if (ch->gid == chid)
2982 * Change the tunnel state.
2984 * @param t Tunnel whose state to change.
2985 * @param state New state.
2988 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2993 "Tunnel %s state was %s\n",
2995 GNUNET_MESH_DEBUG_TS2S (t->state));
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2997 "Tunnel %s state is now %s\n",
2999 GNUNET_MESH_DEBUG_TS2S (state));
3005 * Cache a message to be sent once tunnel is online.
3007 * @param t Tunnel to hold the message.
3008 * @param ch Channel the message is about.
3009 * @param msg Message itself (copy will be made).
3010 * @param fwd Is this fwd?
3013 tunnel_queue_data (struct MeshTunnel2 *t,
3014 struct MeshChannel *ch,
3015 struct GNUNET_MessageHeader *msg,
3018 struct MeshTunnelQueue *tq;
3019 uint16_t size = ntohs (msg->size);
3021 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
3024 memcpy (&tq[1], msg, size);
3025 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
3030 * Send all cached messages that we can, tunnel is online.
3032 * @param t Tunnel that holds the messages.
3033 * @param fwd Is this fwd?
3036 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
3038 struct MeshTunnelQueue *tq;
3039 struct MeshTunnelQueue *next;
3042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3043 "tunnel_send_queued_data on tunnel %s\n",
3045 room = tunnel_get_buffer (t, fwd);
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
3047 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
3051 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
3052 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
3061 connection_change_state (struct MeshConnection* c,
3062 enum MeshConnectionState state)
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3065 "Connection %s state was %s\n",
3066 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3068 "Connection %s state is now %s\n",
3069 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
3075 * Add a client to a channel, initializing all needed data structures.
3077 * @param ch Channel to which add the client.
3078 * @param c Client which to add to the channel.
3081 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3083 struct MeshTunnel2 *t = ch->t;
3085 if (NULL != ch->dest)
3091 /* Assign local id as destination */
3092 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3093 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3094 ch->lid_dest = t->next_local_chid++;
3095 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3097 /* Store in client's hashmap */
3099 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3101 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3107 GNUNET_break (NULL == ch->dest_rel);
3108 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3109 ch->dest_rel->ch = ch;
3110 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3116 static struct MeshConnection *
3117 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3119 struct MeshConnection *c;
3120 struct GNUNET_HashCode cid;
3121 struct MeshPeer *peer;
3122 unsigned int own_pos;
3124 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3126 c = connection_new (&cid);
3128 for (own_pos = 0; own_pos < p->length; own_pos++)
3130 if (p->peers[own_pos] == myid)
3133 if (own_pos > p->length - 1)
3136 connection_destroy (c);
3139 c->own_pos = own_pos;
3144 c->fwd_maintenance_task =
3145 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3146 &connection_fwd_keepalive, c);
3149 peer = connection_get_next_hop (c);
3150 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3152 peer = connection_get_prev_hop (c);
3153 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3154 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3160 * Notifies a tunnel that a connection has broken that affects at least
3161 * some of its peers. Sends a notification towards the root of the tree.
3162 * In case the peer is the owner of the tree, notifies the client that owns
3163 * the tunnel and tries to reconnect.
3165 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3167 * @param t Tunnel affected.
3168 * @param p1 Peer that got disconnected from p2.
3169 * @param p2 Peer that got disconnected from p1.
3171 * @return Short ID of the peer disconnected (either p1 or p2).
3172 * 0 if the tunnel remained unaffected.
3174 static GNUNET_PEER_Id
3175 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3176 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3178 // if (myid != p1 && myid != p2) FIXME
3183 // if (tree_get_predecessor (t->tree) != 0)
3185 // /* We are the peer still connected, notify owner of the disconnection. */
3186 // struct GNUNET_MESH_PathBroken msg;
3187 // struct GNUNET_PeerIdentity neighbor;
3189 // msg.header.size = htons (sizeof (msg));
3190 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3191 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3192 // msg.tid = htonl (t->id.tid);
3193 // msg.peer1 = my_full_id;
3194 // GNUNET_PEER_resolve (pid, &msg.peer2);
3195 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3196 // send_prebuilt_message (&msg.header, &neighbor, t);
3203 * Send an end-to-end ACK message for the most recent in-sequence payload.
3205 * If channel is not reliable, do nothing.
3207 * @param ch Channel this is about.
3208 * @param fwd Is for FWD traffic? (ACK dest->owner)
3211 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3213 struct GNUNET_MESH_DataACK msg;
3214 struct MeshChannelReliability *rel;
3215 struct MeshReliableMessage *copy;
3220 if (GNUNET_NO == ch->reliable)
3224 rel = fwd ? ch->dest_rel : ch->root_rel;
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3226 "send_data_ack for %u\n",
3229 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3230 msg.header.type = htons (type);
3231 msg.header.size = htons (sizeof (msg));
3232 msg.chid = htonl (ch->gid);
3233 msg.mid = htonl (rel->mid_recv - 1);
3235 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3237 if (copy->type != type)
3239 delta = copy->mid - rel->mid_recv;
3242 mask = 0x1LL << delta;
3243 msg.futures |= mask;
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3245 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3246 copy->mid, delta, mask, msg.futures);
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3250 send_prebuilt_message_channel (&msg.header, ch, fwd);
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3256 * Send an ACK informing the predecessor about the available buffer space.
3258 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3259 * the ACK itself goes "back" (dest->root).
3261 * @param c Connection on which to send the ACK.
3262 * @param buffer How much space free to advertise?
3263 * @param fwd Is this FWD ACK? (Going dest->owner)
3266 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3268 struct MeshFlowControl *next_fc;
3269 struct MeshFlowControl *prev_fc;
3270 struct GNUNET_MESH_ACK msg;
3274 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3275 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 "connection send %s ack on %s\n",
3279 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3281 /* Check if we need to transmit the ACK */
3282 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3286 " last pid recv: %u, last ack sent: %u\n",
3287 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3291 /* Ok, ACK might be necessary, what PID to ACK? */
3292 delta = next_fc->queue_max - next_fc->queue_n;
3293 ack = prev_fc->last_pid_recv + delta;
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 " last pid %u, last ack %u, qmax %u, q %u\n",
3297 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3298 next_fc->queue_max, next_fc->queue_n);
3299 if (ack == prev_fc->last_ack_sent)
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3305 prev_fc->last_ack_sent = ack;
3307 /* Build ACK message and send on connection */
3308 msg.header.size = htons (sizeof (msg));
3309 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3310 msg.ack = htonl (ack);
3313 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3318 * Modify the mesh message TID from global to local and send to client.
3320 * @param ch Channel on which to send the message.
3321 * @param msg Message to modify and send.
3322 * @param c Client to send to.
3323 * @param tid Tunnel ID to use (c can be both owner and client).
3326 channel_send_client_to_tid (struct MeshChannel *ch,
3327 const struct GNUNET_MESH_Data *msg,
3328 struct MeshClient *c, MESH_ChannelNumber id)
3330 struct GNUNET_MESH_LocalData *copy;
3331 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3332 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3334 if (size < sizeof (struct GNUNET_MessageHeader))
3336 GNUNET_break_op (0);
3344 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3345 memcpy (©[1], &msg[1], size);
3346 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3347 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3348 copy->id = htonl (id);
3349 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3350 ©->header, GNUNET_NO);
3355 * We have received a message out of order, or the client is not ready.
3356 * Buffer it until we receive an ACK from the client or the missing
3357 * message from the channel.
3359 * @param msg Message to buffer (MUST be of type MESH_DATA).
3360 * @param rel Reliability data to the corresponding direction.
3363 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3364 struct MeshChannelReliability *rel)
3366 struct MeshReliableMessage *copy;
3367 struct MeshReliableMessage *prev;
3371 size = ntohs (msg->header.size);
3372 mid = ntohl (msg->mid);
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3376 copy = GNUNET_malloc (sizeof (*copy) + size);
3379 memcpy (©[1], msg, size);
3383 // FIXME do something better than O(n), although n < 64...
3384 // FIXME start from the end (most messages are the latest ones)
3385 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3388 if (GMC_is_pid_bigger (prev->mid, mid))
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3391 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3397 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3403 * Modify the data message ID from global to local and send to client.
3405 * @param ch Channel on which to send the message.
3406 * @param msg Message to modify and send.
3407 * @param fwd Forward?
3410 channel_send_client_data (struct MeshChannel *ch,
3411 const struct GNUNET_MESH_Data *msg,
3416 if (ch->dest_rel->client_ready)
3417 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3419 channel_rel_add_buffered_data (msg, ch->dest_rel);
3423 if (ch->root_rel->client_ready)
3424 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3426 channel_rel_add_buffered_data (msg, ch->root_rel);
3432 * Send a buffered message to the client, for in order delivery or
3433 * as result of client ACK.
3435 * @param ch Channel on which to empty the message buffer.
3436 * @param c Client to send to.
3437 * @param rel Reliability structure to corresponding peer.
3438 * If rel == bck_rel, this is FWD data.
3441 channel_send_client_buffered_data (struct MeshChannel *ch,
3442 struct MeshClient *c,
3445 struct MeshReliableMessage *copy;
3446 struct MeshChannelReliability *rel;
3448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3449 rel = fwd ? ch->dest_rel : ch->root_rel;
3450 if (GNUNET_NO == rel->client_ready)
3452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3456 copy = rel->head_recv;
3457 /* We never buffer channel management messages */
3460 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3462 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3465 " have %u! now expecting %u\n",
3466 copy->mid, rel->mid_recv + 1);
3467 channel_send_client_data (ch, msg, fwd);
3470 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3476 " reliable && don't have %u, next is %u\n",
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3487 * Destroy a reliable message after it has been acknowledged, either by
3488 * direct mid ACK or bitfield. Updates the appropriate data structures and
3489 * timers and frees all memory.
3491 * @param copy Message that is no longer needed: remote peer got it.
3494 rel_message_free (struct MeshReliableMessage *copy)
3496 struct MeshChannelReliability *rel;
3497 struct GNUNET_TIME_Relative time;
3500 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3501 rel->expected_delay.rel_value_us *= 7;
3502 rel->expected_delay.rel_value_us += time.rel_value_us;
3503 rel->expected_delay.rel_value_us /= 8;
3505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3508 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3510 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3512 rel->retry_timer = rel->expected_delay;
3513 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3519 * Destroy all reliable messages queued for a channel,
3520 * during a channel destruction.
3521 * Frees the reliability structure itself.
3523 * @param rel Reliability data for a channel.
3526 channel_rel_free_all (struct MeshChannelReliability *rel)
3528 struct MeshReliableMessage *copy;
3529 struct MeshReliableMessage *next;
3534 for (copy = rel->head_recv; NULL != copy; copy = next)
3537 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3540 for (copy = rel->head_sent; NULL != copy; copy = next)
3543 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3546 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3547 GNUNET_SCHEDULER_cancel (rel->retry_task);
3553 * Mark future messages as ACK'd.
3555 * @param rel Reliability data.
3556 * @param msg DataACK message with a bitfield of future ACK'd messages.
3559 channel_rel_free_sent (struct MeshChannelReliability *rel,
3560 const struct GNUNET_MESH_DataACK *msg)
3562 struct MeshReliableMessage *copy;
3563 struct MeshReliableMessage *next;
3570 bitfield = msg->futures;
3571 mid = ntohl (msg->mid);
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "free_sent_reliable %u %llX\n",
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3576 " rel %p, head %p\n",
3577 rel, rel->head_sent);
3578 for (i = 0, copy = rel->head_sent;
3579 i < 64 && NULL != copy && 0 != bitfield;
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 " trying bit %u (mid %u)\n",
3586 if (0 == (bitfield & mask))
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3590 /* Bit was set, clear the bit from the bitfield */
3593 /* The i-th bit was set. Do we have that copy? */
3594 /* Skip copies with mid < target */
3595 target = mid + i + 1;
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3597 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3600 /* Did we run out of copies? (previously freed, it's ok) */
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3607 /* Did we overshoot the target? (previously freed, it's ok) */
3608 if (GMC_is_pid_bigger (copy->mid, target))
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3614 /* Now copy->mid == target, free it */
3616 rel_message_free (copy);
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3624 * We haven't received an ACK after a certain time: restransmit the message.
3626 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3627 * @param tc TaskContext.
3630 channel_retransmit_message (void *cls,
3631 const struct GNUNET_SCHEDULER_TaskContext *tc)
3633 struct MeshChannelReliability *rel = cls;
3634 struct MeshReliableMessage *copy;
3635 struct MeshPeerQueue *q;
3636 struct MeshChannel *ch;
3637 struct MeshConnection *c;
3638 struct GNUNET_MESH_Data *payload;
3639 struct MeshPeer *hop;
3642 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3643 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3647 copy = rel->head_sent;
3654 /* Search the message to be retransmitted in the outgoing queue.
3655 * Check only the queue for the connection that is going to be used,
3656 * if the message is stuck in some other connection's queue we shouldn't
3658 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3659 * the old connection could be temporary stalled or the queue happened to
3660 * be long at time of insertion.
3661 * - not sending the new one could cause terrible delays the old connection
3664 payload = (struct GNUNET_MESH_Data *) ©[1];
3665 fwd = (rel == ch->root_rel);
3666 c = tunnel_get_connection (ch->t, fwd);
3667 hop = connection_get_hop (c, fwd);
3668 for (q = hop->queue_head; NULL != q; q = q->next)
3670 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3672 struct GNUNET_MESH_Data *queued_data = q->cls;
3674 if (queued_data->mid == payload->mid)
3679 /* Message not found in the queue that we are going to use. */
3682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3684 send_prebuilt_message_channel (&payload->header, ch, fwd);
3685 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3692 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3693 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3694 &channel_retransmit_message,
3700 * Send ACK on one or more connections due to buffer space to the client.
3702 * Iterates all connections of the tunnel and sends ACKs appropriately.
3704 * @param ch Channel which has some free buffer space.
3705 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3708 channel_send_connections_ack (struct MeshChannel *ch,
3709 unsigned int buffer,
3712 struct MeshTunnel2 *t = ch->t;
3713 struct MeshConnection *c;
3714 struct MeshFlowControl *fc;
3717 uint32_t allow_per_connection;
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3721 "Channel send connection %s ack on %s:%X\n",
3722 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3724 /* Count connections, how many messages are already allowed */
3725 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3727 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3728 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3733 allowed += fc->last_ack_sent - fc->last_pid_recv;
3737 /* Make sure there is no overflow */
3738 if (allowed > buffer)
3744 /* Authorize connections to send more data */
3745 to_allow = buffer - allowed;
3747 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3749 allow_per_connection = to_allow/cs;
3750 to_allow -= allow_per_connection;
3752 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3753 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3757 connection_send_ack (c, allow_per_connection, fwd);
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3761 "Channel send connection %s ack on %s:%X\n",
3762 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3763 GNUNET_break (to_allow == 0);
3768 * Send an ACK on the appropriate connection/channel, depending on
3769 * the direction and the position of the peer.
3771 * @param c Which connection to send the hop-by-hop ACK.
3772 * @param ch Channel, if any.
3773 * @param fwd Is this a fwd ACK? (will go dest->root)
3776 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3778 unsigned int buffer;
3780 if (NULL == c || connection_is_terminal (c, fwd))
3782 buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
3786 GNUNET_assert (NULL != c);
3787 buffer = connection_get_buffer (c, fwd);
3792 GNUNET_assert (NULL != ch);
3793 channel_send_connections_ack (ch, buffer, fwd);
3795 else if (connection_is_origin (c, fwd))
3799 GNUNET_assert (NULL != ch);
3800 send_local_ack (ch, fwd);
3805 connection_send_ack (c, buffer, fwd);
3811 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3813 * @param ch Channel to mark as ready.
3814 * @param fwd Was the CREATE message sent fwd?
3817 channel_confirm (struct MeshChannel *ch, int fwd)
3819 struct MeshChannelReliability *rel;
3820 struct MeshReliableMessage *copy;
3821 struct MeshReliableMessage *next;
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3824 " channel confirm %s %s:%X\n",
3825 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3826 ch->state = MESH_CHANNEL_READY;
3828 rel = fwd ? ch->root_rel : ch->dest_rel;
3829 for (copy = rel->head_sent; NULL != copy; copy = next)
3831 struct GNUNET_MessageHeader *msg;
3834 msg = (struct GNUNET_MessageHeader *) ©[1];
3835 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3837 rel_message_free (copy);
3841 if (GNUNET_NO == rel->client_ready)
3842 send_local_ack (ch, fwd);
3847 * Save a copy to retransmit in case it gets lost.
3849 * Initializes all needed callbacks and timers.
3851 * @param ch Channel this message goes on.
3852 * @param msg Message to copy.
3853 * @param fwd Is this fwd traffic?
3856 channel_save_copy (struct MeshChannel *ch,
3857 const struct GNUNET_MessageHeader *msg,
3860 struct MeshChannelReliability *rel;
3861 struct MeshReliableMessage *copy;
3866 rel = fwd ? ch->root_rel : ch->dest_rel;
3867 mid = rel->mid_send;
3868 type = ntohs (msg->type);
3869 size = ntohs (msg->size);
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3872 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3874 copy->timestamp = GNUNET_TIME_absolute_get ();
3877 memcpy (©[1], msg, size);
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3880 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3881 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3884 GNUNET_TIME_relative_multiply (rel->expected_delay,
3885 MESH_RETRANSMIT_MARGIN);
3887 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3888 &channel_retransmit_message,
3895 * Send keepalive packets for a connection.
3897 * @param c Connection to keep alive..
3898 * @param fwd Is this a FWD keepalive? (owner -> dest).
3901 connection_keepalive (struct MeshConnection *c, int fwd)
3903 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3904 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3908 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3909 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3912 "sending %s keepalive for connection %s[%d]\n",
3913 fwd ? "FWD" : "BCK",
3914 peer2s (c->t->peer),
3917 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3918 msg->header.size = htons (size);
3919 msg->header.type = htons (type);
3922 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3927 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3929 * @param c Connection for which to send the message.
3930 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3933 connection_recreate (struct MeshConnection *c, int fwd)
3935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3937 send_connection_create (c);
3939 send_connection_ack (c, GNUNET_NO);
3944 * Generic connection timer management.
3945 * Depending on the role of the peer in the connection will send the
3946 * appropriate message (build or keepalive)
3948 * @param c Conncetion to maintain.
3949 * @param fwd Is FWD?
3952 connection_maintain (struct MeshConnection *c, int fwd)
3954 if (MESH_TUNNEL_SEARCHING == c->t->state)
3956 /* TODO DHT GET with RO_BART */
3961 case MESH_CONNECTION_NEW:
3963 case MESH_CONNECTION_SENT:
3964 connection_recreate (c, fwd);
3966 case MESH_CONNECTION_READY:
3967 connection_keepalive (c, fwd);
3976 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3978 struct MeshConnection *c = cls;
3980 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3981 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3984 connection_maintain (c, GNUNET_YES);
3985 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3986 &connection_fwd_keepalive,
3992 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3994 struct MeshConnection *c = cls;
3996 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3997 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4000 connection_maintain (c, GNUNET_NO);
4001 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
4002 &connection_bck_keepalive,
4008 * Send a message to all peers in this connection that the connection
4009 * is no longer valid.
4011 * If some peer should not receive the message, it should be zero'ed out
4012 * before calling this function.
4014 * @param c The connection whose peers to notify.
4017 connection_send_destroy (struct MeshConnection *c)
4019 struct GNUNET_MESH_ConnectionDestroy msg;
4021 msg.header.size = htons (sizeof (msg));
4022 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 " sending connection destroy for connection %s[%X]\n",
4026 peer2s (c->t->peer),
4029 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
4030 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
4031 c->destroy = GNUNET_YES;
4036 * Confirm we got a channel create.
4038 * @param ch The channel to confirm.
4039 * @param fwd Should we send the ACK fwd?
4042 channel_send_ack (struct MeshChannel *ch, int fwd)
4044 struct GNUNET_MESH_ChannelManage msg;
4046 msg.header.size = htons (sizeof (msg));
4047 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
4048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4049 " sending channel ack for channel %s:%X\n",
4050 peer2s (ch->t->peer),
4053 msg.chid = htonl (ch->gid);
4054 send_prebuilt_message_channel (&msg.header, ch, fwd);
4059 * Send a message to all clients (local and remote) of this channel
4060 * notifying that the channel is no longer valid.
4062 * If some peer or client should not receive the message,
4063 * should be zero'ed out before calling this function.
4065 * @param ch The channel whose clients to notify.
4068 channel_send_destroy (struct MeshChannel *ch)
4070 struct GNUNET_MESH_ChannelManage msg;
4072 msg.header.size = htons (sizeof (msg));
4073 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 " sending channel destroy for channel %s:%X\n",
4076 peer2s (ch->t->peer),
4079 if (NULL != ch->root)
4081 msg.chid = htonl (ch->lid_root);
4082 send_local_channel_destroy (ch, GNUNET_NO);
4086 msg.chid = htonl (ch->gid);
4087 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4090 if (NULL != ch->dest)
4092 msg.chid = htonl (ch->lid_dest);
4093 send_local_channel_destroy (ch, GNUNET_YES);
4097 msg.chid = htonl (ch->gid);
4098 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4106 static struct MeshTunnel2 *
4109 struct MeshTunnel2 *t;
4111 t = GNUNET_new (struct MeshTunnel2);
4113 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4115 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4116 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4118 // GNUNET_break (0);
4119 // tunnel_destroy (t);
4123 // char salt[] = "salt";
4124 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4125 // salt, sizeof (salt),
4126 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4127 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4128 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4130 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4131 // salt, sizeof (salt),
4132 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4133 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4134 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4142 * Add a connection to a tunnel.
4145 * @param c Connection.
4148 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4151 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4156 * Initialize a Flow Control structure to the initial state.
4158 * @param fc Flow Control structure to initialize.
4161 fc_init (struct MeshFlowControl *fc)
4164 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4165 fc->last_pid_recv = (uint32_t) -1;
4166 fc->last_ack_sent = (uint32_t) 0;
4167 fc->last_ack_recv = (uint32_t) 0;
4168 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4169 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4171 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4175 static struct MeshConnection *
4176 connection_new (const struct GNUNET_HashCode *cid)
4178 struct MeshConnection *c;
4180 c = GNUNET_new (struct MeshConnection);
4182 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4184 fc_init (&c->fwd_fc);
4185 fc_init (&c->bck_fc);
4194 * Find a connection.
4196 * @param cid Connection ID.
4198 static struct MeshConnection *
4199 connection_get (const struct GNUNET_HashCode *cid)
4201 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4206 connection_destroy (struct MeshConnection *c)
4208 struct MeshPeer *peer;
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4214 peer2s (c->t->peer),
4217 /* Cancel all traffic */
4218 connection_cancel_queues (c, GNUNET_YES);
4219 connection_cancel_queues (c, GNUNET_NO);
4221 /* Cancel maintainance task (keepalive/timeout) */
4222 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4223 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4224 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4225 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4227 /* Deregister from neighbors */
4228 peer = connection_get_next_hop (c);
4230 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4231 peer = connection_get_prev_hop (c);
4233 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4236 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4237 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4243 tunnel_destroy (struct MeshTunnel2 *t)
4245 struct MeshConnection *c;
4246 struct MeshConnection *next;
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4254 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4255 // GNUNET_break (0);
4257 for (c = t->connection_head; NULL != c; c = next)
4260 connection_destroy (c);
4263 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4270 * Tunnel is empty: destroy it.
4272 * Notifies all connections about the destruction.
4274 * @param t Tunnel to destroy.
4277 tunnel_destroy_empty (struct MeshTunnel2 *t)
4279 struct MeshConnection *c;
4281 for (c = t->connection_head; NULL != c; c = c->next)
4283 if (GNUNET_NO == c->destroy)
4284 connection_send_destroy (c);
4287 if (0 == t->pending_messages)
4290 t->destroy = GNUNET_YES;
4295 * Destroy tunnel if empty (no more channels).
4297 * @param t Tunnel to destroy if empty.
4300 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4302 if (NULL != t->channel_head)
4305 tunnel_destroy_empty (t);
4310 * Destroy a channel and free all resources.
4312 * @param ch Channel to destroy.
4315 channel_destroy (struct MeshChannel *ch)
4317 struct MeshClient *c;
4325 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4336 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4343 channel_rel_free_all (ch->root_rel);
4344 channel_rel_free_all (ch->dest_rel);
4346 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4347 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4353 * Create a new channel.
4355 * @param t Tunnel this channel is in.
4356 * @param owner Client that owns the channel, NULL for foreign channels.
4357 * @param lid_root Local ID for root client.
4359 * @return A new initialized channel. NULL on error.
4361 static struct MeshChannel *
4362 channel_new (struct MeshTunnel2 *t,
4363 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4365 struct MeshChannel *ch;
4367 ch = GNUNET_new (struct MeshChannel);
4369 ch->lid_root = lid_root;
4372 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4374 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4378 while (NULL != channel_get (t, t->next_chid))
4379 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4380 ch->gid = t->next_chid;
4381 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4384 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4388 channel_destroy (ch);
4389 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4399 * Set options in a channel, extracted from a bit flag field
4401 * @param ch Channel to set options to.
4402 * @param options Bit array in host byte order.
4405 channel_set_options (struct MeshChannel *ch, uint32_t options)
4407 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4408 GNUNET_YES : GNUNET_NO;
4409 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4410 GNUNET_YES : GNUNET_NO;
4415 * Iterator for deleting each channel whose client endpoint disconnected.
4417 * @param cls Closure (client that has disconnected).
4418 * @param key The local channel id (used to access the hashmap).
4419 * @param value The value stored at the key (channel to destroy).
4421 * @return GNUNET_OK, keep iterating.
4424 channel_destroy_iterator (void *cls,
4428 struct MeshChannel *ch = value;
4429 struct MeshClient *c = cls;
4430 struct MeshTunnel2 *t;
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4433 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4434 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4448 channel_send_destroy (ch);
4449 channel_destroy (ch);
4450 tunnel_destroy_if_empty (t);
4457 * Remove client's ports from the global hashmap on disconnect.
4459 * @param cls Closure (unused).
4461 * @param value Client structure.
4463 * @return GNUNET_OK, keep iterating.
4466 client_release_ports (void *cls,
4472 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4473 if (GNUNET_YES != res)
4476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4477 "Port %u by client %p was not registered.\n",
4485 * Timeout function due to lack of keepalive/traffic from the owner.
4486 * Destroys connection if called.
4488 * @param cls Closure (connection to destroy).
4489 * @param tc TaskContext.
4492 connection_fwd_timeout (void *cls,
4493 const struct GNUNET_SCHEDULER_TaskContext *tc)
4495 struct MeshConnection *c = cls;
4497 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4498 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4501 "Connection %s[%X] FWD timed out. Destroying.\n",
4502 peer2s (c->t->peer),
4505 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4508 connection_destroy (c);
4513 * Timeout function due to lack of keepalive/traffic from the destination.
4514 * Destroys connection if called.
4516 * @param cls Closure (connection to destroy).
4517 * @param tc TaskContext
4520 connection_bck_timeout (void *cls,
4521 const struct GNUNET_SCHEDULER_TaskContext *tc)
4523 struct MeshConnection *c = cls;
4525 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4526 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4530 "Connection %s[%X] FWD timed out. Destroying.\n",
4531 peer2s (c->t->peer),
4534 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4537 connection_destroy (c);
4542 * Resets the connection timeout task, some other message has done the
4544 * - For the first peer on the direction this means to send
4545 * a keepalive or a path confirmation message (either create or ACK).
4546 * - For all other peers, this means to destroy the connection,
4547 * due to lack of activity.
4548 * Starts the tiemout if no timeout was running (connection just created).
4550 * @param c Connection whose timeout to reset.
4551 * @param fwd Is this forward?
4553 * TODO use heap to improve efficiency of scheduler.
4556 connection_reset_timeout (struct MeshConnection *c, int fwd)
4558 GNUNET_SCHEDULER_TaskIdentifier *ti;
4559 GNUNET_SCHEDULER_Task f;
4561 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4563 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4564 GNUNET_SCHEDULER_cancel (*ti);
4566 if (connection_is_origin (c, fwd)) /* Endpoint */
4568 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4569 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4573 struct GNUNET_TIME_Relative delay;
4575 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4576 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4577 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4583 * Iterator to notify all connections of a broken link. Mark connections
4584 * to destroy after all traffic has been sent.
4586 * @param cls Closure (peer disconnected).
4587 * @param key Current key code (tid).
4588 * @param value Value in the hash map (connection).
4590 * @return GNUNET_YES if we should continue to iterate,
4594 connection_broken (void *cls,
4595 const struct GNUNET_HashCode *key,
4598 struct MeshPeer *peer = cls;
4599 struct MeshConnection *c = value;
4600 struct GNUNET_MESH_ConnectionBroken msg;
4603 fwd = peer == connection_get_prev_hop (c);
4604 connection_cancel_queues (c, !fwd);
4606 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4607 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4609 msg.peer1 = my_full_id;
4610 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4611 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4612 c->destroy = GNUNET_YES;
4617 /******************************************************************************/
4618 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4619 /******************************************************************************/
4622 * Free a transmission that was already queued with all resources
4623 * associated to the request.
4625 * @param queue Queue handler to cancel.
4626 * @param clear_cls Is it necessary to free associated cls?
4629 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4631 struct MeshPeer *peer;
4632 struct MeshFlowControl *fc;
4637 GNUNET_assert (NULL != queue->c);
4638 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4640 if (GNUNET_YES == clear_cls)
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4643 GNUNET_MESH_DEBUG_M2S (queue->type));
4644 switch (queue->type)
4646 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4647 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4648 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4649 GNUNET_break (GNUNET_YES == queue->c->destroy);
4651 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4652 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4653 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4654 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4655 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4656 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4658 GNUNET_free_non_null (queue->cls);
4663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4664 GNUNET_MESH_DEBUG_M2S (queue->type));
4668 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4670 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4671 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4677 if (NULL != queue->c)
4679 queue->c->pending_messages--;
4680 if (NULL != queue->c->t)
4682 queue->c->t->pending_messages--;
4686 GNUNET_free (queue);
4691 queue_send (void *cls, size_t size, void *buf)
4693 struct MeshPeer *peer = cls;
4694 struct MeshFlowControl *fc;
4695 struct MeshConnection *c;
4696 struct GNUNET_MessageHeader *msg;
4697 struct MeshPeerQueue *queue;
4698 struct MeshTunnel2 *t;
4699 struct MeshChannel *ch;
4700 const struct GNUNET_PeerIdentity *dst_id;
4706 peer->core_transmit = NULL;
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4709 if (NULL == buf || 0 == size)
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4716 queue = peer_get_first_message (peer);
4719 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4724 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4727 dst_id = GNUNET_PEER_resolve2 (peer->id);
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4729 /* Check if buffer size is enough for the message */
4730 if (queue->size > size)
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4733 peer->core_transmit =
4734 GNUNET_CORE_notify_transmit_ready (core_handle,
4737 GNUNET_TIME_UNIT_FOREVER_REL,
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4746 t = (NULL != c) ? c->t : NULL;
4750 switch (queue->type)
4752 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4753 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4754 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4755 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4756 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4757 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4760 GNUNET_MESH_DEBUG_M2S (queue->type));
4761 data_size = send_core_data_raw (queue->cls, size, buf);
4762 msg = (struct GNUNET_MessageHeader *) buf;
4763 type = ntohs (msg->type);
4765 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4767 if (connection_is_origin (c, GNUNET_YES))
4768 data_size = send_core_connection_create (queue->c, size, buf);
4770 data_size = send_core_data_raw (queue->cls, size, buf);
4772 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4774 if (connection_is_origin (c, GNUNET_NO))
4775 data_size = send_core_connection_ack (queue->c, size, buf);
4777 data_size = send_core_data_raw (queue->cls, size, buf);
4779 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4780 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4781 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4782 /* This should be encapsulted */
4787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4792 if (0 < drop_percent &&
4793 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4795 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4796 "Dropping message of type %s\n",
4797 GNUNET_MESH_DEBUG_M2S (queue->type));
4801 /* Free queue, but cls was freed by send_core_* */
4803 queue_destroy (queue, GNUNET_NO);
4805 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4808 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4809 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4810 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4811 fc->last_pid_sent = pid;
4812 send_ack (c, ch, fwd);
4818 /* If more data in queue, send next */
4819 queue = peer_get_first_message (peer);
4822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4823 if (NULL == peer->core_transmit) {
4824 peer->core_transmit =
4825 GNUNET_CORE_notify_transmit_ready(core_handle,
4828 GNUNET_TIME_UNIT_FOREVER_REL,
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4837 "* tmt rdy called somewhere else\n");
4839 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4843 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4848 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4850 GNUNET_SCHEDULER_cancel (fc->poll_task);
4851 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4856 c->pending_messages--;
4857 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4860 connection_destroy (c);
4866 t->pending_messages--;
4867 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4869 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4879 queue_add (void *cls, uint16_t type, size_t size,
4880 struct MeshConnection *c,
4881 struct MeshChannel *ch,
4884 struct MeshPeerQueue *queue;
4885 struct MeshFlowControl *fc;
4886 struct MeshPeer *peer;
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4890 "queue add %s %s (%u) on c %p, ch %p\n",
4891 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4892 GNUNET_assert (NULL != c);
4894 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4895 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4905 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4906 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4913 if (fc->queue_n >= fc->queue_max && 0 == priority)
4915 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4919 "queue full: %u/%u\n",
4920 fc->queue_n, fc->queue_max);
4921 return; /* Drop this message */
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4926 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4927 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4930 "no buffer space (%u > %u): starting poll\n",
4931 fc->last_pid_sent + 1, fc->last_ack_recv);
4932 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4936 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4944 if (100 <= priority)
4946 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4950 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
4956 if (NULL == peer->core_transmit)
4958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4959 "calling core tmt rdy towards %s for %u bytes\n",
4960 peer2s (peer), size);
4961 peer->core_transmit =
4962 GNUNET_CORE_notify_transmit_ready (core_handle,
4965 GNUNET_TIME_UNIT_FOREVER_REL,
4966 GNUNET_PEER_resolve2 (peer->id),
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4974 "core tmt rdy towards %s already called\n",
4978 c->pending_messages++;
4979 c->t->pending_messages++;
4983 /******************************************************************************/
4984 /******************** MESH NETWORK HANDLERS **************************/
4985 /******************************************************************************/
4989 * Generic handler for mesh network payload traffic.
4991 * @param t Tunnel on which we got this message.
4992 * @param message Unencryted data message.
4993 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4995 * @return channel which this message was on.
4997 static struct MeshChannel *
4998 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
5000 struct MeshChannelReliability *rel;
5001 struct MeshChannel *ch;
5002 struct MeshClient *c;
5008 size = ntohs (msg->header.size);
5010 sizeof (struct GNUNET_MESH_Data) +
5011 sizeof (struct GNUNET_MessageHeader))
5016 type = ntohs (msg->header.type);
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
5018 GNUNET_MESH_DEBUG_M2S (type));
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
5020 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5023 ch = channel_get (t, ntohl (msg->chid));
5026 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
5031 /* Initialize FWD/BCK data */
5032 c = fwd ? ch->dest : ch->root;
5033 rel = fwd ? ch->dest_rel : ch->root_rel;
5041 tunnel_change_state (t, MESH_TUNNEL_READY);
5043 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
5045 mid = ntohl (msg->mid);
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
5048 if (GNUNET_NO == ch->reliable ||
5049 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
5050 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
5053 if (GNUNET_YES == ch->reliable)
5055 /* Is this the exact next expected messasge? */
5056 if (mid == rel->mid_recv)
5058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
5060 channel_send_client_data (ch, msg, fwd);
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
5065 channel_rel_add_buffered_data (msg, rel);
5070 /* Tunnel is unreliable: send to clients directly */
5071 /* FIXME: accept Out Of Order traffic */
5072 rel->mid_recv = mid + 1;
5073 channel_send_client_data (ch, msg, fwd);
5078 GNUNET_break_op (0);
5079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5080 " MID %u not expected (%u - %u), dropping!\n",
5081 mid, rel->mid_recv, rel->mid_recv + 64);
5084 channel_send_data_ack (ch, fwd);
5089 * Handler for mesh network traffic end-to-end ACKs.
5091 * @param t Tunnel on which we got this message.
5092 * @param message Data message.
5093 * @param fwd Is this a fwd ACK? (dest->orig)
5095 * @return channel this message was on.
5097 static struct MeshChannel *
5098 handle_data_ack (struct MeshTunnel2 *t,
5099 const struct GNUNET_MESH_DataACK *msg, int fwd)
5101 struct MeshChannelReliability *rel;
5102 struct MeshReliableMessage *copy;
5103 struct MeshReliableMessage *next;
5104 struct MeshChannel *ch;
5109 type = ntohs (msg->header.type);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5111 GNUNET_MESH_DEBUG_M2S (type));
5112 ch = channel_get (t, ntohl (msg->chid));
5115 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5118 ack = ntohl (msg->mid);
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5120 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5122 if (GNUNET_YES == fwd)
5136 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5138 if (GMC_is_pid_bigger (copy->mid, ack))
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5141 channel_rel_free_sent (rel, msg);
5145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5147 rel_message_free (copy);
5149 /* ACK client if needed */
5150 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5152 /* If some message was free'd, update the retransmission delay*/
5153 if (GNUNET_YES == work)
5155 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5157 GNUNET_SCHEDULER_cancel (rel->retry_task);
5158 if (NULL == rel->head_sent)
5160 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5164 struct GNUNET_TIME_Absolute new_target;
5165 struct GNUNET_TIME_Relative delay;
5167 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5168 MESH_RETRANSMIT_MARGIN);
5169 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5171 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5173 GNUNET_SCHEDULER_add_delayed (delay,
5174 &channel_retransmit_message,
5186 * Core handler for connection creation.
5188 * @param cls Closure (unused).
5189 * @param peer Sender (neighbor).
5190 * @param message Message.
5192 * @return GNUNET_OK to keep the connection open,
5193 * GNUNET_SYSERR to close it (signal serious error)
5196 handle_mesh_connection_create (void *cls,
5197 const struct GNUNET_PeerIdentity *peer,
5198 const struct GNUNET_MessageHeader *message)
5200 struct GNUNET_MESH_ConnectionCreate *msg;
5201 struct GNUNET_PeerIdentity *id;
5202 struct GNUNET_HashCode *cid;
5203 struct MeshPeerPath *path;
5204 struct MeshPeer *dest_peer;
5205 struct MeshPeer *orig_peer;
5206 struct MeshConnection *c;
5207 unsigned int own_pos;
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5215 size = ntohs (message->size);
5216 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5218 GNUNET_break_op (0);
5222 /* Calculate hops */
5223 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5224 if (size % sizeof (struct GNUNET_PeerIdentity))
5226 GNUNET_break_op (0);
5229 size /= sizeof (struct GNUNET_PeerIdentity);
5232 GNUNET_break_op (0);
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5237 /* Get parameters */
5238 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5240 id = (struct GNUNET_PeerIdentity *) &msg[1];
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5242 " connection %s (%s).\n",
5243 GNUNET_h2s (cid), GNUNET_i2s (id));
5245 /* Create connection */
5246 c = connection_get (cid);
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5250 c = connection_new (cid);
5254 connection_reset_timeout (c, GNUNET_YES);
5255 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5257 /* Remember peers */
5258 dest_peer = peer_get (&id[size - 1]);
5259 orig_peer = peer_get (&id[0]);
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5263 path = path_new (size);
5265 for (i = 0; i < size; i++)
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5268 GNUNET_i2s (&id[i]));
5269 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5270 if (path->peers[i] == myid)
5273 if (own_pos == 0 && path->peers[own_pos] != myid)
5275 /* create path: self not found in path through self */
5276 GNUNET_break_op (0);
5277 path_destroy (path);
5278 connection_destroy (c);
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5282 path_add_to_peers (path, GNUNET_NO);
5283 c->path = path_duplicate (path);
5284 c->own_pos = own_pos;
5286 /* Is it a connection to us? */
5287 if (own_pos == size - 1)
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5290 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5292 if (NULL == orig_peer->tunnel)
5293 orig_peer->tunnel = tunnel_new ();
5294 tunnel_add_connection (orig_peer->tunnel, c);
5296 send_connection_ack (c, GNUNET_NO);
5298 /* Keep tunnel alive in direction dest->owner*/
5299 connection_reset_timeout (c, GNUNET_NO);
5303 /* It's for somebody else! Retransmit. */
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5305 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5306 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5307 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5314 * Core handler for path ACKs
5316 * @param cls closure
5317 * @param message message
5318 * @param peer peer identity this notification is about
5320 * @return GNUNET_OK to keep the connection open,
5321 * GNUNET_SYSERR to close it (signal serious error)
5324 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5325 const struct GNUNET_MessageHeader *message)
5327 struct GNUNET_MESH_ConnectionACK *msg;
5328 struct MeshPeerPath *p;
5329 struct MeshConnection *c;
5331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5333 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5335 GNUNET_h2s (&msg->cid));
5336 c = connection_get (&msg->cid);
5339 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5348 /* Add path to peers? */
5352 path_add_to_peers (p, GNUNET_YES);
5358 connection_change_state (c, MESH_CONNECTION_READY);
5359 connection_reset_timeout (c, GNUNET_NO);
5360 if (MESH_TUNNEL_READY != c->t->state)
5361 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5362 tunnel_send_queued_data (c->t, GNUNET_YES);
5364 /* Message for us? */
5365 if (connection_is_terminal (c, GNUNET_NO))
5367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5368 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5370 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5371 c->t->peer->dhtget = NULL;
5373 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5378 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5384 * Core handler for notifications of broken paths
5386 * @param cls Closure (unused).
5387 * @param peer Peer identity of sending neighbor.
5388 * @param message Message.
5390 * @return GNUNET_OK to keep the connection open,
5391 * GNUNET_SYSERR to close it (signal serious error)
5394 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5395 const struct GNUNET_MessageHeader *message)
5397 struct GNUNET_MESH_ConnectionBroken *msg;
5398 struct MeshConnection *c;
5400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5401 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5402 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5404 GNUNET_i2s (&msg->peer1));
5405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5406 GNUNET_i2s (&msg->peer2));
5407 c = connection_get (&msg->cid);
5410 GNUNET_break_op (0);
5413 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5414 GNUNET_PEER_search (&msg->peer2));
5421 * Core handler for tunnel destruction
5423 * @param cls Closure (unused).
5424 * @param peer Peer identity of sending neighbor.
5425 * @param message Message.
5427 * @return GNUNET_OK to keep the connection open,
5428 * GNUNET_SYSERR to close it (signal serious error)
5431 handle_mesh_connection_destroy (void *cls,
5432 const struct GNUNET_PeerIdentity *peer,
5433 const struct GNUNET_MessageHeader *message)
5435 struct GNUNET_MESH_ConnectionDestroy *msg;
5436 struct MeshConnection *c;
5440 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5442 "Got a CONNECTION DESTROY message from %s\n",
5444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5445 " for connection %s\n",
5446 GNUNET_h2s (&msg->cid));
5447 c = connection_get (&msg->cid);
5450 /* Probably already got the message from another path,
5451 * destroyed the tunnel and retransmitted to children.
5454 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5458 id = GNUNET_PEER_search (peer);
5459 if (id == connection_get_prev_hop (c)->id)
5461 else if (id == connection_get_next_hop (c)->id)
5465 GNUNET_break_op (0);
5468 send_prebuilt_message_connection (message, c, NULL, fwd);
5469 c->destroy = GNUNET_YES;
5476 * Handler for channel create messages.
5478 * @param t Tunnel this channel is to be created in.
5479 * @param msg Message.
5480 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5482 * @return channel this message was on.
5484 static struct MeshChannel *
5485 handle_channel_create (struct MeshTunnel2 *t,
5486 struct GNUNET_MESH_ChannelCreate *msg,
5489 MESH_ChannelNumber chid;
5490 struct MeshChannel *ch;
5491 struct MeshClient *c;
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5495 /* Check message size */
5496 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5498 GNUNET_break_op (0);
5502 /* Check if channel exists */
5503 chid = ntohl (msg->chid);
5504 ch = channel_get (t, chid);
5507 /* Probably a retransmission, safe to ignore */
5508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5509 if (NULL != ch->dest)
5511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5512 channel_send_ack (ch, !fwd);
5518 /* Create channel */
5519 ch = channel_new (t, NULL, 0);
5520 channel_set_options (ch, ntohl (msg->opt));
5523 /* Find a destination client */
5524 port = ntohl (msg->port);
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5526 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5529 /* TODO send reject */
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5535 channel_add_client (ch, c);
5536 if (GNUNET_YES == ch->reliable)
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5539 send_local_channel_create (ch);
5540 channel_send_ack (ch, !fwd);
5547 * Handler for channel ack messages.
5549 * @param t Tunnel this channel is to be created in.
5550 * @param msg Message.
5551 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5553 * @return channel this message was on.
5555 static struct MeshChannel *
5556 handle_channel_ack (struct MeshTunnel2 *t,
5557 struct GNUNET_MESH_ChannelManage *msg,
5560 MESH_ChannelNumber chid;
5561 struct MeshChannel *ch;
5563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5564 /* Check message size */
5565 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5567 GNUNET_break_op (0);
5571 /* Check if channel exists */
5572 chid = ntohl (msg->chid);
5573 ch = channel_get (t, chid);
5576 GNUNET_break_op (0);
5577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5581 channel_confirm (ch, !fwd);
5587 * Handler for channel destroy messages.
5589 * @param t Tunnel this channel is to be destroyed of.
5590 * @param msg Message.
5591 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5593 * @return channel this message was on.
5595 static struct MeshChannel *
5596 handle_channel_destroy (struct MeshTunnel2 *t,
5597 struct GNUNET_MESH_ChannelManage *msg,
5600 MESH_ChannelNumber chid;
5601 struct MeshChannel *ch;
5603 /* Check message size */
5604 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5606 GNUNET_break_op (0);
5610 /* Check if channel exists */
5611 chid = ntohl (msg->chid);
5612 ch = channel_get (t, chid);
5615 /* Probably a retransmission, safe to ignore */
5619 send_local_channel_destroy (ch, fwd);
5620 channel_destroy (ch);
5627 * Generic handler for mesh network encrypted traffic.
5629 * @param peer Peer identity this notification is about.
5630 * @param message Encrypted message.
5631 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5633 * @return GNUNET_OK to keep the connection open,
5634 * GNUNET_SYSERR to close it (signal serious error)
5637 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5638 const struct GNUNET_MESH_Encrypted *msg,
5641 struct MeshConnection *c;
5642 struct MeshTunnel2 *t;
5643 struct MeshPeer *neighbor;
5644 struct MeshFlowControl *fc;
5651 size = ntohs (msg->header.size);
5653 sizeof (struct GNUNET_MESH_Encrypted) +
5654 sizeof (struct GNUNET_MessageHeader))
5656 GNUNET_break_op (0);
5659 type = ntohs (msg->header.type);
5660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5662 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5664 /* Check connection */
5665 c = connection_get (&msg->cid);
5668 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5673 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5675 /* Check if origin is as expected */
5676 neighbor = connection_get_hop (c, fwd);
5677 if (peer_get (peer)->id != neighbor->id)
5679 GNUNET_break_op (0);
5684 pid = ntohl (msg->pid);
5685 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5687 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5689 "WARNING Received PID %u, (prev %u), ACK %u\n",
5690 pid, fc->last_pid_recv, fc->last_ack_sent);
5693 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5695 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5697 " Pid %u not expected (%u+), dropping!\n",
5698 pid, fc->last_pid_recv + 1);
5701 if (MESH_CONNECTION_SENT == c->state)
5702 connection_change_state (c, MESH_CONNECTION_READY);
5703 connection_reset_timeout (c, fwd);
5704 fc->last_pid_recv = pid;
5706 /* Is this message for us? */
5707 if (connection_is_terminal (c, fwd))
5709 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5711 struct GNUNET_MessageHeader *msgh;
5712 struct MeshChannel *ch;
5714 /* TODO signature verification */
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5716 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5718 fc->last_pid_recv = pid;
5719 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5720 msgh = (struct GNUNET_MessageHeader *) cbuf;
5721 switch (ntohs (msgh->type))
5723 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5724 /* Don't send hop ACK, wait for client to ACK */
5725 ch = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5728 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5729 ch = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5732 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5733 ch = handle_channel_create (t,
5734 (struct GNUNET_MESH_ChannelCreate *) msgh,
5738 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5739 ch = handle_channel_ack (t,
5740 (struct GNUNET_MESH_ChannelManage *) msgh,
5744 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5745 ch = handle_channel_destroy (t,
5746 (struct GNUNET_MESH_ChannelManage *) msgh,
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5752 "end-to-end message not known (%u)\n",
5753 ntohs (msgh->type));
5757 send_ack (c, ch, fwd);
5761 /* Message not for us: forward to next hop */
5762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5763 ttl = ntohl (msg->ttl);
5764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5767 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5769 send_ack (c, NULL, fwd);
5772 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5774 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5781 * Core handler for mesh network traffic going orig->dest.
5783 * @param cls Closure (unused).
5784 * @param message Message received.
5785 * @param peer Peer who sent the message.
5787 * @return GNUNET_OK to keep the connection open,
5788 * GNUNET_SYSERR to close it (signal serious error)
5791 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5792 const struct GNUNET_MessageHeader *message)
5794 return handle_mesh_encrypted (peer,
5795 (struct GNUNET_MESH_Encrypted *)message,
5800 * Core handler for mesh network traffic going dest->orig.
5802 * @param cls Closure (unused).
5803 * @param message Message received.
5804 * @param peer Peer who sent the message.
5806 * @return GNUNET_OK to keep the connection open,
5807 * GNUNET_SYSERR to close it (signal serious error)
5810 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5811 const struct GNUNET_MessageHeader *message)
5813 return handle_mesh_encrypted (peer,
5814 (struct GNUNET_MESH_Encrypted *)message,
5820 * Core handler for mesh network traffic point-to-point acks.
5822 * @param cls closure
5823 * @param message message
5824 * @param peer peer identity this notification is about
5826 * @return GNUNET_OK to keep the connection open,
5827 * GNUNET_SYSERR to close it (signal serious error)
5830 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5831 const struct GNUNET_MessageHeader *message)
5833 struct GNUNET_MESH_ACK *msg;
5834 struct MeshConnection *c;
5835 struct MeshFlowControl *fc;
5839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5842 msg = (struct GNUNET_MESH_ACK *) message;
5844 c = connection_get (&msg->cid);
5848 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5853 /* Is this a forward or backward ACK? */
5854 id = GNUNET_PEER_search (peer);
5855 if (connection_get_next_hop (c)->id == id)
5857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5860 else if (connection_get_prev_hop (c)->id == id)
5862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5867 GNUNET_break_op (0);
5871 ack = ntohl (msg->ack);
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5874 /* Cancel polling if the ACK is bigger than before. */
5875 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5876 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5879 GNUNET_SCHEDULER_cancel (fc->poll_task);
5880 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5881 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5884 fc->last_ack_recv = ack;
5885 connection_unlock_queue (c, fc == &c->fwd_fc);
5892 * Core handler for mesh network traffic point-to-point ack polls.
5894 * @param cls closure
5895 * @param message message
5896 * @param peer peer identity this notification is about
5898 * @return GNUNET_OK to keep the connection open,
5899 * GNUNET_SYSERR to close it (signal serious error)
5902 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5903 const struct GNUNET_MessageHeader *message)
5905 struct GNUNET_MESH_Poll *msg;
5906 struct MeshConnection *c;
5907 struct MeshFlowControl *fc;
5912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5916 msg = (struct GNUNET_MESH_Poll *) message;
5918 c = connection_get (&msg->cid);
5922 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5924 GNUNET_break_op (0);
5928 /* Is this a forward or backward ACK?
5929 * Note: a poll should never be needed in a loopback case,
5930 * since there is no possiblility of packet loss there, so
5931 * this way of discerining FWD/BCK should not be a problem.
5933 id = GNUNET_PEER_search (peer);
5934 if (connection_get_next_hop (c)->id == id)
5936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5939 else if (connection_get_prev_hop (c)->id == id)
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5946 GNUNET_break_op (0);
5950 pid = ntohl (msg->pid);
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5952 pid, fc->last_pid_recv);
5953 fc->last_pid_recv = pid;
5954 fwd = fc == &c->fwd_fc;
5955 send_ack (c, NULL, fwd);
5962 * Core handler for mesh keepalives.
5964 * @param cls closure
5965 * @param message message
5966 * @param peer peer identity this notification is about
5967 * @return GNUNET_OK to keep the connection open,
5968 * GNUNET_SYSERR to close it (signal serious error)
5970 * TODO: Check who we got this from, to validate route.
5973 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5974 const struct GNUNET_MessageHeader *message)
5976 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5977 struct MeshConnection *c;
5978 struct MeshPeer *neighbor;
5981 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5985 c = connection_get (&msg->cid);
5988 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5993 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5994 GNUNET_YES : GNUNET_NO;
5996 /* Check if origin is as expected */
5997 neighbor = connection_get_hop (c, fwd);
5998 if (peer_get (peer)->id != neighbor->id)
6000 GNUNET_break_op (0);
6004 connection_change_state (c, MESH_CONNECTION_READY);
6005 connection_reset_timeout (c, fwd);
6007 if (connection_is_terminal (c, fwd))
6010 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6011 send_prebuilt_message_connection (message, c, NULL, fwd);
6019 * Functions to handle messages from core
6021 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6022 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
6024 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
6025 sizeof (struct GNUNET_MESH_ConnectionACK)},
6026 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
6027 sizeof (struct GNUNET_MESH_ConnectionBroken)},
6028 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
6029 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
6030 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
6031 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6032 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
6033 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
6034 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6035 sizeof (struct GNUNET_MESH_ACK)},
6036 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6037 sizeof (struct GNUNET_MESH_Poll)},
6038 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
6039 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
6045 * Function to process paths received for a new peer addition. The recorded
6046 * paths form the initial tunnel, which can be optimized later.
6047 * Called on each result obtained for the DHT search.
6049 * @param cls closure
6050 * @param exp when will this value expire
6051 * @param key key of the result
6052 * @param get_path path of the get request
6053 * @param get_path_length lenght of get_path
6054 * @param put_path path of the put request
6055 * @param put_path_length length of the put_path
6056 * @param type type of the result
6057 * @param size number of bytes in data
6058 * @param data pointer to the result data
6061 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6062 const struct GNUNET_HashCode * key,
6063 const struct GNUNET_PeerIdentity *get_path,
6064 unsigned int get_path_length,
6065 const struct GNUNET_PeerIdentity *put_path,
6066 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6067 size_t size, const void *data)
6069 struct MeshPeer *peer = cls;
6070 struct MeshPeerPath *p;
6071 struct MeshConnection *c;
6072 struct GNUNET_PeerIdentity pi;
6075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6076 GNUNET_PEER_resolve (peer->id, &pi);
6077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6079 p = path_build_from_dht (get_path, get_path_length,
6080 put_path, put_path_length);
6081 path_add_to_peers (p, GNUNET_NO);
6084 /* Count connections */
6085 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6087 /* If we already have 3 (or more (?!)) connections, it's enough */
6091 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6094 peer_connect (peer);
6100 /******************************************************************************/
6101 /********************* MESH LOCAL HANDLES **************************/
6102 /******************************************************************************/
6106 * Handler for client connection.
6108 * @param cls Closure (unused).
6109 * @param client Client handler.
6112 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6114 struct MeshClient *c;
6118 c = GNUNET_malloc (sizeof (struct MeshClient));
6120 c->id = next_client_id++; /* overflow not important: just for debug */
6121 GNUNET_SERVER_client_keep (client);
6122 GNUNET_SERVER_client_set_user_context (client, c);
6123 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6128 * Handler for client disconnection
6130 * @param cls closure
6131 * @param client identification of the client; NULL
6132 * for the last call when the server is destroyed
6135 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6137 struct MeshClient *c;
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6146 c = client_get (client);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6151 GNUNET_SERVER_client_drop (c->handle);
6152 c->shutting_down = GNUNET_YES;
6153 if (NULL != c->own_channels)
6155 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6156 &channel_destroy_iterator, c);
6157 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6160 if (NULL != c->incoming_channels)
6162 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6163 &channel_destroy_iterator, c);
6164 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6167 if (NULL != c->ports)
6169 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6170 &client_release_ports, c);
6171 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6173 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6174 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6188 * Handler for new clients
6190 * @param cls closure
6191 * @param client identification of the client
6192 * @param message the actual message, which includes messages the client wants
6195 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6196 const struct GNUNET_MessageHeader *message)
6198 struct GNUNET_MESH_ClientConnect *cc_msg;
6199 struct MeshClient *c;
6204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6206 /* Check data sanity */
6207 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6208 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6209 if (0 != (size % sizeof (uint32_t)))
6212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6215 size /= sizeof (uint32_t);
6217 /* Initialize new client structure */
6218 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6225 p = (uint32_t *) &cc_msg[1];
6226 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6227 for (i = 0; i < size; i++)
6230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6232 /* store in client's hashmap */
6233 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6235 /* store in global hashmap */
6236 /* FIXME only allow one client to have the port open,
6237 * have a backup hashmap with waiting clients */
6238 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6243 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6244 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6245 GNUNET_SERVER_notification_context_add (nc, client);
6246 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6248 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6254 * Handler for requests of new tunnels
6256 * @param cls Closure.
6257 * @param client Identification of the client.
6258 * @param message The actual message.
6261 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6262 const struct GNUNET_MessageHeader *message)
6264 struct GNUNET_MESH_ChannelMessage *msg;
6265 struct MeshPeer *peer;
6266 struct MeshTunnel2 *t;
6267 struct MeshChannel *ch;
6268 struct MeshClient *c;
6269 MESH_ChannelNumber chid;
6271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6273 /* Sanity check for client registration */
6274 if (NULL == (c = client_get (client)))
6277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6282 /* Message size sanity check */
6283 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6286 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6290 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6292 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6293 chid = ntohl (msg->channel_id);
6295 /* Sanity check for duplicate channel IDs */
6296 if (NULL != channel_get_by_local_id (c, chid))
6299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6303 peer = peer_get (&msg->peer);
6304 if (NULL == peer->tunnel)
6306 peer->tunnel = tunnel_new ();
6307 peer->tunnel->peer = peer;
6311 /* Create channel */
6312 ch = channel_new (t, c, chid);
6316 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6319 ch->port = ntohl (msg->port);
6320 channel_set_options (ch, ntohl (msg->opt));
6322 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6323 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6324 ch->root_rel->ch = ch;
6325 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6328 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6329 peer_connect (peer);
6331 /* Send create channel */
6333 struct GNUNET_MESH_ChannelCreate msgcc;
6335 msgcc.header.size = htons (sizeof (msgcc));
6336 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6337 msgcc.chid = htonl (ch->gid);
6338 msgcc.port = msg->port;
6339 msgcc.opt = msg->opt;
6341 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6344 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6350 * Handler for requests of deleting tunnels
6352 * @param cls closure
6353 * @param client identification of the client
6354 * @param message the actual message
6357 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6358 const struct GNUNET_MessageHeader *message)
6360 struct GNUNET_MESH_ChannelMessage *msg;
6361 struct MeshClient *c;
6362 struct MeshChannel *ch;
6363 struct MeshTunnel2 *t;
6364 MESH_ChannelNumber chid;
6366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6367 "Got a DESTROY CHANNEL from client!\n");
6369 /* Sanity check for client registration */
6370 if (NULL == (c = client_get (client)))
6373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6378 /* Message sanity check */
6379 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6386 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6388 /* Retrieve tunnel */
6389 chid = ntohl (msg->channel_id);
6390 ch = channel_get_by_local_id (c, chid);
6393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6399 /* Cleanup after the tunnel */
6400 client_delete_channel (c, ch);
6401 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6405 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6412 " channel %X client %p (%p, %p)\n",
6413 chid, c, ch->root, ch->dest);
6418 channel_destroy (ch);
6419 tunnel_destroy_if_empty (t);
6421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6427 * Handler for client traffic
6429 * @param cls closure
6430 * @param client identification of the client
6431 * @param message the actual message
6434 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6435 const struct GNUNET_MessageHeader *message)
6437 struct GNUNET_MESH_LocalData *msg;
6438 struct MeshClient *c;
6439 struct MeshChannel *ch;
6440 struct MeshChannelReliability *rel;
6441 MESH_ChannelNumber chid;
6445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6446 "Got data from a client!\n");
6448 /* Sanity check for client registration */
6449 if (NULL == (c = client_get (client)))
6452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6457 msg = (struct GNUNET_MESH_LocalData *) message;
6459 /* Sanity check for message size */
6460 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6461 if (size < sizeof (struct GNUNET_MessageHeader))
6464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6468 /* Channel exists? */
6469 chid = ntohl (msg->id);
6470 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6471 ch = channel_get_by_local_id (c, chid);
6475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6479 /* Is the client in the channel? */
6482 ch->root->handle == client)
6486 ch->dest->handle == client) ) )
6489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 rel = fwd ? ch->root_rel : ch->dest_rel;
6494 rel->client_ready = GNUNET_NO;
6496 /* Ok, everything is correct, send the message. */
6498 struct GNUNET_MESH_Data *payload;
6499 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6500 unsigned char cbuf[p2p_size];
6502 payload = (struct GNUNET_MESH_Data *) cbuf;
6503 payload->mid = htonl (rel->mid_send);
6505 memcpy (&payload[1], &msg[1], size);
6506 payload->header.size = htons (p2p_size);
6507 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6508 payload->chid = htonl (ch->gid);
6509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6510 send_prebuilt_message_channel (&payload->header, ch, fwd);
6512 if (GNUNET_YES == ch->reliable)
6513 channel_save_copy (ch, &payload->header, fwd);
6515 if (tunnel_get_buffer (ch->t, fwd) > 0)
6516 send_local_ack (ch, fwd);
6517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6518 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6525 * Handler for client's ACKs for payload traffic.
6527 * @param cls Closure (unused).
6528 * @param client Identification of the client.
6529 * @param message The actual message.
6532 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6533 const struct GNUNET_MessageHeader *message)
6535 struct GNUNET_MESH_LocalAck *msg;
6536 struct MeshChannelReliability *rel;
6537 struct MeshChannel *ch;
6538 struct MeshClient *c;
6539 MESH_ChannelNumber chid;
6542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6544 /* Sanity check for client registration */
6545 if (NULL == (c = client_get (client)))
6548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6553 msg = (struct GNUNET_MESH_LocalAck *) message;
6555 /* Channel exists? */
6556 chid = ntohl (msg->channel_id);
6557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6558 ch = channel_get_by_local_id (c, chid);
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6564 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6569 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6570 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6571 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6572 rel = fwd ? ch->dest_rel : ch->root_rel;
6574 rel->client_ready = GNUNET_YES;
6575 channel_send_client_buffered_data (ch, c, fwd);
6576 send_ack (NULL, ch, fwd);
6578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6585 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6587 * @param cls Closure (client handle).
6588 * @param key Key (hashed tunnel ID, unused).
6589 * @param value Tunnel info.
6591 * @return GNUNET_YES, to keep iterating.
6594 // monitor_all_tunnels_iterator (void *cls,
6595 // const struct GNUNET_HashCode * key,
6598 // struct GNUNET_SERVER_Client *client = cls;
6599 // struct MeshChannel *ch = value;
6600 // struct GNUNET_MESH_LocalMonitor *msg;
6602 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6603 // msg->channel_id = htonl (ch->gid);
6604 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6605 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6607 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6608 // "* sending info about tunnel %s\n",
6609 // GNUNET_i2s (&msg->owner));
6611 // GNUNET_SERVER_notification_context_unicast (nc, client,
6612 // &msg->header, GNUNET_NO);
6613 // return GNUNET_YES;
6618 * Handler for client's MONITOR request.
6620 * @param cls Closure (unused).
6621 * @param client Identification of the client.
6622 * @param message The actual message.
6625 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6626 const struct GNUNET_MessageHeader *message)
6628 struct MeshClient *c;
6630 /* Sanity check for client registration */
6631 if (NULL == (c = client_get (client)))
6634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6638 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6639 "Received get tunnels request from client %u\n",
6641 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6642 // monitor_all_tunnels_iterator,
6644 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6645 "Get tunnels request from client %u completed\n",
6647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6652 * Handler for client's MONITOR_TUNNEL request.
6654 * @param cls Closure (unused).
6655 * @param client Identification of the client.
6656 * @param message The actual message.
6659 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6660 const struct GNUNET_MessageHeader *message)
6662 const struct GNUNET_MESH_LocalMonitor *msg;
6663 struct GNUNET_MESH_LocalMonitor *resp;
6664 struct MeshClient *c;
6665 struct MeshChannel *ch;
6667 /* Sanity check for client registration */
6668 if (NULL == (c = client_get (client)))
6671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6675 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6676 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6677 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6680 ntohl (msg->channel_id));
6681 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6685 /* We don't know the tunnel */
6686 struct GNUNET_MESH_LocalMonitor warn;
6689 GNUNET_SERVER_notification_context_unicast (nc, client,
6692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6696 /* Initialize context */
6697 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6699 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6700 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6701 &resp->header, GNUNET_NO);
6704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6705 "Monitor tunnel request from client %u completed\n",
6707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6712 * Functions to handle messages from clients
6714 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6715 {&handle_local_new_client, NULL,
6716 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6717 {&handle_local_channel_create, NULL,
6718 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6719 sizeof (struct GNUNET_MESH_ChannelMessage)},
6720 {&handle_local_channel_destroy, NULL,
6721 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6722 sizeof (struct GNUNET_MESH_ChannelMessage)},
6723 {&handle_local_data, NULL,
6724 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6725 {&handle_local_ack, NULL,
6726 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6727 sizeof (struct GNUNET_MESH_LocalAck)},
6728 {&handle_local_get_tunnels, NULL,
6729 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6730 sizeof (struct GNUNET_MessageHeader)},
6731 {&handle_local_show_tunnel, NULL,
6732 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6733 sizeof (struct GNUNET_MESH_LocalMonitor)},
6739 * Method called whenever a given peer connects.
6741 * @param cls closure
6742 * @param peer peer identity this notification is about
6745 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6747 struct MeshPeer *pi;
6748 struct MeshPeerPath *path;
6750 DEBUG_CONN ("Peer connected\n");
6751 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6752 pi = peer_get (peer);
6755 DEBUG_CONN (" (self)\n");
6756 path = path_new (1);
6760 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6761 path = path_new (2);
6762 path->peers[1] = pi->id;
6763 GNUNET_PEER_change_rc (pi->id, 1);
6764 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6766 path->peers[0] = myid;
6767 GNUNET_PEER_change_rc (myid, 1);
6768 peer_add_path (pi, path, GNUNET_YES);
6770 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6776 * Method called whenever a peer disconnects.
6778 * @param cls closure
6779 * @param peer peer identity this notification is about
6782 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6784 struct MeshPeer *pi;
6786 DEBUG_CONN ("Peer disconnected\n");
6787 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6794 peer_remove_path (pi, myid, pi->id);
6796 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6799 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6800 pi->connections = NULL;
6803 DEBUG_CONN (" (self)\n");
6805 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6812 * Install server (service) handlers and start listening to clients.
6817 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6818 GNUNET_SERVER_connect_notify (server_handle,
6819 &handle_local_client_connect, NULL);
6820 GNUNET_SERVER_disconnect_notify (server_handle,
6821 &handle_local_client_disconnect, NULL);
6822 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6824 clients_head = NULL;
6825 clients_tail = NULL;
6827 GNUNET_SERVER_resume (server_handle);
6832 * To be called on core init/fail.
6834 * @param cls Closure (config)
6835 * @param identity the public identity of this peer
6838 core_init (void *cls,
6839 const struct GNUNET_PeerIdentity *identity)
6841 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6845 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6850 GNUNET_i2s (identity));
6851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6853 GNUNET_i2s (&my_full_id));
6854 GNUNET_CORE_disconnect (core_handle);
6855 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6856 NULL, /* Closure passed to MESH functions */
6857 &core_init, /* Call core_init once connected */
6858 &core_connect, /* Handle connects */
6859 &core_disconnect, /* remove peers on disconnects */
6860 NULL, /* Don't notify about all incoming messages */
6861 GNUNET_NO, /* For header only in notification */
6862 NULL, /* Don't notify about all outbound messages */
6863 GNUNET_NO, /* For header-only out notification */
6864 core_handlers); /* Register these handlers */
6873 /******************************************************************************/
6874 /************************ MAIN FUNCTIONS ****************************/
6875 /******************************************************************************/
6878 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6880 * @param cls closure
6881 * @param key current key code
6882 * @param value value in the hash map
6883 * @return GNUNET_YES if we should continue to iterate,
6887 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6889 struct MeshPeer *p = value;
6890 struct MeshTunnel2 *t = p->tunnel;
6899 * Task run during shutdown.
6905 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6909 if (core_handle != NULL)
6911 GNUNET_CORE_disconnect (core_handle);
6914 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6915 if (dht_handle != NULL)
6917 GNUNET_DHT_disconnect (dht_handle);
6922 GNUNET_SERVER_notification_context_destroy (nc);
6925 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6927 GNUNET_SCHEDULER_cancel (announce_id_task);
6928 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6935 * Process mesh requests.
6937 * @param cls closure
6938 * @param server the initialized server
6939 * @param c configuration to use
6942 run (void *cls, struct GNUNET_SERVER_Handle *server,
6943 const struct GNUNET_CONFIGURATION_Handle *c)
6946 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6949 server_handle = server;
6950 GNUNET_SERVER_suspend (server_handle);
6953 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6956 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6958 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6959 "mesh", "peer/privatekey");
6960 GNUNET_SCHEDULER_shutdown ();
6965 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6966 &refresh_connection_time))
6968 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6970 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6971 "mesh", "refresh path time");
6972 GNUNET_SCHEDULER_shutdown ();
6977 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6980 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6982 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6983 "mesh", "id announce time");
6984 GNUNET_SCHEDULER_shutdown ();
6989 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6994 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6995 "mesh", "connect timeout");
6996 GNUNET_SCHEDULER_shutdown ();
7001 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7004 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7006 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7007 "mesh", "max msgs queue");
7008 GNUNET_SCHEDULER_shutdown ();
7013 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
7016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7018 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7019 "mesh", "max tunnels");
7020 GNUNET_SCHEDULER_shutdown ();
7025 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7028 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7030 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7031 "mesh", "default ttl", 64);
7036 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7040 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7041 "mesh", "max peers", 1000);
7046 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
7053 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7054 "Mesh is running with drop mode enabled. "
7055 "This is NOT a good idea! "
7056 "Remove the DROP_PERCENT option from your configuration.\n");
7060 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7061 &dht_replication_level))
7063 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7065 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7066 "mesh", "dht replication level", 3);
7067 dht_replication_level = 3;
7070 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7071 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7072 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7074 dht_handle = GNUNET_DHT_connect (c, 64);
7075 if (NULL == dht_handle)
7079 stats = GNUNET_STATISTICS_create ("mesh", c);
7081 /* Scheduled the task to clean up when shutdown is called */
7082 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7084 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7085 GNUNET_free (keyfile);
7086 GNUNET_assert (NULL != pk);
7087 my_private_key = pk;
7088 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7089 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7090 &my_full_id.hashPubKey);
7091 myid = GNUNET_PEER_intern (&my_full_id);
7092 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7093 "Mesh for peer [%s] starting\n",
7094 GNUNET_i2s(&my_full_id));
7096 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7097 NULL, /* Closure passed to MESH functions */
7098 &core_init, /* Call core_init once connected */
7099 &core_connect, /* Handle connects */
7100 &core_disconnect, /* remove peers on disconnects */
7101 NULL, /* Don't notify about all incoming messages */
7102 GNUNET_NO, /* For header only in notification */
7103 NULL, /* Don't notify about all outbound messages */
7104 GNUNET_NO, /* For header-only out notification */
7105 core_handlers); /* Register these handlers */
7106 if (NULL == core_handle)
7109 GNUNET_SCHEDULER_shutdown ();
7112 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7118 * The main function for the mesh service.
7120 * @param argc number of arguments from the command line
7121 * @param argv command line arguments
7122 * @return 0 ok, 1 on error
7125 main (int argc, char *const *argv)
7130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7131 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7133 ret = (GNUNET_OK == r) ? 0 : 1;
7134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");