2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-enc.c
23 * @brief GNUnet MESH service with encryption
24 * @author Bartlomiej Polot
27 * - when sending in-order buffered data, wait for client ACKs
30 * - set connection IDs independently from tunnel, tunnel has no ID
33 * - relay corking down to core
34 * - set ttl relative to path length
38 * - peer: other mesh instance. If there is direct connection it's a neighbor.
39 * - tunnel: encrypted connection to a peer, neighbor or not.
40 * - channel: connection between two clients, on the same or different peers.
41 * have properties like reliability.
42 * - path: series of directly connected peer from one peer to another.
43 * - connection: path which is being used in a tunnel.
47 #include "gnunet_crypto_lib.h"
49 #include "mesh_protocol_enc.h"
50 #include "mesh_path.h"
51 #include "block_mesh.h"
52 #include "gnunet_dht_service.h"
53 #include "gnunet_statistics_service.h"
55 #define MESH_BLOOM_SIZE 128
57 #define MESH_DEBUG_DHT GNUNET_NO
58 #define MESH_DEBUG_CONNECTION GNUNET_YES
59 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
61 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
62 GNUNET_TIME_UNIT_MINUTES,\
64 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
65 #define MESH_RETRANSMIT_MARGIN 4
67 #if MESH_DEBUG_CONNECTION
68 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
70 #define DEBUG_CONN(...)
74 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
76 #define DEBUG_DHT(...)
83 struct timespec __mesh_start;
84 struct timespec __mesh_end;
85 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
86 #define INTERVAL_END \
88 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
89 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
90 if (__diff < 0) __diff += 1000000000;\
94 #define INTERVAL_SHOW \
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
98 #define INTERVAL_START
100 #define INTERVAL_SHOW
104 * All the states a tunnel can be in.
109 * Uninitialized status, should never appear in operation.
114 * Path to the peer not known yet
116 MESH_TUNNEL_SEARCHING,
119 * Request sent, not yet answered.
124 * Peer connected and ready to accept data
129 * Peer connected previosly but not responding
131 MESH_TUNNEL_RECONNECTING
136 * All the states a connection can be in.
138 enum MeshConnectionState
141 * Uninitialized status, should never appear in operation.
146 * Connection create message sent, waiting for ACK.
148 MESH_CONNECTION_SENT,
151 * Connection ACK sent, waiting for ACK.
156 * Connection confirmed, ready to carry traffic.
158 MESH_CONNECTION_READY,
163 * All the states a connection can be in.
165 enum MeshChannelState
168 * Uninitialized status, should never appear in operation.
173 * Connection create message sent, waiting for ACK.
178 * Connection confirmed, ready to carry traffic..
184 /******************************************************************************/
185 /************************ DATA STRUCTURES ****************************/
186 /******************************************************************************/
188 /** FWD declaration */
192 struct MeshConnection;
194 struct MeshChannelReliability;
198 * Struct containing info about a queued transmission to this peer
205 struct MeshPeerQueue *next;
210 struct MeshPeerQueue *prev;
213 * Peer this transmission is directed to.
215 struct MeshPeer *peer;
218 * Connection this message belongs to.
220 struct MeshConnection *c;
228 * Channel this message belongs to, if known.
230 struct MeshChannel *ch;
233 * Pointer to info stucture used as cls.
243 * Size of the message
250 * Struct to encapsulate all the Flow Control information to a peer to which
251 * we are directly connected (on a core level).
253 struct MeshFlowControl
256 * Connection this controls.
258 struct MeshConnection *c;
261 * How many messages are in the queue on this connection.
263 unsigned int queue_n;
266 * How many messages do we accept in the queue.
268 unsigned int queue_max;
276 * ID of the last packet sent towards the peer.
278 uint32_t last_pid_sent;
281 * ID of the last packet received from the peer.
283 uint32_t last_pid_recv;
286 * Last ACK sent to the peer (peer can't send more than this PID).
288 uint32_t last_ack_sent;
291 * Last ACK sent towards the origin (for traffic towards leaf node).
293 uint32_t last_ack_recv;
296 * Task to poll the peer in case of a lost ACK causes stall.
298 GNUNET_SCHEDULER_TaskIdentifier poll_task;
301 * How frequently to poll for ACKs.
303 struct GNUNET_TIME_Relative poll_time;
308 * Struct containing all information regarding a given peer
318 * Last time we heard from this peer
320 struct GNUNET_TIME_Absolute last_contact;
323 * Paths to reach the peer, ordered by ascending hop count
325 struct MeshPeerPath *path_head;
328 * Paths to reach the peer, ordered by ascending hop count
330 struct MeshPeerPath *path_tail;
333 * Handle to stop the DHT search for paths to this peer
335 struct GNUNET_DHT_GetHandle *dhtget;
338 * Tunnel to this peer, if any.
340 struct MeshTunnel2 *tunnel;
343 * Connections that go through this peer, indexed by tid;
345 struct GNUNET_CONTAINER_MultiHashMap *connections;
348 * Handle for queued transmissions
350 struct GNUNET_CORE_TransmitHandle *core_transmit;
353 * Transmission queue to core DLL head
355 struct MeshPeerQueue *queue_head;
358 * Transmission queue to core DLL tail
360 struct MeshPeerQueue *queue_tail;
363 * How many messages are in the queue to this peer.
365 unsigned int queue_n;
370 * Info needed to retry a message in case it gets lost.
372 struct MeshReliableMessage
375 * Double linked list, FIFO style
377 struct MeshReliableMessage *next;
378 struct MeshReliableMessage *prev;
381 * Type of message (payload, channel management).
386 * Tunnel Reliability queue this message is in.
388 struct MeshChannelReliability *rel;
391 * ID of the message (ACK needed to free)
396 * When was this message issued (to calculate ACK delay)
398 struct GNUNET_TIME_Absolute timestamp;
400 /* struct GNUNET_MESH_Data with payload */
404 struct MeshChannelReliability
407 * Channel this is about.
409 struct MeshChannel *ch;
412 * DLL of messages sent and not yet ACK'd.
414 struct MeshReliableMessage *head_sent;
415 struct MeshReliableMessage *tail_sent;
418 * Messages pending to send.
423 * DLL of messages received out of order.
425 struct MeshReliableMessage *head_recv;
426 struct MeshReliableMessage *tail_recv;
434 * Can we send data to the client?
439 * Task to resend/poll in case no ACK is received.
441 GNUNET_SCHEDULER_TaskIdentifier retry_task;
444 * Counter for exponential backoff.
446 struct GNUNET_TIME_Relative retry_timer;
449 * How long does it usually take to get an ACK.
451 struct GNUNET_TIME_Relative expected_delay;
456 * Struct containing all information regarding a channel to a remote client.
461 * Tunnel this channel is in.
463 struct MeshTunnel2 *t;
466 * Double linked list.
468 struct MeshChannel *next;
469 struct MeshChannel *prev;
472 * Destination port of the channel.
477 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
479 MESH_ChannelNumber gid;
482 * Local tunnel number for root (owner) client.
483 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
485 MESH_ChannelNumber lid_root;
488 * Local tunnel number for local destination clients (incoming number)
489 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
491 MESH_ChannelNumber lid_dest;
496 enum MeshChannelState state;
499 * Next MID to use for fwd traffic.
501 uint32_t mid_send_fwd;
504 * Next MID expected for fwd traffic.
506 uint32_t mid_recv_fwd;
509 * Next MID to use for bck traffic.
511 uint32_t mid_send_bck;
514 * Next MID expected for bck traffic.
516 uint32_t mid_recv_bck;
519 * Is the tunnel bufferless (minimum latency)?
524 * Is the tunnel reliable?
529 * Last time the channel was used
531 struct GNUNET_TIME_Absolute timestamp;
534 * Client owner of the tunnel, if any
536 struct MeshClient *root;
539 * Client destination of the tunnel, if any.
541 struct MeshClient *dest;
544 * Flag to signal the destruction of the channel.
545 * If this is set GNUNET_YES the channel will be destroyed
546 * when the queue is empty.
551 * Total messages pending for this channel, payload or not.
553 unsigned int pending_messages;
557 * Only present (non-NULL) at the owner of a tunnel.
559 struct MeshChannelReliability *fwd_rel;
563 * Only present (non-NULL) at the destination of a tunnel.
565 struct MeshChannelReliability *bck_rel;
571 * Struct containing all information regarding a connection to a peer.
573 struct MeshConnection
578 struct MeshConnection *next;
579 struct MeshConnection *prev;
582 * Tunnel this connection is part of.
584 struct MeshTunnel2 *t;
587 * Flow control information for traffic fwd.
589 struct MeshFlowControl fwd_fc;
592 * Flow control information for traffic bck.
594 struct MeshFlowControl bck_fc;
597 * ID of the connection.
599 struct GNUNET_HashCode id;
602 * State of the connection.
604 enum MeshConnectionState state;
607 * Path being used for the tunnel.
609 struct MeshPeerPath *path;
612 * Position of the local peer in the path.
614 unsigned int own_pos;
617 * Task to keep the used paths alive at the owner,
618 * time tunnel out on all the other peers.
620 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
623 * Task to keep the used paths alive at the destination,
624 * time tunnel out on all the other peers.
626 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
629 * Pending message count.
631 int pending_messages;
634 * Destroy flag: if true, destroy on last message.
641 * Struct used to queue messages in a tunnel.
643 struct MeshTunnelQueue
648 struct MeshTunnelQueue *next;
649 struct MeshTunnelQueue *prev;
654 struct MeshChannel *ch;
659 /* struct GNUNET_MessageHeader *msg; */
664 * Struct containing all information regarding a tunnel to a peer.
669 * Endpoint of the tunnel.
671 struct MeshPeer *peer;
674 * State of the tunnel.
676 enum MeshTunnelState state;
679 * Local peer ephemeral private key
681 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
684 * Local peer ephemeral public key
686 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
689 * Remote peer's public key.
691 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
694 * Encryption ("our") key.
696 struct GNUNET_CRYPTO_AesSessionKey e_key;
699 * Decryption ("their") key.
701 struct GNUNET_CRYPTO_AesSessionKey d_key;
704 * Paths that are actively used to reach the destination peer.
706 struct MeshConnection *connection_head;
707 struct MeshConnection *connection_tail;
710 * Next connection number.
715 * Channels inside this tunnel.
717 struct MeshChannel *channel_head;
718 struct MeshChannel *channel_tail;
721 * Channel ID for the next created channel.
723 MESH_ChannelNumber next_chid;
726 * Channel ID for the next incoming channel.
728 MESH_ChannelNumber next_local_chid;
731 * Pending message count.
733 int pending_messages;
736 * Destroy flag: if true, destroy on last message.
741 * Queued messages, to transmit once tunnel gets connected.
743 struct MeshTunnelQueue *tq_head;
744 struct MeshTunnelQueue *tq_tail;
750 * Struct containing information about a client of the service
752 * TODO: add a list of 'waiting' ports
759 struct MeshClient *next;
764 struct MeshClient *prev;
767 * Tunnels that belong to this client, indexed by local id
769 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
772 * Tunnels this client has accepted, indexed by incoming local id
774 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
777 * Handle to communicate with the client
779 struct GNUNET_SERVER_Client *handle;
782 * Ports that this client has declared interest in.
783 * Indexed by port, contains *Client.
785 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
788 * Whether the client is active or shutting down (don't send confirmations
789 * to a client that is shutting down.
794 * ID of the client, mainly for debug messages
800 /******************************************************************************/
801 /************************ DEBUG FUNCTIONS ****************************/
802 /******************************************************************************/
806 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
807 * @param cls string to print
808 * @param success GNUNET_OK if the PUT was transmitted,
809 * GNUNET_NO on timeout,
810 * GNUNET_SYSERR on disconnect from service
811 * after the PUT message was transmitted
812 * (so we don't know if it was received or not)
817 mesh_debug (void *cls, int success)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
827 /******************************************************************************/
828 /*********************** GLOBAL VARIABLES ****************************/
829 /******************************************************************************/
831 /************************** Configuration parameters **************************/
834 * How often to send path keepalives. Paths timeout after 4 missed.
836 static struct GNUNET_TIME_Relative refresh_connection_time;
839 * How often to PUT own ID in the DHT.
841 static struct GNUNET_TIME_Relative id_announce_time;
844 * Maximum time allowed to connect to a peer found by string.
846 static struct GNUNET_TIME_Relative connect_timeout;
849 * Default TTL for payload packets.
851 static unsigned long long default_ttl;
854 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
856 static unsigned long long dht_replication_level;
859 * How many connections are we willing to maintain.
860 * Local connections are always allowed, even if there are more connections than max.
862 static unsigned long long max_connections;
865 * How many messages *in total* are we willing to queue, divide by number of
866 * connections to get connection queue size.
868 static unsigned long long max_msgs_queue;
871 * How many peers do we want to remember?
873 static unsigned long long max_peers;
876 * Percentage of messages that will be dropped (for test purposes only).
878 static unsigned long long drop_percent;
880 /*************************** Static global variables **************************/
883 * DLL with all the clients, head.
885 static struct MeshClient *clients_head;
888 * DLL with all the clients, tail.
890 static struct MeshClient *clients_tail;
893 * Connections known, indexed by cid (MeshConnection).
895 static struct GNUNET_CONTAINER_MultiHashMap *connections;
898 * Peers known, indexed by PeerIdentity (MeshPeer).
900 static struct GNUNET_CONTAINER_MultiHashMap *peers;
903 * Handle to communicate with core.
905 static struct GNUNET_CORE_Handle *core_handle;
910 static struct GNUNET_DHT_Handle *dht_handle;
913 * Handle to server lib.
915 static struct GNUNET_SERVER_Handle *server_handle;
918 * Handle to the statistics service.
920 static struct GNUNET_STATISTICS_Handle *stats;
923 * Notification context, to send messages to local clients.
925 static struct GNUNET_SERVER_NotificationContext *nc;
928 * Local peer own ID (memory efficient handle).
930 static GNUNET_PEER_Id myid;
933 * Local peer own ID (full value).
935 static struct GNUNET_PeerIdentity my_full_id;
940 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
945 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
948 * All ports clients of this peer have opened.
950 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
953 * Task to periodically announce itself in the network.
955 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
958 * Next ID to assign to a client.
960 unsigned int next_client_id;
963 /******************************************************************************/
964 /*********************** DECLARATIONS **************************/
965 /******************************************************************************/
968 * Function to process paths received for a new peer addition. The recorded
969 * paths form the initial tunnel, which can be optimized later.
970 * Called on each result obtained for the DHT search.
973 * @param exp when will this value expire
974 * @param key key of the result
975 * @param type type of the result
976 * @param size number of bytes in data
977 * @param data pointer to the result data
980 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
981 const struct GNUNET_HashCode * key,
982 const struct GNUNET_PeerIdentity *get_path,
983 unsigned int get_path_length,
984 const struct GNUNET_PeerIdentity *put_path,
985 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
986 size_t size, const void *data);
990 * Retrieve the MeshPeer stucture associated with the peer, create one
991 * and insert it in the appropriate structures if the peer is not known yet.
993 * @param peer Full identity of the peer.
995 * @return Existing or newly created peer info.
997 static struct MeshPeer *
998 peer_get (const struct GNUNET_PeerIdentity *peer);
1002 * Retrieve the MeshPeer stucture associated with the peer, create one
1003 * and insert it in the appropriate structures if the peer is not known yet.
1005 * @param peer Short identity of the peer.
1007 * @return Existing or newly created peer info.
1009 static struct MeshPeer *
1010 peer_get_short (const GNUNET_PEER_Id peer);
1014 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1015 * to obtain a local peer -> destination path and interning the peer ids.
1017 * @return Newly allocated and created path
1019 static struct MeshPeerPath *
1020 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1021 unsigned int get_path_length,
1022 const struct GNUNET_PeerIdentity *put_path,
1023 unsigned int put_path_length);
1027 * Adds a path to the data structs of all the peers in the path
1029 * @param p Path to process.
1030 * @param confirmed Whether we know if the path works or not.
1033 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1037 * Search for a tunnel by global ID using full PeerIdentities.
1039 * @param t Tunnel containing the channel.
1040 * @param chid Public channel number.
1042 * @return channel handler, NULL if doesn't exist
1044 static struct MeshChannel *
1045 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1049 * Change the tunnel state.
1051 * @param t Tunnel whose state to change.
1052 * @param state New state.
1055 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1059 * Notify a tunnel that a connection has broken that affects at least
1060 * some of its peers.
1062 * @param t Tunnel affected.
1063 * @param p1 Peer that got disconnected from p2.
1064 * @param p2 Peer that got disconnected from p1.
1066 * @return Short ID of the peer disconnected (either p1 or p2).
1067 * 0 if the tunnel remained unaffected.
1069 static GNUNET_PEER_Id
1070 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1071 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1074 * @brief Use the given path for the tunnel.
1075 * Update the next and prev hops (and RCs).
1076 * (Re)start the path refresh in case the tunnel is locally owned.
1078 * @param t Tunnel to update.
1079 * @param p Path to use.
1081 * @return Connection created.
1083 static struct MeshConnection *
1084 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1087 * Tunnel is empty: destroy it.
1089 * Notifies all participants (peers, cleints) about the destruction.
1091 * @param t Tunnel to destroy.
1094 tunnel_destroy_empty (struct MeshTunnel2 *t);
1097 * Destroy the tunnel.
1099 * This function does not generate any warning traffic to clients or peers.
1102 * Cancel messages belonging to this tunnel queued to neighbors.
1103 * Free any allocated resources linked to the tunnel.
1105 * @param t The tunnel to destroy.
1108 tunnel_destroy (struct MeshTunnel2 *t);
1111 * Create a connection.
1113 * @param cid Connection ID.
1115 static struct MeshConnection *
1116 connection_new (const struct GNUNET_HashCode *cid);
1119 * Connection is no longer needed: destroy it and remove from tunnel.
1121 * @param c Connection to destroy.
1124 connection_destroy (struct MeshConnection *c);
1127 * Send FWD keepalive packets for a connection.
1129 * @param cls Closure (connection for which to send the keepalive).
1130 * @param tc Notification context.
1133 connection_fwd_keepalive (void *cls,
1134 const struct GNUNET_SCHEDULER_TaskContext *tc);
1137 * Send BCK keepalive packets for a connection.
1139 * @param cls Closure (connection for which to send the keepalive).
1140 * @param tc Notification context.
1143 connection_bck_keepalive (void *cls,
1144 const struct GNUNET_SCHEDULER_TaskContext *tc);
1148 * Change the tunnel state.
1150 * @param c Connection whose state to change.
1151 * @param state New state.
1154 connection_change_state (struct MeshConnection* c,
1155 enum MeshConnectionState state);
1160 * @brief Queue and pass message to core when possible.
1162 * @param cls Closure (@c type dependant). It will be used by queue_send to
1163 * build the message to be sent if not already prebuilt.
1164 * @param type Type of the message, 0 for a raw message.
1165 * @param size Size of the message.
1166 * @param c Connection this message belongs to (cannot be NULL).
1167 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1168 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1171 queue_add (void* cls,
1174 struct MeshConnection* c,
1175 struct MeshChannel* ch,
1180 * Free a transmission that was already queued with all resources
1181 * associated to the request.
1183 * @param queue Queue handler to cancel.
1184 * @param clear_cls Is it necessary to free associated cls?
1187 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1191 * Core callback to write a queued packet to core buffer
1193 * @param cls Closure (peer info).
1194 * @param size Number of bytes available in buf.
1195 * @param buf Where the to write the message.
1197 * @return number of bytes written to buf
1200 queue_send (void *cls, size_t size, void *buf);
1204 * Dummy function to separate declarations from definitions in function list.
1207 __mesh_divider______________________________________________________________();
1211 * Get string description for tunnel state.
1213 * @param s Tunnel state.
1215 * @return String representation.
1218 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1220 static char buf[128];
1224 case MESH_TUNNEL_NEW:
1225 return "MESH_TUNNEL_NEW";
1226 case MESH_TUNNEL_SEARCHING:
1227 return "MESH_TUNNEL_SEARCHING";
1228 case MESH_TUNNEL_WAITING:
1229 return "MESH_TUNNEL_WAITING";
1230 case MESH_TUNNEL_READY:
1231 return "MESH_TUNNEL_READY";
1232 case MESH_TUNNEL_RECONNECTING:
1233 return "MESH_TUNNEL_RECONNECTING";
1236 sprintf (buf, "%u (UNKNOWN STATE)", s);
1243 * Get string description for tunnel state.
1245 * @param s Tunnel state.
1247 * @return String representation.
1250 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1254 case MESH_CONNECTION_NEW:
1255 return "MESH_CONNECTION_NEW";
1256 case MESH_CONNECTION_SENT:
1257 return "MESH_CONNECTION_SENT";
1258 case MESH_CONNECTION_READY:
1259 return "MESH_CONNECTION_READY";
1261 return "MESH_CONNECTION_STATE_ERROR";
1267 /******************************************************************************/
1268 /************************ PERIODIC FUNCTIONS ****************************/
1269 /******************************************************************************/
1272 * Periodically announce self id in the DHT
1274 * @param cls closure
1275 * @param tc task context
1278 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1280 struct PBlock block;
1282 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1284 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1288 * - Set data expiration in function of X
1289 * - Adapt X to churn
1291 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1293 block.id = my_full_id;
1294 GNUNET_DHT_put (dht_handle, /* DHT handle */
1295 &my_full_id.hashPubKey, /* Key to use */
1296 dht_replication_level, /* Replication level */
1297 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1298 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1299 sizeof (block), /* Size of the data */
1300 (const char *) &block, /* Data itself */
1301 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1302 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1303 NULL, /* Continuation */
1304 NULL); /* Continuation closure */
1306 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1310 /******************************************************************************/
1311 /****************** GENERAL HELPER FUNCTIONS ************************/
1312 /******************************************************************************/
1316 * Get the static string for a peer ID.
1320 * @return Static string for it's ID.
1323 peer2s (const struct MeshPeer *peer)
1327 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1332 * Get the previous hop in a connection
1334 * @param c Connection.
1336 * @return Previous peer in the connection.
1338 static struct MeshPeer *
1339 connection_get_prev_hop (struct MeshConnection *c)
1343 if (0 == c->own_pos || c->path->length < 2)
1344 id = c->path->peers[0];
1346 id = c->path->peers[c->own_pos - 1];
1348 return peer_get_short (id);
1353 * Get the next hop in a connection
1355 * @param c Connection.
1357 * @return Next peer in the connection.
1359 static struct MeshPeer *
1360 connection_get_next_hop (struct MeshConnection *c)
1364 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1365 id = c->path->peers[c->path->length - 1];
1367 id = c->path->peers[c->own_pos + 1];
1369 return peer_get_short (id);
1374 * Get the hop in a connection.
1376 * @param c Connection.
1377 * @param fwd Next hop?
1379 * @return Next peer in the connection.
1381 static struct MeshPeer *
1382 connection_get_hop (struct MeshConnection *c, int fwd)
1385 return connection_get_next_hop (c);
1386 return connection_get_prev_hop (c);
1390 * Check if client has registered with the service and has not disconnected
1392 * @param client the client to check
1394 * @return non-NULL if client exists in the global DLL
1396 static struct MeshClient *
1397 client_get (struct GNUNET_SERVER_Client *client)
1399 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1404 * Deletes a tunnel from a client (either owner or destination).
1406 * @param c Client whose tunnel to delete.
1407 * @param ch Channel which should be deleted.
1410 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1416 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1418 if (GNUNET_YES != res)
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1423 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1425 if (GNUNET_YES != res)
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1432 * Notify the appropriate client that a new incoming channel was created.
1434 * @param ch Channel that was created.
1437 send_local_channel_create (struct MeshChannel *ch)
1439 struct GNUNET_MESH_ChannelMessage msg;
1440 struct MeshTunnel2 *t = ch->t;
1442 if (NULL == ch->dest)
1444 msg.header.size = htons (sizeof (msg));
1445 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1446 msg.channel_id = htonl (ch->lid_dest);
1447 msg.port = htonl (ch->port);
1449 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1450 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1451 msg.opt = htonl (msg.opt);
1452 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1453 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1454 &msg.header, GNUNET_NO);
1459 * Notify a client that the incoming tunnel is no longer valid.
1461 * @param ch Channel that is destroyed.
1462 * @param fwd Forward notification (owner->dest)?
1465 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1467 struct GNUNET_MESH_ChannelMessage msg;
1468 struct MeshClient *c;
1470 c = fwd ? ch->dest : ch->root;
1476 msg.header.size = htons (sizeof (msg));
1477 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1478 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1479 msg.port = htonl (0);
1480 memset (&msg.peer, 0, sizeof (msg.peer));
1481 msg.opt = htonl (0);
1482 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1483 &msg.header, GNUNET_NO);
1488 * Build a local ACK message and send it to a local client.
1490 * @param ch Channel on which to send the ACK.
1491 * @param c Client to whom send the ACK.
1492 * @param fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1495 send_local_ack (struct MeshChannel *ch,
1498 struct GNUNET_MESH_LocalAck msg;
1499 struct MeshChannelReliability *rel;
1500 struct MeshClient *c;
1502 c = fwd ? ch->root : ch->dest;
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "send local %s ack on %s:%X towards %p\n",
1505 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1508 || ( fwd && (0 == ch->lid_root || c != ch->root))
1509 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1514 msg.header.size = htons (sizeof (msg));
1515 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1516 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1517 GNUNET_SERVER_notification_context_unicast (nc,
1521 rel = fwd ? ch->fwd_rel : ch->bck_rel;
1522 rel->client_ready = GNUNET_YES;
1527 * Count established (ready) connections of a tunnel.
1529 * @param t Tunnel on which to send the message.
1531 * @return Number of connections.
1534 tunnel_count_connections (struct MeshTunnel2 *t)
1536 struct MeshConnection *c;
1539 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1546 * Pick a connection on which send the next data message.
1548 * @param t Tunnel on which to send the message.
1549 * @param fwd Is this a fwd message?
1551 * @return The connection on which to send the next message.
1553 static struct MeshConnection *
1554 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1556 struct MeshConnection *c;
1557 struct MeshConnection *best;
1558 struct MeshFlowControl *fc;
1559 unsigned int lowest_q;
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
1564 lowest_q = UINT_MAX;
1565 for (c = t->connection_head; NULL != c; c = c->next)
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s, \n",
1568 GNUNET_h2s (&c->id));
1569 if (MESH_CONNECTION_READY == c->state)
1571 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
1578 if (fc->queue_n < lowest_q)
1581 lowest_q = fc->queue_n;
1590 * Get the total buffer space for a tunnel.
1593 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1595 struct MeshConnection *c;
1596 struct MeshFlowControl *fc;
1597 unsigned int buffer;
1599 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1601 if (c->state != MESH_CONNECTION_READY)
1604 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1605 buffer += fc->last_ack_recv - fc->last_pid_sent;
1613 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1614 * Encrypt data with the tunnel key.
1616 * @param t Tunnel whose key to use.
1617 * @param dst Destination for the encrypted data.
1618 * @param src Source of the plaintext.
1619 * @param size Size of the plaintext.
1620 * @param iv Initialization Vector to use.
1621 * @param fwd Is this a fwd message?
1624 tunnel_encrypt (struct MeshTunnel2 *t,
1625 void *dst, const void *src,
1626 size_t size, uint64_t iv, int fwd)
1628 memcpy (dst, src, size);
1633 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1634 * Decrypt data with the tunnel key.
1636 * @param t Tunnel whose key to use.
1637 * @param dst Destination for the plaintext.
1638 * @param src Source of the encrypted data.
1639 * @param size Size of the encrypted data.
1640 * @param iv Initialization Vector to use.
1641 * @param fwd Is this a fwd message?
1644 tunnel_decrypt (struct MeshTunnel2 *t,
1645 void *dst, const void *src,
1646 size_t size, uint64_t iv, int fwd)
1648 memcpy (dst, src, size);
1653 * Sends an already built message on a connection, properly registering
1654 * all used resources.
1656 * @param message Message to send. Function makes a copy of it.
1657 * If message is not hop-by-hop, decrements TTL of copy.
1658 * @param c Connection on which this message is transmitted.
1659 * @param ch Channel on which this message is transmitted, or NULL.
1660 * @param fwd Is this a fwd message?
1663 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1664 struct MeshConnection *c,
1665 struct MeshChannel *ch,
1672 size = ntohs (message->size);
1673 data = GNUNET_malloc (size);
1674 memcpy (data, message, size);
1675 type = ntohs (message->type);
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s[%u] on connection %s\n",
1677 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1681 struct GNUNET_MESH_Encrypted *emsg;
1682 struct GNUNET_MESH_ACK *amsg;
1683 struct GNUNET_MESH_Poll *pmsg;
1686 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1687 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1688 emsg = (struct GNUNET_MESH_Encrypted *) data;
1689 ttl = ntohl (emsg->ttl);
1692 GNUNET_break_op (0);
1696 emsg->ttl = htonl (ttl - 1);
1697 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1701 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1702 amsg = (struct GNUNET_MESH_ACK *) data;
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1707 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1708 pmsg = (struct GNUNET_MESH_Poll *) data;
1710 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1728 * Sends an already built message on a tunnel, choosing the best connection.
1730 * @param message Message to send. Function modifies it.
1731 * @param t Tunnel on which this message is transmitted.
1732 * @param ch Channel on which this message is transmitted.
1733 * @param fwd Is this a fwd message?
1736 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1737 struct MeshTunnel2 *t,
1738 struct MeshChannel *ch,
1741 struct MeshConnection *c;
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1746 c = tunnel_get_connection (t, fwd);
1749 GNUNET_break (GNUNET_YES == t->destroy);
1752 type = ntohs (msg->header.type);
1755 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1756 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1757 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1758 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1760 msg->ttl = htonl (default_ttl);
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1764 GNUNET_MESH_DEBUG_M2S (type));
1769 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1774 * Sends an already built message on a channel, properly registering
1775 * all used resources and encrypting the message with the tunnel's key.
1777 * @param message Message to send. Function makes a copy of it.
1778 * @param ch Channel on which this message is transmitted.
1779 * @param fwd Is this a fwd message?
1782 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1783 struct MeshChannel *ch,
1786 struct GNUNET_MESH_Encrypted *msg;
1787 size_t size = ntohs (message->size);
1788 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1793 peer2s (ch->t->peer), ch->gid);
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1795 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1796 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1797 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1799 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1800 msg->header.type = htons (type);
1801 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1802 msg->iv = GNUNET_htonll (iv);
1803 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1805 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1810 * Sends a CREATE CONNECTION message for a path to a peer.
1811 * Changes the connection and tunnel states if necessary.
1813 * @param connection Connection to create.
1816 send_connection_create (struct MeshConnection *connection)
1818 struct MeshTunnel2 *t;
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1823 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1824 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1825 (connection->path->length *
1826 sizeof (struct GNUNET_PeerIdentity)),
1831 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1832 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1833 if (MESH_CONNECTION_NEW == connection->state)
1834 connection_change_state (connection, MESH_CONNECTION_SENT);
1839 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1842 * @param connection Connection to confirm.
1843 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1846 send_connection_ack (struct MeshConnection *connection, int fwd)
1848 struct MeshTunnel2 *t;
1851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1853 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1854 sizeof (struct GNUNET_MESH_ConnectionACK),
1858 if (MESH_TUNNEL_NEW == t->state)
1859 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1864 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1866 * @param c Which connection to send the hop-by-hop ACK.
1867 * @param ack Value of the ACK.
1868 * @param fwd Is this fwd?
1871 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1873 struct GNUNET_MESH_ACK msg;
1875 msg.header.size = htons (sizeof (msg));
1876 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1877 msg.ack = htonl (ack);
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "send %s ack %u on %s\n",
1882 fwd ? "FWD" : "BCK", ack, GNUNET_h2s (&c->id));
1884 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1889 * Core callback to write a pre-constructed data packet to core buffer
1891 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1892 * @param size Number of bytes available in buf.
1893 * @param buf Where the to write the message.
1895 * @return number of bytes written to buf
1898 send_core_data_raw (void *cls, size_t size, void *buf)
1900 struct GNUNET_MessageHeader *msg = cls;
1903 GNUNET_assert (NULL != msg);
1904 total_size = ntohs (msg->size);
1906 if (total_size > size)
1911 memcpy (buf, msg, total_size);
1918 * Function to send a create connection message to a peer.
1920 * @param c Connection to create.
1921 * @param size number of bytes available in buf
1922 * @param buf where the callee should write the message
1923 * @return number of bytes written to buf
1926 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1928 struct GNUNET_MESH_ConnectionCreate *msg;
1929 struct GNUNET_PeerIdentity *peer_ptr;
1930 struct MeshPeerPath *p = c->path;
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1936 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1937 p->length * sizeof (struct GNUNET_PeerIdentity);
1939 if (size < size_needed || NULL == buf)
1944 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1945 msg->header.size = htons (size_needed);
1946 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1949 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1950 for (i = 0; i < p->length; i++)
1952 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1962 * Creates a path ack message in buf and frees all unused resources.
1964 * @param c Connection to send an ACK on.
1965 * @param size number of bytes available in buf
1966 * @param buf where the callee should write the message
1968 * @return number of bytes written to buf
1971 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1973 struct GNUNET_MESH_ConnectionACK *msg = buf;
1974 struct MeshTunnel2 *t = c->t;
1976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1977 GNUNET_assert (NULL != t);
1978 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1983 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1984 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1987 /* TODO add signature */
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1990 return sizeof (struct GNUNET_MESH_ConnectionACK);
1995 * Destroy the peer_info and free any allocated resources linked to it
1997 * @param peer The peer_info to destroy.
1999 * @return GNUNET_OK on success
2002 peer_destroy (struct MeshPeer *peer)
2004 struct GNUNET_PeerIdentity id;
2005 struct MeshPeerPath *p;
2006 struct MeshPeerPath *nextp;
2008 GNUNET_PEER_resolve (peer->id, &id);
2009 GNUNET_PEER_change_rc (peer->id, -1);
2012 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2015 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2016 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2018 if (NULL != peer->dhtget)
2020 GNUNET_DHT_get_stop (peer->dhtget);
2022 p = peer->path_head;
2026 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2030 tunnel_destroy_empty (peer->tunnel);
2037 * Returns if peer is used (has a tunnel, is neighbor).
2039 * @peer Peer to check.
2041 * @return GNUNET_YES if peer is in use.
2044 peer_is_used (struct MeshPeer *peer)
2046 struct MeshPeerPath *p;
2048 if (NULL != peer->tunnel)
2051 for (p = peer->path_head; NULL != p; p = p->next)
2060 * Iterator over all the peers to get the oldest timestamp.
2062 * @param cls Closure (unsued).
2063 * @param key ID of the peer.
2064 * @param value Peer_Info of the peer.
2067 peer_get_oldest (void *cls,
2068 const struct GNUNET_HashCode *key,
2071 struct MeshPeer *p = value;
2072 struct GNUNET_TIME_Absolute *abs = cls;
2074 /* Don't count active peers */
2075 if (GNUNET_YES == peer_is_used (p))
2078 if (abs->abs_value_us < p->last_contact.abs_value_us)
2079 abs->abs_value_us = p->last_contact.abs_value_us;
2086 * Iterator over all the peers to remove the oldest entry.
2088 * @param cls Closure (unsued).
2089 * @param key ID of the peer.
2090 * @param value Peer_Info of the peer.
2093 peer_timeout (void *cls,
2094 const struct GNUNET_HashCode *key,
2097 struct MeshPeer *p = value;
2098 struct GNUNET_TIME_Absolute *abs = cls;
2100 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2101 GNUNET_NO == peer_is_used (p))
2111 * Delete oldest unused peer.
2114 peer_delete_oldest (void)
2116 struct GNUNET_TIME_Absolute abs;
2118 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2120 GNUNET_CONTAINER_multihashmap_iterate (peers,
2123 GNUNET_CONTAINER_multihashmap_iterate (peers,
2130 * Retrieve the MeshPeer stucture associated with the peer, create one
2131 * and insert it in the appropriate structures if the peer is not known yet.
2133 * @param peer Full identity of the peer.
2135 * @return Existing or newly created peer info.
2137 static struct MeshPeer *
2138 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2140 struct MeshPeer *peer;
2142 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2145 peer = GNUNET_new (struct MeshPeer);
2146 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2148 peer_delete_oldest ();
2150 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2152 peer->id = GNUNET_PEER_intern (peer_id);
2154 peer->last_contact = GNUNET_TIME_absolute_get();
2161 * Retrieve the MeshPeer stucture associated with the peer, create one
2162 * and insert it in the appropriate structures if the peer is not known yet.
2164 * @param peer Short identity of the peer.
2166 * @return Existing or newly created peer info.
2168 static struct MeshPeer *
2169 peer_get_short (const GNUNET_PEER_Id peer)
2171 return peer_get (GNUNET_PEER_resolve2 (peer));
2176 * Get a cost of a path for a peer considering existing tunnel connections.
2178 * @param peer Peer towards which the path is considered.
2179 * @param path Candidate path.
2181 * @return Cost of the path (path length + number of overlapping nodes)
2184 peer_get_path_cost (const struct MeshPeer *peer,
2185 const struct MeshPeerPath *path)
2187 struct MeshConnection *c;
2188 unsigned int overlap;
2196 GNUNET_assert (NULL != peer->tunnel);
2198 for (i = 0; i < path->length; i++)
2200 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2202 for (j = 0; j < c->path->length; j++)
2204 if (path->peers[i] == c->path->peers[j])
2212 return (path->length + overlap) * (path->score * -1);
2217 * Choose the best path towards a peer considering the tunnel properties.
2219 * @param peer The destination peer.
2221 * @return Best current known path towards the peer, if any.
2223 static struct MeshPeerPath *
2224 peer_get_best_path (const struct MeshPeer *peer)
2226 struct MeshPeerPath *best_p;
2227 struct MeshPeerPath *p;
2228 struct MeshConnection *c;
2229 unsigned int best_cost;
2232 best_cost = UINT_MAX;
2234 for (p = peer->path_head; NULL != p; p = p->next)
2236 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2240 continue; /* If path is in use in a connection, skip it. */
2242 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2252 queue_is_sendable (struct MeshPeerQueue *q)
2254 struct MeshFlowControl *fc;
2256 /* Is PID-independent? */
2259 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2260 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2264 /* Is PID allowed? */
2265 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2266 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2274 * Get first sendable message.
2276 * @param peer The destination peer.
2278 * @return Best current known path towards the peer, if any.
2280 static struct MeshPeerQueue *
2281 peer_get_first_message (const struct MeshPeer *peer)
2283 struct MeshPeerQueue *q;
2285 for (q = peer->queue_head; NULL != q; q = q->next)
2287 if (queue_is_sendable (q))
2296 * Try to establish a new connection to this peer in the given tunnel.
2297 * If the peer doesn't have any path to it yet, try to get one.
2298 * If the peer already has some path, send a CREATE CONNECTION towards it.
2300 * @param peer PeerInfo of the peer.
2303 peer_connect (struct MeshPeer *peer)
2305 struct MeshTunnel2 *t;
2306 struct MeshPeerPath *p;
2307 struct MeshConnection *c;
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 "peer_connect towards %s\n",
2313 if (NULL != peer->path_head)
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2316 p = peer_get_best_path (peer);
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2320 c = tunnel_use_path (t, p);
2321 send_connection_create (c);
2324 else if (NULL == peer->dhtget)
2326 const struct GNUNET_PeerIdentity *id;
2328 id = GNUNET_PEER_resolve2 (peer->id);
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2330 " Starting DHT GET for peer %s\n", peer2s (peer));
2331 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2332 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2333 &id->hashPubKey, /* key to search */
2334 dht_replication_level, /* replication level */
2335 GNUNET_DHT_RO_RECORD_ROUTE |
2336 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2338 0, /* xquery bits */
2339 &dht_get_id_handler, peer);
2340 if (MESH_TUNNEL_NEW == t->state)
2341 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "There is no path but the DHT GET is already started.\n");
2352 * Get the first transmittable message for a connection.
2354 * @param c Connection.
2355 * @param fwd Is this FWD?
2357 * @return First transmittable message.
2359 static struct MeshPeerQueue *
2360 connection_get_first_message (struct MeshConnection *c, int fwd)
2362 struct MeshPeerQueue *q;
2365 p = connection_get_hop (c, fwd);
2367 for (q = p->queue_head; NULL != q; q = q->next)
2371 if (queue_is_sendable (q))
2380 * Is this peer the first one on the connection?
2382 * @param c Connection.
2383 * @param fwd Is this about fwd traffic?
2385 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2388 connection_is_origin (struct MeshConnection *c, int fwd)
2390 if (!fwd && c->own_pos == c->path->length - 1)
2392 if (fwd && c->own_pos == 0)
2399 * Is this peer the last one on the connection?
2401 * @param c Connection.
2402 * @param fwd Is this about fwd traffic?
2403 * Note that the ROOT is the terminal for BCK traffic!
2405 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2408 connection_is_terminal (struct MeshConnection *c, int fwd)
2410 if (fwd && c->own_pos == c->path->length - 1)
2412 if (!fwd && c->own_pos == 0)
2419 * @brief Re-initiate traffic on this connection if necessary.
2421 * Check if there is traffic queued towards this peer
2422 * and the core transmit handle is NULL (traffic was stalled).
2423 * If so, call core tmt rdy.
2425 * @param c Connection on which initiate traffic.
2426 * @param fwd Is this about fwd traffic?
2429 connection_unlock_queue (struct MeshConnection *c, int fwd)
2431 struct MeshPeer *peer;
2432 struct MeshPeerQueue *q;
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "connection_unlock_queue %s on %s\n",
2437 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2439 if (connection_is_origin (c, fwd))
2441 struct MeshTunnel2 *t = c->t;
2442 struct MeshChannel *ch;
2443 struct MeshChannelReliability *rel;
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2446 /* FIXME randomize channel selection, not always first channel */
2447 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2449 rel = fwd ? ch->fwd_rel : ch->bck_rel;
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2452 ch->gid, rel->client_ready ? "ready " : "not ready");
2453 if (GNUNET_NO == rel->client_ready)
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not ready!\n");
2456 send_local_ack (ch, fwd);
2457 return; /* FIXME authorize all channels? */
2463 peer = connection_get_hop (c, fwd);
2465 if (NULL != peer->core_transmit)
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2468 return; /* Already unlocked */
2471 q = connection_get_first_message (c, fwd);
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2475 return; /* Nothing to transmit */
2479 peer->core_transmit =
2480 GNUNET_CORE_notify_transmit_ready (core_handle,
2483 GNUNET_TIME_UNIT_FOREVER_REL,
2484 GNUNET_PEER_resolve2 (peer->id),
2492 * Cancel all transmissions that belong to a certain connection.
2494 * @param c Connection which to cancel.
2495 * @param fwd Cancel fwd traffic?
2498 connection_cancel_queues (struct MeshConnection *c, int fwd)
2500 struct MeshPeerQueue *q;
2501 struct MeshPeerQueue *next;
2502 struct MeshFlowControl *fc;
2503 struct MeshPeer *peer;
2510 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2511 peer = connection_get_hop (c, fwd);
2513 for (q = peer->queue_head; NULL != q; q = next)
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "connection_cancel_queue %s\n",
2520 GNUNET_MESH_DEBUG_M2S (q->type));
2521 queue_destroy (q, GNUNET_YES);
2524 if (NULL == peer->queue_head)
2526 if (NULL != peer->core_transmit)
2528 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2529 peer->core_transmit = NULL;
2531 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2533 GNUNET_SCHEDULER_cancel (fc->poll_task);
2534 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2541 * Remove all paths that rely on a direct connection between p1 and p2
2542 * from the peer itself and notify all tunnels about it.
2544 * @param peer PeerInfo of affected peer.
2545 * @param p1 GNUNET_PEER_Id of one peer.
2546 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2549 * TODO: optimize (see below)
2552 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2555 struct MeshPeerPath *p;
2556 struct MeshPeerPath *next;
2557 struct MeshPeer *peer_d;
2559 unsigned int destroyed;
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2564 for (p = peer->path_head; NULL != p; p = next)
2567 for (i = 0; i < (p->length - 1); i++)
2569 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2570 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2572 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2583 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2585 peer_d = peer_get_short (d); // FIXME
2586 next = peer_get_best_path (peer_d);
2587 tunnel_use_path (peer->tunnel, next);
2588 peer_connect (peer_d);
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2595 * Add the path to the peer and update the path used to reach it in case this
2598 * @param peer_info Destination peer to add the path to.
2599 * @param path New path to add. Last peer must be the peer in arg 1.
2600 * Path will be either used of freed if already known.
2601 * @param trusted Do we trust that this path is real?
2604 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2607 struct MeshPeerPath *aux;
2611 if ((NULL == peer_info) || (NULL == path))
2614 path_destroy (path);
2617 if (path->peers[path->length - 1] != peer_info->id)
2620 path_destroy (path);
2623 if (2 >= path->length && GNUNET_NO == trusted)
2625 /* Only allow CORE to tell us about direct paths */
2626 path_destroy (path);
2629 for (l = 1; l < path->length; l++)
2631 if (path->peers[l] == myid)
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2634 for (l2 = 0; l2 < path->length - l; l2++)
2636 path->peers[l2] = path->peers[l + l2];
2641 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2646 path->length, peer2s (peer_info));
2648 l = path_get_length (path);
2651 path_destroy (path);
2655 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2656 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2658 l2 = path_get_length (aux);
2661 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2662 peer_info->path_tail, aux, path);
2667 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2669 path_destroy (path);
2674 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2681 * Add the path to the origin peer and update the path used to reach it in case
2682 * this is the shortest.
2683 * The path is given in peer_info -> destination, therefore we turn the path
2684 * upside down first.
2686 * @param peer_info Peer to add the path to, being the origin of the path.
2687 * @param path New path to add after being inversed.
2688 * Path will be either used or freed.
2689 * @param trusted Do we trust that this path is real?
2692 peer_add_path_to_origin (struct MeshPeer *peer_info,
2693 struct MeshPeerPath *path, int trusted)
2696 peer_add_path (peer_info, path, trusted);
2702 * Function called if a connection has been stalled for a while,
2703 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2705 * @param cls Closure (poll ctx).
2706 * @param tc TaskContext.
2709 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2711 struct MeshFlowControl *fc = cls;
2712 struct GNUNET_MESH_Poll msg;
2713 struct MeshConnection *c;
2715 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2716 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2724 peer2s (c->t->peer), c->id);
2726 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2727 msg.header.size = htons (sizeof (msg));
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2729 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2730 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2731 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2732 &connection_poll, fc);
2737 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2738 * to obtain a local peer -> destination path and interning the peer ids.
2740 * @return Newly allocated and created path
2742 static struct MeshPeerPath *
2743 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2744 unsigned int get_path_length,
2745 const struct GNUNET_PeerIdentity *put_path,
2746 unsigned int put_path_length)
2748 struct MeshPeerPath *p;
2754 GNUNET_PEER_change_rc (myid, 1);
2755 i = get_path_length;
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2757 for (i--; i >= 0; i--)
2759 id = GNUNET_PEER_intern (&get_path[i]);
2760 if (p->length > 0 && id == p->peers[p->length - 1])
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2763 GNUNET_PEER_change_rc (id, -1);
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2768 GNUNET_i2s (&get_path[i]));
2770 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2771 p->peers[p->length - 1] = id;
2774 i = put_path_length;
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2776 for (i--; i >= 0; i--)
2778 id = GNUNET_PEER_intern (&put_path[i]);
2781 /* PUT path went through us, so discard the path up until now and start
2782 * from here to get a much shorter (and loop-free) path.
2787 if (p->length > 0 && id == p->peers[p->length - 1])
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2790 GNUNET_PEER_change_rc (id, -1);
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2795 GNUNET_i2s (&put_path[i]));
2797 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2798 p->peers[p->length - 1] = id;
2802 if (get_path_length > 0)
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2804 GNUNET_i2s (&get_path[0]));
2805 if (put_path_length > 0)
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2807 GNUNET_i2s (&put_path[0]));
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2810 for (i = 0; i < p->length; i++)
2812 struct GNUNET_PeerIdentity peer_id;
2814 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2816 GNUNET_i2s (&peer_id));
2824 * Adds a path to the peer_infos of all the peers in the path
2826 * @param p Path to process.
2827 * @param confirmed Whether we know if the path works or not.
2830 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2834 /* TODO: invert and add */
2835 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2836 for (i++; i < p->length; i++)
2838 struct MeshPeer *aux;
2839 struct MeshPeerPath *copy;
2841 aux = peer_get_short (p->peers[i]);
2842 copy = path_duplicate (p);
2843 copy->length = i + 1;
2844 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2850 * Search for a channel among the channels for a client
2852 * @param c the client whose channels to search in
2853 * @param chid the local id of the channel
2855 * @return channel handler, NULL if doesn't exist
2857 static struct MeshChannel *
2858 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- get CHID %X\n", chid);
2861 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2863 GNUNET_break_op (0);
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2867 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2868 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2869 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2873 * Search for a tunnel by global ID using full PeerIdentities.
2875 * @param t Tunnel containing the channel.
2876 * @param chid Public channel number.
2878 * @return channel handler, NULL if doesn't exist
2880 static struct MeshChannel *
2881 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2883 struct MeshChannel *ch;
2888 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2890 if (ch->gid == chid)
2899 * Change the tunnel state.
2901 * @param t Tunnel whose state to change.
2902 * @param state New state.
2905 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2910 "Tunnel %s state was %s\n",
2912 GNUNET_MESH_DEBUG_TS2S (t->state));
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "Tunnel %s state is now %s\n",
2916 GNUNET_MESH_DEBUG_TS2S (state));
2922 * Cache a message to be sent once tunnel is online.
2924 * @param t Tunnel to hold the message.
2925 * @param ch Channel the message is about.
2926 * @param msg Message itself (copy will be made).
2927 * @param fwd Is this fwd?
2930 tunnel_queue_data (struct MeshTunnel2 *t,
2931 struct MeshChannel *ch,
2932 struct GNUNET_MessageHeader *msg,
2935 struct MeshTunnelQueue *tq;
2936 uint16_t size = ntohs (msg->size);
2938 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2941 memcpy (&tq[1], msg, size);
2942 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2947 * Send all cached messages that we can, tunnel is online.
2949 * @param t Tunnel that holds the messages.
2950 * @param fwd Is this fwd?
2953 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2955 struct MeshTunnelQueue *tq;
2956 struct MeshTunnelQueue *next;
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2960 "tunnel_send_queued_data on tunnel %s\n",
2962 room = tunnel_get_buffer (t, fwd);
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2964 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2968 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2969 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2978 connection_change_state (struct MeshConnection* c,
2979 enum MeshConnectionState state)
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2982 "Connection %s state was %s\n",
2983 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2985 "Connection %s state is now %s\n",
2986 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
2992 * Add a client to a channel, initializing all needed data structures.
2994 * @param ch Channel to which add the client.
2995 * @param c Client which to add to the channel.
2998 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3000 struct MeshTunnel2 *t = ch->t;
3002 if (NULL != ch->dest)
3008 /* Assign local id as destination */
3009 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3010 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3011 ch->lid_dest = t->next_local_chid++;
3012 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3014 /* Store in client's hashmap */
3016 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3018 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3024 GNUNET_break (NULL == ch->bck_rel);
3025 ch->bck_rel = GNUNET_new (struct MeshChannelReliability);
3026 ch->bck_rel->ch = ch;
3027 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3033 static struct MeshConnection *
3034 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3036 struct MeshConnection *c;
3037 struct GNUNET_HashCode cid;
3038 struct MeshPeer *peer;
3039 unsigned int own_pos;
3041 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3043 c = connection_new (&cid);
3045 for (own_pos = 0; own_pos < p->length; own_pos++)
3047 if (p->peers[own_pos] == myid)
3050 if (own_pos > p->length - 1)
3053 connection_destroy (c);
3056 c->own_pos = own_pos;
3061 c->fwd_maintenance_task =
3062 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3063 &connection_fwd_keepalive, c);
3066 peer = connection_get_next_hop (c);
3067 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3069 peer = connection_get_prev_hop (c);
3070 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3071 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3077 * Notifies a tunnel that a connection has broken that affects at least
3078 * some of its peers. Sends a notification towards the root of the tree.
3079 * In case the peer is the owner of the tree, notifies the client that owns
3080 * the tunnel and tries to reconnect.
3082 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3084 * @param t Tunnel affected.
3085 * @param p1 Peer that got disconnected from p2.
3086 * @param p2 Peer that got disconnected from p1.
3088 * @return Short ID of the peer disconnected (either p1 or p2).
3089 * 0 if the tunnel remained unaffected.
3091 static GNUNET_PEER_Id
3092 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3093 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3095 // if (myid != p1 && myid != p2) FIXME
3100 // if (tree_get_predecessor (t->tree) != 0)
3102 // /* We are the peer still connected, notify owner of the disconnection. */
3103 // struct GNUNET_MESH_PathBroken msg;
3104 // struct GNUNET_PeerIdentity neighbor;
3106 // msg.header.size = htons (sizeof (msg));
3107 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3108 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3109 // msg.tid = htonl (t->id.tid);
3110 // msg.peer1 = my_full_id;
3111 // GNUNET_PEER_resolve (pid, &msg.peer2);
3112 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3113 // send_prebuilt_message (&msg.header, &neighbor, t);
3120 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3122 * If channel is not reliable, do nothing.
3124 * @param ch Channel this is about.
3125 * @param fwd Is for FWD traffic? (ACK dest->owner)
3128 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3130 struct GNUNET_MESH_DataACK msg;
3131 struct MeshChannelReliability *rel;
3132 struct MeshReliableMessage *copy;
3138 if (GNUNET_NO == ch->reliable)
3142 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3143 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3145 "send_data_ack for %u\n",
3148 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3149 msg.header.type = htons (type);
3150 msg.header.size = htons (sizeof (msg));
3151 msg.chid = htonl (ch->gid);
3152 msg.mid = htonl (*mid - 1);
3154 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3156 if (copy->type != type)
3158 delta = copy->mid - *mid;
3161 mask = 0x1LL << delta;
3162 msg.futures |= mask;
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3164 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3165 copy->mid, delta, mask, msg.futures);
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3169 send_prebuilt_message_channel (&msg.header, ch, fwd);
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3175 * Send an ACK informing the predecessor about the available buffer space.
3177 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3178 * the ACK itself goes "back" (dest->root).
3180 * @param c Connection on which to send the ACK.
3181 * @param fwd Is this FWD ACK? (Going dest->owner)
3184 connection_send_ack (struct MeshConnection *c, int fwd)
3186 struct MeshFlowControl *next_fc;
3187 struct MeshFlowControl *prev_fc;
3191 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3192 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 "connection send %s ack on %s\n",
3196 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3198 /* Check if we need to transmit the ACK */
3199 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3203 " last pid recv: %u, last ack sent: %u\n",
3204 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3208 /* Ok, ACK might be necessary, what PID to ACK? */
3209 delta = next_fc->queue_max - next_fc->queue_n;
3210 ack = prev_fc->last_pid_recv + delta;
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3213 " last pid %u, last ack %u, qmax %u, q %u\n",
3214 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3215 next_fc->queue_max, next_fc->queue_n);
3216 if (ack == prev_fc->last_ack_sent)
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3222 prev_fc->last_ack_sent = ack;
3223 send_ack (c, ack, fwd);
3228 * Modify the mesh message TID from global to local and send to client.
3230 * @param ch Channel on which to send the message.
3231 * @param msg Message to modify and send.
3232 * @param c Client to send to.
3233 * @param tid Tunnel ID to use (c can be both owner and client).
3236 channel_send_client_to_tid (struct MeshChannel *ch,
3237 const struct GNUNET_MESH_Data *msg,
3238 struct MeshClient *c, MESH_ChannelNumber id)
3240 struct GNUNET_MESH_LocalData *copy;
3241 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3242 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3244 if (size < sizeof (struct GNUNET_MessageHeader))
3246 GNUNET_break_op (0);
3254 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3255 memcpy (©[1], &msg[1], size);
3256 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3257 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3258 copy->id = htonl (id);
3259 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3260 ©->header, GNUNET_NO);
3264 * Modify the data message ID from global to local and send to client.
3266 * @param ch Channel on which to send the message.
3267 * @param msg Message to modify and send.
3268 * @param fwd Forward?
3271 channel_send_client_data (struct MeshChannel *ch,
3272 const struct GNUNET_MESH_Data *msg,
3276 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3278 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3283 * Send a buffered message to the client, for in order delivery or
3284 * as result of client ACK.
3286 * @param ch Channel on which to empty the message buffer.
3287 * @param c Client to send to.
3288 * @param rel Reliability structure to corresponding peer.
3289 * If rel == bck_rel, this is FWD data.
3292 channel_send_client_buffered_data (struct MeshChannel *ch,
3293 struct MeshClient *c,
3294 struct MeshChannelReliability *rel)
3296 struct MeshReliableMessage *copy;
3299 if (GNUNET_NO == rel->client_ready)
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3305 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3308 copy = rel->head_recv;
3309 /* We never buffer channel management messages */
3312 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3314 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3317 " have %u! now expecting %u\n",
3318 copy->mid, *mid + 1);
3319 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3322 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 " reliable && don't have %u, next is %u\n",
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3339 * We have received a message out of order, or the client is not ready.
3340 * Buffer it until we receive an ACK from the client or the missing
3341 * message from the channel.
3343 * @param msg Message to buffer (MUST be of type MESH_DATA).
3344 * @param rel Reliability data to the corresponding direction.
3347 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3348 struct MeshChannelReliability *rel)
3350 struct MeshReliableMessage *copy;
3351 struct MeshReliableMessage *prev;
3355 size = ntohs (msg->header.size);
3356 mid = ntohl (msg->mid);
3358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3360 copy = GNUNET_malloc (sizeof (*copy) + size);
3363 memcpy (©[1], msg, size);
3367 // FIXME do something better than O(n), although n < 64...
3368 // FIXME start from the end (most messages are the latest ones)
3369 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3372 if (GMC_is_pid_bigger (prev->mid, mid))
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3375 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3381 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3387 * Destroy a reliable message after it has been acknowledged, either by
3388 * direct mid ACK or bitfield. Updates the appropriate data structures and
3389 * timers and frees all memory.
3391 * @param copy Message that is no longer needed: remote peer got it.
3394 rel_message_free (struct MeshReliableMessage *copy)
3396 struct MeshChannelReliability *rel;
3397 struct GNUNET_TIME_Relative time;
3400 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3401 rel->expected_delay.rel_value_us *= 7;
3402 rel->expected_delay.rel_value_us += time.rel_value_us;
3403 rel->expected_delay.rel_value_us /= 8;
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3408 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3410 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3412 rel->retry_timer = rel->expected_delay;
3413 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3419 * Destroy all reliable messages queued for a channel,
3420 * during a channel destruction.
3421 * Frees the reliability structure itself.
3423 * @param rel Reliability data for a channel.
3426 channel_rel_free_all (struct MeshChannelReliability *rel)
3428 struct MeshReliableMessage *copy;
3429 struct MeshReliableMessage *next;
3434 for (copy = rel->head_recv; NULL != copy; copy = next)
3437 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3440 for (copy = rel->head_sent; NULL != copy; copy = next)
3443 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3446 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3447 GNUNET_SCHEDULER_cancel (rel->retry_task);
3453 * Mark future messages as ACK'd.
3455 * @param rel Reliability data.
3456 * @param msg DataACK message with a bitfield of future ACK'd messages.
3459 channel_rel_free_sent (struct MeshChannelReliability *rel,
3460 const struct GNUNET_MESH_DataACK *msg)
3462 struct MeshReliableMessage *copy;
3463 struct MeshReliableMessage *next;
3470 bitfield = msg->futures;
3471 mid = ntohl (msg->mid);
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 "free_sent_reliable %u %llX\n",
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3476 " rel %p, head %p\n",
3477 rel, rel->head_sent);
3478 for (i = 0, copy = rel->head_sent;
3479 i < 64 && NULL != copy && 0 != bitfield;
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3483 " trying bit %u (mid %u)\n",
3486 if (0 == (bitfield & mask))
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3490 /* Bit was set, clear the bit from the bitfield */
3493 /* The i-th bit was set. Do we have that copy? */
3494 /* Skip copies with mid < target */
3495 target = mid + i + 1;
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3497 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3500 /* Did we run out of copies? (previously freed, it's ok) */
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3507 /* Did we overshoot the target? (previously freed, it's ok) */
3508 if (GMC_is_pid_bigger (copy->mid, target))
3510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3514 /* Now copy->mid == target, free it */
3516 rel_message_free (copy);
3519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3524 * We haven't received an ACK after a certain time: restransmit the message.
3526 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3527 * @param tc TaskContext.
3530 channel_retransmit_message (void *cls,
3531 const struct GNUNET_SCHEDULER_TaskContext *tc)
3533 struct MeshChannelReliability *rel = cls;
3534 struct MeshReliableMessage *copy;
3535 struct MeshPeerQueue *q;
3536 struct MeshChannel *ch;
3537 struct MeshConnection *c;
3538 struct GNUNET_MESH_Data *payload;
3539 struct MeshPeer *hop;
3542 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3543 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3547 copy = rel->head_sent;
3554 /* Search the message to be retransmitted in the outgoing queue.
3555 * Check only the queue for the connection that is going to be used,
3556 * if the message is stuck in some other connection's queue we shouldn't
3558 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3559 * the old connection could be temporary stalled or the queue happened to
3560 * be long at time of insertion.
3561 * - not sending the new one could cause terrible delays the old connection
3564 payload = (struct GNUNET_MESH_Data *) ©[1];
3565 fwd = (rel == ch->fwd_rel);
3566 c = tunnel_get_connection (ch->t, fwd);
3567 hop = connection_get_hop (c, fwd);
3568 for (q = hop->queue_head; NULL != q; q = q->next)
3570 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3572 struct GNUNET_MESH_Data *queued_data = q->cls;
3574 if (queued_data->mid == payload->mid)
3579 /* Message not found in the queue that we are going to use. */
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3584 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3585 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3592 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3593 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3594 &channel_retransmit_message,
3600 * Send an ACK to a client if needed.
3602 * @param ch Channel this is regarding.
3603 * @param fwd Is this about fwd traffic? (ACK goes the opposite direction).
3606 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3608 struct MeshChannelReliability *rel;
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3611 "Channel send client %s ack on %s:%X\n",
3612 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3614 /* Check for buffer space */
3615 if (0 >= tunnel_get_buffer (ch->t, fwd))
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " No buffer :(\n");
3621 /* Send ACK (fwd indicates traffic to be ACK'd) to client */
3622 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3623 if (GNUNET_NO == rel->client_ready)
3624 send_local_ack (ch, fwd);
3626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client ready\n");
3631 * Send ACK on one or more connections due to buffer space to the client.
3633 * @param ch Channel which has some free buffer space.
3634 * @param buffer Buffer space.
3635 * @param fwd Is this in the FWD direction?
3638 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3640 struct MeshTunnel2 *t = ch->t;
3641 struct MeshConnection *c;
3642 struct MeshFlowControl *fc;
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "Channel send connection %s ack on %s:%X\n",
3649 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3651 /* Count connections, how many messages are already allowed */
3652 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3654 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3655 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3660 allowed += fc->last_ack_sent - fc->last_pid_recv;
3664 /* Make sure there is no overflow */
3665 if (allowed > buffer)
3671 /* Authorize connections to send more data */
3672 to_allow = buffer - allowed;
3673 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3675 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3676 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3680 send_ack (c, fc->last_ack_sent + 1, fwd);
3684 GNUNET_break (to_allow == 0);
3689 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3691 * @param ch Channel to mark as ready.
3692 * @param fwd Was the CREATE message sent fwd?
3695 channel_confirm (struct MeshChannel *ch, int fwd)
3697 struct MeshChannelReliability *rel;
3698 struct MeshReliableMessage *copy;
3699 struct MeshReliableMessage *next;
3701 ch->state = MESH_CHANNEL_READY;
3703 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3704 for (copy = rel->head_sent; NULL != copy; copy = next)
3706 struct GNUNET_MessageHeader *msg;
3709 msg = (struct GNUNET_MessageHeader *) ©[1];
3710 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3712 rel_message_free (copy);
3720 * Save a copy to retransmit in case it gets lost.
3722 * Initializes all needed callbacks and timers.
3724 * @param ch Channel this message goes on.
3725 * @param msg Message to copy.
3726 * @param fwd Is this fwd traffic?
3729 channel_save_copy (struct MeshChannel *ch,
3730 const struct GNUNET_MessageHeader *msg,
3733 struct MeshChannelReliability *rel;
3734 struct MeshReliableMessage *copy;
3739 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3740 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3741 type = ntohs (msg->type);
3742 size = ntohs (msg->size);
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3745 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3747 copy->timestamp = GNUNET_TIME_absolute_get ();
3750 memcpy (©[1], msg, size);
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3753 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3754 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3757 GNUNET_TIME_relative_multiply (rel->expected_delay,
3758 MESH_RETRANSMIT_MARGIN);
3760 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3761 &channel_retransmit_message,
3768 * Send keepalive packets for a connection.
3770 * @param c Connection to keep alive..
3771 * @param fwd Is this a FWD keepalive? (owner -> dest).
3774 connection_keepalive (struct MeshConnection *c, int fwd)
3776 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3777 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3781 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3782 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3785 "sending %s keepalive for connection %s[%d]\n",
3786 fwd ? "FWD" : "BCK",
3787 peer2s (c->t->peer),
3790 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3791 msg->header.size = htons (size);
3792 msg->header.type = htons (type);
3795 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3800 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3802 * @param c Connection for which to send the message.
3803 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3806 connection_recreate (struct MeshConnection *c, int fwd)
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3810 send_connection_create (c);
3812 send_connection_ack (c, GNUNET_NO);
3817 * Generic connection timer management.
3818 * Depending on the role of the peer in the connection will send the
3819 * appropriate message (build or keepalive)
3821 * @param c Conncetion to maintain.
3822 * @param fwd Is FWD?
3825 connection_maintain (struct MeshConnection *c, int fwd)
3827 if (MESH_TUNNEL_SEARCHING == c->t->state)
3829 /* TODO DHT GET with RO_BART */
3834 case MESH_CONNECTION_NEW:
3836 case MESH_CONNECTION_SENT:
3837 connection_recreate (c, fwd);
3839 case MESH_CONNECTION_READY:
3840 connection_keepalive (c, fwd);
3849 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3851 struct MeshConnection *c = cls;
3853 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3854 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3857 connection_maintain (c, GNUNET_YES);
3858 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3859 &connection_fwd_keepalive,
3865 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3867 struct MeshConnection *c = cls;
3869 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3870 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3873 connection_maintain (c, GNUNET_NO);
3874 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3875 &connection_bck_keepalive,
3881 * Send a message to all peers in this connection that the connection
3882 * is no longer valid.
3884 * If some peer should not receive the message, it should be zero'ed out
3885 * before calling this function.
3887 * @param c The connection whose peers to notify.
3890 connection_send_destroy (struct MeshConnection *c)
3892 struct GNUNET_MESH_ConnectionDestroy msg;
3894 msg.header.size = htons (sizeof (msg));
3895 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 " sending connection destroy for connection %s[%X]\n",
3899 peer2s (c->t->peer),
3902 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3903 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3904 c->destroy = GNUNET_YES;
3909 * Confirm we got a channel create.
3911 * @param ch The channel to confirm.
3912 * @param fwd Should we send the ACK fwd?
3915 channel_send_ack (struct MeshChannel *ch, int fwd)
3917 struct GNUNET_MESH_ChannelManage msg;
3919 msg.header.size = htons (sizeof (msg));
3920 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3922 " sending channel ack for channel %s:%X\n",
3923 peer2s (ch->t->peer),
3926 msg.chid = htonl (ch->gid);
3927 send_prebuilt_message_channel (&msg.header, ch, fwd);
3932 * Send a message to all clients (local and remote) of this channel
3933 * notifying that the channel is no longer valid.
3935 * If some peer or client should not receive the message,
3936 * should be zero'ed out before calling this function.
3938 * @param ch The channel whose clients to notify.
3941 channel_send_destroy (struct MeshChannel *ch)
3943 struct GNUNET_MESH_ChannelManage msg;
3945 msg.header.size = htons (sizeof (msg));
3946 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3948 " sending channel destroy for channel %s:%X\n",
3949 peer2s (ch->t->peer),
3952 if (NULL != ch->root)
3954 msg.chid = htonl (ch->lid_root);
3955 send_local_channel_destroy (ch, GNUNET_NO);
3959 msg.chid = htonl (ch->gid);
3960 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3963 if (NULL != ch->dest)
3965 msg.chid = htonl (ch->lid_dest);
3966 send_local_channel_destroy (ch, GNUNET_YES);
3970 msg.chid = htonl (ch->gid);
3971 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3979 static struct MeshTunnel2 *
3982 struct MeshTunnel2 *t;
3984 t = GNUNET_new (struct MeshTunnel2);
3986 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3988 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3989 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3991 // GNUNET_break (0);
3992 // tunnel_destroy (t);
3996 // char salt[] = "salt";
3997 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3998 // salt, sizeof (salt),
3999 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4000 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4001 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4003 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4004 // salt, sizeof (salt),
4005 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4006 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4007 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4015 * Add a connection to a tunnel.
4018 * @param c Connection.
4021 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4024 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4029 * Initialize a Flow Control structure to the initial state.
4031 * @param fc Flow Control structure to initialize.
4034 fc_init (struct MeshFlowControl *fc)
4037 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4038 fc->last_pid_recv = (uint32_t) -1;
4039 fc->last_ack_sent = (uint32_t) 0;
4040 fc->last_ack_recv = (uint32_t) 0;
4041 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4042 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4044 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4048 static struct MeshConnection *
4049 connection_new (const struct GNUNET_HashCode *cid)
4051 struct MeshConnection *c;
4053 c = GNUNET_new (struct MeshConnection);
4055 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4057 fc_init (&c->fwd_fc);
4058 fc_init (&c->bck_fc);
4067 * Find a connection.
4069 * @param cid Connection ID.
4071 static struct MeshConnection *
4072 connection_get (const struct GNUNET_HashCode *cid)
4074 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4079 connection_destroy (struct MeshConnection *c)
4081 struct MeshPeer *peer;
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4087 peer2s (c->t->peer),
4090 /* Cancel all traffic */
4091 connection_cancel_queues (c, GNUNET_YES);
4092 connection_cancel_queues (c, GNUNET_NO);
4094 /* Cancel maintainance task (keepalive/timeout) */
4095 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4096 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4097 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4098 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4100 /* Deregister from neighbors */
4101 peer = connection_get_next_hop (c);
4103 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4104 peer = connection_get_prev_hop (c);
4106 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4109 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4110 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4116 tunnel_destroy (struct MeshTunnel2 *t)
4118 struct MeshConnection *c;
4119 struct MeshConnection *next;
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4127 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4128 // GNUNET_break (0);
4130 for (c = t->connection_head; NULL != c; c = next)
4133 connection_destroy (c);
4136 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4143 * Tunnel is empty: destroy it.
4145 * Notifies all connections about the destruction.
4147 * @param t Tunnel to destroy.
4150 tunnel_destroy_empty (struct MeshTunnel2 *t)
4152 struct MeshConnection *c;
4154 for (c = t->connection_head; NULL != c; c = c->next)
4156 if (GNUNET_NO == c->destroy)
4157 connection_send_destroy (c);
4160 if (0 == t->pending_messages)
4163 t->destroy = GNUNET_YES;
4168 * Destroy tunnel if empty (no more channels).
4170 * @param t Tunnel to destroy if empty.
4173 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4175 if (NULL != t->channel_head)
4178 tunnel_destroy_empty (t);
4183 * Destroy a channel and free all resources.
4185 * @param ch Channel to destroy.
4188 channel_destroy (struct MeshChannel *ch)
4190 struct MeshClient *c;
4198 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4209 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4216 channel_rel_free_all (ch->fwd_rel);
4217 channel_rel_free_all (ch->bck_rel);
4219 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4220 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4226 * Create a new channel.
4228 * @param t Tunnel this channel is in.
4229 * @param owner Client that owns the channel, NULL for foreign channels.
4230 * @param lid_root Local ID for root client.
4232 * @return A new initialized channel. NULL on error.
4234 static struct MeshChannel *
4235 channel_new (struct MeshTunnel2 *t,
4236 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4238 struct MeshChannel *ch;
4240 ch = GNUNET_new (struct MeshChannel);
4242 ch->lid_root = lid_root;
4245 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4247 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4251 while (NULL != channel_get (t, t->next_chid))
4252 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4253 ch->gid = t->next_chid;
4254 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4257 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4261 channel_destroy (ch);
4262 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4272 * Set options in a channel, extracted from a bit flag field
4274 * @param ch Channel to set options to.
4275 * @param options Bit array in host byte order.
4278 channel_set_options (struct MeshChannel *ch, uint32_t options)
4280 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4281 GNUNET_YES : GNUNET_NO;
4282 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4283 GNUNET_YES : GNUNET_NO;
4288 * Iterator for deleting each channel whose client endpoint disconnected.
4290 * @param cls Closure (client that has disconnected).
4291 * @param key The local channel id (used to access the hashmap).
4292 * @param value The value stored at the key (channel to destroy).
4294 * @return GNUNET_OK, keep iterating.
4297 channel_destroy_iterator (void *cls,
4301 struct MeshChannel *ch = value;
4302 struct MeshClient *c = cls;
4303 struct MeshTunnel2 *t;
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4306 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4307 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4321 channel_send_destroy (ch);
4322 channel_destroy (ch);
4323 tunnel_destroy_if_empty (t);
4330 * Remove client's ports from the global hashmap on disconnect.
4332 * @param cls Closure (unused).
4334 * @param value Client structure.
4336 * @return GNUNET_OK, keep iterating.
4339 client_release_ports (void *cls,
4345 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4346 if (GNUNET_YES != res)
4349 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4350 "Port %u by client %p was not registered.\n",
4358 * Timeout function due to lack of keepalive/traffic from the owner.
4359 * Destroys connection if called.
4361 * @param cls Closure (connection to destroy).
4362 * @param tc TaskContext.
4365 connection_fwd_timeout (void *cls,
4366 const struct GNUNET_SCHEDULER_TaskContext *tc)
4368 struct MeshConnection *c = cls;
4370 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4371 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4374 "Connection %s[%X] FWD timed out. Destroying.\n",
4375 peer2s (c->t->peer),
4378 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4381 connection_destroy (c);
4386 * Timeout function due to lack of keepalive/traffic from the destination.
4387 * Destroys connection if called.
4389 * @param cls Closure (connection to destroy).
4390 * @param tc TaskContext
4393 connection_bck_timeout (void *cls,
4394 const struct GNUNET_SCHEDULER_TaskContext *tc)
4396 struct MeshConnection *c = cls;
4398 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4399 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4403 "Connection %s[%X] FWD timed out. Destroying.\n",
4404 peer2s (c->t->peer),
4407 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4410 connection_destroy (c);
4415 * Resets the connection timeout task, some other message has done the
4417 * - For the first peer on the direction this means to send
4418 * a keepalive or a path confirmation message (either create or ACK).
4419 * - For all other peers, this means to destroy the connection,
4420 * due to lack of activity.
4421 * Starts the tiemout if no timeout was running (connection just created).
4423 * @param c Connection whose timeout to reset.
4424 * @param fwd Is this forward?
4426 * TODO use heap to improve efficiency of scheduler.
4429 connection_reset_timeout (struct MeshConnection *c, int fwd)
4431 GNUNET_SCHEDULER_TaskIdentifier *ti;
4432 GNUNET_SCHEDULER_Task f;
4434 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4436 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4437 GNUNET_SCHEDULER_cancel (*ti);
4439 if (connection_is_origin (c, fwd)) /* Endpoint */
4441 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4442 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4446 struct GNUNET_TIME_Relative delay;
4448 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4449 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4450 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4456 * Iterator to notify all connections of a broken link. Mark connections
4457 * to destroy after all traffic has been sent.
4459 * @param cls Closure (peer disconnected).
4460 * @param key Current key code (tid).
4461 * @param value Value in the hash map (connection).
4463 * @return GNUNET_YES if we should continue to iterate,
4467 connection_broken (void *cls,
4468 const struct GNUNET_HashCode *key,
4471 struct MeshPeer *peer = cls;
4472 struct MeshConnection *c = value;
4473 struct GNUNET_MESH_ConnectionBroken msg;
4476 fwd = peer == connection_get_prev_hop (c);
4477 connection_cancel_queues (c, !fwd);
4479 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4480 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4482 msg.peer1 = my_full_id;
4483 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4484 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4485 c->destroy = GNUNET_YES;
4490 /******************************************************************************/
4491 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4492 /******************************************************************************/
4495 * Free a transmission that was already queued with all resources
4496 * associated to the request.
4498 * @param queue Queue handler to cancel.
4499 * @param clear_cls Is it necessary to free associated cls?
4502 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4504 struct MeshPeer *peer;
4505 struct MeshFlowControl *fc;
4510 GNUNET_assert (NULL != queue->c);
4511 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4513 if (GNUNET_YES == clear_cls)
4515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4516 GNUNET_MESH_DEBUG_M2S (queue->type));
4517 switch (queue->type)
4519 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4520 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4521 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4522 GNUNET_break (GNUNET_YES == queue->c->destroy);
4524 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4525 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4526 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4527 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4528 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4529 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4531 GNUNET_free_non_null (queue->cls);
4536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4537 GNUNET_MESH_DEBUG_M2S (queue->type));
4541 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4543 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4544 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4550 if (NULL != queue->c)
4552 queue->c->pending_messages--;
4553 if (NULL != queue->c->t)
4555 queue->c->t->pending_messages--;
4559 GNUNET_free (queue);
4564 queue_send (void *cls, size_t size, void *buf)
4566 struct MeshPeer *peer = cls;
4567 struct MeshFlowControl *fc;
4568 struct MeshConnection *c;
4569 struct GNUNET_MessageHeader *msg;
4570 struct MeshPeerQueue *queue;
4571 struct MeshTunnel2 *t;
4572 struct MeshChannel *ch;
4573 const struct GNUNET_PeerIdentity *dst_id;
4579 peer->core_transmit = NULL;
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4582 if (NULL == buf || 0 == size)
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4589 queue = peer_get_first_message (peer);
4592 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4597 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4600 dst_id = GNUNET_PEER_resolve2 (peer->id);
4601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4602 /* Check if buffer size is enough for the message */
4603 if (queue->size > size)
4605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4606 peer->core_transmit =
4607 GNUNET_CORE_notify_transmit_ready (core_handle,
4610 GNUNET_TIME_UNIT_FOREVER_REL,
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4619 t = (NULL != c) ? c->t : NULL;
4623 switch (queue->type)
4625 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4626 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4627 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4628 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4629 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4630 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4633 GNUNET_MESH_DEBUG_M2S (queue->type));
4634 data_size = send_core_data_raw (queue->cls, size, buf);
4635 msg = (struct GNUNET_MessageHeader *) buf;
4636 type = ntohs (msg->type);
4638 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4640 if (connection_is_origin (c, GNUNET_YES))
4641 data_size = send_core_connection_create (queue->c, size, buf);
4643 data_size = send_core_data_raw (queue->cls, size, buf);
4645 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4647 if (connection_is_origin (c, GNUNET_NO))
4648 data_size = send_core_connection_ack (queue->c, size, buf);
4650 data_size = send_core_data_raw (queue->cls, size, buf);
4652 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4653 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4654 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4655 /* This should be encapsulted */
4660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4665 if (0 < drop_percent &&
4666 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4668 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4669 "Dropping message of type %s\n",
4670 GNUNET_MESH_DEBUG_M2S (queue->type));
4674 /* Free queue, but cls was freed by send_core_* */
4676 queue_destroy (queue, GNUNET_NO);
4678 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4681 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4682 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4683 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4684 fc->last_pid_sent = pid;
4686 channel_send_client_ack (ch, fwd);
4688 connection_send_ack (c, fwd);
4694 /* If more data in queue, send next */
4695 queue = peer_get_first_message (peer);
4698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4699 if (NULL == peer->core_transmit) {
4700 peer->core_transmit =
4701 GNUNET_CORE_notify_transmit_ready(core_handle,
4704 GNUNET_TIME_UNIT_FOREVER_REL,
4712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4713 "* tmt rdy called somewhere else\n");
4715 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
4719 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4724 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4726 GNUNET_SCHEDULER_cancel (fc->poll_task);
4727 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4732 c->pending_messages--;
4733 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4736 connection_destroy (c);
4742 t->pending_messages--;
4743 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4745 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4755 queue_add (void *cls, uint16_t type, size_t size,
4756 struct MeshConnection *c,
4757 struct MeshChannel *ch,
4760 struct MeshPeerQueue *queue;
4761 struct MeshFlowControl *fc;
4762 struct MeshPeer *peer;
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4766 "queue add %s %s (%u) on c %p, ch %p\n",
4767 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4768 GNUNET_assert (NULL != c);
4770 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4771 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4781 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4782 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4789 if (fc->queue_n >= fc->queue_max && 0 == priority)
4791 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4795 "queue full: %u/%u\n",
4796 fc->queue_n, fc->queue_max);
4797 return; /* Drop this message */
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4802 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4803 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4806 "no buffer space (%u > %u): starting poll\n",
4807 fc->last_pid_sent + 1, fc->last_ack_recv);
4808 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4812 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4820 if (100 <= priority)
4822 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4826 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
4832 if (NULL == peer->core_transmit)
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4835 "calling core tmt rdy towards %s for %u bytes\n",
4836 peer2s (peer), size);
4837 peer->core_transmit =
4838 GNUNET_CORE_notify_transmit_ready (core_handle,
4841 GNUNET_TIME_UNIT_FOREVER_REL,
4842 GNUNET_PEER_resolve2 (peer->id),
4849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4850 "core tmt rdy towards %s already called\n",
4854 c->pending_messages++;
4855 c->t->pending_messages++;
4859 /******************************************************************************/
4860 /******************** MESH NETWORK HANDLERS **************************/
4861 /******************************************************************************/
4865 * Generic handler for mesh network payload traffic.
4867 * @param t Tunnel on which we got this message.
4868 * @param message Unencryted data message.
4869 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4871 * @return GNUNET_OK to keep the connection open,
4872 * GNUNET_SYSERR to close it (signal serious error)
4875 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4877 struct MeshChannelReliability *rel;
4878 struct MeshChannel *ch;
4879 struct MeshClient *c;
4886 size = ntohs (msg->header.size);
4888 sizeof (struct GNUNET_MESH_Data) +
4889 sizeof (struct GNUNET_MessageHeader))
4894 type = ntohs (msg->header.type);
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4896 GNUNET_MESH_DEBUG_M2S (type));
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4898 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4901 ch = channel_get (t, ntohl (msg->chid));
4904 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4909 /* Initialize FWD/BCK data */
4910 c = fwd ? ch->dest : ch->root;
4911 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4912 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4920 tunnel_change_state (t, MESH_TUNNEL_READY);
4922 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4924 mid = ntohl (msg->mid);
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4927 if (GNUNET_NO == ch->reliable ||
4928 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4929 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4932 if (GNUNET_YES == ch->reliable)
4934 /* Is this the exact next expected messasge? */
4935 if (mid == *mid_recv)
4937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4938 *mid_recv = *mid_recv + 1;
4939 channel_send_client_data (ch, msg, fwd);
4940 channel_send_client_buffered_data (ch, c, rel);
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4945 channel_rel_add_buffered_data (msg, rel);
4948 else /* Tunnel unreliable, send to clients directly */
4950 channel_send_client_data (ch, msg, fwd);
4955 GNUNET_break_op (0);
4956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4957 " MID %u not expected (%u - %u), dropping!\n",
4958 mid, *mid_recv, *mid_recv + 64);
4961 channel_send_data_ack (ch, fwd);
4966 * Handler for mesh network traffic end-to-end ACKs.
4968 * @param t Tunnel on which we got this message.
4969 * @param message Data message.
4970 * @param fwd Is this a fwd ACK? (dest->orig)
4972 * @return GNUNET_OK to keep the connection open,
4973 * GNUNET_SYSERR to close it (signal serious error)
4976 handle_data_ack (struct MeshTunnel2 *t,
4977 const struct GNUNET_MESH_DataACK *msg, int fwd)
4979 struct MeshChannelReliability *rel;
4980 struct MeshReliableMessage *copy;
4981 struct MeshReliableMessage *next;
4982 struct MeshChannel *ch;
4987 type = ntohs (msg->header.type);
4988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4989 GNUNET_MESH_DEBUG_M2S (type));
4990 ch = channel_get (t, ntohl (msg->chid));
4993 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4996 ack = ntohl (msg->mid);
4997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4998 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
5000 if (GNUNET_YES == fwd)
5013 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5015 if (GMC_is_pid_bigger (copy->mid, ack))
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5018 channel_rel_free_sent (rel, msg);
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5024 rel_message_free (copy);
5026 /* ACK client if needed */
5027 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5029 /* If some message was free'd, update the retransmission delay*/
5030 if (GNUNET_YES == work)
5032 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5034 GNUNET_SCHEDULER_cancel (rel->retry_task);
5035 if (NULL == rel->head_sent)
5037 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5041 struct GNUNET_TIME_Absolute new_target;
5042 struct GNUNET_TIME_Relative delay;
5044 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5045 MESH_RETRANSMIT_MARGIN);
5046 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5048 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5050 GNUNET_SCHEDULER_add_delayed (delay,
5051 &channel_retransmit_message,
5063 * Core handler for connection creation.
5065 * @param cls Closure (unused).
5066 * @param peer Sender (neighbor).
5067 * @param message Message.
5069 * @return GNUNET_OK to keep the connection open,
5070 * GNUNET_SYSERR to close it (signal serious error)
5073 handle_mesh_connection_create (void *cls,
5074 const struct GNUNET_PeerIdentity *peer,
5075 const struct GNUNET_MessageHeader *message)
5077 struct GNUNET_MESH_ConnectionCreate *msg;
5078 struct GNUNET_PeerIdentity *id;
5079 struct GNUNET_HashCode *cid;
5080 struct MeshPeerPath *path;
5081 struct MeshPeer *dest_peer;
5082 struct MeshPeer *orig_peer;
5083 struct MeshConnection *c;
5084 unsigned int own_pos;
5088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5092 size = ntohs (message->size);
5093 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5095 GNUNET_break_op (0);
5099 /* Calculate hops */
5100 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5101 if (size % sizeof (struct GNUNET_PeerIdentity))
5103 GNUNET_break_op (0);
5106 size /= sizeof (struct GNUNET_PeerIdentity);
5109 GNUNET_break_op (0);
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5114 /* Get parameters */
5115 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5117 id = (struct GNUNET_PeerIdentity *) &msg[1];
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5119 " connection %s (%s).\n",
5120 GNUNET_h2s (cid), GNUNET_i2s (id));
5122 /* Create connection */
5123 c = connection_get (cid);
5126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5127 c = connection_new (cid);
5131 connection_reset_timeout (c, GNUNET_YES);
5132 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5134 /* Remember peers */
5135 dest_peer = peer_get (&id[size - 1]);
5136 orig_peer = peer_get (&id[0]);
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5140 path = path_new (size);
5142 for (i = 0; i < size; i++)
5144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5145 GNUNET_i2s (&id[i]));
5146 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5147 if (path->peers[i] == myid)
5150 if (own_pos == 0 && path->peers[own_pos] != myid)
5152 /* create path: self not found in path through self */
5153 GNUNET_break_op (0);
5154 path_destroy (path);
5155 connection_destroy (c);
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5159 path_add_to_peers (path, GNUNET_NO);
5160 c->path = path_duplicate (path);
5161 c->own_pos = own_pos;
5163 /* Is it a connection to us? */
5164 if (own_pos == size - 1)
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5167 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5169 if (NULL == orig_peer->tunnel)
5170 orig_peer->tunnel = tunnel_new ();
5171 tunnel_add_connection (orig_peer->tunnel, c);
5173 send_connection_ack (c, GNUNET_NO);
5175 /* Keep tunnel alive in direction dest->owner*/
5176 connection_reset_timeout (c, GNUNET_NO);
5180 /* It's for somebody else! Retransmit. */
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5182 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5183 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5184 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5191 * Core handler for path ACKs
5193 * @param cls closure
5194 * @param message message
5195 * @param peer peer identity this notification is about
5197 * @return GNUNET_OK to keep the connection open,
5198 * GNUNET_SYSERR to close it (signal serious error)
5201 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5202 const struct GNUNET_MessageHeader *message)
5204 struct GNUNET_MESH_ConnectionACK *msg;
5205 struct MeshPeerPath *p;
5206 struct MeshConnection *c;
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5210 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5212 GNUNET_h2s (&msg->cid));
5213 c = connection_get (&msg->cid);
5216 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5225 /* Add path to peers? */
5229 path_add_to_peers (p, GNUNET_YES);
5235 connection_change_state (c, MESH_CONNECTION_READY);
5236 connection_reset_timeout (c, GNUNET_NO);
5237 if (MESH_TUNNEL_READY != c->t->state)
5238 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5239 tunnel_send_queued_data (c->t, GNUNET_YES);
5241 /* Message for us? */
5242 if (connection_is_terminal (c, GNUNET_NO))
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5245 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5247 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5248 c->t->peer->dhtget = NULL;
5250 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5255 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5261 * Core handler for notifications of broken paths
5263 * @param cls Closure (unused).
5264 * @param peer Peer identity of sending neighbor.
5265 * @param message Message.
5267 * @return GNUNET_OK to keep the connection open,
5268 * GNUNET_SYSERR to close it (signal serious error)
5271 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5272 const struct GNUNET_MessageHeader *message)
5274 struct GNUNET_MESH_ConnectionBroken *msg;
5275 struct MeshConnection *c;
5277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5278 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5279 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5281 GNUNET_i2s (&msg->peer1));
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5283 GNUNET_i2s (&msg->peer2));
5284 c = connection_get (&msg->cid);
5287 GNUNET_break_op (0);
5290 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5291 GNUNET_PEER_search (&msg->peer2));
5298 * Core handler for tunnel destruction
5300 * @param cls Closure (unused).
5301 * @param peer Peer identity of sending neighbor.
5302 * @param message Message.
5304 * @return GNUNET_OK to keep the connection open,
5305 * GNUNET_SYSERR to close it (signal serious error)
5308 handle_mesh_connection_destroy (void *cls,
5309 const struct GNUNET_PeerIdentity *peer,
5310 const struct GNUNET_MessageHeader *message)
5312 struct GNUNET_MESH_ConnectionDestroy *msg;
5313 struct MeshConnection *c;
5317 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5319 "Got a CONNECTION DESTROY message from %s\n",
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5322 " for connection %s\n",
5323 GNUNET_h2s (&msg->cid));
5324 c = connection_get (&msg->cid);
5327 /* Probably already got the message from another path,
5328 * destroyed the tunnel and retransmitted to children.
5331 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5335 id = GNUNET_PEER_search (peer);
5336 if (id == connection_get_prev_hop (c)->id)
5338 else if (id == connection_get_next_hop (c)->id)
5342 GNUNET_break_op (0);
5345 send_prebuilt_message_connection (message, c, NULL, fwd);
5346 c->destroy = GNUNET_YES;
5353 * Handler for channel create messages.
5355 * @param t Tunnel this channel is to be created in.
5356 * @param msg Message.
5357 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5359 * @return GNUNET_OK to keep the connection open,
5360 * GNUNET_SYSERR to close it (signal serious error)
5363 handle_channel_create (struct MeshTunnel2 *t,
5364 struct GNUNET_MESH_ChannelCreate *msg,
5367 MESH_ChannelNumber chid;
5368 struct MeshChannel *ch;
5369 struct MeshClient *c;
5372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5373 /* Check message size */
5374 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5376 GNUNET_break_op (0);
5380 /* Check if channel exists */
5381 chid = ntohl (msg->chid);
5382 ch = channel_get (t, chid);
5385 /* Probably a retransmission, safe to ignore */
5386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5387 if (NULL != ch->dest)
5389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " duplicate CC!!\n");
5390 channel_send_ack (ch, !fwd);
5396 /* Create channel */
5397 ch = channel_new (t, NULL, 0);
5398 channel_set_options (ch, ntohl (msg->opt));
5401 /* Find a destination client */
5402 port = ntohl (msg->port);
5403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5404 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5407 /* TODO send reject */
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5412 channel_add_client (ch, c);
5413 if (GNUNET_YES == ch->reliable)
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5416 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5417 ch->bck_rel->ch = ch;
5418 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5421 send_local_channel_create (ch);
5422 channel_send_ack (ch, !fwd);
5429 * Handler for channel ack messages.
5431 * @param t Tunnel this channel is to be created in.
5432 * @param msg Message.
5433 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5435 * @return GNUNET_OK to keep the connection open,
5436 * GNUNET_SYSERR to close it (signal serious error)
5439 handle_channel_ack (struct MeshTunnel2 *t,
5440 struct GNUNET_MESH_ChannelManage *msg,
5443 MESH_ChannelNumber chid;
5444 struct MeshChannel *ch;
5446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5447 /* Check message size */
5448 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5450 GNUNET_break_op (0);
5454 /* Check if channel exists */
5455 chid = ntohl (msg->chid);
5456 ch = channel_get (t, chid);
5459 GNUNET_break_op (0);
5460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5464 channel_confirm (ch, !fwd);
5470 * Handler for channel destroy messages.
5472 * @param t Tunnel this channel is to be destroyed of.
5473 * @param msg Message.
5474 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5476 * @return GNUNET_OK to keep the connection open,
5477 * GNUNET_SYSERR to close it (signal serious error)
5480 handle_channel_destroy (struct MeshTunnel2 *t,
5481 struct GNUNET_MESH_ChannelManage *msg,
5484 MESH_ChannelNumber chid;
5485 struct MeshChannel *ch;
5487 /* Check message size */
5488 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5490 GNUNET_break_op (0);
5494 /* Check if channel exists */
5495 chid = ntohl (msg->chid);
5496 ch = channel_get (t, chid);
5499 /* Probably a retransmission, safe to ignore */
5503 send_local_channel_destroy (ch, fwd);
5504 channel_destroy (ch);
5511 * Generic handler for mesh network encrypted traffic.
5513 * @param peer Peer identity this notification is about.
5514 * @param message Encrypted message.
5515 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5517 * @return GNUNET_OK to keep the connection open,
5518 * GNUNET_SYSERR to close it (signal serious error)
5521 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5522 const struct GNUNET_MESH_Encrypted *msg,
5525 struct MeshConnection *c;
5526 struct MeshTunnel2 *t;
5527 struct MeshPeer *neighbor;
5528 struct MeshFlowControl *fc;
5535 size = ntohs (msg->header.size);
5537 sizeof (struct GNUNET_MESH_Encrypted) +
5538 sizeof (struct GNUNET_MessageHeader))
5540 GNUNET_break_op (0);
5543 type = ntohs (msg->header.type);
5544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5546 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5548 /* Check connection */
5549 c = connection_get (&msg->cid);
5552 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5557 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5559 /* Check if origin is as expected */
5560 neighbor = connection_get_hop (c, fwd);
5561 if (peer_get (peer)->id != neighbor->id)
5563 GNUNET_break_op (0);
5568 pid = ntohl (msg->pid);
5569 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5571 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5573 "WARNING Received PID %u, (prev %u), ACK %u\n",
5574 pid, fc->last_pid_recv, fc->last_ack_sent);
5577 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5579 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5581 " Pid %u not expected (%u+), dropping!\n",
5582 pid, fc->last_pid_recv + 1);
5585 if (MESH_CONNECTION_SENT == c->state)
5586 connection_change_state (c, MESH_CONNECTION_READY);
5587 connection_reset_timeout (c, fwd);
5588 fc->last_pid_recv = pid;
5590 /* Is this message for us? */
5591 if (connection_is_terminal (c, fwd))
5593 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5595 struct GNUNET_MessageHeader *msgh;
5598 /* TODO signature verification */
5599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5600 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5602 fc->last_pid_recv = pid;
5603 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5604 msgh = (struct GNUNET_MessageHeader *) cbuf;
5605 switch (ntohs (msgh->type))
5607 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5608 /* Don't send hop ACK, wait for client to ACK */
5609 return handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5611 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5612 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5615 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5616 r = handle_channel_create (t,
5617 (struct GNUNET_MESH_ChannelCreate *) msgh,
5621 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5622 r = handle_channel_ack (t,
5623 (struct GNUNET_MESH_ChannelManage *) msgh,
5627 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5628 r = handle_channel_destroy (t,
5629 (struct GNUNET_MESH_ChannelManage *) msgh,
5634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5635 "end-to-end message not known (%u)\n",
5636 ntohs (msgh->type));
5640 connection_send_ack (c, fwd);
5644 /* Message not for us: forward to next hop */
5645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5646 ttl = ntohl (msg->ttl);
5647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5650 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5652 connection_send_ack (c, fwd);
5655 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5657 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5664 * Core handler for mesh network traffic going orig->dest.
5666 * @param cls Closure (unused).
5667 * @param message Message received.
5668 * @param peer Peer who sent the message.
5670 * @return GNUNET_OK to keep the connection open,
5671 * GNUNET_SYSERR to close it (signal serious error)
5674 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5675 const struct GNUNET_MessageHeader *message)
5677 return handle_mesh_encrypted (peer,
5678 (struct GNUNET_MESH_Encrypted *)message,
5683 * Core handler for mesh network traffic going dest->orig.
5685 * @param cls Closure (unused).
5686 * @param message Message received.
5687 * @param peer Peer who sent the message.
5689 * @return GNUNET_OK to keep the connection open,
5690 * GNUNET_SYSERR to close it (signal serious error)
5693 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5694 const struct GNUNET_MessageHeader *message)
5696 return handle_mesh_encrypted (peer,
5697 (struct GNUNET_MESH_Encrypted *)message,
5703 * Core handler for mesh network traffic point-to-point acks.
5705 * @param cls closure
5706 * @param message message
5707 * @param peer peer identity this notification is about
5709 * @return GNUNET_OK to keep the connection open,
5710 * GNUNET_SYSERR to close it (signal serious error)
5713 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5714 const struct GNUNET_MessageHeader *message)
5716 struct GNUNET_MESH_ACK *msg;
5717 struct MeshConnection *c;
5718 struct MeshFlowControl *fc;
5722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5725 msg = (struct GNUNET_MESH_ACK *) message;
5727 c = connection_get (&msg->cid);
5731 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5736 ack = ntohl (msg->ack);
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5739 /* Is this a forward or backward ACK? */
5740 id = GNUNET_PEER_search (peer);
5741 if (connection_get_next_hop (c)->id == id)
5743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5746 else if (connection_get_prev_hop (c)->id == id)
5748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5753 GNUNET_break_op (0);
5757 /* Cancel polling if the ACK is bigger than before. */
5758 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5759 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5762 GNUNET_SCHEDULER_cancel (fc->poll_task);
5763 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5764 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5767 fc->last_ack_recv = ack;
5768 connection_unlock_queue (c, fc == &c->fwd_fc);
5775 * Core handler for mesh network traffic point-to-point ack polls.
5777 * @param cls closure
5778 * @param message message
5779 * @param peer peer identity this notification is about
5781 * @return GNUNET_OK to keep the connection open,
5782 * GNUNET_SYSERR to close it (signal serious error)
5785 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5786 const struct GNUNET_MessageHeader *message)
5788 struct GNUNET_MESH_Poll *msg;
5789 struct MeshConnection *c;
5790 struct MeshFlowControl *fc;
5794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5798 msg = (struct GNUNET_MESH_Poll *) message;
5800 c = connection_get (&msg->cid);
5804 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5806 GNUNET_break_op (0);
5810 /* Is this a forward or backward ACK? */
5811 id = GNUNET_PEER_search (peer);
5812 if (connection_get_next_hop (c)->id == id)
5814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5817 else if (connection_get_prev_hop (c)->id == id)
5819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5824 GNUNET_break_op (0);
5828 pid = ntohl (msg->pid);
5829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5830 pid, fc->last_pid_recv);
5831 fc->last_pid_recv = pid;
5832 connection_send_ack (c, fc == &c->fwd_fc);
5839 * Core handler for mesh keepalives.
5841 * @param cls closure
5842 * @param message message
5843 * @param peer peer identity this notification is about
5844 * @return GNUNET_OK to keep the connection open,
5845 * GNUNET_SYSERR to close it (signal serious error)
5847 * TODO: Check who we got this from, to validate route.
5850 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5851 const struct GNUNET_MessageHeader *message)
5853 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5854 struct MeshConnection *c;
5855 struct MeshPeer *neighbor;
5858 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5862 c = connection_get (&msg->cid);
5865 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5870 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5871 GNUNET_YES : GNUNET_NO;
5873 /* Check if origin is as expected */
5874 neighbor = connection_get_hop (c, fwd);
5875 if (peer_get (peer)->id != neighbor->id)
5877 GNUNET_break_op (0);
5881 connection_change_state (c, MESH_CONNECTION_READY);
5882 connection_reset_timeout (c, fwd);
5884 if (connection_is_terminal (c, fwd))
5887 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5888 send_prebuilt_message_connection (message, c, NULL, fwd);
5896 * Functions to handle messages from core
5898 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5899 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5901 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5902 sizeof (struct GNUNET_MESH_ConnectionACK)},
5903 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5904 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5905 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5906 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5907 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5908 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5909 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5910 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5911 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5912 sizeof (struct GNUNET_MESH_ACK)},
5913 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5914 sizeof (struct GNUNET_MESH_Poll)},
5915 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5916 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5922 * Function to process paths received for a new peer addition. The recorded
5923 * paths form the initial tunnel, which can be optimized later.
5924 * Called on each result obtained for the DHT search.
5926 * @param cls closure
5927 * @param exp when will this value expire
5928 * @param key key of the result
5929 * @param get_path path of the get request
5930 * @param get_path_length lenght of get_path
5931 * @param put_path path of the put request
5932 * @param put_path_length length of the put_path
5933 * @param type type of the result
5934 * @param size number of bytes in data
5935 * @param data pointer to the result data
5938 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5939 const struct GNUNET_HashCode * key,
5940 const struct GNUNET_PeerIdentity *get_path,
5941 unsigned int get_path_length,
5942 const struct GNUNET_PeerIdentity *put_path,
5943 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5944 size_t size, const void *data)
5946 struct MeshPeer *peer = cls;
5947 struct MeshPeerPath *p;
5948 struct MeshConnection *c;
5949 struct GNUNET_PeerIdentity pi;
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5953 GNUNET_PEER_resolve (peer->id, &pi);
5954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5956 p = path_build_from_dht (get_path, get_path_length,
5957 put_path, put_path_length);
5958 path_add_to_peers (p, GNUNET_NO);
5961 /* Count connections */
5962 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5964 /* If we already have 3 (or more (?!)) connections, it's enough */
5968 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5971 peer_connect (peer);
5977 /******************************************************************************/
5978 /********************* MESH LOCAL HANDLES **************************/
5979 /******************************************************************************/
5983 * Handler for client connection.
5985 * @param cls Closure (unused).
5986 * @param client Client handler.
5989 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5991 struct MeshClient *c;
5995 c = GNUNET_malloc (sizeof (struct MeshClient));
5997 c->id = next_client_id++; /* overflow not important: just for debug */
5998 GNUNET_SERVER_client_keep (client);
5999 GNUNET_SERVER_client_set_user_context (client, c);
6000 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
6005 * Handler for client disconnection
6007 * @param cls closure
6008 * @param client identification of the client; NULL
6009 * for the last call when the server is destroyed
6012 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6014 struct MeshClient *c;
6016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6023 c = client_get (client);
6026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6028 GNUNET_SERVER_client_drop (c->handle);
6029 c->shutting_down = GNUNET_YES;
6030 if (NULL != c->own_channels)
6032 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6033 &channel_destroy_iterator, c);
6034 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6037 if (NULL != c->incoming_channels)
6039 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6040 &channel_destroy_iterator, c);
6041 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6044 if (NULL != c->ports)
6046 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6047 &client_release_ports, c);
6048 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6050 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6051 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6065 * Handler for new clients
6067 * @param cls closure
6068 * @param client identification of the client
6069 * @param message the actual message, which includes messages the client wants
6072 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6073 const struct GNUNET_MessageHeader *message)
6075 struct GNUNET_MESH_ClientConnect *cc_msg;
6076 struct MeshClient *c;
6081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6083 /* Check data sanity */
6084 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6085 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6086 if (0 != (size % sizeof (uint32_t)))
6089 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6092 size /= sizeof (uint32_t);
6094 /* Initialize new client structure */
6095 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6102 p = (uint32_t *) &cc_msg[1];
6103 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6104 for (i = 0; i < size; i++)
6107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6109 /* store in client's hashmap */
6110 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6111 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6112 /* store in global hashmap */
6113 /* FIXME only allow one client to have the port open,
6114 * have a backup hashmap with waiting clients */
6115 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6116 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6120 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6121 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6122 GNUNET_SERVER_notification_context_add (nc, client);
6123 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6131 * Handler for requests of new tunnels
6133 * @param cls Closure.
6134 * @param client Identification of the client.
6135 * @param message The actual message.
6138 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6139 const struct GNUNET_MessageHeader *message)
6141 struct GNUNET_MESH_ChannelMessage *msg;
6142 struct MeshPeer *peer;
6143 struct MeshTunnel2 *t;
6144 struct MeshChannel *ch;
6145 struct MeshClient *c;
6146 MESH_ChannelNumber chid;
6148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6150 /* Sanity check for client registration */
6151 if (NULL == (c = client_get (client)))
6154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6159 /* Message size sanity check */
6160 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6167 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6169 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6170 chid = ntohl (msg->channel_id);
6172 /* Sanity check for duplicate channel IDs */
6173 if (NULL != channel_get_by_local_id (c, chid))
6176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6180 peer = peer_get (&msg->peer);
6181 if (NULL == peer->tunnel)
6183 peer->tunnel = tunnel_new ();
6184 peer->tunnel->peer = peer;
6188 /* Create channel */
6189 ch = channel_new (t, c, chid);
6193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6196 ch->port = ntohl (msg->port);
6197 channel_set_options (ch, ntohl (msg->opt));
6199 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6200 ch->fwd_rel = GNUNET_new (struct MeshChannelReliability);
6201 ch->fwd_rel->ch = ch;
6202 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
6204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6205 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6206 peer_connect (peer);
6208 /* Send create channel */
6210 struct GNUNET_MESH_ChannelCreate msgcc;
6212 msgcc.header.size = htons (sizeof (msgcc));
6213 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6214 msgcc.chid = htonl (ch->gid);
6215 msgcc.port = msg->port;
6216 msgcc.opt = msg->opt;
6218 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6221 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6227 * Handler for requests of deleting tunnels
6229 * @param cls closure
6230 * @param client identification of the client
6231 * @param message the actual message
6234 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6235 const struct GNUNET_MessageHeader *message)
6237 struct GNUNET_MESH_ChannelMessage *msg;
6238 struct MeshClient *c;
6239 struct MeshChannel *ch;
6240 struct MeshTunnel2 *t;
6241 MESH_ChannelNumber chid;
6243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6244 "Got a DESTROY CHANNEL from client!\n");
6246 /* Sanity check for client registration */
6247 if (NULL == (c = client_get (client)))
6250 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6255 /* Message sanity check */
6256 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6263 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6265 /* Retrieve tunnel */
6266 chid = ntohl (msg->channel_id);
6267 ch = channel_get_by_local_id (c, chid);
6270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6276 /* Cleanup after the tunnel */
6277 client_delete_channel (c, ch);
6278 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6282 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6289 " channel %X client %p (%p, %p)\n",
6290 chid, c, ch->root, ch->dest);
6295 channel_destroy (ch);
6296 tunnel_destroy_if_empty (t);
6298 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6304 * Handler for client traffic
6306 * @param cls closure
6307 * @param client identification of the client
6308 * @param message the actual message
6311 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6312 const struct GNUNET_MessageHeader *message)
6314 struct GNUNET_MESH_LocalData *msg;
6315 struct MeshClient *c;
6316 struct MeshChannel *ch;
6317 struct MeshChannelReliability *rel;
6318 MESH_ChannelNumber chid;
6322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6323 "Got data from a client!\n");
6325 /* Sanity check for client registration */
6326 if (NULL == (c = client_get (client)))
6329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6334 msg = (struct GNUNET_MESH_LocalData *) message;
6336 /* Sanity check for message size */
6337 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6338 if (size < sizeof (struct GNUNET_MessageHeader))
6341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6345 /* Channel exists? */
6346 chid = ntohl (msg->id);
6347 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6348 ch = channel_get_by_local_id (c, chid);
6352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6356 /* Is the client in the channel? */
6359 ch->root->handle == client)
6363 ch->dest->handle == client) ) )
6366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6370 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6371 rel->client_ready = GNUNET_NO;
6373 /* Ok, everything is correct, send the message. */
6375 struct GNUNET_MESH_Data *payload;
6376 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6377 unsigned char cbuf[p2p_size];
6380 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6381 payload = (struct GNUNET_MESH_Data *) cbuf;
6382 payload->mid = htonl (*mid);
6384 memcpy (&payload[1], &msg[1], size);
6385 payload->header.size = htons (p2p_size);
6386 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6387 payload->chid = htonl (ch->gid);
6388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6389 send_prebuilt_message_channel (&payload->header, ch, fwd);
6391 if (GNUNET_YES == ch->reliable)
6392 channel_save_copy (ch, &payload->header, fwd);
6394 if (tunnel_get_buffer (ch->t, fwd) > 0)
6395 send_local_ack (ch, fwd);
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6404 * Handler for client's ACKs for payload traffic.
6406 * @param cls Closure (unused).
6407 * @param client Identification of the client.
6408 * @param message The actual message.
6411 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6412 const struct GNUNET_MessageHeader *message)
6414 struct GNUNET_MESH_LocalAck *msg;
6415 struct MeshChannelReliability *rel;
6416 struct MeshChannel *ch;
6417 struct MeshClient *c;
6418 MESH_ChannelNumber chid;
6421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6423 /* Sanity check for client registration */
6424 if (NULL == (c = client_get (client)))
6427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6432 msg = (struct GNUNET_MESH_LocalAck *) message;
6434 /* Channel exists? */
6435 chid = ntohl (msg->channel_id);
6436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6437 ch = channel_get_by_local_id (c, chid);
6438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6448 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6449 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6451 rel->client_ready = GNUNET_YES;
6452 channel_send_client_buffered_data (ch, c, rel);
6453 channel_send_connection_ack (ch, 64 - rel->n_recv, fwd);
6455 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6462 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6464 * @param cls Closure (client handle).
6465 * @param key Key (hashed tunnel ID, unused).
6466 * @param value Tunnel info.
6468 * @return GNUNET_YES, to keep iterating.
6471 // monitor_all_tunnels_iterator (void *cls,
6472 // const struct GNUNET_HashCode * key,
6475 // struct GNUNET_SERVER_Client *client = cls;
6476 // struct MeshChannel *ch = value;
6477 // struct GNUNET_MESH_LocalMonitor *msg;
6479 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6480 // msg->channel_id = htonl (ch->gid);
6481 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6482 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6484 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6485 // "* sending info about tunnel %s\n",
6486 // GNUNET_i2s (&msg->owner));
6488 // GNUNET_SERVER_notification_context_unicast (nc, client,
6489 // &msg->header, GNUNET_NO);
6490 // return GNUNET_YES;
6495 * Handler for client's MONITOR request.
6497 * @param cls Closure (unused).
6498 * @param client Identification of the client.
6499 * @param message The actual message.
6502 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6503 const struct GNUNET_MessageHeader *message)
6505 struct MeshClient *c;
6507 /* Sanity check for client registration */
6508 if (NULL == (c = client_get (client)))
6511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6516 "Received get tunnels request from client %u\n",
6518 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6519 // monitor_all_tunnels_iterator,
6521 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6522 "Get tunnels request from client %u completed\n",
6524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6529 * Handler for client's MONITOR_TUNNEL request.
6531 * @param cls Closure (unused).
6532 * @param client Identification of the client.
6533 * @param message The actual message.
6536 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6537 const struct GNUNET_MessageHeader *message)
6539 const struct GNUNET_MESH_LocalMonitor *msg;
6540 struct GNUNET_MESH_LocalMonitor *resp;
6541 struct MeshClient *c;
6542 struct MeshChannel *ch;
6544 /* Sanity check for client registration */
6545 if (NULL == (c = client_get (client)))
6548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6552 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6553 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6554 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6557 ntohl (msg->channel_id));
6558 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6562 /* We don't know the tunnel */
6563 struct GNUNET_MESH_LocalMonitor warn;
6566 GNUNET_SERVER_notification_context_unicast (nc, client,
6569 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6573 /* Initialize context */
6574 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6576 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6577 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6578 &resp->header, GNUNET_NO);
6581 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6582 "Monitor tunnel request from client %u completed\n",
6584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6589 * Functions to handle messages from clients
6591 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6592 {&handle_local_new_client, NULL,
6593 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6594 {&handle_local_channel_create, NULL,
6595 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6596 sizeof (struct GNUNET_MESH_ChannelMessage)},
6597 {&handle_local_channel_destroy, NULL,
6598 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6599 sizeof (struct GNUNET_MESH_ChannelMessage)},
6600 {&handle_local_data, NULL,
6601 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6602 {&handle_local_ack, NULL,
6603 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6604 sizeof (struct GNUNET_MESH_LocalAck)},
6605 {&handle_local_get_tunnels, NULL,
6606 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6607 sizeof (struct GNUNET_MessageHeader)},
6608 {&handle_local_show_tunnel, NULL,
6609 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6610 sizeof (struct GNUNET_MESH_LocalMonitor)},
6616 * Method called whenever a given peer connects.
6618 * @param cls closure
6619 * @param peer peer identity this notification is about
6622 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6624 struct MeshPeer *pi;
6625 struct MeshPeerPath *path;
6627 DEBUG_CONN ("Peer connected\n");
6628 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6629 pi = peer_get (peer);
6632 DEBUG_CONN (" (self)\n");
6633 path = path_new (1);
6637 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6638 path = path_new (2);
6639 path->peers[1] = pi->id;
6640 GNUNET_PEER_change_rc (pi->id, 1);
6641 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6643 path->peers[0] = myid;
6644 GNUNET_PEER_change_rc (myid, 1);
6645 peer_add_path (pi, path, GNUNET_YES);
6647 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6653 * Method called whenever a peer disconnects.
6655 * @param cls closure
6656 * @param peer peer identity this notification is about
6659 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6661 struct MeshPeer *pi;
6663 DEBUG_CONN ("Peer disconnected\n");
6664 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6671 peer_remove_path (pi, myid, pi->id);
6673 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6676 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6677 pi->connections = NULL;
6680 DEBUG_CONN (" (self)\n");
6682 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6689 * Install server (service) handlers and start listening to clients.
6694 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6695 GNUNET_SERVER_connect_notify (server_handle,
6696 &handle_local_client_connect, NULL);
6697 GNUNET_SERVER_disconnect_notify (server_handle,
6698 &handle_local_client_disconnect, NULL);
6699 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6701 clients_head = NULL;
6702 clients_tail = NULL;
6704 GNUNET_SERVER_resume (server_handle);
6709 * To be called on core init/fail.
6711 * @param cls Closure (config)
6712 * @param identity the public identity of this peer
6715 core_init (void *cls,
6716 const struct GNUNET_PeerIdentity *identity)
6718 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6722 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6727 GNUNET_i2s (identity));
6728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6730 GNUNET_i2s (&my_full_id));
6731 GNUNET_CORE_disconnect (core_handle);
6732 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6733 NULL, /* Closure passed to MESH functions */
6734 &core_init, /* Call core_init once connected */
6735 &core_connect, /* Handle connects */
6736 &core_disconnect, /* remove peers on disconnects */
6737 NULL, /* Don't notify about all incoming messages */
6738 GNUNET_NO, /* For header only in notification */
6739 NULL, /* Don't notify about all outbound messages */
6740 GNUNET_NO, /* For header-only out notification */
6741 core_handlers); /* Register these handlers */
6750 /******************************************************************************/
6751 /************************ MAIN FUNCTIONS ****************************/
6752 /******************************************************************************/
6755 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6757 * @param cls closure
6758 * @param key current key code
6759 * @param value value in the hash map
6760 * @return GNUNET_YES if we should continue to iterate,
6764 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6766 struct MeshPeer *p = value;
6767 struct MeshTunnel2 *t = p->tunnel;
6776 * Task run during shutdown.
6782 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6786 if (core_handle != NULL)
6788 GNUNET_CORE_disconnect (core_handle);
6791 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6792 if (dht_handle != NULL)
6794 GNUNET_DHT_disconnect (dht_handle);
6799 GNUNET_SERVER_notification_context_destroy (nc);
6802 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6804 GNUNET_SCHEDULER_cancel (announce_id_task);
6805 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6812 * Process mesh requests.
6814 * @param cls closure
6815 * @param server the initialized server
6816 * @param c configuration to use
6819 run (void *cls, struct GNUNET_SERVER_Handle *server,
6820 const struct GNUNET_CONFIGURATION_Handle *c)
6823 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6826 server_handle = server;
6827 GNUNET_SERVER_suspend (server_handle);
6830 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6835 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6836 "mesh", "peer/privatekey");
6837 GNUNET_SCHEDULER_shutdown ();
6842 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6843 &refresh_connection_time))
6845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6847 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6848 "mesh", "refresh path time");
6849 GNUNET_SCHEDULER_shutdown ();
6854 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6859 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6860 "mesh", "id announce time");
6861 GNUNET_SCHEDULER_shutdown ();
6866 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6869 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6871 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6872 "mesh", "connect timeout");
6873 GNUNET_SCHEDULER_shutdown ();
6878 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6881 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6883 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6884 "mesh", "max msgs queue");
6885 GNUNET_SCHEDULER_shutdown ();
6890 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6893 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6895 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6896 "mesh", "max tunnels");
6897 GNUNET_SCHEDULER_shutdown ();
6902 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6907 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6908 "mesh", "default ttl", 64);
6913 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6916 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6917 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6918 "mesh", "max peers", 1000);
6923 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6931 "Mesh is running with drop mode enabled. "
6932 "This is NOT a good idea! "
6933 "Remove the DROP_PERCENT option from your configuration.\n");
6937 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6938 &dht_replication_level))
6940 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6942 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6943 "mesh", "dht replication level", 3);
6944 dht_replication_level = 3;
6947 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6948 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6949 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6951 dht_handle = GNUNET_DHT_connect (c, 64);
6952 if (NULL == dht_handle)
6956 stats = GNUNET_STATISTICS_create ("mesh", c);
6958 /* Scheduled the task to clean up when shutdown is called */
6959 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6961 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6962 GNUNET_free (keyfile);
6963 GNUNET_assert (NULL != pk);
6964 my_private_key = pk;
6965 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6966 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6967 &my_full_id.hashPubKey);
6968 myid = GNUNET_PEER_intern (&my_full_id);
6969 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6970 "Mesh for peer [%s] starting\n",
6971 GNUNET_i2s(&my_full_id));
6973 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6974 NULL, /* Closure passed to MESH functions */
6975 &core_init, /* Call core_init once connected */
6976 &core_connect, /* Handle connects */
6977 &core_disconnect, /* remove peers on disconnects */
6978 NULL, /* Don't notify about all incoming messages */
6979 GNUNET_NO, /* For header only in notification */
6980 NULL, /* Don't notify about all outbound messages */
6981 GNUNET_NO, /* For header-only out notification */
6982 core_handlers); /* Register these handlers */
6983 if (NULL == core_handle)
6986 GNUNET_SCHEDULER_shutdown ();
6989 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6995 * The main function for the mesh service.
6997 * @param argc number of arguments from the command line
6998 * @param argv command line arguments
6999 * @return 0 ok, 1 on error
7002 main (int argc, char *const *argv)
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7008 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7010 ret = (GNUNET_OK == r) ? 0 : 1;
7011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");