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,\
54 #define MESH_RETRANSMIT_TIME GNUNET_TIME_relative_multiply (\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define MESH_RETRANSMIT_MARGIN 4
59 #if MESH_DEBUG_CONNECTION
60 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
62 #define DEBUG_CONN(...)
66 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_DHT(...)
75 struct timespec __mesh_start;
76 struct timespec __mesh_end;
77 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
78 #define INTERVAL_END \
80 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
81 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
82 if (__diff < 0) __diff += 1000000000;\
86 #define INTERVAL_SHOW \
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
90 #define INTERVAL_START
95 /******************************************************************************/
96 /************************ DATA STRUCTURES ****************************/
97 /******************************************************************************/
99 /** FWD declaration */
105 * Struct containing info about a queued transmission to this peer
112 struct MeshPeerQueue *next;
117 struct MeshPeerQueue *prev;
120 * Peer this transmission is directed to.
122 struct MeshPeerInfo *peer;
125 * Tunnel this message belongs to.
127 struct MeshTunnel *tunnel;
130 * Pointer to info stucture used as cls.
140 * Size of the message
147 * Struct containing all information regarding a given peer
157 * Last time we heard from this peer
159 struct GNUNET_TIME_Absolute last_contact;
162 * Number of attempts to reconnect so far
164 int n_reconnect_attempts;
167 * Paths to reach the peer, ordered by ascending hop count
169 struct MeshPeerPath *path_head;
172 * Paths to reach the peer, ordered by ascending hop count
174 struct MeshPeerPath *path_tail;
177 * Handle to stop the DHT search for a path to this peer
179 struct GNUNET_DHT_GetHandle *dhtget;
182 * Array of tunnels this peer is the target of.
183 * Most probably a small amount, therefore not a hashmap.
184 * When the path to the peer changes, notify these tunnels to let them
185 * re-adjust their path trees.
187 struct MeshTunnel **tunnels;
190 * Number of tunnels this peers participates in
192 unsigned int ntunnels;
195 * Transmission queue to core DLL head
197 struct MeshPeerQueue *queue_head;
200 * Transmission queue to core DLL tail
202 struct MeshPeerQueue *queue_tail;
205 * How many messages are in the queue to this peer.
207 unsigned int queue_n;
210 * Handle for queued transmissions
212 struct GNUNET_CORE_TransmitHandle *core_transmit;
217 * Struct to encapsulate all the Flow Control information to a peer in the
218 * context of a tunnel: Same peer in different tunnels will have independent
219 * flow control structures, allowing to choke/free tunnels according to its
222 struct MeshFlowControl
225 * ID of the last packet sent towards the peer.
227 uint32_t last_pid_sent;
230 * ID of the last packet received from the peer.
232 uint32_t last_pid_recv;
235 * Last ACK sent to the peer (peer can't send more than this PID).
237 uint32_t last_ack_sent;
240 * Last ACK sent towards the origin (for traffic towards leaf node).
242 uint32_t last_ack_recv;
245 * How many payload messages are in the queue towards this peer.
250 * Task to poll the peer in case of a lost ACK causes stall.
252 GNUNET_SCHEDULER_TaskIdentifier poll_task;
255 * How frequently to poll for ACKs.
257 struct GNUNET_TIME_Relative poll_time;
260 * On which tunnel to poll.
261 * Using an explicit poll_ctx would not help memory wise,
262 * since the allocated context would have to be stored in the
263 * fc struct in order to free it upon cancelling poll_task.
265 struct MeshTunnel *t;
270 * Globally unique tunnel identification (owner + number)
271 * DO NOT USE OVER THE NETWORK
276 * Node that owns the tunnel
281 * Tunnel number to differentiate all the tunnels owned by the node oid
282 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
284 MESH_TunnelNumber tid;
289 * Info needed to retry a message in case it gets lost.
291 struct MeshReliableMessage
294 * Double linked list, FIFO style
296 struct MeshReliableMessage *next;
297 struct MeshReliableMessage *prev;
300 * Tunnel Reliability queue this message is in.
302 struct MeshTunnelReliability *rel;
305 * ID of the message (ACK needed to free)
310 * When was this message issued (to calculate ACK delay) FIXME update with traffic
312 struct GNUNET_TIME_Absolute timestamp;
314 /* struct GNUNET_MESH_Data with payload */
319 * Data needed for reliable tunnel endpoint retransmission management.
321 struct MeshTunnelReliability
324 * Tunnel this is about.
326 struct MeshTunnel *t;
329 * DLL of messages sent and not yet ACK'd.
331 struct MeshReliableMessage *head_sent;
332 struct MeshReliableMessage *tail_sent;
340 * DLL of messages received out of order.
342 struct MeshReliableMessage *head_recv;
343 struct MeshReliableMessage *tail_recv;
346 * Task to resend/poll in case no ACK is received.
348 GNUNET_SCHEDULER_TaskIdentifier retry_task;
351 * Counter for exponential backoff.
353 struct GNUNET_TIME_Relative retry_timer;
356 * How long does it usually take to get an ACK. FIXME update with traffic
358 struct GNUNET_TIME_Relative expected_delay;
363 * Struct containing all information regarding a tunnel
364 * For an intermediate node the improtant info used will be:
365 * - id Tunnel unique identification
366 * - paths[0] To know where to send it next
367 * - metainfo: ready, speeds, accounting
374 struct MESH_TunnelID id;
377 * Port of the tunnel.
382 * State of the tunnel.
384 enum MeshTunnelState state;
387 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
389 MESH_TunnelNumber local_tid;
392 * Local tunnel number for local destination clients (incoming number)
393 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
396 MESH_TunnelNumber local_tid_dest;
399 * Is the tunnel bufferless (minimum latency)?
404 * Is the tunnel reliable?
409 * Force sending ACK? Flag to allow duplicate ACK on POLL.
414 * How many messages do we accept in the forward queue.
416 unsigned int queue_max;
419 * Last time the tunnel was used
421 struct GNUNET_TIME_Absolute timestamp;
424 * Destination of the tunnel.
429 * Next hop in the tunnel. If 0, @c client must be set.
431 GNUNET_PEER_Id next_hop;
434 * Previous hop in the tunnel. If 0, @c owner must be set.
436 GNUNET_PEER_Id prev_hop;
439 * Flow control information about @c next_hop or @c client.
441 struct MeshFlowControl next_fc;
444 * Flow control information about @c prev_hop or @c owner.
446 struct MeshFlowControl prev_fc;
449 * Client owner of the tunnel, if any
451 struct MeshClient *owner;
454 * Client destination of the tunnel, if any.
456 struct MeshClient *client;
459 * Task to keep the used paths alive at the owner,
460 * time tunnel out on all the other peers.
462 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
465 * Path being used for the tunnel.
467 struct MeshPeerPath *path;
470 * Flag to signal the destruction of the tunnel.
471 * If this is set GNUNET_YES the tunnel will be destroyed
472 * when the queue is empty.
477 * Total messages pending for this tunnels, payload or not.
479 unsigned int pending_messages;
483 * Only present (non-NULL) at the owner of a tunnel.
485 struct MeshTunnelReliability *fwd_rel;
489 * Only present (non-NULL) at the destination of a tunnel.
491 struct MeshTunnelReliability *bck_rel;
496 * Struct containing information about a client of the service
498 * TODO: add a list of 'waiting' ports
505 struct MeshClient *next;
510 struct MeshClient *prev;
513 * Tunnels that belong to this client, indexed by local id
515 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
518 * Tunnels this client has accepted, indexed by incoming local id
520 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
523 * Handle to communicate with the client
525 struct GNUNET_SERVER_Client *handle;
528 * Ports that this client has declared interest in.
529 * Indexed by port, contains *Client.
531 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
534 * Whether the client is active or shutting down (don't send confirmations
535 * to a client that is shutting down.
540 * ID of the client, mainly for debug messages
547 /******************************************************************************/
548 /************************ DEBUG FUNCTIONS ****************************/
549 /******************************************************************************/
553 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
554 * @param cls string to print
555 * @param success GNUNET_OK if the PUT was transmitted,
556 * GNUNET_NO on timeout,
557 * GNUNET_SYSERR on disconnect from service
558 * after the PUT message was transmitted
559 * (so we don't know if it was received or not)
564 mesh_debug (void *cls, int success)
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
573 unsigned int debug_fwd_ack;
574 unsigned int debug_bck_ack;
578 /******************************************************************************/
579 /*********************** GLOBAL VARIABLES ****************************/
580 /******************************************************************************/
582 /************************** Configuration parameters **************************/
585 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
587 static struct GNUNET_TIME_Relative refresh_path_time;
590 * How often to PUT own ID in the DHT.
592 static struct GNUNET_TIME_Relative id_announce_time;
595 * Maximum time allowed to connect to a peer found by string.
597 static struct GNUNET_TIME_Relative connect_timeout;
600 * Default TTL for payload packets.
602 static unsigned long long default_ttl;
605 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
607 static unsigned long long dht_replication_level;
610 * How many tunnels are we willing to maintain.
611 * Local tunnels are always allowed, even if there are more tunnels than max.
613 static unsigned long long max_tunnels;
616 * How many messages *in total* are we willing to queue, divided by number of
617 * tunnels to get tunnel queue size.
619 static unsigned long long max_msgs_queue;
622 * How many peers do we want to remember?
624 static unsigned long long max_peers;
627 * Percentage of messages that will be dropped (for test purposes only).
629 static unsigned long long drop_percent;
631 /*************************** Static global variables **************************/
634 * DLL with all the clients, head.
636 static struct MeshClient *clients_head;
639 * DLL with all the clients, tail.
641 static struct MeshClient *clients_tail;
644 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
646 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
649 * Number of tunnels known.
651 static unsigned long long n_tunnels;
654 * Tunnels incoming, indexed by MESH_TunnelNumber
655 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
657 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
660 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
662 static struct GNUNET_CONTAINER_MultiHashMap *peers;
665 * Handle to communicate with transport
667 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
670 * Handle to communicate with core.
672 static struct GNUNET_CORE_Handle *core_handle;
677 static struct GNUNET_DHT_Handle *dht_handle;
682 static struct GNUNET_SERVER_Handle *server_handle;
685 * Handle to the statistics service.
687 static struct GNUNET_STATISTICS_Handle *stats;
690 * Notification context, to send messages to local clients.
692 static struct GNUNET_SERVER_NotificationContext *nc;
695 * Local peer own ID (memory efficient handle).
697 static GNUNET_PEER_Id myid;
700 * Local peer own ID (full value).
702 static struct GNUNET_PeerIdentity my_full_id;
707 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
712 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
715 * Tunnel ID for the next created tunnel (global tunnel number).
717 static MESH_TunnelNumber next_tid;
720 * Tunnel ID for the next incoming tunnel (local tunnel number).
722 static MESH_TunnelNumber next_local_tid;
725 * All ports clients of this peer have opened.
727 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
730 * Task to periodically announce itself in the network.
732 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
735 * Next ID to assign to a client.
737 unsigned int next_client_id;
740 /******************************************************************************/
741 /*********************** DECLARATIONS **************************/
742 /******************************************************************************/
745 * Function to process paths received for a new peer addition. The recorded
746 * paths form the initial tunnel, which can be optimized later.
747 * Called on each result obtained for the DHT search.
750 * @param exp when will this value expire
751 * @param key key of the result
752 * @param type type of the result
753 * @param size number of bytes in data
754 * @param data pointer to the result data
757 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
758 const struct GNUNET_HashCode * key,
759 const struct GNUNET_PeerIdentity *get_path,
760 unsigned int get_path_length,
761 const struct GNUNET_PeerIdentity *put_path,
762 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
763 size_t size, const void *data);
767 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
768 * and insert it in the appropriate structures if the peer is not known yet.
770 * @param peer Full identity of the peer.
772 * @return Existing or newly created peer info.
774 static struct MeshPeerInfo *
775 peer_get (const struct GNUNET_PeerIdentity *peer);
779 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
780 * and insert it in the appropriate structures if the peer is not known yet.
782 * @param peer Short identity of the peer.
784 * @return Existing or newly created peer info.
786 static struct MeshPeerInfo *
787 peer_get_short (const GNUNET_PEER_Id peer);
791 * Build a PeerPath from the paths returned from the DHT, reversing the paths
792 * to obtain a local peer -> destination path and interning the peer ids.
794 * @return Newly allocated and created path
796 static struct MeshPeerPath *
797 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
798 unsigned int get_path_length,
799 const struct GNUNET_PeerIdentity *put_path,
800 unsigned int put_path_length);
804 * Adds a path to the peer_infos of all the peers in the path
806 * @param p Path to process.
807 * @param confirmed Whether we know if the path works or not.
810 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
815 * Search for a tunnel by global ID using full PeerIdentities.
817 * @param oid owner of the tunnel.
818 * @param tid global tunnel number.
820 * @return tunnel handler, NULL if doesn't exist.
822 static struct MeshTunnel *
823 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
827 * Notify a tunnel that a connection has broken that affects at least
830 * @param t Tunnel affected.
831 * @param p1 Peer that got disconnected from p2.
832 * @param p2 Peer that got disconnected from p1.
834 * @return Short ID of the peer disconnected (either p1 or p2).
835 * 0 if the tunnel remained unaffected.
837 static GNUNET_PEER_Id
838 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
843 * @brief Use the given path for the tunnel.
844 * Update the next and prev hops (and RCs).
845 * (Re)start the path refresh in case the tunnel is locally owned.
847 * @param t Tunnel to update.
848 * @param p Path to use.
851 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
854 * Tunnel is empty: destroy it.
856 * Notifies all participants (peers, cleints) about the destruction.
858 * @param t Tunnel to destroy.
861 tunnel_destroy_empty (struct MeshTunnel *t);
864 * @brief Queue and pass message to core when possible.
866 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
867 * and accounts for it. In case the queue is full, the message is dropped and
870 * Otherwise, message is treated as internal and allowed to go regardless of
873 * @param cls Closure (@c type dependant). It will be used by queue_send to
874 * build the message to be sent if not already prebuilt.
875 * @param type Type of the message, 0 for a raw message.
876 * @param size Size of the message.
877 * @param dst Neighbor to send message to.
878 * @param t Tunnel this message belongs to.
881 queue_add (void *cls, uint16_t type, size_t size,
882 struct MeshPeerInfo *dst, struct MeshTunnel *t);
886 * Free a transmission that was already queued with all resources
887 * associated to the request.
889 * @param queue Queue handler to cancel.
890 * @param clear_cls Is it necessary to free associated cls?
893 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
897 * @brief Get the next transmittable message from the queue.
899 * This will be the head, except in the case of being a data packet
900 * not allowed by the destination peer.
902 * @param peer Destination peer.
904 * @return The next viable MeshPeerQueue element to send to that peer.
905 * NULL when there are no transmittable messages.
907 struct MeshPeerQueue *
908 queue_get_next (const struct MeshPeerInfo *peer);
912 * Core callback to write a queued packet to core buffer
914 * @param cls Closure (peer info).
915 * @param size Number of bytes available in buf.
916 * @param buf Where the to write the message.
918 * @return number of bytes written to buf
921 queue_send (void *cls, size_t size, void *buf);
924 /******************************************************************************/
925 /************************ PERIODIC FUNCTIONS ****************************/
926 /******************************************************************************/
929 * Periodically announce self id in the DHT
932 * @param tc task context
935 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
939 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
941 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
945 * - Set data expiration in function of X
948 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
950 block.id = my_full_id;
951 GNUNET_DHT_put (dht_handle, /* DHT handle */
952 &my_full_id.hashPubKey, /* Key to use */
953 dht_replication_level, /* Replication level */
954 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
955 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
956 sizeof (block), /* Size of the data */
957 (const char *) &block, /* Data itself */
958 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
959 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
960 NULL, /* Continuation */
961 NULL); /* Continuation closure */
963 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
967 /******************************************************************************/
968 /****************** GENERAL HELPER FUNCTIONS ************************/
969 /******************************************************************************/
973 * Check if client has registered with the service and has not disconnected
975 * @param client the client to check
977 * @return non-NULL if client exists in the global DLL
979 static struct MeshClient *
980 client_get (struct GNUNET_SERVER_Client *client)
982 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
987 * Deletes a tunnel from a client (either owner or destination). To be used on
990 * @param c Client whose tunnel to delete.
991 * @param t Tunnel which should be deleted.
994 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
998 GNUNET_assert (GNUNET_YES ==
999 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1005 GNUNET_assert (GNUNET_YES ==
1006 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1013 * Notify the appropriate client that a new incoming tunnel was created.
1015 * @param t Tunnel that was created.
1018 send_client_tunnel_create (struct MeshTunnel *t)
1020 struct GNUNET_MESH_TunnelMessage msg;
1022 if (NULL == t->client)
1024 msg.header.size = htons (sizeof (msg));
1025 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1026 msg.tunnel_id = htonl (t->local_tid_dest);
1027 msg.port = htonl (t->port);
1029 msg.options |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1030 msg.options |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1031 msg.options = htonl (msg.options);
1032 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1033 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1034 &msg.header, GNUNET_NO);
1039 * Notify dest client that the incoming tunnel is no longer valid.
1041 * @param c Client to notify..
1042 * @param t Tunnel that is destroyed.
1045 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1047 struct GNUNET_MESH_TunnelMessage msg;
1054 if (c != t->client && c != t->owner)
1059 msg.header.size = htons (sizeof (msg));
1060 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1061 msg.tunnel_id = htonl (t->local_tid_dest);
1062 msg.port = htonl (0);
1063 memset(&msg.peer, 0, sizeof (msg.peer));
1064 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1065 &msg.header, GNUNET_NO);
1070 * Iterator over all the peers to remove the oldest not-used entry.
1072 * @param cls Closure (unsued).
1073 * @param key ID of the peer.
1074 * @param value Peer_Info of the peer.
1079 peer_info_timeout (void *cls,
1080 const struct GNUNET_HashCode *key,
1087 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1088 * and insert it in the appropriate structures if the peer is not known yet.
1090 * @param peer Full identity of the peer.
1092 * @return Existing or newly created peer info.
1094 static struct MeshPeerInfo *
1095 peer_get (const struct GNUNET_PeerIdentity *peer)
1097 struct MeshPeerInfo *peer_info;
1099 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1100 if (NULL == peer_info)
1103 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1104 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1106 GNUNET_CONTAINER_multihashmap_iterate (peers,
1110 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1111 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1112 peer_info->id = GNUNET_PEER_intern (peer);
1114 peer_info->last_contact = GNUNET_TIME_absolute_get();
1121 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1122 * and insert it in the appropriate structures if the peer is not known yet.
1124 * @param peer Short identity of the peer.
1126 * @return Existing or newly created peer info.
1128 static struct MeshPeerInfo *
1129 peer_get_short (const GNUNET_PEER_Id peer)
1131 struct GNUNET_PeerIdentity id;
1133 GNUNET_PEER_resolve (peer, &id);
1134 return peer_get (&id);
1139 * Choose the best path towards a peer considering the tunnel properties.
1141 * @param peer The destination peer.
1142 * @param t The tunnel the path is for.
1144 * @return Best current known path towards the peer, if any.
1146 static struct MeshPeerPath *
1147 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1149 struct MeshPeerPath *best_p;
1150 struct MeshPeerPath *p;
1151 unsigned int best_cost;
1154 best_p = p = peer->path_head;
1155 best_cost = cost = p->length;
1158 if ((cost = p->length) < best_cost)
1169 * Core callback to write a pre-constructed data packet to core buffer
1171 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1172 * @param size Number of bytes available in buf.
1173 * @param buf Where the to write the message.
1175 * @return number of bytes written to buf
1178 send_core_data_raw (void *cls, size_t size, void *buf)
1180 struct GNUNET_MessageHeader *msg = cls;
1183 GNUNET_assert (NULL != msg);
1184 total_size = ntohs (msg->size);
1186 if (total_size > size)
1191 memcpy (buf, msg, total_size);
1198 * Sends an already built message to a peer, properly registrating
1199 * all used resources.
1201 * @param message Message to send. Function makes a copy of it.
1202 * @param peer Short ID of the neighbor whom to send the message.
1203 * @param t Tunnel on which this message is transmitted.
1206 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1207 GNUNET_PEER_Id peer,
1208 struct MeshTunnel *t)
1210 struct GNUNET_PeerIdentity id;
1211 struct MeshPeerInfo *neighbor;
1212 struct MeshPeerPath *p;
1217 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1222 size = ntohs (message->size);
1223 data = GNUNET_malloc (size);
1224 memcpy (data, message, size);
1225 type = ntohs(message->type);
1226 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1227 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1229 struct GNUNET_MESH_Data *u;
1231 u = (struct GNUNET_MESH_Data *) data;
1232 u->ttl = htonl (ntohl (u->ttl) - 1);
1234 GNUNET_PEER_resolve (peer, &id);
1235 neighbor = peer_get (&id);
1236 for (p = neighbor->path_head; NULL != p; p = p->next)
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 " %s IS NOT DIRECTLY CONNECTED\n",
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 for (p = neighbor->path_head; NULL != p; p = p->next)
1254 struct GNUNET_PeerIdentity debug_id;
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 " path with %u hops through:\n",
1260 for (i = 0; i < p->length; i++)
1262 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 i, GNUNET_i2s(&debug_id));
1269 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1271 " no direct connection to %s\n",
1276 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1287 * Sends a CREATE PATH message for a path to a peer, properly registrating
1288 * all used resources.
1290 * @param t Tunnel for which the path is created.
1293 send_create_path (struct MeshTunnel *t)
1295 struct MeshPeerInfo *neighbor;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1298 neighbor = peer_get_short (t->next_hop);
1300 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1301 sizeof (struct GNUNET_MESH_CreateTunnel) +
1302 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1305 t->state = MESH_TUNNEL_WAITING;
1310 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1312 * @param t Tunnel which to confirm.
1315 send_path_ack (struct MeshTunnel *t)
1317 struct MeshPeerInfo *peer;
1319 peer = peer_get_short (t->prev_hop);
1322 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1323 sizeof (struct GNUNET_MESH_PathACK),
1330 * Try to establish a new connection to this peer in the given tunnel.
1331 * If the peer doesn't have any path to it yet, try to get one.
1332 * If the peer already has some path, send a CREATE PATH towards it.
1334 * @param peer PeerInfo of the peer.
1335 * @param t Tunnel for which to create the path, if possible.
1338 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1340 struct MeshPeerPath *p;
1342 if (NULL != peer->path_head)
1344 p = peer_get_best_path (peer, t);
1345 tunnel_use_path (t, p);
1346 send_create_path (t);
1348 else if (NULL == peer->dhtget)
1350 struct GNUNET_PeerIdentity id;
1352 GNUNET_PEER_resolve (peer->id, &id);
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1355 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1356 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1357 &id.hashPubKey, /* key to search */
1358 dht_replication_level, /* replication level */
1359 GNUNET_DHT_RO_RECORD_ROUTE |
1360 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1362 0, /* xquery bits */
1363 &dht_get_id_handler, peer);
1364 t->state = MESH_TUNNEL_SEARCHING;
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 "There is no path but the DHT GET is already started.\n");
1375 * Destroy the peer_info and free any allocated resources linked to it
1377 * @param pi The peer_info to destroy.
1379 * @return GNUNET_OK on success
1382 peer_info_destroy (struct MeshPeerInfo *pi)
1384 struct GNUNET_PeerIdentity id;
1385 struct MeshPeerPath *p;
1386 struct MeshPeerPath *nextp;
1389 GNUNET_PEER_resolve (pi->id, &id);
1390 GNUNET_PEER_change_rc (pi->id, -1);
1393 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1397 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1399 if (NULL != pi->dhtget)
1401 GNUNET_DHT_get_stop (pi->dhtget);
1407 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1411 for (i = 0; i < pi->ntunnels; i++)
1412 tunnel_destroy_empty (pi->tunnels[i]);
1413 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1420 * Remove all paths that rely on a direct connection between p1 and p2
1421 * from the peer itself and notify all tunnels about it.
1423 * @param peer PeerInfo of affected peer.
1424 * @param p1 GNUNET_PEER_Id of one peer.
1425 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1428 * TODO: optimize (see below)
1431 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1434 struct MeshPeerPath *p;
1435 struct MeshPeerPath *next;
1436 struct MeshPeerInfo *peer_d;
1438 unsigned int destroyed;
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1443 for (p = peer->path_head; NULL != p; p = next)
1446 for (i = 0; i < (p->length - 1); i++)
1448 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1449 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1451 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1461 for (i = 0; i < peer->ntunnels; i++)
1463 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1467 peer_d = peer_get_short (d);
1468 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1469 tunnel_use_path (peer->tunnels[i], next);
1470 peer_connect (peer_d, peer->tunnels[i]);
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1477 * Add the path to the peer and update the path used to reach it in case this
1480 * @param peer_info Destination peer to add the path to.
1481 * @param path New path to add. Last peer must be the peer in arg 1.
1482 * Path will be either used of freed if already known.
1483 * @param trusted Do we trust that this path is real?
1486 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1489 struct MeshPeerPath *aux;
1493 if ((NULL == peer_info) || (NULL == path))
1496 path_destroy (path);
1499 if (path->peers[path->length - 1] != peer_info->id)
1502 path_destroy (path);
1505 if (2 >= path->length && GNUNET_NO == trusted)
1507 /* Only allow CORE to tell us about direct paths */
1508 path_destroy (path);
1511 for (l = 1; l < path->length; l++)
1513 if (path->peers[l] == myid)
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1516 for (l2 = 0; l2 < path->length - l; l2++)
1518 path->peers[l2] = path->peers[l + l2];
1523 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1528 struct GNUNET_PeerIdentity id;
1530 GNUNET_PEER_resolve (peer_info->id, &id);
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1532 path->length, GNUNET_i2s (&id));
1535 l = path_get_length (path);
1538 path_destroy (path);
1542 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1543 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1545 l2 = path_get_length (aux);
1548 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1549 peer_info->path_tail, aux, path);
1554 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1556 path_destroy (path);
1561 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1568 * Add the path to the origin peer and update the path used to reach it in case
1569 * this is the shortest.
1570 * The path is given in peer_info -> destination, therefore we turn the path
1571 * upside down first.
1573 * @param peer_info Peer to add the path to, being the origin of the path.
1574 * @param path New path to add after being inversed.
1575 * Path will be either used or freed.
1576 * @param trusted Do we trust that this path is real?
1579 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1580 struct MeshPeerPath *path, int trusted)
1583 peer_info_add_path (peer_info, path, trusted);
1588 * Add a tunnel to the list of tunnels a peer participates in.
1589 * Update the tunnel's destination.
1591 * @param p Peer to add to.
1592 * @param t Tunnel to add.
1595 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1599 GNUNET_break (t->dest == p->id);
1603 GNUNET_PEER_change_rc (t->dest, 1);
1604 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1609 * Remove a tunnel from the list of tunnels a peer participates in.
1610 * Free the tunnel's destination.
1612 * @param p Peer to clean.
1613 * @param t Tunnel to remove.
1616 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1620 if (t->dest == p->id)
1622 GNUNET_PEER_change_rc (t->dest, -1);
1625 for (i = 0; i < p->ntunnels; i++)
1627 if (p->tunnels[i] == t)
1629 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1630 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1638 * Function called if the connection to the peer has been stalled for a while,
1639 * possibly due to a missed ACK. Poll the peer about its ACK status.
1641 * @param cls Closure (poll ctx).
1642 * @param tc TaskContext.
1645 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1647 struct MeshFlowControl *fc = cls;
1648 struct GNUNET_MESH_Poll msg;
1649 struct MeshTunnel *t = fc->t;
1650 GNUNET_PEER_Id peer;
1652 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1653 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1660 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1661 msg.header.size = htons (sizeof (msg));
1662 msg.tid = htonl (t->id.tid);
1663 msg.pid = htonl (fc->last_pid_sent);
1664 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1666 if (fc == &t->prev_fc)
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1671 else if (fc == &t->next_fc)
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1681 send_prebuilt_message (&msg.header, peer, t);
1682 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1683 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1689 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1690 * to obtain a local peer -> destination path and interning the peer ids.
1692 * @return Newly allocated and created path
1694 static struct MeshPeerPath *
1695 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1696 unsigned int get_path_length,
1697 const struct GNUNET_PeerIdentity *put_path,
1698 unsigned int put_path_length)
1700 struct MeshPeerPath *p;
1706 GNUNET_PEER_change_rc (myid, 1);
1707 i = get_path_length;
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1709 for (i--; i >= 0; i--)
1711 id = GNUNET_PEER_intern (&get_path[i]);
1712 if (p->length > 0 && id == p->peers[p->length - 1])
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1715 GNUNET_PEER_change_rc (id, -1);
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1720 GNUNET_i2s (&get_path[i]));
1722 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1723 p->peers[p->length - 1] = id;
1726 i = put_path_length;
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1728 for (i--; i >= 0; i--)
1730 id = GNUNET_PEER_intern (&put_path[i]);
1733 /* PUT path went through us, so discard the path up until now and start
1734 * from here to get a much shorter (and loop-free) path.
1739 if (p->length > 0 && id == p->peers[p->length - 1])
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1742 GNUNET_PEER_change_rc (id, -1);
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1747 GNUNET_i2s (&put_path[i]));
1749 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1750 p->peers[p->length - 1] = id;
1754 if (get_path_length > 0)
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1756 GNUNET_i2s (&get_path[0]));
1757 if (put_path_length > 0)
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1759 GNUNET_i2s (&put_path[0]));
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1762 for (i = 0; i < p->length; i++)
1764 struct GNUNET_PeerIdentity peer_id;
1766 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1768 GNUNET_i2s (&peer_id));
1776 * Adds a path to the peer_infos of all the peers in the path
1778 * @param p Path to process.
1779 * @param confirmed Whether we know if the path works or not.
1782 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1786 /* TODO: invert and add */
1787 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1788 for (i++; i < p->length; i++)
1790 struct MeshPeerInfo *aux;
1791 struct MeshPeerPath *copy;
1793 aux = peer_get_short (p->peers[i]);
1794 copy = path_duplicate (p);
1795 copy->length = i + 1;
1796 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1802 * Send keepalive packets for a peer
1804 * @param cls Closure (tunnel for which to send the keepalive).
1805 * @param tc Notification context.
1808 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1812 * Search for a tunnel among the incoming tunnels
1814 * @param tid the local id of the tunnel
1816 * @return tunnel handler, NULL if doesn't exist
1818 static struct MeshTunnel *
1819 tunnel_get_incoming (MESH_TunnelNumber tid)
1821 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1822 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1827 * Search for a tunnel among the tunnels for a client
1829 * @param c the client whose tunnels to search in
1830 * @param tid the local id of the tunnel
1832 * @return tunnel handler, NULL if doesn't exist
1834 static struct MeshTunnel *
1835 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1837 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1839 return tunnel_get_incoming (tid);
1843 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1844 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1850 * Search for a tunnel by global ID using PEER_ID
1852 * @param pi owner of the tunnel
1853 * @param tid global tunnel number
1855 * @return tunnel handler, NULL if doesn't exist
1857 static struct MeshTunnel *
1858 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1860 struct MESH_TunnelID id;
1861 struct GNUNET_HashCode hash;
1866 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1867 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1872 * Search for a tunnel by global ID using full PeerIdentities
1874 * @param oid owner of the tunnel
1875 * @param tid global tunnel number
1877 * @return tunnel handler, NULL if doesn't exist
1879 static struct MeshTunnel *
1880 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1882 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1887 * Add a client to a tunnel, initializing all needed data structures.
1889 * @param t Tunnel to which add the client.
1890 * @param c Client which to add to the tunnel.
1893 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1895 if (NULL != t->client)
1901 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1902 t->local_tid_dest, t,
1903 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1909 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1910 t->local_tid_dest, t,
1911 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1921 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1923 unsigned int own_pos;
1925 for (own_pos = 0; own_pos < p->length; own_pos++)
1927 if (p->peers[own_pos] == myid)
1930 if (own_pos > p->length - 1)
1936 if (own_pos < p->length - 1)
1937 t->next_hop = p->peers[own_pos + 1];
1939 t->next_hop = p->peers[own_pos];
1940 GNUNET_PEER_change_rc (t->next_hop, 1);
1942 t->prev_hop = p->peers[own_pos - 1];
1944 t->prev_hop = p->peers[0];
1945 GNUNET_PEER_change_rc (t->prev_hop, 1);
1947 if (NULL != t->path)
1948 path_destroy (t->path);
1949 t->path = path_duplicate (p);
1952 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1953 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1954 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1961 * Notifies a tunnel that a connection has broken that affects at least
1962 * some of its peers. Sends a notification towards the root of the tree.
1963 * In case the peer is the owner of the tree, notifies the client that owns
1964 * the tunnel and tries to reconnect.
1966 * @param t Tunnel affected.
1967 * @param p1 Peer that got disconnected from p2.
1968 * @param p2 Peer that got disconnected from p1.
1970 * @return Short ID of the peer disconnected (either p1 or p2).
1971 * 0 if the tunnel remained unaffected.
1973 static GNUNET_PEER_Id
1974 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1977 // if (myid != p1 && myid != p2) FIXME
1982 // if (tree_get_predecessor (t->tree) != 0)
1984 // /* We are the peer still connected, notify owner of the disconnection. */
1985 // struct GNUNET_MESH_PathBroken msg;
1986 // struct GNUNET_PeerIdentity neighbor;
1988 // msg.header.size = htons (sizeof (msg));
1989 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1990 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1991 // msg.tid = htonl (t->id.tid);
1992 // msg.peer1 = my_full_id;
1993 // GNUNET_PEER_resolve (pid, &msg.peer2);
1994 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1995 // send_prebuilt_message (&msg.header, &neighbor, t);
2002 * Build a local ACK message and send it to a local client.
2004 * @param t Tunnel on which to send the ACK.
2005 * @param c Client to whom send the ACK.
2006 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2009 send_local_ack (struct MeshTunnel *t,
2010 struct MeshClient *c,
2013 struct GNUNET_MESH_LocalAck msg;
2015 msg.header.size = htons (sizeof (msg));
2016 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2017 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2018 GNUNET_SERVER_notification_context_unicast (nc,
2025 * Build an ACK message and queue it to send to the given peer.
2027 * @param t Tunnel on which to send the ACK.
2028 * @param peer Peer to whom send the ACK.
2029 * @param ack Value of the ACK.
2032 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2034 struct GNUNET_MESH_ACK msg;
2036 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2037 msg.header.size = htons (sizeof (msg));
2038 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2039 msg.pid = htonl (ack);
2040 msg.tid = htonl (t->id.tid);
2042 send_prebuilt_message (&msg.header, peer, t);
2047 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2049 * @param t Tunnel this is about.
2052 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2054 struct GNUNET_MESH_DataACK msg;
2056 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2057 msg.header.size = htons (sizeof (msg));
2058 msg.tid = htonl (t->id.tid);
2059 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2060 msg.pid = htonl (t->prev_fc.last_pid_recv);
2061 msg.futures = 0; // FIXME set bits of other newer messages received
2063 send_prebuilt_message (&msg.header, t->prev_hop, t);
2068 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2070 * @param t Tunnel this is about.
2073 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2075 struct GNUNET_MESH_DataACK msg;
2077 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2078 msg.header.size = htons (sizeof (msg));
2079 msg.tid = htonl (t->id.tid);
2080 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2081 msg.pid = htonl (t->next_fc.last_pid_recv);
2082 msg.futures = 0; // FIXME set bits of other newer messages received
2084 send_prebuilt_message (&msg.header, t->next_hop, t);
2089 * Send an ACK informing the predecessor about the available buffer space.
2090 * In case there is no predecessor, inform the owning client.
2091 * If buffering is off, send only on behalf of children or self if endpoint.
2092 * If buffering is on, send when sent to children and buffer space is free.
2093 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2094 * the ACK itself goes "back" (towards root).
2096 * @param t Tunnel on which to send the ACK.
2097 * @param type Type of message that triggered the ACK transmission.
2100 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2104 /* Is it after unicast retransmission? */
2107 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 "ACK due to FWD DATA retransmission\n");
2110 if (GNUNET_YES == t->nobuffer)
2112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2115 if (GNUNET_YES == t->reliable && NULL != t->client)
2116 tunnel_send_fwd_data_ack (t);
2117 if (GNUNET_YES == t->reliable && NULL != t->owner)
2118 if (t->fwd_rel->n_sent > 10)
2121 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2122 if (NULL != t->owner && GNUNET_YES == t->reliable)
2124 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2126 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2127 tunnel_send_fwd_data_ack (t);
2129 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2130 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2131 t->force_ack = GNUNET_YES;
2137 /* Check if we need to transmit the ACK */
2138 if (NULL == t->owner &&
2139 t->queue_max > t->next_fc.queue_n * 4 &&
2140 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2141 GNUNET_NO == t->force_ack)
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 " t->qmax: %u, t->qn: %u\n",
2146 t->queue_max, t->next_fc.queue_n);
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 " t->pid: %u, t->ack: %u\n",
2149 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2153 /* Ok, ACK might be necessary, what PID to ACK? */
2154 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2157 " last %u, qmax %u, q %u\n",
2158 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2159 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2165 t->prev_fc.last_ack_sent = ack;
2166 if (NULL != t->owner)
2167 send_local_ack (t, t->owner, GNUNET_YES);
2168 else if (0 != t->prev_hop)
2169 send_ack (t, t->prev_hop, ack);
2173 t->force_ack = GNUNET_NO;
2178 * Send an ACK informing the children node/client about the available
2180 * If buffering is off, send only on behalf of root (can be self).
2181 * If buffering is on, send when sent to predecessor and buffer space is free.
2182 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2183 * the ACK itself goes "forward" (towards children/clients).
2185 * @param t Tunnel on which to send the ACK.
2186 * @param type Type of message that triggered the ACK transmission.
2189 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2194 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2195 /* Is it after data to_origin retransmission? */
2198 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2199 if (GNUNET_YES == t->nobuffer)
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2202 " Not sending ACK, nobuffer + traffic\n");
2206 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2207 /* Why was this here?!
2208 * This prevents the destination from starting traffic to the origin
2210 // if (NULL != t->client && GNUNET_YES == t->reliable)
2212 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2214 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2215 tunnel_send_bck_data_ack (t);
2217 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2218 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2219 t->force_ack = GNUNET_YES;
2225 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2227 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2229 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 " Not sending ACK, not needed, last ack sent was %u\n",
2233 t->next_fc.last_ack_sent);
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2237 " Sending BCK ACK %u (last sent: %u)\n",
2238 ack, t->next_fc.last_ack_sent);
2239 t->next_fc.last_ack_sent = ack;
2241 if (NULL != t->client)
2242 send_local_ack (t, t->client, GNUNET_NO);
2243 else if (0 != t->next_hop)
2244 send_ack (t, t->next_hop, ack);
2247 t->force_ack = GNUNET_NO;
2252 * Modify the mesh message TID from global to local and send to client.
2254 * @param t Tunnel on which to send the message.
2255 * @param msg Message to modify and send.
2256 * @param c Client to send to.
2257 * @param tid Tunnel ID to use (c can be both owner and client).
2260 tunnel_send_client_data (struct MeshTunnel *t,
2261 const struct GNUNET_MESH_Data *msg,
2262 struct MeshClient *c, MESH_TunnelNumber tid)
2264 struct GNUNET_MESH_LocalData *copy;
2265 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2266 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2268 if (size < sizeof (struct GNUNET_MessageHeader))
2270 GNUNET_break_op (0);
2278 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2279 memcpy (©[1], &msg[1], size);
2280 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2281 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2282 copy->tid = htonl (tid);
2283 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2284 ©->header, GNUNET_NO);
2288 * Modify the unicast message TID from global to local and send to client.
2290 * @param t Tunnel on which to send the message.
2291 * @param msg Message to modify and send.
2294 tunnel_send_client_ucast (struct MeshTunnel *t,
2295 const struct GNUNET_MESH_Data *msg)
2297 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2302 * Modify the to_origin message TID from global to local and send to client.
2304 * @param t Tunnel on which to send the message.
2305 * @param msg Message to modify and send.
2308 tunnel_send_client_to_orig (struct MeshTunnel *t,
2309 const struct GNUNET_MESH_Data *msg)
2311 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2316 * We haven't received an ACK after a certain time: restransmit the message.
2318 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2319 * @param tc TaskContext.
2322 tunnel_retransmit_message (void *cls,
2323 const struct GNUNET_SCHEDULER_TaskContext *tc)
2325 struct MeshTunnelReliability *rel = cls;
2326 struct MeshReliableMessage *copy;
2327 struct MeshPeerQueue *q;
2328 struct MeshPeerInfo *pi;
2329 struct MeshTunnel *t;
2330 struct GNUNET_MESH_Data *payload;
2333 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2334 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2338 copy = rel->head_sent;
2345 /* Search the message to be retransmitted in the outgoing queue */
2346 payload = (struct GNUNET_MESH_Data *) ©[1];
2347 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2348 pi = peer_get_short (hop);
2349 for (q = pi->queue_head; NULL != q; q = q->next)
2351 if (ntohs (payload->header.type) == q->type)
2353 struct GNUNET_MESH_Data *queued_data = q->cls;
2355 if (queued_data->pid == payload->pid)
2360 /* Message not found in the queue */
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->id);
2364 copy->timestamp = GNUNET_TIME_absolute_get ();
2365 send_prebuilt_message (&payload->header, hop, t);
2366 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! QUEUED %u\n", copy->id);
2373 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2374 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2375 &tunnel_retransmit_message,
2381 * @brief Re-initiate traffic to this peer if necessary.
2383 * Check if there is traffic queued towards this peer
2384 * and the core transmit handle is NULL (traffic was stalled).
2385 * If so, call core tmt rdy.
2387 * @param peer_id Short ID of peer to which initiate traffic.
2390 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2392 struct MeshPeerInfo *peer;
2393 struct GNUNET_PeerIdentity id;
2394 struct MeshPeerQueue *q;
2397 peer = peer_get_short (peer_id);
2398 if (NULL != peer->core_transmit)
2401 q = queue_get_next (peer);
2404 /* Might br multicast traffic already sent to this particular peer but
2405 * not to other children in this tunnel.
2406 * This way t->queue_n would be > 0 but the queue of this particular peer
2412 GNUNET_PEER_resolve (peer->id, &id);
2413 peer->core_transmit =
2414 GNUNET_CORE_notify_transmit_ready(core_handle,
2417 GNUNET_TIME_UNIT_FOREVER_REL,
2427 * Send a message to all peers and clients in this tunnel that the tunnel
2428 * is no longer valid. If some peer or client should not receive the message,
2429 * should be zero'ed out before calling this function.
2431 * @param t The tunnel whose peers and clients to notify.
2434 tunnel_send_destroy (struct MeshTunnel *t)
2436 struct GNUNET_MESH_TunnelDestroy msg;
2437 struct GNUNET_PeerIdentity id;
2439 msg.header.size = htons (sizeof (msg));
2440 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2441 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2442 msg.tid = htonl (t->id.tid);
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 " sending tunnel destroy for tunnel: %s [%X]\n",
2445 GNUNET_i2s (&msg.oid), t->id.tid);
2447 if (NULL == t->client && 0 != t->next_hop)
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2450 GNUNET_PEER_resolve (t->next_hop, &id);
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 " sending forward to %s\n",
2454 send_prebuilt_message (&msg.header, t->next_hop, t);
2456 if (NULL == t->owner && 0 != t->prev_hop)
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2459 GNUNET_PEER_resolve (t->prev_hop, &id);
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 " sending back to %s\n",
2463 send_prebuilt_message (&msg.header, t->prev_hop, t);
2465 if (NULL != t->owner)
2467 send_client_tunnel_destroy (t->owner, t);
2469 if (NULL != t->client)
2471 send_client_tunnel_destroy (t->client, t);
2477 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2479 * @param t Tunnel which to cancel.
2480 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2483 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2485 struct MeshPeerInfo *peer_info;
2486 struct MeshPeerQueue *pq;
2487 struct MeshPeerQueue *next;
2490 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2491 peer_info = peer_get_short (neighbor);
2492 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2495 if (pq->tunnel == t)
2497 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2498 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2500 /* Should have been removed on destroy children */
2503 queue_destroy (pq, GNUNET_YES);
2506 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2508 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2509 peer_info->core_transmit = NULL;
2515 * Destroy the tunnel.
2517 * This function does not generate any warning traffic to clients or peers.
2520 * Remove the tunnel from peer_info's and clients' hashmaps.
2521 * Cancel messages belonging to this tunnel queued to neighbors.
2522 * Free any allocated resources linked to the tunnel.
2524 * @param t the tunnel to destroy
2526 * @return GNUNET_OK on success
2529 tunnel_destroy (struct MeshTunnel *t)
2531 struct MeshClient *c;
2532 struct GNUNET_HashCode hash;
2542 struct GNUNET_PeerIdentity id;
2544 GNUNET_PEER_resolve (t->id.oid, &id);
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2546 GNUNET_i2s (&id), t->id.tid);
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2552 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2553 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2562 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2569 if (NULL != t->client)
2573 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2574 t->local_tid_dest, t))
2580 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2581 t->local_tid_dest, t))
2588 if (0 != t->prev_hop)
2590 peer_cancel_queues (t->prev_hop, t);
2591 GNUNET_PEER_change_rc (t->prev_hop, -1);
2593 if (0 != t->next_hop)
2595 peer_cancel_queues (t->next_hop, t);
2596 GNUNET_PEER_change_rc (t->next_hop, -1);
2599 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2602 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2603 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2606 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2607 path_destroy (t->path);
2613 * Tunnel is empty: destroy it.
2615 * Notifies all participants (peers, cleints) about the destruction.
2617 * @param t Tunnel to destroy.
2620 tunnel_destroy_empty (struct MeshTunnel *t)
2624 struct GNUNET_PeerIdentity id;
2626 GNUNET_PEER_resolve (t->id.oid, &id);
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "executing destruction of empty tunnel %s [%X]\n",
2629 GNUNET_i2s (&id), t->id.tid);
2633 if (GNUNET_NO == t->destroy)
2634 tunnel_send_destroy (t);
2635 if (0 == t->pending_messages)
2638 t->destroy = GNUNET_YES;
2642 * Initialize a Flow Control structure to the initial state.
2644 * @param fc Flow Control structure to initialize.
2647 fc_init (struct MeshFlowControl *fc)
2649 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2650 fc->last_pid_recv = (uint32_t) -1;
2651 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2652 fc->last_ack_recv = (uint32_t) -1;
2653 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2654 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2659 * Create a new tunnel
2661 * @param owner Who is the owner of the tunnel (short ID).
2662 * @param tid Tunnel Number of the tunnel.
2663 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2664 * @param local Tunnel Number for the tunnel, for the client point of view.
2666 * @return A new initialized tunnel. NULL on error.
2668 static struct MeshTunnel *
2669 tunnel_new (GNUNET_PEER_Id owner,
2670 MESH_TunnelNumber tid,
2671 struct MeshClient *client,
2672 MESH_TunnelNumber local)
2674 struct MeshTunnel *t;
2675 struct GNUNET_HashCode hash;
2677 if (n_tunnels >= max_tunnels && NULL == client)
2680 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2683 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2685 fc_init (&t->next_fc);
2686 fc_init (&t->prev_fc);
2687 t->local_tid = local;
2689 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2691 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2693 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2701 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2709 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2711 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2715 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2725 * Set options in a tunnel, extracted from a bit flag field
2727 * @param t Tunnel to set options to.
2728 * @param options Bit array in host byte order.
2731 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2733 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2734 GNUNET_YES : GNUNET_NO;
2735 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2736 GNUNET_YES : GNUNET_NO;
2741 * Iterator for deleting each tunnel whose client endpoint disconnected.
2743 * @param cls Closure (client that has disconnected).
2744 * @param key The local tunnel id (used to access the hashmap).
2745 * @param value The value stored at the key (tunnel to destroy).
2747 * @return GNUNET_OK, keep iterating.
2750 tunnel_destroy_iterator (void *cls,
2754 struct MeshTunnel *t = value;
2755 struct MeshClient *c = cls;
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2758 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2759 t->local_tid, t->local_tid_dest, c->id);
2760 client_delete_tunnel (c, t);
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2765 if (0 != t->next_hop) { /* destroy could come before a path is used */
2766 GNUNET_PEER_change_rc (t->next_hop, -1);
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2774 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2775 GNUNET_PEER_change_rc (t->prev_hop, -1);
2780 tunnel_destroy_empty (t);
2787 * Timeout function, destroys tunnel if called
2789 * @param cls Closure (tunnel to destroy).
2790 * @param tc TaskContext
2793 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2795 struct MeshTunnel *t = cls;
2796 struct GNUNET_PeerIdentity id;
2798 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2799 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2801 GNUNET_PEER_resolve(t->id.oid, &id);
2802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2803 "Tunnel %s [%X] timed out. Destroying.\n",
2804 GNUNET_i2s(&id), t->id.tid);
2805 if (NULL != t->client)
2806 send_client_tunnel_destroy (t->client, t);
2807 tunnel_destroy (t); /* Do not notify other */
2812 * Resets the tunnel timeout. Starts it if no timeout was running.
2814 * @param t Tunnel whose timeout to reset.
2816 * TODO use heap to improve efficiency of scheduler.
2819 tunnel_reset_timeout (struct MeshTunnel *t)
2821 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2823 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2824 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2825 t->maintenance_task =
2826 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2827 (refresh_path_time, 4), &tunnel_timeout, t);
2831 /******************************************************************************/
2832 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2833 /******************************************************************************/
2836 * Function to send a create path packet to a peer.
2838 * @param cls closure
2839 * @param size number of bytes available in buf
2840 * @param buf where the callee should write the message
2841 * @return number of bytes written to buf
2844 send_core_path_create (void *cls, size_t size, void *buf)
2846 struct MeshTunnel *t = cls;
2847 struct GNUNET_MESH_CreateTunnel *msg;
2848 struct GNUNET_PeerIdentity *peer_ptr;
2849 struct MeshPeerPath *p = t->path;
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2856 sizeof (struct GNUNET_MESH_CreateTunnel) +
2857 p->length * sizeof (struct GNUNET_PeerIdentity);
2859 if (size < size_needed || NULL == buf)
2864 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2865 msg->header.size = htons (size_needed);
2866 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2867 msg->tid = ntohl (t->id.tid);
2870 if (GNUNET_YES == t->nobuffer)
2871 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2872 if (GNUNET_YES == t->reliable)
2873 opt |= GNUNET_MESH_OPTION_RELIABLE;
2874 msg->opt = htonl (opt);
2875 msg->port = htonl (t->port);
2877 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2878 for (i = 0; i < p->length; i++)
2880 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2884 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2890 * Creates a path ack message in buf and frees all unused resources.
2892 * @param cls closure (MeshTransmissionDescriptor)
2893 * @param size number of bytes available in buf
2894 * @param buf where the callee should write the message
2895 * @return number of bytes written to buf
2898 send_core_path_ack (void *cls, size_t size, void *buf)
2900 struct MeshTunnel *t = cls;
2901 struct GNUNET_MESH_PathACK *msg = buf;
2903 GNUNET_assert (NULL != t);
2904 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2909 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2910 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2911 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2912 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2913 msg->tid = htonl (t->id.tid);
2914 msg->peer_id = my_full_id;
2915 msg->ack = htonl (t->prev_fc.last_ack_sent);
2917 /* TODO add signature */
2919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2920 return sizeof (struct GNUNET_MESH_PathACK);
2925 * Free a transmission that was already queued with all resources
2926 * associated to the request.
2928 * @param queue Queue handler to cancel.
2929 * @param clear_cls Is it necessary to free associated cls?
2932 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2934 struct MeshFlowControl *fc;
2936 if (GNUNET_YES == clear_cls)
2938 switch (queue->type)
2940 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2941 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2942 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2944 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2945 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2946 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2947 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2948 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 " prebuilt message\n");
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2953 GNUNET_MESH_DEBUG_M2S (queue->type));
2955 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2960 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2961 " type %s unknown!\n",
2962 GNUNET_MESH_DEBUG_M2S (queue->type));
2964 GNUNET_free_non_null (queue->cls);
2966 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2967 queue->peer->queue_tail,
2970 /* Delete from appropriate fc in the tunnel */
2971 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2972 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2974 if (queue->peer->id == queue->tunnel->prev_hop)
2975 fc = &queue->tunnel->prev_fc;
2976 else if (queue->peer->id == queue->tunnel->next_hop)
2977 fc = &queue->tunnel->next_fc;
2981 GNUNET_free (queue);
2986 GNUNET_free (queue);
2991 * @brief Get the next transmittable message from the queue.
2993 * This will be the head, except in the case of being a data packet
2994 * not allowed by the destination peer.
2996 * @param peer Destination peer.
2998 * @return The next viable MeshPeerQueue element to send to that peer.
2999 * NULL when there are no transmittable messages.
3001 struct MeshPeerQueue *
3002 queue_get_next (const struct MeshPeerInfo *peer)
3004 struct MeshPeerQueue *q;
3006 struct GNUNET_MESH_Data *dmsg;
3007 struct MeshTunnel* t;
3011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3012 for (q = peer->queue_head; NULL != q; q = q->next)
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3017 GNUNET_MESH_DEBUG_M2S (q->type));
3018 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3019 pid = ntohl (dmsg->pid);
3022 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3023 ack = t->next_fc.last_ack_recv;
3025 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3026 ack = t->prev_fc.last_ack_recv;
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3034 "* ACK: %u, PID: %u\n",
3036 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3049 "* nothing found\n");
3055 queue_send (void *cls, size_t size, void *buf)
3057 struct MeshPeerInfo *peer = cls;
3058 struct GNUNET_MessageHeader *msg;
3059 struct MeshPeerQueue *queue;
3060 struct MeshTunnel *t;
3061 struct GNUNET_PeerIdentity dst_id;
3062 struct MeshFlowControl *fc;
3067 peer->core_transmit = NULL;
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3070 queue = queue_get_next (peer);
3072 /* Queue has no internal mesh traffic nor sendable payload */
3075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3076 if (NULL == peer->queue_head)
3077 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3082 GNUNET_PEER_resolve (peer->id, &dst_id);
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3085 GNUNET_i2s (&dst_id));
3086 /* Check if buffer size is enough for the message */
3087 if (queue->size > size)
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "* not enough room, reissue\n");
3091 peer->core_transmit =
3092 GNUNET_CORE_notify_transmit_ready (core_handle,
3095 GNUNET_TIME_UNIT_FOREVER_REL,
3102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3105 GNUNET_assert (0 < t->pending_messages);
3106 t->pending_messages--;
3110 switch (queue->type)
3113 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3114 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3115 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3116 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3117 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3118 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3119 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3120 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123 GNUNET_MESH_DEBUG_M2S (queue->type));
3125 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3126 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3127 data_size = send_core_data_raw (queue->cls, size, buf);
3128 msg = (struct GNUNET_MessageHeader *) buf;
3129 type = ntohs (msg->type);
3131 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3133 data_size = send_core_path_create (queue->cls, size, buf);
3135 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3137 data_size = send_core_path_ack (queue->cls, size, buf);
3141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3142 "* type unknown: %u\n",
3147 if (0 < drop_percent &&
3148 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3151 "Dropping message of type %s\n",
3152 GNUNET_MESH_DEBUG_M2S(queue->type));
3155 /* Free queue, but cls was freed by send_core_* */
3156 queue_destroy (queue, GNUNET_NO);
3158 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3159 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3163 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3164 if (GMC_is_pid_bigger(pid, t->next_fc.last_pid_sent))
3165 t->next_fc.last_pid_sent = pid;
3166 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3168 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3169 if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_sent))
3170 t->prev_fc.last_pid_sent = pid;
3171 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3177 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3183 /* If more data in queue, send next */
3184 queue = queue_get_next (peer);
3187 struct GNUNET_PeerIdentity id;
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3190 GNUNET_PEER_resolve (peer->id, &id);
3191 peer->core_transmit =
3192 GNUNET_CORE_notify_transmit_ready(core_handle,
3195 GNUNET_TIME_UNIT_FOREVER_REL,
3201 else if (NULL != peer->queue_head)
3203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3205 GNUNET_i2s (&my_full_id));
3206 if (peer->id == t->next_hop)
3208 else if (peer->id == t->prev_hop)
3215 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3218 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3228 * @brief Queue and pass message to core when possible.
3230 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3231 * accounts for it. In case the queue is full, the message is dropped and
3234 * Otherwise, message is treated as internal and allowed to go regardless of
3237 * @param cls Closure (@c type dependant). It will be used by queue_send to
3238 * build the message to be sent if not already prebuilt.
3239 * @param type Type of the message, 0 for a raw message.
3240 * @param size Size of the message.
3241 * @param dst Neighbor to send message to.
3242 * @param t Tunnel this message belongs to.
3245 queue_add (void *cls, uint16_t type, size_t size,
3246 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3248 struct MeshPeerQueue *queue;
3249 struct GNUNET_PeerIdentity id;
3250 struct MeshFlowControl *fc;
3256 priority = GNUNET_NO;
3257 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3260 pid = ntohl (((struct GNUNET_MESH_Data *)cls)->pid);
3262 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3265 pid = ntohl (((struct GNUNET_MESH_Data *)cls)->pid);
3269 if (fc->queue_n >= t->queue_max)
3272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3273 "queue full: %u/%u\n",
3274 fc->queue_n, t->queue_max);
3275 GNUNET_STATISTICS_update (stats,
3276 "# messages dropped (buffer full)",
3278 /* Get the PID of the oldest message in the queue */
3279 for (queue = dst->queue_head; queue != NULL; queue = queue->next)
3280 if (queue->type == type && queue->tunnel == t)
3282 pid_q = ntohl (((struct GNUNET_MESH_Data *)(queue->cls))->pid);
3283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3284 "pid: %u, pid_q: %u\n", pid, pid_q);
3287 GNUNET_assert (NULL != queue);
3289 /* If this is an earlier message that that, give it priority:
3290 * - drop the newest message in the queue
3291 * - instert current one at the end of the queue (first to get out)
3293 if (GNUNET_YES == t->reliable && GMC_is_pid_bigger(pid_q, pid))
3295 for (queue = dst->queue_tail; queue != NULL; queue = queue->prev)
3296 if (queue->type == type && queue->tunnel == t)
3298 /* Drop message from queue */
3299 pid_q = ntohl (((struct GNUNET_MESH_Data *)(queue->cls))->pid);
3300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3301 "dropping pid: %u\n", pid_q);
3302 queue_destroy (queue, GNUNET_YES);
3303 t->pending_messages--;
3304 priority = GNUNET_YES;
3309 return; /* Drop this message */
3313 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3319 if (GNUNET_YES == priority)
3320 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3322 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3323 if (NULL == dst->core_transmit)
3325 GNUNET_PEER_resolve (dst->id, &id);
3326 dst->core_transmit =
3327 GNUNET_CORE_notify_transmit_ready (core_handle,
3330 GNUNET_TIME_UNIT_FOREVER_REL,
3336 t->pending_messages++;
3340 /******************************************************************************/
3341 /******************** MESH NETWORK HANDLERS **************************/
3342 /******************************************************************************/
3346 * Core handler for path creation
3348 * @param cls closure
3349 * @param message message
3350 * @param peer peer identity this notification is about
3352 * @return GNUNET_OK to keep the connection open,
3353 * GNUNET_SYSERR to close it (signal serious error)
3356 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3357 const struct GNUNET_MessageHeader *message)
3359 unsigned int own_pos;
3362 MESH_TunnelNumber tid;
3363 struct GNUNET_MESH_CreateTunnel *msg;
3364 struct GNUNET_PeerIdentity *pi;
3365 struct MeshPeerPath *path;
3366 struct MeshPeerInfo *dest_peer_info;
3367 struct MeshPeerInfo *orig_peer_info;
3368 struct MeshTunnel *t;
3370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3371 "Received a path create msg [%s]\n",
3372 GNUNET_i2s (&my_full_id));
3373 size = ntohs (message->size);
3374 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3376 GNUNET_break_op (0);
3380 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3381 if (size % sizeof (struct GNUNET_PeerIdentity))
3383 GNUNET_break_op (0);
3386 size /= sizeof (struct GNUNET_PeerIdentity);
3389 GNUNET_break_op (0);
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3393 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3395 tid = ntohl (msg->tid);
3396 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3398 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3399 t = tunnel_get (pi, tid);
3400 if (NULL == t) /* might be a local tunnel */
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3405 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3411 t->port = ntohl (msg->port);
3412 opt = ntohl (msg->opt);
3413 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3415 t->nobuffer = GNUNET_YES;
3418 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3420 t->reliable = GNUNET_YES;
3422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3424 tunnel_reset_timeout (t);
3426 t->state = MESH_TUNNEL_WAITING;
3428 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3429 if (NULL == dest_peer_info)
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3432 " Creating PeerInfo for destination.\n");
3433 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3434 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3435 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3439 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3440 if (NULL == orig_peer_info)
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3443 " Creating PeerInfo for origin.\n");
3444 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3445 orig_peer_info->id = GNUNET_PEER_intern (pi);
3446 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3450 path = path_new (size);
3452 for (i = 0; i < size; i++)
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3455 GNUNET_i2s (&pi[i]));
3456 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3457 if (path->peers[i] == myid)
3460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3461 if (own_pos == 0 && path->peers[own_pos] != myid)
3463 /* create path: self not found in path through self */
3464 GNUNET_break_op (0);
3465 path_destroy (path);
3469 path_add_to_peers (path, GNUNET_NO);
3470 tunnel_use_path (t, path);
3472 peer_info_add_tunnel (dest_peer_info, t);
3474 if (own_pos == size - 1)
3476 struct MeshClient *c;
3478 /* Find target client */
3479 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3482 /* TODO send reject */
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3487 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3489 /* Assign local tid */
3490 while (NULL != tunnel_get_incoming (next_local_tid))
3491 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3492 t->local_tid_dest = next_local_tid++;
3493 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3495 if (GNUNET_YES == t->reliable)
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3498 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3500 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3503 tunnel_add_client (t, c);
3504 send_client_tunnel_create (t);
3509 struct MeshPeerPath *path2;
3511 t->next_hop = path->peers[own_pos + 1];
3512 GNUNET_PEER_change_rc(t->next_hop, 1);
3514 /* It's for somebody else! Retransmit. */
3515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3516 path2 = path_duplicate (path);
3517 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3518 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3519 send_create_path (t);
3527 * Core handler for path ACKs
3529 * @param cls closure
3530 * @param message message
3531 * @param peer peer identity this notification is about
3533 * @return GNUNET_OK to keep the connection open,
3534 * GNUNET_SYSERR to close it (signal serious error)
3537 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3538 const struct GNUNET_MessageHeader *message)
3540 struct GNUNET_MESH_PathACK *msg;
3541 struct MeshPeerInfo *peer_info;
3542 struct MeshPeerPath *p;
3543 struct MeshTunnel *t;
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3546 GNUNET_i2s (&my_full_id));
3547 msg = (struct GNUNET_MESH_PathACK *) message;
3548 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3551 /* TODO notify that we don't know the tunnel */
3552 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3554 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3558 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3560 peer_info = peer_get (&msg->peer_id);
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3562 GNUNET_i2s (&msg->peer_id));
3563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3566 /* Add path to peers? */
3570 path_add_to_peers (p, GNUNET_YES);
3576 t->state = MESH_TUNNEL_READY;
3577 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3578 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3580 /* Message for us? */
3581 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3584 if (NULL == t->owner)
3586 GNUNET_break_op (0);
3589 if (NULL != peer_info->dhtget)
3591 GNUNET_DHT_get_stop (peer_info->dhtget);
3592 peer_info->dhtget = NULL;
3594 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3595 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3600 " not for us, retransmitting...\n");
3601 peer_info = peer_get (&msg->oid);
3602 send_prebuilt_message (message, t->prev_hop, t);
3608 * Core handler for notifications of broken paths
3610 * @param cls closure
3611 * @param message message
3612 * @param peer peer identity this notification is about
3614 * @return GNUNET_OK to keep the connection open,
3615 * GNUNET_SYSERR to close it (signal serious error)
3618 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3619 const struct GNUNET_MessageHeader *message)
3621 struct GNUNET_MESH_PathBroken *msg;
3622 struct MeshTunnel *t;
3624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3625 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3626 msg = (struct GNUNET_MESH_PathBroken *) message;
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3628 GNUNET_i2s (&msg->peer1));
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3630 GNUNET_i2s (&msg->peer2));
3631 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3634 GNUNET_break_op (0);
3637 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3638 GNUNET_PEER_search (&msg->peer2));
3645 * Core handler for tunnel destruction
3647 * @param cls closure
3648 * @param message message
3649 * @param peer peer identity this notification is about
3651 * @return GNUNET_OK to keep the connection open,
3652 * GNUNET_SYSERR to close it (signal serious error)
3655 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3656 const struct GNUNET_MessageHeader *message)
3658 struct GNUNET_MESH_TunnelDestroy *msg;
3659 struct MeshTunnel *t;
3661 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 "Got a TUNNEL DESTROY packet from %s\n",
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3666 " for tunnel %s [%u]\n",
3667 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3668 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3671 /* Probably already got the message from another path,
3672 * destroyed the tunnel and retransmitted to children.
3675 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3679 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3682 t->local_tid, t->local_tid_dest);
3684 if (GNUNET_PEER_search (peer) == t->prev_hop)
3686 // TODO check owner's signature
3687 // TODO add owner's signatue to tunnel for retransmission
3688 peer_cancel_queues (t->prev_hop, t);
3689 GNUNET_PEER_change_rc (t->prev_hop, -1);
3692 else if (GNUNET_PEER_search (peer) == t->next_hop)
3694 // TODO check dest's signature
3695 // TODO add dest's signatue to tunnel for retransmission
3696 peer_cancel_queues (t->next_hop, t);
3697 GNUNET_PEER_change_rc (t->next_hop, -1);
3702 GNUNET_break_op (0);
3703 // TODO check both owner AND destination's signature to see which matches
3704 // TODO restransmit in appropriate direction
3707 tunnel_destroy_empty (t);
3709 // TODO: add timeout to destroy the tunnel anyway
3715 * Core handler for mesh network traffic going from the origin to a peer
3717 * @param cls closure
3718 * @param peer peer identity this notification is about
3719 * @param message message
3720 * @return GNUNET_OK to keep the connection open,
3721 * GNUNET_SYSERR to close it (signal serious error)
3724 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3725 const struct GNUNET_MessageHeader *message)
3727 struct GNUNET_MESH_Data *msg;
3728 struct MeshTunnel *t;
3733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3736 size = ntohs (message->size);
3738 sizeof (struct GNUNET_MESH_Data) +
3739 sizeof (struct GNUNET_MessageHeader))
3744 msg = (struct GNUNET_MESH_Data *) message;
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3746 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3748 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3751 /* TODO notify back: we don't know this tunnel */
3752 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3753 GNUNET_break_op (0);
3756 pid = ntohl (msg->pid);
3757 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3759 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3760 GNUNET_break_op (0);
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3762 "Received PID %u, ACK %u\n",
3763 pid, t->prev_fc.last_ack_sent);
3764 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3768 tunnel_reset_timeout (t);
3769 if (t->dest == myid)
3771 /* TODO signature verification */
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3773 " it's for us! sending to clients...\n");
3774 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3775 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3776 if ( (GNUNET_NO == t->reliable &&
3777 GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3779 (GNUNET_YES == t->reliable &&
3780 pid == t->prev_fc.last_pid_recv + 1) )
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3783 " pid %u not seen yet, forwarding\n", pid);
3784 t->prev_fc.last_pid_recv = pid;
3785 tunnel_send_client_ucast (t, msg);
3789 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3791 " Pid %u not expected (%u), sending FWD ACK!\n",
3792 pid, t->prev_fc.last_pid_recv + 1);
3794 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3797 if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3798 t->prev_fc.last_pid_recv = pid;
3799 if (0 == t->next_hop)
3804 ttl = ntohl (msg->ttl);
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3808 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3809 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3810 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3814 " not for us, retransmitting...\n");
3816 send_prebuilt_message (message, t->next_hop, t);
3817 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3823 * Core handler for mesh network traffic toward the owner of a tunnel
3825 * @param cls closure
3826 * @param message message
3827 * @param peer peer identity this notification is about
3829 * @return GNUNET_OK to keep the connection open,
3830 * GNUNET_SYSERR to close it (signal serious error)
3833 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3834 const struct GNUNET_MessageHeader *message)
3836 struct GNUNET_MESH_Data *msg;
3837 struct MeshTunnel *t;
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3844 size = ntohs (message->size);
3845 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3846 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3848 GNUNET_break_op (0);
3851 msg = (struct GNUNET_MESH_Data *) message;
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3853 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3854 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3855 pid = ntohl (msg->pid);
3858 /* TODO notify that we dont know this tunnel (whom)? */
3859 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3861 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3862 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3866 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3868 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3869 GNUNET_break_op (0);
3870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3871 "Received PID %u, ACK %u\n",
3872 pid, t->next_fc.last_ack_sent);
3873 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3877 if (myid == t->id.oid)
3879 /* TODO signature verification */
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3881 " it's for us! sending to clients...\n");
3882 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3883 if ( (GNUNET_NO == t->reliable &&
3884 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
3886 (GNUNET_YES == t->reliable &&
3887 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
3889 t->next_fc.last_pid_recv = pid;
3890 tunnel_send_client_to_orig (t, msg);
3894 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3895 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3896 " Pid %u not expected, sending FWD ACK!\n", pid);
3898 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 " not for us, retransmitting...\n");
3903 if (GMC_is_pid_bigger (pid, t->next_fc.last_pid_recv))
3904 t->next_fc.last_pid_recv = pid;
3905 if (0 == t->prev_hop) /* No owner AND no prev hop */
3907 if (GNUNET_YES == t->destroy)
3909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3910 "to orig received on a dying tunnel %s [%X]\n",
3911 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3915 "unknown to origin at %s\n",
3916 GNUNET_i2s (&my_full_id));
3917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3921 "on tunnel %s [%X]\n",
3922 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3925 ttl = ntohl (msg->ttl);
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3929 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3931 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3934 send_prebuilt_message (message, t->prev_hop, t);
3935 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3942 * Core handler for mesh network traffic end-to-end ACKs.
3944 * @param cls Closure.
3945 * @param message Message.
3946 * @param peer Peer identity this notification is about.
3948 * @return GNUNET_OK to keep the connection open,
3949 * GNUNET_SYSERR to close it (signal serious error)
3952 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3953 const struct GNUNET_MessageHeader *message)
3955 struct GNUNET_MESH_DataACK *msg;
3956 struct MeshTunnelReliability *rel;
3957 struct MeshReliableMessage *copy;
3958 struct MeshReliableMessage *next;
3959 struct MeshTunnel *t;
3965 type = ntohs (message->type);
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
3967 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
3968 msg = (struct GNUNET_MESH_DataACK *) message;
3970 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3973 /* TODO notify that we dont know this tunnel (whom)? */
3974 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3977 ack = ntohl (msg->pid);
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3980 /* Is this a forward or backward ACK? */
3981 id = GNUNET_PEER_search (peer);
3982 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3985 if (NULL == t->owner)
3987 send_prebuilt_message (message, t->prev_hop, t);
3991 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3993 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3996 if (NULL == t->client)
3998 send_prebuilt_message (message, t->next_hop, t);
4002 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4006 GNUNET_break_op (0);
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4011 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4013 struct GNUNET_TIME_Relative time;
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->id);
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->id);
4023 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
4024 rel->expected_delay.rel_value += time.rel_value;
4025 rel->expected_delay.rel_value /= 2;
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
4028 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
4030 rel->retry_timer = rel->expected_delay;
4031 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
4035 if (GNUNET_YES == work)
4037 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4039 GNUNET_SCHEDULER_cancel (rel->retry_task);
4040 if (NULL == rel->head_sent)
4042 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4046 struct GNUNET_TIME_Absolute new_target;
4047 struct GNUNET_TIME_Relative delay;
4049 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4051 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4053 GNUNET_SCHEDULER_add_delayed (delay,
4054 &tunnel_retransmit_message,
4065 * Core handler for mesh network traffic point-to-point acks.
4067 * @param cls closure
4068 * @param message message
4069 * @param peer peer identity this notification is about
4071 * @return GNUNET_OK to keep the connection open,
4072 * GNUNET_SYSERR to close it (signal serious error)
4075 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4076 const struct GNUNET_MessageHeader *message)
4078 struct GNUNET_MESH_ACK *msg;
4079 struct MeshTunnel *t;
4080 struct MeshFlowControl *fc;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4086 msg = (struct GNUNET_MESH_ACK *) message;
4088 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4092 /* TODO notify that we dont know this tunnel (whom)? */
4093 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4096 ack = ntohl (msg->pid);
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4099 /* Is this a forward or backward ACK? */
4100 id = GNUNET_PEER_search (peer);
4101 if (t->next_hop == id)
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4107 else if (t->prev_hop == id)
4110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4115 GNUNET_break_op (0);
4119 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4120 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4122 GNUNET_SCHEDULER_cancel (fc->poll_task);
4123 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4124 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4126 fc->last_ack_recv = ack;
4127 peer_unlock_queue (id);
4129 if (t->next_hop == id)
4130 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4132 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4139 * Core handler for mesh network traffic point-to-point ack polls.
4141 * @param cls closure
4142 * @param message message
4143 * @param peer peer identity this notification is about
4145 * @return GNUNET_OK to keep the connection open,
4146 * GNUNET_SYSERR to close it (signal serious error)
4149 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4150 const struct GNUNET_MessageHeader *message)
4152 struct GNUNET_MESH_Poll *msg;
4153 struct MeshTunnel *t;
4154 struct MeshFlowControl *fc;
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4162 msg = (struct GNUNET_MESH_Poll *) message;
4164 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4168 /* TODO notify that we dont know this tunnel (whom)? */
4169 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4170 GNUNET_break_op (0);
4174 /* Is this a forward or backward ACK? */
4175 id = GNUNET_PEER_search(peer);
4176 pid = ntohl (msg->pid);
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4178 if (t->next_hop == id)
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4183 old = fc->last_pid_recv;
4184 fc->last_pid_recv = pid;
4185 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4187 else if (t->prev_hop == id)
4189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4192 old = fc->last_pid_recv;
4193 fc->last_pid_recv = pid;
4194 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4199 if (GNUNET_YES == t->reliable)
4200 fc->last_pid_recv = old;
4207 * Core handler for mesh keepalives.
4209 * @param cls closure
4210 * @param message message
4211 * @param peer peer identity this notification is about
4212 * @return GNUNET_OK to keep the connection open,
4213 * GNUNET_SYSERR to close it (signal serious error)
4215 * TODO: Check who we got this from, to validate route.
4218 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4219 const struct GNUNET_MessageHeader *message)
4221 struct GNUNET_MESH_TunnelKeepAlive *msg;
4222 struct MeshTunnel *t;
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4227 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4228 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4232 /* TODO notify that we dont know that tunnel */
4233 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4238 tunnel_reset_timeout (t);
4239 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4242 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4243 send_prebuilt_message (message, t->next_hop, t);
4250 * Functions to handle messages from core
4252 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4253 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4254 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4255 sizeof (struct GNUNET_MESH_PathBroken)},
4256 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4257 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4258 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4259 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4260 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4261 sizeof (struct GNUNET_MESH_DataACK)},
4262 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4263 sizeof (struct GNUNET_MESH_DataACK)},
4264 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4265 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4266 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4267 sizeof (struct GNUNET_MESH_ACK)},
4268 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4269 sizeof (struct GNUNET_MESH_Poll)},
4270 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4271 sizeof (struct GNUNET_MESH_PathACK)},
4277 /******************************************************************************/
4278 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4279 /******************************************************************************/
4284 * notify_client_connection_failure: notify a client that the connection to the
4285 * requested remote peer is not possible (for instance, no route found)
4286 * Function called when the socket is ready to queue more data. "buf" will be
4287 * NULL and "size" zero if the socket was closed for writing in the meantime.
4289 * @param cls closure
4290 * @param size number of bytes available in buf
4291 * @param buf where the callee should write the message
4292 * @return number of bytes written to buf
4295 notify_client_connection_failure (void *cls, size_t size, void *buf)
4298 struct MeshPeerInfo *peer_info;
4299 struct GNUNET_MESH_PeerControl *msg;
4300 struct GNUNET_PeerIdentity id;
4302 if (0 == size && NULL == buf)
4304 // TODO retry? cancel?
4308 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4309 peer_info = (struct MeshPeerInfo *) cls;
4310 msg = (struct GNUNET_MESH_PeerControl *) buf;
4311 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4312 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4313 // msg->tunnel_id = htonl(peer_info->t->tid);
4314 GNUNET_PEER_resolve (peer_info->id, &id);
4315 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4323 * Send keepalive packets for a tunnel.
4325 * @param cls Closure (tunnel for which to send the keepalive).
4326 * @param tc Notification context.
4328 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4331 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4333 struct MeshTunnel *t = cls;
4334 struct GNUNET_MESH_TunnelKeepAlive *msg;
4335 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4338 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4339 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4340 NULL == t->owner || 0 == t->local_tid)
4345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4346 "sending keepalive for tunnel %d\n", t->id.tid);
4348 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4349 msg->header.size = htons (size);
4350 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4351 msg->oid = my_full_id;
4352 msg->tid = htonl (t->id.tid);
4353 send_prebuilt_message (&msg->header, t->next_hop, t);
4355 t->maintenance_task =
4356 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4361 * Function to process paths received for a new peer addition. The recorded
4362 * paths form the initial tunnel, which can be optimized later.
4363 * Called on each result obtained for the DHT search.
4365 * @param cls closure
4366 * @param exp when will this value expire
4367 * @param key key of the result
4368 * @param get_path path of the get request
4369 * @param get_path_length lenght of get_path
4370 * @param put_path path of the put request
4371 * @param put_path_length length of the put_path
4372 * @param type type of the result
4373 * @param size number of bytes in data
4374 * @param data pointer to the result data
4377 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4378 const struct GNUNET_HashCode * key,
4379 const struct GNUNET_PeerIdentity *get_path,
4380 unsigned int get_path_length,
4381 const struct GNUNET_PeerIdentity *put_path,
4382 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4383 size_t size, const void *data)
4385 struct MeshPeerInfo *peer = cls;
4386 struct MeshPeerPath *p;
4387 struct GNUNET_PeerIdentity pi;
4390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4391 GNUNET_PEER_resolve (peer->id, &pi);
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4394 p = path_build_from_dht (get_path, get_path_length,
4395 put_path, put_path_length);
4396 path_add_to_peers (p, GNUNET_NO);
4398 for (i = 0; i < peer->ntunnels; i++)
4400 struct GNUNET_PeerIdentity id;
4402 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4404 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4405 peer->tunnels[i]->local_tid,
4406 peer->tunnels[i]->local_tid_dest);
4407 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4410 peer_connect (peer, peer->tunnels[i]);
4418 /******************************************************************************/
4419 /********************* MESH LOCAL HANDLES **************************/
4420 /******************************************************************************/
4424 * Handler for client disconnection
4426 * @param cls closure
4427 * @param client identification of the client; NULL
4428 * for the last call when the server is destroyed
4431 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4433 struct MeshClient *c;
4434 struct MeshClient *next;
4436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4443 c = client_get (client);
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4448 GNUNET_SERVER_client_drop (c->handle);
4449 c->shutting_down = GNUNET_YES;
4450 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4451 &tunnel_destroy_iterator, c);
4452 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4453 &tunnel_destroy_iterator, c);
4454 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4455 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4457 if (NULL != c->ports)
4458 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4460 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4463 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4476 * Handler for new clients
4478 * @param cls closure
4479 * @param client identification of the client
4480 * @param message the actual message, which includes messages the client wants
4483 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4484 const struct GNUNET_MessageHeader *message)
4486 struct GNUNET_MESH_ClientConnect *cc_msg;
4487 struct MeshClient *c;
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4494 /* Check data sanity */
4495 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4496 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4497 if (0 != (size % sizeof (uint32_t)))
4500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4503 size /= sizeof (uint32_t);
4505 /* Create new client structure */
4506 c = GNUNET_malloc (sizeof (struct MeshClient));
4507 c->id = next_client_id++; /* overflow not important: just for debug */
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4511 GNUNET_SERVER_client_keep (client);
4512 GNUNET_SERVER_client_set_user_context (client, c);
4517 p = (uint32_t *) &cc_msg[1];
4518 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4519 for (i = 0; i < size; i++)
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4524 /* store in client's hashmap */
4525 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4527 /* store in global hashmap */
4528 /* FIXME only allow one client to have the port open,
4529 * have a backup hashmap with waiting clients */
4530 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4531 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4535 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4536 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4537 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4538 GNUNET_SERVER_notification_context_add (nc, client);
4539 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4541 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4547 * Handler for requests of new tunnels
4549 * @param cls Closure.
4550 * @param client Identification of the client.
4551 * @param message The actual message.
4554 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4555 const struct GNUNET_MessageHeader *message)
4557 struct GNUNET_MESH_TunnelMessage *t_msg;
4558 struct MeshPeerInfo *peer_info;
4559 struct MeshTunnel *t;
4560 struct MeshClient *c;
4561 MESH_TunnelNumber tid;
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4565 /* Sanity check for client registration */
4566 if (NULL == (c = client_get (client)))
4569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4574 /* Message size sanity check */
4575 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4582 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4584 GNUNET_i2s (&t_msg->peer));
4585 /* Sanity check for tunnel numbering */
4586 tid = ntohl (t_msg->tunnel_id);
4587 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4590 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4593 /* Sanity check for duplicate tunnel IDs */
4594 if (NULL != tunnel_get_by_local_id (c, tid))
4597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4602 while (NULL != tunnel_get_by_pi (myid, next_tid))
4603 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4604 t = tunnel_new (myid, next_tid, c, tid);
4605 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4609 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4612 t->port = ntohl (t_msg->port);
4613 tunnel_set_options (t, ntohl (t_msg->options));
4614 if (GNUNET_YES == t->reliable)
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4617 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4619 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4623 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4625 peer_info = peer_get (&t_msg->peer);
4626 peer_info_add_tunnel (peer_info, t);
4627 peer_connect (peer_info, t);
4628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4634 * Handler for requests of deleting tunnels
4636 * @param cls closure
4637 * @param client identification of the client
4638 * @param message the actual message
4641 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4642 const struct GNUNET_MessageHeader *message)
4644 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4645 struct MeshClient *c;
4646 struct MeshTunnel *t;
4647 MESH_TunnelNumber tid;
4649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4650 "Got a DESTROY TUNNEL from client!\n");
4652 /* Sanity check for client registration */
4653 if (NULL == (c = client_get (client)))
4656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4661 /* Message sanity check */
4662 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4669 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4671 /* Retrieve tunnel */
4672 tid = ntohl (tunnel_msg->tunnel_id);
4673 t = tunnel_get_by_local_id(c, tid);
4676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4682 /* Cleanup after the tunnel */
4683 client_delete_tunnel (c, t);
4690 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4694 /* The tunnel will be destroyed when the last message is transmitted. */
4695 tunnel_destroy_empty (t);
4697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4703 * Handler for client traffic
4705 * @param cls closure
4706 * @param client identification of the client
4707 * @param message the actual message
4710 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4711 const struct GNUNET_MessageHeader *message)
4713 struct GNUNET_MESH_LocalData *data_msg;
4714 struct MeshClient *c;
4715 struct MeshTunnel *t;
4716 struct MeshFlowControl *fc;
4717 MESH_TunnelNumber tid;
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4721 "Got data from a client!\n");
4723 /* Sanity check for client registration */
4724 if (NULL == (c = client_get (client)))
4727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4732 data_msg = (struct GNUNET_MESH_LocalData *) message;
4734 /* Sanity check for message size */
4735 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4736 if (size < sizeof (struct GNUNET_MessageHeader))
4739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4743 /* Tunnel exists? */
4744 tid = ntohl (data_msg->tid);
4745 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4751 t = tunnel_get_by_local_id (c, tid);
4755 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4759 /* Is the client in the tunnel? */
4760 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4762 t->owner->handle == client)
4764 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4766 t->client->handle == client) ) )
4769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4773 /* Ok, everything is correct, send the message
4774 * (pretend we got it from a mesh peer)
4777 struct GNUNET_MESH_Data *payload;
4778 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4780 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4781 if (GNUNET_YES == t->reliable)
4783 struct MeshTunnelReliability *rel;
4784 struct MeshReliableMessage *copy;
4786 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
4787 + sizeof(struct GNUNET_MESH_Data)
4789 copy->id = fc->last_pid_recv + 1;
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->id);
4791 copy->timestamp = GNUNET_TIME_absolute_get ();
4792 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
4795 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
4796 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
4799 GNUNET_TIME_relative_multiply (rel->expected_delay,
4800 MESH_RETRANSMIT_MARGIN);
4802 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
4803 &tunnel_retransmit_message,
4806 payload = (struct GNUNET_MESH_Data *) ©[1];
4810 payload = (struct GNUNET_MESH_Data *) cbuf;
4812 memcpy (&payload[1], &data_msg[1], size);
4813 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
4814 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4815 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
4816 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4817 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
4818 payload->tid = htonl (t->id.tid);
4819 payload->ttl = htonl (default_ttl);
4820 payload->pid = htonl (fc->last_pid_recv + 1);
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4822 " calling generic handler...\n");
4823 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4824 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4826 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4836 * Handler for client's ACKs for payload traffic.
4838 * @param cls Closure (unused).
4839 * @param client Identification of the client.
4840 * @param message The actual message.
4843 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4844 const struct GNUNET_MessageHeader *message)
4846 struct GNUNET_MESH_LocalAck *msg;
4847 struct MeshTunnel *t;
4848 struct MeshClient *c;
4849 MESH_TunnelNumber tid;
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4852 /* Sanity check for client registration */
4853 if (NULL == (c = client_get (client)))
4856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4861 msg = (struct GNUNET_MESH_LocalAck *) message;
4863 /* Tunnel exists? */
4864 tid = ntohl (msg->tunnel_id);
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4866 t = tunnel_get_by_local_id (c, tid);
4870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4876 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4879 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4880 t->prev_fc.last_ack_recv++;
4881 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4885 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4886 t->next_fc.last_ack_recv++;
4887 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4890 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4898 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4900 * @param cls Closure (client handle).
4901 * @param key Key (hashed tunnel ID, unused).
4902 * @param value Tunnel info.
4904 * @return GNUNET_YES, to keep iterating.
4907 monitor_all_tunnels_iterator (void *cls,
4908 const struct GNUNET_HashCode * key,
4911 struct GNUNET_SERVER_Client *client = cls;
4912 struct MeshTunnel *t = value;
4913 struct GNUNET_MESH_LocalMonitor *msg;
4915 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4916 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4917 msg->tunnel_id = htonl (t->id.tid);
4918 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4919 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4920 GNUNET_PEER_resolve (t->dest, &msg->destination);
4922 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4923 "* sending info about tunnel %s [%u]\n",
4924 GNUNET_i2s (&msg->owner), t->id.tid);
4926 GNUNET_SERVER_notification_context_unicast (nc, client,
4927 &msg->header, GNUNET_NO);
4933 * Handler for client's MONITOR request.
4935 * @param cls Closure (unused).
4936 * @param client Identification of the client.
4937 * @param message The actual message.
4940 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4941 const struct GNUNET_MessageHeader *message)
4943 struct MeshClient *c;
4945 /* Sanity check for client registration */
4946 if (NULL == (c = client_get (client)))
4949 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4953 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4954 "Received get tunnels request from client %u\n",
4956 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4957 monitor_all_tunnels_iterator,
4959 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4960 "Get tunnels request from client %u completed\n",
4962 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4967 * Handler for client's MONITOR_TUNNEL request.
4969 * @param cls Closure (unused).
4970 * @param client Identification of the client.
4971 * @param message The actual message.
4974 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4975 const struct GNUNET_MessageHeader *message)
4977 const struct GNUNET_MESH_LocalMonitor *msg;
4978 struct GNUNET_MESH_LocalMonitor *resp;
4979 struct MeshClient *c;
4980 struct MeshTunnel *t;
4982 /* Sanity check for client registration */
4983 if (NULL == (c = client_get (client)))
4986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4990 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4991 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4992 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4995 ntohl (msg->tunnel_id));
4996 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4999 /* We don't know the tunnel FIXME */
5000 struct GNUNET_MESH_LocalMonitor warn;
5003 GNUNET_SERVER_notification_context_unicast (nc, client,
5006 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5010 /* Initialize context */
5011 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5013 GNUNET_PEER_resolve (t->dest, &resp->destination);
5014 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5015 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5016 &resp->header, GNUNET_NO);
5019 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5020 "Monitor tunnel request from client %u completed\n",
5022 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5027 * Functions to handle messages from clients
5029 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5030 {&handle_local_new_client, NULL,
5031 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5032 {&handle_local_tunnel_create, NULL,
5033 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5034 sizeof (struct GNUNET_MESH_TunnelMessage)},
5035 {&handle_local_tunnel_destroy, NULL,
5036 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5037 sizeof (struct GNUNET_MESH_TunnelMessage)},
5038 {&handle_local_data, NULL,
5039 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5040 {&handle_local_ack, NULL,
5041 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5042 sizeof (struct GNUNET_MESH_LocalAck)},
5043 {&handle_local_get_tunnels, NULL,
5044 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5045 sizeof (struct GNUNET_MessageHeader)},
5046 {&handle_local_show_tunnel, NULL,
5047 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5048 sizeof (struct GNUNET_MESH_LocalMonitor)},
5054 * Method called whenever a given peer connects.
5056 * @param cls closure
5057 * @param peer peer identity this notification is about
5060 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5062 struct MeshPeerInfo *peer_info;
5063 struct MeshPeerPath *path;
5065 DEBUG_CONN ("Peer connected\n");
5066 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5067 peer_info = peer_get (peer);
5068 if (myid == peer_info->id)
5070 DEBUG_CONN (" (self)\n");
5071 path = path_new (1);
5075 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5076 path = path_new (2);
5077 path->peers[1] = peer_info->id;
5078 GNUNET_PEER_change_rc (peer_info->id, 1);
5079 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5081 path->peers[0] = myid;
5082 GNUNET_PEER_change_rc (myid, 1);
5083 peer_info_add_path (peer_info, path, GNUNET_YES);
5089 * Method called whenever a peer disconnects.
5091 * @param cls closure
5092 * @param peer peer identity this notification is about
5095 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5097 struct MeshPeerInfo *pi;
5098 struct MeshPeerQueue *q;
5099 struct MeshPeerQueue *n;
5101 DEBUG_CONN ("Peer disconnected\n");
5102 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5112 /* TODO try to reroute this traffic instead */
5113 queue_destroy(q, GNUNET_YES);
5116 if (NULL != pi->core_transmit)
5118 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5119 pi->core_transmit = NULL;
5121 peer_remove_path (pi, pi->id, myid);
5124 DEBUG_CONN (" (self)\n");
5126 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5132 * Install server (service) handlers and start listening to clients.
5137 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5138 GNUNET_SERVER_disconnect_notify (server_handle,
5139 &handle_local_client_disconnect, NULL);
5140 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5142 clients_head = NULL;
5143 clients_tail = NULL;
5145 GNUNET_SERVER_resume (server_handle);
5150 * To be called on core init/fail.
5152 * @param cls Closure (config)
5153 * @param server handle to the server for this service
5154 * @param identity the public identity of this peer
5157 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5158 const struct GNUNET_PeerIdentity *identity)
5160 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5164 GNUNET_break (core_handle == server);
5165 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5171 GNUNET_i2s (identity));
5172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5174 GNUNET_i2s (&my_full_id));
5175 GNUNET_CORE_disconnect (core_handle);
5176 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5177 NULL, /* Closure passed to MESH functions */
5178 &core_init, /* Call core_init once connected */
5179 &core_connect, /* Handle connects */
5180 &core_disconnect, /* remove peers on disconnects */
5181 NULL, /* Don't notify about all incoming messages */
5182 GNUNET_NO, /* For header only in notification */
5183 NULL, /* Don't notify about all outbound messages */
5184 GNUNET_NO, /* For header-only out notification */
5185 core_handlers); /* Register these handlers */
5194 /******************************************************************************/
5195 /************************ MAIN FUNCTIONS ****************************/
5196 /******************************************************************************/
5199 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5201 * @param cls closure
5202 * @param key current key code
5203 * @param value value in the hash map
5204 * @return GNUNET_YES if we should continue to iterate,
5208 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5210 struct MeshTunnel *t = value;
5217 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5219 * @param cls closure
5220 * @param key current key code
5221 * @param value value in the hash map
5222 * @return GNUNET_YES if we should continue to iterate,
5226 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5228 struct MeshPeerInfo *p = value;
5229 struct MeshPeerQueue *q;
5230 struct MeshPeerQueue *n;
5238 queue_destroy(q, GNUNET_YES);
5242 peer_info_destroy (p);
5248 * Task run during shutdown.
5254 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5258 if (core_handle != NULL)
5260 GNUNET_CORE_disconnect (core_handle);
5263 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5264 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5265 if (dht_handle != NULL)
5267 GNUNET_DHT_disconnect (dht_handle);
5272 GNUNET_SERVER_notification_context_destroy (nc);
5275 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5277 GNUNET_SCHEDULER_cancel (announce_id_task);
5278 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5285 * Process mesh requests.
5287 * @param cls closure
5288 * @param server the initialized server
5289 * @param c configuration to use
5292 run (void *cls, struct GNUNET_SERVER_Handle *server,
5293 const struct GNUNET_CONFIGURATION_Handle *c)
5296 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5299 server_handle = server;
5300 GNUNET_SERVER_suspend (server_handle);
5303 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5308 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5309 "mesh", "peer/privatekey");
5310 GNUNET_SCHEDULER_shutdown ();
5315 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5316 &refresh_path_time))
5318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5320 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5321 "mesh", "refresh path time");
5322 GNUNET_SCHEDULER_shutdown ();
5327 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5332 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5333 "mesh", "id announce time");
5334 GNUNET_SCHEDULER_shutdown ();
5339 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5344 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5345 "mesh", "connect timeout");
5346 GNUNET_SCHEDULER_shutdown ();
5351 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5356 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5357 "mesh", "max msgs queue");
5358 GNUNET_SCHEDULER_shutdown ();
5363 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5368 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5369 "mesh", "max tunnels");
5370 GNUNET_SCHEDULER_shutdown ();
5375 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5380 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5381 "mesh", "default ttl", 64);
5386 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5390 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5391 "mesh", "max peers", 1000);
5396 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5404 "Mesh is running with drop mode enabled. "
5405 "This is NOT a good idea! "
5406 "Remove the DROP_PERCENT option from your configuration.\n");
5410 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5411 &dht_replication_level))
5413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5415 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5416 "mesh", "dht replication level", 3);
5417 dht_replication_level = 3;
5420 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5421 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5422 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5423 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5425 dht_handle = GNUNET_DHT_connect (c, 64);
5426 if (NULL == dht_handle)
5430 stats = GNUNET_STATISTICS_create ("mesh", c);
5432 /* Scheduled the task to clean up when shutdown is called */
5433 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5435 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5436 GNUNET_free (keyfile);
5437 GNUNET_assert (NULL != pk);
5438 my_private_key = pk;
5439 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5440 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5441 &my_full_id.hashPubKey);
5442 myid = GNUNET_PEER_intern (&my_full_id);
5443 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5444 "Mesh for peer [%s] starting\n",
5445 GNUNET_i2s(&my_full_id));
5447 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5448 NULL, /* Closure passed to MESH functions */
5449 &core_init, /* Call core_init once connected */
5450 &core_connect, /* Handle connects */
5451 &core_disconnect, /* remove peers on disconnects */
5452 NULL, /* Don't notify about all incoming messages */
5453 GNUNET_NO, /* For header only in notification */
5454 NULL, /* Don't notify about all outbound messages */
5455 GNUNET_NO, /* For header-only out notification */
5456 core_handlers); /* Register these handlers */
5457 if (NULL == core_handle)
5460 GNUNET_SCHEDULER_shutdown ();
5464 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5465 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5471 * The main function for the mesh service.
5473 * @param argc number of arguments from the command line
5474 * @param argv command line arguments
5475 * @return 0 ok, 1 on error
5478 main (int argc, char *const *argv)
5483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5484 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5486 ret = (GNUNET_OK == r) ? 0 : 1;
5487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5492 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5493 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);