2 This file is part of GNUnet.
3 (C) 2001-2012 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
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_regex_lib.h"
57 #define MESH_BLOOM_SIZE 128
59 #define MESH_DEBUG_REGEX GNUNET_YES
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
81 #define DEBUG_REGEX(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
83 #define DEBUG_REGEX(...)
90 struct timespec __mesh_start;
91 struct timespec __mesh_end;
92 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
93 #define INTERVAL_END \
95 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
96 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
97 if (__diff < 0) __diff += 1000000000;\
101 #define INTERVAL_SHOW \
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
105 #define INTERVAL_START
107 #define INTERVAL_SHOW
110 /******************************************************************************/
111 /************************ DATA STRUCTURES ****************************/
112 /******************************************************************************/
114 /** FWD declaration */
120 * Struct representing a piece of data being sent to other peers
124 /** Tunnel it belongs to. */
125 struct MeshTunnel *t;
127 /** How many remaining neighbors still hav't got it. */
128 unsigned int reference_counter;
130 /** How many remaining neighbors we need to send this to. */
131 unsigned int total_out;
133 /** Size of the data. */
142 * Struct containing info about a queued transmission to this peer
149 struct MeshPeerQueue *next;
154 struct MeshPeerQueue *prev;
157 * Peer this transmission is directed to.
159 struct MeshPeerInfo *peer;
162 * Tunnel this message belongs to.
164 struct MeshTunnel *tunnel;
167 * Pointer to info stucture used as cls.
177 * Size of the message
184 * Struct to store regex information announced by clients.
186 struct MeshRegexDescriptor
189 * Regular expression itself.
194 * How many characters per edge can we squeeze?
196 uint16_t compression;
199 * Handle to announce the regex.
201 struct GNUNET_REGEX_announce_handle *h;
206 * Struct to keep information of searches of services described by a regex
207 * using a user-provided string service description.
209 struct MeshRegexSearchInfo
212 * Which tunnel is this for
214 struct MeshTunnel *t;
217 * User provided description of the searched service.
222 * Regex search handle.
224 struct GNUNET_REGEX_search_handle *search_handle;
227 * Peer that is connecting via connect_by_string. When connected, free ctx.
232 * Other peers that are found but not yet being connected to.
234 GNUNET_PEER_Id *peers;
237 * Number of elements in peers.
239 unsigned int n_peers;
242 * Next peer to try to connect to.
247 * Timeout for a connect attempt.
248 * When reached, try to connect to a different peer, if any. If not,
249 * try the same peer again.
251 GNUNET_SCHEDULER_TaskIdentifier timeout;
257 * Struct containing all info possibly needed to build a package when called
260 struct MeshTransmissionDescriptor
262 /** ID of the tunnel this packet travels in */
263 struct MESH_TunnelID *origin;
265 /** Who was this message being sent to */
266 struct MeshPeerInfo *peer;
268 /** Ultimate destination of the packet */
269 GNUNET_PEER_Id destination;
271 /** Data descriptor */
272 struct MeshData* mesh_data;
277 * Struct containing all information regarding a given peer
287 * Last time we heard from this peer
289 struct GNUNET_TIME_Absolute last_contact;
292 * Task handler for delayed connect task;
294 GNUNET_SCHEDULER_TaskIdentifier connect_task;
297 * Number of attempts to reconnect so far
299 int n_reconnect_attempts;
302 * Paths to reach the peer, ordered by ascending hop count
304 struct MeshPeerPath *path_head;
307 * Paths to reach the peer, ordered by ascending hop count
309 struct MeshPeerPath *path_tail;
312 * Handle to stop the DHT search for a path to this peer
314 struct GNUNET_DHT_GetHandle *dhtget;
317 * Closure given to the DHT GET
319 struct MeshPathInfo *dhtgetcls;
322 * Array of tunnels this peer participates in
323 * (most probably a small amount, therefore not a hashmap)
324 * When the path to the peer changes, notify these tunnels to let them
325 * re-adjust their path trees.
327 struct MeshTunnel **tunnels;
330 * Number of tunnels this peers participates in
332 unsigned int ntunnels;
335 * Transmission queue to core DLL head
337 struct MeshPeerQueue *queue_head;
340 * Transmission queue to core DLL tail
342 struct MeshPeerQueue *queue_tail;
345 * How many messages are in the queue to this peer.
347 unsigned int queue_n;
350 * Handle to for queued transmissions
352 struct GNUNET_CORE_TransmitHandle *core_transmit;
357 * Globally unique tunnel identification (owner + number)
358 * DO NOT USE OVER THE NETWORK
363 * Node that owns the tunnel
368 * Tunnel number to differentiate all the tunnels owned by the node oid
369 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
371 MESH_TunnelNumber tid;
376 * Struct containing all information regarding a tunnel
377 * For an intermediate node the improtant info used will be:
378 * - id Tunnel unique identification
379 * - paths[0] To know where to send it next
380 * - metainfo: ready, speeds, accounting
387 struct MESH_TunnelID id;
390 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
392 MESH_TunnelNumber local_tid;
395 * Local tunnel number for local destination clients (incoming number)
396 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
399 MESH_TunnelNumber local_tid_dest;
402 * Is the tunnel bufferless (minimum latency)?
407 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
412 * Packet ID of the last bck packet sent (unique counter per hop).
417 * Force sending ACK? Flag to allow duplicate ACK on POLL.
422 * Last ACK sent towards the origin (for traffic towards leaf node).
424 uint32_t last_fwd_ack;
427 * BCK ACK value received from the hop towards the owner of the tunnel,
428 * (previous node / owner): up to what message PID can we sent back to him.
433 * How many messages are in the forward queue (towards leaves).
435 unsigned int fwd_queue_n;
438 * How many messages do we accept in the forward queue.
440 unsigned int fwd_queue_max;
443 * How many messages are in the backward queue (towards origin).
445 unsigned int bck_queue_n;
448 * How many messages do we accept in the backward queue.
450 unsigned int bck_queue_max;
453 * Task to poll peer in case of a stall.
455 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
458 * Last time the tunnel was used
460 struct GNUNET_TIME_Absolute timestamp;
463 * Peer of the tunnel.
468 * Client owner of the tunnel, if any
470 struct MeshClient *owner;
473 * Clients that have been informed about and want to stay in the tunnel.
475 struct MeshClient **clients;
478 * Flow control info for each client.
480 struct MeshTunnelClientInfo *clients_fc;
483 * Number of elements in clients/clients_fc
485 unsigned int nclients;
488 * Clients that have been informed but requested to leave the tunnel.
490 struct MeshClient **ignore;
493 * Number of elements in clients
495 unsigned int nignore;
498 * Bloomfilter (for peer identities) to stop circular routes
500 char bloomfilter[MESH_BLOOM_SIZE];
506 struct MeshTunnelTree *tree;
509 * Task to keep the used paths alive
511 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
514 * Task to destroy the tunnel after timeout
516 * FIXME: merge the two? a tunnel will have either
517 * a path refresh OR a timeout, never both!
519 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
522 * Flag to signal the destruction of the tunnel.
523 * If this is set GNUNET_YES the tunnel will be destroyed
524 * when the queue is empty.
529 * Total messages pending for this tunnels, payload or not.
531 unsigned int pending_messages;
534 * If the tunnel is empty, destoy it.
536 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
541 * Info about a leaf client of a tunnel, needed to perform flow control.
543 struct MeshTunnelClientInfo
546 * PID of the last packet sent to the client (FWD).
551 * PID of the last packet received from the client (BCK).
556 * Maximum PID allowed (FWD ACK received).
561 * Last ACK sent to that child (BCK ACK).
568 * Info needed to work with tunnel paths and peers
575 struct MeshTunnel *t;
578 * Neighbouring peer to whom we send the packet to
580 struct MeshPeerInfo *peer;
585 struct MeshPeerPath *path;
590 * Struct containing information about a client of the service
597 struct MeshClient *next;
602 struct MeshClient *prev;
605 * Tunnels that belong to this client, indexed by local id
607 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
610 * Tunnels this client has accepted, indexed by incoming local id
612 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
615 * Tunnels this client has rejected, indexed by incoming local id
617 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
619 * Handle to communicate with the client
621 struct GNUNET_SERVER_Client *handle;
624 * Messages that this client has declared interest in
626 struct GNUNET_CONTAINER_MultiHashMap *types;
629 * Whether the client is active or shutting down (don't send confirmations
630 * to a client that is shutting down.
635 * ID of the client, mainly for debug messages
642 /******************************************************************************/
643 /************************ DEBUG FUNCTIONS ****************************/
644 /******************************************************************************/
648 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
649 * @param cls string to print
650 * @param success GNUNET_OK if the PUT was transmitted,
651 * GNUNET_NO on timeout,
652 * GNUNET_SYSERR on disconnect from service
653 * after the PUT message was transmitted
654 * (so we don't know if it was received or not)
659 mesh_debug (void *cls, int success)
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
667 unsigned int debug_fwd_ack;
668 unsigned int debug_bck_ack;
672 /******************************************************************************/
673 /*********************** GLOBAL VARIABLES ****************************/
674 /******************************************************************************/
676 /************************** Configuration parameters **************************/
679 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
681 static struct GNUNET_TIME_Relative refresh_path_time;
684 * How often to PUT local application numbers in the DHT.
686 static struct GNUNET_TIME_Relative app_announce_time;
689 * How often to PUT own ID in the DHT.
691 static struct GNUNET_TIME_Relative id_announce_time;
694 * Maximum time allowed to connect to a peer found by string.
696 static struct GNUNET_TIME_Relative connect_timeout;
699 * Default TTL for payload packets.
701 static unsigned long long default_ttl;
704 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
706 static unsigned long long dht_replication_level;
709 * How many tunnels are we willing to maintain.
710 * Local tunnels are always allowed, even if there are more tunnels than max.
712 static unsigned long long max_tunnels;
715 * How many messages *in total* are we willing to queue, divided by number of
716 * tunnels to get tunnel queue size.
718 static unsigned long long max_msgs_queue;
721 * How many peers do we want to remember?
723 static unsigned long long max_peers;
726 /*************************** Static global variables **************************/
729 * Hostkey generation context
731 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
734 * DLL with all the clients, head.
736 static struct MeshClient *clients;
739 * DLL with all the clients, tail.
741 static struct MeshClient *clients_tail;
744 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
746 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
749 * Number of tunnels known.
751 static unsigned long long n_tunnels;
754 * Tunnels incoming, indexed by MESH_TunnelNumber
755 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
757 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
760 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
762 static struct GNUNET_CONTAINER_MultiHashMap *peers;
765 * Handle to communicate with transport
767 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
770 * Handle to communicate with core.
772 static struct GNUNET_CORE_Handle *core_handle;
777 static struct GNUNET_DHT_Handle *dht_handle;
782 static struct GNUNET_SERVER_Handle *server_handle;
785 * Handle to the statistics service.
787 static struct GNUNET_STATISTICS_Handle *stats;
790 * Notification context, to send messages to local clients.
792 static struct GNUNET_SERVER_NotificationContext *nc;
795 * Local peer own ID (memory efficient handle).
797 static GNUNET_PEER_Id myid;
800 * Local peer own ID (full value).
802 static struct GNUNET_PeerIdentity my_full_id;
807 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
812 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
815 * Tunnel ID for the next created tunnel (global tunnel number).
817 static MESH_TunnelNumber next_tid;
820 * Tunnel ID for the next incoming tunnel (local tunnel number).
822 static MESH_TunnelNumber next_local_tid;
825 * All message types clients of this peer are interested in.
827 static struct GNUNET_CONTAINER_MultiHashMap *types;
830 * Task to periodically announce itself in the network.
832 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
835 * Next ID to assign to a client.
837 unsigned int next_client_id;
840 /******************************************************************************/
841 /*********************** DECLARATIONS **************************/
842 /******************************************************************************/
845 * Function to process paths received for a new peer addition. The recorded
846 * paths form the initial tunnel, which can be optimized later.
847 * Called on each result obtained for the DHT search.
850 * @param exp when will this value expire
851 * @param key key of the result
852 * @param type type of the result
853 * @param size number of bytes in data
854 * @param data pointer to the result data
857 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
858 const struct GNUNET_HashCode * key,
859 const struct GNUNET_PeerIdentity *get_path,
860 unsigned int get_path_length,
861 const struct GNUNET_PeerIdentity *put_path,
862 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
863 size_t size, const void *data);
867 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
868 * and insert it in the appropiate structures if the peer is not known yet.
870 * @param peer Full identity of the peer.
872 * @return Existing or newly created peer info.
874 static struct MeshPeerInfo *
875 peer_info_get (const struct GNUNET_PeerIdentity *peer);
879 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
880 * and insert it in the appropiate structures if the peer is not known yet.
882 * @param peer Short identity of the peer.
884 * @return Existing or newly created peer info.
886 static struct MeshPeerInfo *
887 peer_info_get_short (const GNUNET_PEER_Id peer);
891 * Try to establish a new connection to this peer.
892 * Use the best path for the given tunnel.
893 * If the peer doesn't have any path to it yet, try to get one.
894 * If the peer already has some path, send a CREATE PATH towards it.
896 * @param peer PeerInfo of the peer.
897 * @param t Tunnel for which to create the path, if possible.
900 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
904 * Build a PeerPath from the paths returned from the DHT, reversing the paths
905 * to obtain a local peer -> destination path and interning the peer ids.
907 * @return Newly allocated and created path
909 static struct MeshPeerPath *
910 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
911 unsigned int get_path_length,
912 const struct GNUNET_PeerIdentity *put_path,
913 unsigned int put_path_length);
917 * Adds a path to the peer_infos of all the peers in the path
919 * @param p Path to process.
920 * @param confirmed Whether we know if the path works or not.
923 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
928 * Search for a tunnel by global ID using full PeerIdentities.
930 * @param oid owner of the tunnel.
931 * @param tid global tunnel number.
933 * @return tunnel handler, NULL if doesn't exist.
935 static struct MeshTunnel *
936 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
940 * Delete an active client from the tunnel.
946 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
949 * Notify a tunnel that a connection has broken that affects at least
952 * @param t Tunnel affected.
953 * @param p1 Peer that got disconnected from p2.
954 * @param p2 Peer that got disconnected from p1.
956 * @return Short ID of the peer disconnected (either p1 or p2).
957 * 0 if the tunnel remained unaffected.
959 static GNUNET_PEER_Id
960 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
965 * Get the current ack value for a tunnel, for data going from root to leaves,
966 * taking in account the tunnel mode and the status of all children and clients.
970 * @return Maximum PID allowed.
973 tunnel_get_fwd_ack (struct MeshTunnel *t);
977 * Add a client to a tunnel, initializing all needed data structures.
979 * @param t Tunnel to which add the client.
980 * @param c Client which to add to the tunnel.
983 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
987 * @brief Queue and pass message to core when possible.
989 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
990 * and accounts for it. In case the queue is full, the message is dropped and
993 * Otherwise, message is treated as internal and allowed to go regardless of
996 * @param cls Closure (@c type dependant). It will be used by queue_send to
997 * build the message to be sent if not already prebuilt.
998 * @param type Type of the message, 0 for a raw message.
999 * @param size Size of the message.
1000 * @param dst Neighbor to send message to.
1001 * @param t Tunnel this message belongs to.
1004 queue_add (void *cls, uint16_t type, size_t size,
1005 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1009 * Free a transmission that was already queued with all resources
1010 * associated to the request.
1012 * @param queue Queue handler to cancel.
1013 * @param clear_cls Is it necessary to free associated cls?
1016 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1020 * @brief Get the next transmittable message from the queue.
1022 * This will be the head, except in the case of being a data packet
1023 * not allowed by the destination peer.
1025 * @param peer Destination peer.
1027 * @return The next viable MeshPeerQueue element to send to that peer.
1028 * NULL when there are no transmittable messages.
1030 struct MeshPeerQueue *
1031 queue_get_next (const struct MeshPeerInfo *peer);
1035 * Core callback to write a queued packet to core buffer
1037 * @param cls Closure (peer info).
1038 * @param size Number of bytes available in buf.
1039 * @param buf Where the to write the message.
1041 * @return number of bytes written to buf
1044 queue_send (void *cls, size_t size, void *buf);
1047 /******************************************************************************/
1048 /************************ PERIODIC FUNCTIONS ****************************/
1049 /******************************************************************************/
1052 * Periodically announce self id in the DHT
1054 * @param cls closure
1055 * @param tc task context
1058 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1060 struct PBlock block;
1062 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1064 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1068 * - Set data expiration in function of X
1069 * - Adapt X to churn
1071 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1073 block.id = my_full_id;
1074 block.type = htonl (0);
1075 GNUNET_DHT_put (dht_handle, /* DHT handle */
1076 &my_full_id.hashPubKey, /* Key to use */
1077 dht_replication_level, /* Replication level */
1078 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1079 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1080 sizeof (block), /* Size of the data */
1081 (const char *) &block, /* Data itself */
1082 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1083 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1084 NULL, /* Continuation */
1085 NULL); /* Continuation closure */
1087 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1091 /******************************************************************************/
1092 /****************** GENERAL HELPER FUNCTIONS ************************/
1093 /******************************************************************************/
1096 * Decrements the reference counter and frees all resources if needed
1098 * @param mesh_data Data Descriptor used in a multicast message.
1099 * Freed no longer needed (last message).
1102 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1104 if (0 == --(mesh_data->reference_counter))
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1107 GNUNET_free (mesh_data->data);
1108 GNUNET_free (mesh_data);
1114 * Check if client has registered with the service and has not disconnected
1116 * @param client the client to check
1118 * @return non-NULL if client exists in the global DLL
1120 static struct MeshClient *
1121 client_get (struct GNUNET_SERVER_Client *client)
1123 struct MeshClient *c;
1128 if (c->handle == client)
1137 * Checks if a given client has subscribed to certain message type
1139 * @param message_type Type of message to check
1140 * @param c Client to check
1142 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1144 * FIXME: use of crypto_hash slows it down
1145 * The hash function alone takes 8-10us out of the ~55us for the whole
1146 * process of retransmitting the message from one local client to another.
1147 * Find faster implementation!
1150 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1152 struct GNUNET_HashCode hc;
1154 if (NULL == c->types)
1157 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1158 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1163 * Check whether client wants traffic from a tunnel.
1165 * @param c Client to check.
1166 * @param t Tunnel to be found.
1168 * @return GNUNET_YES if client knows tunnel.
1170 * TODO look in client hashmap
1173 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1177 for (i = 0; i < t->nclients; i++)
1178 if (t->clients[i] == c)
1185 * Check whether client has been informed about a tunnel.
1187 * @param c Client to check.
1188 * @param t Tunnel to be found.
1190 * @return GNUNET_YES if client knows tunnel.
1192 * TODO look in client hashmap
1195 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1199 for (i = 0; i < t->nignore; i++)
1200 if (t->ignore[i] == c)
1202 return client_wants_tunnel(c, t);
1207 * Marks a client as uninterested in traffic from the tunnel, updating both
1208 * client and tunnel to reflect this.
1210 * @param c Client that doesn't want traffic anymore.
1211 * @param t Tunnel which should be ignored.
1213 * FIXME when to delete an incoming tunnel?
1216 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1218 struct GNUNET_HashCode hash;
1220 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1221 GNUNET_break (GNUNET_YES ==
1222 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1224 GNUNET_break (GNUNET_YES ==
1225 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1226 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1227 tunnel_delete_active_client (t, c);
1228 GNUNET_array_append (t->ignore, t->nignore, c);
1233 * Deletes a tunnel from a client (either owner or destination). To be used on
1234 * tunnel destroy, otherwise, use client_ignore_tunnel.
1236 * @param c Client whose tunnel to delete.
1237 * @param t Tunnel which should be deleted.
1240 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1242 struct GNUNET_HashCode hash;
1246 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1247 GNUNET_assert (GNUNET_YES ==
1248 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1254 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1256 GNUNET_assert (GNUNET_YES ==
1257 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1261 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1269 * Send the message to all clients that have subscribed to its type
1271 * @param msg Pointer to the message itself
1272 * @param payload Pointer to the payload of the message.
1273 * @param t The tunnel to whose clients this message goes.
1275 * @return number of clients this message was sent to
1278 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1279 const struct GNUNET_MessageHeader *payload,
1280 struct MeshTunnel *t)
1282 struct MeshClient *c;
1283 MESH_TunnelNumber *tid;
1286 char cbuf[htons (msg->size)];
1288 type = ntohs (payload->type);
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1291 GNUNET_MESH_DEBUG_M2S (type));
1293 memcpy (cbuf, msg, sizeof (cbuf));
1294 switch (htons (msg->type))
1296 struct GNUNET_MESH_Unicast *uc;
1297 struct GNUNET_MESH_ToOrigin *to;
1299 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1300 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1303 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1304 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1312 for (count = 0, c = clients; c != NULL; c = c->next)
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1315 if (client_is_subscribed (type, c))
1317 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1321 *tid = htonl (t->local_tid);
1325 if (GNUNET_NO == client_knows_tunnel (c, t))
1327 /* This client doesn't know the tunnel */
1328 struct GNUNET_MESH_TunnelNotification tmsg;
1329 struct GNUNET_HashCode hash;
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1332 tmsg.header.size = htons (sizeof (tmsg));
1333 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1334 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1335 tmsg.tunnel_id = htonl (t->local_tid_dest);
1337 if (GNUNET_YES == t->nobuffer)
1338 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1339 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1340 &tmsg.header, GNUNET_NO);
1341 tunnel_add_client (t, c);
1342 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1344 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1345 c->incoming_tunnels, &hash, t,
1346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1348 *tid = htonl (t->local_tid_dest);
1351 /* Check if the client wants to get traffic from the tunnel */
1352 if (GNUNET_NO == client_wants_tunnel(c, t))
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1356 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1357 (struct GNUNET_MessageHeader
1358 *) cbuf, GNUNET_NO);
1367 * Notify all clients (not depending on registration status) that the incoming
1368 * tunnel is no longer valid.
1370 * @param t Tunnel that was destroyed.
1373 send_clients_tunnel_destroy (struct MeshTunnel *t)
1375 struct GNUNET_MESH_TunnelMessage msg;
1377 msg.header.size = htons (sizeof (msg));
1378 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1379 msg.tunnel_id = htonl (t->local_tid_dest);
1380 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1385 * Notify clients of tunnel disconnections, if needed.
1386 * In case the origin disconnects, the destination clients get a tunnel destroy
1387 * notification. If the last destination disconnects (only one remaining client
1388 * in tunnel), the origin gets a (local ID) peer disconnected.
1389 * Note that the function must be called BEFORE removing the client from
1392 * @param t Tunnel that was destroyed.
1393 * @param c Client that disconnected.
1396 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1402 struct GNUNET_MESH_TunnelMessage msg;
1404 msg.header.size = htons (sizeof (msg));
1405 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1406 msg.tunnel_id = htonl (t->local_tid_dest);
1407 for (i = 0; i < t->nclients; i++)
1408 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1409 &msg.header, GNUNET_NO);
1411 // FIXME when to disconnect an incoming tunnel?
1416 * Iterator over all the peers to remove the oldest not-used entry.
1418 * @param cls Closure (unsued).
1419 * @param key ID of the peer.
1420 * @param value Peer_Info of the peer.
1425 peer_info_timeout (void *cls,
1426 const struct GNUNET_HashCode *key,
1433 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1434 * and insert it in the appropiate structures if the peer is not known yet.
1436 * @param peer Full identity of the peer.
1438 * @return Existing or newly created peer info.
1440 static struct MeshPeerInfo *
1441 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1443 struct MeshPeerInfo *peer_info;
1445 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1446 if (NULL == peer_info)
1449 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1450 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1452 GNUNET_CONTAINER_multihashmap_iterate (peers,
1456 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1458 peer_info->id = GNUNET_PEER_intern (peer);
1460 peer_info->last_contact = GNUNET_TIME_absolute_get();
1467 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1468 * and insert it in the appropiate structures if the peer is not known yet.
1470 * @param peer Short identity of the peer.
1472 * @return Existing or newly created peer info.
1474 static struct MeshPeerInfo *
1475 peer_info_get_short (const GNUNET_PEER_Id peer)
1477 struct GNUNET_PeerIdentity id;
1479 GNUNET_PEER_resolve (peer, &id);
1480 return peer_info_get (&id);
1485 * Iterator to remove the tunnel from the list of tunnels a peer participates
1488 * @param cls Closure (tunnel info)
1489 * @param key GNUNET_PeerIdentity of the peer (unused)
1490 * @param value PeerInfo of the peer
1492 * @return always GNUNET_YES, to keep iterating
1495 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1497 struct MeshTunnel *t = cls;
1498 struct MeshPeerInfo *peer = value;
1501 for (i = 0; i < peer->ntunnels; i++)
1504 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1507 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1509 GNUNET_realloc (peer->tunnels,
1510 peer->ntunnels * sizeof(struct MeshTunnel *));
1519 * Core callback to write a pre-constructed data packet to core buffer
1521 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1522 * @param size Number of bytes available in buf.
1523 * @param buf Where the to write the message.
1525 * @return number of bytes written to buf
1528 send_core_data_raw (void *cls, size_t size, void *buf)
1530 struct MeshTransmissionDescriptor *info = cls;
1531 struct GNUNET_MessageHeader *msg;
1534 GNUNET_assert (NULL != info);
1535 GNUNET_assert (NULL != info->mesh_data);
1536 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1537 total_size = ntohs (msg->size);
1539 if (total_size > size)
1544 memcpy (buf, msg, total_size);
1545 data_descriptor_decrement_rc (info->mesh_data);
1552 * Sends an already built non-multicast message to a peer,
1553 * properly registrating all used resources.
1555 * @param message Message to send. Function makes a copy of it.
1556 * @param peer Short ID of the neighbor whom to send the message.
1557 * @param t Tunnel on which this message is transmitted.
1560 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1561 const struct GNUNET_PeerIdentity *peer,
1562 struct MeshTunnel *t)
1564 struct MeshTransmissionDescriptor *info;
1565 struct MeshPeerInfo *neighbor;
1566 struct MeshPeerPath *p;
1570 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1572 size = ntohs (message->size);
1573 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1574 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1575 info->mesh_data->data = GNUNET_malloc (size);
1576 memcpy (info->mesh_data->data, message, size);
1577 type = ntohs(message->type);
1580 struct GNUNET_MESH_Unicast *m;
1581 struct GNUNET_MESH_ToOrigin *to;
1583 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1584 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1585 m->ttl = htonl (ntohl (m->ttl) - 1);
1587 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1588 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1590 to->pid = htonl(t->bck_pid);
1592 info->mesh_data->data_len = size;
1593 info->mesh_data->reference_counter = 1;
1594 info->mesh_data->total_out = 1;
1595 neighbor = peer_info_get (peer);
1596 for (p = neighbor->path_head; NULL != p; p = p->next)
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 " %s IS NOT DIRECTLY CONNECTED\n",
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 for (p = neighbor->path_head; NULL != p; p = p->next)
1614 struct GNUNET_PeerIdentity debug_id;
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 " path with %u hops through:\n",
1620 for (i = 0; i < p->length; i++)
1622 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 i, GNUNET_i2s(&debug_id));
1629 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1631 " no direct connection to %s\n",
1633 GNUNET_free (info->mesh_data->data);
1634 GNUNET_free (info->mesh_data);
1638 info->peer = neighbor;
1639 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1650 * Sends a CREATE PATH message for a path to a peer, properly registrating
1651 * all used resources.
1653 * @param peer PeerInfo of the final peer for whom this path is being created.
1654 * @param p Path itself.
1655 * @param t Tunnel for which the path is created.
1658 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1659 struct MeshTunnel *t)
1661 struct GNUNET_PeerIdentity id;
1662 struct MeshPathInfo *path_info;
1663 struct MeshPeerInfo *neighbor;
1669 p = tree_get_path_to_peer (t->tree, peer->id);
1676 for (i = 0; i < p->length; i++)
1678 if (p->peers[i] == myid)
1681 if (i >= p->length - 1)
1687 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1689 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1690 path_info->path = p;
1692 neighbor = peer_info_get (&id);
1693 path_info->peer = neighbor;
1694 queue_add (path_info,
1695 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1696 sizeof (struct GNUNET_MESH_ManipulatePath) +
1697 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1704 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1706 * @param t Tunnel whose path to destroy.
1707 * @param destination Short ID of the peer to whom the path to destroy.
1710 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1712 struct MeshPeerPath *p;
1715 p = tree_get_path_to_peer (t->tree, destination);
1721 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1722 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1724 struct GNUNET_MESH_ManipulatePath *msg;
1725 struct GNUNET_PeerIdentity *pi;
1729 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1730 msg->header.size = htons (size);
1731 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1732 msg->tid = htonl (t->id.tid);
1733 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1734 for (i = 0; i < p->length; i++)
1736 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1738 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1745 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1747 * @param t Tunnel which to confirm.
1750 send_path_ack (struct MeshTunnel *t)
1752 struct MeshTransmissionDescriptor *info;
1753 struct GNUNET_PeerIdentity id;
1754 GNUNET_PEER_Id peer;
1756 peer = tree_get_predecessor (t->tree);
1757 GNUNET_PEER_resolve (peer, &id);
1758 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1759 info->origin = &t->id;
1760 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1761 GNUNET_assert (NULL != info->peer);
1764 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1765 sizeof (struct GNUNET_MESH_PathACK),
1772 * Try to establish a new connection to this peer.
1773 * Use the best path for the given tunnel.
1774 * If the peer doesn't have any path to it yet, try to get one.
1775 * If the peer already has some path, send a CREATE PATH towards it.
1777 * @param peer PeerInfo of the peer.
1778 * @param t Tunnel for which to create the path, if possible.
1781 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1783 struct MeshPeerPath *p;
1784 struct MeshPathInfo *path_info;
1786 if (NULL != peer->path_head)
1788 p = tree_get_path_to_peer (t->tree, peer->id);
1795 // FIXME always send create path to self
1798 send_create_path (peer, p, t);
1802 struct GNUNET_HashCode hash;
1805 t->local_tid_dest = next_local_tid++;
1806 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1809 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1810 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1817 else if (NULL == peer->dhtget)
1819 struct GNUNET_PeerIdentity id;
1821 GNUNET_PEER_resolve (peer->id, &id);
1822 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1823 path_info->peer = peer;
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1827 peer->dhtgetcls = path_info;
1828 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1829 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1830 &id.hashPubKey, /* key to search */
1831 dht_replication_level, /* replication level */
1832 GNUNET_DHT_RO_RECORD_ROUTE |
1833 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1834 NULL, /* xquery */ // FIXME BLOOMFILTER
1835 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1836 &dht_get_id_handler, path_info);
1838 /* Otherwise, there is no path but the DHT get is already started. */
1843 * Task to delay the connection of a peer
1845 * @param cls Closure (path info with tunnel and peer to connect).
1846 * Will be free'd on exection.
1847 * @param tc TaskContext
1850 peer_info_connect_task (void *cls,
1851 const struct GNUNET_SCHEDULER_TaskContext *tc)
1853 struct MeshPathInfo *path_info = cls;
1855 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
1857 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1862 peer_info_connect (path_info->peer, path_info->t);
1868 * Destroy the peer_info and free any allocated resources linked to it
1870 * @param pi The peer_info to destroy.
1872 * @return GNUNET_OK on success
1875 peer_info_destroy (struct MeshPeerInfo *pi)
1877 struct GNUNET_PeerIdentity id;
1878 struct MeshPeerPath *p;
1879 struct MeshPeerPath *nextp;
1881 GNUNET_PEER_resolve (pi->id, &id);
1882 GNUNET_PEER_change_rc (pi->id, -1);
1885 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1888 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1889 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1891 if (NULL != pi->dhtget)
1893 GNUNET_DHT_get_stop (pi->dhtget);
1894 GNUNET_free (pi->dhtgetcls);
1900 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1904 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
1906 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
1914 * Remove all paths that rely on a direct connection between p1 and p2
1915 * from the peer itself and notify all tunnels about it.
1917 * @param peer PeerInfo of affected peer.
1918 * @param p1 GNUNET_PEER_Id of one peer.
1919 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1922 * TODO: optimize (see below)
1925 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1928 struct MeshPeerPath *p;
1929 struct MeshPeerPath *aux;
1930 struct MeshPeerInfo *peer_d;
1932 unsigned int destroyed;
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1939 p = peer->path_head;
1943 for (i = 0; i < (p->length - 1); i++)
1945 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1946 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1948 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1959 for (i = 0; i < peer->ntunnels; i++)
1961 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1965 * Problem: one or more peers have been deleted from the tunnel tree.
1966 * We don't know who they are to try to add them again.
1967 * We need to try to find a new path for each of the disconnected peers.
1968 * Some of them might already have a path to reach them that does not
1969 * involve p1 and p2. Adding all anew might render in a better tree than
1970 * the trivial immediate fix.
1972 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1973 * its children will be reachable trough him.
1975 peer_d = peer_info_get_short (d);
1978 for (p = peer_d->path_head; NULL != p; p = p->next)
1980 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1988 /* No callback, as peer will be already disconnected and a connection
1989 * scheduled by tunnel_notify_connection_broken.
1991 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1995 peer_info_connect (peer_d, peer->tunnels[i]);
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2003 * Add the path to the peer and update the path used to reach it in case this
2006 * @param peer_info Destination peer to add the path to.
2007 * @param path New path to add. Last peer must be the peer in arg 1.
2008 * Path will be either used of freed if already known.
2009 * @param trusted Do we trust that this path is real?
2012 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2015 struct MeshPeerPath *aux;
2019 if ((NULL == peer_info) || (NULL == path))
2022 path_destroy (path);
2025 if (path->peers[path->length - 1] != peer_info->id)
2028 path_destroy (path);
2031 if (path->length <= 2 && GNUNET_NO == trusted)
2033 /* Only allow CORE to tell us about direct paths */
2034 path_destroy (path);
2037 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2038 for (l = 1; l < path->length; l++)
2040 if (path->peers[l] == myid)
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2043 for (l2 = 0; l2 < path->length - l; l2++)
2045 path->peers[l2] = path->peers[l + l2];
2050 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2055 struct GNUNET_PeerIdentity id;
2057 GNUNET_PEER_resolve (peer_info->id, &id);
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2059 path->length, GNUNET_i2s (&id));
2062 l = path_get_length (path);
2069 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2070 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2072 l2 = path_get_length (aux);
2075 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2076 peer_info->path_tail, aux, path);
2081 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2083 path_destroy (path);
2088 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2095 * Add the path to the origin peer and update the path used to reach it in case
2096 * this is the shortest.
2097 * The path is given in peer_info -> destination, therefore we turn the path
2098 * upside down first.
2100 * @param peer_info Peer to add the path to, being the origin of the path.
2101 * @param path New path to add after being inversed.
2102 * @param trusted Do we trust that this path is real?
2105 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2106 struct MeshPeerPath *path, int trusted)
2109 peer_info_add_path (peer_info, path, trusted);
2114 * Function called if the connection to the peer has been stalled for a while,
2115 * possibly due to a missed ACK. Poll the peer about its ACK status.
2117 * @param cls Closure (cinfo).
2118 * @param tc TaskContext.
2121 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2123 // struct GNUNET_MESH_Poll msg;
2124 // struct GNUNET_PeerIdentity id;
2125 // struct MeshTunnel *t;
2127 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2128 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2134 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2135 // msg.header.size = htons (sizeof (msg));
2136 // msg.tid = htonl (t->id.tid);
2137 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2138 // msg.last_ack = htonl (cinfo->fwd_ack);
2140 // GNUNET_PEER_resolve (cinfo->id, &id);
2141 // send_prebuilt_message (&msg.header, &id, cinfo->t);
2142 // cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2143 // MESH_MAX_POLL_TIME,
2144 // GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2145 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2146 // &tunnel_poll, cinfo);
2151 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2152 * to obtain a local peer -> destination path and interning the peer ids.
2154 * @return Newly allocated and created path
2156 static struct MeshPeerPath *
2157 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2158 unsigned int get_path_length,
2159 const struct GNUNET_PeerIdentity *put_path,
2160 unsigned int put_path_length)
2162 struct MeshPeerPath *p;
2168 GNUNET_PEER_change_rc (myid, 1);
2169 i = get_path_length;
2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2171 for (i--; i >= 0; i--)
2173 id = GNUNET_PEER_intern (&get_path[i]);
2174 if (p->length > 0 && id == p->peers[p->length - 1])
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2177 GNUNET_PEER_change_rc (id, -1);
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2182 GNUNET_i2s (&get_path[i]));
2184 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2185 p->peers[p->length - 1] = id;
2188 i = put_path_length;
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2190 for (i--; i >= 0; i--)
2192 id = GNUNET_PEER_intern (&put_path[i]);
2195 /* PUT path went through us, so discard the path up until now and start
2196 * from here to get a much shorter (and loop-free) path.
2201 if (p->length > 0 && id == p->peers[p->length - 1])
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2204 GNUNET_PEER_change_rc (id, -1);
2208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2209 GNUNET_i2s (&put_path[i]));
2211 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2212 p->peers[p->length - 1] = id;
2216 if (get_path_length > 0)
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2218 GNUNET_i2s (&get_path[0]));
2219 if (put_path_length > 0)
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2221 GNUNET_i2s (&put_path[0]));
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2224 for (i = 0; i < p->length; i++)
2226 struct GNUNET_PeerIdentity peer_id;
2228 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2230 GNUNET_i2s (&peer_id));
2238 * Adds a path to the peer_infos of all the peers in the path
2240 * @param p Path to process.
2241 * @param confirmed Whether we know if the path works or not.
2244 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2248 /* TODO: invert and add */
2249 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2250 for (i++; i < p->length; i++)
2252 struct MeshPeerInfo *aux;
2253 struct MeshPeerPath *copy;
2255 aux = peer_info_get_short (p->peers[i]);
2256 copy = path_duplicate (p);
2257 copy->length = i + 1;
2258 peer_info_add_path (aux, copy, GNUNET_NO);
2264 * Send keepalive packets for a peer
2266 * @param cls Closure (tunnel for which to send the keepalive).
2267 * @param tc Notification context.
2269 * TODO: implement explicit multicast keepalive?
2272 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2276 * Search for a tunnel among the incoming tunnels
2278 * @param tid the local id of the tunnel
2280 * @return tunnel handler, NULL if doesn't exist
2282 static struct MeshTunnel *
2283 tunnel_get_incoming (MESH_TunnelNumber tid)
2285 struct GNUNET_HashCode hash;
2287 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2288 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2289 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2294 * Search for a tunnel among the tunnels for a client
2296 * @param c the client whose tunnels to search in
2297 * @param tid the local id of the tunnel
2299 * @return tunnel handler, NULL if doesn't exist
2301 static struct MeshTunnel *
2302 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2304 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2306 return tunnel_get_incoming (tid);
2310 struct GNUNET_HashCode hash;
2312 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2313 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2319 * Search for a tunnel by global ID using PEER_ID
2321 * @param pi owner of the tunnel
2322 * @param tid global tunnel number
2324 * @return tunnel handler, NULL if doesn't exist
2326 static struct MeshTunnel *
2327 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2329 struct MESH_TunnelID id;
2330 struct GNUNET_HashCode hash;
2335 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2336 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2341 * Search for a tunnel by global ID using full PeerIdentities
2343 * @param oid owner of the tunnel
2344 * @param tid global tunnel number
2346 * @return tunnel handler, NULL if doesn't exist
2348 static struct MeshTunnel *
2349 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2351 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2356 * Delete an active client from the tunnel.
2362 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2366 for (i = 0; i < t->nclients; i++)
2368 if (t->clients[i] == c)
2370 t->clients[i] = t->clients[t->nclients - 1];
2371 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2372 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2374 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2382 * Delete an ignored client from the tunnel.
2388 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2392 for (i = 0; i < t->nignore; i++)
2394 if (t->ignore[i] == c)
2396 t->ignore[i] = t->ignore[t->nignore - 1];
2397 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2405 * Delete a client from the tunnel. It should be only done on
2406 * client disconnection, otherwise use client_ignore_tunnel.
2412 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2414 tunnel_delete_ignored_client (t, c);
2415 tunnel_delete_active_client (t, c);
2420 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2421 * needed rescources.
2422 * If peer already exists, reevaluate shortest path and change if different.
2424 * @param t Tunnel we want to add a new peer to
2425 * @param peer PeerInfo of the peer being added
2429 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2431 struct MeshPeerPath *best_p;
2432 struct MeshPeerPath *p;
2433 unsigned int best_cost;
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2438 if (t->peer != peer->id)
2440 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2444 if (NULL != (p = peer->path_head))
2447 best_cost = tree_get_path_cost (t->tree, p);
2450 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2457 tree_add_path (t->tree, best_p, NULL, NULL); // FIXME
2458 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2459 t->path_refresh_task =
2460 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2464 /* Start a DHT get */
2465 peer_info_connect (peer, t);
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2471 * Add a path to a tunnel which we don't own, just to remember the next hop.
2472 * If destination node was already in the tunnel, the first hop information
2473 * will be replaced with the new path.
2475 * @param t Tunnel we want to add a new peer to
2476 * @param p Path to add
2477 * @param own_pos Position of local node in path.
2481 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2482 unsigned int own_pos)
2484 struct GNUNET_PeerIdentity id;
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2487 GNUNET_assert (0 != own_pos);
2488 tree_add_path (t->tree, p, NULL, NULL);
2489 if (own_pos < p->length - 1)
2491 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2492 tree_update_first_hops (t->tree, myid, &id);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2498 * Add a client to a tunnel, initializing all needed data structures.
2500 * @param t Tunnel to which add the client.
2501 * @param c Client which to add to the tunnel.
2504 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2506 struct MeshTunnelClientInfo clinfo;
2508 GNUNET_array_append (t->clients, t->nclients, c);
2509 clinfo.fwd_ack = t->fwd_pid + 1;
2510 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2511 clinfo.fwd_pid = t->fwd_pid;
2512 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2514 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2519 * Notifies a tunnel that a connection has broken that affects at least
2520 * some of its peers. Sends a notification towards the root of the tree.
2521 * In case the peer is the owner of the tree, notifies the client that owns
2522 * the tunnel and tries to reconnect.
2524 * @param t Tunnel affected.
2525 * @param p1 Peer that got disconnected from p2.
2526 * @param p2 Peer that got disconnected from p1.
2528 * @return Short ID of the peer disconnected (either p1 or p2).
2529 * 0 if the tunnel remained unaffected.
2531 static GNUNET_PEER_Id
2532 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2535 // if (myid != p1 && myid != p2) FIXME
2540 // if (tree_get_predecessor (t->tree) != 0)
2542 // /* We are the peer still connected, notify owner of the disconnection. */
2543 // struct GNUNET_MESH_PathBroken msg;
2544 // struct GNUNET_PeerIdentity neighbor;
2546 // msg.header.size = htons (sizeof (msg));
2547 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2548 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2549 // msg.tid = htonl (t->id.tid);
2550 // msg.peer1 = my_full_id;
2551 // GNUNET_PEER_resolve (pid, &msg.peer2);
2552 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2553 // send_prebuilt_message (&msg.header, &neighbor, t);
2560 * Get the Flow Control info of a client.
2562 * @param t Tunnel on which to look.
2563 * @param c Client whose ACK to get.
2565 * @return ACK value.
2567 static struct MeshTunnelClientInfo *
2568 tunnel_get_client_fc (struct MeshTunnel *t,
2569 struct MeshClient *c)
2573 for (i = 0; i < t->nclients; i++)
2575 if (t->clients[i] != c)
2577 return &t->clients_fc[i];
2580 return NULL; // avoid compiler / coverity complaints
2585 * Set the FWD ACK value of a client in a particular tunnel.
2587 * @param t Tunnel affected.
2588 * @param c Client whose ACK to set.
2589 * @param ack ACK value.
2592 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2593 struct MeshClient *c,
2598 for (i = 0; i < t->nclients; i++)
2600 if (t->clients[i] != c)
2602 t->clients_fc[i].fwd_ack = ack;
2610 * Get the highest ACK value of all clients in a particular tunnel,
2611 * according to the buffering/speed settings.
2613 * @param t Tunnel on which to look.
2615 * @return Corresponding ACK value (max uint32_t).
2616 * If no clients are suscribed, -1LL.
2619 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2624 if (0 == t->nclients)
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 " tunnel has no clients, no FWD ACK\n");
2631 for (ack = -1LL, i = 0; i < t->nclients; i++)
2634 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2636 ack = t->clients_fc[i].fwd_ack;
2640 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2641 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
2643 return (uint32_t) ack;
2648 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2649 * mode and the status of all children nodes.
2653 * @return Maximum PID allowed.
2656 tunnel_get_fwd_ack (struct MeshTunnel *t)
2660 uint32_t buffer_free;
2664 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
2665 client_ack = tunnel_get_clients_fwd_ack (t);
2666 if (GNUNET_YES == t->nobuffer)
2672 ack = count + buffer_free; // Overflow? OK!
2674 if (-1LL == client_ack)
2679 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 "c %u, bf %u, cl %lld, ACK: %u\n",
2683 count, buffer_free, client_ack, ack);
2689 * Build a local ACK message and send it to a local client.
2691 * @param t Tunnel on which to send the ACK.
2692 * @param c Client to whom send the ACK.
2693 * @param ack Value of the ACK.
2696 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2698 struct GNUNET_MESH_LocalAck msg;
2700 msg.header.size = htons (sizeof (msg));
2701 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2702 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2703 msg.max_pid = htonl (ack);
2704 GNUNET_SERVER_notification_context_unicast(nc,
2711 * Build an ACK message and queue it to send to the given peer.
2713 * @param t Tunnel on which to send the ACK.
2714 * @param peer Peer to whom send the ACK.
2715 * @param ack Value of the ACK.
2718 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
2720 struct GNUNET_MESH_ACK msg;
2722 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2723 msg.header.size = htons (sizeof (msg));
2724 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2725 msg.pid = htonl (ack);
2726 msg.tid = htonl (t->id.tid);
2728 send_prebuilt_message (&msg.header, peer, t);
2733 * Notify a the owner of a tunnel about how many more
2734 * payload packages will we accept on a given tunnel.
2736 * @param t Tunnel on which to send the ACK.
2739 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2744 "Sending client FWD ACK on tunnel %X\n",
2747 ack = tunnel_get_fwd_ack (t);
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2750 if (t->last_fwd_ack == ack)
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2757 t->last_fwd_ack = ack;
2758 send_local_ack (t, t->owner, ack);
2763 * Send an ACK informing the predecessor about the available buffer space.
2764 * In case there is no predecessor, inform the owning client.
2765 * If buffering is off, send only on behalf of children or self if endpoint.
2766 * If buffering is on, send when sent to children and buffer space is free.
2767 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2768 * the ACK itself goes "back" (towards root).
2770 * @param t Tunnel on which to send the ACK.
2771 * @param type Type of message that triggered the ACK transmission.
2774 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2776 struct GNUNET_PeerIdentity id;
2779 if (NULL != t->owner)
2781 tunnel_send_client_fwd_ack (t);
2784 /* Is it after unicast / multicast retransmission? */
2787 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2788 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2790 "ACK due to FWD DATA retransmission\n");
2791 if (GNUNET_YES == t->nobuffer)
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2797 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2798 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2800 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2801 t->force_ack = GNUNET_YES;
2807 /* Check if we need to transmit the ACK */
2808 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
2809 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
2810 GNUNET_NO == t->force_ack)
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 " t->qmax: %u, t->qn: %u\n",
2815 t->fwd_queue_max, t->fwd_queue_n);
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2817 " t->pid: %u, t->ack: %u\n",
2818 t->fwd_pid, t->last_fwd_ack);
2822 /* Ok, ACK might be necessary, what PID to ACK? */
2823 ack = tunnel_get_fwd_ack (t);
2825 /* If speed_min and not all children have ack'd, dont send yet */
2826 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2832 t->last_fwd_ack = ack;
2833 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
2834 send_ack (t, &id, ack);
2836 t->force_ack = GNUNET_NO;
2841 * Iterator to send a child node a BCK ACK to allow him to send more
2844 * @param cls Closure (tunnel).
2845 * @param id Id of the child node.
2848 tunnel_send_child_bck_ack (void *cls,
2851 struct MeshTunnel *t = cls;
2852 struct GNUNET_PeerIdentity peer;
2853 uint32_t ack = 0; // FIXME
2855 GNUNET_PEER_resolve (id, &peer);
2856 // ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
2858 // if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
2860 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2861 // " Not sending ACK, not needed\n");
2864 // cinfo->bck_ack = ack;
2866 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867 // " Sending BCK ACK %u (last sent: %u)\n",
2868 // ack, cinfo->bck_ack);
2869 send_ack (t, &peer, ack);
2874 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2876 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2878 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2879 * - implement a size 1 buffer in each client_fc AND children_fc
2880 * to hold at least 1 message per "child".
2881 * problem: violates no buffer policy
2882 * - ack 0 and make "children" poll for transmission slots
2883 * problem: big overhead, extra latency even in low traffic
2886 * @param t Tunnel on which to send the BCK ACKs.
2889 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2892 unsigned int tunnel_delta;
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2896 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2899 /* Find client whom to allow to send to origin (with lowest buffer space) */
2900 for (i = 0; i < t->nclients; i++)
2902 struct MeshTunnelClientInfo *clinfo;
2905 clinfo = &t->clients_fc[i];
2906 delta = clinfo->bck_ack - clinfo->bck_pid;
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2908 t->clients[i]->id, delta);
2910 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2911 (GNUNET_YES == t->nobuffer && 0 == delta))
2915 ack = clinfo->bck_pid;
2916 ack += t->nobuffer ? 1 : tunnel_delta;
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2918 " sending ack to client %u: %u\n",
2919 t->clients[i]->id, ack);
2920 send_local_ack (t, t->clients[i], ack);
2921 clinfo->bck_ack = ack;
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2926 " not sending ack to client %u (td %u, d %u)\n",
2927 t->clients[i]->id, tunnel_delta, delta);
2934 * Send an ACK informing the children nodes and destination clients about
2935 * the available buffer space.
2936 * If buffering is off, send only on behalf of root (can be self).
2937 * If buffering is on, send when sent to predecessor and buffer space is free.
2938 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2939 * the ACK itself goes "forward" (towards children/clients).
2941 * @param t Tunnel on which to send the ACK.
2942 * @param type Type of message that triggered the ACK transmission.
2945 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2949 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2950 /* Is it after data to_origin retransmission? */
2953 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2954 if (GNUNET_YES == t->nobuffer)
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2957 " Not sending ACK, nobuffer\n");
2961 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2962 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2964 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2965 t->force_ack = GNUNET_YES;
2971 tunnel_send_clients_bck_ack (t);
2972 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
2973 t->force_ack = GNUNET_NO;
2978 * @brief Re-initiate traffic to this peer if necessary.
2980 * Check if there is traffic queued towards this peer
2981 * and the core transmit handle is NULL (traffic was stalled).
2982 * If so, call core tmt rdy.
2984 * @param cls Closure (unused)
2985 * @param peer_id Short ID of peer to which initiate traffic.
2988 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
2990 struct MeshPeerInfo *peer;
2991 struct GNUNET_PeerIdentity id;
2992 struct MeshPeerQueue *q;
2995 peer = peer_info_get_short(peer_id);
2996 if (NULL != peer->core_transmit)
2999 q = queue_get_next(peer);
3002 /* Might br multicast traffic already sent to this particular peer but
3003 * not to other children in this tunnel.
3004 * This way t->queue_n would be > 0 but the queue of this particular peer
3010 GNUNET_PEER_resolve (peer->id, &id);
3011 peer->core_transmit =
3012 GNUNET_CORE_notify_transmit_ready(core_handle,
3015 GNUNET_TIME_UNIT_FOREVER_REL,
3025 * @brief Allow transmission of FWD traffic on this tunnel
3027 * Check if there is traffic queued towards any children
3028 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3030 * @param t Tunnel on which to unlock FWD traffic.
3033 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3035 if (0 == t->fwd_queue_n)
3038 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3043 * @brief Allow transmission of BCK traffic on this tunnel
3045 * Check if there is traffic queued towards the root of the tree
3046 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3048 * @param t Tunnel on which to unlock BCK traffic.
3051 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3053 if (0 == t->bck_queue_n)
3056 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3061 * Send a message to all peers in this tunnel that the tunnel is no longer
3064 * @param t The tunnel whose peers to notify.
3065 * @param parent ID of the parent, in case the tree is already destroyed.
3068 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3070 struct GNUNET_MESH_TunnelDestroy msg;
3071 struct GNUNET_PeerIdentity id;
3073 msg.header.size = htons (sizeof (msg));
3074 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3075 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3076 msg.tid = htonl (t->id.tid);
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3078 " sending tunnel destroy for tunnel: %s [%X]\n",
3079 GNUNET_i2s (&msg.oid), t->id.tid);
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to child\n");
3082 /* FIXME tunnel_send_multicast (t, &msg.header); */
3085 parent = tree_get_predecessor (t->tree);
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3090 GNUNET_PEER_resolve (parent, &id);
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 " sending back to %s\n",
3094 send_prebuilt_message (&msg.header, &id, t);
3099 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3101 * @param cls Closure (Tunnel which to cancel).
3102 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3105 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3107 struct MeshTunnel *t = cls;
3108 struct MeshPeerInfo *peer_info;
3109 struct MeshPeerQueue *pq;
3110 struct MeshPeerQueue *next;
3112 peer_info = peer_info_get_short (neighbor_id);
3113 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3116 if (pq->tunnel == t)
3118 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3119 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3120 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3122 // Should have been removed on destroy children
3125 queue_destroy (pq, GNUNET_YES);
3128 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3130 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3131 peer_info->core_transmit = NULL;
3136 * Destroy the tunnel and free any allocated resources linked to it.
3138 * @param t the tunnel to destroy
3140 * @return GNUNET_OK on success
3143 tunnel_destroy (struct MeshTunnel *t)
3145 struct MeshClient *c;
3146 struct GNUNET_HashCode hash;
3157 struct GNUNET_PeerIdentity id;
3159 GNUNET_PEER_resolve (t->id.oid, &id);
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3161 GNUNET_i2s (&id), t->id.tid);
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3167 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3168 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3176 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3178 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3185 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3186 for (i = 0; i < t->nclients; i++)
3190 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3196 for (i = 0; i < t->nignore; i++)
3200 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3207 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3208 GNUNET_free_non_null (t->clients);
3209 GNUNET_free_non_null (t->ignore);
3210 GNUNET_free_non_null (t->clients_fc);
3212 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3213 tree_destroy (t->tree);
3215 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3216 GNUNET_SCHEDULER_cancel (t->timeout_task);
3217 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3218 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3221 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3226 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3229 * Tunnel is empty: destroy it.
3231 * @param cls Closure (Tunnel).
3232 * @param tc TaskContext.
3235 tunnel_destroy_empty_delayed (void *cls,
3236 const struct GNUNET_SCHEDULER_TaskContext *tc)
3238 struct MeshTunnel *t = cls;
3240 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3241 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3244 if (0 != t->nclients ||
3245 0 != tree_count_children (t->tree))
3250 struct GNUNET_PeerIdentity id;
3252 GNUNET_PEER_resolve (t->id.oid, &id);
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 "executing destruction of empty tunnel %s [%X]\n",
3255 GNUNET_i2s (&id), t->id.tid);
3259 tunnel_send_destroy (t, 0);
3260 if (0 == t->pending_messages)
3263 t->destroy = GNUNET_YES;
3268 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3270 * @param t Tunnel to destroy if empty.
3273 tunnel_destroy_empty (struct MeshTunnel *t)
3275 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3277 0 != tree_count_children (t->tree))
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
3287 struct GNUNET_PeerIdentity id;
3289 GNUNET_PEER_resolve (t->id.oid, &id);
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3291 "scheduling destruction of empty tunnel %s [%X]\n",
3292 GNUNET_i2s (&id), t->id.tid);
3296 t->delayed_destroy =
3297 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3298 &tunnel_destroy_empty_delayed,
3304 * Create a new tunnel
3306 * @param owner Who is the owner of the tunnel (short ID).
3307 * @param tid Tunnel Number of the tunnel.
3308 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3309 * @param local Tunnel Number for the tunnel, for the client point of view.
3311 * @return A new initialized tunnel. NULL on error.
3313 static struct MeshTunnel *
3314 tunnel_new (GNUNET_PEER_Id owner,
3315 MESH_TunnelNumber tid,
3316 struct MeshClient *client,
3317 MESH_TunnelNumber local)
3319 struct MeshTunnel *t;
3320 struct GNUNET_HashCode hash;
3322 if (n_tunnels >= max_tunnels && NULL == client)
3325 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3328 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3329 t->bck_queue_max = t->fwd_queue_max;
3330 t->tree = tree_new (owner);
3332 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3333 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3334 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3335 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3336 t->local_tid = local;
3338 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3340 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3342 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3343 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3350 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3357 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3359 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3360 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3364 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3375 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3376 * client when the client disconnects. If the client is not the owner, the
3377 * owner will get notified if no more clients are in the tunnel and the client
3378 * get removed from the tunnel's list.
3380 * @param cls closure (client that is disconnecting)
3381 * @param key the hash of the local tunnel id (used to access the hashmap)
3382 * @param value the value stored at the key (tunnel to destroy)
3384 * @return GNUNET_OK, keep iterating.
3387 tunnel_destroy_iterator (void *cls,
3388 const struct GNUNET_HashCode * key,
3391 struct MeshTunnel *t = value;
3392 struct MeshClient *c = cls;
3394 send_client_tunnel_disconnect (t, c);
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3398 tunnel_delete_client (t, c);
3399 client_delete_tunnel (c, t);
3400 tunnel_destroy_empty (t);
3403 tunnel_send_destroy (t, 0);
3405 t->destroy = GNUNET_YES;
3412 * Timeout function, destroys tunnel if called
3414 * @param cls Closure (tunnel to destroy).
3415 * @param tc TaskContext
3418 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3420 struct MeshTunnel *t = cls;
3421 struct GNUNET_PeerIdentity id;
3423 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3424 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3426 GNUNET_PEER_resolve(t->id.oid, &id);
3427 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3428 "Tunnel %s [%X] timed out. Destroying.\n",
3429 GNUNET_i2s(&id), t->id.tid);
3430 send_clients_tunnel_destroy (t);
3435 * Resets the tunnel timeout. Starts it if no timeout was running.
3437 * @param t Tunnel whose timeout to reset.
3439 * TODO use heap to improve efficiency of scheduler.
3442 tunnel_reset_timeout (struct MeshTunnel *t)
3444 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3445 GNUNET_SCHEDULER_cancel (t->timeout_task);
3447 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3448 (refresh_path_time, 4), &tunnel_timeout, t);
3452 /******************************************************************************/
3453 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3454 /******************************************************************************/
3457 * Function to send a create path packet to a peer.
3459 * @param cls closure
3460 * @param size number of bytes available in buf
3461 * @param buf where the callee should write the message
3462 * @return number of bytes written to buf
3465 send_core_path_create (void *cls, size_t size, void *buf)
3467 struct MeshPathInfo *info = cls;
3468 struct GNUNET_MESH_ManipulatePath *msg;
3469 struct GNUNET_PeerIdentity *peer_ptr;
3470 struct MeshTunnel *t = info->t;
3471 struct MeshPeerPath *p = info->path;
3476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3478 sizeof (struct GNUNET_MESH_ManipulatePath) +
3479 p->length * sizeof (struct GNUNET_PeerIdentity);
3481 if (size < size_needed || NULL == buf)
3486 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3487 msg->header.size = htons (size_needed);
3488 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3489 msg->tid = ntohl (t->id.tid);
3492 if (GNUNET_YES == t->nobuffer)
3493 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3494 msg->opt = htonl(opt);
3497 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3498 for (i = 0; i < p->length; i++)
3500 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3507 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3513 * Fill the core buffer
3515 * @param cls closure (data itself)
3516 * @param size number of bytes available in buf
3517 * @param buf where the callee should write the message
3519 * @return number of bytes written to buf
3522 send_core_data_multicast (void *cls, size_t size, void *buf)
3524 struct MeshTransmissionDescriptor *info = cls;
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3528 GNUNET_assert (NULL != info);
3529 GNUNET_assert (NULL != info->peer);
3530 total_size = info->mesh_data->data_len;
3531 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3533 if (total_size > size)
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3539 memcpy (buf, info->mesh_data->data, total_size);
3542 struct GNUNET_MessageHeader *mh;
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3546 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
3549 data_descriptor_decrement_rc (info->mesh_data);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3558 * Creates a path ack message in buf and frees all unused resources.
3560 * @param cls closure (MeshTransmissionDescriptor)
3561 * @param size number of bytes available in buf
3562 * @param buf where the callee should write the message
3563 * @return number of bytes written to buf
3566 send_core_path_ack (void *cls, size_t size, void *buf)
3568 struct MeshTransmissionDescriptor *info = cls;
3569 struct GNUNET_MESH_PathACK *msg = buf;
3571 GNUNET_assert (NULL != info);
3572 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3577 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3578 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3579 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3580 msg->tid = htonl (info->origin->tid);
3581 msg->peer_id = my_full_id;
3584 /* TODO add signature */
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3587 return sizeof (struct GNUNET_MESH_PathACK);
3592 * Free a transmission that was already queued with all resources
3593 * associated to the request.
3595 * @param queue Queue handler to cancel.
3596 * @param clear_cls Is it necessary to free associated cls?
3599 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3601 struct MeshTransmissionDescriptor *dd;
3602 struct MeshPathInfo *path_info;
3603 // unsigned int max;
3605 if (GNUNET_YES == clear_cls)
3607 switch (queue->type)
3609 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3610 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3611 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3613 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3614 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3615 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3616 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3617 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3619 " prebuilt message\n");
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 GNUNET_MESH_DEBUG_M2S(queue->type));
3624 data_descriptor_decrement_rc (dd->mesh_data);
3626 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3628 path_info = queue->cls;
3629 path_destroy (path_info->path);
3633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3634 " type %s unknown!\n",
3635 GNUNET_MESH_DEBUG_M2S(queue->type));
3637 GNUNET_free_non_null (queue->cls);
3639 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3640 queue->peer->queue_tail,
3643 /* Delete from child_fc in the appropiate tunnel */
3644 // max = queue->tunnel->fwd_queue_max;
3645 // GNUNET_PEER_resolve (queue->peer->id, &id);
3646 // if (NULL != cinfo)
3648 // for (i = 0; i < cinfo->send_buffer_n; i++)
3650 // i2 = (cinfo->send_buffer_start + i) % max;
3651 // if (cinfo->send_buffer[i2] == queue)
3653 // /* Found corresponding entry in the send_buffer. Move all others back. */
3657 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3659 // j2 = (cinfo->send_buffer_start + j) % max;
3660 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3661 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3664 // cinfo->send_buffer[j3] = NULL;
3665 // cinfo->send_buffer_n--;
3670 GNUNET_free (queue);
3675 * @brief Get the next transmittable message from the queue.
3677 * This will be the head, except in the case of being a data packet
3678 * not allowed by the destination peer.
3680 * @param peer Destination peer.
3682 * @return The next viable MeshPeerQueue element to send to that peer.
3683 * NULL when there are no transmittable messages.
3685 struct MeshPeerQueue *
3686 queue_get_next (const struct MeshPeerInfo *peer)
3688 struct MeshPeerQueue *q;
3690 struct MeshTransmissionDescriptor *info;
3691 // struct GNUNET_MESH_Unicast *ucast;
3692 // struct GNUNET_MESH_ToOrigin *to_orig;
3693 struct GNUNET_PeerIdentity id;
3695 // uint32_t ack; FIXME
3697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3698 for (q = peer->queue_head; NULL != q; q = q->next)
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3704 GNUNET_MESH_DEBUG_M2S(q->type));
3707 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3708 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3709 // pid = ntohl (ucast->pid);
3710 GNUNET_PEER_resolve (info->peer->id, &id);
3711 // ack = cinfo->fwd_ack;
3713 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3714 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3715 // pid = ntohl (to_orig->pid);
3716 // ack = t->bck_ack;
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3724 // "********* ACK: %u, PID: %u\n",
3726 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3728 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3729 // "********* OK!\n");
3734 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3735 // "********* NEXT!\n");
3738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3739 "********* nothing found\n");
3745 * Core callback to write a queued packet to core buffer
3747 * @param cls Closure (peer info).
3748 * @param size Number of bytes available in buf.
3749 * @param buf Where the to write the message.
3751 * @return number of bytes written to buf
3754 queue_send (void *cls, size_t size, void *buf)
3756 struct MeshPeerInfo *peer = cls;
3757 struct GNUNET_MessageHeader *msg;
3758 struct MeshPeerQueue *queue;
3759 struct MeshTunnel *t;
3760 struct GNUNET_PeerIdentity dst_id;
3763 peer->core_transmit = NULL;
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3766 queue = queue_get_next (peer);
3768 /* Queue has no internal mesh traffic nor sendable payload */
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3772 if (NULL == peer->queue_head)
3773 GNUNET_break (0); // Should've been canceled
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3778 GNUNET_PEER_resolve (peer->id, &dst_id);
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3780 "********* towards %s\n",
3781 GNUNET_i2s(&dst_id));
3782 /* Check if buffer size is enough for the message */
3783 if (queue->size > size)
3785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3786 "********* not enough room, reissue\n");
3787 peer->core_transmit =
3788 GNUNET_CORE_notify_transmit_ready (core_handle,
3791 GNUNET_TIME_UNIT_FOREVER_REL,
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3801 GNUNET_assert (0 < t->pending_messages);
3802 t->pending_messages--;
3803 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3807 "********* unicast: t->q (%u/%u)\n",
3808 t->fwd_queue_n, t->fwd_queue_max);
3810 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
3817 switch (queue->type)
3820 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3821 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3822 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3823 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3824 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3826 "********* raw: %s\n",
3827 GNUNET_MESH_DEBUG_M2S (queue->type));
3829 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3830 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3831 data_size = send_core_data_raw (queue->cls, size, buf);
3832 msg = (struct GNUNET_MessageHeader *) buf;
3833 switch (ntohs (msg->type)) // Type of preconstructed message
3835 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3836 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3838 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3839 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3845 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3847 data_size = send_core_path_create (queue->cls, size, buf);
3849 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3851 data_size = send_core_path_ack (queue->cls, size, buf);
3853 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
3855 data_size = send_core_data_multicast (queue->cls, size, buf);
3859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3860 "********* type unknown: %u\n",
3864 switch (queue->type)
3866 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3867 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3868 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3870 // GNUNET_break (0);
3871 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3872 // "at pos %u (%p) != %p\n",
3873 // cinfo->send_buffer_start,
3874 // cinfo->send_buffer[cinfo->send_buffer_start],
3877 // if (cinfo->send_buffer_n > 0)
3879 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3880 // cinfo->send_buffer_n--;
3881 // cinfo->send_buffer_start++;
3882 // cinfo->send_buffer_start %= t->fwd_queue_max;
3886 // GNUNET_break (0);
3893 /* Free queue, but cls was freed by send_core_* */
3894 queue_destroy (queue, GNUNET_NO);
3896 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3902 /* If more data in queue, send next */
3903 queue = queue_get_next(peer);
3906 struct GNUNET_PeerIdentity id;
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3909 GNUNET_PEER_resolve (peer->id, &id);
3910 peer->core_transmit =
3911 GNUNET_CORE_notify_transmit_ready(core_handle,
3914 GNUNET_TIME_UNIT_FOREVER_REL,
3922 if (NULL != peer->queue_head)
3924 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3925 "********* %s stalled\n",
3926 GNUNET_i2s(&my_full_id));
3927 // if (NULL == cinfo) FIXME
3928 // cinfo = tunnel_get_neighbor_fc (t, &dst_id);
3929 // // FIXME unify bck/fwd structures, bck does not have cinfo right now
3930 // if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
3932 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3933 // &tunnel_poll, cinfo);
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3943 * @brief Queue and pass message to core when possible.
3945 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
3946 * and accounts for it. In case the queue is full, the message is dropped and
3949 * Otherwise, message is treated as internal and allowed to go regardless of
3952 * @param cls Closure (@c type dependant). It will be used by queue_send to
3953 * build the message to be sent if not already prebuilt.
3954 * @param type Type of the message, 0 for a raw message.
3955 * @param size Size of the message.
3956 * @param dst Neighbor to send message to.
3957 * @param t Tunnel this message belongs to.
3960 queue_add (void *cls, uint16_t type, size_t size,
3961 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3963 struct MeshPeerQueue *queue;
3964 struct GNUNET_PeerIdentity id;
3969 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
3970 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
3972 n = &t->fwd_queue_n;
3973 max = &t->fwd_queue_max;
3975 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3977 n = &t->bck_queue_n;
3978 max = &t->bck_queue_max;
3985 GNUNET_STATISTICS_update(stats,
3986 "# messages dropped (buffer full)",
3988 return; // Drop message
3992 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3998 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3999 GNUNET_PEER_resolve (dst->id, &id);
4000 if (NULL == dst->core_transmit)
4002 dst->core_transmit =
4003 GNUNET_CORE_notify_transmit_ready (core_handle,
4006 GNUNET_TIME_UNIT_FOREVER_REL,
4012 t->pending_messages++;
4013 if (NULL == n) // Is this internal mesh traffic?
4016 // It's payload, keep track of buffer per peer. FIXME
4017 // i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4018 // if (NULL != cinfo->send_buffer[i])
4020 // GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4021 // queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4022 // cinfo->send_buffer_start++;
4023 // cinfo->send_buffer_start %= t->fwd_queue_max;
4027 // cinfo->send_buffer_n++;
4029 // cinfo->send_buffer[i] = queue;
4030 // if (cinfo->send_buffer_n > t->fwd_queue_max)
4032 // GNUNET_break (0);
4033 // cinfo->send_buffer_n = t->fwd_queue_max;
4038 /******************************************************************************/
4039 /******************** MESH NETWORK HANDLERS **************************/
4040 /******************************************************************************/
4044 * Core handler for path creation
4046 * @param cls closure
4047 * @param message message
4048 * @param peer peer identity this notification is about
4050 * @return GNUNET_OK to keep the connection open,
4051 * GNUNET_SYSERR to close it (signal serious error)
4054 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4055 const struct GNUNET_MessageHeader *message)
4057 unsigned int own_pos;
4060 MESH_TunnelNumber tid;
4061 struct GNUNET_MESH_ManipulatePath *msg;
4062 struct GNUNET_PeerIdentity *pi;
4063 struct GNUNET_HashCode hash;
4064 struct MeshPeerPath *path;
4065 struct MeshPeerInfo *dest_peer_info;
4066 struct MeshPeerInfo *orig_peer_info;
4067 struct MeshTunnel *t;
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 "Received a path create msg [%s]\n",
4071 GNUNET_i2s (&my_full_id));
4072 size = ntohs (message->size);
4073 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4075 GNUNET_break_op (0);
4079 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4080 if (size % sizeof (struct GNUNET_PeerIdentity))
4082 GNUNET_break_op (0);
4085 size /= sizeof (struct GNUNET_PeerIdentity);
4088 GNUNET_break_op (0);
4091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4092 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4094 tid = ntohl (msg->tid);
4095 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4097 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4098 t = tunnel_get (pi, tid);
4099 if (NULL == t) // FIXME only for INCOMING tunnels?
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4104 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4107 // FIXME notify failure
4110 opt = ntohl (msg->opt);
4111 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4113 t->nobuffer = GNUNET_YES;
4114 t->last_fwd_ack = t->fwd_pid + 1;
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
4118 if (GNUNET_YES == t->nobuffer)
4120 t->bck_queue_max = 1;
4121 t->fwd_queue_max = 1;
4124 // FIXME only assign a local tid if a local client is interested (on demand)
4125 while (NULL != tunnel_get_incoming (next_local_tid))
4126 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4127 t->local_tid_dest = next_local_tid++;
4128 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4131 tunnel_reset_timeout (t);
4132 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4134 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4143 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4144 if (NULL == dest_peer_info)
4146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4147 " Creating PeerInfo for destination.\n");
4148 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4149 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4150 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4154 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4155 if (NULL == orig_peer_info)
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4158 " Creating PeerInfo for origin.\n");
4159 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4160 orig_peer_info->id = GNUNET_PEER_intern (pi);
4161 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4165 path = path_new (size);
4167 for (i = 0; i < size; i++)
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4170 GNUNET_i2s (&pi[i]));
4171 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4172 if (path->peers[i] == myid)
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4178 /* cannot be self, must be 'not found' */
4179 /* create path: self not found in path through self */
4180 GNUNET_break_op (0);
4181 path_destroy (path);
4185 path_add_to_peers (path, GNUNET_NO);
4186 tunnel_add_path (t, path, own_pos);
4187 if (own_pos == size - 1)
4189 /* It is for us! Send ack. */
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4191 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4197 struct MeshPeerPath *path2;
4199 /* It's for somebody else! Retransmit. */
4200 path2 = path_duplicate (path);
4201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4202 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4203 path2 = path_duplicate (path);
4204 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4205 send_create_path (dest_peer_info, path, t);
4212 * Core handler for path destruction
4214 * @param cls closure
4215 * @param message message
4216 * @param peer peer identity this notification is about
4218 * @return GNUNET_OK to keep the connection open,
4219 * GNUNET_SYSERR to close it (signal serious error)
4222 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4223 const struct GNUNET_MessageHeader *message)
4225 struct GNUNET_MESH_ManipulatePath *msg;
4226 struct GNUNET_PeerIdentity *pi;
4227 struct MeshPeerPath *path;
4228 struct MeshTunnel *t;
4229 unsigned int own_pos;
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4234 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4235 size = ntohs (message->size);
4236 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4238 GNUNET_break_op (0);
4242 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4243 if (size % sizeof (struct GNUNET_PeerIdentity))
4245 GNUNET_break_op (0);
4248 size /= sizeof (struct GNUNET_PeerIdentity);
4251 GNUNET_break_op (0);
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4256 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4257 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4259 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4261 t = tunnel_get (pi, ntohl (msg->tid));
4264 /* TODO notify back: we don't know this tunnel */
4265 GNUNET_break_op (0);
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4269 path = path_new (size);
4271 for (i = 0; i < size; i++)
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4274 GNUNET_i2s (&pi[i]));
4275 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4276 if (path->peers[i] == myid)
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4280 if (own_pos < path->length - 1)
4281 send_prebuilt_message (message, &pi[own_pos + 1], t);
4283 send_client_tunnel_disconnect(t, NULL);
4285 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
4286 path_destroy (path);
4292 * Core handler for notifications of broken paths
4294 * @param cls closure
4295 * @param message message
4296 * @param peer peer identity this notification is about
4298 * @return GNUNET_OK to keep the connection open,
4299 * GNUNET_SYSERR to close it (signal serious error)
4302 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4303 const struct GNUNET_MessageHeader *message)
4305 struct GNUNET_MESH_PathBroken *msg;
4306 struct MeshTunnel *t;
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4309 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4310 msg = (struct GNUNET_MESH_PathBroken *) message;
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4312 GNUNET_i2s (&msg->peer1));
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4314 GNUNET_i2s (&msg->peer2));
4315 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4318 GNUNET_break_op (0);
4321 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4322 GNUNET_PEER_search (&msg->peer2));
4329 * Core handler for tunnel destruction
4331 * @param cls closure
4332 * @param message message
4333 * @param peer peer identity this notification is about
4335 * @return GNUNET_OK to keep the connection open,
4336 * GNUNET_SYSERR to close it (signal serious error)
4339 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4340 const struct GNUNET_MessageHeader *message)
4342 struct GNUNET_MESH_TunnelDestroy *msg;
4343 struct MeshTunnel *t;
4344 GNUNET_PEER_Id parent;
4347 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4349 "Got a TUNNEL DESTROY packet from %s\n",
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4352 " for tunnel %s [%u]\n",
4353 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4354 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4355 /* Check signature */
4358 /* Probably already got the message from another path,
4359 * destroyed the tunnel and retransmitted to children.
4362 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4366 parent = tree_get_predecessor (t->tree);
4367 pid = GNUNET_PEER_search (peer);
4372 // tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
4373 nc = tree_count_children (t->tree);
4374 if (nc > 0 || NULL != t->owner || t->nclients > 0)
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4377 "still in use: %u cl, %u ch\n",
4382 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4384 /* Tunnel was incoming, notify clients */
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4386 t->local_tid, t->local_tid_dest);
4387 send_clients_tunnel_destroy (t);
4389 tunnel_send_destroy (t, parent);
4390 t->destroy = GNUNET_YES;
4391 // TODO: add timeout to destroy the tunnel anyway
4397 * Core handler for mesh network traffic going from the origin to a peer
4399 * @param cls closure
4400 * @param peer peer identity this notification is about
4401 * @param message message
4402 * @return GNUNET_OK to keep the connection open,
4403 * GNUNET_SYSERR to close it (signal serious error)
4406 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4407 const struct GNUNET_MessageHeader *message)
4409 struct GNUNET_MESH_Unicast *msg;
4410 struct GNUNET_PeerIdentity *neighbor;
4411 struct MeshTunnel *t;
4412 GNUNET_PEER_Id dest_id;
4417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4420 size = ntohs (message->size);
4422 sizeof (struct GNUNET_MESH_Unicast) +
4423 sizeof (struct GNUNET_MessageHeader))
4428 msg = (struct GNUNET_MESH_Unicast *) message;
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4430 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4432 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4435 /* TODO notify back: we don't know this tunnel */
4436 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4437 GNUNET_break_op (0);
4440 pid = ntohl (msg->pid);
4441 if (t->fwd_pid == pid)
4443 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4445 " Already seen pid %u, DROPPING!\n", pid);
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4451 " pid %u not seen yet, forwarding\n", pid);
4456 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4458 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4459 GNUNET_break_op (0);
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4461 "Received PID %u, ACK %u\n",
4462 pid, t->last_fwd_ack);
4466 tunnel_reset_timeout (t);
4468 if (dest_id == myid)
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4471 " it's for us! sending to clients...\n");
4472 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4473 send_subscribed_clients (message, &msg[1].header, t);
4474 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4477 ttl = ntohl (msg->ttl);
4478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4481 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4483 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4487 " not for us, retransmitting...\n");
4489 neighbor = tree_get_first_hop (t->tree, dest_id);
4491 /* cinfo->fwd_pid = pid; FIXME
4493 if (GNUNET_YES == t->nobuffer &&
4494 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4496 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4497 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4498 GNUNET_break_op (0);
4501 send_prebuilt_message (message, neighbor, t);
4502 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4508 * Core handler for mesh network traffic toward the owner of a tunnel
4510 * @param cls closure
4511 * @param message message
4512 * @param peer peer identity this notification is about
4514 * @return GNUNET_OK to keep the connection open,
4515 * GNUNET_SYSERR to close it (signal serious error)
4518 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4519 const struct GNUNET_MessageHeader *message)
4521 struct GNUNET_MESH_ToOrigin *msg;
4522 struct GNUNET_PeerIdentity id;
4523 struct MeshPeerInfo *peer_info;
4524 struct MeshTunnel *t;
4525 GNUNET_PEER_Id predecessor;
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4531 size = ntohs (message->size);
4532 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4533 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4535 GNUNET_break_op (0);
4538 msg = (struct GNUNET_MESH_ToOrigin *) message;
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4540 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4541 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4542 pid = ntohl (msg->pid);
4546 /* TODO notify that we dont know this tunnel (whom)? */
4547 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4549 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4550 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4555 // if (cinfo->bck_pid == pid) FIXME
4557 // /* already seen this packet, drop */
4558 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4559 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4560 // " Already seen pid %u, DROPPING!\n", pid);
4561 // tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4562 // return GNUNET_OK;
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 " pid %u not seen yet, forwarding\n", pid);
4567 // cinfo->bck_pid = pid; FIXME
4569 if (NULL != t->owner)
4572 struct GNUNET_MESH_ToOrigin *copy;
4574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4575 " it's for us! sending to clients...\n");
4576 /* TODO signature verification */
4577 memcpy (cbuf, message, size);
4578 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4579 copy->tid = htonl (t->local_tid);
4581 copy->pid = htonl (t->bck_pid);
4582 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4583 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4584 ©->header, GNUNET_NO);
4585 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 " not for us, retransmitting...\n");
4591 peer_info = peer_info_get (&msg->oid);
4592 if (NULL == peer_info)
4594 /* unknown origin of tunnel */
4598 predecessor = tree_get_predecessor (t->tree);
4599 if (0 == predecessor)
4601 if (GNUNET_YES == t->destroy)
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4604 "to orig received on a dying tunnel %s [%X]\n",
4605 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4609 "unknown to origin at %s\n",
4610 GNUNET_i2s (&my_full_id));
4611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4615 "for tunnel %s [%X]\n",
4616 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4619 tree_debug (t->tree);
4622 GNUNET_PEER_resolve (predecessor, &id);
4623 send_prebuilt_message (message, &id, t);
4624 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4631 * Core handler for mesh network traffic point-to-point acks.
4633 * @param cls closure
4634 * @param message message
4635 * @param peer peer identity this notification is about
4637 * @return GNUNET_OK to keep the connection open,
4638 * GNUNET_SYSERR to close it (signal serious error)
4641 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4642 const struct GNUNET_MessageHeader *message)
4644 struct GNUNET_MESH_ACK *msg;
4645 struct MeshTunnel *t;
4648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4650 msg = (struct GNUNET_MESH_ACK *) message;
4652 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4656 /* TODO notify that we dont know this tunnel (whom)? */
4657 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4660 ack = ntohl (msg->pid);
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4663 /* Is this a forward or backward ACK? */
4664 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4669 // cinfo->fwd_ack = ack; FIXME
4670 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4671 tunnel_unlock_fwd_queues (t);
4672 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4674 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4675 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4676 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4683 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4684 tunnel_unlock_bck_queue (t);
4691 * Core handler for mesh network traffic point-to-point ack polls.
4693 * @param cls closure
4694 * @param message message
4695 * @param peer peer identity this notification is about
4697 * @return GNUNET_OK to keep the connection open,
4698 * GNUNET_SYSERR to close it (signal serious error)
4701 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4702 const struct GNUNET_MessageHeader *message)
4704 struct GNUNET_MESH_Poll *msg;
4705 struct MeshTunnel *t;
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4710 msg = (struct GNUNET_MESH_Poll *) message;
4712 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4716 /* TODO notify that we dont know this tunnel (whom)? */
4717 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4718 GNUNET_break_op (0);
4722 /* Is this a forward or backward ACK? */
4723 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4726 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4727 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4732 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4739 * Core handler for path ACKs
4741 * @param cls closure
4742 * @param message message
4743 * @param peer peer identity this notification is about
4745 * @return GNUNET_OK to keep the connection open,
4746 * GNUNET_SYSERR to close it (signal serious error)
4749 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4750 const struct GNUNET_MessageHeader *message)
4752 struct GNUNET_MESH_PathACK *msg;
4753 struct GNUNET_PeerIdentity id;
4754 struct MeshPeerInfo *peer_info;
4755 struct MeshPeerPath *p;
4756 struct MeshTunnel *t;
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4759 GNUNET_i2s (&my_full_id));
4760 msg = (struct GNUNET_MESH_PathACK *) message;
4761 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4764 /* TODO notify that we don't know the tunnel */
4765 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4767 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4771 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4773 peer_info = peer_info_get (&msg->peer_id);
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4775 GNUNET_i2s (&msg->peer_id));
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4779 /* Add paths to peers? */
4780 p = tree_get_path_to_peer (t->tree, peer_info->id);
4783 path_add_to_peers (p, GNUNET_YES);
4791 /* Message for us? */
4792 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4795 if (NULL == t->owner)
4797 GNUNET_break_op (0);
4800 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4802 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4803 // send_client_peer_connected (t, peer_info->id);
4805 if (NULL != peer_info->dhtget)
4807 GNUNET_DHT_get_stop (peer_info->dhtget);
4808 peer_info->dhtget = NULL;
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4814 " not for us, retransmitting...\n");
4815 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4816 peer_info = peer_info_get (&msg->oid);
4817 send_prebuilt_message (message, &id, t);
4823 * Core handler for mesh keepalives.
4825 * @param cls closure
4826 * @param message message
4827 * @param peer peer identity this notification is about
4828 * @return GNUNET_OK to keep the connection open,
4829 * GNUNET_SYSERR to close it (signal serious error)
4831 * TODO: Check who we got this from, to validate route.
4834 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4835 const struct GNUNET_MessageHeader *message)
4837 struct GNUNET_MESH_TunnelKeepAlive *msg;
4838 struct MeshTunnel *t;
4840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4843 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4844 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4848 /* TODO notify that we dont know that tunnel */
4849 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4854 tunnel_reset_timeout (t);
4856 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4857 /* FIXME tunnel_send_multicast (t, message); */
4864 * Functions to handle messages from core
4866 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4867 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4868 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4869 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4870 sizeof (struct GNUNET_MESH_PathBroken)},
4871 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4872 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4873 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4874 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4875 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4876 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4877 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4878 sizeof (struct GNUNET_MESH_ACK)},
4879 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4880 sizeof (struct GNUNET_MESH_Poll)},
4881 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4882 sizeof (struct GNUNET_MESH_PathACK)},
4888 /******************************************************************************/
4889 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4890 /******************************************************************************/
4895 * notify_client_connection_failure: notify a client that the connection to the
4896 * requested remote peer is not possible (for instance, no route found)
4897 * Function called when the socket is ready to queue more data. "buf" will be
4898 * NULL and "size" zero if the socket was closed for writing in the meantime.
4900 * @param cls closure
4901 * @param size number of bytes available in buf
4902 * @param buf where the callee should write the message
4903 * @return number of bytes written to buf
4906 notify_client_connection_failure (void *cls, size_t size, void *buf)
4909 struct MeshPeerInfo *peer_info;
4910 struct GNUNET_MESH_PeerControl *msg;
4911 struct GNUNET_PeerIdentity id;
4913 if (0 == size && NULL == buf)
4915 // TODO retry? cancel?
4919 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4920 peer_info = (struct MeshPeerInfo *) cls;
4921 msg = (struct GNUNET_MESH_PeerControl *) buf;
4922 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4923 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4924 // msg->tunnel_id = htonl(peer_info->t->tid);
4925 GNUNET_PEER_resolve (peer_info->id, &id);
4926 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4934 * Send keepalive packets for a peer
4936 * @param cls Closure (tunnel for which to send the keepalive).
4937 * @param tc Notification context.
4940 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4942 struct MeshTunnel *t = cls;
4943 struct GNUNET_MESH_TunnelKeepAlive *msg;
4944 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4947 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4948 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 "sending keepalive for tunnel %d\n", t->id.tid);
4956 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4957 msg->header.size = htons (size);
4958 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4959 msg->oid = my_full_id;
4960 msg->tid = htonl (t->id.tid);
4961 /* FIXME tunnel_send_multicast (t, &msg->header); */
4963 t->path_refresh_task =
4964 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4965 tunnel_reset_timeout(t);
4970 * Function to process paths received for a new peer addition. The recorded
4971 * paths form the initial tunnel, which can be optimized later.
4972 * Called on each result obtained for the DHT search.
4974 * @param cls closure
4975 * @param exp when will this value expire
4976 * @param key key of the result
4977 * @param get_path path of the get request
4978 * @param get_path_length lenght of get_path
4979 * @param put_path path of the put request
4980 * @param put_path_length length of the put_path
4981 * @param type type of the result
4982 * @param size number of bytes in data
4983 * @param data pointer to the result data
4985 * TODO: re-issue the request after certain time? cancel after X results?
4988 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4989 const struct GNUNET_HashCode * key,
4990 const struct GNUNET_PeerIdentity *get_path,
4991 unsigned int get_path_length,
4992 const struct GNUNET_PeerIdentity *put_path,
4993 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4994 size_t size, const void *data)
4996 struct MeshPathInfo *path_info = cls;
4997 struct MeshPeerPath *p;
4998 struct GNUNET_PeerIdentity pi;
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5002 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5005 p = path_build_from_dht (get_path, get_path_length,
5006 put_path, put_path_length);
5007 path_add_to_peers (p, GNUNET_NO);
5009 for (i = 0; i < path_info->peer->ntunnels; i++)
5011 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5012 peer_info_connect (path_info->peer, path_info->t);
5019 /******************************************************************************/
5020 /********************* MESH LOCAL HANDLES **************************/
5021 /******************************************************************************/
5025 * Handler for client disconnection
5027 * @param cls closure
5028 * @param client identification of the client; NULL
5029 * for the last call when the server is destroyed
5032 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5034 struct MeshClient *c;
5035 struct MeshClient *next;
5038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5048 if (c->handle != client)
5050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5056 GNUNET_SERVER_client_drop (c->handle);
5057 c->shutting_down = GNUNET_YES;
5058 GNUNET_assert (NULL != c->own_tunnels);
5059 GNUNET_assert (NULL != c->incoming_tunnels);
5060 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5061 &tunnel_destroy_iterator, c);
5062 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5063 &tunnel_destroy_iterator, c);
5064 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5065 &tunnel_destroy_iterator, c);
5066 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5067 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5068 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5070 if (NULL != c->types)
5071 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5073 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5076 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5085 * Handler for new clients
5087 * @param cls closure
5088 * @param client identification of the client
5089 * @param message the actual message, which includes messages the client wants
5092 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5093 const struct GNUNET_MessageHeader *message)
5095 struct GNUNET_MESH_ClientConnect *cc_msg;
5096 struct MeshClient *c;
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5103 /* Check data sanity */
5104 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5105 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5106 ntypes = ntohs (cc_msg->types);
5107 if (size != ntypes * sizeof (uint16_t))
5110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5114 /* Create new client structure */
5115 c = GNUNET_malloc (sizeof (struct MeshClient));
5116 c->id = next_client_id++;
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5119 GNUNET_SERVER_client_keep (client);
5123 struct GNUNET_HashCode hc;
5125 t = (uint16_t *) &cc_msg[1];
5126 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
5127 for (i = 0; i < ntypes; i++)
5130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5131 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5133 /* store in clients hashmap */
5134 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5136 /* store in global hashmap */
5137 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
5143 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5144 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5145 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5146 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5147 GNUNET_SERVER_notification_context_add (nc, client);
5148 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5156 * Handler for requests of new tunnels
5158 * @param cls closure
5159 * @param client identification of the client
5160 * @param message the actual message
5163 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5164 const struct GNUNET_MessageHeader *message)
5166 struct GNUNET_MESH_TunnelMessage *t_msg;
5167 struct MeshTunnel *t;
5168 struct MeshClient *c;
5169 MESH_TunnelNumber tid;
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5173 /* Sanity check for client registration */
5174 if (NULL == (c = client_get (client)))
5177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5182 /* Message sanity check */
5183 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5190 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5191 /* Sanity check for tunnel numbering */
5192 tid = ntohl (t_msg->tunnel_id);
5193 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5196 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5199 /* Sanity check for duplicate tunnel IDs */
5200 if (NULL != tunnel_get_by_local_id (c, tid))
5203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5207 while (NULL != tunnel_get_by_pi (myid, next_tid))
5208 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5209 t = tunnel_new (myid, next_tid++, c, tid);
5212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5214 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5217 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5219 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5222 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5228 * Handler for requests of deleting tunnels
5230 * @param cls closure
5231 * @param client identification of the client
5232 * @param message the actual message
5235 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5236 const struct GNUNET_MessageHeader *message)
5238 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5239 struct MeshClient *c;
5240 struct MeshTunnel *t;
5241 MESH_TunnelNumber tid;
5243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5244 "Got a DESTROY TUNNEL from client!\n");
5246 /* Sanity check for client registration */
5247 if (NULL == (c = client_get (client)))
5250 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5255 /* Message sanity check */
5256 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5263 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5265 /* Retrieve tunnel */
5266 tid = ntohl (tunnel_msg->tunnel_id);
5267 t = tunnel_get_by_local_id(c, tid);
5270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5275 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5277 client_ignore_tunnel (c, t);
5278 tunnel_destroy_empty (t);
5279 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5282 send_client_tunnel_disconnect (t, c);
5283 client_delete_tunnel (c, t);
5285 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5287 tunnel_send_destroy (t, 0);
5288 t->destroy = GNUNET_YES;
5289 /* The tunnel will be destroyed when the last message is transmitted. */
5290 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5296 * Handler for requests of seeting tunnel's buffering policy.
5298 * @param cls Closure (unused).
5299 * @param client Identification of the client.
5300 * @param message The actual message.
5303 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5304 const struct GNUNET_MessageHeader *message)
5306 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5307 struct MeshClient *c;
5308 struct MeshTunnel *t;
5309 MESH_TunnelNumber tid;
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5312 "Got a BUFFER request from client!\n");
5314 /* Sanity check for client registration */
5315 if (NULL == (c = client_get (client)))
5318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5323 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5325 /* Retrieve tunnel */
5326 tid = ntohl (tunnel_msg->tunnel_id);
5327 t = tunnel_get_by_local_id(c, tid);
5330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5336 switch (ntohs(message->type))
5338 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5339 t->nobuffer = GNUNET_NO;
5341 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5342 t->nobuffer = GNUNET_YES;
5348 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5353 * Handler for client traffic directed to one peer
5355 * @param cls closure
5356 * @param client identification of the client
5357 * @param message the actual message
5360 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5361 const struct GNUNET_MessageHeader *message)
5363 struct MeshClient *c;
5364 struct MeshTunnel *t;
5365 struct GNUNET_MESH_Unicast *data_msg;
5366 MESH_TunnelNumber tid;
5369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5370 "Got a unicast request from a client!\n");
5372 /* Sanity check for client registration */
5373 if (NULL == (c = client_get (client)))
5376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5381 data_msg = (struct GNUNET_MESH_Unicast *) message;
5383 /* Sanity check for message size */
5384 size = ntohs (message->size);
5385 if (sizeof (struct GNUNET_MESH_Unicast) +
5386 sizeof (struct GNUNET_MessageHeader) > size)
5389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5393 /* Tunnel exists? */
5394 tid = ntohl (data_msg->tid);
5395 t = tunnel_get_by_local_id (c, tid);
5399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5403 /* Is it a local tunnel? Then, does client own the tunnel? */
5404 if (t->owner->handle != client)
5407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5411 /* PID should be as expected */
5412 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
5415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5416 "Unicast PID, expected %u, got %u\n",
5417 t->fwd_pid + 1, ntohl (data_msg->pid));
5418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5422 /* Ok, everything is correct, send the message
5423 * (pretend we got it from a mesh peer)
5426 /* Work around const limitation */
5427 char buf[ntohs (message->size)] GNUNET_ALIGN;
5428 struct GNUNET_MESH_Unicast *copy;
5430 copy = (struct GNUNET_MESH_Unicast *) buf;
5431 memcpy (buf, data_msg, size);
5432 copy->oid = my_full_id;
5433 copy->tid = htonl (t->id.tid);
5434 copy->ttl = htonl (default_ttl);
5435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5436 " calling generic handler...\n");
5437 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
5439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5440 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5447 * Handler for client traffic directed to the origin
5449 * @param cls closure
5450 * @param client identification of the client
5451 * @param message the actual message
5454 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5455 const struct GNUNET_MessageHeader *message)
5457 struct GNUNET_MESH_ToOrigin *data_msg;
5458 struct MeshTunnelClientInfo *clinfo;
5459 struct MeshClient *c;
5460 struct MeshTunnel *t;
5461 MESH_TunnelNumber tid;
5464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5465 "Got a ToOrigin request from a client!\n");
5466 /* Sanity check for client registration */
5467 if (NULL == (c = client_get (client)))
5470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5475 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5477 /* Sanity check for message size */
5478 size = ntohs (message->size);
5479 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5480 sizeof (struct GNUNET_MessageHeader) > size)
5483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5487 /* Tunnel exists? */
5488 tid = ntohl (data_msg->tid);
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5490 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5496 t = tunnel_get_by_local_id (c, tid);
5499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5506 /* It should be sent by someone who has this as incoming tunnel. */
5507 if (GNUNET_NO == client_knows_tunnel (c, t))
5510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5514 /* PID should be as expected */
5515 clinfo = tunnel_get_client_fc (t, c);
5516 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5520 "To Origin PID, expected %u, got %u\n",
5521 clinfo->bck_pid + 1,
5522 ntohl (data_msg->pid));
5523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5528 /* Ok, everything is correct, send the message
5529 * (pretend we got it from a mesh peer)
5532 char buf[ntohs (message->size)] GNUNET_ALIGN;
5533 struct GNUNET_MESH_ToOrigin *copy;
5535 /* Work around const limitation */
5536 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5537 memcpy (buf, data_msg, size);
5538 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5539 copy->tid = htonl (t->id.tid);
5540 copy->ttl = htonl (default_ttl);
5541 copy->pid = htonl (t->bck_pid + 1);
5543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5544 " calling generic handler...\n");
5545 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5554 * Handler for client's ACKs for payload traffic.
5556 * @param cls Closure (unused).
5557 * @param client Identification of the client.
5558 * @param message The actual message.
5561 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5562 const struct GNUNET_MessageHeader *message)
5564 struct GNUNET_MESH_LocalAck *msg;
5565 struct MeshTunnel *t;
5566 struct MeshClient *c;
5567 MESH_TunnelNumber tid;
5570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5571 /* Sanity check for client registration */
5572 if (NULL == (c = client_get (client)))
5575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5580 msg = (struct GNUNET_MESH_LocalAck *) message;
5582 /* Tunnel exists? */
5583 tid = ntohl (msg->tunnel_id);
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5585 t = tunnel_get_by_local_id (c, tid);
5589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5590 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5591 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5595 ack = ntohl (msg->max_pid);
5596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5598 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5599 if (NULL != t->owner && t->owner->handle == client)
5601 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5603 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5607 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5608 tunnel_set_client_fwd_ack (t, c, ack);
5609 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5612 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5620 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5622 * @param cls Closure (client handle).
5623 * @param key Key (hashed tunnel ID, unused).
5624 * @param value Tunnel info.
5626 * @return GNUNET_YES, to keep iterating.
5629 monitor_all_tunnels_iterator (void *cls,
5630 const struct GNUNET_HashCode * key,
5633 struct GNUNET_SERVER_Client *client = cls;
5634 struct MeshTunnel *t = value;
5635 struct GNUNET_MESH_LocalMonitor *msg;
5637 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5638 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5639 msg->tunnel_id = htonl (t->id.tid);
5640 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5641 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5642 GNUNET_PEER_resolve (t->peer, &msg->destination);
5644 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5645 "* sending info about tunnel %s [%u]\n",
5646 GNUNET_i2s (&msg->owner), t->id.tid);
5648 GNUNET_SERVER_notification_context_unicast (nc, client,
5649 &msg->header, GNUNET_NO);
5655 * Handler for client's MONITOR request.
5657 * @param cls Closure (unused).
5658 * @param client Identification of the client.
5659 * @param message The actual message.
5662 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5663 const struct GNUNET_MessageHeader *message)
5665 struct MeshClient *c;
5667 /* Sanity check for client registration */
5668 if (NULL == (c = client_get (client)))
5671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5675 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5676 "Received get tunnels request from client %u\n",
5678 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5679 monitor_all_tunnels_iterator,
5681 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5682 "Get tunnels request from client %u completed\n",
5684 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5689 * Handler for client's MONITOR_TUNNEL request.
5691 * @param cls Closure (unused).
5692 * @param client Identification of the client.
5693 * @param message The actual message.
5696 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5697 const struct GNUNET_MessageHeader *message)
5699 const struct GNUNET_MESH_LocalMonitor *msg;
5700 struct GNUNET_MESH_LocalMonitor *resp;
5701 struct MeshClient *c;
5702 struct MeshTunnel *t;
5704 /* Sanity check for client registration */
5705 if (NULL == (c = client_get (client)))
5708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5712 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5713 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5714 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5717 ntohl (msg->tunnel_id));
5718 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5721 /* We don't know the tunnel FIXME */
5722 struct GNUNET_MESH_LocalMonitor warn;
5725 GNUNET_SERVER_notification_context_unicast (nc, client,
5728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5732 /* Initialize context */
5733 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5735 GNUNET_PEER_resolve (t->peer, &resp->destination);
5736 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5737 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5738 &resp->header, GNUNET_NO);
5741 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5742 "Monitor tunnel request from client %u completed\n",
5744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5749 * Functions to handle messages from clients
5751 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5752 {&handle_local_new_client, NULL,
5753 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5754 {&handle_local_tunnel_create, NULL,
5755 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5756 sizeof (struct GNUNET_MESH_TunnelMessage)},
5757 {&handle_local_tunnel_destroy, NULL,
5758 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5759 sizeof (struct GNUNET_MESH_TunnelMessage)},
5760 {&handle_local_tunnel_buffer, NULL,
5761 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5762 sizeof (struct GNUNET_MESH_TunnelMessage)},
5763 {&handle_local_tunnel_buffer, NULL,
5764 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5765 sizeof (struct GNUNET_MESH_TunnelMessage)},
5766 {&handle_local_unicast, NULL,
5767 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5768 {&handle_local_to_origin, NULL,
5769 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5770 {&handle_local_ack, NULL,
5771 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5772 sizeof (struct GNUNET_MESH_LocalAck)},
5773 {&handle_local_get_tunnels, NULL,
5774 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5775 sizeof (struct GNUNET_MessageHeader)},
5776 {&handle_local_show_tunnel, NULL,
5777 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5778 sizeof (struct GNUNET_MESH_LocalMonitor)},
5784 * To be called on core init/fail.
5786 * @param cls service closure
5787 * @param server handle to the server for this service
5788 * @param identity the public identity of this peer
5791 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5792 const struct GNUNET_PeerIdentity *identity)
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5796 core_handle = server;
5797 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5801 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5803 GNUNET_i2s (identity));
5804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5806 GNUNET_i2s (&my_full_id));
5807 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5809 GNUNET_abort(); // Try harder
5816 * Method called whenever a given peer connects.
5818 * @param cls closure
5819 * @param peer peer identity this notification is about
5822 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5824 struct MeshPeerInfo *peer_info;
5825 struct MeshPeerPath *path;
5827 DEBUG_CONN ("Peer connected\n");
5828 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5829 peer_info = peer_info_get (peer);
5830 if (myid == peer_info->id)
5832 DEBUG_CONN (" (self)\n");
5837 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5839 path = path_new (2);
5840 path->peers[0] = myid;
5841 path->peers[1] = peer_info->id;
5842 GNUNET_PEER_change_rc (myid, 1);
5843 GNUNET_PEER_change_rc (peer_info->id, 1);
5844 peer_info_add_path (peer_info, path, GNUNET_YES);
5845 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5851 * Method called whenever a peer disconnects.
5853 * @param cls closure
5854 * @param peer peer identity this notification is about
5857 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5859 struct MeshPeerInfo *pi;
5860 struct MeshPeerQueue *q;
5861 struct MeshPeerQueue *n;
5863 DEBUG_CONN ("Peer disconnected\n");
5864 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5874 /* TODO try to reroute this traffic instead */
5875 queue_destroy(q, GNUNET_YES);
5878 if (NULL != pi->core_transmit)
5880 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5881 pi->core_transmit = NULL;
5883 peer_info_remove_path (pi, pi->id, myid);
5886 DEBUG_CONN (" (self)\n");
5888 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5893 /******************************************************************************/
5894 /************************ MAIN FUNCTIONS ****************************/
5895 /******************************************************************************/
5898 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5900 * @param cls closure
5901 * @param key current key code
5902 * @param value value in the hash map
5903 * @return GNUNET_YES if we should continue to iterate,
5907 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5909 struct MeshTunnel *t = value;
5916 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5918 * @param cls closure
5919 * @param key current key code
5920 * @param value value in the hash map
5921 * @return GNUNET_YES if we should continue to iterate,
5925 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5927 struct MeshPeerInfo *p = value;
5928 struct MeshPeerQueue *q;
5929 struct MeshPeerQueue *n;
5937 queue_destroy(q, GNUNET_YES);
5941 peer_info_destroy (p);
5947 * Task run during shutdown.
5953 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5957 if (core_handle != NULL)
5959 GNUNET_CORE_disconnect (core_handle);
5964 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5967 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5968 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5969 if (dht_handle != NULL)
5971 GNUNET_DHT_disconnect (dht_handle);
5976 GNUNET_SERVER_notification_context_destroy (nc);
5979 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5981 GNUNET_SCHEDULER_cancel (announce_id_task);
5982 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5989 * Callback for hostkey read/generation.
5991 * @param cls Closure (Configuration handle).
5992 * @param pk The ECC private key.
5993 * @param emsg Error message, if any.
5996 key_generation_cb (void *cls,
5997 struct GNUNET_CRYPTO_EccPrivateKey *pk,
6000 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6001 struct MeshPeerInfo *peer;
6002 struct MeshPeerPath *p;
6007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6008 _("Mesh service could not access hostkey: %s. Exiting.\n"),
6010 GNUNET_SCHEDULER_shutdown ();
6013 my_private_key = pk;
6014 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6015 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6016 &my_full_id.hashPubKey);
6017 myid = GNUNET_PEER_intern (&my_full_id);
6018 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6019 "Mesh for peer [%s] starting\n",
6020 GNUNET_i2s(&my_full_id));
6022 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6023 NULL, /* Closure passed to MESH functions */
6024 &core_init, /* Call core_init once connected */
6025 &core_connect, /* Handle connects */
6026 &core_disconnect, /* remove peers on disconnects */
6027 NULL, /* Don't notify about all incoming messages */
6028 GNUNET_NO, /* For header only in notification */
6029 NULL, /* Don't notify about all outbound messages */
6030 GNUNET_NO, /* For header-only out notification */
6031 core_handlers); /* Register these handlers */
6033 if (core_handle == NULL)
6036 GNUNET_SCHEDULER_shutdown ();
6041 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6044 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6045 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6046 GNUNET_SERVER_disconnect_notify (server_handle,
6047 &handle_local_client_disconnect, NULL);
6051 clients_tail = NULL;
6054 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6056 /* Create a peer_info for the local peer */
6057 peer = peer_info_get (&my_full_id);
6060 GNUNET_PEER_change_rc (myid, 1);
6061 peer_info_add_path (peer, p, GNUNET_YES);
6062 GNUNET_SERVER_resume (server_handle);
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6068 * Process mesh requests.
6070 * @param cls closure
6071 * @param server the initialized server
6072 * @param c configuration to use
6075 run (void *cls, struct GNUNET_SERVER_Handle *server,
6076 const struct GNUNET_CONFIGURATION_Handle *c)
6080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6081 server_handle = server;
6084 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6089 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6090 "mesh", "peer/privatekey");
6091 GNUNET_SCHEDULER_shutdown ();
6096 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6097 &refresh_path_time))
6099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6101 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6102 "mesh", "refresh path time");
6103 GNUNET_SCHEDULER_shutdown ();
6108 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6109 &app_announce_time))
6111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6113 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6114 "mesh", "app announce time");
6115 GNUNET_SCHEDULER_shutdown ();
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6119 "APP_ANNOUNCE_TIME %llu ms\n",
6120 app_announce_time.rel_value);
6122 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6127 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6128 "mesh", "id announce time");
6129 GNUNET_SCHEDULER_shutdown ();
6134 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6139 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6140 "mesh", "connect timeout");
6141 GNUNET_SCHEDULER_shutdown ();
6146 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6151 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6152 "mesh", "max msgs queue");
6153 GNUNET_SCHEDULER_shutdown ();
6158 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6163 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6164 "mesh", "max tunnels");
6165 GNUNET_SCHEDULER_shutdown ();
6170 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6173 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6175 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6176 "mesh", "default ttl", 64);
6181 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6184 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6185 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6186 "mesh", "max peers", 1000);
6191 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6192 &dht_replication_level))
6194 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6196 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6197 "mesh", "dht replication level", 3);
6198 dht_replication_level = 3;
6201 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6202 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6203 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6204 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6206 dht_handle = GNUNET_DHT_connect (c, 64);
6207 if (NULL == dht_handle)
6211 stats = GNUNET_STATISTICS_create ("mesh", c);
6213 GNUNET_SERVER_suspend (server_handle);
6214 /* Scheduled the task to clean up when shutdown is called */
6215 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6217 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
6220 GNUNET_free (keyfile);
6225 * The main function for the mesh service.
6227 * @param argc number of arguments from the command line
6228 * @param argv command line arguments
6229 * @return 0 ok, 1 on error
6232 main (int argc, char *const *argv)
6237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6238 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6240 ret = (GNUNET_OK == r) ? 0 : 1;
6241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
6245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6246 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
6247 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);