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
32 * - relay corking down to core
33 * - set ttl relative to path length
37 * - peer: other mesh instance. If there is direct connection it's a neighbor.
38 * - tunnel: encrypted connection to a peer, neighbor or not.
39 * - channel: connection between two clients, on the same or different peers.
40 * have properties like reliability.
41 * - path: series of directly connected peer from one peer to another.
42 * - connection: path which is being used in a tunnel.
46 #include "gnunet_crypto_lib.h"
48 #include "mesh_protocol_enc.h"
49 #include "mesh_path.h"
50 #include "block_mesh.h"
51 #include "gnunet_dht_service.h"
52 #include "gnunet_statistics_service.h"
54 #define MESH_BLOOM_SIZE 128
56 #define MESH_DEBUG_DHT GNUNET_NO
57 #define MESH_DEBUG_CONNECTION GNUNET_NO
58 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
60 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
61 GNUNET_TIME_UNIT_MINUTES,\
63 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
64 #define MESH_RETRANSMIT_MARGIN 4
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
103 * All the states a tunnel can be in.
108 * Uninitialized status, should never appear in operation.
113 * Path to the peer not known yet
115 MESH_TUNNEL_SEARCHING,
118 * Request sent, not yet answered.
123 * Peer connected and ready to accept data
128 * Peer connected previosly but not responding
130 MESH_TUNNEL_RECONNECTING
135 * All the states a connection can be in.
137 enum MeshConnectionState
140 * Uninitialized status, should never appear in operation.
145 * Connection created, waiting for ACK.
147 MESH_CONNECTION_SENT,
150 * Connection confirmed, ready to carry traffic..
152 MESH_CONNECTION_READY,
156 /******************************************************************************/
157 /************************ DATA STRUCTURES ****************************/
158 /******************************************************************************/
160 /** FWD declaration */
164 struct MeshConnection;
166 struct MeshChannelReliability;
170 * Struct containing info about a queued transmission to this peer
177 struct MeshPeerQueue *next;
182 struct MeshPeerQueue *prev;
185 * Peer this transmission is directed to.
187 struct MeshPeer *peer;
190 * Connection this message belongs to.
192 struct MeshConnection *c;
195 * Channel this message belongs to, if known.
197 struct MeshChannel *ch;
200 * Pointer to info stucture used as cls.
210 * Size of the message
217 * Struct to encapsulate all the Flow Control information to a peer to which
218 * we are directly connected (on a core level).
220 struct MeshFlowControl
223 * Connection this controls.
225 struct MeshConnection *c;
228 * Transmission queue to core DLL head
230 struct MeshPeerQueue *queue_head;
233 * Transmission queue to core DLL tail
235 struct MeshPeerQueue *queue_tail;
238 * How many messages are in the queue to this peer.
240 unsigned int queue_n;
243 * How many messages do we accept in the queue.
245 unsigned int queue_max;
248 * Handle for queued transmissions
250 struct GNUNET_CORE_TransmitHandle *core_transmit;
253 * ID of the last packet sent towards the peer.
255 uint32_t last_pid_sent;
258 * ID of the last packet received from the peer.
260 uint32_t last_pid_recv;
263 * Last ACK sent to the peer (peer can't send more than this PID).
265 uint32_t last_ack_sent;
268 * Last ACK sent towards the origin (for traffic towards leaf node).
270 uint32_t last_ack_recv;
273 * Task to poll the peer in case of a lost ACK causes stall.
275 GNUNET_SCHEDULER_TaskIdentifier poll_task;
278 * How frequently to poll for ACKs.
280 struct GNUNET_TIME_Relative poll_time;
285 * Struct containing all information regarding a given peer
295 * Last time we heard from this peer
297 struct GNUNET_TIME_Absolute last_contact;
300 * Paths to reach the peer, ordered by ascending hop count
302 struct MeshPeerPath *path_head;
305 * Paths to reach the peer, ordered by ascending hop count
307 struct MeshPeerPath *path_tail;
310 * Handle to stop the DHT search for paths to this peer
312 struct GNUNET_DHT_GetHandle *dhtget;
315 * Tunnel to this peer, if any.
317 struct MeshTunnel2 *tunnel;
323 * Info needed to retry a message in case it gets lost.
325 struct MeshReliableMessage
328 * Double linked list, FIFO style
330 struct MeshReliableMessage *next;
331 struct MeshReliableMessage *prev;
334 * Tunnel Reliability queue this message is in.
336 struct MeshChannelReliability *rel;
339 * ID of the message (ACK needed to free)
344 * When was this message issued (to calculate ACK delay)
346 struct GNUNET_TIME_Absolute timestamp;
348 /* struct GNUNET_MESH_Data with payload */
352 struct MeshChannelReliability
355 * Channel this is about.
357 struct MeshChannel *ch;
360 * DLL of messages sent and not yet ACK'd.
362 struct MeshReliableMessage *head_sent;
363 struct MeshReliableMessage *tail_sent;
376 * DLL of messages received out of order.
378 struct MeshReliableMessage *head_recv;
379 struct MeshReliableMessage *tail_recv;
387 * Task to resend/poll in case no ACK is received.
389 GNUNET_SCHEDULER_TaskIdentifier retry_task;
392 * Counter for exponential backoff.
394 struct GNUNET_TIME_Relative retry_timer;
397 * How long does it usually take to get an ACK.
399 struct GNUNET_TIME_Relative expected_delay;
404 * Struct containing all information regarding a channel to a remote client.
409 * Tunnel this channel is in.
411 struct MeshTunnel2 *t;
414 * Double linked list.
416 struct MeshChannel *next;
417 struct MeshChannel *prev;
420 * Destination port of the channel.
425 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
427 MESH_ChannelNumber gid;
430 * Local tunnel number for root (owner) client.
431 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
433 MESH_ChannelNumber lid_root;
436 * Local tunnel number for local destination clients (incoming number)
437 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
439 MESH_ChannelNumber lid_dest;
442 * Is the tunnel bufferless (minimum latency)?
447 * Is the tunnel reliable?
452 * Last time the channel was used
454 struct GNUNET_TIME_Absolute timestamp;
457 * Client owner of the tunnel, if any
459 struct MeshClient *root;
462 * Client destination of the tunnel, if any.
464 struct MeshClient *dest;
467 * Flag to signal the destruction of the channel.
468 * If this is set GNUNET_YES the channel will be destroyed
469 * when the queue is empty.
474 * Total messages pending for this channel, payload or not.
476 unsigned int pending_messages;
480 * Only present (non-NULL) at the owner of a tunnel.
482 struct MeshChannelReliability *fwd_rel;
486 * Only present (non-NULL) at the destination of a tunnel.
488 struct MeshChannelReliability *bck_rel;
492 struct MeshConnection
497 struct MeshConnection *next;
498 struct MeshConnection *prev;
501 * Tunnel this connection is part of.
503 struct MeshTunnel2 *t;
506 * Flow control information for traffic fwd.
508 struct MeshFlowControl *fwd_fc;
511 * Flow control information for traffic bck.
513 struct MeshFlowControl *bck_fc;
521 * State of the connection.
523 enum MeshConnectionState state;
526 * Path being used for the tunnel.
528 struct MeshPeerPath *path;
531 * Position of the local peer in the path.
533 unsigned int own_pos;
536 * Task to keep the used paths alive at the owner,
537 * time tunnel out on all the other peers.
539 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
542 * Task to keep the used paths alive at the destination,
543 * time tunnel out on all the other peers.
545 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
548 * Pending message count.
550 int pending_messages;
553 * Destroy flag: if true, destroy on last message.
560 * Struct containing all information regarding a tunnel to a peer.
565 * Endpoint of the tunnel.
567 struct MeshPeer *peer;
572 struct GNUNET_HashCode id;
575 * State of the tunnel.
577 enum MeshTunnelState state;
580 * Local peer ephemeral private key
582 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
585 * Local peer ephemeral public key
587 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
590 * Remote peer's public key.
592 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
595 * Encryption ("our") key.
597 struct GNUNET_CRYPTO_AesSessionKey e_key;
600 * Decryption ("their") key.
602 struct GNUNET_CRYPTO_AesSessionKey d_key;
605 * Paths that are actively used to reach the destination peer.
607 struct MeshConnection *connection_head;
608 struct MeshConnection *connection_tail;
611 * Next connection number.
616 * Channels inside this tunnel.
618 struct MeshChannel *channel_head;
619 struct MeshChannel *channel_tail;
622 * Channel ID for the next created tunnel.
624 MESH_ChannelNumber next_chid;
627 * Channel ID for the next incoming tunnel.
629 MESH_ChannelNumber next_local_chid;
632 * Pending message count.
634 int pending_messages;
637 * Destroy flag: if true, destroy on last message.
645 * Struct containing information about a client of the service
647 * TODO: add a list of 'waiting' ports
654 struct MeshClient *next;
659 struct MeshClient *prev;
662 * Tunnels that belong to this client, indexed by local id
664 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
667 * Tunnels this client has accepted, indexed by incoming local id
669 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
672 * Handle to communicate with the client
674 struct GNUNET_SERVER_Client *handle;
677 * Ports that this client has declared interest in.
678 * Indexed by port, contains *Client.
680 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
683 * Whether the client is active or shutting down (don't send confirmations
684 * to a client that is shutting down.
689 * ID of the client, mainly for debug messages
695 /******************************************************************************/
696 /************************ DEBUG FUNCTIONS ****************************/
697 /******************************************************************************/
701 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
702 * @param cls string to print
703 * @param success GNUNET_OK if the PUT was transmitted,
704 * GNUNET_NO on timeout,
705 * GNUNET_SYSERR on disconnect from service
706 * after the PUT message was transmitted
707 * (so we don't know if it was received or not)
712 mesh_debug (void *cls, int success)
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
722 /******************************************************************************/
723 /*********************** GLOBAL VARIABLES ****************************/
724 /******************************************************************************/
726 /************************** Configuration parameters **************************/
729 * How often to send path keepalives. Paths timeout after 4 missed.
731 static struct GNUNET_TIME_Relative refresh_connection_time;
734 * How often to PUT own ID in the DHT.
736 static struct GNUNET_TIME_Relative id_announce_time;
739 * Maximum time allowed to connect to a peer found by string.
741 static struct GNUNET_TIME_Relative connect_timeout;
744 * Default TTL for payload packets.
746 static unsigned long long default_ttl;
749 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
751 static unsigned long long dht_replication_level;
754 * How many tunnels are we willing to maintain.
755 * Local tunnels are always allowed, even if there are more tunnels than max.
757 static unsigned long long max_tunnels;
760 * How many messages *in total* are we willing to queue, divided by number of
761 * tunnels to get tunnel queue size.
763 static unsigned long long max_msgs_queue;
766 * How many peers do we want to remember?
768 static unsigned long long max_peers;
771 * Percentage of messages that will be dropped (for test purposes only).
773 static unsigned long long drop_percent;
775 /*************************** Static global variables **************************/
778 * DLL with all the clients, head.
780 static struct MeshClient *clients_head;
783 * DLL with all the clients, tail.
785 static struct MeshClient *clients_tail;
788 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
790 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
793 * Peers known, indexed by PeerIdentity (MeshPeer).
795 static struct GNUNET_CONTAINER_MultiHashMap *peers;
798 * Handle to communicate with core.
800 static struct GNUNET_CORE_Handle *core_handle;
805 static struct GNUNET_DHT_Handle *dht_handle;
808 * Handle to server lib.
810 static struct GNUNET_SERVER_Handle *server_handle;
813 * Handle to the statistics service.
815 static struct GNUNET_STATISTICS_Handle *stats;
818 * Notification context, to send messages to local clients.
820 static struct GNUNET_SERVER_NotificationContext *nc;
823 * Local peer own ID (memory efficient handle).
825 static GNUNET_PEER_Id myid;
828 * Local peer own ID (full value).
830 static struct GNUNET_PeerIdentity my_full_id;
835 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
840 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
843 * All ports clients of this peer have opened.
845 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
848 * Task to periodically announce itself in the network.
850 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
853 * Next ID to assign to a client.
855 unsigned int next_client_id;
858 /******************************************************************************/
859 /*********************** DECLARATIONS **************************/
860 /******************************************************************************/
863 * Function to process paths received for a new peer addition. The recorded
864 * paths form the initial tunnel, which can be optimized later.
865 * Called on each result obtained for the DHT search.
868 * @param exp when will this value expire
869 * @param key key of the result
870 * @param type type of the result
871 * @param size number of bytes in data
872 * @param data pointer to the result data
875 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
876 const struct GNUNET_HashCode * key,
877 const struct GNUNET_PeerIdentity *get_path,
878 unsigned int get_path_length,
879 const struct GNUNET_PeerIdentity *put_path,
880 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
881 size_t size, const void *data);
885 * Retrieve the MeshPeer stucture associated with the peer, create one
886 * and insert it in the appropriate structures if the peer is not known yet.
888 * @param peer Full identity of the peer.
890 * @return Existing or newly created peer info.
892 static struct MeshPeer *
893 peer_get (const struct GNUNET_PeerIdentity *peer);
897 * Retrieve the MeshPeer stucture associated with the peer, create one
898 * and insert it in the appropriate structures if the peer is not known yet.
900 * @param peer Short identity of the peer.
902 * @return Existing or newly created peer info.
904 static struct MeshPeer *
905 peer_get_short (const GNUNET_PEER_Id peer);
909 * Build a PeerPath from the paths returned from the DHT, reversing the paths
910 * to obtain a local peer -> destination path and interning the peer ids.
912 * @return Newly allocated and created path
914 static struct MeshPeerPath *
915 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
916 unsigned int get_path_length,
917 const struct GNUNET_PeerIdentity *put_path,
918 unsigned int put_path_length);
922 * Adds a path to the data structs of all the peers in the path
924 * @param p Path to process.
925 * @param confirmed Whether we know if the path works or not.
928 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
932 * Search for a tunnel by global ID using full PeerIdentities.
934 * @param t Tunnel containing the channel.
935 * @param chid Public channel number.
937 * @return channel handler, NULL if doesn't exist
939 static struct MeshChannel *
940 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
944 * Change the tunnel state.
946 * @param t Tunnel whose state to change.
947 * @param state New state.
950 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
954 * Notify a tunnel that a connection has broken that affects at least
957 * @param t Tunnel affected.
958 * @param p1 Peer that got disconnected from p2.
959 * @param p2 Peer that got disconnected from p1.
961 * @return Short ID of the peer disconnected (either p1 or p2).
962 * 0 if the tunnel remained unaffected.
964 static GNUNET_PEER_Id
965 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
966 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
969 * @brief Use the given path for the tunnel.
970 * Update the next and prev hops (and RCs).
971 * (Re)start the path refresh in case the tunnel is locally owned.
973 * @param t Tunnel to update.
974 * @param p Path to use.
976 * @return Connection created.
978 static struct MeshConnection *
979 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
982 * Tunnel is empty: destroy it.
984 * Notifies all participants (peers, cleints) about the destruction.
986 * @param t Tunnel to destroy.
989 tunnel_destroy_empty (struct MeshTunnel2 *t);
992 * Destroy the tunnel.
994 * This function does not generate any warning traffic to clients or peers.
997 * Cancel messages belonging to this tunnel queued to neighbors.
998 * Free any allocated resources linked to the tunnel.
1000 * @param t The tunnel to destroy.
1003 tunnel_destroy (struct MeshTunnel2 *t);
1006 * Send FWD keepalive packets for a connection.
1008 * @param cls Closure (connection for which to send the keepalive).
1009 * @param tc Notification context.
1012 connection_fwd_keepalive (void *cls,
1013 const struct GNUNET_SCHEDULER_TaskContext *tc);
1016 * Send BCK keepalive packets for a connection.
1018 * @param cls Closure (connection for which to send the keepalive).
1019 * @param tc Notification context.
1022 connection_bck_keepalive (void *cls,
1023 const struct GNUNET_SCHEDULER_TaskContext *tc);
1027 * Change the tunnel state.
1029 * @param c Connection whose state to change.
1030 * @param state New state.
1033 connection_change_state (struct MeshConnection* c,
1034 enum MeshConnectionState state);
1039 * @brief Queue and pass message to core when possible.
1041 * @param cls Closure (@c type dependant). It will be used by queue_send to
1042 * build the message to be sent if not already prebuilt.
1043 * @param type Type of the message, 0 for a raw message.
1044 * @param size Size of the message.
1045 * @param dst Neighbor to send message to.
1046 * @param c Connection this message belongs to, if any.
1047 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1050 queue_add (void *cls, uint16_t type, size_t size,
1051 struct MeshPeer *dst,
1052 struct MeshConnection *c,
1053 struct MeshChannel *ch);
1057 * Free a transmission that was already queued with all resources
1058 * associated to the request.
1060 * @param queue Queue handler to cancel.
1061 * @param clear_cls Is it necessary to free associated cls?
1064 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1068 * Core callback to write a queued packet to core buffer
1070 * @param cls Closure (peer info).
1071 * @param size Number of bytes available in buf.
1072 * @param buf Where the to write the message.
1074 * @return number of bytes written to buf
1077 queue_send (void *cls, size_t size, void *buf);
1081 * Dummy function to separate declarations from definitions in function list.
1084 __mesh_divider______________________________________________________________();
1088 * Get string description for tunnel state.
1090 * @param s Tunnel state.
1092 * @return String representation.
1095 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1097 static char buf[128];
1101 case MESH_TUNNEL_NEW:
1102 return "MESH_TUNNEL_NEW";
1103 case MESH_TUNNEL_SEARCHING:
1104 return "MESH_TUNNEL_SEARCHING";
1105 case MESH_TUNNEL_WAITING:
1106 return "MESH_TUNNEL_WAITING";
1107 case MESH_TUNNEL_READY:
1108 return "MESH_TUNNEL_READY";
1109 case MESH_TUNNEL_RECONNECTING:
1110 return "MESH_TUNNEL_RECONNECTING";
1113 sprintf (buf, "%u (UNKNOWN STATE)", s);
1120 * Get string description for tunnel state.
1122 * @param s Tunnel state.
1124 * @return String representation.
1127 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1131 case MESH_CONNECTION_NEW:
1132 return "MESH_CONNECTION_NEW";
1133 case MESH_CONNECTION_SENT:
1134 return "MESH_CONNECTION_SENT";
1135 case MESH_CONNECTION_READY:
1136 return "MESH_CONNECTION_READY";
1138 return "MESH_CONNECTION_STATE_ERROR";
1144 /******************************************************************************/
1145 /************************ PERIODIC FUNCTIONS ****************************/
1146 /******************************************************************************/
1149 * Periodically announce self id in the DHT
1151 * @param cls closure
1152 * @param tc task context
1155 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1157 struct PBlock block;
1159 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1161 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1165 * - Set data expiration in function of X
1166 * - Adapt X to churn
1168 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1170 block.id = my_full_id;
1171 GNUNET_DHT_put (dht_handle, /* DHT handle */
1172 &my_full_id.hashPubKey, /* Key to use */
1173 dht_replication_level, /* Replication level */
1174 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1175 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1176 sizeof (block), /* Size of the data */
1177 (const char *) &block, /* Data itself */
1178 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1179 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1180 NULL, /* Continuation */
1181 NULL); /* Continuation closure */
1183 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1187 /******************************************************************************/
1188 /****************** GENERAL HELPER FUNCTIONS ************************/
1189 /******************************************************************************/
1193 * Get the previous hop in a connection
1195 * @param c Connection.
1197 * @return Previous peer in the connection.
1199 static struct MeshPeer *
1200 connection_get_prev_hop (struct MeshConnection *c)
1204 if (0 == c->own_pos || c->path->length < 2)
1205 id = c->path->peers[0];
1207 id = c->path->peers[c->own_pos - 1];
1209 return peer_get_short (id);
1214 * Get the next hop in a connection
1216 * @param c Connection.
1218 * @return Next peer in the connection.
1220 static struct MeshPeer *
1221 connection_get_next_hop (struct MeshConnection *c)
1225 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1226 id = c->path->peers[c->path->length - 1];
1228 id = c->path->peers[c->own_pos + 1];
1230 return peer_get_short (id);
1235 * Check if client has registered with the service and has not disconnected
1237 * @param client the client to check
1239 * @return non-NULL if client exists in the global DLL
1241 static struct MeshClient *
1242 client_get (struct GNUNET_SERVER_Client *client)
1244 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1249 * Deletes a tunnel from a client (either owner or destination). To be used on
1252 * @param c Client whose tunnel to delete.
1253 * @param ch Channel which should be deleted.
1256 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1262 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1264 if (GNUNET_YES != res)
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1269 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1271 if (GNUNET_YES != res)
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1278 * Notify the appropriate client that a new incoming channel was created.
1280 * @param ch Channel that was created.
1283 send_local_channel_create (struct MeshChannel *ch)
1285 struct GNUNET_MESH_ChannelMessage msg;
1286 struct MeshTunnel2 *t = ch->t;
1288 if (NULL == ch->dest)
1290 msg.header.size = htons (sizeof (msg));
1291 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1292 msg.channel_id = htonl (ch->lid_dest);
1293 msg.port = htonl (ch->port);
1295 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1296 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1297 msg.opt = htonl (msg.opt);
1298 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1299 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1300 &msg.header, GNUNET_NO);
1305 * Notify a client that the incoming tunnel is no longer valid.
1307 * @param ch Channel that is destroyed.
1308 * @param fwd Forward notification (owner->dest)?
1311 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1313 struct GNUNET_MESH_ChannelMessage msg;
1314 struct MeshClient *c;
1316 c = fwd ? ch->dest : ch->root;
1322 msg.header.size = htons (sizeof (msg));
1323 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1324 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1325 msg.port = htonl (0);
1326 memset (&msg.peer, 0, sizeof (msg.peer));
1327 msg.opt = htonl (0);
1328 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1329 &msg.header, GNUNET_NO);
1334 * Build a local ACK message and send it to a local client.
1336 * @param ch Channel on which to send the ACK.
1337 * @param c Client to whom send the ACK.
1338 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1341 send_local_ack (struct MeshChannel *ch,
1342 struct MeshClient *c,
1345 struct GNUNET_MESH_LocalAck msg;
1347 msg.header.size = htons (sizeof (msg));
1348 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1349 msg.channel_id = htonl (is_fwd ? ch->lid_root : ch->lid_dest);
1350 GNUNET_SERVER_notification_context_unicast (nc,
1358 * Count established (ready) connections of a tunnel.
1360 * @param t Tunnel on which to send the message.
1362 * @return Number of connections.
1365 tunnel_count_connections (struct MeshTunnel2 *t)
1367 struct MeshConnection *c;
1370 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1377 * Pick a connection on which send the next data message.
1379 * @param t Tunnel on which to send the message.
1380 * @param fwd Is this a fwd message?
1382 * @return The connection on which to send the next message.
1384 static struct MeshConnection *
1385 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1387 struct MeshConnection *c;
1388 struct MeshConnection *best;
1389 struct MeshFlowControl *fc;
1390 unsigned int lowest_q;
1393 lowest_q = UINT_MAX;
1394 for (c = t->connection_head; NULL != c; c = c->next)
1396 if (MESH_CONNECTION_READY == c->state)
1398 fc = fwd ? c->fwd_fc : c->bck_fc;
1404 if (fc->queue_n < lowest_q)
1407 lowest_q = fc->queue_n;
1416 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1417 * Encrypt data with the tunnel key.
1419 * @param t Tunnel whose key to use.
1420 * @param dst Destination for the encrypted data.
1421 * @param src Source of the plaintext.
1422 * @param size Size of the plaintext.
1423 * @param iv Initialization Vector to use.
1424 * @param fwd Is this a fwd message?
1427 tunnel_encrypt (struct MeshTunnel2 *t,
1428 void *dst, const void *src,
1429 size_t size, uint64_t iv, int fwd)
1431 memcpy (dst, src, size);
1436 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1437 * Decrypt data with the tunnel key.
1439 * @param t Tunnel whose key to use.
1440 * @param dst Destination for the plaintext.
1441 * @param src Source of the encrypted data.
1442 * @param size Size of the encrypted data.
1443 * @param iv Initialization Vector to use.
1444 * @param fwd Is this a fwd message?
1447 tunnel_decrypt (struct MeshTunnel2 *t,
1448 void *dst, const void *src,
1449 size_t size, uint64_t iv, int fwd)
1451 memcpy (dst, src, size);
1456 * Sends an already built message on a connection, properly registering
1457 * all used resources.
1459 * @param message Message to send. Function makes a copy of it.
1460 * If message is not hop-by-hop, decrements TTL of copy.
1461 * @param c Connection on which this message is transmitted.
1462 * @param ch Channel on which this message is transmitted, or NULL.
1463 * @param fwd Is this a fwd message?
1466 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1467 struct MeshConnection *c,
1468 struct MeshChannel *ch,
1471 struct MeshPeer *neighbor;
1476 neighbor = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1477 if (NULL == neighbor)
1483 size = ntohs (message->size);
1484 data = GNUNET_malloc (size);
1485 memcpy (data, message, size);
1486 type = ntohs(message->type);
1488 if (GNUNET_MESSAGE_TYPE_MESH_FWD == type ||
1489 GNUNET_MESSAGE_TYPE_MESH_BCK == type)
1491 struct GNUNET_MESH_Encrypted *msg;
1494 msg = (struct GNUNET_MESH_Encrypted *) data;
1495 ttl = ntohl (msg->ttl);
1498 GNUNET_break_op (0);
1501 msg->ttl = htonl (ttl - 1);
1514 * Sends an already built message on a tunnel, choosing the best connection.
1516 * @param message Message to send. Function modifies it.
1517 * @param t Tunnel on which this message is transmitted.
1518 * @param ch Channel on which this message is transmitted.
1519 * @param fwd Is this a fwd message?
1522 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1523 struct MeshTunnel2 *t,
1524 struct MeshChannel *ch,
1527 struct MeshConnection *c;
1530 c = tunnel_get_connection (t, fwd);
1536 type = ntohs (msg->header.size);
1539 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1540 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1541 msg->cid = htonl (c->id);
1543 msg->ttl = default_ttl;
1549 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1554 * Sends an already built message on a channel, properly registering
1555 * all used resources and encrypting the message with the tunnel's key.
1557 * @param message Message to send. Function makes a copy of it.
1558 * @param ch Channel on which this message is transmitted.
1559 * @param fwd Is this a fwd message?
1562 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1563 struct MeshChannel *ch,
1566 struct GNUNET_MESH_Encrypted *msg;
1567 size_t size = ntohs (message->size);
1568 char *cbuf[size + sizeof (struct GNUNET_MESH_Encrypted)];
1572 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1573 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1575 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1576 msg->header.type = htons (type);
1577 msg->header.size = htons (size);
1578 msg->iv = GNUNET_htonll (iv);
1579 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1581 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1586 * Sends a CREATE CONNECTION message for a path to a peer.
1587 * Changes the connection and tunnel states if necessary.
1589 * @param connection Connection to create.
1592 send_connection_create (struct MeshConnection *connection)
1594 struct MeshPeer *neighbor;
1595 struct MeshTunnel2 *t;
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1599 neighbor = connection_get_next_hop (connection);
1600 queue_add (connection,
1601 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1602 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1603 (connection->path->length *
1604 sizeof (struct GNUNET_PeerIdentity)),
1608 if (MESH_TUNNEL_SEARCHING == t->state)
1609 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1610 if (MESH_CONNECTION_NEW == connection->state)
1611 connection_change_state (connection, MESH_CONNECTION_SENT);
1616 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1619 * @param connection Connection to confirm.
1622 send_connection_ack (struct MeshConnection *connection)
1624 struct MeshPeer *neighbor;
1625 struct MeshTunnel2 *t;
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1629 neighbor = connection_get_prev_hop (connection);
1630 queue_add (connection,
1631 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1632 sizeof (struct GNUNET_MESH_ConnectionACK),
1636 if (MESH_TUNNEL_NEW == t->state)
1637 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1642 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1644 * @param c Which connection to send the hop-by-hop ACK.
1645 * @param ack Value of the ACK.
1646 * @param fwd Is this fwd?
1649 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1651 struct GNUNET_MESH_ACK msg;
1653 msg.header.size = htons (sizeof (msg));
1654 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1655 msg.ack = htonl (ack);
1657 msg.cid = htonl (c->id);
1659 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1664 * Core callback to write a pre-constructed data packet to core buffer
1666 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1667 * @param size Number of bytes available in buf.
1668 * @param buf Where the to write the message.
1670 * @return number of bytes written to buf
1673 send_core_data_raw (void *cls, size_t size, void *buf)
1675 struct GNUNET_MessageHeader *msg = cls;
1678 GNUNET_assert (NULL != msg);
1679 total_size = ntohs (msg->size);
1681 if (total_size > size)
1686 memcpy (buf, msg, total_size);
1693 * Function to send a create path packet to a peer.
1695 * @param cls closure
1696 * @param size number of bytes available in buf
1697 * @param buf where the callee should write the message
1698 * @return number of bytes written to buf
1701 send_core_connection_create (void *cls, size_t size, void *buf)
1703 struct MeshConnection *c = cls;
1704 struct GNUNET_MESH_ConnectionCreate *msg;
1705 struct GNUNET_PeerIdentity *peer_ptr;
1706 struct MeshPeerPath *p = c->path;
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1712 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1713 p->length * sizeof (struct GNUNET_PeerIdentity);
1715 if (size < size_needed || NULL == buf)
1720 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1721 msg->header.size = htons (size_needed);
1722 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1723 msg->cid = htonl (c->id);
1725 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1726 for (i = 0; i < p->length; i++)
1728 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1732 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1738 * Creates a path ack message in buf and frees all unused resources.
1740 * @param cls closure (MeshTransmissionDescriptor)
1741 * @param size number of bytes available in buf
1742 * @param buf where the callee should write the message
1743 * @return number of bytes written to buf
1746 send_core_connection_ack (void *cls, size_t size, void *buf)
1748 struct GNUNET_MESH_ConnectionACK *msg = buf;
1749 struct MeshConnection *c = cls;
1750 struct MeshTunnel2 *t = c->t;
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1753 GNUNET_assert (NULL != t);
1754 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1759 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1760 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1761 GNUNET_CRYPTO_hash_xor (&GNUNET_PEER_resolve2 (t->peer->id)->hashPubKey,
1762 &my_full_id.hashPubKey,
1764 msg->cid = htonl (c->id);
1766 /* TODO add signature */
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1769 return sizeof (struct GNUNET_MESH_ConnectionACK);
1774 * Iterator over all the peers to remove the oldest not-used entry.
1776 * @param cls Closure (unsued).
1777 * @param key ID of the peer.
1778 * @param value Peer_Info of the peer.
1783 peer_timeout (void *cls,
1784 const struct GNUNET_HashCode *key,
1792 * Retrieve the MeshPeer stucture associated with the peer, create one
1793 * and insert it in the appropriate structures if the peer is not known yet.
1795 * @param peer Full identity of the peer.
1797 * @return Existing or newly created peer info.
1799 static struct MeshPeer *
1800 peer_get (const struct GNUNET_PeerIdentity *peer_id)
1802 struct MeshPeer *peer;
1804 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
1807 peer = GNUNET_new (struct MeshPeer);
1808 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1810 GNUNET_CONTAINER_multihashmap_iterate (peers,
1814 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
1815 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1816 peer->id = GNUNET_PEER_intern (peer_id);
1818 peer->last_contact = GNUNET_TIME_absolute_get();
1825 * Retrieve the MeshPeer stucture associated with the peer, create one
1826 * and insert it in the appropriate structures if the peer is not known yet.
1828 * @param peer Short identity of the peer.
1830 * @return Existing or newly created peer info.
1832 static struct MeshPeer *
1833 peer_get_short (const GNUNET_PEER_Id peer)
1835 return peer_get (GNUNET_PEER_resolve2 (peer));
1840 * Get a cost of a path for a peer considering existing tunnel connections.
1842 * @param peer Peer towards which the path is considered.
1843 * @param path Candidate path.
1845 * @return Cost of the path (path length + number of overlapping nodes)
1848 peer_get_path_cost (const struct MeshPeer *peer,
1849 const struct MeshPeerPath *path)
1851 struct MeshConnection *c;
1852 unsigned int overlap;
1860 GNUNET_assert (NULL != peer->tunnel);
1862 for (i = 0; i < path->length; i++)
1864 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
1866 for (j = 0; j < c->path->length; j++)
1868 if (path->peers[i] == c->path->peers[j])
1876 return (path->length + overlap) * (path->score * -1);
1881 * Choose the best path towards a peer considering the tunnel properties.
1883 * @param peer The destination peer.
1885 * @return Best current known path towards the peer, if any.
1887 static struct MeshPeerPath *
1888 peer_get_best_path (const struct MeshPeer *peer)
1890 struct MeshPeerPath *best_p;
1891 struct MeshPeerPath *p;
1892 struct MeshConnection *c;
1893 unsigned int best_cost;
1896 best_cost = UINT_MAX;
1898 for (p = peer->path_head; NULL != p; p = p->next)
1900 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
1904 continue; /* If path is in use in a connection, skip it. */
1906 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
1917 * Try to establish a new connection to this peer in the given tunnel.
1918 * If the peer doesn't have any path to it yet, try to get one.
1919 * If the peer already has some path, send a CREATE CONNECTION towards it.
1921 * @param peer PeerInfo of the peer.
1924 peer_connect (struct MeshPeer *peer)
1926 struct MeshTunnel2 *t;
1927 struct MeshPeerPath *p;
1928 struct MeshConnection *c;
1931 if (NULL != peer->path_head)
1933 p = peer_get_best_path (peer);
1936 c = tunnel_use_path (t, p);
1937 send_connection_create (c);
1940 else if (NULL == peer->dhtget)
1942 const struct GNUNET_PeerIdentity *id;
1944 id = GNUNET_PEER_resolve2 (peer->id);
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1946 " Starting DHT GET for peer %s\n", GNUNET_i2s (id));
1947 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1948 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1949 &id->hashPubKey, /* key to search */
1950 dht_replication_level, /* replication level */
1951 GNUNET_DHT_RO_RECORD_ROUTE |
1952 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1954 0, /* xquery bits */
1955 &dht_get_id_handler, peer);
1956 if (MESH_TUNNEL_NEW == t->state)
1957 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "There is no path but the DHT GET is already started.\n");
1968 * @brief Re-initiate traffic on this connection if necessary.
1970 * Check if there is traffic queued towards this peer
1971 * and the core transmit handle is NULL (traffic was stalled).
1972 * If so, call core tmt rdy.
1974 * @param c Connection on which initiate traffic.
1975 * @param fwd Is this about fwd traffic?
1978 connection_unlock_queue (struct MeshConnection *c, int fwd)
1980 struct MeshFlowControl *fc;
1981 struct MeshPeer *peer;
1982 struct MeshPeerQueue *q;
1985 peer = fwd ? connection_get_next_hop(c) : connection_get_prev_hop(c);
1986 fc = fwd ? c->fwd_fc : c->bck_fc;
1988 if (NULL != fc->core_transmit)
1989 return; /* Already unlocked */
1993 return; /* Nothing to transmit */
1997 GNUNET_CORE_notify_transmit_ready (core_handle,
2000 GNUNET_TIME_UNIT_FOREVER_REL,
2001 GNUNET_PEER_resolve2 (peer->id),
2009 * Cancel all transmissions that belong to a certain connection.
2011 * @param c Connection which to cancel.
2012 * @param fwd Cancel fwd traffic?
2015 connection_cancel_queues (struct MeshConnection *c, int fwd)
2017 struct MeshPeerQueue *q;
2018 struct MeshPeerQueue *next;
2019 struct MeshFlowControl *fc;
2026 fc = fwd ? c->fwd_fc : c->bck_fc;
2027 for (q = fc->queue_head; NULL != q; q = next)
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2033 "connection_cancel_queue %s\n",
2034 GNUNET_MESH_DEBUG_M2S (q->type));
2035 queue_destroy (q, GNUNET_YES);
2038 if (NULL == fc->queue_head)
2040 if (NULL != fc->core_transmit)
2042 GNUNET_CORE_notify_transmit_ready_cancel (fc->core_transmit);
2043 fc->core_transmit = NULL;
2045 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2047 GNUNET_SCHEDULER_cancel (fc->poll_task);
2048 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2055 * Destroy the peer_info and free any allocated resources linked to it
2057 * @param peer The peer_info to destroy.
2059 * @return GNUNET_OK on success
2062 peer_destroy (struct MeshPeer *peer)
2064 struct GNUNET_PeerIdentity id;
2065 struct MeshPeerPath *p;
2066 struct MeshPeerPath *nextp;
2068 GNUNET_PEER_resolve (peer->id, &id);
2069 GNUNET_PEER_change_rc (peer->id, -1);
2072 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2075 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2076 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2078 if (NULL != peer->dhtget)
2080 GNUNET_DHT_get_stop (peer->dhtget);
2082 p = peer->path_head;
2086 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2090 tunnel_destroy_empty (peer->tunnel);
2097 * Remove all paths that rely on a direct connection between p1 and p2
2098 * from the peer itself and notify all tunnels about it.
2100 * @param peer PeerInfo of affected peer.
2101 * @param p1 GNUNET_PEER_Id of one peer.
2102 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2105 * TODO: optimize (see below)
2108 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2111 struct MeshPeerPath *p;
2112 struct MeshPeerPath *next;
2113 struct MeshPeer *peer_d;
2115 unsigned int destroyed;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2120 for (p = peer->path_head; NULL != p; p = next)
2123 for (i = 0; i < (p->length - 1); i++)
2125 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2126 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2128 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2139 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2141 peer_d = peer_get_short (d); // FIXME
2142 next = peer_get_best_path (peer_d);
2143 tunnel_use_path (peer->tunnel, next);
2144 peer_connect (peer_d);
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2151 * Add the path to the peer and update the path used to reach it in case this
2154 * @param peer_info Destination peer to add the path to.
2155 * @param path New path to add. Last peer must be the peer in arg 1.
2156 * Path will be either used of freed if already known.
2157 * @param trusted Do we trust that this path is real?
2160 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2163 struct MeshPeerPath *aux;
2167 if ((NULL == peer_info) || (NULL == path))
2170 path_destroy (path);
2173 if (path->peers[path->length - 1] != peer_info->id)
2176 path_destroy (path);
2179 if (2 >= path->length && GNUNET_NO == trusted)
2181 /* Only allow CORE to tell us about direct paths */
2182 path_destroy (path);
2185 for (l = 1; l < path->length; l++)
2187 if (path->peers[l] == myid)
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2190 for (l2 = 0; l2 < path->length - l; l2++)
2192 path->peers[l2] = path->peers[l + l2];
2197 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2202 struct GNUNET_PeerIdentity id;
2204 GNUNET_PEER_resolve (peer_info->id, &id);
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2206 path->length, GNUNET_i2s (&id));
2209 l = path_get_length (path);
2212 path_destroy (path);
2216 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2217 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2219 l2 = path_get_length (aux);
2222 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2223 peer_info->path_tail, aux, path);
2228 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2230 path_destroy (path);
2235 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2242 * Add the path to the origin peer and update the path used to reach it in case
2243 * this is the shortest.
2244 * The path is given in peer_info -> destination, therefore we turn the path
2245 * upside down first.
2247 * @param peer_info Peer to add the path to, being the origin of the path.
2248 * @param path New path to add after being inversed.
2249 * Path will be either used or freed.
2250 * @param trusted Do we trust that this path is real?
2253 peer_add_path_to_origin (struct MeshPeer *peer_info,
2254 struct MeshPeerPath *path, int trusted)
2257 peer_add_path (peer_info, path, trusted);
2263 * Function called if a connection has been stalled for a while,
2264 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2266 * @param cls Closure (poll ctx).
2267 * @param tc TaskContext.
2270 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2272 struct MeshFlowControl *fc = cls;
2273 struct GNUNET_MESH_Poll msg;
2274 struct MeshConnection *c;
2276 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2277 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2285 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id);
2287 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2288 msg.header.size = htons (sizeof (msg));
2289 msg.pid = htonl (fc->last_pid_sent);
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2291 send_prebuilt_message_connection (&msg.header, c, NULL, fc == c->fwd_fc);
2292 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2293 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2294 &connection_poll, fc);
2299 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2300 * to obtain a local peer -> destination path and interning the peer ids.
2302 * @return Newly allocated and created path
2304 static struct MeshPeerPath *
2305 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2306 unsigned int get_path_length,
2307 const struct GNUNET_PeerIdentity *put_path,
2308 unsigned int put_path_length)
2310 struct MeshPeerPath *p;
2316 GNUNET_PEER_change_rc (myid, 1);
2317 i = get_path_length;
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2319 for (i--; i >= 0; i--)
2321 id = GNUNET_PEER_intern (&get_path[i]);
2322 if (p->length > 0 && id == p->peers[p->length - 1])
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2325 GNUNET_PEER_change_rc (id, -1);
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2330 GNUNET_i2s (&get_path[i]));
2332 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2333 p->peers[p->length - 1] = id;
2336 i = put_path_length;
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2338 for (i--; i >= 0; i--)
2340 id = GNUNET_PEER_intern (&put_path[i]);
2343 /* PUT path went through us, so discard the path up until now and start
2344 * from here to get a much shorter (and loop-free) path.
2349 if (p->length > 0 && id == p->peers[p->length - 1])
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2352 GNUNET_PEER_change_rc (id, -1);
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2357 GNUNET_i2s (&put_path[i]));
2359 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2360 p->peers[p->length - 1] = id;
2364 if (get_path_length > 0)
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2366 GNUNET_i2s (&get_path[0]));
2367 if (put_path_length > 0)
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2369 GNUNET_i2s (&put_path[0]));
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2372 for (i = 0; i < p->length; i++)
2374 struct GNUNET_PeerIdentity peer_id;
2376 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2378 GNUNET_i2s (&peer_id));
2386 * Adds a path to the peer_infos of all the peers in the path
2388 * @param p Path to process.
2389 * @param confirmed Whether we know if the path works or not.
2392 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2396 /* TODO: invert and add */
2397 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2398 for (i++; i < p->length; i++)
2400 struct MeshPeer *aux;
2401 struct MeshPeerPath *copy;
2403 aux = peer_get_short (p->peers[i]);
2404 copy = path_duplicate (p);
2405 copy->length = i + 1;
2406 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2412 * Search for a channel among the channels for a client
2414 * @param c the client whose channels to search in
2415 * @param chid the local id of the channel
2417 * @return channel handler, NULL if doesn't exist
2419 static struct MeshChannel *
2420 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2422 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2424 GNUNET_break_op (0);
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2428 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2429 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2430 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2434 * Search for a tunnel by global ID using full PeerIdentities.
2436 * @param t Tunnel containing the channel.
2437 * @param chid Public channel number.
2439 * @return channel handler, NULL if doesn't exist
2441 static struct MeshChannel *
2442 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2444 struct MeshChannel *ch;
2449 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2451 if (ch->gid == chid)
2460 * Change the tunnel state.
2462 * @param t Tunnel whose state to change.
2463 * @param state New state.
2466 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2469 "Tunnel %s state was %s\n",
2470 GNUNET_i2s (GNUNET_PEER_resolve2 (t->peer->id)),
2471 GNUNET_MESH_DEBUG_TS2S (t->state));
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2473 "Tunnel %s state is now %s\n",
2474 GNUNET_i2s (GNUNET_PEER_resolve2 (t->peer->id)),
2475 GNUNET_MESH_DEBUG_TS2S (state));
2481 connection_change_state (struct MeshConnection* c,
2482 enum MeshConnectionState state)
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 "Connection %s[%X] state was %s\n",
2486 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id,
2487 GNUNET_MESH_DEBUG_CS2S (c->state));
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Connection %s[%X] state is now %s\n",
2490 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id,
2491 GNUNET_MESH_DEBUG_CS2S (state));
2497 * Add a client to a channel, initializing all needed data structures.
2499 * @param ch Channel to which add the client.
2500 * @param c Client which to add to the channel.
2503 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2505 if (NULL != ch->dest)
2511 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2513 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2522 static struct MeshConnection *
2523 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2525 struct MeshConnection *c;
2526 unsigned int own_pos;
2528 c = GNUNET_new (struct MeshConnection);
2529 for (own_pos = 0; own_pos < p->length; own_pos++)
2531 if (p->peers[own_pos] == myid)
2534 if (own_pos > p->length - 1)
2539 c->own_pos = own_pos;
2541 c->id = t->next_cid++;
2543 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
2547 c->fwd_maintenance_task =
2548 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
2549 &connection_fwd_keepalive, c);
2556 * Notifies a tunnel that a connection has broken that affects at least
2557 * some of its peers. Sends a notification towards the root of the tree.
2558 * In case the peer is the owner of the tree, notifies the client that owns
2559 * the tunnel and tries to reconnect.
2561 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
2563 * @param t Tunnel affected.
2564 * @param p1 Peer that got disconnected from p2.
2565 * @param p2 Peer that got disconnected from p1.
2567 * @return Short ID of the peer disconnected (either p1 or p2).
2568 * 0 if the tunnel remained unaffected.
2570 static GNUNET_PEER_Id
2571 tunnel_notify_connection_broken (struct MeshTunnel2* t,
2572 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
2574 // if (myid != p1 && myid != p2) FIXME
2579 // if (tree_get_predecessor (t->tree) != 0)
2581 // /* We are the peer still connected, notify owner of the disconnection. */
2582 // struct GNUNET_MESH_PathBroken msg;
2583 // struct GNUNET_PeerIdentity neighbor;
2585 // msg.header.size = htons (sizeof (msg));
2586 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2587 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2588 // msg.tid = htonl (t->id.tid);
2589 // msg.peer1 = my_full_id;
2590 // GNUNET_PEER_resolve (pid, &msg.peer2);
2591 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2592 // send_prebuilt_message (&msg.header, &neighbor, t);
2599 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2601 * @param ch Channel this is about.
2602 * @param fwd Is for FWD traffic? (ACK dest->owner)
2605 channel_send_data_ack (struct MeshChannel *ch, int fwd)
2607 struct GNUNET_MESH_DataACK msg;
2608 struct MeshChannelReliability *rel;
2609 struct MeshReliableMessage *copy;
2613 if (GNUNET_NO == ch->reliable)
2618 rel = fwd ? ch->bck_rel : ch->fwd_rel;
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 "send_data_ack for %u\n",
2623 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2624 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2625 msg.header.size = htons (sizeof (msg));
2626 msg.chid = htonl (ch->gid);
2627 msg.mid = htonl (rel->mid_recv - 1);
2629 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2631 delta = copy->mid - rel->mid_recv;
2634 mask = 0x1LL << delta;
2635 msg.futures |= mask;
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2638 copy->mid, delta, mask, msg.futures);
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2642 send_prebuilt_message_channel (&msg.header, ch, fwd);
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2648 * Send an ACK informing the predecessor about the available buffer space.
2650 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
2651 * the ACK itself goes "back" (dest->root).
2653 * @param c Connection on which to send the ACK.
2654 * @param fwd Is this FWD ACK? (Going dest->owner)
2657 connection_send_ack (struct MeshConnection *c, int fwd)
2659 struct MeshFlowControl *next_fc;
2660 struct MeshFlowControl *prev_fc;
2664 next_fc = fwd ? c->fwd_fc : c->bck_fc;
2665 prev_fc = fwd ? c->bck_fc : c->fwd_fc;
2667 /* Check if we need to transmit the ACK */
2668 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2672 " last pid recv: %u, last ack sent: %u\n",
2673 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2677 /* Ok, ACK might be necessary, what PID to ACK? */
2678 delta = next_fc->queue_max - next_fc->queue_n;
2679 ack = prev_fc->last_pid_recv + delta;
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 " last pid %u, last ack %u, qmax %u, q %u\n",
2683 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2684 next_fc->queue_max, next_fc->queue_n);
2685 if (ack == prev_fc->last_ack_sent)
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2691 prev_fc->last_ack_sent = ack;
2692 send_ack (c, ack, fwd);
2697 * Send an ACK informing the client about available buffer space.
2699 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2700 * the ACK itself goes "back" (towards root).
2702 * @param ch Channel on which to send the ACK, NULL if unknown.
2703 * @param fwd Is this FWD ACK? (Going dest->owner)
2706 // channel_send_ack (struct MeshChannel *ch, uint16_t type, int fwd)
2708 // struct MeshChannelReliability *rel;
2709 // struct MeshFlowControl *next_fc;
2710 // struct MeshFlowControl *prev_fc;
2711 // struct MeshClient *c;
2712 // struct MeshClient *o;
2713 // GNUNET_PEER_Id hop;
2714 // uint32_t delta_mid;
2718 // rel = fwd ? ch->fwd_rel : ch->bck_rel;
2719 // c = fwd ? ch->client : ch->owner;
2720 // o = fwd ? ch->owner : ch->client;
2721 // hop = fwd ? connection_get_prev_hop (cn) : connection_get_next_hop (cn);
2722 // next_fc = fwd ? &t->next_fc : &t->prev_fc;
2723 // prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2727 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2728 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2729 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 // "ACK due to %s\n",
2731 // GNUNET_MESH_DEBUG_M2S (type));
2732 // if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2734 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2737 // if (GNUNET_YES == t->reliable && NULL != c)
2738 // tunnel_send_data_ack (t, fwd);
2740 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2741 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2742 // case GNUNET_MESSAGE_TYPE_MESH_ACK:
2743 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2745 // case GNUNET_MESSAGE_TYPE_MESH_POLL:
2746 // case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2747 // t->force_ack = GNUNET_YES;
2750 // GNUNET_break (0);
2753 // /* Check if we need to transmit the ACK */
2755 // prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2756 // GNUNET_NO == t->force_ack)
2758 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2759 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 // " last pid recv: %u, last ack sent: %u\n",
2761 // prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2765 // /* Ok, ACK might be necessary, what PID to ACK? */
2766 // delta = t->queue_max - next_fc->queue_n;
2767 // if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2768 // delta_mid = rel->mid_sent - rel->head_sent->mid;
2771 // if (0 > delta || (GNUNET_YES == t->reliable &&
2773 // (10 < rel->n_sent || 64 <= delta_mid)))
2775 // if (NULL != o && delta > 1)
2777 // ack = prev_fc->last_pid_recv + delta;
2778 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2779 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2780 // " last pid %u, last ack %u, qmax %u, q %u\n",
2781 // prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2782 // t->queue_max, next_fc->queue_n);
2783 // if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2785 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2789 // prev_fc->last_ack_sent = ack;
2791 // send_local_ack (t, o, fwd);
2792 // else if (0 != hop)
2793 // send_ack (t, hop, ack);
2795 // GNUNET_break (GNUNET_YES == t->destroy);
2796 // t->force_ack = GNUNET_NO;
2801 * Modify the mesh message TID from global to local and send to client.
2803 * @param ch Channel on which to send the message.
2804 * @param msg Message to modify and send.
2805 * @param c Client to send to.
2806 * @param tid Tunnel ID to use (c can be both owner and client).
2809 channel_send_client_to_tid (struct MeshChannel *ch,
2810 const struct GNUNET_MESH_Data *msg,
2811 struct MeshClient *c, MESH_ChannelNumber id)
2813 struct GNUNET_MESH_LocalData *copy;
2814 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2815 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2817 if (size < sizeof (struct GNUNET_MessageHeader))
2819 GNUNET_break_op (0);
2827 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2828 memcpy (©[1], &msg[1], size);
2829 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2830 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2831 copy->id = htonl (id);
2832 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2833 ©->header, GNUNET_NO);
2837 * Modify the data message ID from global to local and send to client.
2839 * @param ch Channel on which to send the message.
2840 * @param msg Message to modify and send.
2841 * @param fwd Forward?
2844 channel_send_client_data (struct MeshChannel *ch,
2845 const struct GNUNET_MESH_Data *msg,
2849 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
2851 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
2856 * Send up to 64 buffered messages to the client for in order delivery.
2858 * @param ch Channel on which to empty the message buffer.
2859 * @param c Client to send to.
2860 * @param rel Reliability structure to corresponding peer.
2861 * If rel == bck_rel, this is FWD data.
2864 channel_send_client_buffered_data (struct MeshChannel *ch,
2865 struct MeshClient *c,
2866 struct MeshChannelReliability *rel)
2868 struct MeshReliableMessage *copy;
2869 struct MeshReliableMessage *next;
2871 if (GNUNET_NO == ch->reliable)
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2878 for (copy = rel->head_recv; NULL != copy; copy = next)
2881 if (copy->mid == rel->mid_recv)
2883 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2886 " have %u! now expecting %u\n",
2887 copy->mid, rel->mid_recv + 1);
2888 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
2890 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2896 " don't have %u, next is %u\n",
2902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2907 * We have received a message out of order, buffer it until we receive
2908 * the missing one and we can feed the rest to the client.
2910 * @param msg Message to buffer.
2911 * @param rel Reliability data to the corresponding direction.
2914 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
2915 struct MeshChannelReliability *rel)
2917 struct MeshReliableMessage *copy;
2918 struct MeshReliableMessage *prev;
2922 size = ntohs (msg->header.size);
2923 mid = ntohl (msg->mid);
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2926 copy = GNUNET_malloc (sizeof (*copy) + size);
2929 memcpy (©[1], msg, size);
2931 // FIXME do something better than O(n), although n < 64...
2932 // FIXME start from the end (most messages are the latest ones)
2933 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2936 if (GMC_is_pid_bigger (prev->mid, mid))
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2939 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2945 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2951 * Destroy a reliable message after it has been acknowledged, either by
2952 * direct mid ACK or bitfield. Updates the appropriate data structures and
2953 * timers and frees all memory.
2955 * @param copy Message that is no longer needed: remote peer got it.
2958 rel_message_free (struct MeshReliableMessage *copy)
2960 struct MeshChannelReliability *rel;
2961 struct GNUNET_TIME_Relative time;
2964 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2965 rel->expected_delay.rel_value *= 7;
2966 rel->expected_delay.rel_value += time.rel_value;
2967 rel->expected_delay.rel_value /= 8;
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2972 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2974 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2976 rel->retry_timer = rel->expected_delay;
2977 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2983 * Destroy all reliable messages queued for a channel,
2984 * during a channel destruction.
2985 * Frees the reliability structure itself.
2987 * @param rel Reliability data for a channel.
2990 channel_rel_free_all (struct MeshChannelReliability *rel)
2992 struct MeshReliableMessage *copy;
2993 struct MeshReliableMessage *next;
2998 for (copy = rel->head_recv; NULL != copy; copy = next)
3001 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3004 for (copy = rel->head_sent; NULL != copy; copy = next)
3007 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3010 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3011 GNUNET_SCHEDULER_cancel (rel->retry_task);
3017 * Mark future messages as ACK'd.
3019 * @param rel Reliability data.
3020 * @param msg DataACK message with a bitfield of future ACK'd messages.
3023 channel_rel_free_sent (struct MeshChannelReliability *rel,
3024 const struct GNUNET_MESH_DataACK *msg)
3026 struct MeshReliableMessage *copy;
3027 struct MeshReliableMessage *next;
3034 bitfield = msg->futures;
3035 mid = ntohl (msg->mid);
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3037 "free_sent_reliable %u %llX\n",
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3040 " rel %p, head %p\n",
3041 rel, rel->head_sent);
3042 for (i = 0, copy = rel->head_sent;
3043 i < 64 && NULL != copy && 0 != bitfield;
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 " trying bit %u (mid %u)\n",
3050 if (0 == (bitfield & mask))
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3054 /* Bit was set, clear the bit from the bitfield */
3057 /* The i-th bit was set. Do we have that copy? */
3058 /* Skip copies with mid < target */
3059 target = mid + i + 1;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3061 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3064 /* Did we run out of copies? (previously freed, it's ok) */
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3071 /* Did we overshoot the target? (previously freed, it's ok) */
3072 if (GMC_is_pid_bigger (copy->mid, target))
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3078 /* Now copy->mid == target, free it */
3080 rel_message_free (copy);
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3088 * We haven't received an ACK after a certain time: restransmit the message.
3090 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3091 * @param tc TaskContext.
3094 channel_retransmit_message (void *cls,
3095 const struct GNUNET_SCHEDULER_TaskContext *tc)
3097 struct MeshChannelReliability *rel = cls;
3098 struct MeshReliableMessage *copy;
3099 struct MeshPeerQueue *q;
3100 struct MeshFlowControl *fc;
3101 struct MeshChannel *ch;
3102 struct MeshConnection *c;
3103 struct GNUNET_MESH_Data *payload;
3106 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3107 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3111 copy = rel->head_sent;
3118 /* Search the message to be retransmitted in the outgoing queue.
3119 * Check only the queue for the connection that is going to be used,
3120 * if the message is stuck in some other connection's queue we shouldn't
3122 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3123 * the old connection could be temporary stalled or the queue happened to
3124 * be long at time of insertion.
3125 * - not sending the new one could cause terrible delays the old connection
3128 payload = (struct GNUNET_MESH_Data *) ©[1];
3129 fwd = (rel == ch->fwd_rel);
3130 c = tunnel_get_connection (ch->t, fwd);
3131 fc = fwd ? c->fwd_fc : c->bck_fc;
3132 for (q = fc->queue_head; NULL != q; q = q->next)
3134 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3136 struct GNUNET_MESH_Data *queued_data = q->cls;
3138 if (queued_data->mid == payload->mid)
3143 /* Message not found in the queue that we are going to use. */
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3148 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3149 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3156 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3157 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3158 &channel_retransmit_message,
3164 * Send keepalive packets for a connection.
3166 * @param c Connection to keep alive..
3167 * @param fwd Is this a FWD keepalive? (owner -> dest).
3170 connection_keepalive (struct MeshConnection *c, int fwd)
3172 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3173 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3177 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3178 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "sending %s keepalive for connection %s[%d]\n",
3182 fwd ? "FWD" : "BCK",
3183 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3186 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3187 msg->header.size = htons (size);
3188 msg->header.type = htons (type);
3189 msg->cid = htonl (c->id);
3190 msg->tid = c->t->id;
3192 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3197 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3199 * @param c Connection for which to send the message.
3200 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3203 connection_recreate (struct MeshConnection *c, int fwd)
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3207 send_connection_create (c);
3209 send_connection_ack (c);
3214 * Generic connection timer management.
3215 * Depending on the role of the peer in the connection will send the
3216 * appropriate message (build or keepalive)
3218 * @param c Conncetion to maintain.
3219 * @param fwd Is FWD?
3222 connection_maintain (struct MeshConnection *c, int fwd)
3224 if (MESH_TUNNEL_SEARCHING == c->t->state)
3226 /* TODO DHT GET with RO_BART */
3231 case MESH_CONNECTION_NEW:
3233 case MESH_CONNECTION_SENT:
3234 connection_recreate (c, fwd);
3236 case MESH_CONNECTION_READY:
3237 connection_keepalive (c, fwd);
3246 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3248 struct MeshConnection *c = cls;
3250 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3251 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3254 connection_keepalive (c, GNUNET_YES);
3255 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3256 &connection_fwd_keepalive,
3262 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3264 struct MeshConnection *c = cls;
3266 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3267 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3270 connection_keepalive (c, GNUNET_NO);
3271 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3272 &connection_bck_keepalive,
3278 * Send a message to all peers in this connection that the connection
3279 * is no longer valid.
3281 * If some peer should not receive the message, it should be zero'ed out
3282 * before calling this function.
3284 * @param c The connection whose peers to notify.
3287 connection_send_destroy (struct MeshConnection *c)
3289 struct GNUNET_MESH_ConnectionDestroy msg;
3291 msg.header.size = htons (sizeof (msg));
3292 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3293 msg.cid = htonl (c->id);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 " sending tunnel destroy for connection %s[%X]\n",
3297 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3300 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3301 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3302 c->destroy = GNUNET_YES;
3307 * Send a message to all clients (local and remote) of this channel
3308 * notifying that the channel is no longer valid.
3310 * If some peer or client should not receive the message,
3311 * should be zero'ed out before calling this function.
3313 * @param ch The channel whose clients to notify.
3316 channel_send_destroy (struct MeshChannel *ch)
3318 struct GNUNET_MESH_ChannelDestroy msg;
3320 msg.header.size = htons (sizeof (msg));
3321 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3323 " sending tunnel destroy for channel %s:%X\n",
3324 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->t->peer->id)),
3327 if (NULL != ch->root)
3329 msg.chid = htonl (ch->lid_root);
3330 send_local_channel_destroy (ch, GNUNET_NO);
3334 msg.chid = htonl (ch->gid);
3335 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3338 if (NULL != ch->dest)
3340 msg.chid = htonl (ch->lid_dest);
3341 send_local_channel_destroy (ch, GNUNET_YES);
3345 msg.chid = htonl (ch->gid);
3346 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3354 * @param tid Tunnel ID.
3356 static struct MeshTunnel2 *
3357 tunnel_new (const struct GNUNET_HashCode *tid)
3359 struct MeshTunnel2 *t;
3361 t = GNUNET_new (struct MeshTunnel2);
3363 t->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3365 GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3380 * @param tid Tunnel ID.
3382 static struct MeshTunnel2 *
3383 tunnel_get (const struct GNUNET_HashCode *tid)
3385 return GNUNET_CONTAINER_multihashmap_get (tunnels, tid);
3390 * Add a connection to a tunnel.
3393 * @param c Connection.
3396 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3399 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3404 * Create a connection.
3406 * @param tid Tunnel ID.
3407 * @param cid Connection ID.
3409 static struct MeshConnection *
3410 connection_new (struct GNUNET_HashCode *tid, uint32_t cid)
3412 struct MeshConnection *c;
3413 struct MeshTunnel2 *t;
3415 t = tunnel_get (tid);
3418 t = tunnel_new (tid);
3426 c = GNUNET_new (struct MeshConnection);
3428 tunnel_add_connection (t, c);
3435 * Find a connection.
3437 * @param tid Tunnel ID.
3438 * @param cid Connection ID.
3440 static struct MeshConnection *
3441 connection_get (const struct GNUNET_HashCode *tid, uint32_t cid)
3443 struct MeshConnection *c;
3444 struct MeshTunnel2 *t;
3446 t = tunnel_get (tid);
3447 for (c = t->connection_head; NULL != c; c = c->next)
3456 * Connection is no longer needed: destroy it and remove from tunnel.
3458 * @param c Connection to destroy.
3461 connection_destroy (struct MeshConnection *c)
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
3467 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3470 connection_cancel_queues (c, GNUNET_YES);
3471 connection_cancel_queues (c, GNUNET_NO);
3473 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
3474 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3475 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
3476 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3478 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
3480 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
3486 tunnel_destroy (struct MeshTunnel2 *t)
3488 struct MeshConnection *c;
3489 struct MeshConnection *next;
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
3495 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)));
3497 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
3500 for (c = t->connection_head; NULL != c; c = next)
3503 connection_destroy (c);
3506 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3513 * Tunnel is empty: destroy it.
3515 * Notifies all connections about the destruction.
3517 * @param t Tunnel to destroy.
3520 tunnel_destroy_empty (struct MeshTunnel2 *t)
3522 struct MeshConnection *c;
3524 for (c = t->connection_head; NULL != c; c = c->next)
3526 if (GNUNET_NO == c->destroy)
3527 connection_send_destroy (c);
3530 if (0 == t->pending_messages)
3533 t->destroy = GNUNET_YES;
3538 * Destroy tunnel if empty (no more channels).
3540 * @param t Tunnel to destroy if empty.
3543 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
3545 if (NULL != t->channel_head)
3548 tunnel_destroy_empty (t);
3553 * Initialize a Flow Control structure to the initial state.
3555 * @param fc Flow Control structure to initialize.
3558 fc_init (struct MeshFlowControl *fc)
3560 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3561 fc->last_pid_recv = (uint32_t) -1;
3562 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3563 fc->last_ack_recv = (uint32_t) -1;
3564 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3565 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3571 * Destroy a channel and free all resources.
3573 * @param ch Channel to destroy.
3576 channel_destroy (struct MeshChannel *ch)
3578 struct MeshClient *c;
3586 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
3597 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
3604 if (GNUNET_YES == ch->reliable)
3606 channel_rel_free_all (ch->fwd_rel);
3607 channel_rel_free_all (ch->bck_rel);
3610 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
3611 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
3617 * Create a new channel.
3619 * @param t Tunnel this channel is in.
3620 * @param owner Client that owns the channel, NULL for foreign channels.
3621 * @param lid_root Local ID for root client.
3623 * @return A new initialized channel. NULL on error.
3625 static struct MeshChannel *
3626 channel_new (struct MeshTunnel2 *t,
3627 struct MeshClient *owner, MESH_ChannelNumber lid_root)
3629 struct MeshChannel *ch;
3631 ch = GNUNET_new (struct MeshChannel);
3633 ch->lid_root = lid_root;
3636 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
3640 while (NULL != channel_get_by_local_id (owner, t->next_chid))
3641 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
3642 ch->gid = t->next_chid;
3643 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
3646 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
3647 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3650 channel_destroy (ch);
3651 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
3661 * Set options in a channel, extracted from a bit flag field
3663 * @param ch Channel to set options to.
3664 * @param options Bit array in host byte order.
3667 channel_set_options (struct MeshChannel *ch, uint32_t options)
3669 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3670 GNUNET_YES : GNUNET_NO;
3671 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3672 GNUNET_YES : GNUNET_NO;
3677 * Iterator for deleting each channel whose client endpoint disconnected.
3679 * @param cls Closure (client that has disconnected).
3680 * @param key The local channel id (used to access the hashmap).
3681 * @param value The value stored at the key (channel to destroy).
3683 * @return GNUNET_OK, keep iterating.
3686 channel_destroy_iterator (void *cls,
3690 struct MeshChannel *ch = value;
3691 struct MeshClient *c = cls;
3692 struct MeshTunnel2 *t;
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
3696 ch->gid, ch->lid_root, ch->lid_dest, c->id);
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3710 channel_send_destroy (ch);
3711 channel_destroy (ch);
3712 tunnel_destroy_if_empty (t);
3719 * Remove client's ports from the global hashmap on disconnect.
3721 * @param cls Closure (unused).
3723 * @param value Client structure.
3725 * @return GNUNET_OK, keep iterating.
3728 client_release_ports (void *cls,
3734 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3735 if (GNUNET_YES != res)
3738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3739 "Port %u by client %p was not registered.\n",
3747 * Timeout function due to lack of keepalive/traffic from the owner.
3748 * Destroys connection if called.
3750 * @param cls Closure (connection to destroy).
3751 * @param tc TaskContext.
3754 connection_fwd_timeout (void *cls,
3755 const struct GNUNET_SCHEDULER_TaskContext *tc)
3757 struct MeshConnection *c = cls;
3759 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3760 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 "Connection %s[%X] FWD timed out. Destroying.\n",
3764 GNUNET_i2s(GNUNET_PEER_resolve2 (c->t->peer->id)),
3767 if (NULL != c->t->channel_head) /* If local, leave TODO review */
3770 connection_destroy (c);
3775 * Timeout function due to lack of keepalive/traffic from the destination.
3776 * Destroys connection if called.
3778 * @param cls Closure (connection to destroy).
3779 * @param tc TaskContext
3782 connection_bck_timeout (void *cls,
3783 const struct GNUNET_SCHEDULER_TaskContext *tc)
3785 struct MeshConnection *c = cls;
3787 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3788 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3792 "Connection %s[%X] FWD timed out. Destroying.\n",
3793 GNUNET_i2s(GNUNET_PEER_resolve2 (c->t->peer->id)),
3796 if (NULL != c->t->channel_head) /* If local, leave TODO review */
3799 connection_destroy (c);
3804 * Resets the connection timeout task, some other message has done the
3806 * - For the first peer on the direction this means to send
3807 * a keepalive or a path confirmation message (either create or ACK).
3808 * - For all other peers, this means to destroy the connection,
3809 * due to lack of activity.
3810 * Starts the tiemout if no timeout was running (connection just created).
3812 * @param c Connection whose timeout to reset.
3813 * @param fwd Is this forward?
3815 * TODO use heap to improve efficiency of scheduler.
3818 connection_reset_timeout (struct MeshConnection *c, int fwd)
3820 GNUNET_SCHEDULER_TaskIdentifier *ti;
3821 GNUNET_SCHEDULER_Task f;
3823 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
3825 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3826 GNUNET_SCHEDULER_cancel (*ti);
3828 if (NULL != c->t->channel_head) /* Endpoint */
3830 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
3831 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
3835 struct GNUNET_TIME_Relative delay;
3837 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
3838 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
3839 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
3844 /******************************************************************************/
3845 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3846 /******************************************************************************/
3849 * Free a transmission that was already queued with all resources
3850 * associated to the request.
3852 * @param queue Queue handler to cancel.
3853 * @param clear_cls Is it necessary to free associated cls?
3856 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3858 struct MeshFlowControl *fc;
3861 fwd = (queue->peer == connection_get_next_hop (queue->c));
3862 fc = fwd ? queue->c->fwd_fc : queue->c->bck_fc;
3864 if (GNUNET_YES == clear_cls)
3866 switch (queue->type)
3868 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
3869 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying CONNECTION_DESTROY\n");
3870 GNUNET_break (GNUNET_YES == queue->c->destroy);
3872 case GNUNET_MESSAGE_TYPE_MESH_FWD:
3873 case GNUNET_MESSAGE_TYPE_MESH_BCK:
3874 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3875 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3876 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3879 GNUNET_MESH_DEBUG_M2S (queue->type));
3882 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
3889 GNUNET_MESH_DEBUG_M2S (queue->type));
3891 GNUNET_free_non_null (queue->cls);
3893 GNUNET_CONTAINER_DLL_remove (fc->queue_head, fc->queue_tail, queue);
3897 GNUNET_free (queue);
3902 queue_send (void *cls, size_t size, void *buf)
3904 struct MeshPeer *peer = cls;
3905 const struct GNUNET_PeerIdentity *dst_id;
3906 struct GNUNET_MessageHeader *msg;
3907 struct MeshPeerQueue *queue;
3908 struct MeshTunnel2 *t;
3909 struct MeshFlowControl *fc;
3910 struct MeshConnection *c;
3917 fwd = (queue->peer == connection_get_next_hop (c));
3918 fc = fwd ? c->fwd_fc : c->bck_fc;
3925 fc->core_transmit = NULL;
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3929 if (NULL == buf || 0 == size)
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
3934 queue = fc->queue_head;
3936 /* Queue has no traffic */
3939 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3943 dst_id = GNUNET_PEER_resolve2 (peer->id);
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
3945 /* Check if buffer size is enough for the message */
3946 if (queue->size > size)
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
3950 GNUNET_CORE_notify_transmit_ready (core_handle,
3953 GNUNET_TIME_UNIT_FOREVER_REL,
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3962 t = (NULL != c) ? c->t : NULL;
3966 switch (queue->type)
3968 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
3969 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
3970 case GNUNET_MESSAGE_TYPE_MESH_FWD:
3971 case GNUNET_MESSAGE_TYPE_MESH_BCK:
3972 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3973 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3976 GNUNET_MESH_DEBUG_M2S (queue->type));
3978 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3979 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3980 data_size = send_core_data_raw (queue->cls, size, buf);
3981 msg = (struct GNUNET_MessageHeader *) buf;
3982 type = ntohs (msg->type);
3984 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3986 if (NULL != c->t->channel_head)
3987 data_size = send_core_connection_create (queue->cls, size, buf);
3989 data_size = send_core_data_raw (queue->cls, size, buf);
3991 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3993 if (NULL != c->t->channel_head)
3994 data_size = send_core_connection_ack (queue->cls, size, buf);
3996 data_size = send_core_data_raw (queue->cls, size, buf);
4000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4007 if (0 < drop_percent &&
4008 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4010 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4011 "Dropping message of type %s\n",
4012 GNUNET_MESH_DEBUG_M2S (queue->type));
4015 /* Free queue, but cls was freed by send_core_* */
4016 queue_destroy (queue, GNUNET_NO);
4018 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4021 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4022 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4023 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4024 fc->last_pid_sent = pid;
4025 connection_send_ack (c, fwd);
4031 /* If more data in queue, send next */
4032 queue = fc->queue_head;
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4036 if (NULL == fc->core_transmit) {
4038 GNUNET_CORE_notify_transmit_ready(core_handle,
4041 GNUNET_TIME_UNIT_FOREVER_REL,
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4050 "* tmt rdy called somewhere else\n");
4052 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4054 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4056 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4061 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4063 GNUNET_SCHEDULER_cancel (fc->poll_task);
4064 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4069 c->pending_messages--;
4070 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4073 connection_destroy (c);
4079 t->pending_messages--;
4080 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4092 queue_add (void *cls, uint16_t type, size_t size,
4093 struct MeshPeer *dst,
4094 struct MeshConnection *c,
4095 struct MeshChannel *ch)
4097 struct MeshPeerQueue *queue;
4098 struct MeshFlowControl *fc;
4102 fwd = (dst == connection_get_next_hop (c));
4103 fc = fwd ? c->fwd_fc : c->bck_fc;
4113 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4114 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4118 ( (NULL != ch->root && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4119 (NULL != ch->dest && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4122 if (fc->queue_n >= fc->queue_max && 0 == priority)
4124 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 "queue full: %u/%u\n",
4129 fc->queue_n, fc->queue_max);
4130 return; /* Drop this message */
4134 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
4135 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4136 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4139 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4146 if (100 <= priority)
4147 GNUNET_CONTAINER_DLL_insert (fc->queue_head, fc->queue_tail, queue);
4149 GNUNET_CONTAINER_DLL_insert_tail (fc->queue_head, fc->queue_tail, queue);
4151 if (NULL == fc->core_transmit)
4154 GNUNET_CORE_notify_transmit_ready (core_handle,
4157 GNUNET_TIME_UNIT_FOREVER_REL,
4158 GNUNET_PEER_resolve2 (dst->id),
4163 c->pending_messages++;
4164 c->t->pending_messages++;
4168 /******************************************************************************/
4169 /******************** MESH NETWORK HANDLERS **************************/
4170 /******************************************************************************/
4174 * Generic handler for mesh network payload traffic.
4176 * @param t Tunnel on which we got this message.
4177 * @param message Unencryted data message.
4178 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4180 * @return GNUNET_OK to keep the connection open,
4181 * GNUNET_SYSERR to close it (signal serious error)
4184 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4186 struct MeshChannelReliability *rel;
4187 struct MeshChannel *ch;
4188 struct MeshClient *c;
4194 size = ntohs (msg->header.size);
4196 sizeof (struct GNUNET_MESH_Data) +
4197 sizeof (struct GNUNET_MessageHeader))
4202 type = ntohs (msg->header.type);
4203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4204 GNUNET_MESH_DEBUG_M2S (type));
4205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4206 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4209 ch = channel_get (t, ntohl (msg->chid));
4212 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4217 /* Initialize FWD/BCK data */
4218 c = fwd ? ch->dest : ch->root;
4219 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4227 tunnel_change_state (t, MESH_TUNNEL_READY);
4229 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4231 mid = ntohl (msg->mid);
4232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4234 if (GNUNET_NO == ch->reliable ||
4235 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4236 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4239 if (GNUNET_YES == ch->reliable)
4241 /* Is this the exact next expected messasge? */
4242 if (mid == rel->mid_recv)
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4246 channel_send_client_data (ch, msg, fwd);
4247 channel_send_client_buffered_data (ch, c, rel);
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4252 channel_rel_add_buffered_data (msg, rel);
4255 else /* Tunnel unreliable, send to clients directly */
4257 channel_send_client_data (ch, msg, fwd);
4262 GNUNET_break_op (0);
4263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4264 " MID %u not expected (%u - %u), dropping!\n",
4265 mid, rel->mid_recv, rel->mid_recv + 64);
4268 channel_send_data_ack (ch, fwd);
4274 * Handler for mesh network traffic going from the origin to a peer
4276 * @param t Tunnel on which we got this message.
4277 * @param message Data message.
4279 * @return GNUNET_OK to keep the connection open,
4280 * GNUNET_SYSERR to close it (signal serious error)
4283 handle_unicast (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *message)
4285 return handle_data (t, message, GNUNET_YES);
4290 * Handler for mesh network traffic towards the owner of a tunnel.
4292 * @param t Tunnel on which we got this message.
4293 * @param message Data message.
4295 * @return GNUNET_OK to keep the connection open,
4296 * GNUNET_SYSERR to close it (signal serious error)
4299 handle_to_orig (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *message)
4301 return handle_data (t, message, GNUNET_NO);
4306 * Handler for mesh network traffic end-to-end ACKs.
4308 * @param t Tunnel on which we got this message.
4309 * @param message Data message.
4310 * @param fwd Is this a fwd ACK? (dest->orig)
4312 * @return GNUNET_OK to keep the connection open,
4313 * GNUNET_SYSERR to close it (signal serious error)
4316 handle_data_ack (struct MeshTunnel2 *t,
4317 const struct GNUNET_MESH_DataACK *msg, int fwd)
4319 struct MeshChannelReliability *rel;
4320 struct MeshReliableMessage *copy;
4321 struct MeshReliableMessage *next;
4322 struct MeshChannel *ch;
4327 type = ntohs (msg->header.type);
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4329 GNUNET_MESH_DEBUG_M2S (type));
4330 ch = channel_get (t, ntohl (msg->chid));
4333 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4336 ack = ntohl (msg->mid);
4337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4338 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4340 if (GNUNET_YES == fwd && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4344 else if (GNUNET_NO == fwd && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4353 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4355 if (GMC_is_pid_bigger (copy->mid, ack))
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4358 channel_rel_free_sent (rel, msg);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4364 rel_message_free (copy);
4366 /* ACK client if needed */
4367 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4369 /* If some message was free'd, update the retransmission delay*/
4370 if (GNUNET_YES == work)
4372 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4374 GNUNET_SCHEDULER_cancel (rel->retry_task);
4375 if (NULL == rel->head_sent)
4377 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4381 struct GNUNET_TIME_Absolute new_target;
4382 struct GNUNET_TIME_Relative delay;
4384 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4385 MESH_RETRANSMIT_MARGIN);
4386 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4388 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4390 GNUNET_SCHEDULER_add_delayed (delay,
4391 &channel_retransmit_message,
4403 * Core handler for connection creation.
4405 * @param cls Closure (unused).
4406 * @param peer Sender (neighbor).
4407 * @param message Message.
4409 * @return GNUNET_OK to keep the connection open,
4410 * GNUNET_SYSERR to close it (signal serious error)
4413 handle_mesh_connection_create (void *cls,
4414 const struct GNUNET_PeerIdentity *peer,
4415 const struct GNUNET_MessageHeader *message)
4417 struct GNUNET_MESH_ConnectionCreate *msg;
4418 struct GNUNET_PeerIdentity *id;
4419 struct GNUNET_HashCode *tid;
4420 struct MeshPeerPath *path;
4421 struct MeshPeer *dest_peer;
4422 struct MeshPeer *orig_peer;
4423 struct MeshConnection *c;
4424 unsigned int own_pos;
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
4432 size = ntohs (message->size);
4433 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
4435 GNUNET_break_op (0);
4439 /* Calculate hops */
4440 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
4441 if (size % sizeof (struct GNUNET_PeerIdentity))
4443 GNUNET_break_op (0);
4446 size /= sizeof (struct GNUNET_PeerIdentity);
4449 GNUNET_break_op (0);
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4454 /* Get parameters */
4455 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
4456 cid = ntohl (msg->cid);
4458 id = (struct GNUNET_PeerIdentity *) &msg[1];
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4460 " connection %s[%X] (%s).\n",
4461 GNUNET_h2s (tid), cid, GNUNET_i2s (id));
4463 /* Create connection */
4464 c = connection_get (tid, cid);
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
4468 c = connection_new (tid, cid);
4472 connection_reset_timeout (c, GNUNET_YES);
4473 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
4475 /* Remember peers */
4476 dest_peer = peer_get (&id[size - 1]);
4477 orig_peer = peer_get (&id[0]);
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4481 path = path_new (size);
4483 for (i = 0; i < size; i++)
4485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4486 GNUNET_i2s (&id[i]));
4487 path->peers[i] = GNUNET_PEER_intern (&id[i]);
4488 if (path->peers[i] == myid)
4491 if (own_pos == 0 && path->peers[own_pos] != myid)
4493 /* create path: self not found in path through self */
4494 GNUNET_break_op (0);
4495 path_destroy (path);
4496 connection_destroy (c);
4499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4500 path_add_to_peers (path, GNUNET_NO);
4502 c->own_pos = own_pos;
4504 /* Is it a connection to us? */
4505 if (own_pos == size - 1)
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4508 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
4510 send_connection_ack (c);
4512 /* Keep tunnel alive in direction dest->owner*/
4513 connection_reset_timeout (c, GNUNET_NO);
4517 /* It's for somebody else! Retransmit. */
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4519 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
4520 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
4521 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
4528 * Core handler for path ACKs
4530 * @param cls closure
4531 * @param message message
4532 * @param peer peer identity this notification is about
4534 * @return GNUNET_OK to keep the connection open,
4535 * GNUNET_SYSERR to close it (signal serious error)
4538 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4539 const struct GNUNET_MessageHeader *message)
4541 struct GNUNET_MESH_ConnectionACK *msg;
4542 struct MeshPeerPath *p;
4543 struct MeshConnection *c;
4546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
4547 msg = (struct GNUNET_MESH_ConnectionACK *) message;
4548 cid = ntohl(msg->cid);
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s[%X]\n",
4550 GNUNET_h2s (&msg->tid), cid);
4551 c = connection_get (&msg->tid, cid);
4554 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
4556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
4560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4563 /* Add path to peers? */
4567 path_add_to_peers (p, GNUNET_YES);
4573 connection_change_state (c, MESH_CONNECTION_READY);
4574 connection_reset_timeout (c, GNUNET_NO);
4576 /* Message for us? */
4577 if (NULL != c->t->channel_head)
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4580 if (3 <= tunnel_count_connections(c->t) && NULL != c->t->peer->dhtget)
4582 GNUNET_DHT_get_stop (c->t->peer->dhtget);
4583 c->t->peer->dhtget = NULL;
4585 //connection_send_ack (c, GNUNET_NO); /* FIXME */
4589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4590 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
4596 * Core handler for notifications of broken paths
4598 * @param cls Closure (unused).
4599 * @param peer Peer identity of sending neighbor.
4600 * @param message Message.
4602 * @return GNUNET_OK to keep the connection open,
4603 * GNUNET_SYSERR to close it (signal serious error)
4606 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4607 const struct GNUNET_MessageHeader *message)
4609 struct GNUNET_MESH_ConnectionBroken *msg;
4610 struct MeshConnection *c;
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4613 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
4614 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4616 GNUNET_i2s (&msg->peer1));
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4618 GNUNET_i2s (&msg->peer2));
4619 c = connection_get (&msg->tid, ntohl (msg->cid));
4622 GNUNET_break_op (0);
4625 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
4626 GNUNET_PEER_search (&msg->peer2));
4633 * Core handler for tunnel destruction
4635 * @param cls Closure (unused).
4636 * @param peer Peer identity of sending neighbor.
4637 * @param message Message.
4639 * @return GNUNET_OK to keep the connection open,
4640 * GNUNET_SYSERR to close it (signal serious error)
4643 handle_mesh_connection_destroy (void *cls,
4644 const struct GNUNET_PeerIdentity *peer,
4645 const struct GNUNET_MessageHeader *message)
4647 struct GNUNET_MESH_ConnectionDestroy *msg;
4648 struct MeshConnection *c;
4652 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4654 "Got a CONNECTION DESTROY message from %s\n",
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4657 " for connection %s[%X]\n",
4658 GNUNET_h2s (&msg->tid), ntohl (msg->cid));
4659 c = connection_get (&msg->tid, ntohl (msg->cid));
4662 /* Probably already got the message from another path,
4663 * destroyed the tunnel and retransmitted to children.
4666 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4670 id = GNUNET_PEER_search (peer);
4671 if (id == connection_get_prev_hop (c)->id)
4673 else if (id == connection_get_next_hop (c)->id)
4677 GNUNET_break_op (0);
4680 send_prebuilt_message_connection (message, c, NULL, fwd);
4681 c->destroy = GNUNET_YES;
4688 * Generic handler for mesh network encrypted traffic.
4690 * @param peer Peer identity this notification is about.
4691 * @param message Encrypted message.
4692 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4694 * @return GNUNET_OK to keep the connection open,
4695 * GNUNET_SYSERR to close it (signal serious error)
4698 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
4699 const struct GNUNET_MESH_Encrypted *msg,
4702 struct MeshConnection *c;
4703 struct MeshTunnel2 *t;
4704 struct MeshPeer *neighbor;
4705 struct MeshFlowControl *fc;
4712 size = ntohs (msg->header.size);
4714 sizeof (struct GNUNET_MESH_Encrypted) +
4715 sizeof (struct GNUNET_MessageHeader))
4720 type = ntohs (msg->header.type);
4721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4722 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4724 /* Check connection */
4725 c = connection_get (&msg->tid, ntohl (msg->cid));
4728 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
4729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
4733 fc = fwd ? c->fwd_fc : c->bck_fc;
4735 /* Check if origin is as expected */
4736 neighbor = fwd ? connection_get_prev_hop (c) : connection_get_next_hop (c);
4737 if (peer_get (peer)->id != neighbor->id)
4739 GNUNET_break_op (0);
4744 pid = ntohl (msg->pid);
4745 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4747 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4749 "WARNING Received PID %u, (prev %u), ACK %u\n",
4750 pid, fc->last_pid_recv, fc->last_ack_sent);
4753 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
4755 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4757 " Pid %u not expected (%u+), dropping!\n",
4758 pid, fc->last_pid_recv + 1);
4761 if (MESH_CONNECTION_SENT == c->state)
4762 connection_change_state (c, MESH_CONNECTION_READY);
4763 connection_reset_timeout (c, fwd);
4764 fc->last_pid_recv = pid;
4766 /* Is this message for us? */
4767 if (NULL != c->t->channel_head)
4769 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
4771 struct GNUNET_MESH_Data *dmsg;
4773 /* TODO signature verification */
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us!\n");
4775 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
4777 fc->last_pid_recv = pid;
4778 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
4779 dmsg = (struct GNUNET_MESH_Data *) cbuf;
4780 handle_data (t, dmsg, fwd);
4781 connection_send_ack (c, fwd);
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4786 ttl = ntohl (msg->ttl);
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4790 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4792 connection_send_ack (c, fwd);
4795 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
4797 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
4798 connection_send_ack (c, fwd);
4805 * Core handler for mesh network traffic going orig->dest.
4807 * @param cls Closure (unused).
4808 * @param message Message received.
4809 * @param peer Peer who sent the message.
4811 * @return GNUNET_OK to keep the connection open,
4812 * GNUNET_SYSERR to close it (signal serious error)
4815 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
4816 const struct GNUNET_MessageHeader *message)
4818 return handle_mesh_encrypted (peer,
4819 (struct GNUNET_MESH_Encrypted *)message,
4824 * Core handler for mesh network traffic going dest->orig.
4826 * @param cls Closure (unused).
4827 * @param message Message received.
4828 * @param peer Peer who sent the message.
4830 * @return GNUNET_OK to keep the connection open,
4831 * GNUNET_SYSERR to close it (signal serious error)
4834 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
4835 const struct GNUNET_MessageHeader *message)
4837 return handle_mesh_encrypted (peer,
4838 (struct GNUNET_MESH_Encrypted *)message,
4844 * Core handler for mesh network traffic point-to-point acks.
4846 * @param cls closure
4847 * @param message message
4848 * @param peer peer identity this notification is about
4850 * @return GNUNET_OK to keep the connection open,
4851 * GNUNET_SYSERR to close it (signal serious error)
4854 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4855 const struct GNUNET_MessageHeader *message)
4857 struct GNUNET_MESH_ACK *msg;
4858 struct MeshConnection *c;
4859 struct MeshFlowControl *fc;
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4865 msg = (struct GNUNET_MESH_ACK *) message;
4867 c = connection_get (&msg->tid, ntohl (msg->cid));
4871 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
4876 ack = ntohl (msg->ack);
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4879 /* Is this a forward or backward ACK? */
4880 id = GNUNET_PEER_search (peer);
4881 if (connection_get_next_hop (c)->id == id)
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4886 else if (connection_get_prev_hop (c)->id == id)
4888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4893 GNUNET_break_op (0);
4897 /* Cancel polling if the ACK is bigger than before. */
4898 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4899 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4901 GNUNET_SCHEDULER_cancel (fc->poll_task);
4902 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4903 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4906 fc->last_ack_recv = ack;
4907 connection_unlock_queue (c, fc == c->fwd_fc);
4914 * Core handler for mesh network traffic point-to-point ack polls.
4916 * @param cls closure
4917 * @param message message
4918 * @param peer peer identity this notification is about
4920 * @return GNUNET_OK to keep the connection open,
4921 * GNUNET_SYSERR to close it (signal serious error)
4924 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4925 const struct GNUNET_MessageHeader *message)
4927 struct GNUNET_MESH_Poll *msg;
4928 struct MeshConnection *c;
4929 struct MeshFlowControl *fc;
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
4936 msg = (struct GNUNET_MESH_Poll *) message;
4938 c = connection_get (&msg->tid, ntohl (msg->cid));
4942 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
4944 GNUNET_break_op (0);
4948 /* Is this a forward or backward ACK? */
4949 id = GNUNET_PEER_search (peer);
4950 if (connection_get_next_hop (c)->id == id)
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4955 else if (connection_get_prev_hop (c)->id == id)
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4962 GNUNET_break_op (0);
4966 pid = ntohl (msg->pid);
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
4968 pid, fc->last_pid_recv);
4969 fc->last_pid_recv = pid;
4970 connection_send_ack (c, fc == c->fwd_fc);
4977 * Core handler for mesh keepalives.
4979 * @param cls closure
4980 * @param message message
4981 * @param peer peer identity this notification is about
4982 * @return GNUNET_OK to keep the connection open,
4983 * GNUNET_SYSERR to close it (signal serious error)
4985 * TODO: Check who we got this from, to validate route.
4988 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4989 const struct GNUNET_MessageHeader *message)
4991 struct GNUNET_MESH_ConnectionKeepAlive *msg;
4992 struct MeshConnection *c;
4993 struct MeshPeer *neighbor;
4996 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
4997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5000 c = connection_get (&msg->tid, ntohl (msg->cid));
5003 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5008 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5009 GNUNET_YES : GNUNET_NO;
5011 /* Check if origin is as expected */
5012 neighbor = fwd ? connection_get_prev_hop (c) : connection_get_next_hop (c);
5013 if (peer_get (peer)->id != neighbor->id)
5015 GNUNET_break_op (0);
5019 connection_change_state (c, MESH_CONNECTION_READY);
5020 connection_reset_timeout (c, fwd);
5022 if (NULL != c->t->channel_head)
5025 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5026 send_prebuilt_message_connection (message, c, NULL, fwd);
5034 * Functions to handle messages from core
5036 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5037 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5039 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5040 sizeof (struct GNUNET_MESH_ConnectionACK)},
5041 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5042 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5043 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5044 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5045 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5046 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5047 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5048 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5049 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5050 sizeof (struct GNUNET_MESH_ACK)},
5051 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5052 sizeof (struct GNUNET_MESH_Poll)},
5053 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5054 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5060 * Function to process paths received for a new peer addition. The recorded
5061 * paths form the initial tunnel, which can be optimized later.
5062 * Called on each result obtained for the DHT search.
5064 * @param cls closure
5065 * @param exp when will this value expire
5066 * @param key key of the result
5067 * @param get_path path of the get request
5068 * @param get_path_length lenght of get_path
5069 * @param put_path path of the put request
5070 * @param put_path_length length of the put_path
5071 * @param type type of the result
5072 * @param size number of bytes in data
5073 * @param data pointer to the result data
5076 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5077 const struct GNUNET_HashCode * key,
5078 const struct GNUNET_PeerIdentity *get_path,
5079 unsigned int get_path_length,
5080 const struct GNUNET_PeerIdentity *put_path,
5081 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5082 size_t size, const void *data)
5084 struct MeshPeer *peer = cls;
5085 struct MeshPeerPath *p;
5086 struct MeshConnection *c;
5087 struct GNUNET_PeerIdentity pi;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5091 GNUNET_PEER_resolve (peer->id, &pi);
5092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5094 p = path_build_from_dht (get_path, get_path_length,
5095 put_path, put_path_length);
5096 path_add_to_peers (p, GNUNET_NO);
5099 /* Count connections */
5100 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5102 /* If we already have 3 (or more (?!)) connections, it's enough */
5106 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5109 peer_connect (peer);
5115 /******************************************************************************/
5116 /********************* MESH LOCAL HANDLES **************************/
5117 /******************************************************************************/
5121 * Handler for client connection.
5123 * @param cls Closure (unused).
5124 * @param client Client handler.
5127 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5129 struct MeshClient *c;
5133 c = GNUNET_malloc (sizeof (struct MeshClient));
5135 c->id = next_client_id++; /* overflow not important: just for debug */
5136 GNUNET_SERVER_client_keep (client);
5137 GNUNET_SERVER_client_set_user_context (client, c);
5138 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5143 * Handler for client disconnection
5145 * @param cls closure
5146 * @param client identification of the client; NULL
5147 * for the last call when the server is destroyed
5150 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5152 struct MeshClient *c;
5153 struct MeshClient *next;
5155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5162 c = client_get (client);
5165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5167 GNUNET_SERVER_client_drop (c->handle);
5168 c->shutting_down = GNUNET_YES;
5169 if (NULL != c->own_channels)
5171 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
5172 &channel_destroy_iterator, c);
5173 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
5176 if (NULL != c->incoming_channels)
5178 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
5179 &channel_destroy_iterator, c);
5180 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
5183 if (NULL != c->ports)
5185 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5186 &client_release_ports, c);
5187 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5190 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5191 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
5206 * Handler for new clients
5208 * @param cls closure
5209 * @param client identification of the client
5210 * @param message the actual message, which includes messages the client wants
5213 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5214 const struct GNUNET_MessageHeader *message)
5216 struct GNUNET_MESH_ClientConnect *cc_msg;
5217 struct MeshClient *c;
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
5224 /* Check data sanity */
5225 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5226 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5227 if (0 != (size % sizeof (uint32_t)))
5230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5233 size /= sizeof (uint32_t);
5235 /* Initialize new client structure */
5236 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
5237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
5238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
5243 p = (uint32_t *) &cc_msg[1];
5244 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
5245 for (i = 0; i < size; i++)
5248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
5250 /* store in client's hashmap */
5251 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
5252 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5253 /* store in global hashmap */
5254 /* FIXME only allow one client to have the port open,
5255 * have a backup hashmap with waiting clients */
5256 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5261 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5262 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5263 GNUNET_SERVER_notification_context_add (nc, client);
5264 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5266 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5272 * Handler for requests of new tunnels
5274 * @param cls Closure.
5275 * @param client Identification of the client.
5276 * @param message The actual message.
5279 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
5280 const struct GNUNET_MessageHeader *message)
5282 struct GNUNET_MESH_ChannelMessage *msg;
5283 struct MeshPeer *peer;
5284 struct MeshTunnel2 *t;
5285 struct MeshChannel *ch;
5286 struct MeshClient *c;
5287 MESH_ChannelNumber chid;
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
5291 /* Sanity check for client registration */
5292 if (NULL == (c = client_get (client)))
5295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5300 /* Message size sanity check */
5301 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5308 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5310 GNUNET_i2s (&msg->peer), ntohl (msg->port));
5311 chid = ntohl (msg->channel_id);
5313 /* Sanity check for duplicate channel IDs */
5314 if (NULL != channel_get_by_local_id (c, chid))
5317 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5321 peer = peer_get (&msg->peer);
5322 if (NULL == peer->tunnel)
5324 struct GNUNET_HashCode tid;
5326 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &tid);
5327 peer->tunnel = tunnel_new (&tid);
5331 /* Create channel */
5332 ch = channel_new (t, c, chid);
5336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5339 ch->port = ntohl (msg->port);
5340 channel_set_options (ch, ntohl (msg->opt));
5341 if (GNUNET_YES == ch->reliable)
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5344 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5345 ch->fwd_rel->ch = ch;
5346 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
5350 GNUNET_h2s (&t->id), ch->gid, ch->port, ch->lid_root);
5351 peer_connect (peer);
5352 /* FIXME send create channel */
5354 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5360 * Handler for requests of deleting tunnels
5362 * @param cls closure
5363 * @param client identification of the client
5364 * @param message the actual message
5367 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5368 const struct GNUNET_MessageHeader *message)
5370 struct GNUNET_MESH_ChannelMessage *tunnel_msg;
5371 struct MeshClient *c;
5372 struct MeshTunnel *t;
5373 MESH_ChannelNumber tid;
5375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5376 "Got a DESTROY TUNNEL from client!\n");
5378 /* Sanity check for client registration */
5379 if (NULL == (c = client_get (client)))
5382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5387 /* Message sanity check */
5388 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5395 tunnel_msg = (struct GNUNET_MESH_ChannelMessage *) message;
5397 /* Retrieve tunnel */
5398 tid = ntohl (tunnel_msg->channel_id);
5399 t = channel_get_by_local_id (c, tid);
5402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5408 /* Cleanup after the tunnel */
5409 client_delete_tunnel (c, t);
5410 if (c == t->client && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= tid)
5414 else if (c == t->owner && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > tid)
5416 peer_remove_tunnel (peer_get_short (t->dest), t);
5421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5422 " tunnel %X client %p (%p, %p)\n",
5423 tid, c, t->owner, t->client);
5427 /* The tunnel will be destroyed when the last message is transmitted. */
5428 tunnel_destroy_empty (t);
5430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5436 * Handler for client traffic
5438 * @param cls closure
5439 * @param client identification of the client
5440 * @param message the actual message
5443 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5444 const struct GNUNET_MessageHeader *message)
5446 struct GNUNET_MESH_LocalData *data_msg;
5447 struct MeshClient *c;
5448 struct MeshTunnel *t;
5449 struct MeshFlowControl *fc;
5450 MESH_ChannelNumber tid;
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5454 "Got data from a client!\n");
5456 /* Sanity check for client registration */
5457 if (NULL == (c = client_get (client)))
5460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5465 data_msg = (struct GNUNET_MESH_LocalData *) message;
5467 /* Sanity check for message size */
5468 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5469 if (size < sizeof (struct GNUNET_MessageHeader))
5472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5476 /* Tunnel exists? */
5477 tid = ntohl (data_msg->tid);
5478 t = channel_get_by_local_id (c, tid);
5482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5486 /* Is the client in the tunnel? */
5487 if ( !( (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV &&
5489 t->owner->handle == client)
5491 (tid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV &&
5493 t->client->handle == client) ) )
5496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5500 /* Ok, everything is correct, send the message
5501 * (pretend we got it from a mesh peer)
5504 struct GNUNET_MESH_Data *payload;
5505 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5507 fc = tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5508 if (GNUNET_YES == t->reliable)
5510 struct MeshChannelReliability *rel;
5511 struct MeshReliableMessage *copy;
5513 rel = (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5514 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5515 + sizeof(struct GNUNET_MESH_Data)
5517 copy->mid = rel->mid_sent++;
5518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5519 copy->timestamp = GNUNET_TIME_absolute_get ();
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5523 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5524 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5527 GNUNET_TIME_relative_multiply (rel->expected_delay,
5528 MESH_RETRANSMIT_MARGIN);
5530 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5531 &tunnel_retransmit_message,
5534 payload = (struct GNUNET_MESH_Data *) ©[1];
5535 payload->mid = htonl (copy->mid);
5539 payload = (struct GNUNET_MESH_Data *) cbuf;
5540 payload->mid = htonl (fc->last_pid_recv + 1);
5542 memcpy (&payload[1], &data_msg[1], size);
5543 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5544 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ?
5545 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5546 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5547 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5548 payload->tid = htonl (t->id.tid);
5549 payload->ttl = htonl (default_ttl);
5550 payload->pid = htonl (fc->last_pid_recv + 1);
5551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5552 " calling generic handler...\n");
5553 if (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
5554 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5556 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5559 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5566 * Handler for client's ACKs for payload traffic.
5568 * @param cls Closure (unused).
5569 * @param client Identification of the client.
5570 * @param message The actual message.
5573 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5574 const struct GNUNET_MessageHeader *message)
5576 struct GNUNET_MESH_LocalAck *msg;
5577 struct MeshTunnel *t;
5578 struct MeshClient *c;
5579 MESH_ChannelNumber tid;
5581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5582 /* Sanity check for client registration */
5583 if (NULL == (c = client_get (client)))
5586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5591 msg = (struct GNUNET_MESH_LocalAck *) message;
5593 /* Tunnel exists? */
5594 tid = ntohl (msg->channel_id);
5595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5596 t = channel_get_by_local_id (c, tid);
5600 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5606 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5607 if (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
5609 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5610 t->prev_fc.last_ack_recv++;
5611 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5615 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5616 t->next_fc.last_ack_recv++;
5617 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5620 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5628 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5630 * @param cls Closure (client handle).
5631 * @param key Key (hashed tunnel ID, unused).
5632 * @param value Tunnel info.
5634 * @return GNUNET_YES, to keep iterating.
5637 monitor_all_tunnels_iterator (void *cls,
5638 const struct GNUNET_HashCode * key,
5641 struct GNUNET_SERVER_Client *client = cls;
5642 struct MeshChannel *ch = value;
5643 struct GNUNET_MESH_LocalMonitor *msg;
5645 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5646 msg->channel_id = htonl (ch->id);
5647 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5648 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5650 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5651 "* sending info about tunnel %s\n",
5652 GNUNET_i2s (&msg->owner));
5654 GNUNET_SERVER_notification_context_unicast (nc, client,
5655 &msg->header, GNUNET_NO);
5661 * Handler for client's MONITOR request.
5663 * @param cls Closure (unused).
5664 * @param client Identification of the client.
5665 * @param message The actual message.
5668 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5669 const struct GNUNET_MessageHeader *message)
5671 struct MeshClient *c;
5673 /* Sanity check for client registration */
5674 if (NULL == (c = client_get (client)))
5677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5681 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5682 "Received get tunnels request from client %u\n",
5684 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5685 monitor_all_tunnels_iterator,
5687 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5688 "Get tunnels request from client %u completed\n",
5690 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5695 * Handler for client's MONITOR_TUNNEL request.
5697 * @param cls Closure (unused).
5698 * @param client Identification of the client.
5699 * @param message The actual message.
5702 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5703 const struct GNUNET_MessageHeader *message)
5705 const struct GNUNET_MESH_LocalMonitor *msg;
5706 struct GNUNET_MESH_LocalMonitor *resp;
5707 struct MeshClient *c;
5708 struct MeshChannel *ch;
5710 /* Sanity check for client registration */
5711 if (NULL == (c = client_get (client)))
5714 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5718 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5719 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5720 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5723 ntohl (msg->channel_id));
5724 ch = channel_get (&msg->owner, ntohl (msg->channel_id));
5727 /* We don't know the tunnel */
5728 struct GNUNET_MESH_LocalMonitor warn;
5731 GNUNET_SERVER_notification_context_unicast (nc, client,
5734 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5738 /* Initialize context */
5739 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5741 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5742 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5743 &resp->header, GNUNET_NO);
5746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5747 "Monitor tunnel request from client %u completed\n",
5749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5754 * Functions to handle messages from clients
5756 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5757 {&handle_local_new_client, NULL,
5758 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5759 {&handle_local_channel_create, NULL,
5760 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
5761 sizeof (struct GNUNET_MESH_ChannelMessage)},
5762 {&handle_local_channel_destroy, NULL,
5763 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
5764 sizeof (struct GNUNET_MESH_ChannelMessage)},
5765 {&handle_local_data, NULL,
5766 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5767 {&handle_local_ack, NULL,
5768 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5769 sizeof (struct GNUNET_MESH_LocalAck)},
5770 {&handle_local_get_tunnels, NULL,
5771 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5772 sizeof (struct GNUNET_MessageHeader)},
5773 {&handle_local_show_tunnel, NULL,
5774 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5775 sizeof (struct GNUNET_MESH_LocalMonitor)},
5781 * Method called whenever a given peer connects.
5783 * @param cls closure
5784 * @param peer peer identity this notification is about
5787 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5789 struct MeshPeer *peer_info;
5790 struct MeshPeerPath *path;
5792 DEBUG_CONN ("Peer connected\n");
5793 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5794 peer_info = peer_get (peer);
5795 if (myid == peer_info->id)
5797 DEBUG_CONN (" (self)\n");
5798 path = path_new (1);
5802 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5803 path = path_new (2);
5804 path->peers[1] = peer_info->id;
5805 GNUNET_PEER_change_rc (peer_info->id, 1);
5806 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5808 path->peers[0] = myid;
5809 GNUNET_PEER_change_rc (myid, 1);
5810 peer_add_path (peer_info, path, GNUNET_YES);
5816 * Method called whenever a peer disconnects.
5818 * @param cls closure
5819 * @param peer peer identity this notification is about
5822 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5824 struct MeshPeer *pi;
5825 struct MeshPeerQueue *q;
5826 struct MeshPeerQueue *n;
5827 struct MeshFlowControl *fc;
5829 DEBUG_CONN ("Peer disconnected\n");
5830 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5848 queue_destroy (q, GNUNET_YES);
5851 if (NULL != fc->core_transmit)
5852 GNUNET_CORE_notify_transmit_ready_cancel (fc->core_transmit);
5853 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
5854 GNUNET_SCHEDULER_cancel (fc->poll_task);
5856 peer_remove_path (pi, pi->id, myid);
5859 DEBUG_CONN (" (self)\n");
5861 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5869 * Install server (service) handlers and start listening to clients.
5874 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5875 GNUNET_SERVER_connect_notify (server_handle,
5876 &handle_local_client_connect, NULL);
5877 GNUNET_SERVER_disconnect_notify (server_handle,
5878 &handle_local_client_disconnect, NULL);
5879 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5881 clients_head = NULL;
5882 clients_tail = NULL;
5884 GNUNET_SERVER_resume (server_handle);
5889 * To be called on core init/fail.
5891 * @param cls Closure (config)
5892 * @param server handle to the server for this service
5893 * @param identity the public identity of this peer
5896 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5897 const struct GNUNET_PeerIdentity *identity)
5899 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5903 GNUNET_break (core_handle == server);
5904 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5910 GNUNET_i2s (identity));
5911 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5913 GNUNET_i2s (&my_full_id));
5914 GNUNET_CORE_disconnect (core_handle);
5915 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5916 NULL, /* Closure passed to MESH functions */
5917 &core_init, /* Call core_init once connected */
5918 &core_connect, /* Handle connects */
5919 &core_disconnect, /* remove peers on disconnects */
5920 NULL, /* Don't notify about all incoming messages */
5921 GNUNET_NO, /* For header only in notification */
5922 NULL, /* Don't notify about all outbound messages */
5923 GNUNET_NO, /* For header-only out notification */
5924 core_handlers); /* Register these handlers */
5933 /******************************************************************************/
5934 /************************ MAIN FUNCTIONS ****************************/
5935 /******************************************************************************/
5938 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5940 * @param cls closure
5941 * @param key current key code
5942 * @param value value in the hash map
5943 * @return GNUNET_YES if we should continue to iterate,
5947 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5949 struct MeshTunnel2 *t = value;
5956 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5958 * @param cls closure
5959 * @param key current key code
5960 * @param value value in the hash map
5961 * @return GNUNET_YES if we should continue to iterate,
5965 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5967 struct MeshPeer *p = value;
5968 struct MeshPeerQueue *q;
5969 struct MeshPeerQueue *n;
5971 q = p->fc->queue_head;
5977 queue_destroy(q, GNUNET_YES);
5987 * Task run during shutdown.
5993 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5997 if (core_handle != NULL)
5999 GNUNET_CORE_disconnect (core_handle);
6002 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6003 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6004 if (dht_handle != NULL)
6006 GNUNET_DHT_disconnect (dht_handle);
6011 GNUNET_SERVER_notification_context_destroy (nc);
6014 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6016 GNUNET_SCHEDULER_cancel (announce_id_task);
6017 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6024 * Process mesh requests.
6026 * @param cls closure
6027 * @param server the initialized server
6028 * @param c configuration to use
6031 run (void *cls, struct GNUNET_SERVER_Handle *server,
6032 const struct GNUNET_CONFIGURATION_Handle *c)
6035 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6038 server_handle = server;
6039 GNUNET_SERVER_suspend (server_handle);
6042 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6047 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6048 "mesh", "peer/privatekey");
6049 GNUNET_SCHEDULER_shutdown ();
6054 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6055 &refresh_connection_time))
6057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6059 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6060 "mesh", "refresh path time");
6061 GNUNET_SCHEDULER_shutdown ();
6066 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6071 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6072 "mesh", "id announce time");
6073 GNUNET_SCHEDULER_shutdown ();
6078 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6081 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6083 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6084 "mesh", "connect timeout");
6085 GNUNET_SCHEDULER_shutdown ();
6090 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6093 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6095 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6096 "mesh", "max msgs queue");
6097 GNUNET_SCHEDULER_shutdown ();
6102 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6107 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6108 "mesh", "max tunnels");
6109 GNUNET_SCHEDULER_shutdown ();
6114 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6119 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6120 "mesh", "default ttl", 64);
6125 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6129 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6130 "mesh", "max peers", 1000);
6135 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6142 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6143 "Mesh is running with drop mode enabled. "
6144 "This is NOT a good idea! "
6145 "Remove the DROP_PERCENT option from your configuration.\n");
6149 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6150 &dht_replication_level))
6152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6154 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6155 "mesh", "dht replication level", 3);
6156 dht_replication_level = 3;
6159 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6160 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6161 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6163 dht_handle = GNUNET_DHT_connect (c, 64);
6164 if (NULL == dht_handle)
6168 stats = GNUNET_STATISTICS_create ("mesh", c);
6170 /* Scheduled the task to clean up when shutdown is called */
6171 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6173 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6174 GNUNET_free (keyfile);
6175 GNUNET_assert (NULL != pk);
6176 my_private_key = pk;
6177 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6178 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6179 &my_full_id.hashPubKey);
6180 myid = GNUNET_PEER_intern (&my_full_id);
6181 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6182 "Mesh for peer [%s] starting\n",
6183 GNUNET_i2s(&my_full_id));
6185 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6186 NULL, /* Closure passed to MESH functions */
6187 &core_init, /* Call core_init once connected */
6188 &core_connect, /* Handle connects */
6189 &core_disconnect, /* remove peers on disconnects */
6190 NULL, /* Don't notify about all incoming messages */
6191 GNUNET_NO, /* For header only in notification */
6192 NULL, /* Don't notify about all outbound messages */
6193 GNUNET_NO, /* For header-only out notification */
6194 core_handlers); /* Register these handlers */
6195 if (NULL == core_handle)
6198 GNUNET_SCHEDULER_shutdown ();
6201 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6207 * The main function for the mesh service.
6209 * @param argc number of arguments from the command line
6210 * @param argv command line arguments
6211 * @return 0 ok, 1 on error
6214 main (int argc, char *const *argv)
6219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6220 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6222 ret = (GNUNET_OK == r) ? 0 : 1;
6223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");