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;
1590 * Get the total buffer space for a tunnel.
1593 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1595 struct MeshConnection *c;
1596 struct MeshFlowControl *fc;
1597 unsigned int buffer;
1599 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1601 if (c->state != MESH_CONNECTION_READY)
1604 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1605 buffer += fc->last_ack_recv - fc->last_pid_sent;
1613 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1614 * Encrypt data with the tunnel key.
1616 * @param t Tunnel whose key to use.
1617 * @param dst Destination for the encrypted data.
1618 * @param src Source of the plaintext.
1619 * @param size Size of the plaintext.
1620 * @param iv Initialization Vector to use.
1621 * @param fwd Is this a fwd message?
1624 tunnel_encrypt (struct MeshTunnel2 *t,
1625 void *dst, const void *src,
1626 size_t size, uint64_t iv, int fwd)
1628 memcpy (dst, src, size);
1633 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1634 * Decrypt data with the tunnel key.
1636 * @param t Tunnel whose key to use.
1637 * @param dst Destination for the plaintext.
1638 * @param src Source of the encrypted data.
1639 * @param size Size of the encrypted data.
1640 * @param iv Initialization Vector to use.
1641 * @param fwd Is this a fwd message?
1644 tunnel_decrypt (struct MeshTunnel2 *t,
1645 void *dst, const void *src,
1646 size_t size, uint64_t iv, int fwd)
1648 memcpy (dst, src, size);
1653 * Sends an already built message on a connection, properly registering
1654 * all used resources.
1656 * @param message Message to send. Function makes a copy of it.
1657 * If message is not hop-by-hop, decrements TTL of copy.
1658 * @param c Connection on which this message is transmitted.
1659 * @param ch Channel on which this message is transmitted, or NULL.
1660 * @param fwd Is this a fwd message?
1663 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1664 struct MeshConnection *c,
1665 struct MeshChannel *ch,
1672 size = ntohs (message->size);
1673 data = GNUNET_malloc (size);
1674 memcpy (data, message, size);
1675 type = ntohs (message->type);
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1677 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1681 struct GNUNET_MESH_Encrypted *emsg;
1682 struct GNUNET_MESH_ACK *amsg;
1683 struct GNUNET_MESH_Poll *pmsg;
1686 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1687 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1688 emsg = (struct GNUNET_MESH_Encrypted *) data;
1689 ttl = ntohl (emsg->ttl);
1692 GNUNET_break_op (0);
1696 emsg->ttl = htonl (ttl - 1);
1697 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1701 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1702 amsg = (struct GNUNET_MESH_ACK *) data;
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1707 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1708 pmsg = (struct GNUNET_MESH_Poll *) data;
1710 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1728 * Sends an already built message on a tunnel, choosing the best connection.
1730 * @param message Message to send. Function modifies it.
1731 * @param t Tunnel on which this message is transmitted.
1732 * @param ch Channel on which this message is transmitted.
1733 * @param fwd Is this a fwd message?
1736 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1737 struct MeshTunnel2 *t,
1738 struct MeshChannel *ch,
1741 struct MeshConnection *c;
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1746 c = tunnel_get_connection (t, fwd);
1749 GNUNET_break (GNUNET_YES == t->destroy);
1752 type = ntohs (msg->header.type);
1755 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1756 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1757 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1758 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1760 msg->ttl = htonl (default_ttl);
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1764 GNUNET_MESH_DEBUG_M2S (type));
1769 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1774 * Sends an already built message on a channel, properly registering
1775 * all used resources and encrypting the message with the tunnel's key.
1777 * @param message Message to send. Function makes a copy of it.
1778 * @param ch Channel on which this message is transmitted.
1779 * @param fwd Is this a fwd message?
1782 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1783 struct MeshChannel *ch,
1786 struct GNUNET_MESH_Encrypted *msg;
1787 size_t size = ntohs (message->size);
1788 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1793 peer2s (ch->t->peer), ch->gid);
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1795 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1796 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1797 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1799 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1800 msg->header.type = htons (type);
1801 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1802 msg->iv = GNUNET_htonll (iv);
1803 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1805 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1810 * Sends a CREATE CONNECTION message for a path to a peer.
1811 * Changes the connection and tunnel states if necessary.
1813 * @param connection Connection to create.
1816 send_connection_create (struct MeshConnection *connection)
1818 struct MeshTunnel2 *t;
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1823 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1824 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1825 (connection->path->length *
1826 sizeof (struct GNUNET_PeerIdentity)),
1831 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1832 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1833 if (MESH_CONNECTION_NEW == connection->state)
1834 connection_change_state (connection, MESH_CONNECTION_SENT);
1839 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1842 * @param connection Connection to confirm.
1843 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1846 send_connection_ack (struct MeshConnection *connection, int fwd)
1848 struct MeshTunnel2 *t;
1851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1853 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1854 sizeof (struct GNUNET_MESH_ConnectionACK),
1858 if (MESH_TUNNEL_NEW == t->state)
1859 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1864 * Core callback to write a pre-constructed data packet to core buffer
1866 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1867 * @param size Number of bytes available in buf.
1868 * @param buf Where the to write the message.
1870 * @return number of bytes written to buf
1873 send_core_data_raw (void *cls, size_t size, void *buf)
1875 struct GNUNET_MessageHeader *msg = cls;
1878 GNUNET_assert (NULL != msg);
1879 total_size = ntohs (msg->size);
1881 if (total_size > size)
1886 memcpy (buf, msg, total_size);
1893 * Function to send a create connection message to a peer.
1895 * @param c Connection to create.
1896 * @param size number of bytes available in buf
1897 * @param buf where the callee should write the message
1898 * @return number of bytes written to buf
1901 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1903 struct GNUNET_MESH_ConnectionCreate *msg;
1904 struct GNUNET_PeerIdentity *peer_ptr;
1905 struct MeshPeerPath *p = c->path;
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1911 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1912 p->length * sizeof (struct GNUNET_PeerIdentity);
1914 if (size < size_needed || NULL == buf)
1919 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1920 msg->header.size = htons (size_needed);
1921 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1924 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1925 for (i = 0; i < p->length; i++)
1927 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1937 * Creates a path ack message in buf and frees all unused resources.
1939 * @param c Connection to send an ACK on.
1940 * @param size number of bytes available in buf
1941 * @param buf where the callee should write the message
1943 * @return number of bytes written to buf
1946 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1948 struct GNUNET_MESH_ConnectionACK *msg = buf;
1949 struct MeshTunnel2 *t = c->t;
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1952 GNUNET_assert (NULL != t);
1953 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1958 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1959 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1962 /* TODO add signature */
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1965 return sizeof (struct GNUNET_MESH_ConnectionACK);
1970 * Destroy the peer_info and free any allocated resources linked to it
1972 * @param peer The peer_info to destroy.
1974 * @return GNUNET_OK on success
1977 peer_destroy (struct MeshPeer *peer)
1979 struct GNUNET_PeerIdentity id;
1980 struct MeshPeerPath *p;
1981 struct MeshPeerPath *nextp;
1983 GNUNET_PEER_resolve (peer->id, &id);
1984 GNUNET_PEER_change_rc (peer->id, -1);
1987 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
1990 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1991 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1993 if (NULL != peer->dhtget)
1995 GNUNET_DHT_get_stop (peer->dhtget);
1997 p = peer->path_head;
2001 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2005 tunnel_destroy_empty (peer->tunnel);
2012 * Returns if peer is used (has a tunnel, is neighbor).
2014 * @peer Peer to check.
2016 * @return GNUNET_YES if peer is in use.
2019 peer_is_used (struct MeshPeer *peer)
2021 struct MeshPeerPath *p;
2023 if (NULL != peer->tunnel)
2026 for (p = peer->path_head; NULL != p; p = p->next)
2035 * Iterator over all the peers to get the oldest timestamp.
2037 * @param cls Closure (unsued).
2038 * @param key ID of the peer.
2039 * @param value Peer_Info of the peer.
2042 peer_get_oldest (void *cls,
2043 const struct GNUNET_HashCode *key,
2046 struct MeshPeer *p = value;
2047 struct GNUNET_TIME_Absolute *abs = cls;
2049 /* Don't count active peers */
2050 if (GNUNET_YES == peer_is_used (p))
2053 if (abs->abs_value_us < p->last_contact.abs_value_us)
2054 abs->abs_value_us = p->last_contact.abs_value_us;
2061 * Iterator over all the peers to remove the oldest entry.
2063 * @param cls Closure (unsued).
2064 * @param key ID of the peer.
2065 * @param value Peer_Info of the peer.
2068 peer_timeout (void *cls,
2069 const struct GNUNET_HashCode *key,
2072 struct MeshPeer *p = value;
2073 struct GNUNET_TIME_Absolute *abs = cls;
2075 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2076 GNUNET_NO == peer_is_used (p))
2086 * Delete oldest unused peer.
2089 peer_delete_oldest (void)
2091 struct GNUNET_TIME_Absolute abs;
2093 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2095 GNUNET_CONTAINER_multihashmap_iterate (peers,
2098 GNUNET_CONTAINER_multihashmap_iterate (peers,
2105 * Retrieve the MeshPeer stucture associated with the peer, create one
2106 * and insert it in the appropriate structures if the peer is not known yet.
2108 * @param peer Full identity of the peer.
2110 * @return Existing or newly created peer info.
2112 static struct MeshPeer *
2113 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2115 struct MeshPeer *peer;
2117 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2120 peer = GNUNET_new (struct MeshPeer);
2121 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2123 peer_delete_oldest ();
2125 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2126 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2127 peer->id = GNUNET_PEER_intern (peer_id);
2129 peer->last_contact = GNUNET_TIME_absolute_get();
2136 * Retrieve the MeshPeer stucture associated with the peer, create one
2137 * and insert it in the appropriate structures if the peer is not known yet.
2139 * @param peer Short identity of the peer.
2141 * @return Existing or newly created peer info.
2143 static struct MeshPeer *
2144 peer_get_short (const GNUNET_PEER_Id peer)
2146 return peer_get (GNUNET_PEER_resolve2 (peer));
2151 * Get a cost of a path for a peer considering existing tunnel connections.
2153 * @param peer Peer towards which the path is considered.
2154 * @param path Candidate path.
2156 * @return Cost of the path (path length + number of overlapping nodes)
2159 peer_get_path_cost (const struct MeshPeer *peer,
2160 const struct MeshPeerPath *path)
2162 struct MeshConnection *c;
2163 unsigned int overlap;
2171 GNUNET_assert (NULL != peer->tunnel);
2173 for (i = 0; i < path->length; i++)
2175 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2177 for (j = 0; j < c->path->length; j++)
2179 if (path->peers[i] == c->path->peers[j])
2187 return (path->length + overlap) * (path->score * -1);
2192 * Choose the best path towards a peer considering the tunnel properties.
2194 * @param peer The destination peer.
2196 * @return Best current known path towards the peer, if any.
2198 static struct MeshPeerPath *
2199 peer_get_best_path (const struct MeshPeer *peer)
2201 struct MeshPeerPath *best_p;
2202 struct MeshPeerPath *p;
2203 struct MeshConnection *c;
2204 unsigned int best_cost;
2207 best_cost = UINT_MAX;
2209 for (p = peer->path_head; NULL != p; p = p->next)
2211 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2215 continue; /* If path is in use in a connection, skip it. */
2217 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2227 queue_is_sendable (struct MeshPeerQueue *q)
2229 struct MeshFlowControl *fc;
2231 /* Is PID-independent? */
2234 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2235 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2239 /* Is PID allowed? */
2240 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2241 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2249 * Get first sendable message.
2251 * @param peer The destination peer.
2253 * @return Best current known path towards the peer, if any.
2255 static struct MeshPeerQueue *
2256 peer_get_first_message (const struct MeshPeer *peer)
2258 struct MeshPeerQueue *q;
2260 for (q = peer->queue_head; NULL != q; q = q->next)
2262 if (queue_is_sendable (q))
2271 * Try to establish a new connection to this peer in the given tunnel.
2272 * If the peer doesn't have any path to it yet, try to get one.
2273 * If the peer already has some path, send a CREATE CONNECTION towards it.
2275 * @param peer PeerInfo of the peer.
2278 peer_connect (struct MeshPeer *peer)
2280 struct MeshTunnel2 *t;
2281 struct MeshPeerPath *p;
2282 struct MeshConnection *c;
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 "peer_connect towards %s\n",
2288 if (NULL != peer->path_head)
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2291 p = peer_get_best_path (peer);
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2295 c = tunnel_use_path (t, p);
2296 send_connection_create (c);
2299 else if (NULL == peer->dhtget)
2301 const struct GNUNET_PeerIdentity *id;
2303 id = GNUNET_PEER_resolve2 (peer->id);
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2305 " Starting DHT GET for peer %s\n", peer2s (peer));
2306 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2307 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2308 &id->hashPubKey, /* key to search */
2309 dht_replication_level, /* replication level */
2310 GNUNET_DHT_RO_RECORD_ROUTE |
2311 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2313 0, /* xquery bits */
2314 &dht_get_id_handler, peer);
2315 if (MESH_TUNNEL_NEW == t->state)
2316 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 "There is no path but the DHT GET is already started.\n");
2327 * Get the first transmittable message for a connection.
2329 * @param c Connection.
2330 * @param fwd Is this FWD?
2332 * @return First transmittable message.
2334 static struct MeshPeerQueue *
2335 connection_get_first_message (struct MeshConnection *c, int fwd)
2337 struct MeshPeerQueue *q;
2340 p = connection_get_hop (c, fwd);
2342 for (q = p->queue_head; NULL != q; q = q->next)
2346 if (queue_is_sendable (q))
2355 * Is this peer the first one on the connection?
2357 * @param c Connection.
2358 * @param fwd Is this about fwd traffic?
2360 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2363 connection_is_origin (struct MeshConnection *c, int fwd)
2365 if (!fwd && c->own_pos == c->path->length - 1)
2367 if (fwd && c->own_pos == 0)
2374 * Is this peer the last one on the connection?
2376 * @param c Connection.
2377 * @param fwd Is this about fwd traffic?
2378 * Note that the ROOT is the terminal for BCK traffic!
2380 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2383 connection_is_terminal (struct MeshConnection *c, int fwd)
2385 if (fwd && c->own_pos == c->path->length - 1)
2387 if (!fwd && c->own_pos == 0)
2394 * @brief Re-initiate traffic on this connection if necessary.
2396 * Check if there is traffic queued towards this peer
2397 * and the core transmit handle is NULL (traffic was stalled).
2398 * If so, call core tmt rdy.
2400 * @param c Connection on which initiate traffic.
2401 * @param fwd Is this about fwd traffic?
2404 connection_unlock_queue (struct MeshConnection *c, int fwd)
2406 struct MeshPeer *peer;
2407 struct MeshPeerQueue *q;
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "connection_unlock_queue %s on %s\n",
2412 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2414 if (connection_is_origin (c, fwd))
2416 struct MeshTunnel2 *t = c->t;
2417 struct MeshChannel *ch;
2418 struct MeshChannelReliability *rel;
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2421 /* FIXME randomize channel selection, not always first channel */
2422 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2424 rel = fwd ? ch->root_rel : ch->dest_rel;
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2427 ch->gid, rel->client_ready ? "ready " : "not ready");
2428 if (GNUNET_NO == rel->client_ready)
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2431 send_local_ack (ch, fwd);
2432 return; /* FIXME authorize all channels? */
2438 peer = connection_get_hop (c, fwd);
2440 if (NULL != peer->core_transmit)
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2443 return; /* Already unlocked */
2446 q = connection_get_first_message (c, fwd);
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2450 return; /* Nothing to transmit */
2454 peer->core_transmit =
2455 GNUNET_CORE_notify_transmit_ready (core_handle,
2458 GNUNET_TIME_UNIT_FOREVER_REL,
2459 GNUNET_PEER_resolve2 (peer->id),
2467 * Cancel all transmissions that belong to a certain connection.
2469 * @param c Connection which to cancel.
2470 * @param fwd Cancel fwd traffic?
2473 connection_cancel_queues (struct MeshConnection *c, int fwd)
2475 struct MeshPeerQueue *q;
2476 struct MeshPeerQueue *next;
2477 struct MeshFlowControl *fc;
2478 struct MeshPeer *peer;
2485 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2486 peer = connection_get_hop (c, fwd);
2488 for (q = peer->queue_head; NULL != q; q = next)
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "connection_cancel_queue %s\n",
2495 GNUNET_MESH_DEBUG_M2S (q->type));
2496 queue_destroy (q, GNUNET_YES);
2499 if (NULL == peer->queue_head)
2501 if (NULL != peer->core_transmit)
2503 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2504 peer->core_transmit = NULL;
2506 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2508 GNUNET_SCHEDULER_cancel (fc->poll_task);
2509 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2516 * Remove all paths that rely on a direct connection between p1 and p2
2517 * from the peer itself and notify all tunnels about it.
2519 * @param peer PeerInfo of affected peer.
2520 * @param p1 GNUNET_PEER_Id of one peer.
2521 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2524 * TODO: optimize (see below)
2527 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2530 struct MeshPeerPath *p;
2531 struct MeshPeerPath *next;
2532 struct MeshPeer *peer_d;
2534 unsigned int destroyed;
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2539 for (p = peer->path_head; NULL != p; p = next)
2542 for (i = 0; i < (p->length - 1); i++)
2544 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2545 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2547 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2558 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2560 peer_d = peer_get_short (d); // FIXME
2561 next = peer_get_best_path (peer_d);
2562 tunnel_use_path (peer->tunnel, next);
2563 peer_connect (peer_d);
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2570 * Add the path to the peer and update the path used to reach it in case this
2573 * @param peer_info Destination peer to add the path to.
2574 * @param path New path to add. Last peer must be the peer in arg 1.
2575 * Path will be either used of freed if already known.
2576 * @param trusted Do we trust that this path is real?
2579 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2582 struct MeshPeerPath *aux;
2586 if ((NULL == peer_info) || (NULL == path))
2589 path_destroy (path);
2592 if (path->peers[path->length - 1] != peer_info->id)
2595 path_destroy (path);
2598 if (2 >= path->length && GNUNET_NO == trusted)
2600 /* Only allow CORE to tell us about direct paths */
2601 path_destroy (path);
2604 for (l = 1; l < path->length; l++)
2606 if (path->peers[l] == myid)
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2609 for (l2 = 0; l2 < path->length - l; l2++)
2611 path->peers[l2] = path->peers[l + l2];
2616 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2621 path->length, peer2s (peer_info));
2623 l = path_get_length (path);
2626 path_destroy (path);
2630 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2631 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2633 l2 = path_get_length (aux);
2636 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2637 peer_info->path_tail, aux, path);
2642 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2644 path_destroy (path);
2649 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2656 * Add the path to the origin peer and update the path used to reach it in case
2657 * this is the shortest.
2658 * The path is given in peer_info -> destination, therefore we turn the path
2659 * upside down first.
2661 * @param peer_info Peer to add the path to, being the origin of the path.
2662 * @param path New path to add after being inversed.
2663 * Path will be either used or freed.
2664 * @param trusted Do we trust that this path is real?
2667 peer_add_path_to_origin (struct MeshPeer *peer_info,
2668 struct MeshPeerPath *path, int trusted)
2671 peer_add_path (peer_info, path, trusted);
2677 * Function called if a connection has been stalled for a while,
2678 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2680 * @param cls Closure (poll ctx).
2681 * @param tc TaskContext.
2684 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2686 struct MeshFlowControl *fc = cls;
2687 struct GNUNET_MESH_Poll msg;
2688 struct MeshConnection *c;
2690 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2691 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2699 peer2s (c->t->peer), c->id);
2701 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2702 msg.header.size = htons (sizeof (msg));
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2704 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2705 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2706 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2707 &connection_poll, fc);
2712 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2713 * to obtain a local peer -> destination path and interning the peer ids.
2715 * @return Newly allocated and created path
2717 static struct MeshPeerPath *
2718 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2719 unsigned int get_path_length,
2720 const struct GNUNET_PeerIdentity *put_path,
2721 unsigned int put_path_length)
2723 struct MeshPeerPath *p;
2729 GNUNET_PEER_change_rc (myid, 1);
2730 i = get_path_length;
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2732 for (i--; i >= 0; i--)
2734 id = GNUNET_PEER_intern (&get_path[i]);
2735 if (p->length > 0 && id == p->peers[p->length - 1])
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2738 GNUNET_PEER_change_rc (id, -1);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2743 GNUNET_i2s (&get_path[i]));
2745 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2746 p->peers[p->length - 1] = id;
2749 i = put_path_length;
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2751 for (i--; i >= 0; i--)
2753 id = GNUNET_PEER_intern (&put_path[i]);
2756 /* PUT path went through us, so discard the path up until now and start
2757 * from here to get a much shorter (and loop-free) path.
2762 if (p->length > 0 && id == p->peers[p->length - 1])
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2765 GNUNET_PEER_change_rc (id, -1);
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2770 GNUNET_i2s (&put_path[i]));
2772 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2773 p->peers[p->length - 1] = id;
2777 if (get_path_length > 0)
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2779 GNUNET_i2s (&get_path[0]));
2780 if (put_path_length > 0)
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2782 GNUNET_i2s (&put_path[0]));
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2785 for (i = 0; i < p->length; i++)
2787 struct GNUNET_PeerIdentity peer_id;
2789 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2791 GNUNET_i2s (&peer_id));
2799 * Adds a path to the peer_infos of all the peers in the path
2801 * @param p Path to process.
2802 * @param confirmed Whether we know if the path works or not.
2805 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2809 /* TODO: invert and add */
2810 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2811 for (i++; i < p->length; i++)
2813 struct MeshPeer *aux;
2814 struct MeshPeerPath *copy;
2816 aux = peer_get_short (p->peers[i]);
2817 copy = path_duplicate (p);
2818 copy->length = i + 1;
2819 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2825 * Search for a channel among the channels for a client
2827 * @param c the client whose channels to search in
2828 * @param chid the local id of the channel
2830 * @return channel handler, NULL if doesn't exist
2832 static struct MeshChannel *
2833 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2836 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2838 GNUNET_break_op (0);
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2842 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2843 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2844 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2848 * Search for a tunnel by global ID using full PeerIdentities.
2850 * @param t Tunnel containing the channel.
2851 * @param chid Public channel number.
2853 * @return channel handler, NULL if doesn't exist
2855 static struct MeshChannel *
2856 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2858 struct MeshChannel *ch;
2863 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2865 if (ch->gid == chid)
2874 * Change the tunnel state.
2876 * @param t Tunnel whose state to change.
2877 * @param state New state.
2880 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2885 "Tunnel %s state was %s\n",
2887 GNUNET_MESH_DEBUG_TS2S (t->state));
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2889 "Tunnel %s state is now %s\n",
2891 GNUNET_MESH_DEBUG_TS2S (state));
2897 * Cache a message to be sent once tunnel is online.
2899 * @param t Tunnel to hold the message.
2900 * @param ch Channel the message is about.
2901 * @param msg Message itself (copy will be made).
2902 * @param fwd Is this fwd?
2905 tunnel_queue_data (struct MeshTunnel2 *t,
2906 struct MeshChannel *ch,
2907 struct GNUNET_MessageHeader *msg,
2910 struct MeshTunnelQueue *tq;
2911 uint16_t size = ntohs (msg->size);
2913 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2916 memcpy (&tq[1], msg, size);
2917 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2922 * Send all cached messages that we can, tunnel is online.
2924 * @param t Tunnel that holds the messages.
2925 * @param fwd Is this fwd?
2928 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2930 struct MeshTunnelQueue *tq;
2931 struct MeshTunnelQueue *next;
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 "tunnel_send_queued_data on tunnel %s\n",
2937 room = tunnel_get_buffer (t, fwd);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2939 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2943 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2944 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2953 connection_change_state (struct MeshConnection* c,
2954 enum MeshConnectionState state)
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2957 "Connection %s state was %s\n",
2958 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2960 "Connection %s state is now %s\n",
2961 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
2967 * Add a client to a channel, initializing all needed data structures.
2969 * @param ch Channel to which add the client.
2970 * @param c Client which to add to the channel.
2973 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2975 struct MeshTunnel2 *t = ch->t;
2977 if (NULL != ch->dest)
2983 /* Assign local id as destination */
2984 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2985 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2986 ch->lid_dest = t->next_local_chid++;
2987 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2989 /* Store in client's hashmap */
2991 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2993 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2999 GNUNET_break (NULL == ch->dest_rel);
3000 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
3001 ch->dest_rel->ch = ch;
3002 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3008 static struct MeshConnection *
3009 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3011 struct MeshConnection *c;
3012 struct GNUNET_HashCode cid;
3013 struct MeshPeer *peer;
3014 unsigned int own_pos;
3016 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3018 c = connection_new (&cid);
3020 for (own_pos = 0; own_pos < p->length; own_pos++)
3022 if (p->peers[own_pos] == myid)
3025 if (own_pos > p->length - 1)
3028 connection_destroy (c);
3031 c->own_pos = own_pos;
3036 c->fwd_maintenance_task =
3037 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3038 &connection_fwd_keepalive, c);
3041 peer = connection_get_next_hop (c);
3042 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3043 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3044 peer = connection_get_prev_hop (c);
3045 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3046 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3052 * Notifies a tunnel that a connection has broken that affects at least
3053 * some of its peers. Sends a notification towards the root of the tree.
3054 * In case the peer is the owner of the tree, notifies the client that owns
3055 * the tunnel and tries to reconnect.
3057 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3059 * @param t Tunnel affected.
3060 * @param p1 Peer that got disconnected from p2.
3061 * @param p2 Peer that got disconnected from p1.
3063 * @return Short ID of the peer disconnected (either p1 or p2).
3064 * 0 if the tunnel remained unaffected.
3066 static GNUNET_PEER_Id
3067 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3068 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3070 // if (myid != p1 && myid != p2) FIXME
3075 // if (tree_get_predecessor (t->tree) != 0)
3077 // /* We are the peer still connected, notify owner of the disconnection. */
3078 // struct GNUNET_MESH_PathBroken msg;
3079 // struct GNUNET_PeerIdentity neighbor;
3081 // msg.header.size = htons (sizeof (msg));
3082 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3083 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3084 // msg.tid = htonl (t->id.tid);
3085 // msg.peer1 = my_full_id;
3086 // GNUNET_PEER_resolve (pid, &msg.peer2);
3087 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3088 // send_prebuilt_message (&msg.header, &neighbor, t);
3095 * Send an end-to-end ACK message for the most recent in-sequence payload.
3097 * If channel is not reliable, do nothing.
3099 * @param ch Channel this is about.
3100 * @param fwd Is for FWD traffic? (ACK dest->owner)
3103 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3105 struct GNUNET_MESH_DataACK msg;
3106 struct MeshChannelReliability *rel;
3107 struct MeshReliableMessage *copy;
3112 if (GNUNET_NO == ch->reliable)
3116 rel = fwd ? ch->dest_rel : ch->root_rel;
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3118 "send_data_ack for %u\n",
3121 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3122 msg.header.type = htons (type);
3123 msg.header.size = htons (sizeof (msg));
3124 msg.chid = htonl (ch->gid);
3125 msg.mid = htonl (rel->mid_recv - 1);
3127 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3129 if (copy->type != type)
3131 delta = copy->mid - rel->mid_recv;
3134 mask = 0x1LL << delta;
3135 msg.futures |= mask;
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3137 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3138 copy->mid, delta, mask, msg.futures);
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3142 send_prebuilt_message_channel (&msg.header, ch, fwd);
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3148 * Send an ACK informing the predecessor about the available buffer space.
3150 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3151 * the ACK itself goes "back" (dest->root).
3153 * @param c Connection on which to send the ACK.
3154 * @param buffer How much space free to advertise?
3155 * @param fwd Is this FWD ACK? (Going dest->owner)
3158 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
3160 struct MeshFlowControl *next_fc;
3161 struct MeshFlowControl *prev_fc;
3162 struct GNUNET_MESH_ACK msg;
3166 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3167 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3170 "connection send %s ack on %s\n",
3171 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3173 /* Check if we need to transmit the ACK */
3174 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3178 " last pid recv: %u, last ack sent: %u\n",
3179 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3183 /* Ok, ACK might be necessary, what PID to ACK? */
3184 delta = next_fc->queue_max - next_fc->queue_n;
3185 ack = prev_fc->last_pid_recv + delta;
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3188 " last pid %u, last ack %u, qmax %u, q %u\n",
3189 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3190 next_fc->queue_max, next_fc->queue_n);
3191 if (ack == prev_fc->last_ack_sent)
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3197 prev_fc->last_ack_sent = ack;
3199 /* Build ACK message and send on connection */
3200 msg.header.size = htons (sizeof (msg));
3201 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3202 msg.ack = htonl (ack);
3205 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3210 * Modify the mesh message TID from global to local and send to client.
3212 * @param ch Channel on which to send the message.
3213 * @param msg Message to modify and send.
3214 * @param c Client to send to.
3215 * @param tid Tunnel ID to use (c can be both owner and client).
3218 channel_send_client_to_tid (struct MeshChannel *ch,
3219 const struct GNUNET_MESH_Data *msg,
3220 struct MeshClient *c, MESH_ChannelNumber id)
3222 struct GNUNET_MESH_LocalData *copy;
3223 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3224 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3226 if (size < sizeof (struct GNUNET_MessageHeader))
3228 GNUNET_break_op (0);
3236 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3237 memcpy (©[1], &msg[1], size);
3238 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3239 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3240 copy->id = htonl (id);
3241 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3242 ©->header, GNUNET_NO);
3247 * We have received a message out of order, or the client is not ready.
3248 * Buffer it until we receive an ACK from the client or the missing
3249 * message from the channel.
3251 * @param msg Message to buffer (MUST be of type MESH_DATA).
3252 * @param rel Reliability data to the corresponding direction.
3255 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3256 struct MeshChannelReliability *rel)
3258 struct MeshReliableMessage *copy;
3259 struct MeshReliableMessage *prev;
3263 size = ntohs (msg->header.size);
3264 mid = ntohl (msg->mid);
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3268 copy = GNUNET_malloc (sizeof (*copy) + size);
3271 memcpy (©[1], msg, size);
3275 // FIXME do something better than O(n), although n < 64...
3276 // FIXME start from the end (most messages are the latest ones)
3277 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3280 if (GMC_is_pid_bigger (prev->mid, mid))
3282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3283 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3289 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3295 * Modify the data message ID from global to local and send to client.
3297 * @param ch Channel on which to send the message.
3298 * @param msg Message to modify and send.
3299 * @param fwd Forward?
3302 channel_send_client_data (struct MeshChannel *ch,
3303 const struct GNUNET_MESH_Data *msg,
3308 if (ch->dest_rel->client_ready)
3309 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3311 channel_rel_add_buffered_data (msg, ch->dest_rel);
3315 if (ch->root_rel->client_ready)
3316 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3318 channel_rel_add_buffered_data (msg, ch->root_rel);
3324 * Send a buffered message to the client, for in order delivery or
3325 * as result of client ACK.
3327 * @param ch Channel on which to empty the message buffer.
3328 * @param c Client to send to.
3329 * @param rel Reliability structure to corresponding peer.
3330 * If rel == bck_rel, this is FWD data.
3333 channel_send_client_buffered_data (struct MeshChannel *ch,
3334 struct MeshClient *c,
3337 struct MeshReliableMessage *copy;
3338 struct MeshChannelReliability *rel;
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3341 rel = fwd ? ch->dest_rel : ch->root_rel;
3342 if (GNUNET_NO == rel->client_ready)
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3348 copy = rel->head_recv;
3349 /* We never buffer channel management messages */
3352 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3354 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357 " have %u! now expecting %u\n",
3358 copy->mid, rel->mid_recv + 1);
3359 channel_send_client_data (ch, msg, fwd);
3362 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3368 " reliable && don't have %u, next is %u\n",
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3379 * Destroy a reliable message after it has been acknowledged, either by
3380 * direct mid ACK or bitfield. Updates the appropriate data structures and
3381 * timers and frees all memory.
3383 * @param copy Message that is no longer needed: remote peer got it.
3386 rel_message_free (struct MeshReliableMessage *copy)
3388 struct MeshChannelReliability *rel;
3389 struct GNUNET_TIME_Relative time;
3392 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3393 rel->expected_delay.rel_value_us *= 7;
3394 rel->expected_delay.rel_value_us += time.rel_value_us;
3395 rel->expected_delay.rel_value_us /= 8;
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3400 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3402 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3404 rel->retry_timer = rel->expected_delay;
3405 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3411 * Destroy all reliable messages queued for a channel,
3412 * during a channel destruction.
3413 * Frees the reliability structure itself.
3415 * @param rel Reliability data for a channel.
3418 channel_rel_free_all (struct MeshChannelReliability *rel)
3420 struct MeshReliableMessage *copy;
3421 struct MeshReliableMessage *next;
3426 for (copy = rel->head_recv; NULL != copy; copy = next)
3429 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3432 for (copy = rel->head_sent; NULL != copy; copy = next)
3435 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3438 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3439 GNUNET_SCHEDULER_cancel (rel->retry_task);
3445 * Mark future messages as ACK'd.
3447 * @param rel Reliability data.
3448 * @param msg DataACK message with a bitfield of future ACK'd messages.
3451 channel_rel_free_sent (struct MeshChannelReliability *rel,
3452 const struct GNUNET_MESH_DataACK *msg)
3454 struct MeshReliableMessage *copy;
3455 struct MeshReliableMessage *next;
3462 bitfield = msg->futures;
3463 mid = ntohl (msg->mid);
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3465 "free_sent_reliable %u %llX\n",
3467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3468 " rel %p, head %p\n",
3469 rel, rel->head_sent);
3470 for (i = 0, copy = rel->head_sent;
3471 i < 64 && NULL != copy && 0 != bitfield;
3474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3475 " trying bit %u (mid %u)\n",
3478 if (0 == (bitfield & mask))
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3482 /* Bit was set, clear the bit from the bitfield */
3485 /* The i-th bit was set. Do we have that copy? */
3486 /* Skip copies with mid < target */
3487 target = mid + i + 1;
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3489 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3492 /* Did we run out of copies? (previously freed, it's ok) */
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3499 /* Did we overshoot the target? (previously freed, it's ok) */
3500 if (GMC_is_pid_bigger (copy->mid, target))
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3506 /* Now copy->mid == target, free it */
3508 rel_message_free (copy);
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3516 * We haven't received an ACK after a certain time: restransmit the message.
3518 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3519 * @param tc TaskContext.
3522 channel_retransmit_message (void *cls,
3523 const struct GNUNET_SCHEDULER_TaskContext *tc)
3525 struct MeshChannelReliability *rel = cls;
3526 struct MeshReliableMessage *copy;
3527 struct MeshPeerQueue *q;
3528 struct MeshChannel *ch;
3529 struct MeshConnection *c;
3530 struct GNUNET_MESH_Data *payload;
3531 struct MeshPeer *hop;
3534 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3535 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3539 copy = rel->head_sent;
3546 /* Search the message to be retransmitted in the outgoing queue.
3547 * Check only the queue for the connection that is going to be used,
3548 * if the message is stuck in some other connection's queue we shouldn't
3550 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3551 * the old connection could be temporary stalled or the queue happened to
3552 * be long at time of insertion.
3553 * - not sending the new one could cause terrible delays the old connection
3556 payload = (struct GNUNET_MESH_Data *) ©[1];
3557 fwd = (rel == ch->root_rel);
3558 c = tunnel_get_connection (ch->t, fwd);
3559 hop = connection_get_hop (c, fwd);
3560 for (q = hop->queue_head; NULL != q; q = q->next)
3562 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3564 struct GNUNET_MESH_Data *queued_data = q->cls;
3566 if (queued_data->mid == payload->mid)
3571 /* Message not found in the queue that we are going to use. */
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3576 send_prebuilt_message_channel (&payload->header, ch, fwd);
3577 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3584 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3585 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3586 &channel_retransmit_message,
3592 * Send ACK on one or more connections due to buffer space to the client.
3594 * Iterates all connections of the tunnel and sends ACKs appropriately.
3596 * @param ch Channel which has some free buffer space.
3597 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3600 channel_send_connections_ack (struct MeshChannel *ch,
3601 unsigned int buffer,
3604 struct MeshTunnel2 *t = ch->t;
3605 struct MeshConnection *c;
3606 struct MeshFlowControl *fc;
3609 uint32_t allow_per_connection;
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3613 "Channel send connection %s ack on %s:%X\n",
3614 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3616 /* Count connections, how many messages are already allowed */
3617 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3619 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3620 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3625 allowed += fc->last_ack_sent - fc->last_pid_recv;
3629 /* Make sure there is no overflow */
3630 if (allowed > buffer)
3636 /* Authorize connections to send more data */
3637 to_allow = buffer - allowed;
3639 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3641 allow_per_connection = to_allow/cs;
3642 to_allow -= allow_per_connection;
3644 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3645 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3649 connection_send_ack (c, allow_per_connection, fwd);
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3653 "Channel send connection %s ack on %s:%X\n",
3654 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3655 GNUNET_break (to_allow == 0);
3660 * Get free buffer space towards the client on a specific channel.
3662 * @param ch Channel.
3663 * @param fwd Is query about FWD traffic?
3665 * @return Free buffer space [0 - 64]
3668 channel_get_buffer (struct MeshChannel *ch, int fwd)
3670 struct MeshChannelReliability *rel;
3672 rel = fwd ? ch->dest_rel : ch->root_rel;
3674 return (64 - rel->n_recv);
3679 * Get free buffer space in a connection.
3681 * @param c Connection.
3682 * @param fwd Is query about FWD traffic?
3684 * @return Free buffer space [0 - max_msgs_queue/max_connections]
3687 connection_get_buffer (struct MeshConnection *c, int fwd)
3689 struct MeshFlowControl *fc;
3691 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3693 return (fc->queue_max - fc->queue_n);
3698 * Send an ACK on the appropriate connection/channel, depending on
3699 * the direction and the position of the peer.
3701 * @param c Which connection to send the hop-by-hop ACK.
3702 * @param ch Channel, if any.
3703 * @param fwd Is this a fwd ACK? (will go dest->root)
3706 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3708 unsigned int buffer;
3710 if (NULL == c || connection_is_terminal (c, fwd))
3712 GNUNET_assert (NULL != ch);
3713 buffer = channel_get_buffer (ch, fwd);
3717 GNUNET_assert (NULL != c);
3718 buffer = connection_get_buffer (c, fwd);
3723 GNUNET_assert (NULL != ch);
3724 channel_send_connections_ack (ch, buffer, fwd);
3726 else if (connection_is_origin (c, fwd))
3730 GNUNET_assert (NULL != ch);
3731 send_local_ack (ch, fwd);
3736 connection_send_ack (c, buffer, fwd);
3742 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3744 * @param ch Channel to mark as ready.
3745 * @param fwd Was the CREATE message sent fwd?
3748 channel_confirm (struct MeshChannel *ch, int fwd)
3750 struct MeshChannelReliability *rel;
3751 struct MeshReliableMessage *copy;
3752 struct MeshReliableMessage *next;
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3755 " channel confirm %s:%X\n",
3756 peer2s (ch->t->peer), ch->gid);
3757 ch->state = MESH_CHANNEL_READY;
3759 rel = fwd ? ch->root_rel : ch->dest_rel;
3760 for (copy = rel->head_sent; NULL != copy; copy = next)
3762 struct GNUNET_MessageHeader *msg;
3765 msg = (struct GNUNET_MessageHeader *) ©[1];
3766 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3768 rel_message_free (copy);
3776 * Save a copy to retransmit in case it gets lost.
3778 * Initializes all needed callbacks and timers.
3780 * @param ch Channel this message goes on.
3781 * @param msg Message to copy.
3782 * @param fwd Is this fwd traffic?
3785 channel_save_copy (struct MeshChannel *ch,
3786 const struct GNUNET_MessageHeader *msg,
3789 struct MeshChannelReliability *rel;
3790 struct MeshReliableMessage *copy;
3795 rel = fwd ? ch->root_rel : ch->dest_rel;
3796 mid = rel->mid_send;
3797 type = ntohs (msg->type);
3798 size = ntohs (msg->size);
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3801 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3803 copy->timestamp = GNUNET_TIME_absolute_get ();
3806 memcpy (©[1], msg, size);
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3809 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3810 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3813 GNUNET_TIME_relative_multiply (rel->expected_delay,
3814 MESH_RETRANSMIT_MARGIN);
3816 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3817 &channel_retransmit_message,
3824 * Send keepalive packets for a connection.
3826 * @param c Connection to keep alive..
3827 * @param fwd Is this a FWD keepalive? (owner -> dest).
3830 connection_keepalive (struct MeshConnection *c, int fwd)
3832 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3833 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3837 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3838 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3841 "sending %s keepalive for connection %s[%d]\n",
3842 fwd ? "FWD" : "BCK",
3843 peer2s (c->t->peer),
3846 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3847 msg->header.size = htons (size);
3848 msg->header.type = htons (type);
3851 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3856 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3858 * @param c Connection for which to send the message.
3859 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3862 connection_recreate (struct MeshConnection *c, int fwd)
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3866 send_connection_create (c);
3868 send_connection_ack (c, GNUNET_NO);
3873 * Generic connection timer management.
3874 * Depending on the role of the peer in the connection will send the
3875 * appropriate message (build or keepalive)
3877 * @param c Conncetion to maintain.
3878 * @param fwd Is FWD?
3881 connection_maintain (struct MeshConnection *c, int fwd)
3883 if (MESH_TUNNEL_SEARCHING == c->t->state)
3885 /* TODO DHT GET with RO_BART */
3890 case MESH_CONNECTION_NEW:
3892 case MESH_CONNECTION_SENT:
3893 connection_recreate (c, fwd);
3895 case MESH_CONNECTION_READY:
3896 connection_keepalive (c, fwd);
3905 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3907 struct MeshConnection *c = cls;
3909 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3910 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3913 connection_maintain (c, GNUNET_YES);
3914 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3915 &connection_fwd_keepalive,
3921 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3923 struct MeshConnection *c = cls;
3925 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3926 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3929 connection_maintain (c, GNUNET_NO);
3930 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3931 &connection_bck_keepalive,
3937 * Send a message to all peers in this connection that the connection
3938 * is no longer valid.
3940 * If some peer should not receive the message, it should be zero'ed out
3941 * before calling this function.
3943 * @param c The connection whose peers to notify.
3946 connection_send_destroy (struct MeshConnection *c)
3948 struct GNUNET_MESH_ConnectionDestroy msg;
3950 msg.header.size = htons (sizeof (msg));
3951 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3954 " sending connection destroy for connection %s[%X]\n",
3955 peer2s (c->t->peer),
3958 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3959 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3960 c->destroy = GNUNET_YES;
3965 * Confirm we got a channel create.
3967 * @param ch The channel to confirm.
3968 * @param fwd Should we send the ACK fwd?
3971 channel_send_ack (struct MeshChannel *ch, int fwd)
3973 struct GNUNET_MESH_ChannelManage msg;
3975 msg.header.size = htons (sizeof (msg));
3976 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3978 " sending channel ack for channel %s:%X\n",
3979 peer2s (ch->t->peer),
3982 msg.chid = htonl (ch->gid);
3983 send_prebuilt_message_channel (&msg.header, ch, fwd);
3988 * Send a message to all clients (local and remote) of this channel
3989 * notifying that the channel is no longer valid.
3991 * If some peer or client should not receive the message,
3992 * should be zero'ed out before calling this function.
3994 * @param ch The channel whose clients to notify.
3997 channel_send_destroy (struct MeshChannel *ch)
3999 struct GNUNET_MESH_ChannelManage msg;
4001 msg.header.size = htons (sizeof (msg));
4002 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4004 " sending channel destroy for channel %s:%X\n",
4005 peer2s (ch->t->peer),
4008 if (NULL != ch->root)
4010 msg.chid = htonl (ch->lid_root);
4011 send_local_channel_destroy (ch, GNUNET_NO);
4015 msg.chid = htonl (ch->gid);
4016 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
4019 if (NULL != ch->dest)
4021 msg.chid = htonl (ch->lid_dest);
4022 send_local_channel_destroy (ch, GNUNET_YES);
4026 msg.chid = htonl (ch->gid);
4027 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
4035 static struct MeshTunnel2 *
4038 struct MeshTunnel2 *t;
4040 t = GNUNET_new (struct MeshTunnel2);
4042 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4044 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
4045 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4047 // GNUNET_break (0);
4048 // tunnel_destroy (t);
4052 // char salt[] = "salt";
4053 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4054 // salt, sizeof (salt),
4055 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4056 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4057 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4059 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4060 // salt, sizeof (salt),
4061 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4062 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4063 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4071 * Add a connection to a tunnel.
4074 * @param c Connection.
4077 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4080 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4085 * Initialize a Flow Control structure to the initial state.
4087 * @param fc Flow Control structure to initialize.
4090 fc_init (struct MeshFlowControl *fc)
4093 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4094 fc->last_pid_recv = (uint32_t) -1;
4095 fc->last_ack_sent = (uint32_t) 0;
4096 fc->last_ack_recv = (uint32_t) 0;
4097 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4098 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4100 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4104 static struct MeshConnection *
4105 connection_new (const struct GNUNET_HashCode *cid)
4107 struct MeshConnection *c;
4109 c = GNUNET_new (struct MeshConnection);
4111 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4112 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4113 fc_init (&c->fwd_fc);
4114 fc_init (&c->bck_fc);
4123 * Find a connection.
4125 * @param cid Connection ID.
4127 static struct MeshConnection *
4128 connection_get (const struct GNUNET_HashCode *cid)
4130 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4135 connection_destroy (struct MeshConnection *c)
4137 struct MeshPeer *peer;
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4143 peer2s (c->t->peer),
4146 /* Cancel all traffic */
4147 connection_cancel_queues (c, GNUNET_YES);
4148 connection_cancel_queues (c, GNUNET_NO);
4150 /* Cancel maintainance task (keepalive/timeout) */
4151 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4152 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4153 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4154 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4156 /* Deregister from neighbors */
4157 peer = connection_get_next_hop (c);
4159 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4160 peer = connection_get_prev_hop (c);
4162 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4165 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4166 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4172 tunnel_destroy (struct MeshTunnel2 *t)
4174 struct MeshConnection *c;
4175 struct MeshConnection *next;
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4183 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4184 // GNUNET_break (0);
4186 for (c = t->connection_head; NULL != c; c = next)
4189 connection_destroy (c);
4192 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4199 * Tunnel is empty: destroy it.
4201 * Notifies all connections about the destruction.
4203 * @param t Tunnel to destroy.
4206 tunnel_destroy_empty (struct MeshTunnel2 *t)
4208 struct MeshConnection *c;
4210 for (c = t->connection_head; NULL != c; c = c->next)
4212 if (GNUNET_NO == c->destroy)
4213 connection_send_destroy (c);
4216 if (0 == t->pending_messages)
4219 t->destroy = GNUNET_YES;
4224 * Destroy tunnel if empty (no more channels).
4226 * @param t Tunnel to destroy if empty.
4229 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4231 if (NULL != t->channel_head)
4234 tunnel_destroy_empty (t);
4239 * Destroy a channel and free all resources.
4241 * @param ch Channel to destroy.
4244 channel_destroy (struct MeshChannel *ch)
4246 struct MeshClient *c;
4254 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4265 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4272 channel_rel_free_all (ch->root_rel);
4273 channel_rel_free_all (ch->dest_rel);
4275 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4276 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4282 * Create a new channel.
4284 * @param t Tunnel this channel is in.
4285 * @param owner Client that owns the channel, NULL for foreign channels.
4286 * @param lid_root Local ID for root client.
4288 * @return A new initialized channel. NULL on error.
4290 static struct MeshChannel *
4291 channel_new (struct MeshTunnel2 *t,
4292 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4294 struct MeshChannel *ch;
4296 ch = GNUNET_new (struct MeshChannel);
4298 ch->lid_root = lid_root;
4301 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4303 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4307 while (NULL != channel_get (t, t->next_chid))
4308 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4309 ch->gid = t->next_chid;
4310 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4313 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4317 channel_destroy (ch);
4318 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4328 * Set options in a channel, extracted from a bit flag field
4330 * @param ch Channel to set options to.
4331 * @param options Bit array in host byte order.
4334 channel_set_options (struct MeshChannel *ch, uint32_t options)
4336 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4337 GNUNET_YES : GNUNET_NO;
4338 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4339 GNUNET_YES : GNUNET_NO;
4344 * Iterator for deleting each channel whose client endpoint disconnected.
4346 * @param cls Closure (client that has disconnected).
4347 * @param key The local channel id (used to access the hashmap).
4348 * @param value The value stored at the key (channel to destroy).
4350 * @return GNUNET_OK, keep iterating.
4353 channel_destroy_iterator (void *cls,
4357 struct MeshChannel *ch = value;
4358 struct MeshClient *c = cls;
4359 struct MeshTunnel2 *t;
4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4362 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4363 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4377 channel_send_destroy (ch);
4378 channel_destroy (ch);
4379 tunnel_destroy_if_empty (t);
4386 * Remove client's ports from the global hashmap on disconnect.
4388 * @param cls Closure (unused).
4390 * @param value Client structure.
4392 * @return GNUNET_OK, keep iterating.
4395 client_release_ports (void *cls,
4401 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4402 if (GNUNET_YES != res)
4405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4406 "Port %u by client %p was not registered.\n",
4414 * Timeout function due to lack of keepalive/traffic from the owner.
4415 * Destroys connection if called.
4417 * @param cls Closure (connection to destroy).
4418 * @param tc TaskContext.
4421 connection_fwd_timeout (void *cls,
4422 const struct GNUNET_SCHEDULER_TaskContext *tc)
4424 struct MeshConnection *c = cls;
4426 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4427 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4430 "Connection %s[%X] FWD timed out. Destroying.\n",
4431 peer2s (c->t->peer),
4434 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4437 connection_destroy (c);
4442 * Timeout function due to lack of keepalive/traffic from the destination.
4443 * Destroys connection if called.
4445 * @param cls Closure (connection to destroy).
4446 * @param tc TaskContext
4449 connection_bck_timeout (void *cls,
4450 const struct GNUNET_SCHEDULER_TaskContext *tc)
4452 struct MeshConnection *c = cls;
4454 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4455 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4459 "Connection %s[%X] FWD timed out. Destroying.\n",
4460 peer2s (c->t->peer),
4463 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4466 connection_destroy (c);
4471 * Resets the connection timeout task, some other message has done the
4473 * - For the first peer on the direction this means to send
4474 * a keepalive or a path confirmation message (either create or ACK).
4475 * - For all other peers, this means to destroy the connection,
4476 * due to lack of activity.
4477 * Starts the tiemout if no timeout was running (connection just created).
4479 * @param c Connection whose timeout to reset.
4480 * @param fwd Is this forward?
4482 * TODO use heap to improve efficiency of scheduler.
4485 connection_reset_timeout (struct MeshConnection *c, int fwd)
4487 GNUNET_SCHEDULER_TaskIdentifier *ti;
4488 GNUNET_SCHEDULER_Task f;
4490 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4492 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4493 GNUNET_SCHEDULER_cancel (*ti);
4495 if (connection_is_origin (c, fwd)) /* Endpoint */
4497 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4498 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4502 struct GNUNET_TIME_Relative delay;
4504 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4505 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4506 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4512 * Iterator to notify all connections of a broken link. Mark connections
4513 * to destroy after all traffic has been sent.
4515 * @param cls Closure (peer disconnected).
4516 * @param key Current key code (tid).
4517 * @param value Value in the hash map (connection).
4519 * @return GNUNET_YES if we should continue to iterate,
4523 connection_broken (void *cls,
4524 const struct GNUNET_HashCode *key,
4527 struct MeshPeer *peer = cls;
4528 struct MeshConnection *c = value;
4529 struct GNUNET_MESH_ConnectionBroken msg;
4532 fwd = peer == connection_get_prev_hop (c);
4533 connection_cancel_queues (c, !fwd);
4535 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4536 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4538 msg.peer1 = my_full_id;
4539 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4540 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4541 c->destroy = GNUNET_YES;
4546 /******************************************************************************/
4547 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4548 /******************************************************************************/
4551 * Free a transmission that was already queued with all resources
4552 * associated to the request.
4554 * @param queue Queue handler to cancel.
4555 * @param clear_cls Is it necessary to free associated cls?
4558 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4560 struct MeshPeer *peer;
4561 struct MeshFlowControl *fc;
4566 GNUNET_assert (NULL != queue->c);
4567 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4569 if (GNUNET_YES == clear_cls)
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4572 GNUNET_MESH_DEBUG_M2S (queue->type));
4573 switch (queue->type)
4575 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4576 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4577 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4578 GNUNET_break (GNUNET_YES == queue->c->destroy);
4580 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4581 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4582 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4583 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4584 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4585 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4587 GNUNET_free_non_null (queue->cls);
4592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4593 GNUNET_MESH_DEBUG_M2S (queue->type));
4597 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4599 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4600 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4606 if (NULL != queue->c)
4608 queue->c->pending_messages--;
4609 if (NULL != queue->c->t)
4611 queue->c->t->pending_messages--;
4615 GNUNET_free (queue);
4620 queue_send (void *cls, size_t size, void *buf)
4622 struct MeshPeer *peer = cls;
4623 struct MeshFlowControl *fc;
4624 struct MeshConnection *c;
4625 struct GNUNET_MessageHeader *msg;
4626 struct MeshPeerQueue *queue;
4627 struct MeshTunnel2 *t;
4628 struct MeshChannel *ch;
4629 const struct GNUNET_PeerIdentity *dst_id;
4635 peer->core_transmit = NULL;
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4638 if (NULL == buf || 0 == size)
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4645 queue = peer_get_first_message (peer);
4648 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4653 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4656 dst_id = GNUNET_PEER_resolve2 (peer->id);
4657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4658 /* Check if buffer size is enough for the message */
4659 if (queue->size > size)
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4662 peer->core_transmit =
4663 GNUNET_CORE_notify_transmit_ready (core_handle,
4666 GNUNET_TIME_UNIT_FOREVER_REL,
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4675 t = (NULL != c) ? c->t : NULL;
4679 switch (queue->type)
4681 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4682 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4683 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4684 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4685 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4686 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4689 GNUNET_MESH_DEBUG_M2S (queue->type));
4690 data_size = send_core_data_raw (queue->cls, size, buf);
4691 msg = (struct GNUNET_MessageHeader *) buf;
4692 type = ntohs (msg->type);
4694 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4696 if (connection_is_origin (c, GNUNET_YES))
4697 data_size = send_core_connection_create (queue->c, size, buf);
4699 data_size = send_core_data_raw (queue->cls, size, buf);
4701 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4703 if (connection_is_origin (c, GNUNET_NO))
4704 data_size = send_core_connection_ack (queue->c, size, buf);
4706 data_size = send_core_data_raw (queue->cls, size, buf);
4708 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4709 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4710 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4711 /* This should be encapsulted */
4716 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4721 if (0 < drop_percent &&
4722 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4725 "Dropping message of type %s\n",
4726 GNUNET_MESH_DEBUG_M2S (queue->type));
4730 /* Free queue, but cls was freed by send_core_* */
4732 queue_destroy (queue, GNUNET_NO);
4734 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4737 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4738 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4739 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4740 fc->last_pid_sent = pid;
4741 send_ack (c, ch, fwd);
4747 /* If more data in queue, send next */
4748 queue = peer_get_first_message (peer);
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4752 if (NULL == peer->core_transmit) {
4753 peer->core_transmit =
4754 GNUNET_CORE_notify_transmit_ready(core_handle,
4757 GNUNET_TIME_UNIT_FOREVER_REL,
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4766 "* tmt rdy called somewhere else\n");
4768 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4772 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4777 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4779 GNUNET_SCHEDULER_cancel (fc->poll_task);
4780 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4785 c->pending_messages--;
4786 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4789 connection_destroy (c);
4795 t->pending_messages--;
4796 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4798 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4808 queue_add (void *cls, uint16_t type, size_t size,
4809 struct MeshConnection *c,
4810 struct MeshChannel *ch,
4813 struct MeshPeerQueue *queue;
4814 struct MeshFlowControl *fc;
4815 struct MeshPeer *peer;
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4819 "queue add %s %s (%u) on c %p, ch %p\n",
4820 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4821 GNUNET_assert (NULL != c);
4823 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4824 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4834 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4835 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4842 if (fc->queue_n >= fc->queue_max && 0 == priority)
4844 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4848 "queue full: %u/%u\n",
4849 fc->queue_n, fc->queue_max);
4850 return; /* Drop this message */
4853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4855 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4856 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4859 "no buffer space (%u > %u): starting poll\n",
4860 fc->last_pid_sent + 1, fc->last_ack_recv);
4861 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4865 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4873 if (100 <= priority)
4875 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4879 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
4885 if (NULL == peer->core_transmit)
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4888 "calling core tmt rdy towards %s for %u bytes\n",
4889 peer2s (peer), size);
4890 peer->core_transmit =
4891 GNUNET_CORE_notify_transmit_ready (core_handle,
4894 GNUNET_TIME_UNIT_FOREVER_REL,
4895 GNUNET_PEER_resolve2 (peer->id),
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4903 "core tmt rdy towards %s already called\n",
4907 c->pending_messages++;
4908 c->t->pending_messages++;
4912 /******************************************************************************/
4913 /******************** MESH NETWORK HANDLERS **************************/
4914 /******************************************************************************/
4918 * Generic handler for mesh network payload traffic.
4920 * @param t Tunnel on which we got this message.
4921 * @param message Unencryted data message.
4922 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4924 * @return channel which this message was on.
4926 static struct MeshChannel *
4927 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4929 struct MeshChannelReliability *rel;
4930 struct MeshChannel *ch;
4931 struct MeshClient *c;
4937 size = ntohs (msg->header.size);
4939 sizeof (struct GNUNET_MESH_Data) +
4940 sizeof (struct GNUNET_MessageHeader))
4945 type = ntohs (msg->header.type);
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4947 GNUNET_MESH_DEBUG_M2S (type));
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4949 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4952 ch = channel_get (t, ntohl (msg->chid));
4955 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4960 /* Initialize FWD/BCK data */
4961 c = fwd ? ch->dest : ch->root;
4962 rel = fwd ? ch->dest_rel : ch->root_rel;
4970 tunnel_change_state (t, MESH_TUNNEL_READY);
4972 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4974 mid = ntohl (msg->mid);
4975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4977 if (GNUNET_NO == ch->reliable ||
4978 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4979 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4982 if (GNUNET_YES == ch->reliable)
4984 /* Is this the exact next expected messasge? */
4985 if (mid == rel->mid_recv)
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4989 channel_send_client_data (ch, msg, fwd);
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4994 channel_rel_add_buffered_data (msg, rel);
4999 /* Tunnel is unreliable: send to clients directly */
5000 /* FIXME: accept Out Of Order traffic */
5001 rel->mid_recv = mid + 1;
5002 channel_send_client_data (ch, msg, fwd);
5007 GNUNET_break_op (0);
5008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5009 " MID %u not expected (%u - %u), dropping!\n",
5010 mid, rel->mid_recv, rel->mid_recv + 64);
5013 channel_send_data_ack (ch, fwd);
5018 * Handler for mesh network traffic end-to-end ACKs.
5020 * @param t Tunnel on which we got this message.
5021 * @param message Data message.
5022 * @param fwd Is this a fwd ACK? (dest->orig)
5024 * @return channel this message was on.
5026 static struct MeshChannel *
5027 handle_data_ack (struct MeshTunnel2 *t,
5028 const struct GNUNET_MESH_DataACK *msg, int fwd)
5030 struct MeshChannelReliability *rel;
5031 struct MeshReliableMessage *copy;
5032 struct MeshReliableMessage *next;
5033 struct MeshChannel *ch;
5038 type = ntohs (msg->header.type);
5039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
5040 GNUNET_MESH_DEBUG_M2S (type));
5041 ch = channel_get (t, ntohl (msg->chid));
5044 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5047 ack = ntohl (msg->mid);
5048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5049 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5051 if (GNUNET_YES == fwd)
5065 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5067 if (GMC_is_pid_bigger (copy->mid, ack))
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5070 channel_rel_free_sent (rel, msg);
5074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5076 rel_message_free (copy);
5078 /* ACK client if needed */
5079 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5081 /* If some message was free'd, update the retransmission delay*/
5082 if (GNUNET_YES == work)
5084 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5086 GNUNET_SCHEDULER_cancel (rel->retry_task);
5087 if (NULL == rel->head_sent)
5089 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5093 struct GNUNET_TIME_Absolute new_target;
5094 struct GNUNET_TIME_Relative delay;
5096 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5097 MESH_RETRANSMIT_MARGIN);
5098 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5100 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5102 GNUNET_SCHEDULER_add_delayed (delay,
5103 &channel_retransmit_message,
5115 * Core handler for connection creation.
5117 * @param cls Closure (unused).
5118 * @param peer Sender (neighbor).
5119 * @param message Message.
5121 * @return GNUNET_OK to keep the connection open,
5122 * GNUNET_SYSERR to close it (signal serious error)
5125 handle_mesh_connection_create (void *cls,
5126 const struct GNUNET_PeerIdentity *peer,
5127 const struct GNUNET_MessageHeader *message)
5129 struct GNUNET_MESH_ConnectionCreate *msg;
5130 struct GNUNET_PeerIdentity *id;
5131 struct GNUNET_HashCode *cid;
5132 struct MeshPeerPath *path;
5133 struct MeshPeer *dest_peer;
5134 struct MeshPeer *orig_peer;
5135 struct MeshConnection *c;
5136 unsigned int own_pos;
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5144 size = ntohs (message->size);
5145 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5147 GNUNET_break_op (0);
5151 /* Calculate hops */
5152 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5153 if (size % sizeof (struct GNUNET_PeerIdentity))
5155 GNUNET_break_op (0);
5158 size /= sizeof (struct GNUNET_PeerIdentity);
5161 GNUNET_break_op (0);
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5166 /* Get parameters */
5167 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5169 id = (struct GNUNET_PeerIdentity *) &msg[1];
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5171 " connection %s (%s).\n",
5172 GNUNET_h2s (cid), GNUNET_i2s (id));
5174 /* Create connection */
5175 c = connection_get (cid);
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5179 c = connection_new (cid);
5183 connection_reset_timeout (c, GNUNET_YES);
5184 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5186 /* Remember peers */
5187 dest_peer = peer_get (&id[size - 1]);
5188 orig_peer = peer_get (&id[0]);
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5192 path = path_new (size);
5194 for (i = 0; i < size; i++)
5196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5197 GNUNET_i2s (&id[i]));
5198 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5199 if (path->peers[i] == myid)
5202 if (own_pos == 0 && path->peers[own_pos] != myid)
5204 /* create path: self not found in path through self */
5205 GNUNET_break_op (0);
5206 path_destroy (path);
5207 connection_destroy (c);
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5211 path_add_to_peers (path, GNUNET_NO);
5212 c->path = path_duplicate (path);
5213 c->own_pos = own_pos;
5215 /* Is it a connection to us? */
5216 if (own_pos == size - 1)
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5219 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5221 if (NULL == orig_peer->tunnel)
5222 orig_peer->tunnel = tunnel_new ();
5223 tunnel_add_connection (orig_peer->tunnel, c);
5225 send_connection_ack (c, GNUNET_NO);
5227 /* Keep tunnel alive in direction dest->owner*/
5228 connection_reset_timeout (c, GNUNET_NO);
5232 /* It's for somebody else! Retransmit. */
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5234 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5235 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5236 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5243 * Core handler for path ACKs
5245 * @param cls closure
5246 * @param message message
5247 * @param peer peer identity this notification is about
5249 * @return GNUNET_OK to keep the connection open,
5250 * GNUNET_SYSERR to close it (signal serious error)
5253 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5254 const struct GNUNET_MessageHeader *message)
5256 struct GNUNET_MESH_ConnectionACK *msg;
5257 struct MeshPeerPath *p;
5258 struct MeshConnection *c;
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5262 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5264 GNUNET_h2s (&msg->cid));
5265 c = connection_get (&msg->cid);
5268 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5277 /* Add path to peers? */
5281 path_add_to_peers (p, GNUNET_YES);
5287 connection_change_state (c, MESH_CONNECTION_READY);
5288 connection_reset_timeout (c, GNUNET_NO);
5289 if (MESH_TUNNEL_READY != c->t->state)
5290 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5291 tunnel_send_queued_data (c->t, GNUNET_YES);
5293 /* Message for us? */
5294 if (connection_is_terminal (c, GNUNET_NO))
5296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5297 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5299 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5300 c->t->peer->dhtget = NULL;
5302 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5307 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5313 * Core handler for notifications of broken paths
5315 * @param cls Closure (unused).
5316 * @param peer Peer identity of sending neighbor.
5317 * @param message Message.
5319 * @return GNUNET_OK to keep the connection open,
5320 * GNUNET_SYSERR to close it (signal serious error)
5323 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5324 const struct GNUNET_MessageHeader *message)
5326 struct GNUNET_MESH_ConnectionBroken *msg;
5327 struct MeshConnection *c;
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5330 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5331 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5333 GNUNET_i2s (&msg->peer1));
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5335 GNUNET_i2s (&msg->peer2));
5336 c = connection_get (&msg->cid);
5339 GNUNET_break_op (0);
5342 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5343 GNUNET_PEER_search (&msg->peer2));
5350 * Core handler for tunnel destruction
5352 * @param cls Closure (unused).
5353 * @param peer Peer identity of sending neighbor.
5354 * @param message Message.
5356 * @return GNUNET_OK to keep the connection open,
5357 * GNUNET_SYSERR to close it (signal serious error)
5360 handle_mesh_connection_destroy (void *cls,
5361 const struct GNUNET_PeerIdentity *peer,
5362 const struct GNUNET_MessageHeader *message)
5364 struct GNUNET_MESH_ConnectionDestroy *msg;
5365 struct MeshConnection *c;
5369 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5371 "Got a CONNECTION DESTROY message from %s\n",
5373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5374 " for connection %s\n",
5375 GNUNET_h2s (&msg->cid));
5376 c = connection_get (&msg->cid);
5379 /* Probably already got the message from another path,
5380 * destroyed the tunnel and retransmitted to children.
5383 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5387 id = GNUNET_PEER_search (peer);
5388 if (id == connection_get_prev_hop (c)->id)
5390 else if (id == connection_get_next_hop (c)->id)
5394 GNUNET_break_op (0);
5397 send_prebuilt_message_connection (message, c, NULL, fwd);
5398 c->destroy = GNUNET_YES;
5405 * Handler for channel create messages.
5407 * @param t Tunnel this channel is to be created in.
5408 * @param msg Message.
5409 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5411 * @return channel this message was on.
5413 static struct MeshChannel *
5414 handle_channel_create (struct MeshTunnel2 *t,
5415 struct GNUNET_MESH_ChannelCreate *msg,
5418 MESH_ChannelNumber chid;
5419 struct MeshChannel *ch;
5420 struct MeshClient *c;
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5424 /* Check message size */
5425 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5427 GNUNET_break_op (0);
5431 /* Check if channel exists */
5432 chid = ntohl (msg->chid);
5433 ch = channel_get (t, chid);
5436 /* Probably a retransmission, safe to ignore */
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5438 if (NULL != ch->dest)
5440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5441 channel_send_ack (ch, !fwd);
5447 /* Create channel */
5448 ch = channel_new (t, NULL, 0);
5449 channel_set_options (ch, ntohl (msg->opt));
5452 /* Find a destination client */
5453 port = ntohl (msg->port);
5454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5455 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5458 /* TODO send reject */
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5464 channel_add_client (ch, c);
5465 if (GNUNET_YES == ch->reliable)
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5468 send_local_channel_create (ch);
5469 channel_send_ack (ch, !fwd);
5476 * Handler for channel ack 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_ack (struct MeshTunnel2 *t,
5486 struct GNUNET_MESH_ChannelManage *msg,
5489 MESH_ChannelNumber chid;
5490 struct MeshChannel *ch;
5492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5493 /* Check message size */
5494 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5496 GNUNET_break_op (0);
5500 /* Check if channel exists */
5501 chid = ntohl (msg->chid);
5502 ch = channel_get (t, chid);
5505 GNUNET_break_op (0);
5506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5510 channel_confirm (ch, !fwd);
5516 * Handler for channel destroy messages.
5518 * @param t Tunnel this channel is to be destroyed of.
5519 * @param msg Message.
5520 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5522 * @return channel this message was on.
5524 static struct MeshChannel *
5525 handle_channel_destroy (struct MeshTunnel2 *t,
5526 struct GNUNET_MESH_ChannelManage *msg,
5529 MESH_ChannelNumber chid;
5530 struct MeshChannel *ch;
5532 /* Check message size */
5533 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5535 GNUNET_break_op (0);
5539 /* Check if channel exists */
5540 chid = ntohl (msg->chid);
5541 ch = channel_get (t, chid);
5544 /* Probably a retransmission, safe to ignore */
5548 send_local_channel_destroy (ch, fwd);
5549 channel_destroy (ch);
5556 * Generic handler for mesh network encrypted traffic.
5558 * @param peer Peer identity this notification is about.
5559 * @param message Encrypted message.
5560 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5562 * @return GNUNET_OK to keep the connection open,
5563 * GNUNET_SYSERR to close it (signal serious error)
5566 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5567 const struct GNUNET_MESH_Encrypted *msg,
5570 struct MeshConnection *c;
5571 struct MeshTunnel2 *t;
5572 struct MeshPeer *neighbor;
5573 struct MeshFlowControl *fc;
5580 size = ntohs (msg->header.size);
5582 sizeof (struct GNUNET_MESH_Encrypted) +
5583 sizeof (struct GNUNET_MessageHeader))
5585 GNUNET_break_op (0);
5588 type = ntohs (msg->header.type);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5591 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5593 /* Check connection */
5594 c = connection_get (&msg->cid);
5597 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5602 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5604 /* Check if origin is as expected */
5605 neighbor = connection_get_hop (c, fwd);
5606 if (peer_get (peer)->id != neighbor->id)
5608 GNUNET_break_op (0);
5613 pid = ntohl (msg->pid);
5614 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5616 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5618 "WARNING Received PID %u, (prev %u), ACK %u\n",
5619 pid, fc->last_pid_recv, fc->last_ack_sent);
5622 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5624 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5626 " Pid %u not expected (%u+), dropping!\n",
5627 pid, fc->last_pid_recv + 1);
5630 if (MESH_CONNECTION_SENT == c->state)
5631 connection_change_state (c, MESH_CONNECTION_READY);
5632 connection_reset_timeout (c, fwd);
5633 fc->last_pid_recv = pid;
5635 /* Is this message for us? */
5636 if (connection_is_terminal (c, fwd))
5638 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5640 struct GNUNET_MessageHeader *msgh;
5641 struct MeshChannel *ch;
5643 /* TODO signature verification */
5644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5645 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5647 fc->last_pid_recv = pid;
5648 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5649 msgh = (struct GNUNET_MessageHeader *) cbuf;
5650 switch (ntohs (msgh->type))
5652 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5653 /* Don't send hop ACK, wait for client to ACK */
5654 ch = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5657 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5658 ch = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5661 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5662 ch = handle_channel_create (t,
5663 (struct GNUNET_MESH_ChannelCreate *) msgh,
5667 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5668 ch = handle_channel_ack (t,
5669 (struct GNUNET_MESH_ChannelManage *) msgh,
5673 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5674 ch = handle_channel_destroy (t,
5675 (struct GNUNET_MESH_ChannelManage *) msgh,
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5681 "end-to-end message not known (%u)\n",
5682 ntohs (msgh->type));
5686 send_ack (c, ch, fwd);
5690 /* Message not for us: forward to next hop */
5691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5692 ttl = ntohl (msg->ttl);
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5696 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5698 connection_send_ack (c, connection_get_buffer (c, fwd), fwd);
5701 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5703 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5710 * Core handler for mesh network traffic going orig->dest.
5712 * @param cls Closure (unused).
5713 * @param message Message received.
5714 * @param peer Peer who sent the message.
5716 * @return GNUNET_OK to keep the connection open,
5717 * GNUNET_SYSERR to close it (signal serious error)
5720 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5721 const struct GNUNET_MessageHeader *message)
5723 return handle_mesh_encrypted (peer,
5724 (struct GNUNET_MESH_Encrypted *)message,
5729 * Core handler for mesh network traffic going dest->orig.
5731 * @param cls Closure (unused).
5732 * @param message Message received.
5733 * @param peer Peer who sent the message.
5735 * @return GNUNET_OK to keep the connection open,
5736 * GNUNET_SYSERR to close it (signal serious error)
5739 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5740 const struct GNUNET_MessageHeader *message)
5742 return handle_mesh_encrypted (peer,
5743 (struct GNUNET_MESH_Encrypted *)message,
5749 * Core handler for mesh network traffic point-to-point acks.
5751 * @param cls closure
5752 * @param message message
5753 * @param peer peer identity this notification is about
5755 * @return GNUNET_OK to keep the connection open,
5756 * GNUNET_SYSERR to close it (signal serious error)
5759 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5760 const struct GNUNET_MessageHeader *message)
5762 struct GNUNET_MESH_ACK *msg;
5763 struct MeshConnection *c;
5764 struct MeshFlowControl *fc;
5768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5771 msg = (struct GNUNET_MESH_ACK *) message;
5773 c = connection_get (&msg->cid);
5777 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5782 /* Is this a forward or backward ACK? */
5783 id = GNUNET_PEER_search (peer);
5784 if (connection_get_next_hop (c)->id == id)
5786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5789 else if (connection_get_prev_hop (c)->id == id)
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5796 GNUNET_break_op (0);
5800 ack = ntohl (msg->ack);
5801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5803 /* Cancel polling if the ACK is bigger than before. */
5804 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5805 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5808 GNUNET_SCHEDULER_cancel (fc->poll_task);
5809 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5810 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5813 fc->last_ack_recv = ack;
5814 connection_unlock_queue (c, fc == &c->fwd_fc);
5821 * Core handler for mesh network traffic point-to-point ack polls.
5823 * @param cls closure
5824 * @param message message
5825 * @param peer peer identity this notification is about
5827 * @return GNUNET_OK to keep the connection open,
5828 * GNUNET_SYSERR to close it (signal serious error)
5831 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5832 const struct GNUNET_MessageHeader *message)
5834 struct GNUNET_MESH_Poll *msg;
5835 struct MeshConnection *c;
5836 struct MeshFlowControl *fc;
5841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5845 msg = (struct GNUNET_MESH_Poll *) message;
5847 c = connection_get (&msg->cid);
5851 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5853 GNUNET_break_op (0);
5857 /* Is this a forward or backward ACK?
5858 * Note: a poll should never be needed in a loopback case,
5859 * since there is no possiblility of packet loss there, so
5860 * this way of discerining FWD/BCK should not be a problem.
5862 id = GNUNET_PEER_search (peer);
5863 if (connection_get_next_hop (c)->id == id)
5865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5868 else if (connection_get_prev_hop (c)->id == id)
5870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5875 GNUNET_break_op (0);
5879 pid = ntohl (msg->pid);
5880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5881 pid, fc->last_pid_recv);
5882 fc->last_pid_recv = pid;
5883 fwd = fc == &c->fwd_fc;
5884 connection_send_ack (c, connection_get_buffer(c, fwd), fwd);
5891 * Core handler for mesh keepalives.
5893 * @param cls closure
5894 * @param message message
5895 * @param peer peer identity this notification is about
5896 * @return GNUNET_OK to keep the connection open,
5897 * GNUNET_SYSERR to close it (signal serious error)
5899 * TODO: Check who we got this from, to validate route.
5902 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5903 const struct GNUNET_MessageHeader *message)
5905 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5906 struct MeshConnection *c;
5907 struct MeshPeer *neighbor;
5910 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5914 c = connection_get (&msg->cid);
5917 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5922 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5923 GNUNET_YES : GNUNET_NO;
5925 /* Check if origin is as expected */
5926 neighbor = connection_get_hop (c, fwd);
5927 if (peer_get (peer)->id != neighbor->id)
5929 GNUNET_break_op (0);
5933 connection_change_state (c, MESH_CONNECTION_READY);
5934 connection_reset_timeout (c, fwd);
5936 if (connection_is_terminal (c, fwd))
5939 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5940 send_prebuilt_message_connection (message, c, NULL, fwd);
5948 * Functions to handle messages from core
5950 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5951 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5953 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5954 sizeof (struct GNUNET_MESH_ConnectionACK)},
5955 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5956 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5957 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5958 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5959 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5960 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5961 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5962 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5963 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5964 sizeof (struct GNUNET_MESH_ACK)},
5965 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5966 sizeof (struct GNUNET_MESH_Poll)},
5967 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5968 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5974 * Function to process paths received for a new peer addition. The recorded
5975 * paths form the initial tunnel, which can be optimized later.
5976 * Called on each result obtained for the DHT search.
5978 * @param cls closure
5979 * @param exp when will this value expire
5980 * @param key key of the result
5981 * @param get_path path of the get request
5982 * @param get_path_length lenght of get_path
5983 * @param put_path path of the put request
5984 * @param put_path_length length of the put_path
5985 * @param type type of the result
5986 * @param size number of bytes in data
5987 * @param data pointer to the result data
5990 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5991 const struct GNUNET_HashCode * key,
5992 const struct GNUNET_PeerIdentity *get_path,
5993 unsigned int get_path_length,
5994 const struct GNUNET_PeerIdentity *put_path,
5995 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5996 size_t size, const void *data)
5998 struct MeshPeer *peer = cls;
5999 struct MeshPeerPath *p;
6000 struct MeshConnection *c;
6001 struct GNUNET_PeerIdentity pi;
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6005 GNUNET_PEER_resolve (peer->id, &pi);
6006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6008 p = path_build_from_dht (get_path, get_path_length,
6009 put_path, put_path_length);
6010 path_add_to_peers (p, GNUNET_NO);
6013 /* Count connections */
6014 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
6016 /* If we already have 3 (or more (?!)) connections, it's enough */
6020 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
6023 peer_connect (peer);
6029 /******************************************************************************/
6030 /********************* MESH LOCAL HANDLES **************************/
6031 /******************************************************************************/
6035 * Handler for client connection.
6037 * @param cls Closure (unused).
6038 * @param client Client handler.
6041 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
6043 struct MeshClient *c;
6047 c = GNUNET_malloc (sizeof (struct MeshClient));
6049 c->id = next_client_id++; /* overflow not important: just for debug */
6050 GNUNET_SERVER_client_keep (client);
6051 GNUNET_SERVER_client_set_user_context (client, c);
6052 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6057 * Handler for client disconnection
6059 * @param cls closure
6060 * @param client identification of the client; NULL
6061 * for the last call when the server is destroyed
6064 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6066 struct MeshClient *c;
6068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6075 c = client_get (client);
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6080 GNUNET_SERVER_client_drop (c->handle);
6081 c->shutting_down = GNUNET_YES;
6082 if (NULL != c->own_channels)
6084 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6085 &channel_destroy_iterator, c);
6086 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6089 if (NULL != c->incoming_channels)
6091 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6092 &channel_destroy_iterator, c);
6093 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6096 if (NULL != c->ports)
6098 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6099 &client_release_ports, c);
6100 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6102 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6103 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6117 * Handler for new clients
6119 * @param cls closure
6120 * @param client identification of the client
6121 * @param message the actual message, which includes messages the client wants
6124 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6125 const struct GNUNET_MessageHeader *message)
6127 struct GNUNET_MESH_ClientConnect *cc_msg;
6128 struct MeshClient *c;
6133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6135 /* Check data sanity */
6136 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6137 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6138 if (0 != (size % sizeof (uint32_t)))
6141 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6144 size /= sizeof (uint32_t);
6146 /* Initialize new client structure */
6147 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6154 p = (uint32_t *) &cc_msg[1];
6155 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6156 for (i = 0; i < size; i++)
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6161 /* store in client's hashmap */
6162 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6164 /* store in global hashmap */
6165 /* FIXME only allow one client to have the port open,
6166 * have a backup hashmap with waiting clients */
6167 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6172 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6173 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6174 GNUNET_SERVER_notification_context_add (nc, client);
6175 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6177 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6183 * Handler for requests of new tunnels
6185 * @param cls Closure.
6186 * @param client Identification of the client.
6187 * @param message The actual message.
6190 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6191 const struct GNUNET_MessageHeader *message)
6193 struct GNUNET_MESH_ChannelMessage *msg;
6194 struct MeshPeer *peer;
6195 struct MeshTunnel2 *t;
6196 struct MeshChannel *ch;
6197 struct MeshClient *c;
6198 MESH_ChannelNumber chid;
6200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6202 /* Sanity check for client registration */
6203 if (NULL == (c = client_get (client)))
6206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6211 /* Message size sanity check */
6212 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6219 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6221 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6222 chid = ntohl (msg->channel_id);
6224 /* Sanity check for duplicate channel IDs */
6225 if (NULL != channel_get_by_local_id (c, chid))
6228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6232 peer = peer_get (&msg->peer);
6233 if (NULL == peer->tunnel)
6235 peer->tunnel = tunnel_new ();
6236 peer->tunnel->peer = peer;
6240 /* Create channel */
6241 ch = channel_new (t, c, chid);
6245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6248 ch->port = ntohl (msg->port);
6249 channel_set_options (ch, ntohl (msg->opt));
6251 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6252 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6253 ch->root_rel->ch = ch;
6254 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6257 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6258 peer_connect (peer);
6260 /* Send create channel */
6262 struct GNUNET_MESH_ChannelCreate msgcc;
6264 msgcc.header.size = htons (sizeof (msgcc));
6265 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6266 msgcc.chid = htonl (ch->gid);
6267 msgcc.port = msg->port;
6268 msgcc.opt = msg->opt;
6270 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6273 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6279 * Handler for requests of deleting tunnels
6281 * @param cls closure
6282 * @param client identification of the client
6283 * @param message the actual message
6286 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6287 const struct GNUNET_MessageHeader *message)
6289 struct GNUNET_MESH_ChannelMessage *msg;
6290 struct MeshClient *c;
6291 struct MeshChannel *ch;
6292 struct MeshTunnel2 *t;
6293 MESH_ChannelNumber chid;
6295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6296 "Got a DESTROY CHANNEL from client!\n");
6298 /* Sanity check for client registration */
6299 if (NULL == (c = client_get (client)))
6302 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6307 /* Message sanity check */
6308 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6315 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6317 /* Retrieve tunnel */
6318 chid = ntohl (msg->channel_id);
6319 ch = channel_get_by_local_id (c, chid);
6322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6328 /* Cleanup after the tunnel */
6329 client_delete_channel (c, ch);
6330 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6334 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6341 " channel %X client %p (%p, %p)\n",
6342 chid, c, ch->root, ch->dest);
6347 channel_destroy (ch);
6348 tunnel_destroy_if_empty (t);
6350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6356 * Handler for client traffic
6358 * @param cls closure
6359 * @param client identification of the client
6360 * @param message the actual message
6363 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6364 const struct GNUNET_MessageHeader *message)
6366 struct GNUNET_MESH_LocalData *msg;
6367 struct MeshClient *c;
6368 struct MeshChannel *ch;
6369 struct MeshChannelReliability *rel;
6370 MESH_ChannelNumber chid;
6374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6375 "Got data from a client!\n");
6377 /* Sanity check for client registration */
6378 if (NULL == (c = client_get (client)))
6381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6386 msg = (struct GNUNET_MESH_LocalData *) message;
6388 /* Sanity check for message size */
6389 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6390 if (size < sizeof (struct GNUNET_MessageHeader))
6393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6397 /* Channel exists? */
6398 chid = ntohl (msg->id);
6399 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6400 ch = channel_get_by_local_id (c, chid);
6404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6408 /* Is the client in the channel? */
6411 ch->root->handle == client)
6415 ch->dest->handle == client) ) )
6418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6422 rel = fwd ? ch->root_rel : ch->dest_rel;
6423 rel->client_ready = GNUNET_NO;
6425 /* Ok, everything is correct, send the message. */
6427 struct GNUNET_MESH_Data *payload;
6428 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6429 unsigned char cbuf[p2p_size];
6431 payload = (struct GNUNET_MESH_Data *) cbuf;
6432 payload->mid = htonl (rel->mid_send);
6434 memcpy (&payload[1], &msg[1], size);
6435 payload->header.size = htons (p2p_size);
6436 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6437 payload->chid = htonl (ch->gid);
6438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6439 send_prebuilt_message_channel (&payload->header, ch, fwd);
6441 if (GNUNET_YES == ch->reliable)
6442 channel_save_copy (ch, &payload->header, fwd);
6444 if (tunnel_get_buffer (ch->t, fwd) > 0)
6445 send_local_ack (ch, fwd);
6446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6454 * Handler for client's ACKs for payload traffic.
6456 * @param cls Closure (unused).
6457 * @param client Identification of the client.
6458 * @param message The actual message.
6461 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6462 const struct GNUNET_MessageHeader *message)
6464 struct GNUNET_MESH_LocalAck *msg;
6465 struct MeshChannelReliability *rel;
6466 struct MeshChannel *ch;
6467 struct MeshClient *c;
6468 MESH_ChannelNumber chid;
6471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6473 /* Sanity check for client registration */
6474 if (NULL == (c = client_get (client)))
6477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6482 msg = (struct GNUNET_MESH_LocalAck *) message;
6484 /* Channel exists? */
6485 chid = ntohl (msg->channel_id);
6486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6487 ch = channel_get_by_local_id (c, chid);
6488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6498 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6499 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6500 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6501 rel = fwd ? ch->dest_rel : ch->root_rel;
6503 rel->client_ready = GNUNET_YES;
6504 channel_send_client_buffered_data (ch, c, fwd);
6505 send_ack (NULL, ch, fwd);
6507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6514 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6516 * @param cls Closure (client handle).
6517 * @param key Key (hashed tunnel ID, unused).
6518 * @param value Tunnel info.
6520 * @return GNUNET_YES, to keep iterating.
6523 // monitor_all_tunnels_iterator (void *cls,
6524 // const struct GNUNET_HashCode * key,
6527 // struct GNUNET_SERVER_Client *client = cls;
6528 // struct MeshChannel *ch = value;
6529 // struct GNUNET_MESH_LocalMonitor *msg;
6531 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6532 // msg->channel_id = htonl (ch->gid);
6533 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6534 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6536 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6537 // "* sending info about tunnel %s\n",
6538 // GNUNET_i2s (&msg->owner));
6540 // GNUNET_SERVER_notification_context_unicast (nc, client,
6541 // &msg->header, GNUNET_NO);
6542 // return GNUNET_YES;
6547 * Handler for client's MONITOR request.
6549 * @param cls Closure (unused).
6550 * @param client Identification of the client.
6551 * @param message The actual message.
6554 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6555 const struct GNUNET_MessageHeader *message)
6557 struct MeshClient *c;
6559 /* Sanity check for client registration */
6560 if (NULL == (c = client_get (client)))
6563 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6567 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6568 "Received get tunnels request from client %u\n",
6570 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6571 // monitor_all_tunnels_iterator,
6573 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6574 "Get tunnels request from client %u completed\n",
6576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6581 * Handler for client's MONITOR_TUNNEL request.
6583 * @param cls Closure (unused).
6584 * @param client Identification of the client.
6585 * @param message The actual message.
6588 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6589 const struct GNUNET_MessageHeader *message)
6591 const struct GNUNET_MESH_LocalMonitor *msg;
6592 struct GNUNET_MESH_LocalMonitor *resp;
6593 struct MeshClient *c;
6594 struct MeshChannel *ch;
6596 /* Sanity check for client registration */
6597 if (NULL == (c = client_get (client)))
6600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6604 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6605 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6606 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6609 ntohl (msg->channel_id));
6610 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6614 /* We don't know the tunnel */
6615 struct GNUNET_MESH_LocalMonitor warn;
6618 GNUNET_SERVER_notification_context_unicast (nc, client,
6621 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6625 /* Initialize context */
6626 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6628 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6629 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6630 &resp->header, GNUNET_NO);
6633 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6634 "Monitor tunnel request from client %u completed\n",
6636 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6641 * Functions to handle messages from clients
6643 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6644 {&handle_local_new_client, NULL,
6645 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6646 {&handle_local_channel_create, NULL,
6647 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6648 sizeof (struct GNUNET_MESH_ChannelMessage)},
6649 {&handle_local_channel_destroy, NULL,
6650 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6651 sizeof (struct GNUNET_MESH_ChannelMessage)},
6652 {&handle_local_data, NULL,
6653 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6654 {&handle_local_ack, NULL,
6655 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6656 sizeof (struct GNUNET_MESH_LocalAck)},
6657 {&handle_local_get_tunnels, NULL,
6658 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6659 sizeof (struct GNUNET_MessageHeader)},
6660 {&handle_local_show_tunnel, NULL,
6661 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6662 sizeof (struct GNUNET_MESH_LocalMonitor)},
6668 * Method called whenever a given peer connects.
6670 * @param cls closure
6671 * @param peer peer identity this notification is about
6674 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6676 struct MeshPeer *pi;
6677 struct MeshPeerPath *path;
6679 DEBUG_CONN ("Peer connected\n");
6680 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6681 pi = peer_get (peer);
6684 DEBUG_CONN (" (self)\n");
6685 path = path_new (1);
6689 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6690 path = path_new (2);
6691 path->peers[1] = pi->id;
6692 GNUNET_PEER_change_rc (pi->id, 1);
6693 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6695 path->peers[0] = myid;
6696 GNUNET_PEER_change_rc (myid, 1);
6697 peer_add_path (pi, path, GNUNET_YES);
6699 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6705 * Method called whenever a peer disconnects.
6707 * @param cls closure
6708 * @param peer peer identity this notification is about
6711 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6713 struct MeshPeer *pi;
6715 DEBUG_CONN ("Peer disconnected\n");
6716 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6723 peer_remove_path (pi, myid, pi->id);
6725 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6728 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6729 pi->connections = NULL;
6732 DEBUG_CONN (" (self)\n");
6734 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6741 * Install server (service) handlers and start listening to clients.
6746 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6747 GNUNET_SERVER_connect_notify (server_handle,
6748 &handle_local_client_connect, NULL);
6749 GNUNET_SERVER_disconnect_notify (server_handle,
6750 &handle_local_client_disconnect, NULL);
6751 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6753 clients_head = NULL;
6754 clients_tail = NULL;
6756 GNUNET_SERVER_resume (server_handle);
6761 * To be called on core init/fail.
6763 * @param cls Closure (config)
6764 * @param identity the public identity of this peer
6767 core_init (void *cls,
6768 const struct GNUNET_PeerIdentity *identity)
6770 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6774 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6779 GNUNET_i2s (identity));
6780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6782 GNUNET_i2s (&my_full_id));
6783 GNUNET_CORE_disconnect (core_handle);
6784 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6785 NULL, /* Closure passed to MESH functions */
6786 &core_init, /* Call core_init once connected */
6787 &core_connect, /* Handle connects */
6788 &core_disconnect, /* remove peers on disconnects */
6789 NULL, /* Don't notify about all incoming messages */
6790 GNUNET_NO, /* For header only in notification */
6791 NULL, /* Don't notify about all outbound messages */
6792 GNUNET_NO, /* For header-only out notification */
6793 core_handlers); /* Register these handlers */
6802 /******************************************************************************/
6803 /************************ MAIN FUNCTIONS ****************************/
6804 /******************************************************************************/
6807 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6809 * @param cls closure
6810 * @param key current key code
6811 * @param value value in the hash map
6812 * @return GNUNET_YES if we should continue to iterate,
6816 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6818 struct MeshPeer *p = value;
6819 struct MeshTunnel2 *t = p->tunnel;
6828 * Task run during shutdown.
6834 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6838 if (core_handle != NULL)
6840 GNUNET_CORE_disconnect (core_handle);
6843 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6844 if (dht_handle != NULL)
6846 GNUNET_DHT_disconnect (dht_handle);
6851 GNUNET_SERVER_notification_context_destroy (nc);
6854 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6856 GNUNET_SCHEDULER_cancel (announce_id_task);
6857 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6864 * Process mesh requests.
6866 * @param cls closure
6867 * @param server the initialized server
6868 * @param c configuration to use
6871 run (void *cls, struct GNUNET_SERVER_Handle *server,
6872 const struct GNUNET_CONFIGURATION_Handle *c)
6875 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6878 server_handle = server;
6879 GNUNET_SERVER_suspend (server_handle);
6882 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6885 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6887 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6888 "mesh", "peer/privatekey");
6889 GNUNET_SCHEDULER_shutdown ();
6894 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6895 &refresh_connection_time))
6897 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6899 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6900 "mesh", "refresh path time");
6901 GNUNET_SCHEDULER_shutdown ();
6906 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6909 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6911 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6912 "mesh", "id announce time");
6913 GNUNET_SCHEDULER_shutdown ();
6918 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6923 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6924 "mesh", "connect timeout");
6925 GNUNET_SCHEDULER_shutdown ();
6930 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6935 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6936 "mesh", "max msgs queue");
6937 GNUNET_SCHEDULER_shutdown ();
6942 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6947 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6948 "mesh", "max tunnels");
6949 GNUNET_SCHEDULER_shutdown ();
6954 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6959 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6960 "mesh", "default ttl", 64);
6965 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6968 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6969 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6970 "mesh", "max peers", 1000);
6975 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6983 "Mesh is running with drop mode enabled. "
6984 "This is NOT a good idea! "
6985 "Remove the DROP_PERCENT option from your configuration.\n");
6989 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6990 &dht_replication_level))
6992 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6994 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6995 "mesh", "dht replication level", 3);
6996 dht_replication_level = 3;
6999 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7000 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7001 ports = GNUNET_CONTAINER_multihashmap32_create (32);
7003 dht_handle = GNUNET_DHT_connect (c, 64);
7004 if (NULL == dht_handle)
7008 stats = GNUNET_STATISTICS_create ("mesh", c);
7010 /* Scheduled the task to clean up when shutdown is called */
7011 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7013 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
7014 GNUNET_free (keyfile);
7015 GNUNET_assert (NULL != pk);
7016 my_private_key = pk;
7017 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7018 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7019 &my_full_id.hashPubKey);
7020 myid = GNUNET_PEER_intern (&my_full_id);
7021 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7022 "Mesh for peer [%s] starting\n",
7023 GNUNET_i2s(&my_full_id));
7025 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7026 NULL, /* Closure passed to MESH functions */
7027 &core_init, /* Call core_init once connected */
7028 &core_connect, /* Handle connects */
7029 &core_disconnect, /* remove peers on disconnects */
7030 NULL, /* Don't notify about all incoming messages */
7031 GNUNET_NO, /* For header only in notification */
7032 NULL, /* Don't notify about all outbound messages */
7033 GNUNET_NO, /* For header-only out notification */
7034 core_handlers); /* Register these handlers */
7035 if (NULL == core_handle)
7038 GNUNET_SCHEDULER_shutdown ();
7041 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7047 * The main function for the mesh service.
7049 * @param argc number of arguments from the command line
7050 * @param argv command line arguments
7051 * @return 0 ok, 1 on error
7054 main (int argc, char *const *argv)
7059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7060 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7062 ret = (GNUNET_OK == r) ? 0 : 1;
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");