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;
335 * DLL of messages received out of order.
337 struct MeshReliableMessage *head_recv;
338 struct MeshReliableMessage *tail_recv;
341 * Task to resend/poll in case no ACK is received.
343 GNUNET_SCHEDULER_TaskIdentifier retry_task;
346 * Counter for exponential backoff.
348 struct GNUNET_TIME_Relative retry_timer;
351 * How long does it usually take to get an ACK. FIXME update with traffic
353 struct GNUNET_TIME_Relative expected_delay;
358 * Struct containing all information regarding a tunnel
359 * For an intermediate node the improtant info used will be:
360 * - id Tunnel unique identification
361 * - paths[0] To know where to send it next
362 * - metainfo: ready, speeds, accounting
369 struct MESH_TunnelID id;
372 * Port of the tunnel.
377 * State of the tunnel.
379 enum MeshTunnelState state;
382 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
384 MESH_TunnelNumber local_tid;
387 * Local tunnel number for local destination clients (incoming number)
388 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
391 MESH_TunnelNumber local_tid_dest;
394 * Is the tunnel bufferless (minimum latency)?
399 * Is the tunnel reliable?
404 * Force sending ACK? Flag to allow duplicate ACK on POLL.
409 * How many messages do we accept in the forward queue.
411 unsigned int queue_max;
414 * Last time the tunnel was used
416 struct GNUNET_TIME_Absolute timestamp;
419 * Destination of the tunnel.
424 * Next hop in the tunnel. If 0, @c client must be set.
426 GNUNET_PEER_Id next_hop;
429 * Previous hop in the tunnel. If 0, @c owner must be set.
431 GNUNET_PEER_Id prev_hop;
434 * Flow control information about @c next_hop or @c client.
436 struct MeshFlowControl next_fc;
439 * Flow control information about @c prev_hop or @c owner.
441 struct MeshFlowControl prev_fc;
444 * Client owner of the tunnel, if any
446 struct MeshClient *owner;
449 * Client destination of the tunnel, if any.
451 struct MeshClient *client;
454 * Task to keep the used paths alive at the owner,
455 * time tunnel out on all the other peers.
457 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
460 * Path being used for the tunnel.
462 struct MeshPeerPath *path;
465 * Flag to signal the destruction of the tunnel.
466 * If this is set GNUNET_YES the tunnel will be destroyed
467 * when the queue is empty.
472 * Total messages pending for this tunnels, payload or not.
474 unsigned int pending_messages;
478 * Only present (non-NULL) at the owner of a tunnel.
480 struct MeshTunnelReliability *fwd_rel;
484 * Only present (non-NULL) at the destination of a tunnel.
486 struct MeshTunnelReliability *bck_rel;
491 * Struct containing information about a client of the service
493 * TODO: add a list of 'waiting' ports
500 struct MeshClient *next;
505 struct MeshClient *prev;
508 * Tunnels that belong to this client, indexed by local id
510 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
513 * Tunnels this client has accepted, indexed by incoming local id
515 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
518 * Handle to communicate with the client
520 struct GNUNET_SERVER_Client *handle;
523 * Ports that this client has declared interest in.
524 * Indexed by port, contains *Client.
526 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
529 * Whether the client is active or shutting down (don't send confirmations
530 * to a client that is shutting down.
535 * ID of the client, mainly for debug messages
542 /******************************************************************************/
543 /************************ DEBUG FUNCTIONS ****************************/
544 /******************************************************************************/
548 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
549 * @param cls string to print
550 * @param success GNUNET_OK if the PUT was transmitted,
551 * GNUNET_NO on timeout,
552 * GNUNET_SYSERR on disconnect from service
553 * after the PUT message was transmitted
554 * (so we don't know if it was received or not)
559 mesh_debug (void *cls, int success)
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
568 unsigned int debug_fwd_ack;
569 unsigned int debug_bck_ack;
573 /******************************************************************************/
574 /*********************** GLOBAL VARIABLES ****************************/
575 /******************************************************************************/
577 /************************** Configuration parameters **************************/
580 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
582 static struct GNUNET_TIME_Relative refresh_path_time;
585 * How often to PUT own ID in the DHT.
587 static struct GNUNET_TIME_Relative id_announce_time;
590 * Maximum time allowed to connect to a peer found by string.
592 static struct GNUNET_TIME_Relative connect_timeout;
595 * Default TTL for payload packets.
597 static unsigned long long default_ttl;
600 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
602 static unsigned long long dht_replication_level;
605 * How many tunnels are we willing to maintain.
606 * Local tunnels are always allowed, even if there are more tunnels than max.
608 static unsigned long long max_tunnels;
611 * How many messages *in total* are we willing to queue, divided by number of
612 * tunnels to get tunnel queue size.
614 static unsigned long long max_msgs_queue;
617 * How many peers do we want to remember?
619 static unsigned long long max_peers;
622 * Percentage of messages that will be dropped (for test purposes only).
624 static unsigned long long drop_percent;
626 /*************************** Static global variables **************************/
629 * DLL with all the clients, head.
631 static struct MeshClient *clients_head;
634 * DLL with all the clients, tail.
636 static struct MeshClient *clients_tail;
639 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
641 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
644 * Number of tunnels known.
646 static unsigned long long n_tunnels;
649 * Tunnels incoming, indexed by MESH_TunnelNumber
650 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
652 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
655 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
657 static struct GNUNET_CONTAINER_MultiHashMap *peers;
660 * Handle to communicate with transport
662 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
665 * Handle to communicate with core.
667 static struct GNUNET_CORE_Handle *core_handle;
672 static struct GNUNET_DHT_Handle *dht_handle;
677 static struct GNUNET_SERVER_Handle *server_handle;
680 * Handle to the statistics service.
682 static struct GNUNET_STATISTICS_Handle *stats;
685 * Notification context, to send messages to local clients.
687 static struct GNUNET_SERVER_NotificationContext *nc;
690 * Local peer own ID (memory efficient handle).
692 static GNUNET_PEER_Id myid;
695 * Local peer own ID (full value).
697 static struct GNUNET_PeerIdentity my_full_id;
702 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
707 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
710 * Tunnel ID for the next created tunnel (global tunnel number).
712 static MESH_TunnelNumber next_tid;
715 * Tunnel ID for the next incoming tunnel (local tunnel number).
717 static MESH_TunnelNumber next_local_tid;
720 * All ports clients of this peer have opened.
722 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
725 * Task to periodically announce itself in the network.
727 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
730 * Next ID to assign to a client.
732 unsigned int next_client_id;
735 /******************************************************************************/
736 /*********************** DECLARATIONS **************************/
737 /******************************************************************************/
740 * Function to process paths received for a new peer addition. The recorded
741 * paths form the initial tunnel, which can be optimized later.
742 * Called on each result obtained for the DHT search.
745 * @param exp when will this value expire
746 * @param key key of the result
747 * @param type type of the result
748 * @param size number of bytes in data
749 * @param data pointer to the result data
752 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
753 const struct GNUNET_HashCode * key,
754 const struct GNUNET_PeerIdentity *get_path,
755 unsigned int get_path_length,
756 const struct GNUNET_PeerIdentity *put_path,
757 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
758 size_t size, const void *data);
762 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
763 * and insert it in the appropriate structures if the peer is not known yet.
765 * @param peer Full identity of the peer.
767 * @return Existing or newly created peer info.
769 static struct MeshPeerInfo *
770 peer_get (const struct GNUNET_PeerIdentity *peer);
774 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
775 * and insert it in the appropriate structures if the peer is not known yet.
777 * @param peer Short identity of the peer.
779 * @return Existing or newly created peer info.
781 static struct MeshPeerInfo *
782 peer_get_short (const GNUNET_PEER_Id peer);
786 * Build a PeerPath from the paths returned from the DHT, reversing the paths
787 * to obtain a local peer -> destination path and interning the peer ids.
789 * @return Newly allocated and created path
791 static struct MeshPeerPath *
792 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
793 unsigned int get_path_length,
794 const struct GNUNET_PeerIdentity *put_path,
795 unsigned int put_path_length);
799 * Adds a path to the peer_infos of all the peers in the path
801 * @param p Path to process.
802 * @param confirmed Whether we know if the path works or not.
805 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
810 * Search for a tunnel by global ID using full PeerIdentities.
812 * @param oid owner of the tunnel.
813 * @param tid global tunnel number.
815 * @return tunnel handler, NULL if doesn't exist.
817 static struct MeshTunnel *
818 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
822 * Notify a tunnel that a connection has broken that affects at least
825 * @param t Tunnel affected.
826 * @param p1 Peer that got disconnected from p2.
827 * @param p2 Peer that got disconnected from p1.
829 * @return Short ID of the peer disconnected (either p1 or p2).
830 * 0 if the tunnel remained unaffected.
832 static GNUNET_PEER_Id
833 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
838 * @brief Use the given path for the tunnel.
839 * Update the next and prev hops (and RCs).
840 * (Re)start the path refresh in case the tunnel is locally owned.
842 * @param t Tunnel to update.
843 * @param p Path to use.
846 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
849 * Tunnel is empty: destroy it.
851 * Notifies all participants (peers, cleints) about the destruction.
853 * @param t Tunnel to destroy.
856 tunnel_destroy_empty (struct MeshTunnel *t);
859 * @brief Queue and pass message to core when possible.
861 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
862 * and accounts for it. In case the queue is full, the message is dropped and
865 * Otherwise, message is treated as internal and allowed to go regardless of
868 * @param cls Closure (@c type dependant). It will be used by queue_send to
869 * build the message to be sent if not already prebuilt.
870 * @param type Type of the message, 0 for a raw message.
871 * @param size Size of the message.
872 * @param dst Neighbor to send message to.
873 * @param t Tunnel this message belongs to.
876 queue_add (void *cls, uint16_t type, size_t size,
877 struct MeshPeerInfo *dst, struct MeshTunnel *t);
881 * Free a transmission that was already queued with all resources
882 * associated to the request.
884 * @param queue Queue handler to cancel.
885 * @param clear_cls Is it necessary to free associated cls?
888 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
892 * @brief Get the next transmittable message from the queue.
894 * This will be the head, except in the case of being a data packet
895 * not allowed by the destination peer.
897 * @param peer Destination peer.
899 * @return The next viable MeshPeerQueue element to send to that peer.
900 * NULL when there are no transmittable messages.
902 struct MeshPeerQueue *
903 queue_get_next (const struct MeshPeerInfo *peer);
907 * Core callback to write a queued packet to core buffer
909 * @param cls Closure (peer info).
910 * @param size Number of bytes available in buf.
911 * @param buf Where the to write the message.
913 * @return number of bytes written to buf
916 queue_send (void *cls, size_t size, void *buf);
919 /******************************************************************************/
920 /************************ PERIODIC FUNCTIONS ****************************/
921 /******************************************************************************/
924 * Periodically announce self id in the DHT
927 * @param tc task context
930 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
934 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
936 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
940 * - Set data expiration in function of X
943 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
945 block.id = my_full_id;
946 GNUNET_DHT_put (dht_handle, /* DHT handle */
947 &my_full_id.hashPubKey, /* Key to use */
948 dht_replication_level, /* Replication level */
949 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
950 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
951 sizeof (block), /* Size of the data */
952 (const char *) &block, /* Data itself */
953 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
954 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
955 NULL, /* Continuation */
956 NULL); /* Continuation closure */
958 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
962 /******************************************************************************/
963 /****************** GENERAL HELPER FUNCTIONS ************************/
964 /******************************************************************************/
968 * Check if client has registered with the service and has not disconnected
970 * @param client the client to check
972 * @return non-NULL if client exists in the global DLL
974 static struct MeshClient *
975 client_get (struct GNUNET_SERVER_Client *client)
977 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
982 * Deletes a tunnel from a client (either owner or destination). To be used on
985 * @param c Client whose tunnel to delete.
986 * @param t Tunnel which should be deleted.
989 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
993 GNUNET_assert (GNUNET_YES ==
994 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1000 GNUNET_assert (GNUNET_YES ==
1001 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1008 * Notify the appropriate client that a new incoming tunnel was created.
1010 * @param t Tunnel that was created.
1013 send_client_tunnel_create (struct MeshTunnel *t)
1015 struct GNUNET_MESH_TunnelMessage msg;
1017 if (NULL == t->client)
1019 msg.header.size = htons (sizeof (msg));
1020 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1021 msg.tunnel_id = htonl (t->local_tid_dest);
1022 msg.port = htonl (t->port);
1024 msg.options |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1025 msg.options |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1026 msg.options = htonl (msg.options);
1027 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1028 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1029 &msg.header, GNUNET_NO);
1034 * Notify dest client that the incoming tunnel is no longer valid.
1036 * @param c Client to notify..
1037 * @param t Tunnel that is destroyed.
1040 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1042 struct GNUNET_MESH_TunnelMessage msg;
1049 if (c != t->client && c != t->owner)
1054 msg.header.size = htons (sizeof (msg));
1055 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1056 msg.tunnel_id = htonl (t->local_tid_dest);
1057 msg.port = htonl (0);
1058 memset(&msg.peer, 0, sizeof (msg.peer));
1059 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1060 &msg.header, GNUNET_NO);
1065 * Iterator over all the peers to remove the oldest not-used entry.
1067 * @param cls Closure (unsued).
1068 * @param key ID of the peer.
1069 * @param value Peer_Info of the peer.
1074 peer_info_timeout (void *cls,
1075 const struct GNUNET_HashCode *key,
1082 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1083 * and insert it in the appropriate structures if the peer is not known yet.
1085 * @param peer Full identity of the peer.
1087 * @return Existing or newly created peer info.
1089 static struct MeshPeerInfo *
1090 peer_get (const struct GNUNET_PeerIdentity *peer)
1092 struct MeshPeerInfo *peer_info;
1094 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1095 if (NULL == peer_info)
1098 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1099 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1101 GNUNET_CONTAINER_multihashmap_iterate (peers,
1105 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1106 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1107 peer_info->id = GNUNET_PEER_intern (peer);
1109 peer_info->last_contact = GNUNET_TIME_absolute_get();
1116 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1117 * and insert it in the appropriate structures if the peer is not known yet.
1119 * @param peer Short identity of the peer.
1121 * @return Existing or newly created peer info.
1123 static struct MeshPeerInfo *
1124 peer_get_short (const GNUNET_PEER_Id peer)
1126 struct GNUNET_PeerIdentity id;
1128 GNUNET_PEER_resolve (peer, &id);
1129 return peer_get (&id);
1134 * Choose the best path towards a peer considering the tunnel properties.
1136 * @param peer The destination peer.
1137 * @param t The tunnel the path is for.
1139 * @return Best current known path towards the peer, if any.
1141 static struct MeshPeerPath *
1142 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1144 struct MeshPeerPath *best_p;
1145 struct MeshPeerPath *p;
1146 unsigned int best_cost;
1149 best_p = p = peer->path_head;
1150 best_cost = cost = p->length;
1153 if ((cost = p->length) < best_cost)
1164 * Core callback to write a pre-constructed data packet to core buffer
1166 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1167 * @param size Number of bytes available in buf.
1168 * @param buf Where the to write the message.
1170 * @return number of bytes written to buf
1173 send_core_data_raw (void *cls, size_t size, void *buf)
1175 struct GNUNET_MessageHeader *msg = cls;
1178 GNUNET_assert (NULL != msg);
1179 total_size = ntohs (msg->size);
1181 if (total_size > size)
1186 memcpy (buf, msg, total_size);
1193 * Sends an already built message to a peer, properly registrating
1194 * all used resources.
1196 * @param message Message to send. Function makes a copy of it.
1197 * @param peer Short ID of the neighbor whom to send the message.
1198 * @param t Tunnel on which this message is transmitted.
1201 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1202 GNUNET_PEER_Id peer,
1203 struct MeshTunnel *t)
1205 struct GNUNET_PeerIdentity id;
1206 struct MeshPeerInfo *neighbor;
1207 struct MeshPeerPath *p;
1212 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1217 size = ntohs (message->size);
1218 data = GNUNET_malloc (size);
1219 memcpy (data, message, size);
1220 type = ntohs(message->type);
1221 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1222 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1224 struct GNUNET_MESH_Data *u;
1226 u = (struct GNUNET_MESH_Data *) data;
1227 u->ttl = htonl (ntohl (u->ttl) - 1);
1229 GNUNET_PEER_resolve (peer, &id);
1230 neighbor = peer_get (&id);
1231 for (p = neighbor->path_head; NULL != p; p = p->next)
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 " %s IS NOT DIRECTLY CONNECTED\n",
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 for (p = neighbor->path_head; NULL != p; p = p->next)
1249 struct GNUNET_PeerIdentity debug_id;
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 " path with %u hops through:\n",
1255 for (i = 0; i < p->length; i++)
1257 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 i, GNUNET_i2s(&debug_id));
1264 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1266 " no direct connection to %s\n",
1271 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1282 * Sends a CREATE PATH message for a path to a peer, properly registrating
1283 * all used resources.
1285 * @param t Tunnel for which the path is created.
1288 send_create_path (struct MeshTunnel *t)
1290 struct MeshPeerInfo *neighbor;
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1293 neighbor = peer_get_short (t->next_hop);
1295 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1296 sizeof (struct GNUNET_MESH_CreateTunnel) +
1297 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1300 t->state = MESH_TUNNEL_WAITING;
1305 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1307 * @param t Tunnel which to confirm.
1310 send_path_ack (struct MeshTunnel *t)
1312 struct MeshPeerInfo *peer;
1314 peer = peer_get_short (t->prev_hop);
1317 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1318 sizeof (struct GNUNET_MESH_PathACK),
1325 * Try to establish a new connection to this peer in the given tunnel.
1326 * If the peer doesn't have any path to it yet, try to get one.
1327 * If the peer already has some path, send a CREATE PATH towards it.
1329 * @param peer PeerInfo of the peer.
1330 * @param t Tunnel for which to create the path, if possible.
1333 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1335 struct MeshPeerPath *p;
1337 if (NULL != peer->path_head)
1339 p = peer_get_best_path (peer, t);
1340 tunnel_use_path (t, p);
1341 send_create_path (t);
1343 else if (NULL == peer->dhtget)
1345 struct GNUNET_PeerIdentity id;
1347 GNUNET_PEER_resolve (peer->id, &id);
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1350 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1351 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1352 &id.hashPubKey, /* key to search */
1353 dht_replication_level, /* replication level */
1354 GNUNET_DHT_RO_RECORD_ROUTE |
1355 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1357 0, /* xquery bits */
1358 &dht_get_id_handler, peer);
1359 t->state = MESH_TUNNEL_SEARCHING;
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "There is no path but the DHT GET is already started.\n");
1370 * Destroy the peer_info and free any allocated resources linked to it
1372 * @param pi The peer_info to destroy.
1374 * @return GNUNET_OK on success
1377 peer_info_destroy (struct MeshPeerInfo *pi)
1379 struct GNUNET_PeerIdentity id;
1380 struct MeshPeerPath *p;
1381 struct MeshPeerPath *nextp;
1384 GNUNET_PEER_resolve (pi->id, &id);
1385 GNUNET_PEER_change_rc (pi->id, -1);
1388 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1392 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1394 if (NULL != pi->dhtget)
1396 GNUNET_DHT_get_stop (pi->dhtget);
1402 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1406 for (i = 0; i < pi->ntunnels; i++)
1407 tunnel_destroy_empty (pi->tunnels[i]);
1408 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1415 * Remove all paths that rely on a direct connection between p1 and p2
1416 * from the peer itself and notify all tunnels about it.
1418 * @param peer PeerInfo of affected peer.
1419 * @param p1 GNUNET_PEER_Id of one peer.
1420 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1423 * TODO: optimize (see below)
1426 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1429 struct MeshPeerPath *p;
1430 struct MeshPeerPath *next;
1431 struct MeshPeerInfo *peer_d;
1433 unsigned int destroyed;
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1438 for (p = peer->path_head; NULL != p; p = next)
1441 for (i = 0; i < (p->length - 1); i++)
1443 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1444 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1446 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1456 for (i = 0; i < peer->ntunnels; i++)
1458 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1462 peer_d = peer_get_short (d);
1463 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1464 tunnel_use_path (peer->tunnels[i], next);
1465 peer_connect (peer_d, peer->tunnels[i]);
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1472 * Add the path to the peer and update the path used to reach it in case this
1475 * @param peer_info Destination peer to add the path to.
1476 * @param path New path to add. Last peer must be the peer in arg 1.
1477 * Path will be either used of freed if already known.
1478 * @param trusted Do we trust that this path is real?
1481 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1484 struct MeshPeerPath *aux;
1488 if ((NULL == peer_info) || (NULL == path))
1491 path_destroy (path);
1494 if (path->peers[path->length - 1] != peer_info->id)
1497 path_destroy (path);
1500 if (2 >= path->length && GNUNET_NO == trusted)
1502 /* Only allow CORE to tell us about direct paths */
1503 path_destroy (path);
1506 for (l = 1; l < path->length; l++)
1508 if (path->peers[l] == myid)
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1511 for (l2 = 0; l2 < path->length - l; l2++)
1513 path->peers[l2] = path->peers[l + l2];
1518 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1523 struct GNUNET_PeerIdentity id;
1525 GNUNET_PEER_resolve (peer_info->id, &id);
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1527 path->length, GNUNET_i2s (&id));
1530 l = path_get_length (path);
1533 path_destroy (path);
1537 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1538 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1540 l2 = path_get_length (aux);
1543 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1544 peer_info->path_tail, aux, path);
1549 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1551 path_destroy (path);
1556 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1563 * Add the path to the origin peer and update the path used to reach it in case
1564 * this is the shortest.
1565 * The path is given in peer_info -> destination, therefore we turn the path
1566 * upside down first.
1568 * @param peer_info Peer to add the path to, being the origin of the path.
1569 * @param path New path to add after being inversed.
1570 * Path will be either used or freed.
1571 * @param trusted Do we trust that this path is real?
1574 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1575 struct MeshPeerPath *path, int trusted)
1578 peer_info_add_path (peer_info, path, trusted);
1583 * Add a tunnel to the list of tunnels a peer participates in.
1584 * Update the tunnel's destination.
1586 * @param p Peer to add to.
1587 * @param t Tunnel to add.
1590 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1594 GNUNET_break (t->dest == p->id);
1598 GNUNET_PEER_change_rc (t->dest, 1);
1599 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1604 * Remove a tunnel from the list of tunnels a peer participates in.
1605 * Free the tunnel's destination.
1607 * @param p Peer to clean.
1608 * @param t Tunnel to remove.
1611 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1615 if (t->dest == p->id)
1617 GNUNET_PEER_change_rc (t->dest, -1);
1620 for (i = 0; i < p->ntunnels; i++)
1622 if (p->tunnels[i] == t)
1624 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1625 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1633 * Function called if the connection to the peer has been stalled for a while,
1634 * possibly due to a missed ACK. Poll the peer about its ACK status.
1636 * @param cls Closure (poll ctx).
1637 * @param tc TaskContext.
1640 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1642 struct MeshFlowControl *fc = cls;
1643 struct GNUNET_MESH_Poll msg;
1644 struct MeshTunnel *t = fc->t;
1645 GNUNET_PEER_Id peer;
1647 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1648 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1655 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1656 msg.header.size = htons (sizeof (msg));
1657 msg.tid = htonl (t->id.tid);
1658 msg.pid = htonl (fc->last_pid_sent);
1659 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1661 if (fc == &t->prev_fc)
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1666 else if (fc == &t->next_fc)
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1676 send_prebuilt_message (&msg.header, peer, t);
1677 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1678 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1684 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1685 * to obtain a local peer -> destination path and interning the peer ids.
1687 * @return Newly allocated and created path
1689 static struct MeshPeerPath *
1690 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1691 unsigned int get_path_length,
1692 const struct GNUNET_PeerIdentity *put_path,
1693 unsigned int put_path_length)
1695 struct MeshPeerPath *p;
1701 GNUNET_PEER_change_rc (myid, 1);
1702 i = get_path_length;
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1704 for (i--; i >= 0; i--)
1706 id = GNUNET_PEER_intern (&get_path[i]);
1707 if (p->length > 0 && id == p->peers[p->length - 1])
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1710 GNUNET_PEER_change_rc (id, -1);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1715 GNUNET_i2s (&get_path[i]));
1717 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1718 p->peers[p->length - 1] = id;
1721 i = put_path_length;
1722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1723 for (i--; i >= 0; i--)
1725 id = GNUNET_PEER_intern (&put_path[i]);
1728 /* PUT path went through us, so discard the path up until now and start
1729 * from here to get a much shorter (and loop-free) path.
1734 if (p->length > 0 && id == p->peers[p->length - 1])
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1737 GNUNET_PEER_change_rc (id, -1);
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1742 GNUNET_i2s (&put_path[i]));
1744 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1745 p->peers[p->length - 1] = id;
1749 if (get_path_length > 0)
1750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1751 GNUNET_i2s (&get_path[0]));
1752 if (put_path_length > 0)
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1754 GNUNET_i2s (&put_path[0]));
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1757 for (i = 0; i < p->length; i++)
1759 struct GNUNET_PeerIdentity peer_id;
1761 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1763 GNUNET_i2s (&peer_id));
1771 * Adds a path to the peer_infos of all the peers in the path
1773 * @param p Path to process.
1774 * @param confirmed Whether we know if the path works or not.
1777 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1781 /* TODO: invert and add */
1782 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1783 for (i++; i < p->length; i++)
1785 struct MeshPeerInfo *aux;
1786 struct MeshPeerPath *copy;
1788 aux = peer_get_short (p->peers[i]);
1789 copy = path_duplicate (p);
1790 copy->length = i + 1;
1791 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1797 * Send keepalive packets for a peer
1799 * @param cls Closure (tunnel for which to send the keepalive).
1800 * @param tc Notification context.
1803 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1807 * Search for a tunnel among the incoming tunnels
1809 * @param tid the local id of the tunnel
1811 * @return tunnel handler, NULL if doesn't exist
1813 static struct MeshTunnel *
1814 tunnel_get_incoming (MESH_TunnelNumber tid)
1816 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1817 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1822 * Search for a tunnel among the tunnels for a client
1824 * @param c the client whose tunnels to search in
1825 * @param tid the local id of the tunnel
1827 * @return tunnel handler, NULL if doesn't exist
1829 static struct MeshTunnel *
1830 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1832 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1834 return tunnel_get_incoming (tid);
1838 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1839 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1845 * Search for a tunnel by global ID using PEER_ID
1847 * @param pi owner of the tunnel
1848 * @param tid global tunnel number
1850 * @return tunnel handler, NULL if doesn't exist
1852 static struct MeshTunnel *
1853 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1855 struct MESH_TunnelID id;
1856 struct GNUNET_HashCode hash;
1861 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1862 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1867 * Search for a tunnel by global ID using full PeerIdentities
1869 * @param oid owner of the tunnel
1870 * @param tid global tunnel number
1872 * @return tunnel handler, NULL if doesn't exist
1874 static struct MeshTunnel *
1875 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1877 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1882 * Add a client to a tunnel, initializing all needed data structures.
1884 * @param t Tunnel to which add the client.
1885 * @param c Client which to add to the tunnel.
1888 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1890 if (NULL != t->client)
1896 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1897 t->local_tid_dest, t,
1898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1904 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1905 t->local_tid_dest, t,
1906 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1916 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1918 unsigned int own_pos;
1920 for (own_pos = 0; own_pos < p->length; own_pos++)
1922 if (p->peers[own_pos] == myid)
1925 if (own_pos > p->length - 1)
1931 if (own_pos < p->length - 1)
1932 t->next_hop = p->peers[own_pos + 1];
1934 t->next_hop = p->peers[own_pos];
1935 GNUNET_PEER_change_rc (t->next_hop, 1);
1937 t->prev_hop = p->peers[own_pos - 1];
1939 t->prev_hop = p->peers[0];
1940 GNUNET_PEER_change_rc (t->prev_hop, 1);
1942 if (NULL != t->path)
1943 path_destroy (t->path);
1944 t->path = path_duplicate (p);
1947 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1948 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1949 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1956 * Notifies a tunnel that a connection has broken that affects at least
1957 * some of its peers. Sends a notification towards the root of the tree.
1958 * In case the peer is the owner of the tree, notifies the client that owns
1959 * the tunnel and tries to reconnect.
1961 * @param t Tunnel affected.
1962 * @param p1 Peer that got disconnected from p2.
1963 * @param p2 Peer that got disconnected from p1.
1965 * @return Short ID of the peer disconnected (either p1 or p2).
1966 * 0 if the tunnel remained unaffected.
1968 static GNUNET_PEER_Id
1969 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1972 // if (myid != p1 && myid != p2) FIXME
1977 // if (tree_get_predecessor (t->tree) != 0)
1979 // /* We are the peer still connected, notify owner of the disconnection. */
1980 // struct GNUNET_MESH_PathBroken msg;
1981 // struct GNUNET_PeerIdentity neighbor;
1983 // msg.header.size = htons (sizeof (msg));
1984 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1985 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1986 // msg.tid = htonl (t->id.tid);
1987 // msg.peer1 = my_full_id;
1988 // GNUNET_PEER_resolve (pid, &msg.peer2);
1989 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1990 // send_prebuilt_message (&msg.header, &neighbor, t);
1997 * Build a local ACK message and send it to a local client.
1999 * @param t Tunnel on which to send the ACK.
2000 * @param c Client to whom send the ACK.
2001 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2004 send_local_ack (struct MeshTunnel *t,
2005 struct MeshClient *c,
2008 struct GNUNET_MESH_LocalAck msg;
2010 msg.header.size = htons (sizeof (msg));
2011 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2012 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2013 GNUNET_SERVER_notification_context_unicast (nc,
2020 * Build an ACK message and queue it to send to the given peer.
2022 * @param t Tunnel on which to send the ACK.
2023 * @param peer Peer to whom send the ACK.
2024 * @param ack Value of the ACK.
2027 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2029 struct GNUNET_MESH_ACK msg;
2031 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2032 msg.header.size = htons (sizeof (msg));
2033 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2034 msg.pid = htonl (ack);
2035 msg.tid = htonl (t->id.tid);
2037 send_prebuilt_message (&msg.header, peer, t);
2042 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2044 * @param t Tunnel this is about.
2047 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2049 struct GNUNET_MESH_DataACK msg;
2051 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2052 msg.header.size = htons (sizeof (msg));
2053 msg.tid = htonl (t->id.tid);
2054 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2055 msg.pid = htonl (t->prev_fc.last_pid_recv);
2056 msg.futures = 0; // FIXME set bits of other newer messages received
2058 send_prebuilt_message (&msg.header, t->prev_hop, t);
2063 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2065 * @param t Tunnel this is about.
2068 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2070 struct GNUNET_MESH_DataACK msg;
2072 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2073 msg.header.size = htons (sizeof (msg));
2074 msg.tid = htonl (t->id.tid);
2075 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2076 msg.pid = htonl (t->next_fc.last_pid_recv);
2077 msg.futures = 0; // FIXME set bits of other newer messages received
2079 send_prebuilt_message (&msg.header, t->next_hop, t);
2084 * Send an ACK informing the predecessor about the available buffer space.
2085 * In case there is no predecessor, inform the owning client.
2086 * If buffering is off, send only on behalf of children or self if endpoint.
2087 * If buffering is on, send when sent to children and buffer space is free.
2088 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2089 * the ACK itself goes "back" (towards root).
2091 * @param t Tunnel on which to send the ACK.
2092 * @param type Type of message that triggered the ACK transmission.
2095 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2099 /* Is it after unicast retransmission? */
2102 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "ACK due to FWD DATA retransmission\n");
2105 if (GNUNET_YES == t->nobuffer)
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2110 if (GNUNET_YES == t->reliable && NULL != t->client)
2111 tunnel_send_fwd_data_ack (t);
2113 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2114 if (NULL != t->owner && GNUNET_YES == t->reliable)
2116 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2118 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2119 tunnel_send_fwd_data_ack (t);
2121 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2122 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2123 t->force_ack = GNUNET_YES;
2129 /* Check if we need to transmit the ACK */
2130 if (NULL == t->owner &&
2131 t->queue_max > t->next_fc.queue_n * 4 &&
2132 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2133 GNUNET_NO == t->force_ack)
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2137 " t->qmax: %u, t->qn: %u\n",
2138 t->queue_max, t->next_fc.queue_n);
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 " t->pid: %u, t->ack: %u\n",
2141 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2145 /* Ok, ACK might be necessary, what PID to ACK? */
2146 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 " last %u, qmax %u, q %u\n",
2150 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2151 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2157 t->prev_fc.last_ack_sent = ack;
2158 if (NULL != t->owner)
2159 send_local_ack (t, t->owner, GNUNET_YES);
2160 else if (0 != t->prev_hop)
2161 send_ack (t, t->prev_hop, ack);
2165 t->force_ack = GNUNET_NO;
2170 * Send an ACK informing the children node/client about the available
2172 * If buffering is off, send only on behalf of root (can be self).
2173 * If buffering is on, send when sent to predecessor and buffer space is free.
2174 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2175 * the ACK itself goes "forward" (towards children/clients).
2177 * @param t Tunnel on which to send the ACK.
2178 * @param type Type of message that triggered the ACK transmission.
2181 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2185 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2186 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2187 /* Is it after data to_origin retransmission? */
2190 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2191 if (GNUNET_YES == t->nobuffer)
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2194 " Not sending ACK, nobuffer + traffic\n");
2198 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2199 /* Why was this here?!
2200 * This prevents the destination from starting traffic to the origin
2202 // if (NULL != t->client && GNUNET_YES == t->reliable)
2204 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2206 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2207 tunnel_send_bck_data_ack (t);
2209 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2210 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2211 t->force_ack = GNUNET_YES;
2217 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2219 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2221 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 " Not sending ACK, not needed, last ack sent was %u\n",
2225 t->next_fc.last_ack_sent);
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2229 " Sending BCK ACK %u (last sent: %u)\n",
2230 ack, t->next_fc.last_ack_sent);
2231 t->next_fc.last_ack_sent = ack;
2233 if (NULL != t->client)
2234 send_local_ack (t, t->client, GNUNET_NO);
2235 else if (0 != t->next_hop)
2236 send_ack (t, t->next_hop, ack);
2239 t->force_ack = GNUNET_NO;
2244 * Modify the mesh message TID from global to local and send to client.
2246 * @param t Tunnel on which to send the message.
2247 * @param msg Message to modify and send.
2248 * @param c Client to send to.
2249 * @param tid Tunnel ID to use (c can be both owner and client).
2252 tunnel_send_client_data (struct MeshTunnel *t,
2253 const struct GNUNET_MESH_Data *msg,
2254 struct MeshClient *c, MESH_TunnelNumber tid)
2256 struct GNUNET_MESH_LocalData *copy;
2257 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2258 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2260 if (size < sizeof (struct GNUNET_MessageHeader))
2262 GNUNET_break_op (0);
2270 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2271 memcpy (©[1], &msg[1], size);
2272 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2273 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2274 copy->tid = htonl (tid);
2275 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2276 ©->header, GNUNET_NO);
2280 * Modify the unicast message TID from global to local and send to client.
2282 * @param t Tunnel on which to send the message.
2283 * @param msg Message to modify and send.
2286 tunnel_send_client_ucast (struct MeshTunnel *t,
2287 const struct GNUNET_MESH_Data *msg)
2289 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2294 * Modify the to_origin message TID from global to local and send to client.
2296 * @param t Tunnel on which to send the message.
2297 * @param msg Message to modify and send.
2300 tunnel_send_client_to_orig (struct MeshTunnel *t,
2301 const struct GNUNET_MESH_Data *msg)
2303 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2308 * We haven't received an ACK after a certain time: restransmit the message.
2310 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2311 * @param tc TaskContext.
2314 tunnel_retransmit_message (void *cls,
2315 const struct GNUNET_SCHEDULER_TaskContext *tc)
2317 struct MeshTunnelReliability *rel = cls;
2318 struct MeshReliableMessage *copy;
2319 struct MeshTunnel *t;
2320 struct GNUNET_MESH_Data *payload;
2323 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2324 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2328 copy = rel->head_sent;
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->id);
2336 copy->timestamp = GNUNET_TIME_absolute_get ();
2337 payload = (struct GNUNET_MESH_Data *) ©[1];
2338 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2339 send_prebuilt_message (&payload->header, hop, t);
2340 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2341 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2342 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2343 &tunnel_retransmit_message,
2349 * @brief Re-initiate traffic to this peer if necessary.
2351 * Check if there is traffic queued towards this peer
2352 * and the core transmit handle is NULL (traffic was stalled).
2353 * If so, call core tmt rdy.
2355 * @param peer_id Short ID of peer to which initiate traffic.
2358 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2360 struct MeshPeerInfo *peer;
2361 struct GNUNET_PeerIdentity id;
2362 struct MeshPeerQueue *q;
2365 peer = peer_get_short (peer_id);
2366 if (NULL != peer->core_transmit)
2369 q = queue_get_next (peer);
2372 /* Might br multicast traffic already sent to this particular peer but
2373 * not to other children in this tunnel.
2374 * This way t->queue_n would be > 0 but the queue of this particular peer
2380 GNUNET_PEER_resolve (peer->id, &id);
2381 peer->core_transmit =
2382 GNUNET_CORE_notify_transmit_ready(core_handle,
2385 GNUNET_TIME_UNIT_FOREVER_REL,
2395 * Send a message to all peers and clients in this tunnel that the tunnel
2396 * is no longer valid. If some peer or client should not receive the message,
2397 * should be zero'ed out before calling this function.
2399 * @param t The tunnel whose peers and clients to notify.
2402 tunnel_send_destroy (struct MeshTunnel *t)
2404 struct GNUNET_MESH_TunnelDestroy msg;
2405 struct GNUNET_PeerIdentity id;
2407 msg.header.size = htons (sizeof (msg));
2408 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2409 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2410 msg.tid = htonl (t->id.tid);
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 " sending tunnel destroy for tunnel: %s [%X]\n",
2413 GNUNET_i2s (&msg.oid), t->id.tid);
2415 if (NULL == t->client && 0 != t->next_hop)
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2418 GNUNET_PEER_resolve (t->next_hop, &id);
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 " sending forward to %s\n",
2422 send_prebuilt_message (&msg.header, t->next_hop, t);
2424 if (NULL == t->owner && 0 != t->prev_hop)
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2427 GNUNET_PEER_resolve (t->prev_hop, &id);
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 " sending back to %s\n",
2431 send_prebuilt_message (&msg.header, t->prev_hop, t);
2433 if (NULL != t->owner)
2435 send_client_tunnel_destroy (t->owner, t);
2437 if (NULL != t->client)
2439 send_client_tunnel_destroy (t->client, t);
2445 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2447 * @param t Tunnel which to cancel.
2448 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2451 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2453 struct MeshPeerInfo *peer_info;
2454 struct MeshPeerQueue *pq;
2455 struct MeshPeerQueue *next;
2458 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2459 peer_info = peer_get_short (neighbor);
2460 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2463 if (pq->tunnel == t)
2465 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2466 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2468 /* Should have been removed on destroy children */
2471 queue_destroy (pq, GNUNET_YES);
2474 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2476 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2477 peer_info->core_transmit = NULL;
2483 * Destroy the tunnel.
2485 * This function does not generate any warning traffic to clients or peers.
2488 * Remove the tunnel from peer_info's and clients' hashmaps.
2489 * Cancel messages belonging to this tunnel queued to neighbors.
2490 * Free any allocated resources linked to the tunnel.
2492 * @param t the tunnel to destroy
2494 * @return GNUNET_OK on success
2497 tunnel_destroy (struct MeshTunnel *t)
2499 struct MeshClient *c;
2500 struct GNUNET_HashCode hash;
2510 struct GNUNET_PeerIdentity id;
2512 GNUNET_PEER_resolve (t->id.oid, &id);
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2514 GNUNET_i2s (&id), t->id.tid);
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2520 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2521 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2530 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2537 if (NULL != t->client)
2541 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2542 t->local_tid_dest, t))
2548 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2549 t->local_tid_dest, t))
2556 if (0 != t->prev_hop)
2558 peer_cancel_queues (t->prev_hop, t);
2559 GNUNET_PEER_change_rc (t->prev_hop, -1);
2561 if (0 != t->next_hop)
2563 peer_cancel_queues (t->next_hop, t);
2564 GNUNET_PEER_change_rc (t->next_hop, -1);
2567 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2570 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2571 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2574 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2575 path_destroy (t->path);
2581 * Tunnel is empty: destroy it.
2583 * Notifies all participants (peers, cleints) about the destruction.
2585 * @param t Tunnel to destroy.
2588 tunnel_destroy_empty (struct MeshTunnel *t)
2592 struct GNUNET_PeerIdentity id;
2594 GNUNET_PEER_resolve (t->id.oid, &id);
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "executing destruction of empty tunnel %s [%X]\n",
2597 GNUNET_i2s (&id), t->id.tid);
2601 if (GNUNET_NO == t->destroy)
2602 tunnel_send_destroy (t);
2603 if (0 == t->pending_messages)
2606 t->destroy = GNUNET_YES;
2610 * Initialize a Flow Control structure to the initial state.
2612 * @param fc Flow Control structure to initialize.
2615 fc_init (struct MeshFlowControl *fc)
2617 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2618 fc->last_pid_recv = (uint32_t) -1;
2619 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2620 fc->last_ack_recv = (uint32_t) -1;
2621 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2622 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2627 * Create a new tunnel
2629 * @param owner Who is the owner of the tunnel (short ID).
2630 * @param tid Tunnel Number of the tunnel.
2631 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2632 * @param local Tunnel Number for the tunnel, for the client point of view.
2634 * @return A new initialized tunnel. NULL on error.
2636 static struct MeshTunnel *
2637 tunnel_new (GNUNET_PEER_Id owner,
2638 MESH_TunnelNumber tid,
2639 struct MeshClient *client,
2640 MESH_TunnelNumber local)
2642 struct MeshTunnel *t;
2643 struct GNUNET_HashCode hash;
2645 if (n_tunnels >= max_tunnels && NULL == client)
2648 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2651 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2653 fc_init (&t->next_fc);
2654 fc_init (&t->prev_fc);
2655 t->local_tid = local;
2657 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2659 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2661 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2662 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2669 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2677 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2679 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2683 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2693 * Set options in a tunnel, extracted from a bit flag field
2695 * @param t Tunnel to set options to.
2696 * @param options Bit array in host byte order.
2699 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2701 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2702 GNUNET_YES : GNUNET_NO;
2703 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2704 GNUNET_YES : GNUNET_NO;
2709 * Iterator for deleting each tunnel whose client endpoint disconnected.
2711 * @param cls Closure (client that has disconnected).
2712 * @param key The local tunnel id (used to access the hashmap).
2713 * @param value The value stored at the key (tunnel to destroy).
2715 * @return GNUNET_OK, keep iterating.
2718 tunnel_destroy_iterator (void *cls,
2722 struct MeshTunnel *t = value;
2723 struct MeshClient *c = cls;
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2727 t->local_tid, t->local_tid_dest, c->id);
2728 client_delete_tunnel (c, t);
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2733 if (0 != t->next_hop) { /* destroy could come before a path is used */
2734 GNUNET_PEER_change_rc (t->next_hop, -1);
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2742 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2743 GNUNET_PEER_change_rc (t->prev_hop, -1);
2748 tunnel_destroy_empty (t);
2755 * Timeout function, destroys tunnel if called
2757 * @param cls Closure (tunnel to destroy).
2758 * @param tc TaskContext
2761 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2763 struct MeshTunnel *t = cls;
2764 struct GNUNET_PeerIdentity id;
2766 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2769 GNUNET_PEER_resolve(t->id.oid, &id);
2770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2771 "Tunnel %s [%X] timed out. Destroying.\n",
2772 GNUNET_i2s(&id), t->id.tid);
2773 if (NULL != t->client)
2774 send_client_tunnel_destroy (t->client, t);
2775 tunnel_destroy (t); /* Do not notify other */
2780 * Resets the tunnel timeout. Starts it if no timeout was running.
2782 * @param t Tunnel whose timeout to reset.
2784 * TODO use heap to improve efficiency of scheduler.
2787 tunnel_reset_timeout (struct MeshTunnel *t)
2789 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2791 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2792 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2793 t->maintenance_task =
2794 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2795 (refresh_path_time, 4), &tunnel_timeout, t);
2799 /******************************************************************************/
2800 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2801 /******************************************************************************/
2804 * Function to send a create path packet to a peer.
2806 * @param cls closure
2807 * @param size number of bytes available in buf
2808 * @param buf where the callee should write the message
2809 * @return number of bytes written to buf
2812 send_core_path_create (void *cls, size_t size, void *buf)
2814 struct MeshTunnel *t = cls;
2815 struct GNUNET_MESH_CreateTunnel *msg;
2816 struct GNUNET_PeerIdentity *peer_ptr;
2817 struct MeshPeerPath *p = t->path;
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2824 sizeof (struct GNUNET_MESH_CreateTunnel) +
2825 p->length * sizeof (struct GNUNET_PeerIdentity);
2827 if (size < size_needed || NULL == buf)
2832 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2833 msg->header.size = htons (size_needed);
2834 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2835 msg->tid = ntohl (t->id.tid);
2838 if (GNUNET_YES == t->nobuffer)
2839 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2840 if (GNUNET_YES == t->reliable)
2841 opt |= GNUNET_MESH_OPTION_RELIABLE;
2842 msg->opt = htonl (opt);
2843 msg->port = htonl (t->port);
2845 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2846 for (i = 0; i < p->length; i++)
2848 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2852 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2858 * Creates a path ack message in buf and frees all unused resources.
2860 * @param cls closure (MeshTransmissionDescriptor)
2861 * @param size number of bytes available in buf
2862 * @param buf where the callee should write the message
2863 * @return number of bytes written to buf
2866 send_core_path_ack (void *cls, size_t size, void *buf)
2868 struct MeshTunnel *t = cls;
2869 struct GNUNET_MESH_PathACK *msg = buf;
2871 GNUNET_assert (NULL != t);
2872 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2877 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2878 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2879 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2880 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2881 msg->tid = htonl (t->id.tid);
2882 msg->peer_id = my_full_id;
2883 msg->ack = htonl (t->prev_fc.last_ack_sent);
2885 /* TODO add signature */
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2888 return sizeof (struct GNUNET_MESH_PathACK);
2893 * Free a transmission that was already queued with all resources
2894 * associated to the request.
2896 * @param queue Queue handler to cancel.
2897 * @param clear_cls Is it necessary to free associated cls?
2900 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2902 struct MeshFlowControl *fc;
2904 if (GNUNET_YES == clear_cls)
2906 switch (queue->type)
2908 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2909 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2910 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2912 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2913 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2914 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2915 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2916 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2918 " prebuilt message\n");
2919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2921 GNUNET_MESH_DEBUG_M2S (queue->type));
2923 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2928 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2929 " type %s unknown!\n",
2930 GNUNET_MESH_DEBUG_M2S (queue->type));
2932 GNUNET_free_non_null (queue->cls);
2934 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2935 queue->peer->queue_tail,
2938 /* Delete from appropriate fc in the tunnel */
2939 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2940 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2942 if (queue->peer->id == queue->tunnel->prev_hop)
2943 fc = &queue->tunnel->prev_fc;
2944 else if (queue->peer->id == queue->tunnel->next_hop)
2945 fc = &queue->tunnel->next_fc;
2949 GNUNET_free (queue);
2954 GNUNET_free (queue);
2959 * @brief Get the next transmittable message from the queue.
2961 * This will be the head, except in the case of being a data packet
2962 * not allowed by the destination peer.
2964 * @param peer Destination peer.
2966 * @return The next viable MeshPeerQueue element to send to that peer.
2967 * NULL when there are no transmittable messages.
2969 struct MeshPeerQueue *
2970 queue_get_next (const struct MeshPeerInfo *peer)
2972 struct MeshPeerQueue *q;
2974 struct GNUNET_MESH_Data *dmsg;
2975 struct MeshTunnel* t;
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2980 for (q = peer->queue_head; NULL != q; q = q->next)
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2985 GNUNET_MESH_DEBUG_M2S (q->type));
2986 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2987 pid = ntohl (dmsg->pid);
2990 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2991 ack = t->next_fc.last_ack_recv;
2993 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2994 ack = t->prev_fc.last_ack_recv;
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3002 "* ACK: %u, PID: %u\n",
3004 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3017 "* nothing found\n");
3023 queue_send (void *cls, size_t size, void *buf)
3025 struct MeshPeerInfo *peer = cls;
3026 struct GNUNET_MessageHeader *msg;
3027 struct MeshPeerQueue *queue;
3028 struct MeshTunnel *t;
3029 struct GNUNET_PeerIdentity dst_id;
3030 struct MeshFlowControl *fc;
3035 peer->core_transmit = NULL;
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3038 queue = queue_get_next (peer);
3040 /* Queue has no internal mesh traffic nor sendable payload */
3043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3044 if (NULL == peer->queue_head)
3045 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3050 GNUNET_PEER_resolve (peer->id, &dst_id);
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3053 GNUNET_i2s (&dst_id));
3054 /* Check if buffer size is enough for the message */
3055 if (queue->size > size)
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3058 "* not enough room, reissue\n");
3059 peer->core_transmit =
3060 GNUNET_CORE_notify_transmit_ready (core_handle,
3063 GNUNET_TIME_UNIT_FOREVER_REL,
3070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3073 GNUNET_assert (0 < t->pending_messages);
3074 t->pending_messages--;
3078 switch (queue->type)
3081 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3082 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3083 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3084 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3085 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3086 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3087 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3088 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 GNUNET_MESH_DEBUG_M2S (queue->type));
3093 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3094 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3095 data_size = send_core_data_raw (queue->cls, size, buf);
3096 msg = (struct GNUNET_MessageHeader *) buf;
3097 type = ntohs (msg->type);
3099 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3101 data_size = send_core_path_create (queue->cls, size, buf);
3103 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3105 data_size = send_core_path_ack (queue->cls, size, buf);
3109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3110 "* type unknown: %u\n",
3115 if (0 < drop_percent &&
3116 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3119 "Dropping message of type %s\n",
3120 GNUNET_MESH_DEBUG_M2S(queue->type));
3123 /* Free queue, but cls was freed by send_core_* */
3124 queue_destroy (queue, GNUNET_NO);
3126 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3127 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3131 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3132 if (GMC_is_pid_bigger(pid, t->next_fc.last_pid_sent))
3133 t->next_fc.last_pid_sent = pid;
3134 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3136 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3137 if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_sent))
3138 t->prev_fc.last_pid_sent = pid;
3139 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3145 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3151 /* If more data in queue, send next */
3152 queue = queue_get_next (peer);
3155 struct GNUNET_PeerIdentity id;
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3158 GNUNET_PEER_resolve (peer->id, &id);
3159 peer->core_transmit =
3160 GNUNET_CORE_notify_transmit_ready(core_handle,
3163 GNUNET_TIME_UNIT_FOREVER_REL,
3169 else if (NULL != peer->queue_head)
3171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3173 GNUNET_i2s (&my_full_id));
3174 if (peer->id == t->next_hop)
3176 else if (peer->id == t->prev_hop)
3183 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3186 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3196 * @brief Queue and pass message to core when possible.
3198 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3199 * accounts for it. In case the queue is full, the message is dropped and
3202 * Otherwise, message is treated as internal and allowed to go regardless of
3205 * @param cls Closure (@c type dependant). It will be used by queue_send to
3206 * build the message to be sent if not already prebuilt.
3207 * @param type Type of the message, 0 for a raw message.
3208 * @param size Size of the message.
3209 * @param dst Neighbor to send message to.
3210 * @param t Tunnel this message belongs to.
3213 queue_add (void *cls, uint16_t type, size_t size,
3214 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3216 struct MeshPeerQueue *queue;
3217 struct GNUNET_PeerIdentity id;
3218 struct MeshFlowControl *fc;
3224 priority = GNUNET_NO;
3225 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3228 pid = ntohl (((struct GNUNET_MESH_Data *)cls)->pid);
3230 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3233 pid = ntohl (((struct GNUNET_MESH_Data *)cls)->pid);
3237 if (fc->queue_n >= t->queue_max)
3240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3241 "queue full: %u/%u\n",
3242 fc->queue_n, t->queue_max);
3243 GNUNET_STATISTICS_update (stats,
3244 "# messages dropped (buffer full)",
3246 /* Get the PID of the oldest message in the queue */
3247 for (queue = dst->queue_head; queue != NULL; queue = queue->next)
3248 if (queue->type == type && queue->tunnel == t)
3250 pid_q = ntohl (((struct GNUNET_MESH_Data *)(queue->cls))->pid);
3251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3252 "pid: %u, pid_q: %u\n", pid, pid_q);
3255 GNUNET_assert (NULL != queue);
3257 /* If this is an earlier message that that, give it priority:
3258 * - drop the newest message in the queue
3259 * - instert current one at the end of the queue (first to get out)
3261 if (GNUNET_YES == t->reliable && GMC_is_pid_bigger(pid_q, pid))
3263 for (queue = dst->queue_tail; queue != NULL; queue = queue->prev)
3264 if (queue->type == type && queue->tunnel == t)
3266 /* Drop message from queue */
3267 pid_q = ntohl (((struct GNUNET_MESH_Data *)(queue->cls))->pid);
3268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3269 "dropping pid: %u\n", pid_q);
3270 queue_destroy (queue, GNUNET_YES);
3271 t->pending_messages--;
3272 priority = GNUNET_YES;
3277 return; /* Drop this message */
3281 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3287 if (GNUNET_YES == priority)
3288 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3290 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3291 if (NULL == dst->core_transmit)
3293 GNUNET_PEER_resolve (dst->id, &id);
3294 dst->core_transmit =
3295 GNUNET_CORE_notify_transmit_ready (core_handle,
3298 GNUNET_TIME_UNIT_FOREVER_REL,
3304 t->pending_messages++;
3308 /******************************************************************************/
3309 /******************** MESH NETWORK HANDLERS **************************/
3310 /******************************************************************************/
3314 * Core handler for path creation
3316 * @param cls closure
3317 * @param message message
3318 * @param peer peer identity this notification is about
3320 * @return GNUNET_OK to keep the connection open,
3321 * GNUNET_SYSERR to close it (signal serious error)
3324 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3325 const struct GNUNET_MessageHeader *message)
3327 unsigned int own_pos;
3330 MESH_TunnelNumber tid;
3331 struct GNUNET_MESH_CreateTunnel *msg;
3332 struct GNUNET_PeerIdentity *pi;
3333 struct MeshPeerPath *path;
3334 struct MeshPeerInfo *dest_peer_info;
3335 struct MeshPeerInfo *orig_peer_info;
3336 struct MeshTunnel *t;
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3339 "Received a path create msg [%s]\n",
3340 GNUNET_i2s (&my_full_id));
3341 size = ntohs (message->size);
3342 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3344 GNUNET_break_op (0);
3348 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3349 if (size % sizeof (struct GNUNET_PeerIdentity))
3351 GNUNET_break_op (0);
3354 size /= sizeof (struct GNUNET_PeerIdentity);
3357 GNUNET_break_op (0);
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3361 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3363 tid = ntohl (msg->tid);
3364 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3366 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3367 t = tunnel_get (pi, tid);
3368 if (NULL == t) /* might be a local tunnel */
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3373 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3379 t->port = ntohl (msg->port);
3380 opt = ntohl (msg->opt);
3381 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3383 t->nobuffer = GNUNET_YES;
3386 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3388 t->reliable = GNUNET_YES;
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3392 tunnel_reset_timeout (t);
3394 t->state = MESH_TUNNEL_WAITING;
3396 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3397 if (NULL == dest_peer_info)
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3400 " Creating PeerInfo for destination.\n");
3401 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3402 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3403 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3407 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3408 if (NULL == orig_peer_info)
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3411 " Creating PeerInfo for origin.\n");
3412 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3413 orig_peer_info->id = GNUNET_PEER_intern (pi);
3414 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3418 path = path_new (size);
3420 for (i = 0; i < size; i++)
3422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3423 GNUNET_i2s (&pi[i]));
3424 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3425 if (path->peers[i] == myid)
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3429 if (own_pos == 0 && path->peers[own_pos] != myid)
3431 /* create path: self not found in path through self */
3432 GNUNET_break_op (0);
3433 path_destroy (path);
3437 path_add_to_peers (path, GNUNET_NO);
3438 tunnel_use_path (t, path);
3440 peer_info_add_tunnel (dest_peer_info, t);
3442 if (own_pos == size - 1)
3444 struct MeshClient *c;
3446 /* Find target client */
3447 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3450 /* TODO send reject */
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3455 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3457 /* Assign local tid */
3458 while (NULL != tunnel_get_incoming (next_local_tid))
3459 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3460 t->local_tid_dest = next_local_tid++;
3461 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3463 if (GNUNET_YES == t->reliable)
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3466 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3468 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3471 tunnel_add_client (t, c);
3472 send_client_tunnel_create (t);
3477 struct MeshPeerPath *path2;
3479 t->next_hop = path->peers[own_pos + 1];
3480 GNUNET_PEER_change_rc(t->next_hop, 1);
3482 /* It's for somebody else! Retransmit. */
3483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3484 path2 = path_duplicate (path);
3485 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3486 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3487 send_create_path (t);
3495 * Core handler for path ACKs
3497 * @param cls closure
3498 * @param message message
3499 * @param peer peer identity this notification is about
3501 * @return GNUNET_OK to keep the connection open,
3502 * GNUNET_SYSERR to close it (signal serious error)
3505 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3506 const struct GNUNET_MessageHeader *message)
3508 struct GNUNET_MESH_PathACK *msg;
3509 struct MeshPeerInfo *peer_info;
3510 struct MeshPeerPath *p;
3511 struct MeshTunnel *t;
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3514 GNUNET_i2s (&my_full_id));
3515 msg = (struct GNUNET_MESH_PathACK *) message;
3516 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3519 /* TODO notify that we don't know the tunnel */
3520 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3522 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3526 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3528 peer_info = peer_get (&msg->peer_id);
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3530 GNUNET_i2s (&msg->peer_id));
3531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3534 /* Add path to peers? */
3538 path_add_to_peers (p, GNUNET_YES);
3544 t->state = MESH_TUNNEL_READY;
3545 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3546 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3548 /* Message for us? */
3549 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3552 if (NULL == t->owner)
3554 GNUNET_break_op (0);
3557 if (NULL != peer_info->dhtget)
3559 GNUNET_DHT_get_stop (peer_info->dhtget);
3560 peer_info->dhtget = NULL;
3562 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3563 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3568 " not for us, retransmitting...\n");
3569 peer_info = peer_get (&msg->oid);
3570 send_prebuilt_message (message, t->prev_hop, t);
3576 * Core handler for notifications of broken paths
3578 * @param cls closure
3579 * @param message message
3580 * @param peer peer identity this notification is about
3582 * @return GNUNET_OK to keep the connection open,
3583 * GNUNET_SYSERR to close it (signal serious error)
3586 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3587 const struct GNUNET_MessageHeader *message)
3589 struct GNUNET_MESH_PathBroken *msg;
3590 struct MeshTunnel *t;
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3594 msg = (struct GNUNET_MESH_PathBroken *) message;
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3596 GNUNET_i2s (&msg->peer1));
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3598 GNUNET_i2s (&msg->peer2));
3599 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3602 GNUNET_break_op (0);
3605 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3606 GNUNET_PEER_search (&msg->peer2));
3613 * Core handler for tunnel destruction
3615 * @param cls closure
3616 * @param message message
3617 * @param peer peer identity this notification is about
3619 * @return GNUNET_OK to keep the connection open,
3620 * GNUNET_SYSERR to close it (signal serious error)
3623 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3624 const struct GNUNET_MessageHeader *message)
3626 struct GNUNET_MESH_TunnelDestroy *msg;
3627 struct MeshTunnel *t;
3629 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3631 "Got a TUNNEL DESTROY packet from %s\n",
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 " for tunnel %s [%u]\n",
3635 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3636 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3639 /* Probably already got the message from another path,
3640 * destroyed the tunnel and retransmitted to children.
3643 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3647 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3650 t->local_tid, t->local_tid_dest);
3652 if (GNUNET_PEER_search (peer) == t->prev_hop)
3654 // TODO check owner's signature
3655 // TODO add owner's signatue to tunnel for retransmission
3656 peer_cancel_queues (t->prev_hop, t);
3657 GNUNET_PEER_change_rc (t->prev_hop, -1);
3660 else if (GNUNET_PEER_search (peer) == t->next_hop)
3662 // TODO check dest's signature
3663 // TODO add dest's signatue to tunnel for retransmission
3664 peer_cancel_queues (t->next_hop, t);
3665 GNUNET_PEER_change_rc (t->next_hop, -1);
3670 GNUNET_break_op (0);
3671 // TODO check both owner AND destination's signature to see which matches
3672 // TODO restransmit in appropriate direction
3675 tunnel_destroy_empty (t);
3677 // TODO: add timeout to destroy the tunnel anyway
3683 * Core handler for mesh network traffic going from the origin to a peer
3685 * @param cls closure
3686 * @param peer peer identity this notification is about
3687 * @param message message
3688 * @return GNUNET_OK to keep the connection open,
3689 * GNUNET_SYSERR to close it (signal serious error)
3692 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3693 const struct GNUNET_MessageHeader *message)
3695 struct GNUNET_MESH_Data *msg;
3696 struct MeshTunnel *t;
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3704 size = ntohs (message->size);
3706 sizeof (struct GNUNET_MESH_Data) +
3707 sizeof (struct GNUNET_MessageHeader))
3712 msg = (struct GNUNET_MESH_Data *) message;
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3714 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3716 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3719 /* TODO notify back: we don't know this tunnel */
3720 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3721 GNUNET_break_op (0);
3724 pid = ntohl (msg->pid);
3725 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3727 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3728 GNUNET_break_op (0);
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3730 "Received PID %u, ACK %u\n",
3731 pid, t->prev_fc.last_ack_sent);
3732 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3736 tunnel_reset_timeout (t);
3737 if (t->dest == myid)
3739 /* TODO signature verification */
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3741 " it's for us! sending to clients...\n");
3742 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3743 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3744 if ( (GNUNET_NO == t->reliable &&
3745 GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3747 (GNUNET_YES == t->reliable &&
3748 pid == t->prev_fc.last_pid_recv + 1) )
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3751 " pid %u not seen yet, forwarding\n", pid);
3752 t->prev_fc.last_pid_recv = pid;
3753 tunnel_send_client_ucast (t, msg);
3757 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3759 " Pid %u not expected (%u), sending FWD ACK!\n",
3760 pid, t->prev_fc.last_pid_recv + 1);
3762 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3765 if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3766 t->prev_fc.last_pid_recv = pid;
3767 if (0 == t->next_hop)
3772 ttl = ntohl (msg->ttl);
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3776 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3778 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3782 " not for us, retransmitting...\n");
3784 send_prebuilt_message (message, t->next_hop, t);
3785 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3791 * Core handler for mesh network traffic toward the owner of a tunnel
3793 * @param cls closure
3794 * @param message message
3795 * @param peer peer identity this notification is about
3797 * @return GNUNET_OK to keep the connection open,
3798 * GNUNET_SYSERR to close it (signal serious error)
3801 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3802 const struct GNUNET_MessageHeader *message)
3804 struct GNUNET_MESH_Data *msg;
3805 struct MeshTunnel *t;
3810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3812 size = ntohs (message->size);
3813 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3814 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3816 GNUNET_break_op (0);
3819 msg = (struct GNUNET_MESH_Data *) message;
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3821 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3822 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3823 pid = ntohl (msg->pid);
3826 /* TODO notify that we dont know this tunnel (whom)? */
3827 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3830 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3834 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3836 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3837 GNUNET_break_op (0);
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3839 "Received PID %u, ACK %u\n",
3840 pid, t->next_fc.last_ack_sent);
3841 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3845 if (myid == t->id.oid)
3847 /* TODO signature verification */
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 " it's for us! sending to clients...\n");
3850 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3851 if ( (GNUNET_NO == t->reliable &&
3852 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
3854 (GNUNET_YES == t->reliable &&
3855 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
3857 t->next_fc.last_pid_recv = pid;
3858 tunnel_send_client_to_orig (t, msg);
3862 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3863 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3864 " Pid %u not expected, sending FWD ACK!\n", pid);
3866 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3870 " not for us, retransmitting...\n");
3871 if (GMC_is_pid_bigger (pid, t->next_fc.last_pid_recv))
3872 t->next_fc.last_pid_recv = pid;
3873 if (0 == t->prev_hop) /* No owner AND no prev hop */
3875 if (GNUNET_YES == t->destroy)
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3878 "to orig received on a dying tunnel %s [%X]\n",
3879 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3883 "unknown to origin at %s\n",
3884 GNUNET_i2s (&my_full_id));
3885 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3889 "on tunnel %s [%X]\n",
3890 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3893 ttl = ntohl (msg->ttl);
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3897 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3899 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3902 send_prebuilt_message (message, t->prev_hop, t);
3903 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3910 * Core handler for mesh network traffic end-to-end ACKs.
3912 * @param cls Closure.
3913 * @param message Message.
3914 * @param peer Peer identity this notification is about.
3916 * @return GNUNET_OK to keep the connection open,
3917 * GNUNET_SYSERR to close it (signal serious error)
3920 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3921 const struct GNUNET_MessageHeader *message)
3923 struct GNUNET_MESH_DataACK *msg;
3924 struct MeshTunnelReliability *rel;
3925 struct MeshReliableMessage *copy;
3926 struct MeshReliableMessage *next;
3927 struct MeshTunnel *t;
3933 type = ntohs (message->type);
3934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
3935 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
3936 msg = (struct GNUNET_MESH_DataACK *) message;
3938 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3941 /* TODO notify that we dont know this tunnel (whom)? */
3942 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3945 ack = ntohl (msg->pid);
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3948 /* Is this a forward or backward ACK? */
3949 id = GNUNET_PEER_search (peer);
3950 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3953 if (NULL == t->owner)
3955 send_prebuilt_message (message, t->prev_hop, t);
3959 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3961 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
3963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3964 if (NULL == t->client)
3966 send_prebuilt_message (message, t->next_hop, t);
3970 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3974 GNUNET_break_op (0);
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
3979 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
3981 struct GNUNET_TIME_Relative time;
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->id);
3989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->id);
3991 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
3992 rel->expected_delay.rel_value += time.rel_value;
3993 rel->expected_delay.rel_value /= 2;
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
3995 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
3997 rel->retry_timer = rel->expected_delay;
3998 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
4002 if (GNUNET_YES == work)
4004 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4006 GNUNET_SCHEDULER_cancel (rel->retry_task);
4007 if (NULL == rel->head_sent)
4009 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4013 struct GNUNET_TIME_Absolute new_target;
4014 struct GNUNET_TIME_Relative delay;
4016 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4018 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4020 GNUNET_SCHEDULER_add_delayed (delay,
4021 &tunnel_retransmit_message,
4032 * Core handler for mesh network traffic point-to-point acks.
4034 * @param cls closure
4035 * @param message message
4036 * @param peer peer identity this notification is about
4038 * @return GNUNET_OK to keep the connection open,
4039 * GNUNET_SYSERR to close it (signal serious error)
4042 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4043 const struct GNUNET_MessageHeader *message)
4045 struct GNUNET_MESH_ACK *msg;
4046 struct MeshTunnel *t;
4047 struct MeshFlowControl *fc;
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4053 msg = (struct GNUNET_MESH_ACK *) message;
4055 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4059 /* TODO notify that we dont know this tunnel (whom)? */
4060 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4063 ack = ntohl (msg->pid);
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4066 /* Is this a forward or backward ACK? */
4067 id = GNUNET_PEER_search (peer);
4068 if (t->next_hop == id)
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4074 else if (t->prev_hop == id)
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4082 GNUNET_break_op (0);
4086 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4087 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4089 GNUNET_SCHEDULER_cancel (fc->poll_task);
4090 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4091 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4093 fc->last_ack_recv = ack;
4094 peer_unlock_queue (id);
4096 if (t->next_hop == id)
4097 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4099 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4106 * Core handler for mesh network traffic point-to-point ack polls.
4108 * @param cls closure
4109 * @param message message
4110 * @param peer peer identity this notification is about
4112 * @return GNUNET_OK to keep the connection open,
4113 * GNUNET_SYSERR to close it (signal serious error)
4116 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4117 const struct GNUNET_MessageHeader *message)
4119 struct GNUNET_MESH_Poll *msg;
4120 struct MeshTunnel *t;
4121 struct MeshFlowControl *fc;
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4129 msg = (struct GNUNET_MESH_Poll *) message;
4131 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4135 /* TODO notify that we dont know this tunnel (whom)? */
4136 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4137 GNUNET_break_op (0);
4141 /* Is this a forward or backward ACK? */
4142 id = GNUNET_PEER_search(peer);
4143 pid = ntohl (msg->pid);
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4145 if (t->next_hop == id)
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4150 old = fc->last_pid_recv;
4151 fc->last_pid_recv = pid;
4152 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4154 else if (t->prev_hop == id)
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4159 old = fc->last_pid_recv;
4160 fc->last_pid_recv = pid;
4161 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4166 if (GNUNET_YES == t->reliable)
4167 fc->last_pid_recv = old;
4174 * Core handler for mesh keepalives.
4176 * @param cls closure
4177 * @param message message
4178 * @param peer peer identity this notification is about
4179 * @return GNUNET_OK to keep the connection open,
4180 * GNUNET_SYSERR to close it (signal serious error)
4182 * TODO: Check who we got this from, to validate route.
4185 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4186 const struct GNUNET_MessageHeader *message)
4188 struct GNUNET_MESH_TunnelKeepAlive *msg;
4189 struct MeshTunnel *t;
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4194 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4195 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4199 /* TODO notify that we dont know that tunnel */
4200 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4205 tunnel_reset_timeout (t);
4206 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4209 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4210 send_prebuilt_message (message, t->next_hop, t);
4217 * Functions to handle messages from core
4219 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4220 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4221 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4222 sizeof (struct GNUNET_MESH_PathBroken)},
4223 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4224 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4225 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4226 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4227 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4228 sizeof (struct GNUNET_MESH_DataACK)},
4229 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4230 sizeof (struct GNUNET_MESH_DataACK)},
4231 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4232 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4233 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4234 sizeof (struct GNUNET_MESH_ACK)},
4235 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4236 sizeof (struct GNUNET_MESH_Poll)},
4237 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4238 sizeof (struct GNUNET_MESH_PathACK)},
4244 /******************************************************************************/
4245 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4246 /******************************************************************************/
4251 * notify_client_connection_failure: notify a client that the connection to the
4252 * requested remote peer is not possible (for instance, no route found)
4253 * Function called when the socket is ready to queue more data. "buf" will be
4254 * NULL and "size" zero if the socket was closed for writing in the meantime.
4256 * @param cls closure
4257 * @param size number of bytes available in buf
4258 * @param buf where the callee should write the message
4259 * @return number of bytes written to buf
4262 notify_client_connection_failure (void *cls, size_t size, void *buf)
4265 struct MeshPeerInfo *peer_info;
4266 struct GNUNET_MESH_PeerControl *msg;
4267 struct GNUNET_PeerIdentity id;
4269 if (0 == size && NULL == buf)
4271 // TODO retry? cancel?
4275 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4276 peer_info = (struct MeshPeerInfo *) cls;
4277 msg = (struct GNUNET_MESH_PeerControl *) buf;
4278 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4279 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4280 // msg->tunnel_id = htonl(peer_info->t->tid);
4281 GNUNET_PEER_resolve (peer_info->id, &id);
4282 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4290 * Send keepalive packets for a tunnel.
4292 * @param cls Closure (tunnel for which to send the keepalive).
4293 * @param tc Notification context.
4295 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4298 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4300 struct MeshTunnel *t = cls;
4301 struct GNUNET_MESH_TunnelKeepAlive *msg;
4302 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4305 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4306 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4307 NULL == t->owner || 0 == t->local_tid)
4312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4313 "sending keepalive for tunnel %d\n", t->id.tid);
4315 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4316 msg->header.size = htons (size);
4317 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4318 msg->oid = my_full_id;
4319 msg->tid = htonl (t->id.tid);
4320 send_prebuilt_message (&msg->header, t->next_hop, t);
4322 t->maintenance_task =
4323 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4328 * Function to process paths received for a new peer addition. The recorded
4329 * paths form the initial tunnel, which can be optimized later.
4330 * Called on each result obtained for the DHT search.
4332 * @param cls closure
4333 * @param exp when will this value expire
4334 * @param key key of the result
4335 * @param get_path path of the get request
4336 * @param get_path_length lenght of get_path
4337 * @param put_path path of the put request
4338 * @param put_path_length length of the put_path
4339 * @param type type of the result
4340 * @param size number of bytes in data
4341 * @param data pointer to the result data
4344 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4345 const struct GNUNET_HashCode * key,
4346 const struct GNUNET_PeerIdentity *get_path,
4347 unsigned int get_path_length,
4348 const struct GNUNET_PeerIdentity *put_path,
4349 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4350 size_t size, const void *data)
4352 struct MeshPeerInfo *peer = cls;
4353 struct MeshPeerPath *p;
4354 struct GNUNET_PeerIdentity pi;
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4358 GNUNET_PEER_resolve (peer->id, &pi);
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4361 p = path_build_from_dht (get_path, get_path_length,
4362 put_path, put_path_length);
4363 path_add_to_peers (p, GNUNET_NO);
4365 for (i = 0; i < peer->ntunnels; i++)
4367 struct GNUNET_PeerIdentity id;
4369 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4371 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4372 peer->tunnels[i]->local_tid,
4373 peer->tunnels[i]->local_tid_dest);
4374 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4377 peer_connect (peer, peer->tunnels[i]);
4385 /******************************************************************************/
4386 /********************* MESH LOCAL HANDLES **************************/
4387 /******************************************************************************/
4391 * Handler for client disconnection
4393 * @param cls closure
4394 * @param client identification of the client; NULL
4395 * for the last call when the server is destroyed
4398 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4400 struct MeshClient *c;
4401 struct MeshClient *next;
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4410 c = client_get (client);
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4415 GNUNET_SERVER_client_drop (c->handle);
4416 c->shutting_down = GNUNET_YES;
4417 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4418 &tunnel_destroy_iterator, c);
4419 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4420 &tunnel_destroy_iterator, c);
4421 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4422 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4424 if (NULL != c->ports)
4425 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4427 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4430 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4443 * Handler for new clients
4445 * @param cls closure
4446 * @param client identification of the client
4447 * @param message the actual message, which includes messages the client wants
4450 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4451 const struct GNUNET_MessageHeader *message)
4453 struct GNUNET_MESH_ClientConnect *cc_msg;
4454 struct MeshClient *c;
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4461 /* Check data sanity */
4462 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4463 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4464 if (0 != (size % sizeof (uint32_t)))
4467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4470 size /= sizeof (uint32_t);
4472 /* Create new client structure */
4473 c = GNUNET_malloc (sizeof (struct MeshClient));
4474 c->id = next_client_id++; /* overflow not important: just for debug */
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4478 GNUNET_SERVER_client_keep (client);
4479 GNUNET_SERVER_client_set_user_context (client, c);
4484 p = (uint32_t *) &cc_msg[1];
4485 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4486 for (i = 0; i < size; i++)
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4491 /* store in client's hashmap */
4492 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4494 /* store in global hashmap */
4495 /* FIXME only allow one client to have the port open,
4496 * have a backup hashmap with waiting clients */
4497 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4502 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4503 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4504 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4505 GNUNET_SERVER_notification_context_add (nc, client);
4506 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4514 * Handler for requests of new tunnels
4516 * @param cls Closure.
4517 * @param client Identification of the client.
4518 * @param message The actual message.
4521 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4522 const struct GNUNET_MessageHeader *message)
4524 struct GNUNET_MESH_TunnelMessage *t_msg;
4525 struct MeshPeerInfo *peer_info;
4526 struct MeshTunnel *t;
4527 struct MeshClient *c;
4528 MESH_TunnelNumber tid;
4530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4532 /* Sanity check for client registration */
4533 if (NULL == (c = client_get (client)))
4536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4541 /* Message size sanity check */
4542 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4549 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4551 GNUNET_i2s (&t_msg->peer));
4552 /* Sanity check for tunnel numbering */
4553 tid = ntohl (t_msg->tunnel_id);
4554 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4560 /* Sanity check for duplicate tunnel IDs */
4561 if (NULL != tunnel_get_by_local_id (c, tid))
4564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4569 while (NULL != tunnel_get_by_pi (myid, next_tid))
4570 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4571 t = tunnel_new (myid, next_tid, c, tid);
4572 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4579 t->port = ntohl (t_msg->port);
4580 tunnel_set_options (t, ntohl (t_msg->options));
4581 if (GNUNET_YES == t->reliable)
4583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4584 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4586 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4590 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4592 peer_info = peer_get (&t_msg->peer);
4593 peer_info_add_tunnel (peer_info, t);
4594 peer_connect (peer_info, t);
4595 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4601 * Handler for requests of deleting tunnels
4603 * @param cls closure
4604 * @param client identification of the client
4605 * @param message the actual message
4608 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4609 const struct GNUNET_MessageHeader *message)
4611 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4612 struct MeshClient *c;
4613 struct MeshTunnel *t;
4614 MESH_TunnelNumber tid;
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4617 "Got a DESTROY TUNNEL from client!\n");
4619 /* Sanity check for client registration */
4620 if (NULL == (c = client_get (client)))
4623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4628 /* Message sanity check */
4629 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4636 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4638 /* Retrieve tunnel */
4639 tid = ntohl (tunnel_msg->tunnel_id);
4640 t = tunnel_get_by_local_id(c, tid);
4643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4645 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4649 /* Cleanup after the tunnel */
4650 client_delete_tunnel (c, t);
4657 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4661 /* The tunnel will be destroyed when the last message is transmitted. */
4662 tunnel_destroy_empty (t);
4664 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4670 * Handler for client traffic
4672 * @param cls closure
4673 * @param client identification of the client
4674 * @param message the actual message
4677 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4678 const struct GNUNET_MessageHeader *message)
4680 struct GNUNET_MESH_LocalData *data_msg;
4681 struct MeshClient *c;
4682 struct MeshTunnel *t;
4683 struct MeshFlowControl *fc;
4684 MESH_TunnelNumber tid;
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4688 "Got data from a client!\n");
4690 /* Sanity check for client registration */
4691 if (NULL == (c = client_get (client)))
4694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4699 data_msg = (struct GNUNET_MESH_LocalData *) message;
4701 /* Sanity check for message size */
4702 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4703 if (size < sizeof (struct GNUNET_MessageHeader))
4706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4710 /* Tunnel exists? */
4711 tid = ntohl (data_msg->tid);
4712 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4718 t = tunnel_get_by_local_id (c, tid);
4722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4726 /* Is the client in the tunnel? */
4727 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4729 t->owner->handle == client)
4731 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4733 t->client->handle == client) ) )
4736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4740 /* Ok, everything is correct, send the message
4741 * (pretend we got it from a mesh peer)
4744 struct GNUNET_MESH_Data *payload;
4745 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4747 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4748 if (GNUNET_YES == t->reliable)
4750 struct MeshTunnelReliability *rel;
4751 struct MeshReliableMessage *copy;
4753 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
4754 + sizeof(struct GNUNET_MESH_Data)
4756 copy->id = fc->last_pid_recv + 1;
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->id);
4758 copy->timestamp = GNUNET_TIME_absolute_get ();
4759 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
4761 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
4762 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
4765 GNUNET_TIME_relative_multiply (rel->expected_delay,
4766 MESH_RETRANSMIT_MARGIN);
4768 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
4769 &tunnel_retransmit_message,
4772 payload = (struct GNUNET_MESH_Data *) ©[1];
4776 payload = (struct GNUNET_MESH_Data *) cbuf;
4778 memcpy (&payload[1], &data_msg[1], size);
4779 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
4780 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4781 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
4782 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4783 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
4784 payload->tid = htonl (t->id.tid);
4785 payload->ttl = htonl (default_ttl);
4786 payload->pid = htonl (fc->last_pid_recv + 1);
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4788 " calling generic handler...\n");
4789 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4790 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4792 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4795 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4802 * Handler for client's ACKs for payload traffic.
4804 * @param cls Closure (unused).
4805 * @param client Identification of the client.
4806 * @param message The actual message.
4809 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4810 const struct GNUNET_MessageHeader *message)
4812 struct GNUNET_MESH_LocalAck *msg;
4813 struct MeshTunnel *t;
4814 struct MeshClient *c;
4815 MESH_TunnelNumber tid;
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4818 /* Sanity check for client registration */
4819 if (NULL == (c = client_get (client)))
4822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4827 msg = (struct GNUNET_MESH_LocalAck *) message;
4829 /* Tunnel exists? */
4830 tid = ntohl (msg->tunnel_id);
4831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4832 t = tunnel_get_by_local_id (c, tid);
4836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4837 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4842 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4845 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4846 t->prev_fc.last_ack_recv++;
4847 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4851 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4852 t->next_fc.last_ack_recv++;
4853 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4856 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4864 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4866 * @param cls Closure (client handle).
4867 * @param key Key (hashed tunnel ID, unused).
4868 * @param value Tunnel info.
4870 * @return GNUNET_YES, to keep iterating.
4873 monitor_all_tunnels_iterator (void *cls,
4874 const struct GNUNET_HashCode * key,
4877 struct GNUNET_SERVER_Client *client = cls;
4878 struct MeshTunnel *t = value;
4879 struct GNUNET_MESH_LocalMonitor *msg;
4881 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4882 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4883 msg->tunnel_id = htonl (t->id.tid);
4884 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4885 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4886 GNUNET_PEER_resolve (t->dest, &msg->destination);
4888 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4889 "* sending info about tunnel %s [%u]\n",
4890 GNUNET_i2s (&msg->owner), t->id.tid);
4892 GNUNET_SERVER_notification_context_unicast (nc, client,
4893 &msg->header, GNUNET_NO);
4899 * Handler for client's MONITOR request.
4901 * @param cls Closure (unused).
4902 * @param client Identification of the client.
4903 * @param message The actual message.
4906 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4907 const struct GNUNET_MessageHeader *message)
4909 struct MeshClient *c;
4911 /* Sanity check for client registration */
4912 if (NULL == (c = client_get (client)))
4915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4919 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4920 "Received get tunnels request from client %u\n",
4922 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4923 monitor_all_tunnels_iterator,
4925 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4926 "Get tunnels request from client %u completed\n",
4928 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4933 * Handler for client's MONITOR_TUNNEL request.
4935 * @param cls Closure (unused).
4936 * @param client Identification of the client.
4937 * @param message The actual message.
4940 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4941 const struct GNUNET_MessageHeader *message)
4943 const struct GNUNET_MESH_LocalMonitor *msg;
4944 struct GNUNET_MESH_LocalMonitor *resp;
4945 struct MeshClient *c;
4946 struct MeshTunnel *t;
4948 /* Sanity check for client registration */
4949 if (NULL == (c = client_get (client)))
4952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4956 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4957 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4958 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4961 ntohl (msg->tunnel_id));
4962 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4965 /* We don't know the tunnel FIXME */
4966 struct GNUNET_MESH_LocalMonitor warn;
4969 GNUNET_SERVER_notification_context_unicast (nc, client,
4972 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4976 /* Initialize context */
4977 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4979 GNUNET_PEER_resolve (t->dest, &resp->destination);
4980 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4981 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4982 &resp->header, GNUNET_NO);
4985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4986 "Monitor tunnel request from client %u completed\n",
4988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4993 * Functions to handle messages from clients
4995 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4996 {&handle_local_new_client, NULL,
4997 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4998 {&handle_local_tunnel_create, NULL,
4999 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5000 sizeof (struct GNUNET_MESH_TunnelMessage)},
5001 {&handle_local_tunnel_destroy, NULL,
5002 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5003 sizeof (struct GNUNET_MESH_TunnelMessage)},
5004 {&handle_local_data, NULL,
5005 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5006 {&handle_local_ack, NULL,
5007 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5008 sizeof (struct GNUNET_MESH_LocalAck)},
5009 {&handle_local_get_tunnels, NULL,
5010 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5011 sizeof (struct GNUNET_MessageHeader)},
5012 {&handle_local_show_tunnel, NULL,
5013 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5014 sizeof (struct GNUNET_MESH_LocalMonitor)},
5020 * Method called whenever a given peer connects.
5022 * @param cls closure
5023 * @param peer peer identity this notification is about
5026 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5028 struct MeshPeerInfo *peer_info;
5029 struct MeshPeerPath *path;
5031 DEBUG_CONN ("Peer connected\n");
5032 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5033 peer_info = peer_get (peer);
5034 if (myid == peer_info->id)
5036 DEBUG_CONN (" (self)\n");
5037 path = path_new (1);
5041 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5042 path = path_new (2);
5043 path->peers[1] = peer_info->id;
5044 GNUNET_PEER_change_rc (peer_info->id, 1);
5045 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5047 path->peers[0] = myid;
5048 GNUNET_PEER_change_rc (myid, 1);
5049 peer_info_add_path (peer_info, path, GNUNET_YES);
5055 * Method called whenever a peer disconnects.
5057 * @param cls closure
5058 * @param peer peer identity this notification is about
5061 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5063 struct MeshPeerInfo *pi;
5064 struct MeshPeerQueue *q;
5065 struct MeshPeerQueue *n;
5067 DEBUG_CONN ("Peer disconnected\n");
5068 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5078 /* TODO try to reroute this traffic instead */
5079 queue_destroy(q, GNUNET_YES);
5082 if (NULL != pi->core_transmit)
5084 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5085 pi->core_transmit = NULL;
5087 peer_remove_path (pi, pi->id, myid);
5090 DEBUG_CONN (" (self)\n");
5092 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5098 * Install server (service) handlers and start listening to clients.
5103 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5104 GNUNET_SERVER_disconnect_notify (server_handle,
5105 &handle_local_client_disconnect, NULL);
5106 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5108 clients_head = NULL;
5109 clients_tail = NULL;
5111 GNUNET_SERVER_resume (server_handle);
5116 * To be called on core init/fail.
5118 * @param cls Closure (config)
5119 * @param server handle to the server for this service
5120 * @param identity the public identity of this peer
5123 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5124 const struct GNUNET_PeerIdentity *identity)
5126 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5130 GNUNET_break (core_handle == server);
5131 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5137 GNUNET_i2s (identity));
5138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5140 GNUNET_i2s (&my_full_id));
5141 GNUNET_CORE_disconnect (core_handle);
5142 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5143 NULL, /* Closure passed to MESH functions */
5144 &core_init, /* Call core_init once connected */
5145 &core_connect, /* Handle connects */
5146 &core_disconnect, /* remove peers on disconnects */
5147 NULL, /* Don't notify about all incoming messages */
5148 GNUNET_NO, /* For header only in notification */
5149 NULL, /* Don't notify about all outbound messages */
5150 GNUNET_NO, /* For header-only out notification */
5151 core_handlers); /* Register these handlers */
5160 /******************************************************************************/
5161 /************************ MAIN FUNCTIONS ****************************/
5162 /******************************************************************************/
5165 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5167 * @param cls closure
5168 * @param key current key code
5169 * @param value value in the hash map
5170 * @return GNUNET_YES if we should continue to iterate,
5174 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5176 struct MeshTunnel *t = value;
5183 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5185 * @param cls closure
5186 * @param key current key code
5187 * @param value value in the hash map
5188 * @return GNUNET_YES if we should continue to iterate,
5192 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5194 struct MeshPeerInfo *p = value;
5195 struct MeshPeerQueue *q;
5196 struct MeshPeerQueue *n;
5204 queue_destroy(q, GNUNET_YES);
5208 peer_info_destroy (p);
5214 * Task run during shutdown.
5220 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5224 if (core_handle != NULL)
5226 GNUNET_CORE_disconnect (core_handle);
5229 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5230 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5231 if (dht_handle != NULL)
5233 GNUNET_DHT_disconnect (dht_handle);
5238 GNUNET_SERVER_notification_context_destroy (nc);
5241 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5243 GNUNET_SCHEDULER_cancel (announce_id_task);
5244 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5251 * Process mesh requests.
5253 * @param cls closure
5254 * @param server the initialized server
5255 * @param c configuration to use
5258 run (void *cls, struct GNUNET_SERVER_Handle *server,
5259 const struct GNUNET_CONFIGURATION_Handle *c)
5262 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5265 server_handle = server;
5266 GNUNET_SERVER_suspend (server_handle);
5269 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5274 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5275 "mesh", "peer/privatekey");
5276 GNUNET_SCHEDULER_shutdown ();
5281 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5282 &refresh_path_time))
5284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5286 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5287 "mesh", "refresh path time");
5288 GNUNET_SCHEDULER_shutdown ();
5293 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5298 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5299 "mesh", "id announce time");
5300 GNUNET_SCHEDULER_shutdown ();
5305 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5310 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5311 "mesh", "connect timeout");
5312 GNUNET_SCHEDULER_shutdown ();
5317 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5322 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5323 "mesh", "max msgs queue");
5324 GNUNET_SCHEDULER_shutdown ();
5329 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5334 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5335 "mesh", "max tunnels");
5336 GNUNET_SCHEDULER_shutdown ();
5341 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5346 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5347 "mesh", "default ttl", 64);
5352 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5356 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5357 "mesh", "max peers", 1000);
5362 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5370 "Mesh is running with drop mode enabled. "
5371 "This is NOT a good idea! "
5372 "Remove the DROP_PERCENT option from your configuration.\n");
5376 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5377 &dht_replication_level))
5379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5381 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5382 "mesh", "dht replication level", 3);
5383 dht_replication_level = 3;
5386 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5387 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5388 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5389 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5391 dht_handle = GNUNET_DHT_connect (c, 64);
5392 if (NULL == dht_handle)
5396 stats = GNUNET_STATISTICS_create ("mesh", c);
5398 /* Scheduled the task to clean up when shutdown is called */
5399 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5401 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5402 GNUNET_free (keyfile);
5403 GNUNET_assert (NULL != pk);
5404 my_private_key = pk;
5405 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5406 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5407 &my_full_id.hashPubKey);
5408 myid = GNUNET_PEER_intern (&my_full_id);
5409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5410 "Mesh for peer [%s] starting\n",
5411 GNUNET_i2s(&my_full_id));
5413 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5414 NULL, /* Closure passed to MESH functions */
5415 &core_init, /* Call core_init once connected */
5416 &core_connect, /* Handle connects */
5417 &core_disconnect, /* remove peers on disconnects */
5418 NULL, /* Don't notify about all incoming messages */
5419 GNUNET_NO, /* For header only in notification */
5420 NULL, /* Don't notify about all outbound messages */
5421 GNUNET_NO, /* For header-only out notification */
5422 core_handlers); /* Register these handlers */
5423 if (NULL == core_handle)
5426 GNUNET_SCHEDULER_shutdown ();
5430 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5431 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5437 * The main function for the mesh service.
5439 * @param argc number of arguments from the command line
5440 * @param argv command line arguments
5441 * @return 0 ok, 1 on error
5444 main (int argc, char *const *argv)
5449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5450 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5452 ret = (GNUNET_OK == r) ? 0 : 1;
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5457 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5458 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5459 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);