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;
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnl_get_connection %s\n",
1573 lowest_q = UINT_MAX;
1574 for (c = t->connection_head; NULL != c; c = c->next)
1576 if (MESH_CONNECTION_READY == c->state)
1578 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1584 if (fc->queue_n < lowest_q)
1587 lowest_q = fc->queue_n;
1596 * Get the total buffer space for a tunnel.
1599 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
1601 struct MeshConnection *c;
1602 struct MeshFlowControl *fc;
1603 unsigned int buffer;
1605 for (buffer = 0, c = t->connection_head; NULL != c; c = c->next)
1607 if (c->state != MESH_CONNECTION_READY)
1610 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1611 buffer += fc->last_ack_recv - fc->last_pid_sent;
1619 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1620 * Encrypt data with the tunnel key.
1622 * @param t Tunnel whose key to use.
1623 * @param dst Destination for the encrypted data.
1624 * @param src Source of the plaintext.
1625 * @param size Size of the plaintext.
1626 * @param iv Initialization Vector to use.
1627 * @param fwd Is this a fwd message?
1630 tunnel_encrypt (struct MeshTunnel2 *t,
1631 void *dst, const void *src,
1632 size_t size, uint64_t iv, int fwd)
1634 memcpy (dst, src, size);
1639 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1640 * Decrypt data with the tunnel key.
1642 * @param t Tunnel whose key to use.
1643 * @param dst Destination for the plaintext.
1644 * @param src Source of the encrypted data.
1645 * @param size Size of the encrypted data.
1646 * @param iv Initialization Vector to use.
1647 * @param fwd Is this a fwd message?
1650 tunnel_decrypt (struct MeshTunnel2 *t,
1651 void *dst, const void *src,
1652 size_t size, uint64_t iv, int fwd)
1654 memcpy (dst, src, size);
1659 * Sends an already built message on a connection, properly registering
1660 * all used resources.
1662 * @param message Message to send. Function makes a copy of it.
1663 * If message is not hop-by-hop, decrements TTL of copy.
1664 * @param c Connection on which this message is transmitted.
1665 * @param ch Channel on which this message is transmitted, or NULL.
1666 * @param fwd Is this a fwd message?
1669 send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
1670 struct MeshConnection *c,
1671 struct MeshChannel *ch,
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Connection %s\n",
1679 GNUNET_h2s (&c->id));
1681 size = ntohs (message->size);
1682 data = GNUNET_malloc (size);
1683 memcpy (data, message, size);
1684 type = ntohs (message->type);
1688 struct GNUNET_MESH_Encrypted *emsg;
1689 struct GNUNET_MESH_ACK *amsg;
1690 struct GNUNET_MESH_Poll *pmsg;
1693 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1694 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1695 emsg = (struct GNUNET_MESH_Encrypted *) data;
1696 ttl = ntohl (emsg->ttl);
1699 GNUNET_break_op (0);
1703 emsg->ttl = htonl (ttl - 1);
1704 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
1708 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1709 amsg = (struct GNUNET_MESH_ACK *) data;
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
1714 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1715 pmsg = (struct GNUNET_MESH_Poll *) data;
1717 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
1735 * Sends an already built message on a tunnel, choosing the best connection.
1737 * @param message Message to send. Function modifies it.
1738 * @param t Tunnel on which this message is transmitted.
1739 * @param ch Channel on which this message is transmitted.
1740 * @param fwd Is this a fwd message?
1743 send_prebuilt_message_tunnel (struct GNUNET_MESH_Encrypted *msg,
1744 struct MeshTunnel2 *t,
1745 struct MeshChannel *ch,
1748 struct MeshConnection *c;
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
1753 c = tunnel_get_connection (t, fwd);
1756 GNUNET_break (GNUNET_YES == t->destroy);
1759 type = ntohs (msg->header.type);
1762 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1763 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1764 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1765 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1767 msg->ttl = htonl (default_ttl);
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1771 GNUNET_MESH_DEBUG_M2S (type));
1775 send_prebuilt_message_connection (&msg->header, c, ch, fwd);
1780 * Sends an already built message on a channel, properly registering
1781 * all used resources and encrypting the message with the tunnel's key.
1783 * @param message Message to send. Function makes a copy of it.
1784 * @param ch Channel on which this message is transmitted.
1785 * @param fwd Is this a fwd message?
1788 send_prebuilt_message_channel (const struct GNUNET_MessageHeader *message,
1789 struct MeshChannel *ch,
1792 struct GNUNET_MESH_Encrypted *msg;
1793 size_t size = ntohs (message->size);
1794 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X\n",
1799 peer2s (ch->t->peer), ch->gid);
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
1801 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
1802 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD : GNUNET_MESSAGE_TYPE_MESH_BCK;
1803 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1805 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1806 msg->header.type = htons (type);
1807 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1808 msg->iv = GNUNET_htonll (iv);
1809 tunnel_encrypt (ch->t, &msg[1], message, size, iv, fwd);
1811 send_prebuilt_message_tunnel (msg, ch->t, ch, fwd);
1816 * Sends a CREATE CONNECTION message for a path to a peer.
1817 * Changes the connection and tunnel states if necessary.
1819 * @param connection Connection to create.
1822 send_connection_create (struct MeshConnection *connection)
1824 struct MeshTunnel2 *t;
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
1829 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
1830 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1831 (connection->path->length *
1832 sizeof (struct GNUNET_PeerIdentity)),
1837 (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
1838 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1839 if (MESH_CONNECTION_NEW == connection->state)
1840 connection_change_state (connection, MESH_CONNECTION_SENT);
1845 * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
1848 * @param connection Connection to confirm.
1849 * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
1852 send_connection_ack (struct MeshConnection *connection, int fwd)
1854 struct MeshTunnel2 *t;
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
1859 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
1860 sizeof (struct GNUNET_MESH_ConnectionACK),
1864 if (MESH_TUNNEL_NEW == t->state)
1865 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1870 * Build a hop-by-hop ACK message and queue it to send for the given connection.
1872 * @param c Which connection to send the hop-by-hop ACK.
1873 * @param ack Value of the ACK.
1874 * @param fwd Is this fwd?
1877 send_ack (struct MeshConnection *c, uint32_t ack, int fwd)
1879 struct GNUNET_MESH_ACK msg;
1881 msg.header.size = htons (sizeof (msg));
1882 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1883 msg.ack = htonl (ack);
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1887 "send %s ack %u on %s\n",
1888 fwd ? "FWD" : "BCK", ack, GNUNET_h2s (&c->id));
1890 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
1895 * Core callback to write a pre-constructed data packet to core buffer
1897 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1898 * @param size Number of bytes available in buf.
1899 * @param buf Where the to write the message.
1901 * @return number of bytes written to buf
1904 send_core_data_raw (void *cls, size_t size, void *buf)
1906 struct GNUNET_MessageHeader *msg = cls;
1909 GNUNET_assert (NULL != msg);
1910 total_size = ntohs (msg->size);
1912 if (total_size > size)
1917 memcpy (buf, msg, total_size);
1924 * Function to send a create connection message to a peer.
1926 * @param c Connection to create.
1927 * @param size number of bytes available in buf
1928 * @param buf where the callee should write the message
1929 * @return number of bytes written to buf
1932 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
1934 struct GNUNET_MESH_ConnectionCreate *msg;
1935 struct GNUNET_PeerIdentity *peer_ptr;
1936 struct MeshPeerPath *p = c->path;
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
1942 sizeof (struct GNUNET_MESH_ConnectionCreate) +
1943 p->length * sizeof (struct GNUNET_PeerIdentity);
1945 if (size < size_needed || NULL == buf)
1950 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
1951 msg->header.size = htons (size_needed);
1952 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
1955 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1956 for (i = 0; i < p->length; i++)
1958 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
1968 * Creates a path ack message in buf and frees all unused resources.
1970 * @param c Connection to send an ACK on.
1971 * @param size number of bytes available in buf
1972 * @param buf where the callee should write the message
1974 * @return number of bytes written to buf
1977 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
1979 struct GNUNET_MESH_ConnectionACK *msg = buf;
1980 struct MeshTunnel2 *t = c->t;
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
1983 GNUNET_assert (NULL != t);
1984 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
1989 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
1990 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
1993 /* TODO add signature */
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
1996 return sizeof (struct GNUNET_MESH_ConnectionACK);
2001 * Destroy the peer_info and free any allocated resources linked to it
2003 * @param peer The peer_info to destroy.
2005 * @return GNUNET_OK on success
2008 peer_destroy (struct MeshPeer *peer)
2010 struct GNUNET_PeerIdentity id;
2011 struct MeshPeerPath *p;
2012 struct MeshPeerPath *nextp;
2014 GNUNET_PEER_resolve (peer->id, &id);
2015 GNUNET_PEER_change_rc (peer->id, -1);
2018 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
2021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2022 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2024 if (NULL != peer->dhtget)
2026 GNUNET_DHT_get_stop (peer->dhtget);
2028 p = peer->path_head;
2032 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2036 tunnel_destroy_empty (peer->tunnel);
2043 * Returns if peer is used (has a tunnel, is neighbor).
2045 * @peer Peer to check.
2047 * @return GNUNET_YES if peer is in use.
2050 peer_is_used (struct MeshPeer *peer)
2052 struct MeshPeerPath *p;
2054 if (NULL != peer->tunnel)
2057 for (p = peer->path_head; NULL != p; p = p->next)
2066 * Iterator over all the peers to get the oldest timestamp.
2068 * @param cls Closure (unsued).
2069 * @param key ID of the peer.
2070 * @param value Peer_Info of the peer.
2073 peer_get_oldest (void *cls,
2074 const struct GNUNET_HashCode *key,
2077 struct MeshPeer *p = value;
2078 struct GNUNET_TIME_Absolute *abs = cls;
2080 /* Don't count active peers */
2081 if (GNUNET_YES == peer_is_used (p))
2084 if (abs->abs_value_us < p->last_contact.abs_value_us)
2085 abs->abs_value_us = p->last_contact.abs_value_us;
2092 * Iterator over all the peers to remove the oldest entry.
2094 * @param cls Closure (unsued).
2095 * @param key ID of the peer.
2096 * @param value Peer_Info of the peer.
2099 peer_timeout (void *cls,
2100 const struct GNUNET_HashCode *key,
2103 struct MeshPeer *p = value;
2104 struct GNUNET_TIME_Absolute *abs = cls;
2106 if (p->last_contact.abs_value_us == abs->abs_value_us &&
2107 GNUNET_NO == peer_is_used (p))
2117 * Delete oldest unused peer.
2120 peer_delete_oldest (void)
2122 struct GNUNET_TIME_Absolute abs;
2124 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
2126 GNUNET_CONTAINER_multihashmap_iterate (peers,
2129 GNUNET_CONTAINER_multihashmap_iterate (peers,
2136 * Retrieve the MeshPeer stucture associated with the peer, create one
2137 * and insert it in the appropriate structures if the peer is not known yet.
2139 * @param peer Full identity of the peer.
2141 * @return Existing or newly created peer info.
2143 static struct MeshPeer *
2144 peer_get (const struct GNUNET_PeerIdentity *peer_id)
2146 struct MeshPeer *peer;
2148 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
2151 peer = GNUNET_new (struct MeshPeer);
2152 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2154 peer_delete_oldest ();
2156 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
2157 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2158 peer->id = GNUNET_PEER_intern (peer_id);
2160 peer->last_contact = GNUNET_TIME_absolute_get();
2167 * Retrieve the MeshPeer stucture associated with the peer, create one
2168 * and insert it in the appropriate structures if the peer is not known yet.
2170 * @param peer Short identity of the peer.
2172 * @return Existing or newly created peer info.
2174 static struct MeshPeer *
2175 peer_get_short (const GNUNET_PEER_Id peer)
2177 return peer_get (GNUNET_PEER_resolve2 (peer));
2182 * Get a cost of a path for a peer considering existing tunnel connections.
2184 * @param peer Peer towards which the path is considered.
2185 * @param path Candidate path.
2187 * @return Cost of the path (path length + number of overlapping nodes)
2190 peer_get_path_cost (const struct MeshPeer *peer,
2191 const struct MeshPeerPath *path)
2193 struct MeshConnection *c;
2194 unsigned int overlap;
2202 GNUNET_assert (NULL != peer->tunnel);
2204 for (i = 0; i < path->length; i++)
2206 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2208 for (j = 0; j < c->path->length; j++)
2210 if (path->peers[i] == c->path->peers[j])
2218 return (path->length + overlap) * (path->score * -1);
2223 * Choose the best path towards a peer considering the tunnel properties.
2225 * @param peer The destination peer.
2227 * @return Best current known path towards the peer, if any.
2229 static struct MeshPeerPath *
2230 peer_get_best_path (const struct MeshPeer *peer)
2232 struct MeshPeerPath *best_p;
2233 struct MeshPeerPath *p;
2234 struct MeshConnection *c;
2235 unsigned int best_cost;
2238 best_cost = UINT_MAX;
2240 for (p = peer->path_head; NULL != p; p = p->next)
2242 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
2246 continue; /* If path is in use in a connection, skip it. */
2248 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
2258 queue_is_sendable (struct MeshPeerQueue *q)
2260 struct MeshFlowControl *fc;
2262 /* Is PID-independent? */
2265 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2266 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2270 /* Is PID allowed? */
2271 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
2272 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
2280 * Get first sendable message.
2282 * @param peer The destination peer.
2284 * @return Best current known path towards the peer, if any.
2286 static struct MeshPeerQueue *
2287 peer_get_first_message (const struct MeshPeer *peer)
2289 struct MeshPeerQueue *q;
2291 for (q = peer->queue_head; NULL != q; q = q->next)
2293 if (queue_is_sendable (q))
2302 * Try to establish a new connection to this peer in the given tunnel.
2303 * If the peer doesn't have any path to it yet, try to get one.
2304 * If the peer already has some path, send a CREATE CONNECTION towards it.
2306 * @param peer PeerInfo of the peer.
2309 peer_connect (struct MeshPeer *peer)
2311 struct MeshTunnel2 *t;
2312 struct MeshPeerPath *p;
2313 struct MeshConnection *c;
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 "peer_connect towards %s\n",
2319 if (NULL != peer->path_head)
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
2322 p = peer_get_best_path (peer);
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
2326 c = tunnel_use_path (t, p);
2327 send_connection_create (c);
2330 else if (NULL == peer->dhtget)
2332 const struct GNUNET_PeerIdentity *id;
2334 id = GNUNET_PEER_resolve2 (peer->id);
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2336 " Starting DHT GET for peer %s\n", peer2s (peer));
2337 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2338 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2339 &id->hashPubKey, /* key to search */
2340 dht_replication_level, /* replication level */
2341 GNUNET_DHT_RO_RECORD_ROUTE |
2342 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2344 0, /* xquery bits */
2345 &dht_get_id_handler, peer);
2346 if (MESH_TUNNEL_NEW == t->state)
2347 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "There is no path but the DHT GET is already started.\n");
2358 * Get the first transmittable message for a connection.
2360 * @param c Connection.
2361 * @param fwd Is this FWD?
2363 * @return First transmittable message.
2365 static struct MeshPeerQueue *
2366 connection_get_first_message (struct MeshConnection *c, int fwd)
2368 struct MeshPeerQueue *q;
2371 p = connection_get_hop (c, fwd);
2373 for (q = p->queue_head; NULL != q; q = q->next)
2377 if (queue_is_sendable (q))
2386 * Is this peer the first one on the connection?
2388 * @param c Connection.
2389 * @param fwd Is this about fwd traffic?
2391 * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
2394 connection_is_origin (struct MeshConnection *c, int fwd)
2396 if (!fwd && c->own_pos == c->path->length - 1)
2398 if (fwd && c->own_pos == 0)
2405 * Is this peer the last one on the connection?
2407 * @param c Connection.
2408 * @param fwd Is this about fwd traffic?
2409 * Note that the ROOT is the terminal for BCK traffic!
2411 * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
2414 connection_is_terminal (struct MeshConnection *c, int fwd)
2416 if (fwd && c->own_pos == c->path->length - 1)
2418 if (!fwd && c->own_pos == 0)
2425 * @brief Re-initiate traffic on this connection if necessary.
2427 * Check if there is traffic queued towards this peer
2428 * and the core transmit handle is NULL (traffic was stalled).
2429 * If so, call core tmt rdy.
2431 * @param c Connection on which initiate traffic.
2432 * @param fwd Is this about fwd traffic?
2435 connection_unlock_queue (struct MeshConnection *c, int fwd)
2437 struct MeshPeer *peer;
2438 struct MeshPeerQueue *q;
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 "connection_unlock_queue %s on %s\n",
2443 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
2445 if (connection_is_origin (c, fwd))
2447 struct MeshTunnel2 *t = c->t;
2448 struct MeshChannel *ch;
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " is origin!\n");
2451 /* FIXME randomize channel selection, not always first channel */
2452 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2454 int blocked = fwd ? ch->blocked_fwd : ch->blocked_bck;
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %X - %s\n",
2457 ch->gid, blocked ? "blocked " : "not blocked");
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " blocked!\n");
2461 send_local_ack (ch, fwd ? ch->root : ch->dest, fwd);
2462 return; /* FIXME authorize all channels? */
2468 peer = connection_get_hop (c, fwd);
2470 if (NULL != peer->core_transmit)
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
2473 return; /* Already unlocked */
2476 q = connection_get_first_message (c, fwd);
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
2480 return; /* Nothing to transmit */
2484 peer->core_transmit =
2485 GNUNET_CORE_notify_transmit_ready (core_handle,
2488 GNUNET_TIME_UNIT_FOREVER_REL,
2489 GNUNET_PEER_resolve2 (peer->id),
2497 * Cancel all transmissions that belong to a certain connection.
2499 * @param c Connection which to cancel.
2500 * @param fwd Cancel fwd traffic?
2503 connection_cancel_queues (struct MeshConnection *c, int fwd)
2505 struct MeshPeerQueue *q;
2506 struct MeshPeerQueue *next;
2507 struct MeshFlowControl *fc;
2508 struct MeshPeer *peer;
2515 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2516 peer = connection_get_hop (c, fwd);
2518 for (q = peer->queue_head; NULL != q; q = next)
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2524 "connection_cancel_queue %s\n",
2525 GNUNET_MESH_DEBUG_M2S (q->type));
2526 queue_destroy (q, GNUNET_YES);
2529 if (NULL == peer->queue_head)
2531 if (NULL != peer->core_transmit)
2533 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
2534 peer->core_transmit = NULL;
2536 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
2538 GNUNET_SCHEDULER_cancel (fc->poll_task);
2539 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2546 * Remove all paths that rely on a direct connection between p1 and p2
2547 * from the peer itself and notify all tunnels about it.
2549 * @param peer PeerInfo of affected peer.
2550 * @param p1 GNUNET_PEER_Id of one peer.
2551 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2554 * TODO: optimize (see below)
2557 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
2560 struct MeshPeerPath *p;
2561 struct MeshPeerPath *next;
2562 struct MeshPeer *peer_d;
2564 unsigned int destroyed;
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2569 for (p = peer->path_head; NULL != p; p = next)
2572 for (i = 0; i < (p->length - 1); i++)
2574 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2575 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2577 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2588 d = tunnel_notify_connection_broken (peer->tunnel, p1, p2);
2590 peer_d = peer_get_short (d); // FIXME
2591 next = peer_get_best_path (peer_d);
2592 tunnel_use_path (peer->tunnel, next);
2593 peer_connect (peer_d);
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2600 * Add the path to the peer and update the path used to reach it in case this
2603 * @param peer_info Destination peer to add the path to.
2604 * @param path New path to add. Last peer must be the peer in arg 1.
2605 * Path will be either used of freed if already known.
2606 * @param trusted Do we trust that this path is real?
2609 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
2612 struct MeshPeerPath *aux;
2616 if ((NULL == peer_info) || (NULL == path))
2619 path_destroy (path);
2622 if (path->peers[path->length - 1] != peer_info->id)
2625 path_destroy (path);
2628 if (2 >= path->length && GNUNET_NO == trusted)
2630 /* Only allow CORE to tell us about direct paths */
2631 path_destroy (path);
2634 for (l = 1; l < path->length; l++)
2636 if (path->peers[l] == myid)
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2639 for (l2 = 0; l2 < path->length - l; l2++)
2641 path->peers[l2] = path->peers[l + l2];
2646 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2651 path->length, peer2s (peer_info));
2653 l = path_get_length (path);
2656 path_destroy (path);
2660 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2661 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2663 l2 = path_get_length (aux);
2666 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2667 peer_info->path_tail, aux, path);
2672 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2674 path_destroy (path);
2679 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2686 * Add the path to the origin peer and update the path used to reach it in case
2687 * this is the shortest.
2688 * The path is given in peer_info -> destination, therefore we turn the path
2689 * upside down first.
2691 * @param peer_info Peer to add the path to, being the origin of the path.
2692 * @param path New path to add after being inversed.
2693 * Path will be either used or freed.
2694 * @param trusted Do we trust that this path is real?
2697 peer_add_path_to_origin (struct MeshPeer *peer_info,
2698 struct MeshPeerPath *path, int trusted)
2701 peer_add_path (peer_info, path, trusted);
2707 * Function called if a connection has been stalled for a while,
2708 * possibly due to a missed ACK. Poll the neighbor about its ACK status.
2710 * @param cls Closure (poll ctx).
2711 * @param tc TaskContext.
2714 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2716 struct MeshFlowControl *fc = cls;
2717 struct GNUNET_MESH_Poll msg;
2718 struct MeshConnection *c;
2720 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2721 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** connection %s[%X]\n",
2729 peer2s (c->t->peer), c->id);
2731 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2732 msg.header.size = htons (sizeof (msg));
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
2734 send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
2735 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2736 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2737 &connection_poll, fc);
2742 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2743 * to obtain a local peer -> destination path and interning the peer ids.
2745 * @return Newly allocated and created path
2747 static struct MeshPeerPath *
2748 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2749 unsigned int get_path_length,
2750 const struct GNUNET_PeerIdentity *put_path,
2751 unsigned int put_path_length)
2753 struct MeshPeerPath *p;
2759 GNUNET_PEER_change_rc (myid, 1);
2760 i = get_path_length;
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2762 for (i--; i >= 0; i--)
2764 id = GNUNET_PEER_intern (&get_path[i]);
2765 if (p->length > 0 && id == p->peers[p->length - 1])
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2768 GNUNET_PEER_change_rc (id, -1);
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2773 GNUNET_i2s (&get_path[i]));
2775 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2776 p->peers[p->length - 1] = id;
2779 i = put_path_length;
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2781 for (i--; i >= 0; i--)
2783 id = GNUNET_PEER_intern (&put_path[i]);
2786 /* PUT path went through us, so discard the path up until now and start
2787 * from here to get a much shorter (and loop-free) path.
2792 if (p->length > 0 && id == p->peers[p->length - 1])
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2795 GNUNET_PEER_change_rc (id, -1);
2799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2800 GNUNET_i2s (&put_path[i]));
2802 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2803 p->peers[p->length - 1] = id;
2807 if (get_path_length > 0)
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2809 GNUNET_i2s (&get_path[0]));
2810 if (put_path_length > 0)
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2812 GNUNET_i2s (&put_path[0]));
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2815 for (i = 0; i < p->length; i++)
2817 struct GNUNET_PeerIdentity peer_id;
2819 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2821 GNUNET_i2s (&peer_id));
2829 * Adds a path to the peer_infos of all the peers in the path
2831 * @param p Path to process.
2832 * @param confirmed Whether we know if the path works or not.
2835 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2839 /* TODO: invert and add */
2840 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2841 for (i++; i < p->length; i++)
2843 struct MeshPeer *aux;
2844 struct MeshPeerPath *copy;
2846 aux = peer_get_short (p->peers[i]);
2847 copy = path_duplicate (p);
2848 copy->length = i + 1;
2849 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2855 * Search for a channel among the channels for a client
2857 * @param c the client whose channels to search in
2858 * @param chid the local id of the channel
2860 * @return channel handler, NULL if doesn't exist
2862 static struct MeshChannel *
2863 channel_get_by_local_id (struct MeshClient *c, MESH_ChannelNumber chid)
2865 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
2867 GNUNET_break_op (0);
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
2871 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
2872 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
2873 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
2877 * Search for a tunnel by global ID using full PeerIdentities.
2879 * @param t Tunnel containing the channel.
2880 * @param chid Public channel number.
2882 * @return channel handler, NULL if doesn't exist
2884 static struct MeshChannel *
2885 channel_get (struct MeshTunnel2 *t, MESH_ChannelNumber chid)
2887 struct MeshChannel *ch;
2892 for (ch = t->channel_head; NULL != ch; ch = ch->next)
2894 if (ch->gid == chid)
2903 * Change the tunnel state.
2905 * @param t Tunnel whose state to change.
2906 * @param state New state.
2909 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "Tunnel %s state was %s\n",
2916 GNUNET_MESH_DEBUG_TS2S (t->state));
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2918 "Tunnel %s state is now %s\n",
2920 GNUNET_MESH_DEBUG_TS2S (state));
2926 * Cache a message to be sent once tunnel is online.
2928 * @param t Tunnel to hold the message.
2929 * @param ch Channel the message is about.
2930 * @param msg Message itself (copy will be made).
2931 * @param fwd Is this fwd?
2934 tunnel_queue_data (struct MeshTunnel2 *t,
2935 struct MeshChannel *ch,
2936 struct GNUNET_MessageHeader *msg,
2939 struct MeshTunnelQueue *tq;
2940 uint16_t size = ntohs (msg->size);
2942 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
2945 memcpy (&tq[1], msg, size);
2946 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
2951 * Send all cached messages that we can, tunnel is online.
2953 * @param t Tunnel that holds the messages.
2954 * @param fwd Is this fwd?
2957 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
2959 struct MeshTunnelQueue *tq;
2960 struct MeshTunnelQueue *next;
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2964 "tunnel_send_queued_data on tunnel %s\n",
2966 room = tunnel_get_buffer (t, fwd);
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
2968 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
2972 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
2973 send_prebuilt_message_channel ((struct GNUNET_MessageHeader *) &tq[1],
2982 connection_change_state (struct MeshConnection* c,
2983 enum MeshConnectionState state)
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2986 "Connection %s state was %s\n",
2987 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (c->state));
2988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2989 "Connection %s state is now %s\n",
2990 GNUNET_h2s (&c->id), GNUNET_MESH_DEBUG_CS2S (state));
2996 * Add a client to a channel, initializing all needed data structures.
2998 * @param ch Channel to which add the client.
2999 * @param c Client which to add to the channel.
3002 channel_add_client (struct MeshChannel *ch, struct MeshClient *c)
3004 struct MeshTunnel2 *t = ch->t;
3006 if (NULL != ch->dest)
3012 /* Assign local id as destination */
3013 while (NULL != channel_get_by_local_id (c, t->next_local_chid))
3014 t->next_local_chid = (t->next_local_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3015 ch->lid_dest = t->next_local_chid++;
3016 t->next_local_chid = t->next_local_chid | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3018 /* Store in client's hashmap */
3020 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
3022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3031 static struct MeshConnection *
3032 tunnel_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
3034 struct MeshConnection *c;
3035 struct GNUNET_HashCode cid;
3036 struct MeshPeer *peer;
3037 unsigned int own_pos;
3039 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
3041 c = connection_new (&cid);
3043 for (own_pos = 0; own_pos < p->length; own_pos++)
3045 if (p->peers[own_pos] == myid)
3048 if (own_pos > p->length - 1)
3051 connection_destroy (c);
3054 c->own_pos = own_pos;
3059 c->fwd_maintenance_task =
3060 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3061 &connection_fwd_keepalive, c);
3064 peer = connection_get_next_hop (c);
3065 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3067 peer = connection_get_prev_hop (c);
3068 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
3069 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3075 * Notifies a tunnel that a connection has broken that affects at least
3076 * some of its peers. Sends a notification towards the root of the tree.
3077 * In case the peer is the owner of the tree, notifies the client that owns
3078 * the tunnel and tries to reconnect.
3080 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
3082 * @param t Tunnel affected.
3083 * @param p1 Peer that got disconnected from p2.
3084 * @param p2 Peer that got disconnected from p1.
3086 * @return Short ID of the peer disconnected (either p1 or p2).
3087 * 0 if the tunnel remained unaffected.
3089 static GNUNET_PEER_Id
3090 tunnel_notify_connection_broken (struct MeshTunnel2* t,
3091 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
3093 // if (myid != p1 && myid != p2) FIXME
3098 // if (tree_get_predecessor (t->tree) != 0)
3100 // /* We are the peer still connected, notify owner of the disconnection. */
3101 // struct GNUNET_MESH_PathBroken msg;
3102 // struct GNUNET_PeerIdentity neighbor;
3104 // msg.header.size = htons (sizeof (msg));
3105 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3106 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3107 // msg.tid = htonl (t->id.tid);
3108 // msg.peer1 = my_full_id;
3109 // GNUNET_PEER_resolve (pid, &msg.peer2);
3110 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3111 // send_prebuilt_message (&msg.header, &neighbor, t);
3118 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
3120 * @param ch Channel this is about.
3121 * @param fwd Is for FWD traffic? (ACK dest->owner)
3124 channel_send_data_ack (struct MeshChannel *ch, int fwd)
3126 struct GNUNET_MESH_DataACK msg;
3127 struct MeshChannelReliability *rel;
3128 struct MeshReliableMessage *copy;
3134 if (GNUNET_NO == ch->reliable)
3139 rel = fwd ? ch->bck_rel : ch->fwd_rel;
3140 mid = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "send_data_ack for %u\n",
3145 type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
3146 msg.header.type = htons (type);
3147 msg.header.size = htons (sizeof (msg));
3148 msg.chid = htonl (ch->gid);
3149 msg.mid = htonl (*mid - 1);
3151 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
3153 if (copy->type != type)
3155 delta = copy->mid - *mid;
3158 mask = 0x1LL << delta;
3159 msg.futures |= mask;
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3161 " setting bit for %u (delta %u) (%llX) -> %llX\n",
3162 copy->mid, delta, mask, msg.futures);
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
3166 send_prebuilt_message_channel (&msg.header, ch, fwd);
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
3172 * Send an ACK informing the predecessor about the available buffer space.
3174 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
3175 * the ACK itself goes "back" (dest->root).
3177 * @param c Connection on which to send the ACK.
3178 * @param fwd Is this FWD ACK? (Going dest->owner)
3181 connection_send_ack (struct MeshConnection *c, int fwd)
3183 struct MeshFlowControl *next_fc;
3184 struct MeshFlowControl *prev_fc;
3188 next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
3189 prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 "connection send %s ack on %s\n",
3193 fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
3195 /* Check if we need to transmit the ACK */
3196 if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
3199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3200 " last pid recv: %u, last ack sent: %u\n",
3201 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
3205 /* Ok, ACK might be necessary, what PID to ACK? */
3206 delta = next_fc->queue_max - next_fc->queue_n;
3207 ack = prev_fc->last_pid_recv + delta;
3208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 " last pid %u, last ack %u, qmax %u, q %u\n",
3211 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
3212 next_fc->queue_max, next_fc->queue_n);
3213 if (ack == prev_fc->last_ack_sent)
3215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
3219 prev_fc->last_ack_sent = ack;
3220 send_ack (c, ack, fwd);
3225 * Modify the mesh message TID from global to local and send to client.
3227 * @param ch Channel on which to send the message.
3228 * @param msg Message to modify and send.
3229 * @param c Client to send to.
3230 * @param tid Tunnel ID to use (c can be both owner and client).
3233 channel_send_client_to_tid (struct MeshChannel *ch,
3234 const struct GNUNET_MESH_Data *msg,
3235 struct MeshClient *c, MESH_ChannelNumber id)
3237 struct GNUNET_MESH_LocalData *copy;
3238 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
3239 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
3241 if (size < sizeof (struct GNUNET_MessageHeader))
3243 GNUNET_break_op (0);
3251 copy = (struct GNUNET_MESH_LocalData *) cbuf;
3252 memcpy (©[1], &msg[1], size);
3253 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
3254 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
3255 copy->id = htonl (id);
3256 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
3257 ©->header, GNUNET_NO);
3261 * Modify the data message ID from global to local and send to client.
3263 * @param ch Channel on which to send the message.
3264 * @param msg Message to modify and send.
3265 * @param fwd Forward?
3268 channel_send_client_data (struct MeshChannel *ch,
3269 const struct GNUNET_MESH_Data *msg,
3273 channel_send_client_to_tid (ch, msg, ch->dest, ch->lid_dest);
3275 channel_send_client_to_tid (ch, msg, ch->root, ch->lid_root);
3280 * Send a buffered message to the client, for in order delivery or
3281 * as result of client ACK.
3283 * @param ch Channel on which to empty the message buffer.
3284 * @param c Client to send to.
3285 * @param rel Reliability structure to corresponding peer.
3286 * If rel == bck_rel, this is FWD data.
3289 channel_send_client_buffered_data (struct MeshChannel *ch,
3290 struct MeshClient *c,
3291 struct MeshChannelReliability *rel)
3293 struct MeshReliableMessage *copy;
3296 if (GNUNET_NO == rel->client_ready)
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
3302 mid = rel == ch->bck_rel ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
3305 copy = rel->head_recv;
3306 /* We never buffer channel management messages */
3309 if (copy->mid == *mid || GNUNET_NO == ch->reliable)
3311 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3314 " have %u! now expecting %u\n",
3315 copy->mid, *mid + 1);
3316 channel_send_client_data (ch, msg, (rel == ch->bck_rel));
3319 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3325 " reliable && don't have %u, next is %u\n",
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
3336 * We have received a message out of order, or the client is not ready.
3337 * Buffer it until we receive an ACK from the client or the missing
3338 * message from the channel.
3340 * @param msg Message to buffer (MUST be of type MESH_DATA).
3341 * @param rel Reliability data to the corresponding direction.
3344 channel_rel_add_buffered_data (const struct GNUNET_MESH_Data *msg,
3345 struct MeshChannelReliability *rel)
3347 struct MeshReliableMessage *copy;
3348 struct MeshReliableMessage *prev;
3352 size = ntohs (msg->header.size);
3353 mid = ntohl (msg->mid);
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
3357 copy = GNUNET_malloc (sizeof (*copy) + size);
3360 memcpy (©[1], msg, size);
3364 // FIXME do something better than O(n), although n < 64...
3365 // FIXME start from the end (most messages are the latest ones)
3366 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
3369 if (GMC_is_pid_bigger (prev->mid, mid))
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
3372 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
3378 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
3384 * Destroy a reliable message after it has been acknowledged, either by
3385 * direct mid ACK or bitfield. Updates the appropriate data structures and
3386 * timers and frees all memory.
3388 * @param copy Message that is no longer needed: remote peer got it.
3391 rel_message_free (struct MeshReliableMessage *copy)
3393 struct MeshChannelReliability *rel;
3394 struct GNUNET_TIME_Relative time;
3397 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3398 rel->expected_delay.rel_value_us *= 7;
3399 rel->expected_delay.rel_value_us += time.rel_value_us;
3400 rel->expected_delay.rel_value_us /= 8;
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
3405 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3407 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3409 rel->retry_timer = rel->expected_delay;
3410 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3416 * Destroy all reliable messages queued for a channel,
3417 * during a channel destruction.
3418 * Frees the reliability structure itself.
3420 * @param rel Reliability data for a channel.
3423 channel_rel_free_all (struct MeshChannelReliability *rel)
3425 struct MeshReliableMessage *copy;
3426 struct MeshReliableMessage *next;
3431 for (copy = rel->head_recv; NULL != copy; copy = next)
3434 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
3437 for (copy = rel->head_sent; NULL != copy; copy = next)
3440 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
3443 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
3444 GNUNET_SCHEDULER_cancel (rel->retry_task);
3450 * Mark future messages as ACK'd.
3452 * @param rel Reliability data.
3453 * @param msg DataACK message with a bitfield of future ACK'd messages.
3456 channel_rel_free_sent (struct MeshChannelReliability *rel,
3457 const struct GNUNET_MESH_DataACK *msg)
3459 struct MeshReliableMessage *copy;
3460 struct MeshReliableMessage *next;
3467 bitfield = msg->futures;
3468 mid = ntohl (msg->mid);
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3470 "free_sent_reliable %u %llX\n",
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 " rel %p, head %p\n",
3474 rel, rel->head_sent);
3475 for (i = 0, copy = rel->head_sent;
3476 i < 64 && NULL != copy && 0 != bitfield;
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3480 " trying bit %u (mid %u)\n",
3483 if (0 == (bitfield & mask))
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
3487 /* Bit was set, clear the bit from the bitfield */
3490 /* The i-th bit was set. Do we have that copy? */
3491 /* Skip copies with mid < target */
3492 target = mid + i + 1;
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
3494 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
3497 /* Did we run out of copies? (previously freed, it's ok) */
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
3504 /* Did we overshoot the target? (previously freed, it's ok) */
3505 if (GMC_is_pid_bigger (copy->mid, target))
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
3511 /* Now copy->mid == target, free it */
3513 rel_message_free (copy);
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
3521 * We haven't received an ACK after a certain time: restransmit the message.
3523 * @param cls Closure (MeshReliableMessage with the message to restransmit)
3524 * @param tc TaskContext.
3527 channel_retransmit_message (void *cls,
3528 const struct GNUNET_SCHEDULER_TaskContext *tc)
3530 struct MeshChannelReliability *rel = cls;
3531 struct MeshReliableMessage *copy;
3532 struct MeshPeerQueue *q;
3533 struct MeshChannel *ch;
3534 struct MeshConnection *c;
3535 struct GNUNET_MESH_Data *payload;
3536 struct MeshPeer *hop;
3539 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
3540 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3544 copy = rel->head_sent;
3551 /* Search the message to be retransmitted in the outgoing queue.
3552 * Check only the queue for the connection that is going to be used,
3553 * if the message is stuck in some other connection's queue we shouldn't
3555 * - cancelling it and sending the new one doesn't guarantee it's delivery,
3556 * the old connection could be temporary stalled or the queue happened to
3557 * be long at time of insertion.
3558 * - not sending the new one could cause terrible delays the old connection
3561 payload = (struct GNUNET_MESH_Data *) ©[1];
3562 fwd = (rel == ch->fwd_rel);
3563 c = tunnel_get_connection (ch->t, fwd);
3564 hop = connection_get_hop (c, fwd);
3565 for (q = hop->queue_head; NULL != q; q = q->next)
3567 if (ntohs (payload->header.type) == q->type && ch == q->ch)
3569 struct GNUNET_MESH_Data *queued_data = q->cls;
3571 if (queued_data->mid == payload->mid)
3576 /* Message not found in the queue that we are going to use. */
3579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
3581 send_prebuilt_message_channel (&payload->header, ch, ch->fwd_rel == rel);
3582 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
3589 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
3590 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3591 &channel_retransmit_message,
3597 * Send an ACK to a client if needed.
3599 * @param ch Channel this is regarding.
3600 * @param fwd Is this about fwd traffic? (ACK goes the opposite direction).
3603 channel_send_client_ack (struct MeshChannel *ch, int fwd)
3605 struct MeshClient *c;
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3609 "Channel send client %s ack on %s:%X\n",
3610 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3612 /* Check for buffer space */
3613 if (0 >= tunnel_get_buffer (ch->t, fwd))
3615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " No buffer :(\n");
3619 /* Client to receive the ACK (fwd indicates traffic to be ACK'd) */
3620 c = fwd ? ch->root : ch->dest;
3622 blocked = fwd ? ch->blocked_fwd : ch->blocked_bck;
3623 if (GNUNET_YES == blocked)
3624 send_local_ack (ch, c, fwd);
3626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client not blocked\n");
3631 * Send ACK on one or more connections due to buffer space to the client.
3633 * @param ch Channel which has some free buffer space.
3634 * @param buffer Buffer space.
3635 * @param fwd Is this in the FWD direction?
3638 channel_send_connection_ack (struct MeshChannel *ch, uint32_t buffer, int fwd)
3640 struct MeshTunnel2 *t = ch->t;
3641 struct MeshConnection *c;
3642 struct MeshFlowControl *fc;
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "Channel send connection %s ack on %s:%X\n",
3649 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid);
3651 /* Count connections, how many messages are already allowed */
3652 for (cs = 0, allowed = 0, c = t->connection_head; NULL != c; c = c->next)
3654 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3655 if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
3660 allowed += fc->last_ack_sent - fc->last_pid_recv;
3664 /* Make sure there is no overflow */
3665 if (allowed > buffer)
3671 /* Authorize connections to send more data */
3672 to_allow = buffer - allowed;
3673 for (c = t->connection_head; NULL != c && to_allow > 0; c = c->next)
3675 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3676 if (fc->last_ack_sent - fc->last_pid_recv > 64 / 3)
3680 send_ack (c, fc->last_ack_sent + 1, fwd);
3684 GNUNET_break (to_allow == 0);
3689 * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
3691 * @param ch Channel to mark as ready.
3692 * @param fwd Was the CREATE message sent fwd?
3695 channel_confirm (struct MeshChannel *ch, int fwd)
3697 struct MeshChannelReliability *rel;
3698 struct MeshReliableMessage *copy;
3699 struct MeshReliableMessage *next;
3701 ch->state = MESH_CHANNEL_READY;
3703 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3704 for (copy = rel->head_sent; NULL != copy; copy = next)
3706 struct GNUNET_MessageHeader *msg;
3709 msg = (struct GNUNET_MessageHeader *) ©[1];
3710 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
3712 rel_message_free (copy);
3720 * Save a copy to retransmit in case it gets lost.
3722 * Initializes all needed callbacks and timers.
3724 * @param ch Channel this message goes on.
3725 * @param msg Message to copy.
3726 * @param fwd Is this fwd traffic?
3729 channel_save_copy (struct MeshChannel *ch,
3730 const struct GNUNET_MessageHeader *msg,
3733 struct MeshChannelReliability *rel;
3734 struct MeshReliableMessage *copy;
3739 rel = fwd ? ch->fwd_rel : ch->bck_rel;
3740 mid = fwd ? ch->mid_send_fwd : ch->mid_send_bck;
3741 type = ntohs (msg->type);
3742 size = ntohs (msg->size);
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
3745 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
3747 copy->timestamp = GNUNET_TIME_absolute_get ();
3750 memcpy (©[1], msg, size);
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
3753 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
3754 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
3757 GNUNET_TIME_relative_multiply (rel->expected_delay,
3758 MESH_RETRANSMIT_MARGIN);
3760 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
3761 &channel_retransmit_message,
3768 * Send keepalive packets for a connection.
3770 * @param c Connection to keep alive..
3771 * @param fwd Is this a FWD keepalive? (owner -> dest).
3774 connection_keepalive (struct MeshConnection *c, int fwd)
3776 struct GNUNET_MESH_ConnectionKeepAlive *msg;
3777 size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
3781 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
3782 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3785 "sending %s keepalive for connection %s[%d]\n",
3786 fwd ? "FWD" : "BCK",
3787 peer2s (c->t->peer),
3790 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
3791 msg->header.size = htons (size);
3792 msg->header.type = htons (type);
3795 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
3800 * Send CONNECTION_{CREATE/ACK} packets for a connection.
3802 * @param c Connection for which to send the message.
3803 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
3806 connection_recreate (struct MeshConnection *c, int fwd)
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
3810 send_connection_create (c);
3812 send_connection_ack (c, GNUNET_NO);
3817 * Generic connection timer management.
3818 * Depending on the role of the peer in the connection will send the
3819 * appropriate message (build or keepalive)
3821 * @param c Conncetion to maintain.
3822 * @param fwd Is FWD?
3825 connection_maintain (struct MeshConnection *c, int fwd)
3827 if (MESH_TUNNEL_SEARCHING == c->t->state)
3829 /* TODO DHT GET with RO_BART */
3834 case MESH_CONNECTION_NEW:
3836 case MESH_CONNECTION_SENT:
3837 connection_recreate (c, fwd);
3839 case MESH_CONNECTION_READY:
3840 connection_keepalive (c, fwd);
3849 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3851 struct MeshConnection *c = cls;
3853 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3854 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3857 connection_maintain (c, GNUNET_YES);
3858 c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3859 &connection_fwd_keepalive,
3865 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3867 struct MeshConnection *c = cls;
3869 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3870 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3873 connection_maintain (c, GNUNET_NO);
3874 c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
3875 &connection_bck_keepalive,
3881 * Send a message to all peers in this connection that the connection
3882 * is no longer valid.
3884 * If some peer should not receive the message, it should be zero'ed out
3885 * before calling this function.
3887 * @param c The connection whose peers to notify.
3890 connection_send_destroy (struct MeshConnection *c)
3892 struct GNUNET_MESH_ConnectionDestroy msg;
3894 msg.header.size = htons (sizeof (msg));
3895 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 " sending connection destroy for connection %s[%X]\n",
3899 peer2s (c->t->peer),
3902 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_YES);
3903 send_prebuilt_message_connection (&msg.header, c, NULL, GNUNET_NO);
3904 c->destroy = GNUNET_YES;
3909 * Confirm we got a channel create.
3911 * @param ch The channel to confirm.
3912 * @param fwd Should we send the ACK fwd?
3915 channel_send_ack (struct MeshChannel *ch, int fwd)
3917 struct GNUNET_MESH_ChannelManage msg;
3919 msg.header.size = htons (sizeof (msg));
3920 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
3921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3922 " sending channel ack for channel %s:%X\n",
3923 peer2s (ch->t->peer),
3926 msg.chid = htonl (ch->gid);
3927 send_prebuilt_message_channel (&msg.header, ch, fwd);
3932 * Send a message to all clients (local and remote) of this channel
3933 * notifying that the channel is no longer valid.
3935 * If some peer or client should not receive the message,
3936 * should be zero'ed out before calling this function.
3938 * @param ch The channel whose clients to notify.
3941 channel_send_destroy (struct MeshChannel *ch)
3943 struct GNUNET_MESH_ChannelManage msg;
3945 msg.header.size = htons (sizeof (msg));
3946 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3948 " sending channel destroy for channel %s:%X\n",
3949 peer2s (ch->t->peer),
3952 if (NULL != ch->root)
3954 msg.chid = htonl (ch->lid_root);
3955 send_local_channel_destroy (ch, GNUNET_NO);
3959 msg.chid = htonl (ch->gid);
3960 send_prebuilt_message_channel (&msg.header, ch, GNUNET_NO);
3963 if (NULL != ch->dest)
3965 msg.chid = htonl (ch->lid_dest);
3966 send_local_channel_destroy (ch, GNUNET_YES);
3970 msg.chid = htonl (ch->gid);
3971 send_prebuilt_message_channel (&msg.header, ch, GNUNET_YES);
3979 static struct MeshTunnel2 *
3982 struct MeshTunnel2 *t;
3984 t = GNUNET_new (struct MeshTunnel2);
3986 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
3988 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
3989 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3991 // GNUNET_break (0);
3992 // tunnel_destroy (t);
3996 // char salt[] = "salt";
3997 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
3998 // salt, sizeof (salt),
3999 // &t->e_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4000 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4001 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4003 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4004 // salt, sizeof (salt),
4005 // &t->d_key, sizeof (struct GNUNET_CRYPTO_AesSessionKey),
4006 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
4007 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
4015 * Add a connection to a tunnel.
4018 * @param c Connection.
4021 tunnel_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
4024 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
4029 * Initialize a Flow Control structure to the initial state.
4031 * @param fc Flow Control structure to initialize.
4034 fc_init (struct MeshFlowControl *fc)
4037 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
4038 fc->last_pid_recv = (uint32_t) -1;
4039 fc->last_ack_sent = (uint32_t) 0;
4040 fc->last_ack_recv = (uint32_t) 0;
4041 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4042 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4044 fc->queue_max = (max_msgs_queue / max_connections) + 1;
4048 static struct MeshConnection *
4049 connection_new (const struct GNUNET_HashCode *cid)
4051 struct MeshConnection *c;
4053 c = GNUNET_new (struct MeshConnection);
4055 GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
4056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4057 fc_init (&c->fwd_fc);
4058 fc_init (&c->bck_fc);
4067 * Find a connection.
4069 * @param cid Connection ID.
4071 static struct MeshConnection *
4072 connection_get (const struct GNUNET_HashCode *cid)
4074 return GNUNET_CONTAINER_multihashmap_get (connections, cid);
4079 connection_destroy (struct MeshConnection *c)
4081 struct MeshPeer *peer;
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s[%X]\n",
4087 peer2s (c->t->peer),
4090 /* Cancel all traffic */
4091 connection_cancel_queues (c, GNUNET_YES);
4092 connection_cancel_queues (c, GNUNET_NO);
4094 /* Cancel maintainance task (keepalive/timeout) */
4095 if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
4096 GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
4097 if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
4098 GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
4100 /* Deregister from neighbors */
4101 peer = connection_get_next_hop (c);
4103 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4104 peer = connection_get_prev_hop (c);
4106 GNUNET_CONTAINER_multihashmap_remove (peer->connections, &c->id, c);
4109 GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
4110 GNUNET_CONTAINER_DLL_remove (c->t->connection_head, c->t->connection_tail, c);
4116 tunnel_destroy (struct MeshTunnel2 *t)
4118 struct MeshConnection *c;
4119 struct MeshConnection *next;
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
4127 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
4128 // GNUNET_break (0);
4130 for (c = t->connection_head; NULL != c; c = next)
4133 connection_destroy (c);
4136 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4143 * Tunnel is empty: destroy it.
4145 * Notifies all connections about the destruction.
4147 * @param t Tunnel to destroy.
4150 tunnel_destroy_empty (struct MeshTunnel2 *t)
4152 struct MeshConnection *c;
4154 for (c = t->connection_head; NULL != c; c = c->next)
4156 if (GNUNET_NO == c->destroy)
4157 connection_send_destroy (c);
4160 if (0 == t->pending_messages)
4163 t->destroy = GNUNET_YES;
4168 * Destroy tunnel if empty (no more channels).
4170 * @param t Tunnel to destroy if empty.
4173 tunnel_destroy_if_empty (struct MeshTunnel2 *t)
4175 if (NULL != t->channel_head)
4178 tunnel_destroy_empty (t);
4183 * Destroy a channel and free all resources.
4185 * @param ch Channel to destroy.
4188 channel_destroy (struct MeshChannel *ch)
4190 struct MeshClient *c;
4198 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
4209 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
4216 channel_rel_free_all (ch->fwd_rel);
4217 channel_rel_free_all (ch->bck_rel);
4219 GNUNET_CONTAINER_DLL_remove (ch->t->channel_head, ch->t->channel_tail, ch);
4220 GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
4226 * Create a new channel.
4228 * @param t Tunnel this channel is in.
4229 * @param owner Client that owns the channel, NULL for foreign channels.
4230 * @param lid_root Local ID for root client.
4232 * @return A new initialized channel. NULL on error.
4234 static struct MeshChannel *
4235 channel_new (struct MeshTunnel2 *t,
4236 struct MeshClient *owner, MESH_ChannelNumber lid_root)
4238 struct MeshChannel *ch;
4240 ch = GNUNET_new (struct MeshChannel);
4242 ch->lid_root = lid_root;
4244 ch->blocked_fwd = GNUNET_YES;
4245 ch->blocked_bck = GNUNET_YES;
4247 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, ch);
4249 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
4253 while (NULL != channel_get (t, t->next_chid))
4254 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4255 ch->gid = t->next_chid;
4256 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
4259 GNUNET_CONTAINER_multihashmap32_put (owner->own_channels, lid_root, ch,
4260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4263 channel_destroy (ch);
4264 GNUNET_SERVER_receive_done (owner->handle, GNUNET_SYSERR);
4274 * Set options in a channel, extracted from a bit flag field
4276 * @param ch Channel to set options to.
4277 * @param options Bit array in host byte order.
4280 channel_set_options (struct MeshChannel *ch, uint32_t options)
4282 ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
4283 GNUNET_YES : GNUNET_NO;
4284 ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
4285 GNUNET_YES : GNUNET_NO;
4290 * Iterator for deleting each channel whose client endpoint disconnected.
4292 * @param cls Closure (client that has disconnected).
4293 * @param key The local channel id (used to access the hashmap).
4294 * @param value The value stored at the key (channel to destroy).
4296 * @return GNUNET_OK, keep iterating.
4299 channel_destroy_iterator (void *cls,
4303 struct MeshChannel *ch = value;
4304 struct MeshClient *c = cls;
4305 struct MeshTunnel2 *t;
4307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4308 " Channel %X (%X / %X) destroy, due to client %u shutdown.\n",
4309 ch->gid, ch->lid_root, ch->lid_dest, c->id);
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
4323 channel_send_destroy (ch);
4324 channel_destroy (ch);
4325 tunnel_destroy_if_empty (t);
4332 * Remove client's ports from the global hashmap on disconnect.
4334 * @param cls Closure (unused).
4336 * @param value Client structure.
4338 * @return GNUNET_OK, keep iterating.
4341 client_release_ports (void *cls,
4347 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
4348 if (GNUNET_YES != res)
4351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4352 "Port %u by client %p was not registered.\n",
4360 * Timeout function due to lack of keepalive/traffic from the owner.
4361 * Destroys connection if called.
4363 * @param cls Closure (connection to destroy).
4364 * @param tc TaskContext.
4367 connection_fwd_timeout (void *cls,
4368 const struct GNUNET_SCHEDULER_TaskContext *tc)
4370 struct MeshConnection *c = cls;
4372 c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4373 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4376 "Connection %s[%X] FWD timed out. Destroying.\n",
4377 peer2s (c->t->peer),
4380 if (connection_is_origin (c, GNUNET_YES)) /* If local, leave. */
4383 connection_destroy (c);
4388 * Timeout function due to lack of keepalive/traffic from the destination.
4389 * Destroys connection if called.
4391 * @param cls Closure (connection to destroy).
4392 * @param tc TaskContext
4395 connection_bck_timeout (void *cls,
4396 const struct GNUNET_SCHEDULER_TaskContext *tc)
4398 struct MeshConnection *c = cls;
4400 c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4401 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4405 "Connection %s[%X] FWD timed out. Destroying.\n",
4406 peer2s (c->t->peer),
4409 if (connection_is_origin (c, GNUNET_NO)) /* If local, leave. */
4412 connection_destroy (c);
4417 * Resets the connection timeout task, some other message has done the
4419 * - For the first peer on the direction this means to send
4420 * a keepalive or a path confirmation message (either create or ACK).
4421 * - For all other peers, this means to destroy the connection,
4422 * due to lack of activity.
4423 * Starts the tiemout if no timeout was running (connection just created).
4425 * @param c Connection whose timeout to reset.
4426 * @param fwd Is this forward?
4428 * TODO use heap to improve efficiency of scheduler.
4431 connection_reset_timeout (struct MeshConnection *c, int fwd)
4433 GNUNET_SCHEDULER_TaskIdentifier *ti;
4434 GNUNET_SCHEDULER_Task f;
4436 ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
4438 if (GNUNET_SCHEDULER_NO_TASK != *ti)
4439 GNUNET_SCHEDULER_cancel (*ti);
4441 if (connection_is_origin (c, fwd)) /* Endpoint */
4443 f = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
4444 *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
4448 struct GNUNET_TIME_Relative delay;
4450 delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
4451 f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
4452 *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
4458 * Iterator to notify all connections of a broken link. Mark connections
4459 * to destroy after all traffic has been sent.
4461 * @param cls Closure (peer disconnected).
4462 * @param key Current key code (tid).
4463 * @param value Value in the hash map (connection).
4465 * @return GNUNET_YES if we should continue to iterate,
4469 connection_broken (void *cls,
4470 const struct GNUNET_HashCode *key,
4473 struct MeshPeer *peer = cls;
4474 struct MeshConnection *c = value;
4475 struct GNUNET_MESH_ConnectionBroken msg;
4478 fwd = peer == connection_get_prev_hop (c);
4479 connection_cancel_queues (c, !fwd);
4481 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
4482 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
4484 msg.peer1 = my_full_id;
4485 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
4486 send_prebuilt_message_connection (&msg.header, c, NULL, fwd);
4487 c->destroy = GNUNET_YES;
4492 /******************************************************************************/
4493 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4494 /******************************************************************************/
4497 * Free a transmission that was already queued with all resources
4498 * associated to the request.
4500 * @param queue Queue handler to cancel.
4501 * @param clear_cls Is it necessary to free associated cls?
4504 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4506 struct MeshPeer *peer;
4507 struct MeshFlowControl *fc;
4512 GNUNET_assert (NULL != queue->c);
4513 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
4515 if (GNUNET_YES == clear_cls)
4517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
4518 GNUNET_MESH_DEBUG_M2S (queue->type));
4519 switch (queue->type)
4521 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4522 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4523 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
4524 GNUNET_break (GNUNET_YES == queue->c->destroy);
4526 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4527 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4528 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4529 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4530 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4531 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
4533 GNUNET_free_non_null (queue->cls);
4538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
4539 GNUNET_MESH_DEBUG_M2S (queue->type));
4543 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
4547 if (NULL != queue->c)
4549 queue->c->pending_messages--;
4550 if (NULL != queue->c->t)
4552 queue->c->t->pending_messages--;
4556 GNUNET_free (queue);
4561 queue_send (void *cls, size_t size, void *buf)
4563 struct MeshPeer *peer = cls;
4564 struct MeshFlowControl *fc;
4565 struct MeshConnection *c;
4566 struct GNUNET_MessageHeader *msg;
4567 struct MeshPeerQueue *queue;
4568 struct MeshTunnel2 *t;
4569 const struct GNUNET_PeerIdentity *dst_id;
4575 peer->core_transmit = NULL;
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
4578 if (NULL == buf || 0 == size)
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
4585 queue = peer_get_first_message (peer);
4588 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
4593 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4596 dst_id = GNUNET_PEER_resolve2 (peer->id);
4597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
4598 /* Check if buffer size is enough for the message */
4599 if (queue->size > size)
4601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
4602 peer->core_transmit =
4603 GNUNET_CORE_notify_transmit_ready (core_handle,
4606 GNUNET_TIME_UNIT_FOREVER_REL,
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
4615 t = (NULL != c) ? c->t : NULL;
4619 switch (queue->type)
4621 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
4622 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
4623 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4624 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4625 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4626 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4629 GNUNET_MESH_DEBUG_M2S (queue->type));
4630 data_size = send_core_data_raw (queue->cls, size, buf);
4631 msg = (struct GNUNET_MessageHeader *) buf;
4632 type = ntohs (msg->type);
4634 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
4636 if (connection_is_origin (c, GNUNET_YES))
4637 data_size = send_core_connection_create (queue->c, size, buf);
4639 data_size = send_core_data_raw (queue->cls, size, buf);
4641 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
4643 if (connection_is_origin (c, GNUNET_NO))
4644 data_size = send_core_connection_ack (queue->c, size, buf);
4646 data_size = send_core_data_raw (queue->cls, size, buf);
4648 case GNUNET_MESSAGE_TYPE_MESH_DATA:
4649 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
4650 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
4651 /* This should be encapsulted */
4656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
4661 if (0 < drop_percent &&
4662 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
4664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4665 "Dropping message of type %s\n",
4666 GNUNET_MESH_DEBUG_M2S (queue->type));
4670 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
4673 case GNUNET_MESSAGE_TYPE_MESH_FWD:
4674 case GNUNET_MESSAGE_TYPE_MESH_BCK:
4675 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
4676 fc->last_pid_sent = pid;
4677 if (NULL != queue->ch)
4678 channel_send_client_ack (queue->ch, fwd);
4680 connection_send_ack (c, fwd);
4686 /* Free queue, but cls was freed by send_core_* */
4687 queue_destroy (queue, GNUNET_NO);
4689 /* If more data in queue, send next */
4690 queue = peer_get_first_message (peer);
4693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
4694 if (NULL == peer->core_transmit) {
4695 peer->core_transmit =
4696 GNUNET_CORE_notify_transmit_ready(core_handle,
4699 GNUNET_TIME_UNIT_FOREVER_REL,
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4708 "* tmt rdy called somewhere else\n");
4710 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4712 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "* %s starting poll timeout\n");
4714 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
4719 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
4721 GNUNET_SCHEDULER_cancel (fc->poll_task);
4722 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4727 c->pending_messages--;
4728 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
4731 connection_destroy (c);
4737 t->pending_messages--;
4738 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4740 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
4750 queue_add (void *cls, uint16_t type, size_t size,
4751 struct MeshConnection *c,
4752 struct MeshChannel *ch,
4755 struct MeshPeerQueue *queue;
4756 struct MeshFlowControl *fc;
4757 struct MeshPeer *peer;
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4761 "queue add %s %s (%u) on c %p, ch %p\n",
4762 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
4763 GNUNET_assert (NULL != c);
4765 fc = fwd ? &c->fwd_fc : &c->bck_fc;
4766 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
4776 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
4777 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
4779 fc->queue_n--; /* We are going to do queue_n++ later, compensate*/
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
4786 if (fc->queue_n >= fc->queue_max && 0 == priority)
4788 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
4791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4792 "queue full: %u/%u\n",
4793 fc->queue_n, fc->queue_max);
4794 return; /* Drop this message */
4797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", fc->last_pid_sent);
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ack %u\n", fc->last_ack_recv);
4799 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv) &&
4800 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
4802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4803 "no buffer space (%u > %u): starting poll\n",
4804 fc->last_pid_sent + 1, fc->last_ack_recv);
4805 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
4809 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4817 if (100 <= priority)
4818 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
4820 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
4822 if (NULL == peer->core_transmit)
4824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4825 "calling core tmt rdy towards %s for %u bytes\n",
4826 peer2s (peer), size);
4827 peer->core_transmit =
4828 GNUNET_CORE_notify_transmit_ready (core_handle,
4831 GNUNET_TIME_UNIT_FOREVER_REL,
4832 GNUNET_PEER_resolve2 (peer->id),
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4840 "core tmt rdy towards %s already called\n",
4844 c->pending_messages++;
4845 c->t->pending_messages++;
4851 /******************************************************************************/
4852 /******************** MESH NETWORK HANDLERS **************************/
4853 /******************************************************************************/
4857 * Generic handler for mesh network payload traffic.
4859 * @param t Tunnel on which we got this message.
4860 * @param message Unencryted data message.
4861 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4863 * @return GNUNET_OK to keep the connection open,
4864 * GNUNET_SYSERR to close it (signal serious error)
4867 handle_data (struct MeshTunnel2 *t, const struct GNUNET_MESH_Data *msg, int fwd)
4869 struct MeshChannelReliability *rel;
4870 struct MeshChannel *ch;
4871 struct MeshClient *c;
4878 size = ntohs (msg->header.size);
4880 sizeof (struct GNUNET_MESH_Data) +
4881 sizeof (struct GNUNET_MessageHeader))
4886 type = ntohs (msg->header.type);
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
4888 GNUNET_MESH_DEBUG_M2S (type));
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4890 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4893 ch = channel_get (t, ntohl (msg->chid));
4896 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1, GNUNET_NO);
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel unknown\n");
4901 /* Initialize FWD/BCK data */
4902 c = fwd ? ch->dest : ch->root;
4903 rel = fwd ? ch->bck_rel : ch->fwd_rel;
4904 mid_recv = fwd ? &ch->mid_recv_fwd : &ch->mid_recv_bck;
4912 tunnel_change_state (t, MESH_TUNNEL_READY);
4914 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4916 mid = ntohl (msg->mid);
4917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
4919 if (GNUNET_NO == ch->reliable ||
4920 ( !GMC_is_pid_bigger (*mid_recv, mid) &&
4921 GMC_is_pid_bigger (*mid_recv + 64, mid) ) )
4923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
4924 if (GNUNET_YES == ch->reliable)
4926 /* Is this the exact next expected messasge? */
4927 if (mid == *mid_recv)
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4930 *mid_recv = *mid_recv + 1;
4931 channel_send_client_data (ch, msg, fwd);
4932 channel_send_client_buffered_data (ch, c, rel);
4936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4937 channel_rel_add_buffered_data (msg, rel);
4940 else /* Tunnel unreliable, send to clients directly */
4942 channel_send_client_data (ch, msg, fwd);
4947 GNUNET_break_op (0);
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4949 " MID %u not expected (%u - %u), dropping!\n",
4950 mid, *mid_recv, *mid_recv + 64);
4953 channel_send_data_ack (ch, fwd);
4958 * Handler for mesh network traffic end-to-end ACKs.
4960 * @param t Tunnel on which we got this message.
4961 * @param message Data message.
4962 * @param fwd Is this a fwd ACK? (dest->orig)
4964 * @return GNUNET_OK to keep the connection open,
4965 * GNUNET_SYSERR to close it (signal serious error)
4968 handle_data_ack (struct MeshTunnel2 *t,
4969 const struct GNUNET_MESH_DataACK *msg, int fwd)
4971 struct MeshChannelReliability *rel;
4972 struct MeshReliableMessage *copy;
4973 struct MeshReliableMessage *next;
4974 struct MeshChannel *ch;
4979 type = ntohs (msg->header.type);
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message!\n",
4981 GNUNET_MESH_DEBUG_M2S (type));
4982 ch = channel_get (t, ntohl (msg->chid));
4985 GNUNET_STATISTICS_update (stats, "# ack on unknown channel", 1, GNUNET_NO);
4988 ack = ntohl (msg->mid);
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
4990 (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
4992 if (GNUNET_YES == fwd)
5005 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
5007 if (GMC_is_pid_bigger (copy->mid, ack))
5009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
5010 channel_rel_free_sent (rel, msg);
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
5016 rel_message_free (copy);
5018 /* ACK client if needed */
5019 // channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
5021 /* If some message was free'd, update the retransmission delay*/
5022 if (GNUNET_YES == work)
5024 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
5026 GNUNET_SCHEDULER_cancel (rel->retry_task);
5027 if (NULL == rel->head_sent)
5029 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
5033 struct GNUNET_TIME_Absolute new_target;
5034 struct GNUNET_TIME_Relative delay;
5036 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
5037 MESH_RETRANSMIT_MARGIN);
5038 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
5040 delay = GNUNET_TIME_absolute_get_remaining (new_target);
5042 GNUNET_SCHEDULER_add_delayed (delay,
5043 &channel_retransmit_message,
5055 * Core handler for connection creation.
5057 * @param cls Closure (unused).
5058 * @param peer Sender (neighbor).
5059 * @param message Message.
5061 * @return GNUNET_OK to keep the connection open,
5062 * GNUNET_SYSERR to close it (signal serious error)
5065 handle_mesh_connection_create (void *cls,
5066 const struct GNUNET_PeerIdentity *peer,
5067 const struct GNUNET_MessageHeader *message)
5069 struct GNUNET_MESH_ConnectionCreate *msg;
5070 struct GNUNET_PeerIdentity *id;
5071 struct GNUNET_HashCode *cid;
5072 struct MeshPeerPath *path;
5073 struct MeshPeer *dest_peer;
5074 struct MeshPeer *orig_peer;
5075 struct MeshConnection *c;
5076 unsigned int own_pos;
5080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
5083 size = ntohs (message->size);
5084 if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
5086 GNUNET_break_op (0);
5090 /* Calculate hops */
5091 size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
5092 if (size % sizeof (struct GNUNET_PeerIdentity))
5094 GNUNET_break_op (0);
5097 size /= sizeof (struct GNUNET_PeerIdentity);
5100 GNUNET_break_op (0);
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5105 /* Get parameters */
5106 msg = (struct GNUNET_MESH_ConnectionCreate *) message;
5108 id = (struct GNUNET_PeerIdentity *) &msg[1];
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5110 " connection %s (%s).\n",
5111 GNUNET_h2s (cid), GNUNET_i2s (id));
5113 /* Create connection */
5114 c = connection_get (cid);
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
5118 c = connection_new (cid);
5122 connection_reset_timeout (c, GNUNET_YES);
5123 tunnel_change_state (c->t, MESH_TUNNEL_WAITING);
5125 /* Remember peers */
5126 dest_peer = peer_get (&id[size - 1]);
5127 orig_peer = peer_get (&id[0]);
5130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5131 path = path_new (size);
5133 for (i = 0; i < size; i++)
5135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5136 GNUNET_i2s (&id[i]));
5137 path->peers[i] = GNUNET_PEER_intern (&id[i]);
5138 if (path->peers[i] == myid)
5141 if (own_pos == 0 && path->peers[own_pos] != myid)
5143 /* create path: self not found in path through self */
5144 GNUNET_break_op (0);
5145 path_destroy (path);
5146 connection_destroy (c);
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5150 path_add_to_peers (path, GNUNET_NO);
5151 c->path = path_duplicate (path);
5152 c->own_pos = own_pos;
5154 /* Is it a connection to us? */
5155 if (own_pos == size - 1)
5157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5158 peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
5160 if (NULL == orig_peer->tunnel)
5161 orig_peer->tunnel = tunnel_new ();
5162 tunnel_add_connection (orig_peer->tunnel, c);
5164 send_connection_ack (c, GNUNET_NO);
5166 /* Keep tunnel alive in direction dest->owner*/
5167 connection_reset_timeout (c, GNUNET_NO);
5171 /* It's for somebody else! Retransmit. */
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5173 peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
5174 peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
5175 send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
5182 * Core handler for path ACKs
5184 * @param cls closure
5185 * @param message message
5186 * @param peer peer identity this notification is about
5188 * @return GNUNET_OK to keep the connection open,
5189 * GNUNET_SYSERR to close it (signal serious error)
5192 handle_mesh_connection_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5193 const struct GNUNET_MessageHeader *message)
5195 struct GNUNET_MESH_ConnectionACK *msg;
5196 struct MeshPeerPath *p;
5197 struct MeshConnection *c;
5199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
5200 msg = (struct GNUNET_MESH_ConnectionACK *) message;
5201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n",
5202 GNUNET_h2s (&msg->cid));
5203 c = connection_get (&msg->cid);
5206 GNUNET_STATISTICS_update (stats, "# control on unknown connection",
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the connection!\n");
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5215 /* Add path to peers? */
5219 path_add_to_peers (p, GNUNET_YES);
5225 connection_change_state (c, MESH_CONNECTION_READY);
5226 connection_reset_timeout (c, GNUNET_NO);
5227 if (MESH_TUNNEL_READY != c->t->state)
5228 tunnel_change_state (c->t, MESH_TUNNEL_READY);
5229 tunnel_send_queued_data (c->t, GNUNET_YES);
5231 /* Message for us? */
5232 if (connection_is_terminal (c, GNUNET_NO))
5234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
5235 if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
5237 GNUNET_DHT_get_stop (c->t->peer->dhtget);
5238 c->t->peer->dhtget = NULL;
5240 //connection_send_ack (c, GNUNET_NO); /* FIXME */
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5245 send_prebuilt_message_connection (message, c, NULL, GNUNET_NO);
5251 * Core handler for notifications of broken paths
5253 * @param cls Closure (unused).
5254 * @param peer Peer identity of sending neighbor.
5255 * @param message Message.
5257 * @return GNUNET_OK to keep the connection open,
5258 * GNUNET_SYSERR to close it (signal serious error)
5261 handle_mesh_connection_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5262 const struct GNUNET_MessageHeader *message)
5264 struct GNUNET_MESH_ConnectionBroken *msg;
5265 struct MeshConnection *c;
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5268 "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
5269 msg = (struct GNUNET_MESH_ConnectionBroken *) message;
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5271 GNUNET_i2s (&msg->peer1));
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5273 GNUNET_i2s (&msg->peer2));
5274 c = connection_get (&msg->cid);
5277 GNUNET_break_op (0);
5280 tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
5281 GNUNET_PEER_search (&msg->peer2));
5288 * Core handler for tunnel destruction
5290 * @param cls Closure (unused).
5291 * @param peer Peer identity of sending neighbor.
5292 * @param message Message.
5294 * @return GNUNET_OK to keep the connection open,
5295 * GNUNET_SYSERR to close it (signal serious error)
5298 handle_mesh_connection_destroy (void *cls,
5299 const struct GNUNET_PeerIdentity *peer,
5300 const struct GNUNET_MessageHeader *message)
5302 struct GNUNET_MESH_ConnectionDestroy *msg;
5303 struct MeshConnection *c;
5307 msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5309 "Got a CONNECTION DESTROY message from %s\n",
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5312 " for connection %s\n",
5313 GNUNET_h2s (&msg->cid));
5314 c = connection_get (&msg->cid);
5317 /* Probably already got the message from another path,
5318 * destroyed the tunnel and retransmitted to children.
5321 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5325 id = GNUNET_PEER_search (peer);
5326 if (id == connection_get_prev_hop (c)->id)
5328 else if (id == connection_get_next_hop (c)->id)
5332 GNUNET_break_op (0);
5335 send_prebuilt_message_connection (message, c, NULL, fwd);
5336 c->destroy = GNUNET_YES;
5343 * Handler for channel create messages.
5345 * @param t Tunnel this channel is to be created in.
5346 * @param msg Message.
5347 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5349 * @return GNUNET_OK to keep the connection open,
5350 * GNUNET_SYSERR to close it (signal serious error)
5353 handle_channel_create (struct MeshTunnel2 *t,
5354 struct GNUNET_MESH_ChannelCreate *msg,
5357 MESH_ChannelNumber chid;
5358 struct MeshChannel *ch;
5359 struct MeshClient *c;
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel Create\n");
5363 /* Check message size */
5364 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelCreate))
5366 GNUNET_break_op (0);
5370 /* Check if channel exists */
5371 chid = ntohl (msg->chid);
5372 ch = channel_get (t, chid);
5375 /* Probably a retransmission, safe to ignore */
5376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
5380 /* Create channel */
5381 ch = channel_new (t, NULL, 0);
5382 channel_set_options (ch, ntohl (msg->opt));
5385 /* Find a destination client */
5386 port = ntohl (msg->port);
5387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", port);
5388 c = GNUNET_CONTAINER_multihashmap32_get (ports, port);
5391 /* TODO send reject */
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n");
5396 channel_add_client (ch, c);
5397 if (GNUNET_YES == ch->reliable)
5399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5400 ch->bck_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
5401 ch->bck_rel->ch = ch;
5402 ch->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
5405 send_local_channel_create (ch);
5406 channel_send_ack (ch, !fwd);
5413 * Handler for channel ack messages.
5415 * @param t Tunnel this channel is to be created in.
5416 * @param msg Message.
5417 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5419 * @return GNUNET_OK to keep the connection open,
5420 * GNUNET_SYSERR to close it (signal serious error)
5423 handle_channel_ack (struct MeshTunnel2 *t,
5424 struct GNUNET_MESH_ChannelManage *msg,
5427 MESH_ChannelNumber chid;
5428 struct MeshChannel *ch;
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Channel ACK\n");
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 GNUNET_break_op (0);
5444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " channel %u unknown!!\n", chid);
5448 channel_confirm (ch, !fwd);
5454 * Handler for channel destroy messages.
5456 * @param t Tunnel this channel is to be destroyed of.
5457 * @param msg Message.
5458 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5460 * @return GNUNET_OK to keep the connection open,
5461 * GNUNET_SYSERR to close it (signal serious error)
5464 handle_channel_destroy (struct MeshTunnel2 *t,
5465 struct GNUNET_MESH_ChannelManage *msg,
5468 MESH_ChannelNumber chid;
5469 struct MeshChannel *ch;
5471 /* Check message size */
5472 if (ntohs (msg->header.size) != sizeof (struct GNUNET_MESH_ChannelManage))
5474 GNUNET_break_op (0);
5478 /* Check if channel exists */
5479 chid = ntohl (msg->chid);
5480 ch = channel_get (t, chid);
5483 /* Probably a retransmission, safe to ignore */
5487 send_local_channel_destroy (ch, fwd);
5488 channel_destroy (ch);
5495 * Generic handler for mesh network encrypted traffic.
5497 * @param peer Peer identity this notification is about.
5498 * @param message Encrypted message.
5499 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
5501 * @return GNUNET_OK to keep the connection open,
5502 * GNUNET_SYSERR to close it (signal serious error)
5505 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
5506 const struct GNUNET_MESH_Encrypted *msg,
5509 struct MeshConnection *c;
5510 struct MeshTunnel2 *t;
5511 struct MeshPeer *neighbor;
5512 struct MeshFlowControl *fc;
5519 size = ntohs (msg->header.size);
5521 sizeof (struct GNUNET_MESH_Encrypted) +
5522 sizeof (struct GNUNET_MessageHeader))
5527 type = ntohs (msg->header.type);
5528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
5529 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
5531 /* Check connection */
5532 c = connection_get (&msg->cid);
5535 GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
5536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
5540 fc = fwd ? &c->fwd_fc : &c->bck_fc;
5542 /* Check if origin is as expected */
5543 neighbor = connection_get_hop (c, fwd);
5544 if (peer_get (peer)->id != neighbor->id)
5546 GNUNET_break_op (0);
5551 pid = ntohl (msg->pid);
5552 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
5554 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5556 "WARNING Received PID %u, (prev %u), ACK %u\n",
5557 pid, fc->last_pid_recv, fc->last_ack_sent);
5560 if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
5562 GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
5563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5564 " Pid %u not expected (%u+), dropping!\n",
5565 pid, fc->last_pid_recv + 1);
5568 if (MESH_CONNECTION_SENT == c->state)
5569 connection_change_state (c, MESH_CONNECTION_READY);
5570 connection_reset_timeout (c, fwd);
5571 fc->last_pid_recv = pid;
5573 /* Is this message for us? */
5574 if (connection_is_terminal (c, fwd))
5576 size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
5578 struct GNUNET_MessageHeader *msgh;
5581 /* TODO signature verification */
5582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
5583 GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
5585 fc->last_pid_recv = pid;
5586 tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
5587 msgh = (struct GNUNET_MessageHeader *) cbuf;
5588 switch (ntohs (msgh->type))
5590 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
5591 r = handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
5594 case GNUNET_MESSAGE_TYPE_MESH_DATA:
5595 r = handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
5598 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
5599 r = handle_channel_create (t,
5600 (struct GNUNET_MESH_ChannelCreate *) msgh,
5604 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
5605 r = handle_channel_ack (t,
5606 (struct GNUNET_MESH_ChannelManage *) msgh,
5610 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
5611 r = handle_channel_destroy (t,
5612 (struct GNUNET_MESH_ChannelManage *) msgh,
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5618 "end-to-end message not known (%u)\n",
5619 ntohs (msgh->type));
5623 connection_send_ack (c, fwd);
5627 /* Message not for us: forward to next hop */
5628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
5629 ttl = ntohl (msg->ttl);
5630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5633 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5635 connection_send_ack (c, fwd);
5638 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
5640 send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
5647 * Core handler for mesh network traffic going orig->dest.
5649 * @param cls Closure (unused).
5650 * @param message Message received.
5651 * @param peer Peer who sent the message.
5653 * @return GNUNET_OK to keep the connection open,
5654 * GNUNET_SYSERR to close it (signal serious error)
5657 handle_mesh_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
5658 const struct GNUNET_MessageHeader *message)
5660 return handle_mesh_encrypted (peer,
5661 (struct GNUNET_MESH_Encrypted *)message,
5666 * Core handler for mesh network traffic going dest->orig.
5668 * @param cls Closure (unused).
5669 * @param message Message received.
5670 * @param peer Peer who sent the message.
5672 * @return GNUNET_OK to keep the connection open,
5673 * GNUNET_SYSERR to close it (signal serious error)
5676 handle_mesh_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
5677 const struct GNUNET_MessageHeader *message)
5679 return handle_mesh_encrypted (peer,
5680 (struct GNUNET_MESH_Encrypted *)message,
5686 * Core handler for mesh network traffic point-to-point acks.
5688 * @param cls closure
5689 * @param message message
5690 * @param peer peer identity this notification is about
5692 * @return GNUNET_OK to keep the connection open,
5693 * GNUNET_SYSERR to close it (signal serious error)
5696 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5697 const struct GNUNET_MessageHeader *message)
5699 struct GNUNET_MESH_ACK *msg;
5700 struct MeshConnection *c;
5701 struct MeshFlowControl *fc;
5705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5707 msg = (struct GNUNET_MESH_ACK *) message;
5709 c = connection_get (&msg->cid);
5713 GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
5718 ack = ntohl (msg->ack);
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5721 /* Is this a forward or backward ACK? */
5722 id = GNUNET_PEER_search (peer);
5723 if (connection_get_next_hop (c)->id == id)
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5728 else if (connection_get_prev_hop (c)->id == id)
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5735 GNUNET_break_op (0);
5739 /* Cancel polling if the ACK is bigger than before. */
5740 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
5741 GMC_is_pid_bigger (ack, fc->last_ack_recv))
5743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
5744 GNUNET_SCHEDULER_cancel (fc->poll_task);
5745 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
5746 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
5749 fc->last_ack_recv = ack;
5750 connection_unlock_queue (c, fc == &c->fwd_fc);
5757 * Core handler for mesh network traffic point-to-point ack polls.
5759 * @param cls closure
5760 * @param message message
5761 * @param peer peer identity this notification is about
5763 * @return GNUNET_OK to keep the connection open,
5764 * GNUNET_SYSERR to close it (signal serious error)
5767 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5768 const struct GNUNET_MessageHeader *message)
5770 struct GNUNET_MESH_Poll *msg;
5771 struct MeshConnection *c;
5772 struct MeshFlowControl *fc;
5776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
5779 msg = (struct GNUNET_MESH_Poll *) message;
5781 c = connection_get (&msg->cid);
5785 GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
5787 GNUNET_break_op (0);
5791 /* Is this a forward or backward ACK? */
5792 id = GNUNET_PEER_search (peer);
5793 if (connection_get_next_hop (c)->id == id)
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5798 else if (connection_get_prev_hop (c)->id == id)
5800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5805 GNUNET_break_op (0);
5809 pid = ntohl (msg->pid);
5810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n",
5811 pid, fc->last_pid_recv);
5812 fc->last_pid_recv = pid;
5813 connection_send_ack (c, fc == &c->fwd_fc);
5820 * Core handler for mesh keepalives.
5822 * @param cls closure
5823 * @param message message
5824 * @param peer peer identity this notification is about
5825 * @return GNUNET_OK to keep the connection open,
5826 * GNUNET_SYSERR to close it (signal serious error)
5828 * TODO: Check who we got this from, to validate route.
5831 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5832 const struct GNUNET_MessageHeader *message)
5834 struct GNUNET_MESH_ConnectionKeepAlive *msg;
5835 struct MeshConnection *c;
5836 struct MeshPeer *neighbor;
5839 msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
5840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5843 c = connection_get (&msg->cid);
5846 GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
5851 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
5852 GNUNET_YES : GNUNET_NO;
5854 /* Check if origin is as expected */
5855 neighbor = connection_get_hop (c, fwd);
5856 if (peer_get (peer)->id != neighbor->id)
5858 GNUNET_break_op (0);
5862 connection_change_state (c, MESH_CONNECTION_READY);
5863 connection_reset_timeout (c, fwd);
5865 if (connection_is_terminal (c, fwd))
5868 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5869 send_prebuilt_message_connection (message, c, NULL, fwd);
5877 * Functions to handle messages from core
5879 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5880 {&handle_mesh_connection_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
5882 {&handle_mesh_connection_ack, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
5883 sizeof (struct GNUNET_MESH_ConnectionACK)},
5884 {&handle_mesh_connection_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
5885 sizeof (struct GNUNET_MESH_ConnectionBroken)},
5886 {&handle_mesh_connection_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
5887 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
5888 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
5889 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5890 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
5891 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
5892 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5893 sizeof (struct GNUNET_MESH_ACK)},
5894 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5895 sizeof (struct GNUNET_MESH_Poll)},
5896 {&handle_mesh_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
5897 {&handle_mesh_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
5903 * Function to process paths received for a new peer addition. The recorded
5904 * paths form the initial tunnel, which can be optimized later.
5905 * Called on each result obtained for the DHT search.
5907 * @param cls closure
5908 * @param exp when will this value expire
5909 * @param key key of the result
5910 * @param get_path path of the get request
5911 * @param get_path_length lenght of get_path
5912 * @param put_path path of the put request
5913 * @param put_path_length length of the put_path
5914 * @param type type of the result
5915 * @param size number of bytes in data
5916 * @param data pointer to the result data
5919 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5920 const struct GNUNET_HashCode * key,
5921 const struct GNUNET_PeerIdentity *get_path,
5922 unsigned int get_path_length,
5923 const struct GNUNET_PeerIdentity *put_path,
5924 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5925 size_t size, const void *data)
5927 struct MeshPeer *peer = cls;
5928 struct MeshPeerPath *p;
5929 struct MeshConnection *c;
5930 struct GNUNET_PeerIdentity pi;
5933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5934 GNUNET_PEER_resolve (peer->id, &pi);
5935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5937 p = path_build_from_dht (get_path, get_path_length,
5938 put_path, put_path_length);
5939 path_add_to_peers (p, GNUNET_NO);
5942 /* Count connections */
5943 for (c = peer->tunnel->connection_head, i = 0; NULL != c; c = c->next, i++);
5945 /* If we already have 3 (or more (?!)) connections, it's enough */
5949 if (peer->tunnel->state == MESH_TUNNEL_SEARCHING)
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
5952 peer_connect (peer);
5958 /******************************************************************************/
5959 /********************* MESH LOCAL HANDLES **************************/
5960 /******************************************************************************/
5964 * Handler for client connection.
5966 * @param cls Closure (unused).
5967 * @param client Client handler.
5970 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
5972 struct MeshClient *c;
5976 c = GNUNET_malloc (sizeof (struct MeshClient));
5978 c->id = next_client_id++; /* overflow not important: just for debug */
5979 GNUNET_SERVER_client_keep (client);
5980 GNUNET_SERVER_client_set_user_context (client, c);
5981 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5986 * Handler for client disconnection
5988 * @param cls closure
5989 * @param client identification of the client; NULL
5990 * for the last call when the server is destroyed
5993 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5995 struct MeshClient *c;
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
6000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6004 c = client_get (client);
6007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
6009 GNUNET_SERVER_client_drop (c->handle);
6010 c->shutting_down = GNUNET_YES;
6011 if (NULL != c->own_channels)
6013 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
6014 &channel_destroy_iterator, c);
6015 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
6018 if (NULL != c->incoming_channels)
6020 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
6021 &channel_destroy_iterator, c);
6022 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
6025 if (NULL != c->ports)
6027 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
6028 &client_release_ports, c);
6029 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
6031 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
6032 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
6038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
6040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
6046 * Handler for new clients
6048 * @param cls closure
6049 * @param client identification of the client
6050 * @param message the actual message, which includes messages the client wants
6053 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6054 const struct GNUNET_MessageHeader *message)
6056 struct GNUNET_MESH_ClientConnect *cc_msg;
6057 struct MeshClient *c;
6062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
6064 /* Check data sanity */
6065 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6066 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6067 if (0 != (size % sizeof (uint32_t)))
6070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6073 size /= sizeof (uint32_t);
6075 /* Initialize new client structure */
6076 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
6077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
6083 p = (uint32_t *) &cc_msg[1];
6084 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
6085 for (i = 0; i < size; i++)
6088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
6090 /* store in client's hashmap */
6091 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
6092 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6093 /* store in global hashmap */
6094 /* FIXME only allow one client to have the port open,
6095 * have a backup hashmap with waiting clients */
6096 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
6097 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6101 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6102 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
6103 GNUNET_SERVER_notification_context_add (nc, client);
6104 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6106 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6112 * Handler for requests of new tunnels
6114 * @param cls Closure.
6115 * @param client Identification of the client.
6116 * @param message The actual message.
6119 handle_local_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
6120 const struct GNUNET_MessageHeader *message)
6122 struct GNUNET_MESH_ChannelMessage *msg;
6123 struct MeshPeer *peer;
6124 struct MeshTunnel2 *t;
6125 struct MeshChannel *ch;
6126 struct MeshClient *c;
6127 MESH_ChannelNumber chid;
6129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
6131 /* Sanity check for client registration */
6132 if (NULL == (c = client_get (client)))
6135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6140 /* Message size sanity check */
6141 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6148 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
6150 GNUNET_i2s (&msg->peer), ntohl (msg->port));
6151 chid = ntohl (msg->channel_id);
6153 /* Sanity check for duplicate channel IDs */
6154 if (NULL != channel_get_by_local_id (c, chid))
6157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6161 peer = peer_get (&msg->peer);
6162 if (NULL == peer->tunnel)
6164 peer->tunnel = tunnel_new ();
6165 peer->tunnel->peer = peer;
6169 /* Create channel */
6170 ch = channel_new (t, c, chid);
6174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6177 ch->port = ntohl (msg->port);
6178 channel_set_options (ch, ntohl (msg->opt));
6180 /* In unreliable channels, we'll use the DLL to buffer data for the root */
6181 ch->fwd_rel = GNUNET_malloc (sizeof (struct MeshChannelReliability));
6182 ch->fwd_rel->ch = ch;
6183 ch->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
6185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
6186 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
6187 peer_connect (peer);
6189 /* Send create channel */
6191 struct GNUNET_MESH_ChannelCreate msgcc;
6193 msgcc.header.size = htons (sizeof (msgcc));
6194 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
6195 msgcc.chid = htonl (ch->gid);
6196 msgcc.port = msg->port;
6197 msgcc.opt = msg->opt;
6199 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
6202 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6208 * Handler for requests of deleting tunnels
6210 * @param cls closure
6211 * @param client identification of the client
6212 * @param message the actual message
6215 handle_local_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6216 const struct GNUNET_MessageHeader *message)
6218 struct GNUNET_MESH_ChannelMessage *msg;
6219 struct MeshClient *c;
6220 struct MeshChannel *ch;
6221 struct MeshTunnel2 *t;
6222 MESH_ChannelNumber chid;
6224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6225 "Got a DESTROY CHANNEL from client!\n");
6227 /* Sanity check for client registration */
6228 if (NULL == (c = client_get (client)))
6231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6236 /* Message sanity check */
6237 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
6240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6244 msg = (struct GNUNET_MESH_ChannelMessage *) message;
6246 /* Retrieve tunnel */
6247 chid = ntohl (msg->channel_id);
6248 ch = channel_get_by_local_id (c, chid);
6251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
6253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6257 /* Cleanup after the tunnel */
6258 client_delete_channel (c, ch);
6259 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
6263 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
6269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6270 " channel %X client %p (%p, %p)\n",
6271 chid, c, ch->root, ch->dest);
6276 channel_destroy (ch);
6277 tunnel_destroy_if_empty (t);
6279 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6285 * Handler for client traffic
6287 * @param cls closure
6288 * @param client identification of the client
6289 * @param message the actual message
6292 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
6293 const struct GNUNET_MessageHeader *message)
6295 struct GNUNET_MESH_LocalData *msg;
6296 struct MeshClient *c;
6297 struct MeshChannel *ch;
6298 MESH_ChannelNumber chid;
6302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6303 "Got data from a client!\n");
6305 /* Sanity check for client registration */
6306 if (NULL == (c = client_get (client)))
6309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6314 msg = (struct GNUNET_MESH_LocalData *) message;
6316 /* Sanity check for message size */
6317 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
6318 if (size < sizeof (struct GNUNET_MessageHeader))
6321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6325 /* Channel exists? */
6326 chid = ntohl (msg->id);
6327 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6328 ch = channel_get_by_local_id (c, chid);
6332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6336 /* Is the client in the channel? */
6339 ch->root->handle == client)
6343 ch->dest->handle == client) ) )
6346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6351 ch->blocked_fwd = GNUNET_YES;
6353 ch->blocked_bck = GNUNET_YES;
6355 /* Ok, everything is correct, send the message. */
6357 struct GNUNET_MESH_Data *payload;
6358 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
6359 unsigned char cbuf[p2p_size];
6362 mid = fwd ? &ch->mid_send_fwd : &ch->mid_send_bck;
6363 payload = (struct GNUNET_MESH_Data *) cbuf;
6364 payload->mid = htonl (*mid);
6366 memcpy (&payload[1], &msg[1], size);
6367 payload->header.size = htons (p2p_size);
6368 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
6369 payload->chid = htonl (ch->gid);
6370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
6371 send_prebuilt_message_channel (&payload->header, ch, fwd);
6373 if (GNUNET_YES == ch->reliable)
6374 channel_save_copy (ch, &payload->header, fwd);
6376 if (tunnel_get_buffer (ch->t, fwd) > 0)
6377 send_local_ack (ch, c, fwd);
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6386 * Handler for client's ACKs for payload traffic.
6388 * @param cls Closure (unused).
6389 * @param client Identification of the client.
6390 * @param message The actual message.
6393 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6394 const struct GNUNET_MessageHeader *message)
6396 struct GNUNET_MESH_LocalAck *msg;
6397 struct MeshChannelReliability *rel;
6398 struct MeshChannel *ch;
6399 struct MeshClient *c;
6400 MESH_ChannelNumber chid;
6403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6405 /* Sanity check for client registration */
6406 if (NULL == (c = client_get (client)))
6409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6414 msg = (struct GNUNET_MESH_LocalAck *) message;
6416 /* Channel exists? */
6417 chid = ntohl (msg->channel_id);
6418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
6419 ch = channel_get_by_local_id (c, chid);
6423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
6424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6429 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
6430 rel = fwd ? ch->fwd_rel : ch->bck_rel;
6432 rel->client_ready = GNUNET_YES;
6433 channel_send_client_buffered_data (ch, c, rel);
6434 channel_send_connection_ack (ch, 64 - rel->n_recv, fwd);
6436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6443 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6445 * @param cls Closure (client handle).
6446 * @param key Key (hashed tunnel ID, unused).
6447 * @param value Tunnel info.
6449 * @return GNUNET_YES, to keep iterating.
6452 // monitor_all_tunnels_iterator (void *cls,
6453 // const struct GNUNET_HashCode * key,
6456 // struct GNUNET_SERVER_Client *client = cls;
6457 // struct MeshChannel *ch = value;
6458 // struct GNUNET_MESH_LocalMonitor *msg;
6460 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6461 // msg->channel_id = htonl (ch->gid);
6462 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6463 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6465 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6466 // "* sending info about tunnel %s\n",
6467 // GNUNET_i2s (&msg->owner));
6469 // GNUNET_SERVER_notification_context_unicast (nc, client,
6470 // &msg->header, GNUNET_NO);
6471 // return GNUNET_YES;
6476 * Handler for client's MONITOR request.
6478 * @param cls Closure (unused).
6479 * @param client Identification of the client.
6480 * @param message The actual message.
6483 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6484 const struct GNUNET_MessageHeader *message)
6486 struct MeshClient *c;
6488 /* Sanity check for client registration */
6489 if (NULL == (c = client_get (client)))
6492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6496 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6497 "Received get tunnels request from client %u\n",
6499 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6500 // monitor_all_tunnels_iterator,
6502 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6503 "Get tunnels request from client %u completed\n",
6505 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6510 * Handler for client's MONITOR_TUNNEL request.
6512 * @param cls Closure (unused).
6513 * @param client Identification of the client.
6514 * @param message The actual message.
6517 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6518 const struct GNUNET_MessageHeader *message)
6520 const struct GNUNET_MESH_LocalMonitor *msg;
6521 struct GNUNET_MESH_LocalMonitor *resp;
6522 struct MeshClient *c;
6523 struct MeshChannel *ch;
6525 /* Sanity check for client registration */
6526 if (NULL == (c = client_get (client)))
6529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6533 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6535 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6538 ntohl (msg->channel_id));
6539 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
6543 /* We don't know the tunnel */
6544 struct GNUNET_MESH_LocalMonitor warn;
6547 GNUNET_SERVER_notification_context_unicast (nc, client,
6550 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6554 /* Initialize context */
6555 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6557 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6558 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6559 &resp->header, GNUNET_NO);
6562 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6563 "Monitor tunnel request from client %u completed\n",
6565 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6570 * Functions to handle messages from clients
6572 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6573 {&handle_local_new_client, NULL,
6574 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6575 {&handle_local_channel_create, NULL,
6576 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
6577 sizeof (struct GNUNET_MESH_ChannelMessage)},
6578 {&handle_local_channel_destroy, NULL,
6579 GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
6580 sizeof (struct GNUNET_MESH_ChannelMessage)},
6581 {&handle_local_data, NULL,
6582 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
6583 {&handle_local_ack, NULL,
6584 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6585 sizeof (struct GNUNET_MESH_LocalAck)},
6586 {&handle_local_get_tunnels, NULL,
6587 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6588 sizeof (struct GNUNET_MessageHeader)},
6589 {&handle_local_show_tunnel, NULL,
6590 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6591 sizeof (struct GNUNET_MESH_LocalMonitor)},
6597 * Method called whenever a given peer connects.
6599 * @param cls closure
6600 * @param peer peer identity this notification is about
6603 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6605 struct MeshPeer *pi;
6606 struct MeshPeerPath *path;
6608 DEBUG_CONN ("Peer connected\n");
6609 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6610 pi = peer_get (peer);
6613 DEBUG_CONN (" (self)\n");
6614 path = path_new (1);
6618 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6619 path = path_new (2);
6620 path->peers[1] = pi->id;
6621 GNUNET_PEER_change_rc (pi->id, 1);
6622 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6624 path->peers[0] = myid;
6625 GNUNET_PEER_change_rc (myid, 1);
6626 peer_add_path (pi, path, GNUNET_YES);
6628 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6634 * Method called whenever a peer disconnects.
6636 * @param cls closure
6637 * @param peer peer identity this notification is about
6640 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6642 struct MeshPeer *pi;
6644 DEBUG_CONN ("Peer disconnected\n");
6645 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6652 peer_remove_path (pi, myid, pi->id);
6654 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
6657 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
6658 pi->connections = NULL;
6661 DEBUG_CONN (" (self)\n");
6663 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6670 * Install server (service) handlers and start listening to clients.
6675 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6676 GNUNET_SERVER_connect_notify (server_handle,
6677 &handle_local_client_connect, NULL);
6678 GNUNET_SERVER_disconnect_notify (server_handle,
6679 &handle_local_client_disconnect, NULL);
6680 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6682 clients_head = NULL;
6683 clients_tail = NULL;
6685 GNUNET_SERVER_resume (server_handle);
6690 * To be called on core init/fail.
6692 * @param cls Closure (config)
6693 * @param identity the public identity of this peer
6696 core_init (void *cls,
6697 const struct GNUNET_PeerIdentity *identity)
6699 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6703 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
6705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6708 GNUNET_i2s (identity));
6709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6711 GNUNET_i2s (&my_full_id));
6712 GNUNET_CORE_disconnect (core_handle);
6713 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6714 NULL, /* Closure passed to MESH functions */
6715 &core_init, /* Call core_init once connected */
6716 &core_connect, /* Handle connects */
6717 &core_disconnect, /* remove peers on disconnects */
6718 NULL, /* Don't notify about all incoming messages */
6719 GNUNET_NO, /* For header only in notification */
6720 NULL, /* Don't notify about all outbound messages */
6721 GNUNET_NO, /* For header-only out notification */
6722 core_handlers); /* Register these handlers */
6731 /******************************************************************************/
6732 /************************ MAIN FUNCTIONS ****************************/
6733 /******************************************************************************/
6736 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6738 * @param cls closure
6739 * @param key current key code
6740 * @param value value in the hash map
6741 * @return GNUNET_YES if we should continue to iterate,
6745 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6747 struct MeshPeer *p = value;
6748 struct MeshTunnel2 *t = p->tunnel;
6757 * Task run during shutdown.
6763 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6767 if (core_handle != NULL)
6769 GNUNET_CORE_disconnect (core_handle);
6772 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_tunnel, NULL);
6773 if (dht_handle != NULL)
6775 GNUNET_DHT_disconnect (dht_handle);
6780 GNUNET_SERVER_notification_context_destroy (nc);
6783 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6785 GNUNET_SCHEDULER_cancel (announce_id_task);
6786 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6793 * Process mesh requests.
6795 * @param cls closure
6796 * @param server the initialized server
6797 * @param c configuration to use
6800 run (void *cls, struct GNUNET_SERVER_Handle *server,
6801 const struct GNUNET_CONFIGURATION_Handle *c)
6804 struct GNUNET_CRYPTO_EccPrivateKey *pk;
6806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6807 server_handle = server;
6808 GNUNET_SERVER_suspend (server_handle);
6811 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6816 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6817 "mesh", "peer/privatekey");
6818 GNUNET_SCHEDULER_shutdown ();
6823 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
6824 &refresh_connection_time))
6826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6828 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6829 "mesh", "refresh path time");
6830 GNUNET_SCHEDULER_shutdown ();
6835 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6840 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6841 "mesh", "id announce time");
6842 GNUNET_SCHEDULER_shutdown ();
6847 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6852 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6853 "mesh", "connect timeout");
6854 GNUNET_SCHEDULER_shutdown ();
6859 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6864 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6865 "mesh", "max msgs queue");
6866 GNUNET_SCHEDULER_shutdown ();
6871 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
6874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6876 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6877 "mesh", "max tunnels");
6878 GNUNET_SCHEDULER_shutdown ();
6883 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6888 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6889 "mesh", "default ttl", 64);
6894 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6897 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6898 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6899 "mesh", "max peers", 1000);
6904 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
6911 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6912 "Mesh is running with drop mode enabled. "
6913 "This is NOT a good idea! "
6914 "Remove the DROP_PERCENT option from your configuration.\n");
6918 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6919 &dht_replication_level))
6921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6923 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6924 "mesh", "dht replication level", 3);
6925 dht_replication_level = 3;
6928 connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
6929 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6930 ports = GNUNET_CONTAINER_multihashmap32_create (32);
6932 dht_handle = GNUNET_DHT_connect (c, 64);
6933 if (NULL == dht_handle)
6937 stats = GNUNET_STATISTICS_create ("mesh", c);
6939 /* Scheduled the task to clean up when shutdown is called */
6940 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6942 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
6943 GNUNET_free (keyfile);
6944 GNUNET_assert (NULL != pk);
6945 my_private_key = pk;
6946 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6947 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6948 &my_full_id.hashPubKey);
6949 myid = GNUNET_PEER_intern (&my_full_id);
6950 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6951 "Mesh for peer [%s] starting\n",
6952 GNUNET_i2s(&my_full_id));
6954 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6955 NULL, /* Closure passed to MESH functions */
6956 &core_init, /* Call core_init once connected */
6957 &core_connect, /* Handle connects */
6958 &core_disconnect, /* remove peers on disconnects */
6959 NULL, /* Don't notify about all incoming messages */
6960 GNUNET_NO, /* For header only in notification */
6961 NULL, /* Don't notify about all outbound messages */
6962 GNUNET_NO, /* For header-only out notification */
6963 core_handlers); /* Register these handlers */
6964 if (NULL == core_handle)
6967 GNUNET_SCHEDULER_shutdown ();
6970 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6976 * The main function for the mesh service.
6978 * @param argc number of arguments from the command line
6979 * @param argv command line arguments
6980 * @return 0 ok, 1 on error
6983 main (int argc, char *const *argv)
6988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6989 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6991 ret = (GNUNET_OK == r) ? 0 : 1;
6992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");