2 This file is part of GNUnet.
3 (C) 2001-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_regex_lib.h"
57 #define MESH_BLOOM_SIZE 128
59 #define MESH_DEBUG_REGEX GNUNET_YES
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
81 #define DEBUG_REGEX(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
83 #define DEBUG_REGEX(...)
90 struct timespec __mesh_start;
91 struct timespec __mesh_end;
92 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
93 #define INTERVAL_END \
95 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
96 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
97 if (__diff < 0) __diff += 1000000000;\
101 #define INTERVAL_SHOW \
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
105 #define INTERVAL_START
107 #define INTERVAL_SHOW
110 /******************************************************************************/
111 /************************ DATA STRUCTURES ****************************/
112 /******************************************************************************/
114 /** FWD declaration */
120 * Struct representing a piece of data being sent to other peers
124 /** Tunnel it belongs to. */
125 struct MeshTunnel *t;
127 /** How many remaining neighbors still hav't got it. */
128 unsigned int reference_counter;
130 /** How many remaining neighbors we need to send this to. */
131 unsigned int total_out;
133 /** Size of the data. */
142 * Struct containing info about a queued transmission to this peer
149 struct MeshPeerQueue *next;
154 struct MeshPeerQueue *prev;
157 * Peer this transmission is directed to.
159 struct MeshPeerInfo *peer;
162 * Tunnel this message belongs to.
164 struct MeshTunnel *tunnel;
167 * Pointer to info stucture used as cls.
177 * Size of the message
184 * Struct to store regex information announced by clients.
186 struct MeshRegexDescriptor
189 * Regular expression itself.
194 * How many characters per edge can we squeeze?
196 uint16_t compression;
199 * Handle to announce the regex.
201 struct GNUNET_REGEX_announce_handle *h;
206 * Struct to keep information of searches of services described by a regex
207 * using a user-provided string service description.
209 struct MeshRegexSearchInfo
212 * Which tunnel is this for
214 struct MeshTunnel *t;
217 * User provided description of the searched service.
222 * Regex search handle.
224 struct GNUNET_REGEX_search_handle *search_handle;
227 * Peer that is connecting via connect_by_string. When connected, free ctx.
232 * Other peers that are found but not yet being connected to.
234 GNUNET_PEER_Id *peers;
237 * Number of elements in peers.
239 unsigned int n_peers;
242 * Next peer to try to connect to.
247 * Timeout for a connect attempt.
248 * When reached, try to connect to a different peer, if any. If not,
249 * try the same peer again.
251 GNUNET_SCHEDULER_TaskIdentifier timeout;
257 * Struct containing all info possibly needed to build a package when called
260 struct MeshTransmissionDescriptor
262 /** ID of the tunnel this packet travels in */
263 struct MESH_TunnelID *origin;
265 /** Who was this message being sent to */
266 struct MeshPeerInfo *peer;
268 /** Ultimate destination of the packet */
269 GNUNET_PEER_Id destination;
271 /** Data descriptor */
272 struct MeshData* mesh_data;
277 * Struct containing all information regarding a given peer
287 * Last time we heard from this peer
289 struct GNUNET_TIME_Absolute last_contact;
292 * Task handler for delayed connect task;
294 GNUNET_SCHEDULER_TaskIdentifier connect_task;
297 * Number of attempts to reconnect so far
299 int n_reconnect_attempts;
302 * Paths to reach the peer, ordered by ascending hop count
304 struct MeshPeerPath *path_head;
307 * Paths to reach the peer, ordered by ascending hop count
309 struct MeshPeerPath *path_tail;
312 * Handle to stop the DHT search for a path to this peer
314 struct GNUNET_DHT_GetHandle *dhtget;
317 * Closure given to the DHT GET
319 struct MeshPathInfo *dhtgetcls;
322 * Array of tunnels this peer participates in
323 * (most probably a small amount, therefore not a hashmap)
324 * When the path to the peer changes, notify these tunnels to let them
325 * re-adjust their path trees.
327 struct MeshTunnel **tunnels;
330 * Number of tunnels this peers participates in
332 unsigned int ntunnels;
335 * Transmission queue to core DLL head
337 struct MeshPeerQueue *queue_head;
340 * Transmission queue to core DLL tail
342 struct MeshPeerQueue *queue_tail;
345 * How many messages are in the queue to this peer.
347 unsigned int queue_n;
350 * Handle to for queued transmissions
352 struct GNUNET_CORE_TransmitHandle *core_transmit;
357 * Globally unique tunnel identification (owner + number)
358 * DO NOT USE OVER THE NETWORK
363 * Node that owns the tunnel
368 * Tunnel number to differentiate all the tunnels owned by the node oid
369 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
371 MESH_TunnelNumber tid;
376 * Struct containing all information regarding a tunnel
377 * For an intermediate node the improtant info used will be:
378 * - id Tunnel unique identification
379 * - paths[0] To know where to send it next
380 * - metainfo: ready, speeds, accounting
387 struct MESH_TunnelID id;
390 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
392 MESH_TunnelNumber local_tid;
395 * Local tunnel number for local destination clients (incoming number)
396 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
399 MESH_TunnelNumber local_tid_dest;
402 * Is the speed on the tunnel limited to the slowest peer?
407 * Is the tunnel bufferless (minimum latency)?
412 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
417 * Packet ID of the last bck packet sent (unique counter per hop).
422 * SKIP value for this tunnel.
427 * Force sending ACK? Flag to allow duplicate ACK on POLL.
432 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
433 * Contains the Flow Control info: FWD ACK value received,
434 * last BCK ACK sent, PID and SKIP values.
436 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
439 * Last ACK sent towards the origin (for traffic towards leaf node).
441 uint32_t last_fwd_ack;
444 * BCK ACK value received from the hop towards the owner of the tunnel,
445 * (previous node / owner): up to what message PID can we sent back to him.
450 * How many messages are in the forward queue (towards leaves).
452 unsigned int fwd_queue_n;
455 * How many messages do we accept in the forward queue.
457 unsigned int fwd_queue_max;
460 * How many messages are in the backward queue (towards origin).
462 unsigned int bck_queue_n;
465 * How many messages do we accept in the backward queue.
467 unsigned int bck_queue_max;
470 * Task to poll peer in case of a stall.
472 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
475 * Last time the tunnel was used
477 struct GNUNET_TIME_Absolute timestamp;
480 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
481 * containing peers added by id or by type, not intermediate peers.
483 struct GNUNET_CONTAINER_MultiHashMap *peers;
486 * Number of peers that are connected and potentially ready to receive data
488 unsigned int peers_ready;
491 * Client owner of the tunnel, if any
493 struct MeshClient *owner;
496 * Clients that have been informed about and want to stay in the tunnel.
498 struct MeshClient **clients;
501 * Flow control info for each client.
503 struct MeshTunnelClientInfo *clients_fc;
506 * Number of elements in clients/clients_fc
508 unsigned int nclients;
511 * Clients that have been informed but requested to leave the tunnel.
513 struct MeshClient **ignore;
516 * Number of elements in clients
518 unsigned int nignore;
523 GNUNET_PEER_Id *blacklisted;
526 * Number of elements in blacklisted
528 unsigned int nblacklisted;
531 * Bloomfilter (for peer identities) to stop circular routes
533 char bloomfilter[MESH_BLOOM_SIZE];
538 struct MeshTunnelTree *tree;
541 * Application type we are looking for in this tunnel
543 GNUNET_MESH_ApplicationType type;
546 * Used to search peers offering a service
548 struct GNUNET_DHT_GetHandle *dht_get_type;
551 * Handle for the regex search for a connect_by_string
553 struct MeshRegexSearchInfo *regex_search;
556 * Task to keep the used paths alive
558 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
561 * Task to destroy the tunnel after timeout
563 * FIXME: merge the two? a tunnel will have either
564 * a path refresh OR a timeout, never both!
566 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
569 * Flag to signal the destruction of the tunnel.
570 * If this is set GNUNET_YES the tunnel will be destroyed
571 * when the queue is empty.
576 * Total messages pending for this tunnels, payload or not.
578 unsigned int pending_messages;
581 * If the tunnel is empty, destoy it.
583 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
588 * Info about a child node in a tunnel, needed to perform flow control.
590 struct MeshTunnelChildInfo
593 * ID of the child node.
613 * Maximum PID allowed (FWD ACK received).
618 * Last ACK sent to that child (BCK ACK).
623 * Circular buffer pointing to MeshPeerQueue elements for all
624 * payload traffic going to this child.
625 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
627 struct MeshPeerQueue **send_buffer;
630 * Index of the oldest element in the send_buffer.
632 unsigned int send_buffer_start;
635 * How many elements are already in the buffer.
637 unsigned int send_buffer_n;
640 * Tunnel this info is about
642 struct MeshTunnel *t;
645 * Task to poll peer in case of a stall.
647 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
650 * Time to use for next polling call.
652 struct GNUNET_TIME_Relative fc_poll_time;
657 * Info about a leaf client of a tunnel, needed to perform flow control.
659 struct MeshTunnelClientInfo
662 * PID of the last packet sent to the client (FWD).
667 * PID of the last packet received from the client (BCK).
672 * Maximum PID allowed (FWD ACK received).
677 * Last ACK sent to that child (BCK ACK).
685 * Info collected during iteration of child nodes in order to get the ACK value
688 struct MeshTunnelChildIteratorContext
691 * Tunnel whose info is being collected.
693 struct MeshTunnel *t;
696 * Is this context initialized? Is the value in max_child_ack valid?
701 * Maximum child ACK so far.
703 uint32_t max_child_ack;
706 * Number of children nodes
708 unsigned int nchildren;
713 * Info needed to work with tunnel paths and peers
720 struct MeshTunnel *t;
723 * Neighbouring peer to whom we send the packet to
725 struct MeshPeerInfo *peer;
730 struct MeshPeerPath *path;
735 * Struct containing information about a client of the service
742 struct MeshClient *next;
747 struct MeshClient *prev;
750 * Tunnels that belong to this client, indexed by local id
752 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
755 * Tunnels this client has accepted, indexed by incoming local id
757 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
760 * Tunnels this client has rejected, indexed by incoming local id
762 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
764 * Handle to communicate with the client
766 struct GNUNET_SERVER_Client *handle;
769 * Applications that this client has claimed to provide: H(app) = app.
771 struct GNUNET_CONTAINER_MultiHashMap *apps;
774 * Messages that this client has declared interest in
776 struct GNUNET_CONTAINER_MultiHashMap *types;
779 * Whether the client is active or shutting down (don't send confirmations
780 * to a client that is shutting down.
785 * ID of the client, mainly for debug messages
790 * Regular expressions describing the services offered by this client.
792 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
795 * Number of regular expressions in regexes.
797 unsigned int n_regex;
800 * Task to refresh all regular expresions in the DHT.
802 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
805 * Tmp store for partially retrieved regex.
812 /******************************************************************************/
813 /************************ DEBUG FUNCTIONS ****************************/
814 /******************************************************************************/
818 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
819 * @param cls string to print
820 * @param success GNUNET_OK if the PUT was transmitted,
821 * GNUNET_NO on timeout,
822 * GNUNET_SYSERR on disconnect from service
823 * after the PUT message was transmitted
824 * (so we don't know if it was received or not)
829 mesh_debug (void *cls, int success)
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
837 unsigned int debug_fwd_ack;
838 unsigned int debug_bck_ack;
842 /******************************************************************************/
843 /*********************** GLOBAL VARIABLES ****************************/
844 /******************************************************************************/
846 /************************** Configuration parameters **************************/
849 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
851 static struct GNUNET_TIME_Relative refresh_path_time;
854 * How often to PUT local application numbers in the DHT.
856 static struct GNUNET_TIME_Relative app_announce_time;
859 * How often to PUT own ID in the DHT.
861 static struct GNUNET_TIME_Relative id_announce_time;
864 * Maximum time allowed to connect to a peer found by string.
866 static struct GNUNET_TIME_Relative connect_timeout;
869 * Default TTL for payload packets.
871 static unsigned long long default_ttl;
874 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
876 static unsigned long long dht_replication_level;
879 * How many tunnels are we willing to maintain.
880 * Local tunnels are always allowed, even if there are more tunnels than max.
882 static unsigned long long max_tunnels;
885 * How many messages *in total* are we willing to queue, divided by number of
886 * tunnels to get tunnel queue size.
888 static unsigned long long max_msgs_queue;
891 * How many peers do we want to remember?
893 static unsigned long long max_peers;
896 /*************************** Static global variables **************************/
899 * Hostkey generation context
901 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
904 * DLL with all the clients, head.
906 static struct MeshClient *clients;
909 * DLL with all the clients, tail.
911 static struct MeshClient *clients_tail;
914 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
916 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
919 * Number of tunnels known.
921 static unsigned long long n_tunnels;
924 * Tunnels incoming, indexed by MESH_TunnelNumber
925 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
927 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
930 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
932 static struct GNUNET_CONTAINER_MultiHashMap *peers;
935 * Handle to communicate with transport
937 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
940 * Handle to communicate with core.
942 static struct GNUNET_CORE_Handle *core_handle;
947 static struct GNUNET_DHT_Handle *dht_handle;
952 static struct GNUNET_SERVER_Handle *server_handle;
955 * Handle to the statistics service.
957 static struct GNUNET_STATISTICS_Handle *stats;
960 * Notification context, to send messages to local clients.
962 static struct GNUNET_SERVER_NotificationContext *nc;
965 * Local peer own ID (memory efficient handle).
967 static GNUNET_PEER_Id myid;
970 * Local peer own ID (full value).
972 static struct GNUNET_PeerIdentity my_full_id;
977 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
982 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
985 * Tunnel ID for the next created tunnel (global tunnel number).
987 static MESH_TunnelNumber next_tid;
990 * Tunnel ID for the next incoming tunnel (local tunnel number).
992 static MESH_TunnelNumber next_local_tid;
995 * All application types provided by this peer: H(app) = *Client.
997 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1000 * All message types clients of this peer are interested in.
1002 static struct GNUNET_CONTAINER_MultiHashMap *types;
1005 * Task to periodically announce provided applications.
1007 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1010 * Task to periodically announce itself in the network.
1012 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1015 * Next ID to assign to a client.
1017 unsigned int next_client_id;
1020 /******************************************************************************/
1021 /*********************** DECLARATIONS **************************/
1022 /******************************************************************************/
1025 * Function to process paths received for a new peer addition. The recorded
1026 * paths form the initial tunnel, which can be optimized later.
1027 * Called on each result obtained for the DHT search.
1029 * @param cls closure
1030 * @param exp when will this value expire
1031 * @param key key of the result
1032 * @param type type of the result
1033 * @param size number of bytes in data
1034 * @param data pointer to the result data
1037 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1038 const struct GNUNET_HashCode * key,
1039 const struct GNUNET_PeerIdentity *get_path,
1040 unsigned int get_path_length,
1041 const struct GNUNET_PeerIdentity *put_path,
1042 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1043 size_t size, const void *data);
1047 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1048 * and insert it in the appropiate structures if the peer is not known yet.
1050 * @param peer Full identity of the peer.
1052 * @return Existing or newly created peer info.
1054 static struct MeshPeerInfo *
1055 peer_info_get (const struct GNUNET_PeerIdentity *peer);
1059 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1060 * and insert it in the appropiate structures if the peer is not known yet.
1062 * @param peer Short identity of the peer.
1064 * @return Existing or newly created peer info.
1066 static struct MeshPeerInfo *
1067 peer_info_get_short (const GNUNET_PEER_Id peer);
1071 * Try to establish a new connection to this peer.
1072 * Use the best path for the given tunnel.
1073 * If the peer doesn't have any path to it yet, try to get one.
1074 * If the peer already has some path, send a CREATE PATH towards it.
1076 * @param peer PeerInfo of the peer.
1077 * @param t Tunnel for which to create the path, if possible.
1080 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1084 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1085 * to obtain a local peer -> destination path and interning the peer ids.
1087 * @return Newly allocated and created path
1089 static struct MeshPeerPath *
1090 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1091 unsigned int get_path_length,
1092 const struct GNUNET_PeerIdentity *put_path,
1093 unsigned int put_path_length);
1097 * Adds a path to the peer_infos of all the peers in the path
1099 * @param p Path to process.
1100 * @param confirmed Whether we know if the path works or not.
1103 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1107 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1108 * needed rescources.
1109 * If peer already exists, reevaluate shortest path and change if different.
1111 * @param t Tunnel we want to add a new peer to
1112 * @param peer PeerInfo of the peer being added
1116 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1120 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1121 * that are no longer needed, as well as nodes of no longer reachable peers.
1122 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1124 * @param t Tunnel from which to remove the path.
1125 * @param peer Short id of the peer which should be removed.
1128 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1132 * Search for a tunnel by global ID using full PeerIdentities.
1134 * @param oid owner of the tunnel.
1135 * @param tid global tunnel number.
1137 * @return tunnel handler, NULL if doesn't exist.
1139 static struct MeshTunnel *
1140 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1144 * Delete an active client from the tunnel.
1150 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1153 * Notify a tunnel that a connection has broken that affects at least
1154 * some of its peers.
1156 * @param t Tunnel affected.
1157 * @param p1 Peer that got disconnected from p2.
1158 * @param p2 Peer that got disconnected from p1.
1160 * @return Short ID of the peer disconnected (either p1 or p2).
1161 * 0 if the tunnel remained unaffected.
1163 static GNUNET_PEER_Id
1164 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1169 * Get the current ack value for a tunnel, for data going from root to leaves,
1170 * taking in account the tunnel mode and the status of all children and clients.
1174 * @return Maximum PID allowed.
1177 tunnel_get_fwd_ack (struct MeshTunnel *t);
1181 * Add a client to a tunnel, initializing all needed data structures.
1183 * @param t Tunnel to which add the client.
1184 * @param c Client which to add to the tunnel.
1187 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1191 * @brief Queue and pass message to core when possible.
1193 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1194 * and accounts for it. In case the queue is full, the message is dropped and
1197 * Otherwise, message is treated as internal and allowed to go regardless of
1200 * @param cls Closure (@c type dependant). It will be used by queue_send to
1201 * build the message to be sent if not already prebuilt.
1202 * @param type Type of the message, 0 for a raw message.
1203 * @param size Size of the message.
1204 * @param dst Neighbor to send message to.
1205 * @param t Tunnel this message belongs to.
1208 queue_add (void *cls, uint16_t type, size_t size,
1209 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1213 * Free a transmission that was already queued with all resources
1214 * associated to the request.
1216 * @param queue Queue handler to cancel.
1217 * @param clear_cls Is it necessary to free associated cls?
1220 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1224 * @brief Get the next transmittable message from the queue.
1226 * This will be the head, except in the case of being a data packet
1227 * not allowed by the destination peer.
1229 * @param peer Destination peer.
1231 * @return The next viable MeshPeerQueue element to send to that peer.
1232 * NULL when there are no transmittable messages.
1234 struct MeshPeerQueue *
1235 queue_get_next (const struct MeshPeerInfo *peer);
1239 * Core callback to write a queued packet to core buffer
1241 * @param cls Closure (peer info).
1242 * @param size Number of bytes available in buf.
1243 * @param buf Where the to write the message.
1245 * @return number of bytes written to buf
1248 queue_send (void *cls, size_t size, void *buf);
1251 /******************************************************************************/
1252 /************************ PERIODIC FUNCTIONS ****************************/
1253 /******************************************************************************/
1256 * Announce iterator over for each application provided by the peer
1258 * @param cls closure
1259 * @param key current key code
1260 * @param value value in the hash map
1261 * @return GNUNET_YES if we should continue to
1266 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1268 struct PBlock block;
1269 struct MeshClient *c;
1271 block.id = my_full_id;
1272 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1273 GNUNET_assert(NULL != c);
1274 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1275 if (0 == block.type)
1280 block.type = htonl (block.type);
1281 DEBUG_DHT ("Putting APP key: %s\n", GNUNET_h2s (key));
1282 GNUNET_break (NULL !=
1283 GNUNET_DHT_put (dht_handle, key,
1284 dht_replication_level,
1285 GNUNET_DHT_RO_RECORD_ROUTE |
1286 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1287 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1289 (const char *) &block,
1290 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1291 app_announce_time, NULL, NULL));
1297 * Periodically announce what applications are provided by local clients
1300 * @param cls closure
1301 * @param tc task context
1304 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1306 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1308 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1312 DEBUG_DHT ("Starting PUT for apps\n");
1314 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1316 announce_applications_task =
1317 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1319 DEBUG_DHT ("Finished PUT for apps\n");
1324 * Periodically announce self id in the DHT
1326 * @param cls closure
1327 * @param tc task context
1330 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1332 struct PBlock block;
1334 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1336 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1340 * - Set data expiration in function of X
1341 * - Adapt X to churn
1343 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1345 block.id = my_full_id;
1346 block.type = htonl (0);
1347 GNUNET_DHT_put (dht_handle, /* DHT handle */
1348 &my_full_id.hashPubKey, /* Key to use */
1349 dht_replication_level, /* Replication level */
1350 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1351 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1352 sizeof (block), /* Size of the data */
1353 (const char *) &block, /* Data itself */
1354 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1355 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1356 NULL, /* Continuation */
1357 NULL); /* Continuation closure */
1359 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1363 /******************************************************************************/
1364 /****************** GENERAL HELPER FUNCTIONS ************************/
1365 /******************************************************************************/
1368 * Decrements the reference counter and frees all resources if needed
1370 * @param mesh_data Data Descriptor used in a multicast message.
1371 * Freed no longer needed (last message).
1374 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1376 if (0 == --(mesh_data->reference_counter))
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1379 GNUNET_free (mesh_data->data);
1380 GNUNET_free (mesh_data);
1386 * Check if client has registered with the service and has not disconnected
1388 * @param client the client to check
1390 * @return non-NULL if client exists in the global DLL
1392 static struct MeshClient *
1393 client_get (struct GNUNET_SERVER_Client *client)
1395 struct MeshClient *c;
1400 if (c->handle == client)
1409 * Checks if a given client has subscribed to certain message type
1411 * @param message_type Type of message to check
1412 * @param c Client to check
1414 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1416 * FIXME: use of crypto_hash slows it down
1417 * The hash function alone takes 8-10us out of the ~55us for the whole
1418 * process of retransmitting the message from one local client to another.
1419 * Find faster implementation!
1422 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1424 struct GNUNET_HashCode hc;
1426 if (NULL == c->types)
1429 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1430 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1435 * Check whether client wants traffic from a tunnel.
1437 * @param c Client to check.
1438 * @param t Tunnel to be found.
1440 * @return GNUNET_YES if client knows tunnel.
1442 * TODO look in client hashmap
1445 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1449 for (i = 0; i < t->nclients; i++)
1450 if (t->clients[i] == c)
1457 * Check whether client has been informed about a tunnel.
1459 * @param c Client to check.
1460 * @param t Tunnel to be found.
1462 * @return GNUNET_YES if client knows tunnel.
1464 * TODO look in client hashmap
1467 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1471 for (i = 0; i < t->nignore; i++)
1472 if (t->ignore[i] == c)
1474 return client_wants_tunnel(c, t);
1479 * Marks a client as uninterested in traffic from the tunnel, updating both
1480 * client and tunnel to reflect this.
1482 * @param c Client that doesn't want traffic anymore.
1483 * @param t Tunnel which should be ignored.
1485 * FIXME when to delete an incoming tunnel?
1488 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1490 struct GNUNET_HashCode hash;
1492 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1493 GNUNET_break (GNUNET_YES ==
1494 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1496 GNUNET_break (GNUNET_YES ==
1497 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1499 tunnel_delete_active_client (t, c);
1500 GNUNET_array_append (t->ignore, t->nignore, c);
1505 * Deletes a tunnel from a client (either owner or destination). To be used on
1506 * tunnel destroy, otherwise, use client_ignore_tunnel.
1508 * @param c Client whose tunnel to delete.
1509 * @param t Tunnel which should be deleted.
1512 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1514 struct GNUNET_HashCode hash;
1518 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1519 GNUNET_assert (GNUNET_YES ==
1520 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1526 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1528 GNUNET_assert (GNUNET_YES ==
1529 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1533 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1541 * Notify the owner of a tunnel that a peer has disconnected.
1543 * @param c Client (owner of tunnel).
1544 * @param t Tunnel this message is about.
1545 * @param peer_id Short ID of the disconnected peer.
1548 client_notify_peer_disconnected (struct MeshClient *c,
1549 struct MeshTunnel *t,
1550 GNUNET_PEER_Id peer_id)
1552 struct GNUNET_MESH_PeerControl msg;
1554 if (NULL == t->owner || NULL == nc)
1557 msg.header.size = htons (sizeof (msg));
1558 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1559 msg.tunnel_id = htonl (t->local_tid);
1560 GNUNET_PEER_resolve (peer_id, &msg.peer);
1561 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1562 &msg.header, GNUNET_NO);
1567 * Send the message to all clients that have subscribed to its type
1569 * @param msg Pointer to the message itself
1570 * @param payload Pointer to the payload of the message.
1571 * @param t The tunnel to whose clients this message goes.
1573 * @return number of clients this message was sent to
1576 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1577 const struct GNUNET_MessageHeader *payload,
1578 struct MeshTunnel *t)
1580 struct MeshClient *c;
1581 MESH_TunnelNumber *tid;
1584 char cbuf[htons (msg->size)];
1586 type = ntohs (payload->type);
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1589 GNUNET_MESH_DEBUG_M2S (type));
1591 memcpy (cbuf, msg, sizeof (cbuf));
1592 switch (htons (msg->type))
1594 struct GNUNET_MESH_Unicast *uc;
1595 struct GNUNET_MESH_Multicast *mc;
1596 struct GNUNET_MESH_ToOrigin *to;
1598 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1599 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1602 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1603 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1606 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1607 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1615 for (count = 0, c = clients; c != NULL; c = c->next)
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1618 if (client_is_subscribed (type, c))
1620 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1624 *tid = htonl (t->local_tid);
1628 if (GNUNET_NO == client_knows_tunnel (c, t))
1630 /* This client doesn't know the tunnel */
1631 struct GNUNET_MESH_TunnelNotification tmsg;
1632 struct GNUNET_HashCode hash;
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1635 tmsg.header.size = htons (sizeof (tmsg));
1636 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1637 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1638 tmsg.tunnel_id = htonl (t->local_tid_dest);
1640 if (GNUNET_YES == t->speed_min)
1641 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1642 if (GNUNET_YES == t->nobuffer)
1643 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1644 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1645 &tmsg.header, GNUNET_NO);
1646 tunnel_add_client (t, c);
1647 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1649 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1650 c->incoming_tunnels, &hash, t,
1651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1653 *tid = htonl (t->local_tid_dest);
1656 /* Check if the client wants to get traffic from the tunnel */
1657 if (GNUNET_NO == client_wants_tunnel(c, t))
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1661 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1662 (struct GNUNET_MessageHeader
1663 *) cbuf, GNUNET_NO);
1672 * Notify the client that owns the tunnel that a peer has connected to it
1673 * (the requested path to it has been confirmed).
1675 * @param t Tunnel whose owner to notify
1676 * @param id Short id of the peer that has connected
1679 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1681 struct GNUNET_MESH_PeerControl pc;
1683 if (NULL == t->owner || GNUNET_YES == t->destroy)
1686 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1687 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1688 pc.tunnel_id = htonl (t->local_tid);
1689 GNUNET_PEER_resolve (id, &pc.peer);
1690 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1696 * Notify all clients (not depending on registration status) that the incoming
1697 * tunnel is no longer valid.
1699 * @param t Tunnel that was destroyed.
1702 send_clients_tunnel_destroy (struct MeshTunnel *t)
1704 struct GNUNET_MESH_TunnelMessage msg;
1706 msg.header.size = htons (sizeof (msg));
1707 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1708 msg.tunnel_id = htonl (t->local_tid_dest);
1709 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1714 * Notify clients of tunnel disconnections, if needed.
1715 * In case the origin disconnects, the destination clients get a tunnel destroy
1716 * notification. If the last destination disconnects (only one remaining client
1717 * in tunnel), the origin gets a (local ID) peer disconnected.
1718 * Note that the function must be called BEFORE removing the client from
1721 * @param t Tunnel that was destroyed.
1722 * @param c Client that disconnected.
1725 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1731 struct GNUNET_MESH_TunnelMessage msg;
1733 msg.header.size = htons (sizeof (msg));
1734 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1735 msg.tunnel_id = htonl (t->local_tid_dest);
1736 for (i = 0; i < t->nclients; i++)
1737 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1738 &msg.header, GNUNET_NO);
1740 // FIXME when to disconnect an incoming tunnel?
1741 else if (1 == t->nclients && NULL != t->owner)
1743 struct GNUNET_MESH_PeerControl msg;
1745 msg.header.size = htons (sizeof (msg));
1746 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1747 msg.tunnel_id = htonl (t->local_tid);
1748 msg.peer = my_full_id;
1749 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1750 &msg.header, GNUNET_NO);
1756 * Iterator over all the peers to remove the oldest not-used entry.
1758 * @param cls Closure (unsued).
1759 * @param key ID of the peer.
1760 * @param value Peer_Info of the peer.
1765 peer_info_timeout (void *cls,
1766 const struct GNUNET_HashCode *key,
1773 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1774 * and insert it in the appropiate structures if the peer is not known yet.
1776 * @param peer Full identity of the peer.
1778 * @return Existing or newly created peer info.
1780 static struct MeshPeerInfo *
1781 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1783 struct MeshPeerInfo *peer_info;
1785 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1786 if (NULL == peer_info)
1789 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1790 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1792 GNUNET_CONTAINER_multihashmap_iterate (peers,
1796 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1797 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1798 peer_info->id = GNUNET_PEER_intern (peer);
1800 peer_info->last_contact = GNUNET_TIME_absolute_get();
1807 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1808 * and insert it in the appropiate structures if the peer is not known yet.
1810 * @param peer Short identity of the peer.
1812 * @return Existing or newly created peer info.
1814 static struct MeshPeerInfo *
1815 peer_info_get_short (const GNUNET_PEER_Id peer)
1817 struct GNUNET_PeerIdentity id;
1819 GNUNET_PEER_resolve (peer, &id);
1820 return peer_info_get (&id);
1825 * Iterator to remove the tunnel from the list of tunnels a peer participates
1828 * @param cls Closure (tunnel info)
1829 * @param key GNUNET_PeerIdentity of the peer (unused)
1830 * @param value PeerInfo of the peer
1832 * @return always GNUNET_YES, to keep iterating
1835 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1837 struct MeshTunnel *t = cls;
1838 struct MeshPeerInfo *peer = value;
1841 for (i = 0; i < peer->ntunnels; i++)
1844 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1847 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1849 GNUNET_realloc (peer->tunnels,
1850 peer->ntunnels * sizeof(struct MeshTunnel *));
1859 * Core callback to write a pre-constructed data packet to core buffer
1861 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1862 * @param size Number of bytes available in buf.
1863 * @param buf Where the to write the message.
1865 * @return number of bytes written to buf
1868 send_core_data_raw (void *cls, size_t size, void *buf)
1870 struct MeshTransmissionDescriptor *info = cls;
1871 struct GNUNET_MessageHeader *msg;
1874 GNUNET_assert (NULL != info);
1875 GNUNET_assert (NULL != info->mesh_data);
1876 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1877 total_size = ntohs (msg->size);
1879 if (total_size > size)
1884 memcpy (buf, msg, total_size);
1885 data_descriptor_decrement_rc (info->mesh_data);
1892 * Sends an already built non-multicast message to a peer,
1893 * properly registrating all used resources.
1895 * @param message Message to send. Function makes a copy of it.
1896 * @param peer Short ID of the neighbor whom to send the message.
1897 * @param t Tunnel on which this message is transmitted.
1900 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1901 const struct GNUNET_PeerIdentity *peer,
1902 struct MeshTunnel *t)
1904 struct MeshTransmissionDescriptor *info;
1905 struct MeshPeerInfo *neighbor;
1906 struct MeshPeerPath *p;
1910 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1912 size = ntohs (message->size);
1913 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1914 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1915 info->mesh_data->data = GNUNET_malloc (size);
1916 memcpy (info->mesh_data->data, message, size);
1917 type = ntohs(message->type);
1920 struct GNUNET_MESH_Unicast *m;
1921 struct GNUNET_MESH_ToOrigin *to;
1923 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1924 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1925 m->ttl = htonl (ntohl (m->ttl) - 1);
1927 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1928 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1930 to->pid = htonl(t->bck_pid);
1932 info->mesh_data->data_len = size;
1933 info->mesh_data->reference_counter = 1;
1934 info->mesh_data->total_out = 1;
1935 neighbor = peer_info_get (peer);
1936 for (p = neighbor->path_head; NULL != p; p = p->next)
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 " %s IS NOT DIRECTLY CONNECTED\n",
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1952 for (p = neighbor->path_head; NULL != p; p = p->next)
1954 struct GNUNET_PeerIdentity debug_id;
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 " path with %u hops through:\n",
1960 for (i = 0; i < p->length; i++)
1962 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1965 i, GNUNET_i2s(&debug_id));
1969 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1971 " no direct connection to %s\n",
1973 GNUNET_free (info->mesh_data->data);
1974 GNUNET_free (info->mesh_data);
1978 info->peer = neighbor;
1979 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1990 * Sends a CREATE PATH message for a path to a peer, properly registrating
1991 * all used resources.
1993 * @param peer PeerInfo of the final peer for whom this path is being created.
1994 * @param p Path itself.
1995 * @param t Tunnel for which the path is created.
1998 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1999 struct MeshTunnel *t)
2001 struct GNUNET_PeerIdentity id;
2002 struct MeshPathInfo *path_info;
2003 struct MeshPeerInfo *neighbor;
2009 p = tree_get_path_to_peer (t->tree, peer->id);
2016 for (i = 0; i < p->length; i++)
2018 if (p->peers[i] == myid)
2021 if (i >= p->length - 1)
2027 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2029 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2030 path_info->path = p;
2032 neighbor = peer_info_get (&id);
2033 path_info->peer = neighbor;
2034 queue_add (path_info,
2035 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2036 sizeof (struct GNUNET_MESH_ManipulatePath) +
2037 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2044 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2046 * @param t Tunnel whose path to destroy.
2047 * @param destination Short ID of the peer to whom the path to destroy.
2050 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2052 struct MeshPeerPath *p;
2055 p = tree_get_path_to_peer (t->tree, destination);
2061 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2062 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2064 struct GNUNET_MESH_ManipulatePath *msg;
2065 struct GNUNET_PeerIdentity *pi;
2069 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2070 msg->header.size = htons (size);
2071 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2072 msg->tid = htonl (t->id.tid);
2073 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2074 for (i = 0; i < p->length; i++)
2076 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2078 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2085 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2087 * @param t Tunnel which to confirm.
2090 send_path_ack (struct MeshTunnel *t)
2092 struct MeshTransmissionDescriptor *info;
2093 struct GNUNET_PeerIdentity id;
2094 GNUNET_PEER_Id peer;
2096 peer = tree_get_predecessor (t->tree);
2097 GNUNET_PEER_resolve (peer, &id);
2098 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2099 info->origin = &t->id;
2100 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2101 GNUNET_assert (NULL != info->peer);
2104 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2105 sizeof (struct GNUNET_MESH_PathACK),
2112 * Try to establish a new connection to this peer.
2113 * Use the best path for the given tunnel.
2114 * If the peer doesn't have any path to it yet, try to get one.
2115 * If the peer already has some path, send a CREATE PATH towards it.
2117 * @param peer PeerInfo of the peer.
2118 * @param t Tunnel for which to create the path, if possible.
2121 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2123 struct MeshPeerPath *p;
2124 struct MeshPathInfo *path_info;
2126 if (NULL != peer->path_head)
2128 p = tree_get_path_to_peer (t->tree, peer->id);
2135 // FIXME always send create path to self
2138 send_create_path (peer, p, t);
2142 struct GNUNET_HashCode hash;
2145 send_client_peer_connected (t, myid);
2146 t->local_tid_dest = next_local_tid++;
2147 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2150 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2158 else if (NULL == peer->dhtget)
2160 struct GNUNET_PeerIdentity id;
2162 GNUNET_PEER_resolve (peer->id, &id);
2163 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2164 path_info->peer = peer;
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2167 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2168 peer->dhtgetcls = path_info;
2169 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2170 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2171 &id.hashPubKey, /* key to search */
2172 dht_replication_level, /* replication level */
2173 GNUNET_DHT_RO_RECORD_ROUTE |
2174 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2175 NULL, /* xquery */ // FIXME BLOOMFILTER
2176 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2177 &dht_get_id_handler, path_info);
2179 /* Otherwise, there is no path but the DHT get is already started. */
2184 * Task to delay the connection of a peer
2186 * @param cls Closure (path info with tunnel and peer to connect).
2187 * Will be free'd on exection.
2188 * @param tc TaskContext
2191 peer_info_connect_task (void *cls,
2192 const struct GNUNET_SCHEDULER_TaskContext *tc)
2194 struct MeshPathInfo *path_info = cls;
2196 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2198 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2203 peer_info_connect (path_info->peer, path_info->t);
2209 * Destroy the peer_info and free any allocated resources linked to it
2211 * @param pi The peer_info to destroy.
2213 * @return GNUNET_OK on success
2216 peer_info_destroy (struct MeshPeerInfo *pi)
2218 struct GNUNET_PeerIdentity id;
2219 struct MeshPeerPath *p;
2220 struct MeshPeerPath *nextp;
2222 GNUNET_PEER_resolve (pi->id, &id);
2223 GNUNET_PEER_change_rc (pi->id, -1);
2226 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2230 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2232 if (NULL != pi->dhtget)
2234 GNUNET_DHT_get_stop (pi->dhtget);
2235 GNUNET_free (pi->dhtgetcls);
2241 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2245 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2247 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2255 * Remove all paths that rely on a direct connection between p1 and p2
2256 * from the peer itself and notify all tunnels about it.
2258 * @param peer PeerInfo of affected peer.
2259 * @param p1 GNUNET_PEER_Id of one peer.
2260 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2263 * TODO: optimize (see below)
2266 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2269 struct MeshPeerPath *p;
2270 struct MeshPeerPath *aux;
2271 struct MeshPeerInfo *peer_d;
2273 unsigned int destroyed;
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2280 p = peer->path_head;
2284 for (i = 0; i < (p->length - 1); i++)
2286 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2287 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2289 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2300 for (i = 0; i < peer->ntunnels; i++)
2302 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2306 * Problem: one or more peers have been deleted from the tunnel tree.
2307 * We don't know who they are to try to add them again.
2308 * We need to try to find a new path for each of the disconnected peers.
2309 * Some of them might already have a path to reach them that does not
2310 * involve p1 and p2. Adding all anew might render in a better tree than
2311 * the trivial immediate fix.
2313 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2314 * its children will be reachable trough him.
2316 peer_d = peer_info_get_short (d);
2319 for (p = peer_d->path_head; NULL != p; p = p->next)
2321 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2329 /* No callback, as peer will be already disconnected and a connection
2330 * scheduled by tunnel_notify_connection_broken.
2332 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2336 peer_info_connect (peer_d, peer->tunnels[i]);
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2344 * Add the path to the peer and update the path used to reach it in case this
2347 * @param peer_info Destination peer to add the path to.
2348 * @param path New path to add. Last peer must be the peer in arg 1.
2349 * Path will be either used of freed if already known.
2350 * @param trusted Do we trust that this path is real?
2353 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2356 struct MeshPeerPath *aux;
2360 if ((NULL == peer_info) || (NULL == path))
2363 path_destroy (path);
2366 if (path->peers[path->length - 1] != peer_info->id)
2369 path_destroy (path);
2372 if (path->length <= 2 && GNUNET_NO == trusted)
2374 /* Only allow CORE to tell us about direct paths */
2375 path_destroy (path);
2378 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2379 for (l = 1; l < path->length; l++)
2381 if (path->peers[l] == myid)
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2384 for (l2 = 0; l2 < path->length - l; l2++)
2386 path->peers[l2] = path->peers[l + l2];
2391 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2396 struct GNUNET_PeerIdentity id;
2398 GNUNET_PEER_resolve (peer_info->id, &id);
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2400 path->length, GNUNET_i2s (&id));
2403 l = path_get_length (path);
2410 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2411 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2413 l2 = path_get_length (aux);
2416 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2417 peer_info->path_tail, aux, path);
2422 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2424 path_destroy (path);
2429 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2436 * Add the path to the origin peer and update the path used to reach it in case
2437 * this is the shortest.
2438 * The path is given in peer_info -> destination, therefore we turn the path
2439 * upside down first.
2441 * @param peer_info Peer to add the path to, being the origin of the path.
2442 * @param path New path to add after being inversed.
2443 * @param trusted Do we trust that this path is real?
2446 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2447 struct MeshPeerPath *path, int trusted)
2450 peer_info_add_path (peer_info, path, trusted);
2455 * Function called if the connection to the peer has been stalled for a while,
2456 * possibly due to a missed ACK. Poll the peer about its ACK status.
2458 * @param cls Closure (cinfo).
2459 * @param tc TaskContext.
2462 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2464 struct MeshTunnelChildInfo *cinfo = cls;
2465 struct GNUNET_MESH_Poll msg;
2466 struct GNUNET_PeerIdentity id;
2467 struct MeshTunnel *t;
2469 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2470 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2476 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2477 msg.header.size = htons (sizeof (msg));
2478 msg.tid = htonl (t->id.tid);
2479 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2480 msg.last_ack = htonl (cinfo->fwd_ack);
2482 GNUNET_PEER_resolve (cinfo->id, &id);
2483 send_prebuilt_message (&msg.header, &id, cinfo->t);
2484 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2486 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2487 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2488 &tunnel_poll, cinfo);
2493 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2494 * to obtain a local peer -> destination path and interning the peer ids.
2496 * @return Newly allocated and created path
2498 static struct MeshPeerPath *
2499 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2500 unsigned int get_path_length,
2501 const struct GNUNET_PeerIdentity *put_path,
2502 unsigned int put_path_length)
2504 struct MeshPeerPath *p;
2510 GNUNET_PEER_change_rc (myid, 1);
2511 i = get_path_length;
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2513 for (i--; i >= 0; i--)
2515 id = GNUNET_PEER_intern (&get_path[i]);
2516 if (p->length > 0 && id == p->peers[p->length - 1])
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2519 GNUNET_PEER_change_rc (id, -1);
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2524 GNUNET_i2s (&get_path[i]));
2526 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2527 p->peers[p->length - 1] = id;
2530 i = put_path_length;
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2532 for (i--; i >= 0; i--)
2534 id = GNUNET_PEER_intern (&put_path[i]);
2537 /* PUT path went through us, so discard the path up until now and start
2538 * from here to get a much shorter (and loop-free) path.
2543 if (p->length > 0 && id == p->peers[p->length - 1])
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2546 GNUNET_PEER_change_rc (id, -1);
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2551 GNUNET_i2s (&put_path[i]));
2553 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2554 p->peers[p->length - 1] = id;
2558 if (get_path_length > 0)
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2560 GNUNET_i2s (&get_path[0]));
2561 if (put_path_length > 0)
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2563 GNUNET_i2s (&put_path[0]));
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2566 for (i = 0; i < p->length; i++)
2568 struct GNUNET_PeerIdentity peer_id;
2570 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2572 GNUNET_i2s (&peer_id));
2580 * Adds a path to the peer_infos of all the peers in the path
2582 * @param p Path to process.
2583 * @param confirmed Whether we know if the path works or not.
2586 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2590 /* TODO: invert and add */
2591 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2592 for (i++; i < p->length; i++)
2594 struct MeshPeerInfo *aux;
2595 struct MeshPeerPath *copy;
2597 aux = peer_info_get_short (p->peers[i]);
2598 copy = path_duplicate (p);
2599 copy->length = i + 1;
2600 peer_info_add_path (aux, copy, GNUNET_NO);
2606 * Send keepalive packets for a peer
2608 * @param cls Closure (tunnel for which to send the keepalive).
2609 * @param tc Notification context.
2611 * TODO: implement explicit multicast keepalive?
2614 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2618 * Search for a tunnel among the incoming tunnels
2620 * @param tid the local id of the tunnel
2622 * @return tunnel handler, NULL if doesn't exist
2624 static struct MeshTunnel *
2625 tunnel_get_incoming (MESH_TunnelNumber tid)
2627 struct GNUNET_HashCode hash;
2629 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2630 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2631 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2636 * Search for a tunnel among the tunnels for a client
2638 * @param c the client whose tunnels to search in
2639 * @param tid the local id of the tunnel
2641 * @return tunnel handler, NULL if doesn't exist
2643 static struct MeshTunnel *
2644 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2646 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2648 return tunnel_get_incoming (tid);
2652 struct GNUNET_HashCode hash;
2654 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2655 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2661 * Search for a tunnel by global ID using PEER_ID
2663 * @param pi owner of the tunnel
2664 * @param tid global tunnel number
2666 * @return tunnel handler, NULL if doesn't exist
2668 static struct MeshTunnel *
2669 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2671 struct MESH_TunnelID id;
2672 struct GNUNET_HashCode hash;
2677 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2678 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2683 * Search for a tunnel by global ID using full PeerIdentities
2685 * @param oid owner of the tunnel
2686 * @param tid global tunnel number
2688 * @return tunnel handler, NULL if doesn't exist
2690 static struct MeshTunnel *
2691 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2693 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2698 * Delete an active client from the tunnel.
2704 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2708 for (i = 0; i < t->nclients; i++)
2710 if (t->clients[i] == c)
2712 t->clients[i] = t->clients[t->nclients - 1];
2713 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2714 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2716 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2724 * Delete an ignored client from the tunnel.
2730 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2734 for (i = 0; i < t->nignore; i++)
2736 if (t->ignore[i] == c)
2738 t->ignore[i] = t->ignore[t->nignore - 1];
2739 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2747 * Delete a client from the tunnel. It should be only done on
2748 * client disconnection, otherwise use client_ignore_tunnel.
2754 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2756 tunnel_delete_ignored_client (t, c);
2757 tunnel_delete_active_client (t, c);
2762 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2764 * Destroys queue elements of all waiting transmissions and frees all memory
2765 * used by the struct and its elements.
2767 * @param cls Closure (tunnel info).
2768 * @param key Hash of GNUNET_PEER_Id (unused).
2769 * @param value MeshTunnelChildInfo of the child.
2771 * @return always GNUNET_YES, to keep iterating
2774 tunnel_destroy_child (void *cls,
2775 const struct GNUNET_HashCode * key,
2778 struct MeshTunnelChildInfo *cinfo = value;
2779 struct MeshTunnel *t = cls;
2780 struct MeshPeerQueue *q;
2784 for (c = 0; c < cinfo->send_buffer_n; c++)
2786 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2787 q = cinfo->send_buffer[i];
2788 cinfo->send_buffer[i] = NULL;
2790 queue_destroy (q, GNUNET_YES);
2793 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2795 GNUNET_free_non_null (cinfo->send_buffer);
2796 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2798 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2799 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2801 GNUNET_free (cinfo);
2807 * Callback used to notify a client owner of a tunnel that a peer has
2808 * disconnected, most likely because of a path change.
2810 * @param cls Closure (tunnel this notification is about).
2811 * @param peer_id Short ID of disconnected peer.
2814 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2816 struct MeshTunnel *t = cls;
2817 struct MeshPeerInfo *peer;
2818 struct MeshPathInfo *path_info;
2820 client_notify_peer_disconnected (t->owner, t, peer_id);
2822 peer = peer_info_get_short (peer_id);
2823 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2824 path_info->peer = peer;
2826 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
2832 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2833 * needed rescources.
2834 * If peer already exists, reevaluate shortest path and change if different.
2836 * @param t Tunnel we want to add a new peer to
2837 * @param peer PeerInfo of the peer being added
2841 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2843 struct GNUNET_PeerIdentity id;
2844 struct MeshPeerPath *best_p;
2845 struct MeshPeerPath *p;
2846 unsigned int best_cost;
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2850 GNUNET_PEER_resolve (peer->id, &id);
2852 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2854 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2855 GNUNET_assert (GNUNET_OK ==
2856 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2861 if (NULL != (p = peer->path_head))
2864 best_cost = tree_get_path_cost (t->tree, p);
2867 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2874 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
2875 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2876 t->path_refresh_task =
2877 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2881 /* Start a DHT get */
2882 peer_info_connect (peer, t);
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2888 * Add a path to a tunnel which we don't own, just to remember the next hop.
2889 * If destination node was already in the tunnel, the first hop information
2890 * will be replaced with the new path.
2892 * @param t Tunnel we want to add a new peer to
2893 * @param p Path to add
2894 * @param own_pos Position of local node in path.
2898 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2899 unsigned int own_pos)
2901 struct GNUNET_PeerIdentity id;
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2904 GNUNET_assert (0 != own_pos);
2905 tree_add_path (t->tree, p, NULL, NULL);
2906 if (own_pos < p->length - 1)
2908 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2909 tree_update_first_hops (t->tree, myid, &id);
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2915 * Add a client to a tunnel, initializing all needed data structures.
2917 * @param t Tunnel to which add the client.
2918 * @param c Client which to add to the tunnel.
2921 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2923 struct MeshTunnelClientInfo clinfo;
2925 GNUNET_array_append (t->clients, t->nclients, c);
2926 clinfo.fwd_ack = t->fwd_pid + 1;
2927 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2928 clinfo.fwd_pid = t->fwd_pid;
2929 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2931 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2936 * Notifies a tunnel that a connection has broken that affects at least
2937 * some of its peers. Sends a notification towards the root of the tree.
2938 * In case the peer is the owner of the tree, notifies the client that owns
2939 * the tunnel and tries to reconnect.
2941 * @param t Tunnel affected.
2942 * @param p1 Peer that got disconnected from p2.
2943 * @param p2 Peer that got disconnected from p1.
2945 * @return Short ID of the peer disconnected (either p1 or p2).
2946 * 0 if the tunnel remained unaffected.
2948 static GNUNET_PEER_Id
2949 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2955 tree_notify_connection_broken (t->tree, p1, p2,
2956 &tunnel_notify_client_peer_disconnected,
2958 if (myid != p1 && myid != p2)
2964 if (tree_get_predecessor (t->tree) != 0)
2966 /* We are the peer still connected, notify owner of the disconnection. */
2967 struct GNUNET_MESH_PathBroken msg;
2968 struct GNUNET_PeerIdentity neighbor;
2970 msg.header.size = htons (sizeof (msg));
2971 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2972 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2973 msg.tid = htonl (t->id.tid);
2974 msg.peer1 = my_full_id;
2975 GNUNET_PEER_resolve (pid, &msg.peer2);
2976 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2977 send_prebuilt_message (&msg.header, &neighbor, t);
2985 * Send a multicast packet to a neighbor.
2987 * @param cls Closure (Info about the multicast packet)
2988 * @param neighbor_id Short ID of the neighbor to send the packet to.
2991 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2993 struct MeshData *mdata = cls;
2994 struct MeshTransmissionDescriptor *info;
2995 struct GNUNET_PeerIdentity neighbor;
2996 struct GNUNET_MessageHeader *msg;
2998 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3000 info->mesh_data = mdata;
3001 (mdata->reference_counter) ++;
3002 info->destination = neighbor_id;
3003 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3005 GNUNET_i2s (&neighbor));
3006 info->peer = peer_info_get (&neighbor);
3007 GNUNET_assert (NULL != info->peer);
3008 msg = (struct GNUNET_MessageHeader *) mdata->data;
3011 info->mesh_data->data_len,
3018 * Queue a message in a tunnel in multicast, sending a copy to each child node
3019 * down the local one in the tunnel tree.
3021 * @param t Tunnel in which to send the data.
3022 * @param msg Message to be sent.
3025 tunnel_send_multicast (struct MeshTunnel *t,
3026 const struct GNUNET_MessageHeader *msg)
3028 struct MeshData *mdata;
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3031 " sending a multicast packet...\n");
3033 mdata = GNUNET_malloc (sizeof (struct MeshData));
3034 mdata->data_len = ntohs (msg->size);
3036 mdata->data = GNUNET_malloc (mdata->data_len);
3037 memcpy (mdata->data, msg, mdata->data_len);
3038 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3040 struct GNUNET_MESH_Multicast *mcast;
3042 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3043 if (t->fwd_queue_n >= t->fwd_queue_max)
3046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3048 " message from %s!\n",
3049 GNUNET_i2s(&mcast->oid));
3050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3051 " message at %s!\n",
3052 GNUNET_i2s(&my_full_id));
3053 GNUNET_free (mdata->data);
3054 GNUNET_free (mdata);
3058 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3060 ntohl (mcast->ttl));
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3067 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3068 if (mdata->reference_counter == 0)
3070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3071 " no one to send data to\n");
3072 GNUNET_free (mdata->data);
3073 GNUNET_free (mdata);
3074 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3079 mdata->total_out = mdata->reference_counter;
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3082 " sending a multicast packet done\n");
3088 * Increase the SKIP value of all peers that
3089 * have not received a unicast message.
3091 * @param cls Closure (ID of the peer that HAS received the message).
3092 * @param key ID of the neighbor.
3093 * @param value Information about the neighbor.
3095 * @return GNUNET_YES to keep iterating.
3098 tunnel_add_skip (void *cls,
3099 const struct GNUNET_HashCode * key,
3102 struct GNUNET_PeerIdentity *neighbor = cls;
3103 struct MeshTunnelChildInfo *cinfo = value;
3105 /* TODO compare only pointers? key == neighbor? */
3106 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3116 * @brief Get neighbor's Flow Control information.
3118 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3119 * descendant of the local node in a certain tunnel.
3120 * If the info is not yet there (recently created path), creates the data struct
3121 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3124 * @param t Tunnel related.
3125 * @param peer Neighbor whose Flow Control info is needed.
3127 * @return Neighbor's Flow Control info.
3129 static struct MeshTunnelChildInfo *
3130 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3131 const struct GNUNET_PeerIdentity *peer)
3133 struct MeshTunnelChildInfo *cinfo;
3135 if (NULL == t->children_fc)
3138 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3144 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3145 cinfo->id = GNUNET_PEER_intern (peer);
3146 cinfo->skip = t->fwd_pid;
3149 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3150 cinfo->fwd_ack = t->fwd_pid + delta;
3151 cinfo->bck_ack = delta;
3152 cinfo->bck_pid = -1;
3154 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3155 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3157 cinfo->send_buffer =
3158 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3160 GNUNET_assert (GNUNET_OK ==
3161 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3171 * Get the Flow Control info of a client.
3173 * @param t Tunnel on which to look.
3174 * @param c Client whose ACK to get.
3176 * @return ACK value.
3178 static struct MeshTunnelClientInfo *
3179 tunnel_get_client_fc (struct MeshTunnel *t,
3180 struct MeshClient *c)
3184 for (i = 0; i < t->nclients; i++)
3186 if (t->clients[i] != c)
3188 return &t->clients_fc[i];
3191 return NULL; // avoid compiler / coverity complaints
3196 * Iterator to get the appropiate ACK value from all children nodes.
3198 * @param cls Closue (tunnel).
3199 * @param id Id of the child node.
3202 tunnel_get_child_fwd_ack (void *cls,
3205 struct GNUNET_PeerIdentity peer_id;
3206 struct MeshTunnelChildInfo *cinfo;
3207 struct MeshTunnelChildIteratorContext *ctx = cls;
3208 struct MeshTunnel *t = ctx->t;
3211 GNUNET_PEER_resolve (id, &peer_id);
3212 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3213 ack = cinfo->fwd_ack;
3216 if (GNUNET_NO == ctx->init)
3218 ctx->max_child_ack = ack;
3219 ctx->init = GNUNET_YES;
3222 if (GNUNET_YES == t->speed_min)
3224 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3228 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3235 * Get the maximum PID allowed to transmit to any
3236 * tunnel child of the local peer, depending on the tunnel
3237 * buffering/speed settings.
3241 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3244 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3246 struct MeshTunnelChildIteratorContext ctx;
3248 ctx.max_child_ack = 0;
3250 ctx.init = GNUNET_NO;
3251 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3253 if (0 == ctx.nchildren)
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 " tunnel has no children, no FWD ACK\n");
3260 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3261 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3263 return (int64_t) ctx.max_child_ack;
3268 * Set the FWD ACK value of a client in a particular tunnel.
3270 * @param t Tunnel affected.
3271 * @param c Client whose ACK to set.
3272 * @param ack ACK value.
3275 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3276 struct MeshClient *c,
3281 for (i = 0; i < t->nclients; i++)
3283 if (t->clients[i] != c)
3285 t->clients_fc[i].fwd_ack = ack;
3293 * Get the highest ACK value of all clients in a particular tunnel,
3294 * according to the buffering/speed settings.
3296 * @param t Tunnel on which to look.
3298 * @return Corresponding ACK value (max uint32_t).
3299 * If no clients are suscribed, -1LL.
3302 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3307 if (0 == t->nclients)
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 " tunnel has no clients, no FWD ACK\n");
3314 for (ack = -1LL, i = 0; i < t->nclients; i++)
3317 (GNUNET_YES == t->speed_min &&
3318 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3319 (GNUNET_NO == t->speed_min &&
3320 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3322 ack = t->clients_fc[i].fwd_ack;
3326 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3327 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3329 return (uint32_t) ack;
3334 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3335 * mode and the status of all children nodes.
3339 * @return Maximum PID allowed.
3342 tunnel_get_fwd_ack (struct MeshTunnel *t)
3346 uint32_t buffer_free;
3350 count = t->fwd_pid - t->skip;
3351 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3352 child_ack = tunnel_get_children_fwd_ack (t);
3353 client_ack = tunnel_get_clients_fwd_ack (t);
3354 if (GNUNET_YES == t->nobuffer)
3357 if (-1LL == child_ack)
3358 child_ack = client_ack;
3359 if (-1LL == child_ack)
3362 client_ack = child_ack = ack;
3367 ack = count + buffer_free; // Overflow? OK!
3369 if (-1LL == child_ack)
3371 // Node has no children, child_ack AND core buffer are irrelevant.
3372 if (-1LL == client_ack) // No children AND no clients? Not good!
3374 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3378 return (uint32_t) client_ack;
3380 if (-1LL == client_ack)
3384 if (GNUNET_YES == t->speed_min)
3386 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3387 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3391 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3392 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3395 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3396 count, buffer_free, child_ack, client_ack, ack);
3402 * Build a local ACK message and send it to a local client.
3404 * @param t Tunnel on which to send the ACK.
3405 * @param c Client to whom send the ACK.
3406 * @param ack Value of the ACK.
3409 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3411 struct GNUNET_MESH_LocalAck msg;
3413 msg.header.size = htons (sizeof (msg));
3414 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3415 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3416 msg.max_pid = htonl (ack);
3417 GNUNET_SERVER_notification_context_unicast(nc,
3424 * Build an ACK message and queue it to send to the given peer.
3426 * @param t Tunnel on which to send the ACK.
3427 * @param peer Peer to whom send the ACK.
3428 * @param ack Value of the ACK.
3431 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3433 struct GNUNET_MESH_ACK msg;
3435 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3436 msg.header.size = htons (sizeof (msg));
3437 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3438 msg.pid = htonl (ack);
3439 msg.tid = htonl (t->id.tid);
3441 send_prebuilt_message (&msg.header, peer, t);
3446 * Notify a the owner of a tunnel about how many more
3447 * payload packages will we accept on a given tunnel.
3449 * @param t Tunnel on which to send the ACK.
3452 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3457 "Sending client FWD ACK on tunnel %X\n",
3460 ack = tunnel_get_fwd_ack (t);
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3463 if (t->last_fwd_ack == ack)
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3470 t->last_fwd_ack = ack;
3471 send_local_ack (t, t->owner, ack);
3476 * Send an ACK informing the predecessor about the available buffer space.
3477 * In case there is no predecessor, inform the owning client.
3478 * If buffering is off, send only on behalf of children or self if endpoint.
3479 * If buffering is on, send when sent to children and buffer space is free.
3480 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3481 * the ACK itself goes "back" (towards root).
3483 * @param t Tunnel on which to send the ACK.
3484 * @param type Type of message that triggered the ACK transmission.
3487 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3489 struct GNUNET_PeerIdentity id;
3492 if (NULL != t->owner)
3494 tunnel_send_client_fwd_ack (t);
3497 /* Is it after unicast / multicast retransmission? */
3500 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3501 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3503 "ACK due to FWD DATA retransmission\n");
3504 if (GNUNET_YES == t->nobuffer)
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3510 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3511 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3513 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3514 t->force_ack = GNUNET_YES;
3520 /* Check if we need to transmit the ACK */
3521 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3522 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3523 GNUNET_NO == t->force_ack)
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3527 " t->qmax: %u, t->qn: %u\n",
3528 t->fwd_queue_max, t->fwd_queue_n);
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3530 " t->pid: %u, t->ack: %u\n",
3531 t->fwd_pid, t->last_fwd_ack);
3535 /* Ok, ACK might be necessary, what PID to ACK? */
3536 ack = tunnel_get_fwd_ack (t);
3538 /* If speed_min and not all children have ack'd, dont send yet */
3539 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3545 t->last_fwd_ack = ack;
3546 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3547 send_ack (t, &id, ack);
3549 t->force_ack = GNUNET_NO;
3554 * Iterator to send a child node a BCK ACK to allow him to send more
3557 * @param cls Closure (tunnel).
3558 * @param id Id of the child node.
3561 tunnel_send_child_bck_ack (void *cls,
3564 struct MeshTunnel *t = cls;
3565 struct MeshTunnelChildInfo *cinfo;
3566 struct GNUNET_PeerIdentity peer;
3569 GNUNET_PEER_resolve (id, &peer);
3570 cinfo = tunnel_get_neighbor_fc (t, &peer);
3571 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3573 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3576 " Not sending ACK, not needed\n");
3579 cinfo->bck_ack = ack;
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3582 " Sending BCK ACK %u (last sent: %u)\n",
3583 ack, cinfo->bck_ack);
3584 send_ack (t, &peer, ack);
3589 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3591 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3593 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3594 * - implement a size 1 buffer in each client_fc AND children_fc
3595 * to hold at least 1 message per "child".
3596 * problem: violates no buffer policy
3597 * - ack 0 and make "children" poll for transmission slots
3598 * problem: big overhead, extra latency even in low traffic
3601 * @param t Tunnel on which to send the BCK ACKs.
3604 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3607 unsigned int tunnel_delta;
3609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3611 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3614 /* Find client whom to allow to send to origin (with lowest buffer space) */
3615 for (i = 0; i < t->nclients; i++)
3617 struct MeshTunnelClientInfo *clinfo;
3620 clinfo = &t->clients_fc[i];
3621 delta = clinfo->bck_ack - clinfo->bck_pid;
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3623 t->clients[i]->id, delta);
3625 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3626 (GNUNET_YES == t->nobuffer && 0 == delta))
3630 ack = clinfo->bck_pid;
3631 ack += t->nobuffer ? 1 : tunnel_delta;
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 " sending ack to client %u: %u\n",
3634 t->clients[i]->id, ack);
3635 send_local_ack (t, t->clients[i], ack);
3636 clinfo->bck_ack = ack;
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3641 " not sending ack to client %u (td %u, d %u)\n",
3642 t->clients[i]->id, tunnel_delta, delta);
3649 * Send an ACK informing the children nodes and destination clients about
3650 * the available buffer space.
3651 * If buffering is off, send only on behalf of root (can be self).
3652 * If buffering is on, send when sent to predecessor and buffer space is free.
3653 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3654 * the ACK itself goes "forward" (towards children/clients).
3656 * @param t Tunnel on which to send the ACK.
3657 * @param type Type of message that triggered the ACK transmission.
3660 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3664 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3665 /* Is it after data to_origin retransmission? */
3668 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3669 if (GNUNET_YES == t->nobuffer)
3671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3672 " Not sending ACK, nobuffer\n");
3676 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3677 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3679 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3680 t->force_ack = GNUNET_YES;
3686 tunnel_send_clients_bck_ack (t);
3687 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3688 t->force_ack = GNUNET_NO;
3693 * @brief Re-initiate traffic to this peer if necessary.
3695 * Check if there is traffic queued towards this peer
3696 * and the core transmit handle is NULL (traffic was stalled).
3697 * If so, call core tmt rdy.
3699 * @param cls Closure (unused)
3700 * @param peer_id Short ID of peer to which initiate traffic.
3703 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3705 struct MeshPeerInfo *peer;
3706 struct GNUNET_PeerIdentity id;
3707 struct MeshPeerQueue *q;
3710 peer = peer_info_get_short(peer_id);
3711 if (NULL != peer->core_transmit)
3714 q = queue_get_next(peer);
3717 /* Might br multicast traffic already sent to this particular peer but
3718 * not to other children in this tunnel.
3719 * This way t->queue_n would be > 0 but the queue of this particular peer
3725 GNUNET_PEER_resolve (peer->id, &id);
3726 peer->core_transmit =
3727 GNUNET_CORE_notify_transmit_ready(core_handle,
3730 GNUNET_TIME_UNIT_FOREVER_REL,
3740 * @brief Allow transmission of FWD traffic on this tunnel
3742 * Check if there is traffic queued towards any children
3743 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3745 * @param t Tunnel on which to unlock FWD traffic.
3748 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3750 if (0 == t->fwd_queue_n)
3753 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3758 * @brief Allow transmission of BCK traffic on this tunnel
3760 * Check if there is traffic queued towards the root of the tree
3761 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3763 * @param t Tunnel on which to unlock BCK traffic.
3766 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3768 if (0 == t->bck_queue_n)
3771 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3776 * Send a message to all peers in this tunnel that the tunnel is no longer
3779 * @param t The tunnel whose peers to notify.
3780 * @param parent ID of the parent, in case the tree is already destroyed.
3783 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3785 struct GNUNET_MESH_TunnelDestroy msg;
3786 struct GNUNET_PeerIdentity id;
3788 msg.header.size = htons (sizeof (msg));
3789 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3790 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3791 msg.tid = htonl (t->id.tid);
3792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3793 " sending tunnel destroy for tunnel: %s [%X]\n",
3794 GNUNET_i2s (&msg.oid), t->id.tid);
3795 if (tree_count_children(t->tree) > 0)
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
3798 tunnel_send_multicast (t, &msg.header);
3801 parent = tree_get_predecessor (t->tree);
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3806 GNUNET_PEER_resolve (parent, &id);
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3808 " sending back to %s\n",
3810 send_prebuilt_message (&msg.header, &id, t);
3815 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3817 * @param cls Closure (Tunnel which to cancel).
3818 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3821 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3823 struct MeshTunnel *t = cls;
3824 struct MeshPeerInfo *peer_info;
3825 struct MeshPeerQueue *pq;
3826 struct MeshPeerQueue *next;
3828 peer_info = peer_info_get_short (neighbor_id);
3829 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3832 if (pq->tunnel == t)
3834 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3835 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3836 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3838 // Should have been removed on destroy children
3841 queue_destroy (pq, GNUNET_YES);
3844 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3846 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3847 peer_info->core_transmit = NULL;
3852 * Destroy the tunnel and free any allocated resources linked to it.
3854 * @param t the tunnel to destroy
3856 * @return GNUNET_OK on success
3859 tunnel_destroy (struct MeshTunnel *t)
3861 struct MeshClient *c;
3862 struct GNUNET_HashCode hash;
3873 struct GNUNET_PeerIdentity id;
3875 GNUNET_PEER_resolve (t->id.oid, &id);
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3877 GNUNET_i2s (&id), t->id.tid);
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3883 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3884 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3892 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3894 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3901 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3902 for (i = 0; i < t->nclients; i++)
3906 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3912 for (i = 0; i < t->nignore; i++)
3916 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3923 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3924 GNUNET_free_non_null (t->clients);
3925 GNUNET_free_non_null (t->ignore);
3926 GNUNET_free_non_null (t->clients_fc);
3928 if (NULL != t->peers)
3930 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3932 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3935 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3936 &tunnel_destroy_child,
3938 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3939 t->children_fc = NULL;
3941 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3942 tree_destroy (t->tree);
3944 if (NULL != t->regex_search)
3945 GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
3946 if (NULL != t->dht_get_type)
3947 GNUNET_DHT_get_stop (t->dht_get_type);
3948 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3949 GNUNET_SCHEDULER_cancel (t->timeout_task);
3950 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3951 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3954 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3959 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3962 * Tunnel is empty: destroy it.
3964 * @param cls Closure (Tunnel).
3965 * @param tc TaskContext.
3968 tunnel_destroy_empty_delayed (void *cls,
3969 const struct GNUNET_SCHEDULER_TaskContext *tc)
3971 struct MeshTunnel *t = cls;
3973 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3974 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3977 if (0 != t->nclients ||
3978 0 != tree_count_children (t->tree))
3983 struct GNUNET_PeerIdentity id;
3985 GNUNET_PEER_resolve (t->id.oid, &id);
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3987 "executing destruction of empty tunnel %s [%X]\n",
3988 GNUNET_i2s (&id), t->id.tid);
3992 tunnel_send_destroy (t, 0);
3993 if (0 == t->pending_messages)
3996 t->destroy = GNUNET_YES;
4001 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4003 * @param t Tunnel to destroy if empty.
4006 tunnel_destroy_empty (struct MeshTunnel *t)
4008 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4010 0 != tree_count_children (t->tree))
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4014 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4020 struct GNUNET_PeerIdentity id;
4022 GNUNET_PEER_resolve (t->id.oid, &id);
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4024 "scheduling destruction of empty tunnel %s [%X]\n",
4025 GNUNET_i2s (&id), t->id.tid);
4029 t->delayed_destroy =
4030 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4031 &tunnel_destroy_empty_delayed,
4037 * Create a new tunnel
4039 * @param owner Who is the owner of the tunnel (short ID).
4040 * @param tid Tunnel Number of the tunnel.
4041 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4042 * @param local Tunnel Number for the tunnel, for the client point of view.
4044 * @return A new initialized tunnel. NULL on error.
4046 static struct MeshTunnel *
4047 tunnel_new (GNUNET_PEER_Id owner,
4048 MESH_TunnelNumber tid,
4049 struct MeshClient *client,
4050 MESH_TunnelNumber local)
4052 struct MeshTunnel *t;
4053 struct GNUNET_HashCode hash;
4055 if (n_tunnels >= max_tunnels && NULL == client)
4058 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4061 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4062 t->bck_queue_max = t->fwd_queue_max;
4063 t->tree = tree_new (owner);
4065 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4066 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4067 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4068 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4069 t->local_tid = local;
4070 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4072 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4074 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4076 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4084 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4091 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4093 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4094 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4098 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4107 * Callback when removing children from a tunnel tree. Notify owner.
4109 * @param cls Closure (tunnel).
4110 * @param peer_id Short ID of the peer deleted.
4113 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4115 struct MeshTunnel *t = cls;
4117 client_notify_peer_disconnected (t->owner, t, peer_id);
4121 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4122 * that are no longer needed, as well as nodes of no longer reachable peers.
4123 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4125 * @param t Tunnel from which to remove the path.
4126 * @param peer Short id of the peer which should be removed.
4129 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4133 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137 "Tunnel %u [%u] has no more nodes\n",
4138 t->id.oid, t->id.tid);
4144 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4145 * client when the client disconnects. If the client is not the owner, the
4146 * owner will get notified if no more clients are in the tunnel and the client
4147 * get removed from the tunnel's list.
4149 * @param cls closure (client that is disconnecting)
4150 * @param key the hash of the local tunnel id (used to access the hashmap)
4151 * @param value the value stored at the key (tunnel to destroy)
4153 * @return GNUNET_OK, keep iterating.
4156 tunnel_destroy_iterator (void *cls,
4157 const struct GNUNET_HashCode * key,
4160 struct MeshTunnel *t = value;
4161 struct MeshClient *c = cls;
4163 send_client_tunnel_disconnect (t, c);
4166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4167 tunnel_delete_client (t, c);
4168 client_delete_tunnel (c, t);
4169 tunnel_destroy_empty (t);
4172 tunnel_send_destroy (t, 0);
4174 t->destroy = GNUNET_YES;
4181 * Timeout function, destroys tunnel if called
4183 * @param cls Closure (tunnel to destroy).
4184 * @param tc TaskContext
4187 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4189 struct MeshTunnel *t = cls;
4190 struct GNUNET_PeerIdentity id;
4192 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4193 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4195 GNUNET_PEER_resolve(t->id.oid, &id);
4196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4197 "Tunnel %s [%X] timed out. Destroying.\n",
4198 GNUNET_i2s(&id), t->id.tid);
4199 send_clients_tunnel_destroy (t);
4204 * Resets the tunnel timeout. Starts it if no timeout was running.
4206 * @param t Tunnel whose timeout to reset.
4208 * TODO use heap to improve efficiency of scheduler.
4211 tunnel_reset_timeout (struct MeshTunnel *t)
4213 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4214 GNUNET_SCHEDULER_cancel (t->timeout_task);
4216 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4217 (refresh_path_time, 4), &tunnel_timeout, t);
4221 /******************************************************************************/
4222 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4223 /******************************************************************************/
4226 * Function to send a create path packet to a peer.
4228 * @param cls closure
4229 * @param size number of bytes available in buf
4230 * @param buf where the callee should write the message
4231 * @return number of bytes written to buf
4234 send_core_path_create (void *cls, size_t size, void *buf)
4236 struct MeshPathInfo *info = cls;
4237 struct GNUNET_MESH_ManipulatePath *msg;
4238 struct GNUNET_PeerIdentity *peer_ptr;
4239 struct MeshTunnel *t = info->t;
4240 struct MeshPeerPath *p = info->path;
4245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4247 sizeof (struct GNUNET_MESH_ManipulatePath) +
4248 p->length * sizeof (struct GNUNET_PeerIdentity);
4250 if (size < size_needed || NULL == buf)
4255 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4256 msg->header.size = htons (size_needed);
4257 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4258 msg->tid = ntohl (t->id.tid);
4261 if (GNUNET_YES == t->speed_min)
4262 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4263 if (GNUNET_YES == t->nobuffer)
4264 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4265 msg->opt = htonl(opt);
4268 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4269 for (i = 0; i < p->length; i++)
4271 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4278 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4284 * Fill the core buffer
4286 * @param cls closure (data itself)
4287 * @param size number of bytes available in buf
4288 * @param buf where the callee should write the message
4290 * @return number of bytes written to buf
4293 send_core_data_multicast (void *cls, size_t size, void *buf)
4295 struct MeshTransmissionDescriptor *info = cls;
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4299 GNUNET_assert (NULL != info);
4300 GNUNET_assert (NULL != info->peer);
4301 total_size = info->mesh_data->data_len;
4302 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4304 if (total_size > size)
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4310 memcpy (buf, info->mesh_data->data, total_size);
4313 struct GNUNET_MESH_Multicast *mc;
4314 struct GNUNET_MessageHeader *mh;
4317 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4319 mc = (struct GNUNET_MESH_Multicast *) mh;
4320 mh = (struct GNUNET_MessageHeader *) &mc[1];
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4322 " multicast, payload type %s\n",
4323 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4325 " multicast, payload size %u\n", ntohs (mh->size));
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4330 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4334 data_descriptor_decrement_rc (info->mesh_data);
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4343 * Creates a path ack message in buf and frees all unused resources.
4345 * @param cls closure (MeshTransmissionDescriptor)
4346 * @param size number of bytes available in buf
4347 * @param buf where the callee should write the message
4348 * @return number of bytes written to buf
4351 send_core_path_ack (void *cls, size_t size, void *buf)
4353 struct MeshTransmissionDescriptor *info = cls;
4354 struct GNUNET_MESH_PathACK *msg = buf;
4356 GNUNET_assert (NULL != info);
4357 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4362 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4363 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4364 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4365 msg->tid = htonl (info->origin->tid);
4366 msg->peer_id = my_full_id;
4369 /* TODO add signature */
4371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4372 return sizeof (struct GNUNET_MESH_PathACK);
4377 * Free a transmission that was already queued with all resources
4378 * associated to the request.
4380 * @param queue Queue handler to cancel.
4381 * @param clear_cls Is it necessary to free associated cls?
4384 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4386 struct MeshTransmissionDescriptor *dd;
4387 struct MeshPathInfo *path_info;
4388 struct MeshTunnelChildInfo *cinfo;
4389 struct GNUNET_PeerIdentity id;
4393 if (GNUNET_YES == clear_cls)
4395 switch (queue->type)
4397 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4398 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4399 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4401 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4402 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4403 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4404 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4405 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4406 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4408 " prebuilt message\n");
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4411 GNUNET_MESH_DEBUG_M2S(queue->type));
4413 data_descriptor_decrement_rc (dd->mesh_data);
4415 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4417 path_info = queue->cls;
4418 path_destroy (path_info->path);
4422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4423 " type %s unknown!\n",
4424 GNUNET_MESH_DEBUG_M2S(queue->type));
4426 GNUNET_free_non_null (queue->cls);
4428 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4429 queue->peer->queue_tail,
4432 /* Delete from child_fc in the appropiate tunnel */
4433 max = queue->tunnel->fwd_queue_max;
4434 GNUNET_PEER_resolve (queue->peer->id, &id);
4435 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4438 for (i = 0; i < cinfo->send_buffer_n; i++)
4441 i2 = (cinfo->send_buffer_start + i) % max;
4442 if (cinfo->send_buffer[i2] == queue)
4444 /* Found corresponding entry in the send_buffer. Move all others back. */
4449 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4451 j2 = (cinfo->send_buffer_start + j) % max;
4452 j3 = (cinfo->send_buffer_start + j + 1) % max;
4453 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4456 cinfo->send_buffer[j3] = NULL;
4457 cinfo->send_buffer_n--;
4462 GNUNET_free (queue);
4467 * @brief Get the next transmittable message from the queue.
4469 * This will be the head, except in the case of being a data packet
4470 * not allowed by the destination peer.
4472 * @param peer Destination peer.
4474 * @return The next viable MeshPeerQueue element to send to that peer.
4475 * NULL when there are no transmittable messages.
4477 struct MeshPeerQueue *
4478 queue_get_next (const struct MeshPeerInfo *peer)
4480 struct MeshPeerQueue *q;
4481 struct MeshTunnel *t;
4482 struct MeshTransmissionDescriptor *info;
4483 struct MeshTunnelChildInfo *cinfo;
4484 struct GNUNET_MESH_Unicast *ucast;
4485 struct GNUNET_MESH_ToOrigin *to_orig;
4486 struct GNUNET_MESH_Multicast *mcast;
4487 struct GNUNET_PeerIdentity id;
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4492 for (q = peer->queue_head; NULL != q; q = q->next)
4496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4498 GNUNET_MESH_DEBUG_M2S(q->type));
4501 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4502 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4503 pid = ntohl (ucast->pid);
4504 GNUNET_PEER_resolve (info->peer->id, &id);
4505 cinfo = tunnel_get_neighbor_fc(t, &id);
4506 ack = cinfo->fwd_ack;
4508 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4509 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4510 pid = ntohl (to_orig->pid);
4513 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4514 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4515 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4517 // Not a multicast payload: multicast control traffic (destroy, etc)
4520 pid = ntohl (mcast->pid);
4521 GNUNET_PEER_resolve (info->peer->id, &id);
4522 cinfo = tunnel_get_neighbor_fc(t, &id);
4523 ack = cinfo->fwd_ack;
4526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4531 "********* ACK: %u, PID: %u\n",
4533 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4542 "********* NEXT!\n");
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4546 "********* nothing found\n");
4552 * Core callback to write a queued packet to core buffer
4554 * @param cls Closure (peer info).
4555 * @param size Number of bytes available in buf.
4556 * @param buf Where the to write the message.
4558 * @return number of bytes written to buf
4561 queue_send (void *cls, size_t size, void *buf)
4563 struct MeshPeerInfo *peer = cls;
4564 struct GNUNET_MessageHeader *msg;
4565 struct MeshPeerQueue *queue;
4566 struct MeshTunnel *t;
4567 struct MeshTunnelChildInfo *cinfo;
4568 struct GNUNET_PeerIdentity dst_id;
4571 peer->core_transmit = NULL;
4574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4575 queue = queue_get_next (peer);
4577 /* Queue has no internal mesh traffic nor sendable payload */
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4581 if (NULL == peer->queue_head)
4582 GNUNET_break (0); // Should've been canceled
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4587 GNUNET_PEER_resolve (peer->id, &dst_id);
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 "********* towards %s\n",
4590 GNUNET_i2s(&dst_id));
4591 /* Check if buffer size is enough for the message */
4592 if (queue->size > size)
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4595 "********* not enough room, reissue\n");
4596 peer->core_transmit =
4597 GNUNET_CORE_notify_transmit_ready (core_handle,
4600 GNUNET_TIME_UNIT_FOREVER_REL,
4607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4610 GNUNET_assert (0 < t->pending_messages);
4611 t->pending_messages--;
4612 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4616 "********* unicast: t->q (%u/%u)\n",
4617 t->fwd_queue_n, t->fwd_queue_max);
4619 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4626 switch (queue->type)
4629 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4630 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4631 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4632 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4633 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4635 "********* raw: %s\n",
4636 GNUNET_MESH_DEBUG_M2S (queue->type));
4638 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4639 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4640 data_size = send_core_data_raw (queue->cls, size, buf);
4641 msg = (struct GNUNET_MessageHeader *) buf;
4642 switch (ntohs (msg->type)) // Type of preconstructed message
4644 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4645 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4647 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4648 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4654 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4657 struct MeshTransmissionDescriptor *info = queue->cls;
4659 if ((1 == info->mesh_data->reference_counter
4660 && GNUNET_YES == t->speed_min)
4662 (info->mesh_data->total_out == info->mesh_data->reference_counter
4663 && GNUNET_NO == t->speed_min))
4665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4666 "********* considered sent\n");
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4672 "********* NOT considered sent yet\n");
4673 t->pending_messages++;
4676 data_size = send_core_data_multicast(queue->cls, size, buf);
4677 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4679 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4681 data_size = send_core_path_create (queue->cls, size, buf);
4683 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4685 data_size = send_core_path_ack (queue->cls, size, buf);
4687 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4689 data_size = send_core_data_multicast (queue->cls, size, buf);
4693 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4694 "********* type unknown: %u\n",
4698 switch (queue->type)
4700 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4701 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4702 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4703 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4704 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4708 "at pos %u (%p) != %p\n",
4709 cinfo->send_buffer_start,
4710 cinfo->send_buffer[cinfo->send_buffer_start],
4713 if (cinfo->send_buffer_n > 0)
4715 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4716 cinfo->send_buffer_n--;
4717 cinfo->send_buffer_start++;
4718 cinfo->send_buffer_start %= t->fwd_queue_max;
4729 /* Free queue, but cls was freed by send_core_* */
4730 queue_destroy (queue, GNUNET_NO);
4732 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4738 /* If more data in queue, send next */
4739 queue = queue_get_next(peer);
4742 struct GNUNET_PeerIdentity id;
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4745 GNUNET_PEER_resolve (peer->id, &id);
4746 peer->core_transmit =
4747 GNUNET_CORE_notify_transmit_ready(core_handle,
4750 GNUNET_TIME_UNIT_FOREVER_REL,
4758 if (NULL != peer->queue_head)
4760 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4761 "********* %s stalled\n",
4762 GNUNET_i2s(&my_full_id));
4764 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4765 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4766 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4768 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4769 &tunnel_poll, cinfo);
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4779 * @brief Queue and pass message to core when possible.
4781 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4782 * and accounts for it. In case the queue is full, the message is dropped and
4785 * Otherwise, message is treated as internal and allowed to go regardless of
4788 * @param cls Closure (@c type dependant). It will be used by queue_send to
4789 * build the message to be sent if not already prebuilt.
4790 * @param type Type of the message, 0 for a raw message.
4791 * @param size Size of the message.
4792 * @param dst Neighbor to send message to.
4793 * @param t Tunnel this message belongs to.
4796 queue_add (void *cls, uint16_t type, size_t size,
4797 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4799 struct MeshPeerQueue *queue;
4800 struct MeshTunnelChildInfo *cinfo;
4801 struct GNUNET_PeerIdentity id;
4807 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4808 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4810 n = &t->fwd_queue_n;
4811 max = &t->fwd_queue_max;
4813 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4815 n = &t->bck_queue_n;
4816 max = &t->bck_queue_max;
4823 GNUNET_STATISTICS_update(stats,
4824 "# messages dropped (buffer full)",
4826 return; // Drop message
4830 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4836 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4837 GNUNET_PEER_resolve (dst->id, &id);
4838 if (NULL == dst->core_transmit)
4840 dst->core_transmit =
4841 GNUNET_CORE_notify_transmit_ready (core_handle,
4844 GNUNET_TIME_UNIT_FOREVER_REL,
4850 t->pending_messages++;
4851 if (NULL == n) // Is this internal mesh traffic?
4854 // It's payload, keep track of buffer per peer.
4855 cinfo = tunnel_get_neighbor_fc(t, &id);
4856 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4857 if (NULL != cinfo->send_buffer[i])
4859 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4860 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4861 cinfo->send_buffer_start++;
4862 cinfo->send_buffer_start %= t->fwd_queue_max;
4866 cinfo->send_buffer_n++;
4868 cinfo->send_buffer[i] = queue;
4869 if (cinfo->send_buffer_n > t->fwd_queue_max)
4872 cinfo->send_buffer_n = t->fwd_queue_max;
4877 /******************************************************************************/
4878 /******************** MESH NETWORK HANDLERS **************************/
4879 /******************************************************************************/
4883 * Core handler for path creation
4885 * @param cls closure
4886 * @param message message
4887 * @param peer peer identity this notification is about
4889 * @return GNUNET_OK to keep the connection open,
4890 * GNUNET_SYSERR to close it (signal serious error)
4893 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4894 const struct GNUNET_MessageHeader *message)
4896 unsigned int own_pos;
4899 MESH_TunnelNumber tid;
4900 struct GNUNET_MESH_ManipulatePath *msg;
4901 struct GNUNET_PeerIdentity *pi;
4902 struct GNUNET_HashCode hash;
4903 struct MeshPeerPath *path;
4904 struct MeshPeerInfo *dest_peer_info;
4905 struct MeshPeerInfo *orig_peer_info;
4906 struct MeshTunnel *t;
4908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4909 "Received a path create msg [%s]\n",
4910 GNUNET_i2s (&my_full_id));
4911 size = ntohs (message->size);
4912 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4914 GNUNET_break_op (0);
4918 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4919 if (size % sizeof (struct GNUNET_PeerIdentity))
4921 GNUNET_break_op (0);
4924 size /= sizeof (struct GNUNET_PeerIdentity);
4927 GNUNET_break_op (0);
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4931 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4933 tid = ntohl (msg->tid);
4934 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4936 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4937 t = tunnel_get (pi, tid);
4938 if (NULL == t) // FIXME only for INCOMING tunnels?
4942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4943 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4946 // FIXME notify failure
4949 opt = ntohl (msg->opt);
4950 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4951 GNUNET_YES : GNUNET_NO;
4952 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4954 t->nobuffer = GNUNET_YES;
4955 t->last_fwd_ack = t->fwd_pid + 1;
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4958 " speed_min: %d, nobuffer:%d\n",
4959 t->speed_min, t->nobuffer);
4961 if (GNUNET_YES == t->nobuffer)
4963 t->bck_queue_max = 1;
4964 t->fwd_queue_max = 1;
4967 // FIXME only assign a local tid if a local client is interested (on demand)
4968 while (NULL != tunnel_get_incoming (next_local_tid))
4969 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4970 t->local_tid_dest = next_local_tid++;
4971 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4974 tunnel_reset_timeout (t);
4975 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4977 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4986 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4987 if (NULL == dest_peer_info)
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4990 " Creating PeerInfo for destination.\n");
4991 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4992 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4993 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4997 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4998 if (NULL == orig_peer_info)
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5001 " Creating PeerInfo for origin.\n");
5002 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5003 orig_peer_info->id = GNUNET_PEER_intern (pi);
5004 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5005 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5008 path = path_new (size);
5010 for (i = 0; i < size; i++)
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5013 GNUNET_i2s (&pi[i]));
5014 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5015 if (path->peers[i] == myid)
5018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5021 /* cannot be self, must be 'not found' */
5022 /* create path: self not found in path through self */
5023 GNUNET_break_op (0);
5024 path_destroy (path);
5028 path_add_to_peers (path, GNUNET_NO);
5029 tunnel_add_path (t, path, own_pos);
5030 if (own_pos == size - 1)
5032 /* It is for us! Send ack. */
5033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5034 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5035 if (NULL == t->peers)
5037 /* New tunnel! Notify clients on first payload message. */
5038 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5040 GNUNET_break (GNUNET_SYSERR !=
5041 GNUNET_CONTAINER_multihashmap_put (t->peers,
5042 &my_full_id.hashPubKey,
5045 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5050 struct MeshPeerPath *path2;
5052 /* It's for somebody else! Retransmit. */
5053 path2 = path_duplicate (path);
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5055 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5056 path2 = path_duplicate (path);
5057 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5058 send_create_path (dest_peer_info, path, t);
5065 * Core handler for path destruction
5067 * @param cls closure
5068 * @param message message
5069 * @param peer peer identity this notification is about
5071 * @return GNUNET_OK to keep the connection open,
5072 * GNUNET_SYSERR to close it (signal serious error)
5075 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5076 const struct GNUNET_MessageHeader *message)
5078 struct GNUNET_MESH_ManipulatePath *msg;
5079 struct GNUNET_PeerIdentity *pi;
5080 struct MeshPeerPath *path;
5081 struct MeshTunnel *t;
5082 unsigned int own_pos;
5086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5087 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
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);
5109 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5110 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5112 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5114 t = tunnel_get (pi, ntohl (msg->tid));
5117 /* TODO notify back: we don't know this tunnel */
5118 GNUNET_break_op (0);
5121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5122 path = path_new (size);
5124 for (i = 0; i < size; i++)
5126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5127 GNUNET_i2s (&pi[i]));
5128 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5129 if (path->peers[i] == myid)
5132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5133 if (own_pos < path->length - 1)
5134 send_prebuilt_message (message, &pi[own_pos + 1], t);
5136 send_client_tunnel_disconnect(t, NULL);
5138 tunnel_delete_peer (t, path->peers[path->length - 1]);
5139 path_destroy (path);
5145 * Core handler for notifications of broken paths
5147 * @param cls closure
5148 * @param message message
5149 * @param peer peer identity this notification is about
5151 * @return GNUNET_OK to keep the connection open,
5152 * GNUNET_SYSERR to close it (signal serious error)
5155 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5156 const struct GNUNET_MessageHeader *message)
5158 struct GNUNET_MESH_PathBroken *msg;
5159 struct MeshTunnel *t;
5161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5162 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5163 msg = (struct GNUNET_MESH_PathBroken *) message;
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5165 GNUNET_i2s (&msg->peer1));
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5167 GNUNET_i2s (&msg->peer2));
5168 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5171 GNUNET_break_op (0);
5174 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5175 GNUNET_PEER_search (&msg->peer2));
5182 * Core handler for tunnel destruction
5184 * @param cls closure
5185 * @param message message
5186 * @param peer peer identity this notification is about
5188 * @return GNUNET_OK to keep the connection open,
5189 * GNUNET_SYSERR to close it (signal serious error)
5192 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5193 const struct GNUNET_MessageHeader *message)
5195 struct GNUNET_MESH_TunnelDestroy *msg;
5196 struct MeshTunnel *t;
5197 GNUNET_PEER_Id parent;
5200 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5202 "Got a TUNNEL DESTROY packet from %s\n",
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5205 " for tunnel %s [%u]\n",
5206 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5207 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5208 /* Check signature */
5211 /* Probably already got the message from another path,
5212 * destroyed the tunnel and retransmitted to children.
5215 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5219 parent = tree_get_predecessor (t->tree);
5220 pid = GNUNET_PEER_search (peer);
5225 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5226 nc = tree_count_children (t->tree);
5227 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5230 "still in use: %u cl, %u ch\n",
5235 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5237 /* Tunnel was incoming, notify clients */
5238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5239 t->local_tid, t->local_tid_dest);
5240 send_clients_tunnel_destroy (t);
5242 tunnel_send_destroy (t, parent);
5243 t->destroy = GNUNET_YES;
5244 // TODO: add timeout to destroy the tunnel anyway
5250 * Core handler for mesh network traffic going from the origin to a peer
5252 * @param cls closure
5253 * @param peer peer identity this notification is about
5254 * @param message message
5255 * @return GNUNET_OK to keep the connection open,
5256 * GNUNET_SYSERR to close it (signal serious error)
5259 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5260 const struct GNUNET_MessageHeader *message)
5262 struct GNUNET_MESH_Unicast *msg;
5263 struct GNUNET_PeerIdentity *neighbor;
5264 struct MeshTunnelChildInfo *cinfo;
5265 struct MeshTunnel *t;
5266 GNUNET_PEER_Id dest_id;
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5274 size = ntohs (message->size);
5276 sizeof (struct GNUNET_MESH_Unicast) +
5277 sizeof (struct GNUNET_MessageHeader))
5282 msg = (struct GNUNET_MESH_Unicast *) message;
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5284 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5286 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5289 /* TODO notify back: we don't know this tunnel */
5290 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5291 GNUNET_break_op (0);
5294 pid = ntohl (msg->pid);
5295 if (t->fwd_pid == pid)
5297 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5299 " Already seen pid %u, DROPPING!\n", pid);
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5305 " pid %u not seen yet, forwarding\n", pid);
5308 t->skip += (pid - t->fwd_pid) - 1;
5311 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5313 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5314 GNUNET_break_op (0);
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5316 "Received PID %u, ACK %u\n",
5317 pid, t->last_fwd_ack);
5321 tunnel_reset_timeout (t);
5322 dest_id = GNUNET_PEER_search (&msg->destination);
5323 if (dest_id == myid)
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5326 " it's for us! sending to clients...\n");
5327 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5328 send_subscribed_clients (message, &msg[1].header, t);
5329 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5332 ttl = ntohl (msg->ttl);
5333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5336 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5338 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5342 " not for us, retransmitting...\n");
5344 neighbor = tree_get_first_hop (t->tree, dest_id);
5345 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5346 cinfo->fwd_pid = pid;
5347 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5350 if (GNUNET_YES == t->nobuffer &&
5351 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5353 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5354 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5355 GNUNET_break_op (0);
5358 send_prebuilt_message (message, neighbor, t);
5359 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5365 * Core handler for mesh network traffic going from the origin to all peers
5367 * @param cls closure
5368 * @param message message
5369 * @param peer peer identity this notification is about
5370 * @return GNUNET_OK to keep the connection open,
5371 * GNUNET_SYSERR to close it (signal serious error)
5373 * TODO: Check who we got this from, to validate route.
5376 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5377 const struct GNUNET_MessageHeader *message)
5379 struct GNUNET_MESH_Multicast *msg;
5380 struct MeshTunnel *t;
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5386 size = ntohs (message->size);
5387 if (sizeof (struct GNUNET_MESH_Multicast) +
5388 sizeof (struct GNUNET_MessageHeader) > size)
5390 GNUNET_break_op (0);
5393 msg = (struct GNUNET_MESH_Multicast *) message;
5394 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5398 /* TODO notify that we dont know that tunnel */
5399 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5400 GNUNET_break_op (0);
5403 pid = ntohl (msg->pid);
5404 if (t->fwd_pid == pid)
5406 /* already seen this packet, drop */
5407 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5409 " Already seen pid %u, DROPPING!\n", pid);
5410 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5416 " pid %u not seen yet, forwarding\n", pid);
5418 t->skip += (pid - t->fwd_pid) - 1;
5420 tunnel_reset_timeout (t);
5422 /* Transmit to locally interested clients */
5423 if (NULL != t->peers &&
5424 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5426 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5427 send_subscribed_clients (message, &msg[1].header, t);
5428 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5431 if (ntohl (msg->ttl) == 0)
5433 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5435 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5438 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5439 tunnel_send_multicast (t, message);
5445 * Core handler for mesh network traffic toward the owner of a tunnel
5447 * @param cls closure
5448 * @param message message
5449 * @param peer peer identity this notification is about
5451 * @return GNUNET_OK to keep the connection open,
5452 * GNUNET_SYSERR to close it (signal serious error)
5455 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5456 const struct GNUNET_MessageHeader *message)
5458 struct GNUNET_MESH_ToOrigin *msg;
5459 struct GNUNET_PeerIdentity id;
5460 struct MeshPeerInfo *peer_info;
5461 struct MeshTunnel *t;
5462 struct MeshTunnelChildInfo *cinfo;
5463 GNUNET_PEER_Id predecessor;
5467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5469 size = ntohs (message->size);
5470 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5471 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5473 GNUNET_break_op (0);
5476 msg = (struct GNUNET_MESH_ToOrigin *) message;
5477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5478 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5479 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5480 pid = ntohl (msg->pid);
5484 /* TODO notify that we dont know this tunnel (whom)? */
5485 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5487 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5488 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5492 cinfo = tunnel_get_neighbor_fc(t, peer);
5499 if (cinfo->bck_pid == pid)
5501 /* already seen this packet, drop */
5502 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5504 " Already seen pid %u, DROPPING!\n", pid);
5505 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5510 " pid %u not seen yet, forwarding\n", pid);
5511 cinfo->bck_pid = pid;
5513 if (NULL != t->owner)
5516 struct GNUNET_MESH_ToOrigin *copy;
5518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5519 " it's for us! sending to clients...\n");
5520 /* TODO signature verification */
5521 memcpy (cbuf, message, size);
5522 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5523 copy->tid = htonl (t->local_tid);
5525 copy->pid = htonl (t->bck_pid);
5526 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5527 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5528 ©->header, GNUNET_NO);
5529 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5533 " not for us, retransmitting...\n");
5535 peer_info = peer_info_get (&msg->oid);
5536 if (NULL == peer_info)
5538 /* unknown origin of tunnel */
5542 predecessor = tree_get_predecessor (t->tree);
5543 if (0 == predecessor)
5545 if (GNUNET_YES == t->destroy)
5547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5548 "to orig received on a dying tunnel %s [%X]\n",
5549 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5553 "unknown to origin at %s\n",
5554 GNUNET_i2s (&my_full_id));
5555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5559 "for tunnel %s [%X]\n",
5560 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5563 tree_debug (t->tree);
5566 GNUNET_PEER_resolve (predecessor, &id);
5567 send_prebuilt_message (message, &id, t);
5568 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5575 * Core handler for mesh network traffic point-to-point acks.
5577 * @param cls closure
5578 * @param message message
5579 * @param peer peer identity this notification is about
5581 * @return GNUNET_OK to keep the connection open,
5582 * GNUNET_SYSERR to close it (signal serious error)
5585 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5586 const struct GNUNET_MessageHeader *message)
5588 struct GNUNET_MESH_ACK *msg;
5589 struct MeshTunnel *t;
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5594 msg = (struct GNUNET_MESH_ACK *) message;
5596 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5600 /* TODO notify that we dont know this tunnel (whom)? */
5601 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5604 ack = ntohl (msg->pid);
5605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5607 /* Is this a forward or backward ACK? */
5608 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5610 struct MeshTunnelChildInfo *cinfo;
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5614 cinfo = tunnel_get_neighbor_fc (t, peer);
5615 cinfo->fwd_ack = ack;
5616 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5617 tunnel_unlock_fwd_queues (t);
5618 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5620 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5621 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5622 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5629 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5630 tunnel_unlock_bck_queue (t);
5637 * Core handler for mesh network traffic point-to-point ack polls.
5639 * @param cls closure
5640 * @param message message
5641 * @param peer peer identity this notification is about
5643 * @return GNUNET_OK to keep the connection open,
5644 * GNUNET_SYSERR to close it (signal serious error)
5647 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5648 const struct GNUNET_MessageHeader *message)
5650 struct GNUNET_MESH_Poll *msg;
5651 struct MeshTunnel *t;
5653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5656 msg = (struct GNUNET_MESH_Poll *) message;
5658 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5662 /* TODO notify that we dont know this tunnel (whom)? */
5663 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5664 GNUNET_break_op (0);
5668 /* Is this a forward or backward ACK? */
5669 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5671 struct MeshTunnelChildInfo *cinfo;
5673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5674 cinfo = tunnel_get_neighbor_fc (t, peer);
5675 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5676 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5681 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5688 * Core handler for path ACKs
5690 * @param cls closure
5691 * @param message message
5692 * @param peer peer identity this notification is about
5694 * @return GNUNET_OK to keep the connection open,
5695 * GNUNET_SYSERR to close it (signal serious error)
5698 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5699 const struct GNUNET_MessageHeader *message)
5701 struct GNUNET_MESH_PathACK *msg;
5702 struct GNUNET_PeerIdentity id;
5703 struct MeshPeerInfo *peer_info;
5704 struct MeshPeerPath *p;
5705 struct MeshTunnel *t;
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5708 GNUNET_i2s (&my_full_id));
5709 msg = (struct GNUNET_MESH_PathACK *) message;
5710 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5713 /* TODO notify that we don't know the tunnel */
5714 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5716 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5720 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5722 peer_info = peer_info_get (&msg->peer_id);
5723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5724 GNUNET_i2s (&msg->peer_id));
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5728 if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5731 "connect_by_string completed, stopping search\n");
5732 regex_cancel_search (t->regex_search);
5733 t->regex_search = NULL;
5736 /* Add paths to peers? */
5737 p = tree_get_path_to_peer (t->tree, peer_info->id);
5740 path_add_to_peers (p, GNUNET_YES);
5748 /* Message for us? */
5749 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5752 if (NULL == t->owner)
5754 GNUNET_break_op (0);
5757 if (NULL != t->dht_get_type)
5759 GNUNET_DHT_get_stop (t->dht_get_type);
5760 t->dht_get_type = NULL;
5762 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5764 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5765 send_client_peer_connected (t, peer_info->id);
5767 if (NULL != peer_info->dhtget)
5769 GNUNET_DHT_get_stop (peer_info->dhtget);
5770 peer_info->dhtget = NULL;
5775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5776 " not for us, retransmitting...\n");
5777 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5778 peer_info = peer_info_get (&msg->oid);
5779 send_prebuilt_message (message, &id, t);
5785 * Core handler for mesh keepalives.
5787 * @param cls closure
5788 * @param message message
5789 * @param peer peer identity this notification is about
5790 * @return GNUNET_OK to keep the connection open,
5791 * GNUNET_SYSERR to close it (signal serious error)
5793 * TODO: Check who we got this from, to validate route.
5796 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5797 const struct GNUNET_MessageHeader *message)
5799 struct GNUNET_MESH_TunnelKeepAlive *msg;
5800 struct MeshTunnel *t;
5802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5805 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5806 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5810 /* TODO notify that we dont know that tunnel */
5811 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
5816 tunnel_reset_timeout (t);
5818 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5819 tunnel_send_multicast (t, message);
5826 * Functions to handle messages from core
5828 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5829 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5830 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5831 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5832 sizeof (struct GNUNET_MESH_PathBroken)},
5833 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5834 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5835 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5836 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5837 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5838 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5839 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5840 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5841 sizeof (struct GNUNET_MESH_ACK)},
5842 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5843 sizeof (struct GNUNET_MESH_Poll)},
5844 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5845 sizeof (struct GNUNET_MESH_PathACK)},
5851 /******************************************************************************/
5852 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5853 /******************************************************************************/
5856 * deregister_app: iterator for removing each application registered by a client
5858 * @param cls closure
5859 * @param key the hash of the application id (used to access the hashmap)
5860 * @param value the value stored at the key (client)
5862 * @return GNUNET_OK on success
5865 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5867 struct MeshClient *c = cls;
5869 GNUNET_break (GNUNET_YES ==
5870 GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
5876 * notify_client_connection_failure: notify a client that the connection to the
5877 * requested remote peer is not possible (for instance, no route found)
5878 * Function called when the socket is ready to queue more data. "buf" will be
5879 * NULL and "size" zero if the socket was closed for writing in the meantime.
5881 * @param cls closure
5882 * @param size number of bytes available in buf
5883 * @param buf where the callee should write the message
5884 * @return number of bytes written to buf
5887 notify_client_connection_failure (void *cls, size_t size, void *buf)
5890 struct MeshPeerInfo *peer_info;
5891 struct GNUNET_MESH_PeerControl *msg;
5892 struct GNUNET_PeerIdentity id;
5894 if (0 == size && NULL == buf)
5896 // TODO retry? cancel?
5900 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5901 peer_info = (struct MeshPeerInfo *) cls;
5902 msg = (struct GNUNET_MESH_PeerControl *) buf;
5903 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5904 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5905 // msg->tunnel_id = htonl(peer_info->t->tid);
5906 GNUNET_PEER_resolve (peer_info->id, &id);
5907 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5915 * Send keepalive packets for a peer
5917 * @param cls Closure (tunnel for which to send the keepalive).
5918 * @param tc Notification context.
5921 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5923 struct MeshTunnel *t = cls;
5924 struct GNUNET_MESH_TunnelKeepAlive *msg;
5925 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5928 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5929 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5935 "sending keepalive for tunnel %d\n", t->id.tid);
5937 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5938 msg->header.size = htons (size);
5939 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5940 msg->oid = my_full_id;
5941 msg->tid = htonl (t->id.tid);
5942 tunnel_send_multicast (t, &msg->header);
5944 t->path_refresh_task =
5945 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5946 tunnel_reset_timeout(t);
5951 * Function to process paths received for a new peer addition. The recorded
5952 * paths form the initial tunnel, which can be optimized later.
5953 * Called on each result obtained for the DHT search.
5955 * @param cls closure
5956 * @param exp when will this value expire
5957 * @param key key of the result
5958 * @param get_path path of the get request
5959 * @param get_path_length lenght of get_path
5960 * @param put_path path of the put request
5961 * @param put_path_length length of the put_path
5962 * @param type type of the result
5963 * @param size number of bytes in data
5964 * @param data pointer to the result data
5966 * TODO: re-issue the request after certain time? cancel after X results?
5969 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5970 const struct GNUNET_HashCode * key,
5971 const struct GNUNET_PeerIdentity *get_path,
5972 unsigned int get_path_length,
5973 const struct GNUNET_PeerIdentity *put_path,
5974 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5975 size_t size, const void *data)
5977 struct MeshPathInfo *path_info = cls;
5978 struct MeshPeerPath *p;
5979 struct GNUNET_PeerIdentity pi;
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5983 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5986 p = path_build_from_dht (get_path, get_path_length,
5987 put_path, put_path_length);
5988 path_add_to_peers (p, GNUNET_NO);
5990 for (i = 0; i < path_info->peer->ntunnels; i++)
5992 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5993 peer_info_connect (path_info->peer, path_info->t);
6000 /******************************************************************************/
6001 /********************* MESH LOCAL HANDLES **************************/
6002 /******************************************************************************/
6006 * Handler for client disconnection
6008 * @param cls closure
6009 * @param client identification of the client; NULL
6010 * for the last call when the server is destroyed
6013 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6015 struct MeshClient *c;
6016 struct MeshClient *next;
6019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6029 if (c->handle != client)
6031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6037 GNUNET_SERVER_client_drop (c->handle);
6038 c->shutting_down = GNUNET_YES;
6039 GNUNET_assert (NULL != c->own_tunnels);
6040 GNUNET_assert (NULL != c->incoming_tunnels);
6041 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6042 &tunnel_destroy_iterator, c);
6043 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6044 &tunnel_destroy_iterator, c);
6045 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6046 &tunnel_destroy_iterator, c);
6047 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6048 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6049 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6051 /* deregister clients applications */
6052 if (NULL != c->apps)
6054 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
6055 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6057 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6058 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6060 GNUNET_SCHEDULER_cancel (announce_applications_task);
6061 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6063 if (NULL != c->types)
6064 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6065 for (i = 0; i < c->n_regex; i++)
6067 GNUNET_free (c->regexes[i].regex);
6068 if (NULL != c->regexes[i].h)
6069 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6071 GNUNET_free_non_null (c->regexes);
6072 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6073 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6075 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6078 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6087 * Handler for new clients
6089 * @param cls closure
6090 * @param client identification of the client
6091 * @param message the actual message, which includes messages the client wants
6094 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6095 const struct GNUNET_MessageHeader *message)
6097 struct GNUNET_MESH_ClientConnect *cc_msg;
6098 struct MeshClient *c;
6099 GNUNET_MESH_ApplicationType *a;
6106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6107 /* Check data sanity */
6108 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6109 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6110 ntypes = ntohs (cc_msg->types);
6111 napps = ntohs (cc_msg->applications);
6113 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6120 /* Create new client structure */
6121 c = GNUNET_malloc (sizeof (struct MeshClient));
6122 c->id = next_client_id++;
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6125 GNUNET_SERVER_client_keep (client);
6126 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6129 GNUNET_MESH_ApplicationType at;
6130 struct GNUNET_HashCode hc;
6132 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6133 for (i = 0; i < napps; i++)
6136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6137 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6138 /* store in clients hashmap */
6139 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6140 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6141 /* store in global hashmap, for announcements */
6142 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6145 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6146 announce_applications_task =
6147 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6153 struct GNUNET_HashCode hc;
6155 t = (uint16_t *) & a[napps];
6156 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6157 for (i = 0; i < ntypes; i++)
6160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6161 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6163 /* store in clients hashmap */
6164 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6166 /* store in global hashmap */
6167 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6172 " client has %u+%u subscriptions\n", napps, ntypes);
6174 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6175 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6176 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6177 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6178 GNUNET_SERVER_notification_context_add (nc, client);
6179 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6181 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6187 * Handler for clients announcing available services by a regular expression.
6189 * @param cls closure
6190 * @param client identification of the client
6191 * @param message the actual message, which includes messages the client wants
6194 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6195 const struct GNUNET_MessageHeader *message)
6197 const struct GNUNET_MESH_RegexAnnounce *msg;
6198 struct MeshRegexDescriptor rd;
6199 struct MeshClient *c;
6204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6206 /* Sanity check for client registration */
6207 if (NULL == (c = client_get (client)))
6210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6215 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6217 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6218 if (NULL != c->partial_regex)
6220 regex = c->partial_regex;
6221 offset = strlen (c->partial_regex);
6222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6223 " continuation, already have %u bytes\n",
6232 regex = GNUNET_realloc (regex, offset + len + 1);
6233 memcpy (®ex[offset], &msg[1], len);
6234 regex[offset + len] = '\0';
6235 if (0 == ntohs (msg->last))
6237 c->partial_regex = regex;
6238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6239 " not ended, stored %u bytes for later\n",
6241 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6245 rd.compression = ntohs (msg->compression_characters);
6247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6250 GNUNET_array_append (c->regexes, c->n_regex, rd);
6251 c->partial_regex = NULL;
6252 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6254 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6260 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6266 * Handler for requests of new tunnels
6268 * @param cls closure
6269 * @param client identification of the client
6270 * @param message the actual message
6273 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6274 const struct GNUNET_MessageHeader *message)
6276 struct GNUNET_MESH_TunnelMessage *t_msg;
6277 struct MeshTunnel *t;
6278 struct MeshClient *c;
6279 MESH_TunnelNumber tid;
6281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6283 /* Sanity check for client registration */
6284 if (NULL == (c = client_get (client)))
6287 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6292 /* Message sanity check */
6293 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6300 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6301 /* Sanity check for tunnel numbering */
6302 tid = ntohl (t_msg->tunnel_id);
6303 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6309 /* Sanity check for duplicate tunnel IDs */
6310 if (NULL != tunnel_get_by_local_id (c, tid))
6313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6317 while (NULL != tunnel_get_by_pi (myid, next_tid))
6318 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6319 t = tunnel_new (myid, next_tid++, c, tid);
6322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6327 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6329 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6330 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6339 * Handler for requests of deleting tunnels
6341 * @param cls closure
6342 * @param client identification of the client
6343 * @param message the actual message
6346 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6347 const struct GNUNET_MessageHeader *message)
6349 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6350 struct MeshClient *c;
6351 struct MeshTunnel *t;
6352 MESH_TunnelNumber tid;
6354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6355 "Got a DESTROY TUNNEL from client!\n");
6357 /* Sanity check for client registration */
6358 if (NULL == (c = client_get (client)))
6361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6366 /* Message sanity check */
6367 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6374 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6376 /* Retrieve tunnel */
6377 tid = ntohl (tunnel_msg->tunnel_id);
6378 t = tunnel_get_by_local_id(c, tid);
6381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6386 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6388 client_ignore_tunnel (c, t);
6389 tunnel_destroy_empty (t);
6390 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6393 send_client_tunnel_disconnect (t, c);
6394 client_delete_tunnel (c, t);
6396 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6398 tunnel_send_destroy (t, 0);
6399 t->destroy = GNUNET_YES;
6400 /* The tunnel will be destroyed when the last message is transmitted. */
6401 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6407 * Handler for requests of seeting tunnel's speed.
6409 * @param cls Closure (unused).
6410 * @param client Identification of the client.
6411 * @param message The actual message.
6414 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6415 const struct GNUNET_MessageHeader *message)
6417 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6418 struct MeshClient *c;
6419 struct MeshTunnel *t;
6420 MESH_TunnelNumber tid;
6422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6423 "Got a SPEED request from client!\n");
6425 /* Sanity check for client registration */
6426 if (NULL == (c = client_get (client)))
6429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6435 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6437 /* Retrieve tunnel */
6438 tid = ntohl (tunnel_msg->tunnel_id);
6439 t = tunnel_get_by_local_id(c, tid);
6442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6448 switch (ntohs(message->type))
6450 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6451 t->speed_min = GNUNET_YES;
6453 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6454 t->speed_min = GNUNET_NO;
6459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6464 * Handler for requests of seeting tunnel's buffering policy.
6466 * @param cls Closure (unused).
6467 * @param client Identification of the client.
6468 * @param message The actual message.
6471 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6472 const struct GNUNET_MessageHeader *message)
6474 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6475 struct MeshClient *c;
6476 struct MeshTunnel *t;
6477 MESH_TunnelNumber tid;
6479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6480 "Got a BUFFER request from client!\n");
6482 /* Sanity check for client registration */
6483 if (NULL == (c = client_get (client)))
6486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6491 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6493 /* Retrieve tunnel */
6494 tid = ntohl (tunnel_msg->tunnel_id);
6495 t = tunnel_get_by_local_id(c, tid);
6498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6504 switch (ntohs(message->type))
6506 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6507 t->nobuffer = GNUNET_NO;
6509 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6510 t->nobuffer = GNUNET_YES;
6516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6521 * Handler for connection requests to new peers
6523 * @param cls closure
6524 * @param client identification of the client
6525 * @param message the actual message (PeerControl)
6528 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6529 const struct GNUNET_MessageHeader *message)
6531 struct GNUNET_MESH_PeerControl *peer_msg;
6532 struct MeshPeerInfo *peer_info;
6533 struct MeshClient *c;
6534 struct MeshTunnel *t;
6535 MESH_TunnelNumber tid;
6537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6538 /* Sanity check for client registration */
6539 if (NULL == (c = client_get (client)))
6542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6547 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6549 /* Sanity check for message size */
6550 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6557 /* Tunnel exists? */
6558 tid = ntohl (peer_msg->tunnel_id);
6559 t = tunnel_get_by_local_id (c, tid);
6563 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6567 /* Does client own tunnel? */
6568 if (t->owner->handle != client)
6571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6575 GNUNET_i2s (&peer_msg->peer));
6576 peer_info = peer_info_get (&peer_msg->peer);
6578 tunnel_add_peer (t, peer_info);
6579 peer_info_connect (peer_info, t);
6581 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6587 * Handler for disconnection requests of peers in a tunnel
6589 * @param cls closure
6590 * @param client identification of the client
6591 * @param message the actual message (PeerControl)
6594 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6595 const struct GNUNET_MessageHeader *message)
6597 struct GNUNET_MESH_PeerControl *peer_msg;
6598 struct MeshPeerInfo *peer_info;
6599 struct MeshClient *c;
6600 struct MeshTunnel *t;
6601 MESH_TunnelNumber tid;
6603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6604 /* Sanity check for client registration */
6605 if (NULL == (c = client_get (client)))
6608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6613 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6615 /* Sanity check for message size */
6616 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6623 /* Tunnel exists? */
6624 tid = ntohl (peer_msg->tunnel_id);
6625 t = tunnel_get_by_local_id (c, tid);
6629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6634 /* Does client own tunnel? */
6635 if (t->owner->handle != client)
6638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6643 GNUNET_i2s (&peer_msg->peer));
6644 /* Is the peer in the tunnel? */
6646 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6647 if (NULL == peer_info)
6650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6654 /* Ok, delete peer from tunnel */
6655 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6656 &peer_msg->peer.hashPubKey);
6658 send_destroy_path (t, peer_info->id);
6659 tunnel_delete_peer (t, peer_info->id);
6660 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6665 * Handler for blacklist requests of peers in a tunnel
6667 * @param cls closure
6668 * @param client identification of the client
6669 * @param message the actual message (PeerControl)
6671 * FIXME implement DHT block bloomfilter
6674 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6675 const struct GNUNET_MessageHeader *message)
6677 struct GNUNET_MESH_PeerControl *peer_msg;
6678 struct MeshClient *c;
6679 struct MeshTunnel *t;
6680 MESH_TunnelNumber tid;
6682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6683 /* Sanity check for client registration */
6684 if (NULL == (c = client_get (client)))
6687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6692 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6694 /* Sanity check for message size */
6695 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6698 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6702 /* Tunnel exists? */
6703 tid = ntohl (peer_msg->tunnel_id);
6704 t = tunnel_get_by_local_id (c, tid);
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6713 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6714 GNUNET_PEER_intern(&peer_msg->peer));
6719 * Handler for unblacklist requests of peers in a tunnel
6721 * @param cls closure
6722 * @param client identification of the client
6723 * @param message the actual message (PeerControl)
6726 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6727 const struct GNUNET_MessageHeader *message)
6729 struct GNUNET_MESH_PeerControl *peer_msg;
6730 struct MeshClient *c;
6731 struct MeshTunnel *t;
6732 MESH_TunnelNumber tid;
6736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6737 /* Sanity check for client registration */
6738 if (NULL == (c = client_get (client)))
6741 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6746 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6748 /* Sanity check for message size */
6749 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6756 /* Tunnel exists? */
6757 tid = ntohl (peer_msg->tunnel_id);
6758 t = tunnel_get_by_local_id (c, tid);
6762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6767 /* if peer is not known, complain */
6768 pid = GNUNET_PEER_search (&peer_msg->peer);
6775 /* search and remove from list */
6776 for (i = 0; i < t->nblacklisted; i++)
6778 if (t->blacklisted[i] == pid)
6780 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6781 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6786 /* if peer hasn't been blacklisted, complain */
6792 * Handler for client traffic directed to one peer
6794 * @param cls closure
6795 * @param client identification of the client
6796 * @param message the actual message
6799 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6800 const struct GNUNET_MessageHeader *message)
6802 struct MeshClient *c;
6803 struct MeshTunnel *t;
6804 struct MeshPeerInfo *pi;
6805 struct GNUNET_MESH_Unicast *data_msg;
6806 MESH_TunnelNumber tid;
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6810 "Got a unicast request from a client!\n");
6812 /* Sanity check for client registration */
6813 if (NULL == (c = client_get (client)))
6816 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6821 data_msg = (struct GNUNET_MESH_Unicast *) message;
6823 /* Sanity check for message size */
6824 size = ntohs (message->size);
6825 if (sizeof (struct GNUNET_MESH_Unicast) +
6826 sizeof (struct GNUNET_MessageHeader) > size)
6829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6833 /* Tunnel exists? */
6834 tid = ntohl (data_msg->tid);
6835 t = tunnel_get_by_local_id (c, tid);
6839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6843 /* Is it a local tunnel? Then, does client own the tunnel? */
6844 if (t->owner->handle != client)
6847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6851 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6852 &data_msg->destination.hashPubKey);
6853 /* Is the selected peer in the tunnel? */
6857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6861 /* PID should be as expected */
6862 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6866 "Unicast PID, expected %u, got %u\n",
6867 t->fwd_pid + 1, ntohl (data_msg->pid));
6868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6872 /* Ok, everything is correct, send the message
6873 * (pretend we got it from a mesh peer)
6876 /* Work around const limitation */
6877 char buf[ntohs (message->size)] GNUNET_ALIGN;
6878 struct GNUNET_MESH_Unicast *copy;
6880 copy = (struct GNUNET_MESH_Unicast *) buf;
6881 memcpy (buf, data_msg, size);
6882 copy->oid = my_full_id;
6883 copy->tid = htonl (t->id.tid);
6884 copy->ttl = htonl (default_ttl);
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6886 " calling generic handler...\n");
6887 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6890 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6897 * Handler for client traffic directed to the origin
6899 * @param cls closure
6900 * @param client identification of the client
6901 * @param message the actual message
6904 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6905 const struct GNUNET_MessageHeader *message)
6907 struct GNUNET_MESH_ToOrigin *data_msg;
6908 struct MeshTunnelClientInfo *clinfo;
6909 struct MeshClient *c;
6910 struct MeshTunnel *t;
6911 MESH_TunnelNumber tid;
6914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6915 "Got a ToOrigin request from a client!\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 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6927 /* Sanity check for message size */
6928 size = ntohs (message->size);
6929 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6930 sizeof (struct GNUNET_MessageHeader) > size)
6933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6937 /* Tunnel exists? */
6938 tid = ntohl (data_msg->tid);
6939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6940 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6946 t = tunnel_get_by_local_id (c, tid);
6949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
6950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6956 /* It should be sent by someone who has this as incoming tunnel. */
6957 if (GNUNET_NO == client_knows_tunnel (c, t))
6960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6964 /* PID should be as expected */
6965 clinfo = tunnel_get_client_fc (t, c);
6966 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6969 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6970 "To Origin PID, expected %u, got %u\n",
6971 clinfo->bck_pid + 1,
6972 ntohl (data_msg->pid));
6973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6978 /* Ok, everything is correct, send the message
6979 * (pretend we got it from a mesh peer)
6982 char buf[ntohs (message->size)] GNUNET_ALIGN;
6983 struct GNUNET_MESH_ToOrigin *copy;
6985 /* Work around const limitation */
6986 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6987 memcpy (buf, data_msg, size);
6988 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6989 copy->tid = htonl (t->id.tid);
6990 copy->ttl = htonl (default_ttl);
6991 copy->pid = htonl (t->bck_pid + 1);
6993 copy->sender = my_full_id;
6994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6995 " calling generic handler...\n");
6996 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
6998 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7005 * Handler for client traffic directed to all peers in a tunnel
7007 * @param cls closure
7008 * @param client identification of the client
7009 * @param message the actual message
7012 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7013 const struct GNUNET_MessageHeader *message)
7015 struct MeshClient *c;
7016 struct MeshTunnel *t;
7017 struct GNUNET_MESH_Multicast *data_msg;
7018 MESH_TunnelNumber tid;
7020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7021 "Got a multicast request from a client!\n");
7023 /* Sanity check for client registration */
7024 if (NULL == (c = client_get (client)))
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7032 data_msg = (struct GNUNET_MESH_Multicast *) message;
7034 /* Sanity check for message size */
7035 if (sizeof (struct GNUNET_MESH_Multicast) +
7036 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7043 /* Tunnel exists? */
7044 tid = ntohl (data_msg->tid);
7045 t = tunnel_get_by_local_id (c, tid);
7049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7050 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7056 /* Does client own tunnel? */
7057 if (t->owner->handle != client)
7060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7064 /* PID should be as expected */
7065 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7069 "Multicast PID, expected %u, got %u\n",
7070 t->fwd_pid + 1, ntohl (data_msg->pid));
7071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7076 char buf[ntohs (message->size)] GNUNET_ALIGN;
7077 struct GNUNET_MESH_Multicast *copy;
7079 copy = (struct GNUNET_MESH_Multicast *) buf;
7080 memcpy (buf, message, ntohs (message->size));
7081 copy->oid = my_full_id;
7082 copy->tid = htonl (t->id.tid);
7083 copy->ttl = htonl (default_ttl);
7084 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7086 " calling generic handler...\n");
7087 handle_mesh_data_multicast (client, &my_full_id, ©->header);
7090 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7096 * Handler for client's ACKs for payload traffic.
7098 * @param cls Closure (unused).
7099 * @param client Identification of the client.
7100 * @param message The actual message.
7103 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7104 const struct GNUNET_MessageHeader *message)
7106 struct GNUNET_MESH_LocalAck *msg;
7107 struct MeshTunnel *t;
7108 struct MeshClient *c;
7109 MESH_TunnelNumber tid;
7112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7113 /* Sanity check for client registration */
7114 if (NULL == (c = client_get (client)))
7117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7122 msg = (struct GNUNET_MESH_LocalAck *) message;
7124 /* Tunnel exists? */
7125 tid = ntohl (msg->tunnel_id);
7126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7127 t = tunnel_get_by_local_id (c, tid);
7131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7137 ack = ntohl (msg->max_pid);
7138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7140 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7141 if (NULL != t->owner && t->owner->handle == client)
7143 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7145 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7149 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7150 tunnel_set_client_fwd_ack (t, c, ack);
7151 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7154 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7161 * Iterator over all peers to send a monitoring client info about a tunnel.
7163 * @param cls Closure (message being built).
7164 * @param key Key (hashed tunnel ID, unused).
7165 * @param value Peer info.
7167 * @return GNUNET_YES, to keep iterating.
7170 monitor_peers_iterator (void *cls,
7171 const struct GNUNET_HashCode * key,
7174 struct GNUNET_MESH_LocalMonitor *msg = cls;
7175 struct GNUNET_PeerIdentity *id;
7176 struct MeshPeerInfo *info = value;
7178 id = (struct GNUNET_PeerIdentity *) &msg[1];
7179 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7182 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7183 "* sending info about peer %s [%u]\n",
7184 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7192 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7194 * @param cls Closure (client handle).
7195 * @param key Key (hashed tunnel ID, unused).
7196 * @param value Tunnel info.
7198 * @return GNUNET_YES, to keep iterating.
7201 monitor_all_tunnels_iterator (void *cls,
7202 const struct GNUNET_HashCode * key,
7205 struct GNUNET_SERVER_Client *client = cls;
7206 struct MeshTunnel *t = value;
7207 struct GNUNET_MESH_LocalMonitor *msg;
7210 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7211 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7212 npeers * sizeof (struct GNUNET_PeerIdentity));
7213 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7214 msg->tunnel_id = htonl (t->id.tid);
7215 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7216 npeers * sizeof (struct GNUNET_PeerIdentity));
7217 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7219 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7220 monitor_peers_iterator,
7223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7224 "* sending info about tunnel %s [%u] (%u peers)\n",
7225 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7227 if (msg->npeers != npeers)
7230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7231 "Get tunnels fail: size %u - iter %u\n",
7232 npeers, msg->npeers);
7235 msg->npeers = htonl (npeers);
7236 GNUNET_SERVER_notification_context_unicast (nc, client,
7237 &msg->header, GNUNET_NO);
7243 * Handler for client's MONITOR request.
7245 * @param cls Closure (unused).
7246 * @param client Identification of the client.
7247 * @param message The actual message.
7250 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7251 const struct GNUNET_MessageHeader *message)
7253 struct MeshClient *c;
7255 /* Sanity check for client registration */
7256 if (NULL == (c = client_get (client)))
7259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7264 "Received get tunnels request from client %u\n",
7266 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7267 monitor_all_tunnels_iterator,
7269 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7270 "Get tunnels request from client %u completed\n",
7272 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7277 * Data needed to build a Monitor_Tunnel message.
7279 struct MeshMonitorTunnelContext
7282 * Partial message, including peer count.
7284 struct GNUNET_MESH_LocalMonitor *msg;
7287 * Hashmap with positions: peer->position.
7289 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7292 * Index of the parent of each peer in the message, realtive to the absolute
7293 * order in the array (can be in a previous message).
7295 uint32_t parents[1024];
7298 * Peers visited so far in the tree, aka position of the current peer.
7300 unsigned int npeers;
7303 * Client requesting the info.
7305 struct MeshClient *c;
7310 * Send a client a message about the structure of a tunnel.
7312 * @param ctx Context of the tunnel iteration, with info regarding the state
7313 * of the execution and the number of peers visited for this message.
7316 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7318 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7319 struct GNUNET_PeerIdentity *pid;
7320 unsigned int *parent;
7323 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7324 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7325 resp->header.size = htons (size);
7326 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7327 parent = (unsigned int *) &pid[resp->npeers];
7328 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7329 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7330 &resp->header, GNUNET_NO);
7334 * Iterator over a tunnel tree to build a message containing all peers
7335 * the in the tunnel, including relay nodes.
7337 * @param cls Closure (pointer to pointer of message being built).
7338 * @param peer Short ID of a peer.
7339 * @param parent Short ID of the @c peer 's parent.
7342 tunnel_tree_iterator (void *cls,
7343 GNUNET_PEER_Id peer,
7344 GNUNET_PEER_Id parent)
7346 struct MeshMonitorTunnelContext *ctx = cls;
7347 struct GNUNET_MESH_LocalMonitor *msg;
7348 struct GNUNET_PeerIdentity *pid;
7349 struct GNUNET_PeerIdentity ppid;
7352 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7353 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7354 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7355 &pid[msg->npeers].hashPubKey,
7356 (void *) (long) ctx->npeers,
7357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7358 GNUNET_PEER_resolve (parent, &ppid);
7359 ctx->parents[msg->npeers] =
7360 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7366 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7368 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7371 send_client_tunnel_info (ctx);
7378 * Handler for client's MONITOR_TUNNEL request.
7380 * @param cls Closure (unused).
7381 * @param client Identification of the client.
7382 * @param message The actual message.
7385 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7386 const struct GNUNET_MessageHeader *message)
7388 const struct GNUNET_MESH_LocalMonitor *msg;
7389 struct GNUNET_MESH_LocalMonitor *resp;
7390 struct MeshMonitorTunnelContext ctx;
7391 struct MeshClient *c;
7392 struct MeshTunnel *t;
7394 /* Sanity check for client registration */
7395 if (NULL == (c = client_get (client)))
7398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7402 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7403 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7404 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7407 ntohl (msg->tunnel_id));
7408 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7411 /* We don't know the tunnel */
7412 struct GNUNET_MESH_LocalMonitor warn;
7415 warn.npeers = htonl (UINT_MAX);
7416 GNUNET_SERVER_notification_context_unicast (nc, client,
7419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7423 /* Initialize context */
7424 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7428 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7431 /* Collect and send information */
7432 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7433 send_client_tunnel_info (&ctx);
7436 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7439 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7440 "Monitor tunnel request from client %u completed\n",
7442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7447 * Functions to handle messages from clients
7449 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7450 {&handle_local_new_client, NULL,
7451 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7452 {&handle_local_announce_regex, NULL,
7453 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7454 {&handle_local_tunnel_create, NULL,
7455 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7456 sizeof (struct GNUNET_MESH_TunnelMessage)},
7457 {&handle_local_tunnel_destroy, NULL,
7458 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7459 sizeof (struct GNUNET_MESH_TunnelMessage)},
7460 {&handle_local_tunnel_speed, NULL,
7461 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7462 sizeof (struct GNUNET_MESH_TunnelMessage)},
7463 {&handle_local_tunnel_speed, NULL,
7464 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7465 sizeof (struct GNUNET_MESH_TunnelMessage)},
7466 {&handle_local_tunnel_buffer, NULL,
7467 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7468 sizeof (struct GNUNET_MESH_TunnelMessage)},
7469 {&handle_local_tunnel_buffer, NULL,
7470 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7471 sizeof (struct GNUNET_MESH_TunnelMessage)},
7472 {&handle_local_connect_add, NULL,
7473 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7474 sizeof (struct GNUNET_MESH_PeerControl)},
7475 {&handle_local_connect_del, NULL,
7476 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7477 sizeof (struct GNUNET_MESH_PeerControl)},
7478 {&handle_local_blacklist, NULL,
7479 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7480 sizeof (struct GNUNET_MESH_PeerControl)},
7481 {&handle_local_unblacklist, NULL,
7482 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7483 sizeof (struct GNUNET_MESH_PeerControl)},
7484 {&handle_local_connect_by_string, NULL,
7485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7486 {&handle_local_unicast, NULL,
7487 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7488 {&handle_local_to_origin, NULL,
7489 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7490 {&handle_local_multicast, NULL,
7491 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7492 {&handle_local_ack, NULL,
7493 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7494 sizeof (struct GNUNET_MESH_LocalAck)},
7495 {&handle_local_get_tunnels, NULL,
7496 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7497 sizeof (struct GNUNET_MessageHeader)},
7498 {&handle_local_show_tunnel, NULL,
7499 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7500 sizeof (struct GNUNET_MESH_LocalMonitor)},
7506 * To be called on core init/fail.
7508 * @param cls service closure
7509 * @param server handle to the server for this service
7510 * @param identity the public identity of this peer
7513 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7514 const struct GNUNET_PeerIdentity *identity)
7517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7518 core_handle = server;
7519 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7525 GNUNET_i2s (identity));
7526 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7528 GNUNET_i2s (&my_full_id));
7529 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7531 GNUNET_abort(); // Try harder
7538 * Method called whenever a given peer connects.
7540 * @param cls closure
7541 * @param peer peer identity this notification is about
7544 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
7546 struct MeshPeerInfo *peer_info;
7547 struct MeshPeerPath *path;
7549 DEBUG_CONN ("Peer connected\n");
7550 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7551 peer_info = peer_info_get (peer);
7552 if (myid == peer_info->id)
7554 DEBUG_CONN (" (self)\n");
7559 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7561 path = path_new (2);
7562 path->peers[0] = myid;
7563 path->peers[1] = peer_info->id;
7564 GNUNET_PEER_change_rc (myid, 1);
7565 GNUNET_PEER_change_rc (peer_info->id, 1);
7566 peer_info_add_path (peer_info, path, GNUNET_YES);
7567 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7573 * Method called whenever a peer disconnects.
7575 * @param cls closure
7576 * @param peer peer identity this notification is about
7579 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7581 struct MeshPeerInfo *pi;
7582 struct MeshPeerQueue *q;
7583 struct MeshPeerQueue *n;
7585 DEBUG_CONN ("Peer disconnected\n");
7586 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7596 /* TODO try to reroute this traffic instead */
7597 queue_destroy(q, GNUNET_YES);
7600 if (NULL != pi->core_transmit)
7602 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
7603 pi->core_transmit = NULL;
7605 peer_info_remove_path (pi, pi->id, myid);
7608 DEBUG_CONN (" (self)\n");
7610 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7615 /******************************************************************************/
7616 /************************ MAIN FUNCTIONS ****************************/
7617 /******************************************************************************/
7620 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7622 * @param cls closure
7623 * @param key current key code
7624 * @param value value in the hash map
7625 * @return GNUNET_YES if we should continue to iterate,
7629 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7631 struct MeshTunnel *t = value;
7638 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7640 * @param cls closure
7641 * @param key current key code
7642 * @param value value in the hash map
7643 * @return GNUNET_YES if we should continue to iterate,
7647 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7649 struct MeshPeerInfo *p = value;
7650 struct MeshPeerQueue *q;
7651 struct MeshPeerQueue *n;
7659 queue_destroy(q, GNUNET_YES);
7663 peer_info_destroy (p);
7669 * Task run during shutdown.
7675 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7679 if (core_handle != NULL)
7681 GNUNET_CORE_disconnect (core_handle);
7686 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
7689 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7690 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7691 if (dht_handle != NULL)
7693 GNUNET_DHT_disconnect (dht_handle);
7698 GNUNET_SERVER_notification_context_destroy (nc);
7701 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7703 GNUNET_SCHEDULER_cancel (announce_id_task);
7704 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7706 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
7708 GNUNET_SCHEDULER_cancel (announce_applications_task);
7709 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7716 * Callback for hostkey read/generation.
7718 * @param cls Closure (Configuration handle).
7719 * @param pk The ECC private key.
7720 * @param emsg Error message, if any.
7723 key_generation_cb (void *cls,
7724 struct GNUNET_CRYPTO_EccPrivateKey *pk,
7727 const struct GNUNET_CONFIGURATION_Handle *c = cls;
7728 struct MeshPeerInfo *peer;
7729 struct MeshPeerPath *p;
7734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7735 _("Mesh service could not access hostkey: %s. Exiting.\n"),
7737 GNUNET_SCHEDULER_shutdown ();
7740 my_private_key = pk;
7741 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
7742 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7743 &my_full_id.hashPubKey);
7744 myid = GNUNET_PEER_intern (&my_full_id);
7745 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7746 "Mesh for peer [%s] starting\n",
7747 GNUNET_i2s(&my_full_id));
7749 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7750 NULL, /* Closure passed to MESH functions */
7751 &core_init, /* Call core_init once connected */
7752 &core_connect, /* Handle connects */
7753 &core_disconnect, /* remove peers on disconnects */
7754 NULL, /* Don't notify about all incoming messages */
7755 GNUNET_NO, /* For header only in notification */
7756 NULL, /* Don't notify about all outbound messages */
7757 GNUNET_NO, /* For header-only out notification */
7758 core_handlers); /* Register these handlers */
7760 if (core_handle == NULL)
7763 GNUNET_SCHEDULER_shutdown ();
7768 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7771 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7772 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7773 GNUNET_SERVER_disconnect_notify (server_handle,
7774 &handle_local_client_disconnect, NULL);
7778 clients_tail = NULL;
7781 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7782 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7784 /* Create a peer_info for the local peer */
7785 peer = peer_info_get (&my_full_id);
7788 GNUNET_PEER_change_rc (myid, 1);
7789 peer_info_add_path (peer, p, GNUNET_YES);
7790 GNUNET_SERVER_resume (server_handle);
7791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7796 * Process mesh requests.
7798 * @param cls closure
7799 * @param server the initialized server
7800 * @param c configuration to use
7803 run (void *cls, struct GNUNET_SERVER_Handle *server,
7804 const struct GNUNET_CONFIGURATION_Handle *c)
7808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7809 server_handle = server;
7812 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
7815 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7817 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7818 "mesh", "peer/privatekey");
7819 GNUNET_SCHEDULER_shutdown ();
7824 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7825 &refresh_path_time))
7827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7829 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7830 "mesh", "refresh path time");
7831 GNUNET_SCHEDULER_shutdown ();
7836 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7837 &app_announce_time))
7839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7841 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7842 "mesh", "app announce time");
7843 GNUNET_SCHEDULER_shutdown ();
7846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7847 "APP_ANNOUNCE_TIME %llu ms\n",
7848 app_announce_time.rel_value);
7850 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7855 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7856 "mesh", "id announce time");
7857 GNUNET_SCHEDULER_shutdown ();
7862 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7867 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7868 "mesh", "connect timeout");
7869 GNUNET_SCHEDULER_shutdown ();
7874 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7879 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7880 "mesh", "max msgs queue");
7881 GNUNET_SCHEDULER_shutdown ();
7886 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7891 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
7892 "mesh", "max tunnels");
7893 GNUNET_SCHEDULER_shutdown ();
7898 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7901 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7903 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7904 "mesh", "default ttl", 64);
7909 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
7912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7913 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7914 "mesh", "max peers", 1000);
7919 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7920 &dht_replication_level))
7922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7924 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
7925 "mesh", "dht replication level", 3);
7926 dht_replication_level = 3;
7929 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7930 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7931 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7932 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7933 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7935 dht_handle = GNUNET_DHT_connect (c, 64);
7936 if (NULL == dht_handle)
7940 stats = GNUNET_STATISTICS_create ("mesh", c);
7942 GNUNET_SERVER_suspend (server_handle);
7943 /* Scheduled the task to clean up when shutdown is called */
7944 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7946 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
7949 GNUNET_free (keyfile);
7954 * The main function for the mesh service.
7956 * @param argc number of arguments from the command line
7957 * @param argv command line arguments
7958 * @return 0 ok, 1 on error
7961 main (int argc, char *const *argv)
7966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7967 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7969 ret = (GNUNET_OK == r) ? 0 : 1;
7970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7974 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7975 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7976 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);