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.
1483 * @param ch Channel on which to send the ACK.
1484 * @param c Client to whom send the ACK.
1485 * @param fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1488 send_local_ack (struct MeshChannel *ch, int fwd)
1490 struct GNUNET_MESH_LocalAck msg;
1491 struct MeshChannelReliability *rel;
1492 struct MeshClient *c;
1494 c = fwd ? ch->root : ch->dest;
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 "send local %s ack on %s:%X towards %p\n",
1497 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1500 || ( fwd && (0 == ch->lid_root || c != ch->root))
1501 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1506 msg.header.size = htons (sizeof (msg));
1507 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1508 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1509 GNUNET_SERVER_notification_context_unicast (nc,
1513 rel = fwd ? ch->root_rel : ch->dest_rel;
1514 rel->client_ready = GNUNET_YES;
1519 * Count established (ready) connections of a tunnel.
1521 * @param t Tunnel on which to send the message.
1523 * @return Number of connections.
1526 tunnel_count_connections (struct MeshTunnel2 *t)
1528 struct MeshConnection *c;
1531 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1538 * Pick a connection on which send the next data message.
1540 * @param t Tunnel on which to send the message.
1541 * @param fwd Is this a fwd message?
1543 * @return The connection on which to send the next message.
1545 static struct MeshConnection *
1546 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1548 struct MeshConnection *c;
1549 struct MeshConnection *best;
1550 struct MeshFlowControl *fc;
1551 unsigned int lowest_q;
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
1556 lowest_q = UINT_MAX;
1557 for (c = t->connection_head; NULL != c; c = c->next)
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s, \n",
1560 GNUNET_h2s (&c->id));
1561 if (MESH_CONNECTION_READY == c->state)
1563 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
1570 if (fc->queue_n < lowest_q)
1573 lowest_q = fc->queue_n;
1582 * Get the total buffer space for a tunnel.
1585 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1587 struct MeshConnection *c;
1588 struct MeshFlowControl *fc;
1589 unsigned int buffer;
1591 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1593 if (c->state != MESH_CONNECTION_READY)
1596 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1597 buffer += fc->last_ack_recv - fc->last_pid_sent;
1605 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1606 * Encrypt data with the tunnel key.
1608 * @param t Tunnel whose key to use.
1609 * @param dst Destination for the encrypted data.
1610 * @param src Source of the plaintext.
1611 * @param size Size of the plaintext.
1612 * @param iv Initialization Vector to use.
1613 * @param fwd Is this a fwd message?
1616 tunnel_encrypt (struct MeshTunnel2 *t,
1617 void *dst, const void *src,
1618 size_t size, uint64_t iv, int fwd)
1620 memcpy (dst, src, size);
1625 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1626 * Decrypt data with the tunnel key.
1628 * @param t Tunnel whose key to use.
1629 * @param dst Destination for the plaintext.
1630 * @param src Source of the encrypted data.
1631 * @param size Size of the encrypted data.
1632 * @param iv Initialization Vector to use.
1633 * @param fwd Is this a fwd message?
1636 tunnel_decrypt (struct MeshTunnel2 *t,
1637 void *dst, const void *src,
1638 size_t size, uint64_t iv, int fwd)
1640 memcpy (dst, src, size);
1645 * Sends an already built message on a connection, properly registering
1646 * all used resources.
1648 * @param message Message to send. Function makes a copy of it.
1649 * If message is not hop-by-hop, decrements TTL of copy.
1650 * @param c Connection on which this message is transmitted.
1651 * @param ch Channel on which this message is transmitted, or NULL.
1652 * @param fwd Is this a fwd message?
1655 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1656 struct MeshConnection *c,
1657 struct MeshChannel *ch,
1664 size = ntohs (message->size);
1665 data = GNUNET_malloc (size);
1666 memcpy (data, message, size);
1667 type = ntohs (message->type);
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1669 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1673 struct GNUNET_MESH_Encrypted *emsg;
1674 struct GNUNET_MESH_ACK *amsg;
1675 struct GNUNET_MESH_Poll *pmsg;
1678 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1679 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1680 emsg = (struct GNUNET_MESH_Encrypted *) data;
1681 ttl = ntohl (emsg->ttl);
1684 GNUNET_break_op (0);
1688 emsg->ttl = htonl (ttl - 1);
1689 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1693 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1694 amsg = (struct GNUNET_MESH_ACK *) data;
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1699 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1700 pmsg = (struct GNUNET_MESH_Poll *) data;
1702 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1720 * Sends an already built message on a tunnel, choosing the best connection.
1722 * @param message Message to send. Function modifies it.
1723 * @param t Tunnel on which this message is transmitted.
1724 * @param ch Channel on which this message is transmitted.
1725 * @param fwd Is this a fwd message?
1728 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1729 struct MeshTunnel2 *t,
1730 struct MeshChannel *ch,
1733 struct MeshConnection *c;
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1738 c = tunnel_get_connection (t, fwd);
1741 GNUNET_break (GNUNET_YES == t->destroy);
1744 type = ntohs (msg->header.type);
1747 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1748 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1749 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1750 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1752 msg->ttl = htonl (default_ttl);
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1756 GNUNET_MESH_DEBUG_M2S (type));
1761 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1766 * Sends an already built message on a channel, properly registering
1767 * all used resources and encrypting the message with the tunnel's key.
1769 * @param message Message to send. Function makes a copy of it.
1770 * @param ch Channel on which this message is transmitted.
1771 * @param fwd Is this a fwd message?
1774 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1775 struct MeshChannel *ch,
1778 struct GNUNET_MESH_Encrypted *msg;
1779 size_t size = ntohs (message->size);
1780 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1785 peer2s (ch->t->peer), ch->gid);
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1787 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1788 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1789 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1791 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1792 msg->header.type = htons (type);
1793 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1794 msg->iv = GNUNET_htonll (iv);
1795 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1797 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1802 * Sends a CREATE CONNECTION message for a path to a peer.
1803 * Changes the connection and tunnel states if necessary.
1805 * @param connection Connection to create.
1808 send_connection_create (struct MeshConnection *connection)
1810 struct MeshTunnel2 *t;
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1815 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1816 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1817 (connection->path->length *
1818 sizeof (struct GNUNET_PeerIdentity)),
1823 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1824 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1825 if (MESH_CONNECTION_NEW == connection->state)
1826 connection_change_state (connection, MESH_CONNECTION_SENT);
1831 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1834 * @param connection Connection to confirm.
1835 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1838 send_connection_ack (struct MeshConnection *connection, int fwd)
1840 struct MeshTunnel2 *t;
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1845 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1846 sizeof (struct GNUNET_MESH_ConnectionACK),
1850 if (MESH_TUNNEL_NEW == t->state)
1851 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1856 * Core callback to write a pre-constructed data packet to core buffer
1858 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1859 * @param size Number of bytes available in buf.
1860 * @param buf Where the to write the message.
1862 * @return number of bytes written to buf
1865 send_core_data_raw (void *cls, size_t size, void *buf)
1867 struct GNUNET_MessageHeader *msg = cls;
1870 GNUNET_assert (NULL != msg);
1871 total_size = ntohs (msg->size);
1873 if (total_size > size)
1878 memcpy (buf, msg, total_size);
1885 * Function to send a create connection message to a peer.
1887 * @param c Connection to create.
1888 * @param size number of bytes available in buf
1889 * @param buf where the callee should write the message
1890 * @return number of bytes written to buf
1893 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1895 struct GNUNET_MESH_ConnectionCreate *msg;
1896 struct GNUNET_PeerIdentity *peer_ptr;
1897 struct MeshPeerPath *p = c->path;
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1903 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1904 p->length * sizeof (struct GNUNET_PeerIdentity);
1906 if (size < size_needed || NULL == buf)
1911 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1912 msg->header.size = htons (size_needed);
1913 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1916 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1917 for (i = 0; i < p->length; i++)
1919 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1929 * Creates a path ack message in buf and frees all unused resources.
1931 * @param c Connection to send an ACK on.
1932 * @param size number of bytes available in buf
1933 * @param buf where the callee should write the message
1935 * @return number of bytes written to buf
1938 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1940 struct GNUNET_MESH_ConnectionACK *msg = buf;
1941 struct MeshTunnel2 *t = c->t;
1943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1944 GNUNET_assert (NULL != t);
1945 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1950 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1951 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1954 /* TODO add signature */
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1957 return sizeof (struct GNUNET_MESH_ConnectionACK);
1962 * Destroy the peer_info and free any allocated resources linked to it
1964 * @param peer The peer_info to destroy.
1966 * @return GNUNET_OK on success
1969 peer_destroy (struct MeshPeer *peer)
1971 struct GNUNET_PeerIdentity id;
1972 struct MeshPeerPath *p;
1973 struct MeshPeerPath *nextp;
1975 GNUNET_PEER_resolve (peer->id, &id);
1976 GNUNET_PEER_change_rc (peer->id, -1);
1979 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
1982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1983 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1985 if (NULL != peer->dhtget)
1987 GNUNET_DHT_get_stop (peer->dhtget);
1989 p = peer->path_head;
1993 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1997 tunnel_destroy_empty (peer->tunnel);
2004 * Returns if peer is used (has a tunnel, is neighbor).
2006 * @peer Peer to check.
2008 * @return GNUNET_YES if peer is in use.
2011 peer_is_used (struct MeshPeer *peer)
2013 struct MeshPeerPath *p;
2015 if (NULL != peer->tunnel)
2018 for (p = peer->path_head; NULL != p; p = p->next)
2027 * Iterator over all the peers to get the oldest timestamp.
2029 * @param cls Closure (unsued).
2030 * @param key ID of the peer.
2031 * @param value Peer_Info of the peer.
2034 peer_get_oldest (void *cls,
2035 const struct GNUNET_HashCode *key,
2038 struct MeshPeer *p = value;
2039 struct GNUNET_TIME_Absolute *abs = cls;
2041 /* Don't count active peers */
2042 if (GNUNET_YES == peer_is_used (p))
2045 if (abs->abs_value_us < p->last_contact.abs_value_us)
2046 abs->abs_value_us = p->last_contact.abs_value_us;
2053 * Iterator over all the peers to remove the oldest entry.
2055 * @param cls Closure (unsued).
2056 * @param key ID of the peer.
2057 * @param value Peer_Info of the peer.
2060 peer_timeout (void *cls,
2061 const struct GNUNET_HashCode *key,
2064 struct MeshPeer *p = value;
2065 struct GNUNET_TIME_Absolute *abs = cls;
2067 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2068 GNUNET_NO == peer_is_used (p))
2078 * Delete oldest unused peer.
2081 peer_delete_oldest (void)
2083 struct GNUNET_TIME_Absolute abs;
2085 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2087 GNUNET_CONTAINER_multihashmap_iterate (peers,
2090 GNUNET_CONTAINER_multihashmap_iterate (peers,
2097 * Retrieve the MeshPeer stucture associated with the peer, create one
2098 * and insert it in the appropriate structures if the peer is not known yet.
2100 * @param peer Full identity of the peer.
2102 * @return Existing or newly created peer info.
2104 static struct MeshPeer *
2105 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2107 struct MeshPeer *peer;
2109 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2112 peer = GNUNET_new (struct MeshPeer);
2113 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2115 peer_delete_oldest ();
2117 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2119 peer->id = GNUNET_PEER_intern (peer_id);
2121 peer->last_contact = GNUNET_TIME_absolute_get();
2128 * Retrieve the MeshPeer stucture associated with the peer, create one
2129 * and insert it in the appropriate structures if the peer is not known yet.
2131 * @param peer Short identity of the peer.
2133 * @return Existing or newly created peer info.
2135 static struct MeshPeer *
2136 peer_get_short (const GNUNET_PEER_Id peer)
2138 return peer_get (GNUNET_PEER_resolve2 (peer));
2143 * Get a cost of a path for a peer considering existing tunnel connections.
2145 * @param peer Peer towards which the path is considered.
2146 * @param path Candidate path.
2148 * @return Cost of the path (path length + number of overlapping nodes)
2151 peer_get_path_cost (const struct MeshPeer *peer,
2152 const struct MeshPeerPath *path)
2154 struct MeshConnection *c;
2155 unsigned int overlap;
2163 GNUNET_assert (NULL != peer->tunnel);
2165 for (i = 0; i < path->length; i++)
2167 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2169 for (j = 0; j < c->path->length; j++)
2171 if (path->peers[i] == c->path->peers[j])
2179 return (path->length + overlap) * (path->score * -1);
2184 * Choose the best path towards a peer considering the tunnel properties.
2186 * @param peer The destination peer.
2188 * @return Best current known path towards the peer, if any.
2190 static struct MeshPeerPath *
2191 peer_get_best_path (const struct MeshPeer *peer)
2193 struct MeshPeerPath *best_p;
2194 struct MeshPeerPath *p;
2195 struct MeshConnection *c;
2196 unsigned int best_cost;
2199 best_cost = UINT_MAX;
2201 for (p = peer->path_head; NULL != p; p = p->next)
2203 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2207 continue; /* If path is in use in a connection, skip it. */
2209 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2219 queue_is_sendable (struct MeshPeerQueue *q)
2221 struct MeshFlowControl *fc;
2223 /* Is PID-independent? */
2226 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2227 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2231 /* Is PID allowed? */
2232 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2233 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2241 * Get first sendable message.
2243 * @param peer The destination peer.
2245 * @return Best current known path towards the peer, if any.
2247 static struct MeshPeerQueue *
2248 peer_get_first_message (const struct MeshPeer *peer)
2250 struct MeshPeerQueue *q;
2252 for (q = peer->queue_head; NULL != q; q = q->next)
2254 if (queue_is_sendable (q))
2263 * Try to establish a new connection to this peer in the given tunnel.
2264 * If the peer doesn't have any path to it yet, try to get one.
2265 * If the peer already has some path, send a CREATE CONNECTION towards it.
2267 * @param peer PeerInfo of the peer.
2270 peer_connect (struct MeshPeer *peer)
2272 struct MeshTunnel2 *t;
2273 struct MeshPeerPath *p;
2274 struct MeshConnection *c;
2276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277 "peer_connect towards %s\n",
2280 if (NULL != peer->path_head)
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2283 p = peer_get_best_path (peer);
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2287 c = tunnel_use_path (t, p);
2288 send_connection_create (c);
2291 else if (NULL == peer->dhtget)
2293 const struct GNUNET_PeerIdentity *id;
2295 id = GNUNET_PEER_resolve2 (peer->id);
2296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2297 " Starting DHT GET for peer %s\n", peer2s (peer));
2298 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2299 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2300 &id->hashPubKey, /* key to search */
2301 dht_replication_level, /* replication level */
2302 GNUNET_DHT_RO_RECORD_ROUTE |
2303 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2305 0, /* xquery bits */
2306 &dht_get_id_handler, peer);
2307 if (MESH_TUNNEL_NEW == t->state)
2308 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 "There is no path but the DHT GET is already started.\n");
2319 * Get the first transmittable message for a connection.
2321 * @param c Connection.
2322 * @param fwd Is this FWD?
2324 * @return First transmittable message.
2326 static struct MeshPeerQueue *
2327 connection_get_first_message (struct MeshConnection *c, int fwd)
2329 struct MeshPeerQueue *q;
2332 p = connection_get_hop (c, fwd);
2334 for (q = p->queue_head; NULL != q; q = q->next)
2338 if (queue_is_sendable (q))
2347 * Is this peer the first one on the connection?
2349 * @param c Connection.
2350 * @param fwd Is this about fwd traffic?
2352 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2355 connection_is_origin (struct MeshConnection *c, int fwd)
2357 if (!fwd && c->own_pos == c->path->length - 1)
2359 if (fwd && c->own_pos == 0)
2366 * Is this peer the last one on the connection?
2368 * @param c Connection.
2369 * @param fwd Is this about fwd traffic?
2370 * Note that the ROOT is the terminal for BCK traffic!
2372 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2375 connection_is_terminal (struct MeshConnection *c, int fwd)
2377 if (fwd && c->own_pos == c->path->length - 1)
2379 if (!fwd && c->own_pos == 0)
2386 * @brief Re-initiate traffic on this connection if necessary.
2388 * Check if there is traffic queued towards this peer
2389 * and the core transmit handle is NULL (traffic was stalled).
2390 * If so, call core tmt rdy.
2392 * @param c Connection on which initiate traffic.
2393 * @param fwd Is this about fwd traffic?
2396 connection_unlock_queue (struct MeshConnection *c, int fwd)
2398 struct MeshPeer *peer;
2399 struct MeshPeerQueue *q;
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 "connection_unlock_queue %s on %s\n",
2404 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2406 if (connection_is_origin (c, fwd))
2408 struct MeshTunnel2 *t = c->t;
2409 struct MeshChannel *ch;
2410 struct MeshChannelReliability *rel;
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2413 /* FIXME randomize channel selection, not always first channel */
2414 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2416 rel = fwd ? ch->root_rel : ch->dest_rel;
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2419 ch->gid, rel->client_ready ? "ready " : "not ready");
2420 if (GNUNET_NO == rel->client_ready)
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending local ack!\n");
2423 send_local_ack (ch, fwd);
2424 return; /* FIXME authorize all channels? */
2430 peer = connection_get_hop (c, fwd);
2432 if (NULL != peer->core_transmit)
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2435 return; /* Already unlocked */
2438 q = connection_get_first_message (c, fwd);
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2442 return; /* Nothing to transmit */
2446 peer->core_transmit =
2447 GNUNET_CORE_notify_transmit_ready (core_handle,
2450 GNUNET_TIME_UNIT_FOREVER_REL,
2451 GNUNET_PEER_resolve2 (peer->id),
2459 * Cancel all transmissions that belong to a certain connection.
2461 * @param c Connection which to cancel.
2462 * @param fwd Cancel fwd traffic?
2465 connection_cancel_queues (struct MeshConnection *c, int fwd)
2467 struct MeshPeerQueue *q;
2468 struct MeshPeerQueue *next;
2469 struct MeshFlowControl *fc;
2470 struct MeshPeer *peer;
2477 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2478 peer = connection_get_hop (c, fwd);
2480 for (q = peer->queue_head; NULL != q; q = next)
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 "connection_cancel_queue %s\n",
2487 GNUNET_MESH_DEBUG_M2S (q->type));
2488 queue_destroy (q, GNUNET_YES);
2491 if (NULL == peer->queue_head)
2493 if (NULL != peer->core_transmit)
2495 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2496 peer->core_transmit = NULL;
2498 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2500 GNUNET_SCHEDULER_cancel (fc->poll_task);
2501 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2508 * Remove all paths that rely on a direct connection between p1 and p2
2509 * from the peer itself and notify all tunnels about it.
2511 * @param peer PeerInfo of affected peer.
2512 * @param p1 GNUNET_PEER_Id of one peer.
2513 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2516 * TODO: optimize (see below)
2519 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2522 struct MeshPeerPath *p;
2523 struct MeshPeerPath *next;
2524 struct MeshPeer *peer_d;
2526 unsigned int destroyed;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2531 for (p = peer->path_head; NULL != p; p = next)
2534 for (i = 0; i < (p->length - 1); i++)
2536 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2537 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2539 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2550 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2552 peer_d = peer_get_short (d); // FIXME
2553 next = peer_get_best_path (peer_d);
2554 tunnel_use_path (peer->tunnel, next);
2555 peer_connect (peer_d);
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2562 * Add the path to the peer and update the path used to reach it in case this
2565 * @param peer_info Destination peer to add the path to.
2566 * @param path New path to add. Last peer must be the peer in arg 1.
2567 * Path will be either used of freed if already known.
2568 * @param trusted Do we trust that this path is real?
2571 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2574 struct MeshPeerPath *aux;
2578 if ((NULL == peer_info) || (NULL == path))
2581 path_destroy (path);
2584 if (path->peers[path->length - 1] != peer_info->id)
2587 path_destroy (path);
2590 if (2 >= path->length && GNUNET_NO == trusted)
2592 /* Only allow CORE to tell us about direct paths */
2593 path_destroy (path);
2596 for (l = 1; l < path->length; l++)
2598 if (path->peers[l] == myid)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2601 for (l2 = 0; l2 < path->length - l; l2++)
2603 path->peers[l2] = path->peers[l + l2];
2608 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2613 path->length, peer2s (peer_info));
2615 l = path_get_length (path);
2618 path_destroy (path);
2622 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2623 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2625 l2 = path_get_length (aux);
2628 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2629 peer_info->path_tail, aux, path);
2634 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2636 path_destroy (path);
2641 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2648 * Add the path to the origin peer and update the path used to reach it in case
2649 * this is the shortest.
2650 * The path is given in peer_info -> destination, therefore we turn the path
2651 * upside down first.
2653 * @param peer_info Peer to add the path to, being the origin of the path.
2654 * @param path New path to add after being inversed.
2655 * Path will be either used or freed.
2656 * @param trusted Do we trust that this path is real?
2659 peer_add_path_to_origin (struct MeshPeer *peer_info,
2660 struct MeshPeerPath *path, int trusted)
2663 peer_add_path (peer_info, path, trusted);
2669 * Function called if a connection has been stalled for a while,
2670 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2672 * @param cls Closure (poll ctx).
2673 * @param tc TaskContext.
2676 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2678 struct MeshFlowControl *fc = cls;
2679 struct GNUNET_MESH_Poll msg;
2680 struct MeshConnection *c;
2682 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2683 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2691 peer2s (c->t->peer), c->id);
2693 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2694 msg.header.size = htons (sizeof (msg));
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2696 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2697 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2698 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2699 &connection_poll, fc);
2704 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2705 * to obtain a local peer -> destination path and interning the peer ids.
2707 * @return Newly allocated and created path
2709 static struct MeshPeerPath *
2710 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2711 unsigned int get_path_length,
2712 const struct GNUNET_PeerIdentity *put_path,
2713 unsigned int put_path_length)
2715 struct MeshPeerPath *p;
2721 GNUNET_PEER_change_rc (myid, 1);
2722 i = get_path_length;
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2724 for (i--; i >= 0; i--)
2726 id = GNUNET_PEER_intern (&get_path[i]);
2727 if (p->length > 0 && id == p->peers[p->length - 1])
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2730 GNUNET_PEER_change_rc (id, -1);
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2735 GNUNET_i2s (&get_path[i]));
2737 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2738 p->peers[p->length - 1] = id;
2741 i = put_path_length;
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2743 for (i--; i >= 0; i--)
2745 id = GNUNET_PEER_intern (&put_path[i]);
2748 /* PUT path went through us, so discard the path up until now and start
2749 * from here to get a much shorter (and loop-free) path.
2754 if (p->length > 0 && id == p->peers[p->length - 1])
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2757 GNUNET_PEER_change_rc (id, -1);
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2762 GNUNET_i2s (&put_path[i]));
2764 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2765 p->peers[p->length - 1] = id;
2769 if (get_path_length > 0)
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2771 GNUNET_i2s (&get_path[0]));
2772 if (put_path_length > 0)
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2774 GNUNET_i2s (&put_path[0]));
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2777 for (i = 0; i < p->length; i++)
2779 struct GNUNET_PeerIdentity peer_id;
2781 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2783 GNUNET_i2s (&peer_id));
2791 * Adds a path to the peer_infos of all the peers in the path
2793 * @param p Path to process.
2794 * @param confirmed Whether we know if the path works or not.
2797 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2801 /* TODO: invert and add */
2802 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2803 for (i++; i < p->length; i++)
2805 struct MeshPeer *aux;
2806 struct MeshPeerPath *copy;
2808 aux = peer_get_short (p->peers[i]);
2809 copy = path_duplicate (p);
2810 copy->length = i + 1;
2811 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2817 * Search for a channel among the channels for a client
2819 * @param c the client whose channels to search in
2820 * @param chid the local id of the channel
2822 * @return channel handler, NULL if doesn't exist
2824 static struct MeshChannel *
2825 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2828 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2830 GNUNET_break_op (0);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2834 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2835 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2836 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2840 * Search for a tunnel by global ID using full PeerIdentities.
2842 * @param t Tunnel containing the channel.
2843 * @param chid Public channel number.
2845 * @return channel handler, NULL if doesn't exist
2847 static struct MeshChannel *
2848 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2850 struct MeshChannel *ch;
2855 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2857 if (ch->gid == chid)
2866 * Change the tunnel state.
2868 * @param t Tunnel whose state to change.
2869 * @param state New state.
2872 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Tunnel %s state was %s\n",
2879 GNUNET_MESH_DEBUG_TS2S (t->state));
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2881 "Tunnel %s state is now %s\n",
2883 GNUNET_MESH_DEBUG_TS2S (state));
2889 * Cache a message to be sent once tunnel is online.
2891 * @param t Tunnel to hold the message.
2892 * @param ch Channel the message is about.
2893 * @param msg Message itself (copy will be made).
2894 * @param fwd Is this fwd?
2897 tunnel_queue_data (struct MeshTunnel2 *t,
2898 struct MeshChannel *ch,
2899 struct GNUNET_MessageHeader *msg,
2902 struct MeshTunnelQueue *tq;
2903 uint16_t size = ntohs (msg->size);
2905 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2908 memcpy (&tq[1], msg, size);
2909 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2914 * Send all cached messages that we can, tunnel is online.
2916 * @param t Tunnel that holds the messages.
2917 * @param fwd Is this fwd?
2920 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2922 struct MeshTunnelQueue *tq;
2923 struct MeshTunnelQueue *next;
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 "tunnel_send_queued_data on tunnel %s\n",
2929 room = tunnel_get_buffer (t, fwd);
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2931 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2935 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2936 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2945 connection_change_state (struct MeshConnection* c,
2946 enum MeshConnectionState state)
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "Connection %s state was %s\n",
2950 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 "Connection %s state is now %s\n",
2953 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
2959 * Add a client to a channel, initializing all needed data structures.
2961 * @param ch Channel to which add the client.
2962 * @param c Client which to add to the channel.
2965 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2967 struct MeshTunnel2 *t = ch->t;
2969 if (NULL != ch->dest)
2975 /* Assign local id as destination */
2976 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2977 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2978 ch->lid_dest = t->next_local_chid++;
2979 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2981 /* Store in client's hashmap */
2983 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2991 GNUNET_break (NULL == ch->dest_rel);
2992 ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
2993 ch->dest_rel->ch = ch;
2994 ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
3000 static struct MeshConnection *
3001 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3003 struct MeshConnection *c;
3004 struct GNUNET_HashCode cid;
3005 struct MeshPeer *peer;
3006 unsigned int own_pos;
3008 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3010 c = connection_new (&cid);
3012 for (own_pos = 0; own_pos < p->length; own_pos++)
3014 if (p->peers[own_pos] == myid)
3017 if (own_pos > p->length - 1)
3020 connection_destroy (c);
3023 c->own_pos = own_pos;
3028 c->fwd_maintenance_task =
3029 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3030 &connection_fwd_keepalive, c);
3033 peer = connection_get_next_hop (c);
3034 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3035 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3036 peer = connection_get_prev_hop (c);
3037 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3038 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3044 * Notifies a tunnel that a connection has broken that affects at least
3045 * some of its peers. Sends a notification towards the root of the tree.
3046 * In case the peer is the owner of the tree, notifies the client that owns
3047 * the tunnel and tries to reconnect.
3049 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3051 * @param t Tunnel affected.
3052 * @param p1 Peer that got disconnected from p2.
3053 * @param p2 Peer that got disconnected from p1.
3055 * @return Short ID of the peer disconnected (either p1 or p2).
3056 * 0 if the tunnel remained unaffected.
3058 static GNUNET_PEER_Id
3059 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3060 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3062 // if (myid != p1 && myid != p2) FIXME
3067 // if (tree_get_predecessor (t->tree) != 0)
3069 // /* We are the peer still connected, notify owner of the disconnection. */
3070 // struct GNUNET_MESH_PathBroken msg;
3071 // struct GNUNET_PeerIdentity neighbor;
3073 // msg.header.size = htons (sizeof (msg));
3074 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3075 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3076 // msg.tid = htonl (t->id.tid);
3077 // msg.peer1 = my_full_id;
3078 // GNUNET_PEER_resolve (pid, &msg.peer2);
3079 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3080 // send_prebuilt_message (&msg.header, &neighbor, t);
3087 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3089 * If channel is not reliable, do nothing.
3091 * @param ch Channel this is about.
3092 * @param fwd Is for FWD traffic? (ACK dest->owner)
3095 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3097 struct GNUNET_MESH_DataACK msg;
3098 struct MeshChannelReliability *rel;
3099 struct MeshReliableMessage *copy;
3104 if (GNUNET_NO == ch->reliable)
3108 rel = fwd ? ch->dest_rel : ch->root_rel;
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 "send_data_ack for %u\n",
3113 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3114 msg.header.type = htons (type);
3115 msg.header.size = htons (sizeof (msg));
3116 msg.chid = htonl (ch->gid);
3117 msg.mid = htonl (rel->mid_recv - 1);
3119 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3121 if (copy->type != type)
3123 delta = copy->mid - rel->mid_recv;
3126 mask = 0x1LL << delta;
3127 msg.futures |= mask;
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3130 copy->mid, delta, mask, msg.futures);
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3134 send_prebuilt_message_channel (&msg.header, ch, fwd);
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3140 * Send an ACK informing the predecessor about the available buffer space.
3142 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3143 * the ACK itself goes "back" (dest->root).
3145 * @param c Connection on which to send the ACK.
3146 * @param fwd Is this FWD ACK? (Going dest->owner)
3149 connection_send_ack (struct MeshConnection *c, int fwd)
3151 struct MeshFlowControl *next_fc;
3152 struct MeshFlowControl *prev_fc;
3153 struct GNUNET_MESH_ACK msg;
3157 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3158 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3161 "connection send %s ack on %s\n",
3162 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3164 /* Check if we need to transmit the ACK */
3165 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 " last pid recv: %u, last ack sent: %u\n",
3170 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3174 /* Ok, ACK might be necessary, what PID to ACK? */
3175 delta = next_fc->queue_max - next_fc->queue_n;
3176 ack = prev_fc->last_pid_recv + delta;
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3179 " last pid %u, last ack %u, qmax %u, q %u\n",
3180 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3181 next_fc->queue_max, next_fc->queue_n);
3182 if (ack == prev_fc->last_ack_sent)
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3188 prev_fc->last_ack_sent = ack;
3190 /* Build ACK message and send on connection */
3191 msg.header.size = htons (sizeof (msg));
3192 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3193 msg.ack = htonl (ack);
3196 send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
3201 * Modify the mesh message TID from global to local and send to client.
3203 * @param ch Channel on which to send the message.
3204 * @param msg Message to modify and send.
3205 * @param c Client to send to.
3206 * @param tid Tunnel ID to use (c can be both owner and client).
3209 channel_send_client_to_tid (struct MeshChannel *ch,
3210 const struct GNUNET_MESH_Data *msg,
3211 struct MeshClient *c, MESH_ChannelNumber id)
3213 struct GNUNET_MESH_LocalData *copy;
3214 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3215 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3217 if (size < sizeof (struct GNUNET_MessageHeader))
3219 GNUNET_break_op (0);
3227 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3228 memcpy (©[1], &msg[1], size);
3229 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3230 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3231 copy->id = htonl (id);
3232 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3233 ©->header, GNUNET_NO);
3237 * Modify the data message ID from global to local and send to client.
3239 * @param ch Channel on which to send the message.
3240 * @param msg Message to modify and send.
3241 * @param fwd Forward?
3244 channel_send_client_data (struct MeshChannel *ch,
3245 const struct GNUNET_MESH_Data *msg,
3249 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3251 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3256 * Send a buffered message to the client, for in order delivery or
3257 * as result of client ACK.
3259 * @param ch Channel on which to empty the message buffer.
3260 * @param c Client to send to.
3261 * @param rel Reliability structure to corresponding peer.
3262 * If rel == bck_rel, this is FWD data.
3265 channel_send_client_buffered_data (struct MeshChannel *ch,
3266 struct MeshClient *c,
3269 struct MeshReliableMessage *copy;
3270 struct MeshChannelReliability *rel;
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3273 rel = fwd ? ch->dest_rel : ch->root_rel;
3274 if (GNUNET_NO == rel->client_ready)
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3280 copy = rel->head_recv;
3281 /* We never buffer channel management messages */
3284 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
3286 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 " have %u! now expecting %u\n",
3290 copy->mid, rel->mid_recv + 1);
3291 channel_send_client_data (ch, msg, fwd);
3294 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 " reliable && don't have %u, next is %u\n",
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3311 * We have received a message out of order, or the client is not ready.
3312 * Buffer it until we receive an ACK from the client or the missing
3313 * message from the channel.
3315 * @param msg Message to buffer (MUST be of type MESH_DATA).
3316 * @param rel Reliability data to the corresponding direction.
3319 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3320 struct MeshChannelReliability *rel)
3322 struct MeshReliableMessage *copy;
3323 struct MeshReliableMessage *prev;
3327 size = ntohs (msg->header.size);
3328 mid = ntohl (msg->mid);
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3332 copy = GNUNET_malloc (sizeof (*copy) + size);
3335 memcpy (©[1], msg, size);
3339 // FIXME do something better than O(n), although n < 64...
3340 // FIXME start from the end (most messages are the latest ones)
3341 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3344 if (GMC_is_pid_bigger (prev->mid, mid))
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3347 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3353 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3359 * Destroy a reliable message after it has been acknowledged, either by
3360 * direct mid ACK or bitfield. Updates the appropriate data structures and
3361 * timers and frees all memory.
3363 * @param copy Message that is no longer needed: remote peer got it.
3366 rel_message_free (struct MeshReliableMessage *copy)
3368 struct MeshChannelReliability *rel;
3369 struct GNUNET_TIME_Relative time;
3372 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3373 rel->expected_delay.rel_value_us *= 7;
3374 rel->expected_delay.rel_value_us += time.rel_value_us;
3375 rel->expected_delay.rel_value_us /= 8;
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3380 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3382 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3384 rel->retry_timer = rel->expected_delay;
3385 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3391 * Destroy all reliable messages queued for a channel,
3392 * during a channel destruction.
3393 * Frees the reliability structure itself.
3395 * @param rel Reliability data for a channel.
3398 channel_rel_free_all (struct MeshChannelReliability *rel)
3400 struct MeshReliableMessage *copy;
3401 struct MeshReliableMessage *next;
3406 for (copy = rel->head_recv; NULL != copy; copy = next)
3409 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3412 for (copy = rel->head_sent; NULL != copy; copy = next)
3415 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3418 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3419 GNUNET_SCHEDULER_cancel (rel->retry_task);
3425 * Mark future messages as ACK'd.
3427 * @param rel Reliability data.
3428 * @param msg DataACK message with a bitfield of future ACK'd messages.
3431 channel_rel_free_sent (struct MeshChannelReliability *rel,
3432 const struct GNUNET_MESH_DataACK *msg)
3434 struct MeshReliableMessage *copy;
3435 struct MeshReliableMessage *next;
3442 bitfield = msg->futures;
3443 mid = ntohl (msg->mid);
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3445 "free_sent_reliable %u %llX\n",
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3448 " rel %p, head %p\n",
3449 rel, rel->head_sent);
3450 for (i = 0, copy = rel->head_sent;
3451 i < 64 && NULL != copy && 0 != bitfield;
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3455 " trying bit %u (mid %u)\n",
3458 if (0 == (bitfield & mask))
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3462 /* Bit was set, clear the bit from the bitfield */
3465 /* The i-th bit was set. Do we have that copy? */
3466 /* Skip copies with mid < target */
3467 target = mid + i + 1;
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3469 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3472 /* Did we run out of copies? (previously freed, it's ok) */
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3479 /* Did we overshoot the target? (previously freed, it's ok) */
3480 if (GMC_is_pid_bigger (copy->mid, target))
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3486 /* Now copy->mid == target, free it */
3488 rel_message_free (copy);
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3496 * We haven't received an ACK after a certain time: restransmit the message.
3498 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3499 * @param tc TaskContext.
3502 channel_retransmit_message (void *cls,
3503 const struct GNUNET_SCHEDULER_TaskContext *tc)
3505 struct MeshChannelReliability *rel = cls;
3506 struct MeshReliableMessage *copy;
3507 struct MeshPeerQueue *q;
3508 struct MeshChannel *ch;
3509 struct MeshConnection *c;
3510 struct GNUNET_MESH_Data *payload;
3511 struct MeshPeer *hop;
3514 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3515 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3519 copy = rel->head_sent;
3526 /* Search the message to be retransmitted in the outgoing queue.
3527 * Check only the queue for the connection that is going to be used,
3528 * if the message is stuck in some other connection's queue we shouldn't
3530 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3531 * the old connection could be temporary stalled or the queue happened to
3532 * be long at time of insertion.
3533 * - not sending the new one could cause terrible delays the old connection
3536 payload = (struct GNUNET_MESH_Data *) ©[1];
3537 fwd = (rel == ch->root_rel);
3538 c = tunnel_get_connection (ch->t, fwd);
3539 hop = connection_get_hop (c, fwd);
3540 for (q = hop->queue_head; NULL != q; q = q->next)
3542 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3544 struct GNUNET_MESH_Data *queued_data = q->cls;
3546 if (queued_data->mid == payload->mid)
3551 /* Message not found in the queue that we are going to use. */
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3556 send_prebuilt_message_channel (&payload->header, ch, fwd);
3557 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3564 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3565 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3566 &channel_retransmit_message,
3572 * Send an ACK to a client if needed.
3574 * @param ch Channel this is regarding.
3575 * @param fwd Is this about fwd traffic? (ACK goes the opposite direction).
3578 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3580 struct MeshChannelReliability *rel;
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 "Channel send client %s ack on %s:%X\n",
3584 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3586 /* Check for buffer space */
3587 if (0 >= tunnel_get_buffer (ch->t, fwd))
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " No buffer :(\n");
3593 /* Send ACK (fwd indicates traffic to be ACK'd) to client */
3594 rel = fwd ? ch->root_rel : ch->dest_rel;
3595 if (GNUNET_NO == rel->client_ready)
3596 send_local_ack (ch, fwd);
3598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client was ready\n");
3603 * Send ACK on one or more connections due to buffer space to the client.
3605 * Iterates all connections of the tunnel and sends ACKs appropriately.
3607 * @param ch Channel which has some free buffer space.
3608 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
3611 channel_send_connection_ack (struct MeshChannel *ch, int fwd)
3613 struct MeshTunnel2 *t = ch->t;
3614 struct MeshConnection *c;
3615 struct MeshFlowControl *fc;
3616 struct MeshChannelReliability *rel;
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 "Channel send connection %s ack on %s:%X\n",
3624 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3627 rel = fwd ? ch->root_rel : ch->dest_rel;
3633 buffer = 64 - rel->n_sent;
3635 /* Count connections, how many messages are already allowed */
3636 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3638 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3639 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3644 allowed += fc->last_ack_sent - fc->last_pid_recv;
3648 /* Make sure there is no overflow */
3649 if (allowed > buffer)
3655 /* Authorize connections to send more data */
3656 to_allow = buffer - allowed;
3657 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3659 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3660 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3664 connection_send_ack (c, fwd);
3668 GNUNET_break (to_allow == 0);
3673 * Send an ACK on the appropriate connection/channel, depending on
3674 * the direction and the position of the peer.
3676 * @param c Which connection to send the hop-by-hop ACK.
3677 * @param ch Channel, if any.
3678 * @param fwd Is this a fwd ACK? (will go dest->root)
3681 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
3685 connection_send_ack (c, fwd);
3688 channel_send_connection_ack (ch, fwd);
3693 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3695 * @param ch Channel to mark as ready.
3696 * @param fwd Was the CREATE message sent fwd?
3699 channel_confirm (struct MeshChannel *ch, int fwd)
3701 struct MeshChannelReliability *rel;
3702 struct MeshReliableMessage *copy;
3703 struct MeshReliableMessage *next;
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3706 " channel confirm %s:%X\n",
3707 peer2s (ch->t->peer), ch->gid);
3708 ch->state = MESH_CHANNEL_READY;
3710 rel = fwd ? ch->root_rel : ch->dest_rel;
3711 for (copy = rel->head_sent; NULL != copy; copy = next)
3713 struct GNUNET_MessageHeader *msg;
3716 msg = (struct GNUNET_MessageHeader *) ©[1];
3717 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3719 rel_message_free (copy);
3727 * Save a copy to retransmit in case it gets lost.
3729 * Initializes all needed callbacks and timers.
3731 * @param ch Channel this message goes on.
3732 * @param msg Message to copy.
3733 * @param fwd Is this fwd traffic?
3736 channel_save_copy (struct MeshChannel *ch,
3737 const struct GNUNET_MessageHeader *msg,
3740 struct MeshChannelReliability *rel;
3741 struct MeshReliableMessage *copy;
3746 rel = fwd ? ch->root_rel : ch->dest_rel;
3747 mid = rel->mid_send;
3748 type = ntohs (msg->type);
3749 size = ntohs (msg->size);
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3752 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3754 copy->timestamp = GNUNET_TIME_absolute_get ();
3757 memcpy (©[1], msg, size);
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3760 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3761 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3764 GNUNET_TIME_relative_multiply (rel->expected_delay,
3765 MESH_RETRANSMIT_MARGIN);
3767 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3768 &channel_retransmit_message,
3775 * Send keepalive packets for a connection.
3777 * @param c Connection to keep alive..
3778 * @param fwd Is this a FWD keepalive? (owner -> dest).
3781 connection_keepalive (struct MeshConnection *c, int fwd)
3783 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3784 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3788 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3789 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3792 "sending %s keepalive for connection %s[%d]\n",
3793 fwd ? "FWD" : "BCK",
3794 peer2s (c->t->peer),
3797 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3798 msg->header.size = htons (size);
3799 msg->header.type = htons (type);
3802 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3807 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3809 * @param c Connection for which to send the message.
3810 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3813 connection_recreate (struct MeshConnection *c, int fwd)
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3817 send_connection_create (c);
3819 send_connection_ack (c, GNUNET_NO);
3824 * Generic connection timer management.
3825 * Depending on the role of the peer in the connection will send the
3826 * appropriate message (build or keepalive)
3828 * @param c Conncetion to maintain.
3829 * @param fwd Is FWD?
3832 connection_maintain (struct MeshConnection *c, int fwd)
3834 if (MESH_TUNNEL_SEARCHING == c->t->state)
3836 /* TODO DHT GET with RO_BART */
3841 case MESH_CONNECTION_NEW:
3843 case MESH_CONNECTION_SENT:
3844 connection_recreate (c, fwd);
3846 case MESH_CONNECTION_READY:
3847 connection_keepalive (c, fwd);
3856 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3858 struct MeshConnection *c = cls;
3860 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3861 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3864 connection_maintain (c, GNUNET_YES);
3865 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3866 &connection_fwd_keepalive,
3872 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3874 struct MeshConnection *c = cls;
3876 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3877 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3880 connection_maintain (c, GNUNET_NO);
3881 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3882 &connection_bck_keepalive,
3888 * Send a message to all peers in this connection that the connection
3889 * is no longer valid.
3891 * If some peer should not receive the message, it should be zero'ed out
3892 * before calling this function.
3894 * @param c The connection whose peers to notify.
3897 connection_send_destroy (struct MeshConnection *c)
3899 struct GNUNET_MESH_ConnectionDestroy msg;
3901 msg.header.size = htons (sizeof (msg));
3902 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3905 " sending connection destroy for connection %s[%X]\n",
3906 peer2s (c->t->peer),
3909 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3910 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3911 c->destroy = GNUNET_YES;
3916 * Confirm we got a channel create.
3918 * @param ch The channel to confirm.
3919 * @param fwd Should we send the ACK fwd?
3922 channel_send_ack (struct MeshChannel *ch, int fwd)
3924 struct GNUNET_MESH_ChannelManage msg;
3926 msg.header.size = htons (sizeof (msg));
3927 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3929 " sending channel ack for channel %s:%X\n",
3930 peer2s (ch->t->peer),
3933 msg.chid = htonl (ch->gid);
3934 send_prebuilt_message_channel (&msg.header, ch, fwd);
3939 * Send a message to all clients (local and remote) of this channel
3940 * notifying that the channel is no longer valid.
3942 * If some peer or client should not receive the message,
3943 * should be zero'ed out before calling this function.
3945 * @param ch The channel whose clients to notify.
3948 channel_send_destroy (struct MeshChannel *ch)
3950 struct GNUNET_MESH_ChannelManage msg;
3952 msg.header.size = htons (sizeof (msg));
3953 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3955 " sending channel destroy for channel %s:%X\n",
3956 peer2s (ch->t->peer),
3959 if (NULL != ch->root)
3961 msg.chid = htonl (ch->lid_root);
3962 send_local_channel_destroy (ch, GNUNET_NO);
3966 msg.chid = htonl (ch->gid);
3967 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3970 if (NULL != ch->dest)
3972 msg.chid = htonl (ch->lid_dest);
3973 send_local_channel_destroy (ch, GNUNET_YES);
3977 msg.chid = htonl (ch->gid);
3978 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3986 static struct MeshTunnel2 *
3989 struct MeshTunnel2 *t;
3991 t = GNUNET_new (struct MeshTunnel2);
3993 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3995 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3996 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3998 // GNUNET_break (0);
3999 // tunnel_destroy (t);
4003 // char salt[] = "salt";
4004 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4005 // salt, sizeof (salt),
4006 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4007 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4008 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4010 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4011 // salt, sizeof (salt),
4012 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4013 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4014 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4022 * Add a connection to a tunnel.
4025 * @param c Connection.
4028 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4031 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4036 * Initialize a Flow Control structure to the initial state.
4038 * @param fc Flow Control structure to initialize.
4041 fc_init (struct MeshFlowControl *fc)
4044 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4045 fc->last_pid_recv = (uint32_t) -1;
4046 fc->last_ack_sent = (uint32_t) 0;
4047 fc->last_ack_recv = (uint32_t) 0;
4048 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4049 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4051 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4055 static struct MeshConnection *
4056 connection_new (const struct GNUNET_HashCode *cid)
4058 struct MeshConnection *c;
4060 c = GNUNET_new (struct MeshConnection);
4062 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4063 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4064 fc_init (&c->fwd_fc);
4065 fc_init (&c->bck_fc);
4074 * Find a connection.
4076 * @param cid Connection ID.
4078 static struct MeshConnection *
4079 connection_get (const struct GNUNET_HashCode *cid)
4081 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4086 connection_destroy (struct MeshConnection *c)
4088 struct MeshPeer *peer;
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4094 peer2s (c->t->peer),
4097 /* Cancel all traffic */
4098 connection_cancel_queues (c, GNUNET_YES);
4099 connection_cancel_queues (c, GNUNET_NO);
4101 /* Cancel maintainance task (keepalive/timeout) */
4102 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4103 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4104 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4105 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4107 /* Deregister from neighbors */
4108 peer = connection_get_next_hop (c);
4110 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4111 peer = connection_get_prev_hop (c);
4113 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4116 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4117 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4123 tunnel_destroy (struct MeshTunnel2 *t)
4125 struct MeshConnection *c;
4126 struct MeshConnection *next;
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4134 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4135 // GNUNET_break (0);
4137 for (c = t->connection_head; NULL != c; c = next)
4140 connection_destroy (c);
4143 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4150 * Tunnel is empty: destroy it.
4152 * Notifies all connections about the destruction.
4154 * @param t Tunnel to destroy.
4157 tunnel_destroy_empty (struct MeshTunnel2 *t)
4159 struct MeshConnection *c;
4161 for (c = t->connection_head; NULL != c; c = c->next)
4163 if (GNUNET_NO == c->destroy)
4164 connection_send_destroy (c);
4167 if (0 == t->pending_messages)
4170 t->destroy = GNUNET_YES;
4175 * Destroy tunnel if empty (no more channels).
4177 * @param t Tunnel to destroy if empty.
4180 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4182 if (NULL != t->channel_head)
4185 tunnel_destroy_empty (t);
4190 * Destroy a channel and free all resources.
4192 * @param ch Channel to destroy.
4195 channel_destroy (struct MeshChannel *ch)
4197 struct MeshClient *c;
4205 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4216 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4223 channel_rel_free_all (ch->root_rel);
4224 channel_rel_free_all (ch->dest_rel);
4226 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4227 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4233 * Create a new channel.
4235 * @param t Tunnel this channel is in.
4236 * @param owner Client that owns the channel, NULL for foreign channels.
4237 * @param lid_root Local ID for root client.
4239 * @return A new initialized channel. NULL on error.
4241 static struct MeshChannel *
4242 channel_new (struct MeshTunnel2 *t,
4243 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4245 struct MeshChannel *ch;
4247 ch = GNUNET_new (struct MeshChannel);
4249 ch->lid_root = lid_root;
4252 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4254 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4258 while (NULL != channel_get (t, t->next_chid))
4259 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4260 ch->gid = t->next_chid;
4261 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4264 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4268 channel_destroy (ch);
4269 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4279 * Set options in a channel, extracted from a bit flag field
4281 * @param ch Channel to set options to.
4282 * @param options Bit array in host byte order.
4285 channel_set_options (struct MeshChannel *ch, uint32_t options)
4287 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4288 GNUNET_YES : GNUNET_NO;
4289 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4290 GNUNET_YES : GNUNET_NO;
4295 * Iterator for deleting each channel whose client endpoint disconnected.
4297 * @param cls Closure (client that has disconnected).
4298 * @param key The local channel id (used to access the hashmap).
4299 * @param value The value stored at the key (channel to destroy).
4301 * @return GNUNET_OK, keep iterating.
4304 channel_destroy_iterator (void *cls,
4308 struct MeshChannel *ch = value;
4309 struct MeshClient *c = cls;
4310 struct MeshTunnel2 *t;
4312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4313 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4314 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4328 channel_send_destroy (ch);
4329 channel_destroy (ch);
4330 tunnel_destroy_if_empty (t);
4337 * Remove client's ports from the global hashmap on disconnect.
4339 * @param cls Closure (unused).
4341 * @param value Client structure.
4343 * @return GNUNET_OK, keep iterating.
4346 client_release_ports (void *cls,
4352 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4353 if (GNUNET_YES != res)
4356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4357 "Port %u by client %p was not registered.\n",
4365 * Timeout function due to lack of keepalive/traffic from the owner.
4366 * Destroys connection if called.
4368 * @param cls Closure (connection to destroy).
4369 * @param tc TaskContext.
4372 connection_fwd_timeout (void *cls,
4373 const struct GNUNET_SCHEDULER_TaskContext *tc)
4375 struct MeshConnection *c = cls;
4377 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4378 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4381 "Connection %s[%X] FWD timed out. Destroying.\n",
4382 peer2s (c->t->peer),
4385 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4388 connection_destroy (c);
4393 * Timeout function due to lack of keepalive/traffic from the destination.
4394 * Destroys connection if called.
4396 * @param cls Closure (connection to destroy).
4397 * @param tc TaskContext
4400 connection_bck_timeout (void *cls,
4401 const struct GNUNET_SCHEDULER_TaskContext *tc)
4403 struct MeshConnection *c = cls;
4405 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4406 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4410 "Connection %s[%X] FWD timed out. Destroying.\n",
4411 peer2s (c->t->peer),
4414 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4417 connection_destroy (c);
4422 * Resets the connection timeout task, some other message has done the
4424 * - For the first peer on the direction this means to send
4425 * a keepalive or a path confirmation message (either create or ACK).
4426 * - For all other peers, this means to destroy the connection,
4427 * due to lack of activity.
4428 * Starts the tiemout if no timeout was running (connection just created).
4430 * @param c Connection whose timeout to reset.
4431 * @param fwd Is this forward?
4433 * TODO use heap to improve efficiency of scheduler.
4436 connection_reset_timeout (struct MeshConnection *c, int fwd)
4438 GNUNET_SCHEDULER_TaskIdentifier *ti;
4439 GNUNET_SCHEDULER_Task f;
4441 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4443 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4444 GNUNET_SCHEDULER_cancel (*ti);
4446 if (connection_is_origin (c, fwd)) /* Endpoint */
4448 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4449 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4453 struct GNUNET_TIME_Relative delay;
4455 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4456 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4457 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4463 * Iterator to notify all connections of a broken link. Mark connections
4464 * to destroy after all traffic has been sent.
4466 * @param cls Closure (peer disconnected).
4467 * @param key Current key code (tid).
4468 * @param value Value in the hash map (connection).
4470 * @return GNUNET_YES if we should continue to iterate,
4474 connection_broken (void *cls,
4475 const struct GNUNET_HashCode *key,
4478 struct MeshPeer *peer = cls;
4479 struct MeshConnection *c = value;
4480 struct GNUNET_MESH_ConnectionBroken msg;
4483 fwd = peer == connection_get_prev_hop (c);
4484 connection_cancel_queues (c, !fwd);
4486 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4487 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4489 msg.peer1 = my_full_id;
4490 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4491 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4492 c->destroy = GNUNET_YES;
4497 /******************************************************************************/
4498 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4499 /******************************************************************************/
4502 * Free a transmission that was already queued with all resources
4503 * associated to the request.
4505 * @param queue Queue handler to cancel.
4506 * @param clear_cls Is it necessary to free associated cls?
4509 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4511 struct MeshPeer *peer;
4512 struct MeshFlowControl *fc;
4517 GNUNET_assert (NULL != queue->c);
4518 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4520 if (GNUNET_YES == clear_cls)
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4523 GNUNET_MESH_DEBUG_M2S (queue->type));
4524 switch (queue->type)
4526 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4527 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4528 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4529 GNUNET_break (GNUNET_YES == queue->c->destroy);
4531 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4532 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4533 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4534 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4535 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4536 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4538 GNUNET_free_non_null (queue->cls);
4543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4544 GNUNET_MESH_DEBUG_M2S (queue->type));
4548 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4550 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4551 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4557 if (NULL != queue->c)
4559 queue->c->pending_messages--;
4560 if (NULL != queue->c->t)
4562 queue->c->t->pending_messages--;
4566 GNUNET_free (queue);
4571 queue_send (void *cls, size_t size, void *buf)
4573 struct MeshPeer *peer = cls;
4574 struct MeshFlowControl *fc;
4575 struct MeshConnection *c;
4576 struct GNUNET_MessageHeader *msg;
4577 struct MeshPeerQueue *queue;
4578 struct MeshTunnel2 *t;
4579 struct MeshChannel *ch;
4580 const struct GNUNET_PeerIdentity *dst_id;
4586 peer->core_transmit = NULL;
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4589 if (NULL == buf || 0 == size)
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4596 queue = peer_get_first_message (peer);
4599 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4604 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4607 dst_id = GNUNET_PEER_resolve2 (peer->id);
4608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4609 /* Check if buffer size is enough for the message */
4610 if (queue->size > size)
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4613 peer->core_transmit =
4614 GNUNET_CORE_notify_transmit_ready (core_handle,
4617 GNUNET_TIME_UNIT_FOREVER_REL,
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4626 t = (NULL != c) ? c->t : NULL;
4630 switch (queue->type)
4632 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4633 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4634 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4635 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4636 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4637 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4640 GNUNET_MESH_DEBUG_M2S (queue->type));
4641 data_size = send_core_data_raw (queue->cls, size, buf);
4642 msg = (struct GNUNET_MessageHeader *) buf;
4643 type = ntohs (msg->type);
4645 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4647 if (connection_is_origin (c, GNUNET_YES))
4648 data_size = send_core_connection_create (queue->c, size, buf);
4650 data_size = send_core_data_raw (queue->cls, size, buf);
4652 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4654 if (connection_is_origin (c, GNUNET_NO))
4655 data_size = send_core_connection_ack (queue->c, size, buf);
4657 data_size = send_core_data_raw (queue->cls, size, buf);
4659 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4660 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4661 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4662 /* This should be encapsulted */
4667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4672 if (0 < drop_percent &&
4673 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4676 "Dropping message of type %s\n",
4677 GNUNET_MESH_DEBUG_M2S (queue->type));
4681 /* Free queue, but cls was freed by send_core_* */
4683 queue_destroy (queue, GNUNET_NO);
4685 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4688 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4689 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4690 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4691 fc->last_pid_sent = pid;
4693 channel_send_client_ack (ch, fwd);
4695 connection_send_ack (c, fwd);
4701 /* If more data in queue, send next */
4702 queue = peer_get_first_message (peer);
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4706 if (NULL == peer->core_transmit) {
4707 peer->core_transmit =
4708 GNUNET_CORE_notify_transmit_ready(core_handle,
4711 GNUNET_TIME_UNIT_FOREVER_REL,
4719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4720 "* tmt rdy called somewhere else\n");
4722 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4726 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4731 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4733 GNUNET_SCHEDULER_cancel (fc->poll_task);
4734 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4739 c->pending_messages--;
4740 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4743 connection_destroy (c);
4749 t->pending_messages--;
4750 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4752 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4762 queue_add (void *cls, uint16_t type, size_t size,
4763 struct MeshConnection *c,
4764 struct MeshChannel *ch,
4767 struct MeshPeerQueue *queue;
4768 struct MeshFlowControl *fc;
4769 struct MeshPeer *peer;
4772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4773 "queue add %s %s (%u) on c %p, ch %p\n",
4774 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4775 GNUNET_assert (NULL != c);
4777 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4778 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4788 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4789 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4796 if (fc->queue_n >= fc->queue_max && 0 == priority)
4798 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4802 "queue full: %u/%u\n",
4803 fc->queue_n, fc->queue_max);
4804 return; /* Drop this message */
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4809 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4810 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 "no buffer space (%u > %u): starting poll\n",
4814 fc->last_pid_sent + 1, fc->last_ack_recv);
4815 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4819 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4827 if (100 <= priority)
4829 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4833 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
4839 if (NULL == peer->core_transmit)
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4842 "calling core tmt rdy towards %s for %u bytes\n",
4843 peer2s (peer), size);
4844 peer->core_transmit =
4845 GNUNET_CORE_notify_transmit_ready (core_handle,
4848 GNUNET_TIME_UNIT_FOREVER_REL,
4849 GNUNET_PEER_resolve2 (peer->id),
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4857 "core tmt rdy towards %s already called\n",
4861 c->pending_messages++;
4862 c->t->pending_messages++;
4866 /******************************************************************************/
4867 /******************** MESH NETWORK HANDLERS **************************/
4868 /******************************************************************************/
4872 * Generic handler for mesh network payload traffic.
4874 * @param t Tunnel on which we got this message.
4875 * @param message Unencryted data message.
4876 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4878 * @return GNUNET_OK to keep the connection open,
4879 * GNUNET_SYSERR to close it (signal serious error)
4882 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4884 struct MeshChannelReliability *rel;
4885 struct MeshChannel *ch;
4886 struct MeshClient *c;
4892 size = ntohs (msg->header.size);
4894 sizeof (struct GNUNET_MESH_Data) +
4895 sizeof (struct GNUNET_MessageHeader))
4900 type = ntohs (msg->header.type);
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4902 GNUNET_MESH_DEBUG_M2S (type));
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4904 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4907 ch = channel_get (t, ntohl (msg->chid));
4910 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4915 /* Initialize FWD/BCK data */
4916 c = fwd ? ch->dest : ch->root;
4917 rel = fwd ? ch->dest_rel : ch->root_rel;
4925 tunnel_change_state (t, MESH_TUNNEL_READY);
4927 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4929 mid = ntohl (msg->mid);
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4932 if (GNUNET_NO == ch->reliable ||
4933 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4934 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4937 if (GNUNET_YES == ch->reliable)
4939 /* Is this the exact next expected messasge? */
4940 if (mid == rel->mid_recv)
4942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4944 channel_send_client_data (ch, msg, fwd);
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4949 channel_rel_add_buffered_data (msg, rel);
4954 /* Tunnel is unreliable: send to clients directly */
4955 /* FIXME: accept Out Of Order traffic */
4956 rel->mid_recv = mid + 1;
4957 channel_send_client_data (ch, msg, fwd);
4962 GNUNET_break_op (0);
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4964 " MID %u not expected (%u - %u), dropping!\n",
4965 mid, rel->mid_recv, rel->mid_recv + 64);
4968 channel_send_data_ack (ch, fwd);
4973 * Handler for mesh network traffic end-to-end ACKs.
4975 * @param t Tunnel on which we got this message.
4976 * @param message Data message.
4977 * @param fwd Is this a fwd ACK? (dest->orig)
4979 * @return GNUNET_OK to keep the connection open,
4980 * GNUNET_SYSERR to close it (signal serious error)
4983 handle_data_ack (struct MeshTunnel2 *t,
4984 const struct GNUNET_MESH_DataACK *msg, int fwd)
4986 struct MeshChannelReliability *rel;
4987 struct MeshReliableMessage *copy;
4988 struct MeshReliableMessage *next;
4989 struct MeshChannel *ch;
4994 type = ntohs (msg->header.type);
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4996 GNUNET_MESH_DEBUG_M2S (type));
4997 ch = channel_get (t, ntohl (msg->chid));
5000 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
5003 ack = ntohl (msg->mid);
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
5005 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5007 if (GNUNET_YES == fwd)
5020 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5022 if (GMC_is_pid_bigger (copy->mid, ack))
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5025 channel_rel_free_sent (rel, msg);
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5031 rel_message_free (copy);
5033 /* ACK client if needed */
5034 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5036 /* If some message was free'd, update the retransmission delay*/
5037 if (GNUNET_YES == work)
5039 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5041 GNUNET_SCHEDULER_cancel (rel->retry_task);
5042 if (NULL == rel->head_sent)
5044 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5048 struct GNUNET_TIME_Absolute new_target;
5049 struct GNUNET_TIME_Relative delay;
5051 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5052 MESH_RETRANSMIT_MARGIN);
5053 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5055 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5057 GNUNET_SCHEDULER_add_delayed (delay,
5058 &channel_retransmit_message,
5070 * Core handler for connection creation.
5072 * @param cls Closure (unused).
5073 * @param peer Sender (neighbor).
5074 * @param message Message.
5076 * @return GNUNET_OK to keep the connection open,
5077 * GNUNET_SYSERR to close it (signal serious error)
5080 handle_mesh_connection_create (void *cls,
5081 const struct GNUNET_PeerIdentity *peer,
5082 const struct GNUNET_MessageHeader *message)
5084 struct GNUNET_MESH_ConnectionCreate *msg;
5085 struct GNUNET_PeerIdentity *id;
5086 struct GNUNET_HashCode *cid;
5087 struct MeshPeerPath *path;
5088 struct MeshPeer *dest_peer;
5089 struct MeshPeer *orig_peer;
5090 struct MeshConnection *c;
5091 unsigned int own_pos;
5095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5099 size = ntohs (message->size);
5100 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5102 GNUNET_break_op (0);
5106 /* Calculate hops */
5107 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5108 if (size % sizeof (struct GNUNET_PeerIdentity))
5110 GNUNET_break_op (0);
5113 size /= sizeof (struct GNUNET_PeerIdentity);
5116 GNUNET_break_op (0);
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5121 /* Get parameters */
5122 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5124 id = (struct GNUNET_PeerIdentity *) &msg[1];
5125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5126 " connection %s (%s).\n",
5127 GNUNET_h2s (cid), GNUNET_i2s (id));
5129 /* Create connection */
5130 c = connection_get (cid);
5133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5134 c = connection_new (cid);
5138 connection_reset_timeout (c, GNUNET_YES);
5139 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5141 /* Remember peers */
5142 dest_peer = peer_get (&id[size - 1]);
5143 orig_peer = peer_get (&id[0]);
5146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5147 path = path_new (size);
5149 for (i = 0; i < size; i++)
5151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5152 GNUNET_i2s (&id[i]));
5153 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5154 if (path->peers[i] == myid)
5157 if (own_pos == 0 && path->peers[own_pos] != myid)
5159 /* create path: self not found in path through self */
5160 GNUNET_break_op (0);
5161 path_destroy (path);
5162 connection_destroy (c);
5165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5166 path_add_to_peers (path, GNUNET_NO);
5167 c->path = path_duplicate (path);
5168 c->own_pos = own_pos;
5170 /* Is it a connection to us? */
5171 if (own_pos == size - 1)
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5174 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5176 if (NULL == orig_peer->tunnel)
5177 orig_peer->tunnel = tunnel_new ();
5178 tunnel_add_connection (orig_peer->tunnel, c);
5180 send_connection_ack (c, GNUNET_NO);
5182 /* Keep tunnel alive in direction dest->owner*/
5183 connection_reset_timeout (c, GNUNET_NO);
5187 /* It's for somebody else! Retransmit. */
5188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5189 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5190 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5191 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5198 * Core handler for path ACKs
5200 * @param cls closure
5201 * @param message message
5202 * @param peer peer identity this notification is about
5204 * @return GNUNET_OK to keep the connection open,
5205 * GNUNET_SYSERR to close it (signal serious error)
5208 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5209 const struct GNUNET_MessageHeader *message)
5211 struct GNUNET_MESH_ConnectionACK *msg;
5212 struct MeshPeerPath *p;
5213 struct MeshConnection *c;
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5217 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5219 GNUNET_h2s (&msg->cid));
5220 c = connection_get (&msg->cid);
5223 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5232 /* Add path to peers? */
5236 path_add_to_peers (p, GNUNET_YES);
5242 connection_change_state (c, MESH_CONNECTION_READY);
5243 connection_reset_timeout (c, GNUNET_NO);
5244 if (MESH_TUNNEL_READY != c->t->state)
5245 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5246 tunnel_send_queued_data (c->t, GNUNET_YES);
5248 /* Message for us? */
5249 if (connection_is_terminal (c, GNUNET_NO))
5251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5252 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5254 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5255 c->t->peer->dhtget = NULL;
5257 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5262 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5268 * Core handler for notifications of broken paths
5270 * @param cls Closure (unused).
5271 * @param peer Peer identity of sending neighbor.
5272 * @param message Message.
5274 * @return GNUNET_OK to keep the connection open,
5275 * GNUNET_SYSERR to close it (signal serious error)
5278 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5279 const struct GNUNET_MessageHeader *message)
5281 struct GNUNET_MESH_ConnectionBroken *msg;
5282 struct MeshConnection *c;
5284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5285 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5286 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5288 GNUNET_i2s (&msg->peer1));
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5290 GNUNET_i2s (&msg->peer2));
5291 c = connection_get (&msg->cid);
5294 GNUNET_break_op (0);
5297 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5298 GNUNET_PEER_search (&msg->peer2));
5305 * Core handler for tunnel destruction
5307 * @param cls Closure (unused).
5308 * @param peer Peer identity of sending neighbor.
5309 * @param message Message.
5311 * @return GNUNET_OK to keep the connection open,
5312 * GNUNET_SYSERR to close it (signal serious error)
5315 handle_mesh_connection_destroy (void *cls,
5316 const struct GNUNET_PeerIdentity *peer,
5317 const struct GNUNET_MessageHeader *message)
5319 struct GNUNET_MESH_ConnectionDestroy *msg;
5320 struct MeshConnection *c;
5324 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5326 "Got a CONNECTION DESTROY message from %s\n",
5328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5329 " for connection %s\n",
5330 GNUNET_h2s (&msg->cid));
5331 c = connection_get (&msg->cid);
5334 /* Probably already got the message from another path,
5335 * destroyed the tunnel and retransmitted to children.
5338 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5342 id = GNUNET_PEER_search (peer);
5343 if (id == connection_get_prev_hop (c)->id)
5345 else if (id == connection_get_next_hop (c)->id)
5349 GNUNET_break_op (0);
5352 send_prebuilt_message_connection (message, c, NULL, fwd);
5353 c->destroy = GNUNET_YES;
5360 * Handler for channel create messages.
5362 * @param t Tunnel this channel is to be created in.
5363 * @param msg Message.
5364 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5366 * @return GNUNET_OK to keep the connection open,
5367 * GNUNET_SYSERR to close it (signal serious error)
5370 handle_channel_create (struct MeshTunnel2 *t,
5371 struct GNUNET_MESH_ChannelCreate *msg,
5374 MESH_ChannelNumber chid;
5375 struct MeshChannel *ch;
5376 struct MeshClient *c;
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5380 /* Check message size */
5381 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5383 GNUNET_break_op (0);
5387 /* Check if channel exists */
5388 chid = ntohl (msg->chid);
5389 ch = channel_get (t, chid);
5392 /* Probably a retransmission, safe to ignore */
5393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5394 if (NULL != ch->dest)
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5397 channel_send_ack (ch, !fwd);
5403 /* Create channel */
5404 ch = channel_new (t, NULL, 0);
5405 channel_set_options (ch, ntohl (msg->opt));
5408 /* Find a destination client */
5409 port = ntohl (msg->port);
5410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5411 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5414 /* TODO send reject */
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5419 channel_add_client (ch, c);
5420 if (GNUNET_YES == ch->reliable)
5421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5423 send_local_channel_create (ch);
5424 channel_send_ack (ch, !fwd);
5431 * Handler for channel ack messages.
5433 * @param t Tunnel this channel is to be created in.
5434 * @param msg Message.
5435 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5437 * @return GNUNET_OK to keep the connection open,
5438 * GNUNET_SYSERR to close it (signal serious error)
5441 handle_channel_ack (struct MeshTunnel2 *t,
5442 struct GNUNET_MESH_ChannelManage *msg,
5445 MESH_ChannelNumber chid;
5446 struct MeshChannel *ch;
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5449 /* Check message size */
5450 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5452 GNUNET_break_op (0);
5456 /* Check if channel exists */
5457 chid = ntohl (msg->chid);
5458 ch = channel_get (t, chid);
5461 GNUNET_break_op (0);
5462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5466 channel_confirm (ch, !fwd);
5472 * Handler for channel destroy messages.
5474 * @param t Tunnel this channel is to be destroyed of.
5475 * @param msg Message.
5476 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5478 * @return GNUNET_OK to keep the connection open,
5479 * GNUNET_SYSERR to close it (signal serious error)
5482 handle_channel_destroy (struct MeshTunnel2 *t,
5483 struct GNUNET_MESH_ChannelManage *msg,
5486 MESH_ChannelNumber chid;
5487 struct MeshChannel *ch;
5489 /* Check message size */
5490 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5492 GNUNET_break_op (0);
5496 /* Check if channel exists */
5497 chid = ntohl (msg->chid);
5498 ch = channel_get (t, chid);
5501 /* Probably a retransmission, safe to ignore */
5505 send_local_channel_destroy (ch, fwd);
5506 channel_destroy (ch);
5513 * Generic handler for mesh network encrypted traffic.
5515 * @param peer Peer identity this notification is about.
5516 * @param message Encrypted message.
5517 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5519 * @return GNUNET_OK to keep the connection open,
5520 * GNUNET_SYSERR to close it (signal serious error)
5523 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5524 const struct GNUNET_MESH_Encrypted *msg,
5527 struct MeshConnection *c;
5528 struct MeshTunnel2 *t;
5529 struct MeshPeer *neighbor;
5530 struct MeshFlowControl *fc;
5537 size = ntohs (msg->header.size);
5539 sizeof (struct GNUNET_MESH_Encrypted) +
5540 sizeof (struct GNUNET_MessageHeader))
5542 GNUNET_break_op (0);
5545 type = ntohs (msg->header.type);
5546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5548 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5550 /* Check connection */
5551 c = connection_get (&msg->cid);
5554 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5559 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5561 /* Check if origin is as expected */
5562 neighbor = connection_get_hop (c, fwd);
5563 if (peer_get (peer)->id != neighbor->id)
5565 GNUNET_break_op (0);
5570 pid = ntohl (msg->pid);
5571 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5573 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5575 "WARNING Received PID %u, (prev %u), ACK %u\n",
5576 pid, fc->last_pid_recv, fc->last_ack_sent);
5579 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5581 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5583 " Pid %u not expected (%u+), dropping!\n",
5584 pid, fc->last_pid_recv + 1);
5587 if (MESH_CONNECTION_SENT == c->state)
5588 connection_change_state (c, MESH_CONNECTION_READY);
5589 connection_reset_timeout (c, fwd);
5590 fc->last_pid_recv = pid;
5592 /* Is this message for us? */
5593 if (connection_is_terminal (c, fwd))
5595 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5597 struct GNUNET_MessageHeader *msgh;
5600 /* TODO signature verification */
5601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5602 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5604 fc->last_pid_recv = pid;
5605 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5606 msgh = (struct GNUNET_MessageHeader *) cbuf;
5607 switch (ntohs (msgh->type))
5609 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5610 /* Don't send hop ACK, wait for client to ACK */
5611 return handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5613 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5614 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5617 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5618 r = handle_channel_create (t,
5619 (struct GNUNET_MESH_ChannelCreate *) msgh,
5623 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5624 r = handle_channel_ack (t,
5625 (struct GNUNET_MESH_ChannelManage *) msgh,
5629 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5630 r = handle_channel_destroy (t,
5631 (struct GNUNET_MESH_ChannelManage *) msgh,
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5637 "end-to-end message not known (%u)\n",
5638 ntohs (msgh->type));
5642 connection_send_ack (c, fwd);
5646 /* Message not for us: forward to next hop */
5647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5648 ttl = ntohl (msg->ttl);
5649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5652 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5654 connection_send_ack (c, fwd);
5657 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5659 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5666 * Core handler for mesh network traffic going orig->dest.
5668 * @param cls Closure (unused).
5669 * @param message Message received.
5670 * @param peer Peer who sent the message.
5672 * @return GNUNET_OK to keep the connection open,
5673 * GNUNET_SYSERR to close it (signal serious error)
5676 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5677 const struct GNUNET_MessageHeader *message)
5679 return handle_mesh_encrypted (peer,
5680 (struct GNUNET_MESH_Encrypted *)message,
5685 * Core handler for mesh network traffic going dest->orig.
5687 * @param cls Closure (unused).
5688 * @param message Message received.
5689 * @param peer Peer who sent the message.
5691 * @return GNUNET_OK to keep the connection open,
5692 * GNUNET_SYSERR to close it (signal serious error)
5695 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5696 const struct GNUNET_MessageHeader *message)
5698 return handle_mesh_encrypted (peer,
5699 (struct GNUNET_MESH_Encrypted *)message,
5705 * Core handler for mesh network traffic point-to-point acks.
5707 * @param cls closure
5708 * @param message message
5709 * @param peer peer identity this notification is about
5711 * @return GNUNET_OK to keep the connection open,
5712 * GNUNET_SYSERR to close it (signal serious error)
5715 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5716 const struct GNUNET_MessageHeader *message)
5718 struct GNUNET_MESH_ACK *msg;
5719 struct MeshConnection *c;
5720 struct MeshFlowControl *fc;
5724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5727 msg = (struct GNUNET_MESH_ACK *) message;
5729 c = connection_get (&msg->cid);
5733 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5738 /* Is this a forward or backward ACK? */
5739 id = GNUNET_PEER_search (peer);
5740 if (connection_get_next_hop (c)->id == id)
5742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5745 else if (connection_get_prev_hop (c)->id == id)
5747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5752 GNUNET_break_op (0);
5756 ack = ntohl (msg->ack);
5757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5759 /* Cancel polling if the ACK is bigger than before. */
5760 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5761 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5764 GNUNET_SCHEDULER_cancel (fc->poll_task);
5765 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5766 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5769 fc->last_ack_recv = ack;
5770 connection_unlock_queue (c, fc == &c->fwd_fc);
5777 * Core handler for mesh network traffic point-to-point ack polls.
5779 * @param cls closure
5780 * @param message message
5781 * @param peer peer identity this notification is about
5783 * @return GNUNET_OK to keep the connection open,
5784 * GNUNET_SYSERR to close it (signal serious error)
5787 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5788 const struct GNUNET_MessageHeader *message)
5790 struct GNUNET_MESH_Poll *msg;
5791 struct MeshConnection *c;
5792 struct MeshFlowControl *fc;
5796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5800 msg = (struct GNUNET_MESH_Poll *) message;
5802 c = connection_get (&msg->cid);
5806 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5808 GNUNET_break_op (0);
5812 /* Is this a forward or backward ACK? */
5813 id = GNUNET_PEER_search (peer);
5814 if (connection_get_next_hop (c)->id == id)
5816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5819 else if (connection_get_prev_hop (c)->id == id)
5821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5826 GNUNET_break_op (0);
5830 pid = ntohl (msg->pid);
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5832 pid, fc->last_pid_recv);
5833 fc->last_pid_recv = pid;
5834 connection_send_ack (c, fc == &c->fwd_fc);
5841 * Core handler for mesh keepalives.
5843 * @param cls closure
5844 * @param message message
5845 * @param peer peer identity this notification is about
5846 * @return GNUNET_OK to keep the connection open,
5847 * GNUNET_SYSERR to close it (signal serious error)
5849 * TODO: Check who we got this from, to validate route.
5852 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5853 const struct GNUNET_MessageHeader *message)
5855 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5856 struct MeshConnection *c;
5857 struct MeshPeer *neighbor;
5860 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5864 c = connection_get (&msg->cid);
5867 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5872 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5873 GNUNET_YES : GNUNET_NO;
5875 /* Check if origin is as expected */
5876 neighbor = connection_get_hop (c, fwd);
5877 if (peer_get (peer)->id != neighbor->id)
5879 GNUNET_break_op (0);
5883 connection_change_state (c, MESH_CONNECTION_READY);
5884 connection_reset_timeout (c, fwd);
5886 if (connection_is_terminal (c, fwd))
5889 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5890 send_prebuilt_message_connection (message, c, NULL, fwd);
5898 * Functions to handle messages from core
5900 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5901 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5903 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5904 sizeof (struct GNUNET_MESH_ConnectionACK)},
5905 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5906 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5907 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5908 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5909 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5910 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5911 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5912 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5913 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5914 sizeof (struct GNUNET_MESH_ACK)},
5915 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5916 sizeof (struct GNUNET_MESH_Poll)},
5917 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5918 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5924 * Function to process paths received for a new peer addition. The recorded
5925 * paths form the initial tunnel, which can be optimized later.
5926 * Called on each result obtained for the DHT search.
5928 * @param cls closure
5929 * @param exp when will this value expire
5930 * @param key key of the result
5931 * @param get_path path of the get request
5932 * @param get_path_length lenght of get_path
5933 * @param put_path path of the put request
5934 * @param put_path_length length of the put_path
5935 * @param type type of the result
5936 * @param size number of bytes in data
5937 * @param data pointer to the result data
5940 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5941 const struct GNUNET_HashCode * key,
5942 const struct GNUNET_PeerIdentity *get_path,
5943 unsigned int get_path_length,
5944 const struct GNUNET_PeerIdentity *put_path,
5945 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5946 size_t size, const void *data)
5948 struct MeshPeer *peer = cls;
5949 struct MeshPeerPath *p;
5950 struct MeshConnection *c;
5951 struct GNUNET_PeerIdentity pi;
5954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5955 GNUNET_PEER_resolve (peer->id, &pi);
5956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5958 p = path_build_from_dht (get_path, get_path_length,
5959 put_path, put_path_length);
5960 path_add_to_peers (p, GNUNET_NO);
5963 /* Count connections */
5964 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5966 /* If we already have 3 (or more (?!)) connections, it's enough */
5970 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5973 peer_connect (peer);
5979 /******************************************************************************/
5980 /********************* MESH LOCAL HANDLES **************************/
5981 /******************************************************************************/
5985 * Handler for client connection.
5987 * @param cls Closure (unused).
5988 * @param client Client handler.
5991 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5993 struct MeshClient *c;
5997 c = GNUNET_malloc (sizeof (struct MeshClient));
5999 c->id = next_client_id++; /* overflow not important: just for debug */
6000 GNUNET_SERVER_client_keep (client);
6001 GNUNET_SERVER_client_set_user_context (client, c);
6002 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6007 * Handler for client disconnection
6009 * @param cls closure
6010 * @param client identification of the client; NULL
6011 * for the last call when the server is destroyed
6014 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6016 struct MeshClient *c;
6018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6025 c = client_get (client);
6028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6030 GNUNET_SERVER_client_drop (c->handle);
6031 c->shutting_down = GNUNET_YES;
6032 if (NULL != c->own_channels)
6034 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6035 &channel_destroy_iterator, c);
6036 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6039 if (NULL != c->incoming_channels)
6041 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6042 &channel_destroy_iterator, c);
6043 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6046 if (NULL != c->ports)
6048 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6049 &client_release_ports, c);
6050 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6052 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6053 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6059 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6067 * Handler for new clients
6069 * @param cls closure
6070 * @param client identification of the client
6071 * @param message the actual message, which includes messages the client wants
6074 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6075 const struct GNUNET_MessageHeader *message)
6077 struct GNUNET_MESH_ClientConnect *cc_msg;
6078 struct MeshClient *c;
6083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6085 /* Check data sanity */
6086 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6087 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6088 if (0 != (size % sizeof (uint32_t)))
6091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6094 size /= sizeof (uint32_t);
6096 /* Initialize new client structure */
6097 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6104 p = (uint32_t *) &cc_msg[1];
6105 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6106 for (i = 0; i < size; i++)
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6111 /* store in client's hashmap */
6112 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6114 /* store in global hashmap */
6115 /* FIXME only allow one client to have the port open,
6116 * have a backup hashmap with waiting clients */
6117 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6122 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6123 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6124 GNUNET_SERVER_notification_context_add (nc, client);
6125 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6127 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6133 * Handler for requests of new tunnels
6135 * @param cls Closure.
6136 * @param client Identification of the client.
6137 * @param message The actual message.
6140 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6141 const struct GNUNET_MessageHeader *message)
6143 struct GNUNET_MESH_ChannelMessage *msg;
6144 struct MeshPeer *peer;
6145 struct MeshTunnel2 *t;
6146 struct MeshChannel *ch;
6147 struct MeshClient *c;
6148 MESH_ChannelNumber chid;
6150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6152 /* Sanity check for client registration */
6153 if (NULL == (c = client_get (client)))
6156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6161 /* Message size sanity check */
6162 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6169 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6171 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6172 chid = ntohl (msg->channel_id);
6174 /* Sanity check for duplicate channel IDs */
6175 if (NULL != channel_get_by_local_id (c, chid))
6178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6182 peer = peer_get (&msg->peer);
6183 if (NULL == peer->tunnel)
6185 peer->tunnel = tunnel_new ();
6186 peer->tunnel->peer = peer;
6190 /* Create channel */
6191 ch = channel_new (t, c, chid);
6195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6198 ch->port = ntohl (msg->port);
6199 channel_set_options (ch, ntohl (msg->opt));
6201 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6202 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
6203 ch->root_rel->ch = ch;
6204 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
6206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6207 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6208 peer_connect (peer);
6210 /* Send create channel */
6212 struct GNUNET_MESH_ChannelCreate msgcc;
6214 msgcc.header.size = htons (sizeof (msgcc));
6215 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6216 msgcc.chid = htonl (ch->gid);
6217 msgcc.port = msg->port;
6218 msgcc.opt = msg->opt;
6220 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6223 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6229 * Handler for requests of deleting tunnels
6231 * @param cls closure
6232 * @param client identification of the client
6233 * @param message the actual message
6236 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6237 const struct GNUNET_MessageHeader *message)
6239 struct GNUNET_MESH_ChannelMessage *msg;
6240 struct MeshClient *c;
6241 struct MeshChannel *ch;
6242 struct MeshTunnel2 *t;
6243 MESH_ChannelNumber chid;
6245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6246 "Got a DESTROY CHANNEL from client!\n");
6248 /* Sanity check for client registration */
6249 if (NULL == (c = client_get (client)))
6252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6257 /* Message sanity check */
6258 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6265 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6267 /* Retrieve tunnel */
6268 chid = ntohl (msg->channel_id);
6269 ch = channel_get_by_local_id (c, chid);
6272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6278 /* Cleanup after the tunnel */
6279 client_delete_channel (c, ch);
6280 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6284 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6291 " channel %X client %p (%p, %p)\n",
6292 chid, c, ch->root, ch->dest);
6297 channel_destroy (ch);
6298 tunnel_destroy_if_empty (t);
6300 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6306 * Handler for client traffic
6308 * @param cls closure
6309 * @param client identification of the client
6310 * @param message the actual message
6313 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6314 const struct GNUNET_MessageHeader *message)
6316 struct GNUNET_MESH_LocalData *msg;
6317 struct MeshClient *c;
6318 struct MeshChannel *ch;
6319 struct MeshChannelReliability *rel;
6320 MESH_ChannelNumber chid;
6324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6325 "Got data from a client!\n");
6327 /* Sanity check for client registration */
6328 if (NULL == (c = client_get (client)))
6331 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6336 msg = (struct GNUNET_MESH_LocalData *) message;
6338 /* Sanity check for message size */
6339 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6340 if (size < sizeof (struct GNUNET_MessageHeader))
6343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6347 /* Channel exists? */
6348 chid = ntohl (msg->id);
6349 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6350 ch = channel_get_by_local_id (c, chid);
6354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6358 /* Is the client in the channel? */
6361 ch->root->handle == client)
6365 ch->dest->handle == client) ) )
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6372 rel = fwd ? ch->root_rel : ch->dest_rel;
6373 rel->client_ready = GNUNET_NO;
6375 /* Ok, everything is correct, send the message. */
6377 struct GNUNET_MESH_Data *payload;
6378 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6379 unsigned char cbuf[p2p_size];
6381 payload = (struct GNUNET_MESH_Data *) cbuf;
6382 payload->mid = htonl (rel->mid_send);
6384 memcpy (&payload[1], &msg[1], size);
6385 payload->header.size = htons (p2p_size);
6386 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6387 payload->chid = htonl (ch->gid);
6388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6389 send_prebuilt_message_channel (&payload->header, ch, fwd);
6391 if (GNUNET_YES == ch->reliable)
6392 channel_save_copy (ch, &payload->header, fwd);
6394 if (tunnel_get_buffer (ch->t, fwd) > 0)
6395 send_local_ack (ch, fwd);
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6404 * Handler for client's ACKs for payload traffic.
6406 * @param cls Closure (unused).
6407 * @param client Identification of the client.
6408 * @param message The actual message.
6411 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6412 const struct GNUNET_MessageHeader *message)
6414 struct GNUNET_MESH_LocalAck *msg;
6415 struct MeshChannelReliability *rel;
6416 struct MeshChannel *ch;
6417 struct MeshClient *c;
6418 MESH_ChannelNumber chid;
6421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6423 /* Sanity check for client registration */
6424 if (NULL == (c = client_get (client)))
6427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6432 msg = (struct GNUNET_MESH_LocalAck *) message;
6434 /* Channel exists? */
6435 chid = ntohl (msg->channel_id);
6436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6437 ch = channel_get_by_local_id (c, chid);
6438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6448 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
6449 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
6450 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6451 rel = fwd ? ch->dest_rel : ch->root_rel;
6453 rel->client_ready = GNUNET_YES;
6454 channel_send_client_buffered_data (ch, c, fwd);
6455 send_ack (NULL, ch, fwd);
6457 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6464 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6466 * @param cls Closure (client handle).
6467 * @param key Key (hashed tunnel ID, unused).
6468 * @param value Tunnel info.
6470 * @return GNUNET_YES, to keep iterating.
6473 // monitor_all_tunnels_iterator (void *cls,
6474 // const struct GNUNET_HashCode * key,
6477 // struct GNUNET_SERVER_Client *client = cls;
6478 // struct MeshChannel *ch = value;
6479 // struct GNUNET_MESH_LocalMonitor *msg;
6481 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6482 // msg->channel_id = htonl (ch->gid);
6483 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6484 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6486 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6487 // "* sending info about tunnel %s\n",
6488 // GNUNET_i2s (&msg->owner));
6490 // GNUNET_SERVER_notification_context_unicast (nc, client,
6491 // &msg->header, GNUNET_NO);
6492 // return GNUNET_YES;
6497 * Handler for client's MONITOR request.
6499 * @param cls Closure (unused).
6500 * @param client Identification of the client.
6501 * @param message The actual message.
6504 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6505 const struct GNUNET_MessageHeader *message)
6507 struct MeshClient *c;
6509 /* Sanity check for client registration */
6510 if (NULL == (c = client_get (client)))
6513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6517 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6518 "Received get tunnels request from client %u\n",
6520 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6521 // monitor_all_tunnels_iterator,
6523 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6524 "Get tunnels request from client %u completed\n",
6526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6531 * Handler for client's MONITOR_TUNNEL request.
6533 * @param cls Closure (unused).
6534 * @param client Identification of the client.
6535 * @param message The actual message.
6538 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6539 const struct GNUNET_MessageHeader *message)
6541 const struct GNUNET_MESH_LocalMonitor *msg;
6542 struct GNUNET_MESH_LocalMonitor *resp;
6543 struct MeshClient *c;
6544 struct MeshChannel *ch;
6546 /* Sanity check for client registration */
6547 if (NULL == (c = client_get (client)))
6550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6554 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6556 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6559 ntohl (msg->channel_id));
6560 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6564 /* We don't know the tunnel */
6565 struct GNUNET_MESH_LocalMonitor warn;
6568 GNUNET_SERVER_notification_context_unicast (nc, client,
6571 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6575 /* Initialize context */
6576 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6578 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6579 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6580 &resp->header, GNUNET_NO);
6583 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6584 "Monitor tunnel request from client %u completed\n",
6586 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6591 * Functions to handle messages from clients
6593 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6594 {&handle_local_new_client, NULL,
6595 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6596 {&handle_local_channel_create, NULL,
6597 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6598 sizeof (struct GNUNET_MESH_ChannelMessage)},
6599 {&handle_local_channel_destroy, NULL,
6600 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6601 sizeof (struct GNUNET_MESH_ChannelMessage)},
6602 {&handle_local_data, NULL,
6603 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6604 {&handle_local_ack, NULL,
6605 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6606 sizeof (struct GNUNET_MESH_LocalAck)},
6607 {&handle_local_get_tunnels, NULL,
6608 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6609 sizeof (struct GNUNET_MessageHeader)},
6610 {&handle_local_show_tunnel, NULL,
6611 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6612 sizeof (struct GNUNET_MESH_LocalMonitor)},
6618 * Method called whenever a given peer connects.
6620 * @param cls closure
6621 * @param peer peer identity this notification is about
6624 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6626 struct MeshPeer *pi;
6627 struct MeshPeerPath *path;
6629 DEBUG_CONN ("Peer connected\n");
6630 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6631 pi = peer_get (peer);
6634 DEBUG_CONN (" (self)\n");
6635 path = path_new (1);
6639 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6640 path = path_new (2);
6641 path->peers[1] = pi->id;
6642 GNUNET_PEER_change_rc (pi->id, 1);
6643 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6645 path->peers[0] = myid;
6646 GNUNET_PEER_change_rc (myid, 1);
6647 peer_add_path (pi, path, GNUNET_YES);
6649 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6655 * Method called whenever a peer disconnects.
6657 * @param cls closure
6658 * @param peer peer identity this notification is about
6661 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6663 struct MeshPeer *pi;
6665 DEBUG_CONN ("Peer disconnected\n");
6666 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6673 peer_remove_path (pi, myid, pi->id);
6675 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6678 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6679 pi->connections = NULL;
6682 DEBUG_CONN (" (self)\n");
6684 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6691 * Install server (service) handlers and start listening to clients.
6696 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6697 GNUNET_SERVER_connect_notify (server_handle,
6698 &handle_local_client_connect, NULL);
6699 GNUNET_SERVER_disconnect_notify (server_handle,
6700 &handle_local_client_disconnect, NULL);
6701 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6703 clients_head = NULL;
6704 clients_tail = NULL;
6706 GNUNET_SERVER_resume (server_handle);
6711 * To be called on core init/fail.
6713 * @param cls Closure (config)
6714 * @param identity the public identity of this peer
6717 core_init (void *cls,
6718 const struct GNUNET_PeerIdentity *identity)
6720 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6724 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6729 GNUNET_i2s (identity));
6730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6732 GNUNET_i2s (&my_full_id));
6733 GNUNET_CORE_disconnect (core_handle);
6734 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6735 NULL, /* Closure passed to MESH functions */
6736 &core_init, /* Call core_init once connected */
6737 &core_connect, /* Handle connects */
6738 &core_disconnect, /* remove peers on disconnects */
6739 NULL, /* Don't notify about all incoming messages */
6740 GNUNET_NO, /* For header only in notification */
6741 NULL, /* Don't notify about all outbound messages */
6742 GNUNET_NO, /* For header-only out notification */
6743 core_handlers); /* Register these handlers */
6752 /******************************************************************************/
6753 /************************ MAIN FUNCTIONS ****************************/
6754 /******************************************************************************/
6757 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6759 * @param cls closure
6760 * @param key current key code
6761 * @param value value in the hash map
6762 * @return GNUNET_YES if we should continue to iterate,
6766 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6768 struct MeshPeer *p = value;
6769 struct MeshTunnel2 *t = p->tunnel;
6778 * Task run during shutdown.
6784 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6788 if (core_handle != NULL)
6790 GNUNET_CORE_disconnect (core_handle);
6793 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6794 if (dht_handle != NULL)
6796 GNUNET_DHT_disconnect (dht_handle);
6801 GNUNET_SERVER_notification_context_destroy (nc);
6804 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6806 GNUNET_SCHEDULER_cancel (announce_id_task);
6807 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6814 * Process mesh requests.
6816 * @param cls closure
6817 * @param server the initialized server
6818 * @param c configuration to use
6821 run (void *cls, struct GNUNET_SERVER_Handle *server,
6822 const struct GNUNET_CONFIGURATION_Handle *c)
6825 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6828 server_handle = server;
6829 GNUNET_SERVER_suspend (server_handle);
6832 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6835 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6837 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6838 "mesh", "peer/privatekey");
6839 GNUNET_SCHEDULER_shutdown ();
6844 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6845 &refresh_connection_time))
6847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6849 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6850 "mesh", "refresh path time");
6851 GNUNET_SCHEDULER_shutdown ();
6856 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6861 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6862 "mesh", "id announce time");
6863 GNUNET_SCHEDULER_shutdown ();
6868 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6873 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6874 "mesh", "connect timeout");
6875 GNUNET_SCHEDULER_shutdown ();
6880 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6883 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6885 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6886 "mesh", "max msgs queue");
6887 GNUNET_SCHEDULER_shutdown ();
6892 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6897 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6898 "mesh", "max tunnels");
6899 GNUNET_SCHEDULER_shutdown ();
6904 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6907 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6909 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6910 "mesh", "default ttl", 64);
6915 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6918 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6919 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6920 "mesh", "max peers", 1000);
6925 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6933 "Mesh is running with drop mode enabled. "
6934 "This is NOT a good idea! "
6935 "Remove the DROP_PERCENT option from your configuration.\n");
6939 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6940 &dht_replication_level))
6942 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6944 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6945 "mesh", "dht replication level", 3);
6946 dht_replication_level = 3;
6949 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6950 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6951 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6953 dht_handle = GNUNET_DHT_connect (c, 64);
6954 if (NULL == dht_handle)
6958 stats = GNUNET_STATISTICS_create ("mesh", c);
6960 /* Scheduled the task to clean up when shutdown is called */
6961 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6963 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6964 GNUNET_free (keyfile);
6965 GNUNET_assert (NULL != pk);
6966 my_private_key = pk;
6967 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6968 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6969 &my_full_id.hashPubKey);
6970 myid = GNUNET_PEER_intern (&my_full_id);
6971 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6972 "Mesh for peer [%s] starting\n",
6973 GNUNET_i2s(&my_full_id));
6975 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6976 NULL, /* Closure passed to MESH functions */
6977 &core_init, /* Call core_init once connected */
6978 &core_connect, /* Handle connects */
6979 &core_disconnect, /* remove peers on disconnects */
6980 NULL, /* Don't notify about all incoming messages */
6981 GNUNET_NO, /* For header only in notification */
6982 NULL, /* Don't notify about all outbound messages */
6983 GNUNET_NO, /* For header-only out notification */
6984 core_handlers); /* Register these handlers */
6985 if (NULL == core_handle)
6988 GNUNET_SCHEDULER_shutdown ();
6991 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6997 * The main function for the mesh service.
6999 * @param argc number of arguments from the command line
7000 * @param argv command line arguments
7001 * @return 0 ok, 1 on error
7004 main (int argc, char *const *argv)
7009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7010 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7012 ret = (GNUNET_OK == r) ? 0 : 1;
7013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");