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 * Double linked list.
426 struct MeshChannel *next;
427 struct MeshChannel *prev;
430 * Destination port of the channel.
435 * Local tunnel number ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
437 MESH_ChannelNumber id;
440 * Local tunnel number for local destination clients (incoming number)
441 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0). All clients share the same
444 MESH_ChannelNumber id_dest;
447 * Is the tunnel bufferless (minimum latency)?
452 * Is the tunnel reliable?
457 * Last time the channel was used
459 struct GNUNET_TIME_Absolute timestamp;
462 * Client owner of the tunnel, if any
464 struct MeshClient *owner;
467 * Client destination of the tunnel, if any.
469 struct MeshClient *client;
472 * Flag to signal the destruction of the channel.
473 * If this is set GNUNET_YES the channel will be destroyed
474 * when the queue is empty.
479 * Total messages pending for this channel, payload or not.
481 unsigned int pending_messages;
485 * Only present (non-NULL) at the owner of a tunnel.
487 struct MeshChannelReliability *fwd_rel;
491 * Only present (non-NULL) at the destination of a tunnel.
493 struct MeshChannelReliability *bck_rel;
497 struct MeshConnection
502 struct MeshConnection *next;
503 struct MeshConnection *prev;
506 * Tunnel this connection is part of.
508 struct MeshTunnel2 *t;
516 * State of the connection.
518 enum MeshConnectionState state;
521 * Path being used for the tunnel.
523 struct MeshPeerPath *path;
526 * Position of the local peer in the path.
528 unsigned int own_pos;
531 * Task to keep the used paths alive at the owner,
532 * time tunnel out on all the other peers.
534 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
537 * Task to keep the used paths alive at the destination,
538 * time tunnel out on all the other peers.
540 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
543 * Pending message count.
545 int pending_messages;
548 * Destroy flag: if true, destroy on last message.
555 * Struct containing all information regarding a tunnel to a peer.
560 * Endpoint of the tunnel.
562 struct MeshPeer *peer;
567 struct GNUNET_HashCode id;
570 * State of the tunnel.
572 enum MeshTunnelState state;
575 * Local peer ephemeral private key
577 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
580 * Local peer ephemeral public key
582 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *my_eph;
585 * Remote peer's public key.
587 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *peers_eph;
590 * Encryption ("our") key.
592 struct GNUNET_CRYPTO_AesSessionKey e_key;
595 * Decryption ("their") key.
597 struct GNUNET_CRYPTO_AesSessionKey d_key;
600 * Paths that are actively used to reach the destination peer.
602 struct MeshConnection *connection_head;
603 struct MeshConnection *connection_tail;
606 * Next connection number.
611 * Channels inside this tunnel.
613 struct MeshChannel *channel_head;
614 struct MeshChannel *channel_tail;
617 * Channel ID for the next created tunnel.
619 MESH_ChannelNumber next_chid;
622 * Channel ID for the next incoming tunnel.
624 MESH_ChannelNumber next_local_chid;
627 * Pending message count.
629 int pending_messages;
632 * Destroy flag: if true, destroy on last message.
640 * Struct containing information about a client of the service
642 * TODO: add a list of 'waiting' ports
649 struct MeshClient *next;
654 struct MeshClient *prev;
657 * Tunnels that belong to this client, indexed by local id
659 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
662 * Tunnels this client has accepted, indexed by incoming local id
664 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
667 * Handle to communicate with the client
669 struct GNUNET_SERVER_Client *handle;
672 * Ports that this client has declared interest in.
673 * Indexed by port, contains *Client.
675 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
678 * Whether the client is active or shutting down (don't send confirmations
679 * to a client that is shutting down.
684 * ID of the client, mainly for debug messages
690 /******************************************************************************/
691 /************************ DEBUG FUNCTIONS ****************************/
692 /******************************************************************************/
696 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
697 * @param cls string to print
698 * @param success GNUNET_OK if the PUT was transmitted,
699 * GNUNET_NO on timeout,
700 * GNUNET_SYSERR on disconnect from service
701 * after the PUT message was transmitted
702 * (so we don't know if it was received or not)
707 mesh_debug (void *cls, int success)
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
717 /******************************************************************************/
718 /*********************** GLOBAL VARIABLES ****************************/
719 /******************************************************************************/
721 /************************** Configuration parameters **************************/
724 * How often to send path keepalives. Paths timeout after 4 missed.
726 static struct GNUNET_TIME_Relative refresh_connection_time;
729 * How often to PUT own ID in the DHT.
731 static struct GNUNET_TIME_Relative id_announce_time;
734 * Maximum time allowed to connect to a peer found by string.
736 static struct GNUNET_TIME_Relative connect_timeout;
739 * Default TTL for payload packets.
741 static unsigned long long default_ttl;
744 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
746 static unsigned long long dht_replication_level;
749 * How many tunnels are we willing to maintain.
750 * Local tunnels are always allowed, even if there are more tunnels than max.
752 static unsigned long long max_tunnels;
755 * How many messages *in total* are we willing to queue, divided by number of
756 * tunnels to get tunnel queue size.
758 static unsigned long long max_msgs_queue;
761 * How many peers do we want to remember?
763 static unsigned long long max_peers;
766 * Percentage of messages that will be dropped (for test purposes only).
768 static unsigned long long drop_percent;
770 /*************************** Static global variables **************************/
773 * DLL with all the clients, head.
775 static struct MeshClient *clients_head;
778 * DLL with all the clients, tail.
780 static struct MeshClient *clients_tail;
783 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
785 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
788 * Peers known, indexed by PeerIdentity (MeshPeer).
790 static struct GNUNET_CONTAINER_MultiHashMap *peers;
793 * Handle to communicate with core.
795 static struct GNUNET_CORE_Handle *core_handle;
800 static struct GNUNET_DHT_Handle *dht_handle;
803 * Handle to server lib.
805 static struct GNUNET_SERVER_Handle *server_handle;
808 * Handle to the statistics service.
810 static struct GNUNET_STATISTICS_Handle *stats;
813 * Notification context, to send messages to local clients.
815 static struct GNUNET_SERVER_NotificationContext *nc;
818 * Local peer own ID (memory efficient handle).
820 static GNUNET_PEER_Id myid;
823 * Local peer own ID (full value).
825 static struct GNUNET_PeerIdentity my_full_id;
830 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
835 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
838 * All ports clients of this peer have opened.
840 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
843 * Task to periodically announce itself in the network.
845 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
848 * Next ID to assign to a client.
850 unsigned int next_client_id;
853 /******************************************************************************/
854 /*********************** DECLARATIONS **************************/
855 /******************************************************************************/
858 * Function to process paths received for a new peer addition. The recorded
859 * paths form the initial tunnel, which can be optimized later.
860 * Called on each result obtained for the DHT search.
863 * @param exp when will this value expire
864 * @param key key of the result
865 * @param type type of the result
866 * @param size number of bytes in data
867 * @param data pointer to the result data
870 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
871 const struct GNUNET_HashCode * key,
872 const struct GNUNET_PeerIdentity *get_path,
873 unsigned int get_path_length,
874 const struct GNUNET_PeerIdentity *put_path,
875 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
876 size_t size, const void *data);
880 * Retrieve the MeshPeer stucture associated with the peer, create one
881 * and insert it in the appropriate structures if the peer is not known yet.
883 * @param peer Full identity of the peer.
885 * @return Existing or newly created peer info.
887 static struct MeshPeer *
888 peer_get (const struct GNUNET_PeerIdentity *peer);
892 * Retrieve the MeshPeer stucture associated with the peer, create one
893 * and insert it in the appropriate structures if the peer is not known yet.
895 * @param peer Short identity of the peer.
897 * @return Existing or newly created peer info.
899 static struct MeshPeer *
900 peer_get_short (const GNUNET_PEER_Id peer);
904 * Build a PeerPath from the paths returned from the DHT, reversing the paths
905 * to obtain a local peer -> destination path and interning the peer ids.
907 * @return Newly allocated and created path
909 static struct MeshPeerPath *
910 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
911 unsigned int get_path_length,
912 const struct GNUNET_PeerIdentity *put_path,
913 unsigned int put_path_length);
917 * Adds a path to the data structs of all the peers in the path
919 * @param p Path to process.
920 * @param confirmed Whether we know if the path works or not.
923 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
927 * Search for a channel by global ID using full PeerIdentities.
929 * @param oid owner of the tunnel.
930 * @param tid global tunnel number.
932 * @return tunnel handler, NULL if doesn't exist.
934 static struct MeshChannel *
935 channel_get (const struct GNUNET_PeerIdentity *oid, MESH_ChannelNumber tid);
939 * Change the tunnel state.
941 * @param t Tunnel whose state to change.
942 * @param state New state.
945 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
949 * Notify a tunnel that a connection has broken that affects at least
952 * @param t Tunnel affected.
953 * @param p1 Peer that got disconnected from p2.
954 * @param p2 Peer that got disconnected from p1.
956 * @return Short ID of the peer disconnected (either p1 or p2).
957 * 0 if the tunnel remained unaffected.
959 static GNUNET_PEER_Id
960 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
961 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
964 * @brief Use the given path for the tunnel.
965 * Update the next and prev hops (and RCs).
966 * (Re)start the path refresh in case the tunnel is locally owned.
968 * @param t Tunnel to update.
969 * @param p Path to use.
971 * @return Connection created.
973 static struct MeshConnection *
974 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
977 * Tunnel is empty: destroy it.
979 * Notifies all participants (peers, cleints) about the destruction.
981 * @param t Tunnel to destroy.
984 tunnel_destroy_empty (struct MeshTunnel2 *t);
987 * Destroy the tunnel.
989 * This function does not generate any warning traffic to clients or peers.
992 * Cancel messages belonging to this tunnel queued to neighbors.
993 * Free any allocated resources linked to the tunnel.
995 * @param t The tunnel to destroy.
998 tunnel_destroy (struct MeshTunnel2 *t);
1001 * Send FWD keepalive packets for a connection.
1003 * @param cls Closure (connection for which to send the keepalive).
1004 * @param tc Notification context.
1007 connection_fwd_keepalive (void *cls,
1008 const struct GNUNET_SCHEDULER_TaskContext *tc);
1011 * Send BCK keepalive packets for a connection.
1013 * @param cls Closure (connection for which to send the keepalive).
1014 * @param tc Notification context.
1017 connection_bck_keepalive (void *cls,
1018 const struct GNUNET_SCHEDULER_TaskContext *tc);
1022 * Change the tunnel state.
1024 * @param c Connection whose state to change.
1025 * @param state New state.
1028 connection_change_state (struct MeshConnection* c,
1029 enum MeshConnectionState state);
1034 * @brief Queue and pass message to core when possible.
1036 * @param cls Closure (@c type dependant). It will be used by queue_send to
1037 * build the message to be sent if not already prebuilt.
1038 * @param type Type of the message, 0 for a raw message.
1039 * @param size Size of the message.
1040 * @param dst Neighbor to send message to.
1041 * @param c Connection this message belongs to, if any.
1042 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1045 queue_add (void *cls, uint16_t type, size_t size,
1046 struct MeshPeer *dst,
1047 struct MeshConnection *c,
1048 struct MeshChannel *ch);
1052 * Free a transmission that was already queued with all resources
1053 * associated to the request.
1055 * @param queue Queue handler to cancel.
1056 * @param clear_cls Is it necessary to free associated cls?
1059 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1063 * @brief Get the next transmittable message from the queue.
1065 * This will be the head, except in the case of being a data packet
1066 * not allowed by the destination peer.
1068 * @param peer Destination peer.
1070 * @return The next viable MeshPeerQueue element to send to that peer.
1071 * NULL when there are no transmittable messages.
1073 struct MeshPeerQueue *
1074 queue_get_next (const struct MeshPeer *peer);
1078 * Core callback to write a queued packet to core buffer
1080 * @param cls Closure (peer info).
1081 * @param size Number of bytes available in buf.
1082 * @param buf Where the to write the message.
1084 * @return number of bytes written to buf
1087 queue_send (void *cls, size_t size, void *buf);
1091 * Dummy function to separate declarations from definitions in function list.
1094 __mesh_divider______________________________________________________________();
1098 * Get string description for tunnel state.
1100 * @param s Tunnel state.
1102 * @return String representation.
1105 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1107 static char buf[128];
1111 case MESH_TUNNEL_NEW:
1112 return "MESH_TUNNEL_NEW";
1113 case MESH_TUNNEL_SEARCHING:
1114 return "MESH_TUNNEL_SEARCHING";
1115 case MESH_TUNNEL_WAITING:
1116 return "MESH_TUNNEL_WAITING";
1117 case MESH_TUNNEL_READY:
1118 return "MESH_TUNNEL_READY";
1119 case MESH_TUNNEL_RECONNECTING:
1120 return "MESH_TUNNEL_RECONNECTING";
1123 sprintf (buf, "%u (UNKNOWN STATE)", s);
1130 * Get string description for tunnel state.
1132 * @param s Tunnel state.
1134 * @return String representation.
1137 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1141 case MESH_CONNECTION_NEW:
1142 return "MESH_CONNECTION_NEW";
1143 case MESH_CONNECTION_SENT:
1144 return "MESH_CONNECTION_SENT";
1145 case MESH_CONNECTION_READY:
1146 return "MESH_CONNECTION_READY";
1148 return "MESH_CONNECTION_STATE_ERROR";
1154 /******************************************************************************/
1155 /************************ PERIODIC FUNCTIONS ****************************/
1156 /******************************************************************************/
1159 * Periodically announce self id in the DHT
1161 * @param cls closure
1162 * @param tc task context
1165 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1167 struct PBlock block;
1169 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1171 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1175 * - Set data expiration in function of X
1176 * - Adapt X to churn
1178 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1180 block.id = my_full_id;
1181 GNUNET_DHT_put (dht_handle, /* DHT handle */
1182 &my_full_id.hashPubKey, /* Key to use */
1183 dht_replication_level, /* Replication level */
1184 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1185 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1186 sizeof (block), /* Size of the data */
1187 (const char *) &block, /* Data itself */
1188 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1189 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1190 NULL, /* Continuation */
1191 NULL); /* Continuation closure */
1193 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1197 /******************************************************************************/
1198 /****************** GENERAL HELPER FUNCTIONS ************************/
1199 /******************************************************************************/
1203 * Get the previous hop in a connection
1205 * @param c Connection.
1207 * @return Previous peer in the connection.
1209 static struct MeshPeer *
1210 connection_get_prev_hop (struct MeshConnection *c)
1214 if (0 == c->own_pos || c->path->length < 2)
1215 id = c->path->peers[0];
1217 id = c->path->peers[c->own_pos - 1];
1219 return peer_get_short (id);
1224 * Get the next hop in a connection
1226 * @param c Connection.
1228 * @return Next peer in the connection.
1230 static struct MeshPeer *
1231 connection_get_next_hop (struct MeshConnection *c)
1235 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1236 id = c->path->peers[c->path->length - 1];
1238 id = c->path->peers[c->own_pos + 1];
1240 return peer_get_short (id);
1245 * Check if client has registered with the service and has not disconnected
1247 * @param client the client to check
1249 * @return non-NULL if client exists in the global DLL
1251 static struct MeshClient *
1252 client_get (struct GNUNET_SERVER_Client *client)
1254 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1259 * Deletes a tunnel from a client (either owner or destination). To be used on
1262 * @param c Client whose tunnel to delete.
1263 * @param ch Channel which should be deleted.
1266 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1272 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1274 if (GNUNET_YES != res)
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1277 if (c == ch->client)
1279 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1281 if (GNUNET_YES != res)
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1288 * Notify the appropriate client that a new incoming channel was created.
1290 * @param ch Channel that was created.
1293 send_local_channel_create (struct MeshChannel *ch)
1295 struct GNUNET_MESH_ChannelMessage msg;
1296 struct MeshTunnel2 *t = ch->t;
1298 if (NULL == ch->client)
1300 msg.header.size = htons (sizeof (msg));
1301 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1302 msg.channel_id = htonl (ch->id_dest);
1303 msg.port = htonl (ch->port);
1305 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1306 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1307 msg.opt = htonl (msg.opt);
1308 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1309 GNUNET_SERVER_notification_context_unicast (nc, ch->client->handle,
1310 &msg.header, GNUNET_NO);
1315 * Notify a client that the incoming tunnel is no longer valid.
1317 * @param ch Channel that is destroyed.
1318 * @param fwd Forward notification (owner->dest)?
1321 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1323 struct GNUNET_MESH_ChannelMessage msg;
1324 struct MeshClient *c;
1326 c = fwd ? ch->client : ch->owner;
1332 msg.header.size = htons (sizeof (msg));
1333 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1334 msg.channel_id = htonl (fwd ? ch->id_dest : ch->id);
1335 msg.port = htonl (0);
1336 memset (&msg.peer, 0, sizeof (msg.peer));
1337 msg.opt = htonl (0);
1338 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1339 &msg.header, GNUNET_NO);
1344 * Build a local ACK message and send it to a local client.
1346 * @param ch Channel on which to send the ACK.
1347 * @param c Client to whom send the ACK.
1348 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1351 send_local_ack (struct MeshChannel *ch,
1352 struct MeshClient *c,
1355 struct GNUNET_MESH_LocalAck msg;
1357 msg.header.size = htons (sizeof (msg));
1358 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1359 msg.channel_id = htonl (is_fwd ? ch->id : ch->id_dest);
1360 GNUNET_SERVER_notification_context_unicast (nc,
1368 * Count established (ready) connections of a tunnel.
1370 * @param t Tunnel on which to send the message.
1372 * @return Number of connections.
1375 tunnel_count_connections (struct MeshTunnel2 *t)
1377 struct MeshConnection *c;
1380 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1387 * Pick a connection on which send the next data message.
1389 * @param t Tunnel on which to send the message.
1390 * @param fwd Is this a fwd message?
1392 * @return The connection on which to send the next message.
1394 static struct MeshConnection *
1395 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1397 struct MeshConnection *c;
1398 struct MeshConnection *best;
1399 struct MeshPeer *peer;
1400 unsigned int lowest_q;
1405 lowest_q = UINT_MAX;
1406 for (c = t->connection_head; NULL != c; c = c->next)
1408 if (MESH_CONNECTION_READY == c->state)
1410 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1411 if (NULL == peer->fc)
1416 if (peer->fc->queue_n < lowest_q)
1419 lowest_q = peer->fc->queue_n;
1428 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1429 * Encrypt data with the tunnel key.
1431 * @param t Tunnel whose key to use.
1432 * @param dst Destination for the encrypted data.
1433 * @param src Source of the plaintext.
1434 * @param size Size of the plaintext.
1435 * @param iv Initialization Vector to use.
1436 * @param fwd Is this a fwd message?
1439 tunnel_encrypt (struct MeshTunnel2 *t,
1440 void *dst, const void *src,
1441 size_t size, uint64_t iv, int fwd)
1443 memcpy (dst, src, size);
1448 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1449 * Decrypt data with the tunnel key.
1451 * @param t Tunnel whose key to use.
1452 * @param dst Destination for the plaintext.
1453 * @param src Source of the encrypted data.
1454 * @param size Size of the encrypted data.
1455 * @param iv Initialization Vector to use.
1456 * @param fwd Is this a fwd message?
1459 tunnel_decrypt (struct MeshTunnel2 *t,
1460 void *dst, const void *src,
1461 size_t size, uint64_t iv, int fwd)
1463 memcpy (dst, src, size);
1468 * Sends an already built message on a connection, properly registering
1469 * all used resources.
1471 * @param message Message to send. Function makes a copy of it.
1472 * If message is not hop-by-hop, decrements TTL of copy.
1473 * @param c Connection on which this message is transmitted.
1474 * @param ch Channel on which this message is transmitted, or NULL.
1475 * @param fwd Is this a fwd message?
1478 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1479 struct MeshConnection *c,
1480 struct MeshChannel *ch,
1483 struct MeshPeer *neighbor;
1488 neighbor = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1489 if (NULL == neighbor)
1495 size = ntohs (message->size);
1496 data = GNUNET_malloc (size);
1497 memcpy (data, message, size);
1498 type = ntohs(message->type);
1500 if (GNUNET_MESSAGE_TYPE_MESH_FWD == type ||
1501 GNUNET_MESSAGE_TYPE_MESH_BCK == type)
1503 struct GNUNET_MESH_Encrypted *msg;
1506 msg = (struct GNUNET_MESH_Encrypted *) data;
1507 ttl = ntohl (msg->ttl);
1510 GNUNET_break_op (0);
1513 msg->ttl = htonl (ttl - 1);
1526 * Sends an already built message on a tunnel, choosing the best connection.
1528 * @param message Message to send. Function modifies it.
1529 * @param t Tunnel on which this message is transmitted.
1530 * @param ch Channel on which this message is transmitted.
1531 * @param fwd Is this a fwd message?
1534 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1535 struct MeshTunnel2 *t,
1536 struct MeshChannel *ch,
1539 struct MeshConnection *c;
1542 c = tunnel_get_connection (t, fwd);
1548 type = ntohs (msg->header.size);
1551 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1552 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1553 msg->cid = htonl (c->id);
1555 msg->ttl = default_ttl;
1561 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1566 * Sends an already built message on a channel, properly registering
1567 * all used resources and encrypting the message with the tunnel's key.
1569 * @param message Message to send. Function makes a copy of it.
1570 * @param ch Channel on which this message is transmitted.
1571 * @param fwd Is this a fwd message?
1574 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1575 struct MeshChannel *ch,
1578 struct GNUNET_MESH_Encrypted *msg;
1579 size_t size = ntohs (message->size);
1580 char *cbuf[size + sizeof (struct GNUNET_MESH_Encrypted)];
1584 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1585 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1587 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1588 msg->header.type = htons (type);
1589 msg->header.size = htons (size);
1590 msg->iv = GNUNET_htonll (iv);
1591 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1593 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1598 * Sends an already built message directly to a peer.
1599 * Message does must not belong to a connection or channel.
1601 * @param message Message to send. Function makes a copy of it.
1602 * @param peer Tunnel on which this message is transmitted.
1605 send_prebuilt_message_peer (const struct GNUNET_MessageHeader *message,
1606 struct MeshPeer *peer)
1618 size = ntohs (message->size);
1619 data = GNUNET_malloc (size);
1620 memcpy (data, message, size);
1621 type = ntohs(message->type);
1633 * Sends a CREATE CONNECTION message for a path to a peer.
1634 * Changes the connection and tunnel states if necessary.
1636 * @param connection Connection to create.
1639 send_connection_create (struct MeshConnection *connection)
1641 struct MeshPeer *neighbor;
1642 struct MeshTunnel2 *t;
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1646 neighbor = connection_get_next_hop (connection);
1647 queue_add (connection,
1648 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1649 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1650 (connection->path->length *
1651 sizeof (struct GNUNET_PeerIdentity)),
1655 if (MESH_TUNNEL_SEARCHING == t->state)
1656 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1657 if (MESH_CONNECTION_NEW == connection->state)
1658 connection_change_state (connection, MESH_CONNECTION_SENT);
1663 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1666 * @param connection Connection to confirm.
1669 send_connection_ack (struct MeshConnection *connection)
1671 struct MeshPeer *neighbor;
1672 struct MeshTunnel2 *t;
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1676 neighbor = connection_get_prev_hop (connection);
1677 queue_add (connection,
1678 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1679 sizeof (struct GNUNET_MESH_ConnectionACK),
1683 if (MESH_TUNNEL_NEW == t->state)
1684 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1689 * Build an ACK message and queue it to send to the given peer.
1691 * @param peer Peer to whom send the ACK.
1692 * @param ack Value of the ACK.
1695 send_ack (struct MeshPeer *peer, uint32_t ack)
1697 struct GNUNET_MESH_ACK msg;
1699 msg.header.size = htons (sizeof (msg));
1700 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1701 msg.ack = htonl (ack);
1703 send_prebuilt_message_peer (&msg.header, peer);
1708 * Core callback to write a pre-constructed data packet to core buffer
1710 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1711 * @param size Number of bytes available in buf.
1712 * @param buf Where the to write the message.
1714 * @return number of bytes written to buf
1717 send_core_data_raw (void *cls, size_t size, void *buf)
1719 struct GNUNET_MessageHeader *msg = cls;
1722 GNUNET_assert (NULL != msg);
1723 total_size = ntohs (msg->size);
1725 if (total_size > size)
1730 memcpy (buf, msg, total_size);
1737 * Function to send a create path packet to a peer.
1739 * @param cls closure
1740 * @param size number of bytes available in buf
1741 * @param buf where the callee should write the message
1742 * @return number of bytes written to buf
1745 send_core_connection_create (void *cls, size_t size, void *buf)
1747 struct MeshConnection *c = cls;
1748 struct GNUNET_MESH_ConnectionCreate *msg;
1749 struct GNUNET_PeerIdentity *peer_ptr;
1750 struct MeshPeerPath *p = c->path;
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1756 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1757 p->length * sizeof (struct GNUNET_PeerIdentity);
1759 if (size < size_needed || NULL == buf)
1764 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1765 msg->header.size = htons (size_needed);
1766 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1767 msg->cid = htonl (c->id);
1769 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1770 for (i = 0; i < p->length; i++)
1772 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1782 * Creates a path ack message in buf and frees all unused resources.
1784 * @param cls closure (MeshTransmissionDescriptor)
1785 * @param size number of bytes available in buf
1786 * @param buf where the callee should write the message
1787 * @return number of bytes written to buf
1790 send_core_connection_ack (void *cls, size_t size, void *buf)
1792 struct GNUNET_MESH_ConnectionACK *msg = buf;
1793 struct MeshConnection *c = cls;
1794 struct MeshTunnel2 *t = c->t;
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1797 GNUNET_assert (NULL != t);
1798 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1803 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1804 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1805 GNUNET_CRYPTO_hash_xor (&GNUNET_PEER_resolve2 (t->peer->id)->hashPubKey,
1806 &my_full_id.hashPubKey,
1808 msg->cid = htonl (c->id);
1810 /* TODO add signature */
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1813 return sizeof (struct GNUNET_MESH_ConnectionACK);
1818 * Iterator over all the peers to remove the oldest not-used entry.
1820 * @param cls Closure (unsued).
1821 * @param key ID of the peer.
1822 * @param value Peer_Info of the peer.
1827 peer_timeout (void *cls,
1828 const struct GNUNET_HashCode *key,
1836 * Retrieve the MeshPeer stucture associated with the peer, create one
1837 * and insert it in the appropriate structures if the peer is not known yet.
1839 * @param peer Full identity of the peer.
1841 * @return Existing or newly created peer info.
1843 static struct MeshPeer *
1844 peer_get (const struct GNUNET_PeerIdentity *peer_id)
1846 struct MeshPeer *peer;
1848 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
1851 peer = GNUNET_new (struct MeshPeer);
1852 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1854 GNUNET_CONTAINER_multihashmap_iterate (peers,
1858 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
1859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1860 peer->id = GNUNET_PEER_intern (peer_id);
1862 peer->last_contact = GNUNET_TIME_absolute_get();
1869 * Retrieve the MeshPeer stucture associated with the peer, create one
1870 * and insert it in the appropriate structures if the peer is not known yet.
1872 * @param peer Short identity of the peer.
1874 * @return Existing or newly created peer info.
1876 static struct MeshPeer *
1877 peer_get_short (const GNUNET_PEER_Id peer)
1879 return peer_get (GNUNET_PEER_resolve2 (peer));
1884 * Get a cost of a path for a peer considering existing tunnel connections.
1886 * @param peer Peer towards which the path is considered.
1887 * @param path Candidate path.
1889 * @return Cost of the path (path length + number of overlapping nodes)
1892 peer_get_path_cost (const struct MeshPeer *peer,
1893 const struct MeshPeerPath *path)
1895 struct MeshConnection *c;
1896 unsigned int overlap;
1904 GNUNET_assert (NULL != peer->tunnel);
1906 for (i = 0; i < path->length; i++)
1908 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
1910 for (j = 0; j < c->path->length; j++)
1912 if (path->peers[i] == c->path->peers[j])
1920 return (path->length + overlap) * (path->score * -1);
1925 * Choose the best path towards a peer considering the tunnel properties.
1927 * @param peer The destination peer.
1929 * @return Best current known path towards the peer, if any.
1931 static struct MeshPeerPath *
1932 peer_get_best_path (const struct MeshPeer *peer)
1934 struct MeshPeerPath *best_p;
1935 struct MeshPeerPath *p;
1936 struct MeshConnection *c;
1937 unsigned int best_cost;
1940 best_cost = UINT_MAX;
1942 for (p = peer->path_head; NULL != p; p = p->next)
1944 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
1948 continue; /* If path is in use in a connection, skip it. */
1950 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
1961 * Try to establish a new connection to this peer in the given tunnel.
1962 * If the peer doesn't have any path to it yet, try to get one.
1963 * If the peer already has some path, send a CREATE CONNECTION towards it.
1965 * @param peer PeerInfo of the peer.
1968 peer_connect (struct MeshPeer *peer)
1970 struct MeshTunnel2 *t;
1971 struct MeshPeerPath *p;
1972 struct MeshConnection *c;
1975 if (NULL != peer->path_head)
1977 p = peer_get_best_path (peer);
1980 c = tunnel_use_path (t, p);
1981 send_connection_create (c);
1984 else if (NULL == peer->dhtget)
1986 const struct GNUNET_PeerIdentity *id;
1988 id = GNUNET_PEER_resolve2 (peer->id);
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1990 " Starting DHT GET for peer %s\n", GNUNET_i2s (id));
1991 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1992 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1993 &id->hashPubKey, /* key to search */
1994 dht_replication_level, /* replication level */
1995 GNUNET_DHT_RO_RECORD_ROUTE |
1996 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1998 0, /* xquery bits */
1999 &dht_get_id_handler, peer);
2000 if (MESH_TUNNEL_NEW == t->state)
2001 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 "There is no path but the DHT GET is already started.\n");
2012 * @brief Re-initiate traffic to this peer if necessary.
2014 * Check if there is traffic queued towards this peer
2015 * and the core transmit handle is NULL (traffic was stalled).
2016 * If so, call core tmt rdy.
2018 * @param peer_id Short ID of peer to which initiate traffic.
2021 peer_unlock_queue (GNUNET_PEER_Id peer_id)
2023 struct MeshPeer *peer;
2024 struct MeshPeerQueue *q;
2027 peer = peer_get_short (peer_id);
2028 if (NULL != peer->fc->core_transmit)
2029 return; /* Already unlocked */
2031 q = queue_get_next (peer);
2033 return; /* Nothing to transmit */
2036 peer->fc->core_transmit =
2037 GNUNET_CORE_notify_transmit_ready (core_handle,
2040 GNUNET_TIME_UNIT_FOREVER_REL,
2041 GNUNET_PEER_resolve2 (peer->id),
2049 * Cancel all transmissions towards a neighbor that belong to
2050 * a certain connection.
2052 * @param peer Neighbor to whom cancel the transmissions.
2053 * @param c Connection which to cancel.
2056 peer_cancel_queues (struct MeshPeer *peer, struct MeshConnection *c)
2058 struct MeshPeerQueue *q;
2059 struct MeshPeerQueue *next;
2060 struct MeshFlowControl *fc;
2062 if (NULL == peer || NULL == peer->fc)
2068 for (q = fc->queue_head; NULL != q; q = next)
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2074 "peer_cancel_queue %s\n",
2075 GNUNET_MESH_DEBUG_M2S (q->type));
2076 queue_destroy (q, GNUNET_YES);
2079 if (NULL == fc->queue_head)
2081 if (NULL != fc->core_transmit)
2083 GNUNET_CORE_notify_transmit_ready_cancel (fc->core_transmit);
2084 fc->core_transmit = NULL;
2086 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2088 GNUNET_SCHEDULER_cancel (fc->poll_task);
2089 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2096 * Destroy the peer_info and free any allocated resources linked to it
2098 * @param peer The peer_info to destroy.
2100 * @return GNUNET_OK on success
2103 peer_destroy (struct MeshPeer *peer)
2105 struct GNUNET_PeerIdentity id;
2106 struct MeshPeerPath *p;
2107 struct MeshPeerPath *nextp;
2109 GNUNET_PEER_resolve (peer->id, &id);
2110 GNUNET_PEER_change_rc (peer->id, -1);
2113 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2116 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2117 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2119 if (NULL != peer->dhtget)
2121 GNUNET_DHT_get_stop (peer->dhtget);
2123 p = peer->path_head;
2127 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2131 tunnel_destroy_empty (peer->tunnel);
2138 * Remove all paths that rely on a direct connection between p1 and p2
2139 * from the peer itself and notify all tunnels about it.
2141 * @param peer PeerInfo of affected peer.
2142 * @param p1 GNUNET_PEER_Id of one peer.
2143 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2146 * TODO: optimize (see below)
2149 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2152 struct MeshPeerPath *p;
2153 struct MeshPeerPath *next;
2154 struct MeshPeer *peer_d;
2156 unsigned int destroyed;
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2161 for (p = peer->path_head; NULL != p; p = next)
2164 for (i = 0; i < (p->length - 1); i++)
2166 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2167 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2169 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2180 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2182 peer_d = peer_get_short (d); // FIXME
2183 next = peer_get_best_path (peer_d);
2184 tunnel_use_path (peer->tunnel, next);
2185 peer_connect (peer_d);
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2192 * Add the path to the peer and update the path used to reach it in case this
2195 * @param peer_info Destination peer to add the path to.
2196 * @param path New path to add. Last peer must be the peer in arg 1.
2197 * Path will be either used of freed if already known.
2198 * @param trusted Do we trust that this path is real?
2201 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2204 struct MeshPeerPath *aux;
2208 if ((NULL == peer_info) || (NULL == path))
2211 path_destroy (path);
2214 if (path->peers[path->length - 1] != peer_info->id)
2217 path_destroy (path);
2220 if (2 >= path->length && GNUNET_NO == trusted)
2222 /* Only allow CORE to tell us about direct paths */
2223 path_destroy (path);
2226 for (l = 1; l < path->length; l++)
2228 if (path->peers[l] == myid)
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2231 for (l2 = 0; l2 < path->length - l; l2++)
2233 path->peers[l2] = path->peers[l + l2];
2238 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2243 struct GNUNET_PeerIdentity id;
2245 GNUNET_PEER_resolve (peer_info->id, &id);
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2247 path->length, GNUNET_i2s (&id));
2250 l = path_get_length (path);
2253 path_destroy (path);
2257 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2258 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2260 l2 = path_get_length (aux);
2263 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2264 peer_info->path_tail, aux, path);
2269 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2271 path_destroy (path);
2276 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2283 * Add the path to the origin peer and update the path used to reach it in case
2284 * this is the shortest.
2285 * The path is given in peer_info -> destination, therefore we turn the path
2286 * upside down first.
2288 * @param peer_info Peer to add the path to, being the origin of the path.
2289 * @param path New path to add after being inversed.
2290 * Path will be either used or freed.
2291 * @param trusted Do we trust that this path is real?
2294 peer_add_path_to_origin (struct MeshPeer *peer_info,
2295 struct MeshPeerPath *path, int trusted)
2298 peer_add_path (peer_info, path, trusted);
2304 * Function called if the connection to the peer has been stalled for a while,
2305 * possibly due to a missed ACK. Poll the peer about its ACK status.
2307 * @param cls Closure (poll ctx).
2308 * @param tc TaskContext.
2311 peer_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2313 struct MeshFlowControl *fc = cls;
2314 struct GNUNET_MESH_Poll msg;
2315 struct MeshPeer *peer;
2317 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2318 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n",
2327 GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id)));
2329 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2330 msg.header.size = htons (sizeof (msg));
2331 msg.pid = htonl (fc->last_pid_sent);
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2333 send_prebuilt_message_peer (&msg.header, peer);
2334 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2335 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2341 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2342 * to obtain a local peer -> destination path and interning the peer ids.
2344 * @return Newly allocated and created path
2346 static struct MeshPeerPath *
2347 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2348 unsigned int get_path_length,
2349 const struct GNUNET_PeerIdentity *put_path,
2350 unsigned int put_path_length)
2352 struct MeshPeerPath *p;
2358 GNUNET_PEER_change_rc (myid, 1);
2359 i = get_path_length;
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2361 for (i--; i >= 0; i--)
2363 id = GNUNET_PEER_intern (&get_path[i]);
2364 if (p->length > 0 && id == p->peers[p->length - 1])
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2367 GNUNET_PEER_change_rc (id, -1);
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2372 GNUNET_i2s (&get_path[i]));
2374 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2375 p->peers[p->length - 1] = id;
2378 i = put_path_length;
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2380 for (i--; i >= 0; i--)
2382 id = GNUNET_PEER_intern (&put_path[i]);
2385 /* PUT path went through us, so discard the path up until now and start
2386 * from here to get a much shorter (and loop-free) path.
2391 if (p->length > 0 && id == p->peers[p->length - 1])
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2394 GNUNET_PEER_change_rc (id, -1);
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2399 GNUNET_i2s (&put_path[i]));
2401 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2402 p->peers[p->length - 1] = id;
2406 if (get_path_length > 0)
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2408 GNUNET_i2s (&get_path[0]));
2409 if (put_path_length > 0)
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2411 GNUNET_i2s (&put_path[0]));
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2414 for (i = 0; i < p->length; i++)
2416 struct GNUNET_PeerIdentity peer_id;
2418 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2420 GNUNET_i2s (&peer_id));
2428 * Adds a path to the peer_infos of all the peers in the path
2430 * @param p Path to process.
2431 * @param confirmed Whether we know if the path works or not.
2434 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2438 /* TODO: invert and add */
2439 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2440 for (i++; i < p->length; i++)
2442 struct MeshPeer *aux;
2443 struct MeshPeerPath *copy;
2445 aux = peer_get_short (p->peers[i]);
2446 copy = path_duplicate (p);
2447 copy->length = i + 1;
2448 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2454 * Search for a channel among the channels for a client
2456 * @param c the client whose channels to search in
2457 * @param chid the local id of the channel
2459 * @return channel handler, NULL if doesn't exist
2461 static struct MeshChannel *
2462 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2464 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2466 GNUNET_break_op (0);
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2470 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2471 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2472 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2477 * Search for a tunnel by global ID using PEER_ID
2479 * @param pi owner of the tunnel
2480 * @param tid global tunnel number
2482 * @return tunnel handler, NULL if doesn't exist
2484 static struct MeshChannel *
2485 channel_get_by_pi (GNUNET_PEER_Id pi, MESH_ChannelNumber tid)
2487 // struct GNUNET_HashCode hash;
2489 // return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash); FIXME
2495 * Search for a tunnel by global ID using full PeerIdentities
2497 * @param oid owner of the tunnel
2498 * @param tid global tunnel number
2500 * @return tunnel handler, NULL if doesn't exist
2502 static struct MeshChannel *
2503 channel_get (const struct GNUNET_PeerIdentity *oid, MESH_ChannelNumber tid)
2505 return channel_get_by_pi (GNUNET_PEER_search (oid), tid);
2510 * Change the tunnel state.
2512 * @param t Tunnel whose state to change.
2513 * @param state New state.
2516 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "Tunnel %s state was %s\n",
2520 GNUNET_i2s (GNUNET_PEER_resolve2 (t->peer->id)),
2521 GNUNET_MESH_DEBUG_TS2S (t->state));
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "Tunnel %s state is now %s\n",
2524 GNUNET_i2s (GNUNET_PEER_resolve2 (t->peer->id)),
2525 GNUNET_MESH_DEBUG_TS2S (state));
2531 connection_change_state (struct MeshConnection* c,
2532 enum MeshConnectionState state)
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 "Connection %s[%X] state was %s\n",
2536 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id,
2537 GNUNET_MESH_DEBUG_CS2S (c->state));
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2539 "Connection %s[%X] state is now %s\n",
2540 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)), c->id,
2541 GNUNET_MESH_DEBUG_CS2S (state));
2547 * Add a client to a channel, initializing all needed data structures.
2549 * @param ch Channel to which add the client.
2550 * @param c Client which to add to the channel.
2553 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2555 if (NULL != ch->client)
2561 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2572 static struct MeshConnection *
2573 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2575 struct MeshConnection *c;
2576 unsigned int own_pos;
2578 c = GNUNET_new (struct MeshConnection);
2579 for (own_pos = 0; own_pos < p->length; own_pos++)
2581 if (p->peers[own_pos] == myid)
2584 if (own_pos > p->length - 1)
2589 c->own_pos = own_pos;
2591 c->id = t->next_cid++;
2593 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
2597 c->fwd_maintenance_task =
2598 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
2599 &connection_fwd_keepalive, c);
2606 * Notifies a tunnel that a connection has broken that affects at least
2607 * some of its peers. Sends a notification towards the root of the tree.
2608 * In case the peer is the owner of the tree, notifies the client that owns
2609 * the tunnel and tries to reconnect.
2611 * @param t Tunnel affected.
2612 * @param p1 Peer that got disconnected from p2.
2613 * @param p2 Peer that got disconnected from p1.
2615 * @return Short ID of the peer disconnected (either p1 or p2).
2616 * 0 if the tunnel remained unaffected.
2618 static GNUNET_PEER_Id
2619 tunnel_notify_connection_broken (struct MeshTunnel2* t,
2620 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
2622 // if (myid != p1 && myid != p2) FIXME
2627 // if (tree_get_predecessor (t->tree) != 0)
2629 // /* We are the peer still connected, notify owner of the disconnection. */
2630 // struct GNUNET_MESH_PathBroken msg;
2631 // struct GNUNET_PeerIdentity neighbor;
2633 // msg.header.size = htons (sizeof (msg));
2634 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2635 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2636 // msg.tid = htonl (t->id.tid);
2637 // msg.peer1 = my_full_id;
2638 // GNUNET_PEER_resolve (pid, &msg.peer2);
2639 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2640 // send_prebuilt_message (&msg.header, &neighbor, t);
2647 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2649 * @param ch Channel this is about.
2650 * @param fwd Is for FWD traffic? (ACK dest->owner)
2653 channel_send_data_ack (struct MeshChannel *ch, int fwd)
2655 struct GNUNET_MESH_DataACK msg;
2656 struct MeshChannelReliability *rel;
2657 struct MeshReliableMessage *copy;
2661 if (GNUNET_NO == ch->reliable)
2666 rel = fwd ? ch->bck_rel : ch->fwd_rel;
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "send_data_ack for %u\n",
2671 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2672 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2673 msg.header.size = htons (sizeof (msg));
2674 msg.chid = htonl (ch->id);
2675 msg.mid = htonl (rel->mid_recv - 1);
2677 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2679 delta = copy->mid - rel->mid_recv;
2682 mask = 0x1LL << delta;
2683 msg.futures |= mask;
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2686 copy->mid, delta, mask, msg.futures);
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2690 send_prebuilt_message_channel (&msg.header, ch, fwd);
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2696 * Send an ACK informing the predecessor about the available buffer space.
2698 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2699 * the ACK itself goes "back" (towards root).
2701 * @param c Connection on which to send the ACK.
2702 * @param fwd Is this FWD ACK? (Going dest->owner)
2705 connection_send_ack (struct MeshConnection *c, int fwd)
2707 struct MeshFlowControl *next_fc;
2708 struct MeshFlowControl *prev_fc;
2709 struct MeshPeer *next;
2710 struct MeshPeer *prev;
2714 next = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
2715 prev = fwd ? connection_get_prev_hop (c) : connection_get_next_hop (c);
2719 /* Check if we need to transmit the ACK */
2720 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2724 " last pid recv: %u, last ack sent: %u\n",
2725 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2729 /* Ok, ACK might be necessary, what PID to ACK? */
2730 delta = next_fc->queue_max - next_fc->queue_n;
2731 ack = prev_fc->last_pid_recv + delta;
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 " last pid %u, last ack %u, qmax %u, q %u\n",
2735 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2736 next_fc->queue_max, next_fc->queue_n);
2737 if (ack == prev_fc->last_ack_sent)
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2743 prev_fc->last_ack_sent = ack;
2744 send_ack (prev, ack);
2749 * Send an ACK informing the client about available buffer space.
2751 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2752 * the ACK itself goes "back" (towards root).
2754 * @param ch Channel on which to send the ACK, NULL if unknown.
2755 * @param fwd Is this FWD ACK? (Going dest->owner)
2758 // channel_send_ack (struct MeshChannel *ch, uint16_t type, int fwd)
2760 // struct MeshChannelReliability *rel;
2761 // struct MeshFlowControl *next_fc;
2762 // struct MeshFlowControl *prev_fc;
2763 // struct MeshClient *c;
2764 // struct MeshClient *o;
2765 // GNUNET_PEER_Id hop;
2766 // uint32_t delta_mid;
2770 // rel = fwd ? ch->fwd_rel : ch->bck_rel;
2771 // c = fwd ? ch->client : ch->owner;
2772 // o = fwd ? ch->owner : ch->client;
2773 // hop = fwd ? connection_get_prev_hop (cn) : connection_get_next_hop (cn);
2774 // next_fc = fwd ? &t->next_fc : &t->prev_fc;
2775 // prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2779 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2780 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2781 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2782 // "ACK due to %s\n",
2783 // GNUNET_MESH_DEBUG_M2S (type));
2784 // if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2786 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2789 // if (GNUNET_YES == t->reliable && NULL != c)
2790 // tunnel_send_data_ack (t, fwd);
2792 // case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2793 // case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2794 // case GNUNET_MESSAGE_TYPE_MESH_ACK:
2795 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2797 // case GNUNET_MESSAGE_TYPE_MESH_POLL:
2798 // case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2799 // t->force_ack = GNUNET_YES;
2802 // GNUNET_break (0);
2805 // /* Check if we need to transmit the ACK */
2807 // prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2808 // GNUNET_NO == t->force_ack)
2810 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2811 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2812 // " last pid recv: %u, last ack sent: %u\n",
2813 // prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2817 // /* Ok, ACK might be necessary, what PID to ACK? */
2818 // delta = t->queue_max - next_fc->queue_n;
2819 // if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2820 // delta_mid = rel->mid_sent - rel->head_sent->mid;
2823 // if (0 > delta || (GNUNET_YES == t->reliable &&
2825 // (10 < rel->n_sent || 64 <= delta_mid)))
2827 // if (NULL != o && delta > 1)
2829 // ack = prev_fc->last_pid_recv + delta;
2830 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2831 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2832 // " last pid %u, last ack %u, qmax %u, q %u\n",
2833 // prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2834 // t->queue_max, next_fc->queue_n);
2835 // if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2837 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2841 // prev_fc->last_ack_sent = ack;
2843 // send_local_ack (t, o, fwd);
2844 // else if (0 != hop)
2845 // send_ack (t, hop, ack);
2847 // GNUNET_break (GNUNET_YES == t->destroy);
2848 // t->force_ack = GNUNET_NO;
2853 * Modify the mesh message TID from global to local and send to client.
2855 * @param ch Channel on which to send the message.
2856 * @param msg Message to modify and send.
2857 * @param c Client to send to.
2858 * @param tid Tunnel ID to use (c can be both owner and client).
2861 channel_send_client_to_tid (struct MeshChannel *ch,
2862 const struct GNUNET_MESH_Data *msg,
2863 struct MeshClient *c, MESH_ChannelNumber id)
2865 struct GNUNET_MESH_LocalData *copy;
2866 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2867 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2869 if (size < sizeof (struct GNUNET_MessageHeader))
2871 GNUNET_break_op (0);
2879 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2880 memcpy (©[1], &msg[1], size);
2881 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2882 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2883 copy->id = htonl (id);
2884 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2885 ©->header, GNUNET_NO);
2889 * Modify the data message ID from global to local and send to client.
2891 * @param ch Channel on which to send the message.
2892 * @param msg Message to modify and send.
2893 * @param fwd Forward?
2896 channel_send_client_data (struct MeshChannel *ch,
2897 const struct GNUNET_MESH_Data *msg,
2901 channel_send_client_to_tid (ch, msg, ch->client, ch->id_dest);
2903 channel_send_client_to_tid (ch, msg, ch->owner, ch->id);
2908 * Send up to 64 buffered messages to the client for in order delivery.
2910 * @param ch Channel on which to empty the message buffer.
2911 * @param c Client to send to.
2912 * @param rel Reliability structure to corresponding peer.
2913 * If rel == bck_rel, this is FWD data.
2916 channel_send_client_buffered_data (struct MeshChannel *ch,
2917 struct MeshClient *c,
2918 struct MeshChannelReliability *rel)
2920 struct MeshReliableMessage *copy;
2921 struct MeshReliableMessage *next;
2923 if (GNUNET_NO == ch->reliable)
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2930 for (copy = rel->head_recv; NULL != copy; copy = next)
2933 if (copy->mid == rel->mid_recv)
2935 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 " have %u! now expecting %u\n",
2939 copy->mid, rel->mid_recv + 1);
2940 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
2942 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 " don't have %u, next is %u\n",
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2959 * We have received a message out of order, buffer it until we receive
2960 * the missing one and we can feed the rest to the client.
2962 * @param msg Message to buffer.
2963 * @param rel Reliability data to the corresponding direction.
2966 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
2967 struct MeshChannelReliability *rel)
2969 struct MeshReliableMessage *copy;
2970 struct MeshReliableMessage *prev;
2974 size = ntohs (msg->header.size);
2975 mid = ntohl (msg->mid);
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2978 copy = GNUNET_malloc (sizeof (*copy) + size);
2981 memcpy (©[1], msg, size);
2983 // FIXME do something better than O(n), although n < 64...
2984 // FIXME start from the end (most messages are the latest ones)
2985 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2988 if (GMC_is_pid_bigger (prev->mid, mid))
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2991 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2997 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3003 * Destroy a reliable message after it has been acknowledged, either by
3004 * direct mid ACK or bitfield. Updates the appropriate data structures and
3005 * timers and frees all memory.
3007 * @param copy Message that is no longer needed: remote peer got it.
3010 rel_message_free (struct MeshReliableMessage *copy)
3012 struct MeshChannelReliability *rel;
3013 struct GNUNET_TIME_Relative time;
3016 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3017 rel->expected_delay.rel_value *= 7;
3018 rel->expected_delay.rel_value += time.rel_value;
3019 rel->expected_delay.rel_value /= 8;
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3024 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3026 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3028 rel->retry_timer = rel->expected_delay;
3029 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3035 * Destroy all reliable messages queued for a channel,
3036 * during a channel destruction.
3037 * Frees the reliability structure itself.
3039 * @param rel Reliability data for a channel.
3042 channel_rel_free_all (struct MeshChannelReliability *rel)
3044 struct MeshReliableMessage *copy;
3045 struct MeshReliableMessage *next;
3050 for (copy = rel->head_recv; NULL != copy; copy = next)
3053 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3056 for (copy = rel->head_sent; NULL != copy; copy = next)
3059 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3062 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3063 GNUNET_SCHEDULER_cancel (rel->retry_task);
3069 * Mark future messages as ACK'd.
3071 * @param rel Reliability data.
3072 * @param msg DataACK message with a bitfield of future ACK'd messages.
3075 channel_rel_free_sent (struct MeshChannelReliability *rel,
3076 const struct GNUNET_MESH_DataACK *msg)
3078 struct MeshReliableMessage *copy;
3079 struct MeshReliableMessage *next;
3086 bitfield = msg->futures;
3087 mid = ntohl (msg->mid);
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3089 "free_sent_reliable %u %llX\n",
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 " rel %p, head %p\n",
3093 rel, rel->head_sent);
3094 for (i = 0, copy = rel->head_sent;
3095 i < 64 && NULL != copy && 0 != bitfield;
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099 " trying bit %u (mid %u)\n",
3102 if (0 == (bitfield & mask))
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3106 /* Bit was set, clear the bit from the bitfield */
3109 /* The i-th bit was set. Do we have that copy? */
3110 /* Skip copies with mid < target */
3111 target = mid + i + 1;
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3113 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3116 /* Did we run out of copies? (previously freed, it's ok) */
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3123 /* Did we overshoot the target? (previously freed, it's ok) */
3124 if (GMC_is_pid_bigger (copy->mid, target))
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3130 /* Now copy->mid == target, free it */
3132 rel_message_free (copy);
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3140 * We haven't received an ACK after a certain time: restransmit the message.
3142 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3143 * @param tc TaskContext.
3146 channel_retransmit_message (void *cls,
3147 const struct GNUNET_SCHEDULER_TaskContext *tc)
3149 struct MeshChannelReliability *rel = cls;
3150 struct MeshReliableMessage *copy;
3151 struct MeshPeerQueue *q;
3152 struct MeshPeer *pi;
3153 struct MeshChannel *ch;
3154 struct MeshConnection *c;
3155 struct GNUNET_MESH_Data *payload;
3158 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3159 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3163 copy = rel->head_sent;
3170 /* Search the message to be retransmitted in the outgoing queue.
3171 * Check only the queue for the connection that is going to be used,
3172 * if the message is stuck in some other connection's queue we shouldn't
3174 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3175 * the old connection could be temporary stalled or the queue happened to
3176 * be long at time of insertion.
3177 * - not sending the new one could cause terrible delays the old connection
3180 payload = (struct GNUNET_MESH_Data *) ©[1];
3181 fwd = (rel == ch->fwd_rel);
3182 c = tunnel_get_connection(ch->t, fwd);
3183 pi = connection_get_next_hop (c);
3184 for (q = pi->fc->queue_head; NULL != q; q = q->next)
3186 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3188 struct GNUNET_MESH_Data *queued_data = q->cls;
3190 if (queued_data->mid == payload->mid)
3195 /* Message not found in the queue that we are going to use. */
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3200 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3201 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3208 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3209 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3210 &channel_retransmit_message,
3216 * Send keepalive packets for a connection.
3218 * @param c Connection to keep alive..
3219 * @param fwd Is this a FWD keepalive? (owner -> dest).
3222 connection_keepalive (struct MeshConnection *c, int fwd)
3224 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3225 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3229 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3230 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3233 "sending %s keepalive for connection %s[%d]\n",
3234 fwd ? "FWD" : "BCK",
3235 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3238 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3239 msg->header.size = htons (size);
3240 msg->header.type = htons (type);
3241 msg->cid = htonl (c->id);
3242 msg->tid = c->t->id;
3244 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3249 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3251 * @param c Connection for which to send the message.
3252 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3255 connection_recreate (struct MeshConnection *c, int fwd)
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3259 send_connection_create (c);
3261 send_connection_ack (c);
3266 * Generic connection timer management.
3267 * Depending on the role of the peer in the connection will send the
3268 * appropriate message (build or keepalive)
3270 * @param c Conncetion to maintain.
3271 * @param fwd Is FWD?
3274 connection_maintain (struct MeshConnection *c, int fwd)
3276 if (MESH_TUNNEL_SEARCHING == c->t->state)
3278 /* TODO DHT GET with RO_BART */
3283 case MESH_CONNECTION_NEW:
3285 case MESH_CONNECTION_SENT:
3286 connection_recreate (c, fwd);
3288 case MESH_CONNECTION_READY:
3289 connection_keepalive (c, fwd);
3298 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3300 struct MeshConnection *c = cls;
3302 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3303 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3306 connection_keepalive (c, GNUNET_YES);
3307 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3308 &connection_fwd_keepalive,
3314 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3316 struct MeshConnection *c = cls;
3318 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3319 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3322 connection_keepalive (c, GNUNET_NO);
3323 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3324 &connection_bck_keepalive,
3330 * Send a message to all peers in this connection that the connection
3331 * is no longer valid.
3333 * If some peer should not receive the message, it should be zero'ed out
3334 * before calling this function.
3336 * @param c The connection whose peers to notify.
3339 connection_send_destroy (struct MeshConnection *c)
3341 struct GNUNET_MESH_ConnectionDestroy msg;
3343 msg.header.size = htons (sizeof (msg));
3344 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3345 msg.cid = htonl (c->id);
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3348 " sending tunnel destroy for connection %s[%X]\n",
3349 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3352 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3353 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3358 * Send a message to all clients (local and remote) of this channel
3359 * notifying that the channel is no longer valid.
3361 * If some peer or client should not receive the message,
3362 * should be zero'ed out before calling this function.
3364 * @param ch The channel whose clients to notify.
3367 channel_send_destroy (struct MeshChannel *ch)
3369 struct GNUNET_MESH_ChannelDestroy msg;
3371 msg.header.size = htons (sizeof (msg));
3372 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3373 msg.chid = htonl (ch->id);
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3375 " sending tunnel destroy for channel %s:%X\n",
3376 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->t->peer->id)),
3379 if (NULL != ch->owner)
3380 send_local_channel_destroy (ch, GNUNET_NO);
3382 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3384 if (NULL != ch->client)
3385 send_local_channel_destroy (ch, GNUNET_YES);
3387 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3394 * @param tid Tunnel ID.
3396 static struct MeshTunnel2 *
3397 tunnel_new (struct GNUNET_HashCode *tid)
3399 struct MeshTunnel2 *t;
3401 t = GNUNET_new (struct MeshTunnel2);
3403 t->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3405 GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3420 * @param tid Tunnel ID.
3422 static struct MeshTunnel2 *
3423 tunnel_get (struct GNUNET_HashCode *tid)
3425 return GNUNET_CONTAINER_multihashmap_get (tunnels, tid);
3430 * Add a connection to a tunnel.
3433 * @param c Connection.
3436 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3439 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3444 * Create a connection.
3446 * @param tid Tunnel ID.
3447 * @param cid Connection ID.
3449 static struct MeshConnection *
3450 connection_new (struct GNUNET_HashCode *tid, uint32_t cid)
3452 struct MeshConnection *c;
3453 struct MeshTunnel2 *t;
3455 t = tunnel_get (tid);
3458 t = tunnel_new (tid);
3466 c = GNUNET_new (struct MeshConnection);
3468 tunnel_add_connection (t, c);
3475 * Find a connection.
3477 * @param tid Tunnel ID.
3478 * @param cid Connection ID.
3480 static struct MeshConnection *
3481 connection_get (struct GNUNET_HashCode *tid, uint32_t cid)
3483 struct MeshConnection *c;
3484 struct MeshTunnel2 *t;
3486 t = tunnel_get (tid);
3487 for (c = t->connection_head; NULL != c; c = c->next)
3496 * Connection is no longer needed: destroy it and remove from tunnel.
3498 * @param c Connection to destroy.
3501 connection_destroy (struct MeshConnection *c)
3503 struct MeshPeer *peer;
3508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
3509 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)),
3512 peer = connection_get_next_hop (c);
3514 peer_cancel_queues (peer, c);
3515 peer = connection_get_prev_hop (c);
3517 peer_cancel_queues (peer, c);
3519 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
3520 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
3521 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
3522 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
3524 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
3526 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
3532 tunnel_destroy (struct MeshTunnel2 *t)
3534 struct MeshConnection *c;
3535 struct MeshConnection *next;
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
3541 GNUNET_i2s (GNUNET_PEER_resolve2 (c->t->peer->id)));
3543 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
3546 for (c = t->connection_head; NULL != c; c = next)
3549 connection_destroy (c);
3552 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3559 * Tunnel is empty: destroy it.
3561 * Notifies all connections about the destruction.
3563 * @param t Tunnel to destroy.
3566 tunnel_destroy_empty (struct MeshTunnel2 *t)
3568 struct MeshConnection *c;
3570 for (c = t->connection_head; NULL != c; c = c->next)
3572 if (GNUNET_NO == c->destroy)
3573 connection_send_destroy (c);
3576 if (0 == t->pending_messages)
3579 t->destroy = GNUNET_YES;
3584 * Destroy tunnel if empty (no more channels).
3586 * @param t Tunnel to destroy if empty.
3589 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
3591 if (NULL != t->channel_head)
3594 tunnel_destroy_empty (t);
3599 * Initialize a Flow Control structure to the initial state.
3601 * @param fc Flow Control structure to initialize.
3604 fc_init (struct MeshFlowControl *fc)
3606 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3607 fc->last_pid_recv = (uint32_t) -1;
3608 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3609 fc->last_ack_recv = (uint32_t) -1;
3610 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3611 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3617 * Destroy a channel and free all resources.
3619 * @param ch Channel to destroy.
3622 channel_destroy (struct MeshChannel *ch)
3624 struct MeshClient *c;
3632 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
3643 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
3650 if (GNUNET_YES == ch->reliable)
3652 channel_rel_free_all (ch->fwd_rel);
3653 channel_rel_free_all (ch->bck_rel);
3656 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
3657 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
3663 * Create a new channel.
3665 * @param owner Clients that owns the channel, NULL for foreign channels.
3666 * @param id Channel Number for the channel, for the owner point of view.
3668 * @return A new initialized channel. NULL on error.
3670 static struct MeshChannel *
3671 channel_new (struct MeshClient *owner,
3672 MESH_ChannelNumber id)
3674 struct MeshChannel *ch;
3679 ch = GNUNET_new (struct MeshChannel);
3683 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
3686 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, id, ch,
3687 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3690 channel_destroy (ch);
3691 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
3700 * Set options in a channel, extracted from a bit flag field
3702 * @param ch Channel to set options to.
3703 * @param options Bit array in host byte order.
3706 channel_set_options (struct MeshChannel *ch, uint32_t options)
3708 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3709 GNUNET_YES : GNUNET_NO;
3710 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3711 GNUNET_YES : GNUNET_NO;
3716 * Iterator for deleting each channel whose client endpoint disconnected.
3718 * @param cls Closure (client that has disconnected).
3719 * @param key The local channel id (used to access the hashmap).
3720 * @param value The value stored at the key (channel to destroy).
3722 * @return GNUNET_OK, keep iterating.
3725 channel_destroy_iterator (void *cls,
3729 struct MeshChannel *ch = value;
3730 struct MeshClient *c = cls;
3731 struct MeshTunnel2 *t;
3733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3734 " Channel %X / %X destroy, due to client %u shutdown.\n",
3735 ch->id, ch->id_dest, c->id);
3737 if (c == ch->client)
3739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3749 channel_send_destroy (ch);
3750 channel_destroy (ch);
3751 tunnel_destroy_if_empty (t);
3758 * Remove client's ports from the global hashmap on disconnect.
3760 * @param cls Closure (unused).
3762 * @param value Client structure.
3764 * @return GNUNET_OK, keep iterating.
3767 client_release_ports (void *cls,
3773 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3774 if (GNUNET_YES != res)
3777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3778 "Port %u by client %p was not registered.\n",
3786 * Timeout function due to lack of keepalive/traffic from the owner.
3787 * Destroys connection if called.
3789 * @param cls Closure (connection to destroy).
3790 * @param tc TaskContext.
3793 connection_fwd_timeout (void *cls,
3794 const struct GNUNET_SCHEDULER_TaskContext *tc)
3796 struct MeshConnection *c = cls;
3798 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3799 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3802 "Connection %s[%X] FWD timed out. Destroying.\n",
3803 GNUNET_i2s(GNUNET_PEER_resolve2 (c->t->peer->id)),
3806 if (NULL != c->t->channel_head) /* If local, leave TODO review */
3809 connection_destroy (c);
3814 * Timeout function due to lack of keepalive/traffic from the destination.
3815 * Destroys connection if called.
3817 * @param cls Closure (connection to destroy).
3818 * @param tc TaskContext
3821 connection_bck_timeout (void *cls,
3822 const struct GNUNET_SCHEDULER_TaskContext *tc)
3824 struct MeshConnection *c = cls;
3826 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3827 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3831 "Connection %s[%X] FWD timed out. Destroying.\n",
3832 GNUNET_i2s(GNUNET_PEER_resolve2 (c->t->peer->id)),
3835 if (NULL != c->t->channel_head) /* If local, leave TODO review */
3838 connection_destroy (c);
3843 * Resets the connection timeout task, some other message has done the
3845 * - For the first peer on the direction this means to send
3846 * a keepalive or a path confirmation message (either create or ACK).
3847 * - For all other peers, this means to destroy the connection,
3848 * due to lack of activity.
3849 * Starts the tiemout if no timeout was running (connection just created).
3851 * @param c Connection whose timeout to reset.
3852 * @param fwd Is this forward?
3854 * TODO use heap to improve efficiency of scheduler.
3857 connection_reset_timeout (struct MeshConnection *c, int fwd)
3859 GNUNET_SCHEDULER_TaskIdentifier *ti;
3860 GNUNET_SCHEDULER_Task f;
3862 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
3864 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3865 GNUNET_SCHEDULER_cancel (*ti);
3867 if (NULL != c->t->channel_head) /* Endpoint */
3869 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
3870 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
3874 struct GNUNET_TIME_Relative delay;
3876 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
3877 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
3878 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
3883 /******************************************************************************/
3884 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3885 /******************************************************************************/
3888 * Free a transmission that was already queued with all resources
3889 * associated to the request.
3891 * @param queue Queue handler to cancel.
3892 * @param clear_cls Is it necessary to free associated cls?
3895 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3897 struct MeshFlowControl *fc;
3899 fc = queue->peer->fc;
3900 if (GNUNET_YES == clear_cls)
3902 switch (queue->type)
3904 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
3905 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying CONNECTION_DESTROY\n");
3906 GNUNET_break (GNUNET_YES == queue->c->destroy);
3908 case GNUNET_MESSAGE_TYPE_MESH_FWD:
3909 case GNUNET_MESSAGE_TYPE_MESH_BCK:
3910 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3911 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3912 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3915 GNUNET_MESH_DEBUG_M2S (queue->type));
3918 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
3925 GNUNET_MESH_DEBUG_M2S (queue->type));
3927 GNUNET_free_non_null (queue->cls);
3929 GNUNET_CONTAINER_DLL_remove (fc->queue_head, fc->queue_tail, queue);
3933 GNUNET_free (queue);
3937 queue_send (void *cls, size_t size, void *buf)
3939 struct MeshPeer *peer = cls;
3940 const struct GNUNET_PeerIdentity *dst_id;
3941 struct GNUNET_MessageHeader *msg;
3942 struct MeshPeerQueue *queue;
3943 struct MeshTunnel2 *t;
3944 struct MeshFlowControl *fc;
3945 struct MeshConnection *c;
3957 fc->core_transmit = NULL;
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3961 if (NULL == buf || 0 == size)
3963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
3966 queue = fc->queue_head;
3968 /* Queue has no traffic */
3971 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3975 dst_id = GNUNET_PEER_resolve2 (peer->id);
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
3977 /* Check if buffer size is enough for the message */
3978 if (queue->size > size)
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
3982 GNUNET_CORE_notify_transmit_ready (core_handle,
3985 GNUNET_TIME_UNIT_FOREVER_REL,
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3995 t = (NULL != c) ? c->t : NULL;
3999 switch (queue->type)
4001 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4002 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4003 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4004 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4005 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4006 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 GNUNET_MESH_DEBUG_M2S (queue->type));
4011 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4012 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4013 data_size = send_core_data_raw (queue->cls, size, buf);
4014 msg = (struct GNUNET_MessageHeader *) buf;
4015 type = ntohs (msg->type);
4017 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4019 if (NULL != c->t->channel_head)
4020 data_size = send_core_connection_create (queue->cls, size, buf);
4022 data_size = send_core_data_raw (queue->cls, size, buf);
4024 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4026 if (NULL != c->t->channel_head)
4027 data_size = send_core_connection_ack (queue->cls, size, buf);
4029 data_size = send_core_data_raw (queue->cls, size, buf);
4033 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4040 if (0 < drop_percent &&
4041 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4044 "Dropping message of type %s\n",
4045 GNUNET_MESH_DEBUG_M2S (queue->type));
4048 /* Free queue, but cls was freed by send_core_* */
4049 queue_destroy (queue, GNUNET_NO);
4051 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4055 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4058 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4059 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4060 fc->last_pid_sent = pid;
4061 connection_send_ack (c, fwd);
4067 /* If more data in queue, send next */
4068 queue = fc->queue_head;
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4072 if (NULL == fc->core_transmit) {
4073 peer->fc->core_transmit =
4074 GNUNET_CORE_notify_transmit_ready(core_handle,
4077 GNUNET_TIME_UNIT_FOREVER_REL,
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 "* tmt rdy called somewhere else\n");
4088 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4090 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4092 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &peer_poll, fc);
4097 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4099 GNUNET_SCHEDULER_cancel (fc->poll_task);
4100 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4105 c->pending_messages--;
4106 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4109 connection_destroy (c);
4115 t->pending_messages--;
4116 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4128 queue_add (void *cls, uint16_t type, size_t size,
4129 struct MeshPeer *dst,
4130 struct MeshConnection *c,
4131 struct MeshChannel *ch)
4133 struct MeshPeerQueue *queue;
4134 struct MeshFlowControl *fc;
4146 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4147 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4151 ( (NULL != ch->owner && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4152 (NULL != ch->client && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4155 if (fc->queue_n >= fc->queue_max && 0 == priority)
4157 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4161 "queue full: %u/%u\n",
4162 fc->queue_n, fc->queue_max);
4163 return; /* Drop this message */
4167 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
4168 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4169 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4172 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4179 if (100 <= priority)
4180 GNUNET_CONTAINER_DLL_insert (fc->queue_head, fc->queue_tail, queue);
4182 GNUNET_CONTAINER_DLL_insert_tail (fc->queue_head, fc->queue_tail, queue);
4184 if (NULL == fc->core_transmit)
4187 GNUNET_CORE_notify_transmit_ready (core_handle,
4190 GNUNET_TIME_UNIT_FOREVER_REL,
4191 GNUNET_PEER_resolve2 (dst->id),
4196 c->pending_messages++;
4197 c->t->pending_messages++;
4201 /******************************************************************************/
4202 /******************** MESH NETWORK HANDLERS **************************/
4203 /******************************************************************************/
4207 * Core handler for connection creation.
4209 * @param cls Closure (unused).
4210 * @param peer Sender (neighbor).
4211 * @param message Message.
4213 * @return GNUNET_OK to keep the connection open,
4214 * GNUNET_SYSERR to close it (signal serious error)
4217 handle_mesh_connection_create (void *cls,
4218 const struct GNUNET_PeerIdentity *peer,
4219 const struct GNUNET_MessageHeader *message)
4221 struct GNUNET_MESH_ConnectionCreate *msg;
4222 struct GNUNET_PeerIdentity *id;
4223 struct GNUNET_HashCode *tid;
4224 struct MeshPeerPath *path;
4225 struct MeshPeer *dest_peer;
4226 struct MeshPeer *orig_peer;
4227 struct MeshConnection *c;
4228 unsigned int own_pos;
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
4236 size = ntohs (message->size);
4237 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
4239 GNUNET_break_op (0);
4243 /* Calculate hops */
4244 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
4245 if (size % sizeof (struct GNUNET_PeerIdentity))
4247 GNUNET_break_op (0);
4250 size /= sizeof (struct GNUNET_PeerIdentity);
4253 GNUNET_break_op (0);
4256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4258 /* Get parameters */
4259 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
4260 cid = ntohl (msg->cid);
4262 id = (struct GNUNET_PeerIdentity *) &msg[1];
4263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4264 " connection %s[%X] (%s).\n",
4265 GNUNET_h2s (tid), cid, GNUNET_i2s (id));
4267 /* Create connection */
4268 c = connection_get (tid, cid);
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
4272 c = connection_new (tid, cid);
4276 connection_reset_timeout (c, GNUNET_YES);
4277 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
4279 /* Remember peers */
4280 dest_peer = peer_get (&id[size - 1]);
4281 orig_peer = peer_get (&id[0]);
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4285 path = path_new (size);
4287 for (i = 0; i < size; i++)
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4290 GNUNET_i2s (&id[i]));
4291 path->peers[i] = GNUNET_PEER_intern (&id[i]);
4292 if (path->peers[i] == myid)
4295 if (own_pos == 0 && path->peers[own_pos] != myid)
4297 /* create path: self not found in path through self */
4298 GNUNET_break_op (0);
4299 path_destroy (path);
4300 connection_destroy (c);
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4304 path_add_to_peers (path, GNUNET_NO);
4306 c->own_pos = own_pos;
4308 /* Is it a connection to us? */
4309 if (own_pos == size - 1)
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4312 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
4314 send_connection_ack (c);
4316 /* Keep tunnel alive in direction dest->owner*/
4317 connection_reset_timeout (c, GNUNET_NO);
4321 /* It's for somebody else! Retransmit. */
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4323 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
4324 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
4325 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
4332 * Core handler for path ACKs
4334 * @param cls closure
4335 * @param message message
4336 * @param peer peer identity this notification is about
4338 * @return GNUNET_OK to keep the connection open,
4339 * GNUNET_SYSERR to close it (signal serious error)
4342 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4343 const struct GNUNET_MessageHeader *message)
4345 struct GNUNET_MESH_ConnectionACK *msg;
4346 struct MeshPeerPath *p;
4347 struct MeshConnection *c;
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
4351 msg = (struct GNUNET_MESH_ConnectionACK *) message;
4352 cid = ntohl(msg->cid);
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s[%X]\n",
4354 GNUNET_h2s (&msg->tid), cid);
4355 c = connection_get (&msg->tid, cid);
4358 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
4360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4367 /* Add path to peers? */
4371 path_add_to_peers (p, GNUNET_YES);
4377 connection_change_state (c, MESH_CONNECTION_READY);
4378 connection_reset_timeout (c, GNUNET_NO);
4380 /* Message for us? */
4381 if (NULL != c->t->channel_head)
4383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4384 if (3 <= tunnel_count_connections(c->t) && NULL != c->t->peer->dhtget)
4386 GNUNET_DHT_get_stop (c->t->peer->dhtget);
4387 c->t->peer->dhtget = NULL;
4389 connection_send_ack (c, GNUNET_NO); /* FIXME */
4393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4394 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
4400 * Core handler for notifications of broken paths
4402 * @param cls closure
4403 * @param message message
4404 * @param peer peer identity this notification is about
4406 * @return GNUNET_OK to keep the connection open,
4407 * GNUNET_SYSERR to close it (signal serious error)
4410 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4411 const struct GNUNET_MessageHeader *message)
4413 struct GNUNET_MESH_PathBroken *msg;
4414 struct MeshTunnel *t;
4416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4417 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4418 msg = (struct GNUNET_MESH_PathBroken *) message;
4419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4420 GNUNET_i2s (&msg->peer1));
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4422 GNUNET_i2s (&msg->peer2));
4423 t = channel_get (&msg->oid, ntohl (msg->tid));
4426 GNUNET_break_op (0);
4429 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4430 GNUNET_PEER_search (&msg->peer2));
4437 * Core handler for tunnel destruction
4439 * @param cls closure
4440 * @param message message
4441 * @param peer peer identity this notification is about
4443 * @return GNUNET_OK to keep the connection open,
4444 * GNUNET_SYSERR to close it (signal serious error)
4447 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4448 const struct GNUNET_MessageHeader *message)
4450 struct GNUNET_MESH_TunnelDestroy *msg;
4451 struct MeshTunnel *t;
4453 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 "Got a TUNNEL DESTROY packet from %s\n",
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4458 " for tunnel %s [%u]\n",
4459 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4460 t = channel_get (&msg->oid, ntohl (msg->tid));
4463 /* Probably already got the message from another path,
4464 * destroyed the tunnel and retransmitted to children.
4467 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4471 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4474 t->local_tid, t->local_tid_dest);
4476 if (GNUNET_PEER_search (peer) == t->prev_hop)
4478 // TODO check owner's signature
4479 // TODO add owner's signatue to tunnel for retransmission
4480 peer_cancel_queues (t->prev_hop, t);
4481 GNUNET_PEER_change_rc (t->prev_hop, -1);
4484 else if (GNUNET_PEER_search (peer) == t->next_hop)
4486 // TODO check dest's signature
4487 // TODO add dest's signatue to tunnel for retransmission
4488 peer_cancel_queues (t->next_hop, t);
4489 GNUNET_PEER_change_rc (t->next_hop, -1);
4494 GNUNET_break_op (0);
4495 // TODO check both owner AND destination's signature to see which matches
4496 // TODO restransmit in appropriate direction
4499 tunnel_destroy_empty (t);
4501 // TODO: add timeout to destroy the tunnel anyway
4507 * Generic handler for mesh network payload traffic.
4509 * @param peer Peer identity this notification is about.
4510 * @param message Data message.
4511 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4513 * @return GNUNET_OK to keep the connection open,
4514 * GNUNET_SYSERR to close it (signal serious error)
4517 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
4518 const struct GNUNET_MessageHeader *message,
4521 struct GNUNET_MESH_Data *msg;
4522 struct MeshFlowControl *fc;
4523 struct MeshChannelReliability *rel;
4524 struct MeshTunnel *t;
4525 struct MeshClient *c;
4533 size = ntohs (message->size);
4535 sizeof (struct GNUNET_MESH_Data) +
4536 sizeof (struct GNUNET_MessageHeader))
4541 type =ntohs (message->type);
4542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4543 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4544 msg = (struct GNUNET_MESH_Data *) message;
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4546 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4548 t = channel_get (&msg->oid, ntohl (msg->tid));
4551 /* TODO notify back: we don't know this tunnel */
4552 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING tunnel unknown\n");
4557 /* Initialize FWD/BCK data */
4558 pid = ntohl (msg->pid);
4559 fc = fwd ? &t->prev_fc : &t->next_fc;
4560 c = fwd ? t->client : t->owner;
4561 rel = fwd ? t->bck_rel : t->fwd_rel;
4562 hop = fwd ? t->next_hop : t->prev_hop;
4563 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4565 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4567 "WARNING Received PID %u, (prev %u), ACK %u\n",
4568 pid, fc->last_pid_recv, fc->last_ack_sent);
4572 tunnel_change_state (t, MESH_TUNNEL_READY);
4573 tunnel_reset_timeout (t, fwd);
4576 /* TODO signature verification */
4577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4578 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4579 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4583 mid = ntohl (msg->mid);
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 " pid %u (mid %u) not seen yet\n", pid, mid);
4586 fc->last_pid_recv = pid;
4588 if (GNUNET_NO == t->reliable ||
4589 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4590 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4593 "!!! RECV %u\n", ntohl (msg->mid));
4594 if (GNUNET_YES == t->reliable)
4596 /* Is this the exact next expected messasge? */
4597 if (mid == rel->mid_recv)
4599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4601 tunnel_send_client_data (t, msg, fwd);
4602 tunnel_send_client_buffered_data (t, c, rel);
4606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4607 tunnel_add_buffered_data (t, msg, rel);
4610 else /* Tunnel unreliable, send to clients directly */
4612 tunnel_send_client_data (t, msg, fwd);
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4618 " MID %u not expected (%u - %u), dropping!\n",
4619 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4624 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4626 " Pid %u not expected (%u+), dropping!\n",
4627 pid, fc->last_pid_recv + 1);
4629 tunnel_send_ack (t, type, fwd);
4632 fc->last_pid_recv = pid;
4635 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4637 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4638 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4640 ttl = ntohl (msg->ttl);
4641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4644 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4646 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4653 send_prebuilt_message (message, hop, t);
4654 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4661 * Core handler for mesh network traffic going from the origin to a peer
4663 * @param cls Closure (unused).
4664 * @param message Message received.
4665 * @param peer Peer who sent the message.
4667 * @return GNUNET_OK to keep the connection open,
4668 * GNUNET_SYSERR to close it (signal serious error)
4671 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4672 const struct GNUNET_MessageHeader *message)
4674 return handle_mesh_data (peer, message, GNUNET_YES);
4678 * Core handler for mesh network traffic towards the owner of a tunnel.
4680 * @param cls Closure (unused).
4681 * @param message Message received.
4682 * @param peer Peer who sent the message.
4684 * @return GNUNET_OK to keep the connection open,
4685 * GNUNET_SYSERR to close it (signal serious error)
4688 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4689 const struct GNUNET_MessageHeader *message)
4691 return handle_mesh_data (peer, message, GNUNET_NO);
4696 * Core handler for mesh network traffic end-to-end ACKs.
4698 * @param cls Closure.
4699 * @param message Message.
4700 * @param peer Peer identity this notification is about.
4702 * @return GNUNET_OK to keep the connection open,
4703 * GNUNET_SYSERR to close it (signal serious error)
4706 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4707 const struct GNUNET_MessageHeader *message)
4709 struct GNUNET_MESH_DataACK *msg;
4710 struct MeshChannelReliability *rel;
4711 struct MeshReliableMessage *copy;
4712 struct MeshReliableMessage *next;
4713 struct MeshTunnel *t;
4719 type = ntohs (message->type);
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4721 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4722 msg = (struct GNUNET_MESH_DataACK *) message;
4724 t = channel_get (&msg->oid, ntohl (msg->tid));
4727 /* TODO notify that we dont know this tunnel (whom)? */
4728 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4731 ack = ntohl (msg->mid);
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4734 /* Is this a forward or backward ACK? */
4735 id = GNUNET_PEER_search (peer);
4736 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4739 if (NULL == t->owner)
4741 send_prebuilt_message (message, t->prev_hop, t);
4746 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4749 if (NULL == t->client)
4751 send_prebuilt_message (message, t->next_hop, t);
4758 GNUNET_break_op (0);
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4763 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4765 if (GMC_is_pid_bigger (copy->mid, ack))
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4768 tunnel_free_sent_reliable (t, msg, rel);
4772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4774 tunnel_free_reliable_message (copy);
4776 /* Once buffers have been free'd, send ACK */
4777 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4779 /* If some message was free'd, update the retransmission delay*/
4780 if (GNUNET_YES == work)
4782 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4784 GNUNET_SCHEDULER_cancel (rel->retry_task);
4785 if (NULL == rel->head_sent)
4787 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4791 struct GNUNET_TIME_Absolute new_target;
4792 struct GNUNET_TIME_Relative delay;
4794 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4795 MESH_RETRANSMIT_MARGIN);
4796 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4798 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4800 GNUNET_SCHEDULER_add_delayed (delay,
4801 &tunnel_retransmit_message,
4812 * Core handler for mesh network traffic point-to-point acks.
4814 * @param cls closure
4815 * @param message message
4816 * @param peer peer identity this notification is about
4818 * @return GNUNET_OK to keep the connection open,
4819 * GNUNET_SYSERR to close it (signal serious error)
4822 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4823 const struct GNUNET_MessageHeader *message)
4825 struct GNUNET_MESH_ACK *msg;
4826 struct MeshTunnel *t;
4827 struct MeshFlowControl *fc;
4831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4833 msg = (struct GNUNET_MESH_ACK *) message;
4835 t = channel_get (&msg->oid, ntohl (msg->tid));
4839 /* TODO notify that we dont know this tunnel (whom)? */
4840 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4843 ack = ntohl (msg->pid);
4844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4846 /* Is this a forward or backward ACK? */
4847 id = GNUNET_PEER_search (peer);
4848 if (t->next_hop == id)
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4853 else if (t->prev_hop == id)
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4860 GNUNET_break_op (0);
4864 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4865 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4867 GNUNET_SCHEDULER_cancel (fc->poll_task);
4868 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4869 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4872 fc->last_ack_recv = ack;
4873 peer_unlock_queue (id);
4874 tunnel_change_state (t, MESH_TUNNEL_READY);
4876 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4883 * Core handler for mesh network traffic point-to-point ack polls.
4885 * @param cls closure
4886 * @param message message
4887 * @param peer peer identity this notification is about
4889 * @return GNUNET_OK to keep the connection open,
4890 * GNUNET_SYSERR to close it (signal serious error)
4893 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4894 const struct GNUNET_MessageHeader *message)
4896 struct GNUNET_MESH_Poll *msg;
4897 struct MeshTunnel *t;
4898 struct MeshFlowControl *fc;
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
4906 msg = (struct GNUNET_MESH_Poll *) message;
4908 t = channel_get (&msg->oid, ntohl (msg->tid));
4912 /* TODO notify that we dont know this tunnel (whom)? */
4913 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4914 GNUNET_break_op (0);
4918 /* Is this a forward or backward ACK? */
4919 id = GNUNET_PEER_search (peer);
4920 pid = ntohl (msg->pid);
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4922 if (t->next_hop == id)
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4926 old = fc->last_pid_recv;
4928 else if (t->prev_hop == id)
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4932 old = fc->last_pid_recv;
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4941 fc->last_pid_recv = pid;
4942 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4944 if (GNUNET_YES == t->reliable)
4945 fc->last_pid_recv = old;
4952 * Core handler for mesh keepalives.
4954 * @param cls closure
4955 * @param message message
4956 * @param peer peer identity this notification is about
4957 * @return GNUNET_OK to keep the connection open,
4958 * GNUNET_SYSERR to close it (signal serious error)
4960 * TODO: Check who we got this from, to validate route.
4963 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4964 const struct GNUNET_MessageHeader *message)
4966 struct GNUNET_MESH_TunnelKeepAlive *msg;
4967 struct MeshTunnel *t;
4968 struct MeshClient *c;
4972 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4976 t = channel_get (&msg->oid, ntohl (msg->tid));
4979 /* TODO notify that we dont know that tunnel */
4980 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4985 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
4986 GNUNET_YES : GNUNET_NO;
4987 c = fwd ? t->client : t->owner;
4988 hop = fwd ? t->next_hop : t->prev_hop;
4991 tunnel_change_state (t, MESH_TUNNEL_READY);
4992 tunnel_reset_timeout (t, fwd);
4993 if (NULL != c || 0 == hop || myid == hop)
4996 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4997 send_prebuilt_message (message, hop, t);
5004 * Functions to handle messages from core
5006 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5007 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5009 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5010 sizeof (struct GNUNET_MESH_ConnectionACK)},
5011 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5012 sizeof (struct GNUNET_MESH_PathBroken)},
5013 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5014 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5015 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5016 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5017 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
5018 sizeof (struct GNUNET_MESH_DataACK)},
5019 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
5020 sizeof (struct GNUNET_MESH_DataACK)},
5021 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5022 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5023 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5024 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5025 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5026 sizeof (struct GNUNET_MESH_ACK)},
5027 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5028 sizeof (struct GNUNET_MESH_Poll)},
5034 * Function to process paths received for a new peer addition. The recorded
5035 * paths form the initial tunnel, which can be optimized later.
5036 * Called on each result obtained for the DHT search.
5038 * @param cls closure
5039 * @param exp when will this value expire
5040 * @param key key of the result
5041 * @param get_path path of the get request
5042 * @param get_path_length lenght of get_path
5043 * @param put_path path of the put request
5044 * @param put_path_length length of the put_path
5045 * @param type type of the result
5046 * @param size number of bytes in data
5047 * @param data pointer to the result data
5050 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5051 const struct GNUNET_HashCode * key,
5052 const struct GNUNET_PeerIdentity *get_path,
5053 unsigned int get_path_length,
5054 const struct GNUNET_PeerIdentity *put_path,
5055 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5056 size_t size, const void *data)
5058 struct MeshPeer *peer = cls;
5059 struct MeshPeerPath *p;
5060 struct MeshConnection *c;
5061 struct GNUNET_PeerIdentity pi;
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5065 GNUNET_PEER_resolve (peer->id, &pi);
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5068 p = path_build_from_dht (get_path, get_path_length,
5069 put_path, put_path_length);
5070 path_add_to_peers (p, GNUNET_NO);
5073 /* Count connections */
5074 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5076 /* If we already have 3 (or more (?!)) connections, it's enough */
5080 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5083 peer_connect (peer);
5089 /******************************************************************************/
5090 /********************* MESH LOCAL HANDLES **************************/
5091 /******************************************************************************/
5095 * Handler for client connection.
5097 * @param cls Closure (unused).
5098 * @param client Client handler.
5101 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5103 struct MeshClient *c;
5107 c = GNUNET_malloc (sizeof (struct MeshClient));
5109 GNUNET_SERVER_client_keep (client);
5110 GNUNET_SERVER_client_set_user_context (client, c);
5111 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5116 * Handler for client disconnection
5118 * @param cls closure
5119 * @param client identification of the client; NULL
5120 * for the last call when the server is destroyed
5123 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5125 struct MeshClient *c;
5126 struct MeshClient *next;
5128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5135 c = client_get (client);
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5140 GNUNET_SERVER_client_drop (c->handle);
5141 c->shutting_down = GNUNET_YES;
5142 if (NULL != c->own_tunnels)
5144 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
5145 &tunnel_destroy_iterator, c);
5146 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
5149 if (NULL != c->incoming_tunnels)
5151 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
5152 &tunnel_destroy_iterator, c);
5153 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
5156 if (NULL != c->ports)
5158 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5159 &client_release_ports, c);
5160 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5163 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5166 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
5179 * Handler for new clients
5181 * @param cls closure
5182 * @param client identification of the client
5183 * @param message the actual message, which includes messages the client wants
5186 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5187 const struct GNUNET_MessageHeader *message)
5189 struct GNUNET_MESH_ClientConnect *cc_msg;
5190 struct MeshClient *c;
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
5197 /* Check data sanity */
5198 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5199 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5200 if (0 != (size % sizeof (uint32_t)))
5203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5206 size /= sizeof (uint32_t);
5208 /* Initialize new client structure */
5209 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
5210 c->id = next_client_id++; /* overflow not important: just for debug */
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
5217 p = (uint32_t *) &cc_msg[1];
5218 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
5219 for (i = 0; i < size; i++)
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
5224 /* store in client's hashmap */
5225 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
5226 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5227 /* store in global hashmap */
5228 /* FIXME only allow one client to have the port open,
5229 * have a backup hashmap with waiting clients */
5230 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5231 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5235 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5236 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
5237 GNUNET_SERVER_notification_context_add (nc, client);
5238 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5240 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5246 * Handler for requests of new tunnels
5248 * @param cls Closure.
5249 * @param client Identification of the client.
5250 * @param message The actual message.
5253 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
5254 const struct GNUNET_MessageHeader *message)
5256 struct GNUNET_MESH_ChannelMessage *msg;
5257 struct MeshPeer *peer;
5258 struct MeshTunnel2 *t;
5259 struct MeshChannel *ch;
5260 struct MeshClient *c;
5261 MESH_ChannelNumber chid;
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
5265 /* Sanity check for client registration */
5266 if (NULL == (c = client_get (client)))
5269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5274 /* Message size sanity check */
5275 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5282 msg = (struct GNUNET_MESH_ChannelMessage *) message;
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5284 GNUNET_i2s (&msg->peer), ntohl (msg->port));
5285 chid = ntohl (msg->channel_id);
5287 /* Sanity check for duplicate tunnel IDs */
5288 if (NULL != channel_get_by_local_id (c, chid))
5291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5295 peer = peer_get (&msg->peer);
5296 if (NULL == peer->tunnel)
5297 peer->tunnel = tunnel_new ();
5300 /* Create channel */
5301 while (NULL != channel_get_by_pi (myid, next_tid))
5302 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
5303 t = tunnel_new (myid, next_tid, c, chid);
5304 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
5308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5311 t->port = ntohl (msg->port);
5312 tunnel_set_options (t, ntohl (msg->opt));
5313 if (GNUNET_YES == t->reliable)
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5316 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5318 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
5322 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
5324 peer_info = peer_get (&msg->peer);
5325 peer_add_tunnel (peer_info, t);
5326 peer_connect (peer_info, t);
5327 tunnel_reset_timeout (t, GNUNET_YES);
5328 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5334 * Handler for requests of deleting tunnels
5336 * @param cls closure
5337 * @param client identification of the client
5338 * @param message the actual message
5341 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5342 const struct GNUNET_MessageHeader *message)
5344 struct GNUNET_MESH_ChannelMessage *tunnel_msg;
5345 struct MeshClient *c;
5346 struct MeshTunnel *t;
5347 MESH_ChannelNumber tid;
5349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5350 "Got a DESTROY TUNNEL from client!\n");
5352 /* Sanity check for client registration */
5353 if (NULL == (c = client_get (client)))
5356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5361 /* Message sanity check */
5362 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
5365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5369 tunnel_msg = (struct GNUNET_MESH_ChannelMessage *) message;
5371 /* Retrieve tunnel */
5372 tid = ntohl (tunnel_msg->channel_id);
5373 t = channel_get_by_local_id (c, tid);
5376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5382 /* Cleanup after the tunnel */
5383 client_delete_tunnel (c, t);
5384 if (c == t->client && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= tid)
5388 else if (c == t->owner && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > tid)
5390 peer_remove_tunnel (peer_get_short (t->dest), t);
5395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5396 " tunnel %X client %p (%p, %p)\n",
5397 tid, c, t->owner, t->client);
5401 /* The tunnel will be destroyed when the last message is transmitted. */
5402 tunnel_destroy_empty (t);
5404 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5410 * Handler for client traffic
5412 * @param cls closure
5413 * @param client identification of the client
5414 * @param message the actual message
5417 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5418 const struct GNUNET_MessageHeader *message)
5420 struct GNUNET_MESH_LocalData *data_msg;
5421 struct MeshClient *c;
5422 struct MeshTunnel *t;
5423 struct MeshFlowControl *fc;
5424 MESH_ChannelNumber tid;
5427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5428 "Got data from a client!\n");
5430 /* Sanity check for client registration */
5431 if (NULL == (c = client_get (client)))
5434 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5439 data_msg = (struct GNUNET_MESH_LocalData *) message;
5441 /* Sanity check for message size */
5442 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5443 if (size < sizeof (struct GNUNET_MessageHeader))
5446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5450 /* Tunnel exists? */
5451 tid = ntohl (data_msg->tid);
5452 t = channel_get_by_local_id (c, tid);
5456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5460 /* Is the client in the tunnel? */
5461 if ( !( (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV &&
5463 t->owner->handle == client)
5465 (tid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV &&
5467 t->client->handle == client) ) )
5470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5474 /* Ok, everything is correct, send the message
5475 * (pretend we got it from a mesh peer)
5478 struct GNUNET_MESH_Data *payload;
5479 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5481 fc = tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5482 if (GNUNET_YES == t->reliable)
5484 struct MeshChannelReliability *rel;
5485 struct MeshReliableMessage *copy;
5487 rel = (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5488 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5489 + sizeof(struct GNUNET_MESH_Data)
5491 copy->mid = rel->mid_sent++;
5492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5493 copy->timestamp = GNUNET_TIME_absolute_get ();
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5497 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5498 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5501 GNUNET_TIME_relative_multiply (rel->expected_delay,
5502 MESH_RETRANSMIT_MARGIN);
5504 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5505 &tunnel_retransmit_message,
5508 payload = (struct GNUNET_MESH_Data *) ©[1];
5509 payload->mid = htonl (copy->mid);
5513 payload = (struct GNUNET_MESH_Data *) cbuf;
5514 payload->mid = htonl (fc->last_pid_recv + 1);
5516 memcpy (&payload[1], &data_msg[1], size);
5517 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5518 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ?
5519 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5520 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5521 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5522 payload->tid = htonl (t->id.tid);
5523 payload->ttl = htonl (default_ttl);
5524 payload->pid = htonl (fc->last_pid_recv + 1);
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5526 " calling generic handler...\n");
5527 if (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
5528 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5530 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5540 * Handler for client's ACKs for payload traffic.
5542 * @param cls Closure (unused).
5543 * @param client Identification of the client.
5544 * @param message The actual message.
5547 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5548 const struct GNUNET_MessageHeader *message)
5550 struct GNUNET_MESH_LocalAck *msg;
5551 struct MeshTunnel *t;
5552 struct MeshClient *c;
5553 MESH_ChannelNumber tid;
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5556 /* Sanity check for client registration */
5557 if (NULL == (c = client_get (client)))
5560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5565 msg = (struct GNUNET_MESH_LocalAck *) message;
5567 /* Tunnel exists? */
5568 tid = ntohl (msg->channel_id);
5569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5570 t = channel_get_by_local_id (c, tid);
5574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5580 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5581 if (tid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
5583 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5584 t->prev_fc.last_ack_recv++;
5585 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5589 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5590 t->next_fc.last_ack_recv++;
5591 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5594 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5602 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5604 * @param cls Closure (client handle).
5605 * @param key Key (hashed tunnel ID, unused).
5606 * @param value Tunnel info.
5608 * @return GNUNET_YES, to keep iterating.
5611 monitor_all_tunnels_iterator (void *cls,
5612 const struct GNUNET_HashCode * key,
5615 struct GNUNET_SERVER_Client *client = cls;
5616 struct MeshChannel *ch = value;
5617 struct GNUNET_MESH_LocalMonitor *msg;
5619 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5620 msg->channel_id = htonl (ch->id);
5621 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5622 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5624 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5625 "* sending info about tunnel %s\n",
5626 GNUNET_i2s (&msg->owner));
5628 GNUNET_SERVER_notification_context_unicast (nc, client,
5629 &msg->header, GNUNET_NO);
5635 * Handler for client's MONITOR request.
5637 * @param cls Closure (unused).
5638 * @param client Identification of the client.
5639 * @param message The actual message.
5642 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5643 const struct GNUNET_MessageHeader *message)
5645 struct MeshClient *c;
5647 /* Sanity check for client registration */
5648 if (NULL == (c = client_get (client)))
5651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5655 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5656 "Received get tunnels request from client %u\n",
5658 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5659 monitor_all_tunnels_iterator,
5661 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5662 "Get tunnels request from client %u completed\n",
5664 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5669 * Handler for client's MONITOR_TUNNEL request.
5671 * @param cls Closure (unused).
5672 * @param client Identification of the client.
5673 * @param message The actual message.
5676 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5677 const struct GNUNET_MessageHeader *message)
5679 const struct GNUNET_MESH_LocalMonitor *msg;
5680 struct GNUNET_MESH_LocalMonitor *resp;
5681 struct MeshClient *c;
5682 struct MeshChannel *ch;
5684 /* Sanity check for client registration */
5685 if (NULL == (c = client_get (client)))
5688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5692 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5693 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5694 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5697 ntohl (msg->channel_id));
5698 ch = channel_get (&msg->owner, ntohl (msg->channel_id));
5701 /* We don't know the tunnel */
5702 struct GNUNET_MESH_LocalMonitor warn;
5705 GNUNET_SERVER_notification_context_unicast (nc, client,
5708 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5712 /* Initialize context */
5713 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5715 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5716 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5717 &resp->header, GNUNET_NO);
5720 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5721 "Monitor tunnel request from client %u completed\n",
5723 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5728 * Functions to handle messages from clients
5730 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5731 {&handle_local_new_client, NULL,
5732 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5733 {&handle_local_channel_create, NULL,
5734 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
5735 sizeof (struct GNUNET_MESH_ChannelMessage)},
5736 {&handle_local_channel_destroy, NULL,
5737 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
5738 sizeof (struct GNUNET_MESH_ChannelMessage)},
5739 {&handle_local_data, NULL,
5740 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5741 {&handle_local_ack, NULL,
5742 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5743 sizeof (struct GNUNET_MESH_LocalAck)},
5744 {&handle_local_get_tunnels, NULL,
5745 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5746 sizeof (struct GNUNET_MessageHeader)},
5747 {&handle_local_show_tunnel, NULL,
5748 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5749 sizeof (struct GNUNET_MESH_LocalMonitor)},
5755 * Method called whenever a given peer connects.
5757 * @param cls closure
5758 * @param peer peer identity this notification is about
5761 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5763 struct MeshPeer *peer_info;
5764 struct MeshPeerPath *path;
5766 DEBUG_CONN ("Peer connected\n");
5767 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5768 peer_info = peer_get (peer);
5769 if (myid == peer_info->id)
5771 DEBUG_CONN (" (self)\n");
5772 path = path_new (1);
5776 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5777 path = path_new (2);
5778 path->peers[1] = peer_info->id;
5779 GNUNET_PEER_change_rc (peer_info->id, 1);
5780 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5782 path->peers[0] = myid;
5783 GNUNET_PEER_change_rc (myid, 1);
5784 peer_add_path (peer_info, path, GNUNET_YES);
5785 if (NULL == peer_info->fc)
5787 peer_info->fc = GNUNET_new (struct MeshFlowControl);
5788 fc_init (peer_info->fc);
5789 peer_info->fc->peer = peer_info;
5796 * Method called whenever a peer disconnects.
5798 * @param cls closure
5799 * @param peer peer identity this notification is about
5802 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5804 struct MeshPeer *pi;
5805 struct MeshPeerQueue *q;
5806 struct MeshPeerQueue *n;
5807 struct MeshFlowControl *fc;
5809 DEBUG_CONN ("Peer disconnected\n");
5810 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5828 queue_destroy (q, GNUNET_YES);
5831 if (NULL != fc->core_transmit)
5832 GNUNET_CORE_notify_transmit_ready_cancel (fc->core_transmit);
5833 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
5834 GNUNET_SCHEDULER_cancel (fc->poll_task);
5836 peer_remove_path (pi, pi->id, myid);
5839 DEBUG_CONN (" (self)\n");
5841 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5849 * Install server (service) handlers and start listening to clients.
5854 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5855 GNUNET_SERVER_connect_notify (server_handle,
5856 &handle_local_client_connect, NULL);
5857 GNUNET_SERVER_disconnect_notify (server_handle,
5858 &handle_local_client_disconnect, NULL);
5859 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5861 clients_head = NULL;
5862 clients_tail = NULL;
5864 GNUNET_SERVER_resume (server_handle);
5869 * To be called on core init/fail.
5871 * @param cls Closure (config)
5872 * @param server handle to the server for this service
5873 * @param identity the public identity of this peer
5876 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5877 const struct GNUNET_PeerIdentity *identity)
5879 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5883 GNUNET_break (core_handle == server);
5884 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5890 GNUNET_i2s (identity));
5891 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5893 GNUNET_i2s (&my_full_id));
5894 GNUNET_CORE_disconnect (core_handle);
5895 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5896 NULL, /* Closure passed to MESH functions */
5897 &core_init, /* Call core_init once connected */
5898 &core_connect, /* Handle connects */
5899 &core_disconnect, /* remove peers on disconnects */
5900 NULL, /* Don't notify about all incoming messages */
5901 GNUNET_NO, /* For header only in notification */
5902 NULL, /* Don't notify about all outbound messages */
5903 GNUNET_NO, /* For header-only out notification */
5904 core_handlers); /* Register these handlers */
5913 /******************************************************************************/
5914 /************************ MAIN FUNCTIONS ****************************/
5915 /******************************************************************************/
5918 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5920 * @param cls closure
5921 * @param key current key code
5922 * @param value value in the hash map
5923 * @return GNUNET_YES if we should continue to iterate,
5927 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5929 struct MeshTunnel2 *t = value;
5936 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5938 * @param cls closure
5939 * @param key current key code
5940 * @param value value in the hash map
5941 * @return GNUNET_YES if we should continue to iterate,
5945 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5947 struct MeshPeer *p = value;
5948 struct MeshPeerQueue *q;
5949 struct MeshPeerQueue *n;
5951 q = p->fc->queue_head;
5957 queue_destroy(q, GNUNET_YES);
5967 * Task run during shutdown.
5973 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5977 if (core_handle != NULL)
5979 GNUNET_CORE_disconnect (core_handle);
5982 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5983 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5984 if (dht_handle != NULL)
5986 GNUNET_DHT_disconnect (dht_handle);
5991 GNUNET_SERVER_notification_context_destroy (nc);
5994 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5996 GNUNET_SCHEDULER_cancel (announce_id_task);
5997 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6004 * Process mesh requests.
6006 * @param cls closure
6007 * @param server the initialized server
6008 * @param c configuration to use
6011 run (void *cls, struct GNUNET_SERVER_Handle *server,
6012 const struct GNUNET_CONFIGURATION_Handle *c)
6015 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6018 server_handle = server;
6019 GNUNET_SERVER_suspend (server_handle);
6022 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6027 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6028 "mesh", "peer/privatekey");
6029 GNUNET_SCHEDULER_shutdown ();
6034 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6035 &refresh_connection_time))
6037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6039 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6040 "mesh", "refresh path time");
6041 GNUNET_SCHEDULER_shutdown ();
6046 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6051 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6052 "mesh", "id announce time");
6053 GNUNET_SCHEDULER_shutdown ();
6058 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6061 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6063 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6064 "mesh", "connect timeout");
6065 GNUNET_SCHEDULER_shutdown ();
6070 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6075 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6076 "mesh", "max msgs queue");
6077 GNUNET_SCHEDULER_shutdown ();
6082 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6085 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6087 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6088 "mesh", "max tunnels");
6089 GNUNET_SCHEDULER_shutdown ();
6094 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6097 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6099 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6100 "mesh", "default ttl", 64);
6105 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6108 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6109 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6110 "mesh", "max peers", 1000);
6115 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6122 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6123 "Mesh is running with drop mode enabled. "
6124 "This is NOT a good idea! "
6125 "Remove the DROP_PERCENT option from your configuration.\n");
6129 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6130 &dht_replication_level))
6132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6134 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6135 "mesh", "dht replication level", 3);
6136 dht_replication_level = 3;
6139 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6140 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6141 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6143 dht_handle = GNUNET_DHT_connect (c, 64);
6144 if (NULL == dht_handle)
6148 stats = GNUNET_STATISTICS_create ("mesh", c);
6150 /* Scheduled the task to clean up when shutdown is called */
6151 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6153 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6154 GNUNET_free (keyfile);
6155 GNUNET_assert (NULL != pk);
6156 my_private_key = pk;
6157 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6158 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6159 &my_full_id.hashPubKey);
6160 myid = GNUNET_PEER_intern (&my_full_id);
6161 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6162 "Mesh for peer [%s] starting\n",
6163 GNUNET_i2s(&my_full_id));
6165 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6166 NULL, /* Closure passed to MESH functions */
6167 &core_init, /* Call core_init once connected */
6168 &core_connect, /* Handle connects */
6169 &core_disconnect, /* remove peers on disconnects */
6170 NULL, /* Don't notify about all incoming messages */
6171 GNUNET_NO, /* For header only in notification */
6172 NULL, /* Don't notify about all outbound messages */
6173 GNUNET_NO, /* For header-only out notification */
6174 core_handlers); /* Register these handlers */
6175 if (NULL == core_handle)
6178 GNUNET_SCHEDULER_shutdown ();
6181 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6187 * The main function for the mesh service.
6189 * @param argc number of arguments from the command line
6190 * @param argv command line arguments
6191 * @return 0 ok, 1 on error
6194 main (int argc, char *const *argv)
6199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6200 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6202 ret = (GNUNET_OK == r) ? 0 : 1;
6203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");