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.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - keep queues until receiving ACK
30 * - relay corking down to core
31 * - set ttl relative to path length
39 #include "mesh_protocol.h"
40 #include "mesh_path.h"
41 #include "block_mesh.h"
42 #include "gnunet_dht_service.h"
43 #include "gnunet_statistics_service.h"
45 #define MESH_BLOOM_SIZE 128
47 #define MESH_DEBUG_DHT GNUNET_NO
48 #define MESH_DEBUG_CONNECTION GNUNET_NO
49 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
51 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
52 GNUNET_TIME_UNIT_MINUTES,\
55 #if MESH_DEBUG_CONNECTION
56 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
58 #define DEBUG_CONN(...)
62 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
64 #define DEBUG_DHT(...)
71 struct timespec __mesh_start;
72 struct timespec __mesh_end;
73 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
74 #define INTERVAL_END \
76 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
77 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
78 if (__diff < 0) __diff += 1000000000;\
82 #define INTERVAL_SHOW \
84 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
86 #define INTERVAL_START
91 /******************************************************************************/
92 /************************ DATA STRUCTURES ****************************/
93 /******************************************************************************/
95 /** FWD declaration */
101 * Struct containing info about a queued transmission to this peer
108 struct MeshPeerQueue *next;
113 struct MeshPeerQueue *prev;
116 * Peer this transmission is directed to.
118 struct MeshPeerInfo *peer;
121 * Tunnel this message belongs to.
123 struct MeshTunnel *tunnel;
126 * Pointer to info stucture used as cls.
136 * Size of the message
143 * Struct containing all information regarding a given peer
153 * Last time we heard from this peer
155 struct GNUNET_TIME_Absolute last_contact;
158 * Number of attempts to reconnect so far
160 int n_reconnect_attempts;
163 * Paths to reach the peer, ordered by ascending hop count
165 struct MeshPeerPath *path_head;
168 * Paths to reach the peer, ordered by ascending hop count
170 struct MeshPeerPath *path_tail;
173 * Handle to stop the DHT search for a path to this peer
175 struct GNUNET_DHT_GetHandle *dhtget;
178 * Array of tunnels this peer is the target of.
179 * Most probably a small amount, therefore not a hashmap.
180 * When the path to the peer changes, notify these tunnels to let them
181 * re-adjust their path trees.
183 struct MeshTunnel **tunnels;
186 * Number of tunnels this peers participates in
188 unsigned int ntunnels;
191 * Transmission queue to core DLL head
193 struct MeshPeerQueue *queue_head;
196 * Transmission queue to core DLL tail
198 struct MeshPeerQueue *queue_tail;
201 * How many messages are in the queue to this peer.
203 unsigned int queue_n;
206 * Handle for queued transmissions
208 struct GNUNET_CORE_TransmitHandle *core_transmit;
213 * Struct to encapsulate all the Flow Control information to a peer in the
214 * context of a tunnel: Same peer in different tunnels will have independent
215 * flow control structures, allowing to choke/free tunnels according to its
218 struct MeshFlowControl
221 * ID of the last packet sent towards the peer.
223 uint32_t last_pid_sent;
226 * ID of the last packet received from the peer.
228 uint32_t last_pid_recv;
231 * Last ACK sent to the peer (peer can't send more than this PID).
233 uint32_t last_ack_sent;
236 * Last ACK sent towards the origin (for traffic towards leaf node).
238 uint32_t last_ack_recv;
241 * How many payload messages are in the queue towards this peer.
246 * Task to poll the peer in case of a lost ACK causes stall.
248 GNUNET_SCHEDULER_TaskIdentifier poll_task;
251 * How frequently to poll for ACKs.
253 struct GNUNET_TIME_Relative poll_time;
256 * On which tunnel to poll.
257 * Using an explicit poll_ctx would not help memory wise,
258 * since the allocated context would have to be stored in the
259 * fc struct in order to free it upon cancelling poll_task.
261 struct MeshTunnel *t;
266 * Globally unique tunnel identification (owner + number)
267 * DO NOT USE OVER THE NETWORK
272 * Node that owns the tunnel
277 * Tunnel number to differentiate all the tunnels owned by the node oid
278 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
280 MESH_TunnelNumber tid;
285 * Info needed to retry a message in case it gets lost.
287 struct MeshSentMessage {
290 * Tunnel this message is in.
292 struct MeshTunnel *t;
295 * ID of the message (ACK needed to free)
300 * Task to resend/poll in case no ACK is received.
302 GNUNET_SCHEDULER_TaskIdentifier retry_task; // FIXME move to per tunnel timer?
305 * Counter for exponential backoff.
307 struct GNUNET_TIME_Relative retry_timer;
310 * Is this a forward or backward going message?
314 /* struct GNUNET_MESH_Data with payload */
318 * Struct containing all information regarding a tunnel
319 * For an intermediate node the improtant info used will be:
320 * - id Tunnel unique identification
321 * - paths[0] To know where to send it next
322 * - metainfo: ready, speeds, accounting
329 struct MESH_TunnelID id;
332 * Port of the tunnel.
337 * State of the tunnel.
339 enum MeshTunnelState state;
342 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
344 MESH_TunnelNumber local_tid;
347 * Local tunnel number for local destination clients (incoming number)
348 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
351 MESH_TunnelNumber local_tid_dest;
354 * Is the tunnel bufferless (minimum latency)?
359 * Is the tunnel reliable?
364 * Force sending ACK? Flag to allow duplicate ACK on POLL.
369 * How many messages do we accept in the forward queue.
371 unsigned int queue_max;
374 * Last time the tunnel was used
376 struct GNUNET_TIME_Absolute timestamp;
379 * Destination of the tunnel.
384 * Next hop in the tunnel. If 0, @c client must be set.
386 GNUNET_PEER_Id next_hop;
389 * Previous hop in the tunnel. If 0, @c owner must be set.
391 GNUNET_PEER_Id prev_hop;
394 * Flow control information about @c next_hop or @c client.
396 struct MeshFlowControl next_fc;
399 * Flow control information about @c prev_hop or @c owner.
401 struct MeshFlowControl prev_fc;
404 * Client owner of the tunnel, if any
406 struct MeshClient *owner;
409 * Client destination of the tunnel, if any.
411 struct MeshClient *client;
414 * Task to keep the used paths alive at the owner,
415 * time tunnel out on all the other peers.
417 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
420 * Path being used for the tunnel.
422 struct MeshPeerPath *path;
425 * Flag to signal the destruction of the tunnel.
426 * If this is set GNUNET_YES the tunnel will be destroyed
427 * when the queue is empty.
432 * Total messages pending for this tunnels, payload or not.
434 unsigned int pending_messages;
437 * Messages sent and not yet ACK'd.
438 * Only present (non-NULL) at the owner of a tunnel.
440 struct GNUNET_CONTAINER_MultiHashMap32 *sent_messages_fwd;
443 * Messages sent and not yet ACK'd.
444 * Only present (non-NULL) at the destination of a tunnel.
446 struct GNUNET_CONTAINER_MultiHashMap32 *sent_messages_bck;
451 * Struct containing information about a client of the service
453 * TODO: add a list of 'waiting' ports
460 struct MeshClient *next;
465 struct MeshClient *prev;
468 * Tunnels that belong to this client, indexed by local id
470 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
473 * Tunnels this client has accepted, indexed by incoming local id
475 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
478 * Handle to communicate with the client
480 struct GNUNET_SERVER_Client *handle;
483 * Ports that this client has declared interest in.
484 * Indexed by a GMC_hash32 (type), contains *Client.
486 struct GNUNET_CONTAINER_MultiHashMap *ports;
489 * Whether the client is active or shutting down (don't send confirmations
490 * to a client that is shutting down.
495 * ID of the client, mainly for debug messages
502 /******************************************************************************/
503 /************************ DEBUG FUNCTIONS ****************************/
504 /******************************************************************************/
508 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
509 * @param cls string to print
510 * @param success GNUNET_OK if the PUT was transmitted,
511 * GNUNET_NO on timeout,
512 * GNUNET_SYSERR on disconnect from service
513 * after the PUT message was transmitted
514 * (so we don't know if it was received or not)
519 mesh_debug (void *cls, int success)
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
528 unsigned int debug_fwd_ack;
529 unsigned int debug_bck_ack;
533 /******************************************************************************/
534 /*********************** GLOBAL VARIABLES ****************************/
535 /******************************************************************************/
537 /************************** Configuration parameters **************************/
540 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
542 static struct GNUNET_TIME_Relative refresh_path_time;
545 * How often to PUT own ID in the DHT.
547 static struct GNUNET_TIME_Relative id_announce_time;
550 * Maximum time allowed to connect to a peer found by string.
552 static struct GNUNET_TIME_Relative connect_timeout;
555 * Default TTL for payload packets.
557 static unsigned long long default_ttl;
560 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
562 static unsigned long long dht_replication_level;
565 * How many tunnels are we willing to maintain.
566 * Local tunnels are always allowed, even if there are more tunnels than max.
568 static unsigned long long max_tunnels;
571 * How many messages *in total* are we willing to queue, divided by number of
572 * tunnels to get tunnel queue size.
574 static unsigned long long max_msgs_queue;
577 * How many peers do we want to remember?
579 static unsigned long long max_peers;
582 * Percentage of messages that will be dropped (for test purposes only).
584 static unsigned long long drop_percent;
586 /*************************** Static global variables **************************/
589 * Hostkey generation context
591 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
594 * DLL with all the clients, head.
596 static struct MeshClient *clients_head;
599 * DLL with all the clients, tail.
601 static struct MeshClient *clients_tail;
604 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
606 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
609 * Number of tunnels known.
611 static unsigned long long n_tunnels;
614 * Tunnels incoming, indexed by MESH_TunnelNumber
615 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
617 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
620 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
622 static struct GNUNET_CONTAINER_MultiHashMap *peers;
625 * Handle to communicate with transport
627 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
630 * Handle to communicate with core.
632 static struct GNUNET_CORE_Handle *core_handle;
637 static struct GNUNET_DHT_Handle *dht_handle;
642 static struct GNUNET_SERVER_Handle *server_handle;
645 * Handle to the statistics service.
647 static struct GNUNET_STATISTICS_Handle *stats;
650 * Notification context, to send messages to local clients.
652 static struct GNUNET_SERVER_NotificationContext *nc;
655 * Local peer own ID (memory efficient handle).
657 static GNUNET_PEER_Id myid;
660 * Local peer own ID (full value).
662 static struct GNUNET_PeerIdentity my_full_id;
667 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
672 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
675 * Tunnel ID for the next created tunnel (global tunnel number).
677 static MESH_TunnelNumber next_tid;
680 * Tunnel ID for the next incoming tunnel (local tunnel number).
682 static MESH_TunnelNumber next_local_tid;
685 * All ports clients of this peer have opened.
687 static struct GNUNET_CONTAINER_MultiHashMap *ports;
690 * Task to periodically announce itself in the network.
692 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
695 * Next ID to assign to a client.
697 unsigned int next_client_id;
700 /******************************************************************************/
701 /*********************** DECLARATIONS **************************/
702 /******************************************************************************/
705 * Function to process paths received for a new peer addition. The recorded
706 * paths form the initial tunnel, which can be optimized later.
707 * Called on each result obtained for the DHT search.
710 * @param exp when will this value expire
711 * @param key key of the result
712 * @param type type of the result
713 * @param size number of bytes in data
714 * @param data pointer to the result data
717 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
718 const struct GNUNET_HashCode * key,
719 const struct GNUNET_PeerIdentity *get_path,
720 unsigned int get_path_length,
721 const struct GNUNET_PeerIdentity *put_path,
722 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
723 size_t size, const void *data);
727 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
728 * and insert it in the appropriate structures if the peer is not known yet.
730 * @param peer Full identity of the peer.
732 * @return Existing or newly created peer info.
734 static struct MeshPeerInfo *
735 peer_get (const struct GNUNET_PeerIdentity *peer);
739 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
740 * and insert it in the appropriate structures if the peer is not known yet.
742 * @param peer Short identity of the peer.
744 * @return Existing or newly created peer info.
746 static struct MeshPeerInfo *
747 peer_get_short (const GNUNET_PEER_Id peer);
751 * Build a PeerPath from the paths returned from the DHT, reversing the paths
752 * to obtain a local peer -> destination path and interning the peer ids.
754 * @return Newly allocated and created path
756 static struct MeshPeerPath *
757 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
758 unsigned int get_path_length,
759 const struct GNUNET_PeerIdentity *put_path,
760 unsigned int put_path_length);
764 * Adds a path to the peer_infos of all the peers in the path
766 * @param p Path to process.
767 * @param confirmed Whether we know if the path works or not.
770 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
775 * Search for a tunnel by global ID using full PeerIdentities.
777 * @param oid owner of the tunnel.
778 * @param tid global tunnel number.
780 * @return tunnel handler, NULL if doesn't exist.
782 static struct MeshTunnel *
783 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
787 * Notify a tunnel that a connection has broken that affects at least
790 * @param t Tunnel affected.
791 * @param p1 Peer that got disconnected from p2.
792 * @param p2 Peer that got disconnected from p1.
794 * @return Short ID of the peer disconnected (either p1 or p2).
795 * 0 if the tunnel remained unaffected.
797 static GNUNET_PEER_Id
798 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
803 * @brief Use the given path for the tunnel.
804 * Update the next and prev hops (and RCs).
805 * (Re)start the path refresh in case the tunnel is locally owned.
807 * @param t Tunnel to update.
808 * @param p Path to use.
811 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
814 * Tunnel is empty: destroy it.
816 * Notifies all participants (peers, cleints) about the destruction.
818 * @param t Tunnel to destroy.
821 tunnel_destroy_empty (struct MeshTunnel *t);
824 * @brief Queue and pass message to core when possible.
826 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
827 * and accounts for it. In case the queue is full, the message is dropped and
830 * Otherwise, message is treated as internal and allowed to go regardless of
833 * @param cls Closure (@c type dependant). It will be used by queue_send to
834 * build the message to be sent if not already prebuilt.
835 * @param type Type of the message, 0 for a raw message.
836 * @param size Size of the message.
837 * @param dst Neighbor to send message to.
838 * @param t Tunnel this message belongs to.
841 queue_add (void *cls, uint16_t type, size_t size,
842 struct MeshPeerInfo *dst, struct MeshTunnel *t);
846 * Free a transmission that was already queued with all resources
847 * associated to the request.
849 * @param queue Queue handler to cancel.
850 * @param clear_cls Is it necessary to free associated cls?
853 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
857 * @brief Get the next transmittable message from the queue.
859 * This will be the head, except in the case of being a data packet
860 * not allowed by the destination peer.
862 * @param peer Destination peer.
864 * @return The next viable MeshPeerQueue element to send to that peer.
865 * NULL when there are no transmittable messages.
867 struct MeshPeerQueue *
868 queue_get_next (const struct MeshPeerInfo *peer);
872 * Core callback to write a queued packet to core buffer
874 * @param cls Closure (peer info).
875 * @param size Number of bytes available in buf.
876 * @param buf Where the to write the message.
878 * @return number of bytes written to buf
881 queue_send (void *cls, size_t size, void *buf);
884 /******************************************************************************/
885 /************************ PERIODIC FUNCTIONS ****************************/
886 /******************************************************************************/
889 * Periodically announce self id in the DHT
892 * @param tc task context
895 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
899 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
901 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
905 * - Set data expiration in function of X
908 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
910 block.id = my_full_id;
911 GNUNET_DHT_put (dht_handle, /* DHT handle */
912 &my_full_id.hashPubKey, /* Key to use */
913 dht_replication_level, /* Replication level */
914 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
915 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
916 sizeof (block), /* Size of the data */
917 (const char *) &block, /* Data itself */
918 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
919 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
920 NULL, /* Continuation */
921 NULL); /* Continuation closure */
923 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
927 /******************************************************************************/
928 /****************** GENERAL HELPER FUNCTIONS ************************/
929 /******************************************************************************/
933 * Check if client has registered with the service and has not disconnected
935 * @param client the client to check
937 * @return non-NULL if client exists in the global DLL
939 static struct MeshClient *
940 client_get (struct GNUNET_SERVER_Client *client)
942 struct MeshClient *c;
947 if (c->handle == client)
956 * Deletes a tunnel from a client (either owner or destination). To be used on
959 * @param c Client whose tunnel to delete.
960 * @param t Tunnel which should be deleted.
963 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
965 struct GNUNET_HashCode hash;
969 GMC_hash32 (t->local_tid, &hash);
970 GNUNET_assert (GNUNET_YES ==
971 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
975 else if (c == t->client)
977 GMC_hash32 (t->local_tid_dest, &hash);
978 GNUNET_assert (GNUNET_YES ==
979 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
990 * Notify the appropriate client that a new incoming tunnel was created.
992 * @param t Tunnel that was created.
995 send_client_tunnel_create (struct MeshTunnel *t)
997 struct GNUNET_MESH_TunnelMessage msg;
999 if (NULL == t->client)
1001 msg.header.size = htons (sizeof (msg));
1002 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1003 msg.tunnel_id = htonl (t->local_tid_dest);
1004 msg.port = htonl (t->port);
1005 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1006 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1007 &msg.header, GNUNET_NO);
1012 * Notify dest client that the incoming tunnel is no longer valid.
1014 * @param c Client to notify..
1015 * @param t Tunnel that is destroyed.
1018 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1020 struct GNUNET_MESH_TunnelMessage msg;
1027 if (c != t->client && c != t->owner)
1032 msg.header.size = htons (sizeof (msg));
1033 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1034 msg.tunnel_id = htonl (t->local_tid_dest);
1035 msg.port = htonl (0);
1036 memset(&msg.peer, 0, sizeof (msg.peer));
1037 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1038 &msg.header, GNUNET_NO);
1043 * Iterator over all the peers to remove the oldest not-used entry.
1045 * @param cls Closure (unsued).
1046 * @param key ID of the peer.
1047 * @param value Peer_Info of the peer.
1052 peer_info_timeout (void *cls,
1053 const struct GNUNET_HashCode *key,
1060 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1061 * and insert it in the appropriate structures if the peer is not known yet.
1063 * @param peer Full identity of the peer.
1065 * @return Existing or newly created peer info.
1067 static struct MeshPeerInfo *
1068 peer_get (const struct GNUNET_PeerIdentity *peer)
1070 struct MeshPeerInfo *peer_info;
1072 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1073 if (NULL == peer_info)
1076 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1077 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1079 GNUNET_CONTAINER_multihashmap_iterate (peers,
1083 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1084 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1085 peer_info->id = GNUNET_PEER_intern (peer);
1087 peer_info->last_contact = GNUNET_TIME_absolute_get();
1094 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1095 * and insert it in the appropriate structures if the peer is not known yet.
1097 * @param peer Short identity of the peer.
1099 * @return Existing or newly created peer info.
1101 static struct MeshPeerInfo *
1102 peer_get_short (const GNUNET_PEER_Id peer)
1104 struct GNUNET_PeerIdentity id;
1106 GNUNET_PEER_resolve (peer, &id);
1107 return peer_get (&id);
1112 * Choose the best path towards a peer considering the tunnel properties.
1114 * @param peer The destination peer.
1115 * @param t The tunnel the path is for.
1117 * @return Best current known path towards the peer, if any.
1119 static struct MeshPeerPath *
1120 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1122 struct MeshPeerPath *best_p;
1123 struct MeshPeerPath *p;
1124 unsigned int best_cost;
1127 best_p = p = peer->path_head;
1128 best_cost = cost = p->length;
1131 if ((cost = p->length) < best_cost)
1142 * Core callback to write a pre-constructed data packet to core buffer
1144 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1145 * @param size Number of bytes available in buf.
1146 * @param buf Where the to write the message.
1148 * @return number of bytes written to buf
1151 send_core_data_raw (void *cls, size_t size, void *buf)
1153 struct GNUNET_MessageHeader *msg = cls;
1156 GNUNET_assert (NULL != msg);
1157 total_size = ntohs (msg->size);
1159 if (total_size > size)
1164 memcpy (buf, msg, total_size);
1171 * Sends an already built message to a peer, properly registrating
1172 * all used resources.
1174 * @param message Message to send. Function makes a copy of it.
1175 * @param peer Short ID of the neighbor whom to send the message.
1176 * @param t Tunnel on which this message is transmitted.
1179 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1180 GNUNET_PEER_Id peer,
1181 struct MeshTunnel *t)
1183 struct GNUNET_PeerIdentity id;
1184 struct MeshPeerInfo *neighbor;
1185 struct MeshPeerPath *p;
1190 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1195 size = ntohs (message->size);
1196 data = GNUNET_malloc (size);
1197 memcpy (data, message, size);
1198 type = ntohs(message->type);
1199 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1200 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1202 struct GNUNET_MESH_Data *u;
1204 u = (struct GNUNET_MESH_Data *) data;
1205 u->ttl = htonl (ntohl (u->ttl) - 1);
1207 GNUNET_PEER_resolve (peer, &id);
1208 neighbor = peer_get (&id);
1209 for (p = neighbor->path_head; NULL != p; p = p->next)
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 " %s IS NOT DIRECTLY CONNECTED\n",
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 for (p = neighbor->path_head; NULL != p; p = p->next)
1227 struct GNUNET_PeerIdentity debug_id;
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 " path with %u hops through:\n",
1233 for (i = 0; i < p->length; i++)
1235 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 i, GNUNET_i2s(&debug_id));
1242 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1244 " no direct connection to %s\n",
1249 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1260 * Sends a CREATE PATH message for a path to a peer, properly registrating
1261 * all used resources.
1263 * @param t Tunnel for which the path is created.
1266 send_create_path (struct MeshTunnel *t)
1268 struct MeshPeerInfo *neighbor;
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1271 neighbor = peer_get_short (t->next_hop);
1273 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1274 sizeof (struct GNUNET_MESH_CreateTunnel) +
1275 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1278 t->state = MESH_TUNNEL_WAITING;
1283 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1285 * @param t Tunnel which to confirm.
1288 send_path_ack (struct MeshTunnel *t)
1290 struct MeshPeerInfo *peer;
1292 peer = peer_get_short (t->prev_hop);
1295 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1296 sizeof (struct GNUNET_MESH_PathACK),
1303 * Try to establish a new connection to this peer in the given tunnel.
1304 * If the peer doesn't have any path to it yet, try to get one.
1305 * If the peer already has some path, send a CREATE PATH towards it.
1307 * @param peer PeerInfo of the peer.
1308 * @param t Tunnel for which to create the path, if possible.
1311 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1313 struct MeshPeerPath *p;
1315 if (NULL != peer->path_head)
1317 p = peer_get_best_path (peer, t);
1318 tunnel_use_path (t, p);
1319 send_create_path (t);
1321 else if (NULL == peer->dhtget)
1323 struct GNUNET_PeerIdentity id;
1325 GNUNET_PEER_resolve (peer->id, &id);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1328 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1329 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1330 &id.hashPubKey, /* key to search */
1331 dht_replication_level, /* replication level */
1332 GNUNET_DHT_RO_RECORD_ROUTE |
1333 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1335 0, /* xquery bits */
1336 &dht_get_id_handler, peer);
1337 t->state = MESH_TUNNEL_SEARCHING;
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "There is no path but the DHT GET is already started.\n");
1348 * Destroy the peer_info and free any allocated resources linked to it
1350 * @param pi The peer_info to destroy.
1352 * @return GNUNET_OK on success
1355 peer_info_destroy (struct MeshPeerInfo *pi)
1357 struct GNUNET_PeerIdentity id;
1358 struct MeshPeerPath *p;
1359 struct MeshPeerPath *nextp;
1362 GNUNET_PEER_resolve (pi->id, &id);
1363 GNUNET_PEER_change_rc (pi->id, -1);
1366 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1370 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1372 if (NULL != pi->dhtget)
1374 GNUNET_DHT_get_stop (pi->dhtget);
1380 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1384 for (i = 0; i < pi->ntunnels; i++)
1385 tunnel_destroy_empty (pi->tunnels[i]);
1386 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1393 * Remove all paths that rely on a direct connection between p1 and p2
1394 * from the peer itself and notify all tunnels about it.
1396 * @param peer PeerInfo of affected peer.
1397 * @param p1 GNUNET_PEER_Id of one peer.
1398 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1401 * TODO: optimize (see below)
1404 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1407 struct MeshPeerPath *p;
1408 struct MeshPeerPath *next;
1409 struct MeshPeerInfo *peer_d;
1411 unsigned int destroyed;
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1416 for (p = peer->path_head; NULL != p; p = next)
1419 for (i = 0; i < (p->length - 1); i++)
1421 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1422 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1424 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1434 for (i = 0; i < peer->ntunnels; i++)
1436 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1440 peer_d = peer_get_short (d);
1441 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1442 tunnel_use_path (peer->tunnels[i], next);
1443 peer_connect (peer_d, peer->tunnels[i]);
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1450 * Add the path to the peer and update the path used to reach it in case this
1453 * @param peer_info Destination peer to add the path to.
1454 * @param path New path to add. Last peer must be the peer in arg 1.
1455 * Path will be either used of freed if already known.
1456 * @param trusted Do we trust that this path is real?
1459 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1462 struct MeshPeerPath *aux;
1466 if ((NULL == peer_info) || (NULL == path))
1469 path_destroy (path);
1472 if (path->peers[path->length - 1] != peer_info->id)
1475 path_destroy (path);
1478 if (2 >= path->length && GNUNET_NO == trusted)
1480 /* Only allow CORE to tell us about direct paths */
1481 path_destroy (path);
1484 for (l = 1; l < path->length; l++)
1486 if (path->peers[l] == myid)
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1489 for (l2 = 0; l2 < path->length - l; l2++)
1491 path->peers[l2] = path->peers[l + l2];
1496 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1501 struct GNUNET_PeerIdentity id;
1503 GNUNET_PEER_resolve (peer_info->id, &id);
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1505 path->length, GNUNET_i2s (&id));
1508 l = path_get_length (path);
1511 path_destroy (path);
1515 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1516 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1518 l2 = path_get_length (aux);
1521 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1522 peer_info->path_tail, aux, path);
1527 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1529 path_destroy (path);
1534 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1541 * Add the path to the origin peer and update the path used to reach it in case
1542 * this is the shortest.
1543 * The path is given in peer_info -> destination, therefore we turn the path
1544 * upside down first.
1546 * @param peer_info Peer to add the path to, being the origin of the path.
1547 * @param path New path to add after being inversed.
1548 * Path will be either used or freed.
1549 * @param trusted Do we trust that this path is real?
1552 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1553 struct MeshPeerPath *path, int trusted)
1556 peer_info_add_path (peer_info, path, trusted);
1561 * Add a tunnel to the list of tunnels a peer participates in.
1562 * Update the tunnel's destination.
1564 * @param p Peer to add to.
1565 * @param t Tunnel to add.
1568 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1572 GNUNET_break (t->dest == p->id);
1576 GNUNET_PEER_change_rc (t->dest, 1);
1577 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1582 * Remove a tunnel from the list of tunnels a peer participates in.
1583 * Free the tunnel's destination.
1585 * @param p Peer to clean.
1586 * @param t Tunnel to remove.
1589 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1593 if (t->dest == p->id)
1595 GNUNET_PEER_change_rc (t->dest, -1);
1598 for (i = 0; i < p->ntunnels; i++)
1600 if (p->tunnels[i] == t)
1602 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1603 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1611 * Function called if the connection to the peer has been stalled for a while,
1612 * possibly due to a missed ACK. Poll the peer about its ACK status.
1614 * @param cls Closure (poll ctx).
1615 * @param tc TaskContext.
1618 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1620 struct MeshFlowControl *fc = cls;
1621 struct GNUNET_MESH_Poll msg;
1622 struct MeshTunnel *t = fc->t;
1623 GNUNET_PEER_Id peer;
1625 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1626 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1631 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1632 msg.header.size = htons (sizeof (msg));
1633 msg.tid = htonl (t->id.tid);
1634 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1636 if (fc == &t->prev_fc)
1640 else if (fc == &t->next_fc)
1649 send_prebuilt_message (&msg.header, peer, t);
1650 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1651 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1657 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1658 * to obtain a local peer -> destination path and interning the peer ids.
1660 * @return Newly allocated and created path
1662 static struct MeshPeerPath *
1663 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1664 unsigned int get_path_length,
1665 const struct GNUNET_PeerIdentity *put_path,
1666 unsigned int put_path_length)
1668 struct MeshPeerPath *p;
1674 GNUNET_PEER_change_rc (myid, 1);
1675 i = get_path_length;
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1677 for (i--; i >= 0; i--)
1679 id = GNUNET_PEER_intern (&get_path[i]);
1680 if (p->length > 0 && id == p->peers[p->length - 1])
1682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1683 GNUNET_PEER_change_rc (id, -1);
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1688 GNUNET_i2s (&get_path[i]));
1690 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1691 p->peers[p->length - 1] = id;
1694 i = put_path_length;
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1696 for (i--; i >= 0; i--)
1698 id = GNUNET_PEER_intern (&put_path[i]);
1701 /* PUT path went through us, so discard the path up until now and start
1702 * from here to get a much shorter (and loop-free) path.
1707 if (p->length > 0 && id == p->peers[p->length - 1])
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1710 GNUNET_PEER_change_rc (id, -1);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1715 GNUNET_i2s (&put_path[i]));
1717 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1718 p->peers[p->length - 1] = id;
1722 if (get_path_length > 0)
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1724 GNUNET_i2s (&get_path[0]));
1725 if (put_path_length > 0)
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1727 GNUNET_i2s (&put_path[0]));
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1730 for (i = 0; i < p->length; i++)
1732 struct GNUNET_PeerIdentity peer_id;
1734 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1736 GNUNET_i2s (&peer_id));
1744 * Adds a path to the peer_infos of all the peers in the path
1746 * @param p Path to process.
1747 * @param confirmed Whether we know if the path works or not.
1750 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1754 /* TODO: invert and add */
1755 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1756 for (i++; i < p->length; i++)
1758 struct MeshPeerInfo *aux;
1759 struct MeshPeerPath *copy;
1761 aux = peer_get_short (p->peers[i]);
1762 copy = path_duplicate (p);
1763 copy->length = i + 1;
1764 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1770 * Send keepalive packets for a peer
1772 * @param cls Closure (tunnel for which to send the keepalive).
1773 * @param tc Notification context.
1776 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1780 * Search for a tunnel among the incoming tunnels
1782 * @param tid the local id of the tunnel
1784 * @return tunnel handler, NULL if doesn't exist
1786 static struct MeshTunnel *
1787 tunnel_get_incoming (MESH_TunnelNumber tid)
1789 struct GNUNET_HashCode hash;
1791 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1792 GMC_hash32 (tid, &hash);
1793 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1798 * Search for a tunnel among the tunnels for a client
1800 * @param c the client whose tunnels to search in
1801 * @param tid the local id of the tunnel
1803 * @return tunnel handler, NULL if doesn't exist
1805 static struct MeshTunnel *
1806 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1808 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1810 return tunnel_get_incoming (tid);
1814 struct GNUNET_HashCode hash;
1816 GMC_hash32 (tid, &hash);
1817 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1823 * Search for a tunnel by global ID using PEER_ID
1825 * @param pi owner of the tunnel
1826 * @param tid global tunnel number
1828 * @return tunnel handler, NULL if doesn't exist
1830 static struct MeshTunnel *
1831 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1833 struct MESH_TunnelID id;
1834 struct GNUNET_HashCode hash;
1839 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1840 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1845 * Search for a tunnel by global ID using full PeerIdentities
1847 * @param oid owner of the tunnel
1848 * @param tid global tunnel number
1850 * @return tunnel handler, NULL if doesn't exist
1852 static struct MeshTunnel *
1853 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1855 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1860 * Add a client to a tunnel, initializing all needed data structures.
1862 * @param t Tunnel to which add the client.
1863 * @param c Client which to add to the tunnel.
1866 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1868 struct GNUNET_HashCode hash;
1870 if (NULL != t->client)
1875 GMC_hash32 (t->local_tid_dest, &hash);
1877 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1878 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1884 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1885 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1895 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1897 unsigned int own_pos;
1899 for (own_pos = 0; own_pos < p->length; own_pos++)
1901 if (p->peers[own_pos] == myid)
1904 if (own_pos > p->length - 1)
1910 if (own_pos < p->length - 1)
1911 t->next_hop = p->peers[own_pos + 1];
1913 t->next_hop = p->peers[own_pos];
1914 GNUNET_PEER_change_rc (t->next_hop, 1);
1916 t->prev_hop = p->peers[own_pos - 1];
1918 t->prev_hop = p->peers[0];
1919 GNUNET_PEER_change_rc (t->prev_hop, 1);
1921 if (NULL != t->path)
1922 path_destroy (t->path);
1923 t->path = path_duplicate (p);
1926 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1927 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1928 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1935 * Notifies a tunnel that a connection has broken that affects at least
1936 * some of its peers. Sends a notification towards the root of the tree.
1937 * In case the peer is the owner of the tree, notifies the client that owns
1938 * the tunnel and tries to reconnect.
1940 * @param t Tunnel affected.
1941 * @param p1 Peer that got disconnected from p2.
1942 * @param p2 Peer that got disconnected from p1.
1944 * @return Short ID of the peer disconnected (either p1 or p2).
1945 * 0 if the tunnel remained unaffected.
1947 static GNUNET_PEER_Id
1948 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1951 // if (myid != p1 && myid != p2) FIXME
1956 // if (tree_get_predecessor (t->tree) != 0)
1958 // /* We are the peer still connected, notify owner of the disconnection. */
1959 // struct GNUNET_MESH_PathBroken msg;
1960 // struct GNUNET_PeerIdentity neighbor;
1962 // msg.header.size = htons (sizeof (msg));
1963 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1964 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1965 // msg.tid = htonl (t->id.tid);
1966 // msg.peer1 = my_full_id;
1967 // GNUNET_PEER_resolve (pid, &msg.peer2);
1968 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1969 // send_prebuilt_message (&msg.header, &neighbor, t);
1976 * Build a local ACK message and send it to a local client.
1978 * @param t Tunnel on which to send the ACK.
1979 * @param c Client to whom send the ACK.
1980 * @param ack Value of the ACK.
1983 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1985 struct GNUNET_MESH_LocalAck msg;
1987 msg.header.size = htons (sizeof (msg));
1988 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1989 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1990 msg.ack = htonl (ack);
1991 GNUNET_SERVER_notification_context_unicast(nc,
1998 * Build an ACK message and queue it to send to the given peer.
2000 * @param t Tunnel on which to send the ACK.
2001 * @param peer Peer to whom send the ACK.
2002 * @param ack Value of the ACK.
2005 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2007 struct GNUNET_MESH_ACK msg;
2009 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2010 msg.header.size = htons (sizeof (msg));
2011 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2012 msg.pid = htonl (ack);
2013 msg.tid = htonl (t->id.tid);
2015 send_prebuilt_message (&msg.header, peer, t);
2020 * Send an ACK informing the predecessor about the available buffer space.
2021 * In case there is no predecessor, inform the owning client.
2022 * If buffering is off, send only on behalf of children or self if endpoint.
2023 * If buffering is on, send when sent to children and buffer space is free.
2024 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2025 * the ACK itself goes "back" (towards root).
2027 * @param t Tunnel on which to send the ACK.
2028 * @param type Type of message that triggered the ACK transmission.
2031 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2035 /* Is it after unicast retransmission? */
2038 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2040 "ACK due to FWD DATA retransmission\n");
2041 if (GNUNET_YES == t->nobuffer)
2043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2047 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2048 if (NULL != t->owner && GNUNET_YES == t->reliable)
2050 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2052 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2053 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2054 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2055 t->force_ack = GNUNET_YES;
2061 /* Check if we need to transmit the ACK */
2062 if (t->queue_max > t->next_fc.queue_n * 4 &&
2063 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2064 GNUNET_NO == t->force_ack)
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 " t->qmax: %u, t->qn: %u\n",
2069 t->queue_max, t->next_fc.queue_n);
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2071 " t->pid: %u, t->ack: %u\n",
2072 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2076 /* Ok, ACK might be necessary, what PID to ACK? */
2077 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2078 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2084 t->prev_fc.last_ack_sent = ack;
2085 if (NULL != t->owner)
2086 send_local_ack (t, t->owner, ack);
2087 else if (0 != t->prev_hop)
2088 send_ack (t, t->prev_hop, ack);
2092 t->force_ack = GNUNET_NO;
2097 * Send an ACK informing the children node/client about the available
2099 * If buffering is off, send only on behalf of root (can be self).
2100 * If buffering is on, send when sent to predecessor and buffer space is free.
2101 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2102 * the ACK itself goes "forward" (towards children/clients).
2104 * @param t Tunnel on which to send the ACK.
2105 * @param type Type of message that triggered the ACK transmission.
2108 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2113 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2114 /* Is it after data to_origin retransmission? */
2117 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2118 if (GNUNET_YES == t->nobuffer)
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 " Not sending ACK, nobuffer + traffic\n");
2125 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2126 if (NULL != t->client && GNUNET_YES == t->reliable)
2128 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2130 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2131 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2132 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2133 t->force_ack = GNUNET_YES;
2139 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2141 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2143 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2146 " Not sending ACK, not needed, last ack sent was %u\n",
2147 t->next_fc.last_ack_sent);
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 " Sending BCK ACK %u (last sent: %u)\n",
2152 ack, t->next_fc.last_ack_sent);
2153 t->next_fc.last_ack_sent = ack;
2155 if (NULL != t->client)
2156 send_local_ack (t, t->client, ack);
2157 else if (0 != t->next_hop)
2158 send_ack (t, t->next_hop, ack);
2161 t->force_ack = GNUNET_NO;
2166 * Modify the unicast message TID from global to local and send to client.
2168 * @param t Tunnel on which to send the message.
2169 * @param msg Message to modify and send.
2172 tunnel_send_client_ucast (struct MeshTunnel *t,
2173 const struct GNUNET_MESH_Data *msg)
2175 struct GNUNET_MESH_Data *copy;
2176 uint16_t size = ntohs (msg->header.size);
2179 if (size < sizeof (struct GNUNET_MESH_Data) +
2180 sizeof (struct GNUNET_MessageHeader))
2182 GNUNET_break_op (0);
2185 if (NULL == t->client)
2190 copy = (struct GNUNET_MESH_Data *) cbuf;
2191 memcpy (copy, msg, size);
2192 copy->tid = htonl (t->local_tid_dest);
2193 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2194 ©->header, GNUNET_NO);
2199 * Modify the to_origin message TID from global to local and send to client.
2201 * @param t Tunnel on which to send the message.
2202 * @param msg Message to modify and send.
2205 tunnel_send_client_to_orig (struct MeshTunnel *t,
2206 const struct GNUNET_MESH_Data *msg)
2208 struct GNUNET_MESH_Data *copy;
2209 uint16_t size = ntohs (msg->header.size);
2212 if (size < sizeof (struct GNUNET_MESH_Data) +
2213 sizeof (struct GNUNET_MessageHeader))
2215 GNUNET_break_op (0);
2218 if (NULL == t->owner)
2223 copy = (struct GNUNET_MESH_Data *) cbuf;
2224 memcpy (cbuf, msg, size);
2225 copy->tid = htonl (t->local_tid);
2226 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2227 ©->header, GNUNET_NO);
2232 * We haven't received an ACK after a certain time: restransmit the message.
2234 * @param cls Closure (MeshSentMessage with the message to restransmit)
2235 * @param tc TaskContext.
2238 tunnel_retransmit_message (void *cls,
2239 const struct GNUNET_SCHEDULER_TaskContext *tc)
2241 struct MeshSentMessage *copy = cls;
2242 struct GNUNET_MESH_Data *payload;
2245 copy->retry_task = GNUNET_SCHEDULER_NO_TASK;
2246 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2249 payload = (struct GNUNET_MESH_Data *) ©[1];
2250 hop = copy->is_forward ? copy->t->next_hop : copy->t->prev_hop;
2251 send_prebuilt_message (&payload->header, hop, copy->t);
2252 GNUNET_STATISTICS_update (stats, "# unicast retransmitted", 1, GNUNET_NO);
2253 copy->retry_timer = GNUNET_TIME_STD_BACKOFF (copy->retry_timer);
2254 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
2255 &tunnel_retransmit_message,
2261 * @brief Re-initiate traffic to this peer if necessary.
2263 * Check if there is traffic queued towards this peer
2264 * and the core transmit handle is NULL (traffic was stalled).
2265 * If so, call core tmt rdy.
2267 * @param peer_id Short ID of peer to which initiate traffic.
2270 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2272 struct MeshPeerInfo *peer;
2273 struct GNUNET_PeerIdentity id;
2274 struct MeshPeerQueue *q;
2277 peer = peer_get_short (peer_id);
2278 if (NULL != peer->core_transmit)
2281 q = queue_get_next (peer);
2284 /* Might br multicast traffic already sent to this particular peer but
2285 * not to other children in this tunnel.
2286 * This way t->queue_n would be > 0 but the queue of this particular peer
2292 GNUNET_PEER_resolve (peer->id, &id);
2293 peer->core_transmit =
2294 GNUNET_CORE_notify_transmit_ready(core_handle,
2297 GNUNET_TIME_UNIT_FOREVER_REL,
2307 * Send a message to all peers and clients in this tunnel that the tunnel
2308 * is no longer valid. If some peer or client should not receive the message,
2309 * should be zero'ed out before calling this function.
2311 * @param t The tunnel whose peers and clients to notify.
2314 tunnel_send_destroy (struct MeshTunnel *t)
2316 struct GNUNET_MESH_TunnelDestroy msg;
2317 struct GNUNET_PeerIdentity id;
2319 msg.header.size = htons (sizeof (msg));
2320 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2321 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2322 msg.tid = htonl (t->id.tid);
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 " sending tunnel destroy for tunnel: %s [%X]\n",
2325 GNUNET_i2s (&msg.oid), t->id.tid);
2327 if (NULL == t->client && 0 != t->next_hop)
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2330 GNUNET_PEER_resolve (t->next_hop, &id);
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 " sending forward to %s\n",
2334 send_prebuilt_message (&msg.header, t->next_hop, t);
2336 if (NULL == t->owner && 0 != t->prev_hop)
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2339 GNUNET_PEER_resolve (t->prev_hop, &id);
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 " sending back to %s\n",
2343 send_prebuilt_message (&msg.header, t->prev_hop, t);
2345 if (NULL != t->owner)
2347 send_client_tunnel_destroy (t->owner, t);
2349 if (NULL != t->client)
2351 send_client_tunnel_destroy (t->client, t);
2357 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2359 * @param t Tunnel which to cancel.
2360 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2363 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2365 struct MeshPeerInfo *peer_info;
2366 struct MeshPeerQueue *pq;
2367 struct MeshPeerQueue *next;
2370 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2371 peer_info = peer_get_short (neighbor);
2372 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2375 if (pq->tunnel == t)
2377 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2378 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2380 /* Should have been removed on destroy children */
2383 queue_destroy (pq, GNUNET_YES);
2386 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2388 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2389 peer_info->core_transmit = NULL;
2395 * Destroy the tunnel.
2397 * This function does not generate any warning traffic to clients or peers.
2400 * Remove the tunnel from peer_info's and clients' hashmaps.
2401 * Cancel messages belonging to this tunnel queued to neighbors.
2402 * Free any allocated resources linked to the tunnel.
2404 * @param t the tunnel to destroy
2406 * @return GNUNET_OK on success
2409 tunnel_destroy (struct MeshTunnel *t)
2411 struct MeshClient *c;
2412 struct GNUNET_HashCode hash;
2422 struct GNUNET_PeerIdentity id;
2424 GNUNET_PEER_resolve (t->id.oid, &id);
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2426 GNUNET_i2s (&id), t->id.tid);
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2432 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2433 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2441 GMC_hash32 (t->local_tid, &hash);
2443 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2450 if (NULL != t->client)
2453 GMC_hash32 (t->local_tid_dest, &hash);
2455 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2461 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2468 if (0 != t->prev_hop)
2470 peer_cancel_queues (t->prev_hop, t);
2471 GNUNET_PEER_change_rc (t->prev_hop, -1);
2473 if (0 != t->next_hop)
2475 peer_cancel_queues (t->next_hop, t);
2476 GNUNET_PEER_change_rc (t->next_hop, -1);
2479 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2482 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2483 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2486 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2487 path_destroy (t->path);
2493 * Tunnel is empty: destroy it.
2495 * Notifies all participants (peers, cleints) about the destruction.
2497 * @param t Tunnel to destroy.
2500 tunnel_destroy_empty (struct MeshTunnel *t)
2504 struct GNUNET_PeerIdentity id;
2506 GNUNET_PEER_resolve (t->id.oid, &id);
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 "executing destruction of empty tunnel %s [%X]\n",
2509 GNUNET_i2s (&id), t->id.tid);
2513 if (GNUNET_NO == t->destroy)
2514 tunnel_send_destroy (t);
2515 if (0 == t->pending_messages)
2518 t->destroy = GNUNET_YES;
2522 * Initialize a Flow Control structure to the initial state.
2524 * @param fc Flow Control structure to initialize.
2527 fc_init (struct MeshFlowControl *fc)
2529 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2530 fc->last_pid_recv = (uint32_t) -1;
2531 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2532 fc->last_ack_recv = (uint32_t) -1;
2533 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2534 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2539 * Create a new tunnel
2541 * @param owner Who is the owner of the tunnel (short ID).
2542 * @param tid Tunnel Number of the tunnel.
2543 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2544 * @param local Tunnel Number for the tunnel, for the client point of view.
2546 * @return A new initialized tunnel. NULL on error.
2548 static struct MeshTunnel *
2549 tunnel_new (GNUNET_PEER_Id owner,
2550 MESH_TunnelNumber tid,
2551 struct MeshClient *client,
2552 MESH_TunnelNumber local)
2554 struct MeshTunnel *t;
2555 struct GNUNET_HashCode hash;
2557 if (n_tunnels >= max_tunnels && NULL == client)
2560 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2563 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2565 fc_init (&t->next_fc);
2566 fc_init (&t->prev_fc);
2567 t->local_tid = local;
2569 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2571 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2573 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2581 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2588 GMC_hash32 (t->local_tid, &hash);
2590 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2591 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2595 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2605 * Set options in a tunnel, extracted from a bit flag field
2607 * @param t Tunnel to set options to.
2608 * @param options Bit array in host byte order.
2611 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2613 t->nobuffer = options & GNUNET_MESH_OPTION_NOBUFFER;
2614 t->reliable = options & GNUNET_MESH_OPTION_RELIABLE;
2619 * Iterator for deleting each tunnel whose client endpoint disconnected.
2621 * @param cls Closure (client that has disconnected).
2622 * @param key The hash of the local tunnel id (used to access the hashmap).
2623 * @param value The value stored at the key (tunnel to destroy).
2625 * @return GNUNET_OK, keep iterating.
2628 tunnel_destroy_iterator (void *cls,
2629 const struct GNUNET_HashCode * key,
2632 struct MeshTunnel *t = value;
2633 struct MeshClient *c = cls;
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2636 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2637 t->local_tid, t->local_tid_dest, c->id);
2638 client_delete_tunnel (c, t);
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2643 if (0 != t->next_hop) { /* destroy could come before a path is used */
2644 GNUNET_PEER_change_rc (t->next_hop, -1);
2648 else if (c == t->owner)
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2652 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2653 GNUNET_PEER_change_rc (t->prev_hop, -1);
2661 tunnel_destroy_empty (t);
2668 * Timeout function, destroys tunnel if called
2670 * @param cls Closure (tunnel to destroy).
2671 * @param tc TaskContext
2674 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2676 struct MeshTunnel *t = cls;
2677 struct GNUNET_PeerIdentity id;
2679 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2680 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2682 GNUNET_PEER_resolve(t->id.oid, &id);
2683 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2684 "Tunnel %s [%X] timed out. Destroying.\n",
2685 GNUNET_i2s(&id), t->id.tid);
2686 if (NULL != t->client)
2687 send_client_tunnel_destroy (t->client, t);
2688 tunnel_destroy (t); /* Do not notify other */
2693 * Resets the tunnel timeout. Starts it if no timeout was running.
2695 * @param t Tunnel whose timeout to reset.
2697 * TODO use heap to improve efficiency of scheduler.
2700 tunnel_reset_timeout (struct MeshTunnel *t)
2702 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2704 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2705 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2706 t->maintenance_task =
2707 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2708 (refresh_path_time, 4), &tunnel_timeout, t);
2712 /******************************************************************************/
2713 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2714 /******************************************************************************/
2717 * Function to send a create path packet to a peer.
2719 * @param cls closure
2720 * @param size number of bytes available in buf
2721 * @param buf where the callee should write the message
2722 * @return number of bytes written to buf
2725 send_core_path_create (void *cls, size_t size, void *buf)
2727 struct MeshTunnel *t = cls;
2728 struct GNUNET_MESH_CreateTunnel *msg;
2729 struct GNUNET_PeerIdentity *peer_ptr;
2730 struct MeshPeerPath *p = t->path;
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2737 sizeof (struct GNUNET_MESH_CreateTunnel) +
2738 p->length * sizeof (struct GNUNET_PeerIdentity);
2740 if (size < size_needed || NULL == buf)
2745 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2746 msg->header.size = htons (size_needed);
2747 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2748 msg->tid = ntohl (t->id.tid);
2751 if (GNUNET_YES == t->nobuffer)
2752 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2753 if (GNUNET_YES == t->reliable)
2754 opt |= GNUNET_MESH_OPTION_RELIABLE;
2755 msg->opt = htonl (opt);
2756 msg->port = htonl (t->port);
2758 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2759 for (i = 0; i < p->length; i++)
2761 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2771 * Creates a path ack message in buf and frees all unused resources.
2773 * @param cls closure (MeshTransmissionDescriptor)
2774 * @param size number of bytes available in buf
2775 * @param buf where the callee should write the message
2776 * @return number of bytes written to buf
2779 send_core_path_ack (void *cls, size_t size, void *buf)
2781 struct MeshTunnel *t = cls;
2782 struct GNUNET_MESH_PathACK *msg = buf;
2784 GNUNET_assert (NULL != t);
2785 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2790 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2791 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2792 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2793 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2794 msg->tid = htonl (t->id.tid);
2795 msg->peer_id = my_full_id;
2796 msg->ack = htonl (t->prev_fc.last_ack_sent);
2798 /* TODO add signature */
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2801 return sizeof (struct GNUNET_MESH_PathACK);
2806 * Free a transmission that was already queued with all resources
2807 * associated to the request.
2809 * @param queue Queue handler to cancel.
2810 * @param clear_cls Is it necessary to free associated cls?
2813 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2815 struct MeshFlowControl *fc;
2817 if (GNUNET_YES == clear_cls)
2819 switch (queue->type)
2821 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2822 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2823 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2825 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2826 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2827 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2828 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2829 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2831 " prebuilt message\n");
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2834 GNUNET_MESH_DEBUG_M2S (queue->type));
2836 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2842 " type %s unknown!\n",
2843 GNUNET_MESH_DEBUG_M2S (queue->type));
2845 GNUNET_free_non_null (queue->cls);
2847 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2848 queue->peer->queue_tail,
2851 /* Delete from appropriate fc in the tunnel */
2852 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2853 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2855 if (queue->peer->id == queue->tunnel->prev_hop)
2856 fc = &queue->tunnel->prev_fc;
2857 else if (queue->peer->id == queue->tunnel->next_hop)
2858 fc = &queue->tunnel->next_fc;
2866 GNUNET_free (queue);
2871 * @brief Get the next transmittable message from the queue.
2873 * This will be the head, except in the case of being a data packet
2874 * not allowed by the destination peer.
2876 * @param peer Destination peer.
2878 * @return The next viable MeshPeerQueue element to send to that peer.
2879 * NULL when there are no transmittable messages.
2881 struct MeshPeerQueue *
2882 queue_get_next (const struct MeshPeerInfo *peer)
2884 struct MeshPeerQueue *q;
2886 struct GNUNET_MESH_Data *dmsg;
2887 struct MeshTunnel* t;
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2892 for (q = peer->queue_head; NULL != q; q = q->next)
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2897 GNUNET_MESH_DEBUG_M2S (q->type));
2898 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2899 pid = ntohl (dmsg->pid);
2902 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2903 ack = t->next_fc.last_ack_recv;
2905 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2906 ack = t->prev_fc.last_ack_recv;
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "* ACK: %u, PID: %u\n",
2916 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2929 "* nothing found\n");
2935 queue_send (void *cls, size_t size, void *buf)
2937 struct MeshPeerInfo *peer = cls;
2938 struct GNUNET_MessageHeader *msg;
2939 struct MeshPeerQueue *queue;
2940 struct MeshTunnel *t;
2941 struct GNUNET_PeerIdentity dst_id;
2942 struct MeshFlowControl *fc;
2947 peer->core_transmit = NULL;
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2950 queue = queue_get_next (peer);
2952 /* Queue has no internal mesh traffic nor sendable payload */
2955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2956 if (NULL == peer->queue_head)
2957 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
2962 GNUNET_PEER_resolve (peer->id, &dst_id);
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2965 GNUNET_i2s (&dst_id));
2966 /* Check if buffer size is enough for the message */
2967 if (queue->size > size)
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2970 "* not enough room, reissue\n");
2971 peer->core_transmit =
2972 GNUNET_CORE_notify_transmit_ready (core_handle,
2975 GNUNET_TIME_UNIT_FOREVER_REL,
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
2985 GNUNET_assert (0 < t->pending_messages);
2986 t->pending_messages--;
2990 switch (queue->type)
2993 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2994 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2995 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2996 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2997 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2998 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3001 GNUNET_MESH_DEBUG_M2S (queue->type));
3003 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3004 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3005 data_size = send_core_data_raw (queue->cls, size, buf);
3006 msg = (struct GNUNET_MessageHeader *) buf;
3007 type = ntohs (msg->type);
3009 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3011 data_size = send_core_path_create (queue->cls, size, buf);
3013 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3015 data_size = send_core_path_ack (queue->cls, size, buf);
3019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3020 "* type unknown: %u\n",
3025 if (0 < drop_percent &&
3026 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3028 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3029 "Dropping message of type %s\n",
3030 GNUNET_MESH_DEBUG_M2S(queue->type));
3033 /* Free queue, but cls was freed by send_core_* */
3034 queue_destroy (queue, GNUNET_NO);
3036 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3037 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3040 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3041 t->next_fc.last_pid_sent = pid;
3042 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3044 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3045 t->prev_fc.last_pid_sent = pid;
3046 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3052 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3058 /* If more data in queue, send next */
3059 queue = queue_get_next (peer);
3062 struct GNUNET_PeerIdentity id;
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3065 GNUNET_PEER_resolve (peer->id, &id);
3066 peer->core_transmit =
3067 GNUNET_CORE_notify_transmit_ready(core_handle,
3070 GNUNET_TIME_UNIT_FOREVER_REL,
3076 else if (NULL != peer->queue_head)
3078 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3080 GNUNET_i2s (&my_full_id));
3081 if (peer->id == t->next_hop)
3083 else if (peer->id == t->prev_hop)
3090 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3093 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3103 * @brief Queue and pass message to core when possible.
3105 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3106 * accounts for it. In case the queue is full, the message is dropped and
3109 * Otherwise, message is treated as internal and allowed to go regardless of
3112 * @param cls Closure (@c type dependant). It will be used by queue_send to
3113 * build the message to be sent if not already prebuilt.
3114 * @param type Type of the message, 0 for a raw message.
3115 * @param size Size of the message.
3116 * @param dst Neighbor to send message to.
3117 * @param t Tunnel this message belongs to.
3120 queue_add (void *cls, uint16_t type, size_t size,
3121 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3123 struct MeshPeerQueue *queue;
3124 struct GNUNET_PeerIdentity id;
3128 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3130 n = &t->next_fc.queue_n;
3132 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3134 n = &t->prev_fc.queue_n;
3138 if (*n >= t->queue_max)
3141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3142 "queue full: %u/%u\n",
3144 GNUNET_STATISTICS_update(stats,
3145 "# messages dropped (buffer full)",
3147 return; /* Drop message */
3151 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3157 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3158 if (NULL == dst->core_transmit)
3160 GNUNET_PEER_resolve (dst->id, &id);
3161 dst->core_transmit =
3162 GNUNET_CORE_notify_transmit_ready (core_handle,
3165 GNUNET_TIME_UNIT_FOREVER_REL,
3171 t->pending_messages++;
3175 /******************************************************************************/
3176 /******************** MESH NETWORK HANDLERS **************************/
3177 /******************************************************************************/
3181 * Core handler for path creation
3183 * @param cls closure
3184 * @param message message
3185 * @param peer peer identity this notification is about
3187 * @return GNUNET_OK to keep the connection open,
3188 * GNUNET_SYSERR to close it (signal serious error)
3191 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3192 const struct GNUNET_MessageHeader *message)
3194 unsigned int own_pos;
3197 MESH_TunnelNumber tid;
3198 struct GNUNET_MESH_CreateTunnel *msg;
3199 struct GNUNET_PeerIdentity *pi;
3200 struct MeshPeerPath *path;
3201 struct MeshPeerInfo *dest_peer_info;
3202 struct MeshPeerInfo *orig_peer_info;
3203 struct MeshTunnel *t;
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3206 "Received a path create msg [%s]\n",
3207 GNUNET_i2s (&my_full_id));
3208 size = ntohs (message->size);
3209 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3211 GNUNET_break_op (0);
3215 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3216 if (size % sizeof (struct GNUNET_PeerIdentity))
3218 GNUNET_break_op (0);
3221 size /= sizeof (struct GNUNET_PeerIdentity);
3224 GNUNET_break_op (0);
3227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3228 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3230 tid = ntohl (msg->tid);
3231 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3233 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3234 t = tunnel_get (pi, tid);
3235 if (NULL == t) /* might be a local tunnel */
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3240 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3246 t->port = ntohl (msg->port);
3247 opt = ntohl (msg->opt);
3248 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3250 t->nobuffer = GNUNET_YES;
3253 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3255 t->reliable = GNUNET_YES;
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3259 tunnel_reset_timeout (t);
3261 t->state = MESH_TUNNEL_WAITING;
3263 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3264 if (NULL == dest_peer_info)
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3267 " Creating PeerInfo for destination.\n");
3268 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3269 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3270 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3272 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3274 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3275 if (NULL == orig_peer_info)
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 " Creating PeerInfo for origin.\n");
3279 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3280 orig_peer_info->id = GNUNET_PEER_intern (pi);
3281 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3282 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3285 path = path_new (size);
3287 for (i = 0; i < size; i++)
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3290 GNUNET_i2s (&pi[i]));
3291 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3292 if (path->peers[i] == myid)
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3296 if (own_pos == 0 && path->peers[own_pos] != myid)
3298 /* create path: self not found in path through self */
3299 GNUNET_break_op (0);
3300 path_destroy (path);
3304 path_add_to_peers (path, GNUNET_NO);
3305 tunnel_use_path (t, path);
3307 peer_info_add_tunnel (dest_peer_info, t);
3309 if (own_pos == size - 1)
3311 struct MeshClient *c;
3312 struct GNUNET_HashCode hc;
3314 /* Find target client */
3315 GMC_hash32 (t->port, &hc);
3316 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3319 /* TODO send reject */
3323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3324 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3326 /* Assign local tid */
3327 while (NULL != tunnel_get_incoming (next_local_tid))
3328 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3329 t->local_tid_dest = next_local_tid++;
3330 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3332 if (GNUNET_YES == t->reliable)
3333 t->sent_messages_bck =
3334 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
3336 tunnel_add_client (t, c);
3337 send_client_tunnel_create (t);
3342 struct MeshPeerPath *path2;
3344 t->next_hop = path->peers[own_pos + 1];
3345 GNUNET_PEER_change_rc(t->next_hop, 1);
3347 /* It's for somebody else! Retransmit. */
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3349 path2 = path_duplicate (path);
3350 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3351 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3352 send_create_path (t);
3360 * Core handler for path ACKs
3362 * @param cls closure
3363 * @param message message
3364 * @param peer peer identity this notification is about
3366 * @return GNUNET_OK to keep the connection open,
3367 * GNUNET_SYSERR to close it (signal serious error)
3370 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3371 const struct GNUNET_MessageHeader *message)
3373 struct GNUNET_MESH_PathACK *msg;
3374 struct MeshPeerInfo *peer_info;
3375 struct MeshPeerPath *p;
3376 struct MeshTunnel *t;
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3379 GNUNET_i2s (&my_full_id));
3380 msg = (struct GNUNET_MESH_PathACK *) message;
3381 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3384 /* TODO notify that we don't know the tunnel */
3385 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3387 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3391 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3393 peer_info = peer_get (&msg->peer_id);
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3395 GNUNET_i2s (&msg->peer_id));
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3399 /* Add path to peers? */
3403 path_add_to_peers (p, GNUNET_YES);
3409 t->state = MESH_TUNNEL_READY;
3410 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3411 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3413 /* Message for us? */
3414 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3417 if (NULL == t->owner)
3419 GNUNET_break_op (0);
3422 if (NULL != peer_info->dhtget)
3424 GNUNET_DHT_get_stop (peer_info->dhtget);
3425 peer_info->dhtget = NULL;
3427 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3428 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 " not for us, retransmitting...\n");
3434 peer_info = peer_get (&msg->oid);
3435 send_prebuilt_message (message, t->prev_hop, t);
3441 * Core handler for notifications of broken paths
3443 * @param cls closure
3444 * @param message message
3445 * @param peer peer identity this notification is about
3447 * @return GNUNET_OK to keep the connection open,
3448 * GNUNET_SYSERR to close it (signal serious error)
3451 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3452 const struct GNUNET_MessageHeader *message)
3454 struct GNUNET_MESH_PathBroken *msg;
3455 struct MeshTunnel *t;
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3458 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3459 msg = (struct GNUNET_MESH_PathBroken *) message;
3460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3461 GNUNET_i2s (&msg->peer1));
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3463 GNUNET_i2s (&msg->peer2));
3464 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3467 GNUNET_break_op (0);
3470 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3471 GNUNET_PEER_search (&msg->peer2));
3478 * Core handler for tunnel destruction
3480 * @param cls closure
3481 * @param message message
3482 * @param peer peer identity this notification is about
3484 * @return GNUNET_OK to keep the connection open,
3485 * GNUNET_SYSERR to close it (signal serious error)
3488 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3489 const struct GNUNET_MessageHeader *message)
3491 struct GNUNET_MESH_TunnelDestroy *msg;
3492 struct MeshTunnel *t;
3494 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3496 "Got a TUNNEL DESTROY packet from %s\n",
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3499 " for tunnel %s [%u]\n",
3500 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3501 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3504 /* Probably already got the message from another path,
3505 * destroyed the tunnel and retransmitted to children.
3508 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3512 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3515 t->local_tid, t->local_tid_dest);
3517 if (GNUNET_PEER_search (peer) == t->prev_hop)
3519 // TODO check owner's signature
3520 // TODO add owner's signatue to tunnel for retransmission
3521 peer_cancel_queues (t->prev_hop, t);
3522 GNUNET_PEER_change_rc (t->prev_hop, -1);
3525 else if (GNUNET_PEER_search (peer) == t->next_hop)
3527 // TODO check dest's signature
3528 // TODO add dest's signatue to tunnel for retransmission
3529 peer_cancel_queues (t->next_hop, t);
3530 GNUNET_PEER_change_rc (t->next_hop, -1);
3535 GNUNET_break_op (0);
3536 // TODO check both owner AND destination's signature to see which matches
3537 // TODO restransmit in appropriate direction
3540 tunnel_destroy_empty (t);
3542 // TODO: add timeout to destroy the tunnel anyway
3548 * Core handler for mesh network traffic going from the origin to a peer
3550 * @param cls closure
3551 * @param peer peer identity this notification is about
3552 * @param message message
3553 * @return GNUNET_OK to keep the connection open,
3554 * GNUNET_SYSERR to close it (signal serious error)
3557 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3558 const struct GNUNET_MessageHeader *message)
3560 struct GNUNET_MESH_Data *msg;
3561 struct MeshTunnel *t;
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3569 size = ntohs (message->size);
3571 sizeof (struct GNUNET_MESH_Data) +
3572 sizeof (struct GNUNET_MessageHeader))
3577 msg = (struct GNUNET_MESH_Data *) message;
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3579 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3581 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3584 /* TODO notify back: we don't know this tunnel */
3585 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3586 GNUNET_break_op (0);
3589 pid = ntohl (msg->pid);
3590 if (t->prev_fc.last_pid_recv == pid)
3592 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3593 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3594 " Already seen pid %u, DROPPING!\n", pid);
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3600 " pid %u not seen yet, forwarding\n", pid);
3603 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3605 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3606 GNUNET_break_op (0);
3607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3608 "Received PID %u, ACK %u\n",
3609 pid, t->prev_fc.last_ack_sent);
3610 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3613 t->prev_fc.last_pid_recv = pid;
3615 tunnel_reset_timeout (t);
3616 if (t->dest == myid)
3618 /* TODO signature verification */
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3620 " it's for us! sending to clients...\n");
3621 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3622 tunnel_send_client_ucast (t, msg);
3623 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3626 if (0 == t->next_hop)
3631 ttl = ntohl (msg->ttl);
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3635 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3636 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3637 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3641 " not for us, retransmitting...\n");
3643 send_prebuilt_message (message, t->next_hop, t);
3644 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3650 * Core handler for mesh network traffic toward the owner of a tunnel
3652 * @param cls closure
3653 * @param message message
3654 * @param peer peer identity this notification is about
3656 * @return GNUNET_OK to keep the connection open,
3657 * GNUNET_SYSERR to close it (signal serious error)
3660 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3661 const struct GNUNET_MessageHeader *message)
3663 struct GNUNET_MESH_Data *msg;
3664 struct MeshTunnel *t;
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3671 size = ntohs (message->size);
3672 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3673 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3675 GNUNET_break_op (0);
3678 msg = (struct GNUNET_MESH_Data *) message;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3680 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3681 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3682 pid = ntohl (msg->pid);
3686 /* TODO notify that we dont know this tunnel (whom)? */
3687 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3689 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3690 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3694 if (t->next_fc.last_pid_recv == pid)
3696 /* already seen this packet, drop */
3697 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3699 " Already seen pid %u, DROPPING!\n", pid);
3700 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3704 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3706 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3707 GNUNET_break_op (0);
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3709 "Received PID %u, ACK %u\n",
3710 pid, t->next_fc.last_ack_sent);
3711 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3714 t->next_fc.last_pid_recv = pid;
3715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3716 " pid %u not seen yet, forwarding\n", pid);
3718 if (myid == t->id.oid)
3720 /* TODO signature verification */
3721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3722 " it's for us! sending to clients...\n");
3723 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3724 tunnel_send_client_to_orig (t, msg);
3725 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3729 " not for us, retransmitting...\n");
3731 if (0 == t->prev_hop) /* No owner AND no prev hop */
3733 if (GNUNET_YES == t->destroy)
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3736 "to orig received on a dying tunnel %s [%X]\n",
3737 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3741 "unknown to origin at %s\n",
3742 GNUNET_i2s (&my_full_id));
3743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3747 "on tunnel %s [%X]\n",
3748 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3751 ttl = ntohl (msg->ttl);
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3755 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3756 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3757 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3760 send_prebuilt_message (message, t->prev_hop, t);
3761 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3768 * Core handler for mesh network traffic end-to-end ACKs.
3770 * @param cls Closure.
3771 * @param message Message.
3772 * @param peer Peer identity this notification is about.
3774 * @return GNUNET_OK to keep the connection open,
3775 * GNUNET_SYSERR to close it (signal serious error)
3778 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3779 const struct GNUNET_MessageHeader *message)
3781 struct GNUNET_MESH_DataACK *msg;
3782 struct GNUNET_CONTAINER_MultiHashMap32 *hm;
3783 struct MeshSentMessage *copy;
3784 struct MeshTunnel *t;
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a DATA ACK message from %s!\n",
3790 msg = (struct GNUNET_MESH_DataACK *) message;
3792 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3795 /* TODO notify that we dont know this tunnel (whom)? */
3796 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3799 ack = ntohl (msg->pid);
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3802 /* Is this a forward or backward ACK? */
3803 id = GNUNET_PEER_search (peer);
3804 if (t->next_hop == id)
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3807 if (NULL == t->owner)
3809 send_prebuilt_message (message, t->prev_hop, t);
3812 hm = t->sent_messages_fwd;
3813 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3815 else if (t->prev_hop == id)
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3818 if (NULL == t->client)
3820 send_prebuilt_message (message, t->next_hop, t);
3823 hm = t->sent_messages_bck;
3824 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3827 GNUNET_break_op (0);
3829 copy = GNUNET_CONTAINER_multihashmap32_get (hm, ack);
3832 GNUNET_break (0); // FIXME needed?
3835 GNUNET_break (GNUNET_YES ==
3836 GNUNET_CONTAINER_multihashmap32_remove (hm, ack, copy));
3837 if (GNUNET_SCHEDULER_NO_TASK != copy->retry_task)
3839 GNUNET_SCHEDULER_cancel (copy->retry_task);
3848 * Core handler for mesh network traffic point-to-point acks.
3850 * @param cls closure
3851 * @param message message
3852 * @param peer peer identity this notification is about
3854 * @return GNUNET_OK to keep the connection open,
3855 * GNUNET_SYSERR to close it (signal serious error)
3858 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3859 const struct GNUNET_MessageHeader *message)
3861 struct GNUNET_MESH_ACK *msg;
3862 struct MeshTunnel *t;
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3868 msg = (struct GNUNET_MESH_ACK *) message;
3870 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3874 /* TODO notify that we dont know this tunnel (whom)? */
3875 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3878 ack = ntohl (msg->pid);
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3881 /* Is this a forward or backward ACK? */
3882 id = GNUNET_PEER_search (peer);
3883 if (t->next_hop == id)
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3887 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3888 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3890 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3891 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3892 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3894 t->next_fc.last_ack_recv = ack;
3895 peer_unlock_queue (t->next_hop);
3896 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3898 else if (t->prev_hop == id)
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3902 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3903 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3905 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3906 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3907 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3909 t->prev_fc.last_ack_recv = ack;
3910 peer_unlock_queue (t->prev_hop);
3911 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3914 GNUNET_break_op (0);
3920 * Core handler for mesh network traffic point-to-point ack polls.
3922 * @param cls closure
3923 * @param message message
3924 * @param peer peer identity this notification is about
3926 * @return GNUNET_OK to keep the connection open,
3927 * GNUNET_SYSERR to close it (signal serious error)
3930 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3931 const struct GNUNET_MessageHeader *message)
3933 struct GNUNET_MESH_Poll *msg;
3934 struct MeshTunnel *t;
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3940 msg = (struct GNUNET_MESH_Poll *) message;
3942 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3946 /* TODO notify that we dont know this tunnel (whom)? */
3947 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3948 GNUNET_break_op (0);
3952 /* Is this a forward or backward ACK? */
3953 id = GNUNET_PEER_search(peer);
3954 if (t->next_hop == id)
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3957 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3959 else if (t->prev_hop == id)
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3962 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3972 * Core handler for mesh keepalives.
3974 * @param cls closure
3975 * @param message message
3976 * @param peer peer identity this notification is about
3977 * @return GNUNET_OK to keep the connection open,
3978 * GNUNET_SYSERR to close it (signal serious error)
3980 * TODO: Check who we got this from, to validate route.
3983 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3984 const struct GNUNET_MessageHeader *message)
3986 struct GNUNET_MESH_TunnelKeepAlive *msg;
3987 struct MeshTunnel *t;
3989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3992 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3993 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3997 /* TODO notify that we dont know that tunnel */
3998 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4003 tunnel_reset_timeout (t);
4004 if (NULL != t->client || 0 == t->next_hop)
4007 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4008 send_prebuilt_message (message, t->next_hop, t);
4015 * Functions to handle messages from core
4017 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4018 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4019 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4020 sizeof (struct GNUNET_MESH_PathBroken)},
4021 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4022 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4023 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4024 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4025 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK,
4026 sizeof (struct GNUNET_MESH_DataACK)},
4027 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4028 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4029 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4030 sizeof (struct GNUNET_MESH_ACK)},
4031 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4032 sizeof (struct GNUNET_MESH_Poll)},
4033 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4034 sizeof (struct GNUNET_MESH_PathACK)},
4040 /******************************************************************************/
4041 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4042 /******************************************************************************/
4047 * notify_client_connection_failure: notify a client that the connection to the
4048 * requested remote peer is not possible (for instance, no route found)
4049 * Function called when the socket is ready to queue more data. "buf" will be
4050 * NULL and "size" zero if the socket was closed for writing in the meantime.
4052 * @param cls closure
4053 * @param size number of bytes available in buf
4054 * @param buf where the callee should write the message
4055 * @return number of bytes written to buf
4058 notify_client_connection_failure (void *cls, size_t size, void *buf)
4061 struct MeshPeerInfo *peer_info;
4062 struct GNUNET_MESH_PeerControl *msg;
4063 struct GNUNET_PeerIdentity id;
4065 if (0 == size && NULL == buf)
4067 // TODO retry? cancel?
4071 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4072 peer_info = (struct MeshPeerInfo *) cls;
4073 msg = (struct GNUNET_MESH_PeerControl *) buf;
4074 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4075 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4076 // msg->tunnel_id = htonl(peer_info->t->tid);
4077 GNUNET_PEER_resolve (peer_info->id, &id);
4078 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4086 * Send keepalive packets for a tunnel.
4088 * @param cls Closure (tunnel for which to send the keepalive).
4089 * @param tc Notification context.
4091 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4094 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4096 struct MeshTunnel *t = cls;
4097 struct GNUNET_MESH_TunnelKeepAlive *msg;
4098 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4101 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4102 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4103 NULL == t->owner || 0 == t->local_tid)
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4109 "sending keepalive for tunnel %d\n", t->id.tid);
4111 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4112 msg->header.size = htons (size);
4113 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4114 msg->oid = my_full_id;
4115 msg->tid = htonl (t->id.tid);
4116 send_prebuilt_message (&msg->header, t->next_hop, t);
4118 t->maintenance_task =
4119 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4124 * Function to process paths received for a new peer addition. The recorded
4125 * paths form the initial tunnel, which can be optimized later.
4126 * Called on each result obtained for the DHT search.
4128 * @param cls closure
4129 * @param exp when will this value expire
4130 * @param key key of the result
4131 * @param get_path path of the get request
4132 * @param get_path_length lenght of get_path
4133 * @param put_path path of the put request
4134 * @param put_path_length length of the put_path
4135 * @param type type of the result
4136 * @param size number of bytes in data
4137 * @param data pointer to the result data
4140 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4141 const struct GNUNET_HashCode * key,
4142 const struct GNUNET_PeerIdentity *get_path,
4143 unsigned int get_path_length,
4144 const struct GNUNET_PeerIdentity *put_path,
4145 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4146 size_t size, const void *data)
4148 struct MeshPeerInfo *peer = cls;
4149 struct MeshPeerPath *p;
4150 struct GNUNET_PeerIdentity pi;
4153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4154 GNUNET_PEER_resolve (peer->id, &pi);
4155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4157 p = path_build_from_dht (get_path, get_path_length,
4158 put_path, put_path_length);
4159 path_add_to_peers (p, GNUNET_NO);
4161 for (i = 0; i < peer->ntunnels; i++)
4163 struct GNUNET_PeerIdentity id;
4165 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4167 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4168 peer->tunnels[i]->local_tid,
4169 peer->tunnels[i]->local_tid_dest);
4170 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4173 peer_connect (peer, peer->tunnels[i]);
4181 /******************************************************************************/
4182 /********************* MESH LOCAL HANDLES **************************/
4183 /******************************************************************************/
4187 * Handler for client disconnection
4189 * @param cls closure
4190 * @param client identification of the client; NULL
4191 * for the last call when the server is destroyed
4194 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4196 struct MeshClient *c;
4197 struct MeshClient *next;
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4209 if (c->handle != client)
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4212 " ... searching %p (%u)\n",
4217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4219 GNUNET_SERVER_client_drop (c->handle);
4220 c->shutting_down = GNUNET_YES;
4221 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4222 &tunnel_destroy_iterator, c);
4223 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4224 &tunnel_destroy_iterator, c);
4225 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4226 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4228 if (NULL != c->ports)
4229 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
4231 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4234 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4243 * Handler for new clients
4245 * @param cls closure
4246 * @param client identification of the client
4247 * @param message the actual message, which includes messages the client wants
4250 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4251 const struct GNUNET_MessageHeader *message)
4253 struct GNUNET_MESH_ClientConnect *cc_msg;
4254 struct MeshClient *c;
4259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4261 /* Check data sanity */
4262 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4263 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4264 if (0 != (size % sizeof (uint32_t)))
4267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4270 size /= sizeof (uint32_t);
4272 /* Create new client structure */
4273 c = GNUNET_malloc (sizeof (struct MeshClient));
4274 c->id = next_client_id++; /* overflow not important: just for debug */
4275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4278 GNUNET_SERVER_client_keep (client);
4282 struct GNUNET_HashCode hc;
4284 p = (uint32_t *) &cc_msg[1];
4285 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
4286 for (i = 0; i < size; i++)
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4290 GMC_hash32 (u32, &hc);
4292 /* store in client's hashmap */
4293 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
4294 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4295 /* store in global hashmap */
4296 /* FIXME only allow one client to have the port open,
4297 * have a backup hashmap with waiting clients */
4298 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
4299 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4303 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4304 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4305 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4306 GNUNET_SERVER_notification_context_add (nc, client);
4307 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4315 * Handler for requests of new tunnels
4317 * @param cls Closure.
4318 * @param client Identification of the client.
4319 * @param message The actual message.
4322 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4323 const struct GNUNET_MessageHeader *message)
4325 struct GNUNET_MESH_TunnelMessage *t_msg;
4326 struct MeshPeerInfo *peer_info;
4327 struct MeshTunnel *t;
4328 struct MeshClient *c;
4329 MESH_TunnelNumber tid;
4331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4333 /* Sanity check for client registration */
4334 if (NULL == (c = client_get (client)))
4337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4342 /* Message size sanity check */
4343 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4350 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4352 GNUNET_i2s (&t_msg->peer));
4353 /* Sanity check for tunnel numbering */
4354 tid = ntohl (t_msg->tunnel_id);
4355 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4361 /* Sanity check for duplicate tunnel IDs */
4362 if (NULL != tunnel_get_by_local_id (c, tid))
4365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4370 while (NULL != tunnel_get_by_pi (myid, next_tid))
4371 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4372 t = tunnel_new (myid, next_tid, c, tid);
4373 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4380 t->port = ntohl (t_msg->port);
4381 tunnel_set_options (t, ntohl (t_msg->options));
4382 if (GNUNET_YES == t->reliable)
4383 t->sent_messages_fwd =
4384 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4386 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4388 peer_info = peer_get (&t_msg->peer);
4389 peer_info_add_tunnel (peer_info, t);
4390 peer_connect (peer_info, t);
4391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4397 * Handler for requests of deleting tunnels
4399 * @param cls closure
4400 * @param client identification of the client
4401 * @param message the actual message
4404 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4405 const struct GNUNET_MessageHeader *message)
4407 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4408 struct MeshClient *c;
4409 struct MeshTunnel *t;
4410 MESH_TunnelNumber tid;
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4413 "Got a DESTROY TUNNEL from client!\n");
4415 /* Sanity check for client registration */
4416 if (NULL == (c = client_get (client)))
4419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4424 /* Message sanity check */
4425 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4432 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4434 /* Retrieve tunnel */
4435 tid = ntohl (tunnel_msg->tunnel_id);
4436 t = tunnel_get_by_local_id(c, tid);
4439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4445 /* Cleanup after the tunnel */
4446 client_delete_tunnel (c, t);
4451 else if (c == t->owner)
4453 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4457 /* The tunnel will be destroyed when the last message is transmitted. */
4458 tunnel_destroy_empty (t);
4460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4466 * Handler for client traffic directed to one peer
4468 * @param cls closure
4469 * @param client identification of the client
4470 * @param message the actual message
4473 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4474 const struct GNUNET_MessageHeader *message)
4476 struct MeshClient *c;
4477 struct MeshTunnel *t;
4478 struct GNUNET_MESH_Data *data_msg;
4479 MESH_TunnelNumber tid;
4482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4483 "Got a unicast request from a client!\n");
4485 /* Sanity check for client registration */
4486 if (NULL == (c = client_get (client)))
4489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4494 data_msg = (struct GNUNET_MESH_Data *) message;
4496 /* Sanity check for message size */
4497 size = ntohs (message->size);
4498 if (sizeof (struct GNUNET_MESH_Data) +
4499 sizeof (struct GNUNET_MessageHeader) > size)
4502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4506 /* Tunnel exists? */
4507 tid = ntohl (data_msg->tid);
4508 t = tunnel_get_by_local_id (c, tid);
4512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4516 /* Is it a local tunnel? Then, does client own the tunnel? */
4517 if (t->owner->handle != client)
4520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4524 /* PID should be as expected: client<->service communication */
4525 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4529 "Unicast PID, expected %u, got %u\n",
4530 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4535 /* Ok, everything is correct, send the message
4536 * (pretend we got it from a mesh peer)
4539 struct MeshSentMessage *copy;
4540 struct GNUNET_MESH_Data *payload;
4542 copy = GNUNET_malloc (sizeof (struct MeshSentMessage) + size);
4543 copy->id = ntohl (data_msg->pid);
4544 copy->is_forward = GNUNET_YES;
4545 copy->retry_timer = GNUNET_TIME_UNIT_MINUTES;
4546 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4547 &tunnel_retransmit_message,
4549 if (GNUNET_YES == t->reliable &&
4551 GNUNET_CONTAINER_multihashmap32_put (t->sent_messages_fwd,
4554 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4560 payload = (struct GNUNET_MESH_Data *) ©[1];
4561 memcpy (payload, data_msg, size);
4562 payload->oid = my_full_id;
4563 payload->tid = htonl (t->id.tid);
4564 payload->ttl = htonl (default_ttl);
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 " calling generic handler...\n");
4567 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4570 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4577 * Handler for client traffic directed to the origin
4579 * @param cls closure
4580 * @param client identification of the client
4581 * @param message the actual message
4584 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4585 const struct GNUNET_MessageHeader *message)
4587 struct GNUNET_MESH_Data *data_msg;
4588 struct MeshFlowControl *fc;
4589 struct MeshClient *c;
4590 struct MeshTunnel *t;
4591 MESH_TunnelNumber tid;
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4595 "Got a ToOrigin request from a client!\n");
4596 /* Sanity check for client registration */
4597 if (NULL == (c = client_get (client)))
4600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4605 data_msg = (struct GNUNET_MESH_Data *) message;
4607 /* Sanity check for message size */
4608 size = ntohs (message->size);
4609 if (sizeof (struct GNUNET_MESH_Data) +
4610 sizeof (struct GNUNET_MessageHeader) > size)
4613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4617 /* Tunnel exists? */
4618 tid = ntohl (data_msg->tid);
4619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4620 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4626 t = tunnel_get_by_local_id (c, tid);
4629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4636 /* It should be sent by someone who has this as incoming tunnel. */
4640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4644 /* PID should be as expected */
4646 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4650 "To Origin PID, expected %u, got %u\n",
4651 fc->last_pid_recv + 1,
4652 ntohl (data_msg->pid));
4653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4657 /* Ok, everything is correct, send the message
4658 * (pretend we got it from a mesh peer)
4661 struct MeshSentMessage *copy;
4662 struct GNUNET_MESH_Data *payload;
4664 copy = GNUNET_malloc (sizeof (struct MeshSentMessage) + size);
4665 copy->id = ntohl (data_msg->pid);
4666 copy->is_forward = GNUNET_NO;
4667 copy->retry_timer = GNUNET_TIME_UNIT_MINUTES;
4668 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4669 &tunnel_retransmit_message,
4672 GNUNET_CONTAINER_multihashmap32_put (t->sent_messages_bck,
4675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4681 payload = (struct GNUNET_MESH_Data *) ©[1];
4682 memcpy (payload, data_msg, size);
4683 GNUNET_PEER_resolve (t->id.oid, &payload->oid);
4684 payload->tid = htonl (t->id.tid);
4685 payload->ttl = htonl (default_ttl);
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4688 " calling generic handler...\n");
4689 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4691 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4698 * Handler for client's ACKs for payload traffic.
4700 * @param cls Closure (unused).
4701 * @param client Identification of the client.
4702 * @param message The actual message.
4705 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4706 const struct GNUNET_MessageHeader *message)
4708 struct GNUNET_MESH_LocalAck *msg;
4709 struct MeshTunnel *t;
4710 struct MeshClient *c;
4711 MESH_TunnelNumber tid;
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4715 /* Sanity check for client registration */
4716 if (NULL == (c = client_get (client)))
4719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4724 msg = (struct GNUNET_MESH_LocalAck *) message;
4726 /* Tunnel exists? */
4727 tid = ntohl (msg->tunnel_id);
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4729 t = tunnel_get_by_local_id (c, tid);
4733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4734 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4739 ack = ntohl (msg->ack);
4740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4742 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4745 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4746 t->prev_fc.last_ack_recv = ack;
4747 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4751 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4752 t->next_fc.last_ack_recv = ack;
4753 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4764 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4766 * @param cls Closure (client handle).
4767 * @param key Key (hashed tunnel ID, unused).
4768 * @param value Tunnel info.
4770 * @return GNUNET_YES, to keep iterating.
4773 monitor_all_tunnels_iterator (void *cls,
4774 const struct GNUNET_HashCode * key,
4777 struct GNUNET_SERVER_Client *client = cls;
4778 struct MeshTunnel *t = value;
4779 struct GNUNET_MESH_LocalMonitor *msg;
4781 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4782 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4783 msg->tunnel_id = htonl (t->id.tid);
4784 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4785 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4786 GNUNET_PEER_resolve (t->dest, &msg->destination);
4788 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4789 "* sending info about tunnel %s [%u]\n",
4790 GNUNET_i2s (&msg->owner), t->id.tid);
4792 GNUNET_SERVER_notification_context_unicast (nc, client,
4793 &msg->header, GNUNET_NO);
4799 * Handler for client's MONITOR request.
4801 * @param cls Closure (unused).
4802 * @param client Identification of the client.
4803 * @param message The actual message.
4806 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4807 const struct GNUNET_MessageHeader *message)
4809 struct MeshClient *c;
4811 /* Sanity check for client registration */
4812 if (NULL == (c = client_get (client)))
4815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4819 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4820 "Received get tunnels request from client %u\n",
4822 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4823 monitor_all_tunnels_iterator,
4825 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4826 "Get tunnels request from client %u completed\n",
4828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4833 * Handler for client's MONITOR_TUNNEL request.
4835 * @param cls Closure (unused).
4836 * @param client Identification of the client.
4837 * @param message The actual message.
4840 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4841 const struct GNUNET_MessageHeader *message)
4843 const struct GNUNET_MESH_LocalMonitor *msg;
4844 struct GNUNET_MESH_LocalMonitor *resp;
4845 struct MeshClient *c;
4846 struct MeshTunnel *t;
4848 /* Sanity check for client registration */
4849 if (NULL == (c = client_get (client)))
4852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4856 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4857 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4858 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4861 ntohl (msg->tunnel_id));
4862 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4865 /* We don't know the tunnel FIXME */
4866 struct GNUNET_MESH_LocalMonitor warn;
4869 GNUNET_SERVER_notification_context_unicast (nc, client,
4872 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4876 /* Initialize context */
4877 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4879 GNUNET_PEER_resolve (t->dest, &resp->destination);
4880 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4881 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4882 &resp->header, GNUNET_NO);
4885 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4886 "Monitor tunnel request from client %u completed\n",
4888 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4893 * Functions to handle messages from clients
4895 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4896 {&handle_local_new_client, NULL,
4897 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4898 {&handle_local_tunnel_create, NULL,
4899 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4900 sizeof (struct GNUNET_MESH_TunnelMessage)},
4901 {&handle_local_tunnel_destroy, NULL,
4902 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4903 sizeof (struct GNUNET_MESH_TunnelMessage)},
4904 {&handle_local_unicast, NULL,
4905 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4906 {&handle_local_to_origin, NULL,
4907 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4908 {&handle_local_ack, NULL,
4909 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4910 sizeof (struct GNUNET_MESH_LocalAck)},
4911 {&handle_local_get_tunnels, NULL,
4912 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4913 sizeof (struct GNUNET_MessageHeader)},
4914 {&handle_local_show_tunnel, NULL,
4915 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4916 sizeof (struct GNUNET_MESH_LocalMonitor)},
4922 * Method called whenever a given peer connects.
4924 * @param cls closure
4925 * @param peer peer identity this notification is about
4928 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4930 struct MeshPeerInfo *peer_info;
4931 struct MeshPeerPath *path;
4933 DEBUG_CONN ("Peer connected\n");
4934 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4935 peer_info = peer_get (peer);
4936 if (myid == peer_info->id)
4938 DEBUG_CONN (" (self)\n");
4939 path = path_new (1);
4943 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4944 path = path_new (2);
4945 path->peers[1] = peer_info->id;
4946 GNUNET_PEER_change_rc (peer_info->id, 1);
4947 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4949 path->peers[0] = myid;
4950 GNUNET_PEER_change_rc (myid, 1);
4951 peer_info_add_path (peer_info, path, GNUNET_YES);
4957 * Method called whenever a peer disconnects.
4959 * @param cls closure
4960 * @param peer peer identity this notification is about
4963 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4965 struct MeshPeerInfo *pi;
4966 struct MeshPeerQueue *q;
4967 struct MeshPeerQueue *n;
4969 DEBUG_CONN ("Peer disconnected\n");
4970 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4980 /* TODO try to reroute this traffic instead */
4981 queue_destroy(q, GNUNET_YES);
4984 if (NULL != pi->core_transmit)
4986 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4987 pi->core_transmit = NULL;
4989 peer_remove_path (pi, pi->id, myid);
4992 DEBUG_CONN (" (self)\n");
4994 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5000 * Install server (service) handlers and start listening to clients.
5005 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5006 GNUNET_SERVER_disconnect_notify (server_handle,
5007 &handle_local_client_disconnect, NULL);
5008 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5010 clients_head = NULL;
5011 clients_tail = NULL;
5013 GNUNET_SERVER_resume (server_handle);
5018 * To be called on core init/fail.
5020 * @param cls Closure (config)
5021 * @param server handle to the server for this service
5022 * @param identity the public identity of this peer
5025 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5026 const struct GNUNET_PeerIdentity *identity)
5028 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5032 GNUNET_break (core_handle == server);
5033 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5039 GNUNET_i2s (identity));
5040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5042 GNUNET_i2s (&my_full_id));
5043 GNUNET_CORE_disconnect (core_handle);
5044 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5045 NULL, /* Closure passed to MESH functions */
5046 &core_init, /* Call core_init once connected */
5047 &core_connect, /* Handle connects */
5048 &core_disconnect, /* remove peers on disconnects */
5049 NULL, /* Don't notify about all incoming messages */
5050 GNUNET_NO, /* For header only in notification */
5051 NULL, /* Don't notify about all outbound messages */
5052 GNUNET_NO, /* For header-only out notification */
5053 core_handlers); /* Register these handlers */
5062 /******************************************************************************/
5063 /************************ MAIN FUNCTIONS ****************************/
5064 /******************************************************************************/
5067 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5069 * @param cls closure
5070 * @param key current key code
5071 * @param value value in the hash map
5072 * @return GNUNET_YES if we should continue to iterate,
5076 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5078 struct MeshTunnel *t = value;
5085 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5087 * @param cls closure
5088 * @param key current key code
5089 * @param value value in the hash map
5090 * @return GNUNET_YES if we should continue to iterate,
5094 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5096 struct MeshPeerInfo *p = value;
5097 struct MeshPeerQueue *q;
5098 struct MeshPeerQueue *n;
5106 queue_destroy(q, GNUNET_YES);
5110 peer_info_destroy (p);
5116 * Task run during shutdown.
5122 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5126 if (core_handle != NULL)
5128 GNUNET_CORE_disconnect (core_handle);
5133 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5136 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5137 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5138 if (dht_handle != NULL)
5140 GNUNET_DHT_disconnect (dht_handle);
5145 GNUNET_SERVER_notification_context_destroy (nc);
5148 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5150 GNUNET_SCHEDULER_cancel (announce_id_task);
5151 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5158 * Callback for hostkey read/generation.
5160 * @param cls Closure (Configuration handle).
5161 * @param pk The ECC private key.
5162 * @param emsg Error message, if any.
5165 key_generation_cb (void *cls,
5166 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5169 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5175 _("Could not access hostkey: %s. Exiting.\n"),
5177 GNUNET_SCHEDULER_shutdown ();
5180 my_private_key = pk;
5181 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5182 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5183 &my_full_id.hashPubKey);
5184 myid = GNUNET_PEER_intern (&my_full_id);
5185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5186 "Mesh for peer [%s] starting\n",
5187 GNUNET_i2s(&my_full_id));
5189 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5190 NULL, /* Closure passed to MESH functions */
5191 &core_init, /* Call core_init once connected */
5192 &core_connect, /* Handle connects */
5193 &core_disconnect, /* remove peers on disconnects */
5194 NULL, /* Don't notify about all incoming messages */
5195 GNUNET_NO, /* For header only in notification */
5196 NULL, /* Don't notify about all outbound messages */
5197 GNUNET_NO, /* For header-only out notification */
5198 core_handlers); /* Register these handlers */
5199 if (core_handle == NULL)
5202 GNUNET_SCHEDULER_shutdown ();
5207 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5209 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5216 * Process mesh requests.
5218 * @param cls closure
5219 * @param server the initialized server
5220 * @param c configuration to use
5223 run (void *cls, struct GNUNET_SERVER_Handle *server,
5224 const struct GNUNET_CONFIGURATION_Handle *c)
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5229 server_handle = server;
5230 GNUNET_SERVER_suspend (server_handle);
5233 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5238 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5239 "mesh", "peer/privatekey");
5240 GNUNET_SCHEDULER_shutdown ();
5245 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5246 &refresh_path_time))
5248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5250 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5251 "mesh", "refresh path time");
5252 GNUNET_SCHEDULER_shutdown ();
5257 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5262 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5263 "mesh", "id announce time");
5264 GNUNET_SCHEDULER_shutdown ();
5269 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5274 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5275 "mesh", "connect timeout");
5276 GNUNET_SCHEDULER_shutdown ();
5281 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5286 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5287 "mesh", "max msgs queue");
5288 GNUNET_SCHEDULER_shutdown ();
5293 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5298 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5299 "mesh", "max tunnels");
5300 GNUNET_SCHEDULER_shutdown ();
5305 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5310 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5311 "mesh", "default ttl", 64);
5316 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5320 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5321 "mesh", "max peers", 1000);
5326 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5334 "Mesh is running with drop mode enabled. "
5335 "This is NOT a good idea! "
5336 "Remove the DROP_PERCENT option from your configuration.\n");
5340 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5341 &dht_replication_level))
5343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5345 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5346 "mesh", "dht replication level", 3);
5347 dht_replication_level = 3;
5350 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5351 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5352 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5353 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5355 dht_handle = GNUNET_DHT_connect (c, 64);
5356 if (NULL == dht_handle)
5360 stats = GNUNET_STATISTICS_create ("mesh", c);
5362 /* Scheduled the task to clean up when shutdown is called */
5363 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5365 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5368 GNUNET_free (keyfile);
5373 * The main function for the mesh service.
5375 * @param argc number of arguments from the command line
5376 * @param argv command line arguments
5377 * @return 0 ok, 1 on error
5380 main (int argc, char *const *argv)
5385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5386 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5388 ret = (GNUNET_OK == r) ? 0 : 1;
5389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5393 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5394 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5395 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);