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 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
423 * Contains the Flow Control info: FWD ACK value received,
424 * last BCK ACK sent, PID and SKIP values.
426 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
429 * Last ACK sent towards the origin (for traffic towards leaf node).
431 uint32_t last_fwd_ack;
434 * BCK ACK value received from the hop towards the owner of the tunnel,
435 * (previous node / owner): up to what message PID can we sent back to him.
440 * How many messages are in the forward queue (towards leaves).
442 unsigned int fwd_queue_n;
445 * How many messages do we accept in the forward queue.
447 unsigned int fwd_queue_max;
450 * How many messages are in the backward queue (towards origin).
452 unsigned int bck_queue_n;
455 * How many messages do we accept in the backward queue.
457 unsigned int bck_queue_max;
460 * Task to poll peer in case of a stall.
462 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
465 * Last time the tunnel was used
467 struct GNUNET_TIME_Absolute timestamp;
470 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
471 * containing peers added by id or by type, not intermediate peers.
473 struct GNUNET_PEER_Id peer;
476 * Client owner of the tunnel, if any
478 struct MeshClient *owner;
481 * Clients that have been informed about and want to stay in the tunnel.
483 struct MeshClient **clients;
486 * Flow control info for each client.
488 struct MeshTunnelClientInfo *clients_fc;
491 * Number of elements in clients/clients_fc
493 unsigned int nclients;
496 * Clients that have been informed but requested to leave the tunnel.
498 struct MeshClient **ignore;
501 * Number of elements in clients
503 unsigned int nignore;
506 * Bloomfilter (for peer identities) to stop circular routes
508 char bloomfilter[MESH_BLOOM_SIZE];
514 struct MeshTunnelTree *tree;
517 * Task to keep the used paths alive
519 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
522 * Task to destroy the tunnel after timeout
524 * FIXME: merge the two? a tunnel will have either
525 * a path refresh OR a timeout, never both!
527 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
530 * Flag to signal the destruction of the tunnel.
531 * If this is set GNUNET_YES the tunnel will be destroyed
532 * when the queue is empty.
537 * Total messages pending for this tunnels, payload or not.
539 unsigned int pending_messages;
542 * If the tunnel is empty, destoy it.
544 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
549 * Info about a child node in a tunnel, needed to perform flow control.
551 struct MeshTunnelChildInfo
554 * ID of the child node.
574 * Maximum PID allowed (FWD ACK received).
579 * Last ACK sent to that child (BCK ACK).
584 * Circular buffer pointing to MeshPeerQueue elements for all
585 * payload traffic going to this child.
586 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
588 struct MeshPeerQueue **send_buffer;
591 * Index of the oldest element in the send_buffer.
593 unsigned int send_buffer_start;
596 * How many elements are already in the buffer.
598 unsigned int send_buffer_n;
601 * Tunnel this info is about
603 struct MeshTunnel *t;
606 * Task to poll peer in case of a stall.
608 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
611 * Time to use for next polling call.
613 struct GNUNET_TIME_Relative fc_poll_time;
618 * Info about a leaf client of a tunnel, needed to perform flow control.
620 struct MeshTunnelClientInfo
623 * PID of the last packet sent to the client (FWD).
628 * PID of the last packet received from the client (BCK).
633 * Maximum PID allowed (FWD ACK received).
638 * Last ACK sent to that child (BCK ACK).
646 * Info collected during iteration of child nodes in order to get the ACK value
649 struct MeshTunnelChildIteratorContext
652 * Tunnel whose info is being collected.
654 struct MeshTunnel *t;
657 * Is this context initialized? Is the value in max_child_ack valid?
662 * Maximum child ACK so far.
664 uint32_t max_child_ack;
667 * Number of children nodes
669 unsigned int nchildren;
674 * Info needed to work with tunnel paths and peers
681 struct MeshTunnel *t;
684 * Neighbouring peer to whom we send the packet to
686 struct MeshPeerInfo *peer;
691 struct MeshPeerPath *path;
696 * Struct containing information about a client of the service
703 struct MeshClient *next;
708 struct MeshClient *prev;
711 * Tunnels that belong to this client, indexed by local id
713 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
716 * Tunnels this client has accepted, indexed by incoming local id
718 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
721 * Tunnels this client has rejected, indexed by incoming local id
723 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
725 * Handle to communicate with the client
727 struct GNUNET_SERVER_Client *handle;
730 * Messages that this client has declared interest in
732 struct GNUNET_CONTAINER_MultiHashMap *types;
735 * Whether the client is active or shutting down (don't send confirmations
736 * to a client that is shutting down.
741 * ID of the client, mainly for debug messages
746 * Regular expressions describing the services offered by this client.
748 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
751 * Number of regular expressions in regexes.
753 unsigned int n_regex;
756 * Task to refresh all regular expresions in the DHT.
758 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
761 * Tmp store for partially retrieved regex.
768 /******************************************************************************/
769 /************************ DEBUG FUNCTIONS ****************************/
770 /******************************************************************************/
774 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
775 * @param cls string to print
776 * @param success GNUNET_OK if the PUT was transmitted,
777 * GNUNET_NO on timeout,
778 * GNUNET_SYSERR on disconnect from service
779 * after the PUT message was transmitted
780 * (so we don't know if it was received or not)
785 mesh_debug (void *cls, int success)
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
793 unsigned int debug_fwd_ack;
794 unsigned int debug_bck_ack;
798 /******************************************************************************/
799 /*********************** GLOBAL VARIABLES ****************************/
800 /******************************************************************************/
802 /************************** Configuration parameters **************************/
805 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
807 static struct GNUNET_TIME_Relative refresh_path_time;
810 * How often to PUT local application numbers in the DHT.
812 static struct GNUNET_TIME_Relative app_announce_time;
815 * How often to PUT own ID in the DHT.
817 static struct GNUNET_TIME_Relative id_announce_time;
820 * Maximum time allowed to connect to a peer found by string.
822 static struct GNUNET_TIME_Relative connect_timeout;
825 * Default TTL for payload packets.
827 static unsigned long long default_ttl;
830 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
832 static unsigned long long dht_replication_level;
835 * How many tunnels are we willing to maintain.
836 * Local tunnels are always allowed, even if there are more tunnels than max.
838 static unsigned long long max_tunnels;
841 * How many messages *in total* are we willing to queue, divided by number of
842 * tunnels to get tunnel queue size.
844 static unsigned long long max_msgs_queue;
847 * How many peers do we want to remember?
849 static unsigned long long max_peers;
852 /*************************** Static global variables **************************/
855 * Hostkey generation context
857 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
860 * DLL with all the clients, head.
862 static struct MeshClient *clients;
865 * DLL with all the clients, tail.
867 static struct MeshClient *clients_tail;
870 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
872 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
875 * Number of tunnels known.
877 static unsigned long long n_tunnels;
880 * Tunnels incoming, indexed by MESH_TunnelNumber
881 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
883 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
886 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
888 static struct GNUNET_CONTAINER_MultiHashMap *peers;
891 * Handle to communicate with transport
893 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
896 * Handle to communicate with core.
898 static struct GNUNET_CORE_Handle *core_handle;
903 static struct GNUNET_DHT_Handle *dht_handle;
908 static struct GNUNET_SERVER_Handle *server_handle;
911 * Handle to the statistics service.
913 static struct GNUNET_STATISTICS_Handle *stats;
916 * Notification context, to send messages to local clients.
918 static struct GNUNET_SERVER_NotificationContext *nc;
921 * Local peer own ID (memory efficient handle).
923 static GNUNET_PEER_Id myid;
926 * Local peer own ID (full value).
928 static struct GNUNET_PeerIdentity my_full_id;
933 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
938 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
941 * Tunnel ID for the next created tunnel (global tunnel number).
943 static MESH_TunnelNumber next_tid;
946 * Tunnel ID for the next incoming tunnel (local tunnel number).
948 static MESH_TunnelNumber next_local_tid;
951 * All message types clients of this peer are interested in.
953 static struct GNUNET_CONTAINER_MultiHashMap *types;
956 * Task to periodically announce itself in the network.
958 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
961 * Next ID to assign to a client.
963 unsigned int next_client_id;
966 /******************************************************************************/
967 /*********************** DECLARATIONS **************************/
968 /******************************************************************************/
971 * Function to process paths received for a new peer addition. The recorded
972 * paths form the initial tunnel, which can be optimized later.
973 * Called on each result obtained for the DHT search.
976 * @param exp when will this value expire
977 * @param key key of the result
978 * @param type type of the result
979 * @param size number of bytes in data
980 * @param data pointer to the result data
983 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
984 const struct GNUNET_HashCode * key,
985 const struct GNUNET_PeerIdentity *get_path,
986 unsigned int get_path_length,
987 const struct GNUNET_PeerIdentity *put_path,
988 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
989 size_t size, const void *data);
993 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
994 * and insert it in the appropiate structures if the peer is not known yet.
996 * @param peer Full identity of the peer.
998 * @return Existing or newly created peer info.
1000 static struct MeshPeerInfo *
1001 peer_info_get (const struct GNUNET_PeerIdentity *peer);
1005 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1006 * and insert it in the appropiate structures if the peer is not known yet.
1008 * @param peer Short identity of the peer.
1010 * @return Existing or newly created peer info.
1012 static struct MeshPeerInfo *
1013 peer_info_get_short (const GNUNET_PEER_Id peer);
1017 * Try to establish a new connection to this peer.
1018 * Use the best path for the given tunnel.
1019 * If the peer doesn't have any path to it yet, try to get one.
1020 * If the peer already has some path, send a CREATE PATH towards it.
1022 * @param peer PeerInfo of the peer.
1023 * @param t Tunnel for which to create the path, if possible.
1026 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1030 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1031 * to obtain a local peer -> destination path and interning the peer ids.
1033 * @return Newly allocated and created path
1035 static struct MeshPeerPath *
1036 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1037 unsigned int get_path_length,
1038 const struct GNUNET_PeerIdentity *put_path,
1039 unsigned int put_path_length);
1043 * Adds a path to the peer_infos of all the peers in the path
1045 * @param p Path to process.
1046 * @param confirmed Whether we know if the path works or not.
1049 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1053 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1054 * needed rescources.
1055 * If peer already exists, reevaluate shortest path and change if different.
1057 * @param t Tunnel we want to add a new peer to
1058 * @param peer PeerInfo of the peer being added
1062 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1066 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1067 * that are no longer needed, as well as nodes of no longer reachable peers.
1068 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1070 * @param t Tunnel from which to remove the path.
1071 * @param peer Short id of the peer which should be removed.
1074 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1078 * Search for a tunnel by global ID using full PeerIdentities.
1080 * @param oid owner of the tunnel.
1081 * @param tid global tunnel number.
1083 * @return tunnel handler, NULL if doesn't exist.
1085 static struct MeshTunnel *
1086 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1090 * Delete an active client from the tunnel.
1096 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1099 * Notify a tunnel that a connection has broken that affects at least
1100 * some of its peers.
1102 * @param t Tunnel affected.
1103 * @param p1 Peer that got disconnected from p2.
1104 * @param p2 Peer that got disconnected from p1.
1106 * @return Short ID of the peer disconnected (either p1 or p2).
1107 * 0 if the tunnel remained unaffected.
1109 static GNUNET_PEER_Id
1110 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1115 * Get the current ack value for a tunnel, for data going from root to leaves,
1116 * taking in account the tunnel mode and the status of all children and clients.
1120 * @return Maximum PID allowed.
1123 tunnel_get_fwd_ack (struct MeshTunnel *t);
1127 * Add a client to a tunnel, initializing all needed data structures.
1129 * @param t Tunnel to which add the client.
1130 * @param c Client which to add to the tunnel.
1133 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1137 * @brief Queue and pass message to core when possible.
1139 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1140 * and accounts for it. In case the queue is full, the message is dropped and
1143 * Otherwise, message is treated as internal and allowed to go regardless of
1146 * @param cls Closure (@c type dependant). It will be used by queue_send to
1147 * build the message to be sent if not already prebuilt.
1148 * @param type Type of the message, 0 for a raw message.
1149 * @param size Size of the message.
1150 * @param dst Neighbor to send message to.
1151 * @param t Tunnel this message belongs to.
1154 queue_add (void *cls, uint16_t type, size_t size,
1155 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1159 * Free a transmission that was already queued with all resources
1160 * associated to the request.
1162 * @param queue Queue handler to cancel.
1163 * @param clear_cls Is it necessary to free associated cls?
1166 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1170 * @brief Get the next transmittable message from the queue.
1172 * This will be the head, except in the case of being a data packet
1173 * not allowed by the destination peer.
1175 * @param peer Destination peer.
1177 * @return The next viable MeshPeerQueue element to send to that peer.
1178 * NULL when there are no transmittable messages.
1180 struct MeshPeerQueue *
1181 queue_get_next (const struct MeshPeerInfo *peer);
1185 * Core callback to write a queued packet to core buffer
1187 * @param cls Closure (peer info).
1188 * @param size Number of bytes available in buf.
1189 * @param buf Where the to write the message.
1191 * @return number of bytes written to buf
1194 queue_send (void *cls, size_t size, void *buf);
1197 /******************************************************************************/
1198 /************************ PERIODIC FUNCTIONS ****************************/
1199 /******************************************************************************/
1202 * Periodically announce self id in the DHT
1204 * @param cls closure
1205 * @param tc task context
1208 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1210 struct PBlock block;
1212 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1214 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1218 * - Set data expiration in function of X
1219 * - Adapt X to churn
1221 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1223 block.id = my_full_id;
1224 block.type = htonl (0);
1225 GNUNET_DHT_put (dht_handle, /* DHT handle */
1226 &my_full_id.hashPubKey, /* Key to use */
1227 dht_replication_level, /* Replication level */
1228 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1229 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1230 sizeof (block), /* Size of the data */
1231 (const char *) &block, /* Data itself */
1232 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1233 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1234 NULL, /* Continuation */
1235 NULL); /* Continuation closure */
1237 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1241 /******************************************************************************/
1242 /****************** GENERAL HELPER FUNCTIONS ************************/
1243 /******************************************************************************/
1246 * Decrements the reference counter and frees all resources if needed
1248 * @param mesh_data Data Descriptor used in a multicast message.
1249 * Freed no longer needed (last message).
1252 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1254 if (0 == --(mesh_data->reference_counter))
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1257 GNUNET_free (mesh_data->data);
1258 GNUNET_free (mesh_data);
1264 * Check if client has registered with the service and has not disconnected
1266 * @param client the client to check
1268 * @return non-NULL if client exists in the global DLL
1270 static struct MeshClient *
1271 client_get (struct GNUNET_SERVER_Client *client)
1273 struct MeshClient *c;
1278 if (c->handle == client)
1287 * Checks if a given client has subscribed to certain message type
1289 * @param message_type Type of message to check
1290 * @param c Client to check
1292 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1294 * FIXME: use of crypto_hash slows it down
1295 * The hash function alone takes 8-10us out of the ~55us for the whole
1296 * process of retransmitting the message from one local client to another.
1297 * Find faster implementation!
1300 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1302 struct GNUNET_HashCode hc;
1304 if (NULL == c->types)
1307 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1308 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1313 * Check whether client wants traffic from a tunnel.
1315 * @param c Client to check.
1316 * @param t Tunnel to be found.
1318 * @return GNUNET_YES if client knows tunnel.
1320 * TODO look in client hashmap
1323 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1327 for (i = 0; i < t->nclients; i++)
1328 if (t->clients[i] == c)
1335 * Check whether client has been informed about a tunnel.
1337 * @param c Client to check.
1338 * @param t Tunnel to be found.
1340 * @return GNUNET_YES if client knows tunnel.
1342 * TODO look in client hashmap
1345 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1349 for (i = 0; i < t->nignore; i++)
1350 if (t->ignore[i] == c)
1352 return client_wants_tunnel(c, t);
1357 * Marks a client as uninterested in traffic from the tunnel, updating both
1358 * client and tunnel to reflect this.
1360 * @param c Client that doesn't want traffic anymore.
1361 * @param t Tunnel which should be ignored.
1363 * FIXME when to delete an incoming tunnel?
1366 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1368 struct GNUNET_HashCode hash;
1370 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1371 GNUNET_break (GNUNET_YES ==
1372 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1374 GNUNET_break (GNUNET_YES ==
1375 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1377 tunnel_delete_active_client (t, c);
1378 GNUNET_array_append (t->ignore, t->nignore, c);
1383 * Deletes a tunnel from a client (either owner or destination). To be used on
1384 * tunnel destroy, otherwise, use client_ignore_tunnel.
1386 * @param c Client whose tunnel to delete.
1387 * @param t Tunnel which should be deleted.
1390 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1392 struct GNUNET_HashCode hash;
1396 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1397 GNUNET_assert (GNUNET_YES ==
1398 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1404 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1406 GNUNET_assert (GNUNET_YES ==
1407 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1411 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1419 * Notify the owner of a tunnel that a peer has disconnected.
1421 * @param c Client (owner of tunnel).
1422 * @param t Tunnel this message is about.
1423 * @param peer_id Short ID of the disconnected peer.
1426 client_notify_peer_disconnected (struct MeshClient *c,
1427 struct MeshTunnel *t,
1428 GNUNET_PEER_Id peer_id)
1430 struct GNUNET_MESH_PeerControl msg;
1432 if (NULL == t->owner || NULL == nc)
1435 msg.header.size = htons (sizeof (msg));
1436 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1437 msg.tunnel_id = htonl (t->local_tid);
1438 GNUNET_PEER_resolve (peer_id, &msg.peer);
1439 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1440 &msg.header, GNUNET_NO);
1445 * Send the message to all clients that have subscribed to its type
1447 * @param msg Pointer to the message itself
1448 * @param payload Pointer to the payload of the message.
1449 * @param t The tunnel to whose clients this message goes.
1451 * @return number of clients this message was sent to
1454 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1455 const struct GNUNET_MessageHeader *payload,
1456 struct MeshTunnel *t)
1458 struct MeshClient *c;
1459 MESH_TunnelNumber *tid;
1462 char cbuf[htons (msg->size)];
1464 type = ntohs (payload->type);
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1467 GNUNET_MESH_DEBUG_M2S (type));
1469 memcpy (cbuf, msg, sizeof (cbuf));
1470 switch (htons (msg->type))
1472 struct GNUNET_MESH_Unicast *uc;
1473 struct GNUNET_MESH_Multicast *mc;
1474 struct GNUNET_MESH_ToOrigin *to;
1476 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1477 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1480 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1481 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1484 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1485 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1493 for (count = 0, c = clients; c != NULL; c = c->next)
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1496 if (client_is_subscribed (type, c))
1498 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1502 *tid = htonl (t->local_tid);
1506 if (GNUNET_NO == client_knows_tunnel (c, t))
1508 /* This client doesn't know the tunnel */
1509 struct GNUNET_MESH_TunnelNotification tmsg;
1510 struct GNUNET_HashCode hash;
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1513 tmsg.header.size = htons (sizeof (tmsg));
1514 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1515 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1516 tmsg.tunnel_id = htonl (t->local_tid_dest);
1518 if (GNUNET_YES == t->speed_min)
1519 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1520 if (GNUNET_YES == t->nobuffer)
1521 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1522 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1523 &tmsg.header, GNUNET_NO);
1524 tunnel_add_client (t, c);
1525 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1527 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1528 c->incoming_tunnels, &hash, t,
1529 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1531 *tid = htonl (t->local_tid_dest);
1534 /* Check if the client wants to get traffic from the tunnel */
1535 if (GNUNET_NO == client_wants_tunnel(c, t))
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1539 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1540 (struct GNUNET_MessageHeader
1541 *) cbuf, GNUNET_NO);
1550 * Notify the client that owns the tunnel that a peer has connected to it
1551 * (the requested path to it has been confirmed).
1553 * @param t Tunnel whose owner to notify
1554 * @param id Short id of the peer that has connected
1557 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1559 struct GNUNET_MESH_PeerControl pc;
1561 if (NULL == t->owner || GNUNET_YES == t->destroy)
1564 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1565 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1566 pc.tunnel_id = htonl (t->local_tid);
1567 GNUNET_PEER_resolve (id, &pc.peer);
1568 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1574 * Notify all clients (not depending on registration status) that the incoming
1575 * tunnel is no longer valid.
1577 * @param t Tunnel that was destroyed.
1580 send_clients_tunnel_destroy (struct MeshTunnel *t)
1582 struct GNUNET_MESH_TunnelMessage msg;
1584 msg.header.size = htons (sizeof (msg));
1585 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1586 msg.tunnel_id = htonl (t->local_tid_dest);
1587 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1592 * Notify clients of tunnel disconnections, if needed.
1593 * In case the origin disconnects, the destination clients get a tunnel destroy
1594 * notification. If the last destination disconnects (only one remaining client
1595 * in tunnel), the origin gets a (local ID) peer disconnected.
1596 * Note that the function must be called BEFORE removing the client from
1599 * @param t Tunnel that was destroyed.
1600 * @param c Client that disconnected.
1603 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1609 struct GNUNET_MESH_TunnelMessage msg;
1611 msg.header.size = htons (sizeof (msg));
1612 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1613 msg.tunnel_id = htonl (t->local_tid_dest);
1614 for (i = 0; i < t->nclients; i++)
1615 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1616 &msg.header, GNUNET_NO);
1618 // FIXME when to disconnect an incoming tunnel?
1619 else if (1 == t->nclients && NULL != t->owner)
1621 struct GNUNET_MESH_PeerControl msg;
1623 msg.header.size = htons (sizeof (msg));
1624 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1625 msg.tunnel_id = htonl (t->local_tid);
1626 msg.peer = my_full_id;
1627 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1628 &msg.header, GNUNET_NO);
1634 * Iterator over all the peers to remove the oldest not-used entry.
1636 * @param cls Closure (unsued).
1637 * @param key ID of the peer.
1638 * @param value Peer_Info of the peer.
1643 peer_info_timeout (void *cls,
1644 const struct GNUNET_HashCode *key,
1651 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1652 * and insert it in the appropiate structures if the peer is not known yet.
1654 * @param peer Full identity of the peer.
1656 * @return Existing or newly created peer info.
1658 static struct MeshPeerInfo *
1659 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1661 struct MeshPeerInfo *peer_info;
1663 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1664 if (NULL == peer_info)
1667 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1668 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1670 GNUNET_CONTAINER_multihashmap_iterate (peers,
1674 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1676 peer_info->id = GNUNET_PEER_intern (peer);
1678 peer_info->last_contact = GNUNET_TIME_absolute_get();
1685 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1686 * and insert it in the appropiate structures if the peer is not known yet.
1688 * @param peer Short identity of the peer.
1690 * @return Existing or newly created peer info.
1692 static struct MeshPeerInfo *
1693 peer_info_get_short (const GNUNET_PEER_Id peer)
1695 struct GNUNET_PeerIdentity id;
1697 GNUNET_PEER_resolve (peer, &id);
1698 return peer_info_get (&id);
1703 * Iterator to remove the tunnel from the list of tunnels a peer participates
1706 * @param cls Closure (tunnel info)
1707 * @param key GNUNET_PeerIdentity of the peer (unused)
1708 * @param value PeerInfo of the peer
1710 * @return always GNUNET_YES, to keep iterating
1713 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1715 struct MeshTunnel *t = cls;
1716 struct MeshPeerInfo *peer = value;
1719 for (i = 0; i < peer->ntunnels; i++)
1722 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1725 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1727 GNUNET_realloc (peer->tunnels,
1728 peer->ntunnels * sizeof(struct MeshTunnel *));
1737 * Core callback to write a pre-constructed data packet to core buffer
1739 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1740 * @param size Number of bytes available in buf.
1741 * @param buf Where the to write the message.
1743 * @return number of bytes written to buf
1746 send_core_data_raw (void *cls, size_t size, void *buf)
1748 struct MeshTransmissionDescriptor *info = cls;
1749 struct GNUNET_MessageHeader *msg;
1752 GNUNET_assert (NULL != info);
1753 GNUNET_assert (NULL != info->mesh_data);
1754 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1755 total_size = ntohs (msg->size);
1757 if (total_size > size)
1762 memcpy (buf, msg, total_size);
1763 data_descriptor_decrement_rc (info->mesh_data);
1770 * Sends an already built non-multicast message to a peer,
1771 * properly registrating all used resources.
1773 * @param message Message to send. Function makes a copy of it.
1774 * @param peer Short ID of the neighbor whom to send the message.
1775 * @param t Tunnel on which this message is transmitted.
1778 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1779 const struct GNUNET_PeerIdentity *peer,
1780 struct MeshTunnel *t)
1782 struct MeshTransmissionDescriptor *info;
1783 struct MeshPeerInfo *neighbor;
1784 struct MeshPeerPath *p;
1788 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1790 size = ntohs (message->size);
1791 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1792 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1793 info->mesh_data->data = GNUNET_malloc (size);
1794 memcpy (info->mesh_data->data, message, size);
1795 type = ntohs(message->type);
1798 struct GNUNET_MESH_Unicast *m;
1799 struct GNUNET_MESH_ToOrigin *to;
1801 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1802 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1803 m->ttl = htonl (ntohl (m->ttl) - 1);
1805 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1806 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1808 to->pid = htonl(t->bck_pid);
1810 info->mesh_data->data_len = size;
1811 info->mesh_data->reference_counter = 1;
1812 info->mesh_data->total_out = 1;
1813 neighbor = peer_info_get (peer);
1814 for (p = neighbor->path_head; NULL != p; p = p->next)
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 " %s IS NOT DIRECTLY CONNECTED\n",
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1830 for (p = neighbor->path_head; NULL != p; p = p->next)
1832 struct GNUNET_PeerIdentity debug_id;
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 " path with %u hops through:\n",
1838 for (i = 0; i < p->length; i++)
1840 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 i, GNUNET_i2s(&debug_id));
1847 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1849 " no direct connection to %s\n",
1851 GNUNET_free (info->mesh_data->data);
1852 GNUNET_free (info->mesh_data);
1856 info->peer = neighbor;
1857 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1868 * Sends a CREATE PATH message for a path to a peer, properly registrating
1869 * all used resources.
1871 * @param peer PeerInfo of the final peer for whom this path is being created.
1872 * @param p Path itself.
1873 * @param t Tunnel for which the path is created.
1876 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1877 struct MeshTunnel *t)
1879 struct GNUNET_PeerIdentity id;
1880 struct MeshPathInfo *path_info;
1881 struct MeshPeerInfo *neighbor;
1887 p = tree_get_path_to_peer (t->tree, peer->id);
1894 for (i = 0; i < p->length; i++)
1896 if (p->peers[i] == myid)
1899 if (i >= p->length - 1)
1905 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1907 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1908 path_info->path = p;
1910 neighbor = peer_info_get (&id);
1911 path_info->peer = neighbor;
1912 queue_add (path_info,
1913 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1914 sizeof (struct GNUNET_MESH_ManipulatePath) +
1915 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1922 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1924 * @param t Tunnel whose path to destroy.
1925 * @param destination Short ID of the peer to whom the path to destroy.
1928 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1930 struct MeshPeerPath *p;
1933 p = tree_get_path_to_peer (t->tree, destination);
1939 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1940 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1942 struct GNUNET_MESH_ManipulatePath *msg;
1943 struct GNUNET_PeerIdentity *pi;
1947 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1948 msg->header.size = htons (size);
1949 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1950 msg->tid = htonl (t->id.tid);
1951 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1952 for (i = 0; i < p->length; i++)
1954 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1956 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1963 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1965 * @param t Tunnel which to confirm.
1968 send_path_ack (struct MeshTunnel *t)
1970 struct MeshTransmissionDescriptor *info;
1971 struct GNUNET_PeerIdentity id;
1972 GNUNET_PEER_Id peer;
1974 peer = tree_get_predecessor (t->tree);
1975 GNUNET_PEER_resolve (peer, &id);
1976 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1977 info->origin = &t->id;
1978 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1979 GNUNET_assert (NULL != info->peer);
1982 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1983 sizeof (struct GNUNET_MESH_PathACK),
1990 * Try to establish a new connection to this peer.
1991 * Use the best path for the given tunnel.
1992 * If the peer doesn't have any path to it yet, try to get one.
1993 * If the peer already has some path, send a CREATE PATH towards it.
1995 * @param peer PeerInfo of the peer.
1996 * @param t Tunnel for which to create the path, if possible.
1999 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2001 struct MeshPeerPath *p;
2002 struct MeshPathInfo *path_info;
2004 if (NULL != peer->path_head)
2006 p = tree_get_path_to_peer (t->tree, peer->id);
2013 // FIXME always send create path to self
2016 send_create_path (peer, p, t);
2020 struct GNUNET_HashCode hash;
2023 send_client_peer_connected (t, myid);
2024 t->local_tid_dest = next_local_tid++;
2025 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2028 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2036 else if (NULL == peer->dhtget)
2038 struct GNUNET_PeerIdentity id;
2040 GNUNET_PEER_resolve (peer->id, &id);
2041 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2042 path_info->peer = peer;
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2045 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2046 peer->dhtgetcls = path_info;
2047 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2048 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2049 &id.hashPubKey, /* key to search */
2050 dht_replication_level, /* replication level */
2051 GNUNET_DHT_RO_RECORD_ROUTE |
2052 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2053 NULL, /* xquery */ // FIXME BLOOMFILTER
2054 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2055 &dht_get_id_handler, path_info);
2057 /* Otherwise, there is no path but the DHT get is already started. */
2062 * Task to delay the connection of a peer
2064 * @param cls Closure (path info with tunnel and peer to connect).
2065 * Will be free'd on exection.
2066 * @param tc TaskContext
2069 peer_info_connect_task (void *cls,
2070 const struct GNUNET_SCHEDULER_TaskContext *tc)
2072 struct MeshPathInfo *path_info = cls;
2074 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2076 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2081 peer_info_connect (path_info->peer, path_info->t);
2087 * Destroy the peer_info and free any allocated resources linked to it
2089 * @param pi The peer_info to destroy.
2091 * @return GNUNET_OK on success
2094 peer_info_destroy (struct MeshPeerInfo *pi)
2096 struct GNUNET_PeerIdentity id;
2097 struct MeshPeerPath *p;
2098 struct MeshPeerPath *nextp;
2100 GNUNET_PEER_resolve (pi->id, &id);
2101 GNUNET_PEER_change_rc (pi->id, -1);
2104 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2107 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2108 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2110 if (NULL != pi->dhtget)
2112 GNUNET_DHT_get_stop (pi->dhtget);
2113 GNUNET_free (pi->dhtgetcls);
2119 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2123 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2125 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2133 * Remove all paths that rely on a direct connection between p1 and p2
2134 * from the peer itself and notify all tunnels about it.
2136 * @param peer PeerInfo of affected peer.
2137 * @param p1 GNUNET_PEER_Id of one peer.
2138 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2141 * TODO: optimize (see below)
2144 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2147 struct MeshPeerPath *p;
2148 struct MeshPeerPath *aux;
2149 struct MeshPeerInfo *peer_d;
2151 unsigned int destroyed;
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2158 p = peer->path_head;
2162 for (i = 0; i < (p->length - 1); i++)
2164 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2165 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2167 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2178 for (i = 0; i < peer->ntunnels; i++)
2180 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2184 * Problem: one or more peers have been deleted from the tunnel tree.
2185 * We don't know who they are to try to add them again.
2186 * We need to try to find a new path for each of the disconnected peers.
2187 * Some of them might already have a path to reach them that does not
2188 * involve p1 and p2. Adding all anew might render in a better tree than
2189 * the trivial immediate fix.
2191 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2192 * its children will be reachable trough him.
2194 peer_d = peer_info_get_short (d);
2197 for (p = peer_d->path_head; NULL != p; p = p->next)
2199 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2207 /* No callback, as peer will be already disconnected and a connection
2208 * scheduled by tunnel_notify_connection_broken.
2210 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2214 peer_info_connect (peer_d, peer->tunnels[i]);
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2222 * Add the path to the peer and update the path used to reach it in case this
2225 * @param peer_info Destination peer to add the path to.
2226 * @param path New path to add. Last peer must be the peer in arg 1.
2227 * Path will be either used of freed if already known.
2228 * @param trusted Do we trust that this path is real?
2231 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2234 struct MeshPeerPath *aux;
2238 if ((NULL == peer_info) || (NULL == path))
2241 path_destroy (path);
2244 if (path->peers[path->length - 1] != peer_info->id)
2247 path_destroy (path);
2250 if (path->length <= 2 && GNUNET_NO == trusted)
2252 /* Only allow CORE to tell us about direct paths */
2253 path_destroy (path);
2256 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2257 for (l = 1; l < path->length; l++)
2259 if (path->peers[l] == myid)
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2262 for (l2 = 0; l2 < path->length - l; l2++)
2264 path->peers[l2] = path->peers[l + l2];
2269 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2274 struct GNUNET_PeerIdentity id;
2276 GNUNET_PEER_resolve (peer_info->id, &id);
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2278 path->length, GNUNET_i2s (&id));
2281 l = path_get_length (path);
2288 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2289 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2291 l2 = path_get_length (aux);
2294 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2295 peer_info->path_tail, aux, path);
2300 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2302 path_destroy (path);
2307 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2314 * Add the path to the origin peer and update the path used to reach it in case
2315 * this is the shortest.
2316 * The path is given in peer_info -> destination, therefore we turn the path
2317 * upside down first.
2319 * @param peer_info Peer to add the path to, being the origin of the path.
2320 * @param path New path to add after being inversed.
2321 * @param trusted Do we trust that this path is real?
2324 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2325 struct MeshPeerPath *path, int trusted)
2328 peer_info_add_path (peer_info, path, trusted);
2333 * Function called if the connection to the peer has been stalled for a while,
2334 * possibly due to a missed ACK. Poll the peer about its ACK status.
2336 * @param cls Closure (cinfo).
2337 * @param tc TaskContext.
2340 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2342 struct MeshTunnelChildInfo *cinfo = cls;
2343 struct GNUNET_MESH_Poll msg;
2344 struct GNUNET_PeerIdentity id;
2345 struct MeshTunnel *t;
2347 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2348 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2354 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2355 msg.header.size = htons (sizeof (msg));
2356 msg.tid = htonl (t->id.tid);
2357 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2358 msg.last_ack = htonl (cinfo->fwd_ack);
2360 GNUNET_PEER_resolve (cinfo->id, &id);
2361 send_prebuilt_message (&msg.header, &id, cinfo->t);
2362 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2364 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2365 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2366 &tunnel_poll, cinfo);
2371 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2372 * to obtain a local peer -> destination path and interning the peer ids.
2374 * @return Newly allocated and created path
2376 static struct MeshPeerPath *
2377 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2378 unsigned int get_path_length,
2379 const struct GNUNET_PeerIdentity *put_path,
2380 unsigned int put_path_length)
2382 struct MeshPeerPath *p;
2388 GNUNET_PEER_change_rc (myid, 1);
2389 i = get_path_length;
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2391 for (i--; i >= 0; i--)
2393 id = GNUNET_PEER_intern (&get_path[i]);
2394 if (p->length > 0 && id == p->peers[p->length - 1])
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2397 GNUNET_PEER_change_rc (id, -1);
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2402 GNUNET_i2s (&get_path[i]));
2404 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2405 p->peers[p->length - 1] = id;
2408 i = put_path_length;
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2410 for (i--; i >= 0; i--)
2412 id = GNUNET_PEER_intern (&put_path[i]);
2415 /* PUT path went through us, so discard the path up until now and start
2416 * from here to get a much shorter (and loop-free) path.
2421 if (p->length > 0 && id == p->peers[p->length - 1])
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2424 GNUNET_PEER_change_rc (id, -1);
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2429 GNUNET_i2s (&put_path[i]));
2431 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2432 p->peers[p->length - 1] = id;
2436 if (get_path_length > 0)
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2438 GNUNET_i2s (&get_path[0]));
2439 if (put_path_length > 0)
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2441 GNUNET_i2s (&put_path[0]));
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2444 for (i = 0; i < p->length; i++)
2446 struct GNUNET_PeerIdentity peer_id;
2448 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2450 GNUNET_i2s (&peer_id));
2458 * Adds a path to the peer_infos of all the peers in the path
2460 * @param p Path to process.
2461 * @param confirmed Whether we know if the path works or not.
2464 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2468 /* TODO: invert and add */
2469 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2470 for (i++; i < p->length; i++)
2472 struct MeshPeerInfo *aux;
2473 struct MeshPeerPath *copy;
2475 aux = peer_info_get_short (p->peers[i]);
2476 copy = path_duplicate (p);
2477 copy->length = i + 1;
2478 peer_info_add_path (aux, copy, GNUNET_NO);
2484 * Send keepalive packets for a peer
2486 * @param cls Closure (tunnel for which to send the keepalive).
2487 * @param tc Notification context.
2489 * TODO: implement explicit multicast keepalive?
2492 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2496 * Search for a tunnel among the incoming tunnels
2498 * @param tid the local id of the tunnel
2500 * @return tunnel handler, NULL if doesn't exist
2502 static struct MeshTunnel *
2503 tunnel_get_incoming (MESH_TunnelNumber tid)
2505 struct GNUNET_HashCode hash;
2507 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2508 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2509 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2514 * Search for a tunnel among the tunnels for a client
2516 * @param c the client whose tunnels to search in
2517 * @param tid the local id of the tunnel
2519 * @return tunnel handler, NULL if doesn't exist
2521 static struct MeshTunnel *
2522 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2524 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2526 return tunnel_get_incoming (tid);
2530 struct GNUNET_HashCode hash;
2532 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2533 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2539 * Search for a tunnel by global ID using PEER_ID
2541 * @param pi owner of the tunnel
2542 * @param tid global tunnel number
2544 * @return tunnel handler, NULL if doesn't exist
2546 static struct MeshTunnel *
2547 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2549 struct MESH_TunnelID id;
2550 struct GNUNET_HashCode hash;
2555 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2556 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2561 * Search for a tunnel by global ID using full PeerIdentities
2563 * @param oid owner of the tunnel
2564 * @param tid global tunnel number
2566 * @return tunnel handler, NULL if doesn't exist
2568 static struct MeshTunnel *
2569 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2571 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2576 * Delete an active client from the tunnel.
2582 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2586 for (i = 0; i < t->nclients; i++)
2588 if (t->clients[i] == c)
2590 t->clients[i] = t->clients[t->nclients - 1];
2591 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2592 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2594 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2602 * Delete an ignored client from the tunnel.
2608 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2612 for (i = 0; i < t->nignore; i++)
2614 if (t->ignore[i] == c)
2616 t->ignore[i] = t->ignore[t->nignore - 1];
2617 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2625 * Delete a client from the tunnel. It should be only done on
2626 * client disconnection, otherwise use client_ignore_tunnel.
2632 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2634 tunnel_delete_ignored_client (t, c);
2635 tunnel_delete_active_client (t, c);
2640 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2642 * Destroys queue elements of all waiting transmissions and frees all memory
2643 * used by the struct and its elements.
2645 * @param cls Closure (tunnel info).
2646 * @param key Hash of GNUNET_PEER_Id (unused).
2647 * @param value MeshTunnelChildInfo of the child.
2649 * @return always GNUNET_YES, to keep iterating
2652 tunnel_destroy_child (void *cls,
2653 const struct GNUNET_HashCode * key,
2656 struct MeshTunnelChildInfo *cinfo = value;
2657 struct MeshTunnel *t = cls;
2658 struct MeshPeerQueue *q;
2662 for (c = 0; c < cinfo->send_buffer_n; c++)
2664 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2665 q = cinfo->send_buffer[i];
2666 cinfo->send_buffer[i] = NULL;
2668 queue_destroy (q, GNUNET_YES);
2671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2673 GNUNET_free_non_null (cinfo->send_buffer);
2674 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2676 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2677 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2679 GNUNET_free (cinfo);
2685 * Callback used to notify a client owner of a tunnel that a peer has
2686 * disconnected, most likely because of a path change.
2688 * @param cls Closure (tunnel this notification is about).
2689 * @param peer_id Short ID of disconnected peer.
2692 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2694 struct MeshTunnel *t = cls;
2695 struct MeshPeerInfo *peer;
2696 struct MeshPathInfo *path_info;
2698 client_notify_peer_disconnected (t->owner, t, peer_id);
2700 peer = peer_info_get_short (peer_id);
2701 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2702 path_info->peer = peer;
2704 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
2710 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2711 * needed rescources.
2712 * If peer already exists, reevaluate shortest path and change if different.
2714 * @param t Tunnel we want to add a new peer to
2715 * @param peer PeerInfo of the peer being added
2719 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2721 struct GNUNET_PeerIdentity id;
2722 struct MeshPeerPath *best_p;
2723 struct MeshPeerPath *p;
2724 unsigned int best_cost;
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2728 GNUNET_PEER_resolve (peer->id, &id);
2730 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2732 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2733 GNUNET_assert (GNUNET_OK ==
2734 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2736 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2739 if (NULL != (p = peer->path_head))
2742 best_cost = tree_get_path_cost (t->tree, p);
2745 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2752 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
2753 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2754 t->path_refresh_task =
2755 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2759 /* Start a DHT get */
2760 peer_info_connect (peer, t);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2766 * Add a path to a tunnel which we don't own, just to remember the next hop.
2767 * If destination node was already in the tunnel, the first hop information
2768 * will be replaced with the new path.
2770 * @param t Tunnel we want to add a new peer to
2771 * @param p Path to add
2772 * @param own_pos Position of local node in path.
2776 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2777 unsigned int own_pos)
2779 struct GNUNET_PeerIdentity id;
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2782 GNUNET_assert (0 != own_pos);
2783 tree_add_path (t->tree, p, NULL, NULL);
2784 if (own_pos < p->length - 1)
2786 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2787 tree_update_first_hops (t->tree, myid, &id);
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2793 * Add a client to a tunnel, initializing all needed data structures.
2795 * @param t Tunnel to which add the client.
2796 * @param c Client which to add to the tunnel.
2799 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2801 struct MeshTunnelClientInfo clinfo;
2803 GNUNET_array_append (t->clients, t->nclients, c);
2804 clinfo.fwd_ack = t->fwd_pid + 1;
2805 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2806 clinfo.fwd_pid = t->fwd_pid;
2807 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2809 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2814 * Notifies a tunnel that a connection has broken that affects at least
2815 * some of its peers. Sends a notification towards the root of the tree.
2816 * In case the peer is the owner of the tree, notifies the client that owns
2817 * the tunnel and tries to reconnect.
2819 * @param t Tunnel affected.
2820 * @param p1 Peer that got disconnected from p2.
2821 * @param p2 Peer that got disconnected from p1.
2823 * @return Short ID of the peer disconnected (either p1 or p2).
2824 * 0 if the tunnel remained unaffected.
2826 static GNUNET_PEER_Id
2827 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2833 tree_notify_connection_broken (t->tree, p1, p2,
2834 &tunnel_notify_client_peer_disconnected,
2836 if (myid != p1 && myid != p2)
2842 if (tree_get_predecessor (t->tree) != 0)
2844 /* We are the peer still connected, notify owner of the disconnection. */
2845 struct GNUNET_MESH_PathBroken msg;
2846 struct GNUNET_PeerIdentity neighbor;
2848 msg.header.size = htons (sizeof (msg));
2849 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2850 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2851 msg.tid = htonl (t->id.tid);
2852 msg.peer1 = my_full_id;
2853 GNUNET_PEER_resolve (pid, &msg.peer2);
2854 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2855 send_prebuilt_message (&msg.header, &neighbor, t);
2863 * Send a multicast packet to a neighbor.
2865 * @param cls Closure (Info about the multicast packet)
2866 * @param neighbor_id Short ID of the neighbor to send the packet to.
2869 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2871 struct MeshData *mdata = cls;
2872 struct MeshTransmissionDescriptor *info;
2873 struct GNUNET_PeerIdentity neighbor;
2874 struct GNUNET_MessageHeader *msg;
2876 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2878 info->mesh_data = mdata;
2879 (mdata->reference_counter) ++;
2880 info->destination = neighbor_id;
2881 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2883 GNUNET_i2s (&neighbor));
2884 info->peer = peer_info_get (&neighbor);
2885 GNUNET_assert (NULL != info->peer);
2886 msg = (struct GNUNET_MessageHeader *) mdata->data;
2889 info->mesh_data->data_len,
2896 * Queue a message in a tunnel in multicast, sending a copy to each child node
2897 * down the local one in the tunnel tree.
2899 * @param t Tunnel in which to send the data.
2900 * @param msg Message to be sent.
2903 tunnel_send_multicast (struct MeshTunnel *t,
2904 const struct GNUNET_MessageHeader *msg)
2906 struct MeshData *mdata;
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2909 " sending a multicast packet...\n");
2911 mdata = GNUNET_malloc (sizeof (struct MeshData));
2912 mdata->data_len = ntohs (msg->size);
2914 mdata->data = GNUNET_malloc (mdata->data_len);
2915 memcpy (mdata->data, msg, mdata->data_len);
2916 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2918 struct GNUNET_MESH_Multicast *mcast;
2920 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2921 if (t->fwd_queue_n >= t->fwd_queue_max)
2924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
2925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2926 " message from %s!\n",
2927 GNUNET_i2s(&mcast->oid));
2928 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2929 " message at %s!\n",
2930 GNUNET_i2s(&my_full_id));
2931 GNUNET_free (mdata->data);
2932 GNUNET_free (mdata);
2936 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2938 ntohl (mcast->ttl));
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2945 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2946 if (mdata->reference_counter == 0)
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 " no one to send data to\n");
2950 GNUNET_free (mdata->data);
2951 GNUNET_free (mdata);
2952 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2957 mdata->total_out = mdata->reference_counter;
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2960 " sending a multicast packet done\n");
2966 * Increase the SKIP value of all peers that
2967 * have not received a unicast message.
2969 * @param cls Closure (ID of the peer that HAS received the message).
2970 * @param key ID of the neighbor.
2971 * @param value Information about the neighbor.
2973 * @return GNUNET_YES to keep iterating.
2976 tunnel_add_skip (void *cls,
2977 const struct GNUNET_HashCode * key,
2980 struct GNUNET_PeerIdentity *neighbor = cls;
2981 struct MeshTunnelChildInfo *cinfo = value;
2983 /* TODO compare only pointers? key == neighbor? */
2984 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
2994 * @brief Get neighbor's Flow Control information.
2996 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
2997 * descendant of the local node in a certain tunnel.
2998 * If the info is not yet there (recently created path), creates the data struct
2999 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3002 * @param t Tunnel related.
3003 * @param peer Neighbor whose Flow Control info is needed.
3005 * @return Neighbor's Flow Control info.
3007 static struct MeshTunnelChildInfo *
3008 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3009 const struct GNUNET_PeerIdentity *peer)
3011 struct MeshTunnelChildInfo *cinfo;
3013 if (NULL == t->children_fc)
3016 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3022 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3023 cinfo->id = GNUNET_PEER_intern (peer);
3024 cinfo->skip = t->fwd_pid;
3027 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3028 cinfo->fwd_ack = t->fwd_pid + delta;
3029 cinfo->bck_ack = delta;
3030 cinfo->bck_pid = -1;
3032 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3033 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3035 cinfo->send_buffer =
3036 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3038 GNUNET_assert (GNUNET_OK ==
3039 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3042 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3049 * Get the Flow Control info of a client.
3051 * @param t Tunnel on which to look.
3052 * @param c Client whose ACK to get.
3054 * @return ACK value.
3056 static struct MeshTunnelClientInfo *
3057 tunnel_get_client_fc (struct MeshTunnel *t,
3058 struct MeshClient *c)
3062 for (i = 0; i < t->nclients; i++)
3064 if (t->clients[i] != c)
3066 return &t->clients_fc[i];
3069 return NULL; // avoid compiler / coverity complaints
3074 * Iterator to get the appropiate ACK value from all children nodes.
3076 * @param cls Closue (tunnel).
3077 * @param id Id of the child node.
3080 tunnel_get_child_fwd_ack (void *cls,
3083 struct GNUNET_PeerIdentity peer_id;
3084 struct MeshTunnelChildInfo *cinfo;
3085 struct MeshTunnelChildIteratorContext *ctx = cls;
3086 struct MeshTunnel *t = ctx->t;
3089 GNUNET_PEER_resolve (id, &peer_id);
3090 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3091 ack = cinfo->fwd_ack;
3094 if (GNUNET_NO == ctx->init)
3096 ctx->max_child_ack = ack;
3097 ctx->init = GNUNET_YES;
3100 if (GNUNET_YES == t->speed_min)
3102 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3106 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3113 * Get the maximum PID allowed to transmit to any
3114 * tunnel child of the local peer, depending on the tunnel
3115 * buffering/speed settings.
3119 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3122 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3124 struct MeshTunnelChildIteratorContext ctx;
3126 ctx.max_child_ack = 0;
3128 ctx.init = GNUNET_NO;
3129 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3131 if (0 == ctx.nchildren)
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3134 " tunnel has no children, no FWD ACK\n");
3138 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3139 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3141 return (int64_t) ctx.max_child_ack;
3146 * Set the FWD ACK value of a client in a particular tunnel.
3148 * @param t Tunnel affected.
3149 * @param c Client whose ACK to set.
3150 * @param ack ACK value.
3153 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3154 struct MeshClient *c,
3159 for (i = 0; i < t->nclients; i++)
3161 if (t->clients[i] != c)
3163 t->clients_fc[i].fwd_ack = ack;
3171 * Get the highest ACK value of all clients in a particular tunnel,
3172 * according to the buffering/speed settings.
3174 * @param t Tunnel on which to look.
3176 * @return Corresponding ACK value (max uint32_t).
3177 * If no clients are suscribed, -1LL.
3180 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3185 if (0 == t->nclients)
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3188 " tunnel has no clients, no FWD ACK\n");
3192 for (ack = -1LL, i = 0; i < t->nclients; i++)
3195 (GNUNET_YES == t->speed_min &&
3196 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3197 (GNUNET_NO == t->speed_min &&
3198 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3200 ack = t->clients_fc[i].fwd_ack;
3204 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3205 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3207 return (uint32_t) ack;
3212 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3213 * mode and the status of all children nodes.
3217 * @return Maximum PID allowed.
3220 tunnel_get_fwd_ack (struct MeshTunnel *t)
3224 uint32_t buffer_free;
3228 count = t->fwd_pid - t->skip;
3229 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3230 child_ack = tunnel_get_children_fwd_ack (t);
3231 client_ack = tunnel_get_clients_fwd_ack (t);
3232 if (GNUNET_YES == t->nobuffer)
3235 if (-1LL == child_ack)
3236 child_ack = client_ack;
3237 if (-1LL == child_ack)
3240 client_ack = child_ack = ack;
3245 ack = count + buffer_free; // Overflow? OK!
3247 if (-1LL == child_ack)
3249 // Node has no children, child_ack AND core buffer are irrelevant.
3250 if (-1LL == client_ack) // No children AND no clients? Not good!
3252 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3256 return (uint32_t) client_ack;
3258 if (-1LL == client_ack)
3262 if (GNUNET_YES == t->speed_min)
3264 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3265 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3269 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3270 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3273 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3274 count, buffer_free, child_ack, client_ack, ack);
3280 * Build a local ACK message and send it to a local client.
3282 * @param t Tunnel on which to send the ACK.
3283 * @param c Client to whom send the ACK.
3284 * @param ack Value of the ACK.
3287 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3289 struct GNUNET_MESH_LocalAck msg;
3291 msg.header.size = htons (sizeof (msg));
3292 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3293 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3294 msg.max_pid = htonl (ack);
3295 GNUNET_SERVER_notification_context_unicast(nc,
3302 * Build an ACK message and queue it to send to the given peer.
3304 * @param t Tunnel on which to send the ACK.
3305 * @param peer Peer to whom send the ACK.
3306 * @param ack Value of the ACK.
3309 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3311 struct GNUNET_MESH_ACK msg;
3313 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3314 msg.header.size = htons (sizeof (msg));
3315 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3316 msg.pid = htonl (ack);
3317 msg.tid = htonl (t->id.tid);
3319 send_prebuilt_message (&msg.header, peer, t);
3324 * Notify a the owner of a tunnel about how many more
3325 * payload packages will we accept on a given tunnel.
3327 * @param t Tunnel on which to send the ACK.
3330 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 "Sending client FWD ACK on tunnel %X\n",
3338 ack = tunnel_get_fwd_ack (t);
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3341 if (t->last_fwd_ack == ack)
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3348 t->last_fwd_ack = ack;
3349 send_local_ack (t, t->owner, ack);
3354 * Send an ACK informing the predecessor about the available buffer space.
3355 * In case there is no predecessor, inform the owning client.
3356 * If buffering is off, send only on behalf of children or self if endpoint.
3357 * If buffering is on, send when sent to children and buffer space is free.
3358 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3359 * the ACK itself goes "back" (towards root).
3361 * @param t Tunnel on which to send the ACK.
3362 * @param type Type of message that triggered the ACK transmission.
3365 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3367 struct GNUNET_PeerIdentity id;
3370 if (NULL != t->owner)
3372 tunnel_send_client_fwd_ack (t);
3375 /* Is it after unicast / multicast retransmission? */
3378 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3379 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 "ACK due to FWD DATA retransmission\n");
3382 if (GNUNET_YES == t->nobuffer)
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3388 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3389 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3391 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3392 t->force_ack = GNUNET_YES;
3398 /* Check if we need to transmit the ACK */
3399 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3400 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3401 GNUNET_NO == t->force_ack)
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3405 " t->qmax: %u, t->qn: %u\n",
3406 t->fwd_queue_max, t->fwd_queue_n);
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3408 " t->pid: %u, t->ack: %u\n",
3409 t->fwd_pid, t->last_fwd_ack);
3413 /* Ok, ACK might be necessary, what PID to ACK? */
3414 ack = tunnel_get_fwd_ack (t);
3416 /* If speed_min and not all children have ack'd, dont send yet */
3417 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3423 t->last_fwd_ack = ack;
3424 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3425 send_ack (t, &id, ack);
3427 t->force_ack = GNUNET_NO;
3432 * Iterator to send a child node a BCK ACK to allow him to send more
3435 * @param cls Closure (tunnel).
3436 * @param id Id of the child node.
3439 tunnel_send_child_bck_ack (void *cls,
3442 struct MeshTunnel *t = cls;
3443 struct MeshTunnelChildInfo *cinfo;
3444 struct GNUNET_PeerIdentity peer;
3447 GNUNET_PEER_resolve (id, &peer);
3448 cinfo = tunnel_get_neighbor_fc (t, &peer);
3449 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3451 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3454 " Not sending ACK, not needed\n");
3457 cinfo->bck_ack = ack;
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460 " Sending BCK ACK %u (last sent: %u)\n",
3461 ack, cinfo->bck_ack);
3462 send_ack (t, &peer, ack);
3467 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3469 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3471 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3472 * - implement a size 1 buffer in each client_fc AND children_fc
3473 * to hold at least 1 message per "child".
3474 * problem: violates no buffer policy
3475 * - ack 0 and make "children" poll for transmission slots
3476 * problem: big overhead, extra latency even in low traffic
3479 * @param t Tunnel on which to send the BCK ACKs.
3482 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3485 unsigned int tunnel_delta;
3487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3489 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3492 /* Find client whom to allow to send to origin (with lowest buffer space) */
3493 for (i = 0; i < t->nclients; i++)
3495 struct MeshTunnelClientInfo *clinfo;
3498 clinfo = &t->clients_fc[i];
3499 delta = clinfo->bck_ack - clinfo->bck_pid;
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3501 t->clients[i]->id, delta);
3503 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3504 (GNUNET_YES == t->nobuffer && 0 == delta))
3508 ack = clinfo->bck_pid;
3509 ack += t->nobuffer ? 1 : tunnel_delta;
3510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3511 " sending ack to client %u: %u\n",
3512 t->clients[i]->id, ack);
3513 send_local_ack (t, t->clients[i], ack);
3514 clinfo->bck_ack = ack;
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3519 " not sending ack to client %u (td %u, d %u)\n",
3520 t->clients[i]->id, tunnel_delta, delta);
3527 * Send an ACK informing the children nodes and destination clients about
3528 * the available buffer space.
3529 * If buffering is off, send only on behalf of root (can be self).
3530 * If buffering is on, send when sent to predecessor and buffer space is free.
3531 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3532 * the ACK itself goes "forward" (towards children/clients).
3534 * @param t Tunnel on which to send the ACK.
3535 * @param type Type of message that triggered the ACK transmission.
3538 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3541 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3542 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3543 /* Is it after data to_origin retransmission? */
3546 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3547 if (GNUNET_YES == t->nobuffer)
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3550 " Not sending ACK, nobuffer\n");
3554 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3555 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3557 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3558 t->force_ack = GNUNET_YES;
3564 tunnel_send_clients_bck_ack (t);
3565 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3566 t->force_ack = GNUNET_NO;
3571 * @brief Re-initiate traffic to this peer if necessary.
3573 * Check if there is traffic queued towards this peer
3574 * and the core transmit handle is NULL (traffic was stalled).
3575 * If so, call core tmt rdy.
3577 * @param cls Closure (unused)
3578 * @param peer_id Short ID of peer to which initiate traffic.
3581 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3583 struct MeshPeerInfo *peer;
3584 struct GNUNET_PeerIdentity id;
3585 struct MeshPeerQueue *q;
3588 peer = peer_info_get_short(peer_id);
3589 if (NULL != peer->core_transmit)
3592 q = queue_get_next(peer);
3595 /* Might br multicast traffic already sent to this particular peer but
3596 * not to other children in this tunnel.
3597 * This way t->queue_n would be > 0 but the queue of this particular peer
3603 GNUNET_PEER_resolve (peer->id, &id);
3604 peer->core_transmit =
3605 GNUNET_CORE_notify_transmit_ready(core_handle,
3608 GNUNET_TIME_UNIT_FOREVER_REL,
3618 * @brief Allow transmission of FWD traffic on this tunnel
3620 * Check if there is traffic queued towards any children
3621 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3623 * @param t Tunnel on which to unlock FWD traffic.
3626 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3628 if (0 == t->fwd_queue_n)
3631 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3636 * @brief Allow transmission of BCK traffic on this tunnel
3638 * Check if there is traffic queued towards the root of the tree
3639 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3641 * @param t Tunnel on which to unlock BCK traffic.
3644 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3646 if (0 == t->bck_queue_n)
3649 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3654 * Send a message to all peers in this tunnel that the tunnel is no longer
3657 * @param t The tunnel whose peers to notify.
3658 * @param parent ID of the parent, in case the tree is already destroyed.
3661 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3663 struct GNUNET_MESH_TunnelDestroy msg;
3664 struct GNUNET_PeerIdentity id;
3666 msg.header.size = htons (sizeof (msg));
3667 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3668 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3669 msg.tid = htonl (t->id.tid);
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3671 " sending tunnel destroy for tunnel: %s [%X]\n",
3672 GNUNET_i2s (&msg.oid), t->id.tid);
3673 if (tree_count_children(t->tree) > 0)
3675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
3676 tunnel_send_multicast (t, &msg.header);
3679 parent = tree_get_predecessor (t->tree);
3680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3684 GNUNET_PEER_resolve (parent, &id);
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3686 " sending back to %s\n",
3688 send_prebuilt_message (&msg.header, &id, t);
3693 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3695 * @param cls Closure (Tunnel which to cancel).
3696 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3699 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3701 struct MeshTunnel *t = cls;
3702 struct MeshPeerInfo *peer_info;
3703 struct MeshPeerQueue *pq;
3704 struct MeshPeerQueue *next;
3706 peer_info = peer_info_get_short (neighbor_id);
3707 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3710 if (pq->tunnel == t)
3712 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3713 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3714 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3716 // Should have been removed on destroy children
3719 queue_destroy (pq, GNUNET_YES);
3722 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3724 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3725 peer_info->core_transmit = NULL;
3730 * Destroy the tunnel and free any allocated resources linked to it.
3732 * @param t the tunnel to destroy
3734 * @return GNUNET_OK on success
3737 tunnel_destroy (struct MeshTunnel *t)
3739 struct MeshClient *c;
3740 struct GNUNET_HashCode hash;
3751 struct GNUNET_PeerIdentity id;
3753 GNUNET_PEER_resolve (t->id.oid, &id);
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3755 GNUNET_i2s (&id), t->id.tid);
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3761 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3762 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3770 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3772 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3779 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3780 for (i = 0; i < t->nclients; i++)
3784 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3790 for (i = 0; i < t->nignore; i++)
3794 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3801 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3802 GNUNET_free_non_null (t->clients);
3803 GNUNET_free_non_null (t->ignore);
3804 GNUNET_free_non_null (t->clients_fc);
3806 if (NULL != t->peers)
3808 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3810 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3813 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3814 &tunnel_destroy_child,
3816 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3817 t->children_fc = NULL;
3819 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3820 tree_destroy (t->tree);
3822 if (NULL != t->regex_search)
3823 GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
3824 if (NULL != t->dht_get_type)
3825 GNUNET_DHT_get_stop (t->dht_get_type);
3826 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3827 GNUNET_SCHEDULER_cancel (t->timeout_task);
3828 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3829 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3832 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3837 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3840 * Tunnel is empty: destroy it.
3842 * @param cls Closure (Tunnel).
3843 * @param tc TaskContext.
3846 tunnel_destroy_empty_delayed (void *cls,
3847 const struct GNUNET_SCHEDULER_TaskContext *tc)
3849 struct MeshTunnel *t = cls;
3851 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3852 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3855 if (0 != t->nclients ||
3856 0 != tree_count_children (t->tree))
3861 struct GNUNET_PeerIdentity id;
3863 GNUNET_PEER_resolve (t->id.oid, &id);
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3865 "executing destruction of empty tunnel %s [%X]\n",
3866 GNUNET_i2s (&id), t->id.tid);
3870 tunnel_send_destroy (t, 0);
3871 if (0 == t->pending_messages)
3874 t->destroy = GNUNET_YES;
3879 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3881 * @param t Tunnel to destroy if empty.
3884 tunnel_destroy_empty (struct MeshTunnel *t)
3886 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3888 0 != tree_count_children (t->tree))
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3892 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
3898 struct GNUNET_PeerIdentity id;
3900 GNUNET_PEER_resolve (t->id.oid, &id);
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 "scheduling destruction of empty tunnel %s [%X]\n",
3903 GNUNET_i2s (&id), t->id.tid);
3907 t->delayed_destroy =
3908 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3909 &tunnel_destroy_empty_delayed,
3915 * Create a new tunnel
3917 * @param owner Who is the owner of the tunnel (short ID).
3918 * @param tid Tunnel Number of the tunnel.
3919 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3920 * @param local Tunnel Number for the tunnel, for the client point of view.
3922 * @return A new initialized tunnel. NULL on error.
3924 static struct MeshTunnel *
3925 tunnel_new (GNUNET_PEER_Id owner,
3926 MESH_TunnelNumber tid,
3927 struct MeshClient *client,
3928 MESH_TunnelNumber local)
3930 struct MeshTunnel *t;
3931 struct GNUNET_HashCode hash;
3933 if (n_tunnels >= max_tunnels && NULL == client)
3936 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3939 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3940 t->bck_queue_max = t->fwd_queue_max;
3941 t->tree = tree_new (owner);
3943 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3944 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3945 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3946 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3947 t->local_tid = local;
3948 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
3950 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3952 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3954 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3962 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3969 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3971 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3976 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3985 * Callback when removing children from a tunnel tree. Notify owner.
3987 * @param cls Closure (tunnel).
3988 * @param peer_id Short ID of the peer deleted.
3991 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
3993 struct MeshTunnel *t = cls;
3995 client_notify_peer_disconnected (t->owner, t, peer_id);
3999 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4000 * that are no longer needed, as well as nodes of no longer reachable peers.
4001 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4003 * @param t Tunnel from which to remove the path.
4004 * @param peer Short id of the peer which should be removed.
4007 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4011 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4015 "Tunnel %u [%u] has no more nodes\n",
4016 t->id.oid, t->id.tid);
4022 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4023 * client when the client disconnects. If the client is not the owner, the
4024 * owner will get notified if no more clients are in the tunnel and the client
4025 * get removed from the tunnel's list.
4027 * @param cls closure (client that is disconnecting)
4028 * @param key the hash of the local tunnel id (used to access the hashmap)
4029 * @param value the value stored at the key (tunnel to destroy)
4031 * @return GNUNET_OK, keep iterating.
4034 tunnel_destroy_iterator (void *cls,
4035 const struct GNUNET_HashCode * key,
4038 struct MeshTunnel *t = value;
4039 struct MeshClient *c = cls;
4041 send_client_tunnel_disconnect (t, c);
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4045 tunnel_delete_client (t, c);
4046 client_delete_tunnel (c, t);
4047 tunnel_destroy_empty (t);
4050 tunnel_send_destroy (t, 0);
4052 t->destroy = GNUNET_YES;
4059 * Timeout function, destroys tunnel if called
4061 * @param cls Closure (tunnel to destroy).
4062 * @param tc TaskContext
4065 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4067 struct MeshTunnel *t = cls;
4068 struct GNUNET_PeerIdentity id;
4070 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4071 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4073 GNUNET_PEER_resolve(t->id.oid, &id);
4074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4075 "Tunnel %s [%X] timed out. Destroying.\n",
4076 GNUNET_i2s(&id), t->id.tid);
4077 send_clients_tunnel_destroy (t);
4082 * Resets the tunnel timeout. Starts it if no timeout was running.
4084 * @param t Tunnel whose timeout to reset.
4086 * TODO use heap to improve efficiency of scheduler.
4089 tunnel_reset_timeout (struct MeshTunnel *t)
4091 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4092 GNUNET_SCHEDULER_cancel (t->timeout_task);
4094 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4095 (refresh_path_time, 4), &tunnel_timeout, t);
4099 /******************************************************************************/
4100 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4101 /******************************************************************************/
4104 * Function to send a create path packet to a peer.
4106 * @param cls closure
4107 * @param size number of bytes available in buf
4108 * @param buf where the callee should write the message
4109 * @return number of bytes written to buf
4112 send_core_path_create (void *cls, size_t size, void *buf)
4114 struct MeshPathInfo *info = cls;
4115 struct GNUNET_MESH_ManipulatePath *msg;
4116 struct GNUNET_PeerIdentity *peer_ptr;
4117 struct MeshTunnel *t = info->t;
4118 struct MeshPeerPath *p = info->path;
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4125 sizeof (struct GNUNET_MESH_ManipulatePath) +
4126 p->length * sizeof (struct GNUNET_PeerIdentity);
4128 if (size < size_needed || NULL == buf)
4133 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4134 msg->header.size = htons (size_needed);
4135 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4136 msg->tid = ntohl (t->id.tid);
4139 if (GNUNET_YES == t->speed_min)
4140 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4141 if (GNUNET_YES == t->nobuffer)
4142 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4143 msg->opt = htonl(opt);
4146 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4147 for (i = 0; i < p->length; i++)
4149 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4156 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4162 * Fill the core buffer
4164 * @param cls closure (data itself)
4165 * @param size number of bytes available in buf
4166 * @param buf where the callee should write the message
4168 * @return number of bytes written to buf
4171 send_core_data_multicast (void *cls, size_t size, void *buf)
4173 struct MeshTransmissionDescriptor *info = cls;
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4177 GNUNET_assert (NULL != info);
4178 GNUNET_assert (NULL != info->peer);
4179 total_size = info->mesh_data->data_len;
4180 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4182 if (total_size > size)
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4188 memcpy (buf, info->mesh_data->data, total_size);
4191 struct GNUNET_MESH_Multicast *mc;
4192 struct GNUNET_MessageHeader *mh;
4195 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4197 mc = (struct GNUNET_MESH_Multicast *) mh;
4198 mh = (struct GNUNET_MessageHeader *) &mc[1];
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4200 " multicast, payload type %s\n",
4201 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4203 " multicast, payload size %u\n", ntohs (mh->size));
4207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4208 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4212 data_descriptor_decrement_rc (info->mesh_data);
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4221 * Creates a path ack message in buf and frees all unused resources.
4223 * @param cls closure (MeshTransmissionDescriptor)
4224 * @param size number of bytes available in buf
4225 * @param buf where the callee should write the message
4226 * @return number of bytes written to buf
4229 send_core_path_ack (void *cls, size_t size, void *buf)
4231 struct MeshTransmissionDescriptor *info = cls;
4232 struct GNUNET_MESH_PathACK *msg = buf;
4234 GNUNET_assert (NULL != info);
4235 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4240 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4241 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4242 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4243 msg->tid = htonl (info->origin->tid);
4244 msg->peer_id = my_full_id;
4247 /* TODO add signature */
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4250 return sizeof (struct GNUNET_MESH_PathACK);
4255 * Free a transmission that was already queued with all resources
4256 * associated to the request.
4258 * @param queue Queue handler to cancel.
4259 * @param clear_cls Is it necessary to free associated cls?
4262 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4264 struct MeshTransmissionDescriptor *dd;
4265 struct MeshPathInfo *path_info;
4266 struct MeshTunnelChildInfo *cinfo;
4267 struct GNUNET_PeerIdentity id;
4271 if (GNUNET_YES == clear_cls)
4273 switch (queue->type)
4275 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4276 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4277 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4279 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4280 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4281 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4282 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4283 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4284 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4286 " prebuilt message\n");
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4289 GNUNET_MESH_DEBUG_M2S(queue->type));
4291 data_descriptor_decrement_rc (dd->mesh_data);
4293 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4295 path_info = queue->cls;
4296 path_destroy (path_info->path);
4300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4301 " type %s unknown!\n",
4302 GNUNET_MESH_DEBUG_M2S(queue->type));
4304 GNUNET_free_non_null (queue->cls);
4306 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4307 queue->peer->queue_tail,
4310 /* Delete from child_fc in the appropiate tunnel */
4311 max = queue->tunnel->fwd_queue_max;
4312 GNUNET_PEER_resolve (queue->peer->id, &id);
4313 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4316 for (i = 0; i < cinfo->send_buffer_n; i++)
4319 i2 = (cinfo->send_buffer_start + i) % max;
4320 if (cinfo->send_buffer[i2] == queue)
4322 /* Found corresponding entry in the send_buffer. Move all others back. */
4327 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4329 j2 = (cinfo->send_buffer_start + j) % max;
4330 j3 = (cinfo->send_buffer_start + j + 1) % max;
4331 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4334 cinfo->send_buffer[j3] = NULL;
4335 cinfo->send_buffer_n--;
4340 GNUNET_free (queue);
4345 * @brief Get the next transmittable message from the queue.
4347 * This will be the head, except in the case of being a data packet
4348 * not allowed by the destination peer.
4350 * @param peer Destination peer.
4352 * @return The next viable MeshPeerQueue element to send to that peer.
4353 * NULL when there are no transmittable messages.
4355 struct MeshPeerQueue *
4356 queue_get_next (const struct MeshPeerInfo *peer)
4358 struct MeshPeerQueue *q;
4359 struct MeshTunnel *t;
4360 struct MeshTransmissionDescriptor *info;
4361 struct MeshTunnelChildInfo *cinfo;
4362 struct GNUNET_MESH_Unicast *ucast;
4363 struct GNUNET_MESH_ToOrigin *to_orig;
4364 struct GNUNET_PeerIdentity id;
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4369 for (q = peer->queue_head; NULL != q; q = q->next)
4373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4375 GNUNET_MESH_DEBUG_M2S(q->type));
4378 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4379 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4380 pid = ntohl (ucast->pid);
4381 GNUNET_PEER_resolve (info->peer->id, &id);
4382 cinfo = tunnel_get_neighbor_fc(t, &id);
4383 ack = cinfo->fwd_ack;
4385 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4386 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4387 pid = ntohl (to_orig->pid);
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4396 "********* ACK: %u, PID: %u\n",
4398 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4407 "********* NEXT!\n");
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4411 "********* nothing found\n");
4417 * Core callback to write a queued packet to core buffer
4419 * @param cls Closure (peer info).
4420 * @param size Number of bytes available in buf.
4421 * @param buf Where the to write the message.
4423 * @return number of bytes written to buf
4426 queue_send (void *cls, size_t size, void *buf)
4428 struct MeshPeerInfo *peer = cls;
4429 struct GNUNET_MessageHeader *msg;
4430 struct MeshPeerQueue *queue;
4431 struct MeshTunnel *t;
4432 struct MeshTunnelChildInfo *cinfo;
4433 struct GNUNET_PeerIdentity dst_id;
4436 peer->core_transmit = NULL;
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4440 queue = queue_get_next (peer);
4442 /* Queue has no internal mesh traffic nor sendable payload */
4445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4446 if (NULL == peer->queue_head)
4447 GNUNET_break (0); // Should've been canceled
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4452 GNUNET_PEER_resolve (peer->id, &dst_id);
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4454 "********* towards %s\n",
4455 GNUNET_i2s(&dst_id));
4456 /* Check if buffer size is enough for the message */
4457 if (queue->size > size)
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4460 "********* not enough room, reissue\n");
4461 peer->core_transmit =
4462 GNUNET_CORE_notify_transmit_ready (core_handle,
4465 GNUNET_TIME_UNIT_FOREVER_REL,
4472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4475 GNUNET_assert (0 < t->pending_messages);
4476 t->pending_messages--;
4477 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4481 "********* unicast: t->q (%u/%u)\n",
4482 t->fwd_queue_n, t->fwd_queue_max);
4484 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4491 switch (queue->type)
4494 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4495 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4496 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4497 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4498 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4500 "********* raw: %s\n",
4501 GNUNET_MESH_DEBUG_M2S (queue->type));
4503 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4504 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4505 data_size = send_core_data_raw (queue->cls, size, buf);
4506 msg = (struct GNUNET_MessageHeader *) buf;
4507 switch (ntohs (msg->type)) // Type of preconstructed message
4509 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4510 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4512 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4513 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4519 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4522 struct MeshTransmissionDescriptor *info = queue->cls;
4524 if ((1 == info->mesh_data->reference_counter
4525 && GNUNET_YES == t->speed_min)
4527 (info->mesh_data->total_out == info->mesh_data->reference_counter
4528 && GNUNET_NO == t->speed_min))
4530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4531 "********* considered sent\n");
4536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4537 "********* NOT considered sent yet\n");
4538 t->pending_messages++;
4541 data_size = send_core_data_multicast(queue->cls, size, buf);
4542 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4544 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4546 data_size = send_core_path_create (queue->cls, size, buf);
4548 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4550 data_size = send_core_path_ack (queue->cls, size, buf);
4552 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4554 data_size = send_core_data_multicast (queue->cls, size, buf);
4558 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4559 "********* type unknown: %u\n",
4563 switch (queue->type)
4565 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4566 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4567 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4568 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4569 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4573 "at pos %u (%p) != %p\n",
4574 cinfo->send_buffer_start,
4575 cinfo->send_buffer[cinfo->send_buffer_start],
4578 if (cinfo->send_buffer_n > 0)
4580 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4581 cinfo->send_buffer_n--;
4582 cinfo->send_buffer_start++;
4583 cinfo->send_buffer_start %= t->fwd_queue_max;
4594 /* Free queue, but cls was freed by send_core_* */
4595 queue_destroy (queue, GNUNET_NO);
4597 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4603 /* If more data in queue, send next */
4604 queue = queue_get_next(peer);
4607 struct GNUNET_PeerIdentity id;
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4610 GNUNET_PEER_resolve (peer->id, &id);
4611 peer->core_transmit =
4612 GNUNET_CORE_notify_transmit_ready(core_handle,
4615 GNUNET_TIME_UNIT_FOREVER_REL,
4623 if (NULL != peer->queue_head)
4625 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4626 "********* %s stalled\n",
4627 GNUNET_i2s(&my_full_id));
4629 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4630 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4631 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4633 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4634 &tunnel_poll, cinfo);
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4644 * @brief Queue and pass message to core when possible.
4646 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4647 * and accounts for it. In case the queue is full, the message is dropped and
4650 * Otherwise, message is treated as internal and allowed to go regardless of
4653 * @param cls Closure (@c type dependant). It will be used by queue_send to
4654 * build the message to be sent if not already prebuilt.
4655 * @param type Type of the message, 0 for a raw message.
4656 * @param size Size of the message.
4657 * @param dst Neighbor to send message to.
4658 * @param t Tunnel this message belongs to.
4661 queue_add (void *cls, uint16_t type, size_t size,
4662 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4664 struct MeshPeerQueue *queue;
4665 struct MeshTunnelChildInfo *cinfo;
4666 struct GNUNET_PeerIdentity id;
4672 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4673 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4675 n = &t->fwd_queue_n;
4676 max = &t->fwd_queue_max;
4678 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4680 n = &t->bck_queue_n;
4681 max = &t->bck_queue_max;
4688 GNUNET_STATISTICS_update(stats,
4689 "# messages dropped (buffer full)",
4691 return; // Drop message
4695 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4701 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4702 GNUNET_PEER_resolve (dst->id, &id);
4703 if (NULL == dst->core_transmit)
4705 dst->core_transmit =
4706 GNUNET_CORE_notify_transmit_ready (core_handle,
4709 GNUNET_TIME_UNIT_FOREVER_REL,
4715 t->pending_messages++;
4716 if (NULL == n) // Is this internal mesh traffic?
4719 // It's payload, keep track of buffer per peer.
4720 cinfo = tunnel_get_neighbor_fc(t, &id);
4721 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4722 if (NULL != cinfo->send_buffer[i])
4724 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4725 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4726 cinfo->send_buffer_start++;
4727 cinfo->send_buffer_start %= t->fwd_queue_max;
4731 cinfo->send_buffer_n++;
4733 cinfo->send_buffer[i] = queue;
4734 if (cinfo->send_buffer_n > t->fwd_queue_max)
4737 cinfo->send_buffer_n = t->fwd_queue_max;
4742 /******************************************************************************/
4743 /******************** MESH NETWORK HANDLERS **************************/
4744 /******************************************************************************/
4748 * Core handler for path creation
4750 * @param cls closure
4751 * @param message message
4752 * @param peer peer identity this notification is about
4754 * @return GNUNET_OK to keep the connection open,
4755 * GNUNET_SYSERR to close it (signal serious error)
4758 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4759 const struct GNUNET_MessageHeader *message)
4761 unsigned int own_pos;
4764 MESH_TunnelNumber tid;
4765 struct GNUNET_MESH_ManipulatePath *msg;
4766 struct GNUNET_PeerIdentity *pi;
4767 struct GNUNET_HashCode hash;
4768 struct MeshPeerPath *path;
4769 struct MeshPeerInfo *dest_peer_info;
4770 struct MeshPeerInfo *orig_peer_info;
4771 struct MeshTunnel *t;
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4774 "Received a path create msg [%s]\n",
4775 GNUNET_i2s (&my_full_id));
4776 size = ntohs (message->size);
4777 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4779 GNUNET_break_op (0);
4783 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4784 if (size % sizeof (struct GNUNET_PeerIdentity))
4786 GNUNET_break_op (0);
4789 size /= sizeof (struct GNUNET_PeerIdentity);
4792 GNUNET_break_op (0);
4795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4796 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4798 tid = ntohl (msg->tid);
4799 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4801 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4802 t = tunnel_get (pi, tid);
4803 if (NULL == t) // FIXME only for INCOMING tunnels?
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4808 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4811 // FIXME notify failure
4814 opt = ntohl (msg->opt);
4815 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4816 GNUNET_YES : GNUNET_NO;
4817 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4819 t->nobuffer = GNUNET_YES;
4820 t->last_fwd_ack = t->fwd_pid + 1;
4822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4823 " speed_min: %d, nobuffer:%d\n",
4824 t->speed_min, t->nobuffer);
4826 if (GNUNET_YES == t->nobuffer)
4828 t->bck_queue_max = 1;
4829 t->fwd_queue_max = 1;
4832 // FIXME only assign a local tid if a local client is interested (on demand)
4833 while (NULL != tunnel_get_incoming (next_local_tid))
4834 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4835 t->local_tid_dest = next_local_tid++;
4836 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4839 tunnel_reset_timeout (t);
4840 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4842 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4843 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4851 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4852 if (NULL == dest_peer_info)
4854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4855 " Creating PeerInfo for destination.\n");
4856 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4857 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4858 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4860 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4862 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4863 if (NULL == orig_peer_info)
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4866 " Creating PeerInfo for origin.\n");
4867 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4868 orig_peer_info->id = GNUNET_PEER_intern (pi);
4869 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4873 path = path_new (size);
4875 for (i = 0; i < size; i++)
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4878 GNUNET_i2s (&pi[i]));
4879 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4880 if (path->peers[i] == myid)
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4886 /* cannot be self, must be 'not found' */
4887 /* create path: self not found in path through self */
4888 GNUNET_break_op (0);
4889 path_destroy (path);
4893 path_add_to_peers (path, GNUNET_NO);
4894 tunnel_add_path (t, path, own_pos);
4895 if (own_pos == size - 1)
4897 /* It is for us! Send ack. */
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4899 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4900 GNUNET_break (GNUNET_SYSERR !=
4901 GNUNET_CONTAINER_multihashmap_put (t->peers,
4902 &my_full_id.hashPubKey,
4905 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4910 struct MeshPeerPath *path2;
4912 /* It's for somebody else! Retransmit. */
4913 path2 = path_duplicate (path);
4914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4915 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4916 path2 = path_duplicate (path);
4917 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4918 send_create_path (dest_peer_info, path, t);
4925 * Core handler for path destruction
4927 * @param cls closure
4928 * @param message message
4929 * @param peer peer identity this notification is about
4931 * @return GNUNET_OK to keep the connection open,
4932 * GNUNET_SYSERR to close it (signal serious error)
4935 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4936 const struct GNUNET_MessageHeader *message)
4938 struct GNUNET_MESH_ManipulatePath *msg;
4939 struct GNUNET_PeerIdentity *pi;
4940 struct MeshPeerPath *path;
4941 struct MeshTunnel *t;
4942 unsigned int own_pos;
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4947 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4948 size = ntohs (message->size);
4949 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4951 GNUNET_break_op (0);
4955 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4956 if (size % sizeof (struct GNUNET_PeerIdentity))
4958 GNUNET_break_op (0);
4961 size /= sizeof (struct GNUNET_PeerIdentity);
4964 GNUNET_break_op (0);
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4969 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4970 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4972 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4974 t = tunnel_get (pi, ntohl (msg->tid));
4977 /* TODO notify back: we don't know this tunnel */
4978 GNUNET_break_op (0);
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4982 path = path_new (size);
4984 for (i = 0; i < size; i++)
4986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4987 GNUNET_i2s (&pi[i]));
4988 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4989 if (path->peers[i] == myid)
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4993 if (own_pos < path->length - 1)
4994 send_prebuilt_message (message, &pi[own_pos + 1], t);
4996 send_client_tunnel_disconnect(t, NULL);
4998 tunnel_delete_peer (t, path->peers[path->length - 1]);
4999 path_destroy (path);
5005 * Core handler for notifications of broken paths
5007 * @param cls closure
5008 * @param message message
5009 * @param peer peer identity this notification is about
5011 * @return GNUNET_OK to keep the connection open,
5012 * GNUNET_SYSERR to close it (signal serious error)
5015 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5016 const struct GNUNET_MessageHeader *message)
5018 struct GNUNET_MESH_PathBroken *msg;
5019 struct MeshTunnel *t;
5021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5022 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5023 msg = (struct GNUNET_MESH_PathBroken *) message;
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5025 GNUNET_i2s (&msg->peer1));
5026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5027 GNUNET_i2s (&msg->peer2));
5028 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5031 GNUNET_break_op (0);
5034 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5035 GNUNET_PEER_search (&msg->peer2));
5042 * Core handler for tunnel destruction
5044 * @param cls closure
5045 * @param message message
5046 * @param peer peer identity this notification is about
5048 * @return GNUNET_OK to keep the connection open,
5049 * GNUNET_SYSERR to close it (signal serious error)
5052 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5053 const struct GNUNET_MessageHeader *message)
5055 struct GNUNET_MESH_TunnelDestroy *msg;
5056 struct MeshTunnel *t;
5057 GNUNET_PEER_Id parent;
5060 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5062 "Got a TUNNEL DESTROY packet from %s\n",
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5065 " for tunnel %s [%u]\n",
5066 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5067 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5068 /* Check signature */
5071 /* Probably already got the message from another path,
5072 * destroyed the tunnel and retransmitted to children.
5075 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5079 parent = tree_get_predecessor (t->tree);
5080 pid = GNUNET_PEER_search (peer);
5085 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5086 nc = tree_count_children (t->tree);
5087 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 "still in use: %u cl, %u ch\n",
5095 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5097 /* Tunnel was incoming, notify clients */
5098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5099 t->local_tid, t->local_tid_dest);
5100 send_clients_tunnel_destroy (t);
5102 tunnel_send_destroy (t, parent);
5103 t->destroy = GNUNET_YES;
5104 // TODO: add timeout to destroy the tunnel anyway
5110 * Core handler for mesh network traffic going from the origin to a peer
5112 * @param cls closure
5113 * @param peer peer identity this notification is about
5114 * @param message message
5115 * @return GNUNET_OK to keep the connection open,
5116 * GNUNET_SYSERR to close it (signal serious error)
5119 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5120 const struct GNUNET_MessageHeader *message)
5122 struct GNUNET_MESH_Unicast *msg;
5123 struct GNUNET_PeerIdentity *neighbor;
5124 struct MeshTunnelChildInfo *cinfo;
5125 struct MeshTunnel *t;
5126 GNUNET_PEER_Id dest_id;
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5134 size = ntohs (message->size);
5136 sizeof (struct GNUNET_MESH_Unicast) +
5137 sizeof (struct GNUNET_MessageHeader))
5142 msg = (struct GNUNET_MESH_Unicast *) message;
5143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5144 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5146 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5149 /* TODO notify back: we don't know this tunnel */
5150 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5151 GNUNET_break_op (0);
5154 pid = ntohl (msg->pid);
5155 if (t->fwd_pid == pid)
5157 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5159 " Already seen pid %u, DROPPING!\n", pid);
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5165 " pid %u not seen yet, forwarding\n", pid);
5170 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5172 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5173 GNUNET_break_op (0);
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5175 "Received PID %u, ACK %u\n",
5176 pid, t->last_fwd_ack);
5180 tunnel_reset_timeout (t);
5182 if (dest_id == myid)
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5185 " it's for us! sending to clients...\n");
5186 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5187 send_subscribed_clients (message, &msg[1].header, t);
5188 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5191 ttl = ntohl (msg->ttl);
5192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5195 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5197 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5201 " not for us, retransmitting...\n");
5203 neighbor = tree_get_first_hop (t->tree, dest_id);
5204 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5205 cinfo->fwd_pid = pid;
5206 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5209 if (GNUNET_YES == t->nobuffer &&
5210 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5212 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5213 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5214 GNUNET_break_op (0);
5217 send_prebuilt_message (message, neighbor, t);
5218 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5224 * Core handler for mesh network traffic toward the owner of a tunnel
5226 * @param cls closure
5227 * @param message message
5228 * @param peer peer identity this notification is about
5230 * @return GNUNET_OK to keep the connection open,
5231 * GNUNET_SYSERR to close it (signal serious error)
5234 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5235 const struct GNUNET_MessageHeader *message)
5237 struct GNUNET_MESH_ToOrigin *msg;
5238 struct GNUNET_PeerIdentity id;
5239 struct MeshPeerInfo *peer_info;
5240 struct MeshTunnel *t;
5241 struct MeshTunnelChildInfo *cinfo;
5242 GNUNET_PEER_Id predecessor;
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5248 size = ntohs (message->size);
5249 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5250 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5252 GNUNET_break_op (0);
5255 msg = (struct GNUNET_MESH_ToOrigin *) message;
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5257 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5258 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5259 pid = ntohl (msg->pid);
5263 /* TODO notify that we dont know this tunnel (whom)? */
5264 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5266 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5267 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5271 cinfo = tunnel_get_neighbor_fc(t, peer);
5278 if (cinfo->bck_pid == pid)
5280 /* already seen this packet, drop */
5281 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5283 " Already seen pid %u, DROPPING!\n", pid);
5284 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5289 " pid %u not seen yet, forwarding\n", pid);
5290 cinfo->bck_pid = pid;
5292 if (NULL != t->owner)
5295 struct GNUNET_MESH_ToOrigin *copy;
5297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5298 " it's for us! sending to clients...\n");
5299 /* TODO signature verification */
5300 memcpy (cbuf, message, size);
5301 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5302 copy->tid = htonl (t->local_tid);
5304 copy->pid = htonl (t->bck_pid);
5305 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5306 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5307 ©->header, GNUNET_NO);
5308 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5312 " not for us, retransmitting...\n");
5314 peer_info = peer_info_get (&msg->oid);
5315 if (NULL == peer_info)
5317 /* unknown origin of tunnel */
5321 predecessor = tree_get_predecessor (t->tree);
5322 if (0 == predecessor)
5324 if (GNUNET_YES == t->destroy)
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5327 "to orig received on a dying tunnel %s [%X]\n",
5328 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5332 "unknown to origin at %s\n",
5333 GNUNET_i2s (&my_full_id));
5334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5338 "for tunnel %s [%X]\n",
5339 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5342 tree_debug (t->tree);
5345 GNUNET_PEER_resolve (predecessor, &id);
5346 send_prebuilt_message (message, &id, t);
5347 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5354 * Core handler for mesh network traffic point-to-point acks.
5356 * @param cls closure
5357 * @param message message
5358 * @param peer peer identity this notification is about
5360 * @return GNUNET_OK to keep the connection open,
5361 * GNUNET_SYSERR to close it (signal serious error)
5364 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5365 const struct GNUNET_MessageHeader *message)
5367 struct GNUNET_MESH_ACK *msg;
5368 struct MeshTunnel *t;
5371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5373 msg = (struct GNUNET_MESH_ACK *) message;
5375 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5379 /* TODO notify that we dont know this tunnel (whom)? */
5380 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5383 ack = ntohl (msg->pid);
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5386 /* Is this a forward or backward ACK? */
5387 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5389 struct MeshTunnelChildInfo *cinfo;
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5393 cinfo = tunnel_get_neighbor_fc (t, peer);
5394 cinfo->fwd_ack = ack;
5395 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5396 tunnel_unlock_fwd_queues (t);
5397 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5399 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5400 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5401 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5408 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5409 tunnel_unlock_bck_queue (t);
5416 * Core handler for mesh network traffic point-to-point ack polls.
5418 * @param cls closure
5419 * @param message message
5420 * @param peer peer identity this notification is about
5422 * @return GNUNET_OK to keep the connection open,
5423 * GNUNET_SYSERR to close it (signal serious error)
5426 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5427 const struct GNUNET_MessageHeader *message)
5429 struct GNUNET_MESH_Poll *msg;
5430 struct MeshTunnel *t;
5432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5435 msg = (struct GNUNET_MESH_Poll *) message;
5437 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5441 /* TODO notify that we dont know this tunnel (whom)? */
5442 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5443 GNUNET_break_op (0);
5447 /* Is this a forward or backward ACK? */
5448 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5450 struct MeshTunnelChildInfo *cinfo;
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5453 cinfo = tunnel_get_neighbor_fc (t, peer);
5454 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5455 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5460 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5467 * Core handler for path ACKs
5469 * @param cls closure
5470 * @param message message
5471 * @param peer peer identity this notification is about
5473 * @return GNUNET_OK to keep the connection open,
5474 * GNUNET_SYSERR to close it (signal serious error)
5477 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5478 const struct GNUNET_MessageHeader *message)
5480 struct GNUNET_MESH_PathACK *msg;
5481 struct GNUNET_PeerIdentity id;
5482 struct MeshPeerInfo *peer_info;
5483 struct MeshPeerPath *p;
5484 struct MeshTunnel *t;
5486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5487 GNUNET_i2s (&my_full_id));
5488 msg = (struct GNUNET_MESH_PathACK *) message;
5489 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5492 /* TODO notify that we don't know the tunnel */
5493 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5495 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5499 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5501 peer_info = peer_info_get (&msg->peer_id);
5502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5503 GNUNET_i2s (&msg->peer_id));
5504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5507 /* Add paths to peers? */
5508 p = tree_get_path_to_peer (t->tree, peer_info->id);
5511 path_add_to_peers (p, GNUNET_YES);
5519 /* Message for us? */
5520 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5523 if (NULL == t->owner)
5525 GNUNET_break_op (0);
5528 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5530 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5531 send_client_peer_connected (t, peer_info->id);
5533 if (NULL != peer_info->dhtget)
5535 GNUNET_DHT_get_stop (peer_info->dhtget);
5536 peer_info->dhtget = NULL;
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5542 " not for us, retransmitting...\n");
5543 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5544 peer_info = peer_info_get (&msg->oid);
5545 send_prebuilt_message (message, &id, t);
5551 * Core handler for mesh keepalives.
5553 * @param cls closure
5554 * @param message message
5555 * @param peer peer identity this notification is about
5556 * @return GNUNET_OK to keep the connection open,
5557 * GNUNET_SYSERR to close it (signal serious error)
5559 * TODO: Check who we got this from, to validate route.
5562 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5563 const struct GNUNET_MessageHeader *message)
5565 struct GNUNET_MESH_TunnelKeepAlive *msg;
5566 struct MeshTunnel *t;
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5571 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5572 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5576 /* TODO notify that we dont know that tunnel */
5577 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
5582 tunnel_reset_timeout (t);
5584 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5585 tunnel_send_multicast (t, message);
5592 * Functions to handle messages from core
5594 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5595 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5596 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5597 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5598 sizeof (struct GNUNET_MESH_PathBroken)},
5599 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5600 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5601 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5602 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5603 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5604 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5605 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5606 sizeof (struct GNUNET_MESH_ACK)},
5607 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5608 sizeof (struct GNUNET_MESH_Poll)},
5609 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5610 sizeof (struct GNUNET_MESH_PathACK)},
5616 /******************************************************************************/
5617 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5618 /******************************************************************************/
5623 * notify_client_connection_failure: notify a client that the connection to the
5624 * requested remote peer is not possible (for instance, no route found)
5625 * Function called when the socket is ready to queue more data. "buf" will be
5626 * NULL and "size" zero if the socket was closed for writing in the meantime.
5628 * @param cls closure
5629 * @param size number of bytes available in buf
5630 * @param buf where the callee should write the message
5631 * @return number of bytes written to buf
5634 notify_client_connection_failure (void *cls, size_t size, void *buf)
5637 struct MeshPeerInfo *peer_info;
5638 struct GNUNET_MESH_PeerControl *msg;
5639 struct GNUNET_PeerIdentity id;
5641 if (0 == size && NULL == buf)
5643 // TODO retry? cancel?
5647 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5648 peer_info = (struct MeshPeerInfo *) cls;
5649 msg = (struct GNUNET_MESH_PeerControl *) buf;
5650 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5651 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5652 // msg->tunnel_id = htonl(peer_info->t->tid);
5653 GNUNET_PEER_resolve (peer_info->id, &id);
5654 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5662 * Send keepalive packets for a peer
5664 * @param cls Closure (tunnel for which to send the keepalive).
5665 * @param tc Notification context.
5668 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5670 struct MeshTunnel *t = cls;
5671 struct GNUNET_MESH_TunnelKeepAlive *msg;
5672 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5675 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5676 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5682 "sending keepalive for tunnel %d\n", t->id.tid);
5684 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5685 msg->header.size = htons (size);
5686 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5687 msg->oid = my_full_id;
5688 msg->tid = htonl (t->id.tid);
5689 tunnel_send_multicast (t, &msg->header);
5691 t->path_refresh_task =
5692 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5693 tunnel_reset_timeout(t);
5698 * Function to process paths received for a new peer addition. The recorded
5699 * paths form the initial tunnel, which can be optimized later.
5700 * Called on each result obtained for the DHT search.
5702 * @param cls closure
5703 * @param exp when will this value expire
5704 * @param key key of the result
5705 * @param get_path path of the get request
5706 * @param get_path_length lenght of get_path
5707 * @param put_path path of the put request
5708 * @param put_path_length length of the put_path
5709 * @param type type of the result
5710 * @param size number of bytes in data
5711 * @param data pointer to the result data
5713 * TODO: re-issue the request after certain time? cancel after X results?
5716 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5717 const struct GNUNET_HashCode * key,
5718 const struct GNUNET_PeerIdentity *get_path,
5719 unsigned int get_path_length,
5720 const struct GNUNET_PeerIdentity *put_path,
5721 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5722 size_t size, const void *data)
5724 struct MeshPathInfo *path_info = cls;
5725 struct MeshPeerPath *p;
5726 struct GNUNET_PeerIdentity pi;
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5730 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5733 p = path_build_from_dht (get_path, get_path_length,
5734 put_path, put_path_length);
5735 path_add_to_peers (p, GNUNET_NO);
5737 for (i = 0; i < path_info->peer->ntunnels; i++)
5739 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5740 peer_info_connect (path_info->peer, path_info->t);
5747 /******************************************************************************/
5748 /********************* MESH LOCAL HANDLES **************************/
5749 /******************************************************************************/
5753 * Handler for client disconnection
5755 * @param cls closure
5756 * @param client identification of the client; NULL
5757 * for the last call when the server is destroyed
5760 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5762 struct MeshClient *c;
5763 struct MeshClient *next;
5766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5776 if (c->handle != client)
5778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5784 GNUNET_SERVER_client_drop (c->handle);
5785 c->shutting_down = GNUNET_YES;
5786 GNUNET_assert (NULL != c->own_tunnels);
5787 GNUNET_assert (NULL != c->incoming_tunnels);
5788 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5789 &tunnel_destroy_iterator, c);
5790 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5791 &tunnel_destroy_iterator, c);
5792 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5793 &tunnel_destroy_iterator, c);
5794 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5795 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5796 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5798 if (NULL != c->types)
5799 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5800 for (i = 0; i < c->n_regex; i++)
5802 GNUNET_free (c->regexes[i].regex);
5803 if (NULL != c->regexes[i].h)
5804 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
5806 GNUNET_free_non_null (c->regexes);
5807 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5808 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5810 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5813 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5822 * Handler for new clients
5824 * @param cls closure
5825 * @param client identification of the client
5826 * @param message the actual message, which includes messages the client wants
5829 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5830 const struct GNUNET_MessageHeader *message)
5832 struct GNUNET_MESH_ClientConnect *cc_msg;
5833 struct MeshClient *c;
5839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5840 /* Check data sanity */
5841 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5842 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5843 ntypes = ntohs (cc_msg->types);
5844 if (size != ntypes * sizeof (uint16_t))
5847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5851 /* Create new client structure */
5852 c = GNUNET_malloc (sizeof (struct MeshClient));
5853 c->id = next_client_id++;
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5856 GNUNET_SERVER_client_keep (client);
5860 struct GNUNET_HashCode hc;
5862 t = (uint16_t *) &cc_msg[1];
5863 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
5864 for (i = 0; i < ntypes; i++)
5867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5868 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5870 /* store in clients hashmap */
5871 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5872 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5873 /* store in global hashmap */
5874 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5875 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
5880 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5881 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5882 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5883 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5884 GNUNET_SERVER_notification_context_add (nc, client);
5885 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5887 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5893 * Handler for requests of new tunnels
5895 * @param cls closure
5896 * @param client identification of the client
5897 * @param message the actual message
5900 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5901 const struct GNUNET_MessageHeader *message)
5903 struct GNUNET_MESH_TunnelMessage *t_msg;
5904 struct MeshTunnel *t;
5905 struct MeshClient *c;
5906 MESH_TunnelNumber tid;
5908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5910 /* Sanity check for client registration */
5911 if (NULL == (c = client_get (client)))
5914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5919 /* Message sanity check */
5920 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5927 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5928 /* Sanity check for tunnel numbering */
5929 tid = ntohl (t_msg->tunnel_id);
5930 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5936 /* Sanity check for duplicate tunnel IDs */
5937 if (NULL != tunnel_get_by_local_id (c, tid))
5940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5944 while (NULL != tunnel_get_by_pi (myid, next_tid))
5945 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5946 t = tunnel_new (myid, next_tid++, c, tid);
5949 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5954 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5956 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5965 * Handler for requests of deleting tunnels
5967 * @param cls closure
5968 * @param client identification of the client
5969 * @param message the actual message
5972 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5973 const struct GNUNET_MessageHeader *message)
5975 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5976 struct MeshClient *c;
5977 struct MeshTunnel *t;
5978 MESH_TunnelNumber tid;
5980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5981 "Got a DESTROY TUNNEL from client!\n");
5983 /* Sanity check for client registration */
5984 if (NULL == (c = client_get (client)))
5987 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5992 /* Message sanity check */
5993 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6000 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6002 /* Retrieve tunnel */
6003 tid = ntohl (tunnel_msg->tunnel_id);
6004 t = tunnel_get_by_local_id(c, tid);
6007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6012 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6014 client_ignore_tunnel (c, t);
6015 tunnel_destroy_empty (t);
6016 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6019 send_client_tunnel_disconnect (t, c);
6020 client_delete_tunnel (c, t);
6022 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6024 tunnel_send_destroy (t, 0);
6025 t->destroy = GNUNET_YES;
6026 /* The tunnel will be destroyed when the last message is transmitted. */
6027 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6033 * Handler for requests of seeting tunnel's buffering policy.
6035 * @param cls Closure (unused).
6036 * @param client Identification of the client.
6037 * @param message The actual message.
6040 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6041 const struct GNUNET_MessageHeader *message)
6043 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6044 struct MeshClient *c;
6045 struct MeshTunnel *t;
6046 MESH_TunnelNumber tid;
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6049 "Got a BUFFER request from client!\n");
6051 /* Sanity check for client registration */
6052 if (NULL == (c = client_get (client)))
6055 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6060 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6062 /* Retrieve tunnel */
6063 tid = ntohl (tunnel_msg->tunnel_id);
6064 t = tunnel_get_by_local_id(c, tid);
6067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6073 switch (ntohs(message->type))
6075 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6076 t->nobuffer = GNUNET_NO;
6078 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6079 t->nobuffer = GNUNET_YES;
6085 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6090 * Handler for client traffic directed to one peer
6092 * @param cls closure
6093 * @param client identification of the client
6094 * @param message the actual message
6097 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6098 const struct GNUNET_MessageHeader *message)
6100 struct MeshClient *c;
6101 struct MeshTunnel *t;
6102 struct GNUNET_MESH_Unicast *data_msg;
6103 MESH_TunnelNumber tid;
6106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6107 "Got a unicast request from a client!\n");
6109 /* Sanity check for client registration */
6110 if (NULL == (c = client_get (client)))
6113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6118 data_msg = (struct GNUNET_MESH_Unicast *) message;
6120 /* Sanity check for message size */
6121 size = ntohs (message->size);
6122 if (sizeof (struct GNUNET_MESH_Unicast) +
6123 sizeof (struct GNUNET_MessageHeader) > size)
6126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6130 /* Tunnel exists? */
6131 tid = ntohl (data_msg->tid);
6132 t = tunnel_get_by_local_id (c, tid);
6136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6140 /* Is it a local tunnel? Then, does client own the tunnel? */
6141 if (t->owner->handle != client)
6144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6148 /* PID should be as expected */
6149 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6153 "Unicast PID, expected %u, got %u\n",
6154 t->fwd_pid + 1, ntohl (data_msg->pid));
6155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6159 /* Ok, everything is correct, send the message
6160 * (pretend we got it from a mesh peer)
6163 /* Work around const limitation */
6164 char buf[ntohs (message->size)] GNUNET_ALIGN;
6165 struct GNUNET_MESH_Unicast *copy;
6167 copy = (struct GNUNET_MESH_Unicast *) buf;
6168 memcpy (buf, data_msg, size);
6169 copy->oid = my_full_id;
6170 copy->tid = htonl (t->id.tid);
6171 copy->ttl = htonl (default_ttl);
6172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6173 " calling generic handler...\n");
6174 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
6176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6177 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6184 * Handler for client traffic directed to the origin
6186 * @param cls closure
6187 * @param client identification of the client
6188 * @param message the actual message
6191 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6192 const struct GNUNET_MessageHeader *message)
6194 struct GNUNET_MESH_ToOrigin *data_msg;
6195 struct MeshTunnelClientInfo *clinfo;
6196 struct MeshClient *c;
6197 struct MeshTunnel *t;
6198 MESH_TunnelNumber tid;
6201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6202 "Got a ToOrigin request from a client!\n");
6203 /* Sanity check for client registration */
6204 if (NULL == (c = client_get (client)))
6207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6212 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6214 /* Sanity check for message size */
6215 size = ntohs (message->size);
6216 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6217 sizeof (struct GNUNET_MessageHeader) > size)
6220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6224 /* Tunnel exists? */
6225 tid = ntohl (data_msg->tid);
6226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6227 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6233 t = tunnel_get_by_local_id (c, tid);
6236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
6237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6239 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6243 /* It should be sent by someone who has this as incoming tunnel. */
6244 if (GNUNET_NO == client_knows_tunnel (c, t))
6247 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6251 /* PID should be as expected */
6252 clinfo = tunnel_get_client_fc (t, c);
6253 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6257 "To Origin PID, expected %u, got %u\n",
6258 clinfo->bck_pid + 1,
6259 ntohl (data_msg->pid));
6260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6265 /* Ok, everything is correct, send the message
6266 * (pretend we got it from a mesh peer)
6269 char buf[ntohs (message->size)] GNUNET_ALIGN;
6270 struct GNUNET_MESH_ToOrigin *copy;
6272 /* Work around const limitation */
6273 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6274 memcpy (buf, data_msg, size);
6275 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6276 copy->tid = htonl (t->id.tid);
6277 copy->ttl = htonl (default_ttl);
6278 copy->pid = htonl (t->bck_pid + 1);
6280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6281 " calling generic handler...\n");
6282 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
6284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6291 * Handler for client's ACKs for payload traffic.
6293 * @param cls Closure (unused).
6294 * @param client Identification of the client.
6295 * @param message The actual message.
6298 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6299 const struct GNUNET_MessageHeader *message)
6301 struct GNUNET_MESH_LocalAck *msg;
6302 struct MeshTunnel *t;
6303 struct MeshClient *c;
6304 MESH_TunnelNumber tid;
6307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6308 /* Sanity check for client registration */
6309 if (NULL == (c = client_get (client)))
6312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6317 msg = (struct GNUNET_MESH_LocalAck *) message;
6319 /* Tunnel exists? */
6320 tid = ntohl (msg->tunnel_id);
6321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6322 t = tunnel_get_by_local_id (c, tid);
6326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
6327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6332 ack = ntohl (msg->max_pid);
6333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
6335 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
6336 if (NULL != t->owner && t->owner->handle == client)
6338 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
6340 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6344 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
6345 tunnel_set_client_fwd_ack (t, c, ack);
6346 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6357 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6359 * @param cls Closure (client handle).
6360 * @param key Key (hashed tunnel ID, unused).
6361 * @param value Tunnel info.
6363 * @return GNUNET_YES, to keep iterating.
6366 monitor_all_tunnels_iterator (void *cls,
6367 const struct GNUNET_HashCode * key,
6370 struct GNUNET_SERVER_Client *client = cls;
6371 struct MeshTunnel *t = value;
6372 struct GNUNET_MESH_LocalMonitor *msg;
6374 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6375 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
6376 msg->tunnel_id = htonl (t->id.tid);
6377 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6378 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6379 GNUNET_PEER_resolve (t->peer, &msg->destination);
6381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6382 "* sending info about tunnel %s [%u]\n",
6383 GNUNET_i2s (&msg->owner), t->id.tid);
6385 GNUNET_SERVER_notification_context_unicast (nc, client,
6386 &msg->header, GNUNET_NO);
6392 * Handler for client's MONITOR request.
6394 * @param cls Closure (unused).
6395 * @param client Identification of the client.
6396 * @param message The actual message.
6399 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6400 const struct GNUNET_MessageHeader *message)
6402 struct MeshClient *c;
6404 /* Sanity check for client registration */
6405 if (NULL == (c = client_get (client)))
6408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6412 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6413 "Received get tunnels request from client %u\n",
6415 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6416 monitor_all_tunnels_iterator,
6418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6419 "Get tunnels request from client %u completed\n",
6421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6426 * Handler for client's MONITOR_TUNNEL request.
6428 * @param cls Closure (unused).
6429 * @param client Identification of the client.
6430 * @param message The actual message.
6433 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6434 const struct GNUNET_MessageHeader *message)
6436 const struct GNUNET_MESH_LocalMonitor *msg;
6437 struct GNUNET_MESH_LocalMonitor *resp;
6438 struct MeshClient *c;
6439 struct MeshTunnel *t;
6441 /* Sanity check for client registration */
6442 if (NULL == (c = client_get (client)))
6445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6449 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6450 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6451 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6454 ntohl (msg->tunnel_id));
6455 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
6458 /* We don't know the tunnel FIXME */
6459 struct GNUNET_MESH_LocalMonitor warn;
6462 GNUNET_SERVER_notification_context_unicast (nc, client,
6465 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6469 /* Initialize context */
6470 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6472 GNUNET_PEER_resolve (t->peer, &resp->destination);
6473 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6474 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6475 &resp->header, GNUNET_NO);
6478 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6479 "Monitor tunnel request from client %u completed\n",
6481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6486 * Functions to handle messages from clients
6488 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6489 {&handle_local_new_client, NULL,
6490 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6491 {&handle_local_tunnel_create, NULL,
6492 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6493 sizeof (struct GNUNET_MESH_TunnelMessage)},
6494 {&handle_local_tunnel_destroy, NULL,
6495 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6496 sizeof (struct GNUNET_MESH_TunnelMessage)},
6497 {&handle_local_tunnel_buffer, NULL,
6498 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6499 sizeof (struct GNUNET_MESH_TunnelMessage)},
6500 {&handle_local_tunnel_buffer, NULL,
6501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6502 sizeof (struct GNUNET_MESH_TunnelMessage)},
6503 {&handle_local_unicast, NULL,
6504 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6505 {&handle_local_to_origin, NULL,
6506 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6507 {&handle_local_ack, NULL,
6508 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6509 sizeof (struct GNUNET_MESH_LocalAck)},
6510 {&handle_local_get_tunnels, NULL,
6511 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6512 sizeof (struct GNUNET_MessageHeader)},
6513 {&handle_local_show_tunnel, NULL,
6514 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6515 sizeof (struct GNUNET_MESH_LocalMonitor)},
6521 * To be called on core init/fail.
6523 * @param cls service closure
6524 * @param server handle to the server for this service
6525 * @param identity the public identity of this peer
6528 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6529 const struct GNUNET_PeerIdentity *identity)
6532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6533 core_handle = server;
6534 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6540 GNUNET_i2s (identity));
6541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6543 GNUNET_i2s (&my_full_id));
6544 GNUNET_SCHEDULER_shutdown (); // Try gracefully
6546 GNUNET_abort(); // Try harder
6553 * Method called whenever a given peer connects.
6555 * @param cls closure
6556 * @param peer peer identity this notification is about
6559 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6561 struct MeshPeerInfo *peer_info;
6562 struct MeshPeerPath *path;
6564 DEBUG_CONN ("Peer connected\n");
6565 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6566 peer_info = peer_info_get (peer);
6567 if (myid == peer_info->id)
6569 DEBUG_CONN (" (self)\n");
6574 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6576 path = path_new (2);
6577 path->peers[0] = myid;
6578 path->peers[1] = peer_info->id;
6579 GNUNET_PEER_change_rc (myid, 1);
6580 GNUNET_PEER_change_rc (peer_info->id, 1);
6581 peer_info_add_path (peer_info, path, GNUNET_YES);
6582 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6588 * Method called whenever a peer disconnects.
6590 * @param cls closure
6591 * @param peer peer identity this notification is about
6594 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6596 struct MeshPeerInfo *pi;
6597 struct MeshPeerQueue *q;
6598 struct MeshPeerQueue *n;
6600 DEBUG_CONN ("Peer disconnected\n");
6601 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6611 /* TODO try to reroute this traffic instead */
6612 queue_destroy(q, GNUNET_YES);
6615 if (NULL != pi->core_transmit)
6617 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
6618 pi->core_transmit = NULL;
6620 peer_info_remove_path (pi, pi->id, myid);
6623 DEBUG_CONN (" (self)\n");
6625 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6630 /******************************************************************************/
6631 /************************ MAIN FUNCTIONS ****************************/
6632 /******************************************************************************/
6635 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6637 * @param cls closure
6638 * @param key current key code
6639 * @param value value in the hash map
6640 * @return GNUNET_YES if we should continue to iterate,
6644 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6646 struct MeshTunnel *t = value;
6653 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6655 * @param cls closure
6656 * @param key current key code
6657 * @param value value in the hash map
6658 * @return GNUNET_YES if we should continue to iterate,
6662 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6664 struct MeshPeerInfo *p = value;
6665 struct MeshPeerQueue *q;
6666 struct MeshPeerQueue *n;
6674 queue_destroy(q, GNUNET_YES);
6678 peer_info_destroy (p);
6684 * Task run during shutdown.
6690 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6694 if (core_handle != NULL)
6696 GNUNET_CORE_disconnect (core_handle);
6701 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
6704 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6705 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6706 if (dht_handle != NULL)
6708 GNUNET_DHT_disconnect (dht_handle);
6713 GNUNET_SERVER_notification_context_destroy (nc);
6716 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6718 GNUNET_SCHEDULER_cancel (announce_id_task);
6719 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6726 * Callback for hostkey read/generation.
6728 * @param cls Closure (Configuration handle).
6729 * @param pk The ECC private key.
6730 * @param emsg Error message, if any.
6733 key_generation_cb (void *cls,
6734 struct GNUNET_CRYPTO_EccPrivateKey *pk,
6737 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6738 struct MeshPeerInfo *peer;
6739 struct MeshPeerPath *p;
6744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6745 _("Mesh service could not access hostkey: %s. Exiting.\n"),
6747 GNUNET_SCHEDULER_shutdown ();
6750 my_private_key = pk;
6751 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6752 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6753 &my_full_id.hashPubKey);
6754 myid = GNUNET_PEER_intern (&my_full_id);
6755 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6756 "Mesh for peer [%s] starting\n",
6757 GNUNET_i2s(&my_full_id));
6759 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6760 NULL, /* Closure passed to MESH functions */
6761 &core_init, /* Call core_init once connected */
6762 &core_connect, /* Handle connects */
6763 &core_disconnect, /* remove peers on disconnects */
6764 NULL, /* Don't notify about all incoming messages */
6765 GNUNET_NO, /* For header only in notification */
6766 NULL, /* Don't notify about all outbound messages */
6767 GNUNET_NO, /* For header-only out notification */
6768 core_handlers); /* Register these handlers */
6770 if (core_handle == NULL)
6773 GNUNET_SCHEDULER_shutdown ();
6778 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6781 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6782 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6783 GNUNET_SERVER_disconnect_notify (server_handle,
6784 &handle_local_client_disconnect, NULL);
6788 clients_tail = NULL;
6791 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6793 /* Create a peer_info for the local peer */
6794 peer = peer_info_get (&my_full_id);
6797 GNUNET_PEER_change_rc (myid, 1);
6798 peer_info_add_path (peer, p, GNUNET_YES);
6799 GNUNET_SERVER_resume (server_handle);
6800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6805 * Process mesh requests.
6807 * @param cls closure
6808 * @param server the initialized server
6809 * @param c configuration to use
6812 run (void *cls, struct GNUNET_SERVER_Handle *server,
6813 const struct GNUNET_CONFIGURATION_Handle *c)
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6818 server_handle = server;
6821 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6824 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6826 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6827 "mesh", "peer/privatekey");
6828 GNUNET_SCHEDULER_shutdown ();
6833 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6834 &refresh_path_time))
6836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6838 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6839 "mesh", "refresh path time");
6840 GNUNET_SCHEDULER_shutdown ();
6845 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6846 &app_announce_time))
6848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6850 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6851 "mesh", "app announce time");
6852 GNUNET_SCHEDULER_shutdown ();
6855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6856 "APP_ANNOUNCE_TIME %llu ms\n",
6857 app_announce_time.rel_value);
6859 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6864 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6865 "mesh", "id announce time");
6866 GNUNET_SCHEDULER_shutdown ();
6871 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6876 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6877 "mesh", "connect timeout");
6878 GNUNET_SCHEDULER_shutdown ();
6883 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6886 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6888 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6889 "mesh", "max msgs queue");
6890 GNUNET_SCHEDULER_shutdown ();
6895 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6898 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6900 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6901 "mesh", "max tunnels");
6902 GNUNET_SCHEDULER_shutdown ();
6907 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6910 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6912 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6913 "mesh", "default ttl", 64);
6918 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6922 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6923 "mesh", "max peers", 1000);
6928 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6929 &dht_replication_level))
6931 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6933 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6934 "mesh", "dht replication level", 3);
6935 dht_replication_level = 3;
6938 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6939 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6940 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6941 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6943 dht_handle = GNUNET_DHT_connect (c, 64);
6944 if (NULL == dht_handle)
6948 stats = GNUNET_STATISTICS_create ("mesh", c);
6950 GNUNET_SERVER_suspend (server_handle);
6951 /* Scheduled the task to clean up when shutdown is called */
6952 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6954 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
6957 GNUNET_free (keyfile);
6962 * The main function for the mesh service.
6964 * @param argc number of arguments from the command line
6965 * @param argv command line arguments
6966 * @return 0 ok, 1 on error
6969 main (int argc, char *const *argv)
6974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6975 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6977 ret = (GNUNET_OK == r) ? 0 : 1;
6978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
6982 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6983 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
6984 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);