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
225 struct MeshPeer *peer;
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 * Number of attempts to reconnect so far
302 int n_reconnect_attempts;
305 * Paths to reach the peer, ordered by ascending hop count
307 struct MeshPeerPath *path_head;
310 * Paths to reach the peer, ordered by ascending hop count
312 struct MeshPeerPath *path_tail;
315 * Handle to stop the DHT search for paths to this peer
317 struct GNUNET_DHT_GetHandle *dhtget;
320 * Tunnel to this peer, if any.
322 struct MeshTunnel2 *tunnel;
325 * Flow control information for direct traffic.
327 struct MeshFlowControl *fc;
333 * Info needed to retry a message in case it gets lost.
335 struct MeshReliableMessage
338 * Double linked list, FIFO style
340 struct MeshReliableMessage *next;
341 struct MeshReliableMessage *prev;
344 * Tunnel Reliability queue this message is in.
346 struct MeshChannelReliability *rel;
349 * ID of the message (ACK needed to free)
354 * When was this message issued (to calculate ACK delay)
356 struct GNUNET_TIME_Absolute timestamp;
358 /* struct GNUNET_MESH_Data with payload */
362 struct MeshChannelReliability
365 * Channel this is about.
367 struct MeshChannel *ch;
370 * DLL of messages sent and not yet ACK'd.
372 struct MeshReliableMessage *head_sent;
373 struct MeshReliableMessage *tail_sent;
386 * DLL of messages received out of order.
388 struct MeshReliableMessage *head_recv;
389 struct MeshReliableMessage *tail_recv;
397 * Task to resend/poll in case no ACK is received.
399 GNUNET_SCHEDULER_TaskIdentifier retry_task;
402 * Counter for exponential backoff.
404 struct GNUNET_TIME_Relative retry_timer;
407 * How long does it usually take to get an ACK.
409 struct GNUNET_TIME_Relative expected_delay;
414 * Struct containing all information regarding a channel to a remote client.
419 * Tunnel this channel is in.
421 struct MeshTunnel2 *t;
424 * Destination port of the channel.
429 * Local tunnel number ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
431 MESH_ChannelNumber id;
434 * Local tunnel number for local destination clients (incoming number)
435 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0). All clients share the same
438 MESH_ChannelNumber id_dest;
441 * Is the tunnel bufferless (minimum latency)?
446 * Is the tunnel reliable?
451 * Last time the channel was used
453 struct GNUNET_TIME_Absolute timestamp;
456 * Client owner of the tunnel, if any
458 struct MeshClient *owner;
461 * Client destination of the tunnel, if any.
463 struct MeshClient *client;
466 * Flag to signal the destruction of the channel.
467 * If this is set GNUNET_YES the channel will be destroyed
468 * when the queue is empty.
473 * Total messages pending for this channel, payload or not.
475 unsigned int pending_messages;
479 * Only present (non-NULL) at the owner of a tunnel.
481 struct MeshChannelReliability *fwd_rel;
485 * Only present (non-NULL) at the destination of a tunnel.
487 struct MeshChannelReliability *bck_rel;
491 struct MeshConnection
496 struct MeshConnection *next;
497 struct MeshConnection *prev;
500 * Tunnes this belongs to
502 struct MeshTunnel2 *t;
510 * State of the connection
512 enum MeshConnectionState state;
515 * Path being used for the tunnel.
517 struct MeshPeerPath *path;
520 * Position of the local peer in the path.
522 unsigned int own_pos;
525 * Task to keep the used paths alive at the owner,
526 * time tunnel out on all the other peers.
528 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
531 * Task to keep the used paths alive at the destination,
532 * time tunnel out on all the other peers.
534 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
539 * Struct containing all information regarding a tunnel to a peer.
544 * Endpoint of the tunnel.
546 struct MeshPeer *peer;
551 struct GNUNET_HashCode id;
554 * State of the tunnel.
556 enum MeshTunnelState state;
559 * Local peer ephemeral private key
561 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
564 * Local peer ephemeral public key
566 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *my_eph;
569 * Remote peer's public key.
571 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *peers_eph;
574 * Encryption ("our") key.
576 struct GNUNET_CRYPTO_AesSessionKey e_key;
579 * Decryption ("their") key.
581 struct GNUNET_CRYPTO_AesSessionKey d_key;
584 * Paths that are actively used to reach the destination peer.
586 struct MeshConnection *connection_head;
587 struct MeshConnection *connection_tail;
590 * Next connection number.
595 * Channels inside this tunnel.
597 struct MeshChannel *channel_head;
598 struct MeshChannel *channel_tail;
601 * Channel ID for the next created tunnel.
603 MESH_ChannelNumber next_chid;
606 * Channel ID for the next incoming tunnel.
608 MESH_ChannelNumber next_local_chid;
614 * Struct containing information about a client of the service
616 * TODO: add a list of 'waiting' ports
623 struct MeshClient *next;
628 struct MeshClient *prev;
631 * Tunnels that belong to this client, indexed by local id
633 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
636 * Tunnels this client has accepted, indexed by incoming local id
638 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
641 * Handle to communicate with the client
643 struct GNUNET_SERVER_Client *handle;
646 * Ports that this client has declared interest in.
647 * Indexed by port, contains *Client.
649 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
652 * Whether the client is active or shutting down (don't send confirmations
653 * to a client that is shutting down.
658 * ID of the client, mainly for debug messages
664 /******************************************************************************/
665 /************************ DEBUG FUNCTIONS ****************************/
666 /******************************************************************************/
670 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
671 * @param cls string to print
672 * @param success GNUNET_OK if the PUT was transmitted,
673 * GNUNET_NO on timeout,
674 * GNUNET_SYSERR on disconnect from service
675 * after the PUT message was transmitted
676 * (so we don't know if it was received or not)
681 mesh_debug (void *cls, int success)
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
691 /******************************************************************************/
692 /*********************** GLOBAL VARIABLES ****************************/
693 /******************************************************************************/
695 /************************** Configuration parameters **************************/
698 * How often to send path keepalives. Paths timeout after 4 missed.
700 static struct GNUNET_TIME_Relative refresh_connection_time;
703 * How often to PUT own ID in the DHT.
705 static struct GNUNET_TIME_Relative id_announce_time;
708 * Maximum time allowed to connect to a peer found by string.
710 static struct GNUNET_TIME_Relative connect_timeout;
713 * Default TTL for payload packets.
715 static unsigned long long default_ttl;
718 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
720 static unsigned long long dht_replication_level;
723 * How many tunnels are we willing to maintain.
724 * Local tunnels are always allowed, even if there are more tunnels than max.
726 static unsigned long long max_tunnels;
729 * How many messages *in total* are we willing to queue, divided by number of
730 * tunnels to get tunnel queue size.
732 static unsigned long long max_msgs_queue;
735 * How many peers do we want to remember?
737 static unsigned long long max_peers;
740 * Percentage of messages that will be dropped (for test purposes only).
742 static unsigned long long drop_percent;
744 /*************************** Static global variables **************************/
747 * DLL with all the clients, head.
749 static struct MeshClient *clients_head;
752 * DLL with all the clients, tail.
754 static struct MeshClient *clients_tail;
757 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
759 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
762 * Peers known, indexed by PeerIdentity (MeshPeer).
764 static struct GNUNET_CONTAINER_MultiHashMap *peers;
767 * Handle to communicate with core.
769 static struct GNUNET_CORE_Handle *core_handle;
774 static struct GNUNET_DHT_Handle *dht_handle;
777 * Handle to server lib.
779 static struct GNUNET_SERVER_Handle *server_handle;
782 * Handle to the statistics service.
784 static struct GNUNET_STATISTICS_Handle *stats;
787 * Notification context, to send messages to local clients.
789 static struct GNUNET_SERVER_NotificationContext *nc;
792 * Local peer own ID (memory efficient handle).
794 static GNUNET_PEER_Id myid;
797 * Local peer own ID (full value).
799 static struct GNUNET_PeerIdentity my_full_id;
804 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
809 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
812 * All ports clients of this peer have opened.
814 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
817 * Task to periodically announce itself in the network.
819 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
822 * Next ID to assign to a client.
824 unsigned int next_client_id;
827 /******************************************************************************/
828 /*********************** DECLARATIONS **************************/
829 /******************************************************************************/
832 * Function to process paths received for a new peer addition. The recorded
833 * paths form the initial tunnel, which can be optimized later.
834 * Called on each result obtained for the DHT search.
837 * @param exp when will this value expire
838 * @param key key of the result
839 * @param type type of the result
840 * @param size number of bytes in data
841 * @param data pointer to the result data
844 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
845 const struct GNUNET_HashCode * key,
846 const struct GNUNET_PeerIdentity *get_path,
847 unsigned int get_path_length,
848 const struct GNUNET_PeerIdentity *put_path,
849 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
850 size_t size, const void *data);
854 * Retrieve the MeshPeer stucture associated with the peer, create one
855 * and insert it in the appropriate structures if the peer is not known yet.
857 * @param peer Full identity of the peer.
859 * @return Existing or newly created peer info.
861 static struct MeshPeer *
862 peer_get (const struct GNUNET_PeerIdentity *peer);
866 * Retrieve the MeshPeer stucture associated with the peer, create one
867 * and insert it in the appropriate structures if the peer is not known yet.
869 * @param peer Short identity of the peer.
871 * @return Existing or newly created peer info.
873 static struct MeshPeer *
874 peer_get_short (const GNUNET_PEER_Id peer);
878 * Build a PeerPath from the paths returned from the DHT, reversing the paths
879 * to obtain a local peer -> destination path and interning the peer ids.
881 * @return Newly allocated and created path
883 static struct MeshPeerPath *
884 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
885 unsigned int get_path_length,
886 const struct GNUNET_PeerIdentity *put_path,
887 unsigned int put_path_length);
891 * Adds a path to the data structs of all the peers in the path
893 * @param p Path to process.
894 * @param confirmed Whether we know if the path works or not.
897 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
901 * Search for a channel by global ID using full PeerIdentities.
903 * @param oid owner of the tunnel.
904 * @param tid global tunnel number.
906 * @return tunnel handler, NULL if doesn't exist.
908 static struct MeshChannel *
909 channel_get (const struct GNUNET_PeerIdentity *oid, MESH_ChannelNumber tid);
913 * Change the tunnel state.
915 * @param t Tunnel whose state to change.
916 * @param state New state.
919 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
923 * Notify a tunnel that a connection has broken that affects at least
926 * @param t Tunnel affected.
927 * @param p1 Peer that got disconnected from p2.
928 * @param p2 Peer that got disconnected from p1.
930 * @return Short ID of the peer disconnected (either p1 or p2).
931 * 0 if the tunnel remained unaffected.
933 static GNUNET_PEER_Id
934 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
935 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
938 * @brief Use the given path for the tunnel.
939 * Update the next and prev hops (and RCs).
940 * (Re)start the path refresh in case the tunnel is locally owned.
942 * @param t Tunnel to update.
943 * @param p Path to use.
945 * @return Connection created.
947 static struct MeshConnection *
948 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
951 * Tunnel is empty: destroy it.
953 * Notifies all participants (peers, cleints) about the destruction.
955 * @param t Tunnel to destroy.
958 tunnel_destroy_empty (struct MeshTunnel2 *t);
961 * Destroy the tunnel.
963 * This function does not generate any warning traffic to clients or peers.
966 * Remove the tunnel from peer_info's and clients' hashmaps.
967 * Cancel messages belonging to this tunnel queued to neighbors.
968 * Free any allocated resources linked to the tunnel.
970 * @param t the tunnel to destroy
972 * @return GNUNET_OK on success
975 tunnel_destroy (struct MeshTunnel2 *t);
978 * Send FWD keepalive packets for a connection.
980 * @param cls Closure (connection for which to send the keepalive).
981 * @param tc Notification context.
984 connection_fwd_keepalive (void *cls,
985 const struct GNUNET_SCHEDULER_TaskContext *tc);
988 * Send BCK keepalive packets for a connection.
990 * @param cls Closure (connection for which to send the keepalive).
991 * @param tc Notification context.
994 connection_bck_keepalive (void *cls,
995 const struct GNUNET_SCHEDULER_TaskContext *tc);
999 * Change the tunnel state.
1001 * @param c Connection whose state to change.
1002 * @param state New state.
1005 connection_change_state (struct MeshConnection* c,
1006 enum MeshConnectionState state);
1011 * @brief Queue and pass message to core when possible.
1013 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
1014 * accounts for it. In case the queue is full, the message is dropped and
1017 * Otherwise, message is treated as internal and allowed to go regardless of
1020 * @param cls Closure (@c type dependant). It will be used by queue_send to
1021 * build the message to be sent if not already prebuilt.
1022 * @param type Type of the message, 0 for a raw message.
1023 * @param size Size of the message.
1024 * @param dst Neighbor to send message to.
1025 * @param c Connection this message belongs to.
1026 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1029 queue_add (void *cls, uint16_t type, size_t size,
1030 struct MeshPeer *dst,
1031 struct MeshConnection *c,
1032 struct MeshChannel *ch);
1036 * Free a transmission that was already queued with all resources
1037 * associated to the request.
1039 * @param queue Queue handler to cancel.
1040 * @param clear_cls Is it necessary to free associated cls?
1043 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1047 * @brief Get the next transmittable message from the queue.
1049 * This will be the head, except in the case of being a data packet
1050 * not allowed by the destination peer.
1052 * @param peer Destination peer.
1054 * @return The next viable MeshPeerQueue element to send to that peer.
1055 * NULL when there are no transmittable messages.
1057 struct MeshPeerQueue *
1058 queue_get_next (const struct MeshPeer *peer);
1062 * Core callback to write a queued packet to core buffer
1064 * @param cls Closure (peer info).
1065 * @param size Number of bytes available in buf.
1066 * @param buf Where the to write the message.
1068 * @return number of bytes written to buf
1071 queue_send (void *cls, size_t size, void *buf);
1075 * Dummy function to separate declarations from definitions in function list.
1078 __mesh_divider______________________________________________________________();
1082 * Get string description for tunnel state.
1084 * @param s Tunnel state.
1086 * @return String representation.
1089 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1091 static char buf[128];
1095 case MESH_TUNNEL_NEW:
1096 return "MESH_TUNNEL_NEW";
1097 case MESH_TUNNEL_SEARCHING:
1098 return "MESH_TUNNEL_SEARCHING";
1099 case MESH_TUNNEL_WAITING:
1100 return "MESH_TUNNEL_WAITING";
1101 case MESH_TUNNEL_READY:
1102 return "MESH_TUNNEL_READY";
1103 case MESH_TUNNEL_RECONNECTING:
1104 return "MESH_TUNNEL_RECONNECTING";
1107 sprintf (buf, "%u (UNKNOWN STATE)", s);
1114 * Get string description for tunnel state.
1116 * @param s Tunnel state.
1118 * @return String representation.
1121 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1125 case MESH_CONNECTION_NEW:
1126 return "MESH_CONNECTION_NEW";
1127 case MESH_CONNECTION_SENT:
1128 return "MESH_CONNECTION_SENT";
1129 case MESH_CONNECTION_READY:
1130 return "MESH_CONNECTION_READY";
1132 return "MESH_CONNECTION_STATE_ERROR";
1138 /******************************************************************************/
1139 /************************ PERIODIC FUNCTIONS ****************************/
1140 /******************************************************************************/
1143 * Periodically announce self id in the DHT
1145 * @param cls closure
1146 * @param tc task context
1149 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1151 struct PBlock block;
1153 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1155 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1159 * - Set data expiration in function of X
1160 * - Adapt X to churn
1162 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1164 block.id = my_full_id;
1165 GNUNET_DHT_put (dht_handle, /* DHT handle */
1166 &my_full_id.hashPubKey, /* Key to use */
1167 dht_replication_level, /* Replication level */
1168 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1169 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1170 sizeof (block), /* Size of the data */
1171 (const char *) &block, /* Data itself */
1172 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1173 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1174 NULL, /* Continuation */
1175 NULL); /* Continuation closure */
1177 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1181 /******************************************************************************/
1182 /****************** GENERAL HELPER FUNCTIONS ************************/
1183 /******************************************************************************/
1187 * Get the previous hop in a connection
1189 * @param c Connection.
1191 * @return Previous peer in the connection.
1193 static struct MeshPeer *
1194 connection_get_prev_hop (struct MeshConnection *c)
1198 if (0 == c->own_pos || c->path->length < 2)
1199 id = c->path->peers[0];
1201 id = c->path->peers[c->own_pos - 1];
1203 return peer_get_short (id);
1208 * Get the next hop in a connection
1210 * @param c Connection.
1212 * @return Next peer in the connection.
1214 static struct MeshPeer *
1215 connection_get_next_hop (struct MeshConnection *c)
1219 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1220 id = c->path->peers[c->path->length - 1];
1222 id = c->path->peers[c->own_pos + 1];
1224 return peer_get_short (id);
1229 * Check if client has registered with the service and has not disconnected
1231 * @param client the client to check
1233 * @return non-NULL if client exists in the global DLL
1235 static struct MeshClient *
1236 client_get (struct GNUNET_SERVER_Client *client)
1238 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1243 * Deletes a tunnel from a client (either owner or destination). To be used on
1246 * @param c Client whose tunnel to delete.
1247 * @param ch Channel which should be deleted.
1250 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1256 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1258 if (GNUNET_YES != res)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1261 if (c == ch->client)
1263 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1265 if (GNUNET_YES != res)
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1272 * Notify the appropriate client that a new incoming channel was created.
1274 * @param ch Channel that was created.
1277 send_local_channel_create (struct MeshChannel *ch)
1279 struct GNUNET_MESH_ChannelMessage msg;
1280 struct MeshTunnel2 *t = ch->t;
1282 if (NULL == ch->client)
1284 msg.header.size = htons (sizeof (msg));
1285 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1286 msg.channel_id = htonl (ch->id_dest);
1287 msg.port = htonl (ch->port);
1289 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1290 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1291 msg.opt = htonl (msg.opt);
1292 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1293 GNUNET_SERVER_notification_context_unicast (nc, ch->client->handle,
1294 &msg.header, GNUNET_NO);
1299 * Notify a client that the incoming tunnel is no longer valid.
1301 * @param ch Channel that is destroyed.
1302 * @param fwd Forward notification (owner->dest)?
1305 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1307 struct GNUNET_MESH_ChannelMessage msg;
1308 struct MeshClient *c;
1310 c = fwd ? ch->client : ch->owner;
1316 msg.header.size = htons (sizeof (msg));
1317 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1318 msg.channel_id = htonl (fwd ? ch->id_dest : ch->id);
1319 msg.port = htonl (0);
1320 memset (&msg.peer, 0, sizeof (msg.peer));
1321 msg.opt = htonl (0);
1322 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1323 &msg.header, GNUNET_NO);
1328 * Build a local ACK message and send it to a local client.
1330 * @param ch Channel on which to send the ACK.
1331 * @param c Client to whom send the ACK.
1332 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1335 send_local_ack (struct MeshChannel *ch,
1336 struct MeshClient *c,
1339 struct GNUNET_MESH_LocalAck msg;
1341 msg.header.size = htons (sizeof (msg));
1342 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1343 msg.channel_id = htonl (is_fwd ? ch->id : ch->id_dest);
1344 GNUNET_SERVER_notification_context_unicast (nc,
1352 * Pick a connection on which send the next data message.
1354 * @param t Tunnel on which to send the message.
1355 * @param fwd Is this a fwd message?
1357 * @return The connection on which to send the next message.
1359 static struct MeshConnection *
1360 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1362 struct MeshConnection *c;
1363 struct MeshConnection *best;
1364 struct MeshPeer *peer;
1365 unsigned int lowest_q;
1370 lowest_q = UINT_MAX;
1371 for (c = t->connection_head; NULL != c; c = c->next)
1373 if (MESH_CONNECTION_READY == c->state)
1375 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1376 if (NULL == peer->fc)
1381 if (peer->fc->queue_n < lowest_q)
1384 lowest_q = peer->fc->queue_n;
1393 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1394 * Encrypt data with the tunnel key.
1396 * @param t Tunnel whose key to use.
1397 * @param dst Destination for the encrypted data.
1398 * @param src Source of the plaintext.
1399 * @param size Size of the plaintext.
1400 * @param iv Initialization Vector to use.
1401 * @param fwd Is this a fwd message?
1404 tunnel_encrypt (struct MeshTunnel2 *t,
1405 void *dst, const void *src,
1406 size_t size, uint64_t iv, int fwd)
1408 memcpy (dst, src, size);
1413 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1414 * Decrypt data with the tunnel key.
1416 * @param t Tunnel whose key to use.
1417 * @param dst Destination for the plaintext.
1418 * @param src Source of the encrypted data.
1419 * @param size Size of the encrypted data.
1420 * @param iv Initialization Vector to use.
1421 * @param fwd Is this a fwd message?
1424 tunnel_decrypt (struct MeshTunnel2 *t,
1425 void *dst, const void *src,
1426 size_t size, uint64_t iv, int fwd)
1428 memcpy (dst, src, size);
1433 * Sends an already built message on a tunnel, properly registering
1434 * all used resources.
1436 * @param message Message to send. Function makes a copy of it.
1437 * If message is not hop-by-hop, decrements TTL of copy.
1438 * @param c Connection on which this message is transmitted.
1439 * @param ch Channel on which this message is transmitted.
1440 * @param fwd Is this a fwd message?
1443 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1444 struct MeshConnection *c,
1445 struct MeshChannel *ch,
1448 struct MeshPeer *neighbor;
1453 neighbor = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1454 if (NULL == neighbor)
1460 size = ntohs (message->size);
1461 data = GNUNET_malloc (size);
1462 memcpy (data, message, size);
1463 type = ntohs(message->type);
1465 if (GNUNET_MESSAGE_TYPE_MESH_FWD == type ||
1466 GNUNET_MESSAGE_TYPE_MESH_BCK == type)
1468 struct GNUNET_MESH_Encrypted *msg;
1471 msg = (struct GNUNET_MESH_Encrypted *) data;
1472 ttl = ntohl (msg->ttl);
1475 GNUNET_break_op (0);
1478 msg->ttl = htonl (ttl - 1);
1491 * Sends an already built message on a tunnel, choosing the best connection.
1493 * @param message Message to send. Function modifies it.
1494 * @param t Tunnel on which this message is transmitted.
1495 * @param ch Channel on which this message is transmitted.
1496 * @param fwd Is this a fwd message?
1499 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1500 struct MeshTunnel2 *t,
1501 struct MeshChannel *ch,
1504 struct MeshConnection *c;
1507 c = tunnel_get_connection (t, fwd);
1513 type = ntohs (msg->header.size);
1516 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1517 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1518 msg->cid = htonl (c->id);
1520 msg->ttl = default_ttl;
1526 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1531 * Sends an already built message on a channel, properly registering
1532 * all used resources and encrypting the message with the tunnel's key.
1534 * @param message Message to send. Function makes a copy of it.
1535 * @param ch Channel on which this message is transmitted.
1536 * @param fwd Is this a fwd message?
1539 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1540 struct MeshChannel *ch,
1543 struct GNUNET_MESH_Encrypted *msg;
1544 size_t size = ntohs (message->size);
1545 char *cbuf[size + sizeof (struct GNUNET_MESH_Encrypted)];
1549 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1550 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1552 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1553 msg->header.type = htons (type);
1554 msg->header.size = htons (size);
1555 msg->iv = GNUNET_htonll (iv);
1556 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1558 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1563 * Sends an already built message directly to a peer.
1565 * @param message Message to send. Function makes a copy of it.
1566 * @param peer Tunnel on which this message is transmitted.
1569 send_prebuilt_message_peer (const struct GNUNET_MessageHeader *message,
1570 struct MeshPeer *peer)
1582 size = ntohs (message->size);
1583 data = GNUNET_malloc (size);
1584 memcpy (data, message, size);
1585 type = ntohs(message->type);
1597 * Sends a CREATE CONNECTION message for a path to a peer.
1598 * Changes the connection and tunnel states if necessary.
1600 * @param connection Connection to create.
1603 send_connection_create (struct MeshConnection *connection)
1605 struct MeshPeer *neighbor;
1606 struct MeshTunnel2 *t;
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1610 neighbor = connection_get_next_hop (connection);
1611 queue_add (connection,
1612 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1613 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1614 (connection->path->length *
1615 sizeof (struct GNUNET_PeerIdentity)),
1619 if (MESH_TUNNEL_SEARCHING == t->state)
1620 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1621 if (MESH_CONNECTION_NEW == connection->state)
1622 connection_change_state (connection, MESH_CONNECTION_SENT);
1627 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1630 * @param connection Connection to confirm.
1633 send_connection_ack (struct MeshConnection *connection)
1635 struct MeshPeer *neighbor;
1636 struct MeshTunnel2 *t;
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1640 neighbor = connection_get_prev_hop (connection);
1641 queue_add (connection,
1642 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1643 sizeof (struct GNUNET_MESH_ConnectionACK),
1647 if (MESH_TUNNEL_NEW == t->state)
1648 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1653 * Build an ACK message and queue it to send to the given peer.
1655 * @param peer Peer to whom send the ACK.
1656 * @param ack Value of the ACK.
1659 send_ack (struct MeshPeer *peer, uint32_t ack)
1661 struct GNUNET_MESH_ACK msg;
1663 msg.header.size = htons (sizeof (msg));
1664 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1665 msg.ack = htonl (ack);
1667 send_prebuilt_message_peer (&msg.header, peer);
1672 * Core callback to write a pre-constructed data packet to core buffer
1674 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1675 * @param size Number of bytes available in buf.
1676 * @param buf Where the to write the message.
1678 * @return number of bytes written to buf
1681 send_core_data_raw (void *cls, size_t size, void *buf)
1683 struct GNUNET_MessageHeader *msg = cls;
1686 GNUNET_assert (NULL != msg);
1687 total_size = ntohs (msg->size);
1689 if (total_size > size)
1694 memcpy (buf, msg, total_size);
1701 * Function to send a create path packet to a peer.
1703 * @param cls closure
1704 * @param size number of bytes available in buf
1705 * @param buf where the callee should write the message
1706 * @return number of bytes written to buf
1709 send_core_connection_create (void *cls, size_t size, void *buf)
1711 struct MeshConnection *c = cls;
1712 struct GNUNET_MESH_ConnectionCreate *msg;
1713 struct GNUNET_PeerIdentity *peer_ptr;
1714 struct MeshPeerPath *p = c->path;
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1720 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1721 p->length * sizeof (struct GNUNET_PeerIdentity);
1723 if (size < size_needed || NULL == buf)
1728 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1729 msg->header.size = htons (size_needed);
1730 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1731 msg->cid = htonl (c->id);
1733 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1734 for (i = 0; i < p->length; i++)
1736 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1746 * Function to send a create path packet to a peer.
1748 * @param cls closure
1749 * @param size number of bytes available in buf
1750 * @param buf where the callee should write the message
1751 * @return number of bytes written to buf
1754 send_core_channel_create (void *cls, size_t size, void *buf)
1756 // struct MeshChannel *ch = cls;
1757 // struct MeshTunnel2 *t = ch->t;
1758 // struct GNUNET_MESH_ConnectionCreate *msg;
1759 // struct GNUNET_PeerIdentity *peer_ptr;
1764 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CHANNEL CREATE...\n");
1766 // sizeof (struct GNUNET_MESH_ConnectionCreate) +
1767 // p->length * sizeof (struct GNUNET_PeerIdentity);
1769 // if (size < size_needed || NULL == buf)
1771 // GNUNET_break (0);
1774 // msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1775 // msg->header.size = htons (size_needed);
1776 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1777 // msg->tid = ntohl (t->id.tid);
1780 // if (GNUNET_YES == ch->nobuffer)
1781 // opt |= GNUNET_MESH_OPTION_NOBUFFER;
1782 // if (GNUNET_YES == ch->reliable)
1783 // opt |= GNUNET_MESH_OPTION_RELIABLE;
1784 // msg->opt = htonl (opt);
1785 // msg->port = htonl (ch->port);
1787 // peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1788 // for (i = 0; i < p->length; i++)
1790 // GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1793 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 // "CREATE PATH (%u bytes long) sent!\n", size_needed);
1800 * Creates a path ack message in buf and frees all unused resources.
1802 * @param cls closure (MeshTransmissionDescriptor)
1803 * @param size number of bytes available in buf
1804 * @param buf where the callee should write the message
1805 * @return number of bytes written to buf
1808 send_core_connection_ack (void *cls, size_t size, void *buf)
1810 struct GNUNET_MESH_ConnectionACK *msg = buf;
1811 struct MeshConnection *c = cls;
1812 struct MeshTunnel2 *t = c->t;
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1815 GNUNET_assert (NULL != t);
1816 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1821 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1822 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1823 GNUNET_CRYPTO_hash_xor (&GNUNET_PEER_resolve2 (t->peer->id)->hashPubKey,
1824 &my_full_id.hashPubKey,
1826 msg->cid = htonl (c->id);
1828 /* TODO add signature */
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1831 return sizeof (struct GNUNET_MESH_ConnectionACK);
1836 * Iterator over all the peers to remove the oldest not-used entry.
1838 * @param cls Closure (unsued).
1839 * @param key ID of the peer.
1840 * @param value Peer_Info of the peer.
1845 peer_timeout (void *cls,
1846 const struct GNUNET_HashCode *key,
1854 * Retrieve the MeshPeer stucture associated with the peer, create one
1855 * and insert it in the appropriate structures if the peer is not known yet.
1857 * @param peer Full identity of the peer.
1859 * @return Existing or newly created peer info.
1861 static struct MeshPeer *
1862 peer_get (const struct GNUNET_PeerIdentity *peer_id)
1864 struct MeshPeer *peer;
1866 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
1869 peer = (struct MeshPeer *) GNUNET_malloc (sizeof (struct MeshPeer));
1870 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1872 GNUNET_CONTAINER_multihashmap_iterate (peers,
1876 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
1877 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1878 peer->id = GNUNET_PEER_intern (peer_id);
1880 peer->last_contact = GNUNET_TIME_absolute_get();
1887 * Retrieve the MeshPeer stucture associated with the peer, create one
1888 * and insert it in the appropriate structures if the peer is not known yet.
1890 * @param peer Short identity of the peer.
1892 * @return Existing or newly created peer info.
1894 static struct MeshPeer *
1895 peer_get_short (const GNUNET_PEER_Id peer)
1897 return peer_get (GNUNET_PEER_resolve2 (peer));
1902 * Select which PID to POLL for, to compensate for lost messages.
1904 * @param pi Peer we want to poll.
1906 * @return PID to use, (last sent).
1909 peer_get_first_pid (struct MeshPeer *p)
1911 return p->fc->last_pid_sent;
1916 * Get a cost of a path for a peer considering existing tunnel connections.
1918 * @param peer Peer towards which the path is considered.
1919 * @param path Candidate path.
1921 * @return Cost of the path (path length + number of overlapping nodes)
1924 peer_get_path_cost (const struct MeshPeer *peer,
1925 const struct MeshPeerPath *path)
1927 struct MeshConnection *c;
1928 unsigned int overlap;
1936 GNUNET_assert (NULL != peer->tunnel);
1938 for (i = 0; i < path->length; i++)
1940 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
1942 for (j = 0; j < c->path->length; j++)
1944 if (path->peers[i] == c->path->peers[j])
1952 return (path->length + overlap) * (path->score * -1);
1957 * Choose the best path towards a peer considering the tunnel properties.
1959 * @param peer The destination peer.
1961 * @return Best current known path towards the peer, if any.
1963 static struct MeshPeerPath *
1964 peer_get_best_path (const struct MeshPeer *peer)
1966 struct MeshPeerPath *best_p;
1967 struct MeshPeerPath *p;
1968 struct MeshConnection *c;
1969 unsigned int best_cost;
1972 best_cost = UINT_MAX;
1974 for (p = peer->path_head; NULL != p; p = p->next)
1976 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
1980 continue; /* If path is in use in a connection, skip it. */
1982 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
1993 * Try to establish a new connection to this peer in the given tunnel.
1994 * If the peer doesn't have any path to it yet, try to get one.
1995 * If the peer already has some path, send a CREATE CONNECTION towards it.
1997 * @param peer PeerInfo of the peer.
2000 peer_connect (struct MeshPeer *peer)
2002 struct MeshTunnel2 *t;
2003 struct MeshPeerPath *p;
2004 struct MeshConnection *c;
2007 if (NULL != peer->path_head)
2009 p = peer_get_best_path (peer);
2012 c = tunnel_use_path (t, p);
2013 send_connection_create (c);
2016 else if (NULL == peer->dhtget)
2018 const struct GNUNET_PeerIdentity *id;
2020 id = GNUNET_PEER_resolve2 (peer->id);
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2022 " Starting DHT GET for peer %s\n", GNUNET_i2s (id));
2023 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2024 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2025 &id->hashPubKey, /* key to search */
2026 dht_replication_level, /* replication level */
2027 GNUNET_DHT_RO_RECORD_ROUTE |
2028 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2030 0, /* xquery bits */
2031 &dht_get_id_handler, peer);
2032 if (MESH_TUNNEL_NEW == t->state)
2033 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "There is no path but the DHT GET is already started.\n");
2044 * @brief Re-initiate traffic to this peer if necessary.
2046 * Check if there is traffic queued towards this peer
2047 * and the core transmit handle is NULL (traffic was stalled).
2048 * If so, call core tmt rdy.
2050 * @param peer_id Short ID of peer to which initiate traffic.
2053 peer_unlock_queue (GNUNET_PEER_Id peer_id)
2055 struct MeshPeer *peer;
2056 struct MeshPeerQueue *q;
2059 peer = peer_get_short (peer_id);
2060 if (NULL != peer->fc->core_transmit)
2061 return; /* Already unlocked */
2063 q = queue_get_next (peer);
2065 return; /* Nothing to transmit */
2068 peer->fc->core_transmit =
2069 GNUNET_CORE_notify_transmit_ready (core_handle,
2072 GNUNET_TIME_UNIT_FOREVER_REL,
2073 GNUNET_PEER_resolve2 (peer->id),
2081 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
2083 * @param peer Neighbor to whom cancel the transmissions.
2084 * @param t Tunnel which to cancel.
2087 peer_cancel_queues (struct MeshPeer *peer, struct MeshTunnel2 *t)
2089 struct MeshPeerQueue *q;
2090 struct MeshPeerQueue *next;
2091 struct MeshFlowControl *fc;
2093 if (NULL == peer || NULL == peer->fc)
2099 for (q = fc->queue_head; NULL != q; q = next)
2102 if (q->peer->tunnel == t)
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 "peer_cancel_queue %s\n",
2106 GNUNET_MESH_DEBUG_M2S (q->type));
2107 queue_destroy (q, GNUNET_YES);
2110 if (NULL == fc->queue_head)
2112 if (NULL != fc->core_transmit)
2114 GNUNET_CORE_notify_transmit_ready_cancel (fc->core_transmit);
2115 fc->core_transmit = NULL;
2117 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2119 GNUNET_SCHEDULER_cancel (fc->poll_task);
2120 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2127 * Destroy the peer_info and free any allocated resources linked to it
2129 * @param peer The peer_info to destroy.
2131 * @return GNUNET_OK on success
2134 peer_destroy (struct MeshPeer *peer)
2136 struct GNUNET_PeerIdentity id;
2137 struct MeshPeerPath *p;
2138 struct MeshPeerPath *nextp;
2140 GNUNET_PEER_resolve (peer->id, &id);
2141 GNUNET_PEER_change_rc (peer->id, -1);
2144 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2148 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2150 if (NULL != peer->dhtget)
2152 GNUNET_DHT_get_stop (peer->dhtget);
2154 p = peer->path_head;
2158 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2162 tunnel_destroy_empty (peer->tunnel);
2169 * Remove all paths that rely on a direct connection between p1 and p2
2170 * from the peer itself and notify all tunnels about it.
2172 * @param peer PeerInfo of affected peer.
2173 * @param p1 GNUNET_PEER_Id of one peer.
2174 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2177 * TODO: optimize (see below)
2180 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2183 struct MeshPeerPath *p;
2184 struct MeshPeerPath *next;
2185 struct MeshPeer *peer_d;
2187 unsigned int destroyed;
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2192 for (p = peer->path_head; NULL != p; p = next)
2195 for (i = 0; i < (p->length - 1); i++)
2197 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2198 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2200 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2211 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2213 peer_d = peer_get_short (d); // FIXME
2214 next = peer_get_best_path (peer_d);
2215 tunnel_use_path (peer->tunnel, next);
2216 peer_connect (peer_d);
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2223 * Add the path to the peer and update the path used to reach it in case this
2226 * @param peer_info Destination peer to add the path to.
2227 * @param path New path to add. Last peer must be the peer in arg 1.
2228 * Path will be either used of freed if already known.
2229 * @param trusted Do we trust that this path is real?
2232 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2235 struct MeshPeerPath *aux;
2239 if ((NULL == peer_info) || (NULL == path))
2242 path_destroy (path);
2245 if (path->peers[path->length - 1] != peer_info->id)
2248 path_destroy (path);
2251 if (2 >= path->length && GNUNET_NO == trusted)
2253 /* Only allow CORE to tell us about direct paths */
2254 path_destroy (path);
2257 for (l = 1; l < path->length; l++)
2259 if (path->peers[l] == myid)
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2262 for (l2 = 0; l2 < path->length - l; l2++)
2264 path->peers[l2] = path->peers[l + l2];
2269 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2274 struct GNUNET_PeerIdentity id;
2276 GNUNET_PEER_resolve (peer_info->id, &id);
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2278 path->length, GNUNET_i2s (&id));
2281 l = path_get_length (path);
2284 path_destroy (path);
2288 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2289 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2291 l2 = path_get_length (aux);
2294 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2295 peer_info->path_tail, aux, path);
2300 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2302 path_destroy (path);
2307 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2314 * Add the path to the origin peer and update the path used to reach it in case
2315 * this is the shortest.
2316 * The path is given in peer_info -> destination, therefore we turn the path
2317 * upside down first.
2319 * @param peer_info Peer to add the path to, being the origin of the path.
2320 * @param path New path to add after being inversed.
2321 * Path will be either used or freed.
2322 * @param trusted Do we trust that this path is real?
2325 peer_add_path_to_origin (struct MeshPeer *peer_info,
2326 struct MeshPeerPath *path, int trusted)
2329 peer_add_path (peer_info, path, trusted);
2335 * Function called if the connection to the peer has been stalled for a while,
2336 * possibly due to a missed ACK. Poll the peer about its ACK status.
2338 * @param cls Closure (poll ctx).
2339 * @param tc TaskContext.
2342 peer_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2344 struct MeshFlowControl *fc = cls;
2345 struct GNUNET_MESH_Poll msg;
2346 GNUNET_PEER_Id peer;
2348 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2349 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2355 peer = fc->peer->id;
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n",
2358 GNUNET_i2s (GNUNET_PEER_resolve2 (peer)));
2360 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2361 msg.header.size = htons (sizeof (msg));
2362 msg.pid = htonl (fc->last_pid_sent);
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2364 send_prebuilt_message_peer (&msg.header, peer_get_short (peer));
2365 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2366 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2372 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2373 * to obtain a local peer -> destination path and interning the peer ids.
2375 * @return Newly allocated and created path
2377 static struct MeshPeerPath *
2378 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2379 unsigned int get_path_length,
2380 const struct GNUNET_PeerIdentity *put_path,
2381 unsigned int put_path_length)
2383 struct MeshPeerPath *p;
2389 GNUNET_PEER_change_rc (myid, 1);
2390 i = get_path_length;
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2392 for (i--; i >= 0; i--)
2394 id = GNUNET_PEER_intern (&get_path[i]);
2395 if (p->length > 0 && id == p->peers[p->length - 1])
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2398 GNUNET_PEER_change_rc (id, -1);
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2403 GNUNET_i2s (&get_path[i]));
2405 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2406 p->peers[p->length - 1] = id;
2409 i = put_path_length;
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2411 for (i--; i >= 0; i--)
2413 id = GNUNET_PEER_intern (&put_path[i]);
2416 /* PUT path went through us, so discard the path up until now and start
2417 * from here to get a much shorter (and loop-free) path.
2422 if (p->length > 0 && id == p->peers[p->length - 1])
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2425 GNUNET_PEER_change_rc (id, -1);
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2430 GNUNET_i2s (&put_path[i]));
2432 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2433 p->peers[p->length - 1] = id;
2437 if (get_path_length > 0)
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2439 GNUNET_i2s (&get_path[0]));
2440 if (put_path_length > 0)
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2442 GNUNET_i2s (&put_path[0]));
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2445 for (i = 0; i < p->length; i++)
2447 struct GNUNET_PeerIdentity peer_id;
2449 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2451 GNUNET_i2s (&peer_id));
2459 * Adds a path to the peer_infos of all the peers in the path
2461 * @param p Path to process.
2462 * @param confirmed Whether we know if the path works or not.
2465 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2469 /* TODO: invert and add */
2470 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2471 for (i++; i < p->length; i++)
2473 struct MeshPeer *aux;
2474 struct MeshPeerPath *copy;
2476 aux = peer_get_short (p->peers[i]);
2477 copy = path_duplicate (p);
2478 copy->length = i + 1;
2479 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2485 * Search for a channel among the channels for a client
2487 * @param c the client whose channels to search in
2488 * @param chid the local id of the channel
2490 * @return channel handler, NULL if doesn't exist
2492 static struct MeshChannel *
2493 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2495 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2497 GNUNET_break_op (0);
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2501 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2502 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2503 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2508 * Search for a tunnel by global ID using PEER_ID
2510 * @param pi owner of the tunnel
2511 * @param tid global tunnel number
2513 * @return tunnel handler, NULL if doesn't exist
2515 static struct MeshChannel *
2516 channel_get_by_pi (GNUNET_PEER_Id pi, MESH_ChannelNumber tid)
2518 // struct GNUNET_HashCode hash;
2520 // return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash); FIXME
2526 * Search for a tunnel by global ID using full PeerIdentities
2528 * @param oid owner of the tunnel
2529 * @param tid global tunnel number
2531 * @return tunnel handler, NULL if doesn't exist
2533 static struct MeshChannel *
2534 channel_get (const struct GNUNET_PeerIdentity *oid, MESH_ChannelNumber tid)
2536 return channel_get_by_pi (GNUNET_PEER_search (oid), tid);
2541 * Change the tunnel state.
2543 * @param t Tunnel whose state to change.
2544 * @param state New state.
2547 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2550 "Tunnel %s state was %s\n",
2551 GNUNET_i2s (GNUNET_PEER_resolve2 (t->peer->id)),
2552 GNUNET_MESH_DEBUG_TS2S (t->state));
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "Tunnel %s state is now %s\n",
2555 GNUNET_i2s (GNUNET_PEER_resolve2 (t->peer->id)),
2556 GNUNET_MESH_DEBUG_TS2S (state));
2562 connection_change_state (struct MeshConnection* c,
2563 enum MeshConnectionState state)
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566 "Connection %s[%X] state was %s\n",
2567 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id,
2568 GNUNET_MESH_DEBUG_CS2S (c->state));
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2570 "Connection %s[%X] state is now %s\n",
2571 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id,
2572 GNUNET_MESH_DEBUG_CS2S (state));
2578 * Add a client to a channel, initializing all needed data structures.
2580 * @param ch Channel to which add the client.
2581 * @param c Client which to add to the channel.
2584 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2586 if (NULL != ch->client)
2592 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2594 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2603 static struct MeshConnection *
2604 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2606 struct MeshConnection *c;
2607 unsigned int own_pos;
2609 c = GNUNET_new (struct MeshConnection);
2610 for (own_pos = 0; own_pos < p->length; own_pos++)
2612 if (p->peers[own_pos] == myid)
2615 if (own_pos > p->length - 1)
2620 c->own_pos = own_pos;
2622 c->id = t->next_cid++;
2624 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
2628 c->fwd_maintenance_task =
2629 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
2630 &connection_fwd_keepalive, c);
2637 * Notifies a tunnel that a connection has broken that affects at least
2638 * some of its peers. Sends a notification towards the root of the tree.
2639 * In case the peer is the owner of the tree, notifies the client that owns
2640 * the tunnel and tries to reconnect.
2642 * @param t Tunnel affected.
2643 * @param p1 Peer that got disconnected from p2.
2644 * @param p2 Peer that got disconnected from p1.
2646 * @return Short ID of the peer disconnected (either p1 or p2).
2647 * 0 if the tunnel remained unaffected.
2649 static GNUNET_PEER_Id
2650 tunnel_notify_connection_broken (struct MeshTunnel2* t,
2651 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
2653 // if (myid != p1 && myid != p2) FIXME
2658 // if (tree_get_predecessor (t->tree) != 0)
2660 // /* We are the peer still connected, notify owner of the disconnection. */
2661 // struct GNUNET_MESH_PathBroken msg;
2662 // struct GNUNET_PeerIdentity neighbor;
2664 // msg.header.size = htons (sizeof (msg));
2665 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2666 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2667 // msg.tid = htonl (t->id.tid);
2668 // msg.peer1 = my_full_id;
2669 // GNUNET_PEER_resolve (pid, &msg.peer2);
2670 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2671 // send_prebuilt_message (&msg.header, &neighbor, t);
2678 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2680 * @param ch Channel this is about.
2681 * @param fwd Is for FWD traffic? (ACK dest->owner)
2684 channel_send_data_ack (struct MeshChannel *ch, int fwd)
2686 struct GNUNET_MESH_DataACK msg;
2687 struct MeshChannelReliability *rel;
2688 struct MeshReliableMessage *copy;
2692 if (GNUNET_NO == ch->reliable)
2697 rel = fwd ? ch->bck_rel : ch->fwd_rel;
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 "send_data_ack for %u\n",
2702 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2703 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2704 msg.header.size = htons (sizeof (msg));
2705 msg.chid = htonl (ch->id);
2706 msg.mid = htonl (rel->mid_recv - 1);
2708 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2710 delta = copy->mid - rel->mid_recv;
2713 mask = 0x1LL << delta;
2714 msg.futures |= mask;
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2717 copy->mid, delta, mask, msg.futures);
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2721 send_prebuilt_message_channel (&msg.header, ch, fwd);
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2727 * Send an ACK informing the predecessor about the available buffer space.
2729 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2730 * the ACK itself goes "back" (towards root).
2732 * @param c Connection on which to send the ACK.
2733 * @param fwd Is this FWD ACK? (Going dest->owner)
2736 connection_send_ack (struct MeshConnection *c, int fwd)
2738 struct MeshFlowControl *next_fc;
2739 struct MeshFlowControl *prev_fc;
2740 struct MeshPeer *next;
2741 struct MeshPeer *prev;
2745 next = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
2746 prev = fwd ? connection_get_prev_hop (c) : connection_get_next_hop (c);
2750 /* Check if we need to transmit the ACK */
2751 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 " last pid recv: %u, last ack sent: %u\n",
2756 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2760 /* Ok, ACK might be necessary, what PID to ACK? */
2761 delta = next_fc->queue_max - next_fc->queue_n;
2762 ack = prev_fc->last_pid_recv + delta;
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 " last pid %u, last ack %u, qmax %u, q %u\n",
2766 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2767 next_fc->queue_max, next_fc->queue_n);
2768 if (ack == prev_fc->last_ack_sent)
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2774 prev_fc->last_ack_sent = ack;
2775 send_ack (prev, ack);
2780 * Send an ACK informing the client about available buffer space.
2782 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2783 * the ACK itself goes "back" (towards root).
2785 * @param ch Channel on which to send the ACK, NULL if unknown.
2786 * @param fwd Is this FWD ACK? (Going dest->owner)
2789 // channel_send_ack (struct MeshChannel *ch, uint16_t type, int fwd)
2791 // struct MeshChannelReliability *rel;
2792 // struct MeshFlowControl *next_fc;
2793 // struct MeshFlowControl *prev_fc;
2794 // struct MeshClient *c;
2795 // struct MeshClient *o;
2796 // GNUNET_PEER_Id hop;
2797 // uint32_t delta_mid;
2801 // rel = fwd ? ch->fwd_rel : ch->bck_rel;
2802 // c = fwd ? ch->client : ch->owner;
2803 // o = fwd ? ch->owner : ch->client;
2804 // hop = fwd ? connection_get_prev_hop (cn) : connection_get_next_hop (cn);
2805 // next_fc = fwd ? &t->next_fc : &t->prev_fc;
2806 // prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2810 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2811 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2812 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 // "ACK due to %s\n",
2814 // GNUNET_MESH_DEBUG_M2S (type));
2815 // if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2817 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2820 // if (GNUNET_YES == t->reliable && NULL != c)
2821 // tunnel_send_data_ack (t, fwd);
2823 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2824 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2825 // case GNUNET_MESSAGE_TYPE_MESH_ACK:
2826 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2828 // case GNUNET_MESSAGE_TYPE_MESH_POLL:
2829 // case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2830 // t->force_ack = GNUNET_YES;
2833 // GNUNET_break (0);
2836 // /* Check if we need to transmit the ACK */
2838 // prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2839 // GNUNET_NO == t->force_ack)
2841 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2842 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 // " last pid recv: %u, last ack sent: %u\n",
2844 // prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2848 // /* Ok, ACK might be necessary, what PID to ACK? */
2849 // delta = t->queue_max - next_fc->queue_n;
2850 // if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2851 // delta_mid = rel->mid_sent - rel->head_sent->mid;
2854 // if (0 > delta || (GNUNET_YES == t->reliable &&
2856 // (10 < rel->n_sent || 64 <= delta_mid)))
2858 // if (NULL != o && delta > 1)
2860 // ack = prev_fc->last_pid_recv + delta;
2861 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2862 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2863 // " last pid %u, last ack %u, qmax %u, q %u\n",
2864 // prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2865 // t->queue_max, next_fc->queue_n);
2866 // if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2868 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2872 // prev_fc->last_ack_sent = ack;
2874 // send_local_ack (t, o, fwd);
2875 // else if (0 != hop)
2876 // send_ack (t, hop, ack);
2878 // GNUNET_break (GNUNET_YES == t->destroy);
2879 // t->force_ack = GNUNET_NO;
2884 * Modify the mesh message TID from global to local and send to client.
2886 * @param ch Channel on which to send the message.
2887 * @param msg Message to modify and send.
2888 * @param c Client to send to.
2889 * @param tid Tunnel ID to use (c can be both owner and client).
2892 channel_send_client_to_tid (struct MeshChannel *ch,
2893 const struct GNUNET_MESH_Data *msg,
2894 struct MeshClient *c, MESH_ChannelNumber id)
2896 struct GNUNET_MESH_LocalData *copy;
2897 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2898 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2900 if (size < sizeof (struct GNUNET_MessageHeader))
2902 GNUNET_break_op (0);
2910 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2911 memcpy (©[1], &msg[1], size);
2912 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2913 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2914 copy->id = htonl (id);
2915 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2916 ©->header, GNUNET_NO);
2920 * Modify the data message ID from global to local and send to client.
2922 * @param ch Channel on which to send the message.
2923 * @param msg Message to modify and send.
2924 * @param fwd Forward?
2927 channel_send_client_data (struct MeshChannel *ch,
2928 const struct GNUNET_MESH_Data *msg,
2932 channel_send_client_to_tid (ch, msg, ch->client, ch->id_dest);
2934 channel_send_client_to_tid (ch, msg, ch->owner, ch->id);
2939 * Send up to 64 buffered messages to the client for in order delivery.
2941 * @param ch Channel on which to empty the message buffer.
2942 * @param c Client to send to.
2943 * @param rel Reliability structure to corresponding peer.
2944 * If rel == bck_rel, this is FWD data.
2947 channel_send_client_buffered_data (struct MeshChannel *ch,
2948 struct MeshClient *c,
2949 struct MeshChannelReliability *rel)
2951 struct MeshReliableMessage *copy;
2952 struct MeshReliableMessage *next;
2954 if (GNUNET_NO == ch->reliable)
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2961 for (copy = rel->head_recv; NULL != copy; copy = next)
2964 if (copy->mid == rel->mid_recv)
2966 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 " have %u! now expecting %u\n",
2970 copy->mid, rel->mid_recv + 1);
2971 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
2973 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2979 " don't have %u, next is %u\n",
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2990 * We have received a message out of order, buffer it until we receive
2991 * the missing one and we can feed the rest to the client.
2993 * @param msg Message to buffer.
2994 * @param rel Reliability data to the corresponding direction.
2997 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
2998 struct MeshChannelReliability *rel)
3000 struct MeshReliableMessage *copy;
3001 struct MeshReliableMessage *prev;
3005 size = ntohs (msg->header.size);
3006 mid = ntohl (msg->mid);
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3009 copy = GNUNET_malloc (sizeof (*copy) + size);
3012 memcpy (©[1], msg, size);
3014 // FIXME do something better than O(n), although n < 64...
3015 // FIXME start from the end (most messages are the latest ones)
3016 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3019 if (GMC_is_pid_bigger (prev->mid, mid))
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3022 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3028 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3034 * Destroy a reliable message after it has been acknowledged, either by
3035 * direct mid ACK or bitfield. Updates the appropriate data structures and
3036 * timers and frees all memory.
3038 * @param copy Message that is no longer needed: remote peer got it.
3041 rel_message_free (struct MeshReliableMessage *copy)
3043 struct MeshChannelReliability *rel;
3044 struct GNUNET_TIME_Relative time;
3047 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3048 rel->expected_delay.rel_value *= 7;
3049 rel->expected_delay.rel_value += time.rel_value;
3050 rel->expected_delay.rel_value /= 8;
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3055 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3057 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3059 rel->retry_timer = rel->expected_delay;
3060 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3066 * Destroy all reliable messages queued for a channel,
3067 * during a channel destruction.
3068 * Frees the reliability structure itself.
3070 * @param rel Reliability data for a channel.
3073 channel_rel_free_all (struct MeshChannelReliability *rel)
3075 struct MeshReliableMessage *copy;
3076 struct MeshReliableMessage *next;
3081 for (copy = rel->head_recv; NULL != copy; copy = next)
3084 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3087 for (copy = rel->head_sent; NULL != copy; copy = next)
3090 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3093 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3094 GNUNET_SCHEDULER_cancel (rel->retry_task);
3100 * Mark future messages as ACK'd.
3102 * @param rel Reliability data.
3103 * @param msg DataACK message with a bitfield of future ACK'd messages.
3106 channel_rel_free_sent (struct MeshChannelReliability *rel,
3107 const struct GNUNET_MESH_DataACK *msg)
3109 struct MeshReliableMessage *copy;
3110 struct MeshReliableMessage *next;
3117 bitfield = msg->futures;
3118 mid = ntohl (msg->mid);
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3120 "free_sent_reliable %u %llX\n",
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123 " rel %p, head %p\n",
3124 rel, rel->head_sent);
3125 for (i = 0, copy = rel->head_sent;
3126 i < 64 && NULL != copy && 0 != bitfield;
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3130 " trying bit %u (mid %u)\n",
3133 if (0 == (bitfield & mask))
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3137 /* Bit was set, clear the bit from the bitfield */
3140 /* The i-th bit was set. Do we have that copy? */
3141 /* Skip copies with mid < target */
3142 target = mid + i + 1;
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3144 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3147 /* Did we run out of copies? (previously freed, it's ok) */
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3154 /* Did we overshoot the target? (previously freed, it's ok) */
3155 if (GMC_is_pid_bigger (copy->mid, target))
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3161 /* Now copy->mid == target, free it */
3163 rel_message_free (copy);
3166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3171 * We haven't received an ACK after a certain time: restransmit the message.
3173 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3174 * @param tc TaskContext.
3177 channel_retransmit_message (void *cls,
3178 const struct GNUNET_SCHEDULER_TaskContext *tc)
3180 struct MeshChannelReliability *rel = cls;
3181 struct MeshReliableMessage *copy;
3182 struct MeshPeerQueue *q;
3183 struct MeshPeer *pi;
3184 struct MeshChannel *ch;
3185 struct MeshConnection *c;
3186 struct GNUNET_MESH_Data *payload;
3189 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3190 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3194 copy = rel->head_sent;
3201 /* Search the message to be retransmitted in the outgoing queue.
3202 * Check only the queue for the connection that is going to be used,
3203 * if the message is stuck in some other connection's queue we shouldn't
3205 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3206 * the old connection could be temporary stalled or the queue happened to
3207 * be long at time of insertion.
3208 * - not sending the new one could cause terrible delays the old connection
3211 payload = (struct GNUNET_MESH_Data *) ©[1];
3212 fwd = (rel == ch->fwd_rel);
3213 c = tunnel_get_connection(ch->t, fwd);
3214 pi = connection_get_next_hop (c);
3215 for (q = pi->fc->queue_head; NULL != q; q = q->next)
3217 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3219 struct GNUNET_MESH_Data *queued_data = q->cls;
3221 if (queued_data->mid == payload->mid)
3226 /* Message not found in the queue that we are going to use. */
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3231 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3232 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3239 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3240 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3241 &channel_retransmit_message,
3247 * Send keepalive packets for a connection.
3249 * @param c Connection to keep alive..
3250 * @param fwd Is this a FWD keepalive? (owner -> dest).
3253 connection_keepalive (struct MeshConnection *c, int fwd)
3255 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3256 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3260 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3261 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 "sending %s keepalive for connection %s[%d]\n",
3265 fwd ? "FWD" : "BCK",
3266 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3269 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3270 msg->header.size = htons (size);
3271 msg->header.type = htons (type);
3272 msg->cid = htonl (c->id);
3273 msg->tid = c->t->id;
3275 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3280 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3282 * @param c Connection for which to send the message.
3283 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3286 connection_recreate (struct MeshConnection *c, int fwd)
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3290 send_connection_create (c);
3292 send_connection_ack (c);
3297 * Generic connection timer management.
3298 * Depending on the role of the peer in the connection will send the
3299 * appropriate message (build or keepalive)
3301 * @param c Conncetion to maintain.
3302 * @param fwd Is FWD?
3305 connection_maintain (struct MeshConnection *c, int fwd)
3307 if (MESH_TUNNEL_SEARCHING == c->t->state)
3309 /* TODO DHT GET with RO_BART */
3314 case MESH_CONNECTION_NEW:
3316 case MESH_CONNECTION_SENT:
3317 connection_recreate (c, fwd);
3319 case MESH_CONNECTION_READY:
3320 connection_keepalive (c, fwd);
3329 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3331 struct MeshConnection *c = cls;
3333 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3334 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3337 connection_keepalive (c, GNUNET_YES);
3338 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3339 &connection_fwd_keepalive,
3345 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3347 struct MeshConnection *c = cls;
3349 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3350 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3353 connection_keepalive (c, GNUNET_NO);
3354 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3355 &connection_bck_keepalive,
3361 * Send a message to all peers in this connection that the connection
3362 * is no longer valid.
3364 * If some peer should not receive the message, it should be zero'ed out
3365 * before calling this function.
3367 * @param c The connection whose peers to notify.
3370 connection_send_destroy (struct MeshConnection *c)
3372 struct GNUNET_MESH_ConnectionDestroy msg;
3374 msg.header.size = htons (sizeof (msg));
3375 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3376 msg.cid = htonl (c->id);
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3379 " sending tunnel destroy for connection %s[%X]\n",
3380 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3383 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3384 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3389 * Send a message to all clients in this channel that the channel
3390 * is no longer valid.
3392 * If some peer or client should not receive the message,
3393 * should be zero'ed out before calling this function.
3395 * @param ch The channel whose clients to notify.
3398 channel_send_destroy (struct MeshChannel *ch)
3400 struct GNUNET_MESH_ChannelDestroy msg;
3402 msg.header.size = htons (sizeof (msg));
3403 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3404 msg.chid = htonl (ch->id);
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3406 " sending tunnel destroy for channel %s:%X\n",
3407 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->t->peer->id)),
3410 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3411 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3413 if (NULL != ch->owner)
3415 send_local_channel_destroy (ch, GNUNET_NO);
3417 if (NULL != ch->client)
3419 send_local_channel_destroy (ch, GNUNET_YES);
3425 tunnel_destroy (struct MeshTunnel2 *t)
3427 struct MeshClient *c;
3428 struct GNUNET_HashCode hash;
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3438 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3442 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3443 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3449 if (0 != t->prev_hop)
3451 peer_cancel_queues (t->prev_hop, t);
3452 GNUNET_PEER_change_rc (t->prev_hop, -1);
3454 if (0 != t->next_hop)
3456 peer_cancel_queues (t->next_hop, t);
3457 GNUNET_PEER_change_rc (t->next_hop, -1);
3459 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
3461 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3462 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3464 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
3466 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3467 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3470 peer_remove_tunnel (peer_get_short (t->dest), t);
3473 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
3474 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
3475 if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task)
3476 GNUNET_SCHEDULER_cancel (t->bck_maintenance_task);
3479 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3480 path_destroy (t->path);
3486 * Tunnel is empty: destroy it.
3488 * Notifies all participants (peers, cleints) about the destruction.
3490 * @param t Tunnel to destroy.
3493 tunnel_destroy_empty (struct MeshTunnel *t)
3497 struct GNUNET_PeerIdentity id;
3499 GNUNET_PEER_resolve (t->id.oid, &id);
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3501 "executing destruction of empty tunnel %s [%X]\n",
3502 GNUNET_i2s (&id), t->id.tid);
3506 if (GNUNET_NO == t->destroy)
3507 tunnel_send_destroy (t);
3508 if (0 == t->pending_messages)
3511 t->destroy = GNUNET_YES;
3515 * Initialize a Flow Control structure to the initial state.
3517 * @param fc Flow Control structure to initialize.
3520 fc_init (struct MeshFlowControl *fc)
3522 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3523 fc->last_pid_recv = (uint32_t) -1;
3524 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3525 fc->last_ack_recv = (uint32_t) -1;
3526 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3527 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3533 * Destroy a channel and free all resources.
3535 * @param ch Channel to destroy.
3538 channel_destroy (struct MeshChannel *ch)
3540 struct MeshClient *c;
3548 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
3559 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
3566 if (GNUNET_YES == ch->reliable)
3568 channel_rel_free_all (ch->fwd_rel);
3569 channel_rel_free_all (ch->bck_rel);
3576 * Create a new channel.
3578 * @param owner Clients that owns the channel, NULL for foreign channels.
3579 * @param id Channel Number for the channel, for the owner point of view.
3581 * @return A new initialized channel. NULL on error.
3583 static struct MeshChannel *
3584 channel_new (struct MeshClient *owner,
3585 MESH_ChannelNumber id)
3587 struct MeshChannel *ch;
3592 ch = GNUNET_new (struct MeshChannel);
3596 GNUNET_STATISTICS_update (stats, "# channel", 1, GNUNET_NO);
3599 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, id, ch,
3600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3603 channel_destroy (t);
3607 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3615 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
3617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3621 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3631 * Set options in a tunnel, extracted from a bit flag field
3633 * @param t Tunnel to set options to.
3634 * @param options Bit array in host byte order.
3637 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3639 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3640 GNUNET_YES : GNUNET_NO;
3641 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3642 GNUNET_YES : GNUNET_NO;
3647 * Iterator for deleting each tunnel whose client endpoint disconnected.
3649 * @param cls Closure (client that has disconnected).
3650 * @param key The local tunnel id (used to access the hashmap).
3651 * @param value The value stored at the key (tunnel to destroy).
3653 * @return GNUNET_OK, keep iterating.
3656 tunnel_destroy_iterator (void *cls,
3660 struct MeshTunnel *t = value;
3661 struct MeshClient *c = cls;
3663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3664 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3665 t->local_tid, t->local_tid_dest, c->id);
3666 client_delete_tunnel (c, t);
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3671 if (0 != t->next_hop) /* destroy could come before a path is used */
3673 GNUNET_PEER_change_rc (t->next_hop, -1);
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3681 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3682 GNUNET_PEER_change_rc (t->prev_hop, -1);
3687 tunnel_destroy_empty (t);
3694 * remove client's ports from the global hashmap on diconnect.
3696 * @param cls Closure (unused).
3698 * @param value ThClient structure.
3700 * @return GNUNET_OK, keep iterating.
3703 client_release_ports (void *cls,
3709 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3710 if (GNUNET_YES != res)
3713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3714 "Port %u by client %p was not registered.\n",
3721 * Timeout function due to lack of keepalive/traffic from the owner.
3722 * Destroys tunnel if called.
3724 * @param cls Closure (tunnel to destroy).
3725 * @param tc TaskContext
3728 tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3730 struct MeshTunnel *t = cls;
3732 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3736 "Tunnel %s [%X] FWD timed out. Destroying.\n",
3737 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3738 if (NULL != t->client)
3739 send_local_tunnel_destroy (t, GNUNET_YES);
3740 tunnel_destroy (t); /* Do not notify other */
3745 * Timeout function due to lack of keepalive/traffic from the destination.
3746 * Destroys tunnel if called.
3748 * @param cls Closure (tunnel to destroy).
3749 * @param tc TaskContext
3752 tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3754 struct MeshTunnel *t = cls;
3756 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3757 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3760 "Tunnel %s [%X] BCK timed out. Destroying.\n",
3761 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3762 if (NULL != t->owner)
3763 send_local_tunnel_destroy (t, GNUNET_NO);
3764 tunnel_destroy (t); /* Do not notify other */
3769 * Resets the tunnel timeout task, some other message has done the task's job.
3770 * - For the first peer on the direction this means to send
3771 * a keepalive or a path confirmation message (either create or ACK).
3772 * - For all other peers, this means to destroy the tunnel,
3773 * due to lack of activity.
3774 * Starts the tiemout if no timeout was running (tunnel just created).
3776 * @param t Tunnel whose timeout to reset.
3777 * @param fwd Is this forward?
3779 * TODO use heap to improve efficiency of scheduler.
3782 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3784 GNUNET_SCHEDULER_TaskIdentifier *ti;
3785 GNUNET_SCHEDULER_Task f;
3786 struct MeshClient *c;
3788 ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task;
3789 c = fwd ? t->owner : t->client;
3791 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3792 GNUNET_SCHEDULER_cancel (*ti);
3796 f = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive;
3797 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, t);
3801 f = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout;
3802 *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3803 (refresh_connection_time, 4),
3809 /******************************************************************************/
3810 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3811 /******************************************************************************/
3814 * Free a transmission that was already queued with all resources
3815 * associated to the request.
3817 * @param queue Queue handler to cancel.
3818 * @param clear_cls Is it necessary to free associated cls?
3821 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3823 struct MeshFlowControl *fc;
3825 if (GNUNET_YES == clear_cls)
3827 switch (queue->type)
3829 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3830 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3831 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3833 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3834 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3835 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3836 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3837 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3838 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3839 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3840 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3842 " prebuilt message\n");
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3845 GNUNET_MESH_DEBUG_M2S (queue->type));
3847 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3852 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3853 " type %s unknown!\n",
3854 GNUNET_MESH_DEBUG_M2S (queue->type));
3856 GNUNET_free_non_null (queue->cls);
3858 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3859 queue->peer->queue_tail,
3862 /* Delete from appropriate fc in the tunnel */
3863 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3864 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3866 if (queue->peer->id == queue->tunnel->prev_hop)
3867 fc = &queue->tunnel->prev_fc;
3868 else if (queue->peer->id == queue->tunnel->next_hop)
3869 fc = &queue->tunnel->next_fc;
3873 GNUNET_free (queue);
3878 GNUNET_free (queue);
3883 * @brief Get the next transmittable message from the queue.
3885 * This will be the head, except in the case of being a data packet
3886 * not allowed by the destination peer.
3888 * @param peer Destination peer.
3890 * @return The next viable MeshPeerQueue element to send to that peer.
3891 * NULL when there are no transmittable messages.
3893 struct MeshPeerQueue *
3894 queue_get_next (const struct MeshPeer *peer)
3896 struct MeshPeerQueue *q;
3898 struct GNUNET_MESH_Data *dmsg;
3899 struct MeshTunnel2 *t;
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3904 for (q = peer->fc->queue_head; NULL != q; q = q->next)
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 GNUNET_MESH_DEBUG_M2S (q->type));
3910 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3911 pid = ntohl (dmsg->pid);
3914 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3915 ack = t->next_fc.last_ack_recv;
3917 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3918 ack = t->prev_fc.last_ack_recv;
3921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3926 "* ACK: %u, PID: %u, MID: %u\n",
3927 ack, pid, ntohl (dmsg->mid));
3928 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3941 "* nothing found\n");
3947 queue_send (void *cls, size_t size, void *buf)
3949 struct MeshPeer *peer = cls;
3950 struct GNUNET_MessageHeader *msg;
3951 struct MeshPeerQueue *queue;
3952 struct MeshTunnel2 *t;
3953 struct GNUNET_PeerIdentity *dst_id;
3954 struct MeshFlowControl *fc;
3965 fc->core_transmit = NULL;
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3969 if (NULL == buf || 0 == size)
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
3974 queue = queue_get_next (peer);
3976 /* Queue has no internal mesh traffic nor sendable payload */
3979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3980 if (NULL == fc->queue_head)
3981 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3987 dst_id = GNUNET_PEER_resolve (peer->id);
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3990 GNUNET_i2s (dst_id));
3991 /* Check if buffer size is enough for the message */
3992 if (queue->size > size)
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 "* not enough room, reissue\n");
3997 GNUNET_CORE_notify_transmit_ready (core_handle,
4000 GNUNET_TIME_UNIT_FOREVER_REL,
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4009 t = queue->peer->tunnel;
4013 switch (queue->type)
4016 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4017 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
4018 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
4019 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4020 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4021 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4022 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4023 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
4024 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4027 GNUNET_MESH_DEBUG_M2S (queue->type));
4029 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4030 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4031 data_size = send_core_data_raw (queue->cls, size, buf);
4032 msg = (struct GNUNET_MessageHeader *) buf;
4033 type = ntohs (msg->type);
4035 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4037 data_size = send_core_connection_create (queue->cls, size, buf);
4039 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4041 if (NULL != t->client)
4042 data_size = send_core_connection_ack (queue->cls, size, buf);
4044 data_size = send_core_data_raw (queue->cls, size, buf);
4048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4049 "* type unknown: %u\n",
4054 if (0 < drop_percent &&
4055 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4058 "Dropping message of type %s\n",
4059 GNUNET_MESH_DEBUG_M2S(queue->type));
4062 /* Free queue, but cls was freed by send_core_* */
4063 queue_destroy (queue, GNUNET_NO);
4065 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4066 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
4070 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4071 t->next_fc.last_pid_sent = pid;
4072 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
4077 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4078 t->prev_fc.last_pid_sent = pid;
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
4082 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
4088 /* If more data in queue, send next */
4089 queue = queue_get_next (peer);
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4093 if (NULL == peer->core_transmit) {
4094 peer->core_transmit =
4095 GNUNET_CORE_notify_transmit_ready(core_handle,
4098 GNUNET_TIME_UNIT_FOREVER_REL,
4106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4107 "* tmt rdy called somewhere else\n");
4110 if (peer->id == t->next_hop)
4112 else if (peer->id == t->prev_hop)
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
4118 peer->id, t->next_hop, t->prev_hop);
4121 if (NULL != peer->queue_head)
4123 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
4125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4126 "* %s starting poll timeout\n",
4127 GNUNET_i2s (&my_full_id));
4128 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4134 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4136 GNUNET_SCHEDULER_cancel (fc->poll_task);
4137 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4140 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4151 queue_add (void *cls, uint16_t type, size_t size,
4152 struct MeshPeer *dst,
4153 struct MeshConnection *c,
4154 struct MeshChannel *ch)
4156 struct MeshPeerQueue *queue;
4157 struct MeshFlowControl *fc;
4158 struct MeshTunnel2 *t;
4162 priority = GNUNET_NO;
4163 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4164 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4170 if (fc->queue_n >= fc->queue_max)
4172 /* If this isn't a retransmission, drop the message */
4174 (GNUNET_NO == ch->reliable ||
4175 (NULL == ch->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
4176 (NULL == ch->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)))
4178 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4182 "queue full: %u/%u\n",
4183 fc->queue_n, fc->queue_max);
4184 return; /* Drop this message */
4186 priority = GNUNET_YES;
4189 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
4190 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4191 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4195 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4202 if (GNUNET_YES == priority)
4204 struct GNUNET_MESH_Data *d;
4208 GNUNET_CONTAINER_DLL_insert (dst->fc->queue_head,
4209 dst->fc->queue_tail,
4211 d = (struct GNUNET_MESH_Data *) queue->cls;
4213 for (queue = dst->fc->queue_tail; NULL != queue; queue = queue->prev)
4215 if (queue->type != type)
4217 d = (struct GNUNET_MESH_Data *) queue->cls;
4224 GNUNET_CONTAINER_DLL_insert_tail (dst->fc->queue_head,
4225 dst->fc->queue_tail,
4228 if (NULL == dst->fc->core_transmit)
4230 dst->fc->core_transmit =
4231 GNUNET_CORE_notify_transmit_ready (core_handle,
4234 GNUNET_TIME_UNIT_FOREVER_REL,
4235 GNUNET_PEER_resolve2 (dst->id),
4243 /******************************************************************************/
4244 /******************** MESH NETWORK HANDLERS **************************/
4245 /******************************************************************************/
4249 * Core handler for path creation
4251 * @param cls closure
4252 * @param message message
4253 * @param peer peer identity this notification is about
4255 * @return GNUNET_OK to keep the connection open,
4256 * GNUNET_SYSERR to close it (signal serious error)
4259 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4260 const struct GNUNET_MessageHeader *message)
4262 unsigned int own_pos;
4265 MESH_ChannelNumber tid;
4266 struct GNUNET_MESH_CreateTunnel *msg;
4267 struct GNUNET_PeerIdentity *pi;
4268 struct MeshPeerPath *path;
4269 struct MeshPeer *dest_peer_info;
4270 struct MeshPeer *orig_peer_info;
4271 struct MeshTunnel *t;
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4274 "Received a path create msg [%s]\n",
4275 GNUNET_i2s (&my_full_id));
4276 size = ntohs (message->size);
4277 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
4279 GNUNET_break_op (0);
4283 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
4284 if (size % sizeof (struct GNUNET_PeerIdentity))
4286 GNUNET_break_op (0);
4289 size /= sizeof (struct GNUNET_PeerIdentity);
4292 GNUNET_break_op (0);
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4296 msg = (struct GNUNET_MESH_CreateTunnel *) message;
4298 tid = ntohl (msg->tid);
4299 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4301 " path is for tunnel %s[%X]:%u.\n",
4302 GNUNET_i2s (pi), tid, ntohl (msg->port));
4303 t = channel_get (pi, tid);
4304 if (NULL == t) /* might be a local tunnel */
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4309 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4315 t->port = ntohl (msg->port);
4316 opt = ntohl (msg->opt);
4317 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
4319 t->nobuffer = GNUNET_YES;
4322 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
4324 t->reliable = GNUNET_YES;
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
4328 tunnel_reset_timeout (t, GNUNET_YES);
4329 tunnel_change_state (t, MESH_TUNNEL_WAITING);
4331 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4332 if (NULL == dest_peer_info)
4334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4335 " Creating PeerInfo for destination.\n");
4336 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeer));
4337 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4338 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4342 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4343 if (NULL == orig_peer_info)
4345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4346 " Creating PeerInfo for origin.\n");
4347 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeer));
4348 orig_peer_info->id = GNUNET_PEER_intern (pi);
4349 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4350 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4353 path = path_new (size);
4355 for (i = 0; i < size; i++)
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4358 GNUNET_i2s (&pi[i]));
4359 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4360 if (path->peers[i] == myid)
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4364 if (own_pos == 0 && path->peers[own_pos] != myid)
4366 /* create path: self not found in path through self */
4367 GNUNET_break_op (0);
4368 path_destroy (path);
4372 path_add_to_peers (path, GNUNET_NO);
4373 tunnel_use_path (t, path);
4375 peer_add_tunnel (dest_peer_info, t);
4377 if (own_pos == size - 1)
4379 struct MeshClient *c;
4381 /* Find target client */
4382 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
4385 /* TODO send reject */
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4390 peer_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
4391 /* This can be a retransmission due to a lost PATH ACK.
4392 * Check if we already have a destination client for the tunnel. */
4395 /* Assign local tid */
4396 while (NULL != channel_get_incoming (next_local_tid))
4397 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4398 t->local_tid_dest = next_local_tid++;
4399 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
4401 if (GNUNET_YES == t->reliable)
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4404 t->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
4406 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
4409 tunnel_add_client (t, c);
4410 send_local_tunnel_create (t);
4413 /* Eliminate tunnel when origin dies */
4414 tunnel_reset_timeout (t, GNUNET_YES);
4415 /* Keep tunnel alive in direction dest->owner*/
4416 tunnel_reset_timeout (t, GNUNET_NO);
4420 struct MeshPeerPath *path2;
4422 t->next_hop = path->peers[own_pos + 1];
4423 GNUNET_PEER_change_rc(t->next_hop, 1);
4425 /* It's for somebody else! Retransmit. */
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4427 path2 = path_duplicate (path);
4428 peer_add_path (dest_peer_info, path2, GNUNET_NO);
4429 peer_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4430 send_path_create (t);
4438 * Core handler for path ACKs
4440 * @param cls closure
4441 * @param message message
4442 * @param peer peer identity this notification is about
4444 * @return GNUNET_OK to keep the connection open,
4445 * GNUNET_SYSERR to close it (signal serious error)
4448 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4449 const struct GNUNET_MessageHeader *message)
4451 struct GNUNET_MESH_PathACK *msg;
4452 struct MeshPeer *peer_info;
4453 struct MeshPeerPath *p;
4454 struct MeshTunnel *t;
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4457 GNUNET_i2s (&my_full_id));
4458 msg = (struct GNUNET_MESH_PathACK *) message;
4459 t = channel_get (&msg->oid, ntohl(msg->tid));
4462 /* TODO notify that we don't know the tunnel */
4463 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4465 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4469 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4471 peer_info = peer_get (&msg->peer_id);
4472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4473 GNUNET_i2s (&msg->peer_id));
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4477 /* Add path to peers? */
4481 path_add_to_peers (p, GNUNET_YES);
4487 tunnel_change_state (t, MESH_TUNNEL_READY);
4488 tunnel_reset_timeout (t, GNUNET_NO);
4489 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
4490 t->prev_fc.last_ack_sent = ntohl (msg->ack);
4492 /* Message for us? */
4493 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4496 if (NULL == t->owner)
4498 GNUNET_break_op (0);
4501 if (NULL != peer_info->dhtget)
4503 GNUNET_DHT_get_stop (peer_info->dhtget);
4504 peer_info->dhtget = NULL;
4506 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK, GNUNET_YES);
4507 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK, GNUNET_NO);
4511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512 " not for us, retransmitting...\n");
4513 send_prebuilt_message (message, t->prev_hop, t);
4519 * Core handler for notifications of broken paths
4521 * @param cls closure
4522 * @param message message
4523 * @param peer peer identity this notification is about
4525 * @return GNUNET_OK to keep the connection open,
4526 * GNUNET_SYSERR to close it (signal serious error)
4529 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4530 const struct GNUNET_MessageHeader *message)
4532 struct GNUNET_MESH_PathBroken *msg;
4533 struct MeshTunnel *t;
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4536 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4537 msg = (struct GNUNET_MESH_PathBroken *) message;
4538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4539 GNUNET_i2s (&msg->peer1));
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4541 GNUNET_i2s (&msg->peer2));
4542 t = channel_get (&msg->oid, ntohl (msg->tid));
4545 GNUNET_break_op (0);
4548 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4549 GNUNET_PEER_search (&msg->peer2));
4556 * Core handler for tunnel destruction
4558 * @param cls closure
4559 * @param message message
4560 * @param peer peer identity this notification is about
4562 * @return GNUNET_OK to keep the connection open,
4563 * GNUNET_SYSERR to close it (signal serious error)
4566 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4567 const struct GNUNET_MessageHeader *message)
4569 struct GNUNET_MESH_TunnelDestroy *msg;
4570 struct MeshTunnel *t;
4572 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4574 "Got a TUNNEL DESTROY packet from %s\n",
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4577 " for tunnel %s [%u]\n",
4578 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4579 t = channel_get (&msg->oid, ntohl (msg->tid));
4582 /* Probably already got the message from another path,
4583 * destroyed the tunnel and retransmitted to children.
4586 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4590 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4593 t->local_tid, t->local_tid_dest);
4595 if (GNUNET_PEER_search (peer) == t->prev_hop)
4597 // TODO check owner's signature
4598 // TODO add owner's signatue to tunnel for retransmission
4599 peer_cancel_queues (t->prev_hop, t);
4600 GNUNET_PEER_change_rc (t->prev_hop, -1);
4603 else if (GNUNET_PEER_search (peer) == t->next_hop)
4605 // TODO check dest's signature
4606 // TODO add dest's signatue to tunnel for retransmission
4607 peer_cancel_queues (t->next_hop, t);
4608 GNUNET_PEER_change_rc (t->next_hop, -1);
4613 GNUNET_break_op (0);
4614 // TODO check both owner AND destination's signature to see which matches
4615 // TODO restransmit in appropriate direction
4618 tunnel_destroy_empty (t);
4620 // TODO: add timeout to destroy the tunnel anyway
4626 * Generic handler for mesh network payload traffic.
4628 * @param peer Peer identity this notification is about.
4629 * @param message Data message.
4630 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4632 * @return GNUNET_OK to keep the connection open,
4633 * GNUNET_SYSERR to close it (signal serious error)
4636 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
4637 const struct GNUNET_MessageHeader *message,
4640 struct GNUNET_MESH_Data *msg;
4641 struct MeshFlowControl *fc;
4642 struct MeshChannelReliability *rel;
4643 struct MeshTunnel *t;
4644 struct MeshClient *c;
4652 size = ntohs (message->size);
4654 sizeof (struct GNUNET_MESH_Data) +
4655 sizeof (struct GNUNET_MessageHeader))
4660 type =ntohs (message->type);
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4662 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4663 msg = (struct GNUNET_MESH_Data *) message;
4664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4665 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4667 t = channel_get (&msg->oid, ntohl (msg->tid));
4670 /* TODO notify back: we don't know this tunnel */
4671 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING tunnel unknown\n");
4676 /* Initialize FWD/BCK data */
4677 pid = ntohl (msg->pid);
4678 fc = fwd ? &t->prev_fc : &t->next_fc;
4679 c = fwd ? t->client : t->owner;
4680 rel = fwd ? t->bck_rel : t->fwd_rel;
4681 hop = fwd ? t->next_hop : t->prev_hop;
4682 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4684 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4686 "WARNING Received PID %u, (prev %u), ACK %u\n",
4687 pid, fc->last_pid_recv, fc->last_ack_sent);
4691 tunnel_change_state (t, MESH_TUNNEL_READY);
4692 tunnel_reset_timeout (t, fwd);
4695 /* TODO signature verification */
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4697 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4698 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4702 mid = ntohl (msg->mid);
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4704 " pid %u (mid %u) not seen yet\n", pid, mid);
4705 fc->last_pid_recv = pid;
4707 if (GNUNET_NO == t->reliable ||
4708 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4709 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4712 "!!! RECV %u\n", ntohl (msg->mid));
4713 if (GNUNET_YES == t->reliable)
4715 /* Is this the exact next expected messasge? */
4716 if (mid == rel->mid_recv)
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4720 tunnel_send_client_data (t, msg, fwd);
4721 tunnel_send_client_buffered_data (t, c, rel);
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4726 tunnel_add_buffered_data (t, msg, rel);
4729 else /* Tunnel unreliable, send to clients directly */
4731 tunnel_send_client_data (t, msg, fwd);
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4737 " MID %u not expected (%u - %u), dropping!\n",
4738 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4743 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4745 " Pid %u not expected (%u+), dropping!\n",
4746 pid, fc->last_pid_recv + 1);
4748 tunnel_send_ack (t, type, fwd);
4751 fc->last_pid_recv = pid;
4754 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4756 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4757 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4759 ttl = ntohl (msg->ttl);
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4763 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4764 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4765 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4772 send_prebuilt_message (message, hop, t);
4773 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4780 * Core handler for mesh network traffic going from the origin to a peer
4782 * @param cls Closure (unused).
4783 * @param message Message received.
4784 * @param peer Peer who sent the message.
4786 * @return GNUNET_OK to keep the connection open,
4787 * GNUNET_SYSERR to close it (signal serious error)
4790 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4791 const struct GNUNET_MessageHeader *message)
4793 return handle_mesh_data (peer, message, GNUNET_YES);
4797 * Core handler for mesh network traffic towards the owner of a tunnel.
4799 * @param cls Closure (unused).
4800 * @param message Message received.
4801 * @param peer Peer who sent the message.
4803 * @return GNUNET_OK to keep the connection open,
4804 * GNUNET_SYSERR to close it (signal serious error)
4807 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4808 const struct GNUNET_MessageHeader *message)
4810 return handle_mesh_data (peer, message, GNUNET_NO);
4815 * Core handler for mesh network traffic end-to-end ACKs.
4817 * @param cls Closure.
4818 * @param message Message.
4819 * @param peer Peer identity this notification is about.
4821 * @return GNUNET_OK to keep the connection open,
4822 * GNUNET_SYSERR to close it (signal serious error)
4825 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4826 const struct GNUNET_MessageHeader *message)
4828 struct GNUNET_MESH_DataACK *msg;
4829 struct MeshChannelReliability *rel;
4830 struct MeshReliableMessage *copy;
4831 struct MeshReliableMessage *next;
4832 struct MeshTunnel *t;
4838 type = ntohs (message->type);
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4840 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4841 msg = (struct GNUNET_MESH_DataACK *) message;
4843 t = channel_get (&msg->oid, ntohl (msg->tid));
4846 /* TODO notify that we dont know this tunnel (whom)? */
4847 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4850 ack = ntohl (msg->mid);
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4853 /* Is this a forward or backward ACK? */
4854 id = GNUNET_PEER_search (peer);
4855 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4858 if (NULL == t->owner)
4860 send_prebuilt_message (message, t->prev_hop, t);
4865 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4868 if (NULL == t->client)
4870 send_prebuilt_message (message, t->next_hop, t);
4877 GNUNET_break_op (0);
4881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4882 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4884 if (GMC_is_pid_bigger (copy->mid, ack))
4886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4887 tunnel_free_sent_reliable (t, msg, rel);
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4893 tunnel_free_reliable_message (copy);
4895 /* Once buffers have been free'd, send ACK */
4896 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4898 /* If some message was free'd, update the retransmission delay*/
4899 if (GNUNET_YES == work)
4901 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4903 GNUNET_SCHEDULER_cancel (rel->retry_task);
4904 if (NULL == rel->head_sent)
4906 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4910 struct GNUNET_TIME_Absolute new_target;
4911 struct GNUNET_TIME_Relative delay;
4913 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4914 MESH_RETRANSMIT_MARGIN);
4915 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4917 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4919 GNUNET_SCHEDULER_add_delayed (delay,
4920 &tunnel_retransmit_message,
4931 * Core handler for mesh network traffic point-to-point acks.
4933 * @param cls closure
4934 * @param message message
4935 * @param peer peer identity this notification is about
4937 * @return GNUNET_OK to keep the connection open,
4938 * GNUNET_SYSERR to close it (signal serious error)
4941 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4942 const struct GNUNET_MessageHeader *message)
4944 struct GNUNET_MESH_ACK *msg;
4945 struct MeshTunnel *t;
4946 struct MeshFlowControl *fc;
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4952 msg = (struct GNUNET_MESH_ACK *) message;
4954 t = channel_get (&msg->oid, ntohl (msg->tid));
4958 /* TODO notify that we dont know this tunnel (whom)? */
4959 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4962 ack = ntohl (msg->pid);
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4965 /* Is this a forward or backward ACK? */
4966 id = GNUNET_PEER_search (peer);
4967 if (t->next_hop == id)
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4972 else if (t->prev_hop == id)
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4979 GNUNET_break_op (0);
4983 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4984 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4986 GNUNET_SCHEDULER_cancel (fc->poll_task);
4987 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4988 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4991 fc->last_ack_recv = ack;
4992 peer_unlock_queue (id);
4993 tunnel_change_state (t, MESH_TUNNEL_READY);
4995 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
5002 * Core handler for mesh network traffic point-to-point ack polls.
5004 * @param cls closure
5005 * @param message message
5006 * @param peer peer identity this notification is about
5008 * @return GNUNET_OK to keep the connection open,
5009 * GNUNET_SYSERR to close it (signal serious error)
5012 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5013 const struct GNUNET_MessageHeader *message)
5015 struct GNUNET_MESH_Poll *msg;
5016 struct MeshTunnel *t;
5017 struct MeshFlowControl *fc;
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5025 msg = (struct GNUNET_MESH_Poll *) message;
5027 t = channel_get (&msg->oid, ntohl (msg->tid));
5031 /* TODO notify that we dont know this tunnel (whom)? */
5032 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5033 GNUNET_break_op (0);
5037 /* Is this a forward or backward ACK? */
5038 id = GNUNET_PEER_search (peer);
5039 pid = ntohl (msg->pid);
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
5041 if (t->next_hop == id)
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5045 old = fc->last_pid_recv;
5047 else if (t->prev_hop == id)
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5051 old = fc->last_pid_recv;
5059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
5060 fc->last_pid_recv = pid;
5061 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
5063 if (GNUNET_YES == t->reliable)
5064 fc->last_pid_recv = old;
5071 * Core handler for mesh keepalives.
5073 * @param cls closure
5074 * @param message message
5075 * @param peer peer identity this notification is about
5076 * @return GNUNET_OK to keep the connection open,
5077 * GNUNET_SYSERR to close it (signal serious error)
5079 * TODO: Check who we got this from, to validate route.
5082 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5083 const struct GNUNET_MessageHeader *message)
5085 struct GNUNET_MESH_TunnelKeepAlive *msg;
5086 struct MeshTunnel *t;
5087 struct MeshClient *c;
5091 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5095 t = channel_get (&msg->oid, ntohl (msg->tid));
5098 /* TODO notify that we dont know that tunnel */
5099 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
5104 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5105 GNUNET_YES : GNUNET_NO;
5106 c = fwd ? t->client : t->owner;
5107 hop = fwd ? t->next_hop : t->prev_hop;
5110 tunnel_change_state (t, MESH_TUNNEL_READY);
5111 tunnel_reset_timeout (t, fwd);
5112 if (NULL != c || 0 == hop || myid == hop)
5115 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5116 send_prebuilt_message (message, hop, t);
5123 * Functions to handle messages from core
5125 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5126 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE, 0},
5127 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5128 sizeof (struct GNUNET_MESH_PathBroken)},
5129 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5130 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5131 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5132 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5133 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
5134 sizeof (struct GNUNET_MESH_DataACK)},
5135 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
5136 sizeof (struct GNUNET_MESH_DataACK)},
5137 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5138 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5139 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5140 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5141 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5142 sizeof (struct GNUNET_MESH_ACK)},
5143 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5144 sizeof (struct GNUNET_MESH_Poll)},
5145 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5146 sizeof (struct GNUNET_MESH_PathACK)},
5152 * Function to process paths received for a new peer addition. The recorded
5153 * paths form the initial tunnel, which can be optimized later.
5154 * Called on each result obtained for the DHT search.
5156 * @param cls closure
5157 * @param exp when will this value expire
5158 * @param key key of the result
5159 * @param get_path path of the get request
5160 * @param get_path_length lenght of get_path
5161 * @param put_path path of the put request
5162 * @param put_path_length length of the put_path
5163 * @param type type of the result
5164 * @param size number of bytes in data
5165 * @param data pointer to the result data
5168 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5169 const struct GNUNET_HashCode * key,
5170 const struct GNUNET_PeerIdentity *get_path,
5171 unsigned int get_path_length,
5172 const struct GNUNET_PeerIdentity *put_path,
5173 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5174 size_t size, const void *data)
5176 struct MeshPeer *peer = cls;
5177 struct MeshPeerPath *p;
5178 struct MeshConnection *c;
5179 struct GNUNET_PeerIdentity pi;
5182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5183 GNUNET_PEER_resolve (peer->id, &pi);
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5186 p = path_build_from_dht (get_path, get_path_length,
5187 put_path, put_path_length);
5188 path_add_to_peers (p, GNUNET_NO);
5191 /* Count connections */
5192 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5194 /* If we already have 3 (or more (?!)) connections, it's enough */
5198 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5201 peer_connect (peer);
5207 /******************************************************************************/
5208 /********************* MESH LOCAL HANDLES **************************/
5209 /******************************************************************************/
5213 * Handler for client connection.
5215 * @param cls Closure (unused).
5216 * @param client Client handler.
5219 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5221 struct MeshClient *c;
5225 c = GNUNET_malloc (sizeof (struct MeshClient));
5227 GNUNET_SERVER_client_keep (client);
5228 GNUNET_SERVER_client_set_user_context (client, c);
5229 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5234 * Handler for client disconnection
5236 * @param cls closure
5237 * @param client identification of the client; NULL
5238 * for the last call when the server is destroyed
5241 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5243 struct MeshClient *c;
5244 struct MeshClient *next;
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5253 c = client_get (client);
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5258 GNUNET_SERVER_client_drop (c->handle);
5259 c->shutting_down = GNUNET_YES;
5260 if (NULL != c->own_tunnels)
5262 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
5263 &tunnel_destroy_iterator, c);
5264 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
5267 if (NULL != c->incoming_tunnels)
5269 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
5270 &tunnel_destroy_iterator, c);
5271 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
5274 if (NULL != c->ports)
5276 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5277 &client_release_ports, c);
5278 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5281 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5284 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
5291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
5297 * Handler for new clients
5299 * @param cls closure
5300 * @param client identification of the client
5301 * @param message the actual message, which includes messages the client wants
5304 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5305 const struct GNUNET_MessageHeader *message)
5307 struct GNUNET_MESH_ClientConnect *cc_msg;
5308 struct MeshClient *c;
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
5315 /* Check data sanity */
5316 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5317 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5318 if (0 != (size % sizeof (uint32_t)))
5321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5324 size /= sizeof (uint32_t);
5326 /* Initialize new client structure */
5327 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
5328 c->id = next_client_id++; /* overflow not important: just for debug */
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
5335 p = (uint32_t *) &cc_msg[1];
5336 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
5337 for (i = 0; i < size; i++)
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
5342 /* store in client's hashmap */
5343 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
5344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5345 /* store in global hashmap */
5346 /* FIXME only allow one client to have the port open,
5347 * have a backup hashmap with waiting clients */
5348 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5349 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5353 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5354 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5355 GNUNET_SERVER_notification_context_add (nc, client);
5356 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5358 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5364 * Handler for requests of new tunnels
5366 * @param cls Closure.
5367 * @param client Identification of the client.
5368 * @param message The actual message.
5371 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
5372 const struct GNUNET_MessageHeader *message)
5374 struct GNUNET_MESH_ChannelMessage *msg;
5375 struct MeshPeer *peer;
5376 struct MeshTunnel2 *t;
5377 struct MeshChannel *ch;
5378 struct MeshClient *c;
5379 MESH_ChannelNumber chid;
5381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
5383 /* Sanity check for client registration */
5384 if (NULL == (c = client_get (client)))
5387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5392 /* Message size sanity check */
5393 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5400 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5402 GNUNET_i2s (&msg->peer), ntohl (msg->port));
5403 chid = ntohl (msg->channel_id);
5405 /* Sanity check for duplicate tunnel IDs */
5406 if (NULL != channel_get_by_local_id (c, chid))
5409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5413 peer = peer_get (&msg->peer);
5414 if (NULL == peer->tunnel)
5415 peer->tunnel = tunnel_new ();
5418 /* Create channel */
5419 while (NULL != channel_get_by_pi (myid, next_tid))
5420 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
5421 t = tunnel_new (myid, next_tid, c, chid);
5422 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
5426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5429 t->port = ntohl (msg->port);
5430 tunnel_set_options (t, ntohl (msg->opt));
5431 if (GNUNET_YES == t->reliable)
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5434 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5436 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
5440 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
5442 peer_info = peer_get (&msg->peer);
5443 peer_add_tunnel (peer_info, t);
5444 peer_connect (peer_info, t);
5445 tunnel_reset_timeout (t, GNUNET_YES);
5446 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5452 * Handler for requests of deleting tunnels
5454 * @param cls closure
5455 * @param client identification of the client
5456 * @param message the actual message
5459 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5460 const struct GNUNET_MessageHeader *message)
5462 struct GNUNET_MESH_ChannelMessage *tunnel_msg;
5463 struct MeshClient *c;
5464 struct MeshTunnel *t;
5465 MESH_ChannelNumber tid;
5467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5468 "Got a DESTROY TUNNEL from client!\n");
5470 /* Sanity check for client registration */
5471 if (NULL == (c = client_get (client)))
5474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5479 /* Message sanity check */
5480 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5487 tunnel_msg = (struct GNUNET_MESH_ChannelMessage *) message;
5489 /* Retrieve tunnel */
5490 tid = ntohl (tunnel_msg->channel_id);
5491 t = channel_get_by_local_id (c, tid);
5494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5500 /* Cleanup after the tunnel */
5501 client_delete_tunnel (c, t);
5502 if (c == t->client && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= tid)
5506 else if (c == t->owner && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > tid)
5508 peer_remove_tunnel (peer_get_short (t->dest), t);
5513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5514 " tunnel %X client %p (%p, %p)\n",
5515 tid, c, t->owner, t->client);
5519 /* The tunnel will be destroyed when the last message is transmitted. */
5520 tunnel_destroy_empty (t);
5522 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5528 * Handler for client traffic
5530 * @param cls closure
5531 * @param client identification of the client
5532 * @param message the actual message
5535 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5536 const struct GNUNET_MessageHeader *message)
5538 struct GNUNET_MESH_LocalData *data_msg;
5539 struct MeshClient *c;
5540 struct MeshTunnel *t;
5541 struct MeshFlowControl *fc;
5542 MESH_ChannelNumber tid;
5545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5546 "Got data from a client!\n");
5548 /* Sanity check for client registration */
5549 if (NULL == (c = client_get (client)))
5552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5557 data_msg = (struct GNUNET_MESH_LocalData *) message;
5559 /* Sanity check for message size */
5560 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5561 if (size < sizeof (struct GNUNET_MessageHeader))
5564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5568 /* Tunnel exists? */
5569 tid = ntohl (data_msg->tid);
5570 t = channel_get_by_local_id (c, tid);
5574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5578 /* Is the client in the tunnel? */
5579 if ( !( (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV &&
5581 t->owner->handle == client)
5583 (tid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV &&
5585 t->client->handle == client) ) )
5588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5592 /* Ok, everything is correct, send the message
5593 * (pretend we got it from a mesh peer)
5596 struct GNUNET_MESH_Data *payload;
5597 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5599 fc = tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5600 if (GNUNET_YES == t->reliable)
5602 struct MeshChannelReliability *rel;
5603 struct MeshReliableMessage *copy;
5605 rel = (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5606 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5607 + sizeof(struct GNUNET_MESH_Data)
5609 copy->mid = rel->mid_sent++;
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5611 copy->timestamp = GNUNET_TIME_absolute_get ();
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5615 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5616 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5619 GNUNET_TIME_relative_multiply (rel->expected_delay,
5620 MESH_RETRANSMIT_MARGIN);
5622 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5623 &tunnel_retransmit_message,
5626 payload = (struct GNUNET_MESH_Data *) ©[1];
5627 payload->mid = htonl (copy->mid);
5631 payload = (struct GNUNET_MESH_Data *) cbuf;
5632 payload->mid = htonl (fc->last_pid_recv + 1);
5634 memcpy (&payload[1], &data_msg[1], size);
5635 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5636 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ?
5637 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5638 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5639 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5640 payload->tid = htonl (t->id.tid);
5641 payload->ttl = htonl (default_ttl);
5642 payload->pid = htonl (fc->last_pid_recv + 1);
5643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5644 " calling generic handler...\n");
5645 if (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
5646 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5648 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5651 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5658 * Handler for client's ACKs for payload traffic.
5660 * @param cls Closure (unused).
5661 * @param client Identification of the client.
5662 * @param message The actual message.
5665 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5666 const struct GNUNET_MessageHeader *message)
5668 struct GNUNET_MESH_LocalAck *msg;
5669 struct MeshTunnel *t;
5670 struct MeshClient *c;
5671 MESH_ChannelNumber tid;
5673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5674 /* Sanity check for client registration */
5675 if (NULL == (c = client_get (client)))
5678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5683 msg = (struct GNUNET_MESH_LocalAck *) message;
5685 /* Tunnel exists? */
5686 tid = ntohl (msg->channel_id);
5687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5688 t = channel_get_by_local_id (c, tid);
5692 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5693 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5698 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5699 if (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
5701 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5702 t->prev_fc.last_ack_recv++;
5703 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5707 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5708 t->next_fc.last_ack_recv++;
5709 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5712 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5720 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5722 * @param cls Closure (client handle).
5723 * @param key Key (hashed tunnel ID, unused).
5724 * @param value Tunnel info.
5726 * @return GNUNET_YES, to keep iterating.
5729 monitor_all_tunnels_iterator (void *cls,
5730 const struct GNUNET_HashCode * key,
5733 struct GNUNET_SERVER_Client *client = cls;
5734 struct MeshTunnel *t = value;
5735 struct GNUNET_MESH_LocalMonitor *msg;
5737 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5738 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5739 msg->channel_id = htonl (t->id.tid);
5740 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5741 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5742 GNUNET_PEER_resolve (t->dest, &msg->destination);
5744 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5745 "* sending info about tunnel %s [%u]\n",
5746 GNUNET_i2s (&msg->owner), t->id.tid);
5748 GNUNET_SERVER_notification_context_unicast (nc, client,
5749 &msg->header, GNUNET_NO);
5755 * Handler for client's MONITOR request.
5757 * @param cls Closure (unused).
5758 * @param client Identification of the client.
5759 * @param message The actual message.
5762 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5763 const struct GNUNET_MessageHeader *message)
5765 struct MeshClient *c;
5767 /* Sanity check for client registration */
5768 if (NULL == (c = client_get (client)))
5771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5775 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5776 "Received get tunnels request from client %u\n",
5778 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5779 monitor_all_tunnels_iterator,
5781 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5782 "Get tunnels request from client %u completed\n",
5784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5789 * Handler for client's MONITOR_TUNNEL request.
5791 * @param cls Closure (unused).
5792 * @param client Identification of the client.
5793 * @param message The actual message.
5796 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5797 const struct GNUNET_MessageHeader *message)
5799 const struct GNUNET_MESH_LocalMonitor *msg;
5800 struct GNUNET_MESH_LocalMonitor *resp;
5801 struct MeshClient *c;
5802 struct MeshTunnel *t;
5804 /* Sanity check for client registration */
5805 if (NULL == (c = client_get (client)))
5808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5812 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5813 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5814 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5817 ntohl (msg->channel_id));
5818 t = channel_get (&msg->owner, ntohl (msg->channel_id));
5821 /* We don't know the tunnel FIXME */
5822 struct GNUNET_MESH_LocalMonitor warn;
5825 GNUNET_SERVER_notification_context_unicast (nc, client,
5828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5832 /* Initialize context */
5833 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5835 GNUNET_PEER_resolve (t->dest, &resp->destination);
5836 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5837 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5838 &resp->header, GNUNET_NO);
5841 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5842 "Monitor tunnel request from client %u completed\n",
5844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5849 * Functions to handle messages from clients
5851 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5852 {&handle_local_new_client, NULL,
5853 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5854 {&handle_local_channel_create, NULL,
5855 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CHANNEL_CREATE,
5856 sizeof (struct GNUNET_MESH_ChannelMessage)},
5857 {&handle_local_channel_destroy, NULL,
5858 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
5859 sizeof (struct GNUNET_MESH_ChannelMessage)},
5860 {&handle_local_data, NULL,
5861 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5862 {&handle_local_ack, NULL,
5863 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5864 sizeof (struct GNUNET_MESH_LocalAck)},
5865 {&handle_local_get_tunnels, NULL,
5866 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5867 sizeof (struct GNUNET_MessageHeader)},
5868 {&handle_local_show_tunnel, NULL,
5869 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5870 sizeof (struct GNUNET_MESH_LocalMonitor)},
5876 * Method called whenever a given peer connects.
5878 * @param cls closure
5879 * @param peer peer identity this notification is about
5882 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5884 struct MeshPeer *peer_info;
5885 struct MeshPeerPath *path;
5887 DEBUG_CONN ("Peer connected\n");
5888 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5889 peer_info = peer_get (peer);
5890 if (myid == peer_info->id)
5892 DEBUG_CONN (" (self)\n");
5893 path = path_new (1);
5897 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5898 path = path_new (2);
5899 path->peers[1] = peer_info->id;
5900 GNUNET_PEER_change_rc (peer_info->id, 1);
5901 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5903 path->peers[0] = myid;
5904 GNUNET_PEER_change_rc (myid, 1);
5905 peer_add_path (peer_info, path, GNUNET_YES);
5906 if (NULL == peer_info->fc)
5908 peer_info->fc = GNUNET_new (struct MeshFlowControl);
5909 fc_init (peer_info->fc);
5910 peer_info->fc->peer = peer_info;
5917 * Method called whenever a peer disconnects.
5919 * @param cls closure
5920 * @param peer peer identity this notification is about
5923 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5925 struct MeshPeer *pi;
5926 struct MeshPeerQueue *q;
5927 struct MeshPeerQueue *n;
5928 struct MeshFlowControl *fc;
5930 DEBUG_CONN ("Peer disconnected\n");
5931 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5949 queue_destroy (q, GNUNET_YES);
5952 if (NULL != fc->core_transmit)
5953 GNUNET_CORE_notify_transmit_ready_cancel (fc->core_transmit);
5954 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
5955 GNUNET_SCHEDULER_cancel (fc->poll_task);
5957 peer_remove_path (pi, pi->id, myid);
5960 DEBUG_CONN (" (self)\n");
5962 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5970 * Install server (service) handlers and start listening to clients.
5975 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5976 GNUNET_SERVER_connect_notify (server_handle,
5977 &handle_local_client_connect, NULL);
5978 GNUNET_SERVER_disconnect_notify (server_handle,
5979 &handle_local_client_disconnect, NULL);
5980 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5982 clients_head = NULL;
5983 clients_tail = NULL;
5985 GNUNET_SERVER_resume (server_handle);
5990 * To be called on core init/fail.
5992 * @param cls Closure (config)
5993 * @param server handle to the server for this service
5994 * @param identity the public identity of this peer
5997 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5998 const struct GNUNET_PeerIdentity *identity)
6000 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6004 GNUNET_break (core_handle == server);
6005 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6011 GNUNET_i2s (identity));
6012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6014 GNUNET_i2s (&my_full_id));
6015 GNUNET_CORE_disconnect (core_handle);
6016 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6017 NULL, /* Closure passed to MESH functions */
6018 &core_init, /* Call core_init once connected */
6019 &core_connect, /* Handle connects */
6020 &core_disconnect, /* remove peers on disconnects */
6021 NULL, /* Don't notify about all incoming messages */
6022 GNUNET_NO, /* For header only in notification */
6023 NULL, /* Don't notify about all outbound messages */
6024 GNUNET_NO, /* For header-only out notification */
6025 core_handlers); /* Register these handlers */
6034 /******************************************************************************/
6035 /************************ MAIN FUNCTIONS ****************************/
6036 /******************************************************************************/
6039 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6041 * @param cls closure
6042 * @param key current key code
6043 * @param value value in the hash map
6044 * @return GNUNET_YES if we should continue to iterate,
6048 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6050 struct MeshTunnel *t = value;
6057 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6059 * @param cls closure
6060 * @param key current key code
6061 * @param value value in the hash map
6062 * @return GNUNET_YES if we should continue to iterate,
6066 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6068 struct MeshPeer *p = value;
6069 struct MeshPeerQueue *q;
6070 struct MeshPeerQueue *n;
6078 queue_destroy(q, GNUNET_YES);
6088 * Task run during shutdown.
6094 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6098 if (core_handle != NULL)
6100 GNUNET_CORE_disconnect (core_handle);
6103 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6104 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6105 if (dht_handle != NULL)
6107 GNUNET_DHT_disconnect (dht_handle);
6112 GNUNET_SERVER_notification_context_destroy (nc);
6115 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6117 GNUNET_SCHEDULER_cancel (announce_id_task);
6118 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6125 * Process mesh requests.
6127 * @param cls closure
6128 * @param server the initialized server
6129 * @param c configuration to use
6132 run (void *cls, struct GNUNET_SERVER_Handle *server,
6133 const struct GNUNET_CONFIGURATION_Handle *c)
6136 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6139 server_handle = server;
6140 GNUNET_SERVER_suspend (server_handle);
6143 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6148 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6149 "mesh", "peer/privatekey");
6150 GNUNET_SCHEDULER_shutdown ();
6155 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6156 &refresh_connection_time))
6158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6160 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6161 "mesh", "refresh path time");
6162 GNUNET_SCHEDULER_shutdown ();
6167 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6172 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6173 "mesh", "id announce time");
6174 GNUNET_SCHEDULER_shutdown ();
6179 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6184 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6185 "mesh", "connect timeout");
6186 GNUNET_SCHEDULER_shutdown ();
6191 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6196 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6197 "mesh", "max msgs queue");
6198 GNUNET_SCHEDULER_shutdown ();
6203 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6208 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6209 "mesh", "max tunnels");
6210 GNUNET_SCHEDULER_shutdown ();
6215 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6220 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6221 "mesh", "default ttl", 64);
6226 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6230 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6231 "mesh", "max peers", 1000);
6236 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6244 "Mesh is running with drop mode enabled. "
6245 "This is NOT a good idea! "
6246 "Remove the DROP_PERCENT option from your configuration.\n");
6250 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6251 &dht_replication_level))
6253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6255 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6256 "mesh", "dht replication level", 3);
6257 dht_replication_level = 3;
6260 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6261 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
6262 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6263 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6265 dht_handle = GNUNET_DHT_connect (c, 64);
6266 if (NULL == dht_handle)
6270 stats = GNUNET_STATISTICS_create ("mesh", c);
6272 /* Scheduled the task to clean up when shutdown is called */
6273 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6275 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6276 GNUNET_free (keyfile);
6277 GNUNET_assert (NULL != pk);
6278 my_private_key = pk;
6279 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6280 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6281 &my_full_id.hashPubKey);
6282 myid = GNUNET_PEER_intern (&my_full_id);
6283 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6284 "Mesh for peer [%s] starting\n",
6285 GNUNET_i2s(&my_full_id));
6287 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6288 NULL, /* Closure passed to MESH functions */
6289 &core_init, /* Call core_init once connected */
6290 &core_connect, /* Handle connects */
6291 &core_disconnect, /* remove peers on disconnects */
6292 NULL, /* Don't notify about all incoming messages */
6293 GNUNET_NO, /* For header only in notification */
6294 NULL, /* Don't notify about all outbound messages */
6295 GNUNET_NO, /* For header-only out notification */
6296 core_handlers); /* Register these handlers */
6297 if (NULL == core_handle)
6300 GNUNET_SCHEDULER_shutdown ();
6304 next_local_tid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6305 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6311 * The main function for the mesh service.
6313 * @param argc number of arguments from the command line
6314 * @param argv command line arguments
6315 * @return 0 ok, 1 on error
6318 main (int argc, char *const *argv)
6323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6324 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6326 ret = (GNUNET_OK == r) ? 0 : 1;
6327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");