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;
568 * Is the root prevented from sending more data? (We "owe" him an ACK).
573 * Is the dest prevented from sending more data? (We "owe" him an ACK).
580 * Struct containing all information regarding a connection to a peer.
582 struct MeshConnection
587 struct MeshConnection *next;
588 struct MeshConnection *prev;
591 * Tunnel this connection is part of.
593 struct MeshTunnel2 *t;
596 * Flow control information for traffic fwd.
598 struct MeshFlowControl fwd_fc;
601 * Flow control information for traffic bck.
603 struct MeshFlowControl bck_fc;
606 * ID of the connection.
608 struct GNUNET_HashCode id;
611 * State of the connection.
613 enum MeshConnectionState state;
616 * Path being used for the tunnel.
618 struct MeshPeerPath *path;
621 * Position of the local peer in the path.
623 unsigned int own_pos;
626 * Task to keep the used paths alive at the owner,
627 * time tunnel out on all the other peers.
629 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
632 * Task to keep the used paths alive at the destination,
633 * time tunnel out on all the other peers.
635 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
638 * Pending message count.
640 int pending_messages;
643 * Destroy flag: if true, destroy on last message.
650 * Struct used to queue messages in a tunnel.
652 struct MeshTunnelQueue
657 struct MeshTunnelQueue *next;
658 struct MeshTunnelQueue *prev;
663 struct MeshChannel *ch;
668 /* struct GNUNET_MessageHeader *msg; */
673 * Struct containing all information regarding a tunnel to a peer.
678 * Endpoint of the tunnel.
680 struct MeshPeer *peer;
683 * State of the tunnel.
685 enum MeshTunnelState state;
688 * Local peer ephemeral private key
690 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
693 * Local peer ephemeral public key
695 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
698 * Remote peer's public key.
700 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
703 * Encryption ("our") key.
705 struct GNUNET_CRYPTO_AesSessionKey e_key;
708 * Decryption ("their") key.
710 struct GNUNET_CRYPTO_AesSessionKey d_key;
713 * Paths that are actively used to reach the destination peer.
715 struct MeshConnection *connection_head;
716 struct MeshConnection *connection_tail;
719 * Next connection number.
724 * Channels inside this tunnel.
726 struct MeshChannel *channel_head;
727 struct MeshChannel *channel_tail;
730 * Channel ID for the next created channel.
732 MESH_ChannelNumber next_chid;
735 * Channel ID for the next incoming channel.
737 MESH_ChannelNumber next_local_chid;
740 * Pending message count.
742 int pending_messages;
745 * Destroy flag: if true, destroy on last message.
750 * Queued messages, to transmit once tunnel gets connected.
752 struct MeshTunnelQueue *tq_head;
753 struct MeshTunnelQueue *tq_tail;
759 * Struct containing information about a client of the service
761 * TODO: add a list of 'waiting' ports
768 struct MeshClient *next;
773 struct MeshClient *prev;
776 * Tunnels that belong to this client, indexed by local id
778 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
781 * Tunnels this client has accepted, indexed by incoming local id
783 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
786 * Handle to communicate with the client
788 struct GNUNET_SERVER_Client *handle;
791 * Ports that this client has declared interest in.
792 * Indexed by port, contains *Client.
794 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
797 * Whether the client is active or shutting down (don't send confirmations
798 * to a client that is shutting down.
803 * ID of the client, mainly for debug messages
809 /******************************************************************************/
810 /************************ DEBUG FUNCTIONS ****************************/
811 /******************************************************************************/
815 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
816 * @param cls string to print
817 * @param success GNUNET_OK if the PUT was transmitted,
818 * GNUNET_NO on timeout,
819 * GNUNET_SYSERR on disconnect from service
820 * after the PUT message was transmitted
821 * (so we don't know if it was received or not)
826 mesh_debug (void *cls, int success)
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
836 /******************************************************************************/
837 /*********************** GLOBAL VARIABLES ****************************/
838 /******************************************************************************/
840 /************************** Configuration parameters **************************/
843 * How often to send path keepalives. Paths timeout after 4 missed.
845 static struct GNUNET_TIME_Relative refresh_connection_time;
848 * How often to PUT own ID in the DHT.
850 static struct GNUNET_TIME_Relative id_announce_time;
853 * Maximum time allowed to connect to a peer found by string.
855 static struct GNUNET_TIME_Relative connect_timeout;
858 * Default TTL for payload packets.
860 static unsigned long long default_ttl;
863 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
865 static unsigned long long dht_replication_level;
868 * How many connections are we willing to maintain.
869 * Local connections are always allowed, even if there are more connections than max.
871 static unsigned long long max_connections;
874 * How many messages *in total* are we willing to queue, divide by number of
875 * connections to get connection queue size.
877 static unsigned long long max_msgs_queue;
880 * How many peers do we want to remember?
882 static unsigned long long max_peers;
885 * Percentage of messages that will be dropped (for test purposes only).
887 static unsigned long long drop_percent;
889 /*************************** Static global variables **************************/
892 * DLL with all the clients, head.
894 static struct MeshClient *clients_head;
897 * DLL with all the clients, tail.
899 static struct MeshClient *clients_tail;
902 * Connections known, indexed by cid (MeshConnection).
904 static struct GNUNET_CONTAINER_MultiHashMap *connections;
907 * Peers known, indexed by PeerIdentity (MeshPeer).
909 static struct GNUNET_CONTAINER_MultiHashMap *peers;
912 * Handle to communicate with core.
914 static struct GNUNET_CORE_Handle *core_handle;
919 static struct GNUNET_DHT_Handle *dht_handle;
922 * Handle to server lib.
924 static struct GNUNET_SERVER_Handle *server_handle;
927 * Handle to the statistics service.
929 static struct GNUNET_STATISTICS_Handle *stats;
932 * Notification context, to send messages to local clients.
934 static struct GNUNET_SERVER_NotificationContext *nc;
937 * Local peer own ID (memory efficient handle).
939 static GNUNET_PEER_Id myid;
942 * Local peer own ID (full value).
944 static struct GNUNET_PeerIdentity my_full_id;
949 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
954 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
957 * All ports clients of this peer have opened.
959 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
962 * Task to periodically announce itself in the network.
964 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
967 * Next ID to assign to a client.
969 unsigned int next_client_id;
972 /******************************************************************************/
973 /*********************** DECLARATIONS **************************/
974 /******************************************************************************/
977 * Function to process paths received for a new peer addition. The recorded
978 * paths form the initial tunnel, which can be optimized later.
979 * Called on each result obtained for the DHT search.
982 * @param exp when will this value expire
983 * @param key key of the result
984 * @param type type of the result
985 * @param size number of bytes in data
986 * @param data pointer to the result data
989 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
990 const struct GNUNET_HashCode * key,
991 const struct GNUNET_PeerIdentity *get_path,
992 unsigned int get_path_length,
993 const struct GNUNET_PeerIdentity *put_path,
994 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
995 size_t size, const void *data);
999 * Retrieve the MeshPeer stucture associated with the peer, create one
1000 * and insert it in the appropriate structures if the peer is not known yet.
1002 * @param peer Full identity of the peer.
1004 * @return Existing or newly created peer info.
1006 static struct MeshPeer *
1007 peer_get (const struct GNUNET_PeerIdentity *peer);
1011 * Retrieve the MeshPeer stucture associated with the peer, create one
1012 * and insert it in the appropriate structures if the peer is not known yet.
1014 * @param peer Short identity of the peer.
1016 * @return Existing or newly created peer info.
1018 static struct MeshPeer *
1019 peer_get_short (const GNUNET_PEER_Id peer);
1023 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1024 * to obtain a local peer -> destination path and interning the peer ids.
1026 * @return Newly allocated and created path
1028 static struct MeshPeerPath *
1029 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1030 unsigned int get_path_length,
1031 const struct GNUNET_PeerIdentity *put_path,
1032 unsigned int put_path_length);
1036 * Adds a path to the data structs of all the peers in the path
1038 * @param p Path to process.
1039 * @param confirmed Whether we know if the path works or not.
1042 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1046 * Search for a tunnel by global ID using full PeerIdentities.
1048 * @param t Tunnel containing the channel.
1049 * @param chid Public channel number.
1051 * @return channel handler, NULL if doesn't exist
1053 static struct MeshChannel *
1054 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1058 * Change the tunnel state.
1060 * @param t Tunnel whose state to change.
1061 * @param state New state.
1064 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1068 * Notify a tunnel that a connection has broken that affects at least
1069 * some of its peers.
1071 * @param t Tunnel affected.
1072 * @param p1 Peer that got disconnected from p2.
1073 * @param p2 Peer that got disconnected from p1.
1075 * @return Short ID of the peer disconnected (either p1 or p2).
1076 * 0 if the tunnel remained unaffected.
1078 static GNUNET_PEER_Id
1079 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1080 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1083 * @brief Use the given path for the tunnel.
1084 * Update the next and prev hops (and RCs).
1085 * (Re)start the path refresh in case the tunnel is locally owned.
1087 * @param t Tunnel to update.
1088 * @param p Path to use.
1090 * @return Connection created.
1092 static struct MeshConnection *
1093 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1096 * Tunnel is empty: destroy it.
1098 * Notifies all participants (peers, cleints) about the destruction.
1100 * @param t Tunnel to destroy.
1103 tunnel_destroy_empty (struct MeshTunnel2 *t);
1106 * Destroy the tunnel.
1108 * This function does not generate any warning traffic to clients or peers.
1111 * Cancel messages belonging to this tunnel queued to neighbors.
1112 * Free any allocated resources linked to the tunnel.
1114 * @param t The tunnel to destroy.
1117 tunnel_destroy (struct MeshTunnel2 *t);
1120 * Create a connection.
1122 * @param cid Connection ID.
1124 static struct MeshConnection *
1125 connection_new (const struct GNUNET_HashCode *cid);
1128 * Connection is no longer needed: destroy it and remove from tunnel.
1130 * @param c Connection to destroy.
1133 connection_destroy (struct MeshConnection *c);
1136 * Send FWD keepalive packets for a connection.
1138 * @param cls Closure (connection for which to send the keepalive).
1139 * @param tc Notification context.
1142 connection_fwd_keepalive (void *cls,
1143 const struct GNUNET_SCHEDULER_TaskContext *tc);
1146 * Send BCK keepalive packets for a connection.
1148 * @param cls Closure (connection for which to send the keepalive).
1149 * @param tc Notification context.
1152 connection_bck_keepalive (void *cls,
1153 const struct GNUNET_SCHEDULER_TaskContext *tc);
1157 * Change the tunnel state.
1159 * @param c Connection whose state to change.
1160 * @param state New state.
1163 connection_change_state (struct MeshConnection* c,
1164 enum MeshConnectionState state);
1169 * @brief Queue and pass message to core when possible.
1171 * @param cls Closure (@c type dependant). It will be used by queue_send to
1172 * build the message to be sent if not already prebuilt.
1173 * @param type Type of the message, 0 for a raw message.
1174 * @param size Size of the message.
1175 * @param c Connection this message belongs to (cannot be NULL).
1176 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1177 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1180 queue_add (void* cls,
1183 struct MeshConnection* c,
1184 struct MeshChannel* ch,
1189 * Free a transmission that was already queued with all resources
1190 * associated to the request.
1192 * @param queue Queue handler to cancel.
1193 * @param clear_cls Is it necessary to free associated cls?
1196 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1200 * Core callback to write a queued packet to core buffer
1202 * @param cls Closure (peer info).
1203 * @param size Number of bytes available in buf.
1204 * @param buf Where the to write the message.
1206 * @return number of bytes written to buf
1209 queue_send (void *cls, size_t size, void *buf);
1213 * Dummy function to separate declarations from definitions in function list.
1216 __mesh_divider______________________________________________________________();
1220 * Get string description for tunnel state.
1222 * @param s Tunnel state.
1224 * @return String representation.
1227 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1229 static char buf[128];
1233 case MESH_TUNNEL_NEW:
1234 return "MESH_TUNNEL_NEW";
1235 case MESH_TUNNEL_SEARCHING:
1236 return "MESH_TUNNEL_SEARCHING";
1237 case MESH_TUNNEL_WAITING:
1238 return "MESH_TUNNEL_WAITING";
1239 case MESH_TUNNEL_READY:
1240 return "MESH_TUNNEL_READY";
1241 case MESH_TUNNEL_RECONNECTING:
1242 return "MESH_TUNNEL_RECONNECTING";
1245 sprintf (buf, "%u (UNKNOWN STATE)", s);
1252 * Get string description for tunnel state.
1254 * @param s Tunnel state.
1256 * @return String representation.
1259 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1263 case MESH_CONNECTION_NEW:
1264 return "MESH_CONNECTION_NEW";
1265 case MESH_CONNECTION_SENT:
1266 return "MESH_CONNECTION_SENT";
1267 case MESH_CONNECTION_READY:
1268 return "MESH_CONNECTION_READY";
1270 return "MESH_CONNECTION_STATE_ERROR";
1276 /******************************************************************************/
1277 /************************ PERIODIC FUNCTIONS ****************************/
1278 /******************************************************************************/
1281 * Periodically announce self id in the DHT
1283 * @param cls closure
1284 * @param tc task context
1287 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1289 struct PBlock block;
1291 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1293 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1297 * - Set data expiration in function of X
1298 * - Adapt X to churn
1300 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1302 block.id = my_full_id;
1303 GNUNET_DHT_put (dht_handle, /* DHT handle */
1304 &my_full_id.hashPubKey, /* Key to use */
1305 dht_replication_level, /* Replication level */
1306 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1307 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1308 sizeof (block), /* Size of the data */
1309 (const char *) &block, /* Data itself */
1310 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1311 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1312 NULL, /* Continuation */
1313 NULL); /* Continuation closure */
1315 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1319 /******************************************************************************/
1320 /****************** GENERAL HELPER FUNCTIONS ************************/
1321 /******************************************************************************/
1325 * Get the static string for a peer ID.
1329 * @return Static string for it's ID.
1332 peer2s (const struct MeshPeer *peer)
1336 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1341 * Get the previous hop in a connection
1343 * @param c Connection.
1345 * @return Previous peer in the connection.
1347 static struct MeshPeer *
1348 connection_get_prev_hop (struct MeshConnection *c)
1352 if (0 == c->own_pos || c->path->length < 2)
1353 id = c->path->peers[0];
1355 id = c->path->peers[c->own_pos - 1];
1357 return peer_get_short (id);
1362 * Get the next hop in a connection
1364 * @param c Connection.
1366 * @return Next peer in the connection.
1368 static struct MeshPeer *
1369 connection_get_next_hop (struct MeshConnection *c)
1373 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1374 id = c->path->peers[c->path->length - 1];
1376 id = c->path->peers[c->own_pos + 1];
1378 return peer_get_short (id);
1383 * Get the hop in a connection.
1385 * @param c Connection.
1386 * @param fwd Next hop?
1388 * @return Next peer in the connection.
1390 static struct MeshPeer *
1391 connection_get_hop (struct MeshConnection *c, int fwd)
1394 return connection_get_next_hop (c);
1395 return connection_get_prev_hop (c);
1399 * Check if client has registered with the service and has not disconnected
1401 * @param client the client to check
1403 * @return non-NULL if client exists in the global DLL
1405 static struct MeshClient *
1406 client_get (struct GNUNET_SERVER_Client *client)
1408 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1413 * Deletes a tunnel from a client (either owner or destination).
1415 * @param c Client whose tunnel to delete.
1416 * @param ch Channel which should be deleted.
1419 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1425 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1427 if (GNUNET_YES != res)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1432 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1434 if (GNUNET_YES != res)
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1441 * Notify the appropriate client that a new incoming channel was created.
1443 * @param ch Channel that was created.
1446 send_local_channel_create (struct MeshChannel *ch)
1448 struct GNUNET_MESH_ChannelMessage msg;
1449 struct MeshTunnel2 *t = ch->t;
1451 if (NULL == ch->dest)
1453 msg.header.size = htons (sizeof (msg));
1454 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1455 msg.channel_id = htonl (ch->lid_dest);
1456 msg.port = htonl (ch->port);
1458 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1459 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1460 msg.opt = htonl (msg.opt);
1461 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1462 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1463 &msg.header, GNUNET_NO);
1468 * Notify a client that the incoming tunnel is no longer valid.
1470 * @param ch Channel that is destroyed.
1471 * @param fwd Forward notification (owner->dest)?
1474 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1476 struct GNUNET_MESH_ChannelMessage msg;
1477 struct MeshClient *c;
1479 c = fwd ? ch->dest : ch->root;
1485 msg.header.size = htons (sizeof (msg));
1486 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1487 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1488 msg.port = htonl (0);
1489 memset (&msg.peer, 0, sizeof (msg.peer));
1490 msg.opt = htonl (0);
1491 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1492 &msg.header, GNUNET_NO);
1497 * Build a local ACK message and send it to a local client.
1499 * @param ch Channel on which to send the ACK.
1500 * @param c Client to whom send the ACK.
1501 * @param fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1504 send_local_ack (struct MeshChannel *ch,
1505 struct MeshClient *c,
1508 struct GNUNET_MESH_LocalAck msg;
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "send local %s ack on %s:%X towards %p\n",
1512 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1515 || ( fwd && (0 == ch->lid_root || c != ch->root))
1516 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1521 msg.header.size = htons (sizeof (msg));
1522 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1523 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1524 GNUNET_SERVER_notification_context_unicast (nc,
1529 ch->blocked_fwd = GNUNET_NO;
1531 ch->blocked_bck = GNUNET_NO;
1536 * Count established (ready) connections of a tunnel.
1538 * @param t Tunnel on which to send the message.
1540 * @return Number of connections.
1543 tunnel_count_connections (struct MeshTunnel2 *t)
1545 struct MeshConnection *c;
1548 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1555 * Pick a connection on which send the next data message.
1557 * @param t Tunnel on which to send the message.
1558 * @param fwd Is this a fwd message?
1560 * @return The connection on which to send the next message.
1562 static struct MeshConnection *
1563 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1565 struct MeshConnection *c;
1566 struct MeshConnection *best;
1567 struct MeshFlowControl *fc;
1568 unsigned int lowest_q;
1571 lowest_q = UINT_MAX;
1572 for (c = t->connection_head; NULL != c; c = c->next)
1574 if (MESH_CONNECTION_READY == c->state)
1576 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1582 if (fc->queue_n < lowest_q)
1585 lowest_q = fc->queue_n;
1594 * Get the total buffer space for a tunnel.
1597 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1599 struct MeshConnection *c;
1600 struct MeshFlowControl *fc;
1601 unsigned int buffer;
1603 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1605 if (c->state != MESH_CONNECTION_READY)
1608 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1609 buffer += fc->last_ack_recv - fc->last_pid_sent;
1617 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1618 * Encrypt data with the tunnel key.
1620 * @param t Tunnel whose key to use.
1621 * @param dst Destination for the encrypted data.
1622 * @param src Source of the plaintext.
1623 * @param size Size of the plaintext.
1624 * @param iv Initialization Vector to use.
1625 * @param fwd Is this a fwd message?
1628 tunnel_encrypt (struct MeshTunnel2 *t,
1629 void *dst, const void *src,
1630 size_t size, uint64_t iv, int fwd)
1632 memcpy (dst, src, size);
1637 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1638 * Decrypt data with the tunnel key.
1640 * @param t Tunnel whose key to use.
1641 * @param dst Destination for the plaintext.
1642 * @param src Source of the encrypted data.
1643 * @param size Size of the encrypted data.
1644 * @param iv Initialization Vector to use.
1645 * @param fwd Is this a fwd message?
1648 tunnel_decrypt (struct MeshTunnel2 *t,
1649 void *dst, const void *src,
1650 size_t size, uint64_t iv, int fwd)
1652 memcpy (dst, src, size);
1657 * Sends an already built message on a connection, properly registering
1658 * all used resources.
1660 * @param message Message to send. Function makes a copy of it.
1661 * If message is not hop-by-hop, decrements TTL of copy.
1662 * @param c Connection on which this message is transmitted.
1663 * @param ch Channel on which this message is transmitted, or NULL.
1664 * @param fwd Is this a fwd message?
1667 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1668 struct MeshConnection *c,
1669 struct MeshChannel *ch,
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Connection %s\n",
1677 GNUNET_h2s (&c->id));
1679 size = ntohs (message->size);
1680 data = GNUNET_malloc (size);
1681 memcpy (data, message, size);
1682 type = ntohs (message->type);
1686 struct GNUNET_MESH_Encrypted *emsg;
1687 struct GNUNET_MESH_ACK *amsg;
1688 struct GNUNET_MESH_Poll *pmsg;
1691 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1692 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1693 emsg = (struct GNUNET_MESH_Encrypted *) data;
1694 ttl = ntohl (emsg->ttl);
1697 GNUNET_break_op (0);
1701 emsg->ttl = htonl (ttl - 1);
1702 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1706 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1707 amsg = (struct GNUNET_MESH_ACK *) data;
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1712 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1713 pmsg = (struct GNUNET_MESH_Poll *) data;
1715 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1733 * Sends an already built message on a tunnel, choosing the best connection.
1735 * @param message Message to send. Function modifies it.
1736 * @param t Tunnel on which this message is transmitted.
1737 * @param ch Channel on which this message is transmitted.
1738 * @param fwd Is this a fwd message?
1741 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1742 struct MeshTunnel2 *t,
1743 struct MeshChannel *ch,
1746 struct MeshConnection *c;
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1751 c = tunnel_get_connection (t, fwd);
1754 GNUNET_break (GNUNET_YES == t->destroy);
1757 type = ntohs (msg->header.type);
1760 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1761 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1762 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1763 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1765 msg->ttl = htonl (default_ttl);
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1769 GNUNET_MESH_DEBUG_M2S (type));
1773 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1778 * Sends an already built message on a channel, properly registering
1779 * all used resources and encrypting the message with the tunnel's key.
1781 * @param message Message to send. Function makes a copy of it.
1782 * @param ch Channel on which this message is transmitted.
1783 * @param fwd Is this a fwd message?
1786 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1787 struct MeshChannel *ch,
1790 struct GNUNET_MESH_Encrypted *msg;
1791 size_t size = ntohs (message->size);
1792 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1797 peer2s (ch->t->peer), ch->gid);
1798 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1799 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1801 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1802 msg->header.type = htons (type);
1803 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1804 msg->iv = GNUNET_htonll (iv);
1805 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1807 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1812 * Sends a CREATE CONNECTION message for a path to a peer.
1813 * Changes the connection and tunnel states if necessary.
1815 * @param connection Connection to create.
1818 send_connection_create (struct MeshConnection *connection)
1820 struct MeshTunnel2 *t;
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1825 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1826 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1827 (connection->path->length *
1828 sizeof (struct GNUNET_PeerIdentity)),
1833 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1834 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1835 if (MESH_CONNECTION_NEW == connection->state)
1836 connection_change_state (connection, MESH_CONNECTION_SENT);
1841 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1844 * @param connection Connection to confirm.
1845 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1848 send_connection_ack (struct MeshConnection *connection, int fwd)
1850 struct MeshTunnel2 *t;
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1855 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1856 sizeof (struct GNUNET_MESH_ConnectionACK),
1860 if (MESH_TUNNEL_NEW == t->state)
1861 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1866 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1868 * @param c Which connection to send the hop-by-hop ACK.
1869 * @param ack Value of the ACK.
1870 * @param fwd Is this fwd?
1873 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1875 struct GNUNET_MESH_ACK msg;
1877 msg.header.size = htons (sizeof (msg));
1878 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1879 msg.ack = htonl (ack);
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "connection send %s ack %u on %s\n",
1884 fwd ? "FWD" : "BCK", ack, GNUNET_h2s (&c->id));
1886 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1891 * Core callback to write a pre-constructed data packet to core buffer
1893 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1894 * @param size Number of bytes available in buf.
1895 * @param buf Where the to write the message.
1897 * @return number of bytes written to buf
1900 send_core_data_raw (void *cls, size_t size, void *buf)
1902 struct GNUNET_MessageHeader *msg = cls;
1905 GNUNET_assert (NULL != msg);
1906 total_size = ntohs (msg->size);
1908 if (total_size > size)
1913 memcpy (buf, msg, total_size);
1920 * Function to send a create connection message to a peer.
1922 * @param c Connection to create.
1923 * @param size number of bytes available in buf
1924 * @param buf where the callee should write the message
1925 * @return number of bytes written to buf
1928 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1930 struct GNUNET_MESH_ConnectionCreate *msg;
1931 struct GNUNET_PeerIdentity *peer_ptr;
1932 struct MeshPeerPath *p = c->path;
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1938 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1939 p->length * sizeof (struct GNUNET_PeerIdentity);
1941 if (size < size_needed || NULL == buf)
1946 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1947 msg->header.size = htons (size_needed);
1948 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1951 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1952 for (i = 0; i < p->length; i++)
1954 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1964 * Creates a path ack message in buf and frees all unused resources.
1966 * @param c Connection to send an ACK on.
1967 * @param size number of bytes available in buf
1968 * @param buf where the callee should write the message
1970 * @return number of bytes written to buf
1973 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1975 struct GNUNET_MESH_ConnectionACK *msg = buf;
1976 struct MeshTunnel2 *t = c->t;
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1979 GNUNET_assert (NULL != t);
1980 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1985 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1986 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1989 /* TODO add signature */
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1992 return sizeof (struct GNUNET_MESH_ConnectionACK);
1997 * Destroy the peer_info and free any allocated resources linked to it
1999 * @param peer The peer_info to destroy.
2001 * @return GNUNET_OK on success
2004 peer_destroy (struct MeshPeer *peer)
2006 struct GNUNET_PeerIdentity id;
2007 struct MeshPeerPath *p;
2008 struct MeshPeerPath *nextp;
2010 GNUNET_PEER_resolve (peer->id, &id);
2011 GNUNET_PEER_change_rc (peer->id, -1);
2014 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2018 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2020 if (NULL != peer->dhtget)
2022 GNUNET_DHT_get_stop (peer->dhtget);
2024 p = peer->path_head;
2028 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2032 tunnel_destroy_empty (peer->tunnel);
2039 * Returns if peer is used (has a tunnel, is neighbor).
2041 * @peer Peer to check.
2043 * @return GNUNET_YES if peer is in use.
2046 peer_is_used (struct MeshPeer *peer)
2048 struct MeshPeerPath *p;
2050 if (NULL != peer->tunnel)
2053 for (p = peer->path_head; NULL != p; p = p->next)
2062 * Iterator over all the peers to get the oldest timestamp.
2064 * @param cls Closure (unsued).
2065 * @param key ID of the peer.
2066 * @param value Peer_Info of the peer.
2069 peer_get_oldest (void *cls,
2070 const struct GNUNET_HashCode *key,
2073 struct MeshPeer *p = value;
2074 struct GNUNET_TIME_Absolute *abs = cls;
2076 /* Don't count active peers */
2077 if (GNUNET_YES == peer_is_used (p))
2080 if (abs->abs_value_us < p->last_contact.abs_value_us)
2081 abs->abs_value_us = p->last_contact.abs_value_us;
2088 * Iterator over all the peers to remove the oldest entry.
2090 * @param cls Closure (unsued).
2091 * @param key ID of the peer.
2092 * @param value Peer_Info of the peer.
2095 peer_timeout (void *cls,
2096 const struct GNUNET_HashCode *key,
2099 struct MeshPeer *p = value;
2100 struct GNUNET_TIME_Absolute *abs = cls;
2102 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2103 GNUNET_NO == peer_is_used (p))
2113 * Delete oldest unused peer.
2116 peer_delete_oldest (void)
2118 struct GNUNET_TIME_Absolute abs;
2120 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2122 GNUNET_CONTAINER_multihashmap_iterate (peers,
2125 GNUNET_CONTAINER_multihashmap_iterate (peers,
2132 * Retrieve the MeshPeer stucture associated with the peer, create one
2133 * and insert it in the appropriate structures if the peer is not known yet.
2135 * @param peer Full identity of the peer.
2137 * @return Existing or newly created peer info.
2139 static struct MeshPeer *
2140 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2142 struct MeshPeer *peer;
2144 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2147 peer = GNUNET_new (struct MeshPeer);
2148 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2150 peer_delete_oldest ();
2152 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2153 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2154 peer->id = GNUNET_PEER_intern (peer_id);
2156 peer->last_contact = GNUNET_TIME_absolute_get();
2163 * Retrieve the MeshPeer stucture associated with the peer, create one
2164 * and insert it in the appropriate structures if the peer is not known yet.
2166 * @param peer Short identity of the peer.
2168 * @return Existing or newly created peer info.
2170 static struct MeshPeer *
2171 peer_get_short (const GNUNET_PEER_Id peer)
2173 return peer_get (GNUNET_PEER_resolve2 (peer));
2178 * Get a cost of a path for a peer considering existing tunnel connections.
2180 * @param peer Peer towards which the path is considered.
2181 * @param path Candidate path.
2183 * @return Cost of the path (path length + number of overlapping nodes)
2186 peer_get_path_cost (const struct MeshPeer *peer,
2187 const struct MeshPeerPath *path)
2189 struct MeshConnection *c;
2190 unsigned int overlap;
2198 GNUNET_assert (NULL != peer->tunnel);
2200 for (i = 0; i < path->length; i++)
2202 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2204 for (j = 0; j < c->path->length; j++)
2206 if (path->peers[i] == c->path->peers[j])
2214 return (path->length + overlap) * (path->score * -1);
2219 * Choose the best path towards a peer considering the tunnel properties.
2221 * @param peer The destination peer.
2223 * @return Best current known path towards the peer, if any.
2225 static struct MeshPeerPath *
2226 peer_get_best_path (const struct MeshPeer *peer)
2228 struct MeshPeerPath *best_p;
2229 struct MeshPeerPath *p;
2230 struct MeshConnection *c;
2231 unsigned int best_cost;
2234 best_cost = UINT_MAX;
2236 for (p = peer->path_head; NULL != p; p = p->next)
2238 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2242 continue; /* If path is in use in a connection, skip it. */
2244 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2254 queue_is_sendable (struct MeshPeerQueue *q)
2256 struct MeshFlowControl *fc;
2258 /* Is PID-independent? */
2261 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2262 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2266 /* Is PID allowed? */
2267 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2268 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2276 * Get first sendable message.
2278 * @param peer The destination peer.
2280 * @return Best current known path towards the peer, if any.
2282 static struct MeshPeerQueue *
2283 peer_get_first_message (const struct MeshPeer *peer)
2285 struct MeshPeerQueue *q;
2287 for (q = peer->queue_head; NULL != q; q = q->next)
2289 if (queue_is_sendable (q))
2298 * Try to establish a new connection to this peer in the given tunnel.
2299 * If the peer doesn't have any path to it yet, try to get one.
2300 * If the peer already has some path, send a CREATE CONNECTION towards it.
2302 * @param peer PeerInfo of the peer.
2305 peer_connect (struct MeshPeer *peer)
2307 struct MeshTunnel2 *t;
2308 struct MeshPeerPath *p;
2309 struct MeshConnection *c;
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "peer_connect towards %s\n",
2315 if (NULL != peer->path_head)
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2318 p = peer_get_best_path (peer);
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2322 c = tunnel_use_path (t, p);
2323 send_connection_create (c);
2326 else if (NULL == peer->dhtget)
2328 const struct GNUNET_PeerIdentity *id;
2330 id = GNUNET_PEER_resolve2 (peer->id);
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 " Starting DHT GET for peer %s\n", peer2s (peer));
2333 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2334 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2335 &id->hashPubKey, /* key to search */
2336 dht_replication_level, /* replication level */
2337 GNUNET_DHT_RO_RECORD_ROUTE |
2338 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2340 0, /* xquery bits */
2341 &dht_get_id_handler, peer);
2342 if (MESH_TUNNEL_NEW == t->state)
2343 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "There is no path but the DHT GET is already started.\n");
2354 * Get the first transmittable message for a connection.
2356 * @param c Connection.
2357 * @param fwd Is this FWD?
2359 * @return First transmittable message.
2361 static struct MeshPeerQueue *
2362 connection_get_first_message (struct MeshConnection *c, int fwd)
2364 struct MeshPeerQueue *q;
2367 p = connection_get_hop (c, fwd);
2369 for (q = p->queue_head; NULL != q; q = q->next)
2373 if (queue_is_sendable (q))
2382 * Is this peer the first one on the connection?
2384 * @param c Connection.
2385 * @param fwd Is this about fwd traffic?
2387 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2390 connection_is_origin (struct MeshConnection *c, int fwd)
2392 if (!fwd && c->own_pos == c->path->length - 1)
2394 if (fwd && c->own_pos == 0)
2401 * Is this peer the last one on the connection?
2403 * @param c Connection.
2404 * @param fwd Is this about fwd traffic?
2405 * Note that the ROOT is the terminal for BCK traffic!
2407 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2410 connection_is_terminal (struct MeshConnection *c, int fwd)
2412 if (fwd && c->own_pos == c->path->length - 1)
2414 if (!fwd && c->own_pos == 0)
2421 * @brief Re-initiate traffic on this connection if necessary.
2423 * Check if there is traffic queued towards this peer
2424 * and the core transmit handle is NULL (traffic was stalled).
2425 * If so, call core tmt rdy.
2427 * @param c Connection on which initiate traffic.
2428 * @param fwd Is this about fwd traffic?
2431 connection_unlock_queue (struct MeshConnection *c, int fwd)
2433 struct MeshPeer *peer;
2434 struct MeshPeerQueue *q;
2437 peer = connection_get_hop (c, fwd);
2439 if (NULL != peer->core_transmit)
2440 return; /* Already unlocked */
2442 q = connection_get_first_message (c, fwd);
2444 return; /* Nothing to transmit */
2447 peer->core_transmit =
2448 GNUNET_CORE_notify_transmit_ready (core_handle,
2451 GNUNET_TIME_UNIT_FOREVER_REL,
2452 GNUNET_PEER_resolve2 (peer->id),
2460 * Cancel all transmissions that belong to a certain connection.
2462 * @param c Connection which to cancel.
2463 * @param fwd Cancel fwd traffic?
2466 connection_cancel_queues (struct MeshConnection *c, int fwd)
2468 struct MeshPeerQueue *q;
2469 struct MeshPeerQueue *next;
2470 struct MeshFlowControl *fc;
2471 struct MeshPeer *peer;
2478 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2479 peer = connection_get_hop (c, fwd);
2481 for (q = peer->queue_head; NULL != q; q = next)
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "connection_cancel_queue %s\n",
2488 GNUNET_MESH_DEBUG_M2S (q->type));
2489 queue_destroy (q, GNUNET_YES);
2492 if (NULL == peer->queue_head)
2494 if (NULL != peer->core_transmit)
2496 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2497 peer->core_transmit = NULL;
2499 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2501 GNUNET_SCHEDULER_cancel (fc->poll_task);
2502 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2509 * Remove all paths that rely on a direct connection between p1 and p2
2510 * from the peer itself and notify all tunnels about it.
2512 * @param peer PeerInfo of affected peer.
2513 * @param p1 GNUNET_PEER_Id of one peer.
2514 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2517 * TODO: optimize (see below)
2520 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2523 struct MeshPeerPath *p;
2524 struct MeshPeerPath *next;
2525 struct MeshPeer *peer_d;
2527 unsigned int destroyed;
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2532 for (p = peer->path_head; NULL != p; p = next)
2535 for (i = 0; i < (p->length - 1); i++)
2537 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2538 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2540 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2551 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2553 peer_d = peer_get_short (d); // FIXME
2554 next = peer_get_best_path (peer_d);
2555 tunnel_use_path (peer->tunnel, next);
2556 peer_connect (peer_d);
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2563 * Add the path to the peer and update the path used to reach it in case this
2566 * @param peer_info Destination peer to add the path to.
2567 * @param path New path to add. Last peer must be the peer in arg 1.
2568 * Path will be either used of freed if already known.
2569 * @param trusted Do we trust that this path is real?
2572 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2575 struct MeshPeerPath *aux;
2579 if ((NULL == peer_info) || (NULL == path))
2582 path_destroy (path);
2585 if (path->peers[path->length - 1] != peer_info->id)
2588 path_destroy (path);
2591 if (2 >= path->length && GNUNET_NO == trusted)
2593 /* Only allow CORE to tell us about direct paths */
2594 path_destroy (path);
2597 for (l = 1; l < path->length; l++)
2599 if (path->peers[l] == myid)
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2602 for (l2 = 0; l2 < path->length - l; l2++)
2604 path->peers[l2] = path->peers[l + l2];
2609 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2614 path->length, peer2s (peer_info));
2616 l = path_get_length (path);
2619 path_destroy (path);
2623 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2624 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2626 l2 = path_get_length (aux);
2629 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2630 peer_info->path_tail, aux, path);
2635 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2637 path_destroy (path);
2642 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2649 * Add the path to the origin peer and update the path used to reach it in case
2650 * this is the shortest.
2651 * The path is given in peer_info -> destination, therefore we turn the path
2652 * upside down first.
2654 * @param peer_info Peer to add the path to, being the origin of the path.
2655 * @param path New path to add after being inversed.
2656 * Path will be either used or freed.
2657 * @param trusted Do we trust that this path is real?
2660 peer_add_path_to_origin (struct MeshPeer *peer_info,
2661 struct MeshPeerPath *path, int trusted)
2664 peer_add_path (peer_info, path, trusted);
2670 * Function called if a connection has been stalled for a while,
2671 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2673 * @param cls Closure (poll ctx).
2674 * @param tc TaskContext.
2677 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2679 struct MeshFlowControl *fc = cls;
2680 struct GNUNET_MESH_Poll msg;
2681 struct MeshConnection *c;
2683 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2684 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2692 peer2s (c->t->peer), c->id);
2694 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2695 msg.header.size = htons (sizeof (msg));
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2697 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2698 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2699 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2700 &connection_poll, fc);
2705 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2706 * to obtain a local peer -> destination path and interning the peer ids.
2708 * @return Newly allocated and created path
2710 static struct MeshPeerPath *
2711 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2712 unsigned int get_path_length,
2713 const struct GNUNET_PeerIdentity *put_path,
2714 unsigned int put_path_length)
2716 struct MeshPeerPath *p;
2722 GNUNET_PEER_change_rc (myid, 1);
2723 i = get_path_length;
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2725 for (i--; i >= 0; i--)
2727 id = GNUNET_PEER_intern (&get_path[i]);
2728 if (p->length > 0 && id == p->peers[p->length - 1])
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2731 GNUNET_PEER_change_rc (id, -1);
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2736 GNUNET_i2s (&get_path[i]));
2738 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2739 p->peers[p->length - 1] = id;
2742 i = put_path_length;
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2744 for (i--; i >= 0; i--)
2746 id = GNUNET_PEER_intern (&put_path[i]);
2749 /* PUT path went through us, so discard the path up until now and start
2750 * from here to get a much shorter (and loop-free) path.
2755 if (p->length > 0 && id == p->peers[p->length - 1])
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2758 GNUNET_PEER_change_rc (id, -1);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2763 GNUNET_i2s (&put_path[i]));
2765 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2766 p->peers[p->length - 1] = id;
2770 if (get_path_length > 0)
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2772 GNUNET_i2s (&get_path[0]));
2773 if (put_path_length > 0)
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2775 GNUNET_i2s (&put_path[0]));
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2778 for (i = 0; i < p->length; i++)
2780 struct GNUNET_PeerIdentity peer_id;
2782 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2784 GNUNET_i2s (&peer_id));
2792 * Adds a path to the peer_infos of all the peers in the path
2794 * @param p Path to process.
2795 * @param confirmed Whether we know if the path works or not.
2798 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2802 /* TODO: invert and add */
2803 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2804 for (i++; i < p->length; i++)
2806 struct MeshPeer *aux;
2807 struct MeshPeerPath *copy;
2809 aux = peer_get_short (p->peers[i]);
2810 copy = path_duplicate (p);
2811 copy->length = i + 1;
2812 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2818 * Search for a channel among the channels for a client
2820 * @param c the client whose channels to search in
2821 * @param chid the local id of the channel
2823 * @return channel handler, NULL if doesn't exist
2825 static struct MeshChannel *
2826 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2828 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2830 GNUNET_break_op (0);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2834 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2835 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2836 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2840 * Search for a tunnel by global ID using full PeerIdentities.
2842 * @param t Tunnel containing the channel.
2843 * @param chid Public channel number.
2845 * @return channel handler, NULL if doesn't exist
2847 static struct MeshChannel *
2848 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2850 struct MeshChannel *ch;
2855 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2857 if (ch->gid == chid)
2866 * Change the tunnel state.
2868 * @param t Tunnel whose state to change.
2869 * @param state New state.
2872 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Tunnel %s state was %s\n",
2879 GNUNET_MESH_DEBUG_TS2S (t->state));
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2881 "Tunnel %s state is now %s\n",
2883 GNUNET_MESH_DEBUG_TS2S (state));
2889 * Cache a message to be sent once tunnel is online.
2891 * @param t Tunnel to hold the message.
2892 * @param ch Channel the message is about.
2893 * @param msg Message itself (copy will be made).
2894 * @param fwd Is this fwd?
2897 tunnel_queue_data (struct MeshTunnel2 *t,
2898 struct MeshChannel *ch,
2899 struct GNUNET_MessageHeader *msg,
2902 struct MeshTunnelQueue *tq;
2903 uint16_t size = ntohs (msg->size);
2905 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2908 memcpy (&tq[1], msg, size);
2909 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2914 * Send all cached messages that we can, tunnel is online.
2916 * @param t Tunnel that holds the messages.
2917 * @param fwd Is this fwd?
2920 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2922 struct MeshTunnelQueue *tq;
2923 struct MeshTunnelQueue *next;
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 "tunnel_send_queued_data on tunnel %s\n",
2929 room = tunnel_get_buffer (t, fwd);
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2931 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2935 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2936 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2945 connection_change_state (struct MeshConnection* c,
2946 enum MeshConnectionState state)
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "Connection %s state was %s\n",
2950 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 "Connection %s state is now %s\n",
2953 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
2959 * Add a client to a channel, initializing all needed data structures.
2961 * @param ch Channel to which add the client.
2962 * @param c Client which to add to the channel.
2965 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2967 struct MeshTunnel2 *t = ch->t;
2969 if (NULL != ch->dest)
2975 /* Assign local id as destination */
2976 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2977 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2978 ch->lid_dest = t->next_local_chid++;
2979 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2981 /* Store in client's hashmap */
2983 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2994 static struct MeshConnection *
2995 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2997 struct MeshConnection *c;
2998 struct GNUNET_HashCode cid;
2999 struct MeshPeer *peer;
3000 unsigned int own_pos;
3002 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3004 c = connection_new (&cid);
3006 for (own_pos = 0; own_pos < p->length; own_pos++)
3008 if (p->peers[own_pos] == myid)
3011 if (own_pos > p->length - 1)
3014 connection_destroy (c);
3017 c->own_pos = own_pos;
3022 c->fwd_maintenance_task =
3023 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3024 &connection_fwd_keepalive, c);
3027 peer = connection_get_next_hop (c);
3028 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3030 peer = connection_get_prev_hop (c);
3031 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3032 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3038 * Notifies a tunnel that a connection has broken that affects at least
3039 * some of its peers. Sends a notification towards the root of the tree.
3040 * In case the peer is the owner of the tree, notifies the client that owns
3041 * the tunnel and tries to reconnect.
3043 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3045 * @param t Tunnel affected.
3046 * @param p1 Peer that got disconnected from p2.
3047 * @param p2 Peer that got disconnected from p1.
3049 * @return Short ID of the peer disconnected (either p1 or p2).
3050 * 0 if the tunnel remained unaffected.
3052 static GNUNET_PEER_Id
3053 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3054 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3056 // if (myid != p1 && myid != p2) FIXME
3061 // if (tree_get_predecessor (t->tree) != 0)
3063 // /* We are the peer still connected, notify owner of the disconnection. */
3064 // struct GNUNET_MESH_PathBroken msg;
3065 // struct GNUNET_PeerIdentity neighbor;
3067 // msg.header.size = htons (sizeof (msg));
3068 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3069 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3070 // msg.tid = htonl (t->id.tid);
3071 // msg.peer1 = my_full_id;
3072 // GNUNET_PEER_resolve (pid, &msg.peer2);
3073 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3074 // send_prebuilt_message (&msg.header, &neighbor, t);
3081 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3083 * @param ch Channel this is about.
3084 * @param fwd Is for FWD traffic? (ACK dest->owner)
3087 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3089 struct GNUNET_MESH_DataACK msg;
3090 struct MeshChannelReliability *rel;
3091 struct MeshReliableMessage *copy;
3097 if (GNUNET_NO == ch->reliable)
3102 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3103 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3105 "send_data_ack for %u\n",
3108 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3109 msg.header.type = htons (type);
3110 msg.header.size = htons (sizeof (msg));
3111 msg.chid = htonl (ch->gid);
3112 msg.mid = htonl (*mid - 1);
3114 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3116 if (copy->type != type)
3118 delta = copy->mid - *mid;
3121 mask = 0x1LL << delta;
3122 msg.futures |= mask;
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3125 copy->mid, delta, mask, msg.futures);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3129 send_prebuilt_message_channel (&msg.header, ch, fwd);
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3135 * Send an ACK informing the predecessor about the available buffer space.
3137 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3138 * the ACK itself goes "back" (dest->root).
3140 * @param c Connection on which to send the ACK.
3141 * @param fwd Is this FWD ACK? (Going dest->owner)
3144 connection_send_ack (struct MeshConnection *c, int fwd)
3146 struct MeshFlowControl *next_fc;
3147 struct MeshFlowControl *prev_fc;
3151 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3152 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3155 "connection send %s ack on %s\n",
3156 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3158 /* Check if we need to transmit the ACK */
3159 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3163 " last pid recv: %u, last ack sent: %u\n",
3164 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3168 /* Ok, ACK might be necessary, what PID to ACK? */
3169 delta = next_fc->queue_max - next_fc->queue_n;
3170 ack = prev_fc->last_pid_recv + delta;
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 " last pid %u, last ack %u, qmax %u, q %u\n",
3174 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3175 next_fc->queue_max, next_fc->queue_n);
3176 if (ack == prev_fc->last_ack_sent)
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3182 prev_fc->last_ack_sent = ack;
3183 send_ack (c, ack, fwd);
3188 * Modify the mesh message TID from global to local and send to client.
3190 * @param ch Channel on which to send the message.
3191 * @param msg Message to modify and send.
3192 * @param c Client to send to.
3193 * @param tid Tunnel ID to use (c can be both owner and client).
3196 channel_send_client_to_tid (struct MeshChannel *ch,
3197 const struct GNUNET_MESH_Data *msg,
3198 struct MeshClient *c, MESH_ChannelNumber id)
3200 struct GNUNET_MESH_LocalData *copy;
3201 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3202 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3204 if (size < sizeof (struct GNUNET_MessageHeader))
3206 GNUNET_break_op (0);
3214 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3215 memcpy (©[1], &msg[1], size);
3216 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3217 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3218 copy->id = htonl (id);
3219 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3220 ©->header, GNUNET_NO);
3224 * Modify the data message ID from global to local and send to client.
3226 * @param ch Channel on which to send the message.
3227 * @param msg Message to modify and send.
3228 * @param fwd Forward?
3231 channel_send_client_data (struct MeshChannel *ch,
3232 const struct GNUNET_MESH_Data *msg,
3236 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3238 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3243 * Send a buffered message to the client, for in order delivery or
3244 * as result of client ACK.
3246 * @param ch Channel on which to empty the message buffer.
3247 * @param c Client to send to.
3248 * @param rel Reliability structure to corresponding peer.
3249 * If rel == bck_rel, this is FWD data.
3252 channel_send_client_buffered_data (struct MeshChannel *ch,
3253 struct MeshClient *c,
3254 struct MeshChannelReliability *rel)
3256 struct MeshReliableMessage *copy;
3259 if (GNUNET_NO == rel->client_ready)
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3265 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3268 copy = rel->head_recv;
3269 /* We never buffer channel management messages */
3272 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3274 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 " have %u! now expecting %u\n",
3278 copy->mid, *mid + 1);
3279 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3282 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 " reliable && don't have %u, next is %u\n",
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3299 * We have received a message out of order, or the client is not ready.
3300 * Buffer it until we receive an ACK from the client or the missing
3301 * message from the channel.
3303 * @param msg Message to buffer (MUST be of type MESH_DATA).
3304 * @param rel Reliability data to the corresponding direction.
3307 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3308 struct MeshChannelReliability *rel)
3310 struct MeshReliableMessage *copy;
3311 struct MeshReliableMessage *prev;
3315 size = ntohs (msg->header.size);
3316 mid = ntohl (msg->mid);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3320 copy = GNUNET_malloc (sizeof (*copy) + size);
3323 memcpy (©[1], msg, size);
3327 // FIXME do something better than O(n), although n < 64...
3328 // FIXME start from the end (most messages are the latest ones)
3329 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3332 if (GMC_is_pid_bigger (prev->mid, mid))
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3335 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3341 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3347 * Destroy a reliable message after it has been acknowledged, either by
3348 * direct mid ACK or bitfield. Updates the appropriate data structures and
3349 * timers and frees all memory.
3351 * @param copy Message that is no longer needed: remote peer got it.
3354 rel_message_free (struct MeshReliableMessage *copy)
3356 struct MeshChannelReliability *rel;
3357 struct GNUNET_TIME_Relative time;
3360 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3361 rel->expected_delay.rel_value_us *= 7;
3362 rel->expected_delay.rel_value_us += time.rel_value_us;
3363 rel->expected_delay.rel_value_us /= 8;
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3368 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3370 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3372 rel->retry_timer = rel->expected_delay;
3373 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3379 * Destroy all reliable messages queued for a channel,
3380 * during a channel destruction.
3381 * Frees the reliability structure itself.
3383 * @param rel Reliability data for a channel.
3386 channel_rel_free_all (struct MeshChannelReliability *rel)
3388 struct MeshReliableMessage *copy;
3389 struct MeshReliableMessage *next;
3394 for (copy = rel->head_recv; NULL != copy; copy = next)
3397 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3400 for (copy = rel->head_sent; NULL != copy; copy = next)
3403 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3406 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3407 GNUNET_SCHEDULER_cancel (rel->retry_task);
3413 * Mark future messages as ACK'd.
3415 * @param rel Reliability data.
3416 * @param msg DataACK message with a bitfield of future ACK'd messages.
3419 channel_rel_free_sent (struct MeshChannelReliability *rel,
3420 const struct GNUNET_MESH_DataACK *msg)
3422 struct MeshReliableMessage *copy;
3423 struct MeshReliableMessage *next;
3430 bitfield = msg->futures;
3431 mid = ntohl (msg->mid);
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 "free_sent_reliable %u %llX\n",
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3436 " rel %p, head %p\n",
3437 rel, rel->head_sent);
3438 for (i = 0, copy = rel->head_sent;
3439 i < 64 && NULL != copy && 0 != bitfield;
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3443 " trying bit %u (mid %u)\n",
3446 if (0 == (bitfield & mask))
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3450 /* Bit was set, clear the bit from the bitfield */
3453 /* The i-th bit was set. Do we have that copy? */
3454 /* Skip copies with mid < target */
3455 target = mid + i + 1;
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3457 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3460 /* Did we run out of copies? (previously freed, it's ok) */
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3467 /* Did we overshoot the target? (previously freed, it's ok) */
3468 if (GMC_is_pid_bigger (copy->mid, target))
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3474 /* Now copy->mid == target, free it */
3476 rel_message_free (copy);
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3484 * We haven't received an ACK after a certain time: restransmit the message.
3486 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3487 * @param tc TaskContext.
3490 channel_retransmit_message (void *cls,
3491 const struct GNUNET_SCHEDULER_TaskContext *tc)
3493 struct MeshChannelReliability *rel = cls;
3494 struct MeshReliableMessage *copy;
3495 struct MeshPeerQueue *q;
3496 struct MeshChannel *ch;
3497 struct MeshConnection *c;
3498 struct GNUNET_MESH_Data *payload;
3499 struct MeshPeer *hop;
3502 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3507 copy = rel->head_sent;
3514 /* Search the message to be retransmitted in the outgoing queue.
3515 * Check only the queue for the connection that is going to be used,
3516 * if the message is stuck in some other connection's queue we shouldn't
3518 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3519 * the old connection could be temporary stalled or the queue happened to
3520 * be long at time of insertion.
3521 * - not sending the new one could cause terrible delays the old connection
3524 payload = (struct GNUNET_MESH_Data *) ©[1];
3525 fwd = (rel == ch->fwd_rel);
3526 c = tunnel_get_connection (ch->t, fwd);
3527 hop = connection_get_hop (c, fwd);
3528 for (q = hop->queue_head; NULL != q; q = q->next)
3530 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3532 struct GNUNET_MESH_Data *queued_data = q->cls;
3534 if (queued_data->mid == payload->mid)
3539 /* Message not found in the queue that we are going to use. */
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3544 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3545 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3552 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3553 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3554 &channel_retransmit_message,
3560 * Send an ACK to a client if needed.
3562 * @param ch Channel this is regarding.
3563 * @param fwd Is this about fwd traffic? (ACk goes the opposite direction).
3566 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3568 struct MeshClient *c;
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 "Channel send client %s ack on %s:%X\n",
3573 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3575 /* Check for buffer space */
3576 if (0 >= tunnel_get_buffer (ch->t, fwd))
3579 /* Client to receive the ACK (fwd indicates traffic to be ACK'd) */
3580 c = fwd ? ch->root : ch->dest;
3582 blocked = fwd ? ch->blocked_fwd : ch->blocked_bck;
3583 if (GNUNET_YES == blocked)
3584 send_local_ack (ch, c, fwd);
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client not blocked\n");
3591 * Send ACK on one or more connections due to buffer space to the client.
3593 * @param ch Channel which has some free buffer space.
3594 * @param buffer Buffer space.
3595 * @param fwd Is this in the FWD direction?
3598 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3600 struct MeshTunnel2 *t = ch->t;
3601 struct MeshConnection *c;
3602 struct MeshFlowControl *fc;
3607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3608 "Channel send connection %s ack on %s:%X\n",
3609 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3611 /* Count connections, how many messages are already allowed */
3612 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3614 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3615 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3620 allowed += fc->last_ack_sent - fc->last_pid_recv;
3624 /* Make sure there is no overflow */
3625 if (allowed > buffer)
3631 /* Authorize connections to send more data */
3632 to_allow = buffer - allowed;
3633 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3635 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3636 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3640 send_ack (c, fc->last_ack_sent + 1, fwd);
3644 GNUNET_break (to_allow == 0);
3649 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3651 * @param ch Channel to mark as ready.
3652 * @param fwd Was the CREATE message sent fwd?
3655 channel_confirm (struct MeshChannel *ch, int fwd)
3657 struct MeshChannelReliability *rel;
3658 struct MeshReliableMessage *copy;
3659 struct MeshReliableMessage *next;
3661 ch->state = MESH_CHANNEL_READY;
3663 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3664 for (copy = rel->head_sent; NULL != copy; copy = next)
3666 struct GNUNET_MessageHeader *msg;
3669 msg = (struct GNUNET_MessageHeader *) ©[1];
3670 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3672 rel_message_free (copy);
3680 * Save a copy to retransmit in case it gets lost.
3682 * Initializes all needed callbacks and timers.
3684 * @param ch Channel this message goes on.
3685 * @param msg Message to copy.
3686 * @param fwd Is this fwd traffic?
3689 channel_save_copy (struct MeshChannel *ch,
3690 const struct GNUNET_MessageHeader *msg,
3693 struct MeshChannelReliability *rel;
3694 struct MeshReliableMessage *copy;
3699 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3700 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3701 type = ntohs (msg->type);
3702 size = ntohs (msg->size);
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3705 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3707 copy->timestamp = GNUNET_TIME_absolute_get ();
3710 memcpy (©[1], msg, size);
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3713 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3714 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3717 GNUNET_TIME_relative_multiply (rel->expected_delay,
3718 MESH_RETRANSMIT_MARGIN);
3720 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3721 &channel_retransmit_message,
3728 * Send keepalive packets for a connection.
3730 * @param c Connection to keep alive..
3731 * @param fwd Is this a FWD keepalive? (owner -> dest).
3734 connection_keepalive (struct MeshConnection *c, int fwd)
3736 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3737 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3741 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3742 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3745 "sending %s keepalive for connection %s[%d]\n",
3746 fwd ? "FWD" : "BCK",
3747 peer2s (c->t->peer),
3750 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3751 msg->header.size = htons (size);
3752 msg->header.type = htons (type);
3755 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3760 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3762 * @param c Connection for which to send the message.
3763 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3766 connection_recreate (struct MeshConnection *c, int fwd)
3768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3770 send_connection_create (c);
3772 send_connection_ack (c, GNUNET_NO);
3777 * Generic connection timer management.
3778 * Depending on the role of the peer in the connection will send the
3779 * appropriate message (build or keepalive)
3781 * @param c Conncetion to maintain.
3782 * @param fwd Is FWD?
3785 connection_maintain (struct MeshConnection *c, int fwd)
3787 if (MESH_TUNNEL_SEARCHING == c->t->state)
3789 /* TODO DHT GET with RO_BART */
3794 case MESH_CONNECTION_NEW:
3796 case MESH_CONNECTION_SENT:
3797 connection_recreate (c, fwd);
3799 case MESH_CONNECTION_READY:
3800 connection_keepalive (c, fwd);
3809 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3811 struct MeshConnection *c = cls;
3813 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3814 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3817 connection_maintain (c, GNUNET_YES);
3818 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3819 &connection_fwd_keepalive,
3825 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3827 struct MeshConnection *c = cls;
3829 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3830 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3833 connection_maintain (c, GNUNET_NO);
3834 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3835 &connection_bck_keepalive,
3841 * Send a message to all peers in this connection that the connection
3842 * is no longer valid.
3844 * If some peer should not receive the message, it should be zero'ed out
3845 * before calling this function.
3847 * @param c The connection whose peers to notify.
3850 connection_send_destroy (struct MeshConnection *c)
3852 struct GNUNET_MESH_ConnectionDestroy msg;
3854 msg.header.size = htons (sizeof (msg));
3855 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3858 " sending connection destroy for connection %s[%X]\n",
3859 peer2s (c->t->peer),
3862 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3863 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3864 c->destroy = GNUNET_YES;
3869 * Confirm we got a channel create.
3871 * @param ch The channel to confirm.
3872 * @param fwd Should we send the ACK fwd?
3875 channel_send_ack (struct MeshChannel *ch, int fwd)
3877 struct GNUNET_MESH_ChannelManage msg;
3879 msg.header.size = htons (sizeof (msg));
3880 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 " sending channel ack for channel %s:%X\n",
3883 peer2s (ch->t->peer),
3886 msg.chid = htonl (ch->gid);
3887 send_prebuilt_message_channel (&msg.header, ch, fwd);
3892 * Send a message to all clients (local and remote) of this channel
3893 * notifying that the channel is no longer valid.
3895 * If some peer or client should not receive the message,
3896 * should be zero'ed out before calling this function.
3898 * @param ch The channel whose clients to notify.
3901 channel_send_destroy (struct MeshChannel *ch)
3903 struct GNUNET_MESH_ChannelManage msg;
3905 msg.header.size = htons (sizeof (msg));
3906 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3908 " sending channel destroy for channel %s:%X\n",
3909 peer2s (ch->t->peer),
3912 if (NULL != ch->root)
3914 msg.chid = htonl (ch->lid_root);
3915 send_local_channel_destroy (ch, GNUNET_NO);
3919 msg.chid = htonl (ch->gid);
3920 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3923 if (NULL != ch->dest)
3925 msg.chid = htonl (ch->lid_dest);
3926 send_local_channel_destroy (ch, GNUNET_YES);
3930 msg.chid = htonl (ch->gid);
3931 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3939 static struct MeshTunnel2 *
3942 struct MeshTunnel2 *t;
3944 t = GNUNET_new (struct MeshTunnel2);
3946 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3948 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3949 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3951 // GNUNET_break (0);
3952 // tunnel_destroy (t);
3956 // char salt[] = "salt";
3957 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3958 // salt, sizeof (salt),
3959 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3960 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3961 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3963 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3964 // salt, sizeof (salt),
3965 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3966 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3967 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3975 * Add a connection to a tunnel.
3978 * @param c Connection.
3981 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3984 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3989 * Initialize a Flow Control structure to the initial state.
3991 * @param fc Flow Control structure to initialize.
3994 fc_init (struct MeshFlowControl *fc)
3997 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3998 fc->last_pid_recv = (uint32_t) -1;
3999 fc->last_ack_sent = (uint32_t) 0;
4000 fc->last_ack_recv = (uint32_t) 0;
4001 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4002 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4004 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4008 static struct MeshConnection *
4009 connection_new (const struct GNUNET_HashCode *cid)
4011 struct MeshConnection *c;
4013 c = GNUNET_new (struct MeshConnection);
4015 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4016 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4017 fc_init (&c->fwd_fc);
4018 fc_init (&c->bck_fc);
4027 * Find a connection.
4029 * @param cid Connection ID.
4031 static struct MeshConnection *
4032 connection_get (const struct GNUNET_HashCode *cid)
4034 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4039 connection_destroy (struct MeshConnection *c)
4041 struct MeshPeer *peer;
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4047 peer2s (c->t->peer),
4050 /* Cancel all traffic */
4051 connection_cancel_queues (c, GNUNET_YES);
4052 connection_cancel_queues (c, GNUNET_NO);
4054 /* Cancel maintainance task (keepalive/timeout) */
4055 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4056 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4057 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4058 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4060 /* Deregister from neighbors */
4061 peer = connection_get_next_hop (c);
4063 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4064 peer = connection_get_prev_hop (c);
4066 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4069 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4070 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4076 tunnel_destroy (struct MeshTunnel2 *t)
4078 struct MeshConnection *c;
4079 struct MeshConnection *next;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4087 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4088 // GNUNET_break (0);
4090 for (c = t->connection_head; NULL != c; c = next)
4093 connection_destroy (c);
4096 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4103 * Tunnel is empty: destroy it.
4105 * Notifies all connections about the destruction.
4107 * @param t Tunnel to destroy.
4110 tunnel_destroy_empty (struct MeshTunnel2 *t)
4112 struct MeshConnection *c;
4114 for (c = t->connection_head; NULL != c; c = c->next)
4116 if (GNUNET_NO == c->destroy)
4117 connection_send_destroy (c);
4120 if (0 == t->pending_messages)
4123 t->destroy = GNUNET_YES;
4128 * Destroy tunnel if empty (no more channels).
4130 * @param t Tunnel to destroy if empty.
4133 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4135 if (NULL != t->channel_head)
4138 tunnel_destroy_empty (t);
4143 * Destroy a channel and free all resources.
4145 * @param ch Channel to destroy.
4148 channel_destroy (struct MeshChannel *ch)
4150 struct MeshClient *c;
4158 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4169 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4176 channel_rel_free_all (ch->fwd_rel);
4177 channel_rel_free_all (ch->bck_rel);
4179 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4180 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4186 * Create a new channel.
4188 * @param t Tunnel this channel is in.
4189 * @param owner Client that owns the channel, NULL for foreign channels.
4190 * @param lid_root Local ID for root client.
4192 * @return A new initialized channel. NULL on error.
4194 static struct MeshChannel *
4195 channel_new (struct MeshTunnel2 *t,
4196 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4198 struct MeshChannel *ch;
4200 ch = GNUNET_new (struct MeshChannel);
4202 ch->lid_root = lid_root;
4204 ch->blocked_fwd = GNUNET_YES;
4205 ch->blocked_bck = GNUNET_YES;
4207 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4209 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4213 while (NULL != channel_get (t, t->next_chid))
4214 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4215 ch->gid = t->next_chid;
4216 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4219 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4223 channel_destroy (ch);
4224 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4234 * Set options in a channel, extracted from a bit flag field
4236 * @param ch Channel to set options to.
4237 * @param options Bit array in host byte order.
4240 channel_set_options (struct MeshChannel *ch, uint32_t options)
4242 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4243 GNUNET_YES : GNUNET_NO;
4244 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4245 GNUNET_YES : GNUNET_NO;
4250 * Iterator for deleting each channel whose client endpoint disconnected.
4252 * @param cls Closure (client that has disconnected).
4253 * @param key The local channel id (used to access the hashmap).
4254 * @param value The value stored at the key (channel to destroy).
4256 * @return GNUNET_OK, keep iterating.
4259 channel_destroy_iterator (void *cls,
4263 struct MeshChannel *ch = value;
4264 struct MeshClient *c = cls;
4265 struct MeshTunnel2 *t;
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4268 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4269 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4283 channel_send_destroy (ch);
4284 channel_destroy (ch);
4285 tunnel_destroy_if_empty (t);
4292 * Remove client's ports from the global hashmap on disconnect.
4294 * @param cls Closure (unused).
4296 * @param value Client structure.
4298 * @return GNUNET_OK, keep iterating.
4301 client_release_ports (void *cls,
4307 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4308 if (GNUNET_YES != res)
4311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4312 "Port %u by client %p was not registered.\n",
4320 * Timeout function due to lack of keepalive/traffic from the owner.
4321 * Destroys connection if called.
4323 * @param cls Closure (connection to destroy).
4324 * @param tc TaskContext.
4327 connection_fwd_timeout (void *cls,
4328 const struct GNUNET_SCHEDULER_TaskContext *tc)
4330 struct MeshConnection *c = cls;
4332 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4333 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4336 "Connection %s[%X] FWD timed out. Destroying.\n",
4337 peer2s (c->t->peer),
4340 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4343 connection_destroy (c);
4348 * Timeout function due to lack of keepalive/traffic from the destination.
4349 * Destroys connection if called.
4351 * @param cls Closure (connection to destroy).
4352 * @param tc TaskContext
4355 connection_bck_timeout (void *cls,
4356 const struct GNUNET_SCHEDULER_TaskContext *tc)
4358 struct MeshConnection *c = cls;
4360 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4361 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4365 "Connection %s[%X] FWD timed out. Destroying.\n",
4366 peer2s (c->t->peer),
4369 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4372 connection_destroy (c);
4377 * Resets the connection timeout task, some other message has done the
4379 * - For the first peer on the direction this means to send
4380 * a keepalive or a path confirmation message (either create or ACK).
4381 * - For all other peers, this means to destroy the connection,
4382 * due to lack of activity.
4383 * Starts the tiemout if no timeout was running (connection just created).
4385 * @param c Connection whose timeout to reset.
4386 * @param fwd Is this forward?
4388 * TODO use heap to improve efficiency of scheduler.
4391 connection_reset_timeout (struct MeshConnection *c, int fwd)
4393 GNUNET_SCHEDULER_TaskIdentifier *ti;
4394 GNUNET_SCHEDULER_Task f;
4396 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4398 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4399 GNUNET_SCHEDULER_cancel (*ti);
4401 if (connection_is_origin (c, fwd)) /* Endpoint */
4403 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4404 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4408 struct GNUNET_TIME_Relative delay;
4410 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4411 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4412 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4418 * Iterator to notify all connections of a broken link. Mark connections
4419 * to destroy after all traffic has been sent.
4421 * @param cls Closure (peer disconnected).
4422 * @param key Current key code (tid).
4423 * @param value Value in the hash map (connection).
4425 * @return GNUNET_YES if we should continue to iterate,
4429 connection_broken (void *cls,
4430 const struct GNUNET_HashCode *key,
4433 struct MeshPeer *peer = cls;
4434 struct MeshConnection *c = value;
4435 struct GNUNET_MESH_ConnectionBroken msg;
4438 fwd = peer == connection_get_prev_hop (c);
4439 connection_cancel_queues (c, !fwd);
4441 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4442 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4444 msg.peer1 = my_full_id;
4445 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4446 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4447 c->destroy = GNUNET_YES;
4452 /******************************************************************************/
4453 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4454 /******************************************************************************/
4457 * Free a transmission that was already queued with all resources
4458 * associated to the request.
4460 * @param queue Queue handler to cancel.
4461 * @param clear_cls Is it necessary to free associated cls?
4464 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4466 struct MeshPeer *peer;
4467 struct MeshFlowControl *fc;
4472 GNUNET_assert (NULL != queue->c);
4473 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4475 if (GNUNET_YES == clear_cls)
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4478 GNUNET_MESH_DEBUG_M2S (queue->type));
4479 switch (queue->type)
4481 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4482 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4483 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4484 GNUNET_break (GNUNET_YES == queue->c->destroy);
4486 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4487 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4488 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4489 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4490 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4491 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4493 GNUNET_free_non_null (queue->cls);
4498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4499 GNUNET_MESH_DEBUG_M2S (queue->type));
4503 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4507 if (NULL != queue->c)
4509 queue->c->pending_messages--;
4510 if (NULL != queue->c->t)
4512 queue->c->t->pending_messages--;
4516 GNUNET_free (queue);
4521 queue_send (void *cls, size_t size, void *buf)
4523 struct MeshPeer *peer = cls;
4524 struct MeshFlowControl *fc;
4525 struct MeshConnection *c;
4526 struct GNUNET_MessageHeader *msg;
4527 struct MeshPeerQueue *queue;
4528 struct MeshTunnel2 *t;
4529 const struct GNUNET_PeerIdentity *dst_id;
4535 peer->core_transmit = NULL;
4536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4538 if (NULL == buf || 0 == size)
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4545 queue = peer_get_first_message (peer);
4548 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4553 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4556 dst_id = GNUNET_PEER_resolve2 (peer->id);
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4558 /* Check if buffer size is enough for the message */
4559 if (queue->size > size)
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4562 peer->core_transmit =
4563 GNUNET_CORE_notify_transmit_ready (core_handle,
4566 GNUNET_TIME_UNIT_FOREVER_REL,
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4575 t = (NULL != c) ? c->t : NULL;
4579 switch (queue->type)
4581 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4582 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4583 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4584 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4585 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4586 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 GNUNET_MESH_DEBUG_M2S (queue->type));
4590 data_size = send_core_data_raw (queue->cls, size, buf);
4591 msg = (struct GNUNET_MessageHeader *) buf;
4592 type = ntohs (msg->type);
4594 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4596 if (connection_is_origin (c, GNUNET_YES))
4597 data_size = send_core_connection_create (queue->c, size, buf);
4599 data_size = send_core_data_raw (queue->cls, size, buf);
4601 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4603 if (connection_is_origin (c, GNUNET_NO))
4604 data_size = send_core_connection_ack (queue->c, size, buf);
4606 data_size = send_core_data_raw (queue->cls, size, buf);
4608 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4609 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4610 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4611 /* This should be encapsulted */
4616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4621 if (0 < drop_percent &&
4622 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4625 "Dropping message of type %s\n",
4626 GNUNET_MESH_DEBUG_M2S (queue->type));
4630 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4633 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4634 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4635 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4636 fc->last_pid_sent = pid;
4637 if (NULL != queue->ch)
4638 channel_send_client_ack (queue->ch, fwd);
4640 connection_send_ack (c, fwd);
4646 /* Free queue, but cls was freed by send_core_* */
4647 queue_destroy (queue, GNUNET_NO);
4649 /* If more data in queue, send next */
4650 queue = peer_get_first_message (peer);
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4654 if (NULL == peer->core_transmit) {
4655 peer->core_transmit =
4656 GNUNET_CORE_notify_transmit_ready(core_handle,
4659 GNUNET_TIME_UNIT_FOREVER_REL,
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4668 "* tmt rdy called somewhere else\n");
4670 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4672 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4674 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4679 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4681 GNUNET_SCHEDULER_cancel (fc->poll_task);
4682 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4687 c->pending_messages--;
4688 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4691 connection_destroy (c);
4697 t->pending_messages--;
4698 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4710 queue_add (void *cls, uint16_t type, size_t size,
4711 struct MeshConnection *c,
4712 struct MeshChannel *ch,
4715 struct MeshPeerQueue *queue;
4716 struct MeshFlowControl *fc;
4717 struct MeshPeer *peer;
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4721 "queue add %s %s (%u) on c %p, ch %p\n",
4722 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4723 GNUNET_assert (NULL != c);
4725 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4726 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4736 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4737 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4741 ( (NULL != ch->root && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4742 (NULL != ch->dest && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4747 if (fc->queue_n >= fc->queue_max && 0 == priority)
4749 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4753 "queue full: %u/%u\n",
4754 fc->queue_n, fc->queue_max);
4755 return; /* Drop this message */
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4760 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4761 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4764 "no buffer space (%u > %u): starting poll\n",
4765 fc->last_pid_sent + 1, fc->last_ack_recv);
4766 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4770 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4778 if (100 <= priority)
4779 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4781 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4783 if (NULL == peer->core_transmit)
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4786 "calling core tmt rdy towards %s\n",
4788 peer->core_transmit =
4789 GNUNET_CORE_notify_transmit_ready (core_handle,
4792 GNUNET_TIME_UNIT_FOREVER_REL,
4793 GNUNET_PEER_resolve2 (peer->id),
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4801 "core tmt rdy towards %s already called\n",
4805 c->pending_messages++;
4806 c->t->pending_messages++;
4812 /******************************************************************************/
4813 /******************** MESH NETWORK HANDLERS **************************/
4814 /******************************************************************************/
4818 * Generic handler for mesh network payload traffic.
4820 * @param t Tunnel on which we got this message.
4821 * @param message Unencryted data message.
4822 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4824 * @return GNUNET_OK to keep the connection open,
4825 * GNUNET_SYSERR to close it (signal serious error)
4828 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4830 struct MeshChannelReliability *rel;
4831 struct MeshChannel *ch;
4832 struct MeshClient *c;
4839 size = ntohs (msg->header.size);
4841 sizeof (struct GNUNET_MESH_Data) +
4842 sizeof (struct GNUNET_MessageHeader))
4847 type = ntohs (msg->header.type);
4848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4849 GNUNET_MESH_DEBUG_M2S (type));
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4851 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4854 ch = channel_get (t, ntohl (msg->chid));
4857 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4862 /* Initialize FWD/BCK data */
4863 c = fwd ? ch->dest : ch->root;
4864 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4865 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4873 tunnel_change_state (t, MESH_TUNNEL_READY);
4875 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4877 mid = ntohl (msg->mid);
4878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4880 if (GNUNET_NO == ch->reliable ||
4881 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4882 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4885 if (GNUNET_YES == ch->reliable)
4887 /* Is this the exact next expected messasge? */
4888 if (mid == *mid_recv)
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4891 *mid_recv = *mid_recv + 1;
4892 channel_send_client_data (ch, msg, fwd);
4893 channel_send_client_buffered_data (ch, c, rel);
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4898 channel_rel_add_buffered_data (msg, rel);
4901 else /* Tunnel unreliable, send to clients directly */
4903 channel_send_client_data (ch, msg, fwd);
4908 GNUNET_break_op (0);
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4910 " MID %u not expected (%u - %u), dropping!\n",
4911 mid, *mid_recv, *mid_recv + 64);
4914 channel_send_data_ack (ch, fwd);
4919 * Handler for mesh network traffic end-to-end ACKs.
4921 * @param t Tunnel on which we got this message.
4922 * @param message Data message.
4923 * @param fwd Is this a fwd ACK? (dest->orig)
4925 * @return GNUNET_OK to keep the connection open,
4926 * GNUNET_SYSERR to close it (signal serious error)
4929 handle_data_ack (struct MeshTunnel2 *t,
4930 const struct GNUNET_MESH_DataACK *msg, int fwd)
4932 struct MeshChannelReliability *rel;
4933 struct MeshReliableMessage *copy;
4934 struct MeshReliableMessage *next;
4935 struct MeshChannel *ch;
4940 type = ntohs (msg->header.type);
4941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4942 GNUNET_MESH_DEBUG_M2S (type));
4943 ch = channel_get (t, ntohl (msg->chid));
4946 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4949 ack = ntohl (msg->mid);
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4951 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4953 if (GNUNET_YES == fwd)
4966 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4968 if (GMC_is_pid_bigger (copy->mid, ack))
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4971 channel_rel_free_sent (rel, msg);
4975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4977 rel_message_free (copy);
4979 /* ACK client if needed */
4980 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4982 /* If some message was free'd, update the retransmission delay*/
4983 if (GNUNET_YES == work)
4985 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4987 GNUNET_SCHEDULER_cancel (rel->retry_task);
4988 if (NULL == rel->head_sent)
4990 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4994 struct GNUNET_TIME_Absolute new_target;
4995 struct GNUNET_TIME_Relative delay;
4997 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4998 MESH_RETRANSMIT_MARGIN);
4999 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5001 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5003 GNUNET_SCHEDULER_add_delayed (delay,
5004 &channel_retransmit_message,
5016 * Core handler for connection creation.
5018 * @param cls Closure (unused).
5019 * @param peer Sender (neighbor).
5020 * @param message Message.
5022 * @return GNUNET_OK to keep the connection open,
5023 * GNUNET_SYSERR to close it (signal serious error)
5026 handle_mesh_connection_create (void *cls,
5027 const struct GNUNET_PeerIdentity *peer,
5028 const struct GNUNET_MessageHeader *message)
5030 struct GNUNET_MESH_ConnectionCreate *msg;
5031 struct GNUNET_PeerIdentity *id;
5032 struct GNUNET_HashCode *cid;
5033 struct MeshPeerPath *path;
5034 struct MeshPeer *dest_peer;
5035 struct MeshPeer *orig_peer;
5036 struct MeshConnection *c;
5037 unsigned int own_pos;
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5044 size = ntohs (message->size);
5045 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5047 GNUNET_break_op (0);
5051 /* Calculate hops */
5052 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5053 if (size % sizeof (struct GNUNET_PeerIdentity))
5055 GNUNET_break_op (0);
5058 size /= sizeof (struct GNUNET_PeerIdentity);
5061 GNUNET_break_op (0);
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5066 /* Get parameters */
5067 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5069 id = (struct GNUNET_PeerIdentity *) &msg[1];
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5071 " connection %s (%s).\n",
5072 GNUNET_h2s (cid), GNUNET_i2s (id));
5074 /* Create connection */
5075 c = connection_get (cid);
5078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5079 c = connection_new (cid);
5083 connection_reset_timeout (c, GNUNET_YES);
5084 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5086 /* Remember peers */
5087 dest_peer = peer_get (&id[size - 1]);
5088 orig_peer = peer_get (&id[0]);
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5092 path = path_new (size);
5094 for (i = 0; i < size; i++)
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5097 GNUNET_i2s (&id[i]));
5098 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5099 if (path->peers[i] == myid)
5102 if (own_pos == 0 && path->peers[own_pos] != myid)
5104 /* create path: self not found in path through self */
5105 GNUNET_break_op (0);
5106 path_destroy (path);
5107 connection_destroy (c);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5111 path_add_to_peers (path, GNUNET_NO);
5112 c->path = path_duplicate (path);
5113 c->own_pos = own_pos;
5115 /* Is it a connection to us? */
5116 if (own_pos == size - 1)
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5119 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5121 if (NULL == orig_peer->tunnel)
5122 orig_peer->tunnel = tunnel_new ();
5123 tunnel_add_connection (orig_peer->tunnel, c);
5125 send_connection_ack (c, GNUNET_NO);
5127 /* Keep tunnel alive in direction dest->owner*/
5128 connection_reset_timeout (c, GNUNET_NO);
5132 /* It's for somebody else! Retransmit. */
5133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5134 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5135 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5136 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5143 * Core handler for path ACKs
5145 * @param cls closure
5146 * @param message message
5147 * @param peer peer identity this notification is about
5149 * @return GNUNET_OK to keep the connection open,
5150 * GNUNET_SYSERR to close it (signal serious error)
5153 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5154 const struct GNUNET_MessageHeader *message)
5156 struct GNUNET_MESH_ConnectionACK *msg;
5157 struct MeshPeerPath *p;
5158 struct MeshConnection *c;
5160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5161 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5163 GNUNET_h2s (&msg->cid));
5164 c = connection_get (&msg->cid);
5167 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5176 /* Add path to peers? */
5180 path_add_to_peers (p, GNUNET_YES);
5186 connection_change_state (c, MESH_CONNECTION_READY);
5187 connection_reset_timeout (c, GNUNET_NO);
5188 if (MESH_TUNNEL_READY != c->t->state)
5189 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5190 tunnel_send_queued_data (c->t, GNUNET_YES);
5192 /* Message for us? */
5193 if (connection_is_terminal (c, GNUNET_NO))
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5196 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5198 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5199 c->t->peer->dhtget = NULL;
5201 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5206 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5212 * Core handler for notifications of broken paths
5214 * @param cls Closure (unused).
5215 * @param peer Peer identity of sending neighbor.
5216 * @param message Message.
5218 * @return GNUNET_OK to keep the connection open,
5219 * GNUNET_SYSERR to close it (signal serious error)
5222 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5223 const struct GNUNET_MessageHeader *message)
5225 struct GNUNET_MESH_ConnectionBroken *msg;
5226 struct MeshConnection *c;
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5229 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5230 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5232 GNUNET_i2s (&msg->peer1));
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5234 GNUNET_i2s (&msg->peer2));
5235 c = connection_get (&msg->cid);
5238 GNUNET_break_op (0);
5241 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5242 GNUNET_PEER_search (&msg->peer2));
5249 * Core handler for tunnel destruction
5251 * @param cls Closure (unused).
5252 * @param peer Peer identity of sending neighbor.
5253 * @param message Message.
5255 * @return GNUNET_OK to keep the connection open,
5256 * GNUNET_SYSERR to close it (signal serious error)
5259 handle_mesh_connection_destroy (void *cls,
5260 const struct GNUNET_PeerIdentity *peer,
5261 const struct GNUNET_MessageHeader *message)
5263 struct GNUNET_MESH_ConnectionDestroy *msg;
5264 struct MeshConnection *c;
5268 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5270 "Got a CONNECTION DESTROY message from %s\n",
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5273 " for connection %s\n",
5274 GNUNET_h2s (&msg->cid));
5275 c = connection_get (&msg->cid);
5278 /* Probably already got the message from another path,
5279 * destroyed the tunnel and retransmitted to children.
5282 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5286 id = GNUNET_PEER_search (peer);
5287 if (id == connection_get_prev_hop (c)->id)
5289 else if (id == connection_get_next_hop (c)->id)
5293 GNUNET_break_op (0);
5296 send_prebuilt_message_connection (message, c, NULL, fwd);
5297 c->destroy = GNUNET_YES;
5304 * Handler for channel create messages.
5306 * @param t Tunnel this channel is to be created in.
5307 * @param msg Message.
5308 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5310 * @return GNUNET_OK to keep the connection open,
5311 * GNUNET_SYSERR to close it (signal serious error)
5314 handle_channel_create (struct MeshTunnel2 *t,
5315 struct GNUNET_MESH_ChannelCreate *msg,
5318 MESH_ChannelNumber chid;
5319 struct MeshChannel *ch;
5320 struct MeshClient *c;
5323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5324 /* Check message size */
5325 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5327 GNUNET_break_op (0);
5331 /* Check if channel exists */
5332 chid = ntohl (msg->chid);
5333 ch = channel_get (t, chid);
5336 /* Probably a retransmission, safe to ignore */
5337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5341 /* Create channel */
5342 ch = channel_new (t, NULL, 0);
5343 channel_set_options (ch, ntohl (msg->opt));
5346 /* Find a destination client */
5347 port = ntohl (msg->port);
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5349 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5352 /* TODO send reject */
5353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5357 channel_add_client (ch, c);
5358 if (GNUNET_YES == ch->reliable)
5360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5361 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5362 ch->bck_rel->ch = ch;
5363 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5366 send_local_channel_create (ch);
5367 channel_send_ack (ch, !fwd);
5374 * Handler for channel ack messages.
5376 * @param t Tunnel this channel is to be created in.
5377 * @param msg Message.
5378 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5380 * @return GNUNET_OK to keep the connection open,
5381 * GNUNET_SYSERR to close it (signal serious error)
5384 handle_channel_ack (struct MeshTunnel2 *t,
5385 struct GNUNET_MESH_ChannelManage *msg,
5388 MESH_ChannelNumber chid;
5389 struct MeshChannel *ch;
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5392 /* Check message size */
5393 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5395 GNUNET_break_op (0);
5399 /* Check if channel exists */
5400 chid = ntohl (msg->chid);
5401 ch = channel_get (t, chid);
5404 GNUNET_break_op (0);
5405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5409 channel_confirm (ch, !fwd);
5415 * Handler for channel destroy messages.
5417 * @param t Tunnel this channel is to be destroyed of.
5418 * @param msg Message.
5419 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5421 * @return GNUNET_OK to keep the connection open,
5422 * GNUNET_SYSERR to close it (signal serious error)
5425 handle_channel_destroy (struct MeshTunnel2 *t,
5426 struct GNUNET_MESH_ChannelManage *msg,
5429 MESH_ChannelNumber chid;
5430 struct MeshChannel *ch;
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 /* Probably a retransmission, safe to ignore */
5448 send_local_channel_destroy (ch, fwd);
5449 channel_destroy (ch);
5456 * Generic handler for mesh network encrypted traffic.
5458 * @param peer Peer identity this notification is about.
5459 * @param message Encrypted message.
5460 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5462 * @return GNUNET_OK to keep the connection open,
5463 * GNUNET_SYSERR to close it (signal serious error)
5466 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5467 const struct GNUNET_MESH_Encrypted *msg,
5470 struct MeshConnection *c;
5471 struct MeshTunnel2 *t;
5472 struct MeshPeer *neighbor;
5473 struct MeshFlowControl *fc;
5480 size = ntohs (msg->header.size);
5482 sizeof (struct GNUNET_MESH_Encrypted) +
5483 sizeof (struct GNUNET_MessageHeader))
5488 type = ntohs (msg->header.type);
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5490 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5492 /* Check connection */
5493 c = connection_get (&msg->cid);
5496 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5501 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5503 /* Check if origin is as expected */
5504 neighbor = connection_get_hop (c, fwd);
5505 if (peer_get (peer)->id != neighbor->id)
5507 GNUNET_break_op (0);
5512 pid = ntohl (msg->pid);
5513 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5515 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5517 "WARNING Received PID %u, (prev %u), ACK %u\n",
5518 pid, fc->last_pid_recv, fc->last_ack_sent);
5521 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5523 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5525 " Pid %u not expected (%u+), dropping!\n",
5526 pid, fc->last_pid_recv + 1);
5529 if (MESH_CONNECTION_SENT == c->state)
5530 connection_change_state (c, MESH_CONNECTION_READY);
5531 connection_reset_timeout (c, fwd);
5532 fc->last_pid_recv = pid;
5534 /* Is this message for us? */
5535 if (connection_is_terminal (c, fwd))
5537 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5539 struct GNUNET_MessageHeader *msgh;
5542 /* TODO signature verification */
5543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us!\n");
5544 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5546 fc->last_pid_recv = pid;
5547 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5548 msgh = (struct GNUNET_MessageHeader *) cbuf;
5549 switch (ntohs (msgh->type))
5551 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5552 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5555 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5556 r = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5559 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5560 r = handle_channel_create (t,
5561 (struct GNUNET_MESH_ChannelCreate *) msgh,
5565 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5566 r = handle_channel_ack (t,
5567 (struct GNUNET_MESH_ChannelManage *) msgh,
5571 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5572 r = handle_channel_destroy (t,
5573 (struct GNUNET_MESH_ChannelManage *) msgh,
5578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5579 "end-to-end message not known (%u)\n",
5580 ntohs (msgh->type));
5584 connection_send_ack (c, fwd);
5588 /* Message not for us: forward to next hop */
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5590 ttl = ntohl (msg->ttl);
5591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5594 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5596 connection_send_ack (c, fwd);
5599 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5601 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5608 * Core handler for mesh network traffic going orig->dest.
5610 * @param cls Closure (unused).
5611 * @param message Message received.
5612 * @param peer Peer who sent the message.
5614 * @return GNUNET_OK to keep the connection open,
5615 * GNUNET_SYSERR to close it (signal serious error)
5618 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5619 const struct GNUNET_MessageHeader *message)
5621 return handle_mesh_encrypted (peer,
5622 (struct GNUNET_MESH_Encrypted *)message,
5627 * Core handler for mesh network traffic going dest->orig.
5629 * @param cls Closure (unused).
5630 * @param message Message received.
5631 * @param peer Peer who sent the message.
5633 * @return GNUNET_OK to keep the connection open,
5634 * GNUNET_SYSERR to close it (signal serious error)
5637 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5638 const struct GNUNET_MessageHeader *message)
5640 return handle_mesh_encrypted (peer,
5641 (struct GNUNET_MESH_Encrypted *)message,
5647 * Core handler for mesh network traffic point-to-point acks.
5649 * @param cls closure
5650 * @param message message
5651 * @param peer peer identity this notification is about
5653 * @return GNUNET_OK to keep the connection open,
5654 * GNUNET_SYSERR to close it (signal serious error)
5657 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5658 const struct GNUNET_MessageHeader *message)
5660 struct GNUNET_MESH_ACK *msg;
5661 struct MeshConnection *c;
5662 struct MeshFlowControl *fc;
5666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5668 msg = (struct GNUNET_MESH_ACK *) message;
5670 c = connection_get (&msg->cid);
5674 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5679 ack = ntohl (msg->ack);
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5682 /* Is this a forward or backward ACK? */
5683 id = GNUNET_PEER_search (peer);
5684 if (connection_get_next_hop (c)->id == id)
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5689 else if (connection_get_prev_hop (c)->id == id)
5691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5696 GNUNET_break_op (0);
5700 /* Cancel polling if the ACK is bigger than before. */
5701 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5702 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5704 GNUNET_SCHEDULER_cancel (fc->poll_task);
5705 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5706 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5709 fc->last_ack_recv = ack;
5710 connection_unlock_queue (c, fc == &c->fwd_fc);
5717 * Core handler for mesh network traffic point-to-point ack polls.
5719 * @param cls closure
5720 * @param message message
5721 * @param peer peer identity this notification is about
5723 * @return GNUNET_OK to keep the connection open,
5724 * GNUNET_SYSERR to close it (signal serious error)
5727 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5728 const struct GNUNET_MessageHeader *message)
5730 struct GNUNET_MESH_Poll *msg;
5731 struct MeshConnection *c;
5732 struct MeshFlowControl *fc;
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5739 msg = (struct GNUNET_MESH_Poll *) message;
5741 c = connection_get (&msg->cid);
5745 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5747 GNUNET_break_op (0);
5751 /* Is this a forward or backward ACK? */
5752 id = GNUNET_PEER_search (peer);
5753 if (connection_get_next_hop (c)->id == id)
5755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5758 else if (connection_get_prev_hop (c)->id == id)
5760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5765 GNUNET_break_op (0);
5769 pid = ntohl (msg->pid);
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5771 pid, fc->last_pid_recv);
5772 fc->last_pid_recv = pid;
5773 connection_send_ack (c, fc == &c->fwd_fc);
5780 * Core handler for mesh keepalives.
5782 * @param cls closure
5783 * @param message message
5784 * @param peer peer identity this notification is about
5785 * @return GNUNET_OK to keep the connection open,
5786 * GNUNET_SYSERR to close it (signal serious error)
5788 * TODO: Check who we got this from, to validate route.
5791 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5792 const struct GNUNET_MessageHeader *message)
5794 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5795 struct MeshConnection *c;
5796 struct MeshPeer *neighbor;
5799 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5803 c = connection_get (&msg->cid);
5806 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5811 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5812 GNUNET_YES : GNUNET_NO;
5814 /* Check if origin is as expected */
5815 neighbor = connection_get_hop (c, fwd);
5816 if (peer_get (peer)->id != neighbor->id)
5818 GNUNET_break_op (0);
5822 connection_change_state (c, MESH_CONNECTION_READY);
5823 connection_reset_timeout (c, fwd);
5825 if (connection_is_terminal (c, fwd))
5828 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5829 send_prebuilt_message_connection (message, c, NULL, fwd);
5837 * Functions to handle messages from core
5839 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5840 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5842 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5843 sizeof (struct GNUNET_MESH_ConnectionACK)},
5844 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5845 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5846 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5847 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5848 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5849 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5850 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5851 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5852 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5853 sizeof (struct GNUNET_MESH_ACK)},
5854 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5855 sizeof (struct GNUNET_MESH_Poll)},
5856 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5857 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5863 * Function to process paths received for a new peer addition. The recorded
5864 * paths form the initial tunnel, which can be optimized later.
5865 * Called on each result obtained for the DHT search.
5867 * @param cls closure
5868 * @param exp when will this value expire
5869 * @param key key of the result
5870 * @param get_path path of the get request
5871 * @param get_path_length lenght of get_path
5872 * @param put_path path of the put request
5873 * @param put_path_length length of the put_path
5874 * @param type type of the result
5875 * @param size number of bytes in data
5876 * @param data pointer to the result data
5879 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5880 const struct GNUNET_HashCode * key,
5881 const struct GNUNET_PeerIdentity *get_path,
5882 unsigned int get_path_length,
5883 const struct GNUNET_PeerIdentity *put_path,
5884 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5885 size_t size, const void *data)
5887 struct MeshPeer *peer = cls;
5888 struct MeshPeerPath *p;
5889 struct MeshConnection *c;
5890 struct GNUNET_PeerIdentity pi;
5893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5894 GNUNET_PEER_resolve (peer->id, &pi);
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5897 p = path_build_from_dht (get_path, get_path_length,
5898 put_path, put_path_length);
5899 path_add_to_peers (p, GNUNET_NO);
5902 /* Count connections */
5903 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5905 /* If we already have 3 (or more (?!)) connections, it's enough */
5909 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5912 peer_connect (peer);
5918 /******************************************************************************/
5919 /********************* MESH LOCAL HANDLES **************************/
5920 /******************************************************************************/
5924 * Handler for client connection.
5926 * @param cls Closure (unused).
5927 * @param client Client handler.
5930 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5932 struct MeshClient *c;
5936 c = GNUNET_malloc (sizeof (struct MeshClient));
5938 c->id = next_client_id++; /* overflow not important: just for debug */
5939 GNUNET_SERVER_client_keep (client);
5940 GNUNET_SERVER_client_set_user_context (client, c);
5941 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5946 * Handler for client disconnection
5948 * @param cls closure
5949 * @param client identification of the client; NULL
5950 * for the last call when the server is destroyed
5953 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5955 struct MeshClient *c;
5957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5964 c = client_get (client);
5967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5969 GNUNET_SERVER_client_drop (c->handle);
5970 c->shutting_down = GNUNET_YES;
5971 if (NULL != c->own_channels)
5973 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
5974 &channel_destroy_iterator, c);
5975 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
5978 if (NULL != c->incoming_channels)
5980 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
5981 &channel_destroy_iterator, c);
5982 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
5985 if (NULL != c->ports)
5987 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5988 &client_release_ports, c);
5989 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5991 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5992 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6006 * Handler for new clients
6008 * @param cls closure
6009 * @param client identification of the client
6010 * @param message the actual message, which includes messages the client wants
6013 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6014 const struct GNUNET_MessageHeader *message)
6016 struct GNUNET_MESH_ClientConnect *cc_msg;
6017 struct MeshClient *c;
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6024 /* Check data sanity */
6025 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6026 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6027 if (0 != (size % sizeof (uint32_t)))
6030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6033 size /= sizeof (uint32_t);
6035 /* Initialize new client structure */
6036 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6043 p = (uint32_t *) &cc_msg[1];
6044 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6045 for (i = 0; i < size; i++)
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6050 /* store in client's hashmap */
6051 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6052 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6053 /* store in global hashmap */
6054 /* FIXME only allow one client to have the port open,
6055 * have a backup hashmap with waiting clients */
6056 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6057 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6061 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6062 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6063 GNUNET_SERVER_notification_context_add (nc, client);
6064 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6066 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6072 * Handler for requests of new tunnels
6074 * @param cls Closure.
6075 * @param client Identification of the client.
6076 * @param message The actual message.
6079 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6080 const struct GNUNET_MessageHeader *message)
6082 struct GNUNET_MESH_ChannelMessage *msg;
6083 struct MeshPeer *peer;
6084 struct MeshTunnel2 *t;
6085 struct MeshChannel *ch;
6086 struct MeshClient *c;
6087 MESH_ChannelNumber chid;
6089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6091 /* Sanity check for client registration */
6092 if (NULL == (c = client_get (client)))
6095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6100 /* Message size sanity check */
6101 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6108 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6110 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6111 chid = ntohl (msg->channel_id);
6113 /* Sanity check for duplicate channel IDs */
6114 if (NULL != channel_get_by_local_id (c, chid))
6117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6121 peer = peer_get (&msg->peer);
6122 if (NULL == peer->tunnel)
6124 peer->tunnel = tunnel_new ();
6125 peer->tunnel->peer = peer;
6129 /* Create channel */
6130 ch = channel_new (t, c, chid);
6134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6137 ch->port = ntohl (msg->port);
6138 channel_set_options (ch, ntohl (msg->opt));
6140 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6141 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
6142 ch->fwd_rel->ch = ch;
6143 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
6145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6146 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6147 peer_connect (peer);
6149 /* Send create channel */
6151 struct GNUNET_MESH_ChannelCreate msgcc;
6153 msgcc.header.size = htons (sizeof (msgcc));
6154 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6155 msgcc.chid = htonl (ch->gid);
6156 msgcc.port = msg->port;
6157 msgcc.opt = msg->opt;
6159 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6168 * Handler for requests of deleting tunnels
6170 * @param cls closure
6171 * @param client identification of the client
6172 * @param message the actual message
6175 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6176 const struct GNUNET_MessageHeader *message)
6178 struct GNUNET_MESH_ChannelMessage *msg;
6179 struct MeshClient *c;
6180 struct MeshChannel *ch;
6181 struct MeshTunnel2 *t;
6182 MESH_ChannelNumber chid;
6184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6185 "Got a DESTROY CHANNEL from client!\n");
6187 /* Sanity check for client registration */
6188 if (NULL == (c = client_get (client)))
6191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6196 /* Message sanity check */
6197 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6200 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6204 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6206 /* Retrieve tunnel */
6207 chid = ntohl (msg->channel_id);
6208 ch = channel_get_by_local_id (c, chid);
6211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6217 /* Cleanup after the tunnel */
6218 client_delete_channel (c, ch);
6219 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6223 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6230 " channel %X client %p (%p, %p)\n",
6231 chid, c, ch->root, ch->dest);
6236 channel_destroy (ch);
6237 tunnel_destroy_if_empty (t);
6239 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6245 * Handler for client traffic
6247 * @param cls closure
6248 * @param client identification of the client
6249 * @param message the actual message
6252 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6253 const struct GNUNET_MessageHeader *message)
6255 struct GNUNET_MESH_LocalData *msg;
6256 struct MeshClient *c;
6257 struct MeshChannel *ch;
6258 MESH_ChannelNumber chid;
6262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6263 "Got data from a client!\n");
6265 /* Sanity check for client registration */
6266 if (NULL == (c = client_get (client)))
6269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6274 msg = (struct GNUNET_MESH_LocalData *) message;
6276 /* Sanity check for message size */
6277 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6278 if (size < sizeof (struct GNUNET_MessageHeader))
6281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6285 /* Channel exists? */
6286 chid = ntohl (msg->id);
6287 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6288 ch = channel_get_by_local_id (c, chid);
6292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6296 /* Is the client in the channel? */
6299 ch->root->handle == client)
6303 ch->dest->handle == client) ) )
6306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6311 ch->blocked_fwd = GNUNET_YES;
6313 ch->blocked_bck = GNUNET_YES;
6315 /* Ok, everything is correct, send the message. */
6317 struct GNUNET_MESH_Data *payload;
6318 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6319 unsigned char cbuf[p2p_size];
6322 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6323 payload = (struct GNUNET_MESH_Data *) cbuf;
6324 payload->mid = htonl (*mid);
6326 memcpy (&payload[1], &msg[1], size);
6327 payload->header.size = htons (p2p_size);
6328 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6329 payload->chid = htonl (ch->gid);
6330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6331 send_prebuilt_message_channel (&payload->header, ch, fwd);
6333 if (GNUNET_YES == ch->reliable)
6334 channel_save_copy (ch, &payload->header, fwd);
6336 if (tunnel_get_buffer (ch->t, fwd) > 0)
6337 send_local_ack (ch, c, fwd);
6338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6339 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6346 * Handler for client's ACKs for payload traffic.
6348 * @param cls Closure (unused).
6349 * @param client Identification of the client.
6350 * @param message The actual message.
6353 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6354 const struct GNUNET_MessageHeader *message)
6356 struct GNUNET_MESH_LocalAck *msg;
6357 struct MeshChannelReliability *rel;
6358 struct MeshChannel *ch;
6359 struct MeshClient *c;
6360 MESH_ChannelNumber chid;
6363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6365 /* Sanity check for client registration */
6366 if (NULL == (c = client_get (client)))
6369 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6374 msg = (struct GNUNET_MESH_LocalAck *) message;
6376 /* Channel exists? */
6377 chid = ntohl (msg->channel_id);
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6379 ch = channel_get_by_local_id (c, chid);
6383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6389 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6390 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6392 rel->client_ready = GNUNET_YES;
6393 channel_send_client_buffered_data (ch, c, rel);
6394 channel_send_connection_ack (ch, 64 - rel->n_recv, fwd);
6396 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6403 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6405 * @param cls Closure (client handle).
6406 * @param key Key (hashed tunnel ID, unused).
6407 * @param value Tunnel info.
6409 * @return GNUNET_YES, to keep iterating.
6412 // monitor_all_tunnels_iterator (void *cls,
6413 // const struct GNUNET_HashCode * key,
6416 // struct GNUNET_SERVER_Client *client = cls;
6417 // struct MeshChannel *ch = value;
6418 // struct GNUNET_MESH_LocalMonitor *msg;
6420 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6421 // msg->channel_id = htonl (ch->gid);
6422 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6423 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6425 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6426 // "* sending info about tunnel %s\n",
6427 // GNUNET_i2s (&msg->owner));
6429 // GNUNET_SERVER_notification_context_unicast (nc, client,
6430 // &msg->header, GNUNET_NO);
6431 // return GNUNET_YES;
6436 * Handler for client's MONITOR request.
6438 * @param cls Closure (unused).
6439 * @param client Identification of the client.
6440 * @param message The actual message.
6443 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6444 const struct GNUNET_MessageHeader *message)
6446 struct MeshClient *c;
6448 /* Sanity check for client registration */
6449 if (NULL == (c = client_get (client)))
6452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6456 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6457 "Received get tunnels request from client %u\n",
6459 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6460 // monitor_all_tunnels_iterator,
6462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6463 "Get tunnels request from client %u completed\n",
6465 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6470 * Handler for client's MONITOR_TUNNEL request.
6472 * @param cls Closure (unused).
6473 * @param client Identification of the client.
6474 * @param message The actual message.
6477 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6478 const struct GNUNET_MessageHeader *message)
6480 const struct GNUNET_MESH_LocalMonitor *msg;
6481 struct GNUNET_MESH_LocalMonitor *resp;
6482 struct MeshClient *c;
6483 struct MeshChannel *ch;
6485 /* Sanity check for client registration */
6486 if (NULL == (c = client_get (client)))
6489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6494 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6495 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6498 ntohl (msg->channel_id));
6499 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6503 /* We don't know the tunnel */
6504 struct GNUNET_MESH_LocalMonitor warn;
6507 GNUNET_SERVER_notification_context_unicast (nc, client,
6510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6514 /* Initialize context */
6515 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6517 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6518 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6519 &resp->header, GNUNET_NO);
6522 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6523 "Monitor tunnel request from client %u completed\n",
6525 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6530 * Functions to handle messages from clients
6532 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6533 {&handle_local_new_client, NULL,
6534 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6535 {&handle_local_channel_create, NULL,
6536 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6537 sizeof (struct GNUNET_MESH_ChannelMessage)},
6538 {&handle_local_channel_destroy, NULL,
6539 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6540 sizeof (struct GNUNET_MESH_ChannelMessage)},
6541 {&handle_local_data, NULL,
6542 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6543 {&handle_local_ack, NULL,
6544 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6545 sizeof (struct GNUNET_MESH_LocalAck)},
6546 {&handle_local_get_tunnels, NULL,
6547 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6548 sizeof (struct GNUNET_MessageHeader)},
6549 {&handle_local_show_tunnel, NULL,
6550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6551 sizeof (struct GNUNET_MESH_LocalMonitor)},
6557 * Method called whenever a given peer connects.
6559 * @param cls closure
6560 * @param peer peer identity this notification is about
6563 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6565 struct MeshPeer *pi;
6566 struct MeshPeerPath *path;
6568 DEBUG_CONN ("Peer connected\n");
6569 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6570 pi = peer_get (peer);
6573 DEBUG_CONN (" (self)\n");
6574 path = path_new (1);
6578 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6579 path = path_new (2);
6580 path->peers[1] = pi->id;
6581 GNUNET_PEER_change_rc (pi->id, 1);
6582 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6584 path->peers[0] = myid;
6585 GNUNET_PEER_change_rc (myid, 1);
6586 peer_add_path (pi, path, GNUNET_YES);
6588 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6594 * Method called whenever a peer disconnects.
6596 * @param cls closure
6597 * @param peer peer identity this notification is about
6600 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6602 struct MeshPeer *pi;
6604 DEBUG_CONN ("Peer disconnected\n");
6605 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6612 peer_remove_path (pi, myid, pi->id);
6614 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6617 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6618 pi->connections = NULL;
6621 DEBUG_CONN (" (self)\n");
6623 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6630 * Install server (service) handlers and start listening to clients.
6635 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6636 GNUNET_SERVER_connect_notify (server_handle,
6637 &handle_local_client_connect, NULL);
6638 GNUNET_SERVER_disconnect_notify (server_handle,
6639 &handle_local_client_disconnect, NULL);
6640 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6642 clients_head = NULL;
6643 clients_tail = NULL;
6645 GNUNET_SERVER_resume (server_handle);
6650 * To be called on core init/fail.
6652 * @param cls Closure (config)
6653 * @param identity the public identity of this peer
6656 core_init (void *cls,
6657 const struct GNUNET_PeerIdentity *identity)
6659 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6663 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6668 GNUNET_i2s (identity));
6669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6671 GNUNET_i2s (&my_full_id));
6672 GNUNET_CORE_disconnect (core_handle);
6673 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6674 NULL, /* Closure passed to MESH functions */
6675 &core_init, /* Call core_init once connected */
6676 &core_connect, /* Handle connects */
6677 &core_disconnect, /* remove peers on disconnects */
6678 NULL, /* Don't notify about all incoming messages */
6679 GNUNET_NO, /* For header only in notification */
6680 NULL, /* Don't notify about all outbound messages */
6681 GNUNET_NO, /* For header-only out notification */
6682 core_handlers); /* Register these handlers */
6691 /******************************************************************************/
6692 /************************ MAIN FUNCTIONS ****************************/
6693 /******************************************************************************/
6696 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6698 * @param cls closure
6699 * @param key current key code
6700 * @param value value in the hash map
6701 * @return GNUNET_YES if we should continue to iterate,
6705 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6707 struct MeshPeer *p = value;
6708 struct MeshTunnel2 *t = p->tunnel;
6717 * Task run during shutdown.
6723 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6727 if (core_handle != NULL)
6729 GNUNET_CORE_disconnect (core_handle);
6732 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6733 if (dht_handle != NULL)
6735 GNUNET_DHT_disconnect (dht_handle);
6740 GNUNET_SERVER_notification_context_destroy (nc);
6743 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6745 GNUNET_SCHEDULER_cancel (announce_id_task);
6746 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6753 * Process mesh requests.
6755 * @param cls closure
6756 * @param server the initialized server
6757 * @param c configuration to use
6760 run (void *cls, struct GNUNET_SERVER_Handle *server,
6761 const struct GNUNET_CONFIGURATION_Handle *c)
6764 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6767 server_handle = server;
6768 GNUNET_SERVER_suspend (server_handle);
6771 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6776 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6777 "mesh", "peer/privatekey");
6778 GNUNET_SCHEDULER_shutdown ();
6783 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6784 &refresh_connection_time))
6786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6788 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6789 "mesh", "refresh path time");
6790 GNUNET_SCHEDULER_shutdown ();
6795 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6800 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6801 "mesh", "id announce time");
6802 GNUNET_SCHEDULER_shutdown ();
6807 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6812 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6813 "mesh", "connect timeout");
6814 GNUNET_SCHEDULER_shutdown ();
6819 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6822 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6824 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6825 "mesh", "max msgs queue");
6826 GNUNET_SCHEDULER_shutdown ();
6831 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6836 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6837 "mesh", "max tunnels");
6838 GNUNET_SCHEDULER_shutdown ();
6843 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6848 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6849 "mesh", "default ttl", 64);
6854 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6858 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6859 "mesh", "max peers", 1000);
6864 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6872 "Mesh is running with drop mode enabled. "
6873 "This is NOT a good idea! "
6874 "Remove the DROP_PERCENT option from your configuration.\n");
6878 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6879 &dht_replication_level))
6881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6883 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6884 "mesh", "dht replication level", 3);
6885 dht_replication_level = 3;
6888 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6889 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6890 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6892 dht_handle = GNUNET_DHT_connect (c, 64);
6893 if (NULL == dht_handle)
6897 stats = GNUNET_STATISTICS_create ("mesh", c);
6899 /* Scheduled the task to clean up when shutdown is called */
6900 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6902 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6903 GNUNET_free (keyfile);
6904 GNUNET_assert (NULL != pk);
6905 my_private_key = pk;
6906 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6907 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6908 &my_full_id.hashPubKey);
6909 myid = GNUNET_PEER_intern (&my_full_id);
6910 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6911 "Mesh for peer [%s] starting\n",
6912 GNUNET_i2s(&my_full_id));
6914 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6915 NULL, /* Closure passed to MESH functions */
6916 &core_init, /* Call core_init once connected */
6917 &core_connect, /* Handle connects */
6918 &core_disconnect, /* remove peers on disconnects */
6919 NULL, /* Don't notify about all incoming messages */
6920 GNUNET_NO, /* For header only in notification */
6921 NULL, /* Don't notify about all outbound messages */
6922 GNUNET_NO, /* For header-only out notification */
6923 core_handlers); /* Register these handlers */
6924 if (NULL == core_handle)
6927 GNUNET_SCHEDULER_shutdown ();
6930 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6936 * The main function for the mesh service.
6938 * @param argc number of arguments from the command line
6939 * @param argv command line arguments
6940 * @return 0 ok, 1 on error
6943 main (int argc, char *const *argv)
6948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6949 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6951 ret = (GNUNET_OK == r) ? 0 : 1;
6952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");