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)),
1832 if (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state)
1833 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1834 if (MESH_CONNECTION_NEW == connection->state)
1835 connection_change_state (connection, MESH_CONNECTION_SENT);
1840 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1843 * @param connection Connection to confirm.
1844 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1847 send_connection_ack (struct MeshConnection *connection, int fwd)
1849 struct MeshTunnel2 *t;
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1854 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1855 sizeof (struct GNUNET_MESH_ConnectionACK),
1859 if (MESH_TUNNEL_NEW == t->state)
1860 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1865 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1867 * @param c Which connection to send the hop-by-hop ACK.
1868 * @param ack Value of the ACK.
1869 * @param fwd Is this fwd?
1872 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1874 struct GNUNET_MESH_ACK msg;
1876 msg.header.size = htons (sizeof (msg));
1877 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1878 msg.ack = htonl (ack);
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "connection send %s ack %u on %s\n",
1883 fwd ? "FWD" : "BCK", ack, GNUNET_h2s (&c->id));
1885 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1890 * Core callback to write a pre-constructed data packet to core buffer
1892 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1893 * @param size Number of bytes available in buf.
1894 * @param buf Where the to write the message.
1896 * @return number of bytes written to buf
1899 send_core_data_raw (void *cls, size_t size, void *buf)
1901 struct GNUNET_MessageHeader *msg = cls;
1904 GNUNET_assert (NULL != msg);
1905 total_size = ntohs (msg->size);
1907 if (total_size > size)
1912 memcpy (buf, msg, total_size);
1919 * Function to send a create connection message to a peer.
1921 * @param c Connection to create.
1922 * @param size number of bytes available in buf
1923 * @param buf where the callee should write the message
1924 * @return number of bytes written to buf
1927 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1929 struct GNUNET_MESH_ConnectionCreate *msg;
1930 struct GNUNET_PeerIdentity *peer_ptr;
1931 struct MeshPeerPath *p = c->path;
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1937 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1938 p->length * sizeof (struct GNUNET_PeerIdentity);
1940 if (size < size_needed || NULL == buf)
1945 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1946 msg->header.size = htons (size_needed);
1947 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1950 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1951 for (i = 0; i < p->length; i++)
1953 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1963 * Creates a path ack message in buf and frees all unused resources.
1965 * @param c Connection to send an ACK on.
1966 * @param size number of bytes available in buf
1967 * @param buf where the callee should write the message
1969 * @return number of bytes written to buf
1972 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1974 struct GNUNET_MESH_ConnectionACK *msg = buf;
1975 struct MeshTunnel2 *t = c->t;
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1978 GNUNET_assert (NULL != t);
1979 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1984 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1985 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1988 /* TODO add signature */
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1991 return sizeof (struct GNUNET_MESH_ConnectionACK);
1996 * Destroy the peer_info and free any allocated resources linked to it
1998 * @param peer The peer_info to destroy.
2000 * @return GNUNET_OK on success
2003 peer_destroy (struct MeshPeer *peer)
2005 struct GNUNET_PeerIdentity id;
2006 struct MeshPeerPath *p;
2007 struct MeshPeerPath *nextp;
2009 GNUNET_PEER_resolve (peer->id, &id);
2010 GNUNET_PEER_change_rc (peer->id, -1);
2013 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2016 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2017 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2019 if (NULL != peer->dhtget)
2021 GNUNET_DHT_get_stop (peer->dhtget);
2023 p = peer->path_head;
2027 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2031 tunnel_destroy_empty (peer->tunnel);
2038 * Returns if peer is used (has a tunnel, is neighbor).
2040 * @peer Peer to check.
2042 * @return GNUNET_YES if peer is in use.
2045 peer_is_used (struct MeshPeer *peer)
2047 struct MeshPeerPath *p;
2049 if (NULL != peer->tunnel)
2052 for (p = peer->path_head; NULL != p; p = p->next)
2061 * Iterator over all the peers to get the oldest timestamp.
2063 * @param cls Closure (unsued).
2064 * @param key ID of the peer.
2065 * @param value Peer_Info of the peer.
2068 peer_get_oldest (void *cls,
2069 const struct GNUNET_HashCode *key,
2072 struct MeshPeer *p = value;
2073 struct GNUNET_TIME_Absolute *abs = cls;
2075 /* Don't count active peers */
2076 if (GNUNET_YES == peer_is_used (p))
2079 if (abs->abs_value_us < p->last_contact.abs_value_us)
2080 abs->abs_value_us = p->last_contact.abs_value_us;
2087 * Iterator over all the peers to remove the oldest entry.
2089 * @param cls Closure (unsued).
2090 * @param key ID of the peer.
2091 * @param value Peer_Info of the peer.
2094 peer_timeout (void *cls,
2095 const struct GNUNET_HashCode *key,
2098 struct MeshPeer *p = value;
2099 struct GNUNET_TIME_Absolute *abs = cls;
2101 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2102 GNUNET_NO == peer_is_used (p))
2112 * Delete oldest unused peer.
2115 peer_delete_oldest (void)
2117 struct GNUNET_TIME_Absolute abs;
2119 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2121 GNUNET_CONTAINER_multihashmap_iterate (peers,
2124 GNUNET_CONTAINER_multihashmap_iterate (peers,
2131 * Retrieve the MeshPeer stucture associated with the peer, create one
2132 * and insert it in the appropriate structures if the peer is not known yet.
2134 * @param peer Full identity of the peer.
2136 * @return Existing or newly created peer info.
2138 static struct MeshPeer *
2139 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2141 struct MeshPeer *peer;
2143 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2146 peer = GNUNET_new (struct MeshPeer);
2147 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2149 peer_delete_oldest ();
2151 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2153 peer->id = GNUNET_PEER_intern (peer_id);
2155 peer->last_contact = GNUNET_TIME_absolute_get();
2162 * Retrieve the MeshPeer stucture associated with the peer, create one
2163 * and insert it in the appropriate structures if the peer is not known yet.
2165 * @param peer Short identity of the peer.
2167 * @return Existing or newly created peer info.
2169 static struct MeshPeer *
2170 peer_get_short (const GNUNET_PEER_Id peer)
2172 return peer_get (GNUNET_PEER_resolve2 (peer));
2177 * Get a cost of a path for a peer considering existing tunnel connections.
2179 * @param peer Peer towards which the path is considered.
2180 * @param path Candidate path.
2182 * @return Cost of the path (path length + number of overlapping nodes)
2185 peer_get_path_cost (const struct MeshPeer *peer,
2186 const struct MeshPeerPath *path)
2188 struct MeshConnection *c;
2189 unsigned int overlap;
2197 GNUNET_assert (NULL != peer->tunnel);
2199 for (i = 0; i < path->length; i++)
2201 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2203 for (j = 0; j < c->path->length; j++)
2205 if (path->peers[i] == c->path->peers[j])
2213 return (path->length + overlap) * (path->score * -1);
2218 * Choose the best path towards a peer considering the tunnel properties.
2220 * @param peer The destination peer.
2222 * @return Best current known path towards the peer, if any.
2224 static struct MeshPeerPath *
2225 peer_get_best_path (const struct MeshPeer *peer)
2227 struct MeshPeerPath *best_p;
2228 struct MeshPeerPath *p;
2229 struct MeshConnection *c;
2230 unsigned int best_cost;
2233 best_cost = UINT_MAX;
2235 for (p = peer->path_head; NULL != p; p = p->next)
2237 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2241 continue; /* If path is in use in a connection, skip it. */
2243 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2253 queue_is_sendable (struct MeshPeerQueue *q)
2255 struct MeshFlowControl *fc;
2257 /* Is PID-independent? */
2260 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2261 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2265 /* Is PID allowed? */
2266 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2267 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2275 * Get first sendable message.
2277 * @param peer The destination peer.
2279 * @return Best current known path towards the peer, if any.
2281 static struct MeshPeerQueue *
2282 peer_get_first_message (const struct MeshPeer *peer)
2284 struct MeshPeerQueue *q;
2286 for (q = peer->queue_head; NULL != q; q = q->next)
2288 if (queue_is_sendable (q))
2297 * Try to establish a new connection to this peer in the given tunnel.
2298 * If the peer doesn't have any path to it yet, try to get one.
2299 * If the peer already has some path, send a CREATE CONNECTION towards it.
2301 * @param peer PeerInfo of the peer.
2304 peer_connect (struct MeshPeer *peer)
2306 struct MeshTunnel2 *t;
2307 struct MeshPeerPath *p;
2308 struct MeshConnection *c;
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 "peer_connect towards %s\n",
2314 if (NULL != peer->path_head)
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2317 p = peer_get_best_path (peer);
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2321 c = tunnel_use_path (t, p);
2322 send_connection_create (c);
2325 else if (NULL == peer->dhtget)
2327 const struct GNUNET_PeerIdentity *id;
2329 id = GNUNET_PEER_resolve2 (peer->id);
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 " Starting DHT GET for peer %s\n", peer2s (peer));
2332 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2333 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2334 &id->hashPubKey, /* key to search */
2335 dht_replication_level, /* replication level */
2336 GNUNET_DHT_RO_RECORD_ROUTE |
2337 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2339 0, /* xquery bits */
2340 &dht_get_id_handler, peer);
2341 if (MESH_TUNNEL_NEW == t->state)
2342 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 "There is no path but the DHT GET is already started.\n");
2353 * Get the first transmittable message for a connection.
2355 * @param c Connection.
2356 * @param fwd Is this FWD?
2358 * @return First transmittable message.
2360 static struct MeshPeerQueue *
2361 connection_get_first_message (struct MeshConnection *c, int fwd)
2363 struct MeshPeerQueue *q;
2366 p = connection_get_hop (c, fwd);
2368 for (q = p->queue_head; NULL != q; q = q->next)
2372 if (queue_is_sendable (q))
2381 * Is this peer the first one on the connection?
2383 * @param c Connection.
2384 * @param fwd Is this about fwd traffic?
2386 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2389 connection_is_origin (struct MeshConnection *c, int fwd)
2391 if (!fwd && c->own_pos == c->path->length - 1)
2393 if (fwd && c->own_pos == 0)
2400 * Is this peer the last one on the connection?
2402 * @param c Connection.
2403 * @param fwd Is this about fwd traffic?
2404 * Note that the ROOT is the terminal for BCK traffic!
2406 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2409 connection_is_terminal (struct MeshConnection *c, int fwd)
2411 if (fwd && c->own_pos == c->path->length - 1)
2413 if (!fwd && c->own_pos == 0)
2420 * @brief Re-initiate traffic on this connection if necessary.
2422 * Check if there is traffic queued towards this peer
2423 * and the core transmit handle is NULL (traffic was stalled).
2424 * If so, call core tmt rdy.
2426 * @param c Connection on which initiate traffic.
2427 * @param fwd Is this about fwd traffic?
2430 connection_unlock_queue (struct MeshConnection *c, int fwd)
2432 struct MeshPeer *peer;
2433 struct MeshPeerQueue *q;
2436 peer = connection_get_hop (c, fwd);
2438 if (NULL != peer->core_transmit)
2439 return; /* Already unlocked */
2441 q = connection_get_first_message (c, fwd);
2443 return; /* Nothing to transmit */
2446 peer->core_transmit =
2447 GNUNET_CORE_notify_transmit_ready (core_handle,
2450 GNUNET_TIME_UNIT_FOREVER_REL,
2451 GNUNET_PEER_resolve2 (peer->id),
2459 * Cancel all transmissions that belong to a certain connection.
2461 * @param c Connection which to cancel.
2462 * @param fwd Cancel fwd traffic?
2465 connection_cancel_queues (struct MeshConnection *c, int fwd)
2467 struct MeshPeerQueue *q;
2468 struct MeshPeerQueue *next;
2469 struct MeshFlowControl *fc;
2470 struct MeshPeer *peer;
2477 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2478 peer = connection_get_hop (c, fwd);
2480 for (q = peer->queue_head; NULL != q; q = next)
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 "connection_cancel_queue %s\n",
2487 GNUNET_MESH_DEBUG_M2S (q->type));
2488 queue_destroy (q, GNUNET_YES);
2491 if (NULL == peer->queue_head)
2493 if (NULL != peer->core_transmit)
2495 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2496 peer->core_transmit = NULL;
2498 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2500 GNUNET_SCHEDULER_cancel (fc->poll_task);
2501 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2508 * Remove all paths that rely on a direct connection between p1 and p2
2509 * from the peer itself and notify all tunnels about it.
2511 * @param peer PeerInfo of affected peer.
2512 * @param p1 GNUNET_PEER_Id of one peer.
2513 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2516 * TODO: optimize (see below)
2519 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2522 struct MeshPeerPath *p;
2523 struct MeshPeerPath *next;
2524 struct MeshPeer *peer_d;
2526 unsigned int destroyed;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2531 for (p = peer->path_head; NULL != p; p = next)
2534 for (i = 0; i < (p->length - 1); i++)
2536 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2537 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2539 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2550 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2552 peer_d = peer_get_short (d); // FIXME
2553 next = peer_get_best_path (peer_d);
2554 tunnel_use_path (peer->tunnel, next);
2555 peer_connect (peer_d);
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2562 * Add the path to the peer and update the path used to reach it in case this
2565 * @param peer_info Destination peer to add the path to.
2566 * @param path New path to add. Last peer must be the peer in arg 1.
2567 * Path will be either used of freed if already known.
2568 * @param trusted Do we trust that this path is real?
2571 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2574 struct MeshPeerPath *aux;
2578 if ((NULL == peer_info) || (NULL == path))
2581 path_destroy (path);
2584 if (path->peers[path->length - 1] != peer_info->id)
2587 path_destroy (path);
2590 if (2 >= path->length && GNUNET_NO == trusted)
2592 /* Only allow CORE to tell us about direct paths */
2593 path_destroy (path);
2596 for (l = 1; l < path->length; l++)
2598 if (path->peers[l] == myid)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2601 for (l2 = 0; l2 < path->length - l; l2++)
2603 path->peers[l2] = path->peers[l + l2];
2608 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2613 path->length, peer2s (peer_info));
2615 l = path_get_length (path);
2618 path_destroy (path);
2622 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2623 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2625 l2 = path_get_length (aux);
2628 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2629 peer_info->path_tail, aux, path);
2634 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2636 path_destroy (path);
2641 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2648 * Add the path to the origin peer and update the path used to reach it in case
2649 * this is the shortest.
2650 * The path is given in peer_info -> destination, therefore we turn the path
2651 * upside down first.
2653 * @param peer_info Peer to add the path to, being the origin of the path.
2654 * @param path New path to add after being inversed.
2655 * Path will be either used or freed.
2656 * @param trusted Do we trust that this path is real?
2659 peer_add_path_to_origin (struct MeshPeer *peer_info,
2660 struct MeshPeerPath *path, int trusted)
2663 peer_add_path (peer_info, path, trusted);
2669 * Function called if a connection has been stalled for a while,
2670 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2672 * @param cls Closure (poll ctx).
2673 * @param tc TaskContext.
2676 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2678 struct MeshFlowControl *fc = cls;
2679 struct GNUNET_MESH_Poll msg;
2680 struct MeshConnection *c;
2682 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2683 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2691 peer2s (c->t->peer), c->id);
2693 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2694 msg.header.size = htons (sizeof (msg));
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2696 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2697 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2698 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2699 &connection_poll, fc);
2704 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2705 * to obtain a local peer -> destination path and interning the peer ids.
2707 * @return Newly allocated and created path
2709 static struct MeshPeerPath *
2710 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2711 unsigned int get_path_length,
2712 const struct GNUNET_PeerIdentity *put_path,
2713 unsigned int put_path_length)
2715 struct MeshPeerPath *p;
2721 GNUNET_PEER_change_rc (myid, 1);
2722 i = get_path_length;
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2724 for (i--; i >= 0; i--)
2726 id = GNUNET_PEER_intern (&get_path[i]);
2727 if (p->length > 0 && id == p->peers[p->length - 1])
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2730 GNUNET_PEER_change_rc (id, -1);
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2735 GNUNET_i2s (&get_path[i]));
2737 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2738 p->peers[p->length - 1] = id;
2741 i = put_path_length;
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2743 for (i--; i >= 0; i--)
2745 id = GNUNET_PEER_intern (&put_path[i]);
2748 /* PUT path went through us, so discard the path up until now and start
2749 * from here to get a much shorter (and loop-free) path.
2754 if (p->length > 0 && id == p->peers[p->length - 1])
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2757 GNUNET_PEER_change_rc (id, -1);
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2762 GNUNET_i2s (&put_path[i]));
2764 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2765 p->peers[p->length - 1] = id;
2769 if (get_path_length > 0)
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2771 GNUNET_i2s (&get_path[0]));
2772 if (put_path_length > 0)
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2774 GNUNET_i2s (&put_path[0]));
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2777 for (i = 0; i < p->length; i++)
2779 struct GNUNET_PeerIdentity peer_id;
2781 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2783 GNUNET_i2s (&peer_id));
2791 * Adds a path to the peer_infos of all the peers in the path
2793 * @param p Path to process.
2794 * @param confirmed Whether we know if the path works or not.
2797 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2801 /* TODO: invert and add */
2802 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2803 for (i++; i < p->length; i++)
2805 struct MeshPeer *aux;
2806 struct MeshPeerPath *copy;
2808 aux = peer_get_short (p->peers[i]);
2809 copy = path_duplicate (p);
2810 copy->length = i + 1;
2811 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2817 * Search for a channel among the channels for a client
2819 * @param c the client whose channels to search in
2820 * @param chid the local id of the channel
2822 * @return channel handler, NULL if doesn't exist
2824 static struct MeshChannel *
2825 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2827 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2829 GNUNET_break_op (0);
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2833 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2834 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2835 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2839 * Search for a tunnel by global ID using full PeerIdentities.
2841 * @param t Tunnel containing the channel.
2842 * @param chid Public channel number.
2844 * @return channel handler, NULL if doesn't exist
2846 static struct MeshChannel *
2847 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2849 struct MeshChannel *ch;
2854 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2856 if (ch->gid == chid)
2865 * Change the tunnel state.
2867 * @param t Tunnel whose state to change.
2868 * @param state New state.
2871 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2876 "Tunnel %s state was %s\n",
2878 GNUNET_MESH_DEBUG_TS2S (t->state));
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2880 "Tunnel %s state is now %s\n",
2882 GNUNET_MESH_DEBUG_TS2S (state));
2888 * Cache a message to be sent once tunnel is online.
2890 * @param t Tunnel to hold the message.
2891 * @param ch Channel the message is about.
2892 * @param msg Message itself (copy will be made).
2893 * @param fwd Is this fwd?
2896 tunnel_queue_data (struct MeshTunnel2 *t,
2897 struct MeshChannel *ch,
2898 struct GNUNET_MessageHeader *msg,
2901 struct MeshTunnelQueue *tq;
2902 uint16_t size = ntohs (msg->size);
2904 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2907 memcpy (&tq[1], msg, size);
2908 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2913 * Send all cached messages that we can, tunnel is online.
2915 * @param t Tunnel that holds the messages.
2916 * @param fwd Is this fwd?
2919 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2921 struct MeshTunnelQueue *tq;
2922 struct MeshTunnelQueue *next;
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2926 "tunnel_send_queued_data on tunnel %s\n",
2928 room = tunnel_get_buffer (t, fwd);
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2930 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2934 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2935 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2944 connection_change_state (struct MeshConnection* c,
2945 enum MeshConnectionState state)
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 "Connection %s[%X] state was %s\n",
2949 peer2s (c->t->peer), c->id,
2950 GNUNET_MESH_DEBUG_CS2S (c->state));
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 "Connection %s[%X] state is now %s\n",
2953 peer2s (c->t->peer), c->id,
2954 GNUNET_MESH_DEBUG_CS2S (state));
2960 * Add a client to a channel, initializing all needed data structures.
2962 * @param ch Channel to which add the client.
2963 * @param c Client which to add to the channel.
2966 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2968 struct MeshTunnel2 *t = ch->t;
2970 if (NULL != ch->dest)
2976 /* Assign local id as destination */
2977 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2978 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2979 ch->lid_dest = t->next_local_chid++;
2980 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2982 /* Store in client's hashmap */
2984 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2986 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2995 static struct MeshConnection *
2996 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2998 struct MeshConnection *c;
2999 struct GNUNET_HashCode cid;
3000 struct MeshPeer *peer;
3001 unsigned int own_pos;
3003 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3005 c = connection_new (&cid);
3007 for (own_pos = 0; own_pos < p->length; own_pos++)
3009 if (p->peers[own_pos] == myid)
3012 if (own_pos > p->length - 1)
3015 connection_destroy (c);
3018 c->own_pos = own_pos;
3023 c->fwd_maintenance_task =
3024 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3025 &connection_fwd_keepalive, c);
3028 peer = connection_get_next_hop (c);
3029 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3031 peer = connection_get_prev_hop (c);
3032 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3033 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3039 * Notifies a tunnel that a connection has broken that affects at least
3040 * some of its peers. Sends a notification towards the root of the tree.
3041 * In case the peer is the owner of the tree, notifies the client that owns
3042 * the tunnel and tries to reconnect.
3044 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3046 * @param t Tunnel affected.
3047 * @param p1 Peer that got disconnected from p2.
3048 * @param p2 Peer that got disconnected from p1.
3050 * @return Short ID of the peer disconnected (either p1 or p2).
3051 * 0 if the tunnel remained unaffected.
3053 static GNUNET_PEER_Id
3054 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3055 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3057 // if (myid != p1 && myid != p2) FIXME
3062 // if (tree_get_predecessor (t->tree) != 0)
3064 // /* We are the peer still connected, notify owner of the disconnection. */
3065 // struct GNUNET_MESH_PathBroken msg;
3066 // struct GNUNET_PeerIdentity neighbor;
3068 // msg.header.size = htons (sizeof (msg));
3069 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3070 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3071 // msg.tid = htonl (t->id.tid);
3072 // msg.peer1 = my_full_id;
3073 // GNUNET_PEER_resolve (pid, &msg.peer2);
3074 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3075 // send_prebuilt_message (&msg.header, &neighbor, t);
3082 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3084 * @param ch Channel this is about.
3085 * @param fwd Is for FWD traffic? (ACK dest->owner)
3088 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3090 struct GNUNET_MESH_DataACK msg;
3091 struct MeshChannelReliability *rel;
3092 struct MeshReliableMessage *copy;
3098 if (GNUNET_NO == ch->reliable)
3103 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3104 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3106 "send_data_ack for %u\n",
3109 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3110 msg.header.type = htons (type);
3111 msg.header.size = htons (sizeof (msg));
3112 msg.chid = htonl (ch->gid);
3113 msg.mid = htonl (*mid - 1);
3115 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3117 if (copy->type != type)
3119 delta = copy->mid - *mid;
3122 mask = 0x1LL << delta;
3123 msg.futures |= mask;
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3125 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3126 copy->mid, delta, mask, msg.futures);
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3130 send_prebuilt_message_channel (&msg.header, ch, fwd);
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3136 * Send an ACK informing the predecessor about the available buffer space.
3138 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3139 * the ACK itself goes "back" (dest->root).
3141 * @param c Connection on which to send the ACK.
3142 * @param fwd Is this FWD ACK? (Going dest->owner)
3145 connection_send_ack (struct MeshConnection *c, int fwd)
3147 struct MeshFlowControl *next_fc;
3148 struct MeshFlowControl *prev_fc;
3152 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3153 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 "connection send %s ack on %s\n",
3157 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3159 /* Check if we need to transmit the ACK */
3160 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3164 " last pid recv: %u, last ack sent: %u\n",
3165 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3169 /* Ok, ACK might be necessary, what PID to ACK? */
3170 delta = next_fc->queue_max - next_fc->queue_n;
3171 ack = prev_fc->last_pid_recv + delta;
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3174 " last pid %u, last ack %u, qmax %u, q %u\n",
3175 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3176 next_fc->queue_max, next_fc->queue_n);
3177 if (ack == prev_fc->last_ack_sent)
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3183 prev_fc->last_ack_sent = ack;
3184 send_ack (c, ack, fwd);
3189 * Modify the mesh message TID from global to local and send to client.
3191 * @param ch Channel on which to send the message.
3192 * @param msg Message to modify and send.
3193 * @param c Client to send to.
3194 * @param tid Tunnel ID to use (c can be both owner and client).
3197 channel_send_client_to_tid (struct MeshChannel *ch,
3198 const struct GNUNET_MESH_Data *msg,
3199 struct MeshClient *c, MESH_ChannelNumber id)
3201 struct GNUNET_MESH_LocalData *copy;
3202 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3203 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3205 if (size < sizeof (struct GNUNET_MessageHeader))
3207 GNUNET_break_op (0);
3215 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3216 memcpy (©[1], &msg[1], size);
3217 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3218 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3219 copy->id = htonl (id);
3220 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3221 ©->header, GNUNET_NO);
3225 * Modify the data message ID from global to local and send to client.
3227 * @param ch Channel on which to send the message.
3228 * @param msg Message to modify and send.
3229 * @param fwd Forward?
3232 channel_send_client_data (struct MeshChannel *ch,
3233 const struct GNUNET_MESH_Data *msg,
3237 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3239 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3244 * Send a buffered message to the client, for in order delivery or
3245 * as result of client ACK.
3247 * @param ch Channel on which to empty the message buffer.
3248 * @param c Client to send to.
3249 * @param rel Reliability structure to corresponding peer.
3250 * If rel == bck_rel, this is FWD data.
3253 channel_send_client_buffered_data (struct MeshChannel *ch,
3254 struct MeshClient *c,
3255 struct MeshChannelReliability *rel)
3257 struct MeshReliableMessage *copy;
3260 if (GNUNET_NO == rel->client_ready)
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3266 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3269 copy = rel->head_recv;
3270 /* We never buffer channel management messages */
3273 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3275 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 " have %u! now expecting %u\n",
3279 copy->mid, *mid + 1);
3280 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3283 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 " reliable && don't have %u, next is %u\n",
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3300 * We have received a message out of order, or the client is not ready.
3301 * Buffer it until we receive an ACK from the client or the missing
3302 * message from the channel.
3304 * @param msg Message to buffer (MUST be of type MESH_DATA).
3305 * @param rel Reliability data to the corresponding direction.
3308 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3309 struct MeshChannelReliability *rel)
3311 struct MeshReliableMessage *copy;
3312 struct MeshReliableMessage *prev;
3316 size = ntohs (msg->header.size);
3317 mid = ntohl (msg->mid);
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3321 copy = GNUNET_malloc (sizeof (*copy) + size);
3324 memcpy (©[1], msg, size);
3328 // FIXME do something better than O(n), although n < 64...
3329 // FIXME start from the end (most messages are the latest ones)
3330 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3333 if (GMC_is_pid_bigger (prev->mid, mid))
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3336 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3342 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3348 * Destroy a reliable message after it has been acknowledged, either by
3349 * direct mid ACK or bitfield. Updates the appropriate data structures and
3350 * timers and frees all memory.
3352 * @param copy Message that is no longer needed: remote peer got it.
3355 rel_message_free (struct MeshReliableMessage *copy)
3357 struct MeshChannelReliability *rel;
3358 struct GNUNET_TIME_Relative time;
3361 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3362 rel->expected_delay.rel_value_us *= 7;
3363 rel->expected_delay.rel_value_us += time.rel_value_us;
3364 rel->expected_delay.rel_value_us /= 8;
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3369 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3371 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3373 rel->retry_timer = rel->expected_delay;
3374 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3380 * Destroy all reliable messages queued for a channel,
3381 * during a channel destruction.
3382 * Frees the reliability structure itself.
3384 * @param rel Reliability data for a channel.
3387 channel_rel_free_all (struct MeshChannelReliability *rel)
3389 struct MeshReliableMessage *copy;
3390 struct MeshReliableMessage *next;
3395 for (copy = rel->head_recv; NULL != copy; copy = next)
3398 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3401 for (copy = rel->head_sent; NULL != copy; copy = next)
3404 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3407 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3408 GNUNET_SCHEDULER_cancel (rel->retry_task);
3414 * Mark future messages as ACK'd.
3416 * @param rel Reliability data.
3417 * @param msg DataACK message with a bitfield of future ACK'd messages.
3420 channel_rel_free_sent (struct MeshChannelReliability *rel,
3421 const struct GNUNET_MESH_DataACK *msg)
3423 struct MeshReliableMessage *copy;
3424 struct MeshReliableMessage *next;
3431 bitfield = msg->futures;
3432 mid = ntohl (msg->mid);
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3434 "free_sent_reliable %u %llX\n",
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3437 " rel %p, head %p\n",
3438 rel, rel->head_sent);
3439 for (i = 0, copy = rel->head_sent;
3440 i < 64 && NULL != copy && 0 != bitfield;
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3444 " trying bit %u (mid %u)\n",
3447 if (0 == (bitfield & mask))
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3451 /* Bit was set, clear the bit from the bitfield */
3454 /* The i-th bit was set. Do we have that copy? */
3455 /* Skip copies with mid < target */
3456 target = mid + i + 1;
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3458 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3461 /* Did we run out of copies? (previously freed, it's ok) */
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3468 /* Did we overshoot the target? (previously freed, it's ok) */
3469 if (GMC_is_pid_bigger (copy->mid, target))
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3475 /* Now copy->mid == target, free it */
3477 rel_message_free (copy);
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3485 * We haven't received an ACK after a certain time: restransmit the message.
3487 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3488 * @param tc TaskContext.
3491 channel_retransmit_message (void *cls,
3492 const struct GNUNET_SCHEDULER_TaskContext *tc)
3494 struct MeshChannelReliability *rel = cls;
3495 struct MeshReliableMessage *copy;
3496 struct MeshPeerQueue *q;
3497 struct MeshChannel *ch;
3498 struct MeshConnection *c;
3499 struct GNUNET_MESH_Data *payload;
3500 struct MeshPeer *hop;
3503 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3504 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3508 copy = rel->head_sent;
3515 /* Search the message to be retransmitted in the outgoing queue.
3516 * Check only the queue for the connection that is going to be used,
3517 * if the message is stuck in some other connection's queue we shouldn't
3519 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3520 * the old connection could be temporary stalled or the queue happened to
3521 * be long at time of insertion.
3522 * - not sending the new one could cause terrible delays the old connection
3525 payload = (struct GNUNET_MESH_Data *) ©[1];
3526 fwd = (rel == ch->fwd_rel);
3527 c = tunnel_get_connection (ch->t, fwd);
3528 hop = connection_get_hop (c, fwd);
3529 for (q = hop->queue_head; NULL != q; q = q->next)
3531 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3533 struct GNUNET_MESH_Data *queued_data = q->cls;
3535 if (queued_data->mid == payload->mid)
3540 /* Message not found in the queue that we are going to use. */
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3545 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3546 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3553 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3554 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3555 &channel_retransmit_message,
3561 * Send an ACK to a client if needed.
3563 * @param ch Channel this is regarding.
3564 * @param fwd Is this about fwd traffic? (ACk goes the opposite direction).
3567 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3569 struct MeshClient *c;
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "Channel send client %s ack on %s:%X\n",
3574 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3576 /* Check for buffer space */
3577 if (0 >= tunnel_get_buffer (ch->t, fwd))
3580 /* Client to receive the ACK (fwd indicates traffic to be ACK'd) */
3581 c = fwd ? ch->root : ch->dest;
3583 blocked = fwd ? ch->blocked_fwd : ch->blocked_bck;
3584 if (GNUNET_YES == blocked)
3585 send_local_ack (ch, c, fwd);
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client not blocked\n");
3592 * Send ACK on one or more connections due to buffer space to the client.
3594 * @param ch Channel which has some free buffer space.
3595 * @param buffer Buffer space.
3596 * @param fwd Is this in the FWD direction?
3599 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3601 struct MeshTunnel2 *t = ch->t;
3602 struct MeshConnection *c;
3603 struct MeshFlowControl *fc;
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3609 "Channel send connection %s ack on %s:%X\n",
3610 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3612 /* Count connections, how many messages are already allowed */
3613 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3615 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3616 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3621 allowed += fc->last_ack_sent - fc->last_pid_recv;
3625 /* Make sure there is no overflow */
3626 if (allowed > buffer)
3632 /* Authorize connections to send more data */
3633 to_allow = buffer - allowed;
3634 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3636 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3637 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3641 send_ack (c, fc->last_ack_sent + 1, fwd);
3645 GNUNET_break (to_allow == 0);
3650 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3652 * @param ch Channel to mark as ready.
3653 * @param fwd Was the CREATE message sent fwd?
3656 channel_confirm (struct MeshChannel *ch, int fwd)
3658 struct MeshChannelReliability *rel;
3659 struct MeshReliableMessage *copy;
3660 struct MeshReliableMessage *next;
3662 ch->state = MESH_CHANNEL_READY;
3664 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3665 for (copy = rel->head_sent; NULL != copy; copy = next)
3667 struct GNUNET_MessageHeader *msg;
3670 msg = (struct GNUNET_MessageHeader *) ©[1];
3671 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3673 rel_message_free (copy);
3681 * Save a copy to retransmit in case it gets lost.
3683 * Initializes all needed callbacks and timers.
3685 * @param ch Channel this message goes on.
3686 * @param msg Message to copy.
3687 * @param fwd Is this fwd traffic?
3690 channel_save_copy (struct MeshChannel *ch,
3691 const struct GNUNET_MessageHeader *msg,
3694 struct MeshChannelReliability *rel;
3695 struct MeshReliableMessage *copy;
3700 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3701 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3702 type = ntohs (msg->type);
3703 size = ntohs (msg->size);
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3706 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3708 copy->timestamp = GNUNET_TIME_absolute_get ();
3711 memcpy (©[1], msg, size);
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3714 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3715 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3718 GNUNET_TIME_relative_multiply (rel->expected_delay,
3719 MESH_RETRANSMIT_MARGIN);
3721 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3722 &channel_retransmit_message,
3729 * Send keepalive packets for a connection.
3731 * @param c Connection to keep alive..
3732 * @param fwd Is this a FWD keepalive? (owner -> dest).
3735 connection_keepalive (struct MeshConnection *c, int fwd)
3737 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3738 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3742 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3743 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3746 "sending %s keepalive for connection %s[%d]\n",
3747 fwd ? "FWD" : "BCK",
3748 peer2s (c->t->peer),
3751 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3752 msg->header.size = htons (size);
3753 msg->header.type = htons (type);
3756 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3761 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3763 * @param c Connection for which to send the message.
3764 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3767 connection_recreate (struct MeshConnection *c, int fwd)
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3771 send_connection_create (c);
3773 send_connection_ack (c, GNUNET_NO);
3778 * Generic connection timer management.
3779 * Depending on the role of the peer in the connection will send the
3780 * appropriate message (build or keepalive)
3782 * @param c Conncetion to maintain.
3783 * @param fwd Is FWD?
3786 connection_maintain (struct MeshConnection *c, int fwd)
3788 if (MESH_TUNNEL_SEARCHING == c->t->state)
3790 /* TODO DHT GET with RO_BART */
3795 case MESH_CONNECTION_NEW:
3797 case MESH_CONNECTION_SENT:
3798 connection_recreate (c, fwd);
3800 case MESH_CONNECTION_READY:
3801 connection_keepalive (c, fwd);
3810 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3812 struct MeshConnection *c = cls;
3814 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3815 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3818 connection_maintain (c, GNUNET_YES);
3819 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3820 &connection_fwd_keepalive,
3826 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3828 struct MeshConnection *c = cls;
3830 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3831 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3834 connection_maintain (c, GNUNET_NO);
3835 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3836 &connection_bck_keepalive,
3842 * Send a message to all peers in this connection that the connection
3843 * is no longer valid.
3845 * If some peer should not receive the message, it should be zero'ed out
3846 * before calling this function.
3848 * @param c The connection whose peers to notify.
3851 connection_send_destroy (struct MeshConnection *c)
3853 struct GNUNET_MESH_ConnectionDestroy msg;
3855 msg.header.size = htons (sizeof (msg));
3856 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3859 " sending connection destroy for connection %s[%X]\n",
3860 peer2s (c->t->peer),
3863 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3864 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3865 c->destroy = GNUNET_YES;
3870 * Confirm we got a channel create.
3872 * @param ch The channel to confirm.
3873 * @param fwd Should we send the ACK fwd?
3876 channel_send_ack (struct MeshChannel *ch, int fwd)
3878 struct GNUNET_MESH_ChannelManage msg;
3880 msg.header.size = htons (sizeof (msg));
3881 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3883 " sending channel ack for channel %s:%X\n",
3884 peer2s (ch->t->peer),
3887 msg.chid = htonl (ch->gid);
3888 send_prebuilt_message_channel (&msg.header, ch, fwd);
3893 * Send a message to all clients (local and remote) of this channel
3894 * notifying that the channel is no longer valid.
3896 * If some peer or client should not receive the message,
3897 * should be zero'ed out before calling this function.
3899 * @param ch The channel whose clients to notify.
3902 channel_send_destroy (struct MeshChannel *ch)
3904 struct GNUNET_MESH_ChannelManage msg;
3906 msg.header.size = htons (sizeof (msg));
3907 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 " sending channel destroy for channel %s:%X\n",
3910 peer2s (ch->t->peer),
3913 if (NULL != ch->root)
3915 msg.chid = htonl (ch->lid_root);
3916 send_local_channel_destroy (ch, GNUNET_NO);
3920 msg.chid = htonl (ch->gid);
3921 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3924 if (NULL != ch->dest)
3926 msg.chid = htonl (ch->lid_dest);
3927 send_local_channel_destroy (ch, GNUNET_YES);
3931 msg.chid = htonl (ch->gid);
3932 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3940 static struct MeshTunnel2 *
3943 struct MeshTunnel2 *t;
3945 t = GNUNET_new (struct MeshTunnel2);
3947 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3949 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3950 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3952 // GNUNET_break (0);
3953 // tunnel_destroy (t);
3957 // char salt[] = "salt";
3958 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3959 // salt, sizeof (salt),
3960 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3961 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3962 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3964 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3965 // salt, sizeof (salt),
3966 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3967 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3968 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3976 * Add a connection to a tunnel.
3979 * @param c Connection.
3982 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3985 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3990 * Initialize a Flow Control structure to the initial state.
3992 * @param fc Flow Control structure to initialize.
3995 fc_init (struct MeshFlowControl *fc)
3998 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3999 fc->last_pid_recv = (uint32_t) -1;
4000 fc->last_ack_sent = (uint32_t) 0;
4001 fc->last_ack_recv = (uint32_t) 0;
4002 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4003 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4005 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4009 static struct MeshConnection *
4010 connection_new (const struct GNUNET_HashCode *cid)
4012 struct MeshConnection *c;
4014 c = GNUNET_new (struct MeshConnection);
4016 fc_init (&c->fwd_fc);
4017 fc_init (&c->bck_fc);
4026 * Find a connection.
4028 * @param cid Connection ID.
4030 static struct MeshConnection *
4031 connection_get (const struct GNUNET_HashCode *cid)
4033 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4038 connection_destroy (struct MeshConnection *c)
4040 struct MeshPeer *peer;
4045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4046 peer2s (c->t->peer),
4049 /* Cancel all traffic */
4050 connection_cancel_queues (c, GNUNET_YES);
4051 connection_cancel_queues (c, GNUNET_NO);
4053 /* Cancel maintainance task (keepalive/timeout) */
4054 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4055 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4056 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4057 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4059 /* Deregister from neighbors */
4060 peer = connection_get_next_hop (c);
4062 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4063 peer = connection_get_prev_hop (c);
4065 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4068 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4069 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4075 tunnel_destroy (struct MeshTunnel2 *t)
4077 struct MeshConnection *c;
4078 struct MeshConnection *next;
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4086 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4087 // GNUNET_break (0);
4089 for (c = t->connection_head; NULL != c; c = next)
4092 connection_destroy (c);
4095 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4102 * Tunnel is empty: destroy it.
4104 * Notifies all connections about the destruction.
4106 * @param t Tunnel to destroy.
4109 tunnel_destroy_empty (struct MeshTunnel2 *t)
4111 struct MeshConnection *c;
4113 for (c = t->connection_head; NULL != c; c = c->next)
4115 if (GNUNET_NO == c->destroy)
4116 connection_send_destroy (c);
4119 if (0 == t->pending_messages)
4122 t->destroy = GNUNET_YES;
4127 * Destroy tunnel if empty (no more channels).
4129 * @param t Tunnel to destroy if empty.
4132 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4134 if (NULL != t->channel_head)
4137 tunnel_destroy_empty (t);
4142 * Destroy a channel and free all resources.
4144 * @param ch Channel to destroy.
4147 channel_destroy (struct MeshChannel *ch)
4149 struct MeshClient *c;
4157 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4168 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4175 channel_rel_free_all (ch->fwd_rel);
4176 channel_rel_free_all (ch->bck_rel);
4178 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4179 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4185 * Create a new channel.
4187 * @param t Tunnel this channel is in.
4188 * @param owner Client that owns the channel, NULL for foreign channels.
4189 * @param lid_root Local ID for root client.
4191 * @return A new initialized channel. NULL on error.
4193 static struct MeshChannel *
4194 channel_new (struct MeshTunnel2 *t,
4195 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4197 struct MeshChannel *ch;
4199 ch = GNUNET_new (struct MeshChannel);
4201 ch->lid_root = lid_root;
4203 ch->blocked_fwd = GNUNET_YES;
4204 ch->blocked_bck = GNUNET_YES;
4206 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4208 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4212 while (NULL != channel_get (t, t->next_chid))
4213 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4214 ch->gid = t->next_chid;
4215 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4218 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4222 channel_destroy (ch);
4223 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4233 * Set options in a channel, extracted from a bit flag field
4235 * @param ch Channel to set options to.
4236 * @param options Bit array in host byte order.
4239 channel_set_options (struct MeshChannel *ch, uint32_t options)
4241 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4242 GNUNET_YES : GNUNET_NO;
4243 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4244 GNUNET_YES : GNUNET_NO;
4249 * Iterator for deleting each channel whose client endpoint disconnected.
4251 * @param cls Closure (client that has disconnected).
4252 * @param key The local channel id (used to access the hashmap).
4253 * @param value The value stored at the key (channel to destroy).
4255 * @return GNUNET_OK, keep iterating.
4258 channel_destroy_iterator (void *cls,
4262 struct MeshChannel *ch = value;
4263 struct MeshClient *c = cls;
4264 struct MeshTunnel2 *t;
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4267 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4268 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4282 channel_send_destroy (ch);
4283 channel_destroy (ch);
4284 tunnel_destroy_if_empty (t);
4291 * Remove client's ports from the global hashmap on disconnect.
4293 * @param cls Closure (unused).
4295 * @param value Client structure.
4297 * @return GNUNET_OK, keep iterating.
4300 client_release_ports (void *cls,
4306 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4307 if (GNUNET_YES != res)
4310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4311 "Port %u by client %p was not registered.\n",
4319 * Timeout function due to lack of keepalive/traffic from the owner.
4320 * Destroys connection if called.
4322 * @param cls Closure (connection to destroy).
4323 * @param tc TaskContext.
4326 connection_fwd_timeout (void *cls,
4327 const struct GNUNET_SCHEDULER_TaskContext *tc)
4329 struct MeshConnection *c = cls;
4331 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4332 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4335 "Connection %s[%X] FWD timed out. Destroying.\n",
4336 peer2s (c->t->peer),
4339 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4342 connection_destroy (c);
4347 * Timeout function due to lack of keepalive/traffic from the destination.
4348 * Destroys connection if called.
4350 * @param cls Closure (connection to destroy).
4351 * @param tc TaskContext
4354 connection_bck_timeout (void *cls,
4355 const struct GNUNET_SCHEDULER_TaskContext *tc)
4357 struct MeshConnection *c = cls;
4359 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4360 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4364 "Connection %s[%X] FWD timed out. Destroying.\n",
4365 peer2s (c->t->peer),
4368 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4371 connection_destroy (c);
4376 * Resets the connection timeout task, some other message has done the
4378 * - For the first peer on the direction this means to send
4379 * a keepalive or a path confirmation message (either create or ACK).
4380 * - For all other peers, this means to destroy the connection,
4381 * due to lack of activity.
4382 * Starts the tiemout if no timeout was running (connection just created).
4384 * @param c Connection whose timeout to reset.
4385 * @param fwd Is this forward?
4387 * TODO use heap to improve efficiency of scheduler.
4390 connection_reset_timeout (struct MeshConnection *c, int fwd)
4392 GNUNET_SCHEDULER_TaskIdentifier *ti;
4393 GNUNET_SCHEDULER_Task f;
4395 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4397 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4398 GNUNET_SCHEDULER_cancel (*ti);
4400 if (connection_is_origin (c, fwd)) /* Endpoint */
4402 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4403 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4407 struct GNUNET_TIME_Relative delay;
4409 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4410 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4411 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4417 * Iterator to notify all connections of a broken link. Mark connections
4418 * to destroy after all traffic has been sent.
4420 * @param cls Closure (peer disconnected).
4421 * @param key Current key code (tid).
4422 * @param value Value in the hash map (connection).
4424 * @return GNUNET_YES if we should continue to iterate,
4428 connection_broken (void *cls,
4429 const struct GNUNET_HashCode *key,
4432 struct MeshPeer *peer = cls;
4433 struct MeshConnection *c = value;
4434 struct GNUNET_MESH_ConnectionBroken msg;
4437 fwd = peer == connection_get_prev_hop (c);
4438 connection_cancel_queues (c, !fwd);
4440 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4441 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4443 msg.peer1 = my_full_id;
4444 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4445 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4446 c->destroy = GNUNET_YES;
4451 /******************************************************************************/
4452 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4453 /******************************************************************************/
4456 * Free a transmission that was already queued with all resources
4457 * associated to the request.
4459 * @param queue Queue handler to cancel.
4460 * @param clear_cls Is it necessary to free associated cls?
4463 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4465 struct MeshPeer *peer;
4466 struct MeshFlowControl *fc;
4471 GNUNET_assert (NULL != queue->c);
4472 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4474 if (GNUNET_YES == clear_cls)
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4477 GNUNET_MESH_DEBUG_M2S (queue->type));
4478 switch (queue->type)
4480 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4481 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4482 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4483 GNUNET_break (GNUNET_YES == queue->c->destroy);
4485 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4486 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4487 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4488 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4489 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4490 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4492 GNUNET_free_non_null (queue->cls);
4497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4498 GNUNET_MESH_DEBUG_M2S (queue->type));
4502 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4506 if (NULL != queue->c)
4508 queue->c->pending_messages--;
4509 if (NULL != queue->c->t)
4511 queue->c->t->pending_messages--;
4515 GNUNET_free (queue);
4520 queue_send (void *cls, size_t size, void *buf)
4522 struct MeshPeer *peer = cls;
4523 struct MeshFlowControl *fc;
4524 struct MeshConnection *c;
4525 struct GNUNET_MessageHeader *msg;
4526 struct MeshPeerQueue *queue;
4527 struct MeshTunnel2 *t;
4528 const struct GNUNET_PeerIdentity *dst_id;
4534 peer->core_transmit = NULL;
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4537 if (NULL == buf || 0 == size)
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4544 queue = peer_get_first_message (peer);
4547 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4552 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4555 dst_id = GNUNET_PEER_resolve2 (peer->id);
4556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4557 /* Check if buffer size is enough for the message */
4558 if (queue->size > size)
4560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4561 peer->core_transmit =
4562 GNUNET_CORE_notify_transmit_ready (core_handle,
4565 GNUNET_TIME_UNIT_FOREVER_REL,
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4574 t = (NULL != c) ? c->t : NULL;
4578 switch (queue->type)
4580 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4581 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4582 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4583 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4584 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4585 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4588 GNUNET_MESH_DEBUG_M2S (queue->type));
4589 data_size = send_core_data_raw (queue->cls, size, buf);
4590 msg = (struct GNUNET_MessageHeader *) buf;
4591 type = ntohs (msg->type);
4593 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4595 if (connection_is_origin (c, GNUNET_YES))
4596 data_size = send_core_connection_create (queue->c, size, buf);
4598 data_size = send_core_data_raw (queue->cls, size, buf);
4600 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4602 if (connection_is_origin (c, GNUNET_NO))
4603 data_size = send_core_connection_ack (queue->c, size, buf);
4605 data_size = send_core_data_raw (queue->cls, size, buf);
4607 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4608 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4609 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4610 /* This should be encapsulted */
4615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4620 if (0 < drop_percent &&
4621 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4624 "Dropping message of type %s\n",
4625 GNUNET_MESH_DEBUG_M2S (queue->type));
4629 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4632 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4633 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4634 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4635 fc->last_pid_sent = pid;
4636 if (NULL != queue->ch)
4637 channel_send_client_ack (queue->ch, fwd);
4639 connection_send_ack (c, fwd);
4645 /* Free queue, but cls was freed by send_core_* */
4646 queue_destroy (queue, GNUNET_NO);
4648 /* If more data in queue, send next */
4649 queue = peer_get_first_message (peer);
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4653 if (NULL == peer->core_transmit) {
4654 peer->core_transmit =
4655 GNUNET_CORE_notify_transmit_ready(core_handle,
4658 GNUNET_TIME_UNIT_FOREVER_REL,
4666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4667 "* tmt rdy called somewhere else\n");
4669 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4673 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4678 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4680 GNUNET_SCHEDULER_cancel (fc->poll_task);
4681 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4686 c->pending_messages--;
4687 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4690 connection_destroy (c);
4696 t->pending_messages--;
4697 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4709 queue_add (void *cls, uint16_t type, size_t size,
4710 struct MeshConnection *c,
4711 struct MeshChannel *ch,
4714 struct MeshPeerQueue *queue;
4715 struct MeshFlowControl *fc;
4716 struct MeshPeer *peer;
4719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4720 "queue add %s %s (%u) on c %p, ch %p\n",
4721 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4722 GNUNET_assert (NULL != c);
4724 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4725 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4735 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4736 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4740 ( (NULL != ch->root && GNUNET_MESSAGE_TYPE_MESH_FWD == type) ||
4741 (NULL != ch->dest && GNUNET_MESSAGE_TYPE_MESH_BCK == type) ))
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4746 if (fc->queue_n >= fc->queue_max && 0 == priority)
4748 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4752 "queue full: %u/%u\n",
4753 fc->queue_n, fc->queue_max);
4754 return; /* Drop this message */
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4759 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4760 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4763 "no buffer space (%u > %u): starting poll\n",
4764 fc->last_pid_sent + 1, fc->last_ack_recv);
4765 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4769 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4777 if (100 <= priority)
4778 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4780 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4782 if (NULL == peer->core_transmit)
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4785 "calling core tmt rdy towards %s\n",
4787 peer->core_transmit =
4788 GNUNET_CORE_notify_transmit_ready (core_handle,
4791 GNUNET_TIME_UNIT_FOREVER_REL,
4792 GNUNET_PEER_resolve2 (peer->id),
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4800 "core tmt rdy towards %s already called\n",
4804 c->pending_messages++;
4805 c->t->pending_messages++;
4811 /******************************************************************************/
4812 /******************** MESH NETWORK HANDLERS **************************/
4813 /******************************************************************************/
4817 * Generic handler for mesh network payload traffic.
4819 * @param t Tunnel on which we got this message.
4820 * @param message Unencryted data message.
4821 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4823 * @return GNUNET_OK to keep the connection open,
4824 * GNUNET_SYSERR to close it (signal serious error)
4827 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4829 struct MeshChannelReliability *rel;
4830 struct MeshChannel *ch;
4831 struct MeshClient *c;
4838 size = ntohs (msg->header.size);
4840 sizeof (struct GNUNET_MESH_Data) +
4841 sizeof (struct GNUNET_MessageHeader))
4846 type = ntohs (msg->header.type);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4848 GNUNET_MESH_DEBUG_M2S (type));
4849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4850 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4853 ch = channel_get (t, ntohl (msg->chid));
4856 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4861 /* Initialize FWD/BCK data */
4862 c = fwd ? ch->dest : ch->root;
4863 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4864 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4872 tunnel_change_state (t, MESH_TUNNEL_READY);
4874 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4876 mid = ntohl (msg->mid);
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4879 if (GNUNET_NO == ch->reliable ||
4880 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4881 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4884 if (GNUNET_YES == ch->reliable)
4886 /* Is this the exact next expected messasge? */
4887 if (mid == *mid_recv)
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4890 *mid_recv = *mid_recv + 1;
4891 channel_send_client_data (ch, msg, fwd);
4892 channel_send_client_buffered_data (ch, c, rel);
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4897 channel_rel_add_buffered_data (msg, rel);
4900 else /* Tunnel unreliable, send to clients directly */
4902 channel_send_client_data (ch, msg, fwd);
4907 GNUNET_break_op (0);
4908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4909 " MID %u not expected (%u - %u), dropping!\n",
4910 mid, *mid_recv, *mid_recv + 64);
4913 channel_send_data_ack (ch, fwd);
4918 * Handler for mesh network traffic end-to-end ACKs.
4920 * @param t Tunnel on which we got this message.
4921 * @param message Data message.
4922 * @param fwd Is this a fwd ACK? (dest->orig)
4924 * @return GNUNET_OK to keep the connection open,
4925 * GNUNET_SYSERR to close it (signal serious error)
4928 handle_data_ack (struct MeshTunnel2 *t,
4929 const struct GNUNET_MESH_DataACK *msg, int fwd)
4931 struct MeshChannelReliability *rel;
4932 struct MeshReliableMessage *copy;
4933 struct MeshReliableMessage *next;
4934 struct MeshChannel *ch;
4939 type = ntohs (msg->header.type);
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4941 GNUNET_MESH_DEBUG_M2S (type));
4942 ch = channel_get (t, ntohl (msg->chid));
4945 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4948 ack = ntohl (msg->mid);
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4950 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4952 if (GNUNET_YES == fwd)
4965 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4967 if (GMC_is_pid_bigger (copy->mid, ack))
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4970 channel_rel_free_sent (rel, msg);
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4976 rel_message_free (copy);
4978 /* ACK client if needed */
4979 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4981 /* If some message was free'd, update the retransmission delay*/
4982 if (GNUNET_YES == work)
4984 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4986 GNUNET_SCHEDULER_cancel (rel->retry_task);
4987 if (NULL == rel->head_sent)
4989 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4993 struct GNUNET_TIME_Absolute new_target;
4994 struct GNUNET_TIME_Relative delay;
4996 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4997 MESH_RETRANSMIT_MARGIN);
4998 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5000 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5002 GNUNET_SCHEDULER_add_delayed (delay,
5003 &channel_retransmit_message,
5015 * Core handler for connection creation.
5017 * @param cls Closure (unused).
5018 * @param peer Sender (neighbor).
5019 * @param message Message.
5021 * @return GNUNET_OK to keep the connection open,
5022 * GNUNET_SYSERR to close it (signal serious error)
5025 handle_mesh_connection_create (void *cls,
5026 const struct GNUNET_PeerIdentity *peer,
5027 const struct GNUNET_MessageHeader *message)
5029 struct GNUNET_MESH_ConnectionCreate *msg;
5030 struct GNUNET_PeerIdentity *id;
5031 struct GNUNET_HashCode *cid;
5032 struct MeshPeerPath *path;
5033 struct MeshPeer *dest_peer;
5034 struct MeshPeer *orig_peer;
5035 struct MeshConnection *c;
5036 unsigned int own_pos;
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5043 size = ntohs (message->size);
5044 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5046 GNUNET_break_op (0);
5050 /* Calculate hops */
5051 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5052 if (size % sizeof (struct GNUNET_PeerIdentity))
5054 GNUNET_break_op (0);
5057 size /= sizeof (struct GNUNET_PeerIdentity);
5060 GNUNET_break_op (0);
5063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5065 /* Get parameters */
5066 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5068 id = (struct GNUNET_PeerIdentity *) &msg[1];
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5070 " connection %s (%s).\n",
5071 GNUNET_h2s (cid), GNUNET_i2s (id));
5073 /* Create connection */
5074 c = connection_get (cid);
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5078 c = connection_new (cid);
5082 connection_reset_timeout (c, GNUNET_YES);
5083 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5085 /* Remember peers */
5086 dest_peer = peer_get (&id[size - 1]);
5087 orig_peer = peer_get (&id[0]);
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5091 path = path_new (size);
5093 for (i = 0; i < size; i++)
5095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5096 GNUNET_i2s (&id[i]));
5097 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5098 if (path->peers[i] == myid)
5101 if (own_pos == 0 && path->peers[own_pos] != myid)
5103 /* create path: self not found in path through self */
5104 GNUNET_break_op (0);
5105 path_destroy (path);
5106 connection_destroy (c);
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5110 path_add_to_peers (path, GNUNET_NO);
5111 c->path = path_duplicate (path);
5112 c->own_pos = own_pos;
5114 /* Is it a connection to us? */
5115 if (own_pos == size - 1)
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5118 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5120 if (NULL == orig_peer->tunnel)
5121 orig_peer->tunnel = tunnel_new ();
5122 tunnel_add_connection (orig_peer->tunnel, c);
5124 send_connection_ack (c, GNUNET_NO);
5126 /* Keep tunnel alive in direction dest->owner*/
5127 connection_reset_timeout (c, GNUNET_NO);
5131 /* It's for somebody else! Retransmit. */
5132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5133 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5134 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5135 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5142 * Core handler for path ACKs
5144 * @param cls closure
5145 * @param message message
5146 * @param peer peer identity this notification is about
5148 * @return GNUNET_OK to keep the connection open,
5149 * GNUNET_SYSERR to close it (signal serious error)
5152 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5153 const struct GNUNET_MessageHeader *message)
5155 struct GNUNET_MESH_ConnectionACK *msg;
5156 struct MeshPeerPath *p;
5157 struct MeshConnection *c;
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5160 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s[%X]\n",
5162 GNUNET_h2s (&msg->cid));
5163 c = connection_get (&msg->cid);
5166 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5175 /* Add path to peers? */
5179 path_add_to_peers (p, GNUNET_YES);
5185 connection_change_state (c, MESH_CONNECTION_READY);
5186 connection_reset_timeout (c, GNUNET_NO);
5187 if (MESH_TUNNEL_READY != c->t->state)
5188 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5189 tunnel_send_queued_data (c->t, GNUNET_YES);
5191 /* Message for us? */
5192 if (connection_is_terminal (c, GNUNET_NO))
5194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5195 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5197 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5198 c->t->peer->dhtget = NULL;
5200 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5205 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5211 * Core handler for notifications of broken paths
5213 * @param cls Closure (unused).
5214 * @param peer Peer identity of sending neighbor.
5215 * @param message Message.
5217 * @return GNUNET_OK to keep the connection open,
5218 * GNUNET_SYSERR to close it (signal serious error)
5221 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5222 const struct GNUNET_MessageHeader *message)
5224 struct GNUNET_MESH_ConnectionBroken *msg;
5225 struct MeshConnection *c;
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5228 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5229 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5231 GNUNET_i2s (&msg->peer1));
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5233 GNUNET_i2s (&msg->peer2));
5234 c = connection_get (&msg->cid);
5237 GNUNET_break_op (0);
5240 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5241 GNUNET_PEER_search (&msg->peer2));
5248 * Core handler for tunnel destruction
5250 * @param cls Closure (unused).
5251 * @param peer Peer identity of sending neighbor.
5252 * @param message Message.
5254 * @return GNUNET_OK to keep the connection open,
5255 * GNUNET_SYSERR to close it (signal serious error)
5258 handle_mesh_connection_destroy (void *cls,
5259 const struct GNUNET_PeerIdentity *peer,
5260 const struct GNUNET_MessageHeader *message)
5262 struct GNUNET_MESH_ConnectionDestroy *msg;
5263 struct MeshConnection *c;
5267 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5269 "Got a CONNECTION DESTROY message from %s\n",
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5272 " for connection %s\n",
5273 GNUNET_h2s (&msg->cid));
5274 c = connection_get (&msg->cid);
5277 /* Probably already got the message from another path,
5278 * destroyed the tunnel and retransmitted to children.
5281 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5285 id = GNUNET_PEER_search (peer);
5286 if (id == connection_get_prev_hop (c)->id)
5288 else if (id == connection_get_next_hop (c)->id)
5292 GNUNET_break_op (0);
5295 send_prebuilt_message_connection (message, c, NULL, fwd);
5296 c->destroy = GNUNET_YES;
5303 * Handler for channel create messages.
5305 * @param t Tunnel this channel is to be created in.
5306 * @param msg Message.
5307 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5309 * @return GNUNET_OK to keep the connection open,
5310 * GNUNET_SYSERR to close it (signal serious error)
5313 handle_channel_create (struct MeshTunnel2 *t,
5314 struct GNUNET_MESH_ChannelCreate *msg,
5317 MESH_ChannelNumber chid;
5318 struct MeshChannel *ch;
5319 struct MeshClient *c;
5322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5323 /* Check message size */
5324 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5326 GNUNET_break_op (0);
5330 /* Check if channel exists */
5331 chid = ntohl (msg->chid);
5332 ch = channel_get (t, chid);
5335 /* Probably a retransmission, safe to ignore */
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5340 /* Create channel */
5341 ch = channel_new (t, NULL, 0);
5342 channel_set_options (ch, ntohl (msg->opt));
5345 /* Find a destination client */
5346 port = ntohl (msg->port);
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5348 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5351 /* TODO send reject */
5352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5356 channel_add_client (ch, c);
5357 if (GNUNET_YES == ch->reliable)
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5360 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5361 ch->bck_rel->ch = ch;
5362 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5365 send_local_channel_create (ch);
5366 channel_send_ack (ch, !fwd);
5373 * Handler for channel ack messages.
5375 * @param t Tunnel this channel is to be created in.
5376 * @param msg Message.
5377 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5379 * @return GNUNET_OK to keep the connection open,
5380 * GNUNET_SYSERR to close it (signal serious error)
5383 handle_channel_ack (struct MeshTunnel2 *t,
5384 struct GNUNET_MESH_ChannelManage *msg,
5387 MESH_ChannelNumber chid;
5388 struct MeshChannel *ch;
5390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
5391 /* Check message size */
5392 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5394 GNUNET_break_op (0);
5398 /* Check if channel exists */
5399 chid = ntohl (msg->chid);
5400 ch = channel_get (t, chid);
5403 GNUNET_break_op (0);
5404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5408 channel_confirm (ch, !fwd);
5414 * Handler for channel destroy messages.
5416 * @param t Tunnel this channel is to be destroyed of.
5417 * @param msg Message.
5418 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5420 * @return GNUNET_OK to keep the connection open,
5421 * GNUNET_SYSERR to close it (signal serious error)
5424 handle_channel_destroy (struct MeshTunnel2 *t,
5425 struct GNUNET_MESH_ChannelManage *msg,
5428 MESH_ChannelNumber chid;
5429 struct MeshChannel *ch;
5431 /* Check message size */
5432 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5434 GNUNET_break_op (0);
5438 /* Check if channel exists */
5439 chid = ntohl (msg->chid);
5440 ch = channel_get (t, chid);
5443 /* Probably a retransmission, safe to ignore */
5447 send_local_channel_destroy (ch, fwd);
5448 channel_destroy (ch);
5455 * Generic handler for mesh network encrypted traffic.
5457 * @param peer Peer identity this notification is about.
5458 * @param message Encrypted message.
5459 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5461 * @return GNUNET_OK to keep the connection open,
5462 * GNUNET_SYSERR to close it (signal serious error)
5465 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5466 const struct GNUNET_MESH_Encrypted *msg,
5469 struct MeshConnection *c;
5470 struct MeshTunnel2 *t;
5471 struct MeshPeer *neighbor;
5472 struct MeshFlowControl *fc;
5479 size = ntohs (msg->header.size);
5481 sizeof (struct GNUNET_MESH_Encrypted) +
5482 sizeof (struct GNUNET_MessageHeader))
5487 type = ntohs (msg->header.type);
5488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5489 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5491 /* Check connection */
5492 c = connection_get (&msg->cid);
5495 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5500 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5502 /* Check if origin is as expected */
5503 neighbor = connection_get_hop (c, fwd);
5504 if (peer_get (peer)->id != neighbor->id)
5506 GNUNET_break_op (0);
5511 pid = ntohl (msg->pid);
5512 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5514 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5516 "WARNING Received PID %u, (prev %u), ACK %u\n",
5517 pid, fc->last_pid_recv, fc->last_ack_sent);
5520 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5522 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5524 " Pid %u not expected (%u+), dropping!\n",
5525 pid, fc->last_pid_recv + 1);
5528 if (MESH_CONNECTION_SENT == c->state)
5529 connection_change_state (c, MESH_CONNECTION_READY);
5530 connection_reset_timeout (c, fwd);
5531 fc->last_pid_recv = pid;
5533 /* Is this message for us? */
5534 if (connection_is_terminal (c, fwd))
5536 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5538 struct GNUNET_MessageHeader *msgh;
5541 /* TODO signature verification */
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us!\n");
5543 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5545 fc->last_pid_recv = pid;
5546 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5547 msgh = (struct GNUNET_MessageHeader *) cbuf;
5548 switch (ntohs (msgh->type))
5550 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5551 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5554 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5555 r = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5558 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5559 r = handle_channel_create (t,
5560 (struct GNUNET_MESH_ChannelCreate *) msgh,
5564 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5565 r = handle_channel_ack (t,
5566 (struct GNUNET_MESH_ChannelManage *) msgh,
5570 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5571 r = handle_channel_destroy (t,
5572 (struct GNUNET_MESH_ChannelManage *) msgh,
5577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5578 "end-to-end message not known (%u)\n",
5579 ntohs (msgh->type));
5583 connection_send_ack (c, fwd);
5587 /* Message not for us: forward to next hop */
5588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5589 ttl = ntohl (msg->ttl);
5590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5593 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5595 connection_send_ack (c, fwd);
5598 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5600 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5607 * Core handler for mesh network traffic going orig->dest.
5609 * @param cls Closure (unused).
5610 * @param message Message received.
5611 * @param peer Peer who sent the message.
5613 * @return GNUNET_OK to keep the connection open,
5614 * GNUNET_SYSERR to close it (signal serious error)
5617 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5618 const struct GNUNET_MessageHeader *message)
5620 return handle_mesh_encrypted (peer,
5621 (struct GNUNET_MESH_Encrypted *)message,
5626 * Core handler for mesh network traffic going dest->orig.
5628 * @param cls Closure (unused).
5629 * @param message Message received.
5630 * @param peer Peer who sent the message.
5632 * @return GNUNET_OK to keep the connection open,
5633 * GNUNET_SYSERR to close it (signal serious error)
5636 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5637 const struct GNUNET_MessageHeader *message)
5639 return handle_mesh_encrypted (peer,
5640 (struct GNUNET_MESH_Encrypted *)message,
5646 * Core handler for mesh network traffic point-to-point acks.
5648 * @param cls closure
5649 * @param message message
5650 * @param peer peer identity this notification is about
5652 * @return GNUNET_OK to keep the connection open,
5653 * GNUNET_SYSERR to close it (signal serious error)
5656 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5657 const struct GNUNET_MessageHeader *message)
5659 struct GNUNET_MESH_ACK *msg;
5660 struct MeshConnection *c;
5661 struct MeshFlowControl *fc;
5665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5667 msg = (struct GNUNET_MESH_ACK *) message;
5669 c = connection_get (&msg->cid);
5673 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5678 ack = ntohl (msg->ack);
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5681 /* Is this a forward or backward ACK? */
5682 id = GNUNET_PEER_search (peer);
5683 if (connection_get_next_hop (c)->id == id)
5685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5688 else if (connection_get_prev_hop (c)->id == id)
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5695 GNUNET_break_op (0);
5699 /* Cancel polling if the ACK is bigger than before. */
5700 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5701 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5703 GNUNET_SCHEDULER_cancel (fc->poll_task);
5704 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5705 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5708 fc->last_ack_recv = ack;
5709 connection_unlock_queue (c, fc == &c->fwd_fc);
5716 * Core handler for mesh network traffic point-to-point ack polls.
5718 * @param cls closure
5719 * @param message message
5720 * @param peer peer identity this notification is about
5722 * @return GNUNET_OK to keep the connection open,
5723 * GNUNET_SYSERR to close it (signal serious error)
5726 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5727 const struct GNUNET_MessageHeader *message)
5729 struct GNUNET_MESH_Poll *msg;
5730 struct MeshConnection *c;
5731 struct MeshFlowControl *fc;
5735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5738 msg = (struct GNUNET_MESH_Poll *) message;
5740 c = connection_get (&msg->cid);
5744 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5746 GNUNET_break_op (0);
5750 /* Is this a forward or backward ACK? */
5751 id = GNUNET_PEER_search (peer);
5752 if (connection_get_next_hop (c)->id == id)
5754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5757 else if (connection_get_prev_hop (c)->id == id)
5759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5764 GNUNET_break_op (0);
5768 pid = ntohl (msg->pid);
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5770 pid, fc->last_pid_recv);
5771 fc->last_pid_recv = pid;
5772 connection_send_ack (c, fc == &c->fwd_fc);
5779 * Core handler for mesh keepalives.
5781 * @param cls closure
5782 * @param message message
5783 * @param peer peer identity this notification is about
5784 * @return GNUNET_OK to keep the connection open,
5785 * GNUNET_SYSERR to close it (signal serious error)
5787 * TODO: Check who we got this from, to validate route.
5790 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5791 const struct GNUNET_MessageHeader *message)
5793 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5794 struct MeshConnection *c;
5795 struct MeshPeer *neighbor;
5798 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5802 c = connection_get (&msg->cid);
5805 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5810 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5811 GNUNET_YES : GNUNET_NO;
5813 /* Check if origin is as expected */
5814 neighbor = connection_get_hop (c, fwd);
5815 if (peer_get (peer)->id != neighbor->id)
5817 GNUNET_break_op (0);
5821 connection_change_state (c, MESH_CONNECTION_READY);
5822 connection_reset_timeout (c, fwd);
5824 if (connection_is_terminal (c, fwd))
5827 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5828 send_prebuilt_message_connection (message, c, NULL, fwd);
5836 * Functions to handle messages from core
5838 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5839 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5841 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5842 sizeof (struct GNUNET_MESH_ConnectionACK)},
5843 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5844 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5845 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5846 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5847 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5848 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5849 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5850 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5851 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5852 sizeof (struct GNUNET_MESH_ACK)},
5853 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5854 sizeof (struct GNUNET_MESH_Poll)},
5855 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5856 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5862 * Function to process paths received for a new peer addition. The recorded
5863 * paths form the initial tunnel, which can be optimized later.
5864 * Called on each result obtained for the DHT search.
5866 * @param cls closure
5867 * @param exp when will this value expire
5868 * @param key key of the result
5869 * @param get_path path of the get request
5870 * @param get_path_length lenght of get_path
5871 * @param put_path path of the put request
5872 * @param put_path_length length of the put_path
5873 * @param type type of the result
5874 * @param size number of bytes in data
5875 * @param data pointer to the result data
5878 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5879 const struct GNUNET_HashCode * key,
5880 const struct GNUNET_PeerIdentity *get_path,
5881 unsigned int get_path_length,
5882 const struct GNUNET_PeerIdentity *put_path,
5883 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5884 size_t size, const void *data)
5886 struct MeshPeer *peer = cls;
5887 struct MeshPeerPath *p;
5888 struct MeshConnection *c;
5889 struct GNUNET_PeerIdentity pi;
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5893 GNUNET_PEER_resolve (peer->id, &pi);
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5896 p = path_build_from_dht (get_path, get_path_length,
5897 put_path, put_path_length);
5898 path_add_to_peers (p, GNUNET_NO);
5901 /* Count connections */
5902 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5904 /* If we already have 3 (or more (?!)) connections, it's enough */
5908 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5911 peer_connect (peer);
5917 /******************************************************************************/
5918 /********************* MESH LOCAL HANDLES **************************/
5919 /******************************************************************************/
5923 * Handler for client connection.
5925 * @param cls Closure (unused).
5926 * @param client Client handler.
5929 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5931 struct MeshClient *c;
5935 c = GNUNET_malloc (sizeof (struct MeshClient));
5937 c->id = next_client_id++; /* overflow not important: just for debug */
5938 GNUNET_SERVER_client_keep (client);
5939 GNUNET_SERVER_client_set_user_context (client, c);
5940 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5945 * Handler for client disconnection
5947 * @param cls closure
5948 * @param client identification of the client; NULL
5949 * for the last call when the server is destroyed
5952 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5954 struct MeshClient *c;
5956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
5959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5963 c = client_get (client);
5966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
5968 GNUNET_SERVER_client_drop (c->handle);
5969 c->shutting_down = GNUNET_YES;
5970 if (NULL != c->own_channels)
5972 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
5973 &channel_destroy_iterator, c);
5974 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
5977 if (NULL != c->incoming_channels)
5979 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
5980 &channel_destroy_iterator, c);
5981 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
5984 if (NULL != c->ports)
5986 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
5987 &client_release_ports, c);
5988 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
5990 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
5991 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
5997 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
5999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6005 * Handler for new clients
6007 * @param cls closure
6008 * @param client identification of the client
6009 * @param message the actual message, which includes messages the client wants
6012 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6013 const struct GNUNET_MessageHeader *message)
6015 struct GNUNET_MESH_ClientConnect *cc_msg;
6016 struct MeshClient *c;
6021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6023 /* Check data sanity */
6024 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6025 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6026 if (0 != (size % sizeof (uint32_t)))
6029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6032 size /= sizeof (uint32_t);
6034 /* Initialize new client structure */
6035 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6042 p = (uint32_t *) &cc_msg[1];
6043 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6044 for (i = 0; i < size; i++)
6047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6049 /* store in client's hashmap */
6050 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6052 /* store in global hashmap */
6053 /* FIXME only allow one client to have the port open,
6054 * have a backup hashmap with waiting clients */
6055 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6060 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6061 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6062 GNUNET_SERVER_notification_context_add (nc, client);
6063 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6071 * Handler for requests of new tunnels
6073 * @param cls Closure.
6074 * @param client Identification of the client.
6075 * @param message The actual message.
6078 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6079 const struct GNUNET_MessageHeader *message)
6081 struct GNUNET_MESH_ChannelMessage *msg;
6082 struct MeshPeer *peer;
6083 struct MeshTunnel2 *t;
6084 struct MeshChannel *ch;
6085 struct MeshClient *c;
6086 MESH_ChannelNumber chid;
6088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6090 /* Sanity check for client registration */
6091 if (NULL == (c = client_get (client)))
6094 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6099 /* Message size sanity check */
6100 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6107 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6109 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6110 chid = ntohl (msg->channel_id);
6112 /* Sanity check for duplicate channel IDs */
6113 if (NULL != channel_get_by_local_id (c, chid))
6116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6120 peer = peer_get (&msg->peer);
6121 if (NULL == peer->tunnel)
6123 peer->tunnel = tunnel_new ();
6124 peer->tunnel->peer = peer;
6128 /* Create channel */
6129 ch = channel_new (t, c, chid);
6133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6136 ch->port = ntohl (msg->port);
6137 channel_set_options (ch, ntohl (msg->opt));
6139 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6140 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
6141 ch->fwd_rel->ch = ch;
6142 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
6144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6145 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6146 peer_connect (peer);
6148 /* Send create channel */
6150 struct GNUNET_MESH_ChannelCreate msgcc;
6152 msgcc.header.size = htons (sizeof (msgcc));
6153 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6154 msgcc.chid = htonl (ch->gid);
6155 msgcc.port = msg->port;
6156 msgcc.opt = msg->opt;
6158 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6161 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6167 * Handler for requests of deleting tunnels
6169 * @param cls closure
6170 * @param client identification of the client
6171 * @param message the actual message
6174 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6175 const struct GNUNET_MessageHeader *message)
6177 struct GNUNET_MESH_ChannelMessage *msg;
6178 struct MeshClient *c;
6179 struct MeshChannel *ch;
6180 struct MeshTunnel2 *t;
6181 MESH_ChannelNumber chid;
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6184 "Got a DESTROY CHANNEL from client!\n");
6186 /* Sanity check for client registration */
6187 if (NULL == (c = client_get (client)))
6190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6195 /* Message sanity check */
6196 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6203 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6205 /* Retrieve tunnel */
6206 chid = ntohl (msg->channel_id);
6207 ch = channel_get_by_local_id (c, chid);
6210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6216 /* Cleanup after the tunnel */
6217 client_delete_channel (c, ch);
6218 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6222 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6229 " channel %X client %p (%p, %p)\n",
6230 chid, c, ch->root, ch->dest);
6235 channel_destroy (ch);
6236 tunnel_destroy_if_empty (t);
6238 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6244 * Handler for client traffic
6246 * @param cls closure
6247 * @param client identification of the client
6248 * @param message the actual message
6251 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6252 const struct GNUNET_MessageHeader *message)
6254 struct GNUNET_MESH_LocalData *msg;
6255 struct MeshClient *c;
6256 struct MeshChannel *ch;
6257 MESH_ChannelNumber chid;
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6262 "Got data from a client!\n");
6264 /* Sanity check for client registration */
6265 if (NULL == (c = client_get (client)))
6268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6273 msg = (struct GNUNET_MESH_LocalData *) message;
6275 /* Sanity check for message size */
6276 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6277 if (size < sizeof (struct GNUNET_MessageHeader))
6280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6284 /* Channel exists? */
6285 chid = ntohl (msg->id);
6286 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6287 ch = channel_get_by_local_id (c, chid);
6291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6295 /* Is the client in the channel? */
6298 ch->root->handle == client)
6302 ch->dest->handle == client) ) )
6305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6310 ch->blocked_fwd = GNUNET_YES;
6312 ch->blocked_bck = GNUNET_YES;
6314 /* Ok, everything is correct, send the message. */
6316 struct GNUNET_MESH_Data *payload;
6317 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6318 unsigned char cbuf[p2p_size];
6321 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6322 payload = (struct GNUNET_MESH_Data *) cbuf;
6323 payload->mid = htonl (*mid);
6325 memcpy (&payload[1], &msg[1], size);
6326 payload->header.size = htons (p2p_size);
6327 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6328 payload->chid = htonl (ch->gid);
6329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6330 send_prebuilt_message_channel (&payload->header, ch, fwd);
6332 if (GNUNET_YES == ch->reliable)
6333 channel_save_copy (ch, &payload->header, fwd);
6335 if (tunnel_get_buffer (ch->t, fwd) > 0)
6336 send_local_ack (ch, c, fwd);
6337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6338 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6345 * Handler for client's ACKs for payload traffic.
6347 * @param cls Closure (unused).
6348 * @param client Identification of the client.
6349 * @param message The actual message.
6352 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6353 const struct GNUNET_MessageHeader *message)
6355 struct GNUNET_MESH_LocalAck *msg;
6356 struct MeshChannelReliability *rel;
6357 struct MeshChannel *ch;
6358 struct MeshClient *c;
6359 MESH_ChannelNumber chid;
6362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6364 /* Sanity check for client registration */
6365 if (NULL == (c = client_get (client)))
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6373 msg = (struct GNUNET_MESH_LocalAck *) message;
6375 /* Channel exists? */
6376 chid = ntohl (msg->channel_id);
6377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6378 ch = channel_get_by_local_id (c, chid);
6382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6388 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6389 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6391 rel->client_ready = GNUNET_YES;
6392 channel_send_client_buffered_data (ch, c, rel);
6393 channel_send_connection_ack (ch, 64 - rel->n_recv, fwd);
6395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6402 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6404 * @param cls Closure (client handle).
6405 * @param key Key (hashed tunnel ID, unused).
6406 * @param value Tunnel info.
6408 * @return GNUNET_YES, to keep iterating.
6411 // monitor_all_tunnels_iterator (void *cls,
6412 // const struct GNUNET_HashCode * key,
6415 // struct GNUNET_SERVER_Client *client = cls;
6416 // struct MeshChannel *ch = value;
6417 // struct GNUNET_MESH_LocalMonitor *msg;
6419 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6420 // msg->channel_id = htonl (ch->gid);
6421 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6422 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6424 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6425 // "* sending info about tunnel %s\n",
6426 // GNUNET_i2s (&msg->owner));
6428 // GNUNET_SERVER_notification_context_unicast (nc, client,
6429 // &msg->header, GNUNET_NO);
6430 // return GNUNET_YES;
6435 * Handler for client's MONITOR request.
6437 * @param cls Closure (unused).
6438 * @param client Identification of the client.
6439 * @param message The actual message.
6442 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6443 const struct GNUNET_MessageHeader *message)
6445 struct MeshClient *c;
6447 /* Sanity check for client registration */
6448 if (NULL == (c = client_get (client)))
6451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6456 "Received get tunnels request from client %u\n",
6458 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6459 // monitor_all_tunnels_iterator,
6461 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6462 "Get tunnels request from client %u completed\n",
6464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6469 * Handler for client's MONITOR_TUNNEL request.
6471 * @param cls Closure (unused).
6472 * @param client Identification of the client.
6473 * @param message The actual message.
6476 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6477 const struct GNUNET_MessageHeader *message)
6479 const struct GNUNET_MESH_LocalMonitor *msg;
6480 struct GNUNET_MESH_LocalMonitor *resp;
6481 struct MeshClient *c;
6482 struct MeshChannel *ch;
6484 /* Sanity check for client registration */
6485 if (NULL == (c = client_get (client)))
6488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6492 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6493 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6494 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6497 ntohl (msg->channel_id));
6498 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6502 /* We don't know the tunnel */
6503 struct GNUNET_MESH_LocalMonitor warn;
6506 GNUNET_SERVER_notification_context_unicast (nc, client,
6509 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6513 /* Initialize context */
6514 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6516 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6517 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6518 &resp->header, GNUNET_NO);
6521 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6522 "Monitor tunnel request from client %u completed\n",
6524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6529 * Functions to handle messages from clients
6531 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6532 {&handle_local_new_client, NULL,
6533 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6534 {&handle_local_channel_create, NULL,
6535 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6536 sizeof (struct GNUNET_MESH_ChannelMessage)},
6537 {&handle_local_channel_destroy, NULL,
6538 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6539 sizeof (struct GNUNET_MESH_ChannelMessage)},
6540 {&handle_local_data, NULL,
6541 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6542 {&handle_local_ack, NULL,
6543 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6544 sizeof (struct GNUNET_MESH_LocalAck)},
6545 {&handle_local_get_tunnels, NULL,
6546 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6547 sizeof (struct GNUNET_MessageHeader)},
6548 {&handle_local_show_tunnel, NULL,
6549 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6550 sizeof (struct GNUNET_MESH_LocalMonitor)},
6556 * Method called whenever a given peer connects.
6558 * @param cls closure
6559 * @param peer peer identity this notification is about
6562 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6564 struct MeshPeer *pi;
6565 struct MeshPeerPath *path;
6567 DEBUG_CONN ("Peer connected\n");
6568 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6569 pi = peer_get (peer);
6572 DEBUG_CONN (" (self)\n");
6573 path = path_new (1);
6577 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6578 path = path_new (2);
6579 path->peers[1] = pi->id;
6580 GNUNET_PEER_change_rc (pi->id, 1);
6581 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6583 path->peers[0] = myid;
6584 GNUNET_PEER_change_rc (myid, 1);
6585 peer_add_path (pi, path, GNUNET_YES);
6587 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6593 * Method called whenever a peer disconnects.
6595 * @param cls closure
6596 * @param peer peer identity this notification is about
6599 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6601 struct MeshPeer *pi;
6603 DEBUG_CONN ("Peer disconnected\n");
6604 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6611 peer_remove_path (pi, myid, pi->id);
6613 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6616 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6617 pi->connections = NULL;
6620 DEBUG_CONN (" (self)\n");
6622 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6629 * Install server (service) handlers and start listening to clients.
6634 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6635 GNUNET_SERVER_connect_notify (server_handle,
6636 &handle_local_client_connect, NULL);
6637 GNUNET_SERVER_disconnect_notify (server_handle,
6638 &handle_local_client_disconnect, NULL);
6639 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6641 clients_head = NULL;
6642 clients_tail = NULL;
6644 GNUNET_SERVER_resume (server_handle);
6649 * To be called on core init/fail.
6651 * @param cls Closure (config)
6652 * @param server handle to the server for this service
6653 * @param identity the public identity of this peer
6656 core_init (void *cls, struct GNUNET_CORE_Handle *server,
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 GNUNET_break (core_handle == server);
6664 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6670 GNUNET_i2s (identity));
6671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6673 GNUNET_i2s (&my_full_id));
6674 GNUNET_CORE_disconnect (core_handle);
6675 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6676 NULL, /* Closure passed to MESH functions */
6677 &core_init, /* Call core_init once connected */
6678 &core_connect, /* Handle connects */
6679 &core_disconnect, /* remove peers on disconnects */
6680 NULL, /* Don't notify about all incoming messages */
6681 GNUNET_NO, /* For header only in notification */
6682 NULL, /* Don't notify about all outbound messages */
6683 GNUNET_NO, /* For header-only out notification */
6684 core_handlers); /* Register these handlers */
6693 /******************************************************************************/
6694 /************************ MAIN FUNCTIONS ****************************/
6695 /******************************************************************************/
6698 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6700 * @param cls closure
6701 * @param key current key code
6702 * @param value value in the hash map
6703 * @return GNUNET_YES if we should continue to iterate,
6707 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6709 struct MeshPeer *p = value;
6710 struct MeshTunnel2 *t = p->tunnel;
6719 * Task run during shutdown.
6725 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6729 if (core_handle != NULL)
6731 GNUNET_CORE_disconnect (core_handle);
6734 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6735 if (dht_handle != NULL)
6737 GNUNET_DHT_disconnect (dht_handle);
6742 GNUNET_SERVER_notification_context_destroy (nc);
6745 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6747 GNUNET_SCHEDULER_cancel (announce_id_task);
6748 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6755 * Process mesh requests.
6757 * @param cls closure
6758 * @param server the initialized server
6759 * @param c configuration to use
6762 run (void *cls, struct GNUNET_SERVER_Handle *server,
6763 const struct GNUNET_CONFIGURATION_Handle *c)
6766 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6769 server_handle = server;
6770 GNUNET_SERVER_suspend (server_handle);
6773 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6778 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6779 "mesh", "peer/privatekey");
6780 GNUNET_SCHEDULER_shutdown ();
6785 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6786 &refresh_connection_time))
6788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6790 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6791 "mesh", "refresh path time");
6792 GNUNET_SCHEDULER_shutdown ();
6797 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6802 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6803 "mesh", "id announce time");
6804 GNUNET_SCHEDULER_shutdown ();
6809 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6814 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6815 "mesh", "connect timeout");
6816 GNUNET_SCHEDULER_shutdown ();
6821 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6824 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6826 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6827 "mesh", "max msgs queue");
6828 GNUNET_SCHEDULER_shutdown ();
6833 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6838 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6839 "mesh", "max tunnels");
6840 GNUNET_SCHEDULER_shutdown ();
6845 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6850 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6851 "mesh", "default ttl", 64);
6856 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6860 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6861 "mesh", "max peers", 1000);
6866 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6873 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6874 "Mesh is running with drop mode enabled. "
6875 "This is NOT a good idea! "
6876 "Remove the DROP_PERCENT option from your configuration.\n");
6880 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6881 &dht_replication_level))
6883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6885 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6886 "mesh", "dht replication level", 3);
6887 dht_replication_level = 3;
6890 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6891 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6892 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6894 dht_handle = GNUNET_DHT_connect (c, 64);
6895 if (NULL == dht_handle)
6899 stats = GNUNET_STATISTICS_create ("mesh", c);
6901 /* Scheduled the task to clean up when shutdown is called */
6902 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6904 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6905 GNUNET_free (keyfile);
6906 GNUNET_assert (NULL != pk);
6907 my_private_key = pk;
6908 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6909 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6910 &my_full_id.hashPubKey);
6911 myid = GNUNET_PEER_intern (&my_full_id);
6912 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6913 "Mesh for peer [%s] starting\n",
6914 GNUNET_i2s(&my_full_id));
6916 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6917 NULL, /* Closure passed to MESH functions */
6918 &core_init, /* Call core_init once connected */
6919 &core_connect, /* Handle connects */
6920 &core_disconnect, /* remove peers on disconnects */
6921 NULL, /* Don't notify about all incoming messages */
6922 GNUNET_NO, /* For header only in notification */
6923 NULL, /* Don't notify about all outbound messages */
6924 GNUNET_NO, /* For header-only out notification */
6925 core_handlers); /* Register these handlers */
6926 if (NULL == core_handle)
6929 GNUNET_SCHEDULER_shutdown ();
6932 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6938 * The main function for the mesh service.
6940 * @param argc number of arguments from the command line
6941 * @param argv command line arguments
6942 * @return 0 ok, 1 on error
6945 main (int argc, char *const *argv)
6950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6951 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6953 ret = (GNUNET_OK == r) ? 0 : 1;
6954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");