2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-enc.c
23 * @brief GNUnet MESH service with encryption
24 * @author Bartlomiej Polot
27 * - when sending in-order buffered data, wait for client ACKs
30 * - set connection IDs independently from tunnel, tunnel has no ID
33 * - relay corking down to core
34 * - set ttl relative to path length
38 * - peer: other mesh instance. If there is direct connection it's a neighbor.
39 * - tunnel: encrypted connection to a peer, neighbor or not.
40 * - channel: connection between two clients, on the same or different peers.
41 * have properties like reliability.
42 * - path: series of directly connected peer from one peer to another.
43 * - connection: path which is being used in a tunnel.
47 #include "gnunet_crypto_lib.h"
49 #include "mesh_protocol_enc.h"
50 #include "mesh_path.h"
51 #include "block_mesh.h"
52 #include "gnunet_dht_service.h"
53 #include "gnunet_statistics_service.h"
55 #define MESH_BLOOM_SIZE 128
57 #define MESH_DEBUG_DHT GNUNET_NO
58 #define MESH_DEBUG_CONNECTION GNUNET_YES
59 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
61 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
62 GNUNET_TIME_UNIT_MINUTES,\
64 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
65 #define MESH_RETRANSMIT_MARGIN 4
67 #if MESH_DEBUG_CONNECTION
68 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
70 #define DEBUG_CONN(...)
74 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
76 #define DEBUG_DHT(...)
83 struct timespec __mesh_start;
84 struct timespec __mesh_end;
85 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
86 #define INTERVAL_END \
88 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
89 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
90 if (__diff < 0) __diff += 1000000000;\
94 #define INTERVAL_SHOW \
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
98 #define INTERVAL_START
100 #define INTERVAL_SHOW
104 * All the states a tunnel can be in.
109 * Uninitialized status, should never appear in operation.
114 * Path to the peer not known yet
116 MESH_TUNNEL_SEARCHING,
119 * Request sent, not yet answered.
124 * Peer connected and ready to accept data
129 * Peer connected previosly but not responding
131 MESH_TUNNEL_RECONNECTING
136 * All the states a connection can be in.
138 enum MeshConnectionState
141 * Uninitialized status, should never appear in operation.
146 * Connection create message sent, waiting for ACK.
148 MESH_CONNECTION_SENT,
151 * Connection ACK sent, waiting for ACK.
156 * Connection confirmed, ready to carry traffic.
158 MESH_CONNECTION_READY,
163 * All the states a connection can be in.
165 enum MeshChannelState
168 * Uninitialized status, should never appear in operation.
173 * Connection create message sent, waiting for ACK.
178 * Connection confirmed, ready to carry traffic..
184 /******************************************************************************/
185 /************************ DATA STRUCTURES ****************************/
186 /******************************************************************************/
188 /** FWD declaration */
192 struct MeshConnection;
194 struct MeshChannelReliability;
198 * Struct containing info about a queued transmission to this peer
205 struct MeshPeerQueue *next;
210 struct MeshPeerQueue *prev;
213 * Peer this transmission is directed to.
215 struct MeshPeer *peer;
218 * Connection this message belongs to.
220 struct MeshConnection *c;
228 * Channel this message belongs to, if known.
230 struct MeshChannel *ch;
233 * Pointer to info stucture used as cls.
243 * Size of the message
250 * Struct to encapsulate all the Flow Control information to a peer to which
251 * we are directly connected (on a core level).
253 struct MeshFlowControl
256 * Connection this controls.
258 struct MeshConnection *c;
261 * How many messages are in the queue on this connection.
263 unsigned int queue_n;
266 * How many messages do we accept in the queue.
268 unsigned int queue_max;
276 * ID of the last packet sent towards the peer.
278 uint32_t last_pid_sent;
281 * ID of the last packet received from the peer.
283 uint32_t last_pid_recv;
286 * Last ACK sent to the peer (peer can't send more than this PID).
288 uint32_t last_ack_sent;
291 * Last ACK sent towards the origin (for traffic towards leaf node).
293 uint32_t last_ack_recv;
296 * Task to poll the peer in case of a lost ACK causes stall.
298 GNUNET_SCHEDULER_TaskIdentifier poll_task;
301 * How frequently to poll for ACKs.
303 struct GNUNET_TIME_Relative poll_time;
308 * Struct containing all information regarding a given peer
318 * Last time we heard from this peer
320 struct GNUNET_TIME_Absolute last_contact;
323 * Paths to reach the peer, ordered by ascending hop count
325 struct MeshPeerPath *path_head;
328 * Paths to reach the peer, ordered by ascending hop count
330 struct MeshPeerPath *path_tail;
333 * Handle to stop the DHT search for paths to this peer
335 struct GNUNET_DHT_GetHandle *dhtget;
338 * Tunnel to this peer, if any.
340 struct MeshTunnel2 *tunnel;
343 * Connections that go through this peer, indexed by tid;
345 struct GNUNET_CONTAINER_MultiHashMap *connections;
348 * Handle for queued transmissions
350 struct GNUNET_CORE_TransmitHandle *core_transmit;
353 * Transmission queue to core DLL head
355 struct MeshPeerQueue *queue_head;
358 * Transmission queue to core DLL tail
360 struct MeshPeerQueue *queue_tail;
363 * How many messages are in the queue to this peer.
365 unsigned int queue_n;
370 * Info needed to retry a message in case it gets lost.
372 struct MeshReliableMessage
375 * Double linked list, FIFO style
377 struct MeshReliableMessage *next;
378 struct MeshReliableMessage *prev;
381 * Type of message (payload, channel management).
386 * Tunnel Reliability queue this message is in.
388 struct MeshChannelReliability *rel;
391 * ID of the message (ACK needed to free)
396 * When was this message issued (to calculate ACK delay)
398 struct GNUNET_TIME_Absolute timestamp;
400 /* struct GNUNET_MESH_Data with payload */
404 struct MeshChannelReliability
407 * Channel this is about.
409 struct MeshChannel *ch;
412 * DLL of messages sent and not yet ACK'd.
414 struct MeshReliableMessage *head_sent;
415 struct MeshReliableMessage *tail_sent;
418 * Messages pending to send.
423 * DLL of messages received out of order.
425 struct MeshReliableMessage *head_recv;
426 struct MeshReliableMessage *tail_recv;
434 * Can we send data to the client?
439 * Task to resend/poll in case no ACK is received.
441 GNUNET_SCHEDULER_TaskIdentifier retry_task;
444 * Counter for exponential backoff.
446 struct GNUNET_TIME_Relative retry_timer;
449 * How long does it usually take to get an ACK.
451 struct GNUNET_TIME_Relative expected_delay;
456 * Struct containing all information regarding a channel to a remote client.
461 * Tunnel this channel is in.
463 struct MeshTunnel2 *t;
466 * Double linked list.
468 struct MeshChannel *next;
469 struct MeshChannel *prev;
472 * Destination port of the channel.
477 * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
479 MESH_ChannelNumber gid;
482 * Local tunnel number for root (owner) client.
483 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
485 MESH_ChannelNumber lid_root;
488 * Local tunnel number for local destination clients (incoming number)
489 * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
491 MESH_ChannelNumber lid_dest;
496 enum MeshChannelState state;
499 * Next MID to use for fwd traffic.
501 uint32_t mid_send_fwd;
504 * Next MID expected for fwd traffic.
506 uint32_t mid_recv_fwd;
509 * Next MID to use for bck traffic.
511 uint32_t mid_send_bck;
514 * Next MID expected for bck traffic.
516 uint32_t mid_recv_bck;
519 * Is the tunnel bufferless (minimum latency)?
524 * Is the tunnel reliable?
529 * Last time the channel was used
531 struct GNUNET_TIME_Absolute timestamp;
534 * Client owner of the tunnel, if any
536 struct MeshClient *root;
539 * Client destination of the tunnel, if any.
541 struct MeshClient *dest;
544 * Flag to signal the destruction of the channel.
545 * If this is set GNUNET_YES the channel will be destroyed
546 * when the queue is empty.
551 * Total messages pending for this channel, payload or not.
553 unsigned int pending_messages;
557 * Only present (non-NULL) at the owner of a tunnel.
559 struct MeshChannelReliability *fwd_rel;
563 * Only present (non-NULL) at the destination of a tunnel.
565 struct MeshChannelReliability *bck_rel;
568 * Is the root prevented from sending more data? (We "owe" him an ACK).
573 * Is the dest prevented from sending more data? (We "owe" him an ACK).
580 * Struct containing all information regarding a connection to a peer.
582 struct MeshConnection
587 struct MeshConnection *next;
588 struct MeshConnection *prev;
591 * Tunnel this connection is part of.
593 struct MeshTunnel2 *t;
596 * Flow control information for traffic fwd.
598 struct MeshFlowControl fwd_fc;
601 * Flow control information for traffic bck.
603 struct MeshFlowControl bck_fc;
606 * ID of the connection.
608 struct GNUNET_HashCode id;
611 * State of the connection.
613 enum MeshConnectionState state;
616 * Path being used for the tunnel.
618 struct MeshPeerPath *path;
621 * Position of the local peer in the path.
623 unsigned int own_pos;
626 * Task to keep the used paths alive at the owner,
627 * time tunnel out on all the other peers.
629 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
632 * Task to keep the used paths alive at the destination,
633 * time tunnel out on all the other peers.
635 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
638 * Pending message count.
640 int pending_messages;
643 * Destroy flag: if true, destroy on last message.
650 * Struct used to queue messages in a tunnel.
652 struct MeshTunnelQueue
657 struct MeshTunnelQueue *next;
658 struct MeshTunnelQueue *prev;
663 struct MeshChannel *ch;
668 /* struct GNUNET_MessageHeader *msg; */
673 * Struct containing all information regarding a tunnel to a peer.
678 * Endpoint of the tunnel.
680 struct MeshPeer *peer;
683 * State of the tunnel.
685 enum MeshTunnelState state;
688 * Local peer ephemeral private key
690 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
693 * Local peer ephemeral public key
695 struct GNUNET_CRYPTO_EccPublicKey *my_eph;
698 * Remote peer's public key.
700 struct GNUNET_CRYPTO_EccPublicKey *peers_eph;
703 * Encryption ("our") key.
705 struct GNUNET_CRYPTO_AesSessionKey e_key;
708 * Decryption ("their") key.
710 struct GNUNET_CRYPTO_AesSessionKey d_key;
713 * Paths that are actively used to reach the destination peer.
715 struct MeshConnection *connection_head;
716 struct MeshConnection *connection_tail;
719 * Next connection number.
724 * Channels inside this tunnel.
726 struct MeshChannel *channel_head;
727 struct MeshChannel *channel_tail;
730 * Channel ID for the next created channel.
732 MESH_ChannelNumber next_chid;
735 * Channel ID for the next incoming channel.
737 MESH_ChannelNumber next_local_chid;
740 * Pending message count.
742 int pending_messages;
745 * Destroy flag: if true, destroy on last message.
750 * Queued messages, to transmit once tunnel gets connected.
752 struct MeshTunnelQueue *tq_head;
753 struct MeshTunnelQueue *tq_tail;
759 * Struct containing information about a client of the service
761 * TODO: add a list of 'waiting' ports
768 struct MeshClient *next;
773 struct MeshClient *prev;
776 * Tunnels that belong to this client, indexed by local id
778 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
781 * Tunnels this client has accepted, indexed by incoming local id
783 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
786 * Handle to communicate with the client
788 struct GNUNET_SERVER_Client *handle;
791 * Ports that this client has declared interest in.
792 * Indexed by port, contains *Client.
794 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
797 * Whether the client is active or shutting down (don't send confirmations
798 * to a client that is shutting down.
803 * ID of the client, mainly for debug messages
809 /******************************************************************************/
810 /************************ DEBUG FUNCTIONS ****************************/
811 /******************************************************************************/
815 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
816 * @param cls string to print
817 * @param success GNUNET_OK if the PUT was transmitted,
818 * GNUNET_NO on timeout,
819 * GNUNET_SYSERR on disconnect from service
820 * after the PUT message was transmitted
821 * (so we don't know if it was received or not)
826 mesh_debug (void *cls, int success)
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
836 /******************************************************************************/
837 /*********************** GLOBAL VARIABLES ****************************/
838 /******************************************************************************/
840 /************************** Configuration parameters **************************/
843 * How often to send path keepalives. Paths timeout after 4 missed.
845 static struct GNUNET_TIME_Relative refresh_connection_time;
848 * How often to PUT own ID in the DHT.
850 static struct GNUNET_TIME_Relative id_announce_time;
853 * Maximum time allowed to connect to a peer found by string.
855 static struct GNUNET_TIME_Relative connect_timeout;
858 * Default TTL for payload packets.
860 static unsigned long long default_ttl;
863 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
865 static unsigned long long dht_replication_level;
868 * How many connections are we willing to maintain.
869 * Local connections are always allowed, even if there are more connections than max.
871 static unsigned long long max_connections;
874 * How many messages *in total* are we willing to queue, divide by number of
875 * connections to get connection queue size.
877 static unsigned long long max_msgs_queue;
880 * How many peers do we want to remember?
882 static unsigned long long max_peers;
885 * Percentage of messages that will be dropped (for test purposes only).
887 static unsigned long long drop_percent;
889 /*************************** Static global variables **************************/
892 * DLL with all the clients, head.
894 static struct MeshClient *clients_head;
897 * DLL with all the clients, tail.
899 static struct MeshClient *clients_tail;
902 * Connections known, indexed by cid (MeshConnection).
904 static struct GNUNET_CONTAINER_MultiHashMap *connections;
907 * Peers known, indexed by PeerIdentity (MeshPeer).
909 static struct GNUNET_CONTAINER_MultiHashMap *peers;
912 * Handle to communicate with core.
914 static struct GNUNET_CORE_Handle *core_handle;
919 static struct GNUNET_DHT_Handle *dht_handle;
922 * Handle to server lib.
924 static struct GNUNET_SERVER_Handle *server_handle;
927 * Handle to the statistics service.
929 static struct GNUNET_STATISTICS_Handle *stats;
932 * Notification context, to send messages to local clients.
934 static struct GNUNET_SERVER_NotificationContext *nc;
937 * Local peer own ID (memory efficient handle).
939 static GNUNET_PEER_Id myid;
942 * Local peer own ID (full value).
944 static struct GNUNET_PeerIdentity my_full_id;
949 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
954 static struct GNUNET_CRYPTO_EccPublicKey my_public_key;
957 * All ports clients of this peer have opened.
959 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
962 * Task to periodically announce itself in the network.
964 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
967 * Next ID to assign to a client.
969 unsigned int next_client_id;
972 /******************************************************************************/
973 /*********************** DECLARATIONS **************************/
974 /******************************************************************************/
977 * Function to process paths received for a new peer addition. The recorded
978 * paths form the initial tunnel, which can be optimized later.
979 * Called on each result obtained for the DHT search.
982 * @param exp when will this value expire
983 * @param key key of the result
984 * @param type type of the result
985 * @param size number of bytes in data
986 * @param data pointer to the result data
989 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
990 const struct GNUNET_HashCode * key,
991 const struct GNUNET_PeerIdentity *get_path,
992 unsigned int get_path_length,
993 const struct GNUNET_PeerIdentity *put_path,
994 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
995 size_t size, const void *data);
999 * Retrieve the MeshPeer stucture associated with the peer, create one
1000 * and insert it in the appropriate structures if the peer is not known yet.
1002 * @param peer Full identity of the peer.
1004 * @return Existing or newly created peer info.
1006 static struct MeshPeer *
1007 peer_get (const struct GNUNET_PeerIdentity *peer);
1011 * Retrieve the MeshPeer stucture associated with the peer, create one
1012 * and insert it in the appropriate structures if the peer is not known yet.
1014 * @param peer Short identity of the peer.
1016 * @return Existing or newly created peer info.
1018 static struct MeshPeer *
1019 peer_get_short (const GNUNET_PEER_Id peer);
1023 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1024 * to obtain a local peer -> destination path and interning the peer ids.
1026 * @return Newly allocated and created path
1028 static struct MeshPeerPath *
1029 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1030 unsigned int get_path_length,
1031 const struct GNUNET_PeerIdentity *put_path,
1032 unsigned int put_path_length);
1036 * Adds a path to the data structs of all the peers in the path
1038 * @param p Path to process.
1039 * @param confirmed Whether we know if the path works or not.
1042 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1046 * Search for a tunnel by global ID using full PeerIdentities.
1048 * @param t Tunnel containing the channel.
1049 * @param chid Public channel number.
1051 * @return channel handler, NULL if doesn't exist
1053 static struct MeshChannel *
1054 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid);
1058 * Change the tunnel state.
1060 * @param t Tunnel whose state to change.
1061 * @param state New state.
1064 tunnel_change_state (struct MeshTunnel2 *t, enum MeshTunnelState state);
1068 * Notify a tunnel that a connection has broken that affects at least
1069 * some of its peers.
1071 * @param t Tunnel affected.
1072 * @param p1 Peer that got disconnected from p2.
1073 * @param p2 Peer that got disconnected from p1.
1075 * @return Short ID of the peer disconnected (either p1 or p2).
1076 * 0 if the tunnel remained unaffected.
1078 static GNUNET_PEER_Id
1079 tunnel_notify_connection_broken (struct MeshTunnel2 *t,
1080 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2);
1083 * @brief Use the given path for the tunnel.
1084 * Update the next and prev hops (and RCs).
1085 * (Re)start the path refresh in case the tunnel is locally owned.
1087 * @param t Tunnel to update.
1088 * @param p Path to use.
1090 * @return Connection created.
1092 static struct MeshConnection *
1093 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p);
1096 * Tunnel is empty: destroy it.
1098 * Notifies all participants (peers, cleints) about the destruction.
1100 * @param t Tunnel to destroy.
1103 tunnel_destroy_empty (struct MeshTunnel2 *t);
1106 * Destroy the tunnel.
1108 * This function does not generate any warning traffic to clients or peers.
1111 * Cancel messages belonging to this tunnel queued to neighbors.
1112 * Free any allocated resources linked to the tunnel.
1114 * @param t The tunnel to destroy.
1117 tunnel_destroy (struct MeshTunnel2 *t);
1120 * Create a connection.
1122 * @param cid Connection ID.
1124 static struct MeshConnection *
1125 connection_new (const struct GNUNET_HashCode *cid);
1128 * Connection is no longer needed: destroy it and remove from tunnel.
1130 * @param c Connection to destroy.
1133 connection_destroy (struct MeshConnection *c);
1136 * Send FWD keepalive packets for a connection.
1138 * @param cls Closure (connection for which to send the keepalive).
1139 * @param tc Notification context.
1142 connection_fwd_keepalive (void *cls,
1143 const struct GNUNET_SCHEDULER_TaskContext *tc);
1146 * Send BCK keepalive packets for a connection.
1148 * @param cls Closure (connection for which to send the keepalive).
1149 * @param tc Notification context.
1152 connection_bck_keepalive (void *cls,
1153 const struct GNUNET_SCHEDULER_TaskContext *tc);
1157 * Change the tunnel state.
1159 * @param c Connection whose state to change.
1160 * @param state New state.
1163 connection_change_state (struct MeshConnection* c,
1164 enum MeshConnectionState state);
1169 * @brief Queue and pass message to core when possible.
1171 * @param cls Closure (@c type dependant). It will be used by queue_send to
1172 * build the message to be sent if not already prebuilt.
1173 * @param type Type of the message, 0 for a raw message.
1174 * @param size Size of the message.
1175 * @param c Connection this message belongs to (cannot be NULL).
1176 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1177 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1180 queue_add (void* cls,
1183 struct MeshConnection* c,
1184 struct MeshChannel* ch,
1189 * Free a transmission that was already queued with all resources
1190 * associated to the request.
1192 * @param queue Queue handler to cancel.
1193 * @param clear_cls Is it necessary to free associated cls?
1196 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1200 * Core callback to write a queued packet to core buffer
1202 * @param cls Closure (peer info).
1203 * @param size Number of bytes available in buf.
1204 * @param buf Where the to write the message.
1206 * @return number of bytes written to buf
1209 queue_send (void *cls, size_t size, void *buf);
1213 * Dummy function to separate declarations from definitions in function list.
1216 __mesh_divider______________________________________________________________();
1220 * Get string description for tunnel state.
1222 * @param s Tunnel state.
1224 * @return String representation.
1227 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
1229 static char buf[128];
1233 case MESH_TUNNEL_NEW:
1234 return "MESH_TUNNEL_NEW";
1235 case MESH_TUNNEL_SEARCHING:
1236 return "MESH_TUNNEL_SEARCHING";
1237 case MESH_TUNNEL_WAITING:
1238 return "MESH_TUNNEL_WAITING";
1239 case MESH_TUNNEL_READY:
1240 return "MESH_TUNNEL_READY";
1241 case MESH_TUNNEL_RECONNECTING:
1242 return "MESH_TUNNEL_RECONNECTING";
1245 sprintf (buf, "%u (UNKNOWN STATE)", s);
1252 * Get string description for tunnel state.
1254 * @param s Tunnel state.
1256 * @return String representation.
1259 GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s)
1263 case MESH_CONNECTION_NEW:
1264 return "MESH_CONNECTION_NEW";
1265 case MESH_CONNECTION_SENT:
1266 return "MESH_CONNECTION_SENT";
1267 case MESH_CONNECTION_READY:
1268 return "MESH_CONNECTION_READY";
1270 return "MESH_CONNECTION_STATE_ERROR";
1276 /******************************************************************************/
1277 /************************ PERIODIC FUNCTIONS ****************************/
1278 /******************************************************************************/
1281 * Periodically announce self id in the DHT
1283 * @param cls closure
1284 * @param tc task context
1287 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1289 struct PBlock block;
1291 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1293 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1297 * - Set data expiration in function of X
1298 * - Adapt X to churn
1300 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1302 block.id = my_full_id;
1303 GNUNET_DHT_put (dht_handle, /* DHT handle */
1304 &my_full_id.hashPubKey, /* Key to use */
1305 dht_replication_level, /* Replication level */
1306 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1307 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1308 sizeof (block), /* Size of the data */
1309 (const char *) &block, /* Data itself */
1310 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1311 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1312 NULL, /* Continuation */
1313 NULL); /* Continuation closure */
1315 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1319 /******************************************************************************/
1320 /****************** GENERAL HELPER FUNCTIONS ************************/
1321 /******************************************************************************/
1325 * Get the static string for a peer ID.
1329 * @return Static string for it's ID.
1332 peer2s (const struct MeshPeer *peer)
1336 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
1341 * Get the previous hop in a connection
1343 * @param c Connection.
1345 * @return Previous peer in the connection.
1347 static struct MeshPeer *
1348 connection_get_prev_hop (struct MeshConnection *c)
1352 if (0 == c->own_pos || c->path->length < 2)
1353 id = c->path->peers[0];
1355 id = c->path->peers[c->own_pos - 1];
1357 return peer_get_short (id);
1362 * Get the next hop in a connection
1364 * @param c Connection.
1366 * @return Next peer in the connection.
1368 static struct MeshPeer *
1369 connection_get_next_hop (struct MeshConnection *c)
1373 if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
1374 id = c->path->peers[c->path->length - 1];
1376 id = c->path->peers[c->own_pos + 1];
1378 return peer_get_short (id);
1383 * Get the hop in a connection.
1385 * @param c Connection.
1386 * @param fwd Next hop?
1388 * @return Next peer in the connection.
1390 static struct MeshPeer *
1391 connection_get_hop (struct MeshConnection *c, int fwd)
1394 return connection_get_next_hop (c);
1395 return connection_get_prev_hop (c);
1399 * Check if client has registered with the service and has not disconnected
1401 * @param client the client to check
1403 * @return non-NULL if client exists in the global DLL
1405 static struct MeshClient *
1406 client_get (struct GNUNET_SERVER_Client *client)
1408 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1413 * Deletes a tunnel from a client (either owner or destination).
1415 * @param c Client whose tunnel to delete.
1416 * @param ch Channel which should be deleted.
1419 client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
1425 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1427 if (GNUNET_YES != res)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
1432 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1434 if (GNUNET_YES != res)
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1441 * Notify the appropriate client that a new incoming channel was created.
1443 * @param ch Channel that was created.
1446 send_local_channel_create (struct MeshChannel *ch)
1448 struct GNUNET_MESH_ChannelMessage msg;
1449 struct MeshTunnel2 *t = ch->t;
1451 if (NULL == ch->dest)
1453 msg.header.size = htons (sizeof (msg));
1454 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1455 msg.channel_id = htonl (ch->lid_dest);
1456 msg.port = htonl (ch->port);
1458 msg.opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1459 msg.opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1460 msg.opt = htonl (msg.opt);
1461 GNUNET_PEER_resolve (t->peer->id, &msg.peer);
1462 GNUNET_SERVER_notification_context_unicast (nc, ch->dest->handle,
1463 &msg.header, GNUNET_NO);
1468 * Notify a client that the incoming tunnel is no longer valid.
1470 * @param ch Channel that is destroyed.
1471 * @param fwd Forward notification (owner->dest)?
1474 send_local_channel_destroy (struct MeshChannel *ch, int fwd)
1476 struct GNUNET_MESH_ChannelMessage msg;
1477 struct MeshClient *c;
1479 c = fwd ? ch->dest : ch->root;
1485 msg.header.size = htons (sizeof (msg));
1486 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1487 msg.channel_id = htonl (fwd ? ch->lid_dest : ch->lid_root);
1488 msg.port = htonl (0);
1489 memset (&msg.peer, 0, sizeof (msg.peer));
1490 msg.opt = htonl (0);
1491 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1492 &msg.header, GNUNET_NO);
1497 * Build a local ACK message and send it to a local client.
1499 * @param ch Channel on which to send the ACK.
1500 * @param c Client to whom send the ACK.
1501 * @param fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1504 send_local_ack (struct MeshChannel *ch,
1505 struct MeshClient *c,
1508 struct GNUNET_MESH_LocalAck msg;
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "send local %s ack on %s:%X towards %p\n",
1512 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1515 || ( fwd && (0 == ch->lid_root || c != ch->root))
1516 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1521 msg.header.size = htons (sizeof (msg));
1522 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1523 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1524 GNUNET_SERVER_notification_context_unicast (nc,
1529 ch->blocked_fwd = GNUNET_NO;
1531 ch->blocked_bck = GNUNET_NO;
1536 * Count established (ready) connections of a tunnel.
1538 * @param t Tunnel on which to send the message.
1540 * @return Number of connections.
1543 tunnel_count_connections (struct MeshTunnel2 *t)
1545 struct MeshConnection *c;
1548 for (c = t->connection_head, i = 0; NULL != c; c = c->next, i++);
1555 * Pick a connection on which send the next data message.
1557 * @param t Tunnel on which to send the message.
1558 * @param fwd Is this a fwd message?
1560 * @return The connection on which to send the next message.
1562 static struct MeshConnection *
1563 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
1565 struct MeshConnection *c;
1566 struct MeshConnection *best;
1567 struct MeshFlowControl *fc;
1568 unsigned int lowest_q;
1571 lowest_q = UINT_MAX;
1572 for (c = t->connection_head; NULL != c; c = c->next)
1574 if (MESH_CONNECTION_READY == c->state)
1576 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1582 if (fc->queue_n < lowest_q)
1585 lowest_q = fc->queue_n;
1594 * Get the total buffer space for a tunnel.
1597 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1599 struct MeshConnection *c;
1600 struct MeshFlowControl *fc;
1601 unsigned int buffer;
1603 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1605 if (c->state != MESH_CONNECTION_READY)
1608 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1609 buffer += fc->last_ack_recv - fc->last_pid_sent;
1617 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1618 * Encrypt data with the tunnel key.
1620 * @param t Tunnel whose key to use.
1621 * @param dst Destination for the encrypted data.
1622 * @param src Source of the plaintext.
1623 * @param size Size of the plaintext.
1624 * @param iv Initialization Vector to use.
1625 * @param fwd Is this a fwd message?
1628 tunnel_encrypt (struct MeshTunnel2 *t,
1629 void *dst, const void *src,
1630 size_t size, uint64_t iv, int fwd)
1632 memcpy (dst, src, size);
1637 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1638 * Decrypt data with the tunnel key.
1640 * @param t Tunnel whose key to use.
1641 * @param dst Destination for the plaintext.
1642 * @param src Source of the encrypted data.
1643 * @param size Size of the encrypted data.
1644 * @param iv Initialization Vector to use.
1645 * @param fwd Is this a fwd message?
1648 tunnel_decrypt (struct MeshTunnel2 *t,
1649 void *dst, const void *src,
1650 size_t size, uint64_t iv, int fwd)
1652 memcpy (dst, src, size);
1657 * Sends an already built message on a connection, properly registering
1658 * all used resources.
1660 * @param message Message to send. Function makes a copy of it.
1661 * If message is not hop-by-hop, decrements TTL of copy.
1662 * @param c Connection on which this message is transmitted.
1663 * @param ch Channel on which this message is transmitted, or NULL.
1664 * @param fwd Is this a fwd message?
1667 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1668 struct MeshConnection *c,
1669 struct MeshChannel *ch,
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Connection %s\n",
1677 GNUNET_h2s (&c->id));
1679 size = ntohs (message->size);
1680 data = GNUNET_malloc (size);
1681 memcpy (data, message, size);
1682 type = ntohs (message->type);
1686 struct GNUNET_MESH_Encrypted *emsg;
1687 struct GNUNET_MESH_ACK *amsg;
1688 struct GNUNET_MESH_Poll *pmsg;
1691 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1692 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1693 emsg = (struct GNUNET_MESH_Encrypted *) data;
1694 ttl = ntohl (emsg->ttl);
1697 GNUNET_break_op (0);
1701 emsg->ttl = htonl (ttl - 1);
1702 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1706 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1707 amsg = (struct GNUNET_MESH_ACK *) data;
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1712 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1713 pmsg = (struct GNUNET_MESH_Poll *) data;
1715 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1733 * Sends an already built message on a tunnel, choosing the best connection.
1735 * @param message Message to send. Function modifies it.
1736 * @param t Tunnel on which this message is transmitted.
1737 * @param ch Channel on which this message is transmitted.
1738 * @param fwd Is this a fwd message?
1741 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1742 struct MeshTunnel2 *t,
1743 struct MeshChannel *ch,
1746 struct MeshConnection *c;
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1751 c = tunnel_get_connection (t, fwd);
1754 GNUNET_break (GNUNET_YES == t->destroy);
1757 type = ntohs (msg->header.type);
1760 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1761 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1762 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1763 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1765 msg->ttl = htonl (default_ttl);
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1769 GNUNET_MESH_DEBUG_M2S (type));
1773 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1778 * Sends an already built message on a channel, properly registering
1779 * all used resources and encrypting the message with the tunnel's key.
1781 * @param message Message to send. Function makes a copy of it.
1782 * @param ch Channel on which this message is transmitted.
1783 * @param fwd Is this a fwd message?
1786 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1787 struct MeshChannel *ch,
1790 struct GNUNET_MESH_Encrypted *msg;
1791 size_t size = ntohs (message->size);
1792 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1797 peer2s (ch->t->peer), ch->gid);
1798 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1799 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1801 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1802 msg->header.type = htons (type);
1803 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1804 msg->iv = GNUNET_htonll (iv);
1805 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1807 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1812 * Sends a CREATE CONNECTION message for a path to a peer.
1813 * Changes the connection and tunnel states if necessary.
1815 * @param connection Connection to create.
1818 send_connection_create (struct MeshConnection *connection)
1820 struct MeshTunnel2 *t;
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1825 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1826 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1827 (connection->path->length *
1828 sizeof (struct GNUNET_PeerIdentity)),
1833 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1834 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1835 if (MESH_CONNECTION_NEW == connection->state)
1836 connection_change_state (connection, MESH_CONNECTION_SENT);
1841 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1844 * @param connection Connection to confirm.
1845 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1848 send_connection_ack (struct MeshConnection *connection, int fwd)
1850 struct MeshTunnel2 *t;
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1855 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1856 sizeof (struct GNUNET_MESH_ConnectionACK),
1860 if (MESH_TUNNEL_NEW == t->state)
1861 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1866 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1868 * @param c Which connection to send the hop-by-hop ACK.
1869 * @param ack Value of the ACK.
1870 * @param fwd Is this fwd?
1873 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1875 struct GNUNET_MESH_ACK msg;
1877 msg.header.size = htons (sizeof (msg));
1878 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1879 msg.ack = htonl (ack);
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "connection send %s ack %u on %s\n",
1884 fwd ? "FWD" : "BCK", ack, GNUNET_h2s (&c->id));
1886 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1891 * Core callback to write a pre-constructed data packet to core buffer
1893 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1894 * @param size Number of bytes available in buf.
1895 * @param buf Where the to write the message.
1897 * @return number of bytes written to buf
1900 send_core_data_raw (void *cls, size_t size, void *buf)
1902 struct GNUNET_MessageHeader *msg = cls;
1905 GNUNET_assert (NULL != msg);
1906 total_size = ntohs (msg->size);
1908 if (total_size > size)
1913 memcpy (buf, msg, total_size);
1920 * Function to send a create connection message to a peer.
1922 * @param c Connection to create.
1923 * @param size number of bytes available in buf
1924 * @param buf where the callee should write the message
1925 * @return number of bytes written to buf
1928 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1930 struct GNUNET_MESH_ConnectionCreate *msg;
1931 struct GNUNET_PeerIdentity *peer_ptr;
1932 struct MeshPeerPath *p = c->path;
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1938 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1939 p->length * sizeof (struct GNUNET_PeerIdentity);
1941 if (size < size_needed || NULL == buf)
1946 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1947 msg->header.size = htons (size_needed);
1948 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1951 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1952 for (i = 0; i < p->length; i++)
1954 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1964 * Creates a path ack message in buf and frees all unused resources.
1966 * @param c Connection to send an ACK on.
1967 * @param size number of bytes available in buf
1968 * @param buf where the callee should write the message
1970 * @return number of bytes written to buf
1973 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1975 struct GNUNET_MESH_ConnectionACK *msg = buf;
1976 struct MeshTunnel2 *t = c->t;
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1979 GNUNET_assert (NULL != t);
1980 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1985 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1986 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1989 /* TODO add signature */
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1992 return sizeof (struct GNUNET_MESH_ConnectionACK);
1997 * Destroy the peer_info and free any allocated resources linked to it
1999 * @param peer The peer_info to destroy.
2001 * @return GNUNET_OK on success
2004 peer_destroy (struct MeshPeer *peer)
2006 struct GNUNET_PeerIdentity id;
2007 struct MeshPeerPath *p;
2008 struct MeshPeerPath *nextp;
2010 GNUNET_PEER_resolve (peer->id, &id);
2011 GNUNET_PEER_change_rc (peer->id, -1);
2014 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2018 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2020 if (NULL != peer->dhtget)
2022 GNUNET_DHT_get_stop (peer->dhtget);
2024 p = peer->path_head;
2028 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2032 tunnel_destroy_empty (peer->tunnel);
2039 * Returns if peer is used (has a tunnel, is neighbor).
2041 * @peer Peer to check.
2043 * @return GNUNET_YES if peer is in use.
2046 peer_is_used (struct MeshPeer *peer)
2048 struct MeshPeerPath *p;
2050 if (NULL != peer->tunnel)
2053 for (p = peer->path_head; NULL != p; p = p->next)
2062 * Iterator over all the peers to get the oldest timestamp.
2064 * @param cls Closure (unsued).
2065 * @param key ID of the peer.
2066 * @param value Peer_Info of the peer.
2069 peer_get_oldest (void *cls,
2070 const struct GNUNET_HashCode *key,
2073 struct MeshPeer *p = value;
2074 struct GNUNET_TIME_Absolute *abs = cls;
2076 /* Don't count active peers */
2077 if (GNUNET_YES == peer_is_used (p))
2080 if (abs->abs_value_us < p->last_contact.abs_value_us)
2081 abs->abs_value_us = p->last_contact.abs_value_us;
2088 * Iterator over all the peers to remove the oldest entry.
2090 * @param cls Closure (unsued).
2091 * @param key ID of the peer.
2092 * @param value Peer_Info of the peer.
2095 peer_timeout (void *cls,
2096 const struct GNUNET_HashCode *key,
2099 struct MeshPeer *p = value;
2100 struct GNUNET_TIME_Absolute *abs = cls;
2102 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2103 GNUNET_NO == peer_is_used (p))
2113 * Delete oldest unused peer.
2116 peer_delete_oldest (void)
2118 struct GNUNET_TIME_Absolute abs;
2120 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2122 GNUNET_CONTAINER_multihashmap_iterate (peers,
2125 GNUNET_CONTAINER_multihashmap_iterate (peers,
2132 * Retrieve the MeshPeer stucture associated with the peer, create one
2133 * and insert it in the appropriate structures if the peer is not known yet.
2135 * @param peer Full identity of the peer.
2137 * @return Existing or newly created peer info.
2139 static struct MeshPeer *
2140 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2142 struct MeshPeer *peer;
2144 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2147 peer = GNUNET_new (struct MeshPeer);
2148 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2150 peer_delete_oldest ();
2152 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2153 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2154 peer->id = GNUNET_PEER_intern (peer_id);
2156 peer->last_contact = GNUNET_TIME_absolute_get();
2163 * Retrieve the MeshPeer stucture associated with the peer, create one
2164 * and insert it in the appropriate structures if the peer is not known yet.
2166 * @param peer Short identity of the peer.
2168 * @return Existing or newly created peer info.
2170 static struct MeshPeer *
2171 peer_get_short (const GNUNET_PEER_Id peer)
2173 return peer_get (GNUNET_PEER_resolve2 (peer));
2178 * Get a cost of a path for a peer considering existing tunnel connections.
2180 * @param peer Peer towards which the path is considered.
2181 * @param path Candidate path.
2183 * @return Cost of the path (path length + number of overlapping nodes)
2186 peer_get_path_cost (const struct MeshPeer *peer,
2187 const struct MeshPeerPath *path)
2189 struct MeshConnection *c;
2190 unsigned int overlap;
2198 GNUNET_assert (NULL != peer->tunnel);
2200 for (i = 0; i < path->length; i++)
2202 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2204 for (j = 0; j < c->path->length; j++)
2206 if (path->peers[i] == c->path->peers[j])
2214 return (path->length + overlap) * (path->score * -1);
2219 * Choose the best path towards a peer considering the tunnel properties.
2221 * @param peer The destination peer.
2223 * @return Best current known path towards the peer, if any.
2225 static struct MeshPeerPath *
2226 peer_get_best_path (const struct MeshPeer *peer)
2228 struct MeshPeerPath *best_p;
2229 struct MeshPeerPath *p;
2230 struct MeshConnection *c;
2231 unsigned int best_cost;
2234 best_cost = UINT_MAX;
2236 for (p = peer->path_head; NULL != p; p = p->next)
2238 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2242 continue; /* If path is in use in a connection, skip it. */
2244 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2254 queue_is_sendable (struct MeshPeerQueue *q)
2256 struct MeshFlowControl *fc;
2258 /* Is PID-independent? */
2261 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2262 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2266 /* Is PID allowed? */
2267 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2268 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2276 * Get first sendable message.
2278 * @param peer The destination peer.
2280 * @return Best current known path towards the peer, if any.
2282 static struct MeshPeerQueue *
2283 peer_get_first_message (const struct MeshPeer *peer)
2285 struct MeshPeerQueue *q;
2287 for (q = peer->queue_head; NULL != q; q = q->next)
2289 if (queue_is_sendable (q))
2298 * Try to establish a new connection to this peer in the given tunnel.
2299 * If the peer doesn't have any path to it yet, try to get one.
2300 * If the peer already has some path, send a CREATE CONNECTION towards it.
2302 * @param peer PeerInfo of the peer.
2305 peer_connect (struct MeshPeer *peer)
2307 struct MeshTunnel2 *t;
2308 struct MeshPeerPath *p;
2309 struct MeshConnection *c;
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "peer_connect towards %s\n",
2315 if (NULL != peer->path_head)
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2318 p = peer_get_best_path (peer);
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2322 c = tunnel_use_path (t, p);
2323 send_connection_create (c);
2326 else if (NULL == peer->dhtget)
2328 const struct GNUNET_PeerIdentity *id;
2330 id = GNUNET_PEER_resolve2 (peer->id);
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 " Starting DHT GET for peer %s\n", peer2s (peer));
2333 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2334 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2335 &id->hashPubKey, /* key to search */
2336 dht_replication_level, /* replication level */
2337 GNUNET_DHT_RO_RECORD_ROUTE |
2338 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2340 0, /* xquery bits */
2341 &dht_get_id_handler, peer);
2342 if (MESH_TUNNEL_NEW == t->state)
2343 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "There is no path but the DHT GET is already started.\n");
2354 * Get the first transmittable message for a connection.
2356 * @param c Connection.
2357 * @param fwd Is this FWD?
2359 * @return First transmittable message.
2361 static struct MeshPeerQueue *
2362 connection_get_first_message (struct MeshConnection *c, int fwd)
2364 struct MeshPeerQueue *q;
2367 p = connection_get_hop (c, fwd);
2369 for (q = p->queue_head; NULL != q; q = q->next)
2373 if (queue_is_sendable (q))
2382 * Is this peer the first one on the connection?
2384 * @param c Connection.
2385 * @param fwd Is this about fwd traffic?
2387 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2390 connection_is_origin (struct MeshConnection *c, int fwd)
2392 if (!fwd && c->own_pos == c->path->length - 1)
2394 if (fwd && c->own_pos == 0)
2401 * Is this peer the last one on the connection?
2403 * @param c Connection.
2404 * @param fwd Is this about fwd traffic?
2405 * Note that the ROOT is the terminal for BCK traffic!
2407 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2410 connection_is_terminal (struct MeshConnection *c, int fwd)
2412 if (fwd && c->own_pos == c->path->length - 1)
2414 if (!fwd && c->own_pos == 0)
2421 * @brief Re-initiate traffic on this connection if necessary.
2423 * Check if there is traffic queued towards this peer
2424 * and the core transmit handle is NULL (traffic was stalled).
2425 * If so, call core tmt rdy.
2427 * @param c Connection on which initiate traffic.
2428 * @param fwd Is this about fwd traffic?
2431 connection_unlock_queue (struct MeshConnection *c, int fwd)
2433 struct MeshPeer *peer;
2434 struct MeshPeerQueue *q;
2437 peer = connection_get_hop (c, fwd);
2439 if (NULL != peer->core_transmit)
2440 return; /* Already unlocked */
2442 q = connection_get_first_message (c, fwd);
2444 return; /* Nothing to transmit */
2447 peer->core_transmit =
2448 GNUNET_CORE_notify_transmit_ready (core_handle,
2451 GNUNET_TIME_UNIT_FOREVER_REL,
2452 GNUNET_PEER_resolve2 (peer->id),
2460 * Cancel all transmissions that belong to a certain connection.
2462 * @param c Connection which to cancel.
2463 * @param fwd Cancel fwd traffic?
2466 connection_cancel_queues (struct MeshConnection *c, int fwd)
2468 struct MeshPeerQueue *q;
2469 struct MeshPeerQueue *next;
2470 struct MeshFlowControl *fc;
2471 struct MeshPeer *peer;
2478 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2479 peer = connection_get_hop (c, fwd);
2481 for (q = peer->queue_head; NULL != q; q = next)
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "connection_cancel_queue %s\n",
2488 GNUNET_MESH_DEBUG_M2S (q->type));
2489 queue_destroy (q, GNUNET_YES);
2492 if (NULL == peer->queue_head)
2494 if (NULL != peer->core_transmit)
2496 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2497 peer->core_transmit = NULL;
2499 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2501 GNUNET_SCHEDULER_cancel (fc->poll_task);
2502 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2509 * Remove all paths that rely on a direct connection between p1 and p2
2510 * from the peer itself and notify all tunnels about it.
2512 * @param peer PeerInfo of affected peer.
2513 * @param p1 GNUNET_PEER_Id of one peer.
2514 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2517 * TODO: optimize (see below)
2520 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2523 struct MeshPeerPath *p;
2524 struct MeshPeerPath *next;
2525 struct MeshPeer *peer_d;
2527 unsigned int destroyed;
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2532 for (p = peer->path_head; NULL != p; p = next)
2535 for (i = 0; i < (p->length - 1); i++)
2537 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2538 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2540 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2551 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2553 peer_d = peer_get_short (d); // FIXME
2554 next = peer_get_best_path (peer_d);
2555 tunnel_use_path (peer->tunnel, next);
2556 peer_connect (peer_d);
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2563 * Add the path to the peer and update the path used to reach it in case this
2566 * @param peer_info Destination peer to add the path to.
2567 * @param path New path to add. Last peer must be the peer in arg 1.
2568 * Path will be either used of freed if already known.
2569 * @param trusted Do we trust that this path is real?
2572 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2575 struct MeshPeerPath *aux;
2579 if ((NULL == peer_info) || (NULL == path))
2582 path_destroy (path);
2585 if (path->peers[path->length - 1] != peer_info->id)
2588 path_destroy (path);
2591 if (2 >= path->length && GNUNET_NO == trusted)
2593 /* Only allow CORE to tell us about direct paths */
2594 path_destroy (path);
2597 for (l = 1; l < path->length; l++)
2599 if (path->peers[l] == myid)
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2602 for (l2 = 0; l2 < path->length - l; l2++)
2604 path->peers[l2] = path->peers[l + l2];
2609 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2614 path->length, peer2s (peer_info));
2616 l = path_get_length (path);
2619 path_destroy (path);
2623 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2624 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2626 l2 = path_get_length (aux);
2629 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2630 peer_info->path_tail, aux, path);
2635 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2637 path_destroy (path);
2642 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2649 * Add the path to the origin peer and update the path used to reach it in case
2650 * this is the shortest.
2651 * The path is given in peer_info -> destination, therefore we turn the path
2652 * upside down first.
2654 * @param peer_info Peer to add the path to, being the origin of the path.
2655 * @param path New path to add after being inversed.
2656 * Path will be either used or freed.
2657 * @param trusted Do we trust that this path is real?
2660 peer_add_path_to_origin (struct MeshPeer *peer_info,
2661 struct MeshPeerPath *path, int trusted)
2664 peer_add_path (peer_info, path, trusted);
2670 * Function called if a connection has been stalled for a while,
2671 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2673 * @param cls Closure (poll ctx).
2674 * @param tc TaskContext.
2677 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2679 struct MeshFlowControl *fc = cls;
2680 struct GNUNET_MESH_Poll msg;
2681 struct MeshConnection *c;
2683 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2684 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2692 peer2s (c->t->peer), c->id);
2694 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2695 msg.header.size = htons (sizeof (msg));
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2697 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2698 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2699 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2700 &connection_poll, fc);
2705 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2706 * to obtain a local peer -> destination path and interning the peer ids.
2708 * @return Newly allocated and created path
2710 static struct MeshPeerPath *
2711 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2712 unsigned int get_path_length,
2713 const struct GNUNET_PeerIdentity *put_path,
2714 unsigned int put_path_length)
2716 struct MeshPeerPath *p;
2722 GNUNET_PEER_change_rc (myid, 1);
2723 i = get_path_length;
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2725 for (i--; i >= 0; i--)
2727 id = GNUNET_PEER_intern (&get_path[i]);
2728 if (p->length > 0 && id == p->peers[p->length - 1])
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2731 GNUNET_PEER_change_rc (id, -1);
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2736 GNUNET_i2s (&get_path[i]));
2738 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2739 p->peers[p->length - 1] = id;
2742 i = put_path_length;
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2744 for (i--; i >= 0; i--)
2746 id = GNUNET_PEER_intern (&put_path[i]);
2749 /* PUT path went through us, so discard the path up until now and start
2750 * from here to get a much shorter (and loop-free) path.
2755 if (p->length > 0 && id == p->peers[p->length - 1])
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2758 GNUNET_PEER_change_rc (id, -1);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2763 GNUNET_i2s (&put_path[i]));
2765 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2766 p->peers[p->length - 1] = id;
2770 if (get_path_length > 0)
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2772 GNUNET_i2s (&get_path[0]));
2773 if (put_path_length > 0)
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2775 GNUNET_i2s (&put_path[0]));
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2778 for (i = 0; i < p->length; i++)
2780 struct GNUNET_PeerIdentity peer_id;
2782 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2784 GNUNET_i2s (&peer_id));
2792 * Adds a path to the peer_infos of all the peers in the path
2794 * @param p Path to process.
2795 * @param confirmed Whether we know if the path works or not.
2798 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2802 /* TODO: invert and add */
2803 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2804 for (i++; i < p->length; i++)
2806 struct MeshPeer *aux;
2807 struct MeshPeerPath *copy;
2809 aux = peer_get_short (p->peers[i]);
2810 copy = path_duplicate (p);
2811 copy->length = i + 1;
2812 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2818 * Search for a channel among the channels for a client
2820 * @param c the client whose channels to search in
2821 * @param chid the local id of the channel
2823 * @return channel handler, NULL if doesn't exist
2825 static struct MeshChannel *
2826 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2828 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2830 GNUNET_break_op (0);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2834 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2835 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2836 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2840 * Search for a tunnel by global ID using full PeerIdentities.
2842 * @param t Tunnel containing the channel.
2843 * @param chid Public channel number.
2845 * @return channel handler, NULL if doesn't exist
2847 static struct MeshChannel *
2848 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2850 struct MeshChannel *ch;
2855 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2857 if (ch->gid == chid)
2866 * Change the tunnel state.
2868 * @param t Tunnel whose state to change.
2869 * @param state New state.
2872 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Tunnel %s state was %s\n",
2879 GNUNET_MESH_DEBUG_TS2S (t->state));
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2881 "Tunnel %s state is now %s\n",
2883 GNUNET_MESH_DEBUG_TS2S (state));
2889 * Cache a message to be sent once tunnel is online.
2891 * @param t Tunnel to hold the message.
2892 * @param ch Channel the message is about.
2893 * @param msg Message itself (copy will be made).
2894 * @param fwd Is this fwd?
2897 tunnel_queue_data (struct MeshTunnel2 *t,
2898 struct MeshChannel *ch,
2899 struct GNUNET_MessageHeader *msg,
2902 struct MeshTunnelQueue *tq;
2903 uint16_t size = ntohs (msg->size);
2905 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2908 memcpy (&tq[1], msg, size);
2909 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2914 * Send all cached messages that we can, tunnel is online.
2916 * @param t Tunnel that holds the messages.
2917 * @param fwd Is this fwd?
2920 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2922 struct MeshTunnelQueue *tq;
2923 struct MeshTunnelQueue *next;
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 "tunnel_send_queued_data on tunnel %s\n",
2929 room = tunnel_get_buffer (t, fwd);
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2931 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2935 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2936 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2945 connection_change_state (struct MeshConnection* c,
2946 enum MeshConnectionState state)
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "Connection %s state was %s\n",
2950 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 "Connection %s state is now %s\n",
2953 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
2959 * Add a client to a channel, initializing all needed data structures.
2961 * @param ch Channel to which add the client.
2962 * @param c Client which to add to the channel.
2965 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
2967 struct MeshTunnel2 *t = ch->t;
2969 if (NULL != ch->dest)
2975 /* Assign local id as destination */
2976 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
2977 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2978 ch->lid_dest = t->next_local_chid++;
2979 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
2981 /* Store in client's hashmap */
2983 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2994 static struct MeshConnection *
2995 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
2997 struct MeshConnection *c;
2998 struct GNUNET_HashCode cid;
2999 struct MeshPeer *peer;
3000 unsigned int own_pos;
3002 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3004 c = connection_new (&cid);
3006 for (own_pos = 0; own_pos < p->length; own_pos++)
3008 if (p->peers[own_pos] == myid)
3011 if (own_pos > p->length - 1)
3014 connection_destroy (c);
3017 c->own_pos = own_pos;
3022 c->fwd_maintenance_task =
3023 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3024 &connection_fwd_keepalive, c);
3027 peer = connection_get_next_hop (c);
3028 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3030 peer = connection_get_prev_hop (c);
3031 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3032 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3038 * Notifies a tunnel that a connection has broken that affects at least
3039 * some of its peers. Sends a notification towards the root of the tree.
3040 * In case the peer is the owner of the tree, notifies the client that owns
3041 * the tunnel and tries to reconnect.
3043 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3045 * @param t Tunnel affected.
3046 * @param p1 Peer that got disconnected from p2.
3047 * @param p2 Peer that got disconnected from p1.
3049 * @return Short ID of the peer disconnected (either p1 or p2).
3050 * 0 if the tunnel remained unaffected.
3052 static GNUNET_PEER_Id
3053 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3054 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3056 // if (myid != p1 && myid != p2) FIXME
3061 // if (tree_get_predecessor (t->tree) != 0)
3063 // /* We are the peer still connected, notify owner of the disconnection. */
3064 // struct GNUNET_MESH_PathBroken msg;
3065 // struct GNUNET_PeerIdentity neighbor;
3067 // msg.header.size = htons (sizeof (msg));
3068 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3069 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3070 // msg.tid = htonl (t->id.tid);
3071 // msg.peer1 = my_full_id;
3072 // GNUNET_PEER_resolve (pid, &msg.peer2);
3073 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3074 // send_prebuilt_message (&msg.header, &neighbor, t);
3081 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3083 * @param ch Channel this is about.
3084 * @param fwd Is for FWD traffic? (ACK dest->owner)
3087 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3089 struct GNUNET_MESH_DataACK msg;
3090 struct MeshChannelReliability *rel;
3091 struct MeshReliableMessage *copy;
3097 if (GNUNET_NO == ch->reliable)
3102 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3103 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3105 "send_data_ack for %u\n",
3108 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3109 msg.header.type = htons (type);
3110 msg.header.size = htons (sizeof (msg));
3111 msg.chid = htonl (ch->gid);
3112 msg.mid = htonl (*mid - 1);
3114 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3116 if (copy->type != type)
3118 delta = copy->mid - *mid;
3121 mask = 0x1LL << delta;
3122 msg.futures |= mask;
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3125 copy->mid, delta, mask, msg.futures);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3129 send_prebuilt_message_channel (&msg.header, ch, fwd);
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3135 * Send an ACK informing the predecessor about the available buffer space.
3137 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3138 * the ACK itself goes "back" (dest->root).
3140 * @param c Connection on which to send the ACK.
3141 * @param fwd Is this FWD ACK? (Going dest->owner)
3144 connection_send_ack (struct MeshConnection *c, int fwd)
3146 struct MeshFlowControl *next_fc;
3147 struct MeshFlowControl *prev_fc;
3151 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3152 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3155 "connection send %s ack on %s\n",
3156 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3158 /* Check if we need to transmit the ACK */
3159 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3163 " last pid recv: %u, last ack sent: %u\n",
3164 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3168 /* Ok, ACK might be necessary, what PID to ACK? */
3169 delta = next_fc->queue_max - next_fc->queue_n;
3170 ack = prev_fc->last_pid_recv + delta;
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 " last pid %u, last ack %u, qmax %u, q %u\n",
3174 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3175 next_fc->queue_max, next_fc->queue_n);
3176 if (ack == prev_fc->last_ack_sent)
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3182 prev_fc->last_ack_sent = ack;
3183 send_ack (c, ack, fwd);
3188 * Modify the mesh message TID from global to local and send to client.
3190 * @param ch Channel on which to send the message.
3191 * @param msg Message to modify and send.
3192 * @param c Client to send to.
3193 * @param tid Tunnel ID to use (c can be both owner and client).
3196 channel_send_client_to_tid (struct MeshChannel *ch,
3197 const struct GNUNET_MESH_Data *msg,
3198 struct MeshClient *c, MESH_ChannelNumber id)
3200 struct GNUNET_MESH_LocalData *copy;
3201 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3202 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3204 if (size < sizeof (struct GNUNET_MessageHeader))
3206 GNUNET_break_op (0);
3214 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3215 memcpy (©[1], &msg[1], size);
3216 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3217 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3218 copy->id = htonl (id);
3219 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3220 ©->header, GNUNET_NO);
3224 * Modify the data message ID from global to local and send to client.
3226 * @param ch Channel on which to send the message.
3227 * @param msg Message to modify and send.
3228 * @param fwd Forward?
3231 channel_send_client_data (struct MeshChannel *ch,
3232 const struct GNUNET_MESH_Data *msg,
3236 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3238 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3243 * Send a buffered message to the client, for in order delivery or
3244 * as result of client ACK.
3246 * @param ch Channel on which to empty the message buffer.
3247 * @param c Client to send to.
3248 * @param rel Reliability structure to corresponding peer.
3249 * If rel == bck_rel, this is FWD data.
3252 channel_send_client_buffered_data (struct MeshChannel *ch,
3253 struct MeshClient *c,
3254 struct MeshChannelReliability *rel)
3256 struct MeshReliableMessage *copy;
3259 if (GNUNET_NO == rel->client_ready)
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3265 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3268 copy = rel->head_recv;
3269 /* We never buffer channel management messages */
3272 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3274 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 " have %u! now expecting %u\n",
3278 copy->mid, *mid + 1);
3279 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3282 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 " reliable && don't have %u, next is %u\n",
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3299 * We have received a message out of order, or the client is not ready.
3300 * Buffer it until we receive an ACK from the client or the missing
3301 * message from the channel.
3303 * @param msg Message to buffer (MUST be of type MESH_DATA).
3304 * @param rel Reliability data to the corresponding direction.
3307 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3308 struct MeshChannelReliability *rel)
3310 struct MeshReliableMessage *copy;
3311 struct MeshReliableMessage *prev;
3315 size = ntohs (msg->header.size);
3316 mid = ntohl (msg->mid);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3320 copy = GNUNET_malloc (sizeof (*copy) + size);
3323 memcpy (©[1], msg, size);
3327 // FIXME do something better than O(n), although n < 64...
3328 // FIXME start from the end (most messages are the latest ones)
3329 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3332 if (GMC_is_pid_bigger (prev->mid, mid))
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3335 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3341 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3347 * Destroy a reliable message after it has been acknowledged, either by
3348 * direct mid ACK or bitfield. Updates the appropriate data structures and
3349 * timers and frees all memory.
3351 * @param copy Message that is no longer needed: remote peer got it.
3354 rel_message_free (struct MeshReliableMessage *copy)
3356 struct MeshChannelReliability *rel;
3357 struct GNUNET_TIME_Relative time;
3360 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3361 rel->expected_delay.rel_value_us *= 7;
3362 rel->expected_delay.rel_value_us += time.rel_value_us;
3363 rel->expected_delay.rel_value_us /= 8;
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3368 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3370 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3372 rel->retry_timer = rel->expected_delay;
3373 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3379 * Destroy all reliable messages queued for a channel,
3380 * during a channel destruction.
3381 * Frees the reliability structure itself.
3383 * @param rel Reliability data for a channel.
3386 channel_rel_free_all (struct MeshChannelReliability *rel)
3388 struct MeshReliableMessage *copy;
3389 struct MeshReliableMessage *next;
3394 for (copy = rel->head_recv; NULL != copy; copy = next)
3397 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3400 for (copy = rel->head_sent; NULL != copy; copy = next)
3403 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3406 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3407 GNUNET_SCHEDULER_cancel (rel->retry_task);
3413 * Mark future messages as ACK'd.
3415 * @param rel Reliability data.
3416 * @param msg DataACK message with a bitfield of future ACK'd messages.
3419 channel_rel_free_sent (struct MeshChannelReliability *rel,
3420 const struct GNUNET_MESH_DataACK *msg)
3422 struct MeshReliableMessage *copy;
3423 struct MeshReliableMessage *next;
3430 bitfield = msg->futures;
3431 mid = ntohl (msg->mid);
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 "free_sent_reliable %u %llX\n",
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3436 " rel %p, head %p\n",
3437 rel, rel->head_sent);
3438 for (i = 0, copy = rel->head_sent;
3439 i < 64 && NULL != copy && 0 != bitfield;
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3443 " trying bit %u (mid %u)\n",
3446 if (0 == (bitfield & mask))
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3450 /* Bit was set, clear the bit from the bitfield */
3453 /* The i-th bit was set. Do we have that copy? */
3454 /* Skip copies with mid < target */
3455 target = mid + i + 1;
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3457 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3460 /* Did we run out of copies? (previously freed, it's ok) */
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3467 /* Did we overshoot the target? (previously freed, it's ok) */
3468 if (GMC_is_pid_bigger (copy->mid, target))
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3474 /* Now copy->mid == target, free it */
3476 rel_message_free (copy);
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3484 * We haven't received an ACK after a certain time: restransmit the message.
3486 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3487 * @param tc TaskContext.
3490 channel_retransmit_message (void *cls,
3491 const struct GNUNET_SCHEDULER_TaskContext *tc)
3493 struct MeshChannelReliability *rel = cls;
3494 struct MeshReliableMessage *copy;
3495 struct MeshPeerQueue *q;
3496 struct MeshChannel *ch;
3497 struct MeshConnection *c;
3498 struct GNUNET_MESH_Data *payload;
3499 struct MeshPeer *hop;
3502 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3507 copy = rel->head_sent;
3514 /* Search the message to be retransmitted in the outgoing queue.
3515 * Check only the queue for the connection that is going to be used,
3516 * if the message is stuck in some other connection's queue we shouldn't
3518 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3519 * the old connection could be temporary stalled or the queue happened to
3520 * be long at time of insertion.
3521 * - not sending the new one could cause terrible delays the old connection
3524 payload = (struct GNUNET_MESH_Data *) ©[1];
3525 fwd = (rel == ch->fwd_rel);
3526 c = tunnel_get_connection (ch->t, fwd);
3527 hop = connection_get_hop (c, fwd);
3528 for (q = hop->queue_head; NULL != q; q = q->next)
3530 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3532 struct GNUNET_MESH_Data *queued_data = q->cls;
3534 if (queued_data->mid == payload->mid)
3539 /* Message not found in the queue that we are going to use. */
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3544 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3545 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3552 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3553 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3554 &channel_retransmit_message,
3560 * Send an ACK to a client if needed.
3562 * @param ch Channel this is regarding.
3563 * @param fwd Is this about fwd traffic? (ACk goes the opposite direction).
3566 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3568 struct MeshClient *c;
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 "Channel send client %s ack on %s:%X\n",
3573 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3575 /* Check for buffer space */
3576 if (0 >= tunnel_get_buffer (ch->t, fwd))
3579 /* Client to receive the ACK (fwd indicates traffic to be ACK'd) */
3580 c = fwd ? ch->root : ch->dest;
3582 blocked = fwd ? ch->blocked_fwd : ch->blocked_bck;
3583 if (GNUNET_YES == blocked)
3584 send_local_ack (ch, c, fwd);
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client not blocked\n");
3591 * Send ACK on one or more connections due to buffer space to the client.
3593 * @param ch Channel which has some free buffer space.
3594 * @param buffer Buffer space.
3595 * @param fwd Is this in the FWD direction?
3598 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3600 struct MeshTunnel2 *t = ch->t;
3601 struct MeshConnection *c;
3602 struct MeshFlowControl *fc;
3607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3608 "Channel send connection %s ack on %s:%X\n",
3609 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3611 /* Count connections, how many messages are already allowed */
3612 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3614 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3615 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3620 allowed += fc->last_ack_sent - fc->last_pid_recv;
3624 /* Make sure there is no overflow */
3625 if (allowed > buffer)
3631 /* Authorize connections to send more data */
3632 to_allow = buffer - allowed;
3633 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3635 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3636 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3640 send_ack (c, fc->last_ack_sent + 1, fwd);
3644 GNUNET_break (to_allow == 0);
3649 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3651 * @param ch Channel to mark as ready.
3652 * @param fwd Was the CREATE message sent fwd?
3655 channel_confirm (struct MeshChannel *ch, int fwd)
3657 struct MeshChannelReliability *rel;
3658 struct MeshReliableMessage *copy;
3659 struct MeshReliableMessage *next;
3661 ch->state = MESH_CHANNEL_READY;
3663 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3664 for (copy = rel->head_sent; NULL != copy; copy = next)
3666 struct GNUNET_MessageHeader *msg;
3669 msg = (struct GNUNET_MessageHeader *) ©[1];
3670 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3672 rel_message_free (copy);
3680 * Save a copy to retransmit in case it gets lost.
3682 * Initializes all needed callbacks and timers.
3684 * @param ch Channel this message goes on.
3685 * @param msg Message to copy.
3686 * @param fwd Is this fwd traffic?
3689 channel_save_copy (struct MeshChannel *ch,
3690 const struct GNUNET_MessageHeader *msg,
3693 struct MeshChannelReliability *rel;
3694 struct MeshReliableMessage *copy;
3699 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3700 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3701 type = ntohs (msg->type);
3702 size = ntohs (msg->size);
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3705 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3707 copy->timestamp = GNUNET_TIME_absolute_get ();
3710 memcpy (©[1], msg, size);
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3713 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3714 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3717 GNUNET_TIME_relative_multiply (rel->expected_delay,
3718 MESH_RETRANSMIT_MARGIN);
3720 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3721 &channel_retransmit_message,
3728 * Send keepalive packets for a connection.
3730 * @param c Connection to keep alive..
3731 * @param fwd Is this a FWD keepalive? (owner -> dest).
3734 connection_keepalive (struct MeshConnection *c, int fwd)
3736 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3737 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3741 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3742 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3745 "sending %s keepalive for connection %s[%d]\n",
3746 fwd ? "FWD" : "BCK",
3747 peer2s (c->t->peer),
3750 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3751 msg->header.size = htons (size);
3752 msg->header.type = htons (type);
3755 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3760 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3762 * @param c Connection for which to send the message.
3763 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3766 connection_recreate (struct MeshConnection *c, int fwd)
3768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3770 send_connection_create (c);
3772 send_connection_ack (c, GNUNET_NO);
3777 * Generic connection timer management.
3778 * Depending on the role of the peer in the connection will send the
3779 * appropriate message (build or keepalive)
3781 * @param c Conncetion to maintain.
3782 * @param fwd Is FWD?
3785 connection_maintain (struct MeshConnection *c, int fwd)
3787 if (MESH_TUNNEL_SEARCHING == c->t->state)
3789 /* TODO DHT GET with RO_BART */
3794 case MESH_CONNECTION_NEW:
3796 case MESH_CONNECTION_SENT:
3797 connection_recreate (c, fwd);
3799 case MESH_CONNECTION_READY:
3800 connection_keepalive (c, fwd);
3809 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3811 struct MeshConnection *c = cls;
3813 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3814 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3817 connection_maintain (c, GNUNET_YES);
3818 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3819 &connection_fwd_keepalive,
3825 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3827 struct MeshConnection *c = cls;
3829 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3830 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3833 connection_maintain (c, GNUNET_NO);
3834 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3835 &connection_bck_keepalive,
3841 * Send a message to all peers in this connection that the connection
3842 * is no longer valid.
3844 * If some peer should not receive the message, it should be zero'ed out
3845 * before calling this function.
3847 * @param c The connection whose peers to notify.
3850 connection_send_destroy (struct MeshConnection *c)
3852 struct GNUNET_MESH_ConnectionDestroy msg;
3854 msg.header.size = htons (sizeof (msg));
3855 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3858 " sending connection destroy for connection %s[%X]\n",
3859 peer2s (c->t->peer),
3862 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3863 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3864 c->destroy = GNUNET_YES;
3869 * Confirm we got a channel create.
3871 * @param ch The channel to confirm.
3872 * @param fwd Should we send the ACK fwd?
3875 channel_send_ack (struct MeshChannel *ch, int fwd)
3877 struct GNUNET_MESH_ChannelManage msg;
3879 msg.header.size = htons (sizeof (msg));
3880 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 " sending channel ack for channel %s:%X\n",
3883 peer2s (ch->t->peer),
3886 msg.chid = htonl (ch->gid);
3887 send_prebuilt_message_channel (&msg.header, ch, fwd);
3892 * Send a message to all clients (local and remote) of this channel
3893 * notifying that the channel is no longer valid.
3895 * If some peer or client should not receive the message,
3896 * should be zero'ed out before calling this function.
3898 * @param ch The channel whose clients to notify.
3901 channel_send_destroy (struct MeshChannel *ch)
3903 struct GNUNET_MESH_ChannelManage msg;
3905 msg.header.size = htons (sizeof (msg));
3906 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3908 " sending channel destroy for channel %s:%X\n",
3909 peer2s (ch->t->peer),
3912 if (NULL != ch->root)
3914 msg.chid = htonl (ch->lid_root);
3915 send_local_channel_destroy (ch, GNUNET_NO);
3919 msg.chid = htonl (ch->gid);
3920 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3923 if (NULL != ch->dest)
3925 msg.chid = htonl (ch->lid_dest);
3926 send_local_channel_destroy (ch, GNUNET_YES);
3930 msg.chid = htonl (ch->gid);
3931 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3939 static struct MeshTunnel2 *
3942 struct MeshTunnel2 *t;
3944 t = GNUNET_new (struct MeshTunnel2);
3946 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3948 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3949 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3951 // GNUNET_break (0);
3952 // tunnel_destroy (t);
3956 // char salt[] = "salt";
3957 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3958 // salt, sizeof (salt),
3959 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3960 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3961 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3963 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3964 // salt, sizeof (salt),
3965 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3966 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
3967 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
3975 * Add a connection to a tunnel.
3978 * @param c Connection.
3981 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
3984 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
3989 * Initialize a Flow Control structure to the initial state.
3991 * @param fc Flow Control structure to initialize.
3994 fc_init (struct MeshFlowControl *fc)
3997 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3998 fc->last_pid_recv = (uint32_t) -1;
3999 fc->last_ack_sent = (uint32_t) 0;
4000 fc->last_ack_recv = (uint32_t) 0;
4001 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4002 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4004 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4008 static struct MeshConnection *
4009 connection_new (const struct GNUNET_HashCode *cid)
4011 struct MeshConnection *c;
4013 c = GNUNET_new (struct MeshConnection);
4015 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4016 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4017 fc_init (&c->fwd_fc);
4018 fc_init (&c->bck_fc);
4027 * Find a connection.
4029 * @param cid Connection ID.
4031 static struct MeshConnection *
4032 connection_get (const struct GNUNET_HashCode *cid)
4034 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4039 connection_destroy (struct MeshConnection *c)
4041 struct MeshPeer *peer;
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4047 peer2s (c->t->peer),
4050 /* Cancel all traffic */
4051 connection_cancel_queues (c, GNUNET_YES);
4052 connection_cancel_queues (c, GNUNET_NO);
4054 /* Cancel maintainance task (keepalive/timeout) */
4055 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4056 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4057 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4058 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4060 /* Deregister from neighbors */
4061 peer = connection_get_next_hop (c);
4063 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4064 peer = connection_get_prev_hop (c);
4066 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4069 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4070 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4076 tunnel_destroy (struct MeshTunnel2 *t)
4078 struct MeshConnection *c;
4079 struct MeshConnection *next;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4087 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4088 // GNUNET_break (0);
4090 for (c = t->connection_head; NULL != c; c = next)
4093 connection_destroy (c);
4096 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4103 * Tunnel is empty: destroy it.
4105 * Notifies all connections about the destruction.
4107 * @param t Tunnel to destroy.
4110 tunnel_destroy_empty (struct MeshTunnel2 *t)
4112 struct MeshConnection *c;
4114 for (c = t->connection_head; NULL != c; c = c->next)
4116 if (GNUNET_NO == c->destroy)
4117 connection_send_destroy (c);
4120 if (0 == t->pending_messages)
4123 t->destroy = GNUNET_YES;
4128 * Destroy tunnel if empty (no more channels).
4130 * @param t Tunnel to destroy if empty.
4133 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4135 if (NULL != t->channel_head)
4138 tunnel_destroy_empty (t);
4143 * Destroy a channel and free all resources.
4145 * @param ch Channel to destroy.
4148 channel_destroy (struct MeshChannel *ch)
4150 struct MeshClient *c;
4158 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4169 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4176 channel_rel_free_all (ch->fwd_rel);
4177 channel_rel_free_all (ch->bck_rel);
4179 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4180 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4186 * Create a new channel.
4188 * @param t Tunnel this channel is in.
4189 * @param owner Client that owns the channel, NULL for foreign channels.
4190 * @param lid_root Local ID for root client.
4192 * @return A new initialized channel. NULL on error.
4194 static struct MeshChannel *
4195 channel_new (struct MeshTunnel2 *t,
4196 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4198 struct MeshChannel *ch;
4200 ch = GNUNET_new (struct MeshChannel);
4202 ch->lid_root = lid_root;
4204 ch->blocked_fwd = GNUNET_YES;
4205 ch->blocked_bck = GNUNET_YES;
4207 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4209 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4213 while (NULL != channel_get (t, t->next_chid))
4214 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4215 ch->gid = t->next_chid;
4216 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4219 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4223 channel_destroy (ch);
4224 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4234 * Set options in a channel, extracted from a bit flag field
4236 * @param ch Channel to set options to.
4237 * @param options Bit array in host byte order.
4240 channel_set_options (struct MeshChannel *ch, uint32_t options)
4242 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4243 GNUNET_YES : GNUNET_NO;
4244 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4245 GNUNET_YES : GNUNET_NO;
4250 * Iterator for deleting each channel whose client endpoint disconnected.
4252 * @param cls Closure (client that has disconnected).
4253 * @param key The local channel id (used to access the hashmap).
4254 * @param value The value stored at the key (channel to destroy).
4256 * @return GNUNET_OK, keep iterating.
4259 channel_destroy_iterator (void *cls,
4263 struct MeshChannel *ch = value;
4264 struct MeshClient *c = cls;
4265 struct MeshTunnel2 *t;
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4268 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4269 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4283 channel_send_destroy (ch);
4284 channel_destroy (ch);
4285 tunnel_destroy_if_empty (t);
4292 * Remove client's ports from the global hashmap on disconnect.
4294 * @param cls Closure (unused).
4296 * @param value Client structure.
4298 * @return GNUNET_OK, keep iterating.
4301 client_release_ports (void *cls,
4307 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4308 if (GNUNET_YES != res)
4311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4312 "Port %u by client %p was not registered.\n",
4320 * Timeout function due to lack of keepalive/traffic from the owner.
4321 * Destroys connection if called.
4323 * @param cls Closure (connection to destroy).
4324 * @param tc TaskContext.
4327 connection_fwd_timeout (void *cls,
4328 const struct GNUNET_SCHEDULER_TaskContext *tc)
4330 struct MeshConnection *c = cls;
4332 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4333 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4336 "Connection %s[%X] FWD timed out. Destroying.\n",
4337 peer2s (c->t->peer),
4340 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4343 connection_destroy (c);
4348 * Timeout function due to lack of keepalive/traffic from the destination.
4349 * Destroys connection if called.
4351 * @param cls Closure (connection to destroy).
4352 * @param tc TaskContext
4355 connection_bck_timeout (void *cls,
4356 const struct GNUNET_SCHEDULER_TaskContext *tc)
4358 struct MeshConnection *c = cls;
4360 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4361 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4365 "Connection %s[%X] FWD timed out. Destroying.\n",
4366 peer2s (c->t->peer),
4369 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4372 connection_destroy (c);
4377 * Resets the connection timeout task, some other message has done the
4379 * - For the first peer on the direction this means to send
4380 * a keepalive or a path confirmation message (either create or ACK).
4381 * - For all other peers, this means to destroy the connection,
4382 * due to lack of activity.
4383 * Starts the tiemout if no timeout was running (connection just created).
4385 * @param c Connection whose timeout to reset.
4386 * @param fwd Is this forward?
4388 * TODO use heap to improve efficiency of scheduler.
4391 connection_reset_timeout (struct MeshConnection *c, int fwd)
4393 GNUNET_SCHEDULER_TaskIdentifier *ti;
4394 GNUNET_SCHEDULER_Task f;
4396 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4398 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4399 GNUNET_SCHEDULER_cancel (*ti);
4401 if (connection_is_origin (c, fwd)) /* Endpoint */
4403 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4404 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4408 struct GNUNET_TIME_Relative delay;
4410 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4411 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4412 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4418 * Iterator to notify all connections of a broken link. Mark connections
4419 * to destroy after all traffic has been sent.
4421 * @param cls Closure (peer disconnected).
4422 * @param key Current key code (tid).
4423 * @param value Value in the hash map (connection).
4425 * @return GNUNET_YES if we should continue to iterate,
4429 connection_broken (void *cls,
4430 const struct GNUNET_HashCode *key,
4433 struct MeshPeer *peer = cls;
4434 struct MeshConnection *c = value;
4435 struct GNUNET_MESH_ConnectionBroken msg;
4438 fwd = peer == connection_get_prev_hop (c);
4439 connection_cancel_queues (c, !fwd);
4441 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4442 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4444 msg.peer1 = my_full_id;
4445 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4446 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4447 c->destroy = GNUNET_YES;
4452 /******************************************************************************/
4453 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4454 /******************************************************************************/
4457 * Free a transmission that was already queued with all resources
4458 * associated to the request.
4460 * @param queue Queue handler to cancel.
4461 * @param clear_cls Is it necessary to free associated cls?
4464 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4466 struct MeshPeer *peer;
4467 struct MeshFlowControl *fc;
4472 GNUNET_assert (NULL != queue->c);
4473 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4475 if (GNUNET_YES == clear_cls)
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4478 GNUNET_MESH_DEBUG_M2S (queue->type));
4479 switch (queue->type)
4481 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4482 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4483 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4484 GNUNET_break (GNUNET_YES == queue->c->destroy);
4486 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4487 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4488 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4489 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4490 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4491 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4493 GNUNET_free_non_null (queue->cls);
4498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4499 GNUNET_MESH_DEBUG_M2S (queue->type));
4503 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4507 if (NULL != queue->c)
4509 queue->c->pending_messages--;
4510 if (NULL != queue->c->t)
4512 queue->c->t->pending_messages--;
4516 GNUNET_free (queue);
4521 queue_send (void *cls, size_t size, void *buf)
4523 struct MeshPeer *peer = cls;
4524 struct MeshFlowControl *fc;
4525 struct MeshConnection *c;
4526 struct GNUNET_MessageHeader *msg;
4527 struct MeshPeerQueue *queue;
4528 struct MeshTunnel2 *t;
4529 const struct GNUNET_PeerIdentity *dst_id;
4535 peer->core_transmit = NULL;
4536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4538 if (NULL == buf || 0 == size)
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4545 queue = peer_get_first_message (peer);
4548 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4553 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4556 dst_id = GNUNET_PEER_resolve2 (peer->id);
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4558 /* Check if buffer size is enough for the message */
4559 if (queue->size > size)
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4562 peer->core_transmit =
4563 GNUNET_CORE_notify_transmit_ready (core_handle,
4566 GNUNET_TIME_UNIT_FOREVER_REL,
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4575 t = (NULL != c) ? c->t : NULL;
4579 switch (queue->type)
4581 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4582 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4583 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4584 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4585 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4586 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 GNUNET_MESH_DEBUG_M2S (queue->type));
4590 data_size = send_core_data_raw (queue->cls, size, buf);
4591 msg = (struct GNUNET_MessageHeader *) buf;
4592 type = ntohs (msg->type);
4594 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4596 if (connection_is_origin (c, GNUNET_YES))
4597 data_size = send_core_connection_create (queue->c, size, buf);
4599 data_size = send_core_data_raw (queue->cls, size, buf);
4601 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4603 if (connection_is_origin (c, GNUNET_NO))
4604 data_size = send_core_connection_ack (queue->c, size, buf);
4606 data_size = send_core_data_raw (queue->cls, size, buf);
4608 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4609 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4610 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4611 /* This should be encapsulted */
4616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4621 if (0 < drop_percent &&
4622 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4625 "Dropping message of type %s\n",
4626 GNUNET_MESH_DEBUG_M2S (queue->type));
4630 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4633 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4634 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4635 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4636 fc->last_pid_sent = pid;
4637 if (NULL != queue->ch)
4638 channel_send_client_ack (queue->ch, fwd);
4640 connection_send_ack (c, fwd);
4646 /* Free queue, but cls was freed by send_core_* */
4647 queue_destroy (queue, GNUNET_NO);
4649 /* If more data in queue, send next */
4650 queue = peer_get_first_message (peer);
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4654 if (NULL == peer->core_transmit) {
4655 peer->core_transmit =
4656 GNUNET_CORE_notify_transmit_ready(core_handle,
4659 GNUNET_TIME_UNIT_FOREVER_REL,
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4668 "* tmt rdy called somewhere else\n");
4670 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4672 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4674 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4679 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4681 GNUNET_SCHEDULER_cancel (fc->poll_task);
4682 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4687 c->pending_messages--;
4688 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4691 connection_destroy (c);
4697 t->pending_messages--;
4698 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4710 queue_add (void *cls, uint16_t type, size_t size,
4711 struct MeshConnection *c,
4712 struct MeshChannel *ch,
4715 struct MeshPeerQueue *queue;
4716 struct MeshFlowControl *fc;
4717 struct MeshPeer *peer;
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4721 "queue add %s %s (%u) on c %p, ch %p\n",
4722 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4723 GNUNET_assert (NULL != c);
4725 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4726 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4736 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4737 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4739 fc->queue_n--; /* We are going to do queue_n++ later, compensate*/
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\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 identity the public identity of this peer
6655 core_init (void *cls,
6656 const struct GNUNET_PeerIdentity *identity)
6658 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6662 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6667 GNUNET_i2s (identity));
6668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6670 GNUNET_i2s (&my_full_id));
6671 GNUNET_CORE_disconnect (core_handle);
6672 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6673 NULL, /* Closure passed to MESH functions */
6674 &core_init, /* Call core_init once connected */
6675 &core_connect, /* Handle connects */
6676 &core_disconnect, /* remove peers on disconnects */
6677 NULL, /* Don't notify about all incoming messages */
6678 GNUNET_NO, /* For header only in notification */
6679 NULL, /* Don't notify about all outbound messages */
6680 GNUNET_NO, /* For header-only out notification */
6681 core_handlers); /* Register these handlers */
6690 /******************************************************************************/
6691 /************************ MAIN FUNCTIONS ****************************/
6692 /******************************************************************************/
6695 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6697 * @param cls closure
6698 * @param key current key code
6699 * @param value value in the hash map
6700 * @return GNUNET_YES if we should continue to iterate,
6704 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6706 struct MeshPeer *p = value;
6707 struct MeshTunnel2 *t = p->tunnel;
6716 * Task run during shutdown.
6722 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6726 if (core_handle != NULL)
6728 GNUNET_CORE_disconnect (core_handle);
6731 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6732 if (dht_handle != NULL)
6734 GNUNET_DHT_disconnect (dht_handle);
6739 GNUNET_SERVER_notification_context_destroy (nc);
6742 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6744 GNUNET_SCHEDULER_cancel (announce_id_task);
6745 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6752 * Process mesh requests.
6754 * @param cls closure
6755 * @param server the initialized server
6756 * @param c configuration to use
6759 run (void *cls, struct GNUNET_SERVER_Handle *server,
6760 const struct GNUNET_CONFIGURATION_Handle *c)
6763 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6766 server_handle = server;
6767 GNUNET_SERVER_suspend (server_handle);
6770 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6775 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6776 "mesh", "peer/privatekey");
6777 GNUNET_SCHEDULER_shutdown ();
6782 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6783 &refresh_connection_time))
6785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6787 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6788 "mesh", "refresh path time");
6789 GNUNET_SCHEDULER_shutdown ();
6794 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6799 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6800 "mesh", "id announce time");
6801 GNUNET_SCHEDULER_shutdown ();
6806 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6811 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6812 "mesh", "connect timeout");
6813 GNUNET_SCHEDULER_shutdown ();
6818 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6823 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6824 "mesh", "max msgs queue");
6825 GNUNET_SCHEDULER_shutdown ();
6830 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6835 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6836 "mesh", "max tunnels");
6837 GNUNET_SCHEDULER_shutdown ();
6842 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6847 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6848 "mesh", "default ttl", 64);
6853 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6856 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6857 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6858 "mesh", "max peers", 1000);
6863 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6871 "Mesh is running with drop mode enabled. "
6872 "This is NOT a good idea! "
6873 "Remove the DROP_PERCENT option from your configuration.\n");
6877 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6878 &dht_replication_level))
6880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6882 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6883 "mesh", "dht replication level", 3);
6884 dht_replication_level = 3;
6887 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6888 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6889 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6891 dht_handle = GNUNET_DHT_connect (c, 64);
6892 if (NULL == dht_handle)
6896 stats = GNUNET_STATISTICS_create ("mesh", c);
6898 /* Scheduled the task to clean up when shutdown is called */
6899 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6901 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6902 GNUNET_free (keyfile);
6903 GNUNET_assert (NULL != pk);
6904 my_private_key = pk;
6905 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6906 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6907 &my_full_id.hashPubKey);
6908 myid = GNUNET_PEER_intern (&my_full_id);
6909 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6910 "Mesh for peer [%s] starting\n",
6911 GNUNET_i2s(&my_full_id));
6913 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6914 NULL, /* Closure passed to MESH functions */
6915 &core_init, /* Call core_init once connected */
6916 &core_connect, /* Handle connects */
6917 &core_disconnect, /* remove peers on disconnects */
6918 NULL, /* Don't notify about all incoming messages */
6919 GNUNET_NO, /* For header only in notification */
6920 NULL, /* Don't notify about all outbound messages */
6921 GNUNET_NO, /* For header-only out notification */
6922 core_handlers); /* Register these handlers */
6923 if (NULL == core_handle)
6926 GNUNET_SCHEDULER_shutdown ();
6929 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6935 * The main function for the mesh service.
6937 * @param argc number of arguments from the command line
6938 * @param argv command line arguments
6939 * @return 0 ok, 1 on error
6942 main (int argc, char *const *argv)
6947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6948 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6950 ret = (GNUNET_OK == r) ? 0 : 1;
6951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");