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))
3027 static struct MeshConnection *
3028 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3030 struct MeshConnection *c;
3031 struct GNUNET_HashCode cid;
3032 struct MeshPeer *peer;
3033 unsigned int own_pos;
3035 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3037 c = connection_new (&cid);
3039 for (own_pos = 0; own_pos < p->length; own_pos++)
3041 if (p->peers[own_pos] == myid)
3044 if (own_pos > p->length - 1)
3047 connection_destroy (c);
3050 c->own_pos = own_pos;
3055 c->fwd_maintenance_task =
3056 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3057 &connection_fwd_keepalive, c);
3060 peer = connection_get_next_hop (c);
3061 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3063 peer = connection_get_prev_hop (c);
3064 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3065 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3071 * Notifies a tunnel that a connection has broken that affects at least
3072 * some of its peers. Sends a notification towards the root of the tree.
3073 * In case the peer is the owner of the tree, notifies the client that owns
3074 * the tunnel and tries to reconnect.
3076 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3078 * @param t Tunnel affected.
3079 * @param p1 Peer that got disconnected from p2.
3080 * @param p2 Peer that got disconnected from p1.
3082 * @return Short ID of the peer disconnected (either p1 or p2).
3083 * 0 if the tunnel remained unaffected.
3085 static GNUNET_PEER_Id
3086 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3087 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3089 // if (myid != p1 && myid != p2) FIXME
3094 // if (tree_get_predecessor (t->tree) != 0)
3096 // /* We are the peer still connected, notify owner of the disconnection. */
3097 // struct GNUNET_MESH_PathBroken msg;
3098 // struct GNUNET_PeerIdentity neighbor;
3100 // msg.header.size = htons (sizeof (msg));
3101 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3102 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3103 // msg.tid = htonl (t->id.tid);
3104 // msg.peer1 = my_full_id;
3105 // GNUNET_PEER_resolve (pid, &msg.peer2);
3106 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3107 // send_prebuilt_message (&msg.header, &neighbor, t);
3114 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3116 * @param ch Channel this is about.
3117 * @param fwd Is for FWD traffic? (ACK dest->owner)
3120 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3122 struct GNUNET_MESH_DataACK msg;
3123 struct MeshChannelReliability *rel;
3124 struct MeshReliableMessage *copy;
3130 if (GNUNET_NO == ch->reliable)
3135 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3136 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 "send_data_ack for %u\n",
3141 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3142 msg.header.type = htons (type);
3143 msg.header.size = htons (sizeof (msg));
3144 msg.chid = htonl (ch->gid);
3145 msg.mid = htonl (*mid - 1);
3147 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3149 if (copy->type != type)
3151 delta = copy->mid - *mid;
3154 mask = 0x1LL << delta;
3155 msg.futures |= mask;
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3158 copy->mid, delta, mask, msg.futures);
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3162 send_prebuilt_message_channel (&msg.header, ch, fwd);
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3168 * Send an ACK informing the predecessor about the available buffer space.
3170 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3171 * the ACK itself goes "back" (dest->root).
3173 * @param c Connection on which to send the ACK.
3174 * @param fwd Is this FWD ACK? (Going dest->owner)
3177 connection_send_ack (struct MeshConnection *c, int fwd)
3179 struct MeshFlowControl *next_fc;
3180 struct MeshFlowControl *prev_fc;
3184 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3185 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3188 "connection send %s ack on %s\n",
3189 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3191 /* Check if we need to transmit the ACK */
3192 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3196 " last pid recv: %u, last ack sent: %u\n",
3197 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3201 /* Ok, ACK might be necessary, what PID to ACK? */
3202 delta = next_fc->queue_max - next_fc->queue_n;
3203 ack = prev_fc->last_pid_recv + delta;
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3206 " last pid %u, last ack %u, qmax %u, q %u\n",
3207 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3208 next_fc->queue_max, next_fc->queue_n);
3209 if (ack == prev_fc->last_ack_sent)
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3215 prev_fc->last_ack_sent = ack;
3216 send_ack (c, ack, fwd);
3221 * Modify the mesh message TID from global to local and send to client.
3223 * @param ch Channel on which to send the message.
3224 * @param msg Message to modify and send.
3225 * @param c Client to send to.
3226 * @param tid Tunnel ID to use (c can be both owner and client).
3229 channel_send_client_to_tid (struct MeshChannel *ch,
3230 const struct GNUNET_MESH_Data *msg,
3231 struct MeshClient *c, MESH_ChannelNumber id)
3233 struct GNUNET_MESH_LocalData *copy;
3234 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3235 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3237 if (size < sizeof (struct GNUNET_MessageHeader))
3239 GNUNET_break_op (0);
3247 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3248 memcpy (©[1], &msg[1], size);
3249 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3250 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3251 copy->id = htonl (id);
3252 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3253 ©->header, GNUNET_NO);
3257 * Modify the data message ID from global to local and send to client.
3259 * @param ch Channel on which to send the message.
3260 * @param msg Message to modify and send.
3261 * @param fwd Forward?
3264 channel_send_client_data (struct MeshChannel *ch,
3265 const struct GNUNET_MESH_Data *msg,
3269 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3271 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3276 * Send a buffered message to the client, for in order delivery or
3277 * as result of client ACK.
3279 * @param ch Channel on which to empty the message buffer.
3280 * @param c Client to send to.
3281 * @param rel Reliability structure to corresponding peer.
3282 * If rel == bck_rel, this is FWD data.
3285 channel_send_client_buffered_data (struct MeshChannel *ch,
3286 struct MeshClient *c,
3287 struct MeshChannelReliability *rel)
3289 struct MeshReliableMessage *copy;
3292 if (GNUNET_NO == rel->client_ready)
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3298 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3301 copy = rel->head_recv;
3302 /* We never buffer channel management messages */
3305 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3307 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 " have %u! now expecting %u\n",
3311 copy->mid, *mid + 1);
3312 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3315 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3321 " reliable && don't have %u, next is %u\n",
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3332 * We have received a message out of order, or the client is not ready.
3333 * Buffer it until we receive an ACK from the client or the missing
3334 * message from the channel.
3336 * @param msg Message to buffer (MUST be of type MESH_DATA).
3337 * @param rel Reliability data to the corresponding direction.
3340 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3341 struct MeshChannelReliability *rel)
3343 struct MeshReliableMessage *copy;
3344 struct MeshReliableMessage *prev;
3348 size = ntohs (msg->header.size);
3349 mid = ntohl (msg->mid);
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3353 copy = GNUNET_malloc (sizeof (*copy) + size);
3356 memcpy (©[1], msg, size);
3360 // FIXME do something better than O(n), although n < 64...
3361 // FIXME start from the end (most messages are the latest ones)
3362 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3365 if (GMC_is_pid_bigger (prev->mid, mid))
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3368 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3374 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3380 * Destroy a reliable message after it has been acknowledged, either by
3381 * direct mid ACK or bitfield. Updates the appropriate data structures and
3382 * timers and frees all memory.
3384 * @param copy Message that is no longer needed: remote peer got it.
3387 rel_message_free (struct MeshReliableMessage *copy)
3389 struct MeshChannelReliability *rel;
3390 struct GNUNET_TIME_Relative time;
3393 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3394 rel->expected_delay.rel_value_us *= 7;
3395 rel->expected_delay.rel_value_us += time.rel_value_us;
3396 rel->expected_delay.rel_value_us /= 8;
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3401 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3403 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3405 rel->retry_timer = rel->expected_delay;
3406 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3412 * Destroy all reliable messages queued for a channel,
3413 * during a channel destruction.
3414 * Frees the reliability structure itself.
3416 * @param rel Reliability data for a channel.
3419 channel_rel_free_all (struct MeshChannelReliability *rel)
3421 struct MeshReliableMessage *copy;
3422 struct MeshReliableMessage *next;
3427 for (copy = rel->head_recv; NULL != copy; copy = next)
3430 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3433 for (copy = rel->head_sent; NULL != copy; copy = next)
3436 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3439 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3440 GNUNET_SCHEDULER_cancel (rel->retry_task);
3446 * Mark future messages as ACK'd.
3448 * @param rel Reliability data.
3449 * @param msg DataACK message with a bitfield of future ACK'd messages.
3452 channel_rel_free_sent (struct MeshChannelReliability *rel,
3453 const struct GNUNET_MESH_DataACK *msg)
3455 struct MeshReliableMessage *copy;
3456 struct MeshReliableMessage *next;
3463 bitfield = msg->futures;
3464 mid = ntohl (msg->mid);
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3466 "free_sent_reliable %u %llX\n",
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 " rel %p, head %p\n",
3470 rel, rel->head_sent);
3471 for (i = 0, copy = rel->head_sent;
3472 i < 64 && NULL != copy && 0 != bitfield;
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3476 " trying bit %u (mid %u)\n",
3479 if (0 == (bitfield & mask))
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3483 /* Bit was set, clear the bit from the bitfield */
3486 /* The i-th bit was set. Do we have that copy? */
3487 /* Skip copies with mid < target */
3488 target = mid + i + 1;
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3490 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3493 /* Did we run out of copies? (previously freed, it's ok) */
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3500 /* Did we overshoot the target? (previously freed, it's ok) */
3501 if (GMC_is_pid_bigger (copy->mid, target))
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3507 /* Now copy->mid == target, free it */
3509 rel_message_free (copy);
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3517 * We haven't received an ACK after a certain time: restransmit the message.
3519 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3520 * @param tc TaskContext.
3523 channel_retransmit_message (void *cls,
3524 const struct GNUNET_SCHEDULER_TaskContext *tc)
3526 struct MeshChannelReliability *rel = cls;
3527 struct MeshReliableMessage *copy;
3528 struct MeshPeerQueue *q;
3529 struct MeshChannel *ch;
3530 struct MeshConnection *c;
3531 struct GNUNET_MESH_Data *payload;
3532 struct MeshPeer *hop;
3535 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3536 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3540 copy = rel->head_sent;
3547 /* Search the message to be retransmitted in the outgoing queue.
3548 * Check only the queue for the connection that is going to be used,
3549 * if the message is stuck in some other connection's queue we shouldn't
3551 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3552 * the old connection could be temporary stalled or the queue happened to
3553 * be long at time of insertion.
3554 * - not sending the new one could cause terrible delays the old connection
3557 payload = (struct GNUNET_MESH_Data *) ©[1];
3558 fwd = (rel == ch->fwd_rel);
3559 c = tunnel_get_connection (ch->t, fwd);
3560 hop = connection_get_hop (c, fwd);
3561 for (q = hop->queue_head; NULL != q; q = q->next)
3563 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3565 struct GNUNET_MESH_Data *queued_data = q->cls;
3567 if (queued_data->mid == payload->mid)
3572 /* Message not found in the queue that we are going to use. */
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3577 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3578 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3585 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3586 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3587 &channel_retransmit_message,
3593 * Send an ACK to a client if needed.
3595 * @param ch Channel this is regarding.
3596 * @param fwd Is this about fwd traffic? (ACK goes the opposite direction).
3599 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3601 struct MeshChannelReliability *rel;
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3604 "Channel send client %s ack on %s:%X\n",
3605 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3607 /* Check for buffer space */
3608 if (0 >= tunnel_get_buffer (ch->t, fwd))
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " No buffer :(\n");
3614 /* Send ACK (fwd indicates traffic to be ACK'd) to client */
3615 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3616 if (GNUNET_NO == rel->client_ready)
3617 send_local_ack (ch, fwd);
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client ready\n");
3624 * Send ACK on one or more connections due to buffer space to the client.
3626 * @param ch Channel which has some free buffer space.
3627 * @param buffer Buffer space.
3628 * @param fwd Is this in the FWD direction?
3631 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3633 struct MeshTunnel2 *t = ch->t;
3634 struct MeshConnection *c;
3635 struct MeshFlowControl *fc;
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3641 "Channel send connection %s ack on %s:%X\n",
3642 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3644 /* Count connections, how many messages are already allowed */
3645 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3647 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3648 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3653 allowed += fc->last_ack_sent - fc->last_pid_recv;
3657 /* Make sure there is no overflow */
3658 if (allowed > buffer)
3664 /* Authorize connections to send more data */
3665 to_allow = buffer - allowed;
3666 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3668 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3669 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3673 send_ack (c, fc->last_ack_sent + 1, fwd);
3677 GNUNET_break (to_allow == 0);
3682 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3684 * @param ch Channel to mark as ready.
3685 * @param fwd Was the CREATE message sent fwd?
3688 channel_confirm (struct MeshChannel *ch, int fwd)
3690 struct MeshChannelReliability *rel;
3691 struct MeshReliableMessage *copy;
3692 struct MeshReliableMessage *next;
3694 ch->state = MESH_CHANNEL_READY;
3696 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3697 for (copy = rel->head_sent; NULL != copy; copy = next)
3699 struct GNUNET_MessageHeader *msg;
3702 msg = (struct GNUNET_MessageHeader *) ©[1];
3703 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3705 rel_message_free (copy);
3713 * Save a copy to retransmit in case it gets lost.
3715 * Initializes all needed callbacks and timers.
3717 * @param ch Channel this message goes on.
3718 * @param msg Message to copy.
3719 * @param fwd Is this fwd traffic?
3722 channel_save_copy (struct MeshChannel *ch,
3723 const struct GNUNET_MessageHeader *msg,
3726 struct MeshChannelReliability *rel;
3727 struct MeshReliableMessage *copy;
3732 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3733 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3734 type = ntohs (msg->type);
3735 size = ntohs (msg->size);
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3738 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3740 copy->timestamp = GNUNET_TIME_absolute_get ();
3743 memcpy (©[1], msg, size);
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3746 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3747 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3750 GNUNET_TIME_relative_multiply (rel->expected_delay,
3751 MESH_RETRANSMIT_MARGIN);
3753 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3754 &channel_retransmit_message,
3761 * Send keepalive packets for a connection.
3763 * @param c Connection to keep alive..
3764 * @param fwd Is this a FWD keepalive? (owner -> dest).
3767 connection_keepalive (struct MeshConnection *c, int fwd)
3769 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3770 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3774 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3775 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3778 "sending %s keepalive for connection %s[%d]\n",
3779 fwd ? "FWD" : "BCK",
3780 peer2s (c->t->peer),
3783 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3784 msg->header.size = htons (size);
3785 msg->header.type = htons (type);
3788 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3793 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3795 * @param c Connection for which to send the message.
3796 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3799 connection_recreate (struct MeshConnection *c, int fwd)
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3803 send_connection_create (c);
3805 send_connection_ack (c, GNUNET_NO);
3810 * Generic connection timer management.
3811 * Depending on the role of the peer in the connection will send the
3812 * appropriate message (build or keepalive)
3814 * @param c Conncetion to maintain.
3815 * @param fwd Is FWD?
3818 connection_maintain (struct MeshConnection *c, int fwd)
3820 if (MESH_TUNNEL_SEARCHING == c->t->state)
3822 /* TODO DHT GET with RO_BART */
3827 case MESH_CONNECTION_NEW:
3829 case MESH_CONNECTION_SENT:
3830 connection_recreate (c, fwd);
3832 case MESH_CONNECTION_READY:
3833 connection_keepalive (c, fwd);
3842 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3844 struct MeshConnection *c = cls;
3846 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3847 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3850 connection_maintain (c, GNUNET_YES);
3851 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3852 &connection_fwd_keepalive,
3858 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3860 struct MeshConnection *c = cls;
3862 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3863 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3866 connection_maintain (c, GNUNET_NO);
3867 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3868 &connection_bck_keepalive,
3874 * Send a message to all peers in this connection that the connection
3875 * is no longer valid.
3877 * If some peer should not receive the message, it should be zero'ed out
3878 * before calling this function.
3880 * @param c The connection whose peers to notify.
3883 connection_send_destroy (struct MeshConnection *c)
3885 struct GNUNET_MESH_ConnectionDestroy msg;
3887 msg.header.size = htons (sizeof (msg));
3888 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3891 " sending connection destroy for connection %s[%X]\n",
3892 peer2s (c->t->peer),
3895 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3896 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3897 c->destroy = GNUNET_YES;
3902 * Confirm we got a channel create.
3904 * @param ch The channel to confirm.
3905 * @param fwd Should we send the ACK fwd?
3908 channel_send_ack (struct MeshChannel *ch, int fwd)
3910 struct GNUNET_MESH_ChannelManage msg;
3912 msg.header.size = htons (sizeof (msg));
3913 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3915 " sending channel ack for channel %s:%X\n",
3916 peer2s (ch->t->peer),
3919 msg.chid = htonl (ch->gid);
3920 send_prebuilt_message_channel (&msg.header, ch, fwd);
3925 * Send a message to all clients (local and remote) of this channel
3926 * notifying that the channel is no longer valid.
3928 * If some peer or client should not receive the message,
3929 * should be zero'ed out before calling this function.
3931 * @param ch The channel whose clients to notify.
3934 channel_send_destroy (struct MeshChannel *ch)
3936 struct GNUNET_MESH_ChannelManage msg;
3938 msg.header.size = htons (sizeof (msg));
3939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3941 " sending channel destroy for channel %s:%X\n",
3942 peer2s (ch->t->peer),
3945 if (NULL != ch->root)
3947 msg.chid = htonl (ch->lid_root);
3948 send_local_channel_destroy (ch, GNUNET_NO);
3952 msg.chid = htonl (ch->gid);
3953 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3956 if (NULL != ch->dest)
3958 msg.chid = htonl (ch->lid_dest);
3959 send_local_channel_destroy (ch, GNUNET_YES);
3963 msg.chid = htonl (ch->gid);
3964 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3972 static struct MeshTunnel2 *
3975 struct MeshTunnel2 *t;
3977 t = GNUNET_new (struct MeshTunnel2);
3979 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3981 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3982 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3984 // GNUNET_break (0);
3985 // tunnel_destroy (t);
3989 // char salt[] = "salt";
3990 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3991 // salt, sizeof (salt),
3992 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3993 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3994 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3996 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3997 // salt, sizeof (salt),
3998 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3999 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4000 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4008 * Add a connection to a tunnel.
4011 * @param c Connection.
4014 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4017 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4022 * Initialize a Flow Control structure to the initial state.
4024 * @param fc Flow Control structure to initialize.
4027 fc_init (struct MeshFlowControl *fc)
4030 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4031 fc->last_pid_recv = (uint32_t) -1;
4032 fc->last_ack_sent = (uint32_t) 0;
4033 fc->last_ack_recv = (uint32_t) 0;
4034 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4035 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4037 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4041 static struct MeshConnection *
4042 connection_new (const struct GNUNET_HashCode *cid)
4044 struct MeshConnection *c;
4046 c = GNUNET_new (struct MeshConnection);
4048 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4049 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4050 fc_init (&c->fwd_fc);
4051 fc_init (&c->bck_fc);
4060 * Find a connection.
4062 * @param cid Connection ID.
4064 static struct MeshConnection *
4065 connection_get (const struct GNUNET_HashCode *cid)
4067 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4072 connection_destroy (struct MeshConnection *c)
4074 struct MeshPeer *peer;
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4080 peer2s (c->t->peer),
4083 /* Cancel all traffic */
4084 connection_cancel_queues (c, GNUNET_YES);
4085 connection_cancel_queues (c, GNUNET_NO);
4087 /* Cancel maintainance task (keepalive/timeout) */
4088 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4089 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4090 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4091 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4093 /* Deregister from neighbors */
4094 peer = connection_get_next_hop (c);
4096 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4097 peer = connection_get_prev_hop (c);
4099 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4102 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4103 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4109 tunnel_destroy (struct MeshTunnel2 *t)
4111 struct MeshConnection *c;
4112 struct MeshConnection *next;
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4120 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4121 // GNUNET_break (0);
4123 for (c = t->connection_head; NULL != c; c = next)
4126 connection_destroy (c);
4129 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4136 * Tunnel is empty: destroy it.
4138 * Notifies all connections about the destruction.
4140 * @param t Tunnel to destroy.
4143 tunnel_destroy_empty (struct MeshTunnel2 *t)
4145 struct MeshConnection *c;
4147 for (c = t->connection_head; NULL != c; c = c->next)
4149 if (GNUNET_NO == c->destroy)
4150 connection_send_destroy (c);
4153 if (0 == t->pending_messages)
4156 t->destroy = GNUNET_YES;
4161 * Destroy tunnel if empty (no more channels).
4163 * @param t Tunnel to destroy if empty.
4166 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4168 if (NULL != t->channel_head)
4171 tunnel_destroy_empty (t);
4176 * Destroy a channel and free all resources.
4178 * @param ch Channel to destroy.
4181 channel_destroy (struct MeshChannel *ch)
4183 struct MeshClient *c;
4191 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4202 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4209 channel_rel_free_all (ch->fwd_rel);
4210 channel_rel_free_all (ch->bck_rel);
4212 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4213 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4219 * Create a new channel.
4221 * @param t Tunnel this channel is in.
4222 * @param owner Client that owns the channel, NULL for foreign channels.
4223 * @param lid_root Local ID for root client.
4225 * @return A new initialized channel. NULL on error.
4227 static struct MeshChannel *
4228 channel_new (struct MeshTunnel2 *t,
4229 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4231 struct MeshChannel *ch;
4233 ch = GNUNET_new (struct MeshChannel);
4235 ch->lid_root = lid_root;
4238 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4240 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4244 while (NULL != channel_get (t, t->next_chid))
4245 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4246 ch->gid = t->next_chid;
4247 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4250 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4251 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4254 channel_destroy (ch);
4255 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4265 * Set options in a channel, extracted from a bit flag field
4267 * @param ch Channel to set options to.
4268 * @param options Bit array in host byte order.
4271 channel_set_options (struct MeshChannel *ch, uint32_t options)
4273 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4274 GNUNET_YES : GNUNET_NO;
4275 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4276 GNUNET_YES : GNUNET_NO;
4281 * Iterator for deleting each channel whose client endpoint disconnected.
4283 * @param cls Closure (client that has disconnected).
4284 * @param key The local channel id (used to access the hashmap).
4285 * @param value The value stored at the key (channel to destroy).
4287 * @return GNUNET_OK, keep iterating.
4290 channel_destroy_iterator (void *cls,
4294 struct MeshChannel *ch = value;
4295 struct MeshClient *c = cls;
4296 struct MeshTunnel2 *t;
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4299 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4300 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4314 channel_send_destroy (ch);
4315 channel_destroy (ch);
4316 tunnel_destroy_if_empty (t);
4323 * Remove client's ports from the global hashmap on disconnect.
4325 * @param cls Closure (unused).
4327 * @param value Client structure.
4329 * @return GNUNET_OK, keep iterating.
4332 client_release_ports (void *cls,
4338 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4339 if (GNUNET_YES != res)
4342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4343 "Port %u by client %p was not registered.\n",
4351 * Timeout function due to lack of keepalive/traffic from the owner.
4352 * Destroys connection if called.
4354 * @param cls Closure (connection to destroy).
4355 * @param tc TaskContext.
4358 connection_fwd_timeout (void *cls,
4359 const struct GNUNET_SCHEDULER_TaskContext *tc)
4361 struct MeshConnection *c = cls;
4363 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4364 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4367 "Connection %s[%X] FWD timed out. Destroying.\n",
4368 peer2s (c->t->peer),
4371 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4374 connection_destroy (c);
4379 * Timeout function due to lack of keepalive/traffic from the destination.
4380 * Destroys connection if called.
4382 * @param cls Closure (connection to destroy).
4383 * @param tc TaskContext
4386 connection_bck_timeout (void *cls,
4387 const struct GNUNET_SCHEDULER_TaskContext *tc)
4389 struct MeshConnection *c = cls;
4391 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4392 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4396 "Connection %s[%X] FWD timed out. Destroying.\n",
4397 peer2s (c->t->peer),
4400 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4403 connection_destroy (c);
4408 * Resets the connection timeout task, some other message has done the
4410 * - For the first peer on the direction this means to send
4411 * a keepalive or a path confirmation message (either create or ACK).
4412 * - For all other peers, this means to destroy the connection,
4413 * due to lack of activity.
4414 * Starts the tiemout if no timeout was running (connection just created).
4416 * @param c Connection whose timeout to reset.
4417 * @param fwd Is this forward?
4419 * TODO use heap to improve efficiency of scheduler.
4422 connection_reset_timeout (struct MeshConnection *c, int fwd)
4424 GNUNET_SCHEDULER_TaskIdentifier *ti;
4425 GNUNET_SCHEDULER_Task f;
4427 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4429 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4430 GNUNET_SCHEDULER_cancel (*ti);
4432 if (connection_is_origin (c, fwd)) /* Endpoint */
4434 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4435 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4439 struct GNUNET_TIME_Relative delay;
4441 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4442 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4443 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4449 * Iterator to notify all connections of a broken link. Mark connections
4450 * to destroy after all traffic has been sent.
4452 * @param cls Closure (peer disconnected).
4453 * @param key Current key code (tid).
4454 * @param value Value in the hash map (connection).
4456 * @return GNUNET_YES if we should continue to iterate,
4460 connection_broken (void *cls,
4461 const struct GNUNET_HashCode *key,
4464 struct MeshPeer *peer = cls;
4465 struct MeshConnection *c = value;
4466 struct GNUNET_MESH_ConnectionBroken msg;
4469 fwd = peer == connection_get_prev_hop (c);
4470 connection_cancel_queues (c, !fwd);
4472 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4473 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4475 msg.peer1 = my_full_id;
4476 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4477 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4478 c->destroy = GNUNET_YES;
4483 /******************************************************************************/
4484 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4485 /******************************************************************************/
4488 * Free a transmission that was already queued with all resources
4489 * associated to the request.
4491 * @param queue Queue handler to cancel.
4492 * @param clear_cls Is it necessary to free associated cls?
4495 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4497 struct MeshPeer *peer;
4498 struct MeshFlowControl *fc;
4503 GNUNET_assert (NULL != queue->c);
4504 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4506 if (GNUNET_YES == clear_cls)
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4509 GNUNET_MESH_DEBUG_M2S (queue->type));
4510 switch (queue->type)
4512 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4513 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4514 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4515 GNUNET_break (GNUNET_YES == queue->c->destroy);
4517 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4518 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4519 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4520 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4521 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4522 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4524 GNUNET_free_non_null (queue->cls);
4529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4530 GNUNET_MESH_DEBUG_M2S (queue->type));
4534 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4536 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
4537 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u, \n", fc, fc->queue_n);
4543 if (NULL != queue->c)
4545 queue->c->pending_messages--;
4546 if (NULL != queue->c->t)
4548 queue->c->t->pending_messages--;
4552 GNUNET_free (queue);
4557 queue_send (void *cls, size_t size, void *buf)
4559 struct MeshPeer *peer = cls;
4560 struct MeshFlowControl *fc;
4561 struct MeshConnection *c;
4562 struct GNUNET_MessageHeader *msg;
4563 struct MeshPeerQueue *queue;
4564 struct MeshTunnel2 *t;
4565 struct MeshChannel *ch;
4566 const struct GNUNET_PeerIdentity *dst_id;
4572 peer->core_transmit = NULL;
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
4575 if (NULL == buf || 0 == size)
4577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4582 queue = peer_get_first_message (peer);
4585 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4590 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4593 dst_id = GNUNET_PEER_resolve2 (peer->id);
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4595 /* Check if buffer size is enough for the message */
4596 if (queue->size > size)
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4599 peer->core_transmit =
4600 GNUNET_CORE_notify_transmit_ready (core_handle,
4603 GNUNET_TIME_UNIT_FOREVER_REL,
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
4612 t = (NULL != c) ? c->t : NULL;
4616 switch (queue->type)
4618 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4619 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4620 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4621 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4622 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4623 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4626 GNUNET_MESH_DEBUG_M2S (queue->type));
4627 data_size = send_core_data_raw (queue->cls, size, buf);
4628 msg = (struct GNUNET_MessageHeader *) buf;
4629 type = ntohs (msg->type);
4631 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4633 if (connection_is_origin (c, GNUNET_YES))
4634 data_size = send_core_connection_create (queue->c, size, buf);
4636 data_size = send_core_data_raw (queue->cls, size, buf);
4638 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4640 if (connection_is_origin (c, GNUNET_NO))
4641 data_size = send_core_connection_ack (queue->c, size, buf);
4643 data_size = send_core_data_raw (queue->cls, size, buf);
4645 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4646 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4647 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4648 /* This should be encapsulted */
4653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4658 if (0 < drop_percent &&
4659 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4662 "Dropping message of type %s\n",
4663 GNUNET_MESH_DEBUG_M2S (queue->type));
4667 /* Free queue, but cls was freed by send_core_* */
4669 queue_destroy (queue, GNUNET_NO);
4671 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4674 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4675 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4676 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4677 fc->last_pid_sent = pid;
4679 channel_send_client_ack (ch, fwd);
4681 connection_send_ack (c, fwd);
4687 /* If more data in queue, send next */
4688 queue = peer_get_first_message (peer);
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4692 if (NULL == peer->core_transmit) {
4693 peer->core_transmit =
4694 GNUNET_CORE_notify_transmit_ready(core_handle,
4697 GNUNET_TIME_UNIT_FOREVER_REL,
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4706 "* tmt rdy called somewhere else\n");
4708 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4710 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4712 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4717 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4719 GNUNET_SCHEDULER_cancel (fc->poll_task);
4720 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4725 c->pending_messages--;
4726 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4729 connection_destroy (c);
4735 t->pending_messages--;
4736 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4738 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4748 queue_add (void *cls, uint16_t type, size_t size,
4749 struct MeshConnection *c,
4750 struct MeshChannel *ch,
4753 struct MeshPeerQueue *queue;
4754 struct MeshFlowControl *fc;
4755 struct MeshPeer *peer;
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4759 "queue add %s %s (%u) on c %p, ch %p\n",
4760 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4761 GNUNET_assert (NULL != c);
4763 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4764 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4774 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4775 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4782 if (fc->queue_n >= fc->queue_max && 0 == priority)
4784 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4788 "queue full: %u/%u\n",
4789 fc->queue_n, fc->queue_max);
4790 return; /* Drop this message */
4793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4795 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4796 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4799 "no buffer space (%u > %u): starting poll\n",
4800 fc->last_pid_sent + 1, fc->last_ack_recv);
4801 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4805 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4813 if (100 <= priority)
4815 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4819 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, \n", fc, fc->queue_n);
4825 if (NULL == peer->core_transmit)
4827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4828 "calling core tmt rdy towards %s for %u bytes\n",
4829 peer2s (peer), size);
4830 peer->core_transmit =
4831 GNUNET_CORE_notify_transmit_ready (core_handle,
4834 GNUNET_TIME_UNIT_FOREVER_REL,
4835 GNUNET_PEER_resolve2 (peer->id),
4842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4843 "core tmt rdy towards %s already called\n",
4847 c->pending_messages++;
4848 c->t->pending_messages++;
4852 /******************************************************************************/
4853 /******************** MESH NETWORK HANDLERS **************************/
4854 /******************************************************************************/
4858 * Generic handler for mesh network payload traffic.
4860 * @param t Tunnel on which we got this message.
4861 * @param message Unencryted data message.
4862 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4864 * @return GNUNET_OK to keep the connection open,
4865 * GNUNET_SYSERR to close it (signal serious error)
4868 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4870 struct MeshChannelReliability *rel;
4871 struct MeshChannel *ch;
4872 struct MeshClient *c;
4879 size = ntohs (msg->header.size);
4881 sizeof (struct GNUNET_MESH_Data) +
4882 sizeof (struct GNUNET_MessageHeader))
4887 type = ntohs (msg->header.type);
4888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4889 GNUNET_MESH_DEBUG_M2S (type));
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4891 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4894 ch = channel_get (t, ntohl (msg->chid));
4897 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4902 /* Initialize FWD/BCK data */
4903 c = fwd ? ch->dest : ch->root;
4904 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4905 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4913 tunnel_change_state (t, MESH_TUNNEL_READY);
4915 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4917 mid = ntohl (msg->mid);
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4920 if (GNUNET_NO == ch->reliable ||
4921 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4922 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4925 if (GNUNET_YES == ch->reliable)
4927 /* Is this the exact next expected messasge? */
4928 if (mid == *mid_recv)
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4931 *mid_recv = *mid_recv + 1;
4932 channel_send_client_data (ch, msg, fwd);
4933 channel_send_client_buffered_data (ch, c, rel);
4937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4938 channel_rel_add_buffered_data (msg, rel);
4941 else /* Tunnel unreliable, send to clients directly */
4943 channel_send_client_data (ch, msg, fwd);
4948 GNUNET_break_op (0);
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4950 " MID %u not expected (%u - %u), dropping!\n",
4951 mid, *mid_recv, *mid_recv + 64);
4954 channel_send_data_ack (ch, fwd);
4959 * Handler for mesh network traffic end-to-end ACKs.
4961 * @param t Tunnel on which we got this message.
4962 * @param message Data message.
4963 * @param fwd Is this a fwd ACK? (dest->orig)
4965 * @return GNUNET_OK to keep the connection open,
4966 * GNUNET_SYSERR to close it (signal serious error)
4969 handle_data_ack (struct MeshTunnel2 *t,
4970 const struct GNUNET_MESH_DataACK *msg, int fwd)
4972 struct MeshChannelReliability *rel;
4973 struct MeshReliableMessage *copy;
4974 struct MeshReliableMessage *next;
4975 struct MeshChannel *ch;
4980 type = ntohs (msg->header.type);
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4982 GNUNET_MESH_DEBUG_M2S (type));
4983 ch = channel_get (t, ntohl (msg->chid));
4986 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4989 ack = ntohl (msg->mid);
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4991 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4993 if (GNUNET_YES == fwd)
5006 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5008 if (GMC_is_pid_bigger (copy->mid, ack))
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5011 channel_rel_free_sent (rel, msg);
5015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5017 rel_message_free (copy);
5019 /* ACK client if needed */
5020 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5022 /* If some message was free'd, update the retransmission delay*/
5023 if (GNUNET_YES == work)
5025 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5027 GNUNET_SCHEDULER_cancel (rel->retry_task);
5028 if (NULL == rel->head_sent)
5030 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5034 struct GNUNET_TIME_Absolute new_target;
5035 struct GNUNET_TIME_Relative delay;
5037 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5038 MESH_RETRANSMIT_MARGIN);
5039 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5041 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5043 GNUNET_SCHEDULER_add_delayed (delay,
5044 &channel_retransmit_message,
5056 * Core handler for connection creation.
5058 * @param cls Closure (unused).
5059 * @param peer Sender (neighbor).
5060 * @param message Message.
5062 * @return GNUNET_OK to keep the connection open,
5063 * GNUNET_SYSERR to close it (signal serious error)
5066 handle_mesh_connection_create (void *cls,
5067 const struct GNUNET_PeerIdentity *peer,
5068 const struct GNUNET_MessageHeader *message)
5070 struct GNUNET_MESH_ConnectionCreate *msg;
5071 struct GNUNET_PeerIdentity *id;
5072 struct GNUNET_HashCode *cid;
5073 struct MeshPeerPath *path;
5074 struct MeshPeer *dest_peer;
5075 struct MeshPeer *orig_peer;
5076 struct MeshConnection *c;
5077 unsigned int own_pos;
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5084 size = ntohs (message->size);
5085 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5087 GNUNET_break_op (0);
5091 /* Calculate hops */
5092 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5093 if (size % sizeof (struct GNUNET_PeerIdentity))
5095 GNUNET_break_op (0);
5098 size /= sizeof (struct GNUNET_PeerIdentity);
5101 GNUNET_break_op (0);
5104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5106 /* Get parameters */
5107 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5109 id = (struct GNUNET_PeerIdentity *) &msg[1];
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5111 " connection %s (%s).\n",
5112 GNUNET_h2s (cid), GNUNET_i2s (id));
5114 /* Create connection */
5115 c = connection_get (cid);
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5119 c = connection_new (cid);
5123 connection_reset_timeout (c, GNUNET_YES);
5124 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5126 /* Remember peers */
5127 dest_peer = peer_get (&id[size - 1]);
5128 orig_peer = peer_get (&id[0]);
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5132 path = path_new (size);
5134 for (i = 0; i < size; i++)
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5137 GNUNET_i2s (&id[i]));
5138 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5139 if (path->peers[i] == myid)
5142 if (own_pos == 0 && path->peers[own_pos] != myid)
5144 /* create path: self not found in path through self */
5145 GNUNET_break_op (0);
5146 path_destroy (path);
5147 connection_destroy (c);
5150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5151 path_add_to_peers (path, GNUNET_NO);
5152 c->path = path_duplicate (path);
5153 c->own_pos = own_pos;
5155 /* Is it a connection to us? */
5156 if (own_pos == size - 1)
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5159 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5161 if (NULL == orig_peer->tunnel)
5162 orig_peer->tunnel = tunnel_new ();
5163 tunnel_add_connection (orig_peer->tunnel, c);
5165 send_connection_ack (c, GNUNET_NO);
5167 /* Keep tunnel alive in direction dest->owner*/
5168 connection_reset_timeout (c, GNUNET_NO);
5172 /* It's for somebody else! Retransmit. */
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5174 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5175 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5176 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5183 * Core handler for path ACKs
5185 * @param cls closure
5186 * @param message message
5187 * @param peer peer identity this notification is about
5189 * @return GNUNET_OK to keep the connection open,
5190 * GNUNET_SYSERR to close it (signal serious error)
5193 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5194 const struct GNUNET_MessageHeader *message)
5196 struct GNUNET_MESH_ConnectionACK *msg;
5197 struct MeshPeerPath *p;
5198 struct MeshConnection *c;
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5201 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5203 GNUNET_h2s (&msg->cid));
5204 c = connection_get (&msg->cid);
5207 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5216 /* Add path to peers? */
5220 path_add_to_peers (p, GNUNET_YES);
5226 connection_change_state (c, MESH_CONNECTION_READY);
5227 connection_reset_timeout (c, GNUNET_NO);
5228 if (MESH_TUNNEL_READY != c->t->state)
5229 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5230 tunnel_send_queued_data (c->t, GNUNET_YES);
5232 /* Message for us? */
5233 if (connection_is_terminal (c, GNUNET_NO))
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5236 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5238 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5239 c->t->peer->dhtget = NULL;
5241 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5246 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5252 * Core handler for notifications of broken paths
5254 * @param cls Closure (unused).
5255 * @param peer Peer identity of sending neighbor.
5256 * @param message Message.
5258 * @return GNUNET_OK to keep the connection open,
5259 * GNUNET_SYSERR to close it (signal serious error)
5262 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5263 const struct GNUNET_MessageHeader *message)
5265 struct GNUNET_MESH_ConnectionBroken *msg;
5266 struct MeshConnection *c;
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5269 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5270 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5272 GNUNET_i2s (&msg->peer1));
5273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5274 GNUNET_i2s (&msg->peer2));
5275 c = connection_get (&msg->cid);
5278 GNUNET_break_op (0);
5281 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5282 GNUNET_PEER_search (&msg->peer2));
5289 * Core handler for tunnel destruction
5291 * @param cls Closure (unused).
5292 * @param peer Peer identity of sending neighbor.
5293 * @param message Message.
5295 * @return GNUNET_OK to keep the connection open,
5296 * GNUNET_SYSERR to close it (signal serious error)
5299 handle_mesh_connection_destroy (void *cls,
5300 const struct GNUNET_PeerIdentity *peer,
5301 const struct GNUNET_MessageHeader *message)
5303 struct GNUNET_MESH_ConnectionDestroy *msg;
5304 struct MeshConnection *c;
5308 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5310 "Got a CONNECTION DESTROY message from %s\n",
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5313 " for connection %s\n",
5314 GNUNET_h2s (&msg->cid));
5315 c = connection_get (&msg->cid);
5318 /* Probably already got the message from another path,
5319 * destroyed the tunnel and retransmitted to children.
5322 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5326 id = GNUNET_PEER_search (peer);
5327 if (id == connection_get_prev_hop (c)->id)
5329 else if (id == connection_get_next_hop (c)->id)
5333 GNUNET_break_op (0);
5336 send_prebuilt_message_connection (message, c, NULL, fwd);
5337 c->destroy = GNUNET_YES;
5344 * Handler for channel create messages.
5346 * @param t Tunnel this channel is to be created in.
5347 * @param msg Message.
5348 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5350 * @return GNUNET_OK to keep the connection open,
5351 * GNUNET_SYSERR to close it (signal serious error)
5354 handle_channel_create (struct MeshTunnel2 *t,
5355 struct GNUNET_MESH_ChannelCreate *msg,
5358 MESH_ChannelNumber chid;
5359 struct MeshChannel *ch;
5360 struct MeshClient *c;
5363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5364 /* Check message size */
5365 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5367 GNUNET_break_op (0);
5371 /* Check if channel exists */
5372 chid = ntohl (msg->chid);
5373 ch = channel_get (t, chid);
5376 /* Probably a retransmission, safe to ignore */
5377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5381 /* Create channel */
5382 ch = channel_new (t, NULL, 0);
5383 channel_set_options (ch, ntohl (msg->opt));
5386 /* Find a destination client */
5387 port = ntohl (msg->port);
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5389 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5392 /* TODO send reject */
5393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5397 channel_add_client (ch, c);
5398 if (GNUNET_YES == ch->reliable)
5400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5401 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5402 ch->bck_rel->ch = ch;
5403 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5406 send_local_channel_create (ch);
5407 channel_send_ack (ch, !fwd);
5414 * Handler for channel ack messages.
5416 * @param t Tunnel this channel is to be created in.
5417 * @param msg Message.
5418 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5420 * @return GNUNET_OK to keep the connection open,
5421 * GNUNET_SYSERR to close it (signal serious error)
5424 handle_channel_ack (struct MeshTunnel2 *t,
5425 struct GNUNET_MESH_ChannelManage *msg,
5428 MESH_ChannelNumber chid;
5429 struct MeshChannel *ch;
5431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5432 /* Check message size */
5433 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5435 GNUNET_break_op (0);
5439 /* Check if channel exists */
5440 chid = ntohl (msg->chid);
5441 ch = channel_get (t, chid);
5444 GNUNET_break_op (0);
5445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5449 channel_confirm (ch, !fwd);
5455 * Handler for channel destroy messages.
5457 * @param t Tunnel this channel is to be destroyed of.
5458 * @param msg Message.
5459 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5461 * @return GNUNET_OK to keep the connection open,
5462 * GNUNET_SYSERR to close it (signal serious error)
5465 handle_channel_destroy (struct MeshTunnel2 *t,
5466 struct GNUNET_MESH_ChannelManage *msg,
5469 MESH_ChannelNumber chid;
5470 struct MeshChannel *ch;
5472 /* Check message size */
5473 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5475 GNUNET_break_op (0);
5479 /* Check if channel exists */
5480 chid = ntohl (msg->chid);
5481 ch = channel_get (t, chid);
5484 /* Probably a retransmission, safe to ignore */
5488 send_local_channel_destroy (ch, fwd);
5489 channel_destroy (ch);
5496 * Generic handler for mesh network encrypted traffic.
5498 * @param peer Peer identity this notification is about.
5499 * @param message Encrypted message.
5500 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5502 * @return GNUNET_OK to keep the connection open,
5503 * GNUNET_SYSERR to close it (signal serious error)
5506 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5507 const struct GNUNET_MESH_Encrypted *msg,
5510 struct MeshConnection *c;
5511 struct MeshTunnel2 *t;
5512 struct MeshPeer *neighbor;
5513 struct MeshFlowControl *fc;
5520 size = ntohs (msg->header.size);
5522 sizeof (struct GNUNET_MESH_Encrypted) +
5523 sizeof (struct GNUNET_MessageHeader))
5528 type = ntohs (msg->header.type);
5529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5530 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5532 /* Check connection */
5533 c = connection_get (&msg->cid);
5536 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5541 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5543 /* Check if origin is as expected */
5544 neighbor = connection_get_hop (c, fwd);
5545 if (peer_get (peer)->id != neighbor->id)
5547 GNUNET_break_op (0);
5552 pid = ntohl (msg->pid);
5553 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5555 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5557 "WARNING Received PID %u, (prev %u), ACK %u\n",
5558 pid, fc->last_pid_recv, fc->last_ack_sent);
5561 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5563 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5565 " Pid %u not expected (%u+), dropping!\n",
5566 pid, fc->last_pid_recv + 1);
5569 if (MESH_CONNECTION_SENT == c->state)
5570 connection_change_state (c, MESH_CONNECTION_READY);
5571 connection_reset_timeout (c, fwd);
5572 fc->last_pid_recv = pid;
5574 /* Is this message for us? */
5575 if (connection_is_terminal (c, fwd))
5577 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5579 struct GNUNET_MessageHeader *msgh;
5582 /* TODO signature verification */
5583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5584 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5586 fc->last_pid_recv = pid;
5587 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5588 msgh = (struct GNUNET_MessageHeader *) cbuf;
5589 switch (ntohs (msgh->type))
5591 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5592 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5595 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5596 r = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5599 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5600 r = handle_channel_create (t,
5601 (struct GNUNET_MESH_ChannelCreate *) msgh,
5605 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5606 r = handle_channel_ack (t,
5607 (struct GNUNET_MESH_ChannelManage *) msgh,
5611 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5612 r = handle_channel_destroy (t,
5613 (struct GNUNET_MESH_ChannelManage *) msgh,
5618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5619 "end-to-end message not known (%u)\n",
5620 ntohs (msgh->type));
5624 connection_send_ack (c, fwd);
5628 /* Message not for us: forward to next hop */
5629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5630 ttl = ntohl (msg->ttl);
5631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5634 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5636 connection_send_ack (c, fwd);
5639 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5641 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5648 * Core handler for mesh network traffic going orig->dest.
5650 * @param cls Closure (unused).
5651 * @param message Message received.
5652 * @param peer Peer who sent the message.
5654 * @return GNUNET_OK to keep the connection open,
5655 * GNUNET_SYSERR to close it (signal serious error)
5658 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5659 const struct GNUNET_MessageHeader *message)
5661 return handle_mesh_encrypted (peer,
5662 (struct GNUNET_MESH_Encrypted *)message,
5667 * Core handler for mesh network traffic going dest->orig.
5669 * @param cls Closure (unused).
5670 * @param message Message received.
5671 * @param peer Peer who sent the message.
5673 * @return GNUNET_OK to keep the connection open,
5674 * GNUNET_SYSERR to close it (signal serious error)
5677 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5678 const struct GNUNET_MessageHeader *message)
5680 return handle_mesh_encrypted (peer,
5681 (struct GNUNET_MESH_Encrypted *)message,
5687 * Core handler for mesh network traffic point-to-point acks.
5689 * @param cls closure
5690 * @param message message
5691 * @param peer peer identity this notification is about
5693 * @return GNUNET_OK to keep the connection open,
5694 * GNUNET_SYSERR to close it (signal serious error)
5697 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5698 const struct GNUNET_MessageHeader *message)
5700 struct GNUNET_MESH_ACK *msg;
5701 struct MeshConnection *c;
5702 struct MeshFlowControl *fc;
5706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5708 msg = (struct GNUNET_MESH_ACK *) message;
5710 c = connection_get (&msg->cid);
5714 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5719 ack = ntohl (msg->ack);
5720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5722 /* Is this a forward or backward ACK? */
5723 id = GNUNET_PEER_search (peer);
5724 if (connection_get_next_hop (c)->id == id)
5726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5729 else if (connection_get_prev_hop (c)->id == id)
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5736 GNUNET_break_op (0);
5740 /* Cancel polling if the ACK is bigger than before. */
5741 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5742 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5745 GNUNET_SCHEDULER_cancel (fc->poll_task);
5746 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5747 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5750 fc->last_ack_recv = ack;
5751 connection_unlock_queue (c, fc == &c->fwd_fc);
5758 * Core handler for mesh network traffic point-to-point ack polls.
5760 * @param cls closure
5761 * @param message message
5762 * @param peer peer identity this notification is about
5764 * @return GNUNET_OK to keep the connection open,
5765 * GNUNET_SYSERR to close it (signal serious error)
5768 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5769 const struct GNUNET_MessageHeader *message)
5771 struct GNUNET_MESH_Poll *msg;
5772 struct MeshConnection *c;
5773 struct MeshFlowControl *fc;
5777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5780 msg = (struct GNUNET_MESH_Poll *) message;
5782 c = connection_get (&msg->cid);
5786 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5788 GNUNET_break_op (0);
5792 /* Is this a forward or backward ACK? */
5793 id = GNUNET_PEER_search (peer);
5794 if (connection_get_next_hop (c)->id == id)
5796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5799 else if (connection_get_prev_hop (c)->id == id)
5801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5806 GNUNET_break_op (0);
5810 pid = ntohl (msg->pid);
5811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5812 pid, fc->last_pid_recv);
5813 fc->last_pid_recv = pid;
5814 connection_send_ack (c, fc == &c->fwd_fc);
5821 * Core handler for mesh keepalives.
5823 * @param cls closure
5824 * @param message message
5825 * @param peer peer identity this notification is about
5826 * @return GNUNET_OK to keep the connection open,
5827 * GNUNET_SYSERR to close it (signal serious error)
5829 * TODO: Check who we got this from, to validate route.
5832 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5833 const struct GNUNET_MessageHeader *message)
5835 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5836 struct MeshConnection *c;
5837 struct MeshPeer *neighbor;
5840 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5844 c = connection_get (&msg->cid);
5847 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5852 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5853 GNUNET_YES : GNUNET_NO;
5855 /* Check if origin is as expected */
5856 neighbor = connection_get_hop (c, fwd);
5857 if (peer_get (peer)->id != neighbor->id)
5859 GNUNET_break_op (0);
5863 connection_change_state (c, MESH_CONNECTION_READY);
5864 connection_reset_timeout (c, fwd);
5866 if (connection_is_terminal (c, fwd))
5869 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5870 send_prebuilt_message_connection (message, c, NULL, fwd);
5878 * Functions to handle messages from core
5880 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5881 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5883 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5884 sizeof (struct GNUNET_MESH_ConnectionACK)},
5885 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5886 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5887 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5888 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5889 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5890 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5891 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5892 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5893 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5894 sizeof (struct GNUNET_MESH_ACK)},
5895 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5896 sizeof (struct GNUNET_MESH_Poll)},
5897 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5898 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5904 * Function to process paths received for a new peer addition. The recorded
5905 * paths form the initial tunnel, which can be optimized later.
5906 * Called on each result obtained for the DHT search.
5908 * @param cls closure
5909 * @param exp when will this value expire
5910 * @param key key of the result
5911 * @param get_path path of the get request
5912 * @param get_path_length lenght of get_path
5913 * @param put_path path of the put request
5914 * @param put_path_length length of the put_path
5915 * @param type type of the result
5916 * @param size number of bytes in data
5917 * @param data pointer to the result data
5920 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5921 const struct GNUNET_HashCode * key,
5922 const struct GNUNET_PeerIdentity *get_path,
5923 unsigned int get_path_length,
5924 const struct GNUNET_PeerIdentity *put_path,
5925 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5926 size_t size, const void *data)
5928 struct MeshPeer *peer = cls;
5929 struct MeshPeerPath *p;
5930 struct MeshConnection *c;
5931 struct GNUNET_PeerIdentity pi;
5934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5935 GNUNET_PEER_resolve (peer->id, &pi);
5936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5938 p = path_build_from_dht (get_path, get_path_length,
5939 put_path, put_path_length);
5940 path_add_to_peers (p, GNUNET_NO);
5943 /* Count connections */
5944 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5946 /* If we already have 3 (or more (?!)) connections, it's enough */
5950 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5953 peer_connect (peer);
5959 /******************************************************************************/
5960 /********************* MESH LOCAL HANDLES **************************/
5961 /******************************************************************************/
5965 * Handler for client connection.
5967 * @param cls Closure (unused).
5968 * @param client Client handler.
5971 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5973 struct MeshClient *c;
5977 c = GNUNET_malloc (sizeof (struct MeshClient));
5979 c->id = next_client_id++; /* overflow not important: just for debug */
5980 GNUNET_SERVER_client_keep (client);
5981 GNUNET_SERVER_client_set_user_context (client, c);
5982 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5987 * Handler for client disconnection
5989 * @param cls closure
5990 * @param client identification of the client; NULL
5991 * for the last call when the server is destroyed
5994 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5996 struct MeshClient *c;
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6005 c = client_get (client);
6008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6010 GNUNET_SERVER_client_drop (c->handle);
6011 c->shutting_down = GNUNET_YES;
6012 if (NULL != c->own_channels)
6014 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6015 &channel_destroy_iterator, c);
6016 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6019 if (NULL != c->incoming_channels)
6021 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6022 &channel_destroy_iterator, c);
6023 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6026 if (NULL != c->ports)
6028 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6029 &client_release_ports, c);
6030 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6032 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6033 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6047 * Handler for new clients
6049 * @param cls closure
6050 * @param client identification of the client
6051 * @param message the actual message, which includes messages the client wants
6054 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6055 const struct GNUNET_MessageHeader *message)
6057 struct GNUNET_MESH_ClientConnect *cc_msg;
6058 struct MeshClient *c;
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6065 /* Check data sanity */
6066 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6067 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6068 if (0 != (size % sizeof (uint32_t)))
6071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6074 size /= sizeof (uint32_t);
6076 /* Initialize new client structure */
6077 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6084 p = (uint32_t *) &cc_msg[1];
6085 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6086 for (i = 0; i < size; i++)
6089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6091 /* store in client's hashmap */
6092 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6093 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6094 /* store in global hashmap */
6095 /* FIXME only allow one client to have the port open,
6096 * have a backup hashmap with waiting clients */
6097 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6098 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6102 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6103 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6104 GNUNET_SERVER_notification_context_add (nc, client);
6105 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6107 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6113 * Handler for requests of new tunnels
6115 * @param cls Closure.
6116 * @param client Identification of the client.
6117 * @param message The actual message.
6120 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6121 const struct GNUNET_MessageHeader *message)
6123 struct GNUNET_MESH_ChannelMessage *msg;
6124 struct MeshPeer *peer;
6125 struct MeshTunnel2 *t;
6126 struct MeshChannel *ch;
6127 struct MeshClient *c;
6128 MESH_ChannelNumber chid;
6130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6132 /* Sanity check for client registration */
6133 if (NULL == (c = client_get (client)))
6136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6141 /* Message size sanity check */
6142 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6149 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6151 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6152 chid = ntohl (msg->channel_id);
6154 /* Sanity check for duplicate channel IDs */
6155 if (NULL != channel_get_by_local_id (c, chid))
6158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6162 peer = peer_get (&msg->peer);
6163 if (NULL == peer->tunnel)
6165 peer->tunnel = tunnel_new ();
6166 peer->tunnel->peer = peer;
6170 /* Create channel */
6171 ch = channel_new (t, c, chid);
6175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6178 ch->port = ntohl (msg->port);
6179 channel_set_options (ch, ntohl (msg->opt));
6181 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6182 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
6183 ch->fwd_rel->ch = ch;
6184 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
6186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6187 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6188 peer_connect (peer);
6190 /* Send create channel */
6192 struct GNUNET_MESH_ChannelCreate msgcc;
6194 msgcc.header.size = htons (sizeof (msgcc));
6195 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6196 msgcc.chid = htonl (ch->gid);
6197 msgcc.port = msg->port;
6198 msgcc.opt = msg->opt;
6200 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6203 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6209 * Handler for requests of deleting tunnels
6211 * @param cls closure
6212 * @param client identification of the client
6213 * @param message the actual message
6216 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6217 const struct GNUNET_MessageHeader *message)
6219 struct GNUNET_MESH_ChannelMessage *msg;
6220 struct MeshClient *c;
6221 struct MeshChannel *ch;
6222 struct MeshTunnel2 *t;
6223 MESH_ChannelNumber chid;
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6226 "Got a DESTROY CHANNEL from client!\n");
6228 /* Sanity check for client registration */
6229 if (NULL == (c = client_get (client)))
6232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6237 /* Message sanity check */
6238 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6245 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6247 /* Retrieve tunnel */
6248 chid = ntohl (msg->channel_id);
6249 ch = channel_get_by_local_id (c, chid);
6252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6258 /* Cleanup after the tunnel */
6259 client_delete_channel (c, ch);
6260 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6264 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6271 " channel %X client %p (%p, %p)\n",
6272 chid, c, ch->root, ch->dest);
6277 channel_destroy (ch);
6278 tunnel_destroy_if_empty (t);
6280 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6286 * Handler for client traffic
6288 * @param cls closure
6289 * @param client identification of the client
6290 * @param message the actual message
6293 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6294 const struct GNUNET_MessageHeader *message)
6296 struct GNUNET_MESH_LocalData *msg;
6297 struct MeshClient *c;
6298 struct MeshChannel *ch;
6299 struct MeshChannelReliability *rel;
6300 MESH_ChannelNumber chid;
6304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6305 "Got data from a client!\n");
6307 /* Sanity check for client registration */
6308 if (NULL == (c = client_get (client)))
6311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6316 msg = (struct GNUNET_MESH_LocalData *) message;
6318 /* Sanity check for message size */
6319 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6320 if (size < sizeof (struct GNUNET_MessageHeader))
6323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6327 /* Channel exists? */
6328 chid = ntohl (msg->id);
6329 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6330 ch = channel_get_by_local_id (c, chid);
6334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6338 /* Is the client in the channel? */
6341 ch->root->handle == client)
6345 ch->dest->handle == client) ) )
6348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6352 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6353 rel->client_ready = GNUNET_NO;
6355 /* Ok, everything is correct, send the message. */
6357 struct GNUNET_MESH_Data *payload;
6358 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6359 unsigned char cbuf[p2p_size];
6362 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6363 payload = (struct GNUNET_MESH_Data *) cbuf;
6364 payload->mid = htonl (*mid);
6366 memcpy (&payload[1], &msg[1], size);
6367 payload->header.size = htons (p2p_size);
6368 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6369 payload->chid = htonl (ch->gid);
6370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6371 send_prebuilt_message_channel (&payload->header, ch, fwd);
6373 if (GNUNET_YES == ch->reliable)
6374 channel_save_copy (ch, &payload->header, fwd);
6376 if (tunnel_get_buffer (ch->t, fwd) > 0)
6377 send_local_ack (ch, fwd);
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6386 * Handler for client's ACKs for payload traffic.
6388 * @param cls Closure (unused).
6389 * @param client Identification of the client.
6390 * @param message The actual message.
6393 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6394 const struct GNUNET_MessageHeader *message)
6396 struct GNUNET_MESH_LocalAck *msg;
6397 struct MeshChannelReliability *rel;
6398 struct MeshChannel *ch;
6399 struct MeshClient *c;
6400 MESH_ChannelNumber chid;
6403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6405 /* Sanity check for client registration */
6406 if (NULL == (c = client_get (client)))
6409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6414 msg = (struct GNUNET_MESH_LocalAck *) message;
6416 /* Channel exists? */
6417 chid = ntohl (msg->channel_id);
6418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6419 ch = channel_get_by_local_id (c, chid);
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
6424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6430 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6431 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6433 rel->client_ready = GNUNET_YES;
6434 channel_send_client_buffered_data (ch, c, rel);
6435 channel_send_connection_ack (ch, 64 - rel->n_recv, fwd);
6437 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6444 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6446 * @param cls Closure (client handle).
6447 * @param key Key (hashed tunnel ID, unused).
6448 * @param value Tunnel info.
6450 * @return GNUNET_YES, to keep iterating.
6453 // monitor_all_tunnels_iterator (void *cls,
6454 // const struct GNUNET_HashCode * key,
6457 // struct GNUNET_SERVER_Client *client = cls;
6458 // struct MeshChannel *ch = value;
6459 // struct GNUNET_MESH_LocalMonitor *msg;
6461 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6462 // msg->channel_id = htonl (ch->gid);
6463 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6464 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6466 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6467 // "* sending info about tunnel %s\n",
6468 // GNUNET_i2s (&msg->owner));
6470 // GNUNET_SERVER_notification_context_unicast (nc, client,
6471 // &msg->header, GNUNET_NO);
6472 // return GNUNET_YES;
6477 * Handler for client's MONITOR request.
6479 * @param cls Closure (unused).
6480 * @param client Identification of the client.
6481 * @param message The actual message.
6484 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6485 const struct GNUNET_MessageHeader *message)
6487 struct MeshClient *c;
6489 /* Sanity check for client registration */
6490 if (NULL == (c = client_get (client)))
6493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6498 "Received get tunnels request from client %u\n",
6500 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6501 // monitor_all_tunnels_iterator,
6503 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6504 "Get tunnels request from client %u completed\n",
6506 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6511 * Handler for client's MONITOR_TUNNEL request.
6513 * @param cls Closure (unused).
6514 * @param client Identification of the client.
6515 * @param message The actual message.
6518 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6519 const struct GNUNET_MessageHeader *message)
6521 const struct GNUNET_MESH_LocalMonitor *msg;
6522 struct GNUNET_MESH_LocalMonitor *resp;
6523 struct MeshClient *c;
6524 struct MeshChannel *ch;
6526 /* Sanity check for client registration */
6527 if (NULL == (c = client_get (client)))
6530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6534 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6536 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6539 ntohl (msg->channel_id));
6540 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6544 /* We don't know the tunnel */
6545 struct GNUNET_MESH_LocalMonitor warn;
6548 GNUNET_SERVER_notification_context_unicast (nc, client,
6551 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6555 /* Initialize context */
6556 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6558 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6559 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6560 &resp->header, GNUNET_NO);
6563 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6564 "Monitor tunnel request from client %u completed\n",
6566 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6571 * Functions to handle messages from clients
6573 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6574 {&handle_local_new_client, NULL,
6575 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6576 {&handle_local_channel_create, NULL,
6577 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6578 sizeof (struct GNUNET_MESH_ChannelMessage)},
6579 {&handle_local_channel_destroy, NULL,
6580 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6581 sizeof (struct GNUNET_MESH_ChannelMessage)},
6582 {&handle_local_data, NULL,
6583 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6584 {&handle_local_ack, NULL,
6585 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6586 sizeof (struct GNUNET_MESH_LocalAck)},
6587 {&handle_local_get_tunnels, NULL,
6588 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6589 sizeof (struct GNUNET_MessageHeader)},
6590 {&handle_local_show_tunnel, NULL,
6591 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6592 sizeof (struct GNUNET_MESH_LocalMonitor)},
6598 * Method called whenever a given peer connects.
6600 * @param cls closure
6601 * @param peer peer identity this notification is about
6604 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6606 struct MeshPeer *pi;
6607 struct MeshPeerPath *path;
6609 DEBUG_CONN ("Peer connected\n");
6610 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6611 pi = peer_get (peer);
6614 DEBUG_CONN (" (self)\n");
6615 path = path_new (1);
6619 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6620 path = path_new (2);
6621 path->peers[1] = pi->id;
6622 GNUNET_PEER_change_rc (pi->id, 1);
6623 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6625 path->peers[0] = myid;
6626 GNUNET_PEER_change_rc (myid, 1);
6627 peer_add_path (pi, path, GNUNET_YES);
6629 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6635 * Method called whenever a peer disconnects.
6637 * @param cls closure
6638 * @param peer peer identity this notification is about
6641 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6643 struct MeshPeer *pi;
6645 DEBUG_CONN ("Peer disconnected\n");
6646 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6653 peer_remove_path (pi, myid, pi->id);
6655 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6658 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6659 pi->connections = NULL;
6662 DEBUG_CONN (" (self)\n");
6664 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6671 * Install server (service) handlers and start listening to clients.
6676 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6677 GNUNET_SERVER_connect_notify (server_handle,
6678 &handle_local_client_connect, NULL);
6679 GNUNET_SERVER_disconnect_notify (server_handle,
6680 &handle_local_client_disconnect, NULL);
6681 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6683 clients_head = NULL;
6684 clients_tail = NULL;
6686 GNUNET_SERVER_resume (server_handle);
6691 * To be called on core init/fail.
6693 * @param cls Closure (config)
6694 * @param identity the public identity of this peer
6697 core_init (void *cls,
6698 const struct GNUNET_PeerIdentity *identity)
6700 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6704 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6709 GNUNET_i2s (identity));
6710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6712 GNUNET_i2s (&my_full_id));
6713 GNUNET_CORE_disconnect (core_handle);
6714 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6715 NULL, /* Closure passed to MESH functions */
6716 &core_init, /* Call core_init once connected */
6717 &core_connect, /* Handle connects */
6718 &core_disconnect, /* remove peers on disconnects */
6719 NULL, /* Don't notify about all incoming messages */
6720 GNUNET_NO, /* For header only in notification */
6721 NULL, /* Don't notify about all outbound messages */
6722 GNUNET_NO, /* For header-only out notification */
6723 core_handlers); /* Register these handlers */
6732 /******************************************************************************/
6733 /************************ MAIN FUNCTIONS ****************************/
6734 /******************************************************************************/
6737 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6739 * @param cls closure
6740 * @param key current key code
6741 * @param value value in the hash map
6742 * @return GNUNET_YES if we should continue to iterate,
6746 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6748 struct MeshPeer *p = value;
6749 struct MeshTunnel2 *t = p->tunnel;
6758 * Task run during shutdown.
6764 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6768 if (core_handle != NULL)
6770 GNUNET_CORE_disconnect (core_handle);
6773 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6774 if (dht_handle != NULL)
6776 GNUNET_DHT_disconnect (dht_handle);
6781 GNUNET_SERVER_notification_context_destroy (nc);
6784 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6786 GNUNET_SCHEDULER_cancel (announce_id_task);
6787 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6794 * Process mesh requests.
6796 * @param cls closure
6797 * @param server the initialized server
6798 * @param c configuration to use
6801 run (void *cls, struct GNUNET_SERVER_Handle *server,
6802 const struct GNUNET_CONFIGURATION_Handle *c)
6805 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6808 server_handle = server;
6809 GNUNET_SERVER_suspend (server_handle);
6812 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6815 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6817 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6818 "mesh", "peer/privatekey");
6819 GNUNET_SCHEDULER_shutdown ();
6824 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6825 &refresh_connection_time))
6827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6829 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6830 "mesh", "refresh path time");
6831 GNUNET_SCHEDULER_shutdown ();
6836 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6841 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6842 "mesh", "id announce time");
6843 GNUNET_SCHEDULER_shutdown ();
6848 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6853 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6854 "mesh", "connect timeout");
6855 GNUNET_SCHEDULER_shutdown ();
6860 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6865 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6866 "mesh", "max msgs queue");
6867 GNUNET_SCHEDULER_shutdown ();
6872 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6875 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6877 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6878 "mesh", "max tunnels");
6879 GNUNET_SCHEDULER_shutdown ();
6884 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6889 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6890 "mesh", "default ttl", 64);
6895 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6899 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6900 "mesh", "max peers", 1000);
6905 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6913 "Mesh is running with drop mode enabled. "
6914 "This is NOT a good idea! "
6915 "Remove the DROP_PERCENT option from your configuration.\n");
6919 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6920 &dht_replication_level))
6922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6924 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6925 "mesh", "dht replication level", 3);
6926 dht_replication_level = 3;
6929 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6930 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6931 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6933 dht_handle = GNUNET_DHT_connect (c, 64);
6934 if (NULL == dht_handle)
6938 stats = GNUNET_STATISTICS_create ("mesh", c);
6940 /* Scheduled the task to clean up when shutdown is called */
6941 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6943 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6944 GNUNET_free (keyfile);
6945 GNUNET_assert (NULL != pk);
6946 my_private_key = pk;
6947 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6948 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6949 &my_full_id.hashPubKey);
6950 myid = GNUNET_PEER_intern (&my_full_id);
6951 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6952 "Mesh for peer [%s] starting\n",
6953 GNUNET_i2s(&my_full_id));
6955 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6956 NULL, /* Closure passed to MESH functions */
6957 &core_init, /* Call core_init once connected */
6958 &core_connect, /* Handle connects */
6959 &core_disconnect, /* remove peers on disconnects */
6960 NULL, /* Don't notify about all incoming messages */
6961 GNUNET_NO, /* For header only in notification */
6962 NULL, /* Don't notify about all outbound messages */
6963 GNUNET_NO, /* For header-only out notification */
6964 core_handlers); /* Register these handlers */
6965 if (NULL == core_handle)
6968 GNUNET_SCHEDULER_shutdown ();
6971 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6977 * The main function for the mesh service.
6979 * @param argc number of arguments from the command line
6980 * @param argv command line arguments
6981 * @return 0 ok, 1 on error
6984 main (int argc, char *const *argv)
6989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6990 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6992 ret = (GNUNET_OK == r) ? 0 : 1;
6993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");