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 "mesh_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_service.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,\
69 #if MESH_DEBUG_CONNECTION
70 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
72 #define DEBUG_CONN(...)
76 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
78 #define DEBUG_DHT(...)
82 #define DEBUG_REGEX(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
84 #define DEBUG_REGEX(...)
91 struct timespec __mesh_start;
92 struct timespec __mesh_end;
93 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
94 #define INTERVAL_END \
96 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
97 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
98 if (__diff < 0) __diff += 1000000000;\
102 #define INTERVAL_SHOW \
104 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
106 #define INTERVAL_START
108 #define INTERVAL_SHOW
111 /******************************************************************************/
112 /************************ DATA STRUCTURES ****************************/
113 /******************************************************************************/
115 /** FWD declaration */
121 * Struct representing a piece of data being sent to other peers
125 /** Tunnel it belongs to. */
126 struct MeshTunnel *t;
128 /** How many remaining neighbors still hav't got it. */
129 unsigned int reference_counter;
131 /** How many remaining neighbors we need to send this to. */
132 unsigned int total_out;
134 /** Size of the data. */
143 * Struct containing info about a queued transmission to this peer
150 struct MeshPeerQueue *next;
155 struct MeshPeerQueue *prev;
158 * Peer this transmission is directed to.
160 struct MeshPeerInfo *peer;
163 * Tunnel this message belongs to.
165 struct MeshTunnel *tunnel;
168 * Pointer to info stucture used as cls.
178 * Size of the message
185 * Struct to store regex information announced by clients.
187 struct MeshRegexDescriptor
190 * Regular expression itself.
195 * How many characters per edge can we squeeze?
197 uint16_t compression;
200 * Handle to announce the regex.
202 struct GNUNET_REGEX_Announcement *h;
207 * Struct to keep information of searches of services described by a regex
208 * using a user-provided string service description.
210 struct MeshRegexSearchInfo
213 * Which tunnel is this for
215 struct MeshTunnel *t;
218 * User provided description of the searched service.
223 * Regex search handle.
225 struct GNUNET_REGEX_Search *search_handle;
228 * Peer that is connecting via connect_by_string. When connected, free ctx.
233 * Other peers that are found but not yet being connected to.
235 GNUNET_PEER_Id *peers;
238 * Number of elements in peers.
240 unsigned int n_peers;
243 * Next peer to try to connect to.
248 * Timeout for a connect attempt.
249 * When reached, try to connect to a different peer, if any. If not,
250 * try the same peer again.
252 GNUNET_SCHEDULER_TaskIdentifier timeout;
258 * Struct containing all info possibly needed to build a package when called
261 struct MeshTransmissionDescriptor
263 /** ID of the tunnel this packet travels in */
264 struct MESH_TunnelID *origin;
266 /** Who was this message being sent to */
267 struct MeshPeerInfo *peer;
269 /** Ultimate destination of the packet */
270 GNUNET_PEER_Id destination;
272 /** Data descriptor */
273 struct MeshData* mesh_data;
278 * Struct containing all information regarding a given peer
288 * Last time we heard from this peer
290 struct GNUNET_TIME_Absolute last_contact;
293 * Task handler for delayed connect task;
295 GNUNET_SCHEDULER_TaskIdentifier connect_task;
298 * Number of attempts to reconnect so far
300 int n_reconnect_attempts;
303 * Paths to reach the peer, ordered by ascending hop count
305 struct MeshPeerPath *path_head;
308 * Paths to reach the peer, ordered by ascending hop count
310 struct MeshPeerPath *path_tail;
313 * Handle to stop the DHT search for a path to this peer
315 struct GNUNET_DHT_GetHandle *dhtget;
318 * Closure given to the DHT GET
320 struct MeshPathInfo *dhtgetcls;
323 * Array of tunnels this peer participates in
324 * (most probably a small amount, therefore not a hashmap)
325 * When the path to the peer changes, notify these tunnels to let them
326 * re-adjust their path trees.
328 struct MeshTunnel **tunnels;
331 * Number of tunnels this peers participates in
333 unsigned int ntunnels;
336 * Transmission queue to core DLL head
338 struct MeshPeerQueue *queue_head;
341 * Transmission queue to core DLL tail
343 struct MeshPeerQueue *queue_tail;
346 * How many messages are in the queue to this peer.
348 unsigned int queue_n;
351 * Handle to for queued transmissions
353 struct GNUNET_CORE_TransmitHandle *core_transmit;
358 * Globally unique tunnel identification (owner + number)
359 * DO NOT USE OVER THE NETWORK
364 * Node that owns the tunnel
369 * Tunnel number to differentiate all the tunnels owned by the node oid
370 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
372 MESH_TunnelNumber tid;
377 * Struct containing all information regarding a tunnel
378 * For an intermediate node the improtant info used will be:
379 * - id Tunnel unique identification
380 * - paths[0] To know where to send it next
381 * - metainfo: ready, speeds, accounting
388 struct MESH_TunnelID id;
391 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
393 MESH_TunnelNumber local_tid;
396 * Local tunnel number for local destination clients (incoming number)
397 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
400 MESH_TunnelNumber local_tid_dest;
403 * Is the speed on the tunnel limited to the slowest peer?
408 * Is the tunnel bufferless (minimum latency)?
413 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
418 * Packet ID of the last bck packet sent (unique counter per hop).
423 * SKIP value for this tunnel.
428 * Force sending ACK? Flag to allow duplicate ACK on POLL.
433 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
434 * Contains the Flow Control info: FWD ACK value received,
435 * last BCK ACK sent, PID and SKIP values.
437 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
440 * Last ACK sent towards the origin (for traffic towards leaf node).
442 uint32_t last_fwd_ack;
445 * BCK ACK value received from the hop towards the owner of the tunnel,
446 * (previous node / owner): up to what message PID can we sent back to him.
451 * How many messages are in the forward queue (towards leaves).
453 unsigned int fwd_queue_n;
456 * How many messages do we accept in the forward queue.
458 unsigned int fwd_queue_max;
461 * How many messages are in the backward queue (towards origin).
463 unsigned int bck_queue_n;
466 * How many messages do we accept in the backward queue.
468 unsigned int bck_queue_max;
471 * Task to poll peer in case of a stall.
473 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
476 * Last time the tunnel was used
478 struct GNUNET_TIME_Absolute timestamp;
481 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
482 * containing peers added by id or by type, not intermediate peers.
484 struct GNUNET_CONTAINER_MultiHashMap *peers;
487 * Number of peers that are connected and potentially ready to receive data
489 unsigned int peers_ready;
492 * Client owner of the tunnel, if any
494 struct MeshClient *owner;
497 * Clients that have been informed about and want to stay in the tunnel.
499 struct MeshClient **clients;
502 * Flow control info for each client.
504 struct MeshTunnelClientInfo *clients_fc;
507 * Number of elements in clients/clients_fc
509 unsigned int nclients;
512 * Clients that have been informed but requested to leave the tunnel.
514 struct MeshClient **ignore;
517 * Number of elements in clients
519 unsigned int nignore;
524 GNUNET_PEER_Id *blacklisted;
527 * Number of elements in blacklisted
529 unsigned int nblacklisted;
532 * Bloomfilter (for peer identities) to stop circular routes
534 char bloomfilter[MESH_BLOOM_SIZE];
539 struct MeshTunnelTree *tree;
542 * Application type we are looking for in this tunnel
544 GNUNET_MESH_ApplicationType type;
547 * Used to search peers offering a service
549 struct GNUNET_DHT_GetHandle *dht_get_type;
552 * Handle for the regex search for a connect_by_string
554 struct MeshRegexSearchInfo *regex_search;
557 * Task to keep the used paths alive
559 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
562 * Task to destroy the tunnel after timeout
564 * FIXME: merge the two? a tunnel will have either
565 * a path refresh OR a timeout, never both!
567 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
570 * Flag to signal the destruction of the tunnel.
571 * If this is set GNUNET_YES the tunnel will be destroyed
572 * when the queue is empty.
577 * Total messages pending for this tunnels, payload or not.
579 unsigned int pending_messages;
582 * If the tunnel is empty, destoy it.
584 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
589 * Info about a child node in a tunnel, needed to perform flow control.
591 struct MeshTunnelChildInfo
594 * ID of the child node.
614 * Maximum PID allowed (FWD ACK received).
619 * Last ACK sent to that child (BCK ACK).
624 * Circular buffer pointing to MeshPeerQueue elements for all
625 * payload traffic going to this child.
626 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
628 struct MeshPeerQueue **send_buffer;
631 * Index of the oldest element in the send_buffer.
633 unsigned int send_buffer_start;
636 * How many elements are already in the buffer.
638 unsigned int send_buffer_n;
641 * Tunnel this info is about
643 struct MeshTunnel *t;
646 * Task to poll peer in case of a stall.
648 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
651 * Time to use for next polling call.
653 struct GNUNET_TIME_Relative fc_poll_time;
658 * Info about a leaf client of a tunnel, needed to perform flow control.
660 struct MeshTunnelClientInfo
663 * PID of the last packet sent to the client (FWD).
668 * PID of the last packet received from the client (BCK).
673 * Maximum PID allowed (FWD ACK received).
678 * Last ACK sent to that child (BCK ACK).
686 * Info collected during iteration of child nodes in order to get the ACK value
689 struct MeshTunnelChildIteratorContext
692 * Tunnel whose info is being collected.
694 struct MeshTunnel *t;
697 * Is this context initialized? Is the value in max_child_ack valid?
702 * Maximum child ACK so far.
704 uint32_t max_child_ack;
707 * Number of children nodes
709 unsigned int nchildren;
714 * Info needed to work with tunnel paths and peers
721 struct MeshTunnel *t;
724 * Neighbouring peer to whom we send the packet to
726 struct MeshPeerInfo *peer;
731 struct MeshPeerPath *path;
736 * Struct containing information about a client of the service
743 struct MeshClient *next;
748 struct MeshClient *prev;
751 * Tunnels that belong to this client, indexed by local id
753 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
756 * Tunnels this client has accepted, indexed by incoming local id
758 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
761 * Tunnels this client has rejected, indexed by incoming local id
763 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
765 * Handle to communicate with the client
767 struct GNUNET_SERVER_Client *handle;
770 * Applications that this client has claimed to provide: H(app) = app.
772 struct GNUNET_CONTAINER_MultiHashMap *apps;
775 * Messages that this client has declared interest in
777 struct GNUNET_CONTAINER_MultiHashMap *types;
780 * Whether the client is active or shutting down (don't send confirmations
781 * to a client that is shutting down.
786 * ID of the client, mainly for debug messages
791 * Regular expressions describing the services offered by this client.
793 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
796 * Number of regular expressions in regexes.
798 unsigned int n_regex;
801 * Task to refresh all regular expresions in the DHT.
803 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
806 * Tmp store for partially retrieved regex.
813 /******************************************************************************/
814 /************************ DEBUG FUNCTIONS ****************************/
815 /******************************************************************************/
819 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
820 * @param cls string to print
821 * @param success GNUNET_OK if the PUT was transmitted,
822 * GNUNET_NO on timeout,
823 * GNUNET_SYSERR on disconnect from service
824 * after the PUT message was transmitted
825 * (so we don't know if it was received or not)
830 mesh_debug (void *cls, int success)
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
838 unsigned int debug_fwd_ack;
839 unsigned int debug_bck_ack;
843 /******************************************************************************/
844 /*********************** GLOBAL VARIABLES ****************************/
845 /******************************************************************************/
847 /************************** Configuration parameters **************************/
850 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
852 static struct GNUNET_TIME_Relative refresh_path_time;
855 * How often to PUT local application numbers in the DHT.
857 static struct GNUNET_TIME_Relative app_announce_time;
860 * How often to PUT own ID in the DHT.
862 static struct GNUNET_TIME_Relative id_announce_time;
865 * Maximum time allowed to connect to a peer found by string.
867 static struct GNUNET_TIME_Relative connect_timeout;
870 * Default TTL for payload packets.
872 static unsigned long long default_ttl;
875 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
877 static unsigned long long dht_replication_level;
880 * How many tunnels are we willing to maintain.
881 * Local tunnels are always allowed, even if there are more tunnels than max.
883 static unsigned long long max_tunnels;
886 * How many messages *in total* are we willing to queue, divided by number of
887 * tunnels to get tunnel queue size.
889 static unsigned long long max_msgs_queue;
892 * How many peers do we want to remember?
894 static unsigned long long max_peers;
897 /*************************** Static global variables **************************/
900 * Hostkey generation context
902 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
905 * DLL with all the clients, head.
907 static struct MeshClient *clients;
910 * DLL with all the clients, tail.
912 static struct MeshClient *clients_tail;
915 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
917 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
920 * Number of tunnels known.
922 static unsigned long long n_tunnels;
925 * Tunnels incoming, indexed by MESH_TunnelNumber
926 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
928 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
931 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
933 static struct GNUNET_CONTAINER_MultiHashMap *peers;
936 * Configuration handle
938 static const struct GNUNET_CONFIGURATION_Handle *cfg;
941 * Handle to communicate with core.
943 static struct GNUNET_CORE_Handle *core_handle;
948 static struct GNUNET_DHT_Handle *dht_handle;
953 static struct GNUNET_SERVER_Handle *server_handle;
956 * Handle to the statistics service.
958 static struct GNUNET_STATISTICS_Handle *stats;
961 * Notification context, to send messages to local clients.
963 static struct GNUNET_SERVER_NotificationContext *nc;
966 * Local peer own ID (memory efficient handle).
968 static GNUNET_PEER_Id myid;
971 * Local peer own ID (full value).
973 static struct GNUNET_PeerIdentity my_full_id;
978 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
983 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
986 * Tunnel ID for the next created tunnel (global tunnel number).
988 static MESH_TunnelNumber next_tid;
991 * Tunnel ID for the next incoming tunnel (local tunnel number).
993 static MESH_TunnelNumber next_local_tid;
996 * All application types provided by this peer: H(app) = *Client.
998 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1001 * All message types clients of this peer are interested in.
1003 static struct GNUNET_CONTAINER_MultiHashMap *types;
1006 * Task to periodically announce provided applications.
1008 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1011 * Task to periodically announce itself in the network.
1013 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1016 * Next ID to assign to a client.
1018 unsigned int next_client_id;
1021 /******************************************************************************/
1022 /*********************** DECLARATIONS **************************/
1023 /******************************************************************************/
1026 * Function to process paths received for a new peer addition. The recorded
1027 * paths form the initial tunnel, which can be optimized later.
1028 * Called on each result obtained for the DHT search.
1030 * @param cls closure
1031 * @param exp when will this value expire
1032 * @param key key of the result
1033 * @param type type of the result
1034 * @param size number of bytes in data
1035 * @param data pointer to the result data
1038 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1039 const struct GNUNET_HashCode * key,
1040 const struct GNUNET_PeerIdentity *get_path,
1041 unsigned int get_path_length,
1042 const struct GNUNET_PeerIdentity *put_path,
1043 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1044 size_t size, const void *data);
1048 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1049 * and insert it in the appropiate structures if the peer is not known yet.
1051 * @param peer Full identity of the peer.
1053 * @return Existing or newly created peer info.
1055 static struct MeshPeerInfo *
1056 peer_info_get (const struct GNUNET_PeerIdentity *peer);
1060 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1061 * and insert it in the appropiate structures if the peer is not known yet.
1063 * @param peer Short identity of the peer.
1065 * @return Existing or newly created peer info.
1067 static struct MeshPeerInfo *
1068 peer_info_get_short (const GNUNET_PEER_Id peer);
1072 * Try to establish a new connection to this peer.
1073 * Use the best path for the given tunnel.
1074 * If the peer doesn't have any path to it yet, try to get one.
1075 * If the peer already has some path, send a CREATE PATH towards it.
1077 * @param peer PeerInfo of the peer.
1078 * @param t Tunnel for which to create the path, if possible.
1081 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1085 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1086 * to obtain a local peer -> destination path and interning the peer ids.
1088 * @return Newly allocated and created path
1090 static struct MeshPeerPath *
1091 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1092 unsigned int get_path_length,
1093 const struct GNUNET_PeerIdentity *put_path,
1094 unsigned int put_path_length);
1098 * Adds a path to the peer_infos of all the peers in the path
1100 * @param p Path to process.
1101 * @param confirmed Whether we know if the path works or not.
1104 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1108 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1109 * needed rescources.
1110 * If peer already exists, reevaluate shortest path and change if different.
1112 * @param t Tunnel we want to add a new peer to
1113 * @param peer PeerInfo of the peer being added
1117 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1121 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1122 * that are no longer needed, as well as nodes of no longer reachable peers.
1123 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1125 * @param t Tunnel from which to remove the path.
1126 * @param peer Short id of the peer which should be removed.
1129 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1133 * Search for a tunnel by global ID using full PeerIdentities.
1135 * @param oid owner of the tunnel.
1136 * @param tid global tunnel number.
1138 * @return tunnel handler, NULL if doesn't exist.
1140 static struct MeshTunnel *
1141 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1145 * Delete an active client from the tunnel.
1151 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1154 * Notify a tunnel that a connection has broken that affects at least
1155 * some of its peers.
1157 * @param t Tunnel affected.
1158 * @param p1 Peer that got disconnected from p2.
1159 * @param p2 Peer that got disconnected from p1.
1161 * @return Short ID of the peer disconnected (either p1 or p2).
1162 * 0 if the tunnel remained unaffected.
1164 static GNUNET_PEER_Id
1165 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1170 * Get the current ack value for a tunnel, for data going from root to leaves,
1171 * taking in account the tunnel mode and the status of all children and clients.
1175 * @return Maximum PID allowed.
1178 tunnel_get_fwd_ack (struct MeshTunnel *t);
1182 * Add a client to a tunnel, initializing all needed data structures.
1184 * @param t Tunnel to which add the client.
1185 * @param c Client which to add to the tunnel.
1188 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1192 * @brief Queue and pass message to core when possible.
1194 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1195 * and accounts for it. In case the queue is full, the message is dropped and
1198 * Otherwise, message is treated as internal and allowed to go regardless of
1201 * @param cls Closure (@c type dependant). It will be used by queue_send to
1202 * build the message to be sent if not already prebuilt.
1203 * @param type Type of the message, 0 for a raw message.
1204 * @param size Size of the message.
1205 * @param dst Neighbor to send message to.
1206 * @param t Tunnel this message belongs to.
1209 queue_add (void *cls, uint16_t type, size_t size,
1210 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1214 * Free a transmission that was already queued with all resources
1215 * associated to the request.
1217 * @param queue Queue handler to cancel.
1218 * @param clear_cls Is it necessary to free associated cls?
1221 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1225 * @brief Get the next transmittable message from the queue.
1227 * This will be the head, except in the case of being a data packet
1228 * not allowed by the destination peer.
1230 * @param peer Destination peer.
1232 * @return The next viable MeshPeerQueue element to send to that peer.
1233 * NULL when there are no transmittable messages.
1235 struct MeshPeerQueue *
1236 queue_get_next (const struct MeshPeerInfo *peer);
1240 * Core callback to write a queued packet to core buffer
1242 * @param cls Closure (peer info).
1243 * @param size Number of bytes available in buf.
1244 * @param buf Where the to write the message.
1246 * @return number of bytes written to buf
1249 queue_send (void *cls, size_t size, void *buf);
1251 /******************************************************************************/
1252 /************************ REGEX INTEGRATION ****************************/
1253 /******************************************************************************/
1256 * Cancel a mesh regex search and free resources.
1259 regex_cancel_search (struct MeshRegexSearchInfo *regex_search)
1261 DEBUG_REGEX ("Search for %s canelled.\n", regex_search->description);
1262 GNUNET_REGEX_search_cancel (regex_search->search_handle);
1263 if (0 < regex_search->n_peers)
1264 GNUNET_free (regex_search->peers);
1265 if (GNUNET_SCHEDULER_NO_TASK != regex_search->timeout)
1267 GNUNET_SCHEDULER_cancel(regex_search->timeout);
1269 GNUNET_free (regex_search);
1274 * Function called if the connect attempt to a peer found via
1275 * connect_by_string times out. Try to connect to another peer, if any.
1276 * Otherwise try to reconnect to the same peer.
1278 * @param cls Closure (info about regex search).
1279 * @param tc TaskContext.
1282 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1284 struct MeshRegexSearchInfo *info = cls;
1285 struct MeshPeerInfo *peer_info;
1289 DEBUG_REGEX ("Regex connect timeout\n");
1290 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1291 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1293 DEBUG_REGEX (" due to shutdown\n");
1298 DEBUG_REGEX (" timed out: %u\n", old);
1300 if (0 < info->n_peers)
1302 // Select next peer, put current in that spot.
1303 id = info->peers[info->i_peer];
1304 info->peers[info->i_peer] = info->peer;
1305 info->i_peer = (info->i_peer + 1) % info->n_peers;
1309 // Try to connect to same peer again.
1312 DEBUG_REGEX (" trying: %u\n", id);
1314 peer_info = peer_info_get_short(id);
1315 tunnel_add_peer (info->t, peer_info);
1317 tunnel_delete_peer (info->t, old);
1318 peer_info_connect (peer_info, info->t);
1319 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1320 ®ex_connect_timeout,
1322 DEBUG_REGEX ("Regex connect timeout END\n");
1327 * Function to process DHT string to regex matching.
1328 * Called on each result obtained for the DHT search.
1330 * @param cls Closure provided in GNUNET_REGEX_search.
1331 * @param id Peer providing a regex that matches the string.
1332 * @param get_path Path of the get request.
1333 * @param get_path_length Lenght of get_path.
1334 * @param put_path Path of the put request.
1335 * @param put_path_length Length of the put_path.
1338 regex_found_handler (void *cls,
1339 const struct GNUNET_PeerIdentity *id,
1340 const struct GNUNET_PeerIdentity *get_path,
1341 unsigned int get_path_length,
1342 const struct GNUNET_PeerIdentity *put_path,
1343 unsigned int put_path_length)
1345 struct MeshRegexSearchInfo *info = cls;
1346 struct MeshPeerPath *p;
1347 struct MeshPeerInfo *peer_info;
1349 DEBUG_REGEX ("Got regex results from DHT!\n");
1350 DEBUG_REGEX (" for %s\n", info->description);
1352 peer_info = peer_info_get (id);
1353 p = path_build_from_dht (get_path, get_path_length,
1354 put_path, put_path_length);
1355 path_add_to_peers (p, GNUNET_NO);
1358 tunnel_add_peer (info->t, peer_info);
1359 peer_info_connect (peer_info, info->t);
1360 if (0 == info->peer)
1362 info->peer = peer_info->id;
1366 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
1369 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1372 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1373 ®ex_connect_timeout,
1381 * Store the regular expression describing a local service into the DHT.
1383 * @param regex The regular expresion.
1386 regex_put (struct MeshRegexDescriptor *regex)
1388 DEBUG_REGEX (" regex_put (%s) start\n", regex->regex);
1389 if (NULL == regex->h)
1391 DEBUG_REGEX (" first put, creating DFA\n");
1392 regex->h = GNUNET_REGEX_announce (cfg,
1396 regex->compression);
1398 DEBUG_REGEX (" regex_put (%s) end\n", regex->regex);
1403 * Periodically announce what applications are provided by local clients
1406 * @param cls closure
1407 * @param tc task context
1410 regex_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1412 struct MeshClient *c = cls;
1415 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1416 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1418 DEBUG_REGEX ("Starting announce for regex\n");
1419 for (i = 0; i < c->n_regex; i++)
1420 regex_put (&c->regexes[i]);
1421 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1424 DEBUG_REGEX ("Finished announce for regex\n");
1428 /******************************************************************************/
1429 /************************ PERIODIC FUNCTIONS ****************************/
1430 /******************************************************************************/
1433 * Announce iterator over for each application provided by the peer
1435 * @param cls closure
1436 * @param key current key code
1437 * @param value value in the hash map
1438 * @return GNUNET_YES if we should continue to
1443 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1445 struct PBlock block;
1446 struct MeshClient *c;
1448 block.id = my_full_id;
1449 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1450 GNUNET_assert(NULL != c);
1451 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1452 if (0 == block.type)
1457 block.type = htonl (block.type);
1458 DEBUG_DHT ("Putting APP key: %s\n", GNUNET_h2s (key));
1459 GNUNET_break (NULL !=
1460 GNUNET_DHT_put (dht_handle, key,
1461 dht_replication_level,
1462 GNUNET_DHT_RO_RECORD_ROUTE |
1463 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1464 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1466 (const char *) &block,
1467 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1468 app_announce_time, NULL, NULL));
1474 * Periodically announce what applications are provided by local clients
1477 * @param cls closure
1478 * @param tc task context
1481 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1483 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1485 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1489 DEBUG_DHT ("Starting PUT for apps\n");
1491 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1493 announce_applications_task =
1494 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1496 DEBUG_DHT ("Finished PUT for apps\n");
1501 * Periodically announce self id in the DHT
1503 * @param cls closure
1504 * @param tc task context
1507 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1509 struct PBlock block;
1511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1513 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1517 * - Set data expiration in function of X
1518 * - Adapt X to churn
1520 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1522 block.id = my_full_id;
1523 block.type = htonl (0);
1524 GNUNET_DHT_put (dht_handle, /* DHT handle */
1525 &my_full_id.hashPubKey, /* Key to use */
1526 dht_replication_level, /* Replication level */
1527 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1528 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1529 sizeof (block), /* Size of the data */
1530 (const char *) &block, /* Data itself */
1531 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1532 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1533 NULL, /* Continuation */
1534 NULL); /* Continuation closure */
1536 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1540 /******************************************************************************/
1541 /****************** GENERAL HELPER FUNCTIONS ************************/
1542 /******************************************************************************/
1545 * Decrements the reference counter and frees all resources if needed
1547 * @param mesh_data Data Descriptor used in a multicast message.
1548 * Freed no longer needed (last message).
1551 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1553 if (0 == --(mesh_data->reference_counter))
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1556 GNUNET_free (mesh_data->data);
1557 GNUNET_free (mesh_data);
1563 * Check if client has registered with the service and has not disconnected
1565 * @param client the client to check
1567 * @return non-NULL if client exists in the global DLL
1569 static struct MeshClient *
1570 client_get (struct GNUNET_SERVER_Client *client)
1572 struct MeshClient *c;
1577 if (c->handle == client)
1586 * Checks if a given client has subscribed to certain message type
1588 * @param message_type Type of message to check
1589 * @param c Client to check
1591 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1593 * FIXME: use of crypto_hash slows it down
1594 * The hash function alone takes 8-10us out of the ~55us for the whole
1595 * process of retransmitting the message from one local client to another.
1596 * Find faster implementation!
1599 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1601 struct GNUNET_HashCode hc;
1603 if (NULL == c->types)
1606 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1607 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1612 * Check whether client wants traffic from a tunnel.
1614 * @param c Client to check.
1615 * @param t Tunnel to be found.
1617 * @return GNUNET_YES if client knows tunnel.
1619 * TODO look in client hashmap
1622 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1626 for (i = 0; i < t->nclients; i++)
1627 if (t->clients[i] == c)
1634 * Check whether client has been informed about a tunnel.
1636 * @param c Client to check.
1637 * @param t Tunnel to be found.
1639 * @return GNUNET_YES if client knows tunnel.
1641 * TODO look in client hashmap
1644 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1648 for (i = 0; i < t->nignore; i++)
1649 if (t->ignore[i] == c)
1651 return client_wants_tunnel(c, t);
1656 * Marks a client as uninterested in traffic from the tunnel, updating both
1657 * client and tunnel to reflect this.
1659 * @param c Client that doesn't want traffic anymore.
1660 * @param t Tunnel which should be ignored.
1662 * FIXME when to delete an incoming tunnel?
1665 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1667 struct GNUNET_HashCode hash;
1669 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1670 GNUNET_break (GNUNET_YES ==
1671 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1673 GNUNET_break (GNUNET_YES ==
1674 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1676 tunnel_delete_active_client (t, c);
1677 GNUNET_array_append (t->ignore, t->nignore, c);
1682 * Deletes a tunnel from a client (either owner or destination). To be used on
1683 * tunnel destroy, otherwise, use client_ignore_tunnel.
1685 * @param c Client whose tunnel to delete.
1686 * @param t Tunnel which should be deleted.
1689 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1691 struct GNUNET_HashCode hash;
1695 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1696 GNUNET_assert (GNUNET_YES ==
1697 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1703 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1705 GNUNET_assert (GNUNET_YES ==
1706 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1710 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1718 * Notify the owner of a tunnel that a peer has disconnected.
1720 * @param c Client (owner of tunnel).
1721 * @param t Tunnel this message is about.
1722 * @param peer_id Short ID of the disconnected peer.
1725 client_notify_peer_disconnected (struct MeshClient *c,
1726 struct MeshTunnel *t,
1727 GNUNET_PEER_Id peer_id)
1729 struct GNUNET_MESH_PeerControl msg;
1731 if (NULL == t->owner || NULL == nc)
1734 msg.header.size = htons (sizeof (msg));
1735 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1736 msg.tunnel_id = htonl (t->local_tid);
1737 GNUNET_PEER_resolve (peer_id, &msg.peer);
1738 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1739 &msg.header, GNUNET_NO);
1744 * Send the message to all clients that have subscribed to its type
1746 * @param msg Pointer to the message itself
1747 * @param payload Pointer to the payload of the message.
1748 * @param t The tunnel to whose clients this message goes.
1750 * @return number of clients this message was sent to
1753 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1754 const struct GNUNET_MessageHeader *payload,
1755 struct MeshTunnel *t)
1757 struct MeshClient *c;
1758 MESH_TunnelNumber *tid;
1761 char cbuf[htons (msg->size)];
1763 type = ntohs (payload->type);
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1766 GNUNET_MESH_DEBUG_M2S (type));
1768 memcpy (cbuf, msg, sizeof (cbuf));
1769 switch (htons (msg->type))
1771 struct GNUNET_MESH_Unicast *uc;
1772 struct GNUNET_MESH_Multicast *mc;
1773 struct GNUNET_MESH_ToOrigin *to;
1775 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1776 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1779 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1780 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1783 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1784 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1792 for (count = 0, c = clients; c != NULL; c = c->next)
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1795 if (client_is_subscribed (type, c))
1797 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1801 *tid = htonl (t->local_tid);
1805 if (GNUNET_NO == client_knows_tunnel (c, t))
1807 /* This client doesn't know the tunnel */
1808 struct GNUNET_MESH_TunnelNotification tmsg;
1809 struct GNUNET_HashCode hash;
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1812 tmsg.header.size = htons (sizeof (tmsg));
1813 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1814 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1815 tmsg.tunnel_id = htonl (t->local_tid_dest);
1817 if (GNUNET_YES == t->speed_min)
1818 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1819 if (GNUNET_YES == t->nobuffer)
1820 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1821 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1822 &tmsg.header, GNUNET_NO);
1823 tunnel_add_client (t, c);
1824 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1826 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1827 c->incoming_tunnels, &hash, t,
1828 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1830 *tid = htonl (t->local_tid_dest);
1833 /* Check if the client wants to get traffic from the tunnel */
1834 if (GNUNET_NO == client_wants_tunnel(c, t))
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1838 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1839 (struct GNUNET_MessageHeader
1840 *) cbuf, GNUNET_NO);
1849 * Notify the client that owns the tunnel that a peer has connected to it
1850 * (the requested path to it has been confirmed).
1852 * @param t Tunnel whose owner to notify
1853 * @param id Short id of the peer that has connected
1856 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1858 struct GNUNET_MESH_PeerControl pc;
1860 if (NULL == t->owner || GNUNET_YES == t->destroy)
1863 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1864 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1865 pc.tunnel_id = htonl (t->local_tid);
1866 GNUNET_PEER_resolve (id, &pc.peer);
1867 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1873 * Notify all clients (not depending on registration status) that the incoming
1874 * tunnel is no longer valid.
1876 * @param t Tunnel that was destroyed.
1879 send_clients_tunnel_destroy (struct MeshTunnel *t)
1881 struct GNUNET_MESH_TunnelMessage msg;
1883 msg.header.size = htons (sizeof (msg));
1884 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1885 msg.tunnel_id = htonl (t->local_tid_dest);
1886 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1891 * Notify clients of tunnel disconnections, if needed.
1892 * In case the origin disconnects, the destination clients get a tunnel destroy
1893 * notification. If the last destination disconnects (only one remaining client
1894 * in tunnel), the origin gets a (local ID) peer disconnected.
1895 * Note that the function must be called BEFORE removing the client from
1898 * @param t Tunnel that was destroyed.
1899 * @param c Client that disconnected.
1902 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1908 struct GNUNET_MESH_TunnelMessage msg;
1910 msg.header.size = htons (sizeof (msg));
1911 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1912 msg.tunnel_id = htonl (t->local_tid_dest);
1913 for (i = 0; i < t->nclients; i++)
1914 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1915 &msg.header, GNUNET_NO);
1917 // FIXME when to disconnect an incoming tunnel?
1918 else if (1 == t->nclients && NULL != t->owner)
1920 struct GNUNET_MESH_PeerControl msg;
1922 msg.header.size = htons (sizeof (msg));
1923 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1924 msg.tunnel_id = htonl (t->local_tid);
1925 msg.peer = my_full_id;
1926 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1927 &msg.header, GNUNET_NO);
1933 * Iterator over all the peers to remove the oldest not-used entry.
1935 * @param cls Closure (unsued).
1936 * @param key ID of the peer.
1937 * @param value Peer_Info of the peer.
1942 peer_info_timeout (void *cls,
1943 const struct GNUNET_HashCode *key,
1950 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1951 * and insert it in the appropiate structures if the peer is not known yet.
1953 * @param peer Full identity of the peer.
1955 * @return Existing or newly created peer info.
1957 static struct MeshPeerInfo *
1958 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1960 struct MeshPeerInfo *peer_info;
1962 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1963 if (NULL == peer_info)
1966 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1967 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1969 GNUNET_CONTAINER_multihashmap_iterate (peers,
1973 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1975 peer_info->id = GNUNET_PEER_intern (peer);
1977 peer_info->last_contact = GNUNET_TIME_absolute_get();
1984 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1985 * and insert it in the appropiate structures if the peer is not known yet.
1987 * @param peer Short identity of the peer.
1989 * @return Existing or newly created peer info.
1991 static struct MeshPeerInfo *
1992 peer_info_get_short (const GNUNET_PEER_Id peer)
1994 struct GNUNET_PeerIdentity id;
1996 GNUNET_PEER_resolve (peer, &id);
1997 return peer_info_get (&id);
2002 * Iterator to remove the tunnel from the list of tunnels a peer participates
2005 * @param cls Closure (tunnel info)
2006 * @param key GNUNET_PeerIdentity of the peer (unused)
2007 * @param value PeerInfo of the peer
2009 * @return always GNUNET_YES, to keep iterating
2012 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2014 struct MeshTunnel *t = cls;
2015 struct MeshPeerInfo *peer = value;
2018 for (i = 0; i < peer->ntunnels; i++)
2021 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2024 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2026 GNUNET_realloc (peer->tunnels,
2027 peer->ntunnels * sizeof(struct MeshTunnel *));
2036 * Core callback to write a pre-constructed data packet to core buffer
2038 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2039 * @param size Number of bytes available in buf.
2040 * @param buf Where the to write the message.
2042 * @return number of bytes written to buf
2045 send_core_data_raw (void *cls, size_t size, void *buf)
2047 struct MeshTransmissionDescriptor *info = cls;
2048 struct GNUNET_MessageHeader *msg;
2051 GNUNET_assert (NULL != info);
2052 GNUNET_assert (NULL != info->mesh_data);
2053 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2054 total_size = ntohs (msg->size);
2056 if (total_size > size)
2061 memcpy (buf, msg, total_size);
2062 data_descriptor_decrement_rc (info->mesh_data);
2069 * Sends an already built non-multicast message to a peer,
2070 * properly registrating all used resources.
2072 * @param message Message to send. Function makes a copy of it.
2073 * @param peer Short ID of the neighbor whom to send the message.
2074 * @param t Tunnel on which this message is transmitted.
2077 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2078 const struct GNUNET_PeerIdentity *peer,
2079 struct MeshTunnel *t)
2081 struct MeshTransmissionDescriptor *info;
2082 struct MeshPeerInfo *neighbor;
2083 struct MeshPeerPath *p;
2087 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2089 size = ntohs (message->size);
2090 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2091 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2092 info->mesh_data->data = GNUNET_malloc (size);
2093 memcpy (info->mesh_data->data, message, size);
2094 type = ntohs(message->type);
2097 struct GNUNET_MESH_Unicast *m;
2098 struct GNUNET_MESH_ToOrigin *to;
2100 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2101 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2102 m->ttl = htonl (ntohl (m->ttl) - 1);
2104 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2105 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2107 to->pid = htonl(t->bck_pid);
2109 info->mesh_data->data_len = size;
2110 info->mesh_data->reference_counter = 1;
2111 info->mesh_data->total_out = 1;
2112 neighbor = peer_info_get (peer);
2113 for (p = neighbor->path_head; NULL != p; p = p->next)
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 " %s IS NOT DIRECTLY CONNECTED\n",
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 for (p = neighbor->path_head; NULL != p; p = p->next)
2131 struct GNUNET_PeerIdentity debug_id;
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 " path with %u hops through:\n",
2137 for (i = 0; i < p->length; i++)
2139 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142 i, GNUNET_i2s(&debug_id));
2146 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2148 " no direct connection to %s\n",
2150 GNUNET_free (info->mesh_data->data);
2151 GNUNET_free (info->mesh_data);
2155 info->peer = neighbor;
2156 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2167 * Sends a CREATE PATH message for a path to a peer, properly registrating
2168 * all used resources.
2170 * @param peer PeerInfo of the final peer for whom this path is being created.
2171 * @param p Path itself.
2172 * @param t Tunnel for which the path is created.
2175 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2176 struct MeshTunnel *t)
2178 struct GNUNET_PeerIdentity id;
2179 struct MeshPathInfo *path_info;
2180 struct MeshPeerInfo *neighbor;
2186 p = tree_get_path_to_peer (t->tree, peer->id);
2193 for (i = 0; i < p->length; i++)
2195 if (p->peers[i] == myid)
2198 if (i >= p->length - 1)
2204 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2206 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2207 path_info->path = p;
2209 neighbor = peer_info_get (&id);
2210 path_info->peer = neighbor;
2211 queue_add (path_info,
2212 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2213 sizeof (struct GNUNET_MESH_ManipulatePath) +
2214 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2221 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2223 * @param t Tunnel whose path to destroy.
2224 * @param destination Short ID of the peer to whom the path to destroy.
2227 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2229 struct MeshPeerPath *p;
2232 p = tree_get_path_to_peer (t->tree, destination);
2238 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2239 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2241 struct GNUNET_MESH_ManipulatePath *msg;
2242 struct GNUNET_PeerIdentity *pi;
2246 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2247 msg->header.size = htons (size);
2248 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2249 msg->tid = htonl (t->id.tid);
2250 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2251 for (i = 0; i < p->length; i++)
2253 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2255 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2262 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2264 * @param t Tunnel which to confirm.
2267 send_path_ack (struct MeshTunnel *t)
2269 struct MeshTransmissionDescriptor *info;
2270 struct GNUNET_PeerIdentity id;
2271 GNUNET_PEER_Id peer;
2273 peer = tree_get_predecessor (t->tree);
2274 GNUNET_PEER_resolve (peer, &id);
2275 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2276 info->origin = &t->id;
2277 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2278 GNUNET_assert (NULL != info->peer);
2281 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2282 sizeof (struct GNUNET_MESH_PathACK),
2289 * Try to establish a new connection to this peer.
2290 * Use the best path for the given tunnel.
2291 * If the peer doesn't have any path to it yet, try to get one.
2292 * If the peer already has some path, send a CREATE PATH towards it.
2294 * @param peer PeerInfo of the peer.
2295 * @param t Tunnel for which to create the path, if possible.
2298 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2300 struct MeshPeerPath *p;
2301 struct MeshPathInfo *path_info;
2303 if (NULL != peer->path_head)
2305 p = tree_get_path_to_peer (t->tree, peer->id);
2312 // FIXME always send create path to self
2315 send_create_path (peer, p, t);
2319 struct GNUNET_HashCode hash;
2322 send_client_peer_connected (t, myid);
2323 t->local_tid_dest = next_local_tid++;
2324 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2327 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2328 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2335 else if (NULL == peer->dhtget)
2337 struct GNUNET_PeerIdentity id;
2339 GNUNET_PEER_resolve (peer->id, &id);
2340 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2341 path_info->peer = peer;
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2345 peer->dhtgetcls = path_info;
2346 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2347 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2348 &id.hashPubKey, /* key to search */
2349 dht_replication_level, /* replication level */
2350 GNUNET_DHT_RO_RECORD_ROUTE |
2351 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2352 NULL, /* xquery */ // FIXME BLOOMFILTER
2353 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2354 &dht_get_id_handler, path_info);
2356 /* Otherwise, there is no path but the DHT get is already started. */
2361 * Task to delay the connection of a peer
2363 * @param cls Closure (path info with tunnel and peer to connect).
2364 * Will be free'd on exection.
2365 * @param tc TaskContext
2368 peer_info_connect_task (void *cls,
2369 const struct GNUNET_SCHEDULER_TaskContext *tc)
2371 struct MeshPathInfo *path_info = cls;
2373 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2375 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2380 peer_info_connect (path_info->peer, path_info->t);
2386 * Destroy the peer_info and free any allocated resources linked to it
2388 * @param pi The peer_info to destroy.
2390 * @return GNUNET_OK on success
2393 peer_info_destroy (struct MeshPeerInfo *pi)
2395 struct GNUNET_PeerIdentity id;
2396 struct MeshPeerPath *p;
2397 struct MeshPeerPath *nextp;
2399 GNUNET_PEER_resolve (pi->id, &id);
2400 GNUNET_PEER_change_rc (pi->id, -1);
2403 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2407 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2409 if (NULL != pi->dhtget)
2411 GNUNET_DHT_get_stop (pi->dhtget);
2412 GNUNET_free (pi->dhtgetcls);
2418 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2422 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2424 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2432 * Remove all paths that rely on a direct connection between p1 and p2
2433 * from the peer itself and notify all tunnels about it.
2435 * @param peer PeerInfo of affected peer.
2436 * @param p1 GNUNET_PEER_Id of one peer.
2437 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2440 * TODO: optimize (see below)
2443 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2446 struct MeshPeerPath *p;
2447 struct MeshPeerPath *aux;
2448 struct MeshPeerInfo *peer_d;
2450 unsigned int destroyed;
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2457 p = peer->path_head;
2461 for (i = 0; i < (p->length - 1); i++)
2463 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2464 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2466 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2477 for (i = 0; i < peer->ntunnels; i++)
2479 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2483 * Problem: one or more peers have been deleted from the tunnel tree.
2484 * We don't know who they are to try to add them again.
2485 * We need to try to find a new path for each of the disconnected peers.
2486 * Some of them might already have a path to reach them that does not
2487 * involve p1 and p2. Adding all anew might render in a better tree than
2488 * the trivial immediate fix.
2490 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2491 * its children will be reachable trough him.
2493 peer_d = peer_info_get_short (d);
2496 for (p = peer_d->path_head; NULL != p; p = p->next)
2498 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2506 /* No callback, as peer will be already disconnected and a connection
2507 * scheduled by tunnel_notify_connection_broken.
2509 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2513 peer_info_connect (peer_d, peer->tunnels[i]);
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2521 * Add the path to the peer and update the path used to reach it in case this
2524 * @param peer_info Destination peer to add the path to.
2525 * @param path New path to add. Last peer must be the peer in arg 1.
2526 * Path will be either used of freed if already known.
2527 * @param trusted Do we trust that this path is real?
2530 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2533 struct MeshPeerPath *aux;
2537 if ((NULL == peer_info) || (NULL == path))
2540 path_destroy (path);
2543 if (path->peers[path->length - 1] != peer_info->id)
2546 path_destroy (path);
2549 if (path->length <= 2 && GNUNET_NO == trusted)
2551 /* Only allow CORE to tell us about direct paths */
2552 path_destroy (path);
2555 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2556 for (l = 1; l < path->length; l++)
2558 if (path->peers[l] == myid)
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2561 for (l2 = 0; l2 < path->length - l; l2++)
2563 path->peers[l2] = path->peers[l + l2];
2568 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2573 struct GNUNET_PeerIdentity id;
2575 GNUNET_PEER_resolve (peer_info->id, &id);
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2577 path->length, GNUNET_i2s (&id));
2580 l = path_get_length (path);
2587 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2588 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2590 l2 = path_get_length (aux);
2593 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2594 peer_info->path_tail, aux, path);
2599 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2601 path_destroy (path);
2606 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2613 * Add the path to the origin peer and update the path used to reach it in case
2614 * this is the shortest.
2615 * The path is given in peer_info -> destination, therefore we turn the path
2616 * upside down first.
2618 * @param peer_info Peer to add the path to, being the origin of the path.
2619 * @param path New path to add after being inversed.
2620 * @param trusted Do we trust that this path is real?
2623 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2624 struct MeshPeerPath *path, int trusted)
2627 peer_info_add_path (peer_info, path, trusted);
2632 * Function called if the connection to the peer has been stalled for a while,
2633 * possibly due to a missed ACK. Poll the peer about its ACK status.
2635 * @param cls Closure (cinfo).
2636 * @param tc TaskContext.
2639 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2641 struct MeshTunnelChildInfo *cinfo = cls;
2642 struct GNUNET_MESH_Poll msg;
2643 struct GNUNET_PeerIdentity id;
2644 struct MeshTunnel *t;
2646 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2647 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2653 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2654 msg.header.size = htons (sizeof (msg));
2655 msg.tid = htonl (t->id.tid);
2656 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2657 msg.last_ack = htonl (cinfo->fwd_ack);
2659 GNUNET_PEER_resolve (cinfo->id, &id);
2660 send_prebuilt_message (&msg.header, &id, cinfo->t);
2661 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2663 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2664 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2665 &tunnel_poll, cinfo);
2670 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2671 * to obtain a local peer -> destination path and interning the peer ids.
2673 * @return Newly allocated and created path
2675 static struct MeshPeerPath *
2676 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2677 unsigned int get_path_length,
2678 const struct GNUNET_PeerIdentity *put_path,
2679 unsigned int put_path_length)
2681 struct MeshPeerPath *p;
2687 GNUNET_PEER_change_rc (myid, 1);
2688 i = get_path_length;
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2690 for (i--; i >= 0; i--)
2692 id = GNUNET_PEER_intern (&get_path[i]);
2693 if (p->length > 0 && id == p->peers[p->length - 1])
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2696 GNUNET_PEER_change_rc (id, -1);
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2701 GNUNET_i2s (&get_path[i]));
2703 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2704 p->peers[p->length - 1] = id;
2707 i = put_path_length;
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2709 for (i--; i >= 0; i--)
2711 id = GNUNET_PEER_intern (&put_path[i]);
2714 /* PUT path went through us, so discard the path up until now and start
2715 * from here to get a much shorter (and loop-free) path.
2720 if (p->length > 0 && id == p->peers[p->length - 1])
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2723 GNUNET_PEER_change_rc (id, -1);
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2728 GNUNET_i2s (&put_path[i]));
2730 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2731 p->peers[p->length - 1] = id;
2735 if (get_path_length > 0)
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2737 GNUNET_i2s (&get_path[0]));
2738 if (put_path_length > 0)
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2740 GNUNET_i2s (&put_path[0]));
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2743 for (i = 0; i < p->length; i++)
2745 struct GNUNET_PeerIdentity peer_id;
2747 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2749 GNUNET_i2s (&peer_id));
2757 * Adds a path to the peer_infos of all the peers in the path
2759 * @param p Path to process.
2760 * @param confirmed Whether we know if the path works or not.
2763 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2767 /* TODO: invert and add */
2768 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2769 for (i++; i < p->length; i++)
2771 struct MeshPeerInfo *aux;
2772 struct MeshPeerPath *copy;
2774 aux = peer_info_get_short (p->peers[i]);
2775 copy = path_duplicate (p);
2776 copy->length = i + 1;
2777 peer_info_add_path (aux, copy, GNUNET_NO);
2783 * Send keepalive packets for a peer
2785 * @param cls Closure (tunnel for which to send the keepalive).
2786 * @param tc Notification context.
2788 * TODO: implement explicit multicast keepalive?
2791 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2795 * Search for a tunnel among the incoming tunnels
2797 * @param tid the local id of the tunnel
2799 * @return tunnel handler, NULL if doesn't exist
2801 static struct MeshTunnel *
2802 tunnel_get_incoming (MESH_TunnelNumber tid)
2804 struct GNUNET_HashCode hash;
2806 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2807 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2808 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2813 * Search for a tunnel among the tunnels for a client
2815 * @param c the client whose tunnels to search in
2816 * @param tid the local id of the tunnel
2818 * @return tunnel handler, NULL if doesn't exist
2820 static struct MeshTunnel *
2821 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2823 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2825 return tunnel_get_incoming (tid);
2829 struct GNUNET_HashCode hash;
2831 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2832 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2838 * Search for a tunnel by global ID using PEER_ID
2840 * @param pi owner of the tunnel
2841 * @param tid global tunnel number
2843 * @return tunnel handler, NULL if doesn't exist
2845 static struct MeshTunnel *
2846 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2848 struct MESH_TunnelID id;
2849 struct GNUNET_HashCode hash;
2854 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2855 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2860 * Search for a tunnel by global ID using full PeerIdentities
2862 * @param oid owner of the tunnel
2863 * @param tid global tunnel number
2865 * @return tunnel handler, NULL if doesn't exist
2867 static struct MeshTunnel *
2868 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2870 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2875 * Delete an active client from the tunnel.
2881 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2885 for (i = 0; i < t->nclients; i++)
2887 if (t->clients[i] == c)
2889 t->clients[i] = t->clients[t->nclients - 1];
2890 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2891 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2893 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2901 * Delete an ignored client from the tunnel.
2907 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2911 for (i = 0; i < t->nignore; i++)
2913 if (t->ignore[i] == c)
2915 t->ignore[i] = t->ignore[t->nignore - 1];
2916 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2924 * Delete a client from the tunnel. It should be only done on
2925 * client disconnection, otherwise use client_ignore_tunnel.
2931 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2933 tunnel_delete_ignored_client (t, c);
2934 tunnel_delete_active_client (t, c);
2939 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2941 * Destroys queue elements of all waiting transmissions and frees all memory
2942 * used by the struct and its elements.
2944 * @param cls Closure (tunnel info).
2945 * @param key Hash of GNUNET_PEER_Id (unused).
2946 * @param value MeshTunnelChildInfo of the child.
2948 * @return always GNUNET_YES, to keep iterating
2951 tunnel_destroy_child (void *cls,
2952 const struct GNUNET_HashCode * key,
2955 struct MeshTunnelChildInfo *cinfo = value;
2956 struct MeshTunnel *t = cls;
2957 struct MeshPeerQueue *q;
2961 for (c = 0; c < cinfo->send_buffer_n; c++)
2963 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2964 q = cinfo->send_buffer[i];
2965 cinfo->send_buffer[i] = NULL;
2967 queue_destroy (q, GNUNET_YES);
2970 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2972 GNUNET_free_non_null (cinfo->send_buffer);
2973 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2975 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2976 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2978 GNUNET_free (cinfo);
2984 * Callback used to notify a client owner of a tunnel that a peer has
2985 * disconnected, most likely because of a path change.
2987 * @param cls Closure (tunnel this notification is about).
2988 * @param peer_id Short ID of disconnected peer.
2991 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2993 struct MeshTunnel *t = cls;
2994 struct MeshPeerInfo *peer;
2995 struct MeshPathInfo *path_info;
2997 client_notify_peer_disconnected (t->owner, t, peer_id);
2999 peer = peer_info_get_short (peer_id);
3000 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3001 path_info->peer = peer;
3003 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3009 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3010 * needed rescources.
3011 * If peer already exists, reevaluate shortest path and change if different.
3013 * @param t Tunnel we want to add a new peer to
3014 * @param peer PeerInfo of the peer being added
3018 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3020 struct GNUNET_PeerIdentity id;
3021 struct MeshPeerPath *best_p;
3022 struct MeshPeerPath *p;
3023 unsigned int best_cost;
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3027 GNUNET_PEER_resolve (peer->id, &id);
3029 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3031 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3032 GNUNET_assert (GNUNET_OK ==
3033 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3035 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3038 if (NULL != (p = peer->path_head))
3041 best_cost = tree_get_path_cost (t->tree, p);
3044 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3051 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3052 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3053 t->path_refresh_task =
3054 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3058 /* Start a DHT get */
3059 peer_info_connect (peer, t);
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3065 * Add a path to a tunnel which we don't own, just to remember the next hop.
3066 * If destination node was already in the tunnel, the first hop information
3067 * will be replaced with the new path.
3069 * @param t Tunnel we want to add a new peer to
3070 * @param p Path to add
3071 * @param own_pos Position of local node in path.
3075 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3076 unsigned int own_pos)
3078 struct GNUNET_PeerIdentity id;
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3081 GNUNET_assert (0 != own_pos);
3082 tree_add_path (t->tree, p, NULL, NULL);
3083 if (own_pos < p->length - 1)
3085 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3086 tree_update_first_hops (t->tree, myid, &id);
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3092 * Add a client to a tunnel, initializing all needed data structures.
3094 * @param t Tunnel to which add the client.
3095 * @param c Client which to add to the tunnel.
3098 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3100 struct MeshTunnelClientInfo clinfo;
3102 GNUNET_array_append (t->clients, t->nclients, c);
3103 clinfo.fwd_ack = t->fwd_pid + 1;
3104 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3105 clinfo.fwd_pid = t->fwd_pid;
3106 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3108 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3113 * Notifies a tunnel that a connection has broken that affects at least
3114 * some of its peers. Sends a notification towards the root of the tree.
3115 * In case the peer is the owner of the tree, notifies the client that owns
3116 * the tunnel and tries to reconnect.
3118 * @param t Tunnel affected.
3119 * @param p1 Peer that got disconnected from p2.
3120 * @param p2 Peer that got disconnected from p1.
3122 * @return Short ID of the peer disconnected (either p1 or p2).
3123 * 0 if the tunnel remained unaffected.
3125 static GNUNET_PEER_Id
3126 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3132 tree_notify_connection_broken (t->tree, p1, p2,
3133 &tunnel_notify_client_peer_disconnected,
3135 if (myid != p1 && myid != p2)
3141 if (tree_get_predecessor (t->tree) != 0)
3143 /* We are the peer still connected, notify owner of the disconnection. */
3144 struct GNUNET_MESH_PathBroken msg;
3145 struct GNUNET_PeerIdentity neighbor;
3147 msg.header.size = htons (sizeof (msg));
3148 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3149 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3150 msg.tid = htonl (t->id.tid);
3151 msg.peer1 = my_full_id;
3152 GNUNET_PEER_resolve (pid, &msg.peer2);
3153 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3154 send_prebuilt_message (&msg.header, &neighbor, t);
3162 * Send a multicast packet to a neighbor.
3164 * @param cls Closure (Info about the multicast packet)
3165 * @param neighbor_id Short ID of the neighbor to send the packet to.
3168 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3170 struct MeshData *mdata = cls;
3171 struct MeshTransmissionDescriptor *info;
3172 struct GNUNET_PeerIdentity neighbor;
3173 struct GNUNET_MessageHeader *msg;
3175 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3177 info->mesh_data = mdata;
3178 (mdata->reference_counter) ++;
3179 info->destination = neighbor_id;
3180 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3182 GNUNET_i2s (&neighbor));
3183 info->peer = peer_info_get (&neighbor);
3184 GNUNET_assert (NULL != info->peer);
3185 msg = (struct GNUNET_MessageHeader *) mdata->data;
3188 info->mesh_data->data_len,
3195 * Queue a message in a tunnel in multicast, sending a copy to each child node
3196 * down the local one in the tunnel tree.
3198 * @param t Tunnel in which to send the data.
3199 * @param msg Message to be sent.
3202 tunnel_send_multicast (struct MeshTunnel *t,
3203 const struct GNUNET_MessageHeader *msg)
3205 struct MeshData *mdata;
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3208 " sending a multicast packet...\n");
3210 mdata = GNUNET_malloc (sizeof (struct MeshData));
3211 mdata->data_len = ntohs (msg->size);
3213 mdata->data = GNUNET_malloc (mdata->data_len);
3214 memcpy (mdata->data, msg, mdata->data_len);
3215 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3217 struct GNUNET_MESH_Multicast *mcast;
3219 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3220 if (t->fwd_queue_n >= t->fwd_queue_max)
3223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3225 " message from %s!\n",
3226 GNUNET_i2s(&mcast->oid));
3227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3228 " message at %s!\n",
3229 GNUNET_i2s(&my_full_id));
3230 GNUNET_free (mdata->data);
3231 GNUNET_free (mdata);
3235 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3237 ntohl (mcast->ttl));
3241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3244 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3245 if (mdata->reference_counter == 0)
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3248 " no one to send data to\n");
3249 GNUNET_free (mdata->data);
3250 GNUNET_free (mdata);
3251 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3256 mdata->total_out = mdata->reference_counter;
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 " sending a multicast packet done\n");
3265 * Increase the SKIP value of all peers that
3266 * have not received a unicast message.
3268 * @param cls Closure (ID of the peer that HAS received the message).
3269 * @param key ID of the neighbor.
3270 * @param value Information about the neighbor.
3272 * @return GNUNET_YES to keep iterating.
3275 tunnel_add_skip (void *cls,
3276 const struct GNUNET_HashCode * key,
3279 struct GNUNET_PeerIdentity *neighbor = cls;
3280 struct MeshTunnelChildInfo *cinfo = value;
3282 /* TODO compare only pointers? key == neighbor? */
3283 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3293 * @brief Get neighbor's Flow Control information.
3295 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3296 * descendant of the local node in a certain tunnel.
3297 * If the info is not yet there (recently created path), creates the data struct
3298 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3301 * @param t Tunnel related.
3302 * @param peer Neighbor whose Flow Control info is needed.
3304 * @return Neighbor's Flow Control info.
3306 static struct MeshTunnelChildInfo *
3307 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3308 const struct GNUNET_PeerIdentity *peer)
3310 struct MeshTunnelChildInfo *cinfo;
3312 if (NULL == t->children_fc)
3315 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3321 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3322 cinfo->id = GNUNET_PEER_intern (peer);
3323 cinfo->skip = t->fwd_pid;
3326 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3327 cinfo->fwd_ack = t->fwd_pid + delta;
3328 cinfo->bck_ack = delta;
3329 cinfo->bck_pid = -1;
3331 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3332 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3334 cinfo->send_buffer =
3335 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3337 GNUNET_assert (GNUNET_OK ==
3338 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3348 * Get the Flow Control info of a client.
3350 * @param t Tunnel on which to look.
3351 * @param c Client whose ACK to get.
3353 * @return ACK value.
3355 static struct MeshTunnelClientInfo *
3356 tunnel_get_client_fc (struct MeshTunnel *t,
3357 struct MeshClient *c)
3361 for (i = 0; i < t->nclients; i++)
3363 if (t->clients[i] != c)
3365 return &t->clients_fc[i];
3368 return NULL; // avoid compiler / coverity complaints
3373 * Iterator to get the appropiate ACK value from all children nodes.
3375 * @param cls Closue (tunnel).
3376 * @param id Id of the child node.
3379 tunnel_get_child_fwd_ack (void *cls,
3382 struct GNUNET_PeerIdentity peer_id;
3383 struct MeshTunnelChildInfo *cinfo;
3384 struct MeshTunnelChildIteratorContext *ctx = cls;
3385 struct MeshTunnel *t = ctx->t;
3388 GNUNET_PEER_resolve (id, &peer_id);
3389 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3390 ack = cinfo->fwd_ack;
3393 if (GNUNET_NO == ctx->init)
3395 ctx->max_child_ack = ack;
3396 ctx->init = GNUNET_YES;
3399 if (GNUNET_YES == t->speed_min)
3401 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3405 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3412 * Get the maximum PID allowed to transmit to any
3413 * tunnel child of the local peer, depending on the tunnel
3414 * buffering/speed settings.
3418 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3421 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3423 struct MeshTunnelChildIteratorContext ctx;
3425 ctx.max_child_ack = 0;
3427 ctx.init = GNUNET_NO;
3428 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3430 if (0 == ctx.nchildren)
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 " tunnel has no children, no FWD ACK\n");
3437 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3438 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3440 return (int64_t) ctx.max_child_ack;
3445 * Set the FWD ACK value of a client in a particular tunnel.
3447 * @param t Tunnel affected.
3448 * @param c Client whose ACK to set.
3449 * @param ack ACK value.
3452 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3453 struct MeshClient *c,
3458 for (i = 0; i < t->nclients; i++)
3460 if (t->clients[i] != c)
3462 t->clients_fc[i].fwd_ack = ack;
3470 * Get the highest ACK value of all clients in a particular tunnel,
3471 * according to the buffering/speed settings.
3473 * @param t Tunnel on which to look.
3475 * @return Corresponding ACK value (max uint32_t).
3476 * If no clients are suscribed, -1LL.
3479 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3484 if (0 == t->nclients)
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 " tunnel has no clients, no FWD ACK\n");
3491 for (ack = -1LL, i = 0; i < t->nclients; i++)
3494 (GNUNET_YES == t->speed_min &&
3495 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3496 (GNUNET_NO == t->speed_min &&
3497 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3499 ack = t->clients_fc[i].fwd_ack;
3503 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3504 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3506 return (uint32_t) ack;
3511 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3512 * mode and the status of all children nodes.
3516 * @return Maximum PID allowed.
3519 tunnel_get_fwd_ack (struct MeshTunnel *t)
3523 uint32_t buffer_free;
3527 count = t->fwd_pid - t->skip;
3528 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3529 child_ack = tunnel_get_children_fwd_ack (t);
3530 client_ack = tunnel_get_clients_fwd_ack (t);
3531 if (GNUNET_YES == t->nobuffer)
3534 if (-1LL == child_ack)
3535 child_ack = client_ack;
3536 if (-1LL == child_ack)
3539 client_ack = child_ack = ack;
3544 ack = count + buffer_free; // Overflow? OK!
3546 if (-1LL == child_ack)
3548 // Node has no children, child_ack AND core buffer are irrelevant.
3549 if (-1LL == client_ack) // No children AND no clients? Not good!
3551 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3555 return (uint32_t) client_ack;
3557 if (-1LL == client_ack)
3561 if (GNUNET_YES == t->speed_min)
3563 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3564 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3568 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3569 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3573 count, buffer_free, child_ack, client_ack, ack);
3579 * Build a local ACK message and send it to a local client.
3581 * @param t Tunnel on which to send the ACK.
3582 * @param c Client to whom send the ACK.
3583 * @param ack Value of the ACK.
3586 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3588 struct GNUNET_MESH_LocalAck msg;
3590 msg.header.size = htons (sizeof (msg));
3591 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3592 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3593 msg.max_pid = htonl (ack);
3594 GNUNET_SERVER_notification_context_unicast(nc,
3601 * Build an ACK message and queue it to send to the given peer.
3603 * @param t Tunnel on which to send the ACK.
3604 * @param peer Peer to whom send the ACK.
3605 * @param ack Value of the ACK.
3608 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3610 struct GNUNET_MESH_ACK msg;
3612 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3613 msg.header.size = htons (sizeof (msg));
3614 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3615 msg.pid = htonl (ack);
3616 msg.tid = htonl (t->id.tid);
3618 send_prebuilt_message (&msg.header, peer, t);
3623 * Notify a the owner of a tunnel about how many more
3624 * payload packages will we accept on a given tunnel.
3626 * @param t Tunnel on which to send the ACK.
3629 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 "Sending client FWD ACK on tunnel %X\n",
3637 ack = tunnel_get_fwd_ack (t);
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3640 if (t->last_fwd_ack == ack)
3642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3647 t->last_fwd_ack = ack;
3648 send_local_ack (t, t->owner, ack);
3653 * Send an ACK informing the predecessor about the available buffer space.
3654 * In case there is no predecessor, inform the owning client.
3655 * If buffering is off, send only on behalf of children or self if endpoint.
3656 * If buffering is on, send when sent to children and buffer space is free.
3657 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3658 * the ACK itself goes "back" (towards root).
3660 * @param t Tunnel on which to send the ACK.
3661 * @param type Type of message that triggered the ACK transmission.
3664 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3666 struct GNUNET_PeerIdentity id;
3669 if (NULL != t->owner)
3671 tunnel_send_client_fwd_ack (t);
3674 /* Is it after unicast / multicast retransmission? */
3677 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3678 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3680 "ACK due to FWD DATA retransmission\n");
3681 if (GNUNET_YES == t->nobuffer)
3683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3687 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3688 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3690 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3691 t->force_ack = GNUNET_YES;
3697 /* Check if we need to transmit the ACK */
3698 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3699 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3700 GNUNET_NO == t->force_ack)
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3704 " t->qmax: %u, t->qn: %u\n",
3705 t->fwd_queue_max, t->fwd_queue_n);
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3707 " t->pid: %u, t->ack: %u\n",
3708 t->fwd_pid, t->last_fwd_ack);
3712 /* Ok, ACK might be necessary, what PID to ACK? */
3713 ack = tunnel_get_fwd_ack (t);
3715 /* If speed_min and not all children have ack'd, dont send yet */
3716 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3722 t->last_fwd_ack = ack;
3723 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3724 send_ack (t, &id, ack);
3726 t->force_ack = GNUNET_NO;
3731 * Iterator to send a child node a BCK ACK to allow him to send more
3734 * @param cls Closure (tunnel).
3735 * @param id Id of the child node.
3738 tunnel_send_child_bck_ack (void *cls,
3741 struct MeshTunnel *t = cls;
3742 struct MeshTunnelChildInfo *cinfo;
3743 struct GNUNET_PeerIdentity peer;
3746 GNUNET_PEER_resolve (id, &peer);
3747 cinfo = tunnel_get_neighbor_fc (t, &peer);
3748 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3750 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3753 " Not sending ACK, not needed\n");
3756 cinfo->bck_ack = ack;
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 " Sending BCK ACK %u (last sent: %u)\n",
3760 ack, cinfo->bck_ack);
3761 send_ack (t, &peer, ack);
3766 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3768 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3770 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3771 * - implement a size 1 buffer in each client_fc AND children_fc
3772 * to hold at least 1 message per "child".
3773 * problem: violates no buffer policy
3774 * - ack 0 and make "children" poll for transmission slots
3775 * problem: big overhead, extra latency even in low traffic
3778 * @param t Tunnel on which to send the BCK ACKs.
3781 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3784 unsigned int tunnel_delta;
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3788 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3791 /* Find client whom to allow to send to origin (with lowest buffer space) */
3792 for (i = 0; i < t->nclients; i++)
3794 struct MeshTunnelClientInfo *clinfo;
3797 clinfo = &t->clients_fc[i];
3798 delta = clinfo->bck_ack - clinfo->bck_pid;
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3800 t->clients[i]->id, delta);
3802 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3803 (GNUNET_YES == t->nobuffer && 0 == delta))
3807 ack = clinfo->bck_pid;
3808 ack += t->nobuffer ? 1 : tunnel_delta;
3809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3810 " sending ack to client %u: %u\n",
3811 t->clients[i]->id, ack);
3812 send_local_ack (t, t->clients[i], ack);
3813 clinfo->bck_ack = ack;
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 " not sending ack to client %u (td %u, d %u)\n",
3819 t->clients[i]->id, tunnel_delta, delta);
3826 * Send an ACK informing the children nodes and destination clients about
3827 * the available buffer space.
3828 * If buffering is off, send only on behalf of root (can be self).
3829 * If buffering is on, send when sent to predecessor and buffer space is free.
3830 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3831 * the ACK itself goes "forward" (towards children/clients).
3833 * @param t Tunnel on which to send the ACK.
3834 * @param type Type of message that triggered the ACK transmission.
3837 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3840 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3841 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3842 /* Is it after data to_origin retransmission? */
3845 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3846 if (GNUNET_YES == t->nobuffer)
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 " Not sending ACK, nobuffer\n");
3853 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3854 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3856 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3857 t->force_ack = GNUNET_YES;
3863 tunnel_send_clients_bck_ack (t);
3864 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3865 t->force_ack = GNUNET_NO;
3870 * @brief Re-initiate traffic to this peer if necessary.
3872 * Check if there is traffic queued towards this peer
3873 * and the core transmit handle is NULL (traffic was stalled).
3874 * If so, call core tmt rdy.
3876 * @param cls Closure (unused)
3877 * @param peer_id Short ID of peer to which initiate traffic.
3880 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3882 struct MeshPeerInfo *peer;
3883 struct GNUNET_PeerIdentity id;
3884 struct MeshPeerQueue *q;
3887 peer = peer_info_get_short(peer_id);
3888 if (NULL != peer->core_transmit)
3891 q = queue_get_next(peer);
3894 /* Might br multicast traffic already sent to this particular peer but
3895 * not to other children in this tunnel.
3896 * This way t->queue_n would be > 0 but the queue of this particular peer
3902 GNUNET_PEER_resolve (peer->id, &id);
3903 peer->core_transmit =
3904 GNUNET_CORE_notify_transmit_ready(core_handle,
3907 GNUNET_TIME_UNIT_FOREVER_REL,
3917 * @brief Allow transmission of FWD traffic on this tunnel
3919 * Check if there is traffic queued towards any children
3920 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3922 * @param t Tunnel on which to unlock FWD traffic.
3925 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3927 if (0 == t->fwd_queue_n)
3930 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3935 * @brief Allow transmission of BCK traffic on this tunnel
3937 * Check if there is traffic queued towards the root of the tree
3938 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3940 * @param t Tunnel on which to unlock BCK traffic.
3943 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3945 if (0 == t->bck_queue_n)
3948 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3953 * Send a message to all peers in this tunnel that the tunnel is no longer
3956 * @param t The tunnel whose peers to notify.
3957 * @param parent ID of the parent, in case the tree is already destroyed.
3960 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3962 struct GNUNET_MESH_TunnelDestroy msg;
3963 struct GNUNET_PeerIdentity id;
3965 msg.header.size = htons (sizeof (msg));
3966 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3967 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3968 msg.tid = htonl (t->id.tid);
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3970 " sending tunnel destroy for tunnel: %s [%X]\n",
3971 GNUNET_i2s (&msg.oid), t->id.tid);
3972 if (tree_count_children(t->tree) > 0)
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
3975 tunnel_send_multicast (t, &msg.header);
3978 parent = tree_get_predecessor (t->tree);
3979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3983 GNUNET_PEER_resolve (parent, &id);
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3985 " sending back to %s\n",
3987 send_prebuilt_message (&msg.header, &id, t);
3992 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3994 * @param cls Closure (Tunnel which to cancel).
3995 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3998 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4000 struct MeshTunnel *t = cls;
4001 struct MeshPeerInfo *peer_info;
4002 struct MeshPeerQueue *pq;
4003 struct MeshPeerQueue *next;
4005 peer_info = peer_info_get_short (neighbor_id);
4006 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4009 if (pq->tunnel == t)
4011 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4012 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4013 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4015 // Should have been removed on destroy children
4018 queue_destroy (pq, GNUNET_YES);
4021 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4023 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4024 peer_info->core_transmit = NULL;
4029 * Destroy the tunnel and free any allocated resources linked to it.
4031 * @param t the tunnel to destroy
4033 * @return GNUNET_OK on success
4036 tunnel_destroy (struct MeshTunnel *t)
4038 struct MeshClient *c;
4039 struct GNUNET_HashCode hash;
4050 struct GNUNET_PeerIdentity id;
4052 GNUNET_PEER_resolve (t->id.oid, &id);
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4054 GNUNET_i2s (&id), t->id.tid);
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4060 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4061 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4069 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4071 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4078 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4079 for (i = 0; i < t->nclients; i++)
4083 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4089 for (i = 0; i < t->nignore; i++)
4093 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4100 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4101 GNUNET_free_non_null (t->clients);
4102 GNUNET_free_non_null (t->ignore);
4103 GNUNET_free_non_null (t->clients_fc);
4105 if (NULL != t->peers)
4107 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4109 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4112 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4113 &tunnel_destroy_child,
4115 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4116 t->children_fc = NULL;
4118 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4119 tree_destroy (t->tree);
4121 if (NULL != t->regex_search)
4122 GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
4123 if (NULL != t->dht_get_type)
4124 GNUNET_DHT_get_stop (t->dht_get_type);
4125 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4126 GNUNET_SCHEDULER_cancel (t->timeout_task);
4127 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4128 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4131 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4136 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4139 * Tunnel is empty: destroy it.
4141 * @param cls Closure (Tunnel).
4142 * @param tc TaskContext.
4145 tunnel_destroy_empty_delayed (void *cls,
4146 const struct GNUNET_SCHEDULER_TaskContext *tc)
4148 struct MeshTunnel *t = cls;
4150 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4151 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4154 if (0 != t->nclients ||
4155 0 != tree_count_children (t->tree))
4160 struct GNUNET_PeerIdentity id;
4162 GNUNET_PEER_resolve (t->id.oid, &id);
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4164 "executing destruction of empty tunnel %s [%X]\n",
4165 GNUNET_i2s (&id), t->id.tid);
4169 tunnel_send_destroy (t, 0);
4170 if (0 == t->pending_messages)
4173 t->destroy = GNUNET_YES;
4178 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4180 * @param t Tunnel to destroy if empty.
4183 tunnel_destroy_empty (struct MeshTunnel *t)
4185 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4187 0 != tree_count_children (t->tree))
4189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4191 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4197 struct GNUNET_PeerIdentity id;
4199 GNUNET_PEER_resolve (t->id.oid, &id);
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "scheduling destruction of empty tunnel %s [%X]\n",
4202 GNUNET_i2s (&id), t->id.tid);
4206 t->delayed_destroy =
4207 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4208 &tunnel_destroy_empty_delayed,
4214 * Create a new tunnel
4216 * @param owner Who is the owner of the tunnel (short ID).
4217 * @param tid Tunnel Number of the tunnel.
4218 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4219 * @param local Tunnel Number for the tunnel, for the client point of view.
4221 * @return A new initialized tunnel. NULL on error.
4223 static struct MeshTunnel *
4224 tunnel_new (GNUNET_PEER_Id owner,
4225 MESH_TunnelNumber tid,
4226 struct MeshClient *client,
4227 MESH_TunnelNumber local)
4229 struct MeshTunnel *t;
4230 struct GNUNET_HashCode hash;
4232 if (n_tunnels >= max_tunnels && NULL == client)
4235 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4238 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4239 t->bck_queue_max = t->fwd_queue_max;
4240 t->tree = tree_new (owner);
4242 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4243 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4244 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4245 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4246 t->local_tid = local;
4247 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4249 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4251 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4253 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4254 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4261 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4268 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4270 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4271 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4275 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4284 * Callback when removing children from a tunnel tree. Notify owner.
4286 * @param cls Closure (tunnel).
4287 * @param peer_id Short ID of the peer deleted.
4290 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4292 struct MeshTunnel *t = cls;
4294 client_notify_peer_disconnected (t->owner, t, peer_id);
4298 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4299 * that are no longer needed, as well as nodes of no longer reachable peers.
4300 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4302 * @param t Tunnel from which to remove the path.
4303 * @param peer Short id of the peer which should be removed.
4306 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4310 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4314 "Tunnel %u [%u] has no more nodes\n",
4315 t->id.oid, t->id.tid);
4321 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4322 * client when the client disconnects. If the client is not the owner, the
4323 * owner will get notified if no more clients are in the tunnel and the client
4324 * get removed from the tunnel's list.
4326 * @param cls closure (client that is disconnecting)
4327 * @param key the hash of the local tunnel id (used to access the hashmap)
4328 * @param value the value stored at the key (tunnel to destroy)
4330 * @return GNUNET_OK, keep iterating.
4333 tunnel_destroy_iterator (void *cls,
4334 const struct GNUNET_HashCode * key,
4337 struct MeshTunnel *t = value;
4338 struct MeshClient *c = cls;
4340 send_client_tunnel_disconnect (t, c);
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4344 tunnel_delete_client (t, c);
4345 client_delete_tunnel (c, t);
4346 tunnel_destroy_empty (t);
4349 tunnel_send_destroy (t, 0);
4351 t->destroy = GNUNET_YES;
4358 * Timeout function, destroys tunnel if called
4360 * @param cls Closure (tunnel to destroy).
4361 * @param tc TaskContext
4364 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4366 struct MeshTunnel *t = cls;
4367 struct GNUNET_PeerIdentity id;
4369 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4370 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4372 GNUNET_PEER_resolve(t->id.oid, &id);
4373 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4374 "Tunnel %s [%X] timed out. Destroying.\n",
4375 GNUNET_i2s(&id), t->id.tid);
4376 send_clients_tunnel_destroy (t);
4381 * Resets the tunnel timeout. Starts it if no timeout was running.
4383 * @param t Tunnel whose timeout to reset.
4385 * TODO use heap to improve efficiency of scheduler.
4388 tunnel_reset_timeout (struct MeshTunnel *t)
4390 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4391 GNUNET_SCHEDULER_cancel (t->timeout_task);
4393 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4394 (refresh_path_time, 4), &tunnel_timeout, t);
4398 /******************************************************************************/
4399 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4400 /******************************************************************************/
4403 * Function to send a create path packet to a peer.
4405 * @param cls closure
4406 * @param size number of bytes available in buf
4407 * @param buf where the callee should write the message
4408 * @return number of bytes written to buf
4411 send_core_path_create (void *cls, size_t size, void *buf)
4413 struct MeshPathInfo *info = cls;
4414 struct GNUNET_MESH_ManipulatePath *msg;
4415 struct GNUNET_PeerIdentity *peer_ptr;
4416 struct MeshTunnel *t = info->t;
4417 struct MeshPeerPath *p = info->path;
4422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4424 sizeof (struct GNUNET_MESH_ManipulatePath) +
4425 p->length * sizeof (struct GNUNET_PeerIdentity);
4427 if (size < size_needed || NULL == buf)
4432 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4433 msg->header.size = htons (size_needed);
4434 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4435 msg->tid = ntohl (t->id.tid);
4438 if (GNUNET_YES == t->speed_min)
4439 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4440 if (GNUNET_YES == t->nobuffer)
4441 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4442 msg->opt = htonl(opt);
4445 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4446 for (i = 0; i < p->length; i++)
4448 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4461 * Fill the core buffer
4463 * @param cls closure (data itself)
4464 * @param size number of bytes available in buf
4465 * @param buf where the callee should write the message
4467 * @return number of bytes written to buf
4470 send_core_data_multicast (void *cls, size_t size, void *buf)
4472 struct MeshTransmissionDescriptor *info = cls;
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4476 GNUNET_assert (NULL != info);
4477 GNUNET_assert (NULL != info->peer);
4478 total_size = info->mesh_data->data_len;
4479 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4481 if (total_size > size)
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4487 memcpy (buf, info->mesh_data->data, total_size);
4490 struct GNUNET_MESH_Multicast *mc;
4491 struct GNUNET_MessageHeader *mh;
4494 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4496 mc = (struct GNUNET_MESH_Multicast *) mh;
4497 mh = (struct GNUNET_MessageHeader *) &mc[1];
4498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4499 " multicast, payload type %s\n",
4500 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4502 " multicast, payload size %u\n", ntohs (mh->size));
4506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4507 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4511 data_descriptor_decrement_rc (info->mesh_data);
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4520 * Creates a path ack message in buf and frees all unused resources.
4522 * @param cls closure (MeshTransmissionDescriptor)
4523 * @param size number of bytes available in buf
4524 * @param buf where the callee should write the message
4525 * @return number of bytes written to buf
4528 send_core_path_ack (void *cls, size_t size, void *buf)
4530 struct MeshTransmissionDescriptor *info = cls;
4531 struct GNUNET_MESH_PathACK *msg = buf;
4533 GNUNET_assert (NULL != info);
4534 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4539 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4540 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4541 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4542 msg->tid = htonl (info->origin->tid);
4543 msg->peer_id = my_full_id;
4546 /* TODO add signature */
4548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4549 return sizeof (struct GNUNET_MESH_PathACK);
4554 * Free a transmission that was already queued with all resources
4555 * associated to the request.
4557 * @param queue Queue handler to cancel.
4558 * @param clear_cls Is it necessary to free associated cls?
4561 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4563 struct MeshTransmissionDescriptor *dd;
4564 struct MeshPathInfo *path_info;
4565 struct MeshTunnelChildInfo *cinfo;
4566 struct GNUNET_PeerIdentity id;
4570 if (GNUNET_YES == clear_cls)
4572 switch (queue->type)
4574 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4575 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4576 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4578 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4579 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4580 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4581 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4582 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4583 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 " prebuilt message\n");
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4588 GNUNET_MESH_DEBUG_M2S(queue->type));
4590 data_descriptor_decrement_rc (dd->mesh_data);
4592 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4594 path_info = queue->cls;
4595 path_destroy (path_info->path);
4599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4600 " type %s unknown!\n",
4601 GNUNET_MESH_DEBUG_M2S(queue->type));
4603 GNUNET_free_non_null (queue->cls);
4605 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4606 queue->peer->queue_tail,
4609 /* Delete from child_fc in the appropiate tunnel */
4610 max = queue->tunnel->fwd_queue_max;
4611 GNUNET_PEER_resolve (queue->peer->id, &id);
4612 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4615 for (i = 0; i < cinfo->send_buffer_n; i++)
4618 i2 = (cinfo->send_buffer_start + i) % max;
4619 if (cinfo->send_buffer[i2] == queue)
4621 /* Found corresponding entry in the send_buffer. Move all others back. */
4626 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4628 j2 = (cinfo->send_buffer_start + j) % max;
4629 j3 = (cinfo->send_buffer_start + j + 1) % max;
4630 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4633 cinfo->send_buffer[j3] = NULL;
4634 cinfo->send_buffer_n--;
4639 GNUNET_free (queue);
4644 * @brief Get the next transmittable message from the queue.
4646 * This will be the head, except in the case of being a data packet
4647 * not allowed by the destination peer.
4649 * @param peer Destination peer.
4651 * @return The next viable MeshPeerQueue element to send to that peer.
4652 * NULL when there are no transmittable messages.
4654 struct MeshPeerQueue *
4655 queue_get_next (const struct MeshPeerInfo *peer)
4657 struct MeshPeerQueue *q;
4658 struct MeshTunnel *t;
4659 struct MeshTransmissionDescriptor *info;
4660 struct MeshTunnelChildInfo *cinfo;
4661 struct GNUNET_MESH_Unicast *ucast;
4662 struct GNUNET_MESH_ToOrigin *to_orig;
4663 struct GNUNET_MESH_Multicast *mcast;
4664 struct GNUNET_PeerIdentity id;
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4669 for (q = peer->queue_head; NULL != q; q = q->next)
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4675 GNUNET_MESH_DEBUG_M2S(q->type));
4678 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4679 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4680 pid = ntohl (ucast->pid);
4681 GNUNET_PEER_resolve (info->peer->id, &id);
4682 cinfo = tunnel_get_neighbor_fc(t, &id);
4683 ack = cinfo->fwd_ack;
4685 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4686 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4687 pid = ntohl (to_orig->pid);
4690 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4691 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4692 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4694 // Not a multicast payload: multicast control traffic (destroy, etc)
4697 pid = ntohl (mcast->pid);
4698 GNUNET_PEER_resolve (info->peer->id, &id);
4699 cinfo = tunnel_get_neighbor_fc(t, &id);
4700 ack = cinfo->fwd_ack;
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4708 "********* ACK: %u, PID: %u\n",
4710 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4719 "********* NEXT!\n");
4722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4723 "********* nothing found\n");
4729 * Core callback to write a queued packet to core buffer
4731 * @param cls Closure (peer info).
4732 * @param size Number of bytes available in buf.
4733 * @param buf Where the to write the message.
4735 * @return number of bytes written to buf
4738 queue_send (void *cls, size_t size, void *buf)
4740 struct MeshPeerInfo *peer = cls;
4741 struct GNUNET_MessageHeader *msg;
4742 struct MeshPeerQueue *queue;
4743 struct MeshTunnel *t;
4744 struct MeshTunnelChildInfo *cinfo;
4745 struct GNUNET_PeerIdentity dst_id;
4748 peer->core_transmit = NULL;
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4752 queue = queue_get_next (peer);
4754 /* Queue has no internal mesh traffic nor sendable payload */
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4758 if (NULL == peer->queue_head)
4759 GNUNET_break (0); // Should've been canceled
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4764 GNUNET_PEER_resolve (peer->id, &dst_id);
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4766 "********* towards %s\n",
4767 GNUNET_i2s(&dst_id));
4768 /* Check if buffer size is enough for the message */
4769 if (queue->size > size)
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4772 "********* not enough room, reissue\n");
4773 peer->core_transmit =
4774 GNUNET_CORE_notify_transmit_ready (core_handle,
4777 GNUNET_TIME_UNIT_FOREVER_REL,
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4787 GNUNET_assert (0 < t->pending_messages);
4788 t->pending_messages--;
4789 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4793 "********* unicast: t->q (%u/%u)\n",
4794 t->fwd_queue_n, t->fwd_queue_max);
4796 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4803 switch (queue->type)
4806 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4807 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4808 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4809 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4810 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4812 "********* raw: %s\n",
4813 GNUNET_MESH_DEBUG_M2S (queue->type));
4815 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4816 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4817 data_size = send_core_data_raw (queue->cls, size, buf);
4818 msg = (struct GNUNET_MessageHeader *) buf;
4819 switch (ntohs (msg->type)) // Type of preconstructed message
4821 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4822 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4824 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4825 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4831 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4834 struct MeshTransmissionDescriptor *info = queue->cls;
4836 if ((1 == info->mesh_data->reference_counter
4837 && GNUNET_YES == t->speed_min)
4839 (info->mesh_data->total_out == info->mesh_data->reference_counter
4840 && GNUNET_NO == t->speed_min))
4842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4843 "********* considered sent\n");
4848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4849 "********* NOT considered sent yet\n");
4850 t->pending_messages++;
4853 data_size = send_core_data_multicast(queue->cls, size, buf);
4854 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4856 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4858 data_size = send_core_path_create (queue->cls, size, buf);
4860 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4862 data_size = send_core_path_ack (queue->cls, size, buf);
4864 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4866 data_size = send_core_data_multicast (queue->cls, size, buf);
4870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4871 "********* type unknown: %u\n",
4875 switch (queue->type)
4877 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4878 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4879 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4880 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4881 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4885 "at pos %u (%p) != %p\n",
4886 cinfo->send_buffer_start,
4887 cinfo->send_buffer[cinfo->send_buffer_start],
4890 if (cinfo->send_buffer_n > 0)
4892 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4893 cinfo->send_buffer_n--;
4894 cinfo->send_buffer_start++;
4895 cinfo->send_buffer_start %= t->fwd_queue_max;
4906 /* Free queue, but cls was freed by send_core_* */
4907 queue_destroy (queue, GNUNET_NO);
4909 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4915 /* If more data in queue, send next */
4916 queue = queue_get_next(peer);
4919 struct GNUNET_PeerIdentity id;
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4922 GNUNET_PEER_resolve (peer->id, &id);
4923 peer->core_transmit =
4924 GNUNET_CORE_notify_transmit_ready(core_handle,
4927 GNUNET_TIME_UNIT_FOREVER_REL,
4935 if (NULL != peer->queue_head)
4937 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4938 "********* %s stalled\n",
4939 GNUNET_i2s(&my_full_id));
4941 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4942 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4943 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4945 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4946 &tunnel_poll, cinfo);
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4956 * @brief Queue and pass message to core when possible.
4958 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4959 * and accounts for it. In case the queue is full, the message is dropped and
4962 * Otherwise, message is treated as internal and allowed to go regardless of
4965 * @param cls Closure (@c type dependant). It will be used by queue_send to
4966 * build the message to be sent if not already prebuilt.
4967 * @param type Type of the message, 0 for a raw message.
4968 * @param size Size of the message.
4969 * @param dst Neighbor to send message to.
4970 * @param t Tunnel this message belongs to.
4973 queue_add (void *cls, uint16_t type, size_t size,
4974 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4976 struct MeshPeerQueue *queue;
4977 struct MeshTunnelChildInfo *cinfo;
4978 struct GNUNET_PeerIdentity id;
4984 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4985 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4987 n = &t->fwd_queue_n;
4988 max = &t->fwd_queue_max;
4990 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4992 n = &t->bck_queue_n;
4993 max = &t->bck_queue_max;
5000 GNUNET_STATISTICS_update(stats,
5001 "# messages dropped (buffer full)",
5003 return; // Drop message
5007 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5013 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5014 GNUNET_PEER_resolve (dst->id, &id);
5015 if (NULL == dst->core_transmit)
5017 dst->core_transmit =
5018 GNUNET_CORE_notify_transmit_ready (core_handle,
5021 GNUNET_TIME_UNIT_FOREVER_REL,
5027 t->pending_messages++;
5028 if (NULL == n) // Is this internal mesh traffic?
5031 // It's payload, keep track of buffer per peer.
5032 cinfo = tunnel_get_neighbor_fc(t, &id);
5033 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5034 if (NULL != cinfo->send_buffer[i])
5036 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5037 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5038 cinfo->send_buffer_start++;
5039 cinfo->send_buffer_start %= t->fwd_queue_max;
5043 cinfo->send_buffer_n++;
5045 cinfo->send_buffer[i] = queue;
5046 if (cinfo->send_buffer_n > t->fwd_queue_max)
5049 cinfo->send_buffer_n = t->fwd_queue_max;
5054 /******************************************************************************/
5055 /******************** MESH NETWORK HANDLERS **************************/
5056 /******************************************************************************/
5060 * Core handler for path creation
5062 * @param cls closure
5063 * @param message message
5064 * @param peer peer identity this notification is about
5066 * @return GNUNET_OK to keep the connection open,
5067 * GNUNET_SYSERR to close it (signal serious error)
5070 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5071 const struct GNUNET_MessageHeader *message)
5073 unsigned int own_pos;
5076 MESH_TunnelNumber tid;
5077 struct GNUNET_MESH_ManipulatePath *msg;
5078 struct GNUNET_PeerIdentity *pi;
5079 struct GNUNET_HashCode hash;
5080 struct MeshPeerPath *path;
5081 struct MeshPeerInfo *dest_peer_info;
5082 struct MeshPeerInfo *orig_peer_info;
5083 struct MeshTunnel *t;
5085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5086 "Received a path create msg [%s]\n",
5087 GNUNET_i2s (&my_full_id));
5088 size = ntohs (message->size);
5089 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5091 GNUNET_break_op (0);
5095 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5096 if (size % sizeof (struct GNUNET_PeerIdentity))
5098 GNUNET_break_op (0);
5101 size /= sizeof (struct GNUNET_PeerIdentity);
5104 GNUNET_break_op (0);
5107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5108 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5110 tid = ntohl (msg->tid);
5111 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5113 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5114 t = tunnel_get (pi, tid);
5115 if (NULL == t) // FIXME only for INCOMING tunnels?
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5120 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5123 // FIXME notify failure
5126 opt = ntohl (msg->opt);
5127 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5128 GNUNET_YES : GNUNET_NO;
5129 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5131 t->nobuffer = GNUNET_YES;
5132 t->last_fwd_ack = t->fwd_pid + 1;
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5135 " speed_min: %d, nobuffer:%d\n",
5136 t->speed_min, t->nobuffer);
5138 if (GNUNET_YES == t->nobuffer)
5140 t->bck_queue_max = 1;
5141 t->fwd_queue_max = 1;
5144 // FIXME only assign a local tid if a local client is interested (on demand)
5145 while (NULL != tunnel_get_incoming (next_local_tid))
5146 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5147 t->local_tid_dest = next_local_tid++;
5148 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5151 tunnel_reset_timeout (t);
5152 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5154 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5163 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5164 if (NULL == dest_peer_info)
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5167 " Creating PeerInfo for destination.\n");
5168 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5169 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5170 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5172 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5174 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5175 if (NULL == orig_peer_info)
5177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5178 " Creating PeerInfo for origin.\n");
5179 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5180 orig_peer_info->id = GNUNET_PEER_intern (pi);
5181 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5182 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5185 path = path_new (size);
5187 for (i = 0; i < size; i++)
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5190 GNUNET_i2s (&pi[i]));
5191 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5192 if (path->peers[i] == myid)
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5198 /* cannot be self, must be 'not found' */
5199 /* create path: self not found in path through self */
5200 GNUNET_break_op (0);
5201 path_destroy (path);
5205 path_add_to_peers (path, GNUNET_NO);
5206 tunnel_add_path (t, path, own_pos);
5207 if (own_pos == size - 1)
5209 /* It is for us! Send ack. */
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5211 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5212 if (NULL == t->peers)
5214 /* New tunnel! Notify clients on first payload message. */
5215 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5217 GNUNET_break (GNUNET_SYSERR !=
5218 GNUNET_CONTAINER_multihashmap_put (t->peers,
5219 &my_full_id.hashPubKey,
5222 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5227 struct MeshPeerPath *path2;
5229 /* It's for somebody else! Retransmit. */
5230 path2 = path_duplicate (path);
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5232 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5233 path2 = path_duplicate (path);
5234 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5235 send_create_path (dest_peer_info, path, t);
5242 * Core handler for path destruction
5244 * @param cls closure
5245 * @param message message
5246 * @param peer peer identity this notification is about
5248 * @return GNUNET_OK to keep the connection open,
5249 * GNUNET_SYSERR to close it (signal serious error)
5252 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5253 const struct GNUNET_MessageHeader *message)
5255 struct GNUNET_MESH_ManipulatePath *msg;
5256 struct GNUNET_PeerIdentity *pi;
5257 struct MeshPeerPath *path;
5258 struct MeshTunnel *t;
5259 unsigned int own_pos;
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5264 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5265 size = ntohs (message->size);
5266 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5268 GNUNET_break_op (0);
5272 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5273 if (size % sizeof (struct GNUNET_PeerIdentity))
5275 GNUNET_break_op (0);
5278 size /= sizeof (struct GNUNET_PeerIdentity);
5281 GNUNET_break_op (0);
5284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5286 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5287 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5289 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5291 t = tunnel_get (pi, ntohl (msg->tid));
5294 /* TODO notify back: we don't know this tunnel */
5295 GNUNET_break_op (0);
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5299 path = path_new (size);
5301 for (i = 0; i < size; i++)
5303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5304 GNUNET_i2s (&pi[i]));
5305 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5306 if (path->peers[i] == myid)
5309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5310 if (own_pos < path->length - 1)
5311 send_prebuilt_message (message, &pi[own_pos + 1], t);
5313 send_client_tunnel_disconnect(t, NULL);
5315 tunnel_delete_peer (t, path->peers[path->length - 1]);
5316 path_destroy (path);
5322 * Core handler for notifications of broken paths
5324 * @param cls closure
5325 * @param message message
5326 * @param peer peer identity this notification is about
5328 * @return GNUNET_OK to keep the connection open,
5329 * GNUNET_SYSERR to close it (signal serious error)
5332 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5333 const struct GNUNET_MessageHeader *message)
5335 struct GNUNET_MESH_PathBroken *msg;
5336 struct MeshTunnel *t;
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5339 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5340 msg = (struct GNUNET_MESH_PathBroken *) message;
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5342 GNUNET_i2s (&msg->peer1));
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5344 GNUNET_i2s (&msg->peer2));
5345 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5348 GNUNET_break_op (0);
5351 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5352 GNUNET_PEER_search (&msg->peer2));
5359 * Core handler for tunnel destruction
5361 * @param cls closure
5362 * @param message message
5363 * @param peer peer identity this notification is about
5365 * @return GNUNET_OK to keep the connection open,
5366 * GNUNET_SYSERR to close it (signal serious error)
5369 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5370 const struct GNUNET_MessageHeader *message)
5372 struct GNUNET_MESH_TunnelDestroy *msg;
5373 struct MeshTunnel *t;
5374 GNUNET_PEER_Id parent;
5377 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5379 "Got a TUNNEL DESTROY packet from %s\n",
5381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5382 " for tunnel %s [%u]\n",
5383 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5384 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5385 /* Check signature */
5388 /* Probably already got the message from another path,
5389 * destroyed the tunnel and retransmitted to children.
5392 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5396 parent = tree_get_predecessor (t->tree);
5397 pid = GNUNET_PEER_search (peer);
5402 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5403 nc = tree_count_children (t->tree);
5404 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5407 "still in use: %u cl, %u ch\n",
5412 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5414 /* Tunnel was incoming, notify clients */
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5416 t->local_tid, t->local_tid_dest);
5417 send_clients_tunnel_destroy (t);
5419 tunnel_send_destroy (t, parent);
5420 t->destroy = GNUNET_YES;
5421 // TODO: add timeout to destroy the tunnel anyway
5427 * Core handler for mesh network traffic going from the origin to a peer
5429 * @param cls closure
5430 * @param peer peer identity this notification is about
5431 * @param message message
5432 * @return GNUNET_OK to keep the connection open,
5433 * GNUNET_SYSERR to close it (signal serious error)
5436 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5437 const struct GNUNET_MessageHeader *message)
5439 struct GNUNET_MESH_Unicast *msg;
5440 struct GNUNET_PeerIdentity *neighbor;
5441 struct MeshTunnelChildInfo *cinfo;
5442 struct MeshTunnel *t;
5443 GNUNET_PEER_Id dest_id;
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5451 size = ntohs (message->size);
5453 sizeof (struct GNUNET_MESH_Unicast) +
5454 sizeof (struct GNUNET_MessageHeader))
5459 msg = (struct GNUNET_MESH_Unicast *) message;
5460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5461 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5463 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5466 /* TODO notify back: we don't know this tunnel */
5467 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5468 GNUNET_break_op (0);
5471 pid = ntohl (msg->pid);
5472 if (t->fwd_pid == pid)
5474 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5476 " Already seen pid %u, DROPPING!\n", pid);
5481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5482 " pid %u not seen yet, forwarding\n", pid);
5485 t->skip += (pid - t->fwd_pid) - 1;
5488 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5490 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5491 GNUNET_break_op (0);
5492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5493 "Received PID %u, ACK %u\n",
5494 pid, t->last_fwd_ack);
5498 tunnel_reset_timeout (t);
5499 dest_id = GNUNET_PEER_search (&msg->destination);
5500 if (dest_id == myid)
5502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5503 " it's for us! sending to clients...\n");
5504 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5505 send_subscribed_clients (message, &msg[1].header, t);
5506 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5509 ttl = ntohl (msg->ttl);
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5513 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5515 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5519 " not for us, retransmitting...\n");
5521 neighbor = tree_get_first_hop (t->tree, dest_id);
5522 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5523 cinfo->fwd_pid = pid;
5524 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5527 if (GNUNET_YES == t->nobuffer &&
5528 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5530 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5531 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5532 GNUNET_break_op (0);
5535 send_prebuilt_message (message, neighbor, t);
5536 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5542 * Core handler for mesh network traffic going from the origin to all peers
5544 * @param cls closure
5545 * @param message message
5546 * @param peer peer identity this notification is about
5547 * @return GNUNET_OK to keep the connection open,
5548 * GNUNET_SYSERR to close it (signal serious error)
5550 * TODO: Check who we got this from, to validate route.
5553 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5554 const struct GNUNET_MessageHeader *message)
5556 struct GNUNET_MESH_Multicast *msg;
5557 struct MeshTunnel *t;
5561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5563 size = ntohs (message->size);
5564 if (sizeof (struct GNUNET_MESH_Multicast) +
5565 sizeof (struct GNUNET_MessageHeader) > size)
5567 GNUNET_break_op (0);
5570 msg = (struct GNUNET_MESH_Multicast *) message;
5571 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5575 /* TODO notify that we dont know that tunnel */
5576 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5577 GNUNET_break_op (0);
5580 pid = ntohl (msg->pid);
5581 if (t->fwd_pid == pid)
5583 /* already seen this packet, drop */
5584 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5586 " Already seen pid %u, DROPPING!\n", pid);
5587 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5593 " pid %u not seen yet, forwarding\n", pid);
5595 t->skip += (pid - t->fwd_pid) - 1;
5597 tunnel_reset_timeout (t);
5599 /* Transmit to locally interested clients */
5600 if (NULL != t->peers &&
5601 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5603 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5604 send_subscribed_clients (message, &msg[1].header, t);
5605 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5608 if (ntohl (msg->ttl) == 0)
5610 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5612 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5615 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5616 tunnel_send_multicast (t, message);
5622 * Core handler for mesh network traffic toward the owner of a tunnel
5624 * @param cls closure
5625 * @param message message
5626 * @param peer peer identity this notification is about
5628 * @return GNUNET_OK to keep the connection open,
5629 * GNUNET_SYSERR to close it (signal serious error)
5632 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5633 const struct GNUNET_MessageHeader *message)
5635 struct GNUNET_MESH_ToOrigin *msg;
5636 struct GNUNET_PeerIdentity id;
5637 struct MeshPeerInfo *peer_info;
5638 struct MeshTunnel *t;
5639 struct MeshTunnelChildInfo *cinfo;
5640 GNUNET_PEER_Id predecessor;
5644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5646 size = ntohs (message->size);
5647 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5648 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5650 GNUNET_break_op (0);
5653 msg = (struct GNUNET_MESH_ToOrigin *) message;
5654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5655 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5656 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5657 pid = ntohl (msg->pid);
5661 /* TODO notify that we dont know this tunnel (whom)? */
5662 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5664 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5665 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5669 cinfo = tunnel_get_neighbor_fc(t, peer);
5676 if (cinfo->bck_pid == pid)
5678 /* already seen this packet, drop */
5679 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5681 " Already seen pid %u, DROPPING!\n", pid);
5682 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5687 " pid %u not seen yet, forwarding\n", pid);
5688 cinfo->bck_pid = pid;
5690 if (NULL != t->owner)
5693 struct GNUNET_MESH_ToOrigin *copy;
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5696 " it's for us! sending to clients...\n");
5697 /* TODO signature verification */
5698 memcpy (cbuf, message, size);
5699 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5700 copy->tid = htonl (t->local_tid);
5702 copy->pid = htonl (t->bck_pid);
5703 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5704 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5705 ©->header, GNUNET_NO);
5706 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5710 " not for us, retransmitting...\n");
5712 peer_info = peer_info_get (&msg->oid);
5713 if (NULL == peer_info)
5715 /* unknown origin of tunnel */
5719 predecessor = tree_get_predecessor (t->tree);
5720 if (0 == predecessor)
5722 if (GNUNET_YES == t->destroy)
5724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5725 "to orig received on a dying tunnel %s [%X]\n",
5726 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5730 "unknown to origin at %s\n",
5731 GNUNET_i2s (&my_full_id));
5732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5736 "for tunnel %s [%X]\n",
5737 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5740 tree_debug (t->tree);
5743 GNUNET_PEER_resolve (predecessor, &id);
5744 send_prebuilt_message (message, &id, t);
5745 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5752 * Core handler for mesh network traffic point-to-point acks.
5754 * @param cls closure
5755 * @param message message
5756 * @param peer peer identity this notification is about
5758 * @return GNUNET_OK to keep the connection open,
5759 * GNUNET_SYSERR to close it (signal serious error)
5762 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5763 const struct GNUNET_MessageHeader *message)
5765 struct GNUNET_MESH_ACK *msg;
5766 struct MeshTunnel *t;
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5771 msg = (struct GNUNET_MESH_ACK *) message;
5773 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5777 /* TODO notify that we dont know this tunnel (whom)? */
5778 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5781 ack = ntohl (msg->pid);
5782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5784 /* Is this a forward or backward ACK? */
5785 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5787 struct MeshTunnelChildInfo *cinfo;
5790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5791 cinfo = tunnel_get_neighbor_fc (t, peer);
5792 cinfo->fwd_ack = ack;
5793 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5794 tunnel_unlock_fwd_queues (t);
5795 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5797 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5798 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5799 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5806 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5807 tunnel_unlock_bck_queue (t);
5814 * Core handler for mesh network traffic point-to-point ack polls.
5816 * @param cls closure
5817 * @param message message
5818 * @param peer peer identity this notification is about
5820 * @return GNUNET_OK to keep the connection open,
5821 * GNUNET_SYSERR to close it (signal serious error)
5824 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5825 const struct GNUNET_MessageHeader *message)
5827 struct GNUNET_MESH_Poll *msg;
5828 struct MeshTunnel *t;
5830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5833 msg = (struct GNUNET_MESH_Poll *) message;
5835 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5839 /* TODO notify that we dont know this tunnel (whom)? */
5840 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5841 GNUNET_break_op (0);
5845 /* Is this a forward or backward ACK? */
5846 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5848 struct MeshTunnelChildInfo *cinfo;
5850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5851 cinfo = tunnel_get_neighbor_fc (t, peer);
5852 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5853 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5858 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5865 * Core handler for path ACKs
5867 * @param cls closure
5868 * @param message message
5869 * @param peer peer identity this notification is about
5871 * @return GNUNET_OK to keep the connection open,
5872 * GNUNET_SYSERR to close it (signal serious error)
5875 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5876 const struct GNUNET_MessageHeader *message)
5878 struct GNUNET_MESH_PathACK *msg;
5879 struct GNUNET_PeerIdentity id;
5880 struct MeshPeerInfo *peer_info;
5881 struct MeshPeerPath *p;
5882 struct MeshTunnel *t;
5884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5885 GNUNET_i2s (&my_full_id));
5886 msg = (struct GNUNET_MESH_PathACK *) message;
5887 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5890 /* TODO notify that we don't know the tunnel */
5891 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5893 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5897 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5899 peer_info = peer_info_get (&msg->peer_id);
5900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5901 GNUNET_i2s (&msg->peer_id));
5902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5905 if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
5907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5908 "connect_by_string completed, stopping search\n");
5909 regex_cancel_search (t->regex_search);
5910 t->regex_search = NULL;
5913 /* Add paths to peers? */
5914 p = tree_get_path_to_peer (t->tree, peer_info->id);
5917 path_add_to_peers (p, GNUNET_YES);
5925 /* Message for us? */
5926 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5929 if (NULL == t->owner)
5931 GNUNET_break_op (0);
5934 if (NULL != t->dht_get_type)
5936 GNUNET_DHT_get_stop (t->dht_get_type);
5937 t->dht_get_type = NULL;
5939 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5941 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5942 send_client_peer_connected (t, peer_info->id);
5944 if (NULL != peer_info->dhtget)
5946 GNUNET_DHT_get_stop (peer_info->dhtget);
5947 peer_info->dhtget = NULL;
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5953 " not for us, retransmitting...\n");
5954 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5955 peer_info = peer_info_get (&msg->oid);
5956 send_prebuilt_message (message, &id, t);
5962 * Core handler for mesh keepalives.
5964 * @param cls closure
5965 * @param message message
5966 * @param peer peer identity this notification is about
5967 * @return GNUNET_OK to keep the connection open,
5968 * GNUNET_SYSERR to close it (signal serious error)
5970 * TODO: Check who we got this from, to validate route.
5973 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5974 const struct GNUNET_MessageHeader *message)
5976 struct GNUNET_MESH_TunnelKeepAlive *msg;
5977 struct MeshTunnel *t;
5979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5982 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5983 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5987 /* TODO notify that we dont know that tunnel */
5988 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
5993 tunnel_reset_timeout (t);
5995 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5996 tunnel_send_multicast (t, message);
6003 * Functions to handle messages from core
6005 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6006 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6007 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6008 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6009 sizeof (struct GNUNET_MESH_PathBroken)},
6010 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6011 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6012 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6013 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6014 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6015 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6016 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6017 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6018 sizeof (struct GNUNET_MESH_ACK)},
6019 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6020 sizeof (struct GNUNET_MESH_Poll)},
6021 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6022 sizeof (struct GNUNET_MESH_PathACK)},
6028 /******************************************************************************/
6029 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6030 /******************************************************************************/
6033 * deregister_app: iterator for removing each application registered by a client
6035 * @param cls closure
6036 * @param key the hash of the application id (used to access the hashmap)
6037 * @param value the value stored at the key (client)
6039 * @return GNUNET_OK on success
6042 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6044 struct MeshClient *c = cls;
6046 GNUNET_break (GNUNET_YES ==
6047 GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
6053 * notify_client_connection_failure: notify a client that the connection to the
6054 * requested remote peer is not possible (for instance, no route found)
6055 * Function called when the socket is ready to queue more data. "buf" will be
6056 * NULL and "size" zero if the socket was closed for writing in the meantime.
6058 * @param cls closure
6059 * @param size number of bytes available in buf
6060 * @param buf where the callee should write the message
6061 * @return number of bytes written to buf
6064 notify_client_connection_failure (void *cls, size_t size, void *buf)
6067 struct MeshPeerInfo *peer_info;
6068 struct GNUNET_MESH_PeerControl *msg;
6069 struct GNUNET_PeerIdentity id;
6071 if (0 == size && NULL == buf)
6073 // TODO retry? cancel?
6077 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6078 peer_info = (struct MeshPeerInfo *) cls;
6079 msg = (struct GNUNET_MESH_PeerControl *) buf;
6080 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6081 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6082 // msg->tunnel_id = htonl(peer_info->t->tid);
6083 GNUNET_PEER_resolve (peer_info->id, &id);
6084 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6092 * Send keepalive packets for a peer
6094 * @param cls Closure (tunnel for which to send the keepalive).
6095 * @param tc Notification context.
6098 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6100 struct MeshTunnel *t = cls;
6101 struct GNUNET_MESH_TunnelKeepAlive *msg;
6102 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6105 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6106 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6112 "sending keepalive for tunnel %d\n", t->id.tid);
6114 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6115 msg->header.size = htons (size);
6116 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6117 msg->oid = my_full_id;
6118 msg->tid = htonl (t->id.tid);
6119 tunnel_send_multicast (t, &msg->header);
6121 t->path_refresh_task =
6122 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6123 tunnel_reset_timeout(t);
6128 * Function to process paths received for a new peer addition. The recorded
6129 * paths form the initial tunnel, which can be optimized later.
6130 * Called on each result obtained for the DHT search.
6132 * @param cls closure
6133 * @param exp when will this value expire
6134 * @param key key of the result
6135 * @param get_path path of the get request
6136 * @param get_path_length lenght of get_path
6137 * @param put_path path of the put request
6138 * @param put_path_length length of the put_path
6139 * @param type type of the result
6140 * @param size number of bytes in data
6141 * @param data pointer to the result data
6143 * TODO: re-issue the request after certain time? cancel after X results?
6146 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6147 const struct GNUNET_HashCode * key,
6148 const struct GNUNET_PeerIdentity *get_path,
6149 unsigned int get_path_length,
6150 const struct GNUNET_PeerIdentity *put_path,
6151 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6152 size_t size, const void *data)
6154 struct MeshPathInfo *path_info = cls;
6155 struct MeshPeerPath *p;
6156 struct GNUNET_PeerIdentity pi;
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6160 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6163 p = path_build_from_dht (get_path, get_path_length,
6164 put_path, put_path_length);
6165 path_add_to_peers (p, GNUNET_NO);
6167 for (i = 0; i < path_info->peer->ntunnels; i++)
6169 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6170 peer_info_connect (path_info->peer, path_info->t);
6178 * Function to process paths received for a new peer addition. The recorded
6179 * paths form the initial tunnel, which can be optimized later.
6180 * Called on each result obtained for the DHT search.
6182 * @param cls closure
6183 * @param exp when will this value expire
6184 * @param key key of the result
6185 * @param get_path path of the get request
6186 * @param get_path_length lenght of get_path
6187 * @param put_path path of the put request
6188 * @param put_path_length length of the put_path
6189 * @param type type of the result
6190 * @param size number of bytes in data
6191 * @param data pointer to the result data
6194 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6195 const struct GNUNET_HashCode * key,
6196 const struct GNUNET_PeerIdentity *get_path,
6197 unsigned int get_path_length,
6198 const struct GNUNET_PeerIdentity *put_path,
6199 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6200 size_t size, const void *data)
6202 const struct PBlock *pb = data;
6203 const struct GNUNET_PeerIdentity *pi = &pb->id;
6204 struct MeshTunnel *t = cls;
6205 struct MeshPeerInfo *peer_info;
6206 struct MeshPeerPath *p;
6208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6209 if (size != sizeof (struct PBlock))
6211 GNUNET_break_op (0);
6214 if (ntohl(pb->type) != t->type)
6216 GNUNET_break_op (0);
6219 GNUNET_assert (NULL != t->owner);
6220 peer_info = peer_info_get (pi);
6221 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6223 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6225 p = path_build_from_dht (get_path, get_path_length, put_path,
6227 path_add_to_peers (p, GNUNET_NO);
6229 tunnel_add_peer (t, peer_info);
6230 peer_info_connect (peer_info, t);
6234 /******************************************************************************/
6235 /********************* MESH LOCAL HANDLES **************************/
6236 /******************************************************************************/
6240 * Handler for client disconnection
6242 * @param cls closure
6243 * @param client identification of the client; NULL
6244 * for the last call when the server is destroyed
6247 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6249 struct MeshClient *c;
6250 struct MeshClient *next;
6253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6263 if (c->handle != client)
6265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6271 GNUNET_SERVER_client_drop (c->handle);
6272 c->shutting_down = GNUNET_YES;
6273 GNUNET_assert (NULL != c->own_tunnels);
6274 GNUNET_assert (NULL != c->incoming_tunnels);
6275 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6276 &tunnel_destroy_iterator, c);
6277 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6278 &tunnel_destroy_iterator, c);
6279 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6280 &tunnel_destroy_iterator, c);
6281 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6282 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6283 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6285 /* deregister clients applications */
6286 if (NULL != c->apps)
6288 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
6289 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6291 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6292 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6294 GNUNET_SCHEDULER_cancel (announce_applications_task);
6295 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6297 if (NULL != c->types)
6298 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6299 for (i = 0; i < c->n_regex; i++)
6301 GNUNET_free (c->regexes[i].regex);
6302 if (NULL != c->regexes[i].h)
6303 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6305 GNUNET_free_non_null (c->regexes);
6306 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6307 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6309 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6312 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6320 * Handler for new clients
6322 * @param cls closure
6323 * @param client identification of the client
6324 * @param message the actual message, which includes messages the client wants
6327 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6328 const struct GNUNET_MessageHeader *message)
6330 struct GNUNET_MESH_ClientConnect *cc_msg;
6331 struct MeshClient *c;
6332 GNUNET_MESH_ApplicationType *a;
6339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6340 /* Check data sanity */
6341 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6342 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6343 ntypes = ntohs (cc_msg->types);
6344 napps = ntohs (cc_msg->applications);
6346 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6353 /* Create new client structure */
6354 c = GNUNET_malloc (sizeof (struct MeshClient));
6355 c->id = next_client_id++;
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6358 GNUNET_SERVER_client_keep (client);
6359 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6362 GNUNET_MESH_ApplicationType at;
6363 struct GNUNET_HashCode hc;
6365 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6366 for (i = 0; i < napps; i++)
6369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6370 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6371 /* store in clients hashmap */
6372 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6373 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6374 /* store in global hashmap, for announcements */
6375 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6378 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6379 announce_applications_task =
6380 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6386 struct GNUNET_HashCode hc;
6388 t = (uint16_t *) & a[napps];
6389 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6390 for (i = 0; i < ntypes; i++)
6393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6394 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6396 /* store in clients hashmap */
6397 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6398 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6399 /* store in global hashmap */
6400 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6405 " client has %u+%u subscriptions\n", napps, ntypes);
6407 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6408 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6409 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6410 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6411 GNUNET_SERVER_notification_context_add (nc, client);
6412 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6414 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6420 * Handler for clients announcing available services by a regular expression.
6422 * @param cls closure
6423 * @param client identification of the client
6424 * @param message the actual message, which includes messages the client wants
6427 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6428 const struct GNUNET_MessageHeader *message)
6430 const struct GNUNET_MESH_RegexAnnounce *msg;
6431 struct MeshRegexDescriptor rd;
6432 struct MeshClient *c;
6437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6439 /* Sanity check for client registration */
6440 if (NULL == (c = client_get (client)))
6443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6448 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6450 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6451 if (NULL != c->partial_regex)
6453 regex = c->partial_regex;
6454 offset = strlen (c->partial_regex);
6455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6456 " continuation, already have %u bytes\n",
6465 regex = GNUNET_realloc (regex, offset + len + 1);
6466 memcpy (®ex[offset], &msg[1], len);
6467 regex[offset + len] = '\0';
6468 if (0 == ntohs (msg->last))
6470 c->partial_regex = regex;
6471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6472 " not ended, stored %u bytes for later\n",
6474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6478 rd.compression = ntohs (msg->compression_characters);
6480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6483 GNUNET_array_append (c->regexes, c->n_regex, rd);
6484 c->partial_regex = NULL;
6485 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6487 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6499 * Handler for requests of new tunnels
6501 * @param cls closure
6502 * @param client identification of the client
6503 * @param message the actual message
6506 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6507 const struct GNUNET_MessageHeader *message)
6509 struct GNUNET_MESH_TunnelMessage *t_msg;
6510 struct MeshTunnel *t;
6511 struct MeshClient *c;
6512 MESH_TunnelNumber tid;
6514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6516 /* Sanity check for client registration */
6517 if (NULL == (c = client_get (client)))
6520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6525 /* Message sanity check */
6526 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6533 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6534 /* Sanity check for tunnel numbering */
6535 tid = ntohl (t_msg->tunnel_id);
6536 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6542 /* Sanity check for duplicate tunnel IDs */
6543 if (NULL != tunnel_get_by_local_id (c, tid))
6546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6550 while (NULL != tunnel_get_by_pi (myid, next_tid))
6551 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6552 t = tunnel_new (myid, next_tid++, c, tid);
6555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6560 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6562 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6563 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6566 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6572 * Handler for requests of deleting tunnels
6574 * @param cls closure
6575 * @param client identification of the client
6576 * @param message the actual message
6579 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6580 const struct GNUNET_MessageHeader *message)
6582 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6583 struct MeshClient *c;
6584 struct MeshTunnel *t;
6585 MESH_TunnelNumber tid;
6587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6588 "Got a DESTROY TUNNEL from client!\n");
6590 /* Sanity check for client registration */
6591 if (NULL == (c = client_get (client)))
6594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6599 /* Message sanity check */
6600 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6607 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6609 /* Retrieve tunnel */
6610 tid = ntohl (tunnel_msg->tunnel_id);
6611 t = tunnel_get_by_local_id(c, tid);
6614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6619 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6621 client_ignore_tunnel (c, t);
6622 tunnel_destroy_empty (t);
6623 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6626 send_client_tunnel_disconnect (t, c);
6627 client_delete_tunnel (c, t);
6629 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6631 tunnel_send_destroy (t, 0);
6632 t->destroy = GNUNET_YES;
6633 /* The tunnel will be destroyed when the last message is transmitted. */
6634 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6640 * Handler for requests of seeting tunnel's speed.
6642 * @param cls Closure (unused).
6643 * @param client Identification of the client.
6644 * @param message The actual message.
6647 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6648 const struct GNUNET_MessageHeader *message)
6650 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6651 struct MeshClient *c;
6652 struct MeshTunnel *t;
6653 MESH_TunnelNumber tid;
6655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6656 "Got a SPEED request from client!\n");
6658 /* Sanity check for client registration */
6659 if (NULL == (c = client_get (client)))
6662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6668 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6670 /* Retrieve tunnel */
6671 tid = ntohl (tunnel_msg->tunnel_id);
6672 t = tunnel_get_by_local_id(c, tid);
6675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6681 switch (ntohs(message->type))
6683 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6684 t->speed_min = GNUNET_YES;
6686 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6687 t->speed_min = GNUNET_NO;
6692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6697 * Handler for requests of seeting tunnel's buffering policy.
6699 * @param cls Closure (unused).
6700 * @param client Identification of the client.
6701 * @param message The actual message.
6704 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6705 const struct GNUNET_MessageHeader *message)
6707 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6708 struct MeshClient *c;
6709 struct MeshTunnel *t;
6710 MESH_TunnelNumber tid;
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6713 "Got a BUFFER request from client!\n");
6715 /* Sanity check for client registration */
6716 if (NULL == (c = client_get (client)))
6719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6724 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6726 /* Retrieve tunnel */
6727 tid = ntohl (tunnel_msg->tunnel_id);
6728 t = tunnel_get_by_local_id(c, tid);
6731 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6733 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6737 switch (ntohs(message->type))
6739 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6740 t->nobuffer = GNUNET_NO;
6742 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6743 t->nobuffer = GNUNET_YES;
6749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6754 * Handler for connection requests to new peers
6756 * @param cls closure
6757 * @param client identification of the client
6758 * @param message the actual message (PeerControl)
6761 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6762 const struct GNUNET_MessageHeader *message)
6764 struct GNUNET_MESH_PeerControl *peer_msg;
6765 struct MeshPeerInfo *peer_info;
6766 struct MeshClient *c;
6767 struct MeshTunnel *t;
6768 MESH_TunnelNumber tid;
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6771 /* Sanity check for client registration */
6772 if (NULL == (c = client_get (client)))
6775 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6782 /* Sanity check for message size */
6783 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6790 /* Tunnel exists? */
6791 tid = ntohl (peer_msg->tunnel_id);
6792 t = tunnel_get_by_local_id (c, tid);
6796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6800 /* Does client own tunnel? */
6801 if (t->owner->handle != client)
6804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6808 GNUNET_i2s (&peer_msg->peer));
6809 peer_info = peer_info_get (&peer_msg->peer);
6811 tunnel_add_peer (t, peer_info);
6812 peer_info_connect (peer_info, t);
6814 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6820 * Handler for disconnection requests of peers in a tunnel
6822 * @param cls closure
6823 * @param client identification of the client
6824 * @param message the actual message (PeerControl)
6827 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6828 const struct GNUNET_MessageHeader *message)
6830 struct GNUNET_MESH_PeerControl *peer_msg;
6831 struct MeshPeerInfo *peer_info;
6832 struct MeshClient *c;
6833 struct MeshTunnel *t;
6834 MESH_TunnelNumber tid;
6836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6837 /* Sanity check for client registration */
6838 if (NULL == (c = client_get (client)))
6841 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6846 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6848 /* Sanity check for message size */
6849 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6856 /* Tunnel exists? */
6857 tid = ntohl (peer_msg->tunnel_id);
6858 t = tunnel_get_by_local_id (c, tid);
6862 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6867 /* Does client own tunnel? */
6868 if (t->owner->handle != client)
6871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6876 GNUNET_i2s (&peer_msg->peer));
6877 /* Is the peer in the tunnel? */
6879 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6880 if (NULL == peer_info)
6883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6887 /* Ok, delete peer from tunnel */
6888 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6889 &peer_msg->peer.hashPubKey);
6891 send_destroy_path (t, peer_info->id);
6892 tunnel_delete_peer (t, peer_info->id);
6893 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6898 * Handler for blacklist requests of peers in a tunnel
6900 * @param cls closure
6901 * @param client identification of the client
6902 * @param message the actual message (PeerControl)
6904 * FIXME implement DHT block bloomfilter
6907 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6908 const struct GNUNET_MessageHeader *message)
6910 struct GNUNET_MESH_PeerControl *peer_msg;
6911 struct MeshClient *c;
6912 struct MeshTunnel *t;
6913 MESH_TunnelNumber tid;
6915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6916 /* Sanity check for client registration */
6917 if (NULL == (c = client_get (client)))
6920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6925 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6927 /* Sanity check for message size */
6928 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6931 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6935 /* Tunnel exists? */
6936 tid = ntohl (peer_msg->tunnel_id);
6937 t = tunnel_get_by_local_id (c, tid);
6941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6946 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6947 GNUNET_PEER_intern(&peer_msg->peer));
6952 * Handler for unblacklist requests of peers in a tunnel
6954 * @param cls closure
6955 * @param client identification of the client
6956 * @param message the actual message (PeerControl)
6959 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6960 const struct GNUNET_MessageHeader *message)
6962 struct GNUNET_MESH_PeerControl *peer_msg;
6963 struct MeshClient *c;
6964 struct MeshTunnel *t;
6965 MESH_TunnelNumber tid;
6969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6970 /* Sanity check for client registration */
6971 if (NULL == (c = client_get (client)))
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6979 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6981 /* Sanity check for message size */
6982 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6989 /* Tunnel exists? */
6990 tid = ntohl (peer_msg->tunnel_id);
6991 t = tunnel_get_by_local_id (c, tid);
6995 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7000 /* if peer is not known, complain */
7001 pid = GNUNET_PEER_search (&peer_msg->peer);
7008 /* search and remove from list */
7009 for (i = 0; i < t->nblacklisted; i++)
7011 if (t->blacklisted[i] == pid)
7013 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7014 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7019 /* if peer hasn't been blacklisted, complain */
7025 * Handler for connection requests to new peers by type
7027 * @param cls closure
7028 * @param client identification of the client
7029 * @param message the actual message (ConnectPeerByType)
7032 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7033 const struct GNUNET_MessageHeader *message)
7035 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7036 struct MeshClient *c;
7037 struct MeshTunnel *t;
7038 struct GNUNET_HashCode hash;
7039 MESH_TunnelNumber tid;
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7042 /* Sanity check for client registration */
7043 if (NULL == (c = client_get (client)))
7046 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7051 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7053 /* Sanity check for message size */
7054 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7055 ntohs (connect_msg->header.size))
7058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7062 /* Tunnel exists? */
7063 tid = ntohl (connect_msg->tunnel_id);
7064 t = tunnel_get_by_local_id (c, tid);
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7072 /* Does client own tunnel? */
7073 if (t->owner->handle != client)
7076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7080 /* Do WE have the service? */
7081 t->type = ntohl (connect_msg->type);
7082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7083 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7084 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7087 /* Yes! Fast forward, add ourselves to the tunnel and send the
7088 * good news to the client, and alert the destination client of
7089 * an incoming tunnel.
7091 * FIXME send a path create to self, avoid code duplication
7093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7094 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7095 peer_info_get (&my_full_id),
7096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7099 send_client_peer_connected (t, myid);
7100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7103 t->local_tid_dest = next_local_tid++;
7104 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7105 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7106 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7110 /* Ok, lets find a peer offering the service */
7111 if (NULL != t->dht_get_type)
7113 GNUNET_DHT_get_stop (t->dht_get_type);
7115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7116 GNUNET_h2s (&hash));
7118 GNUNET_DHT_get_start (dht_handle,
7119 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7121 dht_replication_level,
7122 GNUNET_DHT_RO_RECORD_ROUTE |
7123 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7125 &dht_get_type_handler, t);
7127 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7133 * Handler for connection requests to new peers by a string service description.
7135 * @param cls closure
7136 * @param client identification of the client
7137 * @param message the actual message, which includes messages the client wants
7140 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7141 const struct GNUNET_MessageHeader *message)
7143 struct GNUNET_MESH_ConnectPeerByString *msg;
7144 struct MeshRegexSearchInfo *info;
7145 struct MeshTunnel *t;
7146 struct MeshClient *c;
7147 MESH_TunnelNumber tid;
7152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7153 "Connect by string started\n");
7154 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7155 size = htons (message->size);
7157 /* Sanity check for client registration */
7158 if (NULL == (c = client_get (client)))
7161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7166 /* Message size sanity check */
7167 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7174 /* Tunnel exists? */
7175 tid = ntohl (msg->tunnel_id);
7176 t = tunnel_get_by_local_id (c, tid);
7180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7184 /* Does client own tunnel? */
7185 if (t->owner->handle != client)
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7193 " on tunnel %s [%u]\n",
7194 GNUNET_i2s(&my_full_id),
7197 /* Only one connect_by_string allowed at the same time! */
7198 /* FIXME: allow more, return handle at api level to cancel, document */
7199 if (NULL != t->regex_search)
7202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7206 /* Find string itself */
7207 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7208 string = (const char *) &msg[1];
7210 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7212 info->description = GNUNET_strndup (string, len);
7213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7215 t->regex_search = info;
7217 info->search_handle = GNUNET_REGEX_search (cfg,
7219 ®ex_found_handler, info);
7221 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7227 * Handler for client traffic directed to one peer
7229 * @param cls closure
7230 * @param client identification of the client
7231 * @param message the actual message
7234 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7235 const struct GNUNET_MessageHeader *message)
7237 struct MeshClient *c;
7238 struct MeshTunnel *t;
7239 struct MeshPeerInfo *pi;
7240 struct GNUNET_MESH_Unicast *data_msg;
7241 MESH_TunnelNumber tid;
7244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7245 "Got a unicast request from a client!\n");
7247 /* Sanity check for client registration */
7248 if (NULL == (c = client_get (client)))
7251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7256 data_msg = (struct GNUNET_MESH_Unicast *) message;
7258 /* Sanity check for message size */
7259 size = ntohs (message->size);
7260 if (sizeof (struct GNUNET_MESH_Unicast) +
7261 sizeof (struct GNUNET_MessageHeader) > size)
7264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7268 /* Tunnel exists? */
7269 tid = ntohl (data_msg->tid);
7270 t = tunnel_get_by_local_id (c, tid);
7274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7278 /* Is it a local tunnel? Then, does client own the tunnel? */
7279 if (t->owner->handle != client)
7282 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7286 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7287 &data_msg->destination.hashPubKey);
7288 /* Is the selected peer in the tunnel? */
7292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7296 /* PID should be as expected */
7297 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7301 "Unicast PID, expected %u, got %u\n",
7302 t->fwd_pid + 1, ntohl (data_msg->pid));
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7307 /* Ok, everything is correct, send the message
7308 * (pretend we got it from a mesh peer)
7311 /* Work around const limitation */
7312 char buf[ntohs (message->size)] GNUNET_ALIGN;
7313 struct GNUNET_MESH_Unicast *copy;
7315 copy = (struct GNUNET_MESH_Unicast *) buf;
7316 memcpy (buf, data_msg, size);
7317 copy->oid = my_full_id;
7318 copy->tid = htonl (t->id.tid);
7319 copy->ttl = htonl (default_ttl);
7320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7321 " calling generic handler...\n");
7322 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
7324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7332 * Handler for client traffic directed to the origin
7334 * @param cls closure
7335 * @param client identification of the client
7336 * @param message the actual message
7339 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7340 const struct GNUNET_MessageHeader *message)
7342 struct GNUNET_MESH_ToOrigin *data_msg;
7343 struct MeshTunnelClientInfo *clinfo;
7344 struct MeshClient *c;
7345 struct MeshTunnel *t;
7346 MESH_TunnelNumber tid;
7349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7350 "Got a ToOrigin request from a client!\n");
7351 /* Sanity check for client registration */
7352 if (NULL == (c = client_get (client)))
7355 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7360 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7362 /* Sanity check for message size */
7363 size = ntohs (message->size);
7364 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7365 sizeof (struct GNUNET_MessageHeader) > size)
7368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7372 /* Tunnel exists? */
7373 tid = ntohl (data_msg->tid);
7374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7375 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7381 t = tunnel_get_by_local_id (c, tid);
7384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7385 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7391 /* It should be sent by someone who has this as incoming tunnel. */
7392 if (GNUNET_NO == client_knows_tunnel (c, t))
7395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7399 /* PID should be as expected */
7400 clinfo = tunnel_get_client_fc (t, c);
7401 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7405 "To Origin PID, expected %u, got %u\n",
7406 clinfo->bck_pid + 1,
7407 ntohl (data_msg->pid));
7408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7413 /* Ok, everything is correct, send the message
7414 * (pretend we got it from a mesh peer)
7417 char buf[ntohs (message->size)] GNUNET_ALIGN;
7418 struct GNUNET_MESH_ToOrigin *copy;
7420 /* Work around const limitation */
7421 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7422 memcpy (buf, data_msg, size);
7423 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7424 copy->tid = htonl (t->id.tid);
7425 copy->ttl = htonl (default_ttl);
7426 copy->pid = htonl (t->bck_pid + 1);
7428 copy->sender = my_full_id;
7429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7430 " calling generic handler...\n");
7431 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
7433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7440 * Handler for client traffic directed to all peers in a tunnel
7442 * @param cls closure
7443 * @param client identification of the client
7444 * @param message the actual message
7447 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7448 const struct GNUNET_MessageHeader *message)
7450 struct MeshClient *c;
7451 struct MeshTunnel *t;
7452 struct GNUNET_MESH_Multicast *data_msg;
7453 MESH_TunnelNumber tid;
7455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7456 "Got a multicast request from a client!\n");
7458 /* Sanity check for client registration */
7459 if (NULL == (c = client_get (client)))
7462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7467 data_msg = (struct GNUNET_MESH_Multicast *) message;
7469 /* Sanity check for message size */
7470 if (sizeof (struct GNUNET_MESH_Multicast) +
7471 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7478 /* Tunnel exists? */
7479 tid = ntohl (data_msg->tid);
7480 t = tunnel_get_by_local_id (c, tid);
7484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7491 /* Does client own tunnel? */
7492 if (t->owner->handle != client)
7495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7499 /* PID should be as expected */
7500 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7504 "Multicast PID, expected %u, got %u\n",
7505 t->fwd_pid + 1, ntohl (data_msg->pid));
7506 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7511 char buf[ntohs (message->size)] GNUNET_ALIGN;
7512 struct GNUNET_MESH_Multicast *copy;
7514 copy = (struct GNUNET_MESH_Multicast *) buf;
7515 memcpy (buf, message, ntohs (message->size));
7516 copy->oid = my_full_id;
7517 copy->tid = htonl (t->id.tid);
7518 copy->ttl = htonl (default_ttl);
7519 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7521 " calling generic handler...\n");
7522 handle_mesh_data_multicast (client, &my_full_id, ©->header);
7525 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7531 * Handler for client's ACKs for payload traffic.
7533 * @param cls Closure (unused).
7534 * @param client Identification of the client.
7535 * @param message The actual message.
7538 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7539 const struct GNUNET_MessageHeader *message)
7541 struct GNUNET_MESH_LocalAck *msg;
7542 struct MeshTunnel *t;
7543 struct MeshClient *c;
7544 MESH_TunnelNumber tid;
7547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7548 /* Sanity check for client registration */
7549 if (NULL == (c = client_get (client)))
7552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7557 msg = (struct GNUNET_MESH_LocalAck *) message;
7559 /* Tunnel exists? */
7560 tid = ntohl (msg->tunnel_id);
7561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7562 t = tunnel_get_by_local_id (c, tid);
7566 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7572 ack = ntohl (msg->max_pid);
7573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7575 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7576 if (NULL != t->owner && t->owner->handle == client)
7578 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7580 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7584 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7585 tunnel_set_client_fwd_ack (t, c, ack);
7586 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7589 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7596 * Iterator over all peers to send a monitoring client info about a tunnel.
7598 * @param cls Closure (message being built).
7599 * @param key Key (hashed tunnel ID, unused).
7600 * @param value Peer info.
7602 * @return GNUNET_YES, to keep iterating.
7605 monitor_peers_iterator (void *cls,
7606 const struct GNUNET_HashCode * key,
7609 struct GNUNET_MESH_LocalMonitor *msg = cls;
7610 struct GNUNET_PeerIdentity *id;
7611 struct MeshPeerInfo *info = value;
7613 id = (struct GNUNET_PeerIdentity *) &msg[1];
7614 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7617 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7618 "* sending info about peer %s [%u]\n",
7619 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7627 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7629 * @param cls Closure (client handle).
7630 * @param key Key (hashed tunnel ID, unused).
7631 * @param value Tunnel info.
7633 * @return GNUNET_YES, to keep iterating.
7636 monitor_all_tunnels_iterator (void *cls,
7637 const struct GNUNET_HashCode * key,
7640 struct GNUNET_SERVER_Client *client = cls;
7641 struct MeshTunnel *t = value;
7642 struct GNUNET_MESH_LocalMonitor *msg;
7645 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7646 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7647 npeers * sizeof (struct GNUNET_PeerIdentity));
7648 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7649 msg->tunnel_id = htonl (t->id.tid);
7650 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7651 npeers * sizeof (struct GNUNET_PeerIdentity));
7652 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7654 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7655 monitor_peers_iterator,
7658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7659 "* sending info about tunnel %s [%u] (%u peers)\n",
7660 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7662 if (msg->npeers != npeers)
7665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7666 "Get tunnels fail: size %u - iter %u\n",
7667 npeers, msg->npeers);
7670 msg->npeers = htonl (npeers);
7671 GNUNET_SERVER_notification_context_unicast (nc, client,
7672 &msg->header, GNUNET_NO);
7678 * Handler for client's MONITOR request.
7680 * @param cls Closure (unused).
7681 * @param client Identification of the client.
7682 * @param message The actual message.
7685 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7686 const struct GNUNET_MessageHeader *message)
7688 struct MeshClient *c;
7690 /* Sanity check for client registration */
7691 if (NULL == (c = client_get (client)))
7694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7698 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7699 "Received get tunnels request from client %u\n",
7701 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7702 monitor_all_tunnels_iterator,
7704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7705 "Get tunnels request from client %u completed\n",
7707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7712 * Data needed to build a Monitor_Tunnel message.
7714 struct MeshMonitorTunnelContext
7717 * Partial message, including peer count.
7719 struct GNUNET_MESH_LocalMonitor *msg;
7722 * Hashmap with positions: peer->position.
7724 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7727 * Index of the parent of each peer in the message, realtive to the absolute
7728 * order in the array (can be in a previous message).
7730 uint32_t parents[1024];
7733 * Peers visited so far in the tree, aka position of the current peer.
7735 unsigned int npeers;
7738 * Client requesting the info.
7740 struct MeshClient *c;
7745 * Send a client a message about the structure of a tunnel.
7747 * @param ctx Context of the tunnel iteration, with info regarding the state
7748 * of the execution and the number of peers visited for this message.
7751 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7753 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7754 struct GNUNET_PeerIdentity *pid;
7755 unsigned int *parent;
7758 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7759 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7760 resp->header.size = htons (size);
7761 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7762 parent = (unsigned int *) &pid[resp->npeers];
7763 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7764 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7765 &resp->header, GNUNET_NO);
7769 * Iterator over a tunnel tree to build a message containing all peers
7770 * the in the tunnel, including relay nodes.
7772 * @param cls Closure (pointer to pointer of message being built).
7773 * @param peer Short ID of a peer.
7774 * @param parent Short ID of the @c peer 's parent.
7777 tunnel_tree_iterator (void *cls,
7778 GNUNET_PEER_Id peer,
7779 GNUNET_PEER_Id parent)
7781 struct MeshMonitorTunnelContext *ctx = cls;
7782 struct GNUNET_MESH_LocalMonitor *msg;
7783 struct GNUNET_PeerIdentity *pid;
7784 struct GNUNET_PeerIdentity ppid;
7787 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7788 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7789 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7790 &pid[msg->npeers].hashPubKey,
7791 (void *) (long) ctx->npeers,
7792 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7793 GNUNET_PEER_resolve (parent, &ppid);
7794 ctx->parents[msg->npeers] =
7795 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7801 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7803 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7806 send_client_tunnel_info (ctx);
7813 * Handler for client's MONITOR_TUNNEL request.
7815 * @param cls Closure (unused).
7816 * @param client Identification of the client.
7817 * @param message The actual message.
7820 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7821 const struct GNUNET_MessageHeader *message)
7823 const struct GNUNET_MESH_LocalMonitor *msg;
7824 struct GNUNET_MESH_LocalMonitor *resp;
7825 struct MeshMonitorTunnelContext ctx;
7826 struct MeshClient *c;
7827 struct MeshTunnel *t;
7829 /* Sanity check for client registration */
7830 if (NULL == (c = client_get (client)))
7833 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7837 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7839 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7842 ntohl (msg->tunnel_id));
7843 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7846 /* We don't know the tunnel */
7847 struct GNUNET_MESH_LocalMonitor warn;
7850 warn.npeers = htonl (UINT_MAX);
7851 GNUNET_SERVER_notification_context_unicast (nc, client,
7854 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7858 /* Initialize context */
7859 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7863 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7866 /* Collect and send information */
7867 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7868 send_client_tunnel_info (&ctx);
7871 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7874 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7875 "Monitor tunnel request from client %u completed\n",
7877 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7882 * Functions to handle messages from clients
7884 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7885 {&handle_local_new_client, NULL,
7886 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7887 {&handle_local_announce_regex, NULL,
7888 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7889 {&handle_local_tunnel_create, NULL,
7890 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7891 sizeof (struct GNUNET_MESH_TunnelMessage)},
7892 {&handle_local_tunnel_destroy, NULL,
7893 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7894 sizeof (struct GNUNET_MESH_TunnelMessage)},
7895 {&handle_local_tunnel_speed, NULL,
7896 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7897 sizeof (struct GNUNET_MESH_TunnelMessage)},
7898 {&handle_local_tunnel_speed, NULL,
7899 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7900 sizeof (struct GNUNET_MESH_TunnelMessage)},
7901 {&handle_local_tunnel_buffer, NULL,
7902 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7903 sizeof (struct GNUNET_MESH_TunnelMessage)},
7904 {&handle_local_tunnel_buffer, NULL,
7905 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7906 sizeof (struct GNUNET_MESH_TunnelMessage)},
7907 {&handle_local_connect_add, NULL,
7908 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7909 sizeof (struct GNUNET_MESH_PeerControl)},
7910 {&handle_local_connect_del, NULL,
7911 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7912 sizeof (struct GNUNET_MESH_PeerControl)},
7913 {&handle_local_blacklist, NULL,
7914 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7915 sizeof (struct GNUNET_MESH_PeerControl)},
7916 {&handle_local_unblacklist, NULL,
7917 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7918 sizeof (struct GNUNET_MESH_PeerControl)},
7919 {&handle_local_connect_by_type, NULL,
7920 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7921 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7922 {&handle_local_connect_by_string, NULL,
7923 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7924 {&handle_local_unicast, NULL,
7925 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7926 {&handle_local_to_origin, NULL,
7927 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7928 {&handle_local_multicast, NULL,
7929 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7930 {&handle_local_ack, NULL,
7931 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7932 sizeof (struct GNUNET_MESH_LocalAck)},
7933 {&handle_local_get_tunnels, NULL,
7934 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7935 sizeof (struct GNUNET_MessageHeader)},
7936 {&handle_local_show_tunnel, NULL,
7937 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7938 sizeof (struct GNUNET_MESH_LocalMonitor)},
7944 * To be called on core init/fail.
7946 * @param cls service closure
7947 * @param server handle to the server for this service
7948 * @param identity the public identity of this peer
7951 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7952 const struct GNUNET_PeerIdentity *identity)
7955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7956 core_handle = server;
7957 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7960 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7961 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7963 GNUNET_i2s (identity));
7964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7966 GNUNET_i2s (&my_full_id));
7967 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7969 GNUNET_abort(); // Try harder
7976 * Method called whenever a given peer connects.
7978 * @param cls closure
7979 * @param peer peer identity this notification is about
7982 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
7984 struct MeshPeerInfo *peer_info;
7985 struct MeshPeerPath *path;
7987 DEBUG_CONN ("Peer connected\n");
7988 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7989 peer_info = peer_info_get (peer);
7990 if (myid == peer_info->id)
7992 DEBUG_CONN (" (self)\n");
7997 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7999 path = path_new (2);
8000 path->peers[0] = myid;
8001 path->peers[1] = peer_info->id;
8002 GNUNET_PEER_change_rc (myid, 1);
8003 GNUNET_PEER_change_rc (peer_info->id, 1);
8004 peer_info_add_path (peer_info, path, GNUNET_YES);
8005 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8011 * Method called whenever a peer disconnects.
8013 * @param cls closure
8014 * @param peer peer identity this notification is about
8017 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8019 struct MeshPeerInfo *pi;
8020 struct MeshPeerQueue *q;
8021 struct MeshPeerQueue *n;
8023 DEBUG_CONN ("Peer disconnected\n");
8024 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8034 /* TODO try to reroute this traffic instead */
8035 queue_destroy(q, GNUNET_YES);
8038 if (NULL != pi->core_transmit)
8040 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8041 pi->core_transmit = NULL;
8043 peer_info_remove_path (pi, pi->id, myid);
8046 DEBUG_CONN (" (self)\n");
8048 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8053 /******************************************************************************/
8054 /************************ MAIN FUNCTIONS ****************************/
8055 /******************************************************************************/
8058 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8060 * @param cls closure
8061 * @param key current key code
8062 * @param value value in the hash map
8063 * @return GNUNET_YES if we should continue to iterate,
8067 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8069 struct MeshTunnel *t = value;
8076 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8078 * @param cls closure
8079 * @param key current key code
8080 * @param value value in the hash map
8081 * @return GNUNET_YES if we should continue to iterate,
8085 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8087 struct MeshPeerInfo *p = value;
8088 struct MeshPeerQueue *q;
8089 struct MeshPeerQueue *n;
8097 queue_destroy(q, GNUNET_YES);
8101 peer_info_destroy (p);
8107 * Task run during shutdown.
8113 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8117 if (core_handle != NULL)
8119 GNUNET_CORE_disconnect (core_handle);
8124 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
8127 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8128 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8129 if (dht_handle != NULL)
8131 GNUNET_DHT_disconnect (dht_handle);
8136 GNUNET_SERVER_notification_context_destroy (nc);
8139 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8141 GNUNET_SCHEDULER_cancel (announce_id_task);
8142 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8144 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8146 GNUNET_SCHEDULER_cancel (announce_applications_task);
8147 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8154 * Callback for hostkey read/generation.
8156 * @param cls Closure (Configuration handle).
8157 * @param pk The ECC private key.
8158 * @param emsg Error message, if any.
8161 key_generation_cb (void *cls,
8162 struct GNUNET_CRYPTO_EccPrivateKey *pk,
8165 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8166 struct MeshPeerInfo *peer;
8167 struct MeshPeerPath *p;
8172 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8173 _("Could not access hostkey: %s. Exiting.\n"),
8175 GNUNET_SCHEDULER_shutdown ();
8178 my_private_key = pk;
8179 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
8180 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8181 &my_full_id.hashPubKey);
8182 myid = GNUNET_PEER_intern (&my_full_id);
8183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8184 "Mesh for peer [%s] starting\n",
8185 GNUNET_i2s(&my_full_id));
8187 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8188 NULL, /* Closure passed to MESH functions */
8189 &core_init, /* Call core_init once connected */
8190 &core_connect, /* Handle connects */
8191 &core_disconnect, /* remove peers on disconnects */
8192 NULL, /* Don't notify about all incoming messages */
8193 GNUNET_NO, /* For header only in notification */
8194 NULL, /* Don't notify about all outbound messages */
8195 GNUNET_NO, /* For header-only out notification */
8196 core_handlers); /* Register these handlers */
8198 if (core_handle == NULL)
8201 GNUNET_SCHEDULER_shutdown ();
8206 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8209 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8210 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8211 GNUNET_SERVER_disconnect_notify (server_handle,
8212 &handle_local_client_disconnect, NULL);
8216 clients_tail = NULL;
8219 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8220 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8222 /* Create a peer_info for the local peer */
8223 peer = peer_info_get (&my_full_id);
8226 GNUNET_PEER_change_rc (myid, 1);
8227 peer_info_add_path (peer, p, GNUNET_YES);
8228 GNUNET_SERVER_resume (server_handle);
8229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8234 * Process mesh requests.
8236 * @param cls closure
8237 * @param server the initialized server
8238 * @param c configuration to use
8241 run (void *cls, struct GNUNET_SERVER_Handle *server,
8242 const struct GNUNET_CONFIGURATION_Handle *c)
8247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8248 server_handle = server;
8251 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
8254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8256 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8257 "mesh", "peer/privatekey");
8258 GNUNET_SCHEDULER_shutdown ();
8263 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8264 &refresh_path_time))
8266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8268 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8269 "mesh", "refresh path time");
8270 GNUNET_SCHEDULER_shutdown ();
8275 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8276 &app_announce_time))
8278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8280 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8281 "mesh", "app announce time");
8282 GNUNET_SCHEDULER_shutdown ();
8285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8286 "APP_ANNOUNCE_TIME %llu ms\n",
8287 app_announce_time.rel_value);
8289 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8294 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8295 "mesh", "id announce time");
8296 GNUNET_SCHEDULER_shutdown ();
8301 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8306 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8307 "mesh", "connect timeout");
8308 GNUNET_SCHEDULER_shutdown ();
8313 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8318 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8319 "mesh", "max msgs queue");
8320 GNUNET_SCHEDULER_shutdown ();
8325 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8330 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8331 "mesh", "max tunnels");
8332 GNUNET_SCHEDULER_shutdown ();
8337 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8342 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8343 "mesh", "default ttl", 64);
8348 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8352 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8353 "mesh", "max peers", 1000);
8358 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8359 &dht_replication_level))
8361 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8363 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8364 "mesh", "dht replication level", 3);
8365 dht_replication_level = 3;
8368 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8369 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8370 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8371 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8372 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8374 dht_handle = GNUNET_DHT_connect (c, 64);
8375 if (NULL == dht_handle)
8379 stats = GNUNET_STATISTICS_create ("mesh", c);
8381 GNUNET_SERVER_suspend (server_handle);
8382 /* Scheduled the task to clean up when shutdown is called */
8383 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8385 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
8388 GNUNET_free (keyfile);
8393 * The main function for the mesh service.
8395 * @param argc number of arguments from the command line
8396 * @param argv command line arguments
8397 * @return 0 ok, 1 on error
8400 main (int argc, char *const *argv)
8405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8406 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8408 ret = (GNUNET_OK == r) ? 0 : 1;
8409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8413 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8414 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8415 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);